blob: 81c1ad8b2cf1eb9582443273c427720be71b04d7 [file] [log] [blame]
Marek Szyprowski004cc372010-12-09 10:20:47 -03001/*
2 * videobuf2-memops.c - generic memory handling routines for videobuf2
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Marek Szyprowski004cc372010-12-09 10:20:47 -03007 * Marek Szyprowski <m.szyprowski@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/slab.h>
15#include <linux/module.h>
16#include <linux/dma-mapping.h>
17#include <linux/vmalloc.h>
18#include <linux/mm.h>
19#include <linux/sched.h>
20#include <linux/file.h>
Marek Szyprowski004cc372010-12-09 10:20:47 -030021
22#include <media/videobuf2-core.h>
23#include <media/videobuf2-memops.h>
24
25/**
26 * vb2_get_vma() - acquire and lock the virtual memory area
27 * @vma: given virtual memory area
28 *
29 * This function attempts to acquire an area mapped in the userspace for
30 * the duration of a hardware operation. The area is "locked" by performing
31 * the same set of operation that are done when process calls fork() and
32 * memory areas are duplicated.
33 *
34 * Returns a copy of a virtual memory region on success or NULL.
35 */
36struct vm_area_struct *vb2_get_vma(struct vm_area_struct *vma)
37{
38 struct vm_area_struct *vma_copy;
39
40 vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL);
41 if (vma_copy == NULL)
42 return NULL;
43
44 if (vma->vm_ops && vma->vm_ops->open)
45 vma->vm_ops->open(vma);
46
47 if (vma->vm_file)
48 get_file(vma->vm_file);
49
50 memcpy(vma_copy, vma, sizeof(*vma));
51
52 vma_copy->vm_mm = NULL;
53 vma_copy->vm_next = NULL;
54 vma_copy->vm_prev = NULL;
55
56 return vma_copy;
57}
Laurent Pinchart43c28602012-01-23 10:35:38 -030058EXPORT_SYMBOL_GPL(vb2_get_vma);
Marek Szyprowski004cc372010-12-09 10:20:47 -030059
60/**
61 * vb2_put_userptr() - release a userspace virtual memory area
62 * @vma: virtual memory region associated with the area to be released
63 *
64 * This function releases the previously acquired memory area after a hardware
65 * operation.
66 */
67void vb2_put_vma(struct vm_area_struct *vma)
68{
69 if (!vma)
70 return;
71
Marek Szyprowski004cc372010-12-09 10:20:47 -030072 if (vma->vm_ops && vma->vm_ops->close)
73 vma->vm_ops->close(vma);
74
Yu Tang8607c422011-08-30 02:31:54 -030075 if (vma->vm_file)
76 fput(vma->vm_file);
77
Marek Szyprowski004cc372010-12-09 10:20:47 -030078 kfree(vma);
79}
Guennadi Liakhovetskibd94f582011-01-28 21:06:19 -030080EXPORT_SYMBOL_GPL(vb2_put_vma);
Marek Szyprowski004cc372010-12-09 10:20:47 -030081
82/**
83 * vb2_get_contig_userptr() - lock physically contiguous userspace mapped memory
84 * @vaddr: starting virtual address of the area to be verified
85 * @size: size of the area
86 * @res_paddr: will return physical address for the given vaddr
87 * @res_vma: will return locked copy of struct vm_area for the given area
88 *
89 * This function will go through memory area of size @size mapped at @vaddr and
90 * verify that the underlying physical pages are contiguous. If they are
91 * contiguous the virtual memory area is locked and a @res_vma is filled with
92 * the copy and @res_pa set to the physical address of the buffer.
93 *
94 * Returns 0 on success.
95 */
96int vb2_get_contig_userptr(unsigned long vaddr, unsigned long size,
97 struct vm_area_struct **res_vma, dma_addr_t *res_pa)
98{
99 struct mm_struct *mm = current->mm;
100 struct vm_area_struct *vma;
101 unsigned long offset, start, end;
102 unsigned long this_pfn, prev_pfn;
103 dma_addr_t pa = 0;
Marek Szyprowski004cc372010-12-09 10:20:47 -0300104
105 start = vaddr;
106 offset = start & ~PAGE_MASK;
107 end = start + size;
108
Marek Szyprowski004cc372010-12-09 10:20:47 -0300109 vma = find_vma(mm, start);
110
111 if (vma == NULL || vma->vm_end < end)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -0300112 return -EFAULT;
Marek Szyprowski004cc372010-12-09 10:20:47 -0300113
114 for (prev_pfn = 0; start < end; start += PAGE_SIZE) {
Marek Szyprowskib037c0f2011-11-17 05:32:17 -0300115 int ret = follow_pfn(vma, start, &this_pfn);
Marek Szyprowski004cc372010-12-09 10:20:47 -0300116 if (ret)
Marek Szyprowskib037c0f2011-11-17 05:32:17 -0300117 return ret;
Marek Szyprowski004cc372010-12-09 10:20:47 -0300118
119 if (prev_pfn == 0)
120 pa = this_pfn << PAGE_SHIFT;
Marek Szyprowskib037c0f2011-11-17 05:32:17 -0300121 else if (this_pfn != prev_pfn + 1)
122 return -EFAULT;
123
Marek Szyprowski004cc372010-12-09 10:20:47 -0300124 prev_pfn = this_pfn;
125 }
126
127 /*
128 * Memory is contigous, lock vma and return to the caller
129 */
130 *res_vma = vb2_get_vma(vma);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -0300131 if (*res_vma == NULL)
132 return -ENOMEM;
Marek Szyprowski004cc372010-12-09 10:20:47 -0300133
Marek Szyprowskib037c0f2011-11-17 05:32:17 -0300134 *res_pa = pa + offset;
135 return 0;
Marek Szyprowski004cc372010-12-09 10:20:47 -0300136}
Guennadi Liakhovetskibd94f582011-01-28 21:06:19 -0300137EXPORT_SYMBOL_GPL(vb2_get_contig_userptr);
Marek Szyprowski004cc372010-12-09 10:20:47 -0300138
139/**
Marek Szyprowski004cc372010-12-09 10:20:47 -0300140 * vb2_common_vm_open() - increase refcount of the vma
141 * @vma: virtual memory region for the mapping
142 *
143 * This function adds another user to the provided vma. It expects
144 * struct vb2_vmarea_handler pointer in vma->vm_private_data.
145 */
146static void vb2_common_vm_open(struct vm_area_struct *vma)
147{
148 struct vb2_vmarea_handler *h = vma->vm_private_data;
149
Uwe Kleine-König7f116b32011-06-01 17:19:22 -0300150 pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
Marek Szyprowski004cc372010-12-09 10:20:47 -0300151 __func__, h, atomic_read(h->refcount), vma->vm_start,
152 vma->vm_end);
153
154 atomic_inc(h->refcount);
155}
156
157/**
158 * vb2_common_vm_close() - decrease refcount of the vma
159 * @vma: virtual memory region for the mapping
160 *
161 * This function releases the user from the provided vma. It expects
162 * struct vb2_vmarea_handler pointer in vma->vm_private_data.
163 */
164static void vb2_common_vm_close(struct vm_area_struct *vma)
165{
166 struct vb2_vmarea_handler *h = vma->vm_private_data;
167
Uwe Kleine-König7f116b32011-06-01 17:19:22 -0300168 pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
Marek Szyprowski004cc372010-12-09 10:20:47 -0300169 __func__, h, atomic_read(h->refcount), vma->vm_start,
170 vma->vm_end);
171
172 h->put(h->arg);
173}
174
175/**
176 * vb2_common_vm_ops - common vm_ops used for tracking refcount of mmaped
177 * video buffers
178 */
179const struct vm_operations_struct vb2_common_vm_ops = {
180 .open = vb2_common_vm_open,
181 .close = vb2_common_vm_close,
182};
183EXPORT_SYMBOL_GPL(vb2_common_vm_ops);
184
185MODULE_DESCRIPTION("common memory handling routines for videobuf2");
Pawel Osciak95072082011-03-13 15:23:32 -0300186MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
Marek Szyprowski004cc372010-12-09 10:20:47 -0300187MODULE_LICENSE("GPL");