1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Author: Yun Liu <liuyun@loongson.cn> 4 * Huacai Chen <chenhuacai@loongson.cn> 5 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 6 */ 7 8 #include <asm/efi.h> 9 #include <asm/addrspace.h> 10 #include "efistub.h" 11 #include "loongarch-stub.h" 12 13 typedef void __noreturn (*kernel_entry_t)(bool efi, unsigned long cmdline, 14 unsigned long systab); 15 16 efi_status_t check_platform_features(void) 17 { 18 return EFI_SUCCESS; 19 } 20 21 void efi_cache_sync_image(unsigned long image_base, unsigned long alloc_size) 22 { 23 asm volatile ("ibar 0" ::: "memory"); 24 } 25 26 unsigned long efi_get_kimg_kaslr_address(void) 27 { 28 unsigned int random_offset = 0; 29 30 #ifdef CONFIG_RANDOMIZE_BASE 31 if (!efi_nokaslr) { 32 efi_get_random_bytes(sizeof(random_offset), (u8 *)&random_offset); 33 random_offset ^= (random_get_entropy() << 16); 34 random_offset &= (CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1); 35 random_offset = ALIGN(random_offset + SZ_64K, SZ_64K); 36 } 37 #endif 38 39 return PHYSADDR(VMLINUX_LOAD_ADDRESS) + random_offset; 40 } 41 42 struct exit_boot_struct { 43 efi_memory_desc_t *runtime_map; 44 int runtime_entry_count; 45 }; 46 47 static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv) 48 { 49 struct exit_boot_struct *p = priv; 50 51 /* 52 * Update the memory map with virtual addresses. The function will also 53 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME 54 * entries so that we can pass it straight to SetVirtualAddressMap() 55 */ 56 efi_get_virtmap(map->map, map->map_size, map->desc_size, 57 p->runtime_map, &p->runtime_entry_count); 58 59 return EFI_SUCCESS; 60 } 61 62 unsigned long __weak kernel_entry_address(unsigned long kernel_addr, 63 efi_loaded_image_t *image) 64 { 65 return *(unsigned long *)(kernel_addr + 8) - PHYSADDR(VMLINUX_LOAD_ADDRESS) + kernel_addr; 66 } 67 68 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, 69 unsigned long kernel_addr, char *cmdline_ptr) 70 { 71 kernel_entry_t real_kernel_entry; 72 struct exit_boot_struct priv; 73 unsigned long desc_size; 74 efi_status_t status; 75 u32 desc_ver; 76 77 status = efi_alloc_virtmap(&priv.runtime_map, &desc_size, &desc_ver); 78 if (status != EFI_SUCCESS) { 79 efi_err("Unable to retrieve UEFI memory map.\n"); 80 return status; 81 } 82 83 efi_info("Exiting boot services\n"); 84 85 efi_novamap = false; 86 status = efi_exit_boot_services(handle, &priv, exit_boot_func); 87 if (status != EFI_SUCCESS) 88 return status; 89 90 /* Install the new virtual address map */ 91 efi_rt_call(set_virtual_address_map, 92 priv.runtime_entry_count * desc_size, desc_size, 93 desc_ver, priv.runtime_map); 94 95 /* Config Direct Mapping */ 96 csr_write(CSR_DMW0_INIT, LOONGARCH_CSR_DMWIN0); 97 csr_write(CSR_DMW1_INIT, LOONGARCH_CSR_DMWIN1); 98 csr_write(CSR_DMW2_INIT, LOONGARCH_CSR_DMWIN2); 99 csr_write(CSR_DMW3_INIT, LOONGARCH_CSR_DMWIN3); 100 101 real_kernel_entry = (void *)kernel_entry_address(kernel_addr, image); 102 103 real_kernel_entry(true, (unsigned long)cmdline_ptr, 104 (unsigned long)efi_system_table); 105 } 106