1 #include <linux/types.h>
2 #include <linux/tick.h>
3 #include <linux/percpu-defs.h>
4
5 #include <xen/xen.h>
6 #include <xen/interface/xen.h>
7 #include <xen/grant_table.h>
8 #include <xen/events.h>
9
10 #include <asm/cpufeatures.h>
11 #include <asm/msr-index.h>
12 #include <asm/xen/hypercall.h>
13 #include <asm/xen/page.h>
14 #include <asm/fixmap.h>
15
16 #include "xen-ops.h"
17 #include "mmu.h"
18 #include "pmu.h"
19
xen_pv_pre_suspend(void)20 static void xen_pv_pre_suspend(void)
21 {
22 xen_mm_pin_all();
23
24 xen_start_info->store_mfn = mfn_to_pfn(xen_start_info->store_mfn);
25 xen_start_info->console.domU.mfn =
26 mfn_to_pfn(xen_start_info->console.domU.mfn);
27
28 BUG_ON(!irqs_disabled());
29
30 HYPERVISOR_shared_info = &xen_dummy_shared_info;
31 if (HYPERVISOR_update_va_mapping(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
32 __pte_ma(0), 0))
33 BUG();
34 }
35
xen_hvm_post_suspend(int suspend_cancelled)36 static void xen_hvm_post_suspend(int suspend_cancelled)
37 {
38 #ifdef CONFIG_XEN_PVHVM
39 int cpu;
40 if (!suspend_cancelled)
41 xen_hvm_init_shared_info();
42 xen_callback_vector();
43 xen_unplug_emulated_devices();
44 if (xen_feature(XENFEAT_hvm_safe_pvclock)) {
45 for_each_online_cpu(cpu) {
46 xen_setup_runstate_info(cpu);
47 }
48 }
49 #endif
50 }
51
xen_pv_post_suspend(int suspend_cancelled)52 static void xen_pv_post_suspend(int suspend_cancelled)
53 {
54 xen_build_mfn_list_list();
55
56 xen_setup_shared_info();
57
58 if (suspend_cancelled) {
59 xen_start_info->store_mfn =
60 pfn_to_mfn(xen_start_info->store_mfn);
61 xen_start_info->console.domU.mfn =
62 pfn_to_mfn(xen_start_info->console.domU.mfn);
63 } else {
64 #ifdef CONFIG_SMP
65 BUG_ON(xen_cpu_initialized_map == NULL);
66 cpumask_copy(xen_cpu_initialized_map, cpu_online_mask);
67 #endif
68 xen_vcpu_restore();
69 }
70
71 xen_mm_unpin_all();
72 }
73
74 static DEFINE_PER_CPU(u64, spec_ctrl);
75
xen_arch_pre_suspend(void)76 void xen_arch_pre_suspend(void)
77 {
78 if (xen_pv_domain())
79 xen_pv_pre_suspend();
80 }
81
xen_arch_post_suspend(int cancelled)82 void xen_arch_post_suspend(int cancelled)
83 {
84 if (xen_pv_domain())
85 xen_pv_post_suspend(cancelled);
86 else
87 xen_hvm_post_suspend(cancelled);
88 }
89
xen_vcpu_notify_restore(void * data)90 static void xen_vcpu_notify_restore(void *data)
91 {
92 if (xen_pv_domain() && boot_cpu_has(X86_FEATURE_SPEC_CTRL))
93 wrmsrl(MSR_IA32_SPEC_CTRL, this_cpu_read(spec_ctrl));
94
95 /* Boot processor notified via generic timekeeping_resume() */
96 if (smp_processor_id() == 0)
97 return;
98
99 tick_resume_local();
100 }
101
xen_vcpu_notify_suspend(void * data)102 static void xen_vcpu_notify_suspend(void *data)
103 {
104 u64 tmp;
105
106 tick_suspend_local();
107
108 if (xen_pv_domain() && boot_cpu_has(X86_FEATURE_SPEC_CTRL)) {
109 rdmsrl(MSR_IA32_SPEC_CTRL, tmp);
110 this_cpu_write(spec_ctrl, tmp);
111 wrmsrl(MSR_IA32_SPEC_CTRL, 0);
112 }
113 }
114
xen_arch_resume(void)115 void xen_arch_resume(void)
116 {
117 int cpu;
118
119 on_each_cpu(xen_vcpu_notify_restore, NULL, 1);
120
121 for_each_online_cpu(cpu)
122 xen_pmu_init(cpu);
123 }
124
xen_arch_suspend(void)125 void xen_arch_suspend(void)
126 {
127 int cpu;
128
129 for_each_online_cpu(cpu)
130 xen_pmu_finish(cpu);
131
132 on_each_cpu(xen_vcpu_notify_suspend, NULL, 1);
133 }
134