xref: /openbmc/linux/include/drm/drm_mm.h (revision 3f85fb34)
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>
45f1938cd6SDave Airlie #ifdef CONFIG_DEBUG_FS
46f1938cd6SDave Airlie #include <linux/seq_file.h>
47f1938cd6SDave Airlie #endif
485705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM
495705670dSChris Wilson #include <linux/stackdepot.h>
505705670dSChris Wilson #endif
51249d6048SJerome Glisse 
52b3ee963fSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM
53b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUG_ON(expr)
54b3ee963fSChris Wilson #else
55b3ee963fSChris Wilson #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
56b3ee963fSChris Wilson #endif
57b3ee963fSChris Wilson 
5831e5d7c6SDavid Herrmann enum drm_mm_search_flags {
5931e5d7c6SDavid Herrmann 	DRM_MM_SEARCH_DEFAULT =		0,
6031e5d7c6SDavid Herrmann 	DRM_MM_SEARCH_BEST =		1 << 0,
6162347f9eSLauri Kasanen 	DRM_MM_SEARCH_BELOW =		1 << 1,
6231e5d7c6SDavid Herrmann };
6331e5d7c6SDavid Herrmann 
6462347f9eSLauri Kasanen enum drm_mm_allocator_flags {
6562347f9eSLauri Kasanen 	DRM_MM_CREATE_DEFAULT =		0,
6662347f9eSLauri Kasanen 	DRM_MM_CREATE_TOP =		1 << 0,
6762347f9eSLauri Kasanen };
6862347f9eSLauri Kasanen 
6962347f9eSLauri Kasanen #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT
7062347f9eSLauri Kasanen #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP
7162347f9eSLauri Kasanen 
72249d6048SJerome Glisse struct drm_mm_node {
73d1024ce9SDaniel Vetter 	struct list_head node_list;
74ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
75202b52b7SChris Wilson 	struct rb_node rb;
76ea7b1dd4SDaniel Vetter 	unsigned hole_follows : 1;
77b0b7af18SDaniel Vetter 	unsigned allocated : 1;
78f29051f1SChris Wilson 	bool scanned_block : 1;
796b9d89b4SChris Wilson 	unsigned long color;
80440fd528SThierry Reding 	u64 start;
81440fd528SThierry Reding 	u64 size;
82202b52b7SChris Wilson 	u64 __subtree_last;
83249d6048SJerome Glisse 	struct drm_mm *mm;
845705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM
855705670dSChris Wilson 	depot_stack_handle_t stack;
865705670dSChris Wilson #endif
87249d6048SJerome Glisse };
88249d6048SJerome Glisse 
89249d6048SJerome Glisse struct drm_mm {
9025985edcSLucas De Marchi 	/* List of all memory nodes that immediately precede a free hole. */
91ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
92ea7b1dd4SDaniel Vetter 	/* head_node.node_list is the list of all memory nodes, ordered
93ea7b1dd4SDaniel Vetter 	 * according to the (increasing) start address of the memory node. */
94ea7b1dd4SDaniel Vetter 	struct drm_mm_node head_node;
95202b52b7SChris Wilson 	/* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
96202b52b7SChris Wilson 	struct rb_root interval_tree;
97202b52b7SChris Wilson 
9845b186f1SChris Wilson 	void (*color_adjust)(const struct drm_mm_node *node,
9945b186f1SChris Wilson 			     unsigned long color,
100440fd528SThierry Reding 			     u64 *start, u64 *end);
1019a71e277SChris Wilson 
1029a71e277SChris Wilson 	unsigned long scan_active;
1039a71e277SChris Wilson };
1049a71e277SChris Wilson 
1059a71e277SChris Wilson struct drm_mm_scan {
1069a71e277SChris Wilson 	struct drm_mm *mm;
1079a71e277SChris Wilson 
1089a71e277SChris Wilson 	u64 size;
1099a71e277SChris Wilson 	u64 alignment;
1109a956b15SChris Wilson 	u64 remainder_mask;
1119a71e277SChris Wilson 
1129a71e277SChris Wilson 	u64 range_start;
1139a71e277SChris Wilson 	u64 range_end;
1149a71e277SChris Wilson 
1159a71e277SChris Wilson 	u64 hit_start;
1169a71e277SChris Wilson 	u64 hit_end;
1179a71e277SChris Wilson 
1189a71e277SChris Wilson 	unsigned long color;
1190b04d474SChris Wilson 	unsigned int flags;
120249d6048SJerome Glisse };
121249d6048SJerome Glisse 
122e18c0412SDaniel Vetter /**
123e18c0412SDaniel Vetter  * drm_mm_node_allocated - checks whether a node is allocated
124e18c0412SDaniel Vetter  * @node: drm_mm_node to check
125e18c0412SDaniel Vetter  *
126ba004e39SChris Wilson  * Drivers are required to clear a node prior to using it with the
127ba004e39SChris Wilson  * drm_mm range manager.
128ba004e39SChris Wilson  *
129ba004e39SChris Wilson  * Drivers should use this helper for proper encapsulation of drm_mm
130e18c0412SDaniel Vetter  * internals.
131e18c0412SDaniel Vetter  *
132e18c0412SDaniel Vetter  * Returns:
133e18c0412SDaniel Vetter  * True if the @node is allocated.
134e18c0412SDaniel Vetter  */
13545b186f1SChris Wilson static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
136b0b7af18SDaniel Vetter {
137b0b7af18SDaniel Vetter 	return node->allocated;
138b0b7af18SDaniel Vetter }
139b0b7af18SDaniel Vetter 
140e18c0412SDaniel Vetter /**
141e18c0412SDaniel Vetter  * drm_mm_initialized - checks whether an allocator is initialized
142e18c0412SDaniel Vetter  * @mm: drm_mm to check
143e18c0412SDaniel Vetter  *
144ba004e39SChris Wilson  * Drivers should clear the struct drm_mm prior to initialisation if they
145ba004e39SChris Wilson  * want to use this function.
146ba004e39SChris Wilson  *
147ba004e39SChris Wilson  * Drivers should use this helper for proper encapsulation of drm_mm
148e18c0412SDaniel Vetter  * internals.
149e18c0412SDaniel Vetter  *
150e18c0412SDaniel Vetter  * Returns:
151e18c0412SDaniel Vetter  * True if the @mm is initialized.
152e18c0412SDaniel Vetter  */
15345b186f1SChris Wilson static inline bool drm_mm_initialized(const struct drm_mm *mm)
15431a5b8ceSDaniel Vetter {
155ea7b1dd4SDaniel Vetter 	return mm->hole_stack.next;
15631a5b8ceSDaniel Vetter }
1579e8944abSChris Wilson 
1583f85fb34SChris Wilson /**
1593f85fb34SChris Wilson  * drm_mm_hole_follows - checks whether a hole follows this node
1603f85fb34SChris Wilson  * @node: drm_mm_node to check
1613f85fb34SChris Wilson  *
1623f85fb34SChris Wilson  * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
1633f85fb34SChris Wilson  * If you wish to know whether a hole follows this particular node,
1643f85fb34SChris Wilson  * query this function.
1653f85fb34SChris Wilson  *
1663f85fb34SChris Wilson  * Returns:
1673f85fb34SChris Wilson  * True if a hole follows the @node.
1683f85fb34SChris Wilson  */
1693f85fb34SChris Wilson static inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
1703f85fb34SChris Wilson {
1713f85fb34SChris Wilson 	return node->hole_follows;
1723f85fb34SChris Wilson }
1733f85fb34SChris Wilson 
17445b186f1SChris Wilson static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
1759e8944abSChris Wilson {
1769e8944abSChris Wilson 	return hole_node->start + hole_node->size;
1779e8944abSChris Wilson }
1789e8944abSChris Wilson 
179e18c0412SDaniel Vetter /**
180e18c0412SDaniel Vetter  * drm_mm_hole_node_start - computes the start of the hole following @node
181e18c0412SDaniel Vetter  * @hole_node: drm_mm_node which implicitly tracks the following hole
182e18c0412SDaniel Vetter  *
183ba004e39SChris Wilson  * This is useful for driver-specific debug dumpers. Otherwise drivers should
184ba004e39SChris Wilson  * not inspect holes themselves. Drivers must check first whether a hole indeed
1853f85fb34SChris Wilson  * follows by looking at drm_mm_hole_follows()
186e18c0412SDaniel Vetter  *
187e18c0412SDaniel Vetter  * Returns:
188e18c0412SDaniel Vetter  * Start of the subsequent hole.
189e18c0412SDaniel Vetter  */
19045b186f1SChris Wilson static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
1919e8944abSChris Wilson {
1923f85fb34SChris Wilson 	DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
1939e8944abSChris Wilson 	return __drm_mm_hole_node_start(hole_node);
1949e8944abSChris Wilson }
1959e8944abSChris Wilson 
19645b186f1SChris Wilson static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
1979e8944abSChris Wilson {
19887069f44SGeliang Tang 	return list_next_entry(hole_node, node_list)->start;
1999e8944abSChris Wilson }
2009e8944abSChris Wilson 
201e18c0412SDaniel Vetter /**
202e18c0412SDaniel Vetter  * drm_mm_hole_node_end - computes the end of the hole following @node
203e18c0412SDaniel Vetter  * @hole_node: drm_mm_node which implicitly tracks the following hole
204e18c0412SDaniel Vetter  *
205ba004e39SChris Wilson  * This is useful for driver-specific debug dumpers. Otherwise drivers should
206ba004e39SChris Wilson  * not inspect holes themselves. Drivers must check first whether a hole indeed
2073f85fb34SChris Wilson  * follows by looking at drm_mm_hole_follows().
208e18c0412SDaniel Vetter  *
209e18c0412SDaniel Vetter  * Returns:
210e18c0412SDaniel Vetter  * End of the subsequent hole.
211e18c0412SDaniel Vetter  */
21245b186f1SChris Wilson static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
2139e8944abSChris Wilson {
2149e8944abSChris Wilson 	return __drm_mm_hole_node_end(hole_node);
2159e8944abSChris Wilson }
2169e8944abSChris Wilson 
2172bc98c86SChris Wilson /**
2182bc98c86SChris Wilson  * drm_mm_nodes - list of nodes under the drm_mm range manager
2192bc98c86SChris Wilson  * @mm: the struct drm_mm range manger
2202bc98c86SChris Wilson  *
2212bc98c86SChris Wilson  * As the drm_mm range manager hides its node_list deep with its
2222bc98c86SChris Wilson  * structure, extracting it looks painful and repetitive. This is
2232bc98c86SChris Wilson  * not expected to be used outside of the drm_mm_for_each_node()
2242bc98c86SChris Wilson  * macros and similar internal functions.
2252bc98c86SChris Wilson  *
2262bc98c86SChris Wilson  * Returns:
2272bc98c86SChris Wilson  * The node list, may be empty.
2282bc98c86SChris Wilson  */
2292bc98c86SChris Wilson #define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
230ad579002SChris Wilson 
231e18c0412SDaniel Vetter /**
232e18c0412SDaniel Vetter  * drm_mm_for_each_node - iterator to walk over all allocated nodes
233e18c0412SDaniel Vetter  * @entry: drm_mm_node structure to assign to in each iteration step
234e18c0412SDaniel Vetter  * @mm: drm_mm allocator to walk
235e18c0412SDaniel Vetter  *
236e18c0412SDaniel Vetter  * This iterator walks over all nodes in the range allocator. It is implemented
237e18c0412SDaniel Vetter  * with list_for_each, so not save against removal of elements.
238e18c0412SDaniel Vetter  */
239ad579002SChris Wilson #define drm_mm_for_each_node(entry, mm) \
2402bc98c86SChris Wilson 	list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
241ad579002SChris Wilson 
242ad579002SChris Wilson /**
243ad579002SChris Wilson  * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
244ad579002SChris Wilson  * @entry: drm_mm_node structure to assign to in each iteration step
245ad579002SChris Wilson  * @next: drm_mm_node structure to store the next step
246ad579002SChris Wilson  * @mm: drm_mm allocator to walk
247ad579002SChris Wilson  *
248ad579002SChris Wilson  * This iterator walks over all nodes in the range allocator. It is implemented
249ad579002SChris Wilson  * with list_for_each_safe, so save against removal of elements.
250ad579002SChris Wilson  */
251ad579002SChris Wilson #define drm_mm_for_each_node_safe(entry, next, mm) \
2522bc98c86SChris Wilson 	list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
2539e8944abSChris Wilson 
25418b40c58SGeliang Tang #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
25518b40c58SGeliang Tang 	for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
25618b40c58SGeliang Tang 	     &entry->hole_stack != &(mm)->hole_stack ? \
25718b40c58SGeliang Tang 	     hole_start = drm_mm_hole_node_start(entry), \
25818b40c58SGeliang Tang 	     hole_end = drm_mm_hole_node_end(entry), \
25918b40c58SGeliang Tang 	     1 : 0; \
26018b40c58SGeliang Tang 	     entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
26118b40c58SGeliang Tang 
262e18c0412SDaniel Vetter /**
263e18c0412SDaniel Vetter  * drm_mm_for_each_hole - iterator to walk over all holes
264e18c0412SDaniel Vetter  * @entry: drm_mm_node used internally to track progress
265e18c0412SDaniel Vetter  * @mm: drm_mm allocator to walk
266e18c0412SDaniel Vetter  * @hole_start: ulong variable to assign the hole start to on each iteration
267e18c0412SDaniel Vetter  * @hole_end: ulong variable to assign the hole end to on each iteration
268e18c0412SDaniel Vetter  *
269e18c0412SDaniel Vetter  * This iterator walks over all holes in the range allocator. It is implemented
270e18c0412SDaniel Vetter  * with list_for_each, so not save against removal of elements. @entry is used
271e18c0412SDaniel Vetter  * internally and will not reflect a real drm_mm_node for the very first hole.
272e18c0412SDaniel Vetter  * Hence users of this iterator may not access it.
273e18c0412SDaniel Vetter  *
274e18c0412SDaniel Vetter  * Implementation Note:
275e18c0412SDaniel Vetter  * We need to inline list_for_each_entry in order to be able to set hole_start
276e18c0412SDaniel Vetter  * and hole_end on each iteration while keeping the macro sane.
27762347f9eSLauri Kasanen  *
27862347f9eSLauri Kasanen  * The __drm_mm_for_each_hole version is similar, but with added support for
27962347f9eSLauri Kasanen  * going backwards.
2809e8944abSChris Wilson  */
2819e8944abSChris Wilson #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
28218b40c58SGeliang Tang 	__drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0)
28362347f9eSLauri Kasanen 
284249d6048SJerome Glisse /*
285249d6048SJerome Glisse  * Basic range manager support (drm_mm.c)
286249d6048SJerome Glisse  */
287e18c0412SDaniel Vetter int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
288b8103450SChris Wilson 
289e18c0412SDaniel Vetter int drm_mm_insert_node_generic(struct drm_mm *mm,
290b8103450SChris Wilson 			       struct drm_mm_node *node,
291440fd528SThierry Reding 			       u64 size,
29271733207SChris Wilson 			       u64 alignment,
29331e5d7c6SDavid Herrmann 			       unsigned long color,
29462347f9eSLauri Kasanen 			       enum drm_mm_search_flags sflags,
29562347f9eSLauri Kasanen 			       enum drm_mm_allocator_flags aflags);
296e18c0412SDaniel Vetter /**
297e18c0412SDaniel Vetter  * drm_mm_insert_node - search for space and insert @node
298e18c0412SDaniel Vetter  * @mm: drm_mm to allocate from
299e18c0412SDaniel Vetter  * @node: preallocate node to insert
300e18c0412SDaniel Vetter  * @size: size of the allocation
301e18c0412SDaniel Vetter  * @alignment: alignment of the allocation
302e18c0412SDaniel Vetter  * @flags: flags to fine-tune the allocation
303e18c0412SDaniel Vetter  *
304e18c0412SDaniel Vetter  * This is a simplified version of drm_mm_insert_node_generic() with @color set
305e18c0412SDaniel Vetter  * to 0.
306e18c0412SDaniel Vetter  *
307e18c0412SDaniel Vetter  * The preallocated node must be cleared to 0.
308e18c0412SDaniel Vetter  *
309e18c0412SDaniel Vetter  * Returns:
310e18c0412SDaniel Vetter  * 0 on success, -ENOSPC if there's no suitable hole.
311e18c0412SDaniel Vetter  */
31231e5d7c6SDavid Herrmann static inline int drm_mm_insert_node(struct drm_mm *mm,
31331e5d7c6SDavid Herrmann 				     struct drm_mm_node *node,
314440fd528SThierry Reding 				     u64 size,
31571733207SChris Wilson 				     u64 alignment,
31631e5d7c6SDavid Herrmann 				     enum drm_mm_search_flags flags)
31731e5d7c6SDavid Herrmann {
31862347f9eSLauri Kasanen 	return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags,
31962347f9eSLauri Kasanen 					  DRM_MM_CREATE_DEFAULT);
32031e5d7c6SDavid Herrmann }
32131e5d7c6SDavid Herrmann 
322e18c0412SDaniel Vetter int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
323b8103450SChris Wilson 					struct drm_mm_node *node,
324440fd528SThierry Reding 					u64 size,
32571733207SChris Wilson 					u64 alignment,
326b8103450SChris Wilson 					unsigned long color,
327440fd528SThierry Reding 					u64 start,
328440fd528SThierry Reding 					u64 end,
32962347f9eSLauri Kasanen 					enum drm_mm_search_flags sflags,
33062347f9eSLauri Kasanen 					enum drm_mm_allocator_flags aflags);
331e18c0412SDaniel Vetter /**
332e18c0412SDaniel Vetter  * drm_mm_insert_node_in_range - ranged search for space and insert @node
333e18c0412SDaniel Vetter  * @mm: drm_mm to allocate from
334e18c0412SDaniel Vetter  * @node: preallocate node to insert
335e18c0412SDaniel Vetter  * @size: size of the allocation
336e18c0412SDaniel Vetter  * @alignment: alignment of the allocation
337e18c0412SDaniel Vetter  * @start: start of the allowed range for this node
338e18c0412SDaniel Vetter  * @end: end of the allowed range for this node
339e18c0412SDaniel Vetter  * @flags: flags to fine-tune the allocation
340e18c0412SDaniel Vetter  *
341e18c0412SDaniel Vetter  * This is a simplified version of drm_mm_insert_node_in_range_generic() with
342e18c0412SDaniel Vetter  * @color set to 0.
343e18c0412SDaniel Vetter  *
344e18c0412SDaniel Vetter  * The preallocated node must be cleared to 0.
345e18c0412SDaniel Vetter  *
346e18c0412SDaniel Vetter  * Returns:
347e18c0412SDaniel Vetter  * 0 on success, -ENOSPC if there's no suitable hole.
348e18c0412SDaniel Vetter  */
34931e5d7c6SDavid Herrmann static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
35031e5d7c6SDavid Herrmann 					      struct drm_mm_node *node,
351440fd528SThierry Reding 					      u64 size,
35271733207SChris Wilson 					      u64 alignment,
353440fd528SThierry Reding 					      u64 start,
354440fd528SThierry Reding 					      u64 end,
35531e5d7c6SDavid Herrmann 					      enum drm_mm_search_flags flags)
35631e5d7c6SDavid Herrmann {
35731e5d7c6SDavid Herrmann 	return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
35862347f9eSLauri Kasanen 						   0, start, end, flags,
35962347f9eSLauri Kasanen 						   DRM_MM_CREATE_DEFAULT);
36031e5d7c6SDavid Herrmann }
36131e5d7c6SDavid Herrmann 
362e18c0412SDaniel Vetter void drm_mm_remove_node(struct drm_mm_node *node);
363e18c0412SDaniel Vetter void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
36445b186f1SChris Wilson void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
365e18c0412SDaniel Vetter void drm_mm_takedown(struct drm_mm *mm);
366ac9bb7b7SChris Wilson 
367ac9bb7b7SChris Wilson /**
368ac9bb7b7SChris Wilson  * drm_mm_clean - checks whether an allocator is clean
369ac9bb7b7SChris Wilson  * @mm: drm_mm allocator to check
370ac9bb7b7SChris Wilson  *
371ac9bb7b7SChris Wilson  * Returns:
372ac9bb7b7SChris Wilson  * True if the allocator is completely free, false if there's still a node
373ac9bb7b7SChris Wilson  * allocated in it.
374ac9bb7b7SChris Wilson  */
375ac9bb7b7SChris Wilson static inline bool drm_mm_clean(const struct drm_mm *mm)
376ac9bb7b7SChris Wilson {
377ac9bb7b7SChris Wilson 	return list_empty(drm_mm_nodes(mm));
378ac9bb7b7SChris Wilson }
379249d6048SJerome Glisse 
380202b52b7SChris Wilson struct drm_mm_node *
38145b186f1SChris Wilson __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
382202b52b7SChris Wilson 
383522e85ddSChris Wilson /**
384522e85ddSChris Wilson  * drm_mm_for_each_node_in_range - iterator to walk over a range of
385522e85ddSChris Wilson  * allocated nodes
3868b2fb7b6SChris Wilson  * @node__: drm_mm_node structure to assign to in each iteration step
3878b2fb7b6SChris Wilson  * @mm__: drm_mm allocator to walk
3888b2fb7b6SChris Wilson  * @start__: starting offset, the first node will overlap this
3898b2fb7b6SChris Wilson  * @end__: ending offset, the last node will start before this (but may overlap)
390522e85ddSChris Wilson  *
391522e85ddSChris Wilson  * This iterator walks over all nodes in the range allocator that lie
392522e85ddSChris Wilson  * between @start and @end. It is implemented similarly to list_for_each(),
393522e85ddSChris Wilson  * but using the internal interval tree to accelerate the search for the
394522e85ddSChris Wilson  * starting node, and so not safe against removal of elements. It assumes
395522e85ddSChris Wilson  * that @end is within (or is the upper limit of) the drm_mm allocator.
396522e85ddSChris Wilson  */
3978b2fb7b6SChris Wilson #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__)	\
3988b2fb7b6SChris Wilson 	for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
3998b2fb7b6SChris Wilson 	     node__ && node__->start < (end__);				\
4008b2fb7b6SChris Wilson 	     node__ = list_next_entry(node__, node_list))
401202b52b7SChris Wilson 
4029a71e277SChris Wilson void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
4039a71e277SChris Wilson 				 struct drm_mm *mm,
4040b04d474SChris Wilson 				 u64 size, u64 alignment, unsigned long color,
4050b04d474SChris Wilson 				 u64 start, u64 end,
4060b04d474SChris Wilson 				 unsigned int flags);
4072c4b3895SChris Wilson 
4082c4b3895SChris Wilson /**
4092c4b3895SChris Wilson  * drm_mm_scan_init - initialize lru scanning
4102c4b3895SChris Wilson  * @scan: scan state
4112c4b3895SChris Wilson  * @mm: drm_mm to scan
4122c4b3895SChris Wilson  * @size: size of the allocation
4132c4b3895SChris Wilson  * @alignment: alignment of the allocation
4142c4b3895SChris Wilson  * @color: opaque tag value to use for the allocation
4150b04d474SChris Wilson  * @flags: flags to specify how the allocation will be performed afterwards
4162c4b3895SChris Wilson  *
4172c4b3895SChris Wilson  * This simply sets up the scanning routines with the parameters for the desired
4180b04d474SChris Wilson  * hole.
4192c4b3895SChris Wilson  *
4202c4b3895SChris Wilson  * Warning:
4212c4b3895SChris Wilson  * As long as the scan list is non-empty, no other operations than
4222c4b3895SChris Wilson  * adding/removing nodes to/from the scan list are allowed.
4232c4b3895SChris Wilson  */
4242c4b3895SChris Wilson static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
4252c4b3895SChris Wilson 				    struct drm_mm *mm,
4262c4b3895SChris Wilson 				    u64 size,
4272c4b3895SChris Wilson 				    u64 alignment,
4280b04d474SChris Wilson 				    unsigned long color,
4290b04d474SChris Wilson 				    unsigned int flags)
4302c4b3895SChris Wilson {
4310b04d474SChris Wilson 	drm_mm_scan_init_with_range(scan, mm,
4320b04d474SChris Wilson 				    size, alignment, color,
4330b04d474SChris Wilson 				    0, U64_MAX,
4340b04d474SChris Wilson 				    flags);
4352c4b3895SChris Wilson }
4362c4b3895SChris Wilson 
4379a71e277SChris Wilson bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
4389a71e277SChris Wilson 			   struct drm_mm_node *node);
4399a71e277SChris Wilson bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
4409a71e277SChris Wilson 			      struct drm_mm_node *node);
4413fa489daSChris Wilson struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
442709ea971SDaniel Vetter 
44345b186f1SChris Wilson void drm_mm_debug_table(const struct drm_mm *mm, const char *prefix);
444fa8a1238SDave Airlie #ifdef CONFIG_DEBUG_FS
44545b186f1SChris Wilson int drm_mm_dump_table(struct seq_file *m, const struct drm_mm *mm);
446fa8a1238SDave Airlie #endif
447fa8a1238SDave Airlie 
448249d6048SJerome Glisse #endif
449