arch_timer.c 13 KB

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