drm: Add asserts to catch overflow in drm_mm_init() and drm_mm_init_scan()
[linux-2.6-block.git] / drivers / gpu / drm / drm_mm.c
CommitLineData
3a1bd924
TH
1/**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
ba004e39 4 * Copyright 2016 Intel Corporation
3a1bd924
TH
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/*
31 * Generic simple memory manager implementation. Intended to be used as a base
32 * class implementation for more advanced memory managers.
33 *
34 * Note that the algorithm used is quite simple and there might be substantial
ba004e39
CW
35 * performance gains if a smarter free list is implemented. Currently it is
36 * just an unordered stack of free regions. This could easily be improved if
37 * an RB-tree is used instead. At least if we expect heavy fragmentation.
3a1bd924
TH
38 *
39 * Aligned allocations can also see improvement.
40 *
41 * Authors:
96de0e25 42 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
3a1bd924
TH
43 */
44
760285e7
DH
45#include <drm/drmP.h>
46#include <drm/drm_mm.h>
1d58420b 47#include <linux/slab.h>
fa8a1238 48#include <linux/seq_file.h>
2d1a8a48 49#include <linux/export.h>
202b52b7 50#include <linux/interval_tree_generic.h>
1d58420b 51
93110be6
DV
52/**
53 * DOC: Overview
54 *
55 * drm_mm provides a simple range allocator. The drivers are free to use the
56 * resource allocator from the linux core if it suits them, the upside of drm_mm
57 * is that it's in the DRM core. Which means that it's easier to extend for
58 * some of the crazier special purpose needs of gpus.
59 *
60 * The main data struct is &drm_mm, allocations are tracked in &drm_mm_node.
61 * Drivers are free to embed either of them into their own suitable
62 * datastructures. drm_mm itself will not do any allocations of its own, so if
63 * drivers choose not to embed nodes they need to still allocate them
64 * themselves.
65 *
66 * The range allocator also supports reservation of preallocated blocks. This is
67 * useful for taking over initial mode setting configurations from the firmware,
68 * where an object needs to be created which exactly matches the firmware's
69 * scanout target. As long as the range is still free it can be inserted anytime
70 * after the allocator is initialized, which helps with avoiding looped
ba004e39 71 * dependencies in the driver load sequence.
93110be6
DV
72 *
73 * drm_mm maintains a stack of most recently freed holes, which of all
74 * simplistic datastructures seems to be a fairly decent approach to clustering
75 * allocations and avoiding too much fragmentation. This means free space
76 * searches are O(num_holes). Given that all the fancy features drm_mm supports
77 * something better would be fairly complex and since gfx thrashing is a fairly
78 * steep cliff not a real concern. Removing a node again is O(1).
79 *
80 * drm_mm supports a few features: Alignment and range restrictions can be
81 * supplied. Further more every &drm_mm_node has a color value (which is just an
ba004e39 82 * opaque unsigned long) which in conjunction with a driver callback can be used
93110be6
DV
83 * to implement sophisticated placement restrictions. The i915 DRM driver uses
84 * this to implement guard pages between incompatible caching domains in the
85 * graphics TT.
86 *
ba004e39
CW
87 * Two behaviors are supported for searching and allocating: bottom-up and
88 * top-down. The default is bottom-up. Top-down allocation can be used if the
89 * memory area has different restrictions, or just to reduce fragmentation.
62347f9e 90 *
93110be6
DV
91 * Finally iteration helpers to walk all nodes and all holes are provided as are
92 * some basic allocator dumpers for debugging.
93 */
94
c700c67b 95static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
440fd528 96 u64 size,
71733207 97 u64 alignment,
c700c67b
DH
98 unsigned long color,
99 enum drm_mm_search_flags flags);
100static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
440fd528 101 u64 size,
71733207 102 u64 alignment,
c700c67b 103 unsigned long color,
440fd528
TR
104 u64 start,
105 u64 end,
c700c67b 106 enum drm_mm_search_flags flags);
1d58420b 107
5705670d 108#ifdef CONFIG_DRM_DEBUG_MM
93ce75fa
CW
109#include <linux/stackdepot.h>
110
5705670d
CW
111#define STACKDEPTH 32
112#define BUFSZ 4096
113
114static noinline void save_stack(struct drm_mm_node *node)
115{
116 unsigned long entries[STACKDEPTH];
117 struct stack_trace trace = {
118 .entries = entries,
119 .max_entries = STACKDEPTH,
120 .skip = 1
121 };
122
123 save_stack_trace(&trace);
124 if (trace.nr_entries != 0 &&
125 trace.entries[trace.nr_entries-1] == ULONG_MAX)
126 trace.nr_entries--;
127
128 /* May be called under spinlock, so avoid sleeping */
129 node->stack = depot_save_stack(&trace, GFP_NOWAIT);
130}
131
132static void show_leaks(struct drm_mm *mm)
133{
134 struct drm_mm_node *node;
135 unsigned long entries[STACKDEPTH];
136 char *buf;
137
138 buf = kmalloc(BUFSZ, GFP_KERNEL);
139 if (!buf)
140 return;
141
2bc98c86 142 list_for_each_entry(node, drm_mm_nodes(mm), node_list) {
5705670d
CW
143 struct stack_trace trace = {
144 .entries = entries,
145 .max_entries = STACKDEPTH
146 };
147
148 if (!node->stack) {
149 DRM_ERROR("node [%08llx + %08llx]: unknown owner\n",
150 node->start, node->size);
151 continue;
152 }
153
154 depot_fetch_stack(node->stack, &trace);
155 snprint_stack_trace(buf, BUFSZ, &trace, 0);
156 DRM_ERROR("node [%08llx + %08llx]: inserted at\n%s",
157 node->start, node->size, buf);
158 }
159
160 kfree(buf);
161}
162
163#undef STACKDEPTH
164#undef BUFSZ
165#else
166static void save_stack(struct drm_mm_node *node) { }
167static void show_leaks(struct drm_mm *mm) { }
168#endif
169
202b52b7
CW
170#define START(node) ((node)->start)
171#define LAST(node) ((node)->start + (node)->size - 1)
172
173INTERVAL_TREE_DEFINE(struct drm_mm_node, rb,
174 u64, __subtree_last,
175 START, LAST, static inline, drm_mm_interval_tree)
176
177struct drm_mm_node *
45b186f1 178__drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last)
202b52b7 179{
45b186f1 180 return drm_mm_interval_tree_iter_first((struct rb_root *)&mm->interval_tree,
202b52b7
CW
181 start, last);
182}
522e85dd 183EXPORT_SYMBOL(__drm_mm_interval_first);
202b52b7
CW
184
185static void drm_mm_interval_tree_add_node(struct drm_mm_node *hole_node,
186 struct drm_mm_node *node)
187{
188 struct drm_mm *mm = hole_node->mm;
189 struct rb_node **link, *rb;
190 struct drm_mm_node *parent;
191
192 node->__subtree_last = LAST(node);
193
194 if (hole_node->allocated) {
195 rb = &hole_node->rb;
196 while (rb) {
197 parent = rb_entry(rb, struct drm_mm_node, rb);
198 if (parent->__subtree_last >= node->__subtree_last)
199 break;
200
201 parent->__subtree_last = node->__subtree_last;
202 rb = rb_parent(rb);
203 }
204
205 rb = &hole_node->rb;
206 link = &hole_node->rb.rb_right;
207 } else {
208 rb = NULL;
209 link = &mm->interval_tree.rb_node;
210 }
211
212 while (*link) {
213 rb = *link;
214 parent = rb_entry(rb, struct drm_mm_node, rb);
215 if (parent->__subtree_last < node->__subtree_last)
216 parent->__subtree_last = node->__subtree_last;
217 if (node->start < parent->start)
218 link = &parent->rb.rb_left;
219 else
220 link = &parent->rb.rb_right;
221 }
222
223 rb_link_node(&node->rb, rb, link);
224 rb_insert_augmented(&node->rb,
225 &mm->interval_tree,
226 &drm_mm_interval_tree_augment);
227}
228
9fc935de
DV
229static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
230 struct drm_mm_node *node,
71733207 231 u64 size, u64 alignment,
62347f9e
LK
232 unsigned long color,
233 enum drm_mm_allocator_flags flags)
3a1bd924 234{
ea7b1dd4 235 struct drm_mm *mm = hole_node->mm;
440fd528
TR
236 u64 hole_start = drm_mm_hole_node_start(hole_node);
237 u64 hole_end = drm_mm_hole_node_end(hole_node);
238 u64 adj_start = hole_start;
239 u64 adj_end = hole_end;
ea7b1dd4 240
b3ee963f 241 DRM_MM_BUG_ON(node->allocated);
b0b7af18 242
6b9d89b4
CW
243 if (mm->color_adjust)
244 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
1d58420b 245
62347f9e
LK
246 if (flags & DRM_MM_CREATE_TOP)
247 adj_start = adj_end - size;
248
6b9d89b4 249 if (alignment) {
71733207 250 u64 rem;
440fd528 251
71733207 252 div64_u64_rem(adj_start, alignment, &rem);
440fd528 253 if (rem) {
62347f9e 254 if (flags & DRM_MM_CREATE_TOP)
440fd528 255 adj_start -= rem;
62347f9e 256 else
440fd528 257 adj_start += alignment - rem;
62347f9e 258 }
6b9d89b4
CW
259 }
260
b3ee963f
CW
261 DRM_MM_BUG_ON(adj_start < hole_start);
262 DRM_MM_BUG_ON(adj_end > hole_end);
62347f9e 263
6b9d89b4 264 if (adj_start == hole_start) {
ea7b1dd4 265 hole_node->hole_follows = 0;
6b9d89b4
CW
266 list_del(&hole_node->hole_stack);
267 }
ea7b1dd4 268
6b9d89b4 269 node->start = adj_start;
ea7b1dd4
DV
270 node->size = size;
271 node->mm = mm;
6b9d89b4 272 node->color = color;
b0b7af18 273 node->allocated = 1;
3a1bd924 274
ea7b1dd4
DV
275 list_add(&node->node_list, &hole_node->node_list);
276
202b52b7
CW
277 drm_mm_interval_tree_add_node(hole_node, node);
278
b3ee963f 279 DRM_MM_BUG_ON(node->start + node->size > adj_end);
ea7b1dd4 280
6b9d89b4 281 node->hole_follows = 0;
9e8944ab 282 if (__drm_mm_hole_node_start(node) < hole_end) {
ea7b1dd4
DV
283 list_add(&node->hole_stack, &mm->hole_stack);
284 node->hole_follows = 1;
1d58420b 285 }
5705670d
CW
286
287 save_stack(node);
9fc935de
DV
288}
289
e18c0412
DV
290/**
291 * drm_mm_reserve_node - insert an pre-initialized node
292 * @mm: drm_mm allocator to insert @node into
293 * @node: drm_mm_node to insert
294 *
295 * This functions inserts an already set-up drm_mm_node into the allocator,
296 * meaning that start, size and color must be set by the caller. This is useful
297 * to initialize the allocator with preallocated objects which must be set-up
298 * before the range allocator can be set-up, e.g. when taking over a firmware
299 * framebuffer.
300 *
301 * Returns:
302 * 0 on success, -ENOSPC if there's no hole where @node is.
303 */
338710e7 304int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node)
5973c7ee 305{
202b52b7 306 u64 end = node->start + node->size;
b3a070cc 307 struct drm_mm_node *hole;
202b52b7 308 u64 hole_start, hole_end;
2db86dfc 309 u64 adj_start, adj_end;
338710e7 310
b80d3942 311 end = node->start + node->size;
c820186d
CW
312 if (unlikely(end <= node->start))
313 return -ENOSPC;
b80d3942 314
338710e7 315 /* Find the relevant hole to add our node to */
202b52b7
CW
316 hole = drm_mm_interval_tree_iter_first(&mm->interval_tree,
317 node->start, ~(u64)0);
318 if (hole) {
319 if (hole->start < end)
320 return -ENOSPC;
321 } else {
2bc98c86 322 hole = list_entry(drm_mm_nodes(mm), typeof(*hole), node_list);
202b52b7 323 }
5973c7ee 324
202b52b7
CW
325 hole = list_last_entry(&hole->node_list, typeof(*hole), node_list);
326 if (!hole->hole_follows)
327 return -ENOSPC;
5973c7ee 328
2db86dfc
CW
329 adj_start = hole_start = __drm_mm_hole_node_start(hole);
330 adj_end = hole_end = __drm_mm_hole_node_end(hole);
331
332 if (mm->color_adjust)
333 mm->color_adjust(hole, node->color, &adj_start, &adj_end);
334
335 if (adj_start > node->start || adj_end < end)
202b52b7 336 return -ENOSPC;
5973c7ee 337
202b52b7
CW
338 node->mm = mm;
339 node->allocated = 1;
5973c7ee 340
202b52b7 341 list_add(&node->node_list, &hole->node_list);
5973c7ee 342
202b52b7
CW
343 drm_mm_interval_tree_add_node(hole, node);
344
345 if (node->start == hole_start) {
346 hole->hole_follows = 0;
a7879005 347 list_del(&hole->hole_stack);
202b52b7
CW
348 }
349
350 node->hole_follows = 0;
351 if (end != hole_end) {
352 list_add(&node->hole_stack, &mm->hole_stack);
353 node->hole_follows = 1;
5973c7ee
CW
354 }
355
5705670d
CW
356 save_stack(node);
357
202b52b7 358 return 0;
5973c7ee 359}
338710e7 360EXPORT_SYMBOL(drm_mm_reserve_node);
5973c7ee 361
b0b7af18 362/**
e18c0412
DV
363 * drm_mm_insert_node_generic - search for space and insert @node
364 * @mm: drm_mm to allocate from
365 * @node: preallocate node to insert
366 * @size: size of the allocation
367 * @alignment: alignment of the allocation
368 * @color: opaque tag value to use for this node
62347f9e
LK
369 * @sflags: flags to fine-tune the allocation search
370 * @aflags: flags to fine-tune the allocation behavior
e18c0412
DV
371 *
372 * The preallocated node must be cleared to 0.
373 *
374 * Returns:
375 * 0 on success, -ENOSPC if there's no suitable hole.
b0b7af18 376 */
b8103450 377int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
71733207 378 u64 size, u64 alignment,
31e5d7c6 379 unsigned long color,
62347f9e
LK
380 enum drm_mm_search_flags sflags,
381 enum drm_mm_allocator_flags aflags)
b0b7af18
DV
382{
383 struct drm_mm_node *hole_node;
384
aafdcfd3
CW
385 if (WARN_ON(size == 0))
386 return -EINVAL;
387
b8103450 388 hole_node = drm_mm_search_free_generic(mm, size, alignment,
62347f9e 389 color, sflags);
b0b7af18
DV
390 if (!hole_node)
391 return -ENOSPC;
392
62347f9e 393 drm_mm_insert_helper(hole_node, node, size, alignment, color, aflags);
b0b7af18
DV
394 return 0;
395}
b8103450
CW
396EXPORT_SYMBOL(drm_mm_insert_node_generic);
397
9fc935de
DV
398static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
399 struct drm_mm_node *node,
71733207 400 u64 size, u64 alignment,
6b9d89b4 401 unsigned long color,
440fd528 402 u64 start, u64 end,
62347f9e 403 enum drm_mm_allocator_flags flags)
a2e68e92 404{
ea7b1dd4 405 struct drm_mm *mm = hole_node->mm;
440fd528
TR
406 u64 hole_start = drm_mm_hole_node_start(hole_node);
407 u64 hole_end = drm_mm_hole_node_end(hole_node);
408 u64 adj_start = hole_start;
409 u64 adj_end = hole_end;
a2e68e92 410
b3ee963f 411 DRM_MM_BUG_ON(!hole_node->hole_follows || node->allocated);
b0b7af18 412
6b9d89b4
CW
413 if (adj_start < start)
414 adj_start = start;
901593f2
CW
415 if (adj_end > end)
416 adj_end = end;
417
418 if (mm->color_adjust)
419 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
6b9d89b4 420
fafecc01
MT
421 if (flags & DRM_MM_CREATE_TOP)
422 adj_start = adj_end - size;
423
6b9d89b4 424 if (alignment) {
71733207 425 u64 rem;
440fd528 426
71733207 427 div64_u64_rem(adj_start, alignment, &rem);
440fd528 428 if (rem) {
62347f9e 429 if (flags & DRM_MM_CREATE_TOP)
440fd528 430 adj_start -= rem;
62347f9e 431 else
440fd528 432 adj_start += alignment - rem;
62347f9e 433 }
6b9d89b4 434 }
ea7b1dd4 435
6b9d89b4 436 if (adj_start == hole_start) {
ea7b1dd4 437 hole_node->hole_follows = 0;
6b9d89b4 438 list_del(&hole_node->hole_stack);
a2e68e92
JG
439 }
440
6b9d89b4 441 node->start = adj_start;
ea7b1dd4
DV
442 node->size = size;
443 node->mm = mm;
6b9d89b4 444 node->color = color;
b0b7af18 445 node->allocated = 1;
ea7b1dd4 446
ea7b1dd4
DV
447 list_add(&node->node_list, &hole_node->node_list);
448
202b52b7
CW
449 drm_mm_interval_tree_add_node(hole_node, node);
450
b3ee963f
CW
451 DRM_MM_BUG_ON(node->start < start);
452 DRM_MM_BUG_ON(node->start < adj_start);
453 DRM_MM_BUG_ON(node->start + node->size > adj_end);
454 DRM_MM_BUG_ON(node->start + node->size > end);
ea7b1dd4 455
6b9d89b4 456 node->hole_follows = 0;
9e8944ab 457 if (__drm_mm_hole_node_start(node) < hole_end) {
ea7b1dd4
DV
458 list_add(&node->hole_stack, &mm->hole_stack);
459 node->hole_follows = 1;
a2e68e92 460 }
5705670d
CW
461
462 save_stack(node);
9fc935de
DV
463}
464
b0b7af18 465/**
e18c0412
DV
466 * drm_mm_insert_node_in_range_generic - ranged search for space and insert @node
467 * @mm: drm_mm to allocate from
468 * @node: preallocate node to insert
469 * @size: size of the allocation
470 * @alignment: alignment of the allocation
471 * @color: opaque tag value to use for this node
472 * @start: start of the allowed range for this node
473 * @end: end of the allowed range for this node
62347f9e
LK
474 * @sflags: flags to fine-tune the allocation search
475 * @aflags: flags to fine-tune the allocation behavior
e18c0412
DV
476 *
477 * The preallocated node must be cleared to 0.
478 *
479 * Returns:
480 * 0 on success, -ENOSPC if there's no suitable hole.
3a1bd924 481 */
b8103450 482int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
71733207 483 u64 size, u64 alignment,
62347f9e 484 unsigned long color,
440fd528 485 u64 start, u64 end,
62347f9e
LK
486 enum drm_mm_search_flags sflags,
487 enum drm_mm_allocator_flags aflags)
3a1bd924 488{
b0b7af18
DV
489 struct drm_mm_node *hole_node;
490
aafdcfd3
CW
491 if (WARN_ON(size == 0))
492 return -EINVAL;
493
b8103450
CW
494 hole_node = drm_mm_search_free_in_range_generic(mm,
495 size, alignment, color,
62347f9e 496 start, end, sflags);
b0b7af18
DV
497 if (!hole_node)
498 return -ENOSPC;
499
b8103450
CW
500 drm_mm_insert_helper_range(hole_node, node,
501 size, alignment, color,
62347f9e 502 start, end, aflags);
b0b7af18
DV
503 return 0;
504}
b8103450
CW
505EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
506
b0b7af18 507/**
e18c0412
DV
508 * drm_mm_remove_node - Remove a memory node from the allocator.
509 * @node: drm_mm_node to remove
510 *
511 * This just removes a node from its drm_mm allocator. The node does not need to
512 * be cleared again before it can be re-inserted into this or any other drm_mm
ba004e39 513 * allocator. It is a bug to call this function on a unallocated node.
b0b7af18
DV
514 */
515void drm_mm_remove_node(struct drm_mm_node *node)
516{
ea7b1dd4
DV
517 struct drm_mm *mm = node->mm;
518 struct drm_mm_node *prev_node;
3a1bd924 519
b3ee963f
CW
520 DRM_MM_BUG_ON(!node->allocated);
521 DRM_MM_BUG_ON(node->scanned_block ||
522 node->scanned_prev_free ||
523 node->scanned_next_free);
3a1bd924 524
ea7b1dd4
DV
525 prev_node =
526 list_entry(node->node_list.prev, struct drm_mm_node, node_list);
709ea971 527
ea7b1dd4 528 if (node->hole_follows) {
b3ee963f
CW
529 DRM_MM_BUG_ON(__drm_mm_hole_node_start(node) ==
530 __drm_mm_hole_node_end(node));
ea7b1dd4
DV
531 list_del(&node->hole_stack);
532 } else
b3ee963f
CW
533 DRM_MM_BUG_ON(__drm_mm_hole_node_start(node) !=
534 __drm_mm_hole_node_end(node));
9e8944ab 535
249d6048 536
ea7b1dd4
DV
537 if (!prev_node->hole_follows) {
538 prev_node->hole_follows = 1;
539 list_add(&prev_node->hole_stack, &mm->hole_stack);
540 } else
541 list_move(&prev_node->hole_stack, &mm->hole_stack);
542
202b52b7 543 drm_mm_interval_tree_remove(node, &mm->interval_tree);
ea7b1dd4 544 list_del(&node->node_list);
b0b7af18
DV
545 node->allocated = 0;
546}
547EXPORT_SYMBOL(drm_mm_remove_node);
548
71733207 549static int check_free_hole(u64 start, u64 end, u64 size, u64 alignment)
7a6b2896 550{
75214733 551 if (end - start < size)
7a6b2896
DV
552 return 0;
553
554 if (alignment) {
71733207 555 u64 rem;
440fd528 556
71733207 557 div64_u64_rem(start, alignment, &rem);
046d669c 558 if (rem)
440fd528 559 start += alignment - rem;
7a6b2896
DV
560 }
561
6b9d89b4 562 return end >= start + size;
7a6b2896
DV
563}
564
c700c67b 565static struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
440fd528 566 u64 size,
71733207 567 u64 alignment,
c700c67b
DH
568 unsigned long color,
569 enum drm_mm_search_flags flags)
3a1bd924 570{
55910517
DA
571 struct drm_mm_node *entry;
572 struct drm_mm_node *best;
440fd528
TR
573 u64 adj_start;
574 u64 adj_end;
575 u64 best_size;
3a1bd924 576
b3ee963f 577 DRM_MM_BUG_ON(mm->scanned_blocks);
709ea971 578
3a1bd924
TH
579 best = NULL;
580 best_size = ~0UL;
581
62347f9e
LK
582 __drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
583 flags & DRM_MM_SEARCH_BELOW) {
440fd528 584 u64 hole_size = adj_end - adj_start;
145bccd2 585
6b9d89b4
CW
586 if (mm->color_adjust) {
587 mm->color_adjust(entry, color, &adj_start, &adj_end);
588 if (adj_end <= adj_start)
589 continue;
590 }
591
6b9d89b4 592 if (!check_free_hole(adj_start, adj_end, size, alignment))
1d58420b
TH
593 continue;
594
31e5d7c6 595 if (!(flags & DRM_MM_SEARCH_BEST))
7a6b2896 596 return entry;
1d58420b 597
145bccd2 598 if (hole_size < best_size) {
7a6b2896 599 best = entry;
145bccd2 600 best_size = hole_size;
3a1bd924
TH
601 }
602 }
603
604 return best;
605}
6b9d89b4 606
c700c67b 607static struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
440fd528 608 u64 size,
71733207 609 u64 alignment,
6b9d89b4 610 unsigned long color,
440fd528
TR
611 u64 start,
612 u64 end,
31e5d7c6 613 enum drm_mm_search_flags flags)
a2e68e92 614{
a2e68e92
JG
615 struct drm_mm_node *entry;
616 struct drm_mm_node *best;
440fd528
TR
617 u64 adj_start;
618 u64 adj_end;
619 u64 best_size;
a2e68e92 620
b3ee963f 621 DRM_MM_BUG_ON(mm->scanned_blocks);
709ea971 622
a2e68e92
JG
623 best = NULL;
624 best_size = ~0UL;
625
62347f9e
LK
626 __drm_mm_for_each_hole(entry, mm, adj_start, adj_end,
627 flags & DRM_MM_SEARCH_BELOW) {
440fd528 628 u64 hole_size = adj_end - adj_start;
145bccd2 629
9e8944ab
CW
630 if (adj_start < start)
631 adj_start = start;
632 if (adj_end > end)
633 adj_end = end;
6b9d89b4
CW
634
635 if (mm->color_adjust) {
636 mm->color_adjust(entry, color, &adj_start, &adj_end);
637 if (adj_end <= adj_start)
638 continue;
639 }
640
75214733 641 if (!check_free_hole(adj_start, adj_end, size, alignment))
a2e68e92
JG
642 continue;
643
31e5d7c6 644 if (!(flags & DRM_MM_SEARCH_BEST))
7a6b2896 645 return entry;
a2e68e92 646
145bccd2 647 if (hole_size < best_size) {
7a6b2896 648 best = entry;
145bccd2 649 best_size = hole_size;
a2e68e92
JG
650 }
651 }
652
653 return best;
654}
a2e68e92 655
b0b7af18 656/**
e18c0412
DV
657 * drm_mm_replace_node - move an allocation from @old to @new
658 * @old: drm_mm_node to remove from the allocator
659 * @new: drm_mm_node which should inherit @old's allocation
660 *
661 * This is useful for when drivers embed the drm_mm_node structure and hence
662 * can't move allocations by reassigning pointers. It's a combination of remove
663 * and insert with the guarantee that the allocation start will match.
b0b7af18
DV
664 */
665void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
666{
b3ee963f
CW
667 DRM_MM_BUG_ON(!old->allocated);
668
b0b7af18 669 list_replace(&old->node_list, &new->node_list);
2bbd4492 670 list_replace(&old->hole_stack, &new->hole_stack);
202b52b7 671 rb_replace_node(&old->rb, &new->rb, &old->mm->interval_tree);
b0b7af18
DV
672 new->hole_follows = old->hole_follows;
673 new->mm = old->mm;
674 new->start = old->start;
675 new->size = old->size;
6b9d89b4 676 new->color = old->color;
202b52b7 677 new->__subtree_last = old->__subtree_last;
b0b7af18
DV
678
679 old->allocated = 0;
680 new->allocated = 1;
681}
682EXPORT_SYMBOL(drm_mm_replace_node);
683
93110be6
DV
684/**
685 * DOC: lru scan roaster
686 *
687 * Very often GPUs need to have continuous allocations for a given object. When
688 * evicting objects to make space for a new one it is therefore not most
689 * efficient when we simply start to select all objects from the tail of an LRU
690 * until there's a suitable hole: Especially for big objects or nodes that
691 * otherwise have special allocation constraints there's a good chance we evict
ba004e39 692 * lots of (smaller) objects unnecessarily.
93110be6
DV
693 *
694 * The DRM range allocator supports this use-case through the scanning
695 * interfaces. First a scan operation needs to be initialized with
ba004e39 696 * drm_mm_init_scan() or drm_mm_init_scan_with_range(). The driver adds
93110be6
DV
697 * objects to the roaster (probably by walking an LRU list, but this can be
698 * freely implemented) until a suitable hole is found or there's no further
ba004e39 699 * evictable object.
93110be6 700 *
ba004e39 701 * The driver must walk through all objects again in exactly the reverse
93110be6
DV
702 * order to restore the allocator state. Note that while the allocator is used
703 * in the scan mode no other operation is allowed.
704 *
705 * Finally the driver evicts all objects selected in the scan. Adding and
706 * removing an object is O(1), and since freeing a node is also O(1) the overall
707 * complexity is O(scanned_objects). So like the free stack which needs to be
708 * walked before a scan operation even begins this is linear in the number of
709 * objects. It doesn't seem to hurt badly.
710 */
711
709ea971 712/**
e18c0412
DV
713 * drm_mm_init_scan - initialize lru scanning
714 * @mm: drm_mm to scan
715 * @size: size of the allocation
716 * @alignment: alignment of the allocation
717 * @color: opaque tag value to use for the allocation
709ea971
DV
718 *
719 * This simply sets up the scanning routines with the parameters for the desired
e18c0412
DV
720 * hole. Note that there's no need to specify allocation flags, since they only
721 * change the place a node is allocated from within a suitable hole.
709ea971 722 *
e18c0412
DV
723 * Warning:
724 * As long as the scan list is non-empty, no other operations than
709ea971
DV
725 * adding/removing nodes to/from the scan list are allowed.
726 */
6b9d89b4 727void drm_mm_init_scan(struct drm_mm *mm,
440fd528 728 u64 size,
71733207 729 u64 alignment,
6b9d89b4 730 unsigned long color)
709ea971 731{
6259a56b
CW
732 DRM_MM_BUG_ON(!size);
733
6b9d89b4 734 mm->scan_color = color;
709ea971
DV
735 mm->scan_alignment = alignment;
736 mm->scan_size = size;
737 mm->scanned_blocks = 0;
738 mm->scan_hit_start = 0;
901593f2 739 mm->scan_hit_end = 0;
d935cc61 740 mm->scan_check_range = 0;
ae0cec28 741 mm->prev_scanned_node = NULL;
709ea971
DV
742}
743EXPORT_SYMBOL(drm_mm_init_scan);
744
d935cc61 745/**
e18c0412
DV
746 * drm_mm_init_scan - initialize range-restricted lru scanning
747 * @mm: drm_mm to scan
748 * @size: size of the allocation
749 * @alignment: alignment of the allocation
750 * @color: opaque tag value to use for the allocation
751 * @start: start of the allowed range for the allocation
752 * @end: end of the allowed range for the allocation
d935cc61
DV
753 *
754 * This simply sets up the scanning routines with the parameters for the desired
e18c0412
DV
755 * hole. Note that there's no need to specify allocation flags, since they only
756 * change the place a node is allocated from within a suitable hole.
d935cc61 757 *
e18c0412
DV
758 * Warning:
759 * As long as the scan list is non-empty, no other operations than
d935cc61
DV
760 * adding/removing nodes to/from the scan list are allowed.
761 */
6b9d89b4 762void drm_mm_init_scan_with_range(struct drm_mm *mm,
440fd528 763 u64 size,
71733207 764 u64 alignment,
6b9d89b4 765 unsigned long color,
440fd528
TR
766 u64 start,
767 u64 end)
d935cc61 768{
6259a56b
CW
769 DRM_MM_BUG_ON(start >= end);
770 DRM_MM_BUG_ON(!size || size > end - start);
771
6b9d89b4 772 mm->scan_color = color;
d935cc61
DV
773 mm->scan_alignment = alignment;
774 mm->scan_size = size;
775 mm->scanned_blocks = 0;
776 mm->scan_hit_start = 0;
901593f2 777 mm->scan_hit_end = 0;
d935cc61
DV
778 mm->scan_start = start;
779 mm->scan_end = end;
780 mm->scan_check_range = 1;
ae0cec28 781 mm->prev_scanned_node = NULL;
d935cc61
DV
782}
783EXPORT_SYMBOL(drm_mm_init_scan_with_range);
784
709ea971 785/**
e18c0412
DV
786 * drm_mm_scan_add_block - add a node to the scan list
787 * @node: drm_mm_node to add
788 *
709ea971
DV
789 * Add a node to the scan list that might be freed to make space for the desired
790 * hole.
791 *
e18c0412
DV
792 * Returns:
793 * True if a hole has been found, false otherwise.
709ea971 794 */
e18c0412 795bool drm_mm_scan_add_block(struct drm_mm_node *node)
709ea971
DV
796{
797 struct drm_mm *mm = node->mm;
ea7b1dd4 798 struct drm_mm_node *prev_node;
440fd528
TR
799 u64 hole_start, hole_end;
800 u64 adj_start, adj_end;
709ea971
DV
801
802 mm->scanned_blocks++;
803
b3ee963f 804 DRM_MM_BUG_ON(node->scanned_block);
709ea971 805 node->scanned_block = 1;
709ea971 806
ea7b1dd4
DV
807 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
808 node_list);
709ea971 809
ea7b1dd4
DV
810 node->scanned_preceeds_hole = prev_node->hole_follows;
811 prev_node->hole_follows = 1;
812 list_del(&node->node_list);
813 node->node_list.prev = &prev_node->node_list;
ae0cec28
DV
814 node->node_list.next = &mm->prev_scanned_node->node_list;
815 mm->prev_scanned_node = node;
709ea971 816
901593f2
CW
817 adj_start = hole_start = drm_mm_hole_node_start(prev_node);
818 adj_end = hole_end = drm_mm_hole_node_end(prev_node);
6b9d89b4 819
d935cc61 820 if (mm->scan_check_range) {
6b9d89b4
CW
821 if (adj_start < mm->scan_start)
822 adj_start = mm->scan_start;
823 if (adj_end > mm->scan_end)
824 adj_end = mm->scan_end;
d935cc61
DV
825 }
826
901593f2
CW
827 if (mm->color_adjust)
828 mm->color_adjust(prev_node, mm->scan_color,
829 &adj_start, &adj_end);
830
6b9d89b4 831 if (check_free_hole(adj_start, adj_end,
75214733 832 mm->scan_size, mm->scan_alignment)) {
ea7b1dd4 833 mm->scan_hit_start = hole_start;
901593f2 834 mm->scan_hit_end = hole_end;
e18c0412 835 return true;
709ea971
DV
836 }
837
e18c0412 838 return false;
709ea971
DV
839}
840EXPORT_SYMBOL(drm_mm_scan_add_block);
841
842/**
e18c0412
DV
843 * drm_mm_scan_remove_block - remove a node from the scan list
844 * @node: drm_mm_node to remove
709ea971 845 *
ba004e39
CW
846 * Nodes _must_ be removed in exactly the reverse order from the scan list as
847 * they have been added (e.g. using list_add as they are added and then
848 * list_for_each over that eviction list to remove), otherwise the internal
849 * state of the memory manager will be corrupted.
709ea971
DV
850 *
851 * When the scan list is empty, the selected memory nodes can be freed. An
31e5d7c6
DH
852 * immediately following drm_mm_search_free with !DRM_MM_SEARCH_BEST will then
853 * return the just freed block (because its at the top of the free_stack list).
709ea971 854 *
e18c0412
DV
855 * Returns:
856 * True if this block should be evicted, false otherwise. Will always
857 * return false when no hole has been found.
709ea971 858 */
e18c0412 859bool drm_mm_scan_remove_block(struct drm_mm_node *node)
709ea971
DV
860{
861 struct drm_mm *mm = node->mm;
ea7b1dd4 862 struct drm_mm_node *prev_node;
709ea971
DV
863
864 mm->scanned_blocks--;
865
b3ee963f 866 DRM_MM_BUG_ON(!node->scanned_block);
709ea971 867 node->scanned_block = 0;
709ea971 868
ea7b1dd4
DV
869 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
870 node_list);
709ea971 871
ea7b1dd4 872 prev_node->hole_follows = node->scanned_preceeds_hole;
ea7b1dd4 873 list_add(&node->node_list, &prev_node->node_list);
709ea971 874
901593f2
CW
875 return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
876 node->start < mm->scan_hit_end);
709ea971
DV
877}
878EXPORT_SYMBOL(drm_mm_scan_remove_block);
879
e18c0412
DV
880/**
881 * drm_mm_init - initialize a drm-mm allocator
882 * @mm: the drm_mm structure to initialize
883 * @start: start of the range managed by @mm
884 * @size: end of the range managed by @mm
885 *
886 * Note that @mm must be cleared to 0 before calling this function.
887 */
45b186f1 888void drm_mm_init(struct drm_mm *mm, u64 start, u64 size)
1d58420b 889{
6259a56b
CW
890 DRM_MM_BUG_ON(start + size <= start);
891
ea7b1dd4 892 INIT_LIST_HEAD(&mm->hole_stack);
709ea971 893 mm->scanned_blocks = 0;
3a1bd924 894
ea7b1dd4
DV
895 /* Clever trick to avoid a special case in the free hole tracking. */
896 INIT_LIST_HEAD(&mm->head_node.node_list);
cc98e6ce 897 mm->head_node.allocated = 0;
ea7b1dd4
DV
898 mm->head_node.hole_follows = 1;
899 mm->head_node.scanned_block = 0;
900 mm->head_node.scanned_prev_free = 0;
901 mm->head_node.scanned_next_free = 0;
902 mm->head_node.mm = mm;
903 mm->head_node.start = start + size;
904 mm->head_node.size = start - mm->head_node.start;
905 list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
906
202b52b7
CW
907 mm->interval_tree = RB_ROOT;
908
6b9d89b4 909 mm->color_adjust = NULL;
3a1bd924 910}
673a394b 911EXPORT_SYMBOL(drm_mm_init);
3a1bd924 912
e18c0412
DV
913/**
914 * drm_mm_takedown - clean up a drm_mm allocator
915 * @mm: drm_mm allocator to clean up
916 *
917 * Note that it is a bug to call this function on an allocator which is not
918 * clean.
919 */
5705670d 920void drm_mm_takedown(struct drm_mm *mm)
3a1bd924 921{
ac9bb7b7 922 if (WARN(!drm_mm_clean(mm),
5705670d
CW
923 "Memory manager not clean during takedown.\n"))
924 show_leaks(mm);
3a1bd924 925}
f453ba04 926EXPORT_SYMBOL(drm_mm_takedown);
fa8a1238 927
45b186f1
CW
928static u64 drm_mm_debug_hole(const struct drm_mm_node *entry,
929 const char *prefix)
99d7e48e 930{
440fd528 931 u64 hole_start, hole_end, hole_size;
ea7b1dd4 932
2c54b133
DV
933 if (entry->hole_follows) {
934 hole_start = drm_mm_hole_node_start(entry);
935 hole_end = drm_mm_hole_node_end(entry);
936 hole_size = hole_end - hole_start;
440fd528
TR
937 pr_debug("%s %#llx-%#llx: %llu: free\n", prefix, hole_start,
938 hole_end, hole_size);
2c54b133
DV
939 return hole_size;
940 }
941
942 return 0;
943}
944
e18c0412
DV
945/**
946 * drm_mm_debug_table - dump allocator state to dmesg
947 * @mm: drm_mm allocator to dump
948 * @prefix: prefix to use for dumping to dmesg
949 */
45b186f1 950void drm_mm_debug_table(const struct drm_mm *mm, const char *prefix)
2c54b133 951{
45b186f1 952 const struct drm_mm_node *entry;
440fd528 953 u64 total_used = 0, total_free = 0, total = 0;
2c54b133
DV
954
955 total_free += drm_mm_debug_hole(&mm->head_node, prefix);
ea7b1dd4
DV
956
957 drm_mm_for_each_node(entry, mm) {
440fd528
TR
958 pr_debug("%s %#llx-%#llx: %llu: used\n", prefix, entry->start,
959 entry->start + entry->size, entry->size);
ea7b1dd4 960 total_used += entry->size;
2c54b133 961 total_free += drm_mm_debug_hole(entry, prefix);
99d7e48e 962 }
ea7b1dd4
DV
963 total = total_free + total_used;
964
440fd528
TR
965 pr_debug("%s total: %llu, used %llu free %llu\n", prefix, total,
966 total_used, total_free);
99d7e48e
JG
967}
968EXPORT_SYMBOL(drm_mm_debug_table);
969
fa8a1238 970#if defined(CONFIG_DEBUG_FS)
45b186f1 971static u64 drm_mm_dump_hole(struct seq_file *m, const struct drm_mm_node *entry)
fa8a1238 972{
440fd528 973 u64 hole_start, hole_end, hole_size;
ea7b1dd4 974
3a359f0b
DV
975 if (entry->hole_follows) {
976 hole_start = drm_mm_hole_node_start(entry);
977 hole_end = drm_mm_hole_node_end(entry);
978 hole_size = hole_end - hole_start;
2f15791c 979 seq_printf(m, "%#018llx-%#018llx: %llu: free\n", hole_start,
440fd528 980 hole_end, hole_size);
3a359f0b
DV
981 return hole_size;
982 }
983
984 return 0;
985}
986
e18c0412
DV
987/**
988 * drm_mm_dump_table - dump allocator state to a seq_file
989 * @m: seq_file to dump to
990 * @mm: drm_mm allocator to dump
991 */
45b186f1 992int drm_mm_dump_table(struct seq_file *m, const struct drm_mm *mm)
3a359f0b 993{
45b186f1 994 const struct drm_mm_node *entry;
440fd528 995 u64 total_used = 0, total_free = 0, total = 0;
3a359f0b
DV
996
997 total_free += drm_mm_dump_hole(m, &mm->head_node);
ea7b1dd4
DV
998
999 drm_mm_for_each_node(entry, mm) {
2f15791c 1000 seq_printf(m, "%#018llx-%#018llx: %llu: used\n", entry->start,
440fd528 1001 entry->start + entry->size, entry->size);
ea7b1dd4 1002 total_used += entry->size;
3a359f0b 1003 total_free += drm_mm_dump_hole(m, entry);
fa8a1238 1004 }
ea7b1dd4
DV
1005 total = total_free + total_used;
1006
440fd528
TR
1007 seq_printf(m, "total: %llu, used %llu free %llu\n", total,
1008 total_used, total_free);
fa8a1238
DA
1009 return 0;
1010}
1011EXPORT_SYMBOL(drm_mm_dump_table);
1012#endif