blob: 078c512b8720995fb28003b5afa52376fc674e0f [file] [log] [blame]
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +01001#include <linux/cpu.h>
2#include <linux/kexec.h>
3
4#include <xen/features.h>
5#include <xen/events.h>
6#include <xen/interface/memory.h>
7
8#include <asm/cpu.h>
9#include <asm/smp.h>
10#include <asm/reboot.h>
11#include <asm/setup.h>
12#include <asm/hypervisor.h>
13
14#include <asm/xen/cpuid.h>
15#include <asm/xen/hypervisor.h>
16
17#include "xen-ops.h"
18#include "mmu.h"
19#include "smp.h"
20
Boris Ostrovsky84d582d2017-04-24 15:04:53 -040021__read_mostly int xen_have_vector_callback;
22EXPORT_SYMBOL_GPL(xen_have_vector_callback);
23
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +010024void __ref xen_hvm_init_shared_info(void)
25{
26 int cpu;
27 struct xen_add_to_physmap xatp;
28 static struct shared_info *shared_info_page;
29
30 if (!shared_info_page)
31 shared_info_page = (struct shared_info *)
32 extend_brk(PAGE_SIZE, PAGE_SIZE);
33 xatp.domid = DOMID_SELF;
34 xatp.idx = 0;
35 xatp.space = XENMAPSPACE_shared_info;
36 xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
37 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
38 BUG();
39
40 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
41
42 /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
43 * page, we use it in the event channel upcall and in some pvclock
44 * related functions. We don't need the vcpu_info placement
45 * optimizations because we don't use any pv_mmu or pv_irq op on
46 * HVM.
47 * When xen_hvm_init_shared_info is run at boot time only vcpu 0 is
48 * online but xen_hvm_init_shared_info is run at resume time too and
49 * in that case multiple vcpus might be online. */
50 for_each_online_cpu(cpu) {
51 /* Leave it to be NULL. */
52 if (xen_vcpu_nr(cpu) >= MAX_VIRT_CPUS)
53 continue;
54 per_cpu(xen_vcpu, cpu) =
55 &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
56 }
57}
58
59static void __init init_hvm_pv_info(void)
60{
61 int major, minor;
62 uint32_t eax, ebx, ecx, edx, base;
63
64 base = xen_cpuid_base();
65 eax = cpuid_eax(base + 1);
66
67 major = eax >> 16;
68 minor = eax & 0xffff;
69 printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
70
71 xen_domain_type = XEN_HVM_DOMAIN;
72
73 /* PVH set up hypercall page in xen_prepare_pvh(). */
74 if (xen_pvh_domain())
75 pv_info.name = "Xen PVH";
76 else {
77 u64 pfn;
78 uint32_t msr;
79
80 pv_info.name = "Xen HVM";
81 msr = cpuid_ebx(base + 2);
82 pfn = __pa(hypercall_page);
83 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
84 }
85
86 xen_setup_features();
87
88 cpuid(base + 4, &eax, &ebx, &ecx, &edx);
89 if (eax & XEN_HVM_CPUID_VCPU_ID_PRESENT)
90 this_cpu_write(xen_vcpu_id, ebx);
91 else
92 this_cpu_write(xen_vcpu_id, smp_processor_id());
93}
94
95#ifdef CONFIG_KEXEC_CORE
96static void xen_hvm_shutdown(void)
97{
98 native_machine_shutdown();
99 if (kexec_in_progress)
100 xen_reboot(SHUTDOWN_soft_reset);
101}
102
103static void xen_hvm_crash_shutdown(struct pt_regs *regs)
104{
105 native_machine_crash_shutdown(regs);
106 xen_reboot(SHUTDOWN_soft_reset);
107}
108#endif
109
110static int xen_cpu_up_prepare_hvm(unsigned int cpu)
111{
112 int rc;
113
114 /*
115 * This can happen if CPU was offlined earlier and
116 * offlining timed out in common_cpu_die().
117 */
118 if (cpu_report_state(cpu) == CPU_DEAD_FROZEN) {
119 xen_smp_intr_free(cpu);
120 xen_uninit_lock_cpu(cpu);
121 }
122
123 if (cpu_acpi_id(cpu) != U32_MAX)
124 per_cpu(xen_vcpu_id, cpu) = cpu_acpi_id(cpu);
125 else
126 per_cpu(xen_vcpu_id, cpu) = cpu;
127 xen_vcpu_setup(cpu);
128
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400129 if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100130 xen_setup_timer(cpu);
131
132 rc = xen_smp_intr_init(cpu);
133 if (rc) {
134 WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
135 cpu, rc);
136 return rc;
137 }
138 return 0;
139}
140
141static int xen_cpu_dead_hvm(unsigned int cpu)
142{
143 xen_smp_intr_free(cpu);
144
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400145 if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100146 xen_teardown_timer(cpu);
147
148 return 0;
149}
150
151static void __init xen_hvm_guest_init(void)
152{
153 if (xen_pv_domain())
154 return;
155
156 init_hvm_pv_info();
157
158 xen_hvm_init_shared_info();
159
160 xen_panic_handler_init();
161
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400162 if (xen_feature(XENFEAT_hvm_callback_vector))
163 xen_have_vector_callback = 1;
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100164
165 xen_hvm_smp_init();
166 WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm, xen_cpu_dead_hvm));
167 xen_unplug_emulated_devices();
168 x86_init.irqs.intr_init = xen_init_IRQ;
169 xen_hvm_init_time_ops();
170 xen_hvm_init_mmu_ops();
171
172 if (xen_pvh_domain())
173 machine_ops.emergency_restart = xen_emergency_restart;
174#ifdef CONFIG_KEXEC_CORE
175 machine_ops.shutdown = xen_hvm_shutdown;
176 machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
177#endif
178}
179
180static bool xen_nopv;
181static __init int xen_parse_nopv(char *arg)
182{
183 xen_nopv = true;
184 return 0;
185}
186early_param("xen_nopv", xen_parse_nopv);
187
188bool xen_hvm_need_lapic(void)
189{
190 if (xen_nopv)
191 return false;
192 if (xen_pv_domain())
193 return false;
194 if (!xen_hvm_domain())
195 return false;
Boris Ostrovsky84d582d2017-04-24 15:04:53 -0400196 if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
Vitaly Kuznetsov98f2a472017-03-14 18:35:40 +0100197 return false;
198 return true;
199}
200EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
201
202static uint32_t __init xen_platform_hvm(void)
203{
204 if (xen_pv_domain() || xen_nopv)
205 return 0;
206
207 return xen_cpuid_base();
208}
209
210const struct hypervisor_x86 x86_hyper_xen_hvm = {
211 .name = "Xen HVM",
212 .detect = xen_platform_hvm,
213 .init_platform = xen_hvm_guest_init,
214 .pin_vcpu = xen_pin_vcpu,
215 .x2apic_available = xen_x2apic_para_available,
216};
217EXPORT_SYMBOL(x86_hyper_xen_hvm);