processor_idle.c 36 KB

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