processor_idle.c 27 KB

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