Commit | Line | Data |
---|---|---|
004cc378 MS |
1 | /* |
2 | * videobuf2-memops.c - generic memory handling routines for videobuf2 | |
3 | * | |
4 | * Copyright (C) 2010 Samsung Electronics | |
5 | * | |
95072084 | 6 | * Author: Pawel Osciak <pawel@osciak.com> |
004cc378 MS |
7 | * 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> | |
004cc378 MS |
21 | |
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 | */ | |
36 | struct vm_area_struct *vb2_get_vma(struct vm_area_struct *vma) | |
37 | { | |
38 | struct vm_area_struct *vma_copy; | |
39 | ||
09bf4a85 | 40 | vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL); |
004cc378 MS |
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 | } | |
43c28602 | 58 | EXPORT_SYMBOL_GPL(vb2_get_vma); |
004cc378 MS |
59 | |
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 | */ | |
67 | void vb2_put_vma(struct vm_area_struct *vma) | |
68 | { | |
69 | if (!vma) | |
70 | return; | |
71 | ||
004cc378 MS |
72 | if (vma->vm_ops && vma->vm_ops->close) |
73 | vma->vm_ops->close(vma); | |
74 | ||
8607c425 YT |
75 | if (vma->vm_file) |
76 | fput(vma->vm_file); | |
77 | ||
09bf4a85 | 78 | kfree(vma); |
004cc378 | 79 | } |
bd94f588 | 80 | EXPORT_SYMBOL_GPL(vb2_put_vma); |
004cc378 MS |
81 | |
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 | */ | |
96 | int 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; | |
004cc378 MS |
104 | |
105 | start = vaddr; | |
106 | offset = start & ~PAGE_MASK; | |
107 | end = start + size; | |
108 | ||
004cc378 MS |
109 | vma = find_vma(mm, start); |
110 | ||
111 | if (vma == NULL || vma->vm_end < end) | |
b037c0fd | 112 | return -EFAULT; |
004cc378 MS |
113 | |
114 | for (prev_pfn = 0; start < end; start += PAGE_SIZE) { | |
b037c0fd | 115 | int ret = follow_pfn(vma, start, &this_pfn); |
004cc378 | 116 | if (ret) |
b037c0fd | 117 | return ret; |
004cc378 MS |
118 | |
119 | if (prev_pfn == 0) | |
120 | pa = this_pfn << PAGE_SHIFT; | |
b037c0fd MS |
121 | else if (this_pfn != prev_pfn + 1) |
122 | return -EFAULT; | |
123 | ||
004cc378 MS |
124 | 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); | |
b037c0fd MS |
131 | if (*res_vma == NULL) |
132 | return -ENOMEM; | |
004cc378 | 133 | |
b037c0fd MS |
134 | *res_pa = pa + offset; |
135 | return 0; | |
004cc378 | 136 | } |
bd94f588 | 137 | EXPORT_SYMBOL_GPL(vb2_get_contig_userptr); |
004cc378 | 138 | |
21fb0cb7 JK |
139 | /** |
140 | * vb2_create_framevec() - map virtual addresses to pfns | |
141 | * @start: Virtual user address where we start mapping | |
142 | * @length: Length of a range to map | |
143 | * @write: Should we map for writing into the area | |
144 | * | |
145 | * This function allocates and fills in a vector with pfns corresponding to | |
146 | * virtual address range passed in arguments. If pfns have corresponding pages, | |
147 | * page references are also grabbed to pin pages in memory. The function | |
148 | * returns pointer to the vector on success and error pointer in case of | |
149 | * failure. Returned vector needs to be freed via vb2_destroy_pfnvec(). | |
150 | */ | |
151 | struct frame_vector *vb2_create_framevec(unsigned long start, | |
152 | unsigned long length, | |
153 | bool write) | |
154 | { | |
155 | int ret; | |
156 | unsigned long first, last; | |
157 | unsigned long nr; | |
158 | struct frame_vector *vec; | |
159 | ||
160 | first = start >> PAGE_SHIFT; | |
161 | last = (start + length - 1) >> PAGE_SHIFT; | |
162 | nr = last - first + 1; | |
163 | vec = frame_vector_create(nr); | |
164 | if (!vec) | |
165 | return ERR_PTR(-ENOMEM); | |
166 | ret = get_vaddr_frames(start, nr, write, 1, vec); | |
167 | if (ret < 0) | |
168 | goto out_destroy; | |
169 | /* We accept only complete set of PFNs */ | |
170 | if (ret != nr) { | |
171 | ret = -EFAULT; | |
172 | goto out_release; | |
173 | } | |
174 | return vec; | |
175 | out_release: | |
176 | put_vaddr_frames(vec); | |
177 | out_destroy: | |
178 | frame_vector_destroy(vec); | |
179 | return ERR_PTR(ret); | |
180 | } | |
181 | EXPORT_SYMBOL(vb2_create_framevec); | |
182 | ||
183 | /** | |
184 | * vb2_destroy_framevec() - release vector of mapped pfns | |
185 | * @vec: vector of pfns / pages to release | |
186 | * | |
187 | * This releases references to all pages in the vector @vec (if corresponding | |
188 | * pfns are backed by pages) and frees the passed vector. | |
189 | */ | |
190 | void vb2_destroy_framevec(struct frame_vector *vec) | |
191 | { | |
192 | put_vaddr_frames(vec); | |
193 | frame_vector_destroy(vec); | |
194 | } | |
195 | EXPORT_SYMBOL(vb2_destroy_framevec); | |
196 | ||
004cc378 MS |
197 | /** |
198 | * vb2_common_vm_open() - increase refcount of the vma | |
199 | * @vma: virtual memory region for the mapping | |
200 | * | |
201 | * This function adds another user to the provided vma. It expects | |
202 | * struct vb2_vmarea_handler pointer in vma->vm_private_data. | |
203 | */ | |
204 | static void vb2_common_vm_open(struct vm_area_struct *vma) | |
205 | { | |
206 | struct vb2_vmarea_handler *h = vma->vm_private_data; | |
207 | ||
7f116b3e | 208 | pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n", |
004cc378 MS |
209 | __func__, h, atomic_read(h->refcount), vma->vm_start, |
210 | vma->vm_end); | |
211 | ||
212 | atomic_inc(h->refcount); | |
213 | } | |
214 | ||
215 | /** | |
216 | * vb2_common_vm_close() - decrease refcount of the vma | |
217 | * @vma: virtual memory region for the mapping | |
218 | * | |
219 | * This function releases the user from the provided vma. It expects | |
220 | * struct vb2_vmarea_handler pointer in vma->vm_private_data. | |
221 | */ | |
222 | static void vb2_common_vm_close(struct vm_area_struct *vma) | |
223 | { | |
224 | struct vb2_vmarea_handler *h = vma->vm_private_data; | |
225 | ||
7f116b3e | 226 | pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n", |
004cc378 MS |
227 | __func__, h, atomic_read(h->refcount), vma->vm_start, |
228 | vma->vm_end); | |
229 | ||
230 | h->put(h->arg); | |
231 | } | |
232 | ||
233 | /** | |
234 | * vb2_common_vm_ops - common vm_ops used for tracking refcount of mmaped | |
235 | * video buffers | |
236 | */ | |
237 | const struct vm_operations_struct vb2_common_vm_ops = { | |
238 | .open = vb2_common_vm_open, | |
239 | .close = vb2_common_vm_close, | |
240 | }; | |
241 | EXPORT_SYMBOL_GPL(vb2_common_vm_ops); | |
242 | ||
243 | MODULE_DESCRIPTION("common memory handling routines for videobuf2"); | |
95072084 | 244 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>"); |
004cc378 | 245 | MODULE_LICENSE("GPL"); |