xref: /openbmc/linux/arch/x86/kernel/jailhouse.c (revision cfbb9be8)
1 // SPDX-License-Identifier: GPL2.0
2 /*
3  * Jailhouse paravirt_ops implementation
4  *
5  * Copyright (c) Siemens AG, 2015-2017
6  *
7  * Authors:
8  *  Jan Kiszka <jan.kiszka@siemens.com>
9  */
10 
11 #include <linux/acpi_pmtmr.h>
12 #include <linux/kernel.h>
13 #include <linux/reboot.h>
14 #include <asm/apic.h>
15 #include <asm/cpu.h>
16 #include <asm/hypervisor.h>
17 #include <asm/i8259.h>
18 #include <asm/irqdomain.h>
19 #include <asm/pci_x86.h>
20 #include <asm/reboot.h>
21 #include <asm/setup.h>
22 
23 static __initdata struct jailhouse_setup_data setup_data;
24 static unsigned int precalibrated_tsc_khz;
25 
26 static uint32_t jailhouse_cpuid_base(void)
27 {
28 	if (boot_cpu_data.cpuid_level < 0 ||
29 	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
30 		return 0;
31 
32 	return hypervisor_cpuid_base("Jailhouse\0\0\0", 0);
33 }
34 
35 static uint32_t __init jailhouse_detect(void)
36 {
37 	return jailhouse_cpuid_base();
38 }
39 
40 static void jailhouse_get_wallclock(struct timespec *now)
41 {
42 	memset(now, 0, sizeof(*now));
43 }
44 
45 static void __init jailhouse_timer_init(void)
46 {
47 	lapic_timer_frequency = setup_data.apic_khz * (1000 / HZ);
48 }
49 
50 static unsigned long jailhouse_get_tsc(void)
51 {
52 	return precalibrated_tsc_khz;
53 }
54 
55 static void __init jailhouse_x2apic_init(void)
56 {
57 #ifdef CONFIG_X86_X2APIC
58 	if (!x2apic_enabled())
59 		return;
60 	/*
61 	 * We do not have access to IR inside Jailhouse non-root cells.  So
62 	 * we have to run in physical mode.
63 	 */
64 	x2apic_phys = 1;
65 	/*
66 	 * This will trigger the switch to apic_x2apic_phys.  Empty OEM IDs
67 	 * ensure that only this APIC driver picks up the call.
68 	 */
69 	default_acpi_madt_oem_check("", "");
70 #endif
71 }
72 
73 static void __init jailhouse_get_smp_config(unsigned int early)
74 {
75 	struct ioapic_domain_cfg ioapic_cfg = {
76 		.type = IOAPIC_DOMAIN_STRICT,
77 		.ops = &mp_ioapic_irqdomain_ops,
78 	};
79 	struct mpc_intsrc mp_irq = {
80 		.type = MP_INTSRC,
81 		.irqtype = mp_INT,
82 		.irqflag = MP_IRQPOL_ACTIVE_HIGH | MP_IRQTRIG_EDGE,
83 	};
84 	unsigned int cpu;
85 
86 	jailhouse_x2apic_init();
87 
88 	register_lapic_address(0xfee00000);
89 
90 	for (cpu = 0; cpu < setup_data.num_cpus; cpu++) {
91 		generic_processor_info(setup_data.cpu_ids[cpu],
92 				       boot_cpu_apic_version);
93 	}
94 
95 	smp_found_config = 1;
96 
97 	if (setup_data.standard_ioapic) {
98 		mp_register_ioapic(0, 0xfec00000, gsi_top, &ioapic_cfg);
99 
100 		/* Register 1:1 mapping for legacy UART IRQs 3 and 4 */
101 		mp_irq.srcbusirq = mp_irq.dstirq = 3;
102 		mp_save_irq(&mp_irq);
103 
104 		mp_irq.srcbusirq = mp_irq.dstirq = 4;
105 		mp_save_irq(&mp_irq);
106 	}
107 }
108 
109 static void jailhouse_no_restart(void)
110 {
111 	pr_notice("Jailhouse: Restart not supported, halting\n");
112 	machine_halt();
113 }
114 
115 static int __init jailhouse_pci_arch_init(void)
116 {
117 	pci_direct_init(1);
118 
119 	/*
120 	 * There are no bridges on the virtual PCI root bus under Jailhouse,
121 	 * thus no other way to discover all devices than a full scan.
122 	 * Respect any overrides via the command line, though.
123 	 */
124 	if (pcibios_last_bus < 0)
125 		pcibios_last_bus = 0xff;
126 
127 	return 0;
128 }
129 
130 static void __init jailhouse_init_platform(void)
131 {
132 	u64 pa_data = boot_params.hdr.setup_data;
133 	struct setup_data header;
134 	void *mapping;
135 
136 	x86_init.irqs.pre_vector_init	= x86_init_noop;
137 	x86_init.timers.timer_init	= jailhouse_timer_init;
138 	x86_init.mpparse.get_smp_config	= jailhouse_get_smp_config;
139 	x86_init.pci.arch_init		= jailhouse_pci_arch_init;
140 
141 	x86_platform.calibrate_cpu	= jailhouse_get_tsc;
142 	x86_platform.calibrate_tsc	= jailhouse_get_tsc;
143 	x86_platform.get_wallclock	= jailhouse_get_wallclock;
144 	x86_platform.legacy.rtc		= 0;
145 	x86_platform.legacy.warm_reset	= 0;
146 	x86_platform.legacy.i8042	= X86_LEGACY_I8042_PLATFORM_ABSENT;
147 
148 	legacy_pic			= &null_legacy_pic;
149 
150 	machine_ops.emergency_restart	= jailhouse_no_restart;
151 
152 	while (pa_data) {
153 		mapping = early_memremap(pa_data, sizeof(header));
154 		memcpy(&header, mapping, sizeof(header));
155 		early_memunmap(mapping, sizeof(header));
156 
157 		if (header.type == SETUP_JAILHOUSE &&
158 		    header.len >= sizeof(setup_data)) {
159 			pa_data += offsetof(struct setup_data, data);
160 
161 			mapping = early_memremap(pa_data, sizeof(setup_data));
162 			memcpy(&setup_data, mapping, sizeof(setup_data));
163 			early_memunmap(mapping, sizeof(setup_data));
164 
165 			break;
166 		}
167 
168 		pa_data = header.next;
169 	}
170 
171 	if (!pa_data)
172 		panic("Jailhouse: No valid setup data found");
173 
174 	if (setup_data.compatible_version > JAILHOUSE_SETUP_REQUIRED_VERSION)
175 		panic("Jailhouse: Unsupported setup data structure");
176 
177 	pmtmr_ioport = setup_data.pm_timer_address;
178 	pr_debug("Jailhouse: PM-Timer IO Port: %#x\n", pmtmr_ioport);
179 
180 	precalibrated_tsc_khz = setup_data.tsc_khz;
181 	setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
182 
183 	pci_probe = 0;
184 
185 	/*
186 	 * Avoid that the kernel complains about missing ACPI tables - there
187 	 * are none in a non-root cell.
188 	 */
189 	disable_acpi();
190 }
191 
192 bool jailhouse_paravirt(void)
193 {
194 	return jailhouse_cpuid_base() != 0;
195 }
196 
197 static bool jailhouse_x2apic_available(void)
198 {
199 	/*
200 	 * The x2APIC is only available if the root cell enabled it. Jailhouse
201 	 * does not support switching between xAPIC and x2APIC.
202 	 */
203 	return x2apic_enabled();
204 }
205 
206 const struct hypervisor_x86 x86_hyper_jailhouse __refconst = {
207 	.name			= "Jailhouse",
208 	.detect			= jailhouse_detect,
209 	.init.init_platform	= jailhouse_init_platform,
210 	.init.x2apic_available	= jailhouse_x2apic_available,
211 };
212