processor_idle.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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/pm_qos_params.h>
  41. #include <linux/clockchips.h>
  42. #include <linux/cpuidle.h>
  43. /*
  44. * Include the apic definitions for x86 to have the APIC timer related defines
  45. * available also for UP (on SMP it gets magically included via linux/smp.h).
  46. * asm/acpi.h is not an option, as it would require more include magic. Also
  47. * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
  48. */
  49. #ifdef CONFIG_X86
  50. #include <asm/apic.h>
  51. #endif
  52. #include <asm/io.h>
  53. #include <asm/uaccess.h>
  54. #include <acpi/acpi_bus.h>
  55. #include <acpi/processor.h>
  56. #include <asm/processor.h>
  57. #define ACPI_PROCESSOR_CLASS "processor"
  58. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  59. ACPI_MODULE_NAME("processor_idle");
  60. #define ACPI_PROCESSOR_FILE_POWER "power"
  61. #define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
  62. #define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
  63. #ifndef CONFIG_CPU_IDLE
  64. #define C2_OVERHEAD 4 /* 1us (3.579 ticks per us) */
  65. #define C3_OVERHEAD 4 /* 1us (3.579 ticks per us) */
  66. static void (*pm_idle_save) (void) __read_mostly;
  67. #else
  68. #define C2_OVERHEAD 1 /* 1us */
  69. #define C3_OVERHEAD 1 /* 1us */
  70. #endif
  71. #define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
  72. static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
  73. #ifdef CONFIG_CPU_IDLE
  74. module_param(max_cstate, uint, 0000);
  75. #else
  76. module_param(max_cstate, uint, 0644);
  77. #endif
  78. static unsigned int nocst __read_mostly;
  79. module_param(nocst, uint, 0000);
  80. #ifndef CONFIG_CPU_IDLE
  81. /*
  82. * bm_history -- bit-mask with a bit per jiffy of bus-master activity
  83. * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
  84. * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
  85. * 100 HZ: 0x0000000F: 4 jiffies = 40ms
  86. * reduce history for more aggressive entry into C3
  87. */
  88. static unsigned int bm_history __read_mostly =
  89. (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
  90. module_param(bm_history, uint, 0644);
  91. static int acpi_processor_set_power_policy(struct acpi_processor *pr);
  92. #else /* CONFIG_CPU_IDLE */
  93. static unsigned int latency_factor __read_mostly = 2;
  94. module_param(latency_factor, uint, 0644);
  95. #endif
  96. /*
  97. * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
  98. * For now disable this. Probably a bug somewhere else.
  99. *
  100. * To skip this limit, boot/load with a large max_cstate limit.
  101. */
  102. static int set_max_cstate(const struct dmi_system_id *id)
  103. {
  104. if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
  105. return 0;
  106. printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
  107. " Override with \"processor.max_cstate=%d\"\n", id->ident,
  108. (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
  109. max_cstate = (long)id->driver_data;
  110. return 0;
  111. }
  112. /* Actually this shouldn't be __cpuinitdata, would be better to fix the
  113. callers to only run once -AK */
  114. static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
  115. { set_max_cstate, "IBM ThinkPad R40e", {
  116. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  117. DMI_MATCH(DMI_BIOS_VERSION,"1SET70WW")}, (void *)1},
  118. { set_max_cstate, "IBM ThinkPad R40e", {
  119. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  120. DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
  121. { set_max_cstate, "IBM ThinkPad R40e", {
  122. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  123. DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
  124. { set_max_cstate, "IBM ThinkPad R40e", {
  125. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  126. DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
  127. { set_max_cstate, "IBM ThinkPad R40e", {
  128. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  129. DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
  130. { set_max_cstate, "IBM ThinkPad R40e", {
  131. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  132. DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
  133. { set_max_cstate, "IBM ThinkPad R40e", {
  134. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  135. DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
  136. { set_max_cstate, "IBM ThinkPad R40e", {
  137. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  138. DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
  139. { set_max_cstate, "IBM ThinkPad R40e", {
  140. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  141. DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
  142. { set_max_cstate, "IBM ThinkPad R40e", {
  143. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  144. DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
  145. { set_max_cstate, "IBM ThinkPad R40e", {
  146. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  147. DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
  148. { set_max_cstate, "IBM ThinkPad R40e", {
  149. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  150. DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
  151. { set_max_cstate, "IBM ThinkPad R40e", {
  152. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  153. DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
  154. { set_max_cstate, "IBM ThinkPad R40e", {
  155. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  156. DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
  157. { set_max_cstate, "IBM ThinkPad R40e", {
  158. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  159. DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
  160. { set_max_cstate, "IBM ThinkPad R40e", {
  161. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  162. DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
  163. { set_max_cstate, "Medion 41700", {
  164. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  165. DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
  166. { set_max_cstate, "Clevo 5600D", {
  167. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  168. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  169. (void *)2},
  170. {},
  171. };
  172. static inline u32 ticks_elapsed(u32 t1, u32 t2)
  173. {
  174. if (t2 >= t1)
  175. return (t2 - t1);
  176. else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
  177. return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
  178. else
  179. return ((0xFFFFFFFF - t1) + t2);
  180. }
  181. static inline u32 ticks_elapsed_in_us(u32 t1, u32 t2)
  182. {
  183. if (t2 >= t1)
  184. return PM_TIMER_TICKS_TO_US(t2 - t1);
  185. else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
  186. return PM_TIMER_TICKS_TO_US(((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
  187. else
  188. return PM_TIMER_TICKS_TO_US((0xFFFFFFFF - t1) + t2);
  189. }
  190. /*
  191. * Callers should disable interrupts before the call and enable
  192. * interrupts after return.
  193. */
  194. static void acpi_safe_halt(void)
  195. {
  196. current_thread_info()->status &= ~TS_POLLING;
  197. /*
  198. * TS_POLLING-cleared state must be visible before we
  199. * test NEED_RESCHED:
  200. */
  201. smp_mb();
  202. if (!need_resched()) {
  203. safe_halt();
  204. local_irq_disable();
  205. }
  206. current_thread_info()->status |= TS_POLLING;
  207. }
  208. #ifndef CONFIG_CPU_IDLE
  209. static void
  210. acpi_processor_power_activate(struct acpi_processor *pr,
  211. struct acpi_processor_cx *new)
  212. {
  213. struct acpi_processor_cx *old;
  214. if (!pr || !new)
  215. return;
  216. old = pr->power.state;
  217. if (old)
  218. old->promotion.count = 0;
  219. new->demotion.count = 0;
  220. /* Cleanup from old state. */
  221. if (old) {
  222. switch (old->type) {
  223. case ACPI_STATE_C3:
  224. /* Disable bus master reload */
  225. if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
  226. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  227. break;
  228. }
  229. }
  230. /* Prepare to use new state. */
  231. switch (new->type) {
  232. case ACPI_STATE_C3:
  233. /* Enable bus master reload */
  234. if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
  235. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  236. break;
  237. }
  238. pr->power.state = new;
  239. return;
  240. }
  241. static atomic_t c3_cpu_count;
  242. /* Common C-state entry for C2, C3, .. */
  243. static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
  244. {
  245. /* Don't trace irqs off for idle */
  246. stop_critical_timings();
  247. if (cstate->entry_method == ACPI_CSTATE_FFH) {
  248. /* Call into architectural FFH based C-state */
  249. acpi_processor_ffh_cstate_enter(cstate);
  250. } else {
  251. int unused;
  252. /* IO port based C-state */
  253. inb(cstate->address);
  254. /* Dummy wait op - must do something useless after P_LVL2 read
  255. because chipsets cannot guarantee that STPCLK# signal
  256. gets asserted in time to freeze execution properly. */
  257. unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
  258. }
  259. start_critical_timings();
  260. }
  261. #endif /* !CONFIG_CPU_IDLE */
  262. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  263. /*
  264. * Some BIOS implementations switch to C3 in the published C2 state.
  265. * This seems to be a common problem on AMD boxen, but other vendors
  266. * are affected too. We pick the most conservative approach: we assume
  267. * that the local APIC stops in both C2 and C3.
  268. */
  269. static void acpi_timer_check_state(int state, struct acpi_processor *pr,
  270. struct acpi_processor_cx *cx)
  271. {
  272. struct acpi_processor_power *pwr = &pr->power;
  273. u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
  274. /*
  275. * Check, if one of the previous states already marked the lapic
  276. * unstable
  277. */
  278. if (pwr->timer_broadcast_on_state < state)
  279. return;
  280. if (cx->type >= type)
  281. pr->power.timer_broadcast_on_state = state;
  282. }
  283. static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
  284. {
  285. unsigned long reason;
  286. reason = pr->power.timer_broadcast_on_state < INT_MAX ?
  287. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  288. clockevents_notify(reason, &pr->id);
  289. }
  290. /* Power(C) State timer broadcast control */
  291. static void acpi_state_timer_broadcast(struct acpi_processor *pr,
  292. struct acpi_processor_cx *cx,
  293. int broadcast)
  294. {
  295. int state = cx - pr->power.states;
  296. if (state >= pr->power.timer_broadcast_on_state) {
  297. unsigned long reason;
  298. reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
  299. CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
  300. clockevents_notify(reason, &pr->id);
  301. }
  302. }
  303. #else
  304. static void acpi_timer_check_state(int state, struct acpi_processor *pr,
  305. struct acpi_processor_cx *cstate) { }
  306. static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
  307. static void acpi_state_timer_broadcast(struct acpi_processor *pr,
  308. struct acpi_processor_cx *cx,
  309. int broadcast)
  310. {
  311. }
  312. #endif
  313. /*
  314. * Suspend / resume control
  315. */
  316. static int acpi_idle_suspend;
  317. int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
  318. {
  319. acpi_idle_suspend = 1;
  320. return 0;
  321. }
  322. int acpi_processor_resume(struct acpi_device * device)
  323. {
  324. acpi_idle_suspend = 0;
  325. return 0;
  326. }
  327. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
  328. static int tsc_halts_in_c(int state)
  329. {
  330. switch (boot_cpu_data.x86_vendor) {
  331. case X86_VENDOR_AMD:
  332. case X86_VENDOR_INTEL:
  333. /*
  334. * AMD Fam10h TSC will tick in all
  335. * C/P/S0/S1 states when this bit is set.
  336. */
  337. if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  338. return 0;
  339. /*FALL THROUGH*/
  340. default:
  341. return state > ACPI_STATE_C1;
  342. }
  343. }
  344. #endif
  345. #ifndef CONFIG_CPU_IDLE
  346. static void acpi_processor_idle(void)
  347. {
  348. struct acpi_processor *pr = NULL;
  349. struct acpi_processor_cx *cx = NULL;
  350. struct acpi_processor_cx *next_state = NULL;
  351. int sleep_ticks = 0;
  352. u32 t1, t2 = 0;
  353. /*
  354. * Interrupts must be disabled during bus mastering calculations and
  355. * for C2/C3 transitions.
  356. */
  357. local_irq_disable();
  358. pr = __get_cpu_var(processors);
  359. if (!pr) {
  360. local_irq_enable();
  361. return;
  362. }
  363. /*
  364. * Check whether we truly need to go idle, or should
  365. * reschedule:
  366. */
  367. if (unlikely(need_resched())) {
  368. local_irq_enable();
  369. return;
  370. }
  371. cx = pr->power.state;
  372. if (!cx || acpi_idle_suspend) {
  373. if (pm_idle_save) {
  374. pm_idle_save(); /* enables IRQs */
  375. } else {
  376. acpi_safe_halt();
  377. local_irq_enable();
  378. }
  379. return;
  380. }
  381. /*
  382. * Check BM Activity
  383. * -----------------
  384. * Check for bus mastering activity (if required), record, and check
  385. * for demotion.
  386. */
  387. if (pr->flags.bm_check) {
  388. u32 bm_status = 0;
  389. unsigned long diff = jiffies - pr->power.bm_check_timestamp;
  390. if (diff > 31)
  391. diff = 31;
  392. pr->power.bm_activity <<= diff;
  393. acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  394. if (bm_status) {
  395. pr->power.bm_activity |= 0x1;
  396. acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  397. }
  398. /*
  399. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  400. * the true state of bus mastering activity; forcing us to
  401. * manually check the BMIDEA bit of each IDE channel.
  402. */
  403. else if (errata.piix4.bmisx) {
  404. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  405. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  406. pr->power.bm_activity |= 0x1;
  407. }
  408. pr->power.bm_check_timestamp = jiffies;
  409. /*
  410. * If bus mastering is or was active this jiffy, demote
  411. * to avoid a faulty transition. Note that the processor
  412. * won't enter a low-power state during this call (to this
  413. * function) but should upon the next.
  414. *
  415. * TBD: A better policy might be to fallback to the demotion
  416. * state (use it for this quantum only) istead of
  417. * demoting -- and rely on duration as our sole demotion
  418. * qualification. This may, however, introduce DMA
  419. * issues (e.g. floppy DMA transfer overrun/underrun).
  420. */
  421. if ((pr->power.bm_activity & 0x1) &&
  422. cx->demotion.threshold.bm) {
  423. local_irq_enable();
  424. next_state = cx->demotion.state;
  425. goto end;
  426. }
  427. }
  428. #ifdef CONFIG_HOTPLUG_CPU
  429. /*
  430. * Check for P_LVL2_UP flag before entering C2 and above on
  431. * an SMP system. We do it here instead of doing it at _CST/P_LVL
  432. * detection phase, to work cleanly with logical CPU hotplug.
  433. */
  434. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  435. !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  436. cx = &pr->power.states[ACPI_STATE_C1];
  437. #endif
  438. /*
  439. * Sleep:
  440. * ------
  441. * Invoke the current Cx state to put the processor to sleep.
  442. */
  443. if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
  444. current_thread_info()->status &= ~TS_POLLING;
  445. /*
  446. * TS_POLLING-cleared state must be visible before we
  447. * test NEED_RESCHED:
  448. */
  449. smp_mb();
  450. if (need_resched()) {
  451. current_thread_info()->status |= TS_POLLING;
  452. local_irq_enable();
  453. return;
  454. }
  455. }
  456. switch (cx->type) {
  457. case ACPI_STATE_C1:
  458. /*
  459. * Invoke C1.
  460. * Use the appropriate idle routine, the one that would
  461. * be used without acpi C-states.
  462. */
  463. if (pm_idle_save) {
  464. pm_idle_save(); /* enables IRQs */
  465. } else {
  466. acpi_safe_halt();
  467. local_irq_enable();
  468. }
  469. /*
  470. * TBD: Can't get time duration while in C1, as resumes
  471. * go to an ISR rather than here. Need to instrument
  472. * base interrupt handler.
  473. *
  474. * Note: the TSC better not stop in C1, sched_clock() will
  475. * skew otherwise.
  476. */
  477. sleep_ticks = 0xFFFFFFFF;
  478. break;
  479. case ACPI_STATE_C2:
  480. /* Get start time (ticks) */
  481. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  482. /* Tell the scheduler that we are going deep-idle: */
  483. sched_clock_idle_sleep_event();
  484. /* Invoke C2 */
  485. acpi_state_timer_broadcast(pr, cx, 1);
  486. acpi_cstate_enter(cx);
  487. /* Get end time (ticks) */
  488. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  489. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
  490. /* TSC halts in C2, so notify users */
  491. if (tsc_halts_in_c(ACPI_STATE_C2))
  492. mark_tsc_unstable("possible TSC halt in C2");
  493. #endif
  494. /* Compute time (ticks) that we were actually asleep */
  495. sleep_ticks = ticks_elapsed(t1, t2);
  496. /* Tell the scheduler how much we idled: */
  497. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  498. /* Re-enable interrupts */
  499. local_irq_enable();
  500. /* Do not account our idle-switching overhead: */
  501. sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
  502. current_thread_info()->status |= TS_POLLING;
  503. acpi_state_timer_broadcast(pr, cx, 0);
  504. break;
  505. case ACPI_STATE_C3:
  506. acpi_unlazy_tlb(smp_processor_id());
  507. /*
  508. * Must be done before busmaster disable as we might
  509. * need to access HPET !
  510. */
  511. acpi_state_timer_broadcast(pr, cx, 1);
  512. /*
  513. * disable bus master
  514. * bm_check implies we need ARB_DIS
  515. * !bm_check implies we need cache flush
  516. * bm_control implies whether we can do ARB_DIS
  517. *
  518. * That leaves a case where bm_check is set and bm_control is
  519. * not set. In that case we cannot do much, we enter C3
  520. * without doing anything.
  521. */
  522. if (pr->flags.bm_check && pr->flags.bm_control) {
  523. if (atomic_inc_return(&c3_cpu_count) ==
  524. num_online_cpus()) {
  525. /*
  526. * All CPUs are trying to go to C3
  527. * Disable bus master arbitration
  528. */
  529. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
  530. }
  531. } else if (!pr->flags.bm_check) {
  532. /* SMP with no shared cache... Invalidate cache */
  533. ACPI_FLUSH_CPU_CACHE();
  534. }
  535. /* Get start time (ticks) */
  536. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  537. /* Invoke C3 */
  538. /* Tell the scheduler that we are going deep-idle: */
  539. sched_clock_idle_sleep_event();
  540. acpi_cstate_enter(cx);
  541. /* Get end time (ticks) */
  542. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  543. if (pr->flags.bm_check && pr->flags.bm_control) {
  544. /* Enable bus master arbitration */
  545. atomic_dec(&c3_cpu_count);
  546. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
  547. }
  548. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
  549. /* TSC halts in C3, so notify users */
  550. if (tsc_halts_in_c(ACPI_STATE_C3))
  551. mark_tsc_unstable("TSC halts in C3");
  552. #endif
  553. /* Compute time (ticks) that we were actually asleep */
  554. sleep_ticks = ticks_elapsed(t1, t2);
  555. /* Tell the scheduler how much we idled: */
  556. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  557. /* Re-enable interrupts */
  558. local_irq_enable();
  559. /* Do not account our idle-switching overhead: */
  560. sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
  561. current_thread_info()->status |= TS_POLLING;
  562. acpi_state_timer_broadcast(pr, cx, 0);
  563. break;
  564. default:
  565. local_irq_enable();
  566. return;
  567. }
  568. cx->usage++;
  569. if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
  570. cx->time += sleep_ticks;
  571. next_state = pr->power.state;
  572. #ifdef CONFIG_HOTPLUG_CPU
  573. /* Don't do promotion/demotion */
  574. if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  575. !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
  576. next_state = cx;
  577. goto end;
  578. }
  579. #endif
  580. /*
  581. * Promotion?
  582. * ----------
  583. * Track the number of longs (time asleep is greater than threshold)
  584. * and promote when the count threshold is reached. Note that bus
  585. * mastering activity may prevent promotions.
  586. * Do not promote above max_cstate.
  587. */
  588. if (cx->promotion.state &&
  589. ((cx->promotion.state - pr->power.states) <= max_cstate)) {
  590. if (sleep_ticks > cx->promotion.threshold.ticks &&
  591. cx->promotion.state->latency <=
  592. pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
  593. cx->promotion.count++;
  594. cx->demotion.count = 0;
  595. if (cx->promotion.count >=
  596. cx->promotion.threshold.count) {
  597. if (pr->flags.bm_check) {
  598. if (!
  599. (pr->power.bm_activity & cx->
  600. promotion.threshold.bm)) {
  601. next_state =
  602. cx->promotion.state;
  603. goto end;
  604. }
  605. } else {
  606. next_state = cx->promotion.state;
  607. goto end;
  608. }
  609. }
  610. }
  611. }
  612. /*
  613. * Demotion?
  614. * ---------
  615. * Track the number of shorts (time asleep is less than time threshold)
  616. * and demote when the usage threshold is reached.
  617. */
  618. if (cx->demotion.state) {
  619. if (sleep_ticks < cx->demotion.threshold.ticks) {
  620. cx->demotion.count++;
  621. cx->promotion.count = 0;
  622. if (cx->demotion.count >= cx->demotion.threshold.count) {
  623. next_state = cx->demotion.state;
  624. goto end;
  625. }
  626. }
  627. }
  628. end:
  629. /*
  630. * Demote if current state exceeds max_cstate
  631. * or if the latency of the current state is unacceptable
  632. */
  633. if ((pr->power.state - pr->power.states) > max_cstate ||
  634. pr->power.state->latency >
  635. pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
  636. if (cx->demotion.state)
  637. next_state = cx->demotion.state;
  638. }
  639. /*
  640. * New Cx State?
  641. * -------------
  642. * If we're going to start using a new Cx state we must clean up
  643. * from the previous and prepare to use the new.
  644. */
  645. if (next_state != pr->power.state)
  646. acpi_processor_power_activate(pr, next_state);
  647. }
  648. static int acpi_processor_set_power_policy(struct acpi_processor *pr)
  649. {
  650. unsigned int i;
  651. unsigned int state_is_set = 0;
  652. struct acpi_processor_cx *lower = NULL;
  653. struct acpi_processor_cx *higher = NULL;
  654. struct acpi_processor_cx *cx;
  655. if (!pr)
  656. return -EINVAL;
  657. /*
  658. * This function sets the default Cx state policy (OS idle handler).
  659. * Our scheme is to promote quickly to C2 but more conservatively
  660. * to C3. We're favoring C2 for its characteristics of low latency
  661. * (quick response), good power savings, and ability to allow bus
  662. * mastering activity. Note that the Cx state policy is completely
  663. * customizable and can be altered dynamically.
  664. */
  665. /* startup state */
  666. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  667. cx = &pr->power.states[i];
  668. if (!cx->valid)
  669. continue;
  670. if (!state_is_set)
  671. pr->power.state = cx;
  672. state_is_set++;
  673. break;
  674. }
  675. if (!state_is_set)
  676. return -ENODEV;
  677. /* demotion */
  678. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  679. cx = &pr->power.states[i];
  680. if (!cx->valid)
  681. continue;
  682. if (lower) {
  683. cx->demotion.state = lower;
  684. cx->demotion.threshold.ticks = cx->latency_ticks;
  685. cx->demotion.threshold.count = 1;
  686. if (cx->type == ACPI_STATE_C3)
  687. cx->demotion.threshold.bm = bm_history;
  688. }
  689. lower = cx;
  690. }
  691. /* promotion */
  692. for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
  693. cx = &pr->power.states[i];
  694. if (!cx->valid)
  695. continue;
  696. if (higher) {
  697. cx->promotion.state = higher;
  698. cx->promotion.threshold.ticks = cx->latency_ticks;
  699. if (cx->type >= ACPI_STATE_C2)
  700. cx->promotion.threshold.count = 4;
  701. else
  702. cx->promotion.threshold.count = 10;
  703. if (higher->type == ACPI_STATE_C3)
  704. cx->promotion.threshold.bm = bm_history;
  705. }
  706. higher = cx;
  707. }
  708. return 0;
  709. }
  710. #endif /* !CONFIG_CPU_IDLE */
  711. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  712. {
  713. if (!pr)
  714. return -EINVAL;
  715. if (!pr->pblk)
  716. return -ENODEV;
  717. /* if info is obtained from pblk/fadt, type equals state */
  718. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  719. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  720. #ifndef CONFIG_HOTPLUG_CPU
  721. /*
  722. * Check for P_LVL2_UP flag before entering C2 and above on
  723. * an SMP system.
  724. */
  725. if ((num_online_cpus() > 1) &&
  726. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  727. return -ENODEV;
  728. #endif
  729. /* determine C2 and C3 address from pblk */
  730. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  731. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  732. /* determine latencies from FADT */
  733. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
  734. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
  735. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  736. "lvl2[0x%08x] lvl3[0x%08x]\n",
  737. pr->power.states[ACPI_STATE_C2].address,
  738. pr->power.states[ACPI_STATE_C3].address));
  739. return 0;
  740. }
  741. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  742. {
  743. if (!pr->power.states[ACPI_STATE_C1].valid) {
  744. /* set the first C-State to C1 */
  745. /* all processors need to support C1 */
  746. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  747. pr->power.states[ACPI_STATE_C1].valid = 1;
  748. pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
  749. }
  750. /* the C0 state only exists as a filler in our array */
  751. pr->power.states[ACPI_STATE_C0].valid = 1;
  752. return 0;
  753. }
  754. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  755. {
  756. acpi_status status = 0;
  757. acpi_integer count;
  758. int current_count;
  759. int i;
  760. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  761. union acpi_object *cst;
  762. if (nocst)
  763. return -ENODEV;
  764. current_count = 0;
  765. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  766. if (ACPI_FAILURE(status)) {
  767. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  768. return -ENODEV;
  769. }
  770. cst = buffer.pointer;
  771. /* There must be at least 2 elements */
  772. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  773. printk(KERN_ERR PREFIX "not enough elements in _CST\n");
  774. status = -EFAULT;
  775. goto end;
  776. }
  777. count = cst->package.elements[0].integer.value;
  778. /* Validate number of power states. */
  779. if (count < 1 || count != cst->package.count - 1) {
  780. printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
  781. status = -EFAULT;
  782. goto end;
  783. }
  784. /* Tell driver that at least _CST is supported. */
  785. pr->flags.has_cst = 1;
  786. for (i = 1; i <= count; i++) {
  787. union acpi_object *element;
  788. union acpi_object *obj;
  789. struct acpi_power_register *reg;
  790. struct acpi_processor_cx cx;
  791. memset(&cx, 0, sizeof(cx));
  792. element = &(cst->package.elements[i]);
  793. if (element->type != ACPI_TYPE_PACKAGE)
  794. continue;
  795. if (element->package.count != 4)
  796. continue;
  797. obj = &(element->package.elements[0]);
  798. if (obj->type != ACPI_TYPE_BUFFER)
  799. continue;
  800. reg = (struct acpi_power_register *)obj->buffer.pointer;
  801. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  802. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  803. continue;
  804. /* There should be an easy way to extract an integer... */
  805. obj = &(element->package.elements[1]);
  806. if (obj->type != ACPI_TYPE_INTEGER)
  807. continue;
  808. cx.type = obj->integer.value;
  809. /*
  810. * Some buggy BIOSes won't list C1 in _CST -
  811. * Let acpi_processor_get_power_info_default() handle them later
  812. */
  813. if (i == 1 && cx.type != ACPI_STATE_C1)
  814. current_count++;
  815. cx.address = reg->address;
  816. cx.index = current_count + 1;
  817. cx.entry_method = ACPI_CSTATE_SYSTEMIO;
  818. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  819. if (acpi_processor_ffh_cstate_probe
  820. (pr->id, &cx, reg) == 0) {
  821. cx.entry_method = ACPI_CSTATE_FFH;
  822. } else if (cx.type == ACPI_STATE_C1) {
  823. /*
  824. * C1 is a special case where FIXED_HARDWARE
  825. * can be handled in non-MWAIT way as well.
  826. * In that case, save this _CST entry info.
  827. * Otherwise, ignore this info and continue.
  828. */
  829. cx.entry_method = ACPI_CSTATE_HALT;
  830. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  831. } else {
  832. continue;
  833. }
  834. if (cx.type == ACPI_STATE_C1 &&
  835. (idle_halt || idle_nomwait)) {
  836. /*
  837. * In most cases the C1 space_id obtained from
  838. * _CST object is FIXED_HARDWARE access mode.
  839. * But when the option of idle=halt is added,
  840. * the entry_method type should be changed from
  841. * CSTATE_FFH to CSTATE_HALT.
  842. * When the option of idle=nomwait is added,
  843. * the C1 entry_method type should be
  844. * CSTATE_HALT.
  845. */
  846. cx.entry_method = ACPI_CSTATE_HALT;
  847. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  848. }
  849. } else {
  850. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
  851. cx.address);
  852. }
  853. if (cx.type == ACPI_STATE_C1) {
  854. cx.valid = 1;
  855. }
  856. obj = &(element->package.elements[2]);
  857. if (obj->type != ACPI_TYPE_INTEGER)
  858. continue;
  859. cx.latency = obj->integer.value;
  860. obj = &(element->package.elements[3]);
  861. if (obj->type != ACPI_TYPE_INTEGER)
  862. continue;
  863. cx.power = obj->integer.value;
  864. current_count++;
  865. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  866. /*
  867. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  868. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  869. */
  870. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  871. printk(KERN_WARNING
  872. "Limiting number of power states to max (%d)\n",
  873. ACPI_PROCESSOR_MAX_POWER);
  874. printk(KERN_WARNING
  875. "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  876. break;
  877. }
  878. }
  879. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  880. current_count));
  881. /* Validate number of power states discovered */
  882. if (current_count < 2)
  883. status = -EFAULT;
  884. end:
  885. kfree(buffer.pointer);
  886. return status;
  887. }
  888. static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
  889. {
  890. if (!cx->address)
  891. return;
  892. /*
  893. * C2 latency must be less than or equal to 100
  894. * microseconds.
  895. */
  896. else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  897. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  898. "latency too large [%d]\n", cx->latency));
  899. return;
  900. }
  901. /*
  902. * Otherwise we've met all of our C2 requirements.
  903. * Normalize the C2 latency to expidite policy
  904. */
  905. cx->valid = 1;
  906. #ifndef CONFIG_CPU_IDLE
  907. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  908. #else
  909. cx->latency_ticks = cx->latency;
  910. #endif
  911. return;
  912. }
  913. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  914. struct acpi_processor_cx *cx)
  915. {
  916. static int bm_check_flag;
  917. if (!cx->address)
  918. return;
  919. /*
  920. * C3 latency must be less than or equal to 1000
  921. * microseconds.
  922. */
  923. else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  924. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  925. "latency too large [%d]\n", cx->latency));
  926. return;
  927. }
  928. /*
  929. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  930. * DMA transfers are used by any ISA device to avoid livelock.
  931. * Note that we could disable Type-F DMA (as recommended by
  932. * the erratum), but this is known to disrupt certain ISA
  933. * devices thus we take the conservative approach.
  934. */
  935. else if (errata.piix4.fdma) {
  936. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  937. "C3 not supported on PIIX4 with Type-F DMA\n"));
  938. return;
  939. }
  940. /* All the logic here assumes flags.bm_check is same across all CPUs */
  941. if (!bm_check_flag) {
  942. /* Determine whether bm_check is needed based on CPU */
  943. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  944. bm_check_flag = pr->flags.bm_check;
  945. } else {
  946. pr->flags.bm_check = bm_check_flag;
  947. }
  948. if (pr->flags.bm_check) {
  949. if (!pr->flags.bm_control) {
  950. if (pr->flags.has_cst != 1) {
  951. /* bus mastering control is necessary */
  952. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  953. "C3 support requires BM control\n"));
  954. return;
  955. } else {
  956. /* Here we enter C3 without bus mastering */
  957. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  958. "C3 support without BM control\n"));
  959. }
  960. }
  961. } else {
  962. /*
  963. * WBINVD should be set in fadt, for C3 state to be
  964. * supported on when bm_check is not required.
  965. */
  966. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  967. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  968. "Cache invalidation should work properly"
  969. " for C3 to be enabled on SMP systems\n"));
  970. return;
  971. }
  972. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  973. }
  974. /*
  975. * Otherwise we've met all of our C3 requirements.
  976. * Normalize the C3 latency to expidite policy. Enable
  977. * checking of bus mastering status (bm_check) so we can
  978. * use this in our C3 policy
  979. */
  980. cx->valid = 1;
  981. #ifndef CONFIG_CPU_IDLE
  982. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  983. #else
  984. cx->latency_ticks = cx->latency;
  985. #endif
  986. return;
  987. }
  988. static int acpi_processor_power_verify(struct acpi_processor *pr)
  989. {
  990. unsigned int i;
  991. unsigned int working = 0;
  992. pr->power.timer_broadcast_on_state = INT_MAX;
  993. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  994. struct acpi_processor_cx *cx = &pr->power.states[i];
  995. switch (cx->type) {
  996. case ACPI_STATE_C1:
  997. cx->valid = 1;
  998. break;
  999. case ACPI_STATE_C2:
  1000. acpi_processor_power_verify_c2(cx);
  1001. if (cx->valid)
  1002. acpi_timer_check_state(i, pr, cx);
  1003. break;
  1004. case ACPI_STATE_C3:
  1005. acpi_processor_power_verify_c3(pr, cx);
  1006. if (cx->valid)
  1007. acpi_timer_check_state(i, pr, cx);
  1008. break;
  1009. }
  1010. if (cx->valid)
  1011. working++;
  1012. }
  1013. acpi_propagate_timer_broadcast(pr);
  1014. return (working);
  1015. }
  1016. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  1017. {
  1018. unsigned int i;
  1019. int result;
  1020. /* NOTE: the idle thread may not be running while calling
  1021. * this function */
  1022. /* Zero initialize all the C-states info. */
  1023. memset(pr->power.states, 0, sizeof(pr->power.states));
  1024. result = acpi_processor_get_power_info_cst(pr);
  1025. if (result == -ENODEV)
  1026. result = acpi_processor_get_power_info_fadt(pr);
  1027. if (result)
  1028. return result;
  1029. acpi_processor_get_power_info_default(pr);
  1030. pr->power.count = acpi_processor_power_verify(pr);
  1031. #ifndef CONFIG_CPU_IDLE
  1032. /*
  1033. * Set Default Policy
  1034. * ------------------
  1035. * Now that we know which states are supported, set the default
  1036. * policy. Note that this policy can be changed dynamically
  1037. * (e.g. encourage deeper sleeps to conserve battery life when
  1038. * not on AC).
  1039. */
  1040. result = acpi_processor_set_power_policy(pr);
  1041. if (result)
  1042. return result;
  1043. #endif
  1044. /*
  1045. * if one state of type C2 or C3 is available, mark this
  1046. * CPU as being "idle manageable"
  1047. */
  1048. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  1049. if (pr->power.states[i].valid) {
  1050. pr->power.count = i;
  1051. if (pr->power.states[i].type >= ACPI_STATE_C2)
  1052. pr->flags.power = 1;
  1053. }
  1054. }
  1055. return 0;
  1056. }
  1057. static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
  1058. {
  1059. struct acpi_processor *pr = seq->private;
  1060. unsigned int i;
  1061. if (!pr)
  1062. goto end;
  1063. seq_printf(seq, "active state: C%zd\n"
  1064. "max_cstate: C%d\n"
  1065. "bus master activity: %08x\n"
  1066. "maximum allowed latency: %d usec\n",
  1067. pr->power.state ? pr->power.state - pr->power.states : 0,
  1068. max_cstate, (unsigned)pr->power.bm_activity,
  1069. pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
  1070. seq_puts(seq, "states:\n");
  1071. for (i = 1; i <= pr->power.count; i++) {
  1072. seq_printf(seq, " %cC%d: ",
  1073. (&pr->power.states[i] ==
  1074. pr->power.state ? '*' : ' '), i);
  1075. if (!pr->power.states[i].valid) {
  1076. seq_puts(seq, "<not supported>\n");
  1077. continue;
  1078. }
  1079. switch (pr->power.states[i].type) {
  1080. case ACPI_STATE_C1:
  1081. seq_printf(seq, "type[C1] ");
  1082. break;
  1083. case ACPI_STATE_C2:
  1084. seq_printf(seq, "type[C2] ");
  1085. break;
  1086. case ACPI_STATE_C3:
  1087. seq_printf(seq, "type[C3] ");
  1088. break;
  1089. default:
  1090. seq_printf(seq, "type[--] ");
  1091. break;
  1092. }
  1093. if (pr->power.states[i].promotion.state)
  1094. seq_printf(seq, "promotion[C%zd] ",
  1095. (pr->power.states[i].promotion.state -
  1096. pr->power.states));
  1097. else
  1098. seq_puts(seq, "promotion[--] ");
  1099. if (pr->power.states[i].demotion.state)
  1100. seq_printf(seq, "demotion[C%zd] ",
  1101. (pr->power.states[i].demotion.state -
  1102. pr->power.states));
  1103. else
  1104. seq_puts(seq, "demotion[--] ");
  1105. seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
  1106. pr->power.states[i].latency,
  1107. pr->power.states[i].usage,
  1108. (unsigned long long)pr->power.states[i].time);
  1109. }
  1110. end:
  1111. return 0;
  1112. }
  1113. static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
  1114. {
  1115. return single_open(file, acpi_processor_power_seq_show,
  1116. PDE(inode)->data);
  1117. }
  1118. static const struct file_operations acpi_processor_power_fops = {
  1119. .owner = THIS_MODULE,
  1120. .open = acpi_processor_power_open_fs,
  1121. .read = seq_read,
  1122. .llseek = seq_lseek,
  1123. .release = single_release,
  1124. };
  1125. #ifndef CONFIG_CPU_IDLE
  1126. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  1127. {
  1128. int result = 0;
  1129. if (boot_option_idle_override)
  1130. return 0;
  1131. if (!pr)
  1132. return -EINVAL;
  1133. if (nocst) {
  1134. return -ENODEV;
  1135. }
  1136. if (!pr->flags.power_setup_done)
  1137. return -ENODEV;
  1138. /*
  1139. * Fall back to the default idle loop, when pm_idle_save had
  1140. * been initialized.
  1141. */
  1142. if (pm_idle_save) {
  1143. pm_idle = pm_idle_save;
  1144. /* Relies on interrupts forcing exit from idle. */
  1145. synchronize_sched();
  1146. }
  1147. pr->flags.power = 0;
  1148. result = acpi_processor_get_power_info(pr);
  1149. if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
  1150. pm_idle = acpi_processor_idle;
  1151. return result;
  1152. }
  1153. #ifdef CONFIG_SMP
  1154. static void smp_callback(void *v)
  1155. {
  1156. /* we already woke the CPU up, nothing more to do */
  1157. }
  1158. /*
  1159. * This function gets called when a part of the kernel has a new latency
  1160. * requirement. This means we need to get all processors out of their C-state,
  1161. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  1162. * wakes them all right up.
  1163. */
  1164. static int acpi_processor_latency_notify(struct notifier_block *b,
  1165. unsigned long l, void *v)
  1166. {
  1167. smp_call_function(smp_callback, NULL, 1);
  1168. return NOTIFY_OK;
  1169. }
  1170. static struct notifier_block acpi_processor_latency_notifier = {
  1171. .notifier_call = acpi_processor_latency_notify,
  1172. };
  1173. #endif
  1174. #else /* CONFIG_CPU_IDLE */
  1175. /**
  1176. * acpi_idle_bm_check - checks if bus master activity was detected
  1177. */
  1178. static int acpi_idle_bm_check(void)
  1179. {
  1180. u32 bm_status = 0;
  1181. acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  1182. if (bm_status)
  1183. acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  1184. /*
  1185. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  1186. * the true state of bus mastering activity; forcing us to
  1187. * manually check the BMIDEA bit of each IDE channel.
  1188. */
  1189. else if (errata.piix4.bmisx) {
  1190. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  1191. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  1192. bm_status = 1;
  1193. }
  1194. return bm_status;
  1195. }
  1196. /**
  1197. * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
  1198. * @pr: the processor
  1199. * @target: the new target state
  1200. */
  1201. static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
  1202. struct acpi_processor_cx *target)
  1203. {
  1204. if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
  1205. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  1206. pr->flags.bm_rld_set = 0;
  1207. }
  1208. if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
  1209. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  1210. pr->flags.bm_rld_set = 1;
  1211. }
  1212. }
  1213. /**
  1214. * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
  1215. * @cx: cstate data
  1216. *
  1217. * Caller disables interrupt before call and enables interrupt after return.
  1218. */
  1219. static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
  1220. {
  1221. /* Don't trace irqs off for idle */
  1222. stop_critical_timings();
  1223. if (cx->entry_method == ACPI_CSTATE_FFH) {
  1224. /* Call into architectural FFH based C-state */
  1225. acpi_processor_ffh_cstate_enter(cx);
  1226. } else if (cx->entry_method == ACPI_CSTATE_HALT) {
  1227. acpi_safe_halt();
  1228. } else {
  1229. int unused;
  1230. /* IO port based C-state */
  1231. inb(cx->address);
  1232. /* Dummy wait op - must do something useless after P_LVL2 read
  1233. because chipsets cannot guarantee that STPCLK# signal
  1234. gets asserted in time to freeze execution properly. */
  1235. unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1236. }
  1237. start_critical_timings();
  1238. }
  1239. /**
  1240. * acpi_idle_enter_c1 - enters an ACPI C1 state-type
  1241. * @dev: the target CPU
  1242. * @state: the state data
  1243. *
  1244. * This is equivalent to the HALT instruction.
  1245. */
  1246. static int acpi_idle_enter_c1(struct cpuidle_device *dev,
  1247. struct cpuidle_state *state)
  1248. {
  1249. u32 t1, t2;
  1250. struct acpi_processor *pr;
  1251. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  1252. pr = __get_cpu_var(processors);
  1253. if (unlikely(!pr))
  1254. return 0;
  1255. local_irq_disable();
  1256. /* Do not access any ACPI IO ports in suspend path */
  1257. if (acpi_idle_suspend) {
  1258. acpi_safe_halt();
  1259. local_irq_enable();
  1260. return 0;
  1261. }
  1262. if (pr->flags.bm_check)
  1263. acpi_idle_update_bm_rld(pr, cx);
  1264. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1265. acpi_idle_do_entry(cx);
  1266. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1267. local_irq_enable();
  1268. cx->usage++;
  1269. return ticks_elapsed_in_us(t1, t2);
  1270. }
  1271. /**
  1272. * acpi_idle_enter_simple - enters an ACPI state without BM handling
  1273. * @dev: the target CPU
  1274. * @state: the state data
  1275. */
  1276. static int acpi_idle_enter_simple(struct cpuidle_device *dev,
  1277. struct cpuidle_state *state)
  1278. {
  1279. struct acpi_processor *pr;
  1280. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  1281. u32 t1, t2;
  1282. int sleep_ticks = 0;
  1283. pr = __get_cpu_var(processors);
  1284. if (unlikely(!pr))
  1285. return 0;
  1286. if (acpi_idle_suspend)
  1287. return(acpi_idle_enter_c1(dev, state));
  1288. local_irq_disable();
  1289. current_thread_info()->status &= ~TS_POLLING;
  1290. /*
  1291. * TS_POLLING-cleared state must be visible before we test
  1292. * NEED_RESCHED:
  1293. */
  1294. smp_mb();
  1295. if (unlikely(need_resched())) {
  1296. current_thread_info()->status |= TS_POLLING;
  1297. local_irq_enable();
  1298. return 0;
  1299. }
  1300. /*
  1301. * Must be done before busmaster disable as we might need to
  1302. * access HPET !
  1303. */
  1304. acpi_state_timer_broadcast(pr, cx, 1);
  1305. if (pr->flags.bm_check)
  1306. acpi_idle_update_bm_rld(pr, cx);
  1307. if (cx->type == ACPI_STATE_C3)
  1308. ACPI_FLUSH_CPU_CACHE();
  1309. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1310. /* Tell the scheduler that we are going deep-idle: */
  1311. sched_clock_idle_sleep_event();
  1312. acpi_idle_do_entry(cx);
  1313. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1314. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
  1315. /* TSC could halt in idle, so notify users */
  1316. if (tsc_halts_in_c(cx->type))
  1317. mark_tsc_unstable("TSC halts in idle");;
  1318. #endif
  1319. sleep_ticks = ticks_elapsed(t1, t2);
  1320. /* Tell the scheduler how much we idled: */
  1321. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  1322. local_irq_enable();
  1323. current_thread_info()->status |= TS_POLLING;
  1324. cx->usage++;
  1325. acpi_state_timer_broadcast(pr, cx, 0);
  1326. cx->time += sleep_ticks;
  1327. return ticks_elapsed_in_us(t1, t2);
  1328. }
  1329. static int c3_cpu_count;
  1330. static DEFINE_SPINLOCK(c3_lock);
  1331. /**
  1332. * acpi_idle_enter_bm - enters C3 with proper BM handling
  1333. * @dev: the target CPU
  1334. * @state: the state data
  1335. *
  1336. * If BM is detected, the deepest non-C3 idle state is entered instead.
  1337. */
  1338. static int acpi_idle_enter_bm(struct cpuidle_device *dev,
  1339. struct cpuidle_state *state)
  1340. {
  1341. struct acpi_processor *pr;
  1342. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  1343. u32 t1, t2;
  1344. int sleep_ticks = 0;
  1345. pr = __get_cpu_var(processors);
  1346. if (unlikely(!pr))
  1347. return 0;
  1348. if (acpi_idle_suspend)
  1349. return(acpi_idle_enter_c1(dev, state));
  1350. if (acpi_idle_bm_check()) {
  1351. if (dev->safe_state) {
  1352. dev->last_state = dev->safe_state;
  1353. return dev->safe_state->enter(dev, dev->safe_state);
  1354. } else {
  1355. local_irq_disable();
  1356. acpi_safe_halt();
  1357. local_irq_enable();
  1358. return 0;
  1359. }
  1360. }
  1361. local_irq_disable();
  1362. current_thread_info()->status &= ~TS_POLLING;
  1363. /*
  1364. * TS_POLLING-cleared state must be visible before we test
  1365. * NEED_RESCHED:
  1366. */
  1367. smp_mb();
  1368. if (unlikely(need_resched())) {
  1369. current_thread_info()->status |= TS_POLLING;
  1370. local_irq_enable();
  1371. return 0;
  1372. }
  1373. acpi_unlazy_tlb(smp_processor_id());
  1374. /* Tell the scheduler that we are going deep-idle: */
  1375. sched_clock_idle_sleep_event();
  1376. /*
  1377. * Must be done before busmaster disable as we might need to
  1378. * access HPET !
  1379. */
  1380. acpi_state_timer_broadcast(pr, cx, 1);
  1381. acpi_idle_update_bm_rld(pr, cx);
  1382. /*
  1383. * disable bus master
  1384. * bm_check implies we need ARB_DIS
  1385. * !bm_check implies we need cache flush
  1386. * bm_control implies whether we can do ARB_DIS
  1387. *
  1388. * That leaves a case where bm_check is set and bm_control is
  1389. * not set. In that case we cannot do much, we enter C3
  1390. * without doing anything.
  1391. */
  1392. if (pr->flags.bm_check && pr->flags.bm_control) {
  1393. spin_lock(&c3_lock);
  1394. c3_cpu_count++;
  1395. /* Disable bus master arbitration when all CPUs are in C3 */
  1396. if (c3_cpu_count == num_online_cpus())
  1397. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
  1398. spin_unlock(&c3_lock);
  1399. } else if (!pr->flags.bm_check) {
  1400. ACPI_FLUSH_CPU_CACHE();
  1401. }
  1402. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1403. acpi_idle_do_entry(cx);
  1404. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1405. /* Re-enable bus master arbitration */
  1406. if (pr->flags.bm_check && pr->flags.bm_control) {
  1407. spin_lock(&c3_lock);
  1408. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
  1409. c3_cpu_count--;
  1410. spin_unlock(&c3_lock);
  1411. }
  1412. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
  1413. /* TSC could halt in idle, so notify users */
  1414. if (tsc_halts_in_c(ACPI_STATE_C3))
  1415. mark_tsc_unstable("TSC halts in idle");
  1416. #endif
  1417. sleep_ticks = ticks_elapsed(t1, t2);
  1418. /* Tell the scheduler how much we idled: */
  1419. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  1420. local_irq_enable();
  1421. current_thread_info()->status |= TS_POLLING;
  1422. cx->usage++;
  1423. acpi_state_timer_broadcast(pr, cx, 0);
  1424. cx->time += sleep_ticks;
  1425. return ticks_elapsed_in_us(t1, t2);
  1426. }
  1427. struct cpuidle_driver acpi_idle_driver = {
  1428. .name = "acpi_idle",
  1429. .owner = THIS_MODULE,
  1430. };
  1431. /**
  1432. * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
  1433. * @pr: the ACPI processor
  1434. */
  1435. static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
  1436. {
  1437. int i, count = CPUIDLE_DRIVER_STATE_START;
  1438. struct acpi_processor_cx *cx;
  1439. struct cpuidle_state *state;
  1440. struct cpuidle_device *dev = &pr->power.dev;
  1441. if (!pr->flags.power_setup_done)
  1442. return -EINVAL;
  1443. if (pr->flags.power == 0) {
  1444. return -EINVAL;
  1445. }
  1446. dev->cpu = pr->id;
  1447. for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
  1448. dev->states[i].name[0] = '\0';
  1449. dev->states[i].desc[0] = '\0';
  1450. }
  1451. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  1452. cx = &pr->power.states[i];
  1453. state = &dev->states[count];
  1454. if (!cx->valid)
  1455. continue;
  1456. #ifdef CONFIG_HOTPLUG_CPU
  1457. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  1458. !pr->flags.has_cst &&
  1459. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  1460. continue;
  1461. #endif
  1462. cpuidle_set_statedata(state, cx);
  1463. snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
  1464. strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
  1465. state->exit_latency = cx->latency;
  1466. state->target_residency = cx->latency * latency_factor;
  1467. state->power_usage = cx->power;
  1468. state->flags = 0;
  1469. switch (cx->type) {
  1470. case ACPI_STATE_C1:
  1471. state->flags |= CPUIDLE_FLAG_SHALLOW;
  1472. if (cx->entry_method == ACPI_CSTATE_FFH)
  1473. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  1474. state->enter = acpi_idle_enter_c1;
  1475. dev->safe_state = state;
  1476. break;
  1477. case ACPI_STATE_C2:
  1478. state->flags |= CPUIDLE_FLAG_BALANCED;
  1479. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  1480. state->enter = acpi_idle_enter_simple;
  1481. dev->safe_state = state;
  1482. break;
  1483. case ACPI_STATE_C3:
  1484. state->flags |= CPUIDLE_FLAG_DEEP;
  1485. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  1486. state->flags |= CPUIDLE_FLAG_CHECK_BM;
  1487. state->enter = pr->flags.bm_check ?
  1488. acpi_idle_enter_bm :
  1489. acpi_idle_enter_simple;
  1490. break;
  1491. }
  1492. count++;
  1493. if (count == CPUIDLE_STATE_MAX)
  1494. break;
  1495. }
  1496. dev->state_count = count;
  1497. if (!count)
  1498. return -EINVAL;
  1499. return 0;
  1500. }
  1501. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  1502. {
  1503. int ret = 0;
  1504. if (boot_option_idle_override)
  1505. return 0;
  1506. if (!pr)
  1507. return -EINVAL;
  1508. if (nocst) {
  1509. return -ENODEV;
  1510. }
  1511. if (!pr->flags.power_setup_done)
  1512. return -ENODEV;
  1513. cpuidle_pause_and_lock();
  1514. cpuidle_disable_device(&pr->power.dev);
  1515. acpi_processor_get_power_info(pr);
  1516. if (pr->flags.power) {
  1517. acpi_processor_setup_cpuidle(pr);
  1518. ret = cpuidle_enable_device(&pr->power.dev);
  1519. }
  1520. cpuidle_resume_and_unlock();
  1521. return ret;
  1522. }
  1523. #endif /* CONFIG_CPU_IDLE */
  1524. int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
  1525. struct acpi_device *device)
  1526. {
  1527. acpi_status status = 0;
  1528. static int first_run;
  1529. struct proc_dir_entry *entry = NULL;
  1530. unsigned int i;
  1531. if (boot_option_idle_override)
  1532. return 0;
  1533. if (!first_run) {
  1534. if (idle_halt) {
  1535. /*
  1536. * When the boot option of "idle=halt" is added, halt
  1537. * is used for CPU IDLE.
  1538. * In such case C2/C3 is meaningless. So the max_cstate
  1539. * is set to one.
  1540. */
  1541. max_cstate = 1;
  1542. }
  1543. dmi_check_system(processor_power_dmi_table);
  1544. max_cstate = acpi_processor_cstate_check(max_cstate);
  1545. if (max_cstate < ACPI_C_STATES_MAX)
  1546. printk(KERN_NOTICE
  1547. "ACPI: processor limited to max C-state %d\n",
  1548. max_cstate);
  1549. first_run++;
  1550. #if !defined(CONFIG_CPU_IDLE) && defined(CONFIG_SMP)
  1551. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY,
  1552. &acpi_processor_latency_notifier);
  1553. #endif
  1554. }
  1555. if (!pr)
  1556. return -EINVAL;
  1557. if (acpi_gbl_FADT.cst_control && !nocst) {
  1558. status =
  1559. acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
  1560. if (ACPI_FAILURE(status)) {
  1561. ACPI_EXCEPTION((AE_INFO, status,
  1562. "Notifying BIOS of _CST ability failed"));
  1563. }
  1564. }
  1565. acpi_processor_get_power_info(pr);
  1566. pr->flags.power_setup_done = 1;
  1567. /*
  1568. * Install the idle handler if processor power management is supported.
  1569. * Note that we use previously set idle handler will be used on
  1570. * platforms that only support C1.
  1571. */
  1572. if (pr->flags.power) {
  1573. #ifdef CONFIG_CPU_IDLE
  1574. acpi_processor_setup_cpuidle(pr);
  1575. if (cpuidle_register_device(&pr->power.dev))
  1576. return -EIO;
  1577. #endif
  1578. printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
  1579. for (i = 1; i <= pr->power.count; i++)
  1580. if (pr->power.states[i].valid)
  1581. printk(" C%d[C%d]", i,
  1582. pr->power.states[i].type);
  1583. printk(")\n");
  1584. #ifndef CONFIG_CPU_IDLE
  1585. if (pr->id == 0) {
  1586. pm_idle_save = pm_idle;
  1587. pm_idle = acpi_processor_idle;
  1588. }
  1589. #endif
  1590. }
  1591. /* 'power' [R] */
  1592. entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
  1593. S_IRUGO, acpi_device_dir(device),
  1594. &acpi_processor_power_fops,
  1595. acpi_driver_data(device));
  1596. if (!entry)
  1597. return -EIO;
  1598. return 0;
  1599. }
  1600. int acpi_processor_power_exit(struct acpi_processor *pr,
  1601. struct acpi_device *device)
  1602. {
  1603. if (boot_option_idle_override)
  1604. return 0;
  1605. #ifdef CONFIG_CPU_IDLE
  1606. cpuidle_unregister_device(&pr->power.dev);
  1607. #endif
  1608. pr->flags.power_setup_done = 0;
  1609. if (acpi_device_dir(device))
  1610. remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1611. acpi_device_dir(device));
  1612. #ifndef CONFIG_CPU_IDLE
  1613. /* Unregister the idle handler when processor #0 is removed. */
  1614. if (pr->id == 0) {
  1615. if (pm_idle_save)
  1616. pm_idle = pm_idle_save;
  1617. /*
  1618. * We are about to unload the current idle thread pm callback
  1619. * (pm_idle), Wait for all processors to update cached/local
  1620. * copies of pm_idle before proceeding.
  1621. */
  1622. cpu_idle_wait();
  1623. #ifdef CONFIG_SMP
  1624. pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY,
  1625. &acpi_processor_latency_notifier);
  1626. #endif
  1627. }
  1628. #endif
  1629. return 0;
  1630. }