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