processor_idle.c 31 KB

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