1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Header file for dma buffer sharing framework.
5 * Copyright(C) 2011 Linaro Limited. All rights reserved.
6 * Author: Sumit Semwal <sumit.semwal@ti.com>
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.
16 #include <linux/file.h>
17 #include <linux/err.h>
18 #include <linux/scatterlist.h>
19 #include <linux/list.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/dma-fence.h>
23 #include <linux/wait.h>
27 struct dma_buf_attachment;
30 * struct dma_buf_ops - operations possible on struct dma_buf
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
39 * If true the framework will cache the first mapping made for each
40 * attachment. This avoids creating mappings for attachments multiple
43 bool cache_sgt_mapping;
48 * If true the framework makes sure that the map/unmap_dma_buf
49 * callbacks are always called with the dma_resv object locked.
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.
60 * This is called from dma_buf_attach() to make sure that a given
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.
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.
72 * Any exporter-private housekeeping data can be stored in the
73 * &dma_buf_attachment.priv pointer.
75 * This callback is optional.
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.
83 int (*attach)(struct dma_buf *, struct dma_buf_attachment *);
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.
92 * This callback is optional.
94 void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
99 * This is called by dma_buf_map_attachment() and is used to map a
100 * shared &dma_buf into device address space, and it is mandatory. It
101 * can only be called if @attach has been called successfully. This
102 * essentially pins the DMA buffer into place, and it cannot be moved
105 * This call may sleep, e.g. when the backing storage first needs to be
106 * allocated, or moved to a location suitable for all currently attached
109 * Note that any specific buffer attributes required for this function
110 * should get added to device_dma_parameters accessible via
111 * &device.dma_params from the &dma_buf_attachment. The @attach callback
112 * should also check these constraints.
114 * If this is being called for the first time, the exporter can now
115 * choose to scan through the list of attachments for this buffer,
116 * collate the requirements of the attached devices, and choose an
117 * appropriate backing storage for the buffer.
119 * Based on enum dma_data_direction, it might be possible to have
120 * multiple users accessing at the same time (for reading, maybe), or
121 * any other kind of sharing that the exporter might wish to make
122 * available to buffer-users.
124 * This is always called with the dmabuf->resv object locked when
125 * the dynamic_mapping flag is true.
129 * A &sg_table scatter list of or the backing storage of the DMA buffer,
130 * already mapped into the device address space of the &device attached
131 * with the provided &dma_buf_attachment.
133 * On failure, returns a negative error value wrapped into a pointer.
134 * May also return -EINTR when a signal was received while being
137 struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
138 enum dma_data_direction);
142 * This is called by dma_buf_unmap_attachment() and should unmap and
143 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
144 * It should also unpin the backing storage if this is the last mapping
145 * of the DMA buffer, it the exporter supports backing storage
148 void (*unmap_dma_buf)(struct dma_buf_attachment *,
150 enum dma_data_direction);
152 /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
153 * if the call would block.
159 * Called after the last dma_buf_put to release the &dma_buf, and
162 void (*release)(struct dma_buf *);
167 * This is called from dma_buf_begin_cpu_access() and allows the
168 * exporter to ensure that the memory is actually available for cpu
169 * access - the exporter might need to allocate or swap-in and pin the
170 * backing storage. The exporter also needs to ensure that cpu access is
171 * coherent for the access direction. The direction can be used by the
172 * exporter to optimize the cache flushing, i.e. access with a different
173 * direction (read instead of write) might return stale or even bogus
174 * data (e.g. when the exporter needs to copy the data to temporary
177 * This callback is optional.
179 * FIXME: This is both called through the DMA_BUF_IOCTL_SYNC command
180 * from userspace (where storage shouldn't be pinned to avoid handing
181 * de-factor mlock rights to userspace) and for the kernel-internal
182 * users of the various kmap interfaces, where the backing storage must
183 * be pinned to guarantee that the atomic kmap calls can succeed. Since
184 * there's no in-kernel users of the kmap interfaces yet this isn't a
189 * 0 on success or a negative error code on failure. This can for
190 * example fail when the backing storage can't be allocated. Can also
191 * return -ERESTARTSYS or -EINTR when the call has been interrupted and
192 * needs to be restarted.
194 int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
199 * This is called from dma_buf_end_cpu_access() when the importer is
200 * done accessing the CPU. The exporter can use this to flush caches and
201 * unpin any resources pinned in @begin_cpu_access.
202 * The result of any dma_buf kmap calls after end_cpu_access is
205 * This callback is optional.
209 * 0 on success or a negative error code on failure. Can return
210 * -ERESTARTSYS or -EINTR when the call has been interrupted and needs
213 int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
218 * This callback is used by the dma_buf_mmap() function
220 * Note that the mapping needs to be incoherent, userspace is expected
221 * to braket CPU access using the DMA_BUF_IOCTL_SYNC interface.
223 * Because dma-buf buffers have invariant size over their lifetime, the
224 * dma-buf core checks whether a vma is too large and rejects such
225 * mappings. The exporter hence does not need to duplicate this check.
226 * Drivers do not need to check this themselves.
228 * If an exporter needs to manually flush caches and hence needs to fake
229 * coherency for mmap support, it needs to be able to zap all the ptes
230 * pointing at the backing storage. Now linux mm needs a struct
231 * address_space associated with the struct file stored in vma->vm_file
232 * to do that with the function unmap_mapping_range. But the dma_buf
233 * framework only backs every dma_buf fd with the anon_file struct file,
234 * i.e. all dma_bufs share the same file.
236 * Hence exporters need to setup their own file (and address_space)
237 * association by setting vma->vm_file and adjusting vma->vm_pgoff in
238 * the dma_buf mmap callback. In the specific case of a gem driver the
239 * exporter could use the shmem file already provided by gem (and set
240 * vm_pgoff = 0). Exporters can then zap ptes by unmapping the
241 * corresponding range of the struct address_space associated with their
244 * This callback is optional.
248 * 0 on success or a negative error code on failure.
250 int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
255 * Maps a page from the buffer into kernel address space. The page is
256 * specified by offset into the buffer in PAGE_SIZE units.
258 * This callback is optional.
262 * Virtual address pointer where requested page can be accessed. NULL
263 * on error or when this function is unimplemented by the exporter.
265 void *(*map)(struct dma_buf *, unsigned long);
270 * Unmaps a page from the buffer. Page offset and address pointer should
271 * be the same as the one passed to and returned by matching call to map.
273 * This callback is optional.
275 void (*unmap)(struct dma_buf *, unsigned long, void *);
277 void *(*vmap)(struct dma_buf *);
278 void (*vunmap)(struct dma_buf *, void *vaddr);
282 * struct dma_buf - shared buffer object
283 * @size: size of the buffer
284 * @file: file pointer used for sharing buffers across, and for refcounting.
285 * @attachments: list of dma_buf_attachment that denotes all devices attached,
286 * protected by dma_resv lock.
287 * @ops: dma_buf_ops associated with this buffer object.
288 * @lock: used internally to serialize list manipulation, attach/detach and
290 * @vmapping_counter: used internally to refcnt the vmaps
291 * @vmap_ptr: the current vmap ptr if vmapping_counter > 0
292 * @exp_name: name of the exporter; useful for debugging.
293 * @name: userspace-provided name; useful for accounting and debugging,
294 * protected by @resv.
295 * @owner: pointer to exporter module; used for refcounting when exporter is a
297 * @list_node: node for dma_buf accounting and debugging.
298 * @priv: exporter specific private data for this buffer object.
299 * @resv: reservation object linked to this dma-buf
300 * @poll: for userspace poll support
301 * @cb_excl: for userspace poll support
302 * @cb_shared: for userspace poll support
304 * This represents a shared buffer, created by calling dma_buf_export(). The
305 * userspace representation is a normal file descriptor, which can be created by
306 * calling dma_buf_fd().
308 * Shared dma buffers are reference counted using dma_buf_put() and
311 * Device DMA access is handled by the separate &struct dma_buf_attachment.
316 struct list_head attachments;
317 const struct dma_buf_ops *ops;
319 unsigned vmapping_counter;
321 const char *exp_name;
323 struct module *owner;
324 struct list_head list_node;
326 struct dma_resv *resv;
329 wait_queue_head_t poll;
331 struct dma_buf_poll_cb_t {
332 struct dma_fence_cb cb;
333 wait_queue_head_t *poll;
336 } cb_excl, cb_shared;
340 * struct dma_buf_attachment - holds device-buffer attachment data
341 * @dmabuf: buffer for this attachment.
342 * @dev: device attached to the buffer.
343 * @node: list of dma_buf_attachment, protected by dma_resv lock of the dmabuf.
344 * @sgt: cached mapping.
345 * @dir: direction of cached mapping.
346 * @priv: exporter specific attachment data.
347 * @dynamic_mapping: true if dma_buf_map/unmap_attachment() is called with the
348 * dma_resv lock held.
350 * This structure holds the attachment information between the dma_buf buffer
351 * and its user device(s). The list contains one attachment struct per device
352 * attached to the buffer.
354 * An attachment is created by calling dma_buf_attach(), and released again by
355 * calling dma_buf_detach(). The DMA mapping itself needed to initiate a
356 * transfer is created by dma_buf_map_attachment() and freed again by calling
357 * dma_buf_unmap_attachment().
359 struct dma_buf_attachment {
360 struct dma_buf *dmabuf;
362 struct list_head node;
363 struct sg_table *sgt;
364 enum dma_data_direction dir;
365 bool dynamic_mapping;
370 * struct dma_buf_export_info - holds information needed to export a dma_buf
371 * @exp_name: name of the exporter - useful for debugging.
372 * @owner: pointer to exporter module - used for refcounting kernel module
373 * @ops: Attach allocator-defined dma buf ops to the new buffer
374 * @size: Size of the buffer
375 * @flags: mode flags for the file
376 * @resv: reservation-object, NULL to allocate default one
377 * @priv: Attach private data of allocator to this buffer
379 * This structure holds the information required to export the buffer. Used
380 * with dma_buf_export() only.
382 struct dma_buf_export_info {
383 const char *exp_name;
384 struct module *owner;
385 const struct dma_buf_ops *ops;
388 struct dma_resv *resv;
393 * DEFINE_DMA_BUF_EXPORT_INFO - helper macro for exporters
394 * @name: export-info name
396 * DEFINE_DMA_BUF_EXPORT_INFO macro defines the &struct dma_buf_export_info,
397 * zeroes it out and pre-populates exp_name in it.
399 #define DEFINE_DMA_BUF_EXPORT_INFO(name) \
400 struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \
401 .owner = THIS_MODULE }
404 * get_dma_buf - convenience wrapper for get_file.
405 * @dmabuf: [in] pointer to dma_buf
407 * Increments the reference count on the dma-buf, needed in case of drivers
408 * that either need to create additional references to the dmabuf on the
409 * kernel side. For example, an exporter that needs to keep a dmabuf ptr
410 * so that subsequent exports don't create a new dmabuf.
412 static inline void get_dma_buf(struct dma_buf *dmabuf)
414 get_file(dmabuf->file);
418 * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
419 * @dmabuf: the DMA-buf to check
421 * Returns true if a DMA-buf exporter wants to be called with the dma_resv
422 * locked for the map/unmap callbacks, false if it doesn't wants to be called
423 * with the lock held.
425 static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
427 return dmabuf->ops->dynamic_mapping;
431 * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
433 * @attach: the DMA-buf attachment to check
435 * Returns true if a DMA-buf importer wants to call the map/unmap functions with
436 * the dma_resv lock held.
439 dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
441 return attach->dynamic_mapping;
444 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
446 struct dma_buf_attachment *
447 dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
448 bool dynamic_mapping);
449 void dma_buf_detach(struct dma_buf *dmabuf,
450 struct dma_buf_attachment *attach);
452 struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
454 int dma_buf_fd(struct dma_buf *dmabuf, int flags);
455 struct dma_buf *dma_buf_get(int fd);
456 void dma_buf_put(struct dma_buf *dmabuf);
458 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
459 enum dma_data_direction);
460 void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
461 enum dma_data_direction);
462 void dma_buf_move_notify(struct dma_buf *dma_buf);
463 int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
464 enum dma_data_direction dir);
465 int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
466 enum dma_data_direction dir);
467 void *dma_buf_kmap(struct dma_buf *, unsigned long);
468 void dma_buf_kunmap(struct dma_buf *, unsigned long, void *);
470 int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
472 void *dma_buf_vmap(struct dma_buf *);
473 void dma_buf_vunmap(struct dma_buf *, void *vaddr);
474 #endif /* __DMA_BUF_H__ */