processor_idle.c 35 KB

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