tizen 2.4 release
[kernel/linux-3.0.git] / drivers / gpu / drm / drm_prime.c
1 /*
2  * Copyright © 2012 Red Hat
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 DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Dave Airlie <airlied@redhat.com>
25  *      Rob Clark <rob.clark@linaro.org>
26  *
27  */
28
29 #include <linux/dma-buf.h>
30 #include "drmP.h"
31
32 /*
33  * DMA-BUF/GEM Object references and lifetime overview:
34  *
35  * On the export the dma_buf holds a reference to the exporting GEM
36  * object. It takes this reference in handle_to_fd_ioctl, when it
37  * first calls .prime_export and stores the exporting GEM object in
38  * the dma_buf priv. This reference is released when the dma_buf
39  * object goes away in the driver .release function.
40  *
41  * On the import the importing GEM object holds a reference to the
42  * dma_buf (which in turn holds a ref to the exporting GEM object).
43  * It takes that reference in the fd_to_handle ioctl.
44  * It calls dma_buf_get, creates an attachment to it and stores the
45  * attachment in the GEM object. When this attachment is destroyed
46  * when the imported object is destroyed, we remove the attachment
47  * and drop the reference to the dma_buf.
48  *
49  * Thus the chain of references always flows in one direction
50  * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
51  *
52  * Self-importing: if userspace is using PRIME as a replacement for flink
53  * then it will get a fd->handle request for a GEM object that it created.
54  * Drivers should detect this situation and return back the gem object
55  * from the dma-buf private.
56  */
57
58 struct drm_prime_member {
59         struct list_head entry;
60         struct dma_buf *dma_buf;
61         uint32_t handle;
62 };
63 static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
64
65 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
66                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
67                 int *prime_fd)
68 {
69         struct drm_gem_object *obj;
70         void *buf;
71         int ret = 0;
72         struct dma_buf *dmabuf;
73
74         obj = drm_gem_object_lookup(dev, file_priv, handle);
75         if (!obj)
76                 return -ENOENT;
77
78         mutex_lock(&file_priv->prime.lock);
79         /* re-export the original imported object */
80         if (obj->import_attach) {
81                 dmabuf = obj->import_attach->dmabuf;
82                 goto out_have_obj;
83         }
84
85         if (obj->export_dma_buf) {
86                 dmabuf = obj->export_dma_buf;
87
88                 ret = drm_prime_add_buf_handle(&file_priv->prime,
89                                                dmabuf, handle);
90                 goto out_have_obj;
91         }
92
93         buf = dev->driver->gem_prime_export(dev, obj, flags);
94         if (IS_ERR(buf)) {
95                 /* normally the created dma-buf takes ownership of the ref,
96                  * but if that fails then drop the ref
97                  */
98                 ret = PTR_ERR(buf);
99                 goto out;
100         }
101         obj->export_dma_buf = buf;
102
103         /* if we've exported this buffer the cheat and add it to the import list
104          * so we get the correct handle back
105          */
106         ret = drm_prime_add_buf_handle(&file_priv->prime,
107                                        obj->export_dma_buf, handle);
108         if (ret)
109                 goto out;
110
111         *prime_fd = dma_buf_fd(buf, flags);
112         mutex_unlock(&file_priv->prime.lock);
113         return 0;
114
115 out_have_obj:
116         get_dma_buf(dmabuf);
117         *prime_fd = dma_buf_fd(dmabuf, flags);
118 out:
119         drm_gem_object_unreference_unlocked(obj);
120         mutex_unlock(&file_priv->prime.lock);
121         return ret;
122 }
123 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
124
125 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
126                 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
127 {
128         struct dma_buf *dma_buf;
129         struct drm_gem_object *obj;
130         int ret;
131
132         dma_buf = dma_buf_get(prime_fd);
133         if (IS_ERR(dma_buf))
134                 return PTR_ERR(dma_buf);
135
136         mutex_lock(&file_priv->prime.lock);
137
138         ret = drm_prime_lookup_buf_handle(&file_priv->prime,
139                         dma_buf, handle);
140         if (!ret) {
141                 ret = 0;
142                 goto out_put;
143         }
144
145         /* never seen this one, need to import */
146         obj = dev->driver->gem_prime_import(dev, dma_buf);
147         if (IS_ERR(obj)) {
148                 ret = PTR_ERR(obj);
149                 goto out_put;
150         }
151
152         ret = drm_gem_handle_create(file_priv, obj, handle);
153         drm_gem_object_unreference_unlocked(obj);
154         if (ret)
155                 goto out_put;
156
157         ret = drm_prime_add_buf_handle(&file_priv->prime,
158                         dma_buf, *handle);
159         if (ret)
160                 goto fail;
161
162         mutex_unlock(&file_priv->prime.lock);
163         return 0;
164
165 fail:
166         /* hmm, if driver attached, we are relying on the free-object path
167          * to detach.. which seems ok..
168          */
169         drm_gem_object_handle_unreference_unlocked(obj);
170 out_put:
171         dma_buf_put(dma_buf);
172         mutex_unlock(&file_priv->prime.lock);
173         return ret;
174 }
175 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
176
177 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
178                                  struct drm_file *file_priv)
179 {
180         struct drm_prime_handle *args = data;
181         uint32_t flags;
182
183         if (!drm_core_check_feature(dev, DRIVER_PRIME))
184                 return -EINVAL;
185
186         if (!dev->driver->prime_handle_to_fd)
187                 return -ENOSYS;
188
189         /* check flags are valid */
190         if (args->flags & ~DRM_CLOEXEC)
191                 return -EINVAL;
192
193         /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
194         flags = args->flags & DRM_CLOEXEC;
195
196         return dev->driver->prime_handle_to_fd(dev, file_priv,
197                         args->handle, flags, &args->fd);
198 }
199
200 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
201                                  struct drm_file *file_priv)
202 {
203         struct drm_prime_handle *args = data;
204
205         if (!drm_core_check_feature(dev, DRIVER_PRIME))
206                 return -EINVAL;
207
208         if (!dev->driver->prime_fd_to_handle)
209                 return -ENOSYS;
210
211         return dev->driver->prime_fd_to_handle(dev, file_priv,
212                         args->fd, &args->handle);
213 }
214
215 /*
216  * drm_prime_pages_to_sg
217  *
218  * this helper creates an sg table object from a set of pages
219  * the driver is responsible for mapping the pages into the
220  * importers address space
221  */
222 struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
223 {
224         struct sg_table *sg = NULL;
225         struct scatterlist *iter;
226         int i;
227         int ret;
228
229         sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
230         if (!sg)
231                 goto out;
232
233         ret = sg_alloc_table(sg, nr_pages, GFP_KERNEL);
234         if (ret)
235                 goto out;
236
237         for_each_sg(sg->sgl, iter, nr_pages, i)
238                 sg_set_page(iter, pages[i], PAGE_SIZE, 0);
239
240         return sg;
241 out:
242         kfree(sg);
243         return NULL;
244 }
245 EXPORT_SYMBOL(drm_prime_pages_to_sg);
246
247 /* helper function to cleanup a GEM/prime object */
248 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
249 {
250         struct dma_buf_attachment *attach;
251         struct dma_buf *dma_buf;
252         attach = obj->import_attach;
253         if (sg)
254                 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
255         dma_buf = attach->dmabuf;
256         dma_buf_detach(attach->dmabuf, attach);
257         /* remove the reference */
258         dma_buf_put(dma_buf);
259 }
260 EXPORT_SYMBOL(drm_prime_gem_destroy);
261
262 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
263 {
264         INIT_LIST_HEAD(&prime_fpriv->head);
265         mutex_init(&prime_fpriv->lock);
266 }
267 EXPORT_SYMBOL(drm_prime_init_file_private);
268
269 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
270 {
271         struct drm_prime_member *member, *safe;
272         list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
273                 list_del(&member->entry);
274                 kfree(member);
275         }
276 }
277 EXPORT_SYMBOL(drm_prime_destroy_file_private);
278
279 static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
280 {
281         struct drm_prime_member *member;
282
283         member = kmalloc(sizeof(*member), GFP_KERNEL);
284         if (!member)
285                 return -ENOMEM;
286
287         get_dma_buf(dma_buf);
288         member->dma_buf = dma_buf;
289         member->handle = handle;
290         list_add(&member->entry, &prime_fpriv->head);
291         return 0;
292 }
293
294 int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
295 {
296         struct drm_prime_member *member;
297
298         list_for_each_entry(member, &prime_fpriv->head, entry) {
299                 if (member->dma_buf == dma_buf) {
300                         *handle = member->handle;
301                         return 0;
302                 }
303         }
304         return -ENOENT;
305 }
306 EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
307
308 void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
309 {
310         struct drm_prime_member *member, *safe;
311
312         mutex_lock(&prime_fpriv->lock);
313         list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
314                 if (member->dma_buf == dma_buf) {
315                         dma_buf_put(dma_buf);
316                         list_del(&member->entry);
317                         kfree(member);
318                 }
319         }
320         mutex_unlock(&prime_fpriv->lock);
321 }
322 EXPORT_SYMBOL(drm_prime_remove_buf_handle);