2 * Header file for dma buffer sharing framework.
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Author: Sumit Semwal <sumit.semwal@ti.com>
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.
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.
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
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/>.
27 #include <linux/file.h>
28 #include <linux/err.h>
29 #include <linux/scatterlist.h>
30 #include <linux/list.h>
31 #include <linux/dma-mapping.h>
33 #include <linux/kds.h>
37 struct dma_buf_attachment;
40 * struct dma_buf_ops - operations possible on struct dma_buf
41 * @attach: [optional] allows different devices to 'attach' themselves to the
42 * given buffer. It might return -EBUSY to signal that backing storage
43 * is already allocated and incompatible with the requirements
44 * of requesting device.
45 * @detach: [optional] detach a given device from this buffer.
46 * @map_dma_buf: returns list of scatter pages allocated, increases usecount
47 * of the buffer. Requires atleast one attach to be called
48 * before. Returned sg list should already be mapped into
49 * _device_ address space. This call may sleep. May also return
50 * -EINTR. Should return -EINVAL if attach hasn't been called yet.
51 * @unmap_dma_buf: decreases usecount of buffer, might deallocate scatter
53 * @release: release this buffer; to be called after the last dma_buf_put.
54 * @begin_cpu_access: [optional] called before cpu access to invalidate cpu
55 * caches and allocate backing storage (if not yet done)
56 * respectively pin the objet into memory.
57 * @end_cpu_access: [optional] called after cpu access to flush cashes.
58 * @kmap_atomic: maps a page from the buffer into kernel address
59 * space, users may not block until the subsequent unmap call.
60 * This callback must not sleep.
61 * @kunmap_atomic: [optional] unmaps a atomically mapped page from the buffer.
62 * This Callback must not sleep.
63 * @kmap: maps a page from the buffer into kernel address space.
64 * @kunmap: [optional] unmaps a page from the buffer.
65 * @mmap: used to expose the backing storage to userspace. Note that the
66 * mapping needs to be coherent - if the exporter doesn't directly
67 * support this, it needs to fake coherency by shooting down any ptes
68 * when transitioning away from the cpu domain.
69 * @vmap: [optional] creates a virtual mapping for the buffer into kernel
70 * address space. Same restrictions as for vmap and friends apply.
71 * @vunmap: [optional] unmaps a vmap from the buffer
74 int (*attach)(struct dma_buf *, struct device *,
75 struct dma_buf_attachment *);
77 void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
79 /* For {map,unmap}_dma_buf below, any specific buffer attributes
80 * required should get added to device_dma_parameters accessible
81 * via dev->dma_params.
83 struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
84 enum dma_data_direction);
85 void (*unmap_dma_buf)(struct dma_buf_attachment *,
87 enum dma_data_direction);
88 /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
89 * if the call would block.
92 /* after final dma_buf_put() */
93 void (*release)(struct dma_buf *);
95 int (*begin_cpu_access)(struct dma_buf *, size_t, size_t,
96 enum dma_data_direction);
97 void (*end_cpu_access)(struct dma_buf *, size_t, size_t,
98 enum dma_data_direction);
99 void *(*kmap_atomic)(struct dma_buf *, unsigned long);
100 void (*kunmap_atomic)(struct dma_buf *, unsigned long, void *);
101 void *(*kmap)(struct dma_buf *, unsigned long);
102 void (*kunmap)(struct dma_buf *, unsigned long, void *);
104 int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
106 void *(*vmap)(struct dma_buf *);
107 void (*vunmap)(struct dma_buf *, void *vaddr);
111 * struct dma_buf - shared buffer object
112 * @size: size of the buffer
113 * @file: file pointer used for sharing buffers across, and for refcounting.
114 * @attachments: list of dma_buf_attachment that denotes all devices attached.
115 * @ops: dma_buf_ops associated with this buffer object.
116 * @priv: exporter specific private data for this buffer object.
121 struct list_head attachments;
122 const struct dma_buf_ops *ops;
123 /* mutex to serialize list manipulation and attach/detach */
125 struct kds_resource kds;
130 * struct dma_buf_attachment - holds device-buffer attachment data
131 * @dmabuf: buffer for this attachment.
132 * @dev: device attached to the buffer.
133 * @node: list of dma_buf_attachment.
134 * @priv: exporter specific attachment data.
136 * This structure holds the attachment information between the dma_buf buffer
137 * and its user device(s). The list contains one attachment struct per device
138 * attached to the buffer.
140 struct dma_buf_attachment {
141 struct dma_buf *dmabuf;
143 struct list_head node;
148 * get_dma_buf - convenience wrapper for get_file.
149 * @dmabuf: [in] pointer to dma_buf
151 * Increments the reference count on the dma-buf, needed in case of drivers
152 * that either need to create additional references to the dmabuf on the
153 * kernel side. For example, an exporter that needs to keep a dmabuf ptr
154 * so that subsequent exports don't create a new dmabuf.
156 static inline void get_dma_buf(struct dma_buf *dmabuf)
158 get_file(dmabuf->file);
162 * get_dma_buf_kds_resource - get a KDS resource for this dma-buf
163 * @dmabuf: [in] pointer to dma_buf
165 * Returns a KDS resource that represents the dma-buf. This should be used by
166 * drivers to synchronize access to the buffer. Note that the caller should
167 * ensure that a reference to the dma-buf exists from the call to
168 * kds_async_wait until kds_resource_set_release is called.
170 static inline struct kds_resource *
171 get_dma_buf_kds_resource(struct dma_buf *dmabuf)
176 #ifdef CONFIG_DMA_SHARED_BUFFER
177 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
179 void dma_buf_detach(struct dma_buf *dmabuf,
180 struct dma_buf_attachment *dmabuf_attach);
181 struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
182 size_t size, int flags);
183 int dma_buf_fd(struct dma_buf *dmabuf, int flags);
184 struct dma_buf *dma_buf_get(int fd);
185 void dma_buf_put(struct dma_buf *dmabuf);
187 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
188 enum dma_data_direction);
189 void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
190 enum dma_data_direction);
191 int dma_buf_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t len,
192 enum dma_data_direction dir);
193 void dma_buf_end_cpu_access(struct dma_buf *dma_buf, size_t start, size_t len,
194 enum dma_data_direction dir);
195 void *dma_buf_kmap_atomic(struct dma_buf *, unsigned long);
196 void dma_buf_kunmap_atomic(struct dma_buf *, unsigned long, void *);
197 void *dma_buf_kmap(struct dma_buf *, unsigned long);
198 void dma_buf_kunmap(struct dma_buf *, unsigned long, void *);
200 int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
202 void *dma_buf_vmap(struct dma_buf *);
203 void dma_buf_vunmap(struct dma_buf *, void *vaddr);
206 static inline struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
209 return ERR_PTR(-ENODEV);
212 static inline void dma_buf_detach(struct dma_buf *dmabuf,
213 struct dma_buf_attachment *dmabuf_attach)
218 static inline struct dma_buf *dma_buf_export(void *priv,
219 const struct dma_buf_ops *ops,
220 size_t size, int flags)
222 return ERR_PTR(-ENODEV);
225 static inline int dma_buf_fd(struct dma_buf *dmabuf, int flags)
230 static inline struct dma_buf *dma_buf_get(int fd)
232 return ERR_PTR(-ENODEV);
235 static inline void dma_buf_put(struct dma_buf *dmabuf)
240 static inline struct sg_table *dma_buf_map_attachment(
241 struct dma_buf_attachment *attach, enum dma_data_direction write)
243 return ERR_PTR(-ENODEV);
246 static inline void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
247 struct sg_table *sg, enum dma_data_direction dir)
252 static inline int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
253 size_t start, size_t len,
254 enum dma_data_direction dir)
259 static inline void dma_buf_end_cpu_access(struct dma_buf *dmabuf,
260 size_t start, size_t len,
261 enum dma_data_direction dir)
265 static inline void *dma_buf_kmap_atomic(struct dma_buf *dmabuf,
271 static inline void dma_buf_kunmap_atomic(struct dma_buf *dmabuf,
272 unsigned long pnum, void *vaddr)
276 static inline void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long pnum)
281 static inline void dma_buf_kunmap(struct dma_buf *dmabuf,
282 unsigned long pnum, void *vaddr)
286 static inline int dma_buf_mmap(struct dma_buf *dmabuf,
287 struct vm_area_struct *vma,
293 static inline void *dma_buf_vmap(struct dma_buf *dmabuf)
298 static inline void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
301 #endif /* CONFIG_DMA_SHARED_BUFFER */
303 #endif /* __DMA_BUF_H__ */