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