18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2011 Red Hat 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 * Authors: Dave Airlie
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/dma-buf.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "nouveau_drv.h"
288c2ecf20Sopenharmony_ci#include "nouveau_gem.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
338c2ecf20Sopenharmony_ci	int npages = nvbo->bo.num_pages;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	return drm_prime_pages_to_sg(obj->dev, nvbo->bo.ttm->pages, npages);
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_civoid *nouveau_gem_prime_vmap(struct drm_gem_object *obj)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
418c2ecf20Sopenharmony_ci	int ret;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages,
448c2ecf20Sopenharmony_ci			  &nvbo->dma_buf_vmap);
458c2ecf20Sopenharmony_ci	if (ret)
468c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return nvbo->dma_buf_vmap.virtual;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_civoid nouveau_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	ttm_bo_kunmap(&nvbo->dma_buf_vmap);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev,
598c2ecf20Sopenharmony_ci							 struct dma_buf_attachment *attach,
608c2ecf20Sopenharmony_ci							 struct sg_table *sg)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
638c2ecf20Sopenharmony_ci	struct drm_gem_object *obj;
648c2ecf20Sopenharmony_ci	struct nouveau_bo *nvbo;
658c2ecf20Sopenharmony_ci	struct dma_resv *robj = attach->dmabuf->resv;
668c2ecf20Sopenharmony_ci	u64 size = attach->dmabuf->size;
678c2ecf20Sopenharmony_ci	int align = 0;
688c2ecf20Sopenharmony_ci	int ret;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	dma_resv_lock(robj, NULL);
718c2ecf20Sopenharmony_ci	nvbo = nouveau_bo_alloc(&drm->client, &size, &align,
728c2ecf20Sopenharmony_ci				NOUVEAU_GEM_DOMAIN_GART, 0, 0);
738c2ecf20Sopenharmony_ci	if (IS_ERR(nvbo)) {
748c2ecf20Sopenharmony_ci		obj = ERR_CAST(nvbo);
758c2ecf20Sopenharmony_ci		goto unlock;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* Initialize the embedded gem-object. We return a single gem-reference
818c2ecf20Sopenharmony_ci	 * to the caller, instead of a normal nouveau_bo ttm reference. */
828c2ecf20Sopenharmony_ci	ret = drm_gem_object_init(dev, &nvbo->bo.base, size);
838c2ecf20Sopenharmony_ci	if (ret) {
848c2ecf20Sopenharmony_ci		nouveau_bo_ref(NULL, &nvbo);
858c2ecf20Sopenharmony_ci		obj = ERR_PTR(-ENOMEM);
868c2ecf20Sopenharmony_ci		goto unlock;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	ret = nouveau_bo_init(nvbo, size, align, NOUVEAU_GEM_DOMAIN_GART,
908c2ecf20Sopenharmony_ci			      sg, robj);
918c2ecf20Sopenharmony_ci	if (ret) {
928c2ecf20Sopenharmony_ci		obj = ERR_PTR(ret);
938c2ecf20Sopenharmony_ci		goto unlock;
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	obj = &nvbo->bo.base;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciunlock:
998c2ecf20Sopenharmony_ci	dma_resv_unlock(robj);
1008c2ecf20Sopenharmony_ci	return obj;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciint nouveau_gem_prime_pin(struct drm_gem_object *obj)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
1068c2ecf20Sopenharmony_ci	int ret;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* pin buffer into GTT */
1098c2ecf20Sopenharmony_ci	ret = nouveau_bo_pin(nvbo, NOUVEAU_GEM_DOMAIN_GART, false);
1108c2ecf20Sopenharmony_ci	if (ret)
1118c2ecf20Sopenharmony_ci		return -EINVAL;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
1148c2ecf20Sopenharmony_ci	if (ret)
1158c2ecf20Sopenharmony_ci		goto error;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (nvbo->bo.moving)
1188c2ecf20Sopenharmony_ci		ret = dma_fence_wait(nvbo->bo.moving, true);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	ttm_bo_unreserve(&nvbo->bo);
1218c2ecf20Sopenharmony_ci	if (ret)
1228c2ecf20Sopenharmony_ci		goto error;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return ret;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cierror:
1278c2ecf20Sopenharmony_ci	nouveau_bo_unpin(nvbo);
1288c2ecf20Sopenharmony_ci	return ret;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_civoid nouveau_gem_prime_unpin(struct drm_gem_object *obj)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	nouveau_bo_unpin(nvbo);
1368c2ecf20Sopenharmony_ci}
137