xref: /linux/drivers/gpu/drm/drm_client.c (revision 27b2ae654370e1a8e446b0e48c4e406abed12ca1)
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 
67938f421SLucas De Marchi #include <linux/iosys-map.h>
7c76f0f7cSNoralf Trønnes #include <linux/list.h>
8c76f0f7cSNoralf Trønnes #include <linux/module.h>
9c76f0f7cSNoralf Trønnes #include <linux/mutex.h>
10c76f0f7cSNoralf Trønnes #include <linux/seq_file.h>
11c76f0f7cSNoralf Trønnes #include <linux/slab.h>
12c76f0f7cSNoralf Trønnes 
13c76f0f7cSNoralf Trønnes #include <drm/drm_client.h>
14c76f0f7cSNoralf Trønnes #include <drm/drm_debugfs.h>
15c76f0f7cSNoralf Trønnes #include <drm/drm_device.h>
16c76f0f7cSNoralf Trønnes #include <drm/drm_drv.h>
17c76f0f7cSNoralf Trønnes #include <drm/drm_file.h>
18c76f0f7cSNoralf Trønnes #include <drm/drm_fourcc.h>
190500c04eSSam Ravnborg #include <drm/drm_framebuffer.h>
20c76f0f7cSNoralf Trønnes #include <drm/drm_gem.h>
21c76f0f7cSNoralf Trønnes #include <drm/drm_mode.h>
22c76f0f7cSNoralf Trønnes #include <drm/drm_print.h>
23c76f0f7cSNoralf Trønnes 
24c76f0f7cSNoralf Trønnes #include "drm_crtc_internal.h"
25c76f0f7cSNoralf Trønnes #include "drm_internal.h"
26c76f0f7cSNoralf Trønnes 
27c76f0f7cSNoralf Trønnes /**
28c76f0f7cSNoralf Trønnes  * DOC: overview
29c76f0f7cSNoralf Trønnes  *
30c76f0f7cSNoralf Trønnes  * This library provides support for clients running in the kernel like fbdev and bootsplash.
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 
64c76f0f7cSNoralf Trønnes /**
654d4c2d89SNoralf Trønnes  * drm_client_init - Initialise a DRM client
66c76f0f7cSNoralf Trønnes  * @dev: DRM device
67c76f0f7cSNoralf Trønnes  * @client: DRM client
68c76f0f7cSNoralf Trønnes  * @name: Client name
69c76f0f7cSNoralf Trønnes  * @funcs: DRM client functions (optional)
70c76f0f7cSNoralf Trønnes  *
71e33898a2SNoralf Trønnes  * This initialises the client and opens a &drm_file.
72e33898a2SNoralf Trønnes  * Use drm_client_register() 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 
84b39b5394SNoralf 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 
94d81294afSNoralf Trønnes 	ret = drm_client_modeset_create(client);
95c76f0f7cSNoralf Trønnes 	if (ret)
96c76f0f7cSNoralf Trønnes 		goto err_put_module;
97c76f0f7cSNoralf Trønnes 
98d81294afSNoralf Trønnes 	ret = drm_client_open(client);
99d81294afSNoralf Trønnes 	if (ret)
100d81294afSNoralf Trønnes 		goto err_free;
101d81294afSNoralf Trønnes 
102c76f0f7cSNoralf Trønnes 	drm_dev_get(dev);
103c76f0f7cSNoralf Trønnes 
104c76f0f7cSNoralf Trønnes 	return 0;
105c76f0f7cSNoralf Trønnes 
106d81294afSNoralf Trønnes err_free:
107d81294afSNoralf Trønnes 	drm_client_modeset_free(client);
108c76f0f7cSNoralf Trønnes err_put_module:
109c76f0f7cSNoralf Trønnes 	if (funcs)
110c76f0f7cSNoralf Trønnes 		module_put(funcs->owner);
111c76f0f7cSNoralf Trønnes 
112c76f0f7cSNoralf Trønnes 	return ret;
113c76f0f7cSNoralf Trønnes }
1144d4c2d89SNoralf Trønnes EXPORT_SYMBOL(drm_client_init);
1154d4c2d89SNoralf Trønnes 
1164d4c2d89SNoralf Trønnes /**
117e33898a2SNoralf Trønnes  * drm_client_register - Register client
1184d4c2d89SNoralf Trønnes  * @client: DRM client
1194d4c2d89SNoralf Trønnes  *
1204d4c2d89SNoralf Trønnes  * Add the client to the &drm_device client list to activate its callbacks.
1214d4c2d89SNoralf Trønnes  * @client must be initialized by a call to drm_client_init(). After
122e33898a2SNoralf Trønnes  * drm_client_register() it is no longer permissible to call drm_client_release()
1234d4c2d89SNoralf Trønnes  * directly (outside the unregister callback), instead cleanup will happen
1244d4c2d89SNoralf Trønnes  * automatically on driver unload.
1254d4c2d89SNoralf Trønnes  */
126e33898a2SNoralf Trønnes void drm_client_register(struct drm_client_dev *client)
1274d4c2d89SNoralf Trønnes {
1284d4c2d89SNoralf Trønnes 	struct drm_device *dev = client->dev;
1294d4c2d89SNoralf Trønnes 
1304d4c2d89SNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
1314d4c2d89SNoralf Trønnes 	list_add(&client->list, &dev->clientlist);
1324d4c2d89SNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
1334d4c2d89SNoralf Trønnes }
134e33898a2SNoralf Trønnes EXPORT_SYMBOL(drm_client_register);
135c76f0f7cSNoralf Trønnes 
136c76f0f7cSNoralf Trønnes /**
137c76f0f7cSNoralf Trønnes  * drm_client_release - Release DRM client resources
138c76f0f7cSNoralf Trønnes  * @client: DRM client
139c76f0f7cSNoralf Trønnes  *
1404d4c2d89SNoralf Trønnes  * Releases resources by closing the &drm_file that was opened by drm_client_init().
141c76f0f7cSNoralf Trønnes  * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
142c76f0f7cSNoralf Trønnes  *
143c76f0f7cSNoralf Trønnes  * This function should only be called from the unregister callback. An exception
144c76f0f7cSNoralf Trønnes  * is fbdev which cannot free the buffer if userspace has open file descriptors.
145c76f0f7cSNoralf Trønnes  *
146c76f0f7cSNoralf Trønnes  * Note:
147c76f0f7cSNoralf Trønnes  * Clients cannot initiate a release by themselves. This is done to keep the code simple.
148c76f0f7cSNoralf Trønnes  * The driver has to be unloaded before the client can be unloaded.
149c76f0f7cSNoralf Trønnes  */
150c76f0f7cSNoralf Trønnes void drm_client_release(struct drm_client_dev *client)
151c76f0f7cSNoralf Trønnes {
152c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
153c76f0f7cSNoralf Trønnes 
15441cb6603SJani Nikula 	drm_dbg_kms(dev, "%s\n", client->name);
155c76f0f7cSNoralf Trønnes 
156d81294afSNoralf Trønnes 	drm_client_modeset_free(client);
157c76f0f7cSNoralf Trønnes 	drm_client_close(client);
158c76f0f7cSNoralf Trønnes 	drm_dev_put(dev);
159c76f0f7cSNoralf Trønnes 	if (client->funcs)
160c76f0f7cSNoralf Trønnes 		module_put(client->funcs->owner);
161c76f0f7cSNoralf Trønnes }
162c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_release);
163c76f0f7cSNoralf Trønnes 
164c76f0f7cSNoralf Trønnes void drm_client_dev_unregister(struct drm_device *dev)
165c76f0f7cSNoralf Trønnes {
166c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client, *tmp;
167c76f0f7cSNoralf Trønnes 
168c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
169c76f0f7cSNoralf Trønnes 		return;
170c76f0f7cSNoralf Trønnes 
171c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
172c76f0f7cSNoralf Trønnes 	list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
173c76f0f7cSNoralf Trønnes 		list_del(&client->list);
174c76f0f7cSNoralf Trønnes 		if (client->funcs && client->funcs->unregister) {
175c76f0f7cSNoralf Trønnes 			client->funcs->unregister(client);
176c76f0f7cSNoralf Trønnes 		} else {
177c76f0f7cSNoralf Trønnes 			drm_client_release(client);
178c76f0f7cSNoralf Trønnes 			kfree(client);
179c76f0f7cSNoralf Trønnes 		}
180c76f0f7cSNoralf Trønnes 	}
181c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
182c76f0f7cSNoralf Trønnes }
183c76f0f7cSNoralf Trønnes 
184c76f0f7cSNoralf Trønnes /**
185c76f0f7cSNoralf Trønnes  * drm_client_dev_hotplug - Send hotplug event to clients
186c76f0f7cSNoralf Trønnes  * @dev: DRM device
187c76f0f7cSNoralf Trønnes  *
188c76f0f7cSNoralf Trønnes  * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
189c76f0f7cSNoralf Trønnes  *
190c76f0f7cSNoralf Trønnes  * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
191c76f0f7cSNoralf Trønnes  * don't need to call this function themselves.
192c76f0f7cSNoralf Trønnes  */
193c76f0f7cSNoralf Trønnes void drm_client_dev_hotplug(struct drm_device *dev)
194c76f0f7cSNoralf Trønnes {
195c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client;
196c76f0f7cSNoralf Trønnes 	int ret;
197c76f0f7cSNoralf Trønnes 
198c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
199c76f0f7cSNoralf Trønnes 		return;
200c76f0f7cSNoralf Trønnes 
201c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
202c76f0f7cSNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list) {
203c76f0f7cSNoralf Trønnes 		if (!client->funcs || !client->funcs->hotplug)
204c76f0f7cSNoralf Trønnes 			continue;
205c76f0f7cSNoralf Trønnes 
206c76f0f7cSNoralf Trønnes 		ret = client->funcs->hotplug(client);
20741cb6603SJani Nikula 		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
208c76f0f7cSNoralf Trønnes 	}
209c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
210c76f0f7cSNoralf Trønnes }
211c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_dev_hotplug);
212c76f0f7cSNoralf Trønnes 
213c76f0f7cSNoralf Trønnes void drm_client_dev_restore(struct drm_device *dev)
214c76f0f7cSNoralf Trønnes {
215c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client;
216c76f0f7cSNoralf Trønnes 	int ret;
217c76f0f7cSNoralf Trønnes 
218c76f0f7cSNoralf Trønnes 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
219c76f0f7cSNoralf Trønnes 		return;
220c76f0f7cSNoralf Trønnes 
221c76f0f7cSNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
222c76f0f7cSNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list) {
223c76f0f7cSNoralf Trønnes 		if (!client->funcs || !client->funcs->restore)
224c76f0f7cSNoralf Trønnes 			continue;
225c76f0f7cSNoralf Trønnes 
226c76f0f7cSNoralf Trønnes 		ret = client->funcs->restore(client);
22741cb6603SJani Nikula 		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
228c76f0f7cSNoralf Trønnes 		if (!ret) /* The first one to return zero gets the privilege to restore */
229c76f0f7cSNoralf Trønnes 			break;
230c76f0f7cSNoralf Trønnes 	}
231c76f0f7cSNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
232c76f0f7cSNoralf Trønnes }
233c76f0f7cSNoralf Trønnes 
234c76f0f7cSNoralf Trønnes static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
235c76f0f7cSNoralf Trønnes {
236c76f0f7cSNoralf Trønnes 	struct drm_device *dev = buffer->client->dev;
237c76f0f7cSNoralf Trønnes 
238*27b2ae65SDmitry Osipenko 	drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
239c76f0f7cSNoralf Trønnes 
240c76f0f7cSNoralf Trønnes 	if (buffer->gem)
241be6ee102SEmil Velikov 		drm_gem_object_put(buffer->gem);
242c76f0f7cSNoralf Trønnes 
243cf19fa2cSNoralf Trønnes 	if (buffer->handle)
244c76f0f7cSNoralf Trønnes 		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
245cf19fa2cSNoralf Trønnes 
246c76f0f7cSNoralf Trønnes 	kfree(buffer);
247c76f0f7cSNoralf Trønnes }
248c76f0f7cSNoralf Trønnes 
249c76f0f7cSNoralf Trønnes static struct drm_client_buffer *
250c76f0f7cSNoralf Trønnes drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
251c76f0f7cSNoralf Trønnes {
25224c478eaSMaxime Ripard 	const struct drm_format_info *info = drm_format_info(format);
253c76f0f7cSNoralf Trønnes 	struct drm_mode_create_dumb dumb_args = { };
254c76f0f7cSNoralf Trønnes 	struct drm_device *dev = client->dev;
255c76f0f7cSNoralf Trønnes 	struct drm_client_buffer *buffer;
256c76f0f7cSNoralf Trønnes 	struct drm_gem_object *obj;
257c76f0f7cSNoralf Trønnes 	int ret;
258c76f0f7cSNoralf Trønnes 
259c76f0f7cSNoralf Trønnes 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
260c76f0f7cSNoralf Trønnes 	if (!buffer)
261c76f0f7cSNoralf Trønnes 		return ERR_PTR(-ENOMEM);
262c76f0f7cSNoralf Trønnes 
263c76f0f7cSNoralf Trønnes 	buffer->client = client;
264c76f0f7cSNoralf Trønnes 
265c76f0f7cSNoralf Trønnes 	dumb_args.width = width;
266c76f0f7cSNoralf Trønnes 	dumb_args.height = height;
267356d2c8eSGeert Uytterhoeven 	dumb_args.bpp = drm_format_info_bpp(info, 0);
268c76f0f7cSNoralf Trønnes 	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
269c76f0f7cSNoralf Trønnes 	if (ret)
270cf19fa2cSNoralf Trønnes 		goto err_delete;
271c76f0f7cSNoralf Trønnes 
272c76f0f7cSNoralf Trønnes 	buffer->handle = dumb_args.handle;
273c76f0f7cSNoralf Trønnes 	buffer->pitch = dumb_args.pitch;
274c76f0f7cSNoralf Trønnes 
275c76f0f7cSNoralf Trønnes 	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
276c76f0f7cSNoralf Trønnes 	if (!obj)  {
277c76f0f7cSNoralf Trønnes 		ret = -ENOENT;
278c76f0f7cSNoralf Trønnes 		goto err_delete;
279c76f0f7cSNoralf Trønnes 	}
280c76f0f7cSNoralf Trønnes 
281c76f0f7cSNoralf Trønnes 	buffer->gem = obj;
282c76f0f7cSNoralf Trønnes 
283c76f0f7cSNoralf Trønnes 	return buffer;
284c76f0f7cSNoralf Trønnes 
285c76f0f7cSNoralf Trønnes err_delete:
286c76f0f7cSNoralf Trønnes 	drm_client_buffer_delete(buffer);
287c76f0f7cSNoralf Trønnes 
288c76f0f7cSNoralf Trønnes 	return ERR_PTR(ret);
289c76f0f7cSNoralf Trønnes }
290c76f0f7cSNoralf Trønnes 
29115dd0fc8SThomas Zimmermann /**
29215dd0fc8SThomas Zimmermann  * drm_client_buffer_vmap - Map DRM client buffer into address space
29315dd0fc8SThomas Zimmermann  * @buffer: DRM client buffer
294a8595556SThomas Zimmermann  * @map_copy: Returns the mapped memory's address
29515dd0fc8SThomas Zimmermann  *
29615dd0fc8SThomas Zimmermann  * This function maps a client buffer into kernel address space. If the
297a8595556SThomas Zimmermann  * buffer is already mapped, it returns the existing mapping's address.
29815dd0fc8SThomas Zimmermann  *
29915dd0fc8SThomas Zimmermann  * Client buffer mappings are not ref'counted. Each call to
30015dd0fc8SThomas Zimmermann  * drm_client_buffer_vmap() should be followed by a call to
30115dd0fc8SThomas Zimmermann  * drm_client_buffer_vunmap(); or the client buffer should be mapped
302cf1ca9aeSThomas Zimmermann  * throughout its lifetime.
30315dd0fc8SThomas Zimmermann  *
304a8595556SThomas Zimmermann  * The returned address is a copy of the internal value. In contrast to
305a8595556SThomas Zimmermann  * other vmap interfaces, you don't need it for the client's vunmap
306a8595556SThomas Zimmermann  * function. So you can modify it at will during blit and draw operations.
307a8595556SThomas Zimmermann  *
30815dd0fc8SThomas Zimmermann  * Returns:
309a8595556SThomas Zimmermann  *	0 on success, or a negative errno code otherwise.
31015dd0fc8SThomas Zimmermann  */
311a8595556SThomas Zimmermann int
3127938f421SLucas De Marchi drm_client_buffer_vmap(struct drm_client_buffer *buffer,
3137938f421SLucas De Marchi 		       struct iosys_map *map_copy)
31415dd0fc8SThomas Zimmermann {
3157938f421SLucas De Marchi 	struct iosys_map *map = &buffer->map;
316a745fb1cSThomas Zimmermann 	int ret;
31715dd0fc8SThomas Zimmermann 
31815dd0fc8SThomas Zimmermann 	/*
31915dd0fc8SThomas Zimmermann 	 * FIXME: The dependency on GEM here isn't required, we could
32015dd0fc8SThomas Zimmermann 	 * convert the driver handle to a dma-buf instead and use the
32115dd0fc8SThomas Zimmermann 	 * backend-agnostic dma-buf vmap support instead. This would
32215dd0fc8SThomas Zimmermann 	 * require that the handle2fd prime ioctl is reworked to pull the
32315dd0fc8SThomas Zimmermann 	 * fd_install step out of the driver backend hooks, to make that
32415dd0fc8SThomas Zimmermann 	 * final step optional for internal users.
32515dd0fc8SThomas Zimmermann 	 */
32679e2cf2eSDmitry Osipenko 	ret = drm_gem_vmap_unlocked(buffer->gem, map);
327a745fb1cSThomas Zimmermann 	if (ret)
328a8595556SThomas Zimmermann 		return ret;
32915dd0fc8SThomas Zimmermann 
330a8595556SThomas Zimmermann 	*map_copy = *map;
33115dd0fc8SThomas Zimmermann 
332a8595556SThomas Zimmermann 	return 0;
33315dd0fc8SThomas Zimmermann }
33415dd0fc8SThomas Zimmermann EXPORT_SYMBOL(drm_client_buffer_vmap);
33515dd0fc8SThomas Zimmermann 
33615dd0fc8SThomas Zimmermann /**
33715dd0fc8SThomas Zimmermann  * drm_client_buffer_vunmap - Unmap DRM client buffer
33815dd0fc8SThomas Zimmermann  * @buffer: DRM client buffer
33915dd0fc8SThomas Zimmermann  *
340cf1ca9aeSThomas Zimmermann  * This function removes a client buffer's memory mapping. Calling this
341cf1ca9aeSThomas Zimmermann  * function is only required by clients that manage their buffer mappings
342cf1ca9aeSThomas Zimmermann  * by themselves.
34315dd0fc8SThomas Zimmermann  */
34415dd0fc8SThomas Zimmermann void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
34515dd0fc8SThomas Zimmermann {
3467938f421SLucas De Marchi 	struct iosys_map *map = &buffer->map;
347a745fb1cSThomas Zimmermann 
34879e2cf2eSDmitry Osipenko 	drm_gem_vunmap_unlocked(buffer->gem, map);
34915dd0fc8SThomas Zimmermann }
35015dd0fc8SThomas Zimmermann EXPORT_SYMBOL(drm_client_buffer_vunmap);
35115dd0fc8SThomas Zimmermann 
352c76f0f7cSNoralf Trønnes static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
353c76f0f7cSNoralf Trønnes {
354c76f0f7cSNoralf Trønnes 	int ret;
355c76f0f7cSNoralf Trønnes 
356c76f0f7cSNoralf Trønnes 	if (!buffer->fb)
357c76f0f7cSNoralf Trønnes 		return;
358c76f0f7cSNoralf Trønnes 
359c76f0f7cSNoralf Trønnes 	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
360c76f0f7cSNoralf Trønnes 	if (ret)
36141cb6603SJani Nikula 		drm_err(buffer->client->dev,
362c76f0f7cSNoralf Trønnes 			"Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
363c76f0f7cSNoralf Trønnes 
364c76f0f7cSNoralf Trønnes 	buffer->fb = NULL;
365c76f0f7cSNoralf Trønnes }
366c76f0f7cSNoralf Trønnes 
367c76f0f7cSNoralf Trønnes static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
368c76f0f7cSNoralf Trønnes 				   u32 width, u32 height, u32 format)
369c76f0f7cSNoralf Trønnes {
370c76f0f7cSNoralf Trønnes 	struct drm_client_dev *client = buffer->client;
371c76f0f7cSNoralf Trønnes 	struct drm_mode_fb_cmd fb_req = { };
372c76f0f7cSNoralf Trønnes 	const struct drm_format_info *info;
373c76f0f7cSNoralf Trønnes 	int ret;
374c76f0f7cSNoralf Trønnes 
375c76f0f7cSNoralf Trønnes 	info = drm_format_info(format);
376356d2c8eSGeert Uytterhoeven 	fb_req.bpp = drm_format_info_bpp(info, 0);
377c76f0f7cSNoralf Trønnes 	fb_req.depth = info->depth;
378c76f0f7cSNoralf Trønnes 	fb_req.width = width;
379c76f0f7cSNoralf Trønnes 	fb_req.height = height;
380c76f0f7cSNoralf Trønnes 	fb_req.handle = buffer->handle;
381c76f0f7cSNoralf Trønnes 	fb_req.pitch = buffer->pitch;
382c76f0f7cSNoralf Trønnes 
383c76f0f7cSNoralf Trønnes 	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
384c76f0f7cSNoralf Trønnes 	if (ret)
385c76f0f7cSNoralf Trønnes 		return ret;
386c76f0f7cSNoralf Trønnes 
387c76f0f7cSNoralf Trønnes 	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
388c76f0f7cSNoralf Trønnes 	if (WARN_ON(!buffer->fb))
389c76f0f7cSNoralf Trønnes 		return -ENOENT;
390c76f0f7cSNoralf Trønnes 
391c76f0f7cSNoralf Trønnes 	/* drop the reference we picked up in framebuffer lookup */
392c76f0f7cSNoralf Trønnes 	drm_framebuffer_put(buffer->fb);
393c76f0f7cSNoralf Trønnes 
394c76f0f7cSNoralf Trønnes 	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
395c76f0f7cSNoralf Trønnes 
396c76f0f7cSNoralf Trønnes 	return 0;
397c76f0f7cSNoralf Trønnes }
398c76f0f7cSNoralf Trønnes 
399c76f0f7cSNoralf Trønnes /**
400c76f0f7cSNoralf Trønnes  * drm_client_framebuffer_create - Create a client framebuffer
401c76f0f7cSNoralf Trønnes  * @client: DRM client
402c76f0f7cSNoralf Trønnes  * @width: Framebuffer width
403c76f0f7cSNoralf Trønnes  * @height: Framebuffer height
404c76f0f7cSNoralf Trønnes  * @format: Buffer format
405c76f0f7cSNoralf Trønnes  *
406c76f0f7cSNoralf Trønnes  * This function creates a &drm_client_buffer which consists of a
407c76f0f7cSNoralf Trønnes  * &drm_framebuffer backed by a dumb buffer.
408c76f0f7cSNoralf Trønnes  * Call drm_client_framebuffer_delete() to free the buffer.
409c76f0f7cSNoralf Trønnes  *
410c76f0f7cSNoralf Trønnes  * Returns:
411c76f0f7cSNoralf Trønnes  * Pointer to a client buffer or an error pointer on failure.
412c76f0f7cSNoralf Trønnes  */
413c76f0f7cSNoralf Trønnes struct drm_client_buffer *
414c76f0f7cSNoralf Trønnes drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
415c76f0f7cSNoralf Trønnes {
416c76f0f7cSNoralf Trønnes 	struct drm_client_buffer *buffer;
417c76f0f7cSNoralf Trønnes 	int ret;
418c76f0f7cSNoralf Trønnes 
419c76f0f7cSNoralf Trønnes 	buffer = drm_client_buffer_create(client, width, height, format);
420c76f0f7cSNoralf Trønnes 	if (IS_ERR(buffer))
421c76f0f7cSNoralf Trønnes 		return buffer;
422c76f0f7cSNoralf Trønnes 
423c76f0f7cSNoralf Trønnes 	ret = drm_client_buffer_addfb(buffer, width, height, format);
424c76f0f7cSNoralf Trønnes 	if (ret) {
425c76f0f7cSNoralf Trønnes 		drm_client_buffer_delete(buffer);
426c76f0f7cSNoralf Trønnes 		return ERR_PTR(ret);
427c76f0f7cSNoralf Trønnes 	}
428c76f0f7cSNoralf Trønnes 
429c76f0f7cSNoralf Trønnes 	return buffer;
430c76f0f7cSNoralf Trønnes }
431c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_create);
432c76f0f7cSNoralf Trønnes 
433c76f0f7cSNoralf Trønnes /**
434c76f0f7cSNoralf Trønnes  * drm_client_framebuffer_delete - Delete a client framebuffer
435c76f0f7cSNoralf Trønnes  * @buffer: DRM client buffer (can be NULL)
436c76f0f7cSNoralf Trønnes  */
437c76f0f7cSNoralf Trønnes void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
438c76f0f7cSNoralf Trønnes {
439c76f0f7cSNoralf Trønnes 	if (!buffer)
440c76f0f7cSNoralf Trønnes 		return;
441c76f0f7cSNoralf Trønnes 
442c76f0f7cSNoralf Trønnes 	drm_client_buffer_rmfb(buffer);
443c76f0f7cSNoralf Trønnes 	drm_client_buffer_delete(buffer);
444c76f0f7cSNoralf Trønnes }
445c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_delete);
446e896c132SNoralf Trønnes 
447c9c03e3cSNoralf Trønnes /**
448c9c03e3cSNoralf Trønnes  * drm_client_framebuffer_flush - Manually flush client framebuffer
449c9c03e3cSNoralf Trønnes  * @buffer: DRM client buffer (can be NULL)
450c9c03e3cSNoralf Trønnes  * @rect: Damage rectangle (if NULL flushes all)
451c9c03e3cSNoralf Trønnes  *
452c9c03e3cSNoralf Trønnes  * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
453c9c03e3cSNoralf Trønnes  * for drivers that need it.
454c9c03e3cSNoralf Trønnes  *
455c9c03e3cSNoralf Trønnes  * Returns:
456c9c03e3cSNoralf Trønnes  * Zero on success or negative error code on failure.
457c9c03e3cSNoralf Trønnes  */
458c9c03e3cSNoralf Trønnes int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
459c9c03e3cSNoralf Trønnes {
460c9c03e3cSNoralf Trønnes 	if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
461c9c03e3cSNoralf Trønnes 		return 0;
462c9c03e3cSNoralf Trønnes 
463c9c03e3cSNoralf Trønnes 	if (rect) {
464c9c03e3cSNoralf Trønnes 		struct drm_clip_rect clip = {
465c9c03e3cSNoralf Trønnes 			.x1 = rect->x1,
466c9c03e3cSNoralf Trønnes 			.y1 = rect->y1,
467c9c03e3cSNoralf Trønnes 			.x2 = rect->x2,
468c9c03e3cSNoralf Trønnes 			.y2 = rect->y2,
469c9c03e3cSNoralf Trønnes 		};
470c9c03e3cSNoralf Trønnes 
471c9c03e3cSNoralf Trønnes 		return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
472c9c03e3cSNoralf Trønnes 						0, 0, &clip, 1);
473c9c03e3cSNoralf Trønnes 	}
474c9c03e3cSNoralf Trønnes 
475c9c03e3cSNoralf Trønnes 	return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
476c9c03e3cSNoralf Trønnes 					0, 0, NULL, 0);
477c9c03e3cSNoralf Trønnes }
478c9c03e3cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_flush);
479c9c03e3cSNoralf Trønnes 
480e896c132SNoralf Trønnes #ifdef CONFIG_DEBUG_FS
481e896c132SNoralf Trønnes static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
482e896c132SNoralf Trønnes {
483e896c132SNoralf Trønnes 	struct drm_info_node *node = m->private;
484e896c132SNoralf Trønnes 	struct drm_device *dev = node->minor->dev;
485e896c132SNoralf Trønnes 	struct drm_printer p = drm_seq_file_printer(m);
486e896c132SNoralf Trønnes 	struct drm_client_dev *client;
487e896c132SNoralf Trønnes 
488e896c132SNoralf Trønnes 	mutex_lock(&dev->clientlist_mutex);
489e896c132SNoralf Trønnes 	list_for_each_entry(client, &dev->clientlist, list)
490e896c132SNoralf Trønnes 		drm_printf(&p, "%s\n", client->name);
491e896c132SNoralf Trønnes 	mutex_unlock(&dev->clientlist_mutex);
492e896c132SNoralf Trønnes 
493e896c132SNoralf Trønnes 	return 0;
494e896c132SNoralf Trønnes }
495e896c132SNoralf Trønnes 
496e896c132SNoralf Trønnes static const struct drm_info_list drm_client_debugfs_list[] = {
497e896c132SNoralf Trønnes 	{ "internal_clients", drm_client_debugfs_internal_clients, 0 },
498e896c132SNoralf Trønnes };
499e896c132SNoralf Trønnes 
5007ce84471SWambui Karuga void drm_client_debugfs_init(struct drm_minor *minor)
501e896c132SNoralf Trønnes {
502e196e140SWambui Karuga 	drm_debugfs_create_files(drm_client_debugfs_list,
503e896c132SNoralf Trønnes 				 ARRAY_SIZE(drm_client_debugfs_list),
504e896c132SNoralf Trønnes 				 minor->debugfs_root, minor);
505e896c132SNoralf Trønnes }
506e896c132SNoralf Trønnes #endif
507