processor_idle.c 28 KB

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