arch_timer.c 6.4 KB

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