arch_timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/of_irq.h>
  20. #include <linux/kvm.h>
  21. #include <linux/kvm_host.h>
  22. #include <linux/interrupt.h>
  23. #include <clocksource/arm_arch_timer.h>
  24. #include <asm/arch_timer.h>
  25. #include <kvm/arm_vgic.h>
  26. #include <kvm/arm_arch_timer.h>
  27. #include "trace.h"
  28. static struct timecounter *timecounter;
  29. static struct workqueue_struct *wqueue;
  30. static unsigned int host_vtimer_irq;
  31. static cycle_t kvm_phys_timer_read(void)
  32. {
  33. return timecounter->cc->read(timecounter->cc);
  34. }
  35. static bool timer_is_armed(struct arch_timer_cpu *timer)
  36. {
  37. return timer->armed;
  38. }
  39. /* timer_arm: as in "arm the timer", not as in ARM the company */
  40. static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
  41. {
  42. timer->armed = true;
  43. hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
  44. HRTIMER_MODE_ABS);
  45. }
  46. static void timer_disarm(struct arch_timer_cpu *timer)
  47. {
  48. if (timer_is_armed(timer)) {
  49. hrtimer_cancel(&timer->timer);
  50. cancel_work_sync(&timer->expired);
  51. timer->armed = false;
  52. }
  53. }
  54. static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
  55. {
  56. struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
  57. /*
  58. * We disable the timer in the world switch and let it be
  59. * handled by kvm_timer_sync_hwstate(). Getting a timer
  60. * interrupt at this point is a sure sign of some major
  61. * breakage.
  62. */
  63. pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
  64. return IRQ_HANDLED;
  65. }
  66. /*
  67. * Work function for handling the backup timer that we schedule when a vcpu is
  68. * no longer running, but had a timer programmed to fire in the future.
  69. */
  70. static void kvm_timer_inject_irq_work(struct work_struct *work)
  71. {
  72. struct kvm_vcpu *vcpu;
  73. vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
  74. /*
  75. * If the vcpu is blocked we want to wake it up so that it will see
  76. * the timer has expired when entering the guest.
  77. */
  78. kvm_vcpu_kick(vcpu);
  79. }
  80. static u64 kvm_timer_compute_delta(struct kvm_vcpu *vcpu)
  81. {
  82. cycle_t cval, now;
  83. cval = vcpu->arch.timer_cpu.cntv_cval;
  84. now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
  85. if (now < cval) {
  86. u64 ns;
  87. ns = cyclecounter_cyc2ns(timecounter->cc,
  88. cval - now,
  89. timecounter->mask,
  90. &timecounter->frac);
  91. return ns;
  92. }
  93. return 0;
  94. }
  95. static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
  96. {
  97. struct arch_timer_cpu *timer;
  98. struct kvm_vcpu *vcpu;
  99. u64 ns;
  100. timer = container_of(hrt, struct arch_timer_cpu, timer);
  101. vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
  102. /*
  103. * Check that the timer has really expired from the guest's
  104. * PoV (NTP on the host may have forced it to expire
  105. * early). If we should have slept longer, restart it.
  106. */
  107. ns = kvm_timer_compute_delta(vcpu);
  108. if (unlikely(ns)) {
  109. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  110. return HRTIMER_RESTART;
  111. }
  112. queue_work(wqueue, &timer->expired);
  113. return HRTIMER_NORESTART;
  114. }
  115. static bool kvm_timer_irq_can_fire(struct kvm_vcpu *vcpu)
  116. {
  117. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  118. return !(timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
  119. (timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE);
  120. }
  121. bool kvm_timer_should_fire(struct kvm_vcpu *vcpu)
  122. {
  123. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  124. cycle_t cval, now;
  125. if (!kvm_timer_irq_can_fire(vcpu))
  126. return false;
  127. cval = timer->cntv_cval;
  128. now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
  129. return cval <= now;
  130. }
  131. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level)
  132. {
  133. int ret;
  134. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  135. BUG_ON(!vgic_initialized(vcpu->kvm));
  136. timer->irq.level = new_level;
  137. trace_kvm_timer_update_irq(vcpu->vcpu_id, timer->map->virt_irq,
  138. timer->irq.level);
  139. ret = kvm_vgic_inject_mapped_irq(vcpu->kvm, vcpu->vcpu_id,
  140. timer->map,
  141. timer->irq.level);
  142. WARN_ON(ret);
  143. }
  144. /*
  145. * Check if there was a change in the timer state (should we raise or lower
  146. * the line level to the GIC).
  147. */
  148. static int kvm_timer_update_state(struct kvm_vcpu *vcpu)
  149. {
  150. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  151. /*
  152. * If userspace modified the timer registers via SET_ONE_REG before
  153. * the vgic was initialized, we mustn't set the timer->irq.level value
  154. * because the guest would never see the interrupt. Instead wait
  155. * until we call this function from kvm_timer_flush_hwstate.
  156. */
  157. if (!vgic_initialized(vcpu->kvm))
  158. return -ENODEV;
  159. if (kvm_timer_should_fire(vcpu) != timer->irq.level)
  160. kvm_timer_update_irq(vcpu, !timer->irq.level);
  161. return 0;
  162. }
  163. /*
  164. * Schedule the background timer before calling kvm_vcpu_block, so that this
  165. * thread is removed from its waitqueue and made runnable when there's a timer
  166. * interrupt to handle.
  167. */
  168. void kvm_timer_schedule(struct kvm_vcpu *vcpu)
  169. {
  170. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  171. BUG_ON(timer_is_armed(timer));
  172. /*
  173. * No need to schedule a background timer if the guest timer has
  174. * already expired, because kvm_vcpu_block will return before putting
  175. * the thread to sleep.
  176. */
  177. if (kvm_timer_should_fire(vcpu))
  178. return;
  179. /*
  180. * If the timer is not capable of raising interrupts (disabled or
  181. * masked), then there's no more work for us to do.
  182. */
  183. if (!kvm_timer_irq_can_fire(vcpu))
  184. return;
  185. /* The timer has not yet expired, schedule a background timer */
  186. timer_arm(timer, kvm_timer_compute_delta(vcpu));
  187. }
  188. void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
  189. {
  190. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  191. timer_disarm(timer);
  192. }
  193. /**
  194. * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
  195. * @vcpu: The vcpu pointer
  196. *
  197. * Check if the virtual timer has expired while we were running in the host,
  198. * and inject an interrupt if that was the case.
  199. */
  200. void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
  201. {
  202. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  203. bool phys_active;
  204. int ret;
  205. if (kvm_timer_update_state(vcpu))
  206. return;
  207. /*
  208. * If we enter the guest with the virtual input level to the VGIC
  209. * asserted, then we have already told the VGIC what we need to, and
  210. * we don't need to exit from the guest until the guest deactivates
  211. * the already injected interrupt, so therefore we should set the
  212. * hardware active state to prevent unnecessary exits from the guest.
  213. *
  214. * Also, if we enter the guest with the virtual timer interrupt active,
  215. * then it must be active on the physical distributor, because we set
  216. * the HW bit and the guest must be able to deactivate the virtual and
  217. * physical interrupt at the same time.
  218. *
  219. * Conversely, if the virtual input level is deasserted and the virtual
  220. * interrupt is not active, then always clear the hardware active state
  221. * to ensure that hardware interrupts from the timer triggers a guest
  222. * exit.
  223. */
  224. if (timer->irq.level || kvm_vgic_map_is_active(vcpu, timer->map))
  225. phys_active = true;
  226. else
  227. phys_active = false;
  228. ret = irq_set_irqchip_state(timer->map->irq,
  229. IRQCHIP_STATE_ACTIVE,
  230. phys_active);
  231. WARN_ON(ret);
  232. }
  233. /**
  234. * kvm_timer_sync_hwstate - sync timer state from cpu
  235. * @vcpu: The vcpu pointer
  236. *
  237. * Check if the virtual timer has expired while we were running in the guest,
  238. * and inject an interrupt if that was the case.
  239. */
  240. void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
  241. {
  242. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  243. BUG_ON(timer_is_armed(timer));
  244. /*
  245. * The guest could have modified the timer registers or the timer
  246. * could have expired, update the timer state.
  247. */
  248. kvm_timer_update_state(vcpu);
  249. }
  250. int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
  251. const struct kvm_irq_level *irq)
  252. {
  253. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  254. struct irq_phys_map *map;
  255. /*
  256. * The vcpu timer irq number cannot be determined in
  257. * kvm_timer_vcpu_init() because it is called much before
  258. * kvm_vcpu_set_target(). To handle this, we determine
  259. * vcpu timer irq number when the vcpu is reset.
  260. */
  261. timer->irq.irq = irq->irq;
  262. /*
  263. * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
  264. * and to 0 for ARMv7. We provide an implementation that always
  265. * resets the timer to be disabled and unmasked and is compliant with
  266. * the ARMv7 architecture.
  267. */
  268. timer->cntv_ctl = 0;
  269. kvm_timer_update_state(vcpu);
  270. /*
  271. * Tell the VGIC that the virtual interrupt is tied to a
  272. * physical interrupt. We do that once per VCPU.
  273. */
  274. map = kvm_vgic_map_phys_irq(vcpu, irq->irq, host_vtimer_irq);
  275. if (WARN_ON(IS_ERR(map)))
  276. return PTR_ERR(map);
  277. timer->map = map;
  278. return 0;
  279. }
  280. void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
  281. {
  282. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  283. INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
  284. hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  285. timer->timer.function = kvm_timer_expire;
  286. }
  287. static void kvm_timer_init_interrupt(void *info)
  288. {
  289. enable_percpu_irq(host_vtimer_irq, 0);
  290. }
  291. int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
  292. {
  293. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  294. switch (regid) {
  295. case KVM_REG_ARM_TIMER_CTL:
  296. timer->cntv_ctl = value;
  297. break;
  298. case KVM_REG_ARM_TIMER_CNT:
  299. vcpu->kvm->arch.timer.cntvoff = kvm_phys_timer_read() - value;
  300. break;
  301. case KVM_REG_ARM_TIMER_CVAL:
  302. timer->cntv_cval = value;
  303. break;
  304. default:
  305. return -1;
  306. }
  307. kvm_timer_update_state(vcpu);
  308. return 0;
  309. }
  310. u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
  311. {
  312. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  313. switch (regid) {
  314. case KVM_REG_ARM_TIMER_CTL:
  315. return timer->cntv_ctl;
  316. case KVM_REG_ARM_TIMER_CNT:
  317. return kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
  318. case KVM_REG_ARM_TIMER_CVAL:
  319. return timer->cntv_cval;
  320. }
  321. return (u64)-1;
  322. }
  323. static int kvm_timer_cpu_notify(struct notifier_block *self,
  324. unsigned long action, void *cpu)
  325. {
  326. switch (action) {
  327. case CPU_STARTING:
  328. case CPU_STARTING_FROZEN:
  329. kvm_timer_init_interrupt(NULL);
  330. break;
  331. case CPU_DYING:
  332. case CPU_DYING_FROZEN:
  333. disable_percpu_irq(host_vtimer_irq);
  334. break;
  335. }
  336. return NOTIFY_OK;
  337. }
  338. static struct notifier_block kvm_timer_cpu_nb = {
  339. .notifier_call = kvm_timer_cpu_notify,
  340. };
  341. static const struct of_device_id arch_timer_of_match[] = {
  342. { .compatible = "arm,armv7-timer", },
  343. { .compatible = "arm,armv8-timer", },
  344. {},
  345. };
  346. int kvm_timer_hyp_init(void)
  347. {
  348. struct device_node *np;
  349. unsigned int ppi;
  350. int err;
  351. timecounter = arch_timer_get_timecounter();
  352. if (!timecounter)
  353. return -ENODEV;
  354. np = of_find_matching_node(NULL, arch_timer_of_match);
  355. if (!np) {
  356. kvm_err("kvm_arch_timer: can't find DT node\n");
  357. return -ENODEV;
  358. }
  359. ppi = irq_of_parse_and_map(np, 2);
  360. if (!ppi) {
  361. kvm_err("kvm_arch_timer: no virtual timer interrupt\n");
  362. err = -EINVAL;
  363. goto out;
  364. }
  365. err = request_percpu_irq(ppi, kvm_arch_timer_handler,
  366. "kvm guest timer", kvm_get_running_vcpus());
  367. if (err) {
  368. kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
  369. ppi, err);
  370. goto out;
  371. }
  372. host_vtimer_irq = ppi;
  373. err = __register_cpu_notifier(&kvm_timer_cpu_nb);
  374. if (err) {
  375. kvm_err("Cannot register timer CPU notifier\n");
  376. goto out_free;
  377. }
  378. wqueue = create_singlethread_workqueue("kvm_arch_timer");
  379. if (!wqueue) {
  380. err = -ENOMEM;
  381. goto out_free;
  382. }
  383. kvm_info("%s IRQ%d\n", np->name, ppi);
  384. on_each_cpu(kvm_timer_init_interrupt, NULL, 1);
  385. goto out;
  386. out_free:
  387. free_percpu_irq(ppi, kvm_get_running_vcpus());
  388. out:
  389. of_node_put(np);
  390. return err;
  391. }
  392. void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
  393. {
  394. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  395. timer_disarm(timer);
  396. if (timer->map)
  397. kvm_vgic_unmap_phys_irq(vcpu, timer->map);
  398. }
  399. void kvm_timer_enable(struct kvm *kvm)
  400. {
  401. if (kvm->arch.timer.enabled)
  402. return;
  403. /*
  404. * There is a potential race here between VCPUs starting for the first
  405. * time, which may be enabling the timer multiple times. That doesn't
  406. * hurt though, because we're just setting a variable to the same
  407. * variable that it already was. The important thing is that all
  408. * VCPUs have the enabled variable set, before entering the guest, if
  409. * the arch timers are enabled.
  410. */
  411. if (timecounter && wqueue)
  412. kvm->arch.timer.enabled = 1;
  413. }
  414. void kvm_timer_init(struct kvm *kvm)
  415. {
  416. kvm->arch.timer.cntvoff = kvm_phys_timer_read();
  417. }