processor_idle.c 36 KB

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