processor_idle.c 34 KB

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