amdgpu_dma_buf.c 11.6 KB
Newer Older
A
Alex Deucher 已提交
1
/*
2
 * Copyright 2019 Advanced Micro Devices, Inc.
A
Alex Deucher 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * based on nouveau_prime.c
 *
 * Authors: Alex Deucher
 */
26 27 28 29 30 31 32 33

/**
 * DOC: PRIME Buffer Sharing
 *
 * The following callback implementations are used for :ref:`sharing GEM buffer
 * objects between different devices via PRIME <prime_buffer_sharing>`.
 */

A
Alex Deucher 已提交
34
#include "amdgpu.h"
35
#include "amdgpu_display.h"
36
#include "amdgpu_gem.h"
A
Alex Deucher 已提交
37 38
#include <drm/amdgpu_drm.h>
#include <linux/dma-buf.h>
39
#include <linux/dma-fence-array.h>
A
Alex Deucher 已提交
40

41 42 43
/**
 * amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
 * implementation
44
 * @obj: GEM buffer object (BO)
45 46
 *
 * Returns:
47
 * A scatter/gather table for the pinned pages of the BO's memory.
48
 */
A
Alex Deucher 已提交
49 50 51 52 53 54 55 56
struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
{
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
	int npages = bo->tbo.num_pages;

	return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
}

57 58
/**
 * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
59
 * @obj: GEM BO
60
 *
61
 * Sets up an in-kernel virtual mapping of the BO's memory.
62 63 64 65
 *
 * Returns:
 * The virtual address of the mapping or an error pointer.
 */
A
Alex Deucher 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
{
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
	int ret;

	ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
			  &bo->dma_buf_vmap);
	if (ret)
		return ERR_PTR(ret);

	return bo->dma_buf_vmap.virtual;
}

79 80
/**
 * amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
81 82
 * @obj: GEM BO
 * @vaddr: Virtual address (unused)
83
 *
84
 * Tears down the in-kernel virtual mapping of the BO's memory.
85
 */
A
Alex Deucher 已提交
86 87 88 89 90 91 92
void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
{
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);

	ttm_bo_kunmap(&bo->dma_buf_vmap);
}

93 94
/**
 * amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
95 96
 * @obj: GEM BO
 * @vma: Virtual memory area
97
 *
98
 * Sets up a userspace mapping of the BO's memory in the given
99 100 101
 * virtual memory area.
 *
 * Returns:
102
 * 0 on success or a negative error code on failure.
103
 */
104 105
int amdgpu_gem_prime_mmap(struct drm_gem_object *obj,
			  struct vm_area_struct *vma)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
{
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
	unsigned asize = amdgpu_bo_size(bo);
	int ret;

	if (!vma->vm_file)
		return -ENODEV;

	if (adev == NULL)
		return -ENODEV;

	/* Check for valid size. */
	if (asize < vma->vm_end - vma->vm_start)
		return -EINVAL;

	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
	    (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
		return -EPERM;
	}
	vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;

	/* prime mmap does not need to check access, so allow here */
	ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
	if (ret)
		return ret;

	ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
	drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);

	return ret;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
static int
__reservation_object_make_exclusive(struct reservation_object *obj)
{
	struct dma_fence **fences;
	unsigned int count;
	int r;

	if (!reservation_object_get_list(obj)) /* no shared fences to convert */
		return 0;

	r = reservation_object_get_fences_rcu(obj, NULL, &count, &fences);
	if (r)
		return r;

	if (count == 0) {
		/* Now that was unexpected. */
	} else if (count == 1) {
		reservation_object_add_excl_fence(obj, fences[0]);
		dma_fence_put(fences[0]);
		kfree(fences);
	} else {
		struct dma_fence_array *array;

		array = dma_fence_array_create(count, fences,
					       dma_fence_context_alloc(1), 0,
					       false);
		if (!array)
			goto err_fences_put;

		reservation_object_add_excl_fence(obj, &array->base);
		dma_fence_put(&array->base);
	}

	return 0;

err_fences_put:
	while (count--)
		dma_fence_put(fences[count]);
	kfree(fences);
	return -ENOMEM;
}

181
/**
182
 * amdgpu_dma_buf_map_attach - &dma_buf_ops.attach implementation
183
 * @dma_buf: Shared DMA buffer
184 185 186 187 188 189 190
 * @attach: DMA-buf attachment
 *
 * Makes sure that the shared DMA buffer can be accessed by the target device.
 * For now, simply pins it to the GTT domain, where it should be accessible by
 * all DMA devices.
 *
 * Returns:
191
 * 0 on success or a negative error code on failure.
192
 */
193 194
static int amdgpu_dma_buf_map_attach(struct dma_buf *dma_buf,
				     struct dma_buf_attachment *attach)
A
Alex Deucher 已提交
195
{
196
	struct drm_gem_object *obj = dma_buf->priv;
A
Alex Deucher 已提交
197
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
198
	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
199
	long r;
A
Alex Deucher 已提交
200

201
	r = drm_gem_map_attach(dma_buf, attach);
202 203 204 205 206 207
	if (r)
		return r;

	r = amdgpu_bo_reserve(bo, false);
	if (unlikely(r != 0))
		goto error_detach;
A
Alex Deucher 已提交
208

209

210
	if (attach->dev->driver != adev->dev->driver) {
211
		/*
212 213 214 215 216 217
		 * We only create shared fences for internal use, but importers
		 * of the dmabuf rely on exclusive fences for implicitly
		 * tracking write hazards. As any of the current fences may
		 * correspond to a write, we need to convert all existing
		 * fences on the reservation object into a single exclusive
		 * fence.
218
		 */
219
		r = __reservation_object_make_exclusive(bo->tbo.base.resv);
220
		if (r)
221
			goto error_unreserve;
222 223
	}

A
Alex Deucher 已提交
224
	/* pin buffer into GTT */
225
	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
226 227 228
	if (r)
		goto error_unreserve;

229
	if (attach->dev->driver != adev->dev->driver)
230 231
		bo->prime_shared_count++;

232
error_unreserve:
A
Alex Deucher 已提交
233
	amdgpu_bo_unreserve(bo);
234 235 236 237 238

error_detach:
	if (r)
		drm_gem_map_detach(dma_buf, attach);
	return r;
A
Alex Deucher 已提交
239 240
}

241
/**
242
 * amdgpu_dma_buf_map_detach - &dma_buf_ops.detach implementation
243
 * @dma_buf: Shared DMA buffer
244 245 246
 * @attach: DMA-buf attachment
 *
 * This is called when a shared DMA buffer no longer needs to be accessible by
247
 * another device. For now, simply unpins the buffer from GTT.
248
 */
249 250
static void amdgpu_dma_buf_map_detach(struct dma_buf *dma_buf,
				      struct dma_buf_attachment *attach)
A
Alex Deucher 已提交
251
{
252
	struct drm_gem_object *obj = dma_buf->priv;
A
Alex Deucher 已提交
253
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
254
	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
A
Alex Deucher 已提交
255 256
	int ret = 0;

257
	ret = amdgpu_bo_reserve(bo, true);
A
Alex Deucher 已提交
258
	if (unlikely(ret != 0))
259
		goto error;
A
Alex Deucher 已提交
260 261

	amdgpu_bo_unpin(bo);
262
	if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
263
		bo->prime_shared_count--;
A
Alex Deucher 已提交
264
	amdgpu_bo_unreserve(bo);
265 266 267

error:
	drm_gem_map_detach(dma_buf, attach);
A
Alex Deucher 已提交
268 269
}

270
/**
271
 * amdgpu_dma_buf_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
272 273
 * @dma_buf: Shared DMA buffer
 * @direction: Direction of DMA transfer
274 275 276 277 278 279
 *
 * This is called before CPU access to the shared DMA buffer's memory. If it's
 * a read access, the buffer is moved to the GTT domain if possible, for optimal
 * CPU read performance.
 *
 * Returns:
280
 * 0 on success or a negative error code on failure.
281
 */
282 283
static int amdgpu_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
					   enum dma_data_direction direction)
284 285 286 287
{
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
	struct ttm_operation_ctx ctx = { true, false };
288
	u32 domain = amdgpu_display_supported_domains(adev);
289 290 291 292 293 294 295 296 297 298 299 300 301
	int ret;
	bool reads = (direction == DMA_BIDIRECTIONAL ||
		      direction == DMA_FROM_DEVICE);

	if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
		return 0;

	/* move to gtt */
	ret = amdgpu_bo_reserve(bo, false);
	if (unlikely(ret != 0))
		return ret;

	if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
302
		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
303 304 305 306 307 308 309
		ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
	}

	amdgpu_bo_unreserve(bo);
	return ret;
}

310
const struct dma_buf_ops amdgpu_dmabuf_ops = {
311 312
	.attach = amdgpu_dma_buf_map_attach,
	.detach = amdgpu_dma_buf_map_detach,
313 314 315
	.map_dma_buf = drm_gem_map_dma_buf,
	.unmap_dma_buf = drm_gem_unmap_dma_buf,
	.release = drm_gem_dmabuf_release,
316
	.begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
317 318 319 320 321
	.mmap = drm_gem_dmabuf_mmap,
	.vmap = drm_gem_dmabuf_vmap,
	.vunmap = drm_gem_dmabuf_vunmap,
};

322 323 324
/**
 * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
 * @dev: DRM device
325 326
 * @gobj: GEM BO
 * @flags: Flags such as DRM_CLOEXEC and DRM_RDWR.
327
 *
328
 * The main work is done by the &drm_gem_prime_export helper.
329 330
 *
 * Returns:
331
 * Shared DMA buffer representing the GEM BO from the given device.
332
 */
333
struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
A
Alex Deucher 已提交
334 335 336
					int flags)
{
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
337
	struct dma_buf *buf;
A
Alex Deucher 已提交
338

339 340
	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
	    bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
A
Alex Deucher 已提交
341 342
		return ERR_PTR(-EPERM);

343
	buf = drm_gem_prime_export(gobj, flags);
344
	if (!IS_ERR(buf)) {
345
		buf->file->f_mapping = gobj->dev->anon_inode->i_mapping;
346 347 348
		buf->ops = &amdgpu_dmabuf_ops;
	}

349
	return buf;
A
Alex Deucher 已提交
350
}
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
/**
 * amdgpu_gem_prime_import_sg_table - &drm_driver.gem_prime_import_sg_table
 * implementation
 * @dev: DRM device
 * @attach: DMA-buf attachment
 * @sg: Scatter/gather table
 *
 * Imports shared DMA buffer memory exported by another device.
 *
 * Returns:
 * A new GEM BO of the given DRM device, representing the memory
 * described by the given DMA-buf attachment and scatter/gather table.
 */
struct drm_gem_object *
amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
				 struct dma_buf_attachment *attach,
				 struct sg_table *sg)
{
	struct reservation_object *resv = attach->dmabuf->resv;
	struct amdgpu_device *adev = dev->dev_private;
	struct amdgpu_bo *bo;
	struct amdgpu_bo_param bp;
	int ret;

	memset(&bp, 0, sizeof(bp));
	bp.size = attach->dmabuf->size;
	bp.byte_align = PAGE_SIZE;
	bp.domain = AMDGPU_GEM_DOMAIN_CPU;
	bp.flags = 0;
	bp.type = ttm_bo_type_sg;
	bp.resv = resv;
383
	reservation_object_lock(resv, NULL);
384 385 386 387 388 389 390 391 392 393 394
	ret = amdgpu_bo_create(adev, &bp, &bo);
	if (ret)
		goto error;

	bo->tbo.sg = sg;
	bo->tbo.ttm->sg = sg;
	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
	if (attach->dmabuf->ops != &amdgpu_dmabuf_ops)
		bo->prime_shared_count = 1;

395
	reservation_object_unlock(resv);
396
	return &bo->tbo.base;
397 398

error:
399
	reservation_object_unlock(resv);
400 401 402
	return ERR_PTR(ret);
}

403 404 405 406 407 408 409 410 411
/**
 * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
 * @dev: DRM device
 * @dma_buf: Shared DMA buffer
 *
 * The main work is done by the &drm_gem_prime_import helper, which in turn
 * uses &amdgpu_gem_prime_import_sg_table.
 *
 * Returns:
412
 * GEM BO representing the shared DMA buffer for the given device.
413
 */
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
					    struct dma_buf *dma_buf)
{
	struct drm_gem_object *obj;

	if (dma_buf->ops == &amdgpu_dmabuf_ops) {
		obj = dma_buf->priv;
		if (obj->dev == dev) {
			/*
			 * Importing dmabuf exported from out own gem increases
			 * refcount on gem itself instead of f_count of dmabuf.
			 */
			drm_gem_object_get(obj);
			return obj;
		}
	}

	return drm_gem_prime_import(dev, dma_buf);
}