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.
27 #include <linux/pci.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/vga_switcheroo.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_drv.h>
33 #include <drm/drm_fb_helper.h>
34 #include <drm/drm_fourcc.h>
35 #include <drm/drm_framebuffer.h>
36 #include <drm/drm_gem_framebuffer_helper.h>
40 static void radeon_fbdev_destroy_pinned_object(struct drm_gem_object *gobj)
42 struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
45 ret = radeon_bo_reserve(rbo, false);
46 if (likely(ret == 0)) {
47 radeon_bo_kunmap(rbo);
49 radeon_bo_unreserve(rbo);
51 drm_gem_object_put(gobj);
54 static int radeon_fbdev_create_pinned_object(struct drm_fb_helper *fb_helper,
55 struct drm_mode_fb_cmd2 *mode_cmd,
56 struct drm_gem_object **gobj_p)
58 const struct drm_format_info *info;
59 struct radeon_device *rdev = fb_helper->dev->dev_private;
60 struct drm_gem_object *gobj = NULL;
61 struct radeon_bo *rbo = NULL;
62 bool fb_tiled = false; /* useful for testing */
65 int aligned_size, size;
66 int height = mode_cmd->height;
69 info = drm_get_format_info(rdev->ddev, mode_cmd);
72 /* need to align pitch with crtc limits */
73 mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp,
76 if (rdev->family >= CHIP_R600)
77 height = ALIGN(mode_cmd->height, 8);
78 size = mode_cmd->pitches[0] * height;
79 aligned_size = ALIGN(size, PAGE_SIZE);
80 ret = radeon_gem_object_create(rdev, aligned_size, 0,
81 RADEON_GEM_DOMAIN_VRAM,
84 pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
87 rbo = gem_to_radeon_bo(gobj);
90 tiling_flags = RADEON_TILING_MACRO;
95 tiling_flags |= RADEON_TILING_SWAP_32BIT;
98 tiling_flags |= RADEON_TILING_SWAP_16BIT;
106 ret = radeon_bo_set_tiling_flags(rbo,
107 tiling_flags | RADEON_TILING_SURFACE,
108 mode_cmd->pitches[0]);
110 dev_err(rdev->dev, "FB failed to set tiling flags\n");
113 ret = radeon_bo_reserve(rbo, false);
114 if (unlikely(ret != 0))
115 goto err_radeon_fbdev_destroy_pinned_object;
116 /* Only 27 bit offset for legacy CRTC */
117 ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
118 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
121 radeon_bo_unreserve(rbo);
122 goto err_radeon_fbdev_destroy_pinned_object;
125 radeon_bo_check_tiling(rbo, 0, 0);
126 ret = radeon_bo_kmap(rbo, NULL);
127 radeon_bo_unreserve(rbo);
129 goto err_radeon_fbdev_destroy_pinned_object;
134 err_radeon_fbdev_destroy_pinned_object:
135 radeon_fbdev_destroy_pinned_object(gobj);
141 * Fbdev ops and struct fb_ops
144 static int radeon_fbdev_fb_open(struct fb_info *info, int user)
146 struct drm_fb_helper *fb_helper = info->par;
147 struct radeon_device *rdev = fb_helper->dev->dev_private;
150 ret = pm_runtime_get_sync(rdev->ddev->dev);
151 if (ret < 0 && ret != -EACCES)
152 goto err_pm_runtime_mark_last_busy;
156 err_pm_runtime_mark_last_busy:
157 pm_runtime_mark_last_busy(rdev->ddev->dev);
158 pm_runtime_put_autosuspend(rdev->ddev->dev);
162 static int radeon_fbdev_fb_release(struct fb_info *info, int user)
164 struct drm_fb_helper *fb_helper = info->par;
165 struct radeon_device *rdev = fb_helper->dev->dev_private;
167 pm_runtime_mark_last_busy(rdev->ddev->dev);
168 pm_runtime_put_autosuspend(rdev->ddev->dev);
173 static void radeon_fbdev_fb_destroy(struct fb_info *info)
175 struct drm_fb_helper *fb_helper = info->par;
176 struct drm_framebuffer *fb = fb_helper->fb;
177 struct drm_gem_object *gobj = drm_gem_fb_get_obj(fb, 0);
179 drm_fb_helper_fini(fb_helper);
181 drm_framebuffer_unregister_private(fb);
182 drm_framebuffer_cleanup(fb);
184 radeon_fbdev_destroy_pinned_object(gobj);
186 drm_client_release(&fb_helper->client);
187 drm_fb_helper_unprepare(fb_helper);
191 static const struct fb_ops radeon_fbdev_fb_ops = {
192 .owner = THIS_MODULE,
193 DRM_FB_HELPER_DEFAULT_OPS,
194 .fb_open = radeon_fbdev_fb_open,
195 .fb_release = radeon_fbdev_fb_release,
196 .fb_read = drm_fb_helper_cfb_read,
197 .fb_write = drm_fb_helper_cfb_write,
198 .fb_fillrect = drm_fb_helper_cfb_fillrect,
199 .fb_copyarea = drm_fb_helper_cfb_copyarea,
200 .fb_imageblit = drm_fb_helper_cfb_imageblit,
201 .fb_destroy = radeon_fbdev_fb_destroy,
205 * Fbdev helpers and struct drm_fb_helper_funcs
208 static int radeon_fbdev_fb_helper_fb_probe(struct drm_fb_helper *fb_helper,
209 struct drm_fb_helper_surface_size *sizes)
211 struct radeon_device *rdev = fb_helper->dev->dev_private;
212 struct drm_mode_fb_cmd2 mode_cmd = { };
213 struct fb_info *info;
214 struct drm_gem_object *gobj;
215 struct radeon_bo *rbo;
216 struct drm_framebuffer *fb;
220 mode_cmd.width = sizes->surface_width;
221 mode_cmd.height = sizes->surface_height;
223 /* avivo can't scanout real 24bpp */
224 if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
225 sizes->surface_bpp = 32;
227 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
228 sizes->surface_depth);
230 ret = radeon_fbdev_create_pinned_object(fb_helper, &mode_cmd, &gobj);
232 DRM_ERROR("failed to create fbcon object %d\n", ret);
235 rbo = gem_to_radeon_bo(gobj);
237 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
240 goto err_radeon_fbdev_destroy_pinned_object;
242 ret = radeon_framebuffer_init(rdev->ddev, fb, &mode_cmd, gobj);
244 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
251 /* okay we have an object now allocate the framebuffer */
252 info = drm_fb_helper_alloc_info(fb_helper);
255 goto err_drm_framebuffer_unregister_private;
258 info->fbops = &radeon_fbdev_fb_ops;
259 info->flags = FBINFO_DEFAULT;
260 /* radeon resume is fragile and needs a vt switch to help it along */
261 info->skip_vt_switch = false;
263 drm_fb_helper_fill_info(info, fb_helper, sizes);
265 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
266 info->fix.smem_start = rdev->mc.aper_base + tmp;
267 info->fix.smem_len = radeon_bo_size(rbo);
268 info->screen_base = (__force void __iomem *)rbo->kptr;
269 info->screen_size = radeon_bo_size(rbo);
271 memset_io(info->screen_base, 0, info->screen_size);
273 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
275 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
276 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
277 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
278 DRM_INFO("fb depth is %d\n", fb->format->depth);
279 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
283 err_drm_framebuffer_unregister_private:
284 fb_helper->fb = NULL;
285 drm_framebuffer_unregister_private(fb);
286 drm_framebuffer_cleanup(fb);
289 err_radeon_fbdev_destroy_pinned_object:
290 radeon_fbdev_destroy_pinned_object(gobj);
294 static const struct drm_fb_helper_funcs radeon_fbdev_fb_helper_funcs = {
295 .fb_probe = radeon_fbdev_fb_helper_fb_probe,
299 * Fbdev client and struct drm_client_funcs
302 static void radeon_fbdev_client_unregister(struct drm_client_dev *client)
304 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
305 struct drm_device *dev = fb_helper->dev;
306 struct radeon_device *rdev = dev->dev_private;
308 if (fb_helper->info) {
309 vga_switcheroo_client_fb_set(rdev->pdev, NULL);
310 drm_fb_helper_unregister_info(fb_helper);
312 drm_client_release(&fb_helper->client);
313 drm_fb_helper_unprepare(fb_helper);
318 static int radeon_fbdev_client_restore(struct drm_client_dev *client)
320 drm_fb_helper_lastclose(client->dev);
321 vga_switcheroo_process_delayed_switch();
326 static int radeon_fbdev_client_hotplug(struct drm_client_dev *client)
328 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
329 struct drm_device *dev = client->dev;
330 struct radeon_device *rdev = dev->dev_private;
334 return drm_fb_helper_hotplug_event(dev->fb_helper);
336 ret = drm_fb_helper_init(dev, fb_helper);
340 if (!drm_drv_uses_atomic_modeset(dev))
341 drm_helper_disable_unused_functions(dev);
343 ret = drm_fb_helper_initial_config(fb_helper);
345 goto err_drm_fb_helper_fini;
347 vga_switcheroo_client_fb_set(rdev->pdev, fb_helper->info);
351 err_drm_fb_helper_fini:
352 drm_fb_helper_fini(fb_helper);
354 drm_err(dev, "Failed to setup radeon fbdev emulation (ret=%d)\n", ret);
358 static const struct drm_client_funcs radeon_fbdev_client_funcs = {
359 .owner = THIS_MODULE,
360 .unregister = radeon_fbdev_client_unregister,
361 .restore = radeon_fbdev_client_restore,
362 .hotplug = radeon_fbdev_client_hotplug,
365 void radeon_fbdev_setup(struct radeon_device *rdev)
367 struct drm_fb_helper *fb_helper;
371 if (rdev->mc.real_vram_size <= (8 * 1024 * 1024))
373 else if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32 * 1024 * 1024))
376 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
379 drm_fb_helper_prepare(rdev->ddev, fb_helper, bpp_sel, &radeon_fbdev_fb_helper_funcs);
381 ret = drm_client_init(rdev->ddev, &fb_helper->client, "radeon-fbdev",
382 &radeon_fbdev_client_funcs);
384 drm_err(rdev->ddev, "Failed to register client: %d\n", ret);
385 goto err_drm_client_init;
388 ret = radeon_fbdev_client_hotplug(&fb_helper->client);
390 drm_dbg_kms(rdev->ddev, "client hotplug ret=%d\n", ret);
392 drm_client_register(&fb_helper->client);
397 drm_fb_helper_unprepare(fb_helper);
401 void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
403 if (rdev->ddev->fb_helper)
404 drm_fb_helper_set_suspend(rdev->ddev->fb_helper, state);
407 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
409 struct drm_fb_helper *fb_helper = rdev->ddev->fb_helper;
410 struct drm_gem_object *gobj;
415 gobj = drm_gem_fb_get_obj(fb_helper->fb, 0);
418 if (gobj != &robj->tbo.base)