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 #include <linux/module.h>
27 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_crtc_helper.h>
34 #include <drm/radeon_drm.h>
37 #include <drm/drm_fb_helper.h>
39 #include <linux/vga_switcheroo.h>
42 * this contains a helper + a radeon fb
43 * the helper contains a pointer to radeon framebuffer baseclass.
46 struct drm_fb_helper helper;
47 struct radeon_framebuffer rfb;
48 struct radeon_device *rdev;
52 radeonfb_open(struct fb_info *info, int user)
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);
66 radeonfb_release(struct fb_info *info, int user)
68 struct radeon_fbdev *rfbdev = info->par;
69 struct radeon_device *rdev = rfbdev->rdev;
71 pm_runtime_mark_last_busy(rdev->ddev->dev);
72 pm_runtime_put_autosuspend(rdev->ddev->dev);
76 static struct fb_ops radeonfb_ops = {
78 .fb_open = radeonfb_open,
79 .fb_release = radeonfb_release,
80 .fb_check_var = drm_fb_helper_check_var,
81 .fb_set_par = drm_fb_helper_set_par,
82 .fb_fillrect = drm_fb_helper_cfb_fillrect,
83 .fb_copyarea = drm_fb_helper_cfb_copyarea,
84 .fb_imageblit = drm_fb_helper_cfb_imageblit,
85 .fb_pan_display = drm_fb_helper_pan_display,
86 .fb_blank = drm_fb_helper_blank,
87 .fb_setcmap = drm_fb_helper_setcmap,
88 .fb_debug_enter = drm_fb_helper_debug_enter,
89 .fb_debug_leave = drm_fb_helper_debug_leave,
93 int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
96 int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
101 pitch_mask = align_large ? 255 : 127;
104 pitch_mask = align_large ? 127 : 31;
108 pitch_mask = align_large ? 63 : 15;
112 aligned += pitch_mask;
113 aligned &= ~pitch_mask;
117 static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
119 struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
122 ret = radeon_bo_reserve(rbo, false);
123 if (likely(ret == 0)) {
124 radeon_bo_kunmap(rbo);
125 radeon_bo_unpin(rbo);
126 radeon_bo_unreserve(rbo);
128 drm_gem_object_unreference_unlocked(gobj);
131 static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
132 struct drm_mode_fb_cmd2 *mode_cmd,
133 struct drm_gem_object **gobj_p)
135 struct radeon_device *rdev = rfbdev->rdev;
136 struct drm_gem_object *gobj = NULL;
137 struct radeon_bo *rbo = NULL;
138 bool fb_tiled = false; /* useful for testing */
139 u32 tiling_flags = 0;
141 int aligned_size, size;
142 int height = mode_cmd->height;
145 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
147 /* need to align pitch with crtc limits */
148 mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp,
149 fb_tiled) * ((bpp + 1) / 8);
151 if (rdev->family >= CHIP_R600)
152 height = ALIGN(mode_cmd->height, 8);
153 size = mode_cmd->pitches[0] * height;
154 aligned_size = ALIGN(size, PAGE_SIZE);
155 ret = radeon_gem_object_create(rdev, aligned_size, 0,
156 RADEON_GEM_DOMAIN_VRAM,
159 printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
163 rbo = gem_to_radeon_bo(gobj);
166 tiling_flags = RADEON_TILING_MACRO;
171 tiling_flags |= RADEON_TILING_SWAP_32BIT;
174 tiling_flags |= RADEON_TILING_SWAP_16BIT;
181 ret = radeon_bo_set_tiling_flags(rbo,
182 tiling_flags | RADEON_TILING_SURFACE,
183 mode_cmd->pitches[0]);
185 dev_err(rdev->dev, "FB failed to set tiling flags\n");
189 ret = radeon_bo_reserve(rbo, false);
190 if (unlikely(ret != 0))
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,
197 radeon_bo_unreserve(rbo);
201 radeon_bo_check_tiling(rbo, 0, 0);
202 ret = radeon_bo_kmap(rbo, NULL);
203 radeon_bo_unreserve(rbo);
211 radeonfb_destroy_pinned_object(gobj);
216 static int radeonfb_create(struct drm_fb_helper *helper,
217 struct drm_fb_helper_surface_size *sizes)
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;
230 mode_cmd.width = sizes->surface_width;
231 mode_cmd.height = sizes->surface_height;
233 /* avivo can't scanout real 24bpp */
234 if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
235 sizes->surface_bpp = 32;
237 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
238 sizes->surface_depth);
240 ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
242 DRM_ERROR("failed to create fbcon object %d\n", ret);
246 rbo = gem_to_radeon_bo(gobj);
248 /* okay we have an object now allocate the framebuffer */
249 info = drm_fb_helper_alloc_fbi(helper);
256 info->skip_vt_switch = true;
258 ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
260 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
261 goto out_destroy_fbi;
264 fb = &rfbdev->rfb.base;
267 rfbdev->helper.fb = fb;
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;
276 info->fbops = &radeonfb_ops;
278 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
279 info->fix.smem_start = rdev->mc.aper_base + tmp;
280 info->fix.smem_len = radeon_bo_size(rbo);
281 info->screen_base = rbo->kptr;
282 info->screen_size = radeon_bo_size(rbo);
284 drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
286 /* setup aperture base/size for vesafb takeover */
287 info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
288 info->apertures->ranges[0].size = rdev->mc.aper_size;
290 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
292 if (info->screen_base == NULL) {
294 goto out_destroy_fbi;
297 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
298 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
299 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
300 DRM_INFO("fb depth is %d\n", fb->depth);
301 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
303 vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
307 drm_fb_helper_release_fbi(helper);
313 drm_gem_object_unreference_unlocked(gobj);
314 drm_framebuffer_unregister_private(fb);
315 drm_framebuffer_cleanup(fb);
321 void radeon_fb_output_poll_changed(struct radeon_device *rdev)
323 if (rdev->mode_info.rfbdev)
324 drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper);
327 static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
329 struct radeon_framebuffer *rfb = &rfbdev->rfb;
331 drm_fb_helper_unregister_fbi(&rfbdev->helper);
332 drm_fb_helper_release_fbi(&rfbdev->helper);
335 radeonfb_destroy_pinned_object(rfb->obj);
338 drm_fb_helper_fini(&rfbdev->helper);
339 drm_framebuffer_unregister_private(&rfb->base);
340 drm_framebuffer_cleanup(&rfb->base);
345 static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
346 .gamma_set = radeon_crtc_fb_gamma_set,
347 .gamma_get = radeon_crtc_fb_gamma_get,
348 .fb_probe = radeonfb_create,
351 int radeon_fbdev_init(struct radeon_device *rdev)
353 struct radeon_fbdev *rfbdev;
357 /* don't enable fbdev if no connectors */
358 if (list_empty(&rdev->ddev->mode_config.connector_list))
361 /* select 8 bpp console on RN50 or 16MB cards */
362 if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
365 rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
370 rdev->mode_info.rfbdev = rfbdev;
372 drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
373 &radeon_fb_helper_funcs);
375 ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
377 RADEONFB_CONN_LIMIT);
381 ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
385 /* disable all the possible outputs/crtcs before entering KMS mode */
386 drm_helper_disable_unused_functions(rdev->ddev);
388 ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
395 drm_fb_helper_fini(&rfbdev->helper);
401 void radeon_fbdev_fini(struct radeon_device *rdev)
403 if (!rdev->mode_info.rfbdev)
406 radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
407 kfree(rdev->mode_info.rfbdev);
408 rdev->mode_info.rfbdev = NULL;
411 void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
413 if (rdev->mode_info.rfbdev)
414 fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state);
417 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
419 if (!rdev->mode_info.rfbdev)
422 if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
427 void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
429 if (rdev->mode_info.rfbdev)
430 drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
433 void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
435 if (rdev->mode_info.rfbdev)
436 drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
439 void radeon_fbdev_restore_mode(struct radeon_device *rdev)
441 struct radeon_fbdev *rfbdev = rdev->mode_info.rfbdev;
442 struct drm_fb_helper *fb_helper;
448 fb_helper = &rfbdev->helper;
450 ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
452 DRM_DEBUG("failed to restore crtc mode\n");