xref: /kvm-unit-tests/lib/alloc_page.h (revision 8131e91a4b61a1b6a3e74745cc8472078e357055)
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 1
10 
11 #include <asm/memory_areas.h>
12 
13 /* Returns true if the page allocator has been initialized */
14 bool page_alloc_initialized(void);
15 
16 /*
17  * Initializes a memory area.
18  * n is the number of the area to initialize
19  * base_pfn is the physical frame number of the start of the area to initialize
20  * top_pfn is the physical frame number of the first page immediately after
21  * the end of the area to initialize
22  */
23 void page_alloc_init_area(u8 n, uintptr_t base_pfn, uintptr_t top_pfn);
24 
25 /* Enables the page allocator. At least one area must have been initialized */
26 void page_alloc_ops_enable(void);
27 
28 /*
29  * Allocate aligned memory from the specified areas.
30  * areas is a bitmap of allowed areas
31  * alignment must be a power of 2
32  */
33 void *memalign_pages_area(unsigned int areas, size_t alignment, size_t size);
34 
35 /*
36  * Allocate aligned memory from any area.
37  * Equivalent to memalign_pages_area(~0, alignment, size).
38  */
39 void *memalign_pages(size_t alignment, size_t size);
40 
41 /*
42  * Allocate naturally aligned memory from the specified areas.
43  * Equivalent to memalign_pages_area(areas, 1ull << order, 1ull << order).
44  */
45 void *alloc_pages_area(unsigned int areas, unsigned int order);
46 
47 /*
48  * Allocate one page from any area.
49  * Equivalent to alloc_pages(0);
50  */
51 void *alloc_page(void);
52 
53 /*
54  * Allocate naturally aligned memory from any area.
55  * Equivalent to alloc_pages_area(~0, order);
56  */
57 void *alloc_pages(unsigned int order);
58 
59 /*
60  * Frees a memory block allocated with any of the memalign_pages* or
61  * alloc_pages* functions.
62  * The pointer must point to the start of the block.
63  */
64 void free_pages(void *mem, size_t size);
65 
66 /* For backwards compatibility */
67 static inline void free_page(void *mem)
68 {
69 	return free_pages(mem, 1);
70 }
71 
72 /* For backwards compatibility */
73 static inline void free_pages_by_order(void *mem, unsigned int order)
74 {
75 	free_pages(mem, 1ull << order);
76 }
77 
78 #endif
79