Merge branch 'for-airlied' of git://people.freedesktop.org/~danvet/drm-intel into...
[deliverable/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;
37 int ret;
38 int nents;
39
40 ret = i915_mutex_lock_interruptible(dev);
41 if (ret)
42 return ERR_PTR(ret);
43
44 ret = i915_gem_object_get_pages_gtt(obj);
45 if (ret) {
46 sg = ERR_PTR(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_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 ret = i915_gem_object_get_pages_gtt(obj);
93 if (ret) {
94 mutex_unlock(&dev->struct_mutex);
95 return ERR_PTR(ret);
96 }
97
98 obj->dma_buf_vmapping = vmap(obj->pages, obj->base.size / PAGE_SIZE, 0, PAGE_KERNEL);
99 if (!obj->dma_buf_vmapping) {
100 DRM_ERROR("failed to vmap object\n");
101 goto out_unlock;
102 }
103
104 obj->vmapping_count = 1;
105 out_unlock:
106 mutex_unlock(&dev->struct_mutex);
107 return obj->dma_buf_vmapping;
108 }
109
110 static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
111 {
112 struct drm_i915_gem_object *obj = dma_buf->priv;
113 struct drm_device *dev = obj->base.dev;
114 int ret;
115
116 ret = i915_mutex_lock_interruptible(dev);
117 if (ret)
118 return;
119
120 --obj->vmapping_count;
121 if (obj->vmapping_count == 0) {
122 vunmap(obj->dma_buf_vmapping);
123 obj->dma_buf_vmapping = NULL;
124 }
125 mutex_unlock(&dev->struct_mutex);
126 }
127
128 static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
129 {
130 return NULL;
131 }
132
133 static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
134 {
135
136 }
137 static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
138 {
139 return NULL;
140 }
141
142 static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
143 {
144
145 }
146
147 static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
148 {
149 return -EINVAL;
150 }
151
152 static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
153 {
154 struct drm_i915_gem_object *obj = dma_buf->priv;
155 struct drm_device *dev = obj->base.dev;
156 int ret;
157 bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
158
159 ret = i915_mutex_lock_interruptible(dev);
160 if (ret)
161 return ret;
162
163 ret = i915_gem_object_set_to_cpu_domain(obj, write);
164 mutex_unlock(&dev->struct_mutex);
165 return ret;
166 }
167
168 static const struct dma_buf_ops i915_dmabuf_ops = {
169 .map_dma_buf = i915_gem_map_dma_buf,
170 .unmap_dma_buf = i915_gem_unmap_dma_buf,
171 .release = i915_gem_dmabuf_release,
172 .kmap = i915_gem_dmabuf_kmap,
173 .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
174 .kunmap = i915_gem_dmabuf_kunmap,
175 .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
176 .mmap = i915_gem_dmabuf_mmap,
177 .vmap = i915_gem_dmabuf_vmap,
178 .vunmap = i915_gem_dmabuf_vunmap,
179 .begin_cpu_access = i915_gem_begin_cpu_access,
180 };
181
182 struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
183 struct drm_gem_object *gem_obj, int flags)
184 {
185 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
186
187 return dma_buf_export(obj, &i915_dmabuf_ops,
188 obj->base.size, 0600);
189 }
190
191 struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
192 struct dma_buf *dma_buf)
193 {
194 struct dma_buf_attachment *attach;
195 struct sg_table *sg;
196 struct drm_i915_gem_object *obj;
197 int npages;
198 int size;
199 int ret;
200
201 /* is this one of own objects? */
202 if (dma_buf->ops == &i915_dmabuf_ops) {
203 obj = dma_buf->priv;
204 /* is it from our device? */
205 if (obj->base.dev == dev) {
206 drm_gem_object_reference(&obj->base);
207 return &obj->base;
208 }
209 }
210
211 /* need to attach */
212 attach = dma_buf_attach(dma_buf, dev->dev);
213 if (IS_ERR(attach))
214 return ERR_CAST(attach);
215
216 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
217 if (IS_ERR(sg)) {
218 ret = PTR_ERR(sg);
219 goto fail_detach;
220 }
221
222 size = dma_buf->size;
223 npages = size / PAGE_SIZE;
224
225 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
226 if (obj == NULL) {
227 ret = -ENOMEM;
228 goto fail_unmap;
229 }
230
231 ret = drm_gem_private_object_init(dev, &obj->base, size);
232 if (ret) {
233 kfree(obj);
234 goto fail_unmap;
235 }
236
237 obj->sg_table = sg;
238 obj->base.import_attach = attach;
239
240 return &obj->base;
241
242 fail_unmap:
243 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
244 fail_detach:
245 dma_buf_detach(dma_buf, attach);
246 return ERR_PTR(ret);
247 }
This page took 0.037083 seconds and 6 git commands to generate.