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