processor_idle.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * processor_idle - idle state submodule to the ACPI processor driver
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
  8. * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  9. * - Added processor hotplug support
  10. * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  11. * - Added support for C3 on SMP
  12. */
  13. #define pr_fmt(fmt) "ACPI: " fmt
  14. #include <linux/module.h>
  15. #include <linux/acpi.h>
  16. #include <linux/dmi.h>
  17. #include <linux/sched.h> /* need_resched() */
  18. #include <linux/tick.h>
  19. #include <linux/cpuidle.h>
  20. #include <linux/cpu.h>
  21. #include <linux/minmax.h>
  22. #include <linux/perf_event.h>
  23. #include <acpi/processor.h>
  24. #include <linux/context_tracking.h>
  25. #include "internal.h"
  26. /*
  27. * Include the apic definitions for x86 to have the APIC timer related defines
  28. * available also for UP (on SMP it gets magically included via linux/smp.h).
  29. * asm/acpi.h is not an option, as it would require more include magic. Also
  30. * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
  31. */
  32. #ifdef CONFIG_X86
  33. #include <asm/apic.h>
  34. #include <asm/cpu.h>
  35. #endif
  36. #define ACPI_IDLE_STATE_START (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX) ? 1 : 0)
  37. static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
  38. module_param(max_cstate, uint, 0400);
  39. static bool nocst __read_mostly;
  40. module_param(nocst, bool, 0400);
  41. static bool bm_check_disable __read_mostly;
  42. module_param(bm_check_disable, bool, 0400);
  43. static unsigned int latency_factor __read_mostly = 2;
  44. module_param(latency_factor, uint, 0644);
  45. static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
  46. static struct cpuidle_driver acpi_idle_driver = {
  47. .name = "acpi_idle",
  48. .owner = THIS_MODULE,
  49. };
  50. #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
  51. void acpi_idle_rescan_dead_smt_siblings(void)
  52. {
  53. if (cpuidle_get_driver() == &acpi_idle_driver)
  54. arch_cpu_rescan_dead_smt_siblings();
  55. }
  56. static
  57. DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate);
  58. static int disabled_by_idle_boot_param(void)
  59. {
  60. return boot_option_idle_override == IDLE_POLL ||
  61. boot_option_idle_override == IDLE_HALT;
  62. }
  63. /*
  64. * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
  65. * For now disable this. Probably a bug somewhere else.
  66. *
  67. * To skip this limit, boot/load with a large max_cstate limit.
  68. */
  69. static int set_max_cstate(const struct dmi_system_id *id)
  70. {
  71. if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
  72. return 0;
  73. pr_notice("%s detected - limiting to C%ld max_cstate."
  74. " Override with \"processor.max_cstate=%d\"\n", id->ident,
  75. (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
  76. max_cstate = (long)id->driver_data;
  77. return 0;
  78. }
  79. static const struct dmi_system_id processor_power_dmi_table[] = {
  80. { set_max_cstate, "Clevo 5600D", {
  81. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  82. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  83. (void *)2},
  84. { set_max_cstate, "Pavilion zv5000", {
  85. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  86. DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
  87. (void *)1},
  88. { set_max_cstate, "Asus L8400B", {
  89. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  90. DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
  91. (void *)1},
  92. {},
  93. };
  94. /*
  95. * Callers should disable interrupts before the call and enable
  96. * interrupts after return.
  97. */
  98. static void __cpuidle acpi_safe_halt(void)
  99. {
  100. if (!tif_need_resched()) {
  101. raw_safe_halt();
  102. raw_local_irq_disable();
  103. }
  104. }
  105. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  106. /*
  107. * Some BIOS implementations switch to C3 in the published C2 state.
  108. * This seems to be a common problem on AMD boxen, but other vendors
  109. * are affected too. We pick the most conservative approach: we assume
  110. * that the local APIC stops in both C2 and C3.
  111. */
  112. static void lapic_timer_check_state(int state, struct acpi_processor *pr,
  113. struct acpi_processor_cx *cx)
  114. {
  115. struct acpi_processor_power *pwr = &pr->power;
  116. u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
  117. if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
  118. return;
  119. if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E))
  120. type = ACPI_STATE_C1;
  121. /*
  122. * Check, if one of the previous states already marked the lapic
  123. * unstable
  124. */
  125. if (pwr->timer_broadcast_on_state < state)
  126. return;
  127. if (cx->type >= type)
  128. pr->power.timer_broadcast_on_state = state;
  129. }
  130. static void __lapic_timer_propagate_broadcast(void *arg)
  131. {
  132. struct acpi_processor *pr = arg;
  133. if (pr->power.timer_broadcast_on_state < INT_MAX)
  134. tick_broadcast_enable();
  135. else
  136. tick_broadcast_disable();
  137. }
  138. static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
  139. {
  140. smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
  141. (void *)pr, 1);
  142. }
  143. /* Power(C) State timer broadcast control */
  144. static bool lapic_timer_needs_broadcast(struct acpi_processor *pr,
  145. struct acpi_processor_cx *cx)
  146. {
  147. return cx - pr->power.states >= pr->power.timer_broadcast_on_state;
  148. }
  149. #else
  150. static void lapic_timer_check_state(int state, struct acpi_processor *pr,
  151. struct acpi_processor_cx *cstate) { }
  152. static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
  153. static bool lapic_timer_needs_broadcast(struct acpi_processor *pr,
  154. struct acpi_processor_cx *cx)
  155. {
  156. return false;
  157. }
  158. #endif
  159. #if defined(CONFIG_X86)
  160. static void tsc_check_state(int state)
  161. {
  162. switch (boot_cpu_data.x86_vendor) {
  163. case X86_VENDOR_HYGON:
  164. case X86_VENDOR_AMD:
  165. case X86_VENDOR_INTEL:
  166. case X86_VENDOR_CENTAUR:
  167. case X86_VENDOR_ZHAOXIN:
  168. /*
  169. * AMD Fam10h TSC will tick in all
  170. * C/P/S0/S1 states when this bit is set.
  171. */
  172. if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  173. return;
  174. fallthrough;
  175. default:
  176. /* TSC could halt in idle, so notify users */
  177. if (state > ACPI_STATE_C1)
  178. mark_tsc_unstable("TSC halts in idle");
  179. }
  180. }
  181. #else
  182. static void tsc_check_state(int state) { return; }
  183. #endif
  184. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  185. {
  186. if (!pr->pblk)
  187. return -ENODEV;
  188. /* if info is obtained from pblk/fadt, type equals state */
  189. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  190. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  191. #ifndef CONFIG_HOTPLUG_CPU
  192. /*
  193. * Check for P_LVL2_UP flag before entering C2 and above on
  194. * an SMP system.
  195. */
  196. if ((num_online_cpus() > 1) &&
  197. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  198. return -ENODEV;
  199. #endif
  200. /* determine C2 and C3 address from pblk */
  201. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  202. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  203. /* determine latencies from FADT */
  204. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
  205. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
  206. /*
  207. * FADT specified C2 latency must be less than or equal to
  208. * 100 microseconds.
  209. */
  210. if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  211. acpi_handle_debug(pr->handle, "C2 latency too large [%d]\n",
  212. acpi_gbl_FADT.c2_latency);
  213. /* invalidate C2 */
  214. pr->power.states[ACPI_STATE_C2].address = 0;
  215. }
  216. /*
  217. * FADT supplied C3 latency must be less than or equal to
  218. * 1000 microseconds.
  219. */
  220. if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  221. acpi_handle_debug(pr->handle, "C3 latency too large [%d]\n",
  222. acpi_gbl_FADT.c3_latency);
  223. /* invalidate C3 */
  224. pr->power.states[ACPI_STATE_C3].address = 0;
  225. }
  226. acpi_handle_debug(pr->handle, "lvl2[0x%08x] lvl3[0x%08x]\n",
  227. pr->power.states[ACPI_STATE_C2].address,
  228. pr->power.states[ACPI_STATE_C3].address);
  229. snprintf(pr->power.states[ACPI_STATE_C2].desc,
  230. ACPI_CX_DESC_LEN, "ACPI P_LVL2 IOPORT 0x%x",
  231. pr->power.states[ACPI_STATE_C2].address);
  232. snprintf(pr->power.states[ACPI_STATE_C3].desc,
  233. ACPI_CX_DESC_LEN, "ACPI P_LVL3 IOPORT 0x%x",
  234. pr->power.states[ACPI_STATE_C3].address);
  235. if (!pr->power.states[ACPI_STATE_C2].address &&
  236. !pr->power.states[ACPI_STATE_C3].address)
  237. return -ENODEV;
  238. return 0;
  239. }
  240. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  241. {
  242. if (!pr->power.states[ACPI_STATE_C1].valid) {
  243. /* set the first C-State to C1 */
  244. /* all processors need to support C1 */
  245. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  246. pr->power.states[ACPI_STATE_C1].valid = 1;
  247. pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
  248. snprintf(pr->power.states[ACPI_STATE_C1].desc,
  249. ACPI_CX_DESC_LEN, "ACPI HLT");
  250. }
  251. /* the C0 state only exists as a filler in our array */
  252. pr->power.states[ACPI_STATE_C0].valid = 1;
  253. return 0;
  254. }
  255. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  256. {
  257. int ret;
  258. if (nocst)
  259. return -ENODEV;
  260. ret = acpi_processor_evaluate_cst(pr->handle, pr->id, &pr->power);
  261. if (ret)
  262. return ret;
  263. if (!pr->power.count)
  264. return -EFAULT;
  265. pr->flags.has_cst = 1;
  266. return 0;
  267. }
  268. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  269. struct acpi_processor_cx *cx)
  270. {
  271. static int bm_check_flag = -1;
  272. static int bm_control_flag = -1;
  273. if (!cx->address)
  274. return;
  275. /*
  276. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  277. * DMA transfers are used by any ISA device to avoid livelock.
  278. * Note that we could disable Type-F DMA (as recommended by
  279. * the erratum), but this is known to disrupt certain ISA
  280. * devices thus we take the conservative approach.
  281. */
  282. if (errata.piix4.fdma) {
  283. acpi_handle_debug(pr->handle,
  284. "C3 not supported on PIIX4 with Type-F DMA\n");
  285. return;
  286. }
  287. /* All the logic here assumes flags.bm_check is same across all CPUs */
  288. if (bm_check_flag == -1) {
  289. /* Determine whether bm_check is needed based on CPU */
  290. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  291. bm_check_flag = pr->flags.bm_check;
  292. bm_control_flag = pr->flags.bm_control;
  293. } else {
  294. pr->flags.bm_check = bm_check_flag;
  295. pr->flags.bm_control = bm_control_flag;
  296. }
  297. if (pr->flags.bm_check) {
  298. if (!pr->flags.bm_control) {
  299. if (pr->flags.has_cst != 1) {
  300. /* bus mastering control is necessary */
  301. acpi_handle_debug(pr->handle,
  302. "C3 support requires BM control\n");
  303. return;
  304. } else {
  305. /* Here we enter C3 without bus mastering */
  306. acpi_handle_debug(pr->handle,
  307. "C3 support without BM control\n");
  308. }
  309. }
  310. } else {
  311. /*
  312. * WBINVD should be set in fadt, for C3 state to be
  313. * supported on when bm_check is not required.
  314. */
  315. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  316. acpi_handle_debug(pr->handle,
  317. "Cache invalidation should work properly"
  318. " for C3 to be enabled on SMP systems\n");
  319. return;
  320. }
  321. }
  322. /*
  323. * Otherwise we've met all of our C3 requirements.
  324. * Normalize the C3 latency to expidite policy. Enable
  325. * checking of bus mastering status (bm_check) so we can
  326. * use this in our C3 policy
  327. */
  328. cx->valid = 1;
  329. /*
  330. * On older chipsets, BM_RLD needs to be set
  331. * in order for Bus Master activity to wake the
  332. * system from C3. Newer chipsets handle DMA
  333. * during C3 automatically and BM_RLD is a NOP.
  334. * In either case, the proper way to
  335. * handle BM_RLD is to set it and leave it set.
  336. */
  337. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  338. }
  339. static void acpi_cst_latency_sort(struct acpi_processor_cx *states, size_t length)
  340. {
  341. int i, j, k;
  342. for (i = 1; i < length; i++) {
  343. if (!states[i].valid)
  344. continue;
  345. for (j = i - 1, k = i; j >= 0; j--) {
  346. if (!states[j].valid)
  347. continue;
  348. if (states[j].latency > states[k].latency)
  349. swap(states[j].latency, states[k].latency);
  350. k = j;
  351. }
  352. }
  353. }
  354. static int acpi_processor_power_verify(struct acpi_processor *pr)
  355. {
  356. unsigned int i;
  357. unsigned int working = 0;
  358. unsigned int last_latency = 0;
  359. unsigned int last_type = 0;
  360. bool buggy_latency = false;
  361. pr->power.timer_broadcast_on_state = INT_MAX;
  362. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  363. struct acpi_processor_cx *cx = &pr->power.states[i];
  364. switch (cx->type) {
  365. case ACPI_STATE_C1:
  366. cx->valid = 1;
  367. break;
  368. case ACPI_STATE_C2:
  369. if (!cx->address)
  370. break;
  371. cx->valid = 1;
  372. break;
  373. case ACPI_STATE_C3:
  374. acpi_processor_power_verify_c3(pr, cx);
  375. break;
  376. }
  377. if (!cx->valid)
  378. continue;
  379. if (cx->type >= last_type && cx->latency < last_latency)
  380. buggy_latency = true;
  381. last_latency = cx->latency;
  382. last_type = cx->type;
  383. lapic_timer_check_state(i, pr, cx);
  384. tsc_check_state(cx->type);
  385. working++;
  386. }
  387. if (buggy_latency) {
  388. pr_notice("FW issue: working around C-state latencies out of order\n");
  389. acpi_cst_latency_sort(&pr->power.states[1], max_cstate);
  390. }
  391. lapic_timer_propagate_broadcast(pr);
  392. return working;
  393. }
  394. static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
  395. {
  396. int result;
  397. /* NOTE: the idle thread may not be running while calling
  398. * this function */
  399. /* Zero initialize all the C-states info. */
  400. memset(pr->power.states, 0, sizeof(pr->power.states));
  401. result = acpi_processor_get_power_info_cst(pr);
  402. if (result == -ENODEV)
  403. result = acpi_processor_get_power_info_fadt(pr);
  404. if (result)
  405. return result;
  406. acpi_processor_get_power_info_default(pr);
  407. pr->power.count = acpi_processor_power_verify(pr);
  408. pr->flags.power = 1;
  409. return 0;
  410. }
  411. /**
  412. * acpi_idle_bm_check - checks if bus master activity was detected
  413. */
  414. static int acpi_idle_bm_check(void)
  415. {
  416. u32 bm_status = 0;
  417. if (bm_check_disable)
  418. return 0;
  419. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  420. if (bm_status)
  421. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  422. /*
  423. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  424. * the true state of bus mastering activity; forcing us to
  425. * manually check the BMIDEA bit of each IDE channel.
  426. */
  427. else if (errata.piix4.bmisx) {
  428. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  429. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  430. bm_status = 1;
  431. }
  432. return bm_status;
  433. }
  434. static __cpuidle void io_idle(unsigned long addr)
  435. {
  436. /* IO port based C-state */
  437. inb(addr);
  438. #ifdef CONFIG_X86
  439. /* No delay is needed if we are in guest */
  440. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  441. return;
  442. /*
  443. * Modern (>=Nehalem) Intel systems use ACPI via intel_idle,
  444. * not this code. Assume that any Intel systems using this
  445. * are ancient and may need the dummy wait. This also assumes
  446. * that the motivating chipset issue was Intel-only.
  447. */
  448. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  449. return;
  450. #endif
  451. /*
  452. * Dummy wait op - must do something useless after P_LVL2 read
  453. * because chipsets cannot guarantee that STPCLK# signal gets
  454. * asserted in time to freeze execution properly
  455. *
  456. * This workaround has been in place since the original ACPI
  457. * implementation was merged, circa 2002.
  458. *
  459. * If a profile is pointing to this instruction, please first
  460. * consider moving your system to a more modern idle
  461. * mechanism.
  462. */
  463. inl(acpi_gbl_FADT.xpm_timer_block.address);
  464. }
  465. /**
  466. * acpi_idle_do_entry - enter idle state using the appropriate method
  467. * @cx: cstate data
  468. *
  469. * Caller disables interrupt before call and enables interrupt after return.
  470. */
  471. static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx)
  472. {
  473. perf_lopwr_cb(true);
  474. if (cx->entry_method == ACPI_CSTATE_FFH) {
  475. /* Call into architectural FFH based C-state */
  476. acpi_processor_ffh_cstate_enter(cx);
  477. } else if (cx->entry_method == ACPI_CSTATE_HALT) {
  478. acpi_safe_halt();
  479. } else {
  480. io_idle(cx->address);
  481. }
  482. perf_lopwr_cb(false);
  483. }
  484. /**
  485. * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
  486. * @dev: the target CPU
  487. * @index: the index of suggested state
  488. */
  489. static void acpi_idle_play_dead(struct cpuidle_device *dev, int index)
  490. {
  491. struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
  492. ACPI_FLUSH_CPU_CACHE();
  493. while (1) {
  494. if (cx->entry_method == ACPI_CSTATE_HALT)
  495. raw_safe_halt();
  496. else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
  497. io_idle(cx->address);
  498. } else if (cx->entry_method == ACPI_CSTATE_FFH) {
  499. acpi_processor_ffh_play_dead(cx);
  500. } else
  501. return;
  502. }
  503. }
  504. static __always_inline bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
  505. {
  506. return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
  507. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
  508. }
  509. static int c3_cpu_count;
  510. static DEFINE_RAW_SPINLOCK(c3_lock);
  511. /**
  512. * acpi_idle_enter_bm - enters C3 with proper BM handling
  513. * @drv: cpuidle driver
  514. * @pr: Target processor
  515. * @cx: Target state context
  516. * @index: index of target state
  517. */
  518. static int __cpuidle acpi_idle_enter_bm(struct cpuidle_driver *drv,
  519. struct acpi_processor *pr,
  520. struct acpi_processor_cx *cx,
  521. int index)
  522. {
  523. static struct acpi_processor_cx safe_cx = {
  524. .entry_method = ACPI_CSTATE_HALT,
  525. };
  526. /*
  527. * disable bus master
  528. * bm_check implies we need ARB_DIS
  529. * bm_control implies whether we can do ARB_DIS
  530. *
  531. * That leaves a case where bm_check is set and bm_control is not set.
  532. * In that case we cannot do much, we enter C3 without doing anything.
  533. */
  534. bool dis_bm = pr->flags.bm_control;
  535. instrumentation_begin();
  536. /* If we can skip BM, demote to a safe state. */
  537. if (!cx->bm_sts_skip && acpi_idle_bm_check()) {
  538. dis_bm = false;
  539. index = drv->safe_state_index;
  540. if (index >= 0) {
  541. cx = this_cpu_read(acpi_cstate[index]);
  542. } else {
  543. cx = &safe_cx;
  544. index = -EBUSY;
  545. }
  546. }
  547. if (dis_bm) {
  548. raw_spin_lock(&c3_lock);
  549. c3_cpu_count++;
  550. /* Disable bus master arbitration when all CPUs are in C3 */
  551. if (c3_cpu_count == num_online_cpus())
  552. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
  553. raw_spin_unlock(&c3_lock);
  554. }
  555. ct_cpuidle_enter();
  556. acpi_idle_do_entry(cx);
  557. ct_cpuidle_exit();
  558. /* Re-enable bus master arbitration */
  559. if (dis_bm) {
  560. raw_spin_lock(&c3_lock);
  561. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
  562. c3_cpu_count--;
  563. raw_spin_unlock(&c3_lock);
  564. }
  565. instrumentation_end();
  566. return index;
  567. }
  568. static int __cpuidle acpi_idle_enter(struct cpuidle_device *dev,
  569. struct cpuidle_driver *drv, int index)
  570. {
  571. struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
  572. struct acpi_processor *pr;
  573. pr = __this_cpu_read(processors);
  574. if (unlikely(!pr))
  575. return -EINVAL;
  576. if (cx->type != ACPI_STATE_C1) {
  577. if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check)
  578. return acpi_idle_enter_bm(drv, pr, cx, index);
  579. /* C2 to C1 demotion. */
  580. if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
  581. index = ACPI_IDLE_STATE_START;
  582. cx = per_cpu(acpi_cstate[index], dev->cpu);
  583. }
  584. }
  585. if (cx->type == ACPI_STATE_C3)
  586. ACPI_FLUSH_CPU_CACHE();
  587. acpi_idle_do_entry(cx);
  588. return index;
  589. }
  590. static int __cpuidle acpi_idle_enter_s2idle(struct cpuidle_device *dev,
  591. struct cpuidle_driver *drv, int index)
  592. {
  593. struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
  594. if (cx->type == ACPI_STATE_C3) {
  595. struct acpi_processor *pr = __this_cpu_read(processors);
  596. if (unlikely(!pr))
  597. return 0;
  598. if (pr->flags.bm_check) {
  599. u8 bm_sts_skip = cx->bm_sts_skip;
  600. /* Don't check BM_STS, do an unconditional ARB_DIS for S2IDLE */
  601. cx->bm_sts_skip = 1;
  602. acpi_idle_enter_bm(drv, pr, cx, index);
  603. cx->bm_sts_skip = bm_sts_skip;
  604. return 0;
  605. } else {
  606. ACPI_FLUSH_CPU_CACHE();
  607. }
  608. }
  609. acpi_idle_do_entry(cx);
  610. return 0;
  611. }
  612. static void acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
  613. struct cpuidle_device *dev)
  614. {
  615. int i, count = ACPI_IDLE_STATE_START;
  616. struct acpi_processor_cx *cx;
  617. if (max_cstate == 0)
  618. max_cstate = 1;
  619. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  620. cx = &pr->power.states[i];
  621. if (!cx->valid)
  622. continue;
  623. per_cpu(acpi_cstate[count], dev->cpu) = cx;
  624. count++;
  625. if (count == CPUIDLE_STATE_MAX)
  626. break;
  627. }
  628. }
  629. static void acpi_processor_setup_cstates(struct acpi_processor *pr)
  630. {
  631. int i, count;
  632. struct acpi_processor_cx *cx;
  633. struct cpuidle_state *state;
  634. struct cpuidle_driver *drv = &acpi_idle_driver;
  635. if (max_cstate == 0)
  636. max_cstate = 1;
  637. if (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX)) {
  638. cpuidle_poll_state_init(drv);
  639. count = 1;
  640. } else {
  641. count = 0;
  642. }
  643. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  644. cx = &pr->power.states[i];
  645. if (!cx->valid)
  646. continue;
  647. state = &drv->states[count];
  648. snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
  649. strscpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
  650. state->exit_latency = cx->latency;
  651. state->target_residency = cx->latency * latency_factor;
  652. state->enter = acpi_idle_enter;
  653. state->flags = 0;
  654. state->enter_dead = acpi_idle_play_dead;
  655. if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2)
  656. drv->safe_state_index = count;
  657. /*
  658. * Halt-induced C1 is not good for ->enter_s2idle, because it
  659. * re-enables interrupts on exit. Moreover, C1 is generally not
  660. * particularly interesting from the suspend-to-idle angle, so
  661. * avoid C1 and the situations in which we may need to fall back
  662. * to it altogether.
  663. */
  664. if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
  665. state->enter_s2idle = acpi_idle_enter_s2idle;
  666. if (lapic_timer_needs_broadcast(pr, cx))
  667. state->flags |= CPUIDLE_FLAG_TIMER_STOP;
  668. if (cx->type == ACPI_STATE_C3) {
  669. state->flags |= CPUIDLE_FLAG_TLB_FLUSHED;
  670. if (pr->flags.bm_check)
  671. state->flags |= CPUIDLE_FLAG_RCU_IDLE;
  672. }
  673. count++;
  674. if (count == CPUIDLE_STATE_MAX)
  675. break;
  676. }
  677. drv->state_count = count;
  678. }
  679. static inline void acpi_processor_cstate_first_run_checks(void)
  680. {
  681. static int first_run;
  682. if (first_run)
  683. return;
  684. dmi_check_system(processor_power_dmi_table);
  685. max_cstate = acpi_processor_cstate_check(max_cstate);
  686. if (max_cstate < ACPI_C_STATES_MAX)
  687. pr_notice("processor limited to max C-state %d\n", max_cstate);
  688. first_run++;
  689. if (nocst)
  690. return;
  691. acpi_processor_claim_cst_control();
  692. }
  693. #else
  694. static inline int disabled_by_idle_boot_param(void) { return 0; }
  695. static inline void acpi_processor_cstate_first_run_checks(void) { }
  696. static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
  697. {
  698. return -ENODEV;
  699. }
  700. static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
  701. struct cpuidle_device *dev)
  702. {
  703. return -EINVAL;
  704. }
  705. static int acpi_processor_setup_cstates(struct acpi_processor *pr)
  706. {
  707. return -EINVAL;
  708. }
  709. #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
  710. struct acpi_lpi_states_array {
  711. unsigned int size;
  712. unsigned int composite_states_size;
  713. struct acpi_lpi_state *entries;
  714. struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER];
  715. };
  716. static int obj_get_integer(union acpi_object *obj, u32 *value)
  717. {
  718. if (obj->type != ACPI_TYPE_INTEGER)
  719. return -EINVAL;
  720. *value = obj->integer.value;
  721. return 0;
  722. }
  723. static int acpi_processor_evaluate_lpi(acpi_handle handle,
  724. struct acpi_lpi_states_array *info)
  725. {
  726. acpi_status status;
  727. int ret = 0;
  728. int pkg_count, state_idx = 1, loop;
  729. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  730. union acpi_object *lpi_data;
  731. struct acpi_lpi_state *lpi_state;
  732. status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer);
  733. if (ACPI_FAILURE(status)) {
  734. acpi_handle_debug(handle, "No _LPI, giving up\n");
  735. return -ENODEV;
  736. }
  737. lpi_data = buffer.pointer;
  738. /* There must be at least 4 elements = 3 elements + 1 package */
  739. if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE ||
  740. lpi_data->package.count < 4) {
  741. pr_debug("not enough elements in _LPI\n");
  742. ret = -ENODATA;
  743. goto end;
  744. }
  745. pkg_count = lpi_data->package.elements[2].integer.value;
  746. /* Validate number of power states. */
  747. if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) {
  748. pr_debug("count given by _LPI is not valid\n");
  749. ret = -ENODATA;
  750. goto end;
  751. }
  752. lpi_state = kzalloc_objs(*lpi_state, pkg_count);
  753. if (!lpi_state) {
  754. ret = -ENOMEM;
  755. goto end;
  756. }
  757. info->size = pkg_count;
  758. info->entries = lpi_state;
  759. /* LPI States start at index 3 */
  760. for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) {
  761. union acpi_object *element, *pkg_elem, *obj;
  762. element = &lpi_data->package.elements[loop];
  763. if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7)
  764. continue;
  765. pkg_elem = element->package.elements;
  766. obj = pkg_elem + 6;
  767. if (obj->type == ACPI_TYPE_BUFFER) {
  768. struct acpi_power_register *reg;
  769. reg = (struct acpi_power_register *)obj->buffer.pointer;
  770. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  771. reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)
  772. continue;
  773. lpi_state->address = reg->address;
  774. lpi_state->entry_method =
  775. reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ?
  776. ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO;
  777. } else if (obj->type == ACPI_TYPE_INTEGER) {
  778. lpi_state->entry_method = ACPI_CSTATE_INTEGER;
  779. lpi_state->address = obj->integer.value;
  780. } else {
  781. pr_debug("Entry method of state-%d is invalid, disable it.\n",
  782. state_idx);
  783. continue;
  784. }
  785. /* elements[7,8] skipped for now i.e. Residency/Usage counter*/
  786. obj = pkg_elem + 9;
  787. if (obj->type == ACPI_TYPE_STRING)
  788. strscpy(lpi_state->desc, obj->string.pointer,
  789. ACPI_CX_DESC_LEN);
  790. lpi_state->index = state_idx;
  791. if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) {
  792. pr_debug("No min. residency found, assuming 10 us\n");
  793. lpi_state->min_residency = 10;
  794. }
  795. if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) {
  796. pr_debug("No wakeup residency found, assuming 10 us\n");
  797. lpi_state->wake_latency = 10;
  798. }
  799. if (obj_get_integer(pkg_elem + 2, &lpi_state->flags))
  800. lpi_state->flags = 0;
  801. if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags))
  802. lpi_state->arch_flags = 0;
  803. if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq))
  804. lpi_state->res_cnt_freq = 1;
  805. if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state))
  806. lpi_state->enable_parent_state = 0;
  807. }
  808. acpi_handle_debug(handle, "Found %d power states\n", state_idx);
  809. end:
  810. kfree(buffer.pointer);
  811. return ret;
  812. }
  813. /**
  814. * combine_lpi_states - combine local and parent LPI states to form a composite LPI state
  815. *
  816. * @local: local LPI state
  817. * @parent: parent LPI state
  818. * @result: composite LPI state
  819. */
  820. static bool combine_lpi_states(struct acpi_lpi_state *local,
  821. struct acpi_lpi_state *parent,
  822. struct acpi_lpi_state *result)
  823. {
  824. if (parent->entry_method == ACPI_CSTATE_INTEGER) {
  825. if (!parent->address) /* 0 means autopromotable */
  826. return false;
  827. result->address = local->address + parent->address;
  828. } else {
  829. result->address = parent->address;
  830. }
  831. result->min_residency = max(local->min_residency, parent->min_residency);
  832. result->wake_latency = local->wake_latency + parent->wake_latency;
  833. result->enable_parent_state = parent->enable_parent_state;
  834. result->entry_method = local->entry_method;
  835. result->flags = parent->flags;
  836. result->arch_flags = parent->arch_flags;
  837. result->index = parent->index;
  838. strscpy(result->desc, local->desc, ACPI_CX_DESC_LEN);
  839. strlcat(result->desc, "+", ACPI_CX_DESC_LEN);
  840. strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN);
  841. return true;
  842. }
  843. #define ACPI_LPI_STATE_FLAGS_ENABLED BIT(0)
  844. static void stash_composite_state(struct acpi_lpi_states_array *curr_level,
  845. struct acpi_lpi_state *t)
  846. {
  847. curr_level->composite_states[curr_level->composite_states_size++] = t;
  848. }
  849. static unsigned int flatten_lpi_states(struct acpi_processor *pr,
  850. unsigned int flat_state_cnt,
  851. struct acpi_lpi_states_array *curr_level,
  852. struct acpi_lpi_states_array *prev_level)
  853. {
  854. int i, j, state_count = curr_level->size;
  855. struct acpi_lpi_state *p, *t = curr_level->entries;
  856. curr_level->composite_states_size = 0;
  857. for (j = 0; j < state_count; j++, t++) {
  858. struct acpi_lpi_state *flpi;
  859. if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED))
  860. continue;
  861. if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) {
  862. pr_warn("Limiting number of LPI states to max (%d)\n",
  863. ACPI_PROCESSOR_MAX_POWER);
  864. pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  865. break;
  866. }
  867. flpi = &pr->power.lpi_states[flat_state_cnt];
  868. if (!prev_level) { /* leaf/processor node */
  869. memcpy(flpi, t, sizeof(*t));
  870. stash_composite_state(curr_level, flpi);
  871. flat_state_cnt++;
  872. continue;
  873. }
  874. for (i = 0; i < prev_level->composite_states_size; i++) {
  875. p = prev_level->composite_states[i];
  876. if (t->index <= p->enable_parent_state &&
  877. combine_lpi_states(p, t, flpi)) {
  878. stash_composite_state(curr_level, flpi);
  879. flat_state_cnt++;
  880. flpi++;
  881. }
  882. }
  883. }
  884. kfree(curr_level->entries);
  885. return flat_state_cnt;
  886. }
  887. int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu)
  888. {
  889. return -EOPNOTSUPP;
  890. }
  891. static int acpi_processor_get_lpi_info(struct acpi_processor *pr)
  892. {
  893. int ret, i;
  894. acpi_status status;
  895. acpi_handle handle = pr->handle, pr_ahandle;
  896. struct acpi_device *d = NULL;
  897. struct acpi_lpi_states_array info[2], *tmp, *prev, *curr;
  898. unsigned int state_count;
  899. /* make sure our architecture has support */
  900. ret = acpi_processor_ffh_lpi_probe(pr->id);
  901. if (ret == -EOPNOTSUPP)
  902. return ret;
  903. if (!osc_pc_lpi_support_confirmed)
  904. return -EOPNOTSUPP;
  905. if (!acpi_has_method(handle, "_LPI"))
  906. return -EINVAL;
  907. prev = &info[0];
  908. curr = &info[1];
  909. handle = pr->handle;
  910. ret = acpi_processor_evaluate_lpi(handle, prev);
  911. if (ret)
  912. return ret;
  913. state_count = flatten_lpi_states(pr, 0, prev, NULL);
  914. status = acpi_get_parent(handle, &pr_ahandle);
  915. while (ACPI_SUCCESS(status)) {
  916. d = acpi_fetch_acpi_dev(pr_ahandle);
  917. if (!d)
  918. break;
  919. handle = pr_ahandle;
  920. if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID))
  921. break;
  922. /* can be optional ? */
  923. if (!acpi_has_method(handle, "_LPI"))
  924. break;
  925. ret = acpi_processor_evaluate_lpi(handle, curr);
  926. if (ret)
  927. break;
  928. /* flatten all the LPI states in this level of hierarchy */
  929. state_count = flatten_lpi_states(pr, state_count, curr, prev);
  930. tmp = prev, prev = curr, curr = tmp;
  931. status = acpi_get_parent(handle, &pr_ahandle);
  932. }
  933. /* reset the index after flattening */
  934. for (i = 0; i < state_count; i++)
  935. pr->power.lpi_states[i].index = i;
  936. pr->power.count = state_count;
  937. /* Tell driver that _LPI is supported. */
  938. pr->flags.has_lpi = 1;
  939. pr->flags.power = 1;
  940. return 0;
  941. }
  942. int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi)
  943. {
  944. return -ENODEV;
  945. }
  946. /**
  947. * acpi_idle_lpi_enter - enters an ACPI any LPI state
  948. * @dev: the target CPU
  949. * @drv: cpuidle driver containing cpuidle state info
  950. * @index: index of target state
  951. *
  952. * Return: 0 for success or negative value for error
  953. */
  954. static int acpi_idle_lpi_enter(struct cpuidle_device *dev,
  955. struct cpuidle_driver *drv, int index)
  956. {
  957. struct acpi_processor *pr;
  958. struct acpi_lpi_state *lpi;
  959. pr = __this_cpu_read(processors);
  960. if (unlikely(!pr))
  961. return -EINVAL;
  962. lpi = &pr->power.lpi_states[index];
  963. if (lpi->entry_method == ACPI_CSTATE_FFH)
  964. return acpi_processor_ffh_lpi_enter(lpi);
  965. return -EINVAL;
  966. }
  967. static void acpi_processor_setup_lpi_states(struct acpi_processor *pr)
  968. {
  969. int i;
  970. struct acpi_lpi_state *lpi;
  971. struct cpuidle_state *state;
  972. struct cpuidle_driver *drv = &acpi_idle_driver;
  973. if (!pr->flags.has_lpi)
  974. return;
  975. for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) {
  976. lpi = &pr->power.lpi_states[i];
  977. state = &drv->states[i];
  978. snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i);
  979. strscpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN);
  980. state->exit_latency = lpi->wake_latency;
  981. state->target_residency = lpi->min_residency;
  982. state->flags |= arch_get_idle_state_flags(lpi->arch_flags);
  983. if (i != 0 && lpi->entry_method == ACPI_CSTATE_FFH)
  984. state->flags |= CPUIDLE_FLAG_RCU_IDLE;
  985. state->enter = acpi_idle_lpi_enter;
  986. drv->safe_state_index = i;
  987. }
  988. drv->state_count = i;
  989. }
  990. /**
  991. * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle
  992. * global state data i.e. idle routines
  993. *
  994. * @pr: the ACPI processor
  995. */
  996. static void acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
  997. {
  998. int i;
  999. struct cpuidle_driver *drv = &acpi_idle_driver;
  1000. if (!pr->flags.power_setup_done || !pr->flags.power)
  1001. return;
  1002. drv->safe_state_index = -1;
  1003. for (i = ACPI_IDLE_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
  1004. drv->states[i].name[0] = '\0';
  1005. drv->states[i].desc[0] = '\0';
  1006. }
  1007. if (pr->flags.has_lpi) {
  1008. acpi_processor_setup_lpi_states(pr);
  1009. return;
  1010. }
  1011. acpi_processor_setup_cstates(pr);
  1012. }
  1013. /**
  1014. * acpi_processor_setup_cpuidle_dev - configures CPUIDLE
  1015. * device i.e. per-cpu data
  1016. *
  1017. * @pr: the ACPI processor
  1018. * @dev : the cpuidle device
  1019. */
  1020. static void acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr,
  1021. struct cpuidle_device *dev)
  1022. {
  1023. if (!pr->flags.power_setup_done || !pr->flags.power || !dev)
  1024. return;
  1025. dev->cpu = pr->id;
  1026. if (!pr->flags.has_lpi)
  1027. acpi_processor_setup_cpuidle_cx(pr, dev);
  1028. }
  1029. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  1030. {
  1031. int ret;
  1032. ret = acpi_processor_get_lpi_info(pr);
  1033. if (ret)
  1034. return acpi_processor_get_cstate_info(pr);
  1035. if (pr->flags.has_lpi) {
  1036. ret = acpi_processor_ffh_lpi_probe(pr->id);
  1037. if (ret)
  1038. pr_err("CPU%u: Invalid FFH LPI data\n", pr->id);
  1039. }
  1040. return ret;
  1041. }
  1042. int acpi_processor_hotplug(struct acpi_processor *pr)
  1043. {
  1044. int ret = 0;
  1045. struct cpuidle_device *dev;
  1046. if (disabled_by_idle_boot_param())
  1047. return 0;
  1048. if (!pr->flags.power_setup_done)
  1049. return -ENODEV;
  1050. dev = per_cpu(acpi_cpuidle_device, pr->id);
  1051. cpuidle_pause_and_lock();
  1052. cpuidle_disable_device(dev);
  1053. ret = acpi_processor_get_power_info(pr);
  1054. if (!ret && pr->flags.power) {
  1055. acpi_processor_setup_cpuidle_dev(pr, dev);
  1056. ret = cpuidle_enable_device(dev);
  1057. }
  1058. cpuidle_resume_and_unlock();
  1059. return ret;
  1060. }
  1061. int acpi_processor_power_state_has_changed(struct acpi_processor *pr)
  1062. {
  1063. int cpu;
  1064. struct acpi_processor *_pr;
  1065. struct cpuidle_device *dev;
  1066. if (disabled_by_idle_boot_param())
  1067. return 0;
  1068. if (!pr->flags.power_setup_done)
  1069. return -ENODEV;
  1070. /*
  1071. * FIXME: Design the ACPI notification to make it once per
  1072. * system instead of once per-cpu. This condition is a hack
  1073. * to make the code that updates C-States be called once.
  1074. */
  1075. if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
  1076. /* Protect against cpu-hotplug */
  1077. cpus_read_lock();
  1078. cpuidle_pause_and_lock();
  1079. /* Disable all cpuidle devices */
  1080. for_each_online_cpu(cpu) {
  1081. _pr = per_cpu(processors, cpu);
  1082. if (!_pr || !_pr->flags.power_setup_done)
  1083. continue;
  1084. dev = per_cpu(acpi_cpuidle_device, cpu);
  1085. cpuidle_disable_device(dev);
  1086. }
  1087. /* Populate Updated C-state information */
  1088. acpi_processor_get_power_info(pr);
  1089. acpi_processor_setup_cpuidle_states(pr);
  1090. /* Enable all cpuidle devices */
  1091. for_each_online_cpu(cpu) {
  1092. _pr = per_cpu(processors, cpu);
  1093. if (!_pr || !_pr->flags.power_setup_done)
  1094. continue;
  1095. acpi_processor_get_power_info(_pr);
  1096. if (_pr->flags.power) {
  1097. dev = per_cpu(acpi_cpuidle_device, cpu);
  1098. acpi_processor_setup_cpuidle_dev(_pr, dev);
  1099. cpuidle_enable_device(dev);
  1100. }
  1101. }
  1102. cpuidle_resume_and_unlock();
  1103. cpus_read_unlock();
  1104. }
  1105. return 0;
  1106. }
  1107. void acpi_processor_register_idle_driver(void)
  1108. {
  1109. struct acpi_processor *pr;
  1110. int ret = -ENODEV;
  1111. int cpu;
  1112. /*
  1113. * ACPI idle driver is used by all possible CPUs.
  1114. * Use the processor power info of one in them to set up idle states.
  1115. * Note that the existing idle handler will be used on platforms that
  1116. * only support C1.
  1117. */
  1118. for_each_possible_cpu(cpu) {
  1119. pr = per_cpu(processors, cpu);
  1120. if (!pr)
  1121. continue;
  1122. acpi_processor_cstate_first_run_checks();
  1123. ret = acpi_processor_get_power_info(pr);
  1124. if (!ret) {
  1125. pr->flags.power_setup_done = 1;
  1126. acpi_processor_setup_cpuidle_states(pr);
  1127. break;
  1128. }
  1129. }
  1130. if (ret) {
  1131. pr_debug("No ACPI power information from any CPUs.\n");
  1132. return;
  1133. }
  1134. ret = cpuidle_register_driver(&acpi_idle_driver);
  1135. if (ret) {
  1136. pr_debug("register %s failed.\n", acpi_idle_driver.name);
  1137. return;
  1138. }
  1139. pr_debug("%s registered with cpuidle.\n", acpi_idle_driver.name);
  1140. }
  1141. void acpi_processor_unregister_idle_driver(void)
  1142. {
  1143. cpuidle_unregister_driver(&acpi_idle_driver);
  1144. }
  1145. void acpi_processor_power_init(struct acpi_processor *pr)
  1146. {
  1147. struct cpuidle_device *dev;
  1148. /*
  1149. * The code below only works if the current cpuidle driver is the ACPI
  1150. * idle driver.
  1151. */
  1152. if (cpuidle_get_driver() != &acpi_idle_driver)
  1153. return;
  1154. if (disabled_by_idle_boot_param())
  1155. return;
  1156. acpi_processor_cstate_first_run_checks();
  1157. if (!acpi_processor_get_power_info(pr))
  1158. pr->flags.power_setup_done = 1;
  1159. if (!pr->flags.power)
  1160. return;
  1161. dev = kzalloc_obj(*dev);
  1162. if (!dev)
  1163. return;
  1164. per_cpu(acpi_cpuidle_device, pr->id) = dev;
  1165. acpi_processor_setup_cpuidle_dev(pr, dev);
  1166. /*
  1167. * Register a cpuidle device for this CPU. The cpuidle driver using
  1168. * this device is expected to be registered.
  1169. */
  1170. if (cpuidle_register_device(dev)) {
  1171. per_cpu(acpi_cpuidle_device, pr->id) = NULL;
  1172. kfree(dev);
  1173. }
  1174. }
  1175. void acpi_processor_power_exit(struct acpi_processor *pr)
  1176. {
  1177. struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
  1178. if (disabled_by_idle_boot_param())
  1179. return;
  1180. if (pr->flags.power) {
  1181. cpuidle_unregister_device(dev);
  1182. kfree(dev);
  1183. }
  1184. pr->flags.power_setup_done = 0;
  1185. }
  1186. MODULE_IMPORT_NS("ACPI_PROCESSOR_IDLE");