GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / x86 / xen / enlighten_hvm.c
1 #include <linux/cpu.h>
2 #include <linux/kexec.h>
3 #include <linux/memblock.h>
4
5 #include <xen/features.h>
6 #include <xen/events.h>
7 #include <xen/interface/memory.h>
8
9 #include <asm/cpu.h>
10 #include <asm/smp.h>
11 #include <asm/reboot.h>
12 #include <asm/setup.h>
13 #include <asm/hypervisor.h>
14 #include <asm/e820/api.h>
15 #include <asm/early_ioremap.h>
16
17 #include <asm/xen/cpuid.h>
18 #include <asm/xen/hypervisor.h>
19 #include <asm/xen/page.h>
20
21 #include "xen-ops.h"
22 #include "mmu.h"
23 #include "smp.h"
24
25 static unsigned long shared_info_pfn;
26
27 void xen_hvm_init_shared_info(void)
28 {
29         struct xen_add_to_physmap xatp;
30
31         xatp.domid = DOMID_SELF;
32         xatp.idx = 0;
33         xatp.space = XENMAPSPACE_shared_info;
34         xatp.gpfn = shared_info_pfn;
35         if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
36                 BUG();
37 }
38
39 static void __init reserve_shared_info(void)
40 {
41         u64 pa;
42
43         /*
44          * Search for a free page starting at 4kB physical address.
45          * Low memory is preferred to avoid an EPT large page split up
46          * by the mapping.
47          * Starting below X86_RESERVE_LOW (usually 64kB) is fine as
48          * the BIOS used for HVM guests is well behaved and won't
49          * clobber memory other than the first 4kB.
50          */
51         for (pa = PAGE_SIZE;
52              !e820__mapped_all(pa, pa + PAGE_SIZE, E820_TYPE_RAM) ||
53              memblock_is_reserved(pa);
54              pa += PAGE_SIZE)
55                 ;
56
57         shared_info_pfn = PHYS_PFN(pa);
58
59         memblock_reserve(pa, PAGE_SIZE);
60         HYPERVISOR_shared_info = early_memremap(pa, PAGE_SIZE);
61 }
62
63 static void __init xen_hvm_init_mem_mapping(void)
64 {
65         early_memunmap(HYPERVISOR_shared_info, PAGE_SIZE);
66         HYPERVISOR_shared_info = __va(PFN_PHYS(shared_info_pfn));
67
68         /*
69          * The virtual address of the shared_info page has changed, so
70          * the vcpu_info pointer for VCPU 0 is now stale.
71          *
72          * The prepare_boot_cpu callback will re-initialize it via
73          * xen_vcpu_setup, but we can't rely on that to be called for
74          * old Xen versions (xen_have_vector_callback == 0).
75          *
76          * It is, in any case, bad to have a stale vcpu_info pointer
77          * so reset it now.
78          */
79         xen_vcpu_info_reset(0);
80 }
81
82 static void __init init_hvm_pv_info(void)
83 {
84         int major, minor;
85         uint32_t eax, ebx, ecx, edx, base;
86
87         base = xen_cpuid_base();
88         eax = cpuid_eax(base + 1);
89
90         major = eax >> 16;
91         minor = eax & 0xffff;
92         printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
93
94         xen_domain_type = XEN_HVM_DOMAIN;
95
96         /* PVH set up hypercall page in xen_prepare_pvh(). */
97         if (xen_pvh_domain())
98                 pv_info.name = "Xen PVH";
99         else {
100                 u64 pfn;
101                 uint32_t msr;
102
103                 pv_info.name = "Xen HVM";
104                 msr = cpuid_ebx(base + 2);
105                 pfn = __pa(hypercall_page);
106                 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
107         }
108
109         xen_setup_features();
110
111         cpuid(base + 4, &eax, &ebx, &ecx, &edx);
112         if (eax & XEN_HVM_CPUID_VCPU_ID_PRESENT)
113                 this_cpu_write(xen_vcpu_id, ebx);
114         else
115                 this_cpu_write(xen_vcpu_id, smp_processor_id());
116 }
117
118 #ifdef CONFIG_KEXEC_CORE
119 static void xen_hvm_shutdown(void)
120 {
121         native_machine_shutdown();
122         if (kexec_in_progress)
123                 xen_reboot(SHUTDOWN_soft_reset);
124 }
125
126 static void xen_hvm_crash_shutdown(struct pt_regs *regs)
127 {
128         native_machine_crash_shutdown(regs);
129         xen_reboot(SHUTDOWN_soft_reset);
130 }
131 #endif
132
133 static int xen_cpu_up_prepare_hvm(unsigned int cpu)
134 {
135         int rc = 0;
136
137         /*
138          * This can happen if CPU was offlined earlier and
139          * offlining timed out in common_cpu_die().
140          */
141         if (cpu_report_state(cpu) == CPU_DEAD_FROZEN) {
142                 xen_smp_intr_free(cpu);
143                 xen_uninit_lock_cpu(cpu);
144         }
145
146         if (cpu_acpi_id(cpu) != U32_MAX)
147                 per_cpu(xen_vcpu_id, cpu) = cpu_acpi_id(cpu);
148         else
149                 per_cpu(xen_vcpu_id, cpu) = cpu;
150         rc = xen_vcpu_setup(cpu);
151         if (rc)
152                 return rc;
153
154         if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
155                 xen_setup_timer(cpu);
156
157         rc = xen_smp_intr_init(cpu);
158         if (rc) {
159                 WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
160                      cpu, rc);
161         }
162         return rc;
163 }
164
165 static int xen_cpu_dead_hvm(unsigned int cpu)
166 {
167         xen_smp_intr_free(cpu);
168
169         if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
170                 xen_teardown_timer(cpu);
171
172        return 0;
173 }
174
175 static void __init xen_hvm_guest_init(void)
176 {
177         if (xen_pv_domain())
178                 return;
179
180         init_hvm_pv_info();
181
182         reserve_shared_info();
183         xen_hvm_init_shared_info();
184
185         /*
186          * xen_vcpu is a pointer to the vcpu_info struct in the shared_info
187          * page, we use it in the event channel upcall and in some pvclock
188          * related functions.
189          */
190         xen_vcpu_info_reset(0);
191
192         xen_panic_handler_init();
193
194         if (xen_feature(XENFEAT_hvm_callback_vector))
195                 xen_have_vector_callback = 1;
196
197         xen_hvm_smp_init();
198         WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm, xen_cpu_dead_hvm));
199         xen_unplug_emulated_devices();
200         x86_init.irqs.intr_init = xen_init_IRQ;
201         xen_hvm_init_time_ops();
202         xen_hvm_init_mmu_ops();
203
204         if (xen_pvh_domain())
205                 machine_ops.emergency_restart = xen_emergency_restart;
206 #ifdef CONFIG_KEXEC_CORE
207         machine_ops.shutdown = xen_hvm_shutdown;
208         machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
209 #endif
210 }
211
212 static bool xen_nopv;
213 static __init int xen_parse_nopv(char *arg)
214 {
215        xen_nopv = true;
216        return 0;
217 }
218 early_param("xen_nopv", xen_parse_nopv);
219
220 bool xen_hvm_need_lapic(void)
221 {
222         if (xen_nopv)
223                 return false;
224         if (xen_pv_domain())
225                 return false;
226         if (!xen_hvm_domain())
227                 return false;
228         if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
229                 return false;
230         return true;
231 }
232 EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
233
234 static uint32_t __init xen_platform_hvm(void)
235 {
236         if (xen_pv_domain() || xen_nopv)
237                 return 0;
238
239         return xen_cpuid_base();
240 }
241
242 const __initconst struct hypervisor_x86 x86_hyper_xen_hvm = {
243         .name                   = "Xen HVM",
244         .detect                 = xen_platform_hvm,
245         .type                   = X86_HYPER_XEN_HVM,
246         .init.init_platform     = xen_hvm_guest_init,
247         .init.x2apic_available  = xen_x2apic_para_available,
248         .init.init_mem_mapping  = xen_hvm_init_mem_mapping,
249         .runtime.pin_vcpu       = xen_pin_vcpu,
250 };