Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'kvm-arm-for-v4.12-round2' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm into HEAD

Second round of KVM/ARM Changes for v4.12.

Changes include:
- A fix related to the 32-bit idmap stub
- A fix to the bitmask used to deode the operands of an AArch32 CP
instruction
- We have moved the files shared between arch/arm/kvm and
arch/arm64/kvm to virt/kvm/arm
- We add support for saving/restoring the virtual ITS state to
userspace

+1838 -538
+121
Documentation/virtual/kvm/devices/arm-vgic-its.txt
··· 32 32 KVM_DEV_ARM_VGIC_CTRL_INIT 33 33 request the initialization of the ITS, no additional parameter in 34 34 kvm_device_attr.addr. 35 + 36 + KVM_DEV_ARM_ITS_SAVE_TABLES 37 + save the ITS table data into guest RAM, at the location provisioned 38 + by the guest in corresponding registers/table entries. 39 + 40 + The layout of the tables in guest memory defines an ABI. The entries 41 + are laid out in little endian format as described in the last paragraph. 42 + 43 + KVM_DEV_ARM_ITS_RESTORE_TABLES 44 + restore the ITS tables from guest RAM to ITS internal structures. 45 + 46 + The GICV3 must be restored before the ITS and all ITS registers but 47 + the GITS_CTLR must be restored before restoring the ITS tables. 48 + 49 + The GITS_IIDR read-only register must also be restored before 50 + calling KVM_DEV_ARM_ITS_RESTORE_TABLES as the IIDR revision field 51 + encodes the ABI revision. 52 + 53 + The expected ordering when restoring the GICv3/ITS is described in section 54 + "ITS Restore Sequence". 55 + 35 56 Errors: 36 57 -ENXIO: ITS not properly configured as required prior to setting 37 58 this attribute 38 59 -ENOMEM: Memory shortage when allocating ITS internal data 60 + -EINVAL: Inconsistent restored data 61 + -EFAULT: Invalid guest ram access 62 + -EBUSY: One or more VCPUS are running 63 + 64 + KVM_DEV_ARM_VGIC_GRP_ITS_REGS 65 + Attributes: 66 + The attr field of kvm_device_attr encodes the offset of the 67 + ITS register, relative to the ITS control frame base address 68 + (ITS_base). 69 + 70 + kvm_device_attr.addr points to a __u64 value whatever the width 71 + of the addressed register (32/64 bits). 64 bit registers can only 72 + be accessed with full length. 73 + 74 + Writes to read-only registers are ignored by the kernel except for: 75 + - GITS_CREADR. It must be restored otherwise commands in the queue 76 + will be re-executed after restoring CWRITER. GITS_CREADR must be 77 + restored before restoring the GITS_CTLR which is likely to enable the 78 + ITS. Also it must be restored after GITS_CBASER since a write to 79 + GITS_CBASER resets GITS_CREADR. 80 + - GITS_IIDR. The Revision field encodes the table layout ABI revision. 81 + In the future we might implement direct injection of virtual LPIs. 82 + This will require an upgrade of the table layout and an evolution of 83 + the ABI. GITS_IIDR must be restored before calling 84 + KVM_DEV_ARM_ITS_RESTORE_TABLES. 85 + 86 + For other registers, getting or setting a register has the same 87 + effect as reading/writing the register on real hardware. 88 + Errors: 89 + -ENXIO: Offset does not correspond to any supported register 90 + -EFAULT: Invalid user pointer for attr->addr 91 + -EINVAL: Offset is not 64-bit aligned 92 + -EBUSY: one or more VCPUS are running 93 + 94 + ITS Restore Sequence: 95 + ------------------------- 96 + 97 + The following ordering must be followed when restoring the GIC and the ITS: 98 + a) restore all guest memory and create vcpus 99 + b) restore all redistributors 100 + c) provide the its base address 101 + (KVM_DEV_ARM_VGIC_GRP_ADDR) 102 + d) restore the ITS in the following order: 103 + 1. Restore GITS_CBASER 104 + 2. Restore all other GITS_ registers, except GITS_CTLR! 105 + 3. Load the ITS table data (KVM_DEV_ARM_ITS_RESTORE_TABLES) 106 + 4. Restore GITS_CTLR 107 + 108 + Then vcpus can be started. 109 + 110 + ITS Table ABI REV0: 111 + ------------------- 112 + 113 + Revision 0 of the ABI only supports the features of a virtual GICv3, and does 114 + not support a virtual GICv4 with support for direct injection of virtual 115 + interrupts for nested hypervisors. 116 + 117 + The device table and ITT are indexed by the DeviceID and EventID, 118 + respectively. The collection table is not indexed by CollectionID, and the 119 + entries in the collection are listed in no particular order. 120 + All entries are 8 bytes. 121 + 122 + Device Table Entry (DTE): 123 + 124 + bits: | 63| 62 ... 49 | 48 ... 5 | 4 ... 0 | 125 + values: | V | next | ITT_addr | Size | 126 + 127 + where; 128 + - V indicates whether the entry is valid. If not, other fields 129 + are not meaningful. 130 + - next: equals to 0 if this entry is the last one; otherwise it 131 + corresponds to the DeviceID offset to the next DTE, capped by 132 + 2^14 -1. 133 + - ITT_addr matches bits [51:8] of the ITT address (256 Byte aligned). 134 + - Size specifies the supported number of bits for the EventID, 135 + minus one 136 + 137 + Collection Table Entry (CTE): 138 + 139 + bits: | 63| 62 .. 52 | 51 ... 16 | 15 ... 0 | 140 + values: | V | RES0 | RDBase | ICID | 141 + 142 + where: 143 + - V indicates whether the entry is valid. If not, other fields are 144 + not meaningful. 145 + - RES0: reserved field with Should-Be-Zero-or-Preserved behavior. 146 + - RDBase is the PE number (GICR_TYPER.Processor_Number semantic), 147 + - ICID is the collection ID 148 + 149 + Interrupt Translation Entry (ITE): 150 + 151 + bits: | 63 ... 48 | 47 ... 16 | 15 ... 0 | 152 + values: | next | pINTID | ICID | 153 + 154 + where: 155 + - next: equals to 0 if this entry is the last one; otherwise it corresponds 156 + to the EventID offset to the next ITE capped by 2^16 -1. 157 + - pINTID is the physical LPI ID; if zero, it means the entry is not valid 158 + and other fields are not meaningful. 159 + - ICID is the collection ID
+6
Documentation/virtual/kvm/devices/arm-vgic-v3.txt
··· 167 167 KVM_DEV_ARM_VGIC_CTRL_INIT 168 168 request the initialization of the VGIC, no additional parameter in 169 169 kvm_device_attr.addr. 170 + KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 171 + save all LPI pending bits into guest RAM pending tables. 172 + 173 + The first kB of the pending table is not altered by this operation. 170 174 Errors: 171 175 -ENXIO: VGIC not properly configured as required prior to calling 172 176 this attribute 173 177 -ENODEV: no online VCPU 174 178 -ENOMEM: memory shortage when allocating vgic internal data 179 + -EFAULT: Invalid guest ram access 180 + -EBUSY: One or more VCPUS are running 175 181 176 182 177 183 KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO
+5 -1
arch/arm/include/uapi/asm/kvm.h
··· 196 196 #define KVM_DEV_ARM_VGIC_GRP_REDIST_REGS 5 197 197 #define KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS 6 198 198 #define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO 7 199 + #define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8 199 200 #define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10 200 201 #define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ 201 202 (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT) 202 203 #define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK 0x3ff 203 204 #define VGIC_LEVEL_INFO_LINE_LEVEL 0 204 205 205 - #define KVM_DEV_ARM_VGIC_CTRL_INIT 0 206 + #define KVM_DEV_ARM_VGIC_CTRL_INIT 0 207 + #define KVM_DEV_ARM_ITS_SAVE_TABLES 1 208 + #define KVM_DEV_ARM_ITS_RESTORE_TABLES 2 209 + #define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3 206 210 207 211 /* KVM_IRQ_LINE irq field index values */ 208 212 #define KVM_ARM_IRQ_TYPE_SHIFT 24
+5 -2
arch/arm/kvm/Makefile
··· 18 18 kvm-arm-y = $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o $(KVM)/vfio.o 19 19 20 20 obj-$(CONFIG_KVM_ARM_HOST) += hyp/ 21 + 21 22 obj-y += kvm-arm.o init.o interrupts.o 22 - obj-y += arm.o handle_exit.o guest.o mmu.o emulate.o reset.o 23 - obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o vgic-v3-coproc.o 23 + obj-y += handle_exit.o guest.o emulate.o reset.o 24 + obj-y += coproc.o coproc_a15.o coproc_a7.o vgic-v3-coproc.o 25 + obj-y += $(KVM)/arm/arm.o $(KVM)/arm/mmu.o $(KVM)/arm/mmio.o 26 + obj-y += $(KVM)/arm/psci.o $(KVM)/arm/perf.o 24 27 obj-y += $(KVM)/arm/aarch32.o 25 28 26 29 obj-y += $(KVM)/arm/vgic/vgic.o
+1 -1
arch/arm/kvm/arm.c virt/kvm/arm/arm.c
··· 332 332 333 333 kvm_arm_reset_debug_ptr(vcpu); 334 334 335 - return 0; 335 + return kvm_vgic_vcpu_init(vcpu); 336 336 } 337 337 338 338 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
arch/arm/kvm/mmio.c virt/kvm/arm/mmio.c
arch/arm/kvm/mmu.c virt/kvm/arm/mmu.c
arch/arm/kvm/perf.c virt/kvm/arm/perf.c
arch/arm/kvm/psci.c virt/kvm/arm/psci.c
-247
arch/arm/kvm/trace.h
··· 6 6 #undef TRACE_SYSTEM 7 7 #define TRACE_SYSTEM kvm 8 8 9 - /* 10 - * Tracepoints for entry/exit to guest 11 - */ 12 - TRACE_EVENT(kvm_entry, 13 - TP_PROTO(unsigned long vcpu_pc), 14 - TP_ARGS(vcpu_pc), 15 - 16 - TP_STRUCT__entry( 17 - __field( unsigned long, vcpu_pc ) 18 - ), 19 - 20 - TP_fast_assign( 21 - __entry->vcpu_pc = vcpu_pc; 22 - ), 23 - 24 - TP_printk("PC: 0x%08lx", __entry->vcpu_pc) 25 - ); 26 - 27 - TRACE_EVENT(kvm_exit, 28 - TP_PROTO(int idx, unsigned int exit_reason, unsigned long vcpu_pc), 29 - TP_ARGS(idx, exit_reason, vcpu_pc), 30 - 31 - TP_STRUCT__entry( 32 - __field( int, idx ) 33 - __field( unsigned int, exit_reason ) 34 - __field( unsigned long, vcpu_pc ) 35 - ), 36 - 37 - TP_fast_assign( 38 - __entry->idx = idx; 39 - __entry->exit_reason = exit_reason; 40 - __entry->vcpu_pc = vcpu_pc; 41 - ), 42 - 43 - TP_printk("%s: HSR_EC: 0x%04x (%s), PC: 0x%08lx", 44 - __print_symbolic(__entry->idx, kvm_arm_exception_type), 45 - __entry->exit_reason, 46 - __print_symbolic(__entry->exit_reason, kvm_arm_exception_class), 47 - __entry->vcpu_pc) 48 - ); 49 - 50 - TRACE_EVENT(kvm_guest_fault, 51 - TP_PROTO(unsigned long vcpu_pc, unsigned long hsr, 52 - unsigned long hxfar, 53 - unsigned long long ipa), 54 - TP_ARGS(vcpu_pc, hsr, hxfar, ipa), 55 - 56 - TP_STRUCT__entry( 57 - __field( unsigned long, vcpu_pc ) 58 - __field( unsigned long, hsr ) 59 - __field( unsigned long, hxfar ) 60 - __field( unsigned long long, ipa ) 61 - ), 62 - 63 - TP_fast_assign( 64 - __entry->vcpu_pc = vcpu_pc; 65 - __entry->hsr = hsr; 66 - __entry->hxfar = hxfar; 67 - __entry->ipa = ipa; 68 - ), 69 - 70 - TP_printk("ipa %#llx, hsr %#08lx, hxfar %#08lx, pc %#08lx", 71 - __entry->ipa, __entry->hsr, 72 - __entry->hxfar, __entry->vcpu_pc) 73 - ); 74 - 75 - TRACE_EVENT(kvm_access_fault, 76 - TP_PROTO(unsigned long ipa), 77 - TP_ARGS(ipa), 78 - 79 - TP_STRUCT__entry( 80 - __field( unsigned long, ipa ) 81 - ), 82 - 83 - TP_fast_assign( 84 - __entry->ipa = ipa; 85 - ), 86 - 87 - TP_printk("IPA: %lx", __entry->ipa) 88 - ); 89 - 90 - TRACE_EVENT(kvm_irq_line, 91 - TP_PROTO(unsigned int type, int vcpu_idx, int irq_num, int level), 92 - TP_ARGS(type, vcpu_idx, irq_num, level), 93 - 94 - TP_STRUCT__entry( 95 - __field( unsigned int, type ) 96 - __field( int, vcpu_idx ) 97 - __field( int, irq_num ) 98 - __field( int, level ) 99 - ), 100 - 101 - TP_fast_assign( 102 - __entry->type = type; 103 - __entry->vcpu_idx = vcpu_idx; 104 - __entry->irq_num = irq_num; 105 - __entry->level = level; 106 - ), 107 - 108 - TP_printk("Inject %s interrupt (%d), vcpu->idx: %d, num: %d, level: %d", 109 - (__entry->type == KVM_ARM_IRQ_TYPE_CPU) ? "CPU" : 110 - (__entry->type == KVM_ARM_IRQ_TYPE_PPI) ? "VGIC PPI" : 111 - (__entry->type == KVM_ARM_IRQ_TYPE_SPI) ? "VGIC SPI" : "UNKNOWN", 112 - __entry->type, __entry->vcpu_idx, __entry->irq_num, __entry->level) 113 - ); 114 - 115 - TRACE_EVENT(kvm_mmio_emulate, 116 - TP_PROTO(unsigned long vcpu_pc, unsigned long instr, 117 - unsigned long cpsr), 118 - TP_ARGS(vcpu_pc, instr, cpsr), 119 - 120 - TP_STRUCT__entry( 121 - __field( unsigned long, vcpu_pc ) 122 - __field( unsigned long, instr ) 123 - __field( unsigned long, cpsr ) 124 - ), 125 - 126 - TP_fast_assign( 127 - __entry->vcpu_pc = vcpu_pc; 128 - __entry->instr = instr; 129 - __entry->cpsr = cpsr; 130 - ), 131 - 132 - TP_printk("Emulate MMIO at: 0x%08lx (instr: %08lx, cpsr: %08lx)", 133 - __entry->vcpu_pc, __entry->instr, __entry->cpsr) 134 - ); 135 - 136 9 /* Architecturally implementation defined CP15 register access */ 137 10 TRACE_EVENT(kvm_emulate_cp15_imp, 138 11 TP_PROTO(unsigned long Op1, unsigned long Rt1, unsigned long CRn, ··· 54 181 __entry->is_wfe ? 'e' : 'i', __entry->vcpu_pc) 55 182 ); 56 183 57 - TRACE_EVENT(kvm_unmap_hva, 58 - TP_PROTO(unsigned long hva), 59 - TP_ARGS(hva), 60 - 61 - TP_STRUCT__entry( 62 - __field( unsigned long, hva ) 63 - ), 64 - 65 - TP_fast_assign( 66 - __entry->hva = hva; 67 - ), 68 - 69 - TP_printk("mmu notifier unmap hva: %#08lx", __entry->hva) 70 - ); 71 - 72 - TRACE_EVENT(kvm_unmap_hva_range, 73 - TP_PROTO(unsigned long start, unsigned long end), 74 - TP_ARGS(start, end), 75 - 76 - TP_STRUCT__entry( 77 - __field( unsigned long, start ) 78 - __field( unsigned long, end ) 79 - ), 80 - 81 - TP_fast_assign( 82 - __entry->start = start; 83 - __entry->end = end; 84 - ), 85 - 86 - TP_printk("mmu notifier unmap range: %#08lx -- %#08lx", 87 - __entry->start, __entry->end) 88 - ); 89 - 90 - TRACE_EVENT(kvm_set_spte_hva, 91 - TP_PROTO(unsigned long hva), 92 - TP_ARGS(hva), 93 - 94 - TP_STRUCT__entry( 95 - __field( unsigned long, hva ) 96 - ), 97 - 98 - TP_fast_assign( 99 - __entry->hva = hva; 100 - ), 101 - 102 - TP_printk("mmu notifier set pte hva: %#08lx", __entry->hva) 103 - ); 104 - 105 - TRACE_EVENT(kvm_age_hva, 106 - TP_PROTO(unsigned long start, unsigned long end), 107 - TP_ARGS(start, end), 108 - 109 - TP_STRUCT__entry( 110 - __field( unsigned long, start ) 111 - __field( unsigned long, end ) 112 - ), 113 - 114 - TP_fast_assign( 115 - __entry->start = start; 116 - __entry->end = end; 117 - ), 118 - 119 - TP_printk("mmu notifier age hva: %#08lx -- %#08lx", 120 - __entry->start, __entry->end) 121 - ); 122 - 123 - TRACE_EVENT(kvm_test_age_hva, 124 - TP_PROTO(unsigned long hva), 125 - TP_ARGS(hva), 126 - 127 - TP_STRUCT__entry( 128 - __field( unsigned long, hva ) 129 - ), 130 - 131 - TP_fast_assign( 132 - __entry->hva = hva; 133 - ), 134 - 135 - TP_printk("mmu notifier test age hva: %#08lx", __entry->hva) 136 - ); 137 - 138 184 TRACE_EVENT(kvm_hvc, 139 185 TP_PROTO(unsigned long vcpu_pc, unsigned long r0, unsigned long imm), 140 186 TP_ARGS(vcpu_pc, r0, imm), ··· 72 280 73 281 TP_printk("HVC at 0x%08lx (r0: 0x%08lx, imm: 0x%lx", 74 282 __entry->vcpu_pc, __entry->r0, __entry->imm) 75 - ); 76 - 77 - TRACE_EVENT(kvm_set_way_flush, 78 - TP_PROTO(unsigned long vcpu_pc, bool cache), 79 - TP_ARGS(vcpu_pc, cache), 80 - 81 - TP_STRUCT__entry( 82 - __field( unsigned long, vcpu_pc ) 83 - __field( bool, cache ) 84 - ), 85 - 86 - TP_fast_assign( 87 - __entry->vcpu_pc = vcpu_pc; 88 - __entry->cache = cache; 89 - ), 90 - 91 - TP_printk("S/W flush at 0x%016lx (cache %s)", 92 - __entry->vcpu_pc, __entry->cache ? "on" : "off") 93 - ); 94 - 95 - TRACE_EVENT(kvm_toggle_cache, 96 - TP_PROTO(unsigned long vcpu_pc, bool was, bool now), 97 - TP_ARGS(vcpu_pc, was, now), 98 - 99 - TP_STRUCT__entry( 100 - __field( unsigned long, vcpu_pc ) 101 - __field( bool, was ) 102 - __field( bool, now ) 103 - ), 104 - 105 - TP_fast_assign( 106 - __entry->vcpu_pc = vcpu_pc; 107 - __entry->was = was; 108 - __entry->now = now; 109 - ), 110 - 111 - TP_printk("VM op at 0x%016lx (cache was %s, now %s)", 112 - __entry->vcpu_pc, __entry->was ? "on" : "off", 113 - __entry->now ? "on" : "off") 114 283 ); 115 284 116 285 #endif /* _TRACE_KVM_H */
+6
arch/arm64/include/asm/kvm_emulate.h
··· 240 240 return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC_TYPE; 241 241 } 242 242 243 + static inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu) 244 + { 245 + u32 esr = kvm_vcpu_get_hsr(vcpu); 246 + return (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT; 247 + } 248 + 243 249 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu) 244 250 { 245 251 return vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
+5 -1
arch/arm64/include/uapi/asm/kvm.h
··· 216 216 #define KVM_DEV_ARM_VGIC_GRP_REDIST_REGS 5 217 217 #define KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS 6 218 218 #define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO 7 219 + #define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8 219 220 #define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10 220 221 #define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ 221 222 (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT) 222 223 #define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK 0x3ff 223 224 #define VGIC_LEVEL_INFO_LINE_LEVEL 0 224 225 225 - #define KVM_DEV_ARM_VGIC_CTRL_INIT 0 226 + #define KVM_DEV_ARM_VGIC_CTRL_INIT 0 227 + #define KVM_DEV_ARM_ITS_SAVE_TABLES 1 228 + #define KVM_DEV_ARM_ITS_RESTORE_TABLES 2 229 + #define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3 226 230 227 231 /* Device Control API on vcpu fd */ 228 232 #define KVM_ARM_VCPU_PMU_V3_CTRL 0
+2 -3
arch/arm64/kvm/Makefile
··· 7 7 CFLAGS_mmu.o := -I. 8 8 9 9 KVM=../../../virt/kvm 10 - ARM=../../../arch/arm/kvm 11 10 12 11 obj-$(CONFIG_KVM_ARM_HOST) += kvm.o 13 12 obj-$(CONFIG_KVM_ARM_HOST) += hyp/ 14 13 15 14 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o $(KVM)/vfio.o 16 - kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/arm.o $(ARM)/mmu.o $(ARM)/mmio.o 17 - kvm-$(CONFIG_KVM_ARM_HOST) += $(ARM)/psci.o $(ARM)/perf.o 15 + kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arm.o $(KVM)/arm/mmu.o $(KVM)/arm/mmio.o 16 + kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/psci.o $(KVM)/arm/perf.o 18 17 19 18 kvm-$(CONFIG_KVM_ARM_HOST) += inject_fault.o regmap.o 20 19 kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
+4 -4
arch/arm64/kvm/sys_regs.c
··· 1529 1529 { 1530 1530 struct sys_reg_params params; 1531 1531 u32 hsr = kvm_vcpu_get_hsr(vcpu); 1532 - int Rt = (hsr >> 5) & 0xf; 1533 - int Rt2 = (hsr >> 10) & 0xf; 1532 + int Rt = kvm_vcpu_sys_get_rt(vcpu); 1533 + int Rt2 = (hsr >> 10) & 0x1f; 1534 1534 1535 1535 params.is_aarch32 = true; 1536 1536 params.is_32bit = false; ··· 1586 1586 { 1587 1587 struct sys_reg_params params; 1588 1588 u32 hsr = kvm_vcpu_get_hsr(vcpu); 1589 - int Rt = (hsr >> 5) & 0xf; 1589 + int Rt = kvm_vcpu_sys_get_rt(vcpu); 1590 1590 1591 1591 params.is_aarch32 = true; 1592 1592 params.is_32bit = true; ··· 1688 1688 { 1689 1689 struct sys_reg_params params; 1690 1690 unsigned long esr = kvm_vcpu_get_hsr(vcpu); 1691 - int Rt = (esr >> 5) & 0x1f; 1691 + int Rt = kvm_vcpu_sys_get_rt(vcpu); 1692 1692 int ret; 1693 1693 1694 1694 trace_kvm_handle_sys_reg(esr);
+4 -1
include/kvm/arm_vgic.h
··· 148 148 gpa_t vgic_its_base; 149 149 150 150 bool enabled; 151 - bool initialized; 152 151 struct vgic_io_device iodev; 153 152 struct kvm_device *dev; 154 153 ··· 160 161 u64 cbaser; 161 162 u32 creadr; 162 163 u32 cwriter; 164 + 165 + /* migration ABI revision in use */ 166 + u32 abi_rev; 163 167 164 168 /* Protects the device and collection lists */ 165 169 struct mutex its_lock; ··· 285 283 286 284 int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write); 287 285 void kvm_vgic_early_init(struct kvm *kvm); 286 + int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu); 288 287 int kvm_vgic_create(struct kvm *kvm, u32 type); 289 288 void kvm_vgic_destroy(struct kvm *kvm); 290 289 void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu);
+14
include/linux/irqchip/arm-gic-v3.h
··· 132 132 #define GIC_BASER_SHAREABILITY(reg, type) \ 133 133 (GIC_BASER_##type << reg##_SHAREABILITY_SHIFT) 134 134 135 + /* encode a size field of width @w containing @n - 1 units */ 136 + #define GIC_ENCODE_SZ(n, w) (((unsigned long)(n) - 1) & GENMASK_ULL(((w) - 1), 0)) 137 + 135 138 #define GICR_PROPBASER_SHAREABILITY_SHIFT (10) 136 139 #define GICR_PROPBASER_INNER_CACHEABILITY_SHIFT (7) 137 140 #define GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT (56) ··· 159 156 #define GICR_PROPBASER_RaWaWb GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb) 160 157 161 158 #define GICR_PROPBASER_IDBITS_MASK (0x1f) 159 + #define GICR_PROPBASER_ADDRESS(x) ((x) & GENMASK_ULL(51, 12)) 160 + #define GICR_PENDBASER_ADDRESS(x) ((x) & GENMASK_ULL(51, 16)) 162 161 163 162 #define GICR_PENDBASER_SHAREABILITY_SHIFT (10) 164 163 #define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT (7) ··· 237 232 #define GITS_CTLR_QUIESCENT (1U << 31) 238 233 239 234 #define GITS_TYPER_PLPIS (1UL << 0) 235 + #define GITS_TYPER_ITT_ENTRY_SIZE_SHIFT 4 240 236 #define GITS_TYPER_IDBITS_SHIFT 8 241 237 #define GITS_TYPER_DEVBITS_SHIFT 13 242 238 #define GITS_TYPER_DEVBITS(r) ((((r) >> GITS_TYPER_DEVBITS_SHIFT) & 0x1f) + 1) 243 239 #define GITS_TYPER_PTA (1UL << 19) 244 240 #define GITS_TYPER_HWCOLLCNT_SHIFT 24 241 + 242 + #define GITS_IIDR_REV_SHIFT 12 243 + #define GITS_IIDR_REV_MASK (0xf << GITS_IIDR_REV_SHIFT) 244 + #define GITS_IIDR_REV(r) (((r) >> GITS_IIDR_REV_SHIFT) & 0xf) 245 + #define GITS_IIDR_PRODUCTID_SHIFT 24 245 246 246 247 #define GITS_CBASER_VALID (1ULL << 63) 247 248 #define GITS_CBASER_SHAREABILITY_SHIFT (10) ··· 301 290 #define GITS_BASER_TYPE(r) (((r) >> GITS_BASER_TYPE_SHIFT) & 7) 302 291 #define GITS_BASER_ENTRY_SIZE_SHIFT (48) 303 292 #define GITS_BASER_ENTRY_SIZE(r) ((((r) >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1) 293 + #define GITS_BASER_ENTRY_SIZE_MASK GENMASK_ULL(52, 48) 304 294 #define GITS_BASER_SHAREABILITY_SHIFT (10) 305 295 #define GITS_BASER_InnerShareable \ 306 296 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) ··· 349 337 #define E_ITS_INT_UNMAPPED_INTERRUPT 0x010307 350 338 #define E_ITS_CLEAR_UNMAPPED_INTERRUPT 0x010507 351 339 #define E_ITS_MAPD_DEVICE_OOR 0x010801 340 + #define E_ITS_MAPD_ITTSIZE_OOR 0x010802 352 341 #define E_ITS_MAPC_PROCNUM_OOR 0x010902 353 342 #define E_ITS_MAPC_COLLECTION_OOR 0x010903 354 343 #define E_ITS_MAPTI_UNMAPPED_DEVICE 0x010a04 344 + #define E_ITS_MAPTI_ID_OOR 0x010a05 355 345 #define E_ITS_MAPTI_PHYSICALID_OOR 0x010a06 356 346 #define E_ITS_INV_UNMAPPED_INTERRUPT 0x010c07 357 347 #define E_ITS_INVALL_UNMAPPED_COLLECTION 0x010d09
+11
include/linux/kvm_host.h
··· 499 499 return NULL; 500 500 } 501 501 502 + static inline int kvm_vcpu_get_idx(struct kvm_vcpu *vcpu) 503 + { 504 + struct kvm_vcpu *tmp; 505 + int idx; 506 + 507 + kvm_for_each_vcpu(idx, tmp, vcpu->kvm) 508 + if (tmp == vcpu) 509 + return idx; 510 + BUG(); 511 + } 512 + 502 513 #define kvm_for_each_memslot(memslot, slots) \ 503 514 for (memslot = &slots->memslots[0]; \ 504 515 memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\
+235 -11
virt/kvm/arm/trace.h
··· 7 7 #define TRACE_SYSTEM kvm 8 8 9 9 /* 10 - * Tracepoints for vgic 10 + * Tracepoints for entry/exit to guest 11 11 */ 12 - TRACE_EVENT(vgic_update_irq_pending, 13 - TP_PROTO(unsigned long vcpu_id, __u32 irq, bool level), 14 - TP_ARGS(vcpu_id, irq, level), 12 + TRACE_EVENT(kvm_entry, 13 + TP_PROTO(unsigned long vcpu_pc), 14 + TP_ARGS(vcpu_pc), 15 15 16 16 TP_STRUCT__entry( 17 - __field( unsigned long, vcpu_id ) 18 - __field( __u32, irq ) 19 - __field( bool, level ) 17 + __field( unsigned long, vcpu_pc ) 20 18 ), 21 19 22 20 TP_fast_assign( 23 - __entry->vcpu_id = vcpu_id; 24 - __entry->irq = irq; 21 + __entry->vcpu_pc = vcpu_pc; 22 + ), 23 + 24 + TP_printk("PC: 0x%08lx", __entry->vcpu_pc) 25 + ); 26 + 27 + TRACE_EVENT(kvm_exit, 28 + TP_PROTO(int idx, unsigned int exit_reason, unsigned long vcpu_pc), 29 + TP_ARGS(idx, exit_reason, vcpu_pc), 30 + 31 + TP_STRUCT__entry( 32 + __field( int, idx ) 33 + __field( unsigned int, exit_reason ) 34 + __field( unsigned long, vcpu_pc ) 35 + ), 36 + 37 + TP_fast_assign( 38 + __entry->idx = idx; 39 + __entry->exit_reason = exit_reason; 40 + __entry->vcpu_pc = vcpu_pc; 41 + ), 42 + 43 + TP_printk("%s: HSR_EC: 0x%04x (%s), PC: 0x%08lx", 44 + __print_symbolic(__entry->idx, kvm_arm_exception_type), 45 + __entry->exit_reason, 46 + __print_symbolic(__entry->exit_reason, kvm_arm_exception_class), 47 + __entry->vcpu_pc) 48 + ); 49 + 50 + TRACE_EVENT(kvm_guest_fault, 51 + TP_PROTO(unsigned long vcpu_pc, unsigned long hsr, 52 + unsigned long hxfar, 53 + unsigned long long ipa), 54 + TP_ARGS(vcpu_pc, hsr, hxfar, ipa), 55 + 56 + TP_STRUCT__entry( 57 + __field( unsigned long, vcpu_pc ) 58 + __field( unsigned long, hsr ) 59 + __field( unsigned long, hxfar ) 60 + __field( unsigned long long, ipa ) 61 + ), 62 + 63 + TP_fast_assign( 64 + __entry->vcpu_pc = vcpu_pc; 65 + __entry->hsr = hsr; 66 + __entry->hxfar = hxfar; 67 + __entry->ipa = ipa; 68 + ), 69 + 70 + TP_printk("ipa %#llx, hsr %#08lx, hxfar %#08lx, pc %#08lx", 71 + __entry->ipa, __entry->hsr, 72 + __entry->hxfar, __entry->vcpu_pc) 73 + ); 74 + 75 + TRACE_EVENT(kvm_access_fault, 76 + TP_PROTO(unsigned long ipa), 77 + TP_ARGS(ipa), 78 + 79 + TP_STRUCT__entry( 80 + __field( unsigned long, ipa ) 81 + ), 82 + 83 + TP_fast_assign( 84 + __entry->ipa = ipa; 85 + ), 86 + 87 + TP_printk("IPA: %lx", __entry->ipa) 88 + ); 89 + 90 + TRACE_EVENT(kvm_irq_line, 91 + TP_PROTO(unsigned int type, int vcpu_idx, int irq_num, int level), 92 + TP_ARGS(type, vcpu_idx, irq_num, level), 93 + 94 + TP_STRUCT__entry( 95 + __field( unsigned int, type ) 96 + __field( int, vcpu_idx ) 97 + __field( int, irq_num ) 98 + __field( int, level ) 99 + ), 100 + 101 + TP_fast_assign( 102 + __entry->type = type; 103 + __entry->vcpu_idx = vcpu_idx; 104 + __entry->irq_num = irq_num; 25 105 __entry->level = level; 26 106 ), 27 107 28 - TP_printk("VCPU: %ld, IRQ %d, level: %d", 29 - __entry->vcpu_id, __entry->irq, __entry->level) 108 + TP_printk("Inject %s interrupt (%d), vcpu->idx: %d, num: %d, level: %d", 109 + (__entry->type == KVM_ARM_IRQ_TYPE_CPU) ? "CPU" : 110 + (__entry->type == KVM_ARM_IRQ_TYPE_PPI) ? "VGIC PPI" : 111 + (__entry->type == KVM_ARM_IRQ_TYPE_SPI) ? "VGIC SPI" : "UNKNOWN", 112 + __entry->type, __entry->vcpu_idx, __entry->irq_num, __entry->level) 113 + ); 114 + 115 + TRACE_EVENT(kvm_mmio_emulate, 116 + TP_PROTO(unsigned long vcpu_pc, unsigned long instr, 117 + unsigned long cpsr), 118 + TP_ARGS(vcpu_pc, instr, cpsr), 119 + 120 + TP_STRUCT__entry( 121 + __field( unsigned long, vcpu_pc ) 122 + __field( unsigned long, instr ) 123 + __field( unsigned long, cpsr ) 124 + ), 125 + 126 + TP_fast_assign( 127 + __entry->vcpu_pc = vcpu_pc; 128 + __entry->instr = instr; 129 + __entry->cpsr = cpsr; 130 + ), 131 + 132 + TP_printk("Emulate MMIO at: 0x%08lx (instr: %08lx, cpsr: %08lx)", 133 + __entry->vcpu_pc, __entry->instr, __entry->cpsr) 134 + ); 135 + 136 + TRACE_EVENT(kvm_unmap_hva, 137 + TP_PROTO(unsigned long hva), 138 + TP_ARGS(hva), 139 + 140 + TP_STRUCT__entry( 141 + __field( unsigned long, hva ) 142 + ), 143 + 144 + TP_fast_assign( 145 + __entry->hva = hva; 146 + ), 147 + 148 + TP_printk("mmu notifier unmap hva: %#08lx", __entry->hva) 149 + ); 150 + 151 + TRACE_EVENT(kvm_unmap_hva_range, 152 + TP_PROTO(unsigned long start, unsigned long end), 153 + TP_ARGS(start, end), 154 + 155 + TP_STRUCT__entry( 156 + __field( unsigned long, start ) 157 + __field( unsigned long, end ) 158 + ), 159 + 160 + TP_fast_assign( 161 + __entry->start = start; 162 + __entry->end = end; 163 + ), 164 + 165 + TP_printk("mmu notifier unmap range: %#08lx -- %#08lx", 166 + __entry->start, __entry->end) 167 + ); 168 + 169 + TRACE_EVENT(kvm_set_spte_hva, 170 + TP_PROTO(unsigned long hva), 171 + TP_ARGS(hva), 172 + 173 + TP_STRUCT__entry( 174 + __field( unsigned long, hva ) 175 + ), 176 + 177 + TP_fast_assign( 178 + __entry->hva = hva; 179 + ), 180 + 181 + TP_printk("mmu notifier set pte hva: %#08lx", __entry->hva) 182 + ); 183 + 184 + TRACE_EVENT(kvm_age_hva, 185 + TP_PROTO(unsigned long start, unsigned long end), 186 + TP_ARGS(start, end), 187 + 188 + TP_STRUCT__entry( 189 + __field( unsigned long, start ) 190 + __field( unsigned long, end ) 191 + ), 192 + 193 + TP_fast_assign( 194 + __entry->start = start; 195 + __entry->end = end; 196 + ), 197 + 198 + TP_printk("mmu notifier age hva: %#08lx -- %#08lx", 199 + __entry->start, __entry->end) 200 + ); 201 + 202 + TRACE_EVENT(kvm_test_age_hva, 203 + TP_PROTO(unsigned long hva), 204 + TP_ARGS(hva), 205 + 206 + TP_STRUCT__entry( 207 + __field( unsigned long, hva ) 208 + ), 209 + 210 + TP_fast_assign( 211 + __entry->hva = hva; 212 + ), 213 + 214 + TP_printk("mmu notifier test age hva: %#08lx", __entry->hva) 215 + ); 216 + 217 + TRACE_EVENT(kvm_set_way_flush, 218 + TP_PROTO(unsigned long vcpu_pc, bool cache), 219 + TP_ARGS(vcpu_pc, cache), 220 + 221 + TP_STRUCT__entry( 222 + __field( unsigned long, vcpu_pc ) 223 + __field( bool, cache ) 224 + ), 225 + 226 + TP_fast_assign( 227 + __entry->vcpu_pc = vcpu_pc; 228 + __entry->cache = cache; 229 + ), 230 + 231 + TP_printk("S/W flush at 0x%016lx (cache %s)", 232 + __entry->vcpu_pc, __entry->cache ? "on" : "off") 233 + ); 234 + 235 + TRACE_EVENT(kvm_toggle_cache, 236 + TP_PROTO(unsigned long vcpu_pc, bool was, bool now), 237 + TP_ARGS(vcpu_pc, was, now), 238 + 239 + TP_STRUCT__entry( 240 + __field( unsigned long, vcpu_pc ) 241 + __field( bool, was ) 242 + __field( bool, now ) 243 + ), 244 + 245 + TP_fast_assign( 246 + __entry->vcpu_pc = vcpu_pc; 247 + __entry->was = was; 248 + __entry->now = now; 249 + ), 250 + 251 + TP_printk("VM op at 0x%016lx (cache was %s, now %s)", 252 + __entry->vcpu_pc, __entry->was ? "on" : "off", 253 + __entry->now ? "on" : "off") 30 254 ); 31 255 32 256 /*
+37
virt/kvm/arm/vgic/trace.h
··· 1 + #if !defined(_TRACE_VGIC_H) || defined(TRACE_HEADER_MULTI_READ) 2 + #define _TRACE_VGIC_H 3 + 4 + #include <linux/tracepoint.h> 5 + 6 + #undef TRACE_SYSTEM 7 + #define TRACE_SYSTEM kvm 8 + 9 + TRACE_EVENT(vgic_update_irq_pending, 10 + TP_PROTO(unsigned long vcpu_id, __u32 irq, bool level), 11 + TP_ARGS(vcpu_id, irq, level), 12 + 13 + TP_STRUCT__entry( 14 + __field( unsigned long, vcpu_id ) 15 + __field( __u32, irq ) 16 + __field( bool, level ) 17 + ), 18 + 19 + TP_fast_assign( 20 + __entry->vcpu_id = vcpu_id; 21 + __entry->irq = irq; 22 + __entry->level = level; 23 + ), 24 + 25 + TP_printk("VCPU: %ld, IRQ %d, level: %d", 26 + __entry->vcpu_id, __entry->irq, __entry->level) 27 + ); 28 + 29 + #endif /* _TRACE_VGIC_H */ 30 + 31 + #undef TRACE_INCLUDE_PATH 32 + #define TRACE_INCLUDE_PATH ../../../virt/kvm/arm/vgic 33 + #undef TRACE_INCLUDE_FILE 34 + #define TRACE_INCLUDE_FILE trace 35 + 36 + /* This part must be outside protection */ 37 + #include <trace/define_trace.h>
+21 -4
virt/kvm/arm/vgic/vgic-init.c
··· 227 227 } 228 228 229 229 /** 230 - * kvm_vgic_vcpu_init() - Enable the VCPU interface 231 - * @vcpu: the VCPU which's VGIC should be enabled 230 + * kvm_vgic_vcpu_init() - Register VCPU-specific KVM iodevs 231 + * @vcpu: pointer to the VCPU being created and initialized 232 232 */ 233 - static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) 233 + int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) 234 + { 235 + int ret = 0; 236 + struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 237 + 238 + if (!irqchip_in_kernel(vcpu->kvm)) 239 + return 0; 240 + 241 + /* 242 + * If we are creating a VCPU with a GICv3 we must also register the 243 + * KVM io device for the redistributor that belongs to this VCPU. 244 + */ 245 + if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) 246 + ret = vgic_register_redist_iodev(vcpu); 247 + return ret; 248 + } 249 + 250 + static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu) 234 251 { 235 252 if (kvm_vgic_global_state.type == VGIC_V2) 236 253 vgic_v2_enable(vcpu); ··· 286 269 dist->msis_require_devid = true; 287 270 288 271 kvm_for_each_vcpu(i, vcpu, kvm) 289 - kvm_vgic_vcpu_init(vcpu); 272 + kvm_vgic_vcpu_enable(vcpu); 290 273 291 274 ret = kvm_vgic_setup_default_irq_routing(kvm); 292 275 if (ret)
+1056 -180
virt/kvm/arm/vgic/vgic-its.c
··· 23 23 #include <linux/interrupt.h> 24 24 #include <linux/list.h> 25 25 #include <linux/uaccess.h> 26 + #include <linux/list_sort.h> 26 27 27 28 #include <linux/irqchip/arm-gic-v3.h> 28 29 ··· 34 33 #include "vgic.h" 35 34 #include "vgic-mmio.h" 36 35 36 + static int vgic_its_save_tables_v0(struct vgic_its *its); 37 + static int vgic_its_restore_tables_v0(struct vgic_its *its); 38 + static int vgic_its_commit_v0(struct vgic_its *its); 39 + static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, 40 + struct kvm_vcpu *filter_vcpu); 41 + 37 42 /* 38 43 * Creates a new (reference to a) struct vgic_irq for a given LPI. 39 44 * If this LPI is already mapped on another ITS, we increase its refcount ··· 47 40 * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. 48 41 * This function returns a pointer to the _unlocked_ structure. 49 42 */ 50 - static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) 43 + static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, 44 + struct kvm_vcpu *vcpu) 51 45 { 52 46 struct vgic_dist *dist = &kvm->arch.vgic; 53 47 struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq; 48 + int ret; 54 49 55 50 /* In this case there is no put, since we keep the reference. */ 56 51 if (irq) ··· 69 60 irq->config = VGIC_CONFIG_EDGE; 70 61 kref_init(&irq->refcount); 71 62 irq->intid = intid; 63 + irq->target_vcpu = vcpu; 72 64 73 65 spin_lock(&dist->lpi_list_lock); 74 66 ··· 101 91 out_unlock: 102 92 spin_unlock(&dist->lpi_list_lock); 103 93 94 + /* 95 + * We "cache" the configuration table entries in our struct vgic_irq's. 96 + * However we only have those structs for mapped IRQs, so we read in 97 + * the respective config data from memory here upon mapping the LPI. 98 + */ 99 + ret = update_lpi_config(kvm, irq, NULL); 100 + if (ret) 101 + return ERR_PTR(ret); 102 + 103 + ret = vgic_v3_lpi_sync_pending_status(kvm, irq); 104 + if (ret) 105 + return ERR_PTR(ret); 106 + 104 107 return irq; 105 108 } 106 109 ··· 122 99 123 100 /* the head for the list of ITTEs */ 124 101 struct list_head itt_head; 102 + u32 num_eventid_bits; 103 + gpa_t itt_addr; 125 104 u32 device_id; 126 105 }; 127 106 ··· 139 114 #define its_is_collection_mapped(coll) ((coll) && \ 140 115 ((coll)->target_addr != COLLECTION_NOT_MAPPED)) 141 116 142 - struct its_itte { 143 - struct list_head itte_list; 117 + struct its_ite { 118 + struct list_head ite_list; 144 119 145 120 struct vgic_irq *irq; 146 121 struct its_collection *collection; 147 122 u32 lpi; 148 123 u32 event_id; 149 124 }; 125 + 126 + /** 127 + * struct vgic_its_abi - ITS abi ops and settings 128 + * @cte_esz: collection table entry size 129 + * @dte_esz: device table entry size 130 + * @ite_esz: interrupt translation table entry size 131 + * @save tables: save the ITS tables into guest RAM 132 + * @restore_tables: restore the ITS internal structs from tables 133 + * stored in guest RAM 134 + * @commit: initialize the registers which expose the ABI settings, 135 + * especially the entry sizes 136 + */ 137 + struct vgic_its_abi { 138 + int cte_esz; 139 + int dte_esz; 140 + int ite_esz; 141 + int (*save_tables)(struct vgic_its *its); 142 + int (*restore_tables)(struct vgic_its *its); 143 + int (*commit)(struct vgic_its *its); 144 + }; 145 + 146 + static const struct vgic_its_abi its_table_abi_versions[] = { 147 + [0] = {.cte_esz = 8, .dte_esz = 8, .ite_esz = 8, 148 + .save_tables = vgic_its_save_tables_v0, 149 + .restore_tables = vgic_its_restore_tables_v0, 150 + .commit = vgic_its_commit_v0, 151 + }, 152 + }; 153 + 154 + #define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions) 155 + 156 + inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its) 157 + { 158 + return &its_table_abi_versions[its->abi_rev]; 159 + } 160 + 161 + int vgic_its_set_abi(struct vgic_its *its, int rev) 162 + { 163 + const struct vgic_its_abi *abi; 164 + 165 + its->abi_rev = rev; 166 + abi = vgic_its_get_abi(its); 167 + return abi->commit(its); 168 + } 150 169 151 170 /* 152 171 * Find and returns a device in the device table for an ITS. ··· 212 143 * Device ID/Event ID pair on an ITS. 213 144 * Must be called with the its_lock mutex held. 214 145 */ 215 - static struct its_itte *find_itte(struct vgic_its *its, u32 device_id, 146 + static struct its_ite *find_ite(struct vgic_its *its, u32 device_id, 216 147 u32 event_id) 217 148 { 218 149 struct its_device *device; 219 - struct its_itte *itte; 150 + struct its_ite *ite; 220 151 221 152 device = find_its_device(its, device_id); 222 153 if (device == NULL) 223 154 return NULL; 224 155 225 - list_for_each_entry(itte, &device->itt_head, itte_list) 226 - if (itte->event_id == event_id) 227 - return itte; 156 + list_for_each_entry(ite, &device->itt_head, ite_list) 157 + if (ite->event_id == event_id) 158 + return ite; 228 159 229 160 return NULL; 230 161 } 231 162 232 163 /* To be used as an iterator this macro misses the enclosing parentheses */ 233 - #define for_each_lpi_its(dev, itte, its) \ 164 + #define for_each_lpi_its(dev, ite, its) \ 234 165 list_for_each_entry(dev, &(its)->device_list, dev_list) \ 235 - list_for_each_entry(itte, &(dev)->itt_head, itte_list) 166 + list_for_each_entry(ite, &(dev)->itt_head, ite_list) 236 167 237 168 /* 238 169 * We only implement 48 bits of PA at the moment, although the ITS ··· 240 171 */ 241 172 #define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16)) 242 173 #define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12)) 243 - #define PENDBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16)) 244 - #define PROPBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12)) 245 174 246 175 #define GIC_LPI_OFFSET 8192 176 + 177 + #define VITS_TYPER_IDBITS 16 178 + #define VITS_TYPER_DEVBITS 16 179 + #define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1) 180 + #define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1) 247 181 248 182 /* 249 183 * Finds and returns a collection in the ITS collection table. ··· 276 204 static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, 277 205 struct kvm_vcpu *filter_vcpu) 278 206 { 279 - u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser); 207 + u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser); 280 208 u8 prop; 281 209 int ret; 282 210 ··· 301 229 } 302 230 303 231 /* 304 - * Create a snapshot of the current LPI list, so that we can enumerate all 305 - * LPIs without holding any lock. 306 - * Returns the array length and puts the kmalloc'ed array into intid_ptr. 232 + * Create a snapshot of the current LPIs targeting @vcpu, so that we can 233 + * enumerate those LPIs without holding any lock. 234 + * Returns their number and puts the kmalloc'ed array into intid_ptr. 307 235 */ 308 - static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr) 236 + static int vgic_copy_lpi_list(struct kvm_vcpu *vcpu, u32 **intid_ptr) 309 237 { 310 - struct vgic_dist *dist = &kvm->arch.vgic; 238 + struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 311 239 struct vgic_irq *irq; 312 240 u32 *intids; 313 241 int irq_count = dist->lpi_list_count, i = 0; ··· 326 254 spin_lock(&dist->lpi_list_lock); 327 255 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { 328 256 /* We don't need to "get" the IRQ, as we hold the list lock. */ 329 - intids[i] = irq->intid; 330 - if (++i == irq_count) 331 - break; 257 + if (irq->target_vcpu != vcpu) 258 + continue; 259 + intids[i++] = irq->intid; 332 260 } 333 261 spin_unlock(&dist->lpi_list_lock); 334 262 335 263 *intid_ptr = intids; 336 - return irq_count; 264 + return i; 337 265 } 338 266 339 267 /* ··· 342 270 * Needs to be called whenever either the collection for a LPIs has 343 271 * changed or the collection itself got retargeted. 344 272 */ 345 - static void update_affinity_itte(struct kvm *kvm, struct its_itte *itte) 273 + static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite) 346 274 { 347 275 struct kvm_vcpu *vcpu; 348 276 349 - if (!its_is_collection_mapped(itte->collection)) 277 + if (!its_is_collection_mapped(ite->collection)) 350 278 return; 351 279 352 - vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr); 280 + vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr); 353 281 354 - spin_lock(&itte->irq->irq_lock); 355 - itte->irq->target_vcpu = vcpu; 356 - spin_unlock(&itte->irq->irq_lock); 282 + spin_lock(&ite->irq->irq_lock); 283 + ite->irq->target_vcpu = vcpu; 284 + spin_unlock(&ite->irq->irq_lock); 357 285 } 358 286 359 287 /* ··· 364 292 struct its_collection *coll) 365 293 { 366 294 struct its_device *device; 367 - struct its_itte *itte; 295 + struct its_ite *ite; 368 296 369 - for_each_lpi_its(device, itte, its) { 370 - if (!itte->collection || coll != itte->collection) 297 + for_each_lpi_its(device, ite, its) { 298 + if (!ite->collection || coll != ite->collection) 371 299 continue; 372 300 373 - update_affinity_itte(kvm, itte); 301 + update_affinity_ite(kvm, ite); 374 302 } 375 303 } 376 304 ··· 382 310 } 383 311 384 312 /* 385 - * Scan the whole LPI pending table and sync the pending bit in there 313 + * Sync the pending table pending bit of LPIs targeting @vcpu 386 314 * with our own data structures. This relies on the LPI being 387 315 * mapped before. 388 316 */ 389 317 static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu) 390 318 { 391 - gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); 319 + gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); 392 320 struct vgic_irq *irq; 393 321 int last_byte_offset = -1; 394 322 int ret = 0; 395 323 u32 *intids; 396 324 int nr_irqs, i; 397 325 398 - nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids); 326 + nr_irqs = vgic_copy_lpi_list(vcpu, &intids); 399 327 if (nr_irqs < 0) 400 328 return nr_irqs; 401 329 ··· 436 364 struct vgic_its *its, 437 365 gpa_t addr, unsigned int len) 438 366 { 367 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 439 368 u64 reg = GITS_TYPER_PLPIS; 440 369 441 370 /* ··· 447 374 * To avoid memory waste in the guest, we keep the number of IDBits and 448 375 * DevBits low - as least for the time being. 449 376 */ 450 - reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT; 451 - reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT; 377 + reg |= GIC_ENCODE_SZ(VITS_TYPER_DEVBITS, 5) << GITS_TYPER_DEVBITS_SHIFT; 378 + reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT; 379 + reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT; 452 380 453 381 return extract_bytes(reg, addr & 7, len); 454 382 } ··· 458 384 struct vgic_its *its, 459 385 gpa_t addr, unsigned int len) 460 386 { 461 - return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0); 387 + u32 val; 388 + 389 + val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK; 390 + val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM; 391 + return val; 392 + } 393 + 394 + static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm, 395 + struct vgic_its *its, 396 + gpa_t addr, unsigned int len, 397 + unsigned long val) 398 + { 399 + u32 rev = GITS_IIDR_REV(val); 400 + 401 + if (rev >= NR_ITS_ABIS) 402 + return -EINVAL; 403 + return vgic_its_set_abi(its, rev); 462 404 } 463 405 464 406 static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm, ··· 515 425 u32 devid, u32 eventid) 516 426 { 517 427 struct kvm_vcpu *vcpu; 518 - struct its_itte *itte; 428 + struct its_ite *ite; 519 429 520 430 if (!its->enabled) 521 431 return -EBUSY; 522 432 523 - itte = find_itte(its, devid, eventid); 524 - if (!itte || !its_is_collection_mapped(itte->collection)) 433 + ite = find_ite(its, devid, eventid); 434 + if (!ite || !its_is_collection_mapped(ite->collection)) 525 435 return E_ITS_INT_UNMAPPED_INTERRUPT; 526 436 527 - vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr); 437 + vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr); 528 438 if (!vcpu) 529 439 return E_ITS_INT_UNMAPPED_INTERRUPT; 530 440 531 441 if (!vcpu->arch.vgic_cpu.lpis_enabled) 532 442 return -EBUSY; 533 443 534 - spin_lock(&itte->irq->irq_lock); 535 - itte->irq->pending_latch = true; 536 - vgic_queue_irq_unlock(kvm, itte->irq); 444 + spin_lock(&ite->irq->irq_lock); 445 + ite->irq->pending_latch = true; 446 + vgic_queue_irq_unlock(kvm, ite->irq); 537 447 538 448 return 0; 539 449 } ··· 601 511 } 602 512 603 513 /* Requires the its_lock to be held. */ 604 - static void its_free_itte(struct kvm *kvm, struct its_itte *itte) 514 + static void its_free_ite(struct kvm *kvm, struct its_ite *ite) 605 515 { 606 - list_del(&itte->itte_list); 516 + list_del(&ite->ite_list); 607 517 608 518 /* This put matches the get in vgic_add_lpi. */ 609 - if (itte->irq) 610 - vgic_put_irq(kvm, itte->irq); 519 + if (ite->irq) 520 + vgic_put_irq(kvm, ite->irq); 611 521 612 - kfree(itte); 522 + kfree(ite); 613 523 } 614 524 615 525 static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size) ··· 619 529 620 530 #define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8) 621 531 #define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32) 532 + #define its_cmd_get_size(cmd) (its_cmd_mask_field(cmd, 1, 0, 5) + 1) 622 533 #define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32) 623 534 #define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32) 624 535 #define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16) 536 + #define its_cmd_get_ittaddr(cmd) (its_cmd_mask_field(cmd, 2, 8, 44) << 8) 625 537 #define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32) 626 538 #define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1) 627 539 ··· 636 544 { 637 545 u32 device_id = its_cmd_get_deviceid(its_cmd); 638 546 u32 event_id = its_cmd_get_id(its_cmd); 639 - struct its_itte *itte; 547 + struct its_ite *ite; 640 548 641 549 642 - itte = find_itte(its, device_id, event_id); 643 - if (itte && itte->collection) { 550 + ite = find_ite(its, device_id, event_id); 551 + if (ite && ite->collection) { 644 552 /* 645 553 * Though the spec talks about removing the pending state, we 646 554 * don't bother here since we clear the ITTE anyway and the 647 555 * pending state is a property of the ITTE struct. 648 556 */ 649 - its_free_itte(kvm, itte); 557 + its_free_ite(kvm, ite); 650 558 return 0; 651 559 } 652 560 ··· 664 572 u32 event_id = its_cmd_get_id(its_cmd); 665 573 u32 coll_id = its_cmd_get_collection(its_cmd); 666 574 struct kvm_vcpu *vcpu; 667 - struct its_itte *itte; 575 + struct its_ite *ite; 668 576 struct its_collection *collection; 669 577 670 - itte = find_itte(its, device_id, event_id); 671 - if (!itte) 578 + ite = find_ite(its, device_id, event_id); 579 + if (!ite) 672 580 return E_ITS_MOVI_UNMAPPED_INTERRUPT; 673 581 674 - if (!its_is_collection_mapped(itte->collection)) 582 + if (!its_is_collection_mapped(ite->collection)) 675 583 return E_ITS_MOVI_UNMAPPED_COLLECTION; 676 584 677 585 collection = find_collection(its, coll_id); 678 586 if (!its_is_collection_mapped(collection)) 679 587 return E_ITS_MOVI_UNMAPPED_COLLECTION; 680 588 681 - itte->collection = collection; 589 + ite->collection = collection; 682 590 vcpu = kvm_get_vcpu(kvm, collection->target_addr); 683 591 684 - spin_lock(&itte->irq->irq_lock); 685 - itte->irq->target_vcpu = vcpu; 686 - spin_unlock(&itte->irq->irq_lock); 592 + spin_lock(&ite->irq->irq_lock); 593 + ite->irq->target_vcpu = vcpu; 594 + spin_unlock(&ite->irq->irq_lock); 687 595 688 596 return 0; 689 597 } ··· 692 600 * Check whether an ID can be stored into the corresponding guest table. 693 601 * For a direct table this is pretty easy, but gets a bit nasty for 694 602 * indirect tables. We check whether the resulting guest physical address 695 - * is actually valid (covered by a memslot and guest accessbible). 603 + * is actually valid (covered by a memslot and guest accessible). 696 604 * For this we have to read the respective first level entry. 697 605 */ 698 - static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id) 606 + static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id, 607 + gpa_t *eaddr) 699 608 { 700 609 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; 701 - int index; 702 - u64 indirect_ptr; 703 - gfn_t gfn; 610 + u64 indirect_ptr, type = GITS_BASER_TYPE(baser); 704 611 int esz = GITS_BASER_ENTRY_SIZE(baser); 612 + int index; 613 + gfn_t gfn; 614 + 615 + switch (type) { 616 + case GITS_BASER_TYPE_DEVICE: 617 + if (id >= BIT_ULL(VITS_TYPER_DEVBITS)) 618 + return false; 619 + break; 620 + case GITS_BASER_TYPE_COLLECTION: 621 + /* as GITS_TYPER.CIL == 0, ITS supports 16-bit collection ID */ 622 + if (id >= BIT_ULL(16)) 623 + return false; 624 + break; 625 + default: 626 + return false; 627 + } 705 628 706 629 if (!(baser & GITS_BASER_INDIRECT)) { 707 630 phys_addr_t addr; ··· 727 620 addr = BASER_ADDRESS(baser) + id * esz; 728 621 gfn = addr >> PAGE_SHIFT; 729 622 623 + if (eaddr) 624 + *eaddr = addr; 730 625 return kvm_is_visible_gfn(its->dev->kvm, gfn); 731 626 } 732 627 ··· 761 652 indirect_ptr += index * esz; 762 653 gfn = indirect_ptr >> PAGE_SHIFT; 763 654 655 + if (eaddr) 656 + *eaddr = indirect_ptr; 764 657 return kvm_is_visible_gfn(its->dev->kvm, gfn); 765 658 } 766 659 ··· 772 661 { 773 662 struct its_collection *collection; 774 663 775 - if (!vgic_its_check_id(its, its->baser_coll_table, coll_id)) 664 + if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL)) 776 665 return E_ITS_MAPC_COLLECTION_OOR; 777 666 778 667 collection = kzalloc(sizeof(*collection), GFP_KERNEL); ··· 790 679 { 791 680 struct its_collection *collection; 792 681 struct its_device *device; 793 - struct its_itte *itte; 682 + struct its_ite *ite; 794 683 795 684 /* 796 685 * Clearing the mapping for that collection ID removes the ··· 801 690 if (!collection) 802 691 return; 803 692 804 - for_each_lpi_its(device, itte, its) 805 - if (itte->collection && 806 - itte->collection->collection_id == coll_id) 807 - itte->collection = NULL; 693 + for_each_lpi_its(device, ite, its) 694 + if (ite->collection && 695 + ite->collection->collection_id == coll_id) 696 + ite->collection = NULL; 808 697 809 698 list_del(&collection->coll_list); 810 699 kfree(collection); 700 + } 701 + 702 + /* Must be called with its_lock mutex held */ 703 + static struct its_ite *vgic_its_alloc_ite(struct its_device *device, 704 + struct its_collection *collection, 705 + u32 lpi_id, u32 event_id) 706 + { 707 + struct its_ite *ite; 708 + 709 + ite = kzalloc(sizeof(*ite), GFP_KERNEL); 710 + if (!ite) 711 + return ERR_PTR(-ENOMEM); 712 + 713 + ite->event_id = event_id; 714 + ite->collection = collection; 715 + ite->lpi = lpi_id; 716 + 717 + list_add_tail(&ite->ite_list, &device->itt_head); 718 + return ite; 811 719 } 812 720 813 721 /* ··· 839 709 u32 device_id = its_cmd_get_deviceid(its_cmd); 840 710 u32 event_id = its_cmd_get_id(its_cmd); 841 711 u32 coll_id = its_cmd_get_collection(its_cmd); 842 - struct its_itte *itte; 712 + struct its_ite *ite; 713 + struct kvm_vcpu *vcpu = NULL; 843 714 struct its_device *device; 844 715 struct its_collection *collection, *new_coll = NULL; 845 - int lpi_nr; 846 716 struct vgic_irq *irq; 717 + int lpi_nr; 847 718 848 719 device = find_its_device(its, device_id); 849 720 if (!device) 850 721 return E_ITS_MAPTI_UNMAPPED_DEVICE; 722 + 723 + if (event_id >= BIT_ULL(device->num_eventid_bits)) 724 + return E_ITS_MAPTI_ID_OOR; 851 725 852 726 if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI) 853 727 lpi_nr = its_cmd_get_physical_id(its_cmd); ··· 862 728 return E_ITS_MAPTI_PHYSICALID_OOR; 863 729 864 730 /* If there is an existing mapping, behavior is UNPREDICTABLE. */ 865 - if (find_itte(its, device_id, event_id)) 731 + if (find_ite(its, device_id, event_id)) 866 732 return 0; 867 733 868 734 collection = find_collection(its, coll_id); ··· 873 739 new_coll = collection; 874 740 } 875 741 876 - itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL); 877 - if (!itte) { 742 + ite = vgic_its_alloc_ite(device, collection, lpi_nr, event_id); 743 + if (IS_ERR(ite)) { 878 744 if (new_coll) 879 745 vgic_its_free_collection(its, coll_id); 880 - return -ENOMEM; 746 + return PTR_ERR(ite); 881 747 } 882 748 883 - itte->event_id = event_id; 884 - list_add_tail(&itte->itte_list, &device->itt_head); 749 + if (its_is_collection_mapped(collection)) 750 + vcpu = kvm_get_vcpu(kvm, collection->target_addr); 885 751 886 - itte->collection = collection; 887 - itte->lpi = lpi_nr; 888 - 889 - irq = vgic_add_lpi(kvm, lpi_nr); 752 + irq = vgic_add_lpi(kvm, lpi_nr, vcpu); 890 753 if (IS_ERR(irq)) { 891 754 if (new_coll) 892 755 vgic_its_free_collection(its, coll_id); 893 - its_free_itte(kvm, itte); 756 + its_free_ite(kvm, ite); 894 757 return PTR_ERR(irq); 895 758 } 896 - itte->irq = irq; 897 - 898 - update_affinity_itte(kvm, itte); 899 - 900 - /* 901 - * We "cache" the configuration table entries in out struct vgic_irq's. 902 - * However we only have those structs for mapped IRQs, so we read in 903 - * the respective config data from memory here upon mapping the LPI. 904 - */ 905 - update_lpi_config(kvm, itte->irq, NULL); 759 + ite->irq = irq; 906 760 907 761 return 0; 908 762 } ··· 898 776 /* Requires the its_lock to be held. */ 899 777 static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device) 900 778 { 901 - struct its_itte *itte, *temp; 779 + struct its_ite *ite, *temp; 902 780 903 781 /* 904 782 * The spec says that unmapping a device with still valid 905 783 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs, 906 784 * since we cannot leave the memory unreferenced. 907 785 */ 908 - list_for_each_entry_safe(itte, temp, &device->itt_head, itte_list) 909 - its_free_itte(kvm, itte); 786 + list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list) 787 + its_free_ite(kvm, ite); 910 788 911 789 list_del(&device->dev_list); 912 790 kfree(device); 791 + } 792 + 793 + /* Must be called with its_lock mutex held */ 794 + static struct its_device *vgic_its_alloc_device(struct vgic_its *its, 795 + u32 device_id, gpa_t itt_addr, 796 + u8 num_eventid_bits) 797 + { 798 + struct its_device *device; 799 + 800 + device = kzalloc(sizeof(*device), GFP_KERNEL); 801 + if (!device) 802 + return ERR_PTR(-ENOMEM); 803 + 804 + device->device_id = device_id; 805 + device->itt_addr = itt_addr; 806 + device->num_eventid_bits = num_eventid_bits; 807 + INIT_LIST_HEAD(&device->itt_head); 808 + 809 + list_add_tail(&device->dev_list, &its->device_list); 810 + return device; 913 811 } 914 812 915 813 /* ··· 941 799 { 942 800 u32 device_id = its_cmd_get_deviceid(its_cmd); 943 801 bool valid = its_cmd_get_validbit(its_cmd); 802 + u8 num_eventid_bits = its_cmd_get_size(its_cmd); 803 + gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd); 944 804 struct its_device *device; 945 805 946 - if (!vgic_its_check_id(its, its->baser_device_table, device_id)) 806 + if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL)) 947 807 return E_ITS_MAPD_DEVICE_OOR; 808 + 809 + if (valid && num_eventid_bits > VITS_TYPER_IDBITS) 810 + return E_ITS_MAPD_ITTSIZE_OOR; 948 811 949 812 device = find_its_device(its, device_id); 950 813 ··· 968 821 if (!valid) 969 822 return 0; 970 823 971 - device = kzalloc(sizeof(struct its_device), GFP_KERNEL); 972 - if (!device) 973 - return -ENOMEM; 974 - 975 - device->device_id = device_id; 976 - INIT_LIST_HEAD(&device->itt_head); 977 - 978 - list_add_tail(&device->dev_list, &its->device_list); 824 + device = vgic_its_alloc_device(its, device_id, itt_addr, 825 + num_eventid_bits); 826 + if (IS_ERR(device)) 827 + return PTR_ERR(device); 979 828 980 829 return 0; 981 830 } ··· 1026 883 { 1027 884 u32 device_id = its_cmd_get_deviceid(its_cmd); 1028 885 u32 event_id = its_cmd_get_id(its_cmd); 1029 - struct its_itte *itte; 886 + struct its_ite *ite; 1030 887 1031 888 1032 - itte = find_itte(its, device_id, event_id); 1033 - if (!itte) 889 + ite = find_ite(its, device_id, event_id); 890 + if (!ite) 1034 891 return E_ITS_CLEAR_UNMAPPED_INTERRUPT; 1035 892 1036 - itte->irq->pending_latch = false; 893 + ite->irq->pending_latch = false; 1037 894 1038 895 return 0; 1039 896 } ··· 1047 904 { 1048 905 u32 device_id = its_cmd_get_deviceid(its_cmd); 1049 906 u32 event_id = its_cmd_get_id(its_cmd); 1050 - struct its_itte *itte; 907 + struct its_ite *ite; 1051 908 1052 909 1053 - itte = find_itte(its, device_id, event_id); 1054 - if (!itte) 910 + ite = find_ite(its, device_id, event_id); 911 + if (!ite) 1055 912 return E_ITS_INV_UNMAPPED_INTERRUPT; 1056 913 1057 - return update_lpi_config(kvm, itte->irq, NULL); 914 + return update_lpi_config(kvm, ite->irq, NULL); 1058 915 } 1059 916 1060 917 /* ··· 1081 938 1082 939 vcpu = kvm_get_vcpu(kvm, collection->target_addr); 1083 940 1084 - irq_count = vgic_copy_lpi_list(kvm, &intids); 941 + irq_count = vgic_copy_lpi_list(vcpu, &intids); 1085 942 if (irq_count < 0) 1086 943 return irq_count; 1087 944 ··· 1356 1213 return extract_bytes(its->creadr, addr & 0x7, len); 1357 1214 } 1358 1215 1216 + static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm, 1217 + struct vgic_its *its, 1218 + gpa_t addr, unsigned int len, 1219 + unsigned long val) 1220 + { 1221 + u32 cmd_offset; 1222 + int ret = 0; 1223 + 1224 + mutex_lock(&its->cmd_lock); 1225 + 1226 + if (its->enabled) { 1227 + ret = -EBUSY; 1228 + goto out; 1229 + } 1230 + 1231 + cmd_offset = ITS_CMD_OFFSET(val); 1232 + if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) { 1233 + ret = -EINVAL; 1234 + goto out; 1235 + } 1236 + 1237 + its->creadr = cmd_offset; 1238 + out: 1239 + mutex_unlock(&its->cmd_lock); 1240 + return ret; 1241 + } 1242 + 1359 1243 #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7) 1360 1244 static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, 1361 1245 struct vgic_its *its, ··· 1411 1241 gpa_t addr, unsigned int len, 1412 1242 unsigned long val) 1413 1243 { 1244 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 1414 1245 u64 entry_size, device_type; 1415 1246 u64 reg, *regptr, clearbits = 0; 1416 1247 ··· 1422 1251 switch (BASER_INDEX(addr)) { 1423 1252 case 0: 1424 1253 regptr = &its->baser_device_table; 1425 - entry_size = 8; 1254 + entry_size = abi->dte_esz; 1426 1255 device_type = GITS_BASER_TYPE_DEVICE; 1427 1256 break; 1428 1257 case 1: 1429 1258 regptr = &its->baser_coll_table; 1430 - entry_size = 8; 1259 + entry_size = abi->cte_esz; 1431 1260 device_type = GITS_BASER_TYPE_COLLECTION; 1432 1261 clearbits = GITS_BASER_INDIRECT; 1433 1262 break; ··· 1488 1317 .its_write = wr, \ 1489 1318 } 1490 1319 1320 + #define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\ 1321 + { \ 1322 + .reg_offset = off, \ 1323 + .len = length, \ 1324 + .access_flags = acc, \ 1325 + .its_read = rd, \ 1326 + .its_write = wr, \ 1327 + .uaccess_its_write = uwr, \ 1328 + } 1329 + 1491 1330 static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its, 1492 1331 gpa_t addr, unsigned int len, unsigned long val) 1493 1332 { ··· 1508 1327 REGISTER_ITS_DESC(GITS_CTLR, 1509 1328 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4, 1510 1329 VGIC_ACCESS_32bit), 1511 - REGISTER_ITS_DESC(GITS_IIDR, 1512 - vgic_mmio_read_its_iidr, its_mmio_write_wi, 4, 1330 + REGISTER_ITS_DESC_UACCESS(GITS_IIDR, 1331 + vgic_mmio_read_its_iidr, its_mmio_write_wi, 1332 + vgic_mmio_uaccess_write_its_iidr, 4, 1513 1333 VGIC_ACCESS_32bit), 1514 1334 REGISTER_ITS_DESC(GITS_TYPER, 1515 1335 vgic_mmio_read_its_typer, its_mmio_write_wi, 8, ··· 1521 1339 REGISTER_ITS_DESC(GITS_CWRITER, 1522 1340 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8, 1523 1341 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 1524 - REGISTER_ITS_DESC(GITS_CREADR, 1525 - vgic_mmio_read_its_creadr, its_mmio_write_wi, 8, 1342 + REGISTER_ITS_DESC_UACCESS(GITS_CREADR, 1343 + vgic_mmio_read_its_creadr, its_mmio_write_wi, 1344 + vgic_mmio_uaccess_write_its_creadr, 8, 1526 1345 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 1527 1346 REGISTER_ITS_DESC(GITS_BASER, 1528 1347 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40, ··· 1540 1357 its_sync_lpi_pending_table(vcpu); 1541 1358 } 1542 1359 1543 - static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its) 1360 + static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its, 1361 + u64 addr) 1544 1362 { 1545 1363 struct vgic_io_device *iodev = &its->iodev; 1546 1364 int ret; 1547 1365 1548 - if (!its->initialized) 1549 - return -EBUSY; 1366 + mutex_lock(&kvm->slots_lock); 1367 + if (!IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) { 1368 + ret = -EBUSY; 1369 + goto out; 1370 + } 1550 1371 1551 - if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) 1552 - return -ENXIO; 1553 - 1372 + its->vgic_its_base = addr; 1554 1373 iodev->regions = its_registers; 1555 1374 iodev->nr_regions = ARRAY_SIZE(its_registers); 1556 1375 kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops); ··· 1560 1375 iodev->base_addr = its->vgic_its_base; 1561 1376 iodev->iodev_type = IODEV_ITS; 1562 1377 iodev->its = its; 1563 - mutex_lock(&kvm->slots_lock); 1564 1378 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr, 1565 1379 KVM_VGIC_V3_ITS_SIZE, &iodev->dev); 1380 + out: 1566 1381 mutex_unlock(&kvm->slots_lock); 1567 1382 1568 1383 return ret; ··· 1572 1387 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \ 1573 1388 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \ 1574 1389 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \ 1575 - ((8ULL - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | \ 1576 1390 GITS_BASER_PAGE_SIZE_64K) 1577 1391 1578 1392 #define INITIAL_PROPBASER_VALUE \ ··· 1599 1415 INIT_LIST_HEAD(&its->collection_list); 1600 1416 1601 1417 dev->kvm->arch.vgic.has_its = true; 1602 - its->initialized = false; 1603 1418 its->enabled = false; 1604 1419 its->dev = dev; 1605 1420 ··· 1610 1427 1611 1428 dev->private = its; 1612 1429 1613 - return 0; 1430 + return vgic_its_set_abi(its, NR_ITS_ABIS - 1); 1431 + } 1432 + 1433 + static void vgic_its_free_device(struct kvm *kvm, struct its_device *dev) 1434 + { 1435 + struct its_ite *ite, *tmp; 1436 + 1437 + list_for_each_entry_safe(ite, tmp, &dev->itt_head, ite_list) 1438 + its_free_ite(kvm, ite); 1439 + list_del(&dev->dev_list); 1440 + kfree(dev); 1614 1441 } 1615 1442 1616 1443 static void vgic_its_destroy(struct kvm_device *kvm_dev) 1617 1444 { 1618 1445 struct kvm *kvm = kvm_dev->kvm; 1619 1446 struct vgic_its *its = kvm_dev->private; 1620 - struct its_device *dev; 1621 - struct its_itte *itte; 1622 - struct list_head *dev_cur, *dev_temp; 1623 1447 struct list_head *cur, *temp; 1624 1448 1625 1449 /* ··· 1637 1447 return; 1638 1448 1639 1449 mutex_lock(&its->its_lock); 1640 - list_for_each_safe(dev_cur, dev_temp, &its->device_list) { 1641 - dev = container_of(dev_cur, struct its_device, dev_list); 1642 - list_for_each_safe(cur, temp, &dev->itt_head) { 1643 - itte = (container_of(cur, struct its_itte, itte_list)); 1644 - its_free_itte(kvm, itte); 1645 - } 1646 - list_del(dev_cur); 1647 - kfree(dev); 1450 + list_for_each_safe(cur, temp, &its->device_list) { 1451 + struct its_device *dev; 1452 + 1453 + dev = list_entry(cur, struct its_device, dev_list); 1454 + vgic_its_free_device(kvm, dev); 1648 1455 } 1649 1456 1650 1457 list_for_each_safe(cur, temp, &its->collection_list) { 1458 + struct its_collection *coll; 1459 + 1460 + coll = list_entry(cur, struct its_collection, coll_list); 1651 1461 list_del(cur); 1652 - kfree(container_of(cur, struct its_collection, coll_list)); 1462 + kfree(coll); 1653 1463 } 1654 1464 mutex_unlock(&its->its_lock); 1655 1465 1656 1466 kfree(its); 1467 + } 1468 + 1469 + int vgic_its_has_attr_regs(struct kvm_device *dev, 1470 + struct kvm_device_attr *attr) 1471 + { 1472 + const struct vgic_register_region *region; 1473 + gpa_t offset = attr->attr; 1474 + int align; 1475 + 1476 + align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7; 1477 + 1478 + if (offset & align) 1479 + return -EINVAL; 1480 + 1481 + region = vgic_find_mmio_region(its_registers, 1482 + ARRAY_SIZE(its_registers), 1483 + offset); 1484 + if (!region) 1485 + return -ENXIO; 1486 + 1487 + return 0; 1488 + } 1489 + 1490 + int vgic_its_attr_regs_access(struct kvm_device *dev, 1491 + struct kvm_device_attr *attr, 1492 + u64 *reg, bool is_write) 1493 + { 1494 + const struct vgic_register_region *region; 1495 + struct vgic_its *its; 1496 + gpa_t addr, offset; 1497 + unsigned int len; 1498 + int align, ret = 0; 1499 + 1500 + its = dev->private; 1501 + offset = attr->attr; 1502 + 1503 + /* 1504 + * Although the spec supports upper/lower 32-bit accesses to 1505 + * 64-bit ITS registers, the userspace ABI requires 64-bit 1506 + * accesses to all 64-bit wide registers. We therefore only 1507 + * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID 1508 + * registers 1509 + */ 1510 + if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4)) 1511 + align = 0x3; 1512 + else 1513 + align = 0x7; 1514 + 1515 + if (offset & align) 1516 + return -EINVAL; 1517 + 1518 + mutex_lock(&dev->kvm->lock); 1519 + 1520 + if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) { 1521 + ret = -ENXIO; 1522 + goto out; 1523 + } 1524 + 1525 + region = vgic_find_mmio_region(its_registers, 1526 + ARRAY_SIZE(its_registers), 1527 + offset); 1528 + if (!region) { 1529 + ret = -ENXIO; 1530 + goto out; 1531 + } 1532 + 1533 + if (!lock_all_vcpus(dev->kvm)) { 1534 + ret = -EBUSY; 1535 + goto out; 1536 + } 1537 + 1538 + addr = its->vgic_its_base + offset; 1539 + 1540 + len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4; 1541 + 1542 + if (is_write) { 1543 + if (region->uaccess_its_write) 1544 + ret = region->uaccess_its_write(dev->kvm, its, addr, 1545 + len, *reg); 1546 + else 1547 + region->its_write(dev->kvm, its, addr, len, *reg); 1548 + } else { 1549 + *reg = region->its_read(dev->kvm, its, addr, len); 1550 + } 1551 + unlock_all_vcpus(dev->kvm); 1552 + out: 1553 + mutex_unlock(&dev->kvm->lock); 1554 + return ret; 1555 + } 1556 + 1557 + static u32 compute_next_devid_offset(struct list_head *h, 1558 + struct its_device *dev) 1559 + { 1560 + struct its_device *next; 1561 + u32 next_offset; 1562 + 1563 + if (list_is_last(&dev->dev_list, h)) 1564 + return 0; 1565 + next = list_next_entry(dev, dev_list); 1566 + next_offset = next->device_id - dev->device_id; 1567 + 1568 + return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET); 1569 + } 1570 + 1571 + static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite) 1572 + { 1573 + struct its_ite *next; 1574 + u32 next_offset; 1575 + 1576 + if (list_is_last(&ite->ite_list, h)) 1577 + return 0; 1578 + next = list_next_entry(ite, ite_list); 1579 + next_offset = next->event_id - ite->event_id; 1580 + 1581 + return min_t(u32, next_offset, VITS_ITE_MAX_EVENTID_OFFSET); 1582 + } 1583 + 1584 + /** 1585 + * entry_fn_t - Callback called on a table entry restore path 1586 + * @its: its handle 1587 + * @id: id of the entry 1588 + * @entry: pointer to the entry 1589 + * @opaque: pointer to an opaque data 1590 + * 1591 + * Return: < 0 on error, 0 if last element was identified, id offset to next 1592 + * element otherwise 1593 + */ 1594 + typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry, 1595 + void *opaque); 1596 + 1597 + /** 1598 + * scan_its_table - Scan a contiguous table in guest RAM and applies a function 1599 + * to each entry 1600 + * 1601 + * @its: its handle 1602 + * @base: base gpa of the table 1603 + * @size: size of the table in bytes 1604 + * @esz: entry size in bytes 1605 + * @start_id: the ID of the first entry in the table 1606 + * (non zero for 2d level tables) 1607 + * @fn: function to apply on each entry 1608 + * 1609 + * Return: < 0 on error, 0 if last element was identified, 1 otherwise 1610 + * (the last element may not be found on second level tables) 1611 + */ 1612 + static int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz, 1613 + int start_id, entry_fn_t fn, void *opaque) 1614 + { 1615 + void *entry = kzalloc(esz, GFP_KERNEL); 1616 + struct kvm *kvm = its->dev->kvm; 1617 + unsigned long len = size; 1618 + int id = start_id; 1619 + gpa_t gpa = base; 1620 + int ret; 1621 + 1622 + while (len > 0) { 1623 + int next_offset; 1624 + size_t byte_offset; 1625 + 1626 + ret = kvm_read_guest(kvm, gpa, entry, esz); 1627 + if (ret) 1628 + goto out; 1629 + 1630 + next_offset = fn(its, id, entry, opaque); 1631 + if (next_offset <= 0) { 1632 + ret = next_offset; 1633 + goto out; 1634 + } 1635 + 1636 + byte_offset = next_offset * esz; 1637 + id += next_offset; 1638 + gpa += byte_offset; 1639 + len -= byte_offset; 1640 + } 1641 + ret = 1; 1642 + 1643 + out: 1644 + kfree(entry); 1645 + return ret; 1646 + } 1647 + 1648 + /** 1649 + * vgic_its_save_ite - Save an interrupt translation entry at @gpa 1650 + */ 1651 + static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev, 1652 + struct its_ite *ite, gpa_t gpa, int ite_esz) 1653 + { 1654 + struct kvm *kvm = its->dev->kvm; 1655 + u32 next_offset; 1656 + u64 val; 1657 + 1658 + next_offset = compute_next_eventid_offset(&dev->itt_head, ite); 1659 + val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) | 1660 + ((u64)ite->lpi << KVM_ITS_ITE_PINTID_SHIFT) | 1661 + ite->collection->collection_id; 1662 + val = cpu_to_le64(val); 1663 + return kvm_write_guest(kvm, gpa, &val, ite_esz); 1664 + } 1665 + 1666 + /** 1667 + * vgic_its_restore_ite - restore an interrupt translation entry 1668 + * @event_id: id used for indexing 1669 + * @ptr: pointer to the ITE entry 1670 + * @opaque: pointer to the its_device 1671 + */ 1672 + static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id, 1673 + void *ptr, void *opaque) 1674 + { 1675 + struct its_device *dev = (struct its_device *)opaque; 1676 + struct its_collection *collection; 1677 + struct kvm *kvm = its->dev->kvm; 1678 + struct kvm_vcpu *vcpu = NULL; 1679 + u64 val; 1680 + u64 *p = (u64 *)ptr; 1681 + struct vgic_irq *irq; 1682 + u32 coll_id, lpi_id; 1683 + struct its_ite *ite; 1684 + u32 offset; 1685 + 1686 + val = *p; 1687 + 1688 + val = le64_to_cpu(val); 1689 + 1690 + coll_id = val & KVM_ITS_ITE_ICID_MASK; 1691 + lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT; 1692 + 1693 + if (!lpi_id) 1694 + return 1; /* invalid entry, no choice but to scan next entry */ 1695 + 1696 + if (lpi_id < VGIC_MIN_LPI) 1697 + return -EINVAL; 1698 + 1699 + offset = val >> KVM_ITS_ITE_NEXT_SHIFT; 1700 + if (event_id + offset >= BIT_ULL(dev->num_eventid_bits)) 1701 + return -EINVAL; 1702 + 1703 + collection = find_collection(its, coll_id); 1704 + if (!collection) 1705 + return -EINVAL; 1706 + 1707 + ite = vgic_its_alloc_ite(dev, collection, lpi_id, event_id); 1708 + if (IS_ERR(ite)) 1709 + return PTR_ERR(ite); 1710 + 1711 + if (its_is_collection_mapped(collection)) 1712 + vcpu = kvm_get_vcpu(kvm, collection->target_addr); 1713 + 1714 + irq = vgic_add_lpi(kvm, lpi_id, vcpu); 1715 + if (IS_ERR(irq)) 1716 + return PTR_ERR(irq); 1717 + ite->irq = irq; 1718 + 1719 + return offset; 1720 + } 1721 + 1722 + static int vgic_its_ite_cmp(void *priv, struct list_head *a, 1723 + struct list_head *b) 1724 + { 1725 + struct its_ite *itea = container_of(a, struct its_ite, ite_list); 1726 + struct its_ite *iteb = container_of(b, struct its_ite, ite_list); 1727 + 1728 + if (itea->event_id < iteb->event_id) 1729 + return -1; 1730 + else 1731 + return 1; 1732 + } 1733 + 1734 + static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device) 1735 + { 1736 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 1737 + gpa_t base = device->itt_addr; 1738 + struct its_ite *ite; 1739 + int ret; 1740 + int ite_esz = abi->ite_esz; 1741 + 1742 + list_sort(NULL, &device->itt_head, vgic_its_ite_cmp); 1743 + 1744 + list_for_each_entry(ite, &device->itt_head, ite_list) { 1745 + gpa_t gpa = base + ite->event_id * ite_esz; 1746 + 1747 + ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz); 1748 + if (ret) 1749 + return ret; 1750 + } 1751 + return 0; 1752 + } 1753 + 1754 + static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) 1755 + { 1756 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 1757 + gpa_t base = dev->itt_addr; 1758 + int ret; 1759 + int ite_esz = abi->ite_esz; 1760 + size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz; 1761 + 1762 + ret = scan_its_table(its, base, max_size, ite_esz, 0, 1763 + vgic_its_restore_ite, dev); 1764 + 1765 + return ret; 1766 + } 1767 + 1768 + /** 1769 + * vgic_its_save_dte - Save a device table entry at a given GPA 1770 + * 1771 + * @its: ITS handle 1772 + * @dev: ITS device 1773 + * @ptr: GPA 1774 + */ 1775 + static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev, 1776 + gpa_t ptr, int dte_esz) 1777 + { 1778 + struct kvm *kvm = its->dev->kvm; 1779 + u64 val, itt_addr_field; 1780 + u32 next_offset; 1781 + 1782 + itt_addr_field = dev->itt_addr >> 8; 1783 + next_offset = compute_next_devid_offset(&its->device_list, dev); 1784 + val = (1ULL << KVM_ITS_DTE_VALID_SHIFT | 1785 + ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) | 1786 + (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) | 1787 + (dev->num_eventid_bits - 1)); 1788 + val = cpu_to_le64(val); 1789 + return kvm_write_guest(kvm, ptr, &val, dte_esz); 1790 + } 1791 + 1792 + /** 1793 + * vgic_its_restore_dte - restore a device table entry 1794 + * 1795 + * @its: its handle 1796 + * @id: device id the DTE corresponds to 1797 + * @ptr: kernel VA where the 8 byte DTE is located 1798 + * @opaque: unused 1799 + * 1800 + * Return: < 0 on error, 0 if the dte is the last one, id offset to the 1801 + * next dte otherwise 1802 + */ 1803 + static int vgic_its_restore_dte(struct vgic_its *its, u32 id, 1804 + void *ptr, void *opaque) 1805 + { 1806 + struct its_device *dev; 1807 + gpa_t itt_addr; 1808 + u8 num_eventid_bits; 1809 + u64 entry = *(u64 *)ptr; 1810 + bool valid; 1811 + u32 offset; 1812 + int ret; 1813 + 1814 + entry = le64_to_cpu(entry); 1815 + 1816 + valid = entry >> KVM_ITS_DTE_VALID_SHIFT; 1817 + num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1; 1818 + itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK) 1819 + >> KVM_ITS_DTE_ITTADDR_SHIFT) << 8; 1820 + 1821 + if (!valid) 1822 + return 1; 1823 + 1824 + /* dte entry is valid */ 1825 + offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT; 1826 + 1827 + dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits); 1828 + if (IS_ERR(dev)) 1829 + return PTR_ERR(dev); 1830 + 1831 + ret = vgic_its_restore_itt(its, dev); 1832 + if (ret) { 1833 + vgic_its_free_device(its->dev->kvm, dev); 1834 + return ret; 1835 + } 1836 + 1837 + return offset; 1838 + } 1839 + 1840 + static int vgic_its_device_cmp(void *priv, struct list_head *a, 1841 + struct list_head *b) 1842 + { 1843 + struct its_device *deva = container_of(a, struct its_device, dev_list); 1844 + struct its_device *devb = container_of(b, struct its_device, dev_list); 1845 + 1846 + if (deva->device_id < devb->device_id) 1847 + return -1; 1848 + else 1849 + return 1; 1850 + } 1851 + 1852 + /** 1853 + * vgic_its_save_device_tables - Save the device table and all ITT 1854 + * into guest RAM 1855 + * 1856 + * L1/L2 handling is hidden by vgic_its_check_id() helper which directly 1857 + * returns the GPA of the device entry 1858 + */ 1859 + static int vgic_its_save_device_tables(struct vgic_its *its) 1860 + { 1861 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 1862 + struct its_device *dev; 1863 + int dte_esz = abi->dte_esz; 1864 + u64 baser; 1865 + 1866 + baser = its->baser_device_table; 1867 + 1868 + list_sort(NULL, &its->device_list, vgic_its_device_cmp); 1869 + 1870 + list_for_each_entry(dev, &its->device_list, dev_list) { 1871 + int ret; 1872 + gpa_t eaddr; 1873 + 1874 + if (!vgic_its_check_id(its, baser, 1875 + dev->device_id, &eaddr)) 1876 + return -EINVAL; 1877 + 1878 + ret = vgic_its_save_itt(its, dev); 1879 + if (ret) 1880 + return ret; 1881 + 1882 + ret = vgic_its_save_dte(its, dev, eaddr, dte_esz); 1883 + if (ret) 1884 + return ret; 1885 + } 1886 + return 0; 1887 + } 1888 + 1889 + /** 1890 + * handle_l1_dte - callback used for L1 device table entries (2 stage case) 1891 + * 1892 + * @its: its handle 1893 + * @id: index of the entry in the L1 table 1894 + * @addr: kernel VA 1895 + * @opaque: unused 1896 + * 1897 + * L1 table entries are scanned by steps of 1 entry 1898 + * Return < 0 if error, 0 if last dte was found when scanning the L2 1899 + * table, +1 otherwise (meaning next L1 entry must be scanned) 1900 + */ 1901 + static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr, 1902 + void *opaque) 1903 + { 1904 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 1905 + int l2_start_id = id * (SZ_64K / abi->dte_esz); 1906 + u64 entry = *(u64 *)addr; 1907 + int dte_esz = abi->dte_esz; 1908 + gpa_t gpa; 1909 + int ret; 1910 + 1911 + entry = le64_to_cpu(entry); 1912 + 1913 + if (!(entry & KVM_ITS_L1E_VALID_MASK)) 1914 + return 1; 1915 + 1916 + gpa = entry & KVM_ITS_L1E_ADDR_MASK; 1917 + 1918 + ret = scan_its_table(its, gpa, SZ_64K, dte_esz, 1919 + l2_start_id, vgic_its_restore_dte, NULL); 1920 + 1921 + if (ret <= 0) 1922 + return ret; 1923 + 1924 + return 1; 1925 + } 1926 + 1927 + /** 1928 + * vgic_its_restore_device_tables - Restore the device table and all ITT 1929 + * from guest RAM to internal data structs 1930 + */ 1931 + static int vgic_its_restore_device_tables(struct vgic_its *its) 1932 + { 1933 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 1934 + u64 baser = its->baser_device_table; 1935 + int l1_esz, ret; 1936 + int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; 1937 + gpa_t l1_gpa; 1938 + 1939 + if (!(baser & GITS_BASER_VALID)) 1940 + return 0; 1941 + 1942 + l1_gpa = BASER_ADDRESS(baser); 1943 + 1944 + if (baser & GITS_BASER_INDIRECT) { 1945 + l1_esz = GITS_LVL1_ENTRY_SIZE; 1946 + ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0, 1947 + handle_l1_dte, NULL); 1948 + } else { 1949 + l1_esz = abi->dte_esz; 1950 + ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0, 1951 + vgic_its_restore_dte, NULL); 1952 + } 1953 + 1954 + if (ret > 0) 1955 + ret = -EINVAL; 1956 + 1957 + return ret; 1958 + } 1959 + 1960 + static int vgic_its_save_cte(struct vgic_its *its, 1961 + struct its_collection *collection, 1962 + gpa_t gpa, int esz) 1963 + { 1964 + u64 val; 1965 + 1966 + val = (1ULL << KVM_ITS_CTE_VALID_SHIFT | 1967 + ((u64)collection->target_addr << KVM_ITS_CTE_RDBASE_SHIFT) | 1968 + collection->collection_id); 1969 + val = cpu_to_le64(val); 1970 + return kvm_write_guest(its->dev->kvm, gpa, &val, esz); 1971 + } 1972 + 1973 + static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz) 1974 + { 1975 + struct its_collection *collection; 1976 + struct kvm *kvm = its->dev->kvm; 1977 + u32 target_addr, coll_id; 1978 + u64 val; 1979 + int ret; 1980 + 1981 + BUG_ON(esz > sizeof(val)); 1982 + ret = kvm_read_guest(kvm, gpa, &val, esz); 1983 + if (ret) 1984 + return ret; 1985 + val = le64_to_cpu(val); 1986 + if (!(val & KVM_ITS_CTE_VALID_MASK)) 1987 + return 0; 1988 + 1989 + target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT); 1990 + coll_id = val & KVM_ITS_CTE_ICID_MASK; 1991 + 1992 + if (target_addr >= atomic_read(&kvm->online_vcpus)) 1993 + return -EINVAL; 1994 + 1995 + collection = find_collection(its, coll_id); 1996 + if (collection) 1997 + return -EEXIST; 1998 + ret = vgic_its_alloc_collection(its, &collection, coll_id); 1999 + if (ret) 2000 + return ret; 2001 + collection->target_addr = target_addr; 2002 + return 1; 2003 + } 2004 + 2005 + /** 2006 + * vgic_its_save_collection_table - Save the collection table into 2007 + * guest RAM 2008 + */ 2009 + static int vgic_its_save_collection_table(struct vgic_its *its) 2010 + { 2011 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2012 + struct its_collection *collection; 2013 + u64 val; 2014 + gpa_t gpa; 2015 + size_t max_size, filled = 0; 2016 + int ret, cte_esz = abi->cte_esz; 2017 + 2018 + gpa = BASER_ADDRESS(its->baser_coll_table); 2019 + if (!gpa) 2020 + return 0; 2021 + 2022 + max_size = GITS_BASER_NR_PAGES(its->baser_coll_table) * SZ_64K; 2023 + 2024 + list_for_each_entry(collection, &its->collection_list, coll_list) { 2025 + ret = vgic_its_save_cte(its, collection, gpa, cte_esz); 2026 + if (ret) 2027 + return ret; 2028 + gpa += cte_esz; 2029 + filled += cte_esz; 2030 + } 2031 + 2032 + if (filled == max_size) 2033 + return 0; 2034 + 2035 + /* 2036 + * table is not fully filled, add a last dummy element 2037 + * with valid bit unset 2038 + */ 2039 + val = 0; 2040 + BUG_ON(cte_esz > sizeof(val)); 2041 + ret = kvm_write_guest(its->dev->kvm, gpa, &val, cte_esz); 2042 + return ret; 2043 + } 2044 + 2045 + /** 2046 + * vgic_its_restore_collection_table - reads the collection table 2047 + * in guest memory and restores the ITS internal state. Requires the 2048 + * BASER registers to be restored before. 2049 + */ 2050 + static int vgic_its_restore_collection_table(struct vgic_its *its) 2051 + { 2052 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2053 + int cte_esz = abi->cte_esz; 2054 + size_t max_size, read = 0; 2055 + gpa_t gpa; 2056 + int ret; 2057 + 2058 + if (!(its->baser_coll_table & GITS_BASER_VALID)) 2059 + return 0; 2060 + 2061 + gpa = BASER_ADDRESS(its->baser_coll_table); 2062 + 2063 + max_size = GITS_BASER_NR_PAGES(its->baser_coll_table) * SZ_64K; 2064 + 2065 + while (read < max_size) { 2066 + ret = vgic_its_restore_cte(its, gpa, cte_esz); 2067 + if (ret <= 0) 2068 + break; 2069 + gpa += cte_esz; 2070 + read += cte_esz; 2071 + } 2072 + return ret; 2073 + } 2074 + 2075 + /** 2076 + * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM 2077 + * according to v0 ABI 2078 + */ 2079 + static int vgic_its_save_tables_v0(struct vgic_its *its) 2080 + { 2081 + struct kvm *kvm = its->dev->kvm; 2082 + int ret; 2083 + 2084 + mutex_lock(&kvm->lock); 2085 + mutex_lock(&its->its_lock); 2086 + 2087 + if (!lock_all_vcpus(kvm)) { 2088 + mutex_unlock(&its->its_lock); 2089 + mutex_unlock(&kvm->lock); 2090 + return -EBUSY; 2091 + } 2092 + 2093 + ret = vgic_its_save_device_tables(its); 2094 + if (ret) 2095 + goto out; 2096 + 2097 + ret = vgic_its_save_collection_table(its); 2098 + 2099 + out: 2100 + unlock_all_vcpus(kvm); 2101 + mutex_unlock(&its->its_lock); 2102 + mutex_unlock(&kvm->lock); 2103 + return ret; 2104 + } 2105 + 2106 + /** 2107 + * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM 2108 + * to internal data structs according to V0 ABI 2109 + * 2110 + */ 2111 + static int vgic_its_restore_tables_v0(struct vgic_its *its) 2112 + { 2113 + struct kvm *kvm = its->dev->kvm; 2114 + int ret; 2115 + 2116 + mutex_lock(&kvm->lock); 2117 + mutex_lock(&its->its_lock); 2118 + 2119 + if (!lock_all_vcpus(kvm)) { 2120 + mutex_unlock(&its->its_lock); 2121 + mutex_unlock(&kvm->lock); 2122 + return -EBUSY; 2123 + } 2124 + 2125 + ret = vgic_its_restore_collection_table(its); 2126 + if (ret) 2127 + goto out; 2128 + 2129 + ret = vgic_its_restore_device_tables(its); 2130 + out: 2131 + unlock_all_vcpus(kvm); 2132 + mutex_unlock(&its->its_lock); 2133 + mutex_unlock(&kvm->lock); 2134 + 2135 + return ret; 2136 + } 2137 + 2138 + static int vgic_its_commit_v0(struct vgic_its *its) 2139 + { 2140 + const struct vgic_its_abi *abi; 2141 + 2142 + abi = vgic_its_get_abi(its); 2143 + its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK; 2144 + its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK; 2145 + 2146 + its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5) 2147 + << GITS_BASER_ENTRY_SIZE_SHIFT); 2148 + 2149 + its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5) 2150 + << GITS_BASER_ENTRY_SIZE_SHIFT); 2151 + return 0; 1657 2152 } 1658 2153 1659 2154 static int vgic_its_has_attr(struct kvm_device *dev, ··· 2355 1480 switch (attr->attr) { 2356 1481 case KVM_DEV_ARM_VGIC_CTRL_INIT: 2357 1482 return 0; 1483 + case KVM_DEV_ARM_ITS_SAVE_TABLES: 1484 + return 0; 1485 + case KVM_DEV_ARM_ITS_RESTORE_TABLES: 1486 + return 0; 2358 1487 } 2359 1488 break; 1489 + case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: 1490 + return vgic_its_has_attr_regs(dev, attr); 2360 1491 } 2361 1492 return -ENXIO; 2362 1493 } ··· 2390 1509 if (ret) 2391 1510 return ret; 2392 1511 2393 - its->vgic_its_base = addr; 2394 - 2395 - return 0; 1512 + return vgic_register_its_iodev(dev->kvm, its, addr); 2396 1513 } 2397 - case KVM_DEV_ARM_VGIC_GRP_CTRL: 1514 + case KVM_DEV_ARM_VGIC_GRP_CTRL: { 1515 + const struct vgic_its_abi *abi = vgic_its_get_abi(its); 1516 + 2398 1517 switch (attr->attr) { 2399 1518 case KVM_DEV_ARM_VGIC_CTRL_INIT: 2400 - its->initialized = true; 2401 - 1519 + /* Nothing to do */ 2402 1520 return 0; 1521 + case KVM_DEV_ARM_ITS_SAVE_TABLES: 1522 + return abi->save_tables(its); 1523 + case KVM_DEV_ARM_ITS_RESTORE_TABLES: 1524 + return abi->restore_tables(its); 2403 1525 } 2404 - break; 1526 + } 1527 + case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: { 1528 + u64 __user *uaddr = (u64 __user *)(long)attr->addr; 1529 + u64 reg; 1530 + 1531 + if (get_user(reg, uaddr)) 1532 + return -EFAULT; 1533 + 1534 + return vgic_its_attr_regs_access(dev, attr, &reg, true); 1535 + } 2405 1536 } 2406 1537 return -ENXIO; 2407 1538 } ··· 2434 1541 if (copy_to_user(uaddr, &addr, sizeof(addr))) 2435 1542 return -EFAULT; 2436 1543 break; 1544 + } 1545 + case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: { 1546 + u64 __user *uaddr = (u64 __user *)(long)attr->addr; 1547 + u64 reg; 1548 + int ret; 1549 + 1550 + ret = vgic_its_attr_regs_access(dev, attr, &reg, false); 1551 + if (ret) 1552 + return ret; 1553 + return put_user(reg, uaddr); 1554 + } 2437 1555 default: 2438 1556 return -ENXIO; 2439 - } 2440 1557 } 2441 1558 2442 1559 return 0; ··· 2465 1562 { 2466 1563 return kvm_register_device_ops(&kvm_arm_vgic_its_ops, 2467 1564 KVM_DEV_TYPE_ARM_VGIC_ITS); 2468 - } 2469 - 2470 - /* 2471 - * Registers all ITSes with the kvm_io_bus framework. 2472 - * To follow the existing VGIC initialization sequence, this has to be 2473 - * done as late as possible, just before the first VCPU runs. 2474 - */ 2475 - int vgic_register_its_iodevs(struct kvm *kvm) 2476 - { 2477 - struct kvm_device *dev; 2478 - int ret = 0; 2479 - 2480 - list_for_each_entry(dev, &kvm->devices, vm_node) { 2481 - if (dev->ops != &kvm_arm_vgic_its_ops) 2482 - continue; 2483 - 2484 - ret = vgic_register_its_iodev(kvm, dev->private); 2485 - if (ret) 2486 - return ret; 2487 - /* 2488 - * We don't need to care about tearing down previously 2489 - * registered ITSes, as the kvm_io_bus framework removes 2490 - * them for us if the VM gets destroyed. 2491 - */ 2492 - } 2493 - 2494 - return ret; 2495 1565 }
+41 -12
virt/kvm/arm/vgic/vgic-kvm-device.c
··· 37 37 return 0; 38 38 } 39 39 40 + static int vgic_check_type(struct kvm *kvm, int type_needed) 41 + { 42 + if (kvm->arch.vgic.vgic_model != type_needed) 43 + return -ENODEV; 44 + else 45 + return 0; 46 + } 47 + 40 48 /** 41 49 * kvm_vgic_addr - set or get vgic VM base addresses 42 50 * @kvm: pointer to the vm struct ··· 65 57 { 66 58 int r = 0; 67 59 struct vgic_dist *vgic = &kvm->arch.vgic; 68 - int type_needed; 69 60 phys_addr_t *addr_ptr, alignment; 70 61 71 62 mutex_lock(&kvm->lock); 72 63 switch (type) { 73 64 case KVM_VGIC_V2_ADDR_TYPE_DIST: 74 - type_needed = KVM_DEV_TYPE_ARM_VGIC_V2; 65 + r = vgic_check_type(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); 75 66 addr_ptr = &vgic->vgic_dist_base; 76 67 alignment = SZ_4K; 77 68 break; 78 69 case KVM_VGIC_V2_ADDR_TYPE_CPU: 79 - type_needed = KVM_DEV_TYPE_ARM_VGIC_V2; 70 + r = vgic_check_type(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); 80 71 addr_ptr = &vgic->vgic_cpu_base; 81 72 alignment = SZ_4K; 82 73 break; 83 74 case KVM_VGIC_V3_ADDR_TYPE_DIST: 84 - type_needed = KVM_DEV_TYPE_ARM_VGIC_V3; 75 + r = vgic_check_type(kvm, KVM_DEV_TYPE_ARM_VGIC_V3); 85 76 addr_ptr = &vgic->vgic_dist_base; 86 77 alignment = SZ_64K; 87 78 break; 88 79 case KVM_VGIC_V3_ADDR_TYPE_REDIST: 89 - type_needed = KVM_DEV_TYPE_ARM_VGIC_V3; 80 + r = vgic_check_type(kvm, KVM_DEV_TYPE_ARM_VGIC_V3); 81 + if (r) 82 + break; 83 + if (write) { 84 + r = vgic_v3_set_redist_base(kvm, *addr); 85 + goto out; 86 + } 90 87 addr_ptr = &vgic->vgic_redist_base; 91 - alignment = SZ_64K; 92 88 break; 93 89 default: 94 90 r = -ENODEV; 95 - goto out; 96 91 } 97 92 98 - if (vgic->vgic_model != type_needed) { 99 - r = -ENODEV; 93 + if (r) 100 94 goto out; 101 - } 102 95 103 96 if (write) { 104 97 r = vgic_check_ioaddr(kvm, addr_ptr, *addr, alignment); ··· 268 259 } 269 260 } 270 261 271 - static void unlock_all_vcpus(struct kvm *kvm) 262 + void unlock_all_vcpus(struct kvm *kvm) 272 263 { 273 264 unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1); 274 265 } 275 266 276 267 /* Returns true if all vcpus were locked, false otherwise */ 277 - static bool lock_all_vcpus(struct kvm *kvm) 268 + bool lock_all_vcpus(struct kvm *kvm) 278 269 { 279 270 struct kvm_vcpu *tmp_vcpu; 280 271 int c; ··· 589 580 reg = tmp32; 590 581 return vgic_v3_attr_regs_access(dev, attr, &reg, true); 591 582 } 583 + case KVM_DEV_ARM_VGIC_GRP_CTRL: { 584 + int ret; 585 + 586 + switch (attr->attr) { 587 + case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES: 588 + mutex_lock(&dev->kvm->lock); 589 + 590 + if (!lock_all_vcpus(dev->kvm)) { 591 + mutex_unlock(&dev->kvm->lock); 592 + return -EBUSY; 593 + } 594 + ret = vgic_v3_save_pending_tables(dev->kvm); 595 + unlock_all_vcpus(dev->kvm); 596 + mutex_unlock(&dev->kvm->lock); 597 + return ret; 598 + } 599 + break; 600 + } 592 601 } 593 602 return -ENXIO; 594 603 } ··· 684 657 case KVM_DEV_ARM_VGIC_GRP_CTRL: 685 658 switch (attr->attr) { 686 659 case KVM_DEV_ARM_VGIC_CTRL_INIT: 660 + return 0; 661 + case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES: 687 662 return 0; 688 663 } 689 664 }
+105 -42
virt/kvm/arm/vgic/vgic-mmio-v3.c
··· 556 556 return SZ_64K; 557 557 } 558 558 559 - int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t redist_base_address) 559 + /** 560 + * vgic_register_redist_iodev - register a single redist iodev 561 + * @vcpu: The VCPU to which the redistributor belongs 562 + * 563 + * Register a KVM iodev for this VCPU's redistributor using the address 564 + * provided. 565 + * 566 + * Return 0 on success, -ERRNO otherwise. 567 + */ 568 + int vgic_register_redist_iodev(struct kvm_vcpu *vcpu) 569 + { 570 + struct kvm *kvm = vcpu->kvm; 571 + struct vgic_dist *vgic = &kvm->arch.vgic; 572 + struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev; 573 + struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev; 574 + gpa_t rd_base, sgi_base; 575 + int ret; 576 + 577 + /* 578 + * We may be creating VCPUs before having set the base address for the 579 + * redistributor region, in which case we will come back to this 580 + * function for all VCPUs when the base address is set. Just return 581 + * without doing any work for now. 582 + */ 583 + if (IS_VGIC_ADDR_UNDEF(vgic->vgic_redist_base)) 584 + return 0; 585 + 586 + if (!vgic_v3_check_base(kvm)) 587 + return -EINVAL; 588 + 589 + rd_base = vgic->vgic_redist_base + kvm_vcpu_get_idx(vcpu) * SZ_64K * 2; 590 + sgi_base = rd_base + SZ_64K; 591 + 592 + kvm_iodevice_init(&rd_dev->dev, &kvm_io_gic_ops); 593 + rd_dev->base_addr = rd_base; 594 + rd_dev->iodev_type = IODEV_REDIST; 595 + rd_dev->regions = vgic_v3_rdbase_registers; 596 + rd_dev->nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers); 597 + rd_dev->redist_vcpu = vcpu; 598 + 599 + mutex_lock(&kvm->slots_lock); 600 + ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, rd_base, 601 + SZ_64K, &rd_dev->dev); 602 + mutex_unlock(&kvm->slots_lock); 603 + 604 + if (ret) 605 + return ret; 606 + 607 + kvm_iodevice_init(&sgi_dev->dev, &kvm_io_gic_ops); 608 + sgi_dev->base_addr = sgi_base; 609 + sgi_dev->iodev_type = IODEV_REDIST; 610 + sgi_dev->regions = vgic_v3_sgibase_registers; 611 + sgi_dev->nr_regions = ARRAY_SIZE(vgic_v3_sgibase_registers); 612 + sgi_dev->redist_vcpu = vcpu; 613 + 614 + mutex_lock(&kvm->slots_lock); 615 + ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, sgi_base, 616 + SZ_64K, &sgi_dev->dev); 617 + mutex_unlock(&kvm->slots_lock); 618 + if (ret) 619 + kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, 620 + &rd_dev->dev); 621 + 622 + return ret; 623 + } 624 + 625 + static void vgic_unregister_redist_iodev(struct kvm_vcpu *vcpu) 626 + { 627 + struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev; 628 + struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev; 629 + 630 + kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &rd_dev->dev); 631 + kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &sgi_dev->dev); 632 + } 633 + 634 + static int vgic_register_all_redist_iodevs(struct kvm *kvm) 560 635 { 561 636 struct kvm_vcpu *vcpu; 562 637 int c, ret = 0; 563 638 564 639 kvm_for_each_vcpu(c, vcpu, kvm) { 565 - gpa_t rd_base = redist_base_address + c * SZ_64K * 2; 566 - gpa_t sgi_base = rd_base + SZ_64K; 567 - struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev; 568 - struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev; 569 - 570 - kvm_iodevice_init(&rd_dev->dev, &kvm_io_gic_ops); 571 - rd_dev->base_addr = rd_base; 572 - rd_dev->iodev_type = IODEV_REDIST; 573 - rd_dev->regions = vgic_v3_rdbase_registers; 574 - rd_dev->nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers); 575 - rd_dev->redist_vcpu = vcpu; 576 - 577 - mutex_lock(&kvm->slots_lock); 578 - ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, rd_base, 579 - SZ_64K, &rd_dev->dev); 580 - mutex_unlock(&kvm->slots_lock); 581 - 640 + ret = vgic_register_redist_iodev(vcpu); 582 641 if (ret) 583 642 break; 584 - 585 - kvm_iodevice_init(&sgi_dev->dev, &kvm_io_gic_ops); 586 - sgi_dev->base_addr = sgi_base; 587 - sgi_dev->iodev_type = IODEV_REDIST; 588 - sgi_dev->regions = vgic_v3_sgibase_registers; 589 - sgi_dev->nr_regions = ARRAY_SIZE(vgic_v3_sgibase_registers); 590 - sgi_dev->redist_vcpu = vcpu; 591 - 592 - mutex_lock(&kvm->slots_lock); 593 - ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, sgi_base, 594 - SZ_64K, &sgi_dev->dev); 595 - mutex_unlock(&kvm->slots_lock); 596 - if (ret) { 597 - kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, 598 - &rd_dev->dev); 599 - break; 600 - } 601 643 } 602 644 603 645 if (ret) { 604 646 /* The current c failed, so we start with the previous one. */ 605 647 for (c--; c >= 0; c--) { 606 - struct vgic_cpu *vgic_cpu; 607 - 608 648 vcpu = kvm_get_vcpu(kvm, c); 609 - vgic_cpu = &vcpu->arch.vgic_cpu; 610 - kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, 611 - &vgic_cpu->rd_iodev.dev); 612 - kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, 613 - &vgic_cpu->sgi_iodev.dev); 649 + vgic_unregister_redist_iodev(vcpu); 614 650 } 615 651 } 616 652 617 653 return ret; 654 + } 655 + 656 + int vgic_v3_set_redist_base(struct kvm *kvm, u64 addr) 657 + { 658 + struct vgic_dist *vgic = &kvm->arch.vgic; 659 + int ret; 660 + 661 + /* vgic_check_ioaddr makes sure we don't do this twice */ 662 + ret = vgic_check_ioaddr(kvm, &vgic->vgic_redist_base, addr, SZ_64K); 663 + if (ret) 664 + return ret; 665 + 666 + vgic->vgic_redist_base = addr; 667 + if (!vgic_v3_check_base(kvm)) { 668 + vgic->vgic_redist_base = VGIC_ADDR_UNDEF; 669 + return -EINVAL; 670 + } 671 + 672 + /* 673 + * Register iodevs for each existing VCPU. Adding more VCPUs 674 + * afterwards will register the iodevs when needed. 675 + */ 676 + ret = vgic_register_all_redist_iodevs(kvm); 677 + if (ret) 678 + return ret; 679 + 680 + return 0; 618 681 } 619 682 620 683 int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)
+5 -6
virt/kvm/arm/vgic/vgic-mmio.c
··· 446 446 return 0; 447 447 } 448 448 449 - /* Find the proper register handler entry given a certain address offset. */ 450 - static const struct vgic_register_region * 451 - vgic_find_mmio_region(const struct vgic_register_region *region, int nr_regions, 452 - unsigned int offset) 449 + const struct vgic_register_region * 450 + vgic_find_mmio_region(const struct vgic_register_region *regions, 451 + int nr_regions, unsigned int offset) 453 452 { 454 - return bsearch((void *)(uintptr_t)offset, region, nr_regions, 455 - sizeof(region[0]), match_region); 453 + return bsearch((void *)(uintptr_t)offset, regions, nr_regions, 454 + sizeof(regions[0]), match_region); 456 455 } 457 456 458 457 void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
+12 -2
virt/kvm/arm/vgic/vgic-mmio.h
··· 36 36 }; 37 37 unsigned long (*uaccess_read)(struct kvm_vcpu *vcpu, gpa_t addr, 38 38 unsigned int len); 39 - void (*uaccess_write)(struct kvm_vcpu *vcpu, gpa_t addr, 40 - unsigned int len, unsigned long val); 39 + union { 40 + void (*uaccess_write)(struct kvm_vcpu *vcpu, gpa_t addr, 41 + unsigned int len, unsigned long val); 42 + int (*uaccess_its_write)(struct kvm *kvm, struct vgic_its *its, 43 + gpa_t addr, unsigned int len, 44 + unsigned long val); 45 + }; 41 46 }; 42 47 43 48 extern struct kvm_io_device_ops kvm_io_gic_ops; ··· 196 191 u64 vgic_sanitise_shareability(u64 reg); 197 192 u64 vgic_sanitise_field(u64 reg, u64 field_mask, int field_shift, 198 193 u64 (*sanitise_fn)(u64)); 194 + 195 + /* Find the proper register handler entry given a certain address offset */ 196 + const struct vgic_register_region * 197 + vgic_find_mmio_region(const struct vgic_register_region *regions, 198 + int nr_regions, unsigned int offset); 199 199 200 200 #endif
+110 -18
virt/kvm/arm/vgic/vgic-v3.c
··· 234 234 vgic_v3->vgic_hcr = ICH_HCR_EN; 235 235 } 236 236 237 - /* check for overlapping regions and for regions crossing the end of memory */ 238 - static bool vgic_v3_check_base(struct kvm *kvm) 237 + int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq) 238 + { 239 + struct kvm_vcpu *vcpu; 240 + int byte_offset, bit_nr; 241 + gpa_t pendbase, ptr; 242 + bool status; 243 + u8 val; 244 + int ret; 245 + 246 + retry: 247 + vcpu = irq->target_vcpu; 248 + if (!vcpu) 249 + return 0; 250 + 251 + pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); 252 + 253 + byte_offset = irq->intid / BITS_PER_BYTE; 254 + bit_nr = irq->intid % BITS_PER_BYTE; 255 + ptr = pendbase + byte_offset; 256 + 257 + ret = kvm_read_guest(kvm, ptr, &val, 1); 258 + if (ret) 259 + return ret; 260 + 261 + status = val & (1 << bit_nr); 262 + 263 + spin_lock(&irq->irq_lock); 264 + if (irq->target_vcpu != vcpu) { 265 + spin_unlock(&irq->irq_lock); 266 + goto retry; 267 + } 268 + irq->pending_latch = status; 269 + vgic_queue_irq_unlock(vcpu->kvm, irq); 270 + 271 + if (status) { 272 + /* clear consumed data */ 273 + val &= ~(1 << bit_nr); 274 + ret = kvm_write_guest(kvm, ptr, &val, 1); 275 + if (ret) 276 + return ret; 277 + } 278 + return 0; 279 + } 280 + 281 + /** 282 + * vgic_its_save_pending_tables - Save the pending tables into guest RAM 283 + * kvm lock and all vcpu lock must be held 284 + */ 285 + int vgic_v3_save_pending_tables(struct kvm *kvm) 286 + { 287 + struct vgic_dist *dist = &kvm->arch.vgic; 288 + int last_byte_offset = -1; 289 + struct vgic_irq *irq; 290 + int ret; 291 + 292 + list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { 293 + int byte_offset, bit_nr; 294 + struct kvm_vcpu *vcpu; 295 + gpa_t pendbase, ptr; 296 + bool stored; 297 + u8 val; 298 + 299 + vcpu = irq->target_vcpu; 300 + if (!vcpu) 301 + continue; 302 + 303 + pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); 304 + 305 + byte_offset = irq->intid / BITS_PER_BYTE; 306 + bit_nr = irq->intid % BITS_PER_BYTE; 307 + ptr = pendbase + byte_offset; 308 + 309 + if (byte_offset != last_byte_offset) { 310 + ret = kvm_read_guest(kvm, ptr, &val, 1); 311 + if (ret) 312 + return ret; 313 + last_byte_offset = byte_offset; 314 + } 315 + 316 + stored = val & (1U << bit_nr); 317 + if (stored == irq->pending_latch) 318 + continue; 319 + 320 + if (irq->pending_latch) 321 + val |= 1 << bit_nr; 322 + else 323 + val &= ~(1 << bit_nr); 324 + 325 + ret = kvm_write_guest(kvm, ptr, &val, 1); 326 + if (ret) 327 + return ret; 328 + } 329 + return 0; 330 + } 331 + 332 + /* 333 + * Check for overlapping regions and for regions crossing the end of memory 334 + * for base addresses which have already been set. 335 + */ 336 + bool vgic_v3_check_base(struct kvm *kvm) 239 337 { 240 338 struct vgic_dist *d = &kvm->arch.vgic; 241 339 gpa_t redist_size = KVM_VGIC_V3_REDIST_SIZE; 242 340 243 341 redist_size *= atomic_read(&kvm->online_vcpus); 244 342 245 - if (d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE < d->vgic_dist_base) 343 + if (!IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) && 344 + d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE < d->vgic_dist_base) 246 345 return false; 247 - if (d->vgic_redist_base + redist_size < d->vgic_redist_base) 346 + 347 + if (!IS_VGIC_ADDR_UNDEF(d->vgic_redist_base) && 348 + d->vgic_redist_base + redist_size < d->vgic_redist_base) 248 349 return false; 350 + 351 + /* Both base addresses must be set to check if they overlap */ 352 + if (IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) || 353 + IS_VGIC_ADDR_UNDEF(d->vgic_redist_base)) 354 + return true; 249 355 250 356 if (d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE <= d->vgic_redist_base) 251 357 return true; ··· 395 289 if (ret) { 396 290 kvm_err("Unable to register VGICv3 dist MMIO regions\n"); 397 291 goto out; 398 - } 399 - 400 - ret = vgic_register_redist_iodevs(kvm, dist->vgic_redist_base); 401 - if (ret) { 402 - kvm_err("Unable to register VGICv3 redist MMIO regions\n"); 403 - goto out; 404 - } 405 - 406 - if (vgic_has_its(kvm)) { 407 - ret = vgic_register_its_iodevs(kvm); 408 - if (ret) { 409 - kvm_err("Unable to register VGIC ITS MMIO regions\n"); 410 - goto out; 411 - } 412 292 } 413 293 414 294 dist->ready = true;
+1 -1
virt/kvm/arm/vgic/vgic.c
··· 21 21 #include "vgic.h" 22 22 23 23 #define CREATE_TRACE_POINTS 24 - #include "../trace.h" 24 + #include "trace.h" 25 25 26 26 #ifdef CONFIG_DEBUG_SPINLOCK 27 27 #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
+31 -2
virt/kvm/arm/vgic/vgic.h
··· 73 73 KVM_REG_ARM_VGIC_SYSREG_CRM_MASK | \ 74 74 KVM_REG_ARM_VGIC_SYSREG_OP2_MASK) 75 75 76 + /* 77 + * As per Documentation/virtual/kvm/devices/arm-vgic-its.txt, 78 + * below macros are defined for ITS table entry encoding. 79 + */ 80 + #define KVM_ITS_CTE_VALID_SHIFT 63 81 + #define KVM_ITS_CTE_VALID_MASK BIT_ULL(63) 82 + #define KVM_ITS_CTE_RDBASE_SHIFT 16 83 + #define KVM_ITS_CTE_ICID_MASK GENMASK_ULL(15, 0) 84 + #define KVM_ITS_ITE_NEXT_SHIFT 48 85 + #define KVM_ITS_ITE_PINTID_SHIFT 16 86 + #define KVM_ITS_ITE_PINTID_MASK GENMASK_ULL(47, 16) 87 + #define KVM_ITS_ITE_ICID_MASK GENMASK_ULL(15, 0) 88 + #define KVM_ITS_DTE_VALID_SHIFT 63 89 + #define KVM_ITS_DTE_VALID_MASK BIT_ULL(63) 90 + #define KVM_ITS_DTE_NEXT_SHIFT 49 91 + #define KVM_ITS_DTE_NEXT_MASK GENMASK_ULL(62, 49) 92 + #define KVM_ITS_DTE_ITTADDR_SHIFT 5 93 + #define KVM_ITS_DTE_ITTADDR_MASK GENMASK_ULL(48, 5) 94 + #define KVM_ITS_DTE_SIZE_MASK GENMASK_ULL(4, 0) 95 + #define KVM_ITS_L1E_VALID_MASK BIT_ULL(63) 96 + /* we only support 64 kB translation table page size */ 97 + #define KVM_ITS_L1E_ADDR_MASK GENMASK_ULL(51, 16) 98 + 76 99 static inline bool irq_is_pending(struct vgic_irq *irq) 77 100 { 78 101 if (irq->config == VGIC_CONFIG_EDGE) ··· 180 157 void vgic_v3_enable(struct kvm_vcpu *vcpu); 181 158 int vgic_v3_probe(const struct gic_kvm_info *info); 182 159 int vgic_v3_map_resources(struct kvm *kvm); 183 - int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t dist_base_address); 160 + int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq); 161 + int vgic_v3_save_pending_tables(struct kvm *kvm); 162 + int vgic_v3_set_redist_base(struct kvm *kvm, u64 addr); 163 + int vgic_register_redist_iodev(struct kvm_vcpu *vcpu); 164 + bool vgic_v3_check_base(struct kvm *kvm); 184 165 185 166 void vgic_v3_load(struct kvm_vcpu *vcpu); 186 167 void vgic_v3_put(struct kvm_vcpu *vcpu); 187 168 188 - int vgic_register_its_iodevs(struct kvm *kvm); 189 169 bool vgic_has_its(struct kvm *kvm); 190 170 int kvm_vgic_register_its_device(void); 191 171 void vgic_enable_lpis(struct kvm_vcpu *vcpu); ··· 212 186 213 187 int vgic_debug_init(struct kvm *kvm); 214 188 int vgic_debug_destroy(struct kvm *kvm); 189 + 190 + bool lock_all_vcpus(struct kvm *kvm); 191 + void unlock_all_vcpus(struct kvm *kvm); 215 192 216 193 #endif