processor_idle.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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(const 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. /*
  289. * Suspend / resume control
  290. */
  291. static int acpi_idle_suspend;
  292. int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
  293. {
  294. acpi_idle_suspend = 1;
  295. return 0;
  296. }
  297. int acpi_processor_resume(struct acpi_device * device)
  298. {
  299. acpi_idle_suspend = 0;
  300. return 0;
  301. }
  302. static void acpi_processor_idle(void)
  303. {
  304. struct acpi_processor *pr = NULL;
  305. struct acpi_processor_cx *cx = NULL;
  306. struct acpi_processor_cx *next_state = NULL;
  307. int sleep_ticks = 0;
  308. u32 t1, t2 = 0;
  309. /*
  310. * Interrupts must be disabled during bus mastering calculations and
  311. * for C2/C3 transitions.
  312. */
  313. local_irq_disable();
  314. pr = processors[smp_processor_id()];
  315. if (!pr) {
  316. local_irq_enable();
  317. return;
  318. }
  319. /*
  320. * Check whether we truly need to go idle, or should
  321. * reschedule:
  322. */
  323. if (unlikely(need_resched())) {
  324. local_irq_enable();
  325. return;
  326. }
  327. cx = pr->power.state;
  328. if (!cx || acpi_idle_suspend) {
  329. if (pm_idle_save)
  330. pm_idle_save();
  331. else
  332. acpi_safe_halt();
  333. return;
  334. }
  335. /*
  336. * Check BM Activity
  337. * -----------------
  338. * Check for bus mastering activity (if required), record, and check
  339. * for demotion.
  340. */
  341. if (pr->flags.bm_check) {
  342. u32 bm_status = 0;
  343. unsigned long diff = jiffies - pr->power.bm_check_timestamp;
  344. if (diff > 31)
  345. diff = 31;
  346. pr->power.bm_activity <<= diff;
  347. acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  348. if (bm_status) {
  349. pr->power.bm_activity |= 0x1;
  350. acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  351. }
  352. /*
  353. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  354. * the true state of bus mastering activity; forcing us to
  355. * manually check the BMIDEA bit of each IDE channel.
  356. */
  357. else if (errata.piix4.bmisx) {
  358. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  359. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  360. pr->power.bm_activity |= 0x1;
  361. }
  362. pr->power.bm_check_timestamp = jiffies;
  363. /*
  364. * If bus mastering is or was active this jiffy, demote
  365. * to avoid a faulty transition. Note that the processor
  366. * won't enter a low-power state during this call (to this
  367. * function) but should upon the next.
  368. *
  369. * TBD: A better policy might be to fallback to the demotion
  370. * state (use it for this quantum only) istead of
  371. * demoting -- and rely on duration as our sole demotion
  372. * qualification. This may, however, introduce DMA
  373. * issues (e.g. floppy DMA transfer overrun/underrun).
  374. */
  375. if ((pr->power.bm_activity & 0x1) &&
  376. cx->demotion.threshold.bm) {
  377. local_irq_enable();
  378. next_state = cx->demotion.state;
  379. goto end;
  380. }
  381. }
  382. #ifdef CONFIG_HOTPLUG_CPU
  383. /*
  384. * Check for P_LVL2_UP flag before entering C2 and above on
  385. * an SMP system. We do it here instead of doing it at _CST/P_LVL
  386. * detection phase, to work cleanly with logical CPU hotplug.
  387. */
  388. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  389. !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  390. cx = &pr->power.states[ACPI_STATE_C1];
  391. #endif
  392. /*
  393. * Sleep:
  394. * ------
  395. * Invoke the current Cx state to put the processor to sleep.
  396. */
  397. if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
  398. current_thread_info()->status &= ~TS_POLLING;
  399. /*
  400. * TS_POLLING-cleared state must be visible before we
  401. * test NEED_RESCHED:
  402. */
  403. smp_mb();
  404. if (need_resched()) {
  405. current_thread_info()->status |= TS_POLLING;
  406. local_irq_enable();
  407. return;
  408. }
  409. }
  410. switch (cx->type) {
  411. case ACPI_STATE_C1:
  412. /*
  413. * Invoke C1.
  414. * Use the appropriate idle routine, the one that would
  415. * be used without acpi C-states.
  416. */
  417. if (pm_idle_save)
  418. pm_idle_save();
  419. else
  420. acpi_safe_halt();
  421. /*
  422. * TBD: Can't get time duration while in C1, as resumes
  423. * go to an ISR rather than here. Need to instrument
  424. * base interrupt handler.
  425. *
  426. * Note: the TSC better not stop in C1, sched_clock() will
  427. * skew otherwise.
  428. */
  429. sleep_ticks = 0xFFFFFFFF;
  430. break;
  431. case ACPI_STATE_C2:
  432. /* Get start time (ticks) */
  433. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  434. /* Tell the scheduler that we are going deep-idle: */
  435. sched_clock_idle_sleep_event();
  436. /* Invoke C2 */
  437. acpi_state_timer_broadcast(pr, cx, 1);
  438. acpi_cstate_enter(cx);
  439. /* Get end time (ticks) */
  440. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  441. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
  442. /* TSC halts in C2, so notify users */
  443. mark_tsc_unstable("possible TSC halt in C2");
  444. #endif
  445. /* Compute time (ticks) that we were actually asleep */
  446. sleep_ticks = ticks_elapsed(t1, t2);
  447. /* Tell the scheduler how much we idled: */
  448. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  449. /* Re-enable interrupts */
  450. local_irq_enable();
  451. /* Do not account our idle-switching overhead: */
  452. sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
  453. current_thread_info()->status |= TS_POLLING;
  454. acpi_state_timer_broadcast(pr, cx, 0);
  455. break;
  456. case ACPI_STATE_C3:
  457. /*
  458. * disable bus master
  459. * bm_check implies we need ARB_DIS
  460. * !bm_check implies we need cache flush
  461. * bm_control implies whether we can do ARB_DIS
  462. *
  463. * That leaves a case where bm_check is set and bm_control is
  464. * not set. In that case we cannot do much, we enter C3
  465. * without doing anything.
  466. */
  467. if (pr->flags.bm_check && pr->flags.bm_control) {
  468. if (atomic_inc_return(&c3_cpu_count) ==
  469. num_online_cpus()) {
  470. /*
  471. * All CPUs are trying to go to C3
  472. * Disable bus master arbitration
  473. */
  474. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
  475. }
  476. } else if (!pr->flags.bm_check) {
  477. /* SMP with no shared cache... Invalidate cache */
  478. ACPI_FLUSH_CPU_CACHE();
  479. }
  480. /* Get start time (ticks) */
  481. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  482. /* Invoke C3 */
  483. acpi_state_timer_broadcast(pr, cx, 1);
  484. /* Tell the scheduler that we are going deep-idle: */
  485. sched_clock_idle_sleep_event();
  486. acpi_cstate_enter(cx);
  487. /* Get end time (ticks) */
  488. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  489. if (pr->flags.bm_check && pr->flags.bm_control) {
  490. /* Enable bus master arbitration */
  491. atomic_dec(&c3_cpu_count);
  492. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
  493. }
  494. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
  495. /* TSC halts in C3, so notify users */
  496. mark_tsc_unstable("TSC halts in C3");
  497. #endif
  498. /* Compute time (ticks) that we were actually asleep */
  499. sleep_ticks = ticks_elapsed(t1, t2);
  500. /* Tell the scheduler how much we idled: */
  501. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  502. /* Re-enable interrupts */
  503. local_irq_enable();
  504. /* Do not account our idle-switching overhead: */
  505. sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
  506. current_thread_info()->status |= TS_POLLING;
  507. acpi_state_timer_broadcast(pr, cx, 0);
  508. break;
  509. default:
  510. local_irq_enable();
  511. return;
  512. }
  513. cx->usage++;
  514. if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
  515. cx->time += sleep_ticks;
  516. next_state = pr->power.state;
  517. #ifdef CONFIG_HOTPLUG_CPU
  518. /* Don't do promotion/demotion */
  519. if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  520. !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
  521. next_state = cx;
  522. goto end;
  523. }
  524. #endif
  525. /*
  526. * Promotion?
  527. * ----------
  528. * Track the number of longs (time asleep is greater than threshold)
  529. * and promote when the count threshold is reached. Note that bus
  530. * mastering activity may prevent promotions.
  531. * Do not promote above max_cstate.
  532. */
  533. if (cx->promotion.state &&
  534. ((cx->promotion.state - pr->power.states) <= max_cstate)) {
  535. if (sleep_ticks > cx->promotion.threshold.ticks &&
  536. cx->promotion.state->latency <= system_latency_constraint()) {
  537. cx->promotion.count++;
  538. cx->demotion.count = 0;
  539. if (cx->promotion.count >=
  540. cx->promotion.threshold.count) {
  541. if (pr->flags.bm_check) {
  542. if (!
  543. (pr->power.bm_activity & cx->
  544. promotion.threshold.bm)) {
  545. next_state =
  546. cx->promotion.state;
  547. goto end;
  548. }
  549. } else {
  550. next_state = cx->promotion.state;
  551. goto end;
  552. }
  553. }
  554. }
  555. }
  556. /*
  557. * Demotion?
  558. * ---------
  559. * Track the number of shorts (time asleep is less than time threshold)
  560. * and demote when the usage threshold is reached.
  561. */
  562. if (cx->demotion.state) {
  563. if (sleep_ticks < cx->demotion.threshold.ticks) {
  564. cx->demotion.count++;
  565. cx->promotion.count = 0;
  566. if (cx->demotion.count >= cx->demotion.threshold.count) {
  567. next_state = cx->demotion.state;
  568. goto end;
  569. }
  570. }
  571. }
  572. end:
  573. /*
  574. * Demote if current state exceeds max_cstate
  575. * or if the latency of the current state is unacceptable
  576. */
  577. if ((pr->power.state - pr->power.states) > max_cstate ||
  578. pr->power.state->latency > system_latency_constraint()) {
  579. if (cx->demotion.state)
  580. next_state = cx->demotion.state;
  581. }
  582. /*
  583. * New Cx State?
  584. * -------------
  585. * If we're going to start using a new Cx state we must clean up
  586. * from the previous and prepare to use the new.
  587. */
  588. if (next_state != pr->power.state)
  589. acpi_processor_power_activate(pr, next_state);
  590. }
  591. static int acpi_processor_set_power_policy(struct acpi_processor *pr)
  592. {
  593. unsigned int i;
  594. unsigned int state_is_set = 0;
  595. struct acpi_processor_cx *lower = NULL;
  596. struct acpi_processor_cx *higher = NULL;
  597. struct acpi_processor_cx *cx;
  598. if (!pr)
  599. return -EINVAL;
  600. /*
  601. * This function sets the default Cx state policy (OS idle handler).
  602. * Our scheme is to promote quickly to C2 but more conservatively
  603. * to C3. We're favoring C2 for its characteristics of low latency
  604. * (quick response), good power savings, and ability to allow bus
  605. * mastering activity. Note that the Cx state policy is completely
  606. * customizable and can be altered dynamically.
  607. */
  608. /* startup state */
  609. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  610. cx = &pr->power.states[i];
  611. if (!cx->valid)
  612. continue;
  613. if (!state_is_set)
  614. pr->power.state = cx;
  615. state_is_set++;
  616. break;
  617. }
  618. if (!state_is_set)
  619. return -ENODEV;
  620. /* demotion */
  621. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  622. cx = &pr->power.states[i];
  623. if (!cx->valid)
  624. continue;
  625. if (lower) {
  626. cx->demotion.state = lower;
  627. cx->demotion.threshold.ticks = cx->latency_ticks;
  628. cx->demotion.threshold.count = 1;
  629. if (cx->type == ACPI_STATE_C3)
  630. cx->demotion.threshold.bm = bm_history;
  631. }
  632. lower = cx;
  633. }
  634. /* promotion */
  635. for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
  636. cx = &pr->power.states[i];
  637. if (!cx->valid)
  638. continue;
  639. if (higher) {
  640. cx->promotion.state = higher;
  641. cx->promotion.threshold.ticks = cx->latency_ticks;
  642. if (cx->type >= ACPI_STATE_C2)
  643. cx->promotion.threshold.count = 4;
  644. else
  645. cx->promotion.threshold.count = 10;
  646. if (higher->type == ACPI_STATE_C3)
  647. cx->promotion.threshold.bm = bm_history;
  648. }
  649. higher = cx;
  650. }
  651. return 0;
  652. }
  653. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  654. {
  655. if (!pr)
  656. return -EINVAL;
  657. if (!pr->pblk)
  658. return -ENODEV;
  659. /* if info is obtained from pblk/fadt, type equals state */
  660. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  661. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  662. #ifndef CONFIG_HOTPLUG_CPU
  663. /*
  664. * Check for P_LVL2_UP flag before entering C2 and above on
  665. * an SMP system.
  666. */
  667. if ((num_online_cpus() > 1) &&
  668. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  669. return -ENODEV;
  670. #endif
  671. /* determine C2 and C3 address from pblk */
  672. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  673. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  674. /* determine latencies from FADT */
  675. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
  676. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
  677. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  678. "lvl2[0x%08x] lvl3[0x%08x]\n",
  679. pr->power.states[ACPI_STATE_C2].address,
  680. pr->power.states[ACPI_STATE_C3].address));
  681. return 0;
  682. }
  683. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  684. {
  685. if (!pr->power.states[ACPI_STATE_C1].valid) {
  686. /* set the first C-State to C1 */
  687. /* all processors need to support C1 */
  688. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  689. pr->power.states[ACPI_STATE_C1].valid = 1;
  690. }
  691. /* the C0 state only exists as a filler in our array */
  692. pr->power.states[ACPI_STATE_C0].valid = 1;
  693. return 0;
  694. }
  695. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  696. {
  697. acpi_status status = 0;
  698. acpi_integer count;
  699. int current_count;
  700. int i;
  701. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  702. union acpi_object *cst;
  703. if (nocst)
  704. return -ENODEV;
  705. current_count = 0;
  706. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  707. if (ACPI_FAILURE(status)) {
  708. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  709. return -ENODEV;
  710. }
  711. cst = buffer.pointer;
  712. /* There must be at least 2 elements */
  713. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  714. printk(KERN_ERR PREFIX "not enough elements in _CST\n");
  715. status = -EFAULT;
  716. goto end;
  717. }
  718. count = cst->package.elements[0].integer.value;
  719. /* Validate number of power states. */
  720. if (count < 1 || count != cst->package.count - 1) {
  721. printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
  722. status = -EFAULT;
  723. goto end;
  724. }
  725. /* Tell driver that at least _CST is supported. */
  726. pr->flags.has_cst = 1;
  727. for (i = 1; i <= count; i++) {
  728. union acpi_object *element;
  729. union acpi_object *obj;
  730. struct acpi_power_register *reg;
  731. struct acpi_processor_cx cx;
  732. memset(&cx, 0, sizeof(cx));
  733. element = &(cst->package.elements[i]);
  734. if (element->type != ACPI_TYPE_PACKAGE)
  735. continue;
  736. if (element->package.count != 4)
  737. continue;
  738. obj = &(element->package.elements[0]);
  739. if (obj->type != ACPI_TYPE_BUFFER)
  740. continue;
  741. reg = (struct acpi_power_register *)obj->buffer.pointer;
  742. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  743. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  744. continue;
  745. /* There should be an easy way to extract an integer... */
  746. obj = &(element->package.elements[1]);
  747. if (obj->type != ACPI_TYPE_INTEGER)
  748. continue;
  749. cx.type = obj->integer.value;
  750. /*
  751. * Some buggy BIOSes won't list C1 in _CST -
  752. * Let acpi_processor_get_power_info_default() handle them later
  753. */
  754. if (i == 1 && cx.type != ACPI_STATE_C1)
  755. current_count++;
  756. cx.address = reg->address;
  757. cx.index = current_count + 1;
  758. cx.space_id = ACPI_CSTATE_SYSTEMIO;
  759. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  760. if (acpi_processor_ffh_cstate_probe
  761. (pr->id, &cx, reg) == 0) {
  762. cx.space_id = ACPI_CSTATE_FFH;
  763. } else if (cx.type != ACPI_STATE_C1) {
  764. /*
  765. * C1 is a special case where FIXED_HARDWARE
  766. * can be handled in non-MWAIT way as well.
  767. * In that case, save this _CST entry info.
  768. * That is, we retain space_id of SYSTEM_IO for
  769. * halt based C1.
  770. * Otherwise, ignore this info and continue.
  771. */
  772. continue;
  773. }
  774. }
  775. obj = &(element->package.elements[2]);
  776. if (obj->type != ACPI_TYPE_INTEGER)
  777. continue;
  778. cx.latency = obj->integer.value;
  779. obj = &(element->package.elements[3]);
  780. if (obj->type != ACPI_TYPE_INTEGER)
  781. continue;
  782. cx.power = obj->integer.value;
  783. current_count++;
  784. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  785. /*
  786. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  787. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  788. */
  789. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  790. printk(KERN_WARNING
  791. "Limiting number of power states to max (%d)\n",
  792. ACPI_PROCESSOR_MAX_POWER);
  793. printk(KERN_WARNING
  794. "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  795. break;
  796. }
  797. }
  798. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  799. current_count));
  800. /* Validate number of power states discovered */
  801. if (current_count < 2)
  802. status = -EFAULT;
  803. end:
  804. kfree(buffer.pointer);
  805. return status;
  806. }
  807. static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
  808. {
  809. if (!cx->address)
  810. return;
  811. /*
  812. * C2 latency must be less than or equal to 100
  813. * microseconds.
  814. */
  815. else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  816. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  817. "latency too large [%d]\n", cx->latency));
  818. return;
  819. }
  820. /*
  821. * Otherwise we've met all of our C2 requirements.
  822. * Normalize the C2 latency to expidite policy
  823. */
  824. cx->valid = 1;
  825. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  826. return;
  827. }
  828. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  829. struct acpi_processor_cx *cx)
  830. {
  831. static int bm_check_flag;
  832. if (!cx->address)
  833. return;
  834. /*
  835. * C3 latency must be less than or equal to 1000
  836. * microseconds.
  837. */
  838. else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  839. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  840. "latency too large [%d]\n", cx->latency));
  841. return;
  842. }
  843. /*
  844. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  845. * DMA transfers are used by any ISA device to avoid livelock.
  846. * Note that we could disable Type-F DMA (as recommended by
  847. * the erratum), but this is known to disrupt certain ISA
  848. * devices thus we take the conservative approach.
  849. */
  850. else if (errata.piix4.fdma) {
  851. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  852. "C3 not supported on PIIX4 with Type-F DMA\n"));
  853. return;
  854. }
  855. /* All the logic here assumes flags.bm_check is same across all CPUs */
  856. if (!bm_check_flag) {
  857. /* Determine whether bm_check is needed based on CPU */
  858. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  859. bm_check_flag = pr->flags.bm_check;
  860. } else {
  861. pr->flags.bm_check = bm_check_flag;
  862. }
  863. if (pr->flags.bm_check) {
  864. if (!pr->flags.bm_control) {
  865. if (pr->flags.has_cst != 1) {
  866. /* bus mastering control is necessary */
  867. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  868. "C3 support requires BM control\n"));
  869. return;
  870. } else {
  871. /* Here we enter C3 without bus mastering */
  872. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  873. "C3 support without BM control\n"));
  874. }
  875. }
  876. } else {
  877. /*
  878. * WBINVD should be set in fadt, for C3 state to be
  879. * supported on when bm_check is not required.
  880. */
  881. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  882. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  883. "Cache invalidation should work properly"
  884. " for C3 to be enabled on SMP systems\n"));
  885. return;
  886. }
  887. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  888. }
  889. /*
  890. * Otherwise we've met all of our C3 requirements.
  891. * Normalize the C3 latency to expidite policy. Enable
  892. * checking of bus mastering status (bm_check) so we can
  893. * use this in our C3 policy
  894. */
  895. cx->valid = 1;
  896. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  897. return;
  898. }
  899. static int acpi_processor_power_verify(struct acpi_processor *pr)
  900. {
  901. unsigned int i;
  902. unsigned int working = 0;
  903. pr->power.timer_broadcast_on_state = INT_MAX;
  904. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  905. struct acpi_processor_cx *cx = &pr->power.states[i];
  906. switch (cx->type) {
  907. case ACPI_STATE_C1:
  908. cx->valid = 1;
  909. break;
  910. case ACPI_STATE_C2:
  911. acpi_processor_power_verify_c2(cx);
  912. if (cx->valid)
  913. acpi_timer_check_state(i, pr, cx);
  914. break;
  915. case ACPI_STATE_C3:
  916. acpi_processor_power_verify_c3(pr, cx);
  917. if (cx->valid)
  918. acpi_timer_check_state(i, pr, cx);
  919. break;
  920. }
  921. if (cx->valid)
  922. working++;
  923. }
  924. acpi_propagate_timer_broadcast(pr);
  925. return (working);
  926. }
  927. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  928. {
  929. unsigned int i;
  930. int result;
  931. /* NOTE: the idle thread may not be running while calling
  932. * this function */
  933. /* Zero initialize all the C-states info. */
  934. memset(pr->power.states, 0, sizeof(pr->power.states));
  935. result = acpi_processor_get_power_info_cst(pr);
  936. if (result == -ENODEV)
  937. result = acpi_processor_get_power_info_fadt(pr);
  938. if (result)
  939. return result;
  940. acpi_processor_get_power_info_default(pr);
  941. pr->power.count = acpi_processor_power_verify(pr);
  942. /*
  943. * Set Default Policy
  944. * ------------------
  945. * Now that we know which states are supported, set the default
  946. * policy. Note that this policy can be changed dynamically
  947. * (e.g. encourage deeper sleeps to conserve battery life when
  948. * not on AC).
  949. */
  950. result = acpi_processor_set_power_policy(pr);
  951. if (result)
  952. return result;
  953. /*
  954. * if one state of type C2 or C3 is available, mark this
  955. * CPU as being "idle manageable"
  956. */
  957. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  958. if (pr->power.states[i].valid) {
  959. pr->power.count = i;
  960. if (pr->power.states[i].type >= ACPI_STATE_C2)
  961. pr->flags.power = 1;
  962. }
  963. }
  964. return 0;
  965. }
  966. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  967. {
  968. int result = 0;
  969. if (!pr)
  970. return -EINVAL;
  971. if (nocst) {
  972. return -ENODEV;
  973. }
  974. if (!pr->flags.power_setup_done)
  975. return -ENODEV;
  976. /* Fall back to the default idle loop */
  977. pm_idle = pm_idle_save;
  978. synchronize_sched(); /* Relies on interrupts forcing exit from idle. */
  979. pr->flags.power = 0;
  980. result = acpi_processor_get_power_info(pr);
  981. if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
  982. pm_idle = acpi_processor_idle;
  983. return result;
  984. }
  985. /* proc interface */
  986. static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
  987. {
  988. struct acpi_processor *pr = seq->private;
  989. unsigned int i;
  990. if (!pr)
  991. goto end;
  992. seq_printf(seq, "active state: C%zd\n"
  993. "max_cstate: C%d\n"
  994. "bus master activity: %08x\n"
  995. "maximum allowed latency: %d usec\n",
  996. pr->power.state ? pr->power.state - pr->power.states : 0,
  997. max_cstate, (unsigned)pr->power.bm_activity,
  998. system_latency_constraint());
  999. seq_puts(seq, "states:\n");
  1000. for (i = 1; i <= pr->power.count; i++) {
  1001. seq_printf(seq, " %cC%d: ",
  1002. (&pr->power.states[i] ==
  1003. pr->power.state ? '*' : ' '), i);
  1004. if (!pr->power.states[i].valid) {
  1005. seq_puts(seq, "<not supported>\n");
  1006. continue;
  1007. }
  1008. switch (pr->power.states[i].type) {
  1009. case ACPI_STATE_C1:
  1010. seq_printf(seq, "type[C1] ");
  1011. break;
  1012. case ACPI_STATE_C2:
  1013. seq_printf(seq, "type[C2] ");
  1014. break;
  1015. case ACPI_STATE_C3:
  1016. seq_printf(seq, "type[C3] ");
  1017. break;
  1018. default:
  1019. seq_printf(seq, "type[--] ");
  1020. break;
  1021. }
  1022. if (pr->power.states[i].promotion.state)
  1023. seq_printf(seq, "promotion[C%zd] ",
  1024. (pr->power.states[i].promotion.state -
  1025. pr->power.states));
  1026. else
  1027. seq_puts(seq, "promotion[--] ");
  1028. if (pr->power.states[i].demotion.state)
  1029. seq_printf(seq, "demotion[C%zd] ",
  1030. (pr->power.states[i].demotion.state -
  1031. pr->power.states));
  1032. else
  1033. seq_puts(seq, "demotion[--] ");
  1034. seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
  1035. pr->power.states[i].latency,
  1036. pr->power.states[i].usage,
  1037. (unsigned long long)pr->power.states[i].time);
  1038. }
  1039. end:
  1040. return 0;
  1041. }
  1042. static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
  1043. {
  1044. return single_open(file, acpi_processor_power_seq_show,
  1045. PDE(inode)->data);
  1046. }
  1047. static const struct file_operations acpi_processor_power_fops = {
  1048. .open = acpi_processor_power_open_fs,
  1049. .read = seq_read,
  1050. .llseek = seq_lseek,
  1051. .release = single_release,
  1052. };
  1053. #ifdef CONFIG_SMP
  1054. static void smp_callback(void *v)
  1055. {
  1056. /* we already woke the CPU up, nothing more to do */
  1057. }
  1058. /*
  1059. * This function gets called when a part of the kernel has a new latency
  1060. * requirement. This means we need to get all processors out of their C-state,
  1061. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  1062. * wakes them all right up.
  1063. */
  1064. static int acpi_processor_latency_notify(struct notifier_block *b,
  1065. unsigned long l, void *v)
  1066. {
  1067. smp_call_function(smp_callback, NULL, 0, 1);
  1068. return NOTIFY_OK;
  1069. }
  1070. static struct notifier_block acpi_processor_latency_notifier = {
  1071. .notifier_call = acpi_processor_latency_notify,
  1072. };
  1073. #endif
  1074. int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
  1075. struct acpi_device *device)
  1076. {
  1077. acpi_status status = 0;
  1078. static int first_run;
  1079. struct proc_dir_entry *entry = NULL;
  1080. unsigned int i;
  1081. if (!first_run) {
  1082. dmi_check_system(processor_power_dmi_table);
  1083. if (max_cstate < ACPI_C_STATES_MAX)
  1084. printk(KERN_NOTICE
  1085. "ACPI: processor limited to max C-state %d\n",
  1086. max_cstate);
  1087. first_run++;
  1088. #ifdef CONFIG_SMP
  1089. register_latency_notifier(&acpi_processor_latency_notifier);
  1090. #endif
  1091. }
  1092. if (!pr)
  1093. return -EINVAL;
  1094. if (acpi_gbl_FADT.cst_control && !nocst) {
  1095. status =
  1096. acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
  1097. if (ACPI_FAILURE(status)) {
  1098. ACPI_EXCEPTION((AE_INFO, status,
  1099. "Notifying BIOS of _CST ability failed"));
  1100. }
  1101. }
  1102. acpi_processor_get_power_info(pr);
  1103. /*
  1104. * Install the idle handler if processor power management is supported.
  1105. * Note that we use previously set idle handler will be used on
  1106. * platforms that only support C1.
  1107. */
  1108. if ((pr->flags.power) && (!boot_option_idle_override)) {
  1109. printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
  1110. for (i = 1; i <= pr->power.count; i++)
  1111. if (pr->power.states[i].valid)
  1112. printk(" C%d[C%d]", i,
  1113. pr->power.states[i].type);
  1114. printk(")\n");
  1115. if (pr->id == 0) {
  1116. pm_idle_save = pm_idle;
  1117. pm_idle = acpi_processor_idle;
  1118. }
  1119. }
  1120. /* 'power' [R] */
  1121. entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1122. S_IRUGO, acpi_device_dir(device));
  1123. if (!entry)
  1124. return -EIO;
  1125. else {
  1126. entry->proc_fops = &acpi_processor_power_fops;
  1127. entry->data = acpi_driver_data(device);
  1128. entry->owner = THIS_MODULE;
  1129. }
  1130. pr->flags.power_setup_done = 1;
  1131. return 0;
  1132. }
  1133. int acpi_processor_power_exit(struct acpi_processor *pr,
  1134. struct acpi_device *device)
  1135. {
  1136. pr->flags.power_setup_done = 0;
  1137. if (acpi_device_dir(device))
  1138. remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1139. acpi_device_dir(device));
  1140. /* Unregister the idle handler when processor #0 is removed. */
  1141. if (pr->id == 0) {
  1142. pm_idle = pm_idle_save;
  1143. /*
  1144. * We are about to unload the current idle thread pm callback
  1145. * (pm_idle), Wait for all processors to update cached/local
  1146. * copies of pm_idle before proceeding.
  1147. */
  1148. cpu_idle_wait();
  1149. #ifdef CONFIG_SMP
  1150. unregister_latency_notifier(&acpi_processor_latency_notifier);
  1151. #endif
  1152. }
  1153. return 0;
  1154. }