processor_idle.c 32 KB

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