processor_idle.c 29 KB

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