vcpu_timer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Western Digital Corporation or its affiliates.
  4. *
  5. * Authors:
  6. * Atish Patra <atish.patra@wdc.com>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/err.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/uaccess.h>
  12. #include <clocksource/timer-riscv.h>
  13. #include <asm/delay.h>
  14. #include <asm/kvm_nacl.h>
  15. #include <asm/kvm_vcpu_timer.h>
  16. static u64 kvm_riscv_current_cycles(struct kvm_guest_timer *gt)
  17. {
  18. return get_cycles64() + gt->time_delta;
  19. }
  20. static u64 kvm_riscv_delta_cycles2ns(u64 cycles,
  21. struct kvm_guest_timer *gt,
  22. struct kvm_vcpu_timer *t)
  23. {
  24. unsigned long flags;
  25. u64 cycles_now, cycles_delta, delta_ns;
  26. local_irq_save(flags);
  27. cycles_now = kvm_riscv_current_cycles(gt);
  28. if (cycles_now < cycles)
  29. cycles_delta = cycles - cycles_now;
  30. else
  31. cycles_delta = 0;
  32. delta_ns = (cycles_delta * gt->nsec_mult) >> gt->nsec_shift;
  33. local_irq_restore(flags);
  34. return delta_ns;
  35. }
  36. static enum hrtimer_restart kvm_riscv_vcpu_hrtimer_expired(struct hrtimer *h)
  37. {
  38. u64 delta_ns;
  39. struct kvm_vcpu_timer *t = container_of(h, struct kvm_vcpu_timer, hrt);
  40. struct kvm_vcpu *vcpu = container_of(t, struct kvm_vcpu, arch.timer);
  41. struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
  42. if (kvm_riscv_current_cycles(gt) < t->next_cycles) {
  43. delta_ns = kvm_riscv_delta_cycles2ns(t->next_cycles, gt, t);
  44. hrtimer_forward_now(&t->hrt, ktime_set(0, delta_ns));
  45. return HRTIMER_RESTART;
  46. }
  47. t->next_set = false;
  48. kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_VS_TIMER);
  49. return HRTIMER_NORESTART;
  50. }
  51. static int kvm_riscv_vcpu_timer_cancel(struct kvm_vcpu_timer *t)
  52. {
  53. if (!t->init_done || !t->next_set)
  54. return -EINVAL;
  55. hrtimer_cancel(&t->hrt);
  56. t->next_set = false;
  57. return 0;
  58. }
  59. static int kvm_riscv_vcpu_update_vstimecmp(struct kvm_vcpu *vcpu, u64 ncycles)
  60. {
  61. #if defined(CONFIG_32BIT)
  62. ncsr_write(CSR_VSTIMECMP, ULONG_MAX);
  63. ncsr_write(CSR_VSTIMECMPH, ncycles >> 32);
  64. ncsr_write(CSR_VSTIMECMP, (u32)ncycles);
  65. #else
  66. ncsr_write(CSR_VSTIMECMP, ncycles);
  67. #endif
  68. return 0;
  69. }
  70. static int kvm_riscv_vcpu_update_hrtimer(struct kvm_vcpu *vcpu, u64 ncycles)
  71. {
  72. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  73. struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
  74. u64 delta_ns;
  75. if (!t->init_done)
  76. return -EINVAL;
  77. kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_VS_TIMER);
  78. delta_ns = kvm_riscv_delta_cycles2ns(ncycles, gt, t);
  79. t->next_cycles = ncycles;
  80. hrtimer_start(&t->hrt, ktime_set(0, delta_ns), HRTIMER_MODE_REL);
  81. t->next_set = true;
  82. return 0;
  83. }
  84. int kvm_riscv_vcpu_timer_next_event(struct kvm_vcpu *vcpu, u64 ncycles)
  85. {
  86. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  87. return t->timer_next_event(vcpu, ncycles);
  88. }
  89. static enum hrtimer_restart kvm_riscv_vcpu_vstimer_expired(struct hrtimer *h)
  90. {
  91. u64 delta_ns;
  92. struct kvm_vcpu_timer *t = container_of(h, struct kvm_vcpu_timer, hrt);
  93. struct kvm_vcpu *vcpu = container_of(t, struct kvm_vcpu, arch.timer);
  94. struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
  95. if (kvm_riscv_current_cycles(gt) < t->next_cycles) {
  96. delta_ns = kvm_riscv_delta_cycles2ns(t->next_cycles, gt, t);
  97. hrtimer_forward_now(&t->hrt, ktime_set(0, delta_ns));
  98. return HRTIMER_RESTART;
  99. }
  100. t->next_set = false;
  101. kvm_vcpu_kick(vcpu);
  102. return HRTIMER_NORESTART;
  103. }
  104. bool kvm_riscv_vcpu_timer_pending(struct kvm_vcpu *vcpu)
  105. {
  106. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  107. struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
  108. if (!kvm_riscv_delta_cycles2ns(t->next_cycles, gt, t) ||
  109. kvm_riscv_vcpu_has_interrupts(vcpu, 1UL << IRQ_VS_TIMER))
  110. return true;
  111. else
  112. return false;
  113. }
  114. static void kvm_riscv_vcpu_timer_blocking(struct kvm_vcpu *vcpu)
  115. {
  116. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  117. struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
  118. u64 delta_ns;
  119. if (!t->init_done)
  120. return;
  121. delta_ns = kvm_riscv_delta_cycles2ns(t->next_cycles, gt, t);
  122. hrtimer_start(&t->hrt, ktime_set(0, delta_ns), HRTIMER_MODE_REL);
  123. t->next_set = true;
  124. }
  125. static void kvm_riscv_vcpu_timer_unblocking(struct kvm_vcpu *vcpu)
  126. {
  127. kvm_riscv_vcpu_timer_cancel(&vcpu->arch.timer);
  128. }
  129. int kvm_riscv_vcpu_get_reg_timer(struct kvm_vcpu *vcpu,
  130. const struct kvm_one_reg *reg)
  131. {
  132. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  133. struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
  134. u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
  135. unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK |
  136. KVM_REG_SIZE_MASK |
  137. KVM_REG_RISCV_TIMER);
  138. u64 reg_val;
  139. if (KVM_REG_SIZE(reg->id) != sizeof(u64))
  140. return -EINVAL;
  141. if (reg_num >= sizeof(struct kvm_riscv_timer) / sizeof(u64))
  142. return -ENOENT;
  143. switch (reg_num) {
  144. case KVM_REG_RISCV_TIMER_REG(frequency):
  145. reg_val = riscv_timebase;
  146. break;
  147. case KVM_REG_RISCV_TIMER_REG(time):
  148. reg_val = kvm_riscv_current_cycles(gt);
  149. break;
  150. case KVM_REG_RISCV_TIMER_REG(compare):
  151. reg_val = t->next_cycles;
  152. break;
  153. case KVM_REG_RISCV_TIMER_REG(state):
  154. reg_val = (t->next_set) ? KVM_RISCV_TIMER_STATE_ON :
  155. KVM_RISCV_TIMER_STATE_OFF;
  156. break;
  157. default:
  158. return -ENOENT;
  159. }
  160. if (copy_to_user(uaddr, &reg_val, KVM_REG_SIZE(reg->id)))
  161. return -EFAULT;
  162. return 0;
  163. }
  164. int kvm_riscv_vcpu_set_reg_timer(struct kvm_vcpu *vcpu,
  165. const struct kvm_one_reg *reg)
  166. {
  167. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  168. struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
  169. u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
  170. unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK |
  171. KVM_REG_SIZE_MASK |
  172. KVM_REG_RISCV_TIMER);
  173. u64 reg_val;
  174. int ret = 0;
  175. if (KVM_REG_SIZE(reg->id) != sizeof(u64))
  176. return -EINVAL;
  177. if (reg_num >= sizeof(struct kvm_riscv_timer) / sizeof(u64))
  178. return -ENOENT;
  179. if (copy_from_user(&reg_val, uaddr, KVM_REG_SIZE(reg->id)))
  180. return -EFAULT;
  181. switch (reg_num) {
  182. case KVM_REG_RISCV_TIMER_REG(frequency):
  183. if (reg_val != riscv_timebase)
  184. return -EINVAL;
  185. break;
  186. case KVM_REG_RISCV_TIMER_REG(time):
  187. gt->time_delta = reg_val - get_cycles64();
  188. break;
  189. case KVM_REG_RISCV_TIMER_REG(compare):
  190. t->next_cycles = reg_val;
  191. break;
  192. case KVM_REG_RISCV_TIMER_REG(state):
  193. if (reg_val == KVM_RISCV_TIMER_STATE_ON)
  194. ret = kvm_riscv_vcpu_timer_next_event(vcpu, reg_val);
  195. else
  196. ret = kvm_riscv_vcpu_timer_cancel(t);
  197. break;
  198. default:
  199. ret = -ENOENT;
  200. break;
  201. }
  202. return ret;
  203. }
  204. int kvm_riscv_vcpu_timer_init(struct kvm_vcpu *vcpu)
  205. {
  206. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  207. if (t->init_done)
  208. return -EINVAL;
  209. t->init_done = true;
  210. t->next_set = false;
  211. /* Enable sstc for every vcpu if available in hardware */
  212. if (riscv_isa_extension_available(NULL, SSTC)) {
  213. t->sstc_enabled = true;
  214. hrtimer_setup(&t->hrt, kvm_riscv_vcpu_vstimer_expired, CLOCK_MONOTONIC,
  215. HRTIMER_MODE_REL);
  216. t->timer_next_event = kvm_riscv_vcpu_update_vstimecmp;
  217. } else {
  218. t->sstc_enabled = false;
  219. hrtimer_setup(&t->hrt, kvm_riscv_vcpu_hrtimer_expired, CLOCK_MONOTONIC,
  220. HRTIMER_MODE_REL);
  221. t->timer_next_event = kvm_riscv_vcpu_update_hrtimer;
  222. }
  223. return 0;
  224. }
  225. int kvm_riscv_vcpu_timer_deinit(struct kvm_vcpu *vcpu)
  226. {
  227. int ret;
  228. ret = kvm_riscv_vcpu_timer_cancel(&vcpu->arch.timer);
  229. vcpu->arch.timer.init_done = false;
  230. return ret;
  231. }
  232. int kvm_riscv_vcpu_timer_reset(struct kvm_vcpu *vcpu)
  233. {
  234. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  235. t->next_cycles = -1ULL;
  236. return kvm_riscv_vcpu_timer_cancel(&vcpu->arch.timer);
  237. }
  238. static void kvm_riscv_vcpu_update_timedelta(struct kvm_vcpu *vcpu)
  239. {
  240. struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
  241. #if defined(CONFIG_32BIT)
  242. ncsr_write(CSR_HTIMEDELTA, (u32)(gt->time_delta));
  243. ncsr_write(CSR_HTIMEDELTAH, (u32)(gt->time_delta >> 32));
  244. #else
  245. ncsr_write(CSR_HTIMEDELTA, gt->time_delta);
  246. #endif
  247. }
  248. void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu)
  249. {
  250. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  251. kvm_riscv_vcpu_update_timedelta(vcpu);
  252. if (!t->sstc_enabled)
  253. return;
  254. #if defined(CONFIG_32BIT)
  255. ncsr_write(CSR_VSTIMECMP, ULONG_MAX);
  256. ncsr_write(CSR_VSTIMECMPH, (u32)(t->next_cycles >> 32));
  257. ncsr_write(CSR_VSTIMECMP, (u32)(t->next_cycles));
  258. #else
  259. ncsr_write(CSR_VSTIMECMP, t->next_cycles);
  260. #endif
  261. /* timer should be enabled for the remaining operations */
  262. if (unlikely(!t->init_done))
  263. return;
  264. kvm_riscv_vcpu_timer_unblocking(vcpu);
  265. }
  266. void kvm_riscv_vcpu_timer_sync(struct kvm_vcpu *vcpu)
  267. {
  268. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  269. if (!t->sstc_enabled)
  270. return;
  271. #if defined(CONFIG_32BIT)
  272. t->next_cycles = ncsr_read(CSR_VSTIMECMP);
  273. t->next_cycles |= (u64)ncsr_read(CSR_VSTIMECMPH) << 32;
  274. #else
  275. t->next_cycles = ncsr_read(CSR_VSTIMECMP);
  276. #endif
  277. }
  278. void kvm_riscv_vcpu_timer_save(struct kvm_vcpu *vcpu)
  279. {
  280. struct kvm_vcpu_timer *t = &vcpu->arch.timer;
  281. if (!t->sstc_enabled)
  282. return;
  283. /*
  284. * The vstimecmp CSRs are saved by kvm_riscv_vcpu_timer_sync()
  285. * upon every VM exit so no need to save here.
  286. *
  287. * If VS-timer expires when no VCPU running on a host CPU then
  288. * WFI executed by such host CPU will be effective NOP resulting
  289. * in no power savings. This is because as-per RISC-V Privileged
  290. * specificaiton: "WFI is also required to resume execution for
  291. * locally enabled interrupts pending at any privilege level,
  292. * regardless of the global interrupt enable at each privilege
  293. * level."
  294. *
  295. * To address the above issue, vstimecmp CSR must be set to -1UL
  296. * over here when VCPU is scheduled-out or exits to user space.
  297. */
  298. csr_write(CSR_VSTIMECMP, -1UL);
  299. #if defined(CONFIG_32BIT)
  300. csr_write(CSR_VSTIMECMPH, -1UL);
  301. #endif
  302. /* timer should be enabled for the remaining operations */
  303. if (unlikely(!t->init_done))
  304. return;
  305. if (kvm_vcpu_is_blocking(vcpu))
  306. kvm_riscv_vcpu_timer_blocking(vcpu);
  307. }
  308. void kvm_riscv_guest_timer_init(struct kvm *kvm)
  309. {
  310. struct kvm_guest_timer *gt = &kvm->arch.timer;
  311. riscv_cs_get_mult_shift(&gt->nsec_mult, &gt->nsec_shift);
  312. gt->time_delta = -get_cycles64();
  313. }