drm_mm.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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_next_entry(hole_node, node_list)->start;
  139. }
  140. /**
  141. * drm_mm_hole_node_end - computes the end of the hole following @node
  142. * @hole_node: drm_mm_node which implicitly tracks the following hole
  143. *
  144. * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
  145. * inspect holes themselves. Drivers must check first whether a hole indeed
  146. * follows by looking at node->hole_follows.
  147. *
  148. * Returns:
  149. * End of the subsequent hole.
  150. */
  151. static inline u64 drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  152. {
  153. return __drm_mm_hole_node_end(hole_node);
  154. }
  155. /**
  156. * drm_mm_for_each_node - iterator to walk over all allocated nodes
  157. * @entry: drm_mm_node structure to assign to in each iteration step
  158. * @mm: drm_mm allocator to walk
  159. *
  160. * This iterator walks over all nodes in the range allocator. It is implemented
  161. * with list_for_each, so not save against removal of elements.
  162. */
  163. #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
  164. &(mm)->head_node.node_list, \
  165. node_list)
  166. #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
  167. for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
  168. &entry->hole_stack != &(mm)->hole_stack ? \
  169. hole_start = drm_mm_hole_node_start(entry), \
  170. hole_end = drm_mm_hole_node_end(entry), \
  171. 1 : 0; \
  172. entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
  173. /**
  174. * drm_mm_for_each_hole - iterator to walk over all holes
  175. * @entry: drm_mm_node used internally to track progress
  176. * @mm: drm_mm allocator to walk
  177. * @hole_start: ulong variable to assign the hole start to on each iteration
  178. * @hole_end: ulong variable to assign the hole end to on each iteration
  179. *
  180. * This iterator walks over all holes in the range allocator. It is implemented
  181. * with list_for_each, so not save against removal of elements. @entry is used
  182. * internally and will not reflect a real drm_mm_node for the very first hole.
  183. * Hence users of this iterator may not access it.
  184. *
  185. * Implementation Note:
  186. * We need to inline list_for_each_entry in order to be able to set hole_start
  187. * and hole_end on each iteration while keeping the macro sane.
  188. *
  189. * The __drm_mm_for_each_hole version is similar, but with added support for
  190. * going backwards.
  191. */
  192. #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
  193. __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0)
  194. /*
  195. * Basic range manager support (drm_mm.c)
  196. */
  197. int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
  198. int drm_mm_insert_node_generic(struct drm_mm *mm,
  199. struct drm_mm_node *node,
  200. u64 size,
  201. unsigned alignment,
  202. unsigned long color,
  203. enum drm_mm_search_flags sflags,
  204. enum drm_mm_allocator_flags aflags);
  205. /**
  206. * drm_mm_insert_node - search for space and insert @node
  207. * @mm: drm_mm to allocate from
  208. * @node: preallocate node to insert
  209. * @size: size of the allocation
  210. * @alignment: alignment of the allocation
  211. * @flags: flags to fine-tune the allocation
  212. *
  213. * This is a simplified version of drm_mm_insert_node_generic() with @color set
  214. * to 0.
  215. *
  216. * The preallocated node must be cleared to 0.
  217. *
  218. * Returns:
  219. * 0 on success, -ENOSPC if there's no suitable hole.
  220. */
  221. static inline int drm_mm_insert_node(struct drm_mm *mm,
  222. struct drm_mm_node *node,
  223. u64 size,
  224. unsigned alignment,
  225. enum drm_mm_search_flags flags)
  226. {
  227. return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags,
  228. DRM_MM_CREATE_DEFAULT);
  229. }
  230. int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
  231. struct drm_mm_node *node,
  232. u64 size,
  233. unsigned alignment,
  234. unsigned long color,
  235. u64 start,
  236. u64 end,
  237. enum drm_mm_search_flags sflags,
  238. enum drm_mm_allocator_flags aflags);
  239. /**
  240. * drm_mm_insert_node_in_range - ranged search for space and insert @node
  241. * @mm: drm_mm to allocate from
  242. * @node: preallocate node to insert
  243. * @size: size of the allocation
  244. * @alignment: alignment of the allocation
  245. * @start: start of the allowed range for this node
  246. * @end: end of the allowed range for this node
  247. * @flags: flags to fine-tune the allocation
  248. *
  249. * This is a simplified version of drm_mm_insert_node_in_range_generic() with
  250. * @color set to 0.
  251. *
  252. * The preallocated node must be cleared to 0.
  253. *
  254. * Returns:
  255. * 0 on success, -ENOSPC if there's no suitable hole.
  256. */
  257. static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
  258. struct drm_mm_node *node,
  259. u64 size,
  260. unsigned alignment,
  261. u64 start,
  262. u64 end,
  263. enum drm_mm_search_flags flags)
  264. {
  265. return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
  266. 0, start, end, flags,
  267. DRM_MM_CREATE_DEFAULT);
  268. }
  269. void drm_mm_remove_node(struct drm_mm_node *node);
  270. void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  271. void drm_mm_init(struct drm_mm *mm,
  272. u64 start,
  273. u64 size);
  274. void drm_mm_takedown(struct drm_mm *mm);
  275. bool drm_mm_clean(struct drm_mm *mm);
  276. void drm_mm_init_scan(struct drm_mm *mm,
  277. u64 size,
  278. unsigned alignment,
  279. unsigned long color);
  280. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  281. u64 size,
  282. unsigned alignment,
  283. unsigned long color,
  284. u64 start,
  285. u64 end);
  286. bool drm_mm_scan_add_block(struct drm_mm_node *node);
  287. bool drm_mm_scan_remove_block(struct drm_mm_node *node);
  288. void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
  289. #ifdef CONFIG_DEBUG_FS
  290. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
  291. #endif
  292. #endif