videobuf2-memops.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * videobuf2-memops.c - generic memory handling routines for videobuf2
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <[email protected]>
  7. * Marek Szyprowski <[email protected]>
  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. *
  27. * This function allocates and fills in a vector with pfns corresponding to
  28. * virtual address range passed in arguments. If pfns have corresponding pages,
  29. * page references are also grabbed to pin pages in memory. The function
  30. * returns pointer to the vector on success and error pointer in case of
  31. * failure. Returned vector needs to be freed via vb2_destroy_pfnvec().
  32. */
  33. struct frame_vector *vb2_create_framevec(unsigned long start,
  34. unsigned long length)
  35. {
  36. int ret;
  37. unsigned long first, last;
  38. unsigned long nr;
  39. struct frame_vector *vec;
  40. first = start >> PAGE_SHIFT;
  41. last = (start + length - 1) >> PAGE_SHIFT;
  42. nr = last - first + 1;
  43. vec = frame_vector_create(nr);
  44. if (!vec)
  45. return ERR_PTR(-ENOMEM);
  46. ret = get_vaddr_frames(start & PAGE_MASK, nr, vec);
  47. if (ret < 0)
  48. goto out_destroy;
  49. /* We accept only complete set of PFNs */
  50. if (ret != nr) {
  51. ret = -EFAULT;
  52. goto out_release;
  53. }
  54. return vec;
  55. out_release:
  56. put_vaddr_frames(vec);
  57. out_destroy:
  58. frame_vector_destroy(vec);
  59. return ERR_PTR(ret);
  60. }
  61. EXPORT_SYMBOL(vb2_create_framevec);
  62. /**
  63. * vb2_destroy_framevec() - release vector of mapped pfns
  64. * @vec: vector of pfns / pages to release
  65. *
  66. * This releases references to all pages in the vector @vec (if corresponding
  67. * pfns are backed by pages) and frees the passed vector.
  68. */
  69. void vb2_destroy_framevec(struct frame_vector *vec)
  70. {
  71. put_vaddr_frames(vec);
  72. frame_vector_destroy(vec);
  73. }
  74. EXPORT_SYMBOL(vb2_destroy_framevec);
  75. /**
  76. * vb2_common_vm_open() - increase refcount of the vma
  77. * @vma: virtual memory region for the mapping
  78. *
  79. * This function adds another user to the provided vma. It expects
  80. * struct vb2_vmarea_handler pointer in vma->vm_private_data.
  81. */
  82. static void vb2_common_vm_open(struct vm_area_struct *vma)
  83. {
  84. struct vb2_vmarea_handler *h = vma->vm_private_data;
  85. pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
  86. __func__, h, refcount_read(h->refcount), vma->vm_start,
  87. vma->vm_end);
  88. refcount_inc(h->refcount);
  89. }
  90. /**
  91. * vb2_common_vm_close() - decrease refcount of the vma
  92. * @vma: virtual memory region for the mapping
  93. *
  94. * This function releases the user from the provided vma. It expects
  95. * struct vb2_vmarea_handler pointer in vma->vm_private_data.
  96. */
  97. static void vb2_common_vm_close(struct vm_area_struct *vma)
  98. {
  99. struct vb2_vmarea_handler *h = vma->vm_private_data;
  100. pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
  101. __func__, h, refcount_read(h->refcount), vma->vm_start,
  102. vma->vm_end);
  103. h->put(h->arg);
  104. }
  105. /*
  106. * vb2_common_vm_ops - common vm_ops used for tracking refcount of mmapped
  107. * video buffers
  108. */
  109. const struct vm_operations_struct vb2_common_vm_ops = {
  110. .open = vb2_common_vm_open,
  111. .close = vb2_common_vm_close,
  112. };
  113. EXPORT_SYMBOL_GPL(vb2_common_vm_ops);
  114. MODULE_DESCRIPTION("common memory handling routines for videobuf2");
  115. MODULE_AUTHOR("Pawel Osciak <[email protected]>");
  116. MODULE_LICENSE("GPL");