xref: /linux/drivers/gpu/drm/drm_client.c (revision 51a8f9d7f587290944d6fc733d1f897091c63159)
1 // SPDX-License-Identifier: GPL-2.0 or MIT
2 /*
3  * Copyright 2018 Noralf Trønnes
4  */
5 
6 #include <linux/iosys-map.h>
7 #include <linux/list.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
12 
13 #include <drm/drm_client.h>
14 #include <drm/drm_debugfs.h>
15 #include <drm/drm_device.h>
16 #include <drm/drm_drv.h>
17 #include <drm/drm_file.h>
18 #include <drm/drm_fourcc.h>
19 #include <drm/drm_framebuffer.h>
20 #include <drm/drm_gem.h>
21 #include <drm/drm_mode.h>
22 #include <drm/drm_print.h>
23 
24 #include "drm_crtc_internal.h"
25 #include "drm_internal.h"
26 
27 /**
28  * DOC: overview
29  *
30  * This library provides support for clients running in the kernel like fbdev and bootsplash.
31  *
32  * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
33  */
34 
35 static int drm_client_open(struct drm_client_dev *client)
36 {
37 	struct drm_device *dev = client->dev;
38 	struct drm_file *file;
39 
40 	file = drm_file_alloc(dev->primary);
41 	if (IS_ERR(file))
42 		return PTR_ERR(file);
43 
44 	mutex_lock(&dev->filelist_mutex);
45 	list_add(&file->lhead, &dev->filelist_internal);
46 	mutex_unlock(&dev->filelist_mutex);
47 
48 	client->file = file;
49 
50 	return 0;
51 }
52 
53 static void drm_client_close(struct drm_client_dev *client)
54 {
55 	struct drm_device *dev = client->dev;
56 
57 	mutex_lock(&dev->filelist_mutex);
58 	list_del(&client->file->lhead);
59 	mutex_unlock(&dev->filelist_mutex);
60 
61 	drm_file_free(client->file);
62 }
63 
64 /**
65  * drm_client_init - Initialise a DRM client
66  * @dev: DRM device
67  * @client: DRM client
68  * @name: Client name
69  * @funcs: DRM client functions (optional)
70  *
71  * This initialises the client and opens a &drm_file.
72  * Use drm_client_register() to complete the process.
73  * The caller needs to hold a reference on @dev before calling this function.
74  * The client is freed when the &drm_device is unregistered. See drm_client_release().
75  *
76  * Returns:
77  * Zero on success or negative error code on failure.
78  */
79 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
80 		    const char *name, const struct drm_client_funcs *funcs)
81 {
82 	int ret;
83 
84 	if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
85 		return -EOPNOTSUPP;
86 
87 	if (funcs && !try_module_get(funcs->owner))
88 		return -ENODEV;
89 
90 	client->dev = dev;
91 	client->name = name;
92 	client->funcs = funcs;
93 
94 	ret = drm_client_modeset_create(client);
95 	if (ret)
96 		goto err_put_module;
97 
98 	ret = drm_client_open(client);
99 	if (ret)
100 		goto err_free;
101 
102 	drm_dev_get(dev);
103 
104 	return 0;
105 
106 err_free:
107 	drm_client_modeset_free(client);
108 err_put_module:
109 	if (funcs)
110 		module_put(funcs->owner);
111 
112 	return ret;
113 }
114 EXPORT_SYMBOL(drm_client_init);
115 
116 /**
117  * drm_client_register - Register client
118  * @client: DRM client
119  *
120  * Add the client to the &drm_device client list to activate its callbacks.
121  * @client must be initialized by a call to drm_client_init(). After
122  * drm_client_register() it is no longer permissible to call drm_client_release()
123  * directly (outside the unregister callback), instead cleanup will happen
124  * automatically on driver unload.
125  */
126 void drm_client_register(struct drm_client_dev *client)
127 {
128 	struct drm_device *dev = client->dev;
129 
130 	mutex_lock(&dev->clientlist_mutex);
131 	list_add(&client->list, &dev->clientlist);
132 	mutex_unlock(&dev->clientlist_mutex);
133 }
134 EXPORT_SYMBOL(drm_client_register);
135 
136 /**
137  * drm_client_release - Release DRM client resources
138  * @client: DRM client
139  *
140  * Releases resources by closing the &drm_file that was opened by drm_client_init().
141  * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
142  *
143  * This function should only be called from the unregister callback. An exception
144  * is fbdev which cannot free the buffer if userspace has open file descriptors.
145  *
146  * Note:
147  * Clients cannot initiate a release by themselves. This is done to keep the code simple.
148  * The driver has to be unloaded before the client can be unloaded.
149  */
150 void drm_client_release(struct drm_client_dev *client)
151 {
152 	struct drm_device *dev = client->dev;
153 
154 	drm_dbg_kms(dev, "%s\n", client->name);
155 
156 	drm_client_modeset_free(client);
157 	drm_client_close(client);
158 	drm_dev_put(dev);
159 	if (client->funcs)
160 		module_put(client->funcs->owner);
161 }
162 EXPORT_SYMBOL(drm_client_release);
163 
164 void drm_client_dev_unregister(struct drm_device *dev)
165 {
166 	struct drm_client_dev *client, *tmp;
167 
168 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
169 		return;
170 
171 	mutex_lock(&dev->clientlist_mutex);
172 	list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
173 		list_del(&client->list);
174 		if (client->funcs && client->funcs->unregister) {
175 			client->funcs->unregister(client);
176 		} else {
177 			drm_client_release(client);
178 			kfree(client);
179 		}
180 	}
181 	mutex_unlock(&dev->clientlist_mutex);
182 }
183 
184 /**
185  * drm_client_dev_hotplug - Send hotplug event to clients
186  * @dev: DRM device
187  *
188  * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
189  *
190  * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
191  * don't need to call this function themselves.
192  */
193 void drm_client_dev_hotplug(struct drm_device *dev)
194 {
195 	struct drm_client_dev *client;
196 	int ret;
197 
198 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
199 		return;
200 
201 	mutex_lock(&dev->clientlist_mutex);
202 	list_for_each_entry(client, &dev->clientlist, list) {
203 		if (!client->funcs || !client->funcs->hotplug)
204 			continue;
205 
206 		ret = client->funcs->hotplug(client);
207 		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
208 	}
209 	mutex_unlock(&dev->clientlist_mutex);
210 }
211 EXPORT_SYMBOL(drm_client_dev_hotplug);
212 
213 void drm_client_dev_restore(struct drm_device *dev)
214 {
215 	struct drm_client_dev *client;
216 	int ret;
217 
218 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
219 		return;
220 
221 	mutex_lock(&dev->clientlist_mutex);
222 	list_for_each_entry(client, &dev->clientlist, list) {
223 		if (!client->funcs || !client->funcs->restore)
224 			continue;
225 
226 		ret = client->funcs->restore(client);
227 		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
228 		if (!ret) /* The first one to return zero gets the privilege to restore */
229 			break;
230 	}
231 	mutex_unlock(&dev->clientlist_mutex);
232 }
233 
234 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
235 {
236 	if (buffer->gem) {
237 		drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
238 		drm_gem_object_put(buffer->gem);
239 	}
240 
241 	kfree(buffer);
242 }
243 
244 static struct drm_client_buffer *
245 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
246 			 u32 format, u32 *handle)
247 {
248 	const struct drm_format_info *info = drm_format_info(format);
249 	struct drm_mode_create_dumb dumb_args = { };
250 	struct drm_device *dev = client->dev;
251 	struct drm_client_buffer *buffer;
252 	struct drm_gem_object *obj;
253 	int ret;
254 
255 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
256 	if (!buffer)
257 		return ERR_PTR(-ENOMEM);
258 
259 	buffer->client = client;
260 
261 	dumb_args.width = width;
262 	dumb_args.height = height;
263 	dumb_args.bpp = drm_format_info_bpp(info, 0);
264 	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
265 	if (ret)
266 		goto err_delete;
267 
268 	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
269 	if (!obj)  {
270 		ret = -ENOENT;
271 		goto err_delete;
272 	}
273 
274 	buffer->pitch = dumb_args.pitch;
275 	buffer->gem = obj;
276 	*handle = dumb_args.handle;
277 
278 	return buffer;
279 
280 err_delete:
281 	drm_client_buffer_delete(buffer);
282 
283 	return ERR_PTR(ret);
284 }
285 
286 /**
287  * drm_client_buffer_vmap - Map DRM client buffer into address space
288  * @buffer: DRM client buffer
289  * @map_copy: Returns the mapped memory's address
290  *
291  * This function maps a client buffer into kernel address space. If the
292  * buffer is already mapped, it returns the existing mapping's address.
293  *
294  * Client buffer mappings are not ref'counted. Each call to
295  * drm_client_buffer_vmap() should be followed by a call to
296  * drm_client_buffer_vunmap(); or the client buffer should be mapped
297  * throughout its lifetime.
298  *
299  * The returned address is a copy of the internal value. In contrast to
300  * other vmap interfaces, you don't need it for the client's vunmap
301  * function. So you can modify it at will during blit and draw operations.
302  *
303  * Returns:
304  *	0 on success, or a negative errno code otherwise.
305  */
306 int
307 drm_client_buffer_vmap(struct drm_client_buffer *buffer,
308 		       struct iosys_map *map_copy)
309 {
310 	struct iosys_map *map = &buffer->map;
311 	int ret;
312 
313 	/*
314 	 * FIXME: The dependency on GEM here isn't required, we could
315 	 * convert the driver handle to a dma-buf instead and use the
316 	 * backend-agnostic dma-buf vmap support instead. This would
317 	 * require that the handle2fd prime ioctl is reworked to pull the
318 	 * fd_install step out of the driver backend hooks, to make that
319 	 * final step optional for internal users.
320 	 */
321 	ret = drm_gem_vmap_unlocked(buffer->gem, map);
322 	if (ret)
323 		return ret;
324 
325 	*map_copy = *map;
326 
327 	return 0;
328 }
329 EXPORT_SYMBOL(drm_client_buffer_vmap);
330 
331 /**
332  * drm_client_buffer_vunmap - Unmap DRM client buffer
333  * @buffer: DRM client buffer
334  *
335  * This function removes a client buffer's memory mapping. Calling this
336  * function is only required by clients that manage their buffer mappings
337  * by themselves.
338  */
339 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
340 {
341 	struct iosys_map *map = &buffer->map;
342 
343 	drm_gem_vunmap_unlocked(buffer->gem, map);
344 }
345 EXPORT_SYMBOL(drm_client_buffer_vunmap);
346 
347 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
348 {
349 	int ret;
350 
351 	if (!buffer->fb)
352 		return;
353 
354 	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
355 	if (ret)
356 		drm_err(buffer->client->dev,
357 			"Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
358 
359 	buffer->fb = NULL;
360 }
361 
362 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
363 				   u32 width, u32 height, u32 format,
364 				   u32 handle)
365 {
366 	struct drm_client_dev *client = buffer->client;
367 	struct drm_mode_fb_cmd fb_req = { };
368 	const struct drm_format_info *info;
369 	int ret;
370 
371 	info = drm_format_info(format);
372 	fb_req.bpp = drm_format_info_bpp(info, 0);
373 	fb_req.depth = info->depth;
374 	fb_req.width = width;
375 	fb_req.height = height;
376 	fb_req.handle = handle;
377 	fb_req.pitch = buffer->pitch;
378 
379 	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
380 	if (ret)
381 		return ret;
382 
383 	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
384 	if (WARN_ON(!buffer->fb))
385 		return -ENOENT;
386 
387 	/* drop the reference we picked up in framebuffer lookup */
388 	drm_framebuffer_put(buffer->fb);
389 
390 	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
391 
392 	return 0;
393 }
394 
395 /**
396  * drm_client_framebuffer_create - Create a client framebuffer
397  * @client: DRM client
398  * @width: Framebuffer width
399  * @height: Framebuffer height
400  * @format: Buffer format
401  *
402  * This function creates a &drm_client_buffer which consists of a
403  * &drm_framebuffer backed by a dumb buffer.
404  * Call drm_client_framebuffer_delete() to free the buffer.
405  *
406  * Returns:
407  * Pointer to a client buffer or an error pointer on failure.
408  */
409 struct drm_client_buffer *
410 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
411 {
412 	struct drm_client_buffer *buffer;
413 	u32 handle;
414 	int ret;
415 
416 	buffer = drm_client_buffer_create(client, width, height, format,
417 					  &handle);
418 	if (IS_ERR(buffer))
419 		return buffer;
420 
421 	ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
422 
423 	/*
424 	 * The handle is only needed for creating the framebuffer, destroy it
425 	 * again to solve a circular dependency should anybody export the GEM
426 	 * object as DMA-buf. The framebuffer and our buffer structure are still
427 	 * holding references to the GEM object to prevent its destruction.
428 	 */
429 	drm_mode_destroy_dumb(client->dev, handle, client->file);
430 
431 	if (ret) {
432 		drm_client_buffer_delete(buffer);
433 		return ERR_PTR(ret);
434 	}
435 
436 	return buffer;
437 }
438 EXPORT_SYMBOL(drm_client_framebuffer_create);
439 
440 /**
441  * drm_client_framebuffer_delete - Delete a client framebuffer
442  * @buffer: DRM client buffer (can be NULL)
443  */
444 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
445 {
446 	if (!buffer)
447 		return;
448 
449 	drm_client_buffer_rmfb(buffer);
450 	drm_client_buffer_delete(buffer);
451 }
452 EXPORT_SYMBOL(drm_client_framebuffer_delete);
453 
454 /**
455  * drm_client_framebuffer_flush - Manually flush client framebuffer
456  * @buffer: DRM client buffer (can be NULL)
457  * @rect: Damage rectangle (if NULL flushes all)
458  *
459  * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
460  * for drivers that need it.
461  *
462  * Returns:
463  * Zero on success or negative error code on failure.
464  */
465 int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
466 {
467 	if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
468 		return 0;
469 
470 	if (rect) {
471 		struct drm_clip_rect clip = {
472 			.x1 = rect->x1,
473 			.y1 = rect->y1,
474 			.x2 = rect->x2,
475 			.y2 = rect->y2,
476 		};
477 
478 		return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
479 						0, 0, &clip, 1);
480 	}
481 
482 	return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
483 					0, 0, NULL, 0);
484 }
485 EXPORT_SYMBOL(drm_client_framebuffer_flush);
486 
487 #ifdef CONFIG_DEBUG_FS
488 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
489 {
490 	struct drm_info_node *node = m->private;
491 	struct drm_device *dev = node->minor->dev;
492 	struct drm_printer p = drm_seq_file_printer(m);
493 	struct drm_client_dev *client;
494 
495 	mutex_lock(&dev->clientlist_mutex);
496 	list_for_each_entry(client, &dev->clientlist, list)
497 		drm_printf(&p, "%s\n", client->name);
498 	mutex_unlock(&dev->clientlist_mutex);
499 
500 	return 0;
501 }
502 
503 static const struct drm_info_list drm_client_debugfs_list[] = {
504 	{ "internal_clients", drm_client_debugfs_internal_clients, 0 },
505 };
506 
507 void drm_client_debugfs_init(struct drm_minor *minor)
508 {
509 	drm_debugfs_create_files(drm_client_debugfs_list,
510 				 ARRAY_SIZE(drm_client_debugfs_list),
511 				 minor->debugfs_root, minor);
512 }
513 #endif
514