blob: c2d6d54e10e0ef486280ed584438fa2d786f14cf [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;
Joonyoung Shimf9d8a122013-07-04 16:19:12 +0900122 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 }
Joonyoung Shim538d6662013-06-19 15:03:05 +0900128
Joonyoung Shim538d6662013-06-19 15:03:05 +0900129 kfree(sgt);
130 kfree(prime_attach);
131 attach->priv = NULL;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200132}
133
YoungJun Choda342422013-06-26 10:21:42 +0900134static 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
Aaron Plattner89177642013-01-15 20:47:42 +0000149static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
150 enum dma_data_direction dir)
151{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900152 struct drm_prime_attachment *prime_attach = attach->priv;
Aaron Plattner89177642013-01-15 20:47:42 +0000153 struct drm_gem_object *obj = attach->dmabuf->priv;
154 struct sg_table *sgt;
155
Joonyoung Shim538d6662013-06-19 15:03:05 +0900156 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
Aaron Plattner89177642013-01-15 20:47:42 +0000170 sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
171
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900172 if (!IS_ERR(sgt)) {
YoungJun Chob720d542013-06-24 15:34:21 +0900173 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
174 sg_free_table(sgt);
175 kfree(sgt);
176 sgt = ERR_PTR(-ENOMEM);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900177 } else {
178 prime_attach->sgt = sgt;
179 prime_attach->dir = dir;
YoungJun Chob720d542013-06-24 15:34:21 +0900180 }
181 }
Aaron Plattner89177642013-01-15 20:47:42 +0000182
Aaron Plattner89177642013-01-15 20:47:42 +0000183 return sgt;
184}
185
186static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
187 struct sg_table *sgt, enum dma_data_direction dir)
188{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900189 /* nothing to be done here */
Aaron Plattner89177642013-01-15 20:47:42 +0000190}
191
Daniel Vetterc1d67982013-08-15 00:02:30 +0200192void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
Aaron Plattner89177642013-01-15 20:47:42 +0000193{
194 struct drm_gem_object *obj = dma_buf->priv;
195
196 if (obj->export_dma_buf == dma_buf) {
197 /* drop the reference on the export fd holds */
198 obj->export_dma_buf = NULL;
199 drm_gem_object_unreference_unlocked(obj);
200 }
201}
Daniel Vetterc1d67982013-08-15 00:02:30 +0200202EXPORT_SYMBOL(drm_gem_dmabuf_release);
Aaron Plattner89177642013-01-15 20:47:42 +0000203
204static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
205{
206 struct drm_gem_object *obj = dma_buf->priv;
207 struct drm_device *dev = obj->dev;
208
209 return dev->driver->gem_prime_vmap(obj);
210}
211
212static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
213{
214 struct drm_gem_object *obj = dma_buf->priv;
215 struct drm_device *dev = obj->dev;
216
217 dev->driver->gem_prime_vunmap(obj, vaddr);
218}
219
220static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
221 unsigned long page_num)
222{
223 return NULL;
224}
225
226static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
227 unsigned long page_num, void *addr)
228{
229
230}
231static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
232 unsigned long page_num)
233{
234 return NULL;
235}
236
237static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
238 unsigned long page_num, void *addr)
239{
240
241}
242
243static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
244 struct vm_area_struct *vma)
245{
Joonyoung Shim7c397cd2013-06-28 14:24:53 +0900246 struct drm_gem_object *obj = dma_buf->priv;
247 struct drm_device *dev = obj->dev;
248
249 if (!dev->driver->gem_prime_mmap)
250 return -ENOSYS;
251
252 return dev->driver->gem_prime_mmap(obj, vma);
Aaron Plattner89177642013-01-15 20:47:42 +0000253}
254
255static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200256 .attach = drm_gem_map_attach,
257 .detach = drm_gem_map_detach,
Aaron Plattner89177642013-01-15 20:47:42 +0000258 .map_dma_buf = drm_gem_map_dma_buf,
259 .unmap_dma_buf = drm_gem_unmap_dma_buf,
260 .release = drm_gem_dmabuf_release,
261 .kmap = drm_gem_dmabuf_kmap,
262 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
263 .kunmap = drm_gem_dmabuf_kunmap,
264 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
265 .mmap = drm_gem_dmabuf_mmap,
266 .vmap = drm_gem_dmabuf_vmap,
267 .vunmap = drm_gem_dmabuf_vunmap,
268};
269
270/**
271 * DOC: PRIME Helpers
272 *
273 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
274 * simpler APIs by using the helper functions @drm_gem_prime_export and
275 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
276 * five lower-level driver callbacks:
277 *
278 * Export callbacks:
279 *
280 * - @gem_prime_pin (optional): prepare a GEM object for exporting
281 *
282 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
283 *
284 * - @gem_prime_vmap: vmap a buffer exported by your driver
285 *
286 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
287 *
288 * Import callback:
289 *
290 * - @gem_prime_import_sg_table (import): produce a GEM object from another
291 * driver's scatter/gather table
292 */
293
294struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
295 struct drm_gem_object *obj, int flags)
296{
Laurent Pinchartebc0bad2013-06-19 03:14:20 +0200297 return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
Aaron Plattner89177642013-01-15 20:47:42 +0000298}
299EXPORT_SYMBOL(drm_gem_prime_export);
300
Dave Airlie32488772011-11-25 15:21:02 +0000301int drm_gem_prime_handle_to_fd(struct drm_device *dev,
302 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
303 int *prime_fd)
304{
305 struct drm_gem_object *obj;
Dave Airlie219b4732013-04-22 09:54:36 +1000306 int ret = 0;
307 struct dma_buf *dmabuf;
Dave Airlie32488772011-11-25 15:21:02 +0000308
309 obj = drm_gem_object_lookup(dev, file_priv, handle);
310 if (!obj)
311 return -ENOENT;
312
313 mutex_lock(&file_priv->prime.lock);
314 /* re-export the original imported object */
315 if (obj->import_attach) {
Dave Airlie219b4732013-04-22 09:54:36 +1000316 dmabuf = obj->import_attach->dmabuf;
317 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000318 }
319
320 if (obj->export_dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000321 dmabuf = obj->export_dma_buf;
322 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000323 }
Dave Airlie219b4732013-04-22 09:54:36 +1000324
Daniel Vetter4332bf42013-08-15 00:02:41 +0200325 dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
326 if (IS_ERR(dmabuf)) {
Dave Airlie219b4732013-04-22 09:54:36 +1000327 /* normally the created dma-buf takes ownership of the ref,
328 * but if that fails then drop the ref
329 */
Daniel Vetter4332bf42013-08-15 00:02:41 +0200330 ret = PTR_ERR(dmabuf);
Dave Airlie219b4732013-04-22 09:54:36 +1000331 goto out;
332 }
Daniel Vetter4332bf42013-08-15 00:02:41 +0200333 obj->export_dma_buf = dmabuf;
Dave Airlie219b4732013-04-22 09:54:36 +1000334
Dave Airlie0ff926c2012-05-20 17:31:16 +0100335 /* if we've exported this buffer the cheat and add it to the import list
336 * so we get the correct handle back
337 */
Dave Airlie219b4732013-04-22 09:54:36 +1000338 ret = drm_prime_add_buf_handle(&file_priv->prime,
339 obj->export_dma_buf, handle);
340 if (ret)
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900341 goto fail_put_dmabuf;
Dave Airlie0ff926c2012-05-20 17:31:16 +0100342
Daniel Vetter4332bf42013-08-15 00:02:41 +0200343 ret = dma_buf_fd(dmabuf, flags);
YoungJun Choda342422013-06-26 10:21:42 +0900344 if (ret < 0)
345 goto fail_rm_handle;
346
347 *prime_fd = ret;
Dave Airlie32488772011-11-25 15:21:02 +0000348 mutex_unlock(&file_priv->prime.lock);
349 return 0;
Dave Airlie219b4732013-04-22 09:54:36 +1000350
351out_have_obj:
352 get_dma_buf(dmabuf);
YoungJun Choda342422013-06-26 10:21:42 +0900353 ret = dma_buf_fd(dmabuf, flags);
Daniel Vetter4a88f732013-07-02 09:18:39 +0200354 if (ret < 0) {
YoungJun Choda342422013-06-26 10:21:42 +0900355 dma_buf_put(dmabuf);
Daniel Vetter4a88f732013-07-02 09:18:39 +0200356 } else {
YoungJun Choda342422013-06-26 10:21:42 +0900357 *prime_fd = ret;
Daniel Vetter4a88f732013-07-02 09:18:39 +0200358 ret = 0;
359 }
360
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900361 goto out;
362
YoungJun Choda342422013-06-26 10:21:42 +0900363fail_rm_handle:
Daniel Vetter4332bf42013-08-15 00:02:41 +0200364 drm_prime_remove_buf_handle_locked(&file_priv->prime,
365 dmabuf);
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900366fail_put_dmabuf:
367 /* clear NOT to be checked when releasing dma_buf */
368 obj->export_dma_buf = NULL;
Daniel Vetter4332bf42013-08-15 00:02:41 +0200369 dma_buf_put(dmabuf);
Dave Airlie219b4732013-04-22 09:54:36 +1000370out:
371 drm_gem_object_unreference_unlocked(obj);
372 mutex_unlock(&file_priv->prime.lock);
373 return ret;
Dave Airlie32488772011-11-25 15:21:02 +0000374}
375EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
376
Aaron Plattner89177642013-01-15 20:47:42 +0000377struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
378 struct dma_buf *dma_buf)
379{
380 struct dma_buf_attachment *attach;
381 struct sg_table *sgt;
382 struct drm_gem_object *obj;
383 int ret;
384
385 if (!dev->driver->gem_prime_import_sg_table)
386 return ERR_PTR(-EINVAL);
387
388 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
389 obj = dma_buf->priv;
390 if (obj->dev == dev) {
391 /*
392 * Importing dmabuf exported from out own gem increases
393 * refcount on gem itself instead of f_count of dmabuf.
394 */
395 drm_gem_object_reference(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000396 return obj;
397 }
398 }
399
400 attach = dma_buf_attach(dma_buf, dev->dev);
401 if (IS_ERR(attach))
Thomas Meyerf2a5da42013-06-01 10:09:27 +0000402 return ERR_CAST(attach);
Aaron Plattner89177642013-01-15 20:47:42 +0000403
Imre Deak011c2282013-04-19 11:11:56 +1000404 get_dma_buf(dma_buf);
405
Aaron Plattner89177642013-01-15 20:47:42 +0000406 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
407 if (IS_ERR_OR_NULL(sgt)) {
408 ret = PTR_ERR(sgt);
409 goto fail_detach;
410 }
411
412 obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
413 if (IS_ERR(obj)) {
414 ret = PTR_ERR(obj);
415 goto fail_unmap;
416 }
417
418 obj->import_attach = attach;
419
420 return obj;
421
422fail_unmap:
423 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
424fail_detach:
425 dma_buf_detach(dma_buf, attach);
Imre Deak011c2282013-04-19 11:11:56 +1000426 dma_buf_put(dma_buf);
427
Aaron Plattner89177642013-01-15 20:47:42 +0000428 return ERR_PTR(ret);
429}
430EXPORT_SYMBOL(drm_gem_prime_import);
431
Dave Airlie32488772011-11-25 15:21:02 +0000432int drm_gem_prime_fd_to_handle(struct drm_device *dev,
433 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
434{
435 struct dma_buf *dma_buf;
436 struct drm_gem_object *obj;
437 int ret;
438
439 dma_buf = dma_buf_get(prime_fd);
440 if (IS_ERR(dma_buf))
441 return PTR_ERR(dma_buf);
442
443 mutex_lock(&file_priv->prime.lock);
444
Dave Airlie219b4732013-04-22 09:54:36 +1000445 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000446 dma_buf, handle);
447 if (!ret) {
448 ret = 0;
449 goto out_put;
450 }
451
452 /* never seen this one, need to import */
453 obj = dev->driver->gem_prime_import(dev, dma_buf);
454 if (IS_ERR(obj)) {
455 ret = PTR_ERR(obj);
456 goto out_put;
457 }
458
459 ret = drm_gem_handle_create(file_priv, obj, handle);
460 drm_gem_object_unreference_unlocked(obj);
461 if (ret)
462 goto out_put;
463
Dave Airlie219b4732013-04-22 09:54:36 +1000464 ret = drm_prime_add_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000465 dma_buf, *handle);
466 if (ret)
467 goto fail;
468
469 mutex_unlock(&file_priv->prime.lock);
Imre Deak011c2282013-04-19 11:11:56 +1000470
471 dma_buf_put(dma_buf);
472
Dave Airlie32488772011-11-25 15:21:02 +0000473 return 0;
474
475fail:
476 /* hmm, if driver attached, we are relying on the free-object path
477 * to detach.. which seems ok..
478 */
Daniel Vetter730c4ff2013-08-15 00:02:38 +0200479 drm_gem_handle_delete(file_priv, *handle);
Dave Airlie32488772011-11-25 15:21:02 +0000480out_put:
481 dma_buf_put(dma_buf);
482 mutex_unlock(&file_priv->prime.lock);
483 return ret;
484}
485EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
486
487int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
488 struct drm_file *file_priv)
489{
490 struct drm_prime_handle *args = data;
491 uint32_t flags;
492
493 if (!drm_core_check_feature(dev, DRIVER_PRIME))
494 return -EINVAL;
495
496 if (!dev->driver->prime_handle_to_fd)
497 return -ENOSYS;
498
499 /* check flags are valid */
500 if (args->flags & ~DRM_CLOEXEC)
501 return -EINVAL;
502
503 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
504 flags = args->flags & DRM_CLOEXEC;
505
506 return dev->driver->prime_handle_to_fd(dev, file_priv,
507 args->handle, flags, &args->fd);
508}
509
510int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
511 struct drm_file *file_priv)
512{
513 struct drm_prime_handle *args = data;
514
515 if (!drm_core_check_feature(dev, DRIVER_PRIME))
516 return -EINVAL;
517
518 if (!dev->driver->prime_fd_to_handle)
519 return -ENOSYS;
520
521 return dev->driver->prime_fd_to_handle(dev, file_priv,
522 args->fd, &args->handle);
523}
524
525/*
526 * drm_prime_pages_to_sg
527 *
528 * this helper creates an sg table object from a set of pages
529 * the driver is responsible for mapping the pages into the
530 * importers address space
531 */
532struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
533{
534 struct sg_table *sg = NULL;
Dave Airlie32488772011-11-25 15:21:02 +0000535 int ret;
536
537 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900538 if (!sg) {
539 ret = -ENOMEM;
Dave Airlie32488772011-11-25 15:21:02 +0000540 goto out;
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900541 }
Dave Airlie32488772011-11-25 15:21:02 +0000542
Rahul Sharmadca25cb2013-01-28 08:38:48 -0500543 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
544 nr_pages << PAGE_SHIFT, GFP_KERNEL);
Dave Airlie32488772011-11-25 15:21:02 +0000545 if (ret)
546 goto out;
547
Dave Airlie32488772011-11-25 15:21:02 +0000548 return sg;
549out:
550 kfree(sg);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900551 return ERR_PTR(ret);
Dave Airlie32488772011-11-25 15:21:02 +0000552}
553EXPORT_SYMBOL(drm_prime_pages_to_sg);
554
Dave Airlie51ab7ba2012-05-18 15:40:33 +0100555/* export an sg table into an array of pages and addresses
556 this is currently required by the TTM driver in order to do correct fault
557 handling */
558int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
559 dma_addr_t *addrs, int max_pages)
560{
561 unsigned count;
562 struct scatterlist *sg;
563 struct page *page;
564 u32 len, offset;
565 int pg_index;
566 dma_addr_t addr;
567
568 pg_index = 0;
569 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
570 len = sg->length;
571 offset = sg->offset;
572 page = sg_page(sg);
573 addr = sg_dma_address(sg);
574
575 while (len > 0) {
576 if (WARN_ON(pg_index >= max_pages))
577 return -1;
578 pages[pg_index] = page;
579 if (addrs)
580 addrs[pg_index] = addr;
581
582 page++;
583 addr += PAGE_SIZE;
584 len -= PAGE_SIZE;
585 pg_index++;
586 }
587 }
588 return 0;
589}
590EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
Dave Airlie32488772011-11-25 15:21:02 +0000591/* helper function to cleanup a GEM/prime object */
592void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
593{
594 struct dma_buf_attachment *attach;
595 struct dma_buf *dma_buf;
596 attach = obj->import_attach;
597 if (sg)
598 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
599 dma_buf = attach->dmabuf;
600 dma_buf_detach(attach->dmabuf, attach);
601 /* remove the reference */
602 dma_buf_put(dma_buf);
603}
604EXPORT_SYMBOL(drm_prime_gem_destroy);
605
606void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
607{
608 INIT_LIST_HEAD(&prime_fpriv->head);
609 mutex_init(&prime_fpriv->lock);
610}
611EXPORT_SYMBOL(drm_prime_init_file_private);
612
613void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
614{
Imre Deak98b76232013-04-24 19:04:57 +0300615 /* by now drm_gem_release should've made sure the list is empty */
616 WARN_ON(!list_empty(&prime_fpriv->head));
Dave Airlie32488772011-11-25 15:21:02 +0000617}
618EXPORT_SYMBOL(drm_prime_destroy_file_private);
619
Dave Airlie219b4732013-04-22 09:54:36 +1000620int 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 +0000621{
622 struct drm_prime_member *member;
623
624 list_for_each_entry(member, &prime_fpriv->head, entry) {
625 if (member->dma_buf == dma_buf) {
626 *handle = member->handle;
627 return 0;
628 }
629 }
630 return -ENOENT;
631}
Dave Airlie219b4732013-04-22 09:54:36 +1000632EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
Dave Airlie32488772011-11-25 15:21:02 +0000633
Dave Airlie219b4732013-04-22 09:54:36 +1000634void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
Dave Airlie32488772011-11-25 15:21:02 +0000635{
Dave Airlie32488772011-11-25 15:21:02 +0000636 mutex_lock(&prime_fpriv->lock);
YoungJun Choda342422013-06-26 10:21:42 +0900637 drm_prime_remove_buf_handle_locked(prime_fpriv, dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000638 mutex_unlock(&prime_fpriv->lock);
639}
Dave Airlie219b4732013-04-22 09:54:36 +1000640EXPORT_SYMBOL(drm_prime_remove_buf_handle);