drm_mm.h 8.9 KB

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