processor_idle.c 31 KB

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