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