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