^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "efistub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) static efi_guid_t cpu_state_guid = LINUX_EFI_ARM_CPU_STATE_TABLE_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct efi_arm_entry_state *efi_entry_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static void get_cpu_state(u32 *cpsr, u32 *sctlr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) asm("mrs %0, cpsr" : "=r"(*cpsr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if ((*cpsr & MODE_MASK) == HYP_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) asm("mrc p15, 4, %0, c1, c0, 0" : "=r"(*sctlr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) efi_status_t check_platform_features(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u32 cpsr, sctlr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) get_cpu_state(&cpsr, &sctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) efi_info("Entering in %s mode with MMU %sabled\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) ((cpsr & MODE_MASK) == HYP_MODE) ? "HYP" : "SVC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) (sctlr & 1) ? "en" : "dis");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) sizeof(*efi_entry_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) (void **)&efi_entry_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) efi_err("allocate_pool() failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) efi_entry_state->cpsr_before_ebs = cpsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) efi_entry_state->sctlr_before_ebs = sctlr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) status = efi_bs_call(install_configuration_table, &cpu_state_guid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) efi_entry_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) efi_err("install_configuration_table() failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) goto free_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* non-LPAE kernels can run anywhere */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!IS_ENABLED(CONFIG_ARM_LPAE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* LPAE kernels need compatible hardware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (block < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) efi_err("This LPAE kernel is not supported by your CPU\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) status = EFI_UNSUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) goto drop_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) drop_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) efi_bs_call(install_configuration_table, &cpu_state_guid, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) free_state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) efi_bs_call(free_pool, efi_entry_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) void efi_handle_post_ebs_state(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) get_cpu_state(&efi_entry_state->cpsr_after_ebs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) &efi_entry_state->sctlr_after_ebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct screen_info *alloc_screen_info(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct screen_info *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * Unlike on arm64, where we can directly fill out the screen_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * structure from the stub, we need to allocate a buffer to hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * its contents while we hand over to the kernel proper from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * decompressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) sizeof(*si), (void **)&si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) status = efi_bs_call(install_configuration_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) &screen_info_guid, si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (status == EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) efi_bs_call(free_pool, si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void free_screen_info(struct screen_info *si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) efi_bs_call(install_configuration_table, &screen_info_guid, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) efi_bs_call(free_pool, si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) efi_status_t handle_kernel_image(unsigned long *image_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) unsigned long *image_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned long *reserve_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unsigned long *reserve_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) efi_loaded_image_t *image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) const int slack = TEXT_OFFSET - 5 * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned long alloc_base, kernel_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Allocate space for the decompressed kernel as low as possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * The region should be 16 MiB aligned, but the first 'slack' bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * are not used by Linux, so we allow those to be occupied by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) status = efi_low_alloc_above(alloc_size, EFI_PAGE_SIZE, &alloc_base, 0x0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) efi_err("Unable to allocate memory for uncompressed kernel.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if ((alloc_base % EFI_PHYS_ALIGN) > slack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * More than 'slack' bytes are already occupied at the base of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * the allocation, so we need to advance to the next 16 MiB block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) kernel_base = round_up(alloc_base, EFI_PHYS_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) alloc_base, kernel_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) kernel_base = round_down(alloc_base, EFI_PHYS_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *reserve_addr = kernel_base + slack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* now free the parts that we will not use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (*reserve_addr > alloc_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) efi_bs_call(free_pages, alloc_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) (*reserve_addr - alloc_base) / EFI_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) alloc_size -= *reserve_addr - alloc_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) (alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *image_addr = kernel_base + TEXT_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *image_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) *image_addr, *reserve_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }