xref: /OK3568_Linux_fs/kernel/include/drm/drm_mm.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /**************************************************************************
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
4*4882a593Smuzhiyun  * Copyright 2016 Intel Corporation
5*4882a593Smuzhiyun  * All Rights Reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
8*4882a593Smuzhiyun  * copy of this software and associated documentation files (the
9*4882a593Smuzhiyun  * "Software"), to deal in the Software without restriction, including
10*4882a593Smuzhiyun  * without limitation the rights to use, copy, modify, merge, publish,
11*4882a593Smuzhiyun  * distribute, sub license, and/or sell copies of the Software, and to
12*4882a593Smuzhiyun  * permit persons to whom the Software is furnished to do so, subject to
13*4882a593Smuzhiyun  * the following conditions:
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
16*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial portions
17*4882a593Smuzhiyun  * of the Software.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23*4882a593Smuzhiyun  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24*4882a593Smuzhiyun  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25*4882a593Smuzhiyun  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  **************************************************************************/
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * Authors:
31*4882a593Smuzhiyun  * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #ifndef _DRM_MM_H_
35*4882a593Smuzhiyun #define _DRM_MM_H_
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun  * Generic range manager structs
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun #include <linux/bug.h>
41*4882a593Smuzhiyun #include <linux/rbtree.h>
42*4882a593Smuzhiyun #include <linux/kernel.h>
43*4882a593Smuzhiyun #include <linux/mm_types.h>
44*4882a593Smuzhiyun #include <linux/list.h>
45*4882a593Smuzhiyun #include <linux/spinlock.h>
46*4882a593Smuzhiyun #ifdef CONFIG_DRM_DEBUG_MM
47*4882a593Smuzhiyun #include <linux/stackdepot.h>
48*4882a593Smuzhiyun #endif
49*4882a593Smuzhiyun #include <drm/drm_print.h>
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #ifdef CONFIG_DRM_DEBUG_MM
52*4882a593Smuzhiyun #define DRM_MM_BUG_ON(expr) BUG_ON(expr)
53*4882a593Smuzhiyun #else
54*4882a593Smuzhiyun #define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /**
58*4882a593Smuzhiyun  * enum drm_mm_insert_mode - control search and allocation behaviour
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * The &struct drm_mm range manager supports finding a suitable modes using
61*4882a593Smuzhiyun  * a number of search trees. These trees are oranised by size, by address and
62*4882a593Smuzhiyun  * in most recent eviction order. This allows the user to find either the
63*4882a593Smuzhiyun  * smallest hole to reuse, the lowest or highest address to reuse, or simply
64*4882a593Smuzhiyun  * reuse the most recent eviction that fits. When allocating the &drm_mm_node
65*4882a593Smuzhiyun  * from within the hole, the &drm_mm_insert_mode also dictate whether to
66*4882a593Smuzhiyun  * allocate the lowest matching address or the highest.
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun enum drm_mm_insert_mode {
69*4882a593Smuzhiyun 	/**
70*4882a593Smuzhiyun 	 * @DRM_MM_INSERT_BEST:
71*4882a593Smuzhiyun 	 *
72*4882a593Smuzhiyun 	 * Search for the smallest hole (within the search range) that fits
73*4882a593Smuzhiyun 	 * the desired node.
74*4882a593Smuzhiyun 	 *
75*4882a593Smuzhiyun 	 * Allocates the node from the bottom of the found hole.
76*4882a593Smuzhiyun 	 */
77*4882a593Smuzhiyun 	DRM_MM_INSERT_BEST = 0,
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/**
80*4882a593Smuzhiyun 	 * @DRM_MM_INSERT_LOW:
81*4882a593Smuzhiyun 	 *
82*4882a593Smuzhiyun 	 * Search for the lowest hole (address closest to 0, within the search
83*4882a593Smuzhiyun 	 * range) that fits the desired node.
84*4882a593Smuzhiyun 	 *
85*4882a593Smuzhiyun 	 * Allocates the node from the bottom of the found hole.
86*4882a593Smuzhiyun 	 */
87*4882a593Smuzhiyun 	DRM_MM_INSERT_LOW,
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/**
90*4882a593Smuzhiyun 	 * @DRM_MM_INSERT_HIGH:
91*4882a593Smuzhiyun 	 *
92*4882a593Smuzhiyun 	 * Search for the highest hole (address closest to U64_MAX, within the
93*4882a593Smuzhiyun 	 * search range) that fits the desired node.
94*4882a593Smuzhiyun 	 *
95*4882a593Smuzhiyun 	 * Allocates the node from the *top* of the found hole. The specified
96*4882a593Smuzhiyun 	 * alignment for the node is applied to the base of the node
97*4882a593Smuzhiyun 	 * (&drm_mm_node.start).
98*4882a593Smuzhiyun 	 */
99*4882a593Smuzhiyun 	DRM_MM_INSERT_HIGH,
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/**
102*4882a593Smuzhiyun 	 * @DRM_MM_INSERT_EVICT:
103*4882a593Smuzhiyun 	 *
104*4882a593Smuzhiyun 	 * Search for the most recently evicted hole (within the search range)
105*4882a593Smuzhiyun 	 * that fits the desired node. This is appropriate for use immediately
106*4882a593Smuzhiyun 	 * after performing an eviction scan (see drm_mm_scan_init()) and
107*4882a593Smuzhiyun 	 * removing the selected nodes to form a hole.
108*4882a593Smuzhiyun 	 *
109*4882a593Smuzhiyun 	 * Allocates the node from the bottom of the found hole.
110*4882a593Smuzhiyun 	 */
111*4882a593Smuzhiyun 	DRM_MM_INSERT_EVICT,
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/**
114*4882a593Smuzhiyun 	 * @DRM_MM_INSERT_ONCE:
115*4882a593Smuzhiyun 	 *
116*4882a593Smuzhiyun 	 * Only check the first hole for suitablity and report -ENOSPC
117*4882a593Smuzhiyun 	 * immediately otherwise, rather than check every hole until a
118*4882a593Smuzhiyun 	 * suitable one is found. Can only be used in conjunction with another
119*4882a593Smuzhiyun 	 * search method such as DRM_MM_INSERT_HIGH or DRM_MM_INSERT_LOW.
120*4882a593Smuzhiyun 	 */
121*4882a593Smuzhiyun 	DRM_MM_INSERT_ONCE = BIT(31),
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/**
124*4882a593Smuzhiyun 	 * @DRM_MM_INSERT_HIGHEST:
125*4882a593Smuzhiyun 	 *
126*4882a593Smuzhiyun 	 * Only check the highest hole (the hole with the largest address) and
127*4882a593Smuzhiyun 	 * insert the node at the top of the hole or report -ENOSPC if
128*4882a593Smuzhiyun 	 * unsuitable.
129*4882a593Smuzhiyun 	 *
130*4882a593Smuzhiyun 	 * Does not search all holes.
131*4882a593Smuzhiyun 	 */
132*4882a593Smuzhiyun 	DRM_MM_INSERT_HIGHEST = DRM_MM_INSERT_HIGH | DRM_MM_INSERT_ONCE,
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	/**
135*4882a593Smuzhiyun 	 * @DRM_MM_INSERT_LOWEST:
136*4882a593Smuzhiyun 	 *
137*4882a593Smuzhiyun 	 * Only check the lowest hole (the hole with the smallest address) and
138*4882a593Smuzhiyun 	 * insert the node at the bottom of the hole or report -ENOSPC if
139*4882a593Smuzhiyun 	 * unsuitable.
140*4882a593Smuzhiyun 	 *
141*4882a593Smuzhiyun 	 * Does not search all holes.
142*4882a593Smuzhiyun 	 */
143*4882a593Smuzhiyun 	DRM_MM_INSERT_LOWEST  = DRM_MM_INSERT_LOW | DRM_MM_INSERT_ONCE,
144*4882a593Smuzhiyun };
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /**
147*4882a593Smuzhiyun  * struct drm_mm_node - allocated block in the DRM allocator
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * This represents an allocated block in a &drm_mm allocator. Except for
150*4882a593Smuzhiyun  * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is
151*4882a593Smuzhiyun  * entirely opaque and should only be accessed through the provided funcions.
152*4882a593Smuzhiyun  * Since allocation of these nodes is entirely handled by the driver they can be
153*4882a593Smuzhiyun  * embedded.
154*4882a593Smuzhiyun  */
155*4882a593Smuzhiyun struct drm_mm_node {
156*4882a593Smuzhiyun 	/** @color: Opaque driver-private tag. */
157*4882a593Smuzhiyun 	unsigned long color;
158*4882a593Smuzhiyun 	/** @start: Start address of the allocated block. */
159*4882a593Smuzhiyun 	u64 start;
160*4882a593Smuzhiyun 	/** @size: Size of the allocated block. */
161*4882a593Smuzhiyun 	u64 size;
162*4882a593Smuzhiyun 	/* private: */
163*4882a593Smuzhiyun 	struct drm_mm *mm;
164*4882a593Smuzhiyun 	struct list_head node_list;
165*4882a593Smuzhiyun 	struct list_head hole_stack;
166*4882a593Smuzhiyun 	struct rb_node rb;
167*4882a593Smuzhiyun 	struct rb_node rb_hole_size;
168*4882a593Smuzhiyun 	struct rb_node rb_hole_addr;
169*4882a593Smuzhiyun 	u64 __subtree_last;
170*4882a593Smuzhiyun 	u64 hole_size;
171*4882a593Smuzhiyun 	u64 subtree_max_hole;
172*4882a593Smuzhiyun 	unsigned long flags;
173*4882a593Smuzhiyun #define DRM_MM_NODE_ALLOCATED_BIT	0
174*4882a593Smuzhiyun #define DRM_MM_NODE_SCANNED_BIT		1
175*4882a593Smuzhiyun #ifdef CONFIG_DRM_DEBUG_MM
176*4882a593Smuzhiyun 	depot_stack_handle_t stack;
177*4882a593Smuzhiyun #endif
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /**
181*4882a593Smuzhiyun  * struct drm_mm - DRM allocator
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * DRM range allocator with a few special functions and features geared towards
184*4882a593Smuzhiyun  * managing GPU memory. Except for the @color_adjust callback the structure is
185*4882a593Smuzhiyun  * entirely opaque and should only be accessed through the provided functions
186*4882a593Smuzhiyun  * and macros. This structure can be embedded into larger driver structures.
187*4882a593Smuzhiyun  */
188*4882a593Smuzhiyun struct drm_mm {
189*4882a593Smuzhiyun 	/**
190*4882a593Smuzhiyun 	 * @color_adjust:
191*4882a593Smuzhiyun 	 *
192*4882a593Smuzhiyun 	 * Optional driver callback to further apply restrictions on a hole. The
193*4882a593Smuzhiyun 	 * node argument points at the node containing the hole from which the
194*4882a593Smuzhiyun 	 * block would be allocated (see drm_mm_hole_follows() and friends). The
195*4882a593Smuzhiyun 	 * other arguments are the size of the block to be allocated. The driver
196*4882a593Smuzhiyun 	 * can adjust the start and end as needed to e.g. insert guard pages.
197*4882a593Smuzhiyun 	 */
198*4882a593Smuzhiyun 	void (*color_adjust)(const struct drm_mm_node *node,
199*4882a593Smuzhiyun 			     unsigned long color,
200*4882a593Smuzhiyun 			     u64 *start, u64 *end);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* private: */
203*4882a593Smuzhiyun 	/* List of all memory nodes that immediately precede a free hole. */
204*4882a593Smuzhiyun 	struct list_head hole_stack;
205*4882a593Smuzhiyun 	/* head_node.node_list is the list of all memory nodes, ordered
206*4882a593Smuzhiyun 	 * according to the (increasing) start address of the memory node. */
207*4882a593Smuzhiyun 	struct drm_mm_node head_node;
208*4882a593Smuzhiyun 	/* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
209*4882a593Smuzhiyun 	struct rb_root_cached interval_tree;
210*4882a593Smuzhiyun 	struct rb_root_cached holes_size;
211*4882a593Smuzhiyun 	struct rb_root holes_addr;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	unsigned long scan_active;
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /**
217*4882a593Smuzhiyun  * struct drm_mm_scan - DRM allocator eviction roaster data
218*4882a593Smuzhiyun  *
219*4882a593Smuzhiyun  * This structure tracks data needed for the eviction roaster set up using
220*4882a593Smuzhiyun  * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and
221*4882a593Smuzhiyun  * drm_mm_scan_remove_block(). The structure is entirely opaque and should only
222*4882a593Smuzhiyun  * be accessed through the provided functions and macros. It is meant to be
223*4882a593Smuzhiyun  * allocated temporarily by the driver on the stack.
224*4882a593Smuzhiyun  */
225*4882a593Smuzhiyun struct drm_mm_scan {
226*4882a593Smuzhiyun 	/* private: */
227*4882a593Smuzhiyun 	struct drm_mm *mm;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	u64 size;
230*4882a593Smuzhiyun 	u64 alignment;
231*4882a593Smuzhiyun 	u64 remainder_mask;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	u64 range_start;
234*4882a593Smuzhiyun 	u64 range_end;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	u64 hit_start;
237*4882a593Smuzhiyun 	u64 hit_end;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	unsigned long color;
240*4882a593Smuzhiyun 	enum drm_mm_insert_mode mode;
241*4882a593Smuzhiyun };
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun /**
244*4882a593Smuzhiyun  * drm_mm_node_allocated - checks whether a node is allocated
245*4882a593Smuzhiyun  * @node: drm_mm_node to check
246*4882a593Smuzhiyun  *
247*4882a593Smuzhiyun  * Drivers are required to clear a node prior to using it with the
248*4882a593Smuzhiyun  * drm_mm range manager.
249*4882a593Smuzhiyun  *
250*4882a593Smuzhiyun  * Drivers should use this helper for proper encapsulation of drm_mm
251*4882a593Smuzhiyun  * internals.
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * Returns:
254*4882a593Smuzhiyun  * True if the @node is allocated.
255*4882a593Smuzhiyun  */
drm_mm_node_allocated(const struct drm_mm_node * node)256*4882a593Smuzhiyun static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun 	return test_bit(DRM_MM_NODE_ALLOCATED_BIT, &node->flags);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun /**
262*4882a593Smuzhiyun  * drm_mm_initialized - checks whether an allocator is initialized
263*4882a593Smuzhiyun  * @mm: drm_mm to check
264*4882a593Smuzhiyun  *
265*4882a593Smuzhiyun  * Drivers should clear the struct drm_mm prior to initialisation if they
266*4882a593Smuzhiyun  * want to use this function.
267*4882a593Smuzhiyun  *
268*4882a593Smuzhiyun  * Drivers should use this helper for proper encapsulation of drm_mm
269*4882a593Smuzhiyun  * internals.
270*4882a593Smuzhiyun  *
271*4882a593Smuzhiyun  * Returns:
272*4882a593Smuzhiyun  * True if the @mm is initialized.
273*4882a593Smuzhiyun  */
drm_mm_initialized(const struct drm_mm * mm)274*4882a593Smuzhiyun static inline bool drm_mm_initialized(const struct drm_mm *mm)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	return READ_ONCE(mm->hole_stack.next);
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun /**
280*4882a593Smuzhiyun  * drm_mm_hole_follows - checks whether a hole follows this node
281*4882a593Smuzhiyun  * @node: drm_mm_node to check
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
284*4882a593Smuzhiyun  * If you wish to know whether a hole follows this particular node,
285*4882a593Smuzhiyun  * query this function. See also drm_mm_hole_node_start() and
286*4882a593Smuzhiyun  * drm_mm_hole_node_end().
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  * Returns:
289*4882a593Smuzhiyun  * True if a hole follows the @node.
290*4882a593Smuzhiyun  */
drm_mm_hole_follows(const struct drm_mm_node * node)291*4882a593Smuzhiyun static inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	return node->hole_size;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
__drm_mm_hole_node_start(const struct drm_mm_node * hole_node)296*4882a593Smuzhiyun static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	return hole_node->start + hole_node->size;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun  * drm_mm_hole_node_start - computes the start of the hole following @node
303*4882a593Smuzhiyun  * @hole_node: drm_mm_node which implicitly tracks the following hole
304*4882a593Smuzhiyun  *
305*4882a593Smuzhiyun  * This is useful for driver-specific debug dumpers. Otherwise drivers should
306*4882a593Smuzhiyun  * not inspect holes themselves. Drivers must check first whether a hole indeed
307*4882a593Smuzhiyun  * follows by looking at drm_mm_hole_follows()
308*4882a593Smuzhiyun  *
309*4882a593Smuzhiyun  * Returns:
310*4882a593Smuzhiyun  * Start of the subsequent hole.
311*4882a593Smuzhiyun  */
drm_mm_hole_node_start(const struct drm_mm_node * hole_node)312*4882a593Smuzhiyun static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
313*4882a593Smuzhiyun {
314*4882a593Smuzhiyun 	DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
315*4882a593Smuzhiyun 	return __drm_mm_hole_node_start(hole_node);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
__drm_mm_hole_node_end(const struct drm_mm_node * hole_node)318*4882a593Smuzhiyun static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	return list_next_entry(hole_node, node_list)->start;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun /**
324*4882a593Smuzhiyun  * drm_mm_hole_node_end - computes the end of the hole following @node
325*4882a593Smuzhiyun  * @hole_node: drm_mm_node which implicitly tracks the following hole
326*4882a593Smuzhiyun  *
327*4882a593Smuzhiyun  * This is useful for driver-specific debug dumpers. Otherwise drivers should
328*4882a593Smuzhiyun  * not inspect holes themselves. Drivers must check first whether a hole indeed
329*4882a593Smuzhiyun  * follows by looking at drm_mm_hole_follows().
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  * Returns:
332*4882a593Smuzhiyun  * End of the subsequent hole.
333*4882a593Smuzhiyun  */
drm_mm_hole_node_end(const struct drm_mm_node * hole_node)334*4882a593Smuzhiyun static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	return __drm_mm_hole_node_end(hole_node);
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun /**
340*4882a593Smuzhiyun  * drm_mm_nodes - list of nodes under the drm_mm range manager
341*4882a593Smuzhiyun  * @mm: the struct drm_mm range manager
342*4882a593Smuzhiyun  *
343*4882a593Smuzhiyun  * As the drm_mm range manager hides its node_list deep with its
344*4882a593Smuzhiyun  * structure, extracting it looks painful and repetitive. This is
345*4882a593Smuzhiyun  * not expected to be used outside of the drm_mm_for_each_node()
346*4882a593Smuzhiyun  * macros and similar internal functions.
347*4882a593Smuzhiyun  *
348*4882a593Smuzhiyun  * Returns:
349*4882a593Smuzhiyun  * The node list, may be empty.
350*4882a593Smuzhiyun  */
351*4882a593Smuzhiyun #define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun /**
354*4882a593Smuzhiyun  * drm_mm_for_each_node - iterator to walk over all allocated nodes
355*4882a593Smuzhiyun  * @entry: &struct drm_mm_node to assign to in each iteration step
356*4882a593Smuzhiyun  * @mm: &drm_mm allocator to walk
357*4882a593Smuzhiyun  *
358*4882a593Smuzhiyun  * This iterator walks over all nodes in the range allocator. It is implemented
359*4882a593Smuzhiyun  * with list_for_each(), so not save against removal of elements.
360*4882a593Smuzhiyun  */
361*4882a593Smuzhiyun #define drm_mm_for_each_node(entry, mm) \
362*4882a593Smuzhiyun 	list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun /**
365*4882a593Smuzhiyun  * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
366*4882a593Smuzhiyun  * @entry: &struct drm_mm_node to assign to in each iteration step
367*4882a593Smuzhiyun  * @next: &struct drm_mm_node to store the next step
368*4882a593Smuzhiyun  * @mm: &drm_mm allocator to walk
369*4882a593Smuzhiyun  *
370*4882a593Smuzhiyun  * This iterator walks over all nodes in the range allocator. It is implemented
371*4882a593Smuzhiyun  * with list_for_each_safe(), so save against removal of elements.
372*4882a593Smuzhiyun  */
373*4882a593Smuzhiyun #define drm_mm_for_each_node_safe(entry, next, mm) \
374*4882a593Smuzhiyun 	list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun /**
377*4882a593Smuzhiyun  * drm_mm_for_each_hole - iterator to walk over all holes
378*4882a593Smuzhiyun  * @pos: &drm_mm_node used internally to track progress
379*4882a593Smuzhiyun  * @mm: &drm_mm allocator to walk
380*4882a593Smuzhiyun  * @hole_start: ulong variable to assign the hole start to on each iteration
381*4882a593Smuzhiyun  * @hole_end: ulong variable to assign the hole end to on each iteration
382*4882a593Smuzhiyun  *
383*4882a593Smuzhiyun  * This iterator walks over all holes in the range allocator. It is implemented
384*4882a593Smuzhiyun  * with list_for_each(), so not save against removal of elements. @entry is used
385*4882a593Smuzhiyun  * internally and will not reflect a real drm_mm_node for the very first hole.
386*4882a593Smuzhiyun  * Hence users of this iterator may not access it.
387*4882a593Smuzhiyun  *
388*4882a593Smuzhiyun  * Implementation Note:
389*4882a593Smuzhiyun  * We need to inline list_for_each_entry in order to be able to set hole_start
390*4882a593Smuzhiyun  * and hole_end on each iteration while keeping the macro sane.
391*4882a593Smuzhiyun  */
392*4882a593Smuzhiyun #define drm_mm_for_each_hole(pos, mm, hole_start, hole_end) \
393*4882a593Smuzhiyun 	for (pos = list_first_entry(&(mm)->hole_stack, \
394*4882a593Smuzhiyun 				    typeof(*pos), hole_stack); \
395*4882a593Smuzhiyun 	     &pos->hole_stack != &(mm)->hole_stack ? \
396*4882a593Smuzhiyun 	     hole_start = drm_mm_hole_node_start(pos), \
397*4882a593Smuzhiyun 	     hole_end = hole_start + pos->hole_size, \
398*4882a593Smuzhiyun 	     1 : 0; \
399*4882a593Smuzhiyun 	     pos = list_next_entry(pos, hole_stack))
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun /*
402*4882a593Smuzhiyun  * Basic range manager support (drm_mm.c)
403*4882a593Smuzhiyun  */
404*4882a593Smuzhiyun int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
405*4882a593Smuzhiyun int drm_mm_insert_node_in_range(struct drm_mm *mm,
406*4882a593Smuzhiyun 				struct drm_mm_node *node,
407*4882a593Smuzhiyun 				u64 size,
408*4882a593Smuzhiyun 				u64 alignment,
409*4882a593Smuzhiyun 				unsigned long color,
410*4882a593Smuzhiyun 				u64 start,
411*4882a593Smuzhiyun 				u64 end,
412*4882a593Smuzhiyun 				enum drm_mm_insert_mode mode);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun /**
415*4882a593Smuzhiyun  * drm_mm_insert_node_generic - search for space and insert @node
416*4882a593Smuzhiyun  * @mm: drm_mm to allocate from
417*4882a593Smuzhiyun  * @node: preallocate node to insert
418*4882a593Smuzhiyun  * @size: size of the allocation
419*4882a593Smuzhiyun  * @alignment: alignment of the allocation
420*4882a593Smuzhiyun  * @color: opaque tag value to use for this node
421*4882a593Smuzhiyun  * @mode: fine-tune the allocation search and placement
422*4882a593Smuzhiyun  *
423*4882a593Smuzhiyun  * This is a simplified version of drm_mm_insert_node_in_range() with no
424*4882a593Smuzhiyun  * range restrictions applied.
425*4882a593Smuzhiyun  *
426*4882a593Smuzhiyun  * The preallocated node must be cleared to 0.
427*4882a593Smuzhiyun  *
428*4882a593Smuzhiyun  * Returns:
429*4882a593Smuzhiyun  * 0 on success, -ENOSPC if there's no suitable hole.
430*4882a593Smuzhiyun  */
431*4882a593Smuzhiyun static inline int
drm_mm_insert_node_generic(struct drm_mm * mm,struct drm_mm_node * node,u64 size,u64 alignment,unsigned long color,enum drm_mm_insert_mode mode)432*4882a593Smuzhiyun drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
433*4882a593Smuzhiyun 			   u64 size, u64 alignment,
434*4882a593Smuzhiyun 			   unsigned long color,
435*4882a593Smuzhiyun 			   enum drm_mm_insert_mode mode)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	return drm_mm_insert_node_in_range(mm, node,
438*4882a593Smuzhiyun 					   size, alignment, color,
439*4882a593Smuzhiyun 					   0, U64_MAX, mode);
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun /**
443*4882a593Smuzhiyun  * drm_mm_insert_node - search for space and insert @node
444*4882a593Smuzhiyun  * @mm: drm_mm to allocate from
445*4882a593Smuzhiyun  * @node: preallocate node to insert
446*4882a593Smuzhiyun  * @size: size of the allocation
447*4882a593Smuzhiyun  *
448*4882a593Smuzhiyun  * This is a simplified version of drm_mm_insert_node_generic() with @color set
449*4882a593Smuzhiyun  * to 0.
450*4882a593Smuzhiyun  *
451*4882a593Smuzhiyun  * The preallocated node must be cleared to 0.
452*4882a593Smuzhiyun  *
453*4882a593Smuzhiyun  * Returns:
454*4882a593Smuzhiyun  * 0 on success, -ENOSPC if there's no suitable hole.
455*4882a593Smuzhiyun  */
drm_mm_insert_node(struct drm_mm * mm,struct drm_mm_node * node,u64 size)456*4882a593Smuzhiyun static inline int drm_mm_insert_node(struct drm_mm *mm,
457*4882a593Smuzhiyun 				     struct drm_mm_node *node,
458*4882a593Smuzhiyun 				     u64 size)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	return drm_mm_insert_node_generic(mm, node, size, 0, 0, 0);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun void drm_mm_remove_node(struct drm_mm_node *node);
464*4882a593Smuzhiyun void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
465*4882a593Smuzhiyun void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
466*4882a593Smuzhiyun void drm_mm_takedown(struct drm_mm *mm);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun /**
469*4882a593Smuzhiyun  * drm_mm_clean - checks whether an allocator is clean
470*4882a593Smuzhiyun  * @mm: drm_mm allocator to check
471*4882a593Smuzhiyun  *
472*4882a593Smuzhiyun  * Returns:
473*4882a593Smuzhiyun  * True if the allocator is completely free, false if there's still a node
474*4882a593Smuzhiyun  * allocated in it.
475*4882a593Smuzhiyun  */
drm_mm_clean(const struct drm_mm * mm)476*4882a593Smuzhiyun static inline bool drm_mm_clean(const struct drm_mm *mm)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	return list_empty(drm_mm_nodes(mm));
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun struct drm_mm_node *
482*4882a593Smuzhiyun __drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun /**
485*4882a593Smuzhiyun  * drm_mm_for_each_node_in_range - iterator to walk over a range of
486*4882a593Smuzhiyun  * allocated nodes
487*4882a593Smuzhiyun  * @node__: drm_mm_node structure to assign to in each iteration step
488*4882a593Smuzhiyun  * @mm__: drm_mm allocator to walk
489*4882a593Smuzhiyun  * @start__: starting offset, the first node will overlap this
490*4882a593Smuzhiyun  * @end__: ending offset, the last node will start before this (but may overlap)
491*4882a593Smuzhiyun  *
492*4882a593Smuzhiyun  * This iterator walks over all nodes in the range allocator that lie
493*4882a593Smuzhiyun  * between @start and @end. It is implemented similarly to list_for_each(),
494*4882a593Smuzhiyun  * but using the internal interval tree to accelerate the search for the
495*4882a593Smuzhiyun  * starting node, and so not safe against removal of elements. It assumes
496*4882a593Smuzhiyun  * that @end is within (or is the upper limit of) the drm_mm allocator.
497*4882a593Smuzhiyun  * If [@start, @end] are beyond the range of the drm_mm, the iterator may walk
498*4882a593Smuzhiyun  * over the special _unallocated_ &drm_mm.head_node, and may even continue
499*4882a593Smuzhiyun  * indefinitely.
500*4882a593Smuzhiyun  */
501*4882a593Smuzhiyun #define drm_mm_for_each_node_in_range(node__, mm__, start__, end__)	\
502*4882a593Smuzhiyun 	for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
503*4882a593Smuzhiyun 	     node__->start < (end__);					\
504*4882a593Smuzhiyun 	     node__ = list_next_entry(node__, node_list))
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
507*4882a593Smuzhiyun 				 struct drm_mm *mm,
508*4882a593Smuzhiyun 				 u64 size, u64 alignment, unsigned long color,
509*4882a593Smuzhiyun 				 u64 start, u64 end,
510*4882a593Smuzhiyun 				 enum drm_mm_insert_mode mode);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun /**
513*4882a593Smuzhiyun  * drm_mm_scan_init - initialize lru scanning
514*4882a593Smuzhiyun  * @scan: scan state
515*4882a593Smuzhiyun  * @mm: drm_mm to scan
516*4882a593Smuzhiyun  * @size: size of the allocation
517*4882a593Smuzhiyun  * @alignment: alignment of the allocation
518*4882a593Smuzhiyun  * @color: opaque tag value to use for the allocation
519*4882a593Smuzhiyun  * @mode: fine-tune the allocation search and placement
520*4882a593Smuzhiyun  *
521*4882a593Smuzhiyun  * This is a simplified version of drm_mm_scan_init_with_range() with no range
522*4882a593Smuzhiyun  * restrictions applied.
523*4882a593Smuzhiyun  *
524*4882a593Smuzhiyun  * This simply sets up the scanning routines with the parameters for the desired
525*4882a593Smuzhiyun  * hole.
526*4882a593Smuzhiyun  *
527*4882a593Smuzhiyun  * Warning:
528*4882a593Smuzhiyun  * As long as the scan list is non-empty, no other operations than
529*4882a593Smuzhiyun  * adding/removing nodes to/from the scan list are allowed.
530*4882a593Smuzhiyun  */
drm_mm_scan_init(struct drm_mm_scan * scan,struct drm_mm * mm,u64 size,u64 alignment,unsigned long color,enum drm_mm_insert_mode mode)531*4882a593Smuzhiyun static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
532*4882a593Smuzhiyun 				    struct drm_mm *mm,
533*4882a593Smuzhiyun 				    u64 size,
534*4882a593Smuzhiyun 				    u64 alignment,
535*4882a593Smuzhiyun 				    unsigned long color,
536*4882a593Smuzhiyun 				    enum drm_mm_insert_mode mode)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun 	drm_mm_scan_init_with_range(scan, mm,
539*4882a593Smuzhiyun 				    size, alignment, color,
540*4882a593Smuzhiyun 				    0, U64_MAX, mode);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
544*4882a593Smuzhiyun 			   struct drm_mm_node *node);
545*4882a593Smuzhiyun bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
546*4882a593Smuzhiyun 			      struct drm_mm_node *node);
547*4882a593Smuzhiyun struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun #endif
552