xref: /linux/drivers/gpu/drm/drm_client.c (revision face6a3615a649456eb4549f6d474221d877d604)
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_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 
34 static 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 
52 static 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  */
78 int 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 
102 err_free:
103 	drm_client_modeset_free(client);
104 	return ret;
105 }
106 EXPORT_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  */
122 void 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 }
147 EXPORT_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  */
163 void 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 
172 	if (client->funcs && client->funcs->free)
173 		client->funcs->free(client);
174 
175 	drm_dev_put(dev);
176 }
177 EXPORT_SYMBOL(drm_client_release);
178 
179 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
180 {
181 	if (buffer->gem) {
182 		drm_gem_vunmap(buffer->gem, &buffer->map);
183 		drm_gem_object_put(buffer->gem);
184 	}
185 
186 	kfree(buffer);
187 }
188 
189 static struct drm_client_buffer *
190 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
191 			 u32 format, u32 *handle)
192 {
193 	const struct drm_format_info *info = drm_format_info(format);
194 	struct drm_mode_create_dumb dumb_args = { };
195 	struct drm_device *dev = client->dev;
196 	struct drm_client_buffer *buffer;
197 	struct drm_gem_object *obj;
198 	int ret;
199 
200 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
201 	if (!buffer)
202 		return ERR_PTR(-ENOMEM);
203 
204 	buffer->client = client;
205 
206 	dumb_args.width = width;
207 	dumb_args.height = height;
208 	dumb_args.bpp = drm_format_info_bpp(info, 0);
209 	ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
210 	if (ret)
211 		goto err_delete;
212 
213 	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
214 	if (!obj)  {
215 		ret = -ENOENT;
216 		goto err_delete;
217 	}
218 
219 	buffer->pitch = dumb_args.pitch;
220 	buffer->gem = obj;
221 	*handle = dumb_args.handle;
222 
223 	return buffer;
224 
225 err_delete:
226 	drm_client_buffer_delete(buffer);
227 
228 	return ERR_PTR(ret);
229 }
230 
231 /**
232  * drm_client_buffer_vmap_local - Map DRM client buffer into address space
233  * @buffer: DRM client buffer
234  * @map_copy: Returns the mapped memory's address
235  *
236  * This function maps a client buffer into kernel address space. If the
237  * buffer is already mapped, it returns the existing mapping's address.
238  *
239  * Client buffer mappings are not ref'counted. Each call to
240  * drm_client_buffer_vmap_local() should be closely followed by a call to
241  * drm_client_buffer_vunmap_local(). See drm_client_buffer_vmap() for
242  * long-term mappings.
243  *
244  * The returned address is a copy of the internal value. In contrast to
245  * other vmap interfaces, you don't need it for the client's vunmap
246  * function. So you can modify it at will during blit and draw operations.
247  *
248  * Returns:
249  *	0 on success, or a negative errno code otherwise.
250  */
251 int drm_client_buffer_vmap_local(struct drm_client_buffer *buffer,
252 				 struct iosys_map *map_copy)
253 {
254 	struct drm_gem_object *gem = buffer->gem;
255 	struct iosys_map *map = &buffer->map;
256 	int ret;
257 
258 	drm_gem_lock(gem);
259 
260 	ret = drm_gem_vmap_locked(gem, map);
261 	if (ret)
262 		goto err_drm_gem_vmap_unlocked;
263 	*map_copy = *map;
264 
265 	return 0;
266 
267 err_drm_gem_vmap_unlocked:
268 	drm_gem_unlock(gem);
269 	return ret;
270 }
271 EXPORT_SYMBOL(drm_client_buffer_vmap_local);
272 
273 /**
274  * drm_client_buffer_vunmap_local - Unmap DRM client buffer
275  * @buffer: DRM client buffer
276  *
277  * This function removes a client buffer's memory mapping established
278  * with drm_client_buffer_vunmap_local(). Calling this function is only
279  * required by clients that manage their buffer mappings by themselves.
280  */
281 void drm_client_buffer_vunmap_local(struct drm_client_buffer *buffer)
282 {
283 	struct drm_gem_object *gem = buffer->gem;
284 	struct iosys_map *map = &buffer->map;
285 
286 	drm_gem_vunmap_locked(gem, map);
287 	drm_gem_unlock(gem);
288 }
289 EXPORT_SYMBOL(drm_client_buffer_vunmap_local);
290 
291 /**
292  * drm_client_buffer_vmap - Map DRM client buffer into address space
293  * @buffer: DRM client buffer
294  * @map_copy: Returns the mapped memory's address
295  *
296  * This function maps a client buffer into kernel address space. If the
297  * buffer is already mapped, it returns the existing mapping's address.
298  *
299  * Client buffer mappings are not ref'counted. Each call to
300  * drm_client_buffer_vmap() should be followed by a call to
301  * drm_client_buffer_vunmap(); or the client buffer should be mapped
302  * throughout its lifetime.
303  *
304  * The returned address is a copy of the internal value. In contrast to
305  * other vmap interfaces, you don't need it for the client's vunmap
306  * function. So you can modify it at will during blit and draw operations.
307  *
308  * Returns:
309  *	0 on success, or a negative errno code otherwise.
310  */
311 int drm_client_buffer_vmap(struct drm_client_buffer *buffer,
312 			   struct iosys_map *map_copy)
313 {
314 	int ret;
315 
316 	ret = drm_gem_vmap(buffer->gem, &buffer->map);
317 	if (ret)
318 		return ret;
319 	*map_copy = buffer->map;
320 
321 	return 0;
322 }
323 EXPORT_SYMBOL(drm_client_buffer_vmap);
324 
325 /**
326  * drm_client_buffer_vunmap - Unmap DRM client buffer
327  * @buffer: DRM client buffer
328  *
329  * This function removes a client buffer's memory mapping. Calling this
330  * function is only required by clients that manage their buffer mappings
331  * by themselves.
332  */
333 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
334 {
335 	drm_gem_vunmap(buffer->gem, &buffer->map);
336 }
337 EXPORT_SYMBOL(drm_client_buffer_vunmap);
338 
339 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
340 {
341 	int ret;
342 
343 	if (!buffer->fb)
344 		return;
345 
346 	ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
347 	if (ret)
348 		drm_err(buffer->client->dev,
349 			"Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
350 
351 	buffer->fb = NULL;
352 }
353 
354 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
355 				   u32 width, u32 height, u32 format,
356 				   u32 handle)
357 {
358 	struct drm_client_dev *client = buffer->client;
359 	struct drm_mode_fb_cmd2 fb_req = { };
360 	int ret;
361 
362 	fb_req.width = width;
363 	fb_req.height = height;
364 	fb_req.pixel_format = format;
365 	fb_req.handles[0] = handle;
366 	fb_req.pitches[0] = buffer->pitch;
367 
368 	ret = drm_mode_addfb2(client->dev, &fb_req, client->file);
369 	if (ret)
370 		return ret;
371 
372 	buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
373 	if (WARN_ON(!buffer->fb))
374 		return -ENOENT;
375 
376 	/* drop the reference we picked up in framebuffer lookup */
377 	drm_framebuffer_put(buffer->fb);
378 
379 	strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
380 
381 	return 0;
382 }
383 
384 /**
385  * drm_client_framebuffer_create - Create a client framebuffer
386  * @client: DRM client
387  * @width: Framebuffer width
388  * @height: Framebuffer height
389  * @format: Buffer format
390  *
391  * This function creates a &drm_client_buffer which consists of a
392  * &drm_framebuffer backed by a dumb buffer.
393  * Call drm_client_framebuffer_delete() to free the buffer.
394  *
395  * Returns:
396  * Pointer to a client buffer or an error pointer on failure.
397  */
398 struct drm_client_buffer *
399 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
400 {
401 	struct drm_client_buffer *buffer;
402 	u32 handle;
403 	int ret;
404 
405 	buffer = drm_client_buffer_create(client, width, height, format,
406 					  &handle);
407 	if (IS_ERR(buffer))
408 		return buffer;
409 
410 	ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
411 
412 	/*
413 	 * The handle is only needed for creating the framebuffer, destroy it
414 	 * again to solve a circular dependency should anybody export the GEM
415 	 * object as DMA-buf. The framebuffer and our buffer structure are still
416 	 * holding references to the GEM object to prevent its destruction.
417 	 */
418 	drm_mode_destroy_dumb(client->dev, handle, client->file);
419 
420 	if (ret) {
421 		drm_client_buffer_delete(buffer);
422 		return ERR_PTR(ret);
423 	}
424 
425 	return buffer;
426 }
427 EXPORT_SYMBOL(drm_client_framebuffer_create);
428 
429 /**
430  * drm_client_framebuffer_delete - Delete a client framebuffer
431  * @buffer: DRM client buffer (can be NULL)
432  */
433 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
434 {
435 	if (!buffer)
436 		return;
437 
438 	drm_client_buffer_rmfb(buffer);
439 	drm_client_buffer_delete(buffer);
440 }
441 EXPORT_SYMBOL(drm_client_framebuffer_delete);
442 
443 /**
444  * drm_client_framebuffer_flush - Manually flush client framebuffer
445  * @buffer: DRM client buffer (can be NULL)
446  * @rect: Damage rectangle (if NULL flushes all)
447  *
448  * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
449  * for drivers that need it.
450  *
451  * Returns:
452  * Zero on success or negative error code on failure.
453  */
454 int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
455 {
456 	if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
457 		return 0;
458 
459 	if (rect) {
460 		struct drm_clip_rect clip = {
461 			.x1 = rect->x1,
462 			.y1 = rect->y1,
463 			.x2 = rect->x2,
464 			.y2 = rect->y2,
465 		};
466 
467 		return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
468 						0, 0, &clip, 1);
469 	}
470 
471 	return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
472 					0, 0, NULL, 0);
473 }
474 EXPORT_SYMBOL(drm_client_framebuffer_flush);
475