blob: ed1ea5c1a9cab5ec491bf1473b109ee20ac15190 [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
Daniel Vetterde9564d2013-08-15 00:02:48 +020086static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
87 struct dma_buf *dma_buf,
88 uint32_t *handle)
89{
90 struct drm_prime_member *member;
91
92 list_for_each_entry(member, &prime_fpriv->head, entry) {
93 if (member->dma_buf == dma_buf) {
94 *handle = member->handle;
95 return 0;
96 }
97 }
98 return -ENOENT;
99}
100
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200101static int drm_gem_map_attach(struct dma_buf *dma_buf,
102 struct device *target_dev,
103 struct dma_buf_attachment *attach)
104{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900105 struct drm_prime_attachment *prime_attach;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200106 struct drm_gem_object *obj = dma_buf->priv;
107 struct drm_device *dev = obj->dev;
108
Joonyoung Shim538d6662013-06-19 15:03:05 +0900109 prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
110 if (!prime_attach)
111 return -ENOMEM;
112
113 prime_attach->dir = DMA_NONE;
114 attach->priv = prime_attach;
115
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200116 if (!dev->driver->gem_prime_pin)
117 return 0;
118
119 return dev->driver->gem_prime_pin(obj);
120}
121
122static void drm_gem_map_detach(struct dma_buf *dma_buf,
123 struct dma_buf_attachment *attach)
124{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900125 struct drm_prime_attachment *prime_attach = attach->priv;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200126 struct drm_gem_object *obj = dma_buf->priv;
127 struct drm_device *dev = obj->dev;
Joonyoung Shim538d6662013-06-19 15:03:05 +0900128 struct sg_table *sgt;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200129
130 if (dev->driver->gem_prime_unpin)
131 dev->driver->gem_prime_unpin(obj);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900132
133 if (!prime_attach)
134 return;
135
136 sgt = prime_attach->sgt;
Joonyoung Shimf9d8a122013-07-04 16:19:12 +0900137 if (sgt) {
138 if (prime_attach->dir != DMA_NONE)
139 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
140 prime_attach->dir);
141 sg_free_table(sgt);
142 }
Joonyoung Shim538d6662013-06-19 15:03:05 +0900143
Joonyoung Shim538d6662013-06-19 15:03:05 +0900144 kfree(sgt);
145 kfree(prime_attach);
146 attach->priv = NULL;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200147}
148
YoungJun Choda342422013-06-26 10:21:42 +0900149static void drm_prime_remove_buf_handle_locked(
150 struct drm_prime_file_private *prime_fpriv,
151 struct dma_buf *dma_buf)
152{
153 struct drm_prime_member *member, *safe;
154
155 list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
156 if (member->dma_buf == dma_buf) {
157 dma_buf_put(dma_buf);
158 list_del(&member->entry);
159 kfree(member);
160 }
161 }
162}
163
Aaron Plattner89177642013-01-15 20:47:42 +0000164static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
165 enum dma_data_direction dir)
166{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900167 struct drm_prime_attachment *prime_attach = attach->priv;
Aaron Plattner89177642013-01-15 20:47:42 +0000168 struct drm_gem_object *obj = attach->dmabuf->priv;
169 struct sg_table *sgt;
170
Joonyoung Shim538d6662013-06-19 15:03:05 +0900171 if (WARN_ON(dir == DMA_NONE || !prime_attach))
172 return ERR_PTR(-EINVAL);
173
174 /* return the cached mapping when possible */
175 if (prime_attach->dir == dir)
176 return prime_attach->sgt;
177
178 /*
179 * two mappings with different directions for the same attachment are
180 * not allowed
181 */
182 if (WARN_ON(prime_attach->dir != DMA_NONE))
183 return ERR_PTR(-EBUSY);
184
Aaron Plattner89177642013-01-15 20:47:42 +0000185 sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
186
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900187 if (!IS_ERR(sgt)) {
YoungJun Chob720d542013-06-24 15:34:21 +0900188 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
189 sg_free_table(sgt);
190 kfree(sgt);
191 sgt = ERR_PTR(-ENOMEM);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900192 } else {
193 prime_attach->sgt = sgt;
194 prime_attach->dir = dir;
YoungJun Chob720d542013-06-24 15:34:21 +0900195 }
196 }
Aaron Plattner89177642013-01-15 20:47:42 +0000197
Aaron Plattner89177642013-01-15 20:47:42 +0000198 return sgt;
199}
200
201static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
202 struct sg_table *sgt, enum dma_data_direction dir)
203{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900204 /* nothing to be done here */
Aaron Plattner89177642013-01-15 20:47:42 +0000205}
206
Daniel Vetterc1d67982013-08-15 00:02:30 +0200207void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
Aaron Plattner89177642013-01-15 20:47:42 +0000208{
209 struct drm_gem_object *obj = dma_buf->priv;
210
Daniel Vetter319c9332013-08-15 00:02:46 +0200211 /* drop the reference on the export fd holds */
212 drm_gem_object_unreference_unlocked(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000213}
Daniel Vetterc1d67982013-08-15 00:02:30 +0200214EXPORT_SYMBOL(drm_gem_dmabuf_release);
Aaron Plattner89177642013-01-15 20:47:42 +0000215
216static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
217{
218 struct drm_gem_object *obj = dma_buf->priv;
219 struct drm_device *dev = obj->dev;
220
221 return dev->driver->gem_prime_vmap(obj);
222}
223
224static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
225{
226 struct drm_gem_object *obj = dma_buf->priv;
227 struct drm_device *dev = obj->dev;
228
229 dev->driver->gem_prime_vunmap(obj, vaddr);
230}
231
232static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
233 unsigned long page_num)
234{
235 return NULL;
236}
237
238static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
239 unsigned long page_num, void *addr)
240{
241
242}
243static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
244 unsigned long page_num)
245{
246 return NULL;
247}
248
249static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
250 unsigned long page_num, void *addr)
251{
252
253}
254
255static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
256 struct vm_area_struct *vma)
257{
Joonyoung Shim7c397cd2013-06-28 14:24:53 +0900258 struct drm_gem_object *obj = dma_buf->priv;
259 struct drm_device *dev = obj->dev;
260
261 if (!dev->driver->gem_prime_mmap)
262 return -ENOSYS;
263
264 return dev->driver->gem_prime_mmap(obj, vma);
Aaron Plattner89177642013-01-15 20:47:42 +0000265}
266
267static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200268 .attach = drm_gem_map_attach,
269 .detach = drm_gem_map_detach,
Aaron Plattner89177642013-01-15 20:47:42 +0000270 .map_dma_buf = drm_gem_map_dma_buf,
271 .unmap_dma_buf = drm_gem_unmap_dma_buf,
272 .release = drm_gem_dmabuf_release,
273 .kmap = drm_gem_dmabuf_kmap,
274 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
275 .kunmap = drm_gem_dmabuf_kunmap,
276 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
277 .mmap = drm_gem_dmabuf_mmap,
278 .vmap = drm_gem_dmabuf_vmap,
279 .vunmap = drm_gem_dmabuf_vunmap,
280};
281
282/**
283 * DOC: PRIME Helpers
284 *
285 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
286 * simpler APIs by using the helper functions @drm_gem_prime_export and
287 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
288 * five lower-level driver callbacks:
289 *
290 * Export callbacks:
291 *
292 * - @gem_prime_pin (optional): prepare a GEM object for exporting
293 *
294 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
295 *
296 * - @gem_prime_vmap: vmap a buffer exported by your driver
297 *
298 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
299 *
300 * Import callback:
301 *
302 * - @gem_prime_import_sg_table (import): produce a GEM object from another
303 * driver's scatter/gather table
304 */
305
306struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
307 struct drm_gem_object *obj, int flags)
308{
Laurent Pinchartebc0bad2013-06-19 03:14:20 +0200309 return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
Aaron Plattner89177642013-01-15 20:47:42 +0000310}
311EXPORT_SYMBOL(drm_gem_prime_export);
312
Daniel Vetter319c9332013-08-15 00:02:46 +0200313static struct dma_buf *export_and_register_object(struct drm_device *dev,
314 struct drm_gem_object *obj,
315 uint32_t flags)
316{
317 struct dma_buf *dmabuf;
318
319 /* prevent races with concurrent gem_close. */
320 if (obj->handle_count == 0) {
321 dmabuf = ERR_PTR(-ENOENT);
322 return dmabuf;
323 }
324
325 dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
326 if (IS_ERR(dmabuf)) {
327 /* normally the created dma-buf takes ownership of the ref,
328 * but if that fails then drop the ref
329 */
330 return dmabuf;
331 }
332
333 /*
334 * Note that callers do not need to clean up the export cache
335 * since the check for obj->handle_count guarantees that someone
336 * will clean it up.
337 */
338 obj->dma_buf = dmabuf;
339 get_dma_buf(obj->dma_buf);
340
341 return dmabuf;
342}
343
Dave Airlie32488772011-11-25 15:21:02 +0000344int drm_gem_prime_handle_to_fd(struct drm_device *dev,
345 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
346 int *prime_fd)
347{
348 struct drm_gem_object *obj;
Dave Airlie219b4732013-04-22 09:54:36 +1000349 int ret = 0;
350 struct dma_buf *dmabuf;
Dave Airlie32488772011-11-25 15:21:02 +0000351
352 obj = drm_gem_object_lookup(dev, file_priv, handle);
353 if (!obj)
354 return -ENOENT;
355
Dave Airlie32488772011-11-25 15:21:02 +0000356 /* re-export the original imported object */
357 if (obj->import_attach) {
Dave Airlie219b4732013-04-22 09:54:36 +1000358 dmabuf = obj->import_attach->dmabuf;
Daniel Vetter319c9332013-08-15 00:02:46 +0200359 get_dma_buf(dmabuf);
Dave Airlie219b4732013-04-22 09:54:36 +1000360 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000361 }
362
Daniel Vetter319c9332013-08-15 00:02:46 +0200363 mutex_lock(&dev->object_name_lock);
364 if (obj->dma_buf) {
365 get_dma_buf(obj->dma_buf);
366 dmabuf = obj->dma_buf;
367 mutex_unlock(&dev->object_name_lock);
Dave Airlie219b4732013-04-22 09:54:36 +1000368 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000369 }
Dave Airlie219b4732013-04-22 09:54:36 +1000370
Daniel Vetter319c9332013-08-15 00:02:46 +0200371 dmabuf = export_and_register_object(dev, obj, flags);
372 mutex_unlock(&dev->object_name_lock);
Daniel Vetter4332bf42013-08-15 00:02:41 +0200373 if (IS_ERR(dmabuf)) {
Dave Airlie219b4732013-04-22 09:54:36 +1000374 /* normally the created dma-buf takes ownership of the ref,
375 * but if that fails then drop the ref
376 */
Daniel Vetter4332bf42013-08-15 00:02:41 +0200377 ret = PTR_ERR(dmabuf);
Dave Airlie219b4732013-04-22 09:54:36 +1000378 goto out;
379 }
Dave Airlie219b4732013-04-22 09:54:36 +1000380
Daniel Vetterbdf655d2013-08-15 00:02:42 +0200381 mutex_lock(&file_priv->prime.lock);
Dave Airlie0ff926c2012-05-20 17:31:16 +0100382 /* if we've exported this buffer the cheat and add it to the import list
383 * so we get the correct handle back
384 */
Dave Airlie219b4732013-04-22 09:54:36 +1000385 ret = drm_prime_add_buf_handle(&file_priv->prime,
Daniel Vetter319c9332013-08-15 00:02:46 +0200386 dmabuf, handle);
Dave Airlie219b4732013-04-22 09:54:36 +1000387 if (ret)
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900388 goto fail_put_dmabuf;
Dave Airlie0ff926c2012-05-20 17:31:16 +0100389
Daniel Vetter4332bf42013-08-15 00:02:41 +0200390 ret = dma_buf_fd(dmabuf, flags);
YoungJun Choda342422013-06-26 10:21:42 +0900391 if (ret < 0)
392 goto fail_rm_handle;
393
394 *prime_fd = ret;
Dave Airlie32488772011-11-25 15:21:02 +0000395 mutex_unlock(&file_priv->prime.lock);
396 return 0;
Dave Airlie219b4732013-04-22 09:54:36 +1000397
398out_have_obj:
YoungJun Choda342422013-06-26 10:21:42 +0900399 ret = dma_buf_fd(dmabuf, flags);
Daniel Vetter4a88f732013-07-02 09:18:39 +0200400 if (ret < 0) {
YoungJun Choda342422013-06-26 10:21:42 +0900401 dma_buf_put(dmabuf);
Daniel Vetter4a88f732013-07-02 09:18:39 +0200402 } else {
YoungJun Choda342422013-06-26 10:21:42 +0900403 *prime_fd = ret;
Daniel Vetter4a88f732013-07-02 09:18:39 +0200404 ret = 0;
405 }
406
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900407 goto out;
408
YoungJun Choda342422013-06-26 10:21:42 +0900409fail_rm_handle:
Daniel Vetter4332bf42013-08-15 00:02:41 +0200410 drm_prime_remove_buf_handle_locked(&file_priv->prime,
411 dmabuf);
Daniel Vetterbdf655d2013-08-15 00:02:42 +0200412 mutex_unlock(&file_priv->prime.lock);
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900413fail_put_dmabuf:
Daniel Vetter4332bf42013-08-15 00:02:41 +0200414 dma_buf_put(dmabuf);
Dave Airlie219b4732013-04-22 09:54:36 +1000415out:
416 drm_gem_object_unreference_unlocked(obj);
Dave Airlie219b4732013-04-22 09:54:36 +1000417 return ret;
Dave Airlie32488772011-11-25 15:21:02 +0000418}
419EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
420
Aaron Plattner89177642013-01-15 20:47:42 +0000421struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
422 struct dma_buf *dma_buf)
423{
424 struct dma_buf_attachment *attach;
425 struct sg_table *sgt;
426 struct drm_gem_object *obj;
427 int ret;
428
429 if (!dev->driver->gem_prime_import_sg_table)
430 return ERR_PTR(-EINVAL);
431
432 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
433 obj = dma_buf->priv;
434 if (obj->dev == dev) {
435 /*
436 * Importing dmabuf exported from out own gem increases
437 * refcount on gem itself instead of f_count of dmabuf.
438 */
439 drm_gem_object_reference(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000440 return obj;
441 }
442 }
443
444 attach = dma_buf_attach(dma_buf, dev->dev);
445 if (IS_ERR(attach))
Thomas Meyerf2a5da42013-06-01 10:09:27 +0000446 return ERR_CAST(attach);
Aaron Plattner89177642013-01-15 20:47:42 +0000447
Imre Deak011c2282013-04-19 11:11:56 +1000448 get_dma_buf(dma_buf);
449
Aaron Plattner89177642013-01-15 20:47:42 +0000450 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
451 if (IS_ERR_OR_NULL(sgt)) {
452 ret = PTR_ERR(sgt);
453 goto fail_detach;
454 }
455
456 obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
457 if (IS_ERR(obj)) {
458 ret = PTR_ERR(obj);
459 goto fail_unmap;
460 }
461
462 obj->import_attach = attach;
463
464 return obj;
465
466fail_unmap:
467 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
468fail_detach:
469 dma_buf_detach(dma_buf, attach);
Imre Deak011c2282013-04-19 11:11:56 +1000470 dma_buf_put(dma_buf);
471
Aaron Plattner89177642013-01-15 20:47:42 +0000472 return ERR_PTR(ret);
473}
474EXPORT_SYMBOL(drm_gem_prime_import);
475
Dave Airlie32488772011-11-25 15:21:02 +0000476int drm_gem_prime_fd_to_handle(struct drm_device *dev,
477 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
478{
479 struct dma_buf *dma_buf;
480 struct drm_gem_object *obj;
481 int ret;
482
483 dma_buf = dma_buf_get(prime_fd);
484 if (IS_ERR(dma_buf))
485 return PTR_ERR(dma_buf);
486
487 mutex_lock(&file_priv->prime.lock);
488
Dave Airlie219b4732013-04-22 09:54:36 +1000489 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000490 dma_buf, handle);
Daniel Vetter84341c22013-08-15 00:02:43 +0200491 if (ret == 0)
Dave Airlie32488772011-11-25 15:21:02 +0000492 goto out_put;
Dave Airlie32488772011-11-25 15:21:02 +0000493
494 /* never seen this one, need to import */
Daniel Vetter319c9332013-08-15 00:02:46 +0200495 mutex_lock(&dev->object_name_lock);
Dave Airlie32488772011-11-25 15:21:02 +0000496 obj = dev->driver->gem_prime_import(dev, dma_buf);
497 if (IS_ERR(obj)) {
498 ret = PTR_ERR(obj);
Daniel Vetter319c9332013-08-15 00:02:46 +0200499 goto out_unlock;
Dave Airlie32488772011-11-25 15:21:02 +0000500 }
501
Daniel Vetter319c9332013-08-15 00:02:46 +0200502 if (obj->dma_buf) {
503 WARN_ON(obj->dma_buf != dma_buf);
504 } else {
505 obj->dma_buf = dma_buf;
506 get_dma_buf(dma_buf);
507 }
508
509 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
510 ret = drm_gem_handle_create_tail(file_priv, obj, handle);
Dave Airlie32488772011-11-25 15:21:02 +0000511 drm_gem_object_unreference_unlocked(obj);
512 if (ret)
513 goto out_put;
514
Dave Airlie219b4732013-04-22 09:54:36 +1000515 ret = drm_prime_add_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000516 dma_buf, *handle);
517 if (ret)
518 goto fail;
519
520 mutex_unlock(&file_priv->prime.lock);
Imre Deak011c2282013-04-19 11:11:56 +1000521
522 dma_buf_put(dma_buf);
523
Dave Airlie32488772011-11-25 15:21:02 +0000524 return 0;
525
526fail:
527 /* hmm, if driver attached, we are relying on the free-object path
528 * to detach.. which seems ok..
529 */
Daniel Vetter730c4ff2013-08-15 00:02:38 +0200530 drm_gem_handle_delete(file_priv, *handle);
Daniel Vetter319c9332013-08-15 00:02:46 +0200531out_unlock:
532 mutex_lock(&dev->object_name_lock);
Dave Airlie32488772011-11-25 15:21:02 +0000533out_put:
534 dma_buf_put(dma_buf);
535 mutex_unlock(&file_priv->prime.lock);
536 return ret;
537}
538EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
539
540int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
541 struct drm_file *file_priv)
542{
543 struct drm_prime_handle *args = data;
544 uint32_t flags;
545
546 if (!drm_core_check_feature(dev, DRIVER_PRIME))
547 return -EINVAL;
548
549 if (!dev->driver->prime_handle_to_fd)
550 return -ENOSYS;
551
552 /* check flags are valid */
553 if (args->flags & ~DRM_CLOEXEC)
554 return -EINVAL;
555
556 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
557 flags = args->flags & DRM_CLOEXEC;
558
559 return dev->driver->prime_handle_to_fd(dev, file_priv,
560 args->handle, flags, &args->fd);
561}
562
563int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
564 struct drm_file *file_priv)
565{
566 struct drm_prime_handle *args = data;
567
568 if (!drm_core_check_feature(dev, DRIVER_PRIME))
569 return -EINVAL;
570
571 if (!dev->driver->prime_fd_to_handle)
572 return -ENOSYS;
573
574 return dev->driver->prime_fd_to_handle(dev, file_priv,
575 args->fd, &args->handle);
576}
577
578/*
579 * drm_prime_pages_to_sg
580 *
581 * this helper creates an sg table object from a set of pages
582 * the driver is responsible for mapping the pages into the
583 * importers address space
584 */
585struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
586{
587 struct sg_table *sg = NULL;
Dave Airlie32488772011-11-25 15:21:02 +0000588 int ret;
589
590 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900591 if (!sg) {
592 ret = -ENOMEM;
Dave Airlie32488772011-11-25 15:21:02 +0000593 goto out;
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900594 }
Dave Airlie32488772011-11-25 15:21:02 +0000595
Rahul Sharmadca25cb2013-01-28 08:38:48 -0500596 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
597 nr_pages << PAGE_SHIFT, GFP_KERNEL);
Dave Airlie32488772011-11-25 15:21:02 +0000598 if (ret)
599 goto out;
600
Dave Airlie32488772011-11-25 15:21:02 +0000601 return sg;
602out:
603 kfree(sg);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900604 return ERR_PTR(ret);
Dave Airlie32488772011-11-25 15:21:02 +0000605}
606EXPORT_SYMBOL(drm_prime_pages_to_sg);
607
Dave Airlie51ab7ba2012-05-18 15:40:33 +0100608/* export an sg table into an array of pages and addresses
609 this is currently required by the TTM driver in order to do correct fault
610 handling */
611int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
612 dma_addr_t *addrs, int max_pages)
613{
614 unsigned count;
615 struct scatterlist *sg;
616 struct page *page;
617 u32 len, offset;
618 int pg_index;
619 dma_addr_t addr;
620
621 pg_index = 0;
622 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
623 len = sg->length;
624 offset = sg->offset;
625 page = sg_page(sg);
626 addr = sg_dma_address(sg);
627
628 while (len > 0) {
629 if (WARN_ON(pg_index >= max_pages))
630 return -1;
631 pages[pg_index] = page;
632 if (addrs)
633 addrs[pg_index] = addr;
634
635 page++;
636 addr += PAGE_SIZE;
637 len -= PAGE_SIZE;
638 pg_index++;
639 }
640 }
641 return 0;
642}
643EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
Dave Airlie32488772011-11-25 15:21:02 +0000644/* helper function to cleanup a GEM/prime object */
645void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
646{
647 struct dma_buf_attachment *attach;
648 struct dma_buf *dma_buf;
649 attach = obj->import_attach;
650 if (sg)
651 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
652 dma_buf = attach->dmabuf;
653 dma_buf_detach(attach->dmabuf, attach);
654 /* remove the reference */
655 dma_buf_put(dma_buf);
656}
657EXPORT_SYMBOL(drm_prime_gem_destroy);
658
659void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
660{
661 INIT_LIST_HEAD(&prime_fpriv->head);
662 mutex_init(&prime_fpriv->lock);
663}
664EXPORT_SYMBOL(drm_prime_init_file_private);
665
666void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
667{
Imre Deak98b76232013-04-24 19:04:57 +0300668 /* by now drm_gem_release should've made sure the list is empty */
669 WARN_ON(!list_empty(&prime_fpriv->head));
Dave Airlie32488772011-11-25 15:21:02 +0000670}
671EXPORT_SYMBOL(drm_prime_destroy_file_private);
672
Dave Airlie219b4732013-04-22 09:54:36 +1000673void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
Dave Airlie32488772011-11-25 15:21:02 +0000674{
Dave Airlie32488772011-11-25 15:21:02 +0000675 mutex_lock(&prime_fpriv->lock);
YoungJun Choda342422013-06-26 10:21:42 +0900676 drm_prime_remove_buf_handle_locked(prime_fpriv, dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000677 mutex_unlock(&prime_fpriv->lock);
678}
Dave Airlie219b4732013-04-22 09:54:36 +1000679EXPORT_SYMBOL(drm_prime_remove_buf_handle);