1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Western Digital Corporation or its affiliates.
4 * Copyright (C) 2022 Ventana Micro Systems Inc.
7 * Anup Patel <apatel@ventanamicro.com>
10 #include <linux/kernel.h>
11 #include <linux/bitops.h>
12 #include <linux/irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/kvm_host.h>
15 #include <linux/percpu.h>
16 #include <linux/spinlock.h>
17 #include <asm/cpufeature.h>
18 #include <asm/kvm_aia_imsic.h>
20 struct aia_hgei_control {
22 unsigned long free_bitmap;
23 struct kvm_vcpu *owners[BITS_PER_LONG];
25 static DEFINE_PER_CPU(struct aia_hgei_control, aia_hgei);
26 static int hgei_parent_irq;
28 unsigned int kvm_riscv_aia_nr_hgei;
29 unsigned int kvm_riscv_aia_max_ids;
30 DEFINE_STATIC_KEY_FALSE(kvm_riscv_aia_available);
32 static int aia_find_hgei(struct kvm_vcpu *owner)
36 struct aia_hgei_control *hgctrl = get_cpu_ptr(&aia_hgei);
38 raw_spin_lock_irqsave(&hgctrl->lock, flags);
41 for (i = 1; i <= kvm_riscv_aia_nr_hgei; i++) {
42 if (hgctrl->owners[i] == owner) {
48 raw_spin_unlock_irqrestore(&hgctrl->lock, flags);
50 put_cpu_ptr(&aia_hgei);
54 static void aia_set_hvictl(bool ext_irq_pending)
59 * HVICTL.IID == 9 and HVICTL.IPRIO == 0 represents
60 * no interrupt in HVICTL.
63 hvictl = (IRQ_S_EXT << HVICTL_IID_SHIFT) & HVICTL_IID;
64 hvictl |= ext_irq_pending;
65 csr_write(CSR_HVICTL, hvictl);
69 void kvm_riscv_vcpu_aia_flush_interrupts(struct kvm_vcpu *vcpu)
71 struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr;
72 unsigned long mask, val;
74 if (!kvm_riscv_aia_available())
77 if (READ_ONCE(vcpu->arch.irqs_pending_mask[1])) {
78 mask = xchg_acquire(&vcpu->arch.irqs_pending_mask[1], 0);
79 val = READ_ONCE(vcpu->arch.irqs_pending[1]) & mask;
86 void kvm_riscv_vcpu_aia_sync_interrupts(struct kvm_vcpu *vcpu)
88 struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr;
90 if (kvm_riscv_aia_available())
91 csr->vsieh = csr_read(CSR_VSIEH);
95 bool kvm_riscv_vcpu_aia_has_interrupts(struct kvm_vcpu *vcpu, u64 mask)
100 if (!kvm_riscv_aia_available())
104 if (READ_ONCE(vcpu->arch.irqs_pending[1]) &
105 (vcpu->arch.aia_context.guest_csr.vsieh & upper_32_bits(mask)))
109 seip = vcpu->arch.guest_csr.vsie;
110 seip &= (unsigned long)mask;
111 seip &= BIT(IRQ_S_EXT);
113 if (!kvm_riscv_aia_initialized(vcpu->kvm) || !seip)
116 hgei = aia_find_hgei(vcpu);
118 return !!(csr_read(CSR_HGEIP) & BIT(hgei));
123 void kvm_riscv_vcpu_aia_update_hvip(struct kvm_vcpu *vcpu)
125 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
127 if (!kvm_riscv_aia_available())
131 csr_write(CSR_HVIPH, vcpu->arch.aia_context.guest_csr.hviph);
133 aia_set_hvictl(!!(csr->hvip & BIT(IRQ_VS_EXT)));
136 void kvm_riscv_vcpu_aia_load(struct kvm_vcpu *vcpu, int cpu)
138 struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr;
140 if (!kvm_riscv_aia_available())
143 csr_write(CSR_VSISELECT, csr->vsiselect);
144 csr_write(CSR_HVIPRIO1, csr->hviprio1);
145 csr_write(CSR_HVIPRIO2, csr->hviprio2);
147 csr_write(CSR_VSIEH, csr->vsieh);
148 csr_write(CSR_HVIPH, csr->hviph);
149 csr_write(CSR_HVIPRIO1H, csr->hviprio1h);
150 csr_write(CSR_HVIPRIO2H, csr->hviprio2h);
154 void kvm_riscv_vcpu_aia_put(struct kvm_vcpu *vcpu)
156 struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr;
158 if (!kvm_riscv_aia_available())
161 csr->vsiselect = csr_read(CSR_VSISELECT);
162 csr->hviprio1 = csr_read(CSR_HVIPRIO1);
163 csr->hviprio2 = csr_read(CSR_HVIPRIO2);
165 csr->vsieh = csr_read(CSR_VSIEH);
166 csr->hviph = csr_read(CSR_HVIPH);
167 csr->hviprio1h = csr_read(CSR_HVIPRIO1H);
168 csr->hviprio2h = csr_read(CSR_HVIPRIO2H);
172 int kvm_riscv_vcpu_aia_get_csr(struct kvm_vcpu *vcpu,
173 unsigned long reg_num,
174 unsigned long *out_val)
176 struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr;
178 if (reg_num >= sizeof(struct kvm_riscv_aia_csr) / sizeof(unsigned long))
182 if (kvm_riscv_aia_available())
183 *out_val = ((unsigned long *)csr)[reg_num];
188 int kvm_riscv_vcpu_aia_set_csr(struct kvm_vcpu *vcpu,
189 unsigned long reg_num,
192 struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr;
194 if (reg_num >= sizeof(struct kvm_riscv_aia_csr) / sizeof(unsigned long))
197 if (kvm_riscv_aia_available()) {
198 ((unsigned long *)csr)[reg_num] = val;
201 if (reg_num == KVM_REG_RISCV_CSR_AIA_REG(siph))
202 WRITE_ONCE(vcpu->arch.irqs_pending_mask[1], 0);
209 int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcpu,
210 unsigned int csr_num,
212 unsigned long new_val,
213 unsigned long wr_mask)
215 /* If AIA not available then redirect trap */
216 if (!kvm_riscv_aia_available())
217 return KVM_INSN_ILLEGAL_TRAP;
219 /* If AIA not initialized then forward to user space */
220 if (!kvm_riscv_aia_initialized(vcpu->kvm))
221 return KVM_INSN_EXIT_TO_USER_SPACE;
223 return kvm_riscv_vcpu_aia_imsic_rmw(vcpu, KVM_RISCV_AIA_IMSIC_TOPEI,
224 val, new_val, wr_mask);
228 * External IRQ priority always read-only zero. This means default
229 * priority order is always preferred for external IRQs unless
230 * HVICTL.IID == 9 and HVICTL.IPRIO != 0
232 static int aia_irq2bitpos[] = {
233 0, 8, -1, -1, 16, 24, -1, -1, /* 0 - 7 */
234 32, -1, -1, -1, -1, 40, 48, 56, /* 8 - 15 */
235 64, 72, 80, 88, 96, 104, 112, 120, /* 16 - 23 */
236 -1, -1, -1, -1, -1, -1, -1, -1, /* 24 - 31 */
237 -1, -1, -1, -1, -1, -1, -1, -1, /* 32 - 39 */
238 -1, -1, -1, -1, -1, -1, -1, -1, /* 40 - 47 */
239 -1, -1, -1, -1, -1, -1, -1, -1, /* 48 - 55 */
240 -1, -1, -1, -1, -1, -1, -1, -1, /* 56 - 63 */
243 static u8 aia_get_iprio8(struct kvm_vcpu *vcpu, unsigned int irq)
245 unsigned long hviprio;
246 int bitpos = aia_irq2bitpos[irq];
251 switch (bitpos / BITS_PER_LONG) {
253 hviprio = csr_read(CSR_HVIPRIO1);
257 hviprio = csr_read(CSR_HVIPRIO2);
260 hviprio = csr_read(CSR_HVIPRIO1H);
263 hviprio = csr_read(CSR_HVIPRIO2);
266 hviprio = csr_read(CSR_HVIPRIO2H);
273 return (hviprio >> (bitpos % BITS_PER_LONG)) & TOPI_IPRIO_MASK;
276 static void aia_set_iprio8(struct kvm_vcpu *vcpu, unsigned int irq, u8 prio)
278 unsigned long hviprio;
279 int bitpos = aia_irq2bitpos[irq];
284 switch (bitpos / BITS_PER_LONG) {
286 hviprio = csr_read(CSR_HVIPRIO1);
290 hviprio = csr_read(CSR_HVIPRIO2);
293 hviprio = csr_read(CSR_HVIPRIO1H);
296 hviprio = csr_read(CSR_HVIPRIO2);
299 hviprio = csr_read(CSR_HVIPRIO2H);
306 hviprio &= ~(TOPI_IPRIO_MASK << (bitpos % BITS_PER_LONG));
307 hviprio |= (unsigned long)prio << (bitpos % BITS_PER_LONG);
309 switch (bitpos / BITS_PER_LONG) {
311 csr_write(CSR_HVIPRIO1, hviprio);
315 csr_write(CSR_HVIPRIO2, hviprio);
318 csr_write(CSR_HVIPRIO1H, hviprio);
321 csr_write(CSR_HVIPRIO2, hviprio);
324 csr_write(CSR_HVIPRIO2H, hviprio);
332 static int aia_rmw_iprio(struct kvm_vcpu *vcpu, unsigned int isel,
333 unsigned long *val, unsigned long new_val,
334 unsigned long wr_mask)
336 int i, first_irq, nirqs;
337 unsigned long old_val;
342 return KVM_INSN_ILLEGAL_TRAP;
345 nirqs = 4 * (BITS_PER_LONG / 32);
346 first_irq = (isel - ISELECT_IPRIO0) * 4;
349 for (i = 0; i < nirqs; i++) {
350 prio = aia_get_iprio8(vcpu, first_irq + i);
351 old_val |= (unsigned long)prio << (TOPI_IPRIO_BITS * i);
358 new_val = (old_val & ~wr_mask) | (new_val & wr_mask);
359 for (i = 0; i < nirqs; i++) {
360 prio = (new_val >> (TOPI_IPRIO_BITS * i)) &
362 aia_set_iprio8(vcpu, first_irq + i, prio);
366 return KVM_INSN_CONTINUE_NEXT_SEPC;
369 int kvm_riscv_vcpu_aia_rmw_ireg(struct kvm_vcpu *vcpu, unsigned int csr_num,
370 unsigned long *val, unsigned long new_val,
371 unsigned long wr_mask)
375 /* If AIA not available then redirect trap */
376 if (!kvm_riscv_aia_available())
377 return KVM_INSN_ILLEGAL_TRAP;
379 /* First try to emulate in kernel space */
380 isel = csr_read(CSR_VSISELECT) & ISELECT_MASK;
381 if (isel >= ISELECT_IPRIO0 && isel <= ISELECT_IPRIO15)
382 return aia_rmw_iprio(vcpu, isel, val, new_val, wr_mask);
383 else if (isel >= IMSIC_FIRST && isel <= IMSIC_LAST &&
384 kvm_riscv_aia_initialized(vcpu->kvm))
385 return kvm_riscv_vcpu_aia_imsic_rmw(vcpu, isel, val, new_val,
388 /* We can't handle it here so redirect to user space */
389 return KVM_INSN_EXIT_TO_USER_SPACE;
392 int kvm_riscv_aia_alloc_hgei(int cpu, struct kvm_vcpu *owner,
393 void __iomem **hgei_va, phys_addr_t *hgei_pa)
397 struct aia_hgei_control *hgctrl = per_cpu_ptr(&aia_hgei, cpu);
399 if (!kvm_riscv_aia_available() || !hgctrl)
402 raw_spin_lock_irqsave(&hgctrl->lock, flags);
404 if (hgctrl->free_bitmap) {
405 ret = __ffs(hgctrl->free_bitmap);
406 hgctrl->free_bitmap &= ~BIT(ret);
407 hgctrl->owners[ret] = owner;
410 raw_spin_unlock_irqrestore(&hgctrl->lock, flags);
412 /* TODO: To be updated later by AIA IMSIC HW guest file support */
421 void kvm_riscv_aia_free_hgei(int cpu, int hgei)
424 struct aia_hgei_control *hgctrl = per_cpu_ptr(&aia_hgei, cpu);
426 if (!kvm_riscv_aia_available() || !hgctrl)
429 raw_spin_lock_irqsave(&hgctrl->lock, flags);
431 if (hgei > 0 && hgei <= kvm_riscv_aia_nr_hgei) {
432 if (!(hgctrl->free_bitmap & BIT(hgei))) {
433 hgctrl->free_bitmap |= BIT(hgei);
434 hgctrl->owners[hgei] = NULL;
438 raw_spin_unlock_irqrestore(&hgctrl->lock, flags);
441 void kvm_riscv_aia_wakeon_hgei(struct kvm_vcpu *owner, bool enable)
445 if (!kvm_riscv_aia_available())
448 hgei = aia_find_hgei(owner);
451 csr_set(CSR_HGEIE, BIT(hgei));
453 csr_clear(CSR_HGEIE, BIT(hgei));
457 static irqreturn_t hgei_interrupt(int irq, void *dev_id)
460 unsigned long hgei_mask, flags;
461 struct aia_hgei_control *hgctrl = get_cpu_ptr(&aia_hgei);
463 hgei_mask = csr_read(CSR_HGEIP) & csr_read(CSR_HGEIE);
464 csr_clear(CSR_HGEIE, hgei_mask);
466 raw_spin_lock_irqsave(&hgctrl->lock, flags);
468 for_each_set_bit(i, &hgei_mask, BITS_PER_LONG) {
469 if (hgctrl->owners[i])
470 kvm_vcpu_kick(hgctrl->owners[i]);
473 raw_spin_unlock_irqrestore(&hgctrl->lock, flags);
475 put_cpu_ptr(&aia_hgei);
479 static int aia_hgei_init(void)
482 struct irq_domain *domain;
483 struct aia_hgei_control *hgctrl;
485 /* Initialize per-CPU guest external interrupt line management */
486 for_each_possible_cpu(cpu) {
487 hgctrl = per_cpu_ptr(&aia_hgei, cpu);
488 raw_spin_lock_init(&hgctrl->lock);
489 if (kvm_riscv_aia_nr_hgei) {
490 hgctrl->free_bitmap =
491 BIT(kvm_riscv_aia_nr_hgei + 1) - 1;
492 hgctrl->free_bitmap &= ~BIT(0);
494 hgctrl->free_bitmap = 0;
497 /* Find INTC irq domain */
498 domain = irq_find_matching_fwnode(riscv_get_intc_hwnode(),
501 kvm_err("unable to find INTC domain\n");
505 /* Map per-CPU SGEI interrupt from INTC domain */
506 hgei_parent_irq = irq_create_mapping(domain, IRQ_S_GEXT);
507 if (!hgei_parent_irq) {
508 kvm_err("unable to map SGEI IRQ\n");
512 /* Request per-CPU SGEI interrupt */
513 rc = request_percpu_irq(hgei_parent_irq, hgei_interrupt,
514 "riscv-kvm", &aia_hgei);
516 kvm_err("failed to request SGEI IRQ\n");
523 static void aia_hgei_exit(void)
525 /* Free per-CPU SGEI interrupt */
526 free_percpu_irq(hgei_parent_irq, &aia_hgei);
529 void kvm_riscv_aia_enable(void)
531 if (!kvm_riscv_aia_available())
534 aia_set_hvictl(false);
535 csr_write(CSR_HVIPRIO1, 0x0);
536 csr_write(CSR_HVIPRIO2, 0x0);
538 csr_write(CSR_HVIPH, 0x0);
539 csr_write(CSR_HIDELEGH, 0x0);
540 csr_write(CSR_HVIPRIO1H, 0x0);
541 csr_write(CSR_HVIPRIO2H, 0x0);
544 /* Enable per-CPU SGEI interrupt */
545 enable_percpu_irq(hgei_parent_irq,
546 irq_get_trigger_type(hgei_parent_irq));
547 csr_set(CSR_HIE, BIT(IRQ_S_GEXT));
550 void kvm_riscv_aia_disable(void)
554 struct kvm_vcpu *vcpu;
555 struct aia_hgei_control *hgctrl;
557 if (!kvm_riscv_aia_available())
559 hgctrl = get_cpu_ptr(&aia_hgei);
561 /* Disable per-CPU SGEI interrupt */
562 csr_clear(CSR_HIE, BIT(IRQ_S_GEXT));
563 disable_percpu_irq(hgei_parent_irq);
565 aia_set_hvictl(false);
567 raw_spin_lock_irqsave(&hgctrl->lock, flags);
569 for (i = 0; i <= kvm_riscv_aia_nr_hgei; i++) {
570 vcpu = hgctrl->owners[i];
575 * We release hgctrl->lock before notifying IMSIC
576 * so that we don't have lock ordering issues.
578 raw_spin_unlock_irqrestore(&hgctrl->lock, flags);
581 kvm_riscv_vcpu_aia_imsic_release(vcpu);
584 * Wakeup VCPU if it was blocked so that it can
587 if (csr_read(CSR_HGEIE) & BIT(i)) {
588 csr_clear(CSR_HGEIE, BIT(i));
592 raw_spin_lock_irqsave(&hgctrl->lock, flags);
595 raw_spin_unlock_irqrestore(&hgctrl->lock, flags);
597 put_cpu_ptr(&aia_hgei);
600 int kvm_riscv_aia_init(void)
604 if (!riscv_isa_extension_available(NULL, SxAIA))
607 /* Figure-out number of bits in HGEIE */
608 csr_write(CSR_HGEIE, -1UL);
609 kvm_riscv_aia_nr_hgei = fls_long(csr_read(CSR_HGEIE));
610 csr_write(CSR_HGEIE, 0);
611 if (kvm_riscv_aia_nr_hgei)
612 kvm_riscv_aia_nr_hgei--;
615 * Number of usable HGEI lines should be minimum of per-HART
616 * IMSIC guest files and number of bits in HGEIE
618 * TODO: To be updated later by AIA IMSIC HW guest file support
620 kvm_riscv_aia_nr_hgei = 0;
623 * Find number of guest MSI IDs
625 * TODO: To be updated later by AIA IMSIC HW guest file support
627 kvm_riscv_aia_max_ids = IMSIC_MAX_ID;
629 /* Initialize guest external interrupt line management */
630 rc = aia_hgei_init();
634 /* Register device operations */
635 rc = kvm_register_device_ops(&kvm_riscv_aia_device_ops,
636 KVM_DEV_TYPE_RISCV_AIA);
642 /* Enable KVM AIA support */
643 static_branch_enable(&kvm_riscv_aia_available);
648 void kvm_riscv_aia_exit(void)
650 if (!kvm_riscv_aia_available())
653 /* Unregister device operations */
654 kvm_unregister_device_ops(KVM_DEV_TYPE_RISCV_AIA);
656 /* Cleanup the HGEI state */