Lines Matching full:mm
14 static struct drm_buddy_block *drm_block_alloc(struct drm_buddy *mm, in drm_block_alloc() argument
35 static void drm_block_free(struct drm_buddy *mm, in drm_block_free() argument
41 static void list_insert_sorted(struct drm_buddy *mm, in list_insert_sorted() argument
47 head = &mm->free_list[drm_buddy_block_order(block)]; in list_insert_sorted()
78 static void mark_free(struct drm_buddy *mm, in mark_free() argument
84 list_insert_sorted(mm, block); in mark_free()
120 static unsigned int __drm_buddy_free(struct drm_buddy *mm, in __drm_buddy_free() argument
150 mm->clear_avail -= drm_buddy_block_size(mm, buddy); in __drm_buddy_free()
152 drm_block_free(mm, block); in __drm_buddy_free()
153 drm_block_free(mm, buddy); in __drm_buddy_free()
159 mark_free(mm, block); in __drm_buddy_free()
164 static int __force_merge(struct drm_buddy *mm, in __force_merge() argument
175 if (min_order > mm->max_order) in __force_merge()
181 list_for_each_entry_safe_reverse(block, prev, &mm->free_list[i], link) { in __force_merge()
189 block_end = block_start + drm_buddy_block_size(mm, block) - 1; in __force_merge()
211 mm->clear_avail -= drm_buddy_block_size(mm, block); in __force_merge()
213 order = __drm_buddy_free(mm, block, true); in __force_merge()
225 * @mm: DRM buddy manager to initialize
234 int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size) in drm_buddy_init() argument
250 mm->size = size; in drm_buddy_init()
251 mm->avail = size; in drm_buddy_init()
252 mm->clear_avail = 0; in drm_buddy_init()
253 mm->chunk_size = chunk_size; in drm_buddy_init()
254 mm->max_order = ilog2(size) - ilog2(chunk_size); in drm_buddy_init()
256 BUG_ON(mm->max_order > DRM_BUDDY_MAX_ORDER); in drm_buddy_init()
258 mm->free_list = kmalloc_array(mm->max_order + 1, in drm_buddy_init()
261 if (!mm->free_list) in drm_buddy_init()
264 for (i = 0; i <= mm->max_order; ++i) in drm_buddy_init()
265 INIT_LIST_HEAD(&mm->free_list[i]); in drm_buddy_init()
267 mm->n_roots = hweight64(size); in drm_buddy_init()
269 mm->roots = kmalloc_array(mm->n_roots, in drm_buddy_init()
272 if (!mm->roots) in drm_buddy_init()
290 root = drm_block_alloc(mm, NULL, order, offset); in drm_buddy_init()
294 mark_free(mm, root); in drm_buddy_init()
296 BUG_ON(i > mm->max_order); in drm_buddy_init()
297 BUG_ON(drm_buddy_block_size(mm, root) < chunk_size); in drm_buddy_init()
299 mm->roots[i] = root; in drm_buddy_init()
310 drm_block_free(mm, mm->roots[i]); in drm_buddy_init()
311 kfree(mm->roots); in drm_buddy_init()
313 kfree(mm->free_list); in drm_buddy_init()
321 * @mm: DRM buddy manager to free
325 void drm_buddy_fini(struct drm_buddy *mm) in drm_buddy_fini() argument
331 size = mm->size; in drm_buddy_fini()
333 for (i = 0; i < mm->n_roots; ++i) { in drm_buddy_fini()
334 order = ilog2(size) - ilog2(mm->chunk_size); in drm_buddy_fini()
335 __force_merge(mm, 0, size, order); in drm_buddy_fini()
337 WARN_ON(!drm_buddy_block_is_free(mm->roots[i])); in drm_buddy_fini()
338 drm_block_free(mm, mm->roots[i]); in drm_buddy_fini()
340 root_size = mm->chunk_size << order; in drm_buddy_fini()
344 WARN_ON(mm->avail != mm->size); in drm_buddy_fini()
346 kfree(mm->roots); in drm_buddy_fini()
347 kfree(mm->free_list); in drm_buddy_fini()
351 static int split_block(struct drm_buddy *mm, in split_block() argument
360 block->left = drm_block_alloc(mm, block, block_order, offset); in split_block()
364 block->right = drm_block_alloc(mm, block, block_order, in split_block()
365 offset + (mm->chunk_size << block_order)); in split_block()
367 drm_block_free(mm, block->left); in split_block()
371 mark_free(mm, block->left); in split_block()
372 mark_free(mm, block->right); in split_block()
405 * @mm: DRM buddy manager
408 void drm_buddy_free_block(struct drm_buddy *mm, in drm_buddy_free_block() argument
412 mm->avail += drm_buddy_block_size(mm, block); in drm_buddy_free_block()
414 mm->clear_avail += drm_buddy_block_size(mm, block); in drm_buddy_free_block()
416 __drm_buddy_free(mm, block, false); in drm_buddy_free_block()
420 static void __drm_buddy_free_list(struct drm_buddy *mm, in __drm_buddy_free_list() argument
434 drm_buddy_free_block(mm, block); in __drm_buddy_free_list()
440 static void drm_buddy_free_list_internal(struct drm_buddy *mm, in drm_buddy_free_list_internal() argument
448 __drm_buddy_free_list(mm, objects, false, false); in drm_buddy_free_list_internal()
454 * @mm: DRM buddy manager
458 void drm_buddy_free_list(struct drm_buddy *mm, in drm_buddy_free_list() argument
464 __drm_buddy_free_list(mm, objects, mark_clear, !mark_clear); in drm_buddy_free_list()
476 __alloc_range_bias(struct drm_buddy *mm, in __alloc_range_bias() argument
482 u64 req_size = mm->chunk_size << order; in __alloc_range_bias()
491 for (i = 0; i < mm->n_roots; ++i) in __alloc_range_bias()
492 list_add_tail(&mm->roots[i]->tmp_link, &dfs); in __alloc_range_bias()
510 block_end = block_start + drm_buddy_block_size(mm, block) - 1; in __alloc_range_bias()
542 err = split_block(mm, block); in __alloc_range_bias()
563 __drm_buddy_free(mm, block, false); in __alloc_range_bias()
568 __drm_buddy_alloc_range_bias(struct drm_buddy *mm, in __drm_buddy_alloc_range_bias() argument
576 block = __alloc_range_bias(mm, start, end, order, in __drm_buddy_alloc_range_bias()
579 return __alloc_range_bias(mm, start, end, order, in __drm_buddy_alloc_range_bias()
586 get_maxblock(struct drm_buddy *mm, unsigned int order, in get_maxblock() argument
592 for (i = order; i <= mm->max_order; ++i) { in get_maxblock()
595 list_for_each_entry_reverse(tmp_block, &mm->free_list[i], link) { in get_maxblock()
621 alloc_from_freelist(struct drm_buddy *mm, in alloc_from_freelist() argument
630 block = get_maxblock(mm, order, flags); in alloc_from_freelist()
635 for (tmp = order; tmp <= mm->max_order; ++tmp) { in alloc_from_freelist()
638 list_for_each_entry_reverse(tmp_block, &mm->free_list[tmp], link) { in alloc_from_freelist()
653 for (tmp = order; tmp <= mm->max_order; ++tmp) { in alloc_from_freelist()
654 if (!list_empty(&mm->free_list[tmp])) { in alloc_from_freelist()
655 block = list_last_entry(&mm->free_list[tmp], in alloc_from_freelist()
670 err = split_block(mm, block); in alloc_from_freelist()
681 __drm_buddy_free(mm, block, false); in alloc_from_freelist()
685 static int __alloc_range(struct drm_buddy *mm, in __alloc_range() argument
713 block_end = block_start + drm_buddy_block_size(mm, block) - 1; in __alloc_range()
726 total_allocated += drm_buddy_block_size(mm, block); in __alloc_range()
727 mm->avail -= drm_buddy_block_size(mm, block); in __alloc_range()
729 mm->clear_avail -= drm_buddy_block_size(mm, block); in __alloc_range()
732 } else if (!mm->clear_avail) { in __alloc_range()
739 err = split_block(mm, block); in __alloc_range()
767 __drm_buddy_free(mm, block, false); in __alloc_range()
774 drm_buddy_free_list_internal(mm, &allocated); in __alloc_range()
780 static int __drm_buddy_alloc_range(struct drm_buddy *mm, in __drm_buddy_alloc_range() argument
789 for (i = 0; i < mm->n_roots; ++i) in __drm_buddy_alloc_range()
790 list_add_tail(&mm->roots[i]->tmp_link, &dfs); in __drm_buddy_alloc_range()
792 return __alloc_range(mm, &dfs, start, size, in __drm_buddy_alloc_range()
796 static int __alloc_contig_try_harder(struct drm_buddy *mm, in __alloc_contig_try_harder() argument
811 pages = modify_size >> ilog2(mm->chunk_size); in __alloc_contig_try_harder()
816 list = &mm->free_list[order]; in __alloc_contig_try_harder()
823 err = __drm_buddy_alloc_range(mm, rhs_offset, size, in __alloc_contig_try_harder()
834 err = __drm_buddy_alloc_range(mm, lhs_offset, lhs_size, in __alloc_contig_try_harder()
840 drm_buddy_free_list_internal(mm, blocks); in __alloc_contig_try_harder()
844 drm_buddy_free_list_internal(mm, blocks); in __alloc_contig_try_harder()
853 * @mm: DRM buddy manager
869 int drm_buddy_block_trim(struct drm_buddy *mm, in drm_buddy_block_trim() argument
889 block_end = block_start + drm_buddy_block_size(mm, block); in drm_buddy_block_trim()
894 if (new_size > drm_buddy_block_size(mm, block)) in drm_buddy_block_trim()
897 if (!new_size || !IS_ALIGNED(new_size, mm->chunk_size)) in drm_buddy_block_trim()
900 if (new_size == drm_buddy_block_size(mm, block)) in drm_buddy_block_trim()
910 if (!IS_ALIGNED(new_start, mm->chunk_size)) in drm_buddy_block_trim()
918 mark_free(mm, block); in drm_buddy_block_trim()
919 mm->avail += drm_buddy_block_size(mm, block); in drm_buddy_block_trim()
921 mm->clear_avail += drm_buddy_block_size(mm, block); in drm_buddy_block_trim()
928 err = __alloc_range(mm, &dfs, new_start, new_size, blocks, NULL); in drm_buddy_block_trim()
931 mm->avail -= drm_buddy_block_size(mm, block); in drm_buddy_block_trim()
933 mm->clear_avail -= drm_buddy_block_size(mm, block); in drm_buddy_block_trim()
943 __drm_buddy_alloc_blocks(struct drm_buddy *mm, in __drm_buddy_alloc_blocks() argument
950 return __drm_buddy_alloc_range_bias(mm, start, end, in __drm_buddy_alloc_blocks()
954 return alloc_from_freelist(mm, order, flags); in __drm_buddy_alloc_blocks()
960 * @mm: DRM buddy manager to allocate from
977 int drm_buddy_alloc_blocks(struct drm_buddy *mm, in drm_buddy_alloc_blocks() argument
990 if (size < mm->chunk_size) in drm_buddy_alloc_blocks()
993 if (min_block_size < mm->chunk_size) in drm_buddy_alloc_blocks()
999 if (!IS_ALIGNED(start | end | size, mm->chunk_size)) in drm_buddy_alloc_blocks()
1002 if (end > mm->size) in drm_buddy_alloc_blocks()
1005 if (range_overflows(start, size, mm->size)) in drm_buddy_alloc_blocks()
1013 return __drm_buddy_alloc_range(mm, start, size, NULL, blocks); in drm_buddy_alloc_blocks()
1028 pages = size >> ilog2(mm->chunk_size); in drm_buddy_alloc_blocks()
1030 min_order = ilog2(min_block_size) - ilog2(mm->chunk_size); in drm_buddy_alloc_blocks()
1034 BUG_ON(order > mm->max_order); in drm_buddy_alloc_blocks()
1038 block = __drm_buddy_alloc_blocks(mm, start, in drm_buddy_alloc_blocks()
1047 if (mm->clear_avail && in drm_buddy_alloc_blocks()
1048 !__force_merge(mm, start, end, min_order)) { in drm_buddy_alloc_blocks()
1049 block = __drm_buddy_alloc_blocks(mm, start, in drm_buddy_alloc_blocks()
1065 return __alloc_contig_try_harder(mm, in drm_buddy_alloc_blocks()
1075 mm->avail -= drm_buddy_block_size(mm, block); in drm_buddy_alloc_blocks()
1077 mm->clear_avail -= drm_buddy_block_size(mm, block); in drm_buddy_alloc_blocks()
1101 trim_size = drm_buddy_block_size(mm, block) - in drm_buddy_alloc_blocks()
1105 drm_buddy_block_trim(mm, in drm_buddy_alloc_blocks()
1118 drm_buddy_free_list_internal(mm, &allocated); in drm_buddy_alloc_blocks()
1126 * @mm: DRM buddy manager
1130 void drm_buddy_block_print(struct drm_buddy *mm, in drm_buddy_block_print() argument
1135 u64 size = drm_buddy_block_size(mm, block); in drm_buddy_block_print()
1144 * @mm: DRM buddy manager
1147 void drm_buddy_print(struct drm_buddy *mm, struct drm_printer *p) in drm_buddy_print() argument
1152 mm->chunk_size >> 10, mm->size >> 20, mm->avail >> 20, mm->clear_avail >> 20); in drm_buddy_print()
1154 for (order = mm->max_order; order >= 0; order--) { in drm_buddy_print()
1158 list_for_each_entry(block, &mm->free_list[order], link) { in drm_buddy_print()
1165 free = count * (mm->chunk_size << order); in drm_buddy_print()