1 /************************************************************************** 2 * 3 * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA. 4 * Copyright 2016 Intel Corporation 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 * 28 **************************************************************************/ 29 /* 30 * Authors: 31 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com> 32 */ 33 34 #ifndef _DRM_MM_H_ 35 #define _DRM_MM_H_ 36 37 /* 38 * Generic range manager structs 39 */ 40 #include <linux/bug.h> 41 #include <linux/rbtree.h> 42 #include <linux/kernel.h> 43 #include <linux/list.h> 44 #include <linux/spinlock.h> 45 #ifdef CONFIG_DEBUG_FS 46 #include <linux/seq_file.h> 47 #endif 48 #ifdef CONFIG_DRM_DEBUG_MM 49 #include <linux/stackdepot.h> 50 #endif 51 52 #ifdef CONFIG_DRM_DEBUG_MM 53 #define DRM_MM_BUG_ON(expr) BUG_ON(expr) 54 #else 55 #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr) 56 #endif 57 58 enum drm_mm_search_flags { 59 DRM_MM_SEARCH_DEFAULT = 0, 60 DRM_MM_SEARCH_BEST = 1 << 0, 61 DRM_MM_SEARCH_BELOW = 1 << 1, 62 }; 63 64 enum drm_mm_allocator_flags { 65 DRM_MM_CREATE_DEFAULT = 0, 66 DRM_MM_CREATE_TOP = 1 << 0, 67 }; 68 69 #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT 70 #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP 71 72 struct drm_mm_node { 73 struct list_head node_list; 74 struct list_head hole_stack; 75 struct rb_node rb; 76 unsigned hole_follows : 1; 77 unsigned scanned_block : 1; 78 unsigned scanned_prev_free : 1; 79 unsigned scanned_next_free : 1; 80 unsigned scanned_preceeds_hole : 1; 81 unsigned allocated : 1; 82 unsigned long color; 83 u64 start; 84 u64 size; 85 u64 __subtree_last; 86 struct drm_mm *mm; 87 #ifdef CONFIG_DRM_DEBUG_MM 88 depot_stack_handle_t stack; 89 #endif 90 }; 91 92 struct drm_mm { 93 /* List of all memory nodes that immediately precede a free hole. */ 94 struct list_head hole_stack; 95 /* head_node.node_list is the list of all memory nodes, ordered 96 * according to the (increasing) start address of the memory node. */ 97 struct drm_mm_node head_node; 98 /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */ 99 struct rb_root interval_tree; 100 101 void (*color_adjust)(const struct drm_mm_node *node, 102 unsigned long color, 103 u64 *start, u64 *end); 104 105 unsigned long scan_active; 106 }; 107 108 struct drm_mm_scan { 109 struct drm_mm *mm; 110 111 u64 size; 112 u64 alignment; 113 u64 remainder_mask; 114 115 u64 range_start; 116 u64 range_end; 117 118 u64 hit_start; 119 u64 hit_end; 120 121 struct drm_mm_node *prev_scanned_node; 122 123 unsigned long color; 124 unsigned int flags; 125 }; 126 127 /** 128 * drm_mm_node_allocated - checks whether a node is allocated 129 * @node: drm_mm_node to check 130 * 131 * Drivers are required to clear a node prior to using it with the 132 * drm_mm range manager. 133 * 134 * Drivers should use this helper for proper encapsulation of drm_mm 135 * internals. 136 * 137 * Returns: 138 * True if the @node is allocated. 139 */ 140 static inline bool drm_mm_node_allocated(const struct drm_mm_node *node) 141 { 142 return node->allocated; 143 } 144 145 /** 146 * drm_mm_initialized - checks whether an allocator is initialized 147 * @mm: drm_mm to check 148 * 149 * Drivers should clear the struct drm_mm prior to initialisation if they 150 * want to use this function. 151 * 152 * Drivers should use this helper for proper encapsulation of drm_mm 153 * internals. 154 * 155 * Returns: 156 * True if the @mm is initialized. 157 */ 158 static inline bool drm_mm_initialized(const struct drm_mm *mm) 159 { 160 return mm->hole_stack.next; 161 } 162 163 static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node) 164 { 165 return hole_node->start + hole_node->size; 166 } 167 168 /** 169 * drm_mm_hole_node_start - computes the start of the hole following @node 170 * @hole_node: drm_mm_node which implicitly tracks the following hole 171 * 172 * This is useful for driver-specific debug dumpers. Otherwise drivers should 173 * not inspect holes themselves. Drivers must check first whether a hole indeed 174 * follows by looking at node->hole_follows. 175 * 176 * Returns: 177 * Start of the subsequent hole. 178 */ 179 static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node) 180 { 181 DRM_MM_BUG_ON(!hole_node->hole_follows); 182 return __drm_mm_hole_node_start(hole_node); 183 } 184 185 static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node) 186 { 187 return list_next_entry(hole_node, node_list)->start; 188 } 189 190 /** 191 * drm_mm_hole_node_end - computes the end of the hole following @node 192 * @hole_node: drm_mm_node which implicitly tracks the following hole 193 * 194 * This is useful for driver-specific debug dumpers. Otherwise drivers should 195 * not inspect holes themselves. Drivers must check first whether a hole indeed 196 * follows by looking at node->hole_follows. 197 * 198 * Returns: 199 * End of the subsequent hole. 200 */ 201 static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node) 202 { 203 return __drm_mm_hole_node_end(hole_node); 204 } 205 206 /** 207 * drm_mm_nodes - list of nodes under the drm_mm range manager 208 * @mm: the struct drm_mm range manger 209 * 210 * As the drm_mm range manager hides its node_list deep with its 211 * structure, extracting it looks painful and repetitive. This is 212 * not expected to be used outside of the drm_mm_for_each_node() 213 * macros and similar internal functions. 214 * 215 * Returns: 216 * The node list, may be empty. 217 */ 218 #define drm_mm_nodes(mm) (&(mm)->head_node.node_list) 219 220 /** 221 * drm_mm_for_each_node - iterator to walk over all allocated nodes 222 * @entry: drm_mm_node structure to assign to in each iteration step 223 * @mm: drm_mm allocator to walk 224 * 225 * This iterator walks over all nodes in the range allocator. It is implemented 226 * with list_for_each, so not save against removal of elements. 227 */ 228 #define drm_mm_for_each_node(entry, mm) \ 229 list_for_each_entry(entry, drm_mm_nodes(mm), node_list) 230 231 /** 232 * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes 233 * @entry: drm_mm_node structure to assign to in each iteration step 234 * @next: drm_mm_node structure to store the next step 235 * @mm: drm_mm allocator to walk 236 * 237 * This iterator walks over all nodes in the range allocator. It is implemented 238 * with list_for_each_safe, so save against removal of elements. 239 */ 240 #define drm_mm_for_each_node_safe(entry, next, mm) \ 241 list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list) 242 243 #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \ 244 for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \ 245 &entry->hole_stack != &(mm)->hole_stack ? \ 246 hole_start = drm_mm_hole_node_start(entry), \ 247 hole_end = drm_mm_hole_node_end(entry), \ 248 1 : 0; \ 249 entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack)) 250 251 /** 252 * drm_mm_for_each_hole - iterator to walk over all holes 253 * @entry: drm_mm_node used internally to track progress 254 * @mm: drm_mm allocator to walk 255 * @hole_start: ulong variable to assign the hole start to on each iteration 256 * @hole_end: ulong variable to assign the hole end to on each iteration 257 * 258 * This iterator walks over all holes in the range allocator. It is implemented 259 * with list_for_each, so not save against removal of elements. @entry is used 260 * internally and will not reflect a real drm_mm_node for the very first hole. 261 * Hence users of this iterator may not access it. 262 * 263 * Implementation Note: 264 * We need to inline list_for_each_entry in order to be able to set hole_start 265 * and hole_end on each iteration while keeping the macro sane. 266 * 267 * The __drm_mm_for_each_hole version is similar, but with added support for 268 * going backwards. 269 */ 270 #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \ 271 __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0) 272 273 /* 274 * Basic range manager support (drm_mm.c) 275 */ 276 int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node); 277 278 int drm_mm_insert_node_generic(struct drm_mm *mm, 279 struct drm_mm_node *node, 280 u64 size, 281 u64 alignment, 282 unsigned long color, 283 enum drm_mm_search_flags sflags, 284 enum drm_mm_allocator_flags aflags); 285 /** 286 * drm_mm_insert_node - search for space and insert @node 287 * @mm: drm_mm to allocate from 288 * @node: preallocate node to insert 289 * @size: size of the allocation 290 * @alignment: alignment of the allocation 291 * @flags: flags to fine-tune the allocation 292 * 293 * This is a simplified version of drm_mm_insert_node_generic() with @color set 294 * to 0. 295 * 296 * The preallocated node must be cleared to 0. 297 * 298 * Returns: 299 * 0 on success, -ENOSPC if there's no suitable hole. 300 */ 301 static inline int drm_mm_insert_node(struct drm_mm *mm, 302 struct drm_mm_node *node, 303 u64 size, 304 u64 alignment, 305 enum drm_mm_search_flags flags) 306 { 307 return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags, 308 DRM_MM_CREATE_DEFAULT); 309 } 310 311 int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, 312 struct drm_mm_node *node, 313 u64 size, 314 u64 alignment, 315 unsigned long color, 316 u64 start, 317 u64 end, 318 enum drm_mm_search_flags sflags, 319 enum drm_mm_allocator_flags aflags); 320 /** 321 * drm_mm_insert_node_in_range - ranged search for space and insert @node 322 * @mm: drm_mm to allocate from 323 * @node: preallocate node to insert 324 * @size: size of the allocation 325 * @alignment: alignment of the allocation 326 * @start: start of the allowed range for this node 327 * @end: end of the allowed range for this node 328 * @flags: flags to fine-tune the allocation 329 * 330 * This is a simplified version of drm_mm_insert_node_in_range_generic() with 331 * @color set to 0. 332 * 333 * The preallocated node must be cleared to 0. 334 * 335 * Returns: 336 * 0 on success, -ENOSPC if there's no suitable hole. 337 */ 338 static inline int drm_mm_insert_node_in_range(struct drm_mm *mm, 339 struct drm_mm_node *node, 340 u64 size, 341 u64 alignment, 342 u64 start, 343 u64 end, 344 enum drm_mm_search_flags flags) 345 { 346 return drm_mm_insert_node_in_range_generic(mm, node, size, alignment, 347 0, start, end, flags, 348 DRM_MM_CREATE_DEFAULT); 349 } 350 351 void drm_mm_remove_node(struct drm_mm_node *node); 352 void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new); 353 void drm_mm_init(struct drm_mm *mm, u64 start, u64 size); 354 void drm_mm_takedown(struct drm_mm *mm); 355 356 /** 357 * drm_mm_clean - checks whether an allocator is clean 358 * @mm: drm_mm allocator to check 359 * 360 * Returns: 361 * True if the allocator is completely free, false if there's still a node 362 * allocated in it. 363 */ 364 static inline bool drm_mm_clean(const struct drm_mm *mm) 365 { 366 return list_empty(drm_mm_nodes(mm)); 367 } 368 369 struct drm_mm_node * 370 __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last); 371 372 /** 373 * drm_mm_for_each_node_in_range - iterator to walk over a range of 374 * allocated nodes 375 * @node__: drm_mm_node structure to assign to in each iteration step 376 * @mm__: drm_mm allocator to walk 377 * @start__: starting offset, the first node will overlap this 378 * @end__: ending offset, the last node will start before this (but may overlap) 379 * 380 * This iterator walks over all nodes in the range allocator that lie 381 * between @start and @end. It is implemented similarly to list_for_each(), 382 * but using the internal interval tree to accelerate the search for the 383 * starting node, and so not safe against removal of elements. It assumes 384 * that @end is within (or is the upper limit of) the drm_mm allocator. 385 */ 386 #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__) \ 387 for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \ 388 node__ && node__->start < (end__); \ 389 node__ = list_next_entry(node__, node_list)) 390 391 void drm_mm_scan_init_with_range(struct drm_mm_scan *scan, 392 struct drm_mm *mm, 393 u64 size, u64 alignment, unsigned long color, 394 u64 start, u64 end, 395 unsigned int flags); 396 397 /** 398 * drm_mm_scan_init - initialize lru scanning 399 * @scan: scan state 400 * @mm: drm_mm to scan 401 * @size: size of the allocation 402 * @alignment: alignment of the allocation 403 * @color: opaque tag value to use for the allocation 404 * @flags: flags to specify how the allocation will be performed afterwards 405 * 406 * This simply sets up the scanning routines with the parameters for the desired 407 * hole. 408 * 409 * Warning: 410 * As long as the scan list is non-empty, no other operations than 411 * adding/removing nodes to/from the scan list are allowed. 412 */ 413 static inline void drm_mm_scan_init(struct drm_mm_scan *scan, 414 struct drm_mm *mm, 415 u64 size, 416 u64 alignment, 417 unsigned long color, 418 unsigned int flags) 419 { 420 drm_mm_scan_init_with_range(scan, mm, 421 size, alignment, color, 422 0, U64_MAX, 423 flags); 424 } 425 426 bool drm_mm_scan_add_block(struct drm_mm_scan *scan, 427 struct drm_mm_node *node); 428 bool drm_mm_scan_remove_block(struct drm_mm_scan *scan, 429 struct drm_mm_node *node); 430 431 void drm_mm_debug_table(const struct drm_mm *mm, const char *prefix); 432 #ifdef CONFIG_DEBUG_FS 433 int drm_mm_dump_table(struct seq_file *m, const struct drm_mm *mm); 434 #endif 435 436 #endif 437