Linux 4.9.243
[linux/fpc-iii.git] / drivers / gpu / drm / drm_mm.c
blob11f54df0c19bdc156d38f882e99f146459a9d3a5
1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
4 * All Rights Reserved.
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:
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.
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.
27 **************************************************************************/
30 * Generic simple memory manager implementation. Intended to be used as a base
31 * class implementation for more advanced memory managers.
33 * Note that the algorithm used is quite simple and there might be substantial
34 * performance gains if a smarter free list is implemented. Currently it is just an
35 * unordered stack of free regions. This could easily be improved if an RB-tree
36 * is used instead. At least if we expect heavy fragmentation.
38 * Aligned allocations can also see improvement.
40 * Authors:
41 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
44 #include <drm/drmP.h>
45 #include <drm/drm_mm.h>
46 #include <linux/slab.h>
47 #include <linux/seq_file.h>
48 #include <linux/export.h>
49 #include <linux/interval_tree_generic.h>
51 /**
52 * DOC: Overview
54 * drm_mm provides a simple range allocator. The drivers are free to use the
55 * resource allocator from the linux core if it suits them, the upside of drm_mm
56 * is that it's in the DRM core. Which means that it's easier to extend for
57 * some of the crazier special purpose needs of gpus.
59 * The main data struct is &drm_mm, allocations are tracked in &drm_mm_node.
60 * Drivers are free to embed either of them into their own suitable
61 * datastructures. drm_mm itself will not do any allocations of its own, so if
62 * drivers choose not to embed nodes they need to still allocate them
63 * themselves.
65 * The range allocator also supports reservation of preallocated blocks. This is
66 * useful for taking over initial mode setting configurations from the firmware,
67 * where an object needs to be created which exactly matches the firmware's
68 * scanout target. As long as the range is still free it can be inserted anytime
69 * after the allocator is initialized, which helps with avoiding looped
70 * depencies in the driver load sequence.
72 * drm_mm maintains a stack of most recently freed holes, which of all
73 * simplistic datastructures seems to be a fairly decent approach to clustering
74 * allocations and avoiding too much fragmentation. This means free space
75 * searches are O(num_holes). Given that all the fancy features drm_mm supports
76 * something better would be fairly complex and since gfx thrashing is a fairly
77 * steep cliff not a real concern. Removing a node again is O(1).
79 * drm_mm supports a few features: Alignment and range restrictions can be
80 * supplied. Further more every &drm_mm_node has a color value (which is just an
81 * opaqua unsigned long) which in conjunction with a driver callback can be used
82 * to implement sophisticated placement restrictions. The i915 DRM driver uses
83 * this to implement guard pages between incompatible caching domains in the
84 * graphics TT.
86 * Two behaviors are supported for searching and allocating: bottom-up and top-down.
87 * The default is bottom-up. Top-down allocation can be used if the memory area
88 * has different restrictions, or just to reduce fragmentation.
90 * Finally iteration helpers to walk all nodes and all holes are provided as are
91 * some basic allocator dumpers for debugging.
94 static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
95 u64 size,
96 unsigned alignment,
97 unsigned long color,
98 enum drm_mm_search_flags flags);
99 static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
100 u64 size,
101 unsigned alignment,
102 unsigned long color,
103 u64 start,
104 u64 end,
105 enum drm_mm_search_flags flags);
107 #define START(node) ((node)->start)
108 #define LAST(node) ((node)->start + (node)->size - 1)
110 INTERVAL_TREE_DEFINE(struct drm_mm_node, rb,
111 u64, __subtree_last,
112 START, LAST, static inline, drm_mm_interval_tree)
114 struct drm_mm_node *
115 drm_mm_interval_first(struct drm_mm *mm, u64 start, u64 last)
117 return drm_mm_interval_tree_iter_first(&mm->interval_tree,
118 start, last);
120 EXPORT_SYMBOL(drm_mm_interval_first);
122 struct drm_mm_node *
123 drm_mm_interval_next(struct drm_mm_node *node, u64 start, u64 last)
125 return drm_mm_interval_tree_iter_next(node, start, last);
127 EXPORT_SYMBOL(drm_mm_interval_next);
129 static void drm_mm_interval_tree_add_node(struct drm_mm_node *hole_node,
130 struct drm_mm_node *node)
132 struct drm_mm *mm = hole_node->mm;
133 struct rb_node **link, *rb;
134 struct drm_mm_node *parent;
136 node->__subtree_last = LAST(node);
138 if (hole_node->allocated) {
139 rb = &hole_node->rb;
140 while (rb) {
141 parent = rb_entry(rb, struct drm_mm_node, rb);
142 if (parent->__subtree_last >= node->__subtree_last)
143 break;
145 parent->__subtree_last = node->__subtree_last;
146 rb = rb_parent(rb);
149 rb = &hole_node->rb;
150 link = &hole_node->rb.rb_right;
151 } else {
152 rb = NULL;
153 link = &mm->interval_tree.rb_node;
156 while (*link) {
157 rb = *link;
158 parent = rb_entry(rb, struct drm_mm_node, rb);
159 if (parent->__subtree_last < node->__subtree_last)
160 parent->__subtree_last = node->__subtree_last;
161 if (node->start < parent->start)
162 link = &parent->rb.rb_left;
163 else
164 link = &parent->rb.rb_right;
167 rb_link_node(&node->rb, rb, link);
168 rb_insert_augmented(&node->rb,
169 &mm->interval_tree,
170 &drm_mm_interval_tree_augment);
173 static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
174 struct drm_mm_node *node,
175 u64 size, unsigned alignment,
176 unsigned long color,
177 enum drm_mm_allocator_flags flags)
179 struct drm_mm *mm = hole_node->mm;
180 u64 hole_start = drm_mm_hole_node_start(hole_node);
181 u64 hole_end = drm_mm_hole_node_end(hole_node);
182 u64 adj_start = hole_start;
183 u64 adj_end = hole_end;
185 BUG_ON(node->allocated);
187 if (mm->color_adjust)
188 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
190 if (flags & DRM_MM_CREATE_TOP)
191 adj_start = adj_end - size;
193 if (alignment) {
194 u64 tmp = adj_start;
195 unsigned rem;
197 rem = do_div(tmp, alignment);
198 if (rem) {
199 if (flags & DRM_MM_CREATE_TOP)
200 adj_start -= rem;
201 else
202 adj_start += alignment - rem;
206 BUG_ON(adj_start < hole_start);
207 BUG_ON(adj_end > hole_end);
209 if (adj_start == hole_start) {
210 hole_node->hole_follows = 0;
211 list_del(&hole_node->hole_stack);
214 node->start = adj_start;
215 node->size = size;
216 node->mm = mm;
217 node->color = color;
218 node->allocated = 1;
220 list_add(&node->node_list, &hole_node->node_list);
222 drm_mm_interval_tree_add_node(hole_node, node);
224 BUG_ON(node->start + node->size > adj_end);
226 node->hole_follows = 0;
227 if (__drm_mm_hole_node_start(node) < hole_end) {
228 list_add(&node->hole_stack, &mm->hole_stack);
229 node->hole_follows = 1;
234 * drm_mm_reserve_node - insert an pre-initialized node
235 * @mm: drm_mm allocator to insert @node into
236 * @node: drm_mm_node to insert
238 * This functions inserts an already set-up drm_mm_node into the allocator,
239 * meaning that start, size and color must be set by the caller. This is useful
240 * to initialize the allocator with preallocated objects which must be set-up
241 * before the range allocator can be set-up, e.g. when taking over a firmware
242 * framebuffer.
244 * Returns:
245 * 0 on success, -ENOSPC if there's no hole where @node is.
247 int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
249 u64 end = node->start + node->size;
250 struct drm_mm_node *hole;
251 u64 hole_start, hole_end;
253 if (WARN_ON(node->size == 0))
254 return -EINVAL;
256 end = node->start + node->size;
258 /* Find the relevant hole to add our node to */
259 hole = drm_mm_interval_tree_iter_first(&mm->interval_tree,
260 node->start, ~(u64)0);
261 if (hole) {
262 if (hole->start < end)
263 return -ENOSPC;
264 } else {
265 hole = list_entry(&mm->head_node.node_list,
266 typeof(*hole), node_list);
269 hole = list_last_entry(&hole->node_list, typeof(*hole), node_list);
270 if (!hole->hole_follows)
271 return -ENOSPC;
273 hole_start = __drm_mm_hole_node_start(hole);
274 hole_end = __drm_mm_hole_node_end(hole);
275 if (hole_start > node->start || hole_end < end)
276 return -ENOSPC;
278 node->mm = mm;
279 node->allocated = 1;
281 list_add(&node->node_list, &hole->node_list);
283 drm_mm_interval_tree_add_node(hole, node);
285 if (node->start == hole_start) {
286 hole->hole_follows = 0;
287 list_del(&hole->hole_stack);
290 node->hole_follows = 0;
291 if (end != hole_end) {
292 list_add(&node->hole_stack, &mm->hole_stack);
293 node->hole_follows = 1;
296 return 0;
298 EXPORT_SYMBOL(drm_mm_reserve_node);
301 * drm_mm_insert_node_generic - search for space and insert @node
302 * @mm: drm_mm to allocate from
303 * @node: preallocate node to insert
304 * @size: size of the allocation
305 * @alignment: alignment of the allocation
306 * @color: opaque tag value to use for this node
307 * @sflags: flags to fine-tune the allocation search
308 * @aflags: flags to fine-tune the allocation behavior
310 * The preallocated node must be cleared to 0.
312 * Returns:
313 * 0 on success, -ENOSPC if there's no suitable hole.
315 int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
316 u64 size, unsigned alignment,
317 unsigned long color,
318 enum drm_mm_search_flags sflags,
319 enum drm_mm_allocator_flags aflags)
321 struct drm_mm_node *hole_node;
323 if (WARN_ON(size == 0))
324 return -EINVAL;
326 hole_node = drm_mm_search_free_generic(mm, size, alignment,
327 color, sflags);
328 if (!hole_node)
329 return -ENOSPC;
331 drm_mm_insert_helper(hole_node, node, size, alignment, color, aflags);
332 return 0;
334 EXPORT_SYMBOL(drm_mm_insert_node_generic);
336 static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
337 struct drm_mm_node *node,
338 u64 size, unsigned alignment,
339 unsigned long color,
340 u64 start, u64 end,
341 enum drm_mm_allocator_flags flags)
343 struct drm_mm *mm = hole_node->mm;
344 u64 hole_start = drm_mm_hole_node_start(hole_node);
345 u64 hole_end = drm_mm_hole_node_end(hole_node);
346 u64 adj_start = hole_start;
347 u64 adj_end = hole_end;
349 BUG_ON(!hole_node->hole_follows || node->allocated);
351 if (mm->color_adjust)
352 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
354 adj_start = max(adj_start, start);
355 adj_end = min(adj_end, end);
357 if (flags & DRM_MM_CREATE_TOP)
358 adj_start = adj_end - size;
360 if (alignment) {
361 u64 tmp = adj_start;
362 unsigned rem;
364 rem = do_div(tmp, alignment);
365 if (rem) {
366 if (flags & DRM_MM_CREATE_TOP)
367 adj_start -= rem;
368 else
369 adj_start += alignment - rem;
373 if (adj_start == hole_start) {
374 hole_node->hole_follows = 0;
375 list_del(&hole_node->hole_stack);
378 node->start = adj_start;
379 node->size = size;
380 node->mm = mm;
381 node->color = color;
382 node->allocated = 1;
384 list_add(&node->node_list, &hole_node->node_list);
386 drm_mm_interval_tree_add_node(hole_node, node);
388 BUG_ON(node->start < start);
389 BUG_ON(node->start < adj_start);
390 BUG_ON(node->start + node->size > adj_end);
391 BUG_ON(node->start + node->size > end);
393 node->hole_follows = 0;
394 if (__drm_mm_hole_node_start(node) < hole_end) {
395 list_add(&node->hole_stack, &mm->hole_stack);
396 node->hole_follows = 1;
401 * drm_mm_insert_node_in_range_generic - ranged search for space and insert @node
402 * @mm: drm_mm to allocate from
403 * @node: preallocate node to insert
404 * @size: size of the allocation
405 * @alignment: alignment of the allocation
406 * @color: opaque tag value to use for this node
407 * @start: start of the allowed range for this node
408 * @end: end of the allowed range for this node
409 * @sflags: flags to fine-tune the allocation search
410 * @aflags: flags to fine-tune the allocation behavior
412 * The preallocated node must be cleared to 0.
414 * Returns:
415 * 0 on success, -ENOSPC if there's no suitable hole.
417 int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
418 u64 size, unsigned alignment,
419 unsigned long color,
420 u64 start, u64 end,
421 enum drm_mm_search_flags sflags,
422 enum drm_mm_allocator_flags aflags)
424 struct drm_mm_node *hole_node;
426 if (WARN_ON(size == 0))
427 return -EINVAL;
429 hole_node = drm_mm_search_free_in_range_generic(mm,
430 size, alignment, color,
431 start, end, sflags);
432 if (!hole_node)
433 return -ENOSPC;
435 drm_mm_insert_helper_range(hole_node, node,
436 size, alignment, color,
437 start, end, aflags);
438 return 0;
440 EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
443 * drm_mm_remove_node - Remove a memory node from the allocator.
444 * @node: drm_mm_node to remove
446 * This just removes a node from its drm_mm allocator. The node does not need to
447 * be cleared again before it can be re-inserted into this or any other drm_mm
448 * allocator. It is a bug to call this function on a un-allocated node.
450 void drm_mm_remove_node(struct drm_mm_node *node)
452 struct drm_mm *mm = node->mm;
453 struct drm_mm_node *prev_node;
455 if (WARN_ON(!node->allocated))
456 return;
458 BUG_ON(node->scanned_block || node->scanned_prev_free
459 || node->scanned_next_free);
461 prev_node =
462 list_entry(node->node_list.prev, struct drm_mm_node, node_list);
464 if (node->hole_follows) {
465 BUG_ON(__drm_mm_hole_node_start(node) ==
466 __drm_mm_hole_node_end(node));
467 list_del(&node->hole_stack);
468 } else
469 BUG_ON(__drm_mm_hole_node_start(node) !=
470 __drm_mm_hole_node_end(node));
473 if (!prev_node->hole_follows) {
474 prev_node->hole_follows = 1;
475 list_add(&prev_node->hole_stack, &mm->hole_stack);
476 } else
477 list_move(&prev_node->hole_stack, &mm->hole_stack);
479 drm_mm_interval_tree_remove(node, &mm->interval_tree);
480 list_del(&node->node_list);
481 node->allocated = 0;
483 EXPORT_SYMBOL(drm_mm_remove_node);
485 static int check_free_hole(u64 start, u64 end, u64 size, unsigned alignment)
487 if (end - start < size)
488 return 0;
490 if (alignment) {
491 u64 tmp = start;
492 unsigned rem;
494 rem = do_div(tmp, alignment);
495 if (rem)
496 start += alignment - rem;
499 return end >= start + size;
502 static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
503 u64 size,
504 unsigned alignment,
505 unsigned long color,
506 enum drm_mm_search_flags flags)
508 struct drm_mm_node *entry;
509 struct drm_mm_node *best;
510 u64 adj_start;
511 u64 adj_end;
512 u64 best_size;
514 BUG_ON(mm->scanned_blocks);
516 best = NULL;
517 best_size = ~0UL;
519 __drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
520 flags & DRM_MM_SEARCH_BELOW) {
521 u64 hole_size = adj_end - adj_start;
523 if (mm->color_adjust) {
524 mm->color_adjust(entry, color, &adj_start, &adj_end);
525 if (adj_end <= adj_start)
526 continue;
529 if (!check_free_hole(adj_start, adj_end, size, alignment))
530 continue;
532 if (!(flags & DRM_MM_SEARCH_BEST))
533 return entry;
535 if (hole_size < best_size) {
536 best = entry;
537 best_size = hole_size;
541 return best;
544 static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
545 u64 size,
546 unsigned alignment,
547 unsigned long color,
548 u64 start,
549 u64 end,
550 enum drm_mm_search_flags flags)
552 struct drm_mm_node *entry;
553 struct drm_mm_node *best;
554 u64 adj_start;
555 u64 adj_end;
556 u64 best_size;
558 BUG_ON(mm->scanned_blocks);
560 best = NULL;
561 best_size = ~0UL;
563 __drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
564 flags & DRM_MM_SEARCH_BELOW) {
565 u64 hole_size = adj_end - adj_start;
567 if (mm->color_adjust) {
568 mm->color_adjust(entry, color, &adj_start, &adj_end);
569 if (adj_end <= adj_start)
570 continue;
573 adj_start = max(adj_start, start);
574 adj_end = min(adj_end, end);
576 if (!check_free_hole(adj_start, adj_end, size, alignment))
577 continue;
579 if (!(flags & DRM_MM_SEARCH_BEST))
580 return entry;
582 if (hole_size < best_size) {
583 best = entry;
584 best_size = hole_size;
588 return best;
592 * drm_mm_replace_node - move an allocation from @old to @new
593 * @old: drm_mm_node to remove from the allocator
594 * @new: drm_mm_node which should inherit @old's allocation
596 * This is useful for when drivers embed the drm_mm_node structure and hence
597 * can't move allocations by reassigning pointers. It's a combination of remove
598 * and insert with the guarantee that the allocation start will match.
600 void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
602 list_replace(&old->node_list, &new->node_list);
603 list_replace(&old->hole_stack, &new->hole_stack);
604 rb_replace_node(&old->rb, &new->rb, &old->mm->interval_tree);
605 new->hole_follows = old->hole_follows;
606 new->mm = old->mm;
607 new->start = old->start;
608 new->size = old->size;
609 new->color = old->color;
610 new->__subtree_last = old->__subtree_last;
612 old->allocated = 0;
613 new->allocated = 1;
615 EXPORT_SYMBOL(drm_mm_replace_node);
618 * DOC: lru scan roaster
620 * Very often GPUs need to have continuous allocations for a given object. When
621 * evicting objects to make space for a new one it is therefore not most
622 * efficient when we simply start to select all objects from the tail of an LRU
623 * until there's a suitable hole: Especially for big objects or nodes that
624 * otherwise have special allocation constraints there's a good chance we evict
625 * lots of (smaller) objects unecessarily.
627 * The DRM range allocator supports this use-case through the scanning
628 * interfaces. First a scan operation needs to be initialized with
629 * drm_mm_init_scan() or drm_mm_init_scan_with_range(). The the driver adds
630 * objects to the roaster (probably by walking an LRU list, but this can be
631 * freely implemented) until a suitable hole is found or there's no further
632 * evitable object.
634 * The the driver must walk through all objects again in exactly the reverse
635 * order to restore the allocator state. Note that while the allocator is used
636 * in the scan mode no other operation is allowed.
638 * Finally the driver evicts all objects selected in the scan. Adding and
639 * removing an object is O(1), and since freeing a node is also O(1) the overall
640 * complexity is O(scanned_objects). So like the free stack which needs to be
641 * walked before a scan operation even begins this is linear in the number of
642 * objects. It doesn't seem to hurt badly.
646 * drm_mm_init_scan - initialize lru scanning
647 * @mm: drm_mm to scan
648 * @size: size of the allocation
649 * @alignment: alignment of the allocation
650 * @color: opaque tag value to use for the allocation
652 * This simply sets up the scanning routines with the parameters for the desired
653 * hole. Note that there's no need to specify allocation flags, since they only
654 * change the place a node is allocated from within a suitable hole.
656 * Warning:
657 * As long as the scan list is non-empty, no other operations than
658 * adding/removing nodes to/from the scan list are allowed.
660 void drm_mm_init_scan(struct drm_mm *mm,
661 u64 size,
662 unsigned alignment,
663 unsigned long color)
665 mm->scan_color = color;
666 mm->scan_alignment = alignment;
667 mm->scan_size = size;
668 mm->scanned_blocks = 0;
669 mm->scan_hit_start = 0;
670 mm->scan_hit_end = 0;
671 mm->scan_check_range = 0;
672 mm->prev_scanned_node = NULL;
674 EXPORT_SYMBOL(drm_mm_init_scan);
677 * drm_mm_init_scan - initialize range-restricted lru scanning
678 * @mm: drm_mm to scan
679 * @size: size of the allocation
680 * @alignment: alignment of the allocation
681 * @color: opaque tag value to use for the allocation
682 * @start: start of the allowed range for the allocation
683 * @end: end of the allowed range for the allocation
685 * This simply sets up the scanning routines with the parameters for the desired
686 * hole. Note that there's no need to specify allocation flags, since they only
687 * change the place a node is allocated from within a suitable hole.
689 * Warning:
690 * As long as the scan list is non-empty, no other operations than
691 * adding/removing nodes to/from the scan list are allowed.
693 void drm_mm_init_scan_with_range(struct drm_mm *mm,
694 u64 size,
695 unsigned alignment,
696 unsigned long color,
697 u64 start,
698 u64 end)
700 mm->scan_color = color;
701 mm->scan_alignment = alignment;
702 mm->scan_size = size;
703 mm->scanned_blocks = 0;
704 mm->scan_hit_start = 0;
705 mm->scan_hit_end = 0;
706 mm->scan_start = start;
707 mm->scan_end = end;
708 mm->scan_check_range = 1;
709 mm->prev_scanned_node = NULL;
711 EXPORT_SYMBOL(drm_mm_init_scan_with_range);
714 * drm_mm_scan_add_block - add a node to the scan list
715 * @node: drm_mm_node to add
717 * Add a node to the scan list that might be freed to make space for the desired
718 * hole.
720 * Returns:
721 * True if a hole has been found, false otherwise.
723 bool drm_mm_scan_add_block(struct drm_mm_node *node)
725 struct drm_mm *mm = node->mm;
726 struct drm_mm_node *prev_node;
727 u64 hole_start, hole_end;
728 u64 adj_start, adj_end;
730 mm->scanned_blocks++;
732 BUG_ON(node->scanned_block);
733 node->scanned_block = 1;
735 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
736 node_list);
738 node->scanned_preceeds_hole = prev_node->hole_follows;
739 prev_node->hole_follows = 1;
740 list_del(&node->node_list);
741 node->node_list.prev = &prev_node->node_list;
742 node->node_list.next = &mm->prev_scanned_node->node_list;
743 mm->prev_scanned_node = node;
745 adj_start = hole_start = drm_mm_hole_node_start(prev_node);
746 adj_end = hole_end = drm_mm_hole_node_end(prev_node);
748 if (mm->scan_check_range) {
749 if (adj_start < mm->scan_start)
750 adj_start = mm->scan_start;
751 if (adj_end > mm->scan_end)
752 adj_end = mm->scan_end;
755 if (mm->color_adjust)
756 mm->color_adjust(prev_node, mm->scan_color,
757 &adj_start, &adj_end);
759 if (check_free_hole(adj_start, adj_end,
760 mm->scan_size, mm->scan_alignment)) {
761 mm->scan_hit_start = hole_start;
762 mm->scan_hit_end = hole_end;
763 return true;
766 return false;
768 EXPORT_SYMBOL(drm_mm_scan_add_block);
771 * drm_mm_scan_remove_block - remove a node from the scan list
772 * @node: drm_mm_node to remove
774 * Nodes _must_ be removed in the exact same order from the scan list as they
775 * have been added, otherwise the internal state of the memory manager will be
776 * corrupted.
778 * When the scan list is empty, the selected memory nodes can be freed. An
779 * immediately following drm_mm_search_free with !DRM_MM_SEARCH_BEST will then
780 * return the just freed block (because its at the top of the free_stack list).
782 * Returns:
783 * True if this block should be evicted, false otherwise. Will always
784 * return false when no hole has been found.
786 bool drm_mm_scan_remove_block(struct drm_mm_node *node)
788 struct drm_mm *mm = node->mm;
789 struct drm_mm_node *prev_node;
791 mm->scanned_blocks--;
793 BUG_ON(!node->scanned_block);
794 node->scanned_block = 0;
796 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
797 node_list);
799 prev_node->hole_follows = node->scanned_preceeds_hole;
800 list_add(&node->node_list, &prev_node->node_list);
802 return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
803 node->start < mm->scan_hit_end);
805 EXPORT_SYMBOL(drm_mm_scan_remove_block);
808 * drm_mm_clean - checks whether an allocator is clean
809 * @mm: drm_mm allocator to check
811 * Returns:
812 * True if the allocator is completely free, false if there's still a node
813 * allocated in it.
815 bool drm_mm_clean(struct drm_mm * mm)
817 struct list_head *head = &mm->head_node.node_list;
819 return (head->next->next == head);
821 EXPORT_SYMBOL(drm_mm_clean);
824 * drm_mm_init - initialize a drm-mm allocator
825 * @mm: the drm_mm structure to initialize
826 * @start: start of the range managed by @mm
827 * @size: end of the range managed by @mm
829 * Note that @mm must be cleared to 0 before calling this function.
831 void drm_mm_init(struct drm_mm * mm, u64 start, u64 size)
833 INIT_LIST_HEAD(&mm->hole_stack);
834 mm->scanned_blocks = 0;
836 /* Clever trick to avoid a special case in the free hole tracking. */
837 INIT_LIST_HEAD(&mm->head_node.node_list);
838 mm->head_node.allocated = 0;
839 mm->head_node.hole_follows = 1;
840 mm->head_node.scanned_block = 0;
841 mm->head_node.scanned_prev_free = 0;
842 mm->head_node.scanned_next_free = 0;
843 mm->head_node.mm = mm;
844 mm->head_node.start = start + size;
845 mm->head_node.size = start - mm->head_node.start;
846 list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
848 mm->interval_tree = RB_ROOT;
850 mm->color_adjust = NULL;
852 EXPORT_SYMBOL(drm_mm_init);
855 * drm_mm_takedown - clean up a drm_mm allocator
856 * @mm: drm_mm allocator to clean up
858 * Note that it is a bug to call this function on an allocator which is not
859 * clean.
861 void drm_mm_takedown(struct drm_mm * mm)
863 WARN(!list_empty(&mm->head_node.node_list),
864 "Memory manager not clean during takedown.\n");
866 EXPORT_SYMBOL(drm_mm_takedown);
868 static u64 drm_mm_debug_hole(struct drm_mm_node *entry,
869 const char *prefix)
871 u64 hole_start, hole_end, hole_size;
873 if (entry->hole_follows) {
874 hole_start = drm_mm_hole_node_start(entry);
875 hole_end = drm_mm_hole_node_end(entry);
876 hole_size = hole_end - hole_start;
877 pr_debug("%s %#llx-%#llx: %llu: free\n", prefix, hole_start,
878 hole_end, hole_size);
879 return hole_size;
882 return 0;
886 * drm_mm_debug_table - dump allocator state to dmesg
887 * @mm: drm_mm allocator to dump
888 * @prefix: prefix to use for dumping to dmesg
890 void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
892 struct drm_mm_node *entry;
893 u64 total_used = 0, total_free = 0, total = 0;
895 total_free += drm_mm_debug_hole(&mm->head_node, prefix);
897 drm_mm_for_each_node(entry, mm) {
898 pr_debug("%s %#llx-%#llx: %llu: used\n", prefix, entry->start,
899 entry->start + entry->size, entry->size);
900 total_used += entry->size;
901 total_free += drm_mm_debug_hole(entry, prefix);
903 total = total_free + total_used;
905 pr_debug("%s total: %llu, used %llu free %llu\n", prefix, total,
906 total_used, total_free);
908 EXPORT_SYMBOL(drm_mm_debug_table);
910 #if defined(CONFIG_DEBUG_FS)
911 static u64 drm_mm_dump_hole(struct seq_file *m, struct drm_mm_node *entry)
913 u64 hole_start, hole_end, hole_size;
915 if (entry->hole_follows) {
916 hole_start = drm_mm_hole_node_start(entry);
917 hole_end = drm_mm_hole_node_end(entry);
918 hole_size = hole_end - hole_start;
919 seq_printf(m, "%#018llx-%#018llx: %llu: free\n", hole_start,
920 hole_end, hole_size);
921 return hole_size;
924 return 0;
928 * drm_mm_dump_table - dump allocator state to a seq_file
929 * @m: seq_file to dump to
930 * @mm: drm_mm allocator to dump
932 int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm)
934 struct drm_mm_node *entry;
935 u64 total_used = 0, total_free = 0, total = 0;
937 total_free += drm_mm_dump_hole(m, &mm->head_node);
939 drm_mm_for_each_node(entry, mm) {
940 seq_printf(m, "%#018llx-%#018llx: %llu: used\n", entry->start,
941 entry->start + entry->size, entry->size);
942 total_used += entry->size;
943 total_free += drm_mm_dump_hole(m, entry);
945 total = total_free + total_used;
947 seq_printf(m, "total: %llu, used %llu free %llu\n", total,
948 total_used, total_free);
949 return 0;
951 EXPORT_SYMBOL(drm_mm_dump_table);
952 #endif