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 
check_platform_features(void)16 efi_status_t check_platform_features(void)
17 {
18 	return EFI_SUCCESS;
19 }
20 
21 struct exit_boot_struct {
22 	efi_memory_desc_t	*runtime_map;
23 	int			runtime_entry_count;
24 };
25 
exit_boot_func(struct efi_boot_memmap * map,void * priv)26 static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
27 {
28 	struct exit_boot_struct *p = priv;
29 
30 	/*
31 	 * Update the memory map with virtual addresses. The function will also
32 	 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
33 	 * entries so that we can pass it straight to SetVirtualAddressMap()
34 	 */
35 	efi_get_virtmap(map->map, map->map_size, map->desc_size,
36 			p->runtime_map, &p->runtime_entry_count);
37 
38 	return EFI_SUCCESS;
39 }
40 
kernel_entry_address(unsigned long kernel_addr,efi_loaded_image_t * image)41 unsigned long __weak kernel_entry_address(unsigned long kernel_addr,
42 		efi_loaded_image_t *image)
43 {
44 	return *(unsigned long *)(kernel_addr + 8) - PHYSADDR(VMLINUX_LOAD_ADDRESS) + kernel_addr;
45 }
46 
efi_boot_kernel(void * handle,efi_loaded_image_t * image,unsigned long kernel_addr,char * cmdline_ptr)47 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
48 			     unsigned long kernel_addr, char *cmdline_ptr)
49 {
50 	kernel_entry_t real_kernel_entry;
51 	struct exit_boot_struct priv;
52 	unsigned long desc_size;
53 	efi_status_t status;
54 	u32 desc_ver;
55 
56 	status = efi_alloc_virtmap(&priv.runtime_map, &desc_size, &desc_ver);
57 	if (status != EFI_SUCCESS) {
58 		efi_err("Unable to retrieve UEFI memory map.\n");
59 		return status;
60 	}
61 
62 	efi_info("Exiting boot services\n");
63 
64 	efi_novamap = false;
65 	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
66 	if (status != EFI_SUCCESS)
67 		return status;
68 
69 	/* Install the new virtual address map */
70 	efi_rt_call(set_virtual_address_map,
71 		    priv.runtime_entry_count * desc_size, desc_size,
72 		    desc_ver, priv.runtime_map);
73 
74 	/* Config Direct Mapping */
75 	csr_write64(CSR_DMW0_INIT, LOONGARCH_CSR_DMWIN0);
76 	csr_write64(CSR_DMW1_INIT, LOONGARCH_CSR_DMWIN1);
77 
78 	real_kernel_entry = (void *)kernel_entry_address(kernel_addr, image);
79 
80 	real_kernel_entry(true, (unsigned long)cmdline_ptr,
81 			  (unsigned long)efi_system_table);
82 }
83