Blame view

include/linux/dma-buf.h 8.94 KB
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  /*
   * Header file for dma buffer sharing framework.
   *
   * Copyright(C) 2011 Linaro Limited. All rights reserved.
   * Author: Sumit Semwal <sumit.semwal@ti.com>
   *
   * Many thanks to linaro-mm-sig list, and specially
   * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
   * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
   * refining of this idea.
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published by
   * the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   *
   * You should have received a copy of the GNU General Public License along with
   * this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  #ifndef __DMA_BUF_H__
  #define __DMA_BUF_H__
  
  #include <linux/file.h>
  #include <linux/err.h>
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
29
30
31
  #include <linux/scatterlist.h>
  #include <linux/list.h>
  #include <linux/dma-mapping.h>
f9a24d1ac   Rob Clark   dma-buf: add get_...
32
  #include <linux/fs.h>
9b495a588   Maarten Lankhorst   dma-buf: add poll...
33
34
  #include <linux/fence.h>
  #include <linux/wait.h>
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
35

313162d0b   Paul Gortmaker   device.h: audit a...
36
  struct device;
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  struct dma_buf;
  struct dma_buf_attachment;
  
  /**
   * struct dma_buf_ops - operations possible on struct dma_buf
   * @attach: [optional] allows different devices to 'attach' themselves to the
   *	    given buffer. It might return -EBUSY to signal that backing storage
   *	    is already allocated and incompatible with the requirements
   *	    of requesting device.
   * @detach: [optional] detach a given device from this buffer.
   * @map_dma_buf: returns list of scatter pages allocated, increases usecount
   *		 of the buffer. Requires atleast one attach to be called
   *		 before. Returned sg list should already be mapped into
   *		 _device_ address space. This call may sleep. May also return
   *		 -EINTR. Should return -EINVAL if attach hasn't been called yet.
   * @unmap_dma_buf: decreases usecount of buffer, might deallocate scatter
   *		   pages.
   * @release: release this buffer; to be called after the last dma_buf_put.
fc13020e0   Daniel Vetter   dma-buf: add supp...
55
56
   * @begin_cpu_access: [optional] called before cpu access to invalidate cpu
   * 		      caches and allocate backing storage (if not yet done)
831e9da7d   Tiago Vignatti   dma-buf: Remove r...
57
   * 		      respectively pin the object into memory.
8a168ca70   Masanari Iida   treewide: Fix typ...
58
   * @end_cpu_access: [optional] called after cpu access to flush caches.
fc13020e0   Daniel Vetter   dma-buf: add supp...
59
60
61
62
63
64
65
   * @kmap_atomic: maps a page from the buffer into kernel address
   * 		 space, users may not block until the subsequent unmap call.
   * 		 This callback must not sleep.
   * @kunmap_atomic: [optional] unmaps a atomically mapped page from the buffer.
   * 		   This Callback must not sleep.
   * @kmap: maps a page from the buffer into kernel address space.
   * @kunmap: [optional] unmaps a page from the buffer.
4c78513e4   Daniel Vetter   dma-buf: mmap sup...
66
67
68
69
   * @mmap: used to expose the backing storage to userspace. Note that the
   * 	  mapping needs to be coherent - if the exporter doesn't directly
   * 	  support this, it needs to fake coherency by shooting down any ptes
   * 	  when transitioning away from the cpu domain.
12c4727e1   Sumit Semwal   dma-buf: minor do...
70
71
72
   * @vmap: [optional] creates a virtual mapping for the buffer into kernel
   *	  address space. Same restrictions as for vmap and friends apply.
   * @vunmap: [optional] unmaps a vmap from the buffer
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
   */
  struct dma_buf_ops {
  	int (*attach)(struct dma_buf *, struct device *,
  			struct dma_buf_attachment *);
  
  	void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
  
  	/* For {map,unmap}_dma_buf below, any specific buffer attributes
  	 * required should get added to device_dma_parameters accessible
  	 * via dev->dma_params.
  	 */
  	struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
  						enum dma_data_direction);
  	void (*unmap_dma_buf)(struct dma_buf_attachment *,
33ea2dcb3   Sumit Semwal   dma-buf: add dma_...
87
88
  						struct sg_table *,
  						enum dma_data_direction);
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
89
90
91
92
93
94
  	/* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
  	 * if the call would block.
  	 */
  
  	/* after final dma_buf_put() */
  	void (*release)(struct dma_buf *);
831e9da7d   Tiago Vignatti   dma-buf: Remove r...
95
  	int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
18b862dcd   Chris Wilson   dma-buf, drm, ion...
96
  	int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
fc13020e0   Daniel Vetter   dma-buf: add supp...
97
98
99
100
  	void *(*kmap_atomic)(struct dma_buf *, unsigned long);
  	void (*kunmap_atomic)(struct dma_buf *, unsigned long, void *);
  	void *(*kmap)(struct dma_buf *, unsigned long);
  	void (*kunmap)(struct dma_buf *, unsigned long, void *);
4c78513e4   Daniel Vetter   dma-buf: mmap sup...
101
102
  
  	int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
98f86c9e4   Dave Airlie   dma-buf: add vmap...
103
104
105
  
  	void *(*vmap)(struct dma_buf *);
  	void (*vunmap)(struct dma_buf *, void *vaddr);
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
106
107
108
109
110
111
112
113
  };
  
  /**
   * struct dma_buf - shared buffer object
   * @size: size of the buffer
   * @file: file pointer used for sharing buffers across, and for refcounting.
   * @attachments: list of dma_buf_attachment that denotes all devices attached.
   * @ops: dma_buf_ops associated with this buffer object.
e2082e3ab   Rob Clark   dma-buf: headerdo...
114
115
116
   * @lock: used internally to serialize list manipulation, attach/detach and vmap/unmap
   * @vmapping_counter: used internally to refcnt the vmaps
   * @vmap_ptr: the current vmap ptr if vmapping_counter > 0
78df96955   Sumit Semwal   dma-buf: replace ...
117
   * @exp_name: name of the exporter; useful for debugging.
9abdffe28   Sumit Semwal   dma-buf: add ref ...
118
119
   * @owner: pointer to exporter module; used for refcounting when exporter is a
   *         kernel module.
b89e35636   Sumit Semwal   dma-buf: Add debu...
120
   * @list_node: node for dma_buf accounting and debugging.
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
121
   * @priv: exporter specific private data for this buffer object.
3aac4502f   Maarten Lankhorst   dma-buf: use rese...
122
   * @resv: reservation object linked to this dma-buf
e2082e3ab   Rob Clark   dma-buf: headerdo...
123
124
125
   * @poll: for userspace poll support
   * @cb_excl: for userspace poll support
   * @cb_shared: for userspace poll support
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
126
127
128
129
130
131
   */
  struct dma_buf {
  	size_t size;
  	struct file *file;
  	struct list_head attachments;
  	const struct dma_buf_ops *ops;
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
132
  	struct mutex lock;
f00b4dad9   Daniel Vetter   dma-buf: implemen...
133
134
  	unsigned vmapping_counter;
  	void *vmap_ptr;
78df96955   Sumit Semwal   dma-buf: replace ...
135
  	const char *exp_name;
9abdffe28   Sumit Semwal   dma-buf: add ref ...
136
  	struct module *owner;
b89e35636   Sumit Semwal   dma-buf: Add debu...
137
  	struct list_head list_node;
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
138
  	void *priv;
3aac4502f   Maarten Lankhorst   dma-buf: use rese...
139
  	struct reservation_object *resv;
9b495a588   Maarten Lankhorst   dma-buf: add poll...
140
141
142
143
144
145
146
147
148
149
  
  	/* poll support */
  	wait_queue_head_t poll;
  
  	struct dma_buf_poll_cb_t {
  		struct fence_cb cb;
  		wait_queue_head_t *poll;
  
  		unsigned long active;
  	} cb_excl, cb_shared;
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  };
  
  /**
   * struct dma_buf_attachment - holds device-buffer attachment data
   * @dmabuf: buffer for this attachment.
   * @dev: device attached to the buffer.
   * @node: list of dma_buf_attachment.
   * @priv: exporter specific attachment data.
   *
   * This structure holds the attachment information between the dma_buf buffer
   * and its user device(s). The list contains one attachment struct per device
   * attached to the buffer.
   */
  struct dma_buf_attachment {
  	struct dma_buf *dmabuf;
  	struct device *dev;
  	struct list_head node;
  	void *priv;
  };
f9a24d1ac   Rob Clark   dma-buf: add get_...
169
  /**
d8fbe341b   Sumit Semwal   dma-buf: cleanup ...
170
   * struct dma_buf_export_info - holds information needed to export a dma_buf
9abdffe28   Sumit Semwal   dma-buf: add ref ...
171
172
   * @exp_name:	name of the exporter - useful for debugging.
   * @owner:	pointer to exporter module - used for refcounting kernel module
d8fbe341b   Sumit Semwal   dma-buf: cleanup ...
173
174
175
176
177
178
179
180
181
182
183
   * @ops:	Attach allocator-defined dma buf ops to the new buffer
   * @size:	Size of the buffer
   * @flags:	mode flags for the file
   * @resv:	reservation-object, NULL to allocate default one
   * @priv:	Attach private data of allocator to this buffer
   *
   * This structure holds the information required to export the buffer. Used
   * with dma_buf_export() only.
   */
  struct dma_buf_export_info {
  	const char *exp_name;
9abdffe28   Sumit Semwal   dma-buf: add ref ...
184
  	struct module *owner;
d8fbe341b   Sumit Semwal   dma-buf: cleanup ...
185
186
187
188
189
190
191
192
193
  	const struct dma_buf_ops *ops;
  	size_t size;
  	int flags;
  	struct reservation_object *resv;
  	void *priv;
  };
  
  /**
   * helper macro for exporters; zeros and fills in most common values
e2082e3ab   Rob Clark   dma-buf: headerdo...
194
195
   *
   * @name: export-info name
d8fbe341b   Sumit Semwal   dma-buf: cleanup ...
196
   */
e2082e3ab   Rob Clark   dma-buf: headerdo...
197
198
  #define DEFINE_DMA_BUF_EXPORT_INFO(name)	\
  	struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \
9abdffe28   Sumit Semwal   dma-buf: add ref ...
199
  					 .owner = THIS_MODULE }
d8fbe341b   Sumit Semwal   dma-buf: cleanup ...
200
201
  
  /**
f9a24d1ac   Rob Clark   dma-buf: add get_...
202
203
204
205
206
207
208
209
210
211
212
213
   * get_dma_buf - convenience wrapper for get_file.
   * @dmabuf:	[in]	pointer to dma_buf
   *
   * Increments the reference count on the dma-buf, needed in case of drivers
   * that either need to create additional references to the dmabuf on the
   * kernel side.  For example, an exporter that needs to keep a dmabuf ptr
   * so that subsequent exports don't create a new dmabuf.
   */
  static inline void get_dma_buf(struct dma_buf *dmabuf)
  {
  	get_file(dmabuf->file);
  }
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
214
215
216
217
  struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  							struct device *dev);
  void dma_buf_detach(struct dma_buf *dmabuf,
  				struct dma_buf_attachment *dmabuf_attach);
78df96955   Sumit Semwal   dma-buf: replace ...
218

d8fbe341b   Sumit Semwal   dma-buf: cleanup ...
219
  struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
78df96955   Sumit Semwal   dma-buf: replace ...
220

55c1c4ca2   Dave Airlie   dma-buf: pass fla...
221
  int dma_buf_fd(struct dma_buf *dmabuf, int flags);
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
222
223
224
225
226
  struct dma_buf *dma_buf_get(int fd);
  void dma_buf_put(struct dma_buf *dmabuf);
  
  struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
  					enum dma_data_direction);
33ea2dcb3   Sumit Semwal   dma-buf: add dma_...
227
228
  void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
  				enum dma_data_direction);
831e9da7d   Tiago Vignatti   dma-buf: Remove r...
229
  int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
fc13020e0   Daniel Vetter   dma-buf: add supp...
230
  			     enum dma_data_direction dir);
18b862dcd   Chris Wilson   dma-buf, drm, ion...
231
232
  int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
  			   enum dma_data_direction dir);
fc13020e0   Daniel Vetter   dma-buf: add supp...
233
234
235
236
  void *dma_buf_kmap_atomic(struct dma_buf *, unsigned long);
  void dma_buf_kunmap_atomic(struct dma_buf *, unsigned long, void *);
  void *dma_buf_kmap(struct dma_buf *, unsigned long);
  void dma_buf_kunmap(struct dma_buf *, unsigned long, void *);
4c78513e4   Daniel Vetter   dma-buf: mmap sup...
237
238
239
  
  int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
  		 unsigned long);
98f86c9e4   Dave Airlie   dma-buf: add vmap...
240
241
  void *dma_buf_vmap(struct dma_buf *);
  void dma_buf_vunmap(struct dma_buf *, void *vaddr);
d15bd7ee4   Sumit Semwal   dma-buf: Introduc...
242
  #endif /* __DMA_BUF_H__ */