Merge tag 'soc-dt-late-6.10' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-block.git] / drivers / gpu / drm / drm_buddy.c
CommitLineData
6387a3c4
A
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright © 2021 Intel Corporation
4 */
5
6#include <linux/kmemleak.h>
7#include <linux/module.h>
8#include <linux/sizes.h>
9
10#include <drm/drm_buddy.h>
11
12static struct kmem_cache *slab_blocks;
13
14static struct drm_buddy_block *drm_block_alloc(struct drm_buddy *mm,
15 struct drm_buddy_block *parent,
16 unsigned int order,
17 u64 offset)
18{
19 struct drm_buddy_block *block;
20
21 BUG_ON(order > DRM_BUDDY_MAX_ORDER);
22
23 block = kmem_cache_zalloc(slab_blocks, GFP_KERNEL);
24 if (!block)
25 return NULL;
26
27 block->header = offset;
28 block->header |= order;
29 block->parent = parent;
30
31 BUG_ON(block->header & DRM_BUDDY_HEADER_UNUSED);
32 return block;
33}
34
35static void drm_block_free(struct drm_buddy *mm,
36 struct drm_buddy_block *block)
37{
38 kmem_cache_free(slab_blocks, block);
39}
40
5640e816
APS
41static void list_insert_sorted(struct drm_buddy *mm,
42 struct drm_buddy_block *block)
43{
44 struct drm_buddy_block *node;
45 struct list_head *head;
46
47 head = &mm->free_list[drm_buddy_block_order(block)];
48 if (list_empty(head)) {
49 list_add(&block->link, head);
50 return;
51 }
52
53 list_for_each_entry(node, head, link)
54 if (drm_buddy_block_offset(block) < drm_buddy_block_offset(node))
55 break;
56
57 __list_add(&block->link, node->link.prev, &node->link);
58}
59
96950929
APS
60static void clear_reset(struct drm_buddy_block *block)
61{
62 block->header &= ~DRM_BUDDY_HEADER_CLEAR;
63}
64
65static void mark_cleared(struct drm_buddy_block *block)
66{
67 block->header |= DRM_BUDDY_HEADER_CLEAR;
68}
69
6387a3c4
A
70static void mark_allocated(struct drm_buddy_block *block)
71{
72 block->header &= ~DRM_BUDDY_HEADER_STATE;
73 block->header |= DRM_BUDDY_ALLOCATED;
74
75 list_del(&block->link);
76}
77
78static void mark_free(struct drm_buddy *mm,
79 struct drm_buddy_block *block)
80{
81 block->header &= ~DRM_BUDDY_HEADER_STATE;
82 block->header |= DRM_BUDDY_FREE;
83
5640e816 84 list_insert_sorted(mm, block);
6387a3c4
A
85}
86
87static void mark_split(struct drm_buddy_block *block)
88{
89 block->header &= ~DRM_BUDDY_HEADER_STATE;
90 block->header |= DRM_BUDDY_SPLIT;
91
92 list_del(&block->link);
93}
94
96950929
APS
95static inline bool overlaps(u64 s1, u64 e1, u64 s2, u64 e2)
96{
97 return s1 <= e2 && e1 >= s2;
98}
99
100static inline bool contains(u64 s1, u64 e1, u64 s2, u64 e2)
101{
102 return s1 <= s2 && e1 >= e2;
103}
104
105static struct drm_buddy_block *
106__get_buddy(struct drm_buddy_block *block)
107{
108 struct drm_buddy_block *parent;
109
110 parent = block->parent;
111 if (!parent)
112 return NULL;
113
114 if (parent->left == block)
115 return parent->right;
116
117 return parent->left;
118}
119
120static unsigned int __drm_buddy_free(struct drm_buddy *mm,
121 struct drm_buddy_block *block,
122 bool force_merge)
123{
124 struct drm_buddy_block *parent;
125 unsigned int order;
126
127 while ((parent = block->parent)) {
128 struct drm_buddy_block *buddy;
129
130 buddy = __get_buddy(block);
131
132 if (!drm_buddy_block_is_free(buddy))
133 break;
134
135 if (!force_merge) {
136 /*
137 * Check the block and its buddy clear state and exit
138 * the loop if they both have the dissimilar state.
139 */
140 if (drm_buddy_block_is_clear(block) !=
141 drm_buddy_block_is_clear(buddy))
142 break;
143
144 if (drm_buddy_block_is_clear(block))
145 mark_cleared(parent);
146 }
147
148 list_del(&buddy->link);
149 if (force_merge && drm_buddy_block_is_clear(buddy))
150 mm->clear_avail -= drm_buddy_block_size(mm, buddy);
151
152 drm_block_free(mm, block);
153 drm_block_free(mm, buddy);
154
155 block = parent;
156 }
157
158 order = drm_buddy_block_order(block);
159 mark_free(mm, block);
160
161 return order;
162}
163
164static int __force_merge(struct drm_buddy *mm,
165 u64 start,
166 u64 end,
167 unsigned int min_order)
168{
169 unsigned int order;
170 int i;
171
172 if (!min_order)
173 return -ENOMEM;
174
175 if (min_order > mm->max_order)
176 return -EINVAL;
177
178 for (i = min_order - 1; i >= 0; i--) {
179 struct drm_buddy_block *block, *prev;
180
181 list_for_each_entry_safe_reverse(block, prev, &mm->free_list[i], link) {
182 struct drm_buddy_block *buddy;
183 u64 block_start, block_end;
184
185 if (!block->parent)
186 continue;
187
188 block_start = drm_buddy_block_offset(block);
189 block_end = block_start + drm_buddy_block_size(mm, block) - 1;
190
191 if (!contains(start, end, block_start, block_end))
192 continue;
193
194 buddy = __get_buddy(block);
195 if (!drm_buddy_block_is_free(buddy))
196 continue;
197
198 WARN_ON(drm_buddy_block_is_clear(block) ==
199 drm_buddy_block_is_clear(buddy));
200
201 /*
202 * If the prev block is same as buddy, don't access the
203 * block in the next iteration as we would free the
204 * buddy block as part of the free function.
205 */
206 if (prev == buddy)
207 prev = list_prev_entry(prev, link);
208
209 list_del(&block->link);
210 if (drm_buddy_block_is_clear(block))
211 mm->clear_avail -= drm_buddy_block_size(mm, block);
212
213 order = __drm_buddy_free(mm, block, true);
214 if (order >= min_order)
215 return 0;
216 }
217 }
218
219 return -ENOMEM;
220}
221
6387a3c4
A
222/**
223 * drm_buddy_init - init memory manager
224 *
225 * @mm: DRM buddy manager to initialize
226 * @size: size in bytes to manage
227 * @chunk_size: minimum page size in bytes for our allocations
228 *
229 * Initializes the memory manager and its resources.
230 *
231 * Returns:
232 * 0 on success, error code on failure.
233 */
234int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size)
235{
236 unsigned int i;
237 u64 offset;
238
239 if (size < chunk_size)
240 return -EINVAL;
241
242 if (chunk_size < PAGE_SIZE)
243 return -EINVAL;
244
245 if (!is_power_of_2(chunk_size))
246 return -EINVAL;
247
248 size = round_down(size, chunk_size);
249
250 mm->size = size;
251 mm->avail = size;
bb21700b 252 mm->clear_avail = 0;
6387a3c4
A
253 mm->chunk_size = chunk_size;
254 mm->max_order = ilog2(size) - ilog2(chunk_size);
255
256 BUG_ON(mm->max_order > DRM_BUDDY_MAX_ORDER);
257
258 mm->free_list = kmalloc_array(mm->max_order + 1,
259 sizeof(struct list_head),
260 GFP_KERNEL);
261 if (!mm->free_list)
262 return -ENOMEM;
263
264 for (i = 0; i <= mm->max_order; ++i)
265 INIT_LIST_HEAD(&mm->free_list[i]);
266
267 mm->n_roots = hweight64(size);
268
269 mm->roots = kmalloc_array(mm->n_roots,
270 sizeof(struct drm_buddy_block *),
271 GFP_KERNEL);
272 if (!mm->roots)
273 goto out_free_list;
274
275 offset = 0;
276 i = 0;
277
278 /*
279 * Split into power-of-two blocks, in case we are given a size that is
280 * not itself a power-of-two.
281 */
282 do {
283 struct drm_buddy_block *root;
284 unsigned int order;
285 u64 root_size;
286
4453545b
DG
287 order = ilog2(size) - ilog2(chunk_size);
288 root_size = chunk_size << order;
6387a3c4
A
289
290 root = drm_block_alloc(mm, NULL, order, offset);
291 if (!root)
292 goto out_free_roots;
293
294 mark_free(mm, root);
295
296 BUG_ON(i > mm->max_order);
297 BUG_ON(drm_buddy_block_size(mm, root) < chunk_size);
298
299 mm->roots[i] = root;
300
301 offset += root_size;
302 size -= root_size;
303 i++;
304 } while (size);
305
306 return 0;
307
308out_free_roots:
309 while (i--)
310 drm_block_free(mm, mm->roots[i]);
311 kfree(mm->roots);
312out_free_list:
313 kfree(mm->free_list);
314 return -ENOMEM;
315}
316EXPORT_SYMBOL(drm_buddy_init);
317
318/**
319 * drm_buddy_fini - tear down the memory manager
320 *
321 * @mm: DRM buddy manager to free
322 *
323 * Cleanup memory manager resources and the freelist
324 */
325void drm_buddy_fini(struct drm_buddy *mm)
326{
96950929
APS
327 u64 root_size, size;
328 unsigned int order;
6387a3c4
A
329 int i;
330
96950929
APS
331 size = mm->size;
332
6387a3c4 333 for (i = 0; i < mm->n_roots; ++i) {
96950929
APS
334 order = ilog2(size) - ilog2(mm->chunk_size);
335 __force_merge(mm, 0, size, order);
336
6387a3c4
A
337 WARN_ON(!drm_buddy_block_is_free(mm->roots[i]));
338 drm_block_free(mm, mm->roots[i]);
96950929
APS
339
340 root_size = mm->chunk_size << order;
341 size -= root_size;
6387a3c4
A
342 }
343
344 WARN_ON(mm->avail != mm->size);
345
346 kfree(mm->roots);
347 kfree(mm->free_list);
348}
349EXPORT_SYMBOL(drm_buddy_fini);
350
351static int split_block(struct drm_buddy *mm,
352 struct drm_buddy_block *block)
353{
354 unsigned int block_order = drm_buddy_block_order(block) - 1;
355 u64 offset = drm_buddy_block_offset(block);
356
357 BUG_ON(!drm_buddy_block_is_free(block));
358 BUG_ON(!drm_buddy_block_order(block));
359
360 block->left = drm_block_alloc(mm, block, block_order, offset);
361 if (!block->left)
362 return -ENOMEM;
363
364 block->right = drm_block_alloc(mm, block, block_order,
365 offset + (mm->chunk_size << block_order));
366 if (!block->right) {
367 drm_block_free(mm, block->left);
368 return -ENOMEM;
369 }
370
371 mark_free(mm, block->left);
372 mark_free(mm, block->right);
373
96950929
APS
374 if (drm_buddy_block_is_clear(block)) {
375 mark_cleared(block->left);
376 mark_cleared(block->right);
377 clear_reset(block);
378 }
379
6387a3c4
A
380 mark_split(block);
381
382 return 0;
383}
384
92937f17
A
385/**
386 * drm_get_buddy - get buddy address
387 *
388 * @block: DRM buddy block
389 *
390 * Returns the corresponding buddy block for @block, or NULL
391 * if this is a root block and can't be merged further.
392 * Requires some kind of locking to protect against
393 * any concurrent allocate and free operations.
394 */
395struct drm_buddy_block *
396drm_get_buddy(struct drm_buddy_block *block)
397{
398 return __get_buddy(block);
399}
400EXPORT_SYMBOL(drm_get_buddy);
401
6387a3c4
A
402/**
403 * drm_buddy_free_block - free a block
404 *
405 * @mm: DRM buddy manager
406 * @block: block to be freed
407 */
408void drm_buddy_free_block(struct drm_buddy *mm,
409 struct drm_buddy_block *block)
410{
411 BUG_ON(!drm_buddy_block_is_allocated(block));
412 mm->avail += drm_buddy_block_size(mm, block);
96950929
APS
413 if (drm_buddy_block_is_clear(block))
414 mm->clear_avail += drm_buddy_block_size(mm, block);
415
416 __drm_buddy_free(mm, block, false);
6387a3c4
A
417}
418EXPORT_SYMBOL(drm_buddy_free_block);
419
96950929
APS
420static void __drm_buddy_free_list(struct drm_buddy *mm,
421 struct list_head *objects,
422 bool mark_clear,
423 bool mark_dirty)
6387a3c4
A
424{
425 struct drm_buddy_block *block, *on;
426
96950929
APS
427 WARN_ON(mark_dirty && mark_clear);
428
6387a3c4 429 list_for_each_entry_safe(block, on, objects, link) {
96950929
APS
430 if (mark_clear)
431 mark_cleared(block);
432 else if (mark_dirty)
433 clear_reset(block);
6387a3c4
A
434 drm_buddy_free_block(mm, block);
435 cond_resched();
436 }
437 INIT_LIST_HEAD(objects);
438}
6387a3c4 439
96950929
APS
440static void drm_buddy_free_list_internal(struct drm_buddy *mm,
441 struct list_head *objects)
afea229f 442{
96950929
APS
443 /*
444 * Don't touch the clear/dirty bit, since allocation is still internal
445 * at this point. For example we might have just failed part of the
446 * allocation.
447 */
448 __drm_buddy_free_list(mm, objects, false, false);
afea229f
A
449}
450
96950929
APS
451/**
452 * drm_buddy_free_list - free blocks
453 *
454 * @mm: DRM buddy manager
455 * @objects: input list head to free blocks
456 * @flags: optional flags like DRM_BUDDY_CLEARED
457 */
458void drm_buddy_free_list(struct drm_buddy *mm,
459 struct list_head *objects,
460 unsigned int flags)
afea229f 461{
96950929
APS
462 bool mark_clear = flags & DRM_BUDDY_CLEARED;
463
464 __drm_buddy_free_list(mm, objects, mark_clear, !mark_clear);
465}
466EXPORT_SYMBOL(drm_buddy_free_list);
467
468static bool block_incompatible(struct drm_buddy_block *block, unsigned int flags)
469{
470 bool needs_clear = flags & DRM_BUDDY_CLEAR_ALLOCATION;
471
472 return needs_clear != drm_buddy_block_is_clear(block);
afea229f
A
473}
474
475static struct drm_buddy_block *
96950929
APS
476__alloc_range_bias(struct drm_buddy *mm,
477 u64 start, u64 end,
478 unsigned int order,
479 unsigned long flags,
480 bool fallback)
afea229f 481{
f41900e4 482 u64 req_size = mm->chunk_size << order;
afea229f
A
483 struct drm_buddy_block *block;
484 struct drm_buddy_block *buddy;
485 LIST_HEAD(dfs);
486 int err;
487 int i;
488
489 end = end - 1;
490
491 for (i = 0; i < mm->n_roots; ++i)
492 list_add_tail(&mm->roots[i]->tmp_link, &dfs);
493
494 do {
495 u64 block_start;
496 u64 block_end;
497
498 block = list_first_entry_or_null(&dfs,
499 struct drm_buddy_block,
500 tmp_link);
501 if (!block)
502 break;
503
504 list_del(&block->tmp_link);
505
506 if (drm_buddy_block_order(block) < order)
507 continue;
508
509 block_start = drm_buddy_block_offset(block);
510 block_end = block_start + drm_buddy_block_size(mm, block) - 1;
511
512 if (!overlaps(start, end, block_start, block_end))
513 continue;
514
515 if (drm_buddy_block_is_allocated(block))
516 continue;
517
f41900e4
MA
518 if (block_start < start || block_end > end) {
519 u64 adjusted_start = max(block_start, start);
520 u64 adjusted_end = min(block_end, end);
521
522 if (round_down(adjusted_end + 1, req_size) <=
523 round_up(adjusted_start, req_size))
524 continue;
525 }
526
afea229f
A
527 if (contains(start, end, block_start, block_end) &&
528 order == drm_buddy_block_order(block)) {
96950929
APS
529 if (!fallback && block_incompatible(block, flags))
530 continue;
531
afea229f
A
532 /*
533 * Find the free block within the range.
534 */
535 if (drm_buddy_block_is_free(block))
536 return block;
537
538 continue;
539 }
540
541 if (!drm_buddy_block_is_split(block)) {
542 err = split_block(mm, block);
543 if (unlikely(err))
544 goto err_undo;
545 }
546
547 list_add(&block->right->tmp_link, &dfs);
548 list_add(&block->left->tmp_link, &dfs);
549 } while (1);
550
551 return ERR_PTR(-ENOSPC);
552
553err_undo:
554 /*
555 * We really don't want to leave around a bunch of split blocks, since
556 * bigger is better, so make sure we merge everything back before we
557 * free the allocated blocks.
558 */
92937f17 559 buddy = __get_buddy(block);
afea229f
A
560 if (buddy &&
561 (drm_buddy_block_is_free(block) &&
562 drm_buddy_block_is_free(buddy)))
96950929 563 __drm_buddy_free(mm, block, false);
afea229f
A
564 return ERR_PTR(err);
565}
566
476e4063 567static struct drm_buddy_block *
96950929
APS
568__drm_buddy_alloc_range_bias(struct drm_buddy *mm,
569 u64 start, u64 end,
570 unsigned int order,
571 unsigned long flags)
572{
573 struct drm_buddy_block *block;
574 bool fallback = false;
575
576 block = __alloc_range_bias(mm, start, end, order,
577 flags, fallback);
bb21700b 578 if (IS_ERR(block))
96950929
APS
579 return __alloc_range_bias(mm, start, end, order,
580 flags, !fallback);
581
582 return block;
583}
584
585static struct drm_buddy_block *
586get_maxblock(struct drm_buddy *mm, unsigned int order,
587 unsigned long flags)
476e4063 588{
96950929 589 struct drm_buddy_block *max_block = NULL, *block = NULL;
5640e816 590 unsigned int i;
476e4063 591
5640e816 592 for (i = order; i <= mm->max_order; ++i) {
96950929
APS
593 struct drm_buddy_block *tmp_block;
594
595 list_for_each_entry_reverse(tmp_block, &mm->free_list[i], link) {
596 if (block_incompatible(tmp_block, flags))
5640e816 597 continue;
5640e816 598
96950929
APS
599 block = tmp_block;
600 break;
601 }
602
603 if (!block)
604 continue;
605
606 if (!max_block) {
607 max_block = block;
608 continue;
609 }
610
611 if (drm_buddy_block_offset(block) >
612 drm_buddy_block_offset(max_block)) {
613 max_block = block;
5640e816 614 }
476e4063
A
615 }
616
617 return max_block;
618}
619
afea229f
A
620static struct drm_buddy_block *
621alloc_from_freelist(struct drm_buddy *mm,
622 unsigned int order,
623 unsigned long flags)
6387a3c4
A
624{
625 struct drm_buddy_block *block = NULL;
5640e816 626 unsigned int tmp;
6387a3c4
A
627 int err;
628
5640e816 629 if (flags & DRM_BUDDY_TOPDOWN_ALLOCATION) {
96950929 630 block = get_maxblock(mm, order, flags);
5640e816
APS
631 if (block)
632 /* Store the obtained block order */
633 tmp = drm_buddy_block_order(block);
634 } else {
96950929
APS
635 for (tmp = order; tmp <= mm->max_order; ++tmp) {
636 struct drm_buddy_block *tmp_block;
637
638 list_for_each_entry_reverse(tmp_block, &mm->free_list[tmp], link) {
639 if (block_incompatible(tmp_block, flags))
640 continue;
641
642 block = tmp_block;
643 break;
644 }
645
646 if (block)
647 break;
648 }
649 }
650
651 if (!block) {
652 /* Fallback method */
5640e816
APS
653 for (tmp = order; tmp <= mm->max_order; ++tmp) {
654 if (!list_empty(&mm->free_list[tmp])) {
655 block = list_last_entry(&mm->free_list[tmp],
656 struct drm_buddy_block,
657 link);
658 if (block)
659 break;
660 }
476e4063 661 }
6387a3c4 662
96950929
APS
663 if (!block)
664 return ERR_PTR(-ENOSPC);
665 }
6387a3c4
A
666
667 BUG_ON(!drm_buddy_block_is_free(block));
668
5640e816 669 while (tmp != order) {
6387a3c4
A
670 err = split_block(mm, block);
671 if (unlikely(err))
afea229f 672 goto err_undo;
6387a3c4 673
afea229f 674 block = block->right;
5640e816 675 tmp--;
6387a3c4 676 }
6387a3c4
A
677 return block;
678
afea229f 679err_undo:
5640e816 680 if (tmp != order)
96950929 681 __drm_buddy_free(mm, block, false);
6387a3c4
A
682 return ERR_PTR(err);
683}
6387a3c4 684
afea229f
A
685static int __alloc_range(struct drm_buddy *mm,
686 struct list_head *dfs,
687 u64 start, u64 size,
0a1844bf
APS
688 struct list_head *blocks,
689 u64 *total_allocated_on_err)
6387a3c4
A
690{
691 struct drm_buddy_block *block;
692 struct drm_buddy_block *buddy;
0a1844bf 693 u64 total_allocated = 0;
6387a3c4 694 LIST_HEAD(allocated);
6387a3c4
A
695 u64 end;
696 int err;
6387a3c4
A
697
698 end = start + size - 1;
699
700 do {
701 u64 block_start;
702 u64 block_end;
703
afea229f 704 block = list_first_entry_or_null(dfs,
6387a3c4
A
705 struct drm_buddy_block,
706 tmp_link);
707 if (!block)
708 break;
709
710 list_del(&block->tmp_link);
711
712 block_start = drm_buddy_block_offset(block);
713 block_end = block_start + drm_buddy_block_size(mm, block) - 1;
714
715 if (!overlaps(start, end, block_start, block_end))
716 continue;
717
718 if (drm_buddy_block_is_allocated(block)) {
719 err = -ENOSPC;
720 goto err_free;
721 }
722
723 if (contains(start, end, block_start, block_end)) {
96950929
APS
724 if (drm_buddy_block_is_free(block)) {
725 mark_allocated(block);
726 total_allocated += drm_buddy_block_size(mm, block);
727 mm->avail -= drm_buddy_block_size(mm, block);
728 if (drm_buddy_block_is_clear(block))
729 mm->clear_avail -= drm_buddy_block_size(mm, block);
730 list_add_tail(&block->link, &allocated);
731 continue;
732 } else if (!mm->clear_avail) {
6387a3c4
A
733 err = -ENOSPC;
734 goto err_free;
735 }
6387a3c4
A
736 }
737
738 if (!drm_buddy_block_is_split(block)) {
739 err = split_block(mm, block);
740 if (unlikely(err))
741 goto err_undo;
742 }
743
afea229f
A
744 list_add(&block->right->tmp_link, dfs);
745 list_add(&block->left->tmp_link, dfs);
6387a3c4
A
746 } while (1);
747
8746c6c9
APS
748 if (total_allocated < size) {
749 err = -ENOSPC;
750 goto err_free;
751 }
752
02f76a9c
APS
753 list_splice_tail(&allocated, blocks);
754
6387a3c4
A
755 return 0;
756
757err_undo:
758 /*
759 * We really don't want to leave around a bunch of split blocks, since
760 * bigger is better, so make sure we merge everything back before we
761 * free the allocated blocks.
762 */
92937f17 763 buddy = __get_buddy(block);
6387a3c4
A
764 if (buddy &&
765 (drm_buddy_block_is_free(block) &&
766 drm_buddy_block_is_free(buddy)))
96950929 767 __drm_buddy_free(mm, block, false);
6387a3c4
A
768
769err_free:
0a1844bf
APS
770 if (err == -ENOSPC && total_allocated_on_err) {
771 list_splice_tail(&allocated, blocks);
772 *total_allocated_on_err = total_allocated;
773 } else {
96950929 774 drm_buddy_free_list_internal(mm, &allocated);
0a1844bf
APS
775 }
776
6387a3c4
A
777 return err;
778}
afea229f
A
779
780static int __drm_buddy_alloc_range(struct drm_buddy *mm,
781 u64 start,
782 u64 size,
0a1844bf 783 u64 *total_allocated_on_err,
afea229f
A
784 struct list_head *blocks)
785{
786 LIST_HEAD(dfs);
787 int i;
788
789 for (i = 0; i < mm->n_roots; ++i)
790 list_add_tail(&mm->roots[i]->tmp_link, &dfs);
791
0a1844bf
APS
792 return __alloc_range(mm, &dfs, start, size,
793 blocks, total_allocated_on_err);
794}
795
796static int __alloc_contig_try_harder(struct drm_buddy *mm,
797 u64 size,
798 u64 min_block_size,
799 struct list_head *blocks)
800{
801 u64 rhs_offset, lhs_offset, lhs_size, filled;
802 struct drm_buddy_block *block;
803 struct list_head *list;
804 LIST_HEAD(blocks_lhs);
805 unsigned long pages;
806 unsigned int order;
807 u64 modify_size;
808 int err;
809
810 modify_size = rounddown_pow_of_two(size);
811 pages = modify_size >> ilog2(mm->chunk_size);
812 order = fls(pages) - 1;
813 if (order == 0)
814 return -ENOSPC;
815
816 list = &mm->free_list[order];
817 if (list_empty(list))
818 return -ENOSPC;
819
820 list_for_each_entry_reverse(block, list, link) {
821 /* Allocate blocks traversing RHS */
822 rhs_offset = drm_buddy_block_offset(block);
823 err = __drm_buddy_alloc_range(mm, rhs_offset, size,
824 &filled, blocks);
825 if (!err || err != -ENOSPC)
826 return err;
827
828 lhs_size = max((size - filled), min_block_size);
829 if (!IS_ALIGNED(lhs_size, min_block_size))
830 lhs_size = round_up(lhs_size, min_block_size);
831
832 /* Allocate blocks traversing LHS */
833 lhs_offset = drm_buddy_block_offset(block) - lhs_size;
834 err = __drm_buddy_alloc_range(mm, lhs_offset, lhs_size,
835 NULL, &blocks_lhs);
836 if (!err) {
837 list_splice(&blocks_lhs, blocks);
838 return 0;
839 } else if (err != -ENOSPC) {
96950929 840 drm_buddy_free_list_internal(mm, blocks);
0a1844bf
APS
841 return err;
842 }
843 /* Free blocks for the next iteration */
96950929 844 drm_buddy_free_list_internal(mm, blocks);
0a1844bf
APS
845 }
846
847 return -ENOSPC;
afea229f
A
848}
849
95ee2a8b
A
850/**
851 * drm_buddy_block_trim - free unused pages
852 *
853 * @mm: DRM buddy manager
854 * @new_size: original size requested
855 * @blocks: Input and output list of allocated blocks.
856 * MUST contain single block as input to be trimmed.
857 * On success will contain the newly allocated blocks
858 * making up the @new_size. Blocks always appear in
859 * ascending order
860 *
861 * For contiguous allocation, we round up the size to the nearest
862 * power of two value, drivers consume *actual* size, so remaining
863 * portions are unused and can be optionally freed with this function
864 *
865 * Returns:
866 * 0 on success, error code on failure.
867 */
868int drm_buddy_block_trim(struct drm_buddy *mm,
869 u64 new_size,
870 struct list_head *blocks)
871{
872 struct drm_buddy_block *parent;
873 struct drm_buddy_block *block;
874 LIST_HEAD(dfs);
875 u64 new_start;
876 int err;
877
878 if (!list_is_singular(blocks))
879 return -EINVAL;
880
881 block = list_first_entry(blocks,
882 struct drm_buddy_block,
883 link);
884
885 if (WARN_ON(!drm_buddy_block_is_allocated(block)))
886 return -EINVAL;
887
888 if (new_size > drm_buddy_block_size(mm, block))
889 return -EINVAL;
890
891 if (!new_size || !IS_ALIGNED(new_size, mm->chunk_size))
892 return -EINVAL;
893
894 if (new_size == drm_buddy_block_size(mm, block))
895 return 0;
896
897 list_del(&block->link);
898 mark_free(mm, block);
899 mm->avail += drm_buddy_block_size(mm, block);
96950929
APS
900 if (drm_buddy_block_is_clear(block))
901 mm->clear_avail += drm_buddy_block_size(mm, block);
95ee2a8b
A
902
903 /* Prevent recursively freeing this node */
904 parent = block->parent;
905 block->parent = NULL;
906
907 new_start = drm_buddy_block_offset(block);
908 list_add(&block->tmp_link, &dfs);
0a1844bf 909 err = __alloc_range(mm, &dfs, new_start, new_size, blocks, NULL);
95ee2a8b
A
910 if (err) {
911 mark_allocated(block);
912 mm->avail -= drm_buddy_block_size(mm, block);
96950929
APS
913 if (drm_buddy_block_is_clear(block))
914 mm->clear_avail -= drm_buddy_block_size(mm, block);
95ee2a8b
A
915 list_add(&block->link, blocks);
916 }
917
918 block->parent = parent;
919 return err;
920}
921EXPORT_SYMBOL(drm_buddy_block_trim);
922
96950929
APS
923static struct drm_buddy_block *
924__drm_buddy_alloc_blocks(struct drm_buddy *mm,
925 u64 start, u64 end,
926 unsigned int order,
927 unsigned long flags)
928{
929 if (flags & DRM_BUDDY_RANGE_ALLOCATION)
930 /* Allocate traversing within the range */
931 return __drm_buddy_alloc_range_bias(mm, start, end,
932 order, flags);
933 else
934 /* Allocate from freelist */
935 return alloc_from_freelist(mm, order, flags);
936}
937
afea229f
A
938/**
939 * drm_buddy_alloc_blocks - allocate power-of-two blocks
940 *
941 * @mm: DRM buddy manager to allocate from
942 * @start: start of the allowed range for this block
943 * @end: end of the allowed range for this block
96950929 944 * @size: size of the allocation in bytes
0a1844bf 945 * @min_block_size: alignment of the allocation
afea229f
A
946 * @blocks: output list head to add allocated blocks
947 * @flags: DRM_BUDDY_*_ALLOCATION flags
948 *
949 * alloc_range_bias() called on range limitations, which traverses
950 * the tree and returns the desired block.
951 *
952 * alloc_from_freelist() called when *no* range restrictions
953 * are enforced, which picks the block from the freelist.
954 *
955 * Returns:
956 * 0 on success, error code on failure.
957 */
958int drm_buddy_alloc_blocks(struct drm_buddy *mm,
959 u64 start, u64 end, u64 size,
0a1844bf 960 u64 min_block_size,
afea229f
A
961 struct list_head *blocks,
962 unsigned long flags)
963{
964 struct drm_buddy_block *block = NULL;
0a1844bf 965 u64 original_size, original_min_size;
afea229f 966 unsigned int min_order, order;
afea229f 967 LIST_HEAD(allocated);
0a1844bf 968 unsigned long pages;
afea229f
A
969 int err;
970
971 if (size < mm->chunk_size)
972 return -EINVAL;
973
0a1844bf 974 if (min_block_size < mm->chunk_size)
afea229f
A
975 return -EINVAL;
976
0a1844bf 977 if (!is_power_of_2(min_block_size))
afea229f
A
978 return -EINVAL;
979
980 if (!IS_ALIGNED(start | end | size, mm->chunk_size))
981 return -EINVAL;
982
983 if (end > mm->size)
984 return -EINVAL;
985
986 if (range_overflows(start, size, mm->size))
987 return -EINVAL;
988
989 /* Actual range allocation */
2986314a
MA
990 if (start + size == end) {
991 if (!IS_ALIGNED(start | end, min_block_size))
992 return -EINVAL;
993
0a1844bf 994 return __drm_buddy_alloc_range(mm, start, size, NULL, blocks);
2986314a 995 }
0a1844bf
APS
996
997 original_size = size;
998 original_min_size = min_block_size;
999
1000 /* Roundup the size to power of 2 */
1001 if (flags & DRM_BUDDY_CONTIGUOUS_ALLOCATION) {
1002 size = roundup_pow_of_two(size);
1003 min_block_size = size;
1004 /* Align size value to min_block_size */
1005 } else if (!IS_ALIGNED(size, min_block_size)) {
1006 size = round_up(size, min_block_size);
1007 }
5f778760 1008
afea229f
A
1009 pages = size >> ilog2(mm->chunk_size);
1010 order = fls(pages) - 1;
0a1844bf 1011 min_order = ilog2(min_block_size) - ilog2(mm->chunk_size);
afea229f
A
1012
1013 do {
1014 order = min(order, (unsigned int)fls(pages) - 1);
1015 BUG_ON(order > mm->max_order);
1016 BUG_ON(order < min_order);
1017
1018 do {
96950929
APS
1019 block = __drm_buddy_alloc_blocks(mm, start,
1020 end,
1021 order,
1022 flags);
afea229f
A
1023 if (!IS_ERR(block))
1024 break;
1025
1026 if (order-- == min_order) {
96950929
APS
1027 /* Try allocation through force merge method */
1028 if (mm->clear_avail &&
1029 !__force_merge(mm, start, end, min_order)) {
1030 block = __drm_buddy_alloc_blocks(mm, start,
1031 end,
1032 min_order,
1033 flags);
1034 if (!IS_ERR(block)) {
1035 order = min_order;
1036 break;
1037 }
1038 }
1039
1040 /*
1041 * Try contiguous block allocation through
1042 * try harder method.
1043 */
0a1844bf
APS
1044 if (flags & DRM_BUDDY_CONTIGUOUS_ALLOCATION &&
1045 !(flags & DRM_BUDDY_RANGE_ALLOCATION))
0a1844bf
APS
1046 return __alloc_contig_try_harder(mm,
1047 original_size,
1048 original_min_size,
1049 blocks);
afea229f
A
1050 err = -ENOSPC;
1051 goto err_free;
1052 }
1053 } while (1);
1054
1055 mark_allocated(block);
1056 mm->avail -= drm_buddy_block_size(mm, block);
96950929
APS
1057 if (drm_buddy_block_is_clear(block))
1058 mm->clear_avail -= drm_buddy_block_size(mm, block);
afea229f
A
1059 kmemleak_update_trace(block);
1060 list_add_tail(&block->link, &allocated);
1061
1062 pages -= BIT(order);
1063
1064 if (!pages)
1065 break;
1066 } while (1);
1067
0a1844bf
APS
1068 /* Trim the allocated block to the required size */
1069 if (original_size != size) {
1070 struct list_head *trim_list;
1071 LIST_HEAD(temp);
1072 u64 trim_size;
1073
1074 trim_list = &allocated;
1075 trim_size = original_size;
1076
1077 if (!list_is_singular(&allocated)) {
1078 block = list_last_entry(&allocated, typeof(*block), link);
1079 list_move(&block->link, &temp);
1080 trim_list = &temp;
1081 trim_size = drm_buddy_block_size(mm, block) -
1082 (size - original_size);
1083 }
1084
1085 drm_buddy_block_trim(mm,
1086 trim_size,
1087 trim_list);
1088
1089 if (!list_empty(&temp))
1090 list_splice_tail(trim_list, &allocated);
1091 }
1092
afea229f
A
1093 list_splice_tail(&allocated, blocks);
1094 return 0;
1095
1096err_free:
96950929 1097 drm_buddy_free_list_internal(mm, &allocated);
afea229f
A
1098 return err;
1099}
1100EXPORT_SYMBOL(drm_buddy_alloc_blocks);
6387a3c4
A
1101
1102/**
1103 * drm_buddy_block_print - print block information
1104 *
1105 * @mm: DRM buddy manager
1106 * @block: DRM buddy block
1107 * @p: DRM printer to use
1108 */
1109void drm_buddy_block_print(struct drm_buddy *mm,
1110 struct drm_buddy_block *block,
1111 struct drm_printer *p)
1112{
1113 u64 start = drm_buddy_block_offset(block);
1114 u64 size = drm_buddy_block_size(mm, block);
1115
1116 drm_printf(p, "%#018llx-%#018llx: %llu\n", start, start + size, size);
1117}
1118EXPORT_SYMBOL(drm_buddy_block_print);
1119
1120/**
1121 * drm_buddy_print - print allocator state
1122 *
1123 * @mm: DRM buddy manager
1124 * @p: DRM printer to use
1125 */
1126void drm_buddy_print(struct drm_buddy *mm, struct drm_printer *p)
1127{
1128 int order;
1129
96950929
APS
1130 drm_printf(p, "chunk_size: %lluKiB, total: %lluMiB, free: %lluMiB, clear_free: %lluMiB\n",
1131 mm->chunk_size >> 10, mm->size >> 20, mm->avail >> 20, mm->clear_avail >> 20);
6387a3c4
A
1132
1133 for (order = mm->max_order; order >= 0; order--) {
1134 struct drm_buddy_block *block;
1135 u64 count = 0, free;
1136
1137 list_for_each_entry(block, &mm->free_list[order], link) {
1138 BUG_ON(!drm_buddy_block_is_free(block));
1139 count++;
1140 }
1141
cd11589b 1142 drm_printf(p, "order-%2d ", order);
6387a3c4
A
1143
1144 free = count * (mm->chunk_size << order);
1145 if (free < SZ_1M)
cd11589b 1146 drm_printf(p, "free: %8llu KiB", free >> 10);
6387a3c4 1147 else
cd11589b 1148 drm_printf(p, "free: %8llu MiB", free >> 20);
6387a3c4 1149
cd11589b 1150 drm_printf(p, ", blocks: %llu\n", count);
6387a3c4
A
1151 }
1152}
1153EXPORT_SYMBOL(drm_buddy_print);
1154
1155static void drm_buddy_module_exit(void)
1156{
1157 kmem_cache_destroy(slab_blocks);
1158}
1159
1160static int __init drm_buddy_module_init(void)
1161{
1162 slab_blocks = KMEM_CACHE(drm_buddy_block, 0);
1163 if (!slab_blocks)
1164 return -ENOMEM;
1165
1166 return 0;
1167}
1168
1169module_init(drm_buddy_module_init);
1170module_exit(drm_buddy_module_exit);
1171
1172MODULE_DESCRIPTION("DRM Buddy Allocator");
1173MODULE_LICENSE("Dual MIT/GPL");