arch_timer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. static struct timecounter *timecounter;
  28. static struct workqueue_struct *wqueue;
  29. static unsigned int host_vtimer_irq;
  30. static cycle_t kvm_phys_timer_read(void)
  31. {
  32. return timecounter->cc->read(timecounter->cc);
  33. }
  34. static bool timer_is_armed(struct arch_timer_cpu *timer)
  35. {
  36. return timer->armed;
  37. }
  38. /* timer_arm: as in "arm the timer", not as in ARM the company */
  39. static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
  40. {
  41. timer->armed = true;
  42. hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
  43. HRTIMER_MODE_ABS);
  44. }
  45. static void timer_disarm(struct arch_timer_cpu *timer)
  46. {
  47. if (timer_is_armed(timer)) {
  48. hrtimer_cancel(&timer->timer);
  49. cancel_work_sync(&timer->expired);
  50. timer->armed = false;
  51. }
  52. }
  53. static void kvm_timer_inject_irq(struct kvm_vcpu *vcpu)
  54. {
  55. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  56. timer->cntv_ctl |= ARCH_TIMER_CTRL_IT_MASK;
  57. kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
  58. timer->irq->irq,
  59. timer->irq->level);
  60. }
  61. static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
  62. {
  63. struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
  64. /*
  65. * We disable the timer in the world switch and let it be
  66. * handled by kvm_timer_sync_hwstate(). Getting a timer
  67. * interrupt at this point is a sure sign of some major
  68. * breakage.
  69. */
  70. pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
  71. return IRQ_HANDLED;
  72. }
  73. static void kvm_timer_inject_irq_work(struct work_struct *work)
  74. {
  75. struct kvm_vcpu *vcpu;
  76. vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
  77. vcpu->arch.timer_cpu.armed = false;
  78. kvm_timer_inject_irq(vcpu);
  79. }
  80. static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
  81. {
  82. struct arch_timer_cpu *timer;
  83. timer = container_of(hrt, struct arch_timer_cpu, timer);
  84. queue_work(wqueue, &timer->expired);
  85. return HRTIMER_NORESTART;
  86. }
  87. /**
  88. * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
  89. * @vcpu: The vcpu pointer
  90. *
  91. * Disarm any pending soft timers, since the world-switch code will write the
  92. * virtual timer state back to the physical CPU.
  93. */
  94. void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
  95. {
  96. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  97. /*
  98. * We're about to run this vcpu again, so there is no need to
  99. * keep the background timer running, as we're about to
  100. * populate the CPU timer again.
  101. */
  102. timer_disarm(timer);
  103. }
  104. /**
  105. * kvm_timer_sync_hwstate - sync timer state from cpu
  106. * @vcpu: The vcpu pointer
  107. *
  108. * Check if the virtual timer was armed and either schedule a corresponding
  109. * soft timer or inject directly if already expired.
  110. */
  111. void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
  112. {
  113. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  114. cycle_t cval, now;
  115. u64 ns;
  116. if ((timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) ||
  117. !(timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE))
  118. return;
  119. cval = timer->cntv_cval;
  120. now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
  121. BUG_ON(timer_is_armed(timer));
  122. if (cval <= now) {
  123. /*
  124. * Timer has already expired while we were not
  125. * looking. Inject the interrupt and carry on.
  126. */
  127. kvm_timer_inject_irq(vcpu);
  128. return;
  129. }
  130. ns = cyclecounter_cyc2ns(timecounter->cc, cval - now);
  131. timer_arm(timer, ns);
  132. }
  133. void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
  134. const struct kvm_irq_level *irq)
  135. {
  136. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  137. /*
  138. * The vcpu timer irq number cannot be determined in
  139. * kvm_timer_vcpu_init() because it is called much before
  140. * kvm_vcpu_set_target(). To handle this, we determine
  141. * vcpu timer irq number when the vcpu is reset.
  142. */
  143. timer->irq = irq;
  144. }
  145. void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
  146. {
  147. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  148. INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
  149. hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  150. timer->timer.function = kvm_timer_expire;
  151. }
  152. static void kvm_timer_init_interrupt(void *info)
  153. {
  154. enable_percpu_irq(host_vtimer_irq, 0);
  155. }
  156. static int kvm_timer_cpu_notify(struct notifier_block *self,
  157. unsigned long action, void *cpu)
  158. {
  159. switch (action) {
  160. case CPU_STARTING:
  161. case CPU_STARTING_FROZEN:
  162. kvm_timer_init_interrupt(NULL);
  163. break;
  164. case CPU_DYING:
  165. case CPU_DYING_FROZEN:
  166. disable_percpu_irq(host_vtimer_irq);
  167. break;
  168. }
  169. return NOTIFY_OK;
  170. }
  171. static struct notifier_block kvm_timer_cpu_nb = {
  172. .notifier_call = kvm_timer_cpu_notify,
  173. };
  174. static const struct of_device_id arch_timer_of_match[] = {
  175. { .compatible = "arm,armv7-timer", },
  176. {},
  177. };
  178. int kvm_timer_hyp_init(void)
  179. {
  180. struct device_node *np;
  181. unsigned int ppi;
  182. int err;
  183. timecounter = arch_timer_get_timecounter();
  184. if (!timecounter)
  185. return -ENODEV;
  186. np = of_find_matching_node(NULL, arch_timer_of_match);
  187. if (!np) {
  188. kvm_err("kvm_arch_timer: can't find DT node\n");
  189. return -ENODEV;
  190. }
  191. ppi = irq_of_parse_and_map(np, 2);
  192. if (!ppi) {
  193. kvm_err("kvm_arch_timer: no virtual timer interrupt\n");
  194. err = -EINVAL;
  195. goto out;
  196. }
  197. err = request_percpu_irq(ppi, kvm_arch_timer_handler,
  198. "kvm guest timer", kvm_get_running_vcpus());
  199. if (err) {
  200. kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
  201. ppi, err);
  202. goto out;
  203. }
  204. host_vtimer_irq = ppi;
  205. err = register_cpu_notifier(&kvm_timer_cpu_nb);
  206. if (err) {
  207. kvm_err("Cannot register timer CPU notifier\n");
  208. goto out_free;
  209. }
  210. wqueue = create_singlethread_workqueue("kvm_arch_timer");
  211. if (!wqueue) {
  212. err = -ENOMEM;
  213. goto out_free;
  214. }
  215. kvm_info("%s IRQ%d\n", np->name, ppi);
  216. on_each_cpu(kvm_timer_init_interrupt, NULL, 1);
  217. goto out;
  218. out_free:
  219. free_percpu_irq(ppi, kvm_get_running_vcpus());
  220. out:
  221. of_node_put(np);
  222. return err;
  223. }
  224. void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
  225. {
  226. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  227. timer_disarm(timer);
  228. }
  229. int kvm_timer_init(struct kvm *kvm)
  230. {
  231. if (timecounter && wqueue) {
  232. kvm->arch.timer.cntvoff = kvm_phys_timer_read();
  233. kvm->arch.timer.enabled = 1;
  234. }
  235. return 0;
  236. }