amdgpu_fb.c revision 1.3.2.2 1 /* $NetBSD: amdgpu_fb.c,v 1.3.2.2 2018/09/06 06:56:10 pgoyette 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.3.2.2 2018/09/06 06:56:10 pgoyette 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 /* object hierarchy -
51 this contains a helper + a amdgpu fb
52 the helper contains a pointer to amdgpu framebuffer baseclass.
53 */
54 struct amdgpu_fbdev {
55 struct drm_fb_helper helper;
56 struct amdgpu_framebuffer rfb;
57 struct list_head fbdev_list;
58 struct amdgpu_device *adev;
59 };
60
61 #ifndef __NetBSD__
62 static struct fb_ops amdgpufb_ops = {
63 .owner = THIS_MODULE,
64 .fb_check_var = drm_fb_helper_check_var,
65 .fb_set_par = drm_fb_helper_set_par,
66 .fb_fillrect = drm_fb_helper_cfb_fillrect,
67 .fb_copyarea = drm_fb_helper_cfb_copyarea,
68 .fb_imageblit = drm_fb_helper_cfb_imageblit,
69 .fb_pan_display = drm_fb_helper_pan_display,
70 .fb_blank = drm_fb_helper_blank,
71 .fb_setcmap = drm_fb_helper_setcmap,
72 .fb_debug_enter = drm_fb_helper_debug_enter,
73 .fb_debug_leave = drm_fb_helper_debug_leave,
74 };
75 #endif
76
77
78 int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int bpp, bool tiled)
79 {
80 int aligned = width;
81 int pitch_mask = 0;
82
83 switch (bpp / 8) {
84 case 1:
85 pitch_mask = 255;
86 break;
87 case 2:
88 pitch_mask = 127;
89 break;
90 case 3:
91 case 4:
92 pitch_mask = 63;
93 break;
94 }
95
96 aligned += pitch_mask;
97 aligned &= ~pitch_mask;
98 return aligned;
99 }
100
101 static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
102 {
103 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(gobj);
104 int ret;
105
106 ret = amdgpu_bo_reserve(rbo, false);
107 if (likely(ret == 0)) {
108 amdgpu_bo_kunmap(rbo);
109 amdgpu_bo_unpin(rbo);
110 amdgpu_bo_unreserve(rbo);
111 }
112 drm_gem_object_unreference_unlocked(gobj);
113 }
114
115 static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
116 struct drm_mode_fb_cmd2 *mode_cmd,
117 struct drm_gem_object **gobj_p)
118 {
119 struct amdgpu_device *adev = rfbdev->adev;
120 struct drm_gem_object *gobj = NULL;
121 struct amdgpu_bo *rbo = NULL;
122 bool fb_tiled = false; /* useful for testing */
123 u32 tiling_flags = 0;
124 int ret;
125 int aligned_size, size;
126 int height = mode_cmd->height;
127 u32 bpp, depth;
128
129 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
130
131 /* need to align pitch with crtc limits */
132 mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, bpp,
133 fb_tiled) * ((bpp + 1) / 8);
134
135 #ifdef __NetBSD__ /* XXX ALIGN means something else. */
136 height = round_up(mode_cmd->height, 8);
137 #else
138 height = ALIGN(mode_cmd->height, 8);
139 #endif
140 size = mode_cmd->pitches[0] * height;
141 #ifdef __NetBSD__ /* XXX ALIGN means something else. */
142 aligned_size = round_up(size, PAGE_SIZE);
143 #else
144 aligned_size = ALIGN(size, PAGE_SIZE);
145 #endif
146 ret = amdgpu_gem_object_create(adev, aligned_size, 0,
147 AMDGPU_GEM_DOMAIN_VRAM,
148 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
149 true, &gobj);
150 if (ret) {
151 printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
152 aligned_size);
153 return -ENOMEM;
154 }
155 rbo = gem_to_amdgpu_bo(gobj);
156
157 if (fb_tiled)
158 tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
159
160 ret = amdgpu_bo_reserve(rbo, false);
161 if (unlikely(ret != 0))
162 goto out_unref;
163
164 if (tiling_flags) {
165 ret = amdgpu_bo_set_tiling_flags(rbo,
166 tiling_flags);
167 if (ret)
168 dev_err(adev->dev, "FB failed to set tiling flags\n");
169 }
170
171
172 ret = amdgpu_bo_pin_restricted(rbo, AMDGPU_GEM_DOMAIN_VRAM, 0, 0, NULL);
173 if (ret) {
174 amdgpu_bo_unreserve(rbo);
175 goto out_unref;
176 }
177 ret = amdgpu_bo_kmap(rbo, NULL);
178 amdgpu_bo_unreserve(rbo);
179 if (ret) {
180 goto out_unref;
181 }
182
183 *gobj_p = gobj;
184 return 0;
185 out_unref:
186 amdgpufb_destroy_pinned_object(gobj);
187 *gobj_p = NULL;
188 return ret;
189 }
190
191 static int amdgpufb_create(struct drm_fb_helper *helper,
192 struct drm_fb_helper_surface_size *sizes)
193 {
194 struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
195 struct amdgpu_device *adev = rfbdev->adev;
196 #ifndef __NetBSD__
197 struct fb_info *info;
198 #endif
199 struct drm_framebuffer *fb = NULL;
200 struct drm_mode_fb_cmd2 mode_cmd;
201 struct drm_gem_object *gobj = NULL;
202 struct amdgpu_bo *rbo = NULL;
203 int ret;
204 #ifndef __NetBSD__
205 unsigned long tmp;
206 #endif
207
208 mode_cmd.width = sizes->surface_width;
209 mode_cmd.height = sizes->surface_height;
210
211 if (sizes->surface_bpp == 24)
212 sizes->surface_bpp = 32;
213
214 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
215 sizes->surface_depth);
216
217 ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
218 if (ret) {
219 DRM_ERROR("failed to create fbcon object %d\n", ret);
220 return ret;
221 }
222
223 rbo = gem_to_amdgpu_bo(gobj);
224
225 #ifdef __NetBSD__
226 ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
227 if (ret) {
228 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
229 goto out_unref;
230 }
231
232 (void)memset(rbo->kptr, 0, amdgpu_bo_size(rbo));
233
234 {
235 static const struct amdgpufb_attach_args zero_afa;
236 struct amdgpufb_attach_args afa = zero_afa;
237
238 afa.afa_fb_helper = helper;
239 afa.afa_fb_sizes = *sizes;
240 afa.afa_fb_ptr = rbo->kptr;
241 afa.afa_fb_linebytes = mode_cmd.pitches[0];
242
243 helper->fbdev = config_found_ia(adev->ddev->dev, "amdgpufbbus", &afa,
244 NULL);
245 if (helper->fbdev == NULL) {
246 DRM_ERROR("failed to attach amdgpufb\n");
247 goto out_unref;
248 }
249 }
250 fb = &rfbdev->rfb.base;
251 rfbdev->helper.fb = fb;
252 #else /* __NetBSD__ */
253 /* okay we have an object now allocate the framebuffer */
254 info = drm_fb_helper_alloc_fbi(helper);
255 if (IS_ERR(info)) {
256 ret = PTR_ERR(info);
257 goto out_unref;
258 }
259
260 info->par = rfbdev;
261 info->skip_vt_switch = true;
262
263 ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
264 if (ret) {
265 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
266 goto out_destroy_fbi;
267 }
268
269 fb = &rfbdev->rfb.base;
270
271 /* setup helper */
272 rfbdev->helper.fb = fb;
273
274 memset_io(rbo->kptr, 0x0, amdgpu_bo_size(rbo));
275
276 strcpy(info->fix.id, "amdgpudrmfb");
277
278 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
279
280 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
281 info->fbops = &amdgpufb_ops;
282
283 tmp = amdgpu_bo_gpu_offset(rbo) - adev->mc.vram_start;
284 info->fix.smem_start = adev->mc.aper_base + tmp;
285 info->fix.smem_len = amdgpu_bo_size(rbo);
286 info->screen_base = rbo->kptr;
287 info->screen_size = amdgpu_bo_size(rbo);
288
289 drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
290
291 /* setup aperture base/size for vesafb takeover */
292 info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
293 info->apertures->ranges[0].size = adev->mc.aper_size;
294
295 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
296
297 if (info->screen_base == NULL) {
298 ret = -ENOSPC;
299 goto out_destroy_fbi;
300 }
301
302 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
303 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)adev->mc.aper_base);
304 DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(rbo));
305 DRM_INFO("fb depth is %d\n", fb->depth);
306 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
307
308 vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
309 #endif
310 return 0;
311
312 #ifndef __NetBSD__
313 out_destroy_fbi:
314 drm_fb_helper_release_fbi(helper);
315 #endif
316 out_unref:
317 if (rbo) {
318
319 }
320 if (fb && ret) {
321 drm_gem_object_unreference(gobj);
322 drm_framebuffer_unregister_private(fb);
323 drm_framebuffer_cleanup(fb);
324 kfree(fb);
325 }
326 return ret;
327 }
328
329 void amdgpu_fb_output_poll_changed(struct amdgpu_device *adev)
330 {
331 if (adev->mode_info.rfbdev)
332 drm_fb_helper_hotplug_event(&adev->mode_info.rfbdev->helper);
333 }
334
335 static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
336 {
337 struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
338 #ifdef __NetBSD__
339 int ret;
340 #endif
341
342 #ifdef __NetBSD__
343 /* XXX errno NetBSD->Linux */
344 ret = -config_detach(rfbdev->helper.fbdev, DETACH_FORCE);
345 if (ret) {
346 DRM_ERROR("failed to detach amdgpufb: %d\n", ret);
347 return ret;
348 }
349 rfbdev->helper.fbdev = NULL;
350 #else
351 drm_fb_helper_unregister_fbi(&rfbdev->helper);
352 drm_fb_helper_release_fbi(&rfbdev->helper);
353 #endif
354
355 if (rfb->obj) {
356 amdgpufb_destroy_pinned_object(rfb->obj);
357 rfb->obj = NULL;
358 }
359 drm_fb_helper_fini(&rfbdev->helper);
360 drm_framebuffer_unregister_private(&rfb->base);
361 drm_framebuffer_cleanup(&rfb->base);
362
363 return 0;
364 }
365
366 /** Sets the color ramps on behalf of fbcon */
367 static void amdgpu_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
368 u16 blue, int regno)
369 {
370 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
371
372 amdgpu_crtc->lut_r[regno] = red >> 6;
373 amdgpu_crtc->lut_g[regno] = green >> 6;
374 amdgpu_crtc->lut_b[regno] = blue >> 6;
375 }
376
377 /** Gets the color ramps on behalf of fbcon */
378 static void amdgpu_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
379 u16 *blue, int regno)
380 {
381 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
382
383 *red = amdgpu_crtc->lut_r[regno] << 6;
384 *green = amdgpu_crtc->lut_g[regno] << 6;
385 *blue = amdgpu_crtc->lut_b[regno] << 6;
386 }
387
388 static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
389 .gamma_set = amdgpu_crtc_fb_gamma_set,
390 .gamma_get = amdgpu_crtc_fb_gamma_get,
391 .fb_probe = amdgpufb_create,
392 };
393
394 int amdgpu_fbdev_init(struct amdgpu_device *adev)
395 {
396 struct amdgpu_fbdev *rfbdev;
397 int bpp_sel = 32;
398 int ret;
399
400 /* don't init fbdev on hw without DCE */
401 if (!adev->mode_info.mode_config_initialized)
402 return 0;
403
404 /* select 8 bpp console on low vram cards */
405 if (adev->mc.real_vram_size <= (32*1024*1024))
406 bpp_sel = 8;
407
408 rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
409 if (!rfbdev)
410 return -ENOMEM;
411
412 rfbdev->adev = adev;
413 adev->mode_info.rfbdev = rfbdev;
414
415 drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
416 &amdgpu_fb_helper_funcs);
417
418 ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
419 adev->mode_info.num_crtc,
420 AMDGPUFB_CONN_LIMIT);
421 if (ret) {
422 kfree(rfbdev);
423 return ret;
424 }
425
426 drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
427
428 /* disable all the possible outputs/crtcs before entering KMS mode */
429 drm_helper_disable_unused_functions(adev->ddev);
430
431 drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
432 return 0;
433 }
434
435 void amdgpu_fbdev_fini(struct amdgpu_device *adev)
436 {
437 if (!adev->mode_info.rfbdev)
438 return;
439
440 amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
441 kfree(adev->mode_info.rfbdev);
442 adev->mode_info.rfbdev = NULL;
443 }
444
445 void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
446 {
447 #ifndef __NetBSD__ /* XXX amdgpu fb suspend */
448 if (adev->mode_info.rfbdev)
449 drm_fb_helper_set_suspend(&adev->mode_info.rfbdev->helper,
450 state);
451 #endif
452 }
453
454 int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
455 {
456 struct amdgpu_bo *robj;
457 int size = 0;
458
459 if (!adev->mode_info.rfbdev)
460 return 0;
461
462 robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj);
463 size += amdgpu_bo_size(robj);
464 return size;
465 }
466
467 bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
468 {
469 if (!adev->mode_info.rfbdev)
470 return false;
471 if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj))
472 return true;
473 return false;
474 }
475
476 void amdgpu_fbdev_restore_mode(struct amdgpu_device *adev)
477 {
478 struct amdgpu_fbdev *afbdev = adev->mode_info.rfbdev;
479 struct drm_fb_helper *fb_helper;
480 int ret;
481
482 if (!afbdev)
483 return;
484
485 fb_helper = &afbdev->helper;
486
487 ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
488 if (ret)
489 DRM_DEBUG("failed to restore crtc mode\n");
490 }
491