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