processor_idle.c 28 KB

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