processor_idle.c 30 KB

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