blob: 2d5ac1a1842ea528d9dfd7d1cad48c42fab8cf8c [file] [log] [blame]
Sumit Semwald15bd7e2011-12-26 14:53:15 +05301/*
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>
Sumit Semwalb89e3562013-04-04 11:44:37 +053030#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Sumit Semwald15bd7e2011-12-26 14:53:15 +053032
33static inline int is_dma_buf_file(struct file *);
34
Sumit Semwalb89e3562013-04-04 11:44:37 +053035struct dma_buf_list {
36 struct list_head head;
37 struct mutex lock;
38};
39
40static struct dma_buf_list db_list;
41
Sumit Semwald15bd7e2011-12-26 14:53:15 +053042static int dma_buf_release(struct inode *inode, struct file *file)
43{
44 struct dma_buf *dmabuf;
45
46 if (!is_dma_buf_file(file))
47 return -EINVAL;
48
49 dmabuf = file->private_data;
50
Daniel Vetterf00b4da2012-12-20 14:14:23 +010051 BUG_ON(dmabuf->vmapping_counter);
52
Sumit Semwald15bd7e2011-12-26 14:53:15 +053053 dmabuf->ops->release(dmabuf);
Sumit Semwalb89e3562013-04-04 11:44:37 +053054
55 mutex_lock(&db_list.lock);
56 list_del(&dmabuf->list_node);
57 mutex_unlock(&db_list.lock);
58
Sumit Semwald15bd7e2011-12-26 14:53:15 +053059 kfree(dmabuf);
60 return 0;
61}
62
Daniel Vetter4c785132012-04-24 14:38:52 +053063static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
64{
65 struct dma_buf *dmabuf;
66
67 if (!is_dma_buf_file(file))
68 return -EINVAL;
69
70 dmabuf = file->private_data;
71
72 /* check for overflowing the buffer's size */
73 if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
74 dmabuf->size >> PAGE_SHIFT)
75 return -EINVAL;
76
77 return dmabuf->ops->mmap(dmabuf, vma);
78}
79
Sumit Semwald15bd7e2011-12-26 14:53:15 +053080static const struct file_operations dma_buf_fops = {
81 .release = dma_buf_release,
Daniel Vetter4c785132012-04-24 14:38:52 +053082 .mmap = dma_buf_mmap_internal,
Sumit Semwald15bd7e2011-12-26 14:53:15 +053083};
84
85/*
86 * is_dma_buf_file - Check if struct file* is associated with dma_buf
87 */
88static inline int is_dma_buf_file(struct file *file)
89{
90 return file->f_op == &dma_buf_fops;
91}
92
93/**
Sumit Semwal78df9692013-03-22 18:22:16 +053094 * dma_buf_export_named - Creates a new dma_buf, and associates an anon file
Sumit Semwald15bd7e2011-12-26 14:53:15 +053095 * with this buffer, so it can be exported.
96 * Also connect the allocator specific data and ops to the buffer.
Sumit Semwal78df9692013-03-22 18:22:16 +053097 * Additionally, provide a name string for exporter; useful in debugging.
Sumit Semwald15bd7e2011-12-26 14:53:15 +053098 *
99 * @priv: [in] Attach private data of allocator to this buffer
100 * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
101 * @size: [in] Size of the buffer
102 * @flags: [in] mode flags for the file.
Sumit Semwal78df9692013-03-22 18:22:16 +0530103 * @exp_name: [in] name of the exporting module - useful for debugging.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530104 *
105 * Returns, on success, a newly created dma_buf object, which wraps the
106 * supplied private data and operations for dma_buf_ops. On either missing
107 * ops, or error in allocating struct dma_buf, will return negative error.
108 *
109 */
Sumit Semwal78df9692013-03-22 18:22:16 +0530110struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops,
111 size_t size, int flags, const char *exp_name)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530112{
113 struct dma_buf *dmabuf;
114 struct file *file;
115
116 if (WARN_ON(!priv || !ops
117 || !ops->map_dma_buf
118 || !ops->unmap_dma_buf
Daniel Vetterfc130202012-03-20 00:02:37 +0100119 || !ops->release
120 || !ops->kmap_atomic
Daniel Vetter4c785132012-04-24 14:38:52 +0530121 || !ops->kmap
122 || !ops->mmap)) {
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530123 return ERR_PTR(-EINVAL);
124 }
125
126 dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
127 if (dmabuf == NULL)
128 return ERR_PTR(-ENOMEM);
129
130 dmabuf->priv = priv;
131 dmabuf->ops = ops;
132 dmabuf->size = size;
Sumit Semwal78df9692013-03-22 18:22:16 +0530133 dmabuf->exp_name = exp_name;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530134
135 file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
Tuomas Tynkkynen9022e242013-08-27 16:30:38 +0300136 if (IS_ERR(file)) {
137 kfree(dmabuf);
138 return ERR_CAST(file);
139 }
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530140 dmabuf->file = file;
141
142 mutex_init(&dmabuf->lock);
143 INIT_LIST_HEAD(&dmabuf->attachments);
144
Sumit Semwalb89e3562013-04-04 11:44:37 +0530145 mutex_lock(&db_list.lock);
146 list_add(&dmabuf->list_node, &db_list.head);
147 mutex_unlock(&db_list.lock);
148
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530149 return dmabuf;
150}
Sumit Semwal78df9692013-03-22 18:22:16 +0530151EXPORT_SYMBOL_GPL(dma_buf_export_named);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530152
153
154/**
155 * dma_buf_fd - returns a file descriptor for the given dma_buf
156 * @dmabuf: [in] pointer to dma_buf for which fd is required.
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000157 * @flags: [in] flags to give to fd
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530158 *
159 * On success, returns an associated 'fd'. Else, returns error.
160 */
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000161int dma_buf_fd(struct dma_buf *dmabuf, int flags)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530162{
Borislav Petkovf5e097f2012-12-11 16:05:26 +0100163 int fd;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530164
165 if (!dmabuf || !dmabuf->file)
166 return -EINVAL;
167
Borislav Petkovf5e097f2012-12-11 16:05:26 +0100168 fd = get_unused_fd_flags(flags);
169 if (fd < 0)
170 return fd;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530171
172 fd_install(fd, dmabuf->file);
173
174 return fd;
175}
176EXPORT_SYMBOL_GPL(dma_buf_fd);
177
178/**
179 * dma_buf_get - returns the dma_buf structure related to an fd
180 * @fd: [in] fd associated with the dma_buf to be returned
181 *
182 * On success, returns the dma_buf structure associated with an fd; uses
183 * file's refcounting done by fget to increase refcount. returns ERR_PTR
184 * otherwise.
185 */
186struct dma_buf *dma_buf_get(int fd)
187{
188 struct file *file;
189
190 file = fget(fd);
191
192 if (!file)
193 return ERR_PTR(-EBADF);
194
195 if (!is_dma_buf_file(file)) {
196 fput(file);
197 return ERR_PTR(-EINVAL);
198 }
199
200 return file->private_data;
201}
202EXPORT_SYMBOL_GPL(dma_buf_get);
203
204/**
205 * dma_buf_put - decreases refcount of the buffer
206 * @dmabuf: [in] buffer to reduce refcount of
207 *
208 * Uses file's refcounting done implicitly by fput()
209 */
210void dma_buf_put(struct dma_buf *dmabuf)
211{
212 if (WARN_ON(!dmabuf || !dmabuf->file))
213 return;
214
215 fput(dmabuf->file);
216}
217EXPORT_SYMBOL_GPL(dma_buf_put);
218
219/**
220 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
221 * calls attach() of dma_buf_ops to allow device-specific attach functionality
222 * @dmabuf: [in] buffer to attach device to.
223 * @dev: [in] device to be attached.
224 *
225 * Returns struct dma_buf_attachment * for this attachment; may return negative
226 * error codes.
227 *
228 */
229struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
230 struct device *dev)
231{
232 struct dma_buf_attachment *attach;
233 int ret;
234
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100235 if (WARN_ON(!dmabuf || !dev))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530236 return ERR_PTR(-EINVAL);
237
238 attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
239 if (attach == NULL)
Laurent Pincharta9fbc3b2012-01-26 12:27:24 +0100240 return ERR_PTR(-ENOMEM);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530241
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530242 attach->dev = dev;
243 attach->dmabuf = dmabuf;
Laurent Pinchart2ed92012012-01-26 12:27:25 +0100244
245 mutex_lock(&dmabuf->lock);
246
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530247 if (dmabuf->ops->attach) {
248 ret = dmabuf->ops->attach(dmabuf, dev, attach);
249 if (ret)
250 goto err_attach;
251 }
252 list_add(&attach->node, &dmabuf->attachments);
253
254 mutex_unlock(&dmabuf->lock);
255 return attach;
256
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530257err_attach:
258 kfree(attach);
259 mutex_unlock(&dmabuf->lock);
260 return ERR_PTR(ret);
261}
262EXPORT_SYMBOL_GPL(dma_buf_attach);
263
264/**
265 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
266 * optionally calls detach() of dma_buf_ops for device-specific detach
267 * @dmabuf: [in] buffer to detach from.
268 * @attach: [in] attachment to be detached; is free'd after this call.
269 *
270 */
271void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
272{
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100273 if (WARN_ON(!dmabuf || !attach))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530274 return;
275
276 mutex_lock(&dmabuf->lock);
277 list_del(&attach->node);
278 if (dmabuf->ops->detach)
279 dmabuf->ops->detach(dmabuf, attach);
280
281 mutex_unlock(&dmabuf->lock);
282 kfree(attach);
283}
284EXPORT_SYMBOL_GPL(dma_buf_detach);
285
286/**
287 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
288 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
289 * dma_buf_ops.
290 * @attach: [in] attachment whose scatterlist is to be returned
291 * @direction: [in] direction of DMA transfer
292 *
293 * Returns sg_table containing the scatterlist to be returned; may return NULL
294 * or ERR_PTR.
295 *
296 */
297struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
298 enum dma_data_direction direction)
299{
300 struct sg_table *sg_table = ERR_PTR(-EINVAL);
301
302 might_sleep();
303
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100304 if (WARN_ON(!attach || !attach->dmabuf))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530305 return ERR_PTR(-EINVAL);
306
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100307 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530308
309 return sg_table;
310}
311EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
312
313/**
314 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
315 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
316 * dma_buf_ops.
317 * @attach: [in] attachment to unmap buffer from
318 * @sg_table: [in] scatterlist info of the buffer to unmap
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530319 * @direction: [in] direction of DMA transfer
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530320 *
321 */
322void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530323 struct sg_table *sg_table,
324 enum dma_data_direction direction)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530325{
Rob Clarkb6fa0cd2012-09-28 09:29:43 +0200326 might_sleep();
327
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100328 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530329 return;
330
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530331 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
332 direction);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530333}
334EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
Daniel Vetterfc130202012-03-20 00:02:37 +0100335
336
337/**
338 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
339 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
340 * preparations. Coherency is only guaranteed in the specified range for the
341 * specified access direction.
Randy Dunlapefb4df82012-04-17 17:03:30 -0700342 * @dmabuf: [in] buffer to prepare cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +0100343 * @start: [in] start of range for cpu access.
344 * @len: [in] length of range for cpu access.
345 * @direction: [in] length of range for cpu access.
346 *
347 * Can return negative error values, returns 0 on success.
348 */
349int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
350 enum dma_data_direction direction)
351{
352 int ret = 0;
353
354 if (WARN_ON(!dmabuf))
355 return -EINVAL;
356
357 if (dmabuf->ops->begin_cpu_access)
358 ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
359
360 return ret;
361}
362EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
363
364/**
365 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
366 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
367 * actions. Coherency is only guaranteed in the specified range for the
368 * specified access direction.
Randy Dunlapefb4df82012-04-17 17:03:30 -0700369 * @dmabuf: [in] buffer to complete cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +0100370 * @start: [in] start of range for cpu access.
371 * @len: [in] length of range for cpu access.
372 * @direction: [in] length of range for cpu access.
373 *
374 * This call must always succeed.
375 */
376void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
377 enum dma_data_direction direction)
378{
379 WARN_ON(!dmabuf);
380
381 if (dmabuf->ops->end_cpu_access)
382 dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
383}
384EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
385
386/**
387 * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
388 * space. The same restrictions as for kmap_atomic and friends apply.
Randy Dunlapefb4df82012-04-17 17:03:30 -0700389 * @dmabuf: [in] buffer to map page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100390 * @page_num: [in] page in PAGE_SIZE units to map.
391 *
392 * This call must always succeed, any necessary preparations that might fail
393 * need to be done in begin_cpu_access.
394 */
395void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
396{
397 WARN_ON(!dmabuf);
398
399 return dmabuf->ops->kmap_atomic(dmabuf, page_num);
400}
401EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
402
403/**
404 * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
Randy Dunlapefb4df82012-04-17 17:03:30 -0700405 * @dmabuf: [in] buffer to unmap page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100406 * @page_num: [in] page in PAGE_SIZE units to unmap.
407 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
408 *
409 * This call must always succeed.
410 */
411void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
412 void *vaddr)
413{
414 WARN_ON(!dmabuf);
415
416 if (dmabuf->ops->kunmap_atomic)
417 dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
418}
419EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
420
421/**
422 * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
423 * same restrictions as for kmap and friends apply.
Randy Dunlapefb4df82012-04-17 17:03:30 -0700424 * @dmabuf: [in] buffer to map page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100425 * @page_num: [in] page in PAGE_SIZE units to map.
426 *
427 * This call must always succeed, any necessary preparations that might fail
428 * need to be done in begin_cpu_access.
429 */
430void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
431{
432 WARN_ON(!dmabuf);
433
434 return dmabuf->ops->kmap(dmabuf, page_num);
435}
436EXPORT_SYMBOL_GPL(dma_buf_kmap);
437
438/**
439 * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
Randy Dunlapefb4df82012-04-17 17:03:30 -0700440 * @dmabuf: [in] buffer to unmap page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100441 * @page_num: [in] page in PAGE_SIZE units to unmap.
442 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
443 *
444 * This call must always succeed.
445 */
446void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
447 void *vaddr)
448{
449 WARN_ON(!dmabuf);
450
451 if (dmabuf->ops->kunmap)
452 dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
453}
454EXPORT_SYMBOL_GPL(dma_buf_kunmap);
Daniel Vetter4c785132012-04-24 14:38:52 +0530455
456
457/**
458 * dma_buf_mmap - Setup up a userspace mmap with the given vma
Sumit Semwal12c47272012-05-23 15:27:40 +0530459 * @dmabuf: [in] buffer that should back the vma
Daniel Vetter4c785132012-04-24 14:38:52 +0530460 * @vma: [in] vma for the mmap
461 * @pgoff: [in] offset in pages where this mmap should start within the
462 * dma-buf buffer.
463 *
464 * This function adjusts the passed in vma so that it points at the file of the
465 * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
466 * checking on the size of the vma. Then it calls the exporters mmap function to
467 * set up the mapping.
468 *
469 * Can return negative error values, returns 0 on success.
470 */
471int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
472 unsigned long pgoff)
473{
John Sheu495c10c2013-02-11 17:50:24 -0800474 struct file *oldfile;
475 int ret;
476
Daniel Vetter4c785132012-04-24 14:38:52 +0530477 if (WARN_ON(!dmabuf || !vma))
478 return -EINVAL;
479
480 /* check for offset overflow */
481 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
482 return -EOVERFLOW;
483
484 /* check for overflowing the buffer's size */
485 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
486 dmabuf->size >> PAGE_SHIFT)
487 return -EINVAL;
488
489 /* readjust the vma */
John Sheu495c10c2013-02-11 17:50:24 -0800490 get_file(dmabuf->file);
491 oldfile = vma->vm_file;
492 vma->vm_file = dmabuf->file;
Daniel Vetter4c785132012-04-24 14:38:52 +0530493 vma->vm_pgoff = pgoff;
494
John Sheu495c10c2013-02-11 17:50:24 -0800495 ret = dmabuf->ops->mmap(dmabuf, vma);
496 if (ret) {
497 /* restore old parameters on failure */
498 vma->vm_file = oldfile;
499 fput(dmabuf->file);
500 } else {
501 if (oldfile)
502 fput(oldfile);
503 }
504 return ret;
505
Daniel Vetter4c785132012-04-24 14:38:52 +0530506}
507EXPORT_SYMBOL_GPL(dma_buf_mmap);
Dave Airlie98f86c92012-05-20 12:33:56 +0530508
509/**
Sumit Semwal12c47272012-05-23 15:27:40 +0530510 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
511 * address space. Same restrictions as for vmap and friends apply.
512 * @dmabuf: [in] buffer to vmap
Dave Airlie98f86c92012-05-20 12:33:56 +0530513 *
514 * This call may fail due to lack of virtual mapping address space.
515 * These calls are optional in drivers. The intended use for them
516 * is for mapping objects linear in kernel space for high use objects.
517 * Please attempt to use kmap/kunmap before thinking about these interfaces.
518 */
519void *dma_buf_vmap(struct dma_buf *dmabuf)
520{
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100521 void *ptr;
522
Dave Airlie98f86c92012-05-20 12:33:56 +0530523 if (WARN_ON(!dmabuf))
524 return NULL;
525
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100526 if (!dmabuf->ops->vmap)
527 return NULL;
528
529 mutex_lock(&dmabuf->lock);
530 if (dmabuf->vmapping_counter) {
531 dmabuf->vmapping_counter++;
532 BUG_ON(!dmabuf->vmap_ptr);
533 ptr = dmabuf->vmap_ptr;
534 goto out_unlock;
535 }
536
537 BUG_ON(dmabuf->vmap_ptr);
538
539 ptr = dmabuf->ops->vmap(dmabuf);
540 if (IS_ERR_OR_NULL(ptr))
541 goto out_unlock;
542
543 dmabuf->vmap_ptr = ptr;
544 dmabuf->vmapping_counter = 1;
545
546out_unlock:
547 mutex_unlock(&dmabuf->lock);
548 return ptr;
Dave Airlie98f86c92012-05-20 12:33:56 +0530549}
550EXPORT_SYMBOL_GPL(dma_buf_vmap);
551
552/**
553 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
Sumit Semwal12c47272012-05-23 15:27:40 +0530554 * @dmabuf: [in] buffer to vunmap
Randy Dunlap6e7b4a52012-06-09 15:02:59 -0700555 * @vaddr: [in] vmap to vunmap
Dave Airlie98f86c92012-05-20 12:33:56 +0530556 */
557void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
558{
559 if (WARN_ON(!dmabuf))
560 return;
561
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100562 BUG_ON(!dmabuf->vmap_ptr);
563 BUG_ON(dmabuf->vmapping_counter == 0);
564 BUG_ON(dmabuf->vmap_ptr != vaddr);
565
566 mutex_lock(&dmabuf->lock);
567 if (--dmabuf->vmapping_counter == 0) {
568 if (dmabuf->ops->vunmap)
569 dmabuf->ops->vunmap(dmabuf, vaddr);
570 dmabuf->vmap_ptr = NULL;
571 }
572 mutex_unlock(&dmabuf->lock);
Dave Airlie98f86c92012-05-20 12:33:56 +0530573}
574EXPORT_SYMBOL_GPL(dma_buf_vunmap);
Sumit Semwalb89e3562013-04-04 11:44:37 +0530575
576#ifdef CONFIG_DEBUG_FS
577static int dma_buf_describe(struct seq_file *s)
578{
579 int ret;
580 struct dma_buf *buf_obj;
581 struct dma_buf_attachment *attach_obj;
582 int count = 0, attach_count;
583 size_t size = 0;
584
585 ret = mutex_lock_interruptible(&db_list.lock);
586
587 if (ret)
588 return ret;
589
590 seq_printf(s, "\nDma-buf Objects:\n");
591 seq_printf(s, "\texp_name\tsize\tflags\tmode\tcount\n");
592
593 list_for_each_entry(buf_obj, &db_list.head, list_node) {
594 ret = mutex_lock_interruptible(&buf_obj->lock);
595
596 if (ret) {
597 seq_printf(s,
598 "\tERROR locking buffer object: skipping\n");
599 continue;
600 }
601
602 seq_printf(s, "\t");
603
604 seq_printf(s, "\t%s\t%08zu\t%08x\t%08x\t%08ld\n",
605 buf_obj->exp_name, buf_obj->size,
606 buf_obj->file->f_flags, buf_obj->file->f_mode,
607 (long)(buf_obj->file->f_count.counter));
608
609 seq_printf(s, "\t\tAttached Devices:\n");
610 attach_count = 0;
611
612 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
613 seq_printf(s, "\t\t");
614
615 seq_printf(s, "%s\n", attach_obj->dev->init_name);
616 attach_count++;
617 }
618
619 seq_printf(s, "\n\t\tTotal %d devices attached\n",
620 attach_count);
621
622 count++;
623 size += buf_obj->size;
624 mutex_unlock(&buf_obj->lock);
625 }
626
627 seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
628
629 mutex_unlock(&db_list.lock);
630 return 0;
631}
632
633static int dma_buf_show(struct seq_file *s, void *unused)
634{
635 void (*func)(struct seq_file *) = s->private;
636 func(s);
637 return 0;
638}
639
640static int dma_buf_debug_open(struct inode *inode, struct file *file)
641{
642 return single_open(file, dma_buf_show, inode->i_private);
643}
644
645static const struct file_operations dma_buf_debug_fops = {
646 .open = dma_buf_debug_open,
647 .read = seq_read,
648 .llseek = seq_lseek,
649 .release = single_release,
650};
651
652static struct dentry *dma_buf_debugfs_dir;
653
654static int dma_buf_init_debugfs(void)
655{
656 int err = 0;
657 dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL);
658 if (IS_ERR(dma_buf_debugfs_dir)) {
659 err = PTR_ERR(dma_buf_debugfs_dir);
660 dma_buf_debugfs_dir = NULL;
661 return err;
662 }
663
664 err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe);
665
666 if (err)
667 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
668
669 return err;
670}
671
672static void dma_buf_uninit_debugfs(void)
673{
674 if (dma_buf_debugfs_dir)
675 debugfs_remove_recursive(dma_buf_debugfs_dir);
676}
677
678int dma_buf_debugfs_create_file(const char *name,
679 int (*write)(struct seq_file *))
680{
681 struct dentry *d;
682
683 d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir,
684 write, &dma_buf_debug_fops);
685
Sachin Kamat28ce4202013-07-15 15:51:22 +0530686 return PTR_ERR_OR_ZERO(d);
Sumit Semwalb89e3562013-04-04 11:44:37 +0530687}
688#else
689static inline int dma_buf_init_debugfs(void)
690{
691 return 0;
692}
693static inline void dma_buf_uninit_debugfs(void)
694{
695}
696#endif
697
698static int __init dma_buf_init(void)
699{
700 mutex_init(&db_list.lock);
701 INIT_LIST_HEAD(&db_list.head);
702 dma_buf_init_debugfs();
703 return 0;
704}
705subsys_initcall(dma_buf_init);
706
707static void __exit dma_buf_deinit(void)
708{
709 dma_buf_uninit_debugfs();
710}
711__exitcall(dma_buf_deinit);