arch_timer.c 14 KB

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