Merge tag 'chrome-platform-for-linus-4.13' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / arch / x86 / xen / enlighten_hvm.c
blob87d791356ea9052f79f23e00f5d0024ff8cf0b7f
1 #include <linux/cpu.h>
2 #include <linux/kexec.h>
3 #include <linux/memblock.h>
5 #include <xen/features.h>
6 #include <xen/events.h>
7 #include <xen/interface/memory.h>
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>
16 #include <asm/xen/cpuid.h>
17 #include <asm/xen/hypervisor.h>
18 #include <asm/xen/page.h>
20 #include "xen-ops.h"
21 #include "mmu.h"
22 #include "smp.h"
24 void __ref xen_hvm_init_shared_info(void)
26 struct xen_add_to_physmap xatp;
27 u64 pa;
29 if (HYPERVISOR_shared_info == &xen_dummy_shared_info) {
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.
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)
44 memblock_reserve(pa, PAGE_SIZE);
45 HYPERVISOR_shared_info = __va(pa);
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();
56 static void __init init_hvm_pv_info(void)
58 int major, minor;
59 uint32_t eax, ebx, ecx, edx, base;
61 base = xen_cpuid_base();
62 eax = cpuid_eax(base + 1);
64 major = eax >> 16;
65 minor = eax & 0xffff;
66 printk(KERN_INFO "Xen version %d.%d.\n", major, minor);
68 xen_domain_type = XEN_HVM_DOMAIN;
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;
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));
83 xen_setup_features();
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());
92 #ifdef CONFIG_KEXEC_CORE
93 static void xen_hvm_shutdown(void)
95 native_machine_shutdown();
96 if (kexec_in_progress)
97 xen_reboot(SHUTDOWN_soft_reset);
100 static void xen_hvm_crash_shutdown(struct pt_regs *regs)
102 native_machine_crash_shutdown(regs);
103 xen_reboot(SHUTDOWN_soft_reset);
105 #endif
107 static int xen_cpu_up_prepare_hvm(unsigned int cpu)
109 int rc = 0;
112 * This can happen if CPU was offlined earlier and
113 * offlining timed out in common_cpu_die().
115 if (cpu_report_state(cpu) == CPU_DEAD_FROZEN) {
116 xen_smp_intr_free(cpu);
117 xen_uninit_lock_cpu(cpu);
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;
128 if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
129 xen_setup_timer(cpu);
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);
136 return rc;
139 static int xen_cpu_dead_hvm(unsigned int cpu)
141 xen_smp_intr_free(cpu);
143 if (xen_have_vector_callback && xen_feature(XENFEAT_hvm_safe_pvclock))
144 xen_teardown_timer(cpu);
146 return 0;
149 static void __init xen_hvm_guest_init(void)
151 if (xen_pv_domain())
152 return;
154 init_hvm_pv_info();
156 xen_hvm_init_shared_info();
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.
163 xen_vcpu_info_reset(0);
165 xen_panic_handler_init();
167 if (xen_feature(XENFEAT_hvm_callback_vector))
168 xen_have_vector_callback = 1;
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();
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
185 static bool xen_nopv;
186 static __init int xen_parse_nopv(char *arg)
188 xen_nopv = true;
189 return 0;
191 early_param("xen_nopv", xen_parse_nopv);
193 bool xen_hvm_need_lapic(void)
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;
205 EXPORT_SYMBOL_GPL(xen_hvm_need_lapic);
207 static uint32_t __init xen_platform_hvm(void)
209 if (xen_pv_domain() || xen_nopv)
210 return 0;
212 return xen_cpuid_base();
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,
222 EXPORT_SYMBOL(x86_hyper_xen_hvm);