processor_idle.c 34 KB

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