#include <linux/efi.h>
#include <asm/efi.h>
#include "efistub.h"
efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
					unsigned long max, unsigned long align,
					int memory_type)
{
	efi_physical_addr_t alloc_addr;
	efi_status_t status;
	int slack;
	max = min(max, EFI_ALLOC_LIMIT);
	if (align < EFI_ALLOC_ALIGN)
		align = EFI_ALLOC_ALIGN;
	alloc_addr = ALIGN_DOWN(max + 1, align) - 1;
	size = round_up(size, EFI_ALLOC_ALIGN);
	slack = align / EFI_PAGE_SIZE - 1;
	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
			     memory_type, size / EFI_PAGE_SIZE + slack,
			     &alloc_addr);
	if (status != EFI_SUCCESS)
		return status;
	*addr = ALIGN((unsigned long)alloc_addr, align);
	if (slack > 0) {
		int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
		if (l) {
			efi_bs_call(free_pages, alloc_addr, slack - l + 1);
			slack = l - 1;
		}
		if (slack)
			efi_bs_call(free_pages, *addr + size, slack);
	}
	return EFI_SUCCESS;
}