xref: /linux/drivers/gpu/drm/xe/xe_dma_buf.c (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_dma_buf.h"
7 
8 #include <kunit/test.h>
9 #include <linux/dma-buf.h>
10 #include <linux/dynamic_debug.h>
11 #include <linux/pci-p2pdma.h>
12 
13 #include <drm/drm_device.h>
14 #include <drm/drm_prime.h>
15 #include <drm/ttm/ttm_tt.h>
16 
17 #include "tests/xe_test.h"
18 #include "xe_bo.h"
19 #include "xe_device.h"
20 #include "xe_pm.h"
21 #include "xe_ttm_vram_mgr.h"
22 #include "xe_vm.h"
23 
24 MODULE_IMPORT_NS("DMA_BUF");
25 
26 #if defined(CONFIG_DYNAMIC_DEBUG) || \
27 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
28 static void xe_ddebug_flip_bool(const struct _ddebug *desc, bool *flag)
29 {
30 	*flag = true;
31 }
32 
33 #define xe_dma_buf_p2p_debug_flip(flag) \
34 	_dynamic_func_call("xe dma-buf p2p debug", xe_ddebug_flip_bool, flag)
35 #else
36 #define xe_dma_buf_p2p_debug_flip(flag) do { } while (0)
37 #endif
38 
39 static int xe_dma_buf_attach(struct dma_buf *dmabuf,
40 			     struct dma_buf_attachment *attach)
41 {
42 	struct drm_gem_object *obj = attach->dmabuf->priv;
43 	bool verbose_p2p_dist = drm_debug_enabled(DRM_UT_DRIVER);
44 
45 	xe_dma_buf_p2p_debug_flip(&verbose_p2p_dist);
46 
47 	if (attach->peer2peer &&
48 	    pci_p2pdma_distance(to_pci_dev(obj->dev->dev), attach->dev, verbose_p2p_dist) < 0)
49 		attach->peer2peer = false;
50 
51 	if (!attach->peer2peer && !xe_bo_can_migrate(gem_to_xe_bo(obj), XE_PL_TT))
52 		return -EOPNOTSUPP;
53 
54 	xe_pm_runtime_get(to_xe_device(obj->dev));
55 	return 0;
56 }
57 
58 static void xe_dma_buf_detach(struct dma_buf *dmabuf,
59 			      struct dma_buf_attachment *attach)
60 {
61 	struct drm_gem_object *obj = attach->dmabuf->priv;
62 
63 	xe_pm_runtime_put(to_xe_device(obj->dev));
64 }
65 
66 static int xe_dma_buf_pin(struct dma_buf_attachment *attach)
67 {
68 	struct dma_buf *dmabuf = attach->dmabuf;
69 	struct drm_gem_object *obj = dmabuf->priv;
70 	struct xe_bo *bo = gem_to_xe_bo(obj);
71 	struct xe_device *xe = xe_bo_device(bo);
72 	struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
73 	bool allow_vram = true;
74 	int ret;
75 
76 	list_for_each_entry(attach, &dmabuf->attachments, node) {
77 		if (!attach->peer2peer) {
78 			allow_vram = false;
79 			break;
80 		}
81 	}
82 
83 	if (xe_bo_is_pinned(bo) && !xe_bo_is_mem_type(bo, XE_PL_TT) &&
84 	    !(xe_bo_is_vram(bo) && allow_vram)) {
85 		drm_dbg(&xe->drm, "Can't migrate pinned bo for dma-buf pin.\n");
86 		return -EINVAL;
87 	}
88 
89 	if (!allow_vram) {
90 		ret = xe_bo_migrate(bo, XE_PL_TT, NULL, exec);
91 		if (ret) {
92 			if (ret != -EINTR && ret != -ERESTARTSYS)
93 				drm_dbg(&xe->drm,
94 					"Failed migrating dma-buf to TT memory: %pe\n",
95 					ERR_PTR(ret));
96 			return ret;
97 		}
98 	}
99 
100 	ret = xe_bo_pin_external(bo, !allow_vram, exec);
101 	xe_assert(xe, !ret);
102 
103 	return 0;
104 }
105 
106 static void xe_dma_buf_unpin(struct dma_buf_attachment *attach)
107 {
108 	struct drm_gem_object *obj = attach->dmabuf->priv;
109 	struct xe_bo *bo = gem_to_xe_bo(obj);
110 
111 	xe_bo_unpin_external(bo);
112 }
113 
114 static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
115 				       enum dma_data_direction dir)
116 {
117 	struct dma_buf *dma_buf = attach->dmabuf;
118 	struct drm_gem_object *obj = dma_buf->priv;
119 	struct xe_bo *bo = gem_to_xe_bo(obj);
120 	struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
121 	struct sg_table *sgt;
122 	int r = 0;
123 
124 	if (xe_bo_is_purged(bo))
125 		return ERR_PTR(-ENOENT);
126 
127 	if (!attach->peer2peer && !xe_bo_can_migrate(bo, XE_PL_TT))
128 		return ERR_PTR(-EOPNOTSUPP);
129 
130 	if (!xe_bo_is_pinned(bo)) {
131 		if (!attach->peer2peer)
132 			r = xe_bo_migrate(bo, XE_PL_TT, NULL, exec);
133 		else
134 			r = xe_bo_validate(bo, NULL, false, exec);
135 		if (r)
136 			return ERR_PTR(r);
137 	}
138 
139 	switch (bo->ttm.resource->mem_type) {
140 	case XE_PL_TT:
141 		sgt = drm_prime_pages_to_sg(obj->dev,
142 					    bo->ttm.ttm->pages,
143 					    obj->size >> PAGE_SHIFT);
144 		if (IS_ERR(sgt))
145 			return sgt;
146 
147 		if (dma_map_sgtable(attach->dev, sgt, dir,
148 				    DMA_ATTR_SKIP_CPU_SYNC))
149 			goto error_free;
150 		break;
151 
152 	case XE_PL_VRAM0:
153 	case XE_PL_VRAM1:
154 		r = xe_ttm_vram_mgr_alloc_sgt(xe_bo_device(bo),
155 					      bo->ttm.resource, 0,
156 					      bo->ttm.base.size, attach->dev,
157 					      dir, &sgt);
158 		if (r)
159 			return ERR_PTR(r);
160 		break;
161 	default:
162 		return ERR_PTR(-EINVAL);
163 	}
164 
165 	return sgt;
166 
167 error_free:
168 	sg_free_table(sgt);
169 	kfree(sgt);
170 	return ERR_PTR(-EBUSY);
171 }
172 
173 static void xe_dma_buf_unmap(struct dma_buf_attachment *attach,
174 			     struct sg_table *sgt,
175 			     enum dma_data_direction dir)
176 {
177 	if (sg_page(sgt->sgl)) {
178 		dma_unmap_sgtable(attach->dev, sgt, dir, 0);
179 		sg_free_table(sgt);
180 		kfree(sgt);
181 	} else {
182 		xe_ttm_vram_mgr_free_sgt(attach->dev, dir, sgt);
183 	}
184 }
185 
186 static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
187 				       enum dma_data_direction direction)
188 {
189 	struct drm_gem_object *obj = dma_buf->priv;
190 	struct xe_bo *bo = gem_to_xe_bo(obj);
191 	bool reads =  (direction == DMA_BIDIRECTIONAL ||
192 		       direction == DMA_FROM_DEVICE);
193 	struct xe_validation_ctx ctx;
194 	struct drm_exec exec;
195 	int ret = 0;
196 
197 	if (!reads)
198 		return 0;
199 
200 	/* Can we do interruptible lock here? */
201 	xe_validation_guard(&ctx, &xe_bo_device(bo)->val, &exec, (struct xe_val_flags) {}, ret) {
202 		ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
203 		drm_exec_retry_on_contention(&exec);
204 		if (ret)
205 			break;
206 
207 		ret = xe_bo_migrate(bo, XE_PL_TT, NULL, &exec);
208 		drm_exec_retry_on_contention(&exec);
209 		xe_validation_retry_on_oom(&ctx, &ret);
210 	}
211 
212 	/* If we failed, cpu-access takes place in current placement. */
213 	return 0;
214 }
215 
216 static void xe_dma_buf_release(struct dma_buf *dmabuf)
217 {
218 	struct drm_gem_object *obj = dmabuf->priv;
219 	struct xe_bo *bo = gem_to_xe_bo(obj);
220 
221 	xe_bo_lock(bo, false);
222 	xe_bo_willneed_put_locked(bo);
223 	xe_bo_unlock(bo);
224 
225 	drm_gem_dmabuf_release(dmabuf);
226 }
227 
228 static const struct dma_buf_ops xe_dmabuf_ops = {
229 	.attach = xe_dma_buf_attach,
230 	.detach = xe_dma_buf_detach,
231 	.pin = xe_dma_buf_pin,
232 	.unpin = xe_dma_buf_unpin,
233 	.map_dma_buf = xe_dma_buf_map,
234 	.unmap_dma_buf = xe_dma_buf_unmap,
235 	.release = xe_dma_buf_release,
236 	.begin_cpu_access = xe_dma_buf_begin_cpu_access,
237 	.mmap = drm_gem_dmabuf_mmap,
238 	.vmap = drm_gem_dmabuf_vmap,
239 	.vunmap = drm_gem_dmabuf_vunmap,
240 };
241 
242 struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags)
243 {
244 	struct xe_bo *bo = gem_to_xe_bo(obj);
245 	struct dma_buf *buf;
246 	struct ttm_operation_ctx ctx = {
247 		.interruptible = true,
248 		.no_wait_gpu = true,
249 		/* We opt to avoid OOM on system pages allocations */
250 		.gfp_retry_mayfail = true,
251 		.allow_res_evict = false,
252 	};
253 	int ret;
254 
255 	if (bo->vm)
256 		return ERR_PTR(-EPERM);
257 
258 	/*
259 	 * Reject exporting purgeable BOs. DONTNEED BOs can be purged
260 	 * at any time, making the exported dma-buf unusable. Purged BOs
261 	 * have no backing store and are permanently invalid.
262 	 */
263 	ret = xe_bo_lock(bo, true);
264 	if (ret)
265 		return ERR_PTR(ret);
266 
267 	if (xe_bo_madv_is_dontneed(bo)) {
268 		ret = -EBUSY;
269 		goto out_unlock;
270 	}
271 
272 	if (xe_bo_is_purged(bo)) {
273 		ret = -EINVAL;
274 		goto out_unlock;
275 	}
276 
277 	xe_bo_willneed_get_locked(bo);
278 	xe_bo_unlock(bo);
279 
280 	ret = ttm_bo_setup_export(&bo->ttm, &ctx);
281 	if (ret)
282 		goto out_put;
283 
284 	buf = drm_gem_prime_export(obj, flags);
285 	if (IS_ERR(buf)) {
286 		ret = PTR_ERR(buf);
287 		goto out_put;
288 	}
289 
290 	buf->ops = &xe_dmabuf_ops;
291 	return buf;
292 
293 out_put:
294 	xe_bo_lock(bo, false);
295 	xe_bo_willneed_put_locked(bo);
296 out_unlock:
297 	xe_bo_unlock(bo);
298 	return ERR_PTR(ret);
299 }
300 
301 static struct drm_gem_object *
302 xe_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
303 {
304 	struct dma_resv *resv = dma_buf->resv;
305 	struct xe_device *xe = to_xe_device(dev);
306 	struct xe_validation_ctx ctx;
307 	struct drm_gem_object *dummy_obj;
308 	struct drm_exec exec;
309 	struct xe_bo *bo;
310 	int ret = 0;
311 
312 	dummy_obj = drm_gpuvm_resv_object_alloc(&xe->drm);
313 	if (!dummy_obj)
314 		return ERR_PTR(-ENOMEM);
315 
316 	dummy_obj->resv = resv;
317 	xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {}, ret) {
318 		ret = drm_exec_lock_obj(&exec, dummy_obj);
319 		drm_exec_retry_on_contention(&exec);
320 		if (ret)
321 			break;
322 
323 		bo = xe_bo_init_locked(xe, NULL, NULL, resv, NULL, dma_buf->size,
324 				       0, /* Will require 1way or 2way for vm_bind */
325 				       ttm_bo_type_sg, XE_BO_FLAG_SYSTEM, dma_buf, &exec);
326 		drm_exec_retry_on_contention(&exec);
327 		if (IS_ERR(bo)) {
328 			ret = PTR_ERR(bo);
329 			xe_validation_retry_on_oom(&ctx, &ret);
330 			break;
331 		}
332 	}
333 	drm_gem_object_put(dummy_obj);
334 
335 	return ret ? ERR_PTR(ret) : &bo->ttm.base;
336 }
337 
338 static void xe_dma_buf_move_notify(struct dma_buf_attachment *attach)
339 {
340 	struct drm_gem_object *obj = attach->importer_priv;
341 	struct xe_bo *bo = gem_to_xe_bo(obj);
342 	struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
343 
344 	XE_WARN_ON(xe_bo_evict(bo, exec));
345 }
346 
347 static const struct dma_buf_attach_ops xe_dma_buf_attach_ops = {
348 	.allow_peer2peer = true,
349 	.invalidate_mappings = xe_dma_buf_move_notify
350 };
351 
352 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
353 
354 struct dma_buf_test_params {
355 	struct xe_test_priv base;
356 	const struct dma_buf_attach_ops *attach_ops;
357 	bool force_different_devices;
358 	u32 mem_mask;
359 };
360 
361 #define to_dma_buf_test_params(_priv) \
362 	container_of(_priv, struct dma_buf_test_params, base)
363 #endif
364 
365 struct drm_gem_object *xe_gem_prime_import(struct drm_device *dev,
366 					   struct dma_buf *dma_buf)
367 {
368 	XE_TEST_DECLARE(struct dma_buf_test_params *test =
369 			to_dma_buf_test_params
370 			(xe_cur_kunit_priv(XE_TEST_LIVE_DMA_BUF));)
371 	const struct dma_buf_attach_ops *attach_ops;
372 	struct dma_buf_attachment *attach;
373 	struct drm_gem_object *obj;
374 
375 	if (dma_buf->ops == &xe_dmabuf_ops) {
376 		obj = dma_buf->priv;
377 		if (obj->dev == dev &&
378 		    !XE_TEST_ONLY(test && test->force_different_devices)) {
379 			/*
380 			 * Importing dmabuf exported from out own gem increases
381 			 * refcount on gem itself instead of f_count of dmabuf.
382 			 */
383 			drm_gem_object_get(obj);
384 			return obj;
385 		}
386 	}
387 
388 	/*
389 	 * This needs to happen before the attach, since it will create a new
390 	 * attachment for this, and add it to the list of attachments, at which
391 	 * point it is globally visible, and at any point the export side can
392 	 * call into on invalidate_mappings callback, which require a working
393 	 * object.
394 	 */
395 	obj = xe_dma_buf_create_obj(dev, dma_buf);
396 	if (IS_ERR(obj))
397 		return obj;
398 
399 	attach_ops = &xe_dma_buf_attach_ops;
400 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
401 	if (test)
402 		attach_ops = test->attach_ops;
403 #endif
404 
405 	attach = dma_buf_dynamic_attach(dma_buf, dev->dev, attach_ops, obj);
406 	if (IS_ERR(attach)) {
407 		xe_bo_put(gem_to_xe_bo(obj));
408 		return ERR_CAST(attach);
409 	}
410 
411 	get_dma_buf(dma_buf);
412 	obj->import_attach = attach;
413 	return obj;
414 }
415 
416 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
417 #include "tests/xe_dma_buf.c"
418 #endif
419