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

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

KVM/ARM Changes for v4.8 - Take 2

Includes GSI routing support to go along with the new VGIC and a small fix that
has been cooking in -next for a while.

+228 -62
+33 -13
Documentation/virtual/kvm/api.txt
··· 1433 1433 4.52 KVM_SET_GSI_ROUTING 1434 1434 1435 1435 Capability: KVM_CAP_IRQ_ROUTING 1436 - Architectures: x86 s390 1436 + Architectures: x86 s390 arm arm64 1437 1437 Type: vm ioctl 1438 1438 Parameters: struct kvm_irq_routing (in) 1439 1439 Returns: 0 on success, -1 on error 1440 1440 1441 1441 Sets the GSI routing table entries, overwriting any previously set entries. 1442 + 1443 + On arm/arm64, GSI routing has the following limitation: 1444 + - GSI routing does not apply to KVM_IRQ_LINE but only to KVM_IRQFD. 1442 1445 1443 1446 struct kvm_irq_routing { 1444 1447 __u32 nr; ··· 1471 1468 #define KVM_IRQ_ROUTING_S390_ADAPTER 3 1472 1469 #define KVM_IRQ_ROUTING_HV_SINT 4 1473 1470 1474 - No flags are specified so far, the corresponding field must be set to zero. 1471 + flags: 1472 + - KVM_MSI_VALID_DEVID: used along with KVM_IRQ_ROUTING_MSI routing entry 1473 + type, specifies that the devid field contains a valid value. The per-VM 1474 + KVM_CAP_MSI_DEVID capability advertises the requirement to provide 1475 + the device ID. If this capability is not available, userspace should 1476 + never set the KVM_MSI_VALID_DEVID flag as the ioctl might fail. 1477 + - zero otherwise 1475 1478 1476 1479 struct kvm_irq_routing_irqchip { 1477 1480 __u32 irqchip; ··· 1488 1479 __u32 address_lo; 1489 1480 __u32 address_hi; 1490 1481 __u32 data; 1491 - __u32 pad; 1482 + union { 1483 + __u32 pad; 1484 + __u32 devid; 1485 + }; 1492 1486 }; 1487 + 1488 + If KVM_MSI_VALID_DEVID is set, devid contains a unique device identifier 1489 + for the device that wrote the MSI message. For PCI, this is usually a 1490 + BFD identifier in the lower 16 bits. 1493 1491 1494 1492 On x86, address_hi is ignored unless the KVM_X2APIC_API_USE_32BIT_IDS 1495 1493 feature of KVM_CAP_X2APIC_API capability is enabled. If it is enabled, ··· 2215 2199 __u8 pad[12]; 2216 2200 }; 2217 2201 2218 - flags: KVM_MSI_VALID_DEVID: devid contains a valid value 2219 - devid: If KVM_MSI_VALID_DEVID is set, contains a unique device identifier 2220 - for the device that wrote the MSI message. 2221 - For PCI, this is usually a BFD identifier in the lower 16 bits. 2202 + flags: KVM_MSI_VALID_DEVID: devid contains a valid value. The per-VM 2203 + KVM_CAP_MSI_DEVID capability advertises the requirement to provide 2204 + the device ID. If this capability is not available, userspace 2205 + should never set the KVM_MSI_VALID_DEVID flag as the ioctl might fail. 2222 2206 2223 - The per-VM KVM_CAP_MSI_DEVID capability advertises the need to provide 2224 - the device ID. If this capability is not set, userland cannot rely on 2225 - the kernel to allow the KVM_MSI_VALID_DEVID flag being set. 2207 + If KVM_MSI_VALID_DEVID is set, devid contains a unique device identifier 2208 + for the device that wrote the MSI message. For PCI, this is usually a 2209 + BFD identifier in the lower 16 bits. 2226 2210 2227 2211 On x86, address_hi is ignored unless the KVM_CAP_X2APIC_API capability is 2228 2212 enabled. If it is enabled, address_hi bits 31-8 provide bits 31-8 of the ··· 2399 2383 irqfd. The KVM_IRQFD_FLAG_RESAMPLE is only necessary on assignment 2400 2384 and need not be specified with KVM_IRQFD_FLAG_DEASSIGN. 2401 2385 2402 - On ARM/ARM64, the gsi field in the kvm_irqfd struct specifies the Shared 2403 - Peripheral Interrupt (SPI) index, such that the GIC interrupt ID is 2404 - given by gsi + 32. 2386 + On arm/arm64, gsi routing being supported, the following can happen: 2387 + - in case no routing entry is associated to this gsi, injection fails 2388 + - in case the gsi is associated to an irqchip routing entry, 2389 + irqchip.pin + 32 corresponds to the injected SPI ID. 2390 + - in case the gsi is associated to an MSI routing entry, the MSI 2391 + message and device ID are translated into an LPI (support restricted 2392 + to GICv3 ITS in-kernel emulation). 2405 2393 2406 2394 4.76 KVM_PPC_ALLOCATE_HTAB 2407 2395
+2
arch/arm/kvm/Kconfig
··· 32 32 select KVM_VFIO 33 33 select HAVE_KVM_EVENTFD 34 34 select HAVE_KVM_IRQFD 35 + select HAVE_KVM_IRQCHIP 36 + select HAVE_KVM_IRQ_ROUTING 35 37 depends on ARM_VIRT_EXT && ARM_LPAE && ARM_ARCH_TIMER 36 38 ---help--- 37 39 Support hosting virtualized guest machines.
+1
arch/arm/kvm/Makefile
··· 29 29 obj-y += $(KVM)/arm/vgic/vgic-mmio.o 30 30 obj-y += $(KVM)/arm/vgic/vgic-mmio-v2.o 31 31 obj-y += $(KVM)/arm/vgic/vgic-kvm-device.o 32 + obj-y += $(KVM)/irqchip.o 32 33 obj-y += $(KVM)/arm/arch_timer.o
+19
arch/arm/kvm/irq.h
··· 1 + /* 2 + * irq.h: in kernel interrupt controller related definitions 3 + * Copyright (c) 2016 Red Hat, Inc. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms and conditions of the GNU General Public License, 7 + * version 2, as published by the Free Software Foundation. 8 + * 9 + * This header is included by irqchip.c. However, on ARM, interrupt 10 + * controller declarations are located in include/kvm/arm_vgic.h since 11 + * they are mostly shared between arm and arm64. 12 + */ 13 + 14 + #ifndef __IRQ_H 15 + #define __IRQ_H 16 + 17 + #include <kvm/arm_vgic.h> 18 + 19 + #endif
+2
arch/arm64/kvm/Kconfig
··· 37 37 select KVM_ARM_VGIC_V3 38 38 select KVM_ARM_PMU if HW_PERF_EVENTS 39 39 select HAVE_KVM_MSI 40 + select HAVE_KVM_IRQCHIP 41 + select HAVE_KVM_IRQ_ROUTING 40 42 ---help--- 41 43 Support hosting virtualized guest machines. 42 44 We don't support KVM with 16K page tables yet, due to the multiple
+1
arch/arm64/kvm/Makefile
··· 30 30 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-mmio-v3.o 31 31 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-kvm-device.o 32 32 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-its.o 33 + kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/irqchip.o 33 34 kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o 34 35 kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o
+5 -7
arch/arm64/kvm/inject_fault.c
··· 132 132 static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr) 133 133 { 134 134 unsigned long cpsr = *vcpu_cpsr(vcpu); 135 - bool is_aarch32; 135 + bool is_aarch32 = vcpu_mode_is_32bit(vcpu); 136 136 u32 esr = 0; 137 137 138 - is_aarch32 = vcpu_mode_is_32bit(vcpu); 139 - 140 - *vcpu_spsr(vcpu) = cpsr; 141 138 *vcpu_elr_el1(vcpu) = *vcpu_pc(vcpu); 142 - 143 139 *vcpu_pc(vcpu) = get_except_vector(vcpu, except_type_sync); 140 + 144 141 *vcpu_cpsr(vcpu) = PSTATE_FAULT_BITS_64; 142 + *vcpu_spsr(vcpu) = cpsr; 145 143 146 144 vcpu_sys_reg(vcpu, FAR_EL1) = addr; 147 145 ··· 170 172 unsigned long cpsr = *vcpu_cpsr(vcpu); 171 173 u32 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT); 172 174 173 - *vcpu_spsr(vcpu) = cpsr; 174 175 *vcpu_elr_el1(vcpu) = *vcpu_pc(vcpu); 175 - 176 176 *vcpu_pc(vcpu) = get_except_vector(vcpu, except_type_sync); 177 + 177 178 *vcpu_cpsr(vcpu) = PSTATE_FAULT_BITS_64; 179 + *vcpu_spsr(vcpu) = cpsr; 178 180 179 181 /* 180 182 * Build an unknown exception, depending on the instruction
+19
arch/arm64/kvm/irq.h
··· 1 + /* 2 + * irq.h: in kernel interrupt controller related definitions 3 + * Copyright (c) 2016 Red Hat, Inc. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms and conditions of the GNU General Public License, 7 + * version 2, as published by the Free Software Foundation. 8 + * 9 + * This header is included by irqchip.c. However, on ARM, interrupt 10 + * controller declarations are located in include/kvm/arm_vgic.h since 11 + * they are mostly shared between arm and arm64. 12 + */ 13 + 14 + #ifndef __IRQ_H 15 + #define __IRQ_H 16 + 17 + #include <kvm/arm_vgic.h> 18 + 19 + #endif
+3
arch/x86/kvm/irq.h
··· 120 120 121 121 int apic_has_pending_timer(struct kvm_vcpu *vcpu); 122 122 123 + int kvm_setup_default_irq_routing(struct kvm *kvm); 124 + int kvm_setup_empty_irq_routing(struct kvm *kvm); 125 + 123 126 #endif
+7
include/kvm/arm_vgic.h
··· 34 34 #define VGIC_MAX_SPI 1019 35 35 #define VGIC_MAX_RESERVED 1023 36 36 #define VGIC_MIN_LPI 8192 37 + #define KVM_IRQCHIP_NUM_PINS (1020 - 32) 37 38 38 39 enum vgic_type { 39 40 VGIC_V2, /* Good ol' GICv2 */ ··· 314 313 } 315 314 316 315 int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi); 316 + 317 + /** 318 + * kvm_vgic_setup_default_irq_routing: 319 + * Setup a default flat gsi routing table mapping all SPIs 320 + */ 321 + int kvm_vgic_setup_default_irq_routing(struct kvm *kvm); 317 322 318 323 #endif /* __KVM_ARM_VGIC_H */
+9 -3
include/linux/kvm_host.h
··· 317 317 unsigned irqchip; 318 318 unsigned pin; 319 319 } irqchip; 320 - struct msi_msg msi; 320 + struct { 321 + u32 address_lo; 322 + u32 address_hi; 323 + u32 data; 324 + u32 flags; 325 + u32 devid; 326 + } msi; 321 327 struct kvm_s390_adapter_int adapter; 322 328 struct kvm_hv_sint hv_sint; 323 329 }; ··· 1009 1003 1010 1004 #ifdef CONFIG_S390 1011 1005 #define KVM_MAX_IRQ_ROUTES 4096 //FIXME: we can have more than that... 1006 + #elif defined(CONFIG_ARM64) 1007 + #define KVM_MAX_IRQ_ROUTES 4096 1012 1008 #else 1013 1009 #define KVM_MAX_IRQ_ROUTES 1024 1014 1010 #endif 1015 1011 1016 - int kvm_setup_default_irq_routing(struct kvm *kvm); 1017 - int kvm_setup_empty_irq_routing(struct kvm *kvm); 1018 1012 int kvm_set_irq_routing(struct kvm *kvm, 1019 1013 const struct kvm_irq_routing_entry *entries, 1020 1014 unsigned nr,
+4 -1
include/uapi/linux/kvm.h
··· 882 882 __u32 address_lo; 883 883 __u32 address_hi; 884 884 __u32 data; 885 - __u32 pad; 885 + union { 886 + __u32 pad; 887 + __u32 devid; 888 + }; 886 889 }; 887 890 888 891 struct kvm_irq_routing_s390_adapter {
+4
virt/kvm/arm/vgic/vgic-init.c
··· 264 264 kvm_for_each_vcpu(i, vcpu, kvm) 265 265 kvm_vgic_vcpu_init(vcpu); 266 266 267 + ret = kvm_vgic_setup_default_irq_routing(kvm); 268 + if (ret) 269 + goto out; 270 + 267 271 dist->initialized = true; 268 272 out: 269 273 return ret;
+101 -21
virt/kvm/arm/vgic/vgic-irqfd.c
··· 17 17 #include <linux/kvm.h> 18 18 #include <linux/kvm_host.h> 19 19 #include <trace/events/kvm.h> 20 + #include <kvm/arm_vgic.h> 21 + #include "vgic.h" 20 22 21 - int kvm_irq_map_gsi(struct kvm *kvm, 22 - struct kvm_kernel_irq_routing_entry *entries, 23 - int gsi) 23 + /** 24 + * vgic_irqfd_set_irq: inject the IRQ corresponding to the 25 + * irqchip routing entry 26 + * 27 + * This is the entry point for irqfd IRQ injection 28 + */ 29 + static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e, 30 + struct kvm *kvm, int irq_source_id, 31 + int level, bool line_status) 24 32 { 25 - return 0; 33 + unsigned int spi_id = e->irqchip.pin + VGIC_NR_PRIVATE_IRQS; 34 + 35 + if (!vgic_valid_spi(kvm, spi_id)) 36 + return -EINVAL; 37 + return kvm_vgic_inject_irq(kvm, 0, spi_id, level); 26 38 } 27 39 28 - int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned int irqchip, 29 - unsigned int pin) 40 + /** 41 + * kvm_set_routing_entry: populate a kvm routing entry 42 + * from a user routing entry 43 + * 44 + * @kvm: the VM this entry is applied to 45 + * @e: kvm kernel routing entry handle 46 + * @ue: user api routing entry handle 47 + * return 0 on success, -EINVAL on errors. 48 + */ 49 + #ifdef KVM_CAP_X2APIC_API 50 + int kvm_set_routing_entry(struct kvm *kvm, 51 + struct kvm_kernel_irq_routing_entry *e, 52 + const struct kvm_irq_routing_entry *ue) 53 + #else 54 + /* Remove this version and the ifdefery once merged into 4.8 */ 55 + int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e, 56 + const struct kvm_irq_routing_entry *ue) 57 + #endif 30 58 { 31 - return pin; 59 + int r = -EINVAL; 60 + 61 + switch (ue->type) { 62 + case KVM_IRQ_ROUTING_IRQCHIP: 63 + e->set = vgic_irqfd_set_irq; 64 + e->irqchip.irqchip = ue->u.irqchip.irqchip; 65 + e->irqchip.pin = ue->u.irqchip.pin; 66 + if ((e->irqchip.pin >= KVM_IRQCHIP_NUM_PINS) || 67 + (e->irqchip.irqchip >= KVM_NR_IRQCHIPS)) 68 + goto out; 69 + break; 70 + case KVM_IRQ_ROUTING_MSI: 71 + e->set = kvm_set_msi; 72 + e->msi.address_lo = ue->u.msi.address_lo; 73 + e->msi.address_hi = ue->u.msi.address_hi; 74 + e->msi.data = ue->u.msi.data; 75 + e->msi.flags = ue->flags; 76 + e->msi.devid = ue->u.msi.devid; 77 + break; 78 + default: 79 + goto out; 80 + } 81 + r = 0; 82 + out: 83 + return r; 32 84 } 33 85 34 - int kvm_set_irq(struct kvm *kvm, int irq_source_id, 35 - u32 irq, int level, bool line_status) 36 - { 37 - unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS; 38 - 39 - trace_kvm_set_irq(irq, level, irq_source_id); 40 - 41 - BUG_ON(!vgic_initialized(kvm)); 42 - 43 - return kvm_vgic_inject_irq(kvm, 0, spi, level); 44 - } 45 - 46 - /* MSI not implemented yet */ 86 + /** 87 + * kvm_set_msi: inject the MSI corresponding to the 88 + * MSI routing entry 89 + * 90 + * This is the entry point for irqfd MSI injection 91 + * and userspace MSI injection. 92 + */ 47 93 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, 48 94 struct kvm *kvm, int irq_source_id, 49 95 int level, bool line_status) 50 96 { 51 - return 0; 97 + struct kvm_msi msi; 98 + 99 + msi.address_lo = e->msi.address_lo; 100 + msi.address_hi = e->msi.address_hi; 101 + msi.data = e->msi.data; 102 + msi.flags = e->msi.flags; 103 + msi.devid = e->msi.devid; 104 + 105 + if (!vgic_has_its(kvm)) 106 + return -ENODEV; 107 + 108 + return vgic_its_inject_msi(kvm, &msi); 109 + } 110 + 111 + int kvm_vgic_setup_default_irq_routing(struct kvm *kvm) 112 + { 113 + struct kvm_irq_routing_entry *entries; 114 + struct vgic_dist *dist = &kvm->arch.vgic; 115 + u32 nr = dist->nr_spis; 116 + int i, ret; 117 + 118 + entries = kcalloc(nr, sizeof(struct kvm_kernel_irq_routing_entry), 119 + GFP_KERNEL); 120 + if (!entries) 121 + return -ENOMEM; 122 + 123 + for (i = 0; i < nr; i++) { 124 + entries[i].gsi = i; 125 + entries[i].type = KVM_IRQ_ROUTING_IRQCHIP; 126 + entries[i].u.irqchip.irqchip = 0; 127 + entries[i].u.irqchip.pin = i; 128 + } 129 + ret = kvm_set_irq_routing(kvm, entries, nr, 0); 130 + kfree(entries); 131 + return ret; 52 132 }
-7
virt/kvm/arm/vgic/vgic.c
··· 711 711 return map_is_active; 712 712 } 713 713 714 - int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi) 715 - { 716 - if (vgic_has_its(kvm)) 717 - return vgic_its_inject_msi(kvm, msi); 718 - else 719 - return -ENODEV; 720 - }
+18 -10
virt/kvm/irqchip.c
··· 62 62 { 63 63 struct kvm_kernel_irq_routing_entry route; 64 64 65 - if (!irqchip_in_kernel(kvm) || msi->flags != 0) 65 + if (!irqchip_in_kernel(kvm) || (msi->flags & ~KVM_MSI_VALID_DEVID)) 66 66 return -EINVAL; 67 67 68 68 route.msi.address_lo = msi->address_lo; 69 69 route.msi.address_hi = msi->address_hi; 70 70 route.msi.data = msi->data; 71 + route.msi.flags = msi->flags; 72 + route.msi.devid = msi->devid; 71 73 72 74 return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, false); 73 75 } ··· 179 177 unsigned flags) 180 178 { 181 179 struct kvm_irq_routing_table *new, *old; 180 + struct kvm_kernel_irq_routing_entry *e; 182 181 u32 i, j, nr_rt_entries = 0; 183 182 int r; 184 183 ··· 203 200 new->chip[i][j] = -1; 204 201 205 202 for (i = 0; i < nr; ++i) { 206 - struct kvm_kernel_irq_routing_entry *e; 207 - 208 203 r = -ENOMEM; 209 204 e = kzalloc(sizeof(*e), GFP_KERNEL); 210 205 if (!e) 211 206 goto out; 212 207 213 208 r = -EINVAL; 214 - if (ue->flags) { 215 - kfree(e); 216 - goto out; 209 + switch (ue->type) { 210 + case KVM_IRQ_ROUTING_MSI: 211 + if (ue->flags & ~KVM_MSI_VALID_DEVID) 212 + goto free_entry; 213 + break; 214 + default: 215 + if (ue->flags) 216 + goto free_entry; 217 + break; 217 218 } 218 219 r = setup_routing_entry(kvm, new, e, ue); 219 - if (r) { 220 - kfree(e); 221 - goto out; 222 - } 220 + if (r) 221 + goto free_entry; 223 222 ++ue; 224 223 } 225 224 ··· 238 233 239 234 new = old; 240 235 r = 0; 236 + goto out; 241 237 238 + free_entry: 239 + kfree(e); 242 240 out: 243 241 free_irq_routing_table(new); 244 242