drm_mm.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 struct drm_mm_node *drm_mm_create_block(struct drm_mm *mm,
  128. unsigned long start,
  129. unsigned long size,
  130. bool atomic);
  131. extern struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
  132. unsigned long size,
  133. unsigned alignment,
  134. unsigned long color,
  135. int atomic);
  136. extern struct drm_mm_node *drm_mm_get_block_range_generic(
  137. struct drm_mm_node *node,
  138. unsigned long size,
  139. unsigned alignment,
  140. unsigned long color,
  141. unsigned long start,
  142. unsigned long end,
  143. int atomic);
  144. static inline struct drm_mm_node *drm_mm_get_block(struct drm_mm_node *parent,
  145. unsigned long size,
  146. unsigned alignment)
  147. {
  148. return drm_mm_get_block_generic(parent, size, alignment, 0, 0);
  149. }
  150. static inline struct drm_mm_node *drm_mm_get_block_atomic(struct drm_mm_node *parent,
  151. unsigned long size,
  152. unsigned alignment)
  153. {
  154. return drm_mm_get_block_generic(parent, size, alignment, 0, 1);
  155. }
  156. static inline struct drm_mm_node *drm_mm_get_block_range(
  157. struct drm_mm_node *parent,
  158. unsigned long size,
  159. unsigned alignment,
  160. unsigned long start,
  161. unsigned long end)
  162. {
  163. return drm_mm_get_block_range_generic(parent, size, alignment, 0,
  164. start, end, 0);
  165. }
  166. static inline struct drm_mm_node *drm_mm_get_color_block_range(
  167. struct drm_mm_node *parent,
  168. unsigned long size,
  169. unsigned alignment,
  170. unsigned long color,
  171. unsigned long start,
  172. unsigned long end)
  173. {
  174. return drm_mm_get_block_range_generic(parent, size, alignment, color,
  175. start, end, 0);
  176. }
  177. static inline struct drm_mm_node *drm_mm_get_block_atomic_range(
  178. struct drm_mm_node *parent,
  179. unsigned long size,
  180. unsigned alignment,
  181. unsigned long start,
  182. unsigned long end)
  183. {
  184. return drm_mm_get_block_range_generic(parent, size, alignment, 0,
  185. start, end, 1);
  186. }
  187. extern int drm_mm_insert_node(struct drm_mm *mm,
  188. struct drm_mm_node *node,
  189. unsigned long size,
  190. unsigned alignment);
  191. extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
  192. struct drm_mm_node *node,
  193. unsigned long size,
  194. unsigned alignment,
  195. unsigned long start,
  196. unsigned long end);
  197. extern int drm_mm_insert_node_generic(struct drm_mm *mm,
  198. struct drm_mm_node *node,
  199. unsigned long size,
  200. unsigned alignment,
  201. unsigned long color);
  202. extern int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
  203. struct drm_mm_node *node,
  204. unsigned long size,
  205. unsigned alignment,
  206. unsigned long color,
  207. unsigned long start,
  208. unsigned long end);
  209. extern void drm_mm_put_block(struct drm_mm_node *cur);
  210. extern void drm_mm_remove_node(struct drm_mm_node *node);
  211. extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  212. extern struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
  213. unsigned long size,
  214. unsigned alignment,
  215. unsigned long color,
  216. bool best_match);
  217. extern struct drm_mm_node *drm_mm_search_free_in_range_generic(
  218. const struct drm_mm *mm,
  219. unsigned long size,
  220. unsigned alignment,
  221. unsigned long color,
  222. unsigned long start,
  223. unsigned long end,
  224. bool best_match);
  225. static inline struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  226. unsigned long size,
  227. unsigned alignment,
  228. bool best_match)
  229. {
  230. return drm_mm_search_free_generic(mm,size, alignment, 0, best_match);
  231. }
  232. static inline struct drm_mm_node *drm_mm_search_free_in_range(
  233. const struct drm_mm *mm,
  234. unsigned long size,
  235. unsigned alignment,
  236. unsigned long start,
  237. unsigned long end,
  238. bool best_match)
  239. {
  240. return drm_mm_search_free_in_range_generic(mm, size, alignment, 0,
  241. start, end, best_match);
  242. }
  243. static inline struct drm_mm_node *drm_mm_search_free_color(const struct drm_mm *mm,
  244. unsigned long size,
  245. unsigned alignment,
  246. unsigned long color,
  247. bool best_match)
  248. {
  249. return drm_mm_search_free_generic(mm,size, alignment, color, best_match);
  250. }
  251. static inline struct drm_mm_node *drm_mm_search_free_in_range_color(
  252. const struct drm_mm *mm,
  253. unsigned long size,
  254. unsigned alignment,
  255. unsigned long color,
  256. unsigned long start,
  257. unsigned long end,
  258. bool best_match)
  259. {
  260. return drm_mm_search_free_in_range_generic(mm, size, alignment, color,
  261. start, end, best_match);
  262. }
  263. extern int drm_mm_init(struct drm_mm *mm,
  264. unsigned long start,
  265. unsigned long size);
  266. extern void drm_mm_takedown(struct drm_mm *mm);
  267. extern int drm_mm_clean(struct drm_mm *mm);
  268. extern int drm_mm_pre_get(struct drm_mm *mm);
  269. static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
  270. {
  271. return block->mm;
  272. }
  273. void drm_mm_init_scan(struct drm_mm *mm,
  274. unsigned long size,
  275. unsigned alignment,
  276. unsigned long color);
  277. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  278. unsigned long size,
  279. unsigned alignment,
  280. unsigned long color,
  281. unsigned long start,
  282. unsigned long end);
  283. int drm_mm_scan_add_block(struct drm_mm_node *node);
  284. int drm_mm_scan_remove_block(struct drm_mm_node *node);
  285. extern void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
  286. #ifdef CONFIG_DEBUG_FS
  287. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
  288. #endif
  289. #endif