]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/i915/i915_gem_dmabuf.c
Merge tag 'clock' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[karo-tx-linux.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 "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 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);
53 out:
54         mutex_unlock(&dev->struct_mutex);
55         return sg;
56 }
57
58 static 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
66 static 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
77 static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
78 {
79         return NULL;
80 }
81
82 static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
83 {
84
85 }
86 static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
87 {
88         return NULL;
89 }
90
91 static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
92 {
93
94 }
95
96 static const struct dma_buf_ops i915_dmabuf_ops =  {
97         .map_dma_buf = i915_gem_map_dma_buf,
98         .unmap_dma_buf = i915_gem_unmap_dma_buf,
99         .release = i915_gem_dmabuf_release,
100         .kmap = i915_gem_dmabuf_kmap,
101         .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
102         .kunmap = i915_gem_dmabuf_kunmap,
103         .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
104 };
105
106 struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
107                                 struct drm_gem_object *gem_obj, int flags)
108 {
109         struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
110
111         return dma_buf_export(obj, &i915_dmabuf_ops,
112                                                   obj->base.size, 0600);
113 }
114
115 struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
116                                 struct dma_buf *dma_buf)
117 {
118         struct dma_buf_attachment *attach;
119         struct sg_table *sg;
120         struct drm_i915_gem_object *obj;
121         int npages;
122         int size;
123         int ret;
124
125         /* is this one of own objects? */
126         if (dma_buf->ops == &i915_dmabuf_ops) {
127                 obj = dma_buf->priv;
128                 /* is it from our device? */
129                 if (obj->base.dev == dev) {
130                         drm_gem_object_reference(&obj->base);
131                         return &obj->base;
132                 }
133         }
134
135         /* need to attach */
136         attach = dma_buf_attach(dma_buf, dev->dev);
137         if (IS_ERR(attach))
138                 return ERR_CAST(attach);
139
140         sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
141         if (IS_ERR(sg)) {
142                 ret = PTR_ERR(sg);
143                 goto fail_detach;
144         }
145
146         size = dma_buf->size;
147         npages = size / PAGE_SIZE;
148
149         obj = kzalloc(sizeof(*obj), GFP_KERNEL);
150         if (obj == NULL) {
151                 ret = -ENOMEM;
152                 goto fail_unmap;
153         }
154
155         ret = drm_gem_private_object_init(dev, &obj->base, size);
156         if (ret) {
157                 kfree(obj);
158                 goto fail_unmap;
159         }
160
161         obj->sg_table = sg;
162         obj->base.import_attach = attach;
163
164         return &obj->base;
165
166 fail_unmap:
167         dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
168 fail_detach:
169         dma_buf_detach(dma_buf, attach);
170         return ERR_PTR(ret);
171 }