Commit | Line | Data |
---|---|---|
d15bd7ee SS |
1 | /* |
2 | * Framework for buffer objects that can be shared across devices/subsystems. | |
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 | ||
25 | #include <linux/fs.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/dma-buf.h> | |
28 | #include <linux/anon_inodes.h> | |
29 | #include <linux/export.h> | |
30 | ||
31 | static inline int is_dma_buf_file(struct file *); | |
32 | ||
33 | static int dma_buf_release(struct inode *inode, struct file *file) | |
34 | { | |
35 | struct dma_buf *dmabuf; | |
36 | ||
37 | if (!is_dma_buf_file(file)) | |
38 | return -EINVAL; | |
39 | ||
40 | dmabuf = file->private_data; | |
41 | ||
42 | dmabuf->ops->release(dmabuf); | |
43 | kfree(dmabuf); | |
44 | return 0; | |
45 | } | |
46 | ||
4c78513e DV |
47 | static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) |
48 | { | |
49 | struct dma_buf *dmabuf; | |
50 | ||
51 | if (!is_dma_buf_file(file)) | |
52 | return -EINVAL; | |
53 | ||
54 | dmabuf = file->private_data; | |
55 | ||
56 | /* check for overflowing the buffer's size */ | |
57 | if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > | |
58 | dmabuf->size >> PAGE_SHIFT) | |
59 | return -EINVAL; | |
60 | ||
61 | return dmabuf->ops->mmap(dmabuf, vma); | |
62 | } | |
63 | ||
d15bd7ee SS |
64 | static const struct file_operations dma_buf_fops = { |
65 | .release = dma_buf_release, | |
4c78513e | 66 | .mmap = dma_buf_mmap_internal, |
d15bd7ee SS |
67 | }; |
68 | ||
69 | /* | |
70 | * is_dma_buf_file - Check if struct file* is associated with dma_buf | |
71 | */ | |
72 | static inline int is_dma_buf_file(struct file *file) | |
73 | { | |
74 | return file->f_op == &dma_buf_fops; | |
75 | } | |
76 | ||
77 | /** | |
78 | * dma_buf_export - Creates a new dma_buf, and associates an anon file | |
79 | * with this buffer, so it can be exported. | |
80 | * Also connect the allocator specific data and ops to the buffer. | |
81 | * | |
82 | * @priv: [in] Attach private data of allocator to this buffer | |
83 | * @ops: [in] Attach allocator-defined dma buf ops to the new buffer. | |
84 | * @size: [in] Size of the buffer | |
85 | * @flags: [in] mode flags for the file. | |
86 | * | |
87 | * Returns, on success, a newly created dma_buf object, which wraps the | |
88 | * supplied private data and operations for dma_buf_ops. On either missing | |
89 | * ops, or error in allocating struct dma_buf, will return negative error. | |
90 | * | |
91 | */ | |
5375764f | 92 | struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops, |
d15bd7ee SS |
93 | size_t size, int flags) |
94 | { | |
95 | struct dma_buf *dmabuf; | |
96 | struct file *file; | |
97 | ||
98 | if (WARN_ON(!priv || !ops | |
99 | || !ops->map_dma_buf | |
100 | || !ops->unmap_dma_buf | |
fc13020e DV |
101 | || !ops->release |
102 | || !ops->kmap_atomic | |
4c78513e DV |
103 | || !ops->kmap |
104 | || !ops->mmap)) { | |
d15bd7ee SS |
105 | return ERR_PTR(-EINVAL); |
106 | } | |
107 | ||
108 | dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL); | |
109 | if (dmabuf == NULL) | |
110 | return ERR_PTR(-ENOMEM); | |
111 | ||
112 | dmabuf->priv = priv; | |
113 | dmabuf->ops = ops; | |
114 | dmabuf->size = size; | |
115 | ||
116 | file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags); | |
117 | ||
118 | dmabuf->file = file; | |
119 | ||
120 | mutex_init(&dmabuf->lock); | |
121 | INIT_LIST_HEAD(&dmabuf->attachments); | |
122 | ||
123 | return dmabuf; | |
124 | } | |
125 | EXPORT_SYMBOL_GPL(dma_buf_export); | |
126 | ||
127 | ||
128 | /** | |
129 | * dma_buf_fd - returns a file descriptor for the given dma_buf | |
130 | * @dmabuf: [in] pointer to dma_buf for which fd is required. | |
55c1c4ca | 131 | * @flags: [in] flags to give to fd |
d15bd7ee SS |
132 | * |
133 | * On success, returns an associated 'fd'. Else, returns error. | |
134 | */ | |
55c1c4ca | 135 | int dma_buf_fd(struct dma_buf *dmabuf, int flags) |
d15bd7ee | 136 | { |
f5e097f0 | 137 | int fd; |
d15bd7ee SS |
138 | |
139 | if (!dmabuf || !dmabuf->file) | |
140 | return -EINVAL; | |
141 | ||
f5e097f0 BP |
142 | fd = get_unused_fd_flags(flags); |
143 | if (fd < 0) | |
144 | return fd; | |
d15bd7ee SS |
145 | |
146 | fd_install(fd, dmabuf->file); | |
147 | ||
148 | return fd; | |
149 | } | |
150 | EXPORT_SYMBOL_GPL(dma_buf_fd); | |
151 | ||
152 | /** | |
153 | * dma_buf_get - returns the dma_buf structure related to an fd | |
154 | * @fd: [in] fd associated with the dma_buf to be returned | |
155 | * | |
156 | * On success, returns the dma_buf structure associated with an fd; uses | |
157 | * file's refcounting done by fget to increase refcount. returns ERR_PTR | |
158 | * otherwise. | |
159 | */ | |
160 | struct dma_buf *dma_buf_get(int fd) | |
161 | { | |
162 | struct file *file; | |
163 | ||
164 | file = fget(fd); | |
165 | ||
166 | if (!file) | |
167 | return ERR_PTR(-EBADF); | |
168 | ||
169 | if (!is_dma_buf_file(file)) { | |
170 | fput(file); | |
171 | return ERR_PTR(-EINVAL); | |
172 | } | |
173 | ||
174 | return file->private_data; | |
175 | } | |
176 | EXPORT_SYMBOL_GPL(dma_buf_get); | |
177 | ||
178 | /** | |
179 | * dma_buf_put - decreases refcount of the buffer | |
180 | * @dmabuf: [in] buffer to reduce refcount of | |
181 | * | |
182 | * Uses file's refcounting done implicitly by fput() | |
183 | */ | |
184 | void dma_buf_put(struct dma_buf *dmabuf) | |
185 | { | |
186 | if (WARN_ON(!dmabuf || !dmabuf->file)) | |
187 | return; | |
188 | ||
189 | fput(dmabuf->file); | |
190 | } | |
191 | EXPORT_SYMBOL_GPL(dma_buf_put); | |
192 | ||
193 | /** | |
194 | * dma_buf_attach - Add the device to dma_buf's attachments list; optionally, | |
195 | * calls attach() of dma_buf_ops to allow device-specific attach functionality | |
196 | * @dmabuf: [in] buffer to attach device to. | |
197 | * @dev: [in] device to be attached. | |
198 | * | |
199 | * Returns struct dma_buf_attachment * for this attachment; may return negative | |
200 | * error codes. | |
201 | * | |
202 | */ | |
203 | struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, | |
204 | struct device *dev) | |
205 | { | |
206 | struct dma_buf_attachment *attach; | |
207 | int ret; | |
208 | ||
d1aa06a1 | 209 | if (WARN_ON(!dmabuf || !dev)) |
d15bd7ee SS |
210 | return ERR_PTR(-EINVAL); |
211 | ||
212 | attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL); | |
213 | if (attach == NULL) | |
a9fbc3b7 | 214 | return ERR_PTR(-ENOMEM); |
d15bd7ee | 215 | |
d15bd7ee SS |
216 | attach->dev = dev; |
217 | attach->dmabuf = dmabuf; | |
2ed9201b LP |
218 | |
219 | mutex_lock(&dmabuf->lock); | |
220 | ||
d15bd7ee SS |
221 | if (dmabuf->ops->attach) { |
222 | ret = dmabuf->ops->attach(dmabuf, dev, attach); | |
223 | if (ret) | |
224 | goto err_attach; | |
225 | } | |
226 | list_add(&attach->node, &dmabuf->attachments); | |
227 | ||
228 | mutex_unlock(&dmabuf->lock); | |
229 | return attach; | |
230 | ||
d15bd7ee SS |
231 | err_attach: |
232 | kfree(attach); | |
233 | mutex_unlock(&dmabuf->lock); | |
234 | return ERR_PTR(ret); | |
235 | } | |
236 | EXPORT_SYMBOL_GPL(dma_buf_attach); | |
237 | ||
238 | /** | |
239 | * dma_buf_detach - Remove the given attachment from dmabuf's attachments list; | |
240 | * optionally calls detach() of dma_buf_ops for device-specific detach | |
241 | * @dmabuf: [in] buffer to detach from. | |
242 | * @attach: [in] attachment to be detached; is free'd after this call. | |
243 | * | |
244 | */ | |
245 | void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) | |
246 | { | |
d1aa06a1 | 247 | if (WARN_ON(!dmabuf || !attach)) |
d15bd7ee SS |
248 | return; |
249 | ||
250 | mutex_lock(&dmabuf->lock); | |
251 | list_del(&attach->node); | |
252 | if (dmabuf->ops->detach) | |
253 | dmabuf->ops->detach(dmabuf, attach); | |
254 | ||
255 | mutex_unlock(&dmabuf->lock); | |
256 | kfree(attach); | |
257 | } | |
258 | EXPORT_SYMBOL_GPL(dma_buf_detach); | |
259 | ||
260 | /** | |
261 | * dma_buf_map_attachment - Returns the scatterlist table of the attachment; | |
262 | * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the | |
263 | * dma_buf_ops. | |
264 | * @attach: [in] attachment whose scatterlist is to be returned | |
265 | * @direction: [in] direction of DMA transfer | |
266 | * | |
267 | * Returns sg_table containing the scatterlist to be returned; may return NULL | |
268 | * or ERR_PTR. | |
269 | * | |
270 | */ | |
271 | struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, | |
272 | enum dma_data_direction direction) | |
273 | { | |
274 | struct sg_table *sg_table = ERR_PTR(-EINVAL); | |
275 | ||
276 | might_sleep(); | |
277 | ||
d1aa06a1 | 278 | if (WARN_ON(!attach || !attach->dmabuf)) |
d15bd7ee SS |
279 | return ERR_PTR(-EINVAL); |
280 | ||
d1aa06a1 | 281 | sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); |
d15bd7ee SS |
282 | |
283 | return sg_table; | |
284 | } | |
285 | EXPORT_SYMBOL_GPL(dma_buf_map_attachment); | |
286 | ||
287 | /** | |
288 | * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might | |
289 | * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of | |
290 | * dma_buf_ops. | |
291 | * @attach: [in] attachment to unmap buffer from | |
292 | * @sg_table: [in] scatterlist info of the buffer to unmap | |
33ea2dcb | 293 | * @direction: [in] direction of DMA transfer |
d15bd7ee SS |
294 | * |
295 | */ | |
296 | void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, | |
33ea2dcb SS |
297 | struct sg_table *sg_table, |
298 | enum dma_data_direction direction) | |
d15bd7ee | 299 | { |
b6fa0cd6 RC |
300 | might_sleep(); |
301 | ||
d1aa06a1 | 302 | if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) |
d15bd7ee SS |
303 | return; |
304 | ||
33ea2dcb SS |
305 | attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, |
306 | direction); | |
d15bd7ee SS |
307 | } |
308 | EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment); | |
fc13020e DV |
309 | |
310 | ||
311 | /** | |
312 | * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the | |
313 | * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific | |
314 | * preparations. Coherency is only guaranteed in the specified range for the | |
315 | * specified access direction. | |
efb4df82 | 316 | * @dmabuf: [in] buffer to prepare cpu access for. |
fc13020e DV |
317 | * @start: [in] start of range for cpu access. |
318 | * @len: [in] length of range for cpu access. | |
319 | * @direction: [in] length of range for cpu access. | |
320 | * | |
321 | * Can return negative error values, returns 0 on success. | |
322 | */ | |
323 | int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len, | |
324 | enum dma_data_direction direction) | |
325 | { | |
326 | int ret = 0; | |
327 | ||
328 | if (WARN_ON(!dmabuf)) | |
329 | return -EINVAL; | |
330 | ||
331 | if (dmabuf->ops->begin_cpu_access) | |
332 | ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction); | |
333 | ||
334 | return ret; | |
335 | } | |
336 | EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access); | |
337 | ||
338 | /** | |
339 | * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the | |
340 | * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific | |
341 | * actions. Coherency is only guaranteed in the specified range for the | |
342 | * specified access direction. | |
efb4df82 | 343 | * @dmabuf: [in] buffer to complete cpu access for. |
fc13020e DV |
344 | * @start: [in] start of range for cpu access. |
345 | * @len: [in] length of range for cpu access. | |
346 | * @direction: [in] length of range for cpu access. | |
347 | * | |
348 | * This call must always succeed. | |
349 | */ | |
350 | void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len, | |
351 | enum dma_data_direction direction) | |
352 | { | |
353 | WARN_ON(!dmabuf); | |
354 | ||
355 | if (dmabuf->ops->end_cpu_access) | |
356 | dmabuf->ops->end_cpu_access(dmabuf, start, len, direction); | |
357 | } | |
358 | EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access); | |
359 | ||
360 | /** | |
361 | * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address | |
362 | * space. The same restrictions as for kmap_atomic and friends apply. | |
efb4df82 | 363 | * @dmabuf: [in] buffer to map page from. |
fc13020e DV |
364 | * @page_num: [in] page in PAGE_SIZE units to map. |
365 | * | |
366 | * This call must always succeed, any necessary preparations that might fail | |
367 | * need to be done in begin_cpu_access. | |
368 | */ | |
369 | void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num) | |
370 | { | |
371 | WARN_ON(!dmabuf); | |
372 | ||
373 | return dmabuf->ops->kmap_atomic(dmabuf, page_num); | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic); | |
376 | ||
377 | /** | |
378 | * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic. | |
efb4df82 | 379 | * @dmabuf: [in] buffer to unmap page from. |
fc13020e DV |
380 | * @page_num: [in] page in PAGE_SIZE units to unmap. |
381 | * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic. | |
382 | * | |
383 | * This call must always succeed. | |
384 | */ | |
385 | void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num, | |
386 | void *vaddr) | |
387 | { | |
388 | WARN_ON(!dmabuf); | |
389 | ||
390 | if (dmabuf->ops->kunmap_atomic) | |
391 | dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr); | |
392 | } | |
393 | EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic); | |
394 | ||
395 | /** | |
396 | * dma_buf_kmap - Map a page of the buffer object into kernel address space. The | |
397 | * same restrictions as for kmap and friends apply. | |
efb4df82 | 398 | * @dmabuf: [in] buffer to map page from. |
fc13020e DV |
399 | * @page_num: [in] page in PAGE_SIZE units to map. |
400 | * | |
401 | * This call must always succeed, any necessary preparations that might fail | |
402 | * need to be done in begin_cpu_access. | |
403 | */ | |
404 | void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num) | |
405 | { | |
406 | WARN_ON(!dmabuf); | |
407 | ||
408 | return dmabuf->ops->kmap(dmabuf, page_num); | |
409 | } | |
410 | EXPORT_SYMBOL_GPL(dma_buf_kmap); | |
411 | ||
412 | /** | |
413 | * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap. | |
efb4df82 | 414 | * @dmabuf: [in] buffer to unmap page from. |
fc13020e DV |
415 | * @page_num: [in] page in PAGE_SIZE units to unmap. |
416 | * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap. | |
417 | * | |
418 | * This call must always succeed. | |
419 | */ | |
420 | void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num, | |
421 | void *vaddr) | |
422 | { | |
423 | WARN_ON(!dmabuf); | |
424 | ||
425 | if (dmabuf->ops->kunmap) | |
426 | dmabuf->ops->kunmap(dmabuf, page_num, vaddr); | |
427 | } | |
428 | EXPORT_SYMBOL_GPL(dma_buf_kunmap); | |
4c78513e DV |
429 | |
430 | ||
431 | /** | |
432 | * dma_buf_mmap - Setup up a userspace mmap with the given vma | |
12c4727e | 433 | * @dmabuf: [in] buffer that should back the vma |
4c78513e DV |
434 | * @vma: [in] vma for the mmap |
435 | * @pgoff: [in] offset in pages where this mmap should start within the | |
436 | * dma-buf buffer. | |
437 | * | |
438 | * This function adjusts the passed in vma so that it points at the file of the | |
439 | * dma_buf operation. It alsog adjusts the starting pgoff and does bounds | |
440 | * checking on the size of the vma. Then it calls the exporters mmap function to | |
441 | * set up the mapping. | |
442 | * | |
443 | * Can return negative error values, returns 0 on success. | |
444 | */ | |
445 | int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, | |
446 | unsigned long pgoff) | |
447 | { | |
448 | if (WARN_ON(!dmabuf || !vma)) | |
449 | return -EINVAL; | |
450 | ||
451 | /* check for offset overflow */ | |
452 | if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff) | |
453 | return -EOVERFLOW; | |
454 | ||
455 | /* check for overflowing the buffer's size */ | |
456 | if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > | |
457 | dmabuf->size >> PAGE_SHIFT) | |
458 | return -EINVAL; | |
459 | ||
460 | /* readjust the vma */ | |
461 | if (vma->vm_file) | |
462 | fput(vma->vm_file); | |
463 | ||
cb0942b8 | 464 | vma->vm_file = get_file(dmabuf->file); |
4c78513e DV |
465 | |
466 | vma->vm_pgoff = pgoff; | |
467 | ||
468 | return dmabuf->ops->mmap(dmabuf, vma); | |
469 | } | |
470 | EXPORT_SYMBOL_GPL(dma_buf_mmap); | |
98f86c9e DA |
471 | |
472 | /** | |
12c4727e SS |
473 | * dma_buf_vmap - Create virtual mapping for the buffer object into kernel |
474 | * address space. Same restrictions as for vmap and friends apply. | |
475 | * @dmabuf: [in] buffer to vmap | |
98f86c9e DA |
476 | * |
477 | * This call may fail due to lack of virtual mapping address space. | |
478 | * These calls are optional in drivers. The intended use for them | |
479 | * is for mapping objects linear in kernel space for high use objects. | |
480 | * Please attempt to use kmap/kunmap before thinking about these interfaces. | |
481 | */ | |
482 | void *dma_buf_vmap(struct dma_buf *dmabuf) | |
483 | { | |
484 | if (WARN_ON(!dmabuf)) | |
485 | return NULL; | |
486 | ||
487 | if (dmabuf->ops->vmap) | |
488 | return dmabuf->ops->vmap(dmabuf); | |
489 | return NULL; | |
490 | } | |
491 | EXPORT_SYMBOL_GPL(dma_buf_vmap); | |
492 | ||
493 | /** | |
494 | * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap. | |
12c4727e | 495 | * @dmabuf: [in] buffer to vunmap |
6e7b4a59 | 496 | * @vaddr: [in] vmap to vunmap |
98f86c9e DA |
497 | */ |
498 | void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr) | |
499 | { | |
500 | if (WARN_ON(!dmabuf)) | |
501 | return; | |
502 | ||
503 | if (dmabuf->ops->vunmap) | |
504 | dmabuf->ops->vunmap(dmabuf, vaddr); | |
505 | } | |
506 | EXPORT_SYMBOL_GPL(dma_buf_vunmap); |