processor_idle.c 28 KB

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