processor_idle.c 28 KB

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