Skip to content

Commit f4e40ea

Browse files
gaosong-loongsonchenhuacai
authored andcommitted
LoongArch: KVM: Add PMU support for guest
On LoongArch, the host and guest have their own PMU CSRs registers and they share PMU hardware resources. A set of PMU CSRs consists of a CTRL register and a CNTR register. We can set which PMU CSRs are used by the guest by writing to the GCFG register [24:26] bits. On KVM side: - Save the host PMU CSRs into structure kvm_context. - If the host supports the PMU feature. - When entering guest mode, save the host PMU CSRs and restore the guest PMU CSRs. - When exiting guest mode, save the guest PMU CSRs and restore the host PMU CSRs. Reviewed-by: Bibo Mao <[email protected]> Signed-off-by: Song Gao <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent acc7f20 commit f4e40ea

File tree

7 files changed

+205
-3
lines changed

7 files changed

+205
-3
lines changed

arch/loongarch/include/asm/kvm_csr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
: [val] "+r" (__v) \
3131
: [reg] "i" (csr) \
3232
: "memory"); \
33+
__v; \
3334
})
3435

3536
#define gcsr_xchg(v, m, csr) \
@@ -181,6 +182,8 @@ __BUILD_GCSR_OP(tlbidx)
181182
#define kvm_save_hw_gcsr(csr, gid) (csr->csrs[gid] = gcsr_read(gid))
182183
#define kvm_restore_hw_gcsr(csr, gid) (gcsr_write(csr->csrs[gid], gid))
183184

185+
#define kvm_read_clear_hw_gcsr(csr, gid) (csr->csrs[gid] = gcsr_write(0, gid))
186+
184187
int kvm_emu_iocsr(larch_inst inst, struct kvm_run *run, struct kvm_vcpu *vcpu);
185188

186189
static __always_inline unsigned long kvm_read_sw_gcsr(struct loongarch_csrs *csr, int gid)
@@ -208,4 +211,7 @@ static __always_inline void kvm_change_sw_gcsr(struct loongarch_csrs *csr,
208211
csr->csrs[gid] |= val & _mask;
209212
}
210213

214+
#define KVM_PMU_EVENT_ENABLED (CSR_PERFCTRL_PLV0 | CSR_PERFCTRL_PLV1 | \
215+
CSR_PERFCTRL_PLV2 | CSR_PERFCTRL_PLV3)
216+
211217
#endif /* __ASM_LOONGARCH_KVM_CSR_H__ */

arch/loongarch/include/asm/kvm_host.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define KVM_HALT_POLL_NS_DEFAULT 500000
3131
#define KVM_REQ_TLB_FLUSH_GPA KVM_ARCH_REQ(0)
3232
#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(1)
33+
#define KVM_REQ_PMU KVM_ARCH_REQ(2)
3334

3435
#define KVM_GUESTDBG_SW_BP_MASK \
3536
(KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)
@@ -60,9 +61,13 @@ struct kvm_arch_memory_slot {
6061
unsigned long flags;
6162
};
6263

64+
#define HOST_MAX_PMNUM 16
6365
struct kvm_context {
6466
unsigned long vpid_cache;
6567
struct kvm_vcpu *last_vcpu;
68+
/* Host PMU CSR */
69+
u64 perf_ctrl[HOST_MAX_PMNUM];
70+
u64 perf_cntr[HOST_MAX_PMNUM];
6671
};
6772

6873
struct kvm_world_switch {
@@ -134,8 +139,9 @@ enum emulation_result {
134139
#define KVM_LARCH_LSX (0x1 << 1)
135140
#define KVM_LARCH_LASX (0x1 << 2)
136141
#define KVM_LARCH_LBT (0x1 << 3)
137-
#define KVM_LARCH_SWCSR_LATEST (0x1 << 4)
138-
#define KVM_LARCH_HWCSR_USABLE (0x1 << 5)
142+
#define KVM_LARCH_PMU (0x1 << 4)
143+
#define KVM_LARCH_SWCSR_LATEST (0x1 << 5)
144+
#define KVM_LARCH_HWCSR_USABLE (0x1 << 6)
139145

140146
struct kvm_vcpu_arch {
141147
/*
@@ -174,6 +180,9 @@ struct kvm_vcpu_arch {
174180
/* CSR state */
175181
struct loongarch_csrs *csr;
176182

183+
/* Guest max PMU CSR id */
184+
int max_pmu_csrid;
185+
177186
/* GPR used as IO source/target */
178187
u32 io_gpr;
179188

@@ -246,6 +255,16 @@ static inline bool kvm_guest_has_lbt(struct kvm_vcpu_arch *arch)
246255
return arch->cpucfg[2] & (CPUCFG2_X86BT | CPUCFG2_ARMBT | CPUCFG2_MIPSBT);
247256
}
248257

258+
static inline bool kvm_guest_has_pmu(struct kvm_vcpu_arch *arch)
259+
{
260+
return arch->cpucfg[6] & CPUCFG6_PMP;
261+
}
262+
263+
static inline int kvm_get_pmu_num(struct kvm_vcpu_arch *arch)
264+
{
265+
return (arch->cpucfg[6] & CPUCFG6_PMNUM) >> CPUCFG6_PMNUM_SHIFT;
266+
}
267+
249268
/* Debug: dump vcpu state */
250269
int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu);
251270

arch/loongarch/include/asm/loongarch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
#define CPUCFG6_PMP BIT(0)
120120
#define CPUCFG6_PAMVER GENMASK(3, 1)
121121
#define CPUCFG6_PMNUM GENMASK(7, 4)
122+
#define CPUCFG6_PMNUM_SHIFT 4
122123
#define CPUCFG6_PMBITS GENMASK(13, 8)
123124
#define CPUCFG6_UPM BIT(14)
124125

arch/loongarch/include/uapi/asm/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct kvm_fpu {
9898
#define KVM_LOONGARCH_VM_FEAT_X86BT 2
9999
#define KVM_LOONGARCH_VM_FEAT_ARMBT 3
100100
#define KVM_LOONGARCH_VM_FEAT_MIPSBT 4
101+
#define KVM_LOONGARCH_VM_FEAT_PMU 5
101102

102103
/* Device Control API on vcpu fd */
103104
#define KVM_LOONGARCH_VCPU_CPUCFG 0

arch/loongarch/kvm/exit.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ static int kvm_handle_csr(struct kvm_vcpu *vcpu, larch_inst inst)
127127
rj = inst.reg2csr_format.rj;
128128
csrid = inst.reg2csr_format.csr;
129129

130+
if (csrid >= LOONGARCH_CSR_PERFCTRL0 && csrid <= vcpu->arch.max_pmu_csrid) {
131+
if (kvm_guest_has_pmu(&vcpu->arch)) {
132+
vcpu->arch.pc -= 4;
133+
kvm_make_request(KVM_REQ_PMU, vcpu);
134+
return EMULATE_DONE;
135+
}
136+
}
137+
130138
/* Process CSR ops */
131139
switch (rj) {
132140
case 0: /* process csrrd */

arch/loongarch/kvm/vcpu.c

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,126 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {
3232
sizeof(kvm_vcpu_stats_desc),
3333
};
3434

35+
static inline void kvm_save_host_pmu(struct kvm_vcpu *vcpu)
36+
{
37+
struct kvm_context *context;
38+
39+
context = this_cpu_ptr(vcpu->kvm->arch.vmcs);
40+
context->perf_cntr[0] = read_csr_perfcntr0();
41+
context->perf_cntr[1] = read_csr_perfcntr1();
42+
context->perf_cntr[2] = read_csr_perfcntr2();
43+
context->perf_cntr[3] = read_csr_perfcntr3();
44+
context->perf_ctrl[0] = write_csr_perfctrl0(0);
45+
context->perf_ctrl[1] = write_csr_perfctrl1(0);
46+
context->perf_ctrl[2] = write_csr_perfctrl2(0);
47+
context->perf_ctrl[3] = write_csr_perfctrl3(0);
48+
}
49+
50+
static inline void kvm_restore_host_pmu(struct kvm_vcpu *vcpu)
51+
{
52+
struct kvm_context *context;
53+
54+
context = this_cpu_ptr(vcpu->kvm->arch.vmcs);
55+
write_csr_perfcntr0(context->perf_cntr[0]);
56+
write_csr_perfcntr1(context->perf_cntr[1]);
57+
write_csr_perfcntr2(context->perf_cntr[2]);
58+
write_csr_perfcntr3(context->perf_cntr[3]);
59+
write_csr_perfctrl0(context->perf_ctrl[0]);
60+
write_csr_perfctrl1(context->perf_ctrl[1]);
61+
write_csr_perfctrl2(context->perf_ctrl[2]);
62+
write_csr_perfctrl3(context->perf_ctrl[3]);
63+
}
64+
65+
66+
static inline void kvm_save_guest_pmu(struct kvm_vcpu *vcpu)
67+
{
68+
struct loongarch_csrs *csr = vcpu->arch.csr;
69+
70+
kvm_save_hw_gcsr(csr, LOONGARCH_CSR_PERFCNTR0);
71+
kvm_save_hw_gcsr(csr, LOONGARCH_CSR_PERFCNTR1);
72+
kvm_save_hw_gcsr(csr, LOONGARCH_CSR_PERFCNTR2);
73+
kvm_save_hw_gcsr(csr, LOONGARCH_CSR_PERFCNTR3);
74+
kvm_read_clear_hw_gcsr(csr, LOONGARCH_CSR_PERFCTRL0);
75+
kvm_read_clear_hw_gcsr(csr, LOONGARCH_CSR_PERFCTRL1);
76+
kvm_read_clear_hw_gcsr(csr, LOONGARCH_CSR_PERFCTRL2);
77+
kvm_read_clear_hw_gcsr(csr, LOONGARCH_CSR_PERFCTRL3);
78+
}
79+
80+
static inline void kvm_restore_guest_pmu(struct kvm_vcpu *vcpu)
81+
{
82+
struct loongarch_csrs *csr = vcpu->arch.csr;
83+
84+
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_PERFCNTR0);
85+
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_PERFCNTR1);
86+
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_PERFCNTR2);
87+
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_PERFCNTR3);
88+
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_PERFCTRL0);
89+
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_PERFCTRL1);
90+
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_PERFCTRL2);
91+
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_PERFCTRL3);
92+
}
93+
94+
static int kvm_own_pmu(struct kvm_vcpu *vcpu)
95+
{
96+
unsigned long val;
97+
98+
if (!kvm_guest_has_pmu(&vcpu->arch))
99+
return -EINVAL;
100+
101+
kvm_save_host_pmu(vcpu);
102+
103+
/* Set PM0-PM(num) to guest */
104+
val = read_csr_gcfg() & ~CSR_GCFG_GPERF;
105+
val |= (kvm_get_pmu_num(&vcpu->arch) + 1) << CSR_GCFG_GPERF_SHIFT;
106+
write_csr_gcfg(val);
107+
108+
kvm_restore_guest_pmu(vcpu);
109+
110+
return 0;
111+
}
112+
113+
static void kvm_lose_pmu(struct kvm_vcpu *vcpu)
114+
{
115+
unsigned long val;
116+
struct loongarch_csrs *csr = vcpu->arch.csr;
117+
118+
if (!(vcpu->arch.aux_inuse & KVM_LARCH_PMU))
119+
return;
120+
121+
kvm_save_guest_pmu(vcpu);
122+
123+
/* Disable pmu access from guest */
124+
write_csr_gcfg(read_csr_gcfg() & ~CSR_GCFG_GPERF);
125+
126+
/*
127+
* Clear KVM_LARCH_PMU if the guest is not using PMU CSRs when
128+
* exiting the guest, so that the next time trap into the guest.
129+
* We don't need to deal with PMU CSRs contexts.
130+
*/
131+
val = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL0);
132+
val |= kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL1);
133+
val |= kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL2);
134+
val |= kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL3);
135+
if (!(val & KVM_PMU_EVENT_ENABLED))
136+
vcpu->arch.aux_inuse &= ~KVM_LARCH_PMU;
137+
138+
kvm_restore_host_pmu(vcpu);
139+
}
140+
141+
static void kvm_restore_pmu(struct kvm_vcpu *vcpu)
142+
{
143+
if ((vcpu->arch.aux_inuse & KVM_LARCH_PMU))
144+
kvm_make_request(KVM_REQ_PMU, vcpu);
145+
}
146+
147+
static void kvm_check_pmu(struct kvm_vcpu *vcpu)
148+
{
149+
if (kvm_check_request(KVM_REQ_PMU, vcpu)) {
150+
kvm_own_pmu(vcpu);
151+
vcpu->arch.aux_inuse |= KVM_LARCH_PMU;
152+
}
153+
}
154+
35155
static void kvm_update_stolen_time(struct kvm_vcpu *vcpu)
36156
{
37157
u32 version;
@@ -159,6 +279,7 @@ static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu)
159279
/* Make sure the vcpu mode has been written */
160280
smp_store_mb(vcpu->mode, IN_GUEST_MODE);
161281
kvm_check_vpid(vcpu);
282+
kvm_check_pmu(vcpu);
162283

163284
/*
164285
* Called after function kvm_check_vpid()
@@ -196,6 +317,8 @@ static int kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
196317
/* Set a default exit reason */
197318
run->exit_reason = KVM_EXIT_UNKNOWN;
198319

320+
kvm_lose_pmu(vcpu);
321+
199322
guest_timing_exit_irqoff();
200323
guest_state_exit_irqoff();
201324
local_irq_enable();
@@ -469,6 +592,22 @@ static int _kvm_setcsr(struct kvm_vcpu *vcpu, unsigned int id, u64 val)
469592

470593
kvm_write_sw_gcsr(csr, id, val);
471594

595+
/*
596+
* After modifying the PMU CSR register value of the vcpu.
597+
* If the PMU CSRs are used, we need to set KVM_REQ_PMU.
598+
*/
599+
if (id >= LOONGARCH_CSR_PERFCTRL0 && id <= LOONGARCH_CSR_PERFCNTR3) {
600+
unsigned long val;
601+
602+
val = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL0) |
603+
kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL1) |
604+
kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL2) |
605+
kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL3);
606+
607+
if (val & KVM_PMU_EVENT_ENABLED)
608+
kvm_make_request(KVM_REQ_PMU, vcpu);
609+
}
610+
472611
return ret;
473612
}
474613

@@ -513,6 +652,12 @@ static int _kvm_get_cpucfg_mask(int id, u64 *v)
513652
case LOONGARCH_CPUCFG5:
514653
*v = GENMASK(31, 0);
515654
return 0;
655+
case LOONGARCH_CPUCFG6:
656+
if (cpu_has_pmp)
657+
*v = GENMASK(14, 0);
658+
else
659+
*v = 0;
660+
return 0;
516661
case LOONGARCH_CPUCFG16:
517662
*v = GENMASK(16, 0);
518663
return 0;
@@ -557,6 +702,17 @@ static int kvm_check_cpucfg(int id, u64 val)
557702
/* LASX architecturally implies LSX and FP but val does not satisfy that */
558703
return -EINVAL;
559704
return 0;
705+
case LOONGARCH_CPUCFG6:
706+
if (val & CPUCFG6_PMP) {
707+
u32 host = read_cpucfg(LOONGARCH_CPUCFG6);
708+
if ((val & CPUCFG6_PMBITS) != (host & CPUCFG6_PMBITS))
709+
return -EINVAL;
710+
if ((val & CPUCFG6_PMNUM) > (host & CPUCFG6_PMNUM))
711+
return -EINVAL;
712+
if ((val & CPUCFG6_UPM) && !(host & CPUCFG6_UPM))
713+
return -EINVAL;
714+
}
715+
return 0;
560716
default:
561717
/*
562718
* Values for the other CPUCFG IDs are not being further validated
@@ -670,6 +826,9 @@ static int kvm_set_one_reg(struct kvm_vcpu *vcpu,
670826
if (ret)
671827
break;
672828
vcpu->arch.cpucfg[id] = (u32)v;
829+
if (id == LOONGARCH_CPUCFG6)
830+
vcpu->arch.max_pmu_csrid =
831+
LOONGARCH_CSR_PERFCTRL0 + 2 * kvm_get_pmu_num(&vcpu->arch) + 1;
673832
break;
674833
case KVM_REG_LOONGARCH_LBT:
675834
if (!kvm_guest_has_lbt(&vcpu->arch))
@@ -791,7 +950,8 @@ static int kvm_loongarch_cpucfg_has_attr(struct kvm_vcpu *vcpu,
791950
struct kvm_device_attr *attr)
792951
{
793952
switch (attr->attr) {
794-
case 2:
953+
case LOONGARCH_CPUCFG2:
954+
case LOONGARCH_CPUCFG6:
795955
return 0;
796956
default:
797957
return -ENXIO;
@@ -1356,6 +1516,9 @@ static int _kvm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
13561516
change_csr_gcfg(CSR_GCFG_MATC_MASK, CSR_GCFG_MATC_ROOT);
13571517
kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
13581518

1519+
/* Restore hardware PMU CSRs */
1520+
kvm_restore_pmu(vcpu);
1521+
13591522
/* Don't bother restoring registers multiple times unless necessary */
13601523
if (vcpu->arch.aux_inuse & KVM_LARCH_HWCSR_USABLE)
13611524
return 0;

arch/loongarch/kvm/vm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ static int kvm_vm_feature_has_attr(struct kvm *kvm, struct kvm_device_attr *attr
122122
if (cpu_has_lbt_mips)
123123
return 0;
124124
return -ENXIO;
125+
case KVM_LOONGARCH_VM_FEAT_PMU:
126+
if (cpu_has_pmp)
127+
return 0;
128+
return -ENXIO;
125129
default:
126130
return -ENXIO;
127131
}

0 commit comments

Comments
 (0)