drm_vma_manager.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/fs.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/rbtree.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/types.h>
  31. struct drm_vma_offset_file {
  32. struct rb_node vm_rb;
  33. struct file *vm_filp;
  34. unsigned long vm_count;
  35. };
  36. struct drm_vma_offset_node {
  37. rwlock_t vm_lock;
  38. struct drm_mm_node vm_node;
  39. struct rb_node vm_rb;
  40. struct rb_root vm_files;
  41. };
  42. struct drm_vma_offset_manager {
  43. rwlock_t vm_lock;
  44. struct rb_root vm_addr_space_rb;
  45. struct drm_mm vm_addr_space_mm;
  46. };
  47. void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
  48. unsigned long page_offset, unsigned long size);
  49. void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
  50. struct drm_vma_offset_node *drm_vma_offset_lookup(struct drm_vma_offset_manager *mgr,
  51. unsigned long start,
  52. unsigned long pages);
  53. struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
  54. unsigned long start,
  55. unsigned long pages);
  56. int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
  57. struct drm_vma_offset_node *node, unsigned long pages);
  58. void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
  59. struct drm_vma_offset_node *node);
  60. int drm_vma_node_allow(struct drm_vma_offset_node *node, struct file *filp);
  61. void drm_vma_node_revoke(struct drm_vma_offset_node *node, struct file *filp);
  62. bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
  63. struct file *filp);
  64. /**
  65. * drm_vma_offset_exact_lookup() - Look up node by exact address
  66. * @mgr: Manager object
  67. * @start: Start address (page-based, not byte-based)
  68. * @pages: Size of object (page-based)
  69. *
  70. * Same as drm_vma_offset_lookup() but does not allow any offset into the node.
  71. * It only returns the exact object with the given start address.
  72. *
  73. * RETURNS:
  74. * Node at exact start address @start.
  75. */
  76. static inline struct drm_vma_offset_node *
  77. drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *mgr,
  78. unsigned long start,
  79. unsigned long pages)
  80. {
  81. struct drm_vma_offset_node *node;
  82. node = drm_vma_offset_lookup(mgr, start, pages);
  83. return (node && node->vm_node.start == start) ? node : NULL;
  84. }
  85. /**
  86. * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
  87. * @mgr: Manager object
  88. *
  89. * Lock VMA manager for extended lookups. Only *_locked() VMA function calls
  90. * are allowed while holding this lock. All other contexts are blocked from VMA
  91. * until the lock is released via drm_vma_offset_unlock_lookup().
  92. *
  93. * Use this if you need to take a reference to the objects returned by
  94. * drm_vma_offset_lookup_locked() before releasing this lock again.
  95. *
  96. * This lock must not be used for anything else than extended lookups. You must
  97. * not call any other VMA helpers while holding this lock.
  98. *
  99. * Note: You're in atomic-context while holding this lock!
  100. *
  101. * Example:
  102. * drm_vma_offset_lock_lookup(mgr);
  103. * node = drm_vma_offset_lookup_locked(mgr);
  104. * if (node)
  105. * kref_get_unless_zero(container_of(node, sth, entr));
  106. * drm_vma_offset_unlock_lookup(mgr);
  107. */
  108. static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
  109. {
  110. read_lock(&mgr->vm_lock);
  111. }
  112. /**
  113. * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
  114. * @mgr: Manager object
  115. *
  116. * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
  117. */
  118. static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
  119. {
  120. read_unlock(&mgr->vm_lock);
  121. }
  122. /**
  123. * drm_vma_node_reset() - Initialize or reset node object
  124. * @node: Node to initialize or reset
  125. *
  126. * Reset a node to its initial state. This must be called before using it with
  127. * any VMA offset manager.
  128. *
  129. * This must not be called on an already allocated node, or you will leak
  130. * memory.
  131. */
  132. static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
  133. {
  134. memset(node, 0, sizeof(*node));
  135. node->vm_files = RB_ROOT;
  136. rwlock_init(&node->vm_lock);
  137. }
  138. /**
  139. * drm_vma_node_start() - Return start address for page-based addressing
  140. * @node: Node to inspect
  141. *
  142. * Return the start address of the given node. This can be used as offset into
  143. * the linear VM space that is provided by the VMA offset manager. Note that
  144. * this can only be used for page-based addressing. If you need a proper offset
  145. * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
  146. * drm_vma_node_offset_addr() helper instead.
  147. *
  148. * RETURNS:
  149. * Start address of @node for page-based addressing. 0 if the node does not
  150. * have an offset allocated.
  151. */
  152. static inline unsigned long drm_vma_node_start(struct drm_vma_offset_node *node)
  153. {
  154. return node->vm_node.start;
  155. }
  156. /**
  157. * drm_vma_node_size() - Return size (page-based)
  158. * @node: Node to inspect
  159. *
  160. * Return the size as number of pages for the given node. This is the same size
  161. * that was passed to drm_vma_offset_add(). If no offset is allocated for the
  162. * node, this is 0.
  163. *
  164. * RETURNS:
  165. * Size of @node as number of pages. 0 if the node does not have an offset
  166. * allocated.
  167. */
  168. static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
  169. {
  170. return node->vm_node.size;
  171. }
  172. /**
  173. * drm_vma_node_has_offset() - Check whether node is added to offset manager
  174. * @node: Node to be checked
  175. *
  176. * RETURNS:
  177. * true iff the node was previously allocated an offset and added to
  178. * an vma offset manager.
  179. */
  180. static inline bool drm_vma_node_has_offset(struct drm_vma_offset_node *node)
  181. {
  182. return drm_mm_node_allocated(&node->vm_node);
  183. }
  184. /**
  185. * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
  186. * @node: Linked offset node
  187. *
  188. * Same as drm_vma_node_start() but returns the address as a valid offset that
  189. * can be used for user-space mappings during mmap().
  190. * This must not be called on unlinked nodes.
  191. *
  192. * RETURNS:
  193. * Offset of @node for byte-based addressing. 0 if the node does not have an
  194. * object allocated.
  195. */
  196. static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
  197. {
  198. return ((__u64)node->vm_node.start) << PAGE_SHIFT;
  199. }
  200. /**
  201. * drm_vma_node_unmap() - Unmap offset node
  202. * @node: Offset node
  203. * @file_mapping: Address space to unmap @node from
  204. *
  205. * Unmap all userspace mappings for a given offset node. The mappings must be
  206. * associated with the @file_mapping address-space. If no offset exists
  207. * nothing is done.
  208. *
  209. * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
  210. * is not called on this node concurrently.
  211. */
  212. static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
  213. struct address_space *file_mapping)
  214. {
  215. if (drm_vma_node_has_offset(node))
  216. unmap_mapping_range(file_mapping,
  217. drm_vma_node_offset_addr(node),
  218. drm_vma_node_size(node) << PAGE_SHIFT, 1);
  219. }
  220. /**
  221. * drm_vma_node_verify_access() - Access verification helper for TTM
  222. * @node: Offset node
  223. * @filp: Open-file
  224. *
  225. * This checks whether @filp is granted access to @node. It is the same as
  226. * drm_vma_node_is_allowed() but suitable as drop-in helper for TTM
  227. * verify_access() callbacks.
  228. *
  229. * RETURNS:
  230. * 0 if access is granted, -EACCES otherwise.
  231. */
  232. static inline int drm_vma_node_verify_access(struct drm_vma_offset_node *node,
  233. struct file *filp)
  234. {
  235. return drm_vma_node_is_allowed(node, filp) ? 0 : -EACCES;
  236. }
  237. #endif /* __DRM_VMA_MANAGER_H__ */