drm_mm.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**************************************************************************
  2. *
  3. * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors:
  30. * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
  31. */
  32. #ifndef _DRM_MM_H_
  33. #define _DRM_MM_H_
  34. /*
  35. * Generic range manager structs
  36. */
  37. #include <linux/bug.h>
  38. #include <linux/rbtree.h>
  39. #include <linux/kernel.h>
  40. #include <linux/list.h>
  41. #include <linux/spinlock.h>
  42. #ifdef CONFIG_DEBUG_FS
  43. #include <linux/seq_file.h>
  44. #endif
  45. enum drm_mm_search_flags {
  46. DRM_MM_SEARCH_DEFAULT = 0,
  47. DRM_MM_SEARCH_BEST = 1 << 0,
  48. DRM_MM_SEARCH_BELOW = 1 << 1,
  49. };
  50. enum drm_mm_allocator_flags {
  51. DRM_MM_CREATE_DEFAULT = 0,
  52. DRM_MM_CREATE_TOP = 1 << 0,
  53. };
  54. #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT
  55. #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP
  56. struct drm_mm_node {
  57. struct list_head node_list;
  58. struct list_head hole_stack;
  59. struct rb_node rb;
  60. unsigned hole_follows : 1;
  61. unsigned scanned_block : 1;
  62. unsigned scanned_prev_free : 1;
  63. unsigned scanned_next_free : 1;
  64. unsigned scanned_preceeds_hole : 1;
  65. unsigned allocated : 1;
  66. unsigned long color;
  67. u64 start;
  68. u64 size;
  69. u64 __subtree_last;
  70. struct drm_mm *mm;
  71. };
  72. struct drm_mm {
  73. /* List of all memory nodes that immediately precede a free hole. */
  74. struct list_head hole_stack;
  75. /* head_node.node_list is the list of all memory nodes, ordered
  76. * according to the (increasing) start address of the memory node. */
  77. struct drm_mm_node head_node;
  78. /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
  79. struct rb_root interval_tree;
  80. unsigned int scan_check_range : 1;
  81. unsigned scan_alignment;
  82. unsigned long scan_color;
  83. u64 scan_size;
  84. u64 scan_hit_start;
  85. u64 scan_hit_end;
  86. unsigned scanned_blocks;
  87. u64 scan_start;
  88. u64 scan_end;
  89. struct drm_mm_node *prev_scanned_node;
  90. void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
  91. u64 *start, u64 *end);
  92. };
  93. /**
  94. * drm_mm_node_allocated - checks whether a node is allocated
  95. * @node: drm_mm_node to check
  96. *
  97. * Drivers should use this helpers for proper encapusulation of drm_mm
  98. * internals.
  99. *
  100. * Returns:
  101. * True if the @node is allocated.
  102. */
  103. static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
  104. {
  105. return node->allocated;
  106. }
  107. /**
  108. * drm_mm_initialized - checks whether an allocator is initialized
  109. * @mm: drm_mm to check
  110. *
  111. * Drivers should use this helpers for proper encapusulation of drm_mm
  112. * internals.
  113. *
  114. * Returns:
  115. * True if the @mm is initialized.
  116. */
  117. static inline bool drm_mm_initialized(struct drm_mm *mm)
  118. {
  119. return mm->hole_stack.next;
  120. }
  121. static inline u64 __drm_mm_hole_node_start(struct drm_mm_node *hole_node)
  122. {
  123. return hole_node->start + hole_node->size;
  124. }
  125. /**
  126. * drm_mm_hole_node_start - computes the start of the hole following @node
  127. * @hole_node: drm_mm_node which implicitly tracks the following hole
  128. *
  129. * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
  130. * inspect holes themselves. Drivers must check first whether a hole indeed
  131. * follows by looking at node->hole_follows.
  132. *
  133. * Returns:
  134. * Start of the subsequent hole.
  135. */
  136. static inline u64 drm_mm_hole_node_start(struct drm_mm_node *hole_node)
  137. {
  138. BUG_ON(!hole_node->hole_follows);
  139. return __drm_mm_hole_node_start(hole_node);
  140. }
  141. static inline u64 __drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  142. {
  143. return list_next_entry(hole_node, node_list)->start;
  144. }
  145. /**
  146. * drm_mm_hole_node_end - computes the end of the hole following @node
  147. * @hole_node: drm_mm_node which implicitly tracks the following hole
  148. *
  149. * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
  150. * inspect holes themselves. Drivers must check first whether a hole indeed
  151. * follows by looking at node->hole_follows.
  152. *
  153. * Returns:
  154. * End of the subsequent hole.
  155. */
  156. static inline u64 drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  157. {
  158. return __drm_mm_hole_node_end(hole_node);
  159. }
  160. /**
  161. * drm_mm_for_each_node - iterator to walk over all allocated nodes
  162. * @entry: drm_mm_node structure to assign to in each iteration step
  163. * @mm: drm_mm allocator to walk
  164. *
  165. * This iterator walks over all nodes in the range allocator. It is implemented
  166. * with list_for_each, so not save against removal of elements.
  167. */
  168. #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
  169. &(mm)->head_node.node_list, \
  170. node_list)
  171. #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
  172. for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
  173. &entry->hole_stack != &(mm)->hole_stack ? \
  174. hole_start = drm_mm_hole_node_start(entry), \
  175. hole_end = drm_mm_hole_node_end(entry), \
  176. 1 : 0; \
  177. entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
  178. /**
  179. * drm_mm_for_each_hole - iterator to walk over all holes
  180. * @entry: drm_mm_node used internally to track progress
  181. * @mm: drm_mm allocator to walk
  182. * @hole_start: ulong variable to assign the hole start to on each iteration
  183. * @hole_end: ulong variable to assign the hole end to on each iteration
  184. *
  185. * This iterator walks over all holes in the range allocator. It is implemented
  186. * with list_for_each, so not save against removal of elements. @entry is used
  187. * internally and will not reflect a real drm_mm_node for the very first hole.
  188. * Hence users of this iterator may not access it.
  189. *
  190. * Implementation Note:
  191. * We need to inline list_for_each_entry in order to be able to set hole_start
  192. * and hole_end on each iteration while keeping the macro sane.
  193. *
  194. * The __drm_mm_for_each_hole version is similar, but with added support for
  195. * going backwards.
  196. */
  197. #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
  198. __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0)
  199. /*
  200. * Basic range manager support (drm_mm.c)
  201. */
  202. int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
  203. int drm_mm_insert_node_generic(struct drm_mm *mm,
  204. struct drm_mm_node *node,
  205. u64 size,
  206. unsigned alignment,
  207. unsigned long color,
  208. enum drm_mm_search_flags sflags,
  209. enum drm_mm_allocator_flags aflags);
  210. /**
  211. * drm_mm_insert_node - search for space and insert @node
  212. * @mm: drm_mm to allocate from
  213. * @node: preallocate node to insert
  214. * @size: size of the allocation
  215. * @alignment: alignment of the allocation
  216. * @flags: flags to fine-tune the allocation
  217. *
  218. * This is a simplified version of drm_mm_insert_node_generic() with @color set
  219. * to 0.
  220. *
  221. * The preallocated node must be cleared to 0.
  222. *
  223. * Returns:
  224. * 0 on success, -ENOSPC if there's no suitable hole.
  225. */
  226. static inline int drm_mm_insert_node(struct drm_mm *mm,
  227. struct drm_mm_node *node,
  228. u64 size,
  229. unsigned alignment,
  230. enum drm_mm_search_flags flags)
  231. {
  232. return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags,
  233. DRM_MM_CREATE_DEFAULT);
  234. }
  235. int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
  236. struct drm_mm_node *node,
  237. u64 size,
  238. unsigned alignment,
  239. unsigned long color,
  240. u64 start,
  241. u64 end,
  242. enum drm_mm_search_flags sflags,
  243. enum drm_mm_allocator_flags aflags);
  244. /**
  245. * drm_mm_insert_node_in_range - ranged search for space and insert @node
  246. * @mm: drm_mm to allocate from
  247. * @node: preallocate node to insert
  248. * @size: size of the allocation
  249. * @alignment: alignment of the allocation
  250. * @start: start of the allowed range for this node
  251. * @end: end of the allowed range for this node
  252. * @flags: flags to fine-tune the allocation
  253. *
  254. * This is a simplified version of drm_mm_insert_node_in_range_generic() with
  255. * @color set to 0.
  256. *
  257. * The preallocated node must be cleared to 0.
  258. *
  259. * Returns:
  260. * 0 on success, -ENOSPC if there's no suitable hole.
  261. */
  262. static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
  263. struct drm_mm_node *node,
  264. u64 size,
  265. unsigned alignment,
  266. u64 start,
  267. u64 end,
  268. enum drm_mm_search_flags flags)
  269. {
  270. return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
  271. 0, start, end, flags,
  272. DRM_MM_CREATE_DEFAULT);
  273. }
  274. void drm_mm_remove_node(struct drm_mm_node *node);
  275. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  276. void drm_mm_init(struct drm_mm *mm,
  277. u64 start,
  278. u64 size);
  279. void drm_mm_takedown(struct drm_mm *mm);
  280. bool drm_mm_clean(struct drm_mm *mm);
  281. struct drm_mm_node *
  282. drm_mm_interval_first(struct drm_mm *mm, u64 start, u64 last);
  283. struct drm_mm_node *
  284. drm_mm_interval_next(struct drm_mm_node *node, u64 start, u64 last);
  285. void drm_mm_init_scan(struct drm_mm *mm,
  286. u64 size,
  287. unsigned alignment,
  288. unsigned long color);
  289. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  290. u64 size,
  291. unsigned alignment,
  292. unsigned long color,
  293. u64 start,
  294. u64 end);
  295. bool drm_mm_scan_add_block(struct drm_mm_node *node);
  296. bool drm_mm_scan_remove_block(struct drm_mm_node *node);
  297. void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
  298. #ifdef CONFIG_DEBUG_FS
  299. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
  300. #endif
  301. #endif