drm_mm.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 scan_hit_size;
  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. #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
  86. &(mm)->head_node.node_list, \
  87. node_list)
  88. #define drm_mm_for_each_scanned_node_reverse(entry, n, mm) \
  89. for (entry = (mm)->prev_scanned_node, \
  90. next = entry ? list_entry(entry->node_list.next, \
  91. struct drm_mm_node, node_list) : NULL; \
  92. entry != NULL; entry = next, \
  93. next = entry ? list_entry(entry->node_list.next, \
  94. struct drm_mm_node, node_list) : NULL) \
  95. /*
  96. * Basic range manager support (drm_mm.c)
  97. */
  98. extern struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
  99. unsigned long size,
  100. unsigned alignment,
  101. unsigned long color,
  102. int atomic);
  103. extern struct drm_mm_node *drm_mm_get_block_range_generic(
  104. struct drm_mm_node *node,
  105. unsigned long size,
  106. unsigned alignment,
  107. unsigned long color,
  108. unsigned long start,
  109. unsigned long end,
  110. int atomic);
  111. static inline struct drm_mm_node *drm_mm_get_block(struct drm_mm_node *parent,
  112. unsigned long size,
  113. unsigned alignment)
  114. {
  115. return drm_mm_get_block_generic(parent, size, alignment, 0, 0);
  116. }
  117. static inline struct drm_mm_node *drm_mm_get_block_atomic(struct drm_mm_node *parent,
  118. unsigned long size,
  119. unsigned alignment)
  120. {
  121. return drm_mm_get_block_generic(parent, size, alignment, 0, 1);
  122. }
  123. static inline struct drm_mm_node *drm_mm_get_block_range(
  124. struct drm_mm_node *parent,
  125. unsigned long size,
  126. unsigned alignment,
  127. unsigned long start,
  128. unsigned long end)
  129. {
  130. return drm_mm_get_block_range_generic(parent, size, alignment, 0,
  131. start, end, 0);
  132. }
  133. static inline struct drm_mm_node *drm_mm_get_color_block_range(
  134. struct drm_mm_node *parent,
  135. unsigned long size,
  136. unsigned alignment,
  137. unsigned long color,
  138. unsigned long start,
  139. unsigned long end)
  140. {
  141. return drm_mm_get_block_range_generic(parent, size, alignment, color,
  142. start, end, 0);
  143. }
  144. static inline struct drm_mm_node *drm_mm_get_block_atomic_range(
  145. struct drm_mm_node *parent,
  146. unsigned long size,
  147. unsigned alignment,
  148. unsigned long start,
  149. unsigned long end)
  150. {
  151. return drm_mm_get_block_range_generic(parent, size, alignment, 0,
  152. start, end, 1);
  153. }
  154. extern int drm_mm_insert_node(struct drm_mm *mm,
  155. struct drm_mm_node *node,
  156. unsigned long size,
  157. unsigned alignment);
  158. extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
  159. struct drm_mm_node *node,
  160. unsigned long size,
  161. unsigned alignment,
  162. unsigned long start,
  163. unsigned long end);
  164. extern int drm_mm_insert_node_generic(struct drm_mm *mm,
  165. struct drm_mm_node *node,
  166. unsigned long size,
  167. unsigned alignment,
  168. unsigned long color);
  169. extern int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
  170. struct drm_mm_node *node,
  171. unsigned long size,
  172. unsigned alignment,
  173. unsigned long color,
  174. unsigned long start,
  175. unsigned long end);
  176. extern void drm_mm_put_block(struct drm_mm_node *cur);
  177. extern void drm_mm_remove_node(struct drm_mm_node *node);
  178. extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  179. extern struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
  180. unsigned long size,
  181. unsigned alignment,
  182. unsigned long color,
  183. bool best_match);
  184. extern struct drm_mm_node *drm_mm_search_free_in_range_generic(
  185. const struct drm_mm *mm,
  186. unsigned long size,
  187. unsigned alignment,
  188. unsigned long color,
  189. unsigned long start,
  190. unsigned long end,
  191. bool best_match);
  192. static inline struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  193. unsigned long size,
  194. unsigned alignment,
  195. bool best_match)
  196. {
  197. return drm_mm_search_free_generic(mm,size, alignment, 0, best_match);
  198. }
  199. static inline struct drm_mm_node *drm_mm_search_free_in_range(
  200. const struct drm_mm *mm,
  201. unsigned long size,
  202. unsigned alignment,
  203. unsigned long start,
  204. unsigned long end,
  205. bool best_match)
  206. {
  207. return drm_mm_search_free_in_range_generic(mm, size, alignment, 0,
  208. start, end, best_match);
  209. }
  210. static inline struct drm_mm_node *drm_mm_search_free_color(const struct drm_mm *mm,
  211. unsigned long size,
  212. unsigned alignment,
  213. unsigned long color,
  214. bool best_match)
  215. {
  216. return drm_mm_search_free_generic(mm,size, alignment, color, best_match);
  217. }
  218. static inline struct drm_mm_node *drm_mm_search_free_in_range_color(
  219. const struct drm_mm *mm,
  220. unsigned long size,
  221. unsigned alignment,
  222. unsigned long color,
  223. unsigned long start,
  224. unsigned long end,
  225. bool best_match)
  226. {
  227. return drm_mm_search_free_in_range_generic(mm, size, alignment, color,
  228. start, end, best_match);
  229. }
  230. extern int drm_mm_init(struct drm_mm *mm,
  231. unsigned long start,
  232. unsigned long size);
  233. extern void drm_mm_takedown(struct drm_mm *mm);
  234. extern int drm_mm_clean(struct drm_mm *mm);
  235. extern int drm_mm_pre_get(struct drm_mm *mm);
  236. static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
  237. {
  238. return block->mm;
  239. }
  240. void drm_mm_init_scan(struct drm_mm *mm,
  241. unsigned long size,
  242. unsigned alignment,
  243. unsigned long color);
  244. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  245. unsigned long size,
  246. unsigned alignment,
  247. unsigned long color,
  248. unsigned long start,
  249. unsigned long end);
  250. int drm_mm_scan_add_block(struct drm_mm_node *node);
  251. int drm_mm_scan_remove_block(struct drm_mm_node *node);
  252. extern void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
  253. #ifdef CONFIG_DEBUG_FS
  254. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
  255. #endif
  256. #endif