processor_idle.c 31 KB

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