videobuf2-memops.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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-core.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. first = start >> PAGE_SHIFT;
  43. last = (start + length - 1) >> PAGE_SHIFT;
  44. nr = last - first + 1;
  45. vec = frame_vector_create(nr);
  46. if (!vec)
  47. return ERR_PTR(-ENOMEM);
  48. ret = get_vaddr_frames(start, nr, write, 1, vec);
  49. if (ret < 0)
  50. goto out_destroy;
  51. /* We accept only complete set of PFNs */
  52. if (ret != nr) {
  53. ret = -EFAULT;
  54. goto out_release;
  55. }
  56. return vec;
  57. out_release:
  58. put_vaddr_frames(vec);
  59. out_destroy:
  60. frame_vector_destroy(vec);
  61. return ERR_PTR(ret);
  62. }
  63. EXPORT_SYMBOL(vb2_create_framevec);
  64. /**
  65. * vb2_destroy_framevec() - release vector of mapped pfns
  66. * @vec: vector of pfns / pages to release
  67. *
  68. * This releases references to all pages in the vector @vec (if corresponding
  69. * pfns are backed by pages) and frees the passed vector.
  70. */
  71. void vb2_destroy_framevec(struct frame_vector *vec)
  72. {
  73. put_vaddr_frames(vec);
  74. frame_vector_destroy(vec);
  75. }
  76. EXPORT_SYMBOL(vb2_destroy_framevec);
  77. /**
  78. * vb2_common_vm_open() - increase refcount of the vma
  79. * @vma: virtual memory region for the mapping
  80. *
  81. * This function adds another user to the provided vma. It expects
  82. * struct vb2_vmarea_handler pointer in vma->vm_private_data.
  83. */
  84. static void vb2_common_vm_open(struct vm_area_struct *vma)
  85. {
  86. struct vb2_vmarea_handler *h = vma->vm_private_data;
  87. pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
  88. __func__, h, atomic_read(h->refcount), vma->vm_start,
  89. vma->vm_end);
  90. atomic_inc(h->refcount);
  91. }
  92. /**
  93. * vb2_common_vm_close() - decrease refcount of the vma
  94. * @vma: virtual memory region for the mapping
  95. *
  96. * This function releases the user from the provided vma. It expects
  97. * struct vb2_vmarea_handler pointer in vma->vm_private_data.
  98. */
  99. static void vb2_common_vm_close(struct vm_area_struct *vma)
  100. {
  101. struct vb2_vmarea_handler *h = vma->vm_private_data;
  102. pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
  103. __func__, h, atomic_read(h->refcount), vma->vm_start,
  104. vma->vm_end);
  105. h->put(h->arg);
  106. }
  107. /**
  108. * vb2_common_vm_ops - common vm_ops used for tracking refcount of mmaped
  109. * video buffers
  110. */
  111. const struct vm_operations_struct vb2_common_vm_ops = {
  112. .open = vb2_common_vm_open,
  113. .close = vb2_common_vm_close,
  114. };
  115. EXPORT_SYMBOL_GPL(vb2_common_vm_ops);
  116. MODULE_DESCRIPTION("common memory handling routines for videobuf2");
  117. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  118. MODULE_LICENSE("GPL");