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