162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright 2011 Red Hat 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 * Authors: Dave Airlie 2362306a36Sopenharmony_ci */ 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#include <linux/dma-buf.h> 2662306a36Sopenharmony_ci#include <drm/ttm/ttm_tt.h> 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci#include "nouveau_drv.h" 2962306a36Sopenharmony_ci#include "nouveau_gem.h" 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_cistruct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj) 3262306a36Sopenharmony_ci{ 3362306a36Sopenharmony_ci struct nouveau_bo *nvbo = nouveau_gem_object(obj); 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci return drm_prime_pages_to_sg(obj->dev, nvbo->bo.ttm->pages, 3662306a36Sopenharmony_ci nvbo->bo.ttm->num_pages); 3762306a36Sopenharmony_ci} 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_cistruct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev, 4062306a36Sopenharmony_ci struct dma_buf_attachment *attach, 4162306a36Sopenharmony_ci struct sg_table *sg) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(dev); 4462306a36Sopenharmony_ci struct drm_gem_object *obj; 4562306a36Sopenharmony_ci struct nouveau_bo *nvbo; 4662306a36Sopenharmony_ci struct dma_resv *robj = attach->dmabuf->resv; 4762306a36Sopenharmony_ci u64 size = attach->dmabuf->size; 4862306a36Sopenharmony_ci int align = 0; 4962306a36Sopenharmony_ci int ret; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci dma_resv_lock(robj, NULL); 5262306a36Sopenharmony_ci nvbo = nouveau_bo_alloc(&drm->client, &size, &align, 5362306a36Sopenharmony_ci NOUVEAU_GEM_DOMAIN_GART, 0, 0, true); 5462306a36Sopenharmony_ci if (IS_ERR(nvbo)) { 5562306a36Sopenharmony_ci obj = ERR_CAST(nvbo); 5662306a36Sopenharmony_ci goto unlock; 5762306a36Sopenharmony_ci } 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART; 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci nvbo->bo.base.funcs = &nouveau_gem_object_funcs; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci /* Initialize the embedded gem-object. We return a single gem-reference 6462306a36Sopenharmony_ci * to the caller, instead of a normal nouveau_bo ttm reference. */ 6562306a36Sopenharmony_ci ret = drm_gem_object_init(dev, &nvbo->bo.base, size); 6662306a36Sopenharmony_ci if (ret) { 6762306a36Sopenharmony_ci nouveau_bo_ref(NULL, &nvbo); 6862306a36Sopenharmony_ci obj = ERR_PTR(-ENOMEM); 6962306a36Sopenharmony_ci goto unlock; 7062306a36Sopenharmony_ci } 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci ret = nouveau_bo_init(nvbo, size, align, NOUVEAU_GEM_DOMAIN_GART, 7362306a36Sopenharmony_ci sg, robj); 7462306a36Sopenharmony_ci if (ret) { 7562306a36Sopenharmony_ci obj = ERR_PTR(ret); 7662306a36Sopenharmony_ci goto unlock; 7762306a36Sopenharmony_ci } 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci obj = &nvbo->bo.base; 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ciunlock: 8262306a36Sopenharmony_ci dma_resv_unlock(robj); 8362306a36Sopenharmony_ci return obj; 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ciint nouveau_gem_prime_pin(struct drm_gem_object *obj) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci struct nouveau_bo *nvbo = nouveau_gem_object(obj); 8962306a36Sopenharmony_ci int ret; 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci /* pin buffer into GTT */ 9262306a36Sopenharmony_ci ret = nouveau_bo_pin(nvbo, NOUVEAU_GEM_DOMAIN_GART, false); 9362306a36Sopenharmony_ci if (ret) 9462306a36Sopenharmony_ci return -EINVAL; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci return 0; 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_civoid nouveau_gem_prime_unpin(struct drm_gem_object *obj) 10062306a36Sopenharmony_ci{ 10162306a36Sopenharmony_ci struct nouveau_bo *nvbo = nouveau_gem_object(obj); 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci nouveau_bo_unpin(nvbo); 10462306a36Sopenharmony_ci} 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_cistruct dma_buf *nouveau_gem_prime_export(struct drm_gem_object *gobj, 10762306a36Sopenharmony_ci int flags) 10862306a36Sopenharmony_ci{ 10962306a36Sopenharmony_ci struct nouveau_bo *nvbo = nouveau_gem_object(gobj); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci if (nvbo->no_share) 11262306a36Sopenharmony_ci return ERR_PTR(-EPERM); 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci return drm_gem_prime_export(gobj, flags); 11562306a36Sopenharmony_ci} 116