xref: /dragonfly/sys/dev/drm/radeon/radeon_fb.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1 /*
2  * Copyright © 2007 David Airlie
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *     David Airlie
25  */
26 #include <linux/module.h>
27 #include <linux/fb.h>
28 #include <linux/pm_runtime.h>
29 
30 #include <drm/drmP.h>
31 #include <drm/drm_crtc.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/radeon_drm.h>
34 #include "radeon.h"
35 
36 #include <drm/drm_fb_helper.h>
37 
38 #include <linux/vga_switcheroo.h>
39 
40 /* object hierarchy -
41  * this contains a helper + a radeon fb
42  * the helper contains a pointer to radeon framebuffer baseclass.
43  */
44 struct radeon_fbdev {
45           struct drm_fb_helper helper;
46           struct radeon_framebuffer rfb;
47           struct radeon_device *rdev;
48 };
49 
50 #if 0
51 static int
52 radeonfb_open(struct fb_info *info, int user)
53 {
54           struct radeon_fbdev *rfbdev = info->par;
55           struct radeon_device *rdev = rfbdev->rdev;
56           int ret = pm_runtime_get_sync(rdev->ddev->dev);
57           if (ret < 0 && ret != -EACCES) {
58                     pm_runtime_mark_last_busy(rdev->ddev->dev);
59                     pm_runtime_put_autosuspend(rdev->ddev->dev);
60                     return ret;
61           }
62           return 0;
63 }
64 
65 static int
66 radeonfb_release(struct fb_info *info, int user)
67 {
68           struct radeon_fbdev *rfbdev = info->par;
69           struct radeon_device *rdev = rfbdev->rdev;
70 
71           pm_runtime_mark_last_busy(rdev->ddev->dev);
72           pm_runtime_put_autosuspend(rdev->ddev->dev);
73           return 0;
74 }
75 #endif
76 
77 static struct fb_ops radeonfb_ops = {
78 #if 0
79           .owner = THIS_MODULE,
80 #endif
81           DRM_FB_HELPER_DEFAULT_OPS,
82 #if 0
83           .fb_open = radeonfb_open,
84           .fb_release = radeonfb_release,
85 #endif
86 #if 0
87           .fb_fillrect = drm_fb_helper_cfb_fillrect,
88           .fb_copyarea = drm_fb_helper_cfb_copyarea,
89           .fb_imageblit = drm_fb_helper_cfb_imageblit,
90 #endif
91 };
92 
93 
radeon_align_pitch(struct radeon_device * rdev,int width,int cpp,bool tiled)94 int radeon_align_pitch(struct radeon_device *rdev, int width, int cpp, bool tiled)
95 {
96           int aligned = width;
97           int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
98           int pitch_mask = 0;
99 
100           switch (cpp) {
101           case 1:
102                     pitch_mask = align_large ? 255 : 127;
103                     break;
104           case 2:
105                     pitch_mask = align_large ? 127 : 31;
106                     break;
107           case 3:
108           case 4:
109                     pitch_mask = align_large ? 63 : 15;
110                     break;
111           }
112 
113           aligned += pitch_mask;
114           aligned &= ~pitch_mask;
115           return aligned * cpp;
116 }
117 
radeonfb_destroy_pinned_object(struct drm_gem_object * gobj)118 static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
119 {
120           struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
121           int ret;
122 
123           ret = radeon_bo_reserve(rbo, false);
124           if (likely(ret == 0)) {
125                     radeon_bo_kunmap(rbo);
126                     radeon_bo_unpin(rbo);
127                     radeon_bo_unreserve(rbo);
128           }
129           drm_gem_object_put_unlocked(gobj);
130 }
131 
radeonfb_create_pinned_object(struct radeon_fbdev * rfbdev,struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object ** gobj_p)132 static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
133                                                    struct drm_mode_fb_cmd2 *mode_cmd,
134                                                    struct drm_gem_object **gobj_p)
135 {
136           struct radeon_device *rdev = rfbdev->rdev;
137           struct drm_gem_object *gobj = NULL;
138           struct radeon_bo *rbo = NULL;
139           bool fb_tiled = false; /* useful for testing */
140           u32 tiling_flags = 0;
141           int ret;
142           int aligned_size, size;
143           int height = mode_cmd->height;
144           u32 cpp;
145 
146           cpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0);
147 
148           /* need to align pitch with crtc limits */
149           mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp,
150                                                               fb_tiled);
151 
152           if (rdev->family >= CHIP_R600)
153                     height = ALIGN(mode_cmd->height, 8);
154           size = mode_cmd->pitches[0] * height;
155           aligned_size = ALIGN(size, PAGE_SIZE);
156           ret = radeon_gem_object_create(rdev, aligned_size, 0,
157                                                RADEON_GEM_DOMAIN_VRAM,
158                                                0, true, &gobj);
159           if (ret) {
160                     pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
161                     return -ENOMEM;
162           }
163           rbo = gem_to_radeon_bo(gobj);
164 
165           if (fb_tiled)
166                     tiling_flags = RADEON_TILING_MACRO;
167 
168 #ifdef __BIG_ENDIAN
169           switch (cpp) {
170           case 4:
171                     tiling_flags |= RADEON_TILING_SWAP_32BIT;
172                     break;
173           case 2:
174                     tiling_flags |= RADEON_TILING_SWAP_16BIT;
175           default:
176                     break;
177           }
178 #endif
179 
180           if (tiling_flags) {
181                     ret = radeon_bo_set_tiling_flags(rbo,
182                                                              tiling_flags | RADEON_TILING_SURFACE,
183                                                              mode_cmd->pitches[0]);
184                     if (ret)
185                               dev_err(rdev->dev, "FB failed to set tiling flags\n");
186           }
187 
188 
189           ret = radeon_bo_reserve(rbo, false);
190           if (unlikely(ret != 0))
191                     goto out_unref;
192           /* Only 27 bit offset for legacy CRTC */
193           ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
194                                                ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
195                                                NULL);
196           if (ret) {
197                     radeon_bo_unreserve(rbo);
198                     goto out_unref;
199           }
200           if (fb_tiled)
201                     radeon_bo_check_tiling(rbo, 0, 0);
202           ret = radeon_bo_kmap(rbo, NULL);
203           radeon_bo_unreserve(rbo);
204           if (ret) {
205                     goto out_unref;
206           }
207 
208           *gobj_p = gobj;
209           return 0;
210 out_unref:
211           radeonfb_destroy_pinned_object(gobj);
212           *gobj_p = NULL;
213           return ret;
214 }
215 
radeonfb_create(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)216 static int radeonfb_create(struct drm_fb_helper *helper,
217                                  struct drm_fb_helper_surface_size *sizes)
218 {
219           struct radeon_fbdev *rfbdev =
220                     container_of(helper, struct radeon_fbdev, helper);
221           struct radeon_device *rdev = rfbdev->rdev;
222           struct fb_info *info;
223           struct drm_framebuffer *fb = NULL;
224           struct drm_mode_fb_cmd2 mode_cmd;
225           struct drm_gem_object *gobj = NULL;
226           struct radeon_bo *rbo = NULL;
227           int ret;
228           device_t vga_dev = device_get_parent(rdev->dev->bsddev);
229 
230           mode_cmd.width = sizes->surface_width;
231           mode_cmd.height = sizes->surface_height;
232 
233           /* avivo can't scanout real 24bpp */
234           if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
235                     sizes->surface_bpp = 32;
236 
237           mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
238                                                                         sizes->surface_depth);
239 
240           ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
241           if (ret) {
242                     DRM_ERROR("failed to create fbcon object %d\n", ret);
243                     return ret;
244           }
245 
246           rbo = gem_to_radeon_bo(gobj);
247 
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;
253           }
254 
255           info->par = rfbdev;
256 
257           ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
258           if (ret) {
259                     DRM_ERROR("failed to initialize framebuffer %d\n", ret);
260                     goto out;
261           }
262 
263           fb = &rfbdev->rfb.base;
264 
265           /* setup helper */
266           rfbdev->helper.fb = fb;
267 
268           memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
269 
270 #ifdef __DragonFly__
271           info->width = sizes->fb_width;
272           info->height = sizes->fb_height;
273           info->stride = fb->pitches[0];
274           info->depth = sizes->surface_bpp;
275           info->is_vga_boot_display = vga_pci_is_boot_display(vga_dev);
276           info->fbops = radeonfb_ops;
277           /* We use info->screen_base and info->screen_size would-be values */
278 #if 0
279           info->paddr = (vm_paddr_t)rbo->kptr;
280           info->vaddr = (vm_offset_t)pmap_mapdev_attr(info->paddr,
281                     radeon_bo_size(rbo),
282                     VM_MEMATTR_WRITE_COMBINING);
283 #endif
284           unsigned long tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
285           info->vaddr = (vm_offset_t)rbo->kptr;
286           info->paddr = rdev->mc.aper_base + tmp;
287 #else
288           strcpy(info->fix.id, "radeondrmfb");
289 
290           drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
291 
292           info->fbops = &radeonfb_ops;
293 
294           tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
295           info->fix.smem_start = rdev->mc.aper_base + tmp;
296           info->fix.smem_len = radeon_bo_size(rbo);
297           info->screen_base = rbo->kptr;
298           info->screen_size = radeon_bo_size(rbo);
299 
300           drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
301 
302           /* setup aperture base/size for vesafb takeover */
303           info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
304           info->apertures->ranges[0].size = rdev->mc.aper_size;
305 
306           /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
307 
308           if (info->screen_base == NULL) {
309                     ret = -ENOSPC;
310                     goto out;
311           }
312 #endif
313 
314           DRM_INFO("fb mappable at 0x%jX\n",  info->paddr);
315           DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
316           DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
317           DRM_INFO("fb depth is %d\n", fb->format->depth);
318           DRM_INFO("   pitch is %d\n", fb->pitches[0]);
319 
320           vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
321           return 0;
322 
323 out:
324           if (rbo) {
325 
326           }
327           if (fb && ret) {
328                     drm_gem_object_put_unlocked(gobj);
329                     drm_framebuffer_unregister_private(fb);
330                     drm_framebuffer_cleanup(fb);
331                     kfree(fb);
332           }
333           return ret;
334 }
335 
radeon_fb_output_poll_changed(struct radeon_device * rdev)336 void radeon_fb_output_poll_changed(struct radeon_device *rdev)
337 {
338           if (rdev->mode_info.rfbdev)
339                     drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper);
340 }
341 
radeon_fbdev_destroy(struct drm_device * dev,struct radeon_fbdev * rfbdev)342 static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
343 {
344           struct radeon_framebuffer *rfb = &rfbdev->rfb;
345 
346           drm_fb_helper_unregister_fbi(&rfbdev->helper);
347 
348           if (rfb->obj) {
349                     radeonfb_destroy_pinned_object(rfb->obj);
350                     rfb->obj = NULL;
351                     drm_framebuffer_unregister_private(&rfb->base);
352                     drm_framebuffer_cleanup(&rfb->base);
353           }
354           drm_fb_helper_fini(&rfbdev->helper);
355 
356           return 0;
357 }
358 
359 static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
360           .fb_probe = radeonfb_create,
361 };
362 
radeon_fbdev_init(struct radeon_device * rdev)363 int radeon_fbdev_init(struct radeon_device *rdev)
364 {
365           struct radeon_fbdev *rfbdev;
366           int bpp_sel = 32;
367           int ret;
368 
369           /* don't enable fbdev if no connectors */
370           if (list_empty(&rdev->ddev->mode_config.connector_list))
371                     return 0;
372 
373           /* select 8 bpp console on 8MB cards, or 16 bpp on RN50 or 32MB */
374           if (rdev->mc.real_vram_size <= (8*1024*1024))
375                     bpp_sel = 8;
376           else if (ASIC_IS_RN50(rdev) ||
377                      rdev->mc.real_vram_size <= (32*1024*1024))
378                     bpp_sel = 16;
379 
380           rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
381           if (!rfbdev)
382                     return -ENOMEM;
383 
384           rfbdev->rdev = rdev;
385           rdev->mode_info.rfbdev = rfbdev;
386 
387           drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
388                                     &radeon_fb_helper_funcs);
389 
390           ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
391                                          RADEONFB_CONN_LIMIT);
392           if (ret)
393                     goto free;
394 
395           ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
396           if (ret)
397                     goto fini;
398 
399           /* disable all the possible outputs/crtcs before entering KMS mode */
400           drm_helper_disable_unused_functions(rdev->ddev);
401 
402           ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
403           if (ret)
404                     goto fini;
405 
406           return 0;
407 
408 fini:
409           drm_fb_helper_fini(&rfbdev->helper);
410 free:
411           kfree(rfbdev);
412           return ret;
413 }
414 
radeon_fbdev_fini(struct radeon_device * rdev)415 void radeon_fbdev_fini(struct radeon_device *rdev)
416 {
417           if (!rdev->mode_info.rfbdev)
418                     return;
419 
420           radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
421           kfree(rdev->mode_info.rfbdev);
422           rdev->mode_info.rfbdev = NULL;
423 }
424 
radeon_fbdev_set_suspend(struct radeon_device * rdev,int state)425 void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
426 {
427 #ifdef DUMBBELL_WIP
428           if (rdev->mode_info.rfbdev)
429                     drm_fb_helper_set_suspend(&rdev->mode_info.rfbdev->helper, state);
430 #endif /* DUMBBELL_WIP */
431 }
432 
radeon_fbdev_robj_is_fb(struct radeon_device * rdev,struct radeon_bo * robj)433 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
434 {
435           if (!rdev->mode_info.rfbdev)
436                     return false;
437 
438           if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
439                     return true;
440           return false;
441 }
442 
radeon_fb_add_connector(struct radeon_device * rdev,struct drm_connector * connector)443 void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
444 {
445           if (rdev->mode_info.rfbdev)
446                     drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
447 }
448 
radeon_fb_remove_connector(struct radeon_device * rdev,struct drm_connector * connector)449 void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
450 {
451           if (rdev->mode_info.rfbdev)
452                     drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
453 }
454 
radeon_fbdev_restore_mode(struct radeon_device * rdev)455 void radeon_fbdev_restore_mode(struct radeon_device *rdev)
456 {
457           struct radeon_fbdev *rfbdev = rdev->mode_info.rfbdev;
458           struct drm_fb_helper *fb_helper;
459           int ret;
460 
461           if (!rfbdev)
462                     return;
463 
464           fb_helper = &rfbdev->helper;
465 
466           ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
467           if (ret)
468                     DRM_DEBUG("failed to restore crtc mode\n");
469 }
470