blob: 6b54a14ee8273695d1666ca20dc8b2d3f9311c92 [file] [log] [blame]
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -03001/*
2 * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
6 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/mm.h>
15#include <linux/scatterlist.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19
20#include <media/videobuf2-core.h>
21#include <media/videobuf2-memops.h>
22#include <media/videobuf2-dma-sg.h>
23
Hans Verkuilffdc78e2013-03-02 05:12:08 -030024static int debug;
25module_param(debug, int, 0644);
26
27#define dprintk(level, fmt, arg...) \
28 do { \
29 if (debug >= level) \
30 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
31 } while (0)
32
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030033struct vb2_dma_sg_buf {
34 void *vaddr;
35 struct page **pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030036 int offset;
Hans Verkuilcd474032014-11-18 09:50:58 -030037 enum dma_data_direction dma_dir;
Ricardo Ribalda22301242013-08-02 10:20:00 -030038 struct sg_table sg_table;
39 size_t size;
40 unsigned int num_pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030041 atomic_t refcount;
42 struct vb2_vmarea_handler handler;
Ricardo Ribalda50ac9522013-11-26 09:58:44 -030043 struct vm_area_struct *vma;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030044};
45
46static void vb2_dma_sg_put(void *buf_priv);
47
Ricardo Ribaldadf237282013-08-02 10:19:59 -030048static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
49 gfp_t gfp_flags)
50{
51 unsigned int last_page = 0;
Ricardo Ribalda22301242013-08-02 10:20:00 -030052 int size = buf->size;
Ricardo Ribaldadf237282013-08-02 10:19:59 -030053
54 while (size > 0) {
55 struct page *pages;
56 int order;
57 int i;
58
59 order = get_order(size);
60 /* Dont over allocate*/
61 if ((PAGE_SIZE << order) > size)
62 order--;
63
64 pages = NULL;
65 while (!pages) {
66 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
67 __GFP_NOWARN | gfp_flags, order);
68 if (pages)
69 break;
70
71 if (order == 0) {
72 while (last_page--)
73 __free_page(buf->pages[last_page]);
74 return -ENOMEM;
75 }
76 order--;
77 }
78
79 split_page(pages, order);
Ricardo Ribalda22301242013-08-02 10:20:00 -030080 for (i = 0; i < (1 << order); i++)
81 buf->pages[last_page++] = &pages[i];
Ricardo Ribaldadf237282013-08-02 10:19:59 -030082
83 size -= PAGE_SIZE << order;
84 }
85
86 return 0;
87}
88
Hans Verkuilb6ba2052013-03-01 15:44:20 -030089static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size, gfp_t gfp_flags)
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030090{
91 struct vb2_dma_sg_buf *buf;
Ricardo Ribaldadf237282013-08-02 10:19:59 -030092 int ret;
Ricardo Ribalda22301242013-08-02 10:20:00 -030093 int num_pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030094
95 buf = kzalloc(sizeof *buf, GFP_KERNEL);
96 if (!buf)
97 return NULL;
98
99 buf->vaddr = NULL;
Hans Verkuilcd474032014-11-18 09:50:58 -0300100 buf->dma_dir = DMA_NONE;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300101 buf->offset = 0;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300102 buf->size = size;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -0300103 /* size is already page aligned */
Ricardo Ribalda22301242013-08-02 10:20:00 -0300104 buf->num_pages = size >> PAGE_SHIFT;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300105
Ricardo Ribalda22301242013-08-02 10:20:00 -0300106 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300107 GFP_KERNEL);
108 if (!buf->pages)
109 goto fail_pages_array_alloc;
110
Ricardo Ribaldadf237282013-08-02 10:19:59 -0300111 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
112 if (ret)
113 goto fail_pages_alloc;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300114
Ricardo Ribalda22301242013-08-02 10:20:00 -0300115 ret = sg_alloc_table_from_pages(&buf->sg_table, buf->pages,
Hans Verkuil47bc59c2014-08-01 09:18:50 -0300116 buf->num_pages, 0, size, GFP_KERNEL);
Ricardo Ribalda22301242013-08-02 10:20:00 -0300117 if (ret)
118 goto fail_table_alloc;
119
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300120 buf->handler.refcount = &buf->refcount;
121 buf->handler.put = vb2_dma_sg_put;
122 buf->handler.arg = buf;
123
124 atomic_inc(&buf->refcount);
125
Hans Verkuilffdc78e2013-03-02 05:12:08 -0300126 dprintk(1, "%s: Allocated buffer of %d pages\n",
Ricardo Ribalda22301242013-08-02 10:20:00 -0300127 __func__, buf->num_pages);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300128 return buf;
129
Ricardo Ribalda22301242013-08-02 10:20:00 -0300130fail_table_alloc:
131 num_pages = buf->num_pages;
132 while (num_pages--)
133 __free_page(buf->pages[num_pages]);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300134fail_pages_alloc:
Andrzej Pietrasiewicza9bb36a2011-01-28 09:42:51 -0300135 kfree(buf->pages);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300136fail_pages_array_alloc:
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300137 kfree(buf);
138 return NULL;
139}
140
141static void vb2_dma_sg_put(void *buf_priv)
142{
143 struct vb2_dma_sg_buf *buf = buf_priv;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300144 int i = buf->num_pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300145
146 if (atomic_dec_and_test(&buf->refcount)) {
Hans Verkuilffdc78e2013-03-02 05:12:08 -0300147 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
Ricardo Ribalda22301242013-08-02 10:20:00 -0300148 buf->num_pages);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300149 if (buf->vaddr)
Ricardo Ribalda22301242013-08-02 10:20:00 -0300150 vm_unmap_ram(buf->vaddr, buf->num_pages);
151 sg_free_table(&buf->sg_table);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300152 while (--i >= 0)
153 __free_page(buf->pages[i]);
154 kfree(buf->pages);
155 kfree(buf);
156 }
157}
158
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300159static inline int vma_is_io(struct vm_area_struct *vma)
160{
161 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
162}
163
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300164static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
Hans Verkuilcd474032014-11-18 09:50:58 -0300165 unsigned long size,
166 enum dma_data_direction dma_dir)
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300167{
168 struct vb2_dma_sg_buf *buf;
169 unsigned long first, last;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300170 int num_pages_from_user;
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300171 struct vm_area_struct *vma;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300172
173 buf = kzalloc(sizeof *buf, GFP_KERNEL);
174 if (!buf)
175 return NULL;
176
177 buf->vaddr = NULL;
Hans Verkuilcd474032014-11-18 09:50:58 -0300178 buf->dma_dir = dma_dir;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300179 buf->offset = vaddr & ~PAGE_MASK;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300180 buf->size = size;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300181
182 first = (vaddr & PAGE_MASK) >> PAGE_SHIFT;
183 last = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300184 buf->num_pages = last - first + 1;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300185
Ricardo Ribalda22301242013-08-02 10:20:00 -0300186 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300187 GFP_KERNEL);
188 if (!buf->pages)
Geyslan G. Bem64c832a2013-11-20 18:02:52 -0300189 goto userptr_fail_alloc_pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300190
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300191 vma = find_vma(current->mm, vaddr);
192 if (!vma) {
193 dprintk(1, "no vma for address %lu\n", vaddr);
194 goto userptr_fail_find_vma;
195 }
196
197 if (vma->vm_end < vaddr + size) {
198 dprintk(1, "vma at %lu is too small for %lu bytes\n",
199 vaddr, size);
200 goto userptr_fail_find_vma;
201 }
202
203 buf->vma = vb2_get_vma(vma);
204 if (!buf->vma) {
205 dprintk(1, "failed to copy vma\n");
206 goto userptr_fail_find_vma;
207 }
208
209 if (vma_is_io(buf->vma)) {
210 for (num_pages_from_user = 0;
211 num_pages_from_user < buf->num_pages;
212 ++num_pages_from_user, vaddr += PAGE_SIZE) {
213 unsigned long pfn;
214
Ricardo Ribalda227ae222014-04-25 13:11:29 -0300215 if (follow_pfn(vma, vaddr, &pfn)) {
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300216 dprintk(1, "no page for address %lu\n", vaddr);
217 break;
218 }
219 buf->pages[num_pages_from_user] = pfn_to_page(pfn);
220 }
221 } else
222 num_pages_from_user = get_user_pages(current, current->mm,
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300223 vaddr & PAGE_MASK,
Ricardo Ribalda22301242013-08-02 10:20:00 -0300224 buf->num_pages,
Hans Verkuilcd474032014-11-18 09:50:58 -0300225 buf->dma_dir == DMA_FROM_DEVICE,
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300226 1, /* force */
227 buf->pages,
228 NULL);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -0300229
Ricardo Ribalda22301242013-08-02 10:20:00 -0300230 if (num_pages_from_user != buf->num_pages)
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300231 goto userptr_fail_get_user_pages;
232
Ricardo Ribalda22301242013-08-02 10:20:00 -0300233 if (sg_alloc_table_from_pages(&buf->sg_table, buf->pages,
234 buf->num_pages, buf->offset, size, 0))
235 goto userptr_fail_alloc_table_from_pages;
236
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300237 return buf;
238
Ricardo Ribalda22301242013-08-02 10:20:00 -0300239userptr_fail_alloc_table_from_pages:
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300240userptr_fail_get_user_pages:
Hans Verkuilffdc78e2013-03-02 05:12:08 -0300241 dprintk(1, "get_user_pages requested/got: %d/%d]\n",
Ricardo Ribalda202dfbd2013-11-06 15:48:38 -0300242 buf->num_pages, num_pages_from_user);
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300243 if (!vma_is_io(buf->vma))
244 while (--num_pages_from_user >= 0)
245 put_page(buf->pages[num_pages_from_user]);
246 vb2_put_vma(buf->vma);
247userptr_fail_find_vma:
Andrzej Pietrasiewicza9bb36a2011-01-28 09:42:51 -0300248 kfree(buf->pages);
Geyslan G. Bem64c832a2013-11-20 18:02:52 -0300249userptr_fail_alloc_pages:
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300250 kfree(buf);
251 return NULL;
252}
253
254/*
255 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
256 * be used
257 */
258static void vb2_dma_sg_put_userptr(void *buf_priv)
259{
260 struct vb2_dma_sg_buf *buf = buf_priv;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300261 int i = buf->num_pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300262
Hans Verkuilffdc78e2013-03-02 05:12:08 -0300263 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
Ricardo Ribalda22301242013-08-02 10:20:00 -0300264 __func__, buf->num_pages);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300265 if (buf->vaddr)
Ricardo Ribalda22301242013-08-02 10:20:00 -0300266 vm_unmap_ram(buf->vaddr, buf->num_pages);
267 sg_free_table(&buf->sg_table);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300268 while (--i >= 0) {
Hans Verkuilcd474032014-11-18 09:50:58 -0300269 if (buf->dma_dir == DMA_FROM_DEVICE)
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300270 set_page_dirty_lock(buf->pages[i]);
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300271 if (!vma_is_io(buf->vma))
272 put_page(buf->pages[i]);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300273 }
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300274 kfree(buf->pages);
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300275 vb2_put_vma(buf->vma);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300276 kfree(buf);
277}
278
279static void *vb2_dma_sg_vaddr(void *buf_priv)
280{
281 struct vb2_dma_sg_buf *buf = buf_priv;
282
283 BUG_ON(!buf);
284
285 if (!buf->vaddr)
286 buf->vaddr = vm_map_ram(buf->pages,
Ricardo Ribalda22301242013-08-02 10:20:00 -0300287 buf->num_pages,
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300288 -1,
289 PAGE_KERNEL);
290
291 /* add offset in case userptr is not page-aligned */
292 return buf->vaddr + buf->offset;
293}
294
295static unsigned int vb2_dma_sg_num_users(void *buf_priv)
296{
297 struct vb2_dma_sg_buf *buf = buf_priv;
298
299 return atomic_read(&buf->refcount);
300}
301
302static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
303{
304 struct vb2_dma_sg_buf *buf = buf_priv;
305 unsigned long uaddr = vma->vm_start;
306 unsigned long usize = vma->vm_end - vma->vm_start;
307 int i = 0;
308
309 if (!buf) {
310 printk(KERN_ERR "No memory to map\n");
311 return -EINVAL;
312 }
313
314 do {
315 int ret;
316
317 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
318 if (ret) {
319 printk(KERN_ERR "Remapping memory, error: %d\n", ret);
320 return ret;
321 }
322
323 uaddr += PAGE_SIZE;
324 usize -= PAGE_SIZE;
325 } while (usize > 0);
326
327
328 /*
329 * Use common vm_area operations to track buffer refcount.
330 */
331 vma->vm_private_data = &buf->handler;
332 vma->vm_ops = &vb2_common_vm_ops;
333
334 vma->vm_ops->open(vma);
335
336 return 0;
337}
338
339static void *vb2_dma_sg_cookie(void *buf_priv)
340{
341 struct vb2_dma_sg_buf *buf = buf_priv;
342
Ricardo Ribalda22301242013-08-02 10:20:00 -0300343 return &buf->sg_table;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300344}
345
346const struct vb2_mem_ops vb2_dma_sg_memops = {
347 .alloc = vb2_dma_sg_alloc,
348 .put = vb2_dma_sg_put,
349 .get_userptr = vb2_dma_sg_get_userptr,
350 .put_userptr = vb2_dma_sg_put_userptr,
351 .vaddr = vb2_dma_sg_vaddr,
352 .mmap = vb2_dma_sg_mmap,
353 .num_users = vb2_dma_sg_num_users,
354 .cookie = vb2_dma_sg_cookie,
355};
356EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
357
358MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
359MODULE_AUTHOR("Andrzej Pietrasiewicz");
360MODULE_LICENSE("GPL");