processor_idle.c 49 KB

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