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