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