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/module.h>
9 #include <linux/mutex.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
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>
24 #include "drm_crtc_internal.h"
25 #include "drm_internal.h"
30 * This library provides support for clients running in the kernel like fbdev and bootsplash.
32 * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
35 static int drm_client_open(struct drm_client_dev *client)
37 struct drm_device *dev = client->dev;
38 struct drm_file *file;
40 file = drm_file_alloc(dev->primary);
44 mutex_lock(&dev->filelist_mutex);
45 list_add(&file->lhead, &dev->filelist_internal);
46 mutex_unlock(&dev->filelist_mutex);
53 static void drm_client_close(struct drm_client_dev *client)
55 struct drm_device *dev = client->dev;
57 mutex_lock(&dev->filelist_mutex);
58 list_del(&client->file->lhead);
59 mutex_unlock(&dev->filelist_mutex);
61 drm_file_free(client->file);
65 * drm_client_init - Initialise a DRM client
69 * @funcs: DRM client functions (optional)
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().
77 * Zero on success or negative error code on failure.
79 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
80 const char *name, const struct drm_client_funcs *funcs)
84 if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
87 if (funcs && !try_module_get(funcs->owner))
92 client->funcs = funcs;
94 ret = drm_client_modeset_create(client);
98 ret = drm_client_open(client);
107 drm_client_modeset_free(client);
110 module_put(funcs->owner);
114 EXPORT_SYMBOL(drm_client_init);
117 * drm_client_register - Register client
118 * @client: DRM client
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.
126 void drm_client_register(struct drm_client_dev *client)
128 struct drm_device *dev = client->dev;
130 mutex_lock(&dev->clientlist_mutex);
131 list_add(&client->list, &dev->clientlist);
132 mutex_unlock(&dev->clientlist_mutex);
134 EXPORT_SYMBOL(drm_client_register);
137 * drm_client_release - Release DRM client resources
138 * @client: DRM client
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.
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.
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.
150 void drm_client_release(struct drm_client_dev *client)
152 struct drm_device *dev = client->dev;
154 drm_dbg_kms(dev, "%s\n", client->name);
156 drm_client_modeset_free(client);
157 drm_client_close(client);
160 module_put(client->funcs->owner);
162 EXPORT_SYMBOL(drm_client_release);
164 void drm_client_dev_unregister(struct drm_device *dev)
166 struct drm_client_dev *client, *tmp;
168 if (!drm_core_check_feature(dev, DRIVER_MODESET))
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);
177 drm_client_release(client);
181 mutex_unlock(&dev->clientlist_mutex);
185 * drm_client_dev_hotplug - Send hotplug event to clients
188 * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
190 * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
191 * don't need to call this function themselves.
193 void drm_client_dev_hotplug(struct drm_device *dev)
195 struct drm_client_dev *client;
198 if (!drm_core_check_feature(dev, DRIVER_MODESET))
201 if (!dev->mode_config.num_connector) {
202 drm_dbg_kms(dev, "No connectors found, will not send hotplug events!\n");
206 mutex_lock(&dev->clientlist_mutex);
207 list_for_each_entry(client, &dev->clientlist, list) {
208 if (!client->funcs || !client->funcs->hotplug)
211 if (client->hotplug_failed)
214 ret = client->funcs->hotplug(client);
215 drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
217 client->hotplug_failed = true;
219 mutex_unlock(&dev->clientlist_mutex);
221 EXPORT_SYMBOL(drm_client_dev_hotplug);
223 void drm_client_dev_restore(struct drm_device *dev)
225 struct drm_client_dev *client;
228 if (!drm_core_check_feature(dev, DRIVER_MODESET))
231 mutex_lock(&dev->clientlist_mutex);
232 list_for_each_entry(client, &dev->clientlist, list) {
233 if (!client->funcs || !client->funcs->restore)
236 ret = client->funcs->restore(client);
237 drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
238 if (!ret) /* The first one to return zero gets the privilege to restore */
241 mutex_unlock(&dev->clientlist_mutex);
244 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
246 struct drm_device *dev = buffer->client->dev;
249 drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
250 drm_gem_object_put(buffer->gem);
254 drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
259 static struct drm_client_buffer *
260 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
262 const struct drm_format_info *info = drm_format_info(format);
263 struct drm_mode_create_dumb dumb_args = { };
264 struct drm_device *dev = client->dev;
265 struct drm_client_buffer *buffer;
266 struct drm_gem_object *obj;
269 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
271 return ERR_PTR(-ENOMEM);
273 buffer->client = client;
275 dumb_args.width = width;
276 dumb_args.height = height;
277 dumb_args.bpp = drm_format_info_bpp(info, 0);
278 ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
282 buffer->handle = dumb_args.handle;
283 buffer->pitch = dumb_args.pitch;
285 obj = drm_gem_object_lookup(client->file, dumb_args.handle);
296 drm_client_buffer_delete(buffer);
302 * drm_client_buffer_vmap - Map DRM client buffer into address space
303 * @buffer: DRM client buffer
304 * @map_copy: Returns the mapped memory's address
306 * This function maps a client buffer into kernel address space. If the
307 * buffer is already mapped, it returns the existing mapping's address.
309 * Client buffer mappings are not ref'counted. Each call to
310 * drm_client_buffer_vmap() should be followed by a call to
311 * drm_client_buffer_vunmap(); or the client buffer should be mapped
312 * throughout its lifetime.
314 * The returned address is a copy of the internal value. In contrast to
315 * other vmap interfaces, you don't need it for the client's vunmap
316 * function. So you can modify it at will during blit and draw operations.
319 * 0 on success, or a negative errno code otherwise.
322 drm_client_buffer_vmap(struct drm_client_buffer *buffer,
323 struct iosys_map *map_copy)
325 struct iosys_map *map = &buffer->map;
329 * FIXME: The dependency on GEM here isn't required, we could
330 * convert the driver handle to a dma-buf instead and use the
331 * backend-agnostic dma-buf vmap support instead. This would
332 * require that the handle2fd prime ioctl is reworked to pull the
333 * fd_install step out of the driver backend hooks, to make that
334 * final step optional for internal users.
336 ret = drm_gem_vmap_unlocked(buffer->gem, map);
344 EXPORT_SYMBOL(drm_client_buffer_vmap);
347 * drm_client_buffer_vunmap - Unmap DRM client buffer
348 * @buffer: DRM client buffer
350 * This function removes a client buffer's memory mapping. Calling this
351 * function is only required by clients that manage their buffer mappings
354 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
356 struct iosys_map *map = &buffer->map;
358 drm_gem_vunmap_unlocked(buffer->gem, map);
360 EXPORT_SYMBOL(drm_client_buffer_vunmap);
362 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
369 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
371 drm_err(buffer->client->dev,
372 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
377 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
378 u32 width, u32 height, u32 format)
380 struct drm_client_dev *client = buffer->client;
381 struct drm_mode_fb_cmd fb_req = { };
382 const struct drm_format_info *info;
385 info = drm_format_info(format);
386 fb_req.bpp = drm_format_info_bpp(info, 0);
387 fb_req.depth = info->depth;
388 fb_req.width = width;
389 fb_req.height = height;
390 fb_req.handle = buffer->handle;
391 fb_req.pitch = buffer->pitch;
393 ret = drm_mode_addfb(client->dev, &fb_req, client->file);
397 buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
398 if (WARN_ON(!buffer->fb))
401 /* drop the reference we picked up in framebuffer lookup */
402 drm_framebuffer_put(buffer->fb);
404 strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
410 * drm_client_framebuffer_create - Create a client framebuffer
411 * @client: DRM client
412 * @width: Framebuffer width
413 * @height: Framebuffer height
414 * @format: Buffer format
416 * This function creates a &drm_client_buffer which consists of a
417 * &drm_framebuffer backed by a dumb buffer.
418 * Call drm_client_framebuffer_delete() to free the buffer.
421 * Pointer to a client buffer or an error pointer on failure.
423 struct drm_client_buffer *
424 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
426 struct drm_client_buffer *buffer;
429 buffer = drm_client_buffer_create(client, width, height, format);
433 ret = drm_client_buffer_addfb(buffer, width, height, format);
435 drm_client_buffer_delete(buffer);
441 EXPORT_SYMBOL(drm_client_framebuffer_create);
444 * drm_client_framebuffer_delete - Delete a client framebuffer
445 * @buffer: DRM client buffer (can be NULL)
447 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
452 drm_client_buffer_rmfb(buffer);
453 drm_client_buffer_delete(buffer);
455 EXPORT_SYMBOL(drm_client_framebuffer_delete);
458 * drm_client_framebuffer_flush - Manually flush client framebuffer
459 * @buffer: DRM client buffer (can be NULL)
460 * @rect: Damage rectangle (if NULL flushes all)
462 * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
463 * for drivers that need it.
466 * Zero on success or negative error code on failure.
468 int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
470 if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
474 struct drm_clip_rect clip = {
481 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
485 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
488 EXPORT_SYMBOL(drm_client_framebuffer_flush);
490 #ifdef CONFIG_DEBUG_FS
491 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
493 struct drm_debugfs_entry *entry = m->private;
494 struct drm_device *dev = entry->dev;
495 struct drm_printer p = drm_seq_file_printer(m);
496 struct drm_client_dev *client;
498 mutex_lock(&dev->clientlist_mutex);
499 list_for_each_entry(client, &dev->clientlist, list)
500 drm_printf(&p, "%s\n", client->name);
501 mutex_unlock(&dev->clientlist_mutex);
506 static const struct drm_debugfs_info drm_client_debugfs_list[] = {
507 { "internal_clients", drm_client_debugfs_internal_clients, 0 },
510 void drm_client_debugfs_init(struct drm_minor *minor)
512 drm_debugfs_add_files(minor->dev, drm_client_debugfs_list,
513 ARRAY_SIZE(drm_client_debugfs_list));