arch_timer.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. * Author: Marc Zyngier <[email protected]>
  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 <kvm/arm_vgic.h>
  18. #include <kvm/arm_arch_timer.h>
  19. #include "trace.h"
  20. static struct timecounter *timecounter;
  21. static unsigned int host_vtimer_irq;
  22. static unsigned int host_ptimer_irq;
  23. static u32 host_vtimer_irq_flags;
  24. static u32 host_ptimer_irq_flags;
  25. static DEFINE_STATIC_KEY_FALSE(has_gic_active_state);
  26. static const struct kvm_irq_level default_ptimer_irq = {
  27. .irq = 30,
  28. .level = 1,
  29. };
  30. static const struct kvm_irq_level default_vtimer_irq = {
  31. .irq = 27,
  32. .level = 1,
  33. };
  34. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx);
  35. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  36. struct arch_timer_context *timer_ctx);
  37. static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
  38. static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
  39. struct arch_timer_context *timer,
  40. enum kvm_arch_timer_regs treg,
  41. u64 val);
  42. static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
  43. struct arch_timer_context *timer,
  44. enum kvm_arch_timer_regs treg);
  45. u32 timer_get_ctl(struct arch_timer_context *ctxt)
  46. {
  47. struct kvm_vcpu *vcpu = ctxt->vcpu;
  48. switch(arch_timer_ctx_index(ctxt)) {
  49. case TIMER_VTIMER:
  50. return __vcpu_sys_reg(vcpu, CNTV_CTL_EL0);
  51. case TIMER_PTIMER:
  52. return __vcpu_sys_reg(vcpu, CNTP_CTL_EL0);
  53. default:
  54. WARN_ON(1);
  55. return 0;
  56. }
  57. }
  58. u64 timer_get_cval(struct arch_timer_context *ctxt)
  59. {
  60. struct kvm_vcpu *vcpu = ctxt->vcpu;
  61. switch(arch_timer_ctx_index(ctxt)) {
  62. case TIMER_VTIMER:
  63. return __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0);
  64. case TIMER_PTIMER:
  65. return __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
  66. default:
  67. WARN_ON(1);
  68. return 0;
  69. }
  70. }
  71. static u64 timer_get_offset(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. if (likely(!kvm_vm_is_protected(vcpu->kvm)))
  77. return __vcpu_sys_reg(vcpu, CNTVOFF_EL2);
  78. fallthrough;
  79. default:
  80. return 0;
  81. }
  82. }
  83. static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl)
  84. {
  85. struct kvm_vcpu *vcpu = ctxt->vcpu;
  86. switch(arch_timer_ctx_index(ctxt)) {
  87. case TIMER_VTIMER:
  88. __vcpu_sys_reg(vcpu, CNTV_CTL_EL0) = ctl;
  89. break;
  90. case TIMER_PTIMER:
  91. __vcpu_sys_reg(vcpu, CNTP_CTL_EL0) = ctl;
  92. break;
  93. default:
  94. WARN_ON(1);
  95. }
  96. }
  97. static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)
  98. {
  99. struct kvm_vcpu *vcpu = ctxt->vcpu;
  100. switch(arch_timer_ctx_index(ctxt)) {
  101. case TIMER_VTIMER:
  102. __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0) = cval;
  103. break;
  104. case TIMER_PTIMER:
  105. __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = cval;
  106. break;
  107. default:
  108. WARN_ON(1);
  109. }
  110. }
  111. static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset)
  112. {
  113. struct kvm_vcpu *vcpu = ctxt->vcpu;
  114. switch(arch_timer_ctx_index(ctxt)) {
  115. case TIMER_VTIMER:
  116. __vcpu_sys_reg(vcpu, CNTVOFF_EL2) = offset;
  117. break;
  118. default:
  119. WARN(offset, "timer %ld\n", arch_timer_ctx_index(ctxt));
  120. }
  121. }
  122. u64 kvm_phys_timer_read(void)
  123. {
  124. return timecounter->cc->read(timecounter->cc);
  125. }
  126. static void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
  127. {
  128. if (has_vhe()) {
  129. map->direct_vtimer = vcpu_vtimer(vcpu);
  130. map->direct_ptimer = vcpu_ptimer(vcpu);
  131. map->emul_ptimer = NULL;
  132. } else {
  133. map->direct_vtimer = vcpu_vtimer(vcpu);
  134. map->direct_ptimer = NULL;
  135. map->emul_ptimer = vcpu_ptimer(vcpu);
  136. }
  137. trace_kvm_get_timer_map(vcpu->vcpu_id, map);
  138. }
  139. static inline bool userspace_irqchip(struct kvm *kvm)
  140. {
  141. return static_branch_unlikely(&userspace_irqchip_in_use) &&
  142. unlikely(!irqchip_in_kernel(kvm));
  143. }
  144. static void soft_timer_start(struct hrtimer *hrt, u64 ns)
  145. {
  146. hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
  147. HRTIMER_MODE_ABS_HARD);
  148. }
  149. static void soft_timer_cancel(struct hrtimer *hrt)
  150. {
  151. hrtimer_cancel(hrt);
  152. }
  153. static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
  154. {
  155. struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
  156. struct arch_timer_context *ctx;
  157. struct timer_map map;
  158. /*
  159. * We may see a timer interrupt after vcpu_put() has been called which
  160. * sets the CPU's vcpu pointer to NULL, because even though the timer
  161. * has been disabled in timer_save_state(), the hardware interrupt
  162. * signal may not have been retired from the interrupt controller yet.
  163. */
  164. if (!vcpu)
  165. return IRQ_HANDLED;
  166. get_timer_map(vcpu, &map);
  167. if (irq == host_vtimer_irq)
  168. ctx = map.direct_vtimer;
  169. else
  170. ctx = map.direct_ptimer;
  171. if (kvm_timer_should_fire(ctx))
  172. kvm_timer_update_irq(vcpu, true, ctx);
  173. if (userspace_irqchip(vcpu->kvm) &&
  174. !static_branch_unlikely(&has_gic_active_state))
  175. disable_percpu_irq(host_vtimer_irq);
  176. return IRQ_HANDLED;
  177. }
  178. static u64 kvm_counter_compute_delta(struct arch_timer_context *timer_ctx,
  179. u64 val)
  180. {
  181. u64 now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
  182. if (now < val) {
  183. u64 ns;
  184. ns = cyclecounter_cyc2ns(timecounter->cc,
  185. val - now,
  186. timecounter->mask,
  187. &timecounter->frac);
  188. return ns;
  189. }
  190. return 0;
  191. }
  192. static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
  193. {
  194. return kvm_counter_compute_delta(timer_ctx, timer_get_cval(timer_ctx));
  195. }
  196. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
  197. {
  198. WARN_ON(timer_ctx && timer_ctx->loaded);
  199. return timer_ctx &&
  200. ((timer_get_ctl(timer_ctx) &
  201. (ARCH_TIMER_CTRL_IT_MASK | ARCH_TIMER_CTRL_ENABLE)) == ARCH_TIMER_CTRL_ENABLE);
  202. }
  203. static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu)
  204. {
  205. return (cpus_have_final_cap(ARM64_HAS_WFXT) &&
  206. vcpu_get_flag(vcpu, IN_WFIT));
  207. }
  208. static u64 wfit_delay_ns(struct kvm_vcpu *vcpu)
  209. {
  210. struct arch_timer_context *ctx = vcpu_vtimer(vcpu);
  211. u64 val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
  212. return kvm_counter_compute_delta(ctx, val);
  213. }
  214. /*
  215. * Returns the earliest expiration time in ns among guest timers.
  216. * Note that it will return 0 if none of timers can fire.
  217. */
  218. static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
  219. {
  220. u64 min_delta = ULLONG_MAX;
  221. int i;
  222. for (i = 0; i < NR_KVM_TIMERS; i++) {
  223. struct arch_timer_context *ctx = &vcpu->arch.timer_cpu.timers[i];
  224. WARN(ctx->loaded, "timer %d loaded\n", i);
  225. if (kvm_timer_irq_can_fire(ctx))
  226. min_delta = min(min_delta, kvm_timer_compute_delta(ctx));
  227. }
  228. if (vcpu_has_wfit_active(vcpu))
  229. min_delta = min(min_delta, wfit_delay_ns(vcpu));
  230. /* If none of timers can fire, then return 0 */
  231. if (min_delta == ULLONG_MAX)
  232. return 0;
  233. return min_delta;
  234. }
  235. static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
  236. {
  237. struct arch_timer_cpu *timer;
  238. struct kvm_vcpu *vcpu;
  239. u64 ns;
  240. timer = container_of(hrt, struct arch_timer_cpu, bg_timer);
  241. vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
  242. /*
  243. * Check that the timer has really expired from the guest's
  244. * PoV (NTP on the host may have forced it to expire
  245. * early). If we should have slept longer, restart it.
  246. */
  247. ns = kvm_timer_earliest_exp(vcpu);
  248. if (unlikely(ns)) {
  249. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  250. return HRTIMER_RESTART;
  251. }
  252. kvm_vcpu_wake_up(vcpu);
  253. return HRTIMER_NORESTART;
  254. }
  255. static enum hrtimer_restart kvm_hrtimer_expire(struct hrtimer *hrt)
  256. {
  257. struct arch_timer_context *ctx;
  258. struct kvm_vcpu *vcpu;
  259. u64 ns;
  260. ctx = container_of(hrt, struct arch_timer_context, hrtimer);
  261. vcpu = ctx->vcpu;
  262. trace_kvm_timer_hrtimer_expire(ctx);
  263. /*
  264. * Check that the timer has really expired from the guest's
  265. * PoV (NTP on the host may have forced it to expire
  266. * early). If not ready, schedule for a later time.
  267. */
  268. ns = kvm_timer_compute_delta(ctx);
  269. if (unlikely(ns)) {
  270. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  271. return HRTIMER_RESTART;
  272. }
  273. kvm_timer_update_irq(vcpu, true, ctx);
  274. return HRTIMER_NORESTART;
  275. }
  276. static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
  277. {
  278. enum kvm_arch_timers index;
  279. u64 cval, now;
  280. if (!timer_ctx)
  281. return false;
  282. index = arch_timer_ctx_index(timer_ctx);
  283. if (timer_ctx->loaded) {
  284. u32 cnt_ctl = 0;
  285. switch (index) {
  286. case TIMER_VTIMER:
  287. cnt_ctl = read_sysreg_el0(SYS_CNTV_CTL);
  288. break;
  289. case TIMER_PTIMER:
  290. cnt_ctl = read_sysreg_el0(SYS_CNTP_CTL);
  291. break;
  292. case NR_KVM_TIMERS:
  293. /* GCC is braindead */
  294. cnt_ctl = 0;
  295. break;
  296. }
  297. return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) &&
  298. (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) &&
  299. !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK);
  300. }
  301. if (!kvm_timer_irq_can_fire(timer_ctx))
  302. return false;
  303. cval = timer_get_cval(timer_ctx);
  304. now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
  305. return cval <= now;
  306. }
  307. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  308. {
  309. return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) == 0;
  310. }
  311. /*
  312. * Reflect the timer output level into the kvm_run structure
  313. */
  314. void kvm_timer_update_run(struct kvm_vcpu *vcpu)
  315. {
  316. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  317. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  318. struct kvm_sync_regs *regs = &vcpu->run->s.regs;
  319. /* Populate the device bitmap with the timer states */
  320. regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
  321. KVM_ARM_DEV_EL1_PTIMER);
  322. if (kvm_timer_should_fire(vtimer))
  323. regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
  324. if (kvm_timer_should_fire(ptimer))
  325. regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
  326. }
  327. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  328. struct arch_timer_context *timer_ctx)
  329. {
  330. int ret;
  331. timer_ctx->irq.level = new_level;
  332. trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
  333. timer_ctx->irq.level);
  334. if (!userspace_irqchip(vcpu->kvm)) {
  335. ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
  336. timer_ctx->irq.irq,
  337. timer_ctx->irq.level,
  338. timer_ctx);
  339. WARN_ON(ret);
  340. }
  341. }
  342. /* Only called for a fully emulated timer */
  343. static void timer_emulate(struct arch_timer_context *ctx)
  344. {
  345. bool should_fire = kvm_timer_should_fire(ctx);
  346. trace_kvm_timer_emulate(ctx, should_fire);
  347. if (should_fire != ctx->irq.level) {
  348. kvm_timer_update_irq(ctx->vcpu, should_fire, ctx);
  349. return;
  350. }
  351. /*
  352. * If the timer can fire now, we don't need to have a soft timer
  353. * scheduled for the future. If the timer cannot fire at all,
  354. * then we also don't need a soft timer.
  355. */
  356. if (!kvm_timer_irq_can_fire(ctx)) {
  357. soft_timer_cancel(&ctx->hrtimer);
  358. return;
  359. }
  360. soft_timer_start(&ctx->hrtimer, kvm_timer_compute_delta(ctx));
  361. }
  362. static void timer_save_state(struct arch_timer_context *ctx)
  363. {
  364. struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
  365. enum kvm_arch_timers index = arch_timer_ctx_index(ctx);
  366. unsigned long flags;
  367. if (!timer->enabled)
  368. return;
  369. local_irq_save(flags);
  370. if (!ctx->loaded)
  371. goto out;
  372. switch (index) {
  373. case TIMER_VTIMER:
  374. timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTV_CTL));
  375. timer_set_cval(ctx, read_sysreg_el0(SYS_CNTV_CVAL));
  376. /* Disable the timer */
  377. write_sysreg_el0(0, SYS_CNTV_CTL);
  378. isb();
  379. break;
  380. case TIMER_PTIMER:
  381. timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTP_CTL));
  382. timer_set_cval(ctx, read_sysreg_el0(SYS_CNTP_CVAL));
  383. /* Disable the timer */
  384. write_sysreg_el0(0, SYS_CNTP_CTL);
  385. isb();
  386. break;
  387. case NR_KVM_TIMERS:
  388. BUG();
  389. }
  390. trace_kvm_timer_save_state(ctx);
  391. ctx->loaded = false;
  392. out:
  393. local_irq_restore(flags);
  394. }
  395. /*
  396. * Schedule the background timer before calling kvm_vcpu_halt, so that this
  397. * thread is removed from its waitqueue and made runnable when there's a timer
  398. * interrupt to handle.
  399. */
  400. static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
  401. {
  402. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  403. struct timer_map map;
  404. get_timer_map(vcpu, &map);
  405. /*
  406. * If no timers are capable of raising interrupts (disabled or
  407. * masked), then there's no more work for us to do.
  408. */
  409. if (!kvm_timer_irq_can_fire(map.direct_vtimer) &&
  410. !kvm_timer_irq_can_fire(map.direct_ptimer) &&
  411. !kvm_timer_irq_can_fire(map.emul_ptimer) &&
  412. !vcpu_has_wfit_active(vcpu))
  413. return;
  414. /*
  415. * At least one guest time will expire. Schedule a background timer.
  416. * Set the earliest expiration time among the guest timers.
  417. */
  418. soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
  419. }
  420. static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
  421. {
  422. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  423. soft_timer_cancel(&timer->bg_timer);
  424. }
  425. static void timer_restore_state(struct arch_timer_context *ctx)
  426. {
  427. struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
  428. enum kvm_arch_timers index = arch_timer_ctx_index(ctx);
  429. unsigned long flags;
  430. if (!timer->enabled)
  431. return;
  432. local_irq_save(flags);
  433. if (ctx->loaded)
  434. goto out;
  435. switch (index) {
  436. case TIMER_VTIMER:
  437. write_sysreg_el0(timer_get_cval(ctx), SYS_CNTV_CVAL);
  438. isb();
  439. write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTV_CTL);
  440. break;
  441. case TIMER_PTIMER:
  442. write_sysreg_el0(timer_get_cval(ctx), SYS_CNTP_CVAL);
  443. isb();
  444. write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTP_CTL);
  445. break;
  446. case NR_KVM_TIMERS:
  447. BUG();
  448. }
  449. trace_kvm_timer_restore_state(ctx);
  450. ctx->loaded = true;
  451. out:
  452. local_irq_restore(flags);
  453. }
  454. static void set_cntvoff(u64 cntvoff)
  455. {
  456. kvm_call_hyp(__kvm_timer_set_cntvoff, cntvoff);
  457. }
  458. static inline void set_timer_irq_phys_active(struct arch_timer_context *ctx, bool active)
  459. {
  460. int r;
  461. r = irq_set_irqchip_state(ctx->host_timer_irq, IRQCHIP_STATE_ACTIVE, active);
  462. WARN_ON(r);
  463. }
  464. static void kvm_timer_vcpu_load_gic(struct arch_timer_context *ctx)
  465. {
  466. struct kvm_vcpu *vcpu = ctx->vcpu;
  467. bool phys_active = false;
  468. /*
  469. * Update the timer output so that it is likely to match the
  470. * state we're about to restore. If the timer expires between
  471. * this point and the register restoration, we'll take the
  472. * interrupt anyway.
  473. */
  474. kvm_timer_update_irq(ctx->vcpu, kvm_timer_should_fire(ctx), ctx);
  475. if (irqchip_in_kernel(vcpu->kvm))
  476. phys_active = kvm_vgic_map_is_active(vcpu, ctx->irq.irq);
  477. phys_active |= ctx->irq.level;
  478. set_timer_irq_phys_active(ctx, phys_active);
  479. }
  480. static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu)
  481. {
  482. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  483. /*
  484. * Update the timer output so that it is likely to match the
  485. * state we're about to restore. If the timer expires between
  486. * this point and the register restoration, we'll take the
  487. * interrupt anyway.
  488. */
  489. kvm_timer_update_irq(vcpu, kvm_timer_should_fire(vtimer), vtimer);
  490. /*
  491. * When using a userspace irqchip with the architected timers and a
  492. * host interrupt controller that doesn't support an active state, we
  493. * must still prevent continuously exiting from the guest, and
  494. * therefore mask the physical interrupt by disabling it on the host
  495. * interrupt controller when the virtual level is high, such that the
  496. * guest can make forward progress. Once we detect the output level
  497. * being de-asserted, we unmask the interrupt again so that we exit
  498. * from the guest when the timer fires.
  499. */
  500. if (vtimer->irq.level)
  501. disable_percpu_irq(host_vtimer_irq);
  502. else
  503. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  504. }
  505. void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
  506. {
  507. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  508. struct timer_map map;
  509. if (unlikely(!timer->enabled))
  510. return;
  511. get_timer_map(vcpu, &map);
  512. if (static_branch_likely(&has_gic_active_state)) {
  513. kvm_timer_vcpu_load_gic(map.direct_vtimer);
  514. if (map.direct_ptimer)
  515. kvm_timer_vcpu_load_gic(map.direct_ptimer);
  516. } else {
  517. kvm_timer_vcpu_load_nogic(vcpu);
  518. }
  519. set_cntvoff(timer_get_offset(map.direct_vtimer));
  520. kvm_timer_unblocking(vcpu);
  521. timer_restore_state(map.direct_vtimer);
  522. if (map.direct_ptimer)
  523. timer_restore_state(map.direct_ptimer);
  524. if (map.emul_ptimer)
  525. timer_emulate(map.emul_ptimer);
  526. }
  527. bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
  528. {
  529. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  530. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  531. struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
  532. bool vlevel, plevel;
  533. if (likely(irqchip_in_kernel(vcpu->kvm)))
  534. return false;
  535. vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
  536. plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
  537. return kvm_timer_should_fire(vtimer) != vlevel ||
  538. kvm_timer_should_fire(ptimer) != plevel;
  539. }
  540. void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
  541. {
  542. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  543. struct timer_map map;
  544. if (unlikely(!timer->enabled))
  545. return;
  546. get_timer_map(vcpu, &map);
  547. timer_save_state(map.direct_vtimer);
  548. if (map.direct_ptimer)
  549. timer_save_state(map.direct_ptimer);
  550. /*
  551. * Cancel soft timer emulation, because the only case where we
  552. * need it after a vcpu_put is in the context of a sleeping VCPU, and
  553. * in that case we already factor in the deadline for the physical
  554. * timer when scheduling the bg_timer.
  555. *
  556. * In any case, we re-schedule the hrtimer for the physical timer when
  557. * coming back to the VCPU thread in kvm_timer_vcpu_load().
  558. */
  559. if (map.emul_ptimer)
  560. soft_timer_cancel(&map.emul_ptimer->hrtimer);
  561. if (kvm_vcpu_is_blocking(vcpu))
  562. kvm_timer_blocking(vcpu);
  563. /*
  564. * The kernel may decide to run userspace after calling vcpu_put, so
  565. * we reset cntvoff to 0 to ensure a consistent read between user
  566. * accesses to the virtual counter and kernel access to the physical
  567. * counter of non-VHE case. For VHE, the virtual counter uses a fixed
  568. * virtual offset of zero, so no need to zero CNTVOFF_EL2 register.
  569. */
  570. set_cntvoff(0);
  571. }
  572. /*
  573. * With a userspace irqchip we have to check if the guest de-asserted the
  574. * timer and if so, unmask the timer irq signal on the host interrupt
  575. * controller to ensure that we see future timer signals.
  576. */
  577. static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
  578. {
  579. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  580. if (!kvm_timer_should_fire(vtimer)) {
  581. kvm_timer_update_irq(vcpu, false, vtimer);
  582. if (static_branch_likely(&has_gic_active_state))
  583. set_timer_irq_phys_active(vtimer, false);
  584. else
  585. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  586. }
  587. }
  588. void kvm_timer_sync_user(struct kvm_vcpu *vcpu)
  589. {
  590. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  591. if (unlikely(!timer->enabled))
  592. return;
  593. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  594. unmask_vtimer_irq_user(vcpu);
  595. }
  596. int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
  597. {
  598. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  599. struct timer_map map;
  600. get_timer_map(vcpu, &map);
  601. /*
  602. * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
  603. * and to 0 for ARMv7. We provide an implementation that always
  604. * resets the timer to be disabled and unmasked and is compliant with
  605. * the ARMv7 architecture.
  606. */
  607. timer_set_ctl(vcpu_vtimer(vcpu), 0);
  608. timer_set_ctl(vcpu_ptimer(vcpu), 0);
  609. if (timer->enabled) {
  610. kvm_timer_update_irq(vcpu, false, vcpu_vtimer(vcpu));
  611. kvm_timer_update_irq(vcpu, false, vcpu_ptimer(vcpu));
  612. if (irqchip_in_kernel(vcpu->kvm)) {
  613. kvm_vgic_reset_mapped_irq(vcpu, map.direct_vtimer->irq.irq);
  614. if (map.direct_ptimer)
  615. kvm_vgic_reset_mapped_irq(vcpu, map.direct_ptimer->irq.irq);
  616. }
  617. }
  618. if (map.emul_ptimer)
  619. soft_timer_cancel(&map.emul_ptimer->hrtimer);
  620. return 0;
  621. }
  622. /* Make the updates of cntvoff for all vtimer contexts atomic */
  623. static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
  624. {
  625. unsigned long i;
  626. struct kvm *kvm = vcpu->kvm;
  627. struct kvm_vcpu *tmp;
  628. if (unlikely(kvm_vm_is_protected(vcpu->kvm)))
  629. cntvoff = 0;
  630. mutex_lock(&kvm->lock);
  631. kvm_for_each_vcpu(i, tmp, kvm)
  632. timer_set_offset(vcpu_vtimer(tmp), cntvoff);
  633. /*
  634. * When called from the vcpu create path, the CPU being created is not
  635. * included in the loop above, so we just set it here as well.
  636. */
  637. timer_set_offset(vcpu_vtimer(vcpu), cntvoff);
  638. mutex_unlock(&kvm->lock);
  639. }
  640. void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
  641. {
  642. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  643. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  644. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  645. vtimer->vcpu = vcpu;
  646. ptimer->vcpu = vcpu;
  647. /* Synchronize cntvoff across all vtimers of a VM. */
  648. update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
  649. timer_set_offset(ptimer, 0);
  650. hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
  651. timer->bg_timer.function = kvm_bg_timer_expire;
  652. hrtimer_init(&vtimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
  653. hrtimer_init(&ptimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
  654. vtimer->hrtimer.function = kvm_hrtimer_expire;
  655. ptimer->hrtimer.function = kvm_hrtimer_expire;
  656. vtimer->irq.irq = default_vtimer_irq.irq;
  657. ptimer->irq.irq = default_ptimer_irq.irq;
  658. vtimer->host_timer_irq = host_vtimer_irq;
  659. ptimer->host_timer_irq = host_ptimer_irq;
  660. vtimer->host_timer_irq_flags = host_vtimer_irq_flags;
  661. ptimer->host_timer_irq_flags = host_ptimer_irq_flags;
  662. }
  663. static void kvm_timer_init_interrupt(void *info)
  664. {
  665. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  666. enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags);
  667. }
  668. int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
  669. {
  670. struct arch_timer_context *timer;
  671. switch (regid) {
  672. case KVM_REG_ARM_TIMER_CTL:
  673. timer = vcpu_vtimer(vcpu);
  674. kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
  675. break;
  676. case KVM_REG_ARM_TIMER_CNT:
  677. timer = vcpu_vtimer(vcpu);
  678. update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
  679. break;
  680. case KVM_REG_ARM_TIMER_CVAL:
  681. timer = vcpu_vtimer(vcpu);
  682. kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
  683. break;
  684. case KVM_REG_ARM_PTIMER_CTL:
  685. timer = vcpu_ptimer(vcpu);
  686. kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
  687. break;
  688. case KVM_REG_ARM_PTIMER_CVAL:
  689. timer = vcpu_ptimer(vcpu);
  690. kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
  691. break;
  692. default:
  693. return -1;
  694. }
  695. return 0;
  696. }
  697. static u64 read_timer_ctl(struct arch_timer_context *timer)
  698. {
  699. /*
  700. * Set ISTATUS bit if it's expired.
  701. * Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is
  702. * UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit
  703. * regardless of ENABLE bit for our implementation convenience.
  704. */
  705. u32 ctl = timer_get_ctl(timer);
  706. if (!kvm_timer_compute_delta(timer))
  707. ctl |= ARCH_TIMER_CTRL_IT_STAT;
  708. return ctl;
  709. }
  710. u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
  711. {
  712. switch (regid) {
  713. case KVM_REG_ARM_TIMER_CTL:
  714. return kvm_arm_timer_read(vcpu,
  715. vcpu_vtimer(vcpu), TIMER_REG_CTL);
  716. case KVM_REG_ARM_TIMER_CNT:
  717. return kvm_arm_timer_read(vcpu,
  718. vcpu_vtimer(vcpu), TIMER_REG_CNT);
  719. case KVM_REG_ARM_TIMER_CVAL:
  720. return kvm_arm_timer_read(vcpu,
  721. vcpu_vtimer(vcpu), TIMER_REG_CVAL);
  722. case KVM_REG_ARM_PTIMER_CTL:
  723. return kvm_arm_timer_read(vcpu,
  724. vcpu_ptimer(vcpu), TIMER_REG_CTL);
  725. case KVM_REG_ARM_PTIMER_CNT:
  726. return kvm_arm_timer_read(vcpu,
  727. vcpu_ptimer(vcpu), TIMER_REG_CNT);
  728. case KVM_REG_ARM_PTIMER_CVAL:
  729. return kvm_arm_timer_read(vcpu,
  730. vcpu_ptimer(vcpu), TIMER_REG_CVAL);
  731. }
  732. return (u64)-1;
  733. }
  734. static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
  735. struct arch_timer_context *timer,
  736. enum kvm_arch_timer_regs treg)
  737. {
  738. u64 val;
  739. switch (treg) {
  740. case TIMER_REG_TVAL:
  741. val = timer_get_cval(timer) - kvm_phys_timer_read() + timer_get_offset(timer);
  742. val = lower_32_bits(val);
  743. break;
  744. case TIMER_REG_CTL:
  745. val = read_timer_ctl(timer);
  746. break;
  747. case TIMER_REG_CVAL:
  748. val = timer_get_cval(timer);
  749. break;
  750. case TIMER_REG_CNT:
  751. val = kvm_phys_timer_read() - timer_get_offset(timer);
  752. break;
  753. default:
  754. BUG();
  755. }
  756. return val;
  757. }
  758. u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu,
  759. enum kvm_arch_timers tmr,
  760. enum kvm_arch_timer_regs treg)
  761. {
  762. u64 val;
  763. preempt_disable();
  764. kvm_timer_vcpu_put(vcpu);
  765. val = kvm_arm_timer_read(vcpu, vcpu_get_timer(vcpu, tmr), treg);
  766. kvm_timer_vcpu_load(vcpu);
  767. preempt_enable();
  768. return val;
  769. }
  770. static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
  771. struct arch_timer_context *timer,
  772. enum kvm_arch_timer_regs treg,
  773. u64 val)
  774. {
  775. switch (treg) {
  776. case TIMER_REG_TVAL:
  777. timer_set_cval(timer, kvm_phys_timer_read() - timer_get_offset(timer) + (s32)val);
  778. break;
  779. case TIMER_REG_CTL:
  780. timer_set_ctl(timer, val & ~ARCH_TIMER_CTRL_IT_STAT);
  781. break;
  782. case TIMER_REG_CVAL:
  783. timer_set_cval(timer, val);
  784. break;
  785. default:
  786. BUG();
  787. }
  788. }
  789. void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,
  790. enum kvm_arch_timers tmr,
  791. enum kvm_arch_timer_regs treg,
  792. u64 val)
  793. {
  794. preempt_disable();
  795. kvm_timer_vcpu_put(vcpu);
  796. kvm_arm_timer_write(vcpu, vcpu_get_timer(vcpu, tmr), treg, val);
  797. kvm_timer_vcpu_load(vcpu);
  798. preempt_enable();
  799. }
  800. static int kvm_timer_starting_cpu(unsigned int cpu)
  801. {
  802. kvm_timer_init_interrupt(NULL);
  803. return 0;
  804. }
  805. static int kvm_timer_dying_cpu(unsigned int cpu)
  806. {
  807. disable_percpu_irq(host_vtimer_irq);
  808. return 0;
  809. }
  810. static int timer_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  811. {
  812. if (vcpu)
  813. irqd_set_forwarded_to_vcpu(d);
  814. else
  815. irqd_clr_forwarded_to_vcpu(d);
  816. return 0;
  817. }
  818. static int timer_irq_set_irqchip_state(struct irq_data *d,
  819. enum irqchip_irq_state which, bool val)
  820. {
  821. if (which != IRQCHIP_STATE_ACTIVE || !irqd_is_forwarded_to_vcpu(d))
  822. return irq_chip_set_parent_state(d, which, val);
  823. if (val)
  824. irq_chip_mask_parent(d);
  825. else
  826. irq_chip_unmask_parent(d);
  827. return 0;
  828. }
  829. static void timer_irq_eoi(struct irq_data *d)
  830. {
  831. if (!irqd_is_forwarded_to_vcpu(d))
  832. irq_chip_eoi_parent(d);
  833. }
  834. static void timer_irq_ack(struct irq_data *d)
  835. {
  836. d = d->parent_data;
  837. if (d->chip->irq_ack)
  838. d->chip->irq_ack(d);
  839. }
  840. static struct irq_chip timer_chip = {
  841. .name = "KVM",
  842. .irq_ack = timer_irq_ack,
  843. .irq_mask = irq_chip_mask_parent,
  844. .irq_unmask = irq_chip_unmask_parent,
  845. .irq_eoi = timer_irq_eoi,
  846. .irq_set_type = irq_chip_set_type_parent,
  847. .irq_set_vcpu_affinity = timer_irq_set_vcpu_affinity,
  848. .irq_set_irqchip_state = timer_irq_set_irqchip_state,
  849. };
  850. static int timer_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  851. unsigned int nr_irqs, void *arg)
  852. {
  853. irq_hw_number_t hwirq = (uintptr_t)arg;
  854. return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  855. &timer_chip, NULL);
  856. }
  857. static void timer_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  858. unsigned int nr_irqs)
  859. {
  860. }
  861. static const struct irq_domain_ops timer_domain_ops = {
  862. .alloc = timer_irq_domain_alloc,
  863. .free = timer_irq_domain_free,
  864. };
  865. static struct irq_ops arch_timer_irq_ops = {
  866. .get_input_level = kvm_arch_timer_get_input_level,
  867. };
  868. static void kvm_irq_fixup_flags(unsigned int virq, u32 *flags)
  869. {
  870. *flags = irq_get_trigger_type(virq);
  871. if (*flags != IRQF_TRIGGER_HIGH && *flags != IRQF_TRIGGER_LOW) {
  872. kvm_err("Invalid trigger for timer IRQ%d, assuming level low\n",
  873. virq);
  874. *flags = IRQF_TRIGGER_LOW;
  875. }
  876. }
  877. static int kvm_irq_init(struct arch_timer_kvm_info *info)
  878. {
  879. struct irq_domain *domain = NULL;
  880. if (info->virtual_irq <= 0) {
  881. kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
  882. info->virtual_irq);
  883. return -ENODEV;
  884. }
  885. host_vtimer_irq = info->virtual_irq;
  886. kvm_irq_fixup_flags(host_vtimer_irq, &host_vtimer_irq_flags);
  887. if (kvm_vgic_global_state.no_hw_deactivation) {
  888. struct fwnode_handle *fwnode;
  889. struct irq_data *data;
  890. fwnode = irq_domain_alloc_named_fwnode("kvm-timer");
  891. if (!fwnode)
  892. return -ENOMEM;
  893. /* Assume both vtimer and ptimer in the same parent */
  894. data = irq_get_irq_data(host_vtimer_irq);
  895. domain = irq_domain_create_hierarchy(data->domain, 0,
  896. NR_KVM_TIMERS, fwnode,
  897. &timer_domain_ops, NULL);
  898. if (!domain) {
  899. irq_domain_free_fwnode(fwnode);
  900. return -ENOMEM;
  901. }
  902. arch_timer_irq_ops.flags |= VGIC_IRQ_SW_RESAMPLE;
  903. WARN_ON(irq_domain_push_irq(domain, host_vtimer_irq,
  904. (void *)TIMER_VTIMER));
  905. }
  906. if (info->physical_irq > 0) {
  907. host_ptimer_irq = info->physical_irq;
  908. kvm_irq_fixup_flags(host_ptimer_irq, &host_ptimer_irq_flags);
  909. if (domain)
  910. WARN_ON(irq_domain_push_irq(domain, host_ptimer_irq,
  911. (void *)TIMER_PTIMER));
  912. }
  913. return 0;
  914. }
  915. int kvm_timer_hyp_init(bool has_gic)
  916. {
  917. struct arch_timer_kvm_info *info;
  918. int err;
  919. info = arch_timer_get_kvm_info();
  920. timecounter = &info->timecounter;
  921. if (!timecounter->cc) {
  922. kvm_err("kvm_arch_timer: uninitialized timecounter\n");
  923. return -ENODEV;
  924. }
  925. err = kvm_irq_init(info);
  926. if (err)
  927. return err;
  928. /* First, do the virtual EL1 timer irq */
  929. err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
  930. "kvm guest vtimer", kvm_get_running_vcpus());
  931. if (err) {
  932. kvm_err("kvm_arch_timer: can't request vtimer interrupt %d (%d)\n",
  933. host_vtimer_irq, err);
  934. return err;
  935. }
  936. if (has_gic) {
  937. err = irq_set_vcpu_affinity(host_vtimer_irq,
  938. kvm_get_running_vcpus());
  939. if (err) {
  940. kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
  941. goto out_free_irq;
  942. }
  943. static_branch_enable(&has_gic_active_state);
  944. }
  945. kvm_debug("virtual timer IRQ%d\n", host_vtimer_irq);
  946. /* Now let's do the physical EL1 timer irq */
  947. if (info->physical_irq > 0) {
  948. err = request_percpu_irq(host_ptimer_irq, kvm_arch_timer_handler,
  949. "kvm guest ptimer", kvm_get_running_vcpus());
  950. if (err) {
  951. kvm_err("kvm_arch_timer: can't request ptimer interrupt %d (%d)\n",
  952. host_ptimer_irq, err);
  953. return err;
  954. }
  955. if (has_gic) {
  956. err = irq_set_vcpu_affinity(host_ptimer_irq,
  957. kvm_get_running_vcpus());
  958. if (err) {
  959. kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
  960. goto out_free_irq;
  961. }
  962. }
  963. kvm_debug("physical timer IRQ%d\n", host_ptimer_irq);
  964. } else if (has_vhe()) {
  965. kvm_err("kvm_arch_timer: invalid physical timer IRQ: %d\n",
  966. info->physical_irq);
  967. err = -ENODEV;
  968. goto out_free_irq;
  969. }
  970. cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
  971. "kvm/arm/timer:starting", kvm_timer_starting_cpu,
  972. kvm_timer_dying_cpu);
  973. return 0;
  974. out_free_irq:
  975. free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
  976. return err;
  977. }
  978. void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
  979. {
  980. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  981. soft_timer_cancel(&timer->bg_timer);
  982. }
  983. static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
  984. {
  985. int vtimer_irq, ptimer_irq, ret;
  986. unsigned long i;
  987. vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
  988. ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
  989. if (ret)
  990. return false;
  991. ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
  992. ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
  993. if (ret)
  994. return false;
  995. kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
  996. if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
  997. vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
  998. return false;
  999. }
  1000. return true;
  1001. }
  1002. bool kvm_arch_timer_get_input_level(int vintid)
  1003. {
  1004. struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
  1005. struct arch_timer_context *timer;
  1006. if (WARN(!vcpu, "No vcpu context!\n"))
  1007. return false;
  1008. if (vintid == vcpu_vtimer(vcpu)->irq.irq)
  1009. timer = vcpu_vtimer(vcpu);
  1010. else if (vintid == vcpu_ptimer(vcpu)->irq.irq)
  1011. timer = vcpu_ptimer(vcpu);
  1012. else
  1013. BUG();
  1014. return kvm_timer_should_fire(timer);
  1015. }
  1016. int kvm_timer_enable(struct kvm_vcpu *vcpu)
  1017. {
  1018. struct arch_timer_cpu *timer = vcpu_timer(vcpu);
  1019. struct timer_map map;
  1020. int ret;
  1021. if (timer->enabled)
  1022. return 0;
  1023. /* Without a VGIC we do not map virtual IRQs to physical IRQs */
  1024. if (!irqchip_in_kernel(vcpu->kvm))
  1025. goto no_vgic;
  1026. /*
  1027. * At this stage, we have the guarantee that the vgic is both
  1028. * available and initialized.
  1029. */
  1030. if (!timer_irqs_are_valid(vcpu)) {
  1031. kvm_debug("incorrectly configured timer irqs\n");
  1032. return -EINVAL;
  1033. }
  1034. get_timer_map(vcpu, &map);
  1035. ret = kvm_vgic_map_phys_irq(vcpu,
  1036. map.direct_vtimer->host_timer_irq,
  1037. map.direct_vtimer->irq.irq,
  1038. &arch_timer_irq_ops);
  1039. if (ret)
  1040. return ret;
  1041. if (map.direct_ptimer) {
  1042. ret = kvm_vgic_map_phys_irq(vcpu,
  1043. map.direct_ptimer->host_timer_irq,
  1044. map.direct_ptimer->irq.irq,
  1045. &arch_timer_irq_ops);
  1046. }
  1047. if (ret)
  1048. return ret;
  1049. no_vgic:
  1050. timer->enabled = 1;
  1051. return 0;
  1052. }
  1053. /*
  1054. * On VHE system, we only need to configure the EL2 timer trap register once,
  1055. * not for every world switch.
  1056. * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
  1057. * and this makes those bits have no effect for the host kernel execution.
  1058. */
  1059. void kvm_timer_init_vhe(void)
  1060. {
  1061. /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
  1062. u32 cnthctl_shift = 10;
  1063. u64 val;
  1064. /*
  1065. * VHE systems allow the guest direct access to the EL1 physical
  1066. * timer/counter.
  1067. */
  1068. val = read_sysreg(cnthctl_el2);
  1069. val |= (CNTHCTL_EL1PCEN << cnthctl_shift);
  1070. val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
  1071. write_sysreg(val, cnthctl_el2);
  1072. }
  1073. static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
  1074. {
  1075. struct kvm_vcpu *vcpu;
  1076. unsigned long i;
  1077. kvm_for_each_vcpu(i, vcpu, kvm) {
  1078. vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
  1079. vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
  1080. }
  1081. }
  1082. int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  1083. {
  1084. int __user *uaddr = (int __user *)(long)attr->addr;
  1085. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  1086. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  1087. int irq;
  1088. if (!irqchip_in_kernel(vcpu->kvm))
  1089. return -EINVAL;
  1090. if (get_user(irq, uaddr))
  1091. return -EFAULT;
  1092. if (!(irq_is_ppi(irq)))
  1093. return -EINVAL;
  1094. if (vcpu->arch.timer_cpu.enabled)
  1095. return -EBUSY;
  1096. switch (attr->attr) {
  1097. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  1098. set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
  1099. break;
  1100. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  1101. set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
  1102. break;
  1103. default:
  1104. return -ENXIO;
  1105. }
  1106. return 0;
  1107. }
  1108. int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  1109. {
  1110. int __user *uaddr = (int __user *)(long)attr->addr;
  1111. struct arch_timer_context *timer;
  1112. int irq;
  1113. switch (attr->attr) {
  1114. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  1115. timer = vcpu_vtimer(vcpu);
  1116. break;
  1117. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  1118. timer = vcpu_ptimer(vcpu);
  1119. break;
  1120. default:
  1121. return -ENXIO;
  1122. }
  1123. irq = timer->irq.irq;
  1124. return put_user(irq, uaddr);
  1125. }
  1126. int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  1127. {
  1128. switch (attr->attr) {
  1129. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  1130. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  1131. return 0;
  1132. }
  1133. return -ENXIO;
  1134. }