drm_prime.c revision 1.1.1.4 1 1.1.1.3 riastrad /* $NetBSD: drm_prime.c,v 1.1.1.4 2021/12/18 20:11:03 riastradh Exp $ */
2 1.1.1.3 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Copyright 2012 Red Hat
5 1.1 riastrad *
6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
7 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
8 1.1 riastrad * to deal in the Software without restriction, including without limitation
9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
11 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
12 1.1 riastrad *
13 1.1 riastrad * The above copyright notice and this permission notice (including the next
14 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
15 1.1 riastrad * Software.
16 1.1 riastrad *
17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 1.1 riastrad * IN THE SOFTWARE.
24 1.1 riastrad *
25 1.1 riastrad * Authors:
26 1.1 riastrad * Dave Airlie <airlied (at) redhat.com>
27 1.1 riastrad * Rob Clark <rob.clark (at) linaro.org>
28 1.1 riastrad *
29 1.1 riastrad */
30 1.1 riastrad
31 1.1.1.3 riastrad #include <sys/cdefs.h>
32 1.1.1.3 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_prime.c,v 1.1.1.4 2021/12/18 20:11:03 riastradh Exp $");
33 1.1.1.3 riastrad
34 1.1 riastrad #include <linux/export.h>
35 1.1 riastrad #include <linux/dma-buf.h>
36 1.1.1.4 riastrad #include <linux/rbtree.h>
37 1.1.1.4 riastrad
38 1.1.1.4 riastrad #include <drm/drm.h>
39 1.1.1.4 riastrad #include <drm/drm_drv.h>
40 1.1.1.4 riastrad #include <drm/drm_file.h>
41 1.1.1.4 riastrad #include <drm/drm_framebuffer.h>
42 1.1.1.3 riastrad #include <drm/drm_gem.h>
43 1.1.1.4 riastrad #include <drm/drm_prime.h>
44 1.1.1.3 riastrad
45 1.1.1.3 riastrad #include "drm_internal.h"
46 1.1 riastrad
47 1.1.1.4 riastrad /**
48 1.1.1.4 riastrad * DOC: overview and lifetime rules
49 1.1 riastrad *
50 1.1.1.4 riastrad * Similar to GEM global names, PRIME file descriptors are also used to share
51 1.1.1.4 riastrad * buffer objects across processes. They offer additional security: as file
52 1.1.1.4 riastrad * descriptors must be explicitly sent over UNIX domain sockets to be shared
53 1.1.1.4 riastrad * between applications, they can't be guessed like the globally unique GEM
54 1.1.1.4 riastrad * names.
55 1.1.1.4 riastrad *
56 1.1.1.4 riastrad * Drivers that support the PRIME API implement the
57 1.1.1.4 riastrad * &drm_driver.prime_handle_to_fd and &drm_driver.prime_fd_to_handle operations.
58 1.1.1.4 riastrad * GEM based drivers must use drm_gem_prime_handle_to_fd() and
59 1.1.1.4 riastrad * drm_gem_prime_fd_to_handle() to implement these. For GEM based drivers the
60 1.1.1.4 riastrad * actual driver interfaces is provided through the &drm_gem_object_funcs.export
61 1.1.1.4 riastrad * and &drm_driver.gem_prime_import hooks.
62 1.1.1.4 riastrad *
63 1.1.1.4 riastrad * &dma_buf_ops implementations for GEM drivers are all individually exported
64 1.1.1.4 riastrad * for drivers which need to overwrite or reimplement some of them.
65 1.1.1.4 riastrad *
66 1.1.1.4 riastrad * Reference Counting for GEM Drivers
67 1.1.1.4 riastrad * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68 1.1.1.4 riastrad *
69 1.1.1.4 riastrad * On the export the &dma_buf holds a reference to the exported buffer object,
70 1.1.1.4 riastrad * usually a &drm_gem_object. It takes this reference in the PRIME_HANDLE_TO_FD
71 1.1.1.4 riastrad * IOCTL, when it first calls &drm_gem_object_funcs.export
72 1.1.1.4 riastrad * and stores the exporting GEM object in the &dma_buf.priv field. This
73 1.1.1.4 riastrad * reference needs to be released when the final reference to the &dma_buf
74 1.1.1.4 riastrad * itself is dropped and its &dma_buf_ops.release function is called. For
75 1.1.1.4 riastrad * GEM-based drivers, the &dma_buf should be exported using
76 1.1.1.4 riastrad * drm_gem_dmabuf_export() and then released by drm_gem_dmabuf_release().
77 1.1.1.4 riastrad *
78 1.1.1.4 riastrad * Thus the chain of references always flows in one direction, avoiding loops:
79 1.1.1.4 riastrad * importing GEM object -> dma-buf -> exported GEM bo. A further complication
80 1.1.1.4 riastrad * are the lookup caches for import and export. These are required to guarantee
81 1.1.1.4 riastrad * that any given object will always have only one uniqe userspace handle. This
82 1.1.1.4 riastrad * is required to allow userspace to detect duplicated imports, since some GEM
83 1.1.1.4 riastrad * drivers do fail command submissions if a given buffer object is listed more
84 1.1.1.4 riastrad * than once. These import and export caches in &drm_prime_file_private only
85 1.1.1.4 riastrad * retain a weak reference, which is cleaned up when the corresponding object is
86 1.1.1.4 riastrad * released.
87 1.1.1.4 riastrad *
88 1.1.1.4 riastrad * Self-importing: If userspace is using PRIME as a replacement for flink then
89 1.1.1.4 riastrad * it will get a fd->handle request for a GEM object that it created. Drivers
90 1.1.1.4 riastrad * should detect this situation and return back the underlying object from the
91 1.1.1.4 riastrad * dma-buf private. For GEM based drivers this is handled in
92 1.1.1.4 riastrad * drm_gem_prime_import() already.
93 1.1 riastrad */
94 1.1 riastrad
95 1.1 riastrad struct drm_prime_member {
96 1.1 riastrad struct dma_buf *dma_buf;
97 1.1 riastrad uint32_t handle;
98 1.1 riastrad
99 1.1.1.4 riastrad struct rb_node dmabuf_rb;
100 1.1.1.4 riastrad struct rb_node handle_rb;
101 1.1.1.2 riastrad };
102 1.1.1.2 riastrad
103 1.1.1.2 riastrad static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
104 1.1.1.2 riastrad struct dma_buf *dma_buf, uint32_t handle)
105 1.1.1.2 riastrad {
106 1.1.1.2 riastrad struct drm_prime_member *member;
107 1.1.1.4 riastrad struct rb_node **p, *rb;
108 1.1.1.2 riastrad
109 1.1.1.2 riastrad member = kmalloc(sizeof(*member), GFP_KERNEL);
110 1.1.1.2 riastrad if (!member)
111 1.1.1.2 riastrad return -ENOMEM;
112 1.1.1.2 riastrad
113 1.1.1.2 riastrad get_dma_buf(dma_buf);
114 1.1.1.2 riastrad member->dma_buf = dma_buf;
115 1.1.1.2 riastrad member->handle = handle;
116 1.1.1.4 riastrad
117 1.1.1.4 riastrad rb = NULL;
118 1.1.1.4 riastrad p = &prime_fpriv->dmabufs.rb_node;
119 1.1.1.4 riastrad while (*p) {
120 1.1.1.4 riastrad struct drm_prime_member *pos;
121 1.1.1.4 riastrad
122 1.1.1.4 riastrad rb = *p;
123 1.1.1.4 riastrad pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
124 1.1.1.4 riastrad if (dma_buf > pos->dma_buf)
125 1.1.1.4 riastrad p = &rb->rb_right;
126 1.1.1.4 riastrad else
127 1.1.1.4 riastrad p = &rb->rb_left;
128 1.1.1.4 riastrad }
129 1.1.1.4 riastrad rb_link_node(&member->dmabuf_rb, rb, p);
130 1.1.1.4 riastrad rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
131 1.1.1.4 riastrad
132 1.1.1.4 riastrad rb = NULL;
133 1.1.1.4 riastrad p = &prime_fpriv->handles.rb_node;
134 1.1.1.4 riastrad while (*p) {
135 1.1.1.4 riastrad struct drm_prime_member *pos;
136 1.1.1.4 riastrad
137 1.1.1.4 riastrad rb = *p;
138 1.1.1.4 riastrad pos = rb_entry(rb, struct drm_prime_member, handle_rb);
139 1.1.1.4 riastrad if (handle > pos->handle)
140 1.1.1.4 riastrad p = &rb->rb_right;
141 1.1.1.4 riastrad else
142 1.1.1.4 riastrad p = &rb->rb_left;
143 1.1.1.4 riastrad }
144 1.1.1.4 riastrad rb_link_node(&member->handle_rb, rb, p);
145 1.1.1.4 riastrad rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
146 1.1.1.4 riastrad
147 1.1.1.2 riastrad return 0;
148 1.1.1.2 riastrad }
149 1.1.1.2 riastrad
150 1.1.1.2 riastrad static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
151 1.1.1.2 riastrad uint32_t handle)
152 1.1.1.2 riastrad {
153 1.1.1.4 riastrad struct rb_node *rb;
154 1.1.1.2 riastrad
155 1.1.1.4 riastrad rb = prime_fpriv->handles.rb_node;
156 1.1.1.4 riastrad while (rb) {
157 1.1.1.4 riastrad struct drm_prime_member *member;
158 1.1.1.4 riastrad
159 1.1.1.4 riastrad member = rb_entry(rb, struct drm_prime_member, handle_rb);
160 1.1.1.2 riastrad if (member->handle == handle)
161 1.1.1.2 riastrad return member->dma_buf;
162 1.1.1.4 riastrad else if (member->handle < handle)
163 1.1.1.4 riastrad rb = rb->rb_right;
164 1.1.1.4 riastrad else
165 1.1.1.4 riastrad rb = rb->rb_left;
166 1.1.1.2 riastrad }
167 1.1.1.2 riastrad
168 1.1.1.2 riastrad return NULL;
169 1.1.1.2 riastrad }
170 1.1.1.2 riastrad
171 1.1.1.2 riastrad static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
172 1.1.1.2 riastrad struct dma_buf *dma_buf,
173 1.1.1.2 riastrad uint32_t *handle)
174 1.1.1.2 riastrad {
175 1.1.1.4 riastrad struct rb_node *rb;
176 1.1.1.2 riastrad
177 1.1.1.4 riastrad rb = prime_fpriv->dmabufs.rb_node;
178 1.1.1.4 riastrad while (rb) {
179 1.1.1.4 riastrad struct drm_prime_member *member;
180 1.1.1.4 riastrad
181 1.1.1.4 riastrad member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
182 1.1.1.2 riastrad if (member->dma_buf == dma_buf) {
183 1.1.1.2 riastrad *handle = member->handle;
184 1.1.1.2 riastrad return 0;
185 1.1.1.4 riastrad } else if (member->dma_buf < dma_buf) {
186 1.1.1.4 riastrad rb = rb->rb_right;
187 1.1.1.4 riastrad } else {
188 1.1.1.4 riastrad rb = rb->rb_left;
189 1.1.1.2 riastrad }
190 1.1.1.2 riastrad }
191 1.1.1.2 riastrad
192 1.1.1.4 riastrad return -ENOENT;
193 1.1.1.2 riastrad }
194 1.1.1.2 riastrad
195 1.1.1.2 riastrad void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
196 1.1.1.2 riastrad struct dma_buf *dma_buf)
197 1.1.1.2 riastrad {
198 1.1.1.4 riastrad struct rb_node *rb;
199 1.1.1.2 riastrad
200 1.1.1.4 riastrad rb = prime_fpriv->dmabufs.rb_node;
201 1.1.1.4 riastrad while (rb) {
202 1.1.1.4 riastrad struct drm_prime_member *member;
203 1.1.1.4 riastrad
204 1.1.1.4 riastrad member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
205 1.1.1.2 riastrad if (member->dma_buf == dma_buf) {
206 1.1.1.4 riastrad rb_erase(&member->handle_rb, &prime_fpriv->handles);
207 1.1.1.4 riastrad rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
208 1.1.1.4 riastrad
209 1.1.1.2 riastrad dma_buf_put(dma_buf);
210 1.1.1.2 riastrad kfree(member);
211 1.1.1.4 riastrad return;
212 1.1.1.4 riastrad } else if (member->dma_buf < dma_buf) {
213 1.1.1.4 riastrad rb = rb->rb_right;
214 1.1.1.4 riastrad } else {
215 1.1.1.4 riastrad rb = rb->rb_left;
216 1.1.1.2 riastrad }
217 1.1.1.2 riastrad }
218 1.1.1.2 riastrad }
219 1.1.1.2 riastrad
220 1.1.1.4 riastrad void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
221 1.1.1.2 riastrad {
222 1.1.1.4 riastrad mutex_init(&prime_fpriv->lock);
223 1.1.1.4 riastrad prime_fpriv->dmabufs = RB_ROOT;
224 1.1.1.4 riastrad prime_fpriv->handles = RB_ROOT;
225 1.1.1.4 riastrad }
226 1.1.1.2 riastrad
227 1.1.1.4 riastrad void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
228 1.1.1.4 riastrad {
229 1.1.1.4 riastrad /* by now drm_gem_release should've made sure the list is empty */
230 1.1.1.4 riastrad WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
231 1.1.1.4 riastrad }
232 1.1.1.2 riastrad
233 1.1.1.4 riastrad /**
234 1.1.1.4 riastrad * drm_gem_dmabuf_export - &dma_buf export implementation for GEM
235 1.1.1.4 riastrad * @dev: parent device for the exported dmabuf
236 1.1.1.4 riastrad * @exp_info: the export information used by dma_buf_export()
237 1.1.1.4 riastrad *
238 1.1.1.4 riastrad * This wraps dma_buf_export() for use by generic GEM drivers that are using
239 1.1.1.4 riastrad * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
240 1.1.1.4 riastrad * a reference to the &drm_device and the exported &drm_gem_object (stored in
241 1.1.1.4 riastrad * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
242 1.1.1.4 riastrad *
243 1.1.1.4 riastrad * Returns the new dmabuf.
244 1.1.1.4 riastrad */
245 1.1.1.4 riastrad struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
246 1.1.1.4 riastrad struct dma_buf_export_info *exp_info)
247 1.1.1.4 riastrad {
248 1.1.1.4 riastrad struct drm_gem_object *obj = exp_info->priv;
249 1.1.1.4 riastrad struct dma_buf *dma_buf;
250 1.1.1.2 riastrad
251 1.1.1.4 riastrad dma_buf = dma_buf_export(exp_info);
252 1.1.1.4 riastrad if (IS_ERR(dma_buf))
253 1.1.1.4 riastrad return dma_buf;
254 1.1.1.2 riastrad
255 1.1.1.4 riastrad drm_dev_get(dev);
256 1.1.1.4 riastrad drm_gem_object_get(obj);
257 1.1.1.4 riastrad dma_buf->file->f_mapping = obj->dev->anon_inode->i_mapping;
258 1.1.1.2 riastrad
259 1.1.1.4 riastrad return dma_buf;
260 1.1.1.2 riastrad }
261 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_export);
262 1.1.1.2 riastrad
263 1.1.1.2 riastrad /**
264 1.1.1.4 riastrad * drm_gem_dmabuf_release - &dma_buf release implementation for GEM
265 1.1.1.2 riastrad * @dma_buf: buffer to be released
266 1.1.1.2 riastrad *
267 1.1.1.2 riastrad * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
268 1.1.1.4 riastrad * must use this in their &dma_buf_ops structure as the release callback.
269 1.1.1.4 riastrad * drm_gem_dmabuf_release() should be used in conjunction with
270 1.1.1.4 riastrad * drm_gem_dmabuf_export().
271 1.1.1.2 riastrad */
272 1.1.1.2 riastrad void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
273 1.1.1.2 riastrad {
274 1.1.1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
275 1.1.1.4 riastrad struct drm_device *dev = obj->dev;
276 1.1.1.2 riastrad
277 1.1.1.2 riastrad /* drop the reference on the export fd holds */
278 1.1.1.4 riastrad drm_gem_object_put_unlocked(obj);
279 1.1.1.4 riastrad
280 1.1.1.4 riastrad drm_dev_put(dev);
281 1.1.1.2 riastrad }
282 1.1.1.2 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_release);
283 1.1.1.2 riastrad
284 1.1.1.4 riastrad /**
285 1.1.1.4 riastrad * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
286 1.1.1.4 riastrad * @dev: dev to export the buffer from
287 1.1.1.4 riastrad * @file_priv: drm file-private structure
288 1.1.1.4 riastrad * @prime_fd: fd id of the dma-buf which should be imported
289 1.1.1.4 riastrad * @handle: pointer to storage for the handle of the imported buffer object
290 1.1.1.4 riastrad *
291 1.1.1.4 riastrad * This is the PRIME import function which must be used mandatorily by GEM
292 1.1.1.4 riastrad * drivers to ensure correct lifetime management of the underlying GEM object.
293 1.1.1.4 riastrad * The actual importing of GEM object from the dma-buf is done through the
294 1.1.1.4 riastrad * &drm_driver.gem_prime_import driver callback.
295 1.1.1.4 riastrad *
296 1.1.1.4 riastrad * Returns 0 on success or a negative error code on failure.
297 1.1.1.4 riastrad */
298 1.1.1.4 riastrad int drm_gem_prime_fd_to_handle(struct drm_device *dev,
299 1.1.1.4 riastrad struct drm_file *file_priv, int prime_fd,
300 1.1.1.4 riastrad uint32_t *handle)
301 1.1.1.2 riastrad {
302 1.1.1.4 riastrad struct dma_buf *dma_buf;
303 1.1.1.4 riastrad struct drm_gem_object *obj;
304 1.1.1.4 riastrad int ret;
305 1.1.1.2 riastrad
306 1.1.1.4 riastrad dma_buf = dma_buf_get(prime_fd);
307 1.1.1.4 riastrad if (IS_ERR(dma_buf))
308 1.1.1.4 riastrad return PTR_ERR(dma_buf);
309 1.1.1.2 riastrad
310 1.1.1.4 riastrad mutex_lock(&file_priv->prime.lock);
311 1.1.1.2 riastrad
312 1.1.1.4 riastrad ret = drm_prime_lookup_buf_handle(&file_priv->prime,
313 1.1.1.4 riastrad dma_buf, handle);
314 1.1.1.4 riastrad if (ret == 0)
315 1.1.1.4 riastrad goto out_put;
316 1.1.1.2 riastrad
317 1.1.1.4 riastrad /* never seen this one, need to import */
318 1.1.1.4 riastrad mutex_lock(&dev->object_name_lock);
319 1.1.1.4 riastrad if (dev->driver->gem_prime_import)
320 1.1.1.4 riastrad obj = dev->driver->gem_prime_import(dev, dma_buf);
321 1.1.1.4 riastrad else
322 1.1.1.4 riastrad obj = drm_gem_prime_import(dev, dma_buf);
323 1.1.1.4 riastrad if (IS_ERR(obj)) {
324 1.1.1.4 riastrad ret = PTR_ERR(obj);
325 1.1.1.4 riastrad goto out_unlock;
326 1.1.1.4 riastrad }
327 1.1.1.2 riastrad
328 1.1.1.4 riastrad if (obj->dma_buf) {
329 1.1.1.4 riastrad WARN_ON(obj->dma_buf != dma_buf);
330 1.1.1.4 riastrad } else {
331 1.1.1.4 riastrad obj->dma_buf = dma_buf;
332 1.1.1.4 riastrad get_dma_buf(dma_buf);
333 1.1.1.4 riastrad }
334 1.1.1.2 riastrad
335 1.1.1.4 riastrad /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
336 1.1.1.4 riastrad ret = drm_gem_handle_create_tail(file_priv, obj, handle);
337 1.1.1.4 riastrad drm_gem_object_put_unlocked(obj);
338 1.1.1.4 riastrad if (ret)
339 1.1.1.4 riastrad goto out_put;
340 1.1.1.2 riastrad
341 1.1.1.4 riastrad ret = drm_prime_add_buf_handle(&file_priv->prime,
342 1.1.1.4 riastrad dma_buf, *handle);
343 1.1.1.4 riastrad mutex_unlock(&file_priv->prime.lock);
344 1.1.1.4 riastrad if (ret)
345 1.1.1.4 riastrad goto fail;
346 1.1.1.2 riastrad
347 1.1.1.4 riastrad dma_buf_put(dma_buf);
348 1.1.1.2 riastrad
349 1.1.1.4 riastrad return 0;
350 1.1.1.2 riastrad
351 1.1.1.4 riastrad fail:
352 1.1.1.4 riastrad /* hmm, if driver attached, we are relying on the free-object path
353 1.1.1.4 riastrad * to detach.. which seems ok..
354 1.1.1.4 riastrad */
355 1.1.1.4 riastrad drm_gem_handle_delete(file_priv, *handle);
356 1.1.1.4 riastrad dma_buf_put(dma_buf);
357 1.1.1.4 riastrad return ret;
358 1.1.1.2 riastrad
359 1.1.1.4 riastrad out_unlock:
360 1.1.1.4 riastrad mutex_unlock(&dev->object_name_lock);
361 1.1.1.4 riastrad out_put:
362 1.1.1.4 riastrad mutex_unlock(&file_priv->prime.lock);
363 1.1.1.4 riastrad dma_buf_put(dma_buf);
364 1.1.1.4 riastrad return ret;
365 1.1.1.2 riastrad }
366 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
367 1.1.1.2 riastrad
368 1.1.1.4 riastrad int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
369 1.1.1.4 riastrad struct drm_file *file_priv)
370 1.1.1.2 riastrad {
371 1.1.1.4 riastrad struct drm_prime_handle *args = data;
372 1.1.1.3 riastrad
373 1.1.1.4 riastrad if (!dev->driver->prime_fd_to_handle)
374 1.1.1.4 riastrad return -ENOSYS;
375 1.1.1.3 riastrad
376 1.1.1.4 riastrad return dev->driver->prime_fd_to_handle(dev, file_priv,
377 1.1.1.4 riastrad args->fd, &args->handle);
378 1.1.1.2 riastrad }
379 1.1.1.2 riastrad
380 1.1.1.2 riastrad static struct dma_buf *export_and_register_object(struct drm_device *dev,
381 1.1.1.2 riastrad struct drm_gem_object *obj,
382 1.1.1.2 riastrad uint32_t flags)
383 1.1.1.2 riastrad {
384 1.1.1.2 riastrad struct dma_buf *dmabuf;
385 1.1.1.2 riastrad
386 1.1.1.2 riastrad /* prevent races with concurrent gem_close. */
387 1.1.1.2 riastrad if (obj->handle_count == 0) {
388 1.1.1.2 riastrad dmabuf = ERR_PTR(-ENOENT);
389 1.1.1.2 riastrad return dmabuf;
390 1.1.1.2 riastrad }
391 1.1.1.2 riastrad
392 1.1.1.4 riastrad if (obj->funcs && obj->funcs->export)
393 1.1.1.4 riastrad dmabuf = obj->funcs->export(obj, flags);
394 1.1.1.4 riastrad else if (dev->driver->gem_prime_export)
395 1.1.1.4 riastrad dmabuf = dev->driver->gem_prime_export(obj, flags);
396 1.1.1.4 riastrad else
397 1.1.1.4 riastrad dmabuf = drm_gem_prime_export(obj, flags);
398 1.1.1.2 riastrad if (IS_ERR(dmabuf)) {
399 1.1.1.2 riastrad /* normally the created dma-buf takes ownership of the ref,
400 1.1.1.2 riastrad * but if that fails then drop the ref
401 1.1.1.2 riastrad */
402 1.1.1.2 riastrad return dmabuf;
403 1.1.1.2 riastrad }
404 1.1.1.2 riastrad
405 1.1.1.2 riastrad /*
406 1.1.1.2 riastrad * Note that callers do not need to clean up the export cache
407 1.1.1.2 riastrad * since the check for obj->handle_count guarantees that someone
408 1.1.1.2 riastrad * will clean it up.
409 1.1.1.2 riastrad */
410 1.1.1.2 riastrad obj->dma_buf = dmabuf;
411 1.1.1.2 riastrad get_dma_buf(obj->dma_buf);
412 1.1.1.2 riastrad
413 1.1.1.2 riastrad return dmabuf;
414 1.1.1.2 riastrad }
415 1.1.1.2 riastrad
416 1.1.1.2 riastrad /**
417 1.1.1.2 riastrad * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
418 1.1.1.2 riastrad * @dev: dev to export the buffer from
419 1.1.1.2 riastrad * @file_priv: drm file-private structure
420 1.1.1.2 riastrad * @handle: buffer handle to export
421 1.1.1.2 riastrad * @flags: flags like DRM_CLOEXEC
422 1.1.1.2 riastrad * @prime_fd: pointer to storage for the fd id of the create dma-buf
423 1.1.1.2 riastrad *
424 1.1.1.2 riastrad * This is the PRIME export function which must be used mandatorily by GEM
425 1.1.1.2 riastrad * drivers to ensure correct lifetime management of the underlying GEM object.
426 1.1.1.2 riastrad * The actual exporting from GEM object to a dma-buf is done through the
427 1.1.1.4 riastrad * &drm_driver.gem_prime_export driver callback.
428 1.1.1.2 riastrad */
429 1.1 riastrad int drm_gem_prime_handle_to_fd(struct drm_device *dev,
430 1.1.1.2 riastrad struct drm_file *file_priv, uint32_t handle,
431 1.1.1.2 riastrad uint32_t flags,
432 1.1.1.2 riastrad int *prime_fd)
433 1.1 riastrad {
434 1.1 riastrad struct drm_gem_object *obj;
435 1.1.1.2 riastrad int ret = 0;
436 1.1.1.2 riastrad struct dma_buf *dmabuf;
437 1.1 riastrad
438 1.1.1.2 riastrad mutex_lock(&file_priv->prime.lock);
439 1.1.1.4 riastrad obj = drm_gem_object_lookup(file_priv, handle);
440 1.1.1.2 riastrad if (!obj) {
441 1.1.1.2 riastrad ret = -ENOENT;
442 1.1.1.2 riastrad goto out_unlock;
443 1.1.1.2 riastrad }
444 1.1 riastrad
445 1.1.1.2 riastrad dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
446 1.1.1.2 riastrad if (dmabuf) {
447 1.1.1.2 riastrad get_dma_buf(dmabuf);
448 1.1.1.2 riastrad goto out_have_handle;
449 1.1.1.2 riastrad }
450 1.1.1.2 riastrad
451 1.1.1.2 riastrad mutex_lock(&dev->object_name_lock);
452 1.1 riastrad /* re-export the original imported object */
453 1.1 riastrad if (obj->import_attach) {
454 1.1.1.2 riastrad dmabuf = obj->import_attach->dmabuf;
455 1.1.1.2 riastrad get_dma_buf(dmabuf);
456 1.1.1.2 riastrad goto out_have_obj;
457 1.1 riastrad }
458 1.1 riastrad
459 1.1.1.2 riastrad if (obj->dma_buf) {
460 1.1.1.2 riastrad get_dma_buf(obj->dma_buf);
461 1.1.1.2 riastrad dmabuf = obj->dma_buf;
462 1.1.1.2 riastrad goto out_have_obj;
463 1.1 riastrad }
464 1.1.1.2 riastrad
465 1.1.1.2 riastrad dmabuf = export_and_register_object(dev, obj, flags);
466 1.1.1.2 riastrad if (IS_ERR(dmabuf)) {
467 1.1.1.2 riastrad /* normally the created dma-buf takes ownership of the ref,
468 1.1.1.2 riastrad * but if that fails then drop the ref
469 1.1.1.2 riastrad */
470 1.1.1.2 riastrad ret = PTR_ERR(dmabuf);
471 1.1.1.2 riastrad mutex_unlock(&dev->object_name_lock);
472 1.1.1.2 riastrad goto out;
473 1.1.1.2 riastrad }
474 1.1.1.2 riastrad
475 1.1.1.2 riastrad out_have_obj:
476 1.1.1.2 riastrad /*
477 1.1.1.2 riastrad * If we've exported this buffer then cheat and add it to the import list
478 1.1.1.2 riastrad * so we get the correct handle back. We must do this under the
479 1.1.1.2 riastrad * protection of dev->object_name_lock to ensure that a racing gem close
480 1.1.1.2 riastrad * ioctl doesn't miss to remove this buffer handle from the cache.
481 1.1.1.2 riastrad */
482 1.1.1.2 riastrad ret = drm_prime_add_buf_handle(&file_priv->prime,
483 1.1.1.2 riastrad dmabuf, handle);
484 1.1.1.2 riastrad mutex_unlock(&dev->object_name_lock);
485 1.1.1.2 riastrad if (ret)
486 1.1.1.2 riastrad goto fail_put_dmabuf;
487 1.1.1.2 riastrad
488 1.1.1.2 riastrad out_have_handle:
489 1.1.1.2 riastrad ret = dma_buf_fd(dmabuf, flags);
490 1.1.1.2 riastrad /*
491 1.1.1.2 riastrad * We must _not_ remove the buffer from the handle cache since the newly
492 1.1.1.2 riastrad * created dma buf is already linked in the global obj->dma_buf pointer,
493 1.1.1.2 riastrad * and that is invariant as long as a userspace gem handle exists.
494 1.1.1.2 riastrad * Closing the handle will clean out the cache anyway, so we don't leak.
495 1.1 riastrad */
496 1.1.1.2 riastrad if (ret < 0) {
497 1.1.1.2 riastrad goto fail_put_dmabuf;
498 1.1.1.2 riastrad } else {
499 1.1.1.2 riastrad *prime_fd = ret;
500 1.1.1.2 riastrad ret = 0;
501 1.1 riastrad }
502 1.1 riastrad
503 1.1.1.2 riastrad goto out;
504 1.1.1.2 riastrad
505 1.1.1.2 riastrad fail_put_dmabuf:
506 1.1.1.2 riastrad dma_buf_put(dmabuf);
507 1.1.1.2 riastrad out:
508 1.1.1.4 riastrad drm_gem_object_put_unlocked(obj);
509 1.1.1.2 riastrad out_unlock:
510 1.1 riastrad mutex_unlock(&file_priv->prime.lock);
511 1.1.1.2 riastrad
512 1.1.1.2 riastrad return ret;
513 1.1 riastrad }
514 1.1 riastrad EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
515 1.1 riastrad
516 1.1.1.4 riastrad int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
517 1.1.1.4 riastrad struct drm_file *file_priv)
518 1.1.1.4 riastrad {
519 1.1.1.4 riastrad struct drm_prime_handle *args = data;
520 1.1.1.4 riastrad
521 1.1.1.4 riastrad if (!dev->driver->prime_handle_to_fd)
522 1.1.1.4 riastrad return -ENOSYS;
523 1.1.1.4 riastrad
524 1.1.1.4 riastrad /* check flags are valid */
525 1.1.1.4 riastrad if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
526 1.1.1.4 riastrad return -EINVAL;
527 1.1.1.4 riastrad
528 1.1.1.4 riastrad return dev->driver->prime_handle_to_fd(dev, file_priv,
529 1.1.1.4 riastrad args->handle, args->flags, &args->fd);
530 1.1.1.4 riastrad }
531 1.1.1.4 riastrad
532 1.1.1.2 riastrad /**
533 1.1.1.4 riastrad * DOC: PRIME Helpers
534 1.1.1.2 riastrad *
535 1.1.1.4 riastrad * Drivers can implement &drm_gem_object_funcs.export and
536 1.1.1.4 riastrad * &drm_driver.gem_prime_import in terms of simpler APIs by using the helper
537 1.1.1.4 riastrad * functions drm_gem_prime_export() and drm_gem_prime_import(). These functions
538 1.1.1.4 riastrad * implement dma-buf support in terms of some lower-level helpers, which are
539 1.1.1.4 riastrad * again exported for drivers to use individually:
540 1.1.1.4 riastrad *
541 1.1.1.4 riastrad * Exporting buffers
542 1.1.1.4 riastrad * ~~~~~~~~~~~~~~~~~
543 1.1.1.4 riastrad *
544 1.1.1.4 riastrad * Optional pinning of buffers is handled at dma-buf attach and detach time in
545 1.1.1.4 riastrad * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
546 1.1.1.4 riastrad * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
547 1.1.1.4 riastrad * &drm_gem_object_funcs.get_sg_table.
548 1.1.1.4 riastrad *
549 1.1.1.4 riastrad * For kernel-internal access there's drm_gem_dmabuf_vmap() and
550 1.1.1.4 riastrad * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
551 1.1.1.4 riastrad * drm_gem_dmabuf_mmap().
552 1.1.1.4 riastrad *
553 1.1.1.4 riastrad * Note that these export helpers can only be used if the underlying backing
554 1.1.1.4 riastrad * storage is fully coherent and either permanently pinned, or it is safe to pin
555 1.1.1.4 riastrad * it indefinitely.
556 1.1.1.4 riastrad *
557 1.1.1.4 riastrad * FIXME: The underlying helper functions are named rather inconsistently.
558 1.1.1.4 riastrad *
559 1.1.1.4 riastrad * Exporting buffers
560 1.1.1.4 riastrad * ~~~~~~~~~~~~~~~~~
561 1.1.1.4 riastrad *
562 1.1.1.4 riastrad * Importing dma-bufs using drm_gem_prime_import() relies on
563 1.1.1.4 riastrad * &drm_driver.gem_prime_import_sg_table.
564 1.1.1.4 riastrad *
565 1.1.1.4 riastrad * Note that similarly to the export helpers this permanently pins the
566 1.1.1.4 riastrad * underlying backing storage. Which is ok for scanout, but is not the best
567 1.1.1.4 riastrad * option for sharing lots of buffers for rendering.
568 1.1.1.2 riastrad */
569 1.1.1.4 riastrad
570 1.1.1.4 riastrad /**
571 1.1.1.4 riastrad * drm_gem_map_attach - dma_buf attach implementation for GEM
572 1.1.1.4 riastrad * @dma_buf: buffer to attach device to
573 1.1.1.4 riastrad * @attach: buffer attachment data
574 1.1.1.4 riastrad *
575 1.1.1.4 riastrad * Calls &drm_gem_object_funcs.pin for device specific handling. This can be
576 1.1.1.4 riastrad * used as the &dma_buf_ops.attach callback. Must be used together with
577 1.1.1.4 riastrad * drm_gem_map_detach().
578 1.1.1.4 riastrad *
579 1.1.1.4 riastrad * Returns 0 on success, negative error code on failure.
580 1.1.1.4 riastrad */
581 1.1.1.4 riastrad int drm_gem_map_attach(struct dma_buf *dma_buf,
582 1.1.1.4 riastrad struct dma_buf_attachment *attach)
583 1.1.1.2 riastrad {
584 1.1.1.4 riastrad struct drm_gem_object *obj = dma_buf->priv;
585 1.1.1.2 riastrad
586 1.1.1.4 riastrad return drm_gem_pin(obj);
587 1.1.1.4 riastrad }
588 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_map_attach);
589 1.1.1.2 riastrad
590 1.1.1.4 riastrad /**
591 1.1.1.4 riastrad * drm_gem_map_detach - dma_buf detach implementation for GEM
592 1.1.1.4 riastrad * @dma_buf: buffer to detach from
593 1.1.1.4 riastrad * @attach: attachment to be detached
594 1.1.1.4 riastrad *
595 1.1.1.4 riastrad * Calls &drm_gem_object_funcs.pin for device specific handling. Cleans up
596 1.1.1.4 riastrad * &dma_buf_attachment from drm_gem_map_attach(). This can be used as the
597 1.1.1.4 riastrad * &dma_buf_ops.detach callback.
598 1.1.1.4 riastrad */
599 1.1.1.4 riastrad void drm_gem_map_detach(struct dma_buf *dma_buf,
600 1.1.1.4 riastrad struct dma_buf_attachment *attach)
601 1.1.1.4 riastrad {
602 1.1.1.4 riastrad struct drm_gem_object *obj = dma_buf->priv;
603 1.1.1.3 riastrad
604 1.1.1.4 riastrad drm_gem_unpin(obj);
605 1.1.1.4 riastrad }
606 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_map_detach);
607 1.1.1.2 riastrad
608 1.1.1.4 riastrad /**
609 1.1.1.4 riastrad * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
610 1.1.1.4 riastrad * @attach: attachment whose scatterlist is to be returned
611 1.1.1.4 riastrad * @dir: direction of DMA transfer
612 1.1.1.4 riastrad *
613 1.1.1.4 riastrad * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This
614 1.1.1.4 riastrad * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together
615 1.1.1.4 riastrad * with drm_gem_unmap_dma_buf().
616 1.1.1.4 riastrad *
617 1.1.1.4 riastrad * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR
618 1.1.1.4 riastrad * on error. May return -EINTR if it is interrupted by a signal.
619 1.1.1.4 riastrad */
620 1.1.1.4 riastrad struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
621 1.1.1.4 riastrad enum dma_data_direction dir)
622 1.1.1.4 riastrad {
623 1.1.1.4 riastrad struct drm_gem_object *obj = attach->dmabuf->priv;
624 1.1.1.4 riastrad struct sg_table *sgt;
625 1.1.1.2 riastrad
626 1.1.1.4 riastrad if (WARN_ON(dir == DMA_NONE))
627 1.1.1.4 riastrad return ERR_PTR(-EINVAL);
628 1.1.1.2 riastrad
629 1.1.1.4 riastrad if (obj->funcs)
630 1.1.1.4 riastrad sgt = obj->funcs->get_sg_table(obj);
631 1.1.1.4 riastrad else
632 1.1.1.4 riastrad sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
633 1.1.1.4 riastrad
634 1.1.1.4 riastrad if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
635 1.1.1.4 riastrad DMA_ATTR_SKIP_CPU_SYNC)) {
636 1.1.1.4 riastrad sg_free_table(sgt);
637 1.1.1.4 riastrad kfree(sgt);
638 1.1.1.4 riastrad sgt = ERR_PTR(-ENOMEM);
639 1.1.1.2 riastrad }
640 1.1.1.2 riastrad
641 1.1.1.4 riastrad return sgt;
642 1.1.1.4 riastrad }
643 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_map_dma_buf);
644 1.1.1.2 riastrad
645 1.1.1.4 riastrad /**
646 1.1.1.4 riastrad * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
647 1.1.1.4 riastrad * @attach: attachment to unmap buffer from
648 1.1.1.4 riastrad * @sgt: scatterlist info of the buffer to unmap
649 1.1.1.4 riastrad * @dir: direction of DMA transfer
650 1.1.1.4 riastrad *
651 1.1.1.4 riastrad * This can be used as the &dma_buf_ops.unmap_dma_buf callback.
652 1.1.1.4 riastrad */
653 1.1.1.4 riastrad void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
654 1.1.1.4 riastrad struct sg_table *sgt,
655 1.1.1.4 riastrad enum dma_data_direction dir)
656 1.1.1.4 riastrad {
657 1.1.1.4 riastrad if (!sgt)
658 1.1.1.4 riastrad return;
659 1.1.1.2 riastrad
660 1.1.1.4 riastrad dma_unmap_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
661 1.1.1.4 riastrad DMA_ATTR_SKIP_CPU_SYNC);
662 1.1.1.4 riastrad sg_free_table(sgt);
663 1.1.1.4 riastrad kfree(sgt);
664 1.1.1.4 riastrad }
665 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
666 1.1.1.2 riastrad
667 1.1.1.4 riastrad /**
668 1.1.1.4 riastrad * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
669 1.1.1.4 riastrad * @dma_buf: buffer to be mapped
670 1.1.1.4 riastrad *
671 1.1.1.4 riastrad * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
672 1.1.1.4 riastrad * callback. Calls into &drm_gem_object_funcs.vmap for device specific handling.
673 1.1.1.4 riastrad *
674 1.1.1.4 riastrad * Returns the kernel virtual address or NULL on failure.
675 1.1.1.4 riastrad */
676 1.1.1.4 riastrad void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
677 1.1.1.4 riastrad {
678 1.1.1.4 riastrad struct drm_gem_object *obj = dma_buf->priv;
679 1.1.1.4 riastrad void *vaddr;
680 1.1.1.4 riastrad
681 1.1.1.4 riastrad vaddr = drm_gem_vmap(obj);
682 1.1.1.4 riastrad if (IS_ERR(vaddr))
683 1.1.1.4 riastrad vaddr = NULL;
684 1.1.1.4 riastrad
685 1.1.1.4 riastrad return vaddr;
686 1.1.1.2 riastrad }
687 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
688 1.1.1.2 riastrad
689 1.1.1.2 riastrad /**
690 1.1.1.4 riastrad * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
691 1.1.1.4 riastrad * @dma_buf: buffer to be unmapped
692 1.1.1.4 riastrad * @vaddr: the virtual address of the buffer
693 1.1.1.2 riastrad *
694 1.1.1.4 riastrad * Releases a kernel virtual mapping. This can be used as the
695 1.1.1.4 riastrad * &dma_buf_ops.vunmap callback. Calls into &drm_gem_object_funcs.vunmap for device specific handling.
696 1.1.1.2 riastrad */
697 1.1.1.4 riastrad void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
698 1.1 riastrad {
699 1.1.1.4 riastrad struct drm_gem_object *obj = dma_buf->priv;
700 1.1 riastrad
701 1.1.1.4 riastrad drm_gem_vunmap(obj, vaddr);
702 1.1.1.4 riastrad }
703 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
704 1.1 riastrad
705 1.1.1.4 riastrad /**
706 1.1.1.4 riastrad * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
707 1.1.1.4 riastrad * @obj: GEM object
708 1.1.1.4 riastrad * @vma: Virtual address range
709 1.1.1.4 riastrad *
710 1.1.1.4 riastrad * This function sets up a userspace mapping for PRIME exported buffers using
711 1.1.1.4 riastrad * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
712 1.1.1.4 riastrad * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
713 1.1.1.4 riastrad * called to set up the mapping.
714 1.1.1.4 riastrad *
715 1.1.1.4 riastrad * Drivers can use this as their &drm_driver.gem_prime_mmap callback.
716 1.1.1.4 riastrad */
717 1.1.1.4 riastrad int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
718 1.1.1.4 riastrad {
719 1.1.1.4 riastrad struct drm_file *priv;
720 1.1.1.4 riastrad struct file *fil;
721 1.1.1.4 riastrad int ret;
722 1.1 riastrad
723 1.1.1.4 riastrad /* Add the fake offset */
724 1.1.1.4 riastrad vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
725 1.1 riastrad
726 1.1.1.4 riastrad if (obj->funcs && obj->funcs->mmap) {
727 1.1.1.4 riastrad ret = obj->funcs->mmap(obj, vma);
728 1.1.1.4 riastrad if (ret)
729 1.1.1.4 riastrad return ret;
730 1.1.1.4 riastrad vma->vm_private_data = obj;
731 1.1.1.4 riastrad drm_gem_object_get(obj);
732 1.1.1.4 riastrad return 0;
733 1.1 riastrad }
734 1.1 riastrad
735 1.1.1.4 riastrad priv = kzalloc(sizeof(*priv), GFP_KERNEL);
736 1.1.1.4 riastrad fil = kzalloc(sizeof(*fil), GFP_KERNEL);
737 1.1.1.4 riastrad if (!priv || !fil) {
738 1.1.1.4 riastrad ret = -ENOMEM;
739 1.1.1.4 riastrad goto out;
740 1.1.1.2 riastrad }
741 1.1.1.2 riastrad
742 1.1.1.4 riastrad /* Used by drm_gem_mmap() to lookup the GEM object */
743 1.1.1.4 riastrad priv->minor = obj->dev->primary;
744 1.1.1.4 riastrad fil->private_data = priv;
745 1.1 riastrad
746 1.1.1.4 riastrad ret = drm_vma_node_allow(&obj->vma_node, priv);
747 1.1 riastrad if (ret)
748 1.1.1.4 riastrad goto out;
749 1.1.1.2 riastrad
750 1.1.1.4 riastrad ret = obj->dev->driver->fops->mmap(fil, vma);
751 1.1.1.2 riastrad
752 1.1.1.4 riastrad drm_vma_node_revoke(&obj->vma_node, priv);
753 1.1.1.4 riastrad out:
754 1.1.1.4 riastrad kfree(priv);
755 1.1.1.4 riastrad kfree(fil);
756 1.1 riastrad
757 1.1 riastrad return ret;
758 1.1 riastrad }
759 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_prime_mmap);
760 1.1 riastrad
761 1.1.1.4 riastrad /**
762 1.1.1.4 riastrad * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
763 1.1.1.4 riastrad * @dma_buf: buffer to be mapped
764 1.1.1.4 riastrad * @vma: virtual address range
765 1.1.1.4 riastrad *
766 1.1.1.4 riastrad * Provides memory mapping for the buffer. This can be used as the
767 1.1.1.4 riastrad * &dma_buf_ops.mmap callback. It just forwards to &drm_driver.gem_prime_mmap,
768 1.1.1.4 riastrad * which should be set to drm_gem_prime_mmap().
769 1.1.1.4 riastrad *
770 1.1.1.4 riastrad * FIXME: There's really no point to this wrapper, drivers which need anything
771 1.1.1.4 riastrad * else but drm_gem_prime_mmap can roll their own &dma_buf_ops.mmap callback.
772 1.1.1.4 riastrad *
773 1.1.1.4 riastrad * Returns 0 on success or a negative error code on failure.
774 1.1.1.4 riastrad */
775 1.1.1.4 riastrad int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
776 1.1 riastrad {
777 1.1.1.4 riastrad struct drm_gem_object *obj = dma_buf->priv;
778 1.1.1.4 riastrad struct drm_device *dev = obj->dev;
779 1.1 riastrad
780 1.1.1.4 riastrad if (!dev->driver->gem_prime_mmap)
781 1.1 riastrad return -ENOSYS;
782 1.1 riastrad
783 1.1.1.4 riastrad return dev->driver->gem_prime_mmap(obj, vma);
784 1.1 riastrad }
785 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
786 1.1 riastrad
787 1.1.1.4 riastrad static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
788 1.1.1.4 riastrad .cache_sgt_mapping = true,
789 1.1.1.4 riastrad .attach = drm_gem_map_attach,
790 1.1.1.4 riastrad .detach = drm_gem_map_detach,
791 1.1.1.4 riastrad .map_dma_buf = drm_gem_map_dma_buf,
792 1.1.1.4 riastrad .unmap_dma_buf = drm_gem_unmap_dma_buf,
793 1.1.1.4 riastrad .release = drm_gem_dmabuf_release,
794 1.1.1.4 riastrad .mmap = drm_gem_dmabuf_mmap,
795 1.1.1.4 riastrad .vmap = drm_gem_dmabuf_vmap,
796 1.1.1.4 riastrad .vunmap = drm_gem_dmabuf_vunmap,
797 1.1.1.4 riastrad };
798 1.1 riastrad
799 1.1.1.2 riastrad /**
800 1.1.1.2 riastrad * drm_prime_pages_to_sg - converts a page array into an sg list
801 1.1.1.2 riastrad * @pages: pointer to the array of page pointers to convert
802 1.1.1.2 riastrad * @nr_pages: length of the page vector
803 1.1 riastrad *
804 1.1.1.2 riastrad * This helper creates an sg table object from a set of pages
805 1.1 riastrad * the driver is responsible for mapping the pages into the
806 1.1.1.2 riastrad * importers address space for use with dma_buf itself.
807 1.1.1.4 riastrad *
808 1.1.1.4 riastrad * This is useful for implementing &drm_gem_object_funcs.get_sg_table.
809 1.1 riastrad */
810 1.1.1.3 riastrad struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
811 1.1 riastrad {
812 1.1 riastrad struct sg_table *sg = NULL;
813 1.1 riastrad int ret;
814 1.1 riastrad
815 1.1 riastrad sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
816 1.1.1.2 riastrad if (!sg) {
817 1.1.1.2 riastrad ret = -ENOMEM;
818 1.1 riastrad goto out;
819 1.1.1.2 riastrad }
820 1.1 riastrad
821 1.1.1.2 riastrad ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
822 1.1.1.2 riastrad nr_pages << PAGE_SHIFT, GFP_KERNEL);
823 1.1 riastrad if (ret)
824 1.1 riastrad goto out;
825 1.1 riastrad
826 1.1 riastrad return sg;
827 1.1 riastrad out:
828 1.1 riastrad kfree(sg);
829 1.1.1.2 riastrad return ERR_PTR(ret);
830 1.1 riastrad }
831 1.1 riastrad EXPORT_SYMBOL(drm_prime_pages_to_sg);
832 1.1 riastrad
833 1.1.1.2 riastrad /**
834 1.1.1.4 riastrad * drm_gem_prime_export - helper library implementation of the export callback
835 1.1.1.4 riastrad * @obj: GEM object to export
836 1.1.1.4 riastrad * @flags: flags like DRM_CLOEXEC and DRM_RDWR
837 1.1.1.4 riastrad *
838 1.1.1.4 riastrad * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers
839 1.1.1.4 riastrad * using the PRIME helpers. It is used as the default in
840 1.1.1.4 riastrad * drm_gem_prime_handle_to_fd().
841 1.1.1.4 riastrad */
842 1.1.1.4 riastrad struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj,
843 1.1.1.4 riastrad int flags)
844 1.1.1.4 riastrad {
845 1.1.1.4 riastrad struct drm_device *dev = obj->dev;
846 1.1.1.4 riastrad struct dma_buf_export_info exp_info = {
847 1.1.1.4 riastrad .exp_name = KBUILD_MODNAME, /* white lie for debug */
848 1.1.1.4 riastrad .owner = dev->driver->fops->owner,
849 1.1.1.4 riastrad .ops = &drm_gem_prime_dmabuf_ops,
850 1.1.1.4 riastrad .size = obj->size,
851 1.1.1.4 riastrad .flags = flags,
852 1.1.1.4 riastrad .priv = obj,
853 1.1.1.4 riastrad .resv = obj->resv,
854 1.1.1.4 riastrad };
855 1.1.1.4 riastrad
856 1.1.1.4 riastrad return drm_gem_dmabuf_export(dev, &exp_info);
857 1.1.1.4 riastrad }
858 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_prime_export);
859 1.1.1.4 riastrad
860 1.1.1.4 riastrad /**
861 1.1.1.4 riastrad * drm_gem_prime_import_dev - core implementation of the import callback
862 1.1.1.4 riastrad * @dev: drm_device to import into
863 1.1.1.4 riastrad * @dma_buf: dma-buf object to import
864 1.1.1.4 riastrad * @attach_dev: struct device to dma_buf attach
865 1.1.1.4 riastrad *
866 1.1.1.4 riastrad * This is the core of drm_gem_prime_import(). It's designed to be called by
867 1.1.1.4 riastrad * drivers who want to use a different device structure than &drm_device.dev for
868 1.1.1.4 riastrad * attaching via dma_buf. This function calls
869 1.1.1.4 riastrad * &drm_driver.gem_prime_import_sg_table internally.
870 1.1.1.4 riastrad *
871 1.1.1.4 riastrad * Drivers must arrange to call drm_prime_gem_destroy() from their
872 1.1.1.4 riastrad * &drm_gem_object_funcs.free hook when using this function.
873 1.1.1.4 riastrad */
874 1.1.1.4 riastrad struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
875 1.1.1.4 riastrad struct dma_buf *dma_buf,
876 1.1.1.4 riastrad struct device *attach_dev)
877 1.1.1.4 riastrad {
878 1.1.1.4 riastrad struct dma_buf_attachment *attach;
879 1.1.1.4 riastrad struct sg_table *sgt;
880 1.1.1.4 riastrad struct drm_gem_object *obj;
881 1.1.1.4 riastrad int ret;
882 1.1.1.4 riastrad
883 1.1.1.4 riastrad if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
884 1.1.1.4 riastrad obj = dma_buf->priv;
885 1.1.1.4 riastrad if (obj->dev == dev) {
886 1.1.1.4 riastrad /*
887 1.1.1.4 riastrad * Importing dmabuf exported from out own gem increases
888 1.1.1.4 riastrad * refcount on gem itself instead of f_count of dmabuf.
889 1.1.1.4 riastrad */
890 1.1.1.4 riastrad drm_gem_object_get(obj);
891 1.1.1.4 riastrad return obj;
892 1.1.1.4 riastrad }
893 1.1.1.4 riastrad }
894 1.1.1.4 riastrad
895 1.1.1.4 riastrad if (!dev->driver->gem_prime_import_sg_table)
896 1.1.1.4 riastrad return ERR_PTR(-EINVAL);
897 1.1.1.4 riastrad
898 1.1.1.4 riastrad attach = dma_buf_attach(dma_buf, attach_dev);
899 1.1.1.4 riastrad if (IS_ERR(attach))
900 1.1.1.4 riastrad return ERR_CAST(attach);
901 1.1.1.4 riastrad
902 1.1.1.4 riastrad get_dma_buf(dma_buf);
903 1.1.1.4 riastrad
904 1.1.1.4 riastrad sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
905 1.1.1.4 riastrad if (IS_ERR(sgt)) {
906 1.1.1.4 riastrad ret = PTR_ERR(sgt);
907 1.1.1.4 riastrad goto fail_detach;
908 1.1.1.4 riastrad }
909 1.1.1.4 riastrad
910 1.1.1.4 riastrad obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
911 1.1.1.4 riastrad if (IS_ERR(obj)) {
912 1.1.1.4 riastrad ret = PTR_ERR(obj);
913 1.1.1.4 riastrad goto fail_unmap;
914 1.1.1.4 riastrad }
915 1.1.1.4 riastrad
916 1.1.1.4 riastrad obj->import_attach = attach;
917 1.1.1.4 riastrad obj->resv = dma_buf->resv;
918 1.1.1.4 riastrad
919 1.1.1.4 riastrad return obj;
920 1.1.1.4 riastrad
921 1.1.1.4 riastrad fail_unmap:
922 1.1.1.4 riastrad dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
923 1.1.1.4 riastrad fail_detach:
924 1.1.1.4 riastrad dma_buf_detach(dma_buf, attach);
925 1.1.1.4 riastrad dma_buf_put(dma_buf);
926 1.1.1.4 riastrad
927 1.1.1.4 riastrad return ERR_PTR(ret);
928 1.1.1.4 riastrad }
929 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_prime_import_dev);
930 1.1.1.4 riastrad
931 1.1.1.4 riastrad /**
932 1.1.1.4 riastrad * drm_gem_prime_import - helper library implementation of the import callback
933 1.1.1.4 riastrad * @dev: drm_device to import into
934 1.1.1.4 riastrad * @dma_buf: dma-buf object to import
935 1.1.1.4 riastrad *
936 1.1.1.4 riastrad * This is the implementation of the gem_prime_import functions for GEM drivers
937 1.1.1.4 riastrad * using the PRIME helpers. Drivers can use this as their
938 1.1.1.4 riastrad * &drm_driver.gem_prime_import implementation. It is used as the default
939 1.1.1.4 riastrad * implementation in drm_gem_prime_fd_to_handle().
940 1.1.1.4 riastrad *
941 1.1.1.4 riastrad * Drivers must arrange to call drm_prime_gem_destroy() from their
942 1.1.1.4 riastrad * &drm_gem_object_funcs.free hook when using this function.
943 1.1.1.4 riastrad */
944 1.1.1.4 riastrad struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
945 1.1.1.4 riastrad struct dma_buf *dma_buf)
946 1.1.1.4 riastrad {
947 1.1.1.4 riastrad return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
948 1.1.1.4 riastrad }
949 1.1.1.4 riastrad EXPORT_SYMBOL(drm_gem_prime_import);
950 1.1.1.4 riastrad
951 1.1.1.4 riastrad /**
952 1.1.1.2 riastrad * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
953 1.1.1.2 riastrad * @sgt: scatter-gather table to convert
954 1.1.1.4 riastrad * @pages: optional array of page pointers to store the page array in
955 1.1.1.2 riastrad * @addrs: optional array to store the dma bus address of each page
956 1.1.1.4 riastrad * @max_entries: size of both the passed-in arrays
957 1.1.1.2 riastrad *
958 1.1.1.2 riastrad * Exports an sg table into an array of pages and addresses. This is currently
959 1.1.1.2 riastrad * required by the TTM driver in order to do correct fault handling.
960 1.1.1.4 riastrad *
961 1.1.1.4 riastrad * Drivers can use this in their &drm_driver.gem_prime_import_sg_table
962 1.1.1.4 riastrad * implementation.
963 1.1.1.2 riastrad */
964 1.1 riastrad int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
965 1.1.1.4 riastrad dma_addr_t *addrs, int max_entries)
966 1.1 riastrad {
967 1.1 riastrad unsigned count;
968 1.1 riastrad struct scatterlist *sg;
969 1.1 riastrad struct page *page;
970 1.1.1.4 riastrad u32 len, index;
971 1.1 riastrad dma_addr_t addr;
972 1.1 riastrad
973 1.1.1.4 riastrad index = 0;
974 1.1 riastrad for_each_sg(sgt->sgl, sg, sgt->nents, count) {
975 1.1 riastrad len = sg->length;
976 1.1 riastrad page = sg_page(sg);
977 1.1 riastrad addr = sg_dma_address(sg);
978 1.1 riastrad
979 1.1 riastrad while (len > 0) {
980 1.1.1.4 riastrad if (WARN_ON(index >= max_entries))
981 1.1 riastrad return -1;
982 1.1.1.4 riastrad if (pages)
983 1.1.1.4 riastrad pages[index] = page;
984 1.1 riastrad if (addrs)
985 1.1.1.4 riastrad addrs[index] = addr;
986 1.1 riastrad
987 1.1 riastrad page++;
988 1.1 riastrad addr += PAGE_SIZE;
989 1.1 riastrad len -= PAGE_SIZE;
990 1.1.1.4 riastrad index++;
991 1.1 riastrad }
992 1.1 riastrad }
993 1.1 riastrad return 0;
994 1.1 riastrad }
995 1.1 riastrad EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
996 1.1.1.2 riastrad
997 1.1.1.2 riastrad /**
998 1.1.1.2 riastrad * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
999 1.1.1.2 riastrad * @obj: GEM object which was created from a dma-buf
1000 1.1.1.2 riastrad * @sg: the sg-table which was pinned at import time
1001 1.1.1.2 riastrad *
1002 1.1.1.2 riastrad * This is the cleanup functions which GEM drivers need to call when they use
1003 1.1.1.4 riastrad * drm_gem_prime_import() or drm_gem_prime_import_dev() to import dma-bufs.
1004 1.1.1.2 riastrad */
1005 1.1 riastrad void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
1006 1.1 riastrad {
1007 1.1 riastrad struct dma_buf_attachment *attach;
1008 1.1 riastrad struct dma_buf *dma_buf;
1009 1.1 riastrad attach = obj->import_attach;
1010 1.1 riastrad if (sg)
1011 1.1 riastrad dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
1012 1.1 riastrad dma_buf = attach->dmabuf;
1013 1.1 riastrad dma_buf_detach(attach->dmabuf, attach);
1014 1.1 riastrad /* remove the reference */
1015 1.1 riastrad dma_buf_put(dma_buf);
1016 1.1 riastrad }
1017 1.1 riastrad EXPORT_SYMBOL(drm_prime_gem_destroy);
1018