blob: 6df170fb243fe1a9bdd72045fa4792395e9e34b2 [file] [log] [blame]
Sumit Semwald15bd7e2011-12-26 14:53:15 +05301/*
2 * Header file for dma buffer sharing framework.
3 *
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Author: Sumit Semwal <sumit.semwal@ti.com>
6 *
7 * Many thanks to linaro-mm-sig list, and specially
8 * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
9 * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
10 * refining of this idea.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24#ifndef __DMA_BUF_H__
25#define __DMA_BUF_H__
26
27#include <linux/file.h>
28#include <linux/err.h>
Sumit Semwald15bd7e2011-12-26 14:53:15 +053029#include <linux/scatterlist.h>
30#include <linux/list.h>
31#include <linux/dma-mapping.h>
Rob Clarkf9a24d12012-03-16 11:04:41 -050032#include <linux/fs.h>
Chris Wilsonf54d1862016-10-25 13:00:45 +010033#include <linux/dma-fence.h>
Maarten Lankhorst9b495a52014-07-01 12:57:43 +020034#include <linux/wait.h>
Sumit Semwald15bd7e2011-12-26 14:53:15 +053035
Paul Gortmaker313162d2012-01-30 11:46:54 -050036struct device;
Sumit Semwald15bd7e2011-12-26 14:53:15 +053037struct dma_buf;
38struct dma_buf_attachment;
39
40/**
41 * struct dma_buf_ops - operations possible on struct dma_buf
Daniel Vetterfc130202012-03-20 00:02:37 +010042 * @begin_cpu_access: [optional] called before cpu access to invalidate cpu
43 * caches and allocate backing storage (if not yet done)
Tiago Vignatti831e9da2015-12-22 19:36:45 -020044 * respectively pin the object into memory.
Masanari Iida8a168ca2012-12-29 02:00:09 +090045 * @end_cpu_access: [optional] called after cpu access to flush caches.
Daniel Vetterfc130202012-03-20 00:02:37 +010046 * @kmap_atomic: maps a page from the buffer into kernel address
47 * space, users may not block until the subsequent unmap call.
48 * This callback must not sleep.
49 * @kunmap_atomic: [optional] unmaps a atomically mapped page from the buffer.
50 * This Callback must not sleep.
51 * @kmap: maps a page from the buffer into kernel address space.
52 * @kunmap: [optional] unmaps a page from the buffer.
Daniel Vetter4c785132012-04-24 14:38:52 +053053 * @mmap: used to expose the backing storage to userspace. Note that the
54 * mapping needs to be coherent - if the exporter doesn't directly
55 * support this, it needs to fake coherency by shooting down any ptes
56 * when transitioning away from the cpu domain.
Sumit Semwal12c47272012-05-23 15:27:40 +053057 * @vmap: [optional] creates a virtual mapping for the buffer into kernel
58 * address space. Same restrictions as for vmap and friends apply.
59 * @vunmap: [optional] unmaps a vmap from the buffer
Sumit Semwald15bd7e2011-12-26 14:53:15 +053060 */
61struct dma_buf_ops {
Daniel Vetter2904a8c2016-12-09 19:53:07 +010062 /**
63 * @attach:
64 *
65 * This is called from dma_buf_attach() to make sure that a given
66 * &device can access the provided &dma_buf. Exporters which support
67 * buffer objects in special locations like VRAM or device-specific
68 * carveout areas should check whether the buffer could be move to
69 * system memory (or directly accessed by the provided device), and
70 * otherwise need to fail the attach operation.
71 *
72 * The exporter should also in general check whether the current
73 * allocation fullfills the DMA constraints of the new device. If this
74 * is not the case, and the allocation cannot be moved, it should also
75 * fail the attach operation.
76 *
77 * Any exporter-private housekeeping data can be stored in the priv
78 * pointer of &dma_buf_attachment structure.
79 *
80 * This callback is optional.
81 *
82 * Returns:
83 *
84 * 0 on success, negative error code on failure. It might return -EBUSY
85 * to signal that backing storage is already allocated and incompatible
86 * with the requirements of requesting device.
87 */
Sumit Semwald15bd7e2011-12-26 14:53:15 +053088 int (*attach)(struct dma_buf *, struct device *,
Daniel Vetter2904a8c2016-12-09 19:53:07 +010089 struct dma_buf_attachment *);
Sumit Semwald15bd7e2011-12-26 14:53:15 +053090
Daniel Vetter2904a8c2016-12-09 19:53:07 +010091 /**
92 * @detach:
93 *
94 * This is called by dma_buf_detach() to release a &dma_buf_attachment.
95 * Provided so that exporters can clean up any housekeeping for an
96 * &dma_buf_attachment.
97 *
98 * This callback is optional.
99 */
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530100 void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
101
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100102 /**
103 * @map_dma_buf:
104 *
105 * This is called by dma_buf_map_attachment() and is used to map a
106 * shared &dma_buf into device address space, and it is mandatory. It
107 * can only be called if @attach has been called successfully. This
108 * essentially pins the DMA buffer into place, and it cannot be moved
109 * any more
110 *
111 * This call may sleep, e.g. when the backing storage first needs to be
112 * allocated, or moved to a location suitable for all currently attached
113 * devices.
114 *
115 * Note that any specific buffer attributes required for this function
116 * should get added to device_dma_parameters accessible via
117 * device->dma_params from the &dma_buf_attachment. The @attach callback
118 * should also check these constraints.
119 *
120 * If this is being called for the first time, the exporter can now
121 * choose to scan through the list of attachments for this buffer,
122 * collate the requirements of the attached devices, and choose an
123 * appropriate backing storage for the buffer.
124 *
125 * Based on enum dma_data_direction, it might be possible to have
126 * multiple users accessing at the same time (for reading, maybe), or
127 * any other kind of sharing that the exporter might wish to make
128 * available to buffer-users.
129 *
130 * Returns:
131 *
132 * A &sg_table scatter list of or the backing storage of the DMA buffer,
133 * already mapped into the device address space of the &device attached
134 * with the provided &dma_buf_attachment.
135 *
136 * On failure, returns a negative error value wrapped into a pointer.
137 * May also return -EINTR when a signal was received while being
138 * blocked.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530139 */
140 struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100141 enum dma_data_direction);
142 /**
143 * @unmap_dma_buf:
144 *
145 * This is called by dma_buf_unmap_attachment() and should unmap and
146 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
147 * It should also unpin the backing storage if this is the last mapping
148 * of the DMA buffer, it the exporter supports backing storage
149 * migration.
150 */
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530151 void (*unmap_dma_buf)(struct dma_buf_attachment *,
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100152 struct sg_table *,
153 enum dma_data_direction);
154
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530155 /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
156 * if the call would block.
157 */
158
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100159 /**
160 * @release:
161 *
162 * Called after the last dma_buf_put to release the &dma_buf, and
163 * mandatory.
164 */
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530165 void (*release)(struct dma_buf *);
166
Tiago Vignatti831e9da2015-12-22 19:36:45 -0200167 int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
Chris Wilson18b862d2016-03-18 20:02:39 +0000168 int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
Daniel Vetterfc130202012-03-20 00:02:37 +0100169 void *(*kmap_atomic)(struct dma_buf *, unsigned long);
170 void (*kunmap_atomic)(struct dma_buf *, unsigned long, void *);
171 void *(*kmap)(struct dma_buf *, unsigned long);
172 void (*kunmap)(struct dma_buf *, unsigned long, void *);
Daniel Vetter4c785132012-04-24 14:38:52 +0530173
174 int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
Dave Airlie98f86c92012-05-20 12:33:56 +0530175
176 void *(*vmap)(struct dma_buf *);
177 void (*vunmap)(struct dma_buf *, void *vaddr);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530178};
179
180/**
181 * struct dma_buf - shared buffer object
182 * @size: size of the buffer
183 * @file: file pointer used for sharing buffers across, and for refcounting.
184 * @attachments: list of dma_buf_attachment that denotes all devices attached.
185 * @ops: dma_buf_ops associated with this buffer object.
Rob Clarke2082e32016-03-31 16:26:50 -0400186 * @lock: used internally to serialize list manipulation, attach/detach and vmap/unmap
187 * @vmapping_counter: used internally to refcnt the vmaps
188 * @vmap_ptr: the current vmap ptr if vmapping_counter > 0
Sumit Semwal78df9692013-03-22 18:22:16 +0530189 * @exp_name: name of the exporter; useful for debugging.
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530190 * @owner: pointer to exporter module; used for refcounting when exporter is a
191 * kernel module.
Sumit Semwalb89e3562013-04-04 11:44:37 +0530192 * @list_node: node for dma_buf accounting and debugging.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530193 * @priv: exporter specific private data for this buffer object.
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200194 * @resv: reservation object linked to this dma-buf
Rob Clarke2082e32016-03-31 16:26:50 -0400195 * @poll: for userspace poll support
196 * @cb_excl: for userspace poll support
197 * @cb_shared: for userspace poll support
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100198 *
199 * This represents a shared buffer, created by calling dma_buf_export(). The
200 * userspace representation is a normal file descriptor, which can be created by
201 * calling dma_buf_fd().
202 *
203 * Shared dma buffers are reference counted using dma_buf_put() and
204 * get_dma_buf().
205 *
206 * Device DMA access is handled by the separate struct &dma_buf_attachment.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530207 */
208struct dma_buf {
209 size_t size;
210 struct file *file;
211 struct list_head attachments;
212 const struct dma_buf_ops *ops;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530213 struct mutex lock;
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100214 unsigned vmapping_counter;
215 void *vmap_ptr;
Sumit Semwal78df9692013-03-22 18:22:16 +0530216 const char *exp_name;
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530217 struct module *owner;
Sumit Semwalb89e3562013-04-04 11:44:37 +0530218 struct list_head list_node;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530219 void *priv;
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200220 struct reservation_object *resv;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200221
222 /* poll support */
223 wait_queue_head_t poll;
224
225 struct dma_buf_poll_cb_t {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100226 struct dma_fence_cb cb;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200227 wait_queue_head_t *poll;
228
229 unsigned long active;
230 } cb_excl, cb_shared;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530231};
232
233/**
234 * struct dma_buf_attachment - holds device-buffer attachment data
235 * @dmabuf: buffer for this attachment.
236 * @dev: device attached to the buffer.
237 * @node: list of dma_buf_attachment.
238 * @priv: exporter specific attachment data.
239 *
240 * This structure holds the attachment information between the dma_buf buffer
241 * and its user device(s). The list contains one attachment struct per device
242 * attached to the buffer.
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100243 *
244 * An attachment is created by calling dma_buf_attach(), and released again by
245 * calling dma_buf_detach(). The DMA mapping itself needed to initiate a
246 * transfer is created by dma_buf_map_attachment() and freed again by calling
247 * dma_buf_unmap_attachment().
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530248 */
249struct dma_buf_attachment {
250 struct dma_buf *dmabuf;
251 struct device *dev;
252 struct list_head node;
253 void *priv;
254};
255
Rob Clarkf9a24d12012-03-16 11:04:41 -0500256/**
Sumit Semwald8fbe342015-01-23 12:53:43 +0530257 * struct dma_buf_export_info - holds information needed to export a dma_buf
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530258 * @exp_name: name of the exporter - useful for debugging.
259 * @owner: pointer to exporter module - used for refcounting kernel module
Sumit Semwald8fbe342015-01-23 12:53:43 +0530260 * @ops: Attach allocator-defined dma buf ops to the new buffer
261 * @size: Size of the buffer
262 * @flags: mode flags for the file
263 * @resv: reservation-object, NULL to allocate default one
264 * @priv: Attach private data of allocator to this buffer
265 *
266 * This structure holds the information required to export the buffer. Used
267 * with dma_buf_export() only.
268 */
269struct dma_buf_export_info {
270 const char *exp_name;
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530271 struct module *owner;
Sumit Semwald8fbe342015-01-23 12:53:43 +0530272 const struct dma_buf_ops *ops;
273 size_t size;
274 int flags;
275 struct reservation_object *resv;
276 void *priv;
277};
278
279/**
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100280 * DEFINE_DMA_BUF_EXPORT_INFO - helper macro for exporters
Rob Clarke2082e32016-03-31 16:26:50 -0400281 * @name: export-info name
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100282 *
283 * DEFINE_DMA_BUF_EXPORT_INFO macro defines the struct &dma_buf_export_info,
284 * zeroes it out and pre-populates exp_name in it.
Sumit Semwald8fbe342015-01-23 12:53:43 +0530285 */
Rob Clarke2082e32016-03-31 16:26:50 -0400286#define DEFINE_DMA_BUF_EXPORT_INFO(name) \
287 struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530288 .owner = THIS_MODULE }
Sumit Semwald8fbe342015-01-23 12:53:43 +0530289
290/**
Rob Clarkf9a24d12012-03-16 11:04:41 -0500291 * get_dma_buf - convenience wrapper for get_file.
292 * @dmabuf: [in] pointer to dma_buf
293 *
294 * Increments the reference count on the dma-buf, needed in case of drivers
295 * that either need to create additional references to the dmabuf on the
296 * kernel side. For example, an exporter that needs to keep a dmabuf ptr
297 * so that subsequent exports don't create a new dmabuf.
298 */
299static inline void get_dma_buf(struct dma_buf *dmabuf)
300{
301 get_file(dmabuf->file);
302}
303
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530304struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
305 struct device *dev);
306void dma_buf_detach(struct dma_buf *dmabuf,
307 struct dma_buf_attachment *dmabuf_attach);
Sumit Semwal78df9692013-03-22 18:22:16 +0530308
Sumit Semwald8fbe342015-01-23 12:53:43 +0530309struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
Sumit Semwal78df9692013-03-22 18:22:16 +0530310
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000311int dma_buf_fd(struct dma_buf *dmabuf, int flags);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530312struct dma_buf *dma_buf_get(int fd);
313void dma_buf_put(struct dma_buf *dmabuf);
314
315struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
316 enum dma_data_direction);
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530317void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
318 enum dma_data_direction);
Tiago Vignatti831e9da2015-12-22 19:36:45 -0200319int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
Daniel Vetterfc130202012-03-20 00:02:37 +0100320 enum dma_data_direction dir);
Chris Wilson18b862d2016-03-18 20:02:39 +0000321int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
322 enum dma_data_direction dir);
Daniel Vetterfc130202012-03-20 00:02:37 +0100323void *dma_buf_kmap_atomic(struct dma_buf *, unsigned long);
324void dma_buf_kunmap_atomic(struct dma_buf *, unsigned long, void *);
325void *dma_buf_kmap(struct dma_buf *, unsigned long);
326void dma_buf_kunmap(struct dma_buf *, unsigned long, void *);
Daniel Vetter4c785132012-04-24 14:38:52 +0530327
328int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
329 unsigned long);
Dave Airlie98f86c92012-05-20 12:33:56 +0530330void *dma_buf_vmap(struct dma_buf *);
331void dma_buf_vunmap(struct dma_buf *, void *vaddr);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530332#endif /* __DMA_BUF_H__ */