processor_idle.c 28 KB

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