blob: 2fd254f4dbe2e8c332d7f675cfa610ff9b97ab67 [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 Pinchart7f5036d2014-03-08 09:38:38 -0300151 * isp_video_buffer_prepare_kernel - Build scatter list for a kernel-allocated
152 * buffer
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300153 *
Laurent Pinchart7f5036d2014-03-08 09:38:38 -0300154 * Retrieve the sgtable using the DMA API.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300155 */
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 Pinchart7f5036d2014-03-08 09:38:38 -0300158 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
159 struct isp_video *video = vfh->video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300160
Laurent Pinchart7f5036d2014-03-08 09:38:38 -0300161 return dma_get_sgtable(video->isp->dev, &buf->sgt, buf->vaddr,
162 buf->paddr, PAGE_ALIGN(buf->vbuf.length));
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300163}
164
165/*
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300166 * isp_video_buffer_cleanup - Release pages for a userspace VMA.
167 *
168 * Release pages locked by a call isp_video_buffer_prepare_user and free the
169 * pages table.
170 */
171static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
172{
Laurent Pinchart90004272014-01-02 22:12:42 -0300173 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
174 struct isp_video *video = vfh->video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300175 enum dma_data_direction direction;
176 unsigned int i;
177
Laurent Pinchart90004272014-01-02 22:12:42 -0300178 if (buf->dma) {
Laurent Pinchartd13f19f2014-01-06 15:30:03 -0300179 omap_iommu_vunmap(video->isp->domain, video->isp->dev,
180 buf->dma);
Laurent Pinchart90004272014-01-02 22:12:42 -0300181 buf->dma = 0;
182 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300183
184 if (!(buf->vm_flags & VM_PFNMAP)) {
185 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
186 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300187 dma_unmap_sg(buf->queue->dev, buf->sgt.sgl, buf->sgt.orig_nents,
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300188 direction);
189 }
190
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300191 sg_free_table(&buf->sgt);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300192
193 if (buf->pages != NULL) {
194 isp_video_buffer_lock_vma(buf, 0);
195
196 for (i = 0; i < buf->npages; ++i)
197 page_cache_release(buf->pages[i]);
198
199 vfree(buf->pages);
200 buf->pages = NULL;
201 }
202
203 buf->npages = 0;
204 buf->skip_cache = false;
205}
206
207/*
Laurent Pinchart82716012014-01-06 15:30:03 -0300208 * isp_video_buffer_prepare_user - Prepare a userspace buffer.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300209 *
Laurent Pinchart82716012014-01-06 15:30:03 -0300210 * This function creates a scatter list with a 1:1 mapping for a userspace VMA.
211 * The number of pages is first computed based on the buffer size, and pages are
212 * then retrieved by a call to get_user_pages.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300213 *
214 * Pages are pinned to memory by get_user_pages, making them available for DMA
215 * transfers. However, due to memory management optimization, it seems the
216 * get_user_pages doesn't guarantee that the pinned pages will not be written
217 * to swap and removed from the userspace mapping(s). When this happens, a page
218 * fault can be generated when accessing those unmapped pages.
219 *
220 * If the fault is triggered by a page table walk caused by VIPT cache
221 * management operations, the page fault handler might oops if the MM semaphore
222 * is held, as it can't handle kernel page faults in that case. To fix that, a
223 * fixup entry needs to be added to the cache management code, or the userspace
224 * VMA must be locked to avoid removing pages from the userspace mapping in the
225 * first place.
226 *
227 * If the number of pages retrieved is smaller than the number required by the
228 * buffer size, the function returns -EFAULT.
229 */
230static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf)
231{
Laurent Pinchart82716012014-01-06 15:30:03 -0300232 struct scatterlist *sg;
233 unsigned int offset;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300234 unsigned long data;
235 unsigned int first;
236 unsigned int last;
Laurent Pinchart82716012014-01-06 15:30:03 -0300237 unsigned int i;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300238 int ret;
239
240 data = buf->vbuf.m.userptr;
241 first = (data & PAGE_MASK) >> PAGE_SHIFT;
242 last = ((data + buf->vbuf.length - 1) & PAGE_MASK) >> PAGE_SHIFT;
Laurent Pinchart82716012014-01-06 15:30:03 -0300243 offset = data & ~PAGE_MASK;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300244
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300245 buf->npages = last - first + 1;
246 buf->pages = vmalloc(buf->npages * sizeof(buf->pages[0]));
247 if (buf->pages == NULL)
248 return -ENOMEM;
249
250 down_read(&current->mm->mmap_sem);
251 ret = get_user_pages(current, current->mm, data & PAGE_MASK,
252 buf->npages,
253 buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
254 buf->pages, NULL);
255 up_read(&current->mm->mmap_sem);
256
257 if (ret != buf->npages) {
Laurent Pinchart2578dfb2011-04-07 13:30:14 -0300258 buf->npages = ret < 0 ? 0 : ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300259 return -EFAULT;
260 }
261
262 ret = isp_video_buffer_lock_vma(buf, 1);
263 if (ret < 0)
Laurent Pinchart82716012014-01-06 15:30:03 -0300264 return ret;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300265
Laurent Pinchart82716012014-01-06 15:30:03 -0300266 ret = sg_alloc_table(&buf->sgt, buf->npages, GFP_KERNEL);
267 if (ret < 0)
268 return ret;
269
270 for (sg = buf->sgt.sgl, i = 0; i < buf->npages; ++i) {
271 if (PageHighMem(buf->pages[i])) {
272 sg_free_table(&buf->sgt);
273 return -EINVAL;
274 }
275
276 sg_set_page(sg, buf->pages[i], PAGE_SIZE - offset, offset);
277 sg = sg_next(sg);
278 offset = 0;
279 }
280
281 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300282}
283
284/*
Laurent Pinchart82716012014-01-06 15:30:03 -0300285 * isp_video_buffer_prepare_pfnmap - Prepare a VM_PFNMAP userspace buffer
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300286 *
287 * Userspace VM_PFNMAP buffers are supported only if they are contiguous in
Laurent Pinchart82716012014-01-06 15:30:03 -0300288 * memory and if they span a single VMA. Start by validating the user pointer to
289 * make sure it fulfils that condition, and then build a scatter list of
290 * physically contiguous pages starting at the buffer memory physical address.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300291 *
Laurent Pinchart82716012014-01-06 15:30:03 -0300292 * Return 0 on success, -EFAULT if the buffer isn't valid or -ENOMEM if memory
293 * can't be allocated.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300294 */
295static int isp_video_buffer_prepare_pfnmap(struct isp_video_buffer *buf)
296{
297 struct vm_area_struct *vma;
Laurent Pinchart82716012014-01-06 15:30:03 -0300298 struct scatterlist *sg;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300299 unsigned long prev_pfn;
300 unsigned long this_pfn;
301 unsigned long start;
Laurent Pinchart82716012014-01-06 15:30:03 -0300302 unsigned int offset;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300303 unsigned long end;
Laurent Pinchart82716012014-01-06 15:30:03 -0300304 unsigned long pfn;
305 unsigned int i;
306 int ret = 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300307
308 start = buf->vbuf.m.userptr;
309 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
Laurent Pinchart82716012014-01-06 15:30:03 -0300310 offset = start & ~PAGE_MASK;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300311
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300312 buf->npages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
313 buf->pages = NULL;
314
315 down_read(&current->mm->mmap_sem);
316 vma = find_vma(current->mm, start);
Laurent Pinchart82716012014-01-06 15:30:03 -0300317 if (vma == NULL || vma->vm_end < end) {
318 ret = -EFAULT;
319 goto unlock;
320 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300321
322 for (prev_pfn = 0; start <= end; start += PAGE_SIZE) {
323 ret = follow_pfn(vma, start, &this_pfn);
Laurent Pinchart82716012014-01-06 15:30:03 -0300324 if (ret < 0)
325 goto unlock;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300326
327 if (prev_pfn == 0)
Laurent Pinchart82716012014-01-06 15:30:03 -0300328 pfn = this_pfn;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300329 else if (this_pfn != prev_pfn + 1) {
330 ret = -EFAULT;
Laurent Pinchart82716012014-01-06 15:30:03 -0300331 goto unlock;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300332 }
333
334 prev_pfn = this_pfn;
335 }
336
Laurent Pinchart82716012014-01-06 15:30:03 -0300337unlock:
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300338 up_read(&current->mm->mmap_sem);
Laurent Pinchart82716012014-01-06 15:30:03 -0300339 if (ret < 0)
340 return ret;
341
342 ret = sg_alloc_table(&buf->sgt, buf->npages, GFP_KERNEL);
343 if (ret < 0)
344 return ret;
345
346 for (sg = buf->sgt.sgl, i = 0; i < buf->npages; ++i, ++pfn) {
347 sg_set_page(sg, pfn_to_page(pfn), PAGE_SIZE - offset, offset);
348 /* PFNMAP buffers will not get DMA-mapped, set the DMA address
349 * manually.
350 */
351 sg_dma_address(sg) = (pfn << PAGE_SHIFT) + offset;
352 sg = sg_next(sg);
353 offset = 0;
354 }
355
356 return 0;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300357}
358
359/*
360 * isp_video_buffer_prepare_vm_flags - Get VMA flags for a userspace address
361 *
362 * This function locates the VMAs for the buffer's userspace address and checks
Michael Jones2d4e9d12011-02-28 08:29:03 -0300363 * that their flags match. The only flag that we need to care for at the moment
364 * is VM_PFNMAP.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300365 *
366 * The buffer vm_flags field is set to the first VMA flags.
367 *
368 * Return -EFAULT if no VMA can be found for part of the buffer, or if the VMAs
369 * have incompatible flags.
370 */
371static int isp_video_buffer_prepare_vm_flags(struct isp_video_buffer *buf)
372{
373 struct vm_area_struct *vma;
Laurent Pincharte19bc862012-12-17 04:52:48 -0300374 pgprot_t uninitialized_var(vm_page_prot);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300375 unsigned long start;
376 unsigned long end;
377 int ret = -EFAULT;
378
379 start = buf->vbuf.m.userptr;
380 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
381
382 down_read(&current->mm->mmap_sem);
383
384 do {
385 vma = find_vma(current->mm, start);
386 if (vma == NULL)
387 goto done;
388
389 if (start == buf->vbuf.m.userptr) {
390 buf->vm_flags = vma->vm_flags;
391 vm_page_prot = vma->vm_page_prot;
392 }
393
394 if ((buf->vm_flags ^ vma->vm_flags) & VM_PFNMAP)
395 goto done;
396
397 if (vm_page_prot != vma->vm_page_prot)
398 goto done;
399
400 start = vma->vm_end + 1;
401 } while (vma->vm_end < end);
402
403 /* Skip cache management to enhance performances for non-cached or
404 * write-combining buffers.
405 */
406 if (vm_page_prot == pgprot_noncached(vm_page_prot) ||
407 vm_page_prot == pgprot_writecombine(vm_page_prot))
408 buf->skip_cache = true;
409
410 ret = 0;
411
412done:
413 up_read(&current->mm->mmap_sem);
414 return ret;
415}
416
417/*
418 * isp_video_buffer_prepare - Make a buffer ready for operation
419 *
420 * Preparing a buffer involves:
421 *
422 * - validating VMAs (userspace buffers only)
423 * - locking pages and VMAs into memory (userspace buffers only)
424 * - building page and scatter-gather lists
425 * - mapping buffers for DMA operation
426 * - performing driver-specific preparation
427 *
428 * The function must be called in userspace context with a valid mm context
429 * (this excludes cleanup paths such as sys_close when the userspace process
430 * segfaults).
431 */
432static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
433{
Laurent Pinchart90004272014-01-02 22:12:42 -0300434 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
435 struct isp_video *video = vfh->video;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300436 enum dma_data_direction direction;
Laurent Pinchart90004272014-01-02 22:12:42 -0300437 unsigned long addr;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300438 int ret;
439
440 switch (buf->vbuf.memory) {
441 case V4L2_MEMORY_MMAP:
Laurent Pinchart82716012014-01-06 15:30:03 -0300442 ret = isp_video_buffer_prepare_kernel(buf);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300443 break;
444
445 case V4L2_MEMORY_USERPTR:
446 ret = isp_video_buffer_prepare_vm_flags(buf);
447 if (ret < 0)
448 return ret;
449
Laurent Pinchart82716012014-01-06 15:30:03 -0300450 if (buf->vm_flags & VM_PFNMAP)
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300451 ret = isp_video_buffer_prepare_pfnmap(buf);
Laurent Pinchart82716012014-01-06 15:30:03 -0300452 else
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300453 ret = isp_video_buffer_prepare_user(buf);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300454 break;
455
456 default:
457 return -EINVAL;
458 }
459
460 if (ret < 0)
461 goto done;
462
463 if (!(buf->vm_flags & VM_PFNMAP)) {
464 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
465 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Laurent Pinchart73c1ea42014-01-06 16:21:54 -0300466 ret = dma_map_sg(buf->queue->dev, buf->sgt.sgl,
467 buf->sgt.orig_nents, direction);
468 if (ret != buf->sgt.orig_nents) {
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300469 ret = -EFAULT;
470 goto done;
471 }
472 }
473
Laurent Pinchartd13f19f2014-01-06 15:30:03 -0300474 addr = omap_iommu_vmap(video->isp->domain, video->isp->dev, 0,
475 &buf->sgt, IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8);
Laurent Pinchart90004272014-01-02 22:12:42 -0300476 if (IS_ERR_VALUE(addr)) {
477 ret = -EIO;
478 goto done;
479 }
480
481 buf->dma = addr;
482
483 if (!IS_ALIGNED(addr, 32)) {
484 dev_dbg(video->isp->dev,
485 "Buffer address must be aligned to 32 bytes boundary.\n");
486 ret = -EINVAL;
487 goto done;
488 }
489
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300490 if (buf->queue->ops->buffer_prepare)
491 ret = buf->queue->ops->buffer_prepare(buf);
492
493done:
494 if (ret < 0) {
495 isp_video_buffer_cleanup(buf);
496 return ret;
497 }
498
499 return ret;
500}
501
502/*
503 * isp_video_queue_query - Query the status of a given buffer
504 *
505 * Locking: must be called with the queue lock held.
506 */
507static void isp_video_buffer_query(struct isp_video_buffer *buf,
508 struct v4l2_buffer *vbuf)
509{
510 memcpy(vbuf, &buf->vbuf, sizeof(*vbuf));
511
512 if (buf->vma_use_count)
513 vbuf->flags |= V4L2_BUF_FLAG_MAPPED;
514
515 switch (buf->state) {
516 case ISP_BUF_STATE_ERROR:
517 vbuf->flags |= V4L2_BUF_FLAG_ERROR;
Laurent Pinchart792e8ec2013-12-09 22:46:17 -0300518 /* Fallthrough */
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300519 case ISP_BUF_STATE_DONE:
520 vbuf->flags |= V4L2_BUF_FLAG_DONE;
Laurent Pinchart792e8ec2013-12-09 22:46:17 -0300521 break;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300522 case ISP_BUF_STATE_QUEUED:
523 case ISP_BUF_STATE_ACTIVE:
524 vbuf->flags |= V4L2_BUF_FLAG_QUEUED;
525 break;
526 case ISP_BUF_STATE_IDLE:
527 default:
528 break;
529 }
530}
531
532/*
533 * isp_video_buffer_wait - Wait for a buffer to be ready
534 *
535 * In non-blocking mode, return immediately with 0 if the buffer is ready or
536 * -EAGAIN if the buffer is in the QUEUED or ACTIVE state.
537 *
538 * In blocking mode, wait (interruptibly but with no timeout) on the buffer wait
539 * queue using the same condition.
540 */
541static int isp_video_buffer_wait(struct isp_video_buffer *buf, int nonblocking)
542{
543 if (nonblocking) {
544 return (buf->state != ISP_BUF_STATE_QUEUED &&
545 buf->state != ISP_BUF_STATE_ACTIVE)
546 ? 0 : -EAGAIN;
547 }
548
549 return wait_event_interruptible(buf->wait,
550 buf->state != ISP_BUF_STATE_QUEUED &&
551 buf->state != ISP_BUF_STATE_ACTIVE);
552}
553
554/* -----------------------------------------------------------------------------
555 * Queue management
556 */
557
558/*
559 * isp_video_queue_free - Free video buffers memory
560 *
561 * Buffers can only be freed if the queue isn't streaming and if no buffer is
Lad, Prabhakar25aeb412014-02-21 09:07:21 -0300562 * mapped to userspace. Return -EBUSY if those conditions aren't satisfied.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300563 *
564 * This function must be called with the queue lock held.
565 */
566static int isp_video_queue_free(struct isp_video_queue *queue)
567{
568 unsigned int i;
569
570 if (queue->streaming)
571 return -EBUSY;
572
573 for (i = 0; i < queue->count; ++i) {
574 if (queue->buffers[i]->vma_use_count != 0)
575 return -EBUSY;
576 }
577
578 for (i = 0; i < queue->count; ++i) {
579 struct isp_video_buffer *buf = queue->buffers[i];
580
581 isp_video_buffer_cleanup(buf);
582
Laurent Pinchart7f5036d2014-03-08 09:38:38 -0300583 if (buf->vaddr) {
584 dma_free_coherent(queue->dev,
585 PAGE_ALIGN(buf->vbuf.length),
586 buf->vaddr, buf->paddr);
587 buf->vaddr = NULL;
588 }
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300589
590 kfree(buf);
591 queue->buffers[i] = NULL;
592 }
593
594 INIT_LIST_HEAD(&queue->queue);
595 queue->count = 0;
596 return 0;
597}
598
599/*
600 * isp_video_queue_alloc - Allocate video buffers memory
601 *
602 * This function must be called with the queue lock held.
603 */
604static int isp_video_queue_alloc(struct isp_video_queue *queue,
605 unsigned int nbuffers,
606 unsigned int size, enum v4l2_memory memory)
607{
608 struct isp_video_buffer *buf;
Laurent Pinchart7f5036d2014-03-08 09:38:38 -0300609 dma_addr_t dma;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300610 unsigned int i;
611 void *mem;
612 int ret;
613
614 /* Start by freeing the buffers. */
615 ret = isp_video_queue_free(queue);
616 if (ret < 0)
617 return ret;
618
Michael Jones9d380ad2012-07-26 10:48:25 -0300619 /* Bail out if no buffers should be allocated. */
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300620 if (nbuffers == 0)
621 return 0;
622
623 /* Initialize the allocated buffers. */
624 for (i = 0; i < nbuffers; ++i) {
625 buf = kzalloc(queue->bufsize, GFP_KERNEL);
626 if (buf == NULL)
627 break;
628
629 if (memory == V4L2_MEMORY_MMAP) {
630 /* Allocate video buffers memory for mmap mode. Align
631 * the size to the page size.
632 */
Laurent Pinchart7f5036d2014-03-08 09:38:38 -0300633 mem = dma_alloc_coherent(queue->dev, PAGE_ALIGN(size),
634 &dma, GFP_KERNEL);
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300635 if (mem == NULL) {
636 kfree(buf);
637 break;
638 }
639
640 buf->vbuf.m.offset = i * PAGE_ALIGN(size);
641 buf->vaddr = mem;
Laurent Pinchart7f5036d2014-03-08 09:38:38 -0300642 buf->paddr = dma;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300643 }
644
645 buf->vbuf.index = i;
646 buf->vbuf.length = size;
647 buf->vbuf.type = queue->type;
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300648 buf->vbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300649 buf->vbuf.field = V4L2_FIELD_NONE;
650 buf->vbuf.memory = memory;
651
652 buf->queue = queue;
653 init_waitqueue_head(&buf->wait);
654
655 queue->buffers[i] = buf;
656 }
657
658 if (i == 0)
659 return -ENOMEM;
660
661 queue->count = i;
662 return nbuffers;
663}
664
665/**
666 * omap3isp_video_queue_cleanup - Clean up the video buffers queue
667 * @queue: Video buffers queue
668 *
669 * Free all allocated resources and clean up the video buffers queue. The queue
670 * must not be busy (no ongoing video stream) and buffers must have been
671 * unmapped.
672 *
673 * Return 0 on success or -EBUSY if the queue is busy or buffers haven't been
674 * unmapped.
675 */
676int omap3isp_video_queue_cleanup(struct isp_video_queue *queue)
677{
678 return isp_video_queue_free(queue);
679}
680
681/**
682 * omap3isp_video_queue_init - Initialize the video buffers queue
683 * @queue: Video buffers queue
684 * @type: V4L2 buffer type (capture or output)
685 * @ops: Driver-specific queue operations
686 * @dev: Device used for DMA operations
687 * @bufsize: Size of the driver-specific buffer structure
688 *
689 * Initialize the video buffers queue with the supplied parameters.
690 *
691 * The queue type must be one of V4L2_BUF_TYPE_VIDEO_CAPTURE or
692 * V4L2_BUF_TYPE_VIDEO_OUTPUT. Other buffer types are not supported yet.
693 *
694 * Buffer objects will be allocated using the given buffer size to allow room
695 * for driver-specific fields. Driver-specific buffer structures must start
696 * with a struct isp_video_buffer field. Drivers with no driver-specific buffer
697 * structure must pass the size of the isp_video_buffer structure in the bufsize
698 * parameter.
699 *
700 * Return 0 on success.
701 */
702int omap3isp_video_queue_init(struct isp_video_queue *queue,
703 enum v4l2_buf_type type,
704 const struct isp_video_queue_operations *ops,
705 struct device *dev, unsigned int bufsize)
706{
707 INIT_LIST_HEAD(&queue->queue);
708 mutex_init(&queue->lock);
709 spin_lock_init(&queue->irqlock);
710
711 queue->type = type;
712 queue->ops = ops;
713 queue->dev = dev;
714 queue->bufsize = bufsize;
715
716 return 0;
717}
718
719/* -----------------------------------------------------------------------------
720 * V4L2 operations
721 */
722
723/**
724 * omap3isp_video_queue_reqbufs - Allocate video buffers memory
725 *
726 * This function is intended to be used as a VIDIOC_REQBUFS ioctl handler. It
727 * allocated video buffer objects and, for MMAP buffers, buffer memory.
728 *
729 * If the number of buffers is 0, all buffers are freed and the function returns
730 * without performing any allocation.
731 *
732 * If the number of buffers is not 0, currently allocated buffers (if any) are
733 * freed and the requested number of buffers are allocated. Depending on
734 * driver-specific requirements and on memory availability, a number of buffer
735 * smaller or bigger than requested can be allocated. This isn't considered as
736 * an error.
737 *
738 * Return 0 on success or one of the following error codes:
739 *
740 * -EINVAL if the buffer type or index are invalid
741 * -EBUSY if the queue is busy (streaming or buffers mapped)
742 * -ENOMEM if the buffers can't be allocated due to an out-of-memory condition
743 */
744int omap3isp_video_queue_reqbufs(struct isp_video_queue *queue,
745 struct v4l2_requestbuffers *rb)
746{
747 unsigned int nbuffers = rb->count;
748 unsigned int size;
749 int ret;
750
751 if (rb->type != queue->type)
752 return -EINVAL;
753
754 queue->ops->queue_prepare(queue, &nbuffers, &size);
755 if (size == 0)
756 return -EINVAL;
757
758 nbuffers = min_t(unsigned int, nbuffers, ISP_VIDEO_MAX_BUFFERS);
759
760 mutex_lock(&queue->lock);
761
762 ret = isp_video_queue_alloc(queue, nbuffers, size, rb->memory);
763 if (ret < 0)
764 goto done;
765
766 rb->count = ret;
767 ret = 0;
768
769done:
770 mutex_unlock(&queue->lock);
771 return ret;
772}
773
774/**
775 * omap3isp_video_queue_querybuf - Query the status of a buffer in a queue
776 *
777 * This function is intended to be used as a VIDIOC_QUERYBUF ioctl handler. It
778 * returns the status of a given video buffer.
779 *
780 * Return 0 on success or -EINVAL if the buffer type or index are invalid.
781 */
782int omap3isp_video_queue_querybuf(struct isp_video_queue *queue,
783 struct v4l2_buffer *vbuf)
784{
785 struct isp_video_buffer *buf;
786 int ret = 0;
787
788 if (vbuf->type != queue->type)
789 return -EINVAL;
790
791 mutex_lock(&queue->lock);
792
793 if (vbuf->index >= queue->count) {
794 ret = -EINVAL;
795 goto done;
796 }
797
798 buf = queue->buffers[vbuf->index];
799 isp_video_buffer_query(buf, vbuf);
800
801done:
802 mutex_unlock(&queue->lock);
803 return ret;
804}
805
806/**
807 * omap3isp_video_queue_qbuf - Queue a buffer
808 *
809 * This function is intended to be used as a VIDIOC_QBUF ioctl handler.
810 *
811 * The v4l2_buffer structure passed from userspace is first sanity tested. If
812 * sane, the buffer is then processed and added to the main queue and, if the
813 * queue is streaming, to the IRQ queue.
814 *
815 * Before being enqueued, USERPTR buffers are checked for address changes. If
816 * the buffer has a different userspace address, the old memory area is unlocked
817 * and the new memory area is locked.
818 */
819int omap3isp_video_queue_qbuf(struct isp_video_queue *queue,
820 struct v4l2_buffer *vbuf)
821{
822 struct isp_video_buffer *buf;
823 unsigned long flags;
824 int ret = -EINVAL;
825
826 if (vbuf->type != queue->type)
827 goto done;
828
829 mutex_lock(&queue->lock);
830
831 if (vbuf->index >= queue->count)
832 goto done;
833
834 buf = queue->buffers[vbuf->index];
835
836 if (vbuf->memory != buf->vbuf.memory)
837 goto done;
838
839 if (buf->state != ISP_BUF_STATE_IDLE)
840 goto done;
841
842 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
Michael Jones61e65612011-08-09 08:42:20 +0200843 vbuf->length < buf->vbuf.length)
844 goto done;
845
846 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300847 vbuf->m.userptr != buf->vbuf.m.userptr) {
848 isp_video_buffer_cleanup(buf);
849 buf->vbuf.m.userptr = vbuf->m.userptr;
850 buf->prepared = 0;
851 }
852
853 if (!buf->prepared) {
854 ret = isp_video_buffer_prepare(buf);
855 if (ret < 0)
856 goto done;
857 buf->prepared = 1;
858 }
859
860 isp_video_buffer_cache_sync(buf);
861
862 buf->state = ISP_BUF_STATE_QUEUED;
863 list_add_tail(&buf->stream, &queue->queue);
864
865 if (queue->streaming) {
866 spin_lock_irqsave(&queue->irqlock, flags);
867 queue->ops->buffer_queue(buf);
868 spin_unlock_irqrestore(&queue->irqlock, flags);
869 }
870
871 ret = 0;
872
873done:
874 mutex_unlock(&queue->lock);
875 return ret;
876}
877
878/**
879 * omap3isp_video_queue_dqbuf - Dequeue a buffer
880 *
881 * This function is intended to be used as a VIDIOC_DQBUF ioctl handler.
882 *
Michael Jonesb1e71f32012-06-27 12:06:57 -0300883 * Wait until a buffer is ready to be dequeued, remove it from the queue and
884 * copy its information to the v4l2_buffer structure.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300885 *
Michael Jonesb1e71f32012-06-27 12:06:57 -0300886 * If the nonblocking argument is not zero and no buffer is ready, return
887 * -EAGAIN immediately instead of waiting.
888 *
889 * If no buffer has been enqueued, or if the requested buffer type doesn't match
890 * the queue type, return -EINVAL.
Laurent Pinchartad614ac2011-02-12 18:05:06 -0300891 */
892int omap3isp_video_queue_dqbuf(struct isp_video_queue *queue,
893 struct v4l2_buffer *vbuf, int nonblocking)
894{
895 struct isp_video_buffer *buf;
896 int ret;
897
898 if (vbuf->type != queue->type)
899 return -EINVAL;
900
901 mutex_lock(&queue->lock);
902
903 if (list_empty(&queue->queue)) {
904 ret = -EINVAL;
905 goto done;
906 }
907
908 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
909 ret = isp_video_buffer_wait(buf, nonblocking);
910 if (ret < 0)
911 goto done;
912
913 list_del(&buf->stream);
914
915 isp_video_buffer_query(buf, vbuf);
916 buf->state = ISP_BUF_STATE_IDLE;
917 vbuf->flags &= ~V4L2_BUF_FLAG_QUEUED;
918
919done:
920 mutex_unlock(&queue->lock);
921 return ret;
922}
923
924/**
925 * omap3isp_video_queue_streamon - Start streaming
926 *
927 * This function is intended to be used as a VIDIOC_STREAMON ioctl handler. It
928 * starts streaming on the queue and calls the buffer_queue operation for all
929 * queued buffers.
930 *
931 * Return 0 on success.
932 */
933int omap3isp_video_queue_streamon(struct isp_video_queue *queue)
934{
935 struct isp_video_buffer *buf;
936 unsigned long flags;
937
938 mutex_lock(&queue->lock);
939
940 if (queue->streaming)
941 goto done;
942
943 queue->streaming = 1;
944
945 spin_lock_irqsave(&queue->irqlock, flags);
946 list_for_each_entry(buf, &queue->queue, stream)
947 queue->ops->buffer_queue(buf);
948 spin_unlock_irqrestore(&queue->irqlock, flags);
949
950done:
951 mutex_unlock(&queue->lock);
952 return 0;
953}
954
955/**
956 * omap3isp_video_queue_streamoff - Stop streaming
957 *
958 * This function is intended to be used as a VIDIOC_STREAMOFF ioctl handler. It
959 * stops streaming on the queue and wakes up all the buffers.
960 *
961 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
962 * delayed works before calling this function to make sure no buffer will be
963 * touched by the driver and/or hardware.
964 */
965void omap3isp_video_queue_streamoff(struct isp_video_queue *queue)
966{
967 struct isp_video_buffer *buf;
968 unsigned long flags;
969 unsigned int i;
970
971 mutex_lock(&queue->lock);
972
973 if (!queue->streaming)
974 goto done;
975
976 queue->streaming = 0;
977
978 spin_lock_irqsave(&queue->irqlock, flags);
979 for (i = 0; i < queue->count; ++i) {
980 buf = queue->buffers[i];
981
982 if (buf->state == ISP_BUF_STATE_ACTIVE)
983 wake_up(&buf->wait);
984
985 buf->state = ISP_BUF_STATE_IDLE;
986 }
987 spin_unlock_irqrestore(&queue->irqlock, flags);
988
989 INIT_LIST_HEAD(&queue->queue);
990
991done:
992 mutex_unlock(&queue->lock);
993}
994
995/**
996 * omap3isp_video_queue_discard_done - Discard all buffers marked as DONE
997 *
998 * This function is intended to be used with suspend/resume operations. It
999 * discards all 'done' buffers as they would be too old to be requested after
1000 * resume.
1001 *
1002 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1003 * delayed works before calling this function to make sure no buffer will be
1004 * touched by the driver and/or hardware.
1005 */
1006void omap3isp_video_queue_discard_done(struct isp_video_queue *queue)
1007{
1008 struct isp_video_buffer *buf;
1009 unsigned int i;
1010
1011 mutex_lock(&queue->lock);
1012
1013 if (!queue->streaming)
1014 goto done;
1015
1016 for (i = 0; i < queue->count; ++i) {
1017 buf = queue->buffers[i];
1018
1019 if (buf->state == ISP_BUF_STATE_DONE)
1020 buf->state = ISP_BUF_STATE_ERROR;
1021 }
1022
1023done:
1024 mutex_unlock(&queue->lock);
1025}
1026
1027static void isp_video_queue_vm_open(struct vm_area_struct *vma)
1028{
1029 struct isp_video_buffer *buf = vma->vm_private_data;
1030
1031 buf->vma_use_count++;
1032}
1033
1034static void isp_video_queue_vm_close(struct vm_area_struct *vma)
1035{
1036 struct isp_video_buffer *buf = vma->vm_private_data;
1037
1038 buf->vma_use_count--;
1039}
1040
1041static const struct vm_operations_struct isp_video_queue_vm_ops = {
1042 .open = isp_video_queue_vm_open,
1043 .close = isp_video_queue_vm_close,
1044};
1045
1046/**
1047 * omap3isp_video_queue_mmap - Map buffers to userspace
1048 *
1049 * This function is intended to be used as an mmap() file operation handler. It
1050 * maps a buffer to userspace based on the VMA offset.
1051 *
1052 * Only buffers of memory type MMAP are supported.
1053 */
1054int omap3isp_video_queue_mmap(struct isp_video_queue *queue,
1055 struct vm_area_struct *vma)
1056{
1057 struct isp_video_buffer *uninitialized_var(buf);
1058 unsigned long size;
1059 unsigned int i;
1060 int ret = 0;
1061
1062 mutex_lock(&queue->lock);
1063
1064 for (i = 0; i < queue->count; ++i) {
1065 buf = queue->buffers[i];
1066 if ((buf->vbuf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1067 break;
1068 }
1069
1070 if (i == queue->count) {
1071 ret = -EINVAL;
1072 goto done;
1073 }
1074
1075 size = vma->vm_end - vma->vm_start;
1076
1077 if (buf->vbuf.memory != V4L2_MEMORY_MMAP ||
1078 size != PAGE_ALIGN(buf->vbuf.length)) {
1079 ret = -EINVAL;
1080 goto done;
1081 }
1082
Laurent Pinchart7f5036d2014-03-08 09:38:38 -03001083 /* dma_mmap_coherent() uses vm_pgoff as an offset inside the buffer
1084 * while we used it to identify the buffer and want to map the whole
1085 * buffer.
1086 */
1087 vma->vm_pgoff = 0;
1088
1089 ret = dma_mmap_coherent(queue->dev, vma, buf->vaddr, buf->paddr, size);
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001090 if (ret < 0)
1091 goto done;
1092
Laurent Pinchart7f5036d2014-03-08 09:38:38 -03001093 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Laurent Pinchartad614ac2011-02-12 18:05:06 -03001094 vma->vm_ops = &isp_video_queue_vm_ops;
1095 vma->vm_private_data = buf;
1096 isp_video_queue_vm_open(vma);
1097
1098done:
1099 mutex_unlock(&queue->lock);
1100 return ret;
1101}
1102
1103/**
1104 * omap3isp_video_queue_poll - Poll video queue state
1105 *
1106 * This function is intended to be used as a poll() file operation handler. It
1107 * polls the state of the video buffer at the front of the queue and returns an
1108 * events mask.
1109 *
1110 * If no buffer is present at the front of the queue, POLLERR is returned.
1111 */
1112unsigned int omap3isp_video_queue_poll(struct isp_video_queue *queue,
1113 struct file *file, poll_table *wait)
1114{
1115 struct isp_video_buffer *buf;
1116 unsigned int mask = 0;
1117
1118 mutex_lock(&queue->lock);
1119 if (list_empty(&queue->queue)) {
1120 mask |= POLLERR;
1121 goto done;
1122 }
1123 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
1124
1125 poll_wait(file, &buf->wait, wait);
1126 if (buf->state == ISP_BUF_STATE_DONE ||
1127 buf->state == ISP_BUF_STATE_ERROR) {
1128 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1129 mask |= POLLIN | POLLRDNORM;
1130 else
1131 mask |= POLLOUT | POLLWRNORM;
1132 }
1133
1134done:
1135 mutex_unlock(&queue->lock);
1136 return mask;
1137}