Home | History | Annotate | Line # | Download | only in drm
drm_mm.c revision 1.4
      1 /*	$NetBSD: drm_mm.c,v 1.4 2018/08/27 04:58:19 riastradh Exp $	*/
      2 
      3 /**************************************************************************
      4  *
      5  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
      6  * All Rights Reserved.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sub license, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice (including the
     17  * next paragraph) shall be included in all copies or substantial portions
     18  * of the Software.
     19  *
     20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     27  *
     28  *
     29  **************************************************************************/
     30 
     31 /*
     32  * Generic simple memory manager implementation. Intended to be used as a base
     33  * class implementation for more advanced memory managers.
     34  *
     35  * Note that the algorithm used is quite simple and there might be substantial
     36  * performance gains if a smarter free list is implemented. Currently it is just an
     37  * unordered stack of free regions. This could easily be improved if an RB-tree
     38  * is used instead. At least if we expect heavy fragmentation.
     39  *
     40  * Aligned allocations can also see improvement.
     41  *
     42  * Authors:
     43  * Thomas Hellstrm <thomas-at-tungstengraphics-dot-com>
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: drm_mm.c,v 1.4 2018/08/27 04:58:19 riastradh Exp $");
     48 
     49 #include <drm/drmP.h>
     50 #include <drm/drm_mm.h>
     51 #include <linux/slab.h>
     52 #include <linux/seq_file.h>
     53 #include <linux/export.h>
     54 #include <linux/printk.h>
     55 #include <asm/bug.h>
     56 
     57 /**
     58  * DOC: Overview
     59  *
     60  * drm_mm provides a simple range allocator. The drivers are free to use the
     61  * resource allocator from the linux core if it suits them, the upside of drm_mm
     62  * is that it's in the DRM core. Which means that it's easier to extend for
     63  * some of the crazier special purpose needs of gpus.
     64  *
     65  * The main data struct is &drm_mm, allocations are tracked in &drm_mm_node.
     66  * Drivers are free to embed either of them into their own suitable
     67  * datastructures. drm_mm itself will not do any allocations of its own, so if
     68  * drivers choose not to embed nodes they need to still allocate them
     69  * themselves.
     70  *
     71  * The range allocator also supports reservation of preallocated blocks. This is
     72  * useful for taking over initial mode setting configurations from the firmware,
     73  * where an object needs to be created which exactly matches the firmware's
     74  * scanout target. As long as the range is still free it can be inserted anytime
     75  * after the allocator is initialized, which helps with avoiding looped
     76  * depencies in the driver load sequence.
     77  *
     78  * drm_mm maintains a stack of most recently freed holes, which of all
     79  * simplistic datastructures seems to be a fairly decent approach to clustering
     80  * allocations and avoiding too much fragmentation. This means free space
     81  * searches are O(num_holes). Given that all the fancy features drm_mm supports
     82  * something better would be fairly complex and since gfx thrashing is a fairly
     83  * steep cliff not a real concern. Removing a node again is O(1).
     84  *
     85  * drm_mm supports a few features: Alignment and range restrictions can be
     86  * supplied. Further more every &drm_mm_node has a color value (which is just an
     87  * opaqua unsigned long) which in conjunction with a driver callback can be used
     88  * to implement sophisticated placement restrictions. The i915 DRM driver uses
     89  * this to implement guard pages between incompatible caching domains in the
     90  * graphics TT.
     91  *
     92  * Two behaviors are supported for searching and allocating: bottom-up and top-down.
     93  * The default is bottom-up. Top-down allocation can be used if the memory area
     94  * has different restrictions, or just to reduce fragmentation.
     95  *
     96  * Finally iteration helpers to walk all nodes and all holes are provided as are
     97  * some basic allocator dumpers for debugging.
     98  */
     99 
    100 static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
    101 						u64 size,
    102 						unsigned alignment,
    103 						unsigned long color,
    104 						enum drm_mm_search_flags flags);
    105 static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
    106 						u64 size,
    107 						unsigned alignment,
    108 						unsigned long color,
    109 						u64 start,
    110 						u64 end,
    111 						enum drm_mm_search_flags flags);
    112 
    113 static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
    114 				 struct drm_mm_node *node,
    115 				 u64 size, unsigned alignment,
    116 				 unsigned long color,
    117 				 enum drm_mm_allocator_flags flags)
    118 {
    119 	struct drm_mm *mm = hole_node->mm;
    120 	u64 hole_start = drm_mm_hole_node_start(hole_node);
    121 	u64 hole_end = drm_mm_hole_node_end(hole_node);
    122 	u64 adj_start = hole_start;
    123 	u64 adj_end = hole_end;
    124 
    125 	BUG_ON(node->allocated);
    126 
    127 	if (mm->color_adjust)
    128 		mm->color_adjust(hole_node, color, &adj_start, &adj_end);
    129 
    130 	if (flags & DRM_MM_CREATE_TOP)
    131 		adj_start = adj_end - size;
    132 
    133 	if (alignment) {
    134 		u64 tmp = adj_start;
    135 		unsigned rem;
    136 
    137 		rem = do_div(tmp, alignment);
    138 		if (rem) {
    139 			if (flags & DRM_MM_CREATE_TOP)
    140 				adj_start -= rem;
    141 			else
    142 				adj_start += alignment - rem;
    143 		}
    144 	}
    145 
    146 	BUG_ON(adj_start < hole_start);
    147 	BUG_ON(adj_end > hole_end);
    148 
    149 	if (adj_start == hole_start) {
    150 		hole_node->hole_follows = 0;
    151 		list_del(&hole_node->hole_stack);
    152 	}
    153 
    154 	node->start = adj_start;
    155 	node->size = size;
    156 	node->mm = mm;
    157 	node->color = color;
    158 	node->allocated = 1;
    159 
    160 	INIT_LIST_HEAD(&node->hole_stack);
    161 	list_add(&node->node_list, &hole_node->node_list);
    162 
    163 	BUG_ON(node->start + node->size > adj_end);
    164 
    165 	node->hole_follows = 0;
    166 	if (__drm_mm_hole_node_start(node) < hole_end) {
    167 		list_add(&node->hole_stack, &mm->hole_stack);
    168 		node->hole_follows = 1;
    169 	}
    170 }
    171 
    172 /**
    173  * drm_mm_reserve_node - insert an pre-initialized node
    174  * @mm: drm_mm allocator to insert @node into
    175  * @node: drm_mm_node to insert
    176  *
    177  * This functions inserts an already set-up drm_mm_node into the allocator,
    178  * meaning that start, size and color must be set by the caller. This is useful
    179  * to initialize the allocator with preallocated objects which must be set-up
    180  * before the range allocator can be set-up, e.g. when taking over a firmware
    181  * framebuffer.
    182  *
    183  * Returns:
    184  * 0 on success, -ENOSPC if there's no hole where @node is.
    185  */
    186 int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
    187 {
    188 	struct drm_mm_node *hole;
    189 	u64 end = node->start + node->size;
    190 	u64 hole_start;
    191 	u64 hole_end;
    192 
    193 	BUG_ON(node == NULL);
    194 
    195 	/* Find the relevant hole to add our node to */
    196 	drm_mm_for_each_hole(hole, mm, hole_start, hole_end) {
    197 		if (hole_start > node->start || hole_end < end)
    198 			continue;
    199 
    200 		node->mm = mm;
    201 		node->allocated = 1;
    202 
    203 		INIT_LIST_HEAD(&node->hole_stack);
    204 		list_add(&node->node_list, &hole->node_list);
    205 
    206 		if (node->start == hole_start) {
    207 			hole->hole_follows = 0;
    208 			list_del_init(&hole->hole_stack);
    209 		}
    210 
    211 		node->hole_follows = 0;
    212 		if (end != hole_end) {
    213 			list_add(&node->hole_stack, &mm->hole_stack);
    214 			node->hole_follows = 1;
    215 		}
    216 
    217 		return 0;
    218 	}
    219 
    220 	return -ENOSPC;
    221 }
    222 EXPORT_SYMBOL(drm_mm_reserve_node);
    223 
    224 /**
    225  * drm_mm_insert_node_generic - search for space and insert @node
    226  * @mm: drm_mm to allocate from
    227  * @node: preallocate node to insert
    228  * @size: size of the allocation
    229  * @alignment: alignment of the allocation
    230  * @color: opaque tag value to use for this node
    231  * @sflags: flags to fine-tune the allocation search
    232  * @aflags: flags to fine-tune the allocation behavior
    233  *
    234  * The preallocated node must be cleared to 0.
    235  *
    236  * Returns:
    237  * 0 on success, -ENOSPC if there's no suitable hole.
    238  */
    239 int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
    240 			       u64 size, unsigned alignment,
    241 			       unsigned long color,
    242 			       enum drm_mm_search_flags sflags,
    243 			       enum drm_mm_allocator_flags aflags)
    244 {
    245 	struct drm_mm_node *hole_node;
    246 
    247 	hole_node = drm_mm_search_free_generic(mm, size, alignment,
    248 					       color, sflags);
    249 	if (!hole_node)
    250 		return -ENOSPC;
    251 
    252 	drm_mm_insert_helper(hole_node, node, size, alignment, color, aflags);
    253 	return 0;
    254 }
    255 EXPORT_SYMBOL(drm_mm_insert_node_generic);
    256 
    257 static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
    258 				       struct drm_mm_node *node,
    259 				       u64 size, unsigned alignment,
    260 				       unsigned long color,
    261 				       u64 start, u64 end,
    262 				       enum drm_mm_allocator_flags flags)
    263 {
    264 	struct drm_mm *mm = hole_node->mm;
    265 	u64 hole_start = drm_mm_hole_node_start(hole_node);
    266 	u64 hole_end = drm_mm_hole_node_end(hole_node);
    267 	u64 adj_start = hole_start;
    268 	u64 adj_end = hole_end;
    269 
    270 	BUG_ON(!hole_node->hole_follows || node->allocated);
    271 
    272 	if (mm->color_adjust)
    273 		mm->color_adjust(hole_node, color, &adj_start, &adj_end);
    274 
    275 	adj_start = max(adj_start, start);
    276 	adj_end = min(adj_end, end);
    277 
    278 	if (flags & DRM_MM_CREATE_TOP)
    279 		adj_start = adj_end - size;
    280 
    281 	if (alignment) {
    282 		u64 tmp = adj_start;
    283 		unsigned rem;
    284 
    285 		rem = do_div(tmp, alignment);
    286 		if (rem) {
    287 			if (flags & DRM_MM_CREATE_TOP)
    288 				adj_start -= rem;
    289 			else
    290 				adj_start += alignment - rem;
    291 		}
    292 	}
    293 
    294 	if (adj_start == hole_start) {
    295 		hole_node->hole_follows = 0;
    296 		list_del(&hole_node->hole_stack);
    297 	}
    298 
    299 	node->start = adj_start;
    300 	node->size = size;
    301 	node->mm = mm;
    302 	node->color = color;
    303 	node->allocated = 1;
    304 
    305 	INIT_LIST_HEAD(&node->hole_stack);
    306 	list_add(&node->node_list, &hole_node->node_list);
    307 
    308 	BUG_ON(node->start < start);
    309 	BUG_ON(node->start < adj_start);
    310 	BUG_ON(node->start + node->size > adj_end);
    311 	BUG_ON(node->start + node->size > end);
    312 
    313 	node->hole_follows = 0;
    314 	if (__drm_mm_hole_node_start(node) < hole_end) {
    315 		list_add(&node->hole_stack, &mm->hole_stack);
    316 		node->hole_follows = 1;
    317 	}
    318 }
    319 
    320 /**
    321  * drm_mm_insert_node_in_range_generic - 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  * @color: opaque tag value to use for this node
    327  * @start: start of the allowed range for this node
    328  * @end: end of the allowed range for this node
    329  * @sflags: flags to fine-tune the allocation search
    330  * @aflags: flags to fine-tune the allocation behavior
    331  *
    332  * The preallocated node must be cleared to 0.
    333  *
    334  * Returns:
    335  * 0 on success, -ENOSPC if there's no suitable hole.
    336  */
    337 int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
    338 					u64 size, unsigned alignment,
    339 					unsigned long color,
    340 					u64 start, u64 end,
    341 					enum drm_mm_search_flags sflags,
    342 					enum drm_mm_allocator_flags aflags)
    343 {
    344 	struct drm_mm_node *hole_node;
    345 
    346 	hole_node = drm_mm_search_free_in_range_generic(mm,
    347 							size, alignment, color,
    348 							start, end, sflags);
    349 	if (!hole_node)
    350 		return -ENOSPC;
    351 
    352 	drm_mm_insert_helper_range(hole_node, node,
    353 				   size, alignment, color,
    354 				   start, end, aflags);
    355 	return 0;
    356 }
    357 EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
    358 
    359 /**
    360  * drm_mm_remove_node - Remove a memory node from the allocator.
    361  * @node: drm_mm_node to remove
    362  *
    363  * This just removes a node from its drm_mm allocator. The node does not need to
    364  * be cleared again before it can be re-inserted into this or any other drm_mm
    365  * allocator. It is a bug to call this function on a un-allocated node.
    366  */
    367 void drm_mm_remove_node(struct drm_mm_node *node)
    368 {
    369 	struct drm_mm *mm = node->mm;
    370 	struct drm_mm_node *prev_node;
    371 
    372 	if (WARN_ON(!node->allocated))
    373 		return;
    374 
    375 	BUG_ON(node->scanned_block || node->scanned_prev_free
    376 				   || node->scanned_next_free);
    377 
    378 	prev_node =
    379 	    list_entry(node->node_list.prev, struct drm_mm_node, node_list);
    380 
    381 	if (node->hole_follows) {
    382 		BUG_ON(__drm_mm_hole_node_start(node) ==
    383 		       __drm_mm_hole_node_end(node));
    384 		list_del(&node->hole_stack);
    385 	} else
    386 		BUG_ON(__drm_mm_hole_node_start(node) !=
    387 		       __drm_mm_hole_node_end(node));
    388 
    389 
    390 	if (!prev_node->hole_follows) {
    391 		prev_node->hole_follows = 1;
    392 		list_add(&prev_node->hole_stack, &mm->hole_stack);
    393 	} else
    394 		list_move(&prev_node->hole_stack, &mm->hole_stack);
    395 
    396 	list_del(&node->node_list);
    397 	node->allocated = 0;
    398 }
    399 EXPORT_SYMBOL(drm_mm_remove_node);
    400 
    401 static int check_free_hole(u64 start, u64 end, u64 size, unsigned alignment)
    402 {
    403 	if (end - start < size)
    404 		return 0;
    405 
    406 	if (alignment) {
    407 		u64 tmp = start;
    408 		unsigned rem;
    409 
    410 		rem = do_div(tmp, alignment);
    411 		if (rem)
    412 			start += alignment - rem;
    413 	}
    414 
    415 	return end >= start + size;
    416 }
    417 
    418 static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
    419 						      u64 size,
    420 						      unsigned alignment,
    421 						      unsigned long color,
    422 						      enum drm_mm_search_flags flags)
    423 {
    424 	struct drm_mm_node *entry;
    425 	struct drm_mm_node *best;
    426 	u64 adj_start;
    427 	u64 adj_end;
    428 	u64 best_size;
    429 
    430 	BUG_ON(mm->scanned_blocks);
    431 
    432 	best = NULL;
    433 	best_size = ~0UL;
    434 
    435 	__drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
    436 			       flags & DRM_MM_SEARCH_BELOW) {
    437 		u64 hole_size = adj_end - adj_start;
    438 
    439 		if (mm->color_adjust) {
    440 			mm->color_adjust(entry, color, &adj_start, &adj_end);
    441 			if (adj_end <= adj_start)
    442 				continue;
    443 		}
    444 
    445 		if (!check_free_hole(adj_start, adj_end, size, alignment))
    446 			continue;
    447 
    448 		if (!(flags & DRM_MM_SEARCH_BEST))
    449 			return entry;
    450 
    451 		if (hole_size < best_size) {
    452 			best = entry;
    453 			best_size = hole_size;
    454 		}
    455 	}
    456 
    457 	return best;
    458 }
    459 
    460 static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
    461 							u64 size,
    462 							unsigned alignment,
    463 							unsigned long color,
    464 							u64 start,
    465 							u64 end,
    466 							enum drm_mm_search_flags flags)
    467 {
    468 	struct drm_mm_node *entry;
    469 	struct drm_mm_node *best;
    470 	u64 adj_start;
    471 	u64 adj_end;
    472 	u64 best_size;
    473 
    474 	BUG_ON(mm->scanned_blocks);
    475 
    476 	best = NULL;
    477 	best_size = ~0UL;
    478 
    479 	__drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
    480 			       flags & DRM_MM_SEARCH_BELOW) {
    481 		u64 hole_size = adj_end - adj_start;
    482 
    483 		if (mm->color_adjust) {
    484 			mm->color_adjust(entry, color, &adj_start, &adj_end);
    485 			if (adj_end <= adj_start)
    486 				continue;
    487 		}
    488 
    489 		adj_start = max(adj_start, start);
    490 		adj_end = min(adj_end, end);
    491 
    492 		if (!check_free_hole(adj_start, adj_end, size, alignment))
    493 			continue;
    494 
    495 		if (!(flags & DRM_MM_SEARCH_BEST))
    496 			return entry;
    497 
    498 		if (hole_size < best_size) {
    499 			best = entry;
    500 			best_size = hole_size;
    501 		}
    502 	}
    503 
    504 	return best;
    505 }
    506 
    507 /**
    508  * drm_mm_replace_node - move an allocation from @old to @new
    509  * @old: drm_mm_node to remove from the allocator
    510  * @new: drm_mm_node which should inherit @old's allocation
    511  *
    512  * This is useful for when drivers embed the drm_mm_node structure and hence
    513  * can't move allocations by reassigning pointers. It's a combination of remove
    514  * and insert with the guarantee that the allocation start will match.
    515  */
    516 void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
    517 {
    518 	list_replace(&old->node_list, &new->node_list);
    519 	list_replace(&old->hole_stack, &new->hole_stack);
    520 	new->hole_follows = old->hole_follows;
    521 	new->mm = old->mm;
    522 	new->start = old->start;
    523 	new->size = old->size;
    524 	new->color = old->color;
    525 
    526 	old->allocated = 0;
    527 	new->allocated = 1;
    528 }
    529 EXPORT_SYMBOL(drm_mm_replace_node);
    530 
    531 /**
    532  * DOC: lru scan roaster
    533  *
    534  * Very often GPUs need to have continuous allocations for a given object. When
    535  * evicting objects to make space for a new one it is therefore not most
    536  * efficient when we simply start to select all objects from the tail of an LRU
    537  * until there's a suitable hole: Especially for big objects or nodes that
    538  * otherwise have special allocation constraints there's a good chance we evict
    539  * lots of (smaller) objects unecessarily.
    540  *
    541  * The DRM range allocator supports this use-case through the scanning
    542  * interfaces. First a scan operation needs to be initialized with
    543  * drm_mm_init_scan() or drm_mm_init_scan_with_range(). The the driver adds
    544  * objects to the roaster (probably by walking an LRU list, but this can be
    545  * freely implemented) until a suitable hole is found or there's no further
    546  * evitable object.
    547  *
    548  * The the driver must walk through all objects again in exactly the reverse
    549  * order to restore the allocator state. Note that while the allocator is used
    550  * in the scan mode no other operation is allowed.
    551  *
    552  * Finally the driver evicts all objects selected in the scan. Adding and
    553  * removing an object is O(1), and since freeing a node is also O(1) the overall
    554  * complexity is O(scanned_objects). So like the free stack which needs to be
    555  * walked before a scan operation even begins this is linear in the number of
    556  * objects. It doesn't seem to hurt badly.
    557  */
    558 
    559 /**
    560  * drm_mm_init_scan - initialize lru scanning
    561  * @mm: drm_mm to scan
    562  * @size: size of the allocation
    563  * @alignment: alignment of the allocation
    564  * @color: opaque tag value to use for the allocation
    565  *
    566  * This simply sets up the scanning routines with the parameters for the desired
    567  * hole. Note that there's no need to specify allocation flags, since they only
    568  * change the place a node is allocated from within a suitable hole.
    569  *
    570  * Warning:
    571  * As long as the scan list is non-empty, no other operations than
    572  * adding/removing nodes to/from the scan list are allowed.
    573  */
    574 void drm_mm_init_scan(struct drm_mm *mm,
    575 		      u64 size,
    576 		      unsigned alignment,
    577 		      unsigned long color)
    578 {
    579 	mm->scan_color = color;
    580 	mm->scan_alignment = alignment;
    581 	mm->scan_size = size;
    582 	mm->scanned_blocks = 0;
    583 	mm->scan_hit_start = 0;
    584 	mm->scan_hit_end = 0;
    585 	mm->scan_check_range = 0;
    586 	mm->prev_scanned_node = NULL;
    587 }
    588 EXPORT_SYMBOL(drm_mm_init_scan);
    589 
    590 /**
    591  * drm_mm_init_scan - initialize range-restricted lru scanning
    592  * @mm: drm_mm to scan
    593  * @size: size of the allocation
    594  * @alignment: alignment of the allocation
    595  * @color: opaque tag value to use for the allocation
    596  * @start: start of the allowed range for the allocation
    597  * @end: end of the allowed range for the allocation
    598  *
    599  * This simply sets up the scanning routines with the parameters for the desired
    600  * hole. Note that there's no need to specify allocation flags, since they only
    601  * change the place a node is allocated from within a suitable hole.
    602  *
    603  * Warning:
    604  * As long as the scan list is non-empty, no other operations than
    605  * adding/removing nodes to/from the scan list are allowed.
    606  */
    607 void drm_mm_init_scan_with_range(struct drm_mm *mm,
    608 				 u64 size,
    609 				 unsigned alignment,
    610 				 unsigned long color,
    611 				 u64 start,
    612 				 u64 end)
    613 {
    614 	mm->scan_color = color;
    615 	mm->scan_alignment = alignment;
    616 	mm->scan_size = size;
    617 	mm->scanned_blocks = 0;
    618 	mm->scan_hit_start = 0;
    619 	mm->scan_hit_end = 0;
    620 	mm->scan_start = start;
    621 	mm->scan_end = end;
    622 	mm->scan_check_range = 1;
    623 	mm->prev_scanned_node = NULL;
    624 }
    625 EXPORT_SYMBOL(drm_mm_init_scan_with_range);
    626 
    627 /**
    628  * drm_mm_scan_add_block - add a node to the scan list
    629  * @node: drm_mm_node to add
    630  *
    631  * Add a node to the scan list that might be freed to make space for the desired
    632  * hole.
    633  *
    634  * Returns:
    635  * True if a hole has been found, false otherwise.
    636  */
    637 bool drm_mm_scan_add_block(struct drm_mm_node *node)
    638 {
    639 	struct drm_mm *mm = node->mm;
    640 	struct drm_mm_node *prev_node;
    641 	u64 hole_start, hole_end;
    642 	u64 adj_start, adj_end;
    643 
    644 	mm->scanned_blocks++;
    645 
    646 	BUG_ON(node->scanned_block);
    647 	node->scanned_block = 1;
    648 
    649 	prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
    650 			       node_list);
    651 
    652 	node->scanned_preceeds_hole = prev_node->hole_follows;
    653 	prev_node->hole_follows = 1;
    654 	list_del(&node->node_list);
    655 	node->node_list.prev = &prev_node->node_list;
    656 	node->node_list.next = &mm->prev_scanned_node->node_list;
    657 	mm->prev_scanned_node = node;
    658 
    659 	adj_start = hole_start = drm_mm_hole_node_start(prev_node);
    660 	adj_end = hole_end = drm_mm_hole_node_end(prev_node);
    661 
    662 	if (mm->scan_check_range) {
    663 		if (adj_start < mm->scan_start)
    664 			adj_start = mm->scan_start;
    665 		if (adj_end > mm->scan_end)
    666 			adj_end = mm->scan_end;
    667 	}
    668 
    669 	if (mm->color_adjust)
    670 		mm->color_adjust(prev_node, mm->scan_color,
    671 				 &adj_start, &adj_end);
    672 
    673 	if (check_free_hole(adj_start, adj_end,
    674 			    mm->scan_size, mm->scan_alignment)) {
    675 		mm->scan_hit_start = hole_start;
    676 		mm->scan_hit_end = hole_end;
    677 		return true;
    678 	}
    679 
    680 	return false;
    681 }
    682 EXPORT_SYMBOL(drm_mm_scan_add_block);
    683 
    684 /**
    685  * drm_mm_scan_remove_block - remove a node from the scan list
    686  * @node: drm_mm_node to remove
    687  *
    688  * Nodes _must_ be removed in the exact same order from the scan list as they
    689  * have been added, otherwise the internal state of the memory manager will be
    690  * corrupted.
    691  *
    692  * When the scan list is empty, the selected memory nodes can be freed. An
    693  * immediately following drm_mm_search_free with !DRM_MM_SEARCH_BEST will then
    694  * return the just freed block (because its at the top of the free_stack list).
    695  *
    696  * Returns:
    697  * True if this block should be evicted, false otherwise. Will always
    698  * return false when no hole has been found.
    699  */
    700 bool drm_mm_scan_remove_block(struct drm_mm_node *node)
    701 {
    702 	struct drm_mm *mm = node->mm;
    703 	struct drm_mm_node *prev_node;
    704 
    705 	mm->scanned_blocks--;
    706 
    707 	BUG_ON(!node->scanned_block);
    708 	node->scanned_block = 0;
    709 
    710 	prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
    711 			       node_list);
    712 
    713 	prev_node->hole_follows = node->scanned_preceeds_hole;
    714 	list_add(&node->node_list, &prev_node->node_list);
    715 
    716 	 return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
    717 		 node->start < mm->scan_hit_end);
    718 }
    719 EXPORT_SYMBOL(drm_mm_scan_remove_block);
    720 
    721 /**
    722  * drm_mm_clean - checks whether an allocator is clean
    723  * @mm: drm_mm allocator to check
    724  *
    725  * Returns:
    726  * True if the allocator is completely free, false if there's still a node
    727  * allocated in it.
    728  */
    729 bool drm_mm_clean(struct drm_mm * mm)
    730 {
    731 	struct list_head *head = &mm->head_node.node_list;
    732 
    733 	return (head->next->next == head);
    734 }
    735 EXPORT_SYMBOL(drm_mm_clean);
    736 
    737 /**
    738  * drm_mm_init - initialize a drm-mm allocator
    739  * @mm: the drm_mm structure to initialize
    740  * @start: start of the range managed by @mm
    741  * @size: end of the range managed by @mm
    742  *
    743  * Note that @mm must be cleared to 0 before calling this function.
    744  */
    745 void drm_mm_init(struct drm_mm * mm, u64 start, u64 size)
    746 {
    747 	INIT_LIST_HEAD(&mm->hole_stack);
    748 	mm->scanned_blocks = 0;
    749 
    750 	/* Clever trick to avoid a special case in the free hole tracking. */
    751 	INIT_LIST_HEAD(&mm->head_node.node_list);
    752 	INIT_LIST_HEAD(&mm->head_node.hole_stack);
    753 	mm->head_node.hole_follows = 1;
    754 	mm->head_node.scanned_block = 0;
    755 	mm->head_node.scanned_prev_free = 0;
    756 	mm->head_node.scanned_next_free = 0;
    757 	mm->head_node.mm = mm;
    758 	mm->head_node.start = start + size;
    759 	mm->head_node.size = start - mm->head_node.start;
    760 	list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
    761 
    762 	mm->color_adjust = NULL;
    763 }
    764 EXPORT_SYMBOL(drm_mm_init);
    765 
    766 /**
    767  * drm_mm_takedown - clean up a drm_mm allocator
    768  * @mm: drm_mm allocator to clean up
    769  *
    770  * Note that it is a bug to call this function on an allocator which is not
    771  * clean.
    772  */
    773 void drm_mm_takedown(struct drm_mm * mm)
    774 {
    775 	WARN(!list_empty(&mm->head_node.node_list),
    776 	     "Memory manager not clean during takedown.\n");
    777 }
    778 EXPORT_SYMBOL(drm_mm_takedown);
    779 
    780 static u64 drm_mm_debug_hole(struct drm_mm_node *entry,
    781 				     const char *prefix)
    782 {
    783 	u64 hole_start, hole_end, hole_size;
    784 
    785 	if (entry->hole_follows) {
    786 		hole_start = drm_mm_hole_node_start(entry);
    787 		hole_end = drm_mm_hole_node_end(entry);
    788 		hole_size = hole_end - hole_start;
    789 		pr_debug("%s %#llx-%#llx: %llu: free\n", prefix, hole_start,
    790 			 hole_end, hole_size);
    791 		return hole_size;
    792 	}
    793 
    794 	return 0;
    795 }
    796 
    797 /**
    798  * drm_mm_debug_table - dump allocator state to dmesg
    799  * @mm: drm_mm allocator to dump
    800  * @prefix: prefix to use for dumping to dmesg
    801  */
    802 void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
    803 {
    804 	struct drm_mm_node *entry;
    805 	u64 total_used = 0, total_free = 0, total = 0;
    806 
    807 	total_free += drm_mm_debug_hole(&mm->head_node, prefix);
    808 
    809 	drm_mm_for_each_node(entry, mm) {
    810 		pr_debug("%s %#llx-%#llx: %llu: used\n", prefix, entry->start,
    811 			 entry->start + entry->size, entry->size);
    812 		total_used += entry->size;
    813 		total_free += drm_mm_debug_hole(entry, prefix);
    814 	}
    815 	total = total_free + total_used;
    816 
    817 	pr_debug("%s total: %llu, used %llu free %llu\n", prefix, total,
    818 		 total_used, total_free);
    819 }
    820 EXPORT_SYMBOL(drm_mm_debug_table);
    821 
    822 #if defined(CONFIG_DEBUG_FS)
    823 static u64 drm_mm_dump_hole(struct seq_file *m, struct drm_mm_node *entry)
    824 {
    825 	u64 hole_start, hole_end, hole_size;
    826 
    827 	if (entry->hole_follows) {
    828 		hole_start = drm_mm_hole_node_start(entry);
    829 		hole_end = drm_mm_hole_node_end(entry);
    830 		hole_size = hole_end - hole_start;
    831 		seq_printf(m, "%#018llx-%#018llx: %llu: free\n", hole_start,
    832 			   hole_end, hole_size);
    833 		return hole_size;
    834 	}
    835 
    836 	return 0;
    837 }
    838 
    839 /**
    840  * drm_mm_dump_table - dump allocator state to a seq_file
    841  * @m: seq_file to dump to
    842  * @mm: drm_mm allocator to dump
    843  */
    844 int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
    845 {
    846 	struct drm_mm_node *entry;
    847 	u64 total_used = 0, total_free = 0, total = 0;
    848 
    849 	total_free += drm_mm_dump_hole(m, &mm->head_node);
    850 
    851 	drm_mm_for_each_node(entry, mm) {
    852 		seq_printf(m, "%#018llx-%#018llx: %llu: used\n", entry->start,
    853 			   entry->start + entry->size, entry->size);
    854 		total_used += entry->size;
    855 		total_free += drm_mm_dump_hole(m, entry);
    856 	}
    857 	total = total_free + total_used;
    858 
    859 	seq_printf(m, "total: %llu, used %llu free %llu\n", total,
    860 		   total_used, total_free);
    861 	return 0;
    862 }
    863 EXPORT_SYMBOL(drm_mm_dump_table);
    864 #endif
    865