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