xref: /openbmc/linux/include/drm/drm_mm.h (revision b5c3714f)
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 
5631e5d7c6SDavid Herrmann enum drm_mm_search_flags {
5731e5d7c6SDavid Herrmann 	DRM_MM_SEARCH_DEFAULT =		0,
5831e5d7c6SDavid Herrmann 	DRM_MM_SEARCH_BEST =		1 << 0,
5962347f9eSLauri Kasanen 	DRM_MM_SEARCH_BELOW =		1 << 1,
6031e5d7c6SDavid Herrmann };
6131e5d7c6SDavid Herrmann 
6262347f9eSLauri Kasanen enum drm_mm_allocator_flags {
6362347f9eSLauri Kasanen 	DRM_MM_CREATE_DEFAULT =		0,
6462347f9eSLauri Kasanen 	DRM_MM_CREATE_TOP =		1 << 0,
6562347f9eSLauri Kasanen };
6662347f9eSLauri Kasanen 
6762347f9eSLauri Kasanen #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT
6862347f9eSLauri Kasanen #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP
6962347f9eSLauri Kasanen 
70249d6048SJerome Glisse struct drm_mm_node {
71d1024ce9SDaniel Vetter 	struct list_head node_list;
72ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
73202b52b7SChris Wilson 	struct rb_node rb;
74ea7b1dd4SDaniel Vetter 	unsigned hole_follows : 1;
75b0b7af18SDaniel Vetter 	unsigned allocated : 1;
76f29051f1SChris Wilson 	bool scanned_block : 1;
776b9d89b4SChris Wilson 	unsigned long color;
78440fd528SThierry Reding 	u64 start;
79440fd528SThierry Reding 	u64 size;
80202b52b7SChris Wilson 	u64 __subtree_last;
81249d6048SJerome Glisse 	struct drm_mm *mm;
825705670dSChris Wilson #ifdef CONFIG_DRM_DEBUG_MM
835705670dSChris Wilson 	depot_stack_handle_t stack;
845705670dSChris Wilson #endif
85249d6048SJerome Glisse };
86249d6048SJerome Glisse 
87249d6048SJerome Glisse struct drm_mm {
8825985edcSLucas De Marchi 	/* List of all memory nodes that immediately precede a free hole. */
89ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
90ea7b1dd4SDaniel Vetter 	/* head_node.node_list is the list of all memory nodes, ordered
91ea7b1dd4SDaniel Vetter 	 * according to the (increasing) start address of the memory node. */
92ea7b1dd4SDaniel Vetter 	struct drm_mm_node head_node;
93202b52b7SChris Wilson 	/* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
94202b52b7SChris Wilson 	struct rb_root interval_tree;
95202b52b7SChris Wilson 
9645b186f1SChris Wilson 	void (*color_adjust)(const struct drm_mm_node *node,
9745b186f1SChris Wilson 			     unsigned long color,
98440fd528SThierry Reding 			     u64 *start, u64 *end);
999a71e277SChris Wilson 
1009a71e277SChris Wilson 	unsigned long scan_active;
1019a71e277SChris Wilson };
1029a71e277SChris Wilson 
1039a71e277SChris Wilson struct drm_mm_scan {
1049a71e277SChris Wilson 	struct drm_mm *mm;
1059a71e277SChris Wilson 
1069a71e277SChris Wilson 	u64 size;
1079a71e277SChris Wilson 	u64 alignment;
1089a956b15SChris Wilson 	u64 remainder_mask;
1099a71e277SChris Wilson 
1109a71e277SChris Wilson 	u64 range_start;
1119a71e277SChris Wilson 	u64 range_end;
1129a71e277SChris Wilson 
1139a71e277SChris Wilson 	u64 hit_start;
1149a71e277SChris Wilson 	u64 hit_end;
1159a71e277SChris Wilson 
1169a71e277SChris Wilson 	unsigned long color;
1170b04d474SChris Wilson 	unsigned int flags;
118249d6048SJerome Glisse };
119249d6048SJerome Glisse 
120e18c0412SDaniel Vetter /**
121e18c0412SDaniel Vetter  * drm_mm_node_allocated - checks whether a node is allocated
122e18c0412SDaniel Vetter  * @node: drm_mm_node to check
123e18c0412SDaniel Vetter  *
124ba004e39SChris Wilson  * Drivers are required to clear a node prior to using it with the
125ba004e39SChris Wilson  * drm_mm range manager.
126ba004e39SChris Wilson  *
127ba004e39SChris Wilson  * Drivers should use this helper for proper encapsulation of drm_mm
128e18c0412SDaniel Vetter  * internals.
129e18c0412SDaniel Vetter  *
130e18c0412SDaniel Vetter  * Returns:
131e18c0412SDaniel Vetter  * True if the @node is allocated.
132e18c0412SDaniel Vetter  */
13345b186f1SChris Wilson static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
134b0b7af18SDaniel Vetter {
135b0b7af18SDaniel Vetter 	return node->allocated;
136b0b7af18SDaniel Vetter }
137b0b7af18SDaniel Vetter 
138e18c0412SDaniel Vetter /**
139e18c0412SDaniel Vetter  * drm_mm_initialized - checks whether an allocator is initialized
140e18c0412SDaniel Vetter  * @mm: drm_mm to check
141e18c0412SDaniel Vetter  *
142ba004e39SChris Wilson  * Drivers should clear the struct drm_mm prior to initialisation if they
143ba004e39SChris Wilson  * want to use this function.
144ba004e39SChris Wilson  *
145ba004e39SChris Wilson  * Drivers should use this helper for proper encapsulation of drm_mm
146e18c0412SDaniel Vetter  * internals.
147e18c0412SDaniel Vetter  *
148e18c0412SDaniel Vetter  * Returns:
149e18c0412SDaniel Vetter  * True if the @mm is initialized.
150e18c0412SDaniel Vetter  */
15145b186f1SChris Wilson static inline bool drm_mm_initialized(const struct drm_mm *mm)
15231a5b8ceSDaniel Vetter {
153ea7b1dd4SDaniel Vetter 	return mm->hole_stack.next;
15431a5b8ceSDaniel Vetter }
1559e8944abSChris Wilson 
1563f85fb34SChris Wilson /**
1573f85fb34SChris Wilson  * drm_mm_hole_follows - checks whether a hole follows this node
1583f85fb34SChris Wilson  * @node: drm_mm_node to check
1593f85fb34SChris Wilson  *
1603f85fb34SChris Wilson  * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
1613f85fb34SChris Wilson  * If you wish to know whether a hole follows this particular node,
1623f85fb34SChris Wilson  * query this function.
1633f85fb34SChris Wilson  *
1643f85fb34SChris Wilson  * Returns:
1653f85fb34SChris Wilson  * True if a hole follows the @node.
1663f85fb34SChris Wilson  */
1673f85fb34SChris Wilson static inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
1683f85fb34SChris Wilson {
1693f85fb34SChris Wilson 	return node->hole_follows;
1703f85fb34SChris Wilson }
1713f85fb34SChris Wilson 
17245b186f1SChris Wilson static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
1739e8944abSChris Wilson {
1749e8944abSChris Wilson 	return hole_node->start + hole_node->size;
1759e8944abSChris Wilson }
1769e8944abSChris Wilson 
177e18c0412SDaniel Vetter /**
178e18c0412SDaniel Vetter  * drm_mm_hole_node_start - computes the start of the hole following @node
179e18c0412SDaniel Vetter  * @hole_node: drm_mm_node which implicitly tracks the following hole
180e18c0412SDaniel Vetter  *
181ba004e39SChris Wilson  * This is useful for driver-specific debug dumpers. Otherwise drivers should
182ba004e39SChris Wilson  * not inspect holes themselves. Drivers must check first whether a hole indeed
1833f85fb34SChris Wilson  * follows by looking at drm_mm_hole_follows()
184e18c0412SDaniel Vetter  *
185e18c0412SDaniel Vetter  * Returns:
186e18c0412SDaniel Vetter  * Start of the subsequent hole.
187e18c0412SDaniel Vetter  */
18845b186f1SChris Wilson static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
1899e8944abSChris Wilson {
1903f85fb34SChris Wilson 	DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
1919e8944abSChris Wilson 	return __drm_mm_hole_node_start(hole_node);
1929e8944abSChris Wilson }
1939e8944abSChris Wilson 
19445b186f1SChris Wilson static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
1959e8944abSChris Wilson {
19687069f44SGeliang Tang 	return list_next_entry(hole_node, node_list)->start;
1979e8944abSChris Wilson }
1989e8944abSChris Wilson 
199e18c0412SDaniel Vetter /**
200e18c0412SDaniel Vetter  * drm_mm_hole_node_end - computes the end of the hole following @node
201e18c0412SDaniel Vetter  * @hole_node: drm_mm_node which implicitly tracks the following hole
202e18c0412SDaniel Vetter  *
203ba004e39SChris Wilson  * This is useful for driver-specific debug dumpers. Otherwise drivers should
204ba004e39SChris Wilson  * not inspect holes themselves. Drivers must check first whether a hole indeed
2053f85fb34SChris Wilson  * follows by looking at drm_mm_hole_follows().
206e18c0412SDaniel Vetter  *
207e18c0412SDaniel Vetter  * Returns:
208e18c0412SDaniel Vetter  * End of the subsequent hole.
209e18c0412SDaniel Vetter  */
21045b186f1SChris Wilson static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
2119e8944abSChris Wilson {
2129e8944abSChris Wilson 	return __drm_mm_hole_node_end(hole_node);
2139e8944abSChris Wilson }
2149e8944abSChris Wilson 
2152bc98c86SChris Wilson /**
2162bc98c86SChris Wilson  * drm_mm_nodes - list of nodes under the drm_mm range manager
2172bc98c86SChris Wilson  * @mm: the struct drm_mm range manger
2182bc98c86SChris Wilson  *
2192bc98c86SChris Wilson  * As the drm_mm range manager hides its node_list deep with its
2202bc98c86SChris Wilson  * structure, extracting it looks painful and repetitive. This is
2212bc98c86SChris Wilson  * not expected to be used outside of the drm_mm_for_each_node()
2222bc98c86SChris Wilson  * macros and similar internal functions.
2232bc98c86SChris Wilson  *
2242bc98c86SChris Wilson  * Returns:
2252bc98c86SChris Wilson  * The node list, may be empty.
2262bc98c86SChris Wilson  */
2272bc98c86SChris Wilson #define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
228ad579002SChris Wilson 
229e18c0412SDaniel Vetter /**
230e18c0412SDaniel Vetter  * drm_mm_for_each_node - iterator to walk over all allocated nodes
231e18c0412SDaniel Vetter  * @entry: drm_mm_node structure to assign to in each iteration step
232e18c0412SDaniel Vetter  * @mm: drm_mm allocator to walk
233e18c0412SDaniel Vetter  *
234e18c0412SDaniel Vetter  * This iterator walks over all nodes in the range allocator. It is implemented
235e18c0412SDaniel Vetter  * with list_for_each, so not save against removal of elements.
236e18c0412SDaniel Vetter  */
237ad579002SChris Wilson #define drm_mm_for_each_node(entry, mm) \
2382bc98c86SChris Wilson 	list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
239ad579002SChris Wilson 
240ad579002SChris Wilson /**
241ad579002SChris Wilson  * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
242ad579002SChris Wilson  * @entry: drm_mm_node structure to assign to in each iteration step
243ad579002SChris Wilson  * @next: drm_mm_node structure to store the next step
244ad579002SChris Wilson  * @mm: drm_mm allocator to walk
245ad579002SChris Wilson  *
246ad579002SChris Wilson  * This iterator walks over all nodes in the range allocator. It is implemented
247ad579002SChris Wilson  * with list_for_each_safe, so save against removal of elements.
248ad579002SChris Wilson  */
249ad579002SChris Wilson #define drm_mm_for_each_node_safe(entry, next, mm) \
2502bc98c86SChris Wilson 	list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
2519e8944abSChris Wilson 
25218b40c58SGeliang Tang #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
25318b40c58SGeliang Tang 	for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
25418b40c58SGeliang Tang 	     &entry->hole_stack != &(mm)->hole_stack ? \
25518b40c58SGeliang Tang 	     hole_start = drm_mm_hole_node_start(entry), \
25618b40c58SGeliang Tang 	     hole_end = drm_mm_hole_node_end(entry), \
25718b40c58SGeliang Tang 	     1 : 0; \
25818b40c58SGeliang Tang 	     entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
25918b40c58SGeliang Tang 
260e18c0412SDaniel Vetter /**
261e18c0412SDaniel Vetter  * drm_mm_for_each_hole - iterator to walk over all holes
262e18c0412SDaniel Vetter  * @entry: drm_mm_node used internally to track progress
263e18c0412SDaniel Vetter  * @mm: drm_mm allocator to walk
264e18c0412SDaniel Vetter  * @hole_start: ulong variable to assign the hole start to on each iteration
265e18c0412SDaniel Vetter  * @hole_end: ulong variable to assign the hole end to on each iteration
266e18c0412SDaniel Vetter  *
267e18c0412SDaniel Vetter  * This iterator walks over all holes in the range allocator. It is implemented
268e18c0412SDaniel Vetter  * with list_for_each, so not save against removal of elements. @entry is used
269e18c0412SDaniel Vetter  * internally and will not reflect a real drm_mm_node for the very first hole.
270e18c0412SDaniel Vetter  * Hence users of this iterator may not access it.
271e18c0412SDaniel Vetter  *
272e18c0412SDaniel Vetter  * Implementation Note:
273e18c0412SDaniel Vetter  * We need to inline list_for_each_entry in order to be able to set hole_start
274e18c0412SDaniel Vetter  * and hole_end on each iteration while keeping the macro sane.
27562347f9eSLauri Kasanen  *
27662347f9eSLauri Kasanen  * The __drm_mm_for_each_hole version is similar, but with added support for
27762347f9eSLauri Kasanen  * going backwards.
2789e8944abSChris Wilson  */
2799e8944abSChris Wilson #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
28018b40c58SGeliang Tang 	__drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0)
28162347f9eSLauri Kasanen 
282249d6048SJerome Glisse /*
283249d6048SJerome Glisse  * Basic range manager support (drm_mm.c)
284249d6048SJerome Glisse  */
285e18c0412SDaniel Vetter int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
286e18c0412SDaniel Vetter int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
287b8103450SChris Wilson 					struct drm_mm_node *node,
288440fd528SThierry Reding 					u64 size,
28971733207SChris Wilson 					u64 alignment,
290b8103450SChris Wilson 					unsigned long color,
291440fd528SThierry Reding 					u64 start,
292440fd528SThierry Reding 					u64 end,
29362347f9eSLauri Kasanen 					enum drm_mm_search_flags sflags,
29462347f9eSLauri Kasanen 					enum drm_mm_allocator_flags aflags);
295adb040b8SChris Wilson 
296e18c0412SDaniel Vetter /**
297e18c0412SDaniel Vetter  * drm_mm_insert_node_in_range - ranged 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  * @start: start of the allowed range for this node
303e18c0412SDaniel Vetter  * @end: end of the allowed range for this node
304e18c0412SDaniel Vetter  * @flags: flags to fine-tune the allocation
305e18c0412SDaniel Vetter  *
306e18c0412SDaniel Vetter  * This is a simplified version of drm_mm_insert_node_in_range_generic() with
307e18c0412SDaniel Vetter  * @color set to 0.
308e18c0412SDaniel Vetter  *
309e18c0412SDaniel Vetter  * The preallocated node must be cleared to 0.
310e18c0412SDaniel Vetter  *
311e18c0412SDaniel Vetter  * Returns:
312e18c0412SDaniel Vetter  * 0 on success, -ENOSPC if there's no suitable hole.
313e18c0412SDaniel Vetter  */
31431e5d7c6SDavid Herrmann static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
31531e5d7c6SDavid Herrmann 					      struct drm_mm_node *node,
316440fd528SThierry Reding 					      u64 size,
31771733207SChris Wilson 					      u64 alignment,
318440fd528SThierry Reding 					      u64 start,
319440fd528SThierry Reding 					      u64 end,
32031e5d7c6SDavid Herrmann 					      enum drm_mm_search_flags flags)
32131e5d7c6SDavid Herrmann {
32231e5d7c6SDavid Herrmann 	return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
32362347f9eSLauri Kasanen 						   0, start, end, flags,
32462347f9eSLauri Kasanen 						   DRM_MM_CREATE_DEFAULT);
32531e5d7c6SDavid Herrmann }
32631e5d7c6SDavid Herrmann 
327adb040b8SChris Wilson /**
328adb040b8SChris Wilson  * drm_mm_insert_node_generic - search for space and insert @node
329adb040b8SChris Wilson  * @mm: drm_mm to allocate from
330adb040b8SChris Wilson  * @node: preallocate node to insert
331adb040b8SChris Wilson  * @size: size of the allocation
332adb040b8SChris Wilson  * @alignment: alignment of the allocation
333adb040b8SChris Wilson  * @color: opaque tag value to use for this node
334adb040b8SChris Wilson  * @sflags: flags to fine-tune the allocation search
335adb040b8SChris Wilson  * @aflags: flags to fine-tune the allocation behavior
336adb040b8SChris Wilson  *
337adb040b8SChris Wilson  * The preallocated node must be cleared to 0.
338adb040b8SChris Wilson  *
339adb040b8SChris Wilson  * Returns:
340adb040b8SChris Wilson  * 0 on success, -ENOSPC if there's no suitable hole.
341adb040b8SChris Wilson  */
342adb040b8SChris Wilson static inline int
343adb040b8SChris Wilson drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
344adb040b8SChris Wilson 			   u64 size, u64 alignment,
345adb040b8SChris Wilson 			   unsigned long color,
346adb040b8SChris Wilson 			   enum drm_mm_search_flags sflags,
347adb040b8SChris Wilson 			   enum drm_mm_allocator_flags aflags)
348adb040b8SChris Wilson {
349adb040b8SChris Wilson 	return drm_mm_insert_node_in_range_generic(mm, node,
350adb040b8SChris Wilson 						   size, alignment, 0,
351adb040b8SChris Wilson 						   0, U64_MAX,
352adb040b8SChris Wilson 						   sflags, aflags);
353adb040b8SChris Wilson }
354adb040b8SChris Wilson 
355adb040b8SChris Wilson /**
356adb040b8SChris Wilson  * drm_mm_insert_node - search for space and insert @node
357adb040b8SChris Wilson  * @mm: drm_mm to allocate from
358adb040b8SChris Wilson  * @node: preallocate node to insert
359adb040b8SChris Wilson  * @size: size of the allocation
360adb040b8SChris Wilson  * @alignment: alignment of the allocation
361adb040b8SChris Wilson  * @flags: flags to fine-tune the allocation
362adb040b8SChris Wilson  *
363adb040b8SChris Wilson  * This is a simplified version of drm_mm_insert_node_generic() with @color set
364adb040b8SChris Wilson  * to 0.
365adb040b8SChris Wilson  *
366adb040b8SChris Wilson  * The preallocated node must be cleared to 0.
367adb040b8SChris Wilson  *
368adb040b8SChris Wilson  * Returns:
369adb040b8SChris Wilson  * 0 on success, -ENOSPC if there's no suitable hole.
370adb040b8SChris Wilson  */
371adb040b8SChris Wilson static inline int drm_mm_insert_node(struct drm_mm *mm,
372adb040b8SChris Wilson 				     struct drm_mm_node *node,
373adb040b8SChris Wilson 				     u64 size,
374adb040b8SChris Wilson 				     u64 alignment,
375adb040b8SChris Wilson 				     enum drm_mm_search_flags flags)
376adb040b8SChris Wilson {
377adb040b8SChris Wilson 	return drm_mm_insert_node_generic(mm, node,
378adb040b8SChris Wilson 					  size, alignment, 0,
379adb040b8SChris Wilson 					  flags, DRM_MM_CREATE_DEFAULT);
380adb040b8SChris Wilson }
381adb040b8SChris Wilson 
382e18c0412SDaniel Vetter void drm_mm_remove_node(struct drm_mm_node *node);
383e18c0412SDaniel Vetter void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
38445b186f1SChris Wilson void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
385e18c0412SDaniel Vetter void drm_mm_takedown(struct drm_mm *mm);
386ac9bb7b7SChris Wilson 
387ac9bb7b7SChris Wilson /**
388ac9bb7b7SChris Wilson  * drm_mm_clean - checks whether an allocator is clean
389ac9bb7b7SChris Wilson  * @mm: drm_mm allocator to check
390ac9bb7b7SChris Wilson  *
391ac9bb7b7SChris Wilson  * Returns:
392ac9bb7b7SChris Wilson  * True if the allocator is completely free, false if there's still a node
393ac9bb7b7SChris Wilson  * allocated in it.
394ac9bb7b7SChris Wilson  */
395ac9bb7b7SChris Wilson static inline bool drm_mm_clean(const struct drm_mm *mm)
396ac9bb7b7SChris Wilson {
397ac9bb7b7SChris Wilson 	return list_empty(drm_mm_nodes(mm));
398ac9bb7b7SChris Wilson }
399249d6048SJerome Glisse 
400202b52b7SChris Wilson struct drm_mm_node *
40145b186f1SChris Wilson __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
402202b52b7SChris Wilson 
403522e85ddSChris Wilson /**
404522e85ddSChris Wilson  * drm_mm_for_each_node_in_range - iterator to walk over a range of
405522e85ddSChris Wilson  * allocated nodes
4068b2fb7b6SChris Wilson  * @node__: drm_mm_node structure to assign to in each iteration step
4078b2fb7b6SChris Wilson  * @mm__: drm_mm allocator to walk
4088b2fb7b6SChris Wilson  * @start__: starting offset, the first node will overlap this
4098b2fb7b6SChris Wilson  * @end__: ending offset, the last node will start before this (but may overlap)
410522e85ddSChris Wilson  *
411522e85ddSChris Wilson  * This iterator walks over all nodes in the range allocator that lie
412522e85ddSChris Wilson  * between @start and @end. It is implemented similarly to list_for_each(),
413522e85ddSChris Wilson  * but using the internal interval tree to accelerate the search for the
414522e85ddSChris Wilson  * starting node, and so not safe against removal of elements. It assumes
415522e85ddSChris Wilson  * that @end is within (or is the upper limit of) the drm_mm allocator.
416522e85ddSChris Wilson  */
4178b2fb7b6SChris Wilson #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__)	\
4188b2fb7b6SChris Wilson 	for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
4198b2fb7b6SChris Wilson 	     node__ && node__->start < (end__);				\
4208b2fb7b6SChris Wilson 	     node__ = list_next_entry(node__, node_list))
421202b52b7SChris Wilson 
4229a71e277SChris Wilson void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
4239a71e277SChris Wilson 				 struct drm_mm *mm,
4240b04d474SChris Wilson 				 u64 size, u64 alignment, unsigned long color,
4250b04d474SChris Wilson 				 u64 start, u64 end,
4260b04d474SChris Wilson 				 unsigned int flags);
4272c4b3895SChris Wilson 
4282c4b3895SChris Wilson /**
4292c4b3895SChris Wilson  * drm_mm_scan_init - initialize lru scanning
4302c4b3895SChris Wilson  * @scan: scan state
4312c4b3895SChris Wilson  * @mm: drm_mm to scan
4322c4b3895SChris Wilson  * @size: size of the allocation
4332c4b3895SChris Wilson  * @alignment: alignment of the allocation
4342c4b3895SChris Wilson  * @color: opaque tag value to use for the allocation
4350b04d474SChris Wilson  * @flags: flags to specify how the allocation will be performed afterwards
4362c4b3895SChris Wilson  *
4372c4b3895SChris Wilson  * This simply sets up the scanning routines with the parameters for the desired
4380b04d474SChris Wilson  * hole.
4392c4b3895SChris Wilson  *
4402c4b3895SChris Wilson  * Warning:
4412c4b3895SChris Wilson  * As long as the scan list is non-empty, no other operations than
4422c4b3895SChris Wilson  * adding/removing nodes to/from the scan list are allowed.
4432c4b3895SChris Wilson  */
4442c4b3895SChris Wilson static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
4452c4b3895SChris Wilson 				    struct drm_mm *mm,
4462c4b3895SChris Wilson 				    u64 size,
4472c4b3895SChris Wilson 				    u64 alignment,
4480b04d474SChris Wilson 				    unsigned long color,
4490b04d474SChris Wilson 				    unsigned int flags)
4502c4b3895SChris Wilson {
4510b04d474SChris Wilson 	drm_mm_scan_init_with_range(scan, mm,
4520b04d474SChris Wilson 				    size, alignment, color,
4530b04d474SChris Wilson 				    0, U64_MAX,
4540b04d474SChris Wilson 				    flags);
4552c4b3895SChris Wilson }
4562c4b3895SChris Wilson 
4579a71e277SChris Wilson bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
4589a71e277SChris Wilson 			   struct drm_mm_node *node);
4599a71e277SChris Wilson bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
4609a71e277SChris Wilson 			      struct drm_mm_node *node);
4613fa489daSChris Wilson struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
462709ea971SDaniel Vetter 
463b5c3714fSDaniel Vetter void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p);
464fa8a1238SDave Airlie 
465249d6048SJerome Glisse #endif
466