Commit | Line | Data |
---|---|---|
d15bd7ee SS |
1 | /* |
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> | |
29 | #include <linux/device.h> | |
30 | #include <linux/scatterlist.h> | |
31 | #include <linux/list.h> | |
32 | #include <linux/dma-mapping.h> | |
f9a24d1a | 33 | #include <linux/fs.h> |
d15bd7ee SS |
34 | |
35 | struct dma_buf; | |
36 | struct dma_buf_attachment; | |
37 | ||
38 | /** | |
39 | * struct dma_buf_ops - operations possible on struct dma_buf | |
40 | * @attach: [optional] allows different devices to 'attach' themselves to the | |
41 | * given buffer. It might return -EBUSY to signal that backing storage | |
42 | * is already allocated and incompatible with the requirements | |
43 | * of requesting device. | |
44 | * @detach: [optional] detach a given device from this buffer. | |
45 | * @map_dma_buf: returns list of scatter pages allocated, increases usecount | |
46 | * of the buffer. Requires atleast one attach to be called | |
47 | * before. Returned sg list should already be mapped into | |
48 | * _device_ address space. This call may sleep. May also return | |
49 | * -EINTR. Should return -EINVAL if attach hasn't been called yet. | |
50 | * @unmap_dma_buf: decreases usecount of buffer, might deallocate scatter | |
51 | * pages. | |
52 | * @release: release this buffer; to be called after the last dma_buf_put. | |
53 | */ | |
54 | struct dma_buf_ops { | |
55 | int (*attach)(struct dma_buf *, struct device *, | |
56 | struct dma_buf_attachment *); | |
57 | ||
58 | void (*detach)(struct dma_buf *, struct dma_buf_attachment *); | |
59 | ||
60 | /* For {map,unmap}_dma_buf below, any specific buffer attributes | |
61 | * required should get added to device_dma_parameters accessible | |
62 | * via dev->dma_params. | |
63 | */ | |
64 | struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *, | |
65 | enum dma_data_direction); | |
66 | void (*unmap_dma_buf)(struct dma_buf_attachment *, | |
33ea2dcb SS |
67 | struct sg_table *, |
68 | enum dma_data_direction); | |
d15bd7ee SS |
69 | /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY |
70 | * if the call would block. | |
71 | */ | |
72 | ||
73 | /* after final dma_buf_put() */ | |
74 | void (*release)(struct dma_buf *); | |
75 | ||
76 | }; | |
77 | ||
78 | /** | |
79 | * struct dma_buf - shared buffer object | |
80 | * @size: size of the buffer | |
81 | * @file: file pointer used for sharing buffers across, and for refcounting. | |
82 | * @attachments: list of dma_buf_attachment that denotes all devices attached. | |
83 | * @ops: dma_buf_ops associated with this buffer object. | |
84 | * @priv: exporter specific private data for this buffer object. | |
85 | */ | |
86 | struct dma_buf { | |
87 | size_t size; | |
88 | struct file *file; | |
89 | struct list_head attachments; | |
90 | const struct dma_buf_ops *ops; | |
6b607e3a | 91 | /* mutex to serialize list manipulation and attach/detach */ |
d15bd7ee SS |
92 | struct mutex lock; |
93 | void *priv; | |
94 | }; | |
95 | ||
96 | /** | |
97 | * struct dma_buf_attachment - holds device-buffer attachment data | |
98 | * @dmabuf: buffer for this attachment. | |
99 | * @dev: device attached to the buffer. | |
100 | * @node: list of dma_buf_attachment. | |
101 | * @priv: exporter specific attachment data. | |
102 | * | |
103 | * This structure holds the attachment information between the dma_buf buffer | |
104 | * and its user device(s). The list contains one attachment struct per device | |
105 | * attached to the buffer. | |
106 | */ | |
107 | struct dma_buf_attachment { | |
108 | struct dma_buf *dmabuf; | |
109 | struct device *dev; | |
110 | struct list_head node; | |
111 | void *priv; | |
112 | }; | |
113 | ||
f9a24d1a RC |
114 | /** |
115 | * get_dma_buf - convenience wrapper for get_file. | |
116 | * @dmabuf: [in] pointer to dma_buf | |
117 | * | |
118 | * Increments the reference count on the dma-buf, needed in case of drivers | |
119 | * that either need to create additional references to the dmabuf on the | |
120 | * kernel side. For example, an exporter that needs to keep a dmabuf ptr | |
121 | * so that subsequent exports don't create a new dmabuf. | |
122 | */ | |
123 | static inline void get_dma_buf(struct dma_buf *dmabuf) | |
124 | { | |
125 | get_file(dmabuf->file); | |
126 | } | |
127 | ||
d15bd7ee SS |
128 | #ifdef CONFIG_DMA_SHARED_BUFFER |
129 | struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, | |
130 | struct device *dev); | |
131 | void dma_buf_detach(struct dma_buf *dmabuf, | |
132 | struct dma_buf_attachment *dmabuf_attach); | |
5375764f LP |
133 | struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops, |
134 | size_t size, int flags); | |
55c1c4ca | 135 | int dma_buf_fd(struct dma_buf *dmabuf, int flags); |
d15bd7ee SS |
136 | struct dma_buf *dma_buf_get(int fd); |
137 | void dma_buf_put(struct dma_buf *dmabuf); | |
138 | ||
139 | struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *, | |
140 | enum dma_data_direction); | |
33ea2dcb SS |
141 | void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *, |
142 | enum dma_data_direction); | |
d15bd7ee SS |
143 | #else |
144 | ||
145 | static inline struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, | |
146 | struct device *dev) | |
147 | { | |
148 | return ERR_PTR(-ENODEV); | |
149 | } | |
150 | ||
151 | static inline void dma_buf_detach(struct dma_buf *dmabuf, | |
152 | struct dma_buf_attachment *dmabuf_attach) | |
153 | { | |
154 | return; | |
155 | } | |
156 | ||
157 | static inline struct dma_buf *dma_buf_export(void *priv, | |
5375764f LP |
158 | const struct dma_buf_ops *ops, |
159 | size_t size, int flags) | |
d15bd7ee SS |
160 | { |
161 | return ERR_PTR(-ENODEV); | |
162 | } | |
163 | ||
164 | static inline int dma_buf_fd(struct dma_buf *dmabuf) | |
165 | { | |
166 | return -ENODEV; | |
167 | } | |
168 | ||
169 | static inline struct dma_buf *dma_buf_get(int fd) | |
170 | { | |
171 | return ERR_PTR(-ENODEV); | |
172 | } | |
173 | ||
174 | static inline void dma_buf_put(struct dma_buf *dmabuf) | |
175 | { | |
176 | return; | |
177 | } | |
178 | ||
179 | static inline struct sg_table *dma_buf_map_attachment( | |
180 | struct dma_buf_attachment *attach, enum dma_data_direction write) | |
181 | { | |
182 | return ERR_PTR(-ENODEV); | |
183 | } | |
184 | ||
185 | static inline void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, | |
33ea2dcb | 186 | struct sg_table *sg, enum dma_data_direction dir) |
d15bd7ee SS |
187 | { |
188 | return; | |
189 | } | |
190 | ||
191 | #endif /* CONFIG_DMA_SHARED_BUFFER */ | |
192 | ||
193 | #endif /* __DMA_BUF_H__ */ |