1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018 Noralf Trønnes
6 #include <linux/list.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/seq_file.h>
10 #include <linux/slab.h>
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>
23 #include "drm_crtc_internal.h"
24 #include "drm_internal.h"
29 * This library provides support for clients running in the kernel like fbdev and bootsplash.
31 * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
34 static int drm_client_open(struct drm_client_dev *client)
36 struct drm_device *dev = client->dev;
37 struct drm_file *file;
39 file = drm_file_alloc(dev->primary);
43 mutex_lock(&dev->filelist_mutex);
44 list_add(&file->lhead, &dev->filelist_internal);
45 mutex_unlock(&dev->filelist_mutex);
52 static void drm_client_close(struct drm_client_dev *client)
54 struct drm_device *dev = client->dev;
56 mutex_lock(&dev->filelist_mutex);
57 list_del(&client->file->lhead);
58 mutex_unlock(&dev->filelist_mutex);
60 drm_file_free(client->file);
64 * drm_client_init - Initialise a DRM client
68 * @funcs: DRM client functions (optional)
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().
76 * Zero on success or negative error code on failure.
78 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
79 const char *name, const struct drm_client_funcs *funcs)
83 if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
86 if (funcs && !try_module_get(funcs->owner))
91 client->funcs = funcs;
93 ret = drm_client_modeset_create(client);
97 ret = drm_client_open(client);
106 drm_client_modeset_free(client);
109 module_put(funcs->owner);
113 EXPORT_SYMBOL(drm_client_init);
116 * drm_client_register - Register client
117 * @client: DRM client
119 * Add the client to the &drm_device client list to activate its callbacks.
120 * @client must be initialized by a call to drm_client_init(). After
121 * drm_client_register() it is no longer permissible to call drm_client_release()
122 * directly (outside the unregister callback), instead cleanup will happen
123 * automatically on driver unload.
125 void drm_client_register(struct drm_client_dev *client)
127 struct drm_device *dev = client->dev;
129 mutex_lock(&dev->clientlist_mutex);
130 list_add(&client->list, &dev->clientlist);
131 mutex_unlock(&dev->clientlist_mutex);
133 EXPORT_SYMBOL(drm_client_register);
136 * drm_client_release - Release DRM client resources
137 * @client: DRM client
139 * Releases resources by closing the &drm_file that was opened by drm_client_init().
140 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
142 * This function should only be called from the unregister callback. An exception
143 * is fbdev which cannot free the buffer if userspace has open file descriptors.
146 * Clients cannot initiate a release by themselves. This is done to keep the code simple.
147 * The driver has to be unloaded before the client can be unloaded.
149 void drm_client_release(struct drm_client_dev *client)
151 struct drm_device *dev = client->dev;
153 DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name);
155 drm_client_modeset_free(client);
156 drm_client_close(client);
159 module_put(client->funcs->owner);
161 EXPORT_SYMBOL(drm_client_release);
163 void drm_client_dev_unregister(struct drm_device *dev)
165 struct drm_client_dev *client, *tmp;
167 if (!drm_core_check_feature(dev, DRIVER_MODESET))
170 mutex_lock(&dev->clientlist_mutex);
171 list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
172 list_del(&client->list);
173 if (client->funcs && client->funcs->unregister) {
174 client->funcs->unregister(client);
176 drm_client_release(client);
180 mutex_unlock(&dev->clientlist_mutex);
184 * drm_client_dev_hotplug - Send hotplug event to clients
187 * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
189 * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
190 * don't need to call this function themselves.
192 void drm_client_dev_hotplug(struct drm_device *dev)
194 struct drm_client_dev *client;
197 if (!drm_core_check_feature(dev, DRIVER_MODESET))
200 mutex_lock(&dev->clientlist_mutex);
201 list_for_each_entry(client, &dev->clientlist, list) {
202 if (!client->funcs || !client->funcs->hotplug)
205 ret = client->funcs->hotplug(client);
206 DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
208 mutex_unlock(&dev->clientlist_mutex);
210 EXPORT_SYMBOL(drm_client_dev_hotplug);
212 void drm_client_dev_restore(struct drm_device *dev)
214 struct drm_client_dev *client;
217 if (!drm_core_check_feature(dev, DRIVER_MODESET))
220 mutex_lock(&dev->clientlist_mutex);
221 list_for_each_entry(client, &dev->clientlist, list) {
222 if (!client->funcs || !client->funcs->restore)
225 ret = client->funcs->restore(client);
226 DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
227 if (!ret) /* The first one to return zero gets the privilege to restore */
230 mutex_unlock(&dev->clientlist_mutex);
233 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
235 struct drm_device *dev = buffer->client->dev;
237 drm_gem_vunmap(buffer->gem, buffer->vaddr);
240 drm_gem_object_put_unlocked(buffer->gem);
243 drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
248 static struct drm_client_buffer *
249 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
251 const struct drm_format_info *info = drm_format_info(format);
252 struct drm_mode_create_dumb dumb_args = { };
253 struct drm_device *dev = client->dev;
254 struct drm_client_buffer *buffer;
255 struct drm_gem_object *obj;
258 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
260 return ERR_PTR(-ENOMEM);
262 buffer->client = client;
264 dumb_args.width = width;
265 dumb_args.height = height;
266 dumb_args.bpp = info->cpp[0] * 8;
267 ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
271 buffer->handle = dumb_args.handle;
272 buffer->pitch = dumb_args.pitch;
274 obj = drm_gem_object_lookup(client->file, dumb_args.handle);
285 drm_client_buffer_delete(buffer);
291 * drm_client_buffer_vmap - Map DRM client buffer into address space
292 * @buffer: DRM client buffer
294 * This function maps a client buffer into kernel address space. If the
295 * buffer is already mapped, it returns the mapping's address.
297 * Client buffer mappings are not ref'counted. Each call to
298 * drm_client_buffer_vmap() should be followed by a call to
299 * drm_client_buffer_vunmap(); or the client buffer should be mapped
300 * throughout its lifetime.
303 * The mapped memory's address
305 void *drm_client_buffer_vmap(struct drm_client_buffer *buffer)
310 return buffer->vaddr;
313 * FIXME: The dependency on GEM here isn't required, we could
314 * convert the driver handle to a dma-buf instead and use the
315 * backend-agnostic dma-buf vmap support instead. This would
316 * require that the handle2fd prime ioctl is reworked to pull the
317 * fd_install step out of the driver backend hooks, to make that
318 * final step optional for internal users.
320 vaddr = drm_gem_vmap(buffer->gem);
324 buffer->vaddr = vaddr;
328 EXPORT_SYMBOL(drm_client_buffer_vmap);
331 * drm_client_buffer_vunmap - Unmap DRM client buffer
332 * @buffer: DRM client buffer
334 * This function removes a client buffer's memory mapping. Calling this
335 * function is only required by clients that manage their buffer mappings
338 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
340 drm_gem_vunmap(buffer->gem, buffer->vaddr);
341 buffer->vaddr = NULL;
343 EXPORT_SYMBOL(drm_client_buffer_vunmap);
345 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
352 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
354 DRM_DEV_ERROR(buffer->client->dev->dev,
355 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
360 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
361 u32 width, u32 height, u32 format)
363 struct drm_client_dev *client = buffer->client;
364 struct drm_mode_fb_cmd fb_req = { };
365 const struct drm_format_info *info;
368 info = drm_format_info(format);
369 fb_req.bpp = info->cpp[0] * 8;
370 fb_req.depth = info->depth;
371 fb_req.width = width;
372 fb_req.height = height;
373 fb_req.handle = buffer->handle;
374 fb_req.pitch = buffer->pitch;
376 ret = drm_mode_addfb(client->dev, &fb_req, client->file);
380 buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
381 if (WARN_ON(!buffer->fb))
384 /* drop the reference we picked up in framebuffer lookup */
385 drm_framebuffer_put(buffer->fb);
387 strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
393 * drm_client_framebuffer_create - Create a client framebuffer
394 * @client: DRM client
395 * @width: Framebuffer width
396 * @height: Framebuffer height
397 * @format: Buffer format
399 * This function creates a &drm_client_buffer which consists of a
400 * &drm_framebuffer backed by a dumb buffer.
401 * Call drm_client_framebuffer_delete() to free the buffer.
404 * Pointer to a client buffer or an error pointer on failure.
406 struct drm_client_buffer *
407 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
409 struct drm_client_buffer *buffer;
412 buffer = drm_client_buffer_create(client, width, height, format);
416 ret = drm_client_buffer_addfb(buffer, width, height, format);
418 drm_client_buffer_delete(buffer);
424 EXPORT_SYMBOL(drm_client_framebuffer_create);
427 * drm_client_framebuffer_delete - Delete a client framebuffer
428 * @buffer: DRM client buffer (can be NULL)
430 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
435 drm_client_buffer_rmfb(buffer);
436 drm_client_buffer_delete(buffer);
438 EXPORT_SYMBOL(drm_client_framebuffer_delete);
440 #ifdef CONFIG_DEBUG_FS
441 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
443 struct drm_info_node *node = m->private;
444 struct drm_device *dev = node->minor->dev;
445 struct drm_printer p = drm_seq_file_printer(m);
446 struct drm_client_dev *client;
448 mutex_lock(&dev->clientlist_mutex);
449 list_for_each_entry(client, &dev->clientlist, list)
450 drm_printf(&p, "%s\n", client->name);
451 mutex_unlock(&dev->clientlist_mutex);
456 static const struct drm_info_list drm_client_debugfs_list[] = {
457 { "internal_clients", drm_client_debugfs_internal_clients, 0 },
460 int drm_client_debugfs_init(struct drm_minor *minor)
462 return drm_debugfs_create_files(drm_client_debugfs_list,
463 ARRAY_SIZE(drm_client_debugfs_list),
464 minor->debugfs_root, minor);