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