processor_idle.c 27 KB

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