processor_idle.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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[raw_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_cst (struct acpi_processor *pr)
  441. {
  442. acpi_status status = 0;
  443. acpi_integer count;
  444. int i;
  445. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  446. union acpi_object *cst;
  447. ACPI_FUNCTION_TRACE("acpi_processor_get_power_info_cst");
  448. if (errata.smp)
  449. return_VALUE(-ENODEV);
  450. if (nocst)
  451. return_VALUE(-ENODEV);
  452. pr->power.count = 0;
  453. for (i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++)
  454. memset(pr->power.states, 0, sizeof(struct acpi_processor_cx));
  455. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  456. if (ACPI_FAILURE(status)) {
  457. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  458. return_VALUE(-ENODEV);
  459. }
  460. cst = (union acpi_object *) buffer.pointer;
  461. /* There must be at least 2 elements */
  462. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  463. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "not enough elements in _CST\n"));
  464. status = -EFAULT;
  465. goto end;
  466. }
  467. count = cst->package.elements[0].integer.value;
  468. /* Validate number of power states. */
  469. if (count < 1 || count != cst->package.count - 1) {
  470. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "count given by _CST is not valid\n"));
  471. status = -EFAULT;
  472. goto end;
  473. }
  474. /* We support up to ACPI_PROCESSOR_MAX_POWER. */
  475. if (count > ACPI_PROCESSOR_MAX_POWER) {
  476. printk(KERN_WARNING "Limiting number of power states to max (%d)\n", ACPI_PROCESSOR_MAX_POWER);
  477. printk(KERN_WARNING "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  478. count = ACPI_PROCESSOR_MAX_POWER;
  479. }
  480. /* Tell driver that at least _CST is supported. */
  481. pr->flags.has_cst = 1;
  482. for (i = 1; i <= count; i++) {
  483. union acpi_object *element;
  484. union acpi_object *obj;
  485. struct acpi_power_register *reg;
  486. struct acpi_processor_cx cx;
  487. memset(&cx, 0, sizeof(cx));
  488. element = (union acpi_object *) &(cst->package.elements[i]);
  489. if (element->type != ACPI_TYPE_PACKAGE)
  490. continue;
  491. if (element->package.count != 4)
  492. continue;
  493. obj = (union acpi_object *) &(element->package.elements[0]);
  494. if (obj->type != ACPI_TYPE_BUFFER)
  495. continue;
  496. reg = (struct acpi_power_register *) obj->buffer.pointer;
  497. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  498. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  499. continue;
  500. cx.address = (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) ?
  501. 0 : reg->address;
  502. /* There should be an easy way to extract an integer... */
  503. obj = (union acpi_object *) &(element->package.elements[1]);
  504. if (obj->type != ACPI_TYPE_INTEGER)
  505. continue;
  506. cx.type = obj->integer.value;
  507. if ((cx.type != ACPI_STATE_C1) &&
  508. (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO))
  509. continue;
  510. if ((cx.type < ACPI_STATE_C1) ||
  511. (cx.type > ACPI_STATE_C3))
  512. continue;
  513. obj = (union acpi_object *) &(element->package.elements[2]);
  514. if (obj->type != ACPI_TYPE_INTEGER)
  515. continue;
  516. cx.latency = obj->integer.value;
  517. obj = (union acpi_object *) &(element->package.elements[3]);
  518. if (obj->type != ACPI_TYPE_INTEGER)
  519. continue;
  520. cx.power = obj->integer.value;
  521. (pr->power.count)++;
  522. memcpy(&(pr->power.states[pr->power.count]), &cx, sizeof(cx));
  523. }
  524. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n", pr->power.count));
  525. /* Validate number of power states discovered */
  526. if (pr->power.count < 2)
  527. status = -ENODEV;
  528. end:
  529. acpi_os_free(buffer.pointer);
  530. return_VALUE(status);
  531. }
  532. static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
  533. {
  534. ACPI_FUNCTION_TRACE("acpi_processor_get_power_verify_c2");
  535. if (!cx->address)
  536. return_VOID;
  537. /*
  538. * C2 latency must be less than or equal to 100
  539. * microseconds.
  540. */
  541. else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  542. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  543. "latency too large [%d]\n",
  544. cx->latency));
  545. return_VOID;
  546. }
  547. /* We're (currently) only supporting C2 on UP */
  548. else if (errata.smp) {
  549. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  550. "C2 not supported in SMP mode\n"));
  551. return_VOID;
  552. }
  553. /*
  554. * Otherwise we've met all of our C2 requirements.
  555. * Normalize the C2 latency to expidite policy
  556. */
  557. cx->valid = 1;
  558. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  559. return_VOID;
  560. }
  561. static void acpi_processor_power_verify_c3(
  562. struct acpi_processor *pr,
  563. struct acpi_processor_cx *cx)
  564. {
  565. ACPI_FUNCTION_TRACE("acpi_processor_get_power_verify_c3");
  566. if (!cx->address)
  567. return_VOID;
  568. /*
  569. * C3 latency must be less than or equal to 1000
  570. * microseconds.
  571. */
  572. else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  573. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  574. "latency too large [%d]\n",
  575. cx->latency));
  576. return_VOID;
  577. }
  578. /* bus mastering control is necessary */
  579. else if (!pr->flags.bm_control) {
  580. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  581. "C3 support requires bus mastering control\n"));
  582. return_VOID;
  583. }
  584. /* We're (currently) only supporting C2 on UP */
  585. else if (errata.smp) {
  586. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  587. "C3 not supported in SMP mode\n"));
  588. return_VOID;
  589. }
  590. /*
  591. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  592. * DMA transfers are used by any ISA device to avoid livelock.
  593. * Note that we could disable Type-F DMA (as recommended by
  594. * the erratum), but this is known to disrupt certain ISA
  595. * devices thus we take the conservative approach.
  596. */
  597. else if (errata.piix4.fdma) {
  598. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  599. "C3 not supported on PIIX4 with Type-F DMA\n"));
  600. return_VOID;
  601. }
  602. /*
  603. * Otherwise we've met all of our C3 requirements.
  604. * Normalize the C3 latency to expidite policy. Enable
  605. * checking of bus mastering status (bm_check) so we can
  606. * use this in our C3 policy
  607. */
  608. cx->valid = 1;
  609. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  610. pr->flags.bm_check = 1;
  611. return_VOID;
  612. }
  613. static int acpi_processor_power_verify(struct acpi_processor *pr)
  614. {
  615. unsigned int i;
  616. unsigned int working = 0;
  617. for (i=1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  618. struct acpi_processor_cx *cx = &pr->power.states[i];
  619. switch (cx->type) {
  620. case ACPI_STATE_C1:
  621. cx->valid = 1;
  622. break;
  623. case ACPI_STATE_C2:
  624. acpi_processor_power_verify_c2(cx);
  625. break;
  626. case ACPI_STATE_C3:
  627. acpi_processor_power_verify_c3(pr, cx);
  628. break;
  629. }
  630. if (cx->valid)
  631. working++;
  632. }
  633. return (working);
  634. }
  635. static int acpi_processor_get_power_info (
  636. struct acpi_processor *pr)
  637. {
  638. unsigned int i;
  639. int result;
  640. ACPI_FUNCTION_TRACE("acpi_processor_get_power_info");
  641. /* NOTE: the idle thread may not be running while calling
  642. * this function */
  643. result = acpi_processor_get_power_info_cst(pr);
  644. if ((result) || (acpi_processor_power_verify(pr) < 2)) {
  645. result = acpi_processor_get_power_info_fadt(pr);
  646. if (result)
  647. return_VALUE(result);
  648. if (acpi_processor_power_verify(pr) < 2)
  649. return_VALUE(-ENODEV);
  650. }
  651. /*
  652. * Set Default Policy
  653. * ------------------
  654. * Now that we know which states are supported, set the default
  655. * policy. Note that this policy can be changed dynamically
  656. * (e.g. encourage deeper sleeps to conserve battery life when
  657. * not on AC).
  658. */
  659. result = acpi_processor_set_power_policy(pr);
  660. if (result)
  661. return_VALUE(result);
  662. /*
  663. * if one state of type C2 or C3 is available, mark this
  664. * CPU as being "idle manageable"
  665. */
  666. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  667. if (pr->power.states[i].valid)
  668. pr->power.count = i;
  669. if ((pr->power.states[i].valid) &&
  670. (pr->power.states[i].type >= ACPI_STATE_C2))
  671. pr->flags.power = 1;
  672. }
  673. return_VALUE(0);
  674. }
  675. int acpi_processor_cst_has_changed (struct acpi_processor *pr)
  676. {
  677. int result = 0;
  678. ACPI_FUNCTION_TRACE("acpi_processor_cst_has_changed");
  679. if (!pr)
  680. return_VALUE(-EINVAL);
  681. if (errata.smp || nocst) {
  682. return_VALUE(-ENODEV);
  683. }
  684. if (!pr->flags.power_setup_done)
  685. return_VALUE(-ENODEV);
  686. /* Fall back to the default idle loop */
  687. pm_idle = pm_idle_save;
  688. synchronize_sched(); /* Relies on interrupts forcing exit from idle. */
  689. pr->flags.power = 0;
  690. result = acpi_processor_get_power_info(pr);
  691. if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
  692. pm_idle = acpi_processor_idle;
  693. return_VALUE(result);
  694. }
  695. /* proc interface */
  696. static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
  697. {
  698. struct acpi_processor *pr = (struct acpi_processor *)seq->private;
  699. unsigned int i;
  700. ACPI_FUNCTION_TRACE("acpi_processor_power_seq_show");
  701. if (!pr)
  702. goto end;
  703. seq_printf(seq, "active state: C%zd\n"
  704. "max_cstate: C%d\n"
  705. "bus master activity: %08x\n",
  706. pr->power.state ? pr->power.state - pr->power.states : 0,
  707. max_cstate,
  708. (unsigned)pr->power.bm_activity);
  709. seq_puts(seq, "states:\n");
  710. for (i = 1; i <= pr->power.count; i++) {
  711. seq_printf(seq, " %cC%d: ",
  712. (&pr->power.states[i] == pr->power.state?'*':' '), i);
  713. if (!pr->power.states[i].valid) {
  714. seq_puts(seq, "<not supported>\n");
  715. continue;
  716. }
  717. switch (pr->power.states[i].type) {
  718. case ACPI_STATE_C1:
  719. seq_printf(seq, "type[C1] ");
  720. break;
  721. case ACPI_STATE_C2:
  722. seq_printf(seq, "type[C2] ");
  723. break;
  724. case ACPI_STATE_C3:
  725. seq_printf(seq, "type[C3] ");
  726. break;
  727. default:
  728. seq_printf(seq, "type[--] ");
  729. break;
  730. }
  731. if (pr->power.states[i].promotion.state)
  732. seq_printf(seq, "promotion[C%zd] ",
  733. (pr->power.states[i].promotion.state -
  734. pr->power.states));
  735. else
  736. seq_puts(seq, "promotion[--] ");
  737. if (pr->power.states[i].demotion.state)
  738. seq_printf(seq, "demotion[C%zd] ",
  739. (pr->power.states[i].demotion.state -
  740. pr->power.states));
  741. else
  742. seq_puts(seq, "demotion[--] ");
  743. seq_printf(seq, "latency[%03d] usage[%08d]\n",
  744. pr->power.states[i].latency,
  745. pr->power.states[i].usage);
  746. }
  747. end:
  748. return_VALUE(0);
  749. }
  750. static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
  751. {
  752. return single_open(file, acpi_processor_power_seq_show,
  753. PDE(inode)->data);
  754. }
  755. static struct file_operations acpi_processor_power_fops = {
  756. .open = acpi_processor_power_open_fs,
  757. .read = seq_read,
  758. .llseek = seq_lseek,
  759. .release = single_release,
  760. };
  761. int acpi_processor_power_init(struct acpi_processor *pr, struct acpi_device *device)
  762. {
  763. acpi_status status = 0;
  764. static int first_run = 0;
  765. struct proc_dir_entry *entry = NULL;
  766. unsigned int i;
  767. ACPI_FUNCTION_TRACE("acpi_processor_power_init");
  768. if (!first_run) {
  769. dmi_check_system(processor_power_dmi_table);
  770. if (max_cstate < ACPI_C_STATES_MAX)
  771. printk(KERN_NOTICE "ACPI: processor limited to max C-state %d\n", max_cstate);
  772. first_run++;
  773. }
  774. if (!errata.smp && (pr->id == 0) && acpi_fadt.cst_cnt && !nocst) {
  775. status = acpi_os_write_port(acpi_fadt.smi_cmd, acpi_fadt.cst_cnt, 8);
  776. if (ACPI_FAILURE(status)) {
  777. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  778. "Notifying BIOS of _CST ability failed\n"));
  779. }
  780. }
  781. acpi_processor_get_power_info(pr);
  782. /*
  783. * Install the idle handler if processor power management is supported.
  784. * Note that we use previously set idle handler will be used on
  785. * platforms that only support C1.
  786. */
  787. if ((pr->flags.power) && (!boot_option_idle_override)) {
  788. printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
  789. for (i = 1; i <= pr->power.count; i++)
  790. if (pr->power.states[i].valid)
  791. printk(" C%d[C%d]", i, pr->power.states[i].type);
  792. printk(")\n");
  793. if (pr->id == 0) {
  794. pm_idle_save = pm_idle;
  795. pm_idle = acpi_processor_idle;
  796. }
  797. }
  798. /* 'power' [R] */
  799. entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  800. S_IRUGO, acpi_device_dir(device));
  801. if (!entry)
  802. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  803. "Unable to create '%s' fs entry\n",
  804. ACPI_PROCESSOR_FILE_POWER));
  805. else {
  806. entry->proc_fops = &acpi_processor_power_fops;
  807. entry->data = acpi_driver_data(device);
  808. entry->owner = THIS_MODULE;
  809. }
  810. pr->flags.power_setup_done = 1;
  811. return_VALUE(0);
  812. }
  813. int acpi_processor_power_exit(struct acpi_processor *pr, struct acpi_device *device)
  814. {
  815. ACPI_FUNCTION_TRACE("acpi_processor_power_exit");
  816. pr->flags.power_setup_done = 0;
  817. if (acpi_device_dir(device))
  818. remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,acpi_device_dir(device));
  819. /* Unregister the idle handler when processor #0 is removed. */
  820. if (pr->id == 0) {
  821. pm_idle = pm_idle_save;
  822. /*
  823. * We are about to unload the current idle thread pm callback
  824. * (pm_idle), Wait for all processors to update cached/local
  825. * copies of pm_idle before proceeding.
  826. */
  827. cpu_idle_wait();
  828. }
  829. return_VALUE(0);
  830. }