drm_vma_manager.h 7.7 KB

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