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