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