arch_timer.c 25 KB

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