drm_mm.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_block_atomic_range(
  167. struct drm_mm_node *parent,
  168. unsigned long size,
  169. unsigned alignment,
  170. unsigned long start,
  171. unsigned long end)
  172. {
  173. return drm_mm_get_block_range_generic(parent, size, alignment, 0,
  174. start, end, 1);
  175. }
  176. extern int drm_mm_insert_node(struct drm_mm *mm,
  177. struct drm_mm_node *node,
  178. unsigned long size,
  179. unsigned alignment);
  180. extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
  181. struct drm_mm_node *node,
  182. unsigned long size,
  183. unsigned alignment,
  184. unsigned long start,
  185. unsigned long end);
  186. extern int drm_mm_insert_node_generic(struct drm_mm *mm,
  187. struct drm_mm_node *node,
  188. unsigned long size,
  189. unsigned alignment,
  190. unsigned long color);
  191. extern int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
  192. struct drm_mm_node *node,
  193. unsigned long size,
  194. unsigned alignment,
  195. unsigned long color,
  196. unsigned long start,
  197. unsigned long end);
  198. extern void drm_mm_put_block(struct drm_mm_node *cur);
  199. extern void drm_mm_remove_node(struct drm_mm_node *node);
  200. extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
  201. extern struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
  202. unsigned long size,
  203. unsigned alignment,
  204. unsigned long color,
  205. bool best_match);
  206. extern struct drm_mm_node *drm_mm_search_free_in_range_generic(
  207. const struct drm_mm *mm,
  208. unsigned long size,
  209. unsigned alignment,
  210. unsigned long color,
  211. unsigned long start,
  212. unsigned long end,
  213. bool best_match);
  214. static inline struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
  215. unsigned long size,
  216. unsigned alignment,
  217. bool best_match)
  218. {
  219. return drm_mm_search_free_generic(mm,size, alignment, 0, best_match);
  220. }
  221. static inline struct drm_mm_node *drm_mm_search_free_in_range(
  222. const struct drm_mm *mm,
  223. unsigned long size,
  224. unsigned alignment,
  225. unsigned long start,
  226. unsigned long end,
  227. bool best_match)
  228. {
  229. return drm_mm_search_free_in_range_generic(mm, size, alignment, 0,
  230. start, end, best_match);
  231. }
  232. extern void drm_mm_init(struct drm_mm *mm,
  233. unsigned long start,
  234. unsigned long size);
  235. extern void drm_mm_takedown(struct drm_mm *mm);
  236. extern int drm_mm_clean(struct drm_mm *mm);
  237. extern int drm_mm_pre_get(struct drm_mm *mm);
  238. static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
  239. {
  240. return block->mm;
  241. }
  242. void drm_mm_init_scan(struct drm_mm *mm,
  243. unsigned long size,
  244. unsigned alignment,
  245. unsigned long color);
  246. void drm_mm_init_scan_with_range(struct drm_mm *mm,
  247. unsigned long size,
  248. unsigned alignment,
  249. unsigned long color,
  250. unsigned long start,
  251. unsigned long end);
  252. int drm_mm_scan_add_block(struct drm_mm_node *node);
  253. int drm_mm_scan_remove_block(struct drm_mm_node *node);
  254. extern void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
  255. #ifdef CONFIG_DEBUG_FS
  256. int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
  257. #endif
  258. #endif