xref: /openbmc/linux/include/drm/drm_mm.h (revision 3fa489da)
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 
15845b186f1SChris Wilson static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
1599e8944abSChris Wilson {
1609e8944abSChris Wilson 	return hole_node->start + hole_node->size;
1619e8944abSChris Wilson }
1629e8944abSChris Wilson 
163e18c0412SDaniel Vetter /**
164e18c0412SDaniel Vetter  * drm_mm_hole_node_start - computes the start of the hole following @node
165e18c0412SDaniel Vetter  * @hole_node: drm_mm_node which implicitly tracks the following hole
166e18c0412SDaniel Vetter  *
167ba004e39SChris Wilson  * This is useful for driver-specific debug dumpers. Otherwise drivers should
168ba004e39SChris Wilson  * not inspect holes themselves. Drivers must check first whether a hole indeed
169e18c0412SDaniel Vetter  * follows by looking at node->hole_follows.
170e18c0412SDaniel Vetter  *
171e18c0412SDaniel Vetter  * Returns:
172e18c0412SDaniel Vetter  * Start of the subsequent hole.
173e18c0412SDaniel Vetter  */
17445b186f1SChris Wilson static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
1759e8944abSChris Wilson {
176b3ee963fSChris Wilson 	DRM_MM_BUG_ON(!hole_node->hole_follows);
1779e8944abSChris Wilson 	return __drm_mm_hole_node_start(hole_node);
1789e8944abSChris Wilson }
1799e8944abSChris Wilson 
18045b186f1SChris Wilson static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
1819e8944abSChris Wilson {
18287069f44SGeliang Tang 	return list_next_entry(hole_node, node_list)->start;
1839e8944abSChris Wilson }
1849e8944abSChris Wilson 
185e18c0412SDaniel Vetter /**
186e18c0412SDaniel Vetter  * drm_mm_hole_node_end - computes the end of the hole following @node
187e18c0412SDaniel Vetter  * @hole_node: drm_mm_node which implicitly tracks the following hole
188e18c0412SDaniel Vetter  *
189ba004e39SChris Wilson  * This is useful for driver-specific debug dumpers. Otherwise drivers should
190ba004e39SChris Wilson  * not inspect holes themselves. Drivers must check first whether a hole indeed
191e18c0412SDaniel Vetter  * follows by looking at node->hole_follows.
192e18c0412SDaniel Vetter  *
193e18c0412SDaniel Vetter  * Returns:
194e18c0412SDaniel Vetter  * End of the subsequent hole.
195e18c0412SDaniel Vetter  */
19645b186f1SChris Wilson static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
1979e8944abSChris Wilson {
1989e8944abSChris Wilson 	return __drm_mm_hole_node_end(hole_node);
1999e8944abSChris Wilson }
2009e8944abSChris Wilson 
2012bc98c86SChris Wilson /**
2022bc98c86SChris Wilson  * drm_mm_nodes - list of nodes under the drm_mm range manager
2032bc98c86SChris Wilson  * @mm: the struct drm_mm range manger
2042bc98c86SChris Wilson  *
2052bc98c86SChris Wilson  * As the drm_mm range manager hides its node_list deep with its
2062bc98c86SChris Wilson  * structure, extracting it looks painful and repetitive. This is
2072bc98c86SChris Wilson  * not expected to be used outside of the drm_mm_for_each_node()
2082bc98c86SChris Wilson  * macros and similar internal functions.
2092bc98c86SChris Wilson  *
2102bc98c86SChris Wilson  * Returns:
2112bc98c86SChris Wilson  * The node list, may be empty.
2122bc98c86SChris Wilson  */
2132bc98c86SChris Wilson #define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
214ad579002SChris Wilson 
215e18c0412SDaniel Vetter /**
216e18c0412SDaniel Vetter  * drm_mm_for_each_node - iterator to walk over all allocated nodes
217e18c0412SDaniel Vetter  * @entry: drm_mm_node structure to assign to in each iteration step
218e18c0412SDaniel Vetter  * @mm: drm_mm allocator to walk
219e18c0412SDaniel Vetter  *
220e18c0412SDaniel Vetter  * This iterator walks over all nodes in the range allocator. It is implemented
221e18c0412SDaniel Vetter  * with list_for_each, so not save against removal of elements.
222e18c0412SDaniel Vetter  */
223ad579002SChris Wilson #define drm_mm_for_each_node(entry, mm) \
2242bc98c86SChris Wilson 	list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
225ad579002SChris Wilson 
226ad579002SChris Wilson /**
227ad579002SChris Wilson  * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
228ad579002SChris Wilson  * @entry: drm_mm_node structure to assign to in each iteration step
229ad579002SChris Wilson  * @next: drm_mm_node structure to store the next step
230ad579002SChris Wilson  * @mm: drm_mm allocator to walk
231ad579002SChris Wilson  *
232ad579002SChris Wilson  * This iterator walks over all nodes in the range allocator. It is implemented
233ad579002SChris Wilson  * with list_for_each_safe, so save against removal of elements.
234ad579002SChris Wilson  */
235ad579002SChris Wilson #define drm_mm_for_each_node_safe(entry, next, mm) \
2362bc98c86SChris Wilson 	list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
2379e8944abSChris Wilson 
23818b40c58SGeliang Tang #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
23918b40c58SGeliang Tang 	for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
24018b40c58SGeliang Tang 	     &entry->hole_stack != &(mm)->hole_stack ? \
24118b40c58SGeliang Tang 	     hole_start = drm_mm_hole_node_start(entry), \
24218b40c58SGeliang Tang 	     hole_end = drm_mm_hole_node_end(entry), \
24318b40c58SGeliang Tang 	     1 : 0; \
24418b40c58SGeliang Tang 	     entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
24518b40c58SGeliang Tang 
246e18c0412SDaniel Vetter /**
247e18c0412SDaniel Vetter  * drm_mm_for_each_hole - iterator to walk over all holes
248e18c0412SDaniel Vetter  * @entry: drm_mm_node used internally to track progress
249e18c0412SDaniel Vetter  * @mm: drm_mm allocator to walk
250e18c0412SDaniel Vetter  * @hole_start: ulong variable to assign the hole start to on each iteration
251e18c0412SDaniel Vetter  * @hole_end: ulong variable to assign the hole end to on each iteration
252e18c0412SDaniel Vetter  *
253e18c0412SDaniel Vetter  * This iterator walks over all holes in the range allocator. It is implemented
254e18c0412SDaniel Vetter  * with list_for_each, so not save against removal of elements. @entry is used
255e18c0412SDaniel Vetter  * internally and will not reflect a real drm_mm_node for the very first hole.
256e18c0412SDaniel Vetter  * Hence users of this iterator may not access it.
257e18c0412SDaniel Vetter  *
258e18c0412SDaniel Vetter  * Implementation Note:
259e18c0412SDaniel Vetter  * We need to inline list_for_each_entry in order to be able to set hole_start
260e18c0412SDaniel Vetter  * and hole_end on each iteration while keeping the macro sane.
26162347f9eSLauri Kasanen  *
26262347f9eSLauri Kasanen  * The __drm_mm_for_each_hole version is similar, but with added support for
26362347f9eSLauri Kasanen  * going backwards.
2649e8944abSChris Wilson  */
2659e8944abSChris Wilson #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
26618b40c58SGeliang Tang 	__drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0)
26762347f9eSLauri Kasanen 
268249d6048SJerome Glisse /*
269249d6048SJerome Glisse  * Basic range manager support (drm_mm.c)
270249d6048SJerome Glisse  */
271e18c0412SDaniel Vetter int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
272b8103450SChris Wilson 
273e18c0412SDaniel Vetter int drm_mm_insert_node_generic(struct drm_mm *mm,
274b8103450SChris Wilson 			       struct drm_mm_node *node,
275440fd528SThierry Reding 			       u64 size,
27671733207SChris Wilson 			       u64 alignment,
27731e5d7c6SDavid Herrmann 			       unsigned long color,
27862347f9eSLauri Kasanen 			       enum drm_mm_search_flags sflags,
27962347f9eSLauri Kasanen 			       enum drm_mm_allocator_flags aflags);
280e18c0412SDaniel Vetter /**
281e18c0412SDaniel Vetter  * drm_mm_insert_node - search for space and insert @node
282e18c0412SDaniel Vetter  * @mm: drm_mm to allocate from
283e18c0412SDaniel Vetter  * @node: preallocate node to insert
284e18c0412SDaniel Vetter  * @size: size of the allocation
285e18c0412SDaniel Vetter  * @alignment: alignment of the allocation
286e18c0412SDaniel Vetter  * @flags: flags to fine-tune the allocation
287e18c0412SDaniel Vetter  *
288e18c0412SDaniel Vetter  * This is a simplified version of drm_mm_insert_node_generic() with @color set
289e18c0412SDaniel Vetter  * to 0.
290e18c0412SDaniel Vetter  *
291e18c0412SDaniel Vetter  * The preallocated node must be cleared to 0.
292e18c0412SDaniel Vetter  *
293e18c0412SDaniel Vetter  * Returns:
294e18c0412SDaniel Vetter  * 0 on success, -ENOSPC if there's no suitable hole.
295e18c0412SDaniel Vetter  */
29631e5d7c6SDavid Herrmann static inline int drm_mm_insert_node(struct drm_mm *mm,
29731e5d7c6SDavid Herrmann 				     struct drm_mm_node *node,
298440fd528SThierry Reding 				     u64 size,
29971733207SChris Wilson 				     u64 alignment,
30031e5d7c6SDavid Herrmann 				     enum drm_mm_search_flags flags)
30131e5d7c6SDavid Herrmann {
30262347f9eSLauri Kasanen 	return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags,
30362347f9eSLauri Kasanen 					  DRM_MM_CREATE_DEFAULT);
30431e5d7c6SDavid Herrmann }
30531e5d7c6SDavid Herrmann 
306e18c0412SDaniel Vetter int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
307b8103450SChris Wilson 					struct drm_mm_node *node,
308440fd528SThierry Reding 					u64 size,
30971733207SChris Wilson 					u64 alignment,
310b8103450SChris Wilson 					unsigned long color,
311440fd528SThierry Reding 					u64 start,
312440fd528SThierry Reding 					u64 end,
31362347f9eSLauri Kasanen 					enum drm_mm_search_flags sflags,
31462347f9eSLauri Kasanen 					enum drm_mm_allocator_flags aflags);
315e18c0412SDaniel Vetter /**
316e18c0412SDaniel Vetter  * drm_mm_insert_node_in_range - ranged search for space and insert @node
317e18c0412SDaniel Vetter  * @mm: drm_mm to allocate from
318e18c0412SDaniel Vetter  * @node: preallocate node to insert
319e18c0412SDaniel Vetter  * @size: size of the allocation
320e18c0412SDaniel Vetter  * @alignment: alignment of the allocation
321e18c0412SDaniel Vetter  * @start: start of the allowed range for this node
322e18c0412SDaniel Vetter  * @end: end of the allowed range for this node
323e18c0412SDaniel Vetter  * @flags: flags to fine-tune the allocation
324e18c0412SDaniel Vetter  *
325e18c0412SDaniel Vetter  * This is a simplified version of drm_mm_insert_node_in_range_generic() with
326e18c0412SDaniel Vetter  * @color set to 0.
327e18c0412SDaniel Vetter  *
328e18c0412SDaniel Vetter  * The preallocated node must be cleared to 0.
329e18c0412SDaniel Vetter  *
330e18c0412SDaniel Vetter  * Returns:
331e18c0412SDaniel Vetter  * 0 on success, -ENOSPC if there's no suitable hole.
332e18c0412SDaniel Vetter  */
33331e5d7c6SDavid Herrmann static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
33431e5d7c6SDavid Herrmann 					      struct drm_mm_node *node,
335440fd528SThierry Reding 					      u64 size,
33671733207SChris Wilson 					      u64 alignment,
337440fd528SThierry Reding 					      u64 start,
338440fd528SThierry Reding 					      u64 end,
33931e5d7c6SDavid Herrmann 					      enum drm_mm_search_flags flags)
34031e5d7c6SDavid Herrmann {
34131e5d7c6SDavid Herrmann 	return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
34262347f9eSLauri Kasanen 						   0, start, end, flags,
34362347f9eSLauri Kasanen 						   DRM_MM_CREATE_DEFAULT);
34431e5d7c6SDavid Herrmann }
34531e5d7c6SDavid Herrmann 
346e18c0412SDaniel Vetter void drm_mm_remove_node(struct drm_mm_node *node);
347e18c0412SDaniel Vetter void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
34845b186f1SChris Wilson void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
349e18c0412SDaniel Vetter void drm_mm_takedown(struct drm_mm *mm);
350ac9bb7b7SChris Wilson 
351ac9bb7b7SChris Wilson /**
352ac9bb7b7SChris Wilson  * drm_mm_clean - checks whether an allocator is clean
353ac9bb7b7SChris Wilson  * @mm: drm_mm allocator to check
354ac9bb7b7SChris Wilson  *
355ac9bb7b7SChris Wilson  * Returns:
356ac9bb7b7SChris Wilson  * True if the allocator is completely free, false if there's still a node
357ac9bb7b7SChris Wilson  * allocated in it.
358ac9bb7b7SChris Wilson  */
359ac9bb7b7SChris Wilson static inline bool drm_mm_clean(const struct drm_mm *mm)
360ac9bb7b7SChris Wilson {
361ac9bb7b7SChris Wilson 	return list_empty(drm_mm_nodes(mm));
362ac9bb7b7SChris Wilson }
363249d6048SJerome Glisse 
364202b52b7SChris Wilson struct drm_mm_node *
36545b186f1SChris Wilson __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
366202b52b7SChris Wilson 
367522e85ddSChris Wilson /**
368522e85ddSChris Wilson  * drm_mm_for_each_node_in_range - iterator to walk over a range of
369522e85ddSChris Wilson  * allocated nodes
3708b2fb7b6SChris Wilson  * @node__: drm_mm_node structure to assign to in each iteration step
3718b2fb7b6SChris Wilson  * @mm__: drm_mm allocator to walk
3728b2fb7b6SChris Wilson  * @start__: starting offset, the first node will overlap this
3738b2fb7b6SChris Wilson  * @end__: ending offset, the last node will start before this (but may overlap)
374522e85ddSChris Wilson  *
375522e85ddSChris Wilson  * This iterator walks over all nodes in the range allocator that lie
376522e85ddSChris Wilson  * between @start and @end. It is implemented similarly to list_for_each(),
377522e85ddSChris Wilson  * but using the internal interval tree to accelerate the search for the
378522e85ddSChris Wilson  * starting node, and so not safe against removal of elements. It assumes
379522e85ddSChris Wilson  * that @end is within (or is the upper limit of) the drm_mm allocator.
380522e85ddSChris Wilson  */
3818b2fb7b6SChris Wilson #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__)	\
3828b2fb7b6SChris Wilson 	for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
3838b2fb7b6SChris Wilson 	     node__ && node__->start < (end__);				\
3848b2fb7b6SChris Wilson 	     node__ = list_next_entry(node__, node_list))
385202b52b7SChris Wilson 
3869a71e277SChris Wilson void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
3879a71e277SChris Wilson 				 struct drm_mm *mm,
3880b04d474SChris Wilson 				 u64 size, u64 alignment, unsigned long color,
3890b04d474SChris Wilson 				 u64 start, u64 end,
3900b04d474SChris Wilson 				 unsigned int flags);
3912c4b3895SChris Wilson 
3922c4b3895SChris Wilson /**
3932c4b3895SChris Wilson  * drm_mm_scan_init - initialize lru scanning
3942c4b3895SChris Wilson  * @scan: scan state
3952c4b3895SChris Wilson  * @mm: drm_mm to scan
3962c4b3895SChris Wilson  * @size: size of the allocation
3972c4b3895SChris Wilson  * @alignment: alignment of the allocation
3982c4b3895SChris Wilson  * @color: opaque tag value to use for the allocation
3990b04d474SChris Wilson  * @flags: flags to specify how the allocation will be performed afterwards
4002c4b3895SChris Wilson  *
4012c4b3895SChris Wilson  * This simply sets up the scanning routines with the parameters for the desired
4020b04d474SChris Wilson  * hole.
4032c4b3895SChris Wilson  *
4042c4b3895SChris Wilson  * Warning:
4052c4b3895SChris Wilson  * As long as the scan list is non-empty, no other operations than
4062c4b3895SChris Wilson  * adding/removing nodes to/from the scan list are allowed.
4072c4b3895SChris Wilson  */
4082c4b3895SChris Wilson static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
4092c4b3895SChris Wilson 				    struct drm_mm *mm,
4102c4b3895SChris Wilson 				    u64 size,
4112c4b3895SChris Wilson 				    u64 alignment,
4120b04d474SChris Wilson 				    unsigned long color,
4130b04d474SChris Wilson 				    unsigned int flags)
4142c4b3895SChris Wilson {
4150b04d474SChris Wilson 	drm_mm_scan_init_with_range(scan, mm,
4160b04d474SChris Wilson 				    size, alignment, color,
4170b04d474SChris Wilson 				    0, U64_MAX,
4180b04d474SChris Wilson 				    flags);
4192c4b3895SChris Wilson }
4202c4b3895SChris Wilson 
4219a71e277SChris Wilson bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
4229a71e277SChris Wilson 			   struct drm_mm_node *node);
4239a71e277SChris Wilson bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
4249a71e277SChris Wilson 			      struct drm_mm_node *node);
4253fa489daSChris Wilson struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
426709ea971SDaniel Vetter 
42745b186f1SChris Wilson void drm_mm_debug_table(const struct drm_mm *mm, const char *prefix);
428fa8a1238SDave Airlie #ifdef CONFIG_DEBUG_FS
42945b186f1SChris Wilson int drm_mm_dump_table(struct seq_file *m, const struct drm_mm *mm);
430fa8a1238SDave Airlie #endif
431fa8a1238SDave Airlie 
432249d6048SJerome Glisse #endif
433