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