drm_prime.c revision 1.18 1 1.18 riastrad /* $NetBSD: drm_prime.c,v 1.18 2021/12/19 11:36:25 riastradh Exp $ */
2 1.2 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.2 riastrad #include <sys/cdefs.h>
32 1.18 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_prime.c,v 1.18 2021/12/19 11:36:25 riastradh Exp $");
33 1.2 riastrad
34 1.1 riastrad #include <linux/export.h>
35 1.1 riastrad #include <linux/dma-buf.h>
36 1.10 riastrad #include <linux/rbtree.h>
37 1.10 riastrad
38 1.10 riastrad #include <drm/drm.h>
39 1.10 riastrad #include <drm/drm_drv.h>
40 1.10 riastrad #include <drm/drm_file.h>
41 1.10 riastrad #include <drm/drm_framebuffer.h>
42 1.2 riastrad #include <drm/drm_gem.h>
43 1.10 riastrad #include <drm/drm_prime.h>
44 1.2 riastrad
45 1.2 riastrad #include "drm_internal.h"
46 1.1 riastrad
47 1.4 riastrad #ifdef __NetBSD__
48 1.4 riastrad
49 1.14 riastrad #include <sys/file.h>
50 1.14 riastrad
51 1.5 riastrad #include <drm/bus_dma_hacks.h>
52 1.5 riastrad
53 1.8 riastrad #include <linux/nbsd-namespace.h>
54 1.8 riastrad
55 1.4 riastrad #endif /* __NetBSD__ */
56 1.4 riastrad
57 1.10 riastrad /**
58 1.10 riastrad * DOC: overview and lifetime rules
59 1.1 riastrad *
60 1.10 riastrad * Similar to GEM global names, PRIME file descriptors are also used to share
61 1.10 riastrad * buffer objects across processes. They offer additional security: as file
62 1.10 riastrad * descriptors must be explicitly sent over UNIX domain sockets to be shared
63 1.10 riastrad * between applications, they can't be guessed like the globally unique GEM
64 1.10 riastrad * names.
65 1.10 riastrad *
66 1.10 riastrad * Drivers that support the PRIME API implement the
67 1.10 riastrad * &drm_driver.prime_handle_to_fd and &drm_driver.prime_fd_to_handle operations.
68 1.10 riastrad * GEM based drivers must use drm_gem_prime_handle_to_fd() and
69 1.10 riastrad * drm_gem_prime_fd_to_handle() to implement these. For GEM based drivers the
70 1.10 riastrad * actual driver interfaces is provided through the &drm_gem_object_funcs.export
71 1.10 riastrad * and &drm_driver.gem_prime_import hooks.
72 1.10 riastrad *
73 1.10 riastrad * &dma_buf_ops implementations for GEM drivers are all individually exported
74 1.10 riastrad * for drivers which need to overwrite or reimplement some of them.
75 1.10 riastrad *
76 1.10 riastrad * Reference Counting for GEM Drivers
77 1.10 riastrad * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78 1.10 riastrad *
79 1.10 riastrad * On the export the &dma_buf holds a reference to the exported buffer object,
80 1.10 riastrad * usually a &drm_gem_object. It takes this reference in the PRIME_HANDLE_TO_FD
81 1.10 riastrad * IOCTL, when it first calls &drm_gem_object_funcs.export
82 1.10 riastrad * and stores the exporting GEM object in the &dma_buf.priv field. This
83 1.10 riastrad * reference needs to be released when the final reference to the &dma_buf
84 1.10 riastrad * itself is dropped and its &dma_buf_ops.release function is called. For
85 1.10 riastrad * GEM-based drivers, the &dma_buf should be exported using
86 1.10 riastrad * drm_gem_dmabuf_export() and then released by drm_gem_dmabuf_release().
87 1.10 riastrad *
88 1.10 riastrad * Thus the chain of references always flows in one direction, avoiding loops:
89 1.10 riastrad * importing GEM object -> dma-buf -> exported GEM bo. A further complication
90 1.10 riastrad * are the lookup caches for import and export. These are required to guarantee
91 1.10 riastrad * that any given object will always have only one uniqe userspace handle. This
92 1.10 riastrad * is required to allow userspace to detect duplicated imports, since some GEM
93 1.10 riastrad * drivers do fail command submissions if a given buffer object is listed more
94 1.10 riastrad * than once. These import and export caches in &drm_prime_file_private only
95 1.10 riastrad * retain a weak reference, which is cleaned up when the corresponding object is
96 1.10 riastrad * released.
97 1.10 riastrad *
98 1.10 riastrad * Self-importing: If userspace is using PRIME as a replacement for flink then
99 1.10 riastrad * it will get a fd->handle request for a GEM object that it created. Drivers
100 1.10 riastrad * should detect this situation and return back the underlying object from the
101 1.10 riastrad * dma-buf private. For GEM based drivers this is handled in
102 1.10 riastrad * drm_gem_prime_import() already.
103 1.1 riastrad */
104 1.1 riastrad
105 1.1 riastrad struct drm_prime_member {
106 1.1 riastrad struct dma_buf *dma_buf;
107 1.1 riastrad uint32_t handle;
108 1.1 riastrad
109 1.10 riastrad struct rb_node dmabuf_rb;
110 1.10 riastrad struct rb_node handle_rb;
111 1.2 riastrad };
112 1.2 riastrad
113 1.11 riastrad #ifdef __NetBSD__
114 1.11 riastrad static int
115 1.11 riastrad compare_dmabufs(void *cookie, const void *va, const void *vb)
116 1.11 riastrad {
117 1.11 riastrad const struct drm_prime_member *ma = va;
118 1.11 riastrad const struct drm_prime_member *mb = vb;
119 1.11 riastrad
120 1.11 riastrad if (ma->dma_buf < mb->dma_buf)
121 1.11 riastrad return -1;
122 1.11 riastrad if (ma->dma_buf > mb->dma_buf)
123 1.11 riastrad return +1;
124 1.11 riastrad return 0;
125 1.11 riastrad }
126 1.11 riastrad
127 1.11 riastrad static int
128 1.11 riastrad compare_dmabuf_key(void *cookie, const void *vm, const void *vk)
129 1.11 riastrad {
130 1.11 riastrad const struct drm_prime_member *m = vm;
131 1.11 riastrad const struct dma_buf *const *kp = vk;
132 1.11 riastrad
133 1.11 riastrad if (m->dma_buf < *kp)
134 1.11 riastrad return -1;
135 1.11 riastrad if (m->dma_buf > *kp)
136 1.11 riastrad return +1;
137 1.11 riastrad return 0;
138 1.11 riastrad }
139 1.11 riastrad
140 1.11 riastrad static int
141 1.11 riastrad compare_handles(void *cookie, const void *va, const void *vb)
142 1.11 riastrad {
143 1.11 riastrad const struct drm_prime_member *ma = va;
144 1.11 riastrad const struct drm_prime_member *mb = vb;
145 1.11 riastrad
146 1.11 riastrad if (ma->handle < mb->handle)
147 1.11 riastrad return -1;
148 1.11 riastrad if (ma->handle > mb->handle)
149 1.11 riastrad return +1;
150 1.11 riastrad return 0;
151 1.11 riastrad }
152 1.11 riastrad
153 1.11 riastrad static int
154 1.11 riastrad compare_handle_key(void *cookie, const void *vm, const void *vk)
155 1.11 riastrad {
156 1.11 riastrad const struct drm_prime_member *m = vm;
157 1.11 riastrad const uint32_t *kp = vk;
158 1.11 riastrad
159 1.11 riastrad if (m->handle < *kp)
160 1.11 riastrad return -1;
161 1.11 riastrad if (m->handle > *kp)
162 1.11 riastrad return +1;
163 1.11 riastrad return 0;
164 1.11 riastrad }
165 1.11 riastrad
166 1.11 riastrad static const rb_tree_ops_t dmabuf_ops = {
167 1.11 riastrad .rbto_compare_nodes = compare_dmabufs,
168 1.11 riastrad .rbto_compare_key = compare_dmabuf_key,
169 1.11 riastrad .rbto_node_offset = offsetof(struct drm_prime_member, dmabuf_rb),
170 1.11 riastrad };
171 1.11 riastrad
172 1.11 riastrad static const rb_tree_ops_t handle_ops = {
173 1.11 riastrad .rbto_compare_nodes = compare_handles,
174 1.11 riastrad .rbto_compare_key = compare_handle_key,
175 1.11 riastrad .rbto_node_offset = offsetof(struct drm_prime_member, handle_rb),
176 1.11 riastrad };
177 1.11 riastrad #endif
178 1.11 riastrad
179 1.2 riastrad static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
180 1.2 riastrad struct dma_buf *dma_buf, uint32_t handle)
181 1.2 riastrad {
182 1.2 riastrad struct drm_prime_member *member;
183 1.11 riastrad #ifdef __NetBSD__
184 1.11 riastrad struct drm_prime_member *collision __diagused;
185 1.11 riastrad #else
186 1.10 riastrad struct rb_node **p, *rb;
187 1.11 riastrad #endif
188 1.2 riastrad
189 1.2 riastrad member = kmalloc(sizeof(*member), GFP_KERNEL);
190 1.2 riastrad if (!member)
191 1.2 riastrad return -ENOMEM;
192 1.2 riastrad
193 1.2 riastrad get_dma_buf(dma_buf);
194 1.2 riastrad member->dma_buf = dma_buf;
195 1.2 riastrad member->handle = handle;
196 1.10 riastrad
197 1.11 riastrad #ifdef __NetBSD__
198 1.11 riastrad collision = rb_tree_insert_node(&prime_fpriv->dmabufs.rbr_tree,
199 1.11 riastrad member);
200 1.17 riastrad KASSERT(collision == member);
201 1.11 riastrad #else
202 1.10 riastrad rb = NULL;
203 1.10 riastrad p = &prime_fpriv->dmabufs.rb_node;
204 1.10 riastrad while (*p) {
205 1.10 riastrad struct drm_prime_member *pos;
206 1.10 riastrad
207 1.10 riastrad rb = *p;
208 1.10 riastrad pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
209 1.10 riastrad if (dma_buf > pos->dma_buf)
210 1.10 riastrad p = &rb->rb_right;
211 1.10 riastrad else
212 1.10 riastrad p = &rb->rb_left;
213 1.10 riastrad }
214 1.10 riastrad rb_link_node(&member->dmabuf_rb, rb, p);
215 1.10 riastrad rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
216 1.11 riastrad #endif
217 1.10 riastrad
218 1.11 riastrad #ifdef __NetBSD__
219 1.11 riastrad collision = rb_tree_insert_node(&prime_fpriv->handles.rbr_tree,
220 1.11 riastrad member);
221 1.17 riastrad KASSERT(collision == member);
222 1.11 riastrad #else
223 1.10 riastrad rb = NULL;
224 1.10 riastrad p = &prime_fpriv->handles.rb_node;
225 1.10 riastrad while (*p) {
226 1.10 riastrad struct drm_prime_member *pos;
227 1.10 riastrad
228 1.10 riastrad rb = *p;
229 1.10 riastrad pos = rb_entry(rb, struct drm_prime_member, handle_rb);
230 1.10 riastrad if (handle > pos->handle)
231 1.10 riastrad p = &rb->rb_right;
232 1.10 riastrad else
233 1.10 riastrad p = &rb->rb_left;
234 1.10 riastrad }
235 1.10 riastrad rb_link_node(&member->handle_rb, rb, p);
236 1.10 riastrad rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
237 1.11 riastrad #endif
238 1.10 riastrad
239 1.2 riastrad return 0;
240 1.2 riastrad }
241 1.2 riastrad
242 1.2 riastrad static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
243 1.2 riastrad uint32_t handle)
244 1.2 riastrad {
245 1.11 riastrad #ifdef __NetBSD__
246 1.11 riastrad return rb_tree_find_node(&prime_fpriv->handles.rbr_tree, &handle);
247 1.11 riastrad #else
248 1.10 riastrad struct rb_node *rb;
249 1.10 riastrad
250 1.10 riastrad rb = prime_fpriv->handles.rb_node;
251 1.10 riastrad while (rb) {
252 1.10 riastrad struct drm_prime_member *member;
253 1.2 riastrad
254 1.10 riastrad member = rb_entry(rb, struct drm_prime_member, handle_rb);
255 1.2 riastrad if (member->handle == handle)
256 1.2 riastrad return member->dma_buf;
257 1.10 riastrad else if (member->handle < handle)
258 1.10 riastrad rb = rb->rb_right;
259 1.10 riastrad else
260 1.10 riastrad rb = rb->rb_left;
261 1.2 riastrad }
262 1.2 riastrad
263 1.2 riastrad return NULL;
264 1.11 riastrad #endif
265 1.2 riastrad }
266 1.2 riastrad
267 1.2 riastrad static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
268 1.2 riastrad struct dma_buf *dma_buf,
269 1.2 riastrad uint32_t *handle)
270 1.2 riastrad {
271 1.11 riastrad #ifdef __NetBSD__
272 1.11 riastrad struct drm_prime_member *member;
273 1.11 riastrad
274 1.11 riastrad member = rb_tree_find_node(&prime_fpriv->dmabufs.rbr_tree, &dma_buf);
275 1.11 riastrad if (member == NULL)
276 1.11 riastrad return -ENOENT;
277 1.11 riastrad *handle = member->handle;
278 1.11 riastrad return 0;
279 1.11 riastrad #else
280 1.10 riastrad struct rb_node *rb;
281 1.10 riastrad
282 1.10 riastrad rb = prime_fpriv->dmabufs.rb_node;
283 1.10 riastrad while (rb) {
284 1.10 riastrad struct drm_prime_member *member;
285 1.2 riastrad
286 1.10 riastrad member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
287 1.2 riastrad if (member->dma_buf == dma_buf) {
288 1.2 riastrad *handle = member->handle;
289 1.2 riastrad return 0;
290 1.10 riastrad } else if (member->dma_buf < dma_buf) {
291 1.10 riastrad rb = rb->rb_right;
292 1.10 riastrad } else {
293 1.10 riastrad rb = rb->rb_left;
294 1.2 riastrad }
295 1.2 riastrad }
296 1.10 riastrad
297 1.2 riastrad return -ENOENT;
298 1.11 riastrad #endif
299 1.2 riastrad }
300 1.2 riastrad
301 1.2 riastrad void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
302 1.2 riastrad struct dma_buf *dma_buf)
303 1.2 riastrad {
304 1.11 riastrad #ifdef __NetBSD__
305 1.11 riastrad struct drm_prime_member *member;
306 1.11 riastrad
307 1.11 riastrad member = rb_tree_find_node(&prime_fpriv->dmabufs.rbr_tree, &dma_buf);
308 1.11 riastrad if (member != NULL) {
309 1.11 riastrad rb_tree_remove_node(&prime_fpriv->handles.rbr_tree, member);
310 1.11 riastrad rb_tree_remove_node(&prime_fpriv->dmabufs.rbr_tree, member);
311 1.18 riastrad dma_buf_put(dma_buf);
312 1.18 riastrad kfree(member);
313 1.11 riastrad }
314 1.11 riastrad #else
315 1.10 riastrad struct rb_node *rb;
316 1.2 riastrad
317 1.10 riastrad rb = prime_fpriv->dmabufs.rb_node;
318 1.10 riastrad while (rb) {
319 1.10 riastrad struct drm_prime_member *member;
320 1.10 riastrad
321 1.10 riastrad member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
322 1.2 riastrad if (member->dma_buf == dma_buf) {
323 1.10 riastrad rb_erase(&member->handle_rb, &prime_fpriv->handles);
324 1.10 riastrad rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
325 1.10 riastrad
326 1.2 riastrad dma_buf_put(dma_buf);
327 1.2 riastrad kfree(member);
328 1.10 riastrad return;
329 1.10 riastrad } else if (member->dma_buf < dma_buf) {
330 1.10 riastrad rb = rb->rb_right;
331 1.10 riastrad } else {
332 1.10 riastrad rb = rb->rb_left;
333 1.2 riastrad }
334 1.2 riastrad }
335 1.11 riastrad #endif
336 1.2 riastrad }
337 1.2 riastrad
338 1.10 riastrad void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
339 1.2 riastrad {
340 1.10 riastrad mutex_init(&prime_fpriv->lock);
341 1.11 riastrad #ifdef __NetBSD__
342 1.11 riastrad rb_tree_init(&prime_fpriv->dmabufs.rbr_tree, &dmabuf_ops);
343 1.11 riastrad rb_tree_init(&prime_fpriv->handles.rbr_tree, &handle_ops);
344 1.11 riastrad #else
345 1.10 riastrad prime_fpriv->dmabufs = RB_ROOT;
346 1.10 riastrad prime_fpriv->handles = RB_ROOT;
347 1.11 riastrad #endif
348 1.10 riastrad }
349 1.2 riastrad
350 1.10 riastrad void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
351 1.10 riastrad {
352 1.18 riastrad mutex_destroy(&prime_fpriv->lock);
353 1.10 riastrad /* by now drm_gem_release should've made sure the list is empty */
354 1.10 riastrad WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
355 1.18 riastrad WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->handles));
356 1.10 riastrad }
357 1.2 riastrad
358 1.10 riastrad /**
359 1.10 riastrad * drm_gem_dmabuf_export - &dma_buf export implementation for GEM
360 1.10 riastrad * @dev: parent device for the exported dmabuf
361 1.10 riastrad * @exp_info: the export information used by dma_buf_export()
362 1.10 riastrad *
363 1.10 riastrad * This wraps dma_buf_export() for use by generic GEM drivers that are using
364 1.10 riastrad * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
365 1.10 riastrad * a reference to the &drm_device and the exported &drm_gem_object (stored in
366 1.10 riastrad * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
367 1.10 riastrad *
368 1.10 riastrad * Returns the new dmabuf.
369 1.10 riastrad */
370 1.10 riastrad struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
371 1.10 riastrad struct dma_buf_export_info *exp_info)
372 1.10 riastrad {
373 1.10 riastrad struct drm_gem_object *obj = exp_info->priv;
374 1.10 riastrad struct dma_buf *dma_buf;
375 1.2 riastrad
376 1.10 riastrad dma_buf = dma_buf_export(exp_info);
377 1.10 riastrad if (IS_ERR(dma_buf))
378 1.10 riastrad return dma_buf;
379 1.2 riastrad
380 1.10 riastrad drm_dev_get(dev);
381 1.10 riastrad drm_gem_object_get(obj);
382 1.14 riastrad #ifndef __NetBSD__ /* XXX dmabuf share */
383 1.10 riastrad dma_buf->file->f_mapping = obj->dev->anon_inode->i_mapping;
384 1.14 riastrad #endif
385 1.2 riastrad
386 1.10 riastrad return dma_buf;
387 1.2 riastrad }
388 1.10 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_export);
389 1.2 riastrad
390 1.2 riastrad /**
391 1.10 riastrad * drm_gem_dmabuf_release - &dma_buf release implementation for GEM
392 1.2 riastrad * @dma_buf: buffer to be released
393 1.2 riastrad *
394 1.2 riastrad * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
395 1.10 riastrad * must use this in their &dma_buf_ops structure as the release callback.
396 1.10 riastrad * drm_gem_dmabuf_release() should be used in conjunction with
397 1.10 riastrad * drm_gem_dmabuf_export().
398 1.2 riastrad */
399 1.2 riastrad void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
400 1.2 riastrad {
401 1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
402 1.10 riastrad struct drm_device *dev = obj->dev;
403 1.2 riastrad
404 1.2 riastrad /* drop the reference on the export fd holds */
405 1.10 riastrad drm_gem_object_put_unlocked(obj);
406 1.10 riastrad
407 1.10 riastrad drm_dev_put(dev);
408 1.2 riastrad }
409 1.2 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_release);
410 1.2 riastrad
411 1.10 riastrad /**
412 1.10 riastrad * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
413 1.10 riastrad * @dev: dev to export the buffer from
414 1.10 riastrad * @file_priv: drm file-private structure
415 1.10 riastrad * @prime_fd: fd id of the dma-buf which should be imported
416 1.10 riastrad * @handle: pointer to storage for the handle of the imported buffer object
417 1.10 riastrad *
418 1.10 riastrad * This is the PRIME import function which must be used mandatorily by GEM
419 1.10 riastrad * drivers to ensure correct lifetime management of the underlying GEM object.
420 1.10 riastrad * The actual importing of GEM object from the dma-buf is done through the
421 1.10 riastrad * &drm_driver.gem_prime_import driver callback.
422 1.10 riastrad *
423 1.10 riastrad * Returns 0 on success or a negative error code on failure.
424 1.10 riastrad */
425 1.10 riastrad int drm_gem_prime_fd_to_handle(struct drm_device *dev,
426 1.10 riastrad struct drm_file *file_priv, int prime_fd,
427 1.10 riastrad uint32_t *handle)
428 1.2 riastrad {
429 1.10 riastrad struct dma_buf *dma_buf;
430 1.10 riastrad struct drm_gem_object *obj;
431 1.10 riastrad int ret;
432 1.2 riastrad
433 1.10 riastrad dma_buf = dma_buf_get(prime_fd);
434 1.10 riastrad if (IS_ERR(dma_buf))
435 1.10 riastrad return PTR_ERR(dma_buf);
436 1.2 riastrad
437 1.10 riastrad mutex_lock(&file_priv->prime.lock);
438 1.2 riastrad
439 1.10 riastrad ret = drm_prime_lookup_buf_handle(&file_priv->prime,
440 1.10 riastrad dma_buf, handle);
441 1.10 riastrad if (ret == 0)
442 1.10 riastrad goto out_put;
443 1.2 riastrad
444 1.10 riastrad /* never seen this one, need to import */
445 1.10 riastrad mutex_lock(&dev->object_name_lock);
446 1.10 riastrad if (dev->driver->gem_prime_import)
447 1.10 riastrad obj = dev->driver->gem_prime_import(dev, dma_buf);
448 1.10 riastrad else
449 1.10 riastrad obj = drm_gem_prime_import(dev, dma_buf);
450 1.10 riastrad if (IS_ERR(obj)) {
451 1.10 riastrad ret = PTR_ERR(obj);
452 1.10 riastrad goto out_unlock;
453 1.10 riastrad }
454 1.2 riastrad
455 1.10 riastrad if (obj->dma_buf) {
456 1.10 riastrad WARN_ON(obj->dma_buf != dma_buf);
457 1.10 riastrad } else {
458 1.10 riastrad obj->dma_buf = dma_buf;
459 1.10 riastrad get_dma_buf(dma_buf);
460 1.10 riastrad }
461 1.2 riastrad
462 1.10 riastrad /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
463 1.10 riastrad ret = drm_gem_handle_create_tail(file_priv, obj, handle);
464 1.10 riastrad drm_gem_object_put_unlocked(obj);
465 1.10 riastrad if (ret)
466 1.10 riastrad goto out_put;
467 1.2 riastrad
468 1.10 riastrad ret = drm_prime_add_buf_handle(&file_priv->prime,
469 1.10 riastrad dma_buf, *handle);
470 1.10 riastrad mutex_unlock(&file_priv->prime.lock);
471 1.10 riastrad if (ret)
472 1.10 riastrad goto fail;
473 1.2 riastrad
474 1.10 riastrad dma_buf_put(dma_buf);
475 1.2 riastrad
476 1.10 riastrad return 0;
477 1.2 riastrad
478 1.10 riastrad fail:
479 1.10 riastrad /* hmm, if driver attached, we are relying on the free-object path
480 1.10 riastrad * to detach.. which seems ok..
481 1.10 riastrad */
482 1.10 riastrad drm_gem_handle_delete(file_priv, *handle);
483 1.10 riastrad dma_buf_put(dma_buf);
484 1.10 riastrad return ret;
485 1.2 riastrad
486 1.10 riastrad out_unlock:
487 1.10 riastrad mutex_unlock(&dev->object_name_lock);
488 1.10 riastrad out_put:
489 1.10 riastrad mutex_unlock(&file_priv->prime.lock);
490 1.10 riastrad dma_buf_put(dma_buf);
491 1.10 riastrad return ret;
492 1.2 riastrad }
493 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
494 1.2 riastrad
495 1.10 riastrad int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
496 1.10 riastrad struct drm_file *file_priv)
497 1.2 riastrad {
498 1.10 riastrad struct drm_prime_handle *args = data;
499 1.2 riastrad
500 1.10 riastrad if (!dev->driver->prime_fd_to_handle)
501 1.10 riastrad return -ENOSYS;
502 1.2 riastrad
503 1.10 riastrad return dev->driver->prime_fd_to_handle(dev, file_priv,
504 1.10 riastrad args->fd, &args->handle);
505 1.2 riastrad }
506 1.2 riastrad
507 1.2 riastrad static struct dma_buf *export_and_register_object(struct drm_device *dev,
508 1.2 riastrad struct drm_gem_object *obj,
509 1.2 riastrad uint32_t flags)
510 1.2 riastrad {
511 1.2 riastrad struct dma_buf *dmabuf;
512 1.2 riastrad
513 1.2 riastrad /* prevent races with concurrent gem_close. */
514 1.2 riastrad if (obj->handle_count == 0) {
515 1.2 riastrad dmabuf = ERR_PTR(-ENOENT);
516 1.2 riastrad return dmabuf;
517 1.2 riastrad }
518 1.2 riastrad
519 1.10 riastrad if (obj->funcs && obj->funcs->export)
520 1.10 riastrad dmabuf = obj->funcs->export(obj, flags);
521 1.10 riastrad else if (dev->driver->gem_prime_export)
522 1.10 riastrad dmabuf = dev->driver->gem_prime_export(obj, flags);
523 1.10 riastrad else
524 1.10 riastrad dmabuf = drm_gem_prime_export(obj, flags);
525 1.2 riastrad if (IS_ERR(dmabuf)) {
526 1.2 riastrad /* normally the created dma-buf takes ownership of the ref,
527 1.2 riastrad * but if that fails then drop the ref
528 1.2 riastrad */
529 1.2 riastrad return dmabuf;
530 1.2 riastrad }
531 1.2 riastrad
532 1.2 riastrad /*
533 1.2 riastrad * Note that callers do not need to clean up the export cache
534 1.2 riastrad * since the check for obj->handle_count guarantees that someone
535 1.2 riastrad * will clean it up.
536 1.2 riastrad */
537 1.2 riastrad obj->dma_buf = dmabuf;
538 1.2 riastrad get_dma_buf(obj->dma_buf);
539 1.2 riastrad
540 1.2 riastrad return dmabuf;
541 1.2 riastrad }
542 1.2 riastrad
543 1.2 riastrad /**
544 1.2 riastrad * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
545 1.2 riastrad * @dev: dev to export the buffer from
546 1.2 riastrad * @file_priv: drm file-private structure
547 1.2 riastrad * @handle: buffer handle to export
548 1.2 riastrad * @flags: flags like DRM_CLOEXEC
549 1.2 riastrad * @prime_fd: pointer to storage for the fd id of the create dma-buf
550 1.2 riastrad *
551 1.2 riastrad * This is the PRIME export function which must be used mandatorily by GEM
552 1.2 riastrad * drivers to ensure correct lifetime management of the underlying GEM object.
553 1.2 riastrad * The actual exporting from GEM object to a dma-buf is done through the
554 1.10 riastrad * &drm_driver.gem_prime_export driver callback.
555 1.2 riastrad */
556 1.1 riastrad int drm_gem_prime_handle_to_fd(struct drm_device *dev,
557 1.2 riastrad struct drm_file *file_priv, uint32_t handle,
558 1.2 riastrad uint32_t flags,
559 1.2 riastrad int *prime_fd)
560 1.1 riastrad {
561 1.1 riastrad struct drm_gem_object *obj;
562 1.2 riastrad int ret = 0;
563 1.2 riastrad struct dma_buf *dmabuf;
564 1.1 riastrad
565 1.2 riastrad mutex_lock(&file_priv->prime.lock);
566 1.10 riastrad obj = drm_gem_object_lookup(file_priv, handle);
567 1.2 riastrad if (!obj) {
568 1.2 riastrad ret = -ENOENT;
569 1.2 riastrad goto out_unlock;
570 1.2 riastrad }
571 1.2 riastrad
572 1.2 riastrad dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
573 1.2 riastrad if (dmabuf) {
574 1.2 riastrad get_dma_buf(dmabuf);
575 1.2 riastrad goto out_have_handle;
576 1.2 riastrad }
577 1.1 riastrad
578 1.2 riastrad mutex_lock(&dev->object_name_lock);
579 1.1 riastrad /* re-export the original imported object */
580 1.1 riastrad if (obj->import_attach) {
581 1.2 riastrad dmabuf = obj->import_attach->dmabuf;
582 1.2 riastrad get_dma_buf(dmabuf);
583 1.2 riastrad goto out_have_obj;
584 1.2 riastrad }
585 1.2 riastrad
586 1.2 riastrad if (obj->dma_buf) {
587 1.2 riastrad get_dma_buf(obj->dma_buf);
588 1.2 riastrad dmabuf = obj->dma_buf;
589 1.2 riastrad goto out_have_obj;
590 1.2 riastrad }
591 1.2 riastrad
592 1.2 riastrad dmabuf = export_and_register_object(dev, obj, flags);
593 1.2 riastrad if (IS_ERR(dmabuf)) {
594 1.2 riastrad /* normally the created dma-buf takes ownership of the ref,
595 1.2 riastrad * but if that fails then drop the ref
596 1.2 riastrad */
597 1.2 riastrad ret = PTR_ERR(dmabuf);
598 1.2 riastrad mutex_unlock(&dev->object_name_lock);
599 1.2 riastrad goto out;
600 1.1 riastrad }
601 1.1 riastrad
602 1.2 riastrad out_have_obj:
603 1.2 riastrad /*
604 1.2 riastrad * If we've exported this buffer then cheat and add it to the import list
605 1.2 riastrad * so we get the correct handle back. We must do this under the
606 1.2 riastrad * protection of dev->object_name_lock to ensure that a racing gem close
607 1.2 riastrad * ioctl doesn't miss to remove this buffer handle from the cache.
608 1.2 riastrad */
609 1.2 riastrad ret = drm_prime_add_buf_handle(&file_priv->prime,
610 1.2 riastrad dmabuf, handle);
611 1.2 riastrad mutex_unlock(&dev->object_name_lock);
612 1.2 riastrad if (ret)
613 1.2 riastrad goto fail_put_dmabuf;
614 1.2 riastrad
615 1.2 riastrad out_have_handle:
616 1.2 riastrad ret = dma_buf_fd(dmabuf, flags);
617 1.2 riastrad /*
618 1.2 riastrad * We must _not_ remove the buffer from the handle cache since the newly
619 1.2 riastrad * created dma buf is already linked in the global obj->dma_buf pointer,
620 1.2 riastrad * and that is invariant as long as a userspace gem handle exists.
621 1.2 riastrad * Closing the handle will clean out the cache anyway, so we don't leak.
622 1.2 riastrad */
623 1.2 riastrad if (ret < 0) {
624 1.2 riastrad goto fail_put_dmabuf;
625 1.1 riastrad } else {
626 1.2 riastrad *prime_fd = ret;
627 1.2 riastrad ret = 0;
628 1.2 riastrad }
629 1.2 riastrad
630 1.2 riastrad goto out;
631 1.2 riastrad
632 1.2 riastrad fail_put_dmabuf:
633 1.2 riastrad dma_buf_put(dmabuf);
634 1.2 riastrad out:
635 1.10 riastrad drm_gem_object_put_unlocked(obj);
636 1.2 riastrad out_unlock:
637 1.2 riastrad mutex_unlock(&file_priv->prime.lock);
638 1.2 riastrad
639 1.2 riastrad return ret;
640 1.2 riastrad }
641 1.2 riastrad EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
642 1.2 riastrad
643 1.10 riastrad int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
644 1.10 riastrad struct drm_file *file_priv)
645 1.10 riastrad {
646 1.10 riastrad struct drm_prime_handle *args = data;
647 1.10 riastrad
648 1.10 riastrad if (!dev->driver->prime_handle_to_fd)
649 1.10 riastrad return -ENOSYS;
650 1.10 riastrad
651 1.10 riastrad /* check flags are valid */
652 1.10 riastrad if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
653 1.10 riastrad return -EINVAL;
654 1.10 riastrad
655 1.10 riastrad return dev->driver->prime_handle_to_fd(dev, file_priv,
656 1.10 riastrad args->handle, args->flags, &args->fd);
657 1.10 riastrad }
658 1.10 riastrad
659 1.10 riastrad /**
660 1.10 riastrad * DOC: PRIME Helpers
661 1.10 riastrad *
662 1.10 riastrad * Drivers can implement &drm_gem_object_funcs.export and
663 1.10 riastrad * &drm_driver.gem_prime_import in terms of simpler APIs by using the helper
664 1.10 riastrad * functions drm_gem_prime_export() and drm_gem_prime_import(). These functions
665 1.10 riastrad * implement dma-buf support in terms of some lower-level helpers, which are
666 1.10 riastrad * again exported for drivers to use individually:
667 1.10 riastrad *
668 1.10 riastrad * Exporting buffers
669 1.10 riastrad * ~~~~~~~~~~~~~~~~~
670 1.10 riastrad *
671 1.10 riastrad * Optional pinning of buffers is handled at dma-buf attach and detach time in
672 1.10 riastrad * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
673 1.10 riastrad * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
674 1.10 riastrad * &drm_gem_object_funcs.get_sg_table.
675 1.10 riastrad *
676 1.10 riastrad * For kernel-internal access there's drm_gem_dmabuf_vmap() and
677 1.10 riastrad * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
678 1.10 riastrad * drm_gem_dmabuf_mmap().
679 1.10 riastrad *
680 1.10 riastrad * Note that these export helpers can only be used if the underlying backing
681 1.10 riastrad * storage is fully coherent and either permanently pinned, or it is safe to pin
682 1.10 riastrad * it indefinitely.
683 1.10 riastrad *
684 1.10 riastrad * FIXME: The underlying helper functions are named rather inconsistently.
685 1.10 riastrad *
686 1.10 riastrad * Exporting buffers
687 1.10 riastrad * ~~~~~~~~~~~~~~~~~
688 1.10 riastrad *
689 1.10 riastrad * Importing dma-bufs using drm_gem_prime_import() relies on
690 1.10 riastrad * &drm_driver.gem_prime_import_sg_table.
691 1.10 riastrad *
692 1.10 riastrad * Note that similarly to the export helpers this permanently pins the
693 1.10 riastrad * underlying backing storage. Which is ok for scanout, but is not the best
694 1.10 riastrad * option for sharing lots of buffers for rendering.
695 1.10 riastrad */
696 1.10 riastrad
697 1.2 riastrad /**
698 1.10 riastrad * drm_gem_map_attach - dma_buf attach implementation for GEM
699 1.10 riastrad * @dma_buf: buffer to attach device to
700 1.10 riastrad * @attach: buffer attachment data
701 1.10 riastrad *
702 1.10 riastrad * Calls &drm_gem_object_funcs.pin for device specific handling. This can be
703 1.10 riastrad * used as the &dma_buf_ops.attach callback. Must be used together with
704 1.10 riastrad * drm_gem_map_detach().
705 1.2 riastrad *
706 1.10 riastrad * Returns 0 on success, negative error code on failure.
707 1.10 riastrad */
708 1.10 riastrad int drm_gem_map_attach(struct dma_buf *dma_buf,
709 1.10 riastrad struct dma_buf_attachment *attach)
710 1.10 riastrad {
711 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
712 1.10 riastrad
713 1.10 riastrad return drm_gem_pin(obj);
714 1.10 riastrad }
715 1.10 riastrad EXPORT_SYMBOL(drm_gem_map_attach);
716 1.10 riastrad
717 1.10 riastrad /**
718 1.10 riastrad * drm_gem_map_detach - dma_buf detach implementation for GEM
719 1.10 riastrad * @dma_buf: buffer to detach from
720 1.10 riastrad * @attach: attachment to be detached
721 1.10 riastrad *
722 1.10 riastrad * Calls &drm_gem_object_funcs.pin for device specific handling. Cleans up
723 1.10 riastrad * &dma_buf_attachment from drm_gem_map_attach(). This can be used as the
724 1.10 riastrad * &dma_buf_ops.detach callback.
725 1.2 riastrad */
726 1.10 riastrad void drm_gem_map_detach(struct dma_buf *dma_buf,
727 1.10 riastrad struct dma_buf_attachment *attach)
728 1.2 riastrad {
729 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
730 1.2 riastrad
731 1.10 riastrad drm_gem_unpin(obj);
732 1.10 riastrad }
733 1.10 riastrad EXPORT_SYMBOL(drm_gem_map_detach);
734 1.10 riastrad
735 1.10 riastrad /**
736 1.10 riastrad * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
737 1.10 riastrad * @attach: attachment whose scatterlist is to be returned
738 1.10 riastrad * @dir: direction of DMA transfer
739 1.10 riastrad *
740 1.10 riastrad * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This
741 1.10 riastrad * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together
742 1.10 riastrad * with drm_gem_unmap_dma_buf().
743 1.10 riastrad *
744 1.10 riastrad * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR
745 1.10 riastrad * on error. May return -EINTR if it is interrupted by a signal.
746 1.10 riastrad */
747 1.10 riastrad struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
748 1.10 riastrad enum dma_data_direction dir)
749 1.10 riastrad {
750 1.10 riastrad struct drm_gem_object *obj = attach->dmabuf->priv;
751 1.10 riastrad struct sg_table *sgt;
752 1.2 riastrad
753 1.10 riastrad if (WARN_ON(dir == DMA_NONE))
754 1.2 riastrad return ERR_PTR(-EINVAL);
755 1.2 riastrad
756 1.10 riastrad if (obj->funcs)
757 1.10 riastrad sgt = obj->funcs->get_sg_table(obj);
758 1.10 riastrad else
759 1.10 riastrad sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
760 1.2 riastrad
761 1.10 riastrad if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
762 1.10 riastrad DMA_ATTR_SKIP_CPU_SYNC)) {
763 1.10 riastrad sg_free_table(sgt);
764 1.10 riastrad kfree(sgt);
765 1.10 riastrad sgt = ERR_PTR(-ENOMEM);
766 1.10 riastrad }
767 1.2 riastrad
768 1.10 riastrad return sgt;
769 1.10 riastrad }
770 1.10 riastrad EXPORT_SYMBOL(drm_gem_map_dma_buf);
771 1.2 riastrad
772 1.10 riastrad /**
773 1.10 riastrad * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
774 1.10 riastrad * @attach: attachment to unmap buffer from
775 1.10 riastrad * @sgt: scatterlist info of the buffer to unmap
776 1.10 riastrad * @dir: direction of DMA transfer
777 1.10 riastrad *
778 1.10 riastrad * This can be used as the &dma_buf_ops.unmap_dma_buf callback.
779 1.10 riastrad */
780 1.10 riastrad void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
781 1.10 riastrad struct sg_table *sgt,
782 1.10 riastrad enum dma_data_direction dir)
783 1.10 riastrad {
784 1.10 riastrad if (!sgt)
785 1.10 riastrad return;
786 1.1 riastrad
787 1.10 riastrad dma_unmap_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
788 1.10 riastrad DMA_ATTR_SKIP_CPU_SYNC);
789 1.10 riastrad sg_free_table(sgt);
790 1.10 riastrad kfree(sgt);
791 1.10 riastrad }
792 1.10 riastrad EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
793 1.2 riastrad
794 1.10 riastrad /**
795 1.10 riastrad * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
796 1.10 riastrad * @dma_buf: buffer to be mapped
797 1.10 riastrad *
798 1.10 riastrad * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
799 1.10 riastrad * callback. Calls into &drm_gem_object_funcs.vmap for device specific handling.
800 1.10 riastrad *
801 1.10 riastrad * Returns the kernel virtual address or NULL on failure.
802 1.10 riastrad */
803 1.10 riastrad void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
804 1.10 riastrad {
805 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
806 1.10 riastrad void *vaddr;
807 1.2 riastrad
808 1.10 riastrad vaddr = drm_gem_vmap(obj);
809 1.10 riastrad if (IS_ERR(vaddr))
810 1.10 riastrad vaddr = NULL;
811 1.2 riastrad
812 1.10 riastrad return vaddr;
813 1.1 riastrad }
814 1.10 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
815 1.1 riastrad
816 1.2 riastrad /**
817 1.10 riastrad * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
818 1.10 riastrad * @dma_buf: buffer to be unmapped
819 1.10 riastrad * @vaddr: the virtual address of the buffer
820 1.2 riastrad *
821 1.10 riastrad * Releases a kernel virtual mapping. This can be used as the
822 1.10 riastrad * &dma_buf_ops.vunmap callback. Calls into &drm_gem_object_funcs.vunmap for device specific handling.
823 1.2 riastrad */
824 1.10 riastrad void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
825 1.1 riastrad {
826 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
827 1.1 riastrad
828 1.10 riastrad drm_gem_vunmap(obj, vaddr);
829 1.10 riastrad }
830 1.10 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
831 1.1 riastrad
832 1.10 riastrad /**
833 1.10 riastrad * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
834 1.10 riastrad * @obj: GEM object
835 1.10 riastrad * @vma: Virtual address range
836 1.10 riastrad *
837 1.10 riastrad * This function sets up a userspace mapping for PRIME exported buffers using
838 1.10 riastrad * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
839 1.10 riastrad * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
840 1.10 riastrad * called to set up the mapping.
841 1.10 riastrad *
842 1.10 riastrad * Drivers can use this as their &drm_driver.gem_prime_mmap callback.
843 1.10 riastrad */
844 1.14 riastrad #ifdef __NetBSD__
845 1.14 riastrad int drm_gem_prime_mmap(struct drm_gem_object *obj, off_t *offp, size_t size,
846 1.14 riastrad int prot, int *flagsp, int *advicep, struct uvm_object **uobjp,
847 1.14 riastrad int *maxprotp)
848 1.14 riastrad #else
849 1.10 riastrad int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
850 1.14 riastrad #endif
851 1.10 riastrad {
852 1.10 riastrad struct drm_file *priv;
853 1.10 riastrad struct file *fil;
854 1.10 riastrad int ret;
855 1.1 riastrad
856 1.10 riastrad /* Add the fake offset */
857 1.14 riastrad #ifdef __NetBSD__
858 1.14 riastrad *offp += drm_vma_node_start(&obj->vma_node);
859 1.14 riastrad #else
860 1.10 riastrad vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
861 1.14 riastrad #endif
862 1.1 riastrad
863 1.10 riastrad if (obj->funcs && obj->funcs->mmap) {
864 1.14 riastrad #ifdef __NetBSD__
865 1.14 riastrad ret = obj->funcs->mmap(obj, offp, size, prot, flagsp, advicep,
866 1.14 riastrad uobjp, maxprotp);
867 1.14 riastrad #else
868 1.10 riastrad ret = obj->funcs->mmap(obj, vma);
869 1.14 riastrad #endif
870 1.10 riastrad if (ret)
871 1.10 riastrad return ret;
872 1.14 riastrad #ifndef __NetBSD__
873 1.10 riastrad vma->vm_private_data = obj;
874 1.14 riastrad #endif
875 1.10 riastrad drm_gem_object_get(obj);
876 1.10 riastrad return 0;
877 1.2 riastrad }
878 1.2 riastrad
879 1.10 riastrad priv = kzalloc(sizeof(*priv), GFP_KERNEL);
880 1.10 riastrad fil = kzalloc(sizeof(*fil), GFP_KERNEL);
881 1.10 riastrad if (!priv || !fil) {
882 1.10 riastrad ret = -ENOMEM;
883 1.10 riastrad goto out;
884 1.1 riastrad }
885 1.1 riastrad
886 1.10 riastrad /* Used by drm_gem_mmap() to lookup the GEM object */
887 1.10 riastrad priv->minor = obj->dev->primary;
888 1.14 riastrad #ifdef __NetBSD__
889 1.14 riastrad fil->f_data = priv;
890 1.14 riastrad #else
891 1.10 riastrad fil->private_data = priv;
892 1.14 riastrad #endif
893 1.1 riastrad
894 1.10 riastrad ret = drm_vma_node_allow(&obj->vma_node, priv);
895 1.1 riastrad if (ret)
896 1.10 riastrad goto out;
897 1.1 riastrad
898 1.14 riastrad #ifdef __NetBSD__
899 1.14 riastrad ret = obj->dev->driver->mmap_object(obj->dev, *offp, size, prot, uobjp,
900 1.14 riastrad offp, fil);
901 1.14 riastrad #else
902 1.10 riastrad ret = obj->dev->driver->fops->mmap(fil, vma);
903 1.14 riastrad #endif
904 1.2 riastrad
905 1.10 riastrad drm_vma_node_revoke(&obj->vma_node, priv);
906 1.10 riastrad out:
907 1.10 riastrad kfree(priv);
908 1.10 riastrad kfree(fil);
909 1.2 riastrad
910 1.1 riastrad return ret;
911 1.1 riastrad }
912 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_mmap);
913 1.1 riastrad
914 1.10 riastrad /**
915 1.10 riastrad * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
916 1.10 riastrad * @dma_buf: buffer to be mapped
917 1.10 riastrad * @vma: virtual address range
918 1.10 riastrad *
919 1.10 riastrad * Provides memory mapping for the buffer. This can be used as the
920 1.10 riastrad * &dma_buf_ops.mmap callback. It just forwards to &drm_driver.gem_prime_mmap,
921 1.10 riastrad * which should be set to drm_gem_prime_mmap().
922 1.10 riastrad *
923 1.10 riastrad * FIXME: There's really no point to this wrapper, drivers which need anything
924 1.10 riastrad * else but drm_gem_prime_mmap can roll their own &dma_buf_ops.mmap callback.
925 1.10 riastrad *
926 1.10 riastrad * Returns 0 on success or a negative error code on failure.
927 1.10 riastrad */
928 1.10 riastrad #ifdef __NetBSD__
929 1.13 riastrad int
930 1.10 riastrad drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, off_t *offp, size_t size,
931 1.10 riastrad int prot, int *flagsp, int *advicep, struct uvm_object **uobjp,
932 1.10 riastrad int *maxprotp)
933 1.10 riastrad #else
934 1.10 riastrad int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
935 1.10 riastrad #endif
936 1.1 riastrad {
937 1.10 riastrad struct drm_gem_object *obj = dma_buf->priv;
938 1.10 riastrad struct drm_device *dev = obj->dev;
939 1.1 riastrad
940 1.10 riastrad if (!dev->driver->gem_prime_mmap)
941 1.1 riastrad return -ENOSYS;
942 1.1 riastrad
943 1.10 riastrad #ifdef __NetBSD__
944 1.10 riastrad return dev->driver->gem_prime_mmap(obj, offp, size, prot, flagsp,
945 1.10 riastrad advicep, uobjp, maxprotp);
946 1.10 riastrad #else
947 1.10 riastrad return dev->driver->gem_prime_mmap(obj, vma);
948 1.10 riastrad #endif
949 1.1 riastrad }
950 1.10 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
951 1.1 riastrad
952 1.10 riastrad static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
953 1.10 riastrad .cache_sgt_mapping = true,
954 1.10 riastrad .attach = drm_gem_map_attach,
955 1.10 riastrad .detach = drm_gem_map_detach,
956 1.10 riastrad .map_dma_buf = drm_gem_map_dma_buf,
957 1.10 riastrad .unmap_dma_buf = drm_gem_unmap_dma_buf,
958 1.10 riastrad .release = drm_gem_dmabuf_release,
959 1.10 riastrad .mmap = drm_gem_dmabuf_mmap,
960 1.10 riastrad .vmap = drm_gem_dmabuf_vmap,
961 1.10 riastrad .vunmap = drm_gem_dmabuf_vunmap,
962 1.10 riastrad };
963 1.1 riastrad
964 1.2 riastrad /**
965 1.2 riastrad * drm_prime_pages_to_sg - converts a page array into an sg list
966 1.2 riastrad * @pages: pointer to the array of page pointers to convert
967 1.2 riastrad * @nr_pages: length of the page vector
968 1.1 riastrad *
969 1.2 riastrad * This helper creates an sg table object from a set of pages
970 1.1 riastrad * the driver is responsible for mapping the pages into the
971 1.2 riastrad * importers address space for use with dma_buf itself.
972 1.10 riastrad *
973 1.10 riastrad * This is useful for implementing &drm_gem_object_funcs.get_sg_table.
974 1.1 riastrad */
975 1.2 riastrad struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
976 1.1 riastrad {
977 1.1 riastrad struct sg_table *sg = NULL;
978 1.1 riastrad int ret;
979 1.1 riastrad
980 1.1 riastrad sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
981 1.2 riastrad if (!sg) {
982 1.2 riastrad ret = -ENOMEM;
983 1.1 riastrad goto out;
984 1.2 riastrad }
985 1.1 riastrad
986 1.2 riastrad ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
987 1.2 riastrad nr_pages << PAGE_SHIFT, GFP_KERNEL);
988 1.1 riastrad if (ret)
989 1.1 riastrad goto out;
990 1.1 riastrad
991 1.1 riastrad return sg;
992 1.1 riastrad out:
993 1.1 riastrad kfree(sg);
994 1.2 riastrad return ERR_PTR(ret);
995 1.1 riastrad }
996 1.1 riastrad EXPORT_SYMBOL(drm_prime_pages_to_sg);
997 1.1 riastrad
998 1.10 riastrad /**
999 1.10 riastrad * drm_gem_prime_export - helper library implementation of the export callback
1000 1.10 riastrad * @obj: GEM object to export
1001 1.10 riastrad * @flags: flags like DRM_CLOEXEC and DRM_RDWR
1002 1.10 riastrad *
1003 1.10 riastrad * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers
1004 1.10 riastrad * using the PRIME helpers. It is used as the default in
1005 1.10 riastrad * drm_gem_prime_handle_to_fd().
1006 1.10 riastrad */
1007 1.10 riastrad struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj,
1008 1.10 riastrad int flags)
1009 1.10 riastrad {
1010 1.10 riastrad struct drm_device *dev = obj->dev;
1011 1.10 riastrad struct dma_buf_export_info exp_info = {
1012 1.10 riastrad #ifndef __NetBSD__
1013 1.10 riastrad .exp_name = KBUILD_MODNAME, /* white lie for debug */
1014 1.10 riastrad .owner = dev->driver->fops->owner,
1015 1.10 riastrad #endif
1016 1.10 riastrad .ops = &drm_gem_prime_dmabuf_ops,
1017 1.10 riastrad .size = obj->size,
1018 1.10 riastrad .flags = flags,
1019 1.10 riastrad .priv = obj,
1020 1.10 riastrad .resv = obj->resv,
1021 1.10 riastrad };
1022 1.10 riastrad
1023 1.10 riastrad return drm_gem_dmabuf_export(dev, &exp_info);
1024 1.10 riastrad }
1025 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_export);
1026 1.10 riastrad
1027 1.10 riastrad /**
1028 1.10 riastrad * drm_gem_prime_import_dev - core implementation of the import callback
1029 1.10 riastrad * @dev: drm_device to import into
1030 1.10 riastrad * @dma_buf: dma-buf object to import
1031 1.10 riastrad * @attach_dev: struct device to dma_buf attach
1032 1.10 riastrad *
1033 1.10 riastrad * This is the core of drm_gem_prime_import(). It's designed to be called by
1034 1.10 riastrad * drivers who want to use a different device structure than &drm_device.dev for
1035 1.10 riastrad * attaching via dma_buf. This function calls
1036 1.10 riastrad * &drm_driver.gem_prime_import_sg_table internally.
1037 1.10 riastrad *
1038 1.10 riastrad * Drivers must arrange to call drm_prime_gem_destroy() from their
1039 1.10 riastrad * &drm_gem_object_funcs.free hook when using this function.
1040 1.10 riastrad */
1041 1.16 riastrad #ifdef __NetBSD__
1042 1.16 riastrad struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
1043 1.16 riastrad struct dma_buf *dma_buf,
1044 1.16 riastrad bus_dma_tag_t attach_dev)
1045 1.16 riastrad #else
1046 1.10 riastrad struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
1047 1.10 riastrad struct dma_buf *dma_buf,
1048 1.10 riastrad struct device *attach_dev)
1049 1.16 riastrad #endif
1050 1.10 riastrad {
1051 1.10 riastrad struct dma_buf_attachment *attach;
1052 1.10 riastrad struct sg_table *sgt;
1053 1.10 riastrad struct drm_gem_object *obj;
1054 1.10 riastrad int ret;
1055 1.10 riastrad
1056 1.10 riastrad if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
1057 1.10 riastrad obj = dma_buf->priv;
1058 1.10 riastrad if (obj->dev == dev) {
1059 1.10 riastrad /*
1060 1.10 riastrad * Importing dmabuf exported from out own gem increases
1061 1.10 riastrad * refcount on gem itself instead of f_count of dmabuf.
1062 1.10 riastrad */
1063 1.10 riastrad drm_gem_object_get(obj);
1064 1.10 riastrad return obj;
1065 1.10 riastrad }
1066 1.10 riastrad }
1067 1.10 riastrad
1068 1.10 riastrad if (!dev->driver->gem_prime_import_sg_table)
1069 1.10 riastrad return ERR_PTR(-EINVAL);
1070 1.10 riastrad
1071 1.10 riastrad attach = dma_buf_attach(dma_buf, attach_dev);
1072 1.10 riastrad if (IS_ERR(attach))
1073 1.10 riastrad return ERR_CAST(attach);
1074 1.10 riastrad
1075 1.10 riastrad get_dma_buf(dma_buf);
1076 1.10 riastrad
1077 1.10 riastrad sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
1078 1.10 riastrad if (IS_ERR(sgt)) {
1079 1.10 riastrad ret = PTR_ERR(sgt);
1080 1.10 riastrad goto fail_detach;
1081 1.10 riastrad }
1082 1.10 riastrad
1083 1.10 riastrad obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
1084 1.10 riastrad if (IS_ERR(obj)) {
1085 1.10 riastrad ret = PTR_ERR(obj);
1086 1.10 riastrad goto fail_unmap;
1087 1.10 riastrad }
1088 1.10 riastrad
1089 1.10 riastrad obj->import_attach = attach;
1090 1.10 riastrad obj->resv = dma_buf->resv;
1091 1.10 riastrad
1092 1.10 riastrad return obj;
1093 1.10 riastrad
1094 1.10 riastrad fail_unmap:
1095 1.10 riastrad dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
1096 1.10 riastrad fail_detach:
1097 1.10 riastrad dma_buf_detach(dma_buf, attach);
1098 1.10 riastrad dma_buf_put(dma_buf);
1099 1.10 riastrad
1100 1.10 riastrad return ERR_PTR(ret);
1101 1.10 riastrad }
1102 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_import_dev);
1103 1.10 riastrad
1104 1.10 riastrad /**
1105 1.10 riastrad * drm_gem_prime_import - helper library implementation of the import callback
1106 1.10 riastrad * @dev: drm_device to import into
1107 1.10 riastrad * @dma_buf: dma-buf object to import
1108 1.10 riastrad *
1109 1.10 riastrad * This is the implementation of the gem_prime_import functions for GEM drivers
1110 1.10 riastrad * using the PRIME helpers. Drivers can use this as their
1111 1.10 riastrad * &drm_driver.gem_prime_import implementation. It is used as the default
1112 1.10 riastrad * implementation in drm_gem_prime_fd_to_handle().
1113 1.10 riastrad *
1114 1.10 riastrad * Drivers must arrange to call drm_prime_gem_destroy() from their
1115 1.10 riastrad * &drm_gem_object_funcs.free hook when using this function.
1116 1.10 riastrad */
1117 1.10 riastrad struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1118 1.10 riastrad struct dma_buf *dma_buf)
1119 1.10 riastrad {
1120 1.16 riastrad #ifdef __NetBSD__
1121 1.16 riastrad return drm_gem_prime_import_dev(dev, dma_buf, dev->dmat);
1122 1.16 riastrad #else
1123 1.10 riastrad return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
1124 1.16 riastrad #endif
1125 1.10 riastrad }
1126 1.10 riastrad EXPORT_SYMBOL(drm_gem_prime_import);
1127 1.10 riastrad
1128 1.12 riastrad #ifdef __NetBSD__
1129 1.3 riastrad
1130 1.3 riastrad struct sg_table *
1131 1.5 riastrad drm_prime_bus_dmamem_to_sg(bus_dma_tag_t dmat, const bus_dma_segment_t *segs,
1132 1.5 riastrad int nsegs)
1133 1.4 riastrad {
1134 1.4 riastrad struct sg_table *sg;
1135 1.4 riastrad int ret;
1136 1.4 riastrad
1137 1.4 riastrad sg = kmalloc(sizeof(*sg), GFP_KERNEL);
1138 1.4 riastrad if (sg == NULL) {
1139 1.4 riastrad ret = -ENOMEM;
1140 1.4 riastrad goto out;
1141 1.4 riastrad }
1142 1.4 riastrad
1143 1.5 riastrad ret = sg_alloc_table_from_bus_dmamem(sg, dmat, segs, nsegs,
1144 1.5 riastrad GFP_KERNEL);
1145 1.4 riastrad if (ret)
1146 1.4 riastrad goto out;
1147 1.4 riastrad
1148 1.4 riastrad return sg;
1149 1.4 riastrad out:
1150 1.4 riastrad kfree(sg);
1151 1.4 riastrad return ERR_PTR(ret);
1152 1.4 riastrad }
1153 1.4 riastrad
1154 1.4 riastrad bus_size_t
1155 1.4 riastrad drm_prime_sg_size(struct sg_table *sg)
1156 1.4 riastrad {
1157 1.4 riastrad
1158 1.16 riastrad return sg->sgl->sg_npgs << PAGE_SHIFT;
1159 1.4 riastrad }
1160 1.4 riastrad
1161 1.3 riastrad void
1162 1.3 riastrad drm_prime_sg_free(struct sg_table *sg)
1163 1.3 riastrad {
1164 1.3 riastrad
1165 1.3 riastrad sg_free_table(sg);
1166 1.3 riastrad kfree(sg);
1167 1.3 riastrad }
1168 1.3 riastrad
1169 1.3 riastrad int
1170 1.5 riastrad drm_prime_sg_to_bus_dmamem(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
1171 1.5 riastrad int nsegs, int *rsegs, const struct sg_table *sgt)
1172 1.3 riastrad {
1173 1.3 riastrad
1174 1.3 riastrad /* XXX errno NetBSD->Linux */
1175 1.16 riastrad return -bus_dmamem_import_pages(dmat, segs, nsegs, rsegs,
1176 1.16 riastrad sgt->sgl->sg_pgs, sgt->sgl->sg_npgs);
1177 1.3 riastrad }
1178 1.3 riastrad
1179 1.4 riastrad int
1180 1.5 riastrad drm_prime_bus_dmamap_load_sgt(bus_dma_tag_t dmat, bus_dmamap_t map,
1181 1.5 riastrad struct sg_table *sgt)
1182 1.4 riastrad {
1183 1.5 riastrad bus_dma_segment_t *segs;
1184 1.5 riastrad bus_size_t size = drm_prime_sg_size(sgt);
1185 1.16 riastrad int nsegs = sgt->sgl->sg_npgs;
1186 1.5 riastrad int ret;
1187 1.5 riastrad
1188 1.16 riastrad segs = kcalloc(sgt->sgl->sg_npgs, sizeof(segs[0]), GFP_KERNEL);
1189 1.5 riastrad if (segs == NULL) {
1190 1.5 riastrad ret = -ENOMEM;
1191 1.5 riastrad goto out0;
1192 1.5 riastrad }
1193 1.5 riastrad
1194 1.5 riastrad ret = drm_prime_sg_to_bus_dmamem(dmat, segs, nsegs, &nsegs, sgt);
1195 1.5 riastrad if (ret)
1196 1.5 riastrad goto out1;
1197 1.16 riastrad KASSERT(nsegs <= sgt->sgl->sg_npgs);
1198 1.5 riastrad
1199 1.5 riastrad /* XXX errno NetBSD->Linux */
1200 1.5 riastrad ret = -bus_dmamap_load_raw(dmat, map, segs, nsegs, size,
1201 1.5 riastrad BUS_DMA_NOWAIT);
1202 1.5 riastrad if (ret)
1203 1.5 riastrad goto out1;
1204 1.4 riastrad
1205 1.5 riastrad out1: kfree(segs);
1206 1.5 riastrad out0: return ret;
1207 1.4 riastrad }
1208 1.4 riastrad
1209 1.7 riastrad bool
1210 1.7 riastrad drm_prime_sg_importable(bus_dma_tag_t dmat, struct sg_table *sgt)
1211 1.7 riastrad {
1212 1.7 riastrad unsigned i;
1213 1.7 riastrad
1214 1.16 riastrad for (i = 0; i < sgt->sgl->sg_npgs; i++) {
1215 1.16 riastrad if (bus_dmatag_bounces_paddr(dmat,
1216 1.16 riastrad VM_PAGE_TO_PHYS(&sgt->sgl->sg_pgs[i]->p_vmp)))
1217 1.7 riastrad return false;
1218 1.7 riastrad }
1219 1.7 riastrad return true;
1220 1.7 riastrad }
1221 1.7 riastrad
1222 1.3 riastrad #else /* !__NetBSD__ */
1223 1.3 riastrad
1224 1.2 riastrad /**
1225 1.2 riastrad * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
1226 1.2 riastrad * @sgt: scatter-gather table to convert
1227 1.10 riastrad * @pages: optional array of page pointers to store the page array in
1228 1.2 riastrad * @addrs: optional array to store the dma bus address of each page
1229 1.10 riastrad * @max_entries: size of both the passed-in arrays
1230 1.2 riastrad *
1231 1.2 riastrad * Exports an sg table into an array of pages and addresses. This is currently
1232 1.2 riastrad * required by the TTM driver in order to do correct fault handling.
1233 1.10 riastrad *
1234 1.10 riastrad * Drivers can use this in their &drm_driver.gem_prime_import_sg_table
1235 1.10 riastrad * implementation.
1236 1.2 riastrad */
1237 1.1 riastrad int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1238 1.10 riastrad dma_addr_t *addrs, int max_entries)
1239 1.1 riastrad {
1240 1.1 riastrad unsigned count;
1241 1.1 riastrad struct scatterlist *sg;
1242 1.1 riastrad struct page *page;
1243 1.10 riastrad u32 len, index;
1244 1.1 riastrad dma_addr_t addr;
1245 1.1 riastrad
1246 1.10 riastrad index = 0;
1247 1.1 riastrad for_each_sg(sgt->sgl, sg, sgt->nents, count) {
1248 1.1 riastrad len = sg->length;
1249 1.1 riastrad page = sg_page(sg);
1250 1.1 riastrad addr = sg_dma_address(sg);
1251 1.1 riastrad
1252 1.1 riastrad while (len > 0) {
1253 1.10 riastrad if (WARN_ON(index >= max_entries))
1254 1.1 riastrad return -1;
1255 1.10 riastrad if (pages)
1256 1.10 riastrad pages[index] = page;
1257 1.1 riastrad if (addrs)
1258 1.10 riastrad addrs[index] = addr;
1259 1.1 riastrad
1260 1.1 riastrad page++;
1261 1.1 riastrad addr += PAGE_SIZE;
1262 1.1 riastrad len -= PAGE_SIZE;
1263 1.10 riastrad index++;
1264 1.1 riastrad }
1265 1.1 riastrad }
1266 1.1 riastrad return 0;
1267 1.1 riastrad }
1268 1.1 riastrad EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
1269 1.2 riastrad
1270 1.3 riastrad #endif /* __NetBSD__ */
1271 1.3 riastrad
1272 1.2 riastrad /**
1273 1.2 riastrad * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
1274 1.2 riastrad * @obj: GEM object which was created from a dma-buf
1275 1.2 riastrad * @sg: the sg-table which was pinned at import time
1276 1.2 riastrad *
1277 1.2 riastrad * This is the cleanup functions which GEM drivers need to call when they use
1278 1.10 riastrad * drm_gem_prime_import() or drm_gem_prime_import_dev() to import dma-bufs.
1279 1.2 riastrad */
1280 1.1 riastrad void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
1281 1.1 riastrad {
1282 1.1 riastrad struct dma_buf_attachment *attach;
1283 1.1 riastrad struct dma_buf *dma_buf;
1284 1.1 riastrad attach = obj->import_attach;
1285 1.1 riastrad if (sg)
1286 1.1 riastrad dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
1287 1.1 riastrad dma_buf = attach->dmabuf;
1288 1.1 riastrad dma_buf_detach(attach->dmabuf, attach);
1289 1.1 riastrad /* remove the reference */
1290 1.1 riastrad dma_buf_put(dma_buf);
1291 1.1 riastrad }
1292 1.1 riastrad EXPORT_SYMBOL(drm_prime_gem_destroy);
1293