1 /* SPDX-License-Identifier: MIT */
3 * Copyright © 2019 Intel Corporation
6 #ifndef __I915_BUDDY_H__
7 #define __I915_BUDDY_H__
9 #include <linux/bitops.h>
10 #include <linux/list.h>
12 struct i915_buddy_block {
13 #define I915_BUDDY_HEADER_OFFSET GENMASK_ULL(63, 12)
14 #define I915_BUDDY_HEADER_STATE GENMASK_ULL(11, 10)
15 #define I915_BUDDY_ALLOCATED (1 << 10)
16 #define I915_BUDDY_FREE (2 << 10)
17 #define I915_BUDDY_SPLIT (3 << 10)
18 /* Free to be used, if needed in the future */
19 #define I915_BUDDY_HEADER_UNUSED GENMASK_ULL(9, 6)
20 #define I915_BUDDY_HEADER_ORDER GENMASK_ULL(5, 0)
23 struct i915_buddy_block *left;
24 struct i915_buddy_block *right;
25 struct i915_buddy_block *parent;
27 void *private; /* owned by creator */
30 * While the block is allocated by the user through i915_buddy_alloc*,
31 * the user has ownership of the link, for example to maintain within
32 * a list, if so desired. As soon as the block is freed with
33 * i915_buddy_free* ownership is given back to the mm.
35 struct list_head link;
36 struct list_head tmp_link;
39 /* Order-zero must be at least PAGE_SIZE */
40 #define I915_BUDDY_MAX_ORDER (63 - PAGE_SHIFT)
43 * Binary Buddy System.
45 * Locking should be handled by the user, a simple mutex around
46 * i915_buddy_alloc* and i915_buddy_free* should suffice.
48 struct i915_buddy_mm {
49 /* Maintain a free list for each order. */
50 struct list_head *free_list;
53 * Maintain explicit binary tree(s) to track the allocation of the
54 * address space. This gives us a simple way of finding a buddy block
55 * and performing the potentially recursive merge step when freeing a
56 * block. Nodes are either allocated or free, in which case they will
57 * also exist on the respective free list.
59 struct i915_buddy_block **roots;
62 * Anything from here is public, and remains static for the lifetime of
63 * the mm. Everything above is considered do-not-touch.
66 unsigned int max_order;
68 /* Must be at least PAGE_SIZE */
74 i915_buddy_block_offset(struct i915_buddy_block *block)
76 return block->header & I915_BUDDY_HEADER_OFFSET;
79 static inline unsigned int
80 i915_buddy_block_order(struct i915_buddy_block *block)
82 return block->header & I915_BUDDY_HEADER_ORDER;
85 static inline unsigned int
86 i915_buddy_block_state(struct i915_buddy_block *block)
88 return block->header & I915_BUDDY_HEADER_STATE;
92 i915_buddy_block_is_allocated(struct i915_buddy_block *block)
94 return i915_buddy_block_state(block) == I915_BUDDY_ALLOCATED;
98 i915_buddy_block_is_free(struct i915_buddy_block *block)
100 return i915_buddy_block_state(block) == I915_BUDDY_FREE;
104 i915_buddy_block_is_split(struct i915_buddy_block *block)
106 return i915_buddy_block_state(block) == I915_BUDDY_SPLIT;
110 i915_buddy_block_size(struct i915_buddy_mm *mm,
111 struct i915_buddy_block *block)
113 return mm->chunk_size << i915_buddy_block_order(block);
116 int i915_buddy_init(struct i915_buddy_mm *mm, u64 size, u64 chunk_size);
118 void i915_buddy_fini(struct i915_buddy_mm *mm);
120 struct i915_buddy_block *
121 i915_buddy_alloc(struct i915_buddy_mm *mm, unsigned int order);
123 int i915_buddy_alloc_range(struct i915_buddy_mm *mm,
124 struct list_head *blocks,
125 u64 start, u64 size);
127 void i915_buddy_free(struct i915_buddy_mm *mm, struct i915_buddy_block *block);
129 void i915_buddy_free_list(struct i915_buddy_mm *mm, struct list_head *objects);