xref: /openbmc/linux/arch/x86/power/cpu.c (revision 7a9c2dd08eadd5c6943115dbbec040c38d2e0822)
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>
1508687aecSSergio Luis 
1608687aecSSergio Luis #include <asm/pgtable.h>
1708687aecSSergio Luis #include <asm/proto.h>
1808687aecSSergio Luis #include <asm/mtrr.h>
1908687aecSSergio Luis #include <asm/page.h>
2008687aecSSergio Luis #include <asm/mce.h>
2108687aecSSergio Luis #include <asm/suspend.h>
22952f07ecSIngo Molnar #include <asm/fpu/internal.h>
23eadb8a09SIngo Molnar #include <asm/debugreg.h>
24a71c8bc5SFenghua Yu #include <asm/cpu.h>
2537868fe1SAndy Lutomirski #include <asm/mmu_context.h>
26*7a9c2dd0SChen Yu #include <linux/dmi.h>
2708687aecSSergio Luis 
2808687aecSSergio Luis #ifdef CONFIG_X86_32
29d6efc2f7SAndi Kleen __visible unsigned long saved_context_ebx;
30d6efc2f7SAndi Kleen __visible unsigned long saved_context_esp, saved_context_ebp;
31d6efc2f7SAndi Kleen __visible unsigned long saved_context_esi, saved_context_edi;
32d6efc2f7SAndi Kleen __visible unsigned long saved_context_eflags;
3308687aecSSergio Luis #endif
34cc456c4eSKonrad Rzeszutek Wilk struct saved_context saved_context;
3508687aecSSergio Luis 
36*7a9c2dd0SChen Yu static void msr_save_context(struct saved_context *ctxt)
37*7a9c2dd0SChen Yu {
38*7a9c2dd0SChen Yu 	struct saved_msr *msr = ctxt->saved_msrs.array;
39*7a9c2dd0SChen Yu 	struct saved_msr *end = msr + ctxt->saved_msrs.num;
40*7a9c2dd0SChen Yu 
41*7a9c2dd0SChen Yu 	while (msr < end) {
42*7a9c2dd0SChen Yu 		msr->valid = !rdmsrl_safe(msr->info.msr_no, &msr->info.reg.q);
43*7a9c2dd0SChen Yu 		msr++;
44*7a9c2dd0SChen Yu 	}
45*7a9c2dd0SChen Yu }
46*7a9c2dd0SChen Yu 
47*7a9c2dd0SChen Yu static void msr_restore_context(struct saved_context *ctxt)
48*7a9c2dd0SChen Yu {
49*7a9c2dd0SChen Yu 	struct saved_msr *msr = ctxt->saved_msrs.array;
50*7a9c2dd0SChen Yu 	struct saved_msr *end = msr + ctxt->saved_msrs.num;
51*7a9c2dd0SChen Yu 
52*7a9c2dd0SChen Yu 	while (msr < end) {
53*7a9c2dd0SChen Yu 		if (msr->valid)
54*7a9c2dd0SChen Yu 			wrmsrl(msr->info.msr_no, msr->info.reg.q);
55*7a9c2dd0SChen Yu 		msr++;
56*7a9c2dd0SChen Yu 	}
57*7a9c2dd0SChen Yu }
58*7a9c2dd0SChen Yu 
5908687aecSSergio Luis /**
6008687aecSSergio Luis  *	__save_processor_state - save CPU registers before creating a
6108687aecSSergio Luis  *		hibernation image and before restoring the memory state from it
6208687aecSSergio Luis  *	@ctxt - structure to store the registers contents in
6308687aecSSergio Luis  *
6408687aecSSergio Luis  *	NOTE: If there is a CPU register the modification of which by the
6508687aecSSergio Luis  *	boot kernel (ie. the kernel used for loading the hibernation image)
6608687aecSSergio Luis  *	might affect the operations of the restored target kernel (ie. the one
6708687aecSSergio Luis  *	saved in the hibernation image), then its contents must be saved by this
6808687aecSSergio Luis  *	function.  In other words, if kernel A is hibernated and different
6908687aecSSergio Luis  *	kernel B is used for loading the hibernation image into memory, the
7008687aecSSergio Luis  *	kernel A's __save_processor_state() function must save all registers
7108687aecSSergio Luis  *	needed by kernel A, so that it can operate correctly after the resume
7208687aecSSergio Luis  *	regardless of what kernel B does in the meantime.
7308687aecSSergio Luis  */
7408687aecSSergio Luis static void __save_processor_state(struct saved_context *ctxt)
7508687aecSSergio Luis {
7608687aecSSergio Luis #ifdef CONFIG_X86_32
7708687aecSSergio Luis 	mtrr_save_fixed_ranges(NULL);
7808687aecSSergio Luis #endif
7908687aecSSergio Luis 	kernel_fpu_begin();
8008687aecSSergio Luis 
8108687aecSSergio Luis 	/*
8208687aecSSergio Luis 	 * descriptor tables
8308687aecSSergio Luis 	 */
8408687aecSSergio Luis #ifdef CONFIG_X86_32
8508687aecSSergio Luis 	store_idt(&ctxt->idt);
8608687aecSSergio Luis #else
8708687aecSSergio Luis /* CONFIG_X86_64 */
8808687aecSSergio Luis 	store_idt((struct desc_ptr *)&ctxt->idt_limit);
8908687aecSSergio Luis #endif
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;
97cc456c4eSKonrad Rzeszutek Wilk 	ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_table(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 #ifdef CONFIG_X86_32
10608687aecSSergio Luis 	savesegment(es, ctxt->es);
10708687aecSSergio Luis 	savesegment(fs, ctxt->fs);
10808687aecSSergio Luis 	savesegment(gs, ctxt->gs);
10908687aecSSergio Luis 	savesegment(ss, ctxt->ss);
11008687aecSSergio Luis #else
11108687aecSSergio Luis /* CONFIG_X86_64 */
11208687aecSSergio Luis 	asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
11308687aecSSergio Luis 	asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
11408687aecSSergio Luis 	asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
11508687aecSSergio Luis 	asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
11608687aecSSergio Luis 	asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
11708687aecSSergio Luis 
11808687aecSSergio Luis 	rdmsrl(MSR_FS_BASE, ctxt->fs_base);
11908687aecSSergio Luis 	rdmsrl(MSR_GS_BASE, ctxt->gs_base);
12008687aecSSergio Luis 	rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
12108687aecSSergio Luis 	mtrr_save_fixed_ranges(NULL);
12208687aecSSergio Luis 
12308687aecSSergio Luis 	rdmsrl(MSR_EFER, ctxt->efer);
12408687aecSSergio Luis #endif
12508687aecSSergio Luis 
12608687aecSSergio Luis 	/*
12708687aecSSergio Luis 	 * control registers
12808687aecSSergio Luis 	 */
12908687aecSSergio Luis 	ctxt->cr0 = read_cr0();
13008687aecSSergio Luis 	ctxt->cr2 = read_cr2();
13108687aecSSergio Luis 	ctxt->cr3 = read_cr3();
1321e02ce4cSAndy Lutomirski 	ctxt->cr4 = __read_cr4_safe();
1331e02ce4cSAndy Lutomirski #ifdef CONFIG_X86_64
13408687aecSSergio Luis 	ctxt->cr8 = read_cr8();
13508687aecSSergio Luis #endif
13685a0e753SOndrej Zary 	ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
13785a0e753SOndrej Zary 					       &ctxt->misc_enable);
138*7a9c2dd0SChen Yu 	msr_save_context(ctxt);
13908687aecSSergio Luis }
14008687aecSSergio Luis 
14108687aecSSergio Luis /* Needed by apm.c */
14208687aecSSergio Luis void save_processor_state(void)
14308687aecSSergio Luis {
14408687aecSSergio Luis 	__save_processor_state(&saved_context);
145b74f05d6SMarcelo Tosatti 	x86_platform.save_sched_clock_state();
14608687aecSSergio Luis }
14708687aecSSergio Luis #ifdef CONFIG_X86_32
14808687aecSSergio Luis EXPORT_SYMBOL(save_processor_state);
14908687aecSSergio Luis #endif
15008687aecSSergio Luis 
15108687aecSSergio Luis static void do_fpu_end(void)
15208687aecSSergio Luis {
15308687aecSSergio Luis 	/*
15408687aecSSergio Luis 	 * Restore FPU regs if necessary.
15508687aecSSergio Luis 	 */
15608687aecSSergio Luis 	kernel_fpu_end();
15708687aecSSergio Luis }
15808687aecSSergio Luis 
15908687aecSSergio Luis static void fix_processor_context(void)
16008687aecSSergio Luis {
16108687aecSSergio Luis 	int cpu = smp_processor_id();
16224933b82SAndy Lutomirski 	struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1634d681be3Skonrad@kernel.org #ifdef CONFIG_X86_64
1644d681be3Skonrad@kernel.org 	struct desc_struct *desc = get_cpu_gdt_table(cpu);
1654d681be3Skonrad@kernel.org 	tss_desc tss;
1664d681be3Skonrad@kernel.org #endif
16708687aecSSergio Luis 	set_tss_desc(cpu, t);	/*
16808687aecSSergio Luis 				 * This just modifies memory; should not be
16908687aecSSergio Luis 				 * necessary. But... This is necessary, because
17008687aecSSergio Luis 				 * 386 hardware has concept of busy TSS or some
17108687aecSSergio Luis 				 * similar stupidity.
17208687aecSSergio Luis 				 */
17308687aecSSergio Luis 
17408687aecSSergio Luis #ifdef CONFIG_X86_64
1754d681be3Skonrad@kernel.org 	memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
1764d681be3Skonrad@kernel.org 	tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
1774d681be3Skonrad@kernel.org 	write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
17808687aecSSergio Luis 
17908687aecSSergio Luis 	syscall_init();				/* This sets MSR_*STAR and related */
18008687aecSSergio Luis #endif
18108687aecSSergio Luis 	load_TR_desc();				/* This does ltr */
18237868fe1SAndy Lutomirski 	load_mm_ldt(current->active_mm);	/* This does lldt */
1839254aaa0SIngo Molnar 
1849254aaa0SIngo Molnar 	fpu__resume_cpu();
18508687aecSSergio Luis }
18608687aecSSergio Luis 
18708687aecSSergio Luis /**
18808687aecSSergio Luis  *	__restore_processor_state - restore the contents of CPU registers saved
18908687aecSSergio Luis  *		by __save_processor_state()
19008687aecSSergio Luis  *	@ctxt - structure to load the registers contents from
19108687aecSSergio Luis  */
192b8f99b3eSSteven Rostedt (Red Hat) static void notrace __restore_processor_state(struct saved_context *ctxt)
19308687aecSSergio Luis {
19485a0e753SOndrej Zary 	if (ctxt->misc_enable_saved)
19585a0e753SOndrej Zary 		wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
19608687aecSSergio Luis 	/*
19708687aecSSergio Luis 	 * control registers
19808687aecSSergio Luis 	 */
19908687aecSSergio Luis 	/* cr4 was introduced in the Pentium CPU */
20008687aecSSergio Luis #ifdef CONFIG_X86_32
20108687aecSSergio Luis 	if (ctxt->cr4)
2021e02ce4cSAndy Lutomirski 		__write_cr4(ctxt->cr4);
20308687aecSSergio Luis #else
20408687aecSSergio Luis /* CONFIG X86_64 */
20508687aecSSergio Luis 	wrmsrl(MSR_EFER, ctxt->efer);
20608687aecSSergio Luis 	write_cr8(ctxt->cr8);
2071e02ce4cSAndy Lutomirski 	__write_cr4(ctxt->cr4);
20808687aecSSergio Luis #endif
20908687aecSSergio Luis 	write_cr3(ctxt->cr3);
21008687aecSSergio Luis 	write_cr2(ctxt->cr2);
21108687aecSSergio Luis 	write_cr0(ctxt->cr0);
21208687aecSSergio Luis 
21308687aecSSergio Luis 	/*
21408687aecSSergio Luis 	 * now restore the descriptor tables to their proper values
21508687aecSSergio Luis 	 * ltr is done i fix_processor_context().
21608687aecSSergio Luis 	 */
21708687aecSSergio Luis #ifdef CONFIG_X86_32
21808687aecSSergio Luis 	load_idt(&ctxt->idt);
21908687aecSSergio Luis #else
22008687aecSSergio Luis /* CONFIG_X86_64 */
22108687aecSSergio Luis 	load_idt((const struct desc_ptr *)&ctxt->idt_limit);
22208687aecSSergio Luis #endif
22308687aecSSergio Luis 
22408687aecSSergio Luis 	/*
22508687aecSSergio Luis 	 * segment registers
22608687aecSSergio Luis 	 */
22708687aecSSergio Luis #ifdef CONFIG_X86_32
22808687aecSSergio Luis 	loadsegment(es, ctxt->es);
22908687aecSSergio Luis 	loadsegment(fs, ctxt->fs);
23008687aecSSergio Luis 	loadsegment(gs, ctxt->gs);
23108687aecSSergio Luis 	loadsegment(ss, ctxt->ss);
23208687aecSSergio Luis 
23308687aecSSergio Luis 	/*
23408687aecSSergio Luis 	 * sysenter MSRs
23508687aecSSergio Luis 	 */
23608687aecSSergio Luis 	if (boot_cpu_has(X86_FEATURE_SEP))
23708687aecSSergio Luis 		enable_sep_cpu();
23808687aecSSergio Luis #else
23908687aecSSergio Luis /* CONFIG_X86_64 */
24008687aecSSergio Luis 	asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
24108687aecSSergio Luis 	asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
24208687aecSSergio Luis 	asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
24308687aecSSergio Luis 	load_gs_index(ctxt->gs);
24408687aecSSergio Luis 	asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
24508687aecSSergio Luis 
24608687aecSSergio Luis 	wrmsrl(MSR_FS_BASE, ctxt->fs_base);
24708687aecSSergio Luis 	wrmsrl(MSR_GS_BASE, ctxt->gs_base);
24808687aecSSergio Luis 	wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
24908687aecSSergio Luis #endif
25008687aecSSergio Luis 
25108687aecSSergio Luis 	fix_processor_context();
25208687aecSSergio Luis 
25308687aecSSergio Luis 	do_fpu_end();
254dba69d10SMarcelo Tosatti 	x86_platform.restore_sched_clock_state();
255d0af9eedSSuresh Siddha 	mtrr_bp_restore();
2561d9d8639SStephane Eranian 	perf_restore_debug_store();
257*7a9c2dd0SChen Yu 	msr_restore_context(ctxt);
25808687aecSSergio Luis }
25908687aecSSergio Luis 
26008687aecSSergio Luis /* Needed by apm.c */
261b8f99b3eSSteven Rostedt (Red Hat) void notrace restore_processor_state(void)
26208687aecSSergio Luis {
26308687aecSSergio Luis 	__restore_processor_state(&saved_context);
26408687aecSSergio Luis }
26508687aecSSergio Luis #ifdef CONFIG_X86_32
26608687aecSSergio Luis EXPORT_SYMBOL(restore_processor_state);
26708687aecSSergio Luis #endif
268209efae1SFenghua Yu 
269209efae1SFenghua Yu /*
270209efae1SFenghua Yu  * When bsp_check() is called in hibernate and suspend, cpu hotplug
271209efae1SFenghua Yu  * is disabled already. So it's unnessary to handle race condition between
272209efae1SFenghua Yu  * cpumask query and cpu hotplug.
273209efae1SFenghua Yu  */
274209efae1SFenghua Yu static int bsp_check(void)
275209efae1SFenghua Yu {
276209efae1SFenghua Yu 	if (cpumask_first(cpu_online_mask) != 0) {
277209efae1SFenghua Yu 		pr_warn("CPU0 is offline.\n");
278209efae1SFenghua Yu 		return -ENODEV;
279209efae1SFenghua Yu 	}
280209efae1SFenghua Yu 
281209efae1SFenghua Yu 	return 0;
282209efae1SFenghua Yu }
283209efae1SFenghua Yu 
284209efae1SFenghua Yu static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
285209efae1SFenghua Yu 			   void *ptr)
286209efae1SFenghua Yu {
287209efae1SFenghua Yu 	int ret = 0;
288209efae1SFenghua Yu 
289209efae1SFenghua Yu 	switch (action) {
290209efae1SFenghua Yu 	case PM_SUSPEND_PREPARE:
291209efae1SFenghua Yu 	case PM_HIBERNATION_PREPARE:
292209efae1SFenghua Yu 		ret = bsp_check();
293209efae1SFenghua Yu 		break;
294a71c8bc5SFenghua Yu #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
295a71c8bc5SFenghua Yu 	case PM_RESTORE_PREPARE:
296a71c8bc5SFenghua Yu 		/*
297a71c8bc5SFenghua Yu 		 * When system resumes from hibernation, online CPU0 because
298a71c8bc5SFenghua Yu 		 * 1. it's required for resume and
299a71c8bc5SFenghua Yu 		 * 2. the CPU was online before hibernation
300a71c8bc5SFenghua Yu 		 */
301a71c8bc5SFenghua Yu 		if (!cpu_online(0))
302a71c8bc5SFenghua Yu 			_debug_hotplug_cpu(0, 1);
303a71c8bc5SFenghua Yu 		break;
304a71c8bc5SFenghua Yu 	case PM_POST_RESTORE:
305a71c8bc5SFenghua Yu 		/*
306a71c8bc5SFenghua Yu 		 * When a resume really happens, this code won't be called.
307a71c8bc5SFenghua Yu 		 *
308a71c8bc5SFenghua Yu 		 * This code is called only when user space hibernation software
309a71c8bc5SFenghua Yu 		 * prepares for snapshot device during boot time. So we just
310a71c8bc5SFenghua Yu 		 * call _debug_hotplug_cpu() to restore to CPU0's state prior to
311a71c8bc5SFenghua Yu 		 * preparing the snapshot device.
312a71c8bc5SFenghua Yu 		 *
313a71c8bc5SFenghua Yu 		 * This works for normal boot case in our CPU0 hotplug debug
314a71c8bc5SFenghua Yu 		 * mode, i.e. CPU0 is offline and user mode hibernation
315a71c8bc5SFenghua Yu 		 * software initializes during boot time.
316a71c8bc5SFenghua Yu 		 *
317a71c8bc5SFenghua Yu 		 * If CPU0 is online and user application accesses snapshot
318a71c8bc5SFenghua Yu 		 * device after boot time, this will offline CPU0 and user may
319a71c8bc5SFenghua Yu 		 * see different CPU0 state before and after accessing
320a71c8bc5SFenghua Yu 		 * the snapshot device. But hopefully this is not a case when
321a71c8bc5SFenghua Yu 		 * user debugging CPU0 hotplug. Even if users hit this case,
322a71c8bc5SFenghua Yu 		 * they can easily online CPU0 back.
323a71c8bc5SFenghua Yu 		 *
324a71c8bc5SFenghua Yu 		 * To simplify this debug code, we only consider normal boot
325a71c8bc5SFenghua Yu 		 * case. Otherwise we need to remember CPU0's state and restore
326a71c8bc5SFenghua Yu 		 * to that state and resolve racy conditions etc.
327a71c8bc5SFenghua Yu 		 */
328a71c8bc5SFenghua Yu 		_debug_hotplug_cpu(0, 0);
329a71c8bc5SFenghua Yu 		break;
330a71c8bc5SFenghua Yu #endif
331209efae1SFenghua Yu 	default:
332209efae1SFenghua Yu 		break;
333209efae1SFenghua Yu 	}
334209efae1SFenghua Yu 	return notifier_from_errno(ret);
335209efae1SFenghua Yu }
336209efae1SFenghua Yu 
337209efae1SFenghua Yu static int __init bsp_pm_check_init(void)
338209efae1SFenghua Yu {
339209efae1SFenghua Yu 	/*
340209efae1SFenghua Yu 	 * Set this bsp_pm_callback as lower priority than
341209efae1SFenghua Yu 	 * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
342209efae1SFenghua Yu 	 * earlier to disable cpu hotplug before bsp online check.
343209efae1SFenghua Yu 	 */
344209efae1SFenghua Yu 	pm_notifier(bsp_pm_callback, -INT_MAX);
345209efae1SFenghua Yu 	return 0;
346209efae1SFenghua Yu }
347209efae1SFenghua Yu 
348209efae1SFenghua Yu core_initcall(bsp_pm_check_init);
349*7a9c2dd0SChen Yu 
350*7a9c2dd0SChen Yu static int msr_init_context(const u32 *msr_id, const int total_num)
351*7a9c2dd0SChen Yu {
352*7a9c2dd0SChen Yu 	int i = 0;
353*7a9c2dd0SChen Yu 	struct saved_msr *msr_array;
354*7a9c2dd0SChen Yu 
355*7a9c2dd0SChen Yu 	if (saved_context.saved_msrs.array || saved_context.saved_msrs.num > 0) {
356*7a9c2dd0SChen Yu 		pr_err("x86/pm: MSR quirk already applied, please check your DMI match table.\n");
357*7a9c2dd0SChen Yu 		return -EINVAL;
358*7a9c2dd0SChen Yu 	}
359*7a9c2dd0SChen Yu 
360*7a9c2dd0SChen Yu 	msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
361*7a9c2dd0SChen Yu 	if (!msr_array) {
362*7a9c2dd0SChen Yu 		pr_err("x86/pm: Can not allocate memory to save/restore MSRs during suspend.\n");
363*7a9c2dd0SChen Yu 		return -ENOMEM;
364*7a9c2dd0SChen Yu 	}
365*7a9c2dd0SChen Yu 
366*7a9c2dd0SChen Yu 	for (i = 0; i < total_num; i++) {
367*7a9c2dd0SChen Yu 		msr_array[i].info.msr_no	= msr_id[i];
368*7a9c2dd0SChen Yu 		msr_array[i].valid		= false;
369*7a9c2dd0SChen Yu 		msr_array[i].info.reg.q		= 0;
370*7a9c2dd0SChen Yu 	}
371*7a9c2dd0SChen Yu 	saved_context.saved_msrs.num	= total_num;
372*7a9c2dd0SChen Yu 	saved_context.saved_msrs.array	= msr_array;
373*7a9c2dd0SChen Yu 
374*7a9c2dd0SChen Yu 	return 0;
375*7a9c2dd0SChen Yu }
376*7a9c2dd0SChen Yu 
377*7a9c2dd0SChen Yu /*
378*7a9c2dd0SChen Yu  * The following section is a quirk framework for problematic BIOSen:
379*7a9c2dd0SChen Yu  * Sometimes MSRs are modified by the BIOSen after suspended to
380*7a9c2dd0SChen Yu  * RAM, this might cause unexpected behavior after wakeup.
381*7a9c2dd0SChen Yu  * Thus we save/restore these specified MSRs across suspend/resume
382*7a9c2dd0SChen Yu  * in order to work around it.
383*7a9c2dd0SChen Yu  *
384*7a9c2dd0SChen Yu  * For any further problematic BIOSen/platforms,
385*7a9c2dd0SChen Yu  * please add your own function similar to msr_initialize_bdw.
386*7a9c2dd0SChen Yu  */
387*7a9c2dd0SChen Yu static int msr_initialize_bdw(const struct dmi_system_id *d)
388*7a9c2dd0SChen Yu {
389*7a9c2dd0SChen Yu 	/* Add any extra MSR ids into this array. */
390*7a9c2dd0SChen Yu 	u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
391*7a9c2dd0SChen Yu 
392*7a9c2dd0SChen Yu 	pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
393*7a9c2dd0SChen Yu 	return msr_init_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
394*7a9c2dd0SChen Yu }
395*7a9c2dd0SChen Yu 
396*7a9c2dd0SChen Yu static struct dmi_system_id msr_save_dmi_table[] = {
397*7a9c2dd0SChen Yu 	{
398*7a9c2dd0SChen Yu 	 .callback = msr_initialize_bdw,
399*7a9c2dd0SChen Yu 	 .ident = "BROADWELL BDX_EP",
400*7a9c2dd0SChen Yu 	 .matches = {
401*7a9c2dd0SChen Yu 		DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),
402*7a9c2dd0SChen Yu 		DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),
403*7a9c2dd0SChen Yu 		},
404*7a9c2dd0SChen Yu 	},
405*7a9c2dd0SChen Yu 	{}
406*7a9c2dd0SChen Yu };
407*7a9c2dd0SChen Yu 
408*7a9c2dd0SChen Yu static int pm_check_save_msr(void)
409*7a9c2dd0SChen Yu {
410*7a9c2dd0SChen Yu 	dmi_check_system(msr_save_dmi_table);
411*7a9c2dd0SChen Yu 	return 0;
412*7a9c2dd0SChen Yu }
413*7a9c2dd0SChen Yu 
414*7a9c2dd0SChen Yu device_initcall(pm_check_save_msr);
415