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