processor_idle.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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, 2005 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 <linux/latency.h>
  41. #include <asm/io.h>
  42. #include <asm/uaccess.h>
  43. #include <acpi/acpi_bus.h>
  44. #include <acpi/processor.h>
  45. #define ACPI_PROCESSOR_COMPONENT 0x01000000
  46. #define ACPI_PROCESSOR_CLASS "processor"
  47. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  48. ACPI_MODULE_NAME("processor_idle");
  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) __read_mostly;
  54. module_param(max_cstate, uint, 0644);
  55. static unsigned int nocst __read_mostly;
  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 __read_mostly =
  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. /* Actually this shouldn't be __cpuinitdata, would be better to fix the
  87. callers to only run once -AK */
  88. static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
  89. { set_max_cstate, "IBM ThinkPad R40e", {
  90. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  91. DMI_MATCH(DMI_BIOS_VERSION,"1SET70WW")}, (void *)1},
  92. { set_max_cstate, "IBM ThinkPad R40e", {
  93. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  94. DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
  95. { set_max_cstate, "IBM ThinkPad R40e", {
  96. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  97. DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
  98. { set_max_cstate, "IBM ThinkPad R40e", {
  99. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  100. DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
  101. { set_max_cstate, "IBM ThinkPad R40e", {
  102. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  103. DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
  104. { set_max_cstate, "IBM ThinkPad R40e", {
  105. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  106. DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
  107. { set_max_cstate, "IBM ThinkPad R40e", {
  108. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  109. DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
  110. { set_max_cstate, "IBM ThinkPad R40e", {
  111. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  112. DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
  113. { set_max_cstate, "IBM ThinkPad R40e", {
  114. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  115. DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
  116. { set_max_cstate, "IBM ThinkPad R40e", {
  117. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  118. DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
  119. { set_max_cstate, "IBM ThinkPad R40e", {
  120. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  121. DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
  122. { set_max_cstate, "IBM ThinkPad R40e", {
  123. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  124. DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
  125. { set_max_cstate, "IBM ThinkPad R40e", {
  126. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  127. DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
  128. { set_max_cstate, "IBM ThinkPad R40e", {
  129. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  130. DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
  131. { set_max_cstate, "IBM ThinkPad R40e", {
  132. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  133. DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
  134. { set_max_cstate, "IBM ThinkPad R40e", {
  135. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  136. DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
  137. { set_max_cstate, "Medion 41700", {
  138. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  139. DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
  140. { set_max_cstate, "Clevo 5600D", {
  141. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  142. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  143. (void *)2},
  144. {},
  145. };
  146. static inline u32 ticks_elapsed(u32 t1, u32 t2)
  147. {
  148. if (t2 >= t1)
  149. return (t2 - t1);
  150. else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
  151. return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
  152. else
  153. return ((0xFFFFFFFF - t1) + t2);
  154. }
  155. static void
  156. acpi_processor_power_activate(struct acpi_processor *pr,
  157. struct acpi_processor_cx *new)
  158. {
  159. struct acpi_processor_cx *old;
  160. if (!pr || !new)
  161. return;
  162. old = pr->power.state;
  163. if (old)
  164. old->promotion.count = 0;
  165. new->demotion.count = 0;
  166. /* Cleanup from old state. */
  167. if (old) {
  168. switch (old->type) {
  169. case ACPI_STATE_C3:
  170. /* Disable bus master reload */
  171. if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
  172. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  173. break;
  174. }
  175. }
  176. /* Prepare to use new state. */
  177. switch (new->type) {
  178. case ACPI_STATE_C3:
  179. /* Enable bus master reload */
  180. if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
  181. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  182. break;
  183. }
  184. pr->power.state = new;
  185. return;
  186. }
  187. static void acpi_safe_halt(void)
  188. {
  189. current_thread_info()->status &= ~TS_POLLING;
  190. /*
  191. * TS_POLLING-cleared state must be visible before we
  192. * test NEED_RESCHED:
  193. */
  194. smp_mb();
  195. if (!need_resched())
  196. safe_halt();
  197. current_thread_info()->status |= TS_POLLING;
  198. }
  199. static atomic_t c3_cpu_count;
  200. /* Common C-state entry for C2, C3, .. */
  201. static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
  202. {
  203. if (cstate->space_id == ACPI_CSTATE_FFH) {
  204. /* Call into architectural FFH based C-state */
  205. acpi_processor_ffh_cstate_enter(cstate);
  206. } else {
  207. int unused;
  208. /* IO port based C-state */
  209. inb(cstate->address);
  210. /* Dummy wait op - must do something useless after P_LVL2 read
  211. because chipsets cannot guarantee that STPCLK# signal
  212. gets asserted in time to freeze execution properly. */
  213. unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
  214. }
  215. }
  216. static void acpi_processor_idle(void)
  217. {
  218. struct acpi_processor *pr = NULL;
  219. struct acpi_processor_cx *cx = NULL;
  220. struct acpi_processor_cx *next_state = NULL;
  221. int sleep_ticks = 0;
  222. u32 t1, t2 = 0;
  223. pr = processors[smp_processor_id()];
  224. if (!pr)
  225. return;
  226. /*
  227. * Interrupts must be disabled during bus mastering calculations and
  228. * for C2/C3 transitions.
  229. */
  230. local_irq_disable();
  231. /*
  232. * Check whether we truly need to go idle, or should
  233. * reschedule:
  234. */
  235. if (unlikely(need_resched())) {
  236. local_irq_enable();
  237. return;
  238. }
  239. cx = pr->power.state;
  240. if (!cx) {
  241. if (pm_idle_save)
  242. pm_idle_save();
  243. else
  244. acpi_safe_halt();
  245. return;
  246. }
  247. /*
  248. * Check BM Activity
  249. * -----------------
  250. * Check for bus mastering activity (if required), record, and check
  251. * for demotion.
  252. */
  253. if (pr->flags.bm_check) {
  254. u32 bm_status = 0;
  255. unsigned long diff = jiffies - pr->power.bm_check_timestamp;
  256. if (diff > 31)
  257. diff = 31;
  258. pr->power.bm_activity <<= diff;
  259. acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  260. if (bm_status) {
  261. pr->power.bm_activity |= 0x1;
  262. acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  263. }
  264. /*
  265. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  266. * the true state of bus mastering activity; forcing us to
  267. * manually check the BMIDEA bit of each IDE channel.
  268. */
  269. else if (errata.piix4.bmisx) {
  270. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  271. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  272. pr->power.bm_activity |= 0x1;
  273. }
  274. pr->power.bm_check_timestamp = jiffies;
  275. /*
  276. * If bus mastering is or was active this jiffy, demote
  277. * to avoid a faulty transition. Note that the processor
  278. * won't enter a low-power state during this call (to this
  279. * function) but should upon the next.
  280. *
  281. * TBD: A better policy might be to fallback to the demotion
  282. * state (use it for this quantum only) istead of
  283. * demoting -- and rely on duration as our sole demotion
  284. * qualification. This may, however, introduce DMA
  285. * issues (e.g. floppy DMA transfer overrun/underrun).
  286. */
  287. if ((pr->power.bm_activity & 0x1) &&
  288. cx->demotion.threshold.bm) {
  289. local_irq_enable();
  290. next_state = cx->demotion.state;
  291. goto end;
  292. }
  293. }
  294. #ifdef CONFIG_HOTPLUG_CPU
  295. /*
  296. * Check for P_LVL2_UP flag before entering C2 and above on
  297. * an SMP system. We do it here instead of doing it at _CST/P_LVL
  298. * detection phase, to work cleanly with logical CPU hotplug.
  299. */
  300. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  301. !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  302. cx = &pr->power.states[ACPI_STATE_C1];
  303. #endif
  304. /*
  305. * Sleep:
  306. * ------
  307. * Invoke the current Cx state to put the processor to sleep.
  308. */
  309. if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
  310. current_thread_info()->status &= ~TS_POLLING;
  311. /*
  312. * TS_POLLING-cleared state must be visible before we
  313. * test NEED_RESCHED:
  314. */
  315. smp_mb();
  316. if (need_resched()) {
  317. current_thread_info()->status |= TS_POLLING;
  318. local_irq_enable();
  319. return;
  320. }
  321. }
  322. switch (cx->type) {
  323. case ACPI_STATE_C1:
  324. /*
  325. * Invoke C1.
  326. * Use the appropriate idle routine, the one that would
  327. * be used without acpi C-states.
  328. */
  329. if (pm_idle_save)
  330. pm_idle_save();
  331. else
  332. acpi_safe_halt();
  333. /*
  334. * TBD: Can't get time duration while in C1, as resumes
  335. * go to an ISR rather than here. Need to instrument
  336. * base interrupt handler.
  337. */
  338. sleep_ticks = 0xFFFFFFFF;
  339. break;
  340. case ACPI_STATE_C2:
  341. /* Get start time (ticks) */
  342. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  343. /* Invoke C2 */
  344. acpi_cstate_enter(cx);
  345. /* Get end time (ticks) */
  346. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  347. #ifdef CONFIG_GENERIC_TIME
  348. /* TSC halts in C2, so notify users */
  349. mark_tsc_unstable();
  350. #endif
  351. /* Re-enable interrupts */
  352. local_irq_enable();
  353. current_thread_info()->status |= TS_POLLING;
  354. /* Compute time (ticks) that we were actually asleep */
  355. sleep_ticks =
  356. ticks_elapsed(t1, t2) - cx->latency_ticks - C2_OVERHEAD;
  357. break;
  358. case ACPI_STATE_C3:
  359. if (pr->flags.bm_check) {
  360. if (atomic_inc_return(&c3_cpu_count) ==
  361. num_online_cpus()) {
  362. /*
  363. * All CPUs are trying to go to C3
  364. * Disable bus master arbitration
  365. */
  366. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
  367. }
  368. } else {
  369. /* SMP with no shared cache... Invalidate cache */
  370. ACPI_FLUSH_CPU_CACHE();
  371. }
  372. /* Get start time (ticks) */
  373. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  374. /* Invoke C3 */
  375. acpi_cstate_enter(cx);
  376. /* Get end time (ticks) */
  377. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  378. if (pr->flags.bm_check) {
  379. /* Enable bus master arbitration */
  380. atomic_dec(&c3_cpu_count);
  381. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
  382. }
  383. #ifdef CONFIG_GENERIC_TIME
  384. /* TSC halts in C3, so notify users */
  385. mark_tsc_unstable();
  386. #endif
  387. /* Re-enable interrupts */
  388. local_irq_enable();
  389. current_thread_info()->status |= TS_POLLING;
  390. /* Compute time (ticks) that we were actually asleep */
  391. sleep_ticks =
  392. ticks_elapsed(t1, t2) - cx->latency_ticks - C3_OVERHEAD;
  393. break;
  394. default:
  395. local_irq_enable();
  396. return;
  397. }
  398. cx->usage++;
  399. if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
  400. cx->time += sleep_ticks;
  401. next_state = pr->power.state;
  402. #ifdef CONFIG_HOTPLUG_CPU
  403. /* Don't do promotion/demotion */
  404. if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  405. !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
  406. next_state = cx;
  407. goto end;
  408. }
  409. #endif
  410. /*
  411. * Promotion?
  412. * ----------
  413. * Track the number of longs (time asleep is greater than threshold)
  414. * and promote when the count threshold is reached. Note that bus
  415. * mastering activity may prevent promotions.
  416. * Do not promote above max_cstate.
  417. */
  418. if (cx->promotion.state &&
  419. ((cx->promotion.state - pr->power.states) <= max_cstate)) {
  420. if (sleep_ticks > cx->promotion.threshold.ticks &&
  421. cx->promotion.state->latency <= system_latency_constraint()) {
  422. cx->promotion.count++;
  423. cx->demotion.count = 0;
  424. if (cx->promotion.count >=
  425. cx->promotion.threshold.count) {
  426. if (pr->flags.bm_check) {
  427. if (!
  428. (pr->power.bm_activity & cx->
  429. promotion.threshold.bm)) {
  430. next_state =
  431. cx->promotion.state;
  432. goto end;
  433. }
  434. } else {
  435. next_state = cx->promotion.state;
  436. goto end;
  437. }
  438. }
  439. }
  440. }
  441. /*
  442. * Demotion?
  443. * ---------
  444. * Track the number of shorts (time asleep is less than time threshold)
  445. * and demote when the usage threshold is reached.
  446. */
  447. if (cx->demotion.state) {
  448. if (sleep_ticks < cx->demotion.threshold.ticks) {
  449. cx->demotion.count++;
  450. cx->promotion.count = 0;
  451. if (cx->demotion.count >= cx->demotion.threshold.count) {
  452. next_state = cx->demotion.state;
  453. goto end;
  454. }
  455. }
  456. }
  457. end:
  458. /*
  459. * Demote if current state exceeds max_cstate
  460. * or if the latency of the current state is unacceptable
  461. */
  462. if ((pr->power.state - pr->power.states) > max_cstate ||
  463. pr->power.state->latency > system_latency_constraint()) {
  464. if (cx->demotion.state)
  465. next_state = cx->demotion.state;
  466. }
  467. /*
  468. * New Cx State?
  469. * -------------
  470. * If we're going to start using a new Cx state we must clean up
  471. * from the previous and prepare to use the new.
  472. */
  473. if (next_state != pr->power.state)
  474. acpi_processor_power_activate(pr, next_state);
  475. }
  476. static int acpi_processor_set_power_policy(struct acpi_processor *pr)
  477. {
  478. unsigned int i;
  479. unsigned int state_is_set = 0;
  480. struct acpi_processor_cx *lower = NULL;
  481. struct acpi_processor_cx *higher = NULL;
  482. struct acpi_processor_cx *cx;
  483. if (!pr)
  484. return -EINVAL;
  485. /*
  486. * This function sets the default Cx state policy (OS idle handler).
  487. * Our scheme is to promote quickly to C2 but more conservatively
  488. * to C3. We're favoring C2 for its characteristics of low latency
  489. * (quick response), good power savings, and ability to allow bus
  490. * mastering activity. Note that the Cx state policy is completely
  491. * customizable and can be altered dynamically.
  492. */
  493. /* startup state */
  494. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  495. cx = &pr->power.states[i];
  496. if (!cx->valid)
  497. continue;
  498. if (!state_is_set)
  499. pr->power.state = cx;
  500. state_is_set++;
  501. break;
  502. }
  503. if (!state_is_set)
  504. return -ENODEV;
  505. /* demotion */
  506. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  507. cx = &pr->power.states[i];
  508. if (!cx->valid)
  509. continue;
  510. if (lower) {
  511. cx->demotion.state = lower;
  512. cx->demotion.threshold.ticks = cx->latency_ticks;
  513. cx->demotion.threshold.count = 1;
  514. if (cx->type == ACPI_STATE_C3)
  515. cx->demotion.threshold.bm = bm_history;
  516. }
  517. lower = cx;
  518. }
  519. /* promotion */
  520. for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
  521. cx = &pr->power.states[i];
  522. if (!cx->valid)
  523. continue;
  524. if (higher) {
  525. cx->promotion.state = higher;
  526. cx->promotion.threshold.ticks = cx->latency_ticks;
  527. if (cx->type >= ACPI_STATE_C2)
  528. cx->promotion.threshold.count = 4;
  529. else
  530. cx->promotion.threshold.count = 10;
  531. if (higher->type == ACPI_STATE_C3)
  532. cx->promotion.threshold.bm = bm_history;
  533. }
  534. higher = cx;
  535. }
  536. return 0;
  537. }
  538. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  539. {
  540. if (!pr)
  541. return -EINVAL;
  542. if (!pr->pblk)
  543. return -ENODEV;
  544. /* if info is obtained from pblk/fadt, type equals state */
  545. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  546. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  547. #ifndef CONFIG_HOTPLUG_CPU
  548. /*
  549. * Check for P_LVL2_UP flag before entering C2 and above on
  550. * an SMP system.
  551. */
  552. if ((num_online_cpus() > 1) &&
  553. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  554. return -ENODEV;
  555. #endif
  556. /* determine C2 and C3 address from pblk */
  557. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  558. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  559. /* determine latencies from FADT */
  560. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
  561. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
  562. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  563. "lvl2[0x%08x] lvl3[0x%08x]\n",
  564. pr->power.states[ACPI_STATE_C2].address,
  565. pr->power.states[ACPI_STATE_C3].address));
  566. return 0;
  567. }
  568. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  569. {
  570. if (!pr->power.states[ACPI_STATE_C1].valid) {
  571. /* set the first C-State to C1 */
  572. /* all processors need to support C1 */
  573. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  574. pr->power.states[ACPI_STATE_C1].valid = 1;
  575. }
  576. /* the C0 state only exists as a filler in our array */
  577. pr->power.states[ACPI_STATE_C0].valid = 1;
  578. return 0;
  579. }
  580. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  581. {
  582. acpi_status status = 0;
  583. acpi_integer count;
  584. int current_count;
  585. int i;
  586. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  587. union acpi_object *cst;
  588. if (nocst)
  589. return -ENODEV;
  590. current_count = 0;
  591. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  592. if (ACPI_FAILURE(status)) {
  593. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  594. return -ENODEV;
  595. }
  596. cst = buffer.pointer;
  597. /* There must be at least 2 elements */
  598. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  599. printk(KERN_ERR PREFIX "not enough elements in _CST\n");
  600. status = -EFAULT;
  601. goto end;
  602. }
  603. count = cst->package.elements[0].integer.value;
  604. /* Validate number of power states. */
  605. if (count < 1 || count != cst->package.count - 1) {
  606. printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
  607. status = -EFAULT;
  608. goto end;
  609. }
  610. /* Tell driver that at least _CST is supported. */
  611. pr->flags.has_cst = 1;
  612. for (i = 1; i <= count; i++) {
  613. union acpi_object *element;
  614. union acpi_object *obj;
  615. struct acpi_power_register *reg;
  616. struct acpi_processor_cx cx;
  617. memset(&cx, 0, sizeof(cx));
  618. element = &(cst->package.elements[i]);
  619. if (element->type != ACPI_TYPE_PACKAGE)
  620. continue;
  621. if (element->package.count != 4)
  622. continue;
  623. obj = &(element->package.elements[0]);
  624. if (obj->type != ACPI_TYPE_BUFFER)
  625. continue;
  626. reg = (struct acpi_power_register *)obj->buffer.pointer;
  627. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  628. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  629. continue;
  630. /* There should be an easy way to extract an integer... */
  631. obj = &(element->package.elements[1]);
  632. if (obj->type != ACPI_TYPE_INTEGER)
  633. continue;
  634. cx.type = obj->integer.value;
  635. /*
  636. * Some buggy BIOSes won't list C1 in _CST -
  637. * Let acpi_processor_get_power_info_default() handle them later
  638. */
  639. if (i == 1 && cx.type != ACPI_STATE_C1)
  640. current_count++;
  641. cx.address = reg->address;
  642. cx.index = current_count + 1;
  643. cx.space_id = ACPI_CSTATE_SYSTEMIO;
  644. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  645. if (acpi_processor_ffh_cstate_probe
  646. (pr->id, &cx, reg) == 0) {
  647. cx.space_id = ACPI_CSTATE_FFH;
  648. } else if (cx.type != ACPI_STATE_C1) {
  649. /*
  650. * C1 is a special case where FIXED_HARDWARE
  651. * can be handled in non-MWAIT way as well.
  652. * In that case, save this _CST entry info.
  653. * That is, we retain space_id of SYSTEM_IO for
  654. * halt based C1.
  655. * Otherwise, ignore this info and continue.
  656. */
  657. continue;
  658. }
  659. }
  660. obj = &(element->package.elements[2]);
  661. if (obj->type != ACPI_TYPE_INTEGER)
  662. continue;
  663. cx.latency = obj->integer.value;
  664. obj = &(element->package.elements[3]);
  665. if (obj->type != ACPI_TYPE_INTEGER)
  666. continue;
  667. cx.power = obj->integer.value;
  668. current_count++;
  669. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  670. /*
  671. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  672. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  673. */
  674. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  675. printk(KERN_WARNING
  676. "Limiting number of power states to max (%d)\n",
  677. ACPI_PROCESSOR_MAX_POWER);
  678. printk(KERN_WARNING
  679. "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  680. break;
  681. }
  682. }
  683. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  684. current_count));
  685. /* Validate number of power states discovered */
  686. if (current_count < 2)
  687. status = -EFAULT;
  688. end:
  689. kfree(buffer.pointer);
  690. return status;
  691. }
  692. static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
  693. {
  694. if (!cx->address)
  695. return;
  696. /*
  697. * C2 latency must be less than or equal to 100
  698. * microseconds.
  699. */
  700. else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  701. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  702. "latency too large [%d]\n", cx->latency));
  703. return;
  704. }
  705. /*
  706. * Otherwise we've met all of our C2 requirements.
  707. * Normalize the C2 latency to expidite policy
  708. */
  709. cx->valid = 1;
  710. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  711. return;
  712. }
  713. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  714. struct acpi_processor_cx *cx)
  715. {
  716. static int bm_check_flag;
  717. if (!cx->address)
  718. return;
  719. /*
  720. * C3 latency must be less than or equal to 1000
  721. * microseconds.
  722. */
  723. else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  724. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  725. "latency too large [%d]\n", cx->latency));
  726. return;
  727. }
  728. /*
  729. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  730. * DMA transfers are used by any ISA device to avoid livelock.
  731. * Note that we could disable Type-F DMA (as recommended by
  732. * the erratum), but this is known to disrupt certain ISA
  733. * devices thus we take the conservative approach.
  734. */
  735. else if (errata.piix4.fdma) {
  736. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  737. "C3 not supported on PIIX4 with Type-F DMA\n"));
  738. return;
  739. }
  740. /* All the logic here assumes flags.bm_check is same across all CPUs */
  741. if (!bm_check_flag) {
  742. /* Determine whether bm_check is needed based on CPU */
  743. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  744. bm_check_flag = pr->flags.bm_check;
  745. } else {
  746. pr->flags.bm_check = bm_check_flag;
  747. }
  748. if (pr->flags.bm_check) {
  749. /* bus mastering control is necessary */
  750. if (!pr->flags.bm_control) {
  751. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  752. "C3 support requires bus mastering control\n"));
  753. return;
  754. }
  755. } else {
  756. /*
  757. * WBINVD should be set in fadt, for C3 state to be
  758. * supported on when bm_check is not required.
  759. */
  760. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  761. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  762. "Cache invalidation should work properly"
  763. " for C3 to be enabled on SMP systems\n"));
  764. return;
  765. }
  766. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  767. }
  768. /*
  769. * Otherwise we've met all of our C3 requirements.
  770. * Normalize the C3 latency to expidite policy. Enable
  771. * checking of bus mastering status (bm_check) so we can
  772. * use this in our C3 policy
  773. */
  774. cx->valid = 1;
  775. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  776. return;
  777. }
  778. static int acpi_processor_power_verify(struct acpi_processor *pr)
  779. {
  780. unsigned int i;
  781. unsigned int working = 0;
  782. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  783. int timer_broadcast = 0;
  784. cpumask_t mask = cpumask_of_cpu(pr->id);
  785. on_each_cpu(switch_ipi_to_APIC_timer, &mask, 1, 1);
  786. #endif
  787. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  788. struct acpi_processor_cx *cx = &pr->power.states[i];
  789. switch (cx->type) {
  790. case ACPI_STATE_C1:
  791. cx->valid = 1;
  792. break;
  793. case ACPI_STATE_C2:
  794. acpi_processor_power_verify_c2(cx);
  795. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  796. /* Some AMD systems fake C3 as C2, but still
  797. have timer troubles */
  798. if (cx->valid &&
  799. boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  800. timer_broadcast++;
  801. #endif
  802. break;
  803. case ACPI_STATE_C3:
  804. acpi_processor_power_verify_c3(pr, cx);
  805. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  806. if (cx->valid)
  807. timer_broadcast++;
  808. #endif
  809. break;
  810. }
  811. if (cx->valid)
  812. working++;
  813. }
  814. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  815. if (timer_broadcast)
  816. on_each_cpu(switch_APIC_timer_to_ipi, &mask, 1, 1);
  817. #endif
  818. return (working);
  819. }
  820. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  821. {
  822. unsigned int i;
  823. int result;
  824. /* NOTE: the idle thread may not be running while calling
  825. * this function */
  826. /* Zero initialize all the C-states info. */
  827. memset(pr->power.states, 0, sizeof(pr->power.states));
  828. result = acpi_processor_get_power_info_cst(pr);
  829. if (result == -ENODEV)
  830. result = acpi_processor_get_power_info_fadt(pr);
  831. if (result)
  832. return result;
  833. acpi_processor_get_power_info_default(pr);
  834. pr->power.count = acpi_processor_power_verify(pr);
  835. /*
  836. * Set Default Policy
  837. * ------------------
  838. * Now that we know which states are supported, set the default
  839. * policy. Note that this policy can be changed dynamically
  840. * (e.g. encourage deeper sleeps to conserve battery life when
  841. * not on AC).
  842. */
  843. result = acpi_processor_set_power_policy(pr);
  844. if (result)
  845. return result;
  846. /*
  847. * if one state of type C2 or C3 is available, mark this
  848. * CPU as being "idle manageable"
  849. */
  850. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  851. if (pr->power.states[i].valid) {
  852. pr->power.count = i;
  853. if (pr->power.states[i].type >= ACPI_STATE_C2)
  854. pr->flags.power = 1;
  855. }
  856. }
  857. return 0;
  858. }
  859. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  860. {
  861. int result = 0;
  862. if (!pr)
  863. return -EINVAL;
  864. if (nocst) {
  865. return -ENODEV;
  866. }
  867. if (!pr->flags.power_setup_done)
  868. return -ENODEV;
  869. /* Fall back to the default idle loop */
  870. pm_idle = pm_idle_save;
  871. synchronize_sched(); /* Relies on interrupts forcing exit from idle. */
  872. pr->flags.power = 0;
  873. result = acpi_processor_get_power_info(pr);
  874. if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
  875. pm_idle = acpi_processor_idle;
  876. return result;
  877. }
  878. /* proc interface */
  879. static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
  880. {
  881. struct acpi_processor *pr = seq->private;
  882. unsigned int i;
  883. if (!pr)
  884. goto end;
  885. seq_printf(seq, "active state: C%zd\n"
  886. "max_cstate: C%d\n"
  887. "bus master activity: %08x\n"
  888. "maximum allowed latency: %d usec\n",
  889. pr->power.state ? pr->power.state - pr->power.states : 0,
  890. max_cstate, (unsigned)pr->power.bm_activity,
  891. system_latency_constraint());
  892. seq_puts(seq, "states:\n");
  893. for (i = 1; i <= pr->power.count; i++) {
  894. seq_printf(seq, " %cC%d: ",
  895. (&pr->power.states[i] ==
  896. pr->power.state ? '*' : ' '), i);
  897. if (!pr->power.states[i].valid) {
  898. seq_puts(seq, "<not supported>\n");
  899. continue;
  900. }
  901. switch (pr->power.states[i].type) {
  902. case ACPI_STATE_C1:
  903. seq_printf(seq, "type[C1] ");
  904. break;
  905. case ACPI_STATE_C2:
  906. seq_printf(seq, "type[C2] ");
  907. break;
  908. case ACPI_STATE_C3:
  909. seq_printf(seq, "type[C3] ");
  910. break;
  911. default:
  912. seq_printf(seq, "type[--] ");
  913. break;
  914. }
  915. if (pr->power.states[i].promotion.state)
  916. seq_printf(seq, "promotion[C%zd] ",
  917. (pr->power.states[i].promotion.state -
  918. pr->power.states));
  919. else
  920. seq_puts(seq, "promotion[--] ");
  921. if (pr->power.states[i].demotion.state)
  922. seq_printf(seq, "demotion[C%zd] ",
  923. (pr->power.states[i].demotion.state -
  924. pr->power.states));
  925. else
  926. seq_puts(seq, "demotion[--] ");
  927. seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
  928. pr->power.states[i].latency,
  929. pr->power.states[i].usage,
  930. (unsigned long long)pr->power.states[i].time);
  931. }
  932. end:
  933. return 0;
  934. }
  935. static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
  936. {
  937. return single_open(file, acpi_processor_power_seq_show,
  938. PDE(inode)->data);
  939. }
  940. static const struct file_operations acpi_processor_power_fops = {
  941. .open = acpi_processor_power_open_fs,
  942. .read = seq_read,
  943. .llseek = seq_lseek,
  944. .release = single_release,
  945. };
  946. #ifdef CONFIG_SMP
  947. static void smp_callback(void *v)
  948. {
  949. /* we already woke the CPU up, nothing more to do */
  950. }
  951. /*
  952. * This function gets called when a part of the kernel has a new latency
  953. * requirement. This means we need to get all processors out of their C-state,
  954. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  955. * wakes them all right up.
  956. */
  957. static int acpi_processor_latency_notify(struct notifier_block *b,
  958. unsigned long l, void *v)
  959. {
  960. smp_call_function(smp_callback, NULL, 0, 1);
  961. return NOTIFY_OK;
  962. }
  963. static struct notifier_block acpi_processor_latency_notifier = {
  964. .notifier_call = acpi_processor_latency_notify,
  965. };
  966. #endif
  967. int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
  968. struct acpi_device *device)
  969. {
  970. acpi_status status = 0;
  971. static int first_run;
  972. struct proc_dir_entry *entry = NULL;
  973. unsigned int i;
  974. if (!first_run) {
  975. dmi_check_system(processor_power_dmi_table);
  976. if (max_cstate < ACPI_C_STATES_MAX)
  977. printk(KERN_NOTICE
  978. "ACPI: processor limited to max C-state %d\n",
  979. max_cstate);
  980. first_run++;
  981. #ifdef CONFIG_SMP
  982. register_latency_notifier(&acpi_processor_latency_notifier);
  983. #endif
  984. }
  985. if (!pr)
  986. return -EINVAL;
  987. if (acpi_gbl_FADT.cst_control && !nocst) {
  988. status =
  989. acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
  990. if (ACPI_FAILURE(status)) {
  991. ACPI_EXCEPTION((AE_INFO, status,
  992. "Notifying BIOS of _CST ability failed"));
  993. }
  994. }
  995. acpi_processor_get_power_info(pr);
  996. /*
  997. * Install the idle handler if processor power management is supported.
  998. * Note that we use previously set idle handler will be used on
  999. * platforms that only support C1.
  1000. */
  1001. if ((pr->flags.power) && (!boot_option_idle_override)) {
  1002. printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
  1003. for (i = 1; i <= pr->power.count; i++)
  1004. if (pr->power.states[i].valid)
  1005. printk(" C%d[C%d]", i,
  1006. pr->power.states[i].type);
  1007. printk(")\n");
  1008. if (pr->id == 0) {
  1009. pm_idle_save = pm_idle;
  1010. pm_idle = acpi_processor_idle;
  1011. }
  1012. }
  1013. /* 'power' [R] */
  1014. entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1015. S_IRUGO, acpi_device_dir(device));
  1016. if (!entry)
  1017. return -EIO;
  1018. else {
  1019. entry->proc_fops = &acpi_processor_power_fops;
  1020. entry->data = acpi_driver_data(device);
  1021. entry->owner = THIS_MODULE;
  1022. }
  1023. pr->flags.power_setup_done = 1;
  1024. return 0;
  1025. }
  1026. int acpi_processor_power_exit(struct acpi_processor *pr,
  1027. struct acpi_device *device)
  1028. {
  1029. pr->flags.power_setup_done = 0;
  1030. if (acpi_device_dir(device))
  1031. remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1032. acpi_device_dir(device));
  1033. /* Unregister the idle handler when processor #0 is removed. */
  1034. if (pr->id == 0) {
  1035. pm_idle = pm_idle_save;
  1036. /*
  1037. * We are about to unload the current idle thread pm callback
  1038. * (pm_idle), Wait for all processors to update cached/local
  1039. * copies of pm_idle before proceeding.
  1040. */
  1041. cpu_idle_wait();
  1042. #ifdef CONFIG_SMP
  1043. unregister_latency_notifier(&acpi_processor_latency_notifier);
  1044. #endif
  1045. }
  1046. return 0;
  1047. }