processor_idle.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /*
  2. * processor_idle - idle state submodule to the ACPI processor driver
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
  7. * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  8. * - Added processor hotplug support
  9. * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  10. * - Added support for C3 on SMP
  11. *
  12. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or (at
  17. * your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  27. *
  28. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/cpufreq.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/acpi.h>
  37. #include <linux/dmi.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/sched.h> /* need_resched() */
  40. #include <asm/io.h>
  41. #include <asm/uaccess.h>
  42. #include <acpi/acpi_bus.h>
  43. #include <acpi/processor.h>
  44. #define ACPI_PROCESSOR_COMPONENT 0x01000000
  45. #define ACPI_PROCESSOR_CLASS "processor"
  46. #define ACPI_PROCESSOR_DRIVER_NAME "ACPI Processor Driver"
  47. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  48. ACPI_MODULE_NAME("acpi_processor")
  49. #define ACPI_PROCESSOR_FILE_POWER "power"
  50. #define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
  51. #define C2_OVERHEAD 4 /* 1us (3.579 ticks per us) */
  52. #define C3_OVERHEAD 4 /* 1us (3.579 ticks per us) */
  53. static void (*pm_idle_save) (void) __read_mostly;
  54. module_param(max_cstate, uint, 0644);
  55. static unsigned int nocst __read_mostly;
  56. module_param(nocst, uint, 0000);
  57. /*
  58. * bm_history -- bit-mask with a bit per jiffy of bus-master activity
  59. * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
  60. * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
  61. * 100 HZ: 0x0000000F: 4 jiffies = 40ms
  62. * reduce history for more aggressive entry into C3
  63. */
  64. static unsigned int bm_history __read_mostly =
  65. (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
  66. module_param(bm_history, uint, 0644);
  67. /* --------------------------------------------------------------------------
  68. Power Management
  69. -------------------------------------------------------------------------- */
  70. /*
  71. * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
  72. * For now disable this. Probably a bug somewhere else.
  73. *
  74. * To skip this limit, boot/load with a large max_cstate limit.
  75. */
  76. static int set_max_cstate(struct dmi_system_id *id)
  77. {
  78. if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
  79. return 0;
  80. printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
  81. " Override with \"processor.max_cstate=%d\"\n", id->ident,
  82. (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
  83. max_cstate = (long)id->driver_data;
  84. return 0;
  85. }
  86. /* Actually this shouldn't be __cpuinitdata, would be better to fix the
  87. callers to only run once -AK */
  88. static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
  89. { set_max_cstate, "IBM ThinkPad R40e", {
  90. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  91. DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
  92. { set_max_cstate, "IBM ThinkPad R40e", {
  93. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  94. DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
  95. { set_max_cstate, "IBM ThinkPad R40e", {
  96. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  97. DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
  98. { set_max_cstate, "IBM ThinkPad R40e", {
  99. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  100. DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
  101. { set_max_cstate, "IBM ThinkPad R40e", {
  102. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  103. DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
  104. { set_max_cstate, "IBM ThinkPad R40e", {
  105. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  106. DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
  107. { set_max_cstate, "IBM ThinkPad R40e", {
  108. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  109. DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
  110. { set_max_cstate, "IBM ThinkPad R40e", {
  111. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  112. DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
  113. { set_max_cstate, "IBM ThinkPad R40e", {
  114. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  115. DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
  116. { set_max_cstate, "IBM ThinkPad R40e", {
  117. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  118. DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
  119. { set_max_cstate, "IBM ThinkPad R40e", {
  120. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  121. DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
  122. { set_max_cstate, "IBM ThinkPad R40e", {
  123. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  124. DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
  125. { set_max_cstate, "IBM ThinkPad R40e", {
  126. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  127. DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
  128. { set_max_cstate, "IBM ThinkPad R40e", {
  129. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  130. DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
  131. { set_max_cstate, "IBM ThinkPad R40e", {
  132. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  133. DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
  134. { set_max_cstate, "Medion 41700", {
  135. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  136. DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
  137. { set_max_cstate, "Clevo 5600D", {
  138. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  139. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  140. (void *)2},
  141. {},
  142. };
  143. static inline u32 ticks_elapsed(u32 t1, u32 t2)
  144. {
  145. if (t2 >= t1)
  146. return (t2 - t1);
  147. else if (!acpi_fadt.tmr_val_ext)
  148. return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
  149. else
  150. return ((0xFFFFFFFF - t1) + t2);
  151. }
  152. static void
  153. acpi_processor_power_activate(struct acpi_processor *pr,
  154. struct acpi_processor_cx *new)
  155. {
  156. struct acpi_processor_cx *old;
  157. if (!pr || !new)
  158. return;
  159. old = pr->power.state;
  160. if (old)
  161. old->promotion.count = 0;
  162. new->demotion.count = 0;
  163. /* Cleanup from old state. */
  164. if (old) {
  165. switch (old->type) {
  166. case ACPI_STATE_C3:
  167. /* Disable bus master reload */
  168. if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
  169. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
  170. ACPI_MTX_DO_NOT_LOCK);
  171. break;
  172. }
  173. }
  174. /* Prepare to use new state. */
  175. switch (new->type) {
  176. case ACPI_STATE_C3:
  177. /* Enable bus master reload */
  178. if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
  179. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1,
  180. ACPI_MTX_DO_NOT_LOCK);
  181. break;
  182. }
  183. pr->power.state = new;
  184. return;
  185. }
  186. static void acpi_safe_halt(void)
  187. {
  188. current_thread_info()->status &= ~TS_POLLING;
  189. smp_mb__after_clear_bit();
  190. if (!need_resched())
  191. safe_halt();
  192. current_thread_info()->status |= TS_POLLING;
  193. }
  194. static atomic_t c3_cpu_count;
  195. static void acpi_processor_idle(void)
  196. {
  197. struct acpi_processor *pr = NULL;
  198. struct acpi_processor_cx *cx = NULL;
  199. struct acpi_processor_cx *next_state = NULL;
  200. int sleep_ticks = 0;
  201. u32 t1, t2 = 0;
  202. pr = processors[smp_processor_id()];
  203. if (!pr)
  204. return;
  205. /*
  206. * Interrupts must be disabled during bus mastering calculations and
  207. * for C2/C3 transitions.
  208. */
  209. local_irq_disable();
  210. /*
  211. * Check whether we truly need to go idle, or should
  212. * reschedule:
  213. */
  214. if (unlikely(need_resched())) {
  215. local_irq_enable();
  216. return;
  217. }
  218. cx = pr->power.state;
  219. if (!cx) {
  220. if (pm_idle_save)
  221. pm_idle_save();
  222. else
  223. acpi_safe_halt();
  224. return;
  225. }
  226. /*
  227. * Check BM Activity
  228. * -----------------
  229. * Check for bus mastering activity (if required), record, and check
  230. * for demotion.
  231. */
  232. if (pr->flags.bm_check) {
  233. u32 bm_status = 0;
  234. unsigned long diff = jiffies - pr->power.bm_check_timestamp;
  235. if (diff > 32)
  236. diff = 32;
  237. while (diff) {
  238. /* if we didn't get called, assume there was busmaster activity */
  239. diff--;
  240. if (diff)
  241. pr->power.bm_activity |= 0x1;
  242. pr->power.bm_activity <<= 1;
  243. }
  244. acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS,
  245. &bm_status, ACPI_MTX_DO_NOT_LOCK);
  246. if (bm_status) {
  247. pr->power.bm_activity++;
  248. acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS,
  249. 1, ACPI_MTX_DO_NOT_LOCK);
  250. }
  251. /*
  252. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  253. * the true state of bus mastering activity; forcing us to
  254. * manually check the BMIDEA bit of each IDE channel.
  255. */
  256. else if (errata.piix4.bmisx) {
  257. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  258. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  259. pr->power.bm_activity++;
  260. }
  261. pr->power.bm_check_timestamp = jiffies;
  262. /*
  263. * Apply bus mastering demotion policy. Automatically demote
  264. * to avoid a faulty transition. Note that the processor
  265. * won't enter a low-power state during this call (to this
  266. * funciton) but should upon the next.
  267. *
  268. * TBD: A better policy might be to fallback to the demotion
  269. * state (use it for this quantum only) istead of
  270. * demoting -- and rely on duration as our sole demotion
  271. * qualification. This may, however, introduce DMA
  272. * issues (e.g. floppy DMA transfer overrun/underrun).
  273. */
  274. if (pr->power.bm_activity & cx->demotion.threshold.bm) {
  275. local_irq_enable();
  276. next_state = cx->demotion.state;
  277. goto end;
  278. }
  279. }
  280. #ifdef CONFIG_HOTPLUG_CPU
  281. /*
  282. * Check for P_LVL2_UP flag before entering C2 and above on
  283. * an SMP system. We do it here instead of doing it at _CST/P_LVL
  284. * detection phase, to work cleanly with logical CPU hotplug.
  285. */
  286. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  287. !pr->flags.has_cst && !acpi_fadt.plvl2_up)
  288. cx = &pr->power.states[ACPI_STATE_C1];
  289. #endif
  290. cx->usage++;
  291. /*
  292. * Sleep:
  293. * ------
  294. * Invoke the current Cx state to put the processor to sleep.
  295. */
  296. if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
  297. current_thread_info()->status &= ~TS_POLLING;
  298. smp_mb__after_clear_bit();
  299. if (need_resched()) {
  300. current_thread_info()->status |= TS_POLLING;
  301. local_irq_enable();
  302. return;
  303. }
  304. }
  305. switch (cx->type) {
  306. case ACPI_STATE_C1:
  307. /*
  308. * Invoke C1.
  309. * Use the appropriate idle routine, the one that would
  310. * be used without acpi C-states.
  311. */
  312. if (pm_idle_save)
  313. pm_idle_save();
  314. else
  315. acpi_safe_halt();
  316. /*
  317. * TBD: Can't get time duration while in C1, as resumes
  318. * go to an ISR rather than here. Need to instrument
  319. * base interrupt handler.
  320. */
  321. sleep_ticks = 0xFFFFFFFF;
  322. break;
  323. case ACPI_STATE_C2:
  324. /* Get start time (ticks) */
  325. t1 = inl(acpi_fadt.xpm_tmr_blk.address);
  326. /* Invoke C2 */
  327. inb(cx->address);
  328. /* Dummy op - must do something useless after P_LVL2 read */
  329. t2 = inl(acpi_fadt.xpm_tmr_blk.address);
  330. /* Get end time (ticks) */
  331. t2 = inl(acpi_fadt.xpm_tmr_blk.address);
  332. #ifdef CONFIG_GENERIC_TIME
  333. /* TSC halts in C2, so notify users */
  334. mark_tsc_unstable();
  335. #endif
  336. /* Re-enable interrupts */
  337. local_irq_enable();
  338. current_thread_info()->status |= TS_POLLING;
  339. /* Compute time (ticks) that we were actually asleep */
  340. sleep_ticks =
  341. ticks_elapsed(t1, t2) - cx->latency_ticks - C2_OVERHEAD;
  342. break;
  343. case ACPI_STATE_C3:
  344. if (pr->flags.bm_check) {
  345. if (atomic_inc_return(&c3_cpu_count) ==
  346. num_online_cpus()) {
  347. /*
  348. * All CPUs are trying to go to C3
  349. * Disable bus master arbitration
  350. */
  351. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
  352. ACPI_MTX_DO_NOT_LOCK);
  353. }
  354. } else {
  355. /* SMP with no shared cache... Invalidate cache */
  356. ACPI_FLUSH_CPU_CACHE();
  357. }
  358. /* Get start time (ticks) */
  359. t1 = inl(acpi_fadt.xpm_tmr_blk.address);
  360. /* Invoke C3 */
  361. inb(cx->address);
  362. /* Dummy op - must do something useless after P_LVL3 read */
  363. t2 = inl(acpi_fadt.xpm_tmr_blk.address);
  364. /* Get end time (ticks) */
  365. t2 = inl(acpi_fadt.xpm_tmr_blk.address);
  366. if (pr->flags.bm_check) {
  367. /* Enable bus master arbitration */
  368. atomic_dec(&c3_cpu_count);
  369. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
  370. ACPI_MTX_DO_NOT_LOCK);
  371. }
  372. #ifdef CONFIG_GENERIC_TIME
  373. /* TSC halts in C3, so notify users */
  374. mark_tsc_unstable();
  375. #endif
  376. /* Re-enable interrupts */
  377. local_irq_enable();
  378. current_thread_info()->status |= TS_POLLING;
  379. /* Compute time (ticks) that we were actually asleep */
  380. sleep_ticks =
  381. ticks_elapsed(t1, t2) - cx->latency_ticks - C3_OVERHEAD;
  382. break;
  383. default:
  384. local_irq_enable();
  385. return;
  386. }
  387. next_state = pr->power.state;
  388. #ifdef CONFIG_HOTPLUG_CPU
  389. /* Don't do promotion/demotion */
  390. if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  391. !pr->flags.has_cst && !acpi_fadt.plvl2_up) {
  392. next_state = cx;
  393. goto end;
  394. }
  395. #endif
  396. /*
  397. * Promotion?
  398. * ----------
  399. * Track the number of longs (time asleep is greater than threshold)
  400. * and promote when the count threshold is reached. Note that bus
  401. * mastering activity may prevent promotions.
  402. * Do not promote above max_cstate.
  403. */
  404. if (cx->promotion.state &&
  405. ((cx->promotion.state - pr->power.states) <= max_cstate)) {
  406. if (sleep_ticks > cx->promotion.threshold.ticks) {
  407. cx->promotion.count++;
  408. cx->demotion.count = 0;
  409. if (cx->promotion.count >=
  410. cx->promotion.threshold.count) {
  411. if (pr->flags.bm_check) {
  412. if (!
  413. (pr->power.bm_activity & cx->
  414. promotion.threshold.bm)) {
  415. next_state =
  416. cx->promotion.state;
  417. goto end;
  418. }
  419. } else {
  420. next_state = cx->promotion.state;
  421. goto end;
  422. }
  423. }
  424. }
  425. }
  426. /*
  427. * Demotion?
  428. * ---------
  429. * Track the number of shorts (time asleep is less than time threshold)
  430. * and demote when the usage threshold is reached.
  431. */
  432. if (cx->demotion.state) {
  433. if (sleep_ticks < cx->demotion.threshold.ticks) {
  434. cx->demotion.count++;
  435. cx->promotion.count = 0;
  436. if (cx->demotion.count >= cx->demotion.threshold.count) {
  437. next_state = cx->demotion.state;
  438. goto end;
  439. }
  440. }
  441. }
  442. end:
  443. /*
  444. * Demote if current state exceeds max_cstate
  445. */
  446. if ((pr->power.state - pr->power.states) > max_cstate) {
  447. if (cx->demotion.state)
  448. next_state = cx->demotion.state;
  449. }
  450. /*
  451. * New Cx State?
  452. * -------------
  453. * If we're going to start using a new Cx state we must clean up
  454. * from the previous and prepare to use the new.
  455. */
  456. if (next_state != pr->power.state)
  457. acpi_processor_power_activate(pr, next_state);
  458. }
  459. static int acpi_processor_set_power_policy(struct acpi_processor *pr)
  460. {
  461. unsigned int i;
  462. unsigned int state_is_set = 0;
  463. struct acpi_processor_cx *lower = NULL;
  464. struct acpi_processor_cx *higher = NULL;
  465. struct acpi_processor_cx *cx;
  466. ACPI_FUNCTION_TRACE("acpi_processor_set_power_policy");
  467. if (!pr)
  468. return_VALUE(-EINVAL);
  469. /*
  470. * This function sets the default Cx state policy (OS idle handler).
  471. * Our scheme is to promote quickly to C2 but more conservatively
  472. * to C3. We're favoring C2 for its characteristics of low latency
  473. * (quick response), good power savings, and ability to allow bus
  474. * mastering activity. Note that the Cx state policy is completely
  475. * customizable and can be altered dynamically.
  476. */
  477. /* startup state */
  478. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  479. cx = &pr->power.states[i];
  480. if (!cx->valid)
  481. continue;
  482. if (!state_is_set)
  483. pr->power.state = cx;
  484. state_is_set++;
  485. break;
  486. }
  487. if (!state_is_set)
  488. return_VALUE(-ENODEV);
  489. /* demotion */
  490. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  491. cx = &pr->power.states[i];
  492. if (!cx->valid)
  493. continue;
  494. if (lower) {
  495. cx->demotion.state = lower;
  496. cx->demotion.threshold.ticks = cx->latency_ticks;
  497. cx->demotion.threshold.count = 1;
  498. if (cx->type == ACPI_STATE_C3)
  499. cx->demotion.threshold.bm = bm_history;
  500. }
  501. lower = cx;
  502. }
  503. /* promotion */
  504. for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
  505. cx = &pr->power.states[i];
  506. if (!cx->valid)
  507. continue;
  508. if (higher) {
  509. cx->promotion.state = higher;
  510. cx->promotion.threshold.ticks = cx->latency_ticks;
  511. if (cx->type >= ACPI_STATE_C2)
  512. cx->promotion.threshold.count = 4;
  513. else
  514. cx->promotion.threshold.count = 10;
  515. if (higher->type == ACPI_STATE_C3)
  516. cx->promotion.threshold.bm = bm_history;
  517. }
  518. higher = cx;
  519. }
  520. return_VALUE(0);
  521. }
  522. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  523. {
  524. ACPI_FUNCTION_TRACE("acpi_processor_get_power_info_fadt");
  525. if (!pr)
  526. return_VALUE(-EINVAL);
  527. if (!pr->pblk)
  528. return_VALUE(-ENODEV);
  529. /* if info is obtained from pblk/fadt, type equals state */
  530. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  531. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  532. #ifndef CONFIG_HOTPLUG_CPU
  533. /*
  534. * Check for P_LVL2_UP flag before entering C2 and above on
  535. * an SMP system.
  536. */
  537. if ((num_online_cpus() > 1) && !acpi_fadt.plvl2_up)
  538. return_VALUE(-ENODEV);
  539. #endif
  540. /* determine C2 and C3 address from pblk */
  541. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  542. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  543. /* determine latencies from FADT */
  544. pr->power.states[ACPI_STATE_C2].latency = acpi_fadt.plvl2_lat;
  545. pr->power.states[ACPI_STATE_C3].latency = acpi_fadt.plvl3_lat;
  546. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  547. "lvl2[0x%08x] lvl3[0x%08x]\n",
  548. pr->power.states[ACPI_STATE_C2].address,
  549. pr->power.states[ACPI_STATE_C3].address));
  550. return_VALUE(0);
  551. }
  552. static int acpi_processor_get_power_info_default_c1(struct acpi_processor *pr)
  553. {
  554. ACPI_FUNCTION_TRACE("acpi_processor_get_power_info_default_c1");
  555. /* Zero initialize all the C-states info. */
  556. memset(pr->power.states, 0, sizeof(pr->power.states));
  557. /* set the first C-State to C1 */
  558. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  559. /* the C0 state only exists as a filler in our array,
  560. * and all processors need to support C1 */
  561. pr->power.states[ACPI_STATE_C0].valid = 1;
  562. pr->power.states[ACPI_STATE_C1].valid = 1;
  563. return_VALUE(0);
  564. }
  565. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  566. {
  567. acpi_status status = 0;
  568. acpi_integer count;
  569. int current_count;
  570. int i;
  571. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  572. union acpi_object *cst;
  573. ACPI_FUNCTION_TRACE("acpi_processor_get_power_info_cst");
  574. if (nocst)
  575. return_VALUE(-ENODEV);
  576. current_count = 1;
  577. /* Zero initialize C2 onwards and prepare for fresh CST lookup */
  578. for (i = 2; i < ACPI_PROCESSOR_MAX_POWER; i++)
  579. memset(&(pr->power.states[i]), 0,
  580. sizeof(struct acpi_processor_cx));
  581. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  582. if (ACPI_FAILURE(status)) {
  583. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  584. return_VALUE(-ENODEV);
  585. }
  586. cst = (union acpi_object *)buffer.pointer;
  587. /* There must be at least 2 elements */
  588. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  589. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  590. "not enough elements in _CST\n"));
  591. status = -EFAULT;
  592. goto end;
  593. }
  594. count = cst->package.elements[0].integer.value;
  595. /* Validate number of power states. */
  596. if (count < 1 || count != cst->package.count - 1) {
  597. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  598. "count given by _CST is not valid\n"));
  599. status = -EFAULT;
  600. goto end;
  601. }
  602. /* Tell driver that at least _CST is supported. */
  603. pr->flags.has_cst = 1;
  604. for (i = 1; i <= count; i++) {
  605. union acpi_object *element;
  606. union acpi_object *obj;
  607. struct acpi_power_register *reg;
  608. struct acpi_processor_cx cx;
  609. memset(&cx, 0, sizeof(cx));
  610. element = (union acpi_object *)&(cst->package.elements[i]);
  611. if (element->type != ACPI_TYPE_PACKAGE)
  612. continue;
  613. if (element->package.count != 4)
  614. continue;
  615. obj = (union acpi_object *)&(element->package.elements[0]);
  616. if (obj->type != ACPI_TYPE_BUFFER)
  617. continue;
  618. reg = (struct acpi_power_register *)obj->buffer.pointer;
  619. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  620. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  621. continue;
  622. cx.address = (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) ?
  623. 0 : reg->address;
  624. /* There should be an easy way to extract an integer... */
  625. obj = (union acpi_object *)&(element->package.elements[1]);
  626. if (obj->type != ACPI_TYPE_INTEGER)
  627. continue;
  628. cx.type = obj->integer.value;
  629. if ((cx.type != ACPI_STATE_C1) &&
  630. (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO))
  631. continue;
  632. if ((cx.type < ACPI_STATE_C2) || (cx.type > ACPI_STATE_C3))
  633. continue;
  634. obj = (union acpi_object *)&(element->package.elements[2]);
  635. if (obj->type != ACPI_TYPE_INTEGER)
  636. continue;
  637. cx.latency = obj->integer.value;
  638. obj = (union acpi_object *)&(element->package.elements[3]);
  639. if (obj->type != ACPI_TYPE_INTEGER)
  640. continue;
  641. cx.power = obj->integer.value;
  642. current_count++;
  643. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  644. /*
  645. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  646. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  647. */
  648. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  649. printk(KERN_WARNING
  650. "Limiting number of power states to max (%d)\n",
  651. ACPI_PROCESSOR_MAX_POWER);
  652. printk(KERN_WARNING
  653. "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  654. break;
  655. }
  656. }
  657. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  658. current_count));
  659. /* Validate number of power states discovered */
  660. if (current_count < 2)
  661. status = -EFAULT;
  662. end:
  663. acpi_os_free(buffer.pointer);
  664. return_VALUE(status);
  665. }
  666. static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
  667. {
  668. ACPI_FUNCTION_TRACE("acpi_processor_get_power_verify_c2");
  669. if (!cx->address)
  670. return_VOID;
  671. /*
  672. * C2 latency must be less than or equal to 100
  673. * microseconds.
  674. */
  675. else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  676. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  677. "latency too large [%d]\n", cx->latency));
  678. return_VOID;
  679. }
  680. /*
  681. * Otherwise we've met all of our C2 requirements.
  682. * Normalize the C2 latency to expidite policy
  683. */
  684. cx->valid = 1;
  685. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  686. return_VOID;
  687. }
  688. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  689. struct acpi_processor_cx *cx)
  690. {
  691. static int bm_check_flag;
  692. ACPI_FUNCTION_TRACE("acpi_processor_get_power_verify_c3");
  693. if (!cx->address)
  694. return_VOID;
  695. /*
  696. * C3 latency must be less than or equal to 1000
  697. * microseconds.
  698. */
  699. else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  700. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  701. "latency too large [%d]\n", cx->latency));
  702. return_VOID;
  703. }
  704. /*
  705. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  706. * DMA transfers are used by any ISA device to avoid livelock.
  707. * Note that we could disable Type-F DMA (as recommended by
  708. * the erratum), but this is known to disrupt certain ISA
  709. * devices thus we take the conservative approach.
  710. */
  711. else if (errata.piix4.fdma) {
  712. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  713. "C3 not supported on PIIX4 with Type-F DMA\n"));
  714. return_VOID;
  715. }
  716. /* All the logic here assumes flags.bm_check is same across all CPUs */
  717. if (!bm_check_flag) {
  718. /* Determine whether bm_check is needed based on CPU */
  719. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  720. bm_check_flag = pr->flags.bm_check;
  721. } else {
  722. pr->flags.bm_check = bm_check_flag;
  723. }
  724. if (pr->flags.bm_check) {
  725. /* bus mastering control is necessary */
  726. if (!pr->flags.bm_control) {
  727. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  728. "C3 support requires bus mastering control\n"));
  729. return_VOID;
  730. }
  731. } else {
  732. /*
  733. * WBINVD should be set in fadt, for C3 state to be
  734. * supported on when bm_check is not required.
  735. */
  736. if (acpi_fadt.wb_invd != 1) {
  737. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  738. "Cache invalidation should work properly"
  739. " for C3 to be enabled on SMP systems\n"));
  740. return_VOID;
  741. }
  742. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD,
  743. 0, ACPI_MTX_DO_NOT_LOCK);
  744. }
  745. /*
  746. * Otherwise we've met all of our C3 requirements.
  747. * Normalize the C3 latency to expidite policy. Enable
  748. * checking of bus mastering status (bm_check) so we can
  749. * use this in our C3 policy
  750. */
  751. cx->valid = 1;
  752. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  753. return_VOID;
  754. }
  755. static int acpi_processor_power_verify(struct acpi_processor *pr)
  756. {
  757. unsigned int i;
  758. unsigned int working = 0;
  759. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  760. int timer_broadcast = 0;
  761. cpumask_t mask = cpumask_of_cpu(pr->id);
  762. on_each_cpu(switch_ipi_to_APIC_timer, &mask, 1, 1);
  763. #endif
  764. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  765. struct acpi_processor_cx *cx = &pr->power.states[i];
  766. switch (cx->type) {
  767. case ACPI_STATE_C1:
  768. cx->valid = 1;
  769. break;
  770. case ACPI_STATE_C2:
  771. acpi_processor_power_verify_c2(cx);
  772. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  773. /* Some AMD systems fake C3 as C2, but still
  774. have timer troubles */
  775. if (cx->valid &&
  776. boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  777. timer_broadcast++;
  778. #endif
  779. break;
  780. case ACPI_STATE_C3:
  781. acpi_processor_power_verify_c3(pr, cx);
  782. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  783. if (cx->valid)
  784. timer_broadcast++;
  785. #endif
  786. break;
  787. }
  788. if (cx->valid)
  789. working++;
  790. }
  791. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  792. if (timer_broadcast)
  793. on_each_cpu(switch_APIC_timer_to_ipi, &mask, 1, 1);
  794. #endif
  795. return (working);
  796. }
  797. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  798. {
  799. unsigned int i;
  800. int result;
  801. ACPI_FUNCTION_TRACE("acpi_processor_get_power_info");
  802. /* NOTE: the idle thread may not be running while calling
  803. * this function */
  804. /* Adding C1 state */
  805. acpi_processor_get_power_info_default_c1(pr);
  806. result = acpi_processor_get_power_info_cst(pr);
  807. if (result == -ENODEV)
  808. acpi_processor_get_power_info_fadt(pr);
  809. pr->power.count = acpi_processor_power_verify(pr);
  810. /*
  811. * Set Default Policy
  812. * ------------------
  813. * Now that we know which states are supported, set the default
  814. * policy. Note that this policy can be changed dynamically
  815. * (e.g. encourage deeper sleeps to conserve battery life when
  816. * not on AC).
  817. */
  818. result = acpi_processor_set_power_policy(pr);
  819. if (result)
  820. return_VALUE(result);
  821. /*
  822. * if one state of type C2 or C3 is available, mark this
  823. * CPU as being "idle manageable"
  824. */
  825. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  826. if (pr->power.states[i].valid) {
  827. pr->power.count = i;
  828. if (pr->power.states[i].type >= ACPI_STATE_C2)
  829. pr->flags.power = 1;
  830. }
  831. }
  832. return_VALUE(0);
  833. }
  834. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  835. {
  836. int result = 0;
  837. ACPI_FUNCTION_TRACE("acpi_processor_cst_has_changed");
  838. if (!pr)
  839. return_VALUE(-EINVAL);
  840. if (nocst) {
  841. return_VALUE(-ENODEV);
  842. }
  843. if (!pr->flags.power_setup_done)
  844. return_VALUE(-ENODEV);
  845. /* Fall back to the default idle loop */
  846. pm_idle = pm_idle_save;
  847. synchronize_sched(); /* Relies on interrupts forcing exit from idle. */
  848. pr->flags.power = 0;
  849. result = acpi_processor_get_power_info(pr);
  850. if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
  851. pm_idle = acpi_processor_idle;
  852. return_VALUE(result);
  853. }
  854. /* proc interface */
  855. static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
  856. {
  857. struct acpi_processor *pr = (struct acpi_processor *)seq->private;
  858. unsigned int i;
  859. ACPI_FUNCTION_TRACE("acpi_processor_power_seq_show");
  860. if (!pr)
  861. goto end;
  862. seq_printf(seq, "active state: C%zd\n"
  863. "max_cstate: C%d\n"
  864. "bus master activity: %08x\n",
  865. pr->power.state ? pr->power.state - pr->power.states : 0,
  866. max_cstate, (unsigned)pr->power.bm_activity);
  867. seq_puts(seq, "states:\n");
  868. for (i = 1; i <= pr->power.count; i++) {
  869. seq_printf(seq, " %cC%d: ",
  870. (&pr->power.states[i] ==
  871. pr->power.state ? '*' : ' '), i);
  872. if (!pr->power.states[i].valid) {
  873. seq_puts(seq, "<not supported>\n");
  874. continue;
  875. }
  876. switch (pr->power.states[i].type) {
  877. case ACPI_STATE_C1:
  878. seq_printf(seq, "type[C1] ");
  879. break;
  880. case ACPI_STATE_C2:
  881. seq_printf(seq, "type[C2] ");
  882. break;
  883. case ACPI_STATE_C3:
  884. seq_printf(seq, "type[C3] ");
  885. break;
  886. default:
  887. seq_printf(seq, "type[--] ");
  888. break;
  889. }
  890. if (pr->power.states[i].promotion.state)
  891. seq_printf(seq, "promotion[C%zd] ",
  892. (pr->power.states[i].promotion.state -
  893. pr->power.states));
  894. else
  895. seq_puts(seq, "promotion[--] ");
  896. if (pr->power.states[i].demotion.state)
  897. seq_printf(seq, "demotion[C%zd] ",
  898. (pr->power.states[i].demotion.state -
  899. pr->power.states));
  900. else
  901. seq_puts(seq, "demotion[--] ");
  902. seq_printf(seq, "latency[%03d] usage[%08d]\n",
  903. pr->power.states[i].latency,
  904. pr->power.states[i].usage);
  905. }
  906. end:
  907. return_VALUE(0);
  908. }
  909. static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
  910. {
  911. return single_open(file, acpi_processor_power_seq_show,
  912. PDE(inode)->data);
  913. }
  914. static struct file_operations acpi_processor_power_fops = {
  915. .open = acpi_processor_power_open_fs,
  916. .read = seq_read,
  917. .llseek = seq_lseek,
  918. .release = single_release,
  919. };
  920. int acpi_processor_power_init(struct acpi_processor *pr,
  921. struct acpi_device *device)
  922. {
  923. acpi_status status = 0;
  924. static int first_run;
  925. struct proc_dir_entry *entry = NULL;
  926. unsigned int i;
  927. ACPI_FUNCTION_TRACE("acpi_processor_power_init");
  928. if (!first_run) {
  929. dmi_check_system(processor_power_dmi_table);
  930. if (max_cstate < ACPI_C_STATES_MAX)
  931. printk(KERN_NOTICE
  932. "ACPI: processor limited to max C-state %d\n",
  933. max_cstate);
  934. first_run++;
  935. }
  936. if (!pr)
  937. return_VALUE(-EINVAL);
  938. if (acpi_fadt.cst_cnt && !nocst) {
  939. status =
  940. acpi_os_write_port(acpi_fadt.smi_cmd, acpi_fadt.cst_cnt, 8);
  941. if (ACPI_FAILURE(status)) {
  942. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  943. "Notifying BIOS of _CST ability failed\n"));
  944. }
  945. }
  946. acpi_processor_get_power_info(pr);
  947. /*
  948. * Install the idle handler if processor power management is supported.
  949. * Note that we use previously set idle handler will be used on
  950. * platforms that only support C1.
  951. */
  952. if ((pr->flags.power) && (!boot_option_idle_override)) {
  953. printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
  954. for (i = 1; i <= pr->power.count; i++)
  955. if (pr->power.states[i].valid)
  956. printk(" C%d[C%d]", i,
  957. pr->power.states[i].type);
  958. printk(")\n");
  959. if (pr->id == 0) {
  960. pm_idle_save = pm_idle;
  961. pm_idle = acpi_processor_idle;
  962. }
  963. }
  964. /* 'power' [R] */
  965. entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  966. S_IRUGO, acpi_device_dir(device));
  967. if (!entry)
  968. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  969. "Unable to create '%s' fs entry\n",
  970. ACPI_PROCESSOR_FILE_POWER));
  971. else {
  972. entry->proc_fops = &acpi_processor_power_fops;
  973. entry->data = acpi_driver_data(device);
  974. entry->owner = THIS_MODULE;
  975. }
  976. pr->flags.power_setup_done = 1;
  977. return_VALUE(0);
  978. }
  979. int acpi_processor_power_exit(struct acpi_processor *pr,
  980. struct acpi_device *device)
  981. {
  982. ACPI_FUNCTION_TRACE("acpi_processor_power_exit");
  983. pr->flags.power_setup_done = 0;
  984. if (acpi_device_dir(device))
  985. remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  986. acpi_device_dir(device));
  987. /* Unregister the idle handler when processor #0 is removed. */
  988. if (pr->id == 0) {
  989. pm_idle = pm_idle_save;
  990. /*
  991. * We are about to unload the current idle thread pm callback
  992. * (pm_idle), Wait for all processors to update cached/local
  993. * copies of pm_idle before proceeding.
  994. */
  995. cpu_idle_wait();
  996. }
  997. return_VALUE(0);
  998. }