pmu-emul.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Linaro Ltd.
  4. * Author: Shannon Zhao <shannon.zhao@linaro.org>
  5. */
  6. #include <linux/cpu.h>
  7. #include <linux/kvm.h>
  8. #include <linux/kvm_host.h>
  9. #include <linux/list.h>
  10. #include <linux/perf_event.h>
  11. #include <linux/perf/arm_pmu.h>
  12. #include <linux/uaccess.h>
  13. #include <asm/kvm_emulate.h>
  14. #include <kvm/arm_pmu.h>
  15. #include <kvm/arm_vgic.h>
  16. #define PERF_ATTR_CFG1_COUNTER_64BIT BIT(0)
  17. static LIST_HEAD(arm_pmus);
  18. static DEFINE_MUTEX(arm_pmus_lock);
  19. static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc);
  20. static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc);
  21. static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc);
  22. bool kvm_supports_guest_pmuv3(void)
  23. {
  24. guard(mutex)(&arm_pmus_lock);
  25. return !list_empty(&arm_pmus);
  26. }
  27. static struct kvm_vcpu *kvm_pmc_to_vcpu(const struct kvm_pmc *pmc)
  28. {
  29. return container_of(pmc, struct kvm_vcpu, arch.pmu.pmc[pmc->idx]);
  30. }
  31. static struct kvm_pmc *kvm_vcpu_idx_to_pmc(struct kvm_vcpu *vcpu, int cnt_idx)
  32. {
  33. return &vcpu->arch.pmu.pmc[cnt_idx];
  34. }
  35. static u32 __kvm_pmu_event_mask(unsigned int pmuver)
  36. {
  37. switch (pmuver) {
  38. case ID_AA64DFR0_EL1_PMUVer_IMP:
  39. return GENMASK(9, 0);
  40. case ID_AA64DFR0_EL1_PMUVer_V3P1:
  41. case ID_AA64DFR0_EL1_PMUVer_V3P4:
  42. case ID_AA64DFR0_EL1_PMUVer_V3P5:
  43. case ID_AA64DFR0_EL1_PMUVer_V3P7:
  44. return GENMASK(15, 0);
  45. default: /* Shouldn't be here, just for sanity */
  46. WARN_ONCE(1, "Unknown PMU version %d\n", pmuver);
  47. return 0;
  48. }
  49. }
  50. static u32 kvm_pmu_event_mask(struct kvm *kvm)
  51. {
  52. u64 dfr0 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64DFR0_EL1);
  53. u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, dfr0);
  54. return __kvm_pmu_event_mask(pmuver);
  55. }
  56. u64 kvm_pmu_evtyper_mask(struct kvm *kvm)
  57. {
  58. u64 mask = ARMV8_PMU_EXCLUDE_EL1 | ARMV8_PMU_EXCLUDE_EL0 |
  59. kvm_pmu_event_mask(kvm);
  60. if (kvm_has_feat(kvm, ID_AA64PFR0_EL1, EL2, IMP))
  61. mask |= ARMV8_PMU_INCLUDE_EL2;
  62. if (kvm_has_feat(kvm, ID_AA64PFR0_EL1, EL3, IMP))
  63. mask |= ARMV8_PMU_EXCLUDE_NS_EL0 |
  64. ARMV8_PMU_EXCLUDE_NS_EL1 |
  65. ARMV8_PMU_EXCLUDE_EL3;
  66. return mask;
  67. }
  68. /**
  69. * kvm_pmc_is_64bit - determine if counter is 64bit
  70. * @pmc: counter context
  71. */
  72. static bool kvm_pmc_is_64bit(struct kvm_pmc *pmc)
  73. {
  74. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  75. return (pmc->idx == ARMV8_PMU_CYCLE_IDX ||
  76. kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5));
  77. }
  78. static bool kvm_pmc_has_64bit_overflow(struct kvm_pmc *pmc)
  79. {
  80. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  81. u64 val = kvm_vcpu_read_pmcr(vcpu);
  82. if (kvm_pmu_counter_is_hyp(vcpu, pmc->idx))
  83. return __vcpu_sys_reg(vcpu, MDCR_EL2) & MDCR_EL2_HLP;
  84. return (pmc->idx < ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LP)) ||
  85. (pmc->idx == ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LC));
  86. }
  87. static bool kvm_pmu_counter_can_chain(struct kvm_pmc *pmc)
  88. {
  89. return (!(pmc->idx & 1) && (pmc->idx + 1) < ARMV8_PMU_CYCLE_IDX &&
  90. !kvm_pmc_has_64bit_overflow(pmc));
  91. }
  92. static u32 counter_index_to_reg(u64 idx)
  93. {
  94. return (idx == ARMV8_PMU_CYCLE_IDX) ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + idx;
  95. }
  96. static u32 counter_index_to_evtreg(u64 idx)
  97. {
  98. return (idx == ARMV8_PMU_CYCLE_IDX) ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + idx;
  99. }
  100. static u64 kvm_pmc_read_evtreg(const struct kvm_pmc *pmc)
  101. {
  102. return __vcpu_sys_reg(kvm_pmc_to_vcpu(pmc), counter_index_to_evtreg(pmc->idx));
  103. }
  104. static u64 kvm_pmu_get_pmc_value(struct kvm_pmc *pmc)
  105. {
  106. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  107. u64 counter, reg, enabled, running;
  108. reg = counter_index_to_reg(pmc->idx);
  109. counter = __vcpu_sys_reg(vcpu, reg);
  110. /*
  111. * The real counter value is equal to the value of counter register plus
  112. * the value perf event counts.
  113. */
  114. if (pmc->perf_event)
  115. counter += perf_event_read_value(pmc->perf_event, &enabled,
  116. &running);
  117. if (!kvm_pmc_is_64bit(pmc))
  118. counter = lower_32_bits(counter);
  119. return counter;
  120. }
  121. /**
  122. * kvm_pmu_get_counter_value - get PMU counter value
  123. * @vcpu: The vcpu pointer
  124. * @select_idx: The counter index
  125. */
  126. u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
  127. {
  128. return kvm_pmu_get_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx));
  129. }
  130. static void kvm_pmu_set_pmc_value(struct kvm_pmc *pmc, u64 val, bool force)
  131. {
  132. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  133. u64 reg;
  134. kvm_pmu_release_perf_event(pmc);
  135. reg = counter_index_to_reg(pmc->idx);
  136. if (vcpu_mode_is_32bit(vcpu) && pmc->idx != ARMV8_PMU_CYCLE_IDX &&
  137. !force) {
  138. /*
  139. * Even with PMUv3p5, AArch32 cannot write to the top
  140. * 32bit of the counters. The only possible course of
  141. * action is to use PMCR.P, which will reset them to
  142. * 0 (the only use of the 'force' parameter).
  143. */
  144. val = __vcpu_sys_reg(vcpu, reg) & GENMASK(63, 32);
  145. val |= lower_32_bits(val);
  146. }
  147. __vcpu_assign_sys_reg(vcpu, reg, val);
  148. /* Recreate the perf event to reflect the updated sample_period */
  149. kvm_pmu_create_perf_event(pmc);
  150. }
  151. /**
  152. * kvm_pmu_set_counter_value - set PMU counter value
  153. * @vcpu: The vcpu pointer
  154. * @select_idx: The counter index
  155. * @val: The counter value
  156. */
  157. void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
  158. {
  159. kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx), val, false);
  160. }
  161. /**
  162. * kvm_pmu_set_counter_value_user - set PMU counter value from user
  163. * @vcpu: The vcpu pointer
  164. * @select_idx: The counter index
  165. * @val: The counter value
  166. */
  167. void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
  168. {
  169. kvm_pmu_release_perf_event(kvm_vcpu_idx_to_pmc(vcpu, select_idx));
  170. __vcpu_assign_sys_reg(vcpu, counter_index_to_reg(select_idx), val);
  171. kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
  172. }
  173. /**
  174. * kvm_pmu_release_perf_event - remove the perf event
  175. * @pmc: The PMU counter pointer
  176. */
  177. static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
  178. {
  179. if (pmc->perf_event) {
  180. perf_event_disable(pmc->perf_event);
  181. perf_event_release_kernel(pmc->perf_event);
  182. pmc->perf_event = NULL;
  183. }
  184. }
  185. /**
  186. * kvm_pmu_stop_counter - stop PMU counter
  187. * @pmc: The PMU counter pointer
  188. *
  189. * If this counter has been configured to monitor some event, release it here.
  190. */
  191. static void kvm_pmu_stop_counter(struct kvm_pmc *pmc)
  192. {
  193. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  194. u64 reg, val;
  195. if (!pmc->perf_event)
  196. return;
  197. val = kvm_pmu_get_pmc_value(pmc);
  198. reg = counter_index_to_reg(pmc->idx);
  199. __vcpu_assign_sys_reg(vcpu, reg, val);
  200. kvm_pmu_release_perf_event(pmc);
  201. }
  202. /**
  203. * kvm_pmu_vcpu_init - assign pmu counter idx for cpu
  204. * @vcpu: The vcpu pointer
  205. *
  206. */
  207. void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu)
  208. {
  209. int i;
  210. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  211. for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++)
  212. pmu->pmc[i].idx = i;
  213. }
  214. /**
  215. * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
  216. * @vcpu: The vcpu pointer
  217. *
  218. */
  219. void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
  220. {
  221. int i;
  222. for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++)
  223. kvm_pmu_release_perf_event(kvm_vcpu_idx_to_pmc(vcpu, i));
  224. irq_work_sync(&vcpu->arch.pmu.overflow_work);
  225. }
  226. static u64 kvm_pmu_hyp_counter_mask(struct kvm_vcpu *vcpu)
  227. {
  228. unsigned int hpmn, n;
  229. if (!vcpu_has_nv(vcpu))
  230. return 0;
  231. hpmn = SYS_FIELD_GET(MDCR_EL2, HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
  232. n = vcpu->kvm->arch.nr_pmu_counters;
  233. /*
  234. * Programming HPMN to a value greater than PMCR_EL0.N is
  235. * CONSTRAINED UNPREDICTABLE. Make the implementation choice that an
  236. * UNKNOWN number of counters (in our case, zero) are reserved for EL2.
  237. */
  238. if (hpmn >= n)
  239. return 0;
  240. /*
  241. * Programming HPMN=0 is CONSTRAINED UNPREDICTABLE if FEAT_HPMN0 isn't
  242. * implemented. Since KVM's ability to emulate HPMN=0 does not directly
  243. * depend on hardware (all PMU registers are trapped), make the
  244. * implementation choice that all counters are included in the second
  245. * range reserved for EL2/EL3.
  246. */
  247. return GENMASK(n - 1, hpmn);
  248. }
  249. bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int idx)
  250. {
  251. return kvm_pmu_hyp_counter_mask(vcpu) & BIT(idx);
  252. }
  253. u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)
  254. {
  255. u64 mask = kvm_pmu_implemented_counter_mask(vcpu);
  256. if (!vcpu_has_nv(vcpu) || vcpu_is_el2(vcpu))
  257. return mask;
  258. return mask & ~kvm_pmu_hyp_counter_mask(vcpu);
  259. }
  260. u64 kvm_pmu_implemented_counter_mask(struct kvm_vcpu *vcpu)
  261. {
  262. u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, kvm_vcpu_read_pmcr(vcpu));
  263. if (val == 0)
  264. return BIT(ARMV8_PMU_CYCLE_IDX);
  265. else
  266. return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
  267. }
  268. static void kvm_pmc_enable_perf_event(struct kvm_pmc *pmc)
  269. {
  270. if (!pmc->perf_event) {
  271. kvm_pmu_create_perf_event(pmc);
  272. return;
  273. }
  274. perf_event_enable(pmc->perf_event);
  275. if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
  276. kvm_debug("fail to enable perf event\n");
  277. }
  278. static void kvm_pmc_disable_perf_event(struct kvm_pmc *pmc)
  279. {
  280. if (pmc->perf_event)
  281. perf_event_disable(pmc->perf_event);
  282. }
  283. void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val)
  284. {
  285. int i;
  286. if (!val)
  287. return;
  288. for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++) {
  289. struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
  290. if (!(val & BIT(i)))
  291. continue;
  292. if (kvm_pmu_counter_is_enabled(pmc))
  293. kvm_pmc_enable_perf_event(pmc);
  294. else
  295. kvm_pmc_disable_perf_event(pmc);
  296. }
  297. kvm_vcpu_pmu_restore_guest(vcpu);
  298. }
  299. /*
  300. * Returns the PMU overflow state, which is true if there exists an event
  301. * counter where the values of the global enable control, PMOVSSET_EL0[n], and
  302. * PMINTENSET_EL1[n] are all 1.
  303. */
  304. static bool kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
  305. {
  306. u64 reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
  307. reg &= __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
  308. /*
  309. * PMCR_EL0.E is the global enable control for event counters available
  310. * to EL0 and EL1.
  311. */
  312. if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E))
  313. reg &= kvm_pmu_hyp_counter_mask(vcpu);
  314. /*
  315. * Otherwise, MDCR_EL2.HPME is the global enable control for event
  316. * counters reserved for EL2.
  317. */
  318. if (!(vcpu_read_sys_reg(vcpu, MDCR_EL2) & MDCR_EL2_HPME))
  319. reg &= ~kvm_pmu_hyp_counter_mask(vcpu);
  320. return reg;
  321. }
  322. static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
  323. {
  324. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  325. bool overflow;
  326. overflow = kvm_pmu_overflow_status(vcpu);
  327. if (pmu->irq_level == overflow)
  328. return;
  329. pmu->irq_level = overflow;
  330. if (likely(irqchip_in_kernel(vcpu->kvm))) {
  331. int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu,
  332. pmu->irq_num, overflow, pmu);
  333. WARN_ON(ret);
  334. }
  335. }
  336. bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
  337. {
  338. struct kvm_pmu *pmu = &vcpu->arch.pmu;
  339. struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
  340. bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
  341. if (likely(irqchip_in_kernel(vcpu->kvm)))
  342. return false;
  343. return pmu->irq_level != run_level;
  344. }
  345. /*
  346. * Reflect the PMU overflow interrupt output level into the kvm_run structure
  347. */
  348. void kvm_pmu_update_run(struct kvm_vcpu *vcpu)
  349. {
  350. struct kvm_sync_regs *regs = &vcpu->run->s.regs;
  351. /* Populate the timer bitmap for user space */
  352. regs->device_irq_level &= ~KVM_ARM_DEV_PMU;
  353. if (vcpu->arch.pmu.irq_level)
  354. regs->device_irq_level |= KVM_ARM_DEV_PMU;
  355. }
  356. /**
  357. * kvm_pmu_flush_hwstate - flush pmu state to cpu
  358. * @vcpu: The vcpu pointer
  359. *
  360. * Check if the PMU has overflowed while we were running in the host, and inject
  361. * an interrupt if that was the case.
  362. */
  363. void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
  364. {
  365. kvm_pmu_update_state(vcpu);
  366. }
  367. /**
  368. * kvm_pmu_sync_hwstate - sync pmu state from cpu
  369. * @vcpu: The vcpu pointer
  370. *
  371. * Check if the PMU has overflowed while we were running in the guest, and
  372. * inject an interrupt if that was the case.
  373. */
  374. void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
  375. {
  376. kvm_pmu_update_state(vcpu);
  377. }
  378. /*
  379. * When perf interrupt is an NMI, we cannot safely notify the vcpu corresponding
  380. * to the event.
  381. * This is why we need a callback to do it once outside of the NMI context.
  382. */
  383. static void kvm_pmu_perf_overflow_notify_vcpu(struct irq_work *work)
  384. {
  385. struct kvm_vcpu *vcpu;
  386. vcpu = container_of(work, struct kvm_vcpu, arch.pmu.overflow_work);
  387. kvm_vcpu_kick(vcpu);
  388. }
  389. /*
  390. * Perform an increment on any of the counters described in @mask,
  391. * generating the overflow if required, and propagate it as a chained
  392. * event if possible.
  393. */
  394. static void kvm_pmu_counter_increment(struct kvm_vcpu *vcpu,
  395. unsigned long mask, u32 event)
  396. {
  397. int i;
  398. if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E))
  399. return;
  400. /* Weed out disabled counters */
  401. mask &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
  402. for_each_set_bit(i, &mask, ARMV8_PMU_CYCLE_IDX) {
  403. struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
  404. u64 type, reg;
  405. /* Filter on event type */
  406. type = __vcpu_sys_reg(vcpu, counter_index_to_evtreg(i));
  407. type &= kvm_pmu_event_mask(vcpu->kvm);
  408. if (type != event)
  409. continue;
  410. /* Increment this counter */
  411. reg = __vcpu_sys_reg(vcpu, counter_index_to_reg(i)) + 1;
  412. if (!kvm_pmc_is_64bit(pmc))
  413. reg = lower_32_bits(reg);
  414. __vcpu_assign_sys_reg(vcpu, counter_index_to_reg(i), reg);
  415. /* No overflow? move on */
  416. if (kvm_pmc_has_64bit_overflow(pmc) ? reg : lower_32_bits(reg))
  417. continue;
  418. /* Mark overflow */
  419. __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, BIT(i));
  420. if (kvm_pmu_counter_can_chain(pmc))
  421. kvm_pmu_counter_increment(vcpu, BIT(i + 1),
  422. ARMV8_PMUV3_PERFCTR_CHAIN);
  423. }
  424. }
  425. /* Compute the sample period for a given counter value */
  426. static u64 compute_period(struct kvm_pmc *pmc, u64 counter)
  427. {
  428. u64 val;
  429. if (kvm_pmc_is_64bit(pmc) && kvm_pmc_has_64bit_overflow(pmc))
  430. val = (-counter) & GENMASK(63, 0);
  431. else
  432. val = (-counter) & GENMASK(31, 0);
  433. return val;
  434. }
  435. /*
  436. * When the perf event overflows, set the overflow status and inform the vcpu.
  437. */
  438. static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
  439. struct perf_sample_data *data,
  440. struct pt_regs *regs)
  441. {
  442. struct kvm_pmc *pmc = perf_event->overflow_handler_context;
  443. struct arm_pmu *cpu_pmu = to_arm_pmu(perf_event->pmu);
  444. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  445. int idx = pmc->idx;
  446. u64 period;
  447. cpu_pmu->pmu.stop(perf_event, PERF_EF_UPDATE);
  448. /*
  449. * Reset the sample period to the architectural limit,
  450. * i.e. the point where the counter overflows.
  451. */
  452. period = compute_period(pmc, local64_read(&perf_event->count));
  453. local64_set(&perf_event->hw.period_left, 0);
  454. perf_event->attr.sample_period = period;
  455. perf_event->hw.sample_period = period;
  456. __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, BIT(idx));
  457. if (kvm_pmu_counter_can_chain(pmc))
  458. kvm_pmu_counter_increment(vcpu, BIT(idx + 1),
  459. ARMV8_PMUV3_PERFCTR_CHAIN);
  460. if (kvm_pmu_overflow_status(vcpu)) {
  461. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  462. if (!in_nmi())
  463. kvm_vcpu_kick(vcpu);
  464. else
  465. irq_work_queue(&vcpu->arch.pmu.overflow_work);
  466. }
  467. cpu_pmu->pmu.start(perf_event, PERF_EF_RELOAD);
  468. }
  469. /**
  470. * kvm_pmu_software_increment - do software increment
  471. * @vcpu: The vcpu pointer
  472. * @val: the value guest writes to PMSWINC register
  473. */
  474. void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
  475. {
  476. kvm_pmu_counter_increment(vcpu, val, ARMV8_PMUV3_PERFCTR_SW_INCR);
  477. }
  478. /**
  479. * kvm_pmu_handle_pmcr - handle PMCR register
  480. * @vcpu: The vcpu pointer
  481. * @val: the value guest writes to PMCR register
  482. */
  483. void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
  484. {
  485. int i;
  486. /* Fixup PMCR_EL0 to reconcile the PMU version and the LP bit */
  487. if (!kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5))
  488. val &= ~ARMV8_PMU_PMCR_LP;
  489. /* Request a reload of the PMU to enable/disable affected counters */
  490. if ((__vcpu_sys_reg(vcpu, PMCR_EL0) ^ val) & ARMV8_PMU_PMCR_E)
  491. kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
  492. /* The reset bits don't indicate any state, and shouldn't be saved. */
  493. __vcpu_assign_sys_reg(vcpu, PMCR_EL0, (val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P)));
  494. if (val & ARMV8_PMU_PMCR_C)
  495. kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
  496. if (val & ARMV8_PMU_PMCR_P) {
  497. unsigned long mask = kvm_pmu_implemented_counter_mask(vcpu) &
  498. ~BIT(ARMV8_PMU_CYCLE_IDX);
  499. if (!vcpu_is_el2(vcpu))
  500. mask &= ~kvm_pmu_hyp_counter_mask(vcpu);
  501. for_each_set_bit(i, &mask, 32)
  502. kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, i), 0, true);
  503. }
  504. }
  505. static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc)
  506. {
  507. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  508. unsigned int mdcr = __vcpu_sys_reg(vcpu, MDCR_EL2);
  509. if (!(__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(pmc->idx)))
  510. return false;
  511. if (kvm_pmu_counter_is_hyp(vcpu, pmc->idx))
  512. return mdcr & MDCR_EL2_HPME;
  513. return kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E;
  514. }
  515. static bool kvm_pmc_counts_at_el0(struct kvm_pmc *pmc)
  516. {
  517. u64 evtreg = kvm_pmc_read_evtreg(pmc);
  518. bool nsu = evtreg & ARMV8_PMU_EXCLUDE_NS_EL0;
  519. bool u = evtreg & ARMV8_PMU_EXCLUDE_EL0;
  520. return u == nsu;
  521. }
  522. static bool kvm_pmc_counts_at_el1(struct kvm_pmc *pmc)
  523. {
  524. u64 evtreg = kvm_pmc_read_evtreg(pmc);
  525. bool nsk = evtreg & ARMV8_PMU_EXCLUDE_NS_EL1;
  526. bool p = evtreg & ARMV8_PMU_EXCLUDE_EL1;
  527. return p == nsk;
  528. }
  529. static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc)
  530. {
  531. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  532. u64 mdcr = __vcpu_sys_reg(vcpu, MDCR_EL2);
  533. if (!kvm_pmu_counter_is_hyp(vcpu, pmc->idx) && (mdcr & MDCR_EL2_HPMD))
  534. return false;
  535. return kvm_pmc_read_evtreg(pmc) & ARMV8_PMU_INCLUDE_EL2;
  536. }
  537. static int kvm_map_pmu_event(struct kvm *kvm, unsigned int eventsel)
  538. {
  539. struct arm_pmu *pmu = kvm->arch.arm_pmu;
  540. /*
  541. * The CPU PMU likely isn't PMUv3; let the driver provide a mapping
  542. * for the guest's PMUv3 event ID.
  543. */
  544. if (unlikely(pmu->map_pmuv3_event))
  545. return pmu->map_pmuv3_event(eventsel);
  546. return eventsel;
  547. }
  548. /**
  549. * kvm_pmu_create_perf_event - create a perf event for a counter
  550. * @pmc: Counter context
  551. */
  552. static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc)
  553. {
  554. struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
  555. struct arm_pmu *arm_pmu = vcpu->kvm->arch.arm_pmu;
  556. struct perf_event *event;
  557. struct perf_event_attr attr;
  558. int eventsel;
  559. u64 evtreg;
  560. evtreg = kvm_pmc_read_evtreg(pmc);
  561. kvm_pmu_stop_counter(pmc);
  562. if (pmc->idx == ARMV8_PMU_CYCLE_IDX)
  563. eventsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
  564. else
  565. eventsel = evtreg & kvm_pmu_event_mask(vcpu->kvm);
  566. /*
  567. * Neither SW increment nor chained events need to be backed
  568. * by a perf event.
  569. */
  570. if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR ||
  571. eventsel == ARMV8_PMUV3_PERFCTR_CHAIN)
  572. return;
  573. /*
  574. * If we have a filter in place and that the event isn't allowed, do
  575. * not install a perf event either.
  576. */
  577. if (vcpu->kvm->arch.pmu_filter &&
  578. !test_bit(eventsel, vcpu->kvm->arch.pmu_filter))
  579. return;
  580. /*
  581. * Don't create an event if we're running on hardware that requires
  582. * PMUv3 event translation and we couldn't find a valid mapping.
  583. */
  584. eventsel = kvm_map_pmu_event(vcpu->kvm, eventsel);
  585. if (eventsel < 0)
  586. return;
  587. memset(&attr, 0, sizeof(struct perf_event_attr));
  588. attr.type = arm_pmu->pmu.type;
  589. attr.size = sizeof(attr);
  590. attr.pinned = 1;
  591. attr.disabled = !kvm_pmu_counter_is_enabled(pmc);
  592. attr.exclude_user = !kvm_pmc_counts_at_el0(pmc);
  593. attr.exclude_hv = 1; /* Don't count EL2 events */
  594. attr.exclude_host = 1; /* Don't count host events */
  595. attr.config = eventsel;
  596. /*
  597. * Filter events at EL1 (i.e. vEL2) when in a hyp context based on the
  598. * guest's EL2 filter.
  599. */
  600. if (unlikely(is_hyp_ctxt(vcpu)))
  601. attr.exclude_kernel = !kvm_pmc_counts_at_el2(pmc);
  602. else
  603. attr.exclude_kernel = !kvm_pmc_counts_at_el1(pmc);
  604. /*
  605. * If counting with a 64bit counter, advertise it to the perf
  606. * code, carefully dealing with the initial sample period
  607. * which also depends on the overflow.
  608. */
  609. if (kvm_pmc_is_64bit(pmc))
  610. attr.config1 |= PERF_ATTR_CFG1_COUNTER_64BIT;
  611. attr.sample_period = compute_period(pmc, kvm_pmu_get_pmc_value(pmc));
  612. event = perf_event_create_kernel_counter(&attr, -1, current,
  613. kvm_pmu_perf_overflow, pmc);
  614. if (IS_ERR(event)) {
  615. pr_err_once("kvm: pmu event creation failed %ld\n",
  616. PTR_ERR(event));
  617. return;
  618. }
  619. pmc->perf_event = event;
  620. }
  621. /**
  622. * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
  623. * @vcpu: The vcpu pointer
  624. * @data: The data guest writes to PMXEVTYPER_EL0
  625. * @select_idx: The number of selected counter
  626. *
  627. * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
  628. * event with given hardware event number. Here we call perf_event API to
  629. * emulate this action and create a kernel perf event for it.
  630. */
  631. void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
  632. u64 select_idx)
  633. {
  634. struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, select_idx);
  635. u64 reg;
  636. reg = counter_index_to_evtreg(pmc->idx);
  637. __vcpu_assign_sys_reg(vcpu, reg, (data & kvm_pmu_evtyper_mask(vcpu->kvm)));
  638. kvm_pmu_create_perf_event(pmc);
  639. }
  640. void kvm_host_pmu_init(struct arm_pmu *pmu)
  641. {
  642. struct arm_pmu_entry *entry;
  643. /*
  644. * Check the sanitised PMU version for the system, as KVM does not
  645. * support implementations where PMUv3 exists on a subset of CPUs.
  646. */
  647. if (!pmuv3_implemented(kvm_arm_pmu_get_pmuver_limit()))
  648. return;
  649. guard(mutex)(&arm_pmus_lock);
  650. entry = kmalloc_obj(*entry);
  651. if (!entry)
  652. return;
  653. entry->arm_pmu = pmu;
  654. list_add_tail(&entry->entry, &arm_pmus);
  655. }
  656. static struct arm_pmu *kvm_pmu_probe_armpmu(void)
  657. {
  658. struct arm_pmu_entry *entry;
  659. struct arm_pmu *pmu;
  660. int cpu;
  661. guard(mutex)(&arm_pmus_lock);
  662. /*
  663. * It is safe to use a stale cpu to iterate the list of PMUs so long as
  664. * the same value is used for the entirety of the loop. Given this, and
  665. * the fact that no percpu data is used for the lookup there is no need
  666. * to disable preemption.
  667. *
  668. * It is still necessary to get a valid cpu, though, to probe for the
  669. * default PMU instance as userspace is not required to specify a PMU
  670. * type. In order to uphold the preexisting behavior KVM selects the
  671. * PMU instance for the core during vcpu init. A dependent use
  672. * case would be a user with disdain of all things big.LITTLE that
  673. * affines the VMM to a particular cluster of cores.
  674. *
  675. * In any case, userspace should just do the sane thing and use the UAPI
  676. * to select a PMU type directly. But, be wary of the baggage being
  677. * carried here.
  678. */
  679. cpu = raw_smp_processor_id();
  680. list_for_each_entry(entry, &arm_pmus, entry) {
  681. pmu = entry->arm_pmu;
  682. if (cpumask_test_cpu(cpu, &pmu->supported_cpus))
  683. return pmu;
  684. }
  685. return NULL;
  686. }
  687. static u64 __compute_pmceid(struct arm_pmu *pmu, bool pmceid1)
  688. {
  689. u32 hi[2], lo[2];
  690. bitmap_to_arr32(lo, pmu->pmceid_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
  691. bitmap_to_arr32(hi, pmu->pmceid_ext_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
  692. return ((u64)hi[pmceid1] << 32) | lo[pmceid1];
  693. }
  694. static u64 compute_pmceid0(struct arm_pmu *pmu)
  695. {
  696. u64 val = __compute_pmceid(pmu, 0);
  697. /* always support SW_INCR */
  698. val |= BIT(ARMV8_PMUV3_PERFCTR_SW_INCR);
  699. /* always support CHAIN */
  700. val |= BIT(ARMV8_PMUV3_PERFCTR_CHAIN);
  701. return val;
  702. }
  703. static u64 compute_pmceid1(struct arm_pmu *pmu)
  704. {
  705. u64 val = __compute_pmceid(pmu, 1);
  706. /*
  707. * Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled
  708. * as RAZ
  709. */
  710. val &= ~(BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32) |
  711. BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND - 32) |
  712. BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND - 32));
  713. return val;
  714. }
  715. u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
  716. {
  717. struct arm_pmu *cpu_pmu = vcpu->kvm->arch.arm_pmu;
  718. unsigned long *bmap = vcpu->kvm->arch.pmu_filter;
  719. u64 val, mask = 0;
  720. int base, i, nr_events;
  721. if (!pmceid1) {
  722. val = compute_pmceid0(cpu_pmu);
  723. base = 0;
  724. } else {
  725. val = compute_pmceid1(cpu_pmu);
  726. base = 32;
  727. }
  728. if (!bmap)
  729. return val;
  730. nr_events = kvm_pmu_event_mask(vcpu->kvm) + 1;
  731. for (i = 0; i < 32; i += 8) {
  732. u64 byte;
  733. byte = bitmap_get_value8(bmap, base + i);
  734. mask |= byte << i;
  735. if (nr_events >= (0x4000 + base + 32)) {
  736. byte = bitmap_get_value8(bmap, 0x4000 + base + i);
  737. mask |= byte << (32 + i);
  738. }
  739. }
  740. return val & mask;
  741. }
  742. void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu)
  743. {
  744. u64 mask = kvm_pmu_implemented_counter_mask(vcpu);
  745. __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, &=, mask);
  746. __vcpu_rmw_sys_reg(vcpu, PMINTENSET_EL1, &=, mask);
  747. __vcpu_rmw_sys_reg(vcpu, PMCNTENSET_EL0, &=, mask);
  748. kvm_pmu_reprogram_counter_mask(vcpu, mask);
  749. }
  750. int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
  751. {
  752. if (!vcpu->arch.pmu.created)
  753. return -EINVAL;
  754. /*
  755. * A valid interrupt configuration for the PMU is either to have a
  756. * properly configured interrupt number and using an in-kernel
  757. * irqchip, or to not have an in-kernel GIC and not set an IRQ.
  758. */
  759. if (irqchip_in_kernel(vcpu->kvm)) {
  760. int irq = vcpu->arch.pmu.irq_num;
  761. /*
  762. * If we are using an in-kernel vgic, at this point we know
  763. * the vgic will be initialized, so we can check the PMU irq
  764. * number against the dimensions of the vgic and make sure
  765. * it's valid.
  766. */
  767. if (!irq_is_ppi(irq) && !vgic_valid_spi(vcpu->kvm, irq))
  768. return -EINVAL;
  769. } else if (kvm_arm_pmu_irq_initialized(vcpu)) {
  770. return -EINVAL;
  771. }
  772. return 0;
  773. }
  774. static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
  775. {
  776. if (irqchip_in_kernel(vcpu->kvm)) {
  777. int ret;
  778. /*
  779. * If using the PMU with an in-kernel virtual GIC
  780. * implementation, we require the GIC to be already
  781. * initialized when initializing the PMU.
  782. */
  783. if (!vgic_initialized(vcpu->kvm))
  784. return -ENODEV;
  785. if (!kvm_arm_pmu_irq_initialized(vcpu))
  786. return -ENXIO;
  787. ret = kvm_vgic_set_owner(vcpu, vcpu->arch.pmu.irq_num,
  788. &vcpu->arch.pmu);
  789. if (ret)
  790. return ret;
  791. }
  792. init_irq_work(&vcpu->arch.pmu.overflow_work,
  793. kvm_pmu_perf_overflow_notify_vcpu);
  794. vcpu->arch.pmu.created = true;
  795. return 0;
  796. }
  797. /*
  798. * For one VM the interrupt type must be same for each vcpu.
  799. * As a PPI, the interrupt number is the same for all vcpus,
  800. * while as an SPI it must be a separate number per vcpu.
  801. */
  802. static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
  803. {
  804. unsigned long i;
  805. struct kvm_vcpu *vcpu;
  806. kvm_for_each_vcpu(i, vcpu, kvm) {
  807. if (!kvm_arm_pmu_irq_initialized(vcpu))
  808. continue;
  809. if (irq_is_ppi(irq)) {
  810. if (vcpu->arch.pmu.irq_num != irq)
  811. return false;
  812. } else {
  813. if (vcpu->arch.pmu.irq_num == irq)
  814. return false;
  815. }
  816. }
  817. return true;
  818. }
  819. /**
  820. * kvm_arm_pmu_get_max_counters - Return the max number of PMU counters.
  821. * @kvm: The kvm pointer
  822. */
  823. u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
  824. {
  825. struct arm_pmu *arm_pmu = kvm->arch.arm_pmu;
  826. /*
  827. * PMUv3 requires that all event counters are capable of counting any
  828. * event, though the same may not be true of non-PMUv3 hardware.
  829. */
  830. if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS))
  831. return 1;
  832. /*
  833. * The arm_pmu->cntr_mask considers the fixed counter(s) as well.
  834. * Ignore those and return only the general-purpose counters.
  835. */
  836. return bitmap_weight(arm_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS);
  837. }
  838. static void kvm_arm_set_nr_counters(struct kvm *kvm, unsigned int nr)
  839. {
  840. kvm->arch.nr_pmu_counters = nr;
  841. /* Reset MDCR_EL2.HPMN behind the vcpus' back... */
  842. if (test_bit(KVM_ARM_VCPU_HAS_EL2, kvm->arch.vcpu_features)) {
  843. struct kvm_vcpu *vcpu;
  844. unsigned long i;
  845. kvm_for_each_vcpu(i, vcpu, kvm) {
  846. u64 val = __vcpu_sys_reg(vcpu, MDCR_EL2);
  847. val &= ~MDCR_EL2_HPMN;
  848. val |= FIELD_PREP(MDCR_EL2_HPMN, kvm->arch.nr_pmu_counters);
  849. __vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);
  850. }
  851. }
  852. }
  853. static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
  854. {
  855. lockdep_assert_held(&kvm->arch.config_lock);
  856. kvm->arch.arm_pmu = arm_pmu;
  857. kvm_arm_set_nr_counters(kvm, kvm_arm_pmu_get_max_counters(kvm));
  858. }
  859. /**
  860. * kvm_arm_set_default_pmu - No PMU set, get the default one.
  861. * @kvm: The kvm pointer
  862. *
  863. * The observant among you will notice that the supported_cpus
  864. * mask does not get updated for the default PMU even though it
  865. * is quite possible the selected instance supports only a
  866. * subset of cores in the system. This is intentional, and
  867. * upholds the preexisting behavior on heterogeneous systems
  868. * where vCPUs can be scheduled on any core but the guest
  869. * counters could stop working.
  870. */
  871. int kvm_arm_set_default_pmu(struct kvm *kvm)
  872. {
  873. struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu();
  874. if (!arm_pmu)
  875. return -ENODEV;
  876. kvm_arm_set_pmu(kvm, arm_pmu);
  877. return 0;
  878. }
  879. static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)
  880. {
  881. struct kvm *kvm = vcpu->kvm;
  882. struct arm_pmu_entry *entry;
  883. struct arm_pmu *arm_pmu;
  884. int ret = -ENXIO;
  885. lockdep_assert_held(&kvm->arch.config_lock);
  886. mutex_lock(&arm_pmus_lock);
  887. list_for_each_entry(entry, &arm_pmus, entry) {
  888. arm_pmu = entry->arm_pmu;
  889. if (arm_pmu->pmu.type == pmu_id) {
  890. if (kvm_vm_has_ran_once(kvm) ||
  891. (kvm->arch.pmu_filter && kvm->arch.arm_pmu != arm_pmu)) {
  892. ret = -EBUSY;
  893. break;
  894. }
  895. kvm_arm_set_pmu(kvm, arm_pmu);
  896. cpumask_copy(kvm->arch.supported_cpus, &arm_pmu->supported_cpus);
  897. ret = 0;
  898. break;
  899. }
  900. }
  901. mutex_unlock(&arm_pmus_lock);
  902. return ret;
  903. }
  904. static int kvm_arm_pmu_v3_set_nr_counters(struct kvm_vcpu *vcpu, unsigned int n)
  905. {
  906. struct kvm *kvm = vcpu->kvm;
  907. if (!kvm->arch.arm_pmu)
  908. return -EINVAL;
  909. if (n > kvm_arm_pmu_get_max_counters(kvm))
  910. return -EINVAL;
  911. kvm_arm_set_nr_counters(kvm, n);
  912. return 0;
  913. }
  914. int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  915. {
  916. struct kvm *kvm = vcpu->kvm;
  917. lockdep_assert_held(&kvm->arch.config_lock);
  918. if (!kvm_vcpu_has_pmu(vcpu))
  919. return -ENODEV;
  920. if (vcpu->arch.pmu.created)
  921. return -EBUSY;
  922. switch (attr->attr) {
  923. case KVM_ARM_VCPU_PMU_V3_IRQ: {
  924. int __user *uaddr = (int __user *)(long)attr->addr;
  925. int irq;
  926. if (!irqchip_in_kernel(kvm))
  927. return -EINVAL;
  928. if (get_user(irq, uaddr))
  929. return -EFAULT;
  930. /* The PMU overflow interrupt can be a PPI or a valid SPI. */
  931. if (!(irq_is_ppi(irq) || irq_is_spi(irq)))
  932. return -EINVAL;
  933. if (!pmu_irq_is_valid(kvm, irq))
  934. return -EINVAL;
  935. if (kvm_arm_pmu_irq_initialized(vcpu))
  936. return -EBUSY;
  937. kvm_debug("Set kvm ARM PMU irq: %d\n", irq);
  938. vcpu->arch.pmu.irq_num = irq;
  939. return 0;
  940. }
  941. case KVM_ARM_VCPU_PMU_V3_FILTER: {
  942. u8 pmuver = kvm_arm_pmu_get_pmuver_limit();
  943. struct kvm_pmu_event_filter __user *uaddr;
  944. struct kvm_pmu_event_filter filter;
  945. int nr_events;
  946. /*
  947. * Allow userspace to specify an event filter for the entire
  948. * event range supported by PMUVer of the hardware, rather
  949. * than the guest's PMUVer for KVM backward compatibility.
  950. */
  951. nr_events = __kvm_pmu_event_mask(pmuver) + 1;
  952. uaddr = (struct kvm_pmu_event_filter __user *)(long)attr->addr;
  953. if (copy_from_user(&filter, uaddr, sizeof(filter)))
  954. return -EFAULT;
  955. if (((u32)filter.base_event + filter.nevents) > nr_events ||
  956. (filter.action != KVM_PMU_EVENT_ALLOW &&
  957. filter.action != KVM_PMU_EVENT_DENY))
  958. return -EINVAL;
  959. if (kvm_vm_has_ran_once(kvm))
  960. return -EBUSY;
  961. if (!kvm->arch.pmu_filter) {
  962. kvm->arch.pmu_filter = bitmap_alloc(nr_events, GFP_KERNEL_ACCOUNT);
  963. if (!kvm->arch.pmu_filter)
  964. return -ENOMEM;
  965. /*
  966. * The default depends on the first applied filter.
  967. * If it allows events, the default is to deny.
  968. * Conversely, if the first filter denies a set of
  969. * events, the default is to allow.
  970. */
  971. if (filter.action == KVM_PMU_EVENT_ALLOW)
  972. bitmap_zero(kvm->arch.pmu_filter, nr_events);
  973. else
  974. bitmap_fill(kvm->arch.pmu_filter, nr_events);
  975. }
  976. if (filter.action == KVM_PMU_EVENT_ALLOW)
  977. bitmap_set(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
  978. else
  979. bitmap_clear(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
  980. return 0;
  981. }
  982. case KVM_ARM_VCPU_PMU_V3_SET_PMU: {
  983. int __user *uaddr = (int __user *)(long)attr->addr;
  984. int pmu_id;
  985. if (get_user(pmu_id, uaddr))
  986. return -EFAULT;
  987. return kvm_arm_pmu_v3_set_pmu(vcpu, pmu_id);
  988. }
  989. case KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS: {
  990. unsigned int __user *uaddr = (unsigned int __user *)(long)attr->addr;
  991. unsigned int n;
  992. if (get_user(n, uaddr))
  993. return -EFAULT;
  994. return kvm_arm_pmu_v3_set_nr_counters(vcpu, n);
  995. }
  996. case KVM_ARM_VCPU_PMU_V3_INIT:
  997. return kvm_arm_pmu_v3_init(vcpu);
  998. }
  999. return -ENXIO;
  1000. }
  1001. int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  1002. {
  1003. switch (attr->attr) {
  1004. case KVM_ARM_VCPU_PMU_V3_IRQ: {
  1005. int __user *uaddr = (int __user *)(long)attr->addr;
  1006. int irq;
  1007. if (!irqchip_in_kernel(vcpu->kvm))
  1008. return -EINVAL;
  1009. if (!kvm_vcpu_has_pmu(vcpu))
  1010. return -ENODEV;
  1011. if (!kvm_arm_pmu_irq_initialized(vcpu))
  1012. return -ENXIO;
  1013. irq = vcpu->arch.pmu.irq_num;
  1014. return put_user(irq, uaddr);
  1015. }
  1016. }
  1017. return -ENXIO;
  1018. }
  1019. int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  1020. {
  1021. switch (attr->attr) {
  1022. case KVM_ARM_VCPU_PMU_V3_IRQ:
  1023. case KVM_ARM_VCPU_PMU_V3_INIT:
  1024. case KVM_ARM_VCPU_PMU_V3_FILTER:
  1025. case KVM_ARM_VCPU_PMU_V3_SET_PMU:
  1026. case KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS:
  1027. if (kvm_vcpu_has_pmu(vcpu))
  1028. return 0;
  1029. }
  1030. return -ENXIO;
  1031. }
  1032. u8 kvm_arm_pmu_get_pmuver_limit(void)
  1033. {
  1034. unsigned int pmuver;
  1035. pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer,
  1036. read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1));
  1037. /*
  1038. * Spoof a barebones PMUv3 implementation if the system supports IMPDEF
  1039. * traps of the PMUv3 sysregs
  1040. */
  1041. if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS))
  1042. return ID_AA64DFR0_EL1_PMUVer_IMP;
  1043. /*
  1044. * Otherwise, treat IMPLEMENTATION DEFINED functionality as
  1045. * unimplemented
  1046. */
  1047. if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF)
  1048. return 0;
  1049. return min(pmuver, ID_AA64DFR0_EL1_PMUVer_V3P5);
  1050. }
  1051. /**
  1052. * kvm_vcpu_read_pmcr - Read PMCR_EL0 register for the vCPU
  1053. * @vcpu: The vcpu pointer
  1054. */
  1055. u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
  1056. {
  1057. u64 pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
  1058. u64 n = vcpu->kvm->arch.nr_pmu_counters;
  1059. if (vcpu_has_nv(vcpu) && !vcpu_is_el2(vcpu))
  1060. n = FIELD_GET(MDCR_EL2_HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
  1061. return u64_replace_bits(pmcr, n, ARMV8_PMU_PMCR_N);
  1062. }
  1063. void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu)
  1064. {
  1065. bool reprogrammed = false;
  1066. unsigned long mask;
  1067. int i;
  1068. mask = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
  1069. for_each_set_bit(i, &mask, 32) {
  1070. struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
  1071. /*
  1072. * We only need to reconfigure events where the filter is
  1073. * different at EL1 vs. EL2, as we're multiplexing the true EL1
  1074. * event filter bit for nested.
  1075. */
  1076. if (kvm_pmc_counts_at_el1(pmc) == kvm_pmc_counts_at_el2(pmc))
  1077. continue;
  1078. kvm_pmu_create_perf_event(pmc);
  1079. reprogrammed = true;
  1080. }
  1081. if (reprogrammed)
  1082. kvm_vcpu_pmu_restore_guest(vcpu);
  1083. }