blob: 1e0de41f085c2419863ee5c462d6cbf54ce77c8a [file] [log] [blame]
Dave Airlie32488772011-11-25 15:21:02 +00001/*
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>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
Dave Airlie32488772011-11-25 15:21:02 +000032
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
Aaron Plattner89177642013-01-15 20:47:42 +000056 * from the dma-buf private. Prime will do this automatically for drivers that
57 * use the drm_gem_prime_{import,export} helpers.
Dave Airlie32488772011-11-25 15:21:02 +000058 */
59
60struct drm_prime_member {
61 struct list_head entry;
62 struct dma_buf *dma_buf;
63 uint32_t handle;
64};
Joonyoung Shim538d6662013-06-19 15:03:05 +090065
66struct drm_prime_attachment {
67 struct sg_table *sgt;
68 enum dma_data_direction dir;
69};
70
Seung-Woo Kimce92e3c2013-06-26 10:21:41 +090071static 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}
Dave Airlie32488772011-11-25 15:21:02 +000085
Maarten Lankhorstca793f72013-04-09 09:52:54 +020086static int drm_gem_map_attach(struct dma_buf *dma_buf,
87 struct device *target_dev,
88 struct dma_buf_attachment *attach)
89{
Joonyoung Shim538d6662013-06-19 15:03:05 +090090 struct drm_prime_attachment *prime_attach;
Maarten Lankhorstca793f72013-04-09 09:52:54 +020091 struct drm_gem_object *obj = dma_buf->priv;
92 struct drm_device *dev = obj->dev;
93
Joonyoung Shim538d6662013-06-19 15:03:05 +090094 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
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200101 if (!dev->driver->gem_prime_pin)
102 return 0;
103
104 return dev->driver->gem_prime_pin(obj);
105}
106
107static void drm_gem_map_detach(struct dma_buf *dma_buf,
108 struct dma_buf_attachment *attach)
109{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900110 struct drm_prime_attachment *prime_attach = attach->priv;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200111 struct drm_gem_object *obj = dma_buf->priv;
112 struct drm_device *dev = obj->dev;
Joonyoung Shim538d6662013-06-19 15:03:05 +0900113 struct sg_table *sgt;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200114
115 if (dev->driver->gem_prime_unpin)
116 dev->driver->gem_prime_unpin(obj);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900117
118 if (!prime_attach)
119 return;
120
121 sgt = prime_attach->sgt;
122
123 if (prime_attach->dir != DMA_NONE)
124 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
125 prime_attach->dir);
126
127 sg_free_table(sgt);
128 kfree(sgt);
129 kfree(prime_attach);
130 attach->priv = NULL;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200131}
132
YoungJun Choda342422013-06-26 10:21:42 +0900133static void drm_prime_remove_buf_handle_locked(
134 struct drm_prime_file_private *prime_fpriv,
135 struct dma_buf *dma_buf)
136{
137 struct drm_prime_member *member, *safe;
138
139 list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
140 if (member->dma_buf == dma_buf) {
141 dma_buf_put(dma_buf);
142 list_del(&member->entry);
143 kfree(member);
144 }
145 }
146}
147
Aaron Plattner89177642013-01-15 20:47:42 +0000148static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
149 enum dma_data_direction dir)
150{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900151 struct drm_prime_attachment *prime_attach = attach->priv;
Aaron Plattner89177642013-01-15 20:47:42 +0000152 struct drm_gem_object *obj = attach->dmabuf->priv;
153 struct sg_table *sgt;
154
Joonyoung Shim538d6662013-06-19 15:03:05 +0900155 if (WARN_ON(dir == DMA_NONE || !prime_attach))
156 return ERR_PTR(-EINVAL);
157
158 /* return the cached mapping when possible */
159 if (prime_attach->dir == dir)
160 return prime_attach->sgt;
161
162 /*
163 * two mappings with different directions for the same attachment are
164 * not allowed
165 */
166 if (WARN_ON(prime_attach->dir != DMA_NONE))
167 return ERR_PTR(-EBUSY);
168
Aaron Plattner89177642013-01-15 20:47:42 +0000169 mutex_lock(&obj->dev->struct_mutex);
170
171 sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
172
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900173 if (!IS_ERR(sgt)) {
YoungJun Chob720d542013-06-24 15:34:21 +0900174 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
175 sg_free_table(sgt);
176 kfree(sgt);
177 sgt = ERR_PTR(-ENOMEM);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900178 } else {
179 prime_attach->sgt = sgt;
180 prime_attach->dir = dir;
YoungJun Chob720d542013-06-24 15:34:21 +0900181 }
182 }
Aaron Plattner89177642013-01-15 20:47:42 +0000183
184 mutex_unlock(&obj->dev->struct_mutex);
185 return sgt;
186}
187
188static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
189 struct sg_table *sgt, enum dma_data_direction dir)
190{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900191 /* nothing to be done here */
Aaron Plattner89177642013-01-15 20:47:42 +0000192}
193
194static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
195{
196 struct drm_gem_object *obj = dma_buf->priv;
197
198 if (obj->export_dma_buf == dma_buf) {
199 /* drop the reference on the export fd holds */
200 obj->export_dma_buf = NULL;
201 drm_gem_object_unreference_unlocked(obj);
202 }
203}
204
205static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
206{
207 struct drm_gem_object *obj = dma_buf->priv;
208 struct drm_device *dev = obj->dev;
209
210 return dev->driver->gem_prime_vmap(obj);
211}
212
213static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
214{
215 struct drm_gem_object *obj = dma_buf->priv;
216 struct drm_device *dev = obj->dev;
217
218 dev->driver->gem_prime_vunmap(obj, vaddr);
219}
220
221static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
222 unsigned long page_num)
223{
224 return NULL;
225}
226
227static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
228 unsigned long page_num, void *addr)
229{
230
231}
232static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
233 unsigned long page_num)
234{
235 return NULL;
236}
237
238static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
239 unsigned long page_num, void *addr)
240{
241
242}
243
244static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
245 struct vm_area_struct *vma)
246{
247 return -EINVAL;
248}
249
250static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200251 .attach = drm_gem_map_attach,
252 .detach = drm_gem_map_detach,
Aaron Plattner89177642013-01-15 20:47:42 +0000253 .map_dma_buf = drm_gem_map_dma_buf,
254 .unmap_dma_buf = drm_gem_unmap_dma_buf,
255 .release = drm_gem_dmabuf_release,
256 .kmap = drm_gem_dmabuf_kmap,
257 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
258 .kunmap = drm_gem_dmabuf_kunmap,
259 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
260 .mmap = drm_gem_dmabuf_mmap,
261 .vmap = drm_gem_dmabuf_vmap,
262 .vunmap = drm_gem_dmabuf_vunmap,
263};
264
265/**
266 * DOC: PRIME Helpers
267 *
268 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
269 * simpler APIs by using the helper functions @drm_gem_prime_export and
270 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
271 * five lower-level driver callbacks:
272 *
273 * Export callbacks:
274 *
275 * - @gem_prime_pin (optional): prepare a GEM object for exporting
276 *
277 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
278 *
279 * - @gem_prime_vmap: vmap a buffer exported by your driver
280 *
281 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
282 *
283 * Import callback:
284 *
285 * - @gem_prime_import_sg_table (import): produce a GEM object from another
286 * driver's scatter/gather table
287 */
288
289struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
290 struct drm_gem_object *obj, int flags)
291{
Laurent Pinchartebc0bad2013-06-19 03:14:20 +0200292 return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
Aaron Plattner89177642013-01-15 20:47:42 +0000293}
294EXPORT_SYMBOL(drm_gem_prime_export);
295
Dave Airlie32488772011-11-25 15:21:02 +0000296int drm_gem_prime_handle_to_fd(struct drm_device *dev,
297 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
298 int *prime_fd)
299{
300 struct drm_gem_object *obj;
301 void *buf;
Dave Airlie219b4732013-04-22 09:54:36 +1000302 int ret = 0;
303 struct dma_buf *dmabuf;
Dave Airlie32488772011-11-25 15:21:02 +0000304
305 obj = drm_gem_object_lookup(dev, file_priv, handle);
306 if (!obj)
307 return -ENOENT;
308
309 mutex_lock(&file_priv->prime.lock);
310 /* re-export the original imported object */
311 if (obj->import_attach) {
Dave Airlie219b4732013-04-22 09:54:36 +1000312 dmabuf = obj->import_attach->dmabuf;
313 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000314 }
315
316 if (obj->export_dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000317 dmabuf = obj->export_dma_buf;
318 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000319 }
Dave Airlie219b4732013-04-22 09:54:36 +1000320
321 buf = dev->driver->gem_prime_export(dev, obj, flags);
322 if (IS_ERR(buf)) {
323 /* normally the created dma-buf takes ownership of the ref,
324 * but if that fails then drop the ref
325 */
326 ret = PTR_ERR(buf);
327 goto out;
328 }
329 obj->export_dma_buf = buf;
330
Dave Airlie0ff926c2012-05-20 17:31:16 +0100331 /* if we've exported this buffer the cheat and add it to the import list
332 * so we get the correct handle back
333 */
Dave Airlie219b4732013-04-22 09:54:36 +1000334 ret = drm_prime_add_buf_handle(&file_priv->prime,
335 obj->export_dma_buf, handle);
336 if (ret)
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900337 goto fail_put_dmabuf;
Dave Airlie0ff926c2012-05-20 17:31:16 +0100338
YoungJun Choda342422013-06-26 10:21:42 +0900339 ret = dma_buf_fd(buf, flags);
340 if (ret < 0)
341 goto fail_rm_handle;
342
343 *prime_fd = ret;
Dave Airlie32488772011-11-25 15:21:02 +0000344 mutex_unlock(&file_priv->prime.lock);
345 return 0;
Dave Airlie219b4732013-04-22 09:54:36 +1000346
347out_have_obj:
348 get_dma_buf(dmabuf);
YoungJun Choda342422013-06-26 10:21:42 +0900349 ret = dma_buf_fd(dmabuf, flags);
Daniel Vetter4a88f732013-07-02 09:18:39 +0200350 if (ret < 0) {
YoungJun Choda342422013-06-26 10:21:42 +0900351 dma_buf_put(dmabuf);
Daniel Vetter4a88f732013-07-02 09:18:39 +0200352 } else {
YoungJun Choda342422013-06-26 10:21:42 +0900353 *prime_fd = ret;
Daniel Vetter4a88f732013-07-02 09:18:39 +0200354 ret = 0;
355 }
356
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900357 goto out;
358
YoungJun Choda342422013-06-26 10:21:42 +0900359fail_rm_handle:
360 drm_prime_remove_buf_handle_locked(&file_priv->prime, buf);
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900361fail_put_dmabuf:
362 /* clear NOT to be checked when releasing dma_buf */
363 obj->export_dma_buf = NULL;
364 dma_buf_put(buf);
Dave Airlie219b4732013-04-22 09:54:36 +1000365out:
366 drm_gem_object_unreference_unlocked(obj);
367 mutex_unlock(&file_priv->prime.lock);
368 return ret;
Dave Airlie32488772011-11-25 15:21:02 +0000369}
370EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
371
Aaron Plattner89177642013-01-15 20:47:42 +0000372struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
373 struct dma_buf *dma_buf)
374{
375 struct dma_buf_attachment *attach;
376 struct sg_table *sgt;
377 struct drm_gem_object *obj;
378 int ret;
379
380 if (!dev->driver->gem_prime_import_sg_table)
381 return ERR_PTR(-EINVAL);
382
383 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
384 obj = dma_buf->priv;
385 if (obj->dev == dev) {
386 /*
387 * Importing dmabuf exported from out own gem increases
388 * refcount on gem itself instead of f_count of dmabuf.
389 */
390 drm_gem_object_reference(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000391 return obj;
392 }
393 }
394
395 attach = dma_buf_attach(dma_buf, dev->dev);
396 if (IS_ERR(attach))
Thomas Meyerf2a5da42013-06-01 10:09:27 +0000397 return ERR_CAST(attach);
Aaron Plattner89177642013-01-15 20:47:42 +0000398
Imre Deak011c22822013-04-19 11:11:56 +1000399 get_dma_buf(dma_buf);
400
Aaron Plattner89177642013-01-15 20:47:42 +0000401 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
402 if (IS_ERR_OR_NULL(sgt)) {
403 ret = PTR_ERR(sgt);
404 goto fail_detach;
405 }
406
407 obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
408 if (IS_ERR(obj)) {
409 ret = PTR_ERR(obj);
410 goto fail_unmap;
411 }
412
413 obj->import_attach = attach;
414
415 return obj;
416
417fail_unmap:
418 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
419fail_detach:
420 dma_buf_detach(dma_buf, attach);
Imre Deak011c22822013-04-19 11:11:56 +1000421 dma_buf_put(dma_buf);
422
Aaron Plattner89177642013-01-15 20:47:42 +0000423 return ERR_PTR(ret);
424}
425EXPORT_SYMBOL(drm_gem_prime_import);
426
Dave Airlie32488772011-11-25 15:21:02 +0000427int drm_gem_prime_fd_to_handle(struct drm_device *dev,
428 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
429{
430 struct dma_buf *dma_buf;
431 struct drm_gem_object *obj;
432 int ret;
433
434 dma_buf = dma_buf_get(prime_fd);
435 if (IS_ERR(dma_buf))
436 return PTR_ERR(dma_buf);
437
438 mutex_lock(&file_priv->prime.lock);
439
Dave Airlie219b4732013-04-22 09:54:36 +1000440 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000441 dma_buf, handle);
442 if (!ret) {
443 ret = 0;
444 goto out_put;
445 }
446
447 /* never seen this one, need to import */
448 obj = dev->driver->gem_prime_import(dev, dma_buf);
449 if (IS_ERR(obj)) {
450 ret = PTR_ERR(obj);
451 goto out_put;
452 }
453
454 ret = drm_gem_handle_create(file_priv, obj, handle);
455 drm_gem_object_unreference_unlocked(obj);
456 if (ret)
457 goto out_put;
458
Dave Airlie219b4732013-04-22 09:54:36 +1000459 ret = drm_prime_add_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000460 dma_buf, *handle);
461 if (ret)
462 goto fail;
463
464 mutex_unlock(&file_priv->prime.lock);
Imre Deak011c22822013-04-19 11:11:56 +1000465
466 dma_buf_put(dma_buf);
467
Dave Airlie32488772011-11-25 15:21:02 +0000468 return 0;
469
470fail:
471 /* hmm, if driver attached, we are relying on the free-object path
472 * to detach.. which seems ok..
473 */
474 drm_gem_object_handle_unreference_unlocked(obj);
475out_put:
476 dma_buf_put(dma_buf);
477 mutex_unlock(&file_priv->prime.lock);
478 return ret;
479}
480EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
481
482int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
483 struct drm_file *file_priv)
484{
485 struct drm_prime_handle *args = data;
486 uint32_t flags;
487
488 if (!drm_core_check_feature(dev, DRIVER_PRIME))
489 return -EINVAL;
490
491 if (!dev->driver->prime_handle_to_fd)
492 return -ENOSYS;
493
494 /* check flags are valid */
495 if (args->flags & ~DRM_CLOEXEC)
496 return -EINVAL;
497
498 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
499 flags = args->flags & DRM_CLOEXEC;
500
501 return dev->driver->prime_handle_to_fd(dev, file_priv,
502 args->handle, flags, &args->fd);
503}
504
505int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
506 struct drm_file *file_priv)
507{
508 struct drm_prime_handle *args = data;
509
510 if (!drm_core_check_feature(dev, DRIVER_PRIME))
511 return -EINVAL;
512
513 if (!dev->driver->prime_fd_to_handle)
514 return -ENOSYS;
515
516 return dev->driver->prime_fd_to_handle(dev, file_priv,
517 args->fd, &args->handle);
518}
519
520/*
521 * drm_prime_pages_to_sg
522 *
523 * this helper creates an sg table object from a set of pages
524 * the driver is responsible for mapping the pages into the
525 * importers address space
526 */
527struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
528{
529 struct sg_table *sg = NULL;
Dave Airlie32488772011-11-25 15:21:02 +0000530 int ret;
531
532 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900533 if (!sg) {
534 ret = -ENOMEM;
Dave Airlie32488772011-11-25 15:21:02 +0000535 goto out;
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900536 }
Dave Airlie32488772011-11-25 15:21:02 +0000537
Rahul Sharmadca25cb2013-01-28 08:38:48 -0500538 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
539 nr_pages << PAGE_SHIFT, GFP_KERNEL);
Dave Airlie32488772011-11-25 15:21:02 +0000540 if (ret)
541 goto out;
542
Dave Airlie32488772011-11-25 15:21:02 +0000543 return sg;
544out:
545 kfree(sg);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900546 return ERR_PTR(ret);
Dave Airlie32488772011-11-25 15:21:02 +0000547}
548EXPORT_SYMBOL(drm_prime_pages_to_sg);
549
Dave Airlie51ab7ba2012-05-18 15:40:33 +0100550/* export an sg table into an array of pages and addresses
551 this is currently required by the TTM driver in order to do correct fault
552 handling */
553int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
554 dma_addr_t *addrs, int max_pages)
555{
556 unsigned count;
557 struct scatterlist *sg;
558 struct page *page;
559 u32 len, offset;
560 int pg_index;
561 dma_addr_t addr;
562
563 pg_index = 0;
564 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
565 len = sg->length;
566 offset = sg->offset;
567 page = sg_page(sg);
568 addr = sg_dma_address(sg);
569
570 while (len > 0) {
571 if (WARN_ON(pg_index >= max_pages))
572 return -1;
573 pages[pg_index] = page;
574 if (addrs)
575 addrs[pg_index] = addr;
576
577 page++;
578 addr += PAGE_SIZE;
579 len -= PAGE_SIZE;
580 pg_index++;
581 }
582 }
583 return 0;
584}
585EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
Dave Airlie32488772011-11-25 15:21:02 +0000586/* helper function to cleanup a GEM/prime object */
587void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
588{
589 struct dma_buf_attachment *attach;
590 struct dma_buf *dma_buf;
591 attach = obj->import_attach;
592 if (sg)
593 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
594 dma_buf = attach->dmabuf;
595 dma_buf_detach(attach->dmabuf, attach);
596 /* remove the reference */
597 dma_buf_put(dma_buf);
598}
599EXPORT_SYMBOL(drm_prime_gem_destroy);
600
601void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
602{
603 INIT_LIST_HEAD(&prime_fpriv->head);
604 mutex_init(&prime_fpriv->lock);
605}
606EXPORT_SYMBOL(drm_prime_init_file_private);
607
608void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
609{
Imre Deak98b76232013-04-24 19:04:57 +0300610 /* by now drm_gem_release should've made sure the list is empty */
611 WARN_ON(!list_empty(&prime_fpriv->head));
Dave Airlie32488772011-11-25 15:21:02 +0000612}
613EXPORT_SYMBOL(drm_prime_destroy_file_private);
614
Dave Airlie219b4732013-04-22 09:54:36 +1000615int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
Dave Airlie32488772011-11-25 15:21:02 +0000616{
617 struct drm_prime_member *member;
618
619 list_for_each_entry(member, &prime_fpriv->head, entry) {
620 if (member->dma_buf == dma_buf) {
621 *handle = member->handle;
622 return 0;
623 }
624 }
625 return -ENOENT;
626}
Dave Airlie219b4732013-04-22 09:54:36 +1000627EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
Dave Airlie32488772011-11-25 15:21:02 +0000628
Dave Airlie219b4732013-04-22 09:54:36 +1000629void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
Dave Airlie32488772011-11-25 15:21:02 +0000630{
Dave Airlie32488772011-11-25 15:21:02 +0000631 mutex_lock(&prime_fpriv->lock);
YoungJun Choda342422013-06-26 10:21:42 +0900632 drm_prime_remove_buf_handle_locked(prime_fpriv, dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000633 mutex_unlock(&prime_fpriv->lock);
634}
Dave Airlie219b4732013-04-22 09:54:36 +1000635EXPORT_SYMBOL(drm_prime_remove_buf_handle);