xref: /dragonfly/sys/dev/drm/drm_mm.c (revision a85cb24f)
13f6063ccSDavid Shao /**************************************************************************
23f6063ccSDavid Shao  *
33f6063ccSDavid Shao  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
4*a85cb24fSFrançois Tigeot  * Copyright 2016 Intel Corporation
53f6063ccSDavid Shao  * All Rights Reserved.
63f6063ccSDavid Shao  *
73f6063ccSDavid Shao  * Permission is hereby granted, free of charge, to any person obtaining a
83f6063ccSDavid Shao  * copy of this software and associated documentation files (the
93f6063ccSDavid Shao  * "Software"), to deal in the Software without restriction, including
103f6063ccSDavid Shao  * without limitation the rights to use, copy, modify, merge, publish,
113f6063ccSDavid Shao  * distribute, sub license, and/or sell copies of the Software, and to
123f6063ccSDavid Shao  * permit persons to whom the Software is furnished to do so, subject to
133f6063ccSDavid Shao  * the following conditions:
143f6063ccSDavid Shao  *
153f6063ccSDavid Shao  * The above copyright notice and this permission notice (including the
163f6063ccSDavid Shao  * next paragraph) shall be included in all copies or substantial portions
173f6063ccSDavid Shao  * of the Software.
183f6063ccSDavid Shao  *
193f6063ccSDavid Shao  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
203f6063ccSDavid Shao  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
213f6063ccSDavid Shao  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
223f6063ccSDavid Shao  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
233f6063ccSDavid Shao  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
243f6063ccSDavid Shao  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
253f6063ccSDavid Shao  * USE OR OTHER DEALINGS IN THE SOFTWARE.
263f6063ccSDavid Shao  *
27ea132f0fSFrançois Tigeot  *
283f6063ccSDavid Shao  **************************************************************************/
293f6063ccSDavid Shao 
303f6063ccSDavid Shao /*
313f6063ccSDavid Shao  * Generic simple memory manager implementation. Intended to be used as a base
323f6063ccSDavid Shao  * class implementation for more advanced memory managers.
333f6063ccSDavid Shao  *
343f6063ccSDavid Shao  * Note that the algorithm used is quite simple and there might be substantial
35*a85cb24fSFrançois Tigeot  * performance gains if a smarter free list is implemented. Currently it is
36*a85cb24fSFrançois Tigeot  * just an unordered stack of free regions. This could easily be improved if
37*a85cb24fSFrançois Tigeot  * an RB-tree is used instead. At least if we expect heavy fragmentation.
383f6063ccSDavid Shao  *
393f6063ccSDavid Shao  * Aligned allocations can also see improvement.
403f6063ccSDavid Shao  *
413f6063ccSDavid Shao  * Authors:
423f6063ccSDavid Shao  * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
433f6063ccSDavid Shao  */
443f6063ccSDavid Shao 
4518e26a6dSFrançois Tigeot #include <drm/drmP.h>
4618e26a6dSFrançois Tigeot #include <drm/drm_mm.h>
47b6fbc077SFrançois Tigeot #include <linux/slab.h>
489edbd4a0SFrançois Tigeot #include <linux/seq_file.h>
49ea132f0fSFrançois Tigeot #include <linux/export.h>
503f6063ccSDavid Shao 
51*a85cb24fSFrançois Tigeot extern int drm_vma_debug;
52*a85cb24fSFrançois Tigeot 
53ba55f2f5SFrançois Tigeot /**
54ba55f2f5SFrançois Tigeot  * DOC: Overview
55ba55f2f5SFrançois Tigeot  *
56ba55f2f5SFrançois Tigeot  * drm_mm provides a simple range allocator. The drivers are free to use the
57ba55f2f5SFrançois Tigeot  * resource allocator from the linux core if it suits them, the upside of drm_mm
58ba55f2f5SFrançois Tigeot  * is that it's in the DRM core. Which means that it's easier to extend for
59ba55f2f5SFrançois Tigeot  * some of the crazier special purpose needs of gpus.
60ba55f2f5SFrançois Tigeot  *
61ba55f2f5SFrançois Tigeot  * The main data struct is &drm_mm, allocations are tracked in &drm_mm_node.
62ba55f2f5SFrançois Tigeot  * Drivers are free to embed either of them into their own suitable
63*a85cb24fSFrançois Tigeot  * datastructures. drm_mm itself will not do any memory allocations of its own,
64*a85cb24fSFrançois Tigeot  * so if drivers choose not to embed nodes they need to still allocate them
65ba55f2f5SFrançois Tigeot  * themselves.
66ba55f2f5SFrançois Tigeot  *
67ba55f2f5SFrançois Tigeot  * The range allocator also supports reservation of preallocated blocks. This is
68ba55f2f5SFrançois Tigeot  * useful for taking over initial mode setting configurations from the firmware,
69ba55f2f5SFrançois Tigeot  * where an object needs to be created which exactly matches the firmware's
70ba55f2f5SFrançois Tigeot  * scanout target. As long as the range is still free it can be inserted anytime
71ba55f2f5SFrançois Tigeot  * after the allocator is initialized, which helps with avoiding looped
72*a85cb24fSFrançois Tigeot  * dependencies in the driver load sequence.
73ba55f2f5SFrançois Tigeot  *
74ba55f2f5SFrançois Tigeot  * drm_mm maintains a stack of most recently freed holes, which of all
75ba55f2f5SFrançois Tigeot  * simplistic datastructures seems to be a fairly decent approach to clustering
76ba55f2f5SFrançois Tigeot  * allocations and avoiding too much fragmentation. This means free space
77ba55f2f5SFrançois Tigeot  * searches are O(num_holes). Given that all the fancy features drm_mm supports
78ba55f2f5SFrançois Tigeot  * something better would be fairly complex and since gfx thrashing is a fairly
79ba55f2f5SFrançois Tigeot  * steep cliff not a real concern. Removing a node again is O(1).
80ba55f2f5SFrançois Tigeot  *
81ba55f2f5SFrançois Tigeot  * drm_mm supports a few features: Alignment and range restrictions can be
82ba55f2f5SFrançois Tigeot  * supplied. Furthermore every &drm_mm_node has a color value (which is just an
83*a85cb24fSFrançois Tigeot  * opaque unsigned long) which in conjunction with a driver callback can be used
84ba55f2f5SFrançois Tigeot  * to implement sophisticated placement restrictions. The i915 DRM driver uses
85ba55f2f5SFrançois Tigeot  * this to implement guard pages between incompatible caching domains in the
86ba55f2f5SFrançois Tigeot  * graphics TT.
87ba55f2f5SFrançois Tigeot  *
88*a85cb24fSFrançois Tigeot  * Two behaviors are supported for searching and allocating: bottom-up and
89*a85cb24fSFrançois Tigeot  * top-down. The default is bottom-up. Top-down allocation can be used if the
90*a85cb24fSFrançois Tigeot  * memory area has different restrictions, or just to reduce fragmentation.
91ba55f2f5SFrançois Tigeot  *
92ba55f2f5SFrançois Tigeot  * Finally iteration helpers to walk all nodes and all holes are provided as are
93ba55f2f5SFrançois Tigeot  * some basic allocator dumpers for debugging.
94*a85cb24fSFrançois Tigeot  *
95*a85cb24fSFrançois Tigeot  * Note that this range allocator is not thread-safe, drivers need to protect
96*a85cb24fSFrançois Tigeot  * modifications with their on locking. The idea behind this is that for a full
97*a85cb24fSFrançois Tigeot  * memory manager additional data needs to be protected anyway, hence internal
98*a85cb24fSFrançois Tigeot  * locking would be fully redundant.
99ba55f2f5SFrançois Tigeot  */
1003f6063ccSDavid Shao 
101d7fb4e99SFrançois Tigeot static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
102d7fb4e99SFrançois Tigeot 						u64 size,
103*a85cb24fSFrançois Tigeot 						u64 alignment,
104d7fb4e99SFrançois Tigeot 						unsigned long color,
105d7fb4e99SFrançois Tigeot 						u64 start,
106d7fb4e99SFrançois Tigeot 						u64 end,
107d7fb4e99SFrançois Tigeot 						enum drm_mm_search_flags flags);
108d7fb4e99SFrançois Tigeot 
1094be47400SFrançois Tigeot #ifdef CONFIG_DRM_DEBUG_MM
1104be47400SFrançois Tigeot #include <linux/stackdepot.h>
1114be47400SFrançois Tigeot 
1124be47400SFrançois Tigeot #define STACKDEPTH 32
1134be47400SFrançois Tigeot #define BUFSZ 4096
1144be47400SFrançois Tigeot 
save_stack(struct drm_mm_node * node)1154be47400SFrançois Tigeot static noinline void save_stack(struct drm_mm_node *node)
1164be47400SFrançois Tigeot {
1174be47400SFrançois Tigeot 	unsigned long entries[STACKDEPTH];
1184be47400SFrançois Tigeot 	struct stack_trace trace = {
1194be47400SFrançois Tigeot 		.entries = entries,
1204be47400SFrançois Tigeot 		.max_entries = STACKDEPTH,
1214be47400SFrançois Tigeot 		.skip = 1
1224be47400SFrançois Tigeot 	};
1234be47400SFrançois Tigeot 
1244be47400SFrançois Tigeot 	save_stack_trace(&trace);
1254be47400SFrançois Tigeot 	if (trace.nr_entries != 0 &&
1264be47400SFrançois Tigeot 	    trace.entries[trace.nr_entries-1] == ULONG_MAX)
1274be47400SFrançois Tigeot 		trace.nr_entries--;
1284be47400SFrançois Tigeot 
1294be47400SFrançois Tigeot 	/* May be called under spinlock, so avoid sleeping */
1304be47400SFrançois Tigeot 	node->stack = depot_save_stack(&trace, GFP_NOWAIT);
1314be47400SFrançois Tigeot }
1324be47400SFrançois Tigeot 
show_leaks(struct drm_mm * mm)1334be47400SFrançois Tigeot static void show_leaks(struct drm_mm *mm)
1344be47400SFrançois Tigeot {
1354be47400SFrançois Tigeot 	struct drm_mm_node *node;
1364be47400SFrançois Tigeot 	unsigned long entries[STACKDEPTH];
1374be47400SFrançois Tigeot 	char *buf;
1384be47400SFrançois Tigeot 
1394be47400SFrançois Tigeot 	buf = kmalloc(BUFSZ, M_DRM, GFP_KERNEL);
1404be47400SFrançois Tigeot 	if (!buf)
1414be47400SFrançois Tigeot 		return;
1424be47400SFrançois Tigeot 
143*a85cb24fSFrançois Tigeot 	list_for_each_entry(node, drm_mm_nodes(mm), node_list) {
1444be47400SFrançois Tigeot 		struct stack_trace trace = {
1454be47400SFrançois Tigeot 			.entries = entries,
1464be47400SFrançois Tigeot 			.max_entries = STACKDEPTH
1474be47400SFrançois Tigeot 		};
1484be47400SFrançois Tigeot 
1494be47400SFrançois Tigeot 		if (!node->stack) {
1504be47400SFrançois Tigeot 			DRM_ERROR("node [%08llx + %08llx]: unknown owner\n",
1514be47400SFrançois Tigeot 				  node->start, node->size);
1524be47400SFrançois Tigeot 			continue;
1534be47400SFrançois Tigeot 		}
1544be47400SFrançois Tigeot 
1554be47400SFrançois Tigeot 		depot_fetch_stack(node->stack, &trace);
1564be47400SFrançois Tigeot 		snprint_stack_trace(buf, BUFSZ, &trace, 0);
1574be47400SFrançois Tigeot 		DRM_ERROR("node [%08llx + %08llx]: inserted at\n%s",
1584be47400SFrançois Tigeot 			  node->start, node->size, buf);
1594be47400SFrançois Tigeot 	}
1604be47400SFrançois Tigeot 
1614be47400SFrançois Tigeot 	kfree(buf);
1624be47400SFrançois Tigeot }
1634be47400SFrançois Tigeot 
1644be47400SFrançois Tigeot #undef STACKDEPTH
1654be47400SFrançois Tigeot #undef BUFSZ
1664be47400SFrançois Tigeot #else
save_stack(struct drm_mm_node * node)1674be47400SFrançois Tigeot static void save_stack(struct drm_mm_node *node) { }
show_leaks(struct drm_mm * mm)1684be47400SFrançois Tigeot static void show_leaks(struct drm_mm *mm) { }
1694be47400SFrançois Tigeot #endif
1704be47400SFrançois Tigeot 
1714be47400SFrançois Tigeot #define START(node) ((node)->start)
1724be47400SFrançois Tigeot #define LAST(node)  ((node)->start + (node)->size - 1)
1734be47400SFrançois Tigeot 
174*a85cb24fSFrançois Tigeot #ifndef __DragonFly__
INTERVAL_TREE_DEFINE(struct drm_mm_node,rb,u64,__subtree_last,START,LAST,static inline,drm_mm_interval_tree)175*a85cb24fSFrançois Tigeot INTERVAL_TREE_DEFINE(struct drm_mm_node, rb,
176*a85cb24fSFrançois Tigeot 		     u64, __subtree_last,
177*a85cb24fSFrançois Tigeot 		     START, LAST, static inline, drm_mm_interval_tree)
178*a85cb24fSFrançois Tigeot #else
179*a85cb24fSFrançois Tigeot static struct drm_mm_node *
180*a85cb24fSFrançois Tigeot drm_mm_interval_tree_iter_first(struct rb_root *rb, u64 start, u64 last)
181*a85cb24fSFrançois Tigeot {
182*a85cb24fSFrançois Tigeot 	struct drm_mm *mm = container_of(rb, typeof(*mm), interval_tree);
183*a85cb24fSFrançois Tigeot 	struct drm_mm_node *node;
184*a85cb24fSFrançois Tigeot 
185*a85cb24fSFrançois Tigeot 	drm_mm_for_each_node(node, mm) {
186*a85cb24fSFrançois Tigeot 		if (LAST(node) >= start && START(node) <= last)
187*a85cb24fSFrançois Tigeot 			return node;
188*a85cb24fSFrançois Tigeot 	}
189*a85cb24fSFrançois Tigeot 	return NULL;
190*a85cb24fSFrançois Tigeot }
191*a85cb24fSFrançois Tigeot #endif
192*a85cb24fSFrançois Tigeot 
193*a85cb24fSFrançois Tigeot struct drm_mm_node *
194*a85cb24fSFrançois Tigeot __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last)
195*a85cb24fSFrançois Tigeot {
196*a85cb24fSFrançois Tigeot 	return drm_mm_interval_tree_iter_first((struct rb_root *)&mm->interval_tree,
197*a85cb24fSFrançois Tigeot 					       start, last) ?: (struct drm_mm_node *)&mm->head_node;
198*a85cb24fSFrançois Tigeot }
199*a85cb24fSFrançois Tigeot EXPORT_SYMBOL(__drm_mm_interval_first);
200*a85cb24fSFrançois Tigeot 
drm_mm_insert_helper(struct drm_mm_node * hole_node,struct drm_mm_node * node,u64 size,u64 alignment,unsigned long color,u64 range_start,u64 range_end,enum drm_mm_allocator_flags flags)2015718399fSFrançois Tigeot static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
2025718399fSFrançois Tigeot 				 struct drm_mm_node *node,
203*a85cb24fSFrançois Tigeot 				 u64 size, u64 alignment,
204ba55f2f5SFrançois Tigeot 				 unsigned long color,
205*a85cb24fSFrançois Tigeot 				 u64 range_start, u64 range_end,
206ba55f2f5SFrançois Tigeot 				 enum drm_mm_allocator_flags flags)
2073f6063ccSDavid Shao {
2085718399fSFrançois Tigeot 	struct drm_mm *mm = hole_node->mm;
209f77dbd6cSFrançois Tigeot 	u64 hole_start = drm_mm_hole_node_start(hole_node);
210f77dbd6cSFrançois Tigeot 	u64 hole_end = drm_mm_hole_node_end(hole_node);
211f77dbd6cSFrançois Tigeot 	u64 adj_start = hole_start;
212f77dbd6cSFrançois Tigeot 	u64 adj_end = hole_end;
2133f6063ccSDavid Shao 
214*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node) || node->allocated);
2153f6063ccSDavid Shao 
216ea132f0fSFrançois Tigeot 	if (mm->color_adjust)
217ea132f0fSFrançois Tigeot 		mm->color_adjust(hole_node, color, &adj_start, &adj_end);
2183f6063ccSDavid Shao 
219*a85cb24fSFrançois Tigeot 	adj_start = max(adj_start, range_start);
220*a85cb24fSFrançois Tigeot 	adj_end = min(adj_end, range_end);
221*a85cb24fSFrançois Tigeot 
222ba55f2f5SFrançois Tigeot 	if (flags & DRM_MM_CREATE_TOP)
223ba55f2f5SFrançois Tigeot 		adj_start = adj_end - size;
224ba55f2f5SFrançois Tigeot 
225ea132f0fSFrançois Tigeot 	if (alignment) {
226*a85cb24fSFrançois Tigeot 		u64 rem;
227f77dbd6cSFrançois Tigeot 
228*a85cb24fSFrançois Tigeot 		div64_u64_rem(adj_start, alignment, &rem);
229f77dbd6cSFrançois Tigeot 		if (rem) {
230ba55f2f5SFrançois Tigeot 			if (flags & DRM_MM_CREATE_TOP)
231f77dbd6cSFrançois Tigeot 				adj_start -= rem;
232ba55f2f5SFrançois Tigeot 			else
233f77dbd6cSFrançois Tigeot 				adj_start += alignment - rem;
234ea132f0fSFrançois Tigeot 		}
235ba55f2f5SFrançois Tigeot 	}
236ba55f2f5SFrançois Tigeot 
237ea132f0fSFrançois Tigeot 	if (adj_start == hole_start) {
2385718399fSFrançois Tigeot 		hole_node->hole_follows = 0;
239ea132f0fSFrançois Tigeot 		list_del(&hole_node->hole_stack);
240ea132f0fSFrançois Tigeot 	}
2413f6063ccSDavid Shao 
242ea132f0fSFrançois Tigeot 	node->start = adj_start;
2435718399fSFrançois Tigeot 	node->size = size;
2445718399fSFrançois Tigeot 	node->mm = mm;
245ea132f0fSFrançois Tigeot 	node->color = color;
2465718399fSFrançois Tigeot 	node->allocated = 1;
2473f6063ccSDavid Shao 
2485718399fSFrançois Tigeot 	list_add(&node->node_list, &hole_node->node_list);
2495718399fSFrançois Tigeot 
250*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(node->start < range_start);
251*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(node->start < adj_start);
252*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(node->start + node->size > adj_end);
253*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(node->start + node->size > range_end);
2545718399fSFrançois Tigeot 
255ea132f0fSFrançois Tigeot 	node->hole_follows = 0;
256b6fbc077SFrançois Tigeot 	if (__drm_mm_hole_node_start(node) < hole_end) {
2575718399fSFrançois Tigeot 		list_add(&node->hole_stack, &mm->hole_stack);
2585718399fSFrançois Tigeot 		node->hole_follows = 1;
2595718399fSFrançois Tigeot 	}
2604be47400SFrançois Tigeot 
2614be47400SFrançois Tigeot 	save_stack(node);
2623f6063ccSDavid Shao }
2633f6063ccSDavid Shao 
264ba55f2f5SFrançois Tigeot /**
265ba55f2f5SFrançois Tigeot  * drm_mm_reserve_node - insert an pre-initialized node
266ba55f2f5SFrançois Tigeot  * @mm: drm_mm allocator to insert @node into
267ba55f2f5SFrançois Tigeot  * @node: drm_mm_node to insert
268ba55f2f5SFrançois Tigeot  *
269*a85cb24fSFrançois Tigeot  * This functions inserts an already set-up &drm_mm_node into the allocator,
270*a85cb24fSFrançois Tigeot  * meaning that start, size and color must be set by the caller. All other
271*a85cb24fSFrançois Tigeot  * fields must be cleared to 0. This is useful to initialize the allocator with
272*a85cb24fSFrançois Tigeot  * preallocated objects which must be set-up before the range allocator can be
273*a85cb24fSFrançois Tigeot  * set-up, e.g. when taking over a firmware framebuffer.
274ba55f2f5SFrançois Tigeot  *
275ba55f2f5SFrançois Tigeot  * Returns:
276ba55f2f5SFrançois Tigeot  * 0 on success, -ENOSPC if there's no hole where @node is.
277ba55f2f5SFrançois Tigeot  */
drm_mm_reserve_node(struct drm_mm * mm,struct drm_mm_node * node)2789edbd4a0SFrançois Tigeot int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
279b6fbc077SFrançois Tigeot {
280*a85cb24fSFrançois Tigeot 	u64 end = node->start + node->size;
2819edbd4a0SFrançois Tigeot 	struct drm_mm_node *hole;
282*a85cb24fSFrançois Tigeot 	u64 hole_start, hole_end;
283*a85cb24fSFrançois Tigeot 	u64 adj_start, adj_end;
2841e12ee3bSFrançois Tigeot 
2851dedbd3bSFrançois Tigeot 	end = node->start + node->size;
286*a85cb24fSFrançois Tigeot 	if (unlikely(end <= node->start))
287*a85cb24fSFrançois Tigeot 		return -ENOSPC;
2881dedbd3bSFrançois Tigeot 
2899edbd4a0SFrançois Tigeot 	/* Find the relevant hole to add our node to */
290*a85cb24fSFrançois Tigeot 	hole = drm_mm_interval_tree_iter_first(&mm->interval_tree,
291*a85cb24fSFrançois Tigeot 					       node->start, ~(u64)0);
292*a85cb24fSFrançois Tigeot 	if (hole) {
293*a85cb24fSFrançois Tigeot 		if (hole->start < end)
294*a85cb24fSFrançois Tigeot 			return -ENOSPC;
295*a85cb24fSFrançois Tigeot 	} else {
296*a85cb24fSFrançois Tigeot 		hole = list_entry(drm_mm_nodes(mm), typeof(*hole), node_list);
297*a85cb24fSFrançois Tigeot 	}
298*a85cb24fSFrançois Tigeot 
299*a85cb24fSFrançois Tigeot 	hole = list_last_entry(&hole->node_list, typeof(*hole), node_list);
300*a85cb24fSFrançois Tigeot 	if (!drm_mm_hole_follows(hole))
301*a85cb24fSFrançois Tigeot 		return -ENOSPC;
302*a85cb24fSFrançois Tigeot 
303*a85cb24fSFrançois Tigeot 	adj_start = hole_start = __drm_mm_hole_node_start(hole);
304*a85cb24fSFrançois Tigeot 	adj_end = hole_end = __drm_mm_hole_node_end(hole);
305*a85cb24fSFrançois Tigeot 
306*a85cb24fSFrançois Tigeot 	if (mm->color_adjust)
307*a85cb24fSFrançois Tigeot 		mm->color_adjust(hole, node->color, &adj_start, &adj_end);
308*a85cb24fSFrançois Tigeot 
309*a85cb24fSFrançois Tigeot 	if (adj_start > node->start || adj_end < end)
310*a85cb24fSFrançois Tigeot 		return -ENOSPC;
311b6fbc077SFrançois Tigeot 
312b6fbc077SFrançois Tigeot 	node->mm = mm;
313b6fbc077SFrançois Tigeot 	node->allocated = 1;
314b6fbc077SFrançois Tigeot 
315b6fbc077SFrançois Tigeot 	list_add(&node->node_list, &hole->node_list);
316b6fbc077SFrançois Tigeot 
317*a85cb24fSFrançois Tigeot #if 0
318*a85cb24fSFrançois Tigeot 	drm_mm_interval_tree_add_node(hole, node);
319*a85cb24fSFrançois Tigeot #endif
320*a85cb24fSFrançois Tigeot 
3219edbd4a0SFrançois Tigeot 	if (node->start == hole_start) {
322b6fbc077SFrançois Tigeot 		hole->hole_follows = 0;
323*a85cb24fSFrançois Tigeot 		list_del(&hole->hole_stack);
324b6fbc077SFrançois Tigeot 	}
325b6fbc077SFrançois Tigeot 
326b6fbc077SFrançois Tigeot 	node->hole_follows = 0;
327b6fbc077SFrançois Tigeot 	if (end != hole_end) {
328b6fbc077SFrançois Tigeot 		list_add(&node->hole_stack, &mm->hole_stack);
329b6fbc077SFrançois Tigeot 		node->hole_follows = 1;
330b6fbc077SFrançois Tigeot 	}
331b6fbc077SFrançois Tigeot 
332*a85cb24fSFrançois Tigeot 	save_stack(node);
333b6fbc077SFrançois Tigeot 
334*a85cb24fSFrançois Tigeot 	return 0;
335b6fbc077SFrançois Tigeot }
3369edbd4a0SFrançois Tigeot EXPORT_SYMBOL(drm_mm_reserve_node);
337b6fbc077SFrançois Tigeot 
338ea132f0fSFrançois Tigeot /**
339ba55f2f5SFrançois Tigeot  * drm_mm_insert_node_in_range_generic - ranged search for space and insert @node
340ba55f2f5SFrançois Tigeot  * @mm: drm_mm to allocate from
341ba55f2f5SFrançois Tigeot  * @node: preallocate node to insert
342ba55f2f5SFrançois Tigeot  * @size: size of the allocation
343ba55f2f5SFrançois Tigeot  * @alignment: alignment of the allocation
344ba55f2f5SFrançois Tigeot  * @color: opaque tag value to use for this node
345ba55f2f5SFrançois Tigeot  * @start: start of the allowed range for this node
346ba55f2f5SFrançois Tigeot  * @end: end of the allowed range for this node
347ba55f2f5SFrançois Tigeot  * @sflags: flags to fine-tune the allocation search
348ba55f2f5SFrançois Tigeot  * @aflags: flags to fine-tune the allocation behavior
349ba55f2f5SFrançois Tigeot  *
350*a85cb24fSFrançois Tigeot  * The preallocated @node must be cleared to 0.
351ba55f2f5SFrançois Tigeot  *
352ba55f2f5SFrançois Tigeot  * Returns:
353ba55f2f5SFrançois Tigeot  * 0 on success, -ENOSPC if there's no suitable hole.
354ea132f0fSFrançois Tigeot  */
drm_mm_insert_node_in_range_generic(struct drm_mm * mm,struct drm_mm_node * node,u64 size,u64 alignment,unsigned long color,u64 start,u64 end,enum drm_mm_search_flags sflags,enum drm_mm_allocator_flags aflags)355ea132f0fSFrançois Tigeot int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
356*a85cb24fSFrançois Tigeot 					u64 size, u64 alignment,
357ba55f2f5SFrançois Tigeot 					unsigned long color,
358f77dbd6cSFrançois Tigeot 					u64 start, u64 end,
359ba55f2f5SFrançois Tigeot 					enum drm_mm_search_flags sflags,
360ba55f2f5SFrançois Tigeot 					enum drm_mm_allocator_flags aflags)
361ea132f0fSFrançois Tigeot {
362ea132f0fSFrançois Tigeot 	struct drm_mm_node *hole_node;
363ea132f0fSFrançois Tigeot 
3641dedbd3bSFrançois Tigeot 	if (WARN_ON(size == 0))
3651dedbd3bSFrançois Tigeot 		return -EINVAL;
3661dedbd3bSFrançois Tigeot 
367ea132f0fSFrançois Tigeot 	hole_node = drm_mm_search_free_in_range_generic(mm,
368ea132f0fSFrançois Tigeot 							size, alignment, color,
369ba55f2f5SFrançois Tigeot 							start, end, sflags);
370ea132f0fSFrançois Tigeot 	if (!hole_node)
371ea132f0fSFrançois Tigeot 		return -ENOSPC;
372ea132f0fSFrançois Tigeot 
373*a85cb24fSFrançois Tigeot 	drm_mm_insert_helper(hole_node, node,
374ea132f0fSFrançois Tigeot 			     size, alignment, color,
375ba55f2f5SFrançois Tigeot 			     start, end, aflags);
376ea132f0fSFrançois Tigeot 	return 0;
377ea132f0fSFrançois Tigeot }
378ea132f0fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
3795718399fSFrançois Tigeot 
380ea132f0fSFrançois Tigeot /**
381ba55f2f5SFrançois Tigeot  * drm_mm_remove_node - Remove a memory node from the allocator.
382ba55f2f5SFrançois Tigeot  * @node: drm_mm_node to remove
383ba55f2f5SFrançois Tigeot  *
384ba55f2f5SFrançois Tigeot  * This just removes a node from its drm_mm allocator. The node does not need to
385ba55f2f5SFrançois Tigeot  * be cleared again before it can be re-inserted into this or any other drm_mm
386*a85cb24fSFrançois Tigeot  * allocator. It is a bug to call this function on a unallocated node.
387ea132f0fSFrançois Tigeot  */
drm_mm_remove_node(struct drm_mm_node * node)3885718399fSFrançois Tigeot void drm_mm_remove_node(struct drm_mm_node *node)
3895718399fSFrançois Tigeot {
3905718399fSFrançois Tigeot 	struct drm_mm *mm = node->mm;
3915718399fSFrançois Tigeot 	struct drm_mm_node *prev_node;
3925718399fSFrançois Tigeot 
393*a85cb24fSFrançois Tigeot 	if (drm_vma_debug & 2) {
394*a85cb24fSFrançois Tigeot 		drm_vma_debug &= ~2;
395*a85cb24fSFrançois Tigeot 	}
3969edbd4a0SFrançois Tigeot 
397*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(!node->allocated);
398*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(node->scanned_block);
3995718399fSFrançois Tigeot 
4005718399fSFrançois Tigeot 	prev_node =
4015718399fSFrançois Tigeot 	    list_entry(node->node_list.prev, struct drm_mm_node, node_list);
4025718399fSFrançois Tigeot 
403*a85cb24fSFrançois Tigeot 	if (drm_mm_hole_follows(node)) {
404*a85cb24fSFrançois Tigeot 		DRM_MM_BUG_ON(__drm_mm_hole_node_start(node) ==
405b6fbc077SFrançois Tigeot 			      __drm_mm_hole_node_end(node));
4065718399fSFrançois Tigeot 		list_del(&node->hole_stack);
407*a85cb24fSFrançois Tigeot 	} else {
408*a85cb24fSFrançois Tigeot 		DRM_MM_BUG_ON(__drm_mm_hole_node_start(node) !=
409b6fbc077SFrançois Tigeot 			      __drm_mm_hole_node_end(node));
410*a85cb24fSFrançois Tigeot 	}
411b6fbc077SFrançois Tigeot 
412*a85cb24fSFrançois Tigeot 	if (!drm_mm_hole_follows(prev_node)) {
4135718399fSFrançois Tigeot 		prev_node->hole_follows = 1;
4145718399fSFrançois Tigeot 		list_add(&prev_node->hole_stack, &mm->hole_stack);
4155718399fSFrançois Tigeot 	} else
4165718399fSFrançois Tigeot 		list_move(&prev_node->hole_stack, &mm->hole_stack);
4175718399fSFrançois Tigeot 
4185718399fSFrançois Tigeot 	list_del(&node->node_list);
4195718399fSFrançois Tigeot 	node->allocated = 0;
4205718399fSFrançois Tigeot }
421ea132f0fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_remove_node);
4225718399fSFrançois Tigeot 
check_free_hole(u64 start,u64 end,u64 size,u64 alignment)423*a85cb24fSFrançois Tigeot static int check_free_hole(u64 start, u64 end, u64 size, u64 alignment)
4245718399fSFrançois Tigeot {
4255718399fSFrançois Tigeot 	if (end - start < size)
4265718399fSFrançois Tigeot 		return 0;
4275718399fSFrançois Tigeot 
4285718399fSFrançois Tigeot 	if (alignment) {
429*a85cb24fSFrançois Tigeot 		u64 rem;
430f77dbd6cSFrançois Tigeot 
431*a85cb24fSFrançois Tigeot 		div64_u64_rem(start, alignment, &rem);
432f77dbd6cSFrançois Tigeot 		if (rem)
433f77dbd6cSFrançois Tigeot 			start += alignment - rem;
4343f6063ccSDavid Shao 	}
4355718399fSFrançois Tigeot 
436ea132f0fSFrançois Tigeot 	return end >= start + size;
4373f6063ccSDavid Shao }
4385718399fSFrançois Tigeot 
drm_mm_search_free_in_range_generic(const struct drm_mm * mm,u64 size,u64 alignment,unsigned long color,u64 start,u64 end,enum drm_mm_search_flags flags)439d7fb4e99SFrançois Tigeot static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
440f77dbd6cSFrançois Tigeot 							u64 size,
441*a85cb24fSFrançois Tigeot 							u64 alignment,
442ea132f0fSFrançois Tigeot 							unsigned long color,
443f77dbd6cSFrançois Tigeot 							u64 start,
444f77dbd6cSFrançois Tigeot 							u64 end,
4459edbd4a0SFrançois Tigeot 							enum drm_mm_search_flags flags)
446ea132f0fSFrançois Tigeot {
447ea132f0fSFrançois Tigeot 	struct drm_mm_node *entry;
448ea132f0fSFrançois Tigeot 	struct drm_mm_node *best;
449f77dbd6cSFrançois Tigeot 	u64 adj_start;
450f77dbd6cSFrançois Tigeot 	u64 adj_end;
451f77dbd6cSFrançois Tigeot 	u64 best_size;
452ea132f0fSFrançois Tigeot 
453*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(mm->scan_active);
454ea132f0fSFrançois Tigeot 
455ea132f0fSFrançois Tigeot 	best = NULL;
456ea132f0fSFrançois Tigeot 	best_size = ~0UL;
457ea132f0fSFrançois Tigeot 
458ba55f2f5SFrançois Tigeot 	__drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
459ba55f2f5SFrançois Tigeot 			       flags & DRM_MM_SEARCH_BELOW) {
460f77dbd6cSFrançois Tigeot 		u64 hole_size = adj_end - adj_start;
461ba55f2f5SFrançois Tigeot 
462ea132f0fSFrançois Tigeot 		if (mm->color_adjust) {
463ea132f0fSFrançois Tigeot 			mm->color_adjust(entry, color, &adj_start, &adj_end);
464ea132f0fSFrançois Tigeot 			if (adj_end <= adj_start)
465ea132f0fSFrançois Tigeot 				continue;
466ea132f0fSFrançois Tigeot 		}
467ea132f0fSFrançois Tigeot 
468*a85cb24fSFrançois Tigeot 		adj_start = max(adj_start, start);
469*a85cb24fSFrançois Tigeot 		adj_end = min(adj_end, end);
470*a85cb24fSFrançois Tigeot 
471ea132f0fSFrançois Tigeot 		if (!check_free_hole(adj_start, adj_end, size, alignment))
472ea132f0fSFrançois Tigeot 			continue;
473ea132f0fSFrançois Tigeot 
4749edbd4a0SFrançois Tigeot 		if (!(flags & DRM_MM_SEARCH_BEST))
475ea132f0fSFrançois Tigeot 			return entry;
476ea132f0fSFrançois Tigeot 
477ba55f2f5SFrançois Tigeot 		if (hole_size < best_size) {
478ea132f0fSFrançois Tigeot 			best = entry;
479ba55f2f5SFrançois Tigeot 			best_size = hole_size;
480ea132f0fSFrançois Tigeot 		}
481ea132f0fSFrançois Tigeot 	}
482ea132f0fSFrançois Tigeot 
483ea132f0fSFrançois Tigeot 	return best;
484ea132f0fSFrançois Tigeot }
485ea132f0fSFrançois Tigeot 
486ea132f0fSFrançois Tigeot /**
487ba55f2f5SFrançois Tigeot  * drm_mm_replace_node - move an allocation from @old to @new
488ba55f2f5SFrançois Tigeot  * @old: drm_mm_node to remove from the allocator
489ba55f2f5SFrançois Tigeot  * @new: drm_mm_node which should inherit @old's allocation
490ba55f2f5SFrançois Tigeot  *
491ba55f2f5SFrançois Tigeot  * This is useful for when drivers embed the drm_mm_node structure and hence
492ba55f2f5SFrançois Tigeot  * can't move allocations by reassigning pointers. It's a combination of remove
493ba55f2f5SFrançois Tigeot  * and insert with the guarantee that the allocation start will match.
494ea132f0fSFrançois Tigeot  */
drm_mm_replace_node(struct drm_mm_node * old,struct drm_mm_node * new)4955718399fSFrançois Tigeot void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
4965718399fSFrançois Tigeot {
497*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(!old->allocated);
498*a85cb24fSFrançois Tigeot 
4995718399fSFrançois Tigeot 	list_replace(&old->node_list, &new->node_list);
5005718399fSFrançois Tigeot 	list_replace(&old->hole_stack, &new->hole_stack);
5015718399fSFrançois Tigeot 	new->hole_follows = old->hole_follows;
5025718399fSFrançois Tigeot 	new->mm = old->mm;
5035718399fSFrançois Tigeot 	new->start = old->start;
5045718399fSFrançois Tigeot 	new->size = old->size;
505ea132f0fSFrançois Tigeot 	new->color = old->color;
5065718399fSFrançois Tigeot 
5075718399fSFrançois Tigeot 	old->allocated = 0;
5085718399fSFrançois Tigeot 	new->allocated = 1;
5095718399fSFrançois Tigeot }
510ea132f0fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_replace_node);
5115718399fSFrançois Tigeot 
512ea132f0fSFrançois Tigeot /**
513*a85cb24fSFrançois Tigeot  * DOC: lru scan roster
514ba55f2f5SFrançois Tigeot  *
515ba55f2f5SFrançois Tigeot  * Very often GPUs need to have continuous allocations for a given object. When
516ba55f2f5SFrançois Tigeot  * evicting objects to make space for a new one it is therefore not most
517ba55f2f5SFrançois Tigeot  * efficient when we simply start to select all objects from the tail of an LRU
518ba55f2f5SFrançois Tigeot  * until there's a suitable hole: Especially for big objects or nodes that
519ba55f2f5SFrançois Tigeot  * otherwise have special allocation constraints there's a good chance we evict
520*a85cb24fSFrançois Tigeot  * lots of (smaller) objects unnecessarily.
521ba55f2f5SFrançois Tigeot  *
522ba55f2f5SFrançois Tigeot  * The DRM range allocator supports this use-case through the scanning
523ba55f2f5SFrançois Tigeot  * interfaces. First a scan operation needs to be initialized with
524*a85cb24fSFrançois Tigeot  * drm_mm_scan_init() or drm_mm_scan_init_with_range(). The driver adds
525*a85cb24fSFrançois Tigeot  * objects to the roster, probably by walking an LRU list, but this can be
526*a85cb24fSFrançois Tigeot  * freely implemented. Eviction candiates are added using
527*a85cb24fSFrançois Tigeot  * drm_mm_scan_add_block() until a suitable hole is found or there are no
528*a85cb24fSFrançois Tigeot  * further evictable objects. Eviction roster metadata is tracked in struct
529*a85cb24fSFrançois Tigeot  * &drm_mm_scan.
530ba55f2f5SFrançois Tigeot  *
531*a85cb24fSFrançois Tigeot  * The driver must walk through all objects again in exactly the reverse
532ba55f2f5SFrançois Tigeot  * order to restore the allocator state. Note that while the allocator is used
533ba55f2f5SFrançois Tigeot  * in the scan mode no other operation is allowed.
534ba55f2f5SFrançois Tigeot  *
535*a85cb24fSFrançois Tigeot  * Finally the driver evicts all objects selected (drm_mm_scan_remove_block()
536*a85cb24fSFrançois Tigeot  * reported true) in the scan, and any overlapping nodes after color adjustment
537*a85cb24fSFrançois Tigeot  * (drm_mm_scan_color_evict()). Adding and removing an object is O(1), and
538*a85cb24fSFrançois Tigeot  * since freeing a node is also O(1) the overall complexity is
539*a85cb24fSFrançois Tigeot  * O(scanned_objects). So like the free stack which needs to be walked before a
540*a85cb24fSFrançois Tigeot  * scan operation even begins this is linear in the number of objects. It
541*a85cb24fSFrançois Tigeot  * doesn't seem to hurt too badly.
542ba55f2f5SFrançois Tigeot  */
543ba55f2f5SFrançois Tigeot 
544ba55f2f5SFrançois Tigeot /**
545*a85cb24fSFrançois Tigeot  * drm_mm_scan_init_with_range - initialize range-restricted lru scanning
546*a85cb24fSFrançois Tigeot  * @scan: scan state
547ba55f2f5SFrançois Tigeot  * @mm: drm_mm to scan
548ba55f2f5SFrançois Tigeot  * @size: size of the allocation
549ba55f2f5SFrançois Tigeot  * @alignment: alignment of the allocation
550ba55f2f5SFrançois Tigeot  * @color: opaque tag value to use for the allocation
551ba55f2f5SFrançois Tigeot  * @start: start of the allowed range for the allocation
552ba55f2f5SFrançois Tigeot  * @end: end of the allowed range for the allocation
553*a85cb24fSFrançois Tigeot  * @flags: flags to specify how the allocation will be performed afterwards
554ea132f0fSFrançois Tigeot  *
555ea132f0fSFrançois Tigeot  * This simply sets up the scanning routines with the parameters for the desired
556*a85cb24fSFrançois Tigeot  * hole.
557ea132f0fSFrançois Tigeot  *
558ba55f2f5SFrançois Tigeot  * Warning:
559ba55f2f5SFrançois Tigeot  * As long as the scan list is non-empty, no other operations than
560ea132f0fSFrançois Tigeot  * adding/removing nodes to/from the scan list are allowed.
561ea132f0fSFrançois Tigeot  */
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,unsigned int flags)562*a85cb24fSFrançois Tigeot void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
563*a85cb24fSFrançois Tigeot 				 struct drm_mm *mm,
564f77dbd6cSFrançois Tigeot 				 u64 size,
565*a85cb24fSFrançois Tigeot 				 u64 alignment,
566ea132f0fSFrançois Tigeot 				 unsigned long color,
567f77dbd6cSFrançois Tigeot 				 u64 start,
568*a85cb24fSFrançois Tigeot 				 u64 end,
569*a85cb24fSFrançois Tigeot 				 unsigned int flags)
5705718399fSFrançois Tigeot {
571*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(start >= end);
572*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(!size || size > end - start);
573*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(mm->scan_active);
574*a85cb24fSFrançois Tigeot 
575*a85cb24fSFrançois Tigeot 	scan->mm = mm;
576*a85cb24fSFrançois Tigeot 
577*a85cb24fSFrançois Tigeot 	if (alignment <= 1)
578*a85cb24fSFrançois Tigeot 		alignment = 0;
579*a85cb24fSFrançois Tigeot 
580*a85cb24fSFrançois Tigeot 	scan->color = color;
581*a85cb24fSFrançois Tigeot 	scan->alignment = alignment;
582*a85cb24fSFrançois Tigeot 	scan->remainder_mask = is_power_of_2(alignment) ? alignment - 1 : 0;
583*a85cb24fSFrançois Tigeot 	scan->size = size;
584*a85cb24fSFrançois Tigeot 	scan->flags = flags;
585*a85cb24fSFrançois Tigeot 
586*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(end <= start);
587*a85cb24fSFrançois Tigeot 	scan->range_start = start;
588*a85cb24fSFrançois Tigeot 	scan->range_end = end;
589*a85cb24fSFrançois Tigeot 
590*a85cb24fSFrançois Tigeot 	scan->hit_start = U64_MAX;
591*a85cb24fSFrançois Tigeot 	scan->hit_end = 0;
5925718399fSFrançois Tigeot }
593*a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_scan_init_with_range);
5945718399fSFrançois Tigeot 
595ea132f0fSFrançois Tigeot /**
596ba55f2f5SFrançois Tigeot  * drm_mm_scan_add_block - add a node to the scan list
597*a85cb24fSFrançois Tigeot  * @scan: the active drm_mm scanner
598ba55f2f5SFrançois Tigeot  * @node: drm_mm_node to add
599ba55f2f5SFrançois Tigeot  *
600ea132f0fSFrançois Tigeot  * Add a node to the scan list that might be freed to make space for the desired
601ea132f0fSFrançois Tigeot  * hole.
602ea132f0fSFrançois Tigeot  *
603ba55f2f5SFrançois Tigeot  * Returns:
604ba55f2f5SFrançois Tigeot  * True if a hole has been found, false otherwise.
605ea132f0fSFrançois Tigeot  */
drm_mm_scan_add_block(struct drm_mm_scan * scan,struct drm_mm_node * node)606*a85cb24fSFrançois Tigeot bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
607*a85cb24fSFrançois Tigeot 			   struct drm_mm_node *node)
6085718399fSFrançois Tigeot {
609*a85cb24fSFrançois Tigeot 	struct drm_mm *mm = scan->mm;
610*a85cb24fSFrançois Tigeot 	struct drm_mm_node *hole;
611f77dbd6cSFrançois Tigeot 	u64 hole_start, hole_end;
612*a85cb24fSFrançois Tigeot 	u64 col_start, col_end;
613f77dbd6cSFrançois Tigeot 	u64 adj_start, adj_end;
6145718399fSFrançois Tigeot 
615*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(node->mm != mm);
616*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(!node->allocated);
617*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(node->scanned_block);
618*a85cb24fSFrançois Tigeot 	node->scanned_block = true;
619*a85cb24fSFrançois Tigeot 	mm->scan_active++;
6205718399fSFrançois Tigeot 
621*a85cb24fSFrançois Tigeot 	/* Remove this block from the node_list so that we enlarge the hole
622*a85cb24fSFrançois Tigeot 	 * (distance between the end of our previous node and the start of
623*a85cb24fSFrançois Tigeot 	 * or next), without poisoning the link so that we can restore it
624*a85cb24fSFrançois Tigeot 	 * later in drm_mm_scan_remove_block().
625*a85cb24fSFrançois Tigeot 	 */
626*a85cb24fSFrançois Tigeot 	hole = list_prev_entry(node, node_list);
627*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(list_next_entry(hole, node_list) != node);
628*a85cb24fSFrançois Tigeot 	__list_del_entry(&node->node_list);
6295718399fSFrançois Tigeot 
630*a85cb24fSFrançois Tigeot 	hole_start = __drm_mm_hole_node_start(hole);
631*a85cb24fSFrançois Tigeot 	hole_end = __drm_mm_hole_node_end(hole);
6325718399fSFrançois Tigeot 
633*a85cb24fSFrançois Tigeot 	col_start = hole_start;
634*a85cb24fSFrançois Tigeot 	col_end = hole_end;
635ea132f0fSFrançois Tigeot 	if (mm->color_adjust)
636*a85cb24fSFrançois Tigeot 		mm->color_adjust(hole, scan->color, &col_start, &col_end);
637ea132f0fSFrançois Tigeot 
638*a85cb24fSFrançois Tigeot 	adj_start = max(col_start, scan->range_start);
639*a85cb24fSFrançois Tigeot 	adj_end = min(col_end, scan->range_end);
640*a85cb24fSFrançois Tigeot 	if (adj_end <= adj_start || adj_end - adj_start < scan->size)
641*a85cb24fSFrançois Tigeot 		return false;
642*a85cb24fSFrançois Tigeot 
643*a85cb24fSFrançois Tigeot 	if (scan->flags == DRM_MM_CREATE_TOP)
644*a85cb24fSFrançois Tigeot 		adj_start = adj_end - scan->size;
645*a85cb24fSFrançois Tigeot 
646*a85cb24fSFrançois Tigeot 	if (scan->alignment) {
647*a85cb24fSFrançois Tigeot 		u64 rem;
648*a85cb24fSFrançois Tigeot 
649*a85cb24fSFrançois Tigeot 		if (likely(scan->remainder_mask))
650*a85cb24fSFrançois Tigeot 			rem = adj_start & scan->remainder_mask;
651*a85cb24fSFrançois Tigeot 		else
652*a85cb24fSFrançois Tigeot 			div64_u64_rem(adj_start, scan->alignment, &rem);
653*a85cb24fSFrançois Tigeot 		if (rem) {
654*a85cb24fSFrançois Tigeot 			adj_start -= rem;
655*a85cb24fSFrançois Tigeot 			if (scan->flags != DRM_MM_CREATE_TOP)
656*a85cb24fSFrançois Tigeot 				adj_start += scan->alignment;
657*a85cb24fSFrançois Tigeot 			if (adj_start < max(col_start, scan->range_start) ||
658*a85cb24fSFrançois Tigeot 			    min(col_end, scan->range_end) - adj_start < scan->size)
659*a85cb24fSFrançois Tigeot 				return false;
660*a85cb24fSFrançois Tigeot 
661*a85cb24fSFrançois Tigeot 			if (adj_end <= adj_start ||
662*a85cb24fSFrançois Tigeot 			    adj_end - adj_start < scan->size)
663*a85cb24fSFrançois Tigeot 				return false;
664*a85cb24fSFrançois Tigeot 		}
6655718399fSFrançois Tigeot 	}
6665718399fSFrançois Tigeot 
667*a85cb24fSFrançois Tigeot 	scan->hit_start = adj_start;
668*a85cb24fSFrançois Tigeot 	scan->hit_end = adj_start + scan->size;
669*a85cb24fSFrançois Tigeot 
670*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(scan->hit_start >= scan->hit_end);
671*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(scan->hit_start < hole_start);
672*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(scan->hit_end > hole_end);
673*a85cb24fSFrançois Tigeot 
674*a85cb24fSFrançois Tigeot 	return true;
6755718399fSFrançois Tigeot }
676ea132f0fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_scan_add_block);
6775718399fSFrançois Tigeot 
678ea132f0fSFrançois Tigeot /**
679ba55f2f5SFrançois Tigeot  * drm_mm_scan_remove_block - remove a node from the scan list
680*a85cb24fSFrançois Tigeot  * @scan: the active drm_mm scanner
681ba55f2f5SFrançois Tigeot  * @node: drm_mm_node to remove
682ea132f0fSFrançois Tigeot  *
683*a85cb24fSFrançois Tigeot  * Nodes **must** be removed in exactly the reverse order from the scan list as
684*a85cb24fSFrançois Tigeot  * they have been added (e.g. using list_add() as they are added and then
685*a85cb24fSFrançois Tigeot  * list_for_each() over that eviction list to remove), otherwise the internal
686*a85cb24fSFrançois Tigeot  * state of the memory manager will be corrupted.
687ea132f0fSFrançois Tigeot  *
688ea132f0fSFrançois Tigeot  * When the scan list is empty, the selected memory nodes can be freed. An
689*a85cb24fSFrançois Tigeot  * immediately following drm_mm_insert_node_in_range_generic() or one of the
690*a85cb24fSFrançois Tigeot  * simpler versions of that function with !DRM_MM_SEARCH_BEST will then return
691*a85cb24fSFrançois Tigeot  * the just freed block (because its at the top of the free_stack list).
692ea132f0fSFrançois Tigeot  *
693ba55f2f5SFrançois Tigeot  * Returns:
694ba55f2f5SFrançois Tigeot  * True if this block should be evicted, false otherwise. Will always
695ba55f2f5SFrançois Tigeot  * return false when no hole has been found.
696ea132f0fSFrançois Tigeot  */
drm_mm_scan_remove_block(struct drm_mm_scan * scan,struct drm_mm_node * node)697*a85cb24fSFrançois Tigeot bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
698*a85cb24fSFrançois Tigeot 			      struct drm_mm_node *node)
6995718399fSFrançois Tigeot {
7005718399fSFrançois Tigeot 	struct drm_mm_node *prev_node;
7015718399fSFrançois Tigeot 
702*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(node->mm != scan->mm);
703*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(!node->scanned_block);
704*a85cb24fSFrançois Tigeot 	node->scanned_block = false;
7055718399fSFrançois Tigeot 
706*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(!node->mm->scan_active);
707*a85cb24fSFrançois Tigeot 	node->mm->scan_active--;
7085718399fSFrançois Tigeot 
709*a85cb24fSFrançois Tigeot 	/* During drm_mm_scan_add_block() we decoupled this node leaving
710*a85cb24fSFrançois Tigeot 	 * its pointers intact. Now that the caller is walking back along
711*a85cb24fSFrançois Tigeot 	 * the eviction list we can restore this block into its rightful
712*a85cb24fSFrançois Tigeot 	 * place on the full node_list. To confirm that the caller is walking
713*a85cb24fSFrançois Tigeot 	 * backwards correctly we check that prev_node->next == node->next,
714*a85cb24fSFrançois Tigeot 	 * i.e. both believe the same node should be on the other side of the
715*a85cb24fSFrançois Tigeot 	 * hole.
716*a85cb24fSFrançois Tigeot 	 */
717*a85cb24fSFrançois Tigeot 	prev_node = list_prev_entry(node, node_list);
718*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(list_next_entry(prev_node, node_list) !=
719*a85cb24fSFrançois Tigeot 		      list_next_entry(node, node_list));
7205718399fSFrançois Tigeot 	list_add(&node->node_list, &prev_node->node_list);
7215718399fSFrançois Tigeot 
722*a85cb24fSFrançois Tigeot 	return (node->start + node->size > scan->hit_start &&
723*a85cb24fSFrançois Tigeot 		node->start < scan->hit_end);
7245718399fSFrançois Tigeot }
725ea132f0fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_scan_remove_block);
7265718399fSFrançois Tigeot 
727ba55f2f5SFrançois Tigeot /**
728*a85cb24fSFrançois Tigeot  * drm_mm_scan_color_evict - evict overlapping nodes on either side of hole
729*a85cb24fSFrançois Tigeot  * @scan: drm_mm scan with target hole
730*a85cb24fSFrançois Tigeot  *
731*a85cb24fSFrançois Tigeot  * After completing an eviction scan and removing the selected nodes, we may
732*a85cb24fSFrançois Tigeot  * need to remove a few more nodes from either side of the target hole if
733*a85cb24fSFrançois Tigeot  * mm.color_adjust is being used.
734ba55f2f5SFrançois Tigeot  *
735ba55f2f5SFrançois Tigeot  * Returns:
736*a85cb24fSFrançois Tigeot  * A node to evict, or NULL if there are no overlapping nodes.
737ba55f2f5SFrançois Tigeot  */
drm_mm_scan_color_evict(struct drm_mm_scan * scan)738*a85cb24fSFrançois Tigeot struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan)
7393f6063ccSDavid Shao {
740*a85cb24fSFrançois Tigeot 	struct drm_mm *mm = scan->mm;
741*a85cb24fSFrançois Tigeot 	struct drm_mm_node *hole;
742*a85cb24fSFrançois Tigeot 	u64 hole_start, hole_end;
7433f6063ccSDavid Shao 
744*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(list_empty(&mm->hole_stack));
745*a85cb24fSFrançois Tigeot 
746*a85cb24fSFrançois Tigeot 	if (!mm->color_adjust)
747*a85cb24fSFrançois Tigeot 		return NULL;
748*a85cb24fSFrançois Tigeot 
749*a85cb24fSFrançois Tigeot 	hole = list_first_entry(&mm->hole_stack, typeof(*hole), hole_stack);
750*a85cb24fSFrançois Tigeot 	hole_start = __drm_mm_hole_node_start(hole);
751*a85cb24fSFrançois Tigeot 	hole_end = __drm_mm_hole_node_end(hole);
752*a85cb24fSFrançois Tigeot 
753*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(hole_start > scan->hit_start);
754*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(hole_end < scan->hit_end);
755*a85cb24fSFrançois Tigeot 
756*a85cb24fSFrançois Tigeot 	mm->color_adjust(hole, scan->color, &hole_start, &hole_end);
757*a85cb24fSFrançois Tigeot 	if (hole_start > scan->hit_start)
758*a85cb24fSFrançois Tigeot 		return hole;
759*a85cb24fSFrançois Tigeot 	if (hole_end < scan->hit_end)
760*a85cb24fSFrançois Tigeot 		return list_next_entry(hole, node_list);
761*a85cb24fSFrançois Tigeot 
762*a85cb24fSFrançois Tigeot 	return NULL;
7633f6063ccSDavid Shao }
764*a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_scan_color_evict);
7653f6063ccSDavid Shao 
766ba55f2f5SFrançois Tigeot /**
767ba55f2f5SFrançois Tigeot  * drm_mm_init - initialize a drm-mm allocator
768ba55f2f5SFrançois Tigeot  * @mm: the drm_mm structure to initialize
769ba55f2f5SFrançois Tigeot  * @start: start of the range managed by @mm
770ba55f2f5SFrançois Tigeot  * @size: end of the range managed by @mm
771ba55f2f5SFrançois Tigeot  *
772ba55f2f5SFrançois Tigeot  * Note that @mm must be cleared to 0 before calling this function.
773ba55f2f5SFrançois Tigeot  */
drm_mm_init(struct drm_mm * mm,u64 start,u64 size)774f77dbd6cSFrançois Tigeot void drm_mm_init(struct drm_mm *mm, u64 start, u64 size)
7753f6063ccSDavid Shao {
776*a85cb24fSFrançois Tigeot 	DRM_MM_BUG_ON(start + size <= start);
777*a85cb24fSFrançois Tigeot 
7785718399fSFrançois Tigeot 	INIT_LIST_HEAD(&mm->hole_stack);
779*a85cb24fSFrançois Tigeot 	mm->scan_active = 0;
7803f6063ccSDavid Shao 
781ea132f0fSFrançois Tigeot 	/* Clever trick to avoid a special case in the free hole tracking. */
7825718399fSFrançois Tigeot 	INIT_LIST_HEAD(&mm->head_node.node_list);
783*a85cb24fSFrançois Tigeot 	INIT_LIST_HEAD(&mm->head_node.hole_stack);
7844be47400SFrançois Tigeot 	mm->head_node.allocated = 0;
7855718399fSFrançois Tigeot 	mm->head_node.hole_follows = 1;
7865718399fSFrançois Tigeot 	mm->head_node.mm = mm;
7875718399fSFrançois Tigeot 	mm->head_node.start = start + size;
7885718399fSFrançois Tigeot 	mm->head_node.size = start - mm->head_node.start;
7895718399fSFrançois Tigeot 	list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
7905718399fSFrançois Tigeot 
791ea132f0fSFrançois Tigeot 	mm->color_adjust = NULL;
7923f6063ccSDavid Shao }
793ea132f0fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_init);
7943f6063ccSDavid Shao 
795ba55f2f5SFrançois Tigeot /**
796ba55f2f5SFrançois Tigeot  * drm_mm_takedown - clean up a drm_mm allocator
797ba55f2f5SFrançois Tigeot  * @mm: drm_mm allocator to clean up
798ba55f2f5SFrançois Tigeot  *
799ba55f2f5SFrançois Tigeot  * Note that it is a bug to call this function on an allocator which is not
800ba55f2f5SFrançois Tigeot  * clean.
801ba55f2f5SFrançois Tigeot  */
drm_mm_takedown(struct drm_mm * mm)8023f6063ccSDavid Shao void drm_mm_takedown(struct drm_mm *mm)
8033f6063ccSDavid Shao {
804*a85cb24fSFrançois Tigeot 	if (WARN(!drm_mm_clean(mm),
8054be47400SFrançois Tigeot 		 "Memory manager not clean during takedown.\n"))
8064be47400SFrançois Tigeot 		show_leaks(mm);
8073f6063ccSDavid Shao }
808ea132f0fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_takedown);
8095718399fSFrançois Tigeot 
drm_mm_dump_hole(struct drm_printer * p,struct drm_mm_node * entry)810*a85cb24fSFrançois Tigeot static u64 drm_mm_dump_hole(struct drm_printer *p, struct drm_mm_node *entry)
8115718399fSFrançois Tigeot {
812f77dbd6cSFrançois Tigeot 	u64 hole_start, hole_end, hole_size;
8135718399fSFrançois Tigeot 
8145718399fSFrançois Tigeot 	if (entry->hole_follows) {
8155718399fSFrançois Tigeot 		hole_start = drm_mm_hole_node_start(entry);
8165718399fSFrançois Tigeot 		hole_end = drm_mm_hole_node_end(entry);
8175718399fSFrançois Tigeot 		hole_size = hole_end - hole_start;
818*a85cb24fSFrançois Tigeot 		drm_printf(p, "%#018llx-%#018llx: %llu: free\n", hole_start,
819f77dbd6cSFrançois Tigeot 			   hole_end, hole_size);
820ba55f2f5SFrançois Tigeot 		return hole_size;
8215718399fSFrançois Tigeot 	}
822ba55f2f5SFrançois Tigeot 
823ba55f2f5SFrançois Tigeot 	return 0;
824ba55f2f5SFrançois Tigeot }
825ba55f2f5SFrançois Tigeot 
826ba55f2f5SFrançois Tigeot /**
827*a85cb24fSFrançois Tigeot  * drm_mm_print - print allocator state
828*a85cb24fSFrançois Tigeot  * @mm: drm_mm allocator to print
829*a85cb24fSFrançois Tigeot  * @p: DRM printer to use
830ba55f2f5SFrançois Tigeot  */
drm_mm_print(struct drm_mm * mm,struct drm_printer * p)831*a85cb24fSFrançois Tigeot void drm_mm_print(struct drm_mm *mm, struct drm_printer *p)
832ba55f2f5SFrançois Tigeot {
833ba55f2f5SFrançois Tigeot 	struct drm_mm_node *entry;
834f77dbd6cSFrançois Tigeot 	u64 total_used = 0, total_free = 0, total = 0;
835ba55f2f5SFrançois Tigeot 
836*a85cb24fSFrançois Tigeot 	total_free += drm_mm_dump_hole(p, &mm->head_node);
837ba55f2f5SFrançois Tigeot 
838ba55f2f5SFrançois Tigeot 	drm_mm_for_each_node(entry, mm) {
839*a85cb24fSFrançois Tigeot 		drm_printf(p, "%#018llx-%#018llx: %llu: used\n", entry->start,
840f77dbd6cSFrançois Tigeot 			   entry->start + entry->size, entry->size);
841ba55f2f5SFrançois Tigeot 		total_used += entry->size;
842*a85cb24fSFrançois Tigeot 		total_free += drm_mm_dump_hole(p, entry);
8435718399fSFrançois Tigeot 	}
8445718399fSFrançois Tigeot 	total = total_free + total_used;
8455718399fSFrançois Tigeot 
846*a85cb24fSFrançois Tigeot 	drm_printf(p, "total: %llu, used %llu free %llu\n", total,
8475718399fSFrançois Tigeot 		   total_used, total_free);
8485718399fSFrançois Tigeot }
849*a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_mm_print);
850