processor_idle.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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/slab.h>
  35. #include <linux/acpi.h>
  36. #include <linux/dmi.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/sched.h> /* need_resched() */
  39. #include <linux/pm_qos.h>
  40. #include <linux/clockchips.h>
  41. #include <linux/cpuidle.h>
  42. #include <linux/irqflags.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 PREFIX "ACPI: "
  58. #define ACPI_PROCESSOR_CLASS "processor"
  59. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  60. ACPI_MODULE_NAME("processor_idle");
  61. #define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
  62. #define C2_OVERHEAD 1 /* 1us */
  63. #define C3_OVERHEAD 1 /* 1us */
  64. #define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
  65. static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
  66. module_param(max_cstate, uint, 0000);
  67. static unsigned int nocst __read_mostly;
  68. module_param(nocst, uint, 0000);
  69. static int bm_check_disable __read_mostly;
  70. module_param(bm_check_disable, uint, 0000);
  71. static unsigned int latency_factor __read_mostly = 2;
  72. module_param(latency_factor, uint, 0644);
  73. static int disabled_by_idle_boot_param(void)
  74. {
  75. return boot_option_idle_override == IDLE_POLL ||
  76. boot_option_idle_override == IDLE_FORCE_MWAIT ||
  77. boot_option_idle_override == IDLE_HALT;
  78. }
  79. /*
  80. * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
  81. * For now disable this. Probably a bug somewhere else.
  82. *
  83. * To skip this limit, boot/load with a large max_cstate limit.
  84. */
  85. static int set_max_cstate(const struct dmi_system_id *id)
  86. {
  87. if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
  88. return 0;
  89. printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
  90. " Override with \"processor.max_cstate=%d\"\n", id->ident,
  91. (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
  92. max_cstate = (long)id->driver_data;
  93. return 0;
  94. }
  95. /* Actually this shouldn't be __cpuinitdata, would be better to fix the
  96. callers to only run once -AK */
  97. static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
  98. { set_max_cstate, "Clevo 5600D", {
  99. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  100. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  101. (void *)2},
  102. { set_max_cstate, "Pavilion zv5000", {
  103. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  104. DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
  105. (void *)1},
  106. { set_max_cstate, "Asus L8400B", {
  107. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  108. DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
  109. (void *)1},
  110. {},
  111. };
  112. /*
  113. * Callers should disable interrupts before the call and enable
  114. * interrupts after return.
  115. */
  116. static void acpi_safe_halt(void)
  117. {
  118. current_thread_info()->status &= ~TS_POLLING;
  119. /*
  120. * TS_POLLING-cleared state must be visible before we
  121. * test NEED_RESCHED:
  122. */
  123. smp_mb();
  124. if (!need_resched()) {
  125. safe_halt();
  126. local_irq_disable();
  127. }
  128. current_thread_info()->status |= TS_POLLING;
  129. }
  130. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  131. /*
  132. * Some BIOS implementations switch to C3 in the published C2 state.
  133. * This seems to be a common problem on AMD boxen, but other vendors
  134. * are affected too. We pick the most conservative approach: we assume
  135. * that the local APIC stops in both C2 and C3.
  136. */
  137. static void lapic_timer_check_state(int state, struct acpi_processor *pr,
  138. struct acpi_processor_cx *cx)
  139. {
  140. struct acpi_processor_power *pwr = &pr->power;
  141. u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
  142. if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
  143. return;
  144. if (amd_e400_c1e_detected)
  145. type = ACPI_STATE_C1;
  146. /*
  147. * Check, if one of the previous states already marked the lapic
  148. * unstable
  149. */
  150. if (pwr->timer_broadcast_on_state < state)
  151. return;
  152. if (cx->type >= type)
  153. pr->power.timer_broadcast_on_state = state;
  154. }
  155. static void __lapic_timer_propagate_broadcast(void *arg)
  156. {
  157. struct acpi_processor *pr = (struct acpi_processor *) arg;
  158. unsigned long reason;
  159. reason = pr->power.timer_broadcast_on_state < INT_MAX ?
  160. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  161. clockevents_notify(reason, &pr->id);
  162. }
  163. static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
  164. {
  165. smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
  166. (void *)pr, 1);
  167. }
  168. /* Power(C) State timer broadcast control */
  169. static void lapic_timer_state_broadcast(struct acpi_processor *pr,
  170. struct acpi_processor_cx *cx,
  171. int broadcast)
  172. {
  173. int state = cx - pr->power.states;
  174. if (state >= pr->power.timer_broadcast_on_state) {
  175. unsigned long reason;
  176. reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
  177. CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
  178. clockevents_notify(reason, &pr->id);
  179. }
  180. }
  181. #else
  182. static void lapic_timer_check_state(int state, struct acpi_processor *pr,
  183. struct acpi_processor_cx *cstate) { }
  184. static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
  185. static void lapic_timer_state_broadcast(struct acpi_processor *pr,
  186. struct acpi_processor_cx *cx,
  187. int broadcast)
  188. {
  189. }
  190. #endif
  191. /*
  192. * Suspend / resume control
  193. */
  194. static int acpi_idle_suspend;
  195. static u32 saved_bm_rld;
  196. static void acpi_idle_bm_rld_save(void)
  197. {
  198. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
  199. }
  200. static void acpi_idle_bm_rld_restore(void)
  201. {
  202. u32 resumed_bm_rld;
  203. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
  204. if (resumed_bm_rld != saved_bm_rld)
  205. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
  206. }
  207. int acpi_processor_suspend(struct device *dev)
  208. {
  209. if (acpi_idle_suspend == 1)
  210. return 0;
  211. acpi_idle_bm_rld_save();
  212. acpi_idle_suspend = 1;
  213. return 0;
  214. }
  215. int acpi_processor_resume(struct device *dev)
  216. {
  217. if (acpi_idle_suspend == 0)
  218. return 0;
  219. acpi_idle_bm_rld_restore();
  220. acpi_idle_suspend = 0;
  221. return 0;
  222. }
  223. #if defined(CONFIG_X86)
  224. static void tsc_check_state(int state)
  225. {
  226. switch (boot_cpu_data.x86_vendor) {
  227. case X86_VENDOR_AMD:
  228. case X86_VENDOR_INTEL:
  229. /*
  230. * AMD Fam10h TSC will tick in all
  231. * C/P/S0/S1 states when this bit is set.
  232. */
  233. if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  234. return;
  235. /*FALL THROUGH*/
  236. default:
  237. /* TSC could halt in idle, so notify users */
  238. if (state > ACPI_STATE_C1)
  239. mark_tsc_unstable("TSC halts in idle");
  240. }
  241. }
  242. #else
  243. static void tsc_check_state(int state) { return; }
  244. #endif
  245. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  246. {
  247. if (!pr)
  248. return -EINVAL;
  249. if (!pr->pblk)
  250. return -ENODEV;
  251. /* if info is obtained from pblk/fadt, type equals state */
  252. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  253. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  254. #ifndef CONFIG_HOTPLUG_CPU
  255. /*
  256. * Check for P_LVL2_UP flag before entering C2 and above on
  257. * an SMP system.
  258. */
  259. if ((num_online_cpus() > 1) &&
  260. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  261. return -ENODEV;
  262. #endif
  263. /* determine C2 and C3 address from pblk */
  264. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  265. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  266. /* determine latencies from FADT */
  267. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
  268. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
  269. /*
  270. * FADT specified C2 latency must be less than or equal to
  271. * 100 microseconds.
  272. */
  273. if (acpi_gbl_FADT.C2latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  274. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  275. "C2 latency too large [%d]\n", acpi_gbl_FADT.C2latency));
  276. /* invalidate C2 */
  277. pr->power.states[ACPI_STATE_C2].address = 0;
  278. }
  279. /*
  280. * FADT supplied C3 latency must be less than or equal to
  281. * 1000 microseconds.
  282. */
  283. if (acpi_gbl_FADT.C3latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  284. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  285. "C3 latency too large [%d]\n", acpi_gbl_FADT.C3latency));
  286. /* invalidate C3 */
  287. pr->power.states[ACPI_STATE_C3].address = 0;
  288. }
  289. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  290. "lvl2[0x%08x] lvl3[0x%08x]\n",
  291. pr->power.states[ACPI_STATE_C2].address,
  292. pr->power.states[ACPI_STATE_C3].address));
  293. return 0;
  294. }
  295. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  296. {
  297. if (!pr->power.states[ACPI_STATE_C1].valid) {
  298. /* set the first C-State to C1 */
  299. /* all processors need to support C1 */
  300. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  301. pr->power.states[ACPI_STATE_C1].valid = 1;
  302. pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
  303. }
  304. /* the C0 state only exists as a filler in our array */
  305. pr->power.states[ACPI_STATE_C0].valid = 1;
  306. return 0;
  307. }
  308. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  309. {
  310. acpi_status status = 0;
  311. u64 count;
  312. int current_count;
  313. int i;
  314. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  315. union acpi_object *cst;
  316. if (nocst)
  317. return -ENODEV;
  318. current_count = 0;
  319. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  320. if (ACPI_FAILURE(status)) {
  321. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  322. return -ENODEV;
  323. }
  324. cst = buffer.pointer;
  325. /* There must be at least 2 elements */
  326. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  327. printk(KERN_ERR PREFIX "not enough elements in _CST\n");
  328. status = -EFAULT;
  329. goto end;
  330. }
  331. count = cst->package.elements[0].integer.value;
  332. /* Validate number of power states. */
  333. if (count < 1 || count != cst->package.count - 1) {
  334. printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
  335. status = -EFAULT;
  336. goto end;
  337. }
  338. /* Tell driver that at least _CST is supported. */
  339. pr->flags.has_cst = 1;
  340. for (i = 1; i <= count; i++) {
  341. union acpi_object *element;
  342. union acpi_object *obj;
  343. struct acpi_power_register *reg;
  344. struct acpi_processor_cx cx;
  345. memset(&cx, 0, sizeof(cx));
  346. element = &(cst->package.elements[i]);
  347. if (element->type != ACPI_TYPE_PACKAGE)
  348. continue;
  349. if (element->package.count != 4)
  350. continue;
  351. obj = &(element->package.elements[0]);
  352. if (obj->type != ACPI_TYPE_BUFFER)
  353. continue;
  354. reg = (struct acpi_power_register *)obj->buffer.pointer;
  355. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  356. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  357. continue;
  358. /* There should be an easy way to extract an integer... */
  359. obj = &(element->package.elements[1]);
  360. if (obj->type != ACPI_TYPE_INTEGER)
  361. continue;
  362. cx.type = obj->integer.value;
  363. /*
  364. * Some buggy BIOSes won't list C1 in _CST -
  365. * Let acpi_processor_get_power_info_default() handle them later
  366. */
  367. if (i == 1 && cx.type != ACPI_STATE_C1)
  368. current_count++;
  369. cx.address = reg->address;
  370. cx.index = current_count + 1;
  371. cx.entry_method = ACPI_CSTATE_SYSTEMIO;
  372. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  373. if (acpi_processor_ffh_cstate_probe
  374. (pr->id, &cx, reg) == 0) {
  375. cx.entry_method = ACPI_CSTATE_FFH;
  376. } else if (cx.type == ACPI_STATE_C1) {
  377. /*
  378. * C1 is a special case where FIXED_HARDWARE
  379. * can be handled in non-MWAIT way as well.
  380. * In that case, save this _CST entry info.
  381. * Otherwise, ignore this info and continue.
  382. */
  383. cx.entry_method = ACPI_CSTATE_HALT;
  384. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  385. } else {
  386. continue;
  387. }
  388. if (cx.type == ACPI_STATE_C1 &&
  389. (boot_option_idle_override == IDLE_NOMWAIT)) {
  390. /*
  391. * In most cases the C1 space_id obtained from
  392. * _CST object is FIXED_HARDWARE access mode.
  393. * But when the option of idle=halt is added,
  394. * the entry_method type should be changed from
  395. * CSTATE_FFH to CSTATE_HALT.
  396. * When the option of idle=nomwait is added,
  397. * the C1 entry_method type should be
  398. * CSTATE_HALT.
  399. */
  400. cx.entry_method = ACPI_CSTATE_HALT;
  401. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  402. }
  403. } else {
  404. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
  405. cx.address);
  406. }
  407. if (cx.type == ACPI_STATE_C1) {
  408. cx.valid = 1;
  409. }
  410. obj = &(element->package.elements[2]);
  411. if (obj->type != ACPI_TYPE_INTEGER)
  412. continue;
  413. cx.latency = obj->integer.value;
  414. obj = &(element->package.elements[3]);
  415. if (obj->type != ACPI_TYPE_INTEGER)
  416. continue;
  417. cx.power = obj->integer.value;
  418. current_count++;
  419. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  420. /*
  421. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  422. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  423. */
  424. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  425. printk(KERN_WARNING
  426. "Limiting number of power states to max (%d)\n",
  427. ACPI_PROCESSOR_MAX_POWER);
  428. printk(KERN_WARNING
  429. "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  430. break;
  431. }
  432. }
  433. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  434. current_count));
  435. /* Validate number of power states discovered */
  436. if (current_count < 2)
  437. status = -EFAULT;
  438. end:
  439. kfree(buffer.pointer);
  440. return status;
  441. }
  442. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  443. struct acpi_processor_cx *cx)
  444. {
  445. static int bm_check_flag = -1;
  446. static int bm_control_flag = -1;
  447. if (!cx->address)
  448. return;
  449. /*
  450. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  451. * DMA transfers are used by any ISA device to avoid livelock.
  452. * Note that we could disable Type-F DMA (as recommended by
  453. * the erratum), but this is known to disrupt certain ISA
  454. * devices thus we take the conservative approach.
  455. */
  456. else if (errata.piix4.fdma) {
  457. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  458. "C3 not supported on PIIX4 with Type-F DMA\n"));
  459. return;
  460. }
  461. /* All the logic here assumes flags.bm_check is same across all CPUs */
  462. if (bm_check_flag == -1) {
  463. /* Determine whether bm_check is needed based on CPU */
  464. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  465. bm_check_flag = pr->flags.bm_check;
  466. bm_control_flag = pr->flags.bm_control;
  467. } else {
  468. pr->flags.bm_check = bm_check_flag;
  469. pr->flags.bm_control = bm_control_flag;
  470. }
  471. if (pr->flags.bm_check) {
  472. if (!pr->flags.bm_control) {
  473. if (pr->flags.has_cst != 1) {
  474. /* bus mastering control is necessary */
  475. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  476. "C3 support requires BM control\n"));
  477. return;
  478. } else {
  479. /* Here we enter C3 without bus mastering */
  480. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  481. "C3 support without BM control\n"));
  482. }
  483. }
  484. } else {
  485. /*
  486. * WBINVD should be set in fadt, for C3 state to be
  487. * supported on when bm_check is not required.
  488. */
  489. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  490. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  491. "Cache invalidation should work properly"
  492. " for C3 to be enabled on SMP systems\n"));
  493. return;
  494. }
  495. }
  496. /*
  497. * Otherwise we've met all of our C3 requirements.
  498. * Normalize the C3 latency to expidite policy. Enable
  499. * checking of bus mastering status (bm_check) so we can
  500. * use this in our C3 policy
  501. */
  502. cx->valid = 1;
  503. cx->latency_ticks = cx->latency;
  504. /*
  505. * On older chipsets, BM_RLD needs to be set
  506. * in order for Bus Master activity to wake the
  507. * system from C3. Newer chipsets handle DMA
  508. * during C3 automatically and BM_RLD is a NOP.
  509. * In either case, the proper way to
  510. * handle BM_RLD is to set it and leave it set.
  511. */
  512. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  513. return;
  514. }
  515. static int acpi_processor_power_verify(struct acpi_processor *pr)
  516. {
  517. unsigned int i;
  518. unsigned int working = 0;
  519. pr->power.timer_broadcast_on_state = INT_MAX;
  520. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  521. struct acpi_processor_cx *cx = &pr->power.states[i];
  522. switch (cx->type) {
  523. case ACPI_STATE_C1:
  524. cx->valid = 1;
  525. break;
  526. case ACPI_STATE_C2:
  527. if (!cx->address)
  528. break;
  529. cx->valid = 1;
  530. cx->latency_ticks = cx->latency; /* Normalize latency */
  531. break;
  532. case ACPI_STATE_C3:
  533. acpi_processor_power_verify_c3(pr, cx);
  534. break;
  535. }
  536. if (!cx->valid)
  537. continue;
  538. lapic_timer_check_state(i, pr, cx);
  539. tsc_check_state(cx->type);
  540. working++;
  541. }
  542. lapic_timer_propagate_broadcast(pr);
  543. return (working);
  544. }
  545. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  546. {
  547. unsigned int i;
  548. int result;
  549. /* NOTE: the idle thread may not be running while calling
  550. * this function */
  551. /* Zero initialize all the C-states info. */
  552. memset(pr->power.states, 0, sizeof(pr->power.states));
  553. result = acpi_processor_get_power_info_cst(pr);
  554. if (result == -ENODEV)
  555. result = acpi_processor_get_power_info_fadt(pr);
  556. if (result)
  557. return result;
  558. acpi_processor_get_power_info_default(pr);
  559. pr->power.count = acpi_processor_power_verify(pr);
  560. /*
  561. * if one state of type C2 or C3 is available, mark this
  562. * CPU as being "idle manageable"
  563. */
  564. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  565. if (pr->power.states[i].valid) {
  566. pr->power.count = i;
  567. if (pr->power.states[i].type >= ACPI_STATE_C2)
  568. pr->flags.power = 1;
  569. }
  570. }
  571. return 0;
  572. }
  573. /**
  574. * acpi_idle_bm_check - checks if bus master activity was detected
  575. */
  576. static int acpi_idle_bm_check(void)
  577. {
  578. u32 bm_status = 0;
  579. if (bm_check_disable)
  580. return 0;
  581. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  582. if (bm_status)
  583. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  584. /*
  585. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  586. * the true state of bus mastering activity; forcing us to
  587. * manually check the BMIDEA bit of each IDE channel.
  588. */
  589. else if (errata.piix4.bmisx) {
  590. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  591. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  592. bm_status = 1;
  593. }
  594. return bm_status;
  595. }
  596. /**
  597. * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
  598. * @cx: cstate data
  599. *
  600. * Caller disables interrupt before call and enables interrupt after return.
  601. */
  602. static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
  603. {
  604. /* Don't trace irqs off for idle */
  605. stop_critical_timings();
  606. if (cx->entry_method == ACPI_CSTATE_FFH) {
  607. /* Call into architectural FFH based C-state */
  608. acpi_processor_ffh_cstate_enter(cx);
  609. } else if (cx->entry_method == ACPI_CSTATE_HALT) {
  610. acpi_safe_halt();
  611. } else {
  612. /* IO port based C-state */
  613. inb(cx->address);
  614. /* Dummy wait op - must do something useless after P_LVL2 read
  615. because chipsets cannot guarantee that STPCLK# signal
  616. gets asserted in time to freeze execution properly. */
  617. inl(acpi_gbl_FADT.xpm_timer_block.address);
  618. }
  619. start_critical_timings();
  620. }
  621. /**
  622. * acpi_idle_enter_c1 - enters an ACPI C1 state-type
  623. * @dev: the target CPU
  624. * @drv: cpuidle driver containing cpuidle state info
  625. * @index: index of target state
  626. *
  627. * This is equivalent to the HALT instruction.
  628. */
  629. static int acpi_idle_enter_c1(struct cpuidle_device *dev,
  630. struct cpuidle_driver *drv, int index)
  631. {
  632. ktime_t kt1, kt2;
  633. s64 idle_time;
  634. struct acpi_processor *pr;
  635. struct cpuidle_state_usage *state_usage = &dev->states_usage[index];
  636. struct acpi_processor_cx *cx = cpuidle_get_statedata(state_usage);
  637. pr = __this_cpu_read(processors);
  638. dev->last_residency = 0;
  639. if (unlikely(!pr))
  640. return -EINVAL;
  641. local_irq_disable();
  642. if (acpi_idle_suspend) {
  643. local_irq_enable();
  644. cpu_relax();
  645. return -EBUSY;
  646. }
  647. lapic_timer_state_broadcast(pr, cx, 1);
  648. kt1 = ktime_get_real();
  649. acpi_idle_do_entry(cx);
  650. kt2 = ktime_get_real();
  651. idle_time = ktime_to_us(ktime_sub(kt2, kt1));
  652. /* Update device last_residency*/
  653. dev->last_residency = (int)idle_time;
  654. local_irq_enable();
  655. cx->usage++;
  656. lapic_timer_state_broadcast(pr, cx, 0);
  657. return index;
  658. }
  659. /**
  660. * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
  661. * @dev: the target CPU
  662. * @index: the index of suggested state
  663. */
  664. static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
  665. {
  666. struct cpuidle_state_usage *state_usage = &dev->states_usage[index];
  667. struct acpi_processor_cx *cx = cpuidle_get_statedata(state_usage);
  668. ACPI_FLUSH_CPU_CACHE();
  669. while (1) {
  670. if (cx->entry_method == ACPI_CSTATE_HALT)
  671. safe_halt();
  672. else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
  673. inb(cx->address);
  674. /* See comment in acpi_idle_do_entry() */
  675. inl(acpi_gbl_FADT.xpm_timer_block.address);
  676. } else
  677. return -ENODEV;
  678. }
  679. /* Never reached */
  680. return 0;
  681. }
  682. /**
  683. * acpi_idle_enter_simple - enters an ACPI state without BM handling
  684. * @dev: the target CPU
  685. * @drv: cpuidle driver with cpuidle state information
  686. * @index: the index of suggested state
  687. */
  688. static int acpi_idle_enter_simple(struct cpuidle_device *dev,
  689. struct cpuidle_driver *drv, int index)
  690. {
  691. struct acpi_processor *pr;
  692. struct cpuidle_state_usage *state_usage = &dev->states_usage[index];
  693. struct acpi_processor_cx *cx = cpuidle_get_statedata(state_usage);
  694. ktime_t kt1, kt2;
  695. s64 idle_time_ns;
  696. s64 idle_time;
  697. pr = __this_cpu_read(processors);
  698. dev->last_residency = 0;
  699. if (unlikely(!pr))
  700. return -EINVAL;
  701. local_irq_disable();
  702. if (acpi_idle_suspend) {
  703. local_irq_enable();
  704. cpu_relax();
  705. return -EBUSY;
  706. }
  707. if (cx->entry_method != ACPI_CSTATE_FFH) {
  708. current_thread_info()->status &= ~TS_POLLING;
  709. /*
  710. * TS_POLLING-cleared state must be visible before we test
  711. * NEED_RESCHED:
  712. */
  713. smp_mb();
  714. if (unlikely(need_resched())) {
  715. current_thread_info()->status |= TS_POLLING;
  716. local_irq_enable();
  717. return -EINVAL;
  718. }
  719. }
  720. /*
  721. * Must be done before busmaster disable as we might need to
  722. * access HPET !
  723. */
  724. lapic_timer_state_broadcast(pr, cx, 1);
  725. if (cx->type == ACPI_STATE_C3)
  726. ACPI_FLUSH_CPU_CACHE();
  727. kt1 = ktime_get_real();
  728. /* Tell the scheduler that we are going deep-idle: */
  729. sched_clock_idle_sleep_event();
  730. acpi_idle_do_entry(cx);
  731. kt2 = ktime_get_real();
  732. idle_time_ns = ktime_to_ns(ktime_sub(kt2, kt1));
  733. idle_time = idle_time_ns;
  734. do_div(idle_time, NSEC_PER_USEC);
  735. /* Update device last_residency*/
  736. dev->last_residency = (int)idle_time;
  737. /* Tell the scheduler how much we idled: */
  738. sched_clock_idle_wakeup_event(idle_time_ns);
  739. local_irq_enable();
  740. if (cx->entry_method != ACPI_CSTATE_FFH)
  741. current_thread_info()->status |= TS_POLLING;
  742. cx->usage++;
  743. lapic_timer_state_broadcast(pr, cx, 0);
  744. cx->time += idle_time;
  745. return index;
  746. }
  747. static int c3_cpu_count;
  748. static DEFINE_RAW_SPINLOCK(c3_lock);
  749. /**
  750. * acpi_idle_enter_bm - enters C3 with proper BM handling
  751. * @dev: the target CPU
  752. * @drv: cpuidle driver containing state data
  753. * @index: the index of suggested state
  754. *
  755. * If BM is detected, the deepest non-C3 idle state is entered instead.
  756. */
  757. static int acpi_idle_enter_bm(struct cpuidle_device *dev,
  758. struct cpuidle_driver *drv, int index)
  759. {
  760. struct acpi_processor *pr;
  761. struct cpuidle_state_usage *state_usage = &dev->states_usage[index];
  762. struct acpi_processor_cx *cx = cpuidle_get_statedata(state_usage);
  763. ktime_t kt1, kt2;
  764. s64 idle_time_ns;
  765. s64 idle_time;
  766. pr = __this_cpu_read(processors);
  767. dev->last_residency = 0;
  768. if (unlikely(!pr))
  769. return -EINVAL;
  770. if (!cx->bm_sts_skip && acpi_idle_bm_check()) {
  771. if (drv->safe_state_index >= 0) {
  772. return drv->states[drv->safe_state_index].enter(dev,
  773. drv, drv->safe_state_index);
  774. } else {
  775. local_irq_disable();
  776. if (!acpi_idle_suspend)
  777. acpi_safe_halt();
  778. local_irq_enable();
  779. return -EBUSY;
  780. }
  781. }
  782. local_irq_disable();
  783. if (acpi_idle_suspend) {
  784. local_irq_enable();
  785. cpu_relax();
  786. return -EBUSY;
  787. }
  788. if (cx->entry_method != ACPI_CSTATE_FFH) {
  789. current_thread_info()->status &= ~TS_POLLING;
  790. /*
  791. * TS_POLLING-cleared state must be visible before we test
  792. * NEED_RESCHED:
  793. */
  794. smp_mb();
  795. if (unlikely(need_resched())) {
  796. current_thread_info()->status |= TS_POLLING;
  797. local_irq_enable();
  798. return -EINVAL;
  799. }
  800. }
  801. acpi_unlazy_tlb(smp_processor_id());
  802. /* Tell the scheduler that we are going deep-idle: */
  803. sched_clock_idle_sleep_event();
  804. /*
  805. * Must be done before busmaster disable as we might need to
  806. * access HPET !
  807. */
  808. lapic_timer_state_broadcast(pr, cx, 1);
  809. kt1 = ktime_get_real();
  810. /*
  811. * disable bus master
  812. * bm_check implies we need ARB_DIS
  813. * !bm_check implies we need cache flush
  814. * bm_control implies whether we can do ARB_DIS
  815. *
  816. * That leaves a case where bm_check is set and bm_control is
  817. * not set. In that case we cannot do much, we enter C3
  818. * without doing anything.
  819. */
  820. if (pr->flags.bm_check && pr->flags.bm_control) {
  821. raw_spin_lock(&c3_lock);
  822. c3_cpu_count++;
  823. /* Disable bus master arbitration when all CPUs are in C3 */
  824. if (c3_cpu_count == num_online_cpus())
  825. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
  826. raw_spin_unlock(&c3_lock);
  827. } else if (!pr->flags.bm_check) {
  828. ACPI_FLUSH_CPU_CACHE();
  829. }
  830. acpi_idle_do_entry(cx);
  831. /* Re-enable bus master arbitration */
  832. if (pr->flags.bm_check && pr->flags.bm_control) {
  833. raw_spin_lock(&c3_lock);
  834. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
  835. c3_cpu_count--;
  836. raw_spin_unlock(&c3_lock);
  837. }
  838. kt2 = ktime_get_real();
  839. idle_time_ns = ktime_to_ns(ktime_sub(kt2, kt1));
  840. idle_time = idle_time_ns;
  841. do_div(idle_time, NSEC_PER_USEC);
  842. /* Update device last_residency*/
  843. dev->last_residency = (int)idle_time;
  844. /* Tell the scheduler how much we idled: */
  845. sched_clock_idle_wakeup_event(idle_time_ns);
  846. local_irq_enable();
  847. if (cx->entry_method != ACPI_CSTATE_FFH)
  848. current_thread_info()->status |= TS_POLLING;
  849. cx->usage++;
  850. lapic_timer_state_broadcast(pr, cx, 0);
  851. cx->time += idle_time;
  852. return index;
  853. }
  854. struct cpuidle_driver acpi_idle_driver = {
  855. .name = "acpi_idle",
  856. .owner = THIS_MODULE,
  857. };
  858. /**
  859. * acpi_processor_setup_cpuidle_cx - prepares and configures CPUIDLE
  860. * device i.e. per-cpu data
  861. *
  862. * @pr: the ACPI processor
  863. */
  864. static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr)
  865. {
  866. int i, count = CPUIDLE_DRIVER_STATE_START;
  867. struct acpi_processor_cx *cx;
  868. struct cpuidle_state_usage *state_usage;
  869. struct cpuidle_device *dev = &pr->power.dev;
  870. if (!pr->flags.power_setup_done)
  871. return -EINVAL;
  872. if (pr->flags.power == 0) {
  873. return -EINVAL;
  874. }
  875. dev->cpu = pr->id;
  876. if (max_cstate == 0)
  877. max_cstate = 1;
  878. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  879. cx = &pr->power.states[i];
  880. state_usage = &dev->states_usage[count];
  881. if (!cx->valid)
  882. continue;
  883. #ifdef CONFIG_HOTPLUG_CPU
  884. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  885. !pr->flags.has_cst &&
  886. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  887. continue;
  888. #endif
  889. cpuidle_set_statedata(state_usage, cx);
  890. count++;
  891. if (count == CPUIDLE_STATE_MAX)
  892. break;
  893. }
  894. dev->state_count = count;
  895. if (!count)
  896. return -EINVAL;
  897. return 0;
  898. }
  899. /**
  900. * acpi_processor_setup_cpuidle states- prepares and configures cpuidle
  901. * global state data i.e. idle routines
  902. *
  903. * @pr: the ACPI processor
  904. */
  905. static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
  906. {
  907. int i, count = CPUIDLE_DRIVER_STATE_START;
  908. struct acpi_processor_cx *cx;
  909. struct cpuidle_state *state;
  910. struct cpuidle_driver *drv = &acpi_idle_driver;
  911. if (!pr->flags.power_setup_done)
  912. return -EINVAL;
  913. if (pr->flags.power == 0)
  914. return -EINVAL;
  915. drv->safe_state_index = -1;
  916. for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
  917. drv->states[i].name[0] = '\0';
  918. drv->states[i].desc[0] = '\0';
  919. }
  920. if (max_cstate == 0)
  921. max_cstate = 1;
  922. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  923. cx = &pr->power.states[i];
  924. if (!cx->valid)
  925. continue;
  926. #ifdef CONFIG_HOTPLUG_CPU
  927. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  928. !pr->flags.has_cst &&
  929. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  930. continue;
  931. #endif
  932. state = &drv->states[count];
  933. snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
  934. strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
  935. state->exit_latency = cx->latency;
  936. state->target_residency = cx->latency * latency_factor;
  937. state->flags = 0;
  938. switch (cx->type) {
  939. case ACPI_STATE_C1:
  940. if (cx->entry_method == ACPI_CSTATE_FFH)
  941. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  942. state->enter = acpi_idle_enter_c1;
  943. state->enter_dead = acpi_idle_play_dead;
  944. drv->safe_state_index = count;
  945. break;
  946. case ACPI_STATE_C2:
  947. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  948. state->enter = acpi_idle_enter_simple;
  949. state->enter_dead = acpi_idle_play_dead;
  950. drv->safe_state_index = count;
  951. break;
  952. case ACPI_STATE_C3:
  953. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  954. state->enter = pr->flags.bm_check ?
  955. acpi_idle_enter_bm :
  956. acpi_idle_enter_simple;
  957. break;
  958. }
  959. count++;
  960. if (count == CPUIDLE_STATE_MAX)
  961. break;
  962. }
  963. drv->state_count = count;
  964. if (!count)
  965. return -EINVAL;
  966. return 0;
  967. }
  968. int acpi_processor_hotplug(struct acpi_processor *pr)
  969. {
  970. int ret = 0;
  971. if (disabled_by_idle_boot_param())
  972. return 0;
  973. if (!pr)
  974. return -EINVAL;
  975. if (nocst) {
  976. return -ENODEV;
  977. }
  978. if (!pr->flags.power_setup_done)
  979. return -ENODEV;
  980. cpuidle_pause_and_lock();
  981. cpuidle_disable_device(&pr->power.dev);
  982. acpi_processor_get_power_info(pr);
  983. if (pr->flags.power) {
  984. acpi_processor_setup_cpuidle_cx(pr);
  985. ret = cpuidle_enable_device(&pr->power.dev);
  986. }
  987. cpuidle_resume_and_unlock();
  988. return ret;
  989. }
  990. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  991. {
  992. int cpu;
  993. struct acpi_processor *_pr;
  994. if (disabled_by_idle_boot_param())
  995. return 0;
  996. if (!pr)
  997. return -EINVAL;
  998. if (nocst)
  999. return -ENODEV;
  1000. if (!pr->flags.power_setup_done)
  1001. return -ENODEV;
  1002. /*
  1003. * FIXME: Design the ACPI notification to make it once per
  1004. * system instead of once per-cpu. This condition is a hack
  1005. * to make the code that updates C-States be called once.
  1006. */
  1007. if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
  1008. cpuidle_pause_and_lock();
  1009. /* Protect against cpu-hotplug */
  1010. get_online_cpus();
  1011. /* Disable all cpuidle devices */
  1012. for_each_online_cpu(cpu) {
  1013. _pr = per_cpu(processors, cpu);
  1014. if (!_pr || !_pr->flags.power_setup_done)
  1015. continue;
  1016. cpuidle_disable_device(&_pr->power.dev);
  1017. }
  1018. /* Populate Updated C-state information */
  1019. acpi_processor_setup_cpuidle_states(pr);
  1020. /* Enable all cpuidle devices */
  1021. for_each_online_cpu(cpu) {
  1022. _pr = per_cpu(processors, cpu);
  1023. if (!_pr || !_pr->flags.power_setup_done)
  1024. continue;
  1025. acpi_processor_get_power_info(_pr);
  1026. if (_pr->flags.power) {
  1027. acpi_processor_setup_cpuidle_cx(_pr);
  1028. cpuidle_enable_device(&_pr->power.dev);
  1029. }
  1030. }
  1031. put_online_cpus();
  1032. cpuidle_resume_and_unlock();
  1033. }
  1034. return 0;
  1035. }
  1036. static int acpi_processor_registered;
  1037. int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
  1038. struct acpi_device *device)
  1039. {
  1040. acpi_status status = 0;
  1041. int retval;
  1042. static int first_run;
  1043. if (disabled_by_idle_boot_param())
  1044. return 0;
  1045. if (!first_run) {
  1046. dmi_check_system(processor_power_dmi_table);
  1047. max_cstate = acpi_processor_cstate_check(max_cstate);
  1048. if (max_cstate < ACPI_C_STATES_MAX)
  1049. printk(KERN_NOTICE
  1050. "ACPI: processor limited to max C-state %d\n",
  1051. max_cstate);
  1052. first_run++;
  1053. }
  1054. if (!pr)
  1055. return -EINVAL;
  1056. if (acpi_gbl_FADT.cst_control && !nocst) {
  1057. status =
  1058. acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
  1059. if (ACPI_FAILURE(status)) {
  1060. ACPI_EXCEPTION((AE_INFO, status,
  1061. "Notifying BIOS of _CST ability failed"));
  1062. }
  1063. }
  1064. acpi_processor_get_power_info(pr);
  1065. pr->flags.power_setup_done = 1;
  1066. /*
  1067. * Install the idle handler if processor power management is supported.
  1068. * Note that we use previously set idle handler will be used on
  1069. * platforms that only support C1.
  1070. */
  1071. if (pr->flags.power) {
  1072. /* Register acpi_idle_driver if not already registered */
  1073. if (!acpi_processor_registered) {
  1074. acpi_processor_setup_cpuidle_states(pr);
  1075. retval = cpuidle_register_driver(&acpi_idle_driver);
  1076. if (retval)
  1077. return retval;
  1078. printk(KERN_DEBUG "ACPI: %s registered with cpuidle\n",
  1079. acpi_idle_driver.name);
  1080. }
  1081. /* Register per-cpu cpuidle_device. Cpuidle driver
  1082. * must already be registered before registering device
  1083. */
  1084. acpi_processor_setup_cpuidle_cx(pr);
  1085. retval = cpuidle_register_device(&pr->power.dev);
  1086. if (retval) {
  1087. if (acpi_processor_registered == 0)
  1088. cpuidle_unregister_driver(&acpi_idle_driver);
  1089. return retval;
  1090. }
  1091. acpi_processor_registered++;
  1092. }
  1093. return 0;
  1094. }
  1095. int acpi_processor_power_exit(struct acpi_processor *pr,
  1096. struct acpi_device *device)
  1097. {
  1098. if (disabled_by_idle_boot_param())
  1099. return 0;
  1100. if (pr->flags.power) {
  1101. cpuidle_unregister_device(&pr->power.dev);
  1102. acpi_processor_registered--;
  1103. if (acpi_processor_registered == 0)
  1104. cpuidle_unregister_driver(&acpi_idle_driver);
  1105. }
  1106. pr->flags.power_setup_done = 0;
  1107. return 0;
  1108. }