processor_idle.c 36 KB

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