xref: /openbsd/sys/dev/pci/drm/drm_prime.c (revision 154bfca8)
1 /*
2  * Copyright © 2012 Red Hat
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Dave Airlie <airlied@redhat.com>
25  *      Rob Clark <rob.clark@linaro.org>
26  *
27  */
28 
29 #include <linux/export.h>
30 #include <linux/dma-buf.h>
31 #include <linux/rbtree.h>
32 #include <linux/module.h>
33 
34 #include <drm/drm.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_file.h>
37 #include <drm/drm_framebuffer.h>
38 #include <drm/drm_gem.h>
39 #include <drm/drm_prime.h>
40 
41 #include "drm_internal.h"
42 
43 MODULE_IMPORT_NS(DMA_BUF);
44 
45 /**
46  * DOC: overview and lifetime rules
47  *
48  * Similar to GEM global names, PRIME file descriptors are also used to share
49  * buffer objects across processes. They offer additional security: as file
50  * descriptors must be explicitly sent over UNIX domain sockets to be shared
51  * between applications, they can't be guessed like the globally unique GEM
52  * names.
53  *
54  * Drivers that support the PRIME API implement the drm_gem_object_funcs.export
55  * and &drm_driver.gem_prime_import hooks. &dma_buf_ops implementations for
56  * drivers are all individually exported for drivers which need to overwrite
57  * or reimplement some of them.
58  *
59  * Reference Counting for GEM Drivers
60  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61  *
62  * On the export the &dma_buf holds a reference to the exported buffer object,
63  * usually a &drm_gem_object. It takes this reference in the PRIME_HANDLE_TO_FD
64  * IOCTL, when it first calls &drm_gem_object_funcs.export
65  * and stores the exporting GEM object in the &dma_buf.priv field. This
66  * reference needs to be released when the final reference to the &dma_buf
67  * itself is dropped and its &dma_buf_ops.release function is called.  For
68  * GEM-based drivers, the &dma_buf should be exported using
69  * drm_gem_dmabuf_export() and then released by drm_gem_dmabuf_release().
70  *
71  * Thus the chain of references always flows in one direction, avoiding loops:
72  * importing GEM object -> dma-buf -> exported GEM bo. A further complication
73  * are the lookup caches for import and export. These are required to guarantee
74  * that any given object will always have only one unique userspace handle. This
75  * is required to allow userspace to detect duplicated imports, since some GEM
76  * drivers do fail command submissions if a given buffer object is listed more
77  * than once. These import and export caches in &drm_prime_file_private only
78  * retain a weak reference, which is cleaned up when the corresponding object is
79  * released.
80  *
81  * Self-importing: If userspace is using PRIME as a replacement for flink then
82  * it will get a fd->handle request for a GEM object that it created.  Drivers
83  * should detect this situation and return back the underlying object from the
84  * dma-buf private. For GEM based drivers this is handled in
85  * drm_gem_prime_import() already.
86  */
87 
88 struct drm_prime_member {
89 	struct dma_buf *dma_buf;
90 	uint32_t handle;
91 
92 	struct rb_node dmabuf_rb;
93 	struct rb_node handle_rb;
94 };
95 
drm_prime_add_buf_handle(struct drm_prime_file_private * prime_fpriv,struct dma_buf * dma_buf,uint32_t handle)96 static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
97 				    struct dma_buf *dma_buf, uint32_t handle)
98 {
99 	struct drm_prime_member *member;
100 	struct rb_node **p, *rb;
101 
102 	member = kmalloc(sizeof(*member), GFP_KERNEL);
103 	if (!member)
104 		return -ENOMEM;
105 
106 	get_dma_buf(dma_buf);
107 	member->dma_buf = dma_buf;
108 	member->handle = handle;
109 
110 	rb = NULL;
111 	p = &prime_fpriv->dmabufs.rb_node;
112 	while (*p) {
113 		struct drm_prime_member *pos;
114 
115 		rb = *p;
116 		pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
117 		if (dma_buf > pos->dma_buf)
118 			p = &rb->rb_right;
119 		else
120 			p = &rb->rb_left;
121 	}
122 	rb_link_node(&member->dmabuf_rb, rb, p);
123 	rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
124 
125 	rb = NULL;
126 	p = &prime_fpriv->handles.rb_node;
127 	while (*p) {
128 		struct drm_prime_member *pos;
129 
130 		rb = *p;
131 		pos = rb_entry(rb, struct drm_prime_member, handle_rb);
132 		if (handle > pos->handle)
133 			p = &rb->rb_right;
134 		else
135 			p = &rb->rb_left;
136 	}
137 	rb_link_node(&member->handle_rb, rb, p);
138 	rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
139 
140 	return 0;
141 }
142 
drm_prime_lookup_buf_by_handle(struct drm_prime_file_private * prime_fpriv,uint32_t handle)143 static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
144 						      uint32_t handle)
145 {
146 	struct rb_node *rb;
147 
148 	rb = prime_fpriv->handles.rb_node;
149 	while (rb) {
150 		struct drm_prime_member *member;
151 
152 		member = rb_entry(rb, struct drm_prime_member, handle_rb);
153 		if (member->handle == handle)
154 			return member->dma_buf;
155 		else if (member->handle < handle)
156 			rb = rb->rb_right;
157 		else
158 			rb = rb->rb_left;
159 	}
160 
161 	return NULL;
162 }
163 
drm_prime_lookup_buf_handle(struct drm_prime_file_private * prime_fpriv,struct dma_buf * dma_buf,uint32_t * handle)164 static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
165 				       struct dma_buf *dma_buf,
166 				       uint32_t *handle)
167 {
168 	struct rb_node *rb;
169 
170 	rb = prime_fpriv->dmabufs.rb_node;
171 	while (rb) {
172 		struct drm_prime_member *member;
173 
174 		member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
175 		if (member->dma_buf == dma_buf) {
176 			*handle = member->handle;
177 			return 0;
178 		} else if (member->dma_buf < dma_buf) {
179 			rb = rb->rb_right;
180 		} else {
181 			rb = rb->rb_left;
182 		}
183 	}
184 
185 	return -ENOENT;
186 }
187 
drm_prime_remove_buf_handle(struct drm_prime_file_private * prime_fpriv,uint32_t handle)188 void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
189 				 uint32_t handle)
190 {
191 	struct rb_node *rb;
192 
193 	mutex_lock(&prime_fpriv->lock);
194 
195 	rb = prime_fpriv->handles.rb_node;
196 	while (rb) {
197 		struct drm_prime_member *member;
198 
199 		member = rb_entry(rb, struct drm_prime_member, handle_rb);
200 		if (member->handle == handle) {
201 			rb_erase(&member->handle_rb, &prime_fpriv->handles);
202 			rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
203 
204 			dma_buf_put(member->dma_buf);
205 			kfree(member);
206 			break;
207 		} else if (member->handle < handle) {
208 			rb = rb->rb_right;
209 		} else {
210 			rb = rb->rb_left;
211 		}
212 	}
213 
214 	mutex_unlock(&prime_fpriv->lock);
215 }
216 
drm_prime_init_file_private(struct drm_prime_file_private * prime_fpriv)217 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
218 {
219 	rw_init(&prime_fpriv->lock, "primlk");
220 	prime_fpriv->dmabufs = RB_ROOT;
221 	prime_fpriv->handles = RB_ROOT;
222 }
223 
drm_prime_destroy_file_private(struct drm_prime_file_private * prime_fpriv)224 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
225 {
226 	/* by now drm_gem_release should've made sure the list is empty */
227 	WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
228 }
229 
230 /**
231  * drm_gem_dmabuf_export - &dma_buf export implementation for GEM
232  * @dev: parent device for the exported dmabuf
233  * @exp_info: the export information used by dma_buf_export()
234  *
235  * This wraps dma_buf_export() for use by generic GEM drivers that are using
236  * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
237  * a reference to the &drm_device and the exported &drm_gem_object (stored in
238  * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
239  *
240  * Returns the new dmabuf.
241  */
drm_gem_dmabuf_export(struct drm_device * dev,struct dma_buf_export_info * exp_info)242 struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
243 				      struct dma_buf_export_info *exp_info)
244 {
245 	struct drm_gem_object *obj = exp_info->priv;
246 	struct dma_buf *dma_buf;
247 
248 	dma_buf = dma_buf_export(exp_info);
249 	if (IS_ERR(dma_buf))
250 		return dma_buf;
251 
252 	drm_dev_get(dev);
253 	drm_gem_object_get(obj);
254 #ifdef __linux__
255 	dma_buf->file->f_mapping = obj->dev->anon_inode->i_mapping;
256 #endif
257 
258 	return dma_buf;
259 }
260 EXPORT_SYMBOL(drm_gem_dmabuf_export);
261 
262 /**
263  * drm_gem_dmabuf_release - &dma_buf release implementation for GEM
264  * @dma_buf: buffer to be released
265  *
266  * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
267  * must use this in their &dma_buf_ops structure as the release callback.
268  * drm_gem_dmabuf_release() should be used in conjunction with
269  * drm_gem_dmabuf_export().
270  */
drm_gem_dmabuf_release(struct dma_buf * dma_buf)271 void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
272 {
273 	struct drm_gem_object *obj = dma_buf->priv;
274 	struct drm_device *dev = obj->dev;
275 
276 	/* drop the reference on the export fd holds */
277 	drm_gem_object_put(obj);
278 
279 	drm_dev_put(dev);
280 }
281 EXPORT_SYMBOL(drm_gem_dmabuf_release);
282 
283 /**
284  * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
285  * @dev: drm_device to import into
286  * @file_priv: drm file-private structure
287  * @prime_fd: fd id of the dma-buf which should be imported
288  * @handle: pointer to storage for the handle of the imported buffer object
289  *
290  * This is the PRIME import function which must be used mandatorily by GEM
291  * drivers to ensure correct lifetime management of the underlying GEM object.
292  * The actual importing of GEM object from the dma-buf is done through the
293  * &drm_driver.gem_prime_import driver callback.
294  *
295  * Returns 0 on success or a negative error code on failure.
296  */
drm_gem_prime_fd_to_handle(struct drm_device * dev,struct drm_file * file_priv,int prime_fd,uint32_t * handle)297 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
298 			       struct drm_file *file_priv, int prime_fd,
299 			       uint32_t *handle)
300 {
301 	struct dma_buf *dma_buf;
302 	struct drm_gem_object *obj;
303 	int ret;
304 
305 	dma_buf = dma_buf_get(prime_fd);
306 	if (IS_ERR(dma_buf))
307 		return PTR_ERR(dma_buf);
308 
309 	mutex_lock(&file_priv->prime.lock);
310 
311 	ret = drm_prime_lookup_buf_handle(&file_priv->prime,
312 			dma_buf, handle);
313 	if (ret == 0)
314 		goto out_put;
315 
316 	/* never seen this one, need to import */
317 	mutex_lock(&dev->object_name_lock);
318 	if (dev->driver->gem_prime_import)
319 		obj = dev->driver->gem_prime_import(dev, dma_buf);
320 	else
321 		obj = drm_gem_prime_import(dev, dma_buf);
322 	if (IS_ERR(obj)) {
323 		ret = PTR_ERR(obj);
324 		goto out_unlock;
325 	}
326 
327 	if (obj->dma_buf) {
328 		WARN_ON(obj->dma_buf != dma_buf);
329 	} else {
330 		obj->dma_buf = dma_buf;
331 		get_dma_buf(dma_buf);
332 	}
333 
334 	/* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
335 	ret = drm_gem_handle_create_tail(file_priv, obj, handle);
336 	drm_gem_object_put(obj);
337 	if (ret)
338 		goto out_put;
339 
340 	ret = drm_prime_add_buf_handle(&file_priv->prime,
341 			dma_buf, *handle);
342 	mutex_unlock(&file_priv->prime.lock);
343 	if (ret)
344 		goto fail;
345 
346 	dma_buf_put(dma_buf);
347 
348 	return 0;
349 
350 fail:
351 	/* hmm, if driver attached, we are relying on the free-object path
352 	 * to detach.. which seems ok..
353 	 */
354 	drm_gem_handle_delete(file_priv, *handle);
355 	dma_buf_put(dma_buf);
356 	return ret;
357 
358 out_unlock:
359 	mutex_unlock(&dev->object_name_lock);
360 out_put:
361 	mutex_unlock(&file_priv->prime.lock);
362 	dma_buf_put(dma_buf);
363 	return ret;
364 }
365 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
366 
drm_prime_fd_to_handle_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)367 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
368 				 struct drm_file *file_priv)
369 {
370 	struct drm_prime_handle *args = data;
371 
372 	if (dev->driver->prime_fd_to_handle) {
373 		return dev->driver->prime_fd_to_handle(dev, file_priv, args->fd,
374 						       &args->handle);
375 	}
376 
377 	return drm_gem_prime_fd_to_handle(dev, file_priv, args->fd, &args->handle);
378 }
379 
export_and_register_object(struct drm_device * dev,struct drm_gem_object * obj,uint32_t flags)380 static struct dma_buf *export_and_register_object(struct drm_device *dev,
381 						  struct drm_gem_object *obj,
382 						  uint32_t flags)
383 {
384 	struct dma_buf *dmabuf;
385 
386 	/* prevent races with concurrent gem_close. */
387 	if (obj->handle_count == 0) {
388 		dmabuf = ERR_PTR(-ENOENT);
389 		return dmabuf;
390 	}
391 
392 	if (obj->funcs && obj->funcs->export)
393 		dmabuf = obj->funcs->export(obj, flags);
394 	else
395 		dmabuf = drm_gem_prime_export(obj, flags);
396 	if (IS_ERR(dmabuf)) {
397 		/* normally the created dma-buf takes ownership of the ref,
398 		 * but if that fails then drop the ref
399 		 */
400 		return dmabuf;
401 	}
402 
403 	/*
404 	 * Note that callers do not need to clean up the export cache
405 	 * since the check for obj->handle_count guarantees that someone
406 	 * will clean it up.
407 	 */
408 	obj->dma_buf = dmabuf;
409 	get_dma_buf(obj->dma_buf);
410 
411 	return dmabuf;
412 }
413 
414 /**
415  * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
416  * @dev: dev to export the buffer from
417  * @file_priv: drm file-private structure
418  * @handle: buffer handle to export
419  * @flags: flags like DRM_CLOEXEC
420  * @prime_fd: pointer to storage for the fd id of the create dma-buf
421  *
422  * This is the PRIME export function which must be used mandatorily by GEM
423  * drivers to ensure correct lifetime management of the underlying GEM object.
424  * The actual exporting from GEM object to a dma-buf is done through the
425  * &drm_gem_object_funcs.export callback.
426  */
drm_gem_prime_handle_to_fd(struct drm_device * dev,struct drm_file * file_priv,uint32_t handle,uint32_t flags,int * prime_fd)427 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
428 			       struct drm_file *file_priv, uint32_t handle,
429 			       uint32_t flags,
430 			       int *prime_fd)
431 {
432 	struct drm_gem_object *obj;
433 	int ret = 0;
434 	struct dma_buf *dmabuf;
435 
436 	mutex_lock(&file_priv->prime.lock);
437 	obj = drm_gem_object_lookup(file_priv, handle);
438 	if (!obj)  {
439 		ret = -ENOENT;
440 		goto out_unlock;
441 	}
442 
443 	dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
444 	if (dmabuf) {
445 		get_dma_buf(dmabuf);
446 		goto out_have_handle;
447 	}
448 
449 	mutex_lock(&dev->object_name_lock);
450 #ifdef notyet
451 	/* re-export the original imported object */
452 	if (obj->import_attach) {
453 		dmabuf = obj->import_attach->dmabuf;
454 		get_dma_buf(dmabuf);
455 		goto out_have_obj;
456 	}
457 #endif
458 
459 	if (obj->dma_buf) {
460 		get_dma_buf(obj->dma_buf);
461 		dmabuf = obj->dma_buf;
462 		goto out_have_obj;
463 	}
464 
465 	dmabuf = export_and_register_object(dev, obj, flags);
466 	if (IS_ERR(dmabuf)) {
467 		/* normally the created dma-buf takes ownership of the ref,
468 		 * but if that fails then drop the ref
469 		 */
470 		ret = PTR_ERR(dmabuf);
471 		mutex_unlock(&dev->object_name_lock);
472 		goto out;
473 	}
474 
475 out_have_obj:
476 	/*
477 	 * If we've exported this buffer then cheat and add it to the import list
478 	 * so we get the correct handle back. We must do this under the
479 	 * protection of dev->object_name_lock to ensure that a racing gem close
480 	 * ioctl doesn't miss to remove this buffer handle from the cache.
481 	 */
482 	ret = drm_prime_add_buf_handle(&file_priv->prime,
483 				       dmabuf, handle);
484 	mutex_unlock(&dev->object_name_lock);
485 	if (ret)
486 		goto fail_put_dmabuf;
487 
488 out_have_handle:
489 	ret = dma_buf_fd(dmabuf, flags);
490 	/*
491 	 * We must _not_ remove the buffer from the handle cache since the newly
492 	 * created dma buf is already linked in the global obj->dma_buf pointer,
493 	 * and that is invariant as long as a userspace gem handle exists.
494 	 * Closing the handle will clean out the cache anyway, so we don't leak.
495 	 */
496 	if (ret < 0) {
497 		goto fail_put_dmabuf;
498 	} else {
499 		*prime_fd = ret;
500 		ret = 0;
501 	}
502 
503 	goto out;
504 
505 fail_put_dmabuf:
506 	dma_buf_put(dmabuf);
507 out:
508 	drm_gem_object_put(obj);
509 out_unlock:
510 	mutex_unlock(&file_priv->prime.lock);
511 
512 	return ret;
513 }
514 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
515 
drm_prime_handle_to_fd_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)516 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
517 				 struct drm_file *file_priv)
518 {
519 	struct drm_prime_handle *args = data;
520 
521 	/* check flags are valid */
522 	if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
523 		return -EINVAL;
524 
525 	if (dev->driver->prime_handle_to_fd) {
526 		return dev->driver->prime_handle_to_fd(dev, file_priv,
527 						       args->handle, args->flags,
528 						       &args->fd);
529 	}
530 	return drm_gem_prime_handle_to_fd(dev, file_priv, args->handle,
531 					  args->flags, &args->fd);
532 }
533 
534 /**
535  * DOC: PRIME Helpers
536  *
537  * Drivers can implement &drm_gem_object_funcs.export and
538  * &drm_driver.gem_prime_import in terms of simpler APIs by using the helper
539  * functions drm_gem_prime_export() and drm_gem_prime_import(). These functions
540  * implement dma-buf support in terms of some lower-level helpers, which are
541  * again exported for drivers to use individually:
542  *
543  * Exporting buffers
544  * ~~~~~~~~~~~~~~~~~
545  *
546  * Optional pinning of buffers is handled at dma-buf attach and detach time in
547  * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
548  * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
549  * &drm_gem_object_funcs.get_sg_table. If &drm_gem_object_funcs.get_sg_table is
550  * unimplemented, exports into another device are rejected.
551  *
552  * For kernel-internal access there's drm_gem_dmabuf_vmap() and
553  * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
554  * drm_gem_dmabuf_mmap().
555  *
556  * Note that these export helpers can only be used if the underlying backing
557  * storage is fully coherent and either permanently pinned, or it is safe to pin
558  * it indefinitely.
559  *
560  * FIXME: The underlying helper functions are named rather inconsistently.
561  *
562  * Importing buffers
563  * ~~~~~~~~~~~~~~~~~
564  *
565  * Importing dma-bufs using drm_gem_prime_import() relies on
566  * &drm_driver.gem_prime_import_sg_table.
567  *
568  * Note that similarly to the export helpers this permanently pins the
569  * underlying backing storage. Which is ok for scanout, but is not the best
570  * option for sharing lots of buffers for rendering.
571  */
572 
573 /**
574  * drm_gem_map_attach - dma_buf attach implementation for GEM
575  * @dma_buf: buffer to attach device to
576  * @attach: buffer attachment data
577  *
578  * Calls &drm_gem_object_funcs.pin for device specific handling. This can be
579  * used as the &dma_buf_ops.attach callback. Must be used together with
580  * drm_gem_map_detach().
581  *
582  * Returns 0 on success, negative error code on failure.
583  */
drm_gem_map_attach(struct dma_buf * dma_buf,struct dma_buf_attachment * attach)584 int drm_gem_map_attach(struct dma_buf *dma_buf,
585 		       struct dma_buf_attachment *attach)
586 {
587 	struct drm_gem_object *obj = dma_buf->priv;
588 
589 	/*
590 	 * drm_gem_map_dma_buf() requires obj->get_sg_table(), but drivers
591 	 * that implement their own ->map_dma_buf() do not.
592 	 */
593 #ifdef notyet
594 	if (dma_buf->ops->map_dma_buf == drm_gem_map_dma_buf &&
595 	    !obj->funcs->get_sg_table)
596 #else
597 	if (!obj->funcs->get_sg_table)
598 #endif
599 		return -ENOSYS;
600 
601 	return drm_gem_pin(obj);
602 }
603 EXPORT_SYMBOL(drm_gem_map_attach);
604 
605 /**
606  * drm_gem_map_detach - dma_buf detach implementation for GEM
607  * @dma_buf: buffer to detach from
608  * @attach: attachment to be detached
609  *
610  * Calls &drm_gem_object_funcs.pin for device specific handling.  Cleans up
611  * &dma_buf_attachment from drm_gem_map_attach(). This can be used as the
612  * &dma_buf_ops.detach callback.
613  */
drm_gem_map_detach(struct dma_buf * dma_buf,struct dma_buf_attachment * attach)614 void drm_gem_map_detach(struct dma_buf *dma_buf,
615 			struct dma_buf_attachment *attach)
616 {
617 	struct drm_gem_object *obj = dma_buf->priv;
618 
619 	drm_gem_unpin(obj);
620 }
621 EXPORT_SYMBOL(drm_gem_map_detach);
622 
623 #ifdef notyet
624 
625 /**
626  * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
627  * @attach: attachment whose scatterlist is to be returned
628  * @dir: direction of DMA transfer
629  *
630  * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This
631  * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together
632  * with drm_gem_unmap_dma_buf().
633  *
634  * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR
635  * on error. May return -EINTR if it is interrupted by a signal.
636  */
drm_gem_map_dma_buf(struct dma_buf_attachment * attach,enum dma_data_direction dir)637 struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
638 				     enum dma_data_direction dir)
639 {
640 	struct drm_gem_object *obj = attach->dmabuf->priv;
641 	struct sg_table *sgt;
642 	int ret;
643 
644 	if (WARN_ON(dir == DMA_NONE))
645 		return ERR_PTR(-EINVAL);
646 
647 	if (WARN_ON(!obj->funcs->get_sg_table))
648 		return ERR_PTR(-ENOSYS);
649 
650 	sgt = obj->funcs->get_sg_table(obj);
651 	if (IS_ERR(sgt))
652 		return sgt;
653 
654 	ret = dma_map_sgtable(attach->dev, sgt, dir,
655 			      DMA_ATTR_SKIP_CPU_SYNC);
656 	if (ret) {
657 		sg_free_table(sgt);
658 		kfree(sgt);
659 		sgt = ERR_PTR(ret);
660 	}
661 
662 	return sgt;
663 }
664 EXPORT_SYMBOL(drm_gem_map_dma_buf);
665 
666 /**
667  * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
668  * @attach: attachment to unmap buffer from
669  * @sgt: scatterlist info of the buffer to unmap
670  * @dir: direction of DMA transfer
671  *
672  * This can be used as the &dma_buf_ops.unmap_dma_buf callback.
673  */
drm_gem_unmap_dma_buf(struct dma_buf_attachment * attach,struct sg_table * sgt,enum dma_data_direction dir)674 void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
675 			   struct sg_table *sgt,
676 			   enum dma_data_direction dir)
677 {
678 	if (!sgt)
679 		return;
680 
681 	dma_unmap_sgtable(attach->dev, sgt, dir, DMA_ATTR_SKIP_CPU_SYNC);
682 	sg_free_table(sgt);
683 	kfree(sgt);
684 }
685 EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
686 
687 #endif /* notyet */
688 
689 /**
690  * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
691  * @dma_buf: buffer to be mapped
692  * @map: the virtual address of the buffer
693  *
694  * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
695  * callback. Calls into &drm_gem_object_funcs.vmap for device specific handling.
696  * The kernel virtual address is returned in map.
697  *
698  * Returns 0 on success or a negative errno code otherwise.
699  */
drm_gem_dmabuf_vmap(struct dma_buf * dma_buf,struct iosys_map * map)700 int drm_gem_dmabuf_vmap(struct dma_buf *dma_buf, struct iosys_map *map)
701 {
702 	struct drm_gem_object *obj = dma_buf->priv;
703 
704 	return drm_gem_vmap(obj, map);
705 }
706 EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
707 
708 /**
709  * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
710  * @dma_buf: buffer to be unmapped
711  * @map: the virtual address of the buffer
712  *
713  * Releases a kernel virtual mapping. This can be used as the
714  * &dma_buf_ops.vunmap callback. Calls into &drm_gem_object_funcs.vunmap for device specific handling.
715  */
drm_gem_dmabuf_vunmap(struct dma_buf * dma_buf,struct iosys_map * map)716 void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, struct iosys_map *map)
717 {
718 	struct drm_gem_object *obj = dma_buf->priv;
719 
720 	drm_gem_vunmap(obj, map);
721 }
722 EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
723 
724 #ifdef __linux__
725 /**
726  * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
727  * @obj: GEM object
728  * @vma: Virtual address range
729  *
730  * This function sets up a userspace mapping for PRIME exported buffers using
731  * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
732  * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
733  * called to set up the mapping.
734  */
drm_gem_prime_mmap(struct drm_gem_object * obj,struct vm_area_struct * vma)735 int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
736 {
737 	struct drm_file *priv;
738 	struct file *fil;
739 	int ret;
740 
741 	/* Add the fake offset */
742 	vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
743 
744 	if (obj->funcs && obj->funcs->mmap) {
745 		vma->vm_ops = obj->funcs->vm_ops;
746 
747 		drm_gem_object_get(obj);
748 		ret = obj->funcs->mmap(obj, vma);
749 		if (ret) {
750 			drm_gem_object_put(obj);
751 			return ret;
752 		}
753 		vma->vm_private_data = obj;
754 		return 0;
755 	}
756 
757 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
758 	fil = kzalloc(sizeof(*fil), GFP_KERNEL);
759 	if (!priv || !fil) {
760 		ret = -ENOMEM;
761 		goto out;
762 	}
763 
764 	/* Used by drm_gem_mmap() to lookup the GEM object */
765 	priv->minor = obj->dev->primary;
766 	fil->private_data = priv;
767 
768 	ret = drm_vma_node_allow(&obj->vma_node, priv);
769 	if (ret)
770 		goto out;
771 
772 	ret = obj->dev->driver->fops->mmap(fil, vma);
773 
774 	drm_vma_node_revoke(&obj->vma_node, priv);
775 out:
776 	kfree(priv);
777 	kfree(fil);
778 
779 	return ret;
780 }
781 EXPORT_SYMBOL(drm_gem_prime_mmap);
782 #else
783 struct uvm_object *
drm_gem_prime_mmap(struct file * filp,vm_prot_t accessprot,voff_t off,vsize_t size)784 drm_gem_prime_mmap(struct file *filp, vm_prot_t accessprot, voff_t off,
785     vsize_t size)
786 {
787 	STUB();
788 	return NULL;
789 }
790 #endif
791 
792 #ifdef notyet
793 
794 /**
795  * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
796  * @dma_buf: buffer to be mapped
797  * @vma: virtual address range
798  *
799  * Provides memory mapping for the buffer. This can be used as the
800  * &dma_buf_ops.mmap callback. It just forwards to drm_gem_prime_mmap().
801  *
802  * Returns 0 on success or a negative error code on failure.
803  */
drm_gem_dmabuf_mmap(struct dma_buf * dma_buf,struct vm_area_struct * vma)804 int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
805 {
806 	struct drm_gem_object *obj = dma_buf->priv;
807 
808 	return drm_gem_prime_mmap(obj, vma);
809 }
810 EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
811 
812 #endif /* notyet */
813 
814 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
815 #ifdef notyet
816 	.cache_sgt_mapping = true,
817 	.attach = drm_gem_map_attach,
818 	.detach = drm_gem_map_detach,
819 	.map_dma_buf = drm_gem_map_dma_buf,
820 	.unmap_dma_buf = drm_gem_unmap_dma_buf,
821 #endif
822 	.release = drm_gem_dmabuf_release,
823 #ifdef notyet
824 	.mmap = drm_gem_dmabuf_mmap,
825 	.vmap = drm_gem_dmabuf_vmap,
826 	.vunmap = drm_gem_dmabuf_vunmap,
827 #endif
828 };
829 
830 /**
831  * drm_prime_pages_to_sg - converts a page array into an sg list
832  * @dev: DRM device
833  * @pages: pointer to the array of page pointers to convert
834  * @nr_pages: length of the page vector
835  *
836  * This helper creates an sg table object from a set of pages
837  * the driver is responsible for mapping the pages into the
838  * importers address space for use with dma_buf itself.
839  *
840  * This is useful for implementing &drm_gem_object_funcs.get_sg_table.
841  */
drm_prime_pages_to_sg(struct drm_device * dev,struct vm_page ** pages,unsigned int nr_pages)842 struct sg_table *drm_prime_pages_to_sg(struct drm_device *dev,
843 				       struct vm_page **pages, unsigned int nr_pages)
844 {
845 	STUB();
846 	return NULL;
847 #ifdef notyet
848 	struct sg_table *sg;
849 	size_t max_segment = 0;
850 	int err;
851 
852 	sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
853 	if (!sg)
854 		return ERR_PTR(-ENOMEM);
855 
856 	if (dev)
857 		max_segment = dma_max_mapping_size(dev->dev);
858 	if (max_segment == 0)
859 		max_segment = UINT_MAX;
860 	err = sg_alloc_table_from_pages_segment(sg, pages, nr_pages, 0,
861 						(unsigned long)nr_pages << PAGE_SHIFT,
862 						max_segment, GFP_KERNEL);
863 	if (err) {
864 		kfree(sg);
865 		sg = ERR_PTR(err);
866 	}
867 	return sg;
868 #endif
869 }
870 EXPORT_SYMBOL(drm_prime_pages_to_sg);
871 
872 /**
873  * drm_prime_get_contiguous_size - returns the contiguous size of the buffer
874  * @sgt: sg_table describing the buffer to check
875  *
876  * This helper calculates the contiguous size in the DMA address space
877  * of the buffer described by the provided sg_table.
878  *
879  * This is useful for implementing
880  * &drm_gem_object_funcs.gem_prime_import_sg_table.
881  */
drm_prime_get_contiguous_size(struct sg_table * sgt)882 unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt)
883 {
884 	STUB();
885 	return 0;
886 #ifdef notyet
887 	dma_addr_t expected = sg_dma_address(sgt->sgl);
888 	struct scatterlist *sg;
889 	unsigned long size = 0;
890 	int i;
891 
892 	for_each_sgtable_dma_sg(sgt, sg, i) {
893 		unsigned int len = sg_dma_len(sg);
894 
895 		if (!len)
896 			break;
897 		if (sg_dma_address(sg) != expected)
898 			break;
899 		expected += len;
900 		size += len;
901 	}
902 	return size;
903 #endif
904 }
905 EXPORT_SYMBOL(drm_prime_get_contiguous_size);
906 
907 /**
908  * drm_gem_prime_export - helper library implementation of the export callback
909  * @obj: GEM object to export
910  * @flags: flags like DRM_CLOEXEC and DRM_RDWR
911  *
912  * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers
913  * using the PRIME helpers. It is used as the default in
914  * drm_gem_prime_handle_to_fd().
915  */
drm_gem_prime_export(struct drm_gem_object * obj,int flags)916 struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj,
917 				     int flags)
918 {
919 	struct drm_device *dev = obj->dev;
920 	struct dma_buf_export_info exp_info = {
921 #ifdef __linux__
922 		.exp_name = KBUILD_MODNAME, /* white lie for debug */
923 		.owner = dev->driver->fops->owner,
924 #endif
925 		.ops = &drm_gem_prime_dmabuf_ops,
926 		.size = obj->size,
927 		.flags = flags,
928 		.priv = obj,
929 		.resv = obj->resv,
930 	};
931 
932 	return drm_gem_dmabuf_export(dev, &exp_info);
933 }
934 EXPORT_SYMBOL(drm_gem_prime_export);
935 
936 /**
937  * drm_gem_prime_import_dev - core implementation of the import callback
938  * @dev: drm_device to import into
939  * @dma_buf: dma-buf object to import
940  * @attach_dev: struct device to dma_buf attach
941  *
942  * This is the core of drm_gem_prime_import(). It's designed to be called by
943  * drivers who want to use a different device structure than &drm_device.dev for
944  * attaching via dma_buf. This function calls
945  * &drm_driver.gem_prime_import_sg_table internally.
946  *
947  * Drivers must arrange to call drm_prime_gem_destroy() from their
948  * &drm_gem_object_funcs.free hook when using this function.
949  */
drm_gem_prime_import_dev(struct drm_device * dev,struct dma_buf * dma_buf,struct device * attach_dev)950 struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
951 					    struct dma_buf *dma_buf,
952 					    struct device *attach_dev)
953 {
954 	struct dma_buf_attachment *attach;
955 #ifdef notyet
956 	struct sg_table *sgt;
957 #endif
958 	struct drm_gem_object *obj;
959 	int ret;
960 
961 	if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
962 		obj = dma_buf->priv;
963 		if (obj->dev == dev) {
964 			/*
965 			 * Importing dmabuf exported from our own gem increases
966 			 * refcount on gem itself instead of f_count of dmabuf.
967 			 */
968 			drm_gem_object_get(obj);
969 			return obj;
970 		}
971 	}
972 
973 #ifdef notyet
974 	if (!dev->driver->gem_prime_import_sg_table)
975 		return ERR_PTR(-EINVAL);
976 #endif
977 
978 	attach = dma_buf_attach(dma_buf, attach_dev);
979 	if (IS_ERR(attach))
980 		return ERR_CAST(attach);
981 
982 #ifdef notyet
983 	get_dma_buf(dma_buf);
984 
985 	sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
986 	if (IS_ERR(sgt)) {
987 		ret = PTR_ERR(sgt);
988 		goto fail_detach;
989 	}
990 
991 	obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
992 	if (IS_ERR(obj)) {
993 		ret = PTR_ERR(obj);
994 		goto fail_unmap;
995 	}
996 
997 	obj->import_attach = attach;
998 	obj->resv = dma_buf->resv;
999 
1000 	return obj;
1001 
1002 fail_unmap:
1003 	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
1004 fail_detach:
1005 	dma_buf_detach(dma_buf, attach);
1006 	dma_buf_put(dma_buf);
1007 
1008 	return ERR_PTR(ret);
1009 #else
1010 	ret = 0;
1011 	panic(__func__);
1012 #endif
1013 }
1014 EXPORT_SYMBOL(drm_gem_prime_import_dev);
1015 
1016 /**
1017  * drm_gem_prime_import - helper library implementation of the import callback
1018  * @dev: drm_device to import into
1019  * @dma_buf: dma-buf object to import
1020  *
1021  * This is the implementation of the gem_prime_import functions for GEM drivers
1022  * using the PRIME helpers. Drivers can use this as their
1023  * &drm_driver.gem_prime_import implementation. It is used as the default
1024  * implementation in drm_gem_prime_fd_to_handle().
1025  *
1026  * Drivers must arrange to call drm_prime_gem_destroy() from their
1027  * &drm_gem_object_funcs.free hook when using this function.
1028  */
drm_gem_prime_import(struct drm_device * dev,struct dma_buf * dma_buf)1029 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1030 					    struct dma_buf *dma_buf)
1031 {
1032 	return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
1033 }
1034 EXPORT_SYMBOL(drm_gem_prime_import);
1035 
1036 /**
1037  * drm_prime_sg_to_page_array - convert an sg table into a page array
1038  * @sgt: scatter-gather table to convert
1039  * @pages: array of page pointers to store the pages in
1040  * @max_entries: size of the passed-in array
1041  *
1042  * Exports an sg table into an array of pages.
1043  *
1044  * This function is deprecated and strongly discouraged to be used.
1045  * The page array is only useful for page faults and those can corrupt fields
1046  * in the struct page if they are not handled by the exporting driver.
1047  */
drm_prime_sg_to_page_array(struct sg_table * sgt,struct vm_page ** pages,int max_entries)1048 int __deprecated drm_prime_sg_to_page_array(struct sg_table *sgt,
1049 					    struct vm_page **pages,
1050 					    int max_entries)
1051 {
1052 	STUB();
1053 	return -ENOSYS;
1054 #ifdef notyet
1055 	struct sg_page_iter page_iter;
1056 	struct vm_page **p = pages;
1057 
1058 	for_each_sgtable_page(sgt, &page_iter, 0) {
1059 		if (WARN_ON(p - pages >= max_entries))
1060 			return -1;
1061 		*p++ = sg_page_iter_page(&page_iter);
1062 	}
1063 	return 0;
1064 #endif
1065 }
1066 EXPORT_SYMBOL(drm_prime_sg_to_page_array);
1067 
1068 /**
1069  * drm_prime_sg_to_dma_addr_array - convert an sg table into a dma addr array
1070  * @sgt: scatter-gather table to convert
1071  * @addrs: array to store the dma bus address of each page
1072  * @max_entries: size of both the passed-in arrays
1073  *
1074  * Exports an sg table into an array of addresses.
1075  *
1076  * Drivers should use this in their &drm_driver.gem_prime_import_sg_table
1077  * implementation.
1078  */
drm_prime_sg_to_dma_addr_array(struct sg_table * sgt,dma_addr_t * addrs,int max_entries)1079 int drm_prime_sg_to_dma_addr_array(struct sg_table *sgt, dma_addr_t *addrs,
1080 				   int max_entries)
1081 {
1082 	STUB();
1083 	return -ENOSYS;
1084 #ifdef notyet
1085 	struct sg_dma_page_iter dma_iter;
1086 	dma_addr_t *a = addrs;
1087 
1088 	for_each_sgtable_dma_page(sgt, &dma_iter, 0) {
1089 		if (WARN_ON(a - addrs >= max_entries))
1090 			return -1;
1091 		*a++ = sg_page_iter_dma_address(&dma_iter);
1092 	}
1093 	return 0;
1094 #endif
1095 }
1096 EXPORT_SYMBOL(drm_prime_sg_to_dma_addr_array);
1097 
1098 /**
1099  * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
1100  * @obj: GEM object which was created from a dma-buf
1101  * @sg: the sg-table which was pinned at import time
1102  *
1103  * This is the cleanup functions which GEM drivers need to call when they use
1104  * drm_gem_prime_import() or drm_gem_prime_import_dev() to import dma-bufs.
1105  */
drm_prime_gem_destroy(struct drm_gem_object * obj,struct sg_table * sg)1106 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
1107 {
1108 	STUB();
1109 #ifdef notyet
1110 	struct dma_buf_attachment *attach;
1111 	struct dma_buf *dma_buf;
1112 
1113 	attach = obj->import_attach;
1114 	if (sg)
1115 		dma_buf_unmap_attachment_unlocked(attach, sg, DMA_BIDIRECTIONAL);
1116 	dma_buf = attach->dmabuf;
1117 	dma_buf_detach(attach->dmabuf, attach);
1118 	/* remove the reference */
1119 	dma_buf_put(dma_buf);
1120 #endif
1121 }
1122 EXPORT_SYMBOL(drm_prime_gem_destroy);
1123