xref: /openbmc/linux/arch/x86/xen/enlighten_pvh.c (revision e3d786a3)
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 #include <asm/xen/hypercall.h>
13 
14 #include <xen/xen.h>
15 #include <xen/interface/memory.h>
16 #include <xen/interface/hvm/start_info.h>
17 
18 /*
19  * PVH variables.
20  *
21  * xen_pvh pvh_bootparams and pvh_start_info need to live in data segment
22  * since they are used after startup_{32|64}, which clear .bss, are invoked.
23  */
24 bool xen_pvh __attribute__((section(".data"))) = 0;
25 struct boot_params pvh_bootparams __attribute__((section(".data")));
26 struct hvm_start_info pvh_start_info __attribute__((section(".data")));
27 
28 unsigned int pvh_start_info_sz = sizeof(pvh_start_info);
29 
30 static u64 pvh_get_root_pointer(void)
31 {
32 	return pvh_start_info.rsdp_paddr;
33 }
34 
35 static void __init init_pvh_bootparams(void)
36 {
37 	struct xen_memory_map memmap;
38 	int rc;
39 
40 	memset(&pvh_bootparams, 0, sizeof(pvh_bootparams));
41 
42 	memmap.nr_entries = ARRAY_SIZE(pvh_bootparams.e820_table);
43 	set_xen_guest_handle(memmap.buffer, pvh_bootparams.e820_table);
44 	rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
45 	if (rc) {
46 		xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc);
47 		BUG();
48 	}
49 	pvh_bootparams.e820_entries = memmap.nr_entries;
50 
51 	if (pvh_bootparams.e820_entries < E820_MAX_ENTRIES_ZEROPAGE - 1) {
52 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].addr =
53 			ISA_START_ADDRESS;
54 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].size =
55 			ISA_END_ADDRESS - ISA_START_ADDRESS;
56 		pvh_bootparams.e820_table[pvh_bootparams.e820_entries].type =
57 			E820_TYPE_RESERVED;
58 		pvh_bootparams.e820_entries++;
59 	} else
60 		xen_raw_printk("Warning: Can fit ISA range into e820\n");
61 
62 	pvh_bootparams.hdr.cmd_line_ptr =
63 		pvh_start_info.cmdline_paddr;
64 
65 	/* The first module is always ramdisk. */
66 	if (pvh_start_info.nr_modules) {
67 		struct hvm_modlist_entry *modaddr =
68 			__va(pvh_start_info.modlist_paddr);
69 		pvh_bootparams.hdr.ramdisk_image = modaddr->paddr;
70 		pvh_bootparams.hdr.ramdisk_size = modaddr->size;
71 	}
72 
73 	/*
74 	 * See Documentation/x86/boot.txt.
75 	 *
76 	 * Version 2.12 supports Xen entry point but we will use default x86/PC
77 	 * environment (i.e. hardware_subarch 0).
78 	 */
79 	pvh_bootparams.hdr.version = (2 << 8) | 12;
80 	pvh_bootparams.hdr.type_of_loader = (9 << 4) | 0; /* Xen loader */
81 
82 	x86_init.acpi.get_root_pointer = pvh_get_root_pointer;
83 }
84 
85 /*
86  * This routine (and those that it might call) should not use
87  * anything that lives in .bss since that segment will be cleared later.
88  */
89 void __init xen_prepare_pvh(void)
90 {
91 	u32 msr;
92 	u64 pfn;
93 
94 	if (pvh_start_info.magic != XEN_HVM_START_MAGIC_VALUE) {
95 		xen_raw_printk("Error: Unexpected magic value (0x%08x)\n",
96 				pvh_start_info.magic);
97 		BUG();
98 	}
99 
100 	xen_pvh = 1;
101 	xen_start_flags = pvh_start_info.flags;
102 
103 	msr = cpuid_ebx(xen_cpuid_base() + 2);
104 	pfn = __pa(hypercall_page);
105 	wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
106 
107 	init_pvh_bootparams();
108 }
109