drm_vma_manager.h 7.6 KB

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