xref: /openbmc/linux/drivers/acpi/processor_idle.c (revision b488f021)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * processor_idle - idle state submodule to the ACPI processor driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
51da177e4SLinus Torvalds  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
61da177e4SLinus Torvalds  *  Copyright (C) 2004       Dominik Brodowski <linux@brodo.de>
71da177e4SLinus Torvalds  *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
81da177e4SLinus Torvalds  *  			- Added processor hotplug support
902df8b93SVenkatesh Pallipadi  *  Copyright (C) 2005  Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
1002df8b93SVenkatesh Pallipadi  *  			- Added support for C3 on SMP
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or modify
151da177e4SLinus Torvalds  *  it under the terms of the GNU General Public License as published by
161da177e4SLinus Torvalds  *  the Free Software Foundation; either version 2 of the License, or (at
171da177e4SLinus Torvalds  *  your option) any later version.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  This program is distributed in the hope that it will be useful, but
201da177e4SLinus Torvalds  *  WITHOUT ANY WARRANTY; without even the implied warranty of
211da177e4SLinus Torvalds  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
221da177e4SLinus Torvalds  *  General Public License for more details.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
251da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
261da177e4SLinus Torvalds  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <linux/kernel.h>
321da177e4SLinus Torvalds #include <linux/module.h>
331da177e4SLinus Torvalds #include <linux/init.h>
341da177e4SLinus Torvalds #include <linux/cpufreq.h>
351da177e4SLinus Torvalds #include <linux/proc_fs.h>
361da177e4SLinus Torvalds #include <linux/seq_file.h>
371da177e4SLinus Torvalds #include <linux/acpi.h>
381da177e4SLinus Torvalds #include <linux/dmi.h>
391da177e4SLinus Torvalds #include <linux/moduleparam.h>
404e57b681STim Schmielau #include <linux/sched.h>	/* need_resched() */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/io.h>
431da177e4SLinus Torvalds #include <asm/uaccess.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <acpi/acpi_bus.h>
461da177e4SLinus Torvalds #include <acpi/processor.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #define ACPI_PROCESSOR_COMPONENT        0x01000000
491da177e4SLinus Torvalds #define ACPI_PROCESSOR_CLASS            "processor"
501da177e4SLinus Torvalds #define ACPI_PROCESSOR_DRIVER_NAME      "ACPI Processor Driver"
511da177e4SLinus Torvalds #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
521da177e4SLinus Torvalds ACPI_MODULE_NAME("acpi_processor")
531da177e4SLinus Torvalds #define ACPI_PROCESSOR_FILE_POWER	"power"
541da177e4SLinus Torvalds #define US_TO_PM_TIMER_TICKS(t)		((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
551da177e4SLinus Torvalds #define C2_OVERHEAD			4	/* 1us (3.579 ticks per us) */
561da177e4SLinus Torvalds #define C3_OVERHEAD			4	/* 1us (3.579 ticks per us) */
57b6835052SAndreas Mohr static void (*pm_idle_save) (void) __read_mostly;
581da177e4SLinus Torvalds module_param(max_cstate, uint, 0644);
591da177e4SLinus Torvalds 
60b6835052SAndreas Mohr static unsigned int nocst __read_mostly;
611da177e4SLinus Torvalds module_param(nocst, uint, 0000);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /*
641da177e4SLinus Torvalds  * bm_history -- bit-mask with a bit per jiffy of bus-master activity
651da177e4SLinus Torvalds  * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
661da177e4SLinus Torvalds  * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
671da177e4SLinus Torvalds  * 100 HZ: 0x0000000F: 4 jiffies = 40ms
681da177e4SLinus Torvalds  * reduce history for more aggressive entry into C3
691da177e4SLinus Torvalds  */
70b6835052SAndreas Mohr static unsigned int bm_history __read_mostly =
714be44fcdSLen Brown     (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
721da177e4SLinus Torvalds module_param(bm_history, uint, 0644);
731da177e4SLinus Torvalds /* --------------------------------------------------------------------------
741da177e4SLinus Torvalds                                 Power Management
751da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds  * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
791da177e4SLinus Torvalds  * For now disable this. Probably a bug somewhere else.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  * To skip this limit, boot/load with a large max_cstate limit.
821da177e4SLinus Torvalds  */
83335f16beSDavid Shaohua Li static int set_max_cstate(struct dmi_system_id *id)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds 	if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
861da177e4SLinus Torvalds 		return 0;
871da177e4SLinus Torvalds 
883d35600aSLen Brown 	printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
891da177e4SLinus Torvalds 	       " Override with \"processor.max_cstate=%d\"\n", id->ident,
903d35600aSLen Brown 	       (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
911da177e4SLinus Torvalds 
923d35600aSLen Brown 	max_cstate = (long)id->driver_data;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	return 0;
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
977ded5689SAshok Raj /* Actually this shouldn't be __cpuinitdata, would be better to fix the
987ded5689SAshok Raj    callers to only run once -AK */
997ded5689SAshok Raj static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
100335f16beSDavid Shaohua Li 	{ set_max_cstate, "IBM ThinkPad R40e", {
101876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
102876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
103876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
104876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
105876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
106876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
107876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
108876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
109876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
110876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
111876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
112876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
113876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
114876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
115876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
116876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
117876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
118876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
119876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
120876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
121876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
122876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
123876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
124876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
125876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
126876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
127876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
128876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
129876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
130876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
131876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
132876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
133876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
134876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
135876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
136876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
137876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
138876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
139876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
140876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
141876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
142876c184bSThomas Rosner 	{ set_max_cstate, "IBM ThinkPad R40e", {
143876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
144876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
145335f16beSDavid Shaohua Li 	{ set_max_cstate, "Medion 41700", {
146876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
147876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
148335f16beSDavid Shaohua Li 	{ set_max_cstate, "Clevo 5600D", {
149876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
150876c184bSThomas Rosner 	  DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
151335f16beSDavid Shaohua Li 	 (void *)2},
1521da177e4SLinus Torvalds 	{},
1531da177e4SLinus Torvalds };
1541da177e4SLinus Torvalds 
1554be44fcdSLen Brown static inline u32 ticks_elapsed(u32 t1, u32 t2)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	if (t2 >= t1)
1581da177e4SLinus Torvalds 		return (t2 - t1);
1591da177e4SLinus Torvalds 	else if (!acpi_fadt.tmr_val_ext)
1601da177e4SLinus Torvalds 		return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
1611da177e4SLinus Torvalds 	else
1621da177e4SLinus Torvalds 		return ((0xFFFFFFFF - t1) + t2);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds static void
1664be44fcdSLen Brown acpi_processor_power_activate(struct acpi_processor *pr,
1671da177e4SLinus Torvalds 			      struct acpi_processor_cx *new)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	struct acpi_processor_cx *old;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	if (!pr || !new)
1721da177e4SLinus Torvalds 		return;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	old = pr->power.state;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	if (old)
1771da177e4SLinus Torvalds 		old->promotion.count = 0;
1781da177e4SLinus Torvalds 	new->demotion.count = 0;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	/* Cleanup from old state. */
1811da177e4SLinus Torvalds 	if (old) {
1821da177e4SLinus Torvalds 		switch (old->type) {
1831da177e4SLinus Torvalds 		case ACPI_STATE_C3:
1841da177e4SLinus Torvalds 			/* Disable bus master reload */
18502df8b93SVenkatesh Pallipadi 			if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
1864be44fcdSLen Brown 				acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
1874be44fcdSLen Brown 						  ACPI_MTX_DO_NOT_LOCK);
1881da177e4SLinus Torvalds 			break;
1891da177e4SLinus Torvalds 		}
1901da177e4SLinus Torvalds 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* Prepare to use new state. */
1931da177e4SLinus Torvalds 	switch (new->type) {
1941da177e4SLinus Torvalds 	case ACPI_STATE_C3:
1951da177e4SLinus Torvalds 		/* Enable bus master reload */
19602df8b93SVenkatesh Pallipadi 		if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
1974be44fcdSLen Brown 			acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1,
1984be44fcdSLen Brown 					  ACPI_MTX_DO_NOT_LOCK);
1991da177e4SLinus Torvalds 		break;
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	pr->power.state = new;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	return;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
20764c7c8f8SNick Piggin static void acpi_safe_halt(void)
20864c7c8f8SNick Piggin {
209495ab9c0SAndi Kleen 	current_thread_info()->status &= ~TS_POLLING;
21064c7c8f8SNick Piggin 	smp_mb__after_clear_bit();
21164c7c8f8SNick Piggin 	if (!need_resched())
21264c7c8f8SNick Piggin 		safe_halt();
213495ab9c0SAndi Kleen 	current_thread_info()->status |= TS_POLLING;
21464c7c8f8SNick Piggin }
21564c7c8f8SNick Piggin 
21602df8b93SVenkatesh Pallipadi static atomic_t c3_cpu_count;
21702df8b93SVenkatesh Pallipadi 
2181da177e4SLinus Torvalds static void acpi_processor_idle(void)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	struct acpi_processor *pr = NULL;
2211da177e4SLinus Torvalds 	struct acpi_processor_cx *cx = NULL;
2221da177e4SLinus Torvalds 	struct acpi_processor_cx *next_state = NULL;
2231da177e4SLinus Torvalds 	int sleep_ticks = 0;
2241da177e4SLinus Torvalds 	u32 t1, t2 = 0;
2251da177e4SLinus Torvalds 
22664c7c8f8SNick Piggin 	pr = processors[smp_processor_id()];
2271da177e4SLinus Torvalds 	if (!pr)
2281da177e4SLinus Torvalds 		return;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	/*
2311da177e4SLinus Torvalds 	 * Interrupts must be disabled during bus mastering calculations and
2321da177e4SLinus Torvalds 	 * for C2/C3 transitions.
2331da177e4SLinus Torvalds 	 */
2341da177e4SLinus Torvalds 	local_irq_disable();
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 * Check whether we truly need to go idle, or should
2381da177e4SLinus Torvalds 	 * reschedule:
2391da177e4SLinus Torvalds 	 */
2401da177e4SLinus Torvalds 	if (unlikely(need_resched())) {
2411da177e4SLinus Torvalds 		local_irq_enable();
2421da177e4SLinus Torvalds 		return;
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	cx = pr->power.state;
24664c7c8f8SNick Piggin 	if (!cx) {
24764c7c8f8SNick Piggin 		if (pm_idle_save)
24864c7c8f8SNick Piggin 			pm_idle_save();
24964c7c8f8SNick Piggin 		else
25064c7c8f8SNick Piggin 			acpi_safe_halt();
25164c7c8f8SNick Piggin 		return;
25264c7c8f8SNick Piggin 	}
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	/*
2551da177e4SLinus Torvalds 	 * Check BM Activity
2561da177e4SLinus Torvalds 	 * -----------------
2571da177e4SLinus Torvalds 	 * Check for bus mastering activity (if required), record, and check
2581da177e4SLinus Torvalds 	 * for demotion.
2591da177e4SLinus Torvalds 	 */
2601da177e4SLinus Torvalds 	if (pr->flags.bm_check) {
2611da177e4SLinus Torvalds 		u32 bm_status = 0;
2621da177e4SLinus Torvalds 		unsigned long diff = jiffies - pr->power.bm_check_timestamp;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		if (diff > 32)
2651da177e4SLinus Torvalds 			diff = 32;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		while (diff) {
2681da177e4SLinus Torvalds 			/* if we didn't get called, assume there was busmaster activity */
2691da177e4SLinus Torvalds 			diff--;
2701da177e4SLinus Torvalds 			if (diff)
2711da177e4SLinus Torvalds 				pr->power.bm_activity |= 0x1;
2721da177e4SLinus Torvalds 			pr->power.bm_activity <<= 1;
2731da177e4SLinus Torvalds 		}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 		acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS,
2761da177e4SLinus Torvalds 				  &bm_status, ACPI_MTX_DO_NOT_LOCK);
2771da177e4SLinus Torvalds 		if (bm_status) {
2781da177e4SLinus Torvalds 			pr->power.bm_activity++;
2791da177e4SLinus Torvalds 			acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS,
2801da177e4SLinus Torvalds 					  1, ACPI_MTX_DO_NOT_LOCK);
2811da177e4SLinus Torvalds 		}
2821da177e4SLinus Torvalds 		/*
2831da177e4SLinus Torvalds 		 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
2841da177e4SLinus Torvalds 		 * the true state of bus mastering activity; forcing us to
2851da177e4SLinus Torvalds 		 * manually check the BMIDEA bit of each IDE channel.
2861da177e4SLinus Torvalds 		 */
2871da177e4SLinus Torvalds 		else if (errata.piix4.bmisx) {
2881da177e4SLinus Torvalds 			if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
2891da177e4SLinus Torvalds 			    || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
2901da177e4SLinus Torvalds 				pr->power.bm_activity++;
2911da177e4SLinus Torvalds 		}
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 		pr->power.bm_check_timestamp = jiffies;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 		/*
2961da177e4SLinus Torvalds 		 * Apply bus mastering demotion policy.  Automatically demote
2971da177e4SLinus Torvalds 		 * to avoid a faulty transition.  Note that the processor
2981da177e4SLinus Torvalds 		 * won't enter a low-power state during this call (to this
2991da177e4SLinus Torvalds 		 * funciton) but should upon the next.
3001da177e4SLinus Torvalds 		 *
3011da177e4SLinus Torvalds 		 * TBD: A better policy might be to fallback to the demotion
3021da177e4SLinus Torvalds 		 *      state (use it for this quantum only) istead of
3031da177e4SLinus Torvalds 		 *      demoting -- and rely on duration as our sole demotion
3041da177e4SLinus Torvalds 		 *      qualification.  This may, however, introduce DMA
3051da177e4SLinus Torvalds 		 *      issues (e.g. floppy DMA transfer overrun/underrun).
3061da177e4SLinus Torvalds 		 */
3071da177e4SLinus Torvalds 		if (pr->power.bm_activity & cx->demotion.threshold.bm) {
3081da177e4SLinus Torvalds 			local_irq_enable();
3091da177e4SLinus Torvalds 			next_state = cx->demotion.state;
3101da177e4SLinus Torvalds 			goto end;
3111da177e4SLinus Torvalds 		}
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 
3144c033552SVenkatesh Pallipadi #ifdef CONFIG_HOTPLUG_CPU
3154c033552SVenkatesh Pallipadi 	/*
3164c033552SVenkatesh Pallipadi 	 * Check for P_LVL2_UP flag before entering C2 and above on
3174c033552SVenkatesh Pallipadi 	 * an SMP system. We do it here instead of doing it at _CST/P_LVL
3184c033552SVenkatesh Pallipadi 	 * detection phase, to work cleanly with logical CPU hotplug.
3194c033552SVenkatesh Pallipadi 	 */
3204c033552SVenkatesh Pallipadi 	if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
3211e483969SDavid Shaohua Li 	    !pr->flags.has_cst && !acpi_fadt.plvl2_up)
3221e483969SDavid Shaohua Li 		cx = &pr->power.states[ACPI_STATE_C1];
3234c033552SVenkatesh Pallipadi #endif
3241e483969SDavid Shaohua Li 
3251e483969SDavid Shaohua Li 	cx->usage++;
3261e483969SDavid Shaohua Li 
3271da177e4SLinus Torvalds 	/*
3281da177e4SLinus Torvalds 	 * Sleep:
3291da177e4SLinus Torvalds 	 * ------
3301da177e4SLinus Torvalds 	 * Invoke the current Cx state to put the processor to sleep.
3311da177e4SLinus Torvalds 	 */
3322a298a35SNick Piggin 	if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
333495ab9c0SAndi Kleen 		current_thread_info()->status &= ~TS_POLLING;
3342a298a35SNick Piggin 		smp_mb__after_clear_bit();
3352a298a35SNick Piggin 		if (need_resched()) {
336495ab9c0SAndi Kleen 			current_thread_info()->status |= TS_POLLING;
337af2eb17bSLinus Torvalds 			local_irq_enable();
3382a298a35SNick Piggin 			return;
3392a298a35SNick Piggin 		}
3402a298a35SNick Piggin 	}
3412a298a35SNick Piggin 
3421da177e4SLinus Torvalds 	switch (cx->type) {
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	case ACPI_STATE_C1:
3451da177e4SLinus Torvalds 		/*
3461da177e4SLinus Torvalds 		 * Invoke C1.
3471da177e4SLinus Torvalds 		 * Use the appropriate idle routine, the one that would
3481da177e4SLinus Torvalds 		 * be used without acpi C-states.
3491da177e4SLinus Torvalds 		 */
3501da177e4SLinus Torvalds 		if (pm_idle_save)
3511da177e4SLinus Torvalds 			pm_idle_save();
3521da177e4SLinus Torvalds 		else
35364c7c8f8SNick Piggin 			acpi_safe_halt();
35464c7c8f8SNick Piggin 
3551da177e4SLinus Torvalds 		/*
3561da177e4SLinus Torvalds 		 * TBD: Can't get time duration while in C1, as resumes
3571da177e4SLinus Torvalds 		 *      go to an ISR rather than here.  Need to instrument
3581da177e4SLinus Torvalds 		 *      base interrupt handler.
3591da177e4SLinus Torvalds 		 */
3601da177e4SLinus Torvalds 		sleep_ticks = 0xFFFFFFFF;
3611da177e4SLinus Torvalds 		break;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	case ACPI_STATE_C2:
3641da177e4SLinus Torvalds 		/* Get start time (ticks) */
3651da177e4SLinus Torvalds 		t1 = inl(acpi_fadt.xpm_tmr_blk.address);
3661da177e4SLinus Torvalds 		/* Invoke C2 */
3671da177e4SLinus Torvalds 		inb(cx->address);
368*b488f021SAndreas Mohr 		/* Dummy wait op - must do something useless after P_LVL2 read
369*b488f021SAndreas Mohr 		   because chipsets cannot guarantee that STPCLK# signal
370*b488f021SAndreas Mohr 		   gets asserted in time to freeze execution properly. */
3711da177e4SLinus Torvalds 		t2 = inl(acpi_fadt.xpm_tmr_blk.address);
3721da177e4SLinus Torvalds 		/* Get end time (ticks) */
3731da177e4SLinus Torvalds 		t2 = inl(acpi_fadt.xpm_tmr_blk.address);
374539eb11eSjohn stultz 
375539eb11eSjohn stultz #ifdef CONFIG_GENERIC_TIME
376539eb11eSjohn stultz 		/* TSC halts in C2, so notify users */
377539eb11eSjohn stultz 		mark_tsc_unstable();
378539eb11eSjohn stultz #endif
3791da177e4SLinus Torvalds 		/* Re-enable interrupts */
3801da177e4SLinus Torvalds 		local_irq_enable();
381495ab9c0SAndi Kleen 		current_thread_info()->status |= TS_POLLING;
3821da177e4SLinus Torvalds 		/* Compute time (ticks) that we were actually asleep */
3834be44fcdSLen Brown 		sleep_ticks =
3844be44fcdSLen Brown 		    ticks_elapsed(t1, t2) - cx->latency_ticks - C2_OVERHEAD;
3851da177e4SLinus Torvalds 		break;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	case ACPI_STATE_C3:
38802df8b93SVenkatesh Pallipadi 
38902df8b93SVenkatesh Pallipadi 		if (pr->flags.bm_check) {
39002df8b93SVenkatesh Pallipadi 			if (atomic_inc_return(&c3_cpu_count) ==
39102df8b93SVenkatesh Pallipadi 			    num_online_cpus()) {
39202df8b93SVenkatesh Pallipadi 				/*
39302df8b93SVenkatesh Pallipadi 				 * All CPUs are trying to go to C3
39402df8b93SVenkatesh Pallipadi 				 * Disable bus master arbitration
39502df8b93SVenkatesh Pallipadi 				 */
39602df8b93SVenkatesh Pallipadi 				acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
39702df8b93SVenkatesh Pallipadi 						  ACPI_MTX_DO_NOT_LOCK);
39802df8b93SVenkatesh Pallipadi 			}
39902df8b93SVenkatesh Pallipadi 		} else {
40002df8b93SVenkatesh Pallipadi 			/* SMP with no shared cache... Invalidate cache  */
40102df8b93SVenkatesh Pallipadi 			ACPI_FLUSH_CPU_CACHE();
40202df8b93SVenkatesh Pallipadi 		}
40302df8b93SVenkatesh Pallipadi 
4041da177e4SLinus Torvalds 		/* Get start time (ticks) */
4051da177e4SLinus Torvalds 		t1 = inl(acpi_fadt.xpm_tmr_blk.address);
4061da177e4SLinus Torvalds 		/* Invoke C3 */
4071da177e4SLinus Torvalds 		inb(cx->address);
408*b488f021SAndreas Mohr 		/* Dummy wait op (see above) */
4091da177e4SLinus Torvalds 		t2 = inl(acpi_fadt.xpm_tmr_blk.address);
4101da177e4SLinus Torvalds 		/* Get end time (ticks) */
4111da177e4SLinus Torvalds 		t2 = inl(acpi_fadt.xpm_tmr_blk.address);
41202df8b93SVenkatesh Pallipadi 		if (pr->flags.bm_check) {
4131da177e4SLinus Torvalds 			/* Enable bus master arbitration */
41402df8b93SVenkatesh Pallipadi 			atomic_dec(&c3_cpu_count);
4154be44fcdSLen Brown 			acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
4164be44fcdSLen Brown 					  ACPI_MTX_DO_NOT_LOCK);
41702df8b93SVenkatesh Pallipadi 		}
41802df8b93SVenkatesh Pallipadi 
419539eb11eSjohn stultz #ifdef CONFIG_GENERIC_TIME
420539eb11eSjohn stultz 		/* TSC halts in C3, so notify users */
421539eb11eSjohn stultz 		mark_tsc_unstable();
422539eb11eSjohn stultz #endif
4231da177e4SLinus Torvalds 		/* Re-enable interrupts */
4241da177e4SLinus Torvalds 		local_irq_enable();
425495ab9c0SAndi Kleen 		current_thread_info()->status |= TS_POLLING;
4261da177e4SLinus Torvalds 		/* Compute time (ticks) that we were actually asleep */
4274be44fcdSLen Brown 		sleep_ticks =
4284be44fcdSLen Brown 		    ticks_elapsed(t1, t2) - cx->latency_ticks - C3_OVERHEAD;
4291da177e4SLinus Torvalds 		break;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	default:
4321da177e4SLinus Torvalds 		local_irq_enable();
4331da177e4SLinus Torvalds 		return;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	next_state = pr->power.state;
4371da177e4SLinus Torvalds 
4381e483969SDavid Shaohua Li #ifdef CONFIG_HOTPLUG_CPU
4391e483969SDavid Shaohua Li 	/* Don't do promotion/demotion */
4401e483969SDavid Shaohua Li 	if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
4411e483969SDavid Shaohua Li 	    !pr->flags.has_cst && !acpi_fadt.plvl2_up) {
4421e483969SDavid Shaohua Li 		next_state = cx;
4431e483969SDavid Shaohua Li 		goto end;
4441e483969SDavid Shaohua Li 	}
4451e483969SDavid Shaohua Li #endif
4461e483969SDavid Shaohua Li 
4471da177e4SLinus Torvalds 	/*
4481da177e4SLinus Torvalds 	 * Promotion?
4491da177e4SLinus Torvalds 	 * ----------
4501da177e4SLinus Torvalds 	 * Track the number of longs (time asleep is greater than threshold)
4511da177e4SLinus Torvalds 	 * and promote when the count threshold is reached.  Note that bus
4521da177e4SLinus Torvalds 	 * mastering activity may prevent promotions.
4531da177e4SLinus Torvalds 	 * Do not promote above max_cstate.
4541da177e4SLinus Torvalds 	 */
4551da177e4SLinus Torvalds 	if (cx->promotion.state &&
4561da177e4SLinus Torvalds 	    ((cx->promotion.state - pr->power.states) <= max_cstate)) {
4571da177e4SLinus Torvalds 		if (sleep_ticks > cx->promotion.threshold.ticks) {
4581da177e4SLinus Torvalds 			cx->promotion.count++;
4591da177e4SLinus Torvalds 			cx->demotion.count = 0;
4604be44fcdSLen Brown 			if (cx->promotion.count >=
4614be44fcdSLen Brown 			    cx->promotion.threshold.count) {
4621da177e4SLinus Torvalds 				if (pr->flags.bm_check) {
4634be44fcdSLen Brown 					if (!
4644be44fcdSLen Brown 					    (pr->power.bm_activity & cx->
4654be44fcdSLen Brown 					     promotion.threshold.bm)) {
4664be44fcdSLen Brown 						next_state =
4674be44fcdSLen Brown 						    cx->promotion.state;
4681da177e4SLinus Torvalds 						goto end;
4691da177e4SLinus Torvalds 					}
4704be44fcdSLen Brown 				} else {
4711da177e4SLinus Torvalds 					next_state = cx->promotion.state;
4721da177e4SLinus Torvalds 					goto end;
4731da177e4SLinus Torvalds 				}
4741da177e4SLinus Torvalds 			}
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	/*
4791da177e4SLinus Torvalds 	 * Demotion?
4801da177e4SLinus Torvalds 	 * ---------
4811da177e4SLinus Torvalds 	 * Track the number of shorts (time asleep is less than time threshold)
4821da177e4SLinus Torvalds 	 * and demote when the usage threshold is reached.
4831da177e4SLinus Torvalds 	 */
4841da177e4SLinus Torvalds 	if (cx->demotion.state) {
4851da177e4SLinus Torvalds 		if (sleep_ticks < cx->demotion.threshold.ticks) {
4861da177e4SLinus Torvalds 			cx->demotion.count++;
4871da177e4SLinus Torvalds 			cx->promotion.count = 0;
4881da177e4SLinus Torvalds 			if (cx->demotion.count >= cx->demotion.threshold.count) {
4891da177e4SLinus Torvalds 				next_state = cx->demotion.state;
4901da177e4SLinus Torvalds 				goto end;
4911da177e4SLinus Torvalds 			}
4921da177e4SLinus Torvalds 		}
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds       end:
4961da177e4SLinus Torvalds 	/*
4971da177e4SLinus Torvalds 	 * Demote if current state exceeds max_cstate
4981da177e4SLinus Torvalds 	 */
4991da177e4SLinus Torvalds 	if ((pr->power.state - pr->power.states) > max_cstate) {
5001da177e4SLinus Torvalds 		if (cx->demotion.state)
5011da177e4SLinus Torvalds 			next_state = cx->demotion.state;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	/*
5051da177e4SLinus Torvalds 	 * New Cx State?
5061da177e4SLinus Torvalds 	 * -------------
5071da177e4SLinus Torvalds 	 * If we're going to start using a new Cx state we must clean up
5081da177e4SLinus Torvalds 	 * from the previous and prepare to use the new.
5091da177e4SLinus Torvalds 	 */
5101da177e4SLinus Torvalds 	if (next_state != pr->power.state)
5111da177e4SLinus Torvalds 		acpi_processor_power_activate(pr, next_state);
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5144be44fcdSLen Brown static int acpi_processor_set_power_policy(struct acpi_processor *pr)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	unsigned int i;
5171da177e4SLinus Torvalds 	unsigned int state_is_set = 0;
5181da177e4SLinus Torvalds 	struct acpi_processor_cx *lower = NULL;
5191da177e4SLinus Torvalds 	struct acpi_processor_cx *higher = NULL;
5201da177e4SLinus Torvalds 	struct acpi_processor_cx *cx;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	if (!pr)
524d550d98dSPatrick Mochel 		return -EINVAL;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	/*
5271da177e4SLinus Torvalds 	 * This function sets the default Cx state policy (OS idle handler).
5281da177e4SLinus Torvalds 	 * Our scheme is to promote quickly to C2 but more conservatively
5291da177e4SLinus Torvalds 	 * to C3.  We're favoring C2  for its characteristics of low latency
5301da177e4SLinus Torvalds 	 * (quick response), good power savings, and ability to allow bus
5311da177e4SLinus Torvalds 	 * mastering activity.  Note that the Cx state policy is completely
5321da177e4SLinus Torvalds 	 * customizable and can be altered dynamically.
5331da177e4SLinus Torvalds 	 */
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	/* startup state */
5361da177e4SLinus Torvalds 	for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
5371da177e4SLinus Torvalds 		cx = &pr->power.states[i];
5381da177e4SLinus Torvalds 		if (!cx->valid)
5391da177e4SLinus Torvalds 			continue;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 		if (!state_is_set)
5421da177e4SLinus Torvalds 			pr->power.state = cx;
5431da177e4SLinus Torvalds 		state_is_set++;
5441da177e4SLinus Torvalds 		break;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	if (!state_is_set)
548d550d98dSPatrick Mochel 		return -ENODEV;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	/* demotion */
5511da177e4SLinus Torvalds 	for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
5521da177e4SLinus Torvalds 		cx = &pr->power.states[i];
5531da177e4SLinus Torvalds 		if (!cx->valid)
5541da177e4SLinus Torvalds 			continue;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 		if (lower) {
5571da177e4SLinus Torvalds 			cx->demotion.state = lower;
5581da177e4SLinus Torvalds 			cx->demotion.threshold.ticks = cx->latency_ticks;
5591da177e4SLinus Torvalds 			cx->demotion.threshold.count = 1;
5601da177e4SLinus Torvalds 			if (cx->type == ACPI_STATE_C3)
5611da177e4SLinus Torvalds 				cx->demotion.threshold.bm = bm_history;
5621da177e4SLinus Torvalds 		}
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 		lower = cx;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	/* promotion */
5681da177e4SLinus Torvalds 	for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
5691da177e4SLinus Torvalds 		cx = &pr->power.states[i];
5701da177e4SLinus Torvalds 		if (!cx->valid)
5711da177e4SLinus Torvalds 			continue;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 		if (higher) {
5741da177e4SLinus Torvalds 			cx->promotion.state = higher;
5751da177e4SLinus Torvalds 			cx->promotion.threshold.ticks = cx->latency_ticks;
5761da177e4SLinus Torvalds 			if (cx->type >= ACPI_STATE_C2)
5771da177e4SLinus Torvalds 				cx->promotion.threshold.count = 4;
5781da177e4SLinus Torvalds 			else
5791da177e4SLinus Torvalds 				cx->promotion.threshold.count = 10;
5801da177e4SLinus Torvalds 			if (higher->type == ACPI_STATE_C3)
5811da177e4SLinus Torvalds 				cx->promotion.threshold.bm = bm_history;
5821da177e4SLinus Torvalds 		}
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 		higher = cx;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 
587d550d98dSPatrick Mochel 	return 0;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	if (!pr)
594d550d98dSPatrick Mochel 		return -EINVAL;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	if (!pr->pblk)
597d550d98dSPatrick Mochel 		return -ENODEV;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	/* if info is obtained from pblk/fadt, type equals state */
6001da177e4SLinus Torvalds 	pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
6011da177e4SLinus Torvalds 	pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
6021da177e4SLinus Torvalds 
6034c033552SVenkatesh Pallipadi #ifndef CONFIG_HOTPLUG_CPU
6044c033552SVenkatesh Pallipadi 	/*
6054c033552SVenkatesh Pallipadi 	 * Check for P_LVL2_UP flag before entering C2 and above on
6064c033552SVenkatesh Pallipadi 	 * an SMP system.
6074c033552SVenkatesh Pallipadi 	 */
6081e483969SDavid Shaohua Li 	if ((num_online_cpus() > 1) && !acpi_fadt.plvl2_up)
609d550d98dSPatrick Mochel 		return -ENODEV;
6104c033552SVenkatesh Pallipadi #endif
6114c033552SVenkatesh Pallipadi 
6121da177e4SLinus Torvalds 	/* determine C2 and C3 address from pblk */
6131da177e4SLinus Torvalds 	pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
6141da177e4SLinus Torvalds 	pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	/* determine latencies from FADT */
6171da177e4SLinus Torvalds 	pr->power.states[ACPI_STATE_C2].latency = acpi_fadt.plvl2_lat;
6181da177e4SLinus Torvalds 	pr->power.states[ACPI_STATE_C3].latency = acpi_fadt.plvl3_lat;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
6211da177e4SLinus Torvalds 			  "lvl2[0x%08x] lvl3[0x%08x]\n",
6221da177e4SLinus Torvalds 			  pr->power.states[ACPI_STATE_C2].address,
6231da177e4SLinus Torvalds 			  pr->power.states[ACPI_STATE_C3].address));
6241da177e4SLinus Torvalds 
625d550d98dSPatrick Mochel 	return 0;
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
628acf05f4bSVenkatesh Pallipadi static int acpi_processor_get_power_info_default_c1(struct acpi_processor *pr)
629acf05f4bSVenkatesh Pallipadi {
630acf05f4bSVenkatesh Pallipadi 
631cf824788SJanosch Machowinski 	/* Zero initialize all the C-states info. */
6322203d6edSLinus Torvalds 	memset(pr->power.states, 0, sizeof(pr->power.states));
633acf05f4bSVenkatesh Pallipadi 
634cf824788SJanosch Machowinski 	/* set the first C-State to C1 */
635acf05f4bSVenkatesh Pallipadi 	pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
636acf05f4bSVenkatesh Pallipadi 
637acf05f4bSVenkatesh Pallipadi 	/* the C0 state only exists as a filler in our array,
638acf05f4bSVenkatesh Pallipadi 	 * and all processors need to support C1 */
639acf05f4bSVenkatesh Pallipadi 	pr->power.states[ACPI_STATE_C0].valid = 1;
640acf05f4bSVenkatesh Pallipadi 	pr->power.states[ACPI_STATE_C1].valid = 1;
641acf05f4bSVenkatesh Pallipadi 
642d550d98dSPatrick Mochel 	return 0;
643acf05f4bSVenkatesh Pallipadi }
644acf05f4bSVenkatesh Pallipadi 
6451da177e4SLinus Torvalds static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 	acpi_status status = 0;
6481da177e4SLinus Torvalds 	acpi_integer count;
649cf824788SJanosch Machowinski 	int current_count;
6501da177e4SLinus Torvalds 	int i;
6511da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6521da177e4SLinus Torvalds 	union acpi_object *cst;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	if (nocst)
656d550d98dSPatrick Mochel 		return -ENODEV;
6571da177e4SLinus Torvalds 
658cf824788SJanosch Machowinski 	current_count = 1;
659cf824788SJanosch Machowinski 
660cf824788SJanosch Machowinski 	/* Zero initialize C2 onwards and prepare for fresh CST lookup */
661cf824788SJanosch Machowinski 	for (i = 2; i < ACPI_PROCESSOR_MAX_POWER; i++)
6620b6b2f08SVenkatesh Pallipadi 		memset(&(pr->power.states[i]), 0,
6630b6b2f08SVenkatesh Pallipadi 				sizeof(struct acpi_processor_cx));
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
6661da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
6671da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
668d550d98dSPatrick Mochel 		return -ENODEV;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	cst = (union acpi_object *)buffer.pointer;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	/* There must be at least 2 elements */
6741da177e4SLinus Torvalds 	if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
6756468463aSLen Brown 		printk(KERN_ERR PREFIX "not enough elements in _CST\n");
6761da177e4SLinus Torvalds 		status = -EFAULT;
6771da177e4SLinus Torvalds 		goto end;
6781da177e4SLinus Torvalds 	}
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	count = cst->package.elements[0].integer.value;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	/* Validate number of power states. */
6831da177e4SLinus Torvalds 	if (count < 1 || count != cst->package.count - 1) {
6846468463aSLen Brown 		printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
6851da177e4SLinus Torvalds 		status = -EFAULT;
6861da177e4SLinus Torvalds 		goto end;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	/* Tell driver that at least _CST is supported. */
6901da177e4SLinus Torvalds 	pr->flags.has_cst = 1;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	for (i = 1; i <= count; i++) {
6931da177e4SLinus Torvalds 		union acpi_object *element;
6941da177e4SLinus Torvalds 		union acpi_object *obj;
6951da177e4SLinus Torvalds 		struct acpi_power_register *reg;
6961da177e4SLinus Torvalds 		struct acpi_processor_cx cx;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 		memset(&cx, 0, sizeof(cx));
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 		element = (union acpi_object *)&(cst->package.elements[i]);
7011da177e4SLinus Torvalds 		if (element->type != ACPI_TYPE_PACKAGE)
7021da177e4SLinus Torvalds 			continue;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 		if (element->package.count != 4)
7051da177e4SLinus Torvalds 			continue;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 		obj = (union acpi_object *)&(element->package.elements[0]);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 		if (obj->type != ACPI_TYPE_BUFFER)
7101da177e4SLinus Torvalds 			continue;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 		reg = (struct acpi_power_register *)obj->buffer.pointer;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 		if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
7151da177e4SLinus Torvalds 		    (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
7161da177e4SLinus Torvalds 			continue;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 		cx.address = (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) ?
7191da177e4SLinus Torvalds 		    0 : reg->address;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 		/* There should be an easy way to extract an integer... */
7221da177e4SLinus Torvalds 		obj = (union acpi_object *)&(element->package.elements[1]);
7231da177e4SLinus Torvalds 		if (obj->type != ACPI_TYPE_INTEGER)
7241da177e4SLinus Torvalds 			continue;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		cx.type = obj->integer.value;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 		if ((cx.type != ACPI_STATE_C1) &&
7291da177e4SLinus Torvalds 		    (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO))
7301da177e4SLinus Torvalds 			continue;
7311da177e4SLinus Torvalds 
732cf824788SJanosch Machowinski 		if ((cx.type < ACPI_STATE_C2) || (cx.type > ACPI_STATE_C3))
7331da177e4SLinus Torvalds 			continue;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 		obj = (union acpi_object *)&(element->package.elements[2]);
7361da177e4SLinus Torvalds 		if (obj->type != ACPI_TYPE_INTEGER)
7371da177e4SLinus Torvalds 			continue;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 		cx.latency = obj->integer.value;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 		obj = (union acpi_object *)&(element->package.elements[3]);
7421da177e4SLinus Torvalds 		if (obj->type != ACPI_TYPE_INTEGER)
7431da177e4SLinus Torvalds 			continue;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 		cx.power = obj->integer.value;
7461da177e4SLinus Torvalds 
747cf824788SJanosch Machowinski 		current_count++;
748cf824788SJanosch Machowinski 		memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
749cf824788SJanosch Machowinski 
750cf824788SJanosch Machowinski 		/*
751cf824788SJanosch Machowinski 		 * We support total ACPI_PROCESSOR_MAX_POWER - 1
752cf824788SJanosch Machowinski 		 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
753cf824788SJanosch Machowinski 		 */
754cf824788SJanosch Machowinski 		if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
755cf824788SJanosch Machowinski 			printk(KERN_WARNING
756cf824788SJanosch Machowinski 			       "Limiting number of power states to max (%d)\n",
757cf824788SJanosch Machowinski 			       ACPI_PROCESSOR_MAX_POWER);
758cf824788SJanosch Machowinski 			printk(KERN_WARNING
759cf824788SJanosch Machowinski 			       "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
760cf824788SJanosch Machowinski 			break;
761cf824788SJanosch Machowinski 		}
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 
7644be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
765cf824788SJanosch Machowinski 			  current_count));
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/* Validate number of power states discovered */
768cf824788SJanosch Machowinski 	if (current_count < 2)
7696d93c648SVenkatesh Pallipadi 		status = -EFAULT;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds       end:
7721da177e4SLinus Torvalds 	acpi_os_free(buffer.pointer);
7731da177e4SLinus Torvalds 
774d550d98dSPatrick Mochel 	return status;
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	if (!cx->address)
781d550d98dSPatrick Mochel 		return;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/*
7841da177e4SLinus Torvalds 	 * C2 latency must be less than or equal to 100
7851da177e4SLinus Torvalds 	 * microseconds.
7861da177e4SLinus Torvalds 	 */
7871da177e4SLinus Torvalds 	else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
7881da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
7894be44fcdSLen Brown 				  "latency too large [%d]\n", cx->latency));
790d550d98dSPatrick Mochel 		return;
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	/*
7941da177e4SLinus Torvalds 	 * Otherwise we've met all of our C2 requirements.
7951da177e4SLinus Torvalds 	 * Normalize the C2 latency to expidite policy
7961da177e4SLinus Torvalds 	 */
7971da177e4SLinus Torvalds 	cx->valid = 1;
7981da177e4SLinus Torvalds 	cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
7991da177e4SLinus Torvalds 
800d550d98dSPatrick Mochel 	return;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8034be44fcdSLen Brown static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
8041da177e4SLinus Torvalds 					   struct acpi_processor_cx *cx)
8051da177e4SLinus Torvalds {
80602df8b93SVenkatesh Pallipadi 	static int bm_check_flag;
80702df8b93SVenkatesh Pallipadi 
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	if (!cx->address)
810d550d98dSPatrick Mochel 		return;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	/*
8131da177e4SLinus Torvalds 	 * C3 latency must be less than or equal to 1000
8141da177e4SLinus Torvalds 	 * microseconds.
8151da177e4SLinus Torvalds 	 */
8161da177e4SLinus Torvalds 	else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
8171da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
8184be44fcdSLen Brown 				  "latency too large [%d]\n", cx->latency));
819d550d98dSPatrick Mochel 		return;
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	/*
8231da177e4SLinus Torvalds 	 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
8241da177e4SLinus Torvalds 	 * DMA transfers are used by any ISA device to avoid livelock.
8251da177e4SLinus Torvalds 	 * Note that we could disable Type-F DMA (as recommended by
8261da177e4SLinus Torvalds 	 * the erratum), but this is known to disrupt certain ISA
8271da177e4SLinus Torvalds 	 * devices thus we take the conservative approach.
8281da177e4SLinus Torvalds 	 */
8291da177e4SLinus Torvalds 	else if (errata.piix4.fdma) {
8301da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
8311da177e4SLinus Torvalds 				  "C3 not supported on PIIX4 with Type-F DMA\n"));
832d550d98dSPatrick Mochel 		return;
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 
83502df8b93SVenkatesh Pallipadi 	/* All the logic here assumes flags.bm_check is same across all CPUs */
83602df8b93SVenkatesh Pallipadi 	if (!bm_check_flag) {
83702df8b93SVenkatesh Pallipadi 		/* Determine whether bm_check is needed based on CPU  */
83802df8b93SVenkatesh Pallipadi 		acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
83902df8b93SVenkatesh Pallipadi 		bm_check_flag = pr->flags.bm_check;
84002df8b93SVenkatesh Pallipadi 	} else {
84102df8b93SVenkatesh Pallipadi 		pr->flags.bm_check = bm_check_flag;
84202df8b93SVenkatesh Pallipadi 	}
84302df8b93SVenkatesh Pallipadi 
84402df8b93SVenkatesh Pallipadi 	if (pr->flags.bm_check) {
84502df8b93SVenkatesh Pallipadi 		/* bus mastering control is necessary */
84602df8b93SVenkatesh Pallipadi 		if (!pr->flags.bm_control) {
84702df8b93SVenkatesh Pallipadi 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
84802df8b93SVenkatesh Pallipadi 					  "C3 support requires bus mastering control\n"));
849d550d98dSPatrick Mochel 			return;
85002df8b93SVenkatesh Pallipadi 		}
85102df8b93SVenkatesh Pallipadi 	} else {
85202df8b93SVenkatesh Pallipadi 		/*
85302df8b93SVenkatesh Pallipadi 		 * WBINVD should be set in fadt, for C3 state to be
85402df8b93SVenkatesh Pallipadi 		 * supported on when bm_check is not required.
85502df8b93SVenkatesh Pallipadi 		 */
85602df8b93SVenkatesh Pallipadi 		if (acpi_fadt.wb_invd != 1) {
85702df8b93SVenkatesh Pallipadi 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
85802df8b93SVenkatesh Pallipadi 					  "Cache invalidation should work properly"
85902df8b93SVenkatesh Pallipadi 					  " for C3 to be enabled on SMP systems\n"));
860d550d98dSPatrick Mochel 			return;
86102df8b93SVenkatesh Pallipadi 		}
86202df8b93SVenkatesh Pallipadi 		acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD,
86302df8b93SVenkatesh Pallipadi 				  0, ACPI_MTX_DO_NOT_LOCK);
86402df8b93SVenkatesh Pallipadi 	}
86502df8b93SVenkatesh Pallipadi 
8661da177e4SLinus Torvalds 	/*
8671da177e4SLinus Torvalds 	 * Otherwise we've met all of our C3 requirements.
8681da177e4SLinus Torvalds 	 * Normalize the C3 latency to expidite policy.  Enable
8691da177e4SLinus Torvalds 	 * checking of bus mastering status (bm_check) so we can
8701da177e4SLinus Torvalds 	 * use this in our C3 policy
8711da177e4SLinus Torvalds 	 */
8721da177e4SLinus Torvalds 	cx->valid = 1;
8731da177e4SLinus Torvalds 	cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
8741da177e4SLinus Torvalds 
875d550d98dSPatrick Mochel 	return;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds static int acpi_processor_power_verify(struct acpi_processor *pr)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	unsigned int i;
8811da177e4SLinus Torvalds 	unsigned int working = 0;
8826eb0a0fdSVenkatesh Pallipadi 
883bd663347SAndi Kleen #ifdef ARCH_APICTIMER_STOPS_ON_C3
8840b5c59a1SAndi Kleen 	int timer_broadcast = 0;
8850b5c59a1SAndi Kleen 	cpumask_t mask = cpumask_of_cpu(pr->id);
8866eb0a0fdSVenkatesh Pallipadi 	on_each_cpu(switch_ipi_to_APIC_timer, &mask, 1, 1);
8876eb0a0fdSVenkatesh Pallipadi #endif
8886eb0a0fdSVenkatesh Pallipadi 
8891da177e4SLinus Torvalds 	for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
8901da177e4SLinus Torvalds 		struct acpi_processor_cx *cx = &pr->power.states[i];
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 		switch (cx->type) {
8931da177e4SLinus Torvalds 		case ACPI_STATE_C1:
8941da177e4SLinus Torvalds 			cx->valid = 1;
8951da177e4SLinus Torvalds 			break;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 		case ACPI_STATE_C2:
8981da177e4SLinus Torvalds 			acpi_processor_power_verify_c2(cx);
899bd663347SAndi Kleen #ifdef ARCH_APICTIMER_STOPS_ON_C3
900bd663347SAndi Kleen 			/* Some AMD systems fake C3 as C2, but still
901bd663347SAndi Kleen 			   have timer troubles */
902bd663347SAndi Kleen 			if (cx->valid &&
903bd663347SAndi Kleen 				boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
904bd663347SAndi Kleen 				timer_broadcast++;
905bd663347SAndi Kleen #endif
9061da177e4SLinus Torvalds 			break;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 		case ACPI_STATE_C3:
9091da177e4SLinus Torvalds 			acpi_processor_power_verify_c3(pr, cx);
9106eb0a0fdSVenkatesh Pallipadi #ifdef ARCH_APICTIMER_STOPS_ON_C3
911bd663347SAndi Kleen 			if (cx->valid)
912bd663347SAndi Kleen 				timer_broadcast++;
9136eb0a0fdSVenkatesh Pallipadi #endif
9141da177e4SLinus Torvalds 			break;
9151da177e4SLinus Torvalds 		}
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		if (cx->valid)
9181da177e4SLinus Torvalds 			working++;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
9210b5c59a1SAndi Kleen #ifdef ARCH_APICTIMER_STOPS_ON_C3
922bd663347SAndi Kleen 	if (timer_broadcast)
923bd663347SAndi Kleen 		on_each_cpu(switch_APIC_timer_to_ipi, &mask, 1, 1);
9240b5c59a1SAndi Kleen #endif
925bd663347SAndi Kleen 
9261da177e4SLinus Torvalds 	return (working);
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9294be44fcdSLen Brown static int acpi_processor_get_power_info(struct acpi_processor *pr)
9301da177e4SLinus Torvalds {
9311da177e4SLinus Torvalds 	unsigned int i;
9321da177e4SLinus Torvalds 	int result;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	/* NOTE: the idle thread may not be running while calling
9361da177e4SLinus Torvalds 	 * this function */
9371da177e4SLinus Torvalds 
938cf824788SJanosch Machowinski 	/* Adding C1 state */
939cf824788SJanosch Machowinski 	acpi_processor_get_power_info_default_c1(pr);
9401da177e4SLinus Torvalds 	result = acpi_processor_get_power_info_cst(pr);
9416d93c648SVenkatesh Pallipadi 	if (result == -ENODEV)
942cf824788SJanosch Machowinski 		acpi_processor_get_power_info_fadt(pr);
9436d93c648SVenkatesh Pallipadi 
944cf824788SJanosch Machowinski 	pr->power.count = acpi_processor_power_verify(pr);
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	/*
9471da177e4SLinus Torvalds 	 * Set Default Policy
9481da177e4SLinus Torvalds 	 * ------------------
9491da177e4SLinus Torvalds 	 * Now that we know which states are supported, set the default
9501da177e4SLinus Torvalds 	 * policy.  Note that this policy can be changed dynamically
9511da177e4SLinus Torvalds 	 * (e.g. encourage deeper sleeps to conserve battery life when
9521da177e4SLinus Torvalds 	 * not on AC).
9531da177e4SLinus Torvalds 	 */
9541da177e4SLinus Torvalds 	result = acpi_processor_set_power_policy(pr);
9551da177e4SLinus Torvalds 	if (result)
956d550d98dSPatrick Mochel 		return result;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	/*
9591da177e4SLinus Torvalds 	 * if one state of type C2 or C3 is available, mark this
9601da177e4SLinus Torvalds 	 * CPU as being "idle manageable"
9611da177e4SLinus Torvalds 	 */
9621da177e4SLinus Torvalds 	for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
963acf05f4bSVenkatesh Pallipadi 		if (pr->power.states[i].valid) {
9641da177e4SLinus Torvalds 			pr->power.count = i;
9652203d6edSLinus Torvalds 			if (pr->power.states[i].type >= ACPI_STATE_C2)
9661da177e4SLinus Torvalds 				pr->flags.power = 1;
9671da177e4SLinus Torvalds 		}
968acf05f4bSVenkatesh Pallipadi 	}
9691da177e4SLinus Torvalds 
970d550d98dSPatrick Mochel 	return 0;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds int acpi_processor_cst_has_changed(struct acpi_processor *pr)
9741da177e4SLinus Torvalds {
9751da177e4SLinus Torvalds 	int result = 0;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	if (!pr)
979d550d98dSPatrick Mochel 		return -EINVAL;
9801da177e4SLinus Torvalds 
98102df8b93SVenkatesh Pallipadi 	if (nocst) {
982d550d98dSPatrick Mochel 		return -ENODEV;
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	if (!pr->flags.power_setup_done)
986d550d98dSPatrick Mochel 		return -ENODEV;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	/* Fall back to the default idle loop */
9891da177e4SLinus Torvalds 	pm_idle = pm_idle_save;
990fbd568a3SPaul E. McKenney 	synchronize_sched();	/* Relies on interrupts forcing exit from idle. */
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	pr->flags.power = 0;
9931da177e4SLinus Torvalds 	result = acpi_processor_get_power_info(pr);
9941da177e4SLinus Torvalds 	if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
9951da177e4SLinus Torvalds 		pm_idle = acpi_processor_idle;
9961da177e4SLinus Torvalds 
997d550d98dSPatrick Mochel 	return result;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds /* proc interface */
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
10031da177e4SLinus Torvalds {
10041da177e4SLinus Torvalds 	struct acpi_processor *pr = (struct acpi_processor *)seq->private;
10051da177e4SLinus Torvalds 	unsigned int i;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	if (!pr)
10091da177e4SLinus Torvalds 		goto end;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	seq_printf(seq, "active state:            C%zd\n"
10121da177e4SLinus Torvalds 		   "max_cstate:              C%d\n"
10131da177e4SLinus Torvalds 		   "bus master activity:     %08x\n",
10141da177e4SLinus Torvalds 		   pr->power.state ? pr->power.state - pr->power.states : 0,
10154be44fcdSLen Brown 		   max_cstate, (unsigned)pr->power.bm_activity);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	seq_puts(seq, "states:\n");
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	for (i = 1; i <= pr->power.count; i++) {
10201da177e4SLinus Torvalds 		seq_printf(seq, "   %cC%d:                  ",
10214be44fcdSLen Brown 			   (&pr->power.states[i] ==
10224be44fcdSLen Brown 			    pr->power.state ? '*' : ' '), i);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 		if (!pr->power.states[i].valid) {
10251da177e4SLinus Torvalds 			seq_puts(seq, "<not supported>\n");
10261da177e4SLinus Torvalds 			continue;
10271da177e4SLinus Torvalds 		}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 		switch (pr->power.states[i].type) {
10301da177e4SLinus Torvalds 		case ACPI_STATE_C1:
10311da177e4SLinus Torvalds 			seq_printf(seq, "type[C1] ");
10321da177e4SLinus Torvalds 			break;
10331da177e4SLinus Torvalds 		case ACPI_STATE_C2:
10341da177e4SLinus Torvalds 			seq_printf(seq, "type[C2] ");
10351da177e4SLinus Torvalds 			break;
10361da177e4SLinus Torvalds 		case ACPI_STATE_C3:
10371da177e4SLinus Torvalds 			seq_printf(seq, "type[C3] ");
10381da177e4SLinus Torvalds 			break;
10391da177e4SLinus Torvalds 		default:
10401da177e4SLinus Torvalds 			seq_printf(seq, "type[--] ");
10411da177e4SLinus Torvalds 			break;
10421da177e4SLinus Torvalds 		}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 		if (pr->power.states[i].promotion.state)
10451da177e4SLinus Torvalds 			seq_printf(seq, "promotion[C%zd] ",
10461da177e4SLinus Torvalds 				   (pr->power.states[i].promotion.state -
10471da177e4SLinus Torvalds 				    pr->power.states));
10481da177e4SLinus Torvalds 		else
10491da177e4SLinus Torvalds 			seq_puts(seq, "promotion[--] ");
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 		if (pr->power.states[i].demotion.state)
10521da177e4SLinus Torvalds 			seq_printf(seq, "demotion[C%zd] ",
10531da177e4SLinus Torvalds 				   (pr->power.states[i].demotion.state -
10541da177e4SLinus Torvalds 				    pr->power.states));
10551da177e4SLinus Torvalds 		else
10561da177e4SLinus Torvalds 			seq_puts(seq, "demotion[--] ");
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 		seq_printf(seq, "latency[%03d] usage[%08d]\n",
10591da177e4SLinus Torvalds 			   pr->power.states[i].latency,
10601da177e4SLinus Torvalds 			   pr->power.states[i].usage);
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds       end:
1064d550d98dSPatrick Mochel 	return 0;
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
10681da177e4SLinus Torvalds {
10691da177e4SLinus Torvalds 	return single_open(file, acpi_processor_power_seq_show,
10701da177e4SLinus Torvalds 			   PDE(inode)->data);
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds static struct file_operations acpi_processor_power_fops = {
10741da177e4SLinus Torvalds 	.open = acpi_processor_power_open_fs,
10751da177e4SLinus Torvalds 	.read = seq_read,
10761da177e4SLinus Torvalds 	.llseek = seq_lseek,
10771da177e4SLinus Torvalds 	.release = single_release,
10781da177e4SLinus Torvalds };
10791da177e4SLinus Torvalds 
10804be44fcdSLen Brown int acpi_processor_power_init(struct acpi_processor *pr,
10814be44fcdSLen Brown 			      struct acpi_device *device)
10821da177e4SLinus Torvalds {
10831da177e4SLinus Torvalds 	acpi_status status = 0;
1084b6835052SAndreas Mohr 	static int first_run;
10851da177e4SLinus Torvalds 	struct proc_dir_entry *entry = NULL;
10861da177e4SLinus Torvalds 	unsigned int i;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	if (!first_run) {
10901da177e4SLinus Torvalds 		dmi_check_system(processor_power_dmi_table);
10911da177e4SLinus Torvalds 		if (max_cstate < ACPI_C_STATES_MAX)
10924be44fcdSLen Brown 			printk(KERN_NOTICE
10934be44fcdSLen Brown 			       "ACPI: processor limited to max C-state %d\n",
10944be44fcdSLen Brown 			       max_cstate);
10951da177e4SLinus Torvalds 		first_run++;
10961da177e4SLinus Torvalds 	}
10971da177e4SLinus Torvalds 
109802df8b93SVenkatesh Pallipadi 	if (!pr)
1099d550d98dSPatrick Mochel 		return -EINVAL;
110002df8b93SVenkatesh Pallipadi 
110102df8b93SVenkatesh Pallipadi 	if (acpi_fadt.cst_cnt && !nocst) {
11024be44fcdSLen Brown 		status =
11034be44fcdSLen Brown 		    acpi_os_write_port(acpi_fadt.smi_cmd, acpi_fadt.cst_cnt, 8);
11041da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
1105a6fc6720SThomas Renninger 			ACPI_EXCEPTION((AE_INFO, status,
1106a6fc6720SThomas Renninger 					"Notifying BIOS of _CST ability failed"));
11071da177e4SLinus Torvalds 		}
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	acpi_processor_get_power_info(pr);
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	/*
11131da177e4SLinus Torvalds 	 * Install the idle handler if processor power management is supported.
11141da177e4SLinus Torvalds 	 * Note that we use previously set idle handler will be used on
11151da177e4SLinus Torvalds 	 * platforms that only support C1.
11161da177e4SLinus Torvalds 	 */
11171da177e4SLinus Torvalds 	if ((pr->flags.power) && (!boot_option_idle_override)) {
11181da177e4SLinus Torvalds 		printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
11191da177e4SLinus Torvalds 		for (i = 1; i <= pr->power.count; i++)
11201da177e4SLinus Torvalds 			if (pr->power.states[i].valid)
11214be44fcdSLen Brown 				printk(" C%d[C%d]", i,
11224be44fcdSLen Brown 				       pr->power.states[i].type);
11231da177e4SLinus Torvalds 		printk(")\n");
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 		if (pr->id == 0) {
11261da177e4SLinus Torvalds 			pm_idle_save = pm_idle;
11271da177e4SLinus Torvalds 			pm_idle = acpi_processor_idle;
11281da177e4SLinus Torvalds 		}
11291da177e4SLinus Torvalds 	}
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	/* 'power' [R] */
11321da177e4SLinus Torvalds 	entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
11331da177e4SLinus Torvalds 				  S_IRUGO, acpi_device_dir(device));
11341da177e4SLinus Torvalds 	if (!entry)
1135a6fc6720SThomas Renninger 		return -EIO;
11361da177e4SLinus Torvalds 	else {
11371da177e4SLinus Torvalds 		entry->proc_fops = &acpi_processor_power_fops;
11381da177e4SLinus Torvalds 		entry->data = acpi_driver_data(device);
11391da177e4SLinus Torvalds 		entry->owner = THIS_MODULE;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	pr->flags.power_setup_done = 1;
11431da177e4SLinus Torvalds 
1144d550d98dSPatrick Mochel 	return 0;
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
11474be44fcdSLen Brown int acpi_processor_power_exit(struct acpi_processor *pr,
11484be44fcdSLen Brown 			      struct acpi_device *device)
11491da177e4SLinus Torvalds {
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	pr->flags.power_setup_done = 0;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	if (acpi_device_dir(device))
11544be44fcdSLen Brown 		remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
11554be44fcdSLen Brown 				  acpi_device_dir(device));
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	/* Unregister the idle handler when processor #0 is removed. */
11581da177e4SLinus Torvalds 	if (pr->id == 0) {
11591da177e4SLinus Torvalds 		pm_idle = pm_idle_save;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 		/*
11621da177e4SLinus Torvalds 		 * We are about to unload the current idle thread pm callback
11631da177e4SLinus Torvalds 		 * (pm_idle), Wait for all processors to update cached/local
11641da177e4SLinus Torvalds 		 * copies of pm_idle before proceeding.
11651da177e4SLinus Torvalds 		 */
11661da177e4SLinus Torvalds 		cpu_idle_wait();
11671da177e4SLinus Torvalds 	}
11681da177e4SLinus Torvalds 
1169d550d98dSPatrick Mochel 	return 0;
11701da177e4SLinus Torvalds }
1171