xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_fb.c (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1 /*	$NetBSD: amdgpu_fb.c,v 1.6 2021/04/24 23:37:00 thorpej Exp $	*/
2 
3 /*
4  * Copyright © 2007 David Airlie
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *     David Airlie
27  */
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: amdgpu_fb.c,v 1.6 2021/04/24 23:37:00 thorpej Exp $");
30 
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/fb.h>
34 
35 #include <drm/drmP.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_crtc_helper.h>
38 #include <drm/amdgpu_drm.h>
39 #include "amdgpu.h"
40 #include "cikd.h"
41 
42 #include <drm/drm_fb_helper.h>
43 
44 #include <linux/vga_switcheroo.h>
45 
46 #ifdef __NetBSD__
47 #include "amdgpufb.h"
48 #endif
49 
50 #include <linux/nbsd-namespace.h>
51 
52 /* object hierarchy -
53    this contains a helper + a amdgpu fb
54    the helper contains a pointer to amdgpu framebuffer baseclass.
55 */
56 struct amdgpu_fbdev {
57 	struct drm_fb_helper helper;
58 	struct amdgpu_framebuffer rfb;
59 	struct list_head fbdev_list;
60 	struct amdgpu_device *adev;
61 };
62 
63 #ifndef __NetBSD__
64 static struct fb_ops amdgpufb_ops = {
65 	.owner = THIS_MODULE,
66 	.fb_check_var = drm_fb_helper_check_var,
67 	.fb_set_par = drm_fb_helper_set_par,
68 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
69 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
70 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
71 	.fb_pan_display = drm_fb_helper_pan_display,
72 	.fb_blank = drm_fb_helper_blank,
73 	.fb_setcmap = drm_fb_helper_setcmap,
74 	.fb_debug_enter = drm_fb_helper_debug_enter,
75 	.fb_debug_leave = drm_fb_helper_debug_leave,
76 };
77 #endif
78 
79 
80 int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int bpp, bool tiled)
81 {
82 	int aligned = width;
83 	int pitch_mask = 0;
84 
85 	switch (bpp / 8) {
86 	case 1:
87 		pitch_mask = 255;
88 		break;
89 	case 2:
90 		pitch_mask = 127;
91 		break;
92 	case 3:
93 	case 4:
94 		pitch_mask = 63;
95 		break;
96 	}
97 
98 	aligned += pitch_mask;
99 	aligned &= ~pitch_mask;
100 	return aligned;
101 }
102 
103 static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
104 {
105 	struct amdgpu_bo *rbo = gem_to_amdgpu_bo(gobj);
106 	int ret;
107 
108 	ret = amdgpu_bo_reserve(rbo, false);
109 	if (likely(ret == 0)) {
110 		amdgpu_bo_kunmap(rbo);
111 		amdgpu_bo_unpin(rbo);
112 		amdgpu_bo_unreserve(rbo);
113 	}
114 	drm_gem_object_unreference_unlocked(gobj);
115 }
116 
117 static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
118 					 struct drm_mode_fb_cmd2 *mode_cmd,
119 					 struct drm_gem_object **gobj_p)
120 {
121 	struct amdgpu_device *adev = rfbdev->adev;
122 	struct drm_gem_object *gobj = NULL;
123 	struct amdgpu_bo *rbo = NULL;
124 	bool fb_tiled = false; /* useful for testing */
125 	u32 tiling_flags = 0;
126 	int ret;
127 	int aligned_size, size;
128 	int height = mode_cmd->height;
129 	u32 bpp, depth;
130 
131 	drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
132 
133 	/* need to align pitch with crtc limits */
134 	mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, bpp,
135 						  fb_tiled) * ((bpp + 1) / 8);
136 
137 	height = ALIGN(mode_cmd->height, 8);
138 	size = mode_cmd->pitches[0] * height;
139 	aligned_size = ALIGN(size, PAGE_SIZE);
140 	ret = amdgpu_gem_object_create(adev, aligned_size, 0,
141 				       AMDGPU_GEM_DOMAIN_VRAM,
142 				       AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
143 				       true, &gobj);
144 	if (ret) {
145 		printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
146 		       aligned_size);
147 		return -ENOMEM;
148 	}
149 	rbo = gem_to_amdgpu_bo(gobj);
150 
151 	if (fb_tiled)
152 		tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
153 
154 	ret = amdgpu_bo_reserve(rbo, false);
155 	if (unlikely(ret != 0))
156 		goto out_unref;
157 
158 	if (tiling_flags) {
159 		ret = amdgpu_bo_set_tiling_flags(rbo,
160 						 tiling_flags);
161 		if (ret)
162 			dev_err(adev->dev, "FB failed to set tiling flags\n");
163 	}
164 
165 
166 	ret = amdgpu_bo_pin_restricted(rbo, AMDGPU_GEM_DOMAIN_VRAM, 0, 0, NULL);
167 	if (ret) {
168 		amdgpu_bo_unreserve(rbo);
169 		goto out_unref;
170 	}
171 	ret = amdgpu_bo_kmap(rbo, NULL);
172 	amdgpu_bo_unreserve(rbo);
173 	if (ret) {
174 		goto out_unref;
175 	}
176 
177 	*gobj_p = gobj;
178 	return 0;
179 out_unref:
180 	amdgpufb_destroy_pinned_object(gobj);
181 	*gobj_p = NULL;
182 	return ret;
183 }
184 
185 static int amdgpufb_create(struct drm_fb_helper *helper,
186 			   struct drm_fb_helper_surface_size *sizes)
187 {
188 	struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
189 	struct amdgpu_device *adev = rfbdev->adev;
190 #ifndef __NetBSD__
191 	struct fb_info *info;
192 #endif
193 	struct drm_framebuffer *fb = NULL;
194 	struct drm_mode_fb_cmd2 mode_cmd;
195 	struct drm_gem_object *gobj = NULL;
196 	struct amdgpu_bo *rbo = NULL;
197 	int ret;
198 #ifndef __NetBSD__
199 	unsigned long tmp;
200 #endif
201 
202 	mode_cmd.width = sizes->surface_width;
203 	mode_cmd.height = sizes->surface_height;
204 
205 	if (sizes->surface_bpp == 24)
206 		sizes->surface_bpp = 32;
207 
208 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
209 							  sizes->surface_depth);
210 
211 	ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
212 	if (ret) {
213 		DRM_ERROR("failed to create fbcon object %d\n", ret);
214 		return ret;
215 	}
216 
217 	rbo = gem_to_amdgpu_bo(gobj);
218 
219 #ifdef __NetBSD__
220 	ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
221 	if (ret) {
222 		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
223 		goto out_unref;
224 	}
225 
226 	(void)memset(rbo->kptr, 0, amdgpu_bo_size(rbo));
227 
228     {
229 	static const struct amdgpufb_attach_args zero_afa;
230 	struct amdgpufb_attach_args afa = zero_afa;
231 
232 	afa.afa_fb_helper = helper;
233 	afa.afa_fb_sizes = *sizes;
234 	afa.afa_fb_ptr = rbo->kptr;
235 	afa.afa_fb_linebytes = mode_cmd.pitches[0];
236 
237 	helper->fbdev = config_found(adev->ddev->dev, &afa, NULL,
238 	    CFARG_IATTR, "amdgpufbbus",
239 	    CFARG_EOL);
240 	if (helper->fbdev == NULL) {
241 		DRM_ERROR("failed to attach amdgpufb\n");
242 		goto out_unref;
243 	}
244     }
245 	fb = &rfbdev->rfb.base;
246 	rfbdev->helper.fb = fb;
247 #else  /* __NetBSD__ */
248 	/* okay we have an object now allocate the framebuffer */
249 	info = drm_fb_helper_alloc_fbi(helper);
250 	if (IS_ERR(info)) {
251 		ret = PTR_ERR(info);
252 		goto out_unref;
253 	}
254 
255 	info->par = rfbdev;
256 	info->skip_vt_switch = true;
257 
258 	ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
259 	if (ret) {
260 		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
261 		goto out_destroy_fbi;
262 	}
263 
264 	fb = &rfbdev->rfb.base;
265 
266 	/* setup helper */
267 	rfbdev->helper.fb = fb;
268 
269 	memset_io(rbo->kptr, 0x0, amdgpu_bo_size(rbo));
270 
271 	strcpy(info->fix.id, "amdgpudrmfb");
272 
273 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
274 
275 	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
276 	info->fbops = &amdgpufb_ops;
277 
278 	tmp = amdgpu_bo_gpu_offset(rbo) - adev->mc.vram_start;
279 	info->fix.smem_start = adev->mc.aper_base + tmp;
280 	info->fix.smem_len = amdgpu_bo_size(rbo);
281 	info->screen_base = rbo->kptr;
282 	info->screen_size = amdgpu_bo_size(rbo);
283 
284 	drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
285 
286 	/* setup aperture base/size for vesafb takeover */
287 	info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
288 	info->apertures->ranges[0].size = adev->mc.aper_size;
289 
290 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
291 
292 	if (info->screen_base == NULL) {
293 		ret = -ENOSPC;
294 		goto out_destroy_fbi;
295 	}
296 
297 	DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
298 	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)adev->mc.aper_base);
299 	DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(rbo));
300 	DRM_INFO("fb depth is %d\n", fb->depth);
301 	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
302 
303 	vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
304 #endif
305 	return 0;
306 
307 #ifndef __NetBSD__
308 out_destroy_fbi:
309 	drm_fb_helper_release_fbi(helper);
310 #endif
311 out_unref:
312 	if (rbo) {
313 
314 	}
315 	if (fb && ret) {
316 		drm_gem_object_unreference(gobj);
317 		drm_framebuffer_unregister_private(fb);
318 		drm_framebuffer_cleanup(fb);
319 		kfree(fb);
320 	}
321 	return ret;
322 }
323 
324 void amdgpu_fb_output_poll_changed(struct amdgpu_device *adev)
325 {
326 	if (adev->mode_info.rfbdev)
327 		drm_fb_helper_hotplug_event(&adev->mode_info.rfbdev->helper);
328 }
329 
330 static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
331 {
332 	struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
333 #ifdef __NetBSD__
334 	int ret;
335 #endif
336 
337 #ifdef __NetBSD__
338 	/* XXX errno NetBSD->Linux */
339 	ret = -config_detach(rfbdev->helper.fbdev, DETACH_FORCE);
340 	if (ret) {
341 		DRM_ERROR("failed to detach amdgpufb: %d\n", ret);
342 		return ret;
343 	}
344 	rfbdev->helper.fbdev = NULL;
345 #else
346 	drm_fb_helper_unregister_fbi(&rfbdev->helper);
347 	drm_fb_helper_release_fbi(&rfbdev->helper);
348 #endif
349 
350 	if (rfb->obj) {
351 		amdgpufb_destroy_pinned_object(rfb->obj);
352 		rfb->obj = NULL;
353 	}
354 	drm_fb_helper_fini(&rfbdev->helper);
355 	drm_framebuffer_unregister_private(&rfb->base);
356 	drm_framebuffer_cleanup(&rfb->base);
357 
358 	return 0;
359 }
360 
361 /** Sets the color ramps on behalf of fbcon */
362 static void amdgpu_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
363 				      u16 blue, int regno)
364 {
365 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
366 
367 	amdgpu_crtc->lut_r[regno] = red >> 6;
368 	amdgpu_crtc->lut_g[regno] = green >> 6;
369 	amdgpu_crtc->lut_b[regno] = blue >> 6;
370 }
371 
372 /** Gets the color ramps on behalf of fbcon */
373 static void amdgpu_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
374 				      u16 *blue, int regno)
375 {
376 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
377 
378 	*red = amdgpu_crtc->lut_r[regno] << 6;
379 	*green = amdgpu_crtc->lut_g[regno] << 6;
380 	*blue = amdgpu_crtc->lut_b[regno] << 6;
381 }
382 
383 static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
384 	.gamma_set = amdgpu_crtc_fb_gamma_set,
385 	.gamma_get = amdgpu_crtc_fb_gamma_get,
386 	.fb_probe = amdgpufb_create,
387 };
388 
389 int amdgpu_fbdev_init(struct amdgpu_device *adev)
390 {
391 	struct amdgpu_fbdev *rfbdev;
392 	int bpp_sel = 32;
393 	int ret;
394 
395 	/* don't init fbdev on hw without DCE */
396 	if (!adev->mode_info.mode_config_initialized)
397 		return 0;
398 
399 	/* select 8 bpp console on low vram cards */
400 	if (adev->mc.real_vram_size <= (32*1024*1024))
401 		bpp_sel = 8;
402 
403 	rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
404 	if (!rfbdev)
405 		return -ENOMEM;
406 
407 	rfbdev->adev = adev;
408 	adev->mode_info.rfbdev = rfbdev;
409 
410 	drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
411 			&amdgpu_fb_helper_funcs);
412 
413 	ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
414 				 adev->mode_info.num_crtc,
415 				 AMDGPUFB_CONN_LIMIT);
416 	if (ret) {
417 		kfree(rfbdev);
418 		return ret;
419 	}
420 
421 	drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
422 
423 	/* disable all the possible outputs/crtcs before entering KMS mode */
424 	drm_helper_disable_unused_functions(adev->ddev);
425 
426 	drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
427 	return 0;
428 }
429 
430 void amdgpu_fbdev_fini(struct amdgpu_device *adev)
431 {
432 	if (!adev->mode_info.rfbdev)
433 		return;
434 
435 	amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
436 	kfree(adev->mode_info.rfbdev);
437 	adev->mode_info.rfbdev = NULL;
438 }
439 
440 void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
441 {
442 	if (adev->mode_info.rfbdev)
443 		drm_fb_helper_set_suspend(&adev->mode_info.rfbdev->helper,
444 			state);
445 }
446 
447 int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
448 {
449 	struct amdgpu_bo *robj;
450 	int size = 0;
451 
452 	if (!adev->mode_info.rfbdev)
453 		return 0;
454 
455 	robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj);
456 	size += amdgpu_bo_size(robj);
457 	return size;
458 }
459 
460 bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
461 {
462 	if (!adev->mode_info.rfbdev)
463 		return false;
464 	if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj))
465 		return true;
466 	return false;
467 }
468 
469 void amdgpu_fbdev_restore_mode(struct amdgpu_device *adev)
470 {
471 	struct amdgpu_fbdev *afbdev = adev->mode_info.rfbdev;
472 	struct drm_fb_helper *fb_helper;
473 	int ret;
474 
475 	if (!afbdev)
476 		return;
477 
478 	fb_helper = &afbdev->helper;
479 
480 	ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
481 	if (ret)
482 		DRM_DEBUG("failed to restore crtc mode\n");
483 }
484