xref: /openbmc/linux/include/drm/drm_mm.h (revision bbba9693)
1249d6048SJerome Glisse /**************************************************************************
2249d6048SJerome Glisse  *
3249d6048SJerome Glisse  * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
4ba004e39SChris Wilson  * Copyright 2016 Intel Corporation
5249d6048SJerome Glisse  * All Rights Reserved.
6249d6048SJerome Glisse  *
7249d6048SJerome Glisse  * Permission is hereby granted, free of charge, to any person obtaining a
8249d6048SJerome Glisse  * copy of this software and associated documentation files (the
9249d6048SJerome Glisse  * "Software"), to deal in the Software without restriction, including
10249d6048SJerome Glisse  * without limitation the rights to use, copy, modify, merge, publish,
11249d6048SJerome Glisse  * distribute, sub license, and/or sell copies of the Software, and to
12249d6048SJerome Glisse  * permit persons to whom the Software is furnished to do so, subject to
13249d6048SJerome Glisse  * the following conditions:
14249d6048SJerome Glisse  *
15249d6048SJerome Glisse  * The above copyright notice and this permission notice (including the
16249d6048SJerome Glisse  * next paragraph) shall be included in all copies or substantial portions
17249d6048SJerome Glisse  * of the Software.
18249d6048SJerome Glisse  *
19249d6048SJerome Glisse  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20249d6048SJerome Glisse  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21249d6048SJerome Glisse  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22249d6048SJerome Glisse  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23249d6048SJerome Glisse  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24249d6048SJerome Glisse  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25249d6048SJerome Glisse  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26249d6048SJerome Glisse  *
27249d6048SJerome Glisse  *
28249d6048SJerome Glisse  **************************************************************************/
29249d6048SJerome Glisse /*
30249d6048SJerome Glisse  * Authors:
31249d6048SJerome Glisse  * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32249d6048SJerome Glisse  */
33249d6048SJerome Glisse 
34249d6048SJerome Glisse #ifndef _DRM_MM_H_
35249d6048SJerome Glisse #define _DRM_MM_H_
36249d6048SJerome Glisse 
37249d6048SJerome Glisse /*
38249d6048SJerome Glisse  * Generic range manager structs
39249d6048SJerome Glisse  */
4086e81f0eSDavid Herrmann #include <linux/bug.h>
41202b52b7SChris Wilson #include <linux/rbtree.h>
4286e81f0eSDavid Herrmann #include <linux/kernel.h>
43249d6048SJerome Glisse #include <linux/list.h>
4486e81f0eSDavid Herrmann #include <linux/spinlock.h>
455705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM
465705670dSChris Wilson #include <linux/stackdepot.h>
475705670dSChris Wilson #endif
48b5c3714fSDaniel Vetter #include <drm/drm_print.h>
49249d6048SJerome Glisse 
50b3ee963fSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM
51b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUG_ON(expr)
52b3ee963fSChris Wilson #else
53b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
54b3ee963fSChris Wilson #endif
55b3ee963fSChris Wilson 
564e64e553SChris Wilson /**
574e64e553SChris Wilson  * enum drm_mm_insert_mode - control search and allocation behaviour
584e64e553SChris Wilson  *
594e64e553SChris Wilson  * The &struct drm_mm range manager supports finding a suitable modes using
604e64e553SChris Wilson  * a number of search trees. These trees are oranised by size, by address and
614e64e553SChris Wilson  * in most recent eviction order. This allows the user to find either the
624e64e553SChris Wilson  * smallest hole to reuse, the lowest or highest address to reuse, or simply
634e64e553SChris Wilson  * reuse the most recent eviction that fits. When allocating the &drm_mm_node
644e64e553SChris Wilson  * from within the hole, the &drm_mm_insert_mode also dictate whether to
654e64e553SChris Wilson  * allocate the lowest matching address or the highest.
664e64e553SChris Wilson  */
674e64e553SChris Wilson enum drm_mm_insert_mode {
684e64e553SChris Wilson 	/**
694e64e553SChris Wilson 	 * @DRM_MM_INSERT_BEST:
704e64e553SChris Wilson 	 *
714e64e553SChris Wilson 	 * Search for the smallest hole (within the search range) that fits
724e64e553SChris Wilson 	 * the desired node.
734e64e553SChris Wilson 	 *
744e64e553SChris Wilson 	 * Allocates the node from the bottom of the found hole.
754e64e553SChris Wilson 	 */
764e64e553SChris Wilson 	DRM_MM_INSERT_BEST = 0,
7731e5d7c6SDavid Herrmann 
784e64e553SChris Wilson 	/**
794e64e553SChris Wilson 	 * @DRM_MM_INSERT_LOW:
804e64e553SChris Wilson 	 *
814e64e553SChris Wilson 	 * Search for the lowest hole (address closest to 0, within the search
824e64e553SChris Wilson 	 * range) that fits the desired node.
834e64e553SChris Wilson 	 *
844e64e553SChris Wilson 	 * Allocates the node from the bottom of the found hole.
854e64e553SChris Wilson 	 */
864e64e553SChris Wilson 	DRM_MM_INSERT_LOW,
8762347f9eSLauri Kasanen 
884e64e553SChris Wilson 	/**
894e64e553SChris Wilson 	 * @DRM_MM_INSERT_HIGH:
904e64e553SChris Wilson 	 *
914e64e553SChris Wilson 	 * Search for the highest hole (address closest to U64_MAX, within the
924e64e553SChris Wilson 	 * search range) that fits the desired node.
934e64e553SChris Wilson 	 *
944e64e553SChris Wilson 	 * Allocates the node from the *top* of the found hole. The specified
954e64e553SChris Wilson 	 * alignment for the node is applied to the base of the node
964e64e553SChris Wilson 	 * (&drm_mm_node.start).
974e64e553SChris Wilson 	 */
984e64e553SChris Wilson 	DRM_MM_INSERT_HIGH,
994e64e553SChris Wilson 
1004e64e553SChris Wilson 	/**
1014e64e553SChris Wilson 	 * @DRM_MM_INSERT_EVICT:
1024e64e553SChris Wilson 	 *
1034e64e553SChris Wilson 	 * Search for the most recently evicted hole (within the search range)
1044e64e553SChris Wilson 	 * that fits the desired node. This is appropriate for use immediately
1054e64e553SChris Wilson 	 * after performing an eviction scan (see drm_mm_scan_init()) and
1064e64e553SChris Wilson 	 * removing the selected nodes to form a hole.
1074e64e553SChris Wilson 	 *
1084e64e553SChris Wilson 	 * Allocates the node from the bottom of the found hole.
1094e64e553SChris Wilson 	 */
1104e64e553SChris Wilson 	DRM_MM_INSERT_EVICT,
1114e64e553SChris Wilson };
11262347f9eSLauri Kasanen 
11305fc0321SDaniel Vetter /**
11405fc0321SDaniel Vetter  * struct drm_mm_node - allocated block in the DRM allocator
11505fc0321SDaniel Vetter  *
11605fc0321SDaniel Vetter  * This represents an allocated block in a &drm_mm allocator. Except for
11705fc0321SDaniel Vetter  * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is
11805fc0321SDaniel Vetter  * entirely opaque and should only be accessed through the provided funcions.
11905fc0321SDaniel Vetter  * Since allocation of these nodes is entirely handled by the driver they can be
12005fc0321SDaniel Vetter  * embedded.
12105fc0321SDaniel Vetter  */
122249d6048SJerome Glisse struct drm_mm_node {
12305fc0321SDaniel Vetter 	/** @color: Opaque driver-private tag. */
12405fc0321SDaniel Vetter 	unsigned long color;
12505fc0321SDaniel Vetter 	/** @start: Start address of the allocated block. */
12605fc0321SDaniel Vetter 	u64 start;
12705fc0321SDaniel Vetter 	/** @size: Size of the allocated block. */
12805fc0321SDaniel Vetter 	u64 size;
12905fc0321SDaniel Vetter 	/* private: */
1304e64e553SChris Wilson 	struct drm_mm *mm;
131d1024ce9SDaniel Vetter 	struct list_head node_list;
132ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
133202b52b7SChris Wilson 	struct rb_node rb;
1344e64e553SChris Wilson 	struct rb_node rb_hole_size;
1354e64e553SChris Wilson 	struct rb_node rb_hole_addr;
136202b52b7SChris Wilson 	u64 __subtree_last;
1374e64e553SChris Wilson 	u64 hole_size;
1384e64e553SChris Wilson 	bool allocated : 1;
1394e64e553SChris Wilson 	bool scanned_block : 1;
1405705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM
1415705670dSChris Wilson 	depot_stack_handle_t stack;
1425705670dSChris Wilson #endif
143249d6048SJerome Glisse };
144249d6048SJerome Glisse 
14505fc0321SDaniel Vetter /**
14605fc0321SDaniel Vetter  * struct drm_mm - DRM allocator
14705fc0321SDaniel Vetter  *
14805fc0321SDaniel Vetter  * DRM range allocator with a few special functions and features geared towards
14905fc0321SDaniel Vetter  * managing GPU memory. Except for the @color_adjust callback the structure is
15005fc0321SDaniel Vetter  * entirely opaque and should only be accessed through the provided functions
15105fc0321SDaniel Vetter  * and macros. This structure can be embedded into larger driver structures.
15205fc0321SDaniel Vetter  */
153249d6048SJerome Glisse struct drm_mm {
15405fc0321SDaniel Vetter 	/**
15505fc0321SDaniel Vetter 	 * @color_adjust:
15605fc0321SDaniel Vetter 	 *
15705fc0321SDaniel Vetter 	 * Optional driver callback to further apply restrictions on a hole. The
15805fc0321SDaniel Vetter 	 * node argument points at the node containing the hole from which the
15905fc0321SDaniel Vetter 	 * block would be allocated (see drm_mm_hole_follows() and friends). The
16005fc0321SDaniel Vetter 	 * other arguments are the size of the block to be allocated. The driver
16105fc0321SDaniel Vetter 	 * can adjust the start and end as needed to e.g. insert guard pages.
16205fc0321SDaniel Vetter 	 */
16305fc0321SDaniel Vetter 	void (*color_adjust)(const struct drm_mm_node *node,
16405fc0321SDaniel Vetter 			     unsigned long color,
16505fc0321SDaniel Vetter 			     u64 *start, u64 *end);
16605fc0321SDaniel Vetter 
16705fc0321SDaniel Vetter 	/* private: */
16825985edcSLucas De Marchi 	/* List of all memory nodes that immediately precede a free hole. */
169ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
170ea7b1dd4SDaniel Vetter 	/* head_node.node_list is the list of all memory nodes, ordered
171ea7b1dd4SDaniel Vetter 	 * according to the (increasing) start address of the memory node. */
172ea7b1dd4SDaniel Vetter 	struct drm_mm_node head_node;
173202b52b7SChris Wilson 	/* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
174202b52b7SChris Wilson 	struct rb_root interval_tree;
1754e64e553SChris Wilson 	struct rb_root holes_size;
1764e64e553SChris Wilson 	struct rb_root holes_addr;
177202b52b7SChris Wilson 
1789a71e277SChris Wilson 	unsigned long scan_active;
1799a71e277SChris Wilson };
1809a71e277SChris Wilson 
18105fc0321SDaniel Vetter /**
18205fc0321SDaniel Vetter  * struct drm_mm_scan - DRM allocator eviction roaster data
18305fc0321SDaniel Vetter  *
18405fc0321SDaniel Vetter  * This structure tracks data needed for the eviction roaster set up using
18505fc0321SDaniel Vetter  * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and
18605fc0321SDaniel Vetter  * drm_mm_scan_remove_block(). The structure is entirely opaque and should only
18705fc0321SDaniel Vetter  * be accessed through the provided functions and macros. It is meant to be
18805fc0321SDaniel Vetter  * allocated temporarily by the driver on the stack.
18905fc0321SDaniel Vetter  */
1909a71e277SChris Wilson struct drm_mm_scan {
19105fc0321SDaniel Vetter 	/* private: */
1929a71e277SChris Wilson 	struct drm_mm *mm;
1939a71e277SChris Wilson 
1949a71e277SChris Wilson 	u64 size;
1959a71e277SChris Wilson 	u64 alignment;
1969a956b15SChris Wilson 	u64 remainder_mask;
1979a71e277SChris Wilson 
1989a71e277SChris Wilson 	u64 range_start;
1999a71e277SChris Wilson 	u64 range_end;
2009a71e277SChris Wilson 
2019a71e277SChris Wilson 	u64 hit_start;
2029a71e277SChris Wilson 	u64 hit_end;
2039a71e277SChris Wilson 
2049a71e277SChris Wilson 	unsigned long color;
2054e64e553SChris Wilson 	enum drm_mm_insert_mode mode;
206249d6048SJerome Glisse };
207249d6048SJerome Glisse 
208e18c0412SDaniel Vetter /**
209e18c0412SDaniel Vetter  * drm_mm_node_allocated - checks whether a node is allocated
210e18c0412SDaniel Vetter  * @node: drm_mm_node to check
211e18c0412SDaniel Vetter  *
212ba004e39SChris Wilson  * Drivers are required to clear a node prior to using it with the
213ba004e39SChris Wilson  * drm_mm range manager.
214ba004e39SChris Wilson  *
215ba004e39SChris Wilson  * Drivers should use this helper for proper encapsulation of drm_mm
216e18c0412SDaniel Vetter  * internals.
217e18c0412SDaniel Vetter  *
218e18c0412SDaniel Vetter  * Returns:
219e18c0412SDaniel Vetter  * True if the @node is allocated.
220e18c0412SDaniel Vetter  */
22145b186f1SChris Wilson static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
222b0b7af18SDaniel Vetter {
223b0b7af18SDaniel Vetter 	return node->allocated;
224b0b7af18SDaniel Vetter }
225b0b7af18SDaniel Vetter 
226e18c0412SDaniel Vetter /**
227e18c0412SDaniel Vetter  * drm_mm_initialized - checks whether an allocator is initialized
228e18c0412SDaniel Vetter  * @mm: drm_mm to check
229e18c0412SDaniel Vetter  *
230ba004e39SChris Wilson  * Drivers should clear the struct drm_mm prior to initialisation if they
231ba004e39SChris Wilson  * want to use this function.
232ba004e39SChris Wilson  *
233ba004e39SChris Wilson  * Drivers should use this helper for proper encapsulation of drm_mm
234e18c0412SDaniel Vetter  * internals.
235e18c0412SDaniel Vetter  *
236e18c0412SDaniel Vetter  * Returns:
237e18c0412SDaniel Vetter  * True if the @mm is initialized.
238e18c0412SDaniel Vetter  */
23945b186f1SChris Wilson static inline bool drm_mm_initialized(const struct drm_mm *mm)
24031a5b8ceSDaniel Vetter {
241ea7b1dd4SDaniel Vetter 	return mm->hole_stack.next;
24231a5b8ceSDaniel Vetter }
2439e8944abSChris Wilson 
2443f85fb34SChris Wilson /**
2453f85fb34SChris Wilson  * drm_mm_hole_follows - checks whether a hole follows this node
2463f85fb34SChris Wilson  * @node: drm_mm_node to check
2473f85fb34SChris Wilson  *
2483f85fb34SChris Wilson  * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
2493f85fb34SChris Wilson  * If you wish to know whether a hole follows this particular node,
25005fc0321SDaniel Vetter  * query this function. See also drm_mm_hole_node_start() and
25105fc0321SDaniel Vetter  * drm_mm_hole_node_end().
2523f85fb34SChris Wilson  *
2533f85fb34SChris Wilson  * Returns:
2543f85fb34SChris Wilson  * True if a hole follows the @node.
2553f85fb34SChris Wilson  */
2563f85fb34SChris Wilson static inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
2573f85fb34SChris Wilson {
2584e64e553SChris Wilson 	return node->hole_size;
2593f85fb34SChris Wilson }
2603f85fb34SChris Wilson 
26145b186f1SChris Wilson static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
2629e8944abSChris Wilson {
2639e8944abSChris Wilson 	return hole_node->start + hole_node->size;
2649e8944abSChris Wilson }
2659e8944abSChris Wilson 
266e18c0412SDaniel Vetter /**
267e18c0412SDaniel Vetter  * drm_mm_hole_node_start - computes the start of the hole following @node
268e18c0412SDaniel Vetter  * @hole_node: drm_mm_node which implicitly tracks the following hole
269e18c0412SDaniel Vetter  *
270ba004e39SChris Wilson  * This is useful for driver-specific debug dumpers. Otherwise drivers should
271ba004e39SChris Wilson  * not inspect holes themselves. Drivers must check first whether a hole indeed
2723f85fb34SChris Wilson  * follows by looking at drm_mm_hole_follows()
273e18c0412SDaniel Vetter  *
274e18c0412SDaniel Vetter  * Returns:
275e18c0412SDaniel Vetter  * Start of the subsequent hole.
276e18c0412SDaniel Vetter  */
27745b186f1SChris Wilson static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
2789e8944abSChris Wilson {
2793f85fb34SChris Wilson 	DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
2809e8944abSChris Wilson 	return __drm_mm_hole_node_start(hole_node);
2819e8944abSChris Wilson }
2829e8944abSChris Wilson 
28345b186f1SChris Wilson static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
2849e8944abSChris Wilson {
28587069f44SGeliang Tang 	return list_next_entry(hole_node, node_list)->start;
2869e8944abSChris Wilson }
2879e8944abSChris Wilson 
288e18c0412SDaniel Vetter /**
289e18c0412SDaniel Vetter  * drm_mm_hole_node_end - computes the end of the hole following @node
290e18c0412SDaniel Vetter  * @hole_node: drm_mm_node which implicitly tracks the following hole
291e18c0412SDaniel Vetter  *
292ba004e39SChris Wilson  * This is useful for driver-specific debug dumpers. Otherwise drivers should
293ba004e39SChris Wilson  * not inspect holes themselves. Drivers must check first whether a hole indeed
2943f85fb34SChris Wilson  * follows by looking at drm_mm_hole_follows().
295e18c0412SDaniel Vetter  *
296e18c0412SDaniel Vetter  * Returns:
297e18c0412SDaniel Vetter  * End of the subsequent hole.
298e18c0412SDaniel Vetter  */
29945b186f1SChris Wilson static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
3009e8944abSChris Wilson {
3019e8944abSChris Wilson 	return __drm_mm_hole_node_end(hole_node);
3029e8944abSChris Wilson }
3039e8944abSChris Wilson 
3042bc98c86SChris Wilson /**
3052bc98c86SChris Wilson  * drm_mm_nodes - list of nodes under the drm_mm range manager
3062bc98c86SChris Wilson  * @mm: the struct drm_mm range manger
3072bc98c86SChris Wilson  *
3082bc98c86SChris Wilson  * As the drm_mm range manager hides its node_list deep with its
3092bc98c86SChris Wilson  * structure, extracting it looks painful and repetitive. This is
3102bc98c86SChris Wilson  * not expected to be used outside of the drm_mm_for_each_node()
3112bc98c86SChris Wilson  * macros and similar internal functions.
3122bc98c86SChris Wilson  *
3132bc98c86SChris Wilson  * Returns:
3142bc98c86SChris Wilson  * The node list, may be empty.
3152bc98c86SChris Wilson  */
3162bc98c86SChris Wilson #define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
317ad579002SChris Wilson 
318e18c0412SDaniel Vetter /**
319e18c0412SDaniel Vetter  * drm_mm_for_each_node - iterator to walk over all allocated nodes
32005fc0321SDaniel Vetter  * @entry: &struct drm_mm_node to assign to in each iteration step
32105fc0321SDaniel Vetter  * @mm: &drm_mm allocator to walk
322e18c0412SDaniel Vetter  *
323e18c0412SDaniel Vetter  * This iterator walks over all nodes in the range allocator. It is implemented
32405fc0321SDaniel Vetter  * with list_for_each(), so not save against removal of elements.
325e18c0412SDaniel Vetter  */
326ad579002SChris Wilson #define drm_mm_for_each_node(entry, mm) \
3272bc98c86SChris Wilson 	list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
328ad579002SChris Wilson 
329ad579002SChris Wilson /**
330ad579002SChris Wilson  * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
33105fc0321SDaniel Vetter  * @entry: &struct drm_mm_node to assign to in each iteration step
33205fc0321SDaniel Vetter  * @next: &struct drm_mm_node to store the next step
33305fc0321SDaniel Vetter  * @mm: &drm_mm allocator to walk
334ad579002SChris Wilson  *
335ad579002SChris Wilson  * This iterator walks over all nodes in the range allocator. It is implemented
33605fc0321SDaniel Vetter  * with list_for_each_safe(), so save against removal of elements.
337ad579002SChris Wilson  */
338ad579002SChris Wilson #define drm_mm_for_each_node_safe(entry, next, mm) \
3392bc98c86SChris Wilson 	list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
3409e8944abSChris Wilson 
341e18c0412SDaniel Vetter /**
342e18c0412SDaniel Vetter  * drm_mm_for_each_hole - iterator to walk over all holes
3434e64e553SChris Wilson  * @pos: &drm_mm_node used internally to track progress
34405fc0321SDaniel Vetter  * @mm: &drm_mm allocator to walk
345e18c0412SDaniel Vetter  * @hole_start: ulong variable to assign the hole start to on each iteration
346e18c0412SDaniel Vetter  * @hole_end: ulong variable to assign the hole end to on each iteration
347e18c0412SDaniel Vetter  *
348e18c0412SDaniel Vetter  * This iterator walks over all holes in the range allocator. It is implemented
34905fc0321SDaniel Vetter  * with list_for_each(), so not save against removal of elements. @entry is used
350e18c0412SDaniel Vetter  * internally and will not reflect a real drm_mm_node for the very first hole.
351e18c0412SDaniel Vetter  * Hence users of this iterator may not access it.
352e18c0412SDaniel Vetter  *
353e18c0412SDaniel Vetter  * Implementation Note:
354e18c0412SDaniel Vetter  * We need to inline list_for_each_entry in order to be able to set hole_start
355e18c0412SDaniel Vetter  * and hole_end on each iteration while keeping the macro sane.
3569e8944abSChris Wilson  */
3574e64e553SChris Wilson #define drm_mm_for_each_hole(pos, mm, hole_start, hole_end) \
3584e64e553SChris Wilson 	for (pos = list_first_entry(&(mm)->hole_stack, \
3594e64e553SChris Wilson 				    typeof(*pos), hole_stack); \
3604e64e553SChris Wilson 	     &pos->hole_stack != &(mm)->hole_stack ? \
3614e64e553SChris Wilson 	     hole_start = drm_mm_hole_node_start(pos), \
3624e64e553SChris Wilson 	     hole_end = hole_start + pos->hole_size, \
3634e64e553SChris Wilson 	     1 : 0; \
3644e64e553SChris Wilson 	     pos = list_next_entry(pos, hole_stack))
36562347f9eSLauri Kasanen 
366249d6048SJerome Glisse /*
367249d6048SJerome Glisse  * Basic range manager support (drm_mm.c)
368249d6048SJerome Glisse  */
369e18c0412SDaniel Vetter int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
3704e64e553SChris Wilson int drm_mm_insert_node_in_range(struct drm_mm *mm,
371b8103450SChris Wilson 				struct drm_mm_node *node,
372440fd528SThierry Reding 				u64 size,
37371733207SChris Wilson 				u64 alignment,
374b8103450SChris Wilson 				unsigned long color,
375440fd528SThierry Reding 				u64 start,
376440fd528SThierry Reding 				u64 end,
3774e64e553SChris Wilson 				enum drm_mm_insert_mode mode);
37831e5d7c6SDavid Herrmann 
379adb040b8SChris Wilson /**
380adb040b8SChris Wilson  * drm_mm_insert_node_generic - search for space and insert @node
381adb040b8SChris Wilson  * @mm: drm_mm to allocate from
382adb040b8SChris Wilson  * @node: preallocate node to insert
383adb040b8SChris Wilson  * @size: size of the allocation
384adb040b8SChris Wilson  * @alignment: alignment of the allocation
385adb040b8SChris Wilson  * @color: opaque tag value to use for this node
3864e64e553SChris Wilson  * @mode: fine-tune the allocation search and placement
387adb040b8SChris Wilson  *
38805fc0321SDaniel Vetter  * This is a simplified version of drm_mm_insert_node_in_range_generic() with no
38905fc0321SDaniel Vetter  * range restrictions applied.
39005fc0321SDaniel Vetter  *
391adb040b8SChris Wilson  * The preallocated node must be cleared to 0.
392adb040b8SChris Wilson  *
393adb040b8SChris Wilson  * Returns:
394adb040b8SChris Wilson  * 0 on success, -ENOSPC if there's no suitable hole.
395adb040b8SChris Wilson  */
396adb040b8SChris Wilson static inline int
397adb040b8SChris Wilson drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
398adb040b8SChris Wilson 			   u64 size, u64 alignment,
399adb040b8SChris Wilson 			   unsigned long color,
4004e64e553SChris Wilson 			   enum drm_mm_insert_mode mode)
401adb040b8SChris Wilson {
4024e64e553SChris Wilson 	return drm_mm_insert_node_in_range(mm, node,
4034e64e553SChris Wilson 					   size, alignment, color,
4044e64e553SChris Wilson 					   0, U64_MAX, mode);
405adb040b8SChris Wilson }
406adb040b8SChris Wilson 
407adb040b8SChris Wilson /**
408adb040b8SChris Wilson  * drm_mm_insert_node - search for space and insert @node
409adb040b8SChris Wilson  * @mm: drm_mm to allocate from
410adb040b8SChris Wilson  * @node: preallocate node to insert
411adb040b8SChris Wilson  * @size: size of the allocation
412adb040b8SChris Wilson  *
413adb040b8SChris Wilson  * This is a simplified version of drm_mm_insert_node_generic() with @color set
414adb040b8SChris Wilson  * to 0.
415adb040b8SChris Wilson  *
416adb040b8SChris Wilson  * The preallocated node must be cleared to 0.
417adb040b8SChris Wilson  *
418adb040b8SChris Wilson  * Returns:
419adb040b8SChris Wilson  * 0 on success, -ENOSPC if there's no suitable hole.
420adb040b8SChris Wilson  */
421adb040b8SChris Wilson static inline int drm_mm_insert_node(struct drm_mm *mm,
422adb040b8SChris Wilson 				     struct drm_mm_node *node,
4234e64e553SChris Wilson 				     u64 size)
424adb040b8SChris Wilson {
4254e64e553SChris Wilson 	return drm_mm_insert_node_generic(mm, node, size, 0, 0, 0);
426adb040b8SChris Wilson }
427adb040b8SChris Wilson 
428e18c0412SDaniel Vetter void drm_mm_remove_node(struct drm_mm_node *node);
429e18c0412SDaniel Vetter void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
43045b186f1SChris Wilson void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
431e18c0412SDaniel Vetter void drm_mm_takedown(struct drm_mm *mm);
432ac9bb7b7SChris Wilson 
433ac9bb7b7SChris Wilson /**
434ac9bb7b7SChris Wilson  * drm_mm_clean - checks whether an allocator is clean
435ac9bb7b7SChris Wilson  * @mm: drm_mm allocator to check
436ac9bb7b7SChris Wilson  *
437ac9bb7b7SChris Wilson  * Returns:
438ac9bb7b7SChris Wilson  * True if the allocator is completely free, false if there's still a node
439ac9bb7b7SChris Wilson  * allocated in it.
440ac9bb7b7SChris Wilson  */
441ac9bb7b7SChris Wilson static inline bool drm_mm_clean(const struct drm_mm *mm)
442ac9bb7b7SChris Wilson {
443ac9bb7b7SChris Wilson 	return list_empty(drm_mm_nodes(mm));
444ac9bb7b7SChris Wilson }
445249d6048SJerome Glisse 
446202b52b7SChris Wilson struct drm_mm_node *
44745b186f1SChris Wilson __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
448202b52b7SChris Wilson 
449522e85ddSChris Wilson /**
450522e85ddSChris Wilson  * drm_mm_for_each_node_in_range - iterator to walk over a range of
451522e85ddSChris Wilson  * allocated nodes
4528b2fb7b6SChris Wilson  * @node__: drm_mm_node structure to assign to in each iteration step
4538b2fb7b6SChris Wilson  * @mm__: drm_mm allocator to walk
4548b2fb7b6SChris Wilson  * @start__: starting offset, the first node will overlap this
4558b2fb7b6SChris Wilson  * @end__: ending offset, the last node will start before this (but may overlap)
456522e85ddSChris Wilson  *
457522e85ddSChris Wilson  * This iterator walks over all nodes in the range allocator that lie
458522e85ddSChris Wilson  * between @start and @end. It is implemented similarly to list_for_each(),
459522e85ddSChris Wilson  * but using the internal interval tree to accelerate the search for the
460522e85ddSChris Wilson  * starting node, and so not safe against removal of elements. It assumes
461522e85ddSChris Wilson  * that @end is within (or is the upper limit of) the drm_mm allocator.
462bbba9693SChris Wilson  * If [@start, @end] are beyond the range of the drm_mm, the iterator may walk
463bbba9693SChris Wilson  * over the special _unallocated_ &drm_mm.head_node, and may even continue
464bbba9693SChris Wilson  * indefinitely.
465522e85ddSChris Wilson  */
4668b2fb7b6SChris Wilson #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__)	\
4678b2fb7b6SChris Wilson 	for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
468bbba9693SChris Wilson 	     node__->start < (end__);					\
4698b2fb7b6SChris Wilson 	     node__ = list_next_entry(node__, node_list))
470202b52b7SChris Wilson 
4719a71e277SChris Wilson void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
4729a71e277SChris Wilson 				 struct drm_mm *mm,
4730b04d474SChris Wilson 				 u64 size, u64 alignment, unsigned long color,
4740b04d474SChris Wilson 				 u64 start, u64 end,
4754e64e553SChris Wilson 				 enum drm_mm_insert_mode mode);
4762c4b3895SChris Wilson 
4772c4b3895SChris Wilson /**
4782c4b3895SChris Wilson  * drm_mm_scan_init - initialize lru scanning
4792c4b3895SChris Wilson  * @scan: scan state
4802c4b3895SChris Wilson  * @mm: drm_mm to scan
4812c4b3895SChris Wilson  * @size: size of the allocation
4822c4b3895SChris Wilson  * @alignment: alignment of the allocation
4832c4b3895SChris Wilson  * @color: opaque tag value to use for the allocation
4844e64e553SChris Wilson  * @mode: fine-tune the allocation search and placement
4852c4b3895SChris Wilson  *
48605fc0321SDaniel Vetter  * This is a simplified version of drm_mm_scan_init_with_range() with no range
48705fc0321SDaniel Vetter  * restrictions applied.
48805fc0321SDaniel Vetter  *
4892c4b3895SChris Wilson  * This simply sets up the scanning routines with the parameters for the desired
4900b04d474SChris Wilson  * hole.
4912c4b3895SChris Wilson  *
4922c4b3895SChris Wilson  * Warning:
4932c4b3895SChris Wilson  * As long as the scan list is non-empty, no other operations than
4942c4b3895SChris Wilson  * adding/removing nodes to/from the scan list are allowed.
4952c4b3895SChris Wilson  */
4962c4b3895SChris Wilson static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
4972c4b3895SChris Wilson 				    struct drm_mm *mm,
4982c4b3895SChris Wilson 				    u64 size,
4992c4b3895SChris Wilson 				    u64 alignment,
5000b04d474SChris Wilson 				    unsigned long color,
5014e64e553SChris Wilson 				    enum drm_mm_insert_mode mode)
5022c4b3895SChris Wilson {
5030b04d474SChris Wilson 	drm_mm_scan_init_with_range(scan, mm,
5040b04d474SChris Wilson 				    size, alignment, color,
5054e64e553SChris Wilson 				    0, U64_MAX, mode);
5062c4b3895SChris Wilson }
5072c4b3895SChris Wilson 
5089a71e277SChris Wilson bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
5099a71e277SChris Wilson 			   struct drm_mm_node *node);
5109a71e277SChris Wilson bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
5119a71e277SChris Wilson 			      struct drm_mm_node *node);
5123fa489daSChris Wilson struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
513709ea971SDaniel Vetter 
514b5c3714fSDaniel Vetter void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p);
515fa8a1238SDave Airlie 
516249d6048SJerome Glisse #endif
517