processor_idle.c 32 KB

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