xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_dma_buf.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * based on nouveau_prime.c
23  *
24  * Authors: Alex Deucher
25  */
26 
27 /**
28  * DOC: PRIME Buffer Sharing
29  *
30  * The following callback implementations are used for :ref:`sharing GEM buffer
31  * objects between different devices via PRIME <prime_buffer_sharing>`.
32  */
33 
34 #include "amdgpu.h"
35 #include "amdgpu_display.h"
36 #include "amdgpu_gem.h"
37 #include "amdgpu_dma_buf.h"
38 #include "amdgpu_xgmi.h"
39 #include <drm/amdgpu_drm.h>
40 #include <linux/dma-buf.h>
41 #include <linux/dma-fence-array.h>
42 #include <linux/pci-p2pdma.h>
43 
44 /**
45  * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
46  * @obj: GEM BO
47  *
48  * Sets up an in-kernel virtual mapping of the BO's memory.
49  *
50  * Returns:
51  * The virtual address of the mapping or an error pointer.
52  */
53 void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
54 {
55 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
56 	int ret;
57 
58 	ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
59 			  &bo->dma_buf_vmap);
60 	if (ret)
61 		return ERR_PTR(ret);
62 
63 	return bo->dma_buf_vmap.virtual;
64 }
65 
66 /**
67  * amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
68  * @obj: GEM BO
69  * @vaddr: Virtual address (unused)
70  *
71  * Tears down the in-kernel virtual mapping of the BO's memory.
72  */
73 void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
74 {
75 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
76 
77 	ttm_bo_kunmap(&bo->dma_buf_vmap);
78 }
79 
80 #ifdef notyet
81 
82 /**
83  * amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
84  * @obj: GEM BO
85  * @vma: Virtual memory area
86  *
87  * Sets up a userspace mapping of the BO's memory in the given
88  * virtual memory area.
89  *
90  * Returns:
91  * 0 on success or a negative error code on failure.
92  */
93 int amdgpu_gem_prime_mmap(struct drm_gem_object *obj,
94 			  struct vm_area_struct *vma)
95 {
96 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
97 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
98 	unsigned asize = amdgpu_bo_size(bo);
99 	int ret;
100 
101 	if (!vma->vm_file)
102 		return -ENODEV;
103 
104 	if (adev == NULL)
105 		return -ENODEV;
106 
107 	/* Check for valid size. */
108 	if (asize < vma->vm_end - vma->vm_start)
109 		return -EINVAL;
110 
111 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
112 	    (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
113 		return -EPERM;
114 	}
115 	vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;
116 
117 	/* prime mmap does not need to check access, so allow here */
118 	ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
119 	if (ret)
120 		return ret;
121 
122 	ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
123 	drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);
124 
125 	return ret;
126 }
127 
128 static int
129 __dma_resv_make_exclusive(struct dma_resv *obj)
130 {
131 	struct dma_fence **fences;
132 	unsigned int count;
133 	int r;
134 
135 	if (!dma_resv_get_list(obj)) /* no shared fences to convert */
136 		return 0;
137 
138 	r = dma_resv_get_fences_rcu(obj, NULL, &count, &fences);
139 	if (r)
140 		return r;
141 
142 	if (count == 0) {
143 		/* Now that was unexpected. */
144 	} else if (count == 1) {
145 		dma_resv_add_excl_fence(obj, fences[0]);
146 		dma_fence_put(fences[0]);
147 		kfree(fences);
148 	} else {
149 		struct dma_fence_array *array;
150 
151 		array = dma_fence_array_create(count, fences,
152 					       dma_fence_context_alloc(1), 0,
153 					       false);
154 		if (!array)
155 			goto err_fences_put;
156 
157 		dma_resv_add_excl_fence(obj, &array->base);
158 		dma_fence_put(&array->base);
159 	}
160 
161 	return 0;
162 
163 err_fences_put:
164 	while (count--)
165 		dma_fence_put(fences[count]);
166 	kfree(fences);
167 	return -ENOMEM;
168 }
169 
170 /**
171  * amdgpu_dma_buf_attach - &dma_buf_ops.attach implementation
172  *
173  * @dmabuf: DMA-buf where we attach to
174  * @attach: attachment to add
175  *
176  * Add the attachment as user to the exported DMA-buf.
177  */
178 static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
179 				 struct dma_buf_attachment *attach)
180 {
181 	struct drm_gem_object *obj = dmabuf->priv;
182 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
183 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
184 	int r;
185 
186 	if (pci_p2pdma_distance_many(adev->pdev, &attach->dev, 1, true) < 0)
187 		attach->peer2peer = false;
188 
189 	if (attach->dev->driver == adev->dev->driver)
190 		return 0;
191 
192 	r = amdgpu_bo_reserve(bo, false);
193 	if (unlikely(r != 0))
194 		return r;
195 
196 	/*
197 	 * We only create shared fences for internal use, but importers
198 	 * of the dmabuf rely on exclusive fences for implicitly
199 	 * tracking write hazards. As any of the current fences may
200 	 * correspond to a write, we need to convert all existing
201 	 * fences on the reservation object into a single exclusive
202 	 * fence.
203 	 */
204 	r = __dma_resv_make_exclusive(bo->tbo.base.resv);
205 	if (r)
206 		return r;
207 
208 	bo->prime_shared_count++;
209 	amdgpu_bo_unreserve(bo);
210 	return 0;
211 }
212 
213 /**
214  * amdgpu_dma_buf_detach - &dma_buf_ops.detach implementation
215  *
216  * @dmabuf: DMA-buf where we remove the attachment from
217  * @attach: the attachment to remove
218  *
219  * Called when an attachment is removed from the DMA-buf.
220  */
221 static void amdgpu_dma_buf_detach(struct dma_buf *dmabuf,
222 				  struct dma_buf_attachment *attach)
223 {
224 	struct drm_gem_object *obj = dmabuf->priv;
225 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
226 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
227 
228 	if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
229 		bo->prime_shared_count--;
230 }
231 
232 /**
233  * amdgpu_dma_buf_pin - &dma_buf_ops.pin implementation
234  *
235  * @attach: attachment to pin down
236  *
237  * Pin the BO which is backing the DMA-buf so that it can't move any more.
238  */
239 static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
240 {
241 	struct drm_gem_object *obj = attach->dmabuf->priv;
242 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
243 	int r;
244 
245 	/* pin buffer into GTT */
246 	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
247 	if (r)
248 		return r;
249 
250 	if (bo->tbo.moving) {
251 		r = dma_fence_wait(bo->tbo.moving, true);
252 		if (r) {
253 			amdgpu_bo_unpin(bo);
254 			return r;
255 		}
256 	}
257 	return 0;
258 }
259 
260 /**
261  * amdgpu_dma_buf_unpin - &dma_buf_ops.unpin implementation
262  *
263  * @attach: attachment to unpin
264  *
265  * Unpin a previously pinned BO to make it movable again.
266  */
267 static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach)
268 {
269 	struct drm_gem_object *obj = attach->dmabuf->priv;
270 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
271 
272 	amdgpu_bo_unpin(bo);
273 }
274 
275 /**
276  * amdgpu_dma_buf_map - &dma_buf_ops.map_dma_buf implementation
277  * @attach: DMA-buf attachment
278  * @dir: DMA direction
279  *
280  * Makes sure that the shared DMA buffer can be accessed by the target device.
281  * For now, simply pins it to the GTT domain, where it should be accessible by
282  * all DMA devices.
283  *
284  * Returns:
285  * sg_table filled with the DMA addresses to use or ERR_PRT with negative error
286  * code.
287  */
288 static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
289 					   enum dma_data_direction dir)
290 {
291 	struct dma_buf *dma_buf = attach->dmabuf;
292 	struct drm_gem_object *obj = dma_buf->priv;
293 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
294 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
295 	struct sg_table *sgt;
296 	long r;
297 
298 	if (!bo->pin_count) {
299 		/* move buffer into GTT or VRAM */
300 		struct ttm_operation_ctx ctx = { false, false };
301 		unsigned domains = AMDGPU_GEM_DOMAIN_GTT;
302 
303 		if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM &&
304 		    attach->peer2peer) {
305 			bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
306 			domains |= AMDGPU_GEM_DOMAIN_VRAM;
307 		}
308 		amdgpu_bo_placement_from_domain(bo, domains);
309 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
310 		if (r)
311 			return ERR_PTR(r);
312 
313 	} else if (!(amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type) &
314 		     AMDGPU_GEM_DOMAIN_GTT)) {
315 		return ERR_PTR(-EBUSY);
316 	}
317 
318 	switch (bo->tbo.mem.mem_type) {
319 	case TTM_PL_TT:
320 		sgt = drm_prime_pages_to_sg(obj->dev,
321 					    bo->tbo.ttm->pages,
322 					    bo->tbo.num_pages);
323 		if (IS_ERR(sgt))
324 			return sgt;
325 
326 		if (dma_map_sgtable(attach->dev, sgt, dir,
327 				    DMA_ATTR_SKIP_CPU_SYNC))
328 			goto error_free;
329 		break;
330 
331 	case TTM_PL_VRAM:
332 		r = amdgpu_vram_mgr_alloc_sgt(adev, &bo->tbo.mem, attach->dev,
333 					      dir, &sgt);
334 		if (r)
335 			return ERR_PTR(r);
336 		break;
337 	default:
338 		return ERR_PTR(-EINVAL);
339 	}
340 
341 	return sgt;
342 
343 error_free:
344 	sg_free_table(sgt);
345 	kfree(sgt);
346 	return ERR_PTR(-EBUSY);
347 }
348 
349 /**
350  * amdgpu_dma_buf_unmap - &dma_buf_ops.unmap_dma_buf implementation
351  * @attach: DMA-buf attachment
352  * @sgt: sg_table to unmap
353  * @dir: DMA direction
354  *
355  * This is called when a shared DMA buffer no longer needs to be accessible by
356  * another device. For now, simply unpins the buffer from GTT.
357  */
358 static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach,
359 				 struct sg_table *sgt,
360 				 enum dma_data_direction dir)
361 {
362 	struct dma_buf *dma_buf = attach->dmabuf;
363 	struct drm_gem_object *obj = dma_buf->priv;
364 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
365 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
366 
367 	if (sgt->sgl->page_link) {
368 		dma_unmap_sgtable(attach->dev, sgt, dir, 0);
369 		sg_free_table(sgt);
370 		kfree(sgt);
371 	} else {
372 		amdgpu_vram_mgr_free_sgt(adev, attach->dev, dir, sgt);
373 	}
374 }
375 
376 /**
377  * amdgpu_dma_buf_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
378  * @dma_buf: Shared DMA buffer
379  * @direction: Direction of DMA transfer
380  *
381  * This is called before CPU access to the shared DMA buffer's memory. If it's
382  * a read access, the buffer is moved to the GTT domain if possible, for optimal
383  * CPU read performance.
384  *
385  * Returns:
386  * 0 on success or a negative error code on failure.
387  */
388 static int amdgpu_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
389 					   enum dma_data_direction direction)
390 {
391 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
392 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
393 	struct ttm_operation_ctx ctx = { true, false };
394 	u32 domain = amdgpu_display_supported_domains(adev, bo->flags);
395 	int ret;
396 	bool reads = (direction == DMA_BIDIRECTIONAL ||
397 		      direction == DMA_FROM_DEVICE);
398 
399 	if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
400 		return 0;
401 
402 	/* move to gtt */
403 	ret = amdgpu_bo_reserve(bo, false);
404 	if (unlikely(ret != 0))
405 		return ret;
406 
407 	if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
408 		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
409 		ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
410 	}
411 
412 	amdgpu_bo_unreserve(bo);
413 	return ret;
414 }
415 
416 #endif /* notyet */
417 
418 const struct dma_buf_ops amdgpu_dmabuf_ops = {
419 #ifdef notyet
420 	.attach = amdgpu_dma_buf_attach,
421 	.detach = amdgpu_dma_buf_detach,
422 	.pin = amdgpu_dma_buf_pin,
423 	.unpin = amdgpu_dma_buf_unpin,
424 	.map_dma_buf = amdgpu_dma_buf_map,
425 	.unmap_dma_buf = amdgpu_dma_buf_unmap,
426 #endif
427 	.release = drm_gem_dmabuf_release,
428 #ifdef notyet
429 	.begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
430 	.mmap = drm_gem_dmabuf_mmap,
431 	.vmap = drm_gem_dmabuf_vmap,
432 	.vunmap = drm_gem_dmabuf_vunmap,
433 #endif
434 };
435 
436 /**
437  * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
438  * @gobj: GEM BO
439  * @flags: Flags such as DRM_CLOEXEC and DRM_RDWR.
440  *
441  * The main work is done by the &drm_gem_prime_export helper.
442  *
443  * Returns:
444  * Shared DMA buffer representing the GEM BO from the given device.
445  */
446 struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
447 					int flags)
448 {
449 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
450 	struct dma_buf *buf;
451 
452 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
453 	    bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
454 		return ERR_PTR(-EPERM);
455 
456 	buf = drm_gem_prime_export(gobj, flags);
457 	if (!IS_ERR(buf))
458 		buf->ops = &amdgpu_dmabuf_ops;
459 
460 	return buf;
461 }
462 
463 /**
464  * amdgpu_dma_buf_create_obj - create BO for DMA-buf import
465  *
466  * @dev: DRM device
467  * @dma_buf: DMA-buf
468  *
469  * Creates an empty SG BO for DMA-buf import.
470  *
471  * Returns:
472  * A new GEM BO of the given DRM device, representing the memory
473  * described by the given DMA-buf attachment and scatter/gather table.
474  */
475 static struct drm_gem_object *
476 amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
477 {
478 	struct dma_resv *resv = dma_buf->resv;
479 	struct amdgpu_device *adev = drm_to_adev(dev);
480 	struct amdgpu_bo *bo;
481 	struct amdgpu_bo_param bp;
482 	struct drm_gem_object *gobj;
483 	int ret;
484 
485 	memset(&bp, 0, sizeof(bp));
486 	bp.size = dma_buf->size;
487 	bp.byte_align = PAGE_SIZE;
488 	bp.domain = AMDGPU_GEM_DOMAIN_CPU;
489 	bp.flags = 0;
490 	bp.type = ttm_bo_type_sg;
491 	bp.resv = resv;
492 	dma_resv_lock(resv, NULL);
493 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
494 			AMDGPU_GEM_DOMAIN_CPU,
495 			0, ttm_bo_type_sg, resv, &gobj);
496 	if (ret)
497 		goto error;
498 
499 	bo = gem_to_amdgpu_bo(gobj);
500 	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
501 	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
502 	if (dma_buf->ops != &amdgpu_dmabuf_ops)
503 		bo->prime_shared_count = 1;
504 
505 	dma_resv_unlock(resv);
506 	return gobj;
507 
508 error:
509 	dma_resv_unlock(resv);
510 	return ERR_PTR(ret);
511 }
512 
513 /**
514  * amdgpu_dma_buf_move_notify - &attach.move_notify implementation
515  *
516  * @attach: the DMA-buf attachment
517  *
518  * Invalidate the DMA-buf attachment, making sure that the we re-create the
519  * mapping before the next use.
520  */
521 static void
522 amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)
523 {
524 	struct drm_gem_object *obj = attach->importer_priv;
525 	struct ww_acquire_ctx *ticket = dma_resv_locking_ctx(obj->resv);
526 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
527 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
528 	struct ttm_operation_ctx ctx = { false, false };
529 	struct ttm_placement placement = {};
530 	struct amdgpu_vm_bo_base *bo_base;
531 	int r;
532 
533 	if (bo->tbo.mem.mem_type == TTM_PL_SYSTEM)
534 		return;
535 
536 	r = ttm_bo_validate(&bo->tbo, &placement, &ctx);
537 	if (r) {
538 		DRM_ERROR("Failed to invalidate DMA-buf import (%d))\n", r);
539 		return;
540 	}
541 
542 	for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
543 		struct amdgpu_vm *vm = bo_base->vm;
544 		struct dma_resv *resv = vm->root.base.bo->tbo.base.resv;
545 
546 		if (ticket) {
547 			/* When we get an error here it means that somebody
548 			 * else is holding the VM lock and updating page tables
549 			 * So we can just continue here.
550 			 */
551 			r = dma_resv_lock(resv, ticket);
552 			if (r)
553 				continue;
554 
555 		} else {
556 			/* TODO: This is more problematic and we actually need
557 			 * to allow page tables updates without holding the
558 			 * lock.
559 			 */
560 			if (!dma_resv_trylock(resv))
561 				continue;
562 		}
563 
564 		r = amdgpu_vm_clear_freed(adev, vm, NULL);
565 		if (!r)
566 			r = amdgpu_vm_handle_moved(adev, vm);
567 
568 		if (r && r != -EBUSY)
569 			DRM_ERROR("Failed to invalidate VM page tables (%d))\n",
570 				  r);
571 
572 		dma_resv_unlock(resv);
573 	}
574 }
575 
576 static const struct dma_buf_attach_ops amdgpu_dma_buf_attach_ops = {
577 	.allow_peer2peer = true,
578 	.move_notify = amdgpu_dma_buf_move_notify
579 };
580 
581 /**
582  * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
583  * @dev: DRM device
584  * @dma_buf: Shared DMA buffer
585  *
586  * Import a dma_buf into a the driver and potentially create a new GEM object.
587  *
588  * Returns:
589  * GEM BO representing the shared DMA buffer for the given device.
590  */
591 struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
592 					       struct dma_buf *dma_buf)
593 {
594 	struct dma_buf_attachment *attach;
595 	struct drm_gem_object *obj;
596 
597 	if (dma_buf->ops == &amdgpu_dmabuf_ops) {
598 		obj = dma_buf->priv;
599 		if (obj->dev == dev) {
600 			/*
601 			 * Importing dmabuf exported from out own gem increases
602 			 * refcount on gem itself instead of f_count of dmabuf.
603 			 */
604 			drm_gem_object_get(obj);
605 			return obj;
606 		}
607 	}
608 
609 	obj = amdgpu_dma_buf_create_obj(dev, dma_buf);
610 	if (IS_ERR(obj))
611 		return obj;
612 
613 	STUB();
614 #ifdef notyet
615 	attach = dma_buf_dynamic_attach(dma_buf, dev->dev,
616 					&amdgpu_dma_buf_attach_ops, obj);
617 	if (IS_ERR(attach)) {
618 		drm_gem_object_put(obj);
619 		return ERR_CAST(attach);
620 	}
621 #else
622 	attach = NULL;
623 #endif
624 
625 	get_dma_buf(dma_buf);
626 	obj->import_attach = attach;
627 	return obj;
628 }
629 
630 /**
631  * amdgpu_dmabuf_is_xgmi_accessible - Check if xgmi available for P2P transfer
632  *
633  * @adev: amdgpu_device pointer of the importer
634  * @bo: amdgpu buffer object
635  *
636  * Returns:
637  * True if dmabuf accessible over xgmi, false otherwise.
638  */
639 bool amdgpu_dmabuf_is_xgmi_accessible(struct amdgpu_device *adev,
640 				      struct amdgpu_bo *bo)
641 {
642 	struct drm_gem_object *obj = &bo->tbo.base;
643 	struct drm_gem_object *gobj;
644 
645 	if (obj->import_attach) {
646 #ifdef notyet
647 		struct dma_buf *dma_buf = obj->import_attach->dmabuf;
648 
649 		if (dma_buf->ops != &amdgpu_dmabuf_ops)
650 			/* No XGMI with non AMD GPUs */
651 			return false;
652 
653 		gobj = dma_buf->priv;
654 		bo = gem_to_amdgpu_bo(gobj);
655 #else
656 		return false;
657 #endif
658 	}
659 
660 	if (amdgpu_xgmi_same_hive(adev, amdgpu_ttm_adev(bo->tbo.bdev)) &&
661 			(bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM))
662 		return true;
663 
664 	return false;
665 }
666