processor_idle.c 47 KB

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