processor_idle.c 29 KB

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