processor_idle.c 28 KB

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