18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2012 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * based on nouveau_prime.c
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Authors: Alex Deucher
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/dma-buf.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <drm/drm_prime.h>
308c2ecf20Sopenharmony_ci#include <drm/radeon_drm.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "radeon.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct sg_table *radeon_gem_prime_get_sg_table(struct drm_gem_object *obj)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(obj);
378c2ecf20Sopenharmony_ci	int npages = bo->tbo.num_pages;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	return drm_prime_pages_to_sg(obj->dev, bo->tbo.ttm->pages, npages);
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_civoid *radeon_gem_prime_vmap(struct drm_gem_object *obj)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(obj);
458c2ecf20Sopenharmony_ci	int ret;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
488c2ecf20Sopenharmony_ci			  &bo->dma_buf_vmap);
498c2ecf20Sopenharmony_ci	if (ret)
508c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	return bo->dma_buf_vmap.virtual;
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_civoid radeon_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(obj);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	ttm_bo_kunmap(&bo->dma_buf_vmap);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev,
638c2ecf20Sopenharmony_ci							struct dma_buf_attachment *attach,
648c2ecf20Sopenharmony_ci							struct sg_table *sg)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct dma_resv *resv = attach->dmabuf->resv;
678c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
688c2ecf20Sopenharmony_ci	struct radeon_bo *bo;
698c2ecf20Sopenharmony_ci	int ret;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	dma_resv_lock(resv, NULL);
728c2ecf20Sopenharmony_ci	ret = radeon_bo_create(rdev, attach->dmabuf->size, PAGE_SIZE, false,
738c2ecf20Sopenharmony_ci			       RADEON_GEM_DOMAIN_GTT, 0, sg, resv, &bo);
748c2ecf20Sopenharmony_ci	dma_resv_unlock(resv);
758c2ecf20Sopenharmony_ci	if (ret)
768c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	mutex_lock(&rdev->gem.mutex);
798c2ecf20Sopenharmony_ci	list_add_tail(&bo->list, &rdev->gem.objects);
808c2ecf20Sopenharmony_ci	mutex_unlock(&rdev->gem.mutex);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	bo->prime_shared_count = 1;
838c2ecf20Sopenharmony_ci	return &bo->tbo.base;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ciint radeon_gem_prime_pin(struct drm_gem_object *obj)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(obj);
898c2ecf20Sopenharmony_ci	int ret = 0;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	ret = radeon_bo_reserve(bo, false);
928c2ecf20Sopenharmony_ci	if (unlikely(ret != 0))
938c2ecf20Sopenharmony_ci		return ret;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* pin buffer into GTT */
968c2ecf20Sopenharmony_ci	ret = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_GTT, NULL);
978c2ecf20Sopenharmony_ci	if (unlikely(ret))
988c2ecf20Sopenharmony_ci		goto error;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (bo->tbo.moving) {
1018c2ecf20Sopenharmony_ci		ret = dma_fence_wait(bo->tbo.moving, false);
1028c2ecf20Sopenharmony_ci		if (unlikely(ret)) {
1038c2ecf20Sopenharmony_ci			radeon_bo_unpin(bo);
1048c2ecf20Sopenharmony_ci			goto error;
1058c2ecf20Sopenharmony_ci		}
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	bo->prime_shared_count++;
1098c2ecf20Sopenharmony_cierror:
1108c2ecf20Sopenharmony_ci	radeon_bo_unreserve(bo);
1118c2ecf20Sopenharmony_ci	return ret;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_civoid radeon_gem_prime_unpin(struct drm_gem_object *obj)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(obj);
1178c2ecf20Sopenharmony_ci	int ret = 0;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	ret = radeon_bo_reserve(bo, false);
1208c2ecf20Sopenharmony_ci	if (unlikely(ret != 0))
1218c2ecf20Sopenharmony_ci		return;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	radeon_bo_unpin(bo);
1248c2ecf20Sopenharmony_ci	if (bo->prime_shared_count)
1258c2ecf20Sopenharmony_ci		bo->prime_shared_count--;
1268c2ecf20Sopenharmony_ci	radeon_bo_unreserve(bo);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistruct dma_buf *radeon_gem_prime_export(struct drm_gem_object *gobj,
1318c2ecf20Sopenharmony_ci					int flags)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct radeon_bo *bo = gem_to_radeon_bo(gobj);
1348c2ecf20Sopenharmony_ci	if (radeon_ttm_tt_has_userptr(bo->rdev, bo->tbo.ttm))
1358c2ecf20Sopenharmony_ci		return ERR_PTR(-EPERM);
1368c2ecf20Sopenharmony_ci	return drm_gem_prime_export(gobj, flags);
1378c2ecf20Sopenharmony_ci}
138