amdgpu_prime.c 11.1 KB
Newer Older
A
Alex Deucher 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright 2012 Advanced Micro Devices, Inc.
 *
 * 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 35 36
#include <drm/drmP.h>

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

41 42
static const struct dma_buf_ops amdgpu_dmabuf_ops;

43 44 45 46 47 48 49 50
/**
 * amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
 * implementation
 * @obj: GEM buffer object
 *
 * Returns:
 * A scatter/gather table for the pinned pages of the buffer object's memory.
 */
A
Alex Deucher 已提交
51 52 53 54 55 56 57 58
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);
}

59 60 61 62 63 64 65 66 67
/**
 * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
 * @obj: GEM buffer object
 *
 * Sets up an in-kernel virtual mapping of the buffer object's memory.
 *
 * Returns:
 * The virtual address of the mapping or an error pointer.
 */
A
Alex Deucher 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
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;
}

81 82 83 84 85 86 87
/**
 * amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
 * @obj: GEM buffer object
 * @vaddr: virtual address (unused)
 *
 * Tears down the in-kernel virtual mapping of the buffer object's memory.
 */
A
Alex Deucher 已提交
88 89 90 91 92 93 94
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);
}

95 96 97 98 99 100 101 102 103 104 105
/**
 * amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
 * @obj: GEM buffer object
 * @vma: virtual memory area
 *
 * Sets up a userspace mapping of the buffer object's memory in the given
 * virtual memory area.
 *
 * Returns:
 * 0 on success or negative error code.
 */
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 139
int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
{
	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;
}

140 141 142 143 144 145 146 147 148 149 150 151 152
/**
 * 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
 *
 * Import shared DMA buffer memory exported by another device.
 *
 * Returns:
 * A new GEM buffer object of the given DRM device, representing the memory
 * described by the given DMA-buf attachment and scatter/gather table.
 */
153 154 155 156
struct drm_gem_object *
amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
				 struct dma_buf_attachment *attach,
				 struct sg_table *sg)
A
Alex Deucher 已提交
157
{
158
	struct reservation_object *resv = attach->dmabuf->resv;
A
Alex Deucher 已提交
159 160
	struct amdgpu_device *adev = dev->dev_private;
	struct amdgpu_bo *bo;
161
	struct amdgpu_bo_param bp;
A
Alex Deucher 已提交
162 163
	int ret;

164 165 166 167 168 169 170
	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;
171
	ww_mutex_lock(&resv->lock, NULL);
172
	ret = amdgpu_bo_create(adev, &bp, &bo);
A
Alex Deucher 已提交
173
	if (ret)
174 175
		goto error;

176 177
	bo->tbo.sg = sg;
	bo->tbo.ttm->sg = sg;
178 179
	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
180 181
	if (attach->dmabuf->ops != &amdgpu_dmabuf_ops)
		bo->prime_shared_count = 1;
A
Alex Deucher 已提交
182

183
	ww_mutex_unlock(&resv->lock);
A
Alex Deucher 已提交
184
	return &bo->gem_base;
185 186 187 188

error:
	ww_mutex_unlock(&resv->lock);
	return ERR_PTR(ret);
A
Alex Deucher 已提交
189 190
}

191 192 193 194 195 196 197 198 199 200 201 202
/**
 * amdgpu_gem_map_attach - &dma_buf_ops.attach implementation
 * @dma_buf: shared DMA buffer
 * @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:
 * 0 on success or negative error code.
 */
203 204
static int amdgpu_gem_map_attach(struct dma_buf *dma_buf,
				 struct dma_buf_attachment *attach)
A
Alex Deucher 已提交
205
{
206
	struct drm_gem_object *obj = dma_buf->priv;
A
Alex Deucher 已提交
207
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
208
	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
209
	long r;
A
Alex Deucher 已提交
210

211
	r = drm_gem_map_attach(dma_buf, attach);
212 213 214 215 216 217
	if (r)
		return r;

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

219

220
	if (attach->dev->driver != adev->dev->driver) {
221 222 223 224 225 226 227 228 229 230 231
		/*
		 * Wait for all shared fences to complete before we switch to future
		 * use of exclusive fence on this prime shared bo.
		 */
		r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
							true, false,
							MAX_SCHEDULE_TIMEOUT);
		if (unlikely(r < 0)) {
			DRM_DEBUG_PRIME("Fence wait failed: %li\n", r);
			goto error_unreserve;
		}
232 233
	}

A
Alex Deucher 已提交
234
	/* pin buffer into GTT */
235
	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
236 237 238
	if (r)
		goto error_unreserve;

239
	if (attach->dev->driver != adev->dev->driver)
240 241
		bo->prime_shared_count++;

242
error_unreserve:
A
Alex Deucher 已提交
243
	amdgpu_bo_unreserve(bo);
244 245 246 247 248

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

251 252 253 254 255 256 257 258
/**
 * amdgpu_gem_map_detach - &dma_buf_ops.detach implementation
 * @dma_buf: shared DMA buffer
 * @attach: DMA-buf attachment
 *
 * This is called when a shared DMA buffer no longer needs to be accessible by
 * the other device. For now, simply unpins the buffer from GTT.
 */
259 260
static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
				  struct dma_buf_attachment *attach)
A
Alex Deucher 已提交
261
{
262
	struct drm_gem_object *obj = dma_buf->priv;
A
Alex Deucher 已提交
263
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
264
	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
A
Alex Deucher 已提交
265 266
	int ret = 0;

267
	ret = amdgpu_bo_reserve(bo, true);
A
Alex Deucher 已提交
268
	if (unlikely(ret != 0))
269
		goto error;
A
Alex Deucher 已提交
270 271

	amdgpu_bo_unpin(bo);
272
	if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
273
		bo->prime_shared_count--;
A
Alex Deucher 已提交
274
	amdgpu_bo_unreserve(bo);
275 276 277

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

280 281 282 283 284 285 286
/**
 * amdgpu_gem_prime_res_obj - &drm_driver.gem_prime_res_obj implementation
 * @obj: GEM buffer object
 *
 * Returns:
 * The buffer object's reservation object.
 */
A
Alex Deucher 已提交
287 288 289 290 291 292 293
struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *obj)
{
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);

	return bo->tbo.resv;
}

294 295 296 297 298 299 300 301 302 303 304 305
/**
 * amdgpu_gem_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
 * @dma_buf: shared DMA buffer
 * @direction: direction of DMA transfer
 *
 * 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:
 * 0 on success or negative error code.
 */
306 307 308 309 310 311
static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf,
				       enum dma_data_direction direction)
{
	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 };
312
	u32 domain = amdgpu_display_supported_domains(adev);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	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)) {
		amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
		ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
	}

	amdgpu_bo_unreserve(bo);
	return ret;
}

static const struct dma_buf_ops amdgpu_dmabuf_ops = {
335 336
	.attach = amdgpu_gem_map_attach,
	.detach = amdgpu_gem_map_detach,
337 338 339 340 341 342 343 344 345 346 347
	.map_dma_buf = drm_gem_map_dma_buf,
	.unmap_dma_buf = drm_gem_unmap_dma_buf,
	.release = drm_gem_dmabuf_release,
	.begin_cpu_access = amdgpu_gem_begin_cpu_access,
	.map = drm_gem_dmabuf_kmap,
	.unmap = drm_gem_dmabuf_kunmap,
	.mmap = drm_gem_dmabuf_mmap,
	.vmap = drm_gem_dmabuf_vmap,
	.vunmap = drm_gem_dmabuf_vunmap,
};

348 349 350 351 352 353 354 355 356 357 358 359
/**
 * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
 * @dev: DRM device
 * @gobj: GEM buffer object
 * @flags: flags like DRM_CLOEXEC and DRM_RDWR
 *
 * The main work is done by the &drm_gem_prime_export helper, which in turn
 * uses &amdgpu_gem_prime_res_obj.
 *
 * Returns:
 * Shared DMA buffer representing the GEM buffer object from the given device.
 */
A
Alex Deucher 已提交
360 361 362 363 364
struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
					struct drm_gem_object *gobj,
					int flags)
{
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
365
	struct dma_buf *buf;
A
Alex Deucher 已提交
366

367 368
	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
	    bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
A
Alex Deucher 已提交
369 370
		return ERR_PTR(-EPERM);

371
	buf = drm_gem_prime_export(dev, gobj, flags);
372
	if (!IS_ERR(buf)) {
373
		buf->file->f_mapping = dev->anon_inode->i_mapping;
374 375 376
		buf->ops = &amdgpu_dmabuf_ops;
	}

377
	return buf;
A
Alex Deucher 已提交
378
}
379

380 381 382 383 384 385 386 387 388 389 390
/**
 * 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:
 * GEM buffer object representing the shared DMA buffer for the given device.
 */
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
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);
}