Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/powernv/idle: add a basic stop 0-3 driver for POWER10

This driver does not restore stop > 3 state, so it limits itself
to states which do not lose full state or TB.

The POWER10 SPRs are sufficiently different from P9 that it seems
easier to split out the P10 code. The POWER10 deep sleep code
(e.g., the BHRB restore) has been taken out, but it can be re-added
when stop > 3 support is added.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Tested-by: Pratik Rajesh Sampat<psampat@linux.ibm.com>
Tested-by: Vaidyanathan Srinivasan <svaidy@linux.ibm.com>
Reviewed-by: Pratik Rajesh Sampat<psampat@linux.ibm.com>
Reviewed-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20200819094700.493399-1-npiggin@gmail.com

authored by

Nicholas Piggin and committed by
Michael Ellerman
ffd2961b 79b123cd

+212 -97
-2
arch/powerpc/include/asm/machdep.h
··· 222 222 223 223 extern void e500_idle(void); 224 224 extern void power4_idle(void); 225 - extern void power7_idle(void); 226 - extern void power9_idle(void); 227 225 extern void ppc6xx_idle(void); 228 226 extern void book3e_idle(void); 229 227
+1 -1
arch/powerpc/include/asm/processor.h
··· 432 432 extern int powersave_nap; /* set if nap mode can be used in idle loop */ 433 433 434 434 extern void power7_idle_type(unsigned long type); 435 - extern void power9_idle_type(unsigned long stop_psscr_val, 435 + extern void arch300_idle_type(unsigned long stop_psscr_val, 436 436 unsigned long stop_psscr_mask); 437 437 438 438 extern int fix_alignment(struct pt_regs *);
+1
arch/powerpc/include/asm/reg.h
··· 1353 1353 #define PVR_POWER8NVL 0x004C 1354 1354 #define PVR_POWER8 0x004D 1355 1355 #define PVR_POWER9 0x004E 1356 + #define PVR_POWER10 0x0080 1356 1357 #define PVR_BE 0x0070 1357 1358 #define PVR_PA6T 0x0090 1358 1359
+209 -93
arch/powerpc/platforms/powernv/idle.c
··· 565 565 irq_set_pending_from_srr1(srr1); 566 566 } 567 567 568 - void power7_idle(void) 568 + static void power7_idle(void) 569 569 { 570 570 if (!powersave_nap) 571 571 return; ··· 659 659 mmcr0 = mfspr(SPRN_MMCR0); 660 660 } 661 661 662 - if (cpu_has_feature(CPU_FTR_ARCH_31)) { 663 - /* 664 - * POWER10 uses MMCRA (BHRBRD) as BHRB disable bit. 665 - * If the user hasn't asked for the BHRB to be 666 - * written, the value of MMCRA[BHRBRD] is 1. 667 - * On wakeup from stop, MMCRA[BHRBD] will be 0, 668 - * since it is previleged resource and will be lost. 669 - * Thus, if we do not save and restore the MMCRA[BHRBD], 670 - * hardware will be needlessly writing to the BHRB 671 - * in problem mode. 672 - */ 673 - mmcra = mfspr(SPRN_MMCRA); 674 - } 675 - 676 662 if ((psscr & PSSCR_RL_MASK) >= deep_spr_loss_state) { 677 663 sprs.lpcr = mfspr(SPRN_LPCR); 678 664 sprs.hfscr = mfspr(SPRN_HFSCR); ··· 720 734 asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT); 721 735 mtspr(SPRN_MMCR0, mmcr0); 722 736 } 723 - 724 - /* Reload MMCRA to restore BHRB disable bit for POWER10 */ 725 - if (cpu_has_feature(CPU_FTR_ARCH_31)) 726 - mtspr(SPRN_MMCRA, mmcra); 727 737 728 738 /* 729 739 * DD2.2 and earlier need to set then clear bit 60 in MMCRA ··· 805 823 return srr1; 806 824 } 807 825 808 - #ifdef CONFIG_HOTPLUG_CPU 809 - static unsigned long power9_offline_stop(unsigned long psscr) 810 - { 811 - unsigned long srr1; 812 - 813 - #ifndef CONFIG_KVM_BOOK3S_HV_POSSIBLE 814 - __ppc64_runlatch_off(); 815 - srr1 = power9_idle_stop(psscr, true); 816 - __ppc64_runlatch_on(); 817 - #else 818 - /* 819 - * Tell KVM we're entering idle. 820 - * This does not have to be done in real mode because the P9 MMU 821 - * is independent per-thread. Some steppings share radix/hash mode 822 - * between threads, but in that case KVM has a barrier sync in real 823 - * mode before and after switching between radix and hash. 824 - * 825 - * kvm_start_guest must still be called in real mode though, hence 826 - * the false argument. 827 - */ 828 - local_paca->kvm_hstate.hwthread_state = KVM_HWTHREAD_IN_IDLE; 829 - 830 - __ppc64_runlatch_off(); 831 - srr1 = power9_idle_stop(psscr, false); 832 - __ppc64_runlatch_on(); 833 - 834 - local_paca->kvm_hstate.hwthread_state = KVM_HWTHREAD_IN_KERNEL; 835 - /* Order setting hwthread_state vs. testing hwthread_req */ 836 - smp_mb(); 837 - if (local_paca->kvm_hstate.hwthread_req) 838 - srr1 = idle_kvm_start_guest(srr1); 839 - mtmsr(MSR_KERNEL); 840 - #endif 841 - 842 - return srr1; 843 - } 844 - #endif 845 - 846 - void power9_idle_type(unsigned long stop_psscr_val, 847 - unsigned long stop_psscr_mask) 848 - { 849 - unsigned long psscr; 850 - unsigned long srr1; 851 - 852 - if (!prep_irq_for_idle_irqsoff()) 853 - return; 854 - 855 - psscr = mfspr(SPRN_PSSCR); 856 - psscr = (psscr & ~stop_psscr_mask) | stop_psscr_val; 857 - 858 - __ppc64_runlatch_off(); 859 - srr1 = power9_idle_stop(psscr, true); 860 - __ppc64_runlatch_on(); 861 - 862 - fini_irq_for_idle_irqsoff(); 863 - 864 - irq_set_pending_from_srr1(srr1); 865 - } 866 - 867 - /* 868 - * Used for ppc_md.power_save which needs a function with no parameters 869 - */ 870 - void power9_idle(void) 871 - { 872 - power9_idle_type(pnv_default_stop_val, pnv_default_stop_mask); 873 - } 874 - 875 826 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 876 827 /* 877 828 * This is used in working around bugs in thread reconfiguration ··· 877 962 EXPORT_SYMBOL_GPL(pnv_power9_force_smt4_release); 878 963 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ 879 964 965 + struct p10_sprs { 966 + /* 967 + * SPRs that get lost in shallow states: 968 + * 969 + * P10 loses CR, LR, CTR, FPSCR, VSCR, XER, TAR, SPRG2, and HSPRG1 970 + * isa300 idle routines restore CR, LR. 971 + * CTR is volatile 972 + * idle thread doesn't use FP or VEC 973 + * kernel doesn't use TAR 974 + * HSPRG1 is only live in HV interrupt entry 975 + * SPRG2 is only live in KVM guests, KVM handles it. 976 + */ 977 + }; 978 + 979 + static unsigned long power10_idle_stop(unsigned long psscr, bool mmu_on) 980 + { 981 + int cpu = raw_smp_processor_id(); 982 + int first = cpu_first_thread_sibling(cpu); 983 + unsigned long *state = &paca_ptrs[first]->idle_state; 984 + unsigned long core_thread_mask = (1UL << threads_per_core) - 1; 985 + unsigned long srr1; 986 + unsigned long pls; 987 + // struct p10_sprs sprs = {}; /* avoid false used-uninitialised */ 988 + bool sprs_saved = false; 989 + 990 + if (!(psscr & (PSSCR_EC|PSSCR_ESL))) { 991 + /* EC=ESL=0 case */ 992 + 993 + BUG_ON(!mmu_on); 994 + 995 + /* 996 + * Wake synchronously. SRESET via xscom may still cause 997 + * a 0x100 powersave wakeup with SRR1 reason! 998 + */ 999 + srr1 = isa300_idle_stop_noloss(psscr); /* go idle */ 1000 + if (likely(!srr1)) 1001 + return 0; 1002 + 1003 + /* 1004 + * Registers not saved, can't recover! 1005 + * This would be a hardware bug 1006 + */ 1007 + BUG_ON((srr1 & SRR1_WAKESTATE) != SRR1_WS_NOLOSS); 1008 + 1009 + goto out; 1010 + } 1011 + 1012 + /* EC=ESL=1 case */ 1013 + if ((psscr & PSSCR_RL_MASK) >= deep_spr_loss_state) { 1014 + /* XXX: save SPRs for deep state loss here. */ 1015 + 1016 + sprs_saved = true; 1017 + 1018 + atomic_start_thread_idle(); 1019 + } 1020 + 1021 + srr1 = isa300_idle_stop_mayloss(psscr); /* go idle */ 1022 + 1023 + psscr = mfspr(SPRN_PSSCR); 1024 + 1025 + WARN_ON_ONCE(!srr1); 1026 + WARN_ON_ONCE(mfmsr() & (MSR_IR|MSR_DR)); 1027 + 1028 + if (unlikely((srr1 & SRR1_WAKEMASK_P8) == SRR1_WAKEHMI)) 1029 + hmi_exception_realmode(NULL); 1030 + 1031 + /* 1032 + * On POWER10, SRR1 bits do not match exactly as expected. 1033 + * SRR1_WS_GPRLOSS (10b) can also result in SPR loss, so 1034 + * just always test PSSCR for SPR/TB state loss. 1035 + */ 1036 + pls = (psscr & PSSCR_PLS) >> PSSCR_PLS_SHIFT; 1037 + if (likely(pls < deep_spr_loss_state)) { 1038 + if (sprs_saved) 1039 + atomic_stop_thread_idle(); 1040 + goto out; 1041 + } 1042 + 1043 + /* HV state loss */ 1044 + BUG_ON(!sprs_saved); 1045 + 1046 + atomic_lock_thread_idle(); 1047 + 1048 + if ((*state & core_thread_mask) != 0) 1049 + goto core_woken; 1050 + 1051 + /* XXX: restore per-core SPRs here */ 1052 + 1053 + if (pls >= pnv_first_tb_loss_level) { 1054 + /* TB loss */ 1055 + if (opal_resync_timebase() != OPAL_SUCCESS) 1056 + BUG(); 1057 + } 1058 + 1059 + /* 1060 + * isync after restoring shared SPRs and before unlocking. Unlock 1061 + * only contains hwsync which does not necessarily do the right 1062 + * thing for SPRs. 1063 + */ 1064 + isync(); 1065 + 1066 + core_woken: 1067 + atomic_unlock_and_stop_thread_idle(); 1068 + 1069 + /* XXX: restore per-thread SPRs here */ 1070 + 1071 + if (!radix_enabled()) 1072 + __slb_restore_bolted_realmode(); 1073 + 1074 + out: 1075 + if (mmu_on) 1076 + mtmsr(MSR_KERNEL); 1077 + 1078 + return srr1; 1079 + } 1080 + 1081 + #ifdef CONFIG_HOTPLUG_CPU 1082 + static unsigned long arch300_offline_stop(unsigned long psscr) 1083 + { 1084 + unsigned long srr1; 1085 + 1086 + #ifndef CONFIG_KVM_BOOK3S_HV_POSSIBLE 1087 + __ppc64_runlatch_off(); 1088 + if (cpu_has_feature(CPU_FTR_ARCH_31)) 1089 + srr1 = power10_idle_stop(psscr, true); 1090 + else 1091 + srr1 = power9_idle_stop(psscr, true); 1092 + __ppc64_runlatch_on(); 1093 + #else 1094 + /* 1095 + * Tell KVM we're entering idle. 1096 + * This does not have to be done in real mode because the P9 MMU 1097 + * is independent per-thread. Some steppings share radix/hash mode 1098 + * between threads, but in that case KVM has a barrier sync in real 1099 + * mode before and after switching between radix and hash. 1100 + * 1101 + * kvm_start_guest must still be called in real mode though, hence 1102 + * the false argument. 1103 + */ 1104 + local_paca->kvm_hstate.hwthread_state = KVM_HWTHREAD_IN_IDLE; 1105 + 1106 + __ppc64_runlatch_off(); 1107 + if (cpu_has_feature(CPU_FTR_ARCH_31)) 1108 + srr1 = power10_idle_stop(psscr, false); 1109 + else 1110 + srr1 = power9_idle_stop(psscr, false); 1111 + __ppc64_runlatch_on(); 1112 + 1113 + local_paca->kvm_hstate.hwthread_state = KVM_HWTHREAD_IN_KERNEL; 1114 + /* Order setting hwthread_state vs. testing hwthread_req */ 1115 + smp_mb(); 1116 + if (local_paca->kvm_hstate.hwthread_req) 1117 + srr1 = idle_kvm_start_guest(srr1); 1118 + mtmsr(MSR_KERNEL); 1119 + #endif 1120 + 1121 + return srr1; 1122 + } 1123 + #endif 1124 + 1125 + void arch300_idle_type(unsigned long stop_psscr_val, 1126 + unsigned long stop_psscr_mask) 1127 + { 1128 + unsigned long psscr; 1129 + unsigned long srr1; 1130 + 1131 + if (!prep_irq_for_idle_irqsoff()) 1132 + return; 1133 + 1134 + psscr = mfspr(SPRN_PSSCR); 1135 + psscr = (psscr & ~stop_psscr_mask) | stop_psscr_val; 1136 + 1137 + __ppc64_runlatch_off(); 1138 + if (cpu_has_feature(CPU_FTR_ARCH_31)) 1139 + srr1 = power10_idle_stop(psscr, true); 1140 + else 1141 + srr1 = power9_idle_stop(psscr, true); 1142 + __ppc64_runlatch_on(); 1143 + 1144 + fini_irq_for_idle_irqsoff(); 1145 + 1146 + irq_set_pending_from_srr1(srr1); 1147 + } 1148 + 1149 + /* 1150 + * Used for ppc_md.power_save which needs a function with no parameters 1151 + */ 1152 + static void arch300_idle(void) 1153 + { 1154 + arch300_idle_type(pnv_default_stop_val, pnv_default_stop_mask); 1155 + } 1156 + 880 1157 #ifdef CONFIG_HOTPLUG_CPU 881 1158 882 1159 void pnv_program_cpu_hotplug_lpcr(unsigned int cpu, u64 lpcr_val) ··· 1102 995 psscr = mfspr(SPRN_PSSCR); 1103 996 psscr = (psscr & ~pnv_deepest_stop_psscr_mask) | 1104 997 pnv_deepest_stop_psscr_val; 1105 - srr1 = power9_offline_stop(psscr); 998 + srr1 = arch300_offline_stop(psscr); 1106 999 } else if (cpu_has_feature(CPU_FTR_ARCH_206) && power7_offline_type) { 1107 1000 srr1 = power7_offline(); 1108 1001 } else { ··· 1200 1093 * @dt_idle_states: Number of idle state entries 1201 1094 * Returns 0 on success 1202 1095 */ 1203 - static void __init pnv_power9_idle_init(void) 1096 + static void __init pnv_arch300_idle_init(void) 1204 1097 { 1205 1098 u64 max_residency_ns = 0; 1206 1099 int i; 1100 + 1101 + /* stop is not really architected, we only have p9,p10 drivers */ 1102 + if (!pvr_version_is(PVR_POWER10) && !pvr_version_is(PVR_POWER9)) 1103 + return; 1207 1104 1208 1105 /* 1209 1106 * pnv_deepest_stop_{val,mask} should be set to values corresponding to ··· 1222 1111 int err; 1223 1112 struct pnv_idle_states_t *state = &pnv_idle_states[i]; 1224 1113 u64 psscr_rl = state->psscr_val & PSSCR_RL_MASK; 1114 + 1115 + /* No deep loss driver implemented for POWER10 yet */ 1116 + if (pvr_version_is(PVR_POWER10) && 1117 + state->flags & (OPAL_PM_TIMEBASE_STOP|OPAL_PM_LOSE_FULL_CONTEXT)) 1118 + continue; 1225 1119 1226 1120 if ((state->flags & OPAL_PM_TIMEBASE_STOP) && 1227 1121 (pnv_first_tb_loss_level > psscr_rl)) ··· 1278 1162 if (unlikely(!default_stop_found)) { 1279 1163 pr_warn("cpuidle-powernv: No suitable default stop state found. Disabling platform idle.\n"); 1280 1164 } else { 1281 - ppc_md.power_save = power9_idle; 1165 + ppc_md.power_save = arch300_idle; 1282 1166 pr_info("cpuidle-powernv: Default stop: psscr = 0x%016llx,mask=0x%016llx\n", 1283 1167 pnv_default_stop_val, pnv_default_stop_mask); 1284 1168 } ··· 1340 1224 } 1341 1225 1342 1226 if (cpu_has_feature(CPU_FTR_ARCH_300)) 1343 - pnv_power9_idle_init(); 1227 + pnv_arch300_idle_init(); 1344 1228 1345 1229 for (i = 0; i < nr_pnv_idle_states; i++) 1346 1230 supported_cpuidle_states |= pnv_idle_states[i].flags; ··· 1411 1295 for (i = 0; i < nr_idle_states; i++) 1412 1296 pnv_idle_states[i].residency_ns = temp_u32[i]; 1413 1297 1414 - /* For power9 */ 1298 + /* For power9 and later */ 1415 1299 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 1416 1300 /* Read pm_crtl_val */ 1417 1301 if (of_property_read_u64_array(np, "ibm,cpu-idle-state-psscr", ··· 1474 1358 if (!cpu_has_feature(CPU_FTR_ARCH_300)) { 1475 1359 /* P7/P8 nap */ 1476 1360 p->thread_idle_state = PNV_THREAD_RUNNING; 1477 - } else { 1478 - /* P9 stop */ 1361 + } else if (pvr_version_is(PVR_POWER9)) { 1362 + /* P9 stop workarounds */ 1479 1363 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 1480 1364 p->requested_psscr = 0; 1481 1365 atomic_set(&p->dont_stop, 0);
+1 -1
drivers/cpuidle/cpuidle-powernv.c
··· 141 141 struct cpuidle_driver *drv, 142 142 int index) 143 143 { 144 - power9_idle_type(stop_psscr_table[index].val, 144 + arch300_idle_type(stop_psscr_table[index].val, 145 145 stop_psscr_table[index].mask); 146 146 return index; 147 147 }