arch_timer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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 <linux/uaccess.h>
  24. #include <clocksource/arm_arch_timer.h>
  25. #include <asm/arch_timer.h>
  26. #include <asm/kvm_hyp.h>
  27. #include <kvm/arm_vgic.h>
  28. #include <kvm/arm_arch_timer.h>
  29. #include "trace.h"
  30. static struct timecounter *timecounter;
  31. static unsigned int host_vtimer_irq;
  32. static u32 host_vtimer_irq_flags;
  33. static const struct kvm_irq_level default_ptimer_irq = {
  34. .irq = 30,
  35. .level = 1,
  36. };
  37. static const struct kvm_irq_level default_vtimer_irq = {
  38. .irq = 27,
  39. .level = 1,
  40. };
  41. void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
  42. {
  43. vcpu_vtimer(vcpu)->active_cleared_last = false;
  44. }
  45. u64 kvm_phys_timer_read(void)
  46. {
  47. return timecounter->cc->read(timecounter->cc);
  48. }
  49. static bool timer_is_armed(struct arch_timer_cpu *timer)
  50. {
  51. return timer->armed;
  52. }
  53. /* timer_arm: as in "arm the timer", not as in ARM the company */
  54. static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
  55. {
  56. timer->armed = true;
  57. hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
  58. HRTIMER_MODE_ABS);
  59. }
  60. static void timer_disarm(struct arch_timer_cpu *timer)
  61. {
  62. if (timer_is_armed(timer)) {
  63. hrtimer_cancel(&timer->timer);
  64. cancel_work_sync(&timer->expired);
  65. timer->armed = false;
  66. }
  67. }
  68. static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
  69. {
  70. struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
  71. /*
  72. * We disable the timer in the world switch and let it be
  73. * handled by kvm_timer_sync_hwstate(). Getting a timer
  74. * interrupt at this point is a sure sign of some major
  75. * breakage.
  76. */
  77. pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
  78. return IRQ_HANDLED;
  79. }
  80. /*
  81. * Work function for handling the backup timer that we schedule when a vcpu is
  82. * no longer running, but had a timer programmed to fire in the future.
  83. */
  84. static void kvm_timer_inject_irq_work(struct work_struct *work)
  85. {
  86. struct kvm_vcpu *vcpu;
  87. vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
  88. /*
  89. * If the vcpu is blocked we want to wake it up so that it will see
  90. * the timer has expired when entering the guest.
  91. */
  92. kvm_vcpu_wake_up(vcpu);
  93. }
  94. static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
  95. {
  96. u64 cval, now;
  97. cval = timer_ctx->cnt_cval;
  98. now = kvm_phys_timer_read() - timer_ctx->cntvoff;
  99. if (now < cval) {
  100. u64 ns;
  101. ns = cyclecounter_cyc2ns(timecounter->cc,
  102. cval - now,
  103. timecounter->mask,
  104. &timecounter->frac);
  105. return ns;
  106. }
  107. return 0;
  108. }
  109. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
  110. {
  111. return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
  112. (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE);
  113. }
  114. /*
  115. * Returns the earliest expiration time in ns among guest timers.
  116. * Note that it will return 0 if none of timers can fire.
  117. */
  118. static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
  119. {
  120. u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX;
  121. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  122. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  123. if (kvm_timer_irq_can_fire(vtimer))
  124. min_virt = kvm_timer_compute_delta(vtimer);
  125. if (kvm_timer_irq_can_fire(ptimer))
  126. min_phys = kvm_timer_compute_delta(ptimer);
  127. /* If none of timers can fire, then return 0 */
  128. if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX))
  129. return 0;
  130. return min(min_virt, min_phys);
  131. }
  132. static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
  133. {
  134. struct arch_timer_cpu *timer;
  135. struct kvm_vcpu *vcpu;
  136. u64 ns;
  137. timer = container_of(hrt, struct arch_timer_cpu, timer);
  138. vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
  139. /*
  140. * Check that the timer has really expired from the guest's
  141. * PoV (NTP on the host may have forced it to expire
  142. * early). If we should have slept longer, restart it.
  143. */
  144. ns = kvm_timer_earliest_exp(vcpu);
  145. if (unlikely(ns)) {
  146. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  147. return HRTIMER_RESTART;
  148. }
  149. schedule_work(&timer->expired);
  150. return HRTIMER_NORESTART;
  151. }
  152. bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
  153. {
  154. u64 cval, now;
  155. if (!kvm_timer_irq_can_fire(timer_ctx))
  156. return false;
  157. cval = timer_ctx->cnt_cval;
  158. now = kvm_phys_timer_read() - timer_ctx->cntvoff;
  159. return cval <= now;
  160. }
  161. /*
  162. * Reflect the timer output level into the kvm_run structure
  163. */
  164. void kvm_timer_update_run(struct kvm_vcpu *vcpu)
  165. {
  166. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  167. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  168. struct kvm_sync_regs *regs = &vcpu->run->s.regs;
  169. /* Populate the device bitmap with the timer states */
  170. regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
  171. KVM_ARM_DEV_EL1_PTIMER);
  172. if (vtimer->irq.level)
  173. regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
  174. if (ptimer->irq.level)
  175. regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
  176. }
  177. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  178. struct arch_timer_context *timer_ctx)
  179. {
  180. int ret;
  181. timer_ctx->active_cleared_last = false;
  182. timer_ctx->irq.level = new_level;
  183. trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
  184. timer_ctx->irq.level);
  185. if (likely(irqchip_in_kernel(vcpu->kvm))) {
  186. ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
  187. timer_ctx->irq.irq,
  188. timer_ctx->irq.level,
  189. timer_ctx);
  190. WARN_ON(ret);
  191. }
  192. }
  193. /*
  194. * Check if there was a change in the timer state (should we raise or lower
  195. * the line level to the GIC).
  196. */
  197. static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
  198. {
  199. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  200. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  201. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  202. /*
  203. * If userspace modified the timer registers via SET_ONE_REG before
  204. * the vgic was initialized, we mustn't set the vtimer->irq.level value
  205. * because the guest would never see the interrupt. Instead wait
  206. * until we call this function from kvm_timer_flush_hwstate.
  207. */
  208. if (unlikely(!timer->enabled))
  209. return;
  210. if (kvm_timer_should_fire(vtimer) != vtimer->irq.level)
  211. kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer);
  212. if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
  213. kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
  214. }
  215. /* Schedule the background timer for the emulated timer. */
  216. static void kvm_timer_emulate(struct kvm_vcpu *vcpu,
  217. struct arch_timer_context *timer_ctx)
  218. {
  219. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  220. if (kvm_timer_should_fire(timer_ctx))
  221. return;
  222. if (!kvm_timer_irq_can_fire(timer_ctx))
  223. return;
  224. /* The timer has not yet expired, schedule a background timer */
  225. timer_arm(timer, kvm_timer_compute_delta(timer_ctx));
  226. }
  227. /*
  228. * Schedule the background timer before calling kvm_vcpu_block, so that this
  229. * thread is removed from its waitqueue and made runnable when there's a timer
  230. * interrupt to handle.
  231. */
  232. void kvm_timer_schedule(struct kvm_vcpu *vcpu)
  233. {
  234. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  235. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  236. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  237. BUG_ON(timer_is_armed(timer));
  238. /*
  239. * No need to schedule a background timer if any guest timer has
  240. * already expired, because kvm_vcpu_block will return before putting
  241. * the thread to sleep.
  242. */
  243. if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer))
  244. return;
  245. /*
  246. * If both timers are not capable of raising interrupts (disabled or
  247. * masked), then there's no more work for us to do.
  248. */
  249. if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer))
  250. return;
  251. /*
  252. * The guest timers have not yet expired, schedule a background timer.
  253. * Set the earliest expiration time among the guest timers.
  254. */
  255. timer_arm(timer, kvm_timer_earliest_exp(vcpu));
  256. }
  257. void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
  258. {
  259. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  260. timer_disarm(timer);
  261. }
  262. static void kvm_timer_flush_hwstate_vgic(struct kvm_vcpu *vcpu)
  263. {
  264. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  265. bool phys_active;
  266. int ret;
  267. /*
  268. * If we enter the guest with the virtual input level to the VGIC
  269. * asserted, then we have already told the VGIC what we need to, and
  270. * we don't need to exit from the guest until the guest deactivates
  271. * the already injected interrupt, so therefore we should set the
  272. * hardware active state to prevent unnecessary exits from the guest.
  273. *
  274. * Also, if we enter the guest with the virtual timer interrupt active,
  275. * then it must be active on the physical distributor, because we set
  276. * the HW bit and the guest must be able to deactivate the virtual and
  277. * physical interrupt at the same time.
  278. *
  279. * Conversely, if the virtual input level is deasserted and the virtual
  280. * interrupt is not active, then always clear the hardware active state
  281. * to ensure that hardware interrupts from the timer triggers a guest
  282. * exit.
  283. */
  284. phys_active = vtimer->irq.level ||
  285. kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
  286. /*
  287. * We want to avoid hitting the (re)distributor as much as
  288. * possible, as this is a potentially expensive MMIO access
  289. * (not to mention locks in the irq layer), and a solution for
  290. * this is to cache the "active" state in memory.
  291. *
  292. * Things to consider: we cannot cache an "active set" state,
  293. * because the HW can change this behind our back (it becomes
  294. * "clear" in the HW). We must then restrict the caching to
  295. * the "clear" state.
  296. *
  297. * The cache is invalidated on:
  298. * - vcpu put, indicating that the HW cannot be trusted to be
  299. * in a sane state on the next vcpu load,
  300. * - any change in the interrupt state
  301. *
  302. * Usage conditions:
  303. * - cached value is "active clear"
  304. * - value to be programmed is "active clear"
  305. */
  306. if (vtimer->active_cleared_last && !phys_active)
  307. return;
  308. ret = irq_set_irqchip_state(host_vtimer_irq,
  309. IRQCHIP_STATE_ACTIVE,
  310. phys_active);
  311. WARN_ON(ret);
  312. vtimer->active_cleared_last = !phys_active;
  313. }
  314. bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
  315. {
  316. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  317. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  318. struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
  319. bool vlevel, plevel;
  320. if (likely(irqchip_in_kernel(vcpu->kvm)))
  321. return false;
  322. vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
  323. plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
  324. return vtimer->irq.level != vlevel ||
  325. ptimer->irq.level != plevel;
  326. }
  327. static void kvm_timer_flush_hwstate_user(struct kvm_vcpu *vcpu)
  328. {
  329. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  330. /*
  331. * To prevent continuously exiting from the guest, we mask the
  332. * physical interrupt such that the guest can make forward progress.
  333. * Once we detect the output level being deasserted, we unmask the
  334. * interrupt again so that we exit from the guest when the timer
  335. * fires.
  336. */
  337. if (vtimer->irq.level)
  338. disable_percpu_irq(host_vtimer_irq);
  339. else
  340. enable_percpu_irq(host_vtimer_irq, 0);
  341. }
  342. /**
  343. * kvm_timer_flush_hwstate - prepare timers before running the vcpu
  344. * @vcpu: The vcpu pointer
  345. *
  346. * Check if the virtual timer has expired while we were running in the host,
  347. * and inject an interrupt if that was the case, making sure the timer is
  348. * masked or disabled on the host so that we keep executing. Also schedule a
  349. * software timer for the physical timer if it is enabled.
  350. */
  351. void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
  352. {
  353. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  354. if (unlikely(!timer->enabled))
  355. return;
  356. kvm_timer_update_state(vcpu);
  357. /* Set the background timer for the physical timer emulation. */
  358. kvm_timer_emulate(vcpu, vcpu_ptimer(vcpu));
  359. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  360. kvm_timer_flush_hwstate_user(vcpu);
  361. else
  362. kvm_timer_flush_hwstate_vgic(vcpu);
  363. }
  364. /**
  365. * kvm_timer_sync_hwstate - sync timer state from cpu
  366. * @vcpu: The vcpu pointer
  367. *
  368. * Check if any of the timers have expired while we were running in the guest,
  369. * and inject an interrupt if that was the case.
  370. */
  371. void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
  372. {
  373. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  374. /*
  375. * This is to cancel the background timer for the physical timer
  376. * emulation if it is set.
  377. */
  378. timer_disarm(timer);
  379. /*
  380. * The guest could have modified the timer registers or the timer
  381. * could have expired, update the timer state.
  382. */
  383. kvm_timer_update_state(vcpu);
  384. }
  385. int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
  386. {
  387. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  388. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  389. /*
  390. * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
  391. * and to 0 for ARMv7. We provide an implementation that always
  392. * resets the timer to be disabled and unmasked and is compliant with
  393. * the ARMv7 architecture.
  394. */
  395. vtimer->cnt_ctl = 0;
  396. ptimer->cnt_ctl = 0;
  397. kvm_timer_update_state(vcpu);
  398. return 0;
  399. }
  400. /* Make the updates of cntvoff for all vtimer contexts atomic */
  401. static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
  402. {
  403. int i;
  404. struct kvm *kvm = vcpu->kvm;
  405. struct kvm_vcpu *tmp;
  406. mutex_lock(&kvm->lock);
  407. kvm_for_each_vcpu(i, tmp, kvm)
  408. vcpu_vtimer(tmp)->cntvoff = cntvoff;
  409. /*
  410. * When called from the vcpu create path, the CPU being created is not
  411. * included in the loop above, so we just set it here as well.
  412. */
  413. vcpu_vtimer(vcpu)->cntvoff = cntvoff;
  414. mutex_unlock(&kvm->lock);
  415. }
  416. void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
  417. {
  418. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  419. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  420. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  421. /* Synchronize cntvoff across all vtimers of a VM. */
  422. update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
  423. vcpu_ptimer(vcpu)->cntvoff = 0;
  424. INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
  425. hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  426. timer->timer.function = kvm_timer_expire;
  427. vtimer->irq.irq = default_vtimer_irq.irq;
  428. ptimer->irq.irq = default_ptimer_irq.irq;
  429. }
  430. static void kvm_timer_init_interrupt(void *info)
  431. {
  432. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  433. }
  434. int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
  435. {
  436. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  437. switch (regid) {
  438. case KVM_REG_ARM_TIMER_CTL:
  439. vtimer->cnt_ctl = value;
  440. break;
  441. case KVM_REG_ARM_TIMER_CNT:
  442. update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
  443. break;
  444. case KVM_REG_ARM_TIMER_CVAL:
  445. vtimer->cnt_cval = value;
  446. break;
  447. default:
  448. return -1;
  449. }
  450. kvm_timer_update_state(vcpu);
  451. return 0;
  452. }
  453. u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
  454. {
  455. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  456. switch (regid) {
  457. case KVM_REG_ARM_TIMER_CTL:
  458. return vtimer->cnt_ctl;
  459. case KVM_REG_ARM_TIMER_CNT:
  460. return kvm_phys_timer_read() - vtimer->cntvoff;
  461. case KVM_REG_ARM_TIMER_CVAL:
  462. return vtimer->cnt_cval;
  463. }
  464. return (u64)-1;
  465. }
  466. static int kvm_timer_starting_cpu(unsigned int cpu)
  467. {
  468. kvm_timer_init_interrupt(NULL);
  469. return 0;
  470. }
  471. static int kvm_timer_dying_cpu(unsigned int cpu)
  472. {
  473. disable_percpu_irq(host_vtimer_irq);
  474. return 0;
  475. }
  476. int kvm_timer_hyp_init(void)
  477. {
  478. struct arch_timer_kvm_info *info;
  479. int err;
  480. info = arch_timer_get_kvm_info();
  481. timecounter = &info->timecounter;
  482. if (!timecounter->cc) {
  483. kvm_err("kvm_arch_timer: uninitialized timecounter\n");
  484. return -ENODEV;
  485. }
  486. if (info->virtual_irq <= 0) {
  487. kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
  488. info->virtual_irq);
  489. return -ENODEV;
  490. }
  491. host_vtimer_irq = info->virtual_irq;
  492. host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
  493. if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
  494. host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
  495. kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
  496. host_vtimer_irq);
  497. host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
  498. }
  499. err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
  500. "kvm guest timer", kvm_get_running_vcpus());
  501. if (err) {
  502. kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
  503. host_vtimer_irq, err);
  504. return err;
  505. }
  506. kvm_debug("virtual timer IRQ%d\n", host_vtimer_irq);
  507. cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
  508. "kvm/arm/timer:starting", kvm_timer_starting_cpu,
  509. kvm_timer_dying_cpu);
  510. return err;
  511. }
  512. void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
  513. {
  514. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  515. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  516. timer_disarm(timer);
  517. kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
  518. }
  519. static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
  520. {
  521. int vtimer_irq, ptimer_irq;
  522. int i, ret;
  523. vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
  524. ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
  525. if (ret)
  526. return false;
  527. ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
  528. ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
  529. if (ret)
  530. return false;
  531. kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
  532. if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
  533. vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
  534. return false;
  535. }
  536. return true;
  537. }
  538. int kvm_timer_enable(struct kvm_vcpu *vcpu)
  539. {
  540. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  541. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  542. struct irq_desc *desc;
  543. struct irq_data *data;
  544. int phys_irq;
  545. int ret;
  546. if (timer->enabled)
  547. return 0;
  548. /* Without a VGIC we do not map virtual IRQs to physical IRQs */
  549. if (!irqchip_in_kernel(vcpu->kvm))
  550. goto no_vgic;
  551. if (!vgic_initialized(vcpu->kvm))
  552. return -ENODEV;
  553. if (!timer_irqs_are_valid(vcpu)) {
  554. kvm_debug("incorrectly configured timer irqs\n");
  555. return -EINVAL;
  556. }
  557. /*
  558. * Find the physical IRQ number corresponding to the host_vtimer_irq
  559. */
  560. desc = irq_to_desc(host_vtimer_irq);
  561. if (!desc) {
  562. kvm_err("%s: no interrupt descriptor\n", __func__);
  563. return -EINVAL;
  564. }
  565. data = irq_desc_get_irq_data(desc);
  566. while (data->parent_data)
  567. data = data->parent_data;
  568. phys_irq = data->hwirq;
  569. /*
  570. * Tell the VGIC that the virtual interrupt is tied to a
  571. * physical interrupt. We do that once per VCPU.
  572. */
  573. ret = kvm_vgic_map_phys_irq(vcpu, vtimer->irq.irq, phys_irq);
  574. if (ret)
  575. return ret;
  576. no_vgic:
  577. timer->enabled = 1;
  578. return 0;
  579. }
  580. /*
  581. * On VHE system, we only need to configure trap on physical timer and counter
  582. * accesses in EL0 and EL1 once, not for every world switch.
  583. * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
  584. * and this makes those bits have no effect for the host kernel execution.
  585. */
  586. void kvm_timer_init_vhe(void)
  587. {
  588. /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
  589. u32 cnthctl_shift = 10;
  590. u64 val;
  591. /*
  592. * Disallow physical timer access for the guest.
  593. * Physical counter access is allowed.
  594. */
  595. val = read_sysreg(cnthctl_el2);
  596. val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift);
  597. val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
  598. write_sysreg(val, cnthctl_el2);
  599. }
  600. static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
  601. {
  602. struct kvm_vcpu *vcpu;
  603. int i;
  604. kvm_for_each_vcpu(i, vcpu, kvm) {
  605. vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
  606. vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
  607. }
  608. }
  609. int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  610. {
  611. int __user *uaddr = (int __user *)(long)attr->addr;
  612. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  613. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  614. int irq;
  615. if (!irqchip_in_kernel(vcpu->kvm))
  616. return -EINVAL;
  617. if (get_user(irq, uaddr))
  618. return -EFAULT;
  619. if (!(irq_is_ppi(irq)))
  620. return -EINVAL;
  621. if (vcpu->arch.timer_cpu.enabled)
  622. return -EBUSY;
  623. switch (attr->attr) {
  624. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  625. set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
  626. break;
  627. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  628. set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
  629. break;
  630. default:
  631. return -ENXIO;
  632. }
  633. return 0;
  634. }
  635. int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  636. {
  637. int __user *uaddr = (int __user *)(long)attr->addr;
  638. struct arch_timer_context *timer;
  639. int irq;
  640. switch (attr->attr) {
  641. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  642. timer = vcpu_vtimer(vcpu);
  643. break;
  644. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  645. timer = vcpu_ptimer(vcpu);
  646. break;
  647. default:
  648. return -ENXIO;
  649. }
  650. irq = timer->irq.irq;
  651. return put_user(irq, uaddr);
  652. }
  653. int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  654. {
  655. switch (attr->attr) {
  656. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  657. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  658. return 0;
  659. }
  660. return -ENXIO;
  661. }