ispqueue.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  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. #include <asm/cacheflush.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/mm.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/poll.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/vmalloc.h>
  34. #include "isp.h"
  35. #include "ispqueue.h"
  36. #include "ispvideo.h"
  37. /* -----------------------------------------------------------------------------
  38. * Video buffers management
  39. */
  40. /*
  41. * isp_video_buffer_cache_sync - Keep the buffers coherent between CPU and ISP
  42. *
  43. * The typical operation required here is Cache Invalidation across
  44. * the (user space) buffer address range. And this _must_ be done
  45. * at QBUF stage (and *only* at QBUF).
  46. *
  47. * We try to use optimal cache invalidation function:
  48. * - dmac_map_area:
  49. * - used when the number of pages are _low_.
  50. * - it becomes quite slow as the number of pages increase.
  51. * - for 648x492 viewfinder (150 pages) it takes 1.3 ms.
  52. * - for 5 Mpix buffer (2491 pages) it takes between 25-50 ms.
  53. *
  54. * - flush_cache_all:
  55. * - used when the number of pages are _high_.
  56. * - time taken in the range of 500-900 us.
  57. * - has a higher penalty but, as whole dcache + icache is invalidated
  58. */
  59. /*
  60. * FIXME: dmac_inv_range crashes randomly on the user space buffer
  61. * address. Fall back to flush_cache_all for now.
  62. */
  63. #define ISP_CACHE_FLUSH_PAGES_MAX 0
  64. static void isp_video_buffer_cache_sync(struct isp_video_buffer *buf)
  65. {
  66. if (buf->skip_cache)
  67. return;
  68. if (buf->vbuf.m.userptr == 0 || buf->npages == 0 ||
  69. buf->npages > ISP_CACHE_FLUSH_PAGES_MAX)
  70. flush_cache_all();
  71. else {
  72. dmac_map_area((void *)buf->vbuf.m.userptr, buf->vbuf.length,
  73. DMA_FROM_DEVICE);
  74. outer_inv_range(buf->vbuf.m.userptr,
  75. buf->vbuf.m.userptr + buf->vbuf.length);
  76. }
  77. }
  78. /*
  79. * isp_video_buffer_lock_vma - Prevent VMAs from being unmapped
  80. *
  81. * Lock the VMAs underlying the given buffer into memory. This avoids the
  82. * userspace buffer mapping from being swapped out, making VIPT cache handling
  83. * easier.
  84. *
  85. * Note that the pages will not be freed as the buffers have been locked to
  86. * memory using by a call to get_user_pages(), but the userspace mapping could
  87. * still disappear if the VMAs are not locked. This is caused by the memory
  88. * management code trying to be as lock-less as possible, which results in the
  89. * userspace mapping manager not finding out that the pages are locked under
  90. * some conditions.
  91. */
  92. static int isp_video_buffer_lock_vma(struct isp_video_buffer *buf, int lock)
  93. {
  94. struct vm_area_struct *vma;
  95. unsigned long start;
  96. unsigned long end;
  97. int ret = 0;
  98. if (buf->vbuf.memory == V4L2_MEMORY_MMAP)
  99. return 0;
  100. /* We can be called from workqueue context if the current task dies to
  101. * unlock the VMAs. In that case there's no current memory management
  102. * context so unlocking can't be performed, but the VMAs have been or
  103. * are getting destroyed anyway so it doesn't really matter.
  104. */
  105. if (!current || !current->mm)
  106. return lock ? -EINVAL : 0;
  107. start = buf->vbuf.m.userptr;
  108. end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
  109. down_write(&current->mm->mmap_sem);
  110. spin_lock(&current->mm->page_table_lock);
  111. do {
  112. vma = find_vma(current->mm, start);
  113. if (vma == NULL) {
  114. ret = -EFAULT;
  115. goto out;
  116. }
  117. if (lock)
  118. vma->vm_flags |= VM_LOCKED;
  119. else
  120. vma->vm_flags &= ~VM_LOCKED;
  121. start = vma->vm_end + 1;
  122. } while (vma->vm_end < end);
  123. if (lock)
  124. buf->vm_flags |= VM_LOCKED;
  125. else
  126. buf->vm_flags &= ~VM_LOCKED;
  127. out:
  128. spin_unlock(&current->mm->page_table_lock);
  129. up_write(&current->mm->mmap_sem);
  130. return ret;
  131. }
  132. /*
  133. * isp_video_buffer_cleanup - Release pages for a userspace VMA.
  134. *
  135. * Release pages locked by a call isp_video_buffer_prepare_user and free the
  136. * pages table.
  137. */
  138. static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
  139. {
  140. enum dma_data_direction direction;
  141. DEFINE_DMA_ATTRS(attrs);
  142. unsigned int i;
  143. if (buf->vbuf.memory == V4L2_MEMORY_USERPTR) {
  144. if (buf->skip_cache)
  145. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  146. direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
  147. ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  148. dma_unmap_sg_attrs(buf->queue->dev, buf->sgt.sgl,
  149. buf->sgt.orig_nents, direction, &attrs);
  150. sg_free_table(&buf->sgt);
  151. }
  152. if (buf->pages != NULL) {
  153. isp_video_buffer_lock_vma(buf, 0);
  154. for (i = 0; i < buf->npages; ++i)
  155. page_cache_release(buf->pages[i]);
  156. vfree(buf->pages);
  157. buf->pages = NULL;
  158. }
  159. buf->npages = 0;
  160. buf->skip_cache = false;
  161. }
  162. /*
  163. * isp_video_buffer_prepare_user - Prepare a userspace buffer.
  164. *
  165. * This function creates a scatter list with a 1:1 mapping for a userspace VMA.
  166. * The number of pages is first computed based on the buffer size, and pages are
  167. * then retrieved by a call to get_user_pages.
  168. *
  169. * Pages are pinned to memory by get_user_pages, making them available for DMA
  170. * transfers. However, due to memory management optimization, it seems the
  171. * get_user_pages doesn't guarantee that the pinned pages will not be written
  172. * to swap and removed from the userspace mapping(s). When this happens, a page
  173. * fault can be generated when accessing those unmapped pages.
  174. *
  175. * If the fault is triggered by a page table walk caused by VIPT cache
  176. * management operations, the page fault handler might oops if the MM semaphore
  177. * is held, as it can't handle kernel page faults in that case. To fix that, a
  178. * fixup entry needs to be added to the cache management code, or the userspace
  179. * VMA must be locked to avoid removing pages from the userspace mapping in the
  180. * first place.
  181. *
  182. * If the number of pages retrieved is smaller than the number required by the
  183. * buffer size, the function returns -EFAULT.
  184. */
  185. static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf)
  186. {
  187. unsigned int offset;
  188. unsigned long data;
  189. unsigned int first;
  190. unsigned int last;
  191. int ret;
  192. data = buf->vbuf.m.userptr;
  193. first = (data & PAGE_MASK) >> PAGE_SHIFT;
  194. last = ((data + buf->vbuf.length - 1) & PAGE_MASK) >> PAGE_SHIFT;
  195. offset = data & ~PAGE_MASK;
  196. buf->npages = last - first + 1;
  197. buf->pages = vmalloc(buf->npages * sizeof(buf->pages[0]));
  198. if (buf->pages == NULL)
  199. return -ENOMEM;
  200. down_read(&current->mm->mmap_sem);
  201. ret = get_user_pages(current, current->mm, data & PAGE_MASK,
  202. buf->npages,
  203. buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
  204. buf->pages, NULL);
  205. up_read(&current->mm->mmap_sem);
  206. if (ret != buf->npages) {
  207. buf->npages = ret < 0 ? 0 : ret;
  208. return -EFAULT;
  209. }
  210. ret = isp_video_buffer_lock_vma(buf, 1);
  211. if (ret < 0)
  212. return ret;
  213. ret = sg_alloc_table_from_pages(&buf->sgt, buf->pages, buf->npages,
  214. offset, buf->vbuf.length, GFP_KERNEL);
  215. if (ret < 0)
  216. return ret;
  217. return 0;
  218. }
  219. /*
  220. * isp_video_buffer_prepare_pfnmap - Prepare a VM_PFNMAP userspace buffer
  221. *
  222. * Userspace VM_PFNMAP buffers are supported only if they are contiguous in
  223. * memory and if they span a single VMA. Start by validating the user pointer to
  224. * make sure it fulfils that condition, and then build a scatter list of
  225. * physically contiguous pages starting at the buffer memory physical address.
  226. *
  227. * Return 0 on success, -EFAULT if the buffer isn't valid or -ENOMEM if memory
  228. * can't be allocated.
  229. */
  230. static int isp_video_buffer_prepare_pfnmap(struct isp_video_buffer *buf)
  231. {
  232. struct vm_area_struct *vma;
  233. struct scatterlist *sg;
  234. unsigned long prev_pfn;
  235. unsigned long this_pfn;
  236. unsigned long start;
  237. unsigned int offset;
  238. unsigned long end;
  239. unsigned long pfn;
  240. unsigned int i;
  241. int ret = 0;
  242. start = buf->vbuf.m.userptr;
  243. end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
  244. offset = start & ~PAGE_MASK;
  245. buf->npages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
  246. buf->pages = NULL;
  247. down_read(&current->mm->mmap_sem);
  248. vma = find_vma(current->mm, start);
  249. if (vma == NULL || vma->vm_end < end) {
  250. ret = -EFAULT;
  251. goto unlock;
  252. }
  253. for (prev_pfn = 0; start <= end; start += PAGE_SIZE) {
  254. ret = follow_pfn(vma, start, &this_pfn);
  255. if (ret < 0)
  256. goto unlock;
  257. if (prev_pfn == 0)
  258. pfn = this_pfn;
  259. else if (this_pfn != prev_pfn + 1) {
  260. ret = -EFAULT;
  261. goto unlock;
  262. }
  263. prev_pfn = this_pfn;
  264. }
  265. unlock:
  266. up_read(&current->mm->mmap_sem);
  267. if (ret < 0)
  268. return ret;
  269. ret = sg_alloc_table(&buf->sgt, buf->npages, GFP_KERNEL);
  270. if (ret < 0)
  271. return ret;
  272. for (sg = buf->sgt.sgl, i = 0; i < buf->npages; ++i, ++pfn) {
  273. sg_set_page(sg, pfn_to_page(pfn), PAGE_SIZE - offset, offset);
  274. sg = sg_next(sg);
  275. offset = 0;
  276. }
  277. return 0;
  278. }
  279. /*
  280. * isp_video_buffer_prepare_vm_flags - Get VMA flags for a userspace address
  281. *
  282. * This function locates the VMAs for the buffer's userspace address and checks
  283. * that their flags match. The only flag that we need to care for at the moment
  284. * is VM_PFNMAP.
  285. *
  286. * The buffer vm_flags field is set to the first VMA flags.
  287. *
  288. * Return -EFAULT if no VMA can be found for part of the buffer, or if the VMAs
  289. * have incompatible flags.
  290. */
  291. static int isp_video_buffer_prepare_vm_flags(struct isp_video_buffer *buf)
  292. {
  293. struct vm_area_struct *vma;
  294. pgprot_t uninitialized_var(vm_page_prot);
  295. unsigned long start;
  296. unsigned long end;
  297. int ret = -EFAULT;
  298. start = buf->vbuf.m.userptr;
  299. end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
  300. down_read(&current->mm->mmap_sem);
  301. do {
  302. vma = find_vma(current->mm, start);
  303. if (vma == NULL)
  304. goto done;
  305. if (start == buf->vbuf.m.userptr) {
  306. buf->vm_flags = vma->vm_flags;
  307. vm_page_prot = vma->vm_page_prot;
  308. }
  309. if ((buf->vm_flags ^ vma->vm_flags) & VM_PFNMAP)
  310. goto done;
  311. if (vm_page_prot != vma->vm_page_prot)
  312. goto done;
  313. start = vma->vm_end + 1;
  314. } while (vma->vm_end < end);
  315. /* Skip cache management to enhance performances for non-cached or
  316. * write-combining buffers.
  317. */
  318. if (vm_page_prot == pgprot_noncached(vm_page_prot) ||
  319. vm_page_prot == pgprot_writecombine(vm_page_prot))
  320. buf->skip_cache = true;
  321. ret = 0;
  322. done:
  323. up_read(&current->mm->mmap_sem);
  324. return ret;
  325. }
  326. /*
  327. * isp_video_buffer_prepare - Make a buffer ready for operation
  328. *
  329. * Preparing a buffer involves:
  330. *
  331. * - validating VMAs (userspace buffers only)
  332. * - locking pages and VMAs into memory (userspace buffers only)
  333. * - building page and scatter-gather lists (userspace buffers only)
  334. * - mapping buffers for DMA operation
  335. * - performing driver-specific preparation
  336. *
  337. * The function must be called in userspace context with a valid mm context
  338. * (this excludes cleanup paths such as sys_close when the userspace process
  339. * segfaults).
  340. */
  341. static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
  342. {
  343. enum dma_data_direction direction;
  344. DEFINE_DMA_ATTRS(attrs);
  345. int ret;
  346. switch (buf->vbuf.memory) {
  347. case V4L2_MEMORY_MMAP:
  348. ret = 0;
  349. break;
  350. case V4L2_MEMORY_USERPTR:
  351. ret = isp_video_buffer_prepare_vm_flags(buf);
  352. if (ret < 0)
  353. return ret;
  354. if (buf->vm_flags & VM_PFNMAP)
  355. ret = isp_video_buffer_prepare_pfnmap(buf);
  356. else
  357. ret = isp_video_buffer_prepare_user(buf);
  358. if (ret < 0)
  359. goto done;
  360. if (buf->skip_cache)
  361. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  362. direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
  363. ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  364. ret = dma_map_sg_attrs(buf->queue->dev, buf->sgt.sgl,
  365. buf->sgt.orig_nents, direction, &attrs);
  366. if (ret <= 0) {
  367. ret = -EFAULT;
  368. goto done;
  369. }
  370. buf->dma = sg_dma_address(buf->sgt.sgl);
  371. break;
  372. default:
  373. return -EINVAL;
  374. }
  375. if (!IS_ALIGNED(buf->dma, 32)) {
  376. dev_dbg(buf->queue->dev,
  377. "Buffer address must be aligned to 32 bytes boundary.\n");
  378. ret = -EINVAL;
  379. goto done;
  380. }
  381. if (buf->queue->ops->buffer_prepare)
  382. ret = buf->queue->ops->buffer_prepare(buf);
  383. done:
  384. if (ret < 0) {
  385. isp_video_buffer_cleanup(buf);
  386. return ret;
  387. }
  388. return ret;
  389. }
  390. /*
  391. * isp_video_queue_query - Query the status of a given buffer
  392. *
  393. * Locking: must be called with the queue lock held.
  394. */
  395. static void isp_video_buffer_query(struct isp_video_buffer *buf,
  396. struct v4l2_buffer *vbuf)
  397. {
  398. memcpy(vbuf, &buf->vbuf, sizeof(*vbuf));
  399. if (buf->vma_use_count)
  400. vbuf->flags |= V4L2_BUF_FLAG_MAPPED;
  401. switch (buf->state) {
  402. case ISP_BUF_STATE_ERROR:
  403. vbuf->flags |= V4L2_BUF_FLAG_ERROR;
  404. /* Fallthrough */
  405. case ISP_BUF_STATE_DONE:
  406. vbuf->flags |= V4L2_BUF_FLAG_DONE;
  407. break;
  408. case ISP_BUF_STATE_QUEUED:
  409. case ISP_BUF_STATE_ACTIVE:
  410. vbuf->flags |= V4L2_BUF_FLAG_QUEUED;
  411. break;
  412. case ISP_BUF_STATE_IDLE:
  413. default:
  414. break;
  415. }
  416. }
  417. /*
  418. * isp_video_buffer_wait - Wait for a buffer to be ready
  419. *
  420. * In non-blocking mode, return immediately with 0 if the buffer is ready or
  421. * -EAGAIN if the buffer is in the QUEUED or ACTIVE state.
  422. *
  423. * In blocking mode, wait (interruptibly but with no timeout) on the buffer wait
  424. * queue using the same condition.
  425. */
  426. static int isp_video_buffer_wait(struct isp_video_buffer *buf, int nonblocking)
  427. {
  428. if (nonblocking) {
  429. return (buf->state != ISP_BUF_STATE_QUEUED &&
  430. buf->state != ISP_BUF_STATE_ACTIVE)
  431. ? 0 : -EAGAIN;
  432. }
  433. return wait_event_interruptible(buf->wait,
  434. buf->state != ISP_BUF_STATE_QUEUED &&
  435. buf->state != ISP_BUF_STATE_ACTIVE);
  436. }
  437. /* -----------------------------------------------------------------------------
  438. * Queue management
  439. */
  440. /*
  441. * isp_video_queue_free - Free video buffers memory
  442. *
  443. * Buffers can only be freed if the queue isn't streaming and if no buffer is
  444. * mapped to userspace. Return -EBUSY if those conditions aren't satisfied.
  445. *
  446. * This function must be called with the queue lock held.
  447. */
  448. static int isp_video_queue_free(struct isp_video_queue *queue)
  449. {
  450. unsigned int i;
  451. if (queue->streaming)
  452. return -EBUSY;
  453. for (i = 0; i < queue->count; ++i) {
  454. if (queue->buffers[i]->vma_use_count != 0)
  455. return -EBUSY;
  456. }
  457. for (i = 0; i < queue->count; ++i) {
  458. struct isp_video_buffer *buf = queue->buffers[i];
  459. isp_video_buffer_cleanup(buf);
  460. if (buf->vaddr) {
  461. dma_free_coherent(queue->dev,
  462. PAGE_ALIGN(buf->vbuf.length),
  463. buf->vaddr, buf->dma);
  464. buf->vaddr = NULL;
  465. }
  466. kfree(buf);
  467. queue->buffers[i] = NULL;
  468. }
  469. INIT_LIST_HEAD(&queue->queue);
  470. queue->count = 0;
  471. return 0;
  472. }
  473. /*
  474. * isp_video_queue_alloc - Allocate video buffers memory
  475. *
  476. * This function must be called with the queue lock held.
  477. */
  478. static int isp_video_queue_alloc(struct isp_video_queue *queue,
  479. unsigned int nbuffers,
  480. unsigned int size, enum v4l2_memory memory)
  481. {
  482. struct isp_video_buffer *buf;
  483. dma_addr_t dma;
  484. unsigned int i;
  485. void *mem;
  486. int ret;
  487. /* Start by freeing the buffers. */
  488. ret = isp_video_queue_free(queue);
  489. if (ret < 0)
  490. return ret;
  491. /* Bail out if no buffers should be allocated. */
  492. if (nbuffers == 0)
  493. return 0;
  494. /* Initialize the allocated buffers. */
  495. for (i = 0; i < nbuffers; ++i) {
  496. buf = kzalloc(queue->bufsize, GFP_KERNEL);
  497. if (buf == NULL)
  498. break;
  499. if (memory == V4L2_MEMORY_MMAP) {
  500. /* Allocate video buffers memory for mmap mode. Align
  501. * the size to the page size.
  502. */
  503. mem = dma_alloc_coherent(queue->dev, PAGE_ALIGN(size),
  504. &dma, GFP_KERNEL);
  505. if (mem == NULL) {
  506. kfree(buf);
  507. break;
  508. }
  509. buf->vbuf.m.offset = i * PAGE_ALIGN(size);
  510. buf->vaddr = mem;
  511. buf->dma = dma;
  512. }
  513. buf->vbuf.index = i;
  514. buf->vbuf.length = size;
  515. buf->vbuf.type = queue->type;
  516. buf->vbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  517. buf->vbuf.field = V4L2_FIELD_NONE;
  518. buf->vbuf.memory = memory;
  519. buf->queue = queue;
  520. init_waitqueue_head(&buf->wait);
  521. queue->buffers[i] = buf;
  522. }
  523. if (i == 0)
  524. return -ENOMEM;
  525. queue->count = i;
  526. return nbuffers;
  527. }
  528. /**
  529. * omap3isp_video_queue_cleanup - Clean up the video buffers queue
  530. * @queue: Video buffers queue
  531. *
  532. * Free all allocated resources and clean up the video buffers queue. The queue
  533. * must not be busy (no ongoing video stream) and buffers must have been
  534. * unmapped.
  535. *
  536. * Return 0 on success or -EBUSY if the queue is busy or buffers haven't been
  537. * unmapped.
  538. */
  539. int omap3isp_video_queue_cleanup(struct isp_video_queue *queue)
  540. {
  541. return isp_video_queue_free(queue);
  542. }
  543. /**
  544. * omap3isp_video_queue_init - Initialize the video buffers queue
  545. * @queue: Video buffers queue
  546. * @type: V4L2 buffer type (capture or output)
  547. * @ops: Driver-specific queue operations
  548. * @dev: Device used for DMA operations
  549. * @bufsize: Size of the driver-specific buffer structure
  550. *
  551. * Initialize the video buffers queue with the supplied parameters.
  552. *
  553. * The queue type must be one of V4L2_BUF_TYPE_VIDEO_CAPTURE or
  554. * V4L2_BUF_TYPE_VIDEO_OUTPUT. Other buffer types are not supported yet.
  555. *
  556. * Buffer objects will be allocated using the given buffer size to allow room
  557. * for driver-specific fields. Driver-specific buffer structures must start
  558. * with a struct isp_video_buffer field. Drivers with no driver-specific buffer
  559. * structure must pass the size of the isp_video_buffer structure in the bufsize
  560. * parameter.
  561. *
  562. * Return 0 on success.
  563. */
  564. int omap3isp_video_queue_init(struct isp_video_queue *queue,
  565. enum v4l2_buf_type type,
  566. const struct isp_video_queue_operations *ops,
  567. struct device *dev, unsigned int bufsize)
  568. {
  569. INIT_LIST_HEAD(&queue->queue);
  570. queue->type = type;
  571. queue->ops = ops;
  572. queue->dev = dev;
  573. queue->bufsize = bufsize;
  574. return 0;
  575. }
  576. /* -----------------------------------------------------------------------------
  577. * V4L2 operations
  578. */
  579. /**
  580. * omap3isp_video_queue_reqbufs - Allocate video buffers memory
  581. *
  582. * This function is intended to be used as a VIDIOC_REQBUFS ioctl handler. It
  583. * allocated video buffer objects and, for MMAP buffers, buffer memory.
  584. *
  585. * If the number of buffers is 0, all buffers are freed and the function returns
  586. * without performing any allocation.
  587. *
  588. * If the number of buffers is not 0, currently allocated buffers (if any) are
  589. * freed and the requested number of buffers are allocated. Depending on
  590. * driver-specific requirements and on memory availability, a number of buffer
  591. * smaller or bigger than requested can be allocated. This isn't considered as
  592. * an error.
  593. *
  594. * Return 0 on success or one of the following error codes:
  595. *
  596. * -EINVAL if the buffer type or index are invalid
  597. * -EBUSY if the queue is busy (streaming or buffers mapped)
  598. * -ENOMEM if the buffers can't be allocated due to an out-of-memory condition
  599. */
  600. int omap3isp_video_queue_reqbufs(struct isp_video_queue *queue,
  601. struct v4l2_requestbuffers *rb)
  602. {
  603. unsigned int nbuffers = rb->count;
  604. unsigned int size;
  605. int ret;
  606. if (rb->type != queue->type)
  607. return -EINVAL;
  608. queue->ops->queue_prepare(queue, &nbuffers, &size);
  609. if (size == 0)
  610. return -EINVAL;
  611. nbuffers = min_t(unsigned int, nbuffers, ISP_VIDEO_MAX_BUFFERS);
  612. ret = isp_video_queue_alloc(queue, nbuffers, size, rb->memory);
  613. if (ret < 0)
  614. return ret;
  615. rb->count = ret;
  616. return 0;
  617. }
  618. /**
  619. * omap3isp_video_queue_querybuf - Query the status of a buffer in a queue
  620. *
  621. * This function is intended to be used as a VIDIOC_QUERYBUF ioctl handler. It
  622. * returns the status of a given video buffer.
  623. *
  624. * Return 0 on success or -EINVAL if the buffer type or index are invalid.
  625. */
  626. int omap3isp_video_queue_querybuf(struct isp_video_queue *queue,
  627. struct v4l2_buffer *vbuf)
  628. {
  629. struct isp_video_buffer *buf;
  630. if (vbuf->type != queue->type)
  631. return -EINVAL;
  632. if (vbuf->index >= queue->count)
  633. return -EINVAL;
  634. buf = queue->buffers[vbuf->index];
  635. isp_video_buffer_query(buf, vbuf);
  636. return 0;
  637. }
  638. /**
  639. * omap3isp_video_queue_qbuf - Queue a buffer
  640. *
  641. * This function is intended to be used as a VIDIOC_QBUF ioctl handler.
  642. *
  643. * The v4l2_buffer structure passed from userspace is first sanity tested. If
  644. * sane, the buffer is then processed and added to the main queue and, if the
  645. * queue is streaming, to the IRQ queue.
  646. *
  647. * Before being enqueued, USERPTR buffers are checked for address changes. If
  648. * the buffer has a different userspace address, the old memory area is unlocked
  649. * and the new memory area is locked.
  650. */
  651. int omap3isp_video_queue_qbuf(struct isp_video_queue *queue,
  652. struct v4l2_buffer *vbuf)
  653. {
  654. struct isp_video_buffer *buf;
  655. int ret;
  656. if (vbuf->type != queue->type)
  657. return -EINVAL;
  658. if (vbuf->index >= queue->count)
  659. return -EINVAL;
  660. buf = queue->buffers[vbuf->index];
  661. if (vbuf->memory != buf->vbuf.memory)
  662. return -EINVAL;
  663. if (buf->state != ISP_BUF_STATE_IDLE)
  664. return -EINVAL;
  665. if (vbuf->memory == V4L2_MEMORY_USERPTR &&
  666. vbuf->length < buf->vbuf.length)
  667. return -EINVAL;
  668. if (vbuf->memory == V4L2_MEMORY_USERPTR &&
  669. vbuf->m.userptr != buf->vbuf.m.userptr) {
  670. isp_video_buffer_cleanup(buf);
  671. buf->vbuf.m.userptr = vbuf->m.userptr;
  672. buf->prepared = 0;
  673. }
  674. if (!buf->prepared) {
  675. ret = isp_video_buffer_prepare(buf);
  676. if (ret < 0)
  677. return ret;
  678. buf->prepared = 1;
  679. }
  680. isp_video_buffer_cache_sync(buf);
  681. buf->state = ISP_BUF_STATE_QUEUED;
  682. list_add_tail(&buf->stream, &queue->queue);
  683. if (queue->streaming)
  684. queue->ops->buffer_queue(buf);
  685. return 0;
  686. }
  687. /**
  688. * omap3isp_video_queue_dqbuf - Dequeue a buffer
  689. *
  690. * This function is intended to be used as a VIDIOC_DQBUF ioctl handler.
  691. *
  692. * Wait until a buffer is ready to be dequeued, remove it from the queue and
  693. * copy its information to the v4l2_buffer structure.
  694. *
  695. * If the nonblocking argument is not zero and no buffer is ready, return
  696. * -EAGAIN immediately instead of waiting.
  697. *
  698. * If no buffer has been enqueued, or if the requested buffer type doesn't match
  699. * the queue type, return -EINVAL.
  700. */
  701. int omap3isp_video_queue_dqbuf(struct isp_video_queue *queue,
  702. struct v4l2_buffer *vbuf, int nonblocking)
  703. {
  704. struct isp_video_buffer *buf;
  705. int ret;
  706. if (vbuf->type != queue->type)
  707. return -EINVAL;
  708. if (list_empty(&queue->queue))
  709. return -EINVAL;
  710. buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
  711. ret = isp_video_buffer_wait(buf, nonblocking);
  712. if (ret < 0)
  713. return ret;
  714. list_del(&buf->stream);
  715. isp_video_buffer_query(buf, vbuf);
  716. buf->state = ISP_BUF_STATE_IDLE;
  717. vbuf->flags &= ~V4L2_BUF_FLAG_QUEUED;
  718. return 0;
  719. }
  720. /**
  721. * omap3isp_video_queue_streamon - Start streaming
  722. *
  723. * This function is intended to be used as a VIDIOC_STREAMON ioctl handler. It
  724. * starts streaming on the queue and calls the buffer_queue operation for all
  725. * queued buffers.
  726. *
  727. * Return 0 on success.
  728. */
  729. int omap3isp_video_queue_streamon(struct isp_video_queue *queue)
  730. {
  731. struct isp_video_buffer *buf;
  732. if (queue->streaming)
  733. return 0;
  734. queue->streaming = 1;
  735. list_for_each_entry(buf, &queue->queue, stream)
  736. queue->ops->buffer_queue(buf);
  737. return 0;
  738. }
  739. /**
  740. * omap3isp_video_queue_streamoff - Stop streaming
  741. *
  742. * This function is intended to be used as a VIDIOC_STREAMOFF ioctl handler. It
  743. * stops streaming on the queue and wakes up all the buffers.
  744. *
  745. * Drivers must stop the hardware and synchronize with interrupt handlers and/or
  746. * delayed works before calling this function to make sure no buffer will be
  747. * touched by the driver and/or hardware.
  748. */
  749. void omap3isp_video_queue_streamoff(struct isp_video_queue *queue)
  750. {
  751. struct isp_video_buffer *buf;
  752. unsigned int i;
  753. if (!queue->streaming)
  754. return;
  755. queue->streaming = 0;
  756. for (i = 0; i < queue->count; ++i) {
  757. buf = queue->buffers[i];
  758. if (buf->state == ISP_BUF_STATE_ACTIVE)
  759. wake_up(&buf->wait);
  760. buf->state = ISP_BUF_STATE_IDLE;
  761. }
  762. INIT_LIST_HEAD(&queue->queue);
  763. }
  764. /**
  765. * omap3isp_video_queue_discard_done - Discard all buffers marked as DONE
  766. *
  767. * This function is intended to be used with suspend/resume operations. It
  768. * discards all 'done' buffers as they would be too old to be requested after
  769. * resume.
  770. *
  771. * Drivers must stop the hardware and synchronize with interrupt handlers and/or
  772. * delayed works before calling this function to make sure no buffer will be
  773. * touched by the driver and/or hardware.
  774. */
  775. void omap3isp_video_queue_discard_done(struct isp_video_queue *queue)
  776. {
  777. struct isp_video_buffer *buf;
  778. unsigned int i;
  779. if (!queue->streaming)
  780. return;
  781. for (i = 0; i < queue->count; ++i) {
  782. buf = queue->buffers[i];
  783. if (buf->state == ISP_BUF_STATE_DONE)
  784. buf->state = ISP_BUF_STATE_ERROR;
  785. }
  786. }
  787. static void isp_video_queue_vm_open(struct vm_area_struct *vma)
  788. {
  789. struct isp_video_buffer *buf = vma->vm_private_data;
  790. buf->vma_use_count++;
  791. }
  792. static void isp_video_queue_vm_close(struct vm_area_struct *vma)
  793. {
  794. struct isp_video_buffer *buf = vma->vm_private_data;
  795. buf->vma_use_count--;
  796. }
  797. static const struct vm_operations_struct isp_video_queue_vm_ops = {
  798. .open = isp_video_queue_vm_open,
  799. .close = isp_video_queue_vm_close,
  800. };
  801. /**
  802. * omap3isp_video_queue_mmap - Map buffers to userspace
  803. *
  804. * This function is intended to be used as an mmap() file operation handler. It
  805. * maps a buffer to userspace based on the VMA offset.
  806. *
  807. * Only buffers of memory type MMAP are supported.
  808. */
  809. int omap3isp_video_queue_mmap(struct isp_video_queue *queue,
  810. struct vm_area_struct *vma)
  811. {
  812. struct isp_video_buffer *uninitialized_var(buf);
  813. unsigned long size;
  814. unsigned int i;
  815. int ret = 0;
  816. for (i = 0; i < queue->count; ++i) {
  817. buf = queue->buffers[i];
  818. if ((buf->vbuf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  819. break;
  820. }
  821. if (i == queue->count)
  822. return -EINVAL;
  823. size = vma->vm_end - vma->vm_start;
  824. if (buf->vbuf.memory != V4L2_MEMORY_MMAP ||
  825. size != PAGE_ALIGN(buf->vbuf.length))
  826. return -EINVAL;
  827. /* dma_mmap_coherent() uses vm_pgoff as an offset inside the buffer
  828. * while we used it to identify the buffer and want to map the whole
  829. * buffer.
  830. */
  831. vma->vm_pgoff = 0;
  832. ret = dma_mmap_coherent(queue->dev, vma, buf->vaddr, buf->dma, size);
  833. if (ret < 0)
  834. return ret;
  835. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  836. vma->vm_ops = &isp_video_queue_vm_ops;
  837. vma->vm_private_data = buf;
  838. isp_video_queue_vm_open(vma);
  839. return 0;
  840. }
  841. /**
  842. * omap3isp_video_queue_poll - Poll video queue state
  843. *
  844. * This function is intended to be used as a poll() file operation handler. It
  845. * polls the state of the video buffer at the front of the queue and returns an
  846. * events mask.
  847. *
  848. * If no buffer is present at the front of the queue, POLLERR is returned.
  849. */
  850. unsigned int omap3isp_video_queue_poll(struct isp_video_queue *queue,
  851. struct file *file, poll_table *wait)
  852. {
  853. struct isp_video_buffer *buf;
  854. unsigned int mask = 0;
  855. if (list_empty(&queue->queue)) {
  856. mask |= POLLERR;
  857. goto done;
  858. }
  859. buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
  860. poll_wait(file, &buf->wait, wait);
  861. if (buf->state == ISP_BUF_STATE_DONE ||
  862. buf->state == ISP_BUF_STATE_ERROR) {
  863. if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  864. mask |= POLLIN | POLLRDNORM;
  865. else
  866. mask |= POLLOUT | POLLWRNORM;
  867. }
  868. done:
  869. return mask;
  870. }