1 /* Copyright (c) 2007, Sandia National Laboratories */
6 #include <linux/list.h>
7 #include <linux/rbtree.h>
11 struct buddy_memzone {
12 unsigned long max_order; /** max size of memory pool = 2^max_order */
13 unsigned long min_order; /** minimum allocatable block size */
16 struct list_head *avail; /** one free list for each block size,
17 * indexed by block order:
18 * avail[i] = free list of 2^i blocks
21 spinlock_t lock; /** For now we will lock all zone operations...
22 * Hopefully this does not cause a performance issue
25 unsigned int node_id; /** The NUMA node this zone allocates from
29 unsigned long num_pools;
30 struct rb_root mempools;
34 * This structure stores the state of a buddy system memory allocator object.
36 struct buddy_mempool {
37 struct buddy_memzone * zone;
39 unsigned long base_addr; /** physical base address of the memory pool */
42 unsigned long pool_order; /** Size of this memory pool = 2^pool_order */
44 unsigned long num_blocks; /** number of bits in tag_bits */
45 unsigned long *tag_bits; /** one bit for each 2^min_order block
46 * 0 = block is allocated
47 * 1 = block is available
50 unsigned long num_free_blocks;
52 void *user_metadata; // whatever the user wants
54 struct rb_node tree_node;
59 * Each free block has one of these structures at its head. The link member
60 * provides linkage for the mp->avail[order] free list, where order is the
61 * size of the free block.
64 struct buddy_mempool * mp;
65 struct list_head link;
70 extern struct buddy_memzone *
71 buddy_init(unsigned long pool_order,
72 unsigned long min_order,
73 unsigned int node_id);
76 buddy_deinit(struct buddy_memzone * zone,
77 int (*free_callback)(void *user_metadata));
79 /* Add pool at given physical address */
81 buddy_add_pool(struct buddy_memzone * zone,
82 unsigned long base_addr,
83 unsigned long pool_order,
87 /* Remove pool based at given physical address */
89 buddy_remove_pool(struct buddy_memzone * zone,
90 unsigned long base_addr,
92 void **user_metadata);
95 // constraint=0 => no constraints, otherwise bitmask of:
96 #define LWK_BUDDY_CONSTRAINT_4GB 0x1
98 /* Allocate pages, returns physical address */
100 buddy_alloc(struct buddy_memzone * zone,
105 /* Free a physical address */
107 buddy_free(struct buddy_memzone * zone,
109 unsigned long order);
113 buddy_dump_memzone(struct buddy_memzone * zone);