xref: /openbmc/linux/arch/x86/platform/pvh/enlighten.c (revision 9fb29c73)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 
4 #include <xen/hvc-console.h>
5 
6 #include <asm/io_apic.h>
7 #include <asm/hypervisor.h>
8 #include <asm/e820/api.h>
9 #include <asm/x86_init.h>
10 
11 #include <asm/xen/interface.h>
12 
13 #include <xen/xen.h>
14 #include <xen/interface/hvm/start_info.h>
15 
16 /*
17  * PVH variables.
18  *
19  * pvh_bootparams and pvh_start_info need to live in the data segment since
20  * they are used after startup_{32|64}, which clear .bss, are invoked.
21  */
22 struct boot_params pvh_bootparams __attribute__((section(".data")));
23 struct hvm_start_info pvh_start_info __attribute__((section(".data")));
24 
25 unsigned int pvh_start_info_sz = sizeof(pvh_start_info);
26 
27 static u64 pvh_get_root_pointer(void)
28 {
29 	return pvh_start_info.rsdp_paddr;
30 }
31 
32 /*
33  * Xen guests are able to obtain the memory map from the hypervisor via the
34  * HYPERVISOR_memory_op hypercall.
35  * If we are trying to boot a Xen PVH guest, it is expected that the kernel
36  * will have been configured to provide an override for this routine to do
37  * just that.
38  */
39 void __init __weak mem_map_via_hcall(struct boot_params *ptr __maybe_unused)
40 {
41 	xen_raw_printk("Error: Could not find memory map\n");
42 	BUG();
43 }
44 
45 static void __init init_pvh_bootparams(bool xen_guest)
46 {
47 	memset(&pvh_bootparams, 0, sizeof(pvh_bootparams));
48 
49 	if ((pvh_start_info.version > 0) && (pvh_start_info.memmap_entries)) {
50 		struct hvm_memmap_table_entry *ep;
51 		int i;
52 
53 		ep = __va(pvh_start_info.memmap_paddr);
54 		pvh_bootparams.e820_entries = pvh_start_info.memmap_entries;
55 
56 		for (i = 0; i < pvh_bootparams.e820_entries ; i++, ep++) {
57 			pvh_bootparams.e820_table[i].addr = ep->addr;
58 			pvh_bootparams.e820_table[i].size = ep->size;
59 			pvh_bootparams.e820_table[i].type = ep->type;
60 		}
61 	} else if (xen_guest) {
62 		mem_map_via_hcall(&pvh_bootparams);
63 	} else {
64 		/* Non-xen guests are not supported by version 0 */
65 		BUG();
66 	}
67 
68 	if (pvh_bootparams.e820_entries < E820_MAX_ENTRIES_ZEROPAGE - 1) {
69 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].addr =
70 			ISA_START_ADDRESS;
71 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].size =
72 			ISA_END_ADDRESS - ISA_START_ADDRESS;
73 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].type =
74 			E820_TYPE_RESERVED;
75 		pvh_bootparams.e820_entries++;
76 	} else
77 		xen_raw_printk("Warning: Can fit ISA range into e820\n");
78 
79 	pvh_bootparams.hdr.cmd_line_ptr =
80 		pvh_start_info.cmdline_paddr;
81 
82 	/* The first module is always ramdisk. */
83 	if (pvh_start_info.nr_modules) {
84 		struct hvm_modlist_entry *modaddr =
85 			__va(pvh_start_info.modlist_paddr);
86 		pvh_bootparams.hdr.ramdisk_image = modaddr->paddr;
87 		pvh_bootparams.hdr.ramdisk_size = modaddr->size;
88 	}
89 
90 	/*
91 	 * See Documentation/x86/boot.txt.
92 	 *
93 	 * Version 2.12 supports Xen entry point but we will use default x86/PC
94 	 * environment (i.e. hardware_subarch 0).
95 	 */
96 	pvh_bootparams.hdr.version = (2 << 8) | 12;
97 	pvh_bootparams.hdr.type_of_loader = ((xen_guest ? 0x9 : 0xb) << 4) | 0;
98 
99 	x86_init.acpi.get_root_pointer = pvh_get_root_pointer;
100 }
101 
102 /*
103  * If we are trying to boot a Xen PVH guest, it is expected that the kernel
104  * will have been configured to provide the required override for this routine.
105  */
106 void __init __weak xen_pvh_init(void)
107 {
108 	xen_raw_printk("Error: Missing xen PVH initialization\n");
109 	BUG();
110 }
111 
112 static void hypervisor_specific_init(bool xen_guest)
113 {
114 	if (xen_guest)
115 		xen_pvh_init();
116 }
117 
118 /*
119  * This routine (and those that it might call) should not use
120  * anything that lives in .bss since that segment will be cleared later.
121  */
122 void __init xen_prepare_pvh(void)
123 {
124 
125 	u32 msr = xen_cpuid_base();
126 	bool xen_guest = !!msr;
127 
128 	if (pvh_start_info.magic != XEN_HVM_START_MAGIC_VALUE) {
129 		xen_raw_printk("Error: Unexpected magic value (0x%08x)\n",
130 				pvh_start_info.magic);
131 		BUG();
132 	}
133 
134 	hypervisor_specific_init(xen_guest);
135 
136 	init_pvh_bootparams(xen_guest);
137 }
138