xref: /openbmc/linux/arch/x86/power/cpu.c (revision f9e14dbbd454581061c736bf70bf5cbb15ac927c)
1767a67b0SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
208687aecSSergio Luis /*
308687aecSSergio Luis  * Suspend support specific for i386/x86-64.
408687aecSSergio Luis  *
508687aecSSergio Luis  * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
6a2531293SPavel Machek  * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
708687aecSSergio Luis  * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
808687aecSSergio Luis  */
908687aecSSergio Luis 
1008687aecSSergio Luis #include <linux/suspend.h>
1169c60c88SPaul Gortmaker #include <linux/export.h>
1208687aecSSergio Luis #include <linux/smp.h>
131d9d8639SStephane Eranian #include <linux/perf_event.h>
14406f992eSRafael J. Wysocki #include <linux/tboot.h>
15c49a0a80STom Lendacky #include <linux/dmi.h>
16ca5999fdSMike Rapoport #include <linux/pgtable.h>
1765fddcfcSMike Rapoport 
1808687aecSSergio Luis #include <asm/proto.h>
1908687aecSSergio Luis #include <asm/mtrr.h>
2008687aecSSergio Luis #include <asm/page.h>
2108687aecSSergio Luis #include <asm/mce.h>
2208687aecSSergio Luis #include <asm/suspend.h>
23b56d2795SThomas Gleixner #include <asm/fpu/api.h>
24eadb8a09SIngo Molnar #include <asm/debugreg.h>
25a71c8bc5SFenghua Yu #include <asm/cpu.h>
2637868fe1SAndy Lutomirski #include <asm/mmu_context.h>
27c49a0a80STom Lendacky #include <asm/cpu_device_id.h>
28*f9e14dbbSBorislav Petkov #include <asm/microcode.h>
2908687aecSSergio Luis 
3008687aecSSergio Luis #ifdef CONFIG_X86_32
31d6efc2f7SAndi Kleen __visible unsigned long saved_context_ebx;
32d6efc2f7SAndi Kleen __visible unsigned long saved_context_esp, saved_context_ebp;
33d6efc2f7SAndi Kleen __visible unsigned long saved_context_esi, saved_context_edi;
34d6efc2f7SAndi Kleen __visible unsigned long saved_context_eflags;
3508687aecSSergio Luis #endif
36cc456c4eSKonrad Rzeszutek Wilk struct saved_context saved_context;
3708687aecSSergio Luis 
387a9c2dd0SChen Yu static void msr_save_context(struct saved_context *ctxt)
397a9c2dd0SChen Yu {
407a9c2dd0SChen Yu 	struct saved_msr *msr = ctxt->saved_msrs.array;
417a9c2dd0SChen Yu 	struct saved_msr *end = msr + ctxt->saved_msrs.num;
427a9c2dd0SChen Yu 
437a9c2dd0SChen Yu 	while (msr < end) {
4473924ec4SPawan Gupta 		if (msr->valid)
4573924ec4SPawan Gupta 			rdmsrl(msr->info.msr_no, msr->info.reg.q);
467a9c2dd0SChen Yu 		msr++;
477a9c2dd0SChen Yu 	}
487a9c2dd0SChen Yu }
497a9c2dd0SChen Yu 
507a9c2dd0SChen Yu static void msr_restore_context(struct saved_context *ctxt)
517a9c2dd0SChen Yu {
527a9c2dd0SChen Yu 	struct saved_msr *msr = ctxt->saved_msrs.array;
537a9c2dd0SChen Yu 	struct saved_msr *end = msr + ctxt->saved_msrs.num;
547a9c2dd0SChen Yu 
557a9c2dd0SChen Yu 	while (msr < end) {
567a9c2dd0SChen Yu 		if (msr->valid)
577a9c2dd0SChen Yu 			wrmsrl(msr->info.msr_no, msr->info.reg.q);
587a9c2dd0SChen Yu 		msr++;
597a9c2dd0SChen Yu 	}
607a9c2dd0SChen Yu }
617a9c2dd0SChen Yu 
6208687aecSSergio Luis /**
63afc880cbSBaokun Li  * __save_processor_state() - Save CPU registers before creating a
64afc880cbSBaokun Li  *                             hibernation image and before restoring
65afc880cbSBaokun Li  *                             the memory state from it
66afc880cbSBaokun Li  * @ctxt: Structure to store the registers contents in.
6708687aecSSergio Luis  *
6808687aecSSergio Luis  * NOTE: If there is a CPU register the modification of which by the
6908687aecSSergio Luis  * boot kernel (ie. the kernel used for loading the hibernation image)
7008687aecSSergio Luis  * might affect the operations of the restored target kernel (ie. the one
7108687aecSSergio Luis  * saved in the hibernation image), then its contents must be saved by this
7208687aecSSergio Luis  * function.  In other words, if kernel A is hibernated and different
7308687aecSSergio Luis  * kernel B is used for loading the hibernation image into memory, the
7408687aecSSergio Luis  * kernel A's __save_processor_state() function must save all registers
7508687aecSSergio Luis  * needed by kernel A, so that it can operate correctly after the resume
7608687aecSSergio Luis  * regardless of what kernel B does in the meantime.
7708687aecSSergio Luis  */
7808687aecSSergio Luis static void __save_processor_state(struct saved_context *ctxt)
7908687aecSSergio Luis {
8008687aecSSergio Luis #ifdef CONFIG_X86_32
8108687aecSSergio Luis 	mtrr_save_fixed_ranges(NULL);
8208687aecSSergio Luis #endif
8308687aecSSergio Luis 	kernel_fpu_begin();
8408687aecSSergio Luis 
8508687aecSSergio Luis 	/*
8608687aecSSergio Luis 	 * descriptor tables
8708687aecSSergio Luis 	 */
8808687aecSSergio Luis 	store_idt(&ctxt->idt);
89090edbe2SAndy Lutomirski 
90cc456c4eSKonrad Rzeszutek Wilk 	/*
91cc456c4eSKonrad Rzeszutek Wilk 	 * We save it here, but restore it only in the hibernate case.
92cc456c4eSKonrad Rzeszutek Wilk 	 * For ACPI S3 resume, this is loaded via 'early_gdt_desc' in 64-bit
93cc456c4eSKonrad Rzeszutek Wilk 	 * mode in "secondary_startup_64". In 32-bit mode it is done via
94cc456c4eSKonrad Rzeszutek Wilk 	 * 'pmode_gdt' in wakeup_start.
95cc456c4eSKonrad Rzeszutek Wilk 	 */
96cc456c4eSKonrad Rzeszutek Wilk 	ctxt->gdt_desc.size = GDT_SIZE - 1;
9769218e47SThomas Garnier 	ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_rw(smp_processor_id());
98cc456c4eSKonrad Rzeszutek Wilk 
9908687aecSSergio Luis 	store_tr(ctxt->tr);
10008687aecSSergio Luis 
10108687aecSSergio Luis 	/* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
10208687aecSSergio Luis 	/*
10308687aecSSergio Luis 	 * segment registers
10408687aecSSergio Luis 	 */
10508687aecSSergio Luis 	savesegment(gs, ctxt->gs);
1067ee18d67SAndy Lutomirski #ifdef CONFIG_X86_64
1077ee18d67SAndy Lutomirski 	savesegment(fs, ctxt->fs);
1087ee18d67SAndy Lutomirski 	savesegment(ds, ctxt->ds);
1097ee18d67SAndy Lutomirski 	savesegment(es, ctxt->es);
11008687aecSSergio Luis 
11108687aecSSergio Luis 	rdmsrl(MSR_FS_BASE, ctxt->fs_base);
1127ee18d67SAndy Lutomirski 	rdmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
1137ee18d67SAndy Lutomirski 	rdmsrl(MSR_KERNEL_GS_BASE, ctxt->usermode_gs_base);
11408687aecSSergio Luis 	mtrr_save_fixed_ranges(NULL);
11508687aecSSergio Luis 
11608687aecSSergio Luis 	rdmsrl(MSR_EFER, ctxt->efer);
11708687aecSSergio Luis #endif
11808687aecSSergio Luis 
11908687aecSSergio Luis 	/*
12008687aecSSergio Luis 	 * control registers
12108687aecSSergio Luis 	 */
12208687aecSSergio Luis 	ctxt->cr0 = read_cr0();
12308687aecSSergio Luis 	ctxt->cr2 = read_cr2();
1246c690ee1SAndy Lutomirski 	ctxt->cr3 = __read_cr3();
1251ef55be1SAndy Lutomirski 	ctxt->cr4 = __read_cr4();
12685a0e753SOndrej Zary 	ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
12785a0e753SOndrej Zary 					       &ctxt->misc_enable);
1287a9c2dd0SChen Yu 	msr_save_context(ctxt);
12908687aecSSergio Luis }
13008687aecSSergio Luis 
13108687aecSSergio Luis /* Needed by apm.c */
13208687aecSSergio Luis void save_processor_state(void)
13308687aecSSergio Luis {
13408687aecSSergio Luis 	__save_processor_state(&saved_context);
135b74f05d6SMarcelo Tosatti 	x86_platform.save_sched_clock_state();
13608687aecSSergio Luis }
13708687aecSSergio Luis #ifdef CONFIG_X86_32
13808687aecSSergio Luis EXPORT_SYMBOL(save_processor_state);
13908687aecSSergio Luis #endif
14008687aecSSergio Luis 
14108687aecSSergio Luis static void do_fpu_end(void)
14208687aecSSergio Luis {
14308687aecSSergio Luis 	/*
14408687aecSSergio Luis 	 * Restore FPU regs if necessary.
14508687aecSSergio Luis 	 */
14608687aecSSergio Luis 	kernel_fpu_end();
14708687aecSSergio Luis }
14808687aecSSergio Luis 
14908687aecSSergio Luis static void fix_processor_context(void)
15008687aecSSergio Luis {
15108687aecSSergio Luis 	int cpu = smp_processor_id();
1524d681be3Skonrad@kernel.org #ifdef CONFIG_X86_64
15369218e47SThomas Garnier 	struct desc_struct *desc = get_cpu_gdt_rw(cpu);
1544d681be3Skonrad@kernel.org 	tss_desc tss;
1554d681be3Skonrad@kernel.org #endif
1567fb983b4SAndy Lutomirski 
1577fb983b4SAndy Lutomirski 	/*
15872f5e08dSAndy Lutomirski 	 * We need to reload TR, which requires that we change the
15972f5e08dSAndy Lutomirski 	 * GDT entry to indicate "available" first.
16072f5e08dSAndy Lutomirski 	 *
16172f5e08dSAndy Lutomirski 	 * XXX: This could probably all be replaced by a call to
16272f5e08dSAndy Lutomirski 	 * force_reload_TR().
16308687aecSSergio Luis 	 */
16472f5e08dSAndy Lutomirski 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
16508687aecSSergio Luis 
16608687aecSSergio Luis #ifdef CONFIG_X86_64
1674d681be3Skonrad@kernel.org 	memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
1684d681be3Skonrad@kernel.org 	tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
1694d681be3Skonrad@kernel.org 	write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
17008687aecSSergio Luis 
17108687aecSSergio Luis 	syscall_init();				/* This sets MSR_*STAR and related */
172896c80beSAndy Lutomirski #else
173896c80beSAndy Lutomirski 	if (boot_cpu_has(X86_FEATURE_SEP))
174896c80beSAndy Lutomirski 		enable_sep_cpu();
17508687aecSSergio Luis #endif
17608687aecSSergio Luis 	load_TR_desc();				/* This does ltr */
17737868fe1SAndy Lutomirski 	load_mm_ldt(current->active_mm);	/* This does lldt */
17872c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
1799254aaa0SIngo Molnar 
1809254aaa0SIngo Molnar 	fpu__resume_cpu();
18169218e47SThomas Garnier 
18269218e47SThomas Garnier 	/* The processor is back on the direct GDT, load back the fixmap */
18369218e47SThomas Garnier 	load_fixmap_gdt(cpu);
18408687aecSSergio Luis }
18508687aecSSergio Luis 
18608687aecSSergio Luis /**
187afc880cbSBaokun Li  * __restore_processor_state() - Restore the contents of CPU registers saved
18808687aecSSergio Luis  *                               by __save_processor_state()
189afc880cbSBaokun Li  * @ctxt: Structure to load the registers contents from.
1907ee18d67SAndy Lutomirski  *
1917ee18d67SAndy Lutomirski  * The asm code that gets us here will have restored a usable GDT, although
1927ee18d67SAndy Lutomirski  * it will be pointing to the wrong alias.
19308687aecSSergio Luis  */
194b8f99b3eSSteven Rostedt (Red Hat) static void notrace __restore_processor_state(struct saved_context *ctxt)
19508687aecSSergio Luis {
1965d510359SSean Christopherson 	struct cpuinfo_x86 *c;
1975d510359SSean Christopherson 
19885a0e753SOndrej Zary 	if (ctxt->misc_enable_saved)
19985a0e753SOndrej Zary 		wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
20008687aecSSergio Luis 	/*
20108687aecSSergio Luis 	 * control registers
20208687aecSSergio Luis 	 */
20308687aecSSergio Luis 	/* cr4 was introduced in the Pentium CPU */
20408687aecSSergio Luis #ifdef CONFIG_X86_32
20508687aecSSergio Luis 	if (ctxt->cr4)
2061e02ce4cSAndy Lutomirski 		__write_cr4(ctxt->cr4);
20708687aecSSergio Luis #else
20808687aecSSergio Luis /* CONFIG X86_64 */
20908687aecSSergio Luis 	wrmsrl(MSR_EFER, ctxt->efer);
2101e02ce4cSAndy Lutomirski 	__write_cr4(ctxt->cr4);
21108687aecSSergio Luis #endif
21208687aecSSergio Luis 	write_cr3(ctxt->cr3);
21308687aecSSergio Luis 	write_cr2(ctxt->cr2);
21408687aecSSergio Luis 	write_cr0(ctxt->cr0);
21508687aecSSergio Luis 
2167ee18d67SAndy Lutomirski 	/* Restore the IDT. */
21708687aecSSergio Luis 	load_idt(&ctxt->idt);
21808687aecSSergio Luis 
21908687aecSSergio Luis 	/*
2207ee18d67SAndy Lutomirski 	 * Just in case the asm code got us here with the SS, DS, or ES
2217ee18d67SAndy Lutomirski 	 * out of sync with the GDT, update them.
2225b06bbcfSAndy Lutomirski 	 */
2237ee18d67SAndy Lutomirski 	loadsegment(ss, __KERNEL_DS);
2247ee18d67SAndy Lutomirski 	loadsegment(ds, __USER_DS);
2257ee18d67SAndy Lutomirski 	loadsegment(es, __USER_DS);
2267ee18d67SAndy Lutomirski 
2277ee18d67SAndy Lutomirski 	/*
2287ee18d67SAndy Lutomirski 	 * Restore percpu access.  Percpu access can happen in exception
2297ee18d67SAndy Lutomirski 	 * handlers or in complicated helpers like load_gs_index().
2307ee18d67SAndy Lutomirski 	 */
2317ee18d67SAndy Lutomirski #ifdef CONFIG_X86_64
2327ee18d67SAndy Lutomirski 	wrmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
2337ee18d67SAndy Lutomirski #else
2347ee18d67SAndy Lutomirski 	loadsegment(fs, __KERNEL_PERCPU);
2355b06bbcfSAndy Lutomirski #endif
2365b06bbcfSAndy Lutomirski 
2377ee18d67SAndy Lutomirski 	/* Restore the TSS, RO GDT, LDT, and usermode-relevant MSRs. */
2385b06bbcfSAndy Lutomirski 	fix_processor_context();
2395b06bbcfSAndy Lutomirski 
2405b06bbcfSAndy Lutomirski 	/*
2417ee18d67SAndy Lutomirski 	 * Now that we have descriptor tables fully restored and working
2427ee18d67SAndy Lutomirski 	 * exception handling, restore the usermode segments.
24308687aecSSergio Luis 	 */
2447ee18d67SAndy Lutomirski #ifdef CONFIG_X86_64
2457ee18d67SAndy Lutomirski 	loadsegment(ds, ctxt->es);
24608687aecSSergio Luis 	loadsegment(es, ctxt->es);
24708687aecSSergio Luis 	loadsegment(fs, ctxt->fs);
24808687aecSSergio Luis 	load_gs_index(ctxt->gs);
24908687aecSSergio Luis 
2505b06bbcfSAndy Lutomirski 	/*
2517ee18d67SAndy Lutomirski 	 * Restore FSBASE and GSBASE after restoring the selectors, since
2527ee18d67SAndy Lutomirski 	 * restoring the selectors clobbers the bases.  Keep in mind
2537ee18d67SAndy Lutomirski 	 * that MSR_KERNEL_GS_BASE is horribly misnamed.
2545b06bbcfSAndy Lutomirski 	 */
25508687aecSSergio Luis 	wrmsrl(MSR_FS_BASE, ctxt->fs_base);
2567ee18d67SAndy Lutomirski 	wrmsrl(MSR_KERNEL_GS_BASE, ctxt->usermode_gs_base);
2573fb0fdb3SAndy Lutomirski #else
2587ee18d67SAndy Lutomirski 	loadsegment(gs, ctxt->gs);
25908687aecSSergio Luis #endif
26008687aecSSergio Luis 
26108687aecSSergio Luis 	do_fpu_end();
2626a369583SThomas Gleixner 	tsc_verify_tsc_adjust(true);
263dba69d10SMarcelo Tosatti 	x86_platform.restore_sched_clock_state();
264d0af9eedSSuresh Siddha 	mtrr_bp_restore();
2651d9d8639SStephane Eranian 	perf_restore_debug_store();
2665d510359SSean Christopherson 
2675d510359SSean Christopherson 	c = &cpu_data(smp_processor_id());
2685d510359SSean Christopherson 	if (cpu_has(c, X86_FEATURE_MSR_IA32_FEAT_CTL))
2695d510359SSean Christopherson 		init_ia32_feat_ctl(c);
270*f9e14dbbSBorislav Petkov 
271*f9e14dbbSBorislav Petkov 	microcode_bsp_resume();
272*f9e14dbbSBorislav Petkov 
273*f9e14dbbSBorislav Petkov 	/*
274*f9e14dbbSBorislav Petkov 	 * This needs to happen after the microcode has been updated upon resume
275*f9e14dbbSBorislav Petkov 	 * because some of the MSRs are "emulated" in microcode.
276*f9e14dbbSBorislav Petkov 	 */
277*f9e14dbbSBorislav Petkov 	msr_restore_context(ctxt);
27808687aecSSergio Luis }
27908687aecSSergio Luis 
28008687aecSSergio Luis /* Needed by apm.c */
281b8f99b3eSSteven Rostedt (Red Hat) void notrace restore_processor_state(void)
28208687aecSSergio Luis {
28308687aecSSergio Luis 	__restore_processor_state(&saved_context);
28408687aecSSergio Luis }
28508687aecSSergio Luis #ifdef CONFIG_X86_32
28608687aecSSergio Luis EXPORT_SYMBOL(restore_processor_state);
28708687aecSSergio Luis #endif
288209efae1SFenghua Yu 
289406f992eSRafael J. Wysocki #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HOTPLUG_CPU)
290406f992eSRafael J. Wysocki static void resume_play_dead(void)
291406f992eSRafael J. Wysocki {
292406f992eSRafael J. Wysocki 	play_dead_common();
293406f992eSRafael J. Wysocki 	tboot_shutdown(TB_SHUTDOWN_WFS);
294406f992eSRafael J. Wysocki 	hlt_play_dead();
295406f992eSRafael J. Wysocki }
296406f992eSRafael J. Wysocki 
297406f992eSRafael J. Wysocki int hibernate_resume_nonboot_cpu_disable(void)
298406f992eSRafael J. Wysocki {
299406f992eSRafael J. Wysocki 	void (*play_dead)(void) = smp_ops.play_dead;
300406f992eSRafael J. Wysocki 	int ret;
301406f992eSRafael J. Wysocki 
302406f992eSRafael J. Wysocki 	/*
303406f992eSRafael J. Wysocki 	 * Ensure that MONITOR/MWAIT will not be used in the "play dead" loop
304406f992eSRafael J. Wysocki 	 * during hibernate image restoration, because it is likely that the
305406f992eSRafael J. Wysocki 	 * monitored address will be actually written to at that time and then
306406f992eSRafael J. Wysocki 	 * the "dead" CPU will attempt to execute instructions again, but the
307406f992eSRafael J. Wysocki 	 * address in its instruction pointer may not be possible to resolve
308406f992eSRafael J. Wysocki 	 * any more at that point (the page tables used by it previously may
309406f992eSRafael J. Wysocki 	 * have been overwritten by hibernate image data).
310ec527c31SJiri Kosina 	 *
311ec527c31SJiri Kosina 	 * First, make sure that we wake up all the potentially disabled SMT
312ec527c31SJiri Kosina 	 * threads which have been initially brought up and then put into
313ec527c31SJiri Kosina 	 * mwait/cpuidle sleep.
314ec527c31SJiri Kosina 	 * Those will be put to proper (not interfering with hibernation
315ec527c31SJiri Kosina 	 * resume) sleep afterwards, and the resumed kernel will decide itself
316ec527c31SJiri Kosina 	 * what to do with them.
317406f992eSRafael J. Wysocki 	 */
318ec527c31SJiri Kosina 	ret = cpuhp_smt_enable();
319ec527c31SJiri Kosina 	if (ret)
320ec527c31SJiri Kosina 		return ret;
321406f992eSRafael J. Wysocki 	smp_ops.play_dead = resume_play_dead;
32256555855SQais Yousef 	ret = freeze_secondary_cpus(0);
323406f992eSRafael J. Wysocki 	smp_ops.play_dead = play_dead;
324406f992eSRafael J. Wysocki 	return ret;
325406f992eSRafael J. Wysocki }
326406f992eSRafael J. Wysocki #endif
327406f992eSRafael J. Wysocki 
328209efae1SFenghua Yu /*
329209efae1SFenghua Yu  * When bsp_check() is called in hibernate and suspend, cpu hotplug
330163b0991SIngo Molnar  * is disabled already. So it's unnecessary to handle race condition between
331209efae1SFenghua Yu  * cpumask query and cpu hotplug.
332209efae1SFenghua Yu  */
333209efae1SFenghua Yu static int bsp_check(void)
334209efae1SFenghua Yu {
335209efae1SFenghua Yu 	if (cpumask_first(cpu_online_mask) != 0) {
336209efae1SFenghua Yu 		pr_warn("CPU0 is offline.\n");
337209efae1SFenghua Yu 		return -ENODEV;
338209efae1SFenghua Yu 	}
339209efae1SFenghua Yu 
340209efae1SFenghua Yu 	return 0;
341209efae1SFenghua Yu }
342209efae1SFenghua Yu 
343209efae1SFenghua Yu static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
344209efae1SFenghua Yu 			   void *ptr)
345209efae1SFenghua Yu {
346209efae1SFenghua Yu 	int ret = 0;
347209efae1SFenghua Yu 
348209efae1SFenghua Yu 	switch (action) {
349209efae1SFenghua Yu 	case PM_SUSPEND_PREPARE:
350209efae1SFenghua Yu 	case PM_HIBERNATION_PREPARE:
351209efae1SFenghua Yu 		ret = bsp_check();
352209efae1SFenghua Yu 		break;
353a71c8bc5SFenghua Yu #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
354a71c8bc5SFenghua Yu 	case PM_RESTORE_PREPARE:
355a71c8bc5SFenghua Yu 		/*
356a71c8bc5SFenghua Yu 		 * When system resumes from hibernation, online CPU0 because
357a71c8bc5SFenghua Yu 		 * 1. it's required for resume and
358a71c8bc5SFenghua Yu 		 * 2. the CPU was online before hibernation
359a71c8bc5SFenghua Yu 		 */
360a71c8bc5SFenghua Yu 		if (!cpu_online(0))
361a71c8bc5SFenghua Yu 			_debug_hotplug_cpu(0, 1);
362a71c8bc5SFenghua Yu 		break;
363a71c8bc5SFenghua Yu 	case PM_POST_RESTORE:
364a71c8bc5SFenghua Yu 		/*
365a71c8bc5SFenghua Yu 		 * When a resume really happens, this code won't be called.
366a71c8bc5SFenghua Yu 		 *
367a71c8bc5SFenghua Yu 		 * This code is called only when user space hibernation software
368a71c8bc5SFenghua Yu 		 * prepares for snapshot device during boot time. So we just
369a71c8bc5SFenghua Yu 		 * call _debug_hotplug_cpu() to restore to CPU0's state prior to
370a71c8bc5SFenghua Yu 		 * preparing the snapshot device.
371a71c8bc5SFenghua Yu 		 *
372a71c8bc5SFenghua Yu 		 * This works for normal boot case in our CPU0 hotplug debug
373a71c8bc5SFenghua Yu 		 * mode, i.e. CPU0 is offline and user mode hibernation
374a71c8bc5SFenghua Yu 		 * software initializes during boot time.
375a71c8bc5SFenghua Yu 		 *
376a71c8bc5SFenghua Yu 		 * If CPU0 is online and user application accesses snapshot
377a71c8bc5SFenghua Yu 		 * device after boot time, this will offline CPU0 and user may
378a71c8bc5SFenghua Yu 		 * see different CPU0 state before and after accessing
379a71c8bc5SFenghua Yu 		 * the snapshot device. But hopefully this is not a case when
380a71c8bc5SFenghua Yu 		 * user debugging CPU0 hotplug. Even if users hit this case,
381a71c8bc5SFenghua Yu 		 * they can easily online CPU0 back.
382a71c8bc5SFenghua Yu 		 *
383a71c8bc5SFenghua Yu 		 * To simplify this debug code, we only consider normal boot
384a71c8bc5SFenghua Yu 		 * case. Otherwise we need to remember CPU0's state and restore
385a71c8bc5SFenghua Yu 		 * to that state and resolve racy conditions etc.
386a71c8bc5SFenghua Yu 		 */
387a71c8bc5SFenghua Yu 		_debug_hotplug_cpu(0, 0);
388a71c8bc5SFenghua Yu 		break;
389a71c8bc5SFenghua Yu #endif
390209efae1SFenghua Yu 	default:
391209efae1SFenghua Yu 		break;
392209efae1SFenghua Yu 	}
393209efae1SFenghua Yu 	return notifier_from_errno(ret);
394209efae1SFenghua Yu }
395209efae1SFenghua Yu 
396209efae1SFenghua Yu static int __init bsp_pm_check_init(void)
397209efae1SFenghua Yu {
398209efae1SFenghua Yu 	/*
399209efae1SFenghua Yu 	 * Set this bsp_pm_callback as lower priority than
400209efae1SFenghua Yu 	 * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
401209efae1SFenghua Yu 	 * earlier to disable cpu hotplug before bsp online check.
402209efae1SFenghua Yu 	 */
403209efae1SFenghua Yu 	pm_notifier(bsp_pm_callback, -INT_MAX);
404209efae1SFenghua Yu 	return 0;
405209efae1SFenghua Yu }
406209efae1SFenghua Yu 
407209efae1SFenghua Yu core_initcall(bsp_pm_check_init);
4087a9c2dd0SChen Yu 
409c49a0a80STom Lendacky static int msr_build_context(const u32 *msr_id, const int num)
4107a9c2dd0SChen Yu {
411c49a0a80STom Lendacky 	struct saved_msrs *saved_msrs = &saved_context.saved_msrs;
4127a9c2dd0SChen Yu 	struct saved_msr *msr_array;
413c49a0a80STom Lendacky 	int total_num;
414c49a0a80STom Lendacky 	int i, j;
4157a9c2dd0SChen Yu 
416c49a0a80STom Lendacky 	total_num = saved_msrs->num + num;
4177a9c2dd0SChen Yu 
4187a9c2dd0SChen Yu 	msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
4197a9c2dd0SChen Yu 	if (!msr_array) {
4207a9c2dd0SChen Yu 		pr_err("x86/pm: Can not allocate memory to save/restore MSRs during suspend.\n");
4217a9c2dd0SChen Yu 		return -ENOMEM;
4227a9c2dd0SChen Yu 	}
4237a9c2dd0SChen Yu 
424c49a0a80STom Lendacky 	if (saved_msrs->array) {
425c49a0a80STom Lendacky 		/*
426c49a0a80STom Lendacky 		 * Multiple callbacks can invoke this function, so copy any
427c49a0a80STom Lendacky 		 * MSR save requests from previous invocations.
428c49a0a80STom Lendacky 		 */
429c49a0a80STom Lendacky 		memcpy(msr_array, saved_msrs->array,
430c49a0a80STom Lendacky 		       sizeof(struct saved_msr) * saved_msrs->num);
431c49a0a80STom Lendacky 
432c49a0a80STom Lendacky 		kfree(saved_msrs->array);
433c49a0a80STom Lendacky 	}
434c49a0a80STom Lendacky 
435c49a0a80STom Lendacky 	for (i = saved_msrs->num, j = 0; i < total_num; i++, j++) {
43673924ec4SPawan Gupta 		u64 dummy;
43773924ec4SPawan Gupta 
438c49a0a80STom Lendacky 		msr_array[i].info.msr_no	= msr_id[j];
43973924ec4SPawan Gupta 		msr_array[i].valid		= !rdmsrl_safe(msr_id[j], &dummy);
4407a9c2dd0SChen Yu 		msr_array[i].info.reg.q		= 0;
4417a9c2dd0SChen Yu 	}
442c49a0a80STom Lendacky 	saved_msrs->num   = total_num;
443c49a0a80STom Lendacky 	saved_msrs->array = msr_array;
4447a9c2dd0SChen Yu 
4457a9c2dd0SChen Yu 	return 0;
4467a9c2dd0SChen Yu }
4477a9c2dd0SChen Yu 
4487a9c2dd0SChen Yu /*
449c49a0a80STom Lendacky  * The following sections are a quirk framework for problematic BIOSen:
4507a9c2dd0SChen Yu  * Sometimes MSRs are modified by the BIOSen after suspended to
4517a9c2dd0SChen Yu  * RAM, this might cause unexpected behavior after wakeup.
4527a9c2dd0SChen Yu  * Thus we save/restore these specified MSRs across suspend/resume
4537a9c2dd0SChen Yu  * in order to work around it.
4547a9c2dd0SChen Yu  *
4557a9c2dd0SChen Yu  * For any further problematic BIOSen/platforms,
4567a9c2dd0SChen Yu  * please add your own function similar to msr_initialize_bdw.
4577a9c2dd0SChen Yu  */
4587a9c2dd0SChen Yu static int msr_initialize_bdw(const struct dmi_system_id *d)
4597a9c2dd0SChen Yu {
4607a9c2dd0SChen Yu 	/* Add any extra MSR ids into this array. */
4617a9c2dd0SChen Yu 	u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
4627a9c2dd0SChen Yu 
4637a9c2dd0SChen Yu 	pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
464c49a0a80STom Lendacky 	return msr_build_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
4657a9c2dd0SChen Yu }
4667a9c2dd0SChen Yu 
4676faadbbbSChristoph Hellwig static const struct dmi_system_id msr_save_dmi_table[] = {
4687a9c2dd0SChen Yu 	{
4697a9c2dd0SChen Yu 	 .callback = msr_initialize_bdw,
4707a9c2dd0SChen Yu 	 .ident = "BROADWELL BDX_EP",
4717a9c2dd0SChen Yu 	 .matches = {
4727a9c2dd0SChen Yu 		DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),
4737a9c2dd0SChen Yu 		DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),
4747a9c2dd0SChen Yu 		},
4757a9c2dd0SChen Yu 	},
4767a9c2dd0SChen Yu 	{}
4777a9c2dd0SChen Yu };
4787a9c2dd0SChen Yu 
479c49a0a80STom Lendacky static int msr_save_cpuid_features(const struct x86_cpu_id *c)
480c49a0a80STom Lendacky {
481c49a0a80STom Lendacky 	u32 cpuid_msr_id[] = {
482c49a0a80STom Lendacky 		MSR_AMD64_CPUID_FN_1,
483c49a0a80STom Lendacky 	};
484c49a0a80STom Lendacky 
485c49a0a80STom Lendacky 	pr_info("x86/pm: family %#hx cpu detected, MSR saving is needed during suspending.\n",
486c49a0a80STom Lendacky 		c->family);
487c49a0a80STom Lendacky 
488c49a0a80STom Lendacky 	return msr_build_context(cpuid_msr_id, ARRAY_SIZE(cpuid_msr_id));
489c49a0a80STom Lendacky }
490c49a0a80STom Lendacky 
491c49a0a80STom Lendacky static const struct x86_cpu_id msr_save_cpu_table[] = {
492adefe55eSThomas Gleixner 	X86_MATCH_VENDOR_FAM(AMD, 0x15, &msr_save_cpuid_features),
493adefe55eSThomas Gleixner 	X86_MATCH_VENDOR_FAM(AMD, 0x16, &msr_save_cpuid_features),
494c49a0a80STom Lendacky 	{}
495c49a0a80STom Lendacky };
496c49a0a80STom Lendacky 
497c49a0a80STom Lendacky typedef int (*pm_cpu_match_t)(const struct x86_cpu_id *);
498c49a0a80STom Lendacky static int pm_cpu_check(const struct x86_cpu_id *c)
499c49a0a80STom Lendacky {
500c49a0a80STom Lendacky 	const struct x86_cpu_id *m;
501c49a0a80STom Lendacky 	int ret = 0;
502c49a0a80STom Lendacky 
503c49a0a80STom Lendacky 	m = x86_match_cpu(msr_save_cpu_table);
504c49a0a80STom Lendacky 	if (m) {
505c49a0a80STom Lendacky 		pm_cpu_match_t fn;
506c49a0a80STom Lendacky 
507c49a0a80STom Lendacky 		fn = (pm_cpu_match_t)m->driver_data;
508c49a0a80STom Lendacky 		ret = fn(m);
509c49a0a80STom Lendacky 	}
510c49a0a80STom Lendacky 
511c49a0a80STom Lendacky 	return ret;
512c49a0a80STom Lendacky }
513c49a0a80STom Lendacky 
514e2a1256bSPawan Gupta static void pm_save_spec_msr(void)
515e2a1256bSPawan Gupta {
516e2a1256bSPawan Gupta 	u32 spec_msr_id[] = {
517e2a1256bSPawan Gupta 		MSR_IA32_SPEC_CTRL,
518e2a1256bSPawan Gupta 		MSR_IA32_TSX_CTRL,
519e2a1256bSPawan Gupta 		MSR_TSX_FORCE_ABORT,
520e2a1256bSPawan Gupta 		MSR_IA32_MCU_OPT_CTRL,
521e2a1256bSPawan Gupta 		MSR_AMD64_LS_CFG,
522e2a1256bSPawan Gupta 	};
523e2a1256bSPawan Gupta 
524e2a1256bSPawan Gupta 	msr_build_context(spec_msr_id, ARRAY_SIZE(spec_msr_id));
525e2a1256bSPawan Gupta }
526e2a1256bSPawan Gupta 
5277a9c2dd0SChen Yu static int pm_check_save_msr(void)
5287a9c2dd0SChen Yu {
5297a9c2dd0SChen Yu 	dmi_check_system(msr_save_dmi_table);
530c49a0a80STom Lendacky 	pm_cpu_check(msr_save_cpu_table);
531e2a1256bSPawan Gupta 	pm_save_spec_msr();
532c49a0a80STom Lendacky 
5337a9c2dd0SChen Yu 	return 0;
5347a9c2dd0SChen Yu }
5357a9c2dd0SChen Yu 
5367a9c2dd0SChen Yu device_initcall(pm_check_save_msr);
537