processor_idle.c 36 KB

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