xref: /linux/arch/x86/kernel/acpi/cstate.c (revision bc83cccc761953f878088cdfa682de0970b5561f)
123d6f82bSThomas Gleixner /*
223d6f82bSThomas Gleixner  * Copyright (C) 2005 Intel Corporation
323d6f82bSThomas Gleixner  * 	Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
423d6f82bSThomas Gleixner  * 	- Added _PDC for SMP C-states on Intel CPUs
523d6f82bSThomas Gleixner  */
623d6f82bSThomas Gleixner 
723d6f82bSThomas Gleixner #include <linux/kernel.h>
823d6f82bSThomas Gleixner #include <linux/module.h>
923d6f82bSThomas Gleixner #include <linux/init.h>
1023d6f82bSThomas Gleixner #include <linux/acpi.h>
1123d6f82bSThomas Gleixner #include <linux/cpu.h>
1223d6f82bSThomas Gleixner #include <linux/sched.h>
1323d6f82bSThomas Gleixner 
1423d6f82bSThomas Gleixner #include <acpi/processor.h>
1523d6f82bSThomas Gleixner #include <asm/acpi.h>
16*bc83ccccSH. Peter Anvin #include <asm/mwait.h>
1723d6f82bSThomas Gleixner 
1823d6f82bSThomas Gleixner /*
1923d6f82bSThomas Gleixner  * Initialize bm_flags based on the CPU cache properties
2023d6f82bSThomas Gleixner  * On SMP it depends on cache configuration
2123d6f82bSThomas Gleixner  * - When cache is not shared among all CPUs, we flush cache
2223d6f82bSThomas Gleixner  *   before entering C3.
2323d6f82bSThomas Gleixner  * - When cache is shared among all CPUs, we use bm_check
2423d6f82bSThomas Gleixner  *   mechanism as in UP case
2523d6f82bSThomas Gleixner  *
2623d6f82bSThomas Gleixner  * This routine is called only after all the CPUs are online
2723d6f82bSThomas Gleixner  */
2823d6f82bSThomas Gleixner void acpi_processor_power_init_bm_check(struct acpi_processor_flags *flags,
2923d6f82bSThomas Gleixner 					unsigned int cpu)
3023d6f82bSThomas Gleixner {
3192cb7612SMike Travis 	struct cpuinfo_x86 *c = &cpu_data(cpu);
3223d6f82bSThomas Gleixner 
3323d6f82bSThomas Gleixner 	flags->bm_check = 0;
3423d6f82bSThomas Gleixner 	if (num_online_cpus() == 1)
3523d6f82bSThomas Gleixner 		flags->bm_check = 1;
3623d6f82bSThomas Gleixner 	else if (c->x86_vendor == X86_VENDOR_INTEL) {
3723d6f82bSThomas Gleixner 		/*
38ee1ca48fSPallipadi, Venkatesh 		 * Today all MP CPUs that support C3 share cache.
39ee1ca48fSPallipadi, Venkatesh 		 * And caches should not be flushed by software while
40ee1ca48fSPallipadi, Venkatesh 		 * entering C3 type state.
4123d6f82bSThomas Gleixner 		 */
4223d6f82bSThomas Gleixner 		flags->bm_check = 1;
4323d6f82bSThomas Gleixner 	}
44ee1ca48fSPallipadi, Venkatesh 
45ee1ca48fSPallipadi, Venkatesh 	/*
46ee1ca48fSPallipadi, Venkatesh 	 * On all recent Intel platforms, ARB_DISABLE is a nop.
47ee1ca48fSPallipadi, Venkatesh 	 * So, set bm_control to zero to indicate that ARB_DISABLE
48ee1ca48fSPallipadi, Venkatesh 	 * is not required while entering C3 type state on
49ee1ca48fSPallipadi, Venkatesh 	 * P4, Core and beyond CPUs
50ee1ca48fSPallipadi, Venkatesh 	 */
51ee1ca48fSPallipadi, Venkatesh 	if (c->x86_vendor == X86_VENDOR_INTEL &&
5203a05ed1SZhao Yakui 	    (c->x86 > 0xf || (c->x86 == 6 && c->x86_model >= 0x0f)))
53ee1ca48fSPallipadi, Venkatesh 			flags->bm_control = 0;
5423d6f82bSThomas Gleixner }
5523d6f82bSThomas Gleixner EXPORT_SYMBOL(acpi_processor_power_init_bm_check);
5623d6f82bSThomas Gleixner 
5723d6f82bSThomas Gleixner /* The code below handles cstate entry with monitor-mwait pair on Intel*/
5823d6f82bSThomas Gleixner 
5923d6f82bSThomas Gleixner struct cstate_entry {
6023d6f82bSThomas Gleixner 	struct {
6123d6f82bSThomas Gleixner 		unsigned int eax;
6223d6f82bSThomas Gleixner 		unsigned int ecx;
6323d6f82bSThomas Gleixner 	} states[ACPI_PROCESSOR_MAX_POWER];
6423d6f82bSThomas Gleixner };
6523d6f82bSThomas Gleixner static struct cstate_entry *cpu_cstate_entry;	/* per CPU ptr */
6623d6f82bSThomas Gleixner 
6723d6f82bSThomas Gleixner static short mwait_supported[ACPI_PROCESSOR_MAX_POWER];
6823d6f82bSThomas Gleixner 
6923d6f82bSThomas Gleixner #define NATIVE_CSTATE_BEYOND_HALT	(2)
7023d6f82bSThomas Gleixner 
71c74f31c0SMike Travis static long acpi_processor_ffh_cstate_probe_cpu(void *_cx)
7223d6f82bSThomas Gleixner {
73c74f31c0SMike Travis 	struct acpi_processor_cx *cx = _cx;
74c74f31c0SMike Travis 	long retval;
7523d6f82bSThomas Gleixner 	unsigned int eax, ebx, ecx, edx;
7623d6f82bSThomas Gleixner 	unsigned int edx_part;
7723d6f82bSThomas Gleixner 	unsigned int cstate_type; /* C-state type and not ACPI C-state type */
7823d6f82bSThomas Gleixner 	unsigned int num_cstate_subtype;
7923d6f82bSThomas Gleixner 
8023d6f82bSThomas Gleixner 	cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
8123d6f82bSThomas Gleixner 
8223d6f82bSThomas Gleixner 	/* Check whether this particular cx_type (in CST) is supported or not */
8313b40a1aSZhao Yakui 	cstate_type = ((cx->address >> MWAIT_SUBSTATE_SIZE) &
8413b40a1aSZhao Yakui 			MWAIT_CSTATE_MASK) + 1;
8523d6f82bSThomas Gleixner 	edx_part = edx >> (cstate_type * MWAIT_SUBSTATE_SIZE);
8623d6f82bSThomas Gleixner 	num_cstate_subtype = edx_part & MWAIT_SUBSTATE_MASK;
8723d6f82bSThomas Gleixner 
8823d6f82bSThomas Gleixner 	retval = 0;
8923d6f82bSThomas Gleixner 	if (num_cstate_subtype < (cx->address & MWAIT_SUBSTATE_MASK)) {
9023d6f82bSThomas Gleixner 		retval = -1;
9123d6f82bSThomas Gleixner 		goto out;
9223d6f82bSThomas Gleixner 	}
9323d6f82bSThomas Gleixner 
9423d6f82bSThomas Gleixner 	/* mwait ecx extensions INTERRUPT_BREAK should be supported for C2/C3 */
9523d6f82bSThomas Gleixner 	if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
9623d6f82bSThomas Gleixner 	    !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) {
9723d6f82bSThomas Gleixner 		retval = -1;
9823d6f82bSThomas Gleixner 		goto out;
9923d6f82bSThomas Gleixner 	}
10023d6f82bSThomas Gleixner 
10123d6f82bSThomas Gleixner 	if (!mwait_supported[cstate_type]) {
10223d6f82bSThomas Gleixner 		mwait_supported[cstate_type] = 1;
103c74f31c0SMike Travis 		printk(KERN_DEBUG
104c74f31c0SMike Travis 			"Monitor-Mwait will be used to enter C-%d "
10523d6f82bSThomas Gleixner 			"state\n", cx->type);
10623d6f82bSThomas Gleixner 	}
107c74f31c0SMike Travis 	snprintf(cx->desc,
108c74f31c0SMike Travis 			ACPI_CX_DESC_LEN, "ACPI FFH INTEL MWAIT 0x%x",
1094fcb2fcdSVenkatesh Pallipadi 			cx->address);
11023d6f82bSThomas Gleixner out:
111c74f31c0SMike Travis 	return retval;
112c74f31c0SMike Travis }
113c74f31c0SMike Travis 
114c74f31c0SMike Travis int acpi_processor_ffh_cstate_probe(unsigned int cpu,
115c74f31c0SMike Travis 		struct acpi_processor_cx *cx, struct acpi_power_register *reg)
116c74f31c0SMike Travis {
117c74f31c0SMike Travis 	struct cstate_entry *percpu_entry;
118c74f31c0SMike Travis 	struct cpuinfo_x86 *c = &cpu_data(cpu);
119c74f31c0SMike Travis 	long retval;
120c74f31c0SMike Travis 
121c74f31c0SMike Travis 	if (!cpu_cstate_entry || c->cpuid_level < CPUID_MWAIT_LEAF)
122c74f31c0SMike Travis 		return -1;
123c74f31c0SMike Travis 
124c74f31c0SMike Travis 	if (reg->bit_offset != NATIVE_CSTATE_BEYOND_HALT)
125c74f31c0SMike Travis 		return -1;
126c74f31c0SMike Travis 
127c74f31c0SMike Travis 	percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
128c74f31c0SMike Travis 	percpu_entry->states[cx->index].eax = 0;
129c74f31c0SMike Travis 	percpu_entry->states[cx->index].ecx = 0;
130c74f31c0SMike Travis 
131c74f31c0SMike Travis 	/* Make sure we are running on right CPU */
132c74f31c0SMike Travis 
133c74f31c0SMike Travis 	retval = work_on_cpu(cpu, acpi_processor_ffh_cstate_probe_cpu, cx);
134c74f31c0SMike Travis 	if (retval == 0) {
135c74f31c0SMike Travis 		/* Use the hint in CST */
136c74f31c0SMike Travis 		percpu_entry->states[cx->index].eax = cx->address;
137c74f31c0SMike Travis 		percpu_entry->states[cx->index].ecx = MWAIT_ECX_INTERRUPT_BREAK;
138c74f31c0SMike Travis 	}
139718be4aaSLen Brown 
140718be4aaSLen Brown 	/*
141718be4aaSLen Brown 	 * For _CST FFH on Intel, if GAS.access_size bit 1 is cleared,
142718be4aaSLen Brown 	 * then we should skip checking BM_STS for this C-state.
143718be4aaSLen Brown 	 * ref: "Intel Processor Vendor-Specific ACPI Interface Specification"
144718be4aaSLen Brown 	 */
145718be4aaSLen Brown 	if ((c->x86_vendor == X86_VENDOR_INTEL) && !(reg->access_size & 0x2))
146718be4aaSLen Brown 		cx->bm_sts_skip = 1;
147718be4aaSLen Brown 
14823d6f82bSThomas Gleixner 	return retval;
14923d6f82bSThomas Gleixner }
15023d6f82bSThomas Gleixner EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
15123d6f82bSThomas Gleixner 
15223d6f82bSThomas Gleixner void acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
15323d6f82bSThomas Gleixner {
15423d6f82bSThomas Gleixner 	unsigned int cpu = smp_processor_id();
15523d6f82bSThomas Gleixner 	struct cstate_entry *percpu_entry;
15623d6f82bSThomas Gleixner 
15723d6f82bSThomas Gleixner 	percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
15823d6f82bSThomas Gleixner 	mwait_idle_with_hints(percpu_entry->states[cx->index].eax,
15923d6f82bSThomas Gleixner 	                      percpu_entry->states[cx->index].ecx);
16023d6f82bSThomas Gleixner }
16123d6f82bSThomas Gleixner EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_enter);
16223d6f82bSThomas Gleixner 
16323d6f82bSThomas Gleixner static int __init ffh_cstate_init(void)
16423d6f82bSThomas Gleixner {
16523d6f82bSThomas Gleixner 	struct cpuinfo_x86 *c = &boot_cpu_data;
16623d6f82bSThomas Gleixner 	if (c->x86_vendor != X86_VENDOR_INTEL)
16723d6f82bSThomas Gleixner 		return -1;
16823d6f82bSThomas Gleixner 
16923d6f82bSThomas Gleixner 	cpu_cstate_entry = alloc_percpu(struct cstate_entry);
17023d6f82bSThomas Gleixner 	return 0;
17123d6f82bSThomas Gleixner }
17223d6f82bSThomas Gleixner 
17323d6f82bSThomas Gleixner static void __exit ffh_cstate_exit(void)
17423d6f82bSThomas Gleixner {
17523d6f82bSThomas Gleixner 	free_percpu(cpu_cstate_entry);
17623d6f82bSThomas Gleixner 	cpu_cstate_entry = NULL;
17723d6f82bSThomas Gleixner }
17823d6f82bSThomas Gleixner 
17923d6f82bSThomas Gleixner arch_initcall(ffh_cstate_init);
18023d6f82bSThomas Gleixner __exitcall(ffh_cstate_exit);
181