pmu-emul.c 28 KB

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