PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/video/videobuf-dma-sg.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 632 lines | 464 code | 105 blank | 63 comment | 81 complexity | 33867bf56d7d891a956ff5bb513808b8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * helper functions for SG DMA video4linux capture buffers
  3. *
  4. * The functions expect the hardware being able to scatter gather
  5. * (i.e. the buffers are not linear in physical memory, but fragmented
  6. * into PAGE_SIZE chunks). They also assume the driver does not need
  7. * to touch the video data.
  8. *
  9. * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
  10. *
  11. * Highly based on video-buf written originally by:
  12. * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
  13. * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
  14. * (c) 2006 Ted Walther and John Sokol
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/scatterlist.h>
  30. #include <asm/page.h>
  31. #include <asm/pgtable.h>
  32. #include <media/videobuf-dma-sg.h>
  33. #define MAGIC_DMABUF 0x19721112
  34. #define MAGIC_SG_MEM 0x17890714
  35. #define MAGIC_CHECK(is, should) \
  36. if (unlikely((is) != (should))) { \
  37. printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \
  38. is, should); \
  39. BUG(); \
  40. }
  41. static int debug;
  42. module_param(debug, int, 0644);
  43. MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
  44. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  45. MODULE_LICENSE("GPL");
  46. #define dprintk(level, fmt, arg...) \
  47. if (debug >= level) \
  48. printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
  49. /* --------------------------------------------------------------------- */
  50. /*
  51. * Return a scatterlist for some page-aligned vmalloc()'ed memory
  52. * block (NULL on errors). Memory for the scatterlist is allocated
  53. * using kmalloc. The caller must free the memory.
  54. */
  55. static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt,
  56. int nr_pages)
  57. {
  58. struct scatterlist *sglist;
  59. struct page *pg;
  60. int i;
  61. sglist = vzalloc(nr_pages * sizeof(*sglist));
  62. if (NULL == sglist)
  63. return NULL;
  64. sg_init_table(sglist, nr_pages);
  65. for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
  66. pg = vmalloc_to_page(virt);
  67. if (NULL == pg)
  68. goto err;
  69. BUG_ON(PageHighMem(pg));
  70. sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
  71. }
  72. return sglist;
  73. err:
  74. vfree(sglist);
  75. return NULL;
  76. }
  77. /*
  78. * Return a scatterlist for a an array of userpages (NULL on errors).
  79. * Memory for the scatterlist is allocated using kmalloc. The caller
  80. * must free the memory.
  81. */
  82. static struct scatterlist *videobuf_pages_to_sg(struct page **pages,
  83. int nr_pages, int offset, size_t size)
  84. {
  85. struct scatterlist *sglist;
  86. int i;
  87. if (NULL == pages[0])
  88. return NULL;
  89. sglist = vmalloc(nr_pages * sizeof(*sglist));
  90. if (NULL == sglist)
  91. return NULL;
  92. sg_init_table(sglist, nr_pages);
  93. if (PageHighMem(pages[0]))
  94. /* DMA to highmem pages might not work */
  95. goto highmem;
  96. sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
  97. size -= PAGE_SIZE - offset;
  98. for (i = 1; i < nr_pages; i++) {
  99. if (NULL == pages[i])
  100. goto nopage;
  101. if (PageHighMem(pages[i]))
  102. goto highmem;
  103. sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0);
  104. size -= min_t(size_t, PAGE_SIZE, size);
  105. }
  106. return sglist;
  107. nopage:
  108. dprintk(2, "sgl: oops - no page\n");
  109. vfree(sglist);
  110. return NULL;
  111. highmem:
  112. dprintk(2, "sgl: oops - highmem page\n");
  113. vfree(sglist);
  114. return NULL;
  115. }
  116. /* --------------------------------------------------------------------- */
  117. struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
  118. {
  119. struct videobuf_dma_sg_memory *mem = buf->priv;
  120. BUG_ON(!mem);
  121. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  122. return &mem->dma;
  123. }
  124. EXPORT_SYMBOL_GPL(videobuf_to_dma);
  125. void videobuf_dma_init(struct videobuf_dmabuf *dma)
  126. {
  127. memset(dma, 0, sizeof(*dma));
  128. dma->magic = MAGIC_DMABUF;
  129. }
  130. EXPORT_SYMBOL_GPL(videobuf_dma_init);
  131. static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
  132. int direction, unsigned long data, unsigned long size)
  133. {
  134. unsigned long first, last;
  135. int err, rw = 0;
  136. dma->direction = direction;
  137. switch (dma->direction) {
  138. case DMA_FROM_DEVICE:
  139. rw = READ;
  140. break;
  141. case DMA_TO_DEVICE:
  142. rw = WRITE;
  143. break;
  144. default:
  145. BUG();
  146. }
  147. first = (data & PAGE_MASK) >> PAGE_SHIFT;
  148. last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
  149. dma->offset = data & ~PAGE_MASK;
  150. dma->size = size;
  151. dma->nr_pages = last-first+1;
  152. dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL);
  153. if (NULL == dma->pages)
  154. return -ENOMEM;
  155. dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
  156. data, size, dma->nr_pages);
  157. err = get_user_pages(current, current->mm,
  158. data & PAGE_MASK, dma->nr_pages,
  159. rw == READ, 1, /* force */
  160. dma->pages, NULL);
  161. if (err != dma->nr_pages) {
  162. dma->nr_pages = (err >= 0) ? err : 0;
  163. dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages);
  164. return err < 0 ? err : -EINVAL;
  165. }
  166. return 0;
  167. }
  168. int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
  169. unsigned long data, unsigned long size)
  170. {
  171. int ret;
  172. down_read(&current->mm->mmap_sem);
  173. ret = videobuf_dma_init_user_locked(dma, direction, data, size);
  174. up_read(&current->mm->mmap_sem);
  175. return ret;
  176. }
  177. EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
  178. int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
  179. int nr_pages)
  180. {
  181. dprintk(1, "init kernel [%d pages]\n", nr_pages);
  182. dma->direction = direction;
  183. dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
  184. if (NULL == dma->vaddr) {
  185. dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
  186. return -ENOMEM;
  187. }
  188. dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
  189. (unsigned long)dma->vaddr,
  190. nr_pages << PAGE_SHIFT);
  191. memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
  192. dma->nr_pages = nr_pages;
  193. return 0;
  194. }
  195. EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
  196. int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
  197. dma_addr_t addr, int nr_pages)
  198. {
  199. dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
  200. nr_pages, (unsigned long)addr);
  201. dma->direction = direction;
  202. if (0 == addr)
  203. return -EINVAL;
  204. dma->bus_addr = addr;
  205. dma->nr_pages = nr_pages;
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
  209. int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
  210. {
  211. MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
  212. BUG_ON(0 == dma->nr_pages);
  213. if (dma->pages) {
  214. dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
  215. dma->offset, dma->size);
  216. }
  217. if (dma->vaddr) {
  218. dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
  219. dma->nr_pages);
  220. }
  221. if (dma->bus_addr) {
  222. dma->sglist = vmalloc(sizeof(*dma->sglist));
  223. if (NULL != dma->sglist) {
  224. dma->sglen = 1;
  225. sg_dma_address(&dma->sglist[0]) = dma->bus_addr
  226. & PAGE_MASK;
  227. dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
  228. sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
  229. }
  230. }
  231. if (NULL == dma->sglist) {
  232. dprintk(1, "scatterlist is NULL\n");
  233. return -ENOMEM;
  234. }
  235. if (!dma->bus_addr) {
  236. dma->sglen = dma_map_sg(dev, dma->sglist,
  237. dma->nr_pages, dma->direction);
  238. if (0 == dma->sglen) {
  239. printk(KERN_WARNING
  240. "%s: videobuf_map_sg failed\n", __func__);
  241. vfree(dma->sglist);
  242. dma->sglist = NULL;
  243. dma->sglen = 0;
  244. return -ENOMEM;
  245. }
  246. }
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(videobuf_dma_map);
  250. int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
  251. {
  252. MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
  253. if (!dma->sglen)
  254. return 0;
  255. dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction);
  256. vfree(dma->sglist);
  257. dma->sglist = NULL;
  258. dma->sglen = 0;
  259. return 0;
  260. }
  261. EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
  262. int videobuf_dma_free(struct videobuf_dmabuf *dma)
  263. {
  264. int i;
  265. MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
  266. BUG_ON(dma->sglen);
  267. if (dma->pages) {
  268. for (i = 0; i < dma->nr_pages; i++)
  269. page_cache_release(dma->pages[i]);
  270. kfree(dma->pages);
  271. dma->pages = NULL;
  272. }
  273. vfree(dma->vaddr);
  274. dma->vaddr = NULL;
  275. if (dma->bus_addr)
  276. dma->bus_addr = 0;
  277. dma->direction = DMA_NONE;
  278. return 0;
  279. }
  280. EXPORT_SYMBOL_GPL(videobuf_dma_free);
  281. /* --------------------------------------------------------------------- */
  282. static void videobuf_vm_open(struct vm_area_struct *vma)
  283. {
  284. struct videobuf_mapping *map = vma->vm_private_data;
  285. dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
  286. map->count, vma->vm_start, vma->vm_end);
  287. map->count++;
  288. }
  289. static void videobuf_vm_close(struct vm_area_struct *vma)
  290. {
  291. struct videobuf_mapping *map = vma->vm_private_data;
  292. struct videobuf_queue *q = map->q;
  293. struct videobuf_dma_sg_memory *mem;
  294. int i;
  295. dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
  296. map->count, vma->vm_start, vma->vm_end);
  297. map->count--;
  298. if (0 == map->count) {
  299. dprintk(1, "munmap %p q=%p\n", map, q);
  300. videobuf_queue_lock(q);
  301. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  302. if (NULL == q->bufs[i])
  303. continue;
  304. mem = q->bufs[i]->priv;
  305. if (!mem)
  306. continue;
  307. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  308. if (q->bufs[i]->map != map)
  309. continue;
  310. q->bufs[i]->map = NULL;
  311. q->bufs[i]->baddr = 0;
  312. q->ops->buf_release(q, q->bufs[i]);
  313. }
  314. videobuf_queue_unlock(q);
  315. kfree(map);
  316. }
  317. return;
  318. }
  319. /*
  320. * Get a anonymous page for the mapping. Make sure we can DMA to that
  321. * memory location with 32bit PCI devices (i.e. don't use highmem for
  322. * now ...). Bounce buffers don't work very well for the data rates
  323. * video capture has.
  324. */
  325. static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  326. {
  327. struct page *page;
  328. dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
  329. (unsigned long)vmf->virtual_address,
  330. vma->vm_start, vma->vm_end);
  331. page = alloc_page(GFP_USER | __GFP_DMA32);
  332. if (!page)
  333. return VM_FAULT_OOM;
  334. clear_user_highpage(page, (unsigned long)vmf->virtual_address);
  335. vmf->page = page;
  336. return 0;
  337. }
  338. static const struct vm_operations_struct videobuf_vm_ops = {
  339. .open = videobuf_vm_open,
  340. .close = videobuf_vm_close,
  341. .fault = videobuf_vm_fault,
  342. };
  343. /* ---------------------------------------------------------------------
  344. * SG handlers for the generic methods
  345. */
  346. /* Allocated area consists on 3 parts:
  347. struct video_buffer
  348. struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
  349. struct videobuf_dma_sg_memory
  350. */
  351. static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
  352. {
  353. struct videobuf_dma_sg_memory *mem;
  354. struct videobuf_buffer *vb;
  355. vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
  356. if (!vb)
  357. return vb;
  358. mem = vb->priv = ((char *)vb) + size;
  359. mem->magic = MAGIC_SG_MEM;
  360. videobuf_dma_init(&mem->dma);
  361. dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
  362. __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
  363. mem, (long)sizeof(*mem));
  364. return vb;
  365. }
  366. static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
  367. {
  368. struct videobuf_dma_sg_memory *mem = buf->priv;
  369. BUG_ON(!mem);
  370. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  371. return mem->dma.vaddr;
  372. }
  373. static int __videobuf_iolock(struct videobuf_queue *q,
  374. struct videobuf_buffer *vb,
  375. struct v4l2_framebuffer *fbuf)
  376. {
  377. int err, pages;
  378. dma_addr_t bus;
  379. struct videobuf_dma_sg_memory *mem = vb->priv;
  380. BUG_ON(!mem);
  381. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  382. switch (vb->memory) {
  383. case V4L2_MEMORY_MMAP:
  384. case V4L2_MEMORY_USERPTR:
  385. if (0 == vb->baddr) {
  386. /* no userspace addr -- kernel bounce buffer */
  387. pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
  388. err = videobuf_dma_init_kernel(&mem->dma,
  389. DMA_FROM_DEVICE,
  390. pages);
  391. if (0 != err)
  392. return err;
  393. } else if (vb->memory == V4L2_MEMORY_USERPTR) {
  394. /* dma directly to userspace */
  395. err = videobuf_dma_init_user(&mem->dma,
  396. DMA_FROM_DEVICE,
  397. vb->baddr, vb->bsize);
  398. if (0 != err)
  399. return err;
  400. } else {
  401. /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
  402. buffers can only be called from videobuf_qbuf
  403. we take current->mm->mmap_sem there, to prevent
  404. locking inversion, so don't take it here */
  405. err = videobuf_dma_init_user_locked(&mem->dma,
  406. DMA_FROM_DEVICE,
  407. vb->baddr, vb->bsize);
  408. if (0 != err)
  409. return err;
  410. }
  411. break;
  412. case V4L2_MEMORY_OVERLAY:
  413. if (NULL == fbuf)
  414. return -EINVAL;
  415. /* FIXME: need sanity checks for vb->boff */
  416. /*
  417. * Using a double cast to avoid compiler warnings when
  418. * building for PAE. Compiler doesn't like direct casting
  419. * of a 32 bit ptr to 64 bit integer.
  420. */
  421. bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
  422. pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
  423. err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
  424. bus, pages);
  425. if (0 != err)
  426. return err;
  427. break;
  428. default:
  429. BUG();
  430. }
  431. err = videobuf_dma_map(q->dev, &mem->dma);
  432. if (0 != err)
  433. return err;
  434. return 0;
  435. }
  436. static int __videobuf_sync(struct videobuf_queue *q,
  437. struct videobuf_buffer *buf)
  438. {
  439. struct videobuf_dma_sg_memory *mem = buf->priv;
  440. BUG_ON(!mem || !mem->dma.sglen);
  441. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  442. MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
  443. dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
  444. mem->dma.sglen, mem->dma.direction);
  445. return 0;
  446. }
  447. static int __videobuf_mmap_mapper(struct videobuf_queue *q,
  448. struct videobuf_buffer *buf,
  449. struct vm_area_struct *vma)
  450. {
  451. struct videobuf_dma_sg_memory *mem = buf->priv;
  452. struct videobuf_mapping *map;
  453. unsigned int first, last, size = 0, i;
  454. int retval;
  455. retval = -EINVAL;
  456. BUG_ON(!mem);
  457. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  458. /* look for first buffer to map */
  459. for (first = 0; first < VIDEO_MAX_FRAME; first++) {
  460. if (buf == q->bufs[first]) {
  461. size = PAGE_ALIGN(q->bufs[first]->bsize);
  462. break;
  463. }
  464. }
  465. /* paranoia, should never happen since buf is always valid. */
  466. if (!size) {
  467. dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
  468. (vma->vm_pgoff << PAGE_SHIFT));
  469. goto done;
  470. }
  471. last = first;
  472. /* create mapping + update buffer list */
  473. retval = -ENOMEM;
  474. map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
  475. if (NULL == map)
  476. goto done;
  477. size = 0;
  478. for (i = first; i <= last; i++) {
  479. if (NULL == q->bufs[i])
  480. continue;
  481. q->bufs[i]->map = map;
  482. q->bufs[i]->baddr = vma->vm_start + size;
  483. size += PAGE_ALIGN(q->bufs[i]->bsize);
  484. }
  485. map->count = 1;
  486. map->q = q;
  487. vma->vm_ops = &videobuf_vm_ops;
  488. vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
  489. vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
  490. vma->vm_private_data = map;
  491. dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
  492. map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
  493. retval = 0;
  494. done:
  495. return retval;
  496. }
  497. static struct videobuf_qtype_ops sg_ops = {
  498. .magic = MAGIC_QTYPE_OPS,
  499. .alloc_vb = __videobuf_alloc_vb,
  500. .iolock = __videobuf_iolock,
  501. .sync = __videobuf_sync,
  502. .mmap_mapper = __videobuf_mmap_mapper,
  503. .vaddr = __videobuf_to_vaddr,
  504. };
  505. void *videobuf_sg_alloc(size_t size)
  506. {
  507. struct videobuf_queue q;
  508. /* Required to make generic handler to call __videobuf_alloc */
  509. q.int_ops = &sg_ops;
  510. q.msize = size;
  511. return videobuf_alloc_vb(&q);
  512. }
  513. EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
  514. void videobuf_queue_sg_init(struct videobuf_queue *q,
  515. const struct videobuf_queue_ops *ops,
  516. struct device *dev,
  517. spinlock_t *irqlock,
  518. enum v4l2_buf_type type,
  519. enum v4l2_field field,
  520. unsigned int msize,
  521. void *priv,
  522. struct mutex *ext_lock)
  523. {
  524. videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
  525. priv, &sg_ops, ext_lock);
  526. }
  527. EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);