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