Revert "drm/prime: resolve race for drm_gem_prime_handle_to_fd()"
[profile/mobile/platform/kernel/linux-3.10-sc7730.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/export.h>
30 #include <linux/dma-buf.h>
31 #include <drm/drmP.h>
32
33 /*
34  * DMA-BUF/GEM Object references and lifetime overview:
35  *
36  * On the export the dma_buf holds a reference to the exporting GEM
37  * object. It takes this reference in handle_to_fd_ioctl, when it
38  * first calls .prime_export and stores the exporting GEM object in
39  * the dma_buf priv. This reference is released when the dma_buf
40  * object goes away in the driver .release function.
41  *
42  * On the import the importing GEM object holds a reference to the
43  * dma_buf (which in turn holds a ref to the exporting GEM object).
44  * It takes that reference in the fd_to_handle ioctl.
45  * It calls dma_buf_get, creates an attachment to it and stores the
46  * attachment in the GEM object. When this attachment is destroyed
47  * when the imported object is destroyed, we remove the attachment
48  * and drop the reference to the dma_buf.
49  *
50  * Thus the chain of references always flows in one direction
51  * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
52  *
53  * Self-importing: if userspace is using PRIME as a replacement for flink
54  * then it will get a fd->handle request for a GEM object that it created.
55  * Drivers should detect this situation and return back the gem object
56  * from the dma-buf private.  Prime will do this automatically for drivers that
57  * use the drm_gem_prime_{import,export} helpers.
58  */
59
60 struct drm_prime_member {
61         struct list_head entry;
62         struct dma_buf *dma_buf;
63         uint32_t handle;
64 };
65
66 static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
67                 enum dma_data_direction dir)
68 {
69         struct drm_gem_object *obj = attach->dmabuf->priv;
70         struct sg_table *sgt;
71
72         mutex_lock(&obj->dev->struct_mutex);
73
74         sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
75
76         if (!IS_ERR_OR_NULL(sgt))
77                 dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
78
79         mutex_unlock(&obj->dev->struct_mutex);
80         return sgt;
81 }
82
83 static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
84                 struct sg_table *sgt, enum dma_data_direction dir)
85 {
86         dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
87         sg_free_table(sgt);
88         kfree(sgt);
89 }
90
91 static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
92 {
93         struct drm_gem_object *obj = dma_buf->priv;
94
95         if (obj->export_dma_buf == dma_buf) {
96                 /* drop the reference on the export fd holds */
97                 obj->export_dma_buf = NULL;
98                 drm_gem_object_unreference_unlocked(obj);
99         }
100 }
101
102 static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
103 {
104         struct drm_gem_object *obj = dma_buf->priv;
105         struct drm_device *dev = obj->dev;
106
107         return dev->driver->gem_prime_vmap(obj);
108 }
109
110 static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
111 {
112         struct drm_gem_object *obj = dma_buf->priv;
113         struct drm_device *dev = obj->dev;
114
115         dev->driver->gem_prime_vunmap(obj, vaddr);
116 }
117
118 static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
119                 unsigned long page_num)
120 {
121         return NULL;
122 }
123
124 static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
125                 unsigned long page_num, void *addr)
126 {
127
128 }
129 static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
130                 unsigned long page_num)
131 {
132         return NULL;
133 }
134
135 static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
136                 unsigned long page_num, void *addr)
137 {
138
139 }
140
141 static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
142                 struct vm_area_struct *vma)
143 {
144         return -EINVAL;
145 }
146
147 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
148         .map_dma_buf = drm_gem_map_dma_buf,
149         .unmap_dma_buf = drm_gem_unmap_dma_buf,
150         .release = drm_gem_dmabuf_release,
151         .kmap = drm_gem_dmabuf_kmap,
152         .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
153         .kunmap = drm_gem_dmabuf_kunmap,
154         .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
155         .mmap = drm_gem_dmabuf_mmap,
156         .vmap = drm_gem_dmabuf_vmap,
157         .vunmap = drm_gem_dmabuf_vunmap,
158 };
159
160 /**
161  * DOC: PRIME Helpers
162  *
163  * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
164  * simpler APIs by using the helper functions @drm_gem_prime_export and
165  * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
166  * five lower-level driver callbacks:
167  *
168  * Export callbacks:
169  *
170  *  - @gem_prime_pin (optional): prepare a GEM object for exporting
171  *
172  *  - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
173  *
174  *  - @gem_prime_vmap: vmap a buffer exported by your driver
175  *
176  *  - @gem_prime_vunmap: vunmap a buffer exported by your driver
177  *
178  * Import callback:
179  *
180  *  - @gem_prime_import_sg_table (import): produce a GEM object from another
181  *    driver's scatter/gather table
182  */
183
184 struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
185                                      struct drm_gem_object *obj, int flags)
186 {
187         if (dev->driver->gem_prime_pin) {
188                 int ret = dev->driver->gem_prime_pin(obj);
189                 if (ret)
190                         return ERR_PTR(ret);
191         }
192         return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
193 }
194 EXPORT_SYMBOL(drm_gem_prime_export);
195
196 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
197                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
198                 int *prime_fd)
199 {
200         struct drm_gem_object *obj;
201         void *buf;
202         int ret = 0;
203         struct dma_buf *dmabuf;
204
205         obj = drm_gem_object_lookup(dev, file_priv, handle);
206         if (!obj)
207                 return -ENOENT;
208
209         mutex_lock(&file_priv->prime.lock);
210         /* re-export the original imported object */
211         if (obj->import_attach) {
212                 dmabuf = obj->import_attach->dmabuf;
213                 goto out_have_obj;
214         }
215
216         if (obj->export_dma_buf) {
217                 dmabuf = obj->export_dma_buf;
218                 goto out_have_obj;
219         }
220
221         buf = dev->driver->gem_prime_export(dev, obj, flags);
222         if (IS_ERR(buf)) {
223                 /* normally the created dma-buf takes ownership of the ref,
224                  * but if that fails then drop the ref
225                  */
226                 ret = PTR_ERR(buf);
227                 goto out;
228         }
229         obj->export_dma_buf = buf;
230
231         /* if we've exported this buffer the cheat and add it to the import list
232          * so we get the correct handle back
233          */
234         ret = drm_prime_add_buf_handle(&file_priv->prime,
235                                        obj->export_dma_buf, handle);
236         if (ret)
237                 goto out;
238
239         *prime_fd = dma_buf_fd(buf, flags);
240         mutex_unlock(&file_priv->prime.lock);
241         return 0;
242
243 out_have_obj:
244         get_dma_buf(dmabuf);
245         *prime_fd = dma_buf_fd(dmabuf, flags);
246 out:
247         drm_gem_object_unreference_unlocked(obj);
248         mutex_unlock(&file_priv->prime.lock);
249         return ret;
250 }
251 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
252
253 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
254                                             struct dma_buf *dma_buf)
255 {
256         struct dma_buf_attachment *attach;
257         struct sg_table *sgt;
258         struct drm_gem_object *obj;
259         int ret;
260
261         if (!dev->driver->gem_prime_import_sg_table)
262                 return ERR_PTR(-EINVAL);
263
264         if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
265                 obj = dma_buf->priv;
266                 if (obj->dev == dev) {
267                         /*
268                          * Importing dmabuf exported from out own gem increases
269                          * refcount on gem itself instead of f_count of dmabuf.
270                          */
271                         drm_gem_object_reference(obj);
272                         return obj;
273                 }
274         }
275
276         attach = dma_buf_attach(dma_buf, dev->dev);
277         if (IS_ERR(attach))
278                 return ERR_PTR(PTR_ERR(attach));
279
280         get_dma_buf(dma_buf);
281
282         sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
283         if (IS_ERR_OR_NULL(sgt)) {
284                 ret = PTR_ERR(sgt);
285                 goto fail_detach;
286         }
287
288         obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
289         if (IS_ERR(obj)) {
290                 ret = PTR_ERR(obj);
291                 goto fail_unmap;
292         }
293
294         obj->import_attach = attach;
295
296         return obj;
297
298 fail_unmap:
299         dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
300 fail_detach:
301         dma_buf_detach(dma_buf, attach);
302         dma_buf_put(dma_buf);
303
304         return ERR_PTR(ret);
305 }
306 EXPORT_SYMBOL(drm_gem_prime_import);
307
308 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
309                 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
310 {
311         struct dma_buf *dma_buf;
312         struct drm_gem_object *obj;
313         int ret;
314
315         dma_buf = dma_buf_get(prime_fd);
316         if (IS_ERR(dma_buf))
317                 return PTR_ERR(dma_buf);
318
319         mutex_lock(&file_priv->prime.lock);
320
321         ret = drm_prime_lookup_buf_handle(&file_priv->prime,
322                         dma_buf, handle);
323         if (!ret) {
324                 ret = 0;
325                 goto out_put;
326         }
327
328         /* never seen this one, need to import */
329         obj = dev->driver->gem_prime_import(dev, dma_buf);
330         if (IS_ERR(obj)) {
331                 ret = PTR_ERR(obj);
332                 goto out_put;
333         }
334
335         ret = drm_gem_handle_create(file_priv, obj, handle);
336         drm_gem_object_unreference_unlocked(obj);
337         if (ret)
338                 goto out_put;
339
340         ret = drm_prime_add_buf_handle(&file_priv->prime,
341                         dma_buf, *handle);
342         if (ret)
343                 goto fail;
344
345         mutex_unlock(&file_priv->prime.lock);
346
347         dma_buf_put(dma_buf);
348
349         return 0;
350
351 fail:
352         /* hmm, if driver attached, we are relying on the free-object path
353          * to detach.. which seems ok..
354          */
355         drm_gem_object_handle_unreference_unlocked(obj);
356 out_put:
357         dma_buf_put(dma_buf);
358         mutex_unlock(&file_priv->prime.lock);
359         return ret;
360 }
361 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
362
363 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
364                                  struct drm_file *file_priv)
365 {
366         struct drm_prime_handle *args = data;
367         uint32_t flags;
368
369         if (!drm_core_check_feature(dev, DRIVER_PRIME))
370                 return -EINVAL;
371
372         if (!dev->driver->prime_handle_to_fd)
373                 return -ENOSYS;
374
375         /* check flags are valid */
376         if (args->flags & ~DRM_CLOEXEC)
377                 return -EINVAL;
378
379         /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
380         flags = args->flags & DRM_CLOEXEC;
381
382         return dev->driver->prime_handle_to_fd(dev, file_priv,
383                         args->handle, flags, &args->fd);
384 }
385
386 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
387                                  struct drm_file *file_priv)
388 {
389         struct drm_prime_handle *args = data;
390
391         if (!drm_core_check_feature(dev, DRIVER_PRIME))
392                 return -EINVAL;
393
394         if (!dev->driver->prime_fd_to_handle)
395                 return -ENOSYS;
396
397         return dev->driver->prime_fd_to_handle(dev, file_priv,
398                         args->fd, &args->handle);
399 }
400
401 /*
402  * drm_prime_pages_to_sg
403  *
404  * this helper creates an sg table object from a set of pages
405  * the driver is responsible for mapping the pages into the
406  * importers address space
407  */
408 struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
409 {
410         struct sg_table *sg = NULL;
411         int ret;
412
413         sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
414         if (!sg)
415                 goto out;
416
417         ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
418                                 nr_pages << PAGE_SHIFT, GFP_KERNEL);
419         if (ret)
420                 goto out;
421
422         return sg;
423 out:
424         kfree(sg);
425         return NULL;
426 }
427 EXPORT_SYMBOL(drm_prime_pages_to_sg);
428
429 /* export an sg table into an array of pages and addresses
430    this is currently required by the TTM driver in order to do correct fault
431    handling */
432 int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
433                                      dma_addr_t *addrs, int max_pages)
434 {
435         unsigned count;
436         struct scatterlist *sg;
437         struct page *page;
438         u32 len, offset;
439         int pg_index;
440         dma_addr_t addr;
441
442         pg_index = 0;
443         for_each_sg(sgt->sgl, sg, sgt->nents, count) {
444                 len = sg->length;
445                 offset = sg->offset;
446                 page = sg_page(sg);
447                 addr = sg_dma_address(sg);
448
449                 while (len > 0) {
450                         if (WARN_ON(pg_index >= max_pages))
451                                 return -1;
452                         pages[pg_index] = page;
453                         if (addrs)
454                                 addrs[pg_index] = addr;
455
456                         page++;
457                         addr += PAGE_SIZE;
458                         len -= PAGE_SIZE;
459                         pg_index++;
460                 }
461         }
462         return 0;
463 }
464 EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
465 /* helper function to cleanup a GEM/prime object */
466 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
467 {
468         struct dma_buf_attachment *attach;
469         struct dma_buf *dma_buf;
470         attach = obj->import_attach;
471         if (sg)
472                 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
473         dma_buf = attach->dmabuf;
474         dma_buf_detach(attach->dmabuf, attach);
475         /* remove the reference */
476         dma_buf_put(dma_buf);
477 }
478 EXPORT_SYMBOL(drm_prime_gem_destroy);
479
480 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
481 {
482         INIT_LIST_HEAD(&prime_fpriv->head);
483         mutex_init(&prime_fpriv->lock);
484 }
485 EXPORT_SYMBOL(drm_prime_init_file_private);
486
487 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
488 {
489         /* by now drm_gem_release should've made sure the list is empty */
490         WARN_ON(!list_empty(&prime_fpriv->head));
491 }
492 EXPORT_SYMBOL(drm_prime_destroy_file_private);
493
494 int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
495 {
496         struct drm_prime_member *member;
497
498         member = kmalloc(sizeof(*member), GFP_KERNEL);
499         if (!member)
500                 return -ENOMEM;
501
502         get_dma_buf(dma_buf);
503         member->dma_buf = dma_buf;
504         member->handle = handle;
505         list_add(&member->entry, &prime_fpriv->head);
506         return 0;
507 }
508 EXPORT_SYMBOL(drm_prime_add_buf_handle);
509
510 int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
511 {
512         struct drm_prime_member *member;
513
514         list_for_each_entry(member, &prime_fpriv->head, entry) {
515                 if (member->dma_buf == dma_buf) {
516                         *handle = member->handle;
517                         return 0;
518                 }
519         }
520         return -ENOENT;
521 }
522 EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
523
524 void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
525 {
526         struct drm_prime_member *member, *safe;
527
528         mutex_lock(&prime_fpriv->lock);
529         list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
530                 if (member->dma_buf == dma_buf) {
531                         dma_buf_put(dma_buf);
532                         list_del(&member->entry);
533                         kfree(member);
534                 }
535         }
536         mutex_unlock(&prime_fpriv->lock);
537 }
538 EXPORT_SYMBOL(drm_prime_remove_buf_handle);