arch_timer.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/cpu.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/uaccess.h>
  24. #include <clocksource/arm_arch_timer.h>
  25. #include <asm/arch_timer.h>
  26. #include <asm/kvm_hyp.h>
  27. #include <kvm/arm_vgic.h>
  28. #include <kvm/arm_arch_timer.h>
  29. #include "trace.h"
  30. static struct timecounter *timecounter;
  31. static unsigned int host_vtimer_irq;
  32. static u32 host_vtimer_irq_flags;
  33. static DEFINE_STATIC_KEY_FALSE(has_gic_active_state);
  34. static const struct kvm_irq_level default_ptimer_irq = {
  35. .irq = 30,
  36. .level = 1,
  37. };
  38. static const struct kvm_irq_level default_vtimer_irq = {
  39. .irq = 27,
  40. .level = 1,
  41. };
  42. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx);
  43. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  44. struct arch_timer_context *timer_ctx);
  45. static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
  46. u64 kvm_phys_timer_read(void)
  47. {
  48. return timecounter->cc->read(timecounter->cc);
  49. }
  50. static inline bool userspace_irqchip(struct kvm *kvm)
  51. {
  52. return static_branch_unlikely(&userspace_irqchip_in_use) &&
  53. unlikely(!irqchip_in_kernel(kvm));
  54. }
  55. static void soft_timer_start(struct hrtimer *hrt, u64 ns)
  56. {
  57. hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
  58. HRTIMER_MODE_ABS);
  59. }
  60. static void soft_timer_cancel(struct hrtimer *hrt, struct work_struct *work)
  61. {
  62. hrtimer_cancel(hrt);
  63. if (work)
  64. cancel_work_sync(work);
  65. }
  66. static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
  67. {
  68. struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
  69. struct arch_timer_context *vtimer;
  70. /*
  71. * We may see a timer interrupt after vcpu_put() has been called which
  72. * sets the CPU's vcpu pointer to NULL, because even though the timer
  73. * has been disabled in vtimer_save_state(), the hardware interrupt
  74. * signal may not have been retired from the interrupt controller yet.
  75. */
  76. if (!vcpu)
  77. return IRQ_HANDLED;
  78. vtimer = vcpu_vtimer(vcpu);
  79. if (kvm_timer_should_fire(vtimer))
  80. kvm_timer_update_irq(vcpu, true, vtimer);
  81. if (userspace_irqchip(vcpu->kvm) &&
  82. !static_branch_unlikely(&has_gic_active_state))
  83. disable_percpu_irq(host_vtimer_irq);
  84. return IRQ_HANDLED;
  85. }
  86. /*
  87. * Work function for handling the backup timer that we schedule when a vcpu is
  88. * no longer running, but had a timer programmed to fire in the future.
  89. */
  90. static void kvm_timer_inject_irq_work(struct work_struct *work)
  91. {
  92. struct kvm_vcpu *vcpu;
  93. vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
  94. /*
  95. * If the vcpu is blocked we want to wake it up so that it will see
  96. * the timer has expired when entering the guest.
  97. */
  98. kvm_vcpu_wake_up(vcpu);
  99. }
  100. static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
  101. {
  102. u64 cval, now;
  103. cval = timer_ctx->cnt_cval;
  104. now = kvm_phys_timer_read() - timer_ctx->cntvoff;
  105. if (now < cval) {
  106. u64 ns;
  107. ns = cyclecounter_cyc2ns(timecounter->cc,
  108. cval - now,
  109. timecounter->mask,
  110. &timecounter->frac);
  111. return ns;
  112. }
  113. return 0;
  114. }
  115. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
  116. {
  117. return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
  118. (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE);
  119. }
  120. /*
  121. * Returns the earliest expiration time in ns among guest timers.
  122. * Note that it will return 0 if none of timers can fire.
  123. */
  124. static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
  125. {
  126. u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX;
  127. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  128. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  129. if (kvm_timer_irq_can_fire(vtimer))
  130. min_virt = kvm_timer_compute_delta(vtimer);
  131. if (kvm_timer_irq_can_fire(ptimer))
  132. min_phys = kvm_timer_compute_delta(ptimer);
  133. /* If none of timers can fire, then return 0 */
  134. if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX))
  135. return 0;
  136. return min(min_virt, min_phys);
  137. }
  138. static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
  139. {
  140. struct arch_timer_cpu *timer;
  141. struct kvm_vcpu *vcpu;
  142. u64 ns;
  143. timer = container_of(hrt, struct arch_timer_cpu, bg_timer);
  144. vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
  145. /*
  146. * Check that the timer has really expired from the guest's
  147. * PoV (NTP on the host may have forced it to expire
  148. * early). If we should have slept longer, restart it.
  149. */
  150. ns = kvm_timer_earliest_exp(vcpu);
  151. if (unlikely(ns)) {
  152. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  153. return HRTIMER_RESTART;
  154. }
  155. schedule_work(&timer->expired);
  156. return HRTIMER_NORESTART;
  157. }
  158. static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt)
  159. {
  160. struct arch_timer_context *ptimer;
  161. struct arch_timer_cpu *timer;
  162. struct kvm_vcpu *vcpu;
  163. u64 ns;
  164. timer = container_of(hrt, struct arch_timer_cpu, phys_timer);
  165. vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
  166. ptimer = vcpu_ptimer(vcpu);
  167. /*
  168. * Check that the timer has really expired from the guest's
  169. * PoV (NTP on the host may have forced it to expire
  170. * early). If not ready, schedule for a later time.
  171. */
  172. ns = kvm_timer_compute_delta(ptimer);
  173. if (unlikely(ns)) {
  174. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  175. return HRTIMER_RESTART;
  176. }
  177. kvm_timer_update_irq(vcpu, true, ptimer);
  178. return HRTIMER_NORESTART;
  179. }
  180. static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
  181. {
  182. u64 cval, now;
  183. if (timer_ctx->loaded) {
  184. u32 cnt_ctl;
  185. /* Only the virtual timer can be loaded so far */
  186. cnt_ctl = read_sysreg_el0(cntv_ctl);
  187. return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) &&
  188. (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) &&
  189. !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK);
  190. }
  191. if (!kvm_timer_irq_can_fire(timer_ctx))
  192. return false;
  193. cval = timer_ctx->cnt_cval;
  194. now = kvm_phys_timer_read() - timer_ctx->cntvoff;
  195. return cval <= now;
  196. }
  197. bool kvm_timer_is_pending(struct kvm_vcpu *vcpu)
  198. {
  199. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  200. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  201. if (kvm_timer_should_fire(vtimer))
  202. return true;
  203. return kvm_timer_should_fire(ptimer);
  204. }
  205. /*
  206. * Reflect the timer output level into the kvm_run structure
  207. */
  208. void kvm_timer_update_run(struct kvm_vcpu *vcpu)
  209. {
  210. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  211. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  212. struct kvm_sync_regs *regs = &vcpu->run->s.regs;
  213. /* Populate the device bitmap with the timer states */
  214. regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
  215. KVM_ARM_DEV_EL1_PTIMER);
  216. if (kvm_timer_should_fire(vtimer))
  217. regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
  218. if (kvm_timer_should_fire(ptimer))
  219. regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
  220. }
  221. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  222. struct arch_timer_context *timer_ctx)
  223. {
  224. int ret;
  225. timer_ctx->irq.level = new_level;
  226. trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
  227. timer_ctx->irq.level);
  228. if (!userspace_irqchip(vcpu->kvm)) {
  229. ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
  230. timer_ctx->irq.irq,
  231. timer_ctx->irq.level,
  232. timer_ctx);
  233. WARN_ON(ret);
  234. }
  235. }
  236. /* Schedule the background timer for the emulated timer. */
  237. static void phys_timer_emulate(struct kvm_vcpu *vcpu)
  238. {
  239. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  240. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  241. /*
  242. * If the timer can fire now, we don't need to have a soft timer
  243. * scheduled for the future. If the timer cannot fire at all,
  244. * then we also don't need a soft timer.
  245. */
  246. if (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) {
  247. soft_timer_cancel(&timer->phys_timer, NULL);
  248. return;
  249. }
  250. soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(ptimer));
  251. }
  252. /*
  253. * Check if there was a change in the timer state, so that we should either
  254. * raise or lower the line level to the GIC or schedule a background timer to
  255. * emulate the physical timer.
  256. */
  257. static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
  258. {
  259. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  260. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  261. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  262. bool level;
  263. if (unlikely(!timer->enabled))
  264. return;
  265. /*
  266. * The vtimer virtual interrupt is a 'mapped' interrupt, meaning part
  267. * of its lifecycle is offloaded to the hardware, and we therefore may
  268. * not have lowered the irq.level value before having to signal a new
  269. * interrupt, but have to signal an interrupt every time the level is
  270. * asserted.
  271. */
  272. level = kvm_timer_should_fire(vtimer);
  273. kvm_timer_update_irq(vcpu, level, vtimer);
  274. phys_timer_emulate(vcpu);
  275. if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
  276. kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
  277. }
  278. static void vtimer_save_state(struct kvm_vcpu *vcpu)
  279. {
  280. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  281. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  282. unsigned long flags;
  283. local_irq_save(flags);
  284. if (!vtimer->loaded)
  285. goto out;
  286. if (timer->enabled) {
  287. vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl);
  288. vtimer->cnt_cval = read_sysreg_el0(cntv_cval);
  289. }
  290. /* Disable the virtual timer */
  291. write_sysreg_el0(0, cntv_ctl);
  292. isb();
  293. vtimer->loaded = false;
  294. out:
  295. local_irq_restore(flags);
  296. }
  297. /*
  298. * Schedule the background timer before calling kvm_vcpu_block, so that this
  299. * thread is removed from its waitqueue and made runnable when there's a timer
  300. * interrupt to handle.
  301. */
  302. void kvm_timer_schedule(struct kvm_vcpu *vcpu)
  303. {
  304. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  305. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  306. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  307. vtimer_save_state(vcpu);
  308. /*
  309. * No need to schedule a background timer if any guest timer has
  310. * already expired, because kvm_vcpu_block will return before putting
  311. * the thread to sleep.
  312. */
  313. if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer))
  314. return;
  315. /*
  316. * If both timers are not capable of raising interrupts (disabled or
  317. * masked), then there's no more work for us to do.
  318. */
  319. if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer))
  320. return;
  321. /*
  322. * The guest timers have not yet expired, schedule a background timer.
  323. * Set the earliest expiration time among the guest timers.
  324. */
  325. soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
  326. }
  327. static void vtimer_restore_state(struct kvm_vcpu *vcpu)
  328. {
  329. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  330. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  331. unsigned long flags;
  332. local_irq_save(flags);
  333. if (vtimer->loaded)
  334. goto out;
  335. if (timer->enabled) {
  336. write_sysreg_el0(vtimer->cnt_cval, cntv_cval);
  337. isb();
  338. write_sysreg_el0(vtimer->cnt_ctl, cntv_ctl);
  339. }
  340. vtimer->loaded = true;
  341. out:
  342. local_irq_restore(flags);
  343. }
  344. void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
  345. {
  346. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  347. vtimer_restore_state(vcpu);
  348. soft_timer_cancel(&timer->bg_timer, &timer->expired);
  349. }
  350. static void set_cntvoff(u64 cntvoff)
  351. {
  352. u32 low = lower_32_bits(cntvoff);
  353. u32 high = upper_32_bits(cntvoff);
  354. /*
  355. * Since kvm_call_hyp doesn't fully support the ARM PCS especially on
  356. * 32-bit systems, but rather passes register by register shifted one
  357. * place (we put the function address in r0/x0), we cannot simply pass
  358. * a 64-bit value as an argument, but have to split the value in two
  359. * 32-bit halves.
  360. */
  361. kvm_call_hyp(__kvm_timer_set_cntvoff, low, high);
  362. }
  363. static inline void set_vtimer_irq_phys_active(struct kvm_vcpu *vcpu, bool active)
  364. {
  365. int r;
  366. r = irq_set_irqchip_state(host_vtimer_irq, IRQCHIP_STATE_ACTIVE, active);
  367. WARN_ON(r);
  368. }
  369. static void kvm_timer_vcpu_load_gic(struct kvm_vcpu *vcpu)
  370. {
  371. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  372. bool phys_active;
  373. if (irqchip_in_kernel(vcpu->kvm))
  374. phys_active = kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
  375. else
  376. phys_active = vtimer->irq.level;
  377. set_vtimer_irq_phys_active(vcpu, phys_active);
  378. }
  379. static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu)
  380. {
  381. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  382. /*
  383. * When using a userspace irqchip with the architected timers and a
  384. * host interrupt controller that doesn't support an active state, we
  385. * must still prevent continuously exiting from the guest, and
  386. * therefore mask the physical interrupt by disabling it on the host
  387. * interrupt controller when the virtual level is high, such that the
  388. * guest can make forward progress. Once we detect the output level
  389. * being de-asserted, we unmask the interrupt again so that we exit
  390. * from the guest when the timer fires.
  391. */
  392. if (vtimer->irq.level)
  393. disable_percpu_irq(host_vtimer_irq);
  394. else
  395. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  396. }
  397. void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
  398. {
  399. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  400. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  401. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  402. if (unlikely(!timer->enabled))
  403. return;
  404. if (static_branch_likely(&has_gic_active_state))
  405. kvm_timer_vcpu_load_gic(vcpu);
  406. else
  407. kvm_timer_vcpu_load_nogic(vcpu);
  408. set_cntvoff(vtimer->cntvoff);
  409. vtimer_restore_state(vcpu);
  410. /* Set the background timer for the physical timer emulation. */
  411. phys_timer_emulate(vcpu);
  412. /* If the timer fired while we weren't running, inject it now */
  413. if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
  414. kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
  415. }
  416. bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
  417. {
  418. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  419. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  420. struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
  421. bool vlevel, plevel;
  422. if (likely(irqchip_in_kernel(vcpu->kvm)))
  423. return false;
  424. vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
  425. plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
  426. return kvm_timer_should_fire(vtimer) != vlevel ||
  427. kvm_timer_should_fire(ptimer) != plevel;
  428. }
  429. void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
  430. {
  431. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  432. if (unlikely(!timer->enabled))
  433. return;
  434. vtimer_save_state(vcpu);
  435. /*
  436. * Cancel the physical timer emulation, because the only case where we
  437. * need it after a vcpu_put is in the context of a sleeping VCPU, and
  438. * in that case we already factor in the deadline for the physical
  439. * timer when scheduling the bg_timer.
  440. *
  441. * In any case, we re-schedule the hrtimer for the physical timer when
  442. * coming back to the VCPU thread in kvm_timer_vcpu_load().
  443. */
  444. soft_timer_cancel(&timer->phys_timer, NULL);
  445. /*
  446. * The kernel may decide to run userspace after calling vcpu_put, so
  447. * we reset cntvoff to 0 to ensure a consistent read between user
  448. * accesses to the virtual counter and kernel access to the physical
  449. * counter of non-VHE case. For VHE, the virtual counter uses a fixed
  450. * virtual offset of zero, so no need to zero CNTVOFF_EL2 register.
  451. */
  452. if (!has_vhe())
  453. set_cntvoff(0);
  454. }
  455. /*
  456. * With a userspace irqchip we have to check if the guest de-asserted the
  457. * timer and if so, unmask the timer irq signal on the host interrupt
  458. * controller to ensure that we see future timer signals.
  459. */
  460. static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
  461. {
  462. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  463. if (!kvm_timer_should_fire(vtimer)) {
  464. kvm_timer_update_irq(vcpu, false, vtimer);
  465. if (static_branch_likely(&has_gic_active_state))
  466. set_vtimer_irq_phys_active(vcpu, false);
  467. else
  468. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  469. }
  470. }
  471. void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
  472. {
  473. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  474. if (unlikely(!timer->enabled))
  475. return;
  476. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  477. unmask_vtimer_irq_user(vcpu);
  478. }
  479. int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
  480. {
  481. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  482. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  483. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  484. /*
  485. * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
  486. * and to 0 for ARMv7. We provide an implementation that always
  487. * resets the timer to be disabled and unmasked and is compliant with
  488. * the ARMv7 architecture.
  489. */
  490. vtimer->cnt_ctl = 0;
  491. ptimer->cnt_ctl = 0;
  492. kvm_timer_update_state(vcpu);
  493. if (timer->enabled && irqchip_in_kernel(vcpu->kvm))
  494. kvm_vgic_reset_mapped_irq(vcpu, vtimer->irq.irq);
  495. return 0;
  496. }
  497. /* Make the updates of cntvoff for all vtimer contexts atomic */
  498. static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
  499. {
  500. int i;
  501. struct kvm *kvm = vcpu->kvm;
  502. struct kvm_vcpu *tmp;
  503. mutex_lock(&kvm->lock);
  504. kvm_for_each_vcpu(i, tmp, kvm)
  505. vcpu_vtimer(tmp)->cntvoff = cntvoff;
  506. /*
  507. * When called from the vcpu create path, the CPU being created is not
  508. * included in the loop above, so we just set it here as well.
  509. */
  510. vcpu_vtimer(vcpu)->cntvoff = cntvoff;
  511. mutex_unlock(&kvm->lock);
  512. }
  513. void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
  514. {
  515. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  516. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  517. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  518. /* Synchronize cntvoff across all vtimers of a VM. */
  519. update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
  520. vcpu_ptimer(vcpu)->cntvoff = 0;
  521. INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
  522. hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  523. timer->bg_timer.function = kvm_bg_timer_expire;
  524. hrtimer_init(&timer->phys_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  525. timer->phys_timer.function = kvm_phys_timer_expire;
  526. vtimer->irq.irq = default_vtimer_irq.irq;
  527. ptimer->irq.irq = default_ptimer_irq.irq;
  528. }
  529. static void kvm_timer_init_interrupt(void *info)
  530. {
  531. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  532. }
  533. int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
  534. {
  535. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  536. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  537. switch (regid) {
  538. case KVM_REG_ARM_TIMER_CTL:
  539. vtimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT;
  540. break;
  541. case KVM_REG_ARM_TIMER_CNT:
  542. update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
  543. break;
  544. case KVM_REG_ARM_TIMER_CVAL:
  545. vtimer->cnt_cval = value;
  546. break;
  547. case KVM_REG_ARM_PTIMER_CTL:
  548. ptimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT;
  549. break;
  550. case KVM_REG_ARM_PTIMER_CVAL:
  551. ptimer->cnt_cval = value;
  552. break;
  553. default:
  554. return -1;
  555. }
  556. kvm_timer_update_state(vcpu);
  557. return 0;
  558. }
  559. static u64 read_timer_ctl(struct arch_timer_context *timer)
  560. {
  561. /*
  562. * Set ISTATUS bit if it's expired.
  563. * Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is
  564. * UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit
  565. * regardless of ENABLE bit for our implementation convenience.
  566. */
  567. if (!kvm_timer_compute_delta(timer))
  568. return timer->cnt_ctl | ARCH_TIMER_CTRL_IT_STAT;
  569. else
  570. return timer->cnt_ctl;
  571. }
  572. u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
  573. {
  574. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  575. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  576. switch (regid) {
  577. case KVM_REG_ARM_TIMER_CTL:
  578. return read_timer_ctl(vtimer);
  579. case KVM_REG_ARM_TIMER_CNT:
  580. return kvm_phys_timer_read() - vtimer->cntvoff;
  581. case KVM_REG_ARM_TIMER_CVAL:
  582. return vtimer->cnt_cval;
  583. case KVM_REG_ARM_PTIMER_CTL:
  584. return read_timer_ctl(ptimer);
  585. case KVM_REG_ARM_PTIMER_CVAL:
  586. return ptimer->cnt_cval;
  587. case KVM_REG_ARM_PTIMER_CNT:
  588. return kvm_phys_timer_read();
  589. }
  590. return (u64)-1;
  591. }
  592. static int kvm_timer_starting_cpu(unsigned int cpu)
  593. {
  594. kvm_timer_init_interrupt(NULL);
  595. return 0;
  596. }
  597. static int kvm_timer_dying_cpu(unsigned int cpu)
  598. {
  599. disable_percpu_irq(host_vtimer_irq);
  600. return 0;
  601. }
  602. int kvm_timer_hyp_init(bool has_gic)
  603. {
  604. struct arch_timer_kvm_info *info;
  605. int err;
  606. info = arch_timer_get_kvm_info();
  607. timecounter = &info->timecounter;
  608. if (!timecounter->cc) {
  609. kvm_err("kvm_arch_timer: uninitialized timecounter\n");
  610. return -ENODEV;
  611. }
  612. if (info->virtual_irq <= 0) {
  613. kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
  614. info->virtual_irq);
  615. return -ENODEV;
  616. }
  617. host_vtimer_irq = info->virtual_irq;
  618. host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
  619. if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
  620. host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
  621. kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
  622. host_vtimer_irq);
  623. host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
  624. }
  625. err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
  626. "kvm guest timer", kvm_get_running_vcpus());
  627. if (err) {
  628. kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
  629. host_vtimer_irq, err);
  630. return err;
  631. }
  632. if (has_gic) {
  633. err = irq_set_vcpu_affinity(host_vtimer_irq,
  634. kvm_get_running_vcpus());
  635. if (err) {
  636. kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
  637. goto out_free_irq;
  638. }
  639. static_branch_enable(&has_gic_active_state);
  640. }
  641. kvm_debug("virtual timer IRQ%d\n", host_vtimer_irq);
  642. cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
  643. "kvm/arm/timer:starting", kvm_timer_starting_cpu,
  644. kvm_timer_dying_cpu);
  645. return 0;
  646. out_free_irq:
  647. free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
  648. return err;
  649. }
  650. void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
  651. {
  652. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  653. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  654. soft_timer_cancel(&timer->bg_timer, &timer->expired);
  655. soft_timer_cancel(&timer->phys_timer, NULL);
  656. kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
  657. }
  658. static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
  659. {
  660. int vtimer_irq, ptimer_irq;
  661. int i, ret;
  662. vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
  663. ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
  664. if (ret)
  665. return false;
  666. ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
  667. ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
  668. if (ret)
  669. return false;
  670. kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
  671. if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
  672. vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
  673. return false;
  674. }
  675. return true;
  676. }
  677. bool kvm_arch_timer_get_input_level(int vintid)
  678. {
  679. struct kvm_vcpu *vcpu = kvm_arm_get_running_vcpu();
  680. struct arch_timer_context *timer;
  681. if (vintid == vcpu_vtimer(vcpu)->irq.irq)
  682. timer = vcpu_vtimer(vcpu);
  683. else
  684. BUG(); /* We only map the vtimer so far */
  685. return kvm_timer_should_fire(timer);
  686. }
  687. int kvm_timer_enable(struct kvm_vcpu *vcpu)
  688. {
  689. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  690. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  691. int ret;
  692. if (timer->enabled)
  693. return 0;
  694. /* Without a VGIC we do not map virtual IRQs to physical IRQs */
  695. if (!irqchip_in_kernel(vcpu->kvm))
  696. goto no_vgic;
  697. if (!vgic_initialized(vcpu->kvm))
  698. return -ENODEV;
  699. if (!timer_irqs_are_valid(vcpu)) {
  700. kvm_debug("incorrectly configured timer irqs\n");
  701. return -EINVAL;
  702. }
  703. ret = kvm_vgic_map_phys_irq(vcpu, host_vtimer_irq, vtimer->irq.irq,
  704. kvm_arch_timer_get_input_level);
  705. if (ret)
  706. return ret;
  707. no_vgic:
  708. timer->enabled = 1;
  709. return 0;
  710. }
  711. /*
  712. * On VHE system, we only need to configure trap on physical timer and counter
  713. * accesses in EL0 and EL1 once, not for every world switch.
  714. * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
  715. * and this makes those bits have no effect for the host kernel execution.
  716. */
  717. void kvm_timer_init_vhe(void)
  718. {
  719. /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
  720. u32 cnthctl_shift = 10;
  721. u64 val;
  722. /*
  723. * Disallow physical timer access for the guest.
  724. * Physical counter access is allowed.
  725. */
  726. val = read_sysreg(cnthctl_el2);
  727. val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift);
  728. val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
  729. write_sysreg(val, cnthctl_el2);
  730. }
  731. static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
  732. {
  733. struct kvm_vcpu *vcpu;
  734. int i;
  735. kvm_for_each_vcpu(i, vcpu, kvm) {
  736. vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
  737. vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
  738. }
  739. }
  740. int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  741. {
  742. int __user *uaddr = (int __user *)(long)attr->addr;
  743. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  744. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  745. int irq;
  746. if (!irqchip_in_kernel(vcpu->kvm))
  747. return -EINVAL;
  748. if (get_user(irq, uaddr))
  749. return -EFAULT;
  750. if (!(irq_is_ppi(irq)))
  751. return -EINVAL;
  752. if (vcpu->arch.timer_cpu.enabled)
  753. return -EBUSY;
  754. switch (attr->attr) {
  755. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  756. set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
  757. break;
  758. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  759. set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
  760. break;
  761. default:
  762. return -ENXIO;
  763. }
  764. return 0;
  765. }
  766. int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  767. {
  768. int __user *uaddr = (int __user *)(long)attr->addr;
  769. struct arch_timer_context *timer;
  770. int irq;
  771. switch (attr->attr) {
  772. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  773. timer = vcpu_vtimer(vcpu);
  774. break;
  775. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  776. timer = vcpu_ptimer(vcpu);
  777. break;
  778. default:
  779. return -ENXIO;
  780. }
  781. irq = timer->irq.irq;
  782. return put_user(irq, uaddr);
  783. }
  784. int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  785. {
  786. switch (attr->attr) {
  787. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  788. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  789. return 0;
  790. }
  791. return -ENXIO;
  792. }