processor_idle.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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/slab.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/acpi.h>
  38. #include <linux/dmi.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/sched.h> /* need_resched() */
  41. #include <linux/pm_qos_params.h>
  42. #include <linux/clockchips.h>
  43. #include <linux/cpuidle.h>
  44. #include <linux/irqflags.h>
  45. /*
  46. * Include the apic definitions for x86 to have the APIC timer related defines
  47. * available also for UP (on SMP it gets magically included via linux/smp.h).
  48. * asm/acpi.h is not an option, as it would require more include magic. Also
  49. * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
  50. */
  51. #ifdef CONFIG_X86
  52. #include <asm/apic.h>
  53. #endif
  54. #include <asm/io.h>
  55. #include <asm/uaccess.h>
  56. #include <acpi/acpi_bus.h>
  57. #include <acpi/processor.h>
  58. #include <asm/processor.h>
  59. #define PREFIX "ACPI: "
  60. #define ACPI_PROCESSOR_CLASS "processor"
  61. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  62. ACPI_MODULE_NAME("processor_idle");
  63. #define ACPI_PROCESSOR_FILE_POWER "power"
  64. #define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
  65. #define C2_OVERHEAD 1 /* 1us */
  66. #define C3_OVERHEAD 1 /* 1us */
  67. #define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
  68. static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
  69. module_param(max_cstate, uint, 0000);
  70. static unsigned int nocst __read_mostly;
  71. module_param(nocst, uint, 0000);
  72. static unsigned int latency_factor __read_mostly = 2;
  73. module_param(latency_factor, uint, 0644);
  74. static s64 us_to_pm_timer_ticks(s64 t)
  75. {
  76. return div64_u64(t * PM_TIMER_FREQUENCY, 1000000);
  77. }
  78. /*
  79. * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
  80. * For now disable this. Probably a bug somewhere else.
  81. *
  82. * To skip this limit, boot/load with a large max_cstate limit.
  83. */
  84. static int set_max_cstate(const struct dmi_system_id *id)
  85. {
  86. if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
  87. return 0;
  88. printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
  89. " Override with \"processor.max_cstate=%d\"\n", id->ident,
  90. (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
  91. max_cstate = (long)id->driver_data;
  92. return 0;
  93. }
  94. /* Actually this shouldn't be __cpuinitdata, would be better to fix the
  95. callers to only run once -AK */
  96. static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
  97. { set_max_cstate, "Clevo 5600D", {
  98. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  99. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  100. (void *)2},
  101. { set_max_cstate, "Pavilion zv5000", {
  102. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  103. DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
  104. (void *)1},
  105. { set_max_cstate, "Asus L8400B", {
  106. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  107. DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
  108. (void *)1},
  109. {},
  110. };
  111. /*
  112. * Callers should disable interrupts before the call and enable
  113. * interrupts after return.
  114. */
  115. static void acpi_safe_halt(void)
  116. {
  117. current_thread_info()->status &= ~TS_POLLING;
  118. /*
  119. * TS_POLLING-cleared state must be visible before we
  120. * test NEED_RESCHED:
  121. */
  122. smp_mb();
  123. if (!need_resched()) {
  124. safe_halt();
  125. local_irq_disable();
  126. }
  127. current_thread_info()->status |= TS_POLLING;
  128. }
  129. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  130. /*
  131. * Some BIOS implementations switch to C3 in the published C2 state.
  132. * This seems to be a common problem on AMD boxen, but other vendors
  133. * are affected too. We pick the most conservative approach: we assume
  134. * that the local APIC stops in both C2 and C3.
  135. */
  136. static void lapic_timer_check_state(int state, struct acpi_processor *pr,
  137. struct acpi_processor_cx *cx)
  138. {
  139. struct acpi_processor_power *pwr = &pr->power;
  140. u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
  141. if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
  142. return;
  143. if (boot_cpu_has(X86_FEATURE_AMDC1E))
  144. type = ACPI_STATE_C1;
  145. /*
  146. * Check, if one of the previous states already marked the lapic
  147. * unstable
  148. */
  149. if (pwr->timer_broadcast_on_state < state)
  150. return;
  151. if (cx->type >= type)
  152. pr->power.timer_broadcast_on_state = state;
  153. }
  154. static void __lapic_timer_propagate_broadcast(void *arg)
  155. {
  156. struct acpi_processor *pr = (struct acpi_processor *) arg;
  157. unsigned long reason;
  158. reason = pr->power.timer_broadcast_on_state < INT_MAX ?
  159. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  160. clockevents_notify(reason, &pr->id);
  161. }
  162. static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
  163. {
  164. smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
  165. (void *)pr, 1);
  166. }
  167. /* Power(C) State timer broadcast control */
  168. static void lapic_timer_state_broadcast(struct acpi_processor *pr,
  169. struct acpi_processor_cx *cx,
  170. int broadcast)
  171. {
  172. int state = cx - pr->power.states;
  173. if (state >= pr->power.timer_broadcast_on_state) {
  174. unsigned long reason;
  175. reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
  176. CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
  177. clockevents_notify(reason, &pr->id);
  178. }
  179. }
  180. #else
  181. static void lapic_timer_check_state(int state, struct acpi_processor *pr,
  182. struct acpi_processor_cx *cstate) { }
  183. static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
  184. static void lapic_timer_state_broadcast(struct acpi_processor *pr,
  185. struct acpi_processor_cx *cx,
  186. int broadcast)
  187. {
  188. }
  189. #endif
  190. /*
  191. * Suspend / resume control
  192. */
  193. static int acpi_idle_suspend;
  194. static u32 saved_bm_rld;
  195. static void acpi_idle_bm_rld_save(void)
  196. {
  197. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
  198. }
  199. static void acpi_idle_bm_rld_restore(void)
  200. {
  201. u32 resumed_bm_rld;
  202. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
  203. if (resumed_bm_rld != saved_bm_rld)
  204. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
  205. }
  206. int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
  207. {
  208. if (acpi_idle_suspend == 1)
  209. return 0;
  210. acpi_idle_bm_rld_save();
  211. acpi_idle_suspend = 1;
  212. return 0;
  213. }
  214. int acpi_processor_resume(struct acpi_device * device)
  215. {
  216. if (acpi_idle_suspend == 0)
  217. return 0;
  218. acpi_idle_bm_rld_restore();
  219. acpi_idle_suspend = 0;
  220. return 0;
  221. }
  222. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
  223. static void tsc_check_state(int state)
  224. {
  225. switch (boot_cpu_data.x86_vendor) {
  226. case X86_VENDOR_AMD:
  227. case X86_VENDOR_INTEL:
  228. /*
  229. * AMD Fam10h TSC will tick in all
  230. * C/P/S0/S1 states when this bit is set.
  231. */
  232. if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  233. return;
  234. /*FALL THROUGH*/
  235. default:
  236. /* TSC could halt in idle, so notify users */
  237. if (state > ACPI_STATE_C1)
  238. mark_tsc_unstable("TSC halts in idle");
  239. }
  240. }
  241. #else
  242. static void tsc_check_state(int state) { return; }
  243. #endif
  244. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  245. {
  246. if (!pr)
  247. return -EINVAL;
  248. if (!pr->pblk)
  249. return -ENODEV;
  250. /* if info is obtained from pblk/fadt, type equals state */
  251. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  252. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  253. #ifndef CONFIG_HOTPLUG_CPU
  254. /*
  255. * Check for P_LVL2_UP flag before entering C2 and above on
  256. * an SMP system.
  257. */
  258. if ((num_online_cpus() > 1) &&
  259. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  260. return -ENODEV;
  261. #endif
  262. /* determine C2 and C3 address from pblk */
  263. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  264. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  265. /* determine latencies from FADT */
  266. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
  267. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
  268. /*
  269. * FADT specified C2 latency must be less than or equal to
  270. * 100 microseconds.
  271. */
  272. if (acpi_gbl_FADT.C2latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  273. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  274. "C2 latency too large [%d]\n", acpi_gbl_FADT.C2latency));
  275. /* invalidate C2 */
  276. pr->power.states[ACPI_STATE_C2].address = 0;
  277. }
  278. /*
  279. * FADT supplied C3 latency must be less than or equal to
  280. * 1000 microseconds.
  281. */
  282. if (acpi_gbl_FADT.C3latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  283. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  284. "C3 latency too large [%d]\n", acpi_gbl_FADT.C3latency));
  285. /* invalidate C3 */
  286. pr->power.states[ACPI_STATE_C3].address = 0;
  287. }
  288. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  289. "lvl2[0x%08x] lvl3[0x%08x]\n",
  290. pr->power.states[ACPI_STATE_C2].address,
  291. pr->power.states[ACPI_STATE_C3].address));
  292. return 0;
  293. }
  294. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  295. {
  296. if (!pr->power.states[ACPI_STATE_C1].valid) {
  297. /* set the first C-State to C1 */
  298. /* all processors need to support C1 */
  299. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  300. pr->power.states[ACPI_STATE_C1].valid = 1;
  301. pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
  302. }
  303. /* the C0 state only exists as a filler in our array */
  304. pr->power.states[ACPI_STATE_C0].valid = 1;
  305. return 0;
  306. }
  307. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  308. {
  309. acpi_status status = 0;
  310. u64 count;
  311. int current_count;
  312. int i;
  313. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  314. union acpi_object *cst;
  315. if (nocst)
  316. return -ENODEV;
  317. current_count = 0;
  318. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  319. if (ACPI_FAILURE(status)) {
  320. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  321. return -ENODEV;
  322. }
  323. cst = buffer.pointer;
  324. /* There must be at least 2 elements */
  325. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  326. printk(KERN_ERR PREFIX "not enough elements in _CST\n");
  327. status = -EFAULT;
  328. goto end;
  329. }
  330. count = cst->package.elements[0].integer.value;
  331. /* Validate number of power states. */
  332. if (count < 1 || count != cst->package.count - 1) {
  333. printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
  334. status = -EFAULT;
  335. goto end;
  336. }
  337. /* Tell driver that at least _CST is supported. */
  338. pr->flags.has_cst = 1;
  339. for (i = 1; i <= count; i++) {
  340. union acpi_object *element;
  341. union acpi_object *obj;
  342. struct acpi_power_register *reg;
  343. struct acpi_processor_cx cx;
  344. memset(&cx, 0, sizeof(cx));
  345. element = &(cst->package.elements[i]);
  346. if (element->type != ACPI_TYPE_PACKAGE)
  347. continue;
  348. if (element->package.count != 4)
  349. continue;
  350. obj = &(element->package.elements[0]);
  351. if (obj->type != ACPI_TYPE_BUFFER)
  352. continue;
  353. reg = (struct acpi_power_register *)obj->buffer.pointer;
  354. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  355. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  356. continue;
  357. /* There should be an easy way to extract an integer... */
  358. obj = &(element->package.elements[1]);
  359. if (obj->type != ACPI_TYPE_INTEGER)
  360. continue;
  361. cx.type = obj->integer.value;
  362. /*
  363. * Some buggy BIOSes won't list C1 in _CST -
  364. * Let acpi_processor_get_power_info_default() handle them later
  365. */
  366. if (i == 1 && cx.type != ACPI_STATE_C1)
  367. current_count++;
  368. cx.address = reg->address;
  369. cx.index = current_count + 1;
  370. cx.entry_method = ACPI_CSTATE_SYSTEMIO;
  371. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  372. if (acpi_processor_ffh_cstate_probe
  373. (pr->id, &cx, reg) == 0) {
  374. cx.entry_method = ACPI_CSTATE_FFH;
  375. } else if (cx.type == ACPI_STATE_C1) {
  376. /*
  377. * C1 is a special case where FIXED_HARDWARE
  378. * can be handled in non-MWAIT way as well.
  379. * In that case, save this _CST entry info.
  380. * Otherwise, ignore this info and continue.
  381. */
  382. cx.entry_method = ACPI_CSTATE_HALT;
  383. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  384. } else {
  385. continue;
  386. }
  387. if (cx.type == ACPI_STATE_C1 &&
  388. (idle_halt || idle_nomwait)) {
  389. /*
  390. * In most cases the C1 space_id obtained from
  391. * _CST object is FIXED_HARDWARE access mode.
  392. * But when the option of idle=halt is added,
  393. * the entry_method type should be changed from
  394. * CSTATE_FFH to CSTATE_HALT.
  395. * When the option of idle=nomwait is added,
  396. * the C1 entry_method type should be
  397. * CSTATE_HALT.
  398. */
  399. cx.entry_method = ACPI_CSTATE_HALT;
  400. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  401. }
  402. } else {
  403. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
  404. cx.address);
  405. }
  406. if (cx.type == ACPI_STATE_C1) {
  407. cx.valid = 1;
  408. }
  409. obj = &(element->package.elements[2]);
  410. if (obj->type != ACPI_TYPE_INTEGER)
  411. continue;
  412. cx.latency = obj->integer.value;
  413. obj = &(element->package.elements[3]);
  414. if (obj->type != ACPI_TYPE_INTEGER)
  415. continue;
  416. cx.power = obj->integer.value;
  417. current_count++;
  418. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  419. /*
  420. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  421. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  422. */
  423. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  424. printk(KERN_WARNING
  425. "Limiting number of power states to max (%d)\n",
  426. ACPI_PROCESSOR_MAX_POWER);
  427. printk(KERN_WARNING
  428. "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  429. break;
  430. }
  431. }
  432. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  433. current_count));
  434. /* Validate number of power states discovered */
  435. if (current_count < 2)
  436. status = -EFAULT;
  437. end:
  438. kfree(buffer.pointer);
  439. return status;
  440. }
  441. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  442. struct acpi_processor_cx *cx)
  443. {
  444. static int bm_check_flag = -1;
  445. static int bm_control_flag = -1;
  446. if (!cx->address)
  447. return;
  448. /*
  449. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  450. * DMA transfers are used by any ISA device to avoid livelock.
  451. * Note that we could disable Type-F DMA (as recommended by
  452. * the erratum), but this is known to disrupt certain ISA
  453. * devices thus we take the conservative approach.
  454. */
  455. else if (errata.piix4.fdma) {
  456. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  457. "C3 not supported on PIIX4 with Type-F DMA\n"));
  458. return;
  459. }
  460. /* All the logic here assumes flags.bm_check is same across all CPUs */
  461. if (bm_check_flag == -1) {
  462. /* Determine whether bm_check is needed based on CPU */
  463. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  464. bm_check_flag = pr->flags.bm_check;
  465. bm_control_flag = pr->flags.bm_control;
  466. } else {
  467. pr->flags.bm_check = bm_check_flag;
  468. pr->flags.bm_control = bm_control_flag;
  469. }
  470. if (pr->flags.bm_check) {
  471. if (!pr->flags.bm_control) {
  472. if (pr->flags.has_cst != 1) {
  473. /* bus mastering control is necessary */
  474. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  475. "C3 support requires BM control\n"));
  476. return;
  477. } else {
  478. /* Here we enter C3 without bus mastering */
  479. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  480. "C3 support without BM control\n"));
  481. }
  482. }
  483. } else {
  484. /*
  485. * WBINVD should be set in fadt, for C3 state to be
  486. * supported on when bm_check is not required.
  487. */
  488. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  489. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  490. "Cache invalidation should work properly"
  491. " for C3 to be enabled on SMP systems\n"));
  492. return;
  493. }
  494. }
  495. /*
  496. * Otherwise we've met all of our C3 requirements.
  497. * Normalize the C3 latency to expidite policy. Enable
  498. * checking of bus mastering status (bm_check) so we can
  499. * use this in our C3 policy
  500. */
  501. cx->valid = 1;
  502. cx->latency_ticks = cx->latency;
  503. /*
  504. * On older chipsets, BM_RLD needs to be set
  505. * in order for Bus Master activity to wake the
  506. * system from C3. Newer chipsets handle DMA
  507. * during C3 automatically and BM_RLD is a NOP.
  508. * In either case, the proper way to
  509. * handle BM_RLD is to set it and leave it set.
  510. */
  511. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  512. return;
  513. }
  514. static int acpi_processor_power_verify(struct acpi_processor *pr)
  515. {
  516. unsigned int i;
  517. unsigned int working = 0;
  518. pr->power.timer_broadcast_on_state = INT_MAX;
  519. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  520. struct acpi_processor_cx *cx = &pr->power.states[i];
  521. switch (cx->type) {
  522. case ACPI_STATE_C1:
  523. cx->valid = 1;
  524. break;
  525. case ACPI_STATE_C2:
  526. if (!cx->address)
  527. break;
  528. cx->valid = 1;
  529. cx->latency_ticks = cx->latency; /* Normalize latency */
  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. if (cx->entry_method != ACPI_CSTATE_FFH) {
  741. current_thread_info()->status &= ~TS_POLLING;
  742. /*
  743. * TS_POLLING-cleared state must be visible before we test
  744. * NEED_RESCHED:
  745. */
  746. smp_mb();
  747. }
  748. if (unlikely(need_resched())) {
  749. current_thread_info()->status |= TS_POLLING;
  750. local_irq_enable();
  751. return 0;
  752. }
  753. /*
  754. * Must be done before busmaster disable as we might need to
  755. * access HPET !
  756. */
  757. lapic_timer_state_broadcast(pr, cx, 1);
  758. if (cx->type == ACPI_STATE_C3)
  759. ACPI_FLUSH_CPU_CACHE();
  760. kt1 = ktime_get_real();
  761. /* Tell the scheduler that we are going deep-idle: */
  762. sched_clock_idle_sleep_event();
  763. acpi_idle_do_entry(cx);
  764. kt2 = ktime_get_real();
  765. idle_time = ktime_to_us(ktime_sub(kt2, kt1));
  766. sleep_ticks = us_to_pm_timer_ticks(idle_time);
  767. /* Tell the scheduler how much we idled: */
  768. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  769. local_irq_enable();
  770. current_thread_info()->status |= TS_POLLING;
  771. cx->usage++;
  772. lapic_timer_state_broadcast(pr, cx, 0);
  773. cx->time += sleep_ticks;
  774. return idle_time;
  775. }
  776. static int c3_cpu_count;
  777. static DEFINE_SPINLOCK(c3_lock);
  778. /**
  779. * acpi_idle_enter_bm - enters C3 with proper BM handling
  780. * @dev: the target CPU
  781. * @state: the state data
  782. *
  783. * If BM is detected, the deepest non-C3 idle state is entered instead.
  784. */
  785. static int acpi_idle_enter_bm(struct cpuidle_device *dev,
  786. struct cpuidle_state *state)
  787. {
  788. struct acpi_processor *pr;
  789. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  790. ktime_t kt1, kt2;
  791. s64 idle_time;
  792. s64 sleep_ticks = 0;
  793. pr = __get_cpu_var(processors);
  794. if (unlikely(!pr))
  795. return 0;
  796. if (acpi_idle_suspend)
  797. return(acpi_idle_enter_c1(dev, state));
  798. if (acpi_idle_bm_check()) {
  799. if (dev->safe_state) {
  800. dev->last_state = dev->safe_state;
  801. return dev->safe_state->enter(dev, dev->safe_state);
  802. } else {
  803. local_irq_disable();
  804. acpi_safe_halt();
  805. local_irq_enable();
  806. return 0;
  807. }
  808. }
  809. local_irq_disable();
  810. if (cx->entry_method != ACPI_CSTATE_FFH) {
  811. current_thread_info()->status &= ~TS_POLLING;
  812. /*
  813. * TS_POLLING-cleared state must be visible before we test
  814. * NEED_RESCHED:
  815. */
  816. smp_mb();
  817. }
  818. if (unlikely(need_resched())) {
  819. current_thread_info()->status |= TS_POLLING;
  820. local_irq_enable();
  821. return 0;
  822. }
  823. acpi_unlazy_tlb(smp_processor_id());
  824. /* Tell the scheduler that we are going deep-idle: */
  825. sched_clock_idle_sleep_event();
  826. /*
  827. * Must be done before busmaster disable as we might need to
  828. * access HPET !
  829. */
  830. lapic_timer_state_broadcast(pr, cx, 1);
  831. kt1 = ktime_get_real();
  832. /*
  833. * disable bus master
  834. * bm_check implies we need ARB_DIS
  835. * !bm_check implies we need cache flush
  836. * bm_control implies whether we can do ARB_DIS
  837. *
  838. * That leaves a case where bm_check is set and bm_control is
  839. * not set. In that case we cannot do much, we enter C3
  840. * without doing anything.
  841. */
  842. if (pr->flags.bm_check && pr->flags.bm_control) {
  843. spin_lock(&c3_lock);
  844. c3_cpu_count++;
  845. /* Disable bus master arbitration when all CPUs are in C3 */
  846. if (c3_cpu_count == num_online_cpus())
  847. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
  848. spin_unlock(&c3_lock);
  849. } else if (!pr->flags.bm_check) {
  850. ACPI_FLUSH_CPU_CACHE();
  851. }
  852. acpi_idle_do_entry(cx);
  853. /* Re-enable bus master arbitration */
  854. if (pr->flags.bm_check && pr->flags.bm_control) {
  855. spin_lock(&c3_lock);
  856. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
  857. c3_cpu_count--;
  858. spin_unlock(&c3_lock);
  859. }
  860. kt2 = ktime_get_real();
  861. idle_time = ktime_to_us(ktime_sub(kt2, kt1));
  862. sleep_ticks = us_to_pm_timer_ticks(idle_time);
  863. /* Tell the scheduler how much we idled: */
  864. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  865. local_irq_enable();
  866. current_thread_info()->status |= TS_POLLING;
  867. cx->usage++;
  868. lapic_timer_state_broadcast(pr, cx, 0);
  869. cx->time += sleep_ticks;
  870. return idle_time;
  871. }
  872. struct cpuidle_driver acpi_idle_driver = {
  873. .name = "acpi_idle",
  874. .owner = THIS_MODULE,
  875. };
  876. /**
  877. * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
  878. * @pr: the ACPI processor
  879. */
  880. static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
  881. {
  882. int i, count = CPUIDLE_DRIVER_STATE_START;
  883. struct acpi_processor_cx *cx;
  884. struct cpuidle_state *state;
  885. struct cpuidle_device *dev = &pr->power.dev;
  886. if (!pr->flags.power_setup_done)
  887. return -EINVAL;
  888. if (pr->flags.power == 0) {
  889. return -EINVAL;
  890. }
  891. dev->cpu = pr->id;
  892. for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
  893. dev->states[i].name[0] = '\0';
  894. dev->states[i].desc[0] = '\0';
  895. }
  896. if (max_cstate == 0)
  897. max_cstate = 1;
  898. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  899. cx = &pr->power.states[i];
  900. state = &dev->states[count];
  901. if (!cx->valid)
  902. continue;
  903. #ifdef CONFIG_HOTPLUG_CPU
  904. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  905. !pr->flags.has_cst &&
  906. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  907. continue;
  908. #endif
  909. cpuidle_set_statedata(state, cx);
  910. snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
  911. strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
  912. state->exit_latency = cx->latency;
  913. state->target_residency = cx->latency * latency_factor;
  914. state->power_usage = cx->power;
  915. state->flags = 0;
  916. switch (cx->type) {
  917. case ACPI_STATE_C1:
  918. state->flags |= CPUIDLE_FLAG_SHALLOW;
  919. if (cx->entry_method == ACPI_CSTATE_FFH)
  920. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  921. state->enter = acpi_idle_enter_c1;
  922. dev->safe_state = state;
  923. break;
  924. case ACPI_STATE_C2:
  925. state->flags |= CPUIDLE_FLAG_BALANCED;
  926. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  927. state->enter = acpi_idle_enter_simple;
  928. dev->safe_state = state;
  929. break;
  930. case ACPI_STATE_C3:
  931. state->flags |= CPUIDLE_FLAG_DEEP;
  932. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  933. state->flags |= CPUIDLE_FLAG_CHECK_BM;
  934. state->enter = pr->flags.bm_check ?
  935. acpi_idle_enter_bm :
  936. acpi_idle_enter_simple;
  937. break;
  938. }
  939. count++;
  940. if (count == CPUIDLE_STATE_MAX)
  941. break;
  942. }
  943. dev->state_count = count;
  944. if (!count)
  945. return -EINVAL;
  946. return 0;
  947. }
  948. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  949. {
  950. int ret = 0;
  951. if (boot_option_idle_override)
  952. return 0;
  953. if (!pr)
  954. return -EINVAL;
  955. if (nocst) {
  956. return -ENODEV;
  957. }
  958. if (!pr->flags.power_setup_done)
  959. return -ENODEV;
  960. cpuidle_pause_and_lock();
  961. cpuidle_disable_device(&pr->power.dev);
  962. acpi_processor_get_power_info(pr);
  963. if (pr->flags.power) {
  964. acpi_processor_setup_cpuidle(pr);
  965. ret = cpuidle_enable_device(&pr->power.dev);
  966. }
  967. cpuidle_resume_and_unlock();
  968. return ret;
  969. }
  970. int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
  971. struct acpi_device *device)
  972. {
  973. acpi_status status = 0;
  974. static int first_run;
  975. #ifdef CONFIG_ACPI_PROCFS
  976. struct proc_dir_entry *entry = NULL;
  977. #endif
  978. if (boot_option_idle_override)
  979. return 0;
  980. if (!first_run) {
  981. if (idle_halt) {
  982. /*
  983. * When the boot option of "idle=halt" is added, halt
  984. * is used for CPU IDLE.
  985. * In such case C2/C3 is meaningless. So the max_cstate
  986. * is set to one.
  987. */
  988. max_cstate = 1;
  989. }
  990. dmi_check_system(processor_power_dmi_table);
  991. max_cstate = acpi_processor_cstate_check(max_cstate);
  992. if (max_cstate < ACPI_C_STATES_MAX)
  993. printk(KERN_NOTICE
  994. "ACPI: processor limited to max C-state %d\n",
  995. max_cstate);
  996. first_run++;
  997. }
  998. if (!pr)
  999. return -EINVAL;
  1000. if (acpi_gbl_FADT.cst_control && !nocst) {
  1001. status =
  1002. acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
  1003. if (ACPI_FAILURE(status)) {
  1004. ACPI_EXCEPTION((AE_INFO, status,
  1005. "Notifying BIOS of _CST ability failed"));
  1006. }
  1007. }
  1008. acpi_processor_get_power_info(pr);
  1009. pr->flags.power_setup_done = 1;
  1010. /*
  1011. * Install the idle handler if processor power management is supported.
  1012. * Note that we use previously set idle handler will be used on
  1013. * platforms that only support C1.
  1014. */
  1015. if (pr->flags.power) {
  1016. acpi_processor_setup_cpuidle(pr);
  1017. if (cpuidle_register_device(&pr->power.dev))
  1018. return -EIO;
  1019. }
  1020. #ifdef CONFIG_ACPI_PROCFS
  1021. /* 'power' [R] */
  1022. entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
  1023. S_IRUGO, acpi_device_dir(device),
  1024. &acpi_processor_power_fops,
  1025. acpi_driver_data(device));
  1026. if (!entry)
  1027. return -EIO;
  1028. #endif
  1029. return 0;
  1030. }
  1031. int acpi_processor_power_exit(struct acpi_processor *pr,
  1032. struct acpi_device *device)
  1033. {
  1034. if (boot_option_idle_override)
  1035. return 0;
  1036. cpuidle_unregister_device(&pr->power.dev);
  1037. pr->flags.power_setup_done = 0;
  1038. #ifdef CONFIG_ACPI_PROCFS
  1039. if (acpi_device_dir(device))
  1040. remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1041. acpi_device_dir(device));
  1042. #endif
  1043. return 0;
  1044. }