arch_timer.c 12 KB

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