drm_mm.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/kernel.h>
  39. #include <linux/list.h>
  40. #include <linux/spinlock.h>
  41. #ifdef CONFIG_DEBUG_FS
  42. #include <linux/seq_file.h>
  43. #endif
  44. enum drm_mm_search_flags {
  45. DRM_MM_SEARCH_DEFAULT = 0,
  46. DRM_MM_SEARCH_BEST = 1 << 0,
  47. DRM_MM_SEARCH_BELOW = 1 << 1,
  48. };
  49. enum drm_mm_allocator_flags {
  50. DRM_MM_CREATE_DEFAULT = 0,
  51. DRM_MM_CREATE_TOP = 1 << 0,
  52. };
  53. #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT
  54. #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP
  55. struct drm_mm_node {
  56. struct list_head node_list;
  57. struct list_head hole_stack;
  58. unsigned hole_follows : 1;
  59. unsigned scanned_block : 1;
  60. unsigned scanned_prev_free : 1;
  61. unsigned scanned_next_free : 1;
  62. unsigned scanned_preceeds_hole : 1;
  63. unsigned allocated : 1;
  64. unsigned long color;
  65. u64 start;
  66. u64 size;
  67. struct drm_mm *mm;
  68. };
  69. struct drm_mm {
  70. /* List of all memory nodes that immediately precede a free hole. */
  71. struct list_head hole_stack;
  72. /* head_node.node_list is the list of all memory nodes, ordered
  73. * according to the (increasing) start address of the memory node. */
  74. struct drm_mm_node head_node;
  75. unsigned int scan_check_range : 1;
  76. unsigned scan_alignment;
  77. unsigned long scan_color;
  78. u64 scan_size;
  79. u64 scan_hit_start;
  80. u64 scan_hit_end;
  81. unsigned scanned_blocks;
  82. u64 scan_start;
  83. u64 scan_end;
  84. struct drm_mm_node *prev_scanned_node;
  85. void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
  86. u64 *start, u64 *end);
  87. };
  88. /**
  89. * drm_mm_node_allocated - checks whether a node is allocated
  90. * @node: drm_mm_node to check
  91. *
  92. * Drivers should use this helpers for proper encapusulation of drm_mm
  93. * internals.
  94. *
  95. * Returns:
  96. * True if the @node is allocated.
  97. */
  98. static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
  99. {
  100. return node->allocated;
  101. }
  102. /**
  103. * drm_mm_initialized - checks whether an allocator is initialized
  104. * @mm: drm_mm to check
  105. *
  106. * Drivers should use this helpers for proper encapusulation of drm_mm
  107. * internals.
  108. *
  109. * Returns:
  110. * True if the @mm is initialized.
  111. */
  112. static inline bool drm_mm_initialized(struct drm_mm *mm)
  113. {
  114. return mm->hole_stack.next;
  115. }
  116. static inline u64 __drm_mm_hole_node_start(struct drm_mm_node *hole_node)
  117. {
  118. return hole_node->start + hole_node->size;
  119. }
  120. /**
  121. * drm_mm_hole_node_start - computes the start of the hole following @node
  122. * @hole_node: drm_mm_node which implicitly tracks the following hole
  123. *
  124. * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
  125. * inspect holes themselves. Drivers must check first whether a hole indeed
  126. * follows by looking at node->hole_follows.
  127. *
  128. * Returns:
  129. * Start of the subsequent hole.
  130. */
  131. static inline u64 drm_mm_hole_node_start(struct drm_mm_node *hole_node)
  132. {
  133. BUG_ON(!hole_node->hole_follows);
  134. return __drm_mm_hole_node_start(hole_node);
  135. }
  136. static inline u64 __drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  137. {
  138. return list_entry(hole_node->node_list.next,
  139. struct drm_mm_node, node_list)->start;
  140. }
  141. /**
  142. * drm_mm_hole_node_end - computes the end of the hole following @node
  143. * @hole_node: drm_mm_node which implicitly tracks the following hole
  144. *
  145. * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
  146. * inspect holes themselves. Drivers must check first whether a hole indeed
  147. * follows by looking at node->hole_follows.
  148. *
  149. * Returns:
  150. * End of the subsequent hole.
  151. */
  152. static inline u64 drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  153. {
  154. return __drm_mm_hole_node_end(hole_node);
  155. }
  156. /**
  157. * drm_mm_for_each_node - iterator to walk over all allocated nodes
  158. * @entry: drm_mm_node structure to assign to in each iteration step
  159. * @mm: drm_mm allocator to walk
  160. *
  161. * This iterator walks over all nodes in the range allocator. It is implemented
  162. * with list_for_each, so not save against removal of elements.
  163. */
  164. #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
  165. &(mm)->head_node.node_list, \
  166. node_list)
  167. /**
  168. * drm_mm_for_each_hole - iterator to walk over all holes
  169. * @entry: drm_mm_node used internally to track progress
  170. * @mm: drm_mm allocator to walk
  171. * @hole_start: ulong variable to assign the hole start to on each iteration
  172. * @hole_end: ulong variable to assign the hole end to on each iteration
  173. *
  174. * This iterator walks over all holes in the range allocator. It is implemented
  175. * with list_for_each, so not save against removal of elements. @entry is used
  176. * internally and will not reflect a real drm_mm_node for the very first hole.
  177. * Hence users of this iterator may not access it.
  178. *
  179. * Implementation Note:
  180. * We need to inline list_for_each_entry in order to be able to set hole_start
  181. * and hole_end on each iteration while keeping the macro sane.
  182. *
  183. * The __drm_mm_for_each_hole version is similar, but with added support for
  184. * going backwards.
  185. */
  186. #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
  187. for (entry = list_entry((mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
  188. &entry->hole_stack != &(mm)->hole_stack ? \
  189. hole_start = drm_mm_hole_node_start(entry), \
  190. hole_end = drm_mm_hole_node_end(entry), \
  191. 1 : 0; \
  192. entry = list_entry(entry->hole_stack.next, struct drm_mm_node, hole_stack))
  193. #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
  194. for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
  195. &entry->hole_stack != &(mm)->hole_stack ? \
  196. hole_start = drm_mm_hole_node_start(entry), \
  197. hole_end = drm_mm_hole_node_end(entry), \
  198. 1 : 0; \
  199. entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
  200. /*
  201. * Basic range manager support (drm_mm.c)
  202. */
  203. int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
  204. int drm_mm_insert_node_generic(struct drm_mm *mm,
  205. struct drm_mm_node *node,
  206. u64 size,
  207. unsigned alignment,
  208. unsigned long color,
  209. enum drm_mm_search_flags sflags,
  210. enum drm_mm_allocator_flags aflags);
  211. /**
  212. * drm_mm_insert_node - search for space and insert @node
  213. * @mm: drm_mm to allocate from
  214. * @node: preallocate node to insert
  215. * @size: size of the allocation
  216. * @alignment: alignment of the allocation
  217. * @flags: flags to fine-tune the allocation
  218. *
  219. * This is a simplified version of drm_mm_insert_node_generic() with @color set
  220. * to 0.
  221. *
  222. * The preallocated node must be cleared to 0.
  223. *
  224. * Returns:
  225. * 0 on success, -ENOSPC if there's no suitable hole.
  226. */
  227. static inline int drm_mm_insert_node(struct drm_mm *mm,
  228. struct drm_mm_node *node,
  229. u64 size,
  230. unsigned alignment,
  231. enum drm_mm_search_flags flags)
  232. {
  233. return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags,
  234. DRM_MM_CREATE_DEFAULT);
  235. }
  236. int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
  237. struct drm_mm_node *node,
  238. u64 size,
  239. unsigned alignment,
  240. unsigned long color,
  241. u64 start,
  242. u64 end,
  243. enum drm_mm_search_flags sflags,
  244. enum drm_mm_allocator_flags aflags);
  245. /**
  246. * drm_mm_insert_node_in_range - ranged search for space and insert @node
  247. * @mm: drm_mm to allocate from
  248. * @node: preallocate node to insert
  249. * @size: size of the allocation
  250. * @alignment: alignment of the allocation
  251. * @start: start of the allowed range for this node
  252. * @end: end of the allowed range for this node
  253. * @flags: flags to fine-tune the allocation
  254. *
  255. * This is a simplified version of drm_mm_insert_node_in_range_generic() with
  256. * @color set to 0.
  257. *
  258. * The preallocated node must be cleared to 0.
  259. *
  260. * Returns:
  261. * 0 on success, -ENOSPC if there's no suitable hole.
  262. */
  263. static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
  264. struct drm_mm_node *node,
  265. u64 size,
  266. unsigned alignment,
  267. u64 start,
  268. u64 end,
  269. enum drm_mm_search_flags flags)
  270. {
  271. return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
  272. 0, start, end, flags,
  273. DRM_MM_CREATE_DEFAULT);
  274. }
  275. void drm_mm_remove_node(struct drm_mm_node *node);
  276. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  277. void drm_mm_init(struct drm_mm *mm,
  278. u64 start,
  279. u64 size);
  280. void drm_mm_takedown(struct drm_mm *mm);
  281. bool drm_mm_clean(struct drm_mm *mm);
  282. void drm_mm_init_scan(struct drm_mm *mm,
  283. u64 size,
  284. unsigned alignment,
  285. unsigned long color);
  286. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  287. u64 size,
  288. unsigned alignment,
  289. unsigned long color,
  290. u64 start,
  291. u64 end);
  292. bool drm_mm_scan_add_block(struct drm_mm_node *node);
  293. bool drm_mm_scan_remove_block(struct drm_mm_node *node);
  294. void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
  295. #ifdef CONFIG_DEBUG_FS
  296. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
  297. #endif
  298. #endif