processor_idle.c 31 KB

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