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>
12 static int has_steal_clock;
13 struct static_key paravirt_steal_enabled;
14 struct static_key paravirt_steal_rq_enabled;
15 static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
16 DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key);
18 static u64 native_steal_clock(int cpu)
23 DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
25 static bool steal_acc = true;
27 static int __init parse_no_stealacc(char *arg)
32 early_param("no-steal-acc", parse_no_stealacc);
34 static u64 paravt_steal_clock(int cpu)
38 struct kvm_steal_time *src;
40 src = &per_cpu(steal_time, cpu);
43 version = src->version;
44 virt_rmb(); /* Make sure that the version is read before the steal */
46 virt_rmb(); /* Make sure that the steal is read before the next version */
48 } while ((version & 1) || (version != src->version));
54 static struct smp_ops native_ops;
56 static void pv_send_ipi_single(int cpu, unsigned int action)
59 irq_cpustat_t *info = &per_cpu(irq_stat, cpu);
61 if (unlikely(action == ACTION_BOOT_CPU)) {
62 native_ops.send_ipi_single(cpu, action);
66 old = atomic_fetch_or(BIT(action), &info->message);
70 min = cpu_logical_map(cpu);
71 kvm_hypercall3(KVM_HCALL_FUNC_IPI, 1, 0, min);
74 #define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
76 static void pv_send_ipi_mask(const struct cpumask *mask, unsigned int action)
78 int i, cpu, min = 0, max = 0, old;
79 __uint128_t bitmap = 0;
82 if (cpumask_empty(mask))
85 if (unlikely(action == ACTION_BOOT_CPU)) {
86 native_ops.send_ipi_mask(mask, action);
91 for_each_cpu(i, mask) {
92 info = &per_cpu(irq_stat, i);
93 old = atomic_fetch_or(action, &info->message);
97 cpu = cpu_logical_map(i);
100 } else if (cpu < min && cpu > (max - KVM_IPI_CLUSTER_SIZE)) {
101 /* cpu < min, and bitmap still enough */
102 bitmap <<= min - cpu;
104 } else if (cpu > min && cpu < (min + KVM_IPI_CLUSTER_SIZE)) {
105 /* cpu > min, and bitmap still enough */
106 max = cpu > max ? cpu : max;
109 * With cpu, bitmap will exceed KVM_IPI_CLUSTER_SIZE,
110 * send IPI here directly and skip the remaining CPUs.
112 kvm_hypercall3(KVM_HCALL_FUNC_IPI, (unsigned long)bitmap,
113 (unsigned long)(bitmap >> BITS_PER_LONG), min);
117 __set_bit(cpu - min, (unsigned long *)&bitmap);
121 kvm_hypercall3(KVM_HCALL_FUNC_IPI, (unsigned long)bitmap,
122 (unsigned long)(bitmap >> BITS_PER_LONG), min);
125 static irqreturn_t pv_ipi_interrupt(int irq, void *dev)
130 /* Clear SWI interrupt */
131 clear_csr_estat(1 << INT_SWI0);
132 info = this_cpu_ptr(&irq_stat);
133 action = atomic_xchg(&info->message, 0);
135 if (action & SMP_RESCHEDULE) {
137 info->ipi_irqs[IPI_RESCHEDULE]++;
140 if (action & SMP_CALL_FUNCTION) {
141 generic_smp_call_function_interrupt();
142 info->ipi_irqs[IPI_CALL_FUNCTION]++;
145 if (action & SMP_IRQ_WORK) {
147 info->ipi_irqs[IPI_IRQ_WORK]++;
150 if (action & SMP_CLEAR_VECTOR) {
151 complete_irq_moving();
152 info->ipi_irqs[IPI_CLEAR_VECTOR]++;
158 static void pv_init_ipi(void)
162 /* Init native ipi irq for ACTION_BOOT_CPU */
163 native_ops.init_ipi();
164 swi = get_percpu_irq(INT_SWI0);
166 panic("SWI0 IRQ mapping failed\n");
167 irq_set_percpu_devid(swi);
168 r = request_percpu_irq(swi, pv_ipi_interrupt, "SWI0-IPI", &irq_stat);
170 panic("SWI0 IRQ request failed\n");
174 bool kvm_para_available(void)
177 static int hypervisor_type;
179 if (!cpu_has_hypervisor)
182 if (!hypervisor_type) {
183 config = read_cpucfg(CPUCFG_KVM_SIG);
184 if (!memcmp(&config, KVM_SIGNATURE, 4))
185 hypervisor_type = HYPERVISOR_KVM;
188 return hypervisor_type == HYPERVISOR_KVM;
191 unsigned int kvm_arch_para_features(void)
193 static unsigned int feature;
195 if (!kvm_para_available())
199 feature = read_cpucfg(CPUCFG_KVM_FEATURE);
204 int __init pv_ipi_init(void)
206 if (!kvm_para_has_feature(KVM_FEATURE_IPI))
211 mp_ops.init_ipi = pv_init_ipi;
212 mp_ops.send_ipi_single = pv_send_ipi_single;
213 mp_ops.send_ipi_mask = pv_send_ipi_mask;
219 static int pv_enable_steal_time(void)
221 int cpu = smp_processor_id();
223 struct kvm_steal_time *st;
225 if (!has_steal_clock)
228 st = &per_cpu(steal_time, cpu);
229 addr = per_cpu_ptr_to_phys(st);
231 /* The whole structure kvm_steal_time should be in one page */
232 if (PFN_DOWN(addr) != PFN_DOWN(addr + sizeof(*st))) {
233 pr_warn("Illegal PV steal time addr %lx\n", addr);
237 addr |= KVM_STEAL_PHYS_VALID;
238 kvm_hypercall2(KVM_HCALL_FUNC_NOTIFY, BIT(KVM_FEATURE_STEAL_TIME), addr);
243 static void pv_disable_steal_time(void)
246 kvm_hypercall2(KVM_HCALL_FUNC_NOTIFY, BIT(KVM_FEATURE_STEAL_TIME), 0);
250 static int pv_time_cpu_online(unsigned int cpu)
254 local_irq_save(flags);
255 pv_enable_steal_time();
256 local_irq_restore(flags);
261 static int pv_time_cpu_down_prepare(unsigned int cpu)
265 local_irq_save(flags);
266 pv_disable_steal_time();
267 local_irq_restore(flags);
273 static void pv_cpu_reboot(void *unused)
275 pv_disable_steal_time();
278 static int pv_reboot_notify(struct notifier_block *nb, unsigned long code, void *unused)
280 on_each_cpu(pv_cpu_reboot, NULL, 1);
284 static struct notifier_block pv_reboot_nb = {
285 .notifier_call = pv_reboot_notify,
288 int __init pv_time_init(void)
292 if (!kvm_para_has_feature(KVM_FEATURE_STEAL_TIME))
296 r = pv_enable_steal_time();
301 register_reboot_notifier(&pv_reboot_nb);
304 r = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
305 "loongarch/pv_time:online",
306 pv_time_cpu_online, pv_time_cpu_down_prepare);
309 pr_err("Failed to install cpu hotplug callbacks\n");
314 static_call_update(pv_steal_clock, paravt_steal_clock);
316 static_key_slow_inc(¶virt_steal_enabled);
317 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
319 static_key_slow_inc(¶virt_steal_rq_enabled);
322 pr_info("Using paravirt steal-time\n");
327 int __init pv_spinlock_init(void)
329 if (!cpu_has_hypervisor)
332 static_branch_enable(&virt_spin_lock_key);