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