drm_vma_manager.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef __DRM_VMA_MANAGER_H__
  2. #define __DRM_VMA_MANAGER_H__
  3. /*
  4. * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <drm/drm_mm.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/rbtree.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/types.h>
  30. struct drm_vma_offset_node {
  31. struct drm_mm_node vm_node;
  32. struct rb_node vm_rb;
  33. };
  34. struct drm_vma_offset_manager {
  35. rwlock_t vm_lock;
  36. struct rb_root vm_addr_space_rb;
  37. struct drm_mm vm_addr_space_mm;
  38. };
  39. void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
  40. unsigned long page_offset, unsigned long size);
  41. void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
  42. struct drm_vma_offset_node *drm_vma_offset_lookup(struct drm_vma_offset_manager *mgr,
  43. unsigned long start,
  44. unsigned long pages);
  45. struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
  46. unsigned long start,
  47. unsigned long pages);
  48. int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
  49. struct drm_vma_offset_node *node, unsigned long pages);
  50. void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
  51. struct drm_vma_offset_node *node);
  52. /**
  53. * drm_vma_offset_exact_lookup() - Look up node by exact address
  54. * @mgr: Manager object
  55. * @start: Start address (page-based, not byte-based)
  56. * @pages: Size of object (page-based)
  57. *
  58. * Same as drm_vma_offset_lookup() but does not allow any offset into the node.
  59. * It only returns the exact object with the given start address.
  60. *
  61. * RETURNS:
  62. * Node at exact start address @start.
  63. */
  64. static inline struct drm_vma_offset_node *
  65. drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *mgr,
  66. unsigned long start,
  67. unsigned long pages)
  68. {
  69. struct drm_vma_offset_node *node;
  70. node = drm_vma_offset_lookup(mgr, start, pages);
  71. return (node && node->vm_node.start == start) ? node : NULL;
  72. }
  73. /**
  74. * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
  75. * @mgr: Manager object
  76. *
  77. * Lock VMA manager for extended lookups. Only *_locked() VMA function calls
  78. * are allowed while holding this lock. All other contexts are blocked from VMA
  79. * until the lock is released via drm_vma_offset_unlock_lookup().
  80. *
  81. * Use this if you need to take a reference to the objects returned by
  82. * drm_vma_offset_lookup_locked() before releasing this lock again.
  83. *
  84. * This lock must not be used for anything else than extended lookups. You must
  85. * not call any other VMA helpers while holding this lock.
  86. *
  87. * Note: You're in atomic-context while holding this lock!
  88. *
  89. * Example:
  90. * drm_vma_offset_lock_lookup(mgr);
  91. * node = drm_vma_offset_lookup_locked(mgr);
  92. * if (node)
  93. * kref_get_unless_zero(container_of(node, sth, entr));
  94. * drm_vma_offset_unlock_lookup(mgr);
  95. */
  96. static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
  97. {
  98. read_lock(&mgr->vm_lock);
  99. }
  100. /**
  101. * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
  102. * @mgr: Manager object
  103. *
  104. * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
  105. */
  106. static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
  107. {
  108. read_unlock(&mgr->vm_lock);
  109. }
  110. /**
  111. * drm_vma_node_reset() - Initialize or reset node object
  112. * @node: Node to initialize or reset
  113. *
  114. * Reset a node to its initial state. This must be called if @node isn't
  115. * already cleared (eg., via kzalloc) before using it with any VMA offset
  116. * manager.
  117. *
  118. * This must not be called on an already allocated node, or you will leak
  119. * memory.
  120. */
  121. static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
  122. {
  123. memset(node, 0, sizeof(*node));
  124. }
  125. /**
  126. * drm_vma_node_start() - Return start address for page-based addressing
  127. * @node: Node to inspect
  128. *
  129. * Return the start address of the given node. This can be used as offset into
  130. * the linear VM space that is provided by the VMA offset manager. Note that
  131. * this can only be used for page-based addressing. If you need a proper offset
  132. * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
  133. * drm_vma_node_offset_addr() helper instead.
  134. *
  135. * RETURNS:
  136. * Start address of @node for page-based addressing. 0 if the node does not
  137. * have an offset allocated.
  138. */
  139. static inline unsigned long drm_vma_node_start(struct drm_vma_offset_node *node)
  140. {
  141. return node->vm_node.start;
  142. }
  143. /**
  144. * drm_vma_node_size() - Return size (page-based)
  145. * @node: Node to inspect
  146. *
  147. * Return the size as number of pages for the given node. This is the same size
  148. * that was passed to drm_vma_offset_add(). If no offset is allocated for the
  149. * node, this is 0.
  150. *
  151. * RETURNS:
  152. * Size of @node as number of pages. 0 if the node does not have an offset
  153. * allocated.
  154. */
  155. static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
  156. {
  157. return node->vm_node.size;
  158. }
  159. /**
  160. * drm_vma_node_has_offset() - Check whether node is added to offset manager
  161. * @node: Node to be checked
  162. *
  163. * RETURNS:
  164. * true iff the node was previously allocated an offset and added to
  165. * an vma offset manager.
  166. */
  167. static inline bool drm_vma_node_has_offset(struct drm_vma_offset_node *node)
  168. {
  169. return drm_mm_node_allocated(&node->vm_node);
  170. }
  171. /**
  172. * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
  173. * @node: Linked offset node
  174. *
  175. * Same as drm_vma_node_start() but returns the address as a valid offset that
  176. * can be used for user-space mappings during mmap().
  177. * This must not be called on unlinked nodes.
  178. *
  179. * RETURNS:
  180. * Offset of @node for byte-based addressing. 0 if the node does not have an
  181. * object allocated.
  182. */
  183. static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
  184. {
  185. return ((__u64)node->vm_node.start) << PAGE_SHIFT;
  186. }
  187. /**
  188. * drm_vma_node_unmap() - Unmap offset node
  189. * @node: Offset node
  190. * @file_mapping: Address space to unmap @node from
  191. *
  192. * Unmap all userspace mappings for a given offset node. The mappings must be
  193. * associated with the @file_mapping address-space. If no offset exists or
  194. * the address-space is invalid, nothing is done.
  195. *
  196. * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
  197. * is not called on this node concurrently.
  198. */
  199. static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
  200. struct address_space *file_mapping)
  201. {
  202. if (file_mapping && drm_vma_node_has_offset(node))
  203. unmap_mapping_range(file_mapping,
  204. drm_vma_node_offset_addr(node),
  205. drm_vma_node_size(node) << PAGE_SHIFT, 1);
  206. }
  207. #endif /* __DRM_VMA_MANAGER_H__ */