processor_idle.c 47 KB

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