arch_timer.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. */
  6. #include <linux/cpu.h>
  7. #include <linux/kvm.h>
  8. #include <linux/kvm_host.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/uaccess.h>
  13. #include <clocksource/arm_arch_timer.h>
  14. #include <asm/arch_timer.h>
  15. #include <asm/kvm_emulate.h>
  16. #include <asm/kvm_hyp.h>
  17. #include <asm/kvm_nested.h>
  18. #include <kvm/arm_vgic.h>
  19. #include <kvm/arm_arch_timer.h>
  20. #include "trace.h"
  21. static struct timecounter *timecounter;
  22. static unsigned int host_vtimer_irq;
  23. static unsigned int host_ptimer_irq;
  24. static u32 host_vtimer_irq_flags;
  25. static u32 host_ptimer_irq_flags;
  26. static DEFINE_STATIC_KEY_FALSE(has_gic_active_state);
  27. static const u8 default_ppi[] = {
  28. [TIMER_PTIMER] = 30,
  29. [TIMER_VTIMER] = 27,
  30. [TIMER_HPTIMER] = 26,
  31. [TIMER_HVTIMER] = 28,
  32. };
  33. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx);
  34. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  35. struct arch_timer_context *timer_ctx);
  36. static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
  37. static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
  38. struct arch_timer_context *timer,
  39. enum kvm_arch_timer_regs treg,
  40. u64 val);
  41. static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
  42. struct arch_timer_context *timer,
  43. enum kvm_arch_timer_regs treg);
  44. static bool kvm_arch_timer_get_input_level(int vintid);
  45. static struct irq_ops arch_timer_irq_ops = {
  46. .get_input_level = kvm_arch_timer_get_input_level,
  47. };
  48. static int nr_timers(struct kvm_vcpu *vcpu)
  49. {
  50. if (!vcpu_has_nv(vcpu))
  51. return NR_KVM_EL0_TIMERS;
  52. return NR_KVM_TIMERS;
  53. }
  54. u32 timer_get_ctl(struct arch_timer_context *ctxt)
  55. {
  56. struct kvm_vcpu *vcpu = ctxt->vcpu;
  57. switch(arch_timer_ctx_index(ctxt)) {
  58. case TIMER_VTIMER:
  59. return __vcpu_sys_reg(vcpu, CNTV_CTL_EL0);
  60. case TIMER_PTIMER:
  61. return __vcpu_sys_reg(vcpu, CNTP_CTL_EL0);
  62. case TIMER_HVTIMER:
  63. return __vcpu_sys_reg(vcpu, CNTHV_CTL_EL2);
  64. case TIMER_HPTIMER:
  65. return __vcpu_sys_reg(vcpu, CNTHP_CTL_EL2);
  66. default:
  67. WARN_ON(1);
  68. return 0;
  69. }
  70. }
  71. u64 timer_get_cval(struct arch_timer_context *ctxt)
  72. {
  73. struct kvm_vcpu *vcpu = ctxt->vcpu;
  74. switch(arch_timer_ctx_index(ctxt)) {
  75. case TIMER_VTIMER:
  76. return __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0);
  77. case TIMER_PTIMER:
  78. return __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
  79. case TIMER_HVTIMER:
  80. return __vcpu_sys_reg(vcpu, CNTHV_CVAL_EL2);
  81. case TIMER_HPTIMER:
  82. return __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2);
  83. default:
  84. WARN_ON(1);
  85. return 0;
  86. }
  87. }
  88. static u64 timer_get_offset(struct arch_timer_context *ctxt)
  89. {
  90. u64 offset = 0;
  91. if (!ctxt)
  92. return 0;
  93. if (ctxt->offset.vm_offset)
  94. offset += *ctxt->offset.vm_offset;
  95. if (ctxt->offset.vcpu_offset)
  96. offset += *ctxt->offset.vcpu_offset;
  97. return offset;
  98. }
  99. static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl)
  100. {
  101. struct kvm_vcpu *vcpu = ctxt->vcpu;
  102. switch(arch_timer_ctx_index(ctxt)) {
  103. case TIMER_VTIMER:
  104. __vcpu_sys_reg(vcpu, CNTV_CTL_EL0) = ctl;
  105. break;
  106. case TIMER_PTIMER:
  107. __vcpu_sys_reg(vcpu, CNTP_CTL_EL0) = ctl;
  108. break;
  109. case TIMER_HVTIMER:
  110. __vcpu_sys_reg(vcpu, CNTHV_CTL_EL2) = ctl;
  111. break;
  112. case TIMER_HPTIMER:
  113. __vcpu_sys_reg(vcpu, CNTHP_CTL_EL2) = ctl;
  114. break;
  115. default:
  116. WARN_ON(1);
  117. }
  118. }
  119. static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)
  120. {
  121. struct kvm_vcpu *vcpu = ctxt->vcpu;
  122. switch(arch_timer_ctx_index(ctxt)) {
  123. case TIMER_VTIMER:
  124. __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0) = cval;
  125. break;
  126. case TIMER_PTIMER:
  127. __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = cval;
  128. break;
  129. case TIMER_HVTIMER:
  130. __vcpu_sys_reg(vcpu, CNTHV_CVAL_EL2) = cval;
  131. break;
  132. case TIMER_HPTIMER:
  133. __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2) = cval;
  134. break;
  135. default:
  136. WARN_ON(1);
  137. }
  138. }
  139. static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset)
  140. {
  141. if (!ctxt->offset.vm_offset) {
  142. WARN(offset, "timer %ld\n", arch_timer_ctx_index(ctxt));
  143. return;
  144. }
  145. WRITE_ONCE(*ctxt->offset.vm_offset, offset);
  146. }
  147. u64 kvm_phys_timer_read(void)
  148. {
  149. return timecounter->cc->read(timecounter->cc);
  150. }
  151. void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
  152. {
  153. if (vcpu_has_nv(vcpu)) {
  154. if (is_hyp_ctxt(vcpu)) {
  155. map->direct_vtimer = vcpu_hvtimer(vcpu);
  156. map->direct_ptimer = vcpu_hptimer(vcpu);
  157. map->emul_vtimer = vcpu_vtimer(vcpu);
  158. map->emul_ptimer = vcpu_ptimer(vcpu);
  159. } else {
  160. map->direct_vtimer = vcpu_vtimer(vcpu);
  161. map->direct_ptimer = vcpu_ptimer(vcpu);
  162. map->emul_vtimer = vcpu_hvtimer(vcpu);
  163. map->emul_ptimer = vcpu_hptimer(vcpu);
  164. }
  165. } else if (has_vhe()) {
  166. map->direct_vtimer = vcpu_vtimer(vcpu);
  167. map->direct_ptimer = vcpu_ptimer(vcpu);
  168. map->emul_vtimer = NULL;
  169. map->emul_ptimer = NULL;
  170. } else {
  171. map->direct_vtimer = vcpu_vtimer(vcpu);
  172. map->direct_ptimer = NULL;
  173. map->emul_vtimer = NULL;
  174. map->emul_ptimer = vcpu_ptimer(vcpu);
  175. }
  176. trace_kvm_get_timer_map(vcpu->vcpu_id, map);
  177. }
  178. static inline bool userspace_irqchip(struct kvm *kvm)
  179. {
  180. return unlikely(!irqchip_in_kernel(kvm));
  181. }
  182. static void soft_timer_start(struct hrtimer *hrt, u64 ns)
  183. {
  184. hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
  185. HRTIMER_MODE_ABS_HARD);
  186. }
  187. static void soft_timer_cancel(struct hrtimer *hrt)
  188. {
  189. hrtimer_cancel(hrt);
  190. }
  191. static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
  192. {
  193. struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
  194. struct arch_timer_context *ctx;
  195. struct timer_map map;
  196. /*
  197. * We may see a timer interrupt after vcpu_put() has been called which
  198. * sets the CPU's vcpu pointer to NULL, because even though the timer
  199. * has been disabled in timer_save_state(), the hardware interrupt
  200. * signal may not have been retired from the interrupt controller yet.
  201. */
  202. if (!vcpu)
  203. return IRQ_HANDLED;
  204. get_timer_map(vcpu, &map);
  205. if (irq == host_vtimer_irq)
  206. ctx = map.direct_vtimer;
  207. else
  208. ctx = map.direct_ptimer;
  209. if (kvm_timer_should_fire(ctx))
  210. kvm_timer_update_irq(vcpu, true, ctx);
  211. if (userspace_irqchip(vcpu->kvm) &&
  212. !static_branch_unlikely(&has_gic_active_state))
  213. disable_percpu_irq(host_vtimer_irq);
  214. return IRQ_HANDLED;
  215. }
  216. static u64 kvm_counter_compute_delta(struct arch_timer_context *timer_ctx,
  217. u64 val)
  218. {
  219. u64 now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
  220. if (now < val) {
  221. u64 ns;
  222. ns = cyclecounter_cyc2ns(timecounter->cc,
  223. val - now,
  224. timecounter->mask,
  225. &timer_ctx->ns_frac);
  226. return ns;
  227. }
  228. return 0;
  229. }
  230. static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
  231. {
  232. return kvm_counter_compute_delta(timer_ctx, timer_get_cval(timer_ctx));
  233. }
  234. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
  235. {
  236. WARN_ON(timer_ctx && timer_ctx->loaded);
  237. return timer_ctx &&
  238. ((timer_get_ctl(timer_ctx) &
  239. (ARCH_TIMER_CTRL_IT_MASK | ARCH_TIMER_CTRL_ENABLE)) == ARCH_TIMER_CTRL_ENABLE);
  240. }
  241. static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu)
  242. {
  243. return (cpus_have_final_cap(ARM64_HAS_WFXT) &&
  244. vcpu_get_flag(vcpu, IN_WFIT));
  245. }
  246. static u64 wfit_delay_ns(struct kvm_vcpu *vcpu)
  247. {
  248. u64 val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
  249. struct arch_timer_context *ctx;
  250. ctx = is_hyp_ctxt(vcpu) ? vcpu_hvtimer(vcpu) : vcpu_vtimer(vcpu);
  251. return kvm_counter_compute_delta(ctx, val);
  252. }
  253. /*
  254. * Returns the earliest expiration time in ns among guest timers.
  255. * Note that it will return 0 if none of timers can fire.
  256. */
  257. static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
  258. {
  259. u64 min_delta = ULLONG_MAX;
  260. int i;
  261. for (i = 0; i < nr_timers(vcpu); i++) {
  262. struct arch_timer_context *ctx = &vcpu->arch.timer_cpu.timers[i];
  263. WARN(ctx->loaded, "timer %d loaded\n", i);
  264. if (kvm_timer_irq_can_fire(ctx))
  265. min_delta = min(min_delta, kvm_timer_compute_delta(ctx));
  266. }
  267. if (vcpu_has_wfit_active(vcpu))
  268. min_delta = min(min_delta, wfit_delay_ns(vcpu));
  269. /* If none of timers can fire, then return 0 */
  270. if (min_delta == ULLONG_MAX)
  271. return 0;
  272. return min_delta;
  273. }
  274. static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
  275. {
  276. struct arch_timer_cpu *timer;
  277. struct kvm_vcpu *vcpu;
  278. u64 ns;
  279. timer = container_of(hrt, struct arch_timer_cpu, bg_timer);
  280. vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
  281. /*
  282. * Check that the timer has really expired from the guest's
  283. * PoV (NTP on the host may have forced it to expire
  284. * early). If we should have slept longer, restart it.
  285. */
  286. ns = kvm_timer_earliest_exp(vcpu);
  287. if (unlikely(ns)) {
  288. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  289. return HRTIMER_RESTART;
  290. }
  291. kvm_vcpu_wake_up(vcpu);
  292. return HRTIMER_NORESTART;
  293. }
  294. static enum hrtimer_restart kvm_hrtimer_expire(struct hrtimer *hrt)
  295. {
  296. struct arch_timer_context *ctx;
  297. struct kvm_vcpu *vcpu;
  298. u64 ns;
  299. ctx = container_of(hrt, struct arch_timer_context, hrtimer);
  300. vcpu = ctx->vcpu;
  301. trace_kvm_timer_hrtimer_expire(ctx);
  302. /*
  303. * Check that the timer has really expired from the guest's
  304. * PoV (NTP on the host may have forced it to expire
  305. * early). If not ready, schedule for a later time.
  306. */
  307. ns = kvm_timer_compute_delta(ctx);
  308. if (unlikely(ns)) {
  309. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  310. return HRTIMER_RESTART;
  311. }
  312. kvm_timer_update_irq(vcpu, true, ctx);
  313. return HRTIMER_NORESTART;
  314. }
  315. static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
  316. {
  317. enum kvm_arch_timers index;
  318. u64 cval, now;
  319. if (!timer_ctx)
  320. return false;
  321. index = arch_timer_ctx_index(timer_ctx);
  322. if (timer_ctx->loaded) {
  323. u32 cnt_ctl = 0;
  324. switch (index) {
  325. case TIMER_VTIMER:
  326. case TIMER_HVTIMER:
  327. cnt_ctl = read_sysreg_el0(SYS_CNTV_CTL);
  328. break;
  329. case TIMER_PTIMER:
  330. case TIMER_HPTIMER:
  331. cnt_ctl = read_sysreg_el0(SYS_CNTP_CTL);
  332. break;
  333. case NR_KVM_TIMERS:
  334. /* GCC is braindead */
  335. cnt_ctl = 0;
  336. break;
  337. }
  338. return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) &&
  339. (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) &&
  340. !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK);
  341. }
  342. if (!kvm_timer_irq_can_fire(timer_ctx))
  343. return false;
  344. cval = timer_get_cval(timer_ctx);
  345. now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
  346. return cval <= now;
  347. }
  348. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  349. {
  350. return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) == 0;
  351. }
  352. /*
  353. * Reflect the timer output level into the kvm_run structure
  354. */
  355. void kvm_timer_update_run(struct kvm_vcpu *vcpu)
  356. {
  357. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  358. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  359. struct kvm_sync_regs *regs = &vcpu->run->s.regs;
  360. /* Populate the device bitmap with the timer states */
  361. regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
  362. KVM_ARM_DEV_EL1_PTIMER);
  363. if (kvm_timer_should_fire(vtimer))
  364. regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
  365. if (kvm_timer_should_fire(ptimer))
  366. regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
  367. }
  368. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  369. struct arch_timer_context *timer_ctx)
  370. {
  371. int ret;
  372. timer_ctx->irq.level = new_level;
  373. trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_irq(timer_ctx),
  374. timer_ctx->irq.level);
  375. if (!userspace_irqchip(vcpu->kvm)) {
  376. ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu,
  377. timer_irq(timer_ctx),
  378. timer_ctx->irq.level,
  379. timer_ctx);
  380. WARN_ON(ret);
  381. }
  382. }
  383. /* Only called for a fully emulated timer */
  384. static void timer_emulate(struct arch_timer_context *ctx)
  385. {
  386. bool should_fire = kvm_timer_should_fire(ctx);
  387. trace_kvm_timer_emulate(ctx, should_fire);
  388. if (should_fire != ctx->irq.level)
  389. kvm_timer_update_irq(ctx->vcpu, should_fire, ctx);
  390. /*
  391. * If the timer can fire now, we don't need to have a soft timer
  392. * scheduled for the future. If the timer cannot fire at all,
  393. * then we also don't need a soft timer.
  394. */
  395. if (should_fire || !kvm_timer_irq_can_fire(ctx))
  396. return;
  397. soft_timer_start(&ctx->hrtimer, kvm_timer_compute_delta(ctx));
  398. }
  399. static void set_cntvoff(u64 cntvoff)
  400. {
  401. kvm_call_hyp(__kvm_timer_set_cntvoff, cntvoff);
  402. }
  403. static void set_cntpoff(u64 cntpoff)
  404. {
  405. if (has_cntpoff())
  406. write_sysreg_s(cntpoff, SYS_CNTPOFF_EL2);
  407. }
  408. static void timer_save_state(struct arch_timer_context *ctx)
  409. {
  410. struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
  411. enum kvm_arch_timers index = arch_timer_ctx_index(ctx);
  412. unsigned long flags;
  413. if (!timer->enabled)
  414. return;
  415. local_irq_save(flags);
  416. if (!ctx->loaded)
  417. goto out;
  418. switch (index) {
  419. u64 cval;
  420. case TIMER_VTIMER:
  421. case TIMER_HVTIMER:
  422. timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTV_CTL));
  423. timer_set_cval(ctx, read_sysreg_el0(SYS_CNTV_CVAL));
  424. /* Disable the timer */
  425. write_sysreg_el0(0, SYS_CNTV_CTL);
  426. isb();
  427. /*
  428. * The kernel may decide to run userspace after
  429. * calling vcpu_put, so we reset cntvoff to 0 to
  430. * ensure a consistent read between user accesses to
  431. * the virtual counter and kernel access to the
  432. * physical counter of non-VHE case.
  433. *
  434. * For VHE, the virtual counter uses a fixed virtual
  435. * offset of zero, so no need to zero CNTVOFF_EL2
  436. * register, but this is actually useful when switching
  437. * between EL1/vEL2 with NV.
  438. *
  439. * Do it unconditionally, as this is either unavoidable
  440. * or dirt cheap.
  441. */
  442. set_cntvoff(0);
  443. break;
  444. case TIMER_PTIMER:
  445. case TIMER_HPTIMER:
  446. timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTP_CTL));
  447. cval = read_sysreg_el0(SYS_CNTP_CVAL);
  448. cval -= timer_get_offset(ctx);
  449. timer_set_cval(ctx, cval);
  450. /* Disable the timer */
  451. write_sysreg_el0(0, SYS_CNTP_CTL);
  452. isb();
  453. set_cntpoff(0);
  454. break;
  455. case NR_KVM_TIMERS:
  456. BUG();
  457. }
  458. trace_kvm_timer_save_state(ctx);
  459. ctx->loaded = false;
  460. out:
  461. local_irq_restore(flags);
  462. }
  463. /*
  464. * Schedule the background timer before calling kvm_vcpu_halt, so that this
  465. * thread is removed from its waitqueue and made runnable when there's a timer
  466. * interrupt to handle.
  467. */
  468. static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
  469. {
  470. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  471. struct timer_map map;
  472. get_timer_map(vcpu, &map);
  473. /*
  474. * If no timers are capable of raising interrupts (disabled or
  475. * masked), then there's no more work for us to do.
  476. */
  477. if (!kvm_timer_irq_can_fire(map.direct_vtimer) &&
  478. !kvm_timer_irq_can_fire(map.direct_ptimer) &&
  479. !kvm_timer_irq_can_fire(map.emul_vtimer) &&
  480. !kvm_timer_irq_can_fire(map.emul_ptimer) &&
  481. !vcpu_has_wfit_active(vcpu))
  482. return;
  483. /*
  484. * At least one guest time will expire. Schedule a background timer.
  485. * Set the earliest expiration time among the guest timers.
  486. */
  487. soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
  488. }
  489. static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
  490. {
  491. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  492. soft_timer_cancel(&timer->bg_timer);
  493. }
  494. static void timer_restore_state(struct arch_timer_context *ctx)
  495. {
  496. struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
  497. enum kvm_arch_timers index = arch_timer_ctx_index(ctx);
  498. unsigned long flags;
  499. if (!timer->enabled)
  500. return;
  501. local_irq_save(flags);
  502. if (ctx->loaded)
  503. goto out;
  504. switch (index) {
  505. u64 cval, offset;
  506. case TIMER_VTIMER:
  507. case TIMER_HVTIMER:
  508. set_cntvoff(timer_get_offset(ctx));
  509. write_sysreg_el0(timer_get_cval(ctx), SYS_CNTV_CVAL);
  510. isb();
  511. write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTV_CTL);
  512. break;
  513. case TIMER_PTIMER:
  514. case TIMER_HPTIMER:
  515. cval = timer_get_cval(ctx);
  516. offset = timer_get_offset(ctx);
  517. set_cntpoff(offset);
  518. cval += offset;
  519. write_sysreg_el0(cval, SYS_CNTP_CVAL);
  520. isb();
  521. write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTP_CTL);
  522. break;
  523. case NR_KVM_TIMERS:
  524. BUG();
  525. }
  526. trace_kvm_timer_restore_state(ctx);
  527. ctx->loaded = true;
  528. out:
  529. local_irq_restore(flags);
  530. }
  531. static inline void set_timer_irq_phys_active(struct arch_timer_context *ctx, bool active)
  532. {
  533. int r;
  534. r = irq_set_irqchip_state(ctx->host_timer_irq, IRQCHIP_STATE_ACTIVE, active);
  535. WARN_ON(r);
  536. }
  537. static void kvm_timer_vcpu_load_gic(struct arch_timer_context *ctx)
  538. {
  539. struct kvm_vcpu *vcpu = ctx->vcpu;
  540. bool phys_active = false;
  541. /*
  542. * Update the timer output so that it is likely to match the
  543. * state we're about to restore. If the timer expires between
  544. * this point and the register restoration, we'll take the
  545. * interrupt anyway.
  546. */
  547. kvm_timer_update_irq(ctx->vcpu, kvm_timer_should_fire(ctx), ctx);
  548. if (irqchip_in_kernel(vcpu->kvm))
  549. phys_active = kvm_vgic_map_is_active(vcpu, timer_irq(ctx));
  550. phys_active |= ctx->irq.level;
  551. set_timer_irq_phys_active(ctx, phys_active);
  552. }
  553. static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu)
  554. {
  555. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  556. /*
  557. * Update the timer output so that it is likely to match the
  558. * state we're about to restore. If the timer expires between
  559. * this point and the register restoration, we'll take the
  560. * interrupt anyway.
  561. */
  562. kvm_timer_update_irq(vcpu, kvm_timer_should_fire(vtimer), vtimer);
  563. /*
  564. * When using a userspace irqchip with the architected timers and a
  565. * host interrupt controller that doesn't support an active state, we
  566. * must still prevent continuously exiting from the guest, and
  567. * therefore mask the physical interrupt by disabling it on the host
  568. * interrupt controller when the virtual level is high, such that the
  569. * guest can make forward progress. Once we detect the output level
  570. * being de-asserted, we unmask the interrupt again so that we exit
  571. * from the guest when the timer fires.
  572. */
  573. if (vtimer->irq.level)
  574. disable_percpu_irq(host_vtimer_irq);
  575. else
  576. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  577. }
  578. /* If _pred is true, set bit in _set, otherwise set it in _clr */
  579. #define assign_clear_set_bit(_pred, _bit, _clr, _set) \
  580. do { \
  581. if (_pred) \
  582. (_set) |= (_bit); \
  583. else \
  584. (_clr) |= (_bit); \
  585. } while (0)
  586. static void kvm_timer_vcpu_load_nested_switch(struct kvm_vcpu *vcpu,
  587. struct timer_map *map)
  588. {
  589. int hw, ret;
  590. if (!irqchip_in_kernel(vcpu->kvm))
  591. return;
  592. /*
  593. * We only ever unmap the vtimer irq on a VHE system that runs nested
  594. * virtualization, in which case we have both a valid emul_vtimer,
  595. * emul_ptimer, direct_vtimer, and direct_ptimer.
  596. *
  597. * Since this is called from kvm_timer_vcpu_load(), a change between
  598. * vEL2 and vEL1/0 will have just happened, and the timer_map will
  599. * represent this, and therefore we switch the emul/direct mappings
  600. * below.
  601. */
  602. hw = kvm_vgic_get_map(vcpu, timer_irq(map->direct_vtimer));
  603. if (hw < 0) {
  604. kvm_vgic_unmap_phys_irq(vcpu, timer_irq(map->emul_vtimer));
  605. kvm_vgic_unmap_phys_irq(vcpu, timer_irq(map->emul_ptimer));
  606. ret = kvm_vgic_map_phys_irq(vcpu,
  607. map->direct_vtimer->host_timer_irq,
  608. timer_irq(map->direct_vtimer),
  609. &arch_timer_irq_ops);
  610. WARN_ON_ONCE(ret);
  611. ret = kvm_vgic_map_phys_irq(vcpu,
  612. map->direct_ptimer->host_timer_irq,
  613. timer_irq(map->direct_ptimer),
  614. &arch_timer_irq_ops);
  615. WARN_ON_ONCE(ret);
  616. /*
  617. * The virtual offset behaviour is "interesting", as it
  618. * always applies when HCR_EL2.E2H==0, but only when
  619. * accessed from EL1 when HCR_EL2.E2H==1. So make sure we
  620. * track E2H when putting the HV timer in "direct" mode.
  621. */
  622. if (map->direct_vtimer == vcpu_hvtimer(vcpu)) {
  623. struct arch_timer_offset *offs = &map->direct_vtimer->offset;
  624. if (vcpu_el2_e2h_is_set(vcpu))
  625. offs->vcpu_offset = NULL;
  626. else
  627. offs->vcpu_offset = &__vcpu_sys_reg(vcpu, CNTVOFF_EL2);
  628. }
  629. }
  630. }
  631. static void timer_set_traps(struct kvm_vcpu *vcpu, struct timer_map *map)
  632. {
  633. bool tpt, tpc;
  634. u64 clr, set;
  635. /*
  636. * No trapping gets configured here with nVHE. See
  637. * __timer_enable_traps(), which is where the stuff happens.
  638. */
  639. if (!has_vhe())
  640. return;
  641. /*
  642. * Our default policy is not to trap anything. As we progress
  643. * within this function, reality kicks in and we start adding
  644. * traps based on emulation requirements.
  645. */
  646. tpt = tpc = false;
  647. /*
  648. * We have two possibility to deal with a physical offset:
  649. *
  650. * - Either we have CNTPOFF (yay!) or the offset is 0:
  651. * we let the guest freely access the HW
  652. *
  653. * - or neither of these condition apply:
  654. * we trap accesses to the HW, but still use it
  655. * after correcting the physical offset
  656. */
  657. if (!has_cntpoff() && timer_get_offset(map->direct_ptimer))
  658. tpt = tpc = true;
  659. /*
  660. * Apply the enable bits that the guest hypervisor has requested for
  661. * its own guest. We can only add traps that wouldn't have been set
  662. * above.
  663. */
  664. if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
  665. u64 val = __vcpu_sys_reg(vcpu, CNTHCTL_EL2);
  666. /* Use the VHE format for mental sanity */
  667. if (!vcpu_el2_e2h_is_set(vcpu))
  668. val = (val & (CNTHCTL_EL1PCEN | CNTHCTL_EL1PCTEN)) << 10;
  669. tpt |= !(val & (CNTHCTL_EL1PCEN << 10));
  670. tpc |= !(val & (CNTHCTL_EL1PCTEN << 10));
  671. }
  672. /*
  673. * Now that we have collected our requirements, compute the
  674. * trap and enable bits.
  675. */
  676. set = 0;
  677. clr = 0;
  678. assign_clear_set_bit(tpt, CNTHCTL_EL1PCEN << 10, set, clr);
  679. assign_clear_set_bit(tpc, CNTHCTL_EL1PCTEN << 10, set, clr);
  680. /* This only happens on VHE, so use the CNTHCTL_EL2 accessor. */
  681. sysreg_clear_set(cnthctl_el2, clr, set);
  682. }
  683. void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
  684. {
  685. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  686. struct timer_map map;
  687. if (unlikely(!timer->enabled))
  688. return;
  689. get_timer_map(vcpu, &map);
  690. if (static_branch_likely(&has_gic_active_state)) {
  691. if (vcpu_has_nv(vcpu))
  692. kvm_timer_vcpu_load_nested_switch(vcpu, &map);
  693. kvm_timer_vcpu_load_gic(map.direct_vtimer);
  694. if (map.direct_ptimer)
  695. kvm_timer_vcpu_load_gic(map.direct_ptimer);
  696. } else {
  697. kvm_timer_vcpu_load_nogic(vcpu);
  698. }
  699. kvm_timer_unblocking(vcpu);
  700. timer_restore_state(map.direct_vtimer);
  701. if (map.direct_ptimer)
  702. timer_restore_state(map.direct_ptimer);
  703. if (map.emul_vtimer)
  704. timer_emulate(map.emul_vtimer);
  705. if (map.emul_ptimer)
  706. timer_emulate(map.emul_ptimer);
  707. timer_set_traps(vcpu, &map);
  708. }
  709. bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
  710. {
  711. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  712. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  713. struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
  714. bool vlevel, plevel;
  715. if (likely(irqchip_in_kernel(vcpu->kvm)))
  716. return false;
  717. vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
  718. plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
  719. return kvm_timer_should_fire(vtimer) != vlevel ||
  720. kvm_timer_should_fire(ptimer) != plevel;
  721. }
  722. void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
  723. {
  724. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  725. struct timer_map map;
  726. if (unlikely(!timer->enabled))
  727. return;
  728. get_timer_map(vcpu, &map);
  729. timer_save_state(map.direct_vtimer);
  730. if (map.direct_ptimer)
  731. timer_save_state(map.direct_ptimer);
  732. /*
  733. * Cancel soft timer emulation, because the only case where we
  734. * need it after a vcpu_put is in the context of a sleeping VCPU, and
  735. * in that case we already factor in the deadline for the physical
  736. * timer when scheduling the bg_timer.
  737. *
  738. * In any case, we re-schedule the hrtimer for the physical timer when
  739. * coming back to the VCPU thread in kvm_timer_vcpu_load().
  740. */
  741. if (map.emul_vtimer)
  742. soft_timer_cancel(&map.emul_vtimer->hrtimer);
  743. if (map.emul_ptimer)
  744. soft_timer_cancel(&map.emul_ptimer->hrtimer);
  745. if (kvm_vcpu_is_blocking(vcpu))
  746. kvm_timer_blocking(vcpu);
  747. }
  748. /*
  749. * With a userspace irqchip we have to check if the guest de-asserted the
  750. * timer and if so, unmask the timer irq signal on the host interrupt
  751. * controller to ensure that we see future timer signals.
  752. */
  753. static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
  754. {
  755. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  756. if (!kvm_timer_should_fire(vtimer)) {
  757. kvm_timer_update_irq(vcpu, false, vtimer);
  758. if (static_branch_likely(&has_gic_active_state))
  759. set_timer_irq_phys_active(vtimer, false);
  760. else
  761. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  762. }
  763. }
  764. void kvm_timer_sync_user(struct kvm_vcpu *vcpu)
  765. {
  766. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  767. if (unlikely(!timer->enabled))
  768. return;
  769. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  770. unmask_vtimer_irq_user(vcpu);
  771. }
  772. void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
  773. {
  774. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  775. struct timer_map map;
  776. get_timer_map(vcpu, &map);
  777. /*
  778. * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
  779. * and to 0 for ARMv7. We provide an implementation that always
  780. * resets the timer to be disabled and unmasked and is compliant with
  781. * the ARMv7 architecture.
  782. */
  783. for (int i = 0; i < nr_timers(vcpu); i++)
  784. timer_set_ctl(vcpu_get_timer(vcpu, i), 0);
  785. /*
  786. * A vcpu running at EL2 is in charge of the offset applied to
  787. * the virtual timer, so use the physical VM offset, and point
  788. * the vcpu offset to CNTVOFF_EL2.
  789. */
  790. if (vcpu_has_nv(vcpu)) {
  791. struct arch_timer_offset *offs = &vcpu_vtimer(vcpu)->offset;
  792. offs->vcpu_offset = &__vcpu_sys_reg(vcpu, CNTVOFF_EL2);
  793. offs->vm_offset = &vcpu->kvm->arch.timer_data.poffset;
  794. }
  795. if (timer->enabled) {
  796. for (int i = 0; i < nr_timers(vcpu); i++)
  797. kvm_timer_update_irq(vcpu, false,
  798. vcpu_get_timer(vcpu, i));
  799. if (irqchip_in_kernel(vcpu->kvm)) {
  800. kvm_vgic_reset_mapped_irq(vcpu, timer_irq(map.direct_vtimer));
  801. if (map.direct_ptimer)
  802. kvm_vgic_reset_mapped_irq(vcpu, timer_irq(map.direct_ptimer));
  803. }
  804. }
  805. if (map.emul_vtimer)
  806. soft_timer_cancel(&map.emul_vtimer->hrtimer);
  807. if (map.emul_ptimer)
  808. soft_timer_cancel(&map.emul_ptimer->hrtimer);
  809. }
  810. static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)
  811. {
  812. struct arch_timer_context *ctxt = vcpu_get_timer(vcpu, timerid);
  813. struct kvm *kvm = vcpu->kvm;
  814. ctxt->vcpu = vcpu;
  815. if (timerid == TIMER_VTIMER)
  816. ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
  817. else
  818. ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
  819. hrtimer_init(&ctxt->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
  820. ctxt->hrtimer.function = kvm_hrtimer_expire;
  821. switch (timerid) {
  822. case TIMER_PTIMER:
  823. case TIMER_HPTIMER:
  824. ctxt->host_timer_irq = host_ptimer_irq;
  825. break;
  826. case TIMER_VTIMER:
  827. case TIMER_HVTIMER:
  828. ctxt->host_timer_irq = host_vtimer_irq;
  829. break;
  830. }
  831. }
  832. void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
  833. {
  834. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  835. for (int i = 0; i < NR_KVM_TIMERS; i++)
  836. timer_context_init(vcpu, i);
  837. /* Synchronize offsets across timers of a VM if not already provided */
  838. if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
  839. timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
  840. timer_set_offset(vcpu_ptimer(vcpu), 0);
  841. }
  842. hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
  843. timer->bg_timer.function = kvm_bg_timer_expire;
  844. }
  845. void kvm_timer_init_vm(struct kvm *kvm)
  846. {
  847. for (int i = 0; i < NR_KVM_TIMERS; i++)
  848. kvm->arch.timer_data.ppi[i] = default_ppi[i];
  849. }
  850. void kvm_timer_cpu_up(void)
  851. {
  852. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  853. if (host_ptimer_irq)
  854. enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags);
  855. }
  856. void kvm_timer_cpu_down(void)
  857. {
  858. disable_percpu_irq(host_vtimer_irq);
  859. if (host_ptimer_irq)
  860. disable_percpu_irq(host_ptimer_irq);
  861. }
  862. int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
  863. {
  864. struct arch_timer_context *timer;
  865. switch (regid) {
  866. case KVM_REG_ARM_TIMER_CTL:
  867. timer = vcpu_vtimer(vcpu);
  868. kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
  869. break;
  870. case KVM_REG_ARM_TIMER_CNT:
  871. if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET,
  872. &vcpu->kvm->arch.flags)) {
  873. timer = vcpu_vtimer(vcpu);
  874. timer_set_offset(timer, kvm_phys_timer_read() - value);
  875. }
  876. break;
  877. case KVM_REG_ARM_TIMER_CVAL:
  878. timer = vcpu_vtimer(vcpu);
  879. kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
  880. break;
  881. case KVM_REG_ARM_PTIMER_CTL:
  882. timer = vcpu_ptimer(vcpu);
  883. kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
  884. break;
  885. case KVM_REG_ARM_PTIMER_CNT:
  886. if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET,
  887. &vcpu->kvm->arch.flags)) {
  888. timer = vcpu_ptimer(vcpu);
  889. timer_set_offset(timer, kvm_phys_timer_read() - value);
  890. }
  891. break;
  892. case KVM_REG_ARM_PTIMER_CVAL:
  893. timer = vcpu_ptimer(vcpu);
  894. kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
  895. break;
  896. default:
  897. return -1;
  898. }
  899. return 0;
  900. }
  901. static u64 read_timer_ctl(struct arch_timer_context *timer)
  902. {
  903. /*
  904. * Set ISTATUS bit if it's expired.
  905. * Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is
  906. * UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit
  907. * regardless of ENABLE bit for our implementation convenience.
  908. */
  909. u32 ctl = timer_get_ctl(timer);
  910. if (!kvm_timer_compute_delta(timer))
  911. ctl |= ARCH_TIMER_CTRL_IT_STAT;
  912. return ctl;
  913. }
  914. u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
  915. {
  916. switch (regid) {
  917. case KVM_REG_ARM_TIMER_CTL:
  918. return kvm_arm_timer_read(vcpu,
  919. vcpu_vtimer(vcpu), TIMER_REG_CTL);
  920. case KVM_REG_ARM_TIMER_CNT:
  921. return kvm_arm_timer_read(vcpu,
  922. vcpu_vtimer(vcpu), TIMER_REG_CNT);
  923. case KVM_REG_ARM_TIMER_CVAL:
  924. return kvm_arm_timer_read(vcpu,
  925. vcpu_vtimer(vcpu), TIMER_REG_CVAL);
  926. case KVM_REG_ARM_PTIMER_CTL:
  927. return kvm_arm_timer_read(vcpu,
  928. vcpu_ptimer(vcpu), TIMER_REG_CTL);
  929. case KVM_REG_ARM_PTIMER_CNT:
  930. return kvm_arm_timer_read(vcpu,
  931. vcpu_ptimer(vcpu), TIMER_REG_CNT);
  932. case KVM_REG_ARM_PTIMER_CVAL:
  933. return kvm_arm_timer_read(vcpu,
  934. vcpu_ptimer(vcpu), TIMER_REG_CVAL);
  935. }
  936. return (u64)-1;
  937. }
  938. static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
  939. struct arch_timer_context *timer,
  940. enum kvm_arch_timer_regs treg)
  941. {
  942. u64 val;
  943. switch (treg) {
  944. case TIMER_REG_TVAL:
  945. val = timer_get_cval(timer) - kvm_phys_timer_read() + timer_get_offset(timer);
  946. val = lower_32_bits(val);
  947. break;
  948. case TIMER_REG_CTL:
  949. val = read_timer_ctl(timer);
  950. break;
  951. case TIMER_REG_CVAL:
  952. val = timer_get_cval(timer);
  953. break;
  954. case TIMER_REG_CNT:
  955. val = kvm_phys_timer_read() - timer_get_offset(timer);
  956. break;
  957. case TIMER_REG_VOFF:
  958. val = *timer->offset.vcpu_offset;
  959. break;
  960. default:
  961. BUG();
  962. }
  963. return val;
  964. }
  965. u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu,
  966. enum kvm_arch_timers tmr,
  967. enum kvm_arch_timer_regs treg)
  968. {
  969. struct arch_timer_context *timer;
  970. struct timer_map map;
  971. u64 val;
  972. get_timer_map(vcpu, &map);
  973. timer = vcpu_get_timer(vcpu, tmr);
  974. if (timer == map.emul_vtimer || timer == map.emul_ptimer)
  975. return kvm_arm_timer_read(vcpu, timer, treg);
  976. preempt_disable();
  977. timer_save_state(timer);
  978. val = kvm_arm_timer_read(vcpu, timer, treg);
  979. timer_restore_state(timer);
  980. preempt_enable();
  981. return val;
  982. }
  983. static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
  984. struct arch_timer_context *timer,
  985. enum kvm_arch_timer_regs treg,
  986. u64 val)
  987. {
  988. switch (treg) {
  989. case TIMER_REG_TVAL:
  990. timer_set_cval(timer, kvm_phys_timer_read() - timer_get_offset(timer) + (s32)val);
  991. break;
  992. case TIMER_REG_CTL:
  993. timer_set_ctl(timer, val & ~ARCH_TIMER_CTRL_IT_STAT);
  994. break;
  995. case TIMER_REG_CVAL:
  996. timer_set_cval(timer, val);
  997. break;
  998. case TIMER_REG_VOFF:
  999. *timer->offset.vcpu_offset = val;
  1000. break;
  1001. default:
  1002. BUG();
  1003. }
  1004. }
  1005. void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,
  1006. enum kvm_arch_timers tmr,
  1007. enum kvm_arch_timer_regs treg,
  1008. u64 val)
  1009. {
  1010. struct arch_timer_context *timer;
  1011. struct timer_map map;
  1012. get_timer_map(vcpu, &map);
  1013. timer = vcpu_get_timer(vcpu, tmr);
  1014. if (timer == map.emul_vtimer || timer == map.emul_ptimer) {
  1015. soft_timer_cancel(&timer->hrtimer);
  1016. kvm_arm_timer_write(vcpu, timer, treg, val);
  1017. timer_emulate(timer);
  1018. } else {
  1019. preempt_disable();
  1020. timer_save_state(timer);
  1021. kvm_arm_timer_write(vcpu, timer, treg, val);
  1022. timer_restore_state(timer);
  1023. preempt_enable();
  1024. }
  1025. }
  1026. static int timer_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  1027. {
  1028. if (vcpu)
  1029. irqd_set_forwarded_to_vcpu(d);
  1030. else
  1031. irqd_clr_forwarded_to_vcpu(d);
  1032. return 0;
  1033. }
  1034. static int timer_irq_set_irqchip_state(struct irq_data *d,
  1035. enum irqchip_irq_state which, bool val)
  1036. {
  1037. if (which != IRQCHIP_STATE_ACTIVE || !irqd_is_forwarded_to_vcpu(d))
  1038. return irq_chip_set_parent_state(d, which, val);
  1039. if (val)
  1040. irq_chip_mask_parent(d);
  1041. else
  1042. irq_chip_unmask_parent(d);
  1043. return 0;
  1044. }
  1045. static void timer_irq_eoi(struct irq_data *d)
  1046. {
  1047. if (!irqd_is_forwarded_to_vcpu(d))
  1048. irq_chip_eoi_parent(d);
  1049. }
  1050. static void timer_irq_ack(struct irq_data *d)
  1051. {
  1052. d = d->parent_data;
  1053. if (d->chip->irq_ack)
  1054. d->chip->irq_ack(d);
  1055. }
  1056. static struct irq_chip timer_chip = {
  1057. .name = "KVM",
  1058. .irq_ack = timer_irq_ack,
  1059. .irq_mask = irq_chip_mask_parent,
  1060. .irq_unmask = irq_chip_unmask_parent,
  1061. .irq_eoi = timer_irq_eoi,
  1062. .irq_set_type = irq_chip_set_type_parent,
  1063. .irq_set_vcpu_affinity = timer_irq_set_vcpu_affinity,
  1064. .irq_set_irqchip_state = timer_irq_set_irqchip_state,
  1065. };
  1066. static int timer_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  1067. unsigned int nr_irqs, void *arg)
  1068. {
  1069. irq_hw_number_t hwirq = (uintptr_t)arg;
  1070. return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  1071. &timer_chip, NULL);
  1072. }
  1073. static void timer_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  1074. unsigned int nr_irqs)
  1075. {
  1076. }
  1077. static const struct irq_domain_ops timer_domain_ops = {
  1078. .alloc = timer_irq_domain_alloc,
  1079. .free = timer_irq_domain_free,
  1080. };
  1081. static void kvm_irq_fixup_flags(unsigned int virq, u32 *flags)
  1082. {
  1083. *flags = irq_get_trigger_type(virq);
  1084. if (*flags != IRQF_TRIGGER_HIGH && *flags != IRQF_TRIGGER_LOW) {
  1085. kvm_err("Invalid trigger for timer IRQ%d, assuming level low\n",
  1086. virq);
  1087. *flags = IRQF_TRIGGER_LOW;
  1088. }
  1089. }
  1090. static int kvm_irq_init(struct arch_timer_kvm_info *info)
  1091. {
  1092. struct irq_domain *domain = NULL;
  1093. if (info->virtual_irq <= 0) {
  1094. kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
  1095. info->virtual_irq);
  1096. return -ENODEV;
  1097. }
  1098. host_vtimer_irq = info->virtual_irq;
  1099. kvm_irq_fixup_flags(host_vtimer_irq, &host_vtimer_irq_flags);
  1100. if (kvm_vgic_global_state.no_hw_deactivation) {
  1101. struct fwnode_handle *fwnode;
  1102. struct irq_data *data;
  1103. fwnode = irq_domain_alloc_named_fwnode("kvm-timer");
  1104. if (!fwnode)
  1105. return -ENOMEM;
  1106. /* Assume both vtimer and ptimer in the same parent */
  1107. data = irq_get_irq_data(host_vtimer_irq);
  1108. domain = irq_domain_create_hierarchy(data->domain, 0,
  1109. NR_KVM_TIMERS, fwnode,
  1110. &timer_domain_ops, NULL);
  1111. if (!domain) {
  1112. irq_domain_free_fwnode(fwnode);
  1113. return -ENOMEM;
  1114. }
  1115. arch_timer_irq_ops.flags |= VGIC_IRQ_SW_RESAMPLE;
  1116. WARN_ON(irq_domain_push_irq(domain, host_vtimer_irq,
  1117. (void *)TIMER_VTIMER));
  1118. }
  1119. if (info->physical_irq > 0) {
  1120. host_ptimer_irq = info->physical_irq;
  1121. kvm_irq_fixup_flags(host_ptimer_irq, &host_ptimer_irq_flags);
  1122. if (domain)
  1123. WARN_ON(irq_domain_push_irq(domain, host_ptimer_irq,
  1124. (void *)TIMER_PTIMER));
  1125. }
  1126. return 0;
  1127. }
  1128. int __init kvm_timer_hyp_init(bool has_gic)
  1129. {
  1130. struct arch_timer_kvm_info *info;
  1131. int err;
  1132. info = arch_timer_get_kvm_info();
  1133. timecounter = &info->timecounter;
  1134. if (!timecounter->cc) {
  1135. kvm_err("kvm_arch_timer: uninitialized timecounter\n");
  1136. return -ENODEV;
  1137. }
  1138. err = kvm_irq_init(info);
  1139. if (err)
  1140. return err;
  1141. /* First, do the virtual EL1 timer irq */
  1142. err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
  1143. "kvm guest vtimer", kvm_get_running_vcpus());
  1144. if (err) {
  1145. kvm_err("kvm_arch_timer: can't request vtimer interrupt %d (%d)\n",
  1146. host_vtimer_irq, err);
  1147. return err;
  1148. }
  1149. if (has_gic) {
  1150. err = irq_set_vcpu_affinity(host_vtimer_irq,
  1151. kvm_get_running_vcpus());
  1152. if (err) {
  1153. kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
  1154. goto out_free_vtimer_irq;
  1155. }
  1156. static_branch_enable(&has_gic_active_state);
  1157. }
  1158. kvm_debug("virtual timer IRQ%d\n", host_vtimer_irq);
  1159. /* Now let's do the physical EL1 timer irq */
  1160. if (info->physical_irq > 0) {
  1161. err = request_percpu_irq(host_ptimer_irq, kvm_arch_timer_handler,
  1162. "kvm guest ptimer", kvm_get_running_vcpus());
  1163. if (err) {
  1164. kvm_err("kvm_arch_timer: can't request ptimer interrupt %d (%d)\n",
  1165. host_ptimer_irq, err);
  1166. goto out_free_vtimer_irq;
  1167. }
  1168. if (has_gic) {
  1169. err = irq_set_vcpu_affinity(host_ptimer_irq,
  1170. kvm_get_running_vcpus());
  1171. if (err) {
  1172. kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
  1173. goto out_free_ptimer_irq;
  1174. }
  1175. }
  1176. kvm_debug("physical timer IRQ%d\n", host_ptimer_irq);
  1177. } else if (has_vhe()) {
  1178. kvm_err("kvm_arch_timer: invalid physical timer IRQ: %d\n",
  1179. info->physical_irq);
  1180. err = -ENODEV;
  1181. goto out_free_vtimer_irq;
  1182. }
  1183. return 0;
  1184. out_free_ptimer_irq:
  1185. if (info->physical_irq > 0)
  1186. free_percpu_irq(host_ptimer_irq, kvm_get_running_vcpus());
  1187. out_free_vtimer_irq:
  1188. free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
  1189. return err;
  1190. }
  1191. void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
  1192. {
  1193. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  1194. soft_timer_cancel(&timer->bg_timer);
  1195. }
  1196. static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
  1197. {
  1198. u32 ppis = 0;
  1199. bool valid;
  1200. mutex_lock(&vcpu->kvm->arch.config_lock);
  1201. for (int i = 0; i < nr_timers(vcpu); i++) {
  1202. struct arch_timer_context *ctx;
  1203. int irq;
  1204. ctx = vcpu_get_timer(vcpu, i);
  1205. irq = timer_irq(ctx);
  1206. if (kvm_vgic_set_owner(vcpu, irq, ctx))
  1207. break;
  1208. /*
  1209. * We know by construction that we only have PPIs, so
  1210. * all values are less than 32.
  1211. */
  1212. ppis |= BIT(irq);
  1213. }
  1214. valid = hweight32(ppis) == nr_timers(vcpu);
  1215. if (valid)
  1216. set_bit(KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE, &vcpu->kvm->arch.flags);
  1217. mutex_unlock(&vcpu->kvm->arch.config_lock);
  1218. return valid;
  1219. }
  1220. static bool kvm_arch_timer_get_input_level(int vintid)
  1221. {
  1222. struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
  1223. if (WARN(!vcpu, "No vcpu context!\n"))
  1224. return false;
  1225. for (int i = 0; i < nr_timers(vcpu); i++) {
  1226. struct arch_timer_context *ctx;
  1227. ctx = vcpu_get_timer(vcpu, i);
  1228. if (timer_irq(ctx) == vintid)
  1229. return kvm_timer_should_fire(ctx);
  1230. }
  1231. /* A timer IRQ has fired, but no matching timer was found? */
  1232. WARN_RATELIMIT(1, "timer INTID%d unknown\n", vintid);
  1233. return false;
  1234. }
  1235. int kvm_timer_enable(struct kvm_vcpu *vcpu)
  1236. {
  1237. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  1238. struct timer_map map;
  1239. int ret;
  1240. if (timer->enabled)
  1241. return 0;
  1242. /* Without a VGIC we do not map virtual IRQs to physical IRQs */
  1243. if (!irqchip_in_kernel(vcpu->kvm))
  1244. goto no_vgic;
  1245. /*
  1246. * At this stage, we have the guarantee that the vgic is both
  1247. * available and initialized.
  1248. */
  1249. if (!timer_irqs_are_valid(vcpu)) {
  1250. kvm_debug("incorrectly configured timer irqs\n");
  1251. return -EINVAL;
  1252. }
  1253. get_timer_map(vcpu, &map);
  1254. ret = kvm_vgic_map_phys_irq(vcpu,
  1255. map.direct_vtimer->host_timer_irq,
  1256. timer_irq(map.direct_vtimer),
  1257. &arch_timer_irq_ops);
  1258. if (ret)
  1259. return ret;
  1260. if (map.direct_ptimer) {
  1261. ret = kvm_vgic_map_phys_irq(vcpu,
  1262. map.direct_ptimer->host_timer_irq,
  1263. timer_irq(map.direct_ptimer),
  1264. &arch_timer_irq_ops);
  1265. }
  1266. if (ret)
  1267. return ret;
  1268. no_vgic:
  1269. timer->enabled = 1;
  1270. return 0;
  1271. }
  1272. /* If we have CNTPOFF, permanently set ECV to enable it */
  1273. void kvm_timer_init_vhe(void)
  1274. {
  1275. if (cpus_have_final_cap(ARM64_HAS_ECV_CNTPOFF))
  1276. sysreg_clear_set(cnthctl_el2, 0, CNTHCTL_ECV);
  1277. }
  1278. int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  1279. {
  1280. int __user *uaddr = (int __user *)(long)attr->addr;
  1281. int irq, idx, ret = 0;
  1282. if (!irqchip_in_kernel(vcpu->kvm))
  1283. return -EINVAL;
  1284. if (get_user(irq, uaddr))
  1285. return -EFAULT;
  1286. if (!(irq_is_ppi(irq)))
  1287. return -EINVAL;
  1288. mutex_lock(&vcpu->kvm->arch.config_lock);
  1289. if (test_bit(KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE,
  1290. &vcpu->kvm->arch.flags)) {
  1291. ret = -EBUSY;
  1292. goto out;
  1293. }
  1294. switch (attr->attr) {
  1295. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  1296. idx = TIMER_VTIMER;
  1297. break;
  1298. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  1299. idx = TIMER_PTIMER;
  1300. break;
  1301. case KVM_ARM_VCPU_TIMER_IRQ_HVTIMER:
  1302. idx = TIMER_HVTIMER;
  1303. break;
  1304. case KVM_ARM_VCPU_TIMER_IRQ_HPTIMER:
  1305. idx = TIMER_HPTIMER;
  1306. break;
  1307. default:
  1308. ret = -ENXIO;
  1309. goto out;
  1310. }
  1311. /*
  1312. * We cannot validate the IRQ unicity before we run, so take it at
  1313. * face value. The verdict will be given on first vcpu run, for each
  1314. * vcpu. Yes this is late. Blame it on the stupid API.
  1315. */
  1316. vcpu->kvm->arch.timer_data.ppi[idx] = irq;
  1317. out:
  1318. mutex_unlock(&vcpu->kvm->arch.config_lock);
  1319. return ret;
  1320. }
  1321. int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  1322. {
  1323. int __user *uaddr = (int __user *)(long)attr->addr;
  1324. struct arch_timer_context *timer;
  1325. int irq;
  1326. switch (attr->attr) {
  1327. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  1328. timer = vcpu_vtimer(vcpu);
  1329. break;
  1330. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  1331. timer = vcpu_ptimer(vcpu);
  1332. break;
  1333. case KVM_ARM_VCPU_TIMER_IRQ_HVTIMER:
  1334. timer = vcpu_hvtimer(vcpu);
  1335. break;
  1336. case KVM_ARM_VCPU_TIMER_IRQ_HPTIMER:
  1337. timer = vcpu_hptimer(vcpu);
  1338. break;
  1339. default:
  1340. return -ENXIO;
  1341. }
  1342. irq = timer_irq(timer);
  1343. return put_user(irq, uaddr);
  1344. }
  1345. int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  1346. {
  1347. switch (attr->attr) {
  1348. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  1349. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  1350. case KVM_ARM_VCPU_TIMER_IRQ_HVTIMER:
  1351. case KVM_ARM_VCPU_TIMER_IRQ_HPTIMER:
  1352. return 0;
  1353. }
  1354. return -ENXIO;
  1355. }
  1356. int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm,
  1357. struct kvm_arm_counter_offset *offset)
  1358. {
  1359. int ret = 0;
  1360. if (offset->reserved)
  1361. return -EINVAL;
  1362. mutex_lock(&kvm->lock);
  1363. if (lock_all_vcpus(kvm)) {
  1364. set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
  1365. /*
  1366. * If userspace decides to set the offset using this
  1367. * API rather than merely restoring the counter
  1368. * values, the offset applies to both the virtual and
  1369. * physical views.
  1370. */
  1371. kvm->arch.timer_data.voffset = offset->counter_offset;
  1372. kvm->arch.timer_data.poffset = offset->counter_offset;
  1373. unlock_all_vcpus(kvm);
  1374. } else {
  1375. ret = -EBUSY;
  1376. }
  1377. mutex_unlock(&kvm->lock);
  1378. return ret;
  1379. }