processor_idle.c 36 KB

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