xref: /linux/drivers/gpu/drm/drm_client.c (revision c9c03e3cf07299bf635e6fadad8d09106d26252f)
1df0e7f7eSEmmanuel Vadot // SPDX-License-Identifier: GPL-2.0 or MIT
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>
180500c04eSSam Ravnborg #include <drm/drm_framebuffer.h>
19c76f0f7cSNoralf Trønnes #include <drm/drm_gem.h>
20c76f0f7cSNoralf Trønnes #include <drm/drm_mode.h>
21c76f0f7cSNoralf Trønnes #include <drm/drm_print.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  *
31c76f0f7cSNoralf Trønnes  * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
32c76f0f7cSNoralf Trønnes  */
33c76f0f7cSNoralf Trønnes 
34c76f0f7cSNoralf Trønnes static int drm_client_open(struct drm_client_dev *client)
35c76f0f7cSNoralf Trønnes {
36c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
37c76f0f7cSNoralf Trønnes 	struct drm_file *file;
38c76f0f7cSNoralf Trønnes 
39c76f0f7cSNoralf Trønnes 	file = drm_file_alloc(dev->primary);
40c76f0f7cSNoralf Trønnes 	if (IS_ERR(file))
41c76f0f7cSNoralf Trønnes 		return PTR_ERR(file);
42c76f0f7cSNoralf Trønnes 
43c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->filelist_mutex);
44c76f0f7cSNoralf Trønnes 	list_add(&file->lhead, &dev->filelist_internal);
45c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->filelist_mutex);
46c76f0f7cSNoralf Trønnes 
47c76f0f7cSNoralf Trønnes 	client->file = file;
48c76f0f7cSNoralf Trønnes 
49c76f0f7cSNoralf Trønnes 	return 0;
50c76f0f7cSNoralf Trønnes }
51c76f0f7cSNoralf Trønnes 
52c76f0f7cSNoralf Trønnes static void drm_client_close(struct drm_client_dev *client)
53c76f0f7cSNoralf Trønnes {
54c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
55c76f0f7cSNoralf Trønnes 
56c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->filelist_mutex);
57c76f0f7cSNoralf Trønnes 	list_del(&client->file->lhead);
58c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->filelist_mutex);
59c76f0f7cSNoralf Trønnes 
60c76f0f7cSNoralf Trønnes 	drm_file_free(client->file);
61c76f0f7cSNoralf Trønnes }
62c76f0f7cSNoralf Trønnes 
63c76f0f7cSNoralf Trønnes /**
644d4c2d89SNoralf Trønnes  * drm_client_init - Initialise a DRM client
65c76f0f7cSNoralf Trønnes  * @dev: DRM device
66c76f0f7cSNoralf Trønnes  * @client: DRM client
67c76f0f7cSNoralf Trønnes  * @name: Client name
68c76f0f7cSNoralf Trønnes  * @funcs: DRM client functions (optional)
69c76f0f7cSNoralf Trønnes  *
70e33898a2SNoralf Trønnes  * This initialises the client and opens a &drm_file.
71e33898a2SNoralf Trønnes  * Use drm_client_register() to complete the process.
72c76f0f7cSNoralf Trønnes  * The caller needs to hold a reference on @dev before calling this function.
73c76f0f7cSNoralf Trønnes  * The client is freed when the &drm_device is unregistered. See drm_client_release().
74c76f0f7cSNoralf Trønnes  *
75c76f0f7cSNoralf Trønnes  * Returns:
76c76f0f7cSNoralf Trønnes  * Zero on success or negative error code on failure.
77c76f0f7cSNoralf Trønnes  */
784d4c2d89SNoralf Trønnes int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
79c76f0f7cSNoralf Trønnes 		    const char *name, const struct drm_client_funcs *funcs)
80c76f0f7cSNoralf Trønnes {
81c76f0f7cSNoralf Trønnes 	int ret;
82c76f0f7cSNoralf Trønnes 
83b39b5394SNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
8469fdf420SChris Wilson 		return -EOPNOTSUPP;
85c76f0f7cSNoralf Trønnes 
86c76f0f7cSNoralf Trønnes 	if (funcs && !try_module_get(funcs->owner))
87c76f0f7cSNoralf Trønnes 		return -ENODEV;
88c76f0f7cSNoralf Trønnes 
89c76f0f7cSNoralf Trønnes 	client->dev = dev;
90c76f0f7cSNoralf Trønnes 	client->name = name;
91c76f0f7cSNoralf Trønnes 	client->funcs = funcs;
92c76f0f7cSNoralf Trønnes 
93d81294afSNoralf Trønnes 	ret = drm_client_modeset_create(client);
94c76f0f7cSNoralf Trønnes 	if (ret)
95c76f0f7cSNoralf Trønnes 		goto err_put_module;
96c76f0f7cSNoralf Trønnes 
97d81294afSNoralf Trønnes 	ret = drm_client_open(client);
98d81294afSNoralf Trønnes 	if (ret)
99d81294afSNoralf Trønnes 		goto err_free;
100d81294afSNoralf Trønnes 
101c76f0f7cSNoralf Trønnes 	drm_dev_get(dev);
102c76f0f7cSNoralf Trønnes 
103c76f0f7cSNoralf Trønnes 	return 0;
104c76f0f7cSNoralf Trønnes 
105d81294afSNoralf Trønnes err_free:
106d81294afSNoralf Trønnes 	drm_client_modeset_free(client);
107c76f0f7cSNoralf Trønnes err_put_module:
108c76f0f7cSNoralf Trønnes 	if (funcs)
109c76f0f7cSNoralf Trønnes 		module_put(funcs->owner);
110c76f0f7cSNoralf Trønnes 
111c76f0f7cSNoralf Trønnes 	return ret;
112c76f0f7cSNoralf Trønnes }
1134d4c2d89SNoralf Trønnes EXPORT_SYMBOL(drm_client_init);
1144d4c2d89SNoralf Trønnes 
1154d4c2d89SNoralf Trønnes /**
116e33898a2SNoralf Trønnes  * drm_client_register - Register client
1174d4c2d89SNoralf Trønnes  * @client: DRM client
1184d4c2d89SNoralf Trønnes  *
1194d4c2d89SNoralf Trønnes  * Add the client to the &drm_device client list to activate its callbacks.
1204d4c2d89SNoralf Trønnes  * @client must be initialized by a call to drm_client_init(). After
121e33898a2SNoralf Trønnes  * drm_client_register() it is no longer permissible to call drm_client_release()
1224d4c2d89SNoralf Trønnes  * directly (outside the unregister callback), instead cleanup will happen
1234d4c2d89SNoralf Trønnes  * automatically on driver unload.
1244d4c2d89SNoralf Trønnes  */
125e33898a2SNoralf Trønnes void drm_client_register(struct drm_client_dev *client)
1264d4c2d89SNoralf Trønnes {
1274d4c2d89SNoralf Trønnes 	struct drm_device *dev = client->dev;
1284d4c2d89SNoralf Trønnes 
1294d4c2d89SNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
1304d4c2d89SNoralf Trønnes 	list_add(&client->list, &dev->clientlist);
1314d4c2d89SNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
1324d4c2d89SNoralf Trønnes }
133e33898a2SNoralf Trønnes EXPORT_SYMBOL(drm_client_register);
134c76f0f7cSNoralf Trønnes 
135c76f0f7cSNoralf Trønnes /**
136c76f0f7cSNoralf Trønnes  * drm_client_release - Release DRM client resources
137c76f0f7cSNoralf Trønnes  * @client: DRM client
138c76f0f7cSNoralf Trønnes  *
1394d4c2d89SNoralf Trønnes  * Releases resources by closing the &drm_file that was opened by drm_client_init().
140c76f0f7cSNoralf Trønnes  * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
141c76f0f7cSNoralf Trønnes  *
142c76f0f7cSNoralf Trønnes  * This function should only be called from the unregister callback. An exception
143c76f0f7cSNoralf Trønnes  * is fbdev which cannot free the buffer if userspace has open file descriptors.
144c76f0f7cSNoralf Trønnes  *
145c76f0f7cSNoralf Trønnes  * Note:
146c76f0f7cSNoralf Trønnes  * Clients cannot initiate a release by themselves. This is done to keep the code simple.
147c76f0f7cSNoralf Trønnes  * The driver has to be unloaded before the client can be unloaded.
148c76f0f7cSNoralf Trønnes  */
149c76f0f7cSNoralf Trønnes void drm_client_release(struct drm_client_dev *client)
150c76f0f7cSNoralf Trønnes {
151c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
152c76f0f7cSNoralf Trønnes 
15341cb6603SJani Nikula 	drm_dbg_kms(dev, "%s\n", client->name);
154c76f0f7cSNoralf Trønnes 
155d81294afSNoralf Trønnes 	drm_client_modeset_free(client);
156c76f0f7cSNoralf Trønnes 	drm_client_close(client);
157c76f0f7cSNoralf Trønnes 	drm_dev_put(dev);
158c76f0f7cSNoralf Trønnes 	if (client->funcs)
159c76f0f7cSNoralf Trønnes 		module_put(client->funcs->owner);
160c76f0f7cSNoralf Trønnes }
161c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_release);
162c76f0f7cSNoralf Trønnes 
163c76f0f7cSNoralf Trønnes void drm_client_dev_unregister(struct drm_device *dev)
164c76f0f7cSNoralf Trønnes {
165c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client, *tmp;
166c76f0f7cSNoralf Trønnes 
167c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
168c76f0f7cSNoralf Trønnes 		return;
169c76f0f7cSNoralf Trønnes 
170c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
171c76f0f7cSNoralf Trønnes 	list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
172c76f0f7cSNoralf Trønnes 		list_del(&client->list);
173c76f0f7cSNoralf Trønnes 		if (client->funcs && client->funcs->unregister) {
174c76f0f7cSNoralf Trønnes 			client->funcs->unregister(client);
175c76f0f7cSNoralf Trønnes 		} else {
176c76f0f7cSNoralf Trønnes 			drm_client_release(client);
177c76f0f7cSNoralf Trønnes 			kfree(client);
178c76f0f7cSNoralf Trønnes 		}
179c76f0f7cSNoralf Trønnes 	}
180c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
181c76f0f7cSNoralf Trønnes }
182c76f0f7cSNoralf Trønnes 
183c76f0f7cSNoralf Trønnes /**
184c76f0f7cSNoralf Trønnes  * drm_client_dev_hotplug - Send hotplug event to clients
185c76f0f7cSNoralf Trønnes  * @dev: DRM device
186c76f0f7cSNoralf Trønnes  *
187c76f0f7cSNoralf Trønnes  * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
188c76f0f7cSNoralf Trønnes  *
189c76f0f7cSNoralf Trønnes  * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
190c76f0f7cSNoralf Trønnes  * don't need to call this function themselves.
191c76f0f7cSNoralf Trønnes  */
192c76f0f7cSNoralf Trønnes void drm_client_dev_hotplug(struct drm_device *dev)
193c76f0f7cSNoralf Trønnes {
194c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client;
195c76f0f7cSNoralf Trønnes 	int ret;
196c76f0f7cSNoralf Trønnes 
197c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
198c76f0f7cSNoralf Trønnes 		return;
199c76f0f7cSNoralf Trønnes 
200c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
201c76f0f7cSNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list) {
202c76f0f7cSNoralf Trønnes 		if (!client->funcs || !client->funcs->hotplug)
203c76f0f7cSNoralf Trønnes 			continue;
204c76f0f7cSNoralf Trønnes 
205c76f0f7cSNoralf Trønnes 		ret = client->funcs->hotplug(client);
20641cb6603SJani Nikula 		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
207c76f0f7cSNoralf Trønnes 	}
208c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
209c76f0f7cSNoralf Trønnes }
210c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_dev_hotplug);
211c76f0f7cSNoralf Trønnes 
212c76f0f7cSNoralf Trønnes void drm_client_dev_restore(struct drm_device *dev)
213c76f0f7cSNoralf Trønnes {
214c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client;
215c76f0f7cSNoralf Trønnes 	int ret;
216c76f0f7cSNoralf Trønnes 
217c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
218c76f0f7cSNoralf Trønnes 		return;
219c76f0f7cSNoralf Trønnes 
220c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
221c76f0f7cSNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list) {
222c76f0f7cSNoralf Trønnes 		if (!client->funcs || !client->funcs->restore)
223c76f0f7cSNoralf Trønnes 			continue;
224c76f0f7cSNoralf Trønnes 
225c76f0f7cSNoralf Trønnes 		ret = client->funcs->restore(client);
22641cb6603SJani Nikula 		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
227c76f0f7cSNoralf Trønnes 		if (!ret) /* The first one to return zero gets the privilege to restore */
228c76f0f7cSNoralf Trønnes 			break;
229c76f0f7cSNoralf Trønnes 	}
230c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
231c76f0f7cSNoralf Trønnes }
232c76f0f7cSNoralf Trønnes 
233c76f0f7cSNoralf Trønnes static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
234c76f0f7cSNoralf Trønnes {
235c76f0f7cSNoralf Trønnes 	struct drm_device *dev = buffer->client->dev;
236c76f0f7cSNoralf Trønnes 
237b39b5394SNoralf Trønnes 	drm_gem_vunmap(buffer->gem, buffer->vaddr);
238c76f0f7cSNoralf Trønnes 
239c76f0f7cSNoralf Trønnes 	if (buffer->gem)
240be6ee102SEmil Velikov 		drm_gem_object_put(buffer->gem);
241c76f0f7cSNoralf Trønnes 
242cf19fa2cSNoralf Trønnes 	if (buffer->handle)
243c76f0f7cSNoralf Trønnes 		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
244cf19fa2cSNoralf Trønnes 
245c76f0f7cSNoralf Trønnes 	kfree(buffer);
246c76f0f7cSNoralf Trønnes }
247c76f0f7cSNoralf Trønnes 
248c76f0f7cSNoralf Trønnes static struct drm_client_buffer *
249c76f0f7cSNoralf Trønnes drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
250c76f0f7cSNoralf Trønnes {
25124c478eaSMaxime Ripard 	const struct drm_format_info *info = drm_format_info(format);
252c76f0f7cSNoralf Trønnes 	struct drm_mode_create_dumb dumb_args = { };
253c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
254c76f0f7cSNoralf Trønnes 	struct drm_client_buffer *buffer;
255c76f0f7cSNoralf Trønnes 	struct drm_gem_object *obj;
256c76f0f7cSNoralf Trønnes 	int ret;
257c76f0f7cSNoralf Trønnes 
258c76f0f7cSNoralf Trønnes 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
259c76f0f7cSNoralf Trønnes 	if (!buffer)
260c76f0f7cSNoralf Trønnes 		return ERR_PTR(-ENOMEM);
261c76f0f7cSNoralf Trønnes 
262c76f0f7cSNoralf Trønnes 	buffer->client = client;
263c76f0f7cSNoralf Trønnes 
264c76f0f7cSNoralf Trønnes 	dumb_args.width = width;
265c76f0f7cSNoralf Trønnes 	dumb_args.height = height;
266b0f986b4SMaxime Ripard 	dumb_args.bpp = info->cpp[0] * 8;
267c76f0f7cSNoralf Trønnes 	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
268c76f0f7cSNoralf Trønnes 	if (ret)
269cf19fa2cSNoralf Trønnes 		goto err_delete;
270c76f0f7cSNoralf Trønnes 
271c76f0f7cSNoralf Trønnes 	buffer->handle = dumb_args.handle;
272c76f0f7cSNoralf Trønnes 	buffer->pitch = dumb_args.pitch;
273c76f0f7cSNoralf Trønnes 
274c76f0f7cSNoralf Trønnes 	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
275c76f0f7cSNoralf Trønnes 	if (!obj)  {
276c76f0f7cSNoralf Trønnes 		ret = -ENOENT;
277c76f0f7cSNoralf Trønnes 		goto err_delete;
278c76f0f7cSNoralf Trønnes 	}
279c76f0f7cSNoralf Trønnes 
280c76f0f7cSNoralf Trønnes 	buffer->gem = obj;
281c76f0f7cSNoralf Trønnes 
282c76f0f7cSNoralf Trønnes 	return buffer;
283c76f0f7cSNoralf Trønnes 
284c76f0f7cSNoralf Trønnes err_delete:
285c76f0f7cSNoralf Trønnes 	drm_client_buffer_delete(buffer);
286c76f0f7cSNoralf Trønnes 
287c76f0f7cSNoralf Trønnes 	return ERR_PTR(ret);
288c76f0f7cSNoralf Trønnes }
289c76f0f7cSNoralf Trønnes 
29015dd0fc8SThomas Zimmermann /**
29115dd0fc8SThomas Zimmermann  * drm_client_buffer_vmap - Map DRM client buffer into address space
29215dd0fc8SThomas Zimmermann  * @buffer: DRM client buffer
29315dd0fc8SThomas Zimmermann  *
29415dd0fc8SThomas Zimmermann  * This function maps a client buffer into kernel address space. If the
29515dd0fc8SThomas Zimmermann  * buffer is already mapped, it returns the mapping's address.
29615dd0fc8SThomas Zimmermann  *
29715dd0fc8SThomas Zimmermann  * Client buffer mappings are not ref'counted. Each call to
29815dd0fc8SThomas Zimmermann  * drm_client_buffer_vmap() should be followed by a call to
29915dd0fc8SThomas Zimmermann  * drm_client_buffer_vunmap(); or the client buffer should be mapped
300cf1ca9aeSThomas Zimmermann  * throughout its lifetime.
30115dd0fc8SThomas Zimmermann  *
30215dd0fc8SThomas Zimmermann  * Returns:
30315dd0fc8SThomas Zimmermann  *	The mapped memory's address
30415dd0fc8SThomas Zimmermann  */
30515dd0fc8SThomas Zimmermann void *drm_client_buffer_vmap(struct drm_client_buffer *buffer)
30615dd0fc8SThomas Zimmermann {
30715dd0fc8SThomas Zimmermann 	void *vaddr;
30815dd0fc8SThomas Zimmermann 
30915dd0fc8SThomas Zimmermann 	if (buffer->vaddr)
31015dd0fc8SThomas Zimmermann 		return buffer->vaddr;
31115dd0fc8SThomas Zimmermann 
31215dd0fc8SThomas Zimmermann 	/*
31315dd0fc8SThomas Zimmermann 	 * FIXME: The dependency on GEM here isn't required, we could
31415dd0fc8SThomas Zimmermann 	 * convert the driver handle to a dma-buf instead and use the
31515dd0fc8SThomas Zimmermann 	 * backend-agnostic dma-buf vmap support instead. This would
31615dd0fc8SThomas Zimmermann 	 * require that the handle2fd prime ioctl is reworked to pull the
31715dd0fc8SThomas Zimmermann 	 * fd_install step out of the driver backend hooks, to make that
31815dd0fc8SThomas Zimmermann 	 * final step optional for internal users.
31915dd0fc8SThomas Zimmermann 	 */
32015dd0fc8SThomas Zimmermann 	vaddr = drm_gem_vmap(buffer->gem);
32115dd0fc8SThomas Zimmermann 	if (IS_ERR(vaddr))
32215dd0fc8SThomas Zimmermann 		return vaddr;
32315dd0fc8SThomas Zimmermann 
32415dd0fc8SThomas Zimmermann 	buffer->vaddr = vaddr;
32515dd0fc8SThomas Zimmermann 
32615dd0fc8SThomas Zimmermann 	return vaddr;
32715dd0fc8SThomas Zimmermann }
32815dd0fc8SThomas Zimmermann EXPORT_SYMBOL(drm_client_buffer_vmap);
32915dd0fc8SThomas Zimmermann 
33015dd0fc8SThomas Zimmermann /**
33115dd0fc8SThomas Zimmermann  * drm_client_buffer_vunmap - Unmap DRM client buffer
33215dd0fc8SThomas Zimmermann  * @buffer: DRM client buffer
33315dd0fc8SThomas Zimmermann  *
334cf1ca9aeSThomas Zimmermann  * This function removes a client buffer's memory mapping. Calling this
335cf1ca9aeSThomas Zimmermann  * function is only required by clients that manage their buffer mappings
336cf1ca9aeSThomas Zimmermann  * by themselves.
33715dd0fc8SThomas Zimmermann  */
33815dd0fc8SThomas Zimmermann void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
33915dd0fc8SThomas Zimmermann {
34015dd0fc8SThomas Zimmermann 	drm_gem_vunmap(buffer->gem, buffer->vaddr);
34115dd0fc8SThomas Zimmermann 	buffer->vaddr = NULL;
34215dd0fc8SThomas Zimmermann }
34315dd0fc8SThomas Zimmermann EXPORT_SYMBOL(drm_client_buffer_vunmap);
34415dd0fc8SThomas Zimmermann 
345c76f0f7cSNoralf Trønnes static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
346c76f0f7cSNoralf Trønnes {
347c76f0f7cSNoralf Trønnes 	int ret;
348c76f0f7cSNoralf Trønnes 
349c76f0f7cSNoralf Trønnes 	if (!buffer->fb)
350c76f0f7cSNoralf Trønnes 		return;
351c76f0f7cSNoralf Trønnes 
352c76f0f7cSNoralf Trønnes 	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
353c76f0f7cSNoralf Trønnes 	if (ret)
35441cb6603SJani Nikula 		drm_err(buffer->client->dev,
355c76f0f7cSNoralf Trønnes 			"Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
356c76f0f7cSNoralf Trønnes 
357c76f0f7cSNoralf Trønnes 	buffer->fb = NULL;
358c76f0f7cSNoralf Trønnes }
359c76f0f7cSNoralf Trønnes 
360c76f0f7cSNoralf Trønnes static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
361c76f0f7cSNoralf Trønnes 				   u32 width, u32 height, u32 format)
362c76f0f7cSNoralf Trønnes {
363c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client = buffer->client;
364c76f0f7cSNoralf Trønnes 	struct drm_mode_fb_cmd fb_req = { };
365c76f0f7cSNoralf Trønnes 	const struct drm_format_info *info;
366c76f0f7cSNoralf Trønnes 	int ret;
367c76f0f7cSNoralf Trønnes 
368c76f0f7cSNoralf Trønnes 	info = drm_format_info(format);
369c76f0f7cSNoralf Trønnes 	fb_req.bpp = info->cpp[0] * 8;
370c76f0f7cSNoralf Trønnes 	fb_req.depth = info->depth;
371c76f0f7cSNoralf Trønnes 	fb_req.width = width;
372c76f0f7cSNoralf Trønnes 	fb_req.height = height;
373c76f0f7cSNoralf Trønnes 	fb_req.handle = buffer->handle;
374c76f0f7cSNoralf Trønnes 	fb_req.pitch = buffer->pitch;
375c76f0f7cSNoralf Trønnes 
376c76f0f7cSNoralf Trønnes 	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
377c76f0f7cSNoralf Trønnes 	if (ret)
378c76f0f7cSNoralf Trønnes 		return ret;
379c76f0f7cSNoralf Trønnes 
380c76f0f7cSNoralf Trønnes 	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
381c76f0f7cSNoralf Trønnes 	if (WARN_ON(!buffer->fb))
382c76f0f7cSNoralf Trønnes 		return -ENOENT;
383c76f0f7cSNoralf Trønnes 
384c76f0f7cSNoralf Trønnes 	/* drop the reference we picked up in framebuffer lookup */
385c76f0f7cSNoralf Trønnes 	drm_framebuffer_put(buffer->fb);
386c76f0f7cSNoralf Trønnes 
387c76f0f7cSNoralf Trønnes 	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
388c76f0f7cSNoralf Trønnes 
389c76f0f7cSNoralf Trønnes 	return 0;
390c76f0f7cSNoralf Trønnes }
391c76f0f7cSNoralf Trønnes 
392c76f0f7cSNoralf Trønnes /**
393c76f0f7cSNoralf Trønnes  * drm_client_framebuffer_create - Create a client framebuffer
394c76f0f7cSNoralf Trønnes  * @client: DRM client
395c76f0f7cSNoralf Trønnes  * @width: Framebuffer width
396c76f0f7cSNoralf Trønnes  * @height: Framebuffer height
397c76f0f7cSNoralf Trønnes  * @format: Buffer format
398c76f0f7cSNoralf Trønnes  *
399c76f0f7cSNoralf Trønnes  * This function creates a &drm_client_buffer which consists of a
400c76f0f7cSNoralf Trønnes  * &drm_framebuffer backed by a dumb buffer.
401c76f0f7cSNoralf Trønnes  * Call drm_client_framebuffer_delete() to free the buffer.
402c76f0f7cSNoralf Trønnes  *
403c76f0f7cSNoralf Trønnes  * Returns:
404c76f0f7cSNoralf Trønnes  * Pointer to a client buffer or an error pointer on failure.
405c76f0f7cSNoralf Trønnes  */
406c76f0f7cSNoralf Trønnes struct drm_client_buffer *
407c76f0f7cSNoralf Trønnes drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
408c76f0f7cSNoralf Trønnes {
409c76f0f7cSNoralf Trønnes 	struct drm_client_buffer *buffer;
410c76f0f7cSNoralf Trønnes 	int ret;
411c76f0f7cSNoralf Trønnes 
412c76f0f7cSNoralf Trønnes 	buffer = drm_client_buffer_create(client, width, height, format);
413c76f0f7cSNoralf Trønnes 	if (IS_ERR(buffer))
414c76f0f7cSNoralf Trønnes 		return buffer;
415c76f0f7cSNoralf Trønnes 
416c76f0f7cSNoralf Trønnes 	ret = drm_client_buffer_addfb(buffer, width, height, format);
417c76f0f7cSNoralf Trønnes 	if (ret) {
418c76f0f7cSNoralf Trønnes 		drm_client_buffer_delete(buffer);
419c76f0f7cSNoralf Trønnes 		return ERR_PTR(ret);
420c76f0f7cSNoralf Trønnes 	}
421c76f0f7cSNoralf Trønnes 
422c76f0f7cSNoralf Trønnes 	return buffer;
423c76f0f7cSNoralf Trønnes }
424c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_create);
425c76f0f7cSNoralf Trønnes 
426c76f0f7cSNoralf Trønnes /**
427c76f0f7cSNoralf Trønnes  * drm_client_framebuffer_delete - Delete a client framebuffer
428c76f0f7cSNoralf Trønnes  * @buffer: DRM client buffer (can be NULL)
429c76f0f7cSNoralf Trønnes  */
430c76f0f7cSNoralf Trønnes void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
431c76f0f7cSNoralf Trønnes {
432c76f0f7cSNoralf Trønnes 	if (!buffer)
433c76f0f7cSNoralf Trønnes 		return;
434c76f0f7cSNoralf Trønnes 
435c76f0f7cSNoralf Trønnes 	drm_client_buffer_rmfb(buffer);
436c76f0f7cSNoralf Trønnes 	drm_client_buffer_delete(buffer);
437c76f0f7cSNoralf Trønnes }
438c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_delete);
439e896c132SNoralf Trønnes 
440*c9c03e3cSNoralf Trønnes /**
441*c9c03e3cSNoralf Trønnes  * drm_client_framebuffer_flush - Manually flush client framebuffer
442*c9c03e3cSNoralf Trønnes  * @buffer: DRM client buffer (can be NULL)
443*c9c03e3cSNoralf Trønnes  * @rect: Damage rectangle (if NULL flushes all)
444*c9c03e3cSNoralf Trønnes  *
445*c9c03e3cSNoralf Trønnes  * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
446*c9c03e3cSNoralf Trønnes  * for drivers that need it.
447*c9c03e3cSNoralf Trønnes  *
448*c9c03e3cSNoralf Trønnes  * Returns:
449*c9c03e3cSNoralf Trønnes  * Zero on success or negative error code on failure.
450*c9c03e3cSNoralf Trønnes  */
451*c9c03e3cSNoralf Trønnes int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
452*c9c03e3cSNoralf Trønnes {
453*c9c03e3cSNoralf Trønnes 	if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
454*c9c03e3cSNoralf Trønnes 		return 0;
455*c9c03e3cSNoralf Trønnes 
456*c9c03e3cSNoralf Trønnes 	if (rect) {
457*c9c03e3cSNoralf Trønnes 		struct drm_clip_rect clip = {
458*c9c03e3cSNoralf Trønnes 			.x1 = rect->x1,
459*c9c03e3cSNoralf Trønnes 			.y1 = rect->y1,
460*c9c03e3cSNoralf Trønnes 			.x2 = rect->x2,
461*c9c03e3cSNoralf Trønnes 			.y2 = rect->y2,
462*c9c03e3cSNoralf Trønnes 		};
463*c9c03e3cSNoralf Trønnes 
464*c9c03e3cSNoralf Trønnes 		return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
465*c9c03e3cSNoralf Trønnes 						0, 0, &clip, 1);
466*c9c03e3cSNoralf Trønnes 	}
467*c9c03e3cSNoralf Trønnes 
468*c9c03e3cSNoralf Trønnes 	return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
469*c9c03e3cSNoralf Trønnes 					0, 0, NULL, 0);
470*c9c03e3cSNoralf Trønnes }
471*c9c03e3cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_flush);
472*c9c03e3cSNoralf Trønnes 
473e896c132SNoralf Trønnes #ifdef CONFIG_DEBUG_FS
474e896c132SNoralf Trønnes static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
475e896c132SNoralf Trønnes {
476e896c132SNoralf Trønnes 	struct drm_info_node *node = m->private;
477e896c132SNoralf Trønnes 	struct drm_device *dev = node->minor->dev;
478e896c132SNoralf Trønnes 	struct drm_printer p = drm_seq_file_printer(m);
479e896c132SNoralf Trønnes 	struct drm_client_dev *client;
480e896c132SNoralf Trønnes 
481e896c132SNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
482e896c132SNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list)
483e896c132SNoralf Trønnes 		drm_printf(&p, "%s\n", client->name);
484e896c132SNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
485e896c132SNoralf Trønnes 
486e896c132SNoralf Trønnes 	return 0;
487e896c132SNoralf Trønnes }
488e896c132SNoralf Trønnes 
489e896c132SNoralf Trønnes static const struct drm_info_list drm_client_debugfs_list[] = {
490e896c132SNoralf Trønnes 	{ "internal_clients", drm_client_debugfs_internal_clients, 0 },
491e896c132SNoralf Trønnes };
492e896c132SNoralf Trønnes 
4937ce84471SWambui Karuga void drm_client_debugfs_init(struct drm_minor *minor)
494e896c132SNoralf Trønnes {
495e196e140SWambui Karuga 	drm_debugfs_create_files(drm_client_debugfs_list,
496e896c132SNoralf Trønnes 				 ARRAY_SIZE(drm_client_debugfs_list),
497e896c132SNoralf Trønnes 				 minor->debugfs_root, minor);
498e896c132SNoralf Trønnes }
499e896c132SNoralf Trønnes #endif
500