videobuf2-dma-sg.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.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/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/scatterlist.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-memops.h>
  20. #include <media/videobuf2-dma-sg.h>
  21. struct vb2_dma_sg_buf {
  22. void *vaddr;
  23. struct page **pages;
  24. int write;
  25. int offset;
  26. struct vb2_dma_sg_desc sg_desc;
  27. atomic_t refcount;
  28. struct vb2_vmarea_handler handler;
  29. };
  30. static void vb2_dma_sg_put(void *buf_priv);
  31. static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size)
  32. {
  33. struct vb2_dma_sg_buf *buf;
  34. int i;
  35. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  36. if (!buf)
  37. return NULL;
  38. buf->vaddr = NULL;
  39. buf->write = 0;
  40. buf->offset = 0;
  41. buf->sg_desc.size = size;
  42. buf->sg_desc.num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  43. buf->sg_desc.sglist = vzalloc(buf->sg_desc.num_pages *
  44. sizeof(*buf->sg_desc.sglist));
  45. if (!buf->sg_desc.sglist)
  46. goto fail_sglist_alloc;
  47. sg_init_table(buf->sg_desc.sglist, buf->sg_desc.num_pages);
  48. buf->pages = kzalloc(buf->sg_desc.num_pages * sizeof(struct page *),
  49. GFP_KERNEL);
  50. if (!buf->pages)
  51. goto fail_pages_array_alloc;
  52. for (i = 0; i < buf->sg_desc.num_pages; ++i) {
  53. buf->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
  54. if (NULL == buf->pages[i])
  55. goto fail_pages_alloc;
  56. sg_set_page(&buf->sg_desc.sglist[i],
  57. buf->pages[i], PAGE_SIZE, 0);
  58. }
  59. buf->handler.refcount = &buf->refcount;
  60. buf->handler.put = vb2_dma_sg_put;
  61. buf->handler.arg = buf;
  62. atomic_inc(&buf->refcount);
  63. printk(KERN_DEBUG "%s: Allocated buffer of %d pages\n",
  64. __func__, buf->sg_desc.num_pages);
  65. return buf;
  66. fail_pages_alloc:
  67. while (--i >= 0)
  68. __free_page(buf->pages[i]);
  69. kfree(buf->pages);
  70. fail_pages_array_alloc:
  71. vfree(buf->sg_desc.sglist);
  72. fail_sglist_alloc:
  73. kfree(buf);
  74. return NULL;
  75. }
  76. static void vb2_dma_sg_put(void *buf_priv)
  77. {
  78. struct vb2_dma_sg_buf *buf = buf_priv;
  79. int i = buf->sg_desc.num_pages;
  80. if (atomic_dec_and_test(&buf->refcount)) {
  81. printk(KERN_DEBUG "%s: Freeing buffer of %d pages\n", __func__,
  82. buf->sg_desc.num_pages);
  83. if (buf->vaddr)
  84. vm_unmap_ram(buf->vaddr, buf->sg_desc.num_pages);
  85. vfree(buf->sg_desc.sglist);
  86. while (--i >= 0)
  87. __free_page(buf->pages[i]);
  88. kfree(buf->pages);
  89. kfree(buf);
  90. }
  91. }
  92. static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
  93. unsigned long size, int write)
  94. {
  95. struct vb2_dma_sg_buf *buf;
  96. unsigned long first, last;
  97. int num_pages_from_user, i;
  98. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  99. if (!buf)
  100. return NULL;
  101. buf->vaddr = NULL;
  102. buf->write = write;
  103. buf->offset = vaddr & ~PAGE_MASK;
  104. buf->sg_desc.size = size;
  105. first = (vaddr & PAGE_MASK) >> PAGE_SHIFT;
  106. last = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
  107. buf->sg_desc.num_pages = last - first + 1;
  108. buf->sg_desc.sglist = vzalloc(
  109. buf->sg_desc.num_pages * sizeof(*buf->sg_desc.sglist));
  110. if (!buf->sg_desc.sglist)
  111. goto userptr_fail_sglist_alloc;
  112. sg_init_table(buf->sg_desc.sglist, buf->sg_desc.num_pages);
  113. buf->pages = kzalloc(buf->sg_desc.num_pages * sizeof(struct page *),
  114. GFP_KERNEL);
  115. if (!buf->pages)
  116. goto userptr_fail_pages_array_alloc;
  117. num_pages_from_user = get_user_pages(current, current->mm,
  118. vaddr & PAGE_MASK,
  119. buf->sg_desc.num_pages,
  120. write,
  121. 1, /* force */
  122. buf->pages,
  123. NULL);
  124. if (num_pages_from_user != buf->sg_desc.num_pages)
  125. goto userptr_fail_get_user_pages;
  126. sg_set_page(&buf->sg_desc.sglist[0], buf->pages[0],
  127. PAGE_SIZE - buf->offset, buf->offset);
  128. size -= PAGE_SIZE - buf->offset;
  129. for (i = 1; i < buf->sg_desc.num_pages; ++i) {
  130. sg_set_page(&buf->sg_desc.sglist[i], buf->pages[i],
  131. min_t(size_t, PAGE_SIZE, size), 0);
  132. size -= min_t(size_t, PAGE_SIZE, size);
  133. }
  134. return buf;
  135. userptr_fail_get_user_pages:
  136. printk(KERN_DEBUG "get_user_pages requested/got: %d/%d]\n",
  137. num_pages_from_user, buf->sg_desc.num_pages);
  138. while (--num_pages_from_user >= 0)
  139. put_page(buf->pages[num_pages_from_user]);
  140. kfree(buf->pages);
  141. userptr_fail_pages_array_alloc:
  142. vfree(buf->sg_desc.sglist);
  143. userptr_fail_sglist_alloc:
  144. kfree(buf);
  145. return NULL;
  146. }
  147. /*
  148. * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  149. * be used
  150. */
  151. static void vb2_dma_sg_put_userptr(void *buf_priv)
  152. {
  153. struct vb2_dma_sg_buf *buf = buf_priv;
  154. int i = buf->sg_desc.num_pages;
  155. printk(KERN_DEBUG "%s: Releasing userspace buffer of %d pages\n",
  156. __func__, buf->sg_desc.num_pages);
  157. if (buf->vaddr)
  158. vm_unmap_ram(buf->vaddr, buf->sg_desc.num_pages);
  159. while (--i >= 0) {
  160. if (buf->write)
  161. set_page_dirty_lock(buf->pages[i]);
  162. put_page(buf->pages[i]);
  163. }
  164. vfree(buf->sg_desc.sglist);
  165. kfree(buf->pages);
  166. kfree(buf);
  167. }
  168. static void *vb2_dma_sg_vaddr(void *buf_priv)
  169. {
  170. struct vb2_dma_sg_buf *buf = buf_priv;
  171. BUG_ON(!buf);
  172. if (!buf->vaddr)
  173. buf->vaddr = vm_map_ram(buf->pages,
  174. buf->sg_desc.num_pages,
  175. -1,
  176. PAGE_KERNEL);
  177. /* add offset in case userptr is not page-aligned */
  178. return buf->vaddr + buf->offset;
  179. }
  180. static unsigned int vb2_dma_sg_num_users(void *buf_priv)
  181. {
  182. struct vb2_dma_sg_buf *buf = buf_priv;
  183. return atomic_read(&buf->refcount);
  184. }
  185. static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
  186. {
  187. struct vb2_dma_sg_buf *buf = buf_priv;
  188. unsigned long uaddr = vma->vm_start;
  189. unsigned long usize = vma->vm_end - vma->vm_start;
  190. int i = 0;
  191. if (!buf) {
  192. printk(KERN_ERR "No memory to map\n");
  193. return -EINVAL;
  194. }
  195. do {
  196. int ret;
  197. ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
  198. if (ret) {
  199. printk(KERN_ERR "Remapping memory, error: %d\n", ret);
  200. return ret;
  201. }
  202. uaddr += PAGE_SIZE;
  203. usize -= PAGE_SIZE;
  204. } while (usize > 0);
  205. /*
  206. * Use common vm_area operations to track buffer refcount.
  207. */
  208. vma->vm_private_data = &buf->handler;
  209. vma->vm_ops = &vb2_common_vm_ops;
  210. vma->vm_ops->open(vma);
  211. return 0;
  212. }
  213. static void *vb2_dma_sg_cookie(void *buf_priv)
  214. {
  215. struct vb2_dma_sg_buf *buf = buf_priv;
  216. return &buf->sg_desc;
  217. }
  218. const struct vb2_mem_ops vb2_dma_sg_memops = {
  219. .alloc = vb2_dma_sg_alloc,
  220. .put = vb2_dma_sg_put,
  221. .get_userptr = vb2_dma_sg_get_userptr,
  222. .put_userptr = vb2_dma_sg_put_userptr,
  223. .vaddr = vb2_dma_sg_vaddr,
  224. .mmap = vb2_dma_sg_mmap,
  225. .num_users = vb2_dma_sg_num_users,
  226. .cookie = vb2_dma_sg_cookie,
  227. };
  228. EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
  229. MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
  230. MODULE_AUTHOR("Andrzej Pietrasiewicz");
  231. MODULE_LICENSE("GPL");