blob: 3bddca8fd2b536e481fb860862db2068d640426b [file] [log] [blame]
Jerome Glisse249d6042009-04-08 17:11:16 +02001/**************************************************************************
2 *
3 * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
Chris Wilsonba004e32016-12-22 08:36:25 +00004 * Copyright 2016 Intel Corporation
Jerome Glisse249d6042009-04-08 17:11:16 +02005 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 *
28 **************************************************************************/
29/*
30 * Authors:
31 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32 */
33
34#ifndef _DRM_MM_H_
35#define _DRM_MM_H_
36
37/*
38 * Generic range manager structs
39 */
David Herrmann86e81f0e2013-07-25 18:02:31 +020040#include <linux/bug.h>
Chris Wilson202b52b2016-08-03 16:04:09 +010041#include <linux/rbtree.h>
David Herrmann86e81f0e2013-07-25 18:02:31 +020042#include <linux/kernel.h>
Jerome Glisse249d6042009-04-08 17:11:16 +020043#include <linux/list.h>
David Herrmann86e81f0e2013-07-25 18:02:31 +020044#include <linux/spinlock.h>
Chris Wilson57056702016-10-31 09:08:06 +000045#ifdef CONFIG_DRM_DEBUG_MM
46#include <linux/stackdepot.h>
47#endif
Daniel Vetterb5c37142016-12-29 12:09:24 +010048#include <drm/drm_print.h>
Jerome Glisse249d6042009-04-08 17:11:16 +020049
Chris Wilsonb3ee9632016-12-22 08:36:06 +000050#ifdef CONFIG_DRM_DEBUG_MM
51#define DRM_MM_BUG_ON(expr) BUG_ON(expr)
52#else
53#define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
54#endif
55
David Herrmann31e5d7c2013-07-27 13:36:27 +020056enum drm_mm_search_flags {
57 DRM_MM_SEARCH_DEFAULT = 0,
58 DRM_MM_SEARCH_BEST = 1 << 0,
Lauri Kasanen62347f92014-04-02 20:03:57 +030059 DRM_MM_SEARCH_BELOW = 1 << 1,
David Herrmann31e5d7c2013-07-27 13:36:27 +020060};
61
Lauri Kasanen62347f92014-04-02 20:03:57 +030062enum drm_mm_allocator_flags {
63 DRM_MM_CREATE_DEFAULT = 0,
64 DRM_MM_CREATE_TOP = 1 << 0,
65};
66
67#define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT
68#define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP
69
Daniel Vetter05fc0322016-12-29 21:48:23 +010070/**
71 * struct drm_mm_node - allocated block in the DRM allocator
72 *
73 * This represents an allocated block in a &drm_mm allocator. Except for
74 * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is
75 * entirely opaque and should only be accessed through the provided funcions.
76 * Since allocation of these nodes is entirely handled by the driver they can be
77 * embedded.
78 */
Jerome Glisse249d6042009-04-08 17:11:16 +020079struct drm_mm_node {
Daniel Vetter05fc0322016-12-29 21:48:23 +010080 /** @color: Opaque driver-private tag. */
81 unsigned long color;
82 /** @start: Start address of the allocated block. */
83 u64 start;
84 /** @size: Size of the allocated block. */
85 u64 size;
86 /* private: */
Daniel Vetterd1024ce2010-07-02 15:02:14 +010087 struct list_head node_list;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010088 struct list_head hole_stack;
Chris Wilson202b52b2016-08-03 16:04:09 +010089 struct rb_node rb;
Daniel Vetterea7b1dd2011-02-18 17:59:12 +010090 unsigned hole_follows : 1;
Daniel Vetterb0b7af12011-02-18 17:59:14 +010091 unsigned allocated : 1;
Chris Wilsonf29051f2016-12-22 08:36:35 +000092 bool scanned_block : 1;
Chris Wilson202b52b2016-08-03 16:04:09 +010093 u64 __subtree_last;
Jerome Glisse249d6042009-04-08 17:11:16 +020094 struct drm_mm *mm;
Chris Wilson57056702016-10-31 09:08:06 +000095#ifdef CONFIG_DRM_DEBUG_MM
96 depot_stack_handle_t stack;
97#endif
Jerome Glisse249d6042009-04-08 17:11:16 +020098};
99
Daniel Vetter05fc0322016-12-29 21:48:23 +0100100/**
101 * struct drm_mm - DRM allocator
102 *
103 * DRM range allocator with a few special functions and features geared towards
104 * managing GPU memory. Except for the @color_adjust callback the structure is
105 * entirely opaque and should only be accessed through the provided functions
106 * and macros. This structure can be embedded into larger driver structures.
107 */
Jerome Glisse249d6042009-04-08 17:11:16 +0200108struct drm_mm {
Daniel Vetter05fc0322016-12-29 21:48:23 +0100109 /**
110 * @color_adjust:
111 *
112 * Optional driver callback to further apply restrictions on a hole. The
113 * node argument points at the node containing the hole from which the
114 * block would be allocated (see drm_mm_hole_follows() and friends). The
115 * other arguments are the size of the block to be allocated. The driver
116 * can adjust the start and end as needed to e.g. insert guard pages.
117 */
118 void (*color_adjust)(const struct drm_mm_node *node,
119 unsigned long color,
120 u64 *start, u64 *end);
121
122 /* private: */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300123 /* List of all memory nodes that immediately precede a free hole. */
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100124 struct list_head hole_stack;
125 /* head_node.node_list is the list of all memory nodes, ordered
126 * according to the (increasing) start address of the memory node. */
127 struct drm_mm_node head_node;
Chris Wilson202b52b2016-08-03 16:04:09 +0100128 /* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
129 struct rb_root interval_tree;
130
Chris Wilson9a71e272016-12-22 08:36:29 +0000131 unsigned long scan_active;
132};
133
Daniel Vetter05fc0322016-12-29 21:48:23 +0100134/**
135 * struct drm_mm_scan - DRM allocator eviction roaster data
136 *
137 * This structure tracks data needed for the eviction roaster set up using
138 * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and
139 * drm_mm_scan_remove_block(). The structure is entirely opaque and should only
140 * be accessed through the provided functions and macros. It is meant to be
141 * allocated temporarily by the driver on the stack.
142 */
Chris Wilson9a71e272016-12-22 08:36:29 +0000143struct drm_mm_scan {
Daniel Vetter05fc0322016-12-29 21:48:23 +0100144 /* private: */
Chris Wilson9a71e272016-12-22 08:36:29 +0000145 struct drm_mm *mm;
146
147 u64 size;
148 u64 alignment;
Chris Wilson9a956b12016-12-22 08:36:34 +0000149 u64 remainder_mask;
Chris Wilson9a71e272016-12-22 08:36:29 +0000150
151 u64 range_start;
152 u64 range_end;
153
154 u64 hit_start;
155 u64 hit_end;
156
Chris Wilson9a71e272016-12-22 08:36:29 +0000157 unsigned long color;
Chris Wilson0b04d472016-12-22 08:36:33 +0000158 unsigned int flags;
Jerome Glisse249d6042009-04-08 17:11:16 +0200159};
160
Daniel Vettere18c0412014-01-23 00:39:13 +0100161/**
162 * drm_mm_node_allocated - checks whether a node is allocated
163 * @node: drm_mm_node to check
164 *
Chris Wilsonba004e32016-12-22 08:36:25 +0000165 * Drivers are required to clear a node prior to using it with the
166 * drm_mm range manager.
167 *
168 * Drivers should use this helper for proper encapsulation of drm_mm
Daniel Vettere18c0412014-01-23 00:39:13 +0100169 * internals.
170 *
171 * Returns:
172 * True if the @node is allocated.
173 */
Chris Wilson45b186f2016-12-16 07:46:42 +0000174static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
Daniel Vetterb0b7af12011-02-18 17:59:14 +0100175{
176 return node->allocated;
177}
178
Daniel Vettere18c0412014-01-23 00:39:13 +0100179/**
180 * drm_mm_initialized - checks whether an allocator is initialized
181 * @mm: drm_mm to check
182 *
Chris Wilsonba004e32016-12-22 08:36:25 +0000183 * Drivers should clear the struct drm_mm prior to initialisation if they
184 * want to use this function.
185 *
186 * Drivers should use this helper for proper encapsulation of drm_mm
Daniel Vettere18c0412014-01-23 00:39:13 +0100187 * internals.
188 *
189 * Returns:
190 * True if the @mm is initialized.
191 */
Chris Wilson45b186f2016-12-16 07:46:42 +0000192static inline bool drm_mm_initialized(const struct drm_mm *mm)
Daniel Vetter31a5b8c2011-02-18 17:59:11 +0100193{
Daniel Vetterea7b1dd2011-02-18 17:59:12 +0100194 return mm->hole_stack.next;
Daniel Vetter31a5b8c2011-02-18 17:59:11 +0100195}
Chris Wilson9e8944a2012-11-15 11:32:17 +0000196
Chris Wilson3f85fb32016-12-22 08:36:37 +0000197/**
198 * drm_mm_hole_follows - checks whether a hole follows this node
199 * @node: drm_mm_node to check
200 *
201 * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
202 * If you wish to know whether a hole follows this particular node,
Daniel Vetter05fc0322016-12-29 21:48:23 +0100203 * query this function. See also drm_mm_hole_node_start() and
204 * drm_mm_hole_node_end().
Chris Wilson3f85fb32016-12-22 08:36:37 +0000205 *
206 * Returns:
207 * True if a hole follows the @node.
208 */
209static inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
210{
211 return node->hole_follows;
212}
213
Chris Wilson45b186f2016-12-16 07:46:42 +0000214static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
Chris Wilson9e8944a2012-11-15 11:32:17 +0000215{
216 return hole_node->start + hole_node->size;
217}
218
Daniel Vettere18c0412014-01-23 00:39:13 +0100219/**
220 * drm_mm_hole_node_start - computes the start of the hole following @node
221 * @hole_node: drm_mm_node which implicitly tracks the following hole
222 *
Chris Wilsonba004e32016-12-22 08:36:25 +0000223 * This is useful for driver-specific debug dumpers. Otherwise drivers should
224 * not inspect holes themselves. Drivers must check first whether a hole indeed
Chris Wilson3f85fb32016-12-22 08:36:37 +0000225 * follows by looking at drm_mm_hole_follows()
Daniel Vettere18c0412014-01-23 00:39:13 +0100226 *
227 * Returns:
228 * Start of the subsequent hole.
229 */
Chris Wilson45b186f2016-12-16 07:46:42 +0000230static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
Chris Wilson9e8944a2012-11-15 11:32:17 +0000231{
Chris Wilson3f85fb32016-12-22 08:36:37 +0000232 DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
Chris Wilson9e8944a2012-11-15 11:32:17 +0000233 return __drm_mm_hole_node_start(hole_node);
234}
235
Chris Wilson45b186f2016-12-16 07:46:42 +0000236static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
Chris Wilson9e8944a2012-11-15 11:32:17 +0000237{
Geliang Tang87069f42015-11-25 21:23:07 +0800238 return list_next_entry(hole_node, node_list)->start;
Chris Wilson9e8944a2012-11-15 11:32:17 +0000239}
240
Daniel Vettere18c0412014-01-23 00:39:13 +0100241/**
242 * drm_mm_hole_node_end - computes the end of the hole following @node
243 * @hole_node: drm_mm_node which implicitly tracks the following hole
244 *
Chris Wilsonba004e32016-12-22 08:36:25 +0000245 * This is useful for driver-specific debug dumpers. Otherwise drivers should
246 * not inspect holes themselves. Drivers must check first whether a hole indeed
Chris Wilson3f85fb32016-12-22 08:36:37 +0000247 * follows by looking at drm_mm_hole_follows().
Daniel Vettere18c0412014-01-23 00:39:13 +0100248 *
249 * Returns:
250 * End of the subsequent hole.
251 */
Chris Wilson45b186f2016-12-16 07:46:42 +0000252static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
Chris Wilson9e8944a2012-11-15 11:32:17 +0000253{
254 return __drm_mm_hole_node_end(hole_node);
255}
256
Chris Wilson2bc98c82016-12-22 08:36:05 +0000257/**
258 * drm_mm_nodes - list of nodes under the drm_mm range manager
259 * @mm: the struct drm_mm range manger
260 *
261 * As the drm_mm range manager hides its node_list deep with its
262 * structure, extracting it looks painful and repetitive. This is
263 * not expected to be used outside of the drm_mm_for_each_node()
264 * macros and similar internal functions.
265 *
266 * Returns:
267 * The node list, may be empty.
268 */
269#define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
Chris Wilsonad579002016-12-16 07:46:41 +0000270
Daniel Vettere18c0412014-01-23 00:39:13 +0100271/**
272 * drm_mm_for_each_node - iterator to walk over all allocated nodes
Daniel Vetter05fc0322016-12-29 21:48:23 +0100273 * @entry: &struct drm_mm_node to assign to in each iteration step
274 * @mm: &drm_mm allocator to walk
Daniel Vettere18c0412014-01-23 00:39:13 +0100275 *
276 * This iterator walks over all nodes in the range allocator. It is implemented
Daniel Vetter05fc0322016-12-29 21:48:23 +0100277 * with list_for_each(), so not save against removal of elements.
Daniel Vettere18c0412014-01-23 00:39:13 +0100278 */
Chris Wilsonad579002016-12-16 07:46:41 +0000279#define drm_mm_for_each_node(entry, mm) \
Chris Wilson2bc98c82016-12-22 08:36:05 +0000280 list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
Chris Wilsonad579002016-12-16 07:46:41 +0000281
282/**
283 * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
Daniel Vetter05fc0322016-12-29 21:48:23 +0100284 * @entry: &struct drm_mm_node to assign to in each iteration step
285 * @next: &struct drm_mm_node to store the next step
286 * @mm: &drm_mm allocator to walk
Chris Wilsonad579002016-12-16 07:46:41 +0000287 *
288 * This iterator walks over all nodes in the range allocator. It is implemented
Daniel Vetter05fc0322016-12-29 21:48:23 +0100289 * with list_for_each_safe(), so save against removal of elements.
Chris Wilsonad579002016-12-16 07:46:41 +0000290 */
291#define drm_mm_for_each_node_safe(entry, next, mm) \
Chris Wilson2bc98c82016-12-22 08:36:05 +0000292 list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
Chris Wilson9e8944a2012-11-15 11:32:17 +0000293
Geliang Tang18b40c52015-11-21 22:04:04 +0800294#define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
295 for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
296 &entry->hole_stack != &(mm)->hole_stack ? \
297 hole_start = drm_mm_hole_node_start(entry), \
298 hole_end = drm_mm_hole_node_end(entry), \
299 1 : 0; \
300 entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
301
Daniel Vettere18c0412014-01-23 00:39:13 +0100302/**
303 * drm_mm_for_each_hole - iterator to walk over all holes
Daniel Vetter05fc0322016-12-29 21:48:23 +0100304 * @entry: &drm_mm_node used internally to track progress
305 * @mm: &drm_mm allocator to walk
Daniel Vettere18c0412014-01-23 00:39:13 +0100306 * @hole_start: ulong variable to assign the hole start to on each iteration
307 * @hole_end: ulong variable to assign the hole end to on each iteration
308 *
309 * This iterator walks over all holes in the range allocator. It is implemented
Daniel Vetter05fc0322016-12-29 21:48:23 +0100310 * with list_for_each(), so not save against removal of elements. @entry is used
Daniel Vettere18c0412014-01-23 00:39:13 +0100311 * internally and will not reflect a real drm_mm_node for the very first hole.
312 * Hence users of this iterator may not access it.
313 *
314 * Implementation Note:
315 * We need to inline list_for_each_entry in order to be able to set hole_start
316 * and hole_end on each iteration while keeping the macro sane.
Lauri Kasanen62347f92014-04-02 20:03:57 +0300317 *
318 * The __drm_mm_for_each_hole version is similar, but with added support for
319 * going backwards.
Chris Wilson9e8944a2012-11-15 11:32:17 +0000320 */
321#define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
Geliang Tang18b40c52015-11-21 22:04:04 +0800322 __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0)
Lauri Kasanen62347f92014-04-02 20:03:57 +0300323
Jerome Glisse249d6042009-04-08 17:11:16 +0200324/*
325 * Basic range manager support (drm_mm.c)
326 */
Daniel Vettere18c0412014-01-23 00:39:13 +0100327int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
Daniel Vettere18c0412014-01-23 00:39:13 +0100328int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
329 struct drm_mm_node *node,
Thierry Reding440fd522015-01-23 09:05:06 +0100330 u64 size,
Chris Wilson71733202016-12-22 08:36:24 +0000331 u64 alignment,
Daniel Vettere18c0412014-01-23 00:39:13 +0100332 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +0100333 u64 start,
334 u64 end,
Lauri Kasanen62347f92014-04-02 20:03:57 +0300335 enum drm_mm_search_flags sflags,
336 enum drm_mm_allocator_flags aflags);
Chris Wilsonadb040b2016-12-22 08:36:39 +0000337
Daniel Vettere18c0412014-01-23 00:39:13 +0100338/**
339 * drm_mm_insert_node_in_range - ranged search for space and insert @node
340 * @mm: drm_mm to allocate from
341 * @node: preallocate node to insert
342 * @size: size of the allocation
343 * @alignment: alignment of the allocation
344 * @start: start of the allowed range for this node
345 * @end: end of the allowed range for this node
346 * @flags: flags to fine-tune the allocation
347 *
348 * This is a simplified version of drm_mm_insert_node_in_range_generic() with
349 * @color set to 0.
350 *
351 * The preallocated node must be cleared to 0.
352 *
353 * Returns:
354 * 0 on success, -ENOSPC if there's no suitable hole.
355 */
David Herrmann31e5d7c2013-07-27 13:36:27 +0200356static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
357 struct drm_mm_node *node,
Thierry Reding440fd522015-01-23 09:05:06 +0100358 u64 size,
Chris Wilson71733202016-12-22 08:36:24 +0000359 u64 alignment,
Thierry Reding440fd522015-01-23 09:05:06 +0100360 u64 start,
361 u64 end,
David Herrmann31e5d7c2013-07-27 13:36:27 +0200362 enum drm_mm_search_flags flags)
363{
364 return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
Lauri Kasanen62347f92014-04-02 20:03:57 +0300365 0, start, end, flags,
366 DRM_MM_CREATE_DEFAULT);
David Herrmann31e5d7c2013-07-27 13:36:27 +0200367}
368
Chris Wilsonadb040b2016-12-22 08:36:39 +0000369/**
370 * drm_mm_insert_node_generic - search for space and insert @node
371 * @mm: drm_mm to allocate from
372 * @node: preallocate node to insert
373 * @size: size of the allocation
374 * @alignment: alignment of the allocation
375 * @color: opaque tag value to use for this node
376 * @sflags: flags to fine-tune the allocation search
377 * @aflags: flags to fine-tune the allocation behavior
378 *
Daniel Vetter05fc0322016-12-29 21:48:23 +0100379 * This is a simplified version of drm_mm_insert_node_in_range_generic() with no
380 * range restrictions applied.
381 *
Chris Wilsonadb040b2016-12-22 08:36:39 +0000382 * The preallocated node must be cleared to 0.
383 *
384 * Returns:
385 * 0 on success, -ENOSPC if there's no suitable hole.
386 */
387static inline int
388drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
389 u64 size, u64 alignment,
390 unsigned long color,
391 enum drm_mm_search_flags sflags,
392 enum drm_mm_allocator_flags aflags)
393{
394 return drm_mm_insert_node_in_range_generic(mm, node,
395 size, alignment, 0,
396 0, U64_MAX,
397 sflags, aflags);
398}
399
400/**
401 * drm_mm_insert_node - search for space and insert @node
402 * @mm: drm_mm to allocate from
403 * @node: preallocate node to insert
404 * @size: size of the allocation
405 * @alignment: alignment of the allocation
406 * @flags: flags to fine-tune the allocation
407 *
408 * This is a simplified version of drm_mm_insert_node_generic() with @color set
409 * to 0.
410 *
411 * The preallocated node must be cleared to 0.
412 *
413 * Returns:
414 * 0 on success, -ENOSPC if there's no suitable hole.
415 */
416static inline int drm_mm_insert_node(struct drm_mm *mm,
417 struct drm_mm_node *node,
418 u64 size,
419 u64 alignment,
420 enum drm_mm_search_flags flags)
421{
422 return drm_mm_insert_node_generic(mm, node,
423 size, alignment, 0,
424 flags, DRM_MM_CREATE_DEFAULT);
425}
426
Daniel Vettere18c0412014-01-23 00:39:13 +0100427void drm_mm_remove_node(struct drm_mm_node *node);
428void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
Chris Wilson45b186f2016-12-16 07:46:42 +0000429void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
Daniel Vettere18c0412014-01-23 00:39:13 +0100430void drm_mm_takedown(struct drm_mm *mm);
Chris Wilsonac9bb7b2016-12-22 08:36:27 +0000431
432/**
433 * drm_mm_clean - checks whether an allocator is clean
434 * @mm: drm_mm allocator to check
435 *
436 * Returns:
437 * True if the allocator is completely free, false if there's still a node
438 * allocated in it.
439 */
440static inline bool drm_mm_clean(const struct drm_mm *mm)
441{
442 return list_empty(drm_mm_nodes(mm));
443}
Jerome Glisse249d6042009-04-08 17:11:16 +0200444
Chris Wilson202b52b2016-08-03 16:04:09 +0100445struct drm_mm_node *
Chris Wilson45b186f2016-12-16 07:46:42 +0000446__drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
Chris Wilson202b52b2016-08-03 16:04:09 +0100447
Chris Wilson522e85d2016-11-23 14:11:14 +0000448/**
449 * drm_mm_for_each_node_in_range - iterator to walk over a range of
450 * allocated nodes
Chris Wilson8b2fb7b2016-11-27 11:16:23 +0000451 * @node__: drm_mm_node structure to assign to in each iteration step
452 * @mm__: drm_mm allocator to walk
453 * @start__: starting offset, the first node will overlap this
454 * @end__: ending offset, the last node will start before this (but may overlap)
Chris Wilson522e85d2016-11-23 14:11:14 +0000455 *
456 * This iterator walks over all nodes in the range allocator that lie
457 * between @start and @end. It is implemented similarly to list_for_each(),
458 * but using the internal interval tree to accelerate the search for the
459 * starting node, and so not safe against removal of elements. It assumes
460 * that @end is within (or is the upper limit of) the drm_mm allocator.
461 */
Chris Wilson8b2fb7b2016-11-27 11:16:23 +0000462#define drm_mm_for_each_node_in_range(node__, mm__, start__, end__) \
463 for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
464 node__ && node__->start < (end__); \
465 node__ = list_next_entry(node__, node_list))
Chris Wilson202b52b2016-08-03 16:04:09 +0100466
Chris Wilson9a71e272016-12-22 08:36:29 +0000467void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
468 struct drm_mm *mm,
Chris Wilson0b04d472016-12-22 08:36:33 +0000469 u64 size, u64 alignment, unsigned long color,
470 u64 start, u64 end,
471 unsigned int flags);
Chris Wilson2c4b3892016-12-22 08:36:31 +0000472
473/**
474 * drm_mm_scan_init - initialize lru scanning
475 * @scan: scan state
476 * @mm: drm_mm to scan
477 * @size: size of the allocation
478 * @alignment: alignment of the allocation
479 * @color: opaque tag value to use for the allocation
Chris Wilson0b04d472016-12-22 08:36:33 +0000480 * @flags: flags to specify how the allocation will be performed afterwards
Chris Wilson2c4b3892016-12-22 08:36:31 +0000481 *
Daniel Vetter05fc0322016-12-29 21:48:23 +0100482 * This is a simplified version of drm_mm_scan_init_with_range() with no range
483 * restrictions applied.
484 *
Chris Wilson2c4b3892016-12-22 08:36:31 +0000485 * This simply sets up the scanning routines with the parameters for the desired
Chris Wilson0b04d472016-12-22 08:36:33 +0000486 * hole.
Chris Wilson2c4b3892016-12-22 08:36:31 +0000487 *
488 * Warning:
489 * As long as the scan list is non-empty, no other operations than
490 * adding/removing nodes to/from the scan list are allowed.
491 */
492static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
493 struct drm_mm *mm,
494 u64 size,
495 u64 alignment,
Chris Wilson0b04d472016-12-22 08:36:33 +0000496 unsigned long color,
497 unsigned int flags)
Chris Wilson2c4b3892016-12-22 08:36:31 +0000498{
Chris Wilson0b04d472016-12-22 08:36:33 +0000499 drm_mm_scan_init_with_range(scan, mm,
500 size, alignment, color,
501 0, U64_MAX,
502 flags);
Chris Wilson2c4b3892016-12-22 08:36:31 +0000503}
504
Chris Wilson9a71e272016-12-22 08:36:29 +0000505bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
506 struct drm_mm_node *node);
507bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
508 struct drm_mm_node *node);
Chris Wilson3fa489d2016-12-22 08:36:36 +0000509struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
Daniel Vetter709ea972010-07-02 15:02:16 +0100510
Daniel Vetterb5c37142016-12-29 12:09:24 +0100511void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p);
Dave Airliefa8a1232009-08-26 13:13:37 +1000512
Jerome Glisse249d6042009-04-08 17:11:16 +0200513#endif