File Index
Files and directories
Sign in
FILE

dma-buf.h

File preview with syntax highlighting.

linux-6.19.14/include/linux/dma-buf.h OBJECTIVE-C
  1/* SPDX-License-Identifier: GPL-2.0-only */
  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.
 12 */
 13#ifndef __DMA_BUF_H__
 14#define __DMA_BUF_H__
 15
 16#include <linux/iosys-map.h>
 17#include <linux/file.h>
 18#include <linux/err.h>
 19#include <linux/scatterlist.h>
 20#include <linux/list.h>
 21#include <linux/dma-mapping.h>
 22#include <linux/fs.h>
 23#include <linux/dma-fence.h>
 24#include <linux/wait.h>
 25#include <linux/pci-p2pdma.h>
 26
 27struct device;
 28struct dma_buf;
 29struct dma_buf_attachment;
 30
 31/**
 32 * struct dma_buf_ops - operations possible on struct dma_buf
 33 * @vmap: [optional] creates a virtual mapping for the buffer into kernel
 34 *	  address space. Same restrictions as for vmap and friends apply.
 35 * @vunmap: [optional] unmaps a vmap from the buffer
 36 */
 37struct dma_buf_ops {
 38	/**
 39	 * @attach:
 40	 *
 41	 * This is called from dma_buf_attach() to make sure that a given
 42	 * &dma_buf_attachment.dev can access the provided &dma_buf. Exporters
 43	 * which support buffer objects in special locations like VRAM or
 44	 * device-specific carveout areas should check whether the buffer could
 45	 * be move to system memory (or directly accessed by the provided
 46	 * device), and otherwise need to fail the attach operation.
 47	 *
 48	 * The exporter should also in general check whether the current
 49	 * allocation fulfills the DMA constraints of the new device. If this
 50	 * is not the case, and the allocation cannot be moved, it should also
 51	 * fail the attach operation.
 52	 *
 53	 * Any exporter-private housekeeping data can be stored in the
 54	 * &dma_buf_attachment.priv pointer.
 55	 *
 56	 * This callback is optional.
 57	 *
 58	 * Returns:
 59	 *
 60	 * 0 on success, negative error code on failure. It might return -EBUSY
 61	 * to signal that backing storage is already allocated and incompatible
 62	 * with the requirements of requesting device.
 63	 */
 64	int (*attach)(struct dma_buf *, struct dma_buf_attachment *);
 65
 66	/**
 67	 * @detach:
 68	 *
 69	 * This is called by dma_buf_detach() to release a &dma_buf_attachment.
 70	 * Provided so that exporters can clean up any housekeeping for an
 71	 * &dma_buf_attachment.
 72	 *
 73	 * This callback is optional.
 74	 */
 75	void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
 76
 77	/**
 78	 * @pin:
 79	 *
 80	 * This is called by dma_buf_pin() and lets the exporter know that the
 81	 * DMA-buf can't be moved any more. Ideally, the exporter should
 82	 * pin the buffer so that it is generally accessible by all
 83	 * devices.
 84	 *
 85	 * This is called with the &dmabuf.resv object locked and is mutual
 86	 * exclusive with @cache_sgt_mapping.
 87	 *
 88	 * This is called automatically for non-dynamic importers from
 89	 * dma_buf_attach().
 90	 *
 91	 * Note that similar to non-dynamic exporters in their @map_dma_buf
 92	 * callback the driver must guarantee that the memory is available for
 93	 * use and cleared of any old data by the time this function returns.
 94	 * Drivers which pipeline their buffer moves internally must wait for
 95	 * all moves and clears to complete.
 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	 *
106	 * This is called by dma_buf_unpin() and lets the exporter know that the
107	 * DMA-buf can be moved again.
108	 *
109	 * This is called with the dmabuf->resv object locked and is mutual
110	 * exclusive with @cache_sgt_mapping.
111	 *
112	 * This callback is optional.
113	 */
114	void (*unpin)(struct dma_buf_attachment *attach);
115
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
121	 * can only be called if @attach has been called successfully.
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
129	 * &device.dma_params from the &dma_buf_attachment. The @attach callback
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	 *
142	 * This is always called with the dmabuf->resv object locked when
143	 * the dynamic_mapping flag is true.
144	 *
145	 * Note that for non-dynamic exporters the driver must guarantee that
146	 * that the memory is available for use and cleared of any old data by
147	 * the time this function returns.  Drivers which pipeline their buffer
148	 * moves internally must wait for all moves and clears to complete.
149	 * Dynamic exporters do not need to follow this rule: For non-dynamic
150	 * importers the buffer is already pinned through @pin, which has the
151	 * same requirements. Dynamic importers otoh are required to obey the
152	 * dma_resv fences.
153	 *
154	 * Returns:
155	 *
156	 * A &sg_table scatter list of 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. The addresses and lengths in
159	 * the scatter list are PAGE_SIZE aligned.
160	 *
161	 * On failure, returns a negative error value wrapped into a pointer.
162	 * May also return -EINTR when a signal was received while being
163	 * blocked.
164	 *
165	 * Note that exporters should not try to cache the scatter list, or
166	 * return the same one for multiple calls. Caching is done either by the
167	 * DMA-BUF code (for non-dynamic importers) or the importer. Ownership
168	 * of the scatter list is transferred to the caller, and returned by
169	 * @unmap_dma_buf.
170	 */
171	struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
172					 enum dma_data_direction);
173	/**
174	 * @unmap_dma_buf:
175	 *
176	 * This is called by dma_buf_unmap_attachment() and should unmap and
177	 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
178	 * For static dma_buf handling this might also unpin the backing
179	 * storage if this is the last mapping of the DMA buffer.
180	 */
181	void (*unmap_dma_buf)(struct dma_buf_attachment *,
182			      struct sg_table *,
183			      enum dma_data_direction);
184
185	/* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
186	 * if the call would block.
187	 */
188
189	/**
190	 * @release:
191	 *
192	 * Called after the last dma_buf_put to release the &dma_buf, and
193	 * mandatory.
194	 */
195	void (*release)(struct dma_buf *);
196
197	/**
198	 * @begin_cpu_access:
199	 *
200	 * This is called from dma_buf_begin_cpu_access() and allows the
201	 * exporter to ensure that the memory is actually coherent for cpu
202	 * access. The exporter also needs to ensure that cpu access is coherent
203	 * for the access direction. The direction can be used by the exporter
204	 * to optimize the cache flushing, i.e. access with a different
205	 * direction (read instead of write) might return stale or even bogus
206	 * data (e.g. when the exporter needs to copy the data to temporary
207	 * storage).
208	 *
209	 * Note that this is both called through the DMA_BUF_IOCTL_SYNC IOCTL
210	 * command for userspace mappings established through @mmap, and also
211	 * for kernel mappings established with @vmap.
212	 *
213	 * This callback is optional.
214	 *
215	 * Returns:
216	 *
217	 * 0 on success or a negative error code on failure. This can for
218	 * example fail when the backing storage can't be allocated. Can also
219	 * return -ERESTARTSYS or -EINTR when the call has been interrupted and
220	 * needs to be restarted.
221	 */
222	int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
223
224	/**
225	 * @end_cpu_access:
226	 *
227	 * This is called from dma_buf_end_cpu_access() when the importer is
228	 * done accessing the CPU. The exporter can use this to flush caches and
229	 * undo anything else done in @begin_cpu_access.
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	 */
239	int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
240
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 bracket 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	 */
276	int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
277
278	int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
279	void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
280};
281
282/**
283 * struct dma_buf - shared buffer object
284 *
285 * This represents a shared buffer, created by calling dma_buf_export(). The
286 * userspace representation is a normal file descriptor, which can be created by
287 * calling dma_buf_fd().
288 *
289 * Shared dma buffers are reference counted using dma_buf_put() and
290 * get_dma_buf().
291 *
292 * Device DMA access is handled by the separate &struct dma_buf_attachment.
293 */
294struct dma_buf {
295	/**
296	 * @size:
297	 *
298	 * Size of the buffer; invariant over the lifetime of the buffer.
299	 */
300	size_t size;
301
302	/**
303	 * @file:
304	 *
305	 * File pointer used for sharing buffers across, and for refcounting.
306	 * See dma_buf_get() and dma_buf_put().
307	 */
308	struct file *file;
309
310	/**
311	 * @attachments:
312	 *
313	 * List of dma_buf_attachment that denotes all devices attached,
314	 * protected by &dma_resv lock @resv.
315	 */
316	struct list_head attachments;
317
318	/** @ops: dma_buf_ops associated with this buffer object. */
319	const struct dma_buf_ops *ops;
320
321	/**
322	 * @vmapping_counter:
323	 *
324	 * Used internally to refcnt the vmaps returned by dma_buf_vmap().
325	 * Protected by @lock.
326	 */
327	unsigned vmapping_counter;
328
329	/**
330	 * @vmap_ptr:
331	 * The current vmap ptr if @vmapping_counter > 0. Protected by @lock.
332	 */
333	struct iosys_map vmap_ptr;
334
335	/**
336	 * @exp_name:
337	 *
338	 * Name of the exporter; useful for debugging. Must not be NULL
339	 */
340	const char *exp_name;
341
342	/**
343	 * @name:
344	 *
345	 * Userspace-provided name. Default value is NULL. If not NULL,
346	 * length cannot be longer than DMA_BUF_NAME_LEN, including NIL
347	 * char. Useful for accounting and debugging. Read/Write accesses
348	 * are protected by @name_lock
349	 *
350	 * See the IOCTLs DMA_BUF_SET_NAME or DMA_BUF_SET_NAME_A/B
351	 */
352	const char *name;
353
354	/** @name_lock: Spinlock to protect name access for read access. */
355	spinlock_t name_lock;
356
357	/**
358	 * @owner:
359	 *
360	 * Pointer to exporter module; used for refcounting when exporter is a
361	 * kernel module.
362	 */
363	struct module *owner;
364
365	/** @list_node: node for dma_buf accounting and debugging. */
366	struct list_head list_node;
367
368	/** @priv: exporter specific private data for this buffer object. */
369	void *priv;
370
371	/**
372	 * @resv:
373	 *
374	 * Reservation object linked to this dma-buf.
375	 *
376	 * IMPLICIT SYNCHRONIZATION RULES:
377	 *
378	 * Drivers which support implicit synchronization of buffer access as
379	 * e.g. exposed in `Implicit Fence Poll Support`_ must follow the
380	 * below rules.
381	 *
382	 * - Drivers must add a read fence through dma_resv_add_fence() with the
383	 *   DMA_RESV_USAGE_READ flag for anything the userspace API considers a
384	 *   read access. This highly depends upon the API and window system.
385	 *
386	 * - Similarly drivers must add a write fence through
387	 *   dma_resv_add_fence() with the DMA_RESV_USAGE_WRITE flag for
388	 *   anything the userspace API considers write access.
389	 *
390	 * - Drivers may just always add a write fence, since that only
391	 *   causes unnecessary synchronization, but no correctness issues.
392	 *
393	 * - Some drivers only expose a synchronous userspace API with no
394	 *   pipelining across drivers. These do not set any fences for their
395	 *   access. An example here is v4l.
396	 *
397	 * - Driver should use dma_resv_usage_rw() when retrieving fences as
398	 *   dependency for implicit synchronization.
399	 *
400	 * DYNAMIC IMPORTER RULES:
401	 *
402	 * Dynamic importers, see dma_buf_attachment_is_dynamic(), have
403	 * additional constraints on how they set up fences:
404	 *
405	 * - Dynamic importers must obey the write fences and wait for them to
406	 *   signal before allowing access to the buffer's underlying storage
407	 *   through the device.
408	 *
409	 * - Dynamic importers should set fences for any access that they can't
410	 *   disable immediately from their &dma_buf_attach_ops.move_notify
411	 *   callback.
412	 *
413	 * IMPORTANT:
414	 *
415	 * All drivers and memory management related functions must obey the
416	 * struct dma_resv rules, specifically the rules for updating and
417	 * obeying fences. See enum dma_resv_usage for further descriptions.
418	 */
419	struct dma_resv *resv;
420
421	/** @poll: for userspace poll support */
422	wait_queue_head_t poll;
423
424	/** @cb_in: for userspace poll support */
425	/** @cb_out: for userspace poll support */
426	struct dma_buf_poll_cb_t {
427		struct dma_fence_cb cb;
428		wait_queue_head_t *poll;
429
430		__poll_t active;
431	} cb_in, cb_out;
432#ifdef CONFIG_DMABUF_SYSFS_STATS
433	/**
434	 * @sysfs_entry:
435	 *
436	 * For exposing information about this buffer in sysfs. See also
437	 * `DMA-BUF statistics`_ for the uapi this enables.
438	 */
439	struct dma_buf_sysfs_entry {
440		struct kobject kobj;
441		struct dma_buf *dmabuf;
442	} *sysfs_entry;
443#endif
444};
445
446/**
447 * struct dma_buf_attach_ops - importer operations for an attachment
448 *
449 * Attachment operations implemented by the importer.
450 */
451struct dma_buf_attach_ops {
452	/**
453	 * @allow_peer2peer:
454	 *
455	 * If this is set to true the importer must be able to handle peer
456	 * resources without struct pages.
457	 */
458	bool allow_peer2peer;
459
460	/**
461	 * @move_notify: [optional] notification that the DMA-buf is moving
462	 *
463	 * If this callback is provided the framework can avoid pinning the
464	 * backing store while mappings exists.
465	 *
466	 * This callback is called with the lock of the reservation object
467	 * associated with the dma_buf held and the mapping function must be
468	 * called with this lock held as well. This makes sure that no mapping
469	 * is created concurrently with an ongoing move operation.
470	 *
471	 * Mappings stay valid and are not directly affected by this callback.
472	 * But the DMA-buf can now be in a different physical location, so all
473	 * mappings should be destroyed and re-created as soon as possible.
474	 *
475	 * New mappings can be created after this callback returns, and will
476	 * point to the new location of the DMA-buf.
477	 */
478	void (*move_notify)(struct dma_buf_attachment *attach);
479};
480
481/**
482 * struct dma_buf_attachment - holds device-buffer attachment data
483 * @dmabuf: buffer for this attachment.
484 * @dev: device attached to the buffer.
485 * @node: list of dma_buf_attachment, protected by dma_resv lock of the dmabuf.
486 * @peer2peer: true if the importer can handle peer resources without pages.
487 * @priv: exporter specific attachment data.
488 * @importer_ops: importer operations for this attachment, if provided
489 * dma_buf_map/unmap_attachment() must be called with the dma_resv lock held.
490 * @importer_priv: importer specific attachment data.
491 *
492 * This structure holds the attachment information between the dma_buf buffer
493 * and its user device(s). The list contains one attachment struct per device
494 * attached to the buffer.
495 *
496 * An attachment is created by calling dma_buf_attach(), and released again by
497 * calling dma_buf_detach(). The DMA mapping itself needed to initiate a
498 * transfer is created by dma_buf_map_attachment() and freed again by calling
499 * dma_buf_unmap_attachment().
500 */
501struct dma_buf_attachment {
502	struct dma_buf *dmabuf;
503	struct device *dev;
504	struct list_head node;
505	bool peer2peer;
506	const struct dma_buf_attach_ops *importer_ops;
507	void *importer_priv;
508	void *priv;
509};
510
511/**
512 * struct dma_buf_export_info - holds information needed to export a dma_buf
513 * @exp_name:	name of the exporter - useful for debugging.
514 * @owner:	pointer to exporter module - used for refcounting kernel module
515 * @ops:	Attach allocator-defined dma buf ops to the new buffer
516 * @size:	Size of the buffer - invariant over the lifetime of the buffer
517 * @flags:	mode flags for the file
518 * @resv:	reservation-object, NULL to allocate default one
519 * @priv:	Attach private data of allocator to this buffer
520 *
521 * This structure holds the information required to export the buffer. Used
522 * with dma_buf_export() only.
523 */
524struct dma_buf_export_info {
525	const char *exp_name;
526	struct module *owner;
527	const struct dma_buf_ops *ops;
528	size_t size;
529	int flags;
530	struct dma_resv *resv;
531	void *priv;
532};
533
534/**
535 * struct dma_buf_phys_vec - describe continuous chunk of memory
536 * @paddr:   physical address of that chunk
537 * @len:     Length of this chunk
538 */
539struct dma_buf_phys_vec {
540	phys_addr_t paddr;
541	size_t len;
542};
543
544/**
545 * DEFINE_DMA_BUF_EXPORT_INFO - helper macro for exporters
546 * @name: export-info name
547 *
548 * DEFINE_DMA_BUF_EXPORT_INFO macro defines the &struct dma_buf_export_info,
549 * zeroes it out and pre-populates exp_name in it.
550 */
551#define DEFINE_DMA_BUF_EXPORT_INFO(name)	\
552	struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \
553					 .owner = THIS_MODULE }
554
555/**
556 * get_dma_buf - convenience wrapper for get_file.
557 * @dmabuf:	[in]	pointer to dma_buf
558 *
559 * Increments the reference count on the dma-buf, needed in case of drivers
560 * that either need to create additional references to the dmabuf on the
561 * kernel side.  For example, an exporter that needs to keep a dmabuf ptr
562 * so that subsequent exports don't create a new dmabuf.
563 */
564static inline void get_dma_buf(struct dma_buf *dmabuf)
565{
566	get_file(dmabuf->file);
567}
568
569/**
570 * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
571 * @dmabuf: the DMA-buf to check
572 *
573 * Returns true if a DMA-buf exporter wants to be called with the dma_resv
574 * locked for the map/unmap callbacks, false if it doesn't wants to be called
575 * with the lock held.
576 */
577static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
578{
579	return !!dmabuf->ops->pin;
580}
581
582struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
583					  struct device *dev);
584struct dma_buf_attachment *
585dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
586		       const struct dma_buf_attach_ops *importer_ops,
587		       void *importer_priv);
588void dma_buf_detach(struct dma_buf *dmabuf,
589		    struct dma_buf_attachment *attach);
590int dma_buf_pin(struct dma_buf_attachment *attach);
591void dma_buf_unpin(struct dma_buf_attachment *attach);
592
593struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
594
595int dma_buf_fd(struct dma_buf *dmabuf, int flags);
596struct dma_buf *dma_buf_get(int fd);
597void dma_buf_put(struct dma_buf *dmabuf);
598
599struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
600					enum dma_data_direction);
601void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
602				enum dma_data_direction);
603void dma_buf_move_notify(struct dma_buf *dma_buf);
604int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
605			     enum dma_data_direction dir);
606int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
607			   enum dma_data_direction dir);
608struct sg_table *
609dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
610				enum dma_data_direction direction);
611void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
612				       struct sg_table *sg_table,
613				       enum dma_data_direction direction);
614
615int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
616		 unsigned long);
617int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map);
618void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map);
619int dma_buf_vmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map);
620void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map);
621struct dma_buf *dma_buf_iter_begin(void);
622struct dma_buf *dma_buf_iter_next(struct dma_buf *dmbuf);
623#endif /* __DMA_BUF_H__ */
~/ Read-only file preview.