xref: /linux/drivers/gpu/drm/nouveau/nouveau_prime.c (revision ed853f6c3fbc9d83a987801a36e563abafaff86e)
1e9bf5f36SDave Airlie /*
2e9bf5f36SDave Airlie  * Copyright 2011 Red Hat Inc.
3e9bf5f36SDave Airlie  *
4e9bf5f36SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
5e9bf5f36SDave Airlie  * copy of this software and associated documentation files (the "Software"),
6e9bf5f36SDave Airlie  * to deal in the Software without restriction, including without limitation
7e9bf5f36SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e9bf5f36SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
9e9bf5f36SDave Airlie  * Software is furnished to do so, subject to the following conditions:
10e9bf5f36SDave Airlie  *
11e9bf5f36SDave Airlie  * The above copyright notice and this permission notice shall be included in
12e9bf5f36SDave Airlie  * all copies or substantial portions of the Software.
13e9bf5f36SDave Airlie  *
14e9bf5f36SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15e9bf5f36SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16e9bf5f36SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17e9bf5f36SDave Airlie  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18e9bf5f36SDave Airlie  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19e9bf5f36SDave Airlie  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20e9bf5f36SDave Airlie  * OTHER DEALINGS IN THE SOFTWARE.
21e9bf5f36SDave Airlie  *
22e9bf5f36SDave Airlie  * Authors: Dave Airlie
23e9bf5f36SDave Airlie  */
2422b33e8eSDave Airlie 
25b5e9c1a2SMaarten Lankhorst #include <linux/dma-buf.h>
2622b33e8eSDave Airlie 
274dc28134SBen Skeggs #include "nouveau_drv.h"
2877145f1cSBen Skeggs #include "nouveau_gem.h"
2922b33e8eSDave Airlie 
30ab9ccb96SAaron Plattner struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj)
3122b33e8eSDave Airlie {
32ab9ccb96SAaron Plattner 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
3322b33e8eSDave Airlie 	int npages = nvbo->bo.num_pages;
3422b33e8eSDave Airlie 
35707d561fSGerd Hoffmann 	return drm_prime_pages_to_sg(obj->dev, nvbo->bo.ttm->pages, npages);
3622b33e8eSDave Airlie }
3722b33e8eSDave Airlie 
38ab9ccb96SAaron Plattner void *nouveau_gem_prime_vmap(struct drm_gem_object *obj)
3922b33e8eSDave Airlie {
40ab9ccb96SAaron Plattner 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
4135916aceSDave Airlie 	int ret;
4235916aceSDave Airlie 
4335916aceSDave Airlie 	ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages,
4435916aceSDave Airlie 			  &nvbo->dma_buf_vmap);
45ab9ccb96SAaron Plattner 	if (ret)
4635916aceSDave Airlie 		return ERR_PTR(ret);
47ab9ccb96SAaron Plattner 
4835916aceSDave Airlie 	return nvbo->dma_buf_vmap.virtual;
4935916aceSDave Airlie }
5035916aceSDave Airlie 
51ab9ccb96SAaron Plattner void nouveau_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
5235916aceSDave Airlie {
53ab9ccb96SAaron Plattner 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
5435916aceSDave Airlie 
5535916aceSDave Airlie 	ttm_bo_kunmap(&nvbo->dma_buf_vmap);
5635916aceSDave Airlie }
5735916aceSDave Airlie 
58ab9ccb96SAaron Plattner struct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev,
59b5e9c1a2SMaarten Lankhorst 							 struct dma_buf_attachment *attach,
60ab9ccb96SAaron Plattner 							 struct sg_table *sg)
6122b33e8eSDave Airlie {
62bab7cc18SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
630bb21c96SThierry Reding 	struct drm_gem_object *obj;
6422b33e8eSDave Airlie 	struct nouveau_bo *nvbo;
6552791eeeSChristian König 	struct dma_resv *robj = attach->dmabuf->resv;
669ca7f796SThierry Reding 	u64 size = attach->dmabuf->size;
679ca7f796SThierry Reding 	int align = 0;
6822b33e8eSDave Airlie 	int ret;
6922b33e8eSDave Airlie 
7052791eeeSChristian König 	dma_resv_lock(robj, NULL);
7181b61579SChristian König 	nvbo = nouveau_bo_alloc(&drm->client, &size, &align,
7281b61579SChristian König 				NOUVEAU_GEM_DOMAIN_GART, 0, 0);
730bb21c96SThierry Reding 	if (IS_ERR(nvbo)) {
740bb21c96SThierry Reding 		obj = ERR_CAST(nvbo);
750bb21c96SThierry Reding 		goto unlock;
760bb21c96SThierry Reding 	}
7722b33e8eSDave Airlie 
7822b33e8eSDave Airlie 	nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
7955fb74adSDavid Herrmann 
80*ed853f6cSThomas Zimmermann 	nvbo->bo.base.funcs = &nouveau_gem_object_funcs;
81*ed853f6cSThomas Zimmermann 
8255fb74adSDavid Herrmann 	/* Initialize the embedded gem-object. We return a single gem-reference
8355fb74adSDavid Herrmann 	 * to the caller, instead of a normal nouveau_bo ttm reference. */
84019cbd4aSThierry Reding 	ret = drm_gem_object_init(dev, &nvbo->bo.base, size);
8555fb74adSDavid Herrmann 	if (ret) {
86ab9ccb96SAaron Plattner 		nouveau_bo_ref(NULL, &nvbo);
870bb21c96SThierry Reding 		obj = ERR_PTR(-ENOMEM);
880bb21c96SThierry Reding 		goto unlock;
8922b33e8eSDave Airlie 	}
9022b33e8eSDave Airlie 
9181b61579SChristian König 	ret = nouveau_bo_init(nvbo, size, align, NOUVEAU_GEM_DOMAIN_GART,
9281b61579SChristian König 			      sg, robj);
93019cbd4aSThierry Reding 	if (ret) {
94019cbd4aSThierry Reding 		nouveau_bo_ref(NULL, &nvbo);
950bb21c96SThierry Reding 		obj = ERR_PTR(ret);
960bb21c96SThierry Reding 		goto unlock;
97019cbd4aSThierry Reding 	}
98019cbd4aSThierry Reding 
990bb21c96SThierry Reding 	obj = &nvbo->bo.base;
1000bb21c96SThierry Reding 
1010bb21c96SThierry Reding unlock:
1020bb21c96SThierry Reding 	dma_resv_unlock(robj);
1030bb21c96SThierry Reding 	return obj;
10422b33e8eSDave Airlie }
10522b33e8eSDave Airlie 
106ab9ccb96SAaron Plattner int nouveau_gem_prime_pin(struct drm_gem_object *obj)
10722b33e8eSDave Airlie {
10822b33e8eSDave Airlie 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
1091af7c7ddSMaarten Lankhorst 	int ret;
11022b33e8eSDave Airlie 
11122b33e8eSDave Airlie 	/* pin buffer into GTT */
11281b61579SChristian König 	ret = nouveau_bo_pin(nvbo, NOUVEAU_GEM_DOMAIN_GART, false);
11322b33e8eSDave Airlie 	if (ret)
114ab9ccb96SAaron Plattner 		return -EINVAL;
11522b33e8eSDave Airlie 
116ab9ccb96SAaron Plattner 	return 0;
11722b33e8eSDave Airlie }
1181af7c7ddSMaarten Lankhorst 
1191af7c7ddSMaarten Lankhorst void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
1201af7c7ddSMaarten Lankhorst {
1211af7c7ddSMaarten Lankhorst 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
1221af7c7ddSMaarten Lankhorst 
1231af7c7ddSMaarten Lankhorst 	nouveau_bo_unpin(nvbo);
1241af7c7ddSMaarten Lankhorst }
125