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