processor_idle.c 34 KB

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