processor_idle.c 29 KB

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