xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_fb.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: amdgpu_fb.c,v 1.5 2020/02/14 04:37:09 riastradh 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.5 2020/02/14 04:37:09 riastradh 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_ia(adev->ddev->dev, "amdgpufbbus", &afa,
238 	    NULL);
239 	if (helper->fbdev == NULL) {
240 		DRM_ERROR("failed to attach amdgpufb\n");
241 		goto out_unref;
242 	}
243     }
244 	fb = &rfbdev->rfb.base;
245 	rfbdev->helper.fb = fb;
246 #else  /* __NetBSD__ */
247 	/* okay we have an object now allocate the framebuffer */
248 	info = drm_fb_helper_alloc_fbi(helper);
249 	if (IS_ERR(info)) {
250 		ret = PTR_ERR(info);
251 		goto out_unref;
252 	}
253 
254 	info->par = rfbdev;
255 	info->skip_vt_switch = true;
256 
257 	ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
258 	if (ret) {
259 		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
260 		goto out_destroy_fbi;
261 	}
262 
263 	fb = &rfbdev->rfb.base;
264 
265 	/* setup helper */
266 	rfbdev->helper.fb = fb;
267 
268 	memset_io(rbo->kptr, 0x0, amdgpu_bo_size(rbo));
269 
270 	strcpy(info->fix.id, "amdgpudrmfb");
271 
272 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
273 
274 	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
275 	info->fbops = &amdgpufb_ops;
276 
277 	tmp = amdgpu_bo_gpu_offset(rbo) - adev->mc.vram_start;
278 	info->fix.smem_start = adev->mc.aper_base + tmp;
279 	info->fix.smem_len = amdgpu_bo_size(rbo);
280 	info->screen_base = rbo->kptr;
281 	info->screen_size = amdgpu_bo_size(rbo);
282 
283 	drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
284 
285 	/* setup aperture base/size for vesafb takeover */
286 	info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
287 	info->apertures->ranges[0].size = adev->mc.aper_size;
288 
289 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
290 
291 	if (info->screen_base == NULL) {
292 		ret = -ENOSPC;
293 		goto out_destroy_fbi;
294 	}
295 
296 	DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
297 	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)adev->mc.aper_base);
298 	DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(rbo));
299 	DRM_INFO("fb depth is %d\n", fb->depth);
300 	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
301 
302 	vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
303 #endif
304 	return 0;
305 
306 #ifndef __NetBSD__
307 out_destroy_fbi:
308 	drm_fb_helper_release_fbi(helper);
309 #endif
310 out_unref:
311 	if (rbo) {
312 
313 	}
314 	if (fb && ret) {
315 		drm_gem_object_unreference(gobj);
316 		drm_framebuffer_unregister_private(fb);
317 		drm_framebuffer_cleanup(fb);
318 		kfree(fb);
319 	}
320 	return ret;
321 }
322 
323 void amdgpu_fb_output_poll_changed(struct amdgpu_device *adev)
324 {
325 	if (adev->mode_info.rfbdev)
326 		drm_fb_helper_hotplug_event(&adev->mode_info.rfbdev->helper);
327 }
328 
329 static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
330 {
331 	struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
332 #ifdef __NetBSD__
333 	int ret;
334 #endif
335 
336 #ifdef __NetBSD__
337 	/* XXX errno NetBSD->Linux */
338 	ret = -config_detach(rfbdev->helper.fbdev, DETACH_FORCE);
339 	if (ret) {
340 		DRM_ERROR("failed to detach amdgpufb: %d\n", ret);
341 		return ret;
342 	}
343 	rfbdev->helper.fbdev = NULL;
344 #else
345 	drm_fb_helper_unregister_fbi(&rfbdev->helper);
346 	drm_fb_helper_release_fbi(&rfbdev->helper);
347 #endif
348 
349 	if (rfb->obj) {
350 		amdgpufb_destroy_pinned_object(rfb->obj);
351 		rfb->obj = NULL;
352 	}
353 	drm_fb_helper_fini(&rfbdev->helper);
354 	drm_framebuffer_unregister_private(&rfb->base);
355 	drm_framebuffer_cleanup(&rfb->base);
356 
357 	return 0;
358 }
359 
360 /** Sets the color ramps on behalf of fbcon */
361 static void amdgpu_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
362 				      u16 blue, int regno)
363 {
364 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
365 
366 	amdgpu_crtc->lut_r[regno] = red >> 6;
367 	amdgpu_crtc->lut_g[regno] = green >> 6;
368 	amdgpu_crtc->lut_b[regno] = blue >> 6;
369 }
370 
371 /** Gets the color ramps on behalf of fbcon */
372 static void amdgpu_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
373 				      u16 *blue, int regno)
374 {
375 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
376 
377 	*red = amdgpu_crtc->lut_r[regno] << 6;
378 	*green = amdgpu_crtc->lut_g[regno] << 6;
379 	*blue = amdgpu_crtc->lut_b[regno] << 6;
380 }
381 
382 static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
383 	.gamma_set = amdgpu_crtc_fb_gamma_set,
384 	.gamma_get = amdgpu_crtc_fb_gamma_get,
385 	.fb_probe = amdgpufb_create,
386 };
387 
388 int amdgpu_fbdev_init(struct amdgpu_device *adev)
389 {
390 	struct amdgpu_fbdev *rfbdev;
391 	int bpp_sel = 32;
392 	int ret;
393 
394 	/* don't init fbdev on hw without DCE */
395 	if (!adev->mode_info.mode_config_initialized)
396 		return 0;
397 
398 	/* select 8 bpp console on low vram cards */
399 	if (adev->mc.real_vram_size <= (32*1024*1024))
400 		bpp_sel = 8;
401 
402 	rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
403 	if (!rfbdev)
404 		return -ENOMEM;
405 
406 	rfbdev->adev = adev;
407 	adev->mode_info.rfbdev = rfbdev;
408 
409 	drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
410 			&amdgpu_fb_helper_funcs);
411 
412 	ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
413 				 adev->mode_info.num_crtc,
414 				 AMDGPUFB_CONN_LIMIT);
415 	if (ret) {
416 		kfree(rfbdev);
417 		return ret;
418 	}
419 
420 	drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
421 
422 	/* disable all the possible outputs/crtcs before entering KMS mode */
423 	drm_helper_disable_unused_functions(adev->ddev);
424 
425 	drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
426 	return 0;
427 }
428 
429 void amdgpu_fbdev_fini(struct amdgpu_device *adev)
430 {
431 	if (!adev->mode_info.rfbdev)
432 		return;
433 
434 	amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
435 	kfree(adev->mode_info.rfbdev);
436 	adev->mode_info.rfbdev = NULL;
437 }
438 
439 void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
440 {
441 	if (adev->mode_info.rfbdev)
442 		drm_fb_helper_set_suspend(&adev->mode_info.rfbdev->helper,
443 			state);
444 }
445 
446 int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
447 {
448 	struct amdgpu_bo *robj;
449 	int size = 0;
450 
451 	if (!adev->mode_info.rfbdev)
452 		return 0;
453 
454 	robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj);
455 	size += amdgpu_bo_size(robj);
456 	return size;
457 }
458 
459 bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
460 {
461 	if (!adev->mode_info.rfbdev)
462 		return false;
463 	if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj))
464 		return true;
465 	return false;
466 }
467 
468 void amdgpu_fbdev_restore_mode(struct amdgpu_device *adev)
469 {
470 	struct amdgpu_fbdev *afbdev = adev->mode_info.rfbdev;
471 	struct drm_fb_helper *fb_helper;
472 	int ret;
473 
474 	if (!afbdev)
475 		return;
476 
477 	fb_helper = &afbdev->helper;
478 
479 	ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
480 	if (ret)
481 		DRM_DEBUG("failed to restore crtc mode\n");
482 }
483