Merge branch 'drm-fixes' of git://people.freedesktop.org/~airlied/linux
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / gpu / drm / i915 / i915_gem_dmabuf.c
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 <drm/drmP.h>
27 #include "i915_drv.h"
28 #include <linux/dma-buf.h>
29
30 static 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 sg_table *st;
35         struct scatterlist *src, *dst;
36         int ret, i;
37
38         ret = i915_mutex_lock_interruptible(obj->base.dev);
39         if (ret)
40                 return ERR_PTR(ret);
41
42         ret = i915_gem_object_get_pages(obj);
43         if (ret) {
44                 st = ERR_PTR(ret);
45                 goto out;
46         }
47
48         /* Copy sg so that we make an independent mapping */
49         st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
50         if (st == NULL) {
51                 st = ERR_PTR(-ENOMEM);
52                 goto out;
53         }
54
55         ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL);
56         if (ret) {
57                 kfree(st);
58                 st = ERR_PTR(ret);
59                 goto out;
60         }
61
62         src = obj->pages->sgl;
63         dst = st->sgl;
64         for (i = 0; i < obj->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                 sg_free_table(st);
72                 kfree(st);
73                 st = ERR_PTR(-ENOMEM);
74                 goto out;
75         }
76
77         i915_gem_object_pin_pages(obj);
78
79 out:
80         mutex_unlock(&obj->base.dev->struct_mutex);
81         return st;
82 }
83
84 static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
85                                    struct sg_table *sg,
86                                    enum dma_data_direction dir)
87 {
88         struct drm_i915_gem_object *obj = attachment->dmabuf->priv;
89
90         mutex_lock(&obj->base.dev->struct_mutex);
91
92         dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
93         sg_free_table(sg);
94         kfree(sg);
95
96         i915_gem_object_unpin_pages(obj);
97
98         mutex_unlock(&obj->base.dev->struct_mutex);
99 }
100
101 static void i915_gem_dmabuf_release(struct dma_buf *dma_buf)
102 {
103         struct drm_i915_gem_object *obj = dma_buf->priv;
104
105         if (obj->base.export_dma_buf == dma_buf) {
106                 /* drop the reference on the export fd holds */
107                 obj->base.export_dma_buf = NULL;
108                 drm_gem_object_unreference_unlocked(&obj->base);
109         }
110 }
111
112 static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
113 {
114         struct drm_i915_gem_object *obj = dma_buf->priv;
115         struct drm_device *dev = obj->base.dev;
116         struct sg_page_iter sg_iter;
117         struct page **pages;
118         int ret, i;
119
120         ret = i915_mutex_lock_interruptible(dev);
121         if (ret)
122                 return ERR_PTR(ret);
123
124         if (obj->dma_buf_vmapping) {
125                 obj->vmapping_count++;
126                 goto out_unlock;
127         }
128
129         ret = i915_gem_object_get_pages(obj);
130         if (ret)
131                 goto error;
132
133         ret = -ENOMEM;
134
135         pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
136         if (pages == NULL)
137                 goto error;
138
139         i = 0;
140         for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0)
141                 pages[i++] = sg_page_iter_page(&sg_iter);
142
143         obj->dma_buf_vmapping = vmap(pages, i, 0, PAGE_KERNEL);
144         drm_free_large(pages);
145
146         if (!obj->dma_buf_vmapping)
147                 goto error;
148
149         obj->vmapping_count = 1;
150         i915_gem_object_pin_pages(obj);
151 out_unlock:
152         mutex_unlock(&dev->struct_mutex);
153         return obj->dma_buf_vmapping;
154
155 error:
156         mutex_unlock(&dev->struct_mutex);
157         return ERR_PTR(ret);
158 }
159
160 static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
161 {
162         struct drm_i915_gem_object *obj = dma_buf->priv;
163         struct drm_device *dev = obj->base.dev;
164         int ret;
165
166         ret = i915_mutex_lock_interruptible(dev);
167         if (ret)
168                 return;
169
170         if (--obj->vmapping_count == 0) {
171                 vunmap(obj->dma_buf_vmapping);
172                 obj->dma_buf_vmapping = NULL;
173
174                 i915_gem_object_unpin_pages(obj);
175         }
176         mutex_unlock(&dev->struct_mutex);
177 }
178
179 static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
180 {
181         return NULL;
182 }
183
184 static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
185 {
186
187 }
188 static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
189 {
190         return NULL;
191 }
192
193 static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
194 {
195
196 }
197
198 static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
199 {
200         return -EINVAL;
201 }
202
203 static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
204 {
205         struct drm_i915_gem_object *obj = dma_buf->priv;
206         struct drm_device *dev = obj->base.dev;
207         int ret;
208         bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
209
210         ret = i915_mutex_lock_interruptible(dev);
211         if (ret)
212                 return ret;
213
214         ret = i915_gem_object_set_to_cpu_domain(obj, write);
215         mutex_unlock(&dev->struct_mutex);
216         return ret;
217 }
218
219 static const struct dma_buf_ops i915_dmabuf_ops =  {
220         .map_dma_buf = i915_gem_map_dma_buf,
221         .unmap_dma_buf = i915_gem_unmap_dma_buf,
222         .release = i915_gem_dmabuf_release,
223         .kmap = i915_gem_dmabuf_kmap,
224         .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
225         .kunmap = i915_gem_dmabuf_kunmap,
226         .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
227         .mmap = i915_gem_dmabuf_mmap,
228         .vmap = i915_gem_dmabuf_vmap,
229         .vunmap = i915_gem_dmabuf_vunmap,
230         .begin_cpu_access = i915_gem_begin_cpu_access,
231 };
232
233 struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
234                                       struct drm_gem_object *gem_obj, int flags)
235 {
236         struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
237
238         return dma_buf_export(obj, &i915_dmabuf_ops, obj->base.size, flags);
239 }
240
241 static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
242 {
243         struct sg_table *sg;
244
245         sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL);
246         if (IS_ERR(sg))
247                 return PTR_ERR(sg);
248
249         obj->pages = sg;
250         obj->has_dma_mapping = true;
251         return 0;
252 }
253
254 static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj)
255 {
256         dma_buf_unmap_attachment(obj->base.import_attach,
257                                  obj->pages, DMA_BIDIRECTIONAL);
258         obj->has_dma_mapping = false;
259 }
260
261 static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
262         .get_pages = i915_gem_object_get_pages_dmabuf,
263         .put_pages = i915_gem_object_put_pages_dmabuf,
264 };
265
266 struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
267                                              struct dma_buf *dma_buf)
268 {
269         struct dma_buf_attachment *attach;
270         struct drm_i915_gem_object *obj;
271         int ret;
272
273         /* is this one of own objects? */
274         if (dma_buf->ops == &i915_dmabuf_ops) {
275                 obj = dma_buf->priv;
276                 /* is it from our device? */
277                 if (obj->base.dev == dev) {
278                         /*
279                          * Importing dmabuf exported from out own gem increases
280                          * refcount on gem itself instead of f_count of dmabuf.
281                          */
282                         drm_gem_object_reference(&obj->base);
283                         return &obj->base;
284                 }
285         }
286
287         /* need to attach */
288         attach = dma_buf_attach(dma_buf, dev->dev);
289         if (IS_ERR(attach))
290                 return ERR_CAST(attach);
291
292         get_dma_buf(dma_buf);
293
294         obj = i915_gem_object_alloc(dev);
295         if (obj == NULL) {
296                 ret = -ENOMEM;
297                 goto fail_detach;
298         }
299
300         ret = drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
301         if (ret) {
302                 i915_gem_object_free(obj);
303                 goto fail_detach;
304         }
305
306         i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
307         obj->base.import_attach = attach;
308
309         return &obj->base;
310
311 fail_detach:
312         dma_buf_detach(dma_buf, attach);
313         dma_buf_put(dma_buf);
314
315         return ERR_PTR(ret);
316 }