xref: /linux/drivers/gpu/drm/nouveau/nouveau_prime.c (revision ab9ccb96a6e6f95bcde6b8b2a524370efdbfdcd6)
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 
25760285e7SDavid Howells #include <drm/drmP.h>
2622b33e8eSDave Airlie 
2777145f1cSBen Skeggs #include "nouveau_drm.h"
2877145f1cSBen Skeggs #include "nouveau_gem.h"
2922b33e8eSDave Airlie 
30*ab9ccb96SAaron Plattner struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj)
3122b33e8eSDave Airlie {
32*ab9ccb96SAaron Plattner 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
3322b33e8eSDave Airlie 	int npages = nvbo->bo.num_pages;
3422b33e8eSDave Airlie 
35*ab9ccb96SAaron Plattner 	return drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages);
3622b33e8eSDave Airlie }
3722b33e8eSDave Airlie 
38*ab9ccb96SAaron Plattner void *nouveau_gem_prime_vmap(struct drm_gem_object *obj)
3922b33e8eSDave Airlie {
40*ab9ccb96SAaron 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);
45*ab9ccb96SAaron Plattner 	if (ret)
4635916aceSDave Airlie 		return ERR_PTR(ret);
47*ab9ccb96SAaron Plattner 
4835916aceSDave Airlie 	return nvbo->dma_buf_vmap.virtual;
4935916aceSDave Airlie }
5035916aceSDave Airlie 
51*ab9ccb96SAaron Plattner void nouveau_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
5235916aceSDave Airlie {
53*ab9ccb96SAaron Plattner 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
5435916aceSDave Airlie 
5535916aceSDave Airlie 	ttm_bo_kunmap(&nvbo->dma_buf_vmap);
5635916aceSDave Airlie }
5735916aceSDave Airlie 
58*ab9ccb96SAaron Plattner struct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev,
5922b33e8eSDave Airlie 							 size_t size,
60*ab9ccb96SAaron Plattner 							 struct sg_table *sg)
6122b33e8eSDave Airlie {
6222b33e8eSDave Airlie 	struct nouveau_bo *nvbo;
6322b33e8eSDave Airlie 	u32 flags = 0;
6422b33e8eSDave Airlie 	int ret;
6522b33e8eSDave Airlie 
6622b33e8eSDave Airlie 	flags = TTM_PL_FLAG_TT;
6722b33e8eSDave Airlie 
6822b33e8eSDave Airlie 	ret = nouveau_bo_new(dev, size, 0, flags, 0, 0,
69*ab9ccb96SAaron Plattner 			     sg, &nvbo);
7022b33e8eSDave Airlie 	if (ret)
71*ab9ccb96SAaron Plattner 		return ERR_PTR(ret);
7222b33e8eSDave Airlie 
7322b33e8eSDave Airlie 	nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
7422b33e8eSDave Airlie 	nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
7522b33e8eSDave Airlie 	if (!nvbo->gem) {
76*ab9ccb96SAaron Plattner 		nouveau_bo_ref(NULL, &nvbo);
77*ab9ccb96SAaron Plattner 		return ERR_PTR(-ENOMEM);
7822b33e8eSDave Airlie 	}
7922b33e8eSDave Airlie 
8022b33e8eSDave Airlie 	nvbo->gem->driver_private = nvbo;
81*ab9ccb96SAaron Plattner 	return nvbo->gem;
8222b33e8eSDave Airlie }
8322b33e8eSDave Airlie 
84*ab9ccb96SAaron Plattner int nouveau_gem_prime_pin(struct drm_gem_object *obj)
8522b33e8eSDave Airlie {
8622b33e8eSDave Airlie 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
8722b33e8eSDave Airlie 	int ret = 0;
8822b33e8eSDave Airlie 
8922b33e8eSDave Airlie 	/* pin buffer into GTT */
9022b33e8eSDave Airlie 	ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT);
9122b33e8eSDave Airlie 	if (ret)
92*ab9ccb96SAaron Plattner 		return -EINVAL;
9322b33e8eSDave Airlie 
94*ab9ccb96SAaron Plattner 	return 0;
9522b33e8eSDave Airlie }
96