processor_idle.c 31 KB

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