Commit | Line | Data |
---|---|---|
3c7f2550 MS |
1 | /* |
2 | * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org> | |
3 | * | |
4 | * This file implements the EFI boot stub for the arm64 kernel. | |
5 | * Adapted from ARM version by Mark Salter <msalter@redhat.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | */ | |
12 | #include <linux/efi.h> | |
a13b0077 | 13 | #include <asm/efi.h> |
3c7f2550 | 14 | #include <asm/sections.h> |
42b55734 AB |
15 | #include <asm/sysreg.h> |
16 | ||
17 | efi_status_t check_platform_features(efi_system_table_t *sys_table_arg) | |
18 | { | |
19 | u64 tg; | |
20 | ||
21 | /* UEFI mandates support for 4 KB granularity, no need to check */ | |
22 | if (IS_ENABLED(CONFIG_ARM64_4K_PAGES)) | |
23 | return EFI_SUCCESS; | |
24 | ||
25 | tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf; | |
26 | if (tg != ID_AA64MMFR0_TGRAN_SUPPORTED) { | |
27 | if (IS_ENABLED(CONFIG_ARM64_64K_PAGES)) | |
28 | pr_efi_err(sys_table_arg, "This 64 KB granular kernel is not supported by your CPU\n"); | |
29 | else | |
30 | pr_efi_err(sys_table_arg, "This 16 KB granular kernel is not supported by your CPU\n"); | |
31 | return EFI_UNSUPPORTED; | |
32 | } | |
33 | return EFI_SUCCESS; | |
34 | } | |
3c7f2550 | 35 | |
dae31fd2 AB |
36 | efi_status_t handle_kernel_image(efi_system_table_t *sys_table_arg, |
37 | unsigned long *image_addr, | |
38 | unsigned long *image_size, | |
39 | unsigned long *reserve_addr, | |
40 | unsigned long *reserve_size, | |
41 | unsigned long dram_base, | |
42 | efi_loaded_image_t *image) | |
3c7f2550 MS |
43 | { |
44 | efi_status_t status; | |
45 | unsigned long kernel_size, kernel_memsize = 0; | |
e38457c3 AB |
46 | unsigned long nr_pages; |
47 | void *old_image_addr = (void *)*image_addr; | |
73effccb AB |
48 | unsigned long preferred_offset; |
49 | ||
50 | /* | |
51 | * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond | |
52 | * a 2 MB aligned base, which itself may be lower than dram_base, as | |
53 | * long as the resulting offset equals or exceeds it. | |
54 | */ | |
55 | preferred_offset = round_down(dram_base, SZ_2M) + TEXT_OFFSET; | |
56 | if (preferred_offset < dram_base) | |
57 | preferred_offset += SZ_2M; | |
3c7f2550 MS |
58 | |
59 | /* Relocate the image, if required. */ | |
60 | kernel_size = _edata - _text; | |
73effccb | 61 | if (*image_addr != preferred_offset) { |
3c7f2550 | 62 | kernel_memsize = kernel_size + (_end - _edata); |
e38457c3 AB |
63 | |
64 | /* | |
65 | * First, try a straight allocation at the preferred offset. | |
66 | * This will work around the issue where, if dram_base == 0x0, | |
67 | * efi_low_alloc() refuses to allocate at 0x0 (to prevent the | |
68 | * address of the allocation to be mistaken for a FAIL return | |
69 | * value or a NULL pointer). It will also ensure that, on | |
70 | * platforms where the [dram_base, dram_base + TEXT_OFFSET) | |
71 | * interval is partially occupied by the firmware (like on APM | |
72 | * Mustang), we can still place the kernel at the address | |
73 | * 'dram_base + TEXT_OFFSET'. | |
74 | */ | |
73effccb | 75 | *image_addr = *reserve_addr = preferred_offset; |
e38457c3 AB |
76 | nr_pages = round_up(kernel_memsize, EFI_ALLOC_ALIGN) / |
77 | EFI_PAGE_SIZE; | |
78 | status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS, | |
79 | EFI_LOADER_DATA, nr_pages, | |
80 | (efi_physical_addr_t *)reserve_addr); | |
3c7f2550 | 81 | if (status != EFI_SUCCESS) { |
e38457c3 AB |
82 | kernel_memsize += TEXT_OFFSET; |
83 | status = efi_low_alloc(sys_table_arg, kernel_memsize, | |
84 | SZ_2M, reserve_addr); | |
85 | ||
86 | if (status != EFI_SUCCESS) { | |
87 | pr_efi_err(sys_table_arg, "Failed to relocate kernel\n"); | |
88 | return status; | |
89 | } | |
90 | *image_addr = *reserve_addr + TEXT_OFFSET; | |
3c7f2550 | 91 | } |
e38457c3 AB |
92 | memcpy((void *)*image_addr, old_image_addr, kernel_size); |
93 | *reserve_size = kernel_memsize; | |
3c7f2550 MS |
94 | } |
95 | ||
96 | ||
97 | return EFI_SUCCESS; | |
98 | } |