Home | History | Annotate | Line # | Download | only in radeon
      1  1.2  riastrad /*	$NetBSD: radeon_prime.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2012 Advanced Micro Devices, 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  * based on nouveau_prime.c
     25  1.1  riastrad  *
     26  1.1  riastrad  * Authors: Alex Deucher
     27  1.1  riastrad  */
     28  1.3  riastrad 
     29  1.2  riastrad #include <sys/cdefs.h>
     30  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_prime.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
     31  1.2  riastrad 
     32  1.3  riastrad #include <linux/dma-buf.h>
     33  1.3  riastrad 
     34  1.3  riastrad #include <drm/drm_prime.h>
     35  1.3  riastrad #include <drm/radeon_drm.h>
     36  1.1  riastrad 
     37  1.1  riastrad #include "radeon.h"
     38  1.1  riastrad 
     39  1.1  riastrad struct sg_table *radeon_gem_prime_get_sg_table(struct drm_gem_object *obj)
     40  1.1  riastrad {
     41  1.1  riastrad 	struct radeon_bo *bo = gem_to_radeon_bo(obj);
     42  1.1  riastrad 	int npages = bo->tbo.num_pages;
     43  1.1  riastrad 
     44  1.1  riastrad 	return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
     45  1.1  riastrad }
     46  1.1  riastrad 
     47  1.1  riastrad void *radeon_gem_prime_vmap(struct drm_gem_object *obj)
     48  1.1  riastrad {
     49  1.1  riastrad 	struct radeon_bo *bo = gem_to_radeon_bo(obj);
     50  1.1  riastrad 	int ret;
     51  1.1  riastrad 
     52  1.1  riastrad 	ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
     53  1.1  riastrad 			  &bo->dma_buf_vmap);
     54  1.1  riastrad 	if (ret)
     55  1.1  riastrad 		return ERR_PTR(ret);
     56  1.1  riastrad 
     57  1.1  riastrad 	return bo->dma_buf_vmap.virtual;
     58  1.1  riastrad }
     59  1.1  riastrad 
     60  1.1  riastrad void radeon_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
     61  1.1  riastrad {
     62  1.1  riastrad 	struct radeon_bo *bo = gem_to_radeon_bo(obj);
     63  1.1  riastrad 
     64  1.1  riastrad 	ttm_bo_kunmap(&bo->dma_buf_vmap);
     65  1.1  riastrad }
     66  1.1  riastrad 
     67  1.1  riastrad struct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev,
     68  1.2  riastrad 							struct dma_buf_attachment *attach,
     69  1.1  riastrad 							struct sg_table *sg)
     70  1.1  riastrad {
     71  1.3  riastrad 	struct dma_resv *resv = attach->dmabuf->resv;
     72  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
     73  1.1  riastrad 	struct radeon_bo *bo;
     74  1.1  riastrad 	int ret;
     75  1.1  riastrad 
     76  1.3  riastrad 	dma_resv_lock(resv, NULL);
     77  1.2  riastrad 	ret = radeon_bo_create(rdev, attach->dmabuf->size, PAGE_SIZE, false,
     78  1.2  riastrad 			       RADEON_GEM_DOMAIN_GTT, 0, sg, resv, &bo);
     79  1.3  riastrad 	dma_resv_unlock(resv);
     80  1.1  riastrad 	if (ret)
     81  1.1  riastrad 		return ERR_PTR(ret);
     82  1.1  riastrad 
     83  1.1  riastrad 	mutex_lock(&rdev->gem.mutex);
     84  1.1  riastrad 	list_add_tail(&bo->list, &rdev->gem.objects);
     85  1.1  riastrad 	mutex_unlock(&rdev->gem.mutex);
     86  1.1  riastrad 
     87  1.3  riastrad 	bo->prime_shared_count = 1;
     88  1.3  riastrad 	return &bo->tbo.base;
     89  1.1  riastrad }
     90  1.1  riastrad 
     91  1.1  riastrad int radeon_gem_prime_pin(struct drm_gem_object *obj)
     92  1.1  riastrad {
     93  1.1  riastrad 	struct radeon_bo *bo = gem_to_radeon_bo(obj);
     94  1.1  riastrad 	int ret = 0;
     95  1.1  riastrad 
     96  1.1  riastrad 	ret = radeon_bo_reserve(bo, false);
     97  1.1  riastrad 	if (unlikely(ret != 0))
     98  1.1  riastrad 		return ret;
     99  1.1  riastrad 
    100  1.1  riastrad 	/* pin buffer into GTT */
    101  1.1  riastrad 	ret = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_GTT, NULL);
    102  1.3  riastrad 	if (likely(ret == 0))
    103  1.3  riastrad 		bo->prime_shared_count++;
    104  1.3  riastrad 
    105  1.1  riastrad 	radeon_bo_unreserve(bo);
    106  1.1  riastrad 	return ret;
    107  1.1  riastrad }
    108  1.1  riastrad 
    109  1.1  riastrad void radeon_gem_prime_unpin(struct drm_gem_object *obj)
    110  1.1  riastrad {
    111  1.1  riastrad 	struct radeon_bo *bo = gem_to_radeon_bo(obj);
    112  1.1  riastrad 	int ret = 0;
    113  1.1  riastrad 
    114  1.1  riastrad 	ret = radeon_bo_reserve(bo, false);
    115  1.1  riastrad 	if (unlikely(ret != 0))
    116  1.1  riastrad 		return;
    117  1.1  riastrad 
    118  1.1  riastrad 	radeon_bo_unpin(bo);
    119  1.3  riastrad 	if (bo->prime_shared_count)
    120  1.3  riastrad 		bo->prime_shared_count--;
    121  1.1  riastrad 	radeon_bo_unreserve(bo);
    122  1.1  riastrad }
    123  1.2  riastrad 
    124  1.2  riastrad 
    125  1.3  riastrad struct dma_buf *radeon_gem_prime_export(struct drm_gem_object *gobj,
    126  1.2  riastrad 					int flags)
    127  1.2  riastrad {
    128  1.2  riastrad 	struct radeon_bo *bo = gem_to_radeon_bo(gobj);
    129  1.2  riastrad 	if (radeon_ttm_tt_has_userptr(bo->tbo.ttm))
    130  1.2  riastrad 		return ERR_PTR(-EPERM);
    131  1.3  riastrad 	return drm_gem_prime_export(gobj, flags);
    132  1.2  riastrad }
    133