xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_dma_buf.c (revision 42ac1f71ddfc8f2b1ea1555399aa1e1ffc2faced)
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 #include <linux/pm_runtime.h>
44 
45 /**
46  * amdgpu_dma_buf_attach - &dma_buf_ops.attach implementation
47  *
48  * @dmabuf: DMA-buf where we attach to
49  * @attach: attachment to add
50  *
51  * Add the attachment as user to the exported DMA-buf.
52  */
53 static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
54 				 struct dma_buf_attachment *attach)
55 {
56 	struct drm_gem_object *obj = dmabuf->priv;
57 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
58 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
59 	int r;
60 
61 #ifdef notyet
62 	if (pci_p2pdma_distance_many(adev->pdev, &attach->dev, 1, true) < 0)
63 		attach->peer2peer = false;
64 
65 	if (attach->dev->driver == adev->dev->driver)
66 		return 0;
67 #endif
68 
69 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
70 	if (r < 0)
71 		goto out;
72 
73 	return 0;
74 
75 out:
76 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
77 	return r;
78 }
79 
80 #ifdef notyet
81 
82 /**
83  * amdgpu_dma_buf_detach - &dma_buf_ops.detach implementation
84  *
85  * @dmabuf: DMA-buf where we remove the attachment from
86  * @attach: the attachment to remove
87  *
88  * Called when an attachment is removed from the DMA-buf.
89  */
90 static void amdgpu_dma_buf_detach(struct dma_buf *dmabuf,
91 				  struct dma_buf_attachment *attach)
92 {
93 	struct drm_gem_object *obj = dmabuf->priv;
94 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
95 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
96 
97 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
98 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
99 }
100 
101 /**
102  * amdgpu_dma_buf_pin - &dma_buf_ops.pin implementation
103  *
104  * @attach: attachment to pin down
105  *
106  * Pin the BO which is backing the DMA-buf so that it can't move any more.
107  */
108 static int amdgpu_dma_buf_pin(struct dma_buf_attachment *attach)
109 {
110 	struct drm_gem_object *obj = attach->dmabuf->priv;
111 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
112 	int r;
113 
114 	/* pin buffer into GTT */
115 	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
116 	if (r)
117 		return r;
118 
119 	if (bo->tbo.moving) {
120 		r = dma_fence_wait(bo->tbo.moving, true);
121 		if (r) {
122 			amdgpu_bo_unpin(bo);
123 			return r;
124 		}
125 	}
126 	return 0;
127 }
128 
129 /**
130  * amdgpu_dma_buf_unpin - &dma_buf_ops.unpin implementation
131  *
132  * @attach: attachment to unpin
133  *
134  * Unpin a previously pinned BO to make it movable again.
135  */
136 static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach)
137 {
138 	struct drm_gem_object *obj = attach->dmabuf->priv;
139 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
140 
141 	amdgpu_bo_unpin(bo);
142 }
143 
144 /**
145  * amdgpu_dma_buf_map - &dma_buf_ops.map_dma_buf implementation
146  * @attach: DMA-buf attachment
147  * @dir: DMA direction
148  *
149  * Makes sure that the shared DMA buffer can be accessed by the target device.
150  * For now, simply pins it to the GTT domain, where it should be accessible by
151  * all DMA devices.
152  *
153  * Returns:
154  * sg_table filled with the DMA addresses to use or ERR_PRT with negative error
155  * code.
156  */
157 static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
158 					   enum dma_data_direction dir)
159 {
160 	struct dma_buf *dma_buf = attach->dmabuf;
161 	struct drm_gem_object *obj = dma_buf->priv;
162 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
163 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
164 	struct sg_table *sgt;
165 	long r;
166 
167 	if (!bo->tbo.pin_count) {
168 		/* move buffer into GTT or VRAM */
169 		struct ttm_operation_ctx ctx = { false, false };
170 		unsigned domains = AMDGPU_GEM_DOMAIN_GTT;
171 
172 		if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM &&
173 		    attach->peer2peer) {
174 			bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
175 			domains |= AMDGPU_GEM_DOMAIN_VRAM;
176 		}
177 		amdgpu_bo_placement_from_domain(bo, domains);
178 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
179 		if (r)
180 			return ERR_PTR(r);
181 
182 	} else if (!(amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type) &
183 		     AMDGPU_GEM_DOMAIN_GTT)) {
184 		return ERR_PTR(-EBUSY);
185 	}
186 
187 	switch (bo->tbo.resource->mem_type) {
188 	case TTM_PL_TT:
189 		sgt = drm_prime_pages_to_sg(obj->dev,
190 					    bo->tbo.ttm->pages,
191 					    bo->tbo.ttm->num_pages);
192 		if (IS_ERR(sgt))
193 			return sgt;
194 
195 		if (dma_map_sgtable(attach->dev, sgt, dir,
196 				    DMA_ATTR_SKIP_CPU_SYNC))
197 			goto error_free;
198 		break;
199 
200 	case TTM_PL_VRAM:
201 		r = amdgpu_vram_mgr_alloc_sgt(adev, bo->tbo.resource, 0,
202 					      bo->tbo.base.size, attach->dev,
203 					      dir, &sgt);
204 		if (r)
205 			return ERR_PTR(r);
206 		break;
207 	default:
208 		return ERR_PTR(-EINVAL);
209 	}
210 
211 	return sgt;
212 
213 error_free:
214 	sg_free_table(sgt);
215 	kfree(sgt);
216 	return ERR_PTR(-EBUSY);
217 }
218 
219 /**
220  * amdgpu_dma_buf_unmap - &dma_buf_ops.unmap_dma_buf implementation
221  * @attach: DMA-buf attachment
222  * @sgt: sg_table to unmap
223  * @dir: DMA direction
224  *
225  * This is called when a shared DMA buffer no longer needs to be accessible by
226  * another device. For now, simply unpins the buffer from GTT.
227  */
228 static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach,
229 				 struct sg_table *sgt,
230 				 enum dma_data_direction dir)
231 {
232 	if (sgt->sgl->page_link) {
233 		dma_unmap_sgtable(attach->dev, sgt, dir, 0);
234 		sg_free_table(sgt);
235 		kfree(sgt);
236 	} else {
237 		amdgpu_vram_mgr_free_sgt(attach->dev, dir, sgt);
238 	}
239 }
240 
241 /**
242  * amdgpu_dma_buf_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
243  * @dma_buf: Shared DMA buffer
244  * @direction: Direction of DMA transfer
245  *
246  * This is called before CPU access to the shared DMA buffer's memory. If it's
247  * a read access, the buffer is moved to the GTT domain if possible, for optimal
248  * CPU read performance.
249  *
250  * Returns:
251  * 0 on success or a negative error code on failure.
252  */
253 static int amdgpu_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
254 					   enum dma_data_direction direction)
255 {
256 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
257 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
258 	struct ttm_operation_ctx ctx = { true, false };
259 	u32 domain = amdgpu_display_supported_domains(adev, bo->flags);
260 	int ret;
261 	bool reads = (direction == DMA_BIDIRECTIONAL ||
262 		      direction == DMA_FROM_DEVICE);
263 
264 	if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
265 		return 0;
266 
267 	/* move to gtt */
268 	ret = amdgpu_bo_reserve(bo, false);
269 	if (unlikely(ret != 0))
270 		return ret;
271 
272 	if (!bo->tbo.pin_count &&
273 	    (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
274 		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
275 		ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
276 	}
277 
278 	amdgpu_bo_unreserve(bo);
279 	return ret;
280 }
281 
282 #endif /* notyet */
283 
284 const struct dma_buf_ops amdgpu_dmabuf_ops = {
285 #ifdef notyet
286 	.attach = amdgpu_dma_buf_attach,
287 	.detach = amdgpu_dma_buf_detach,
288 	.pin = amdgpu_dma_buf_pin,
289 	.unpin = amdgpu_dma_buf_unpin,
290 	.map_dma_buf = amdgpu_dma_buf_map,
291 	.unmap_dma_buf = amdgpu_dma_buf_unmap,
292 #endif
293 	.release = drm_gem_dmabuf_release,
294 #ifdef notyet
295 	.begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
296 	.mmap = drm_gem_dmabuf_mmap,
297 	.vmap = drm_gem_dmabuf_vmap,
298 	.vunmap = drm_gem_dmabuf_vunmap,
299 #endif
300 };
301 
302 /**
303  * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
304  * @gobj: GEM BO
305  * @flags: Flags such as DRM_CLOEXEC and DRM_RDWR.
306  *
307  * The main work is done by the &drm_gem_prime_export helper.
308  *
309  * Returns:
310  * Shared DMA buffer representing the GEM BO from the given device.
311  */
312 struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
313 					int flags)
314 {
315 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
316 	struct dma_buf *buf;
317 
318 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
319 	    bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
320 		return ERR_PTR(-EPERM);
321 
322 	buf = drm_gem_prime_export(gobj, flags);
323 	if (!IS_ERR(buf))
324 		buf->ops = &amdgpu_dmabuf_ops;
325 
326 	return buf;
327 }
328 
329 /**
330  * amdgpu_dma_buf_create_obj - create BO for DMA-buf import
331  *
332  * @dev: DRM device
333  * @dma_buf: DMA-buf
334  *
335  * Creates an empty SG BO for DMA-buf import.
336  *
337  * Returns:
338  * A new GEM BO of the given DRM device, representing the memory
339  * described by the given DMA-buf attachment and scatter/gather table.
340  */
341 static struct drm_gem_object *
342 amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
343 {
344 	struct dma_resv *resv = dma_buf->resv;
345 	struct amdgpu_device *adev = drm_to_adev(dev);
346 	struct drm_gem_object *gobj;
347 	struct amdgpu_bo *bo;
348 	uint64_t flags = 0;
349 	int ret;
350 
351 	dma_resv_lock(resv, NULL);
352 
353 	if (dma_buf->ops == &amdgpu_dmabuf_ops) {
354 		struct amdgpu_bo *other = gem_to_amdgpu_bo(dma_buf->priv);
355 
356 		flags |= other->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC;
357 	}
358 
359 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
360 				       AMDGPU_GEM_DOMAIN_CPU, flags,
361 				       ttm_bo_type_sg, resv, &gobj);
362 	if (ret)
363 		goto error;
364 
365 	bo = gem_to_amdgpu_bo(gobj);
366 	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
367 	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
368 
369 	dma_resv_unlock(resv);
370 	return gobj;
371 
372 error:
373 	dma_resv_unlock(resv);
374 	return ERR_PTR(ret);
375 }
376 
377 /**
378  * amdgpu_dma_buf_move_notify - &attach.move_notify implementation
379  *
380  * @attach: the DMA-buf attachment
381  *
382  * Invalidate the DMA-buf attachment, making sure that the we re-create the
383  * mapping before the next use.
384  */
385 static void
386 amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)
387 {
388 	struct drm_gem_object *obj = attach->importer_priv;
389 	struct ww_acquire_ctx *ticket = dma_resv_locking_ctx(obj->resv);
390 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
391 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
392 	struct ttm_operation_ctx ctx = { false, false };
393 	struct ttm_placement placement = {};
394 	struct amdgpu_vm_bo_base *bo_base;
395 	int r;
396 
397 	if (!bo->tbo.resource || bo->tbo.resource->mem_type == TTM_PL_SYSTEM)
398 		return;
399 
400 	r = ttm_bo_validate(&bo->tbo, &placement, &ctx);
401 	if (r) {
402 		DRM_ERROR("Failed to invalidate DMA-buf import (%d))\n", r);
403 		return;
404 	}
405 
406 	for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
407 		struct amdgpu_vm *vm = bo_base->vm;
408 		struct dma_resv *resv = vm->root.bo->tbo.base.resv;
409 
410 		if (ticket) {
411 			/* When we get an error here it means that somebody
412 			 * else is holding the VM lock and updating page tables
413 			 * So we can just continue here.
414 			 */
415 			r = dma_resv_lock(resv, ticket);
416 			if (r)
417 				continue;
418 
419 		} else {
420 			/* TODO: This is more problematic and we actually need
421 			 * to allow page tables updates without holding the
422 			 * lock.
423 			 */
424 			if (!dma_resv_trylock(resv))
425 				continue;
426 		}
427 
428 		r = amdgpu_vm_clear_freed(adev, vm, NULL);
429 		if (!r)
430 			r = amdgpu_vm_handle_moved(adev, vm);
431 
432 		if (r && r != -EBUSY)
433 			DRM_ERROR("Failed to invalidate VM page tables (%d))\n",
434 				  r);
435 
436 		dma_resv_unlock(resv);
437 	}
438 }
439 
440 static const struct dma_buf_attach_ops amdgpu_dma_buf_attach_ops = {
441 	.allow_peer2peer = true,
442 	.move_notify = amdgpu_dma_buf_move_notify
443 };
444 
445 /**
446  * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
447  * @dev: DRM device
448  * @dma_buf: Shared DMA buffer
449  *
450  * Import a dma_buf into a the driver and potentially create a new GEM object.
451  *
452  * Returns:
453  * GEM BO representing the shared DMA buffer for the given device.
454  */
455 struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
456 					       struct dma_buf *dma_buf)
457 {
458 	struct dma_buf_attachment *attach;
459 	struct drm_gem_object *obj;
460 
461 	if (dma_buf->ops == &amdgpu_dmabuf_ops) {
462 		obj = dma_buf->priv;
463 		if (obj->dev == dev) {
464 			/*
465 			 * Importing dmabuf exported from out own gem increases
466 			 * refcount on gem itself instead of f_count of dmabuf.
467 			 */
468 			drm_gem_object_get(obj);
469 			return obj;
470 		}
471 	}
472 
473 	obj = amdgpu_dma_buf_create_obj(dev, dma_buf);
474 	if (IS_ERR(obj))
475 		return obj;
476 
477 	STUB();
478 #ifdef notyet
479 	attach = dma_buf_dynamic_attach(dma_buf, dev->dev,
480 					&amdgpu_dma_buf_attach_ops, obj);
481 	if (IS_ERR(attach)) {
482 		drm_gem_object_put(obj);
483 		return ERR_CAST(attach);
484 	}
485 #else
486 	attach = NULL;
487 #endif
488 
489 	get_dma_buf(dma_buf);
490 	obj->import_attach = attach;
491 	return obj;
492 }
493 
494 /**
495  * amdgpu_dmabuf_is_xgmi_accessible - Check if xgmi available for P2P transfer
496  *
497  * @adev: amdgpu_device pointer of the importer
498  * @bo: amdgpu buffer object
499  *
500  * Returns:
501  * True if dmabuf accessible over xgmi, false otherwise.
502  */
503 bool amdgpu_dmabuf_is_xgmi_accessible(struct amdgpu_device *adev,
504 				      struct amdgpu_bo *bo)
505 {
506 	struct drm_gem_object *obj = &bo->tbo.base;
507 	struct drm_gem_object *gobj;
508 
509 	if (obj->import_attach) {
510 #ifdef notyet
511 		struct dma_buf *dma_buf = obj->import_attach->dmabuf;
512 
513 		if (dma_buf->ops != &amdgpu_dmabuf_ops)
514 			/* No XGMI with non AMD GPUs */
515 			return false;
516 
517 		gobj = dma_buf->priv;
518 		bo = gem_to_amdgpu_bo(gobj);
519 #else
520 		return false;
521 #endif
522 	}
523 
524 	if (amdgpu_xgmi_same_hive(adev, amdgpu_ttm_adev(bo->tbo.bdev)) &&
525 			(bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM))
526 		return true;
527 
528 	return false;
529 }
530