arch_timer.c 14 KB

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