2 * Copyright © 2012 Red Hat
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:
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
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
24 * Dave Airlie <airlied@redhat.com>
25 * Rob Clark <rob.clark@linaro.org>
29 #include <linux/export.h>
30 #include <linux/dma-buf.h>
34 * DMA-BUF/GEM Object references and lifetime overview:
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.
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.
50 * Thus the chain of references always flows in one direction
51 * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
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
56 * from the dma-buf private.
59 struct drm_prime_member
{
60 struct list_head entry
;
61 struct dma_buf
*dma_buf
;
65 int drm_gem_prime_handle_to_fd(struct drm_device
*dev
,
66 struct drm_file
*file_priv
, uint32_t handle
, uint32_t flags
,
69 struct drm_gem_object
*obj
;
73 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
77 mutex_lock(&file_priv
->prime
.lock
);
78 /* re-export the original imported object */
79 if (obj
->import_attach
) {
80 get_dma_buf(obj
->import_attach
->dmabuf
);
81 *prime_fd
= dma_buf_fd(obj
->import_attach
->dmabuf
, flags
);
82 drm_gem_object_unreference_unlocked(obj
);
83 mutex_unlock(&file_priv
->prime
.lock
);
87 if (obj
->export_dma_buf
) {
88 get_dma_buf(obj
->export_dma_buf
);
89 *prime_fd
= dma_buf_fd(obj
->export_dma_buf
, flags
);
90 drm_gem_object_unreference_unlocked(obj
);
92 buf
= dev
->driver
->gem_prime_export(dev
, obj
, flags
);
94 /* normally the created dma-buf takes ownership of the ref,
95 * but if that fails then drop the ref
97 drm_gem_object_unreference_unlocked(obj
);
98 mutex_unlock(&file_priv
->prime
.lock
);
101 obj
->export_dma_buf
= buf
;
102 *prime_fd
= dma_buf_fd(buf
, flags
);
104 /* if we've exported this buffer the cheat and add it to the import list
105 * so we get the correct handle back
107 ret
= drm_prime_add_imported_buf_handle(&file_priv
->prime
,
108 obj
->export_dma_buf
, handle
);
110 drm_gem_object_unreference_unlocked(obj
);
111 mutex_unlock(&file_priv
->prime
.lock
);
115 mutex_unlock(&file_priv
->prime
.lock
);
118 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd
);
120 int drm_gem_prime_fd_to_handle(struct drm_device
*dev
,
121 struct drm_file
*file_priv
, int prime_fd
, uint32_t *handle
)
123 struct dma_buf
*dma_buf
;
124 struct drm_gem_object
*obj
;
127 dma_buf
= dma_buf_get(prime_fd
);
129 return PTR_ERR(dma_buf
);
131 mutex_lock(&file_priv
->prime
.lock
);
133 ret
= drm_prime_lookup_imported_buf_handle(&file_priv
->prime
,
140 /* never seen this one, need to import */
141 obj
= dev
->driver
->gem_prime_import(dev
, dma_buf
);
147 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
148 drm_gem_object_unreference_unlocked(obj
);
152 ret
= drm_prime_add_imported_buf_handle(&file_priv
->prime
,
157 mutex_unlock(&file_priv
->prime
.lock
);
161 /* hmm, if driver attached, we are relying on the free-object path
162 * to detach.. which seems ok..
164 drm_gem_object_handle_unreference_unlocked(obj
);
166 dma_buf_put(dma_buf
);
167 mutex_unlock(&file_priv
->prime
.lock
);
170 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle
);
172 int drm_prime_handle_to_fd_ioctl(struct drm_device
*dev
, void *data
,
173 struct drm_file
*file_priv
)
175 struct drm_prime_handle
*args
= data
;
178 if (!drm_core_check_feature(dev
, DRIVER_PRIME
))
181 if (!dev
->driver
->prime_handle_to_fd
)
184 /* check flags are valid */
185 if (args
->flags
& ~DRM_CLOEXEC
)
188 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
189 flags
= args
->flags
& DRM_CLOEXEC
;
191 return dev
->driver
->prime_handle_to_fd(dev
, file_priv
,
192 args
->handle
, flags
, &args
->fd
);
195 int drm_prime_fd_to_handle_ioctl(struct drm_device
*dev
, void *data
,
196 struct drm_file
*file_priv
)
198 struct drm_prime_handle
*args
= data
;
200 if (!drm_core_check_feature(dev
, DRIVER_PRIME
))
203 if (!dev
->driver
->prime_fd_to_handle
)
206 return dev
->driver
->prime_fd_to_handle(dev
, file_priv
,
207 args
->fd
, &args
->handle
);
211 * drm_prime_pages_to_sg
213 * this helper creates an sg table object from a set of pages
214 * the driver is responsible for mapping the pages into the
215 * importers address space
217 struct sg_table
*drm_prime_pages_to_sg(struct page
**pages
, int nr_pages
)
219 struct sg_table
*sg
= NULL
;
220 struct scatterlist
*iter
;
224 sg
= kmalloc(sizeof(struct sg_table
), GFP_KERNEL
);
228 ret
= sg_alloc_table(sg
, nr_pages
, GFP_KERNEL
);
232 for_each_sg(sg
->sgl
, iter
, nr_pages
, i
)
233 sg_set_page(iter
, pages
[i
], PAGE_SIZE
, 0);
240 EXPORT_SYMBOL(drm_prime_pages_to_sg
);
242 /* export an sg table into an array of pages and addresses
243 this is currently required by the TTM driver in order to do correct fault
245 int drm_prime_sg_to_page_addr_arrays(struct sg_table
*sgt
, struct page
**pages
,
246 dma_addr_t
*addrs
, int max_pages
)
249 struct scatterlist
*sg
;
256 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, count
) {
260 addr
= sg_dma_address(sg
);
263 if (WARN_ON(pg_index
>= max_pages
))
265 pages
[pg_index
] = page
;
267 addrs
[pg_index
] = addr
;
277 EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays
);
278 /* helper function to cleanup a GEM/prime object */
279 void drm_prime_gem_destroy(struct drm_gem_object
*obj
, struct sg_table
*sg
)
281 struct dma_buf_attachment
*attach
;
282 struct dma_buf
*dma_buf
;
283 attach
= obj
->import_attach
;
285 dma_buf_unmap_attachment(attach
, sg
, DMA_BIDIRECTIONAL
);
286 dma_buf
= attach
->dmabuf
;
287 dma_buf_detach(attach
->dmabuf
, attach
);
288 /* remove the reference */
289 dma_buf_put(dma_buf
);
291 EXPORT_SYMBOL(drm_prime_gem_destroy
);
293 void drm_prime_init_file_private(struct drm_prime_file_private
*prime_fpriv
)
295 INIT_LIST_HEAD(&prime_fpriv
->head
);
296 mutex_init(&prime_fpriv
->lock
);
298 EXPORT_SYMBOL(drm_prime_init_file_private
);
300 void drm_prime_destroy_file_private(struct drm_prime_file_private
*prime_fpriv
)
302 struct drm_prime_member
*member
, *safe
;
303 list_for_each_entry_safe(member
, safe
, &prime_fpriv
->head
, entry
) {
304 list_del(&member
->entry
);
308 EXPORT_SYMBOL(drm_prime_destroy_file_private
);
310 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
, uint32_t handle
)
312 struct drm_prime_member
*member
;
314 member
= kmalloc(sizeof(*member
), GFP_KERNEL
);
318 member
->dma_buf
= dma_buf
;
319 member
->handle
= handle
;
320 list_add(&member
->entry
, &prime_fpriv
->head
);
323 EXPORT_SYMBOL(drm_prime_add_imported_buf_handle
);
325 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
, uint32_t *handle
)
327 struct drm_prime_member
*member
;
329 list_for_each_entry(member
, &prime_fpriv
->head
, entry
) {
330 if (member
->dma_buf
== dma_buf
) {
331 *handle
= member
->handle
;
337 EXPORT_SYMBOL(drm_prime_lookup_imported_buf_handle
);
339 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
)
341 struct drm_prime_member
*member
, *safe
;
343 mutex_lock(&prime_fpriv
->lock
);
344 list_for_each_entry_safe(member
, safe
, &prime_fpriv
->head
, entry
) {
345 if (member
->dma_buf
== dma_buf
) {
346 list_del(&member
->entry
);
350 mutex_unlock(&prime_fpriv
->lock
);
352 EXPORT_SYMBOL(drm_prime_remove_imported_buf_handle
);