xref: /kvm-unit-tests/lib/alloc_page.h (revision 2c96b77ec9d3b1fcec7525174e23a6240ee05949)
1 /*
2  * This work is licensed under the terms of the GNU LGPL, version 2.
3  *
4  * This is a simple allocator that provides contiguous physical addresses
5  * with byte granularity.
6  */
7 
8 #ifndef _ALLOC_PAGE_H_
9 #define _ALLOC_PAGE_H_
10 
11 #include <stdbool.h>
12 #include <asm/memory_areas.h>
13 
14 #define AREA_ANY_NUMBER	0xff
15 
16 #define AREA_ANY	0x00000
17 #define AREA_MASK	0x0ffff
18 
19 #define FLAG_DONTZERO	0x10000
20 #define FLAG_FRESH	0x20000
21 
22 /* Returns true if the page allocator has been initialized */
23 bool page_alloc_initialized(void);
24 
25 /*
26  * Initializes a memory area.
27  * n is the number of the area to initialize
28  * base_pfn is the physical frame number of the start of the area to initialize
29  * top_pfn is the physical frame number of the first page immediately after
30  * the end of the area to initialize
31  */
32 void page_alloc_init_area(u8 n, phys_addr_t base_pfn, phys_addr_t top_pfn);
33 
34 /* Enables the page allocator. At least one area must have been initialized */
35 void page_alloc_ops_enable(void);
36 
37 /*
38  * Allocate aligned memory with the specified flags.
39  * flags is a bitmap of allowed areas and flags.
40  * alignment must be a power of 2
41  */
42 void *memalign_pages_flags(size_t alignment, size_t size, unsigned int flags);
43 
44 /*
45  * Allocate aligned memory from any area and with default flags.
46  * Equivalent to memalign_pages_flags(alignment, size, AREA_ANY).
47  */
48 static inline void *memalign_pages(size_t alignment, size_t size)
49 {
50 	return memalign_pages_flags(alignment, size, AREA_ANY);
51 }
52 
53 /*
54  * Allocate 1ull << order naturally aligned pages with the specified flags.
55  * Equivalent to memalign_pages_flags(1ull << order, 1ull << order, flags).
56  */
57 void *alloc_pages_flags(unsigned int order, unsigned int flags);
58 
59 /*
60  * Allocate 1ull << order naturally aligned pages from any area and with
61  * default flags.
62  * Equivalent to alloc_pages_flags(order, AREA_ANY);
63  */
64 static inline void *alloc_pages(unsigned int order)
65 {
66 	return alloc_pages_flags(order, AREA_ANY);
67 }
68 
69 /*
70  * Allocate one page from any area and with default flags.
71  * Equivalent to alloc_pages(0);
72  */
73 static inline void *alloc_page(void)
74 {
75 	return alloc_pages(0);
76 }
77 
78 /*
79  * Frees a memory block allocated with any of the memalign_pages* or
80  * alloc_pages* functions.
81  * The pointer must point to the start of the block.
82  */
83 void free_pages(void *mem);
84 
85 /*
86  * Free one page.
87  * Equivalent to free_pages(mem).
88  */
89 static inline void free_page(void *mem)
90 {
91 	free_pages(mem);
92 }
93 
94 /*
95  * Free pages by order.
96  * Equivalent to free_pages(mem).
97  */
98 static inline void free_pages_by_order(void *mem, unsigned int order)
99 {
100 	free_pages(mem);
101 }
102 
103 /*
104  * Reserves the specified physical memory range if possible.
105  * If the specified range cannot be reserved in its entirety, no action is
106  * performed and -1 is returned.
107  *
108  * Returns 0 in case of success, -1 otherwise.
109  */
110 int reserve_pages(phys_addr_t addr, size_t npages);
111 
112 /*
113  * Frees a reserved memory range that had been reserved with
114  * reserve_pages.
115  * The memory range does not need to match a previous allocation
116  * exactly, it can also be a subset, in which case only the specified
117  * pages will be freed and unreserved.
118  */
119 void unreserve_pages(phys_addr_t addr, size_t npages);
120 
121 #endif
122