videobuf2-vmalloc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * videobuf2-vmalloc.c - vmalloc memory allocator for videobuf2
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <media/videobuf2-core.h>
  19. #include <media/videobuf2-vmalloc.h>
  20. #include <media/videobuf2-memops.h>
  21. struct vb2_vmalloc_buf {
  22. void *vaddr;
  23. struct page **pages;
  24. struct vm_area_struct *vma;
  25. enum dma_data_direction dma_dir;
  26. unsigned long size;
  27. unsigned int n_pages;
  28. atomic_t refcount;
  29. struct vb2_vmarea_handler handler;
  30. struct dma_buf *dbuf;
  31. };
  32. static void vb2_vmalloc_put(void *buf_priv);
  33. static void *vb2_vmalloc_alloc(void *alloc_ctx, unsigned long size,
  34. enum dma_data_direction dma_dir, gfp_t gfp_flags)
  35. {
  36. struct vb2_vmalloc_buf *buf;
  37. buf = kzalloc(sizeof(*buf), GFP_KERNEL | gfp_flags);
  38. if (!buf)
  39. return NULL;
  40. buf->size = size;
  41. buf->vaddr = vmalloc_user(buf->size);
  42. buf->dma_dir = dma_dir;
  43. buf->handler.refcount = &buf->refcount;
  44. buf->handler.put = vb2_vmalloc_put;
  45. buf->handler.arg = buf;
  46. if (!buf->vaddr) {
  47. pr_debug("vmalloc of size %ld failed\n", buf->size);
  48. kfree(buf);
  49. return NULL;
  50. }
  51. atomic_inc(&buf->refcount);
  52. return buf;
  53. }
  54. static void vb2_vmalloc_put(void *buf_priv)
  55. {
  56. struct vb2_vmalloc_buf *buf = buf_priv;
  57. if (atomic_dec_and_test(&buf->refcount)) {
  58. vfree(buf->vaddr);
  59. kfree(buf);
  60. }
  61. }
  62. static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
  63. unsigned long size,
  64. enum dma_data_direction dma_dir)
  65. {
  66. struct vb2_vmalloc_buf *buf;
  67. unsigned long first, last;
  68. int n_pages, offset;
  69. struct vm_area_struct *vma;
  70. dma_addr_t physp;
  71. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  72. if (!buf)
  73. return NULL;
  74. buf->dma_dir = dma_dir;
  75. offset = vaddr & ~PAGE_MASK;
  76. buf->size = size;
  77. vma = find_vma(current->mm, vaddr);
  78. if (vma && (vma->vm_flags & VM_PFNMAP) && (vma->vm_pgoff)) {
  79. if (vb2_get_contig_userptr(vaddr, size, &vma, &physp))
  80. goto fail_pages_array_alloc;
  81. buf->vma = vma;
  82. buf->vaddr = ioremap_nocache(physp, size);
  83. if (!buf->vaddr)
  84. goto fail_pages_array_alloc;
  85. } else {
  86. first = vaddr >> PAGE_SHIFT;
  87. last = (vaddr + size - 1) >> PAGE_SHIFT;
  88. buf->n_pages = last - first + 1;
  89. buf->pages = kzalloc(buf->n_pages * sizeof(struct page *),
  90. GFP_KERNEL);
  91. if (!buf->pages)
  92. goto fail_pages_array_alloc;
  93. /* current->mm->mmap_sem is taken by videobuf2 core */
  94. n_pages = get_user_pages(current, current->mm,
  95. vaddr & PAGE_MASK, buf->n_pages,
  96. dma_dir == DMA_FROM_DEVICE,
  97. 1, /* force */
  98. buf->pages, NULL);
  99. if (n_pages != buf->n_pages)
  100. goto fail_get_user_pages;
  101. buf->vaddr = vm_map_ram(buf->pages, buf->n_pages, -1,
  102. PAGE_KERNEL);
  103. if (!buf->vaddr)
  104. goto fail_get_user_pages;
  105. }
  106. buf->vaddr += offset;
  107. return buf;
  108. fail_get_user_pages:
  109. pr_debug("get_user_pages requested/got: %d/%d]\n", n_pages,
  110. buf->n_pages);
  111. while (--n_pages >= 0)
  112. put_page(buf->pages[n_pages]);
  113. kfree(buf->pages);
  114. fail_pages_array_alloc:
  115. kfree(buf);
  116. return NULL;
  117. }
  118. static void vb2_vmalloc_put_userptr(void *buf_priv)
  119. {
  120. struct vb2_vmalloc_buf *buf = buf_priv;
  121. unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK;
  122. unsigned int i;
  123. if (buf->pages) {
  124. if (vaddr)
  125. vm_unmap_ram((void *)vaddr, buf->n_pages);
  126. for (i = 0; i < buf->n_pages; ++i) {
  127. if (buf->dma_dir == DMA_FROM_DEVICE)
  128. set_page_dirty_lock(buf->pages[i]);
  129. put_page(buf->pages[i]);
  130. }
  131. kfree(buf->pages);
  132. } else {
  133. if (buf->vma)
  134. vb2_put_vma(buf->vma);
  135. iounmap(buf->vaddr);
  136. }
  137. kfree(buf);
  138. }
  139. static void *vb2_vmalloc_vaddr(void *buf_priv)
  140. {
  141. struct vb2_vmalloc_buf *buf = buf_priv;
  142. if (!buf->vaddr) {
  143. pr_err("Address of an unallocated plane requested "
  144. "or cannot map user pointer\n");
  145. return NULL;
  146. }
  147. return buf->vaddr;
  148. }
  149. static unsigned int vb2_vmalloc_num_users(void *buf_priv)
  150. {
  151. struct vb2_vmalloc_buf *buf = buf_priv;
  152. return atomic_read(&buf->refcount);
  153. }
  154. static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma)
  155. {
  156. struct vb2_vmalloc_buf *buf = buf_priv;
  157. int ret;
  158. if (!buf) {
  159. pr_err("No memory to map\n");
  160. return -EINVAL;
  161. }
  162. ret = remap_vmalloc_range(vma, buf->vaddr, 0);
  163. if (ret) {
  164. pr_err("Remapping vmalloc memory, error: %d\n", ret);
  165. return ret;
  166. }
  167. /*
  168. * Make sure that vm_areas for 2 buffers won't be merged together
  169. */
  170. vma->vm_flags |= VM_DONTEXPAND;
  171. /*
  172. * Use common vm_area operations to track buffer refcount.
  173. */
  174. vma->vm_private_data = &buf->handler;
  175. vma->vm_ops = &vb2_common_vm_ops;
  176. vma->vm_ops->open(vma);
  177. return 0;
  178. }
  179. /*********************************************/
  180. /* callbacks for DMABUF buffers */
  181. /*********************************************/
  182. static int vb2_vmalloc_map_dmabuf(void *mem_priv)
  183. {
  184. struct vb2_vmalloc_buf *buf = mem_priv;
  185. buf->vaddr = dma_buf_vmap(buf->dbuf);
  186. return buf->vaddr ? 0 : -EFAULT;
  187. }
  188. static void vb2_vmalloc_unmap_dmabuf(void *mem_priv)
  189. {
  190. struct vb2_vmalloc_buf *buf = mem_priv;
  191. dma_buf_vunmap(buf->dbuf, buf->vaddr);
  192. buf->vaddr = NULL;
  193. }
  194. static void vb2_vmalloc_detach_dmabuf(void *mem_priv)
  195. {
  196. struct vb2_vmalloc_buf *buf = mem_priv;
  197. if (buf->vaddr)
  198. dma_buf_vunmap(buf->dbuf, buf->vaddr);
  199. kfree(buf);
  200. }
  201. static void *vb2_vmalloc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
  202. unsigned long size, enum dma_data_direction dma_dir)
  203. {
  204. struct vb2_vmalloc_buf *buf;
  205. if (dbuf->size < size)
  206. return ERR_PTR(-EFAULT);
  207. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  208. if (!buf)
  209. return ERR_PTR(-ENOMEM);
  210. buf->dbuf = dbuf;
  211. buf->dma_dir = dma_dir;
  212. buf->size = size;
  213. return buf;
  214. }
  215. const struct vb2_mem_ops vb2_vmalloc_memops = {
  216. .alloc = vb2_vmalloc_alloc,
  217. .put = vb2_vmalloc_put,
  218. .get_userptr = vb2_vmalloc_get_userptr,
  219. .put_userptr = vb2_vmalloc_put_userptr,
  220. .map_dmabuf = vb2_vmalloc_map_dmabuf,
  221. .unmap_dmabuf = vb2_vmalloc_unmap_dmabuf,
  222. .attach_dmabuf = vb2_vmalloc_attach_dmabuf,
  223. .detach_dmabuf = vb2_vmalloc_detach_dmabuf,
  224. .vaddr = vb2_vmalloc_vaddr,
  225. .mmap = vb2_vmalloc_mmap,
  226. .num_users = vb2_vmalloc_num_users,
  227. };
  228. EXPORT_SYMBOL_GPL(vb2_vmalloc_memops);
  229. MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
  230. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  231. MODULE_LICENSE("GPL");