xref: /openbmc/linux/drivers/gpu/drm/drm_client.c (revision 62e59c4e)
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. 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().
75  *
76  * Returns:
77  * Zero on success or negative error code on failure.
78  */
79 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
80 		    const char *name, const struct drm_client_funcs *funcs)
81 {
82 	int ret;
83 
84 	if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
85 		return -EOPNOTSUPP;
86 
87 	if (funcs && !try_module_get(funcs->owner))
88 		return -ENODEV;
89 
90 	client->dev = dev;
91 	client->name = name;
92 	client->funcs = funcs;
93 
94 	ret = drm_client_open(client);
95 	if (ret)
96 		goto err_put_module;
97 
98 	drm_dev_get(dev);
99 
100 	return 0;
101 
102 err_put_module:
103 	if (funcs)
104 		module_put(funcs->owner);
105 
106 	return ret;
107 }
108 EXPORT_SYMBOL(drm_client_init);
109 
110 /**
111  * drm_client_add - Add client to the device list
112  * @client: DRM client
113  *
114  * Add the client to the &drm_device client list to activate its callbacks.
115  * @client must be initialized by a call to drm_client_init(). After
116  * drm_client_add() it is no longer permissible to call drm_client_release()
117  * directly (outside the unregister callback), instead cleanup will happen
118  * automatically on driver unload.
119  */
120 void drm_client_add(struct drm_client_dev *client)
121 {
122 	struct drm_device *dev = client->dev;
123 
124 	mutex_lock(&dev->clientlist_mutex);
125 	list_add(&client->list, &dev->clientlist);
126 	mutex_unlock(&dev->clientlist_mutex);
127 }
128 EXPORT_SYMBOL(drm_client_add);
129 
130 /**
131  * drm_client_release - Release DRM client resources
132  * @client: DRM client
133  *
134  * Releases resources by closing the &drm_file that was opened by drm_client_init().
135  * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
136  *
137  * This function should only be called from the unregister callback. An exception
138  * is fbdev which cannot free the buffer if userspace has open file descriptors.
139  *
140  * Note:
141  * Clients cannot initiate a release by themselves. This is done to keep the code simple.
142  * The driver has to be unloaded before the client can be unloaded.
143  */
144 void drm_client_release(struct drm_client_dev *client)
145 {
146 	struct drm_device *dev = client->dev;
147 
148 	DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name);
149 
150 	drm_client_close(client);
151 	drm_dev_put(dev);
152 	if (client->funcs)
153 		module_put(client->funcs->owner);
154 }
155 EXPORT_SYMBOL(drm_client_release);
156 
157 void drm_client_dev_unregister(struct drm_device *dev)
158 {
159 	struct drm_client_dev *client, *tmp;
160 
161 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
162 		return;
163 
164 	mutex_lock(&dev->clientlist_mutex);
165 	list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
166 		list_del(&client->list);
167 		if (client->funcs && client->funcs->unregister) {
168 			client->funcs->unregister(client);
169 		} else {
170 			drm_client_release(client);
171 			kfree(client);
172 		}
173 	}
174 	mutex_unlock(&dev->clientlist_mutex);
175 }
176 
177 /**
178  * drm_client_dev_hotplug - Send hotplug event to clients
179  * @dev: DRM device
180  *
181  * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
182  *
183  * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
184  * don't need to call this function themselves.
185  */
186 void drm_client_dev_hotplug(struct drm_device *dev)
187 {
188 	struct drm_client_dev *client;
189 	int ret;
190 
191 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
192 		return;
193 
194 	mutex_lock(&dev->clientlist_mutex);
195 	list_for_each_entry(client, &dev->clientlist, list) {
196 		if (!client->funcs || !client->funcs->hotplug)
197 			continue;
198 
199 		ret = client->funcs->hotplug(client);
200 		DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
201 	}
202 	mutex_unlock(&dev->clientlist_mutex);
203 }
204 EXPORT_SYMBOL(drm_client_dev_hotplug);
205 
206 void drm_client_dev_restore(struct drm_device *dev)
207 {
208 	struct drm_client_dev *client;
209 	int ret;
210 
211 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
212 		return;
213 
214 	mutex_lock(&dev->clientlist_mutex);
215 	list_for_each_entry(client, &dev->clientlist, list) {
216 		if (!client->funcs || !client->funcs->restore)
217 			continue;
218 
219 		ret = client->funcs->restore(client);
220 		DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
221 		if (!ret) /* The first one to return zero gets the privilege to restore */
222 			break;
223 	}
224 	mutex_unlock(&dev->clientlist_mutex);
225 }
226 
227 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
228 {
229 	struct drm_device *dev = buffer->client->dev;
230 
231 	drm_gem_vunmap(buffer->gem, buffer->vaddr);
232 
233 	if (buffer->gem)
234 		drm_gem_object_put_unlocked(buffer->gem);
235 
236 	if (buffer->handle)
237 		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
238 
239 	kfree(buffer);
240 }
241 
242 static struct drm_client_buffer *
243 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
244 {
245 	struct drm_mode_create_dumb dumb_args = { };
246 	struct drm_device *dev = client->dev;
247 	struct drm_client_buffer *buffer;
248 	struct drm_gem_object *obj;
249 	void *vaddr;
250 	int ret;
251 
252 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
253 	if (!buffer)
254 		return ERR_PTR(-ENOMEM);
255 
256 	buffer->client = client;
257 
258 	dumb_args.width = width;
259 	dumb_args.height = height;
260 	dumb_args.bpp = drm_format_plane_cpp(format, 0) * 8;
261 	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
262 	if (ret)
263 		goto err_delete;
264 
265 	buffer->handle = dumb_args.handle;
266 	buffer->pitch = dumb_args.pitch;
267 
268 	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
269 	if (!obj)  {
270 		ret = -ENOENT;
271 		goto err_delete;
272 	}
273 
274 	buffer->gem = obj;
275 
276 	/*
277 	 * FIXME: The dependency on GEM here isn't required, we could
278 	 * convert the driver handle to a dma-buf instead and use the
279 	 * backend-agnostic dma-buf vmap support instead. This would
280 	 * require that the handle2fd prime ioctl is reworked to pull the
281 	 * fd_install step out of the driver backend hooks, to make that
282 	 * final step optional for internal users.
283 	 */
284 	vaddr = drm_gem_vmap(obj);
285 	if (IS_ERR(vaddr)) {
286 		ret = PTR_ERR(vaddr);
287 		goto err_delete;
288 	}
289 
290 	buffer->vaddr = vaddr;
291 
292 	return buffer;
293 
294 err_delete:
295 	drm_client_buffer_delete(buffer);
296 
297 	return ERR_PTR(ret);
298 }
299 
300 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
301 {
302 	int ret;
303 
304 	if (!buffer->fb)
305 		return;
306 
307 	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
308 	if (ret)
309 		DRM_DEV_ERROR(buffer->client->dev->dev,
310 			      "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
311 
312 	buffer->fb = NULL;
313 }
314 
315 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
316 				   u32 width, u32 height, u32 format)
317 {
318 	struct drm_client_dev *client = buffer->client;
319 	struct drm_mode_fb_cmd fb_req = { };
320 	const struct drm_format_info *info;
321 	int ret;
322 
323 	info = drm_format_info(format);
324 	fb_req.bpp = info->cpp[0] * 8;
325 	fb_req.depth = info->depth;
326 	fb_req.width = width;
327 	fb_req.height = height;
328 	fb_req.handle = buffer->handle;
329 	fb_req.pitch = buffer->pitch;
330 
331 	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
332 	if (ret)
333 		return ret;
334 
335 	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
336 	if (WARN_ON(!buffer->fb))
337 		return -ENOENT;
338 
339 	/* drop the reference we picked up in framebuffer lookup */
340 	drm_framebuffer_put(buffer->fb);
341 
342 	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
343 
344 	return 0;
345 }
346 
347 /**
348  * drm_client_framebuffer_create - Create a client framebuffer
349  * @client: DRM client
350  * @width: Framebuffer width
351  * @height: Framebuffer height
352  * @format: Buffer format
353  *
354  * This function creates a &drm_client_buffer which consists of a
355  * &drm_framebuffer backed by a dumb buffer.
356  * Call drm_client_framebuffer_delete() to free the buffer.
357  *
358  * Returns:
359  * Pointer to a client buffer or an error pointer on failure.
360  */
361 struct drm_client_buffer *
362 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
363 {
364 	struct drm_client_buffer *buffer;
365 	int ret;
366 
367 	buffer = drm_client_buffer_create(client, width, height, format);
368 	if (IS_ERR(buffer))
369 		return buffer;
370 
371 	ret = drm_client_buffer_addfb(buffer, width, height, format);
372 	if (ret) {
373 		drm_client_buffer_delete(buffer);
374 		return ERR_PTR(ret);
375 	}
376 
377 	return buffer;
378 }
379 EXPORT_SYMBOL(drm_client_framebuffer_create);
380 
381 /**
382  * drm_client_framebuffer_delete - Delete a client framebuffer
383  * @buffer: DRM client buffer (can be NULL)
384  */
385 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
386 {
387 	if (!buffer)
388 		return;
389 
390 	drm_client_buffer_rmfb(buffer);
391 	drm_client_buffer_delete(buffer);
392 }
393 EXPORT_SYMBOL(drm_client_framebuffer_delete);
394 
395 #ifdef CONFIG_DEBUG_FS
396 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
397 {
398 	struct drm_info_node *node = m->private;
399 	struct drm_device *dev = node->minor->dev;
400 	struct drm_printer p = drm_seq_file_printer(m);
401 	struct drm_client_dev *client;
402 
403 	mutex_lock(&dev->clientlist_mutex);
404 	list_for_each_entry(client, &dev->clientlist, list)
405 		drm_printf(&p, "%s\n", client->name);
406 	mutex_unlock(&dev->clientlist_mutex);
407 
408 	return 0;
409 }
410 
411 static const struct drm_info_list drm_client_debugfs_list[] = {
412 	{ "internal_clients", drm_client_debugfs_internal_clients, 0 },
413 };
414 
415 int drm_client_debugfs_init(struct drm_minor *minor)
416 {
417 	return drm_debugfs_create_files(drm_client_debugfs_list,
418 					ARRAY_SIZE(drm_client_debugfs_list),
419 					minor->debugfs_root, minor);
420 }
421 #endif
422