1c0e09200SDave Airlie /**************************************************************************
2c0e09200SDave Airlie *
3c0e09200SDave Airlie * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
4ba004e39SChris Wilson * Copyright 2016 Intel Corporation
5c0e09200SDave Airlie * All Rights Reserved.
6c0e09200SDave Airlie *
7c0e09200SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie * copy of this software and associated documentation files (the
9c0e09200SDave Airlie * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie * the following conditions:
14c0e09200SDave Airlie *
15c0e09200SDave Airlie * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie * of the Software.
18c0e09200SDave Airlie *
19c0e09200SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20c0e09200SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21c0e09200SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22c0e09200SDave Airlie * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23c0e09200SDave Airlie * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24c0e09200SDave Airlie * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25c0e09200SDave Airlie * USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie *
27c0e09200SDave Airlie *
28c0e09200SDave Airlie **************************************************************************/
29c0e09200SDave Airlie
30c0e09200SDave Airlie /*
31c0e09200SDave Airlie * Generic simple memory manager implementation. Intended to be used as a base
32c0e09200SDave Airlie * class implementation for more advanced memory managers.
33c0e09200SDave Airlie *
34c0e09200SDave Airlie * Note that the algorithm used is quite simple and there might be substantial
35ba004e39SChris Wilson * performance gains if a smarter free list is implemented. Currently it is
36ba004e39SChris Wilson * just an unordered stack of free regions. This could easily be improved if
37ba004e39SChris Wilson * an RB-tree is used instead. At least if we expect heavy fragmentation.
38c0e09200SDave Airlie *
39c0e09200SDave Airlie * Aligned allocations can also see improvement.
40c0e09200SDave Airlie *
41c0e09200SDave Airlie * Authors:
42c0e09200SDave Airlie * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
43c0e09200SDave Airlie */
44c0e09200SDave Airlie
452d1a8a48SPaul Gortmaker #include <linux/export.h>
46202b52b7SChris Wilson #include <linux/interval_tree_generic.h>
470500c04eSSam Ravnborg #include <linux/seq_file.h>
480500c04eSSam Ravnborg #include <linux/slab.h>
490500c04eSSam Ravnborg #include <linux/stacktrace.h>
500500c04eSSam Ravnborg
510500c04eSSam Ravnborg #include <drm/drm_mm.h>
52c0e09200SDave Airlie
5393110be6SDaniel Vetter /**
5493110be6SDaniel Vetter * DOC: Overview
5593110be6SDaniel Vetter *
5693110be6SDaniel Vetter * drm_mm provides a simple range allocator. The drivers are free to use the
5793110be6SDaniel Vetter * resource allocator from the linux core if it suits them, the upside of drm_mm
5893110be6SDaniel Vetter * is that it's in the DRM core. Which means that it's easier to extend for
5993110be6SDaniel Vetter * some of the crazier special purpose needs of gpus.
6093110be6SDaniel Vetter *
6193110be6SDaniel Vetter * The main data struct is &drm_mm, allocations are tracked in &drm_mm_node.
6293110be6SDaniel Vetter * Drivers are free to embed either of them into their own suitable
6305fc0321SDaniel Vetter * datastructures. drm_mm itself will not do any memory allocations of its own,
6405fc0321SDaniel Vetter * so if drivers choose not to embed nodes they need to still allocate them
6593110be6SDaniel Vetter * themselves.
6693110be6SDaniel Vetter *
6793110be6SDaniel Vetter * The range allocator also supports reservation of preallocated blocks. This is
6893110be6SDaniel Vetter * useful for taking over initial mode setting configurations from the firmware,
6993110be6SDaniel Vetter * where an object needs to be created which exactly matches the firmware's
7093110be6SDaniel Vetter * scanout target. As long as the range is still free it can be inserted anytime
7193110be6SDaniel Vetter * after the allocator is initialized, which helps with avoiding looped
72ba004e39SChris Wilson * dependencies in the driver load sequence.
7393110be6SDaniel Vetter *
7493110be6SDaniel Vetter * drm_mm maintains a stack of most recently freed holes, which of all
7593110be6SDaniel Vetter * simplistic datastructures seems to be a fairly decent approach to clustering
7693110be6SDaniel Vetter * allocations and avoiding too much fragmentation. This means free space
7793110be6SDaniel Vetter * searches are O(num_holes). Given that all the fancy features drm_mm supports
7893110be6SDaniel Vetter * something better would be fairly complex and since gfx thrashing is a fairly
7993110be6SDaniel Vetter * steep cliff not a real concern. Removing a node again is O(1).
8093110be6SDaniel Vetter *
8193110be6SDaniel Vetter * drm_mm supports a few features: Alignment and range restrictions can be
8293110be6SDaniel Vetter * supplied. Furthermore every &drm_mm_node has a color value (which is just an
83ba004e39SChris Wilson * opaque unsigned long) which in conjunction with a driver callback can be used
8493110be6SDaniel Vetter * to implement sophisticated placement restrictions. The i915 DRM driver uses
8593110be6SDaniel Vetter * this to implement guard pages between incompatible caching domains in the
8693110be6SDaniel Vetter * graphics TT.
8793110be6SDaniel Vetter *
88ba004e39SChris Wilson * Two behaviors are supported for searching and allocating: bottom-up and
89ba004e39SChris Wilson * top-down. The default is bottom-up. Top-down allocation can be used if the
90ba004e39SChris Wilson * memory area has different restrictions, or just to reduce fragmentation.
9162347f9eSLauri Kasanen *
9293110be6SDaniel Vetter * Finally iteration helpers to walk all nodes and all holes are provided as are
9393110be6SDaniel Vetter * some basic allocator dumpers for debugging.
941c9bd1edSDaniel Vetter *
951c9bd1edSDaniel Vetter * Note that this range allocator is not thread-safe, drivers need to protect
9621be9154SLiviu Dudau * modifications with their own locking. The idea behind this is that for a full
971c9bd1edSDaniel Vetter * memory manager additional data needs to be protected anyway, hence internal
981c9bd1edSDaniel Vetter * locking would be fully redundant.
9993110be6SDaniel Vetter */
10093110be6SDaniel Vetter
1015705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM
10293ce75faSChris Wilson #include <linux/stackdepot.h>
10393ce75faSChris Wilson
1045705670dSChris Wilson #define STACKDEPTH 32
1055705670dSChris Wilson #define BUFSZ 4096
1065705670dSChris Wilson
save_stack(struct drm_mm_node * node)1075705670dSChris Wilson static noinline void save_stack(struct drm_mm_node *node)
1085705670dSChris Wilson {
1095705670dSChris Wilson unsigned long entries[STACKDEPTH];
110487f3c7fSThomas Gleixner unsigned int n;
1115705670dSChris Wilson
112487f3c7fSThomas Gleixner n = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
1135705670dSChris Wilson
1145705670dSChris Wilson /* May be called under spinlock, so avoid sleeping */
115487f3c7fSThomas Gleixner node->stack = stack_depot_save(entries, n, GFP_NOWAIT);
1165705670dSChris Wilson }
1175705670dSChris Wilson
show_leaks(struct drm_mm * mm)1185705670dSChris Wilson static void show_leaks(struct drm_mm *mm)
1195705670dSChris Wilson {
1205705670dSChris Wilson struct drm_mm_node *node;
1215705670dSChris Wilson char *buf;
1225705670dSChris Wilson
1235705670dSChris Wilson buf = kmalloc(BUFSZ, GFP_KERNEL);
1245705670dSChris Wilson if (!buf)
1255705670dSChris Wilson return;
1265705670dSChris Wilson
1272bc98c86SChris Wilson list_for_each_entry(node, drm_mm_nodes(mm), node_list) {
1285705670dSChris Wilson if (!node->stack) {
1295705670dSChris Wilson DRM_ERROR("node [%08llx + %08llx]: unknown owner\n",
1305705670dSChris Wilson node->start, node->size);
1315705670dSChris Wilson continue;
1325705670dSChris Wilson }
1335705670dSChris Wilson
1340f68d45eSImran Khan stack_depot_snprint(node->stack, buf, BUFSZ, 0);
1355705670dSChris Wilson DRM_ERROR("node [%08llx + %08llx]: inserted at\n%s",
1365705670dSChris Wilson node->start, node->size, buf);
1375705670dSChris Wilson }
1385705670dSChris Wilson
1395705670dSChris Wilson kfree(buf);
1405705670dSChris Wilson }
1415705670dSChris Wilson
1425705670dSChris Wilson #undef STACKDEPTH
1435705670dSChris Wilson #undef BUFSZ
1445705670dSChris Wilson #else
save_stack(struct drm_mm_node * node)1455705670dSChris Wilson static void save_stack(struct drm_mm_node *node) { }
show_leaks(struct drm_mm * mm)1465705670dSChris Wilson static void show_leaks(struct drm_mm *mm) { }
1475705670dSChris Wilson #endif
1485705670dSChris Wilson
149202b52b7SChris Wilson #define START(node) ((node)->start)
150202b52b7SChris Wilson #define LAST(node) ((node)->start + (node)->size - 1)
151202b52b7SChris Wilson
INTERVAL_TREE_DEFINE(struct drm_mm_node,rb,u64,__subtree_last,START,LAST,static inline __maybe_unused,drm_mm_interval_tree)152202b52b7SChris Wilson INTERVAL_TREE_DEFINE(struct drm_mm_node, rb,
153202b52b7SChris Wilson u64, __subtree_last,
154*2ee46de4SAndy Shevchenko START, LAST, static inline __maybe_unused, drm_mm_interval_tree)
155202b52b7SChris Wilson
156202b52b7SChris Wilson struct drm_mm_node *
15745b186f1SChris Wilson __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last)
158202b52b7SChris Wilson {
159f808c13fSDavidlohr Bueso return drm_mm_interval_tree_iter_first((struct rb_root_cached *)&mm->interval_tree,
160bbba9693SChris Wilson start, last) ?: (struct drm_mm_node *)&mm->head_node;
161202b52b7SChris Wilson }
162522e85ddSChris Wilson EXPORT_SYMBOL(__drm_mm_interval_first);
163202b52b7SChris Wilson
drm_mm_interval_tree_add_node(struct drm_mm_node * hole_node,struct drm_mm_node * node)164202b52b7SChris Wilson static void drm_mm_interval_tree_add_node(struct drm_mm_node *hole_node,
165202b52b7SChris Wilson struct drm_mm_node *node)
166202b52b7SChris Wilson {
167202b52b7SChris Wilson struct drm_mm *mm = hole_node->mm;
168202b52b7SChris Wilson struct rb_node **link, *rb;
169202b52b7SChris Wilson struct drm_mm_node *parent;
1708a194945SChris Wilson bool leftmost;
171202b52b7SChris Wilson
172202b52b7SChris Wilson node->__subtree_last = LAST(node);
173202b52b7SChris Wilson
17471724f70SChris Wilson if (drm_mm_node_allocated(hole_node)) {
175202b52b7SChris Wilson rb = &hole_node->rb;
176202b52b7SChris Wilson while (rb) {
177202b52b7SChris Wilson parent = rb_entry(rb, struct drm_mm_node, rb);
178202b52b7SChris Wilson if (parent->__subtree_last >= node->__subtree_last)
179202b52b7SChris Wilson break;
180202b52b7SChris Wilson
181202b52b7SChris Wilson parent->__subtree_last = node->__subtree_last;
182202b52b7SChris Wilson rb = rb_parent(rb);
183202b52b7SChris Wilson }
184202b52b7SChris Wilson
185202b52b7SChris Wilson rb = &hole_node->rb;
186202b52b7SChris Wilson link = &hole_node->rb.rb_right;
187f808c13fSDavidlohr Bueso leftmost = false;
188202b52b7SChris Wilson } else {
189202b52b7SChris Wilson rb = NULL;
190f808c13fSDavidlohr Bueso link = &mm->interval_tree.rb_root.rb_node;
1918a194945SChris Wilson leftmost = true;
192202b52b7SChris Wilson }
193202b52b7SChris Wilson
194202b52b7SChris Wilson while (*link) {
195202b52b7SChris Wilson rb = *link;
196202b52b7SChris Wilson parent = rb_entry(rb, struct drm_mm_node, rb);
197202b52b7SChris Wilson if (parent->__subtree_last < node->__subtree_last)
198202b52b7SChris Wilson parent->__subtree_last = node->__subtree_last;
1998a194945SChris Wilson if (node->start < parent->start) {
200202b52b7SChris Wilson link = &parent->rb.rb_left;
2018a194945SChris Wilson } else {
202202b52b7SChris Wilson link = &parent->rb.rb_right;
2038a194945SChris Wilson leftmost = false;
204f808c13fSDavidlohr Bueso }
205202b52b7SChris Wilson }
206202b52b7SChris Wilson
207202b52b7SChris Wilson rb_link_node(&node->rb, rb, link);
208f808c13fSDavidlohr Bueso rb_insert_augmented_cached(&node->rb, &mm->interval_tree, leftmost,
209202b52b7SChris Wilson &drm_mm_interval_tree_augment);
210202b52b7SChris Wilson }
211202b52b7SChris Wilson
2124e64e553SChris Wilson #define HOLE_SIZE(NODE) ((NODE)->hole_size)
2134e64e553SChris Wilson #define HOLE_ADDR(NODE) (__drm_mm_hole_node_start(NODE))
2144e64e553SChris Wilson
rb_to_hole_size(struct rb_node * rb)2152f7e8769SChris Wilson static u64 rb_to_hole_size(struct rb_node *rb)
2162f7e8769SChris Wilson {
2172f7e8769SChris Wilson return rb_entry(rb, struct drm_mm_node, rb_hole_size)->hole_size;
2182f7e8769SChris Wilson }
2192f7e8769SChris Wilson
insert_hole_size(struct rb_root_cached * root,struct drm_mm_node * node)2202f7e8769SChris Wilson static void insert_hole_size(struct rb_root_cached *root,
2212f7e8769SChris Wilson struct drm_mm_node *node)
2222f7e8769SChris Wilson {
2232f7e8769SChris Wilson struct rb_node **link = &root->rb_root.rb_node, *rb = NULL;
2242f7e8769SChris Wilson u64 x = node->hole_size;
2252f7e8769SChris Wilson bool first = true;
2262f7e8769SChris Wilson
2272f7e8769SChris Wilson while (*link) {
2282f7e8769SChris Wilson rb = *link;
2292f7e8769SChris Wilson if (x > rb_to_hole_size(rb)) {
2302f7e8769SChris Wilson link = &rb->rb_left;
2312f7e8769SChris Wilson } else {
2322f7e8769SChris Wilson link = &rb->rb_right;
2332f7e8769SChris Wilson first = false;
2342f7e8769SChris Wilson }
2352f7e8769SChris Wilson }
2362f7e8769SChris Wilson
2372f7e8769SChris Wilson rb_link_node(&node->rb_hole_size, rb, link);
2382f7e8769SChris Wilson rb_insert_color_cached(&node->rb_hole_size, root, first);
2392f7e8769SChris Wilson }
2402f7e8769SChris Wilson
RB_DECLARE_CALLBACKS_MAX(static,augment_callbacks,struct drm_mm_node,rb_hole_addr,u64,subtree_max_hole,HOLE_SIZE)2410cdea445SNirmoy Das RB_DECLARE_CALLBACKS_MAX(static, augment_callbacks,
2420cdea445SNirmoy Das struct drm_mm_node, rb_hole_addr,
2430cdea445SNirmoy Das u64, subtree_max_hole, HOLE_SIZE)
2440cdea445SNirmoy Das
2450cdea445SNirmoy Das static void insert_hole_addr(struct rb_root *root, struct drm_mm_node *node)
2460cdea445SNirmoy Das {
2470cdea445SNirmoy Das struct rb_node **link = &root->rb_node, *rb_parent = NULL;
2480cdea445SNirmoy Das u64 start = HOLE_ADDR(node), subtree_max_hole = node->subtree_max_hole;
2490cdea445SNirmoy Das struct drm_mm_node *parent;
2500cdea445SNirmoy Das
2510cdea445SNirmoy Das while (*link) {
2520cdea445SNirmoy Das rb_parent = *link;
2530cdea445SNirmoy Das parent = rb_entry(rb_parent, struct drm_mm_node, rb_hole_addr);
2540cdea445SNirmoy Das if (parent->subtree_max_hole < subtree_max_hole)
2550cdea445SNirmoy Das parent->subtree_max_hole = subtree_max_hole;
2560cdea445SNirmoy Das if (start < HOLE_ADDR(parent))
2570cdea445SNirmoy Das link = &parent->rb_hole_addr.rb_left;
2580cdea445SNirmoy Das else
2590cdea445SNirmoy Das link = &parent->rb_hole_addr.rb_right;
2600cdea445SNirmoy Das }
2610cdea445SNirmoy Das
2620cdea445SNirmoy Das rb_link_node(&node->rb_hole_addr, rb_parent, link);
2630cdea445SNirmoy Das rb_insert_augmented(&node->rb_hole_addr, root, &augment_callbacks);
2640cdea445SNirmoy Das }
2650cdea445SNirmoy Das
add_hole(struct drm_mm_node * node)2664e64e553SChris Wilson static void add_hole(struct drm_mm_node *node)
267c0e09200SDave Airlie {
2684e64e553SChris Wilson struct drm_mm *mm = node->mm;
269ea7b1dd4SDaniel Vetter
2704e64e553SChris Wilson node->hole_size =
2714e64e553SChris Wilson __drm_mm_hole_node_end(node) - __drm_mm_hole_node_start(node);
2720cdea445SNirmoy Das node->subtree_max_hole = node->hole_size;
2734e64e553SChris Wilson DRM_MM_BUG_ON(!drm_mm_hole_follows(node));
274b0b7af18SDaniel Vetter
2752f7e8769SChris Wilson insert_hole_size(&mm->holes_size, node);
2760cdea445SNirmoy Das insert_hole_addr(&mm->holes_addr, node);
277c0e09200SDave Airlie
278ea7b1dd4SDaniel Vetter list_add(&node->hole_stack, &mm->hole_stack);
279c0e09200SDave Airlie }
2805705670dSChris Wilson
rm_hole(struct drm_mm_node * node)2814e64e553SChris Wilson static void rm_hole(struct drm_mm_node *node)
2824e64e553SChris Wilson {
2834e64e553SChris Wilson DRM_MM_BUG_ON(!drm_mm_hole_follows(node));
2844e64e553SChris Wilson
2854e64e553SChris Wilson list_del(&node->hole_stack);
2862f7e8769SChris Wilson rb_erase_cached(&node->rb_hole_size, &node->mm->holes_size);
2870cdea445SNirmoy Das rb_erase_augmented(&node->rb_hole_addr, &node->mm->holes_addr,
2880cdea445SNirmoy Das &augment_callbacks);
2894e64e553SChris Wilson node->hole_size = 0;
2900cdea445SNirmoy Das node->subtree_max_hole = 0;
2914e64e553SChris Wilson
2924e64e553SChris Wilson DRM_MM_BUG_ON(drm_mm_hole_follows(node));
2934e64e553SChris Wilson }
2944e64e553SChris Wilson
rb_hole_size_to_node(struct rb_node * rb)2954e64e553SChris Wilson static inline struct drm_mm_node *rb_hole_size_to_node(struct rb_node *rb)
2964e64e553SChris Wilson {
2974e64e553SChris Wilson return rb_entry_safe(rb, struct drm_mm_node, rb_hole_size);
2984e64e553SChris Wilson }
2994e64e553SChris Wilson
rb_hole_addr_to_node(struct rb_node * rb)3004e64e553SChris Wilson static inline struct drm_mm_node *rb_hole_addr_to_node(struct rb_node *rb)
3014e64e553SChris Wilson {
3024e64e553SChris Wilson return rb_entry_safe(rb, struct drm_mm_node, rb_hole_addr);
3034e64e553SChris Wilson }
3044e64e553SChris Wilson
best_hole(struct drm_mm * mm,u64 size)3054e64e553SChris Wilson static struct drm_mm_node *best_hole(struct drm_mm *mm, u64 size)
3064e64e553SChris Wilson {
3072f7e8769SChris Wilson struct rb_node *rb = mm->holes_size.rb_root.rb_node;
3082f7e8769SChris Wilson struct drm_mm_node *best = NULL;
3094e64e553SChris Wilson
3102f7e8769SChris Wilson do {
3112f7e8769SChris Wilson struct drm_mm_node *node =
3122f7e8769SChris Wilson rb_entry(rb, struct drm_mm_node, rb_hole_size);
3134e64e553SChris Wilson
3142f7e8769SChris Wilson if (size <= node->hole_size) {
3152f7e8769SChris Wilson best = node;
3162f7e8769SChris Wilson rb = rb->rb_right;
3174e64e553SChris Wilson } else {
3182f7e8769SChris Wilson rb = rb->rb_left;
3194e64e553SChris Wilson }
3202f7e8769SChris Wilson } while (rb);
3214e64e553SChris Wilson
3222f7e8769SChris Wilson return best;
3234e64e553SChris Wilson }
3244e64e553SChris Wilson
usable_hole_addr(struct rb_node * rb,u64 size)3255fad79fdSChristian König static bool usable_hole_addr(struct rb_node *rb, u64 size)
3265fad79fdSChristian König {
3275fad79fdSChristian König return rb && rb_hole_addr_to_node(rb)->subtree_max_hole >= size;
3285fad79fdSChristian König }
3295fad79fdSChristian König
find_hole_addr(struct drm_mm * mm,u64 addr,u64 size)330271e7decSChristian König static struct drm_mm_node *find_hole_addr(struct drm_mm *mm, u64 addr, u64 size)
3314e64e553SChris Wilson {
3322f7e8769SChris Wilson struct rb_node *rb = mm->holes_addr.rb_node;
3334e64e553SChris Wilson struct drm_mm_node *node = NULL;
3344e64e553SChris Wilson
3352f7e8769SChris Wilson while (rb) {
3364e64e553SChris Wilson u64 hole_start;
3374e64e553SChris Wilson
3385fad79fdSChristian König if (!usable_hole_addr(rb, size))
339271e7decSChristian König break;
340271e7decSChristian König
3412f7e8769SChris Wilson node = rb_hole_addr_to_node(rb);
3424e64e553SChris Wilson hole_start = __drm_mm_hole_node_start(node);
3434e64e553SChris Wilson
3444e64e553SChris Wilson if (addr < hole_start)
3452f7e8769SChris Wilson rb = node->rb_hole_addr.rb_left;
3464e64e553SChris Wilson else if (addr > hole_start + node->hole_size)
3472f7e8769SChris Wilson rb = node->rb_hole_addr.rb_right;
3484e64e553SChris Wilson else
3494e64e553SChris Wilson break;
3504e64e553SChris Wilson }
3514e64e553SChris Wilson
3524e64e553SChris Wilson return node;
3534e64e553SChris Wilson }
3544e64e553SChris Wilson
3554e64e553SChris Wilson static struct drm_mm_node *
first_hole(struct drm_mm * mm,u64 start,u64 end,u64 size,enum drm_mm_insert_mode mode)3564e64e553SChris Wilson first_hole(struct drm_mm *mm,
3574e64e553SChris Wilson u64 start, u64 end, u64 size,
3584e64e553SChris Wilson enum drm_mm_insert_mode mode)
3594e64e553SChris Wilson {
3604e64e553SChris Wilson switch (mode) {
3614e64e553SChris Wilson default:
3624e64e553SChris Wilson case DRM_MM_INSERT_BEST:
3634e64e553SChris Wilson return best_hole(mm, size);
3644e64e553SChris Wilson
3654e64e553SChris Wilson case DRM_MM_INSERT_LOW:
366271e7decSChristian König return find_hole_addr(mm, start, size);
3674e64e553SChris Wilson
3684e64e553SChris Wilson case DRM_MM_INSERT_HIGH:
369271e7decSChristian König return find_hole_addr(mm, end, size);
3704e64e553SChris Wilson
3714e64e553SChris Wilson case DRM_MM_INSERT_EVICT:
3724e64e553SChris Wilson return list_first_entry_or_null(&mm->hole_stack,
3734e64e553SChris Wilson struct drm_mm_node,
3744e64e553SChris Wilson hole_stack);
3754e64e553SChris Wilson }
3764e64e553SChris Wilson }
3774e64e553SChris Wilson
3780cdea445SNirmoy Das /**
3795fad79fdSChristian König * DECLARE_NEXT_HOLE_ADDR - macro to declare next hole functions
3805fad79fdSChristian König * @name: name of function to declare
3815fad79fdSChristian König * @first: first rb member to traverse (either rb_left or rb_right).
3825fad79fdSChristian König * @last: last rb member to traverse (either rb_right or rb_left).
3830cdea445SNirmoy Das *
3845fad79fdSChristian König * This macro declares a function to return the next hole of the addr rb tree.
3855fad79fdSChristian König * While traversing the tree we take the searched size into account and only
3865fad79fdSChristian König * visit branches with potential big enough holes.
3870cdea445SNirmoy Das */
3880cdea445SNirmoy Das
3895fad79fdSChristian König #define DECLARE_NEXT_HOLE_ADDR(name, first, last) \
3905fad79fdSChristian König static struct drm_mm_node *name(struct drm_mm_node *entry, u64 size) \
3915fad79fdSChristian König { \
3925fad79fdSChristian König struct rb_node *parent, *node = &entry->rb_hole_addr; \
3935fad79fdSChristian König \
3945fad79fdSChristian König if (!entry || RB_EMPTY_NODE(node)) \
3955fad79fdSChristian König return NULL; \
3965fad79fdSChristian König \
3975fad79fdSChristian König if (usable_hole_addr(node->first, size)) { \
3985fad79fdSChristian König node = node->first; \
3995fad79fdSChristian König while (usable_hole_addr(node->last, size)) \
4005fad79fdSChristian König node = node->last; \
4015fad79fdSChristian König return rb_hole_addr_to_node(node); \
4025fad79fdSChristian König } \
4035fad79fdSChristian König \
4045fad79fdSChristian König while ((parent = rb_parent(node)) && node == parent->first) \
4055fad79fdSChristian König node = parent; \
4065fad79fdSChristian König \
4075fad79fdSChristian König return rb_hole_addr_to_node(parent); \
4080cdea445SNirmoy Das }
4090cdea445SNirmoy Das
DECLARE_NEXT_HOLE_ADDR(next_hole_high_addr,rb_left,rb_right)4105fad79fdSChristian König DECLARE_NEXT_HOLE_ADDR(next_hole_high_addr, rb_left, rb_right)
4115fad79fdSChristian König DECLARE_NEXT_HOLE_ADDR(next_hole_low_addr, rb_right, rb_left)
4120cdea445SNirmoy Das
4134e64e553SChris Wilson static struct drm_mm_node *
4144e64e553SChris Wilson next_hole(struct drm_mm *mm,
4154e64e553SChris Wilson struct drm_mm_node *node,
4160cdea445SNirmoy Das u64 size,
4174e64e553SChris Wilson enum drm_mm_insert_mode mode)
4184e64e553SChris Wilson {
4194e64e553SChris Wilson switch (mode) {
4204e64e553SChris Wilson default:
4214e64e553SChris Wilson case DRM_MM_INSERT_BEST:
4222f7e8769SChris Wilson return rb_hole_size_to_node(rb_prev(&node->rb_hole_size));
4234e64e553SChris Wilson
4244e64e553SChris Wilson case DRM_MM_INSERT_LOW:
4250cdea445SNirmoy Das return next_hole_low_addr(node, size);
4264e64e553SChris Wilson
4274e64e553SChris Wilson case DRM_MM_INSERT_HIGH:
4280cdea445SNirmoy Das return next_hole_high_addr(node, size);
4294e64e553SChris Wilson
4304e64e553SChris Wilson case DRM_MM_INSERT_EVICT:
4314e64e553SChris Wilson node = list_next_entry(node, hole_stack);
4324e64e553SChris Wilson return &node->hole_stack == &mm->hole_stack ? NULL : node;
4334e64e553SChris Wilson }
4349fc935deSDaniel Vetter }
4359fc935deSDaniel Vetter
436e18c0412SDaniel Vetter /**
437e18c0412SDaniel Vetter * drm_mm_reserve_node - insert an pre-initialized node
438e18c0412SDaniel Vetter * @mm: drm_mm allocator to insert @node into
439e18c0412SDaniel Vetter * @node: drm_mm_node to insert
440e18c0412SDaniel Vetter *
44105fc0321SDaniel Vetter * This functions inserts an already set-up &drm_mm_node into the allocator,
44205fc0321SDaniel Vetter * meaning that start, size and color must be set by the caller. All other
44305fc0321SDaniel Vetter * fields must be cleared to 0. This is useful to initialize the allocator with
44405fc0321SDaniel Vetter * preallocated objects which must be set-up before the range allocator can be
44505fc0321SDaniel Vetter * set-up, e.g. when taking over a firmware framebuffer.
446e18c0412SDaniel Vetter *
447e18c0412SDaniel Vetter * Returns:
448e18c0412SDaniel Vetter * 0 on success, -ENOSPC if there's no hole where @node is.
449e18c0412SDaniel Vetter */
drm_mm_reserve_node(struct drm_mm * mm,struct drm_mm_node * node)450338710e7SBen Widawsky int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
4515973c7eeSChris Wilson {
452b3a070ccSBen Widawsky struct drm_mm_node *hole;
453202b52b7SChris Wilson u64 hole_start, hole_end;
4542db86dfcSChris Wilson u64 adj_start, adj_end;
4550d1650faSAkeem G Abodunrin u64 end;
456338710e7SBen Widawsky
457b80d3942SHeinrich Schuchardt end = node->start + node->size;
458c820186dSChris Wilson if (unlikely(end <= node->start))
459c820186dSChris Wilson return -ENOSPC;
460b80d3942SHeinrich Schuchardt
461338710e7SBen Widawsky /* Find the relevant hole to add our node to */
462271e7decSChristian König hole = find_hole_addr(mm, node->start, 0);
4634e64e553SChris Wilson if (!hole)
464202b52b7SChris Wilson return -ENOSPC;
465202b52b7SChris Wilson
4662db86dfcSChris Wilson adj_start = hole_start = __drm_mm_hole_node_start(hole);
4674e64e553SChris Wilson adj_end = hole_end = hole_start + hole->hole_size;
4682db86dfcSChris Wilson
4692db86dfcSChris Wilson if (mm->color_adjust)
4702db86dfcSChris Wilson mm->color_adjust(hole, node->color, &adj_start, &adj_end);
4712db86dfcSChris Wilson
4722db86dfcSChris Wilson if (adj_start > node->start || adj_end < end)
473202b52b7SChris Wilson return -ENOSPC;
4745973c7eeSChris Wilson
4755973c7eeSChris Wilson node->mm = mm;
4765973c7eeSChris Wilson
4773dda22d3SChris Wilson __set_bit(DRM_MM_NODE_ALLOCATED_BIT, &node->flags);
4785973c7eeSChris Wilson list_add(&node->node_list, &hole->node_list);
479202b52b7SChris Wilson drm_mm_interval_tree_add_node(hole, node);
4804e64e553SChris Wilson node->hole_size = 0;
481202b52b7SChris Wilson
4824e64e553SChris Wilson rm_hole(hole);
4834e64e553SChris Wilson if (node->start > hole_start)
4844e64e553SChris Wilson add_hole(hole);
4854e64e553SChris Wilson if (end < hole_end)
4864e64e553SChris Wilson add_hole(node);
4875973c7eeSChris Wilson
4885705670dSChris Wilson save_stack(node);
489b3a070ccSBen Widawsky return 0;
4905973c7eeSChris Wilson }
491338710e7SBen Widawsky EXPORT_SYMBOL(drm_mm_reserve_node);
4925973c7eeSChris Wilson
rb_to_hole_size_or_zero(struct rb_node * rb)4932f7e8769SChris Wilson static u64 rb_to_hole_size_or_zero(struct rb_node *rb)
4942f7e8769SChris Wilson {
4952f7e8769SChris Wilson return rb ? rb_to_hole_size(rb) : 0;
4962f7e8769SChris Wilson }
4972f7e8769SChris Wilson
498b0b7af18SDaniel Vetter /**
4994e64e553SChris Wilson * drm_mm_insert_node_in_range - ranged search for space and insert @node
500e18c0412SDaniel Vetter * @mm: drm_mm to allocate from
501e18c0412SDaniel Vetter * @node: preallocate node to insert
502e18c0412SDaniel Vetter * @size: size of the allocation
503e18c0412SDaniel Vetter * @alignment: alignment of the allocation
504e18c0412SDaniel Vetter * @color: opaque tag value to use for this node
5054e64e553SChris Wilson * @range_start: start of the allowed range for this node
5064e64e553SChris Wilson * @range_end: end of the allowed range for this node
5074e64e553SChris Wilson * @mode: fine-tune the allocation search and placement
508e18c0412SDaniel Vetter *
50905fc0321SDaniel Vetter * The preallocated @node must be cleared to 0.
510e18c0412SDaniel Vetter *
511e18c0412SDaniel Vetter * Returns:
512e18c0412SDaniel Vetter * 0 on success, -ENOSPC if there's no suitable hole.
513c0e09200SDave Airlie */
drm_mm_insert_node_in_range(struct drm_mm * const mm,struct drm_mm_node * const node,u64 size,u64 alignment,unsigned long color,u64 range_start,u64 range_end,enum drm_mm_insert_mode mode)5144e64e553SChris Wilson int drm_mm_insert_node_in_range(struct drm_mm * const mm,
5154e64e553SChris Wilson struct drm_mm_node * const node,
51671733207SChris Wilson u64 size, u64 alignment,
51762347f9eSLauri Kasanen unsigned long color,
5184e64e553SChris Wilson u64 range_start, u64 range_end,
5194e64e553SChris Wilson enum drm_mm_insert_mode mode)
520c0e09200SDave Airlie {
5214e64e553SChris Wilson struct drm_mm_node *hole;
5224e64e553SChris Wilson u64 remainder_mask;
52383bc4ec3SChris Wilson bool once;
524c0e09200SDave Airlie
525c1a495a5SChris Wilson DRM_MM_BUG_ON(range_start > range_end);
526aafdcfd3SChris Wilson
5274e64e553SChris Wilson if (unlikely(size == 0 || range_end - range_start < size))
528b0b7af18SDaniel Vetter return -ENOSPC;
529b0b7af18SDaniel Vetter
5302f7e8769SChris Wilson if (rb_to_hole_size_or_zero(rb_first_cached(&mm->holes_size)) < size)
5312f7e8769SChris Wilson return -ENOSPC;
5322f7e8769SChris Wilson
5334e64e553SChris Wilson if (alignment <= 1)
5344e64e553SChris Wilson alignment = 0;
5354e64e553SChris Wilson
53683bc4ec3SChris Wilson once = mode & DRM_MM_INSERT_ONCE;
53783bc4ec3SChris Wilson mode &= ~DRM_MM_INSERT_ONCE;
53883bc4ec3SChris Wilson
5394e64e553SChris Wilson remainder_mask = is_power_of_2(alignment) ? alignment - 1 : 0;
54083bc4ec3SChris Wilson for (hole = first_hole(mm, range_start, range_end, size, mode);
54183bc4ec3SChris Wilson hole;
5420cdea445SNirmoy Das hole = once ? NULL : next_hole(mm, hole, size, mode)) {
5434e64e553SChris Wilson u64 hole_start = __drm_mm_hole_node_start(hole);
5444e64e553SChris Wilson u64 hole_end = hole_start + hole->hole_size;
5454e64e553SChris Wilson u64 adj_start, adj_end;
5464e64e553SChris Wilson u64 col_start, col_end;
5474e64e553SChris Wilson
5484e64e553SChris Wilson if (mode == DRM_MM_INSERT_LOW && hole_start >= range_end)
5494e64e553SChris Wilson break;
5504e64e553SChris Wilson
5514e64e553SChris Wilson if (mode == DRM_MM_INSERT_HIGH && hole_end <= range_start)
5524e64e553SChris Wilson break;
5534e64e553SChris Wilson
5544e64e553SChris Wilson col_start = hole_start;
5554e64e553SChris Wilson col_end = hole_end;
5564e64e553SChris Wilson if (mm->color_adjust)
5574e64e553SChris Wilson mm->color_adjust(hole, color, &col_start, &col_end);
5584e64e553SChris Wilson
5594e64e553SChris Wilson adj_start = max(col_start, range_start);
5604e64e553SChris Wilson adj_end = min(col_end, range_end);
5614e64e553SChris Wilson
5624e64e553SChris Wilson if (adj_end <= adj_start || adj_end - adj_start < size)
5634e64e553SChris Wilson continue;
5644e64e553SChris Wilson
5654e64e553SChris Wilson if (mode == DRM_MM_INSERT_HIGH)
5664e64e553SChris Wilson adj_start = adj_end - size;
5674e64e553SChris Wilson
5684e64e553SChris Wilson if (alignment) {
5694e64e553SChris Wilson u64 rem;
5704e64e553SChris Wilson
5714e64e553SChris Wilson if (likely(remainder_mask))
5724e64e553SChris Wilson rem = adj_start & remainder_mask;
5734e64e553SChris Wilson else
5744e64e553SChris Wilson div64_u64_rem(adj_start, alignment, &rem);
5754e64e553SChris Wilson if (rem) {
5764e64e553SChris Wilson adj_start -= rem;
5774e64e553SChris Wilson if (mode != DRM_MM_INSERT_HIGH)
5784e64e553SChris Wilson adj_start += alignment;
5794e64e553SChris Wilson
5804e64e553SChris Wilson if (adj_start < max(col_start, range_start) ||
5814e64e553SChris Wilson min(col_end, range_end) - adj_start < size)
5824e64e553SChris Wilson continue;
5834e64e553SChris Wilson
5844e64e553SChris Wilson if (adj_end <= adj_start ||
5854e64e553SChris Wilson adj_end - adj_start < size)
5864e64e553SChris Wilson continue;
5874e64e553SChris Wilson }
5884e64e553SChris Wilson }
5894e64e553SChris Wilson
5904e64e553SChris Wilson node->mm = mm;
5914e64e553SChris Wilson node->size = size;
5924e64e553SChris Wilson node->start = adj_start;
5934e64e553SChris Wilson node->color = color;
5944e64e553SChris Wilson node->hole_size = 0;
5954e64e553SChris Wilson
5963dda22d3SChris Wilson __set_bit(DRM_MM_NODE_ALLOCATED_BIT, &node->flags);
5974e64e553SChris Wilson list_add(&node->node_list, &hole->node_list);
5984e64e553SChris Wilson drm_mm_interval_tree_add_node(hole, node);
5994e64e553SChris Wilson
6004e64e553SChris Wilson rm_hole(hole);
6014e64e553SChris Wilson if (adj_start > hole_start)
6024e64e553SChris Wilson add_hole(hole);
6034e64e553SChris Wilson if (adj_start + size < hole_end)
6044e64e553SChris Wilson add_hole(node);
6054e64e553SChris Wilson
6064e64e553SChris Wilson save_stack(node);
607b0b7af18SDaniel Vetter return 0;
608b0b7af18SDaniel Vetter }
6094e64e553SChris Wilson
6102713778cSChristian König return -ENOSPC;
6114e64e553SChris Wilson }
6124e64e553SChris Wilson EXPORT_SYMBOL(drm_mm_insert_node_in_range);
613b8103450SChris Wilson
drm_mm_node_scanned_block(const struct drm_mm_node * node)61471724f70SChris Wilson static inline bool drm_mm_node_scanned_block(const struct drm_mm_node *node)
61571724f70SChris Wilson {
6164ee92c71SChris Wilson return test_bit(DRM_MM_NODE_SCANNED_BIT, &node->flags);
61771724f70SChris Wilson }
61871724f70SChris Wilson
619b0b7af18SDaniel Vetter /**
620e18c0412SDaniel Vetter * drm_mm_remove_node - Remove a memory node from the allocator.
621e18c0412SDaniel Vetter * @node: drm_mm_node to remove
622e18c0412SDaniel Vetter *
623e18c0412SDaniel Vetter * This just removes a node from its drm_mm allocator. The node does not need to
624e18c0412SDaniel Vetter * be cleared again before it can be re-inserted into this or any other drm_mm
625ba004e39SChris Wilson * allocator. It is a bug to call this function on a unallocated node.
626b0b7af18SDaniel Vetter */
drm_mm_remove_node(struct drm_mm_node * node)627b0b7af18SDaniel Vetter void drm_mm_remove_node(struct drm_mm_node *node)
628b0b7af18SDaniel Vetter {
629ea7b1dd4SDaniel Vetter struct drm_mm *mm = node->mm;
630ea7b1dd4SDaniel Vetter struct drm_mm_node *prev_node;
631c0e09200SDave Airlie
63271724f70SChris Wilson DRM_MM_BUG_ON(!drm_mm_node_allocated(node));
63371724f70SChris Wilson DRM_MM_BUG_ON(drm_mm_node_scanned_block(node));
634c0e09200SDave Airlie
6354e64e553SChris Wilson prev_node = list_prev_entry(node, node_list);
636249d6048SJerome Glisse
6374e64e553SChris Wilson if (drm_mm_hole_follows(node))
6384e64e553SChris Wilson rm_hole(node);
639ea7b1dd4SDaniel Vetter
640202b52b7SChris Wilson drm_mm_interval_tree_remove(node, &mm->interval_tree);
641ea7b1dd4SDaniel Vetter list_del(&node->node_list);
6424e64e553SChris Wilson
6434e64e553SChris Wilson if (drm_mm_hole_follows(prev_node))
6444e64e553SChris Wilson rm_hole(prev_node);
6454e64e553SChris Wilson add_hole(prev_node);
6463dda22d3SChris Wilson
6473dda22d3SChris Wilson clear_bit_unlock(DRM_MM_NODE_ALLOCATED_BIT, &node->flags);
648b0b7af18SDaniel Vetter }
649b0b7af18SDaniel Vetter EXPORT_SYMBOL(drm_mm_remove_node);
650b0b7af18SDaniel Vetter
651709ea971SDaniel Vetter /**
652e18c0412SDaniel Vetter * drm_mm_replace_node - move an allocation from @old to @new
653e18c0412SDaniel Vetter * @old: drm_mm_node to remove from the allocator
654e18c0412SDaniel Vetter * @new: drm_mm_node which should inherit @old's allocation
655e18c0412SDaniel Vetter *
656e18c0412SDaniel Vetter * This is useful for when drivers embed the drm_mm_node structure and hence
657e18c0412SDaniel Vetter * can't move allocations by reassigning pointers. It's a combination of remove
658e18c0412SDaniel Vetter * and insert with the guarantee that the allocation start will match.
659b0b7af18SDaniel Vetter */
drm_mm_replace_node(struct drm_mm_node * old,struct drm_mm_node * new)660b0b7af18SDaniel Vetter void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
661b0b7af18SDaniel Vetter {
662338f1d9dSChris Wilson struct drm_mm *mm = old->mm;
663338f1d9dSChris Wilson
66471724f70SChris Wilson DRM_MM_BUG_ON(!drm_mm_node_allocated(old));
665b3ee963fSChris Wilson
6664e64e553SChris Wilson *new = *old;
667b0b7af18SDaniel Vetter
6683dda22d3SChris Wilson __set_bit(DRM_MM_NODE_ALLOCATED_BIT, &new->flags);
6694e64e553SChris Wilson list_replace(&old->node_list, &new->node_list);
670338f1d9dSChris Wilson rb_replace_node_cached(&old->rb, &new->rb, &mm->interval_tree);
6714e64e553SChris Wilson
6724e64e553SChris Wilson if (drm_mm_hole_follows(old)) {
6734e64e553SChris Wilson list_replace(&old->hole_stack, &new->hole_stack);
6742f7e8769SChris Wilson rb_replace_node_cached(&old->rb_hole_size,
6754e64e553SChris Wilson &new->rb_hole_size,
676338f1d9dSChris Wilson &mm->holes_size);
6774e64e553SChris Wilson rb_replace_node(&old->rb_hole_addr,
6784e64e553SChris Wilson &new->rb_hole_addr,
679338f1d9dSChris Wilson &mm->holes_addr);
6804e64e553SChris Wilson }
6814e64e553SChris Wilson
6823dda22d3SChris Wilson clear_bit_unlock(DRM_MM_NODE_ALLOCATED_BIT, &old->flags);
683b0b7af18SDaniel Vetter }
684b0b7af18SDaniel Vetter EXPORT_SYMBOL(drm_mm_replace_node);
685b0b7af18SDaniel Vetter
686b0b7af18SDaniel Vetter /**
68705fc0321SDaniel Vetter * DOC: lru scan roster
68893110be6SDaniel Vetter *
68993110be6SDaniel Vetter * Very often GPUs need to have continuous allocations for a given object. When
69093110be6SDaniel Vetter * evicting objects to make space for a new one it is therefore not most
69193110be6SDaniel Vetter * efficient when we simply start to select all objects from the tail of an LRU
69293110be6SDaniel Vetter * until there's a suitable hole: Especially for big objects or nodes that
69393110be6SDaniel Vetter * otherwise have special allocation constraints there's a good chance we evict
694ba004e39SChris Wilson * lots of (smaller) objects unnecessarily.
69593110be6SDaniel Vetter *
69693110be6SDaniel Vetter * The DRM range allocator supports this use-case through the scanning
69793110be6SDaniel Vetter * interfaces. First a scan operation needs to be initialized with
6989a71e277SChris Wilson * drm_mm_scan_init() or drm_mm_scan_init_with_range(). The driver adds
69905fc0321SDaniel Vetter * objects to the roster, probably by walking an LRU list, but this can be
7000ae865efSCai Huoqing * freely implemented. Eviction candidates are added using
70105fc0321SDaniel Vetter * drm_mm_scan_add_block() until a suitable hole is found or there are no
702940eba2dSDaniel Vetter * further evictable objects. Eviction roster metadata is tracked in &struct
703940eba2dSDaniel Vetter * drm_mm_scan.
70493110be6SDaniel Vetter *
705ba004e39SChris Wilson * The driver must walk through all objects again in exactly the reverse
70693110be6SDaniel Vetter * order to restore the allocator state. Note that while the allocator is used
70793110be6SDaniel Vetter * in the scan mode no other operation is allowed.
70893110be6SDaniel Vetter *
7093fa489daSChris Wilson * Finally the driver evicts all objects selected (drm_mm_scan_remove_block()
7103fa489daSChris Wilson * reported true) in the scan, and any overlapping nodes after color adjustment
71105fc0321SDaniel Vetter * (drm_mm_scan_color_evict()). Adding and removing an object is O(1), and
7123fa489daSChris Wilson * since freeing a node is also O(1) the overall complexity is
7133fa489daSChris Wilson * O(scanned_objects). So like the free stack which needs to be walked before a
7143fa489daSChris Wilson * scan operation even begins this is linear in the number of objects. It
7153fa489daSChris Wilson * doesn't seem to hurt too badly.
71693110be6SDaniel Vetter */
71793110be6SDaniel Vetter
71893110be6SDaniel Vetter /**
7199a71e277SChris Wilson * drm_mm_scan_init_with_range - initialize range-restricted lru scanning
7209a71e277SChris Wilson * @scan: scan state
721e18c0412SDaniel Vetter * @mm: drm_mm to scan
722e18c0412SDaniel Vetter * @size: size of the allocation
723e18c0412SDaniel Vetter * @alignment: alignment of the allocation
724e18c0412SDaniel Vetter * @color: opaque tag value to use for the allocation
725e18c0412SDaniel Vetter * @start: start of the allowed range for the allocation
726e18c0412SDaniel Vetter * @end: end of the allowed range for the allocation
7274e64e553SChris Wilson * @mode: fine-tune the allocation search and placement
728d935cc61SDaniel Vetter *
729d935cc61SDaniel Vetter * This simply sets up the scanning routines with the parameters for the desired
7300b04d474SChris Wilson * hole.
731d935cc61SDaniel Vetter *
732e18c0412SDaniel Vetter * Warning:
733e18c0412SDaniel Vetter * As long as the scan list is non-empty, no other operations than
734d935cc61SDaniel Vetter * adding/removing nodes to/from the scan list are allowed.
735d935cc61SDaniel Vetter */
drm_mm_scan_init_with_range(struct drm_mm_scan * scan,struct drm_mm * mm,u64 size,u64 alignment,unsigned long color,u64 start,u64 end,enum drm_mm_insert_mode mode)7369a71e277SChris Wilson void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
7379a71e277SChris Wilson struct drm_mm *mm,
738440fd528SThierry Reding u64 size,
73971733207SChris Wilson u64 alignment,
7406b9d89b4SChris Wilson unsigned long color,
741440fd528SThierry Reding u64 start,
7420b04d474SChris Wilson u64 end,
7434e64e553SChris Wilson enum drm_mm_insert_mode mode)
744d935cc61SDaniel Vetter {
7456259a56bSChris Wilson DRM_MM_BUG_ON(start >= end);
7466259a56bSChris Wilson DRM_MM_BUG_ON(!size || size > end - start);
7479a71e277SChris Wilson DRM_MM_BUG_ON(mm->scan_active);
7486259a56bSChris Wilson
7499a71e277SChris Wilson scan->mm = mm;
7509a71e277SChris Wilson
7519a956b15SChris Wilson if (alignment <= 1)
7529a956b15SChris Wilson alignment = 0;
7539a956b15SChris Wilson
7549a71e277SChris Wilson scan->color = color;
7559a71e277SChris Wilson scan->alignment = alignment;
7569a956b15SChris Wilson scan->remainder_mask = is_power_of_2(alignment) ? alignment - 1 : 0;
7579a71e277SChris Wilson scan->size = size;
7584e64e553SChris Wilson scan->mode = mode;
7599a71e277SChris Wilson
7609a71e277SChris Wilson DRM_MM_BUG_ON(end <= start);
7619a71e277SChris Wilson scan->range_start = start;
7629a71e277SChris Wilson scan->range_end = end;
7639a71e277SChris Wilson
7649a71e277SChris Wilson scan->hit_start = U64_MAX;
7659a71e277SChris Wilson scan->hit_end = 0;
766d935cc61SDaniel Vetter }
7679a71e277SChris Wilson EXPORT_SYMBOL(drm_mm_scan_init_with_range);
768d935cc61SDaniel Vetter
769d935cc61SDaniel Vetter /**
770e18c0412SDaniel Vetter * drm_mm_scan_add_block - add a node to the scan list
7719b8b75deSChris Wilson * @scan: the active drm_mm scanner
772e18c0412SDaniel Vetter * @node: drm_mm_node to add
773e18c0412SDaniel Vetter *
774709ea971SDaniel Vetter * Add a node to the scan list that might be freed to make space for the desired
775709ea971SDaniel Vetter * hole.
776709ea971SDaniel Vetter *
777e18c0412SDaniel Vetter * Returns:
778e18c0412SDaniel Vetter * True if a hole has been found, false otherwise.
779709ea971SDaniel Vetter */
drm_mm_scan_add_block(struct drm_mm_scan * scan,struct drm_mm_node * node)7809a71e277SChris Wilson bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
7819a71e277SChris Wilson struct drm_mm_node *node)
782709ea971SDaniel Vetter {
7839a71e277SChris Wilson struct drm_mm *mm = scan->mm;
7844a6c156fSChris Wilson struct drm_mm_node *hole;
785440fd528SThierry Reding u64 hole_start, hole_end;
786268c6498SChris Wilson u64 col_start, col_end;
787440fd528SThierry Reding u64 adj_start, adj_end;
788709ea971SDaniel Vetter
7899a71e277SChris Wilson DRM_MM_BUG_ON(node->mm != mm);
79071724f70SChris Wilson DRM_MM_BUG_ON(!drm_mm_node_allocated(node));
79171724f70SChris Wilson DRM_MM_BUG_ON(drm_mm_node_scanned_block(node));
7924ee92c71SChris Wilson __set_bit(DRM_MM_NODE_SCANNED_BIT, &node->flags);
7939a71e277SChris Wilson mm->scan_active++;
794709ea971SDaniel Vetter
795f29051f1SChris Wilson /* Remove this block from the node_list so that we enlarge the hole
796f29051f1SChris Wilson * (distance between the end of our previous node and the start of
797f29051f1SChris Wilson * or next), without poisoning the link so that we can restore it
798f29051f1SChris Wilson * later in drm_mm_scan_remove_block().
799f29051f1SChris Wilson */
8004a6c156fSChris Wilson hole = list_prev_entry(node, node_list);
801f29051f1SChris Wilson DRM_MM_BUG_ON(list_next_entry(hole, node_list) != node);
802f29051f1SChris Wilson __list_del_entry(&node->node_list);
803709ea971SDaniel Vetter
804268c6498SChris Wilson hole_start = __drm_mm_hole_node_start(hole);
805268c6498SChris Wilson hole_end = __drm_mm_hole_node_end(hole);
8066b9d89b4SChris Wilson
807268c6498SChris Wilson col_start = hole_start;
808268c6498SChris Wilson col_end = hole_end;
809901593f2SChris Wilson if (mm->color_adjust)
810268c6498SChris Wilson mm->color_adjust(hole, scan->color, &col_start, &col_end);
811268c6498SChris Wilson
812268c6498SChris Wilson adj_start = max(col_start, scan->range_start);
813268c6498SChris Wilson adj_end = min(col_end, scan->range_end);
8140b04d474SChris Wilson if (adj_end <= adj_start || adj_end - adj_start < scan->size)
8150b04d474SChris Wilson return false;
816901593f2SChris Wilson
8174e64e553SChris Wilson if (scan->mode == DRM_MM_INSERT_HIGH)
8180b04d474SChris Wilson adj_start = adj_end - scan->size;
8190b04d474SChris Wilson
8200b04d474SChris Wilson if (scan->alignment) {
8210b04d474SChris Wilson u64 rem;
8220b04d474SChris Wilson
8239a956b15SChris Wilson if (likely(scan->remainder_mask))
8249a956b15SChris Wilson rem = adj_start & scan->remainder_mask;
8259a956b15SChris Wilson else
8260b04d474SChris Wilson div64_u64_rem(adj_start, scan->alignment, &rem);
8270b04d474SChris Wilson if (rem) {
8280b04d474SChris Wilson adj_start -= rem;
8294e64e553SChris Wilson if (scan->mode != DRM_MM_INSERT_HIGH)
8300b04d474SChris Wilson adj_start += scan->alignment;
8310b04d474SChris Wilson if (adj_start < max(col_start, scan->range_start) ||
8320b04d474SChris Wilson min(col_end, scan->range_end) - adj_start < scan->size)
8330b04d474SChris Wilson return false;
8340b04d474SChris Wilson
8350b04d474SChris Wilson if (adj_end <= adj_start ||
8360b04d474SChris Wilson adj_end - adj_start < scan->size)
8370b04d474SChris Wilson return false;
8380b04d474SChris Wilson }
839709ea971SDaniel Vetter }
840709ea971SDaniel Vetter
8410b04d474SChris Wilson scan->hit_start = adj_start;
8420b04d474SChris Wilson scan->hit_end = adj_start + scan->size;
8430b04d474SChris Wilson
8440b04d474SChris Wilson DRM_MM_BUG_ON(scan->hit_start >= scan->hit_end);
8450b04d474SChris Wilson DRM_MM_BUG_ON(scan->hit_start < hole_start);
8460b04d474SChris Wilson DRM_MM_BUG_ON(scan->hit_end > hole_end);
8470b04d474SChris Wilson
8480b04d474SChris Wilson return true;
849709ea971SDaniel Vetter }
850709ea971SDaniel Vetter EXPORT_SYMBOL(drm_mm_scan_add_block);
851709ea971SDaniel Vetter
852709ea971SDaniel Vetter /**
853e18c0412SDaniel Vetter * drm_mm_scan_remove_block - remove a node from the scan list
8549b8b75deSChris Wilson * @scan: the active drm_mm scanner
855e18c0412SDaniel Vetter * @node: drm_mm_node to remove
856709ea971SDaniel Vetter *
85705fc0321SDaniel Vetter * Nodes **must** be removed in exactly the reverse order from the scan list as
85805fc0321SDaniel Vetter * they have been added (e.g. using list_add() as they are added and then
85905fc0321SDaniel Vetter * list_for_each() over that eviction list to remove), otherwise the internal
860ba004e39SChris Wilson * state of the memory manager will be corrupted.
861709ea971SDaniel Vetter *
862709ea971SDaniel Vetter * When the scan list is empty, the selected memory nodes can be freed. An
86305fc0321SDaniel Vetter * immediately following drm_mm_insert_node_in_range_generic() or one of the
86405fc0321SDaniel Vetter * simpler versions of that function with !DRM_MM_SEARCH_BEST will then return
8651e55a53aSMatt Roper * the just freed block (because it's at the top of the free_stack list).
866709ea971SDaniel Vetter *
867e18c0412SDaniel Vetter * Returns:
868e18c0412SDaniel Vetter * True if this block should be evicted, false otherwise. Will always
869e18c0412SDaniel Vetter * return false when no hole has been found.
870709ea971SDaniel Vetter */
drm_mm_scan_remove_block(struct drm_mm_scan * scan,struct drm_mm_node * node)8719a71e277SChris Wilson bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
8729a71e277SChris Wilson struct drm_mm_node *node)
873709ea971SDaniel Vetter {
874ea7b1dd4SDaniel Vetter struct drm_mm_node *prev_node;
875709ea971SDaniel Vetter
8769a71e277SChris Wilson DRM_MM_BUG_ON(node->mm != scan->mm);
87771724f70SChris Wilson DRM_MM_BUG_ON(!drm_mm_node_scanned_block(node));
8784ee92c71SChris Wilson __clear_bit(DRM_MM_NODE_SCANNED_BIT, &node->flags);
879709ea971SDaniel Vetter
8809a71e277SChris Wilson DRM_MM_BUG_ON(!node->mm->scan_active);
8819a71e277SChris Wilson node->mm->scan_active--;
8829a71e277SChris Wilson
883f29051f1SChris Wilson /* During drm_mm_scan_add_block() we decoupled this node leaving
884f29051f1SChris Wilson * its pointers intact. Now that the caller is walking back along
885f29051f1SChris Wilson * the eviction list we can restore this block into its rightful
886f29051f1SChris Wilson * place on the full node_list. To confirm that the caller is walking
887f29051f1SChris Wilson * backwards correctly we check that prev_node->next == node->next,
888f29051f1SChris Wilson * i.e. both believe the same node should be on the other side of the
889f29051f1SChris Wilson * hole.
890f29051f1SChris Wilson */
8919a71e277SChris Wilson prev_node = list_prev_entry(node, node_list);
892f29051f1SChris Wilson DRM_MM_BUG_ON(list_next_entry(prev_node, node_list) !=
893f29051f1SChris Wilson list_next_entry(node, node_list));
894ea7b1dd4SDaniel Vetter list_add(&node->node_list, &prev_node->node_list);
895709ea971SDaniel Vetter
8960b04d474SChris Wilson return (node->start + node->size > scan->hit_start &&
8979a71e277SChris Wilson node->start < scan->hit_end);
898709ea971SDaniel Vetter }
899709ea971SDaniel Vetter EXPORT_SYMBOL(drm_mm_scan_remove_block);
900709ea971SDaniel Vetter
901e18c0412SDaniel Vetter /**
9023fa489daSChris Wilson * drm_mm_scan_color_evict - evict overlapping nodes on either side of hole
9033fa489daSChris Wilson * @scan: drm_mm scan with target hole
9043fa489daSChris Wilson *
9053fa489daSChris Wilson * After completing an eviction scan and removing the selected nodes, we may
9063fa489daSChris Wilson * need to remove a few more nodes from either side of the target hole if
9073fa489daSChris Wilson * mm.color_adjust is being used.
9083fa489daSChris Wilson *
9093fa489daSChris Wilson * Returns:
9103fa489daSChris Wilson * A node to evict, or NULL if there are no overlapping nodes.
9113fa489daSChris Wilson */
drm_mm_scan_color_evict(struct drm_mm_scan * scan)9123fa489daSChris Wilson struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan)
9133fa489daSChris Wilson {
9143fa489daSChris Wilson struct drm_mm *mm = scan->mm;
9153fa489daSChris Wilson struct drm_mm_node *hole;
9163fa489daSChris Wilson u64 hole_start, hole_end;
9173fa489daSChris Wilson
9183fa489daSChris Wilson DRM_MM_BUG_ON(list_empty(&mm->hole_stack));
9193fa489daSChris Wilson
9203fa489daSChris Wilson if (!mm->color_adjust)
9213fa489daSChris Wilson return NULL;
9223fa489daSChris Wilson
923b8ff1802SChris Wilson /*
924b8ff1802SChris Wilson * The hole found during scanning should ideally be the first element
925b8ff1802SChris Wilson * in the hole_stack list, but due to side-effects in the driver it
926b8ff1802SChris Wilson * may not be.
927b8ff1802SChris Wilson */
928b8ff1802SChris Wilson list_for_each_entry(hole, &mm->hole_stack, hole_stack) {
9293fa489daSChris Wilson hole_start = __drm_mm_hole_node_start(hole);
9304e64e553SChris Wilson hole_end = hole_start + hole->hole_size;
9313fa489daSChris Wilson
932b8ff1802SChris Wilson if (hole_start <= scan->hit_start &&
933b8ff1802SChris Wilson hole_end >= scan->hit_end)
934b8ff1802SChris Wilson break;
935b8ff1802SChris Wilson }
936b8ff1802SChris Wilson
937b8ff1802SChris Wilson /* We should only be called after we found the hole previously */
938b8ff1802SChris Wilson DRM_MM_BUG_ON(&hole->hole_stack == &mm->hole_stack);
939b8ff1802SChris Wilson if (unlikely(&hole->hole_stack == &mm->hole_stack))
940b8ff1802SChris Wilson return NULL;
941b8ff1802SChris Wilson
9423fa489daSChris Wilson DRM_MM_BUG_ON(hole_start > scan->hit_start);
9433fa489daSChris Wilson DRM_MM_BUG_ON(hole_end < scan->hit_end);
9443fa489daSChris Wilson
9453fa489daSChris Wilson mm->color_adjust(hole, scan->color, &hole_start, &hole_end);
9463fa489daSChris Wilson if (hole_start > scan->hit_start)
9473fa489daSChris Wilson return hole;
9483fa489daSChris Wilson if (hole_end < scan->hit_end)
9493fa489daSChris Wilson return list_next_entry(hole, node_list);
9503fa489daSChris Wilson
9513fa489daSChris Wilson return NULL;
9523fa489daSChris Wilson }
9533fa489daSChris Wilson EXPORT_SYMBOL(drm_mm_scan_color_evict);
9543fa489daSChris Wilson
9553fa489daSChris Wilson /**
956e18c0412SDaniel Vetter * drm_mm_init - initialize a drm-mm allocator
957e18c0412SDaniel Vetter * @mm: the drm_mm structure to initialize
958e18c0412SDaniel Vetter * @start: start of the range managed by @mm
959e18c0412SDaniel Vetter * @size: end of the range managed by @mm
960e18c0412SDaniel Vetter *
961e18c0412SDaniel Vetter * Note that @mm must be cleared to 0 before calling this function.
962e18c0412SDaniel Vetter */
drm_mm_init(struct drm_mm * mm,u64 start,u64 size)963440fd528SThierry Reding void drm_mm_init(struct drm_mm *mm, u64 start, u64 size)
964c0e09200SDave Airlie {
9656259a56bSChris Wilson DRM_MM_BUG_ON(start + size <= start);
9666259a56bSChris Wilson
9674e64e553SChris Wilson mm->color_adjust = NULL;
9684e64e553SChris Wilson
969ea7b1dd4SDaniel Vetter INIT_LIST_HEAD(&mm->hole_stack);
970f808c13fSDavidlohr Bueso mm->interval_tree = RB_ROOT_CACHED;
9712f7e8769SChris Wilson mm->holes_size = RB_ROOT_CACHED;
9724e64e553SChris Wilson mm->holes_addr = RB_ROOT;
973c0e09200SDave Airlie
974ea7b1dd4SDaniel Vetter /* Clever trick to avoid a special case in the free hole tracking. */
975ea7b1dd4SDaniel Vetter INIT_LIST_HEAD(&mm->head_node.node_list);
9764ee92c71SChris Wilson mm->head_node.flags = 0;
977ea7b1dd4SDaniel Vetter mm->head_node.mm = mm;
978ea7b1dd4SDaniel Vetter mm->head_node.start = start + size;
9794e64e553SChris Wilson mm->head_node.size = -size;
9804e64e553SChris Wilson add_hole(&mm->head_node);
981ea7b1dd4SDaniel Vetter
9824e64e553SChris Wilson mm->scan_active = 0;
9832dba5eb1SVlastimil Babka
9842dba5eb1SVlastimil Babka #ifdef CONFIG_DRM_DEBUG_MM
9852dba5eb1SVlastimil Babka stack_depot_init();
9862dba5eb1SVlastimil Babka #endif
987c0e09200SDave Airlie }
988673a394bSEric Anholt EXPORT_SYMBOL(drm_mm_init);
989c0e09200SDave Airlie
990e18c0412SDaniel Vetter /**
991e18c0412SDaniel Vetter * drm_mm_takedown - clean up a drm_mm allocator
992e18c0412SDaniel Vetter * @mm: drm_mm allocator to clean up
993e18c0412SDaniel Vetter *
994e18c0412SDaniel Vetter * Note that it is a bug to call this function on an allocator which is not
995e18c0412SDaniel Vetter * clean.
996e18c0412SDaniel Vetter */
drm_mm_takedown(struct drm_mm * mm)997c0e09200SDave Airlie void drm_mm_takedown(struct drm_mm *mm)
998c0e09200SDave Airlie {
999ac9bb7b7SChris Wilson if (WARN(!drm_mm_clean(mm),
10005705670dSChris Wilson "Memory manager not clean during takedown.\n"))
10015705670dSChris Wilson show_leaks(mm);
1002c0e09200SDave Airlie }
1003f453ba04SDave Airlie EXPORT_SYMBOL(drm_mm_takedown);
1004fa8a1238SDave Airlie
drm_mm_dump_hole(struct drm_printer * p,const struct drm_mm_node * entry)1005b5c3714fSDaniel Vetter static u64 drm_mm_dump_hole(struct drm_printer *p, const struct drm_mm_node *entry)
100699d7e48eSJerome Glisse {
10074e64e553SChris Wilson u64 start, size;
100899d7e48eSJerome Glisse
10094e64e553SChris Wilson size = entry->hole_size;
10104e64e553SChris Wilson if (size) {
10114e64e553SChris Wilson start = drm_mm_hole_node_start(entry);
10124e64e553SChris Wilson drm_printf(p, "%#018llx-%#018llx: %llu: free\n",
10134e64e553SChris Wilson start, start + size, size);
101499d7e48eSJerome Glisse }
10152c54b133SDaniel Vetter
10164e64e553SChris Wilson return size;
10172c54b133SDaniel Vetter }
1018e18c0412SDaniel Vetter /**
1019b5c3714fSDaniel Vetter * drm_mm_print - print allocator state
1020b5c3714fSDaniel Vetter * @mm: drm_mm allocator to print
1021b5c3714fSDaniel Vetter * @p: DRM printer to use
1022e18c0412SDaniel Vetter */
drm_mm_print(const struct drm_mm * mm,struct drm_printer * p)1023b5c3714fSDaniel Vetter void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p)
10242c54b133SDaniel Vetter {
102545b186f1SChris Wilson const struct drm_mm_node *entry;
1026440fd528SThierry Reding u64 total_used = 0, total_free = 0, total = 0;
10272c54b133SDaniel Vetter
1028b5c3714fSDaniel Vetter total_free += drm_mm_dump_hole(p, &mm->head_node);
10292c54b133SDaniel Vetter
10302c54b133SDaniel Vetter drm_mm_for_each_node(entry, mm) {
1031b5c3714fSDaniel Vetter drm_printf(p, "%#018llx-%#018llx: %llu: used\n", entry->start,
1032440fd528SThierry Reding entry->start + entry->size, entry->size);
10332c54b133SDaniel Vetter total_used += entry->size;
1034b5c3714fSDaniel Vetter total_free += drm_mm_dump_hole(p, entry);
1035ea7b1dd4SDaniel Vetter }
1036ea7b1dd4SDaniel Vetter total = total_free + total_used;
1037ea7b1dd4SDaniel Vetter
1038b5c3714fSDaniel Vetter drm_printf(p, "total: %llu, used %llu free %llu\n", total,
103999d7e48eSJerome Glisse total_used, total_free);
104099d7e48eSJerome Glisse }
1041b5c3714fSDaniel Vetter EXPORT_SYMBOL(drm_mm_print);
1042