1 // SPDX-License-Identifier: GPL-2.0 or MIT
3 * Copyright 2018 Noralf Trønnes
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>
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)
88 client->funcs = funcs;
90 ret = drm_client_modeset_create(client);
94 ret = drm_client_open(client);
103 drm_client_modeset_free(client);
106 EXPORT_SYMBOL(drm_client_init);
109 * drm_client_register - Register client
110 * @client: DRM client
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.
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.
122 void drm_client_register(struct drm_client_dev *client)
124 struct drm_device *dev = client->dev;
127 mutex_lock(&dev->clientlist_mutex);
128 list_add(&client->list, &dev->clientlist);
130 if (client->funcs && client->funcs->hotplug) {
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.
138 * Hold the clientlist_mutex as for a regular hotplug
141 ret = client->funcs->hotplug(client);
143 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
145 mutex_unlock(&dev->clientlist_mutex);
147 EXPORT_SYMBOL(drm_client_register);
150 * drm_client_release - Release DRM client resources
151 * @client: DRM client
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.
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.
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.
163 void drm_client_release(struct drm_client_dev *client)
165 struct drm_device *dev = client->dev;
167 drm_dbg_kms(dev, "%s\n", client->name);
169 drm_client_modeset_free(client);
170 drm_client_close(client);
173 EXPORT_SYMBOL(drm_client_release);
176 * drm_client_dev_unregister - Unregister clients
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
184 * The helper drm_dev_unregister() calls this function. Drivers
185 * that use it don't need to call this function themselves.
187 void drm_client_dev_unregister(struct drm_device *dev)
189 struct drm_client_dev *client, *tmp;
191 if (!drm_core_check_feature(dev, DRIVER_MODESET))
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);
200 drm_client_release(client);
204 mutex_unlock(&dev->clientlist_mutex);
206 EXPORT_SYMBOL(drm_client_dev_unregister);
209 * drm_client_dev_hotplug - Send hotplug event to clients
212 * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
214 * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
215 * don't need to call this function themselves.
217 void drm_client_dev_hotplug(struct drm_device *dev)
219 struct drm_client_dev *client;
222 if (!drm_core_check_feature(dev, DRIVER_MODESET))
225 if (!dev->mode_config.num_connector) {
226 drm_dbg_kms(dev, "No connectors found, will not send hotplug events!\n");
230 mutex_lock(&dev->clientlist_mutex);
231 list_for_each_entry(client, &dev->clientlist, list) {
232 if (!client->funcs || !client->funcs->hotplug)
235 if (client->hotplug_failed)
238 ret = client->funcs->hotplug(client);
239 drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
241 client->hotplug_failed = true;
243 mutex_unlock(&dev->clientlist_mutex);
245 EXPORT_SYMBOL(drm_client_dev_hotplug);
247 void drm_client_dev_restore(struct drm_device *dev)
249 struct drm_client_dev *client;
252 if (!drm_core_check_feature(dev, DRIVER_MODESET))
255 mutex_lock(&dev->clientlist_mutex);
256 list_for_each_entry(client, &dev->clientlist, list) {
257 if (!client->funcs || !client->funcs->restore)
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 */
265 mutex_unlock(&dev->clientlist_mutex);
268 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
271 drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
272 drm_gem_object_put(buffer->gem);
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)
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;
289 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
291 return ERR_PTR(-ENOMEM);
293 buffer->client = client;
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);
302 obj = drm_gem_object_lookup(client->file, dumb_args.handle);
308 buffer->pitch = dumb_args.pitch;
310 *handle = dumb_args.handle;
315 drm_client_buffer_delete(buffer);
321 * drm_client_buffer_vmap - Map DRM client buffer into address space
322 * @buffer: DRM client buffer
323 * @map_copy: Returns the mapped memory's address
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.
328 * Client buffer mappings are not ref'counted. Each call to
329 * drm_client_buffer_vmap() should be followed by a call to
330 * drm_client_buffer_vunmap(); or the client buffer should be mapped
331 * throughout its lifetime.
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.
338 * 0 on success, or a negative errno code otherwise.
341 drm_client_buffer_vmap(struct drm_client_buffer *buffer,
342 struct iosys_map *map_copy)
344 struct iosys_map *map = &buffer->map;
348 * FIXME: The dependency on GEM here isn't required, we could
349 * convert the driver handle to a dma-buf instead and use the
350 * backend-agnostic dma-buf vmap support instead. This would
351 * require that the handle2fd prime ioctl is reworked to pull the
352 * fd_install step out of the driver backend hooks, to make that
353 * final step optional for internal users.
355 ret = drm_gem_vmap_unlocked(buffer->gem, map);
363 EXPORT_SYMBOL(drm_client_buffer_vmap);
366 * drm_client_buffer_vunmap - Unmap DRM client buffer
367 * @buffer: DRM client buffer
369 * This function removes a client buffer's memory mapping. Calling this
370 * function is only required by clients that manage their buffer mappings
373 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
375 struct iosys_map *map = &buffer->map;
377 drm_gem_vunmap_unlocked(buffer->gem, map);
379 EXPORT_SYMBOL(drm_client_buffer_vunmap);
381 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
388 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
390 drm_err(buffer->client->dev,
391 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
396 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
397 u32 width, u32 height, u32 format,
400 struct drm_client_dev *client = buffer->client;
401 struct drm_mode_fb_cmd2 fb_req = { };
404 fb_req.width = width;
405 fb_req.height = height;
406 fb_req.pixel_format = format;
407 fb_req.handles[0] = handle;
408 fb_req.pitches[0] = buffer->pitch;
410 ret = drm_mode_addfb2(client->dev, &fb_req, client->file);
414 buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
415 if (WARN_ON(!buffer->fb))
418 /* drop the reference we picked up in framebuffer lookup */
419 drm_framebuffer_put(buffer->fb);
421 strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
427 * drm_client_framebuffer_create - Create a client framebuffer
428 * @client: DRM client
429 * @width: Framebuffer width
430 * @height: Framebuffer height
431 * @format: Buffer format
433 * This function creates a &drm_client_buffer which consists of a
434 * &drm_framebuffer backed by a dumb buffer.
435 * Call drm_client_framebuffer_delete() to free the buffer.
438 * Pointer to a client buffer or an error pointer on failure.
440 struct drm_client_buffer *
441 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
443 struct drm_client_buffer *buffer;
447 buffer = drm_client_buffer_create(client, width, height, format,
452 ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
455 * The handle is only needed for creating the framebuffer, destroy it
456 * again to solve a circular dependency should anybody export the GEM
457 * object as DMA-buf. The framebuffer and our buffer structure are still
458 * holding references to the GEM object to prevent its destruction.
460 drm_mode_destroy_dumb(client->dev, handle, client->file);
463 drm_client_buffer_delete(buffer);
469 EXPORT_SYMBOL(drm_client_framebuffer_create);
472 * drm_client_framebuffer_delete - Delete a client framebuffer
473 * @buffer: DRM client buffer (can be NULL)
475 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
480 drm_client_buffer_rmfb(buffer);
481 drm_client_buffer_delete(buffer);
483 EXPORT_SYMBOL(drm_client_framebuffer_delete);
486 * drm_client_framebuffer_flush - Manually flush client framebuffer
487 * @buffer: DRM client buffer (can be NULL)
488 * @rect: Damage rectangle (if NULL flushes all)
490 * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
491 * for drivers that need it.
494 * Zero on success or negative error code on failure.
496 int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
498 if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
502 struct drm_clip_rect clip = {
509 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
513 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
516 EXPORT_SYMBOL(drm_client_framebuffer_flush);
518 #ifdef CONFIG_DEBUG_FS
519 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
521 struct drm_debugfs_entry *entry = m->private;
522 struct drm_device *dev = entry->dev;
523 struct drm_printer p = drm_seq_file_printer(m);
524 struct drm_client_dev *client;
526 mutex_lock(&dev->clientlist_mutex);
527 list_for_each_entry(client, &dev->clientlist, list)
528 drm_printf(&p, "%s\n", client->name);
529 mutex_unlock(&dev->clientlist_mutex);
534 static const struct drm_debugfs_info drm_client_debugfs_list[] = {
535 { "internal_clients", drm_client_debugfs_internal_clients, 0 },
538 void drm_client_debugfs_init(struct drm_device *dev)
540 drm_debugfs_add_files(dev, drm_client_debugfs_list,
541 ARRAY_SIZE(drm_client_debugfs_list));