xref: /openbmc/linux/arch/x86/xen/enlighten_pvh.c (revision e6d49dd4)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 #include <linux/export.h>
4 #include <linux/mm.h>
5 
6 #include <xen/hvc-console.h>
7 
8 #include <asm/io_apic.h>
9 #include <asm/hypervisor.h>
10 #include <asm/e820/api.h>
11 
12 #include <xen/xen.h>
13 #include <asm/xen/interface.h>
14 #include <asm/xen/hypercall.h>
15 
16 #include <xen/interface/memory.h>
17 
18 #include "xen-ops.h"
19 
20 /*
21  * PVH variables.
22  *
23  * The variable xen_pvh needs to live in a data segment since it is used
24  * after startup_{32|64} is invoked, which will clear the .bss segment.
25  */
26 bool __ro_after_init xen_pvh;
27 EXPORT_SYMBOL_GPL(xen_pvh);
28 
29 void __init xen_pvh_init(struct boot_params *boot_params)
30 {
31 	u32 msr;
32 	u64 pfn;
33 
34 	xen_pvh = 1;
35 	xen_domain_type = XEN_HVM_DOMAIN;
36 	xen_start_flags = pvh_start_info.flags;
37 
38 	msr = cpuid_ebx(xen_cpuid_base() + 2);
39 	pfn = __pa(hypercall_page);
40 	wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
41 
42 	if (xen_initial_domain())
43 		x86_init.oem.arch_setup = xen_add_preferred_consoles;
44 	x86_init.oem.banner = xen_banner;
45 
46 	xen_efi_init(boot_params);
47 
48 	if (xen_initial_domain()) {
49 		struct xen_platform_op op = {
50 			.cmd = XENPF_get_dom0_console,
51 		};
52 		int ret = HYPERVISOR_platform_op(&op);
53 
54 		if (ret > 0)
55 			xen_init_vga(&op.u.dom0_console,
56 				     min(ret * sizeof(char),
57 					 sizeof(op.u.dom0_console)),
58 				     &boot_params->screen_info);
59 	}
60 }
61 
62 void __init mem_map_via_hcall(struct boot_params *boot_params_p)
63 {
64 	struct xen_memory_map memmap;
65 	int rc;
66 
67 	memmap.nr_entries = ARRAY_SIZE(boot_params_p->e820_table);
68 	set_xen_guest_handle(memmap.buffer, boot_params_p->e820_table);
69 	rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
70 	if (rc) {
71 		xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc);
72 		BUG();
73 	}
74 	boot_params_p->e820_entries = memmap.nr_entries;
75 }
76 
77 /*
78  * Reserve e820 UNUSABLE regions to inflate the memory balloon.
79  *
80  * On PVH dom0 the host memory map is used, RAM regions available to dom0 are
81  * located as the same place as in the native memory map, but since dom0 gets
82  * less memory than the total amount of host RAM the ranges that can't be
83  * populated are converted from RAM -> UNUSABLE.  Use such regions (up to the
84  * ratio signaled in EXTRA_MEM_RATIO) in order to inflate the balloon driver at
85  * boot.  Doing so prevents the guest (even if just temporary) from using holes
86  * in the memory map in order to map grants or foreign addresses, and
87  * hopefully limits the risk of a clash with a device MMIO region.  Ideally the
88  * hypervisor should notify us which memory ranges are suitable for creating
89  * foreign mappings, but that's not yet implemented.
90  */
91 void __init xen_reserve_extra_memory(struct boot_params *bootp)
92 {
93 	unsigned int i, ram_pages = 0, extra_pages;
94 
95 	for (i = 0; i < bootp->e820_entries; i++) {
96 		struct boot_e820_entry *e = &bootp->e820_table[i];
97 
98 		if (e->type != E820_TYPE_RAM)
99 			continue;
100 		ram_pages += PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr);
101 	}
102 
103 	/* Max amount of extra memory. */
104 	extra_pages = EXTRA_MEM_RATIO * ram_pages;
105 
106 	/*
107 	 * Convert UNUSABLE ranges to RAM and reserve them for foreign mapping
108 	 * purposes.
109 	 */
110 	for (i = 0; i < bootp->e820_entries && extra_pages; i++) {
111 		struct boot_e820_entry *e = &bootp->e820_table[i];
112 		unsigned long pages;
113 
114 		if (e->type != E820_TYPE_UNUSABLE)
115 			continue;
116 
117 		pages = min(extra_pages,
118 			PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr));
119 
120 		if (pages != (PFN_DOWN(e->addr + e->size) - PFN_UP(e->addr))) {
121 			struct boot_e820_entry *next;
122 
123 			if (bootp->e820_entries ==
124 			    ARRAY_SIZE(bootp->e820_table))
125 				/* No space left to split - skip region. */
126 				continue;
127 
128 			/* Split entry. */
129 			next = e + 1;
130 			memmove(next, e,
131 				(bootp->e820_entries - i) * sizeof(*e));
132 			bootp->e820_entries++;
133 			next->addr = PAGE_ALIGN(e->addr) + PFN_PHYS(pages);
134 			e->size = next->addr - e->addr;
135 			next->size -= e->size;
136 		}
137 		e->type = E820_TYPE_RAM;
138 		extra_pages -= pages;
139 
140 		xen_add_extra_mem(PFN_UP(e->addr), pages);
141 	}
142 }
143