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_flags:
324 *
325 * This is called by dma_buf_get_flags and is used to get the buffer's
326 * flags.
327 * This callback is optional.
328 *
329 * Returns:
330 *
331 * 0 on success or a negative error code on failure. On success flags
332 * will be populated with the buffer's flags.
333 */
334 int (*get_flags)(struct dma_buf *dmabuf, unsigned long *flags);
335 };
336
337 /**
338 * struct dma_buf - shared buffer object
339 * @size: size of the buffer
340 * @file: file pointer used for sharing buffers across, and for refcounting.
341 * @attachments: list of dma_buf_attachment that denotes all devices attached.
342 * @ops: dma_buf_ops associated with this buffer object.
343 * @lock: used internally to serialize list manipulation, attach/detach and
344 * vmap/unmap, and accesses to name
345 * @vmapping_counter: used internally to refcnt the vmaps
346 * @vmap_ptr: the current vmap ptr if vmapping_counter > 0
347 * @exp_name: name of the exporter; useful for debugging.
348 * @name: userspace-provided name; useful for accounting and debugging.
349 * @owner: pointer to exporter module; used for refcounting when exporter is a
350 * kernel module.
351 * @list_node: node for dma_buf accounting and debugging.
352 * @priv: exporter specific private data for this buffer object.
353 * @resv: reservation object linked to this dma-buf
354 * @poll: for userspace poll support
355 * @cb_excl: for userspace poll support
356 * @cb_shared: for userspace poll support
357 *
358 * This represents a shared buffer, created by calling dma_buf_export(). The
359 * userspace representation is a normal file descriptor, which can be created by
360 * calling dma_buf_fd().
361 *
362 * Shared dma buffers are reference counted using dma_buf_put() and
363 * get_dma_buf().
364 *
365 * Device DMA access is handled by the separate &struct dma_buf_attachment.
366 */
367 struct dma_buf {
368 size_t size;
369 struct file *file;
370 struct list_head attachments;
371 const struct dma_buf_ops *ops;
372 struct mutex lock;
373 unsigned vmapping_counter;
374 void *vmap_ptr;
375 const char *exp_name;
376 const char *name;
377 struct module *owner;
378 struct list_head list_node;
379 void *priv;
380 struct dma_resv *resv;
381
382 /* poll support */
383 wait_queue_head_t poll;
384
385 struct dma_buf_poll_cb_t {
386 struct dma_fence_cb cb;
387 wait_queue_head_t *poll;
388
389 __poll_t active;
390 } cb_excl, cb_shared;
391 };
392
393 /**
394 * struct dma_buf_attachment - holds device-buffer attachment data
395 * @dmabuf: buffer for this attachment.
396 * @dev: device attached to the buffer.
397 * @node: list of dma_buf_attachment.
398 * @sgt: cached mapping.
399 * @dir: direction of cached mapping.
400 * @priv: exporter specific attachment data.
401 * @dma_map_attrs: DMA attributes to be used when the exporter maps the buffer
402 * through dma_buf_map_attachment.
403 *
404 * This structure holds the attachment information between the dma_buf buffer
405 * and its user device(s). The list contains one attachment struct per device
406 * attached to the buffer.
407 *
408 * An attachment is created by calling dma_buf_attach(), and released again by
409 * calling dma_buf_detach(). The DMA mapping itself needed to initiate a
410 * transfer is created by dma_buf_map_attachment() and freed again by calling
411 * dma_buf_unmap_attachment().
412 */
413 struct dma_buf_attachment {
414 struct dma_buf *dmabuf;
415 struct device *dev;
416 struct list_head node;
417 struct sg_table *sgt;
418 enum dma_data_direction dir;
419 void *priv;
420 unsigned long dma_map_attrs;
421 };
422
423 /**
424 * struct dma_buf_export_info - holds information needed to export a dma_buf
425 * @exp_name: name of the exporter - useful for debugging.
426 * @owner: pointer to exporter module - used for refcounting kernel module
427 * @ops: Attach allocator-defined dma buf ops to the new buffer
428 * @size: Size of the buffer
429 * @flags: mode flags for the file
430 * @resv: reservation-object, NULL to allocate default one
431 * @priv: Attach private data of allocator to this buffer
432 *
433 * This structure holds the information required to export the buffer. Used
434 * with dma_buf_export() only.
435 */
436 struct dma_buf_export_info {
437 const char *exp_name;
438 struct module *owner;
439 const struct dma_buf_ops *ops;
440 size_t size;
441 int flags;
442 struct dma_resv *resv;
443 void *priv;
444 };
445
446 /**
447 * DEFINE_DMA_BUF_EXPORT_INFO - helper macro for exporters
448 * @name: export-info name
449 *
450 * DEFINE_DMA_BUF_EXPORT_INFO macro defines the &struct dma_buf_export_info,
451 * zeroes it out and pre-populates exp_name in it.
452 */
453 #define DEFINE_DMA_BUF_EXPORT_INFO(name) \
454 struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \
455 .owner = THIS_MODULE }
456
457 /**
458 * get_dma_buf - convenience wrapper for get_file.
459 * @dmabuf: [in] pointer to dma_buf
460 *
461 * Increments the reference count on the dma-buf, needed in case of drivers
462 * that either need to create additional references to the dmabuf on the
463 * kernel side. For example, an exporter that needs to keep a dmabuf ptr
464 * so that subsequent exports don't create a new dmabuf.
465 */
get_dma_buf(struct dma_buf * dmabuf)466 static inline void get_dma_buf(struct dma_buf *dmabuf)
467 {
468 get_file(dmabuf->file);
469 }
470
471 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
472 struct device *dev);
473 void dma_buf_detach(struct dma_buf *dmabuf,
474 struct dma_buf_attachment *dmabuf_attach);
475
476 struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
477
478 int dma_buf_fd(struct dma_buf *dmabuf, int flags);
479 struct dma_buf *dma_buf_get(int fd);
480 void dma_buf_put(struct dma_buf *dmabuf);
481
482 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
483 enum dma_data_direction);
484 void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
485 enum dma_data_direction);
486 int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
487 enum dma_data_direction dir);
488 int dma_buf_begin_cpu_access_partial(struct dma_buf *dma_buf,
489 enum dma_data_direction dir,
490 unsigned int offset, unsigned int len);
491 int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
492 enum dma_data_direction dir);
493 int dma_buf_end_cpu_access_partial(struct dma_buf *dma_buf,
494 enum dma_data_direction dir,
495 unsigned int offset, unsigned int len);
496 void *dma_buf_kmap(struct dma_buf *, unsigned long);
497 void dma_buf_kunmap(struct dma_buf *, unsigned long, void *);
498
499 int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
500 unsigned long);
501 void *dma_buf_vmap(struct dma_buf *);
502 void dma_buf_vunmap(struct dma_buf *, void *vaddr);
503 int dma_buf_get_flags(struct dma_buf *dmabuf, unsigned long *flags);
504 #endif /* __DMA_BUF_H__ */
505