processor_idle.c 46 KB

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