xref: /linux/drivers/gpu/drm/radeon/radeon_fbdev.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
19b926bcfSThomas Zimmermann /*
29b926bcfSThomas Zimmermann  * Copyright © 2007 David Airlie
39b926bcfSThomas Zimmermann  *
49b926bcfSThomas Zimmermann  * Permission is hereby granted, free of charge, to any person obtaining a
59b926bcfSThomas Zimmermann  * copy of this software and associated documentation files (the "Software"),
69b926bcfSThomas Zimmermann  * to deal in the Software without restriction, including without limitation
79b926bcfSThomas Zimmermann  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
89b926bcfSThomas Zimmermann  * and/or sell copies of the Software, and to permit persons to whom the
99b926bcfSThomas Zimmermann  * Software is furnished to do so, subject to the following conditions:
109b926bcfSThomas Zimmermann  *
119b926bcfSThomas Zimmermann  * The above copyright notice and this permission notice (including the next
129b926bcfSThomas Zimmermann  * paragraph) shall be included in all copies or substantial portions of the
139b926bcfSThomas Zimmermann  * Software.
149b926bcfSThomas Zimmermann  *
159b926bcfSThomas Zimmermann  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
169b926bcfSThomas Zimmermann  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
179b926bcfSThomas Zimmermann  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
189b926bcfSThomas Zimmermann  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
199b926bcfSThomas Zimmermann  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
209b926bcfSThomas Zimmermann  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
219b926bcfSThomas Zimmermann  * DEALINGS IN THE SOFTWARE.
229b926bcfSThomas Zimmermann  *
239b926bcfSThomas Zimmermann  * Authors:
249b926bcfSThomas Zimmermann  *     David Airlie
259b926bcfSThomas Zimmermann  */
269b926bcfSThomas Zimmermann 
276ebf23bfSThomas Zimmermann #include <linux/fb.h>
289b926bcfSThomas Zimmermann #include <linux/pci.h>
299b926bcfSThomas Zimmermann #include <linux/pm_runtime.h>
309b926bcfSThomas Zimmermann #include <linux/vga_switcheroo.h>
319b926bcfSThomas Zimmermann 
329b926bcfSThomas Zimmermann #include <drm/drm_crtc_helper.h>
339b926bcfSThomas Zimmermann #include <drm/drm_drv.h>
349b926bcfSThomas Zimmermann #include <drm/drm_fb_helper.h>
359b926bcfSThomas Zimmermann #include <drm/drm_fourcc.h>
369b926bcfSThomas Zimmermann #include <drm/drm_framebuffer.h>
379b926bcfSThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h>
389b926bcfSThomas Zimmermann 
399b926bcfSThomas Zimmermann #include "radeon.h"
409b926bcfSThomas Zimmermann 
radeon_fbdev_destroy_pinned_object(struct drm_gem_object * gobj)419b926bcfSThomas Zimmermann static void radeon_fbdev_destroy_pinned_object(struct drm_gem_object *gobj)
429b926bcfSThomas Zimmermann {
439b926bcfSThomas Zimmermann 	struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
449b926bcfSThomas Zimmermann 	int ret;
459b926bcfSThomas Zimmermann 
469b926bcfSThomas Zimmermann 	ret = radeon_bo_reserve(rbo, false);
479b926bcfSThomas Zimmermann 	if (likely(ret == 0)) {
489b926bcfSThomas Zimmermann 		radeon_bo_kunmap(rbo);
499b926bcfSThomas Zimmermann 		radeon_bo_unpin(rbo);
509b926bcfSThomas Zimmermann 		radeon_bo_unreserve(rbo);
519b926bcfSThomas Zimmermann 	}
529b926bcfSThomas Zimmermann 	drm_gem_object_put(gobj);
539b926bcfSThomas Zimmermann }
549b926bcfSThomas Zimmermann 
radeon_fbdev_create_pinned_object(struct drm_fb_helper * fb_helper,struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object ** gobj_p)559b926bcfSThomas Zimmermann static int radeon_fbdev_create_pinned_object(struct drm_fb_helper *fb_helper,
569b926bcfSThomas Zimmermann 					     struct drm_mode_fb_cmd2 *mode_cmd,
579b926bcfSThomas Zimmermann 					     struct drm_gem_object **gobj_p)
589b926bcfSThomas Zimmermann {
599b926bcfSThomas Zimmermann 	const struct drm_format_info *info;
609b926bcfSThomas Zimmermann 	struct radeon_device *rdev = fb_helper->dev->dev_private;
619b926bcfSThomas Zimmermann 	struct drm_gem_object *gobj = NULL;
629b926bcfSThomas Zimmermann 	struct radeon_bo *rbo = NULL;
639b926bcfSThomas Zimmermann 	bool fb_tiled = false; /* useful for testing */
649b926bcfSThomas Zimmermann 	u32 tiling_flags = 0;
659b926bcfSThomas Zimmermann 	int ret;
669b926bcfSThomas Zimmermann 	int aligned_size, size;
679b926bcfSThomas Zimmermann 	int height = mode_cmd->height;
689b926bcfSThomas Zimmermann 	u32 cpp;
699b926bcfSThomas Zimmermann 
70*fb1b5e1dSWu Hoi Pok 	info = drm_get_format_info(rdev_to_drm(rdev), mode_cmd);
719b926bcfSThomas Zimmermann 	cpp = info->cpp[0];
729b926bcfSThomas Zimmermann 
739b926bcfSThomas Zimmermann 	/* need to align pitch with crtc limits */
749b926bcfSThomas Zimmermann 	mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp,
759b926bcfSThomas Zimmermann 						  fb_tiled);
769b926bcfSThomas Zimmermann 
779b926bcfSThomas Zimmermann 	if (rdev->family >= CHIP_R600)
789b926bcfSThomas Zimmermann 		height = ALIGN(mode_cmd->height, 8);
799b926bcfSThomas Zimmermann 	size = mode_cmd->pitches[0] * height;
809b926bcfSThomas Zimmermann 	aligned_size = ALIGN(size, PAGE_SIZE);
819b926bcfSThomas Zimmermann 	ret = radeon_gem_object_create(rdev, aligned_size, 0,
829b926bcfSThomas Zimmermann 				       RADEON_GEM_DOMAIN_VRAM,
839b926bcfSThomas Zimmermann 				       0, true, &gobj);
849b926bcfSThomas Zimmermann 	if (ret) {
859b926bcfSThomas Zimmermann 		pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
869b926bcfSThomas Zimmermann 		return -ENOMEM;
879b926bcfSThomas Zimmermann 	}
889b926bcfSThomas Zimmermann 	rbo = gem_to_radeon_bo(gobj);
899b926bcfSThomas Zimmermann 
909b926bcfSThomas Zimmermann 	if (fb_tiled)
919b926bcfSThomas Zimmermann 		tiling_flags = RADEON_TILING_MACRO;
929b926bcfSThomas Zimmermann 
939b926bcfSThomas Zimmermann #ifdef __BIG_ENDIAN
949b926bcfSThomas Zimmermann 	switch (cpp) {
959b926bcfSThomas Zimmermann 	case 4:
969b926bcfSThomas Zimmermann 		tiling_flags |= RADEON_TILING_SWAP_32BIT;
979b926bcfSThomas Zimmermann 		break;
989b926bcfSThomas Zimmermann 	case 2:
999b926bcfSThomas Zimmermann 		tiling_flags |= RADEON_TILING_SWAP_16BIT;
1009b926bcfSThomas Zimmermann 		break;
1019b926bcfSThomas Zimmermann 	default:
1029b926bcfSThomas Zimmermann 		break;
1039b926bcfSThomas Zimmermann 	}
1049b926bcfSThomas Zimmermann #endif
1059b926bcfSThomas Zimmermann 
1069b926bcfSThomas Zimmermann 	if (tiling_flags) {
1079b926bcfSThomas Zimmermann 		ret = radeon_bo_set_tiling_flags(rbo,
1089b926bcfSThomas Zimmermann 						 tiling_flags | RADEON_TILING_SURFACE,
1099b926bcfSThomas Zimmermann 						 mode_cmd->pitches[0]);
1109b926bcfSThomas Zimmermann 		if (ret)
1119b926bcfSThomas Zimmermann 			dev_err(rdev->dev, "FB failed to set tiling flags\n");
1129b926bcfSThomas Zimmermann 	}
1139b926bcfSThomas Zimmermann 
1149b926bcfSThomas Zimmermann 	ret = radeon_bo_reserve(rbo, false);
1159b926bcfSThomas Zimmermann 	if (unlikely(ret != 0))
1169b926bcfSThomas Zimmermann 		goto err_radeon_fbdev_destroy_pinned_object;
1179b926bcfSThomas Zimmermann 	/* Only 27 bit offset for legacy CRTC */
1189b926bcfSThomas Zimmermann 	ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
1199b926bcfSThomas Zimmermann 				       ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
1209b926bcfSThomas Zimmermann 				       NULL);
1219b926bcfSThomas Zimmermann 	if (ret) {
1229b926bcfSThomas Zimmermann 		radeon_bo_unreserve(rbo);
1239b926bcfSThomas Zimmermann 		goto err_radeon_fbdev_destroy_pinned_object;
1249b926bcfSThomas Zimmermann 	}
1259b926bcfSThomas Zimmermann 	if (fb_tiled)
1269b926bcfSThomas Zimmermann 		radeon_bo_check_tiling(rbo, 0, 0);
1279b926bcfSThomas Zimmermann 	ret = radeon_bo_kmap(rbo, NULL);
1289b926bcfSThomas Zimmermann 	radeon_bo_unreserve(rbo);
1299b926bcfSThomas Zimmermann 	if (ret)
1309b926bcfSThomas Zimmermann 		goto err_radeon_fbdev_destroy_pinned_object;
1319b926bcfSThomas Zimmermann 
1329b926bcfSThomas Zimmermann 	*gobj_p = gobj;
1339b926bcfSThomas Zimmermann 	return 0;
1349b926bcfSThomas Zimmermann 
1359b926bcfSThomas Zimmermann err_radeon_fbdev_destroy_pinned_object:
1369b926bcfSThomas Zimmermann 	radeon_fbdev_destroy_pinned_object(gobj);
1379b926bcfSThomas Zimmermann 	*gobj_p = NULL;
1389b926bcfSThomas Zimmermann 	return ret;
1399b926bcfSThomas Zimmermann }
1409b926bcfSThomas Zimmermann 
1419b926bcfSThomas Zimmermann /*
1429b926bcfSThomas Zimmermann  * Fbdev ops and struct fb_ops
1439b926bcfSThomas Zimmermann  */
1449b926bcfSThomas Zimmermann 
radeon_fbdev_fb_open(struct fb_info * info,int user)1459b926bcfSThomas Zimmermann static int radeon_fbdev_fb_open(struct fb_info *info, int user)
1469b926bcfSThomas Zimmermann {
1479b926bcfSThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
1489b926bcfSThomas Zimmermann 	struct radeon_device *rdev = fb_helper->dev->dev_private;
1499b926bcfSThomas Zimmermann 	int ret;
1509b926bcfSThomas Zimmermann 
151*fb1b5e1dSWu Hoi Pok 	ret = pm_runtime_get_sync(rdev_to_drm(rdev)->dev);
1529b926bcfSThomas Zimmermann 	if (ret < 0 && ret != -EACCES)
1539b926bcfSThomas Zimmermann 		goto err_pm_runtime_mark_last_busy;
1549b926bcfSThomas Zimmermann 
1559b926bcfSThomas Zimmermann 	return 0;
1569b926bcfSThomas Zimmermann 
1579b926bcfSThomas Zimmermann err_pm_runtime_mark_last_busy:
158*fb1b5e1dSWu Hoi Pok 	pm_runtime_mark_last_busy(rdev_to_drm(rdev)->dev);
159*fb1b5e1dSWu Hoi Pok 	pm_runtime_put_autosuspend(rdev_to_drm(rdev)->dev);
1609b926bcfSThomas Zimmermann 	return ret;
1619b926bcfSThomas Zimmermann }
1629b926bcfSThomas Zimmermann 
radeon_fbdev_fb_release(struct fb_info * info,int user)1639b926bcfSThomas Zimmermann static int radeon_fbdev_fb_release(struct fb_info *info, int user)
1649b926bcfSThomas Zimmermann {
1659b926bcfSThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
1669b926bcfSThomas Zimmermann 	struct radeon_device *rdev = fb_helper->dev->dev_private;
1679b926bcfSThomas Zimmermann 
168*fb1b5e1dSWu Hoi Pok 	pm_runtime_mark_last_busy(rdev_to_drm(rdev)->dev);
169*fb1b5e1dSWu Hoi Pok 	pm_runtime_put_autosuspend(rdev_to_drm(rdev)->dev);
1709b926bcfSThomas Zimmermann 
1719b926bcfSThomas Zimmermann 	return 0;
1729b926bcfSThomas Zimmermann }
1739b926bcfSThomas Zimmermann 
radeon_fbdev_fb_destroy(struct fb_info * info)1749b926bcfSThomas Zimmermann static void radeon_fbdev_fb_destroy(struct fb_info *info)
1759b926bcfSThomas Zimmermann {
1769b926bcfSThomas Zimmermann 	struct drm_fb_helper *fb_helper = info->par;
1779b926bcfSThomas Zimmermann 	struct drm_framebuffer *fb = fb_helper->fb;
1789b926bcfSThomas Zimmermann 	struct drm_gem_object *gobj = drm_gem_fb_get_obj(fb, 0);
1799b926bcfSThomas Zimmermann 
1809b926bcfSThomas Zimmermann 	drm_fb_helper_fini(fb_helper);
1819b926bcfSThomas Zimmermann 
1829b926bcfSThomas Zimmermann 	drm_framebuffer_unregister_private(fb);
1839b926bcfSThomas Zimmermann 	drm_framebuffer_cleanup(fb);
1849b926bcfSThomas Zimmermann 	kfree(fb);
1859b926bcfSThomas Zimmermann 	radeon_fbdev_destroy_pinned_object(gobj);
1869b926bcfSThomas Zimmermann 
1879b926bcfSThomas Zimmermann 	drm_client_release(&fb_helper->client);
1889b926bcfSThomas Zimmermann 	drm_fb_helper_unprepare(fb_helper);
1899b926bcfSThomas Zimmermann 	kfree(fb_helper);
1909b926bcfSThomas Zimmermann }
1919b926bcfSThomas Zimmermann 
1929b926bcfSThomas Zimmermann static const struct fb_ops radeon_fbdev_fb_ops = {
1939b926bcfSThomas Zimmermann 	.owner = THIS_MODULE,
1949b926bcfSThomas Zimmermann 	.fb_open = radeon_fbdev_fb_open,
1959b926bcfSThomas Zimmermann 	.fb_release = radeon_fbdev_fb_release,
1964520844bSThomas Zimmermann 	FB_DEFAULT_IOMEM_OPS,
1976ebf23bfSThomas Zimmermann 	DRM_FB_HELPER_DEFAULT_OPS,
1989b926bcfSThomas Zimmermann 	.fb_destroy = radeon_fbdev_fb_destroy,
1999b926bcfSThomas Zimmermann };
2009b926bcfSThomas Zimmermann 
2019b926bcfSThomas Zimmermann /*
2029b926bcfSThomas Zimmermann  * Fbdev helpers and struct drm_fb_helper_funcs
2039b926bcfSThomas Zimmermann  */
2049b926bcfSThomas Zimmermann 
radeon_fbdev_fb_helper_fb_probe(struct drm_fb_helper * fb_helper,struct drm_fb_helper_surface_size * sizes)2059b926bcfSThomas Zimmermann static int radeon_fbdev_fb_helper_fb_probe(struct drm_fb_helper *fb_helper,
2069b926bcfSThomas Zimmermann 					   struct drm_fb_helper_surface_size *sizes)
2079b926bcfSThomas Zimmermann {
2089b926bcfSThomas Zimmermann 	struct radeon_device *rdev = fb_helper->dev->dev_private;
2099b926bcfSThomas Zimmermann 	struct drm_mode_fb_cmd2 mode_cmd = { };
2109b926bcfSThomas Zimmermann 	struct fb_info *info;
2119b926bcfSThomas Zimmermann 	struct drm_gem_object *gobj;
2129b926bcfSThomas Zimmermann 	struct radeon_bo *rbo;
2139b926bcfSThomas Zimmermann 	struct drm_framebuffer *fb;
2149b926bcfSThomas Zimmermann 	int ret;
2159b926bcfSThomas Zimmermann 	unsigned long tmp;
2169b926bcfSThomas Zimmermann 
2179b926bcfSThomas Zimmermann 	mode_cmd.width = sizes->surface_width;
2189b926bcfSThomas Zimmermann 	mode_cmd.height = sizes->surface_height;
2199b926bcfSThomas Zimmermann 
2209b926bcfSThomas Zimmermann 	/* avivo can't scanout real 24bpp */
2219b926bcfSThomas Zimmermann 	if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
2229b926bcfSThomas Zimmermann 		sizes->surface_bpp = 32;
2239b926bcfSThomas Zimmermann 
2249b926bcfSThomas Zimmermann 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
2259b926bcfSThomas Zimmermann 							  sizes->surface_depth);
2269b926bcfSThomas Zimmermann 
2279b926bcfSThomas Zimmermann 	ret = radeon_fbdev_create_pinned_object(fb_helper, &mode_cmd, &gobj);
2289b926bcfSThomas Zimmermann 	if (ret) {
2299b926bcfSThomas Zimmermann 		DRM_ERROR("failed to create fbcon object %d\n", ret);
2309b926bcfSThomas Zimmermann 		return ret;
2319b926bcfSThomas Zimmermann 	}
2329b926bcfSThomas Zimmermann 	rbo = gem_to_radeon_bo(gobj);
2339b926bcfSThomas Zimmermann 
2349b926bcfSThomas Zimmermann 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
2359b926bcfSThomas Zimmermann 	if (!fb) {
2369b926bcfSThomas Zimmermann 		ret = -ENOMEM;
2379b926bcfSThomas Zimmermann 		goto err_radeon_fbdev_destroy_pinned_object;
2389b926bcfSThomas Zimmermann 	}
239*fb1b5e1dSWu Hoi Pok 	ret = radeon_framebuffer_init(rdev_to_drm(rdev), fb, &mode_cmd, gobj);
2409b926bcfSThomas Zimmermann 	if (ret) {
2419b926bcfSThomas Zimmermann 		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
2429b926bcfSThomas Zimmermann 		goto err_kfree;
2439b926bcfSThomas Zimmermann 	}
2449b926bcfSThomas Zimmermann 
2459b926bcfSThomas Zimmermann 	/* setup helper */
2469b926bcfSThomas Zimmermann 	fb_helper->fb = fb;
2479b926bcfSThomas Zimmermann 
2489b926bcfSThomas Zimmermann 	/* okay we have an object now allocate the framebuffer */
2499b926bcfSThomas Zimmermann 	info = drm_fb_helper_alloc_info(fb_helper);
2509b926bcfSThomas Zimmermann 	if (IS_ERR(info)) {
2519b926bcfSThomas Zimmermann 		ret = PTR_ERR(info);
2529b926bcfSThomas Zimmermann 		goto err_drm_framebuffer_unregister_private;
2539b926bcfSThomas Zimmermann 	}
2549b926bcfSThomas Zimmermann 
2559b926bcfSThomas Zimmermann 	info->fbops = &radeon_fbdev_fb_ops;
25640e324e0SThomas Zimmermann 
2579b926bcfSThomas Zimmermann 	/* radeon resume is fragile and needs a vt switch to help it along */
2589b926bcfSThomas Zimmermann 	info->skip_vt_switch = false;
2599b926bcfSThomas Zimmermann 
2609b926bcfSThomas Zimmermann 	drm_fb_helper_fill_info(info, fb_helper, sizes);
2619b926bcfSThomas Zimmermann 
2629b926bcfSThomas Zimmermann 	tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
2639b926bcfSThomas Zimmermann 	info->fix.smem_start = rdev->mc.aper_base + tmp;
2649b926bcfSThomas Zimmermann 	info->fix.smem_len = radeon_bo_size(rbo);
2659b926bcfSThomas Zimmermann 	info->screen_base = (__force void __iomem *)rbo->kptr;
2669b926bcfSThomas Zimmermann 	info->screen_size = radeon_bo_size(rbo);
2679b926bcfSThomas Zimmermann 
2689b926bcfSThomas Zimmermann 	memset_io(info->screen_base, 0, info->screen_size);
2699b926bcfSThomas Zimmermann 
2709b926bcfSThomas Zimmermann 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
2719b926bcfSThomas Zimmermann 
2729b926bcfSThomas Zimmermann 	DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
2739b926bcfSThomas Zimmermann 	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
2749b926bcfSThomas Zimmermann 	DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
2759b926bcfSThomas Zimmermann 	DRM_INFO("fb depth is %d\n", fb->format->depth);
2769b926bcfSThomas Zimmermann 	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
2779b926bcfSThomas Zimmermann 
2789b926bcfSThomas Zimmermann 	return 0;
2799b926bcfSThomas Zimmermann 
2809b926bcfSThomas Zimmermann err_drm_framebuffer_unregister_private:
2819b926bcfSThomas Zimmermann 	fb_helper->fb = NULL;
2829b926bcfSThomas Zimmermann 	drm_framebuffer_unregister_private(fb);
2839b926bcfSThomas Zimmermann 	drm_framebuffer_cleanup(fb);
2849b926bcfSThomas Zimmermann err_kfree:
2859b926bcfSThomas Zimmermann 	kfree(fb);
2869b926bcfSThomas Zimmermann err_radeon_fbdev_destroy_pinned_object:
2879b926bcfSThomas Zimmermann 	radeon_fbdev_destroy_pinned_object(gobj);
2889b926bcfSThomas Zimmermann 	return ret;
2899b926bcfSThomas Zimmermann }
2909b926bcfSThomas Zimmermann 
2919b926bcfSThomas Zimmermann static const struct drm_fb_helper_funcs radeon_fbdev_fb_helper_funcs = {
2929b926bcfSThomas Zimmermann 	.fb_probe = radeon_fbdev_fb_helper_fb_probe,
2939b926bcfSThomas Zimmermann };
2949b926bcfSThomas Zimmermann 
2959b926bcfSThomas Zimmermann /*
2969b926bcfSThomas Zimmermann  * Fbdev client and struct drm_client_funcs
2979b926bcfSThomas Zimmermann  */
2989b926bcfSThomas Zimmermann 
radeon_fbdev_client_unregister(struct drm_client_dev * client)2999b926bcfSThomas Zimmermann static void radeon_fbdev_client_unregister(struct drm_client_dev *client)
3009b926bcfSThomas Zimmermann {
3019b926bcfSThomas Zimmermann 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
3029b926bcfSThomas Zimmermann 	struct drm_device *dev = fb_helper->dev;
3039b926bcfSThomas Zimmermann 	struct radeon_device *rdev = dev->dev_private;
3049b926bcfSThomas Zimmermann 
3059b926bcfSThomas Zimmermann 	if (fb_helper->info) {
3069b926bcfSThomas Zimmermann 		vga_switcheroo_client_fb_set(rdev->pdev, NULL);
307c069dbbcSThomas Zimmermann 		drm_helper_force_disable_all(dev);
3089b926bcfSThomas Zimmermann 		drm_fb_helper_unregister_info(fb_helper);
3099b926bcfSThomas Zimmermann 	} else {
3109b926bcfSThomas Zimmermann 		drm_client_release(&fb_helper->client);
3119b926bcfSThomas Zimmermann 		drm_fb_helper_unprepare(fb_helper);
3129b926bcfSThomas Zimmermann 		kfree(fb_helper);
3139b926bcfSThomas Zimmermann 	}
3149b926bcfSThomas Zimmermann }
3159b926bcfSThomas Zimmermann 
radeon_fbdev_client_restore(struct drm_client_dev * client)3169b926bcfSThomas Zimmermann static int radeon_fbdev_client_restore(struct drm_client_dev *client)
3179b926bcfSThomas Zimmermann {
3189b926bcfSThomas Zimmermann 	drm_fb_helper_lastclose(client->dev);
3199b926bcfSThomas Zimmermann 	vga_switcheroo_process_delayed_switch();
3209b926bcfSThomas Zimmermann 
3219b926bcfSThomas Zimmermann 	return 0;
3229b926bcfSThomas Zimmermann }
3239b926bcfSThomas Zimmermann 
radeon_fbdev_client_hotplug(struct drm_client_dev * client)3249b926bcfSThomas Zimmermann static int radeon_fbdev_client_hotplug(struct drm_client_dev *client)
3259b926bcfSThomas Zimmermann {
3269b926bcfSThomas Zimmermann 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
3279b926bcfSThomas Zimmermann 	struct drm_device *dev = client->dev;
3289b926bcfSThomas Zimmermann 	struct radeon_device *rdev = dev->dev_private;
3299b926bcfSThomas Zimmermann 	int ret;
3309b926bcfSThomas Zimmermann 
3319b926bcfSThomas Zimmermann 	if (dev->fb_helper)
3329b926bcfSThomas Zimmermann 		return drm_fb_helper_hotplug_event(dev->fb_helper);
3339b926bcfSThomas Zimmermann 
3349b926bcfSThomas Zimmermann 	ret = drm_fb_helper_init(dev, fb_helper);
3359b926bcfSThomas Zimmermann 	if (ret)
3369b926bcfSThomas Zimmermann 		goto err_drm_err;
3379b926bcfSThomas Zimmermann 
3389b926bcfSThomas Zimmermann 	if (!drm_drv_uses_atomic_modeset(dev))
3399b926bcfSThomas Zimmermann 		drm_helper_disable_unused_functions(dev);
3409b926bcfSThomas Zimmermann 
3419b926bcfSThomas Zimmermann 	ret = drm_fb_helper_initial_config(fb_helper);
3429b926bcfSThomas Zimmermann 	if (ret)
3439b926bcfSThomas Zimmermann 		goto err_drm_fb_helper_fini;
3449b926bcfSThomas Zimmermann 
3459b926bcfSThomas Zimmermann 	vga_switcheroo_client_fb_set(rdev->pdev, fb_helper->info);
3469b926bcfSThomas Zimmermann 
3479b926bcfSThomas Zimmermann 	return 0;
3489b926bcfSThomas Zimmermann 
3499b926bcfSThomas Zimmermann err_drm_fb_helper_fini:
3509b926bcfSThomas Zimmermann 	drm_fb_helper_fini(fb_helper);
3519b926bcfSThomas Zimmermann err_drm_err:
3529b926bcfSThomas Zimmermann 	drm_err(dev, "Failed to setup radeon fbdev emulation (ret=%d)\n", ret);
3539b926bcfSThomas Zimmermann 	return ret;
3549b926bcfSThomas Zimmermann }
3559b926bcfSThomas Zimmermann 
3569b926bcfSThomas Zimmermann static const struct drm_client_funcs radeon_fbdev_client_funcs = {
3579b926bcfSThomas Zimmermann 	.owner		= THIS_MODULE,
3589b926bcfSThomas Zimmermann 	.unregister	= radeon_fbdev_client_unregister,
3599b926bcfSThomas Zimmermann 	.restore	= radeon_fbdev_client_restore,
3609b926bcfSThomas Zimmermann 	.hotplug	= radeon_fbdev_client_hotplug,
3619b926bcfSThomas Zimmermann };
3629b926bcfSThomas Zimmermann 
radeon_fbdev_setup(struct radeon_device * rdev)3639b926bcfSThomas Zimmermann void radeon_fbdev_setup(struct radeon_device *rdev)
3649b926bcfSThomas Zimmermann {
3659b926bcfSThomas Zimmermann 	struct drm_fb_helper *fb_helper;
3669b926bcfSThomas Zimmermann 	int bpp_sel = 32;
3679b926bcfSThomas Zimmermann 	int ret;
3689b926bcfSThomas Zimmermann 
3699b926bcfSThomas Zimmermann 	if (rdev->mc.real_vram_size <= (8 * 1024 * 1024))
3709b926bcfSThomas Zimmermann 		bpp_sel = 8;
3719b926bcfSThomas Zimmermann 	else if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32 * 1024 * 1024))
3729b926bcfSThomas Zimmermann 		bpp_sel = 16;
3739b926bcfSThomas Zimmermann 
3749b926bcfSThomas Zimmermann 	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
3759b926bcfSThomas Zimmermann 	if (!fb_helper)
3769b926bcfSThomas Zimmermann 		return;
377*fb1b5e1dSWu Hoi Pok 	drm_fb_helper_prepare(rdev_to_drm(rdev), fb_helper, bpp_sel, &radeon_fbdev_fb_helper_funcs);
3789b926bcfSThomas Zimmermann 
379*fb1b5e1dSWu Hoi Pok 	ret = drm_client_init(rdev_to_drm(rdev), &fb_helper->client, "radeon-fbdev",
3809b926bcfSThomas Zimmermann 			      &radeon_fbdev_client_funcs);
3819b926bcfSThomas Zimmermann 	if (ret) {
382*fb1b5e1dSWu Hoi Pok 		drm_err(rdev_to_drm(rdev), "Failed to register client: %d\n", ret);
3839b926bcfSThomas Zimmermann 		goto err_drm_client_init;
3849b926bcfSThomas Zimmermann 	}
3859b926bcfSThomas Zimmermann 
3869b926bcfSThomas Zimmermann 	drm_client_register(&fb_helper->client);
3879b926bcfSThomas Zimmermann 
3889b926bcfSThomas Zimmermann 	return;
3899b926bcfSThomas Zimmermann 
3909b926bcfSThomas Zimmermann err_drm_client_init:
3919b926bcfSThomas Zimmermann 	drm_fb_helper_unprepare(fb_helper);
3929b926bcfSThomas Zimmermann 	kfree(fb_helper);
3939b926bcfSThomas Zimmermann }
3949b926bcfSThomas Zimmermann 
radeon_fbdev_set_suspend(struct radeon_device * rdev,int state)3959b926bcfSThomas Zimmermann void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
3969b926bcfSThomas Zimmermann {
397*fb1b5e1dSWu Hoi Pok 	if (rdev_to_drm(rdev)->fb_helper)
398*fb1b5e1dSWu Hoi Pok 		drm_fb_helper_set_suspend(rdev_to_drm(rdev)->fb_helper, state);
3999b926bcfSThomas Zimmermann }
4009b926bcfSThomas Zimmermann 
radeon_fbdev_robj_is_fb(struct radeon_device * rdev,struct radeon_bo * robj)4019b926bcfSThomas Zimmermann bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
4029b926bcfSThomas Zimmermann {
403*fb1b5e1dSWu Hoi Pok 	struct drm_fb_helper *fb_helper = rdev_to_drm(rdev)->fb_helper;
4049b926bcfSThomas Zimmermann 	struct drm_gem_object *gobj;
4059b926bcfSThomas Zimmermann 
4069b926bcfSThomas Zimmermann 	if (!fb_helper)
4079b926bcfSThomas Zimmermann 		return false;
4089b926bcfSThomas Zimmermann 
4099b926bcfSThomas Zimmermann 	gobj = drm_gem_fb_get_obj(fb_helper->fb, 0);
4109b926bcfSThomas Zimmermann 	if (!gobj)
4119b926bcfSThomas Zimmermann 		return false;
4129b926bcfSThomas Zimmermann 	if (gobj != &robj->tbo.base)
4139b926bcfSThomas Zimmermann 		return false;
4149b926bcfSThomas Zimmermann 
4159b926bcfSThomas Zimmermann 	return true;
4169b926bcfSThomas Zimmermann }
417