dma-buf: begin/end_cpu might lock the dma_resv lock
[linux-block.git] / include / linux / dma-buf.h
CommitLineData
caab277b 1/* SPDX-License-Identifier: GPL-2.0-only */
d15bd7ee
SS
2/*
3 * Header file for dma buffer sharing framework.
4 *
5 * Copyright(C) 2011 Linaro Limited. All rights reserved.
6 * Author: Sumit Semwal <sumit.semwal@ti.com>
7 *
8 * Many thanks to linaro-mm-sig list, and specially
9 * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
10 * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
11 * refining of this idea.
d15bd7ee
SS
12 */
13#ifndef __DMA_BUF_H__
14#define __DMA_BUF_H__
15
01fd30da 16#include <linux/dma-buf-map.h>
d15bd7ee
SS
17#include <linux/file.h>
18#include <linux/err.h>
d15bd7ee
SS
19#include <linux/scatterlist.h>
20#include <linux/list.h>
21#include <linux/dma-mapping.h>
f9a24d1a 22#include <linux/fs.h>
f54d1867 23#include <linux/dma-fence.h>
9b495a58 24#include <linux/wait.h>
d15bd7ee 25
313162d0 26struct device;
d15bd7ee
SS
27struct dma_buf;
28struct dma_buf_attachment;
29
30/**
31 * struct dma_buf_ops - operations possible on struct dma_buf
12c4727e
SS
32 * @vmap: [optional] creates a virtual mapping for the buffer into kernel
33 * address space. Same restrictions as for vmap and friends apply.
34 * @vunmap: [optional] unmaps a vmap from the buffer
d15bd7ee
SS
35 */
36struct dma_buf_ops {
f13e143e
CK
37 /**
38 * @cache_sgt_mapping:
39 *
40 * If true the framework will cache the first mapping made for each
41 * attachment. This avoids creating mappings for attachments multiple
42 * times.
43 */
44 bool cache_sgt_mapping;
45
2904a8c1
DV
46 /**
47 * @attach:
48 *
49 * This is called from dma_buf_attach() to make sure that a given
a19741e5
CK
50 * &dma_buf_attachment.dev can access the provided &dma_buf. Exporters
51 * which support buffer objects in special locations like VRAM or
52 * device-specific carveout areas should check whether the buffer could
53 * be move to system memory (or directly accessed by the provided
54 * device), and otherwise need to fail the attach operation.
2904a8c1
DV
55 *
56 * The exporter should also in general check whether the current
57 * allocation fullfills the DMA constraints of the new device. If this
58 * is not the case, and the allocation cannot be moved, it should also
59 * fail the attach operation.
60 *
e9b4d7b5
DV
61 * Any exporter-private housekeeping data can be stored in the
62 * &dma_buf_attachment.priv pointer.
2904a8c1
DV
63 *
64 * This callback is optional.
65 *
66 * Returns:
67 *
68 * 0 on success, negative error code on failure. It might return -EBUSY
69 * to signal that backing storage is already allocated and incompatible
70 * with the requirements of requesting device.
71 */
a19741e5 72 int (*attach)(struct dma_buf *, struct dma_buf_attachment *);
d15bd7ee 73
2904a8c1
DV
74 /**
75 * @detach:
76 *
77 * This is called by dma_buf_detach() to release a &dma_buf_attachment.
78 * Provided so that exporters can clean up any housekeeping for an
79 * &dma_buf_attachment.
80 *
81 * This callback is optional.
82 */
d15bd7ee
SS
83 void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
84
bb42df46
CK
85 /**
86 * @pin:
87 *
85804b70 88 * This is called by dma_buf_pin() and lets the exporter know that the
bb42df46
CK
89 * DMA-buf can't be moved any more.
90 *
85804b70 91 * This is called with the &dmabuf.resv object locked and is mutual
bd2275ee 92 * exclusive with @cache_sgt_mapping.
bb42df46
CK
93 *
94 * This callback is optional and should only be used in limited use
95 * cases like scanout and not for temporary pin operations.
96 *
97 * Returns:
98 *
99 * 0 on success, negative error code on failure.
100 */
101 int (*pin)(struct dma_buf_attachment *attach);
102
103 /**
104 * @unpin:
105 *
85804b70 106 * This is called by dma_buf_unpin() and lets the exporter know that the
bb42df46
CK
107 * DMA-buf can be moved again.
108 *
bd2275ee
CK
109 * This is called with the dmabuf->resv object locked and is mutual
110 * exclusive with @cache_sgt_mapping.
bb42df46
CK
111 *
112 * This callback is optional.
113 */
114 void (*unpin)(struct dma_buf_attachment *attach);
115
2904a8c1
DV
116 /**
117 * @map_dma_buf:
118 *
119 * This is called by dma_buf_map_attachment() and is used to map a
120 * shared &dma_buf into device address space, and it is mandatory. It
bb42df46 121 * can only be called if @attach has been called successfully.
2904a8c1
DV
122 *
123 * This call may sleep, e.g. when the backing storage first needs to be
124 * allocated, or moved to a location suitable for all currently attached
125 * devices.
126 *
127 * Note that any specific buffer attributes required for this function
128 * should get added to device_dma_parameters accessible via
e9b4d7b5 129 * &device.dma_params from the &dma_buf_attachment. The @attach callback
2904a8c1
DV
130 * should also check these constraints.
131 *
132 * If this is being called for the first time, the exporter can now
133 * choose to scan through the list of attachments for this buffer,
134 * collate the requirements of the attached devices, and choose an
135 * appropriate backing storage for the buffer.
136 *
137 * Based on enum dma_data_direction, it might be possible to have
138 * multiple users accessing at the same time (for reading, maybe), or
139 * any other kind of sharing that the exporter might wish to make
140 * available to buffer-users.
141 *
15fd552d
CK
142 * This is always called with the dmabuf->resv object locked when
143 * the dynamic_mapping flag is true.
144 *
2904a8c1
DV
145 * Returns:
146 *
147 * A &sg_table scatter list of or the backing storage of the DMA buffer,
148 * already mapped into the device address space of the &device attached
ac80cd17
JX
149 * with the provided &dma_buf_attachment. The addresses and lengths in
150 * the scatter list are PAGE_SIZE aligned.
2904a8c1
DV
151 *
152 * On failure, returns a negative error value wrapped into a pointer.
153 * May also return -EINTR when a signal was received while being
154 * blocked.
d15bd7ee
SS
155 */
156 struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
2904a8c1
DV
157 enum dma_data_direction);
158 /**
159 * @unmap_dma_buf:
160 *
161 * This is called by dma_buf_unmap_attachment() and should unmap and
162 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
bb42df46
CK
163 * For static dma_buf handling this might also unpins the backing
164 * storage if this is the last mapping of the DMA buffer.
2904a8c1 165 */
d15bd7ee 166 void (*unmap_dma_buf)(struct dma_buf_attachment *,
2904a8c1
DV
167 struct sg_table *,
168 enum dma_data_direction);
169
d15bd7ee
SS
170 /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
171 * if the call would block.
172 */
173
2904a8c1
DV
174 /**
175 * @release:
176 *
177 * Called after the last dma_buf_put to release the &dma_buf, and
178 * mandatory.
179 */
d15bd7ee
SS
180 void (*release)(struct dma_buf *);
181
0959a168
DV
182 /**
183 * @begin_cpu_access:
184 *
185 * This is called from dma_buf_begin_cpu_access() and allows the
de9114ec
DV
186 * exporter to ensure that the memory is actually coherent for cpu
187 * access. The exporter also needs to ensure that cpu access is coherent
188 * for the access direction. The direction can be used by the exporter
189 * to optimize the cache flushing, i.e. access with a different
0959a168
DV
190 * direction (read instead of write) might return stale or even bogus
191 * data (e.g. when the exporter needs to copy the data to temporary
192 * storage).
193 *
de9114ec
DV
194 * Note that this is both called through the DMA_BUF_IOCTL_SYNC IOCTL
195 * command for userspace mappings established through @mmap, and also
196 * for kernel mappings established with @vmap.
0959a168 197 *
de9114ec 198 * This callback is optional.
0959a168
DV
199 *
200 * Returns:
201 *
202 * 0 on success or a negative error code on failure. This can for
203 * example fail when the backing storage can't be allocated. Can also
204 * return -ERESTARTSYS or -EINTR when the call has been interrupted and
205 * needs to be restarted.
206 */
831e9da7 207 int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
0959a168
DV
208
209 /**
210 * @end_cpu_access:
211 *
212 * This is called from dma_buf_end_cpu_access() when the importer is
213 * done accessing the CPU. The exporter can use this to flush caches and
de9114ec 214 * undo anything else done in @begin_cpu_access.
0959a168
DV
215 *
216 * This callback is optional.
217 *
218 * Returns:
219 *
220 * 0 on success or a negative error code on failure. Can return
221 * -ERESTARTSYS or -EINTR when the call has been interrupted and needs
222 * to be restarted.
223 */
18b862dc 224 int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
4c78513e 225
0959a168
DV
226 /**
227 * @mmap:
228 *
229 * This callback is used by the dma_buf_mmap() function
230 *
231 * Note that the mapping needs to be incoherent, userspace is expected
232 * to braket CPU access using the DMA_BUF_IOCTL_SYNC interface.
233 *
234 * Because dma-buf buffers have invariant size over their lifetime, the
235 * dma-buf core checks whether a vma is too large and rejects such
236 * mappings. The exporter hence does not need to duplicate this check.
237 * Drivers do not need to check this themselves.
238 *
239 * If an exporter needs to manually flush caches and hence needs to fake
240 * coherency for mmap support, it needs to be able to zap all the ptes
241 * pointing at the backing storage. Now linux mm needs a struct
242 * address_space associated with the struct file stored in vma->vm_file
243 * to do that with the function unmap_mapping_range. But the dma_buf
244 * framework only backs every dma_buf fd with the anon_file struct file,
245 * i.e. all dma_bufs share the same file.
246 *
247 * Hence exporters need to setup their own file (and address_space)
248 * association by setting vma->vm_file and adjusting vma->vm_pgoff in
249 * the dma_buf mmap callback. In the specific case of a gem driver the
250 * exporter could use the shmem file already provided by gem (and set
251 * vm_pgoff = 0). Exporters can then zap ptes by unmapping the
252 * corresponding range of the struct address_space associated with their
253 * own file.
254 *
255 * This callback is optional.
256 *
257 * Returns:
258 *
259 * 0 on success or a negative error code on failure.
260 */
4c78513e 261 int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
98f86c9e 262
6619ccf1 263 int (*vmap)(struct dma_buf *dmabuf, struct dma_buf_map *map);
20e76f1a 264 void (*vunmap)(struct dma_buf *dmabuf, struct dma_buf_map *map);
d15bd7ee
SS
265};
266
267/**
268 * struct dma_buf - shared buffer object
476b485b 269 * @size: size of the buffer; invariant over the lifetime of the buffer.
d15bd7ee 270 * @file: file pointer used for sharing buffers across, and for refcounting.
15fd552d
CK
271 * @attachments: list of dma_buf_attachment that denotes all devices attached,
272 * protected by dma_resv lock.
d15bd7ee 273 * @ops: dma_buf_ops associated with this buffer object.
bb2bb903 274 * @lock: used internally to serialize list manipulation, attach/detach and
15fd552d 275 * vmap/unmap
e2082e3a
RC
276 * @vmapping_counter: used internally to refcnt the vmaps
277 * @vmap_ptr: the current vmap ptr if vmapping_counter > 0
78df9695 278 * @exp_name: name of the exporter; useful for debugging.
15fd552d
CK
279 * @name: userspace-provided name; useful for accounting and debugging,
280 * protected by @resv.
0f50257f 281 * @name_lock: spinlock to protect name access
9abdffe2
SS
282 * @owner: pointer to exporter module; used for refcounting when exporter is a
283 * kernel module.
b89e3563 284 * @list_node: node for dma_buf accounting and debugging.
d15bd7ee 285 * @priv: exporter specific private data for this buffer object.
3aac4502 286 * @resv: reservation object linked to this dma-buf
e2082e3a
RC
287 * @poll: for userspace poll support
288 * @cb_excl: for userspace poll support
289 * @cb_shared: for userspace poll support
2904a8c1
DV
290 *
291 * This represents a shared buffer, created by calling dma_buf_export(). The
292 * userspace representation is a normal file descriptor, which can be created by
293 * calling dma_buf_fd().
294 *
295 * Shared dma buffers are reference counted using dma_buf_put() and
296 * get_dma_buf().
297 *
f641d3b5 298 * Device DMA access is handled by the separate &struct dma_buf_attachment.
d15bd7ee
SS
299 */
300struct dma_buf {
301 size_t size;
302 struct file *file;
303 struct list_head attachments;
304 const struct dma_buf_ops *ops;
d15bd7ee 305 struct mutex lock;
f00b4dad 306 unsigned vmapping_counter;
01fd30da 307 struct dma_buf_map vmap_ptr;
78df9695 308 const char *exp_name;
bb2bb903 309 const char *name;
0f50257f 310 spinlock_t name_lock;
9abdffe2 311 struct module *owner;
b89e3563 312 struct list_head list_node;
d15bd7ee 313 void *priv;
52791eee 314 struct dma_resv *resv;
9b495a58
ML
315
316 /* poll support */
317 wait_queue_head_t poll;
318
319 struct dma_buf_poll_cb_t {
f54d1867 320 struct dma_fence_cb cb;
9b495a58
ML
321 wait_queue_head_t *poll;
322
01e5d556 323 __poll_t active;
9b495a58 324 } cb_excl, cb_shared;
d15bd7ee
SS
325};
326
bb42df46
CK
327/**
328 * struct dma_buf_attach_ops - importer operations for an attachment
bb42df46
CK
329 *
330 * Attachment operations implemented by the importer.
331 */
332struct dma_buf_attach_ops {
09606b54
CK
333 /**
334 * @allow_peer2peer:
335 *
336 * If this is set to true the importer must be able to handle peer
337 * resources without struct pages.
338 */
339 bool allow_peer2peer;
340
bb42df46 341 /**
6f49c251 342 * @move_notify: [optional] notification that the DMA-buf is moving
bb42df46
CK
343 *
344 * If this callback is provided the framework can avoid pinning the
345 * backing store while mappings exists.
346 *
347 * This callback is called with the lock of the reservation object
348 * associated with the dma_buf held and the mapping function must be
349 * called with this lock held as well. This makes sure that no mapping
350 * is created concurrently with an ongoing move operation.
351 *
352 * Mappings stay valid and are not directly affected by this callback.
353 * But the DMA-buf can now be in a different physical location, so all
354 * mappings should be destroyed and re-created as soon as possible.
355 *
356 * New mappings can be created after this callback returns, and will
357 * point to the new location of the DMA-buf.
358 */
359 void (*move_notify)(struct dma_buf_attachment *attach);
360};
361
d15bd7ee
SS
362/**
363 * struct dma_buf_attachment - holds device-buffer attachment data
364 * @dmabuf: buffer for this attachment.
365 * @dev: device attached to the buffer.
15fd552d 366 * @node: list of dma_buf_attachment, protected by dma_resv lock of the dmabuf.
f13e143e
CK
367 * @sgt: cached mapping.
368 * @dir: direction of cached mapping.
09606b54 369 * @peer2peer: true if the importer can handle peer resources without pages.
d15bd7ee 370 * @priv: exporter specific attachment data.
bb42df46
CK
371 * @importer_ops: importer operations for this attachment, if provided
372 * dma_buf_map/unmap_attachment() must be called with the dma_resv lock held.
373 * @importer_priv: importer specific attachment data.
d15bd7ee
SS
374 *
375 * This structure holds the attachment information between the dma_buf buffer
376 * and its user device(s). The list contains one attachment struct per device
377 * attached to the buffer.
2904a8c1
DV
378 *
379 * An attachment is created by calling dma_buf_attach(), and released again by
380 * calling dma_buf_detach(). The DMA mapping itself needed to initiate a
381 * transfer is created by dma_buf_map_attachment() and freed again by calling
382 * dma_buf_unmap_attachment().
d15bd7ee
SS
383 */
384struct dma_buf_attachment {
385 struct dma_buf *dmabuf;
386 struct device *dev;
387 struct list_head node;
f13e143e
CK
388 struct sg_table *sgt;
389 enum dma_data_direction dir;
09606b54 390 bool peer2peer;
bb42df46
CK
391 const struct dma_buf_attach_ops *importer_ops;
392 void *importer_priv;
d15bd7ee
SS
393 void *priv;
394};
395
d8fbe341
SS
396/**
397 * struct dma_buf_export_info - holds information needed to export a dma_buf
9abdffe2
SS
398 * @exp_name: name of the exporter - useful for debugging.
399 * @owner: pointer to exporter module - used for refcounting kernel module
d8fbe341 400 * @ops: Attach allocator-defined dma buf ops to the new buffer
476b485b 401 * @size: Size of the buffer - invariant over the lifetime of the buffer
d8fbe341
SS
402 * @flags: mode flags for the file
403 * @resv: reservation-object, NULL to allocate default one
404 * @priv: Attach private data of allocator to this buffer
405 *
406 * This structure holds the information required to export the buffer. Used
407 * with dma_buf_export() only.
408 */
409struct dma_buf_export_info {
410 const char *exp_name;
9abdffe2 411 struct module *owner;
d8fbe341
SS
412 const struct dma_buf_ops *ops;
413 size_t size;
414 int flags;
52791eee 415 struct dma_resv *resv;
d8fbe341
SS
416 void *priv;
417};
418
419/**
2904a8c1 420 * DEFINE_DMA_BUF_EXPORT_INFO - helper macro for exporters
e2082e3a 421 * @name: export-info name
2904a8c1 422 *
f641d3b5 423 * DEFINE_DMA_BUF_EXPORT_INFO macro defines the &struct dma_buf_export_info,
2904a8c1 424 * zeroes it out and pre-populates exp_name in it.
d8fbe341 425 */
e2082e3a
RC
426#define DEFINE_DMA_BUF_EXPORT_INFO(name) \
427 struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \
9abdffe2 428 .owner = THIS_MODULE }
d8fbe341 429
f9a24d1a
RC
430/**
431 * get_dma_buf - convenience wrapper for get_file.
432 * @dmabuf: [in] pointer to dma_buf
433 *
434 * Increments the reference count on the dma-buf, needed in case of drivers
435 * that either need to create additional references to the dmabuf on the
436 * kernel side. For example, an exporter that needs to keep a dmabuf ptr
437 * so that subsequent exports don't create a new dmabuf.
438 */
439static inline void get_dma_buf(struct dma_buf *dmabuf)
440{
441 get_file(dmabuf->file);
442}
443
15fd552d
CK
444/**
445 * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
446 * @dmabuf: the DMA-buf to check
447 *
448 * Returns true if a DMA-buf exporter wants to be called with the dma_resv
449 * locked for the map/unmap callbacks, false if it doesn't wants to be called
450 * with the lock held.
451 */
452static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
453{
bd2275ee 454 return !!dmabuf->ops->pin;
15fd552d
CK
455}
456
457/**
458 * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
459 * mappinsg
460 * @attach: the DMA-buf attachment to check
461 *
462 * Returns true if a DMA-buf importer wants to call the map/unmap functions with
463 * the dma_resv lock held.
464 */
465static inline bool
466dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
467{
bb42df46 468 return !!attach->importer_ops;
15fd552d
CK
469}
470
d15bd7ee 471struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
15fd552d
CK
472 struct device *dev);
473struct dma_buf_attachment *
474dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
bb42df46
CK
475 const struct dma_buf_attach_ops *importer_ops,
476 void *importer_priv);
d15bd7ee 477void dma_buf_detach(struct dma_buf *dmabuf,
15fd552d 478 struct dma_buf_attachment *attach);
bb42df46
CK
479int dma_buf_pin(struct dma_buf_attachment *attach);
480void dma_buf_unpin(struct dma_buf_attachment *attach);
78df9695 481
d8fbe341 482struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
78df9695 483
55c1c4ca 484int dma_buf_fd(struct dma_buf *dmabuf, int flags);
d15bd7ee
SS
485struct dma_buf *dma_buf_get(int fd);
486void dma_buf_put(struct dma_buf *dmabuf);
487
488struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
489 enum dma_data_direction);
33ea2dcb
SS
490void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
491 enum dma_data_direction);
15fd552d 492void dma_buf_move_notify(struct dma_buf *dma_buf);
831e9da7 493int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
fc13020e 494 enum dma_data_direction dir);
18b862dc
CW
495int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
496 enum dma_data_direction dir);
4c78513e
DV
497
498int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
499 unsigned long);
6619ccf1 500int dma_buf_vmap(struct dma_buf *dmabuf, struct dma_buf_map *map);
20e76f1a 501void dma_buf_vunmap(struct dma_buf *dmabuf, struct dma_buf_map *map);
d15bd7ee 502#endif /* __DMA_BUF_H__ */