idle.c 38 KB

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