processor_idle.c 31 KB

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