File Index
Files and directories
Sign in
FILE

radeon_fbdev.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/radeon/radeon_fbdev.c C
  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
 27#include <linux/fb.h>
 28#include <linux/pci.h>
 29#include <linux/pm_runtime.h>
 30#include <linux/vga_switcheroo.h>
 31
 32#include <drm/drm_crtc_helper.h>
 33#include <drm/drm_drv.h>
 34#include <drm/drm_fb_helper.h>
 35#include <drm/drm_fourcc.h>
 36#include <drm/drm_framebuffer.h>
 37#include <drm/drm_gem_framebuffer_helper.h>
 38
 39#include "radeon.h"
 40
 41static void radeon_fbdev_destroy_pinned_object(struct drm_gem_object *gobj)
 42{
 43	struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
 44	int ret;
 45
 46	ret = radeon_bo_reserve(rbo, false);
 47	if (likely(ret == 0)) {
 48		radeon_bo_kunmap(rbo);
 49		radeon_bo_unpin(rbo);
 50		radeon_bo_unreserve(rbo);
 51	}
 52	drm_gem_object_put(gobj);
 53}
 54
 55static int radeon_fbdev_create_pinned_object(struct drm_fb_helper *fb_helper,
 56					     const struct drm_format_info *info,
 57					     struct drm_mode_fb_cmd2 *mode_cmd,
 58					     struct drm_gem_object **gobj_p)
 59{
 60	struct radeon_device *rdev = fb_helper->dev->dev_private;
 61	struct drm_gem_object *gobj = NULL;
 62	struct radeon_bo *rbo = NULL;
 63	bool fb_tiled = false; /* useful for testing */
 64	u32 tiling_flags = 0;
 65	int ret;
 66	int aligned_size, size;
 67	int height = mode_cmd->height;
 68	u32 cpp;
 69
 70	cpp = info->cpp[0];
 71
 72	/* need to align pitch with crtc limits */
 73	mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp,
 74						  fb_tiled);
 75
 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,
 82				       0, true, &gobj);
 83	if (ret) {
 84		pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
 85		return -ENOMEM;
 86	}
 87	rbo = gem_to_radeon_bo(gobj);
 88
 89	if (fb_tiled)
 90		tiling_flags = RADEON_TILING_MACRO;
 91
 92#ifdef __BIG_ENDIAN
 93	switch (cpp) {
 94	case 4:
 95		tiling_flags |= RADEON_TILING_SWAP_32BIT;
 96		break;
 97	case 2:
 98		tiling_flags |= RADEON_TILING_SWAP_16BIT;
 99		break;
100	default:
101		break;
102	}
103#endif
104
105	if (tiling_flags) {
106		ret = radeon_bo_set_tiling_flags(rbo,
107						 tiling_flags | RADEON_TILING_SURFACE,
108						 mode_cmd->pitches[0]);
109		if (ret)
110			dev_err(rdev->dev, "FB failed to set tiling flags\n");
111	}
112
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,
119				       NULL);
120	if (ret) {
121		radeon_bo_unreserve(rbo);
122		goto err_radeon_fbdev_destroy_pinned_object;
123	}
124	if (fb_tiled)
125		radeon_bo_check_tiling(rbo, 0, 0);
126	ret = radeon_bo_kmap(rbo, NULL);
127	radeon_bo_unreserve(rbo);
128	if (ret)
129		goto err_radeon_fbdev_destroy_pinned_object;
130
131	*gobj_p = gobj;
132	return 0;
133
134err_radeon_fbdev_destroy_pinned_object:
135	radeon_fbdev_destroy_pinned_object(gobj);
136	*gobj_p = NULL;
137	return ret;
138}
139
140/*
141 * Fbdev ops and struct fb_ops
142 */
143
144static int radeon_fbdev_fb_open(struct fb_info *info, int user)
145{
146	struct drm_fb_helper *fb_helper = info->par;
147	struct radeon_device *rdev = fb_helper->dev->dev_private;
148	int ret;
149
150	ret = pm_runtime_get_sync(rdev_to_drm(rdev)->dev);
151	if (ret < 0 && ret != -EACCES)
152		goto err_pm_runtime_mark_last_busy;
153
154	return 0;
155
156err_pm_runtime_mark_last_busy:
157	pm_runtime_put_autosuspend(rdev_to_drm(rdev)->dev);
158	return ret;
159}
160
161static int radeon_fbdev_fb_release(struct fb_info *info, int user)
162{
163	struct drm_fb_helper *fb_helper = info->par;
164	struct radeon_device *rdev = fb_helper->dev->dev_private;
165
166	pm_runtime_put_autosuspend(rdev_to_drm(rdev)->dev);
167
168	return 0;
169}
170
171static void radeon_fbdev_fb_destroy(struct fb_info *info)
172{
173	struct drm_fb_helper *fb_helper = info->par;
174	struct drm_framebuffer *fb = fb_helper->fb;
175	struct drm_gem_object *gobj = drm_gem_fb_get_obj(fb, 0);
176
177	drm_fb_helper_fini(fb_helper);
178
179	drm_framebuffer_unregister_private(fb);
180	drm_framebuffer_cleanup(fb);
181	kfree(fb);
182	radeon_fbdev_destroy_pinned_object(gobj);
183
184	drm_client_release(&fb_helper->client);
185}
186
187static const struct fb_ops radeon_fbdev_fb_ops = {
188	.owner = THIS_MODULE,
189	.fb_open = radeon_fbdev_fb_open,
190	.fb_release = radeon_fbdev_fb_release,
191	FB_DEFAULT_IOMEM_OPS,
192	DRM_FB_HELPER_DEFAULT_OPS,
193	.fb_destroy = radeon_fbdev_fb_destroy,
194};
195
196static const struct drm_fb_helper_funcs radeon_fbdev_fb_helper_funcs = {
197};
198
199int radeon_fbdev_driver_fbdev_probe(struct drm_fb_helper *fb_helper,
200				    struct drm_fb_helper_surface_size *sizes)
201{
202	struct radeon_device *rdev = fb_helper->dev->dev_private;
203	const struct drm_format_info *format_info;
204	struct drm_mode_fb_cmd2 mode_cmd = { };
205	struct fb_info *info = fb_helper->info;
206	struct drm_gem_object *gobj;
207	struct radeon_bo *rbo;
208	struct drm_framebuffer *fb;
209	int ret;
210	unsigned long tmp;
211
212	mode_cmd.width = sizes->surface_width;
213	mode_cmd.height = sizes->surface_height;
214
215	/* avivo can't scanout real 24bpp */
216	if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
217		sizes->surface_bpp = 32;
218
219	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
220							  sizes->surface_depth);
221
222	format_info = drm_get_format_info(rdev_to_drm(rdev), mode_cmd.pixel_format,
223					  mode_cmd.modifier[0]);
224	ret = radeon_fbdev_create_pinned_object(fb_helper, format_info, &mode_cmd, &gobj);
225	if (ret) {
226		DRM_ERROR("failed to create fbcon object %d\n", ret);
227		return ret;
228	}
229	rbo = gem_to_radeon_bo(gobj);
230
231	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
232	if (!fb) {
233		ret = -ENOMEM;
234		goto err_radeon_fbdev_destroy_pinned_object;
235	}
236	ret = radeon_framebuffer_init(rdev_to_drm(rdev), fb, format_info, &mode_cmd, gobj);
237	if (ret) {
238		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
239		goto err_kfree;
240	}
241
242	/* setup helper */
243	fb_helper->funcs = &radeon_fbdev_fb_helper_funcs;
244	fb_helper->fb = fb;
245
246	info->fbops = &radeon_fbdev_fb_ops;
247
248	/* radeon resume is fragile and needs a vt switch to help it along */
249	info->skip_vt_switch = false;
250
251	drm_fb_helper_fill_info(info, fb_helper, sizes);
252
253	tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
254	info->fix.smem_start = rdev->mc.aper_base + tmp;
255	info->fix.smem_len = radeon_bo_size(rbo);
256	info->screen_base = (__force void __iomem *)rbo->kptr;
257	info->screen_size = radeon_bo_size(rbo);
258
259	memset_io(info->screen_base, 0, info->screen_size);
260
261	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
262
263	DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
264	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
265	DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
266	DRM_INFO("fb depth is %d\n", fb->format->depth);
267	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
268
269	return 0;
270
271err_kfree:
272	kfree(fb);
273err_radeon_fbdev_destroy_pinned_object:
274	radeon_fbdev_destroy_pinned_object(gobj);
275	return ret;
276}
277
278bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
279{
280	struct drm_fb_helper *fb_helper = rdev_to_drm(rdev)->fb_helper;
281	struct drm_gem_object *gobj;
282
283	if (!fb_helper)
284		return false;
285
286	gobj = drm_gem_fb_get_obj(fb_helper->fb, 0);
287	if (!gobj)
288		return false;
289	if (gobj != &robj->tbo.base)
290		return false;
291
292	return true;
293}
~/ Read-only file preview.