1bf457786SArd Biesheuvel /*
2bf457786SArd Biesheuvel  * Copyright (C) 2013, 2014 Linaro Ltd;  <roy.franz@linaro.org>
3bf457786SArd Biesheuvel  *
4bf457786SArd Biesheuvel  * This file implements the EFI boot stub for the arm64 kernel.
5bf457786SArd Biesheuvel  * Adapted from ARM version by Mark Salter <msalter@redhat.com>
6bf457786SArd Biesheuvel  *
7bf457786SArd Biesheuvel  * This program is free software; you can redistribute it and/or modify
8bf457786SArd Biesheuvel  * it under the terms of the GNU General Public License version 2 as
9bf457786SArd Biesheuvel  * published by the Free Software Foundation.
10bf457786SArd Biesheuvel  *
11bf457786SArd Biesheuvel  */
12bf457786SArd Biesheuvel #include <linux/efi.h>
13bf457786SArd Biesheuvel #include <asm/efi.h>
14bf457786SArd Biesheuvel #include <asm/sections.h>
15bf457786SArd Biesheuvel 
16bf457786SArd Biesheuvel efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table_arg,
17bf457786SArd Biesheuvel 					unsigned long *image_addr,
18bf457786SArd Biesheuvel 					unsigned long *image_size,
19bf457786SArd Biesheuvel 					unsigned long *reserve_addr,
20bf457786SArd Biesheuvel 					unsigned long *reserve_size,
21bf457786SArd Biesheuvel 					unsigned long dram_base,
22bf457786SArd Biesheuvel 					efi_loaded_image_t *image)
23bf457786SArd Biesheuvel {
24bf457786SArd Biesheuvel 	efi_status_t status;
25bf457786SArd Biesheuvel 	unsigned long kernel_size, kernel_memsize = 0;
26bf457786SArd Biesheuvel 	unsigned long nr_pages;
27bf457786SArd Biesheuvel 	void *old_image_addr = (void *)*image_addr;
282dc10ad8SLinus Torvalds 	unsigned long preferred_offset;
292dc10ad8SLinus Torvalds 
302dc10ad8SLinus Torvalds 	/*
312dc10ad8SLinus Torvalds 	 * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond
322dc10ad8SLinus Torvalds 	 * a 2 MB aligned base, which itself may be lower than dram_base, as
332dc10ad8SLinus Torvalds 	 * long as the resulting offset equals or exceeds it.
342dc10ad8SLinus Torvalds 	 */
352dc10ad8SLinus Torvalds 	preferred_offset = round_down(dram_base, SZ_2M) + TEXT_OFFSET;
362dc10ad8SLinus Torvalds 	if (preferred_offset < dram_base)
372dc10ad8SLinus Torvalds 		preferred_offset += SZ_2M;
38bf457786SArd Biesheuvel 
39bf457786SArd Biesheuvel 	/* Relocate the image, if required. */
40bf457786SArd Biesheuvel 	kernel_size = _edata - _text;
412dc10ad8SLinus Torvalds 	if (*image_addr != preferred_offset) {
42bf457786SArd Biesheuvel 		kernel_memsize = kernel_size + (_end - _edata);
43bf457786SArd Biesheuvel 
44bf457786SArd Biesheuvel 		/*
45bf457786SArd Biesheuvel 		 * First, try a straight allocation at the preferred offset.
46bf457786SArd Biesheuvel 		 * This will work around the issue where, if dram_base == 0x0,
47bf457786SArd Biesheuvel 		 * efi_low_alloc() refuses to allocate at 0x0 (to prevent the
48bf457786SArd Biesheuvel 		 * address of the allocation to be mistaken for a FAIL return
49bf457786SArd Biesheuvel 		 * value or a NULL pointer). It will also ensure that, on
50bf457786SArd Biesheuvel 		 * platforms where the [dram_base, dram_base + TEXT_OFFSET)
51bf457786SArd Biesheuvel 		 * interval is partially occupied by the firmware (like on APM
52bf457786SArd Biesheuvel 		 * Mustang), we can still place the kernel at the address
53bf457786SArd Biesheuvel 		 * 'dram_base + TEXT_OFFSET'.
54bf457786SArd Biesheuvel 		 */
552dc10ad8SLinus Torvalds 		*image_addr = *reserve_addr = preferred_offset;
56bf457786SArd Biesheuvel 		nr_pages = round_up(kernel_memsize, EFI_ALLOC_ALIGN) /
57bf457786SArd Biesheuvel 			   EFI_PAGE_SIZE;
58bf457786SArd Biesheuvel 		status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
59bf457786SArd Biesheuvel 					EFI_LOADER_DATA, nr_pages,
60bf457786SArd Biesheuvel 					(efi_physical_addr_t *)reserve_addr);
61bf457786SArd Biesheuvel 		if (status != EFI_SUCCESS) {
62bf457786SArd Biesheuvel 			kernel_memsize += TEXT_OFFSET;
63bf457786SArd Biesheuvel 			status = efi_low_alloc(sys_table_arg, kernel_memsize,
64bf457786SArd Biesheuvel 					       SZ_2M, reserve_addr);
65bf457786SArd Biesheuvel 
66bf457786SArd Biesheuvel 			if (status != EFI_SUCCESS) {
67bf457786SArd Biesheuvel 				pr_efi_err(sys_table_arg, "Failed to relocate kernel\n");
68bf457786SArd Biesheuvel 				return status;
69bf457786SArd Biesheuvel 			}
70bf457786SArd Biesheuvel 			*image_addr = *reserve_addr + TEXT_OFFSET;
71bf457786SArd Biesheuvel 		}
72bf457786SArd Biesheuvel 		memcpy((void *)*image_addr, old_image_addr, kernel_size);
73bf457786SArd Biesheuvel 		*reserve_size = kernel_memsize;
74bf457786SArd Biesheuvel 	}
75bf457786SArd Biesheuvel 
76bf457786SArd Biesheuvel 
77bf457786SArd Biesheuvel 	return EFI_SUCCESS;
78bf457786SArd Biesheuvel }
79