processor_idle.c 27 KB

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