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