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