processor_idle.c 49 KB

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