xref: /linux-6.15/include/linux/dma-buf.h (revision bfc7bc53)
1caab277bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2d15bd7eeSSumit Semwal /*
3d15bd7eeSSumit Semwal  * Header file for dma buffer sharing framework.
4d15bd7eeSSumit Semwal  *
5d15bd7eeSSumit Semwal  * Copyright(C) 2011 Linaro Limited. All rights reserved.
6d15bd7eeSSumit Semwal  * Author: Sumit Semwal <[email protected]>
7d15bd7eeSSumit Semwal  *
8d15bd7eeSSumit Semwal  * Many thanks to linaro-mm-sig list, and specially
9d15bd7eeSSumit Semwal  * Arnd Bergmann <[email protected]>, Rob Clark <[email protected]> and
10d15bd7eeSSumit Semwal  * Daniel Vetter <[email protected]> for their support in creation and
11d15bd7eeSSumit Semwal  * refining of this idea.
12d15bd7eeSSumit Semwal  */
13d15bd7eeSSumit Semwal #ifndef __DMA_BUF_H__
14d15bd7eeSSumit Semwal #define __DMA_BUF_H__
15d15bd7eeSSumit Semwal 
167938f421SLucas De Marchi #include <linux/iosys-map.h>
17d15bd7eeSSumit Semwal #include <linux/file.h>
18d15bd7eeSSumit Semwal #include <linux/err.h>
19d15bd7eeSSumit Semwal #include <linux/scatterlist.h>
20d15bd7eeSSumit Semwal #include <linux/list.h>
21d15bd7eeSSumit Semwal #include <linux/dma-mapping.h>
22f9a24d1aSRob Clark #include <linux/fs.h>
23f54d1867SChris Wilson #include <linux/dma-fence.h>
249b495a58SMaarten Lankhorst #include <linux/wait.h>
25d15bd7eeSSumit Semwal 
26313162d0SPaul Gortmaker struct device;
27d15bd7eeSSumit Semwal struct dma_buf;
28d15bd7eeSSumit Semwal struct dma_buf_attachment;
29d15bd7eeSSumit Semwal 
30d15bd7eeSSumit Semwal /**
31d15bd7eeSSumit Semwal  * struct dma_buf_ops - operations possible on struct dma_buf
3212c4727eSSumit Semwal  * @vmap: [optional] creates a virtual mapping for the buffer into kernel
3312c4727eSSumit Semwal  *	  address space. Same restrictions as for vmap and friends apply.
3412c4727eSSumit Semwal  * @vunmap: [optional] unmaps a vmap from the buffer
35d15bd7eeSSumit Semwal  */
36d15bd7eeSSumit Semwal struct dma_buf_ops {
372904a8c1SDaniel Vetter 	/**
38f13e143eSChristian König 	  * @cache_sgt_mapping:
39f13e143eSChristian König 	  *
40f13e143eSChristian König 	  * If true the framework will cache the first mapping made for each
41f13e143eSChristian König 	  * attachment. This avoids creating mappings for attachments multiple
42f13e143eSChristian König 	  * times.
43f13e143eSChristian König 	  */
44f13e143eSChristian König 	bool cache_sgt_mapping;
45f13e143eSChristian König 
46f13e143eSChristian König 	/**
472904a8c1SDaniel Vetter 	 * @attach:
482904a8c1SDaniel Vetter 	 *
492904a8c1SDaniel Vetter 	 * This is called from dma_buf_attach() to make sure that a given
50a19741e5SChristian König 	 * &dma_buf_attachment.dev can access the provided &dma_buf. Exporters
51a19741e5SChristian König 	 * which support buffer objects in special locations like VRAM or
52a19741e5SChristian König 	 * device-specific carveout areas should check whether the buffer could
53a19741e5SChristian König 	 * be move to system memory (or directly accessed by the provided
54a19741e5SChristian König 	 * device), and otherwise need to fail the attach operation.
552904a8c1SDaniel Vetter 	 *
562904a8c1SDaniel Vetter 	 * The exporter should also in general check whether the current
57a4812d0bSGal Pressman 	 * allocation fulfills the DMA constraints of the new device. If this
582904a8c1SDaniel Vetter 	 * is not the case, and the allocation cannot be moved, it should also
592904a8c1SDaniel Vetter 	 * fail the attach operation.
602904a8c1SDaniel Vetter 	 *
61e9b4d7b5SDaniel Vetter 	 * Any exporter-private housekeeping data can be stored in the
62e9b4d7b5SDaniel Vetter 	 * &dma_buf_attachment.priv pointer.
632904a8c1SDaniel Vetter 	 *
642904a8c1SDaniel Vetter 	 * This callback is optional.
652904a8c1SDaniel Vetter 	 *
662904a8c1SDaniel Vetter 	 * Returns:
672904a8c1SDaniel Vetter 	 *
682904a8c1SDaniel Vetter 	 * 0 on success, negative error code on failure. It might return -EBUSY
692904a8c1SDaniel Vetter 	 * to signal that backing storage is already allocated and incompatible
702904a8c1SDaniel Vetter 	 * with the requirements of requesting device.
712904a8c1SDaniel Vetter 	 */
72a19741e5SChristian König 	int (*attach)(struct dma_buf *, struct dma_buf_attachment *);
73d15bd7eeSSumit Semwal 
742904a8c1SDaniel Vetter 	/**
752904a8c1SDaniel Vetter 	 * @detach:
762904a8c1SDaniel Vetter 	 *
772904a8c1SDaniel Vetter 	 * This is called by dma_buf_detach() to release a &dma_buf_attachment.
782904a8c1SDaniel Vetter 	 * Provided so that exporters can clean up any housekeeping for an
792904a8c1SDaniel Vetter 	 * &dma_buf_attachment.
802904a8c1SDaniel Vetter 	 *
812904a8c1SDaniel Vetter 	 * This callback is optional.
822904a8c1SDaniel Vetter 	 */
83d15bd7eeSSumit Semwal 	void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
84d15bd7eeSSumit Semwal 
852904a8c1SDaniel Vetter 	/**
86bb42df46SChristian König 	 * @pin:
87bb42df46SChristian König 	 *
8885804b70SDaniel Vetter 	 * This is called by dma_buf_pin() and lets the exporter know that the
895460601dSGal Pressman 	 * DMA-buf can't be moved any more. Ideally, the exporter should
905460601dSGal Pressman 	 * pin the buffer so that it is generally accessible by all
91c545781eSDaniel Vetter 	 * devices.
92bb42df46SChristian König 	 *
9385804b70SDaniel Vetter 	 * This is called with the &dmabuf.resv object locked and is mutual
94bd2275eeSChristian König 	 * exclusive with @cache_sgt_mapping.
95bb42df46SChristian König 	 *
96c545781eSDaniel Vetter 	 * This is called automatically for non-dynamic importers from
97c545781eSDaniel Vetter 	 * dma_buf_attach().
98bb42df46SChristian König 	 *
9989bcadc8SDaniel Vetter 	 * Note that similar to non-dynamic exporters in their @map_dma_buf
10089bcadc8SDaniel Vetter 	 * callback the driver must guarantee that the memory is available for
10189bcadc8SDaniel Vetter 	 * use and cleared of any old data by the time this function returns.
10289bcadc8SDaniel Vetter 	 * Drivers which pipeline their buffer moves internally must wait for
10389bcadc8SDaniel Vetter 	 * all moves and clears to complete.
10489bcadc8SDaniel Vetter 	 *
105bb42df46SChristian König 	 * Returns:
106bb42df46SChristian König 	 *
107bb42df46SChristian König 	 * 0 on success, negative error code on failure.
108bb42df46SChristian König 	 */
109bb42df46SChristian König 	int (*pin)(struct dma_buf_attachment *attach);
110bb42df46SChristian König 
111bb42df46SChristian König 	/**
112bb42df46SChristian König 	 * @unpin:
113bb42df46SChristian König 	 *
11485804b70SDaniel Vetter 	 * This is called by dma_buf_unpin() and lets the exporter know that the
115bb42df46SChristian König 	 * DMA-buf can be moved again.
116bb42df46SChristian König 	 *
117bd2275eeSChristian König 	 * This is called with the dmabuf->resv object locked and is mutual
118bd2275eeSChristian König 	 * exclusive with @cache_sgt_mapping.
119bb42df46SChristian König 	 *
120bb42df46SChristian König 	 * This callback is optional.
121bb42df46SChristian König 	 */
122bb42df46SChristian König 	void (*unpin)(struct dma_buf_attachment *attach);
123bb42df46SChristian König 
124bb42df46SChristian König 	/**
1252904a8c1SDaniel Vetter 	 * @map_dma_buf:
1262904a8c1SDaniel Vetter 	 *
1272904a8c1SDaniel Vetter 	 * This is called by dma_buf_map_attachment() and is used to map a
1282904a8c1SDaniel Vetter 	 * shared &dma_buf into device address space, and it is mandatory. It
129bb42df46SChristian König 	 * can only be called if @attach has been called successfully.
1302904a8c1SDaniel Vetter 	 *
1312904a8c1SDaniel Vetter 	 * This call may sleep, e.g. when the backing storage first needs to be
1322904a8c1SDaniel Vetter 	 * allocated, or moved to a location suitable for all currently attached
1332904a8c1SDaniel Vetter 	 * devices.
1342904a8c1SDaniel Vetter 	 *
1352904a8c1SDaniel Vetter 	 * Note that any specific buffer attributes required for this function
1362904a8c1SDaniel Vetter 	 * should get added to device_dma_parameters accessible via
137e9b4d7b5SDaniel Vetter 	 * &device.dma_params from the &dma_buf_attachment. The @attach callback
1382904a8c1SDaniel Vetter 	 * should also check these constraints.
1392904a8c1SDaniel Vetter 	 *
1402904a8c1SDaniel Vetter 	 * If this is being called for the first time, the exporter can now
1412904a8c1SDaniel Vetter 	 * choose to scan through the list of attachments for this buffer,
1422904a8c1SDaniel Vetter 	 * collate the requirements of the attached devices, and choose an
1432904a8c1SDaniel Vetter 	 * appropriate backing storage for the buffer.
1442904a8c1SDaniel Vetter 	 *
1452904a8c1SDaniel Vetter 	 * Based on enum dma_data_direction, it might be possible to have
1462904a8c1SDaniel Vetter 	 * multiple users accessing at the same time (for reading, maybe), or
1472904a8c1SDaniel Vetter 	 * any other kind of sharing that the exporter might wish to make
1482904a8c1SDaniel Vetter 	 * available to buffer-users.
1492904a8c1SDaniel Vetter 	 *
15015fd552dSChristian König 	 * This is always called with the dmabuf->resv object locked when
15115fd552dSChristian König 	 * the dynamic_mapping flag is true.
15215fd552dSChristian König 	 *
15389bcadc8SDaniel Vetter 	 * Note that for non-dynamic exporters the driver must guarantee that
15489bcadc8SDaniel Vetter 	 * that the memory is available for use and cleared of any old data by
15589bcadc8SDaniel Vetter 	 * the time this function returns.  Drivers which pipeline their buffer
15689bcadc8SDaniel Vetter 	 * moves internally must wait for all moves and clears to complete.
15789bcadc8SDaniel Vetter 	 * Dynamic exporters do not need to follow this rule: For non-dynamic
15889bcadc8SDaniel Vetter 	 * importers the buffer is already pinned through @pin, which has the
15989bcadc8SDaniel Vetter 	 * same requirements. Dynamic importers otoh are required to obey the
16089bcadc8SDaniel Vetter 	 * dma_resv fences.
16189bcadc8SDaniel Vetter 	 *
1622904a8c1SDaniel Vetter 	 * Returns:
1632904a8c1SDaniel Vetter 	 *
164a4812d0bSGal Pressman 	 * A &sg_table scatter list of the backing storage of the DMA buffer,
1652904a8c1SDaniel Vetter 	 * already mapped into the device address space of the &device attached
166ac80cd17SJianxin Xiong 	 * with the provided &dma_buf_attachment. The addresses and lengths in
167ac80cd17SJianxin Xiong 	 * the scatter list are PAGE_SIZE aligned.
1682904a8c1SDaniel Vetter 	 *
1692904a8c1SDaniel Vetter 	 * On failure, returns a negative error value wrapped into a pointer.
1702904a8c1SDaniel Vetter 	 * May also return -EINTR when a signal was received while being
1712904a8c1SDaniel Vetter 	 * blocked.
17284335675SDaniel Vetter 	 *
17384335675SDaniel Vetter 	 * Note that exporters should not try to cache the scatter list, or
17484335675SDaniel Vetter 	 * return the same one for multiple calls. Caching is done either by the
17584335675SDaniel Vetter 	 * DMA-BUF code (for non-dynamic importers) or the importer. Ownership
17684335675SDaniel Vetter 	 * of the scatter list is transferred to the caller, and returned by
17784335675SDaniel Vetter 	 * @unmap_dma_buf.
178d15bd7eeSSumit Semwal 	 */
179d15bd7eeSSumit Semwal 	struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
180d15bd7eeSSumit Semwal 					 enum dma_data_direction);
1812904a8c1SDaniel Vetter 	/**
1822904a8c1SDaniel Vetter 	 * @unmap_dma_buf:
1832904a8c1SDaniel Vetter 	 *
1842904a8c1SDaniel Vetter 	 * This is called by dma_buf_unmap_attachment() and should unmap and
1852904a8c1SDaniel Vetter 	 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
186a4812d0bSGal Pressman 	 * For static dma_buf handling this might also unpin the backing
187bb42df46SChristian König 	 * storage if this is the last mapping of the DMA buffer.
1882904a8c1SDaniel Vetter 	 */
189d15bd7eeSSumit Semwal 	void (*unmap_dma_buf)(struct dma_buf_attachment *,
19033ea2dcbSSumit Semwal 			      struct sg_table *,
19133ea2dcbSSumit Semwal 			      enum dma_data_direction);
1922904a8c1SDaniel Vetter 
193d15bd7eeSSumit Semwal 	/* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
194d15bd7eeSSumit Semwal 	 * if the call would block.
195d15bd7eeSSumit Semwal 	 */
196d15bd7eeSSumit Semwal 
1972904a8c1SDaniel Vetter 	/**
1982904a8c1SDaniel Vetter 	 * @release:
1992904a8c1SDaniel Vetter 	 *
2002904a8c1SDaniel Vetter 	 * Called after the last dma_buf_put to release the &dma_buf, and
2012904a8c1SDaniel Vetter 	 * mandatory.
2022904a8c1SDaniel Vetter 	 */
203d15bd7eeSSumit Semwal 	void (*release)(struct dma_buf *);
204d15bd7eeSSumit Semwal 
2050959a168SDaniel Vetter 	/**
2060959a168SDaniel Vetter 	 * @begin_cpu_access:
2070959a168SDaniel Vetter 	 *
2080959a168SDaniel Vetter 	 * This is called from dma_buf_begin_cpu_access() and allows the
209de9114ecSDaniel Vetter 	 * exporter to ensure that the memory is actually coherent for cpu
210de9114ecSDaniel Vetter 	 * access. The exporter also needs to ensure that cpu access is coherent
211de9114ecSDaniel Vetter 	 * for the access direction. The direction can be used by the exporter
212de9114ecSDaniel Vetter 	 * to optimize the cache flushing, i.e. access with a different
2130959a168SDaniel Vetter 	 * direction (read instead of write) might return stale or even bogus
2140959a168SDaniel Vetter 	 * data (e.g. when the exporter needs to copy the data to temporary
2150959a168SDaniel Vetter 	 * storage).
2160959a168SDaniel Vetter 	 *
217de9114ecSDaniel Vetter 	 * Note that this is both called through the DMA_BUF_IOCTL_SYNC IOCTL
218de9114ecSDaniel Vetter 	 * command for userspace mappings established through @mmap, and also
219de9114ecSDaniel Vetter 	 * for kernel mappings established with @vmap.
2200959a168SDaniel Vetter 	 *
221de9114ecSDaniel Vetter 	 * This callback is optional.
2220959a168SDaniel Vetter 	 *
2230959a168SDaniel Vetter 	 * Returns:
2240959a168SDaniel Vetter 	 *
2250959a168SDaniel Vetter 	 * 0 on success or a negative error code on failure. This can for
2260959a168SDaniel Vetter 	 * example fail when the backing storage can't be allocated. Can also
2270959a168SDaniel Vetter 	 * return -ERESTARTSYS or -EINTR when the call has been interrupted and
2280959a168SDaniel Vetter 	 * needs to be restarted.
2290959a168SDaniel Vetter 	 */
230831e9da7STiago Vignatti 	int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
2310959a168SDaniel Vetter 
2320959a168SDaniel Vetter 	/**
2330959a168SDaniel Vetter 	 * @end_cpu_access:
2340959a168SDaniel Vetter 	 *
2350959a168SDaniel Vetter 	 * This is called from dma_buf_end_cpu_access() when the importer is
2360959a168SDaniel Vetter 	 * done accessing the CPU. The exporter can use this to flush caches and
237de9114ecSDaniel Vetter 	 * undo anything else done in @begin_cpu_access.
2380959a168SDaniel Vetter 	 *
2390959a168SDaniel Vetter 	 * This callback is optional.
2400959a168SDaniel Vetter 	 *
2410959a168SDaniel Vetter 	 * Returns:
2420959a168SDaniel Vetter 	 *
2430959a168SDaniel Vetter 	 * 0 on success or a negative error code on failure. Can return
2440959a168SDaniel Vetter 	 * -ERESTARTSYS or -EINTR when the call has been interrupted and needs
2450959a168SDaniel Vetter 	 * to be restarted.
2460959a168SDaniel Vetter 	 */
24718b862dcSChris Wilson 	int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
2484c78513eSDaniel Vetter 
2490959a168SDaniel Vetter 	/**
2500959a168SDaniel Vetter 	 * @mmap:
2510959a168SDaniel Vetter 	 *
2520959a168SDaniel Vetter 	 * This callback is used by the dma_buf_mmap() function
2530959a168SDaniel Vetter 	 *
2540959a168SDaniel Vetter 	 * Note that the mapping needs to be incoherent, userspace is expected
255a4812d0bSGal Pressman 	 * to bracket CPU access using the DMA_BUF_IOCTL_SYNC interface.
2560959a168SDaniel Vetter 	 *
2570959a168SDaniel Vetter 	 * Because dma-buf buffers have invariant size over their lifetime, the
2580959a168SDaniel Vetter 	 * dma-buf core checks whether a vma is too large and rejects such
2590959a168SDaniel Vetter 	 * mappings. The exporter hence does not need to duplicate this check.
2600959a168SDaniel Vetter 	 * Drivers do not need to check this themselves.
2610959a168SDaniel Vetter 	 *
2620959a168SDaniel Vetter 	 * If an exporter needs to manually flush caches and hence needs to fake
2630959a168SDaniel Vetter 	 * coherency for mmap support, it needs to be able to zap all the ptes
2640959a168SDaniel Vetter 	 * pointing at the backing storage. Now linux mm needs a struct
2650959a168SDaniel Vetter 	 * address_space associated with the struct file stored in vma->vm_file
2660959a168SDaniel Vetter 	 * to do that with the function unmap_mapping_range. But the dma_buf
2670959a168SDaniel Vetter 	 * framework only backs every dma_buf fd with the anon_file struct file,
2680959a168SDaniel Vetter 	 * i.e. all dma_bufs share the same file.
2690959a168SDaniel Vetter 	 *
2700959a168SDaniel Vetter 	 * Hence exporters need to setup their own file (and address_space)
2710959a168SDaniel Vetter 	 * association by setting vma->vm_file and adjusting vma->vm_pgoff in
2720959a168SDaniel Vetter 	 * the dma_buf mmap callback. In the specific case of a gem driver the
2730959a168SDaniel Vetter 	 * exporter could use the shmem file already provided by gem (and set
2740959a168SDaniel Vetter 	 * vm_pgoff = 0). Exporters can then zap ptes by unmapping the
2750959a168SDaniel Vetter 	 * corresponding range of the struct address_space associated with their
2760959a168SDaniel Vetter 	 * own file.
2770959a168SDaniel Vetter 	 *
2780959a168SDaniel Vetter 	 * This callback is optional.
2790959a168SDaniel Vetter 	 *
2800959a168SDaniel Vetter 	 * Returns:
2810959a168SDaniel Vetter 	 *
2820959a168SDaniel Vetter 	 * 0 on success or a negative error code on failure.
2830959a168SDaniel Vetter 	 */
2844c78513eSDaniel Vetter 	int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
28598f86c9eSDave Airlie 
2867938f421SLucas De Marchi 	int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
2877938f421SLucas De Marchi 	void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
288d15bd7eeSSumit Semwal };
289d15bd7eeSSumit Semwal 
290d15bd7eeSSumit Semwal /**
291d15bd7eeSSumit Semwal  * struct dma_buf - shared buffer object
2922904a8c1SDaniel Vetter  *
2932904a8c1SDaniel Vetter  * This represents a shared buffer, created by calling dma_buf_export(). The
2942904a8c1SDaniel Vetter  * userspace representation is a normal file descriptor, which can be created by
2952904a8c1SDaniel Vetter  * calling dma_buf_fd().
2962904a8c1SDaniel Vetter  *
2972904a8c1SDaniel Vetter  * Shared dma buffers are reference counted using dma_buf_put() and
2982904a8c1SDaniel Vetter  * get_dma_buf().
2992904a8c1SDaniel Vetter  *
300f641d3b5SDaniel Vetter  * Device DMA access is handled by the separate &struct dma_buf_attachment.
301d15bd7eeSSumit Semwal  */
302d15bd7eeSSumit Semwal struct dma_buf {
303d6abed2aSDaniel Vetter 	/**
304d6abed2aSDaniel Vetter 	 * @size:
305d6abed2aSDaniel Vetter 	 *
306d6abed2aSDaniel Vetter 	 * Size of the buffer; invariant over the lifetime of the buffer.
307d6abed2aSDaniel Vetter 	 */
308d15bd7eeSSumit Semwal 	size_t size;
309d6abed2aSDaniel Vetter 
310d6abed2aSDaniel Vetter 	/**
311d6abed2aSDaniel Vetter 	 * @file:
312d6abed2aSDaniel Vetter 	 *
313d6abed2aSDaniel Vetter 	 * File pointer used for sharing buffers across, and for refcounting.
314d6abed2aSDaniel Vetter 	 * See dma_buf_get() and dma_buf_put().
315d6abed2aSDaniel Vetter 	 */
316d15bd7eeSSumit Semwal 	struct file *file;
317d6abed2aSDaniel Vetter 
318d6abed2aSDaniel Vetter 	/**
319d6abed2aSDaniel Vetter 	 * @attachments:
320d6abed2aSDaniel Vetter 	 *
321d6abed2aSDaniel Vetter 	 * List of dma_buf_attachment that denotes all devices attached,
322d6abed2aSDaniel Vetter 	 * protected by &dma_resv lock @resv.
323d6abed2aSDaniel Vetter 	 */
324d15bd7eeSSumit Semwal 	struct list_head attachments;
325d6abed2aSDaniel Vetter 
326d6abed2aSDaniel Vetter 	/** @ops: dma_buf_ops associated with this buffer object. */
327d15bd7eeSSumit Semwal 	const struct dma_buf_ops *ops;
328d6abed2aSDaniel Vetter 
329d6abed2aSDaniel Vetter 	/**
330d6abed2aSDaniel Vetter 	 * @vmapping_counter:
331d6abed2aSDaniel Vetter 	 *
332d6abed2aSDaniel Vetter 	 * Used internally to refcnt the vmaps returned by dma_buf_vmap().
333d6abed2aSDaniel Vetter 	 * Protected by @lock.
334d6abed2aSDaniel Vetter 	 */
335f00b4dadSDaniel Vetter 	unsigned vmapping_counter;
336d6abed2aSDaniel Vetter 
337d6abed2aSDaniel Vetter 	/**
338d6abed2aSDaniel Vetter 	 * @vmap_ptr:
339d6abed2aSDaniel Vetter 	 * The current vmap ptr if @vmapping_counter > 0. Protected by @lock.
340d6abed2aSDaniel Vetter 	 */
3417938f421SLucas De Marchi 	struct iosys_map vmap_ptr;
342d6abed2aSDaniel Vetter 
343d6abed2aSDaniel Vetter 	/**
344d6abed2aSDaniel Vetter 	 * @exp_name:
345d6abed2aSDaniel Vetter 	 *
3463d1ff9dfSRamesh Errabolu 	 * Name of the exporter; useful for debugging. Must not be NULL
347d6abed2aSDaniel Vetter 	 */
34878df9695SSumit Semwal 	const char *exp_name;
349d6abed2aSDaniel Vetter 
350d6abed2aSDaniel Vetter 	/**
351d6abed2aSDaniel Vetter 	 * @name:
352d6abed2aSDaniel Vetter 	 *
3533d1ff9dfSRamesh Errabolu 	 * Userspace-provided name. Default value is NULL. If not NULL,
3543d1ff9dfSRamesh Errabolu 	 * length cannot be longer than DMA_BUF_NAME_LEN, including NIL
3553d1ff9dfSRamesh Errabolu 	 * char. Useful for accounting and debugging. Read/Write accesses
3563d1ff9dfSRamesh Errabolu 	 * are protected by @name_lock
3573d1ff9dfSRamesh Errabolu 	 *
3583d1ff9dfSRamesh Errabolu 	 * See the IOCTLs DMA_BUF_SET_NAME or DMA_BUF_SET_NAME_A/B
359d6abed2aSDaniel Vetter 	 */
360bb2bb903SGreg Hackmann 	const char *name;
361d6abed2aSDaniel Vetter 
362b56ffa58ST.J. Mercier 	/** @name_lock: Spinlock to protect name access for read access. */
3630f50257fSRandy Dunlap 	spinlock_t name_lock;
364d6abed2aSDaniel Vetter 
365d6abed2aSDaniel Vetter 	/**
366d6abed2aSDaniel Vetter 	 * @owner:
367d6abed2aSDaniel Vetter 	 *
368d6abed2aSDaniel Vetter 	 * Pointer to exporter module; used for refcounting when exporter is a
369d6abed2aSDaniel Vetter 	 * kernel module.
370d6abed2aSDaniel Vetter 	 */
3719abdffe2SSumit Semwal 	struct module *owner;
372d6abed2aSDaniel Vetter 
373*bfc7bc53STvrtko Ursulin #if IS_ENABLED(CONFIG_DEBUG_FS)
374d6abed2aSDaniel Vetter 	/** @list_node: node for dma_buf accounting and debugging. */
375b89e3563SSumit Semwal 	struct list_head list_node;
376*bfc7bc53STvrtko Ursulin #endif
377d6abed2aSDaniel Vetter 
378d6abed2aSDaniel Vetter 	/** @priv: exporter specific private data for this buffer object. */
379d15bd7eeSSumit Semwal 	void *priv;
380d6abed2aSDaniel Vetter 
381d6abed2aSDaniel Vetter 	/**
382d6abed2aSDaniel Vetter 	 * @resv:
383d6abed2aSDaniel Vetter 	 *
384d6abed2aSDaniel Vetter 	 * Reservation object linked to this dma-buf.
38505459351SDaniel Vetter 	 *
38605459351SDaniel Vetter 	 * IMPLICIT SYNCHRONIZATION RULES:
38705459351SDaniel Vetter 	 *
38805459351SDaniel Vetter 	 * Drivers which support implicit synchronization of buffer access as
38905459351SDaniel Vetter 	 * e.g. exposed in `Implicit Fence Poll Support`_ must follow the
39005459351SDaniel Vetter 	 * below rules.
39105459351SDaniel Vetter 	 *
39273511edfSChristian König 	 * - Drivers must add a read fence through dma_resv_add_fence() with the
39373511edfSChristian König 	 *   DMA_RESV_USAGE_READ flag for anything the userspace API considers a
39473511edfSChristian König 	 *   read access. This highly depends upon the API and window system.
39505459351SDaniel Vetter 	 *
39673511edfSChristian König 	 * - Similarly drivers must add a write fence through
39773511edfSChristian König 	 *   dma_resv_add_fence() with the DMA_RESV_USAGE_WRITE flag for
39873511edfSChristian König 	 *   anything the userspace API considers write access.
39905459351SDaniel Vetter 	 *
40073511edfSChristian König 	 * - Drivers may just always add a write fence, since that only
401b56ffa58ST.J. Mercier 	 *   causes unnecessary synchronization, but no correctness issues.
40205459351SDaniel Vetter 	 *
40305459351SDaniel Vetter 	 * - Some drivers only expose a synchronous userspace API with no
40405459351SDaniel Vetter 	 *   pipelining across drivers. These do not set any fences for their
40505459351SDaniel Vetter 	 *   access. An example here is v4l.
40605459351SDaniel Vetter 	 *
4077bc80a54SChristian König 	 * - Driver should use dma_resv_usage_rw() when retrieving fences as
4087bc80a54SChristian König 	 *   dependency for implicit synchronization.
4097bc80a54SChristian König 	 *
41005459351SDaniel Vetter 	 * DYNAMIC IMPORTER RULES:
41105459351SDaniel Vetter 	 *
41205459351SDaniel Vetter 	 * Dynamic importers, see dma_buf_attachment_is_dynamic(), have
41305459351SDaniel Vetter 	 * additional constraints on how they set up fences:
41405459351SDaniel Vetter 	 *
41573511edfSChristian König 	 * - Dynamic importers must obey the write fences and wait for them to
41605459351SDaniel Vetter 	 *   signal before allowing access to the buffer's underlying storage
41705459351SDaniel Vetter 	 *   through the device.
41805459351SDaniel Vetter 	 *
41905459351SDaniel Vetter 	 * - Dynamic importers should set fences for any access that they can't
42005459351SDaniel Vetter 	 *   disable immediately from their &dma_buf_attach_ops.move_notify
42105459351SDaniel Vetter 	 *   callback.
422d9edf92dSDaniel Vetter 	 *
423d9edf92dSDaniel Vetter 	 * IMPORTANT:
424d9edf92dSDaniel Vetter 	 *
4257bc80a54SChristian König 	 * All drivers and memory management related functions must obey the
4267bc80a54SChristian König 	 * struct dma_resv rules, specifically the rules for updating and
4277bc80a54SChristian König 	 * obeying fences. See enum dma_resv_usage for further descriptions.
428d6abed2aSDaniel Vetter 	 */
42952791eeeSChristian König 	struct dma_resv *resv;
4309b495a58SMaarten Lankhorst 
431d6abed2aSDaniel Vetter 	/** @poll: for userspace poll support */
4329b495a58SMaarten Lankhorst 	wait_queue_head_t poll;
4339b495a58SMaarten Lankhorst 
434dd66f56cSChristian König 	/** @cb_in: for userspace poll support */
435dd66f56cSChristian König 	/** @cb_out: for userspace poll support */
4369b495a58SMaarten Lankhorst 	struct dma_buf_poll_cb_t {
437f54d1867SChris Wilson 		struct dma_fence_cb cb;
4389b495a58SMaarten Lankhorst 		wait_queue_head_t *poll;
4399b495a58SMaarten Lankhorst 
44001e5d556SAl Viro 		__poll_t active;
4416b51b02aSChristian König 	} cb_in, cb_out;
442bdb8d06dSHridya Valsaraju #ifdef CONFIG_DMABUF_SYSFS_STATS
443d6abed2aSDaniel Vetter 	/**
444d6abed2aSDaniel Vetter 	 * @sysfs_entry:
445d6abed2aSDaniel Vetter 	 *
446d6abed2aSDaniel Vetter 	 * For exposing information about this buffer in sysfs. See also
447d6abed2aSDaniel Vetter 	 * `DMA-BUF statistics`_ for the uapi this enables.
448d6abed2aSDaniel Vetter 	 */
449bdb8d06dSHridya Valsaraju 	struct dma_buf_sysfs_entry {
450bdb8d06dSHridya Valsaraju 		struct kobject kobj;
451bdb8d06dSHridya Valsaraju 		struct dma_buf *dmabuf;
452bdb8d06dSHridya Valsaraju 	} *sysfs_entry;
453bdb8d06dSHridya Valsaraju #endif
454d15bd7eeSSumit Semwal };
455d15bd7eeSSumit Semwal 
456d15bd7eeSSumit Semwal /**
457bb42df46SChristian König  * struct dma_buf_attach_ops - importer operations for an attachment
458bb42df46SChristian König  *
459bb42df46SChristian König  * Attachment operations implemented by the importer.
460bb42df46SChristian König  */
461bb42df46SChristian König struct dma_buf_attach_ops {
462bb42df46SChristian König 	/**
46309606b54SChristian König 	 * @allow_peer2peer:
46409606b54SChristian König 	 *
46509606b54SChristian König 	 * If this is set to true the importer must be able to handle peer
46609606b54SChristian König 	 * resources without struct pages.
46709606b54SChristian König 	 */
46809606b54SChristian König 	bool allow_peer2peer;
46909606b54SChristian König 
47009606b54SChristian König 	/**
4716f49c251SRandy Dunlap 	 * @move_notify: [optional] notification that the DMA-buf is moving
472bb42df46SChristian König 	 *
473bb42df46SChristian König 	 * If this callback is provided the framework can avoid pinning the
474bb42df46SChristian König 	 * backing store while mappings exists.
475bb42df46SChristian König 	 *
476bb42df46SChristian König 	 * This callback is called with the lock of the reservation object
477bb42df46SChristian König 	 * associated with the dma_buf held and the mapping function must be
478bb42df46SChristian König 	 * called with this lock held as well. This makes sure that no mapping
479bb42df46SChristian König 	 * is created concurrently with an ongoing move operation.
480bb42df46SChristian König 	 *
481bb42df46SChristian König 	 * Mappings stay valid and are not directly affected by this callback.
482bb42df46SChristian König 	 * But the DMA-buf can now be in a different physical location, so all
483bb42df46SChristian König 	 * mappings should be destroyed and re-created as soon as possible.
484bb42df46SChristian König 	 *
485bb42df46SChristian König 	 * New mappings can be created after this callback returns, and will
486bb42df46SChristian König 	 * point to the new location of the DMA-buf.
487bb42df46SChristian König 	 */
488bb42df46SChristian König 	void (*move_notify)(struct dma_buf_attachment *attach);
489bb42df46SChristian König };
490bb42df46SChristian König 
491bb42df46SChristian König /**
492d15bd7eeSSumit Semwal  * struct dma_buf_attachment - holds device-buffer attachment data
493d15bd7eeSSumit Semwal  * @dmabuf: buffer for this attachment.
494d15bd7eeSSumit Semwal  * @dev: device attached to the buffer.
49515fd552dSChristian König  * @node: list of dma_buf_attachment, protected by dma_resv lock of the dmabuf.
496f13e143eSChristian König  * @sgt: cached mapping.
497f13e143eSChristian König  * @dir: direction of cached mapping.
49809606b54SChristian König  * @peer2peer: true if the importer can handle peer resources without pages.
499d15bd7eeSSumit Semwal  * @priv: exporter specific attachment data.
500bb42df46SChristian König  * @importer_ops: importer operations for this attachment, if provided
501bb42df46SChristian König  * dma_buf_map/unmap_attachment() must be called with the dma_resv lock held.
502bb42df46SChristian König  * @importer_priv: importer specific attachment data.
503d15bd7eeSSumit Semwal  *
504d15bd7eeSSumit Semwal  * This structure holds the attachment information between the dma_buf buffer
505d15bd7eeSSumit Semwal  * and its user device(s). The list contains one attachment struct per device
506d15bd7eeSSumit Semwal  * attached to the buffer.
5072904a8c1SDaniel Vetter  *
5082904a8c1SDaniel Vetter  * An attachment is created by calling dma_buf_attach(), and released again by
5092904a8c1SDaniel Vetter  * calling dma_buf_detach(). The DMA mapping itself needed to initiate a
5102904a8c1SDaniel Vetter  * transfer is created by dma_buf_map_attachment() and freed again by calling
5112904a8c1SDaniel Vetter  * dma_buf_unmap_attachment().
512d15bd7eeSSumit Semwal  */
513d15bd7eeSSumit Semwal struct dma_buf_attachment {
514d15bd7eeSSumit Semwal 	struct dma_buf *dmabuf;
515d15bd7eeSSumit Semwal 	struct device *dev;
516d15bd7eeSSumit Semwal 	struct list_head node;
517f13e143eSChristian König 	struct sg_table *sgt;
518f13e143eSChristian König 	enum dma_data_direction dir;
51909606b54SChristian König 	bool peer2peer;
520bb42df46SChristian König 	const struct dma_buf_attach_ops *importer_ops;
521bb42df46SChristian König 	void *importer_priv;
522d15bd7eeSSumit Semwal 	void *priv;
523d15bd7eeSSumit Semwal };
524d15bd7eeSSumit Semwal 
525f9a24d1aSRob Clark /**
526d8fbe341SSumit Semwal  * struct dma_buf_export_info - holds information needed to export a dma_buf
5279abdffe2SSumit Semwal  * @exp_name:	name of the exporter - useful for debugging.
5289abdffe2SSumit Semwal  * @owner:	pointer to exporter module - used for refcounting kernel module
529d8fbe341SSumit Semwal  * @ops:	Attach allocator-defined dma buf ops to the new buffer
530476b485bSJianxin Xiong  * @size:	Size of the buffer - invariant over the lifetime of the buffer
531d8fbe341SSumit Semwal  * @flags:	mode flags for the file
532d8fbe341SSumit Semwal  * @resv:	reservation-object, NULL to allocate default one
533d8fbe341SSumit Semwal  * @priv:	Attach private data of allocator to this buffer
534d8fbe341SSumit Semwal  *
535d8fbe341SSumit Semwal  * This structure holds the information required to export the buffer. Used
536d8fbe341SSumit Semwal  * with dma_buf_export() only.
537d8fbe341SSumit Semwal  */
538d8fbe341SSumit Semwal struct dma_buf_export_info {
539d8fbe341SSumit Semwal 	const char *exp_name;
5409abdffe2SSumit Semwal 	struct module *owner;
541d8fbe341SSumit Semwal 	const struct dma_buf_ops *ops;
542d8fbe341SSumit Semwal 	size_t size;
543d8fbe341SSumit Semwal 	int flags;
54452791eeeSChristian König 	struct dma_resv *resv;
545d8fbe341SSumit Semwal 	void *priv;
546d8fbe341SSumit Semwal };
547d8fbe341SSumit Semwal 
548d8fbe341SSumit Semwal /**
5492904a8c1SDaniel Vetter  * DEFINE_DMA_BUF_EXPORT_INFO - helper macro for exporters
550e2082e3aSRob Clark  * @name: export-info name
5512904a8c1SDaniel Vetter  *
552f641d3b5SDaniel Vetter  * DEFINE_DMA_BUF_EXPORT_INFO macro defines the &struct dma_buf_export_info,
5532904a8c1SDaniel Vetter  * zeroes it out and pre-populates exp_name in it.
554d8fbe341SSumit Semwal  */
555e2082e3aSRob Clark #define DEFINE_DMA_BUF_EXPORT_INFO(name)	\
556e2082e3aSRob Clark 	struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \
5579abdffe2SSumit Semwal 					 .owner = THIS_MODULE }
558d8fbe341SSumit Semwal 
559d8fbe341SSumit Semwal /**
560f9a24d1aSRob Clark  * get_dma_buf - convenience wrapper for get_file.
561f9a24d1aSRob Clark  * @dmabuf:	[in]	pointer to dma_buf
562f9a24d1aSRob Clark  *
563f9a24d1aSRob Clark  * Increments the reference count on the dma-buf, needed in case of drivers
564f9a24d1aSRob Clark  * that either need to create additional references to the dmabuf on the
565f9a24d1aSRob Clark  * kernel side.  For example, an exporter that needs to keep a dmabuf ptr
566f9a24d1aSRob Clark  * so that subsequent exports don't create a new dmabuf.
567f9a24d1aSRob Clark  */
get_dma_buf(struct dma_buf * dmabuf)568f9a24d1aSRob Clark static inline void get_dma_buf(struct dma_buf *dmabuf)
569f9a24d1aSRob Clark {
570f9a24d1aSRob Clark 	get_file(dmabuf->file);
571f9a24d1aSRob Clark }
572f9a24d1aSRob Clark 
57315fd552dSChristian König /**
57415fd552dSChristian König  * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
57515fd552dSChristian König  * @dmabuf: the DMA-buf to check
57615fd552dSChristian König  *
57715fd552dSChristian König  * Returns true if a DMA-buf exporter wants to be called with the dma_resv
57815fd552dSChristian König  * locked for the map/unmap callbacks, false if it doesn't wants to be called
57915fd552dSChristian König  * with the lock held.
58015fd552dSChristian König  */
dma_buf_is_dynamic(struct dma_buf * dmabuf)58115fd552dSChristian König static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
58215fd552dSChristian König {
583bd2275eeSChristian König 	return !!dmabuf->ops->pin;
58415fd552dSChristian König }
58515fd552dSChristian König 
58615fd552dSChristian König /**
58715fd552dSChristian König  * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
588a4812d0bSGal Pressman  * mappings
58915fd552dSChristian König  * @attach: the DMA-buf attachment to check
59015fd552dSChristian König  *
59115fd552dSChristian König  * Returns true if a DMA-buf importer wants to call the map/unmap functions with
59215fd552dSChristian König  * the dma_resv lock held.
59315fd552dSChristian König  */
59415fd552dSChristian König static inline bool
dma_buf_attachment_is_dynamic(struct dma_buf_attachment * attach)59515fd552dSChristian König dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
59615fd552dSChristian König {
597bb42df46SChristian König 	return !!attach->importer_ops;
59815fd552dSChristian König }
59915fd552dSChristian König 
600d15bd7eeSSumit Semwal struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
601d15bd7eeSSumit Semwal 					  struct device *dev);
60215fd552dSChristian König struct dma_buf_attachment *
60315fd552dSChristian König dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
604bb42df46SChristian König 		       const struct dma_buf_attach_ops *importer_ops,
605bb42df46SChristian König 		       void *importer_priv);
606d15bd7eeSSumit Semwal void dma_buf_detach(struct dma_buf *dmabuf,
60715fd552dSChristian König 		    struct dma_buf_attachment *attach);
608bb42df46SChristian König int dma_buf_pin(struct dma_buf_attachment *attach);
609bb42df46SChristian König void dma_buf_unpin(struct dma_buf_attachment *attach);
61078df9695SSumit Semwal 
611d8fbe341SSumit Semwal struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
61278df9695SSumit Semwal 
61355c1c4caSDave Airlie int dma_buf_fd(struct dma_buf *dmabuf, int flags);
614d15bd7eeSSumit Semwal struct dma_buf *dma_buf_get(int fd);
615d15bd7eeSSumit Semwal void dma_buf_put(struct dma_buf *dmabuf);
616d15bd7eeSSumit Semwal 
617d15bd7eeSSumit Semwal struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
618d15bd7eeSSumit Semwal 					enum dma_data_direction);
61933ea2dcbSSumit Semwal void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
62033ea2dcbSSumit Semwal 				enum dma_data_direction);
62115fd552dSChristian König void dma_buf_move_notify(struct dma_buf *dma_buf);
622831e9da7STiago Vignatti int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
623fc13020eSDaniel Vetter 			     enum dma_data_direction dir);
62418b862dcSChris Wilson int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
625fc13020eSDaniel Vetter 			   enum dma_data_direction dir);
62619d6634dSDmitry Osipenko struct sg_table *
62719d6634dSDmitry Osipenko dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
62819d6634dSDmitry Osipenko 				enum dma_data_direction direction);
62919d6634dSDmitry Osipenko void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
63019d6634dSDmitry Osipenko 				       struct sg_table *sg_table,
63119d6634dSDmitry Osipenko 				       enum dma_data_direction direction);
6324c78513eSDaniel Vetter 
6334c78513eSDaniel Vetter int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
6344c78513eSDaniel Vetter 		 unsigned long);
6357938f421SLucas De Marchi int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map);
6367938f421SLucas De Marchi void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map);
63756e5abbaSDmitry Osipenko int dma_buf_vmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map);
63856e5abbaSDmitry Osipenko void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map);
639d15bd7eeSSumit Semwal #endif /* __DMA_BUF_H__ */
640