xref: /openbmc/linux/drivers/gpu/drm/drm_client.c (revision 22d55f02)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2018 Noralf Trønnes
4  */
5 
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>
11 
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>
22 
23 #include "drm_crtc_internal.h"
24 #include "drm_internal.h"
25 
26 /**
27  * DOC: overview
28  *
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.
31  *
32  * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
33  */
34 
35 static int drm_client_open(struct drm_client_dev *client)
36 {
37 	struct drm_device *dev = client->dev;
38 	struct drm_file *file;
39 
40 	file = drm_file_alloc(dev->primary);
41 	if (IS_ERR(file))
42 		return PTR_ERR(file);
43 
44 	mutex_lock(&dev->filelist_mutex);
45 	list_add(&file->lhead, &dev->filelist_internal);
46 	mutex_unlock(&dev->filelist_mutex);
47 
48 	client->file = file;
49 
50 	return 0;
51 }
52 
53 static void drm_client_close(struct drm_client_dev *client)
54 {
55 	struct drm_device *dev = client->dev;
56 
57 	mutex_lock(&dev->filelist_mutex);
58 	list_del(&client->file->lhead);
59 	mutex_unlock(&dev->filelist_mutex);
60 
61 	drm_file_free(client->file);
62 }
63 EXPORT_SYMBOL(drm_client_close);
64 
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)
71  *
72  * This initialises the client and opens a &drm_file.
73  * Use drm_client_register() to complete the process.
74  * The caller needs to hold a reference on @dev before calling this function.
75  * The client is freed when the &drm_device is unregistered. See drm_client_release().
76  *
77  * Returns:
78  * Zero on success or negative error code on failure.
79  */
80 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
81 		    const char *name, const struct drm_client_funcs *funcs)
82 {
83 	int ret;
84 
85 	if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
86 		return -EOPNOTSUPP;
87 
88 	if (funcs && !try_module_get(funcs->owner))
89 		return -ENODEV;
90 
91 	client->dev = dev;
92 	client->name = name;
93 	client->funcs = funcs;
94 
95 	ret = drm_client_open(client);
96 	if (ret)
97 		goto err_put_module;
98 
99 	drm_dev_get(dev);
100 
101 	return 0;
102 
103 err_put_module:
104 	if (funcs)
105 		module_put(funcs->owner);
106 
107 	return ret;
108 }
109 EXPORT_SYMBOL(drm_client_init);
110 
111 /**
112  * drm_client_register - Register client
113  * @client: DRM client
114  *
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_register() 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.
120  */
121 void drm_client_register(struct drm_client_dev *client)
122 {
123 	struct drm_device *dev = client->dev;
124 
125 	mutex_lock(&dev->clientlist_mutex);
126 	list_add(&client->list, &dev->clientlist);
127 	mutex_unlock(&dev->clientlist_mutex);
128 }
129 EXPORT_SYMBOL(drm_client_register);
130 
131 /**
132  * drm_client_release - Release DRM client resources
133  * @client: DRM client
134  *
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.
137  *
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.
140  *
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.
144  */
145 void drm_client_release(struct drm_client_dev *client)
146 {
147 	struct drm_device *dev = client->dev;
148 
149 	DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name);
150 
151 	drm_client_close(client);
152 	drm_dev_put(dev);
153 	if (client->funcs)
154 		module_put(client->funcs->owner);
155 }
156 EXPORT_SYMBOL(drm_client_release);
157 
158 void drm_client_dev_unregister(struct drm_device *dev)
159 {
160 	struct drm_client_dev *client, *tmp;
161 
162 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
163 		return;
164 
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);
173 		}
174 	}
175 	mutex_unlock(&dev->clientlist_mutex);
176 }
177 
178 /**
179  * drm_client_dev_hotplug - Send hotplug event to clients
180  * @dev: DRM device
181  *
182  * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
183  *
184  * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
185  * don't need to call this function themselves.
186  */
187 void drm_client_dev_hotplug(struct drm_device *dev)
188 {
189 	struct drm_client_dev *client;
190 	int ret;
191 
192 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
193 		return;
194 
195 	mutex_lock(&dev->clientlist_mutex);
196 	list_for_each_entry(client, &dev->clientlist, list) {
197 		if (!client->funcs || !client->funcs->hotplug)
198 			continue;
199 
200 		ret = client->funcs->hotplug(client);
201 		DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
202 	}
203 	mutex_unlock(&dev->clientlist_mutex);
204 }
205 EXPORT_SYMBOL(drm_client_dev_hotplug);
206 
207 void drm_client_dev_restore(struct drm_device *dev)
208 {
209 	struct drm_client_dev *client;
210 	int ret;
211 
212 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
213 		return;
214 
215 	mutex_lock(&dev->clientlist_mutex);
216 	list_for_each_entry(client, &dev->clientlist, list) {
217 		if (!client->funcs || !client->funcs->restore)
218 			continue;
219 
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;
224 	}
225 	mutex_unlock(&dev->clientlist_mutex);
226 }
227 
228 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
229 {
230 	struct drm_device *dev = buffer->client->dev;
231 
232 	drm_gem_vunmap(buffer->gem, buffer->vaddr);
233 
234 	if (buffer->gem)
235 		drm_gem_object_put_unlocked(buffer->gem);
236 
237 	if (buffer->handle)
238 		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
239 
240 	kfree(buffer);
241 }
242 
243 static struct drm_client_buffer *
244 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
245 {
246 	struct drm_mode_create_dumb dumb_args = { };
247 	struct drm_device *dev = client->dev;
248 	struct drm_client_buffer *buffer;
249 	struct drm_gem_object *obj;
250 	void *vaddr;
251 	int ret;
252 
253 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
254 	if (!buffer)
255 		return ERR_PTR(-ENOMEM);
256 
257 	buffer->client = client;
258 
259 	dumb_args.width = width;
260 	dumb_args.height = height;
261 	dumb_args.bpp = drm_format_plane_cpp(format, 0) * 8;
262 	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
263 	if (ret)
264 		goto err_delete;
265 
266 	buffer->handle = dumb_args.handle;
267 	buffer->pitch = dumb_args.pitch;
268 
269 	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
270 	if (!obj)  {
271 		ret = -ENOENT;
272 		goto err_delete;
273 	}
274 
275 	buffer->gem = obj;
276 
277 	/*
278 	 * FIXME: The dependency on GEM here isn't required, we could
279 	 * convert the driver handle to a dma-buf instead and use the
280 	 * backend-agnostic dma-buf vmap support instead. This would
281 	 * require that the handle2fd prime ioctl is reworked to pull the
282 	 * fd_install step out of the driver backend hooks, to make that
283 	 * final step optional for internal users.
284 	 */
285 	vaddr = drm_gem_vmap(obj);
286 	if (IS_ERR(vaddr)) {
287 		ret = PTR_ERR(vaddr);
288 		goto err_delete;
289 	}
290 
291 	buffer->vaddr = vaddr;
292 
293 	return buffer;
294 
295 err_delete:
296 	drm_client_buffer_delete(buffer);
297 
298 	return ERR_PTR(ret);
299 }
300 
301 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
302 {
303 	int ret;
304 
305 	if (!buffer->fb)
306 		return;
307 
308 	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
309 	if (ret)
310 		DRM_DEV_ERROR(buffer->client->dev->dev,
311 			      "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
312 
313 	buffer->fb = NULL;
314 }
315 
316 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
317 				   u32 width, u32 height, u32 format)
318 {
319 	struct drm_client_dev *client = buffer->client;
320 	struct drm_mode_fb_cmd fb_req = { };
321 	const struct drm_format_info *info;
322 	int ret;
323 
324 	info = drm_format_info(format);
325 	fb_req.bpp = info->cpp[0] * 8;
326 	fb_req.depth = info->depth;
327 	fb_req.width = width;
328 	fb_req.height = height;
329 	fb_req.handle = buffer->handle;
330 	fb_req.pitch = buffer->pitch;
331 
332 	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
333 	if (ret)
334 		return ret;
335 
336 	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
337 	if (WARN_ON(!buffer->fb))
338 		return -ENOENT;
339 
340 	/* drop the reference we picked up in framebuffer lookup */
341 	drm_framebuffer_put(buffer->fb);
342 
343 	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
344 
345 	return 0;
346 }
347 
348 /**
349  * drm_client_framebuffer_create - Create a client framebuffer
350  * @client: DRM client
351  * @width: Framebuffer width
352  * @height: Framebuffer height
353  * @format: Buffer format
354  *
355  * This function creates a &drm_client_buffer which consists of a
356  * &drm_framebuffer backed by a dumb buffer.
357  * Call drm_client_framebuffer_delete() to free the buffer.
358  *
359  * Returns:
360  * Pointer to a client buffer or an error pointer on failure.
361  */
362 struct drm_client_buffer *
363 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
364 {
365 	struct drm_client_buffer *buffer;
366 	int ret;
367 
368 	buffer = drm_client_buffer_create(client, width, height, format);
369 	if (IS_ERR(buffer))
370 		return buffer;
371 
372 	ret = drm_client_buffer_addfb(buffer, width, height, format);
373 	if (ret) {
374 		drm_client_buffer_delete(buffer);
375 		return ERR_PTR(ret);
376 	}
377 
378 	return buffer;
379 }
380 EXPORT_SYMBOL(drm_client_framebuffer_create);
381 
382 /**
383  * drm_client_framebuffer_delete - Delete a client framebuffer
384  * @buffer: DRM client buffer (can be NULL)
385  */
386 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
387 {
388 	if (!buffer)
389 		return;
390 
391 	drm_client_buffer_rmfb(buffer);
392 	drm_client_buffer_delete(buffer);
393 }
394 EXPORT_SYMBOL(drm_client_framebuffer_delete);
395 
396 #ifdef CONFIG_DEBUG_FS
397 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
398 {
399 	struct drm_info_node *node = m->private;
400 	struct drm_device *dev = node->minor->dev;
401 	struct drm_printer p = drm_seq_file_printer(m);
402 	struct drm_client_dev *client;
403 
404 	mutex_lock(&dev->clientlist_mutex);
405 	list_for_each_entry(client, &dev->clientlist, list)
406 		drm_printf(&p, "%s\n", client->name);
407 	mutex_unlock(&dev->clientlist_mutex);
408 
409 	return 0;
410 }
411 
412 static const struct drm_info_list drm_client_debugfs_list[] = {
413 	{ "internal_clients", drm_client_debugfs_internal_clients, 0 },
414 };
415 
416 int drm_client_debugfs_init(struct drm_minor *minor)
417 {
418 	return drm_debugfs_create_files(drm_client_debugfs_list,
419 					ARRAY_SIZE(drm_client_debugfs_list),
420 					minor->debugfs_root, minor);
421 }
422 #endif
423