File Index
Files and directories
Sign in
FILE

drm_fbdev_ttm.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/drm_fbdev_ttm.c C
  1// SPDX-License-Identifier: MIT
  2
  3#include <linux/export.h>
  4#include <linux/moduleparam.h>
  5#include <linux/vmalloc.h>
  6
  7#include <drm/drm_crtc_helper.h>
  8#include <drm/drm_drv.h>
  9#include <drm/drm_fb_helper.h>
 10#include <drm/drm_framebuffer.h>
 11#include <drm/drm_gem.h>
 12#include <drm/drm_print.h>
 13
 14#include <drm/drm_fbdev_ttm.h>
 15
 16/* @user: 1=userspace, 0=fbcon */
 17static int drm_fbdev_ttm_fb_open(struct fb_info *info, int user)
 18{
 19	struct drm_fb_helper *fb_helper = info->par;
 20
 21	/* No need to take a ref for fbcon because it unbinds on unregister */
 22	if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
 23		return -ENODEV;
 24
 25	return 0;
 26}
 27
 28static int drm_fbdev_ttm_fb_release(struct fb_info *info, int user)
 29{
 30	struct drm_fb_helper *fb_helper = info->par;
 31
 32	if (user)
 33		module_put(fb_helper->dev->driver->fops->owner);
 34
 35	return 0;
 36}
 37
 38FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(drm_fbdev_ttm,
 39				   drm_fb_helper_damage_range,
 40				   drm_fb_helper_damage_area);
 41
 42static void drm_fbdev_ttm_fb_destroy(struct fb_info *info)
 43{
 44	struct drm_fb_helper *fb_helper = info->par;
 45	void *shadow = info->screen_buffer;
 46
 47	if (!fb_helper->dev)
 48		return;
 49
 50	fb_deferred_io_cleanup(info);
 51	drm_fb_helper_fini(fb_helper);
 52	vfree(shadow);
 53	drm_client_buffer_delete(fb_helper->buffer);
 54
 55	drm_client_release(&fb_helper->client);
 56}
 57
 58static const struct fb_ops drm_fbdev_ttm_fb_ops = {
 59	.owner		= THIS_MODULE,
 60	.fb_open	= drm_fbdev_ttm_fb_open,
 61	.fb_release	= drm_fbdev_ttm_fb_release,
 62	FB_DEFAULT_DEFERRED_OPS(drm_fbdev_ttm),
 63	DRM_FB_HELPER_DEFAULT_OPS,
 64	.fb_destroy	= drm_fbdev_ttm_fb_destroy,
 65};
 66
 67static void drm_fbdev_ttm_damage_blit_real(struct drm_fb_helper *fb_helper,
 68					   struct drm_clip_rect *clip,
 69					   struct iosys_map *dst)
 70{
 71	struct drm_framebuffer *fb = fb_helper->fb;
 72	size_t offset = clip->y1 * fb->pitches[0];
 73	size_t len = clip->x2 - clip->x1;
 74	unsigned int y;
 75	void *src;
 76
 77	switch (drm_format_info_bpp(fb->format, 0)) {
 78	case 1:
 79		offset += clip->x1 / 8;
 80		len = DIV_ROUND_UP(len + clip->x1 % 8, 8);
 81		break;
 82	case 2:
 83		offset += clip->x1 / 4;
 84		len = DIV_ROUND_UP(len + clip->x1 % 4, 4);
 85		break;
 86	case 4:
 87		offset += clip->x1 / 2;
 88		len = DIV_ROUND_UP(len + clip->x1 % 2, 2);
 89		break;
 90	default:
 91		offset += clip->x1 * fb->format->cpp[0];
 92		len *= fb->format->cpp[0];
 93		break;
 94	}
 95
 96	src = fb_helper->info->screen_buffer + offset;
 97	iosys_map_incr(dst, offset); /* go to first pixel within clip rect */
 98
 99	for (y = clip->y1; y < clip->y2; y++) {
100		iosys_map_memcpy_to(dst, 0, src, len);
101		iosys_map_incr(dst, fb->pitches[0]);
102		src += fb->pitches[0];
103	}
104}
105
106static int drm_fbdev_ttm_damage_blit(struct drm_fb_helper *fb_helper,
107				     struct drm_clip_rect *clip)
108{
109	struct drm_client_buffer *buffer = fb_helper->buffer;
110	struct iosys_map map, dst;
111	int ret;
112
113	/*
114	 * We have to pin the client buffer to its current location while
115	 * flushing the shadow buffer. In the general case, concurrent
116	 * modesetting operations could try to move the buffer and would
117	 * fail. The modeset has to be serialized by acquiring the reservation
118	 * object of the underlying BO here.
119	 *
120	 * For fbdev emulation, we only have to protect against fbdev modeset
121	 * operations. Nothing else will involve the client buffer's BO. So it
122	 * is sufficient to acquire struct drm_fb_helper.lock here.
123	 */
124	mutex_lock(&fb_helper->lock);
125
126	ret = drm_client_buffer_vmap_local(buffer, &map);
127	if (ret)
128		goto out;
129
130	dst = map;
131	drm_fbdev_ttm_damage_blit_real(fb_helper, clip, &dst);
132
133	drm_client_buffer_vunmap_local(buffer);
134
135out:
136	mutex_unlock(&fb_helper->lock);
137
138	return ret;
139}
140
141static int drm_fbdev_ttm_helper_fb_dirty(struct drm_fb_helper *helper,
142					 struct drm_clip_rect *clip)
143{
144	struct drm_device *dev = helper->dev;
145	int ret;
146
147	/* Call damage handlers only if necessary */
148	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
149		return 0;
150
151	ret = drm_fbdev_ttm_damage_blit(helper, clip);
152	if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret))
153		return ret;
154
155	if (helper->fb->funcs->dirty) {
156		ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
157		if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret))
158			return ret;
159	}
160
161	return 0;
162}
163
164static const struct drm_fb_helper_funcs drm_fbdev_ttm_helper_funcs = {
165	.fb_dirty = drm_fbdev_ttm_helper_fb_dirty,
166};
167
168/*
169 * struct drm_driver
170 */
171
172int drm_fbdev_ttm_driver_fbdev_probe(struct drm_fb_helper *fb_helper,
173				     struct drm_fb_helper_surface_size *sizes)
174{
175	struct drm_client_dev *client = &fb_helper->client;
176	struct drm_device *dev = fb_helper->dev;
177	struct fb_info *info = fb_helper->info;
178	struct drm_client_buffer *buffer;
179	size_t screen_size;
180	void *screen_buffer;
181	u32 format;
182	int ret;
183
184	drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
185		    sizes->surface_width, sizes->surface_height,
186		    sizes->surface_bpp);
187
188	format = drm_driver_legacy_fb_format(dev, sizes->surface_bpp,
189					     sizes->surface_depth);
190	buffer = drm_client_buffer_create_dumb(client, sizes->surface_width,
191					       sizes->surface_height, format);
192	if (IS_ERR(buffer))
193		return PTR_ERR(buffer);
194
195	fb_helper->funcs = &drm_fbdev_ttm_helper_funcs;
196	fb_helper->buffer = buffer;
197	fb_helper->fb = buffer->fb;
198
199	screen_size = buffer->gem->size;
200	screen_buffer = vzalloc(screen_size);
201	if (!screen_buffer) {
202		ret = -ENOMEM;
203		goto err_drm_client_buffer_delete;
204	}
205
206	drm_fb_helper_fill_info(info, fb_helper, sizes);
207
208	info->fbops = &drm_fbdev_ttm_fb_ops;
209
210	/* screen */
211	info->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST;
212	info->screen_buffer = screen_buffer;
213	info->fix.smem_len = screen_size;
214
215	/* deferred I/O */
216	fb_helper->fbdefio.delay = HZ / 20;
217	fb_helper->fbdefio.deferred_io = drm_fb_helper_deferred_io;
218
219	info->fbdefio = &fb_helper->fbdefio;
220	ret = fb_deferred_io_init(info);
221	if (ret)
222		goto err_vfree;
223
224	return 0;
225
226err_vfree:
227	vfree(screen_buffer);
228err_drm_client_buffer_delete:
229	fb_helper->fb = NULL;
230	fb_helper->buffer = NULL;
231	drm_client_buffer_delete(buffer);
232	return ret;
233}
234EXPORT_SYMBOL(drm_fbdev_ttm_driver_fbdev_probe);
~/ Read-only file preview.