paravirt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include <linux/types.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/irq_work.h>
  6. #include <linux/jump_label.h>
  7. #include <linux/kvm_para.h>
  8. #include <linux/reboot.h>
  9. #include <linux/static_call.h>
  10. #include <asm/paravirt.h>
  11. static int has_steal_clock;
  12. struct static_key paravirt_steal_enabled;
  13. struct static_key paravirt_steal_rq_enabled;
  14. static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
  15. DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key);
  16. static u64 native_steal_clock(int cpu)
  17. {
  18. return 0;
  19. }
  20. DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
  21. static bool steal_acc = true;
  22. static int __init parse_no_stealacc(char *arg)
  23. {
  24. steal_acc = false;
  25. return 0;
  26. }
  27. early_param("no-steal-acc", parse_no_stealacc);
  28. static u64 paravt_steal_clock(int cpu)
  29. {
  30. int version;
  31. u64 steal;
  32. struct kvm_steal_time *src;
  33. src = &per_cpu(steal_time, cpu);
  34. do {
  35. version = src->version;
  36. virt_rmb(); /* Make sure that the version is read before the steal */
  37. steal = src->steal;
  38. virt_rmb(); /* Make sure that the steal is read before the next version */
  39. } while ((version & 1) || (version != src->version));
  40. return steal;
  41. }
  42. #ifdef CONFIG_SMP
  43. static struct smp_ops native_ops;
  44. static void pv_send_ipi_single(int cpu, unsigned int action)
  45. {
  46. int min, old;
  47. irq_cpustat_t *info = &per_cpu(irq_stat, cpu);
  48. if (unlikely(action == ACTION_BOOT_CPU)) {
  49. native_ops.send_ipi_single(cpu, action);
  50. return;
  51. }
  52. old = atomic_fetch_or(BIT(action), &info->message);
  53. if (old)
  54. return;
  55. min = cpu_logical_map(cpu);
  56. kvm_hypercall3(KVM_HCALL_FUNC_IPI, 1, 0, min);
  57. }
  58. #define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
  59. static void pv_send_ipi_mask(const struct cpumask *mask, unsigned int action)
  60. {
  61. int i, cpu, min = 0, max = 0, old;
  62. __uint128_t bitmap = 0;
  63. irq_cpustat_t *info;
  64. if (cpumask_empty(mask))
  65. return;
  66. if (unlikely(action == ACTION_BOOT_CPU)) {
  67. native_ops.send_ipi_mask(mask, action);
  68. return;
  69. }
  70. action = BIT(action);
  71. for_each_cpu(i, mask) {
  72. info = &per_cpu(irq_stat, i);
  73. old = atomic_fetch_or(action, &info->message);
  74. if (old)
  75. continue;
  76. cpu = cpu_logical_map(i);
  77. if (!bitmap) {
  78. min = max = cpu;
  79. } else if (cpu < min && cpu > (max - KVM_IPI_CLUSTER_SIZE)) {
  80. /* cpu < min, and bitmap still enough */
  81. bitmap <<= min - cpu;
  82. min = cpu;
  83. } else if (cpu > min && cpu < (min + KVM_IPI_CLUSTER_SIZE)) {
  84. /* cpu > min, and bitmap still enough */
  85. max = cpu > max ? cpu : max;
  86. } else {
  87. /*
  88. * With cpu, bitmap will exceed KVM_IPI_CLUSTER_SIZE,
  89. * send IPI here directly and skip the remaining CPUs.
  90. */
  91. kvm_hypercall3(KVM_HCALL_FUNC_IPI, (unsigned long)bitmap,
  92. (unsigned long)(bitmap >> BITS_PER_LONG), min);
  93. min = max = cpu;
  94. bitmap = 0;
  95. }
  96. __set_bit(cpu - min, (unsigned long *)&bitmap);
  97. }
  98. if (bitmap)
  99. kvm_hypercall3(KVM_HCALL_FUNC_IPI, (unsigned long)bitmap,
  100. (unsigned long)(bitmap >> BITS_PER_LONG), min);
  101. }
  102. static irqreturn_t pv_ipi_interrupt(int irq, void *dev)
  103. {
  104. u32 action;
  105. irq_cpustat_t *info;
  106. /* Clear SWI interrupt */
  107. clear_csr_estat(1 << INT_SWI0);
  108. info = this_cpu_ptr(&irq_stat);
  109. action = atomic_xchg(&info->message, 0);
  110. if (action & SMP_RESCHEDULE) {
  111. scheduler_ipi();
  112. info->ipi_irqs[IPI_RESCHEDULE]++;
  113. }
  114. if (action & SMP_CALL_FUNCTION) {
  115. generic_smp_call_function_interrupt();
  116. info->ipi_irqs[IPI_CALL_FUNCTION]++;
  117. }
  118. if (action & SMP_IRQ_WORK) {
  119. irq_work_run();
  120. info->ipi_irqs[IPI_IRQ_WORK]++;
  121. }
  122. if (action & SMP_CLEAR_VECTOR) {
  123. complete_irq_moving();
  124. info->ipi_irqs[IPI_CLEAR_VECTOR]++;
  125. }
  126. return IRQ_HANDLED;
  127. }
  128. static void pv_init_ipi(void)
  129. {
  130. int r, swi;
  131. /* Init native ipi irq for ACTION_BOOT_CPU */
  132. native_ops.init_ipi();
  133. swi = get_percpu_irq(INT_SWI0);
  134. if (swi < 0)
  135. panic("SWI0 IRQ mapping failed\n");
  136. irq_set_percpu_devid(swi);
  137. r = request_percpu_irq(swi, pv_ipi_interrupt, "SWI0-IPI", &irq_stat);
  138. if (r < 0)
  139. panic("SWI0 IRQ request failed\n");
  140. }
  141. #endif
  142. bool kvm_para_available(void)
  143. {
  144. int config;
  145. static int hypervisor_type;
  146. if (!cpu_has_hypervisor)
  147. return false;
  148. if (!hypervisor_type) {
  149. config = read_cpucfg(CPUCFG_KVM_SIG);
  150. if (!memcmp(&config, KVM_SIGNATURE, 4))
  151. hypervisor_type = HYPERVISOR_KVM;
  152. }
  153. return hypervisor_type == HYPERVISOR_KVM;
  154. }
  155. unsigned int kvm_arch_para_features(void)
  156. {
  157. static unsigned int feature;
  158. if (!kvm_para_available())
  159. return 0;
  160. if (!feature)
  161. feature = read_cpucfg(CPUCFG_KVM_FEATURE);
  162. return feature;
  163. }
  164. int __init pv_ipi_init(void)
  165. {
  166. if (!kvm_para_has_feature(KVM_FEATURE_IPI))
  167. return 0;
  168. #ifdef CONFIG_SMP
  169. native_ops = mp_ops;
  170. mp_ops.init_ipi = pv_init_ipi;
  171. mp_ops.send_ipi_single = pv_send_ipi_single;
  172. mp_ops.send_ipi_mask = pv_send_ipi_mask;
  173. #endif
  174. return 0;
  175. }
  176. static int pv_enable_steal_time(void)
  177. {
  178. int cpu = smp_processor_id();
  179. unsigned long addr;
  180. struct kvm_steal_time *st;
  181. if (!has_steal_clock)
  182. return -EPERM;
  183. st = &per_cpu(steal_time, cpu);
  184. addr = per_cpu_ptr_to_phys(st);
  185. /* The whole structure kvm_steal_time should be in one page */
  186. if (PFN_DOWN(addr) != PFN_DOWN(addr + sizeof(*st))) {
  187. pr_warn("Illegal PV steal time addr %lx\n", addr);
  188. return -EFAULT;
  189. }
  190. addr |= KVM_STEAL_PHYS_VALID;
  191. kvm_hypercall2(KVM_HCALL_FUNC_NOTIFY, BIT(KVM_FEATURE_STEAL_TIME), addr);
  192. return 0;
  193. }
  194. static void pv_disable_steal_time(void)
  195. {
  196. if (has_steal_clock)
  197. kvm_hypercall2(KVM_HCALL_FUNC_NOTIFY, BIT(KVM_FEATURE_STEAL_TIME), 0);
  198. }
  199. #ifdef CONFIG_SMP
  200. static int pv_time_cpu_online(unsigned int cpu)
  201. {
  202. unsigned long flags;
  203. local_irq_save(flags);
  204. pv_enable_steal_time();
  205. local_irq_restore(flags);
  206. return 0;
  207. }
  208. static int pv_time_cpu_down_prepare(unsigned int cpu)
  209. {
  210. unsigned long flags;
  211. local_irq_save(flags);
  212. pv_disable_steal_time();
  213. local_irq_restore(flags);
  214. return 0;
  215. }
  216. #endif
  217. static void pv_cpu_reboot(void *unused)
  218. {
  219. pv_disable_steal_time();
  220. }
  221. static int pv_reboot_notify(struct notifier_block *nb, unsigned long code, void *unused)
  222. {
  223. on_each_cpu(pv_cpu_reboot, NULL, 1);
  224. return NOTIFY_DONE;
  225. }
  226. static struct notifier_block pv_reboot_nb = {
  227. .notifier_call = pv_reboot_notify,
  228. };
  229. int __init pv_time_init(void)
  230. {
  231. int r;
  232. if (!kvm_para_has_feature(KVM_FEATURE_STEAL_TIME))
  233. return 0;
  234. has_steal_clock = 1;
  235. r = pv_enable_steal_time();
  236. if (r < 0) {
  237. has_steal_clock = 0;
  238. return 0;
  239. }
  240. register_reboot_notifier(&pv_reboot_nb);
  241. #ifdef CONFIG_SMP
  242. r = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  243. "loongarch/pv_time:online",
  244. pv_time_cpu_online, pv_time_cpu_down_prepare);
  245. if (r < 0) {
  246. has_steal_clock = 0;
  247. pr_err("Failed to install cpu hotplug callbacks\n");
  248. return r;
  249. }
  250. #endif
  251. static_call_update(pv_steal_clock, paravt_steal_clock);
  252. static_key_slow_inc(&paravirt_steal_enabled);
  253. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  254. if (steal_acc)
  255. static_key_slow_inc(&paravirt_steal_rq_enabled);
  256. #endif
  257. pr_info("Using paravirt steal-time\n");
  258. return 0;
  259. }
  260. int __init pv_spinlock_init(void)
  261. {
  262. if (!cpu_has_hypervisor)
  263. return 0;
  264. static_branch_enable(&virt_spin_lock_key);
  265. return 0;
  266. }