xref: /dragonfly/sys/dev/drm/drm_prime.c (revision 97edc4fd)
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 <drm/drmP.h>
33 #include <drm/drm_gem.h>
34 
35 #include "drm_internal.h"
36 
37 /*
38  * DMA-BUF/GEM Object references and lifetime overview:
39  *
40  * On the export the dma_buf holds a reference to the exporting GEM
41  * object. It takes this reference in handle_to_fd_ioctl, when it
42  * first calls .prime_export and stores the exporting GEM object in
43  * the dma_buf priv. This reference is released when the dma_buf
44  * object goes away in the driver .release function.
45  *
46  * On the import the importing GEM object holds a reference to the
47  * dma_buf (which in turn holds a ref to the exporting GEM object).
48  * It takes that reference in the fd_to_handle ioctl.
49  * It calls dma_buf_get, creates an attachment to it and stores the
50  * attachment in the GEM object. When this attachment is destroyed
51  * when the imported object is destroyed, we remove the attachment
52  * and drop the reference to the dma_buf.
53  *
54  * Thus the chain of references always flows in one direction
55  * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
56  *
57  * Self-importing: if userspace is using PRIME as a replacement for flink
58  * then it will get a fd->handle request for a GEM object that it created.
59  * Drivers should detect this situation and return back the gem object
60  * from the dma-buf private.  Prime will do this automatically for drivers that
61  * use the drm_gem_prime_{import,export} helpers.
62  */
63 
64 struct drm_prime_member {
65 	struct dma_buf *dma_buf;
66 	uint32_t handle;
67 
68 	struct rb_node dmabuf_rb;
69 	struct rb_node handle_rb;
70 };
71 
72 struct drm_prime_attachment {
73 	struct sg_table *sgt;
74 	enum dma_data_direction dir;
75 };
76 
77 #if 0
78 static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
79 				    struct dma_buf *dma_buf, uint32_t handle)
80 {
81 	struct drm_prime_member *member;
82 	struct rb_node **p, *rb;
83 
84 	member = kmalloc(sizeof(*member), GFP_KERNEL);
85 	if (!member)
86 		return -ENOMEM;
87 
88 	get_dma_buf(dma_buf);
89 	member->dma_buf = dma_buf;
90 	member->handle = handle;
91 
92 	rb = NULL;
93 	p = &prime_fpriv->dmabufs.rb_node;
94 	while (*p) {
95 		struct drm_prime_member *pos;
96 
97 		rb = *p;
98 		pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
99 		if (dma_buf > pos->dma_buf)
100 			p = &rb->rb_right;
101 		else
102 			p = &rb->rb_left;
103 	}
104 	rb_link_node(&member->dmabuf_rb, rb, p);
105 	rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
106 
107 	rb = NULL;
108 	p = &prime_fpriv->handles.rb_node;
109 	while (*p) {
110 		struct drm_prime_member *pos;
111 
112 		rb = *p;
113 		pos = rb_entry(rb, struct drm_prime_member, handle_rb);
114 		if (handle > pos->handle)
115 			p = &rb->rb_right;
116 		else
117 			p = &rb->rb_left;
118 	}
119 	rb_link_node(&member->handle_rb, rb, p);
120 	rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
121 
122 	return 0;
123 }
124 
125 static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
126 						      uint32_t handle)
127 {
128 	struct rb_node *rb;
129 
130 	rb = prime_fpriv->handles.rb_node;
131 	while (rb) {
132 		struct drm_prime_member *member;
133 
134 		member = rb_entry(rb, struct drm_prime_member, handle_rb);
135 		if (member->handle == handle)
136 			return member->dma_buf;
137 		else if (member->handle < handle)
138 			rb = rb->rb_right;
139 		else
140 			rb = rb->rb_left;
141 	}
142 
143 	return NULL;
144 }
145 
146 static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
147 				       struct dma_buf *dma_buf,
148 				       uint32_t *handle)
149 {
150 	struct rb_node *rb;
151 
152 	rb = prime_fpriv->dmabufs.rb_node;
153 	while (rb) {
154 		struct drm_prime_member *member;
155 
156 		member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
157 		if (member->dma_buf == dma_buf) {
158 			*handle = member->handle;
159 			return 0;
160 		} else if (member->dma_buf < dma_buf) {
161 			rb = rb->rb_right;
162 		} else {
163 			rb = rb->rb_left;
164 		}
165 	}
166 
167 	return -ENOENT;
168 }
169 #endif
170 
171 static int drm_gem_map_attach(struct dma_buf *dma_buf,
172 			      struct device *target_dev,
173 			      struct dma_buf_attachment *attach)
174 {
175 	struct drm_prime_attachment *prime_attach;
176 	struct drm_gem_object *obj = dma_buf->priv;
177 	struct drm_device *dev = obj->dev;
178 
179 	prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
180 	if (!prime_attach)
181 		return -ENOMEM;
182 
183 	prime_attach->dir = DMA_NONE;
184 	attach->priv = prime_attach;
185 
186 	if (!dev->driver->gem_prime_pin)
187 		return 0;
188 
189 	return dev->driver->gem_prime_pin(obj);
190 }
191 
192 static void drm_gem_map_detach(struct dma_buf *dma_buf,
193 			       struct dma_buf_attachment *attach)
194 {
195 	struct drm_prime_attachment *prime_attach = attach->priv;
196 	struct drm_gem_object *obj = dma_buf->priv;
197 	struct drm_device *dev = obj->dev;
198 	struct sg_table *sgt;
199 
200 	if (dev->driver->gem_prime_unpin)
201 		dev->driver->gem_prime_unpin(obj);
202 
203 	if (!prime_attach)
204 		return;
205 
206 	sgt = prime_attach->sgt;
207 	if (sgt) {
208 		if (prime_attach->dir != DMA_NONE)
209 			dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
210 					prime_attach->dir);
211 		sg_free_table(sgt);
212 	}
213 
214 	kfree(sgt);
215 	kfree(prime_attach);
216 	attach->priv = NULL;
217 }
218 
219 #if 0
220 void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
221 					struct dma_buf *dma_buf)
222 {
223 	struct rb_node *rb;
224 
225 	rb = prime_fpriv->dmabufs.rb_node;
226 	while (rb) {
227 		struct drm_prime_member *member;
228 
229 		member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
230 		if (member->dma_buf == dma_buf) {
231 			rb_erase(&member->handle_rb, &prime_fpriv->handles);
232 			rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
233 
234 			dma_buf_put(dma_buf);
235 			kfree(member);
236 			return;
237 		} else if (member->dma_buf < dma_buf) {
238 			rb = rb->rb_right;
239 		} else {
240 			rb = rb->rb_left;
241 		}
242 	}
243 }
244 #endif
245 
246 static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
247 					    enum dma_data_direction dir)
248 {
249 	struct drm_prime_attachment *prime_attach = attach->priv;
250 	struct drm_gem_object *obj = attach->dmabuf->priv;
251 	struct sg_table *sgt;
252 
253 	if (WARN_ON(dir == DMA_NONE || !prime_attach))
254 		return ERR_PTR(-EINVAL);
255 
256 	/* return the cached mapping when possible */
257 	if (prime_attach->dir == dir)
258 		return prime_attach->sgt;
259 
260 	/*
261 	 * two mappings with different directions for the same attachment are
262 	 * not allowed
263 	 */
264 	if (WARN_ON(prime_attach->dir != DMA_NONE))
265 		return ERR_PTR(-EBUSY);
266 
267 	sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
268 
269 	if (!IS_ERR(sgt)) {
270 		if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
271 			sg_free_table(sgt);
272 			kfree(sgt);
273 			sgt = ERR_PTR(-ENOMEM);
274 		} else {
275 			prime_attach->sgt = sgt;
276 			prime_attach->dir = dir;
277 		}
278 	}
279 
280 	return sgt;
281 }
282 
283 static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
284 				  struct sg_table *sgt,
285 				  enum dma_data_direction dir)
286 {
287 	/* nothing to be done here */
288 }
289 
290 /**
291  * drm_gem_dmabuf_export - dma_buf export implementation for GEM
292  * @dev: parent device for the exported dmabuf
293  * @exp_info: the export information used by dma_buf_export()
294  *
295  * This wraps dma_buf_export() for use by generic GEM drivers that are using
296  * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
297  * a reference to the &drm_device and the exported &drm_gem_object (stored in
298  * exp_info->priv) which is released by drm_gem_dmabuf_release().
299  *
300  * Returns the new dmabuf.
301  */
302 struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
303 				      struct dma_buf_export_info *exp_info)
304 {
305 	struct dma_buf *dma_buf;
306 
307 	dma_buf = dma_buf_export(exp_info);
308 	if (IS_ERR(dma_buf))
309 		return dma_buf;
310 
311 	drm_dev_ref(dev);
312 	drm_gem_object_reference(exp_info->priv);
313 
314 	return dma_buf;
315 }
316 EXPORT_SYMBOL(drm_gem_dmabuf_export);
317 
318 /**
319  * drm_gem_dmabuf_release - dma_buf release implementation for GEM
320  * @dma_buf: buffer to be released
321  *
322  * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
323  * must use this in their dma_buf ops structure as the release callback.
324  * drm_gem_dmabuf_release() should be used in conjunction with
325  * drm_gem_dmabuf_export().
326  */
327 void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
328 {
329 	struct drm_gem_object *obj = dma_buf->priv;
330 	struct drm_device *dev = obj->dev;
331 
332 	/* drop the reference on the export fd holds */
333 	drm_gem_object_unreference_unlocked(obj);
334 
335 	drm_dev_unref(dev);
336 }
337 EXPORT_SYMBOL(drm_gem_dmabuf_release);
338 
339 static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
340 {
341 	struct drm_gem_object *obj = dma_buf->priv;
342 	struct drm_device *dev = obj->dev;
343 
344 	return dev->driver->gem_prime_vmap(obj);
345 }
346 
347 static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
348 {
349 	struct drm_gem_object *obj = dma_buf->priv;
350 	struct drm_device *dev = obj->dev;
351 
352 	dev->driver->gem_prime_vunmap(obj, vaddr);
353 }
354 
355 static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
356 					unsigned long page_num)
357 {
358 	return NULL;
359 }
360 
361 static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
362 					 unsigned long page_num, void *addr)
363 {
364 
365 }
366 static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
367 				 unsigned long page_num)
368 {
369 	return NULL;
370 }
371 
372 static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
373 				  unsigned long page_num, void *addr)
374 {
375 
376 }
377 
378 static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
379 			       struct vm_area_struct *vma)
380 {
381 	struct drm_gem_object *obj = dma_buf->priv;
382 	struct drm_device *dev = obj->dev;
383 
384 	if (!dev->driver->gem_prime_mmap)
385 		return -ENOSYS;
386 
387 	return dev->driver->gem_prime_mmap(obj, vma);
388 }
389 
390 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
391 	.attach = drm_gem_map_attach,
392 	.detach = drm_gem_map_detach,
393 	.map_dma_buf = drm_gem_map_dma_buf,
394 	.unmap_dma_buf = drm_gem_unmap_dma_buf,
395 	.release = drm_gem_dmabuf_release,
396 	.kmap = drm_gem_dmabuf_kmap,
397 	.kmap_atomic = drm_gem_dmabuf_kmap_atomic,
398 	.kunmap = drm_gem_dmabuf_kunmap,
399 	.kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
400 	.mmap = drm_gem_dmabuf_mmap,
401 	.vmap = drm_gem_dmabuf_vmap,
402 	.vunmap = drm_gem_dmabuf_vunmap,
403 };
404 
405 /**
406  * DOC: PRIME Helpers
407  *
408  * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
409  * simpler APIs by using the helper functions @drm_gem_prime_export and
410  * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
411  * six lower-level driver callbacks:
412  *
413  * Export callbacks:
414  *
415  *  * @gem_prime_pin (optional): prepare a GEM object for exporting
416  *  * @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
417  *  * @gem_prime_vmap: vmap a buffer exported by your driver
418  *  * @gem_prime_vunmap: vunmap a buffer exported by your driver
419  *  * @gem_prime_mmap (optional): mmap a buffer exported by your driver
420  *
421  * Import callback:
422  *
423  *  * @gem_prime_import_sg_table (import): produce a GEM object from another
424  *    driver's scatter/gather table
425  */
426 
427 /**
428  * drm_gem_prime_export - helper library implementation of the export callback
429  * @dev: drm_device to export from
430  * @obj: GEM object to export
431  * @flags: flags like DRM_CLOEXEC and DRM_RDWR
432  *
433  * This is the implementation of the gem_prime_export functions for GEM drivers
434  * using the PRIME helpers.
435  */
436 struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
437 				     struct drm_gem_object *obj,
438 				     int flags)
439 {
440 	struct dma_buf_export_info exp_info = {
441 #if 0
442 		.exp_name = KBUILD_MODNAME, /* white lie for debug */
443 		.owner = dev->driver->fops->owner,
444 #endif
445 		.ops = &drm_gem_prime_dmabuf_ops,
446 		.size = obj->size,
447 		.flags = flags,
448 		.priv = obj,
449 	};
450 
451 	if (dev->driver->gem_prime_res_obj)
452 		exp_info.resv = dev->driver->gem_prime_res_obj(obj);
453 
454 	return drm_gem_dmabuf_export(dev, &exp_info);
455 }
456 EXPORT_SYMBOL(drm_gem_prime_export);
457 
458 #if 0
459 static struct dma_buf *export_and_register_object(struct drm_device *dev,
460 						  struct drm_gem_object *obj,
461 						  uint32_t flags)
462 {
463 	struct dma_buf *dmabuf;
464 
465 	/* prevent races with concurrent gem_close. */
466 	if (obj->handle_count == 0) {
467 		dmabuf = ERR_PTR(-ENOENT);
468 		return dmabuf;
469 	}
470 
471 	dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
472 	if (IS_ERR(dmabuf)) {
473 		/* normally the created dma-buf takes ownership of the ref,
474 		 * but if that fails then drop the ref
475 		 */
476 		return dmabuf;
477 	}
478 
479 	/*
480 	 * Note that callers do not need to clean up the export cache
481 	 * since the check for obj->handle_count guarantees that someone
482 	 * will clean it up.
483 	 */
484 	obj->dma_buf = dmabuf;
485 	get_dma_buf(obj->dma_buf);
486 
487 	return dmabuf;
488 }
489 #endif
490 
491 /**
492  * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
493  * @dev: dev to export the buffer from
494  * @file_priv: drm file-private structure
495  * @handle: buffer handle to export
496  * @flags: flags like DRM_CLOEXEC
497  * @prime_fd: pointer to storage for the fd id of the create dma-buf
498  *
499  * This is the PRIME export function which must be used mandatorily by GEM
500  * drivers to ensure correct lifetime management of the underlying GEM object.
501  * The actual exporting from GEM object to a dma-buf is done through the
502  * gem_prime_export driver callback.
503  */
504 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
505 			       struct drm_file *file_priv, uint32_t handle,
506 			       uint32_t flags,
507 			       int *prime_fd)
508 {
509 #if 0
510 	struct drm_gem_object *obj;
511 #endif
512 	int ret = 0;
513 #if 0
514 	struct dma_buf *dmabuf;
515 
516 	mutex_lock(&file_priv->prime.lock);
517 	obj = drm_gem_object_lookup(file_priv, handle);
518 	if (!obj)  {
519 #endif
520 		ret = -ENOENT;
521 #if 0
522 		goto out_unlock;
523 	}
524 
525 	dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
526 	if (dmabuf) {
527 		get_dma_buf(dmabuf);
528 		goto out_have_handle;
529 	}
530 
531 	mutex_lock(&dev->object_name_lock);
532 	/* re-export the original imported object */
533 	if (obj->import_attach) {
534 		dmabuf = obj->import_attach->dmabuf;
535 		get_dma_buf(dmabuf);
536 		goto out_have_obj;
537 	}
538 
539 	if (obj->dma_buf) {
540 		get_dma_buf(obj->dma_buf);
541 		dmabuf = obj->dma_buf;
542 		goto out_have_obj;
543 	}
544 
545 	dmabuf = export_and_register_object(dev, obj, flags);
546 	if (IS_ERR(dmabuf)) {
547 		/* normally the created dma-buf takes ownership of the ref,
548 		 * but if that fails then drop the ref
549 		 */
550 		ret = PTR_ERR(dmabuf);
551 		mutex_unlock(&dev->object_name_lock);
552 		goto out;
553 	}
554 
555 out_have_obj:
556 	/*
557 	 * If we've exported this buffer then cheat and add it to the import list
558 	 * so we get the correct handle back. We must do this under the
559 	 * protection of dev->object_name_lock to ensure that a racing gem close
560 	 * ioctl doesn't miss to remove this buffer handle from the cache.
561 	 */
562 	ret = drm_prime_add_buf_handle(&file_priv->prime,
563 				       dmabuf, handle);
564 	mutex_unlock(&dev->object_name_lock);
565 	if (ret)
566 		goto fail_put_dmabuf;
567 
568 out_have_handle:
569 	ret = dma_buf_fd(dmabuf, flags);
570 	/*
571 	 * We must _not_ remove the buffer from the handle cache since the newly
572 	 * created dma buf is already linked in the global obj->dma_buf pointer,
573 	 * and that is invariant as long as a userspace gem handle exists.
574 	 * Closing the handle will clean out the cache anyway, so we don't leak.
575 	 */
576 	if (ret < 0) {
577 		goto fail_put_dmabuf;
578 	} else {
579 		*prime_fd = ret;
580 		ret = 0;
581 	}
582 
583 	goto out;
584 
585 fail_put_dmabuf:
586 	dma_buf_put(dmabuf);
587 out:
588 	drm_gem_object_unreference_unlocked(obj);
589 out_unlock:
590 	mutex_unlock(&file_priv->prime.lock);
591 #endif
592 
593 	return ret;
594 }
595 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
596 
597 /**
598  * drm_gem_prime_import - helper library implementation of the import callback
599  * @dev: drm_device to import into
600  * @dma_buf: dma-buf object to import
601  *
602  * This is the implementation of the gem_prime_import functions for GEM drivers
603  * using the PRIME helpers.
604  */
605 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
606 					    struct dma_buf *dma_buf)
607 {
608 #if 0
609 	struct dma_buf_attachment *attach;
610 	struct sg_table *sgt;
611 	struct drm_gem_object *obj;
612 	int ret;
613 
614 	if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
615 		obj = dma_buf->priv;
616 		if (obj->dev == dev) {
617 			/*
618 			 * Importing dmabuf exported from out own gem increases
619 			 * refcount on gem itself instead of f_count of dmabuf.
620 			 */
621 			drm_gem_object_reference(obj);
622 			return obj;
623 		}
624 	}
625 
626 	if (!dev->driver->gem_prime_import_sg_table)
627 #endif
628 		return ERR_PTR(-EINVAL);
629 
630 #if 0
631 	attach = dma_buf_attach(dma_buf, dev->dev);
632 	if (IS_ERR(attach))
633 		return ERR_CAST(attach);
634 
635 	get_dma_buf(dma_buf);
636 
637 	sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
638 	if (IS_ERR(sgt)) {
639 		ret = PTR_ERR(sgt);
640 		goto fail_detach;
641 	}
642 
643 	obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
644 	if (IS_ERR(obj)) {
645 		ret = PTR_ERR(obj);
646 		goto fail_unmap;
647 	}
648 
649 	obj->import_attach = attach;
650 
651 	return obj;
652 
653 fail_unmap:
654 	dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
655 fail_detach:
656 	dma_buf_detach(dma_buf, attach);
657 	dma_buf_put(dma_buf);
658 
659 	return ERR_PTR(ret);
660 #endif
661 }
662 EXPORT_SYMBOL(drm_gem_prime_import);
663 
664 /**
665  * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
666  * @dev: dev to export the buffer from
667  * @file_priv: drm file-private structure
668  * @prime_fd: fd id of the dma-buf which should be imported
669  * @handle: pointer to storage for the handle of the imported buffer object
670  *
671  * This is the PRIME import function which must be used mandatorily by GEM
672  * drivers to ensure correct lifetime management of the underlying GEM object.
673  * The actual importing of GEM object from the dma-buf is done through the
674  * gem_import_export driver callback.
675  */
676 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
677 			       struct drm_file *file_priv, int prime_fd,
678 			       uint32_t *handle)
679 {
680 #if 0
681 	struct dma_buf *dma_buf;
682 	struct drm_gem_object *obj;
683 	int ret;
684 
685 	dma_buf = dma_buf_get(prime_fd);
686 	if (IS_ERR(dma_buf))
687 		return PTR_ERR(dma_buf);
688 
689 	mutex_lock(&file_priv->prime.lock);
690 
691 	ret = drm_prime_lookup_buf_handle(&file_priv->prime,
692 			dma_buf, handle);
693 	if (ret == 0)
694 		goto out_put;
695 
696 	/* never seen this one, need to import */
697 	mutex_lock(&dev->object_name_lock);
698 	obj = dev->driver->gem_prime_import(dev, dma_buf);
699 	if (IS_ERR(obj)) {
700 		ret = PTR_ERR(obj);
701 		goto out_unlock;
702 	}
703 
704 	if (obj->dma_buf) {
705 		WARN_ON(obj->dma_buf != dma_buf);
706 	} else {
707 		obj->dma_buf = dma_buf;
708 		get_dma_buf(dma_buf);
709 	}
710 
711 	/* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
712 	ret = drm_gem_handle_create_tail(file_priv, obj, handle);
713 	drm_gem_object_unreference_unlocked(obj);
714 	if (ret)
715 		goto out_put;
716 
717 	ret = drm_prime_add_buf_handle(&file_priv->prime,
718 			dma_buf, *handle);
719 	mutex_unlock(&file_priv->prime.lock);
720 	if (ret)
721 		goto fail;
722 
723 	dma_buf_put(dma_buf);
724 
725 	return 0;
726 
727 fail:
728 	/* hmm, if driver attached, we are relying on the free-object path
729 	 * to detach.. which seems ok..
730 	 */
731 	drm_gem_handle_delete(file_priv, *handle);
732 	dma_buf_put(dma_buf);
733 	return ret;
734 
735 out_unlock:
736 	mutex_unlock(&dev->object_name_lock);
737 out_put:
738 	mutex_unlock(&file_priv->prime.lock);
739 	dma_buf_put(dma_buf);
740 	return ret;
741 #endif
742 	return -EINVAL;
743 }
744 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
745 
746 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
747 				 struct drm_file *file_priv)
748 {
749 	struct drm_prime_handle *args = data;
750 
751 	if (!drm_core_check_feature(dev, DRIVER_PRIME))
752 		return -EINVAL;
753 
754 	if (!dev->driver->prime_handle_to_fd)
755 		return -ENOSYS;
756 
757 	/* check flags are valid */
758 	if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
759 		return -EINVAL;
760 
761 	return dev->driver->prime_handle_to_fd(dev, file_priv,
762 			args->handle, args->flags, &args->fd);
763 }
764 
765 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
766 				 struct drm_file *file_priv)
767 {
768 	struct drm_prime_handle *args = data;
769 
770 	if (!drm_core_check_feature(dev, DRIVER_PRIME))
771 		return -EINVAL;
772 
773 	if (!dev->driver->prime_fd_to_handle)
774 		return -ENOSYS;
775 
776 	return dev->driver->prime_fd_to_handle(dev, file_priv,
777 			args->fd, &args->handle);
778 }
779 
780 /**
781  * drm_prime_pages_to_sg - converts a page array into an sg list
782  * @pages: pointer to the array of page pointers to convert
783  * @nr_pages: length of the page vector
784  *
785  * This helper creates an sg table object from a set of pages
786  * the driver is responsible for mapping the pages into the
787  * importers address space for use with dma_buf itself.
788  */
789 struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
790 {
791 #if 0
792 	struct sg_table *sg = NULL;
793 #endif
794 	int ret;
795 
796 #if 0
797 	sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
798 	if (!sg) {
799 #endif
800 		ret = -ENOMEM;
801 #if 0
802 		goto out;
803 	}
804 
805 	ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
806 				nr_pages << PAGE_SHIFT, GFP_KERNEL);
807 	if (ret)
808 		goto out;
809 
810 	return sg;
811 out:
812 	kfree(sg);
813 #endif
814 	return ERR_PTR(ret);
815 }
816 EXPORT_SYMBOL(drm_prime_pages_to_sg);
817 
818 /**
819  * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
820  * @sgt: scatter-gather table to convert
821  * @pages: array of page pointers to store the page array in
822  * @addrs: optional array to store the dma bus address of each page
823  * @max_pages: size of both the passed-in arrays
824  *
825  * Exports an sg table into an array of pages and addresses. This is currently
826  * required by the TTM driver in order to do correct fault handling.
827  */
828 int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
829 				     dma_addr_t *addrs, int max_pages)
830 {
831 	unsigned count;
832 	struct scatterlist *sg;
833 	struct page *page;
834 	u32 len;
835 	int pg_index;
836 	dma_addr_t addr;
837 
838 	pg_index = 0;
839 	for_each_sg(sgt->sgl, sg, sgt->nents, count) {
840 		len = sg->length;
841 		page = sg_page(sg);
842 		addr = sg_dma_address(sg);
843 
844 		while (len > 0) {
845 			if (WARN_ON(pg_index >= max_pages))
846 				return -1;
847 			pages[pg_index] = page;
848 			if (addrs)
849 				addrs[pg_index] = addr;
850 
851 			page++;
852 			addr += PAGE_SIZE;
853 			len -= PAGE_SIZE;
854 			pg_index++;
855 		}
856 	}
857 	return 0;
858 }
859 EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
860 
861 /**
862  * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
863  * @obj: GEM object which was created from a dma-buf
864  * @sg: the sg-table which was pinned at import time
865  *
866  * This is the cleanup functions which GEM drivers need to call when they use
867  * @drm_gem_prime_import to import dma-bufs.
868  */
869 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
870 {
871 	struct dma_buf_attachment *attach;
872 	struct dma_buf *dma_buf;
873 	attach = obj->import_attach;
874 	if (sg)
875 		dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
876 	dma_buf = attach->dmabuf;
877 #if 0
878 	dma_buf_detach(attach->dmabuf, attach);
879 	/* remove the reference */
880 	dma_buf_put(dma_buf);
881 #endif
882 }
883 EXPORT_SYMBOL(drm_prime_gem_destroy);
884 
885 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
886 {
887 	lockinit(&prime_fpriv->lock, "drmpfpl", 0, LK_CANRECURSE);
888 	prime_fpriv->dmabufs = LINUX_RB_ROOT;
889 	prime_fpriv->handles = LINUX_RB_ROOT;
890 }
891 
892 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
893 {
894 	/* by now drm_gem_release should've made sure the list is empty */
895 	WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
896 }
897