xref: /OK3568_Linux_fs/kernel/arch/x86/power/cpu.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Suspend support specific for i386/x86-64.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
6*4882a593Smuzhiyun  * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
7*4882a593Smuzhiyun  * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/suspend.h>
11*4882a593Smuzhiyun #include <linux/export.h>
12*4882a593Smuzhiyun #include <linux/smp.h>
13*4882a593Smuzhiyun #include <linux/perf_event.h>
14*4882a593Smuzhiyun #include <linux/tboot.h>
15*4882a593Smuzhiyun #include <linux/dmi.h>
16*4882a593Smuzhiyun #include <linux/pgtable.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <asm/proto.h>
19*4882a593Smuzhiyun #include <asm/mtrr.h>
20*4882a593Smuzhiyun #include <asm/page.h>
21*4882a593Smuzhiyun #include <asm/mce.h>
22*4882a593Smuzhiyun #include <asm/suspend.h>
23*4882a593Smuzhiyun #include <asm/fpu/internal.h>
24*4882a593Smuzhiyun #include <asm/debugreg.h>
25*4882a593Smuzhiyun #include <asm/cpu.h>
26*4882a593Smuzhiyun #include <asm/mmu_context.h>
27*4882a593Smuzhiyun #include <asm/cpu_device_id.h>
28*4882a593Smuzhiyun #include <asm/microcode.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #ifdef CONFIG_X86_32
31*4882a593Smuzhiyun __visible unsigned long saved_context_ebx;
32*4882a593Smuzhiyun __visible unsigned long saved_context_esp, saved_context_ebp;
33*4882a593Smuzhiyun __visible unsigned long saved_context_esi, saved_context_edi;
34*4882a593Smuzhiyun __visible unsigned long saved_context_eflags;
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun struct saved_context saved_context;
37*4882a593Smuzhiyun 
msr_save_context(struct saved_context * ctxt)38*4882a593Smuzhiyun static void msr_save_context(struct saved_context *ctxt)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	struct saved_msr *msr = ctxt->saved_msrs.array;
41*4882a593Smuzhiyun 	struct saved_msr *end = msr + ctxt->saved_msrs.num;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	while (msr < end) {
44*4882a593Smuzhiyun 		if (msr->valid)
45*4882a593Smuzhiyun 			rdmsrl(msr->info.msr_no, msr->info.reg.q);
46*4882a593Smuzhiyun 		msr++;
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
msr_restore_context(struct saved_context * ctxt)50*4882a593Smuzhiyun static void msr_restore_context(struct saved_context *ctxt)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct saved_msr *msr = ctxt->saved_msrs.array;
53*4882a593Smuzhiyun 	struct saved_msr *end = msr + ctxt->saved_msrs.num;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	while (msr < end) {
56*4882a593Smuzhiyun 		if (msr->valid)
57*4882a593Smuzhiyun 			wrmsrl(msr->info.msr_no, msr->info.reg.q);
58*4882a593Smuzhiyun 		msr++;
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun  *	__save_processor_state - save CPU registers before creating a
64*4882a593Smuzhiyun  *		hibernation image and before restoring the memory state from it
65*4882a593Smuzhiyun  *	@ctxt - structure to store the registers contents in
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  *	NOTE: If there is a CPU register the modification of which by the
68*4882a593Smuzhiyun  *	boot kernel (ie. the kernel used for loading the hibernation image)
69*4882a593Smuzhiyun  *	might affect the operations of the restored target kernel (ie. the one
70*4882a593Smuzhiyun  *	saved in the hibernation image), then its contents must be saved by this
71*4882a593Smuzhiyun  *	function.  In other words, if kernel A is hibernated and different
72*4882a593Smuzhiyun  *	kernel B is used for loading the hibernation image into memory, the
73*4882a593Smuzhiyun  *	kernel A's __save_processor_state() function must save all registers
74*4882a593Smuzhiyun  *	needed by kernel A, so that it can operate correctly after the resume
75*4882a593Smuzhiyun  *	regardless of what kernel B does in the meantime.
76*4882a593Smuzhiyun  */
__save_processor_state(struct saved_context * ctxt)77*4882a593Smuzhiyun static void __save_processor_state(struct saved_context *ctxt)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun #ifdef CONFIG_X86_32
80*4882a593Smuzhiyun 	mtrr_save_fixed_ranges(NULL);
81*4882a593Smuzhiyun #endif
82*4882a593Smuzhiyun 	kernel_fpu_begin();
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/*
85*4882a593Smuzhiyun 	 * descriptor tables
86*4882a593Smuzhiyun 	 */
87*4882a593Smuzhiyun 	store_idt(&ctxt->idt);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/*
90*4882a593Smuzhiyun 	 * We save it here, but restore it only in the hibernate case.
91*4882a593Smuzhiyun 	 * For ACPI S3 resume, this is loaded via 'early_gdt_desc' in 64-bit
92*4882a593Smuzhiyun 	 * mode in "secondary_startup_64". In 32-bit mode it is done via
93*4882a593Smuzhiyun 	 * 'pmode_gdt' in wakeup_start.
94*4882a593Smuzhiyun 	 */
95*4882a593Smuzhiyun 	ctxt->gdt_desc.size = GDT_SIZE - 1;
96*4882a593Smuzhiyun 	ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_rw(smp_processor_id());
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	store_tr(ctxt->tr);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	/* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
101*4882a593Smuzhiyun 	/*
102*4882a593Smuzhiyun 	 * segment registers
103*4882a593Smuzhiyun 	 */
104*4882a593Smuzhiyun #ifdef CONFIG_X86_32_LAZY_GS
105*4882a593Smuzhiyun 	savesegment(gs, ctxt->gs);
106*4882a593Smuzhiyun #endif
107*4882a593Smuzhiyun #ifdef CONFIG_X86_64
108*4882a593Smuzhiyun 	savesegment(gs, ctxt->gs);
109*4882a593Smuzhiyun 	savesegment(fs, ctxt->fs);
110*4882a593Smuzhiyun 	savesegment(ds, ctxt->ds);
111*4882a593Smuzhiyun 	savesegment(es, ctxt->es);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	rdmsrl(MSR_FS_BASE, ctxt->fs_base);
114*4882a593Smuzhiyun 	rdmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
115*4882a593Smuzhiyun 	rdmsrl(MSR_KERNEL_GS_BASE, ctxt->usermode_gs_base);
116*4882a593Smuzhiyun 	mtrr_save_fixed_ranges(NULL);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	rdmsrl(MSR_EFER, ctxt->efer);
119*4882a593Smuzhiyun #endif
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/*
122*4882a593Smuzhiyun 	 * control registers
123*4882a593Smuzhiyun 	 */
124*4882a593Smuzhiyun 	ctxt->cr0 = read_cr0();
125*4882a593Smuzhiyun 	ctxt->cr2 = read_cr2();
126*4882a593Smuzhiyun 	ctxt->cr3 = __read_cr3();
127*4882a593Smuzhiyun 	ctxt->cr4 = __read_cr4();
128*4882a593Smuzhiyun 	ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
129*4882a593Smuzhiyun 					       &ctxt->misc_enable);
130*4882a593Smuzhiyun 	msr_save_context(ctxt);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /* Needed by apm.c */
save_processor_state(void)134*4882a593Smuzhiyun void save_processor_state(void)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	__save_processor_state(&saved_context);
137*4882a593Smuzhiyun 	x86_platform.save_sched_clock_state();
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun #ifdef CONFIG_X86_32
140*4882a593Smuzhiyun EXPORT_SYMBOL(save_processor_state);
141*4882a593Smuzhiyun #endif
142*4882a593Smuzhiyun 
do_fpu_end(void)143*4882a593Smuzhiyun static void do_fpu_end(void)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	/*
146*4882a593Smuzhiyun 	 * Restore FPU regs if necessary.
147*4882a593Smuzhiyun 	 */
148*4882a593Smuzhiyun 	kernel_fpu_end();
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
fix_processor_context(void)151*4882a593Smuzhiyun static void fix_processor_context(void)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	int cpu = smp_processor_id();
154*4882a593Smuzhiyun #ifdef CONFIG_X86_64
155*4882a593Smuzhiyun 	struct desc_struct *desc = get_cpu_gdt_rw(cpu);
156*4882a593Smuzhiyun 	tss_desc tss;
157*4882a593Smuzhiyun #endif
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/*
160*4882a593Smuzhiyun 	 * We need to reload TR, which requires that we change the
161*4882a593Smuzhiyun 	 * GDT entry to indicate "available" first.
162*4882a593Smuzhiyun 	 *
163*4882a593Smuzhiyun 	 * XXX: This could probably all be replaced by a call to
164*4882a593Smuzhiyun 	 * force_reload_TR().
165*4882a593Smuzhiyun 	 */
166*4882a593Smuzhiyun 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun #ifdef CONFIG_X86_64
169*4882a593Smuzhiyun 	memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
170*4882a593Smuzhiyun 	tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
171*4882a593Smuzhiyun 	write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	syscall_init();				/* This sets MSR_*STAR and related */
174*4882a593Smuzhiyun #else
175*4882a593Smuzhiyun 	if (boot_cpu_has(X86_FEATURE_SEP))
176*4882a593Smuzhiyun 		enable_sep_cpu();
177*4882a593Smuzhiyun #endif
178*4882a593Smuzhiyun 	load_TR_desc();				/* This does ltr */
179*4882a593Smuzhiyun 	load_mm_ldt(current->active_mm);	/* This does lldt */
180*4882a593Smuzhiyun 	initialize_tlbstate_and_flush();
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	fpu__resume_cpu();
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	/* The processor is back on the direct GDT, load back the fixmap */
185*4882a593Smuzhiyun 	load_fixmap_gdt(cpu);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /**
189*4882a593Smuzhiyun  * __restore_processor_state - restore the contents of CPU registers saved
190*4882a593Smuzhiyun  *                             by __save_processor_state()
191*4882a593Smuzhiyun  * @ctxt - structure to load the registers contents from
192*4882a593Smuzhiyun  *
193*4882a593Smuzhiyun  * The asm code that gets us here will have restored a usable GDT, although
194*4882a593Smuzhiyun  * it will be pointing to the wrong alias.
195*4882a593Smuzhiyun  */
__restore_processor_state(struct saved_context * ctxt)196*4882a593Smuzhiyun static void notrace __restore_processor_state(struct saved_context *ctxt)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	struct cpuinfo_x86 *c;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	if (ctxt->misc_enable_saved)
201*4882a593Smuzhiyun 		wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
202*4882a593Smuzhiyun 	/*
203*4882a593Smuzhiyun 	 * control registers
204*4882a593Smuzhiyun 	 */
205*4882a593Smuzhiyun 	/* cr4 was introduced in the Pentium CPU */
206*4882a593Smuzhiyun #ifdef CONFIG_X86_32
207*4882a593Smuzhiyun 	if (ctxt->cr4)
208*4882a593Smuzhiyun 		__write_cr4(ctxt->cr4);
209*4882a593Smuzhiyun #else
210*4882a593Smuzhiyun /* CONFIG X86_64 */
211*4882a593Smuzhiyun 	wrmsrl(MSR_EFER, ctxt->efer);
212*4882a593Smuzhiyun 	__write_cr4(ctxt->cr4);
213*4882a593Smuzhiyun #endif
214*4882a593Smuzhiyun 	write_cr3(ctxt->cr3);
215*4882a593Smuzhiyun 	write_cr2(ctxt->cr2);
216*4882a593Smuzhiyun 	write_cr0(ctxt->cr0);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/* Restore the IDT. */
219*4882a593Smuzhiyun 	load_idt(&ctxt->idt);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/*
222*4882a593Smuzhiyun 	 * Just in case the asm code got us here with the SS, DS, or ES
223*4882a593Smuzhiyun 	 * out of sync with the GDT, update them.
224*4882a593Smuzhiyun 	 */
225*4882a593Smuzhiyun 	loadsegment(ss, __KERNEL_DS);
226*4882a593Smuzhiyun 	loadsegment(ds, __USER_DS);
227*4882a593Smuzhiyun 	loadsegment(es, __USER_DS);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	/*
230*4882a593Smuzhiyun 	 * Restore percpu access.  Percpu access can happen in exception
231*4882a593Smuzhiyun 	 * handlers or in complicated helpers like load_gs_index().
232*4882a593Smuzhiyun 	 */
233*4882a593Smuzhiyun #ifdef CONFIG_X86_64
234*4882a593Smuzhiyun 	wrmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
235*4882a593Smuzhiyun #else
236*4882a593Smuzhiyun 	loadsegment(fs, __KERNEL_PERCPU);
237*4882a593Smuzhiyun 	loadsegment(gs, __KERNEL_STACK_CANARY);
238*4882a593Smuzhiyun #endif
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	/* Restore the TSS, RO GDT, LDT, and usermode-relevant MSRs. */
241*4882a593Smuzhiyun 	fix_processor_context();
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/*
244*4882a593Smuzhiyun 	 * Now that we have descriptor tables fully restored and working
245*4882a593Smuzhiyun 	 * exception handling, restore the usermode segments.
246*4882a593Smuzhiyun 	 */
247*4882a593Smuzhiyun #ifdef CONFIG_X86_64
248*4882a593Smuzhiyun 	loadsegment(ds, ctxt->es);
249*4882a593Smuzhiyun 	loadsegment(es, ctxt->es);
250*4882a593Smuzhiyun 	loadsegment(fs, ctxt->fs);
251*4882a593Smuzhiyun 	load_gs_index(ctxt->gs);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	/*
254*4882a593Smuzhiyun 	 * Restore FSBASE and GSBASE after restoring the selectors, since
255*4882a593Smuzhiyun 	 * restoring the selectors clobbers the bases.  Keep in mind
256*4882a593Smuzhiyun 	 * that MSR_KERNEL_GS_BASE is horribly misnamed.
257*4882a593Smuzhiyun 	 */
258*4882a593Smuzhiyun 	wrmsrl(MSR_FS_BASE, ctxt->fs_base);
259*4882a593Smuzhiyun 	wrmsrl(MSR_KERNEL_GS_BASE, ctxt->usermode_gs_base);
260*4882a593Smuzhiyun #elif defined(CONFIG_X86_32_LAZY_GS)
261*4882a593Smuzhiyun 	loadsegment(gs, ctxt->gs);
262*4882a593Smuzhiyun #endif
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	do_fpu_end();
265*4882a593Smuzhiyun 	tsc_verify_tsc_adjust(true);
266*4882a593Smuzhiyun 	x86_platform.restore_sched_clock_state();
267*4882a593Smuzhiyun 	mtrr_bp_restore();
268*4882a593Smuzhiyun 	perf_restore_debug_store();
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	c = &cpu_data(smp_processor_id());
271*4882a593Smuzhiyun 	if (cpu_has(c, X86_FEATURE_MSR_IA32_FEAT_CTL))
272*4882a593Smuzhiyun 		init_ia32_feat_ctl(c);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	microcode_bsp_resume();
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/*
277*4882a593Smuzhiyun 	 * This needs to happen after the microcode has been updated upon resume
278*4882a593Smuzhiyun 	 * because some of the MSRs are "emulated" in microcode.
279*4882a593Smuzhiyun 	 */
280*4882a593Smuzhiyun 	msr_restore_context(ctxt);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun /* Needed by apm.c */
restore_processor_state(void)284*4882a593Smuzhiyun void notrace restore_processor_state(void)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun #ifdef __clang__
287*4882a593Smuzhiyun 	// The following code snippet is copied from __restore_processor_state.
288*4882a593Smuzhiyun 	// Its purpose is to prepare GS segment before the function is called.
289*4882a593Smuzhiyun 	// Since the function is compiled with SCS on, it will use GS at its
290*4882a593Smuzhiyun 	// entry.
291*4882a593Smuzhiyun 	// TODO: Hack to be removed later when compiler bug is fixed.
292*4882a593Smuzhiyun #ifdef CONFIG_X86_64
293*4882a593Smuzhiyun 	wrmsrl(MSR_GS_BASE, saved_context.kernelmode_gs_base);
294*4882a593Smuzhiyun #else
295*4882a593Smuzhiyun 	loadsegment(fs, __KERNEL_PERCPU);
296*4882a593Smuzhiyun 	loadsegment(gs, __KERNEL_STACK_CANARY);
297*4882a593Smuzhiyun #endif
298*4882a593Smuzhiyun #endif
299*4882a593Smuzhiyun 	__restore_processor_state(&saved_context);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun #ifdef CONFIG_X86_32
302*4882a593Smuzhiyun EXPORT_SYMBOL(restore_processor_state);
303*4882a593Smuzhiyun #endif
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HOTPLUG_CPU)
resume_play_dead(void)306*4882a593Smuzhiyun static void resume_play_dead(void)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	play_dead_common();
309*4882a593Smuzhiyun 	tboot_shutdown(TB_SHUTDOWN_WFS);
310*4882a593Smuzhiyun 	hlt_play_dead();
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
hibernate_resume_nonboot_cpu_disable(void)313*4882a593Smuzhiyun int hibernate_resume_nonboot_cpu_disable(void)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	void (*play_dead)(void) = smp_ops.play_dead;
316*4882a593Smuzhiyun 	int ret;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/*
319*4882a593Smuzhiyun 	 * Ensure that MONITOR/MWAIT will not be used in the "play dead" loop
320*4882a593Smuzhiyun 	 * during hibernate image restoration, because it is likely that the
321*4882a593Smuzhiyun 	 * monitored address will be actually written to at that time and then
322*4882a593Smuzhiyun 	 * the "dead" CPU will attempt to execute instructions again, but the
323*4882a593Smuzhiyun 	 * address in its instruction pointer may not be possible to resolve
324*4882a593Smuzhiyun 	 * any more at that point (the page tables used by it previously may
325*4882a593Smuzhiyun 	 * have been overwritten by hibernate image data).
326*4882a593Smuzhiyun 	 *
327*4882a593Smuzhiyun 	 * First, make sure that we wake up all the potentially disabled SMT
328*4882a593Smuzhiyun 	 * threads which have been initially brought up and then put into
329*4882a593Smuzhiyun 	 * mwait/cpuidle sleep.
330*4882a593Smuzhiyun 	 * Those will be put to proper (not interfering with hibernation
331*4882a593Smuzhiyun 	 * resume) sleep afterwards, and the resumed kernel will decide itself
332*4882a593Smuzhiyun 	 * what to do with them.
333*4882a593Smuzhiyun 	 */
334*4882a593Smuzhiyun 	ret = cpuhp_smt_enable();
335*4882a593Smuzhiyun 	if (ret)
336*4882a593Smuzhiyun 		return ret;
337*4882a593Smuzhiyun 	smp_ops.play_dead = resume_play_dead;
338*4882a593Smuzhiyun 	ret = freeze_secondary_cpus(0);
339*4882a593Smuzhiyun 	smp_ops.play_dead = play_dead;
340*4882a593Smuzhiyun 	return ret;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun #endif
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /*
345*4882a593Smuzhiyun  * When bsp_check() is called in hibernate and suspend, cpu hotplug
346*4882a593Smuzhiyun  * is disabled already. So it's unnessary to handle race condition between
347*4882a593Smuzhiyun  * cpumask query and cpu hotplug.
348*4882a593Smuzhiyun  */
bsp_check(void)349*4882a593Smuzhiyun static int bsp_check(void)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun 	if (cpumask_first(cpu_online_mask) != 0) {
352*4882a593Smuzhiyun 		pr_warn("CPU0 is offline.\n");
353*4882a593Smuzhiyun 		return -ENODEV;
354*4882a593Smuzhiyun 	}
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	return 0;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
bsp_pm_callback(struct notifier_block * nb,unsigned long action,void * ptr)359*4882a593Smuzhiyun static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
360*4882a593Smuzhiyun 			   void *ptr)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	int ret = 0;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	switch (action) {
365*4882a593Smuzhiyun 	case PM_SUSPEND_PREPARE:
366*4882a593Smuzhiyun 	case PM_HIBERNATION_PREPARE:
367*4882a593Smuzhiyun 		ret = bsp_check();
368*4882a593Smuzhiyun 		break;
369*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
370*4882a593Smuzhiyun 	case PM_RESTORE_PREPARE:
371*4882a593Smuzhiyun 		/*
372*4882a593Smuzhiyun 		 * When system resumes from hibernation, online CPU0 because
373*4882a593Smuzhiyun 		 * 1. it's required for resume and
374*4882a593Smuzhiyun 		 * 2. the CPU was online before hibernation
375*4882a593Smuzhiyun 		 */
376*4882a593Smuzhiyun 		if (!cpu_online(0))
377*4882a593Smuzhiyun 			_debug_hotplug_cpu(0, 1);
378*4882a593Smuzhiyun 		break;
379*4882a593Smuzhiyun 	case PM_POST_RESTORE:
380*4882a593Smuzhiyun 		/*
381*4882a593Smuzhiyun 		 * When a resume really happens, this code won't be called.
382*4882a593Smuzhiyun 		 *
383*4882a593Smuzhiyun 		 * This code is called only when user space hibernation software
384*4882a593Smuzhiyun 		 * prepares for snapshot device during boot time. So we just
385*4882a593Smuzhiyun 		 * call _debug_hotplug_cpu() to restore to CPU0's state prior to
386*4882a593Smuzhiyun 		 * preparing the snapshot device.
387*4882a593Smuzhiyun 		 *
388*4882a593Smuzhiyun 		 * This works for normal boot case in our CPU0 hotplug debug
389*4882a593Smuzhiyun 		 * mode, i.e. CPU0 is offline and user mode hibernation
390*4882a593Smuzhiyun 		 * software initializes during boot time.
391*4882a593Smuzhiyun 		 *
392*4882a593Smuzhiyun 		 * If CPU0 is online and user application accesses snapshot
393*4882a593Smuzhiyun 		 * device after boot time, this will offline CPU0 and user may
394*4882a593Smuzhiyun 		 * see different CPU0 state before and after accessing
395*4882a593Smuzhiyun 		 * the snapshot device. But hopefully this is not a case when
396*4882a593Smuzhiyun 		 * user debugging CPU0 hotplug. Even if users hit this case,
397*4882a593Smuzhiyun 		 * they can easily online CPU0 back.
398*4882a593Smuzhiyun 		 *
399*4882a593Smuzhiyun 		 * To simplify this debug code, we only consider normal boot
400*4882a593Smuzhiyun 		 * case. Otherwise we need to remember CPU0's state and restore
401*4882a593Smuzhiyun 		 * to that state and resolve racy conditions etc.
402*4882a593Smuzhiyun 		 */
403*4882a593Smuzhiyun 		_debug_hotplug_cpu(0, 0);
404*4882a593Smuzhiyun 		break;
405*4882a593Smuzhiyun #endif
406*4882a593Smuzhiyun 	default:
407*4882a593Smuzhiyun 		break;
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 	return notifier_from_errno(ret);
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
bsp_pm_check_init(void)412*4882a593Smuzhiyun static int __init bsp_pm_check_init(void)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	/*
415*4882a593Smuzhiyun 	 * Set this bsp_pm_callback as lower priority than
416*4882a593Smuzhiyun 	 * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
417*4882a593Smuzhiyun 	 * earlier to disable cpu hotplug before bsp online check.
418*4882a593Smuzhiyun 	 */
419*4882a593Smuzhiyun 	pm_notifier(bsp_pm_callback, -INT_MAX);
420*4882a593Smuzhiyun 	return 0;
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun core_initcall(bsp_pm_check_init);
424*4882a593Smuzhiyun 
msr_build_context(const u32 * msr_id,const int num)425*4882a593Smuzhiyun static int msr_build_context(const u32 *msr_id, const int num)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct saved_msrs *saved_msrs = &saved_context.saved_msrs;
428*4882a593Smuzhiyun 	struct saved_msr *msr_array;
429*4882a593Smuzhiyun 	int total_num;
430*4882a593Smuzhiyun 	int i, j;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	total_num = saved_msrs->num + num;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
435*4882a593Smuzhiyun 	if (!msr_array) {
436*4882a593Smuzhiyun 		pr_err("x86/pm: Can not allocate memory to save/restore MSRs during suspend.\n");
437*4882a593Smuzhiyun 		return -ENOMEM;
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	if (saved_msrs->array) {
441*4882a593Smuzhiyun 		/*
442*4882a593Smuzhiyun 		 * Multiple callbacks can invoke this function, so copy any
443*4882a593Smuzhiyun 		 * MSR save requests from previous invocations.
444*4882a593Smuzhiyun 		 */
445*4882a593Smuzhiyun 		memcpy(msr_array, saved_msrs->array,
446*4882a593Smuzhiyun 		       sizeof(struct saved_msr) * saved_msrs->num);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 		kfree(saved_msrs->array);
449*4882a593Smuzhiyun 	}
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	for (i = saved_msrs->num, j = 0; i < total_num; i++, j++) {
452*4882a593Smuzhiyun 		u64 dummy;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 		msr_array[i].info.msr_no	= msr_id[j];
455*4882a593Smuzhiyun 		msr_array[i].valid		= !rdmsrl_safe(msr_id[j], &dummy);
456*4882a593Smuzhiyun 		msr_array[i].info.reg.q		= 0;
457*4882a593Smuzhiyun 	}
458*4882a593Smuzhiyun 	saved_msrs->num   = total_num;
459*4882a593Smuzhiyun 	saved_msrs->array = msr_array;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	return 0;
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /*
465*4882a593Smuzhiyun  * The following sections are a quirk framework for problematic BIOSen:
466*4882a593Smuzhiyun  * Sometimes MSRs are modified by the BIOSen after suspended to
467*4882a593Smuzhiyun  * RAM, this might cause unexpected behavior after wakeup.
468*4882a593Smuzhiyun  * Thus we save/restore these specified MSRs across suspend/resume
469*4882a593Smuzhiyun  * in order to work around it.
470*4882a593Smuzhiyun  *
471*4882a593Smuzhiyun  * For any further problematic BIOSen/platforms,
472*4882a593Smuzhiyun  * please add your own function similar to msr_initialize_bdw.
473*4882a593Smuzhiyun  */
msr_initialize_bdw(const struct dmi_system_id * d)474*4882a593Smuzhiyun static int msr_initialize_bdw(const struct dmi_system_id *d)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	/* Add any extra MSR ids into this array. */
477*4882a593Smuzhiyun 	u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
480*4882a593Smuzhiyun 	return msr_build_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun static const struct dmi_system_id msr_save_dmi_table[] = {
484*4882a593Smuzhiyun 	{
485*4882a593Smuzhiyun 	 .callback = msr_initialize_bdw,
486*4882a593Smuzhiyun 	 .ident = "BROADWELL BDX_EP",
487*4882a593Smuzhiyun 	 .matches = {
488*4882a593Smuzhiyun 		DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),
489*4882a593Smuzhiyun 		DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),
490*4882a593Smuzhiyun 		},
491*4882a593Smuzhiyun 	},
492*4882a593Smuzhiyun 	{}
493*4882a593Smuzhiyun };
494*4882a593Smuzhiyun 
msr_save_cpuid_features(const struct x86_cpu_id * c)495*4882a593Smuzhiyun static int msr_save_cpuid_features(const struct x86_cpu_id *c)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun 	u32 cpuid_msr_id[] = {
498*4882a593Smuzhiyun 		MSR_AMD64_CPUID_FN_1,
499*4882a593Smuzhiyun 	};
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	pr_info("x86/pm: family %#hx cpu detected, MSR saving is needed during suspending.\n",
502*4882a593Smuzhiyun 		c->family);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	return msr_build_context(cpuid_msr_id, ARRAY_SIZE(cpuid_msr_id));
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun static const struct x86_cpu_id msr_save_cpu_table[] = {
508*4882a593Smuzhiyun 	X86_MATCH_VENDOR_FAM(AMD, 0x15, &msr_save_cpuid_features),
509*4882a593Smuzhiyun 	X86_MATCH_VENDOR_FAM(AMD, 0x16, &msr_save_cpuid_features),
510*4882a593Smuzhiyun 	{}
511*4882a593Smuzhiyun };
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun typedef int (*pm_cpu_match_t)(const struct x86_cpu_id *);
pm_cpu_check(const struct x86_cpu_id * c)514*4882a593Smuzhiyun static int pm_cpu_check(const struct x86_cpu_id *c)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun 	const struct x86_cpu_id *m;
517*4882a593Smuzhiyun 	int ret = 0;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	m = x86_match_cpu(msr_save_cpu_table);
520*4882a593Smuzhiyun 	if (m) {
521*4882a593Smuzhiyun 		pm_cpu_match_t fn;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 		fn = (pm_cpu_match_t)m->driver_data;
524*4882a593Smuzhiyun 		ret = fn(m);
525*4882a593Smuzhiyun 	}
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	return ret;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
pm_save_spec_msr(void)530*4882a593Smuzhiyun static void pm_save_spec_msr(void)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun 	struct msr_enumeration {
533*4882a593Smuzhiyun 		u32 msr_no;
534*4882a593Smuzhiyun 		u32 feature;
535*4882a593Smuzhiyun 	} msr_enum[] = {
536*4882a593Smuzhiyun 		{ MSR_IA32_SPEC_CTRL,	 X86_FEATURE_MSR_SPEC_CTRL },
537*4882a593Smuzhiyun 		{ MSR_IA32_TSX_CTRL,	 X86_FEATURE_MSR_TSX_CTRL },
538*4882a593Smuzhiyun 		{ MSR_TSX_FORCE_ABORT,	 X86_FEATURE_TSX_FORCE_ABORT },
539*4882a593Smuzhiyun 		{ MSR_IA32_MCU_OPT_CTRL, X86_FEATURE_SRBDS_CTRL },
540*4882a593Smuzhiyun 		{ MSR_AMD64_LS_CFG,	 X86_FEATURE_LS_CFG_SSBD },
541*4882a593Smuzhiyun 		{ MSR_AMD64_DE_CFG,	 X86_FEATURE_LFENCE_RDTSC },
542*4882a593Smuzhiyun 	};
543*4882a593Smuzhiyun 	int i;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(msr_enum); i++) {
546*4882a593Smuzhiyun 		if (boot_cpu_has(msr_enum[i].feature))
547*4882a593Smuzhiyun 			msr_build_context(&msr_enum[i].msr_no, 1);
548*4882a593Smuzhiyun 	}
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
pm_check_save_msr(void)551*4882a593Smuzhiyun static int pm_check_save_msr(void)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun 	dmi_check_system(msr_save_dmi_table);
554*4882a593Smuzhiyun 	pm_cpu_check(msr_save_cpu_table);
555*4882a593Smuzhiyun 	pm_save_spec_msr();
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	return 0;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun device_initcall(pm_check_save_msr);
561