arch_timer.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx);
  42. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  43. struct arch_timer_context *timer_ctx);
  44. static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
  45. u64 kvm_phys_timer_read(void)
  46. {
  47. return timecounter->cc->read(timecounter->cc);
  48. }
  49. static void soft_timer_start(struct hrtimer *hrt, u64 ns)
  50. {
  51. hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
  52. HRTIMER_MODE_ABS);
  53. }
  54. static void soft_timer_cancel(struct hrtimer *hrt, struct work_struct *work)
  55. {
  56. hrtimer_cancel(hrt);
  57. if (work)
  58. cancel_work_sync(work);
  59. }
  60. static void kvm_vtimer_update_mask_user(struct kvm_vcpu *vcpu)
  61. {
  62. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  63. /*
  64. * When using a userspace irqchip with the architected timers, we must
  65. * prevent continuously exiting from the guest, and therefore mask the
  66. * physical interrupt by disabling it on the host interrupt controller
  67. * when the virtual level is high, such that the guest can make
  68. * forward progress. Once we detect the output level being
  69. * de-asserted, we unmask the interrupt again so that we exit from the
  70. * guest when the timer fires.
  71. */
  72. if (vtimer->irq.level)
  73. disable_percpu_irq(host_vtimer_irq);
  74. else
  75. enable_percpu_irq(host_vtimer_irq, 0);
  76. }
  77. static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
  78. {
  79. struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
  80. struct arch_timer_context *vtimer;
  81. if (!vcpu) {
  82. pr_warn_once("Spurious arch timer IRQ on non-VCPU thread\n");
  83. return IRQ_NONE;
  84. }
  85. vtimer = vcpu_vtimer(vcpu);
  86. if (!vtimer->irq.level) {
  87. vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl);
  88. if (kvm_timer_irq_can_fire(vtimer))
  89. kvm_timer_update_irq(vcpu, true, vtimer);
  90. }
  91. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  92. kvm_vtimer_update_mask_user(vcpu);
  93. return IRQ_HANDLED;
  94. }
  95. /*
  96. * Work function for handling the backup timer that we schedule when a vcpu is
  97. * no longer running, but had a timer programmed to fire in the future.
  98. */
  99. static void kvm_timer_inject_irq_work(struct work_struct *work)
  100. {
  101. struct kvm_vcpu *vcpu;
  102. vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
  103. /*
  104. * If the vcpu is blocked we want to wake it up so that it will see
  105. * the timer has expired when entering the guest.
  106. */
  107. kvm_vcpu_wake_up(vcpu);
  108. }
  109. static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
  110. {
  111. u64 cval, now;
  112. cval = timer_ctx->cnt_cval;
  113. now = kvm_phys_timer_read() - timer_ctx->cntvoff;
  114. if (now < cval) {
  115. u64 ns;
  116. ns = cyclecounter_cyc2ns(timecounter->cc,
  117. cval - now,
  118. timecounter->mask,
  119. &timecounter->frac);
  120. return ns;
  121. }
  122. return 0;
  123. }
  124. static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
  125. {
  126. return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
  127. (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE);
  128. }
  129. /*
  130. * Returns the earliest expiration time in ns among guest timers.
  131. * Note that it will return 0 if none of timers can fire.
  132. */
  133. static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
  134. {
  135. u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX;
  136. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  137. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  138. if (kvm_timer_irq_can_fire(vtimer))
  139. min_virt = kvm_timer_compute_delta(vtimer);
  140. if (kvm_timer_irq_can_fire(ptimer))
  141. min_phys = kvm_timer_compute_delta(ptimer);
  142. /* If none of timers can fire, then return 0 */
  143. if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX))
  144. return 0;
  145. return min(min_virt, min_phys);
  146. }
  147. static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
  148. {
  149. struct arch_timer_cpu *timer;
  150. struct kvm_vcpu *vcpu;
  151. u64 ns;
  152. timer = container_of(hrt, struct arch_timer_cpu, bg_timer);
  153. vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
  154. /*
  155. * Check that the timer has really expired from the guest's
  156. * PoV (NTP on the host may have forced it to expire
  157. * early). If we should have slept longer, restart it.
  158. */
  159. ns = kvm_timer_earliest_exp(vcpu);
  160. if (unlikely(ns)) {
  161. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  162. return HRTIMER_RESTART;
  163. }
  164. schedule_work(&timer->expired);
  165. return HRTIMER_NORESTART;
  166. }
  167. static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt)
  168. {
  169. struct arch_timer_context *ptimer;
  170. struct arch_timer_cpu *timer;
  171. struct kvm_vcpu *vcpu;
  172. u64 ns;
  173. timer = container_of(hrt, struct arch_timer_cpu, phys_timer);
  174. vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
  175. ptimer = vcpu_ptimer(vcpu);
  176. /*
  177. * Check that the timer has really expired from the guest's
  178. * PoV (NTP on the host may have forced it to expire
  179. * early). If not ready, schedule for a later time.
  180. */
  181. ns = kvm_timer_compute_delta(ptimer);
  182. if (unlikely(ns)) {
  183. hrtimer_forward_now(hrt, ns_to_ktime(ns));
  184. return HRTIMER_RESTART;
  185. }
  186. kvm_timer_update_irq(vcpu, true, ptimer);
  187. return HRTIMER_NORESTART;
  188. }
  189. static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
  190. {
  191. u64 cval, now;
  192. if (!kvm_timer_irq_can_fire(timer_ctx))
  193. return false;
  194. cval = timer_ctx->cnt_cval;
  195. now = kvm_phys_timer_read() - timer_ctx->cntvoff;
  196. return cval <= now;
  197. }
  198. bool kvm_timer_is_pending(struct kvm_vcpu *vcpu)
  199. {
  200. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  201. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  202. if (vtimer->irq.level || ptimer->irq.level)
  203. return true;
  204. /*
  205. * When this is called from withing the wait loop of kvm_vcpu_block(),
  206. * the software view of the timer state is up to date (timer->loaded
  207. * is false), and so we can simply check if the timer should fire now.
  208. */
  209. if (!vtimer->loaded && kvm_timer_should_fire(vtimer))
  210. return true;
  211. return kvm_timer_should_fire(ptimer);
  212. }
  213. /*
  214. * Reflect the timer output level into the kvm_run structure
  215. */
  216. void kvm_timer_update_run(struct kvm_vcpu *vcpu)
  217. {
  218. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  219. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  220. struct kvm_sync_regs *regs = &vcpu->run->s.regs;
  221. /* Populate the device bitmap with the timer states */
  222. regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
  223. KVM_ARM_DEV_EL1_PTIMER);
  224. if (vtimer->irq.level)
  225. regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
  226. if (ptimer->irq.level)
  227. regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
  228. }
  229. static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
  230. struct arch_timer_context *timer_ctx)
  231. {
  232. int ret;
  233. timer_ctx->irq.level = new_level;
  234. trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
  235. timer_ctx->irq.level);
  236. if (likely(irqchip_in_kernel(vcpu->kvm))) {
  237. ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
  238. timer_ctx->irq.irq,
  239. timer_ctx->irq.level,
  240. timer_ctx);
  241. WARN_ON(ret);
  242. }
  243. }
  244. /* Schedule the background timer for the emulated timer. */
  245. static void phys_timer_emulate(struct kvm_vcpu *vcpu)
  246. {
  247. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  248. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  249. /*
  250. * If the timer can fire now we have just raised the IRQ line and we
  251. * don't need to have a soft timer scheduled for the future. If the
  252. * timer cannot fire at all, then we also don't need a soft timer.
  253. */
  254. if (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) {
  255. soft_timer_cancel(&timer->phys_timer, NULL);
  256. return;
  257. }
  258. soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(ptimer));
  259. }
  260. /*
  261. * Check if there was a change in the timer state, so that we should either
  262. * raise or lower the line level to the GIC or schedule a background timer to
  263. * emulate the physical timer.
  264. */
  265. static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
  266. {
  267. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  268. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  269. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  270. if (unlikely(!timer->enabled))
  271. return;
  272. if (kvm_timer_should_fire(vtimer) != vtimer->irq.level)
  273. kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer);
  274. if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
  275. kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
  276. phys_timer_emulate(vcpu);
  277. }
  278. static void vtimer_save_state(struct kvm_vcpu *vcpu)
  279. {
  280. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  281. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  282. unsigned long flags;
  283. local_irq_save(flags);
  284. if (!vtimer->loaded)
  285. goto out;
  286. if (timer->enabled) {
  287. vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl);
  288. vtimer->cnt_cval = read_sysreg_el0(cntv_cval);
  289. }
  290. /* Disable the virtual timer */
  291. write_sysreg_el0(0, cntv_ctl);
  292. vtimer->loaded = false;
  293. out:
  294. local_irq_restore(flags);
  295. }
  296. /*
  297. * Schedule the background timer before calling kvm_vcpu_block, so that this
  298. * thread is removed from its waitqueue and made runnable when there's a timer
  299. * interrupt to handle.
  300. */
  301. void kvm_timer_schedule(struct kvm_vcpu *vcpu)
  302. {
  303. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  304. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  305. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  306. vtimer_save_state(vcpu);
  307. /*
  308. * No need to schedule a background timer if any guest timer has
  309. * already expired, because kvm_vcpu_block will return before putting
  310. * the thread to sleep.
  311. */
  312. if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer))
  313. return;
  314. /*
  315. * If both timers are not capable of raising interrupts (disabled or
  316. * masked), then there's no more work for us to do.
  317. */
  318. if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer))
  319. return;
  320. /*
  321. * The guest timers have not yet expired, schedule a background timer.
  322. * Set the earliest expiration time among the guest timers.
  323. */
  324. soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
  325. }
  326. static void vtimer_restore_state(struct kvm_vcpu *vcpu)
  327. {
  328. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  329. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  330. unsigned long flags;
  331. local_irq_save(flags);
  332. if (vtimer->loaded)
  333. goto out;
  334. if (timer->enabled) {
  335. write_sysreg_el0(vtimer->cnt_cval, cntv_cval);
  336. isb();
  337. write_sysreg_el0(vtimer->cnt_ctl, cntv_ctl);
  338. }
  339. vtimer->loaded = true;
  340. out:
  341. local_irq_restore(flags);
  342. }
  343. void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
  344. {
  345. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  346. vtimer_restore_state(vcpu);
  347. soft_timer_cancel(&timer->bg_timer, &timer->expired);
  348. }
  349. static void set_cntvoff(u64 cntvoff)
  350. {
  351. u32 low = lower_32_bits(cntvoff);
  352. u32 high = upper_32_bits(cntvoff);
  353. /*
  354. * Since kvm_call_hyp doesn't fully support the ARM PCS especially on
  355. * 32-bit systems, but rather passes register by register shifted one
  356. * place (we put the function address in r0/x0), we cannot simply pass
  357. * a 64-bit value as an argument, but have to split the value in two
  358. * 32-bit halves.
  359. */
  360. kvm_call_hyp(__kvm_timer_set_cntvoff, low, high);
  361. }
  362. static void kvm_timer_vcpu_load_vgic(struct kvm_vcpu *vcpu)
  363. {
  364. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  365. bool phys_active;
  366. int ret;
  367. phys_active = vtimer->irq.level ||
  368. kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
  369. ret = irq_set_irqchip_state(host_vtimer_irq,
  370. IRQCHIP_STATE_ACTIVE,
  371. phys_active);
  372. WARN_ON(ret);
  373. }
  374. static void kvm_timer_vcpu_load_user(struct kvm_vcpu *vcpu)
  375. {
  376. kvm_vtimer_update_mask_user(vcpu);
  377. }
  378. void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
  379. {
  380. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  381. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  382. if (unlikely(!timer->enabled))
  383. return;
  384. if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
  385. kvm_timer_vcpu_load_user(vcpu);
  386. else
  387. kvm_timer_vcpu_load_vgic(vcpu);
  388. set_cntvoff(vtimer->cntvoff);
  389. vtimer_restore_state(vcpu);
  390. if (has_vhe())
  391. disable_el1_phys_timer_access();
  392. /* Set the background timer for the physical timer emulation. */
  393. phys_timer_emulate(vcpu);
  394. }
  395. bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
  396. {
  397. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  398. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  399. struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
  400. bool vlevel, plevel;
  401. if (likely(irqchip_in_kernel(vcpu->kvm)))
  402. return false;
  403. vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
  404. plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
  405. return vtimer->irq.level != vlevel ||
  406. ptimer->irq.level != plevel;
  407. }
  408. void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
  409. {
  410. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  411. if (unlikely(!timer->enabled))
  412. return;
  413. if (has_vhe())
  414. enable_el1_phys_timer_access();
  415. vtimer_save_state(vcpu);
  416. /*
  417. * Cancel the physical timer emulation, because the only case where we
  418. * need it after a vcpu_put is in the context of a sleeping VCPU, and
  419. * in that case we already factor in the deadline for the physical
  420. * timer when scheduling the bg_timer.
  421. *
  422. * In any case, we re-schedule the hrtimer for the physical timer when
  423. * coming back to the VCPU thread in kvm_timer_vcpu_load().
  424. */
  425. soft_timer_cancel(&timer->phys_timer, NULL);
  426. /*
  427. * The kernel may decide to run userspace after calling vcpu_put, so
  428. * we reset cntvoff to 0 to ensure a consistent read between user
  429. * accesses to the virtual counter and kernel access to the physical
  430. * counter.
  431. */
  432. set_cntvoff(0);
  433. }
  434. static void unmask_vtimer_irq(struct kvm_vcpu *vcpu)
  435. {
  436. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  437. if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
  438. kvm_vtimer_update_mask_user(vcpu);
  439. return;
  440. }
  441. /*
  442. * If the guest disabled the timer without acking the interrupt, then
  443. * we must make sure the physical and virtual active states are in
  444. * sync by deactivating the physical interrupt, because otherwise we
  445. * wouldn't see the next timer interrupt in the host.
  446. */
  447. if (!kvm_vgic_map_is_active(vcpu, vtimer->irq.irq)) {
  448. int ret;
  449. ret = irq_set_irqchip_state(host_vtimer_irq,
  450. IRQCHIP_STATE_ACTIVE,
  451. false);
  452. WARN_ON(ret);
  453. }
  454. }
  455. /**
  456. * kvm_timer_sync_hwstate - sync timer state from cpu
  457. * @vcpu: The vcpu pointer
  458. *
  459. * Check if any of the timers have expired while we were running in the guest,
  460. * and inject an interrupt if that was the case.
  461. */
  462. void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
  463. {
  464. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  465. /*
  466. * If we entered the guest with the vtimer output asserted we have to
  467. * check if the guest has modified the timer so that we should lower
  468. * the line at this point.
  469. */
  470. if (vtimer->irq.level) {
  471. vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl);
  472. vtimer->cnt_cval = read_sysreg_el0(cntv_cval);
  473. if (!kvm_timer_should_fire(vtimer)) {
  474. kvm_timer_update_irq(vcpu, false, vtimer);
  475. unmask_vtimer_irq(vcpu);
  476. }
  477. }
  478. }
  479. int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
  480. {
  481. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  482. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  483. /*
  484. * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
  485. * and to 0 for ARMv7. We provide an implementation that always
  486. * resets the timer to be disabled and unmasked and is compliant with
  487. * the ARMv7 architecture.
  488. */
  489. vtimer->cnt_ctl = 0;
  490. ptimer->cnt_ctl = 0;
  491. kvm_timer_update_state(vcpu);
  492. return 0;
  493. }
  494. /* Make the updates of cntvoff for all vtimer contexts atomic */
  495. static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
  496. {
  497. int i;
  498. struct kvm *kvm = vcpu->kvm;
  499. struct kvm_vcpu *tmp;
  500. mutex_lock(&kvm->lock);
  501. kvm_for_each_vcpu(i, tmp, kvm)
  502. vcpu_vtimer(tmp)->cntvoff = cntvoff;
  503. /*
  504. * When called from the vcpu create path, the CPU being created is not
  505. * included in the loop above, so we just set it here as well.
  506. */
  507. vcpu_vtimer(vcpu)->cntvoff = cntvoff;
  508. mutex_unlock(&kvm->lock);
  509. }
  510. void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
  511. {
  512. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  513. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  514. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  515. /* Synchronize cntvoff across all vtimers of a VM. */
  516. update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
  517. vcpu_ptimer(vcpu)->cntvoff = 0;
  518. INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
  519. hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  520. timer->bg_timer.function = kvm_bg_timer_expire;
  521. hrtimer_init(&timer->phys_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  522. timer->phys_timer.function = kvm_phys_timer_expire;
  523. vtimer->irq.irq = default_vtimer_irq.irq;
  524. ptimer->irq.irq = default_ptimer_irq.irq;
  525. }
  526. static void kvm_timer_init_interrupt(void *info)
  527. {
  528. enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
  529. }
  530. int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
  531. {
  532. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  533. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  534. switch (regid) {
  535. case KVM_REG_ARM_TIMER_CTL:
  536. vtimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT;
  537. break;
  538. case KVM_REG_ARM_TIMER_CNT:
  539. update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
  540. break;
  541. case KVM_REG_ARM_TIMER_CVAL:
  542. vtimer->cnt_cval = value;
  543. break;
  544. case KVM_REG_ARM_PTIMER_CTL:
  545. ptimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT;
  546. break;
  547. case KVM_REG_ARM_PTIMER_CVAL:
  548. ptimer->cnt_cval = value;
  549. break;
  550. default:
  551. return -1;
  552. }
  553. kvm_timer_update_state(vcpu);
  554. return 0;
  555. }
  556. static u64 read_timer_ctl(struct arch_timer_context *timer)
  557. {
  558. /*
  559. * Set ISTATUS bit if it's expired.
  560. * Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is
  561. * UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit
  562. * regardless of ENABLE bit for our implementation convenience.
  563. */
  564. if (!kvm_timer_compute_delta(timer))
  565. return timer->cnt_ctl | ARCH_TIMER_CTRL_IT_STAT;
  566. else
  567. return timer->cnt_ctl;
  568. }
  569. u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
  570. {
  571. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  572. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  573. switch (regid) {
  574. case KVM_REG_ARM_TIMER_CTL:
  575. return read_timer_ctl(vtimer);
  576. case KVM_REG_ARM_TIMER_CNT:
  577. return kvm_phys_timer_read() - vtimer->cntvoff;
  578. case KVM_REG_ARM_TIMER_CVAL:
  579. return vtimer->cnt_cval;
  580. case KVM_REG_ARM_PTIMER_CTL:
  581. return read_timer_ctl(ptimer);
  582. case KVM_REG_ARM_PTIMER_CVAL:
  583. return ptimer->cnt_cval;
  584. case KVM_REG_ARM_PTIMER_CNT:
  585. return kvm_phys_timer_read();
  586. }
  587. return (u64)-1;
  588. }
  589. static int kvm_timer_starting_cpu(unsigned int cpu)
  590. {
  591. kvm_timer_init_interrupt(NULL);
  592. return 0;
  593. }
  594. static int kvm_timer_dying_cpu(unsigned int cpu)
  595. {
  596. disable_percpu_irq(host_vtimer_irq);
  597. return 0;
  598. }
  599. int kvm_timer_hyp_init(void)
  600. {
  601. struct arch_timer_kvm_info *info;
  602. int err;
  603. info = arch_timer_get_kvm_info();
  604. timecounter = &info->timecounter;
  605. if (!timecounter->cc) {
  606. kvm_err("kvm_arch_timer: uninitialized timecounter\n");
  607. return -ENODEV;
  608. }
  609. if (info->virtual_irq <= 0) {
  610. kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
  611. info->virtual_irq);
  612. return -ENODEV;
  613. }
  614. host_vtimer_irq = info->virtual_irq;
  615. host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
  616. if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
  617. host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
  618. kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
  619. host_vtimer_irq);
  620. host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
  621. }
  622. err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
  623. "kvm guest timer", kvm_get_running_vcpus());
  624. if (err) {
  625. kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
  626. host_vtimer_irq, err);
  627. return err;
  628. }
  629. err = irq_set_vcpu_affinity(host_vtimer_irq, kvm_get_running_vcpus());
  630. if (err) {
  631. kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
  632. goto out_free_irq;
  633. }
  634. kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
  635. cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
  636. "kvm/arm/timer:starting", kvm_timer_starting_cpu,
  637. kvm_timer_dying_cpu);
  638. return 0;
  639. out_free_irq:
  640. free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
  641. return err;
  642. }
  643. void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
  644. {
  645. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  646. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  647. soft_timer_cancel(&timer->bg_timer, &timer->expired);
  648. soft_timer_cancel(&timer->phys_timer, NULL);
  649. kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
  650. }
  651. static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
  652. {
  653. int vtimer_irq, ptimer_irq;
  654. int i, ret;
  655. vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
  656. ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
  657. if (ret)
  658. return false;
  659. ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
  660. ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
  661. if (ret)
  662. return false;
  663. kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
  664. if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
  665. vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
  666. return false;
  667. }
  668. return true;
  669. }
  670. int kvm_timer_enable(struct kvm_vcpu *vcpu)
  671. {
  672. struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
  673. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  674. struct irq_desc *desc;
  675. struct irq_data *data;
  676. int phys_irq;
  677. int ret;
  678. if (timer->enabled)
  679. return 0;
  680. /* Without a VGIC we do not map virtual IRQs to physical IRQs */
  681. if (!irqchip_in_kernel(vcpu->kvm))
  682. goto no_vgic;
  683. if (!vgic_initialized(vcpu->kvm))
  684. return -ENODEV;
  685. if (!timer_irqs_are_valid(vcpu)) {
  686. kvm_debug("incorrectly configured timer irqs\n");
  687. return -EINVAL;
  688. }
  689. /*
  690. * Find the physical IRQ number corresponding to the host_vtimer_irq
  691. */
  692. desc = irq_to_desc(host_vtimer_irq);
  693. if (!desc) {
  694. kvm_err("%s: no interrupt descriptor\n", __func__);
  695. return -EINVAL;
  696. }
  697. data = irq_desc_get_irq_data(desc);
  698. while (data->parent_data)
  699. data = data->parent_data;
  700. phys_irq = data->hwirq;
  701. /*
  702. * Tell the VGIC that the virtual interrupt is tied to a
  703. * physical interrupt. We do that once per VCPU.
  704. */
  705. ret = kvm_vgic_map_phys_irq(vcpu, vtimer->irq.irq, phys_irq);
  706. if (ret)
  707. return ret;
  708. no_vgic:
  709. timer->enabled = 1;
  710. return 0;
  711. }
  712. /*
  713. * On VHE system, we only need to configure trap on physical timer and counter
  714. * accesses in EL0 and EL1 once, not for every world switch.
  715. * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
  716. * and this makes those bits have no effect for the host kernel execution.
  717. */
  718. void kvm_timer_init_vhe(void)
  719. {
  720. /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
  721. u32 cnthctl_shift = 10;
  722. u64 val;
  723. /*
  724. * Disallow physical timer access for the guest.
  725. * Physical counter access is allowed.
  726. */
  727. val = read_sysreg(cnthctl_el2);
  728. val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift);
  729. val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
  730. write_sysreg(val, cnthctl_el2);
  731. }
  732. static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
  733. {
  734. struct kvm_vcpu *vcpu;
  735. int i;
  736. kvm_for_each_vcpu(i, vcpu, kvm) {
  737. vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
  738. vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
  739. }
  740. }
  741. int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  742. {
  743. int __user *uaddr = (int __user *)(long)attr->addr;
  744. struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
  745. struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
  746. int irq;
  747. if (!irqchip_in_kernel(vcpu->kvm))
  748. return -EINVAL;
  749. if (get_user(irq, uaddr))
  750. return -EFAULT;
  751. if (!(irq_is_ppi(irq)))
  752. return -EINVAL;
  753. if (vcpu->arch.timer_cpu.enabled)
  754. return -EBUSY;
  755. switch (attr->attr) {
  756. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  757. set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
  758. break;
  759. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  760. set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
  761. break;
  762. default:
  763. return -ENXIO;
  764. }
  765. return 0;
  766. }
  767. int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  768. {
  769. int __user *uaddr = (int __user *)(long)attr->addr;
  770. struct arch_timer_context *timer;
  771. int irq;
  772. switch (attr->attr) {
  773. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  774. timer = vcpu_vtimer(vcpu);
  775. break;
  776. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  777. timer = vcpu_ptimer(vcpu);
  778. break;
  779. default:
  780. return -ENXIO;
  781. }
  782. irq = timer->irq.irq;
  783. return put_user(irq, uaddr);
  784. }
  785. int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
  786. {
  787. switch (attr->attr) {
  788. case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
  789. case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
  790. return 0;
  791. }
  792. return -ENXIO;
  793. }