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_device.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_file.h>
16 #include <drm/drm_fourcc.h>
17 #include <drm/drm_framebuffer.h>
18 #include <drm/drm_gem.h>
19 #include <drm/drm_mode.h>
20 #include <drm/drm_print.h>
22 #include "drm_crtc_internal.h"
23 #include "drm_internal.h"
28 * This library provides support for clients running in the kernel like fbdev and bootsplash.
30 * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
33 static int drm_client_open(struct drm_client_dev *client)
35 struct drm_device *dev = client->dev;
36 struct drm_file *file;
38 file = drm_file_alloc(dev->primary);
42 mutex_lock(&dev->filelist_mutex);
43 list_add(&file->lhead, &dev->filelist_internal);
44 mutex_unlock(&dev->filelist_mutex);
51 static void drm_client_close(struct drm_client_dev *client)
53 struct drm_device *dev = client->dev;
55 mutex_lock(&dev->filelist_mutex);
56 list_del(&client->file->lhead);
57 mutex_unlock(&dev->filelist_mutex);
59 drm_file_free(client->file);
63 * drm_client_init - Initialise a DRM client
67 * @funcs: DRM client functions (optional)
69 * This initialises the client and opens a &drm_file.
70 * Use drm_client_register() to complete the process.
71 * The caller needs to hold a reference on @dev before calling this function.
72 * The client is freed when the &drm_device is unregistered. See drm_client_release().
75 * Zero on success or negative error code on failure.
77 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
78 const char *name, const struct drm_client_funcs *funcs)
82 if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
87 client->funcs = funcs;
89 ret = drm_client_modeset_create(client);
93 ret = drm_client_open(client);
102 drm_client_modeset_free(client);
105 EXPORT_SYMBOL(drm_client_init);
108 * drm_client_register - Register client
109 * @client: DRM client
111 * Add the client to the &drm_device client list to activate its callbacks.
112 * @client must be initialized by a call to drm_client_init(). After
113 * drm_client_register() it is no longer permissible to call drm_client_release()
114 * directly (outside the unregister callback), instead cleanup will happen
115 * automatically on driver unload.
117 * Registering a client generates a hotplug event that allows the client
118 * to set up its display from pre-existing outputs. The client must have
119 * initialized its state to able to handle the hotplug event successfully.
121 void drm_client_register(struct drm_client_dev *client)
123 struct drm_device *dev = client->dev;
126 mutex_lock(&dev->clientlist_mutex);
127 list_add(&client->list, &dev->clientlist);
129 if (client->funcs && client->funcs->hotplug) {
131 * Perform an initial hotplug event to pick up the
132 * display configuration for the client. This step
133 * has to be performed *after* registering the client
134 * in the list of clients, or a concurrent hotplug
135 * event might be lost; leaving the display off.
137 * Hold the clientlist_mutex as for a regular hotplug
140 ret = client->funcs->hotplug(client);
142 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
144 mutex_unlock(&dev->clientlist_mutex);
146 EXPORT_SYMBOL(drm_client_register);
149 * drm_client_release - Release DRM client resources
150 * @client: DRM client
152 * Releases resources by closing the &drm_file that was opened by drm_client_init().
153 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
155 * This function should only be called from the unregister callback. An exception
156 * is fbdev which cannot free the buffer if userspace has open file descriptors.
159 * Clients cannot initiate a release by themselves. This is done to keep the code simple.
160 * The driver has to be unloaded before the client can be unloaded.
162 void drm_client_release(struct drm_client_dev *client)
164 struct drm_device *dev = client->dev;
166 drm_dbg_kms(dev, "%s\n", client->name);
168 drm_client_modeset_free(client);
169 drm_client_close(client);
172 EXPORT_SYMBOL(drm_client_release);
174 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
177 drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
178 drm_gem_object_put(buffer->gem);
184 static struct drm_client_buffer *
185 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
186 u32 format, u32 *handle)
188 const struct drm_format_info *info = drm_format_info(format);
189 struct drm_mode_create_dumb dumb_args = { };
190 struct drm_device *dev = client->dev;
191 struct drm_client_buffer *buffer;
192 struct drm_gem_object *obj;
195 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
197 return ERR_PTR(-ENOMEM);
199 buffer->client = client;
201 dumb_args.width = width;
202 dumb_args.height = height;
203 dumb_args.bpp = drm_format_info_bpp(info, 0);
204 ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
208 obj = drm_gem_object_lookup(client->file, dumb_args.handle);
214 buffer->pitch = dumb_args.pitch;
216 *handle = dumb_args.handle;
221 drm_client_buffer_delete(buffer);
227 * drm_client_buffer_vmap_local - Map DRM client buffer into address space
228 * @buffer: DRM client buffer
229 * @map_copy: Returns the mapped memory's address
231 * This function maps a client buffer into kernel address space. If the
232 * buffer is already mapped, it returns the existing mapping's address.
234 * Client buffer mappings are not ref'counted. Each call to
235 * drm_client_buffer_vmap_local() should be closely followed by a call to
236 * drm_client_buffer_vunmap_local(). See drm_client_buffer_vmap() for
237 * long-term mappings.
239 * The returned address is a copy of the internal value. In contrast to
240 * other vmap interfaces, you don't need it for the client's vunmap
241 * function. So you can modify it at will during blit and draw operations.
244 * 0 on success, or a negative errno code otherwise.
246 int drm_client_buffer_vmap_local(struct drm_client_buffer *buffer,
247 struct iosys_map *map_copy)
249 struct drm_gem_object *gem = buffer->gem;
250 struct iosys_map *map = &buffer->map;
255 ret = drm_gem_vmap(gem, map);
257 goto err_drm_gem_vmap_unlocked;
262 err_drm_gem_vmap_unlocked:
266 EXPORT_SYMBOL(drm_client_buffer_vmap_local);
269 * drm_client_buffer_vunmap_local - Unmap DRM client buffer
270 * @buffer: DRM client buffer
272 * This function removes a client buffer's memory mapping established
273 * with drm_client_buffer_vunmap_local(). Calling this function is only
274 * required by clients that manage their buffer mappings by themselves.
276 void drm_client_buffer_vunmap_local(struct drm_client_buffer *buffer)
278 struct drm_gem_object *gem = buffer->gem;
279 struct iosys_map *map = &buffer->map;
281 drm_gem_vunmap(gem, map);
284 EXPORT_SYMBOL(drm_client_buffer_vunmap_local);
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
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.
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.
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.
304 * 0 on success, or a negative errno code otherwise.
307 drm_client_buffer_vmap(struct drm_client_buffer *buffer,
308 struct iosys_map *map_copy)
310 struct drm_gem_object *gem = buffer->gem;
311 struct iosys_map *map = &buffer->map;
316 ret = drm_gem_pin_locked(gem);
318 goto err_drm_gem_pin_locked;
319 ret = drm_gem_vmap(gem, map);
321 goto err_drm_gem_vmap;
330 drm_gem_unpin_locked(buffer->gem);
331 err_drm_gem_pin_locked:
335 EXPORT_SYMBOL(drm_client_buffer_vmap);
338 * drm_client_buffer_vunmap - Unmap DRM client buffer
339 * @buffer: DRM client buffer
341 * This function removes a client buffer's memory mapping. Calling this
342 * function is only required by clients that manage their buffer mappings
345 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
347 struct drm_gem_object *gem = buffer->gem;
348 struct iosys_map *map = &buffer->map;
351 drm_gem_vunmap(gem, map);
352 drm_gem_unpin_locked(gem);
355 EXPORT_SYMBOL(drm_client_buffer_vunmap);
357 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
364 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
366 drm_err(buffer->client->dev,
367 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
372 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
373 u32 width, u32 height, u32 format,
376 struct drm_client_dev *client = buffer->client;
377 struct drm_mode_fb_cmd2 fb_req = { };
380 fb_req.width = width;
381 fb_req.height = height;
382 fb_req.pixel_format = format;
383 fb_req.handles[0] = handle;
384 fb_req.pitches[0] = buffer->pitch;
386 ret = drm_mode_addfb2(client->dev, &fb_req, client->file);
390 buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
391 if (WARN_ON(!buffer->fb))
394 /* drop the reference we picked up in framebuffer lookup */
395 drm_framebuffer_put(buffer->fb);
397 strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
403 * drm_client_framebuffer_create - Create a client framebuffer
404 * @client: DRM client
405 * @width: Framebuffer width
406 * @height: Framebuffer height
407 * @format: Buffer format
409 * This function creates a &drm_client_buffer which consists of a
410 * &drm_framebuffer backed by a dumb buffer.
411 * Call drm_client_framebuffer_delete() to free the buffer.
414 * Pointer to a client buffer or an error pointer on failure.
416 struct drm_client_buffer *
417 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
419 struct drm_client_buffer *buffer;
423 buffer = drm_client_buffer_create(client, width, height, format,
428 ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
431 * The handle is only needed for creating the framebuffer, destroy it
432 * again to solve a circular dependency should anybody export the GEM
433 * object as DMA-buf. The framebuffer and our buffer structure are still
434 * holding references to the GEM object to prevent its destruction.
436 drm_mode_destroy_dumb(client->dev, handle, client->file);
439 drm_client_buffer_delete(buffer);
445 EXPORT_SYMBOL(drm_client_framebuffer_create);
448 * drm_client_framebuffer_delete - Delete a client framebuffer
449 * @buffer: DRM client buffer (can be NULL)
451 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
456 drm_client_buffer_rmfb(buffer);
457 drm_client_buffer_delete(buffer);
459 EXPORT_SYMBOL(drm_client_framebuffer_delete);
462 * drm_client_framebuffer_flush - Manually flush client framebuffer
463 * @buffer: DRM client buffer (can be NULL)
464 * @rect: Damage rectangle (if NULL flushes all)
466 * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
467 * for drivers that need it.
470 * Zero on success or negative error code on failure.
472 int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
474 if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
478 struct drm_clip_rect clip = {
485 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
489 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
492 EXPORT_SYMBOL(drm_client_framebuffer_flush);