xref: /linux/drivers/gpu/drm/drm_client.c (revision b39b5394fabc79acbaafb26b777fd348c868bf7e)
1c76f0f7cSNoralf Trønnes // SPDX-License-Identifier: GPL-2.0
2c76f0f7cSNoralf Trønnes /*
3c76f0f7cSNoralf Trønnes  * Copyright 2018 Noralf Trønnes
4c76f0f7cSNoralf Trønnes  */
5c76f0f7cSNoralf Trønnes 
6c76f0f7cSNoralf Trønnes #include <linux/list.h>
7c76f0f7cSNoralf Trønnes #include <linux/module.h>
8c76f0f7cSNoralf Trønnes #include <linux/mutex.h>
9c76f0f7cSNoralf Trønnes #include <linux/seq_file.h>
10c76f0f7cSNoralf Trønnes #include <linux/slab.h>
11c76f0f7cSNoralf Trønnes 
12c76f0f7cSNoralf Trønnes #include <drm/drm_client.h>
13c76f0f7cSNoralf Trønnes #include <drm/drm_debugfs.h>
14c76f0f7cSNoralf Trønnes #include <drm/drm_device.h>
15c76f0f7cSNoralf Trønnes #include <drm/drm_drv.h>
16c76f0f7cSNoralf Trønnes #include <drm/drm_file.h>
17c76f0f7cSNoralf Trønnes #include <drm/drm_fourcc.h>
18c76f0f7cSNoralf Trønnes #include <drm/drm_gem.h>
19c76f0f7cSNoralf Trønnes #include <drm/drm_mode.h>
20c76f0f7cSNoralf Trønnes #include <drm/drm_print.h>
21c76f0f7cSNoralf Trønnes #include <drm/drmP.h>
22c76f0f7cSNoralf Trønnes 
23c76f0f7cSNoralf Trønnes #include "drm_crtc_internal.h"
24c76f0f7cSNoralf Trønnes #include "drm_internal.h"
25c76f0f7cSNoralf Trønnes 
26c76f0f7cSNoralf Trønnes /**
27c76f0f7cSNoralf Trønnes  * DOC: overview
28c76f0f7cSNoralf Trønnes  *
29c76f0f7cSNoralf Trønnes  * This library provides support for clients running in the kernel like fbdev and bootsplash.
30c76f0f7cSNoralf Trønnes  * Currently it's only partially implemented, just enough to support fbdev.
31c76f0f7cSNoralf Trønnes  *
32c76f0f7cSNoralf Trønnes  * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
33c76f0f7cSNoralf Trønnes  */
34c76f0f7cSNoralf Trønnes 
35c76f0f7cSNoralf Trønnes static int drm_client_open(struct drm_client_dev *client)
36c76f0f7cSNoralf Trønnes {
37c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
38c76f0f7cSNoralf Trønnes 	struct drm_file *file;
39c76f0f7cSNoralf Trønnes 
40c76f0f7cSNoralf Trønnes 	file = drm_file_alloc(dev->primary);
41c76f0f7cSNoralf Trønnes 	if (IS_ERR(file))
42c76f0f7cSNoralf Trønnes 		return PTR_ERR(file);
43c76f0f7cSNoralf Trønnes 
44c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->filelist_mutex);
45c76f0f7cSNoralf Trønnes 	list_add(&file->lhead, &dev->filelist_internal);
46c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->filelist_mutex);
47c76f0f7cSNoralf Trønnes 
48c76f0f7cSNoralf Trønnes 	client->file = file;
49c76f0f7cSNoralf Trønnes 
50c76f0f7cSNoralf Trønnes 	return 0;
51c76f0f7cSNoralf Trønnes }
52c76f0f7cSNoralf Trønnes 
53c76f0f7cSNoralf Trønnes static void drm_client_close(struct drm_client_dev *client)
54c76f0f7cSNoralf Trønnes {
55c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
56c76f0f7cSNoralf Trønnes 
57c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->filelist_mutex);
58c76f0f7cSNoralf Trønnes 	list_del(&client->file->lhead);
59c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->filelist_mutex);
60c76f0f7cSNoralf Trønnes 
61c76f0f7cSNoralf Trønnes 	drm_file_free(client->file);
62c76f0f7cSNoralf Trønnes }
63c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_close);
64c76f0f7cSNoralf Trønnes 
65c76f0f7cSNoralf Trønnes /**
664d4c2d89SNoralf Trønnes  * drm_client_init - Initialise a DRM client
67c76f0f7cSNoralf Trønnes  * @dev: DRM device
68c76f0f7cSNoralf Trønnes  * @client: DRM client
69c76f0f7cSNoralf Trønnes  * @name: Client name
70c76f0f7cSNoralf Trønnes  * @funcs: DRM client functions (optional)
71c76f0f7cSNoralf Trønnes  *
724d4c2d89SNoralf Trønnes  * This initialises the client and opens a &drm_file. Use drm_client_add() to complete the process.
73c76f0f7cSNoralf Trønnes  * The caller needs to hold a reference on @dev before calling this function.
74c76f0f7cSNoralf Trønnes  * The client is freed when the &drm_device is unregistered. See drm_client_release().
75c76f0f7cSNoralf Trønnes  *
76c76f0f7cSNoralf Trønnes  * Returns:
77c76f0f7cSNoralf Trønnes  * Zero on success or negative error code on failure.
78c76f0f7cSNoralf Trønnes  */
794d4c2d89SNoralf Trønnes int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
80c76f0f7cSNoralf Trønnes 		    const char *name, const struct drm_client_funcs *funcs)
81c76f0f7cSNoralf Trønnes {
82c76f0f7cSNoralf Trønnes 	int ret;
83c76f0f7cSNoralf Trønnes 
84*b39b5394SNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
8569fdf420SChris Wilson 		return -EOPNOTSUPP;
86c76f0f7cSNoralf Trønnes 
87c76f0f7cSNoralf Trønnes 	if (funcs && !try_module_get(funcs->owner))
88c76f0f7cSNoralf Trønnes 		return -ENODEV;
89c76f0f7cSNoralf Trønnes 
90c76f0f7cSNoralf Trønnes 	client->dev = dev;
91c76f0f7cSNoralf Trønnes 	client->name = name;
92c76f0f7cSNoralf Trønnes 	client->funcs = funcs;
93c76f0f7cSNoralf Trønnes 
94c76f0f7cSNoralf Trønnes 	ret = drm_client_open(client);
95c76f0f7cSNoralf Trønnes 	if (ret)
96c76f0f7cSNoralf Trønnes 		goto err_put_module;
97c76f0f7cSNoralf Trønnes 
98c76f0f7cSNoralf Trønnes 	drm_dev_get(dev);
99c76f0f7cSNoralf Trønnes 
100c76f0f7cSNoralf Trønnes 	return 0;
101c76f0f7cSNoralf Trønnes 
102c76f0f7cSNoralf Trønnes err_put_module:
103c76f0f7cSNoralf Trønnes 	if (funcs)
104c76f0f7cSNoralf Trønnes 		module_put(funcs->owner);
105c76f0f7cSNoralf Trønnes 
106c76f0f7cSNoralf Trønnes 	return ret;
107c76f0f7cSNoralf Trønnes }
1084d4c2d89SNoralf Trønnes EXPORT_SYMBOL(drm_client_init);
1094d4c2d89SNoralf Trønnes 
1104d4c2d89SNoralf Trønnes /**
1114d4c2d89SNoralf Trønnes  * drm_client_add - Add client to the device list
1124d4c2d89SNoralf Trønnes  * @client: DRM client
1134d4c2d89SNoralf Trønnes  *
1144d4c2d89SNoralf Trønnes  * Add the client to the &drm_device client list to activate its callbacks.
1154d4c2d89SNoralf Trønnes  * @client must be initialized by a call to drm_client_init(). After
1164d4c2d89SNoralf Trønnes  * drm_client_add() it is no longer permissible to call drm_client_release()
1174d4c2d89SNoralf Trønnes  * directly (outside the unregister callback), instead cleanup will happen
1184d4c2d89SNoralf Trønnes  * automatically on driver unload.
1194d4c2d89SNoralf Trønnes  */
1204d4c2d89SNoralf Trønnes void drm_client_add(struct drm_client_dev *client)
1214d4c2d89SNoralf Trønnes {
1224d4c2d89SNoralf Trønnes 	struct drm_device *dev = client->dev;
1234d4c2d89SNoralf Trønnes 
1244d4c2d89SNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
1254d4c2d89SNoralf Trønnes 	list_add(&client->list, &dev->clientlist);
1264d4c2d89SNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
1274d4c2d89SNoralf Trønnes }
1284d4c2d89SNoralf Trønnes EXPORT_SYMBOL(drm_client_add);
129c76f0f7cSNoralf Trønnes 
130c76f0f7cSNoralf Trønnes /**
131c76f0f7cSNoralf Trønnes  * drm_client_release - Release DRM client resources
132c76f0f7cSNoralf Trønnes  * @client: DRM client
133c76f0f7cSNoralf Trønnes  *
1344d4c2d89SNoralf Trønnes  * Releases resources by closing the &drm_file that was opened by drm_client_init().
135c76f0f7cSNoralf Trønnes  * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
136c76f0f7cSNoralf Trønnes  *
137c76f0f7cSNoralf Trønnes  * This function should only be called from the unregister callback. An exception
138c76f0f7cSNoralf Trønnes  * is fbdev which cannot free the buffer if userspace has open file descriptors.
139c76f0f7cSNoralf Trønnes  *
140c76f0f7cSNoralf Trønnes  * Note:
141c76f0f7cSNoralf Trønnes  * Clients cannot initiate a release by themselves. This is done to keep the code simple.
142c76f0f7cSNoralf Trønnes  * The driver has to be unloaded before the client can be unloaded.
143c76f0f7cSNoralf Trønnes  */
144c76f0f7cSNoralf Trønnes void drm_client_release(struct drm_client_dev *client)
145c76f0f7cSNoralf Trønnes {
146c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
147c76f0f7cSNoralf Trønnes 
148c76f0f7cSNoralf Trønnes 	DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name);
149c76f0f7cSNoralf Trønnes 
150c76f0f7cSNoralf Trønnes 	drm_client_close(client);
151c76f0f7cSNoralf Trønnes 	drm_dev_put(dev);
152c76f0f7cSNoralf Trønnes 	if (client->funcs)
153c76f0f7cSNoralf Trønnes 		module_put(client->funcs->owner);
154c76f0f7cSNoralf Trønnes }
155c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_release);
156c76f0f7cSNoralf Trønnes 
157c76f0f7cSNoralf Trønnes void drm_client_dev_unregister(struct drm_device *dev)
158c76f0f7cSNoralf Trønnes {
159c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client, *tmp;
160c76f0f7cSNoralf Trønnes 
161c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
162c76f0f7cSNoralf Trønnes 		return;
163c76f0f7cSNoralf Trønnes 
164c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
165c76f0f7cSNoralf Trønnes 	list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
166c76f0f7cSNoralf Trønnes 		list_del(&client->list);
167c76f0f7cSNoralf Trønnes 		if (client->funcs && client->funcs->unregister) {
168c76f0f7cSNoralf Trønnes 			client->funcs->unregister(client);
169c76f0f7cSNoralf Trønnes 		} else {
170c76f0f7cSNoralf Trønnes 			drm_client_release(client);
171c76f0f7cSNoralf Trønnes 			kfree(client);
172c76f0f7cSNoralf Trønnes 		}
173c76f0f7cSNoralf Trønnes 	}
174c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
175c76f0f7cSNoralf Trønnes }
176c76f0f7cSNoralf Trønnes 
177c76f0f7cSNoralf Trønnes /**
178c76f0f7cSNoralf Trønnes  * drm_client_dev_hotplug - Send hotplug event to clients
179c76f0f7cSNoralf Trønnes  * @dev: DRM device
180c76f0f7cSNoralf Trønnes  *
181c76f0f7cSNoralf Trønnes  * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
182c76f0f7cSNoralf Trønnes  *
183c76f0f7cSNoralf Trønnes  * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
184c76f0f7cSNoralf Trønnes  * don't need to call this function themselves.
185c76f0f7cSNoralf Trønnes  */
186c76f0f7cSNoralf Trønnes void drm_client_dev_hotplug(struct drm_device *dev)
187c76f0f7cSNoralf Trønnes {
188c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client;
189c76f0f7cSNoralf Trønnes 	int ret;
190c76f0f7cSNoralf Trønnes 
191c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
192c76f0f7cSNoralf Trønnes 		return;
193c76f0f7cSNoralf Trønnes 
194c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
195c76f0f7cSNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list) {
196c76f0f7cSNoralf Trønnes 		if (!client->funcs || !client->funcs->hotplug)
197c76f0f7cSNoralf Trønnes 			continue;
198c76f0f7cSNoralf Trønnes 
199c76f0f7cSNoralf Trønnes 		ret = client->funcs->hotplug(client);
200c76f0f7cSNoralf Trønnes 		DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
201c76f0f7cSNoralf Trønnes 	}
202c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
203c76f0f7cSNoralf Trønnes }
204c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_dev_hotplug);
205c76f0f7cSNoralf Trønnes 
206c76f0f7cSNoralf Trønnes void drm_client_dev_restore(struct drm_device *dev)
207c76f0f7cSNoralf Trønnes {
208c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client;
209c76f0f7cSNoralf Trønnes 	int ret;
210c76f0f7cSNoralf Trønnes 
211c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
212c76f0f7cSNoralf Trønnes 		return;
213c76f0f7cSNoralf Trønnes 
214c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
215c76f0f7cSNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list) {
216c76f0f7cSNoralf Trønnes 		if (!client->funcs || !client->funcs->restore)
217c76f0f7cSNoralf Trønnes 			continue;
218c76f0f7cSNoralf Trønnes 
219c76f0f7cSNoralf Trønnes 		ret = client->funcs->restore(client);
220c76f0f7cSNoralf Trønnes 		DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
221c76f0f7cSNoralf Trønnes 		if (!ret) /* The first one to return zero gets the privilege to restore */
222c76f0f7cSNoralf Trønnes 			break;
223c76f0f7cSNoralf Trønnes 	}
224c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
225c76f0f7cSNoralf Trønnes }
226c76f0f7cSNoralf Trønnes 
227c76f0f7cSNoralf Trønnes static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
228c76f0f7cSNoralf Trønnes {
229c76f0f7cSNoralf Trønnes 	struct drm_device *dev = buffer->client->dev;
230c76f0f7cSNoralf Trønnes 
231*b39b5394SNoralf Trønnes 	drm_gem_vunmap(buffer->gem, buffer->vaddr);
232c76f0f7cSNoralf Trønnes 
233c76f0f7cSNoralf Trønnes 	if (buffer->gem)
234c76f0f7cSNoralf Trønnes 		drm_gem_object_put_unlocked(buffer->gem);
235c76f0f7cSNoralf Trønnes 
236cf19fa2cSNoralf Trønnes 	if (buffer->handle)
237c76f0f7cSNoralf Trønnes 		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
238cf19fa2cSNoralf Trønnes 
239c76f0f7cSNoralf Trønnes 	kfree(buffer);
240c76f0f7cSNoralf Trønnes }
241c76f0f7cSNoralf Trønnes 
242c76f0f7cSNoralf Trønnes static struct drm_client_buffer *
243c76f0f7cSNoralf Trønnes drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
244c76f0f7cSNoralf Trønnes {
245c76f0f7cSNoralf Trønnes 	struct drm_mode_create_dumb dumb_args = { };
246c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
247c76f0f7cSNoralf Trønnes 	struct drm_client_buffer *buffer;
248c76f0f7cSNoralf Trønnes 	struct drm_gem_object *obj;
249c76f0f7cSNoralf Trønnes 	void *vaddr;
250c76f0f7cSNoralf Trønnes 	int ret;
251c76f0f7cSNoralf Trønnes 
252c76f0f7cSNoralf Trønnes 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
253c76f0f7cSNoralf Trønnes 	if (!buffer)
254c76f0f7cSNoralf Trønnes 		return ERR_PTR(-ENOMEM);
255c76f0f7cSNoralf Trønnes 
256c76f0f7cSNoralf Trønnes 	buffer->client = client;
257c76f0f7cSNoralf Trønnes 
258c76f0f7cSNoralf Trønnes 	dumb_args.width = width;
259c76f0f7cSNoralf Trønnes 	dumb_args.height = height;
260c76f0f7cSNoralf Trønnes 	dumb_args.bpp = drm_format_plane_cpp(format, 0) * 8;
261c76f0f7cSNoralf Trønnes 	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
262c76f0f7cSNoralf Trønnes 	if (ret)
263cf19fa2cSNoralf Trønnes 		goto err_delete;
264c76f0f7cSNoralf Trønnes 
265c76f0f7cSNoralf Trønnes 	buffer->handle = dumb_args.handle;
266c76f0f7cSNoralf Trønnes 	buffer->pitch = dumb_args.pitch;
267c76f0f7cSNoralf Trønnes 
268c76f0f7cSNoralf Trønnes 	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
269c76f0f7cSNoralf Trønnes 	if (!obj)  {
270c76f0f7cSNoralf Trønnes 		ret = -ENOENT;
271c76f0f7cSNoralf Trønnes 		goto err_delete;
272c76f0f7cSNoralf Trønnes 	}
273c76f0f7cSNoralf Trønnes 
274c76f0f7cSNoralf Trønnes 	buffer->gem = obj;
275c76f0f7cSNoralf Trønnes 
276c76f0f7cSNoralf Trønnes 	/*
277c76f0f7cSNoralf Trønnes 	 * FIXME: The dependency on GEM here isn't required, we could
278c76f0f7cSNoralf Trønnes 	 * convert the driver handle to a dma-buf instead and use the
279c76f0f7cSNoralf Trønnes 	 * backend-agnostic dma-buf vmap support instead. This would
280c76f0f7cSNoralf Trønnes 	 * require that the handle2fd prime ioctl is reworked to pull the
281c76f0f7cSNoralf Trønnes 	 * fd_install step out of the driver backend hooks, to make that
282c76f0f7cSNoralf Trønnes 	 * final step optional for internal users.
283c76f0f7cSNoralf Trønnes 	 */
284*b39b5394SNoralf Trønnes 	vaddr = drm_gem_vmap(obj);
285*b39b5394SNoralf Trønnes 	if (IS_ERR(vaddr)) {
286*b39b5394SNoralf Trønnes 		ret = PTR_ERR(vaddr);
287c76f0f7cSNoralf Trønnes 		goto err_delete;
288c76f0f7cSNoralf Trønnes 	}
289c76f0f7cSNoralf Trønnes 
290c76f0f7cSNoralf Trønnes 	buffer->vaddr = vaddr;
291c76f0f7cSNoralf Trønnes 
292c76f0f7cSNoralf Trønnes 	return buffer;
293c76f0f7cSNoralf Trønnes 
294c76f0f7cSNoralf Trønnes err_delete:
295c76f0f7cSNoralf Trønnes 	drm_client_buffer_delete(buffer);
296c76f0f7cSNoralf Trønnes 
297c76f0f7cSNoralf Trønnes 	return ERR_PTR(ret);
298c76f0f7cSNoralf Trønnes }
299c76f0f7cSNoralf Trønnes 
300c76f0f7cSNoralf Trønnes static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
301c76f0f7cSNoralf Trønnes {
302c76f0f7cSNoralf Trønnes 	int ret;
303c76f0f7cSNoralf Trønnes 
304c76f0f7cSNoralf Trønnes 	if (!buffer->fb)
305c76f0f7cSNoralf Trønnes 		return;
306c76f0f7cSNoralf Trønnes 
307c76f0f7cSNoralf Trønnes 	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
308c76f0f7cSNoralf Trønnes 	if (ret)
309c76f0f7cSNoralf Trønnes 		DRM_DEV_ERROR(buffer->client->dev->dev,
310c76f0f7cSNoralf Trønnes 			      "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
311c76f0f7cSNoralf Trønnes 
312c76f0f7cSNoralf Trønnes 	buffer->fb = NULL;
313c76f0f7cSNoralf Trønnes }
314c76f0f7cSNoralf Trønnes 
315c76f0f7cSNoralf Trønnes static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
316c76f0f7cSNoralf Trønnes 				   u32 width, u32 height, u32 format)
317c76f0f7cSNoralf Trønnes {
318c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client = buffer->client;
319c76f0f7cSNoralf Trønnes 	struct drm_mode_fb_cmd fb_req = { };
320c76f0f7cSNoralf Trønnes 	const struct drm_format_info *info;
321c76f0f7cSNoralf Trønnes 	int ret;
322c76f0f7cSNoralf Trønnes 
323c76f0f7cSNoralf Trønnes 	info = drm_format_info(format);
324c76f0f7cSNoralf Trønnes 	fb_req.bpp = info->cpp[0] * 8;
325c76f0f7cSNoralf Trønnes 	fb_req.depth = info->depth;
326c76f0f7cSNoralf Trønnes 	fb_req.width = width;
327c76f0f7cSNoralf Trønnes 	fb_req.height = height;
328c76f0f7cSNoralf Trønnes 	fb_req.handle = buffer->handle;
329c76f0f7cSNoralf Trønnes 	fb_req.pitch = buffer->pitch;
330c76f0f7cSNoralf Trønnes 
331c76f0f7cSNoralf Trønnes 	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
332c76f0f7cSNoralf Trønnes 	if (ret)
333c76f0f7cSNoralf Trønnes 		return ret;
334c76f0f7cSNoralf Trønnes 
335c76f0f7cSNoralf Trønnes 	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
336c76f0f7cSNoralf Trønnes 	if (WARN_ON(!buffer->fb))
337c76f0f7cSNoralf Trønnes 		return -ENOENT;
338c76f0f7cSNoralf Trønnes 
339c76f0f7cSNoralf Trønnes 	/* drop the reference we picked up in framebuffer lookup */
340c76f0f7cSNoralf Trønnes 	drm_framebuffer_put(buffer->fb);
341c76f0f7cSNoralf Trønnes 
342c76f0f7cSNoralf Trønnes 	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
343c76f0f7cSNoralf Trønnes 
344c76f0f7cSNoralf Trønnes 	return 0;
345c76f0f7cSNoralf Trønnes }
346c76f0f7cSNoralf Trønnes 
347c76f0f7cSNoralf Trønnes /**
348c76f0f7cSNoralf Trønnes  * drm_client_framebuffer_create - Create a client framebuffer
349c76f0f7cSNoralf Trønnes  * @client: DRM client
350c76f0f7cSNoralf Trønnes  * @width: Framebuffer width
351c76f0f7cSNoralf Trønnes  * @height: Framebuffer height
352c76f0f7cSNoralf Trønnes  * @format: Buffer format
353c76f0f7cSNoralf Trønnes  *
354c76f0f7cSNoralf Trønnes  * This function creates a &drm_client_buffer which consists of a
355c76f0f7cSNoralf Trønnes  * &drm_framebuffer backed by a dumb buffer.
356c76f0f7cSNoralf Trønnes  * Call drm_client_framebuffer_delete() to free the buffer.
357c76f0f7cSNoralf Trønnes  *
358c76f0f7cSNoralf Trønnes  * Returns:
359c76f0f7cSNoralf Trønnes  * Pointer to a client buffer or an error pointer on failure.
360c76f0f7cSNoralf Trønnes  */
361c76f0f7cSNoralf Trønnes struct drm_client_buffer *
362c76f0f7cSNoralf Trønnes drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
363c76f0f7cSNoralf Trønnes {
364c76f0f7cSNoralf Trønnes 	struct drm_client_buffer *buffer;
365c76f0f7cSNoralf Trønnes 	int ret;
366c76f0f7cSNoralf Trønnes 
367c76f0f7cSNoralf Trønnes 	buffer = drm_client_buffer_create(client, width, height, format);
368c76f0f7cSNoralf Trønnes 	if (IS_ERR(buffer))
369c76f0f7cSNoralf Trønnes 		return buffer;
370c76f0f7cSNoralf Trønnes 
371c76f0f7cSNoralf Trønnes 	ret = drm_client_buffer_addfb(buffer, width, height, format);
372c76f0f7cSNoralf Trønnes 	if (ret) {
373c76f0f7cSNoralf Trønnes 		drm_client_buffer_delete(buffer);
374c76f0f7cSNoralf Trønnes 		return ERR_PTR(ret);
375c76f0f7cSNoralf Trønnes 	}
376c76f0f7cSNoralf Trønnes 
377c76f0f7cSNoralf Trønnes 	return buffer;
378c76f0f7cSNoralf Trønnes }
379c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_create);
380c76f0f7cSNoralf Trønnes 
381c76f0f7cSNoralf Trønnes /**
382c76f0f7cSNoralf Trønnes  * drm_client_framebuffer_delete - Delete a client framebuffer
383c76f0f7cSNoralf Trønnes  * @buffer: DRM client buffer (can be NULL)
384c76f0f7cSNoralf Trønnes  */
385c76f0f7cSNoralf Trønnes void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
386c76f0f7cSNoralf Trønnes {
387c76f0f7cSNoralf Trønnes 	if (!buffer)
388c76f0f7cSNoralf Trønnes 		return;
389c76f0f7cSNoralf Trønnes 
390c76f0f7cSNoralf Trønnes 	drm_client_buffer_rmfb(buffer);
391c76f0f7cSNoralf Trønnes 	drm_client_buffer_delete(buffer);
392c76f0f7cSNoralf Trønnes }
393c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_delete);
394e896c132SNoralf Trønnes 
395e896c132SNoralf Trønnes #ifdef CONFIG_DEBUG_FS
396e896c132SNoralf Trønnes static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
397e896c132SNoralf Trønnes {
398e896c132SNoralf Trønnes 	struct drm_info_node *node = m->private;
399e896c132SNoralf Trønnes 	struct drm_device *dev = node->minor->dev;
400e896c132SNoralf Trønnes 	struct drm_printer p = drm_seq_file_printer(m);
401e896c132SNoralf Trønnes 	struct drm_client_dev *client;
402e896c132SNoralf Trønnes 
403e896c132SNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
404e896c132SNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list)
405e896c132SNoralf Trønnes 		drm_printf(&p, "%s\n", client->name);
406e896c132SNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
407e896c132SNoralf Trønnes 
408e896c132SNoralf Trønnes 	return 0;
409e896c132SNoralf Trønnes }
410e896c132SNoralf Trønnes 
411e896c132SNoralf Trønnes static const struct drm_info_list drm_client_debugfs_list[] = {
412e896c132SNoralf Trønnes 	{ "internal_clients", drm_client_debugfs_internal_clients, 0 },
413e896c132SNoralf Trønnes };
414e896c132SNoralf Trønnes 
415e896c132SNoralf Trønnes int drm_client_debugfs_init(struct drm_minor *minor)
416e896c132SNoralf Trønnes {
417e896c132SNoralf Trønnes 	return drm_debugfs_create_files(drm_client_debugfs_list,
418e896c132SNoralf Trønnes 					ARRAY_SIZE(drm_client_debugfs_list),
419e896c132SNoralf Trønnes 					minor->debugfs_root, minor);
420e896c132SNoralf Trønnes }
421e896c132SNoralf Trønnes #endif
422