processor_idle.c 26 KB

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