videobuf-dma-contig.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * helper functions for physically contiguous capture buffers
  3. *
  4. * The functions support hardware lacking scatter gather support
  5. * (i.e. the buffers must be linear in physical memory)
  6. *
  7. * Copyright (c) 2008 Magnus Damm
  8. *
  9. * Based on videobuf-vmalloc.c,
  10. * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/mm.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <media/videobuf-dma-contig.h>
  24. struct videobuf_dma_contig_memory {
  25. u32 magic;
  26. void *vaddr;
  27. dma_addr_t dma_handle;
  28. unsigned long size;
  29. };
  30. #define MAGIC_DC_MEM 0x0733ac61
  31. #define MAGIC_CHECK(is, should) \
  32. if (unlikely((is) != (should))) { \
  33. pr_err("magic mismatch: %x expected %x\n", (is), (should)); \
  34. BUG(); \
  35. }
  36. static int __videobuf_dc_alloc(struct device *dev,
  37. struct videobuf_dma_contig_memory *mem,
  38. unsigned long size, gfp_t flags)
  39. {
  40. mem->size = size;
  41. mem->vaddr = dma_alloc_coherent(dev, mem->size,
  42. &mem->dma_handle, flags);
  43. if (!mem->vaddr) {
  44. dev_err(dev, "memory alloc size %ld failed\n", mem->size);
  45. return -ENOMEM;
  46. }
  47. dev_dbg(dev, "dma mapped data is at %p (%ld)\n", mem->vaddr, mem->size);
  48. return 0;
  49. }
  50. static void __videobuf_dc_free(struct device *dev,
  51. struct videobuf_dma_contig_memory *mem)
  52. {
  53. dma_free_coherent(dev, mem->size, mem->vaddr, mem->dma_handle);
  54. mem->vaddr = NULL;
  55. }
  56. static void videobuf_vm_open(struct vm_area_struct *vma)
  57. {
  58. struct videobuf_mapping *map = vma->vm_private_data;
  59. dev_dbg(map->q->dev, "vm_open %p [count=%u,vma=%08lx-%08lx]\n",
  60. map, map->count, vma->vm_start, vma->vm_end);
  61. map->count++;
  62. }
  63. static void videobuf_vm_close(struct vm_area_struct *vma)
  64. {
  65. struct videobuf_mapping *map = vma->vm_private_data;
  66. struct videobuf_queue *q = map->q;
  67. int i;
  68. dev_dbg(q->dev, "vm_close %p [count=%u,vma=%08lx-%08lx]\n",
  69. map, map->count, vma->vm_start, vma->vm_end);
  70. map->count--;
  71. if (0 == map->count) {
  72. struct videobuf_dma_contig_memory *mem;
  73. dev_dbg(q->dev, "munmap %p q=%p\n", map, q);
  74. videobuf_queue_lock(q);
  75. /* We need first to cancel streams, before unmapping */
  76. if (q->streaming)
  77. videobuf_queue_cancel(q);
  78. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  79. if (NULL == q->bufs[i])
  80. continue;
  81. if (q->bufs[i]->map != map)
  82. continue;
  83. mem = q->bufs[i]->priv;
  84. if (mem) {
  85. /* This callback is called only if kernel has
  86. allocated memory and this memory is mmapped.
  87. In this case, memory should be freed,
  88. in order to do memory unmap.
  89. */
  90. MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
  91. /* vfree is not atomic - can't be
  92. called with IRQ's disabled
  93. */
  94. dev_dbg(q->dev, "buf[%d] freeing %p\n",
  95. i, mem->vaddr);
  96. __videobuf_dc_free(q->dev, mem);
  97. mem->vaddr = NULL;
  98. }
  99. q->bufs[i]->map = NULL;
  100. q->bufs[i]->baddr = 0;
  101. }
  102. kfree(map);
  103. videobuf_queue_unlock(q);
  104. }
  105. }
  106. static const struct vm_operations_struct videobuf_vm_ops = {
  107. .open = videobuf_vm_open,
  108. .close = videobuf_vm_close,
  109. };
  110. /**
  111. * videobuf_dma_contig_user_put() - reset pointer to user space buffer
  112. * @mem: per-buffer private videobuf-dma-contig data
  113. *
  114. * This function resets the user space pointer
  115. */
  116. static void videobuf_dma_contig_user_put(struct videobuf_dma_contig_memory *mem)
  117. {
  118. mem->dma_handle = 0;
  119. mem->size = 0;
  120. }
  121. /**
  122. * videobuf_dma_contig_user_get() - setup user space memory pointer
  123. * @mem: per-buffer private videobuf-dma-contig data
  124. * @vb: video buffer to map
  125. *
  126. * This function validates and sets up a pointer to user space memory.
  127. * Only physically contiguous pfn-mapped memory is accepted.
  128. *
  129. * Returns 0 if successful.
  130. */
  131. static int videobuf_dma_contig_user_get(struct videobuf_dma_contig_memory *mem,
  132. struct videobuf_buffer *vb)
  133. {
  134. struct mm_struct *mm = current->mm;
  135. struct vm_area_struct *vma;
  136. unsigned long prev_pfn, this_pfn;
  137. unsigned long pages_done, user_address;
  138. unsigned int offset;
  139. int ret;
  140. offset = vb->baddr & ~PAGE_MASK;
  141. mem->size = PAGE_ALIGN(vb->size + offset);
  142. ret = -EINVAL;
  143. down_read(&mm->mmap_sem);
  144. vma = find_vma(mm, vb->baddr);
  145. if (!vma)
  146. goto out_up;
  147. if ((vb->baddr + mem->size) > vma->vm_end)
  148. goto out_up;
  149. pages_done = 0;
  150. prev_pfn = 0; /* kill warning */
  151. user_address = vb->baddr;
  152. while (pages_done < (mem->size >> PAGE_SHIFT)) {
  153. ret = follow_pfn(vma, user_address, &this_pfn);
  154. if (ret)
  155. break;
  156. if (pages_done == 0)
  157. mem->dma_handle = (this_pfn << PAGE_SHIFT) + offset;
  158. else if (this_pfn != (prev_pfn + 1))
  159. ret = -EFAULT;
  160. if (ret)
  161. break;
  162. prev_pfn = this_pfn;
  163. user_address += PAGE_SIZE;
  164. pages_done++;
  165. }
  166. out_up:
  167. up_read(&current->mm->mmap_sem);
  168. return ret;
  169. }
  170. static struct videobuf_buffer *__videobuf_alloc(size_t size)
  171. {
  172. struct videobuf_dma_contig_memory *mem;
  173. struct videobuf_buffer *vb;
  174. vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
  175. if (vb) {
  176. vb->priv = ((char *)vb) + size;
  177. mem = vb->priv;
  178. mem->magic = MAGIC_DC_MEM;
  179. }
  180. return vb;
  181. }
  182. static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
  183. {
  184. struct videobuf_dma_contig_memory *mem = buf->priv;
  185. BUG_ON(!mem);
  186. MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
  187. return mem->vaddr;
  188. }
  189. static int __videobuf_iolock(struct videobuf_queue *q,
  190. struct videobuf_buffer *vb,
  191. struct v4l2_framebuffer *fbuf)
  192. {
  193. struct videobuf_dma_contig_memory *mem = vb->priv;
  194. BUG_ON(!mem);
  195. MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
  196. switch (vb->memory) {
  197. case V4L2_MEMORY_MMAP:
  198. dev_dbg(q->dev, "%s memory method MMAP\n", __func__);
  199. /* All handling should be done by __videobuf_mmap_mapper() */
  200. if (!mem->vaddr) {
  201. dev_err(q->dev, "memory is not alloced/mmapped.\n");
  202. return -EINVAL;
  203. }
  204. break;
  205. case V4L2_MEMORY_USERPTR:
  206. dev_dbg(q->dev, "%s memory method USERPTR\n", __func__);
  207. /* handle pointer from user space */
  208. if (vb->baddr)
  209. return videobuf_dma_contig_user_get(mem, vb);
  210. /* allocate memory for the read() method */
  211. if (__videobuf_dc_alloc(q->dev, mem, PAGE_ALIGN(vb->size),
  212. GFP_KERNEL))
  213. return -ENOMEM;
  214. break;
  215. case V4L2_MEMORY_OVERLAY:
  216. default:
  217. dev_dbg(q->dev, "%s memory method OVERLAY/unknown\n", __func__);
  218. return -EINVAL;
  219. }
  220. return 0;
  221. }
  222. static int __videobuf_mmap_mapper(struct videobuf_queue *q,
  223. struct videobuf_buffer *buf,
  224. struct vm_area_struct *vma)
  225. {
  226. struct videobuf_dma_contig_memory *mem;
  227. struct videobuf_mapping *map;
  228. int retval;
  229. unsigned long size;
  230. dev_dbg(q->dev, "%s\n", __func__);
  231. /* create mapping + update buffer list */
  232. map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
  233. if (!map)
  234. return -ENOMEM;
  235. buf->map = map;
  236. map->q = q;
  237. buf->baddr = vma->vm_start;
  238. mem = buf->priv;
  239. BUG_ON(!mem);
  240. MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
  241. if (__videobuf_dc_alloc(q->dev, mem, PAGE_ALIGN(buf->bsize),
  242. GFP_KERNEL | __GFP_COMP))
  243. goto error;
  244. /* Try to remap memory */
  245. size = vma->vm_end - vma->vm_start;
  246. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  247. /* the "vm_pgoff" is just used in v4l2 to find the
  248. * corresponding buffer data structure which is allocated
  249. * earlier and it does not mean the offset from the physical
  250. * buffer start address as usual. So set it to 0 to pass
  251. * the sanity check in vm_iomap_memory().
  252. */
  253. vma->vm_pgoff = 0;
  254. retval = vm_iomap_memory(vma, mem->dma_handle, size);
  255. if (retval) {
  256. dev_err(q->dev, "mmap: remap failed with error %d. ",
  257. retval);
  258. dma_free_coherent(q->dev, mem->size,
  259. mem->vaddr, mem->dma_handle);
  260. goto error;
  261. }
  262. vma->vm_ops = &videobuf_vm_ops;
  263. vma->vm_flags |= VM_DONTEXPAND;
  264. vma->vm_private_data = map;
  265. dev_dbg(q->dev, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
  266. map, q, vma->vm_start, vma->vm_end,
  267. (long int)buf->bsize, vma->vm_pgoff, buf->i);
  268. videobuf_vm_open(vma);
  269. return 0;
  270. error:
  271. kfree(map);
  272. return -ENOMEM;
  273. }
  274. static struct videobuf_qtype_ops qops = {
  275. .magic = MAGIC_QTYPE_OPS,
  276. .alloc_vb = __videobuf_alloc,
  277. .iolock = __videobuf_iolock,
  278. .mmap_mapper = __videobuf_mmap_mapper,
  279. .vaddr = __videobuf_to_vaddr,
  280. };
  281. void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
  282. const struct videobuf_queue_ops *ops,
  283. struct device *dev,
  284. spinlock_t *irqlock,
  285. enum v4l2_buf_type type,
  286. enum v4l2_field field,
  287. unsigned int msize,
  288. void *priv,
  289. struct mutex *ext_lock)
  290. {
  291. videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
  292. priv, &qops, ext_lock);
  293. }
  294. EXPORT_SYMBOL_GPL(videobuf_queue_dma_contig_init);
  295. dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf)
  296. {
  297. struct videobuf_dma_contig_memory *mem = buf->priv;
  298. BUG_ON(!mem);
  299. MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
  300. return mem->dma_handle;
  301. }
  302. EXPORT_SYMBOL_GPL(videobuf_to_dma_contig);
  303. void videobuf_dma_contig_free(struct videobuf_queue *q,
  304. struct videobuf_buffer *buf)
  305. {
  306. struct videobuf_dma_contig_memory *mem = buf->priv;
  307. /* mmapped memory can't be freed here, otherwise mmapped region
  308. would be released, while still needed. In this case, the memory
  309. release should happen inside videobuf_vm_close().
  310. So, it should free memory only if the memory were allocated for
  311. read() operation.
  312. */
  313. if (buf->memory != V4L2_MEMORY_USERPTR)
  314. return;
  315. if (!mem)
  316. return;
  317. MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
  318. /* handle user space pointer case */
  319. if (buf->baddr) {
  320. videobuf_dma_contig_user_put(mem);
  321. return;
  322. }
  323. /* read() method */
  324. if (mem->vaddr) {
  325. __videobuf_dc_free(q->dev, mem);
  326. mem->vaddr = NULL;
  327. }
  328. }
  329. EXPORT_SYMBOL_GPL(videobuf_dma_contig_free);
  330. MODULE_DESCRIPTION("helper module to manage video4linux dma contig buffers");
  331. MODULE_AUTHOR("Magnus Damm");
  332. MODULE_LICENSE("GPL");