blob: 51ec40d6ea420daa70e5e1812a12612c44f1502c [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/* -----------------------------------------------------------------------------
42 * IOMMU management
43 */
44
45#define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
46
47/*
Laurent Pinchart73c1ea42014-01-06 16:21:54 -030048 * ispmmu_vmap - Wrapper for virtual memory mapping of a scatter gather table
Laurent Pinchart90004272014-01-02 22:12:42 -030049 * @dev: Device pointer specific to the OMAP3 ISP.
Laurent Pinchart73c1ea42014-01-06 16:21:54 -030050 * @sgt: Pointer to source scatter gather table.
Laurent Pinchart90004272014-01-02 22:12:42 -030051 *
52 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
53 * we ran out of memory.
54 */
55static dma_addr_t
Laurent Pinchart73c1ea42014-01-06 16:21:54 -030056ispmmu_vmap(struct isp_device *isp, const struct sg_table *sgt)
Laurent Pinchart90004272014-01-02 22:12:42 -030057{
Laurent Pinchart73c1ea42014-01-06 16:21:54 -030058 return omap_iommu_vmap(isp->domain, isp->dev, 0, sgt, IOMMU_FLAG);
Laurent Pinchart90004272014-01-02 22:12:42 -030059}
60
61/*
62 * ispmmu_vunmap - Unmap a device address from the ISP MMU
63 * @dev: Device pointer specific to the OMAP3 ISP.
64 * @da: Device address generated from a ispmmu_vmap call.
65 */
66static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
67{
Laurent Pinchart73c1ea42014-01-06 16:21:54 -030068 omap_iommu_vunmap(isp->domain, isp->dev, (u32)da);
Laurent Pinchart90004272014-01-02 22:12:42 -030069}
Laurent Pinchartad614ac2011-02-12 18:05:06 -030070
71/* -----------------------------------------------------------------------------
72 * Video buffers management
73 */
74
75/*
76 * isp_video_buffer_cache_sync - Keep the buffers coherent between CPU and ISP
77 *
78 * The typical operation required here is Cache Invalidation across
79 * the (user space) buffer address range. And this _must_ be done
80 * at QBUF stage (and *only* at QBUF).
81 *
82 * We try to use optimal cache invalidation function:
83 * - dmac_map_area:
84 * - used when the number of pages are _low_.
85 * - it becomes quite slow as the number of pages increase.
86 * - for 648x492 viewfinder (150 pages) it takes 1.3 ms.
87 * - for 5 Mpix buffer (2491 pages) it takes between 25-50 ms.
88 *
89 * - flush_cache_all:
90 * - used when the number of pages are _high_.
91 * - time taken in the range of 500-900 us.
92 * - has a higher penalty but, as whole dcache + icache is invalidated
93 */
94/*
95 * FIXME: dmac_inv_range crashes randomly on the user space buffer
96 * address. Fall back to flush_cache_all for now.
97 */
98#define ISP_CACHE_FLUSH_PAGES_MAX 0
99
100static void isp_video_buffer_cache_sync(struct isp_video_buffer *buf)
101{
102 if (buf->skip_cache)
103 return;
104
105 if (buf->vbuf.m.userptr == 0 || buf->npages == 0 ||
106 buf->npages > ISP_CACHE_FLUSH_PAGES_MAX)
107 flush_cache_all();
108 else {
109 dmac_map_area((void *)buf->vbuf.m.userptr, buf->vbuf.length,
110 DMA_FROM_DEVICE);
111 outer_inv_range(buf->vbuf.m.userptr,
112 buf->vbuf.m.userptr + buf->vbuf.length);
113 }
114}
115
116/*
117 * isp_video_buffer_lock_vma - Prevent VMAs from being unmapped
118 *
119 * Lock the VMAs underlying the given buffer into memory. This avoids the
120 * userspace buffer mapping from being swapped out, making VIPT cache handling
121 * easier.
122 *
123 * Note that the pages will not be freed as the buffers have been locked to
124 * memory using by a call to get_user_pages(), but the userspace mapping could
125 * still disappear if the VMAs are not locked. This is caused by the memory
126 * management code trying to be as lock-less as possible, which results in the
127 * userspace mapping manager not finding out that the pages are locked under
128 * some conditions.
129 */
130static int isp_video_buffer_lock_vma(struct isp_video_buffer *buf, int lock)
131{
132 struct vm_area_struct *vma;
133 unsigned long start;
134 unsigned long end;
135 int ret = 0;
136
137 if (buf->vbuf.memory == V4L2_MEMORY_MMAP)
138 return 0;
139
140 /* We can be called from workqueue context if the current task dies to
141 * unlock the VMAs. In that case there's no current memory management
142 * context so unlocking can't be performed, but the VMAs have been or
143 * are getting destroyed anyway so it doesn't really matter.
144 */
145 if (!current || !current->mm)
146 return lock ? -EINVAL : 0;
147
148 start = buf->vbuf.m.userptr;
149 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
150
151 down_write(&current->mm->mmap_sem);
152 spin_lock(&current->mm->page_table_lock);
153
154 do {
155 vma = find_vma(current->mm, start);
156 if (vma == NULL) {
157 ret = -EFAULT;
158 goto out;
159 }
160
161 if (lock)
162 vma->vm_flags |= VM_LOCKED;
163 else
164 vma->vm_flags &= ~VM_LOCKED;
165
166 start = vma->vm_end + 1;
167 } while (vma->vm_end < end);
168
169 if (lock)
170 buf->vm_flags |= VM_LOCKED;
171 else
172 buf->vm_flags &= ~VM_LOCKED;
173
174out:
175 spin_unlock(&current->mm->page_table_lock);
176 up_write(&current->mm->mmap_sem);
177 return ret;
178}
179
180/*
181 * isp_video_buffer_sglist_kernel - Build a scatter list for a vmalloc'ed buffer
182 *
183 * Iterate over the vmalloc'ed area and create a scatter list entry for every
184 * page.
185 */
186static int isp_video_buffer_sglist_kernel(struct isp_video_buffer *buf)
187{
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300188 struct scatterlist *sg;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300189 unsigned int npages;
190 unsigned int i;
191 void *addr;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300192 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300193
194 addr = buf->vaddr;
195 npages = PAGE_ALIGN(buf->vbuf.length) >> PAGE_SHIFT;
196
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300197 ret = sg_alloc_table(&buf->sgt, npages, GFP_KERNEL);
198 if (ret < 0)
199 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300200
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300201 for (sg = buf->sgt.sgl, i = 0; i < npages; ++i, addr += PAGE_SIZE) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300202 struct page *page = vmalloc_to_page(addr);
203
204 if (page == NULL || PageHighMem(page)) {
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300205 sg_free_table(&buf->sgt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300206 return -EINVAL;
207 }
208
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300209 sg_set_page(sg, page, PAGE_SIZE, 0);
210 sg = sg_next(sg);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300211 }
212
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300213 return 0;
214}
215
216/*
217 * isp_video_buffer_sglist_user - Build a scatter list for a userspace buffer
218 *
219 * Walk the buffer pages list and create a 1:1 mapping to a scatter list.
220 */
221static int isp_video_buffer_sglist_user(struct isp_video_buffer *buf)
222{
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300223 unsigned int offset = buf->offset;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300224 struct scatterlist *sg;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300225 unsigned int i;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300226 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300227
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300228 ret = sg_alloc_table(&buf->sgt, buf->npages, GFP_KERNEL);
229 if (ret < 0)
230 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300231
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300232 for (sg = buf->sgt.sgl, i = 0; i < buf->npages; ++i) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300233 if (PageHighMem(buf->pages[i])) {
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300234 sg_free_table(&buf->sgt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300235 return -EINVAL;
236 }
237
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300238 sg_set_page(sg, buf->pages[i], PAGE_SIZE - offset, offset);
239 sg = sg_next(sg);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300240 offset = 0;
241 }
242
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300243 return 0;
244}
245
246/*
247 * isp_video_buffer_sglist_pfnmap - Build a scatter list for a VM_PFNMAP buffer
248 *
249 * Create a scatter list of physically contiguous pages starting at the buffer
250 * memory physical address.
251 */
252static int isp_video_buffer_sglist_pfnmap(struct isp_video_buffer *buf)
253{
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300254 struct scatterlist *sg;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300255 unsigned int offset = buf->offset;
256 unsigned long pfn = buf->paddr >> PAGE_SHIFT;
257 unsigned int i;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300258 int ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300259
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300260 ret = sg_alloc_table(&buf->sgt, buf->npages, GFP_KERNEL);
261 if (ret < 0)
262 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300263
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300264 for (sg = buf->sgt.sgl, i = 0; i < buf->npages; ++i, ++pfn) {
265 sg_set_page(sg, pfn_to_page(pfn), PAGE_SIZE - offset, offset);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300266 /* PFNMAP buffers will not get DMA-mapped, set the DMA address
267 * manually.
268 */
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300269 sg_dma_address(sg) = (pfn << PAGE_SHIFT) + offset;
270 sg = sg_next(sg);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300271 offset = 0;
272 }
273
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300274 return 0;
275}
276
277/*
278 * isp_video_buffer_cleanup - Release pages for a userspace VMA.
279 *
280 * Release pages locked by a call isp_video_buffer_prepare_user and free the
281 * pages table.
282 */
283static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
284{
Laurent Pinchart90004272014-01-02 22:12:42 -0300285 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
286 struct isp_video *video = vfh->video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300287 enum dma_data_direction direction;
288 unsigned int i;
289
Laurent Pinchart90004272014-01-02 22:12:42 -0300290 if (buf->dma) {
291 ispmmu_vunmap(video->isp, buf->dma);
292 buf->dma = 0;
293 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300294
295 if (!(buf->vm_flags & VM_PFNMAP)) {
296 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
297 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300298 dma_unmap_sg(buf->queue->dev, buf->sgt.sgl, buf->sgt.orig_nents,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300299 direction);
300 }
301
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300302 sg_free_table(&buf->sgt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300303
304 if (buf->pages != NULL) {
305 isp_video_buffer_lock_vma(buf, 0);
306
307 for (i = 0; i < buf->npages; ++i)
308 page_cache_release(buf->pages[i]);
309
310 vfree(buf->pages);
311 buf->pages = NULL;
312 }
313
314 buf->npages = 0;
315 buf->skip_cache = false;
316}
317
318/*
319 * isp_video_buffer_prepare_user - Pin userspace VMA pages to memory.
320 *
321 * This function creates a list of pages for a userspace VMA. The number of
322 * pages is first computed based on the buffer size, and pages are then
323 * retrieved by a call to get_user_pages.
324 *
325 * Pages are pinned to memory by get_user_pages, making them available for DMA
326 * transfers. However, due to memory management optimization, it seems the
327 * get_user_pages doesn't guarantee that the pinned pages will not be written
328 * to swap and removed from the userspace mapping(s). When this happens, a page
329 * fault can be generated when accessing those unmapped pages.
330 *
331 * If the fault is triggered by a page table walk caused by VIPT cache
332 * management operations, the page fault handler might oops if the MM semaphore
333 * is held, as it can't handle kernel page faults in that case. To fix that, a
334 * fixup entry needs to be added to the cache management code, or the userspace
335 * VMA must be locked to avoid removing pages from the userspace mapping in the
336 * first place.
337 *
338 * If the number of pages retrieved is smaller than the number required by the
339 * buffer size, the function returns -EFAULT.
340 */
341static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf)
342{
343 unsigned long data;
344 unsigned int first;
345 unsigned int last;
346 int ret;
347
348 data = buf->vbuf.m.userptr;
349 first = (data & PAGE_MASK) >> PAGE_SHIFT;
350 last = ((data + buf->vbuf.length - 1) & PAGE_MASK) >> PAGE_SHIFT;
351
352 buf->offset = data & ~PAGE_MASK;
353 buf->npages = last - first + 1;
354 buf->pages = vmalloc(buf->npages * sizeof(buf->pages[0]));
355 if (buf->pages == NULL)
356 return -ENOMEM;
357
358 down_read(&current->mm->mmap_sem);
359 ret = get_user_pages(current, current->mm, data & PAGE_MASK,
360 buf->npages,
361 buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
362 buf->pages, NULL);
363 up_read(&current->mm->mmap_sem);
364
365 if (ret != buf->npages) {
Laurent Pinchart2578dfb2011-04-07 13:30:14 -0300366 buf->npages = ret < 0 ? 0 : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300367 isp_video_buffer_cleanup(buf);
368 return -EFAULT;
369 }
370
371 ret = isp_video_buffer_lock_vma(buf, 1);
372 if (ret < 0)
373 isp_video_buffer_cleanup(buf);
374
375 return ret;
376}
377
378/*
379 * isp_video_buffer_prepare_pfnmap - Validate a VM_PFNMAP userspace buffer
380 *
381 * Userspace VM_PFNMAP buffers are supported only if they are contiguous in
382 * memory and if they span a single VMA.
383 *
384 * Return 0 if the buffer is valid, or -EFAULT otherwise.
385 */
386static int isp_video_buffer_prepare_pfnmap(struct isp_video_buffer *buf)
387{
388 struct vm_area_struct *vma;
389 unsigned long prev_pfn;
390 unsigned long this_pfn;
391 unsigned long start;
392 unsigned long end;
Laurent Pincharte19bc862012-12-17 04:52:48 -0300393 dma_addr_t pa = 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300394 int ret = -EFAULT;
395
396 start = buf->vbuf.m.userptr;
397 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
398
399 buf->offset = start & ~PAGE_MASK;
400 buf->npages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
401 buf->pages = NULL;
402
403 down_read(&current->mm->mmap_sem);
404 vma = find_vma(current->mm, start);
405 if (vma == NULL || vma->vm_end < end)
406 goto done;
407
408 for (prev_pfn = 0; start <= end; start += PAGE_SIZE) {
409 ret = follow_pfn(vma, start, &this_pfn);
410 if (ret)
411 goto done;
412
413 if (prev_pfn == 0)
414 pa = this_pfn << PAGE_SHIFT;
415 else if (this_pfn != prev_pfn + 1) {
416 ret = -EFAULT;
417 goto done;
418 }
419
420 prev_pfn = this_pfn;
421 }
422
423 buf->paddr = pa + buf->offset;
424 ret = 0;
425
426done:
427 up_read(&current->mm->mmap_sem);
428 return ret;
429}
430
431/*
432 * isp_video_buffer_prepare_vm_flags - Get VMA flags for a userspace address
433 *
434 * This function locates the VMAs for the buffer's userspace address and checks
Michael Jones2d4e9d12011-02-28 08:29:03 -0300435 * that their flags match. The only flag that we need to care for at the moment
436 * is VM_PFNMAP.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300437 *
438 * The buffer vm_flags field is set to the first VMA flags.
439 *
440 * Return -EFAULT if no VMA can be found for part of the buffer, or if the VMAs
441 * have incompatible flags.
442 */
443static int isp_video_buffer_prepare_vm_flags(struct isp_video_buffer *buf)
444{
445 struct vm_area_struct *vma;
Laurent Pincharte19bc862012-12-17 04:52:48 -0300446 pgprot_t uninitialized_var(vm_page_prot);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300447 unsigned long start;
448 unsigned long end;
449 int ret = -EFAULT;
450
451 start = buf->vbuf.m.userptr;
452 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
453
454 down_read(&current->mm->mmap_sem);
455
456 do {
457 vma = find_vma(current->mm, start);
458 if (vma == NULL)
459 goto done;
460
461 if (start == buf->vbuf.m.userptr) {
462 buf->vm_flags = vma->vm_flags;
463 vm_page_prot = vma->vm_page_prot;
464 }
465
466 if ((buf->vm_flags ^ vma->vm_flags) & VM_PFNMAP)
467 goto done;
468
469 if (vm_page_prot != vma->vm_page_prot)
470 goto done;
471
472 start = vma->vm_end + 1;
473 } while (vma->vm_end < end);
474
475 /* Skip cache management to enhance performances for non-cached or
476 * write-combining buffers.
477 */
478 if (vm_page_prot == pgprot_noncached(vm_page_prot) ||
479 vm_page_prot == pgprot_writecombine(vm_page_prot))
480 buf->skip_cache = true;
481
482 ret = 0;
483
484done:
485 up_read(&current->mm->mmap_sem);
486 return ret;
487}
488
489/*
490 * isp_video_buffer_prepare - Make a buffer ready for operation
491 *
492 * Preparing a buffer involves:
493 *
494 * - validating VMAs (userspace buffers only)
495 * - locking pages and VMAs into memory (userspace buffers only)
496 * - building page and scatter-gather lists
497 * - mapping buffers for DMA operation
498 * - performing driver-specific preparation
499 *
500 * The function must be called in userspace context with a valid mm context
501 * (this excludes cleanup paths such as sys_close when the userspace process
502 * segfaults).
503 */
504static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
505{
Laurent Pinchart90004272014-01-02 22:12:42 -0300506 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
507 struct isp_video *video = vfh->video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300508 enum dma_data_direction direction;
Laurent Pinchart90004272014-01-02 22:12:42 -0300509 unsigned long addr;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300510 int ret;
511
512 switch (buf->vbuf.memory) {
513 case V4L2_MEMORY_MMAP:
514 ret = isp_video_buffer_sglist_kernel(buf);
515 break;
516
517 case V4L2_MEMORY_USERPTR:
518 ret = isp_video_buffer_prepare_vm_flags(buf);
519 if (ret < 0)
520 return ret;
521
522 if (buf->vm_flags & VM_PFNMAP) {
523 ret = isp_video_buffer_prepare_pfnmap(buf);
524 if (ret < 0)
525 return ret;
526
527 ret = isp_video_buffer_sglist_pfnmap(buf);
528 } else {
529 ret = isp_video_buffer_prepare_user(buf);
530 if (ret < 0)
531 return ret;
532
533 ret = isp_video_buffer_sglist_user(buf);
534 }
535 break;
536
537 default:
538 return -EINVAL;
539 }
540
541 if (ret < 0)
542 goto done;
543
544 if (!(buf->vm_flags & VM_PFNMAP)) {
545 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
546 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300547 ret = dma_map_sg(buf->queue->dev, buf->sgt.sgl,
548 buf->sgt.orig_nents, direction);
549 if (ret != buf->sgt.orig_nents) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300550 ret = -EFAULT;
551 goto done;
552 }
553 }
554
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300555 addr = ispmmu_vmap(video->isp, &buf->sgt);
Laurent Pinchart90004272014-01-02 22:12:42 -0300556 if (IS_ERR_VALUE(addr)) {
557 ret = -EIO;
558 goto done;
559 }
560
561 buf->dma = addr;
562
563 if (!IS_ALIGNED(addr, 32)) {
564 dev_dbg(video->isp->dev,
565 "Buffer address must be aligned to 32 bytes boundary.\n");
566 ret = -EINVAL;
567 goto done;
568 }
569
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300570 if (buf->queue->ops->buffer_prepare)
571 ret = buf->queue->ops->buffer_prepare(buf);
572
573done:
574 if (ret < 0) {
575 isp_video_buffer_cleanup(buf);
576 return ret;
577 }
578
579 return ret;
580}
581
582/*
583 * isp_video_queue_query - Query the status of a given buffer
584 *
585 * Locking: must be called with the queue lock held.
586 */
587static void isp_video_buffer_query(struct isp_video_buffer *buf,
588 struct v4l2_buffer *vbuf)
589{
590 memcpy(vbuf, &buf->vbuf, sizeof(*vbuf));
591
592 if (buf->vma_use_count)
593 vbuf->flags |= V4L2_BUF_FLAG_MAPPED;
594
595 switch (buf->state) {
596 case ISP_BUF_STATE_ERROR:
597 vbuf->flags |= V4L2_BUF_FLAG_ERROR;
Laurent Pinchart792e8ec2013-12-09 22:46:17 -0300598 /* Fallthrough */
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300599 case ISP_BUF_STATE_DONE:
600 vbuf->flags |= V4L2_BUF_FLAG_DONE;
Laurent Pinchart792e8ec2013-12-09 22:46:17 -0300601 break;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300602 case ISP_BUF_STATE_QUEUED:
603 case ISP_BUF_STATE_ACTIVE:
604 vbuf->flags |= V4L2_BUF_FLAG_QUEUED;
605 break;
606 case ISP_BUF_STATE_IDLE:
607 default:
608 break;
609 }
610}
611
612/*
613 * isp_video_buffer_wait - Wait for a buffer to be ready
614 *
615 * In non-blocking mode, return immediately with 0 if the buffer is ready or
616 * -EAGAIN if the buffer is in the QUEUED or ACTIVE state.
617 *
618 * In blocking mode, wait (interruptibly but with no timeout) on the buffer wait
619 * queue using the same condition.
620 */
621static int isp_video_buffer_wait(struct isp_video_buffer *buf, int nonblocking)
622{
623 if (nonblocking) {
624 return (buf->state != ISP_BUF_STATE_QUEUED &&
625 buf->state != ISP_BUF_STATE_ACTIVE)
626 ? 0 : -EAGAIN;
627 }
628
629 return wait_event_interruptible(buf->wait,
630 buf->state != ISP_BUF_STATE_QUEUED &&
631 buf->state != ISP_BUF_STATE_ACTIVE);
632}
633
634/* -----------------------------------------------------------------------------
635 * Queue management
636 */
637
638/*
639 * isp_video_queue_free - Free video buffers memory
640 *
641 * Buffers can only be freed if the queue isn't streaming and if no buffer is
Lad, Prabhakar25aeb412014-02-21 09:07:21 -0300642 * mapped to userspace. Return -EBUSY if those conditions aren't satisfied.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300643 *
644 * This function must be called with the queue lock held.
645 */
646static int isp_video_queue_free(struct isp_video_queue *queue)
647{
648 unsigned int i;
649
650 if (queue->streaming)
651 return -EBUSY;
652
653 for (i = 0; i < queue->count; ++i) {
654 if (queue->buffers[i]->vma_use_count != 0)
655 return -EBUSY;
656 }
657
658 for (i = 0; i < queue->count; ++i) {
659 struct isp_video_buffer *buf = queue->buffers[i];
660
661 isp_video_buffer_cleanup(buf);
662
663 vfree(buf->vaddr);
664 buf->vaddr = NULL;
665
666 kfree(buf);
667 queue->buffers[i] = NULL;
668 }
669
670 INIT_LIST_HEAD(&queue->queue);
671 queue->count = 0;
672 return 0;
673}
674
675/*
676 * isp_video_queue_alloc - Allocate video buffers memory
677 *
678 * This function must be called with the queue lock held.
679 */
680static int isp_video_queue_alloc(struct isp_video_queue *queue,
681 unsigned int nbuffers,
682 unsigned int size, enum v4l2_memory memory)
683{
684 struct isp_video_buffer *buf;
685 unsigned int i;
686 void *mem;
687 int ret;
688
689 /* Start by freeing the buffers. */
690 ret = isp_video_queue_free(queue);
691 if (ret < 0)
692 return ret;
693
Michael Jones9d380ad2012-07-26 10:48:25 -0300694 /* Bail out if no buffers should be allocated. */
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300695 if (nbuffers == 0)
696 return 0;
697
698 /* Initialize the allocated buffers. */
699 for (i = 0; i < nbuffers; ++i) {
700 buf = kzalloc(queue->bufsize, GFP_KERNEL);
701 if (buf == NULL)
702 break;
703
704 if (memory == V4L2_MEMORY_MMAP) {
705 /* Allocate video buffers memory for mmap mode. Align
706 * the size to the page size.
707 */
708 mem = vmalloc_32_user(PAGE_ALIGN(size));
709 if (mem == NULL) {
710 kfree(buf);
711 break;
712 }
713
714 buf->vbuf.m.offset = i * PAGE_ALIGN(size);
715 buf->vaddr = mem;
716 }
717
718 buf->vbuf.index = i;
719 buf->vbuf.length = size;
720 buf->vbuf.type = queue->type;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300721 buf->vbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300722 buf->vbuf.field = V4L2_FIELD_NONE;
723 buf->vbuf.memory = memory;
724
725 buf->queue = queue;
726 init_waitqueue_head(&buf->wait);
727
728 queue->buffers[i] = buf;
729 }
730
731 if (i == 0)
732 return -ENOMEM;
733
734 queue->count = i;
735 return nbuffers;
736}
737
738/**
739 * omap3isp_video_queue_cleanup - Clean up the video buffers queue
740 * @queue: Video buffers queue
741 *
742 * Free all allocated resources and clean up the video buffers queue. The queue
743 * must not be busy (no ongoing video stream) and buffers must have been
744 * unmapped.
745 *
746 * Return 0 on success or -EBUSY if the queue is busy or buffers haven't been
747 * unmapped.
748 */
749int omap3isp_video_queue_cleanup(struct isp_video_queue *queue)
750{
751 return isp_video_queue_free(queue);
752}
753
754/**
755 * omap3isp_video_queue_init - Initialize the video buffers queue
756 * @queue: Video buffers queue
757 * @type: V4L2 buffer type (capture or output)
758 * @ops: Driver-specific queue operations
759 * @dev: Device used for DMA operations
760 * @bufsize: Size of the driver-specific buffer structure
761 *
762 * Initialize the video buffers queue with the supplied parameters.
763 *
764 * The queue type must be one of V4L2_BUF_TYPE_VIDEO_CAPTURE or
765 * V4L2_BUF_TYPE_VIDEO_OUTPUT. Other buffer types are not supported yet.
766 *
767 * Buffer objects will be allocated using the given buffer size to allow room
768 * for driver-specific fields. Driver-specific buffer structures must start
769 * with a struct isp_video_buffer field. Drivers with no driver-specific buffer
770 * structure must pass the size of the isp_video_buffer structure in the bufsize
771 * parameter.
772 *
773 * Return 0 on success.
774 */
775int omap3isp_video_queue_init(struct isp_video_queue *queue,
776 enum v4l2_buf_type type,
777 const struct isp_video_queue_operations *ops,
778 struct device *dev, unsigned int bufsize)
779{
780 INIT_LIST_HEAD(&queue->queue);
781 mutex_init(&queue->lock);
782 spin_lock_init(&queue->irqlock);
783
784 queue->type = type;
785 queue->ops = ops;
786 queue->dev = dev;
787 queue->bufsize = bufsize;
788
789 return 0;
790}
791
792/* -----------------------------------------------------------------------------
793 * V4L2 operations
794 */
795
796/**
797 * omap3isp_video_queue_reqbufs - Allocate video buffers memory
798 *
799 * This function is intended to be used as a VIDIOC_REQBUFS ioctl handler. It
800 * allocated video buffer objects and, for MMAP buffers, buffer memory.
801 *
802 * If the number of buffers is 0, all buffers are freed and the function returns
803 * without performing any allocation.
804 *
805 * If the number of buffers is not 0, currently allocated buffers (if any) are
806 * freed and the requested number of buffers are allocated. Depending on
807 * driver-specific requirements and on memory availability, a number of buffer
808 * smaller or bigger than requested can be allocated. This isn't considered as
809 * an error.
810 *
811 * Return 0 on success or one of the following error codes:
812 *
813 * -EINVAL if the buffer type or index are invalid
814 * -EBUSY if the queue is busy (streaming or buffers mapped)
815 * -ENOMEM if the buffers can't be allocated due to an out-of-memory condition
816 */
817int omap3isp_video_queue_reqbufs(struct isp_video_queue *queue,
818 struct v4l2_requestbuffers *rb)
819{
820 unsigned int nbuffers = rb->count;
821 unsigned int size;
822 int ret;
823
824 if (rb->type != queue->type)
825 return -EINVAL;
826
827 queue->ops->queue_prepare(queue, &nbuffers, &size);
828 if (size == 0)
829 return -EINVAL;
830
831 nbuffers = min_t(unsigned int, nbuffers, ISP_VIDEO_MAX_BUFFERS);
832
833 mutex_lock(&queue->lock);
834
835 ret = isp_video_queue_alloc(queue, nbuffers, size, rb->memory);
836 if (ret < 0)
837 goto done;
838
839 rb->count = ret;
840 ret = 0;
841
842done:
843 mutex_unlock(&queue->lock);
844 return ret;
845}
846
847/**
848 * omap3isp_video_queue_querybuf - Query the status of a buffer in a queue
849 *
850 * This function is intended to be used as a VIDIOC_QUERYBUF ioctl handler. It
851 * returns the status of a given video buffer.
852 *
853 * Return 0 on success or -EINVAL if the buffer type or index are invalid.
854 */
855int omap3isp_video_queue_querybuf(struct isp_video_queue *queue,
856 struct v4l2_buffer *vbuf)
857{
858 struct isp_video_buffer *buf;
859 int ret = 0;
860
861 if (vbuf->type != queue->type)
862 return -EINVAL;
863
864 mutex_lock(&queue->lock);
865
866 if (vbuf->index >= queue->count) {
867 ret = -EINVAL;
868 goto done;
869 }
870
871 buf = queue->buffers[vbuf->index];
872 isp_video_buffer_query(buf, vbuf);
873
874done:
875 mutex_unlock(&queue->lock);
876 return ret;
877}
878
879/**
880 * omap3isp_video_queue_qbuf - Queue a buffer
881 *
882 * This function is intended to be used as a VIDIOC_QBUF ioctl handler.
883 *
884 * The v4l2_buffer structure passed from userspace is first sanity tested. If
885 * sane, the buffer is then processed and added to the main queue and, if the
886 * queue is streaming, to the IRQ queue.
887 *
888 * Before being enqueued, USERPTR buffers are checked for address changes. If
889 * the buffer has a different userspace address, the old memory area is unlocked
890 * and the new memory area is locked.
891 */
892int omap3isp_video_queue_qbuf(struct isp_video_queue *queue,
893 struct v4l2_buffer *vbuf)
894{
895 struct isp_video_buffer *buf;
896 unsigned long flags;
897 int ret = -EINVAL;
898
899 if (vbuf->type != queue->type)
900 goto done;
901
902 mutex_lock(&queue->lock);
903
904 if (vbuf->index >= queue->count)
905 goto done;
906
907 buf = queue->buffers[vbuf->index];
908
909 if (vbuf->memory != buf->vbuf.memory)
910 goto done;
911
912 if (buf->state != ISP_BUF_STATE_IDLE)
913 goto done;
914
915 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
Michael Jones61e65612011-08-09 08:42:20 +0200916 vbuf->length < buf->vbuf.length)
917 goto done;
918
919 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300920 vbuf->m.userptr != buf->vbuf.m.userptr) {
921 isp_video_buffer_cleanup(buf);
922 buf->vbuf.m.userptr = vbuf->m.userptr;
923 buf->prepared = 0;
924 }
925
926 if (!buf->prepared) {
927 ret = isp_video_buffer_prepare(buf);
928 if (ret < 0)
929 goto done;
930 buf->prepared = 1;
931 }
932
933 isp_video_buffer_cache_sync(buf);
934
935 buf->state = ISP_BUF_STATE_QUEUED;
936 list_add_tail(&buf->stream, &queue->queue);
937
938 if (queue->streaming) {
939 spin_lock_irqsave(&queue->irqlock, flags);
940 queue->ops->buffer_queue(buf);
941 spin_unlock_irqrestore(&queue->irqlock, flags);
942 }
943
944 ret = 0;
945
946done:
947 mutex_unlock(&queue->lock);
948 return ret;
949}
950
951/**
952 * omap3isp_video_queue_dqbuf - Dequeue a buffer
953 *
954 * This function is intended to be used as a VIDIOC_DQBUF ioctl handler.
955 *
Michael Jonesb1e71f32012-06-27 12:06:57 -0300956 * Wait until a buffer is ready to be dequeued, remove it from the queue and
957 * copy its information to the v4l2_buffer structure.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300958 *
Michael Jonesb1e71f32012-06-27 12:06:57 -0300959 * If the nonblocking argument is not zero and no buffer is ready, return
960 * -EAGAIN immediately instead of waiting.
961 *
962 * If no buffer has been enqueued, or if the requested buffer type doesn't match
963 * the queue type, return -EINVAL.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300964 */
965int omap3isp_video_queue_dqbuf(struct isp_video_queue *queue,
966 struct v4l2_buffer *vbuf, int nonblocking)
967{
968 struct isp_video_buffer *buf;
969 int ret;
970
971 if (vbuf->type != queue->type)
972 return -EINVAL;
973
974 mutex_lock(&queue->lock);
975
976 if (list_empty(&queue->queue)) {
977 ret = -EINVAL;
978 goto done;
979 }
980
981 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
982 ret = isp_video_buffer_wait(buf, nonblocking);
983 if (ret < 0)
984 goto done;
985
986 list_del(&buf->stream);
987
988 isp_video_buffer_query(buf, vbuf);
989 buf->state = ISP_BUF_STATE_IDLE;
990 vbuf->flags &= ~V4L2_BUF_FLAG_QUEUED;
991
992done:
993 mutex_unlock(&queue->lock);
994 return ret;
995}
996
997/**
998 * omap3isp_video_queue_streamon - Start streaming
999 *
1000 * This function is intended to be used as a VIDIOC_STREAMON ioctl handler. It
1001 * starts streaming on the queue and calls the buffer_queue operation for all
1002 * queued buffers.
1003 *
1004 * Return 0 on success.
1005 */
1006int omap3isp_video_queue_streamon(struct isp_video_queue *queue)
1007{
1008 struct isp_video_buffer *buf;
1009 unsigned long flags;
1010
1011 mutex_lock(&queue->lock);
1012
1013 if (queue->streaming)
1014 goto done;
1015
1016 queue->streaming = 1;
1017
1018 spin_lock_irqsave(&queue->irqlock, flags);
1019 list_for_each_entry(buf, &queue->queue, stream)
1020 queue->ops->buffer_queue(buf);
1021 spin_unlock_irqrestore(&queue->irqlock, flags);
1022
1023done:
1024 mutex_unlock(&queue->lock);
1025 return 0;
1026}
1027
1028/**
1029 * omap3isp_video_queue_streamoff - Stop streaming
1030 *
1031 * This function is intended to be used as a VIDIOC_STREAMOFF ioctl handler. It
1032 * stops streaming on the queue and wakes up all the buffers.
1033 *
1034 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1035 * delayed works before calling this function to make sure no buffer will be
1036 * touched by the driver and/or hardware.
1037 */
1038void omap3isp_video_queue_streamoff(struct isp_video_queue *queue)
1039{
1040 struct isp_video_buffer *buf;
1041 unsigned long flags;
1042 unsigned int i;
1043
1044 mutex_lock(&queue->lock);
1045
1046 if (!queue->streaming)
1047 goto done;
1048
1049 queue->streaming = 0;
1050
1051 spin_lock_irqsave(&queue->irqlock, flags);
1052 for (i = 0; i < queue->count; ++i) {
1053 buf = queue->buffers[i];
1054
1055 if (buf->state == ISP_BUF_STATE_ACTIVE)
1056 wake_up(&buf->wait);
1057
1058 buf->state = ISP_BUF_STATE_IDLE;
1059 }
1060 spin_unlock_irqrestore(&queue->irqlock, flags);
1061
1062 INIT_LIST_HEAD(&queue->queue);
1063
1064done:
1065 mutex_unlock(&queue->lock);
1066}
1067
1068/**
1069 * omap3isp_video_queue_discard_done - Discard all buffers marked as DONE
1070 *
1071 * This function is intended to be used with suspend/resume operations. It
1072 * discards all 'done' buffers as they would be too old to be requested after
1073 * resume.
1074 *
1075 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1076 * delayed works before calling this function to make sure no buffer will be
1077 * touched by the driver and/or hardware.
1078 */
1079void omap3isp_video_queue_discard_done(struct isp_video_queue *queue)
1080{
1081 struct isp_video_buffer *buf;
1082 unsigned int i;
1083
1084 mutex_lock(&queue->lock);
1085
1086 if (!queue->streaming)
1087 goto done;
1088
1089 for (i = 0; i < queue->count; ++i) {
1090 buf = queue->buffers[i];
1091
1092 if (buf->state == ISP_BUF_STATE_DONE)
1093 buf->state = ISP_BUF_STATE_ERROR;
1094 }
1095
1096done:
1097 mutex_unlock(&queue->lock);
1098}
1099
1100static void isp_video_queue_vm_open(struct vm_area_struct *vma)
1101{
1102 struct isp_video_buffer *buf = vma->vm_private_data;
1103
1104 buf->vma_use_count++;
1105}
1106
1107static void isp_video_queue_vm_close(struct vm_area_struct *vma)
1108{
1109 struct isp_video_buffer *buf = vma->vm_private_data;
1110
1111 buf->vma_use_count--;
1112}
1113
1114static const struct vm_operations_struct isp_video_queue_vm_ops = {
1115 .open = isp_video_queue_vm_open,
1116 .close = isp_video_queue_vm_close,
1117};
1118
1119/**
1120 * omap3isp_video_queue_mmap - Map buffers to userspace
1121 *
1122 * This function is intended to be used as an mmap() file operation handler. It
1123 * maps a buffer to userspace based on the VMA offset.
1124 *
1125 * Only buffers of memory type MMAP are supported.
1126 */
1127int omap3isp_video_queue_mmap(struct isp_video_queue *queue,
1128 struct vm_area_struct *vma)
1129{
1130 struct isp_video_buffer *uninitialized_var(buf);
1131 unsigned long size;
1132 unsigned int i;
1133 int ret = 0;
1134
1135 mutex_lock(&queue->lock);
1136
1137 for (i = 0; i < queue->count; ++i) {
1138 buf = queue->buffers[i];
1139 if ((buf->vbuf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1140 break;
1141 }
1142
1143 if (i == queue->count) {
1144 ret = -EINVAL;
1145 goto done;
1146 }
1147
1148 size = vma->vm_end - vma->vm_start;
1149
1150 if (buf->vbuf.memory != V4L2_MEMORY_MMAP ||
1151 size != PAGE_ALIGN(buf->vbuf.length)) {
1152 ret = -EINVAL;
1153 goto done;
1154 }
1155
1156 ret = remap_vmalloc_range(vma, buf->vaddr, 0);
1157 if (ret < 0)
1158 goto done;
1159
1160 vma->vm_ops = &isp_video_queue_vm_ops;
1161 vma->vm_private_data = buf;
1162 isp_video_queue_vm_open(vma);
1163
1164done:
1165 mutex_unlock(&queue->lock);
1166 return ret;
1167}
1168
1169/**
1170 * omap3isp_video_queue_poll - Poll video queue state
1171 *
1172 * This function is intended to be used as a poll() file operation handler. It
1173 * polls the state of the video buffer at the front of the queue and returns an
1174 * events mask.
1175 *
1176 * If no buffer is present at the front of the queue, POLLERR is returned.
1177 */
1178unsigned int omap3isp_video_queue_poll(struct isp_video_queue *queue,
1179 struct file *file, poll_table *wait)
1180{
1181 struct isp_video_buffer *buf;
1182 unsigned int mask = 0;
1183
1184 mutex_lock(&queue->lock);
1185 if (list_empty(&queue->queue)) {
1186 mask |= POLLERR;
1187 goto done;
1188 }
1189 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
1190
1191 poll_wait(file, &buf->wait, wait);
1192 if (buf->state == ISP_BUF_STATE_DONE ||
1193 buf->state == ISP_BUF_STATE_ERROR) {
1194 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1195 mask |= POLLIN | POLLRDNORM;
1196 else
1197 mask |= POLLOUT | POLLWRNORM;
1198 }
1199
1200done:
1201 mutex_unlock(&queue->lock);
1202 return mask;
1203}