regulator: core: fix a possible race in disable_work handling
[sfrench/cifs-2.6.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
16 #include <asm/xen/cpuid.h>
17 #include <asm/xen/hypervisor.h>
18 #include <asm/xen/page.h>
19
20 #include "xen-ops.h"
21 #include "mmu.h"
22 #include "smp.h"
23
24 void __ref xen_hvm_init_shared_info(void)
25 {
26         struct xen_add_to_physmap xatp;
27         u64 pa;
28
29         if (HYPERVISOR_shared_info == &xen_dummy_shared_info) {
30                 /*
31                  * Search for a free page starting at 4kB physical address.
32                  * Low memory is preferred to avoid an EPT large page split up
33                  * by the mapping.
34                  * Starting below X86_RESERVE_LOW (usually 64kB) is fine as
35                  * the BIOS used for HVM guests is well behaved and won't
36                  * clobber memory other than the first 4kB.
37                  */
38                 for (pa = PAGE_SIZE;
39                      !e820__mapped_all(pa, pa + PAGE_SIZE, E820_TYPE_RAM) ||
40                      memblock_is_reserved(pa);
41                      pa += PAGE_SIZE)
42                         ;
43
44                 memblock_reserve(pa, PAGE_SIZE);
45                 HYPERVISOR_shared_info = __va(pa);
46         }
47
48         xatp.domid = DOMID_SELF;
49         xatp.idx = 0;
50         xatp.space = XENMAPSPACE_shared_info;
51         xatp.gpfn = virt_to_pfn(HYPERVISOR_shared_info);
52         if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
53                 BUG();
54 }
55
56 static void __init init_hvm_pv_info(void)
57 {
58         int major, minor;
59         uint32_t eax, ebx, ecx, edx, base;
60
61         base = xen_cpuid_base();
62         eax = cpuid_eax(base + 1);
63
64         major = eax >> 16;
65         minor = eax & 0xffff;
66         printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
67
68         xen_domain_type = XEN_HVM_DOMAIN;
69
70         /* PVH set up hypercall page in xen_prepare_pvh(). */
71         if (xen_pvh_domain())
72                 pv_info.name = "Xen PVH";
73         else {
74                 u64 pfn;
75                 uint32_t msr;
76
77                 pv_info.name = "Xen HVM";
78                 msr = cpuid_ebx(base + 2);
79                 pfn = __pa(hypercall_page);
80                 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
81         }
82
83         xen_setup_features();
84
85         cpuid(base + 4, &eax, &ebx, &ecx, &edx);
86         if (eax & XEN_HVM_CPUID_VCPU_ID_PRESENT)
87                 this_cpu_write(xen_vcpu_id, ebx);
88         else
89                 this_cpu_write(xen_vcpu_id, smp_processor_id());
90 }
91
92 #ifdef CONFIG_KEXEC_CORE
93 static void xen_hvm_shutdown(void)
94 {
95         native_machine_shutdown();
96         if (kexec_in_progress)
97                 xen_reboot(SHUTDOWN_soft_reset);
98 }
99
100 static void xen_hvm_crash_shutdown(struct pt_regs *regs)
101 {
102         native_machine_crash_shutdown(regs);
103         xen_reboot(SHUTDOWN_soft_reset);
104 }
105 #endif
106
107 static int xen_cpu_up_prepare_hvm(unsigned int cpu)
108 {
109         int rc = 0;
110
111         /*
112          * This can happen if CPU was offlined earlier and
113          * offlining timed out in common_cpu_die().
114          */
115         if (cpu_report_state(cpu) == CPU_DEAD_FROZEN) {
116                 xen_smp_intr_free(cpu);
117                 xen_uninit_lock_cpu(cpu);
118         }
119
120         if (cpu_acpi_id(cpu) != U32_MAX)
121                 per_cpu(xen_vcpu_id, cpu) = cpu_acpi_id(cpu);
122         else
123                 per_cpu(xen_vcpu_id, cpu) = cpu;
124         rc = xen_vcpu_setup(cpu);
125         if (rc)
126                 return rc;
127
128         if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
129                 xen_setup_timer(cpu);
130
131         rc = xen_smp_intr_init(cpu);
132         if (rc) {
133                 WARN(1, "xen_smp_intr_init() for CPU %d failed: %d\n",
134                      cpu, rc);
135         }
136         return rc;
137 }
138
139 static int xen_cpu_dead_hvm(unsigned int cpu)
140 {
141         xen_smp_intr_free(cpu);
142
143         if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
144                 xen_teardown_timer(cpu);
145
146        return 0;
147 }
148
149 static void __init xen_hvm_guest_init(void)
150 {
151         if (xen_pv_domain())
152                 return;
153
154         init_hvm_pv_info();
155
156         xen_hvm_init_shared_info();
157
158         /*
159          * xen_vcpu is a pointer to the vcpu_info struct in the shared_info
160          * page, we use it in the event channel upcall and in some pvclock
161          * related functions.
162          */
163         xen_vcpu_info_reset(0);
164
165         xen_panic_handler_init();
166
167         if (xen_feature(XENFEAT_hvm_callback_vector))
168                 xen_have_vector_callback = 1;
169
170         xen_hvm_smp_init();
171         WARN_ON(xen_cpuhp_setup(xen_cpu_up_prepare_hvm, xen_cpu_dead_hvm));
172         xen_unplug_emulated_devices();
173         x86_init.irqs.intr_init = xen_init_IRQ;
174         xen_hvm_init_time_ops();
175         xen_hvm_init_mmu_ops();
176
177         if (xen_pvh_domain())
178                 machine_ops.emergency_restart = xen_emergency_restart;
179 #ifdef CONFIG_KEXEC_CORE
180         machine_ops.shutdown = xen_hvm_shutdown;
181         machine_ops.crash_shutdown = xen_hvm_crash_shutdown;
182 #endif
183 }
184
185 static bool xen_nopv;
186 static __init int xen_parse_nopv(char *arg)
187 {
188        xen_nopv = true;
189        return 0;
190 }
191 early_param("xen_nopv", xen_parse_nopv);
192
193 bool xen_hvm_need_lapic(void)
194 {
195         if (xen_nopv)
196                 return false;
197         if (xen_pv_domain())
198                 return false;
199         if (!xen_hvm_domain())
200                 return false;
201         if (xen_feature(XENFEAT_hvm_pirqs) && xen_have_vector_callback)
202                 return false;
203         return true;
204 }
205 EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
206
207 static uint32_t __init xen_platform_hvm(void)
208 {
209         if (xen_pv_domain() || xen_nopv)
210                 return 0;
211
212         return xen_cpuid_base();
213 }
214
215 const struct hypervisor_x86 x86_hyper_xen_hvm = {
216         .name                   = "Xen HVM",
217         .detect                 = xen_platform_hvm,
218         .init_platform          = xen_hvm_guest_init,
219         .pin_vcpu               = xen_pin_vcpu,
220         .x2apic_available       = xen_x2apic_para_available,
221 };
222 EXPORT_SYMBOL(x86_hyper_xen_hvm);