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