1 /*
2  * Copyright 2012 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * based on nouveau_prime.c
23  *
24  * Authors: Alex Deucher
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: stable/9/sys/dev/drm2/radeon/radeon_prime.c 254885 2013-08-25 19:37:15Z dumbbell $");
29 
30 #include <drm/drmP.h>
31 
32 #include "radeon.h"
33 #include <drm/radeon_drm.h>
34 
35 #include <linux/dma-buf.h>
36 
radeon_gem_map_dma_buf(struct dma_buf_attachment * attachment,enum dma_data_direction dir)37 static struct sg_table *radeon_gem_map_dma_buf(struct dma_buf_attachment *attachment,
38 					       enum dma_data_direction dir)
39 {
40 	struct radeon_bo *bo = attachment->dmabuf->priv;
41 	struct drm_device *dev = bo->rdev->ddev;
42 	int npages = bo->tbo.num_pages;
43 	struct sg_table *sg;
44 	int nents;
45 
46 	mutex_lock(&dev->struct_mutex);
47 	sg = drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
48 	nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir);
49 	mutex_unlock(&dev->struct_mutex);
50 	return sg;
51 }
52 
radeon_gem_unmap_dma_buf(struct dma_buf_attachment * attachment,struct sg_table * sg,enum dma_data_direction dir)53 static void radeon_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
54 				     struct sg_table *sg, enum dma_data_direction dir)
55 {
56 	dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
57 	sg_free_table(sg);
58 	kfree(sg);
59 }
60 
radeon_gem_dmabuf_release(struct dma_buf * dma_buf)61 static void radeon_gem_dmabuf_release(struct dma_buf *dma_buf)
62 {
63 	struct radeon_bo *bo = dma_buf->priv;
64 
65 	if (bo->gem_base.export_dma_buf == dma_buf) {
66 		DRM_ERROR("unreference dmabuf %p\n", &bo->gem_base);
67 		bo->gem_base.export_dma_buf = NULL;
68 		drm_gem_object_unreference_unlocked(&bo->gem_base);
69 	}
70 }
71 
radeon_gem_kmap_atomic(struct dma_buf * dma_buf,unsigned long page_num)72 static void *radeon_gem_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
73 {
74 	return NULL;
75 }
76 
radeon_gem_kunmap_atomic(struct dma_buf * dma_buf,unsigned long page_num,void * addr)77 static void radeon_gem_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
78 {
79 
80 }
radeon_gem_kmap(struct dma_buf * dma_buf,unsigned long page_num)81 static void *radeon_gem_kmap(struct dma_buf *dma_buf, unsigned long page_num)
82 {
83 	return NULL;
84 }
85 
radeon_gem_kunmap(struct dma_buf * dma_buf,unsigned long page_num,void * addr)86 static void radeon_gem_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
87 {
88 
89 }
90 
radeon_gem_prime_mmap(struct dma_buf * dma_buf,struct vm_area_struct * vma)91 static int radeon_gem_prime_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
92 {
93 	return -EINVAL;
94 }
95 
radeon_gem_prime_vmap(struct dma_buf * dma_buf)96 static void *radeon_gem_prime_vmap(struct dma_buf *dma_buf)
97 {
98 	struct radeon_bo *bo = dma_buf->priv;
99 	struct drm_device *dev = bo->rdev->ddev;
100 	int ret;
101 
102 	mutex_lock(&dev->struct_mutex);
103 	if (bo->vmapping_count) {
104 		bo->vmapping_count++;
105 		goto out_unlock;
106 	}
107 
108 	ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
109 			  &bo->dma_buf_vmap);
110 	if (ret) {
111 		mutex_unlock(&dev->struct_mutex);
112 		return ERR_PTR(ret);
113 	}
114 	bo->vmapping_count = 1;
115 out_unlock:
116 	mutex_unlock(&dev->struct_mutex);
117 	return bo->dma_buf_vmap.virtual;
118 }
119 
radeon_gem_prime_vunmap(struct dma_buf * dma_buf,void * vaddr)120 static void radeon_gem_prime_vunmap(struct dma_buf *dma_buf, void *vaddr)
121 {
122 	struct radeon_bo *bo = dma_buf->priv;
123 	struct drm_device *dev = bo->rdev->ddev;
124 
125 	mutex_lock(&dev->struct_mutex);
126 	bo->vmapping_count--;
127 	if (bo->vmapping_count == 0) {
128 		ttm_bo_kunmap(&bo->dma_buf_vmap);
129 	}
130 	mutex_unlock(&dev->struct_mutex);
131 }
132 const static struct dma_buf_ops radeon_dmabuf_ops =  {
133 	.map_dma_buf = radeon_gem_map_dma_buf,
134 	.unmap_dma_buf = radeon_gem_unmap_dma_buf,
135 	.release = radeon_gem_dmabuf_release,
136 	.kmap = radeon_gem_kmap,
137 	.kmap_atomic = radeon_gem_kmap_atomic,
138 	.kunmap = radeon_gem_kunmap,
139 	.kunmap_atomic = radeon_gem_kunmap_atomic,
140 	.mmap = radeon_gem_prime_mmap,
141 	.vmap = radeon_gem_prime_vmap,
142 	.vunmap = radeon_gem_prime_vunmap,
143 };
144 
radeon_prime_create(struct drm_device * dev,size_t size,struct sg_table * sg,struct radeon_bo ** pbo)145 static int radeon_prime_create(struct drm_device *dev,
146 			       size_t size,
147 			       struct sg_table *sg,
148 			       struct radeon_bo **pbo)
149 {
150 	struct radeon_device *rdev = dev->dev_private;
151 	struct radeon_bo *bo;
152 	int ret;
153 
154 	ret = radeon_bo_create(rdev, size, PAGE_SIZE, false,
155 			       RADEON_GEM_DOMAIN_GTT, sg, pbo);
156 	if (ret)
157 		return ret;
158 	bo = *pbo;
159 	bo->gem_base.driver_private = bo;
160 
161 	mutex_lock(&rdev->gem.mutex);
162 	list_add_tail(&bo->list, &rdev->gem.objects);
163 	mutex_unlock(&rdev->gem.mutex);
164 
165 	return 0;
166 }
167 
radeon_gem_prime_export(struct drm_device * dev,struct drm_gem_object * obj,int flags)168 struct dma_buf *radeon_gem_prime_export(struct drm_device *dev,
169 					struct drm_gem_object *obj,
170 					int flags)
171 {
172 	struct radeon_bo *bo = gem_to_radeon_bo(obj);
173 	int ret = 0;
174 
175 	ret = radeon_bo_reserve(bo, false);
176 	if (unlikely(ret != 0))
177 		return ERR_PTR(ret);
178 
179 	/* pin buffer into GTT */
180 	ret = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_GTT, NULL);
181 	if (ret) {
182 		radeon_bo_unreserve(bo);
183 		return ERR_PTR(ret);
184 	}
185 	radeon_bo_unreserve(bo);
186 	return dma_buf_export(bo, &radeon_dmabuf_ops, obj->size, flags);
187 }
188 
radeon_gem_prime_import(struct drm_device * dev,struct dma_buf * dma_buf)189 struct drm_gem_object *radeon_gem_prime_import(struct drm_device *dev,
190 					       struct dma_buf *dma_buf)
191 {
192 	struct dma_buf_attachment *attach;
193 	struct sg_table *sg;
194 	struct radeon_bo *bo;
195 	int ret;
196 
197 	if (dma_buf->ops == &radeon_dmabuf_ops) {
198 		bo = dma_buf->priv;
199 		if (bo->gem_base.dev == dev) {
200 			drm_gem_object_reference(&bo->gem_base);
201 			dma_buf_put(dma_buf);
202 			return &bo->gem_base;
203 		}
204 	}
205 
206 	/* need to attach */
207 	attach = dma_buf_attach(dma_buf, dev->dev);
208 	if (IS_ERR(attach))
209 		return ERR_CAST(attach);
210 
211 	sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
212 	if (IS_ERR(sg)) {
213 		ret = PTR_ERR(sg);
214 		goto fail_detach;
215 	}
216 
217 	ret = radeon_prime_create(dev, dma_buf->size, sg, &bo);
218 	if (ret)
219 		goto fail_unmap;
220 
221 	bo->gem_base.import_attach = attach;
222 
223 	return &bo->gem_base;
224 
225 fail_unmap:
226 	dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
227 fail_detach:
228 	dma_buf_detach(dma_buf, attach);
229 	return ERR_PTR(ret);
230 }
231