idle.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerNV cpuidle code
  4. *
  5. * Copyright 2015 IBM Corp.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/mm.h>
  9. #include <linux/slab.h>
  10. #include <linux/of.h>
  11. #include <linux/device.h>
  12. #include <linux/cpu.h>
  13. #include <asm/firmware.h>
  14. #include <asm/interrupt.h>
  15. #include <asm/machdep.h>
  16. #include <asm/opal.h>
  17. #include <asm/cputhreads.h>
  18. #include <asm/cpuidle.h>
  19. #include <asm/code-patching.h>
  20. #include <asm/smp.h>
  21. #include <asm/runlatch.h>
  22. #include <asm/dbell.h>
  23. #include "powernv.h"
  24. #include "subcore.h"
  25. /* Power ISA 3.0 allows for stop states 0x0 - 0xF */
  26. #define MAX_STOP_STATE 0xF
  27. #define P9_STOP_SPR_MSR 2000
  28. #define P9_STOP_SPR_PSSCR 855
  29. static u32 supported_cpuidle_states;
  30. struct pnv_idle_states_t *pnv_idle_states;
  31. int nr_pnv_idle_states;
  32. /*
  33. * The default stop state that will be used by ppc_md.power_save
  34. * function on platforms that support stop instruction.
  35. */
  36. static u64 pnv_default_stop_val;
  37. static u64 pnv_default_stop_mask;
  38. static bool default_stop_found;
  39. /*
  40. * First stop state levels when SPR and TB loss can occur.
  41. */
  42. static u64 pnv_first_tb_loss_level = MAX_STOP_STATE + 1;
  43. static u64 deep_spr_loss_state = MAX_STOP_STATE + 1;
  44. /*
  45. * psscr value and mask of the deepest stop idle state.
  46. * Used when a cpu is offlined.
  47. */
  48. static u64 pnv_deepest_stop_psscr_val;
  49. static u64 pnv_deepest_stop_psscr_mask;
  50. static u64 pnv_deepest_stop_flag;
  51. static bool deepest_stop_found;
  52. static unsigned long power7_offline_type;
  53. static int __init pnv_save_sprs_for_deep_states(void)
  54. {
  55. int cpu;
  56. int rc;
  57. /*
  58. * hid0, hid1, hid4, hid5, hmeer and lpcr values are symmetric across
  59. * all cpus at boot. Get these reg values of current cpu and use the
  60. * same across all cpus.
  61. */
  62. uint64_t lpcr_val = mfspr(SPRN_LPCR);
  63. uint64_t hid0_val = mfspr(SPRN_HID0);
  64. uint64_t hmeer_val = mfspr(SPRN_HMEER);
  65. uint64_t msr_val = MSR_IDLE;
  66. uint64_t psscr_val = pnv_deepest_stop_psscr_val;
  67. for_each_present_cpu(cpu) {
  68. uint64_t pir = get_hard_smp_processor_id(cpu);
  69. uint64_t hsprg0_val = (uint64_t)paca_ptrs[cpu];
  70. rc = opal_slw_set_reg(pir, SPRN_HSPRG0, hsprg0_val);
  71. if (rc != 0)
  72. return rc;
  73. rc = opal_slw_set_reg(pir, SPRN_LPCR, lpcr_val);
  74. if (rc != 0)
  75. return rc;
  76. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  77. rc = opal_slw_set_reg(pir, P9_STOP_SPR_MSR, msr_val);
  78. if (rc)
  79. return rc;
  80. rc = opal_slw_set_reg(pir,
  81. P9_STOP_SPR_PSSCR, psscr_val);
  82. if (rc)
  83. return rc;
  84. }
  85. /* HIDs are per core registers */
  86. if (cpu_thread_in_core(cpu) == 0) {
  87. rc = opal_slw_set_reg(pir, SPRN_HMEER, hmeer_val);
  88. if (rc != 0)
  89. return rc;
  90. rc = opal_slw_set_reg(pir, SPRN_HID0, hid0_val);
  91. if (rc != 0)
  92. return rc;
  93. /* Only p8 needs to set extra HID registers */
  94. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  95. uint64_t hid1_val = mfspr(SPRN_HID1);
  96. uint64_t hid4_val = mfspr(SPRN_HID4);
  97. uint64_t hid5_val = mfspr(SPRN_HID5);
  98. rc = opal_slw_set_reg(pir, SPRN_HID1, hid1_val);
  99. if (rc != 0)
  100. return rc;
  101. rc = opal_slw_set_reg(pir, SPRN_HID4, hid4_val);
  102. if (rc != 0)
  103. return rc;
  104. rc = opal_slw_set_reg(pir, SPRN_HID5, hid5_val);
  105. if (rc != 0)
  106. return rc;
  107. }
  108. }
  109. }
  110. return 0;
  111. }
  112. u32 pnv_get_supported_cpuidle_states(void)
  113. {
  114. return supported_cpuidle_states;
  115. }
  116. EXPORT_SYMBOL_GPL(pnv_get_supported_cpuidle_states);
  117. static void pnv_fastsleep_workaround_apply(void *info)
  118. {
  119. int cpu = smp_processor_id();
  120. int rc;
  121. int *err = info;
  122. if (cpu_first_thread_sibling(cpu) != cpu)
  123. return;
  124. rc = opal_config_cpu_idle_state(OPAL_CONFIG_IDLE_FASTSLEEP,
  125. OPAL_CONFIG_IDLE_APPLY);
  126. if (rc)
  127. *err = 1;
  128. }
  129. static bool power7_fastsleep_workaround_entry = true;
  130. static bool power7_fastsleep_workaround_exit = true;
  131. /*
  132. * Used to store fastsleep workaround state
  133. * 0 - Workaround applied/undone at fastsleep entry/exit path (Default)
  134. * 1 - Workaround applied once, never undone.
  135. */
  136. static u8 fastsleep_workaround_applyonce;
  137. static ssize_t show_fastsleep_workaround_applyonce(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. return sprintf(buf, "%u\n", fastsleep_workaround_applyonce);
  141. }
  142. static ssize_t store_fastsleep_workaround_applyonce(struct device *dev,
  143. struct device_attribute *attr, const char *buf,
  144. size_t count)
  145. {
  146. int err;
  147. u8 val;
  148. if (kstrtou8(buf, 0, &val) || val != 1)
  149. return -EINVAL;
  150. if (fastsleep_workaround_applyonce == 1)
  151. return count;
  152. /*
  153. * fastsleep_workaround_applyonce = 1 implies
  154. * fastsleep workaround needs to be left in 'applied' state on all
  155. * the cores. Do this by-
  156. * 1. Disable the 'undo' workaround in fastsleep exit path
  157. * 2. Sendi IPIs to all the cores which have at least one online thread
  158. * 3. Disable the 'apply' workaround in fastsleep entry path
  159. *
  160. * There is no need to send ipi to cores which have all threads
  161. * offlined, as last thread of the core entering fastsleep or deeper
  162. * state would have applied workaround.
  163. */
  164. power7_fastsleep_workaround_exit = false;
  165. cpus_read_lock();
  166. on_each_cpu(pnv_fastsleep_workaround_apply, &err, 1);
  167. cpus_read_unlock();
  168. if (err) {
  169. pr_err("fastsleep_workaround_applyonce change failed while running pnv_fastsleep_workaround_apply");
  170. goto fail;
  171. }
  172. power7_fastsleep_workaround_entry = false;
  173. fastsleep_workaround_applyonce = 1;
  174. return count;
  175. fail:
  176. return -EIO;
  177. }
  178. static DEVICE_ATTR(fastsleep_workaround_applyonce, 0600,
  179. show_fastsleep_workaround_applyonce,
  180. store_fastsleep_workaround_applyonce);
  181. static inline void atomic_start_thread_idle(void)
  182. {
  183. int cpu = raw_smp_processor_id();
  184. int first = cpu_first_thread_sibling(cpu);
  185. int thread_nr = cpu_thread_in_core(cpu);
  186. unsigned long *state = &paca_ptrs[first]->idle_state;
  187. clear_bit(thread_nr, state);
  188. }
  189. static inline void atomic_stop_thread_idle(void)
  190. {
  191. int cpu = raw_smp_processor_id();
  192. int first = cpu_first_thread_sibling(cpu);
  193. int thread_nr = cpu_thread_in_core(cpu);
  194. unsigned long *state = &paca_ptrs[first]->idle_state;
  195. set_bit(thread_nr, state);
  196. }
  197. static inline void atomic_lock_thread_idle(void)
  198. {
  199. int cpu = raw_smp_processor_id();
  200. int first = cpu_first_thread_sibling(cpu);
  201. unsigned long *state = &paca_ptrs[first]->idle_state;
  202. while (unlikely(test_and_set_bit_lock(NR_PNV_CORE_IDLE_LOCK_BIT, state)))
  203. barrier();
  204. }
  205. static inline void atomic_unlock_and_stop_thread_idle(void)
  206. {
  207. int cpu = raw_smp_processor_id();
  208. int first = cpu_first_thread_sibling(cpu);
  209. unsigned long thread = 1UL << cpu_thread_in_core(cpu);
  210. unsigned long *state = &paca_ptrs[first]->idle_state;
  211. u64 s = READ_ONCE(*state);
  212. u64 new, tmp;
  213. BUG_ON(!(s & PNV_CORE_IDLE_LOCK_BIT));
  214. BUG_ON(s & thread);
  215. again:
  216. new = (s | thread) & ~PNV_CORE_IDLE_LOCK_BIT;
  217. tmp = cmpxchg(state, s, new);
  218. if (unlikely(tmp != s)) {
  219. s = tmp;
  220. goto again;
  221. }
  222. }
  223. static inline void atomic_unlock_thread_idle(void)
  224. {
  225. int cpu = raw_smp_processor_id();
  226. int first = cpu_first_thread_sibling(cpu);
  227. unsigned long *state = &paca_ptrs[first]->idle_state;
  228. BUG_ON(!test_bit(NR_PNV_CORE_IDLE_LOCK_BIT, state));
  229. clear_bit_unlock(NR_PNV_CORE_IDLE_LOCK_BIT, state);
  230. }
  231. /* P7 and P8 */
  232. struct p7_sprs {
  233. /* per core */
  234. u64 tscr;
  235. u64 worc;
  236. /* per subcore */
  237. u64 sdr1;
  238. u64 rpr;
  239. /* per thread */
  240. u64 lpcr;
  241. u64 hfscr;
  242. u64 fscr;
  243. u64 purr;
  244. u64 spurr;
  245. u64 dscr;
  246. u64 wort;
  247. /* per thread SPRs that get lost in shallow states */
  248. u64 amr;
  249. u64 iamr;
  250. u64 uamor;
  251. /* amor is restored to constant ~0 */
  252. };
  253. static unsigned long power7_idle_insn(unsigned long type)
  254. {
  255. int cpu = raw_smp_processor_id();
  256. int first = cpu_first_thread_sibling(cpu);
  257. unsigned long *state = &paca_ptrs[first]->idle_state;
  258. unsigned long thread = 1UL << cpu_thread_in_core(cpu);
  259. unsigned long core_thread_mask = (1UL << threads_per_core) - 1;
  260. unsigned long srr1;
  261. bool full_winkle;
  262. struct p7_sprs sprs = {}; /* avoid false use-uninitialised */
  263. bool sprs_saved = false;
  264. int rc;
  265. if (unlikely(type != PNV_THREAD_NAP)) {
  266. atomic_lock_thread_idle();
  267. BUG_ON(!(*state & thread));
  268. *state &= ~thread;
  269. if (power7_fastsleep_workaround_entry) {
  270. if ((*state & core_thread_mask) == 0) {
  271. rc = opal_config_cpu_idle_state(
  272. OPAL_CONFIG_IDLE_FASTSLEEP,
  273. OPAL_CONFIG_IDLE_APPLY);
  274. BUG_ON(rc);
  275. }
  276. }
  277. if (type == PNV_THREAD_WINKLE) {
  278. sprs.tscr = mfspr(SPRN_TSCR);
  279. sprs.worc = mfspr(SPRN_WORC);
  280. sprs.sdr1 = mfspr(SPRN_SDR1);
  281. sprs.rpr = mfspr(SPRN_RPR);
  282. sprs.lpcr = mfspr(SPRN_LPCR);
  283. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  284. sprs.hfscr = mfspr(SPRN_HFSCR);
  285. sprs.fscr = mfspr(SPRN_FSCR);
  286. }
  287. sprs.purr = mfspr(SPRN_PURR);
  288. sprs.spurr = mfspr(SPRN_SPURR);
  289. sprs.dscr = mfspr(SPRN_DSCR);
  290. sprs.wort = mfspr(SPRN_WORT);
  291. sprs_saved = true;
  292. /*
  293. * Increment winkle counter and set all winkle bits if
  294. * all threads are winkling. This allows wakeup side to
  295. * distinguish between fast sleep and winkle state
  296. * loss. Fast sleep still has to resync the timebase so
  297. * this may not be a really big win.
  298. */
  299. *state += 1 << PNV_CORE_IDLE_WINKLE_COUNT_SHIFT;
  300. if ((*state & PNV_CORE_IDLE_WINKLE_COUNT_BITS)
  301. >> PNV_CORE_IDLE_WINKLE_COUNT_SHIFT
  302. == threads_per_core)
  303. *state |= PNV_CORE_IDLE_THREAD_WINKLE_BITS;
  304. WARN_ON((*state & PNV_CORE_IDLE_WINKLE_COUNT_BITS) == 0);
  305. }
  306. atomic_unlock_thread_idle();
  307. }
  308. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  309. sprs.amr = mfspr(SPRN_AMR);
  310. sprs.iamr = mfspr(SPRN_IAMR);
  311. sprs.uamor = mfspr(SPRN_UAMOR);
  312. }
  313. local_paca->thread_idle_state = type;
  314. srr1 = isa206_idle_insn_mayloss(type); /* go idle */
  315. local_paca->thread_idle_state = PNV_THREAD_RUNNING;
  316. WARN_ON_ONCE(!srr1);
  317. WARN_ON_ONCE(mfmsr() & (MSR_IR|MSR_DR));
  318. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  319. if ((srr1 & SRR1_WAKESTATE) != SRR1_WS_NOLOSS) {
  320. /*
  321. * We don't need an isync after the mtsprs here because
  322. * the upcoming mtmsrd is execution synchronizing.
  323. */
  324. mtspr(SPRN_AMR, sprs.amr);
  325. mtspr(SPRN_IAMR, sprs.iamr);
  326. mtspr(SPRN_AMOR, ~0);
  327. mtspr(SPRN_UAMOR, sprs.uamor);
  328. }
  329. }
  330. if (unlikely((srr1 & SRR1_WAKEMASK_P8) == SRR1_WAKEHMI))
  331. hmi_exception_realmode(NULL);
  332. if (likely((srr1 & SRR1_WAKESTATE) != SRR1_WS_HVLOSS)) {
  333. if (unlikely(type != PNV_THREAD_NAP)) {
  334. atomic_lock_thread_idle();
  335. if (type == PNV_THREAD_WINKLE) {
  336. WARN_ON((*state & PNV_CORE_IDLE_WINKLE_COUNT_BITS) == 0);
  337. *state -= 1 << PNV_CORE_IDLE_WINKLE_COUNT_SHIFT;
  338. *state &= ~(thread << PNV_CORE_IDLE_THREAD_WINKLE_BITS_SHIFT);
  339. }
  340. atomic_unlock_and_stop_thread_idle();
  341. }
  342. return srr1;
  343. }
  344. /* HV state loss */
  345. BUG_ON(type == PNV_THREAD_NAP);
  346. atomic_lock_thread_idle();
  347. full_winkle = false;
  348. if (type == PNV_THREAD_WINKLE) {
  349. WARN_ON((*state & PNV_CORE_IDLE_WINKLE_COUNT_BITS) == 0);
  350. *state -= 1 << PNV_CORE_IDLE_WINKLE_COUNT_SHIFT;
  351. if (*state & (thread << PNV_CORE_IDLE_THREAD_WINKLE_BITS_SHIFT)) {
  352. *state &= ~(thread << PNV_CORE_IDLE_THREAD_WINKLE_BITS_SHIFT);
  353. full_winkle = true;
  354. BUG_ON(!sprs_saved);
  355. }
  356. }
  357. WARN_ON(*state & thread);
  358. if ((*state & core_thread_mask) != 0)
  359. goto core_woken;
  360. /* Per-core SPRs */
  361. if (full_winkle) {
  362. mtspr(SPRN_TSCR, sprs.tscr);
  363. mtspr(SPRN_WORC, sprs.worc);
  364. }
  365. if (power7_fastsleep_workaround_exit) {
  366. rc = opal_config_cpu_idle_state(OPAL_CONFIG_IDLE_FASTSLEEP,
  367. OPAL_CONFIG_IDLE_UNDO);
  368. BUG_ON(rc);
  369. }
  370. /* TB */
  371. if (opal_resync_timebase() != OPAL_SUCCESS)
  372. BUG();
  373. core_woken:
  374. if (!full_winkle)
  375. goto subcore_woken;
  376. if ((*state & local_paca->subcore_sibling_mask) != 0)
  377. goto subcore_woken;
  378. /* Per-subcore SPRs */
  379. mtspr(SPRN_SDR1, sprs.sdr1);
  380. mtspr(SPRN_RPR, sprs.rpr);
  381. subcore_woken:
  382. /*
  383. * isync after restoring shared SPRs and before unlocking. Unlock
  384. * only contains hwsync which does not necessarily do the right
  385. * thing for SPRs.
  386. */
  387. isync();
  388. atomic_unlock_and_stop_thread_idle();
  389. /* Fast sleep does not lose SPRs */
  390. if (!full_winkle)
  391. return srr1;
  392. /* Per-thread SPRs */
  393. mtspr(SPRN_LPCR, sprs.lpcr);
  394. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  395. mtspr(SPRN_HFSCR, sprs.hfscr);
  396. mtspr(SPRN_FSCR, sprs.fscr);
  397. }
  398. mtspr(SPRN_PURR, sprs.purr);
  399. mtspr(SPRN_SPURR, sprs.spurr);
  400. mtspr(SPRN_DSCR, sprs.dscr);
  401. mtspr(SPRN_WORT, sprs.wort);
  402. mtspr(SPRN_SPRG3, local_paca->sprg_vdso);
  403. #ifdef CONFIG_PPC_64S_HASH_MMU
  404. /*
  405. * The SLB has to be restored here, but it sometimes still
  406. * contains entries, so the __ variant must be used to prevent
  407. * multi hits.
  408. */
  409. __slb_restore_bolted_realmode();
  410. #endif
  411. return srr1;
  412. }
  413. extern unsigned long idle_kvm_start_guest(unsigned long srr1);
  414. #ifdef CONFIG_HOTPLUG_CPU
  415. static unsigned long power7_offline(void)
  416. {
  417. unsigned long srr1;
  418. mtmsr(MSR_IDLE);
  419. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  420. /* Tell KVM we're entering idle. */
  421. /******************************************************/
  422. /* N O T E W E L L ! ! ! N O T E W E L L */
  423. /* The following store to HSTATE_HWTHREAD_STATE(r13) */
  424. /* MUST occur in real mode, i.e. with the MMU off, */
  425. /* and the MMU must stay off until we clear this flag */
  426. /* and test HSTATE_HWTHREAD_REQ(r13) in */
  427. /* pnv_powersave_wakeup in this file. */
  428. /* The reason is that another thread can switch the */
  429. /* MMU to a guest context whenever this flag is set */
  430. /* to KVM_HWTHREAD_IN_IDLE, and if the MMU was on, */
  431. /* that would potentially cause this thread to start */
  432. /* executing instructions from guest memory in */
  433. /* hypervisor mode, leading to a host crash or data */
  434. /* corruption, or worse. */
  435. /******************************************************/
  436. local_paca->kvm_hstate.hwthread_state = KVM_HWTHREAD_IN_IDLE;
  437. #endif
  438. __ppc64_runlatch_off();
  439. srr1 = power7_idle_insn(power7_offline_type);
  440. __ppc64_runlatch_on();
  441. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  442. local_paca->kvm_hstate.hwthread_state = KVM_HWTHREAD_IN_KERNEL;
  443. /* Order setting hwthread_state vs. testing hwthread_req */
  444. smp_mb();
  445. if (local_paca->kvm_hstate.hwthread_req)
  446. srr1 = idle_kvm_start_guest(srr1);
  447. #endif
  448. mtmsr(MSR_KERNEL);
  449. return srr1;
  450. }
  451. #endif
  452. void power7_idle_type(unsigned long type)
  453. {
  454. unsigned long srr1;
  455. if (!prep_irq_for_idle_irqsoff())
  456. return;
  457. mtmsr(MSR_IDLE);
  458. __ppc64_runlatch_off();
  459. srr1 = power7_idle_insn(type);
  460. __ppc64_runlatch_on();
  461. mtmsr(MSR_KERNEL);
  462. fini_irq_for_idle_irqsoff();
  463. irq_set_pending_from_srr1(srr1);
  464. }
  465. static void power7_idle(void)
  466. {
  467. if (!powersave_nap)
  468. return;
  469. power7_idle_type(PNV_THREAD_NAP);
  470. }
  471. struct p9_sprs {
  472. /* per core */
  473. u64 ptcr;
  474. u64 rpr;
  475. u64 tscr;
  476. u64 ldbar;
  477. /* per thread */
  478. u64 lpcr;
  479. u64 hfscr;
  480. u64 fscr;
  481. u64 pid;
  482. u64 purr;
  483. u64 spurr;
  484. u64 dscr;
  485. u64 ciabr;
  486. u64 mmcra;
  487. u32 mmcr0;
  488. u32 mmcr1;
  489. u64 mmcr2;
  490. /* per thread SPRs that get lost in shallow states */
  491. u64 amr;
  492. u64 iamr;
  493. u64 amor;
  494. u64 uamor;
  495. };
  496. static unsigned long power9_idle_stop(unsigned long psscr)
  497. {
  498. int cpu = raw_smp_processor_id();
  499. int first = cpu_first_thread_sibling(cpu);
  500. unsigned long *state = &paca_ptrs[first]->idle_state;
  501. unsigned long core_thread_mask = (1UL << threads_per_core) - 1;
  502. unsigned long srr1;
  503. unsigned long pls;
  504. unsigned long mmcr0 = 0;
  505. unsigned long mmcra = 0;
  506. struct p9_sprs sprs = {}; /* avoid false used-uninitialised */
  507. bool sprs_saved = false;
  508. if (!(psscr & (PSSCR_EC|PSSCR_ESL))) {
  509. /* EC=ESL=0 case */
  510. /*
  511. * Wake synchronously. SRESET via xscom may still cause
  512. * a 0x100 powersave wakeup with SRR1 reason!
  513. */
  514. srr1 = isa300_idle_stop_noloss(psscr); /* go idle */
  515. if (likely(!srr1))
  516. return 0;
  517. /*
  518. * Registers not saved, can't recover!
  519. * This would be a hardware bug
  520. */
  521. BUG_ON((srr1 & SRR1_WAKESTATE) != SRR1_WS_NOLOSS);
  522. goto out;
  523. }
  524. /* EC=ESL=1 case */
  525. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  526. if (cpu_has_feature(CPU_FTR_P9_TM_XER_SO_BUG)) {
  527. local_paca->requested_psscr = psscr;
  528. /* order setting requested_psscr vs testing dont_stop */
  529. smp_mb();
  530. if (atomic_read(&local_paca->dont_stop)) {
  531. local_paca->requested_psscr = 0;
  532. return 0;
  533. }
  534. }
  535. #endif
  536. if (!cpu_has_feature(CPU_FTR_POWER9_DD2_1)) {
  537. /*
  538. * POWER9 DD2 can incorrectly set PMAO when waking up
  539. * after a state-loss idle. Saving and restoring MMCR0
  540. * over idle is a workaround.
  541. */
  542. mmcr0 = mfspr(SPRN_MMCR0);
  543. }
  544. if ((psscr & PSSCR_RL_MASK) >= deep_spr_loss_state) {
  545. sprs.lpcr = mfspr(SPRN_LPCR);
  546. sprs.hfscr = mfspr(SPRN_HFSCR);
  547. sprs.fscr = mfspr(SPRN_FSCR);
  548. sprs.pid = mfspr(SPRN_PID);
  549. sprs.purr = mfspr(SPRN_PURR);
  550. sprs.spurr = mfspr(SPRN_SPURR);
  551. sprs.dscr = mfspr(SPRN_DSCR);
  552. sprs.ciabr = mfspr(SPRN_CIABR);
  553. sprs.mmcra = mfspr(SPRN_MMCRA);
  554. sprs.mmcr0 = mfspr(SPRN_MMCR0);
  555. sprs.mmcr1 = mfspr(SPRN_MMCR1);
  556. sprs.mmcr2 = mfspr(SPRN_MMCR2);
  557. sprs.ptcr = mfspr(SPRN_PTCR);
  558. sprs.rpr = mfspr(SPRN_RPR);
  559. sprs.tscr = mfspr(SPRN_TSCR);
  560. if (!firmware_has_feature(FW_FEATURE_ULTRAVISOR))
  561. sprs.ldbar = mfspr(SPRN_LDBAR);
  562. sprs_saved = true;
  563. atomic_start_thread_idle();
  564. }
  565. sprs.amr = mfspr(SPRN_AMR);
  566. sprs.iamr = mfspr(SPRN_IAMR);
  567. sprs.uamor = mfspr(SPRN_UAMOR);
  568. srr1 = isa300_idle_stop_mayloss(psscr); /* go idle */
  569. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  570. local_paca->requested_psscr = 0;
  571. #endif
  572. psscr = mfspr(SPRN_PSSCR);
  573. WARN_ON_ONCE(!srr1);
  574. WARN_ON_ONCE(mfmsr() & (MSR_IR|MSR_DR));
  575. if ((srr1 & SRR1_WAKESTATE) != SRR1_WS_NOLOSS) {
  576. /*
  577. * We don't need an isync after the mtsprs here because the
  578. * upcoming mtmsrd is execution synchronizing.
  579. */
  580. mtspr(SPRN_AMR, sprs.amr);
  581. mtspr(SPRN_IAMR, sprs.iamr);
  582. mtspr(SPRN_AMOR, ~0);
  583. mtspr(SPRN_UAMOR, sprs.uamor);
  584. /*
  585. * Workaround for POWER9 DD2.0, if we lost resources, the ERAT
  586. * might have been corrupted and needs flushing. We also need
  587. * to reload MMCR0 (see mmcr0 comment above).
  588. */
  589. if (!cpu_has_feature(CPU_FTR_POWER9_DD2_1)) {
  590. asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT);
  591. mtspr(SPRN_MMCR0, mmcr0);
  592. }
  593. /*
  594. * DD2.2 and earlier need to set then clear bit 60 in MMCRA
  595. * to ensure the PMU starts running.
  596. */
  597. mmcra = mfspr(SPRN_MMCRA);
  598. mmcra |= PPC_BIT(60);
  599. mtspr(SPRN_MMCRA, mmcra);
  600. mmcra &= ~PPC_BIT(60);
  601. mtspr(SPRN_MMCRA, mmcra);
  602. }
  603. if (unlikely((srr1 & SRR1_WAKEMASK_P8) == SRR1_WAKEHMI))
  604. hmi_exception_realmode(NULL);
  605. /*
  606. * On POWER9, SRR1 bits do not match exactly as expected.
  607. * SRR1_WS_GPRLOSS (10b) can also result in SPR loss, so
  608. * just always test PSSCR for SPR/TB state loss.
  609. */
  610. pls = (psscr & PSSCR_PLS) >> PSSCR_PLS_SHIFT;
  611. if (likely(pls < deep_spr_loss_state)) {
  612. if (sprs_saved)
  613. atomic_stop_thread_idle();
  614. goto out;
  615. }
  616. /* HV state loss */
  617. BUG_ON(!sprs_saved);
  618. atomic_lock_thread_idle();
  619. if ((*state & core_thread_mask) != 0)
  620. goto core_woken;
  621. /* Per-core SPRs */
  622. mtspr(SPRN_PTCR, sprs.ptcr);
  623. mtspr(SPRN_RPR, sprs.rpr);
  624. mtspr(SPRN_TSCR, sprs.tscr);
  625. if (pls >= pnv_first_tb_loss_level) {
  626. /* TB loss */
  627. if (opal_resync_timebase() != OPAL_SUCCESS)
  628. BUG();
  629. }
  630. /*
  631. * isync after restoring shared SPRs and before unlocking. Unlock
  632. * only contains hwsync which does not necessarily do the right
  633. * thing for SPRs.
  634. */
  635. isync();
  636. core_woken:
  637. atomic_unlock_and_stop_thread_idle();
  638. /* Per-thread SPRs */
  639. mtspr(SPRN_LPCR, sprs.lpcr);
  640. mtspr(SPRN_HFSCR, sprs.hfscr);
  641. mtspr(SPRN_FSCR, sprs.fscr);
  642. mtspr(SPRN_PID, sprs.pid);
  643. mtspr(SPRN_PURR, sprs.purr);
  644. mtspr(SPRN_SPURR, sprs.spurr);
  645. mtspr(SPRN_DSCR, sprs.dscr);
  646. mtspr(SPRN_CIABR, sprs.ciabr);
  647. mtspr(SPRN_MMCRA, sprs.mmcra);
  648. mtspr(SPRN_MMCR0, sprs.mmcr0);
  649. mtspr(SPRN_MMCR1, sprs.mmcr1);
  650. mtspr(SPRN_MMCR2, sprs.mmcr2);
  651. if (!firmware_has_feature(FW_FEATURE_ULTRAVISOR))
  652. mtspr(SPRN_LDBAR, sprs.ldbar);
  653. mtspr(SPRN_SPRG3, local_paca->sprg_vdso);
  654. if (!radix_enabled())
  655. __slb_restore_bolted_realmode();
  656. out:
  657. mtmsr(MSR_KERNEL);
  658. return srr1;
  659. }
  660. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  661. /*
  662. * This is used in working around bugs in thread reconfiguration
  663. * on POWER9 (at least up to Nimbus DD2.2) relating to transactional
  664. * memory and the way that XER[SO] is checkpointed.
  665. * This function forces the core into SMT4 in order by asking
  666. * all other threads not to stop, and sending a message to any
  667. * that are in a stop state.
  668. * Must be called with preemption disabled.
  669. */
  670. void pnv_power9_force_smt4_catch(void)
  671. {
  672. int cpu, cpu0, thr;
  673. int awake_threads = 1; /* this thread is awake */
  674. int poke_threads = 0;
  675. int need_awake = threads_per_core;
  676. cpu = smp_processor_id();
  677. cpu0 = cpu & ~(threads_per_core - 1);
  678. for (thr = 0; thr < threads_per_core; ++thr) {
  679. if (cpu != cpu0 + thr)
  680. atomic_inc(&paca_ptrs[cpu0+thr]->dont_stop);
  681. }
  682. /* order setting dont_stop vs testing requested_psscr */
  683. smp_mb();
  684. for (thr = 0; thr < threads_per_core; ++thr) {
  685. if (!paca_ptrs[cpu0+thr]->requested_psscr)
  686. ++awake_threads;
  687. else
  688. poke_threads |= (1 << thr);
  689. }
  690. /* If at least 3 threads are awake, the core is in SMT4 already */
  691. if (awake_threads < need_awake) {
  692. /* We have to wake some threads; we'll use msgsnd */
  693. for (thr = 0; thr < threads_per_core; ++thr) {
  694. if (poke_threads & (1 << thr)) {
  695. ppc_msgsnd_sync();
  696. ppc_msgsnd(PPC_DBELL_MSGTYPE, 0,
  697. paca_ptrs[cpu0+thr]->hw_cpu_id);
  698. }
  699. }
  700. /* now spin until at least 3 threads are awake */
  701. do {
  702. for (thr = 0; thr < threads_per_core; ++thr) {
  703. if ((poke_threads & (1 << thr)) &&
  704. !paca_ptrs[cpu0+thr]->requested_psscr) {
  705. ++awake_threads;
  706. poke_threads &= ~(1 << thr);
  707. }
  708. }
  709. } while (awake_threads < need_awake);
  710. }
  711. }
  712. EXPORT_SYMBOL_GPL(pnv_power9_force_smt4_catch);
  713. void pnv_power9_force_smt4_release(void)
  714. {
  715. int cpu, cpu0, thr;
  716. cpu = smp_processor_id();
  717. cpu0 = cpu & ~(threads_per_core - 1);
  718. /* clear all the dont_stop flags */
  719. for (thr = 0; thr < threads_per_core; ++thr) {
  720. if (cpu != cpu0 + thr)
  721. atomic_dec(&paca_ptrs[cpu0+thr]->dont_stop);
  722. }
  723. }
  724. EXPORT_SYMBOL_GPL(pnv_power9_force_smt4_release);
  725. #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
  726. struct p10_sprs {
  727. /*
  728. * SPRs that get lost in shallow states:
  729. *
  730. * P10 loses CR, LR, CTR, FPSCR, VSCR, XER, TAR, SPRG2, and HSPRG1
  731. * isa300 idle routines restore CR, LR.
  732. * CTR is volatile
  733. * idle thread doesn't use FP or VEC
  734. * kernel doesn't use TAR
  735. * HSPRG1 is only live in HV interrupt entry
  736. * SPRG2 is only live in KVM guests, KVM handles it.
  737. */
  738. };
  739. static unsigned long power10_idle_stop(unsigned long psscr)
  740. {
  741. int cpu = raw_smp_processor_id();
  742. int first = cpu_first_thread_sibling(cpu);
  743. unsigned long *state = &paca_ptrs[first]->idle_state;
  744. unsigned long core_thread_mask = (1UL << threads_per_core) - 1;
  745. unsigned long srr1;
  746. unsigned long pls;
  747. // struct p10_sprs sprs = {}; /* avoid false used-uninitialised */
  748. bool sprs_saved = false;
  749. if (!(psscr & (PSSCR_EC|PSSCR_ESL))) {
  750. /* EC=ESL=0 case */
  751. /*
  752. * Wake synchronously. SRESET via xscom may still cause
  753. * a 0x100 powersave wakeup with SRR1 reason!
  754. */
  755. srr1 = isa300_idle_stop_noloss(psscr); /* go idle */
  756. if (likely(!srr1))
  757. return 0;
  758. /*
  759. * Registers not saved, can't recover!
  760. * This would be a hardware bug
  761. */
  762. BUG_ON((srr1 & SRR1_WAKESTATE) != SRR1_WS_NOLOSS);
  763. goto out;
  764. }
  765. /* EC=ESL=1 case */
  766. if ((psscr & PSSCR_RL_MASK) >= deep_spr_loss_state) {
  767. /* XXX: save SPRs for deep state loss here. */
  768. sprs_saved = true;
  769. atomic_start_thread_idle();
  770. }
  771. srr1 = isa300_idle_stop_mayloss(psscr); /* go idle */
  772. psscr = mfspr(SPRN_PSSCR);
  773. WARN_ON_ONCE(!srr1);
  774. WARN_ON_ONCE(mfmsr() & (MSR_IR|MSR_DR));
  775. if (unlikely((srr1 & SRR1_WAKEMASK_P8) == SRR1_WAKEHMI))
  776. hmi_exception_realmode(NULL);
  777. /*
  778. * On POWER10, SRR1 bits do not match exactly as expected.
  779. * SRR1_WS_GPRLOSS (10b) can also result in SPR loss, so
  780. * just always test PSSCR for SPR/TB state loss.
  781. */
  782. pls = (psscr & PSSCR_PLS) >> PSSCR_PLS_SHIFT;
  783. if (likely(pls < deep_spr_loss_state)) {
  784. if (sprs_saved)
  785. atomic_stop_thread_idle();
  786. goto out;
  787. }
  788. /* HV state loss */
  789. BUG_ON(!sprs_saved);
  790. atomic_lock_thread_idle();
  791. if ((*state & core_thread_mask) != 0)
  792. goto core_woken;
  793. /* XXX: restore per-core SPRs here */
  794. if (pls >= pnv_first_tb_loss_level) {
  795. /* TB loss */
  796. if (opal_resync_timebase() != OPAL_SUCCESS)
  797. BUG();
  798. }
  799. /*
  800. * isync after restoring shared SPRs and before unlocking. Unlock
  801. * only contains hwsync which does not necessarily do the right
  802. * thing for SPRs.
  803. */
  804. isync();
  805. core_woken:
  806. atomic_unlock_and_stop_thread_idle();
  807. /* XXX: restore per-thread SPRs here */
  808. if (!radix_enabled())
  809. __slb_restore_bolted_realmode();
  810. out:
  811. mtmsr(MSR_KERNEL);
  812. return srr1;
  813. }
  814. #ifdef CONFIG_HOTPLUG_CPU
  815. static unsigned long arch300_offline_stop(unsigned long psscr)
  816. {
  817. unsigned long srr1;
  818. if (cpu_has_feature(CPU_FTR_ARCH_31))
  819. srr1 = power10_idle_stop(psscr);
  820. else
  821. srr1 = power9_idle_stop(psscr);
  822. return srr1;
  823. }
  824. #endif
  825. void arch300_idle_type(unsigned long stop_psscr_val,
  826. unsigned long stop_psscr_mask)
  827. {
  828. unsigned long psscr;
  829. unsigned long srr1;
  830. if (!prep_irq_for_idle_irqsoff())
  831. return;
  832. psscr = mfspr(SPRN_PSSCR);
  833. psscr = (psscr & ~stop_psscr_mask) | stop_psscr_val;
  834. __ppc64_runlatch_off();
  835. if (cpu_has_feature(CPU_FTR_ARCH_31))
  836. srr1 = power10_idle_stop(psscr);
  837. else
  838. srr1 = power9_idle_stop(psscr);
  839. __ppc64_runlatch_on();
  840. fini_irq_for_idle_irqsoff();
  841. irq_set_pending_from_srr1(srr1);
  842. }
  843. /*
  844. * Used for ppc_md.power_save which needs a function with no parameters
  845. */
  846. static void arch300_idle(void)
  847. {
  848. arch300_idle_type(pnv_default_stop_val, pnv_default_stop_mask);
  849. }
  850. #ifdef CONFIG_HOTPLUG_CPU
  851. void pnv_program_cpu_hotplug_lpcr(unsigned int cpu, u64 lpcr_val)
  852. {
  853. u64 pir = get_hard_smp_processor_id(cpu);
  854. mtspr(SPRN_LPCR, lpcr_val);
  855. /*
  856. * Program the LPCR via stop-api only if the deepest stop state
  857. * can lose hypervisor context.
  858. */
  859. if (supported_cpuidle_states & OPAL_PM_LOSE_FULL_CONTEXT)
  860. opal_slw_set_reg(pir, SPRN_LPCR, lpcr_val);
  861. }
  862. /*
  863. * pnv_cpu_offline: A function that puts the CPU into the deepest
  864. * available platform idle state on a CPU-Offline.
  865. * interrupts hard disabled and no lazy irq pending.
  866. */
  867. unsigned long pnv_cpu_offline(unsigned int cpu)
  868. {
  869. unsigned long srr1;
  870. __ppc64_runlatch_off();
  871. if (cpu_has_feature(CPU_FTR_ARCH_300) && deepest_stop_found) {
  872. unsigned long psscr;
  873. psscr = mfspr(SPRN_PSSCR);
  874. psscr = (psscr & ~pnv_deepest_stop_psscr_mask) |
  875. pnv_deepest_stop_psscr_val;
  876. srr1 = arch300_offline_stop(psscr);
  877. } else if (cpu_has_feature(CPU_FTR_ARCH_206) && power7_offline_type) {
  878. srr1 = power7_offline();
  879. } else {
  880. /* This is the fallback method. We emulate snooze */
  881. while (!generic_check_cpu_restart(cpu)) {
  882. HMT_low();
  883. HMT_very_low();
  884. }
  885. srr1 = 0;
  886. HMT_medium();
  887. }
  888. __ppc64_runlatch_on();
  889. return srr1;
  890. }
  891. #endif
  892. /*
  893. * Power ISA 3.0 idle initialization.
  894. *
  895. * POWER ISA 3.0 defines a new SPR Processor stop Status and Control
  896. * Register (PSSCR) to control idle behavior.
  897. *
  898. * PSSCR layout:
  899. * ----------------------------------------------------------
  900. * | PLS | /// | SD | ESL | EC | PSLL | /// | TR | MTL | RL |
  901. * ----------------------------------------------------------
  902. * 0 4 41 42 43 44 48 54 56 60
  903. *
  904. * PSSCR key fields:
  905. * Bits 0:3 - Power-Saving Level Status (PLS). This field indicates the
  906. * lowest power-saving state the thread entered since stop instruction was
  907. * last executed.
  908. *
  909. * Bit 41 - Status Disable(SD)
  910. * 0 - Shows PLS entries
  911. * 1 - PLS entries are all 0
  912. *
  913. * Bit 42 - Enable State Loss
  914. * 0 - No state is lost irrespective of other fields
  915. * 1 - Allows state loss
  916. *
  917. * Bit 43 - Exit Criterion
  918. * 0 - Exit from power-save mode on any interrupt
  919. * 1 - Exit from power-save mode controlled by LPCR's PECE bits
  920. *
  921. * Bits 44:47 - Power-Saving Level Limit
  922. * This limits the power-saving level that can be entered into.
  923. *
  924. * Bits 60:63 - Requested Level
  925. * Used to specify which power-saving level must be entered on executing
  926. * stop instruction
  927. */
  928. int __init validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags)
  929. {
  930. int err = 0;
  931. /*
  932. * psscr_mask == 0xf indicates an older firmware.
  933. * Set remaining fields of psscr to the default values.
  934. * See NOTE above definition of PSSCR_HV_DEFAULT_VAL
  935. */
  936. if (*psscr_mask == 0xf) {
  937. *psscr_val = *psscr_val | PSSCR_HV_DEFAULT_VAL;
  938. *psscr_mask = PSSCR_HV_DEFAULT_MASK;
  939. return err;
  940. }
  941. /*
  942. * New firmware is expected to set the psscr_val bits correctly.
  943. * Validate that the following invariants are correctly maintained by
  944. * the new firmware.
  945. * - ESL bit value matches the EC bit value.
  946. * - ESL bit is set for all the deep stop states.
  947. */
  948. if (GET_PSSCR_ESL(*psscr_val) != GET_PSSCR_EC(*psscr_val)) {
  949. err = ERR_EC_ESL_MISMATCH;
  950. } else if ((flags & OPAL_PM_LOSE_FULL_CONTEXT) &&
  951. GET_PSSCR_ESL(*psscr_val) == 0) {
  952. err = ERR_DEEP_STATE_ESL_MISMATCH;
  953. }
  954. return err;
  955. }
  956. /*
  957. * pnv_arch300_idle_init: Initializes the default idle state, first
  958. * deep idle state and deepest idle state on
  959. * ISA 3.0 CPUs.
  960. *
  961. * @np: /ibm,opal/power-mgt device node
  962. * @flags: cpu-idle-state-flags array
  963. * @dt_idle_states: Number of idle state entries
  964. * Returns 0 on success
  965. */
  966. static void __init pnv_arch300_idle_init(void)
  967. {
  968. u64 max_residency_ns = 0;
  969. int i;
  970. /* stop is not really architected, we only have p9,p10 drivers */
  971. if (!pvr_version_is(PVR_POWER10) && !pvr_version_is(PVR_POWER9))
  972. return;
  973. /*
  974. * pnv_deepest_stop_{val,mask} should be set to values corresponding to
  975. * the deepest stop state.
  976. *
  977. * pnv_default_stop_{val,mask} should be set to values corresponding to
  978. * the deepest loss-less (OPAL_PM_STOP_INST_FAST) stop state.
  979. */
  980. pnv_first_tb_loss_level = MAX_STOP_STATE + 1;
  981. deep_spr_loss_state = MAX_STOP_STATE + 1;
  982. for (i = 0; i < nr_pnv_idle_states; i++) {
  983. int err;
  984. struct pnv_idle_states_t *state = &pnv_idle_states[i];
  985. u64 psscr_rl = state->psscr_val & PSSCR_RL_MASK;
  986. /* No deep loss driver implemented for POWER10 yet */
  987. if (pvr_version_is(PVR_POWER10) &&
  988. state->flags & (OPAL_PM_TIMEBASE_STOP|OPAL_PM_LOSE_FULL_CONTEXT))
  989. continue;
  990. if ((state->flags & OPAL_PM_TIMEBASE_STOP) &&
  991. (pnv_first_tb_loss_level > psscr_rl))
  992. pnv_first_tb_loss_level = psscr_rl;
  993. if ((state->flags & OPAL_PM_LOSE_FULL_CONTEXT) &&
  994. (deep_spr_loss_state > psscr_rl))
  995. deep_spr_loss_state = psscr_rl;
  996. /*
  997. * The idle code does not deal with TB loss occurring
  998. * in a shallower state than SPR loss, so force it to
  999. * behave like SPRs are lost if TB is lost. POWER9 would
  1000. * never encounter this, but a POWER8 core would if it
  1001. * implemented the stop instruction. So this is for forward
  1002. * compatibility.
  1003. */
  1004. if ((state->flags & OPAL_PM_TIMEBASE_STOP) &&
  1005. (deep_spr_loss_state > psscr_rl))
  1006. deep_spr_loss_state = psscr_rl;
  1007. err = validate_psscr_val_mask(&state->psscr_val,
  1008. &state->psscr_mask,
  1009. state->flags);
  1010. if (err) {
  1011. report_invalid_psscr_val(state->psscr_val, err);
  1012. continue;
  1013. }
  1014. state->valid = true;
  1015. if (max_residency_ns < state->residency_ns) {
  1016. max_residency_ns = state->residency_ns;
  1017. pnv_deepest_stop_psscr_val = state->psscr_val;
  1018. pnv_deepest_stop_psscr_mask = state->psscr_mask;
  1019. pnv_deepest_stop_flag = state->flags;
  1020. deepest_stop_found = true;
  1021. }
  1022. if (!default_stop_found &&
  1023. (state->flags & OPAL_PM_STOP_INST_FAST)) {
  1024. pnv_default_stop_val = state->psscr_val;
  1025. pnv_default_stop_mask = state->psscr_mask;
  1026. default_stop_found = true;
  1027. WARN_ON(state->flags & OPAL_PM_LOSE_FULL_CONTEXT);
  1028. }
  1029. }
  1030. if (unlikely(!default_stop_found)) {
  1031. pr_warn("cpuidle-powernv: No suitable default stop state found. Disabling platform idle.\n");
  1032. } else {
  1033. ppc_md.power_save = arch300_idle;
  1034. pr_info("cpuidle-powernv: Default stop: psscr = 0x%016llx,mask=0x%016llx\n",
  1035. pnv_default_stop_val, pnv_default_stop_mask);
  1036. }
  1037. if (unlikely(!deepest_stop_found)) {
  1038. pr_warn("cpuidle-powernv: No suitable stop state for CPU-Hotplug. Offlined CPUs will busy wait");
  1039. } else {
  1040. pr_info("cpuidle-powernv: Deepest stop: psscr = 0x%016llx,mask=0x%016llx\n",
  1041. pnv_deepest_stop_psscr_val,
  1042. pnv_deepest_stop_psscr_mask);
  1043. }
  1044. pr_info("cpuidle-powernv: First stop level that may lose SPRs = 0x%llx\n",
  1045. deep_spr_loss_state);
  1046. pr_info("cpuidle-powernv: First stop level that may lose timebase = 0x%llx\n",
  1047. pnv_first_tb_loss_level);
  1048. }
  1049. static void __init pnv_disable_deep_states(void)
  1050. {
  1051. /*
  1052. * The stop-api is unable to restore hypervisor
  1053. * resources on wakeup from platform idle states which
  1054. * lose full context. So disable such states.
  1055. */
  1056. supported_cpuidle_states &= ~OPAL_PM_LOSE_FULL_CONTEXT;
  1057. pr_warn("cpuidle-powernv: Disabling idle states that lose full context\n");
  1058. pr_warn("cpuidle-powernv: Idle power-savings, CPU-Hotplug affected\n");
  1059. if (cpu_has_feature(CPU_FTR_ARCH_300) &&
  1060. (pnv_deepest_stop_flag & OPAL_PM_LOSE_FULL_CONTEXT)) {
  1061. /*
  1062. * Use the default stop state for CPU-Hotplug
  1063. * if available.
  1064. */
  1065. if (default_stop_found) {
  1066. pnv_deepest_stop_psscr_val = pnv_default_stop_val;
  1067. pnv_deepest_stop_psscr_mask = pnv_default_stop_mask;
  1068. pr_warn("cpuidle-powernv: Offlined CPUs will stop with psscr = 0x%016llx\n",
  1069. pnv_deepest_stop_psscr_val);
  1070. } else { /* Fallback to snooze loop for CPU-Hotplug */
  1071. deepest_stop_found = false;
  1072. pr_warn("cpuidle-powernv: Offlined CPUs will busy wait\n");
  1073. }
  1074. }
  1075. }
  1076. /*
  1077. * Probe device tree for supported idle states
  1078. */
  1079. static void __init pnv_probe_idle_states(void)
  1080. {
  1081. int i;
  1082. if (nr_pnv_idle_states < 0) {
  1083. pr_warn("cpuidle-powernv: no idle states found in the DT\n");
  1084. return;
  1085. }
  1086. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1087. pnv_arch300_idle_init();
  1088. for (i = 0; i < nr_pnv_idle_states; i++)
  1089. supported_cpuidle_states |= pnv_idle_states[i].flags;
  1090. }
  1091. /*
  1092. * This function parses device-tree and populates all the information
  1093. * into pnv_idle_states structure. It also sets up nr_pnv_idle_states
  1094. * which is the number of cpuidle states discovered through device-tree.
  1095. */
  1096. static int __init pnv_parse_cpuidle_dt(void)
  1097. {
  1098. struct device_node *np;
  1099. int nr_idle_states, i;
  1100. int rc = 0;
  1101. u32 *temp_u32;
  1102. u64 *temp_u64;
  1103. const char **temp_string;
  1104. np = of_find_node_by_path("/ibm,opal/power-mgt");
  1105. if (!np) {
  1106. pr_warn("opal: PowerMgmt Node not found\n");
  1107. return -ENODEV;
  1108. }
  1109. nr_idle_states = of_property_count_u32_elems(np,
  1110. "ibm,cpu-idle-state-flags");
  1111. pnv_idle_states = kcalloc(nr_idle_states, sizeof(*pnv_idle_states),
  1112. GFP_KERNEL);
  1113. temp_u32 = kcalloc(nr_idle_states, sizeof(u32), GFP_KERNEL);
  1114. temp_u64 = kcalloc(nr_idle_states, sizeof(u64), GFP_KERNEL);
  1115. temp_string = kcalloc(nr_idle_states, sizeof(char *), GFP_KERNEL);
  1116. if (!(pnv_idle_states && temp_u32 && temp_u64 && temp_string)) {
  1117. pr_err("Could not allocate memory for dt parsing\n");
  1118. rc = -ENOMEM;
  1119. goto out;
  1120. }
  1121. /* Read flags */
  1122. if (of_property_read_u32_array(np, "ibm,cpu-idle-state-flags",
  1123. temp_u32, nr_idle_states)) {
  1124. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-flags in DT\n");
  1125. rc = -EINVAL;
  1126. goto out;
  1127. }
  1128. for (i = 0; i < nr_idle_states; i++)
  1129. pnv_idle_states[i].flags = temp_u32[i];
  1130. /* Read latencies */
  1131. if (of_property_read_u32_array(np, "ibm,cpu-idle-state-latencies-ns",
  1132. temp_u32, nr_idle_states)) {
  1133. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
  1134. rc = -EINVAL;
  1135. goto out;
  1136. }
  1137. for (i = 0; i < nr_idle_states; i++)
  1138. pnv_idle_states[i].latency_ns = temp_u32[i];
  1139. /* Read residencies */
  1140. if (of_property_read_u32_array(np, "ibm,cpu-idle-state-residency-ns",
  1141. temp_u32, nr_idle_states)) {
  1142. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-residency-ns in DT\n");
  1143. rc = -EINVAL;
  1144. goto out;
  1145. }
  1146. for (i = 0; i < nr_idle_states; i++)
  1147. pnv_idle_states[i].residency_ns = temp_u32[i];
  1148. /* For power9 and later */
  1149. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1150. /* Read pm_crtl_val */
  1151. if (of_property_read_u64_array(np, "ibm,cpu-idle-state-psscr",
  1152. temp_u64, nr_idle_states)) {
  1153. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
  1154. rc = -EINVAL;
  1155. goto out;
  1156. }
  1157. for (i = 0; i < nr_idle_states; i++)
  1158. pnv_idle_states[i].psscr_val = temp_u64[i];
  1159. /* Read pm_crtl_mask */
  1160. if (of_property_read_u64_array(np, "ibm,cpu-idle-state-psscr-mask",
  1161. temp_u64, nr_idle_states)) {
  1162. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr-mask in DT\n");
  1163. rc = -EINVAL;
  1164. goto out;
  1165. }
  1166. for (i = 0; i < nr_idle_states; i++)
  1167. pnv_idle_states[i].psscr_mask = temp_u64[i];
  1168. }
  1169. /*
  1170. * power8 specific properties ibm,cpu-idle-state-pmicr-mask and
  1171. * ibm,cpu-idle-state-pmicr-val were never used and there is no
  1172. * plan to use it in near future. Hence, not parsing these properties
  1173. */
  1174. if (of_property_read_string_array(np, "ibm,cpu-idle-state-names",
  1175. temp_string, nr_idle_states) < 0) {
  1176. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
  1177. rc = -EINVAL;
  1178. goto out;
  1179. }
  1180. for (i = 0; i < nr_idle_states; i++)
  1181. strscpy(pnv_idle_states[i].name, temp_string[i],
  1182. PNV_IDLE_NAME_LEN);
  1183. nr_pnv_idle_states = nr_idle_states;
  1184. rc = 0;
  1185. out:
  1186. kfree(temp_u32);
  1187. kfree(temp_u64);
  1188. kfree(temp_string);
  1189. of_node_put(np);
  1190. return rc;
  1191. }
  1192. static int __init pnv_init_idle_states(void)
  1193. {
  1194. int cpu;
  1195. int rc = 0;
  1196. /* Set up PACA fields */
  1197. for_each_present_cpu(cpu) {
  1198. struct paca_struct *p = paca_ptrs[cpu];
  1199. p->idle_state = 0;
  1200. if (cpu == cpu_first_thread_sibling(cpu))
  1201. p->idle_state = (1 << threads_per_core) - 1;
  1202. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  1203. /* P7/P8 nap */
  1204. p->thread_idle_state = PNV_THREAD_RUNNING;
  1205. } else if (pvr_version_is(PVR_POWER9)) {
  1206. /* P9 stop workarounds */
  1207. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  1208. p->requested_psscr = 0;
  1209. atomic_set(&p->dont_stop, 0);
  1210. #endif
  1211. }
  1212. }
  1213. /* In case we error out nr_pnv_idle_states will be zero */
  1214. nr_pnv_idle_states = 0;
  1215. supported_cpuidle_states = 0;
  1216. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  1217. goto out;
  1218. rc = pnv_parse_cpuidle_dt();
  1219. if (rc)
  1220. return rc;
  1221. pnv_probe_idle_states();
  1222. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  1223. if (!(supported_cpuidle_states & OPAL_PM_SLEEP_ENABLED_ER1)) {
  1224. power7_fastsleep_workaround_entry = false;
  1225. power7_fastsleep_workaround_exit = false;
  1226. } else {
  1227. /*
  1228. * OPAL_PM_SLEEP_ENABLED_ER1 is set. It indicates that
  1229. * workaround is needed to use fastsleep. Provide sysfs
  1230. * control to choose how this workaround has to be
  1231. * applied.
  1232. */
  1233. device_create_file(cpu_subsys.dev_root,
  1234. &dev_attr_fastsleep_workaround_applyonce);
  1235. }
  1236. update_subcore_sibling_mask();
  1237. if (supported_cpuidle_states & OPAL_PM_NAP_ENABLED) {
  1238. ppc_md.power_save = power7_idle;
  1239. power7_offline_type = PNV_THREAD_NAP;
  1240. }
  1241. if ((supported_cpuidle_states & OPAL_PM_WINKLE_ENABLED) &&
  1242. (supported_cpuidle_states & OPAL_PM_LOSE_FULL_CONTEXT))
  1243. power7_offline_type = PNV_THREAD_WINKLE;
  1244. else if ((supported_cpuidle_states & OPAL_PM_SLEEP_ENABLED) ||
  1245. (supported_cpuidle_states & OPAL_PM_SLEEP_ENABLED_ER1))
  1246. power7_offline_type = PNV_THREAD_SLEEP;
  1247. }
  1248. if (supported_cpuidle_states & OPAL_PM_LOSE_FULL_CONTEXT) {
  1249. if (pnv_save_sprs_for_deep_states())
  1250. pnv_disable_deep_states();
  1251. }
  1252. out:
  1253. return 0;
  1254. }
  1255. machine_subsys_initcall(powernv, pnv_init_idle_states);