Home | History | Annotate | Line # | Download | only in nouveau
      1  1.2  riastrad /*	$NetBSD: nouveau_prime.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2011 Red Hat Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Dave Airlie
     25  1.1  riastrad  */
     26  1.1  riastrad 
     27  1.2  riastrad #include <sys/cdefs.h>
     28  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_prime.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $");
     29  1.2  riastrad 
     30  1.2  riastrad #include <linux/dma-buf.h>
     31  1.1  riastrad 
     32  1.3  riastrad #include "nouveau_drv.h"
     33  1.1  riastrad #include "nouveau_gem.h"
     34  1.1  riastrad 
     35  1.1  riastrad struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj)
     36  1.1  riastrad {
     37  1.1  riastrad 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
     38  1.1  riastrad 	int npages = nvbo->bo.num_pages;
     39  1.1  riastrad 
     40  1.1  riastrad 	return drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages);
     41  1.1  riastrad }
     42  1.1  riastrad 
     43  1.1  riastrad void *nouveau_gem_prime_vmap(struct drm_gem_object *obj)
     44  1.1  riastrad {
     45  1.1  riastrad 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
     46  1.1  riastrad 	int ret;
     47  1.1  riastrad 
     48  1.1  riastrad 	ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages,
     49  1.1  riastrad 			  &nvbo->dma_buf_vmap);
     50  1.1  riastrad 	if (ret)
     51  1.1  riastrad 		return ERR_PTR(ret);
     52  1.1  riastrad 
     53  1.1  riastrad 	return nvbo->dma_buf_vmap.virtual;
     54  1.1  riastrad }
     55  1.1  riastrad 
     56  1.1  riastrad void nouveau_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
     57  1.1  riastrad {
     58  1.1  riastrad 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
     59  1.1  riastrad 
     60  1.1  riastrad 	ttm_bo_kunmap(&nvbo->dma_buf_vmap);
     61  1.1  riastrad }
     62  1.1  riastrad 
     63  1.1  riastrad struct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev,
     64  1.2  riastrad 							 struct dma_buf_attachment *attach,
     65  1.1  riastrad 							 struct sg_table *sg)
     66  1.1  riastrad {
     67  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
     68  1.3  riastrad 	struct drm_gem_object *obj;
     69  1.1  riastrad 	struct nouveau_bo *nvbo;
     70  1.3  riastrad 	struct dma_resv *robj = attach->dmabuf->resv;
     71  1.3  riastrad 	u64 size = attach->dmabuf->size;
     72  1.1  riastrad 	u32 flags = 0;
     73  1.3  riastrad 	int align = 0;
     74  1.1  riastrad 	int ret;
     75  1.1  riastrad 
     76  1.1  riastrad 	flags = TTM_PL_FLAG_TT;
     77  1.1  riastrad 
     78  1.3  riastrad 	dma_resv_lock(robj, NULL);
     79  1.3  riastrad 	nvbo = nouveau_bo_alloc(&drm->client, &size, &align, flags, 0, 0);
     80  1.3  riastrad 	if (IS_ERR(nvbo)) {
     81  1.3  riastrad 		obj = ERR_CAST(nvbo);
     82  1.3  riastrad 		goto unlock;
     83  1.3  riastrad 	}
     84  1.1  riastrad 
     85  1.1  riastrad 	nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
     86  1.1  riastrad 
     87  1.1  riastrad 	/* Initialize the embedded gem-object. We return a single gem-reference
     88  1.1  riastrad 	 * to the caller, instead of a normal nouveau_bo ttm reference. */
     89  1.3  riastrad 	ret = drm_gem_object_init(dev, &nvbo->bo.base, size);
     90  1.3  riastrad 	if (ret) {
     91  1.3  riastrad 		nouveau_bo_ref(NULL, &nvbo);
     92  1.3  riastrad 		obj = ERR_PTR(-ENOMEM);
     93  1.3  riastrad 		goto unlock;
     94  1.3  riastrad 	}
     95  1.3  riastrad 
     96  1.3  riastrad 	ret = nouveau_bo_init(nvbo, size, align, flags, sg, robj);
     97  1.1  riastrad 	if (ret) {
     98  1.1  riastrad 		nouveau_bo_ref(NULL, &nvbo);
     99  1.3  riastrad 		obj = ERR_PTR(ret);
    100  1.3  riastrad 		goto unlock;
    101  1.1  riastrad 	}
    102  1.1  riastrad 
    103  1.3  riastrad 	obj = &nvbo->bo.base;
    104  1.3  riastrad 
    105  1.3  riastrad unlock:
    106  1.3  riastrad 	dma_resv_unlock(robj);
    107  1.3  riastrad 	return obj;
    108  1.1  riastrad }
    109  1.1  riastrad 
    110  1.1  riastrad int nouveau_gem_prime_pin(struct drm_gem_object *obj)
    111  1.1  riastrad {
    112  1.1  riastrad 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
    113  1.1  riastrad 	int ret;
    114  1.1  riastrad 
    115  1.1  riastrad 	/* pin buffer into GTT */
    116  1.2  riastrad 	ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT, false);
    117  1.1  riastrad 	if (ret)
    118  1.1  riastrad 		return -EINVAL;
    119  1.1  riastrad 
    120  1.1  riastrad 	return 0;
    121  1.1  riastrad }
    122  1.1  riastrad 
    123  1.1  riastrad void nouveau_gem_prime_unpin(struct drm_gem_object *obj)
    124  1.1  riastrad {
    125  1.1  riastrad 	struct nouveau_bo *nvbo = nouveau_gem_object(obj);
    126  1.1  riastrad 
    127  1.1  riastrad 	nouveau_bo_unpin(nvbo);
    128  1.1  riastrad }
    129