processor_idle.c 32 KB

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