processor_idle.c 28 KB

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