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
;
72 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
76 mutex_lock(&file_priv
->prime
.lock
);
77 /* re-export the original imported object */
78 if (obj
->import_attach
) {
79 get_dma_buf(obj
->import_attach
->dmabuf
);
80 *prime_fd
= dma_buf_fd(obj
->import_attach
->dmabuf
, flags
);
81 drm_gem_object_unreference_unlocked(obj
);
82 mutex_unlock(&file_priv
->prime
.lock
);
86 if (obj
->export_dma_buf
) {
87 get_dma_buf(obj
->export_dma_buf
);
88 *prime_fd
= dma_buf_fd(obj
->export_dma_buf
, flags
);
89 drm_gem_object_unreference_unlocked(obj
);
91 buf
= dev
->driver
->gem_prime_export(dev
, obj
, flags
);
93 /* normally the created dma-buf takes ownership of the ref,
94 * but if that fails then drop the ref
96 drm_gem_object_unreference_unlocked(obj
);
97 mutex_unlock(&file_priv
->prime
.lock
);
100 obj
->export_dma_buf
= buf
;
101 *prime_fd
= dma_buf_fd(buf
, flags
);
103 mutex_unlock(&file_priv
->prime
.lock
);
106 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd
);
108 int drm_gem_prime_fd_to_handle(struct drm_device
*dev
,
109 struct drm_file
*file_priv
, int prime_fd
, uint32_t *handle
)
111 struct dma_buf
*dma_buf
;
112 struct drm_gem_object
*obj
;
115 dma_buf
= dma_buf_get(prime_fd
);
117 return PTR_ERR(dma_buf
);
119 mutex_lock(&file_priv
->prime
.lock
);
121 ret
= drm_prime_lookup_imported_buf_handle(&file_priv
->prime
,
128 /* never seen this one, need to import */
129 obj
= dev
->driver
->gem_prime_import(dev
, dma_buf
);
135 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
136 drm_gem_object_unreference_unlocked(obj
);
140 ret
= drm_prime_add_imported_buf_handle(&file_priv
->prime
,
145 mutex_unlock(&file_priv
->prime
.lock
);
149 /* hmm, if driver attached, we are relying on the free-object path
150 * to detach.. which seems ok..
152 drm_gem_object_handle_unreference_unlocked(obj
);
154 dma_buf_put(dma_buf
);
155 mutex_unlock(&file_priv
->prime
.lock
);
158 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle
);
160 int drm_prime_handle_to_fd_ioctl(struct drm_device
*dev
, void *data
,
161 struct drm_file
*file_priv
)
163 struct drm_prime_handle
*args
= data
;
166 if (!drm_core_check_feature(dev
, DRIVER_PRIME
))
169 if (!dev
->driver
->prime_handle_to_fd
)
172 /* check flags are valid */
173 if (args
->flags
& ~DRM_CLOEXEC
)
176 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
177 flags
= args
->flags
& DRM_CLOEXEC
;
179 return dev
->driver
->prime_handle_to_fd(dev
, file_priv
,
180 args
->handle
, flags
, &args
->fd
);
183 int drm_prime_fd_to_handle_ioctl(struct drm_device
*dev
, void *data
,
184 struct drm_file
*file_priv
)
186 struct drm_prime_handle
*args
= data
;
188 if (!drm_core_check_feature(dev
, DRIVER_PRIME
))
191 if (!dev
->driver
->prime_fd_to_handle
)
194 return dev
->driver
->prime_fd_to_handle(dev
, file_priv
,
195 args
->fd
, &args
->handle
);
199 * drm_prime_pages_to_sg
201 * this helper creates an sg table object from a set of pages
202 * the driver is responsible for mapping the pages into the
203 * importers address space
205 struct sg_table
*drm_prime_pages_to_sg(struct page
**pages
, int nr_pages
)
207 struct sg_table
*sg
= NULL
;
208 struct scatterlist
*iter
;
212 sg
= kmalloc(sizeof(struct sg_table
), GFP_KERNEL
);
216 ret
= sg_alloc_table(sg
, nr_pages
, GFP_KERNEL
);
220 for_each_sg(sg
->sgl
, iter
, nr_pages
, i
)
221 sg_set_page(iter
, pages
[i
], PAGE_SIZE
, 0);
228 EXPORT_SYMBOL(drm_prime_pages_to_sg
);
230 /* helper function to cleanup a GEM/prime object */
231 void drm_prime_gem_destroy(struct drm_gem_object
*obj
, struct sg_table
*sg
)
233 struct dma_buf_attachment
*attach
;
234 struct dma_buf
*dma_buf
;
235 attach
= obj
->import_attach
;
237 dma_buf_unmap_attachment(attach
, sg
, DMA_BIDIRECTIONAL
);
238 dma_buf
= attach
->dmabuf
;
239 dma_buf_detach(attach
->dmabuf
, attach
);
240 /* remove the reference */
241 dma_buf_put(dma_buf
);
243 EXPORT_SYMBOL(drm_prime_gem_destroy
);
245 void drm_prime_init_file_private(struct drm_prime_file_private
*prime_fpriv
)
247 INIT_LIST_HEAD(&prime_fpriv
->head
);
248 mutex_init(&prime_fpriv
->lock
);
250 EXPORT_SYMBOL(drm_prime_init_file_private
);
252 void drm_prime_destroy_file_private(struct drm_prime_file_private
*prime_fpriv
)
254 struct drm_prime_member
*member
, *safe
;
255 list_for_each_entry_safe(member
, safe
, &prime_fpriv
->head
, entry
) {
256 list_del(&member
->entry
);
260 EXPORT_SYMBOL(drm_prime_destroy_file_private
);
262 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
, uint32_t handle
)
264 struct drm_prime_member
*member
;
266 member
= kmalloc(sizeof(*member
), GFP_KERNEL
);
270 member
->dma_buf
= dma_buf
;
271 member
->handle
= handle
;
272 list_add(&member
->entry
, &prime_fpriv
->head
);
275 EXPORT_SYMBOL(drm_prime_add_imported_buf_handle
);
277 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
, uint32_t *handle
)
279 struct drm_prime_member
*member
;
281 list_for_each_entry(member
, &prime_fpriv
->head
, entry
) {
282 if (member
->dma_buf
== dma_buf
) {
283 *handle
= member
->handle
;
289 EXPORT_SYMBOL(drm_prime_lookup_imported_buf_handle
);
291 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private
*prime_fpriv
, struct dma_buf
*dma_buf
)
293 struct drm_prime_member
*member
, *safe
;
295 mutex_lock(&prime_fpriv
->lock
);
296 list_for_each_entry_safe(member
, safe
, &prime_fpriv
->head
, entry
) {
297 if (member
->dma_buf
== dma_buf
) {
298 list_del(&member
->entry
);
302 mutex_unlock(&prime_fpriv
->lock
);
304 EXPORT_SYMBOL(drm_prime_remove_imported_buf_handle
);