processor_idle.c 31 KB

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