blob: 5e543e9264d72dbbde8d3d89a7d1b9cffe7551b3 [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
Daniel Vetter319c9332013-08-15 00:02:46 +0200196 /* drop the reference on the export fd holds */
197 drm_gem_object_unreference_unlocked(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000198}
Daniel Vetterc1d67982013-08-15 00:02:30 +0200199EXPORT_SYMBOL(drm_gem_dmabuf_release);
Aaron Plattner89177642013-01-15 20:47:42 +0000200
201static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
202{
203 struct drm_gem_object *obj = dma_buf->priv;
204 struct drm_device *dev = obj->dev;
205
206 return dev->driver->gem_prime_vmap(obj);
207}
208
209static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
210{
211 struct drm_gem_object *obj = dma_buf->priv;
212 struct drm_device *dev = obj->dev;
213
214 dev->driver->gem_prime_vunmap(obj, vaddr);
215}
216
217static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
218 unsigned long page_num)
219{
220 return NULL;
221}
222
223static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
224 unsigned long page_num, void *addr)
225{
226
227}
228static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
229 unsigned long page_num)
230{
231 return NULL;
232}
233
234static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
235 unsigned long page_num, void *addr)
236{
237
238}
239
240static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
241 struct vm_area_struct *vma)
242{
Joonyoung Shim7c397cd2013-06-28 14:24:53 +0900243 struct drm_gem_object *obj = dma_buf->priv;
244 struct drm_device *dev = obj->dev;
245
246 if (!dev->driver->gem_prime_mmap)
247 return -ENOSYS;
248
249 return dev->driver->gem_prime_mmap(obj, vma);
Aaron Plattner89177642013-01-15 20:47:42 +0000250}
251
252static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200253 .attach = drm_gem_map_attach,
254 .detach = drm_gem_map_detach,
Aaron Plattner89177642013-01-15 20:47:42 +0000255 .map_dma_buf = drm_gem_map_dma_buf,
256 .unmap_dma_buf = drm_gem_unmap_dma_buf,
257 .release = drm_gem_dmabuf_release,
258 .kmap = drm_gem_dmabuf_kmap,
259 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
260 .kunmap = drm_gem_dmabuf_kunmap,
261 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
262 .mmap = drm_gem_dmabuf_mmap,
263 .vmap = drm_gem_dmabuf_vmap,
264 .vunmap = drm_gem_dmabuf_vunmap,
265};
266
267/**
268 * DOC: PRIME Helpers
269 *
270 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
271 * simpler APIs by using the helper functions @drm_gem_prime_export and
272 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
273 * five lower-level driver callbacks:
274 *
275 * Export callbacks:
276 *
277 * - @gem_prime_pin (optional): prepare a GEM object for exporting
278 *
279 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
280 *
281 * - @gem_prime_vmap: vmap a buffer exported by your driver
282 *
283 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
284 *
285 * Import callback:
286 *
287 * - @gem_prime_import_sg_table (import): produce a GEM object from another
288 * driver's scatter/gather table
289 */
290
291struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
292 struct drm_gem_object *obj, int flags)
293{
Laurent Pinchartebc0bad2013-06-19 03:14:20 +0200294 return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
Aaron Plattner89177642013-01-15 20:47:42 +0000295}
296EXPORT_SYMBOL(drm_gem_prime_export);
297
Daniel Vetter319c9332013-08-15 00:02:46 +0200298static struct dma_buf *export_and_register_object(struct drm_device *dev,
299 struct drm_gem_object *obj,
300 uint32_t flags)
301{
302 struct dma_buf *dmabuf;
303
304 /* prevent races with concurrent gem_close. */
305 if (obj->handle_count == 0) {
306 dmabuf = ERR_PTR(-ENOENT);
307 return dmabuf;
308 }
309
310 dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
311 if (IS_ERR(dmabuf)) {
312 /* normally the created dma-buf takes ownership of the ref,
313 * but if that fails then drop the ref
314 */
315 return dmabuf;
316 }
317
318 /*
319 * Note that callers do not need to clean up the export cache
320 * since the check for obj->handle_count guarantees that someone
321 * will clean it up.
322 */
323 obj->dma_buf = dmabuf;
324 get_dma_buf(obj->dma_buf);
325
326 return dmabuf;
327}
328
Dave Airlie32488772011-11-25 15:21:02 +0000329int drm_gem_prime_handle_to_fd(struct drm_device *dev,
330 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
331 int *prime_fd)
332{
333 struct drm_gem_object *obj;
Dave Airlie219b4732013-04-22 09:54:36 +1000334 int ret = 0;
335 struct dma_buf *dmabuf;
Dave Airlie32488772011-11-25 15:21:02 +0000336
337 obj = drm_gem_object_lookup(dev, file_priv, handle);
338 if (!obj)
339 return -ENOENT;
340
Dave Airlie32488772011-11-25 15:21:02 +0000341 /* re-export the original imported object */
342 if (obj->import_attach) {
Dave Airlie219b4732013-04-22 09:54:36 +1000343 dmabuf = obj->import_attach->dmabuf;
Daniel Vetter319c9332013-08-15 00:02:46 +0200344 get_dma_buf(dmabuf);
Dave Airlie219b4732013-04-22 09:54:36 +1000345 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000346 }
347
Daniel Vetter319c9332013-08-15 00:02:46 +0200348 mutex_lock(&dev->object_name_lock);
349 if (obj->dma_buf) {
350 get_dma_buf(obj->dma_buf);
351 dmabuf = obj->dma_buf;
352 mutex_unlock(&dev->object_name_lock);
Dave Airlie219b4732013-04-22 09:54:36 +1000353 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000354 }
Dave Airlie219b4732013-04-22 09:54:36 +1000355
Daniel Vetter319c9332013-08-15 00:02:46 +0200356 dmabuf = export_and_register_object(dev, obj, flags);
357 mutex_unlock(&dev->object_name_lock);
Daniel Vetter4332bf42013-08-15 00:02:41 +0200358 if (IS_ERR(dmabuf)) {
Dave Airlie219b4732013-04-22 09:54:36 +1000359 /* normally the created dma-buf takes ownership of the ref,
360 * but if that fails then drop the ref
361 */
Daniel Vetter4332bf42013-08-15 00:02:41 +0200362 ret = PTR_ERR(dmabuf);
Dave Airlie219b4732013-04-22 09:54:36 +1000363 goto out;
364 }
Dave Airlie219b4732013-04-22 09:54:36 +1000365
Daniel Vetterbdf655d2013-08-15 00:02:42 +0200366 mutex_lock(&file_priv->prime.lock);
Dave Airlie0ff926c2012-05-20 17:31:16 +0100367 /* if we've exported this buffer the cheat and add it to the import list
368 * so we get the correct handle back
369 */
Dave Airlie219b4732013-04-22 09:54:36 +1000370 ret = drm_prime_add_buf_handle(&file_priv->prime,
Daniel Vetter319c9332013-08-15 00:02:46 +0200371 dmabuf, handle);
Dave Airlie219b4732013-04-22 09:54:36 +1000372 if (ret)
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900373 goto fail_put_dmabuf;
Dave Airlie0ff926c2012-05-20 17:31:16 +0100374
Daniel Vetter4332bf42013-08-15 00:02:41 +0200375 ret = dma_buf_fd(dmabuf, flags);
YoungJun Choda342422013-06-26 10:21:42 +0900376 if (ret < 0)
377 goto fail_rm_handle;
378
379 *prime_fd = ret;
Dave Airlie32488772011-11-25 15:21:02 +0000380 mutex_unlock(&file_priv->prime.lock);
381 return 0;
Dave Airlie219b4732013-04-22 09:54:36 +1000382
383out_have_obj:
YoungJun Choda342422013-06-26 10:21:42 +0900384 ret = dma_buf_fd(dmabuf, flags);
Daniel Vetter4a88f732013-07-02 09:18:39 +0200385 if (ret < 0) {
YoungJun Choda342422013-06-26 10:21:42 +0900386 dma_buf_put(dmabuf);
Daniel Vetter4a88f732013-07-02 09:18:39 +0200387 } else {
YoungJun Choda342422013-06-26 10:21:42 +0900388 *prime_fd = ret;
Daniel Vetter4a88f732013-07-02 09:18:39 +0200389 ret = 0;
390 }
391
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900392 goto out;
393
YoungJun Choda342422013-06-26 10:21:42 +0900394fail_rm_handle:
Daniel Vetter4332bf42013-08-15 00:02:41 +0200395 drm_prime_remove_buf_handle_locked(&file_priv->prime,
396 dmabuf);
Daniel Vetterbdf655d2013-08-15 00:02:42 +0200397 mutex_unlock(&file_priv->prime.lock);
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900398fail_put_dmabuf:
Daniel Vetter4332bf42013-08-15 00:02:41 +0200399 dma_buf_put(dmabuf);
Dave Airlie219b4732013-04-22 09:54:36 +1000400out:
401 drm_gem_object_unreference_unlocked(obj);
Dave Airlie219b4732013-04-22 09:54:36 +1000402 return ret;
Dave Airlie32488772011-11-25 15:21:02 +0000403}
404EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
405
Aaron Plattner89177642013-01-15 20:47:42 +0000406struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
407 struct dma_buf *dma_buf)
408{
409 struct dma_buf_attachment *attach;
410 struct sg_table *sgt;
411 struct drm_gem_object *obj;
412 int ret;
413
414 if (!dev->driver->gem_prime_import_sg_table)
415 return ERR_PTR(-EINVAL);
416
417 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
418 obj = dma_buf->priv;
419 if (obj->dev == dev) {
420 /*
421 * Importing dmabuf exported from out own gem increases
422 * refcount on gem itself instead of f_count of dmabuf.
423 */
424 drm_gem_object_reference(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000425 return obj;
426 }
427 }
428
429 attach = dma_buf_attach(dma_buf, dev->dev);
430 if (IS_ERR(attach))
Thomas Meyerf2a5da42013-06-01 10:09:27 +0000431 return ERR_CAST(attach);
Aaron Plattner89177642013-01-15 20:47:42 +0000432
Imre Deak011c2282013-04-19 11:11:56 +1000433 get_dma_buf(dma_buf);
434
Aaron Plattner89177642013-01-15 20:47:42 +0000435 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
436 if (IS_ERR_OR_NULL(sgt)) {
437 ret = PTR_ERR(sgt);
438 goto fail_detach;
439 }
440
441 obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
442 if (IS_ERR(obj)) {
443 ret = PTR_ERR(obj);
444 goto fail_unmap;
445 }
446
447 obj->import_attach = attach;
448
449 return obj;
450
451fail_unmap:
452 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
453fail_detach:
454 dma_buf_detach(dma_buf, attach);
Imre Deak011c2282013-04-19 11:11:56 +1000455 dma_buf_put(dma_buf);
456
Aaron Plattner89177642013-01-15 20:47:42 +0000457 return ERR_PTR(ret);
458}
459EXPORT_SYMBOL(drm_gem_prime_import);
460
Dave Airlie32488772011-11-25 15:21:02 +0000461int drm_gem_prime_fd_to_handle(struct drm_device *dev,
462 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
463{
464 struct dma_buf *dma_buf;
465 struct drm_gem_object *obj;
466 int ret;
467
468 dma_buf = dma_buf_get(prime_fd);
469 if (IS_ERR(dma_buf))
470 return PTR_ERR(dma_buf);
471
472 mutex_lock(&file_priv->prime.lock);
473
Dave Airlie219b4732013-04-22 09:54:36 +1000474 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000475 dma_buf, handle);
Daniel Vetter84341c22013-08-15 00:02:43 +0200476 if (ret == 0)
Dave Airlie32488772011-11-25 15:21:02 +0000477 goto out_put;
Dave Airlie32488772011-11-25 15:21:02 +0000478
479 /* never seen this one, need to import */
Daniel Vetter319c9332013-08-15 00:02:46 +0200480 mutex_lock(&dev->object_name_lock);
Dave Airlie32488772011-11-25 15:21:02 +0000481 obj = dev->driver->gem_prime_import(dev, dma_buf);
482 if (IS_ERR(obj)) {
483 ret = PTR_ERR(obj);
Daniel Vetter319c9332013-08-15 00:02:46 +0200484 goto out_unlock;
Dave Airlie32488772011-11-25 15:21:02 +0000485 }
486
Daniel Vetter319c9332013-08-15 00:02:46 +0200487 if (obj->dma_buf) {
488 WARN_ON(obj->dma_buf != dma_buf);
489 } else {
490 obj->dma_buf = dma_buf;
491 get_dma_buf(dma_buf);
492 }
493
494 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
495 ret = drm_gem_handle_create_tail(file_priv, obj, handle);
Dave Airlie32488772011-11-25 15:21:02 +0000496 drm_gem_object_unreference_unlocked(obj);
497 if (ret)
498 goto out_put;
499
Dave Airlie219b4732013-04-22 09:54:36 +1000500 ret = drm_prime_add_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000501 dma_buf, *handle);
502 if (ret)
503 goto fail;
504
505 mutex_unlock(&file_priv->prime.lock);
Imre Deak011c2282013-04-19 11:11:56 +1000506
507 dma_buf_put(dma_buf);
508
Dave Airlie32488772011-11-25 15:21:02 +0000509 return 0;
510
511fail:
512 /* hmm, if driver attached, we are relying on the free-object path
513 * to detach.. which seems ok..
514 */
Daniel Vetter730c4ff2013-08-15 00:02:38 +0200515 drm_gem_handle_delete(file_priv, *handle);
Daniel Vetter319c9332013-08-15 00:02:46 +0200516out_unlock:
517 mutex_lock(&dev->object_name_lock);
Dave Airlie32488772011-11-25 15:21:02 +0000518out_put:
519 dma_buf_put(dma_buf);
520 mutex_unlock(&file_priv->prime.lock);
521 return ret;
522}
523EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
524
525int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
526 struct drm_file *file_priv)
527{
528 struct drm_prime_handle *args = data;
529 uint32_t flags;
530
531 if (!drm_core_check_feature(dev, DRIVER_PRIME))
532 return -EINVAL;
533
534 if (!dev->driver->prime_handle_to_fd)
535 return -ENOSYS;
536
537 /* check flags are valid */
538 if (args->flags & ~DRM_CLOEXEC)
539 return -EINVAL;
540
541 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
542 flags = args->flags & DRM_CLOEXEC;
543
544 return dev->driver->prime_handle_to_fd(dev, file_priv,
545 args->handle, flags, &args->fd);
546}
547
548int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
549 struct drm_file *file_priv)
550{
551 struct drm_prime_handle *args = data;
552
553 if (!drm_core_check_feature(dev, DRIVER_PRIME))
554 return -EINVAL;
555
556 if (!dev->driver->prime_fd_to_handle)
557 return -ENOSYS;
558
559 return dev->driver->prime_fd_to_handle(dev, file_priv,
560 args->fd, &args->handle);
561}
562
563/*
564 * drm_prime_pages_to_sg
565 *
566 * this helper creates an sg table object from a set of pages
567 * the driver is responsible for mapping the pages into the
568 * importers address space
569 */
570struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
571{
572 struct sg_table *sg = NULL;
Dave Airlie32488772011-11-25 15:21:02 +0000573 int ret;
574
575 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900576 if (!sg) {
577 ret = -ENOMEM;
Dave Airlie32488772011-11-25 15:21:02 +0000578 goto out;
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900579 }
Dave Airlie32488772011-11-25 15:21:02 +0000580
Rahul Sharmadca25cb2013-01-28 08:38:48 -0500581 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
582 nr_pages << PAGE_SHIFT, GFP_KERNEL);
Dave Airlie32488772011-11-25 15:21:02 +0000583 if (ret)
584 goto out;
585
Dave Airlie32488772011-11-25 15:21:02 +0000586 return sg;
587out:
588 kfree(sg);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900589 return ERR_PTR(ret);
Dave Airlie32488772011-11-25 15:21:02 +0000590}
591EXPORT_SYMBOL(drm_prime_pages_to_sg);
592
Dave Airlie51ab7ba2012-05-18 15:40:33 +0100593/* export an sg table into an array of pages and addresses
594 this is currently required by the TTM driver in order to do correct fault
595 handling */
596int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
597 dma_addr_t *addrs, int max_pages)
598{
599 unsigned count;
600 struct scatterlist *sg;
601 struct page *page;
602 u32 len, offset;
603 int pg_index;
604 dma_addr_t addr;
605
606 pg_index = 0;
607 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
608 len = sg->length;
609 offset = sg->offset;
610 page = sg_page(sg);
611 addr = sg_dma_address(sg);
612
613 while (len > 0) {
614 if (WARN_ON(pg_index >= max_pages))
615 return -1;
616 pages[pg_index] = page;
617 if (addrs)
618 addrs[pg_index] = addr;
619
620 page++;
621 addr += PAGE_SIZE;
622 len -= PAGE_SIZE;
623 pg_index++;
624 }
625 }
626 return 0;
627}
628EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
Dave Airlie32488772011-11-25 15:21:02 +0000629/* helper function to cleanup a GEM/prime object */
630void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
631{
632 struct dma_buf_attachment *attach;
633 struct dma_buf *dma_buf;
634 attach = obj->import_attach;
635 if (sg)
636 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
637 dma_buf = attach->dmabuf;
638 dma_buf_detach(attach->dmabuf, attach);
639 /* remove the reference */
640 dma_buf_put(dma_buf);
641}
642EXPORT_SYMBOL(drm_prime_gem_destroy);
643
644void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
645{
646 INIT_LIST_HEAD(&prime_fpriv->head);
647 mutex_init(&prime_fpriv->lock);
648}
649EXPORT_SYMBOL(drm_prime_init_file_private);
650
651void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
652{
Imre Deak98b76232013-04-24 19:04:57 +0300653 /* by now drm_gem_release should've made sure the list is empty */
654 WARN_ON(!list_empty(&prime_fpriv->head));
Dave Airlie32488772011-11-25 15:21:02 +0000655}
656EXPORT_SYMBOL(drm_prime_destroy_file_private);
657
Dave Airlie219b4732013-04-22 09:54:36 +1000658int 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 +0000659{
660 struct drm_prime_member *member;
661
662 list_for_each_entry(member, &prime_fpriv->head, entry) {
663 if (member->dma_buf == dma_buf) {
664 *handle = member->handle;
665 return 0;
666 }
667 }
668 return -ENOENT;
669}
Dave Airlie219b4732013-04-22 09:54:36 +1000670EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
Dave Airlie32488772011-11-25 15:21:02 +0000671
Dave Airlie219b4732013-04-22 09:54:36 +1000672void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
Dave Airlie32488772011-11-25 15:21:02 +0000673{
Dave Airlie32488772011-11-25 15:21:02 +0000674 mutex_lock(&prime_fpriv->lock);
YoungJun Choda342422013-06-26 10:21:42 +0900675 drm_prime_remove_buf_handle_locked(prime_fpriv, dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000676 mutex_unlock(&prime_fpriv->lock);
677}
Dave Airlie219b4732013-04-22 09:54:36 +1000678EXPORT_SYMBOL(drm_prime_remove_buf_handle);