videobuf2-memops.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * videobuf2-memops.c - generic memory handling routines for videobuf2
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. * Marek Szyprowski <m.szyprowski@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation.
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/mm.h>
  18. #include <linux/sched.h>
  19. #include <linux/file.h>
  20. #include <media/videobuf2-v4l2.h>
  21. #include <media/videobuf2-memops.h>
  22. /**
  23. * vb2_create_framevec() - map virtual addresses to pfns
  24. * @start: Virtual user address where we start mapping
  25. * @length: Length of a range to map
  26. * @write: Should we map for writing into the area
  27. *
  28. * This function allocates and fills in a vector with pfns corresponding to
  29. * virtual address range passed in arguments. If pfns have corresponding pages,
  30. * page references are also grabbed to pin pages in memory. The function
  31. * returns pointer to the vector on success and error pointer in case of
  32. * failure. Returned vector needs to be freed via vb2_destroy_pfnvec().
  33. */
  34. struct frame_vector *vb2_create_framevec(unsigned long start,
  35. unsigned long length,
  36. bool write)
  37. {
  38. int ret;
  39. unsigned long first, last;
  40. unsigned long nr;
  41. struct frame_vector *vec;
  42. unsigned int flags = FOLL_FORCE;
  43. if (write)
  44. flags |= FOLL_WRITE;
  45. first = start >> PAGE_SHIFT;
  46. last = (start + length - 1) >> PAGE_SHIFT;
  47. nr = last - first + 1;
  48. vec = frame_vector_create(nr);
  49. if (!vec)
  50. return ERR_PTR(-ENOMEM);
  51. ret = get_vaddr_frames(start & PAGE_MASK, nr, flags, vec);
  52. if (ret < 0)
  53. goto out_destroy;
  54. /* We accept only complete set of PFNs */
  55. if (ret != nr) {
  56. ret = -EFAULT;
  57. goto out_release;
  58. }
  59. return vec;
  60. out_release:
  61. put_vaddr_frames(vec);
  62. out_destroy:
  63. frame_vector_destroy(vec);
  64. return ERR_PTR(ret);
  65. }
  66. EXPORT_SYMBOL(vb2_create_framevec);
  67. /**
  68. * vb2_destroy_framevec() - release vector of mapped pfns
  69. * @vec: vector of pfns / pages to release
  70. *
  71. * This releases references to all pages in the vector @vec (if corresponding
  72. * pfns are backed by pages) and frees the passed vector.
  73. */
  74. void vb2_destroy_framevec(struct frame_vector *vec)
  75. {
  76. put_vaddr_frames(vec);
  77. frame_vector_destroy(vec);
  78. }
  79. EXPORT_SYMBOL(vb2_destroy_framevec);
  80. /**
  81. * vb2_common_vm_open() - increase refcount of the vma
  82. * @vma: virtual memory region for the mapping
  83. *
  84. * This function adds another user to the provided vma. It expects
  85. * struct vb2_vmarea_handler pointer in vma->vm_private_data.
  86. */
  87. static void vb2_common_vm_open(struct vm_area_struct *vma)
  88. {
  89. struct vb2_vmarea_handler *h = vma->vm_private_data;
  90. pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
  91. __func__, h, atomic_read(h->refcount), vma->vm_start,
  92. vma->vm_end);
  93. atomic_inc(h->refcount);
  94. }
  95. /**
  96. * vb2_common_vm_close() - decrease refcount of the vma
  97. * @vma: virtual memory region for the mapping
  98. *
  99. * This function releases the user from the provided vma. It expects
  100. * struct vb2_vmarea_handler pointer in vma->vm_private_data.
  101. */
  102. static void vb2_common_vm_close(struct vm_area_struct *vma)
  103. {
  104. struct vb2_vmarea_handler *h = vma->vm_private_data;
  105. pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
  106. __func__, h, atomic_read(h->refcount), vma->vm_start,
  107. vma->vm_end);
  108. h->put(h->arg);
  109. }
  110. /**
  111. * vb2_common_vm_ops - common vm_ops used for tracking refcount of mmaped
  112. * video buffers
  113. */
  114. const struct vm_operations_struct vb2_common_vm_ops = {
  115. .open = vb2_common_vm_open,
  116. .close = vb2_common_vm_close,
  117. };
  118. EXPORT_SYMBOL_GPL(vb2_common_vm_ops);
  119. MODULE_DESCRIPTION("common memory handling routines for videobuf2");
  120. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  121. MODULE_LICENSE("GPL");