1 /* $NetBSD: radeon_prime.c,v 1.2 2018/08/27 04:58:36 riastradh Exp $ */ 2 3 /* 4 * Copyright 2012 Advanced Micro Devices, Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * based on nouveau_prime.c 25 * 26 * Authors: Alex Deucher 27 */ 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: radeon_prime.c,v 1.2 2018/08/27 04:58:36 riastradh Exp $"); 30 31 #include <drm/drmP.h> 32 33 #include "radeon.h" 34 #include <drm/radeon_drm.h> 35 #include <linux/dma-buf.h> 36 37 struct sg_table *radeon_gem_prime_get_sg_table(struct drm_gem_object *obj) 38 { 39 struct radeon_bo *bo = gem_to_radeon_bo(obj); 40 int npages = bo->tbo.num_pages; 41 42 return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages); 43 } 44 45 void *radeon_gem_prime_vmap(struct drm_gem_object *obj) 46 { 47 struct radeon_bo *bo = gem_to_radeon_bo(obj); 48 int ret; 49 50 ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, 51 &bo->dma_buf_vmap); 52 if (ret) 53 return ERR_PTR(ret); 54 55 return bo->dma_buf_vmap.virtual; 56 } 57 58 void radeon_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) 59 { 60 struct radeon_bo *bo = gem_to_radeon_bo(obj); 61 62 ttm_bo_kunmap(&bo->dma_buf_vmap); 63 } 64 65 struct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev, 66 struct dma_buf_attachment *attach, 67 struct sg_table *sg) 68 { 69 struct reservation_object *resv = attach->dmabuf->resv; 70 struct radeon_device *rdev = dev->dev_private; 71 struct radeon_bo *bo; 72 int ret; 73 74 ww_mutex_lock(&resv->lock, NULL); 75 ret = radeon_bo_create(rdev, attach->dmabuf->size, PAGE_SIZE, false, 76 RADEON_GEM_DOMAIN_GTT, 0, sg, resv, &bo); 77 ww_mutex_unlock(&resv->lock); 78 if (ret) 79 return ERR_PTR(ret); 80 81 mutex_lock(&rdev->gem.mutex); 82 list_add_tail(&bo->list, &rdev->gem.objects); 83 mutex_unlock(&rdev->gem.mutex); 84 85 return &bo->gem_base; 86 } 87 88 int radeon_gem_prime_pin(struct drm_gem_object *obj) 89 { 90 struct radeon_bo *bo = gem_to_radeon_bo(obj); 91 int ret = 0; 92 93 ret = radeon_bo_reserve(bo, false); 94 if (unlikely(ret != 0)) 95 return ret; 96 97 /* pin buffer into GTT */ 98 ret = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_GTT, NULL); 99 radeon_bo_unreserve(bo); 100 return ret; 101 } 102 103 void radeon_gem_prime_unpin(struct drm_gem_object *obj) 104 { 105 struct radeon_bo *bo = gem_to_radeon_bo(obj); 106 int ret = 0; 107 108 ret = radeon_bo_reserve(bo, false); 109 if (unlikely(ret != 0)) 110 return; 111 112 radeon_bo_unpin(bo); 113 radeon_bo_unreserve(bo); 114 } 115 116 117 struct reservation_object *radeon_gem_prime_res_obj(struct drm_gem_object *obj) 118 { 119 struct radeon_bo *bo = gem_to_radeon_bo(obj); 120 121 return bo->tbo.resv; 122 } 123 124 struct dma_buf *radeon_gem_prime_export(struct drm_device *dev, 125 struct drm_gem_object *gobj, 126 int flags) 127 { 128 struct radeon_bo *bo = gem_to_radeon_bo(gobj); 129 if (radeon_ttm_tt_has_userptr(bo->tbo.ttm)) 130 return ERR_PTR(-EPERM); 131 return drm_gem_prime_export(dev, gobj, flags); 132 } 133