blob: 088710b2a5ea9ab3569c9e2b91b22053a96b4c2c [file] [log] [blame]
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001/*
2 * ispqueue.c
3 *
4 * TI OMAP3 ISP - Video buffers queue handling
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26#include <asm/cacheflush.h>
27#include <linux/dma-mapping.h>
28#include <linux/mm.h>
Laurent Pinchart90004272014-01-02 22:12:42 -030029#include <linux/omap-iommu.h>
Laurent Pinchartad614ac2011-02-12 18:05:06 -030030#include <linux/pagemap.h>
31#include <linux/poll.h>
32#include <linux/scatterlist.h>
33#include <linux/sched.h>
34#include <linux/slab.h>
35#include <linux/vmalloc.h>
36
Laurent Pinchart90004272014-01-02 22:12:42 -030037#include "isp.h"
Laurent Pinchartad614ac2011-02-12 18:05:06 -030038#include "ispqueue.h"
Laurent Pinchart90004272014-01-02 22:12:42 -030039#include "ispvideo.h"
40
41/* -----------------------------------------------------------------------------
Laurent Pinchartad614ac2011-02-12 18:05:06 -030042 * Video buffers management
43 */
44
45/*
46 * isp_video_buffer_cache_sync - Keep the buffers coherent between CPU and ISP
47 *
48 * The typical operation required here is Cache Invalidation across
49 * the (user space) buffer address range. And this _must_ be done
50 * at QBUF stage (and *only* at QBUF).
51 *
52 * We try to use optimal cache invalidation function:
53 * - dmac_map_area:
54 * - used when the number of pages are _low_.
55 * - it becomes quite slow as the number of pages increase.
56 * - for 648x492 viewfinder (150 pages) it takes 1.3 ms.
57 * - for 5 Mpix buffer (2491 pages) it takes between 25-50 ms.
58 *
59 * - flush_cache_all:
60 * - used when the number of pages are _high_.
61 * - time taken in the range of 500-900 us.
62 * - has a higher penalty but, as whole dcache + icache is invalidated
63 */
64/*
65 * FIXME: dmac_inv_range crashes randomly on the user space buffer
66 * address. Fall back to flush_cache_all for now.
67 */
68#define ISP_CACHE_FLUSH_PAGES_MAX 0
69
70static void isp_video_buffer_cache_sync(struct isp_video_buffer *buf)
71{
72 if (buf->skip_cache)
73 return;
74
75 if (buf->vbuf.m.userptr == 0 || buf->npages == 0 ||
76 buf->npages > ISP_CACHE_FLUSH_PAGES_MAX)
77 flush_cache_all();
78 else {
79 dmac_map_area((void *)buf->vbuf.m.userptr, buf->vbuf.length,
80 DMA_FROM_DEVICE);
81 outer_inv_range(buf->vbuf.m.userptr,
82 buf->vbuf.m.userptr + buf->vbuf.length);
83 }
84}
85
86/*
87 * isp_video_buffer_lock_vma - Prevent VMAs from being unmapped
88 *
89 * Lock the VMAs underlying the given buffer into memory. This avoids the
90 * userspace buffer mapping from being swapped out, making VIPT cache handling
91 * easier.
92 *
93 * Note that the pages will not be freed as the buffers have been locked to
94 * memory using by a call to get_user_pages(), but the userspace mapping could
95 * still disappear if the VMAs are not locked. This is caused by the memory
96 * management code trying to be as lock-less as possible, which results in the
97 * userspace mapping manager not finding out that the pages are locked under
98 * some conditions.
99 */
100static int isp_video_buffer_lock_vma(struct isp_video_buffer *buf, int lock)
101{
102 struct vm_area_struct *vma;
103 unsigned long start;
104 unsigned long end;
105 int ret = 0;
106
107 if (buf->vbuf.memory == V4L2_MEMORY_MMAP)
108 return 0;
109
110 /* We can be called from workqueue context if the current task dies to
111 * unlock the VMAs. In that case there's no current memory management
112 * context so unlocking can't be performed, but the VMAs have been or
113 * are getting destroyed anyway so it doesn't really matter.
114 */
115 if (!current || !current->mm)
116 return lock ? -EINVAL : 0;
117
118 start = buf->vbuf.m.userptr;
119 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
120
121 down_write(&current->mm->mmap_sem);
122 spin_lock(&current->mm->page_table_lock);
123
124 do {
125 vma = find_vma(current->mm, start);
126 if (vma == NULL) {
127 ret = -EFAULT;
128 goto out;
129 }
130
131 if (lock)
132 vma->vm_flags |= VM_LOCKED;
133 else
134 vma->vm_flags &= ~VM_LOCKED;
135
136 start = vma->vm_end + 1;
137 } while (vma->vm_end < end);
138
139 if (lock)
140 buf->vm_flags |= VM_LOCKED;
141 else
142 buf->vm_flags &= ~VM_LOCKED;
143
144out:
145 spin_unlock(&current->mm->page_table_lock);
146 up_write(&current->mm->mmap_sem);
147 return ret;
148}
149
150/*
Laurent Pinchart82716012014-01-06 15:30:03 -0300151 * isp_video_buffer_prepare_kernel - Build scatter list for a vmalloc'ed buffer
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300152 *
153 * Iterate over the vmalloc'ed area and create a scatter list entry for every
154 * page.
155 */
Laurent Pinchart82716012014-01-06 15:30:03 -0300156static int isp_video_buffer_prepare_kernel(struct isp_video_buffer *buf)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300157{
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300158 struct scatterlist *sg;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300159 unsigned int npages;
160 unsigned int i;
161 void *addr;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300162 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300163
164 addr = buf->vaddr;
165 npages = PAGE_ALIGN(buf->vbuf.length) >> PAGE_SHIFT;
166
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300167 ret = sg_alloc_table(&buf->sgt, npages, GFP_KERNEL);
168 if (ret < 0)
169 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300170
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300171 for (sg = buf->sgt.sgl, i = 0; i < npages; ++i, addr += PAGE_SIZE) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300172 struct page *page = vmalloc_to_page(addr);
173
174 if (page == NULL || PageHighMem(page)) {
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300175 sg_free_table(&buf->sgt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300176 return -EINVAL;
177 }
178
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300179 sg_set_page(sg, page, PAGE_SIZE, 0);
180 sg = sg_next(sg);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300181 }
182
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300183 return 0;
184}
185
186/*
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300187 * isp_video_buffer_cleanup - Release pages for a userspace VMA.
188 *
189 * Release pages locked by a call isp_video_buffer_prepare_user and free the
190 * pages table.
191 */
192static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
193{
Laurent Pinchart90004272014-01-02 22:12:42 -0300194 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
195 struct isp_video *video = vfh->video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300196 enum dma_data_direction direction;
197 unsigned int i;
198
Laurent Pinchart90004272014-01-02 22:12:42 -0300199 if (buf->dma) {
Laurent Pinchartd13f19f2014-01-06 15:30:03 -0300200 omap_iommu_vunmap(video->isp->domain, video->isp->dev,
201 buf->dma);
Laurent Pinchart90004272014-01-02 22:12:42 -0300202 buf->dma = 0;
203 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300204
205 if (!(buf->vm_flags & VM_PFNMAP)) {
206 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
207 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300208 dma_unmap_sg(buf->queue->dev, buf->sgt.sgl, buf->sgt.orig_nents,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300209 direction);
210 }
211
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300212 sg_free_table(&buf->sgt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300213
214 if (buf->pages != NULL) {
215 isp_video_buffer_lock_vma(buf, 0);
216
217 for (i = 0; i < buf->npages; ++i)
218 page_cache_release(buf->pages[i]);
219
220 vfree(buf->pages);
221 buf->pages = NULL;
222 }
223
224 buf->npages = 0;
225 buf->skip_cache = false;
226}
227
228/*
Laurent Pinchart82716012014-01-06 15:30:03 -0300229 * isp_video_buffer_prepare_user - Prepare a userspace buffer.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300230 *
Laurent Pinchart82716012014-01-06 15:30:03 -0300231 * This function creates a scatter list with a 1:1 mapping for a userspace VMA.
232 * The number of pages is first computed based on the buffer size, and pages are
233 * then retrieved by a call to get_user_pages.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300234 *
235 * Pages are pinned to memory by get_user_pages, making them available for DMA
236 * transfers. However, due to memory management optimization, it seems the
237 * get_user_pages doesn't guarantee that the pinned pages will not be written
238 * to swap and removed from the userspace mapping(s). When this happens, a page
239 * fault can be generated when accessing those unmapped pages.
240 *
241 * If the fault is triggered by a page table walk caused by VIPT cache
242 * management operations, the page fault handler might oops if the MM semaphore
243 * is held, as it can't handle kernel page faults in that case. To fix that, a
244 * fixup entry needs to be added to the cache management code, or the userspace
245 * VMA must be locked to avoid removing pages from the userspace mapping in the
246 * first place.
247 *
248 * If the number of pages retrieved is smaller than the number required by the
249 * buffer size, the function returns -EFAULT.
250 */
251static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf)
252{
Laurent Pinchart82716012014-01-06 15:30:03 -0300253 struct scatterlist *sg;
254 unsigned int offset;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300255 unsigned long data;
256 unsigned int first;
257 unsigned int last;
Laurent Pinchart82716012014-01-06 15:30:03 -0300258 unsigned int i;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300259 int ret;
260
261 data = buf->vbuf.m.userptr;
262 first = (data & PAGE_MASK) >> PAGE_SHIFT;
263 last = ((data + buf->vbuf.length - 1) & PAGE_MASK) >> PAGE_SHIFT;
Laurent Pinchart82716012014-01-06 15:30:03 -0300264 offset = data & ~PAGE_MASK;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300265
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300266 buf->npages = last - first + 1;
267 buf->pages = vmalloc(buf->npages * sizeof(buf->pages[0]));
268 if (buf->pages == NULL)
269 return -ENOMEM;
270
271 down_read(&current->mm->mmap_sem);
272 ret = get_user_pages(current, current->mm, data & PAGE_MASK,
273 buf->npages,
274 buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
275 buf->pages, NULL);
276 up_read(&current->mm->mmap_sem);
277
278 if (ret != buf->npages) {
Laurent Pinchart2578dfb2011-04-07 13:30:14 -0300279 buf->npages = ret < 0 ? 0 : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300280 return -EFAULT;
281 }
282
283 ret = isp_video_buffer_lock_vma(buf, 1);
284 if (ret < 0)
Laurent Pinchart82716012014-01-06 15:30:03 -0300285 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300286
Laurent Pinchart82716012014-01-06 15:30:03 -0300287 ret = sg_alloc_table(&buf->sgt, buf->npages, GFP_KERNEL);
288 if (ret < 0)
289 return ret;
290
291 for (sg = buf->sgt.sgl, i = 0; i < buf->npages; ++i) {
292 if (PageHighMem(buf->pages[i])) {
293 sg_free_table(&buf->sgt);
294 return -EINVAL;
295 }
296
297 sg_set_page(sg, buf->pages[i], PAGE_SIZE - offset, offset);
298 sg = sg_next(sg);
299 offset = 0;
300 }
301
302 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300303}
304
305/*
Laurent Pinchart82716012014-01-06 15:30:03 -0300306 * isp_video_buffer_prepare_pfnmap - Prepare a VM_PFNMAP userspace buffer
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300307 *
308 * Userspace VM_PFNMAP buffers are supported only if they are contiguous in
Laurent Pinchart82716012014-01-06 15:30:03 -0300309 * memory and if they span a single VMA. Start by validating the user pointer to
310 * make sure it fulfils that condition, and then build a scatter list of
311 * physically contiguous pages starting at the buffer memory physical address.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300312 *
Laurent Pinchart82716012014-01-06 15:30:03 -0300313 * Return 0 on success, -EFAULT if the buffer isn't valid or -ENOMEM if memory
314 * can't be allocated.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300315 */
316static int isp_video_buffer_prepare_pfnmap(struct isp_video_buffer *buf)
317{
318 struct vm_area_struct *vma;
Laurent Pinchart82716012014-01-06 15:30:03 -0300319 struct scatterlist *sg;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300320 unsigned long prev_pfn;
321 unsigned long this_pfn;
322 unsigned long start;
Laurent Pinchart82716012014-01-06 15:30:03 -0300323 unsigned int offset;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300324 unsigned long end;
Laurent Pinchart82716012014-01-06 15:30:03 -0300325 unsigned long pfn;
326 unsigned int i;
327 int ret = 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300328
329 start = buf->vbuf.m.userptr;
330 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
Laurent Pinchart82716012014-01-06 15:30:03 -0300331 offset = start & ~PAGE_MASK;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300332
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300333 buf->npages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
334 buf->pages = NULL;
335
336 down_read(&current->mm->mmap_sem);
337 vma = find_vma(current->mm, start);
Laurent Pinchart82716012014-01-06 15:30:03 -0300338 if (vma == NULL || vma->vm_end < end) {
339 ret = -EFAULT;
340 goto unlock;
341 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300342
343 for (prev_pfn = 0; start <= end; start += PAGE_SIZE) {
344 ret = follow_pfn(vma, start, &this_pfn);
Laurent Pinchart82716012014-01-06 15:30:03 -0300345 if (ret < 0)
346 goto unlock;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300347
348 if (prev_pfn == 0)
Laurent Pinchart82716012014-01-06 15:30:03 -0300349 pfn = this_pfn;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300350 else if (this_pfn != prev_pfn + 1) {
351 ret = -EFAULT;
Laurent Pinchart82716012014-01-06 15:30:03 -0300352 goto unlock;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300353 }
354
355 prev_pfn = this_pfn;
356 }
357
Laurent Pinchart82716012014-01-06 15:30:03 -0300358unlock:
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300359 up_read(&current->mm->mmap_sem);
Laurent Pinchart82716012014-01-06 15:30:03 -0300360 if (ret < 0)
361 return ret;
362
363 ret = sg_alloc_table(&buf->sgt, buf->npages, GFP_KERNEL);
364 if (ret < 0)
365 return ret;
366
367 for (sg = buf->sgt.sgl, i = 0; i < buf->npages; ++i, ++pfn) {
368 sg_set_page(sg, pfn_to_page(pfn), PAGE_SIZE - offset, offset);
369 /* PFNMAP buffers will not get DMA-mapped, set the DMA address
370 * manually.
371 */
372 sg_dma_address(sg) = (pfn << PAGE_SHIFT) + offset;
373 sg = sg_next(sg);
374 offset = 0;
375 }
376
377 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300378}
379
380/*
381 * isp_video_buffer_prepare_vm_flags - Get VMA flags for a userspace address
382 *
383 * This function locates the VMAs for the buffer's userspace address and checks
Michael Jones2d4e9d12011-02-28 08:29:03 -0300384 * that their flags match. The only flag that we need to care for at the moment
385 * is VM_PFNMAP.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300386 *
387 * The buffer vm_flags field is set to the first VMA flags.
388 *
389 * Return -EFAULT if no VMA can be found for part of the buffer, or if the VMAs
390 * have incompatible flags.
391 */
392static int isp_video_buffer_prepare_vm_flags(struct isp_video_buffer *buf)
393{
394 struct vm_area_struct *vma;
Laurent Pincharte19bc862012-12-17 04:52:48 -0300395 pgprot_t uninitialized_var(vm_page_prot);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300396 unsigned long start;
397 unsigned long end;
398 int ret = -EFAULT;
399
400 start = buf->vbuf.m.userptr;
401 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
402
403 down_read(&current->mm->mmap_sem);
404
405 do {
406 vma = find_vma(current->mm, start);
407 if (vma == NULL)
408 goto done;
409
410 if (start == buf->vbuf.m.userptr) {
411 buf->vm_flags = vma->vm_flags;
412 vm_page_prot = vma->vm_page_prot;
413 }
414
415 if ((buf->vm_flags ^ vma->vm_flags) & VM_PFNMAP)
416 goto done;
417
418 if (vm_page_prot != vma->vm_page_prot)
419 goto done;
420
421 start = vma->vm_end + 1;
422 } while (vma->vm_end < end);
423
424 /* Skip cache management to enhance performances for non-cached or
425 * write-combining buffers.
426 */
427 if (vm_page_prot == pgprot_noncached(vm_page_prot) ||
428 vm_page_prot == pgprot_writecombine(vm_page_prot))
429 buf->skip_cache = true;
430
431 ret = 0;
432
433done:
434 up_read(&current->mm->mmap_sem);
435 return ret;
436}
437
438/*
439 * isp_video_buffer_prepare - Make a buffer ready for operation
440 *
441 * Preparing a buffer involves:
442 *
443 * - validating VMAs (userspace buffers only)
444 * - locking pages and VMAs into memory (userspace buffers only)
445 * - building page and scatter-gather lists
446 * - mapping buffers for DMA operation
447 * - performing driver-specific preparation
448 *
449 * The function must be called in userspace context with a valid mm context
450 * (this excludes cleanup paths such as sys_close when the userspace process
451 * segfaults).
452 */
453static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
454{
Laurent Pinchart90004272014-01-02 22:12:42 -0300455 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
456 struct isp_video *video = vfh->video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300457 enum dma_data_direction direction;
Laurent Pinchart90004272014-01-02 22:12:42 -0300458 unsigned long addr;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300459 int ret;
460
461 switch (buf->vbuf.memory) {
462 case V4L2_MEMORY_MMAP:
Laurent Pinchart82716012014-01-06 15:30:03 -0300463 ret = isp_video_buffer_prepare_kernel(buf);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300464 break;
465
466 case V4L2_MEMORY_USERPTR:
467 ret = isp_video_buffer_prepare_vm_flags(buf);
468 if (ret < 0)
469 return ret;
470
Laurent Pinchart82716012014-01-06 15:30:03 -0300471 if (buf->vm_flags & VM_PFNMAP)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300472 ret = isp_video_buffer_prepare_pfnmap(buf);
Laurent Pinchart82716012014-01-06 15:30:03 -0300473 else
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300474 ret = isp_video_buffer_prepare_user(buf);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300475 break;
476
477 default:
478 return -EINVAL;
479 }
480
481 if (ret < 0)
482 goto done;
483
484 if (!(buf->vm_flags & VM_PFNMAP)) {
485 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
486 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300487 ret = dma_map_sg(buf->queue->dev, buf->sgt.sgl,
488 buf->sgt.orig_nents, direction);
489 if (ret != buf->sgt.orig_nents) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300490 ret = -EFAULT;
491 goto done;
492 }
493 }
494
Laurent Pinchartd13f19f2014-01-06 15:30:03 -0300495 addr = omap_iommu_vmap(video->isp->domain, video->isp->dev, 0,
496 &buf->sgt, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8);
Laurent Pinchart90004272014-01-02 22:12:42 -0300497 if (IS_ERR_VALUE(addr)) {
498 ret = -EIO;
499 goto done;
500 }
501
502 buf->dma = addr;
503
504 if (!IS_ALIGNED(addr, 32)) {
505 dev_dbg(video->isp->dev,
506 "Buffer address must be aligned to 32 bytes boundary.\n");
507 ret = -EINVAL;
508 goto done;
509 }
510
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300511 if (buf->queue->ops->buffer_prepare)
512 ret = buf->queue->ops->buffer_prepare(buf);
513
514done:
515 if (ret < 0) {
516 isp_video_buffer_cleanup(buf);
517 return ret;
518 }
519
520 return ret;
521}
522
523/*
524 * isp_video_queue_query - Query the status of a given buffer
525 *
526 * Locking: must be called with the queue lock held.
527 */
528static void isp_video_buffer_query(struct isp_video_buffer *buf,
529 struct v4l2_buffer *vbuf)
530{
531 memcpy(vbuf, &buf->vbuf, sizeof(*vbuf));
532
533 if (buf->vma_use_count)
534 vbuf->flags |= V4L2_BUF_FLAG_MAPPED;
535
536 switch (buf->state) {
537 case ISP_BUF_STATE_ERROR:
538 vbuf->flags |= V4L2_BUF_FLAG_ERROR;
Laurent Pinchart792e8ec2013-12-09 22:46:17 -0300539 /* Fallthrough */
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300540 case ISP_BUF_STATE_DONE:
541 vbuf->flags |= V4L2_BUF_FLAG_DONE;
Laurent Pinchart792e8ec2013-12-09 22:46:17 -0300542 break;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300543 case ISP_BUF_STATE_QUEUED:
544 case ISP_BUF_STATE_ACTIVE:
545 vbuf->flags |= V4L2_BUF_FLAG_QUEUED;
546 break;
547 case ISP_BUF_STATE_IDLE:
548 default:
549 break;
550 }
551}
552
553/*
554 * isp_video_buffer_wait - Wait for a buffer to be ready
555 *
556 * In non-blocking mode, return immediately with 0 if the buffer is ready or
557 * -EAGAIN if the buffer is in the QUEUED or ACTIVE state.
558 *
559 * In blocking mode, wait (interruptibly but with no timeout) on the buffer wait
560 * queue using the same condition.
561 */
562static int isp_video_buffer_wait(struct isp_video_buffer *buf, int nonblocking)
563{
564 if (nonblocking) {
565 return (buf->state != ISP_BUF_STATE_QUEUED &&
566 buf->state != ISP_BUF_STATE_ACTIVE)
567 ? 0 : -EAGAIN;
568 }
569
570 return wait_event_interruptible(buf->wait,
571 buf->state != ISP_BUF_STATE_QUEUED &&
572 buf->state != ISP_BUF_STATE_ACTIVE);
573}
574
575/* -----------------------------------------------------------------------------
576 * Queue management
577 */
578
579/*
580 * isp_video_queue_free - Free video buffers memory
581 *
582 * Buffers can only be freed if the queue isn't streaming and if no buffer is
Lad, Prabhakar25aeb412014-02-21 09:07:21 -0300583 * mapped to userspace. Return -EBUSY if those conditions aren't satisfied.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300584 *
585 * This function must be called with the queue lock held.
586 */
587static int isp_video_queue_free(struct isp_video_queue *queue)
588{
589 unsigned int i;
590
591 if (queue->streaming)
592 return -EBUSY;
593
594 for (i = 0; i < queue->count; ++i) {
595 if (queue->buffers[i]->vma_use_count != 0)
596 return -EBUSY;
597 }
598
599 for (i = 0; i < queue->count; ++i) {
600 struct isp_video_buffer *buf = queue->buffers[i];
601
602 isp_video_buffer_cleanup(buf);
603
604 vfree(buf->vaddr);
605 buf->vaddr = NULL;
606
607 kfree(buf);
608 queue->buffers[i] = NULL;
609 }
610
611 INIT_LIST_HEAD(&queue->queue);
612 queue->count = 0;
613 return 0;
614}
615
616/*
617 * isp_video_queue_alloc - Allocate video buffers memory
618 *
619 * This function must be called with the queue lock held.
620 */
621static int isp_video_queue_alloc(struct isp_video_queue *queue,
622 unsigned int nbuffers,
623 unsigned int size, enum v4l2_memory memory)
624{
625 struct isp_video_buffer *buf;
626 unsigned int i;
627 void *mem;
628 int ret;
629
630 /* Start by freeing the buffers. */
631 ret = isp_video_queue_free(queue);
632 if (ret < 0)
633 return ret;
634
Michael Jones9d380ad2012-07-26 10:48:25 -0300635 /* Bail out if no buffers should be allocated. */
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300636 if (nbuffers == 0)
637 return 0;
638
639 /* Initialize the allocated buffers. */
640 for (i = 0; i < nbuffers; ++i) {
641 buf = kzalloc(queue->bufsize, GFP_KERNEL);
642 if (buf == NULL)
643 break;
644
645 if (memory == V4L2_MEMORY_MMAP) {
646 /* Allocate video buffers memory for mmap mode. Align
647 * the size to the page size.
648 */
649 mem = vmalloc_32_user(PAGE_ALIGN(size));
650 if (mem == NULL) {
651 kfree(buf);
652 break;
653 }
654
655 buf->vbuf.m.offset = i * PAGE_ALIGN(size);
656 buf->vaddr = mem;
657 }
658
659 buf->vbuf.index = i;
660 buf->vbuf.length = size;
661 buf->vbuf.type = queue->type;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300662 buf->vbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300663 buf->vbuf.field = V4L2_FIELD_NONE;
664 buf->vbuf.memory = memory;
665
666 buf->queue = queue;
667 init_waitqueue_head(&buf->wait);
668
669 queue->buffers[i] = buf;
670 }
671
672 if (i == 0)
673 return -ENOMEM;
674
675 queue->count = i;
676 return nbuffers;
677}
678
679/**
680 * omap3isp_video_queue_cleanup - Clean up the video buffers queue
681 * @queue: Video buffers queue
682 *
683 * Free all allocated resources and clean up the video buffers queue. The queue
684 * must not be busy (no ongoing video stream) and buffers must have been
685 * unmapped.
686 *
687 * Return 0 on success or -EBUSY if the queue is busy or buffers haven't been
688 * unmapped.
689 */
690int omap3isp_video_queue_cleanup(struct isp_video_queue *queue)
691{
692 return isp_video_queue_free(queue);
693}
694
695/**
696 * omap3isp_video_queue_init - Initialize the video buffers queue
697 * @queue: Video buffers queue
698 * @type: V4L2 buffer type (capture or output)
699 * @ops: Driver-specific queue operations
700 * @dev: Device used for DMA operations
701 * @bufsize: Size of the driver-specific buffer structure
702 *
703 * Initialize the video buffers queue with the supplied parameters.
704 *
705 * The queue type must be one of V4L2_BUF_TYPE_VIDEO_CAPTURE or
706 * V4L2_BUF_TYPE_VIDEO_OUTPUT. Other buffer types are not supported yet.
707 *
708 * Buffer objects will be allocated using the given buffer size to allow room
709 * for driver-specific fields. Driver-specific buffer structures must start
710 * with a struct isp_video_buffer field. Drivers with no driver-specific buffer
711 * structure must pass the size of the isp_video_buffer structure in the bufsize
712 * parameter.
713 *
714 * Return 0 on success.
715 */
716int omap3isp_video_queue_init(struct isp_video_queue *queue,
717 enum v4l2_buf_type type,
718 const struct isp_video_queue_operations *ops,
719 struct device *dev, unsigned int bufsize)
720{
721 INIT_LIST_HEAD(&queue->queue);
722 mutex_init(&queue->lock);
723 spin_lock_init(&queue->irqlock);
724
725 queue->type = type;
726 queue->ops = ops;
727 queue->dev = dev;
728 queue->bufsize = bufsize;
729
730 return 0;
731}
732
733/* -----------------------------------------------------------------------------
734 * V4L2 operations
735 */
736
737/**
738 * omap3isp_video_queue_reqbufs - Allocate video buffers memory
739 *
740 * This function is intended to be used as a VIDIOC_REQBUFS ioctl handler. It
741 * allocated video buffer objects and, for MMAP buffers, buffer memory.
742 *
743 * If the number of buffers is 0, all buffers are freed and the function returns
744 * without performing any allocation.
745 *
746 * If the number of buffers is not 0, currently allocated buffers (if any) are
747 * freed and the requested number of buffers are allocated. Depending on
748 * driver-specific requirements and on memory availability, a number of buffer
749 * smaller or bigger than requested can be allocated. This isn't considered as
750 * an error.
751 *
752 * Return 0 on success or one of the following error codes:
753 *
754 * -EINVAL if the buffer type or index are invalid
755 * -EBUSY if the queue is busy (streaming or buffers mapped)
756 * -ENOMEM if the buffers can't be allocated due to an out-of-memory condition
757 */
758int omap3isp_video_queue_reqbufs(struct isp_video_queue *queue,
759 struct v4l2_requestbuffers *rb)
760{
761 unsigned int nbuffers = rb->count;
762 unsigned int size;
763 int ret;
764
765 if (rb->type != queue->type)
766 return -EINVAL;
767
768 queue->ops->queue_prepare(queue, &nbuffers, &size);
769 if (size == 0)
770 return -EINVAL;
771
772 nbuffers = min_t(unsigned int, nbuffers, ISP_VIDEO_MAX_BUFFERS);
773
774 mutex_lock(&queue->lock);
775
776 ret = isp_video_queue_alloc(queue, nbuffers, size, rb->memory);
777 if (ret < 0)
778 goto done;
779
780 rb->count = ret;
781 ret = 0;
782
783done:
784 mutex_unlock(&queue->lock);
785 return ret;
786}
787
788/**
789 * omap3isp_video_queue_querybuf - Query the status of a buffer in a queue
790 *
791 * This function is intended to be used as a VIDIOC_QUERYBUF ioctl handler. It
792 * returns the status of a given video buffer.
793 *
794 * Return 0 on success or -EINVAL if the buffer type or index are invalid.
795 */
796int omap3isp_video_queue_querybuf(struct isp_video_queue *queue,
797 struct v4l2_buffer *vbuf)
798{
799 struct isp_video_buffer *buf;
800 int ret = 0;
801
802 if (vbuf->type != queue->type)
803 return -EINVAL;
804
805 mutex_lock(&queue->lock);
806
807 if (vbuf->index >= queue->count) {
808 ret = -EINVAL;
809 goto done;
810 }
811
812 buf = queue->buffers[vbuf->index];
813 isp_video_buffer_query(buf, vbuf);
814
815done:
816 mutex_unlock(&queue->lock);
817 return ret;
818}
819
820/**
821 * omap3isp_video_queue_qbuf - Queue a buffer
822 *
823 * This function is intended to be used as a VIDIOC_QBUF ioctl handler.
824 *
825 * The v4l2_buffer structure passed from userspace is first sanity tested. If
826 * sane, the buffer is then processed and added to the main queue and, if the
827 * queue is streaming, to the IRQ queue.
828 *
829 * Before being enqueued, USERPTR buffers are checked for address changes. If
830 * the buffer has a different userspace address, the old memory area is unlocked
831 * and the new memory area is locked.
832 */
833int omap3isp_video_queue_qbuf(struct isp_video_queue *queue,
834 struct v4l2_buffer *vbuf)
835{
836 struct isp_video_buffer *buf;
837 unsigned long flags;
838 int ret = -EINVAL;
839
840 if (vbuf->type != queue->type)
841 goto done;
842
843 mutex_lock(&queue->lock);
844
845 if (vbuf->index >= queue->count)
846 goto done;
847
848 buf = queue->buffers[vbuf->index];
849
850 if (vbuf->memory != buf->vbuf.memory)
851 goto done;
852
853 if (buf->state != ISP_BUF_STATE_IDLE)
854 goto done;
855
856 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
Michael Jones61e65612011-08-09 08:42:20 +0200857 vbuf->length < buf->vbuf.length)
858 goto done;
859
860 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300861 vbuf->m.userptr != buf->vbuf.m.userptr) {
862 isp_video_buffer_cleanup(buf);
863 buf->vbuf.m.userptr = vbuf->m.userptr;
864 buf->prepared = 0;
865 }
866
867 if (!buf->prepared) {
868 ret = isp_video_buffer_prepare(buf);
869 if (ret < 0)
870 goto done;
871 buf->prepared = 1;
872 }
873
874 isp_video_buffer_cache_sync(buf);
875
876 buf->state = ISP_BUF_STATE_QUEUED;
877 list_add_tail(&buf->stream, &queue->queue);
878
879 if (queue->streaming) {
880 spin_lock_irqsave(&queue->irqlock, flags);
881 queue->ops->buffer_queue(buf);
882 spin_unlock_irqrestore(&queue->irqlock, flags);
883 }
884
885 ret = 0;
886
887done:
888 mutex_unlock(&queue->lock);
889 return ret;
890}
891
892/**
893 * omap3isp_video_queue_dqbuf - Dequeue a buffer
894 *
895 * This function is intended to be used as a VIDIOC_DQBUF ioctl handler.
896 *
Michael Jonesb1e71f32012-06-27 12:06:57 -0300897 * Wait until a buffer is ready to be dequeued, remove it from the queue and
898 * copy its information to the v4l2_buffer structure.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300899 *
Michael Jonesb1e71f32012-06-27 12:06:57 -0300900 * If the nonblocking argument is not zero and no buffer is ready, return
901 * -EAGAIN immediately instead of waiting.
902 *
903 * If no buffer has been enqueued, or if the requested buffer type doesn't match
904 * the queue type, return -EINVAL.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300905 */
906int omap3isp_video_queue_dqbuf(struct isp_video_queue *queue,
907 struct v4l2_buffer *vbuf, int nonblocking)
908{
909 struct isp_video_buffer *buf;
910 int ret;
911
912 if (vbuf->type != queue->type)
913 return -EINVAL;
914
915 mutex_lock(&queue->lock);
916
917 if (list_empty(&queue->queue)) {
918 ret = -EINVAL;
919 goto done;
920 }
921
922 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
923 ret = isp_video_buffer_wait(buf, nonblocking);
924 if (ret < 0)
925 goto done;
926
927 list_del(&buf->stream);
928
929 isp_video_buffer_query(buf, vbuf);
930 buf->state = ISP_BUF_STATE_IDLE;
931 vbuf->flags &= ~V4L2_BUF_FLAG_QUEUED;
932
933done:
934 mutex_unlock(&queue->lock);
935 return ret;
936}
937
938/**
939 * omap3isp_video_queue_streamon - Start streaming
940 *
941 * This function is intended to be used as a VIDIOC_STREAMON ioctl handler. It
942 * starts streaming on the queue and calls the buffer_queue operation for all
943 * queued buffers.
944 *
945 * Return 0 on success.
946 */
947int omap3isp_video_queue_streamon(struct isp_video_queue *queue)
948{
949 struct isp_video_buffer *buf;
950 unsigned long flags;
951
952 mutex_lock(&queue->lock);
953
954 if (queue->streaming)
955 goto done;
956
957 queue->streaming = 1;
958
959 spin_lock_irqsave(&queue->irqlock, flags);
960 list_for_each_entry(buf, &queue->queue, stream)
961 queue->ops->buffer_queue(buf);
962 spin_unlock_irqrestore(&queue->irqlock, flags);
963
964done:
965 mutex_unlock(&queue->lock);
966 return 0;
967}
968
969/**
970 * omap3isp_video_queue_streamoff - Stop streaming
971 *
972 * This function is intended to be used as a VIDIOC_STREAMOFF ioctl handler. It
973 * stops streaming on the queue and wakes up all the buffers.
974 *
975 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
976 * delayed works before calling this function to make sure no buffer will be
977 * touched by the driver and/or hardware.
978 */
979void omap3isp_video_queue_streamoff(struct isp_video_queue *queue)
980{
981 struct isp_video_buffer *buf;
982 unsigned long flags;
983 unsigned int i;
984
985 mutex_lock(&queue->lock);
986
987 if (!queue->streaming)
988 goto done;
989
990 queue->streaming = 0;
991
992 spin_lock_irqsave(&queue->irqlock, flags);
993 for (i = 0; i < queue->count; ++i) {
994 buf = queue->buffers[i];
995
996 if (buf->state == ISP_BUF_STATE_ACTIVE)
997 wake_up(&buf->wait);
998
999 buf->state = ISP_BUF_STATE_IDLE;
1000 }
1001 spin_unlock_irqrestore(&queue->irqlock, flags);
1002
1003 INIT_LIST_HEAD(&queue->queue);
1004
1005done:
1006 mutex_unlock(&queue->lock);
1007}
1008
1009/**
1010 * omap3isp_video_queue_discard_done - Discard all buffers marked as DONE
1011 *
1012 * This function is intended to be used with suspend/resume operations. It
1013 * discards all 'done' buffers as they would be too old to be requested after
1014 * resume.
1015 *
1016 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1017 * delayed works before calling this function to make sure no buffer will be
1018 * touched by the driver and/or hardware.
1019 */
1020void omap3isp_video_queue_discard_done(struct isp_video_queue *queue)
1021{
1022 struct isp_video_buffer *buf;
1023 unsigned int i;
1024
1025 mutex_lock(&queue->lock);
1026
1027 if (!queue->streaming)
1028 goto done;
1029
1030 for (i = 0; i < queue->count; ++i) {
1031 buf = queue->buffers[i];
1032
1033 if (buf->state == ISP_BUF_STATE_DONE)
1034 buf->state = ISP_BUF_STATE_ERROR;
1035 }
1036
1037done:
1038 mutex_unlock(&queue->lock);
1039}
1040
1041static void isp_video_queue_vm_open(struct vm_area_struct *vma)
1042{
1043 struct isp_video_buffer *buf = vma->vm_private_data;
1044
1045 buf->vma_use_count++;
1046}
1047
1048static void isp_video_queue_vm_close(struct vm_area_struct *vma)
1049{
1050 struct isp_video_buffer *buf = vma->vm_private_data;
1051
1052 buf->vma_use_count--;
1053}
1054
1055static const struct vm_operations_struct isp_video_queue_vm_ops = {
1056 .open = isp_video_queue_vm_open,
1057 .close = isp_video_queue_vm_close,
1058};
1059
1060/**
1061 * omap3isp_video_queue_mmap - Map buffers to userspace
1062 *
1063 * This function is intended to be used as an mmap() file operation handler. It
1064 * maps a buffer to userspace based on the VMA offset.
1065 *
1066 * Only buffers of memory type MMAP are supported.
1067 */
1068int omap3isp_video_queue_mmap(struct isp_video_queue *queue,
1069 struct vm_area_struct *vma)
1070{
1071 struct isp_video_buffer *uninitialized_var(buf);
1072 unsigned long size;
1073 unsigned int i;
1074 int ret = 0;
1075
1076 mutex_lock(&queue->lock);
1077
1078 for (i = 0; i < queue->count; ++i) {
1079 buf = queue->buffers[i];
1080 if ((buf->vbuf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1081 break;
1082 }
1083
1084 if (i == queue->count) {
1085 ret = -EINVAL;
1086 goto done;
1087 }
1088
1089 size = vma->vm_end - vma->vm_start;
1090
1091 if (buf->vbuf.memory != V4L2_MEMORY_MMAP ||
1092 size != PAGE_ALIGN(buf->vbuf.length)) {
1093 ret = -EINVAL;
1094 goto done;
1095 }
1096
1097 ret = remap_vmalloc_range(vma, buf->vaddr, 0);
1098 if (ret < 0)
1099 goto done;
1100
1101 vma->vm_ops = &isp_video_queue_vm_ops;
1102 vma->vm_private_data = buf;
1103 isp_video_queue_vm_open(vma);
1104
1105done:
1106 mutex_unlock(&queue->lock);
1107 return ret;
1108}
1109
1110/**
1111 * omap3isp_video_queue_poll - Poll video queue state
1112 *
1113 * This function is intended to be used as a poll() file operation handler. It
1114 * polls the state of the video buffer at the front of the queue and returns an
1115 * events mask.
1116 *
1117 * If no buffer is present at the front of the queue, POLLERR is returned.
1118 */
1119unsigned int omap3isp_video_queue_poll(struct isp_video_queue *queue,
1120 struct file *file, poll_table *wait)
1121{
1122 struct isp_video_buffer *buf;
1123 unsigned int mask = 0;
1124
1125 mutex_lock(&queue->lock);
1126 if (list_empty(&queue->queue)) {
1127 mask |= POLLERR;
1128 goto done;
1129 }
1130 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
1131
1132 poll_wait(file, &buf->wait, wait);
1133 if (buf->state == ISP_BUF_STATE_DONE ||
1134 buf->state == ISP_BUF_STATE_ERROR) {
1135 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1136 mask |= POLLIN | POLLRDNORM;
1137 else
1138 mask |= POLLOUT | POLLWRNORM;
1139 }
1140
1141done:
1142 mutex_unlock(&queue->lock);
1143 return mask;
1144}