File Index
Files and directories
Sign in
FILE

drm_client.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/drm_client.c C
  1// SPDX-License-Identifier: GPL-2.0 or MIT
  2/*
  3 * Copyright 2018 Noralf Trønnes
  4 */
  5
  6#include <linux/export.h>
  7#include <linux/iosys-map.h>
  8#include <linux/list.h>
  9#include <linux/mutex.h>
 10#include <linux/seq_file.h>
 11#include <linux/slab.h>
 12
 13#include <drm/drm_client.h>
 14#include <drm/drm_client_event.h>
 15#include <drm/drm_device.h>
 16#include <drm/drm_drv.h>
 17#include <drm/drm_file.h>
 18#include <drm/drm_fourcc.h>
 19#include <drm/drm_framebuffer.h>
 20#include <drm/drm_gem.h>
 21#include <drm/drm_gem_framebuffer_helper.h>
 22#include <drm/drm_mode.h>
 23#include <drm/drm_print.h>
 24
 25#include "drm_crtc_internal.h"
 26#include "drm_internal.h"
 27
 28/**
 29 * DOC: overview
 30 *
 31 * This library provides support for clients running in the kernel like fbdev and bootsplash.
 32 *
 33 * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
 34 */
 35
 36static int drm_client_open(struct drm_client_dev *client)
 37{
 38	struct drm_device *dev = client->dev;
 39	struct drm_file *file;
 40
 41	file = drm_file_alloc(dev->primary);
 42	if (IS_ERR(file))
 43		return PTR_ERR(file);
 44
 45	mutex_lock(&dev->filelist_mutex);
 46	list_add(&file->lhead, &dev->filelist_internal);
 47	mutex_unlock(&dev->filelist_mutex);
 48
 49	client->file = file;
 50
 51	return 0;
 52}
 53
 54static void drm_client_close(struct drm_client_dev *client)
 55{
 56	struct drm_device *dev = client->dev;
 57
 58	mutex_lock(&dev->filelist_mutex);
 59	list_del(&client->file->lhead);
 60	mutex_unlock(&dev->filelist_mutex);
 61
 62	drm_file_free(client->file);
 63}
 64
 65/**
 66 * drm_client_init - Initialise a DRM client
 67 * @dev: DRM device
 68 * @client: DRM client
 69 * @name: Client name
 70 * @funcs: DRM client functions (optional)
 71 *
 72 * This initialises the client and opens a &drm_file.
 73 * Use drm_client_register() to complete the process.
 74 * The caller needs to hold a reference on @dev before calling this function.
 75 * The client is freed when the &drm_device is unregistered. See drm_client_release().
 76 *
 77 * Returns:
 78 * Zero on success or negative error code on failure.
 79 */
 80int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 81		    const char *name, const struct drm_client_funcs *funcs)
 82{
 83	int ret;
 84
 85	if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
 86		return -EOPNOTSUPP;
 87
 88	client->dev = dev;
 89	client->name = name;
 90	client->funcs = funcs;
 91
 92	ret = drm_client_modeset_create(client);
 93	if (ret)
 94		return ret;
 95
 96	ret = drm_client_open(client);
 97	if (ret)
 98		goto err_free;
 99
100	drm_dev_get(dev);
101
102	return 0;
103
104err_free:
105	drm_client_modeset_free(client);
106	return ret;
107}
108EXPORT_SYMBOL(drm_client_init);
109
110/**
111 * drm_client_register - Register client
112 * @client: DRM client
113 *
114 * Add the client to the &drm_device client list to activate its callbacks.
115 * @client must be initialized by a call to drm_client_init(). After
116 * drm_client_register() it is no longer permissible to call drm_client_release()
117 * directly (outside the unregister callback), instead cleanup will happen
118 * automatically on driver unload.
119 *
120 * Registering a client generates a hotplug event that allows the client
121 * to set up its display from pre-existing outputs. The client must have
122 * initialized its state to able to handle the hotplug event successfully.
123 */
124void drm_client_register(struct drm_client_dev *client)
125{
126	struct drm_device *dev = client->dev;
127	int ret;
128
129	mutex_lock(&dev->clientlist_mutex);
130	list_add(&client->list, &dev->clientlist);
131
132	if (client->funcs && client->funcs->hotplug) {
133		/*
134		 * Perform an initial hotplug event to pick up the
135		 * display configuration for the client. This step
136		 * has to be performed *after* registering the client
137		 * in the list of clients, or a concurrent hotplug
138		 * event might be lost; leaving the display off.
139		 *
140		 * Hold the clientlist_mutex as for a regular hotplug
141		 * event.
142		 */
143		ret = client->funcs->hotplug(client);
144		if (ret)
145			drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
146	}
147	mutex_unlock(&dev->clientlist_mutex);
148}
149EXPORT_SYMBOL(drm_client_register);
150
151/**
152 * drm_client_release - Release DRM client resources
153 * @client: DRM client
154 *
155 * Releases resources by closing the &drm_file that was opened by drm_client_init().
156 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
157 *
158 * This function should only be called from the unregister callback. An exception
159 * is fbdev which cannot free the buffer if userspace has open file descriptors.
160 *
161 * Note:
162 * Clients cannot initiate a release by themselves. This is done to keep the code simple.
163 * The driver has to be unloaded before the client can be unloaded.
164 */
165void drm_client_release(struct drm_client_dev *client)
166{
167	struct drm_device *dev = client->dev;
168
169	drm_dbg_kms(dev, "%s\n", client->name);
170
171	drm_client_modeset_free(client);
172	drm_client_close(client);
173
174	if (client->funcs && client->funcs->free)
175		client->funcs->free(client);
176
177	drm_dev_put(dev);
178}
179EXPORT_SYMBOL(drm_client_release);
180
181/**
182 * drm_client_buffer_delete - Delete a client buffer
183 * @buffer: DRM client buffer
184 */
185void drm_client_buffer_delete(struct drm_client_buffer *buffer)
186{
187	struct drm_gem_object *gem;
188	int ret;
189
190	if (!buffer)
191		return;
192
193	gem = buffer->fb->obj[0];
194	drm_gem_vunmap(gem, &buffer->map);
195
196	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
197	if (ret)
198		drm_err(buffer->client->dev,
199			"Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
200
201	drm_gem_object_put(buffer->gem);
202
203	kfree(buffer);
204}
205EXPORT_SYMBOL(drm_client_buffer_delete);
206
207static struct drm_client_buffer *
208drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
209			 u32 format, u32 handle, u32 pitch)
210{
211	struct drm_mode_fb_cmd2 fb_req = {
212		.width = width,
213		.height = height,
214		.pixel_format = format,
215		.handles = {
216			handle,
217		},
218		.pitches = {
219			pitch,
220		},
221	};
222	struct drm_device *dev = client->dev;
223	struct drm_client_buffer *buffer;
224	struct drm_gem_object *obj;
225	struct drm_framebuffer *fb;
226	int ret;
227
228	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
229	if (!buffer)
230		return ERR_PTR(-ENOMEM);
231
232	buffer->client = client;
233
234	obj = drm_gem_object_lookup(client->file, handle);
235	if (!obj)  {
236		ret = -ENOENT;
237		goto err_delete;
238	}
239
240	ret = drm_mode_addfb2(dev, &fb_req, client->file);
241	if (ret)
242		goto err_drm_gem_object_put;
243
244	fb = drm_framebuffer_lookup(dev, client->file, fb_req.fb_id);
245	if (drm_WARN_ON(dev, !fb)) {
246		ret = -ENOENT;
247		goto err_drm_mode_rmfb;
248	}
249
250	/* drop the reference we picked up in framebuffer lookup */
251	drm_framebuffer_put(fb);
252
253	strscpy(fb->comm, client->name, TASK_COMM_LEN);
254
255	buffer->gem = obj;
256	buffer->fb = fb;
257
258	return buffer;
259
260err_drm_mode_rmfb:
261	drm_mode_rmfb(dev, fb_req.fb_id, client->file);
262err_drm_gem_object_put:
263	drm_gem_object_put(obj);
264err_delete:
265	kfree(buffer);
266	return ERR_PTR(ret);
267}
268
269/**
270 * drm_client_buffer_vmap_local - Map DRM client buffer into address space
271 * @buffer: DRM client buffer
272 * @map_copy: Returns the mapped memory's address
273 *
274 * This function maps a client buffer into kernel address space. If the
275 * buffer is already mapped, it returns the existing mapping's address.
276 *
277 * Client buffer mappings are not ref'counted. Each call to
278 * drm_client_buffer_vmap_local() should be closely followed by a call to
279 * drm_client_buffer_vunmap_local(). See drm_client_buffer_vmap() for
280 * long-term mappings.
281 *
282 * The returned address is a copy of the internal value. In contrast to
283 * other vmap interfaces, you don't need it for the client's vunmap
284 * function. So you can modify it at will during blit and draw operations.
285 *
286 * Returns:
287 *	0 on success, or a negative errno code otherwise.
288 */
289int drm_client_buffer_vmap_local(struct drm_client_buffer *buffer,
290				 struct iosys_map *map_copy)
291{
292	struct drm_gem_object *gem = buffer->fb->obj[0];
293	struct iosys_map *map = &buffer->map;
294	int ret;
295
296	drm_gem_lock(gem);
297
298	ret = drm_gem_vmap_locked(gem, map);
299	if (ret)
300		goto err_drm_gem_vmap_unlocked;
301	*map_copy = *map;
302
303	return 0;
304
305err_drm_gem_vmap_unlocked:
306	drm_gem_unlock(gem);
307	return ret;
308}
309EXPORT_SYMBOL(drm_client_buffer_vmap_local);
310
311/**
312 * drm_client_buffer_vunmap_local - Unmap DRM client buffer
313 * @buffer: DRM client buffer
314 *
315 * This function removes a client buffer's memory mapping established
316 * with drm_client_buffer_vunmap_local(). Calling this function is only
317 * required by clients that manage their buffer mappings by themselves.
318 */
319void drm_client_buffer_vunmap_local(struct drm_client_buffer *buffer)
320{
321	struct drm_gem_object *gem = buffer->fb->obj[0];
322	struct iosys_map *map = &buffer->map;
323
324	drm_gem_vunmap_locked(gem, map);
325	drm_gem_unlock(gem);
326}
327EXPORT_SYMBOL(drm_client_buffer_vunmap_local);
328
329/**
330 * drm_client_buffer_vmap - Map DRM client buffer into address space
331 * @buffer: DRM client buffer
332 * @map_copy: Returns the mapped memory's address
333 *
334 * This function maps a client buffer into kernel address space. If the
335 * buffer is already mapped, it returns the existing mapping's address.
336 *
337 * Client buffer mappings are not ref'counted. Each call to
338 * drm_client_buffer_vmap() should be followed by a call to
339 * drm_client_buffer_vunmap(); or the client buffer should be mapped
340 * throughout its lifetime.
341 *
342 * The returned address is a copy of the internal value. In contrast to
343 * other vmap interfaces, you don't need it for the client's vunmap
344 * function. So you can modify it at will during blit and draw operations.
345 *
346 * Returns:
347 *	0 on success, or a negative errno code otherwise.
348 */
349int drm_client_buffer_vmap(struct drm_client_buffer *buffer,
350			   struct iosys_map *map_copy)
351{
352	struct drm_gem_object *gem = buffer->fb->obj[0];
353	int ret;
354
355	ret = drm_gem_vmap(gem, &buffer->map);
356	if (ret)
357		return ret;
358	*map_copy = buffer->map;
359
360	return 0;
361}
362EXPORT_SYMBOL(drm_client_buffer_vmap);
363
364/**
365 * drm_client_buffer_vunmap - Unmap DRM client buffer
366 * @buffer: DRM client buffer
367 *
368 * This function removes a client buffer's memory mapping. Calling this
369 * function is only required by clients that manage their buffer mappings
370 * by themselves.
371 */
372void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
373{
374	struct drm_gem_object *gem = buffer->fb->obj[0];
375
376	drm_gem_vunmap(gem, &buffer->map);
377}
378EXPORT_SYMBOL(drm_client_buffer_vunmap);
379
380/**
381 * drm_client_buffer_create_dumb - Create a client buffer backed by a dumb buffer
382 * @client: DRM client
383 * @width: Framebuffer width
384 * @height: Framebuffer height
385 * @format: Buffer format
386 *
387 * This function creates a &drm_client_buffer which consists of a
388 * &drm_framebuffer backed by a dumb buffer.
389 * Call drm_client_buffer_delete() to free the buffer.
390 *
391 * Returns:
392 * Pointer to a client buffer or an error pointer on failure.
393 */
394struct drm_client_buffer *
395drm_client_buffer_create_dumb(struct drm_client_dev *client, u32 width, u32 height, u32 format)
396{
397	const struct drm_format_info *info = drm_format_info(format);
398	struct drm_device *dev = client->dev;
399	struct drm_mode_create_dumb dumb_args = { };
400	struct drm_client_buffer *buffer;
401	int ret;
402
403	dumb_args.width = width;
404	dumb_args.height = height;
405	dumb_args.bpp = drm_format_info_bpp(info, 0);
406	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
407	if (ret)
408		return ERR_PTR(ret);
409
410	buffer = drm_client_buffer_create(client, width, height, format,
411					  dumb_args.handle, dumb_args.pitch);
412	if (IS_ERR(buffer)) {
413		ret = PTR_ERR(buffer);
414		goto err_drm_mode_destroy_dumb;
415	}
416
417	/*
418	 * The handle is only needed for creating the framebuffer, destroy it
419	 * again to solve a circular dependency should anybody export the GEM
420	 * object as DMA-buf. The framebuffer and our buffer structure are still
421	 * holding references to the GEM object to prevent its destruction.
422	 */
423	drm_mode_destroy_dumb(client->dev, dumb_args.handle, client->file);
424
425	return buffer;
426
427err_drm_mode_destroy_dumb:
428	drm_mode_destroy_dumb(client->dev, dumb_args.handle, client->file);
429	return ERR_PTR(ret);
430}
431EXPORT_SYMBOL(drm_client_buffer_create_dumb);
432
433/**
434 * drm_client_buffer_flush - Manually flush client buffer
435 * @buffer: DRM client buffer
436 * @rect: Damage rectangle (if NULL flushes all)
437 *
438 * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
439 * for drivers that need it.
440 *
441 * Returns:
442 * Zero on success or negative error code on failure.
443 */
444int drm_client_buffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
445{
446	if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
447		return 0;
448
449	if (rect) {
450		struct drm_clip_rect clip = {
451			.x1 = rect->x1,
452			.y1 = rect->y1,
453			.x2 = rect->x2,
454			.y2 = rect->y2,
455		};
456
457		return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
458						0, 0, &clip, 1);
459	}
460
461	return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
462					0, 0, NULL, 0);
463}
464EXPORT_SYMBOL(drm_client_buffer_flush);
~/ Read-only file preview.