arch_timer.c 24 KB

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