|
5 | 5 |
|
6 | 6 | #include <linux/kvm.h>
|
7 | 7 | #include <linux/sizes.h>
|
| 8 | +#include <asm/kvm_para.h> |
8 | 9 | #include <asm/kvm.h>
|
9 | 10 |
|
10 | 11 | #include "kvm_util.h"
|
11 | 12 | #include "../kvm_util_internal.h"
|
12 | 13 | #include "vgic.h"
|
| 14 | +#include "gic.h" |
| 15 | +#include "gic_v3.h" |
13 | 16 |
|
14 | 17 | /*
|
15 | 18 | * vGIC-v3 default host setup
|
@@ -68,3 +71,93 @@ int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus,
|
68 | 71 |
|
69 | 72 | return gic_fd;
|
70 | 73 | }
|
| 74 | + |
| 75 | +/* should only work for level sensitive interrupts */ |
| 76 | +int _kvm_irq_set_level_info(int gic_fd, uint32_t intid, int level) |
| 77 | +{ |
| 78 | + uint64_t attr = 32 * (intid / 32); |
| 79 | + uint64_t index = intid % 32; |
| 80 | + uint64_t val; |
| 81 | + int ret; |
| 82 | + |
| 83 | + ret = _kvm_device_access(gic_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO, |
| 84 | + attr, &val, false); |
| 85 | + if (ret != 0) |
| 86 | + return ret; |
| 87 | + |
| 88 | + val |= 1U << index; |
| 89 | + ret = _kvm_device_access(gic_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO, |
| 90 | + attr, &val, true); |
| 91 | + return ret; |
| 92 | +} |
| 93 | + |
| 94 | +void kvm_irq_set_level_info(int gic_fd, uint32_t intid, int level) |
| 95 | +{ |
| 96 | + int ret = _kvm_irq_set_level_info(gic_fd, intid, level); |
| 97 | + |
| 98 | + TEST_ASSERT(ret == 0, "KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO failed, " |
| 99 | + "rc: %i errno: %i", ret, errno); |
| 100 | +} |
| 101 | + |
| 102 | +int _kvm_arm_irq_line(struct kvm_vm *vm, uint32_t intid, int level) |
| 103 | +{ |
| 104 | + uint32_t irq = intid & KVM_ARM_IRQ_NUM_MASK; |
| 105 | + |
| 106 | + if (INTID_IS_PPI(intid)) |
| 107 | + irq |= KVM_ARM_IRQ_TYPE_PPI << KVM_ARM_IRQ_TYPE_SHIFT; |
| 108 | + else if (INTID_IS_SPI(intid)) |
| 109 | + irq |= KVM_ARM_IRQ_TYPE_SPI << KVM_ARM_IRQ_TYPE_SHIFT; |
| 110 | + else |
| 111 | + TEST_FAIL("KVM_IRQ_LINE can't be used with SGIs."); |
| 112 | + |
| 113 | + return _kvm_irq_line(vm, irq, level); |
| 114 | +} |
| 115 | + |
| 116 | +void kvm_arm_irq_line(struct kvm_vm *vm, uint32_t intid, int level) |
| 117 | +{ |
| 118 | + int ret = _kvm_arm_irq_line(vm, intid, level); |
| 119 | + |
| 120 | + TEST_ASSERT(ret == 0, "KVM_IRQ_LINE failed, rc: %i errno: %i", |
| 121 | + ret, errno); |
| 122 | +} |
| 123 | + |
| 124 | +static void vgic_poke_irq(int gic_fd, uint32_t intid, |
| 125 | + uint32_t vcpu, uint64_t reg_off) |
| 126 | +{ |
| 127 | + uint64_t reg = intid / 32; |
| 128 | + uint64_t index = intid % 32; |
| 129 | + uint64_t attr = reg_off + reg * 4; |
| 130 | + uint64_t val; |
| 131 | + bool intid_is_private = INTID_IS_SGI(intid) || INTID_IS_PPI(intid); |
| 132 | + |
| 133 | + /* Check that the addr part of the attr is within 32 bits. */ |
| 134 | + assert(attr <= KVM_DEV_ARM_VGIC_OFFSET_MASK); |
| 135 | + |
| 136 | + uint32_t group = intid_is_private ? KVM_DEV_ARM_VGIC_GRP_REDIST_REGS |
| 137 | + : KVM_DEV_ARM_VGIC_GRP_DIST_REGS; |
| 138 | + |
| 139 | + if (intid_is_private) { |
| 140 | + /* TODO: only vcpu 0 implemented for now. */ |
| 141 | + assert(vcpu == 0); |
| 142 | + attr += SZ_64K; |
| 143 | + } |
| 144 | + |
| 145 | + /* All calls will succeed, even with invalid intid's, as long as the |
| 146 | + * addr part of the attr is within 32 bits (checked above). An invalid |
| 147 | + * intid will just make the read/writes point to above the intended |
| 148 | + * register space (i.e., ICPENDR after ISPENDR). |
| 149 | + */ |
| 150 | + kvm_device_access(gic_fd, group, attr, &val, false); |
| 151 | + val |= 1ULL << index; |
| 152 | + kvm_device_access(gic_fd, group, attr, &val, true); |
| 153 | +} |
| 154 | + |
| 155 | +void kvm_irq_write_ispendr(int gic_fd, uint32_t intid, uint32_t vcpu) |
| 156 | +{ |
| 157 | + vgic_poke_irq(gic_fd, intid, vcpu, GICD_ISPENDR); |
| 158 | +} |
| 159 | + |
| 160 | +void kvm_irq_write_isactiver(int gic_fd, uint32_t intid, uint32_t vcpu) |
| 161 | +{ |
| 162 | + vgic_poke_irq(gic_fd, intid, vcpu, GICD_ISACTIVER); |
| 163 | +} |
0 commit comments