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