Loading...
1/*
2 * Copyright 2012 Red Hat 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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Dave Airlie <airlied@redhat.com>
25 */
26
27#include <linux/dma-buf.h>
28#include <linux/reservation.h>
29
30#include <drm/drmP.h>
31
32#include "i915_drv.h"
33
34static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
35{
36 return to_intel_bo(buf->priv);
37}
38
39static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
40 enum dma_data_direction dir)
41{
42 struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
43 struct sg_table *st;
44 struct scatterlist *src, *dst;
45 int ret, i;
46
47 ret = i915_gem_object_pin_pages(obj);
48 if (ret)
49 goto err;
50
51 /* Copy sg so that we make an independent mapping */
52 st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
53 if (st == NULL) {
54 ret = -ENOMEM;
55 goto err_unpin_pages;
56 }
57
58 ret = sg_alloc_table(st, obj->mm.pages->nents, GFP_KERNEL);
59 if (ret)
60 goto err_free;
61
62 src = obj->mm.pages->sgl;
63 dst = st->sgl;
64 for (i = 0; i < obj->mm.pages->nents; i++) {
65 sg_set_page(dst, sg_page(src), src->length, 0);
66 dst = sg_next(dst);
67 src = sg_next(src);
68 }
69
70 if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
71 ret = -ENOMEM;
72 goto err_free_sg;
73 }
74
75 return st;
76
77err_free_sg:
78 sg_free_table(st);
79err_free:
80 kfree(st);
81err_unpin_pages:
82 i915_gem_object_unpin_pages(obj);
83err:
84 return ERR_PTR(ret);
85}
86
87static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
88 struct sg_table *sg,
89 enum dma_data_direction dir)
90{
91 struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
92
93 dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
94 sg_free_table(sg);
95 kfree(sg);
96
97 i915_gem_object_unpin_pages(obj);
98}
99
100static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
101{
102 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
103
104 return i915_gem_object_pin_map(obj, I915_MAP_WB);
105}
106
107static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
108{
109 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
110
111 i915_gem_object_unpin_map(obj);
112}
113
114static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
115{
116 return NULL;
117}
118
119static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
120{
121
122}
123static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
124{
125 return NULL;
126}
127
128static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
129{
130
131}
132
133static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
134{
135 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
136 int ret;
137
138 if (obj->base.size < vma->vm_end - vma->vm_start)
139 return -EINVAL;
140
141 if (!obj->base.filp)
142 return -ENODEV;
143
144 ret = obj->base.filp->f_op->mmap(obj->base.filp, vma);
145 if (ret)
146 return ret;
147
148 fput(vma->vm_file);
149 vma->vm_file = get_file(obj->base.filp);
150
151 return 0;
152}
153
154static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
155{
156 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
157 struct drm_device *dev = obj->base.dev;
158 bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
159 int err;
160
161 err = i915_gem_object_pin_pages(obj);
162 if (err)
163 return err;
164
165 err = i915_mutex_lock_interruptible(dev);
166 if (err)
167 goto out;
168
169 err = i915_gem_object_set_to_cpu_domain(obj, write);
170 mutex_unlock(&dev->struct_mutex);
171
172out:
173 i915_gem_object_unpin_pages(obj);
174 return err;
175}
176
177static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
178{
179 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
180 struct drm_device *dev = obj->base.dev;
181 int err;
182
183 err = i915_gem_object_pin_pages(obj);
184 if (err)
185 return err;
186
187 err = i915_mutex_lock_interruptible(dev);
188 if (err)
189 goto out;
190
191 err = i915_gem_object_set_to_gtt_domain(obj, false);
192 mutex_unlock(&dev->struct_mutex);
193
194out:
195 i915_gem_object_unpin_pages(obj);
196 return err;
197}
198
199static const struct dma_buf_ops i915_dmabuf_ops = {
200 .map_dma_buf = i915_gem_map_dma_buf,
201 .unmap_dma_buf = i915_gem_unmap_dma_buf,
202 .release = drm_gem_dmabuf_release,
203 .kmap = i915_gem_dmabuf_kmap,
204 .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
205 .kunmap = i915_gem_dmabuf_kunmap,
206 .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
207 .mmap = i915_gem_dmabuf_mmap,
208 .vmap = i915_gem_dmabuf_vmap,
209 .vunmap = i915_gem_dmabuf_vunmap,
210 .begin_cpu_access = i915_gem_begin_cpu_access,
211 .end_cpu_access = i915_gem_end_cpu_access,
212};
213
214struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
215 struct drm_gem_object *gem_obj, int flags)
216{
217 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
218 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
219
220 exp_info.ops = &i915_dmabuf_ops;
221 exp_info.size = gem_obj->size;
222 exp_info.flags = flags;
223 exp_info.priv = gem_obj;
224 exp_info.resv = obj->resv;
225
226 if (obj->ops->dmabuf_export) {
227 int ret = obj->ops->dmabuf_export(obj);
228 if (ret)
229 return ERR_PTR(ret);
230 }
231
232 return drm_gem_dmabuf_export(dev, &exp_info);
233}
234
235static struct sg_table *
236i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
237{
238 return dma_buf_map_attachment(obj->base.import_attach,
239 DMA_BIDIRECTIONAL);
240}
241
242static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj,
243 struct sg_table *pages)
244{
245 dma_buf_unmap_attachment(obj->base.import_attach, pages,
246 DMA_BIDIRECTIONAL);
247}
248
249static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
250 .get_pages = i915_gem_object_get_pages_dmabuf,
251 .put_pages = i915_gem_object_put_pages_dmabuf,
252};
253
254struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
255 struct dma_buf *dma_buf)
256{
257 struct dma_buf_attachment *attach;
258 struct drm_i915_gem_object *obj;
259 int ret;
260
261 /* is this one of own objects? */
262 if (dma_buf->ops == &i915_dmabuf_ops) {
263 obj = dma_buf_to_obj(dma_buf);
264 /* is it from our device? */
265 if (obj->base.dev == dev) {
266 /*
267 * Importing dmabuf exported from out own gem increases
268 * refcount on gem itself instead of f_count of dmabuf.
269 */
270 return &i915_gem_object_get(obj)->base;
271 }
272 }
273
274 /* need to attach */
275 attach = dma_buf_attach(dma_buf, dev->dev);
276 if (IS_ERR(attach))
277 return ERR_CAST(attach);
278
279 get_dma_buf(dma_buf);
280
281 obj = i915_gem_object_alloc(dev);
282 if (obj == NULL) {
283 ret = -ENOMEM;
284 goto fail_detach;
285 }
286
287 drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
288 i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
289 obj->base.import_attach = attach;
290 obj->resv = dma_buf->resv;
291
292 /* We use GTT as shorthand for a coherent domain, one that is
293 * neither in the GPU cache nor in the CPU cache, where all
294 * writes are immediately visible in memory. (That's not strictly
295 * true, but it's close! There are internal buffers such as the
296 * write-combined buffer or a delay through the chipset for GTT
297 * writes that do require us to treat GTT as a separate cache domain.)
298 */
299 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
300 obj->base.write_domain = 0;
301
302 return &obj->base;
303
304fail_detach:
305 dma_buf_detach(dma_buf, attach);
306 dma_buf_put(dma_buf);
307
308 return ERR_PTR(ret);
309}
1/*
2 * Copyright 2012 Red Hat 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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Dave Airlie <airlied@redhat.com>
25 */
26#include "drmP.h"
27#include "i915_drv.h"
28#include <linux/dma-buf.h>
29
30static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
31 enum dma_data_direction dir)
32{
33 struct drm_i915_gem_object *obj = attachment->dmabuf->priv;
34 struct drm_device *dev = obj->base.dev;
35 int npages = obj->base.size / PAGE_SIZE;
36 struct sg_table *sg = NULL;
37 int ret;
38 int nents;
39
40 ret = i915_mutex_lock_interruptible(dev);
41 if (ret)
42 return ERR_PTR(ret);
43
44 if (!obj->pages) {
45 ret = i915_gem_object_get_pages_gtt(obj, __GFP_NORETRY | __GFP_NOWARN);
46 if (ret)
47 goto out;
48 }
49
50 /* link the pages into an SG then map the sg */
51 sg = drm_prime_pages_to_sg(obj->pages, npages);
52 nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir);
53out:
54 mutex_unlock(&dev->struct_mutex);
55 return sg;
56}
57
58static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
59 struct sg_table *sg, enum dma_data_direction dir)
60{
61 dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
62 sg_free_table(sg);
63 kfree(sg);
64}
65
66static void i915_gem_dmabuf_release(struct dma_buf *dma_buf)
67{
68 struct drm_i915_gem_object *obj = dma_buf->priv;
69
70 if (obj->base.export_dma_buf == dma_buf) {
71 /* drop the reference on the export fd holds */
72 obj->base.export_dma_buf = NULL;
73 drm_gem_object_unreference_unlocked(&obj->base);
74 }
75}
76
77static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
78{
79 struct drm_i915_gem_object *obj = dma_buf->priv;
80 struct drm_device *dev = obj->base.dev;
81 int ret;
82
83 ret = i915_mutex_lock_interruptible(dev);
84 if (ret)
85 return ERR_PTR(ret);
86
87 if (obj->dma_buf_vmapping) {
88 obj->vmapping_count++;
89 goto out_unlock;
90 }
91
92 if (!obj->pages) {
93 ret = i915_gem_object_get_pages_gtt(obj, __GFP_NORETRY | __GFP_NOWARN);
94 if (ret) {
95 mutex_unlock(&dev->struct_mutex);
96 return ERR_PTR(ret);
97 }
98 }
99
100 obj->dma_buf_vmapping = vmap(obj->pages, obj->base.size / PAGE_SIZE, 0, PAGE_KERNEL);
101 if (!obj->dma_buf_vmapping) {
102 DRM_ERROR("failed to vmap object\n");
103 goto out_unlock;
104 }
105
106 obj->vmapping_count = 1;
107out_unlock:
108 mutex_unlock(&dev->struct_mutex);
109 return obj->dma_buf_vmapping;
110}
111
112static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
113{
114 struct drm_i915_gem_object *obj = dma_buf->priv;
115 struct drm_device *dev = obj->base.dev;
116 int ret;
117
118 ret = i915_mutex_lock_interruptible(dev);
119 if (ret)
120 return;
121
122 --obj->vmapping_count;
123 if (obj->vmapping_count == 0) {
124 vunmap(obj->dma_buf_vmapping);
125 obj->dma_buf_vmapping = NULL;
126 }
127 mutex_unlock(&dev->struct_mutex);
128}
129
130static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
131{
132 return NULL;
133}
134
135static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
136{
137
138}
139static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
140{
141 return NULL;
142}
143
144static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
145{
146
147}
148
149static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
150{
151 return -EINVAL;
152}
153
154static const struct dma_buf_ops i915_dmabuf_ops = {
155 .map_dma_buf = i915_gem_map_dma_buf,
156 .unmap_dma_buf = i915_gem_unmap_dma_buf,
157 .release = i915_gem_dmabuf_release,
158 .kmap = i915_gem_dmabuf_kmap,
159 .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
160 .kunmap = i915_gem_dmabuf_kunmap,
161 .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
162 .mmap = i915_gem_dmabuf_mmap,
163 .vmap = i915_gem_dmabuf_vmap,
164 .vunmap = i915_gem_dmabuf_vunmap,
165};
166
167struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
168 struct drm_gem_object *gem_obj, int flags)
169{
170 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
171
172 return dma_buf_export(obj, &i915_dmabuf_ops,
173 obj->base.size, 0600);
174}
175
176struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
177 struct dma_buf *dma_buf)
178{
179 struct dma_buf_attachment *attach;
180 struct sg_table *sg;
181 struct drm_i915_gem_object *obj;
182 int npages;
183 int size;
184 int ret;
185
186 /* is this one of own objects? */
187 if (dma_buf->ops == &i915_dmabuf_ops) {
188 obj = dma_buf->priv;
189 /* is it from our device? */
190 if (obj->base.dev == dev) {
191 drm_gem_object_reference(&obj->base);
192 return &obj->base;
193 }
194 }
195
196 /* need to attach */
197 attach = dma_buf_attach(dma_buf, dev->dev);
198 if (IS_ERR(attach))
199 return ERR_CAST(attach);
200
201 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
202 if (IS_ERR(sg)) {
203 ret = PTR_ERR(sg);
204 goto fail_detach;
205 }
206
207 size = dma_buf->size;
208 npages = size / PAGE_SIZE;
209
210 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
211 if (obj == NULL) {
212 ret = -ENOMEM;
213 goto fail_unmap;
214 }
215
216 ret = drm_gem_private_object_init(dev, &obj->base, size);
217 if (ret) {
218 kfree(obj);
219 goto fail_unmap;
220 }
221
222 obj->sg_table = sg;
223 obj->base.import_attach = attach;
224
225 return &obj->base;
226
227fail_unmap:
228 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
229fail_detach:
230 dma_buf_detach(dma_buf, attach);
231 return ERR_PTR(ret);
232}