drm_mm.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/list.h>
  38. #ifdef CONFIG_DEBUG_FS
  39. #include <linux/seq_file.h>
  40. #endif
  41. struct drm_mm_node {
  42. struct list_head node_list;
  43. struct list_head hole_stack;
  44. unsigned hole_follows : 1;
  45. unsigned scanned_block : 1;
  46. unsigned scanned_prev_free : 1;
  47. unsigned scanned_next_free : 1;
  48. unsigned scanned_preceeds_hole : 1;
  49. unsigned allocated : 1;
  50. unsigned long color;
  51. unsigned long start;
  52. unsigned long size;
  53. struct drm_mm *mm;
  54. };
  55. struct drm_mm {
  56. /* List of all memory nodes that immediately precede a free hole. */
  57. struct list_head hole_stack;
  58. /* head_node.node_list is the list of all memory nodes, ordered
  59. * according to the (increasing) start address of the memory node. */
  60. struct drm_mm_node head_node;
  61. struct list_head unused_nodes;
  62. int num_unused;
  63. spinlock_t unused_lock;
  64. unsigned int scan_check_range : 1;
  65. unsigned scan_alignment;
  66. unsigned long scan_color;
  67. unsigned long scan_size;
  68. unsigned long scan_hit_start;
  69. unsigned long scan_hit_end;
  70. unsigned scanned_blocks;
  71. unsigned long scan_start;
  72. unsigned long scan_end;
  73. struct drm_mm_node *prev_scanned_node;
  74. void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
  75. unsigned long *start, unsigned long *end);
  76. };
  77. static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
  78. {
  79. return node->allocated;
  80. }
  81. static inline bool drm_mm_initialized(struct drm_mm *mm)
  82. {
  83. return mm->hole_stack.next;
  84. }
  85. static inline unsigned long __drm_mm_hole_node_start(struct drm_mm_node *hole_node)
  86. {
  87. return hole_node->start + hole_node->size;
  88. }
  89. static inline unsigned long drm_mm_hole_node_start(struct drm_mm_node *hole_node)
  90. {
  91. BUG_ON(!hole_node->hole_follows);
  92. return __drm_mm_hole_node_start(hole_node);
  93. }
  94. static inline unsigned long __drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  95. {
  96. return list_entry(hole_node->node_list.next,
  97. struct drm_mm_node, node_list)->start;
  98. }
  99. static inline unsigned long drm_mm_hole_node_end(struct drm_mm_node *hole_node)
  100. {
  101. return __drm_mm_hole_node_end(hole_node);
  102. }
  103. #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
  104. &(mm)->head_node.node_list, \
  105. node_list)
  106. #define drm_mm_for_each_scanned_node_reverse(entry, n, mm) \
  107. for (entry = (mm)->prev_scanned_node, \
  108. next = entry ? list_entry(entry->node_list.next, \
  109. struct drm_mm_node, node_list) : NULL; \
  110. entry != NULL; entry = next, \
  111. next = entry ? list_entry(entry->node_list.next, \
  112. struct drm_mm_node, node_list) : NULL) \
  113. /* Note that we need to unroll list_for_each_entry in order to inline
  114. * setting hole_start and hole_end on each iteration and keep the
  115. * macro sane.
  116. */
  117. #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
  118. for (entry = list_entry((mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
  119. &entry->hole_stack != &(mm)->hole_stack ? \
  120. hole_start = drm_mm_hole_node_start(entry), \
  121. hole_end = drm_mm_hole_node_end(entry), \
  122. 1 : 0; \
  123. entry = list_entry(entry->hole_stack.next, struct drm_mm_node, hole_stack))
  124. /*
  125. * Basic range manager support (drm_mm.c)
  126. */
  127. extern int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
  128. extern struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
  129. unsigned long size,
  130. unsigned alignment,
  131. unsigned long color,
  132. int atomic);
  133. extern struct drm_mm_node *drm_mm_get_block_range_generic(
  134. struct drm_mm_node *node,
  135. unsigned long size,
  136. unsigned alignment,
  137. unsigned long color,
  138. unsigned long start,
  139. unsigned long end,
  140. int atomic);
  141. static inline struct drm_mm_node *drm_mm_get_block(struct drm_mm_node *parent,
  142. unsigned long size,
  143. unsigned alignment)
  144. {
  145. return drm_mm_get_block_generic(parent, size, alignment, 0, 0);
  146. }
  147. static inline struct drm_mm_node *drm_mm_get_block_atomic(struct drm_mm_node *parent,
  148. unsigned long size,
  149. unsigned alignment)
  150. {
  151. return drm_mm_get_block_generic(parent, size, alignment, 0, 1);
  152. }
  153. static inline struct drm_mm_node *drm_mm_get_block_range(
  154. struct drm_mm_node *parent,
  155. unsigned long size,
  156. unsigned alignment,
  157. unsigned long start,
  158. unsigned long end)
  159. {
  160. return drm_mm_get_block_range_generic(parent, size, alignment, 0,
  161. start, end, 0);
  162. }
  163. static inline struct drm_mm_node *drm_mm_get_color_block_range(
  164. struct drm_mm_node *parent,
  165. unsigned long size,
  166. unsigned alignment,
  167. unsigned long color,
  168. unsigned long start,
  169. unsigned long end)
  170. {
  171. return drm_mm_get_block_range_generic(parent, size, alignment, color,
  172. start, end, 0);
  173. }
  174. static inline struct drm_mm_node *drm_mm_get_block_atomic_range(
  175. struct drm_mm_node *parent,
  176. unsigned long size,
  177. unsigned alignment,
  178. unsigned long start,
  179. unsigned long end)
  180. {
  181. return drm_mm_get_block_range_generic(parent, size, alignment, 0,
  182. start, end, 1);
  183. }
  184. extern int drm_mm_insert_node(struct drm_mm *mm,
  185. struct drm_mm_node *node,
  186. unsigned long size,
  187. unsigned alignment);
  188. extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
  189. struct drm_mm_node *node,
  190. unsigned long size,
  191. unsigned alignment,
  192. unsigned long start,
  193. unsigned long end);
  194. extern int drm_mm_insert_node_generic(struct drm_mm *mm,
  195. struct drm_mm_node *node,
  196. unsigned long size,
  197. unsigned alignment,
  198. unsigned long color);
  199. extern int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
  200. struct drm_mm_node *node,
  201. unsigned long size,
  202. unsigned alignment,
  203. unsigned long color,
  204. unsigned long start,
  205. unsigned long end);
  206. extern void drm_mm_put_block(struct drm_mm_node *cur);
  207. extern void drm_mm_remove_node(struct drm_mm_node *node);
  208. extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  209. extern struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
  210. unsigned long size,
  211. unsigned alignment,
  212. unsigned long color,
  213. bool best_match);
  214. extern struct drm_mm_node *drm_mm_search_free_in_range_generic(
  215. const struct drm_mm *mm,
  216. unsigned long size,
  217. unsigned alignment,
  218. unsigned long color,
  219. unsigned long start,
  220. unsigned long end,
  221. bool best_match);
  222. static inline struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  223. unsigned long size,
  224. unsigned alignment,
  225. bool best_match)
  226. {
  227. return drm_mm_search_free_generic(mm,size, alignment, 0, best_match);
  228. }
  229. static inline struct drm_mm_node *drm_mm_search_free_in_range(
  230. const struct drm_mm *mm,
  231. unsigned long size,
  232. unsigned alignment,
  233. unsigned long start,
  234. unsigned long end,
  235. bool best_match)
  236. {
  237. return drm_mm_search_free_in_range_generic(mm, size, alignment, 0,
  238. start, end, best_match);
  239. }
  240. static inline struct drm_mm_node *drm_mm_search_free_color(const struct drm_mm *mm,
  241. unsigned long size,
  242. unsigned alignment,
  243. unsigned long color,
  244. bool best_match)
  245. {
  246. return drm_mm_search_free_generic(mm,size, alignment, color, best_match);
  247. }
  248. static inline struct drm_mm_node *drm_mm_search_free_in_range_color(
  249. const struct drm_mm *mm,
  250. unsigned long size,
  251. unsigned alignment,
  252. unsigned long color,
  253. unsigned long start,
  254. unsigned long end,
  255. bool best_match)
  256. {
  257. return drm_mm_search_free_in_range_generic(mm, size, alignment, color,
  258. start, end, best_match);
  259. }
  260. extern int drm_mm_init(struct drm_mm *mm,
  261. unsigned long start,
  262. unsigned long size);
  263. extern void drm_mm_takedown(struct drm_mm *mm);
  264. extern int drm_mm_clean(struct drm_mm *mm);
  265. extern int drm_mm_pre_get(struct drm_mm *mm);
  266. static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
  267. {
  268. return block->mm;
  269. }
  270. void drm_mm_init_scan(struct drm_mm *mm,
  271. unsigned long size,
  272. unsigned alignment,
  273. unsigned long color);
  274. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  275. unsigned long size,
  276. unsigned alignment,
  277. unsigned long color,
  278. unsigned long start,
  279. unsigned long end);
  280. int drm_mm_scan_add_block(struct drm_mm_node *node);
  281. int drm_mm_scan_remove_block(struct drm_mm_node *node);
  282. extern void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
  283. #ifdef CONFIG_DEBUG_FS
  284. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
  285. #endif
  286. #endif