162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2012 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * based on nouveau_prime.c
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * Authors: Alex Deucher
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include <linux/dma-buf.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <drm/drm_prime.h>
3062306a36Sopenharmony_ci#include <drm/radeon_drm.h>
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#include <drm/ttm/ttm_tt.h>
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include "radeon.h"
3562306a36Sopenharmony_ci#include "radeon_prime.h"
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct sg_table *radeon_gem_prime_get_sg_table(struct drm_gem_object *obj)
3862306a36Sopenharmony_ci{
3962306a36Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(obj);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	return drm_prime_pages_to_sg(obj->dev, bo->tbo.ttm->pages,
4262306a36Sopenharmony_ci				     bo->tbo.ttm->num_pages);
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistruct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev,
4662306a36Sopenharmony_ci							struct dma_buf_attachment *attach,
4762306a36Sopenharmony_ci							struct sg_table *sg)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	struct dma_resv *resv = attach->dmabuf->resv;
5062306a36Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
5162306a36Sopenharmony_ci	struct radeon_bo *bo;
5262306a36Sopenharmony_ci	int ret;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	dma_resv_lock(resv, NULL);
5562306a36Sopenharmony_ci	ret = radeon_bo_create(rdev, attach->dmabuf->size, PAGE_SIZE, false,
5662306a36Sopenharmony_ci			       RADEON_GEM_DOMAIN_GTT, 0, sg, resv, &bo);
5762306a36Sopenharmony_ci	dma_resv_unlock(resv);
5862306a36Sopenharmony_ci	if (ret)
5962306a36Sopenharmony_ci		return ERR_PTR(ret);
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	bo->tbo.base.funcs = &radeon_gem_object_funcs;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	mutex_lock(&rdev->gem.mutex);
6462306a36Sopenharmony_ci	list_add_tail(&bo->list, &rdev->gem.objects);
6562306a36Sopenharmony_ci	mutex_unlock(&rdev->gem.mutex);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	bo->prime_shared_count = 1;
6862306a36Sopenharmony_ci	return &bo->tbo.base;
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciint radeon_gem_prime_pin(struct drm_gem_object *obj)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(obj);
7462306a36Sopenharmony_ci	int ret = 0;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	ret = radeon_bo_reserve(bo, false);
7762306a36Sopenharmony_ci	if (unlikely(ret != 0))
7862306a36Sopenharmony_ci		return ret;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	/* pin buffer into GTT */
8162306a36Sopenharmony_ci	ret = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_GTT, NULL);
8262306a36Sopenharmony_ci	if (likely(ret == 0))
8362306a36Sopenharmony_ci		bo->prime_shared_count++;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	radeon_bo_unreserve(bo);
8662306a36Sopenharmony_ci	return ret;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_civoid radeon_gem_prime_unpin(struct drm_gem_object *obj)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(obj);
9262306a36Sopenharmony_ci	int ret = 0;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	ret = radeon_bo_reserve(bo, false);
9562306a36Sopenharmony_ci	if (unlikely(ret != 0))
9662306a36Sopenharmony_ci		return;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	radeon_bo_unpin(bo);
9962306a36Sopenharmony_ci	if (bo->prime_shared_count)
10062306a36Sopenharmony_ci		bo->prime_shared_count--;
10162306a36Sopenharmony_ci	radeon_bo_unreserve(bo);
10262306a36Sopenharmony_ci}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistruct dma_buf *radeon_gem_prime_export(struct drm_gem_object *gobj,
10662306a36Sopenharmony_ci					int flags)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(gobj);
10962306a36Sopenharmony_ci	if (radeon_ttm_tt_has_userptr(bo->rdev, bo->tbo.ttm))
11062306a36Sopenharmony_ci		return ERR_PTR(-EPERM);
11162306a36Sopenharmony_ci	return drm_gem_prime_export(gobj, flags);
11262306a36Sopenharmony_ci}
113