drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / drm_client.c
blobdf31c3815092b33f6fbbd6db1cebf341642e1b66
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2018 Noralf Trønnes
4 */
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_gem.h>
19 #include <drm/drm_mode.h>
20 #include <drm/drm_print.h>
21 #include <drm/drmP.h>
23 #include "drm_crtc_internal.h"
24 #include "drm_internal.h"
26 /**
27 * DOC: overview
29 * This library provides support for clients running in the kernel like fbdev and bootsplash.
30 * Currently it's only partially implemented, just enough to support fbdev.
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);
41 if (IS_ERR(file))
42 return PTR_ERR(file);
44 mutex_lock(&dev->filelist_mutex);
45 list_add(&file->lhead, &dev->filelist_internal);
46 mutex_unlock(&dev->filelist_mutex);
48 client->file = file;
50 return 0;
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);
63 EXPORT_SYMBOL(drm_client_close);
65 /**
66 * drm_client_init - Initialise a DRM client
67 * @dev: DRM device
68 * @client: DRM client
69 * @name: Client name
70 * @funcs: DRM client functions (optional)
72 * This initialises the client and opens a &drm_file. Use drm_client_add() 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().
76 * Returns:
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)
82 int ret;
84 if (!drm_core_check_feature(dev, DRIVER_MODESET) ||
85 !dev->driver->dumb_create || !dev->driver->gem_prime_vmap)
86 return -ENOTSUPP;
88 if (funcs && !try_module_get(funcs->owner))
89 return -ENODEV;
91 client->dev = dev;
92 client->name = name;
93 client->funcs = funcs;
95 ret = drm_client_open(client);
96 if (ret)
97 goto err_put_module;
99 drm_dev_get(dev);
101 return 0;
103 err_put_module:
104 if (funcs)
105 module_put(funcs->owner);
107 return ret;
109 EXPORT_SYMBOL(drm_client_init);
112 * drm_client_add - Add client to the device list
113 * @client: DRM client
115 * Add the client to the &drm_device client list to activate its callbacks.
116 * @client must be initialized by a call to drm_client_init(). After
117 * drm_client_add() it is no longer permissible to call drm_client_release()
118 * directly (outside the unregister callback), instead cleanup will happen
119 * automatically on driver unload.
121 void drm_client_add(struct drm_client_dev *client)
123 struct drm_device *dev = client->dev;
125 mutex_lock(&dev->clientlist_mutex);
126 list_add(&client->list, &dev->clientlist);
127 mutex_unlock(&dev->clientlist_mutex);
129 EXPORT_SYMBOL(drm_client_add);
132 * drm_client_release - Release DRM client resources
133 * @client: DRM client
135 * Releases resources by closing the &drm_file that was opened by drm_client_init().
136 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
138 * This function should only be called from the unregister callback. An exception
139 * is fbdev which cannot free the buffer if userspace has open file descriptors.
141 * Note:
142 * Clients cannot initiate a release by themselves. This is done to keep the code simple.
143 * The driver has to be unloaded before the client can be unloaded.
145 void drm_client_release(struct drm_client_dev *client)
147 struct drm_device *dev = client->dev;
149 DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name);
151 drm_client_close(client);
152 drm_dev_put(dev);
153 if (client->funcs)
154 module_put(client->funcs->owner);
156 EXPORT_SYMBOL(drm_client_release);
158 void drm_client_dev_unregister(struct drm_device *dev)
160 struct drm_client_dev *client, *tmp;
162 if (!drm_core_check_feature(dev, DRIVER_MODESET))
163 return;
165 mutex_lock(&dev->clientlist_mutex);
166 list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
167 list_del(&client->list);
168 if (client->funcs && client->funcs->unregister) {
169 client->funcs->unregister(client);
170 } else {
171 drm_client_release(client);
172 kfree(client);
175 mutex_unlock(&dev->clientlist_mutex);
179 * drm_client_dev_hotplug - Send hotplug event to clients
180 * @dev: DRM device
182 * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
184 * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
185 * don't need to call this function themselves.
187 void drm_client_dev_hotplug(struct drm_device *dev)
189 struct drm_client_dev *client;
190 int ret;
192 if (!drm_core_check_feature(dev, DRIVER_MODESET))
193 return;
195 mutex_lock(&dev->clientlist_mutex);
196 list_for_each_entry(client, &dev->clientlist, list) {
197 if (!client->funcs || !client->funcs->hotplug)
198 continue;
200 ret = client->funcs->hotplug(client);
201 DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
203 mutex_unlock(&dev->clientlist_mutex);
205 EXPORT_SYMBOL(drm_client_dev_hotplug);
207 void drm_client_dev_restore(struct drm_device *dev)
209 struct drm_client_dev *client;
210 int ret;
212 if (!drm_core_check_feature(dev, DRIVER_MODESET))
213 return;
215 mutex_lock(&dev->clientlist_mutex);
216 list_for_each_entry(client, &dev->clientlist, list) {
217 if (!client->funcs || !client->funcs->restore)
218 continue;
220 ret = client->funcs->restore(client);
221 DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
222 if (!ret) /* The first one to return zero gets the privilege to restore */
223 break;
225 mutex_unlock(&dev->clientlist_mutex);
228 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
230 struct drm_device *dev = buffer->client->dev;
232 if (buffer->vaddr && dev->driver->gem_prime_vunmap)
233 dev->driver->gem_prime_vunmap(buffer->gem, buffer->vaddr);
235 if (buffer->gem)
236 drm_gem_object_put_unlocked(buffer->gem);
238 if (buffer->handle)
239 drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
241 kfree(buffer);
244 static struct drm_client_buffer *
245 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
247 struct drm_mode_create_dumb dumb_args = { };
248 struct drm_device *dev = client->dev;
249 struct drm_client_buffer *buffer;
250 struct drm_gem_object *obj;
251 void *vaddr;
252 int ret;
254 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
255 if (!buffer)
256 return ERR_PTR(-ENOMEM);
258 buffer->client = client;
260 dumb_args.width = width;
261 dumb_args.height = height;
262 dumb_args.bpp = drm_format_plane_cpp(format, 0) * 8;
263 ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
264 if (ret)
265 goto err_delete;
267 buffer->handle = dumb_args.handle;
268 buffer->pitch = dumb_args.pitch;
270 obj = drm_gem_object_lookup(client->file, dumb_args.handle);
271 if (!obj) {
272 ret = -ENOENT;
273 goto err_delete;
276 buffer->gem = obj;
279 * FIXME: The dependency on GEM here isn't required, we could
280 * convert the driver handle to a dma-buf instead and use the
281 * backend-agnostic dma-buf vmap support instead. This would
282 * require that the handle2fd prime ioctl is reworked to pull the
283 * fd_install step out of the driver backend hooks, to make that
284 * final step optional for internal users.
286 vaddr = dev->driver->gem_prime_vmap(obj);
287 if (!vaddr) {
288 ret = -ENOMEM;
289 goto err_delete;
292 buffer->vaddr = vaddr;
294 return buffer;
296 err_delete:
297 drm_client_buffer_delete(buffer);
299 return ERR_PTR(ret);
302 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
304 int ret;
306 if (!buffer->fb)
307 return;
309 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
310 if (ret)
311 DRM_DEV_ERROR(buffer->client->dev->dev,
312 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
314 buffer->fb = NULL;
317 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
318 u32 width, u32 height, u32 format)
320 struct drm_client_dev *client = buffer->client;
321 struct drm_mode_fb_cmd fb_req = { };
322 const struct drm_format_info *info;
323 int ret;
325 info = drm_format_info(format);
326 fb_req.bpp = info->cpp[0] * 8;
327 fb_req.depth = info->depth;
328 fb_req.width = width;
329 fb_req.height = height;
330 fb_req.handle = buffer->handle;
331 fb_req.pitch = buffer->pitch;
333 ret = drm_mode_addfb(client->dev, &fb_req, client->file);
334 if (ret)
335 return ret;
337 buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
338 if (WARN_ON(!buffer->fb))
339 return -ENOENT;
341 /* drop the reference we picked up in framebuffer lookup */
342 drm_framebuffer_put(buffer->fb);
344 strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
346 return 0;
350 * drm_client_framebuffer_create - Create a client framebuffer
351 * @client: DRM client
352 * @width: Framebuffer width
353 * @height: Framebuffer height
354 * @format: Buffer format
356 * This function creates a &drm_client_buffer which consists of a
357 * &drm_framebuffer backed by a dumb buffer.
358 * Call drm_client_framebuffer_delete() to free the buffer.
360 * Returns:
361 * Pointer to a client buffer or an error pointer on failure.
363 struct drm_client_buffer *
364 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
366 struct drm_client_buffer *buffer;
367 int ret;
369 buffer = drm_client_buffer_create(client, width, height, format);
370 if (IS_ERR(buffer))
371 return buffer;
373 ret = drm_client_buffer_addfb(buffer, width, height, format);
374 if (ret) {
375 drm_client_buffer_delete(buffer);
376 return ERR_PTR(ret);
379 return buffer;
381 EXPORT_SYMBOL(drm_client_framebuffer_create);
384 * drm_client_framebuffer_delete - Delete a client framebuffer
385 * @buffer: DRM client buffer (can be NULL)
387 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
389 if (!buffer)
390 return;
392 drm_client_buffer_rmfb(buffer);
393 drm_client_buffer_delete(buffer);
395 EXPORT_SYMBOL(drm_client_framebuffer_delete);
397 #ifdef CONFIG_DEBUG_FS
398 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
400 struct drm_info_node *node = m->private;
401 struct drm_device *dev = node->minor->dev;
402 struct drm_printer p = drm_seq_file_printer(m);
403 struct drm_client_dev *client;
405 mutex_lock(&dev->clientlist_mutex);
406 list_for_each_entry(client, &dev->clientlist, list)
407 drm_printf(&p, "%s\n", client->name);
408 mutex_unlock(&dev->clientlist_mutex);
410 return 0;
413 static const struct drm_info_list drm_client_debugfs_list[] = {
414 { "internal_clients", drm_client_debugfs_internal_clients, 0 },
417 int drm_client_debugfs_init(struct drm_minor *minor)
419 return drm_debugfs_create_files(drm_client_debugfs_list,
420 ARRAY_SIZE(drm_client_debugfs_list),
421 minor->debugfs_root, minor);
423 #endif