1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <asm/kvm_ppc.h>
8 static void freeze_pmu(unsigned long mmcr0, unsigned long mmcra)
10 if (!(mmcr0 & MMCR0_FC))
12 if (mmcra & MMCRA_SAMPLE_ENABLE)
14 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
15 if (!(mmcr0 & MMCR0_PMCCEXT))
17 if (!(mmcra & MMCRA_BHRB_DISABLE))
25 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
26 mmcr0 |= MMCR0_PMCCEXT;
27 mmcra = MMCRA_BHRB_DISABLE;
30 mtspr(SPRN_MMCR0, mmcr0);
31 mtspr(SPRN_MMCRA, mmcra);
35 void switch_pmu_to_guest(struct kvm_vcpu *vcpu,
36 struct p9_host_os_sprs *host_os_sprs)
41 lp = vcpu->arch.vpa.pinned_addr;
43 load_pmu = lp->pmcregs_in_use;
46 if (ppc_get_pmu_inuse()) {
47 /* POWER9, POWER10 do not implement HPMC or SPMC */
49 host_os_sprs->mmcr0 = mfspr(SPRN_MMCR0);
50 host_os_sprs->mmcra = mfspr(SPRN_MMCRA);
52 freeze_pmu(host_os_sprs->mmcr0, host_os_sprs->mmcra);
54 host_os_sprs->pmc1 = mfspr(SPRN_PMC1);
55 host_os_sprs->pmc2 = mfspr(SPRN_PMC2);
56 host_os_sprs->pmc3 = mfspr(SPRN_PMC3);
57 host_os_sprs->pmc4 = mfspr(SPRN_PMC4);
58 host_os_sprs->pmc5 = mfspr(SPRN_PMC5);
59 host_os_sprs->pmc6 = mfspr(SPRN_PMC6);
60 host_os_sprs->mmcr1 = mfspr(SPRN_MMCR1);
61 host_os_sprs->mmcr2 = mfspr(SPRN_MMCR2);
62 host_os_sprs->sdar = mfspr(SPRN_SDAR);
63 host_os_sprs->siar = mfspr(SPRN_SIAR);
64 host_os_sprs->sier1 = mfspr(SPRN_SIER);
66 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
67 host_os_sprs->mmcr3 = mfspr(SPRN_MMCR3);
68 host_os_sprs->sier2 = mfspr(SPRN_SIER2);
69 host_os_sprs->sier3 = mfspr(SPRN_SIER3);
73 #ifdef CONFIG_PPC_PSERIES
74 /* After saving PMU, before loading guest PMU, flip pmcregs_in_use */
75 if (kvmhv_on_pseries()) {
77 get_lppaca()->pmcregs_in_use = load_pmu;
83 * Load guest. If the VPA said the PMCs are not in use but the guest
84 * tried to access them anyway, HFSCR[PM] will be set by the HFAC
85 * fault so we can make forward progress.
87 if (load_pmu || (vcpu->arch.hfscr & HFSCR_PM)) {
88 mtspr(SPRN_PMC1, vcpu->arch.pmc[0]);
89 mtspr(SPRN_PMC2, vcpu->arch.pmc[1]);
90 mtspr(SPRN_PMC3, vcpu->arch.pmc[2]);
91 mtspr(SPRN_PMC4, vcpu->arch.pmc[3]);
92 mtspr(SPRN_PMC5, vcpu->arch.pmc[4]);
93 mtspr(SPRN_PMC6, vcpu->arch.pmc[5]);
94 mtspr(SPRN_MMCR1, vcpu->arch.mmcr[1]);
95 mtspr(SPRN_MMCR2, vcpu->arch.mmcr[2]);
96 mtspr(SPRN_SDAR, vcpu->arch.sdar);
97 mtspr(SPRN_SIAR, vcpu->arch.siar);
98 mtspr(SPRN_SIER, vcpu->arch.sier[0]);
100 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
101 mtspr(SPRN_MMCR3, vcpu->arch.mmcr[3]);
102 mtspr(SPRN_SIER2, vcpu->arch.sier[1]);
103 mtspr(SPRN_SIER3, vcpu->arch.sier[2]);
106 /* Set MMCRA then MMCR0 last */
107 mtspr(SPRN_MMCRA, vcpu->arch.mmcra);
108 mtspr(SPRN_MMCR0, vcpu->arch.mmcr[0]);
109 /* No isync necessary because we're starting counters */
111 if (!vcpu->arch.nested &&
112 (vcpu->arch.hfscr_permitted & HFSCR_PM))
113 vcpu->arch.hfscr |= HFSCR_PM;
116 EXPORT_SYMBOL_GPL(switch_pmu_to_guest);
118 void switch_pmu_to_host(struct kvm_vcpu *vcpu,
119 struct p9_host_os_sprs *host_os_sprs)
124 lp = vcpu->arch.vpa.pinned_addr;
126 save_pmu = lp->pmcregs_in_use;
127 if (IS_ENABLED(CONFIG_KVM_BOOK3S_HV_NESTED_PMU_WORKAROUND)) {
129 * Save pmu if this guest is capable of running nested guests.
130 * This is option is for old L1s that do not set their
131 * lppaca->pmcregs_in_use properly when entering their L2.
133 save_pmu |= nesting_enabled(vcpu->kvm);
137 vcpu->arch.mmcr[0] = mfspr(SPRN_MMCR0);
138 vcpu->arch.mmcra = mfspr(SPRN_MMCRA);
140 freeze_pmu(vcpu->arch.mmcr[0], vcpu->arch.mmcra);
142 vcpu->arch.pmc[0] = mfspr(SPRN_PMC1);
143 vcpu->arch.pmc[1] = mfspr(SPRN_PMC2);
144 vcpu->arch.pmc[2] = mfspr(SPRN_PMC3);
145 vcpu->arch.pmc[3] = mfspr(SPRN_PMC4);
146 vcpu->arch.pmc[4] = mfspr(SPRN_PMC5);
147 vcpu->arch.pmc[5] = mfspr(SPRN_PMC6);
148 vcpu->arch.mmcr[1] = mfspr(SPRN_MMCR1);
149 vcpu->arch.mmcr[2] = mfspr(SPRN_MMCR2);
150 vcpu->arch.sdar = mfspr(SPRN_SDAR);
151 vcpu->arch.siar = mfspr(SPRN_SIAR);
152 vcpu->arch.sier[0] = mfspr(SPRN_SIER);
154 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
155 vcpu->arch.mmcr[3] = mfspr(SPRN_MMCR3);
156 vcpu->arch.sier[1] = mfspr(SPRN_SIER2);
157 vcpu->arch.sier[2] = mfspr(SPRN_SIER3);
160 } else if (vcpu->arch.hfscr & HFSCR_PM) {
162 * The guest accessed PMC SPRs without specifying they should
163 * be preserved, or it cleared pmcregs_in_use after the last
164 * access. Just ensure they are frozen.
166 freeze_pmu(mfspr(SPRN_MMCR0), mfspr(SPRN_MMCRA));
169 * Demand-fault PMU register access in the guest.
171 * This is used to grab the guest's VPA pmcregs_in_use value
172 * and reflect it into the host's VPA in the case of a nested
175 * It also avoids having to zero-out SPRs after each guest
176 * exit to avoid side-channels when.
178 * This is cleared here when we exit the guest, so later HFSCR
179 * interrupt handling can add it back to run the guest with
180 * PM enabled next time.
182 if (!vcpu->arch.nested)
183 vcpu->arch.hfscr &= ~HFSCR_PM;
184 } /* otherwise the PMU should still be frozen */
186 #ifdef CONFIG_PPC_PSERIES
187 if (kvmhv_on_pseries()) {
189 get_lppaca()->pmcregs_in_use = ppc_get_pmu_inuse();
194 if (ppc_get_pmu_inuse()) {
195 mtspr(SPRN_PMC1, host_os_sprs->pmc1);
196 mtspr(SPRN_PMC2, host_os_sprs->pmc2);
197 mtspr(SPRN_PMC3, host_os_sprs->pmc3);
198 mtspr(SPRN_PMC4, host_os_sprs->pmc4);
199 mtspr(SPRN_PMC5, host_os_sprs->pmc5);
200 mtspr(SPRN_PMC6, host_os_sprs->pmc6);
201 mtspr(SPRN_MMCR1, host_os_sprs->mmcr1);
202 mtspr(SPRN_MMCR2, host_os_sprs->mmcr2);
203 mtspr(SPRN_SDAR, host_os_sprs->sdar);
204 mtspr(SPRN_SIAR, host_os_sprs->siar);
205 mtspr(SPRN_SIER, host_os_sprs->sier1);
207 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
208 mtspr(SPRN_MMCR3, host_os_sprs->mmcr3);
209 mtspr(SPRN_SIER2, host_os_sprs->sier2);
210 mtspr(SPRN_SIER3, host_os_sprs->sier3);
213 /* Set MMCRA then MMCR0 last */
214 mtspr(SPRN_MMCRA, host_os_sprs->mmcra);
215 mtspr(SPRN_MMCR0, host_os_sprs->mmcr0);
219 EXPORT_SYMBOL_GPL(switch_pmu_to_host);