xref: /openbmc/linux/arch/x86/power/cpu.c (revision 6faadbbb7f9da70ce484f98f72223c20125a1009)
108687aecSSergio Luis /*
208687aecSSergio Luis  * Suspend support specific for i386/x86-64.
308687aecSSergio Luis  *
408687aecSSergio Luis  * Distribute under GPLv2
508687aecSSergio Luis  *
608687aecSSergio Luis  * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
7a2531293SPavel Machek  * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
808687aecSSergio Luis  * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
908687aecSSergio Luis  */
1008687aecSSergio Luis 
1108687aecSSergio Luis #include <linux/suspend.h>
1269c60c88SPaul Gortmaker #include <linux/export.h>
1308687aecSSergio Luis #include <linux/smp.h>
141d9d8639SStephane Eranian #include <linux/perf_event.h>
15406f992eSRafael J. Wysocki #include <linux/tboot.h>
1608687aecSSergio Luis 
1708687aecSSergio Luis #include <asm/pgtable.h>
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>
23952f07ecSIngo Molnar #include <asm/fpu/internal.h>
24eadb8a09SIngo Molnar #include <asm/debugreg.h>
25a71c8bc5SFenghua Yu #include <asm/cpu.h>
2637868fe1SAndy Lutomirski #include <asm/mmu_context.h>
277a9c2dd0SChen Yu #include <linux/dmi.h>
2808687aecSSergio Luis 
2908687aecSSergio Luis #ifdef CONFIG_X86_32
30d6efc2f7SAndi Kleen __visible unsigned long saved_context_ebx;
31d6efc2f7SAndi Kleen __visible unsigned long saved_context_esp, saved_context_ebp;
32d6efc2f7SAndi Kleen __visible unsigned long saved_context_esi, saved_context_edi;
33d6efc2f7SAndi Kleen __visible unsigned long saved_context_eflags;
3408687aecSSergio Luis #endif
35cc456c4eSKonrad Rzeszutek Wilk struct saved_context saved_context;
3608687aecSSergio Luis 
377a9c2dd0SChen Yu static void msr_save_context(struct saved_context *ctxt)
387a9c2dd0SChen Yu {
397a9c2dd0SChen Yu 	struct saved_msr *msr = ctxt->saved_msrs.array;
407a9c2dd0SChen Yu 	struct saved_msr *end = msr + ctxt->saved_msrs.num;
417a9c2dd0SChen Yu 
427a9c2dd0SChen Yu 	while (msr < end) {
437a9c2dd0SChen Yu 		msr->valid = !rdmsrl_safe(msr->info.msr_no, &msr->info.reg.q);
447a9c2dd0SChen Yu 		msr++;
457a9c2dd0SChen Yu 	}
467a9c2dd0SChen Yu }
477a9c2dd0SChen Yu 
487a9c2dd0SChen Yu static void msr_restore_context(struct saved_context *ctxt)
497a9c2dd0SChen Yu {
507a9c2dd0SChen Yu 	struct saved_msr *msr = ctxt->saved_msrs.array;
517a9c2dd0SChen Yu 	struct saved_msr *end = msr + ctxt->saved_msrs.num;
527a9c2dd0SChen Yu 
537a9c2dd0SChen Yu 	while (msr < end) {
547a9c2dd0SChen Yu 		if (msr->valid)
557a9c2dd0SChen Yu 			wrmsrl(msr->info.msr_no, msr->info.reg.q);
567a9c2dd0SChen Yu 		msr++;
577a9c2dd0SChen Yu 	}
587a9c2dd0SChen Yu }
597a9c2dd0SChen Yu 
6008687aecSSergio Luis /**
6108687aecSSergio Luis  *	__save_processor_state - save CPU registers before creating a
6208687aecSSergio Luis  *		hibernation image and before restoring the memory state from it
6308687aecSSergio Luis  *	@ctxt - structure to store the registers contents in
6408687aecSSergio Luis  *
6508687aecSSergio Luis  *	NOTE: If there is a CPU register the modification of which by the
6608687aecSSergio Luis  *	boot kernel (ie. the kernel used for loading the hibernation image)
6708687aecSSergio Luis  *	might affect the operations of the restored target kernel (ie. the one
6808687aecSSergio Luis  *	saved in the hibernation image), then its contents must be saved by this
6908687aecSSergio Luis  *	function.  In other words, if kernel A is hibernated and different
7008687aecSSergio Luis  *	kernel B is used for loading the hibernation image into memory, the
7108687aecSSergio Luis  *	kernel A's __save_processor_state() function must save all registers
7208687aecSSergio Luis  *	needed by kernel A, so that it can operate correctly after the resume
7308687aecSSergio Luis  *	regardless of what kernel B does in the meantime.
7408687aecSSergio Luis  */
7508687aecSSergio Luis static void __save_processor_state(struct saved_context *ctxt)
7608687aecSSergio Luis {
7708687aecSSergio Luis #ifdef CONFIG_X86_32
7808687aecSSergio Luis 	mtrr_save_fixed_ranges(NULL);
7908687aecSSergio Luis #endif
8008687aecSSergio Luis 	kernel_fpu_begin();
8108687aecSSergio Luis 
8208687aecSSergio Luis 	/*
8308687aecSSergio Luis 	 * descriptor tables
8408687aecSSergio Luis 	 */
8508687aecSSergio Luis #ifdef CONFIG_X86_32
8608687aecSSergio Luis 	store_idt(&ctxt->idt);
8708687aecSSergio Luis #else
8808687aecSSergio Luis /* CONFIG_X86_64 */
8908687aecSSergio Luis 	store_idt((struct desc_ptr *)&ctxt->idt_limit);
9008687aecSSergio Luis #endif
91cc456c4eSKonrad Rzeszutek Wilk 	/*
92cc456c4eSKonrad Rzeszutek Wilk 	 * We save it here, but restore it only in the hibernate case.
93cc456c4eSKonrad Rzeszutek Wilk 	 * For ACPI S3 resume, this is loaded via 'early_gdt_desc' in 64-bit
94cc456c4eSKonrad Rzeszutek Wilk 	 * mode in "secondary_startup_64". In 32-bit mode it is done via
95cc456c4eSKonrad Rzeszutek Wilk 	 * 'pmode_gdt' in wakeup_start.
96cc456c4eSKonrad Rzeszutek Wilk 	 */
97cc456c4eSKonrad Rzeszutek Wilk 	ctxt->gdt_desc.size = GDT_SIZE - 1;
9869218e47SThomas Garnier 	ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_rw(smp_processor_id());
99cc456c4eSKonrad Rzeszutek Wilk 
10008687aecSSergio Luis 	store_tr(ctxt->tr);
10108687aecSSergio Luis 
10208687aecSSergio Luis 	/* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
10308687aecSSergio Luis 	/*
10408687aecSSergio Luis 	 * segment registers
10508687aecSSergio Luis 	 */
10608687aecSSergio Luis #ifdef CONFIG_X86_32
10708687aecSSergio Luis 	savesegment(es, ctxt->es);
10808687aecSSergio Luis 	savesegment(fs, ctxt->fs);
10908687aecSSergio Luis 	savesegment(gs, ctxt->gs);
11008687aecSSergio Luis 	savesegment(ss, ctxt->ss);
11108687aecSSergio Luis #else
11208687aecSSergio Luis /* CONFIG_X86_64 */
11308687aecSSergio Luis 	asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
11408687aecSSergio Luis 	asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
11508687aecSSergio Luis 	asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
11608687aecSSergio Luis 	asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
11708687aecSSergio Luis 	asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
11808687aecSSergio Luis 
11908687aecSSergio Luis 	rdmsrl(MSR_FS_BASE, ctxt->fs_base);
12008687aecSSergio Luis 	rdmsrl(MSR_GS_BASE, ctxt->gs_base);
12108687aecSSergio Luis 	rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
12208687aecSSergio Luis 	mtrr_save_fixed_ranges(NULL);
12308687aecSSergio Luis 
12408687aecSSergio Luis 	rdmsrl(MSR_EFER, ctxt->efer);
12508687aecSSergio Luis #endif
12608687aecSSergio Luis 
12708687aecSSergio Luis 	/*
12808687aecSSergio Luis 	 * control registers
12908687aecSSergio Luis 	 */
13008687aecSSergio Luis 	ctxt->cr0 = read_cr0();
13108687aecSSergio Luis 	ctxt->cr2 = read_cr2();
1326c690ee1SAndy Lutomirski 	ctxt->cr3 = __read_cr3();
1331ef55be1SAndy Lutomirski 	ctxt->cr4 = __read_cr4();
1341e02ce4cSAndy Lutomirski #ifdef CONFIG_X86_64
13508687aecSSergio Luis 	ctxt->cr8 = read_cr8();
13608687aecSSergio Luis #endif
13785a0e753SOndrej Zary 	ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
13885a0e753SOndrej Zary 					       &ctxt->misc_enable);
1397a9c2dd0SChen Yu 	msr_save_context(ctxt);
14008687aecSSergio Luis }
14108687aecSSergio Luis 
14208687aecSSergio Luis /* Needed by apm.c */
14308687aecSSergio Luis void save_processor_state(void)
14408687aecSSergio Luis {
14508687aecSSergio Luis 	__save_processor_state(&saved_context);
146b74f05d6SMarcelo Tosatti 	x86_platform.save_sched_clock_state();
14708687aecSSergio Luis }
14808687aecSSergio Luis #ifdef CONFIG_X86_32
14908687aecSSergio Luis EXPORT_SYMBOL(save_processor_state);
15008687aecSSergio Luis #endif
15108687aecSSergio Luis 
15208687aecSSergio Luis static void do_fpu_end(void)
15308687aecSSergio Luis {
15408687aecSSergio Luis 	/*
15508687aecSSergio Luis 	 * Restore FPU regs if necessary.
15608687aecSSergio Luis 	 */
15708687aecSSergio Luis 	kernel_fpu_end();
15808687aecSSergio Luis }
15908687aecSSergio Luis 
16008687aecSSergio Luis static void fix_processor_context(void)
16108687aecSSergio Luis {
16208687aecSSergio Luis 	int cpu = smp_processor_id();
16324933b82SAndy Lutomirski 	struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1644d681be3Skonrad@kernel.org #ifdef CONFIG_X86_64
16569218e47SThomas Garnier 	struct desc_struct *desc = get_cpu_gdt_rw(cpu);
1664d681be3Skonrad@kernel.org 	tss_desc tss;
1674d681be3Skonrad@kernel.org #endif
16808687aecSSergio Luis 	set_tss_desc(cpu, t);	/*
16908687aecSSergio Luis 				 * This just modifies memory; should not be
17008687aecSSergio Luis 				 * necessary. But... This is necessary, because
17108687aecSSergio Luis 				 * 386 hardware has concept of busy TSS or some
17208687aecSSergio Luis 				 * similar stupidity.
17308687aecSSergio Luis 				 */
17408687aecSSergio Luis 
17508687aecSSergio Luis #ifdef CONFIG_X86_64
1764d681be3Skonrad@kernel.org 	memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
1774d681be3Skonrad@kernel.org 	tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
1784d681be3Skonrad@kernel.org 	write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
17908687aecSSergio Luis 
18008687aecSSergio Luis 	syscall_init();				/* This sets MSR_*STAR and related */
18108687aecSSergio Luis #endif
18208687aecSSergio Luis 	load_TR_desc();				/* This does ltr */
18337868fe1SAndy Lutomirski 	load_mm_ldt(current->active_mm);	/* This does lldt */
18472c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
1859254aaa0SIngo Molnar 
1869254aaa0SIngo Molnar 	fpu__resume_cpu();
18769218e47SThomas Garnier 
18869218e47SThomas Garnier 	/* The processor is back on the direct GDT, load back the fixmap */
18969218e47SThomas Garnier 	load_fixmap_gdt(cpu);
19008687aecSSergio Luis }
19108687aecSSergio Luis 
19208687aecSSergio Luis /**
19308687aecSSergio Luis  *	__restore_processor_state - restore the contents of CPU registers saved
19408687aecSSergio Luis  *		by __save_processor_state()
19508687aecSSergio Luis  *	@ctxt - structure to load the registers contents from
19608687aecSSergio Luis  */
197b8f99b3eSSteven Rostedt (Red Hat) static void notrace __restore_processor_state(struct saved_context *ctxt)
19808687aecSSergio Luis {
19985a0e753SOndrej Zary 	if (ctxt->misc_enable_saved)
20085a0e753SOndrej Zary 		wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
20108687aecSSergio Luis 	/*
20208687aecSSergio Luis 	 * control registers
20308687aecSSergio Luis 	 */
20408687aecSSergio Luis 	/* cr4 was introduced in the Pentium CPU */
20508687aecSSergio Luis #ifdef CONFIG_X86_32
20608687aecSSergio Luis 	if (ctxt->cr4)
2071e02ce4cSAndy Lutomirski 		__write_cr4(ctxt->cr4);
20808687aecSSergio Luis #else
20908687aecSSergio Luis /* CONFIG X86_64 */
21008687aecSSergio Luis 	wrmsrl(MSR_EFER, ctxt->efer);
21108687aecSSergio Luis 	write_cr8(ctxt->cr8);
2121e02ce4cSAndy Lutomirski 	__write_cr4(ctxt->cr4);
21308687aecSSergio Luis #endif
21408687aecSSergio Luis 	write_cr3(ctxt->cr3);
21508687aecSSergio Luis 	write_cr2(ctxt->cr2);
21608687aecSSergio Luis 	write_cr0(ctxt->cr0);
21708687aecSSergio Luis 
21808687aecSSergio Luis 	/*
21908687aecSSergio Luis 	 * now restore the descriptor tables to their proper values
22008687aecSSergio Luis 	 * ltr is done i fix_processor_context().
22108687aecSSergio Luis 	 */
22208687aecSSergio Luis #ifdef CONFIG_X86_32
22308687aecSSergio Luis 	load_idt(&ctxt->idt);
22408687aecSSergio Luis #else
22508687aecSSergio Luis /* CONFIG_X86_64 */
22608687aecSSergio Luis 	load_idt((const struct desc_ptr *)&ctxt->idt_limit);
22708687aecSSergio Luis #endif
22808687aecSSergio Luis 
22908687aecSSergio Luis 	/*
23008687aecSSergio Luis 	 * segment registers
23108687aecSSergio Luis 	 */
23208687aecSSergio Luis #ifdef CONFIG_X86_32
23308687aecSSergio Luis 	loadsegment(es, ctxt->es);
23408687aecSSergio Luis 	loadsegment(fs, ctxt->fs);
23508687aecSSergio Luis 	loadsegment(gs, ctxt->gs);
23608687aecSSergio Luis 	loadsegment(ss, ctxt->ss);
23708687aecSSergio Luis 
23808687aecSSergio Luis 	/*
23908687aecSSergio Luis 	 * sysenter MSRs
24008687aecSSergio Luis 	 */
24108687aecSSergio Luis 	if (boot_cpu_has(X86_FEATURE_SEP))
24208687aecSSergio Luis 		enable_sep_cpu();
24308687aecSSergio Luis #else
24408687aecSSergio Luis /* CONFIG_X86_64 */
24508687aecSSergio Luis 	asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
24608687aecSSergio Luis 	asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
24708687aecSSergio Luis 	asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
24808687aecSSergio Luis 	load_gs_index(ctxt->gs);
24908687aecSSergio Luis 	asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
25008687aecSSergio Luis 
25108687aecSSergio Luis 	wrmsrl(MSR_FS_BASE, ctxt->fs_base);
25208687aecSSergio Luis 	wrmsrl(MSR_GS_BASE, ctxt->gs_base);
25308687aecSSergio Luis 	wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
25408687aecSSergio Luis #endif
25508687aecSSergio Luis 
25608687aecSSergio Luis 	fix_processor_context();
25708687aecSSergio Luis 
25808687aecSSergio Luis 	do_fpu_end();
2596a369583SThomas Gleixner 	tsc_verify_tsc_adjust(true);
260dba69d10SMarcelo Tosatti 	x86_platform.restore_sched_clock_state();
261d0af9eedSSuresh Siddha 	mtrr_bp_restore();
2621d9d8639SStephane Eranian 	perf_restore_debug_store();
2637a9c2dd0SChen Yu 	msr_restore_context(ctxt);
26408687aecSSergio Luis }
26508687aecSSergio Luis 
26608687aecSSergio Luis /* Needed by apm.c */
267b8f99b3eSSteven Rostedt (Red Hat) void notrace restore_processor_state(void)
26808687aecSSergio Luis {
26908687aecSSergio Luis 	__restore_processor_state(&saved_context);
27008687aecSSergio Luis }
27108687aecSSergio Luis #ifdef CONFIG_X86_32
27208687aecSSergio Luis EXPORT_SYMBOL(restore_processor_state);
27308687aecSSergio Luis #endif
274209efae1SFenghua Yu 
275406f992eSRafael J. Wysocki #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HOTPLUG_CPU)
276406f992eSRafael J. Wysocki static void resume_play_dead(void)
277406f992eSRafael J. Wysocki {
278406f992eSRafael J. Wysocki 	play_dead_common();
279406f992eSRafael J. Wysocki 	tboot_shutdown(TB_SHUTDOWN_WFS);
280406f992eSRafael J. Wysocki 	hlt_play_dead();
281406f992eSRafael J. Wysocki }
282406f992eSRafael J. Wysocki 
283406f992eSRafael J. Wysocki int hibernate_resume_nonboot_cpu_disable(void)
284406f992eSRafael J. Wysocki {
285406f992eSRafael J. Wysocki 	void (*play_dead)(void) = smp_ops.play_dead;
286406f992eSRafael J. Wysocki 	int ret;
287406f992eSRafael J. Wysocki 
288406f992eSRafael J. Wysocki 	/*
289406f992eSRafael J. Wysocki 	 * Ensure that MONITOR/MWAIT will not be used in the "play dead" loop
290406f992eSRafael J. Wysocki 	 * during hibernate image restoration, because it is likely that the
291406f992eSRafael J. Wysocki 	 * monitored address will be actually written to at that time and then
292406f992eSRafael J. Wysocki 	 * the "dead" CPU will attempt to execute instructions again, but the
293406f992eSRafael J. Wysocki 	 * address in its instruction pointer may not be possible to resolve
294406f992eSRafael J. Wysocki 	 * any more at that point (the page tables used by it previously may
295406f992eSRafael J. Wysocki 	 * have been overwritten by hibernate image data).
296406f992eSRafael J. Wysocki 	 */
297406f992eSRafael J. Wysocki 	smp_ops.play_dead = resume_play_dead;
298406f992eSRafael J. Wysocki 	ret = disable_nonboot_cpus();
299406f992eSRafael J. Wysocki 	smp_ops.play_dead = play_dead;
300406f992eSRafael J. Wysocki 	return ret;
301406f992eSRafael J. Wysocki }
302406f992eSRafael J. Wysocki #endif
303406f992eSRafael J. Wysocki 
304209efae1SFenghua Yu /*
305209efae1SFenghua Yu  * When bsp_check() is called in hibernate and suspend, cpu hotplug
306209efae1SFenghua Yu  * is disabled already. So it's unnessary to handle race condition between
307209efae1SFenghua Yu  * cpumask query and cpu hotplug.
308209efae1SFenghua Yu  */
309209efae1SFenghua Yu static int bsp_check(void)
310209efae1SFenghua Yu {
311209efae1SFenghua Yu 	if (cpumask_first(cpu_online_mask) != 0) {
312209efae1SFenghua Yu 		pr_warn("CPU0 is offline.\n");
313209efae1SFenghua Yu 		return -ENODEV;
314209efae1SFenghua Yu 	}
315209efae1SFenghua Yu 
316209efae1SFenghua Yu 	return 0;
317209efae1SFenghua Yu }
318209efae1SFenghua Yu 
319209efae1SFenghua Yu static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
320209efae1SFenghua Yu 			   void *ptr)
321209efae1SFenghua Yu {
322209efae1SFenghua Yu 	int ret = 0;
323209efae1SFenghua Yu 
324209efae1SFenghua Yu 	switch (action) {
325209efae1SFenghua Yu 	case PM_SUSPEND_PREPARE:
326209efae1SFenghua Yu 	case PM_HIBERNATION_PREPARE:
327209efae1SFenghua Yu 		ret = bsp_check();
328209efae1SFenghua Yu 		break;
329a71c8bc5SFenghua Yu #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
330a71c8bc5SFenghua Yu 	case PM_RESTORE_PREPARE:
331a71c8bc5SFenghua Yu 		/*
332a71c8bc5SFenghua Yu 		 * When system resumes from hibernation, online CPU0 because
333a71c8bc5SFenghua Yu 		 * 1. it's required for resume and
334a71c8bc5SFenghua Yu 		 * 2. the CPU was online before hibernation
335a71c8bc5SFenghua Yu 		 */
336a71c8bc5SFenghua Yu 		if (!cpu_online(0))
337a71c8bc5SFenghua Yu 			_debug_hotplug_cpu(0, 1);
338a71c8bc5SFenghua Yu 		break;
339a71c8bc5SFenghua Yu 	case PM_POST_RESTORE:
340a71c8bc5SFenghua Yu 		/*
341a71c8bc5SFenghua Yu 		 * When a resume really happens, this code won't be called.
342a71c8bc5SFenghua Yu 		 *
343a71c8bc5SFenghua Yu 		 * This code is called only when user space hibernation software
344a71c8bc5SFenghua Yu 		 * prepares for snapshot device during boot time. So we just
345a71c8bc5SFenghua Yu 		 * call _debug_hotplug_cpu() to restore to CPU0's state prior to
346a71c8bc5SFenghua Yu 		 * preparing the snapshot device.
347a71c8bc5SFenghua Yu 		 *
348a71c8bc5SFenghua Yu 		 * This works for normal boot case in our CPU0 hotplug debug
349a71c8bc5SFenghua Yu 		 * mode, i.e. CPU0 is offline and user mode hibernation
350a71c8bc5SFenghua Yu 		 * software initializes during boot time.
351a71c8bc5SFenghua Yu 		 *
352a71c8bc5SFenghua Yu 		 * If CPU0 is online and user application accesses snapshot
353a71c8bc5SFenghua Yu 		 * device after boot time, this will offline CPU0 and user may
354a71c8bc5SFenghua Yu 		 * see different CPU0 state before and after accessing
355a71c8bc5SFenghua Yu 		 * the snapshot device. But hopefully this is not a case when
356a71c8bc5SFenghua Yu 		 * user debugging CPU0 hotplug. Even if users hit this case,
357a71c8bc5SFenghua Yu 		 * they can easily online CPU0 back.
358a71c8bc5SFenghua Yu 		 *
359a71c8bc5SFenghua Yu 		 * To simplify this debug code, we only consider normal boot
360a71c8bc5SFenghua Yu 		 * case. Otherwise we need to remember CPU0's state and restore
361a71c8bc5SFenghua Yu 		 * to that state and resolve racy conditions etc.
362a71c8bc5SFenghua Yu 		 */
363a71c8bc5SFenghua Yu 		_debug_hotplug_cpu(0, 0);
364a71c8bc5SFenghua Yu 		break;
365a71c8bc5SFenghua Yu #endif
366209efae1SFenghua Yu 	default:
367209efae1SFenghua Yu 		break;
368209efae1SFenghua Yu 	}
369209efae1SFenghua Yu 	return notifier_from_errno(ret);
370209efae1SFenghua Yu }
371209efae1SFenghua Yu 
372209efae1SFenghua Yu static int __init bsp_pm_check_init(void)
373209efae1SFenghua Yu {
374209efae1SFenghua Yu 	/*
375209efae1SFenghua Yu 	 * Set this bsp_pm_callback as lower priority than
376209efae1SFenghua Yu 	 * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
377209efae1SFenghua Yu 	 * earlier to disable cpu hotplug before bsp online check.
378209efae1SFenghua Yu 	 */
379209efae1SFenghua Yu 	pm_notifier(bsp_pm_callback, -INT_MAX);
380209efae1SFenghua Yu 	return 0;
381209efae1SFenghua Yu }
382209efae1SFenghua Yu 
383209efae1SFenghua Yu core_initcall(bsp_pm_check_init);
3847a9c2dd0SChen Yu 
3857a9c2dd0SChen Yu static int msr_init_context(const u32 *msr_id, const int total_num)
3867a9c2dd0SChen Yu {
3877a9c2dd0SChen Yu 	int i = 0;
3887a9c2dd0SChen Yu 	struct saved_msr *msr_array;
3897a9c2dd0SChen Yu 
3907a9c2dd0SChen Yu 	if (saved_context.saved_msrs.array || saved_context.saved_msrs.num > 0) {
3917a9c2dd0SChen Yu 		pr_err("x86/pm: MSR quirk already applied, please check your DMI match table.\n");
3927a9c2dd0SChen Yu 		return -EINVAL;
3937a9c2dd0SChen Yu 	}
3947a9c2dd0SChen Yu 
3957a9c2dd0SChen Yu 	msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
3967a9c2dd0SChen Yu 	if (!msr_array) {
3977a9c2dd0SChen Yu 		pr_err("x86/pm: Can not allocate memory to save/restore MSRs during suspend.\n");
3987a9c2dd0SChen Yu 		return -ENOMEM;
3997a9c2dd0SChen Yu 	}
4007a9c2dd0SChen Yu 
4017a9c2dd0SChen Yu 	for (i = 0; i < total_num; i++) {
4027a9c2dd0SChen Yu 		msr_array[i].info.msr_no	= msr_id[i];
4037a9c2dd0SChen Yu 		msr_array[i].valid		= false;
4047a9c2dd0SChen Yu 		msr_array[i].info.reg.q		= 0;
4057a9c2dd0SChen Yu 	}
4067a9c2dd0SChen Yu 	saved_context.saved_msrs.num	= total_num;
4077a9c2dd0SChen Yu 	saved_context.saved_msrs.array	= msr_array;
4087a9c2dd0SChen Yu 
4097a9c2dd0SChen Yu 	return 0;
4107a9c2dd0SChen Yu }
4117a9c2dd0SChen Yu 
4127a9c2dd0SChen Yu /*
4137a9c2dd0SChen Yu  * The following section is a quirk framework for problematic BIOSen:
4147a9c2dd0SChen Yu  * Sometimes MSRs are modified by the BIOSen after suspended to
4157a9c2dd0SChen Yu  * RAM, this might cause unexpected behavior after wakeup.
4167a9c2dd0SChen Yu  * Thus we save/restore these specified MSRs across suspend/resume
4177a9c2dd0SChen Yu  * in order to work around it.
4187a9c2dd0SChen Yu  *
4197a9c2dd0SChen Yu  * For any further problematic BIOSen/platforms,
4207a9c2dd0SChen Yu  * please add your own function similar to msr_initialize_bdw.
4217a9c2dd0SChen Yu  */
4227a9c2dd0SChen Yu static int msr_initialize_bdw(const struct dmi_system_id *d)
4237a9c2dd0SChen Yu {
4247a9c2dd0SChen Yu 	/* Add any extra MSR ids into this array. */
4257a9c2dd0SChen Yu 	u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
4267a9c2dd0SChen Yu 
4277a9c2dd0SChen Yu 	pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
4287a9c2dd0SChen Yu 	return msr_init_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
4297a9c2dd0SChen Yu }
4307a9c2dd0SChen Yu 
431*6faadbbbSChristoph Hellwig static const struct dmi_system_id msr_save_dmi_table[] = {
4327a9c2dd0SChen Yu 	{
4337a9c2dd0SChen Yu 	 .callback = msr_initialize_bdw,
4347a9c2dd0SChen Yu 	 .ident = "BROADWELL BDX_EP",
4357a9c2dd0SChen Yu 	 .matches = {
4367a9c2dd0SChen Yu 		DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),
4377a9c2dd0SChen Yu 		DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),
4387a9c2dd0SChen Yu 		},
4397a9c2dd0SChen Yu 	},
4407a9c2dd0SChen Yu 	{}
4417a9c2dd0SChen Yu };
4427a9c2dd0SChen Yu 
4437a9c2dd0SChen Yu static int pm_check_save_msr(void)
4447a9c2dd0SChen Yu {
4457a9c2dd0SChen Yu 	dmi_check_system(msr_save_dmi_table);
4467a9c2dd0SChen Yu 	return 0;
4477a9c2dd0SChen Yu }
4487a9c2dd0SChen Yu 
4497a9c2dd0SChen Yu device_initcall(pm_check_save_msr);
450