processor_idle.c 32 KB

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