processor_idle.c 31 KB

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