2 * Copyright © 2007 David Airlie
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:
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
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.
26 * $FreeBSD: head/sys/dev/drm2/radeon/radeon_fb.c 254885 2013-08-25 19:37:15Z dumbbell $
29 #include <linux/module.h>
30 #include <linux/slab.h>
35 #include <drm/drm_crtc.h>
36 #include <drm/drm_crtc_helper.h>
37 #include <uapi_drm/radeon_drm.h>
40 #include <drm/drm_fb_helper.h>
43 #include <linux/vga_switcheroo.h>
47 * this contains a helper + a radeon fb
48 * the helper contains a pointer to radeon framebuffer baseclass.
52 struct drm_fb_helper helper;
53 struct radeon_framebuffer rfb;
54 struct radeon_device *rdev;
57 static struct fb_ops radeonfb_ops = {
60 .fb_check_var = drm_fb_helper_check_var,
62 .fb_set_par = drm_fb_helper_set_par,
64 .fb_fillrect = drm_fb_helper_cfb_fillrect,
65 .fb_copyarea = drm_fb_helper_cfb_copyarea,
66 .fb_imageblit = drm_fb_helper_cfb_imageblit,
67 .fb_pan_display = drm_fb_helper_pan_display,
69 .fb_blank = drm_fb_helper_blank,
71 .fb_setcmap = drm_fb_helper_setcmap,
73 .fb_debug_enter = drm_fb_helper_debug_enter,
75 .fb_debug_leave = drm_fb_helper_debug_leave,
80 int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
83 int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
88 pitch_mask = align_large ? 255 : 127;
91 pitch_mask = align_large ? 127 : 31;
95 pitch_mask = align_large ? 63 : 15;
99 aligned += pitch_mask;
100 aligned &= ~pitch_mask;
104 static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
106 struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
109 ret = radeon_bo_reserve(rbo, false);
110 if (likely(ret == 0)) {
111 radeon_bo_kunmap(rbo);
112 radeon_bo_unpin(rbo);
113 radeon_bo_unreserve(rbo);
115 drm_gem_object_unreference_unlocked(gobj);
118 static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
119 struct drm_mode_fb_cmd2 *mode_cmd,
120 struct drm_gem_object **gobj_p)
122 struct radeon_device *rdev = rfbdev->rdev;
123 struct drm_gem_object *gobj = NULL;
124 struct radeon_bo *rbo = NULL;
125 bool fb_tiled = false; /* useful for testing */
126 u32 tiling_flags = 0;
128 int aligned_size, size;
129 int height = mode_cmd->height;
132 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
134 /* need to align pitch with crtc limits */
135 mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp,
136 fb_tiled) * ((bpp + 1) / 8);
138 if (rdev->family >= CHIP_R600)
139 height = ALIGN(mode_cmd->height, 8);
140 size = mode_cmd->pitches[0] * height;
141 aligned_size = ALIGN(size, PAGE_SIZE);
142 ret = radeon_gem_object_create(rdev, aligned_size, 0,
143 RADEON_GEM_DOMAIN_VRAM,
146 printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
150 rbo = gem_to_radeon_bo(gobj);
153 tiling_flags = RADEON_TILING_MACRO;
158 tiling_flags |= RADEON_TILING_SWAP_32BIT;
161 tiling_flags |= RADEON_TILING_SWAP_16BIT;
168 ret = radeon_bo_set_tiling_flags(rbo,
169 tiling_flags | RADEON_TILING_SURFACE,
170 mode_cmd->pitches[0]);
172 dev_err(rdev->dev, "FB failed to set tiling flags\n");
176 ret = radeon_bo_reserve(rbo, false);
177 if (unlikely(ret != 0))
179 /* Only 27 bit offset for legacy CRTC */
180 ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
181 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
184 radeon_bo_unreserve(rbo);
188 radeon_bo_check_tiling(rbo, 0, 0);
189 ret = radeon_bo_kmap(rbo, NULL);
190 radeon_bo_unreserve(rbo);
198 radeonfb_destroy_pinned_object(gobj);
203 static int radeonfb_create(struct drm_fb_helper *helper,
204 struct drm_fb_helper_surface_size *sizes)
206 struct radeon_fbdev *rfbdev =
207 container_of(helper, struct radeon_fbdev, helper);
208 struct radeon_device *rdev = rfbdev->rdev;
209 struct fb_info *info;
210 struct drm_framebuffer *fb = NULL;
211 struct drm_mode_fb_cmd2 mode_cmd;
212 struct drm_gem_object *gobj = NULL;
213 struct radeon_bo *rbo = NULL;
214 device_t vga_dev = device_get_parent(rdev->dev->bsddev);
218 mode_cmd.width = sizes->surface_width;
219 mode_cmd.height = sizes->surface_height;
221 /* avivo can't scanout real 24bpp */
222 if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
223 sizes->surface_bpp = 32;
225 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
226 sizes->surface_depth);
228 ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
230 DRM_ERROR("failed to create fbcon object %d\n", ret);
234 rbo = gem_to_radeon_bo(gobj);
236 /* okay we have an object now allocate the framebuffer */
237 info = drm_fb_helper_alloc_fbi(helper);
245 info->skip_vt_switch = true;
248 ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
250 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
251 goto out_destroy_fbi;
254 fb = &rfbdev->rfb.base;
257 rfbdev->helper.fb = fb;
259 memset(rbo->kptr, 0, radeon_bo_size(rbo));
260 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
261 info->vaddr = (vm_offset_t)rbo->kptr;
262 info->paddr = rdev->mc.aper_base + tmp;
263 info->width = sizes->surface_width;
264 info->height = sizes->surface_height;
265 info->stride = fb->pitches[0];
266 info->depth = sizes->surface_bpp;
267 info->is_vga_boot_display = vga_pci_is_boot_display(vga_dev);
269 memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
271 strcpy(info->fix.id, "radeondrmfb");
273 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
275 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
278 info->fbops = radeonfb_ops;
280 info->fbops = &radeonfb_ops;
284 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
285 info->fix.smem_start = rdev->mc.aper_base + tmp;
286 info->fix.smem_len = radeon_bo_size(rbo);
287 info->screen_base = rbo->kptr;
288 info->screen_size = radeon_bo_size(rbo);
290 drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
292 /* setup aperture base/size for vesafb takeover */
293 info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
294 info->apertures->ranges[0].size = rdev->mc.aper_size;
296 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
298 if (info->screen_base == NULL) {
300 goto out_destroy_fbi;
303 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
305 DRM_INFO("fb mappable at 0x%jX\n", info->paddr);
306 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
307 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
308 DRM_INFO("fb depth is %d\n", fb->depth);
309 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
312 vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
317 drm_fb_helper_release_fbi(helper);
323 drm_gem_object_unreference_unlocked(gobj);
324 drm_framebuffer_unregister_private(fb);
325 drm_framebuffer_cleanup(fb);
326 kfree(fb); /* XXX malloc'd in radeon_user_framebuffer_create? */
331 void radeon_fb_output_poll_changed(struct radeon_device *rdev)
333 if (rdev->mode_info.rfbdev)
334 drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper);
337 static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
339 struct radeon_framebuffer *rfb = &rfbdev->rfb;
341 drm_fb_helper_unregister_fbi(&rfbdev->helper);
342 drm_fb_helper_release_fbi(&rfbdev->helper);
345 radeonfb_destroy_pinned_object(rfb->obj);
348 drm_fb_helper_fini(&rfbdev->helper);
349 drm_framebuffer_unregister_private(&rfb->base);
350 drm_framebuffer_cleanup(&rfb->base);
355 static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
356 .gamma_set = radeon_crtc_fb_gamma_set,
357 .gamma_get = radeon_crtc_fb_gamma_get,
358 .fb_probe = radeonfb_create,
361 int radeon_fbdev_init(struct radeon_device *rdev)
363 struct radeon_fbdev *rfbdev;
367 /* don't enable fbdev if no connectors */
368 if (list_empty(&rdev->ddev->mode_config.connector_list))
371 /* select 8 bpp console on RN50 or 16MB cards */
372 if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
375 rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
380 rdev->mode_info.rfbdev = rfbdev;
382 drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
383 &radeon_fb_helper_funcs);
385 ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
387 RADEONFB_CONN_LIMIT);
391 ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
395 /* disable all the possible outputs/crtcs before entering KMS mode */
396 drm_helper_disable_unused_functions(rdev->ddev);
398 ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
405 drm_fb_helper_fini(&rfbdev->helper);
411 void radeon_fbdev_fini(struct radeon_device *rdev)
413 if (!rdev->mode_info.rfbdev)
416 radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
417 kfree(rdev->mode_info.rfbdev);
418 rdev->mode_info.rfbdev = NULL;
421 void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
424 if (rdev->mode_info.rfbdev)
425 fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state);
426 #endif /* DUMBBELL_WIP */
429 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
431 if (!rdev->mode_info.rfbdev)
434 if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
439 void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
441 if (rdev->mode_info.rfbdev)
442 drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
445 void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
447 if (rdev->mode_info.rfbdev)
448 drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
451 void radeon_fbdev_restore_mode(struct radeon_device *rdev)
453 struct radeon_fbdev *rfbdev = rdev->mode_info.rfbdev;
454 struct drm_fb_helper *fb_helper;
460 fb_helper = &rfbdev->helper;
462 ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
464 DRM_DEBUG("failed to restore crtc mode\n");