1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Generic simple memory manager implementation. Intended to be used as a base
34 * class implementation for more advanced memory managers.
36 * Note that the algorithm used is quite simple and there might be substantial
37 * performance gains if a smarter free list is implemented. Currently it is just an
38 * unordered stack of free regions. This could easily be improved if an RB-tree
39 * is used instead. At least if we expect heavy fragmentation.
41 * Aligned allocations can also see improvement.
44 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
47 #include <dev/drm2/drmP.h>
48 #include <dev/drm2/drm_mm.h>
50 #define MM_UNUSED_TARGET 4
52 static struct drm_mm_node *drm_mm_kmalloc(struct drm_mm *mm, int atomic)
54 struct drm_mm_node *child;
56 child = malloc(sizeof(*child), DRM_MEM_MM, M_NOWAIT | M_ZERO);
58 if (unlikely(child == NULL)) {
59 mtx_lock(&mm->unused_lock);
60 if (list_empty(&mm->unused_nodes))
64 list_entry(mm->unused_nodes.next,
65 struct drm_mm_node, node_list);
66 list_del(&child->node_list);
69 mtx_unlock(&mm->unused_lock);
74 /* drm_mm_pre_get() - pre allocate drm_mm_node structure
75 * drm_mm: memory manager struct we are pre-allocating for
77 * Returns 0 on success or -ENOMEM if allocation fails.
79 int drm_mm_pre_get(struct drm_mm *mm)
81 struct drm_mm_node *node;
83 mtx_lock(&mm->unused_lock);
84 while (mm->num_unused < MM_UNUSED_TARGET) {
85 mtx_unlock(&mm->unused_lock);
86 node = malloc(sizeof(*node), DRM_MEM_MM, M_NOWAIT | M_ZERO);
87 mtx_lock(&mm->unused_lock);
89 if (unlikely(node == NULL)) {
90 int ret = (mm->num_unused < 2) ? -ENOMEM : 0;
91 mtx_unlock(&mm->unused_lock);
95 list_add_tail(&node->node_list, &mm->unused_nodes);
97 mtx_unlock(&mm->unused_lock);
100 EXPORT_SYMBOL(drm_mm_pre_get);
102 static inline unsigned long drm_mm_hole_node_start(struct drm_mm_node *hole_node)
104 return hole_node->start + hole_node->size;
107 static inline unsigned long drm_mm_hole_node_end(struct drm_mm_node *hole_node)
109 struct drm_mm_node *next_node =
110 list_entry(hole_node->node_list.next, struct drm_mm_node,
113 return next_node->start;
116 static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
117 struct drm_mm_node *node,
118 unsigned long size, unsigned alignment,
121 struct drm_mm *mm = hole_node->mm;
122 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
123 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
124 unsigned long adj_start = hole_start;
125 unsigned long adj_end = hole_end;
127 BUG_ON(!hole_node->hole_follows || node->allocated);
129 if (mm->color_adjust)
130 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
133 unsigned tmp = adj_start % alignment;
135 adj_start += alignment - tmp;
138 if (adj_start == hole_start) {
139 hole_node->hole_follows = 0;
140 list_del(&hole_node->hole_stack);
143 node->start = adj_start;
149 INIT_LIST_HEAD(&node->hole_stack);
150 list_add(&node->node_list, &hole_node->node_list);
152 BUG_ON(node->start + node->size > adj_end);
154 node->hole_follows = 0;
155 if (node->start + node->size < hole_end) {
156 list_add(&node->hole_stack, &mm->hole_stack);
157 node->hole_follows = 1;
161 struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *hole_node,
167 struct drm_mm_node *node;
169 node = drm_mm_kmalloc(hole_node->mm, atomic);
170 if (unlikely(node == NULL))
173 drm_mm_insert_helper(hole_node, node, size, alignment, color);
177 EXPORT_SYMBOL(drm_mm_get_block_generic);
180 * Search for free space and insert a preallocated memory node. Returns
181 * -ENOSPC if no suitable free area is available. The preallocated memory node
184 int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
185 unsigned long size, unsigned alignment,
188 struct drm_mm_node *hole_node;
190 hole_node = drm_mm_search_free_generic(mm, size, alignment,
195 drm_mm_insert_helper(hole_node, node, size, alignment, color);
198 EXPORT_SYMBOL(drm_mm_insert_node_generic);
200 int drm_mm_insert_node(struct drm_mm *mm, struct drm_mm_node *node,
201 unsigned long size, unsigned alignment)
203 return drm_mm_insert_node_generic(mm, node, size, alignment, 0);
205 EXPORT_SYMBOL(drm_mm_insert_node);
207 static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
208 struct drm_mm_node *node,
209 unsigned long size, unsigned alignment,
211 unsigned long start, unsigned long end)
213 struct drm_mm *mm = hole_node->mm;
214 unsigned long hole_start = drm_mm_hole_node_start(hole_node);
215 unsigned long hole_end = drm_mm_hole_node_end(hole_node);
216 unsigned long adj_start = hole_start;
217 unsigned long adj_end = hole_end;
219 BUG_ON(!hole_node->hole_follows || node->allocated);
221 if (adj_start < start)
226 if (mm->color_adjust)
227 mm->color_adjust(hole_node, color, &adj_start, &adj_end);
230 unsigned tmp = adj_start % alignment;
232 adj_start += alignment - tmp;
235 if (adj_start == hole_start) {
236 hole_node->hole_follows = 0;
237 list_del(&hole_node->hole_stack);
240 node->start = adj_start;
246 INIT_LIST_HEAD(&node->hole_stack);
247 list_add(&node->node_list, &hole_node->node_list);
249 BUG_ON(node->start + node->size > adj_end);
250 BUG_ON(node->start + node->size > end);
252 node->hole_follows = 0;
253 if (node->start + node->size < hole_end) {
254 list_add(&node->hole_stack, &mm->hole_stack);
255 node->hole_follows = 1;
259 struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *hole_node,
267 struct drm_mm_node *node;
269 node = drm_mm_kmalloc(hole_node->mm, atomic);
270 if (unlikely(node == NULL))
273 drm_mm_insert_helper_range(hole_node, node, size, alignment, color,
278 EXPORT_SYMBOL(drm_mm_get_block_range_generic);
281 * Search for free space and insert a preallocated memory node. Returns
282 * -ENOSPC if no suitable free area is available. This is for range
283 * restricted allocations. The preallocated memory node must be cleared.
285 int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
286 unsigned long size, unsigned alignment, unsigned long color,
287 unsigned long start, unsigned long end)
289 struct drm_mm_node *hole_node;
291 hole_node = drm_mm_search_free_in_range_generic(mm,
292 size, alignment, color,
297 drm_mm_insert_helper_range(hole_node, node,
298 size, alignment, color,
302 EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
304 int drm_mm_insert_node_in_range(struct drm_mm *mm, struct drm_mm_node *node,
305 unsigned long size, unsigned alignment,
306 unsigned long start, unsigned long end)
308 return drm_mm_insert_node_in_range_generic(mm, node, size, alignment, 0, start, end);
310 EXPORT_SYMBOL(drm_mm_insert_node_in_range);
313 * Remove a memory node from the allocator.
315 void drm_mm_remove_node(struct drm_mm_node *node)
317 struct drm_mm *mm = node->mm;
318 struct drm_mm_node *prev_node;
320 BUG_ON(node->scanned_block || node->scanned_prev_free
321 || node->scanned_next_free);
324 list_entry(node->node_list.prev, struct drm_mm_node, node_list);
326 if (node->hole_follows) {
327 BUG_ON(drm_mm_hole_node_start(node)
328 == drm_mm_hole_node_end(node));
329 list_del(&node->hole_stack);
331 BUG_ON(drm_mm_hole_node_start(node)
332 != drm_mm_hole_node_end(node));
334 if (!prev_node->hole_follows) {
335 prev_node->hole_follows = 1;
336 list_add(&prev_node->hole_stack, &mm->hole_stack);
338 list_move(&prev_node->hole_stack, &mm->hole_stack);
340 list_del(&node->node_list);
343 EXPORT_SYMBOL(drm_mm_remove_node);
346 * Remove a memory node from the allocator and free the allocated struct
347 * drm_mm_node. Only to be used on a struct drm_mm_node obtained by one of the
348 * drm_mm_get_block functions.
350 void drm_mm_put_block(struct drm_mm_node *node)
353 struct drm_mm *mm = node->mm;
355 drm_mm_remove_node(node);
357 mtx_lock(&mm->unused_lock);
358 if (mm->num_unused < MM_UNUSED_TARGET) {
359 list_add(&node->node_list, &mm->unused_nodes);
362 free(node, DRM_MEM_MM);
363 mtx_unlock(&mm->unused_lock);
365 EXPORT_SYMBOL(drm_mm_put_block);
367 static int check_free_hole(unsigned long start, unsigned long end,
368 unsigned long size, unsigned alignment)
370 if (end - start < size)
374 unsigned tmp = start % alignment;
376 start += alignment - tmp;
379 return end >= start + size;
382 struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
388 struct drm_mm_node *entry;
389 struct drm_mm_node *best;
390 unsigned long best_size;
392 BUG_ON(mm->scanned_blocks);
397 list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
398 unsigned long adj_start = drm_mm_hole_node_start(entry);
399 unsigned long adj_end = drm_mm_hole_node_end(entry);
401 if (mm->color_adjust) {
402 mm->color_adjust(entry, color, &adj_start, &adj_end);
403 if (adj_end <= adj_start)
407 BUG_ON(!entry->hole_follows);
408 if (!check_free_hole(adj_start, adj_end, size, alignment))
414 if (entry->size < best_size) {
416 best_size = entry->size;
422 EXPORT_SYMBOL(drm_mm_search_free_generic);
424 struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
432 struct drm_mm_node *entry;
433 struct drm_mm_node *best;
434 unsigned long best_size;
436 BUG_ON(mm->scanned_blocks);
441 list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
442 unsigned long adj_start = drm_mm_hole_node_start(entry) < start ?
443 start : drm_mm_hole_node_start(entry);
444 unsigned long adj_end = drm_mm_hole_node_end(entry) > end ?
445 end : drm_mm_hole_node_end(entry);
447 BUG_ON(!entry->hole_follows);
449 if (mm->color_adjust) {
450 mm->color_adjust(entry, color, &adj_start, &adj_end);
451 if (adj_end <= adj_start)
455 if (!check_free_hole(adj_start, adj_end, size, alignment))
461 if (entry->size < best_size) {
463 best_size = entry->size;
469 EXPORT_SYMBOL(drm_mm_search_free_in_range_generic);
472 * Moves an allocation. To be used with embedded struct drm_mm_node.
474 void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
476 list_replace(&old->node_list, &new->node_list);
477 list_replace(&old->hole_stack, &new->hole_stack);
478 new->hole_follows = old->hole_follows;
480 new->start = old->start;
481 new->size = old->size;
482 new->color = old->color;
487 EXPORT_SYMBOL(drm_mm_replace_node);
490 * Initializa lru scanning.
492 * This simply sets up the scanning routines with the parameters for the desired
495 * Warning: As long as the scan list is non-empty, no other operations than
496 * adding/removing nodes to/from the scan list are allowed.
498 void drm_mm_init_scan(struct drm_mm *mm,
503 mm->scan_color = color;
504 mm->scan_alignment = alignment;
505 mm->scan_size = size;
506 mm->scanned_blocks = 0;
507 mm->scan_hit_start = 0;
508 mm->scan_hit_end = 0;
509 mm->scan_check_range = 0;
510 mm->prev_scanned_node = NULL;
512 EXPORT_SYMBOL(drm_mm_init_scan);
515 * Initializa lru scanning.
517 * This simply sets up the scanning routines with the parameters for the desired
518 * hole. This version is for range-restricted scans.
520 * Warning: As long as the scan list is non-empty, no other operations than
521 * adding/removing nodes to/from the scan list are allowed.
523 void drm_mm_init_scan_with_range(struct drm_mm *mm,
530 mm->scan_color = color;
531 mm->scan_alignment = alignment;
532 mm->scan_size = size;
533 mm->scanned_blocks = 0;
534 mm->scan_hit_start = 0;
535 mm->scan_hit_end = 0;
536 mm->scan_start = start;
538 mm->scan_check_range = 1;
539 mm->prev_scanned_node = NULL;
541 EXPORT_SYMBOL(drm_mm_init_scan_with_range);
544 * Add a node to the scan list that might be freed to make space for the desired
547 * Returns non-zero, if a hole has been found, zero otherwise.
549 int drm_mm_scan_add_block(struct drm_mm_node *node)
551 struct drm_mm *mm = node->mm;
552 struct drm_mm_node *prev_node;
553 unsigned long hole_start, hole_end;
554 unsigned long adj_start, adj_end;
556 mm->scanned_blocks++;
558 BUG_ON(node->scanned_block);
559 node->scanned_block = 1;
561 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
564 node->scanned_preceeds_hole = prev_node->hole_follows;
565 prev_node->hole_follows = 1;
566 list_del(&node->node_list);
567 node->node_list.prev = &prev_node->node_list;
568 node->node_list.next = &mm->prev_scanned_node->node_list;
569 mm->prev_scanned_node = node;
571 adj_start = hole_start = drm_mm_hole_node_start(prev_node);
572 adj_end = hole_end = drm_mm_hole_node_end(prev_node);
574 if (mm->scan_check_range) {
575 if (adj_start < mm->scan_start)
576 adj_start = mm->scan_start;
577 if (adj_end > mm->scan_end)
578 adj_end = mm->scan_end;
581 if (mm->color_adjust)
582 mm->color_adjust(prev_node, mm->scan_color,
583 &adj_start, &adj_end);
585 if (check_free_hole(adj_start, adj_end,
586 mm->scan_size, mm->scan_alignment)) {
587 mm->scan_hit_start = hole_start;
588 mm->scan_hit_end = hole_end;
594 EXPORT_SYMBOL(drm_mm_scan_add_block);
597 * Remove a node from the scan list.
599 * Nodes _must_ be removed in the exact same order from the scan list as they
600 * have been added, otherwise the internal state of the memory manager will be
603 * When the scan list is empty, the selected memory nodes can be freed. An
604 * immediately following drm_mm_search_free with best_match = 0 will then return
605 * the just freed block (because its at the top of the free_stack list).
607 * Returns one if this block should be evicted, zero otherwise. Will always
608 * return zero when no hole has been found.
610 int drm_mm_scan_remove_block(struct drm_mm_node *node)
612 struct drm_mm *mm = node->mm;
613 struct drm_mm_node *prev_node;
615 mm->scanned_blocks--;
617 BUG_ON(!node->scanned_block);
618 node->scanned_block = 0;
620 prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
623 prev_node->hole_follows = node->scanned_preceeds_hole;
624 list_add(&node->node_list, &prev_node->node_list);
626 return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
627 node->start < mm->scan_hit_end);
629 EXPORT_SYMBOL(drm_mm_scan_remove_block);
631 int drm_mm_clean(struct drm_mm * mm)
633 struct list_head *head = &mm->head_node.node_list;
635 return (head->next->next == head);
637 EXPORT_SYMBOL(drm_mm_clean);
639 int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
641 INIT_LIST_HEAD(&mm->hole_stack);
642 INIT_LIST_HEAD(&mm->unused_nodes);
644 mm->scanned_blocks = 0;
645 mtx_init(&mm->unused_lock, "drm_unused", NULL, MTX_DEF);
647 /* Clever trick to avoid a special case in the free hole tracking. */
648 INIT_LIST_HEAD(&mm->head_node.node_list);
649 INIT_LIST_HEAD(&mm->head_node.hole_stack);
650 mm->head_node.hole_follows = 1;
651 mm->head_node.scanned_block = 0;
652 mm->head_node.scanned_prev_free = 0;
653 mm->head_node.scanned_next_free = 0;
654 mm->head_node.mm = mm;
655 mm->head_node.start = start + size;
656 mm->head_node.size = start - mm->head_node.start;
657 list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
659 mm->color_adjust = NULL;
663 EXPORT_SYMBOL(drm_mm_init);
665 void drm_mm_takedown(struct drm_mm * mm)
667 struct drm_mm_node *entry, *next;
669 if (!list_empty(&mm->head_node.node_list)) {
670 DRM_ERROR("Memory manager not clean. Delaying takedown\n");
674 mtx_lock(&mm->unused_lock);
675 list_for_each_entry_safe(entry, next, &mm->unused_nodes, node_list) {
676 list_del(&entry->node_list);
677 free(entry, DRM_MEM_MM);
680 mtx_unlock(&mm->unused_lock);
682 BUG_ON(mm->num_unused != 0);
684 EXPORT_SYMBOL(drm_mm_takedown);
686 void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
688 struct drm_mm_node *entry;
689 unsigned long total_used = 0, total_free = 0, total = 0;
690 unsigned long hole_start, hole_end, hole_size;
692 hole_start = drm_mm_hole_node_start(&mm->head_node);
693 hole_end = drm_mm_hole_node_end(&mm->head_node);
694 hole_size = hole_end - hole_start;
696 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
697 prefix, hole_start, hole_end,
699 total_free += hole_size;
701 drm_mm_for_each_node(entry, mm) {
702 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
703 prefix, entry->start, entry->start + entry->size,
705 total_used += entry->size;
707 if (entry->hole_follows) {
708 hole_start = drm_mm_hole_node_start(entry);
709 hole_end = drm_mm_hole_node_end(entry);
710 hole_size = hole_end - hole_start;
711 printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
712 prefix, hole_start, hole_end,
714 total_free += hole_size;
717 total = total_free + total_used;
719 printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
720 total_used, total_free);
722 EXPORT_SYMBOL(drm_mm_debug_table);