videobuf2-vmalloc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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-memops.h>
  20. struct vb2_vmalloc_buf {
  21. void *vaddr;
  22. struct page **pages;
  23. struct vm_area_struct *vma;
  24. int write;
  25. unsigned long size;
  26. unsigned int n_pages;
  27. atomic_t refcount;
  28. struct vb2_vmarea_handler handler;
  29. };
  30. static void vb2_vmalloc_put(void *buf_priv);
  31. static void *vb2_vmalloc_alloc(void *alloc_ctx, unsigned long size)
  32. {
  33. struct vb2_vmalloc_buf *buf;
  34. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  35. if (!buf)
  36. return NULL;
  37. buf->size = size;
  38. buf->vaddr = vmalloc_user(buf->size);
  39. buf->handler.refcount = &buf->refcount;
  40. buf->handler.put = vb2_vmalloc_put;
  41. buf->handler.arg = buf;
  42. if (!buf->vaddr) {
  43. pr_debug("vmalloc of size %ld failed\n", buf->size);
  44. kfree(buf);
  45. return NULL;
  46. }
  47. atomic_inc(&buf->refcount);
  48. return buf;
  49. }
  50. static void vb2_vmalloc_put(void *buf_priv)
  51. {
  52. struct vb2_vmalloc_buf *buf = buf_priv;
  53. if (atomic_dec_and_test(&buf->refcount)) {
  54. vfree(buf->vaddr);
  55. kfree(buf);
  56. }
  57. }
  58. static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
  59. unsigned long size, int write)
  60. {
  61. struct vb2_vmalloc_buf *buf;
  62. unsigned long first, last;
  63. int n_pages, offset;
  64. struct vm_area_struct *vma;
  65. dma_addr_t physp;
  66. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  67. if (!buf)
  68. return NULL;
  69. buf->write = write;
  70. offset = vaddr & ~PAGE_MASK;
  71. buf->size = size;
  72. vma = find_vma(current->mm, vaddr);
  73. if (vma && (vma->vm_flags & VM_PFNMAP) && (vma->vm_pgoff)) {
  74. if (vb2_get_contig_userptr(vaddr, size, &vma, &physp))
  75. goto fail_pages_array_alloc;
  76. buf->vma = vma;
  77. buf->vaddr = ioremap_nocache(physp, size);
  78. if (!buf->vaddr)
  79. goto fail_pages_array_alloc;
  80. } else {
  81. first = vaddr >> PAGE_SHIFT;
  82. last = (vaddr + size - 1) >> PAGE_SHIFT;
  83. buf->n_pages = last - first + 1;
  84. buf->pages = kzalloc(buf->n_pages * sizeof(struct page *),
  85. GFP_KERNEL);
  86. if (!buf->pages)
  87. goto fail_pages_array_alloc;
  88. /* current->mm->mmap_sem is taken by videobuf2 core */
  89. n_pages = get_user_pages(current, current->mm,
  90. vaddr & PAGE_MASK, buf->n_pages,
  91. write, 1, /* force */
  92. buf->pages, NULL);
  93. if (n_pages != buf->n_pages)
  94. goto fail_get_user_pages;
  95. buf->vaddr = vm_map_ram(buf->pages, buf->n_pages, -1,
  96. PAGE_KERNEL);
  97. if (!buf->vaddr)
  98. goto fail_get_user_pages;
  99. }
  100. buf->vaddr += offset;
  101. return buf;
  102. fail_get_user_pages:
  103. pr_debug("get_user_pages requested/got: %d/%d]\n", n_pages,
  104. buf->n_pages);
  105. while (--n_pages >= 0)
  106. put_page(buf->pages[n_pages]);
  107. kfree(buf->pages);
  108. fail_pages_array_alloc:
  109. kfree(buf);
  110. return NULL;
  111. }
  112. static void vb2_vmalloc_put_userptr(void *buf_priv)
  113. {
  114. struct vb2_vmalloc_buf *buf = buf_priv;
  115. unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK;
  116. unsigned int i;
  117. if (buf->pages) {
  118. if (vaddr)
  119. vm_unmap_ram((void *)vaddr, buf->n_pages);
  120. for (i = 0; i < buf->n_pages; ++i) {
  121. if (buf->write)
  122. set_page_dirty_lock(buf->pages[i]);
  123. put_page(buf->pages[i]);
  124. }
  125. kfree(buf->pages);
  126. } else {
  127. if (buf->vma)
  128. vb2_put_vma(buf->vma);
  129. iounmap(buf->vaddr);
  130. }
  131. kfree(buf);
  132. }
  133. static void *vb2_vmalloc_vaddr(void *buf_priv)
  134. {
  135. struct vb2_vmalloc_buf *buf = buf_priv;
  136. if (!buf->vaddr) {
  137. pr_err("Address of an unallocated plane requested "
  138. "or cannot map user pointer\n");
  139. return NULL;
  140. }
  141. return buf->vaddr;
  142. }
  143. static unsigned int vb2_vmalloc_num_users(void *buf_priv)
  144. {
  145. struct vb2_vmalloc_buf *buf = buf_priv;
  146. return atomic_read(&buf->refcount);
  147. }
  148. static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma)
  149. {
  150. struct vb2_vmalloc_buf *buf = buf_priv;
  151. int ret;
  152. if (!buf) {
  153. pr_err("No memory to map\n");
  154. return -EINVAL;
  155. }
  156. ret = remap_vmalloc_range(vma, buf->vaddr, 0);
  157. if (ret) {
  158. pr_err("Remapping vmalloc memory, error: %d\n", ret);
  159. return ret;
  160. }
  161. /*
  162. * Make sure that vm_areas for 2 buffers won't be merged together
  163. */
  164. vma->vm_flags |= VM_DONTEXPAND;
  165. /*
  166. * Use common vm_area operations to track buffer refcount.
  167. */
  168. vma->vm_private_data = &buf->handler;
  169. vma->vm_ops = &vb2_common_vm_ops;
  170. vma->vm_ops->open(vma);
  171. return 0;
  172. }
  173. const struct vb2_mem_ops vb2_vmalloc_memops = {
  174. .alloc = vb2_vmalloc_alloc,
  175. .put = vb2_vmalloc_put,
  176. .get_userptr = vb2_vmalloc_get_userptr,
  177. .put_userptr = vb2_vmalloc_put_userptr,
  178. .vaddr = vb2_vmalloc_vaddr,
  179. .mmap = vb2_vmalloc_mmap,
  180. .num_users = vb2_vmalloc_num_users,
  181. };
  182. EXPORT_SYMBOL_GPL(vb2_vmalloc_memops);
  183. MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
  184. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  185. MODULE_LICENSE("GPL");