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