processor_idle.c 46 KB

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