drm_mm.h 8.1 KB

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