Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0 or MIT
  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_framebuffer.h>
 19#include <drm/drm_gem.h>
 20#include <drm/drm_mode.h>
 21#include <drm/drm_print.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 *
 31 * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
 32 */
 33
 34static int drm_client_open(struct drm_client_dev *client)
 35{
 36	struct drm_device *dev = client->dev;
 37	struct drm_file *file;
 38
 39	file = drm_file_alloc(dev->primary);
 40	if (IS_ERR(file))
 41		return PTR_ERR(file);
 42
 43	mutex_lock(&dev->filelist_mutex);
 44	list_add(&file->lhead, &dev->filelist_internal);
 45	mutex_unlock(&dev->filelist_mutex);
 46
 47	client->file = file;
 48
 49	return 0;
 50}
 51
 52static void drm_client_close(struct drm_client_dev *client)
 53{
 54	struct drm_device *dev = client->dev;
 55
 56	mutex_lock(&dev->filelist_mutex);
 57	list_del(&client->file->lhead);
 58	mutex_unlock(&dev->filelist_mutex);
 59
 60	drm_file_free(client->file);
 61}
 62
 63/**
 64 * drm_client_init - Initialise a DRM client
 65 * @dev: DRM device
 66 * @client: DRM client
 67 * @name: Client name
 68 * @funcs: DRM client functions (optional)
 69 *
 70 * This initialises the client and opens a &drm_file.
 71 * Use drm_client_register() to complete the process.
 72 * The caller needs to hold a reference on @dev before calling this function.
 73 * The client is freed when the &drm_device is unregistered. See drm_client_release().
 74 *
 75 * Returns:
 76 * Zero on success or negative error code on failure.
 77 */
 78int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 79		    const char *name, const struct drm_client_funcs *funcs)
 80{
 81	int ret;
 82
 83	if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
 84		return -EOPNOTSUPP;
 85
 86	if (funcs && !try_module_get(funcs->owner))
 87		return -ENODEV;
 88
 89	client->dev = dev;
 90	client->name = name;
 91	client->funcs = funcs;
 92
 93	ret = drm_client_modeset_create(client);
 94	if (ret)
 95		goto err_put_module;
 96
 97	ret = drm_client_open(client);
 98	if (ret)
 99		goto err_free;
100
101	drm_dev_get(dev);
102
103	return 0;
104
105err_free:
106	drm_client_modeset_free(client);
107err_put_module:
108	if (funcs)
109		module_put(funcs->owner);
110
111	return ret;
112}
113EXPORT_SYMBOL(drm_client_init);
114
115/**
116 * drm_client_register - Register client
117 * @client: DRM client
118 *
119 * Add the client to the &drm_device client list to activate its callbacks.
120 * @client must be initialized by a call to drm_client_init(). After
121 * drm_client_register() it is no longer permissible to call drm_client_release()
122 * directly (outside the unregister callback), instead cleanup will happen
123 * automatically on driver unload.
 
 
 
 
124 */
125void drm_client_register(struct drm_client_dev *client)
126{
127	struct drm_device *dev = client->dev;
 
128
129	mutex_lock(&dev->clientlist_mutex);
130	list_add(&client->list, &dev->clientlist);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131	mutex_unlock(&dev->clientlist_mutex);
132}
133EXPORT_SYMBOL(drm_client_register);
134
135/**
136 * drm_client_release - Release DRM client resources
137 * @client: DRM client
138 *
139 * Releases resources by closing the &drm_file that was opened by drm_client_init().
140 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
141 *
142 * This function should only be called from the unregister callback. An exception
143 * is fbdev which cannot free the buffer if userspace has open file descriptors.
144 *
145 * Note:
146 * Clients cannot initiate a release by themselves. This is done to keep the code simple.
147 * The driver has to be unloaded before the client can be unloaded.
148 */
149void drm_client_release(struct drm_client_dev *client)
150{
151	struct drm_device *dev = client->dev;
152
153	drm_dbg_kms(dev, "%s\n", client->name);
154
155	drm_client_modeset_free(client);
156	drm_client_close(client);
157	drm_dev_put(dev);
158	if (client->funcs)
159		module_put(client->funcs->owner);
160}
161EXPORT_SYMBOL(drm_client_release);
162
163void drm_client_dev_unregister(struct drm_device *dev)
164{
165	struct drm_client_dev *client, *tmp;
166
167	if (!drm_core_check_feature(dev, DRIVER_MODESET))
168		return;
169
170	mutex_lock(&dev->clientlist_mutex);
171	list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
172		list_del(&client->list);
173		if (client->funcs && client->funcs->unregister) {
174			client->funcs->unregister(client);
175		} else {
176			drm_client_release(client);
177			kfree(client);
178		}
179	}
180	mutex_unlock(&dev->clientlist_mutex);
181}
182
183/**
184 * drm_client_dev_hotplug - Send hotplug event to clients
185 * @dev: DRM device
186 *
187 * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
188 *
189 * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
190 * don't need to call this function themselves.
191 */
192void drm_client_dev_hotplug(struct drm_device *dev)
193{
194	struct drm_client_dev *client;
195	int ret;
196
197	if (!drm_core_check_feature(dev, DRIVER_MODESET))
198		return;
199
 
 
 
 
 
200	mutex_lock(&dev->clientlist_mutex);
201	list_for_each_entry(client, &dev->clientlist, list) {
202		if (!client->funcs || !client->funcs->hotplug)
203			continue;
204
 
 
 
205		ret = client->funcs->hotplug(client);
206		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
 
 
207	}
208	mutex_unlock(&dev->clientlist_mutex);
209}
210EXPORT_SYMBOL(drm_client_dev_hotplug);
211
212void drm_client_dev_restore(struct drm_device *dev)
213{
214	struct drm_client_dev *client;
215	int ret;
216
217	if (!drm_core_check_feature(dev, DRIVER_MODESET))
218		return;
219
220	mutex_lock(&dev->clientlist_mutex);
221	list_for_each_entry(client, &dev->clientlist, list) {
222		if (!client->funcs || !client->funcs->restore)
223			continue;
224
225		ret = client->funcs->restore(client);
226		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
227		if (!ret) /* The first one to return zero gets the privilege to restore */
228			break;
229	}
230	mutex_unlock(&dev->clientlist_mutex);
231}
232
233static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
234{
235	struct drm_device *dev = buffer->client->dev;
236
237	drm_gem_vunmap(buffer->gem, buffer->vaddr);
238
239	if (buffer->gem)
240		drm_gem_object_put(buffer->gem);
241
242	if (buffer->handle)
243		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
244
245	kfree(buffer);
246}
247
248static struct drm_client_buffer *
249drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
 
250{
251	const struct drm_format_info *info = drm_format_info(format);
252	struct drm_mode_create_dumb dumb_args = { };
253	struct drm_device *dev = client->dev;
254	struct drm_client_buffer *buffer;
255	struct drm_gem_object *obj;
256	int ret;
257
258	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
259	if (!buffer)
260		return ERR_PTR(-ENOMEM);
261
262	buffer->client = client;
263
264	dumb_args.width = width;
265	dumb_args.height = height;
266	dumb_args.bpp = info->cpp[0] * 8;
267	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
268	if (ret)
269		goto err_delete;
270
271	buffer->handle = dumb_args.handle;
272	buffer->pitch = dumb_args.pitch;
273
274	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
275	if (!obj)  {
276		ret = -ENOENT;
277		goto err_delete;
278	}
279
 
280	buffer->gem = obj;
 
281
282	return buffer;
283
284err_delete:
285	drm_client_buffer_delete(buffer);
286
287	return ERR_PTR(ret);
288}
289
290/**
291 * drm_client_buffer_vmap - Map DRM client buffer into address space
292 * @buffer: DRM client buffer
 
293 *
294 * This function maps a client buffer into kernel address space. If the
295 * buffer is already mapped, it returns the mapping's address.
296 *
297 * Client buffer mappings are not ref'counted. Each call to
298 * drm_client_buffer_vmap() should be followed by a call to
299 * drm_client_buffer_vunmap(); or the client buffer should be mapped
300 * throughout its lifetime.
301 *
 
 
 
 
302 * Returns:
303 *	The mapped memory's address
304 */
305void *drm_client_buffer_vmap(struct drm_client_buffer *buffer)
 
 
306{
307	void *vaddr;
308
309	if (buffer->vaddr)
310		return buffer->vaddr;
311
312	/*
313	 * FIXME: The dependency on GEM here isn't required, we could
314	 * convert the driver handle to a dma-buf instead and use the
315	 * backend-agnostic dma-buf vmap support instead. This would
316	 * require that the handle2fd prime ioctl is reworked to pull the
317	 * fd_install step out of the driver backend hooks, to make that
318	 * final step optional for internal users.
319	 */
320	vaddr = drm_gem_vmap(buffer->gem);
321	if (IS_ERR(vaddr))
322		return vaddr;
323
324	buffer->vaddr = vaddr;
325
326	return vaddr;
327}
328EXPORT_SYMBOL(drm_client_buffer_vmap);
329
330/**
331 * drm_client_buffer_vunmap - Unmap DRM client buffer
332 * @buffer: DRM client buffer
333 *
334 * This function removes a client buffer's memory mapping. Calling this
335 * function is only required by clients that manage their buffer mappings
336 * by themselves.
337 */
338void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
339{
340	drm_gem_vunmap(buffer->gem, buffer->vaddr);
341	buffer->vaddr = NULL;
 
342}
343EXPORT_SYMBOL(drm_client_buffer_vunmap);
344
345static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
346{
347	int ret;
348
349	if (!buffer->fb)
350		return;
351
352	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
353	if (ret)
354		drm_err(buffer->client->dev,
355			"Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
356
357	buffer->fb = NULL;
358}
359
360static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
361				   u32 width, u32 height, u32 format)
 
362{
363	struct drm_client_dev *client = buffer->client;
364	struct drm_mode_fb_cmd fb_req = { };
365	const struct drm_format_info *info;
366	int ret;
367
368	info = drm_format_info(format);
369	fb_req.bpp = info->cpp[0] * 8;
370	fb_req.depth = info->depth;
371	fb_req.width = width;
372	fb_req.height = height;
373	fb_req.handle = buffer->handle;
374	fb_req.pitch = buffer->pitch;
 
375
376	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
377	if (ret)
378		return ret;
379
380	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
381	if (WARN_ON(!buffer->fb))
382		return -ENOENT;
383
384	/* drop the reference we picked up in framebuffer lookup */
385	drm_framebuffer_put(buffer->fb);
386
387	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
388
389	return 0;
390}
391
392/**
393 * drm_client_framebuffer_create - Create a client framebuffer
394 * @client: DRM client
395 * @width: Framebuffer width
396 * @height: Framebuffer height
397 * @format: Buffer format
398 *
399 * This function creates a &drm_client_buffer which consists of a
400 * &drm_framebuffer backed by a dumb buffer.
401 * Call drm_client_framebuffer_delete() to free the buffer.
402 *
403 * Returns:
404 * Pointer to a client buffer or an error pointer on failure.
405 */
406struct drm_client_buffer *
407drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
408{
409	struct drm_client_buffer *buffer;
 
410	int ret;
411
412	buffer = drm_client_buffer_create(client, width, height, format);
 
413	if (IS_ERR(buffer))
414		return buffer;
415
416	ret = drm_client_buffer_addfb(buffer, width, height, format);
 
 
 
 
 
 
 
 
 
417	if (ret) {
418		drm_client_buffer_delete(buffer);
419		return ERR_PTR(ret);
420	}
421
422	return buffer;
423}
424EXPORT_SYMBOL(drm_client_framebuffer_create);
425
426/**
427 * drm_client_framebuffer_delete - Delete a client framebuffer
428 * @buffer: DRM client buffer (can be NULL)
429 */
430void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
431{
432	if (!buffer)
433		return;
434
435	drm_client_buffer_rmfb(buffer);
436	drm_client_buffer_delete(buffer);
437}
438EXPORT_SYMBOL(drm_client_framebuffer_delete);
439
440/**
441 * drm_client_framebuffer_flush - Manually flush client framebuffer
442 * @buffer: DRM client buffer (can be NULL)
443 * @rect: Damage rectangle (if NULL flushes all)
444 *
445 * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
446 * for drivers that need it.
447 *
448 * Returns:
449 * Zero on success or negative error code on failure.
450 */
451int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
452{
453	if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
454		return 0;
455
456	if (rect) {
457		struct drm_clip_rect clip = {
458			.x1 = rect->x1,
459			.y1 = rect->y1,
460			.x2 = rect->x2,
461			.y2 = rect->y2,
462		};
463
464		return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
465						0, 0, &clip, 1);
466	}
467
468	return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
469					0, 0, NULL, 0);
470}
471EXPORT_SYMBOL(drm_client_framebuffer_flush);
472
473#ifdef CONFIG_DEBUG_FS
474static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
475{
476	struct drm_info_node *node = m->private;
477	struct drm_device *dev = node->minor->dev;
478	struct drm_printer p = drm_seq_file_printer(m);
479	struct drm_client_dev *client;
480
481	mutex_lock(&dev->clientlist_mutex);
482	list_for_each_entry(client, &dev->clientlist, list)
483		drm_printf(&p, "%s\n", client->name);
484	mutex_unlock(&dev->clientlist_mutex);
485
486	return 0;
487}
488
489static const struct drm_info_list drm_client_debugfs_list[] = {
490	{ "internal_clients", drm_client_debugfs_internal_clients, 0 },
491};
492
493void drm_client_debugfs_init(struct drm_minor *minor)
494{
495	drm_debugfs_create_files(drm_client_debugfs_list,
496				 ARRAY_SIZE(drm_client_debugfs_list),
497				 minor->debugfs_root, minor);
498}
499#endif
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0 or MIT
  2/*
  3 * Copyright 2018 Noralf Trønnes
  4 */
  5
  6#include <linux/iosys-map.h>
  7#include <linux/list.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_framebuffer.h>
 19#include <drm/drm_gem.h>
 20#include <drm/drm_mode.h>
 21#include <drm/drm_print.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 *
 31 * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
 32 */
 33
 34static int drm_client_open(struct drm_client_dev *client)
 35{
 36	struct drm_device *dev = client->dev;
 37	struct drm_file *file;
 38
 39	file = drm_file_alloc(dev->primary);
 40	if (IS_ERR(file))
 41		return PTR_ERR(file);
 42
 43	mutex_lock(&dev->filelist_mutex);
 44	list_add(&file->lhead, &dev->filelist_internal);
 45	mutex_unlock(&dev->filelist_mutex);
 46
 47	client->file = file;
 48
 49	return 0;
 50}
 51
 52static void drm_client_close(struct drm_client_dev *client)
 53{
 54	struct drm_device *dev = client->dev;
 55
 56	mutex_lock(&dev->filelist_mutex);
 57	list_del(&client->file->lhead);
 58	mutex_unlock(&dev->filelist_mutex);
 59
 60	drm_file_free(client->file);
 61}
 62
 63/**
 64 * drm_client_init - Initialise a DRM client
 65 * @dev: DRM device
 66 * @client: DRM client
 67 * @name: Client name
 68 * @funcs: DRM client functions (optional)
 69 *
 70 * This initialises the client and opens a &drm_file.
 71 * Use drm_client_register() to complete the process.
 72 * The caller needs to hold a reference on @dev before calling this function.
 73 * The client is freed when the &drm_device is unregistered. See drm_client_release().
 74 *
 75 * Returns:
 76 * Zero on success or negative error code on failure.
 77 */
 78int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 79		    const char *name, const struct drm_client_funcs *funcs)
 80{
 81	int ret;
 82
 83	if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
 84		return -EOPNOTSUPP;
 85
 
 
 
 86	client->dev = dev;
 87	client->name = name;
 88	client->funcs = funcs;
 89
 90	ret = drm_client_modeset_create(client);
 91	if (ret)
 92		return ret;
 93
 94	ret = drm_client_open(client);
 95	if (ret)
 96		goto err_free;
 97
 98	drm_dev_get(dev);
 99
100	return 0;
101
102err_free:
103	drm_client_modeset_free(client);
 
 
 
 
104	return ret;
105}
106EXPORT_SYMBOL(drm_client_init);
107
108/**
109 * drm_client_register - Register client
110 * @client: DRM client
111 *
112 * Add the client to the &drm_device client list to activate its callbacks.
113 * @client must be initialized by a call to drm_client_init(). After
114 * drm_client_register() it is no longer permissible to call drm_client_release()
115 * directly (outside the unregister callback), instead cleanup will happen
116 * automatically on driver unload.
117 *
118 * Registering a client generates a hotplug event that allows the client
119 * to set up its display from pre-existing outputs. The client must have
120 * initialized its state to able to handle the hotplug event successfully.
121 */
122void drm_client_register(struct drm_client_dev *client)
123{
124	struct drm_device *dev = client->dev;
125	int ret;
126
127	mutex_lock(&dev->clientlist_mutex);
128	list_add(&client->list, &dev->clientlist);
129
130	if (client->funcs && client->funcs->hotplug) {
131		/*
132		 * Perform an initial hotplug event to pick up the
133		 * display configuration for the client. This step
134		 * has to be performed *after* registering the client
135		 * in the list of clients, or a concurrent hotplug
136		 * event might be lost; leaving the display off.
137		 *
138		 * Hold the clientlist_mutex as for a regular hotplug
139		 * event.
140		 */
141		ret = client->funcs->hotplug(client);
142		if (ret)
143			drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
144	}
145	mutex_unlock(&dev->clientlist_mutex);
146}
147EXPORT_SYMBOL(drm_client_register);
148
149/**
150 * drm_client_release - Release DRM client resources
151 * @client: DRM client
152 *
153 * Releases resources by closing the &drm_file that was opened by drm_client_init().
154 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
155 *
156 * This function should only be called from the unregister callback. An exception
157 * is fbdev which cannot free the buffer if userspace has open file descriptors.
158 *
159 * Note:
160 * Clients cannot initiate a release by themselves. This is done to keep the code simple.
161 * The driver has to be unloaded before the client can be unloaded.
162 */
163void drm_client_release(struct drm_client_dev *client)
164{
165	struct drm_device *dev = client->dev;
166
167	drm_dbg_kms(dev, "%s\n", client->name);
168
169	drm_client_modeset_free(client);
170	drm_client_close(client);
171	drm_dev_put(dev);
 
 
172}
173EXPORT_SYMBOL(drm_client_release);
174
175void drm_client_dev_unregister(struct drm_device *dev)
176{
177	struct drm_client_dev *client, *tmp;
178
179	if (!drm_core_check_feature(dev, DRIVER_MODESET))
180		return;
181
182	mutex_lock(&dev->clientlist_mutex);
183	list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
184		list_del(&client->list);
185		if (client->funcs && client->funcs->unregister) {
186			client->funcs->unregister(client);
187		} else {
188			drm_client_release(client);
189			kfree(client);
190		}
191	}
192	mutex_unlock(&dev->clientlist_mutex);
193}
194
195/**
196 * drm_client_dev_hotplug - Send hotplug event to clients
197 * @dev: DRM device
198 *
199 * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
200 *
201 * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
202 * don't need to call this function themselves.
203 */
204void drm_client_dev_hotplug(struct drm_device *dev)
205{
206	struct drm_client_dev *client;
207	int ret;
208
209	if (!drm_core_check_feature(dev, DRIVER_MODESET))
210		return;
211
212	if (!dev->mode_config.num_connector) {
213		drm_dbg_kms(dev, "No connectors found, will not send hotplug events!\n");
214		return;
215	}
216
217	mutex_lock(&dev->clientlist_mutex);
218	list_for_each_entry(client, &dev->clientlist, list) {
219		if (!client->funcs || !client->funcs->hotplug)
220			continue;
221
222		if (client->hotplug_failed)
223			continue;
224
225		ret = client->funcs->hotplug(client);
226		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
227		if (ret)
228			client->hotplug_failed = true;
229	}
230	mutex_unlock(&dev->clientlist_mutex);
231}
232EXPORT_SYMBOL(drm_client_dev_hotplug);
233
234void drm_client_dev_restore(struct drm_device *dev)
235{
236	struct drm_client_dev *client;
237	int ret;
238
239	if (!drm_core_check_feature(dev, DRIVER_MODESET))
240		return;
241
242	mutex_lock(&dev->clientlist_mutex);
243	list_for_each_entry(client, &dev->clientlist, list) {
244		if (!client->funcs || !client->funcs->restore)
245			continue;
246
247		ret = client->funcs->restore(client);
248		drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
249		if (!ret) /* The first one to return zero gets the privilege to restore */
250			break;
251	}
252	mutex_unlock(&dev->clientlist_mutex);
253}
254
255static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
256{
257	if (buffer->gem) {
258		drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
 
 
 
259		drm_gem_object_put(buffer->gem);
260	}
 
 
261
262	kfree(buffer);
263}
264
265static struct drm_client_buffer *
266drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
267			 u32 format, u32 *handle)
268{
269	const struct drm_format_info *info = drm_format_info(format);
270	struct drm_mode_create_dumb dumb_args = { };
271	struct drm_device *dev = client->dev;
272	struct drm_client_buffer *buffer;
273	struct drm_gem_object *obj;
274	int ret;
275
276	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
277	if (!buffer)
278		return ERR_PTR(-ENOMEM);
279
280	buffer->client = client;
281
282	dumb_args.width = width;
283	dumb_args.height = height;
284	dumb_args.bpp = drm_format_info_bpp(info, 0);
285	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
286	if (ret)
287		goto err_delete;
288
 
 
 
289	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
290	if (!obj)  {
291		ret = -ENOENT;
292		goto err_delete;
293	}
294
295	buffer->pitch = dumb_args.pitch;
296	buffer->gem = obj;
297	*handle = dumb_args.handle;
298
299	return buffer;
300
301err_delete:
302	drm_client_buffer_delete(buffer);
303
304	return ERR_PTR(ret);
305}
306
307/**
308 * drm_client_buffer_vmap - Map DRM client buffer into address space
309 * @buffer: DRM client buffer
310 * @map_copy: Returns the mapped memory's address
311 *
312 * This function maps a client buffer into kernel address space. If the
313 * buffer is already mapped, it returns the existing mapping's address.
314 *
315 * Client buffer mappings are not ref'counted. Each call to
316 * drm_client_buffer_vmap() should be followed by a call to
317 * drm_client_buffer_vunmap(); or the client buffer should be mapped
318 * throughout its lifetime.
319 *
320 * The returned address is a copy of the internal value. In contrast to
321 * other vmap interfaces, you don't need it for the client's vunmap
322 * function. So you can modify it at will during blit and draw operations.
323 *
324 * Returns:
325 *	0 on success, or a negative errno code otherwise.
326 */
327int
328drm_client_buffer_vmap(struct drm_client_buffer *buffer,
329		       struct iosys_map *map_copy)
330{
331	struct iosys_map *map = &buffer->map;
332	int ret;
 
 
333
334	/*
335	 * FIXME: The dependency on GEM here isn't required, we could
336	 * convert the driver handle to a dma-buf instead and use the
337	 * backend-agnostic dma-buf vmap support instead. This would
338	 * require that the handle2fd prime ioctl is reworked to pull the
339	 * fd_install step out of the driver backend hooks, to make that
340	 * final step optional for internal users.
341	 */
342	ret = drm_gem_vmap_unlocked(buffer->gem, map);
343	if (ret)
344		return ret;
345
346	*map_copy = *map;
347
348	return 0;
349}
350EXPORT_SYMBOL(drm_client_buffer_vmap);
351
352/**
353 * drm_client_buffer_vunmap - Unmap DRM client buffer
354 * @buffer: DRM client buffer
355 *
356 * This function removes a client buffer's memory mapping. Calling this
357 * function is only required by clients that manage their buffer mappings
358 * by themselves.
359 */
360void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
361{
362	struct iosys_map *map = &buffer->map;
363
364	drm_gem_vunmap_unlocked(buffer->gem, map);
365}
366EXPORT_SYMBOL(drm_client_buffer_vunmap);
367
368static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
369{
370	int ret;
371
372	if (!buffer->fb)
373		return;
374
375	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
376	if (ret)
377		drm_err(buffer->client->dev,
378			"Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
379
380	buffer->fb = NULL;
381}
382
383static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
384				   u32 width, u32 height, u32 format,
385				   u32 handle)
386{
387	struct drm_client_dev *client = buffer->client;
388	struct drm_mode_fb_cmd2 fb_req = { };
 
389	int ret;
390
 
 
 
391	fb_req.width = width;
392	fb_req.height = height;
393	fb_req.pixel_format = format;
394	fb_req.handles[0] = handle;
395	fb_req.pitches[0] = buffer->pitch;
396
397	ret = drm_mode_addfb2(client->dev, &fb_req, client->file);
398	if (ret)
399		return ret;
400
401	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
402	if (WARN_ON(!buffer->fb))
403		return -ENOENT;
404
405	/* drop the reference we picked up in framebuffer lookup */
406	drm_framebuffer_put(buffer->fb);
407
408	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
409
410	return 0;
411}
412
413/**
414 * drm_client_framebuffer_create - Create a client framebuffer
415 * @client: DRM client
416 * @width: Framebuffer width
417 * @height: Framebuffer height
418 * @format: Buffer format
419 *
420 * This function creates a &drm_client_buffer which consists of a
421 * &drm_framebuffer backed by a dumb buffer.
422 * Call drm_client_framebuffer_delete() to free the buffer.
423 *
424 * Returns:
425 * Pointer to a client buffer or an error pointer on failure.
426 */
427struct drm_client_buffer *
428drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
429{
430	struct drm_client_buffer *buffer;
431	u32 handle;
432	int ret;
433
434	buffer = drm_client_buffer_create(client, width, height, format,
435					  &handle);
436	if (IS_ERR(buffer))
437		return buffer;
438
439	ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
440
441	/*
442	 * The handle is only needed for creating the framebuffer, destroy it
443	 * again to solve a circular dependency should anybody export the GEM
444	 * object as DMA-buf. The framebuffer and our buffer structure are still
445	 * holding references to the GEM object to prevent its destruction.
446	 */
447	drm_mode_destroy_dumb(client->dev, handle, client->file);
448
449	if (ret) {
450		drm_client_buffer_delete(buffer);
451		return ERR_PTR(ret);
452	}
453
454	return buffer;
455}
456EXPORT_SYMBOL(drm_client_framebuffer_create);
457
458/**
459 * drm_client_framebuffer_delete - Delete a client framebuffer
460 * @buffer: DRM client buffer (can be NULL)
461 */
462void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
463{
464	if (!buffer)
465		return;
466
467	drm_client_buffer_rmfb(buffer);
468	drm_client_buffer_delete(buffer);
469}
470EXPORT_SYMBOL(drm_client_framebuffer_delete);
471
472/**
473 * drm_client_framebuffer_flush - Manually flush client framebuffer
474 * @buffer: DRM client buffer (can be NULL)
475 * @rect: Damage rectangle (if NULL flushes all)
476 *
477 * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
478 * for drivers that need it.
479 *
480 * Returns:
481 * Zero on success or negative error code on failure.
482 */
483int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
484{
485	if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
486		return 0;
487
488	if (rect) {
489		struct drm_clip_rect clip = {
490			.x1 = rect->x1,
491			.y1 = rect->y1,
492			.x2 = rect->x2,
493			.y2 = rect->y2,
494		};
495
496		return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
497						0, 0, &clip, 1);
498	}
499
500	return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
501					0, 0, NULL, 0);
502}
503EXPORT_SYMBOL(drm_client_framebuffer_flush);
504
505#ifdef CONFIG_DEBUG_FS
506static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
507{
508	struct drm_debugfs_entry *entry = m->private;
509	struct drm_device *dev = entry->dev;
510	struct drm_printer p = drm_seq_file_printer(m);
511	struct drm_client_dev *client;
512
513	mutex_lock(&dev->clientlist_mutex);
514	list_for_each_entry(client, &dev->clientlist, list)
515		drm_printf(&p, "%s\n", client->name);
516	mutex_unlock(&dev->clientlist_mutex);
517
518	return 0;
519}
520
521static const struct drm_debugfs_info drm_client_debugfs_list[] = {
522	{ "internal_clients", drm_client_debugfs_internal_clients, 0 },
523};
524
525void drm_client_debugfs_init(struct drm_device *dev)
526{
527	drm_debugfs_add_files(dev, drm_client_debugfs_list,
528			      ARRAY_SIZE(drm_client_debugfs_list));
 
529}
530#endif