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 118131e91aSClaudio Imbrenda #include <asm/memory_areas.h> 128131e91aSClaudio Imbrenda 13*322cdd64SClaudio Imbrenda #define AREA_ANY -1 14*322cdd64SClaudio Imbrenda #define AREA_ANY_NUMBER 0xff 15*322cdd64SClaudio Imbrenda 168131e91aSClaudio Imbrenda /* Returns true if the page allocator has been initialized */ 17bf62a925SAndrew Jones bool page_alloc_initialized(void); 188131e91aSClaudio Imbrenda 198131e91aSClaudio Imbrenda /* 208131e91aSClaudio Imbrenda * Initializes a memory area. 218131e91aSClaudio Imbrenda * n is the number of the area to initialize 228131e91aSClaudio Imbrenda * base_pfn is the physical frame number of the start of the area to initialize 238131e91aSClaudio Imbrenda * top_pfn is the physical frame number of the first page immediately after 248131e91aSClaudio Imbrenda * the end of the area to initialize 258131e91aSClaudio Imbrenda */ 268131e91aSClaudio Imbrenda void page_alloc_init_area(u8 n, uintptr_t base_pfn, uintptr_t top_pfn); 278131e91aSClaudio Imbrenda 288131e91aSClaudio Imbrenda /* Enables the page allocator. At least one area must have been initialized */ 29be60de6fSAndrew Jones void page_alloc_ops_enable(void); 308131e91aSClaudio Imbrenda 318131e91aSClaudio Imbrenda /* 328131e91aSClaudio Imbrenda * Allocate aligned memory from the specified areas. 338131e91aSClaudio Imbrenda * areas is a bitmap of allowed areas 348131e91aSClaudio Imbrenda * alignment must be a power of 2 358131e91aSClaudio Imbrenda */ 368131e91aSClaudio Imbrenda void *memalign_pages_area(unsigned int areas, size_t alignment, size_t size); 378131e91aSClaudio Imbrenda 388131e91aSClaudio Imbrenda /* 398131e91aSClaudio Imbrenda * Allocate aligned memory from any area. 408131e91aSClaudio Imbrenda * Equivalent to memalign_pages_area(~0, alignment, size). 418131e91aSClaudio Imbrenda */ 428131e91aSClaudio Imbrenda void *memalign_pages(size_t alignment, size_t size); 438131e91aSClaudio Imbrenda 448131e91aSClaudio Imbrenda /* 458131e91aSClaudio Imbrenda * Allocate naturally aligned memory from the specified areas. 468131e91aSClaudio Imbrenda * Equivalent to memalign_pages_area(areas, 1ull << order, 1ull << order). 478131e91aSClaudio Imbrenda */ 488131e91aSClaudio Imbrenda void *alloc_pages_area(unsigned int areas, unsigned int order); 498131e91aSClaudio Imbrenda 508131e91aSClaudio Imbrenda /* 518131e91aSClaudio Imbrenda * Allocate one page from any area. 528131e91aSClaudio Imbrenda * Equivalent to alloc_pages(0); 538131e91aSClaudio Imbrenda */ 54da7eceb3SThomas Huth void *alloc_page(void); 558131e91aSClaudio Imbrenda 568131e91aSClaudio Imbrenda /* 578131e91aSClaudio Imbrenda * Allocate naturally aligned memory from any area. 588131e91aSClaudio Imbrenda * Equivalent to alloc_pages_area(~0, order); 598131e91aSClaudio Imbrenda */ 6073f4b202SClaudio Imbrenda void *alloc_pages(unsigned int order); 618131e91aSClaudio Imbrenda 628131e91aSClaudio Imbrenda /* 638131e91aSClaudio Imbrenda * Frees a memory block allocated with any of the memalign_pages* or 648131e91aSClaudio Imbrenda * alloc_pages* functions. 658131e91aSClaudio Imbrenda * The pointer must point to the start of the block. 668131e91aSClaudio Imbrenda */ 67f90ddba3SClaudio Imbrenda void free_pages(void *mem); 688131e91aSClaudio Imbrenda 698131e91aSClaudio Imbrenda /* For backwards compatibility */ 708131e91aSClaudio Imbrenda static inline void free_page(void *mem) 718131e91aSClaudio Imbrenda { 72f90ddba3SClaudio Imbrenda return free_pages(mem); 738131e91aSClaudio Imbrenda } 748131e91aSClaudio Imbrenda 758131e91aSClaudio Imbrenda /* For backwards compatibility */ 768131e91aSClaudio Imbrenda static inline void free_pages_by_order(void *mem, unsigned int order) 778131e91aSClaudio Imbrenda { 78f90ddba3SClaudio Imbrenda free_pages(mem); 798131e91aSClaudio Imbrenda } 805aca024eSPaolo Bonzini 8134c95065SClaudio Imbrenda /* 8234c95065SClaudio Imbrenda * Allocates and reserves the specified memory range if possible. 8334c95065SClaudio Imbrenda * Returns NULL in case of failure. 8434c95065SClaudio Imbrenda */ 8534c95065SClaudio Imbrenda void *alloc_pages_special(uintptr_t addr, size_t npages); 8634c95065SClaudio Imbrenda 8734c95065SClaudio Imbrenda /* 8834c95065SClaudio Imbrenda * Frees a reserved memory range that had been reserved with 8934c95065SClaudio Imbrenda * alloc_pages_special. 9034c95065SClaudio Imbrenda * The memory range does not need to match a previous allocation 9134c95065SClaudio Imbrenda * exactly, it can also be a subset, in which case only the specified 9234c95065SClaudio Imbrenda * pages will be freed and unreserved. 9334c95065SClaudio Imbrenda */ 9434c95065SClaudio Imbrenda void free_pages_special(uintptr_t addr, size_t npages); 9534c95065SClaudio Imbrenda 965aca024eSPaolo Bonzini #endif 97