processor_idle.c 29 KB

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