IOMMU: Enable interrupt remapping for Intel IOMMU
v6 cover IOMMU: Enable interrupt remapping for Intel IOMMU
Hi, all,
This is v6 for Intel IOMMU IR support. This series introduced quite a few new patches based on v5. Sorry for that (Yes, Jan is contributing to it as well, though most of which are really good ideas for me :). Hopefully we can get its convergence in this version.
To make the review easier, I tried to keep all the existing patches and indexes (also, this is easier for me too to do the modifications, and logically I feel this make more sense and clean, please let me know if I am wrong). Patches 1-18 are v5 patches, and patches 19-26 are newly added patches.
All the new patches may need more review, many of them are outside Intel IOMMU scope, and touching other part of codes, which I am still not very sure about.
Testing is only covering basic smoke test for the following matrix:
- IR enabled/disable
- kernel irqchip off/split
- network device: tap with/without vhost, e1000
Here's the change log. Please review. Thanks,
v6 changes:
- patch 10: use write_with_attrs() rather than write(), preparing for SID verification [Jan]
- patch 17-18: add r-b line from Radim [Radim]
- new patch 19: put together Jan's EIM patch [Jan]
- new patch 20: add SID validation process
- new patch 21-22: introduce X86IOMMU class, which is the parent of IntelIOMMU class. Patch 21 only introduce the class and did nothing, patch 22 cleaned up all the vtd_*() hooks into x86 ones. This is only a start. In the future, we can abstract more things into X86IOMMU class, like iotlb, address spaces mgmt, etc. [Jan]
- new patch 23-25: this is to do IEC notify to all irqfd consumers like vhost/vfio. patch 23 changed interface for kvm_irqchip_add_msi_route(), provide vector info rather than a raw MSI message. Patch 24 added new hooks to do arch-specific notification on addition/deletion of msi routes. Patch 25 is x86 specific, which added one more IEC notifier for msi routes. [Jan]
- new patch 26: this is to partially solve the issue that Jan has encountered (1 sec delay when invalidating IR cache).
v5 changes:
- patch 10: add vector checking for IOAPIC interrupts (this may help debug in the future, will only generate warning if specify IOMMU_DEBUG)
- patch 13: replace error_report() with a trace. [Jan]
- patch 14: rename parameter "intr" to "intremap", to be aligned with kernel parameter [Jan]
- patch 15: fix comments for vtd_iec_notify_fn
- patch 17 & 18 (added): fix issue when IR enabled with devices using level-triggered interrupts, like e1000. Adding it to the end of series, since this issue never happen without IR.
Patch 17 adds read-only check for IOAPIC entries. Patch 18 clears remote IRR bit when entry configured as edge-triggered.
v4 changes (all patch number corresponds to v3):
- add one patch at the start of v3 series: I missed to send the first patch in v3. adding it in. [Jan]
- patch 9: add support for compatible mode (no reason not to support it, if not, we will get some warnings when using split irqchip)
- patch 11: further simplify ioapic_update_kvm_routes() using the helper function.
- patch 12: tweak on kvm_arch_fixup_msi_route() rather than ioapic_update_kvm_routes() only. [Radim]
- add patch 15: introduce IEC (Interrupt Entry Cache) invalidation notifier list. We can register to this list if we want to be notified when we got IR invalidation requests [Radim]
- add patch 16: let IOAPIC the first consumer for the above IEC notifier list. [Radim]
- several other trivial fixes (like moving some defines from .c to .h, moving several lines of changes from one patch to another to make it make more sense, etc.)
v3 changes (all patch numbers corresponds to v2):
- patch 1 (-> v3 patch 13)
- move to the end of series [Alex]
- patch 10 (dropped)
- drop this one, since re-worked on IOAPIC support, so we do not need this any more.
- patch 12 (-> v3 patch 10)
- leverage MSI path for IOAPIC IR [Jan]
- patch 13 (v3 -> patch 9)
- remove vtd_interrupt_remap_msi() declaration by reordering the functions [mst]
- vtd_generate_msi_message(): init msg using {}, remove FIXME
[mst]
- new patches
- v3 patch 11: introduce ioapic_entry_parse() helper function
- v3 patch 12: add support for kernel-irqchip=split. This needs more reviews, logically this should enable lots of things:
splitted irqchip, irqfd, vhost, and irqfd support for passthrough devices (not tested). Please refer to the patch for more information.
v2 changes:
- patch 1
- rename "int_remap" to "intr" in several places [Marcel]
- remove "Intel" specific words in desc or commit message, prepare itself with further AMD support [Marcel]
- avoid using object_property_get_bool() [Marcel]
- patch 5
- use PCI bus number 0xff rather than 0xf0 for the IOAPIC scope definition. (please let me know if anyone knows how I can avoid user using PCI bus number 0xff... TIA)
- patch 11
- fix comments [Marcel]
- all
- remove intr_supported variable [Marcel]
This patchset provide very basic functionalities for interrupt remapping (IR) support of the emulated Intel IOMMU device.
By default, IR is disabled to be better compatible with current QEMU. To enable IR, we can using the following command to boot a IR-supported VM with virtio-net device with vhost (still do not support kvm-ioapic, so we need to specify kernel-irqchip={split|off} here):
$ qemu-system-x86_64 -M q35,iommu=on,intr=on,kernel-irqchip=split \
-enable-kvm -m 1024 \
-netdev tap,id=net0,vhost=on \
-device virtio-net-pci,netdev=user.0 \
-monitor telnet::3333,server,nowait \
/var/lib/libvirt/images/vm1.qcow2When guest boots, we can verify whether IR enabled by grepping the dmesg like:
Feb 19 11:21:23 localhost.localdomain kernel: DMAR-IR: IOAPIC id 0 under DRHD base 0xfed90000 IOMMU 0 Feb 19 11:21:23 localhost.localdomain kernel: DMAR-IR: Enabled IRQ remapping in x2apic mode
Currently supported:
- Emulated/Splitted irqchip
- Generic PCI Devices
- vhost devices
- pass through device support? Not tested, but suppose it should work.
- IEC (Interrupt Entry Cache) cache invalidation notification
- EIM (from Jan)
- IRTE Source-id validation
TODO List:
- explicit IEC invalidation (currently, we do update without checking. Also, we can process QI invalidation in bulk, as Jan suggested)
- IR fault reporting
- migration support (for IOMMU as general?)
- more?
Jan Kiszka (1):
intel_iommu: Add support for Extended Interrupt Mode
Peter Xu (25):
acpi: enable INTR for DMAR report structure
intel_iommu: allow queued invalidation for IR
intel_iommu: set IR bit for ECAP register
acpi: add DMAR scope definition for root IOAPIC
intel_iommu: define interrupt remap table addr register
intel_iommu: handle interrupt remap enable
intel_iommu: define several structs for IOMMU IR
intel_iommu: provide helper function vtd_get_iommu
intel_iommu: add IR translation faults defines
intel_iommu: Add support for PCI MSI remap
q35: ioapic: add support for emulated IOAPIC IR
ioapic: introduce ioapic_entry_parse() helper
intel_iommu: add support for split irqchip
q35: add "intremap" parameter to enable IR
intel_iommu: introduce IEC notifiers
ioapic: register VT-d IEC invalidate notifier
ioapic: keep RO bits for IOAPIC entry
ioapic: clear remote irr bit for edge-triggered interrupts
intel_iommu: add SID validation for IR
x86-iommu: introduce parent class
x86-iommu: replace existing VT-d hooks into X86 ones
kvm-irqchip: simplify kvm_irqchip_add_msi_route
kvm-irqchip: i386: add hook for add/remove virq
kvm-irqchip: x86: add msi route notify fn
kvm-irqchip: do explicit commit when update irq
hw/core/machine.c | 22 ++ hw/i386/Makefile.objs | 2 +- hw/i386/acpi-build.c | 38 ++-- hw/i386/intel_iommu.c | 429 +++++++++++++++++++++++++++++++++++++- hw/i386/intel_iommu_internal.h | 49 ++++- hw/i386/kvm/pci-assign.c | 10 +- hw/i386/pc.c | 3 + hw/i386/x86-iommu.c | 91 ++++++++ hw/intc/ioapic.c | 158 ++++++++++---- hw/misc/ivshmem.c | 4 +- hw/pci-host/q35.c | 17 +- hw/pci/pci.c | 15 ++ hw/vfio/pci.c | 12 +- hw/virtio/virtio-pci.c | 10 +- include/hw/acpi/acpi-defs.h | 15 ++ include/hw/boards.h | 1 + include/hw/i386/apic-msidef.h | 1 + include/hw/i386/intel_iommu.h | 147 ++++++++++++- include/hw/i386/ioapic_internal.h | 6 + include/hw/i386/pc.h | 4 + include/hw/i386/x86-iommu.h | 110 ++++++++++ include/hw/pci-host/q35.h | 9 + include/hw/pci/pci.h | 2 + include/sysemu/kvm.h | 21 +- kvm-all.c | 14 +- kvm-stub.c | 6 +- target-arm/kvm.c | 11 + target-i386/kvm.c | 98 ++++++++- target-mips/kvm.c | 11 + target-ppc/kvm.c | 11 + target-s390x/kvm.c | 11 + trace-events | 6 + 32 files changed, 1232 insertions(+), 112 deletions(-) create mode 100644 hw/i386/x86-iommu.c create mode 100644 include/hw/i386/x86-iommu.h
Signature · 1 line
2.4.11
PATCH v6 1/26 acpi: enable INTR for DMAR report structure
Introduce iommu_intr in MachineState to show whether IOMMU IR is enabled. By default, IR is off.
In ACPI DMA remapping report structure, enable INTR flag when specified.
--- hw/core/machine.c | 2 ++ hw/i386/acpi-build.c | 12 +++++++++--- include/hw/boards.h | 1 + include/hw/i386/intel_iommu.h | 2 ++ 4 files changed, 14 insertions(+), 3 deletions(-)
hw/core/machine.c +2 -0
@@ -382,6 +382,8 @@ static void machine_initfn(Object *obj) ms->kvm_shadow_mem =-1 ; ms->dump_guest_core = true; ms->mem_merge = true; +/* Disable interrupt remapping by default. */ + ms->iommu_intr = false;object_property_add_str (obj,"accel" , machine_get_accel, machine_set_accel,NULL );
hw/i386/acpi-build.c +9 -3
@@ -2575,16 +2575,22 @@ build_mcfg_q35(GArray *table_data, GArray *linker, AcpiMcfgInfo *info) }static void -build_dmar_q35 (GArray *table_data,GArray *linker) +build_dmar_q35 (MachineState *ms,GArray *table_data,GArray *linker) {int dmar_start = table_data->len;AcpiTableDmar *dmar;AcpiDmarHardwareUnit *drhd; +uint8_t dmar_flags =0 ; + +if (ms->iommu_intr) { +/* enable INTR for the IOMMU device */ + dmar_flags |=DMAR_REPORT_F_INTR ; + } dmar =acpi_data_push (table_data,sizeof (*dmar)); dmar->host_address_width =VTD_HOST_ADDRESS_WIDTH -1 ; - dmar->flags =0 ;/* No intr_remap for now */ + dmar->flags = dmar_flags;/* DMAR Remapping Hardware Unit Definition structure */ drhd =acpi_data_push (table_data,sizeof (*drhd));
@@ -2745,7 +2751,7 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine) }if (acpi_has_iommu ()) {acpi_add_table (table_offsets, tables_blob); -build_dmar_q35 (tables_blob, tables->linker); +build_dmar_q35 (MACHINE (pcms), tables_blob, tables->linker); }if (pcms->acpi_nvdimm_state.is_enabled) {nvdimm_build_acpi (table_offsets, tables_blob, tables->linker);
include/hw/boards.h +1 -0
@@ -152,6 +152,7 @@ struct MachineState {bool igd_gfx_passthru;char *firmware;bool iommu; +bool iommu_intr;bool suppress_vmdesc;bool enforce_config_section;
include/hw/i386/intel_iommu.h +2 -0
@@ -44,6 +44,8 @@#define VTD_HOST_ADDRESS_WIDTH 39#define VTD_HAW_MASK ((1ULL << VTD_HOST_ADDRESS_WIDTH) - 1) +#define DMAR_REPORT_F_INTR (1) +typedef struct VTDContextEntry VTDContextEntry ;typedef struct VTDContextCacheEntry VTDContextCacheEntry ;typedef struct IntelIOMMUState IntelIOMMUState ;
Signature · 1 line
2.4.11
PATCH v6 2/26 intel_iommu: allow queued invalidation for IR
In reply to Peter XuQueued invalidation is required for IR. This patch add basic support for interrupt cache invalidate requests. Since we currently have no IR cache implemented yet, we can just skip all interrupt cache invalidation requests for now.
--- hw/i386/intel_iommu.c | 9 +++++++++ hw/i386/intel_iommu_internal.h | 2 ++ 2 files changed, 11 insertions(+)
hw/i386/intel_iommu.c +9 -0
@@ -1400,6 +1400,15 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s) }break ; +case VTD_INV_DESC_IEC : +VTD_DPRINTF (INV ,"Interrupt Entry Cache Invalidation " +"not implemented yet" ); +/* +* Since currently we do not cache interrupt entries, we can +* just mark this descriptor as "good" and move on. +*/ +break ; +default : VTD_DPRINTF(GENERAL, "error: unkonw Invalidation Descriptor type " "hi 0x%"PRIx64 " lo 0x%"PRIx64 " type %"PRIu8,
hw/i386/intel_iommu_internal.h +2 -0
@@ -286,6 +286,8 @@ typedef struct VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_TYPE 0xf#define VTD_INV_DESC_CC 0x1/* Context-cache Invalidate Desc */ #define VTD_INV_DESC_IOTLB 0x2 +#define VTD_INV_DESC_IEC 0x4/* Interrupt Entry Cache +Invalidate Descriptor */ #define VTD_INV_DESC_WAIT 0x5/* Invalidation Wait Descriptor */ #define VTD_INV_DESC_NONE 0/* Not an Invalidate Descriptor */
Signature · 1 line
2.4.11
PATCH v6 3/26 intel_iommu: set IR bit for ECAP register
In reply to Peter XuEnable IR in IOMMU Extended Capability register.
--- hw/i386/intel_iommu.c | 7 +++++++ hw/i386/intel_iommu_internal.h | 2 ++ 2 files changed, 9 insertions(+)
hw/i386/intel_iommu.c +7 -0
@@ -24,6 +24,7 @@#include "exec/address-spaces.h" #include "intel_iommu_internal.h" #include "hw/pci/pci.h" +#include "hw/boards.h" /*#define DEBUG_INTEL_IOMMU*/ #ifdef DEBUG_INTEL_IOMMU
@@ -1941,6 +1942,8 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)*/ static void vtd_init (IntelIOMMUState *s) { +MachineState *ms =MACHINE (qdev_get_machine ()); +memset (s->csr,0 ,DMAR_REG_SIZE );memset (s->wmask,0 ,DMAR_REG_SIZE );memset (s->w1cmask,0 ,DMAR_REG_SIZE );
@@ -1961,6 +1964,10 @@ static void vtd_init(IntelIOMMUState *s)VTD_CAP_SAGAW |VTD_CAP_MAMV |VTD_CAP_PSI |VTD_CAP_SLLPS ; s->ecap =VTD_ECAP_QI |VTD_ECAP_IRO ; +if (ms->iommu_intr) { + s->ecap |=VTD_ECAP_IR ; + } +vtd_reset_context_cache (s);vtd_reset_iotlb (s);
hw/i386/intel_iommu_internal.h +2 -0
@@ -176,6 +176,8 @@/* (offset >> 4) << 8 */ #define VTD_ECAP_IRO (DMAR_IOTLB_REG_OFFSET << 4)#define VTD_ECAP_QI (1ULL << 1) +/* Interrupt Remapping support */ +#define VTD_ECAP_IR (1ULL << 3)/* CAP_REG */ /* (offset >> 4) << 24 */
Signature · 1 line
2.4.11
PATCH v6 4/26 acpi: add DMAR scope definition for root IOAPIC
In reply to Peter XuTo enable interrupt remapping for intel IOMMU device, each IOAPIC device in the system reported via ACPI MADT must be explicitly enumerated under one specific remapping hardware unit. This patch adds the root-complex IOAPIC into the default DMAR device.
Please refer to VT-d spec 8.3.1.1 for more information.
--- hw/i386/acpi-build.c | 17 +++++++++++++++-- include/hw/acpi/acpi-defs.h | 15 +++++++++++++++ include/hw/pci-host/q35.h | 9 +++++++++ 3 files changed, 39 insertions(+), 2 deletions(-)
hw/i386/acpi-build.c +15 -2
@@ -77,6 +77,9 @@#define ACPI_BUILD_DPRINTF (fmt, ...)#endif +/* Default IOAPIC ID */ +#define ACPI_BUILD_IOAPIC_ID 0x0 + typedef struct AcpiMcfgInfo { uint64_t mcfg_base; uint32_t mcfg_size;
@@ -375,7 +378,6 @@ build_madt(GArray *table_data, GArray *linker, PCMachineState *pcms) io_apic =acpi_data_push (table_data,sizeof *io_apic); io_apic->type =ACPI_APIC_IO ; io_apic->length =sizeof (*io_apic); -#define ACPI_BUILD_IOAPIC_ID 0x0 io_apic->io_apic_id =ACPI_BUILD_IOAPIC_ID ; io_apic->address =cpu_to_le32 (IO_APIC_DEFAULT_ADDRESS ); io_apic->interrupt =cpu_to_le32 (0 );
@@ -2582,6 +2584,9 @@ build_dmar_q35(MachineState *ms, GArray *table_data, GArray *linker)AcpiTableDmar *dmar;AcpiDmarHardwareUnit *drhd;uint8_t dmar_flags =0 ; +AcpiDmarDeviceScope *scope =NULL ; +/* Root complex IOAPIC use one path[0] only */ +uint16_t scope_size =sizeof (*scope) +sizeof (uint16_t );if (ms->iommu_intr) {/* enable INTR for the IOMMU device */
@@ -2595,11 +2600,19 @@ build_dmar_q35(MachineState *ms, GArray *table_data, GArray *linker)/* DMAR Remapping Hardware Unit Definition structure */ drhd =acpi_data_push (table_data,sizeof (*drhd)); drhd->type =cpu_to_le16 (ACPI_DMAR_TYPE_HARDWARE_UNIT ); - drhd->length =cpu_to_le16 (sizeof (*drhd));/* No device scope now */ + drhd->length =cpu_to_le16 (sizeof (*drhd) + scope_size); drhd->flags =ACPI_DMAR_INCLUDE_PCI_ALL ; drhd->pci_segment =cpu_to_le16 (0 ); drhd->address =cpu_to_le64 (Q35_HOST_BRIDGE_IOMMU_ADDR ); +/* Scope definition for the root-complex IOAPIC */ + scope =acpi_data_push (table_data, scope_size); + scope->entry_type =cpu_to_le16 (ACPI_DMAR_DEV_SCOPE_TYPE_IOAPIC ); + scope->length = scope_size; + scope->enumeration_id =cpu_to_le16 (ACPI_BUILD_IOAPIC_ID ); + scope->bus =cpu_to_le16 (Q35_PSEUDO_BUS_PLATFORM ); + scope->path[0 ] =cpu_to_le16 (Q35_PSEUDO_DEVFN_IOAPIC ); +build_header (linker, table_data, (void *)(table_data->data + dmar_start),"DMAR" , table_data->len - dmar_start,1 ,NULL ,NULL ); }
include/hw/acpi/acpi-defs.h +15 -0
@@ -556,6 +556,20 @@ enum {/* * Sub-structures for DMAR */ + +#define ACPI_DMAR_DEV_SCOPE_TYPE_IOAPIC (0x03) + +/* Device scope structure for DRHD. */ +struct AcpiDmarDeviceScope { +uint8_t entry_type; +uint8_t length; +uint16_t reserved; +uint8_t enumeration_id; +uint8_t bus; +uint16_t path[0 ];/* list of dev:func pairs */ +}QEMU_PACKED ; +typedef struct AcpiDmarDeviceScope AcpiDmarDeviceScope ; +/* Type 0: Hardware Unit Definition */ struct AcpiDmarHardwareUnit {uint16_t type;
@@ -564,6 +578,7 @@ struct AcpiDmarHardwareUnit {uint8_t reserved;uint16_t pci_segment;/* The PCI Segment associated with this unit */ uint64_t address;/* Base address of remapping hardware register-set */ +AcpiDmarDeviceScope scope[0 ]; }QEMU_PACKED ;typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit ;
include/hw/pci-host/q35.h +9 -0
@@ -175,4 +175,13 @@ typedef struct Q35PCIHost {uint64_t mch_mcfg_base (void ); +/* +* Arbitary but unique BNF number for IOAPIC device. This is only +* used when interrupt remapping is enabled. +* +* TODO: make sure there would have no conflict with real PCI bus +*/ +#define Q35_PSEUDO_BUS_PLATFORM (0xff) +#define Q35_PSEUDO_DEVFN_IOAPIC (0x00) +#endif /* HW_Q35_H */
Signature · 1 line
2.4.11
PATCH v6 5/26 intel_iommu: define interrupt remap table addr register
In reply to Peter XuDefined Interrupt Remap Table Address register to store IR table pointer. Also, do proper handling on global command register writes to store table pointer and its size.
One more debug flag "DEBUG_IR" is added for interrupt remapping.
--- hw/i386/intel_iommu.c | 52 +++++++++++++++++++++++++++++++++++++++++- hw/i386/intel_iommu_internal.h | 4 ++++ include/hw/i386/intel_iommu.h | 5 ++++ 3 files changed, 60 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +51 -1
@@ -30,7 +30,7 @@#ifdef DEBUG_INTEL_IOMMU enum {DEBUG_GENERAL ,DEBUG_CSR ,DEBUG_INV ,DEBUG_MMU ,DEBUG_FLOG , -DEBUG_CACHE , +DEBUG_CACHE ,DEBUG_IR , };#define VTD_DBGBIT (x) (1 << DEBUG_##x)static int vtd_dbgflags =VTD_DBGBIT (GENERAL ) |VTD_DBGBIT (CSR );
@@ -900,6 +900,19 @@ static void vtd_root_table_setup(IntelIOMMUState *s) (s->root_extended ? "(extended)" : "")); } +static void vtd_interrupt_remap_table_setup (IntelIOMMUState *s) +{ +uint64_t value =0 ; + value =vtd_get_quad_raw (s,DMAR_IRTA_REG ); + s->intr_size =1UL << ((value &VTD_IRTA_SIZE_MASK ) +1 ); + s->intr_root = value &VTD_IRTA_ADDR_MASK ; + +/* TODO: invalidate interrupt entry cache */ + +VTD_DPRINTF (CSR ,"int remap table addr 0x%" PRIx64" size %" PRIu32, + s->intr_root, s->intr_size); +} +static void vtd_context_global_invalidate (IntelIOMMUState *s) { s->context_cache_gen++;
@@ -1138,6 +1151,16 @@ static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)vtd_set_clear_mask_long (s,DMAR_GSTS_REG ,0 ,VTD_GSTS_RTPS ); } +/* Set Interrupt Remap Table Pointer */ +static void vtd_handle_gcmd_sirtp (IntelIOMMUState *s) +{ +VTD_DPRINTF (CSR ,"set Interrupt Remap Table Pointer" ); + +vtd_interrupt_remap_table_setup (s); +/* Ok - report back to driver */ +vtd_set_clear_mask_long (s,DMAR_GSTS_REG ,0 ,VTD_GSTS_IRTPS ); +} +/* Handle Translation Enable/Disable */ static void vtd_handle_gcmd_te (IntelIOMMUState *s,bool en) {
@@ -1177,6 +1200,10 @@ static void vtd_handle_gcmd_write(IntelIOMMUState *s)/* Queued Invalidation Enable */ vtd_handle_gcmd_qie (s, val &VTD_GCMD_QIE ); } +if (val &VTD_GCMD_SIRTP ) { +/* Set/update the interrupt remapping root-table pointer */ +vtd_handle_gcmd_sirtp (s); + } }/* Handle write to Context Command Register */
@@ -1838,6 +1865,23 @@ static void vtd_mem_write(void *opaque, hwaddr addr,vtd_update_fsts_ppf (s);break ; +case DMAR_IRTA_REG : +VTD_DPRINTF (IR ,"DMAR_IRTA_REG write addr 0x%" PRIx64 +", size %d, val 0x%" PRIx64, addr, size, val); +if (size ==4 ) { +vtd_set_long (s, addr, val); + }else { +vtd_set_quad (s, addr, val); + } +break ; + +case DMAR_IRTA_REG_HI : +VTD_DPRINTF (IR ,"DMAR_IRTA_REG_HI write addr 0x%" PRIx64 +", size %d, val 0x%" PRIx64, addr, size, val); +assert (size ==4 ); +vtd_set_long (s, addr, val); +break ; +default :VTD_DPRINTF (GENERAL ,"error: unhandled reg write addr 0x%" PRIx64", size %d, val 0x%" PRIx64, addr, size, val);
@@ -2017,6 +2061,12 @@ static void vtd_init(IntelIOMMUState *s)/* Fault Recording Registers, 128-bit */ vtd_define_quad (s,DMAR_FRCD_REG_0_0 ,0 ,0 ,0 );vtd_define_quad (s,DMAR_FRCD_REG_0_2 ,0 ,0 ,0x8000000000000000ULL ); + +/* +* Interrupt remapping registers, not support extended interrupt +* mode for now. +*/ +vtd_define_quad (s,DMAR_IRTA_REG ,0 ,0xfffffffffffff00fULL ,0 ); }/* Should not reset address_spaces when reset because devices will still use
hw/i386/intel_iommu_internal.h +4 -0
@@ -172,6 +172,10 @@#define VTD_RTADDR_RTT (1ULL << 11)#define VTD_RTADDR_ADDR_MASK (VTD_HAW_MASK ^ 0xfffULL) +/* IRTA_REG */ +#define VTD_IRTA_ADDR_MASK (VTD_HAW_MASK ^ 0xfffULL) +#define VTD_IRTA_SIZE_MASK (0xfULL) +/* ECAP_REG */ /* (offset >> 4) << 8 */ #define VTD_ECAP_IRO (DMAR_IOTLB_REG_OFFSET << 4)
include/hw/i386/intel_iommu.h +5 -0
@@ -125,6 +125,11 @@ struct IntelIOMMUState {MemoryRegionIOMMUOps iommu_ops;GHashTable *vtd_as_by_busptr;/* VTDBus objects indexed by PCIBus* reference */ VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX ];/* VTDBus objects indexed by bus number */ + +/* interrupt remapping */ +bool intr_enabled;/* Whether guest enabled IR */ +dma_addr_t intr_root;/* Interrupt remapping table pointer */ +uint32_t intr_size;/* Number of IR table entries */ };/* Find the VTD Address space associated with the given bus pointer,
Signature · 1 line
2.4.11
PATCH v6 6/26 intel_iommu: handle interrupt remap enable
In reply to Peter XuHandle writting to IRE bit in global command register.
--- hw/i386/intel_iommu.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
hw/i386/intel_iommu.c +20 -0
@@ -1180,6 +1180,22 @@ static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en) } } +/* Handle Interrupt Remap Enable/Disable */ +static void vtd_handle_gcmd_ire (IntelIOMMUState *s,bool en) +{ +VTD_DPRINTF (CSR ,"Interrupt Remap Enable %s" , (en ?"on" :"off" )); + +if (en) { + s->intr_enabled = true; +/* Ok - report back to driver */ +vtd_set_clear_mask_long (s,DMAR_GSTS_REG ,0 ,VTD_GSTS_IRES ); + }else { + s->intr_enabled = false; +/* Ok - report back to driver */ +vtd_set_clear_mask_long (s,DMAR_GSTS_REG ,VTD_GSTS_IRES ,0 ); + } +} +/* Handle write to Global Command Register */ static void vtd_handle_gcmd_write (IntelIOMMUState *s) {
@@ -1204,6 +1220,10 @@ static void vtd_handle_gcmd_write(IntelIOMMUState *s)/* Set/update the interrupt remapping root-table pointer */ vtd_handle_gcmd_sirtp (s); } +if (changed &VTD_GCMD_IRE ) { +/* Interrupt remap enable/disable */ +vtd_handle_gcmd_ire (s, val &VTD_GCMD_IRE ); + } }/* Handle write to Context Command Register */
Signature · 1 line
2.4.11
PATCH v6 7/26 intel_iommu: define several structs for IOMMU IR
In reply to Peter XuSeveral data structs are defined to better support the rest of the
patches: IRTE to parse remapping table entries, and IOAPIC/MSI related structure bits to parse interrupt entries to be filled in by guest kernel.
--- include/hw/i386/intel_iommu.h | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+)
include/hw/i386/intel_iommu.h +60 -0
@@ -52,6 +52,9 @@ typedef struct IntelIOMMUState IntelIOMMUState;typedef struct VTDAddressSpace VTDAddressSpace ;typedef struct VTDIOTLBEntry VTDIOTLBEntry ;typedef struct VTDBus VTDBus ; +typedef union VTD_IRTE VTD_IRTE ; +typedef union VTD_IR_IOAPICEntry VTD_IR_IOAPICEntry ; +typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress ;/* Context-Entry */ struct VTDContextEntry {
@@ -90,6 +93,63 @@ struct VTDIOTLBEntry {bool write_flags; }; +/* Interrupt Remapping Table Entry Definition */ +union VTD_IRTE { +struct { +uint8_t present:1 ;/* Whether entry present/available */ +uint8_t fault_disable:1 ;/* Fault Processing Disable */ +uint8_t dest_mode:1 ;/* Destination Mode */ +uint8_t redir_hint:1 ;/* Redirection Hint */ +uint8_t trigger_mode:1 ;/* Trigger Mode */ +uint8_t delivery_mode:3 ;/* Delivery Mode */ +uint8_t __avail:4 ;/* Available spaces for software */ +uint8_t __reserved_0:3 ;/* Reserved 0 */ +uint8_t irte_mode:1 ;/* IRTE Mode */ +uint8_t vector:8 ;/* Interrupt Vector */ +uint8_t __reserved_1:8 ;/* Reserved 1 */ +uint32_t dest_id:32 ;/* Destination ID */ +uint16_t source_id:16 ;/* Source-ID */ +uint8_t sid_q:2 ;/* Source-ID Qualifier */ +uint8_t sid_vtype:2 ;/* Source-ID Validation Type */ +uint64_t __reserved_2:44 ;/* Reserved 2 */ + } QEMU_PACKED; +uint64_t data[2 ]; +}; + +/* Programming format for IOAPIC table entries */ +union VTD_IR_IOAPICEntry { +struct { +uint8_t vector:8 ;/* Vector */ +uint8_t __zeros:3 ;/* Reserved (all zero) */ +uint8_t index_h:1 ;/* Interrupt Index bit 15 */ +uint8_t status:1 ;/* Deliver Status */ +uint8_t polarity:1 ;/* Interrupt Polarity */ +uint8_t remote_irr:1 ;/* Remote IRR */ +uint8_t trigger_mode:1 ;/* Trigger Mode */ +uint8_t mask:1 ;/* Mask */ +uint32_t __reserved:31 ;/* Reserved (should all zero) */ +uint8_t int_mode:1 ;/* Interrupt Format */ +uint16_t index_l:15 ;/* Interrupt Index bits 14-0 */ + } QEMU_PACKED; +uint64_t data; +}; + +/* Programming format for MSI/MSI-X addresses */ +union VTD_IR_MSIAddress { +struct { +uint8_t __not_care:2 ; +uint8_t index_h:1 ;/* Interrupt index bit 15 */ +uint8_t sub_valid:1 ;/* SHV: Sub-Handle Valid bit */ +uint8_t int_mode:1 ;/* Interrupt format */ +uint16_t index_l:15 ;/* Interrupt index bit 14-0 */ +uint16_t __head:12 ;/* Should always be: 0x0fee */ + } QEMU_PACKED; +uint32_t data; +}; + +/* When IR is enabled, all MSI/MSI-X data bits should be zero */ +#define VTD_IR_MSI_DATA (0) +/* The iommu (DMAR) device state struct */ struct IntelIOMMUState {SysBusDevice busdev;
Signature · 1 line
2.4.11
PATCH v6 8/26 intel_iommu: provide helper function vtd_get_iommu
In reply to Peter XuMoves acpi_get_iommu() under VT-d to make it a public function.
--- hw/i386/acpi-build.c | 7 +------ hw/i386/intel_iommu.c | 13 +++++++++++++ include/hw/i386/intel_iommu.h | 2 ++ 3 files changed, 16 insertions(+), 6 deletions(-)
hw/i386/acpi-build.c +1 -6
@@ -2677,12 +2677,7 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)static bool acpi_has_iommu (void ) { -bool ambiguous; -Object *intel_iommu; - - intel_iommu =object_resolve_path_type ("" ,TYPE_INTEL_IOMMU_DEVICE , - &ambiguous); -return intel_iommu && !ambiguous; +return !!vtd_iommu_get (); } static
hw/i386/intel_iommu.c +13 -0
@@ -2001,6 +2001,19 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +IntelIOMMUState *vtd_iommu_get (void ) +{ +bool ambiguous = false; +Object *intel_iommu =NULL ; + + intel_iommu =object_resolve_path_type ("" ,TYPE_INTEL_IOMMU_DEVICE , + &ambiguous); +if (ambiguous) + intel_iommu =NULL ; + +return (IntelIOMMUState *)intel_iommu; +} +/* Do the initialization. It will also be called when reset, so pay * attention when adding new initialization stuff. */
include/hw/i386/intel_iommu.h +2 -0
@@ -196,5 +196,7 @@ struct IntelIOMMUState {* create a new one if none exists */ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s,PCIBus *bus,int devfn); +/* Get default IOMMU object */ +IntelIOMMUState *vtd_iommu_get (void );#endif
Signature · 1 line
2.4.11
Re: [Qemu-devel] [PATCH v6 08/26] intel_iommu: provide helper function vtd_get_iommu
Quoted 15 lines · Peter Xu
Moves
acpi_get_iommu()under VT-d to make it a public function.Signed-off-by Peter Xu <[email protected]>--- hw/i386/acpi-build.c | 7 +------ hw/i386/intel_iommu.c | 13 +++++++++++++ include/hw/i386/intel_iommu.h | 2 ++ 3 files changed, 16 insertions(+), 6 deletions(-)hw/i386/acpi-build.c +0
-0@@ -2677,12 +2677,7 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
hw/i386/acpi-build.c static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg) · line 2681
Quoted 10 lines
hw/i386/acpi-build.c +1
-6static bool acpi_has_iommu (void ) { -bool ambiguous; -Object *intel_iommu; - - intel_iommu =object_resolve_path_type ("" ,TYPE_INTEL_IOMMU_DEVICE , - &ambiguous); -return intel_iommu && !ambiguous; +return !!vtd_iommu_get (); }
This is not consistent with what we have in the AMD IOMMU patches but I guess this could be easily fixed.
Quoted 41 lines
hw/i386/acpi-build.c +0
-0statichw/i386/intel_iommu.c +13
-0@@ -2001,6 +2001,19 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +IntelIOMMUState *vtd_iommu_get (void ) +{ +bool ambiguous = false; +Object *intel_iommu =NULL ; + + intel_iommu =object_resolve_path_type ("" ,TYPE_INTEL_IOMMU_DEVICE , + &ambiguous); +if (ambiguous) + intel_iommu =NULL ; + +return (IntelIOMMUState *)intel_iommu; +} +/* Do the initialization. It will also be called when reset, so pay * attention when adding new initialization stuff. */ include/hw/i386/intel_iommu.h +2
-1@@ -196,5 +196,7 @@ struct IntelIOMMUState {* create a new one if none exists */ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s,PCIBus *bus,int devfn); +/* Get default IOMMU object */ +IntelIOMMUState *vtd_iommu_get (void );#endif --2.4.11
Re: [Qemu-devel] [PATCH v6 08/26] intel_iommu: provide helper function vtd_get_iommu
Quoted 16 lines · David Kiarie
Quoted 15 lines · Peter Xu
Moves
acpi_get_iommu()under VT-d to make it a public function.Signed-off-by Peter Xu <[email protected]>--- hw/i386/acpi-build.c | 7 +------ hw/i386/intel_iommu.c | 13 +++++++++++++ include/hw/i386/intel_iommu.h | 2 ++ 3 files changed, 16 insertions(+), 6 deletions(-)hw/i386/acpi-build.c +0
-0@@ -2677,12 +2677,7 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
Quoted 13 lines
hw/i386/acpi-build.c static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg) · line 2681
Quoted 10 lines
hw/i386/acpi-build.c +1
-6static bool acpi_has_iommu (void ) { -bool ambiguous; -Object *intel_iommu; - - intel_iommu =object_resolve_path_type ("" ,TYPE_INTEL_IOMMU_DEVICE , - &ambiguous); -return intel_iommu && !ambiguous; +return !!vtd_iommu_get (); }This is not consistent with what we have in the AMD IOMMU patches but I guess this could be easily fixed.
Yes, I suppose there might be some conflict with corrent AMD IOMMU patchset. The vtd_iommu_get() is further replaced by x86_iommu_get_default() in patch 21/22. I'll possibly do a clean-up on these overlaps in next spin. Sorry for the inconvenience.
Would you please help have a look on patches 21/22 about introducing x86-iommu? To see whether that would be okay for further AMD integration.
TIA.
-- peterx
PATCH v6 9/26 intel_iommu: add IR translation faults defines
In reply to Peter XuAdding translation fault definitions for interrupt remapping. Please refer to VT-d spec section 7.1.
--- hw/i386/intel_iommu_internal.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
hw/i386/intel_iommu_internal.h +13 -0
@@ -271,6 +271,19 @@ typedef enum VTDFaultReason {* context-entry. */ VTD_FR_CONTEXT_ENTRY_TT , + +/* Interrupt remapping transition faults */ +VTD_FR_IR_REQ_RSVD =0x20 ,/* One or more IR request reserved +* fields set */ +VTD_FR_IR_INDEX_OVER =0x21 ,/* Index value greater than max */ +VTD_FR_IR_ENTRY_P =0x22 ,/* Present (P) not set in IRTE */ +VTD_FR_IR_ROOT_INVAL =0x23 ,/* IR Root table invalid */ +VTD_FR_IR_IRTE_RSVD =0x24 ,/* IRTE Rsvd field non-zero with +* Present flag set */ +VTD_FR_IR_REQ_COMPAT =0x25 ,/* Encountered compatible IR +* request while disabled */ +VTD_FR_IR_SID_ERR =0x26 ,/* Invalid Source-ID */ +/* This is not a normal fault reason. We use this to indicate some faults * that are not referenced by the VT-d specification. * Fault event with such reason should not be recorded.
Signature · 1 line
2.4.11
PATCH v6 10/26 intel_iommu: Add support for PCI MSI remap
In reply to Peter XuThis patch enables interrupt remapping for PCI devices.
To play the trick, one memory region "iommu_ir" is added as child region of the original iommu memory region, covering range 0xfeeXXXXX (which is the address range for APIC). All the writes to this range will be taken as MSI, and translation is carried out only when IR is enabled.
Idea suggested by Paolo Bonzini.
--- hw/i386/intel_iommu.c | 247 +++++++++++++++++++++++++++++++++++++++++ hw/i386/intel_iommu_internal.h | 2 + include/hw/i386/intel_iommu.h | 52 +++++++++ 3 files changed, 301 insertions(+)
hw/i386/intel_iommu.c +247 -0
@@ -1969,6 +1969,248 @@ static Property vtd_properties[] = { DEFINE_PROP_END_OF_LIST(), }; +/* Read IRTE entry with specific index */ +static int vtd_irte_get (IntelIOMMUState *iommu,uint16_t index, +VTD_IRTE *entry) +{ +dma_addr_t addr =0x00 ; + + addr = iommu->intr_root + index *sizeof (*entry); +if (dma_memory_read (&address_space_memory, addr, entry, +sizeof (*entry))) { +VTD_DPRINTF (GENERAL ,"error: fail to access IR root at 0x%" PRIx64 +" + %" PRIu16, iommu->intr_root, index); +return -VTD_FR_IR_ROOT_INVAL ; + } + +if (!entry->present) { +VTD_DPRINTF (GENERAL ,"error: present flag not set in IRTE" +" entry index %u value 0x%" PRIx64" 0x%" PRIx64, + index,le64_to_cpu (entry->data[1 ]), +le64_to_cpu (entry->data[0 ])); +return -VTD_FR_IR_ENTRY_P ; + } + +if (entry->__reserved_0 || entry->__reserved_1 || \ + entry->__reserved_2) { +VTD_DPRINTF (GENERAL ,"error: IRTE entry index %" PRIu16 +" reserved fields non-zero: 0x%" PRIx64" 0x%" PRIx64, + index,le64_to_cpu (entry->data[1 ]), +le64_to_cpu (entry->data[0 ])); +return -VTD_FR_IR_IRTE_RSVD ; + } + +/* +* TODO: Check Source-ID corresponds to SVT (Source Validation +* Type) bits +*/ + +return 0 ; +} + +/* Fetch IRQ information of specific IR index */ +static int vtd_remap_irq_get (IntelIOMMUState *iommu,uint16_t index,VTDIrq *irq) +{ +VTD_IRTE irte; +int ret =0 ; + +bzero (&irte,sizeof (irte)); + + ret =vtd_irte_get (iommu, index, &irte); +if (ret) { +return ret; + } + + irq->trigger_mode = irte.trigger_mode; + irq->vector = irte.vector; + irq->delivery_mode = irte.delivery_mode; +/* Not support EIM yet: please refer to vt-d 9.10 DST bits */ +#define VTD_IR_APIC_DEST_MASK (0xff00ULL) +#define VTD_IR_APIC_DEST_SHIFT (8) + irq->dest = (irte.dest_id &VTD_IR_APIC_DEST_MASK ) >> \ +VTD_IR_APIC_DEST_SHIFT ; + irq->dest_mode = irte.dest_mode; + irq->redir_hint = irte.redir_hint; + +VTD_DPRINTF (IR ,"remapping interrupt index %d: trig:%u,vec:%u," +"deliver:%u,dest:%u,dest_mode:%u" , index, + irq->trigger_mode, irq->vector, irq->delivery_mode, + irq->dest, irq->dest_mode); + +return 0 ; +} + +/* Generate one MSI message from VTDIrq info */ +static void vtd_generate_msi_message (VTDIrq *irq,MSIMessage *msg_out) +{ +VTD_MSIMessage msg = {}; + +/* Generate address bits */ + msg.dest_mode = irq->dest_mode; + msg.redir_hint = irq->redir_hint; + msg.dest = irq->dest; + msg.__addr_head =0xfee ; +/* Keep this from original MSI address bits */ + msg.__not_used = irq->msi_addr_last_bits; + +/* Generate data bits */ + msg.vector = irq->vector; + msg.delivery_mode = irq->delivery_mode; + msg.level =1 ; + msg.trigger_mode = irq->trigger_mode; + + msg_out->address = msg.msi_addr; + msg_out->data = msg.msi_data; +} + +/* Interrupt remapping for MSI/MSI-X entry */ +static int vtd_interrupt_remap_msi (IntelIOMMUState *iommu, +MSIMessage *origin, +MSIMessage *translated) +{ +int ret =0 ; +VTD_IR_MSIAddress addr; +uint16_t index =0 ; +VTDIrq irq = {0 }; + +assert (origin && translated); + +if (!iommu || !iommu->intr_enabled) { + goto do_not_translate; + } + +if (origin->address &VTD_MSI_ADDR_HI_MASK ) { +VTD_DPRINTF (GENERAL ,"error: MSI addr high 32 bits nonzero" +" during interrupt remapping: 0x%" PRIx32, + (uint32_t )((origin->address &VTD_MSI_ADDR_HI_MASK ) >> \ +VTD_MSI_ADDR_HI_SHIFT )); +return -VTD_FR_IR_REQ_RSVD ; + } + + addr.data = origin->address &VTD_MSI_ADDR_LO_MASK ; +if (addr.__head !=0xfee ) { +VTD_DPRINTF (GENERAL ,"error: MSI addr low 32 bits invalid: " +"0x%" PRIx32, addr.data); +return -VTD_FR_IR_REQ_RSVD ; + } + +/* This is compatible mode. */ +if (addr.int_mode !=VTD_IR_INT_FORMAT_REMAP ) { + goto do_not_translate; + } + + index = addr.index_h <<15 | addr.index_l; + + ret =vtd_remap_irq_get (iommu, index, &irq); +if (ret) { +return ret; + } + +if (addr.sub_valid ==1 ) { +VTD_DPRINTF (IR ,"received MSI interrupt" ); +if (origin->data) { +VTD_DPRINTF (GENERAL ,"error: MSI data bits non-zero for " +"interrupt remappable entry: 0x%" PRIx32, + origin->data); +return -VTD_FR_IR_REQ_RSVD ; + } + }else { +uint8_t vector = origin->data &0xff ; +VTD_DPRINTF (IR ,"received IOAPIC interrupt" ); +/* IOAPIC entry vector should be aligned with IRTE vector +* (see vt-d spec 5.1.5.1). */ +if (vector != irq.vector) { +VTD_DPRINTF (GENERAL ,"IOAPIC vector inconsistent: " +"entry: %d, IRTE: %d, index: %d\n" , + vector, irq.vector, index); + } + } + +/* +* We'd better keep the last two bits, assuming that guest OS +* might modify it. Keep it does not hurt after all. +*/ + irq.msi_addr_last_bits = addr.__not_care; + +/* Translate VTDIrq to MSI message */ +vtd_generate_msi_message (&irq, translated); + +VTD_DPRINTF (IR ,"mapping MSI 0x%" PRIx64":0x%" PRIx32" -> " +"0x%" PRIx64":0x%" PRIx32, origin->address, origin->data, + translated->address, translated->data); +return 0 ; + +do_not_translate: +memcpy (translated, origin,sizeof (*origin)); +return 0 ; +} + +int vtd_int_remap (void *iommu,MSIMessage *src,MSIMessage *dst) +{ +return vtd_interrupt_remap_msi (iommu, src, dst); +} + +static MemTxResult vtd_mem_ir_read (void *opaque,hwaddr addr, +uint64_t *data,unsigned size, +MemTxAttrs attrs) +{ + addr +=VTD_INTERRUPT_ADDR_FIRST ; + +VTD_DPRINTF (IR ,"read mem_ir addr 0x%" PRIx64" size %u" , + addr, size); + +if (dma_memory_read (&address_space_memory, addr, &data, size)) { +VTD_DPRINTF (GENERAL ,"error: fail to access 0x%" PRIx64, addr); +return MEMTX_ERROR ; + } + +return MEMTX_OK ; +} + +static MemTxResult vtd_mem_ir_write (void *opaque,hwaddr addr, +uint64_t value,unsigned size, +MemTxAttrs attrs) +{ +int ret =0 ; +MSIMessage from = {0 }, to = {0 }; + + from.address = (uint64_t ) addr +VTD_INTERRUPT_ADDR_FIRST ; + from.data = (uint32_t ) value; + + ret =vtd_interrupt_remap_msi (opaque, &from, &to); +if (ret) { +/* TODO: report error */ +VTD_DPRINTF (GENERAL ,"int remap fail for addr 0x%" PRIx64 +" data 0x%" PRIx32, from.address, from.data); +/* Drop this interrupt */ +return MEMTX_ERROR ; + } + +VTD_DPRINTF (IR ,"delivering MSI 0x%" PRIx64":0x%" PRIx32, + to.address, to.data); + +if (dma_memory_write (&address_space_memory, to.address, + &to.data, size)) { +VTD_DPRINTF (GENERAL ,"error: fail to write 0x%" PRIx64 +" value 0x%" PRIx32, to.address, to.data); + } + +return MEMTX_OK ; +} + +static const MemoryRegionOps vtd_mem_ir_ops = { + .read_with_attrs = vtd_mem_ir_read, + .write_with_attrs = vtd_mem_ir_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =4 , + .max_access_size =4 , + }, + .valid = { + .min_access_size =4 , + .max_access_size =4 , + }, +};VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s,PCIBus *bus,int devfn) {
@@ -1995,6 +2237,11 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn) vtd_dev_as->context_cache_entry.context_cache_gen =0 ;memory_region_init_iommu (&vtd_dev_as->iommu,OBJECT (s), &s->iommu_ops,"intel_iommu" ,UINT64_MAX ); +memory_region_init_io (&vtd_dev_as->iommu_ir,OBJECT (s), + &vtd_mem_ir_ops, s,"intel_iommu_ir" , +VTD_INTERRUPT_ADDR_SIZE ); +memory_region_add_subregion (&vtd_dev_as->iommu,VTD_INTERRUPT_ADDR_FIRST , + &vtd_dev_as->iommu_ir);address_space_init (&vtd_dev_as->as, &vtd_dev_as->iommu,"intel_iommu" ); }
hw/i386/intel_iommu_internal.h +2 -0
@@ -110,6 +110,8 @@/* Interrupt Address Range */ #define VTD_INTERRUPT_ADDR_FIRST 0xfee00000ULL#define VTD_INTERRUPT_ADDR_LAST 0xfeefffffULL +#define VTD_INTERRUPT_ADDR_SIZE (VTD_INTERRUPT_ADDR_LAST - \ + VTD_INTERRUPT_ADDR_FIRST + 1)/* The shift of source_id in the key of IOTLB hash table */ #define VTD_IOTLB_SID_SHIFT 36
include/hw/i386/intel_iommu.h +52 -0
88 lines not on this page · raw message
Signature · 1 line
2.4.11
PATCH v6 11/26 q35: ioapic: add support for emulated IOAPIC IR
In reply to Peter XuThis patch translates all IOAPIC interrupts into MSI ones. One pseudo ioapic address space is added to transfer the MSI message. By default, it will be system memory address space. When IR is enabled, it will be IOMMU address space.
Currently, only emulated IOAPIC is supported.
Idea suggested by Jan Kiszka and Rita Sinha in the following patch:
https://lists.gnu.org/archive/html/qemu-devel/2016-03/msg01933.html
--- hw/i386/pc.c | 3 +++ hw/intc/ioapic.c | 28 ++++++++++++++++++++++++---- hw/pci-host/q35.c | 4 ++++ include/hw/i386/apic-msidef.h | 1 + include/hw/i386/ioapic_internal.h | 1 + include/hw/i386/pc.h | 4 ++++ 6 files changed, 37 insertions(+), 4 deletions(-)
hw/i386/pc.c +3 -0
@@ -1395,6 +1395,9 @@ void pc_memory_init(PCMachineState *pcms,rom_add_option (option_rom[i].name, option_rom[i].bootindex); } pcms->fw_cfg = fw_cfg; + +/* Init default IOAPIC address space */ + pcms->ioapic_as = &address_space_memory; }qemu_irq pc_allocate_cpu_irq (void )
hw/intc/ioapic.c +24 -4
@@ -28,6 +28,8 @@#include "hw/i386/ioapic_internal.h" #include "include/hw/pci/msi.h" #include "sysemu/kvm.h" +#include "target-i386/cpu.h" +#include "hw/i386/apic-msidef.h" //#define DEBUG_IOAPIC
@@ -49,13 +51,15 @@ extern int ioapic_no;static void ioapic_service (IOAPICCommonState *s) { +AddressSpace *ioapic_as =PC_MACHINE (qdev_get_machine ())->ioapic_as; +uint32_t addr, data;uint8_t i;uint8_t trig_mode;uint8_t vector;uint8_t delivery_mode;uint32_t mask;uint64_t entry; -uint8_t dest; +uint16_t dest_idx;uint8_t dest_mode;for (i =0 ; i <IOAPIC_NUM_PINS ; i++) {
@@ -66,7 +70,14 @@ static void ioapic_service(IOAPICCommonState *s) entry = s->ioredtbl[i];if (!(entry &IOAPIC_LVT_MASKED )) { trig_mode = ((entry >>IOAPIC_LVT_TRIGGER_MODE_SHIFT ) &1 ); - dest = entry >>IOAPIC_LVT_DEST_SHIFT ; +/* +* By default, this would be dest_id[8] + +* reserved[8]. When IR is enabled, this would be +* interrupt_index[15] + interrupt_format[1]. This +* field never means anything, but only used to +* generate corresponding MSI. +*/ + dest_idx = entry >>IOAPIC_LVT_DEST_IDX_SHIFT ; dest_mode = (entry >>IOAPIC_LVT_DEST_MODE_SHIFT ) &1 ; delivery_mode = (entry >>IOAPIC_LVT_DELIV_MODE_SHIFT ) &IOAPIC_DM_MASK ;
@@ -96,8 +107,17 @@ static void ioapic_service(IOAPICCommonState *s)#else (void )coalesce;#endif -apic_deliver_irq (dest, dest_mode, delivery_mode, vector, - trig_mode); +/* No matter whether IR is enabled, we translate +* the IOAPIC message into a MSI one, and its +* address space will decide whether we need a +* translation. */ + addr =APIC_DEFAULT_ADDRESS | \ + (dest_idx <<MSI_ADDR_DEST_IDX_SHIFT ) | + (dest_mode <<MSI_ADDR_DEST_MODE_SHIFT ); + data = (vector <<MSI_DATA_VECTOR_SHIFT ) | + (trig_mode <<MSI_DATA_TRIGGER_SHIFT ) | + (delivery_mode <<MSI_DATA_DELIVERY_MODE_SHIFT ); +stl_le_phys (ioapic_as, addr, data); } } }
hw/pci-host/q35.c +4 -0
@@ -437,6 +437,7 @@ static AddressSpace *q35_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)static void mch_init_dmar (MCHPCIState *mch) { +PCMachineState *pcms =PC_MACHINE (qdev_get_machine ());PCIBus *pci_bus =PCI_BUS (qdev_get_parent_bus (DEVICE (mch))); mch->iommu =INTEL_IOMMU_DEVICE (qdev_create (NULL ,TYPE_INTEL_IOMMU_DEVICE ));
@@ -446,6 +447,9 @@ static void mch_init_dmar(MCHPCIState *mch)sysbus_mmio_map (SYS_BUS_DEVICE (mch->iommu),0 ,Q35_HOST_BRIDGE_IOMMU_ADDR );pci_setup_iommu (pci_bus, q35_host_dma_iommu, mch->iommu); +/* Pseudo address space under root PCI bus. */ + pcms->ioapic_as =q35_host_dma_iommu (pci_bus, mch->iommu, +Q35_PSEUDO_DEVFN_IOAPIC ); }static void mch_realize (PCIDevice *d,Error **errp)
include/hw/i386/apic-msidef.h +1 -0
@@ -25,6 +25,7 @@#define MSI_ADDR_REDIRECTION_SHIFT 3#define MSI_ADDR_DEST_ID_SHIFT 12 +#define MSI_ADDR_DEST_IDX_SHIFT 4#define MSI_ADDR_DEST_ID_MASK 0x00ffff0#endif /* HW_APIC_MSIDEF_H */
include/hw/i386/ioapic_internal.h +1 -0
@@ -31,6 +31,7 @@#define IOAPIC_VERSION 0x11#define IOAPIC_LVT_DEST_SHIFT 56 +#define IOAPIC_LVT_DEST_IDX_SHIFT 48#define IOAPIC_LVT_MASKED_SHIFT 16#define IOAPIC_LVT_TRIGGER_MODE_SHIFT 15#define IOAPIC_LVT_REMOTE_IRR_SHIFT 14
include/hw/i386/pc.h +4 -0
@@ -72,6 +72,10 @@ struct PCMachineState {uint64_t numa_nodes;uint64_t *node_mem;uint64_t *node_cpu; + +/* Address space used by IOAPIC device. All IOAPIC interrupts +* will be translated to MSI messages in the address space. */ +AddressSpace *ioapic_as; };#define PC_MACHINE_ACPI_DEVICE_PROP "acpi-device"
Signature · 1 line
2.4.11
PATCH v6 12/26 ioapic: introduce ioapic_entry_parse() helper
In reply to Peter XuAbstract IOAPIC entry parsing logic into a helper function.
--- hw/intc/ioapic.c | 110 +++++++++++++++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 56 deletions(-)
hw/intc/ioapic.c +54 -56
@@ -49,18 +49,56 @@ static IOAPICCommonState *ioapics[MAX_IOAPICS];/* global variable from ioapic_common.c */ extern int ioapic_no; +struct ioapic_entry_info { +/* fields parsed from IOAPIC entries */ +uint8_t masked; +uint8_t trig_mode; +uint16_t dest_idx; +uint8_t dest_mode; +uint8_t delivery_mode; +uint8_t vector; + +/* MSI message generated from above parsed fields */ +uint32_t addr; +uint32_t data; +}; + +static void ioapic_entry_parse (uint64_t entry,struct ioapic_entry_info *info) +{ +bzero (info,sizeof (*info)); + info->masked = (entry >>IOAPIC_LVT_MASKED_SHIFT ) &1 ; + info->trig_mode = (entry >>IOAPIC_LVT_TRIGGER_MODE_SHIFT ) &1 ; +/* +* By default, this would be dest_id[8] + reserved[8]. When IR +* is enabled, this would be interrupt_index[15] + +* interrupt_format[1]. This field never means anything, but +* only used to generate corresponding MSI. +*/ + info->dest_idx = (entry >>IOAPIC_LVT_DEST_IDX_SHIFT ) &0xffff ; + info->dest_mode = (entry >>IOAPIC_LVT_DEST_MODE_SHIFT ) &1 ; + info->delivery_mode = (entry >>IOAPIC_LVT_DELIV_MODE_SHIFT ) \ + &IOAPIC_DM_MASK ; +if (info->delivery_mode ==IOAPIC_DM_EXTINT ) { + info->vector =pic_read_irq (isa_pic); + }else { + info->vector = entry &IOAPIC_VECTOR_MASK ; + } + + info->addr =APIC_DEFAULT_ADDRESS | \ + (info->dest_idx <<MSI_ADDR_DEST_IDX_SHIFT ) | \ + (info->dest_mode <<MSI_ADDR_DEST_MODE_SHIFT ); + info->data = (info->vector <<MSI_DATA_VECTOR_SHIFT ) | \ + (info->trig_mode <<MSI_DATA_TRIGGER_SHIFT ) | \ + (info->delivery_mode <<MSI_DATA_DELIVERY_MODE_SHIFT ); +} +static void ioapic_service (IOAPICCommonState *s) {AddressSpace *ioapic_as =PC_MACHINE (qdev_get_machine ())->ioapic_as; -uint32_t addr, data; +struct ioapic_entry_info info;uint8_t i; -uint8_t trig_mode; -uint8_t vector; -uint8_t delivery_mode;uint32_t mask;uint64_t entry; -uint16_t dest_idx; -uint8_t dest_mode;for (i =0 ; i <IOAPIC_NUM_PINS ; i++) { mask =1 << i;
@@ -68,33 +106,18 @@ static void ioapic_service(IOAPICCommonState *s)int coalesce =0 ; entry = s->ioredtbl[i]; -if (!(entry &IOAPIC_LVT_MASKED )) { - trig_mode = ((entry >>IOAPIC_LVT_TRIGGER_MODE_SHIFT ) &1 ); -/* -* By default, this would be dest_id[8] + -* reserved[8]. When IR is enabled, this would be -* interrupt_index[15] + interrupt_format[1]. This -* field never means anything, but only used to -* generate corresponding MSI. -*/ - dest_idx = entry >>IOAPIC_LVT_DEST_IDX_SHIFT ; - dest_mode = (entry >>IOAPIC_LVT_DEST_MODE_SHIFT ) &1 ; - delivery_mode = - (entry >>IOAPIC_LVT_DELIV_MODE_SHIFT ) &IOAPIC_DM_MASK ; -if (trig_mode ==IOAPIC_TRIGGER_EDGE ) { +ioapic_entry_parse (entry, &info); +if (!info.masked) { +if (info.trig_mode ==IOAPIC_TRIGGER_EDGE ) { s->irr &= ~mask; }else { coalesce = s->ioredtbl[i] &IOAPIC_LVT_REMOTE_IRR ; s->ioredtbl[i] |=IOAPIC_LVT_REMOTE_IRR ; } -if (delivery_mode ==IOAPIC_DM_EXTINT ) { - vector =pic_read_irq (isa_pic); - }else { - vector = entry &IOAPIC_VECTOR_MASK ; - } +#ifdef CONFIG_KVM if (kvm_irqchip_is_split ()) { -if (trig_mode ==IOAPIC_TRIGGER_EDGE ) { +if (info.trig_mode ==IOAPIC_TRIGGER_EDGE ) {kvm_set_irq (kvm_state, i,1 );kvm_set_irq (kvm_state, i,0 ); }else {
@@ -111,13 +134,7 @@ static void ioapic_service(IOAPICCommonState *s)* the IOAPIC message into a MSI one, and its * address space will decide whether we need a * translation. */ - addr =APIC_DEFAULT_ADDRESS | \ - (dest_idx <<MSI_ADDR_DEST_IDX_SHIFT ) | - (dest_mode <<MSI_ADDR_DEST_MODE_SHIFT ); - data = (vector <<MSI_DATA_VECTOR_SHIFT ) | - (trig_mode <<MSI_DATA_TRIGGER_SHIFT ) | - (delivery_mode <<MSI_DATA_DELIVERY_MODE_SHIFT ); -stl_le_phys (ioapic_as, addr, data); +stl_le_phys (ioapic_as, info.addr, info.data); } } }
@@ -168,30 +185,11 @@ static void ioapic_update_kvm_routes(IOAPICCommonState *s)if (kvm_irqchip_is_split ()) {for (i =0 ; i <IOAPIC_NUM_PINS ; i++) { -uint64_t entry = s->ioredtbl[i]; -uint8_t trig_mode; -uint8_t delivery_mode; -uint8_t dest; -uint8_t dest_mode; -uint64_t pin_polarity;MSIMessage msg; - - trig_mode = ((entry >>IOAPIC_LVT_TRIGGER_MODE_SHIFT ) &1 ); - dest = entry >>IOAPIC_LVT_DEST_SHIFT ; - dest_mode = (entry >>IOAPIC_LVT_DEST_MODE_SHIFT ) &1 ; - pin_polarity = (entry >>IOAPIC_LVT_TRIGGER_MODE_SHIFT ) &1 ; - delivery_mode = - (entry >>IOAPIC_LVT_DELIV_MODE_SHIFT ) &IOAPIC_DM_MASK ; - - msg.address =APIC_DEFAULT_ADDRESS ; - msg.address |= dest_mode <<2 ; - msg.address |= dest <<12 ; - - msg.data = entry &IOAPIC_VECTOR_MASK ; - msg.data |= delivery_mode <<APIC_DELIVERY_MODE_SHIFT ; - msg.data |= pin_polarity <<APIC_POLARITY_SHIFT ; - msg.data |= trig_mode <<APIC_TRIG_MODE_SHIFT ; - +struct ioapic_entry_info info; +ioapic_entry_parse (s->ioredtbl[i], &info); + msg.address = info.addr; + msg.data = info.data;kvm_irqchip_update_msi_route (kvm_state, i, msg,NULL ); }kvm_irqchip_commit_routes (kvm_state);
Signature · 1 line
2.4.11
PATCH v6 13/26 intel_iommu: add support for split irqchip
In reply to Peter XuIn split irqchip mode, IOAPIC is working in user space, only update kernel irq routes when entry changed. When IR is enabled, we directly update the kernel with translated messages. It works just like a kernel cache for the remapping entries.
Since KVM irqfd is using kernel gsi routes to deliver interrupts, as long as we can support split irqchip, we will support irqfd as well. Also, since kernel gsi routes will cache translated interrupts, irqfd delivery will not suffer from any performance impact due to IR.
And, since we supported irqfd, vhost devices will be able to work seamlessly with IR now. Logically this should contain both vhost-net and vhost-user case.
--- include/hw/i386/intel_iommu.h | 2 ++ target-i386/kvm.c | 24 ++++++++++++++++++++++++ trace-events | 3 +++ 3 files changed, 29 insertions(+)
include/hw/i386/intel_iommu.h +2 -0
@@ -25,6 +25,7 @@#include "sysemu/dma.h" #include "hw/i386/ioapic.h" #include "hw/pci/msi.h" +#include "hw/sysbus.h" #define TYPE_INTEL_IOMMU_DEVICE "intel-iommu"#define INTEL_IOMMU_DEVICE (obj) \
@@ -250,5 +251,6 @@ struct IntelIOMMUState {VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s,PCIBus *bus,int devfn);/* Get default IOMMU object */ IntelIOMMUState *vtd_iommu_get (void ); +int vtd_int_remap (void *iommu,MSIMessage *src,MSIMessage *dst);#endif
target-i386/kvm.c +24 -0
@@ -36,6 +36,7 @@#include "hw/i386/apic.h" #include "hw/i386/apic_internal.h" #include "hw/i386/apic-msidef.h" +#include "hw/i386/intel_iommu.h" #include "exec/ioport.h" #include "standard-headers/asm-x86/hyperv.h"
@@ -43,6 +44,7 @@#include "hw/pci/msi.h" #include "migration/migration.h" #include "exec/memattrs.h" +#include "trace.h" //#define DEBUG_KVM
@@ -3327,6 +3329,28 @@ int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id)int kvm_arch_fixup_msi_route (struct kvm_irq_routing_entry *route,uint64_t address,uint32_t data,PCIDevice *dev) { +IntelIOMMUState *iommu =vtd_iommu_get (); + +if (iommu) { +int ret; +MSIMessage src, dst; + + src.address = route->u.msi.address_hi; + src.address <<=VTD_MSI_ADDR_HI_SHIFT ; + src.address |= route->u.msi.address_lo; + src.data = route->u.msi.data; + + ret =vtd_int_remap (iommu, &src, &dst); +if (ret) { +trace_kvm_x86_fixup_msi_error (route->gsi); +return 1 ; + } + + route->u.msi.address_hi = dst.address >>VTD_MSI_ADDR_HI_SHIFT ; + route->u.msi.address_lo = dst.address &VTD_MSI_ADDR_LO_MASK ; + route->u.msi.data = dst.data; + } +return 0 ; }
trace-events +3 -0
@@ -1909,3 +1909,6 @@ aspeed_vic_update_fiq(int flags) "Raising FIQ: %d" aspeed_vic_update_irq(int flags) "Raising IRQ: %d" aspeed_vic_read(uint64_t offset, unsigned size, uint32_t value) "From 0x%" PRIx64 " of size %u: 0x%" PRIx32 aspeed_vic_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32 + +# target-i386/kvm.c +kvm_x86_fixup_msi_error(uint32_t gsi) "VT-d failed to remap interrupt for GSI %" PRIu32
Signature · 1 line
2.4.11
PATCH v6 14/26 q35: add "intremap" parameter to enable IR
In reply to Peter XuOne flag is added to specify whether to enable IR for emulated IOMMU. By default, interrupt remapping is not supportted. To enable it, we should specify something like:
$ qemu-system-x86_64 -M q35,iommu=on,intremap=on
To be more clear, the following command:
$ qemu-system-x86_64 -M q35,iommu=on
Will enable IOMMU only, without interrupt remapping support.
Currently, Intel IOMMU IR only support kernel-irqchip={off|split}. We need to specify either of it in -M as well.
--- hw/core/machine.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
hw/core/machine.c +20 -0
@@ -300,6 +300,20 @@ static void machine_set_iommu(Object *obj, bool value, Error **errp) ms->iommu = value; } +static bool machine_get_intremap (Object *obj,Error **errp) +{ +MachineState *ms =MACHINE (obj); + +return ms->iommu_intr; +} + +static void machine_set_intremap (Object *obj,bool value,Error **errp) +{ +MachineState *ms =MACHINE (obj); + + ms->iommu_intr = value; +} +static void machine_set_suppress_vmdesc (Object *obj,bool value,Error **errp) {MachineState *ms =MACHINE (obj);
@@ -480,6 +494,12 @@ static void machine_initfn(Object *obj)object_property_set_description (obj,"iommu" ,"Set on/off to enable/disable Intel IOMMU (VT-d)" ,NULL ); +object_property_add_bool (obj,"intremap" , machine_get_intremap, + machine_set_intremap,NULL ); +object_property_set_description (obj,"intremap" , +"Set on/off to enable/disable IOMMU" +" interrupt remapping" , +NULL );object_property_add_bool (obj,"suppress-vmdesc" , machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,NULL );
Signature · 1 line
2.4.11
PATCH v6 15/26 intel_iommu: introduce IEC notifiers
In reply to Peter XuThis patch introduces Intel VT-d IEC (Interrupt Entry Cache) invalidation notifier list. When vIOMMU receives IEC invalidate request, all the registered units will be notified with specific invalidation requests.
--- hw/i386/intel_iommu.c | 56 ++++++++++++++++++++++++++++++++++++------ hw/i386/intel_iommu_internal.h | 24 +++++++++++++++--- include/hw/i386/intel_iommu.h | 22 +++++++++++++++++ 3 files changed, 91 insertions(+), 11 deletions(-)
hw/i386/intel_iommu.c +49 -7
@@ -900,6 +900,22 @@ static void vtd_root_table_setup(IntelIOMMUState *s) (s->root_extended ? "(extended)" : "")); } +static void vtd_iec_notify_all (IntelIOMMUState *s,bool global, +uint32_t index,uint32_t mask) +{ +VTD_IEC_Notifier *notifier; + +VTD_DPRINTF (INV ,"notify IEC invalidate: global=%d, index=%u, mask=%u" , + global, index, mask); + + QLIST_FOREACH(notifier, &s->iec_notifiers, list) { +if (notifier->iec_notify) { + notifier->iec_notify (notifier->private, global, + index, mask); + } + } +} +static void vtd_interrupt_remap_table_setup (IntelIOMMUState *s) {uint64_t value =0 ;
@@ -907,7 +923,8 @@ static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s) s->intr_size =1UL << ((value &VTD_IRTA_SIZE_MASK ) +1 ); s->intr_root = value &VTD_IRTA_ADDR_MASK ; -/* TODO: invalidate interrupt entry cache */ +/* Notify global invalidation */ +vtd_iec_notify_all (s, true,0 ,0 );VTD_DPRINTF (CSR ,"int remap table addr 0x%" PRIx64" size %" PRIu32, s->intr_root, s->intr_size);
@@ -1409,6 +1426,21 @@ static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)return true; } +static bool vtd_process_inv_iec_desc (IntelIOMMUState *s, +VTDInvDesc *inv_desc) +{ +VTD_DPRINTF (INV ,"inv ir glob %d index %d mask %d" , + inv_desc->iec.granularity, + inv_desc->iec.index, + inv_desc->iec.index_mask); + +vtd_iec_notify_all (s, inv_desc->iec.granularity, + inv_desc->iec.index, + inv_desc->iec.index_mask); + +return true; +} +static bool vtd_process_inv_desc (IntelIOMMUState *s) {VTDInvDesc inv_desc;
@@ -1449,12 +1481,12 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s)break ;case VTD_INV_DESC_IEC : -VTD_DPRINTF (INV ,"Interrupt Entry Cache Invalidation " -"not implemented yet" ); -/* -* Since currently we do not cache interrupt entries, we can -* just mark this descriptor as "good" and move on. -*/ +VTD_DPRINTF (INV ,"Invalidation Interrupt Entry Cache " +"Descriptor hi 0x%" PRIx64" lo 0x%" PRIx64, + inv_desc.hi, inv_desc.lo); +if (!vtd_process_inv_iec_desc (s, &inv_desc)) { +return false; + }break ;default :
@@ -2212,6 +2244,15 @@ static const MemoryRegionOps vtd_mem_ir_ops = { }, }; +void vtd_iec_register_notifier (IntelIOMMUState *s,vtd_iec_notify_fn fn, +void *data) +{ +VTD_IEC_Notifier *notifier =g_new0 (VTD_IEC_Notifier,1 ); + notifier->iec_notify = fn; + notifier->private = data; +QLIST_INSERT_HEAD (&s->iec_notifiers, notifier, list); +} +VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s,PCIBus *bus,int devfn) {uintptr_t key = (uintptr_t )bus;
@@ -2374,6 +2415,7 @@ static void vtd_realize(DeviceState *dev, Error **errp) g_free, g_free); s->vtd_as_by_busptr =g_hash_table_new_full (vtd_uint64_hash, vtd_uint64_equal, g_free, g_free); +QLIST_INIT (&s->iec_notifiers);vtd_init (s); }
hw/i386/intel_iommu_internal.h +20 -4
@@ -296,12 +296,28 @@ typedef enum VTDFaultReason {#define VTD_CONTEXT_CACHE_GEN_MAX 0xffffffffUL +/* Interrupt Entry Cache Invalidation Descriptor: VT-d 6.5.2.7. */ +struct VTDInvDescIEC { +uint32_t type:4 ;/* Should always be 0x4 */ +uint32_t granularity:1 ;/* If set, it's global IR invalidation */ +uint32_t resved_1:22 ; +uint32_t index_mask:5 ;/* 2^N for continuous int invalidation */ +uint32_t index:16 ;/* Start index to invalidate */ +uint32_t reserved_2:16 ; +}; +typedef struct VTDInvDescIEC VTDInvDescIEC ; +/* Queued Invalidation Descriptor */ -struct VTDInvDesc { -uint64_t lo; -uint64_t hi; +union VTDInvDesc { +struct { +uint64_t lo; +uint64_t hi; + }; +union { +VTDInvDescIEC iec; + }; }; -typedef struct VTDInvDesc VTDInvDesc ; +typedef union VTDInvDesc VTDInvDesc ;/* Masks for struct VTDInvDesc */ #define VTD_INV_DESC_TYPE 0xf
include/hw/i386/intel_iommu.h +22 -0
@@ -203,6 +203,24 @@ struct VTD_MSIMessage {/* When IR is enabled, all MSI/MSI-X data bits should be zero */ #define VTD_IR_MSI_DATA (0) +/** +* vtd_iec_notify_fn - IEC (Interrupt Entry Cache) notifier hook, +* triggered when IR invalidation happens. +* @private: private data +* @global: whether this is a global IEC invalidation +* @index: IRTE index to invalidate (start from) +* @mask: invalidation mask +*/ +typedef void (*vtd_iec_notify_fn )(void *private,bool global, +uint32_t index,uint32_t mask); + +struct VTD_IEC_Notifier { +vtd_iec_notify_fn iec_notify; +void *private; +QLIST_ENTRY (VTD_IEC_Notifier) list; +}; +typedef struct VTD_IEC_Notifier VTD_IEC_Notifier ; +/* The iommu (DMAR) device state struct */ struct IntelIOMMUState {SysBusDevice busdev;
@@ -243,6 +261,7 @@ struct IntelIOMMUState {bool intr_enabled;/* Whether guest enabled IR */ dma_addr_t intr_root;/* Interrupt remapping table pointer */ uint32_t intr_size;/* Number of IR table entries */ + QLIST_HEAD(, VTD_IEC_Notifier) iec_notifiers; /* IEC notify list */ };/* Find the VTD Address space associated with the given bus pointer,
@@ -252,5 +271,8 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn);/* Get default IOMMU object */ IntelIOMMUState *vtd_iommu_get (void );int vtd_int_remap (void *iommu,MSIMessage *src,MSIMessage *dst); +/* Register IEC invalidate notifier */ +void vtd_iec_register_notifier (IntelIOMMUState *s,vtd_iec_notify_fn fn, +void *data);#endif
Signature · 1 line
2.4.11
PATCH v6 16/26 ioapic: register VT-d IEC invalidate notifier
In reply to Peter XuLet IOAPIC the first consumer of VT-d IEC invalidation notifiers. This is only used for split irqchip case, when VT-d receives IR invalidation requests, IOAPIC will be notified to update kernel irq routes. For simplicity, we just update all IOAPIC routes, even if the invalidated entries are not IOAPIC ones.
--- hw/intc/ioapic.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
hw/intc/ioapic.c +21 -0
@@ -30,6 +30,7 @@#include "sysemu/kvm.h" #include "target-i386/cpu.h" #include "hw/i386/apic-msidef.h" +#include "hw/i386/intel_iommu.h" //#define DEBUG_IOAPIC
@@ -197,6 +198,14 @@ static void ioapic_update_kvm_routes(IOAPICCommonState *s)#endif } +static void ioapic_iec_notifier (void *private,bool global, +uint32_t index,uint32_t mask) +{ +IOAPICCommonState *s = (IOAPICCommonState *)private; +/* For simplicity, we just update all the routes */ +ioapic_update_kvm_routes (s); +} +void ioapic_eoi_broadcast (int vector) {IOAPICCommonState *s;
@@ -330,6 +339,18 @@ static void ioapic_realize(DeviceState *dev, Error **errp)qdev_init_gpio_in (dev, ioapic_set_irq,IOAPIC_NUM_PINS ); ioapics[ioapic_no] = s; + +#ifdef CONFIG_KVM +if (kvm_irqchip_is_split ()) { +IntelIOMMUState *iommu =vtd_iommu_get (); +if (iommu) { +/* Register this IOAPIC with IOMMU IEC notifier, so that +* when there are IR invalidates, we can be notified to +* update kernel IR cache. */ +vtd_iec_register_notifier (iommu, ioapic_iec_notifier, s); + } + } +#endif }static void ioapic_class_init (ObjectClass *klass,void *data)
Signature · 1 line
2.4.11
PATCH v6 17/26 ioapic: keep RO bits for IOAPIC entry
In reply to Peter XuCurrently IOAPIC RO bits can be written. To be better aligned with hardware, we should let them read-only.
--- hw/intc/ioapic.c | 4 ++++ include/hw/i386/ioapic_internal.h | 5 +++++ 2 files changed, 9 insertions(+)
hw/intc/ioapic.c +4 -0
@@ -307,6 +307,7 @@ ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,default : index = (s->ioregsel -IOAPIC_REG_REDTBL_BASE ) >>1 ;if (index >=0 && index <IOAPIC_NUM_PINS ) { +uint64_t ro_bits = s->ioredtbl[index] &IOAPIC_RO_BITS ;if (s->ioregsel &1 ) { s->ioredtbl[index] &=0xffffffff ; s->ioredtbl[index] |= (uint64_t )val <<32 ;
@@ -314,6 +315,9 @@ ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val, s->ioredtbl[index] &= ~0xffffffffULL ; s->ioredtbl[index] |= val; } +/* restore RO bits */ + s->ioredtbl[index] &=IOAPIC_RW_BITS ; + s->ioredtbl[index] |= ro_bits;ioapic_service (s); } }
include/hw/i386/ioapic_internal.h +5 -0
@@ -48,6 +48,11 @@#define IOAPIC_LVT_DEST_MODE (1 << IOAPIC_LVT_DEST_MODE_SHIFT)#define IOAPIC_LVT_DELIV_MODE (7 << IOAPIC_LVT_DELIV_MODE_SHIFT) +/* Bits that are read-only for IOAPIC entry */ +#define IOAPIC_RO_BITS (IOAPIC_LVT_REMOTE_IRR | \ + IOAPIC_LVT_DELIV_STATUS) +#define IOAPIC_RW_BITS (~(uint64_t)IOAPIC_RO_BITS) +#define IOAPIC_TRIGGER_EDGE 0#define IOAPIC_TRIGGER_LEVEL 1
Signature · 1 line
2.4.11
PATCH v6 18/26 ioapic: clear remote irr bit for edge-triggered interrupts
In reply to Peter XuThis is to better emulate IOAPIC version 0x1X hardware. Linux kernel leveraged this "feature" to do explicit EOI since EOI register is still not introduced at that time. This will also fix the issue that level triggered interrupts failed to work when IR enabled (tested with Linux kernel version 4.5).
--- hw/intc/ioapic.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
hw/intc/ioapic.c +29 -0
@@ -281,6 +281,34 @@ ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)return val; } +/* +* This is to satisfy the hack in Linux kernel. One hack of it is to +* simulate clearing the Remote IRR bit of IOAPIC entry using the +* following: +* +* "For IO-APIC's with EOI register, we use that to do an explicit EOI. +* Otherwise, we simulate the EOI message manually by changing the trigger +* mode to edge and then back to level, with RTE being masked during +* this." +* +* (See linux kernel __eoi_ioapic_pin() comment in commit c0205701) +* +* This is based on the assumption that, Remote IRR bit will be +* cleared by IOAPIC hardware when configured as edge-triggered +* interrupts. +* +* Without this, level-triggered interrupts in IR mode might fail to +* work correctly. +*/ +static inline void +ioapic_fix_edge_remote_irr (uint64_t *entry) +{ +if (!(*entry &IOAPIC_LVT_TRIGGER_MODE )) { +/* Edge-triggered interrupts, make sure remote IRR is zero */ + *entry &= ~((uint64_t )IOAPIC_LVT_REMOTE_IRR ); + } +} +static void ioapic_mem_write (void *opaque,hwaddr addr,uint64_t val,unsigned int size)
@@ -318,6 +346,7 @@ ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,/* restore RO bits */ s->ioredtbl[index] &=IOAPIC_RW_BITS ; s->ioredtbl[index] |= ro_bits; +ioapic_fix_edge_remote_irr (&s->ioredtbl[index]);ioapic_service (s); } }
Signature · 1 line
2.4.11
PATCH v6 19/26 intel_iommu: Add support for Extended Interrupt Mode
In reply to Peter XuFrom: Jan Kiszka <[email protected]>
As neither QEMU nor KVM support more than 255 CPUs so far, this is
simple: we only need to switch the destination ID translation in vtd_remap_irq_get if EIME is set.
Once CFI support is there, it will have to take EIM into account as well. So far, nothing to do for this.
This patch allows to use x2APIC in split irqchip mode of KVM.
--- hw/i386/intel_iommu.c | 16 +++++++++------- hw/i386/intel_iommu_internal.h | 2 ++ include/hw/i386/intel_iommu.h | 1 + 3 files changed, 12 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +9 -7
@@ -922,6 +922,7 @@ static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s) value =vtd_get_quad_raw (s,DMAR_IRTA_REG ); s->intr_size =1UL << ((value &VTD_IRTA_SIZE_MASK ) +1 ); s->intr_root = value &VTD_IRTA_ADDR_MASK ; + s->intr_eime = value &VTD_IRTA_EIME ;/* Notify global invalidation */ vtd_iec_notify_all (s, true,0 ,0 );
@@ -2056,11 +2057,13 @@ static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index, VTDIrq *irq irq->trigger_mode = irte.trigger_mode; irq->vector = irte.vector; irq->delivery_mode = irte.delivery_mode; -/* Not support EIM yet: please refer to vt-d 9.10 DST bits */ + irq->dest = irte.dest_id; +if (!iommu->intr_eime) {#define VTD_IR_APIC_DEST_MASK (0xff00ULL)#define VTD_IR_APIC_DEST_SHIFT (8) - irq->dest = (irte.dest_id &VTD_IR_APIC_DEST_MASK ) >> \ -VTD_IR_APIC_DEST_SHIFT ; + irq->dest = (irq->dest &VTD_IR_APIC_DEST_MASK ) >> +VTD_IR_APIC_DEST_SHIFT ; + } irq->dest_mode = irte.dest_mode; irq->redir_hint = irte.redir_hint;
@@ -2330,7 +2333,7 @@ static void vtd_init(IntelIOMMUState *s) s->ecap =VTD_ECAP_QI |VTD_ECAP_IRO ;if (ms->iommu_intr) { - s->ecap |=VTD_ECAP_IR ; + s->ecap |=VTD_ECAP_IR |VTD_ECAP_EIM ; }vtd_reset_context_cache (s);
@@ -2384,10 +2387,9 @@ static void vtd_init(IntelIOMMUState *s)vtd_define_quad (s,DMAR_FRCD_REG_0_2 ,0 ,0 ,0x8000000000000000ULL );/* -* Interrupt remapping registers, not support extended interrupt -* mode for now. +* Interrupt remapping registers. */ -vtd_define_quad (s,DMAR_IRTA_REG ,0 ,0xfffffffffffff00fULL ,0 ); +vtd_define_quad (s,DMAR_IRTA_REG ,0 ,0xfffffffffffff80fULL ,0 ); }/* Should not reset address_spaces when reset because devices will still use
hw/i386/intel_iommu_internal.h +2 -0
@@ -176,6 +176,7 @@/* IRTA_REG */ #define VTD_IRTA_ADDR_MASK (VTD_HAW_MASK ^ 0xfffULL) +#define VTD_IRTA_EIME (1ULL << 11)#define VTD_IRTA_SIZE_MASK (0xfULL)/* ECAP_REG */
@@ -184,6 +185,7 @@#define VTD_ECAP_QI (1ULL << 1)/* Interrupt Remapping support */ #define VTD_ECAP_IR (1ULL << 3) +#define VTD_ECAP_EIM (1ULL << 4)/* CAP_REG */ /* (offset >> 4) << 24 */
include/hw/i386/intel_iommu.h +1 -0
@@ -261,6 +261,7 @@ struct IntelIOMMUState {bool intr_enabled;/* Whether guest enabled IR */ dma_addr_t intr_root;/* Interrupt remapping table pointer */ uint32_t intr_size;/* Number of IR table entries */ +bool intr_eime;/* Extended interrupt mode enabled */ QLIST_HEAD(, VTD_IEC_Notifier) iec_notifiers; /* IEC notify list */ };
Signature · 1 line
2.4.11
PATCH v6 20/26 intel_iommu: add SID validation for IR
In reply to Peter XuThis patch enables SID validation. Invalid interrupts will be dropped.
--- hw/i386/intel_iommu.c | 70 +++++++++++++++++++++++++++++++++++-------- include/hw/i386/intel_iommu.h | 21 ++++++++++++- target-i386/kvm.c | 3 +- 3 files changed, 80 insertions(+), 14 deletions(-)
hw/i386/intel_iommu.c +58 -12
@@ -2002,11 +2002,15 @@ static Property vtd_properties[] = { DEFINE_PROP_END_OF_LIST(), }; +uint16_t vtd_svt_mask[VTD_SQ_MAX ] = {0xffff ,0xfffb ,0xfff9 ,0xfff8 }; +/* Read IRTE entry with specific index */ static int vtd_irte_get (IntelIOMMUState *iommu,uint16_t index, -VTD_IRTE *entry) +VTD_IRTE *entry,uint16_t sid) {dma_addr_t addr =0x00 ; +uint16_t mask; +uint8_t bus, bus_max, bus_min; addr = iommu->intr_root + index *sizeof (*entry); if (dma_memory_read(&address_space_memory, addr, entry,
@@ -2033,23 +2037,57 @@ static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index,return -VTD_FR_IR_IRTE_RSVD ; } -/* -* TODO: Check Source-ID corresponds to SVT (Source Validation -* Type) bits -*/ +if (sid !=VTD_SID_INVALID ) { +/* Validate IRTE SID */ +switch (entry->sid_vtype) { +case VTD_SVT_NONE : +VTD_DPRINTF (IR ,"No SID validation for IRTE index %d" , index); +break ; + +case VTD_SVT_ALL : + mask = vtd_svt_mask[entry->sid_q]; +if ((entry->source_id & mask) != (sid & mask)) { +VTD_DPRINTF (GENERAL ,"SID validation for IRTE index " +"%d failed (reqid 0x%04x sid 0x%04x)" , index, + sid, entry->source_id); +return -VTD_FR_IR_SID_ERR ; + } +break ; + +case VTD_SVT_BUS : + bus_max = entry->source_id >>8 ; + bus_min = entry->source_id &0xff ; + bus = sid >>8 ; +if (bus > bus_max || bus < bus_min) { +VTD_DPRINTF (GENERAL ,"SID validation for IRTE index %d " +"failed (bus %d outside %d-%d)" , index, bus, + bus_min, bus_max); +return -VTD_FR_IR_SID_ERR ; + } +break ; + +default : +VTD_DPRINTF (GENERAL ,"Invalid SVT bits (0x%x) in IRTE index " +"%d" , entry->sid_vtype, index); +/* Take this as verification failure. */ +return -VTD_FR_IR_SID_ERR ; +break ; + } + }return 0 ; }/* Fetch IRQ information of specific IR index */ -static int vtd_remap_irq_get (IntelIOMMUState *iommu,uint16_t index,VTDIrq *irq) +static int vtd_remap_irq_get (IntelIOMMUState *iommu,uint16_t index, +VTDIrq *irq,uint16_t sid) {VTD_IRTE irte;int ret =0 ;bzero (&irte,sizeof (irte)); - ret =vtd_irte_get (iommu, index, &irte); + ret =vtd_irte_get (iommu, index, &irte, sid);if (ret) {return ret; }
@@ -2101,7 +2139,8 @@ static void vtd_generate_msi_message(VTDIrq *irq, MSIMessage *msg_out)/* Interrupt remapping for MSI/MSI-X entry */ static int vtd_interrupt_remap_msi (IntelIOMMUState *iommu,MSIMessage *origin, -MSIMessage *translated) +MSIMessage *translated, +uint16_t sid) {int ret =0 ;VTD_IR_MSIAddress addr;
@@ -2136,7 +2175,7 @@ static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu, index = addr.index_h <<15 | addr.index_l; - ret =vtd_remap_irq_get (iommu, index, &irq); + ret =vtd_remap_irq_get (iommu, index, &irq, sid);if (ret) {return ret; }
@@ -2180,9 +2219,10 @@ do_not_translate:return 0 ; } -int vtd_int_remap (void *iommu,MSIMessage *src,MSIMessage *dst) +int vtd_int_remap (void *iommu,MSIMessage *src,MSIMessage *dst, +uint16_t sid) { -return vtd_interrupt_remap_msi (iommu, src, dst); +return vtd_interrupt_remap_msi (iommu, src, dst, sid); } static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr,
@@ -2208,11 +2248,17 @@ static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr, {int ret =0 ;MSIMessage from = {0 }, to = {0 }; +uint16_t sid =VTD_SID_INVALID ; from.address = (uint64_t ) addr +VTD_INTERRUPT_ADDR_FIRST ; from.data = (uint32_t ) value; - ret =vtd_interrupt_remap_msi (opaque, &from, &to); +if (!attrs.unspecified) { +/* We have explicit Source ID */ + sid = attrs.requester_id; + } + + ret =vtd_interrupt_remap_msi (opaque, &from, &to, sid);if (ret) {/* TODO: report error */ VTD_DPRINTF(GENERAL, "int remap fail for addr 0x%"PRIx64
include/hw/i386/intel_iommu.h +20 -1
@@ -103,6 +103,23 @@ struct VTDIOTLBEntry {bool write_flags; }; +/* VT-d Source-ID Qualifier types */ +enum { +VTD_SQ_FULL =0x00 ,/* Full SID verification */ +VTD_SQ_IGN_3 =0x01 ,/* Ignore bit 3 */ +VTD_SQ_IGN_2_3 =0x02 ,/* Ignore bits 2 & 3 */ +VTD_SQ_IGN_1_3 =0x03 ,/* Ignore bits 1-3 */ +VTD_SQ_MAX , +}; + +/* VT-d Source Validation Types */ +enum { +VTD_SVT_NONE =0x00 ,/* No validation */ +VTD_SVT_ALL =0x01 ,/* Do full validation */ +VTD_SVT_BUS =0x02 ,/* Validate bus range */ +VTD_SVT_MAX , +}; +/* Interrupt Remapping Table Entry Definition */ union VTD_IRTE { struct {
@@ -271,9 +288,11 @@ struct IntelIOMMUState {VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s,PCIBus *bus,int devfn);/* Get default IOMMU object */ IntelIOMMUState *vtd_iommu_get (void ); -int vtd_int_remap (void *iommu,MSIMessage *src,MSIMessage *dst); +int vtd_int_remap (void *iommu,MSIMessage *src,MSIMessage *dst,uint16_t sid);/* Register IEC invalidate notifier */ void vtd_iec_register_notifier (IntelIOMMUState *s,vtd_iec_notify_fn fn,void *data); +#define VTD_SID_INVALID (0xffff) +#endif
target-i386/kvm.c +2 -1
@@ -3340,7 +3340,8 @@ int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, src.address |= route->u.msi.address_lo; src.data = route->u.msi.data; - ret =vtd_int_remap (iommu, &src, &dst); + ret =vtd_int_remap (iommu, &src, &dst, dev ?pci_requester_id (dev) : +VTD_SID_INVALID );if (ret) {trace_kvm_x86_fixup_msi_error (route->gsi);return 1 ;
Signature · 1 line
2.4.11
PATCH v6 21/26 x86-iommu: introduce parent class
In reply to Peter XuIntroducing parent class for intel-iommu devices named "x86-iommu". This is preparation work to abstract shared functionalities out from Intel and AMD IOMMUs. Currently, only the parent class is introduced. It does nothing yet.
--- hw/i386/Makefile.objs | 2 +- hw/i386/intel_iommu.c | 4 ++-- hw/i386/x86-iommu.c | 42 ++++++++++++++++++++++++++++++++++++++++++ hw/pci-host/q35.c | 2 +- include/hw/i386/intel_iommu.h | 3 ++- include/hw/i386/x86-iommu.h | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 hw/i386/x86-iommu.c create mode 100644 include/hw/i386/x86-iommu.h
hw/i386/Makefile.objs +1 -1
@@ -2,7 +2,7 @@ obj-$(CONFIG_KVM) += kvm/ obj-y += multiboot.o obj-y += pc.o pc_piix.o pc_q35.o obj-y += pc_sysfw.o -obj-y += intel_iommu.o +obj-y += x86-iommu.o intel_iommu.o obj-$(CONFIG_XEN) += ../xenpv/ xen/ obj-y += kvmvapic.o
hw/i386/intel_iommu.c +2 -2
@@ -2343,7 +2343,7 @@ IntelIOMMUState *vtd_iommu_get(void)bool ambiguous = false;Object *intel_iommu =NULL ; - intel_iommu =object_resolve_path_type ("" ,TYPE_INTEL_IOMMU_DEVICE , + intel_iommu =object_resolve_path_type ("" ,TYPE_X86_IOMMU_DEVICE , &ambiguous);if (ambiguous) intel_iommu =NULL ;
@@ -2479,7 +2479,7 @@ static void vtd_class_init(ObjectClass *klass, void *data)static const TypeInfo vtd_info = { .name =TYPE_INTEL_IOMMU_DEVICE , - .parent =TYPE_SYS_BUS_DEVICE , + .parent =TYPE_X86_IOMMU_DEVICE , .instance_size =sizeof (IntelIOMMUState), .class_init = vtd_class_init, };
hw/i386/x86-iommu.c new file +42 -0
42 lines not on this page · raw message
hw/pci-host/q35.c +1 -1
@@ -441,7 +441,7 @@ static void mch_init_dmar(MCHPCIState *mch)PCIBus *pci_bus =PCI_BUS (qdev_get_parent_bus (DEVICE (mch))); mch->iommu =INTEL_IOMMU_DEVICE (qdev_create (NULL ,TYPE_INTEL_IOMMU_DEVICE )); -object_property_add_child (OBJECT (mch),"intel-iommu" , +object_property_add_child (OBJECT (mch),"x86-iommu" ,OBJECT (mch->iommu),NULL );qdev_init_nofail (DEVICE (mch->iommu));sysbus_mmio_map (SYS_BUS_DEVICE (mch->iommu),0 ,Q35_HOST_BRIDGE_IOMMU_ADDR );
include/hw/i386/intel_iommu.h +2 -1
@@ -26,6 +26,7 @@#include "hw/i386/ioapic.h" #include "hw/pci/msi.h" #include "hw/sysbus.h" +#include "hw/i386/x86-iommu.h" #define TYPE_INTEL_IOMMU_DEVICE "intel-iommu"#define INTEL_IOMMU_DEVICE (obj) \
@@ -240,7 +241,7 @@ typedef struct VTD_IEC_Notifier VTD_IEC_Notifier;/* The iommu (DMAR) device state struct */ struct IntelIOMMUState { -SysBusDevice busdev; +X86IOMMUState x86_iommu;MemoryRegion csrmem;uint8_t csr[DMAR_REG_SIZE ];/* register values */ uint8_t wmask[DMAR_REG_SIZE ];/* R/W bytes */
include/hw/i386/x86-iommu.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* Common IOMMU interface for X86 platform +* +* Copyright (C) 2016 Peter Xu, Red Hat <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. + +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. + +* You should have received a copy of the GNU General Public License along +* with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef IOMMU_COMMON_H +#define IOMMU_COMMON_H + +#include "hw/sysbus.h" + +#define TYPE_X86_IOMMU_DEVICE ("x86-iommu") +#define X86_IOMMU_DEVICE (obj) \ + OBJECT_CHECK(X86IOMMUState, (obj), TYPE_X86_IOMMU_DEVICE) + +typedef struct X86IOMMUState X86IOMMUState ; + +struct X86IOMMUState { +SysBusDevice busdev; +}; + +#endif
Signature · 1 line
2.4.11
PATCH v6 22/26 x86-iommu: replace existing VT-d hooks into X86 ones
In reply to Peter XuPreviously, there are lots of VT-d hooks in common codes (like q35, ioapic, etc.). A better way is to avoid using VT-d interfaces. Also, we can start to abstract some common functions between Intel and future AMD IOMMU device.
This patch cleaned up all the VT-d hooks into x86 ones, and moved IEC notifier list from VT-d state to x86 state, so that this can be further leveraged by AMD IOMMU codes.
Instead of searching in the global device tree every time, one static variable is declared to store the default system x86 IOMMU device.
--- hw/i386/acpi-build.c | 4 +-- hw/i386/intel_iommu.c | 67 +++++++++++++------------------------ hw/i386/x86-iommu.c | 49 +++++++++++++++++++++++++++ hw/intc/ioapic.c | 6 ++-- hw/pci-host/q35.c | 11 +++---- include/hw/i386/intel_iommu.h | 33 ------------------- include/hw/i386/x86-iommu.h | 77 ++++++++++++++++++++++++++++++++++++++++++- target-i386/kvm.c | 7 ++-- 8 files changed, 160 insertions(+), 94 deletions(-)
hw/i386/acpi-build.c +2 -2
@@ -51,7 +51,7 @@#include "hw/i386/ich9.h" #include "hw/pci/pci_bus.h" #include "hw/pci-host/q35.h" -#include "hw/i386/intel_iommu.h" +#include "hw/i386/x86-iommu.h" #include "hw/timer/hpet.h" #include "hw/acpi/aml-build.h"
@@ -2677,7 +2677,7 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)static bool acpi_has_iommu (void ) { -return !!vtd_iommu_get (); +return !!x86_iommu_get_default (); } static
hw/i386/intel_iommu.c +22 -45
151 lines not on this page · raw message
hw/i386/x86-iommu.c +49 -0
63 lines not on this page · raw message
hw/intc/ioapic.c +3 -3
@@ -30,7 +30,7 @@#include "sysemu/kvm.h" #include "target-i386/cpu.h" #include "hw/i386/apic-msidef.h" -#include "hw/i386/intel_iommu.h" +#include "hw/i386/x86-iommu.h" //#define DEBUG_IOAPIC
@@ -375,12 +375,12 @@ static void ioapic_realize(DeviceState *dev, Error **errp)#ifdef CONFIG_KVM if (kvm_irqchip_is_split ()) { -IntelIOMMUState *iommu =vtd_iommu_get (); +X86IOMMUState *iommu =x86_iommu_get_default ();if (iommu) {/* Register this IOAPIC with IOMMU IEC notifier, so that * when there are IR invalidates, we can be notified to * update kernel IR cache. */ -vtd_iec_register_notifier (iommu, ioapic_iec_notifier, s); +x86_iommu_iec_register_notifier (iommu, ioapic_iec_notifier, s); } }#endif
hw/pci-host/q35.c +4 -7
@@ -426,13 +426,10 @@ static void mch_reset(DeviceState *qdev)static AddressSpace *q35_host_dma_iommu (PCIBus *bus,void *opaque,int devfn) { -IntelIOMMUState *s = opaque; -VTDAddressSpace *vtd_as; - -assert (0 <= devfn && devfn <=VTD_PCI_DEVFN_MAX ); - - vtd_as =vtd_find_add_as (s, bus, devfn); -return &vtd_as->as; +X86IOMMUState *x86_iommu = opaque; +X86IOMMUClass *x86_class =X86_IOMMU_GET_CLASS (x86_iommu); +assert (0 <= devfn && devfn <=X86_IOMMU_PCI_DEVFN_MAX ); +return x86_class->find_add_as (x86_iommu, bus, devfn); }static void mch_init_dmar (MCHPCIState *mch)
include/hw/i386/intel_iommu.h +0 -33
@@ -38,7 +38,6 @@#define VTD_PCI_BUS_MAX 256#define VTD_PCI_SLOT_MAX 32#define VTD_PCI_FUNC_MAX 8 -#define VTD_PCI_DEVFN_MAX 256#define VTD_PCI_SLOT (devfn) (((devfn) >> 3) & 0x1f)#define VTD_PCI_FUNC (devfn) ((devfn) & 0x07)#define VTD_SID_TO_BUS (sid) (((sid) >> 8) & 0xff)
@@ -221,24 +220,6 @@ struct VTD_MSIMessage {/* When IR is enabled, all MSI/MSI-X data bits should be zero */ #define VTD_IR_MSI_DATA (0) -/** -* vtd_iec_notify_fn - IEC (Interrupt Entry Cache) notifier hook, -* triggered when IR invalidation happens. -* @private: private data -* @global: whether this is a global IEC invalidation -* @index: IRTE index to invalidate (start from) -* @mask: invalidation mask -*/ -typedef void (*vtd_iec_notify_fn )(void *private,bool global, -uint32_t index,uint32_t mask); - -struct VTD_IEC_Notifier { -vtd_iec_notify_fn iec_notify; -void *private; -QLIST_ENTRY (VTD_IEC_Notifier) list; -}; -typedef struct VTD_IEC_Notifier VTD_IEC_Notifier ; -/* The iommu (DMAR) device state struct */ struct IntelIOMMUState {X86IOMMUState x86_iommu;
@@ -280,20 +261,6 @@ struct IntelIOMMUState {dma_addr_t intr_root;/* Interrupt remapping table pointer */ uint32_t intr_size;/* Number of IR table entries */ bool intr_eime; /* Extended interrupt mode enabled */ - QLIST_HEAD(, VTD_IEC_Notifier) iec_notifiers; /* IEC notify list */ }; -/* Find the VTD Address space associated with the given bus pointer, -* create a new one if none exists -*/ -VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s,PCIBus *bus,int devfn); -/* Get default IOMMU object */ -IntelIOMMUState *vtd_iommu_get (void ); -int vtd_int_remap (void *iommu,MSIMessage *src,MSIMessage *dst,uint16_t sid); -/* Register IEC invalidate notifier */ -void vtd_iec_register_notifier (IntelIOMMUState *s,vtd_iec_notify_fn fn, -void *data); - -#define VTD_SID_INVALID (0xffff) -#endif
include/hw/i386/x86-iommu.h +76 -1
91 lines not on this page · raw message
target-i386/kvm.c +4 -3
@@ -3329,19 +3329,20 @@ int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id)int kvm_arch_fixup_msi_route (struct kvm_irq_routing_entry *route,uint64_t address,uint32_t data,PCIDevice *dev) { -IntelIOMMUState *iommu =vtd_iommu_get (); +X86IOMMUState *iommu =x86_iommu_get_default ();if (iommu) {int ret;MSIMessage src, dst; +X86IOMMUClass *class =X86_IOMMU_GET_CLASS (iommu); src.address = route->u.msi.address_hi; src.address <<=VTD_MSI_ADDR_HI_SHIFT ; src.address |= route->u.msi.address_lo; src.data = route->u.msi.data; - ret =vtd_int_remap (iommu, &src, &dst, dev ?pci_requester_id (dev) : -VTD_SID_INVALID ); + ret = class->int_remap (iommu, &src, &dst, dev ? \ +pci_requester_id (dev) :X86_IOMMU_SID_INVALID );if (ret) {trace_kvm_x86_fixup_msi_error (route->gsi);return 1 ;
Signature · 1 line
2.4.11
Re: [Qemu-devel] [PATCH v6 22/26] x86-iommu: replace existing VT-d hooks into X86 ones
Quoted 4 lines · Peter Xu
Previously, there are lots of VT-d hooks in common codes (like q35, ioapic, etc.). A better way is to avoid using VT-d interfaces. Also, we can start to abstract some common functions between Intel and future AMD IOMMU device.
Just don't introduce the VT-d variants in the first place. Will also shorten your patch series again.
Jan
Re: [Qemu-devel] [PATCH v6 22/26] x86-iommu: replace existing VT-d hooks into X86 ones
Quoted 8 lines · Jan Kiszka
Quoted 4 lines · Peter Xu
Previously, there are lots of VT-d hooks in common codes (like q35, ioapic, etc.). A better way is to avoid using VT-d interfaces. Also, we can start to abstract some common functions between Intel and future AMD IOMMU device.
Just don't introduce the VT-d variants in the first place. Will also shorten your patch series again.
Yes, I can do that.
After waiting for some more review comments and making sure this is the correct way to go, I can do the re-arrangement in next version.
Thanks,
-- peterx
PATCH v6 23/26 kvm-irqchip: simplify kvm_irqchip_add_msi_route
In reply to Peter XuChanging the original MSIMessage parameter in kvm_irqchip_add_msi_route into the vector number. Vector index provides more information than the MSIMessage, we can retrieve the MSIMessage using the vector easily. This will avoid fetching MSIMessage every time before adding MSI routes.
Meanwhile, the vector info will be used in the coming patches to further enable gsi route update notifications.
--- hw/i386/kvm/pci-assign.c | 8 ++------ hw/misc/ivshmem.c | 3 +-- hw/vfio/pci.c | 11 +++++------ hw/virtio/virtio-pci.c | 9 +++------ include/sysemu/kvm.h | 13 ++++++++++++- kvm-all.c | 18 ++++++++++++++++-- kvm-stub.c | 2 +- target-i386/kvm.c | 3 +-- 8 files changed, 41 insertions(+), 26 deletions(-)
hw/i386/kvm/pci-assign.c +2 -6
@@ -974,10 +974,9 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev) }if (ctrl_byte &PCI_MSI_FLAGS_ENABLE ) { -MSIMessage msg =msi_get_message (pci_dev,0 );int virq; - virq =kvm_irqchip_add_msi_route (kvm_state, msg, pci_dev); + virq =kvm_irqchip_add_msi_route (kvm_state,0 , pci_dev);if (virq <0 ) {perror ("assigned_dev_update_msi: kvm_irqchip_add_msi_route" );return ;
@@ -1042,7 +1041,6 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)uint16_t entries_nr =0 ;int i, r =0 ;MSIXTableEntry *entry = adev->msix_table; -MSIMessage msg;/* Get the usable entry number for allocating */ for (i =0 ; i < adev->msix_max; i++, entry++) {
@@ -1079,9 +1077,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)continue ; } - msg.address = entry->addr_lo | ((uint64_t )entry->addr_hi <<32 ); - msg.data = entry->data; - r =kvm_irqchip_add_msi_route (kvm_state, msg, pci_dev); + r =kvm_irqchip_add_msi_route (kvm_state, i, pci_dev);if (r <0 ) {return r; }
hw/misc/ivshmem.c +1 -2
@@ -444,13 +444,12 @@ static void ivshmem_add_kvm_msi_virq(IVShmemState *s, int vector, Error **errp) {PCIDevice *pdev =PCI_DEVICE (s); -MSIMessage msg =msix_get_message (pdev, vector);int ret;IVSHMEM_DPRINTF ("ivshmem_add_kvm_msi_virq vector:%d\n" , vector);assert (!s->msi_vectors[vector].pdev); - ret =kvm_irqchip_add_msi_route (kvm_state, msg, pdev); + ret =kvm_irqchip_add_msi_route (kvm_state, vector, pdev);if (ret <0 ) {error_setg (errp,"kvm_irqchip_add_msi_route failed" );return ;
hw/vfio/pci.c +5 -6
@@ -417,11 +417,11 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev, bool msix) }static void vfio_add_kvm_msi_virq (VFIOPCIDevice *vdev,VFIOMSIVector *vector, -MSIMessage *msg,bool msix) +int vector_n,bool msix) {int virq; -if ((msix && vdev->no_kvm_msix) || (!msix && vdev->no_kvm_msi) || !msg) { +if ((msix && vdev->no_kvm_msix) || (!msix && vdev->no_kvm_msi)) {return ; }
@@ -429,7 +429,7 @@ static void vfio_add_kvm_msi_virq(VFIOPCIDevice *vdev, VFIOMSIVector *vector,return ; } - virq =kvm_irqchip_add_msi_route (kvm_state, *msg, &vdev->pdev); + virq =kvm_irqchip_add_msi_route (kvm_state, vector_n, &vdev->pdev);if (virq <0 ) {event_notifier_cleanup (&vector->kvm_interrupt);return ;
@@ -495,7 +495,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,vfio_update_kvm_msi_virq (vector, *msg, pdev); } }else { -vfio_add_kvm_msi_virq (vdev, vector, msg, true); +vfio_add_kvm_msi_virq (vdev, vector, nr, true); }/*
@@ -639,7 +639,6 @@ retry:for (i =0 ; i < vdev->nr_vectors; i++) {VFIOMSIVector *vector = &vdev->msi_vectors[i]; -MSIMessage msg =msi_get_message (&vdev->pdev, i); vector->vdev = vdev; vector->virq =-1 ;
@@ -656,7 +655,7 @@ retry:* Attempt to enable route through KVM irqchip, * default to userspace handling if unavailable. */ -vfio_add_kvm_msi_virq (vdev, vector, &msg, false); +vfio_add_kvm_msi_virq (vdev, vector, i, false); }/* Set interrupt type prior to possible interrupts */
hw/virtio/virtio-pci.c +3 -6
@@ -727,14 +727,13 @@ static uint32_t virtio_read_config(PCIDevice *pci_dev,static int kvm_virtio_pci_vq_vector_use (VirtIOPCIProxy *proxy,unsigned int queue_no, -unsigned int vector, -MSIMessage msg) +unsigned int vector) {VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];int ret;if (irqfd->users ==0 ) { - ret =kvm_irqchip_add_msi_route (kvm_state, msg, &proxy->pci_dev); + ret =kvm_irqchip_add_msi_route (kvm_state, vector, &proxy->pci_dev);if (ret <0 ) {return ret; }
@@ -787,7 +786,6 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)VirtioDeviceClass *k =VIRTIO_DEVICE_GET_CLASS (vdev);unsigned int vector;int ret, queue_no; -MSIMessage msg;for (queue_no =0 ; queue_no < nvqs; queue_no++) {if (!virtio_queue_get_num (vdev, queue_no)) {
@@ -797,8 +795,7 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)if (vector >=msix_nr_vectors_allocated (dev)) {continue ; } - msg =msix_get_message (dev, vector); - ret =kvm_virtio_pci_vq_vector_use (proxy, queue_no, vector, msg); + ret =kvm_virtio_pci_vq_vector_use (proxy, queue_no, vector);if (ret <0 ) { goto undo; }
include/sysemu/kvm.h +12 -1
@@ -470,7 +470,18 @@ static inline void cpu_synchronize_post_init(CPUState *cpu) } } -int kvm_irqchip_add_msi_route (KVMState *s,MSIMessage msg,PCIDevice *dev); +/** +* kvm_irqchip_add_msi_route - Add MSI route for specific vector +* @s: KVM state +* @vector: which vector to add. This can be either MSI/MSIX +* vector. The function will automatically detect whether +* MSI/MSIX is enabled, and fetch corresponding MSI +* message. +* @dev: Owner PCI device to add the route. If @dev is specified +* as @NULL, an empty MSI message will be inited. +* @return: virq (>=0) when success, errno (<0) when failed. +*/ +int kvm_irqchip_add_msi_route (KVMState *s,int vector,PCIDevice *dev);int kvm_irqchip_update_msi_route (KVMState *s,int virq,MSIMessage msg,PCIDevice *dev);void kvm_irqchip_release_virq (KVMState *s,int virq);
kvm-all.c +16 -2
@@ -26,6 +26,7 @@#include "qemu/error-report.h" #include "hw/hw.h" #include "hw/pci/msi.h" +#include "hw/pci/msix.h" #include "hw/s390x/adapter.h" #include "exec/gdbstub.h" #include "sysemu/kvm_int.h"
@@ -1177,10 +1178,23 @@ int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)return kvm_set_irq (s, route->kroute.gsi,1 ); } -int kvm_irqchip_add_msi_route (KVMState *s,MSIMessage msg,PCIDevice *dev) +int kvm_irqchip_add_msi_route (KVMState *s,int vector,PCIDevice *dev) {struct kvm_irq_routing_entry kroute = {};int virq; +MSIMessage msg = {0 ,0 }; + +if (dev) { +if (msix_enabled (dev)) { + msg =msix_get_message (dev, vector); + }else if (msi_enabled (dev)) { + msg =msi_get_message (dev, vector); + }else { +/* Should never happen */ +error_report ("%s: unknown interrupt type" , __func__); +abort (); + } + }if (kvm_gsi_direct_mapping ()) {return kvm_arch_msi_data_to_gsi (msg.data);
@@ -1330,7 +1344,7 @@ int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)abort (); } -int kvm_irqchip_add_msi_route (KVMState *s,MSIMessage msg) +int kvm_irqchip_add_msi_route (KVMState *s,int vector,PCIDevice *dev) {return -ENOSYS ; }
kvm-stub.c +1 -1
@@ -112,7 +112,7 @@ int kvm_on_sigbus(int code, void *addr) }#ifndef CONFIG_USER_ONLY -int kvm_irqchip_add_msi_route (KVMState *s,MSIMessage msg,PCIDevice *dev) +int kvm_irqchip_add_msi_route (KVMState *s,int vector,PCIDevice *dev) {return -ENOSYS ; }
target-i386/kvm.c +1 -2
@@ -3158,8 +3158,7 @@ void kvm_arch_init_irq_routing(KVMState *s)/* If the ioapic is in QEMU and the lapics are in KVM, reserve MSI routes for signaling interrupts to the local apics. */ for (i =0 ; i <IOAPIC_NUM_PINS ; i++) { -struct MSIMessage msg = {0x0 ,0x0 }; -if (kvm_irqchip_add_msi_route (s, msg,NULL ) <0 ) { +if (kvm_irqchip_add_msi_route (s,0 ,NULL ) <0 ) {error_report ("Could not enable split IRQ mode." );exit (1 ); }
Signature · 1 line
2.4.11
PATCH v6 24/26 kvm-irqchip: i386: add hook for add/remove virq
In reply to Peter XuAdding two hooks to be notified when adding/removing msi routes. On x86 platform, one list is maintained for all existing msi routes.
--- include/sysemu/kvm.h | 6 ++++++ kvm-all.c | 2 ++ target-arm/kvm.c | 11 +++++++++++ target-i386/kvm.c | 38 ++++++++++++++++++++++++++++++++++++++ target-mips/kvm.c | 11 +++++++++++ target-ppc/kvm.c | 11 +++++++++++ target-s390x/kvm.c | 11 +++++++++++ trace-events | 2 ++ 8 files changed, 92 insertions(+)
include/sysemu/kvm.h +6 -0
@@ -355,6 +355,12 @@ void kvm_arch_init_irq_routing(KVMState *s);int kvm_arch_fixup_msi_route (struct kvm_irq_routing_entry *route,uint64_t address,uint32_t data,PCIDevice *dev); +/* Notify arch about newly added MSI routes */ +int kvm_arch_add_msi_route_post (struct kvm_irq_routing_entry *route, +int vector,PCIDevice *dev); +/* Notify arch about released MSI routes */ +int kvm_arch_release_virq_post (int virq); +int kvm_arch_msi_data_to_gsi (uint32_t data);int kvm_set_irq (KVMState *s,int irq,int level);
kvm-all.c +2 -0
@@ -1073,6 +1073,7 @@ void kvm_irqchip_release_virq(KVMState *s, int virq) } }clear_gsi (s, virq); +kvm_arch_release_virq_post (virq); }static unsigned int kvm_hash_msi (uint32_t data)
@@ -1221,6 +1222,7 @@ int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev) }kvm_add_routing_entry (s, &kroute); +kvm_arch_add_msi_route_post (&kroute, vector, dev);kvm_irqchip_commit_routes (s);return virq;
target-arm/kvm.c +11 -0
@@ -622,6 +622,17 @@ int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,return 0 ; } +int kvm_arch_add_msi_route_post (struct kvm_irq_routing_entry *route, +int vector,PCIDevice *dev) +{ +return 0 ; +} + +int kvm_arch_release_virq_post (int virq) +{ +return 0 ; +} +int kvm_arch_msi_data_to_gsi (uint32_t data) {return (data -32 ) &0xffff ;
target-i386/kvm.c +38 -0
@@ -3355,6 +3355,44 @@ int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,return 0 ; } +typedef struct MSIRouteEntry MSIRouteEntry ; + +struct MSIRouteEntry { +PCIDevice *dev;/* Device pointer */ +int vector;/* MSI/MSIX vector index */ +int virq;/* Virtual IRQ index */ +QLIST_ENTRY (MSIRouteEntry) list; +}; + +/* List of used GSI routes */ +static QLIST_HEAD(, MSIRouteEntry) msi_route_list = \ +QLIST_HEAD_INITIALIZER (msi_route_list); + +int kvm_arch_add_msi_route_post (struct kvm_irq_routing_entry *route, +int vector,PCIDevice *dev) +{ +MSIRouteEntry *entry =g_new0 (MSIRouteEntry,1 ); + entry->dev = dev; + entry->vector = vector; + entry->virq = route->gsi; +QLIST_INSERT_HEAD (&msi_route_list, entry, list); +trace_kvm_x86_add_msi_route (route->gsi); +return 0 ; +} + +int kvm_arch_release_virq_post (int virq) +{ +MSIRouteEntry *entry, *next; + QLIST_FOREACH_SAFE(entry, &msi_route_list, list, next) { +if (entry->virq == virq) { +trace_kvm_x86_remove_msi_route (virq); +QLIST_REMOVE (entry, list); +break ; + } + } +return 0 ; +} +int kvm_arch_msi_data_to_gsi (uint32_t data) {abort ();
target-mips/kvm.c +11 -0
@@ -1044,6 +1044,17 @@ int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,return 0 ; } +int kvm_arch_add_msi_route_post (struct kvm_irq_routing_entry *route, +int vector,PCIDevice *dev) +{ +return 0 ; +} + +int kvm_arch_release_virq_post (int virq) +{ +return 0 ; +} +int kvm_arch_msi_data_to_gsi (uint32_t data) {abort ();
target-ppc/kvm.c +11 -0
@@ -2566,6 +2566,17 @@ int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,return 0 ; } +int kvm_arch_add_msi_route_post (struct kvm_irq_routing_entry *route, +int vector,PCIDevice *dev) +{ +return 0 ; +} + +int kvm_arch_release_virq_post (int virq) +{ +return 0 ; +} +int kvm_arch_msi_data_to_gsi (uint32_t data) {return data &0xffff ;
target-s390x/kvm.c +11 -0
@@ -2246,6 +2246,17 @@ int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,return 0 ; } +int kvm_arch_add_msi_route_post (struct kvm_irq_routing_entry *route, +int vector,PCIDevice *dev) +{ +return 0 ; +} + +int kvm_arch_release_virq_post (int virq) +{ +return 0 ; +} +int kvm_arch_msi_data_to_gsi (uint32_t data) {abort ();
trace-events +2 -0
@@ -1912,3 +1912,5 @@ aspeed_vic_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 # target-i386/kvm.c kvm_x86_fixup_msi_error(uint32_t gsi) "VT-d failed to remap interrupt for GSI %" PRIu32 +kvm_x86_add_msi_route(int virq) "Adding route entry for virq %d" +kvm_x86_remove_msi_route(int virq) "Removing route entry for virq %d"
Signature · 1 line
2.4.11
Re: [Qemu-devel] [PATCH v6 24/26] kvm-irqchip: i386: add hook for add/remove virq
Quoted 2 lines · Peter Xu
Adding two hooks to be notified when adding/removing msi routes. On x86 platform, one list is maintained for all existing msi routes.
Here the commit message is not accurate. Not all existing MSI routes are maintained. Only irqfd ones.
There are two kinds of MSI routes:
- in kvm_irqchip_add_irq_route(): before assigning IRQFD. Used by vhost, vfio, etc.
- in kvm_irqchip_send_msi(): when sending direct MSI message, if direct MSI not allowed, we will first create one MSI route entry in the kernel, then trigger it.
This patch only hooks the first one (irqfd case). We do not need to take care for the 2nd one, since it's only used by QEMU userspace (kvm-apic) and the messages will always do in-time translation when triggered.
-- peterx
Re: [Qemu-devel] [PATCH v6 24/26] kvm-irqchip: i386: add hook for add/remove virq
In reply to Peter XuQuoted 14 lines · Peter Xu
Adding two hooks to be notified when adding/removing msi routes. On x86 platform, one list is maintained for all existing msi routes.
Signed-off-by Peter Xu <[email protected]>--- include/sysemu/kvm.h | 6 ++++++ kvm-all.c | 2 ++ target-arm/kvm.c | 11 +++++++++++ target-i386/kvm.c | 38 ++++++++++++++++++++++++++++++++++++++ target-mips/kvm.c | 11 +++++++++++ target-ppc/kvm.c | 11 +++++++++++ target-s390x/kvm.c | 11 +++++++++++ trace-events | 2 ++ 8 files changed, 92 insertions(+)
[...]
Quoted 15 lines
target-i386/kvm.c +7
-0@@ -3355,6 +3355,44 @@ int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,return 0 ; } +typedef struct MSIRouteEntry MSIRouteEntry ; + +struct MSIRouteEntry { +PCIDevice *dev;/* Device pointer */ +int vector;/* MSI/MSIX vector index */ +int virq;/* Virtual IRQ index */ +QLIST_ENTRY (MSIRouteEntry) list;
target-i386/kvm.c int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, · line 3374
Quoted 10 lines
target-i386/kvm.c +10
-0+}; + +/* List of used GSI routes */ +static QLIST_HEAD(, MSIRouteEntry) msi_route_list = \ +QLIST_HEAD_INITIALIZER (msi_route_list); + +int kvm_arch_add_msi_route_post (struct kvm_irq_routing_entry *route, +int vector,PCIDevice *dev) +{ +MSIRouteEntry *entry =g_new0 (MSIRouteEntry,1 );
Ah... Here, I should skip all IOAPIC msi routes by checking whether dev == NULL. We should only care about irqfd users, while IOAPIC has its own notifier. Will fix this in next version.
-- peterx
PATCH v6 25/26 kvm-irqchip: x86: add msi route notify fn
In reply to Peter XuOne more IEC notifier is added to let msi routes know about the IEC changes. When interrupt invalidation happens, all registered msi routes will be updated for all PCI devices.
Since both vfio and vhost are possible gsi route consumers, this patch will go one step further to keep them safe in split irqchip mode and when irqfd is enabled.
--- hw/pci/pci.c | 15 +++++++++++++++ include/hw/pci/pci.h | 2 ++ kvm-all.c | 10 +--------- target-i386/kvm.c | 30 ++++++++++++++++++++++++++++++ trace-events | 1 + 5 files changed, 49 insertions(+), 9 deletions(-)
hw/pci/pci.c +15 -0
@@ -2498,6 +2498,21 @@ PCIDevice *pci_get_function_0(PCIDevice *pci_dev) } } +MSIMessage pci_get_msi_message (PCIDevice *dev,int vector) +{ +MSIMessage msg; +if (msix_enabled (dev)) { + msg =msix_get_message (dev, vector); + }else if (msi_enabled (dev)) { + msg =msi_get_message (dev, vector); + }else { +/* Should never happen */ +error_report ("%s: unknown interrupt type" , __func__); +abort (); + } +return msg; +} +static const TypeInfo pci_device_type_info = { .name =TYPE_PCI_DEVICE , .parent =TYPE_DEVICE ,
include/hw/pci/pci.h +2 -0
@@ -776,4 +776,6 @@ extern const VMStateDescription vmstate_pci_device; .offset = vmstate_offset_pointer(_state, _field, PCIDevice), \ } +MSIMessage pci_get_msi_message(PCIDevice *dev, int vector); +#endif
kvm-all.c +1 -9
@@ -1186,15 +1186,7 @@ int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev)MSIMessage msg = {0 ,0 };if (dev) { -if (msix_enabled (dev)) { - msg =msix_get_message (dev, vector); - }else if (msi_enabled (dev)) { - msg =msi_get_message (dev, vector); - }else { -/* Should never happen */ -error_report ("%s: unknown interrupt type" , __func__); -abort (); - } + msg =pci_get_msi_message (dev, vector); }if (kvm_gsi_direct_mapping ()) {
target-i386/kvm.c +30 -0
@@ -37,6 +37,7 @@#include "hw/i386/apic_internal.h" #include "hw/i386/apic-msidef.h" #include "hw/i386/intel_iommu.h" +#include "hw/i386/x86-iommu.h" #include "exec/ioport.h" #include "standard-headers/asm-x86/hyperv.h"
@@ -3368,15 +3369,44 @@ struct MSIRouteEntry { static QLIST_HEAD(, MSIRouteEntry) msi_route_list = \QLIST_HEAD_INITIALIZER (msi_route_list); +static void kvm_update_msi_routes_all (void *private,bool global, +uint32_t index,uint32_t mask) +{ +int cnt =0 ; +MSIRouteEntry *entry; +MSIMessage msg; +/* TODO: explicit route update */ + QLIST_FOREACH(entry, &msi_route_list, list) { + cnt++; + msg =pci_get_msi_message (entry->dev, entry->vector); +kvm_irqchip_update_msi_route (kvm_state, entry->virq, + msg, entry->dev); + } +trace_kvm_x86_update_msi_routes (cnt); +} +int kvm_arch_add_msi_route_post (struct kvm_irq_routing_entry *route,int vector,PCIDevice *dev) { +static bool notify_list_inited = false;MSIRouteEntry *entry =g_new0 (MSIRouteEntry,1 ); entry->dev = dev; entry->vector = vector; entry->virq = route->gsi;QLIST_INSERT_HEAD (&msi_route_list, entry, list);trace_kvm_x86_add_msi_route (route->gsi); + +if (!notify_list_inited) { +/* For the first time we do add route, add ourselves into +* IOMMU's IEC notify list if needed. */ +X86IOMMUState *iommu =x86_iommu_get_default (); +if (iommu) { +x86_iommu_iec_register_notifier (iommu, + kvm_update_msi_routes_all, +NULL ); + } + notify_list_inited = true; + }return 0 ; }
trace-events +1 -0
@@ -1914,3 +1914,4 @@ aspeed_vic_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 kvm_x86_fixup_msi_error(uint32_t gsi) "VT-d failed to remap interrupt for GSI %" PRIu32 kvm_x86_add_msi_route(int virq) "Adding route entry for virq %d" kvm_x86_remove_msi_route(int virq) "Removing route entry for virq %d" +kvm_x86_update_msi_routes(int num) "Updated %d MSI routes"
Signature · 1 line
2.4.11
PATCH v6 26/26 kvm-irqchip: do explicit commit when update irq
In reply to Peter XuIn the past, we are doing gsi route commit for each irqchip route update. This is not efficient if we are updating lots of routes in the same time. This patch removes the committing phase in kvm_irqchip_update_msi_route(). Instead, we do explicit commit after all routes updated.
--- hw/i386/kvm/pci-assign.c | 2 ++ hw/misc/ivshmem.c | 1 + hw/vfio/pci.c | 1 + hw/virtio/virtio-pci.c | 1 + include/sysemu/kvm.h | 2 +- kvm-all.c | 2 -- kvm-stub.c | 4 ++++ target-i386/kvm.c | 1 + 8 files changed, 11 insertions(+), 3 deletions(-)
hw/i386/kvm/pci-assign.c +2 -0
@@ -1015,6 +1015,7 @@ static void assigned_dev_update_msi_msg(PCIDevice *pci_dev)kvm_irqchip_update_msi_route (kvm_state, assigned_dev->msi_virq[0 ],msi_get_message (pci_dev,0 ), pci_dev); +kvm_irqchip_commit_routes (kvm_state); }static bool assigned_dev_msix_masked (MSIXTableEntry *entry)
@@ -1601,6 +1602,7 @@ static void assigned_dev_msix_mmio_write(void *opaque, hwaddr addr,if (ret) {error_report ("Error updating irq routing entry (%d)" , ret); } +kvm_irqchip_commit_routes (kvm_state); } } }
hw/misc/ivshmem.c +1 -0
@@ -325,6 +325,7 @@ static int ivshmem_vector_unmask(PCIDevice *dev, unsigned vector,if (ret <0 ) {return ret; } +kvm_irqchip_commit_routes (kvm_state);return kvm_irqchip_add_irqfd_notifier_gsi (kvm_state, n,NULL , v->virq); }
hw/vfio/pci.c +1 -0
@@ -458,6 +458,7 @@ static void vfio_update_kvm_msi_virq(VFIOMSIVector *vector, MSIMessage msg, PCIDevice *pdev) {kvm_irqchip_update_msi_route (kvm_state, vector->virq, msg, pdev); +kvm_irqchip_commit_routes (kvm_state); } static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
hw/virtio/virtio-pci.c +1 -0
@@ -872,6 +872,7 @@ static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,if (ret <0 ) {return ret; } +kvm_irqchip_commit_routes (kvm_state); } }
include/sysemu/kvm.h +1 -1
@@ -367,7 +367,6 @@ int kvm_set_irq(KVMState *s, int irq, int level);int kvm_irqchip_send_msi (KVMState *s,MSIMessage msg);void kvm_irqchip_add_irq_route (KVMState *s,int gsi,int irqchip,int pin); -void kvm_irqchip_commit_routes (KVMState *s);void kvm_put_apic_state (DeviceState *d,struct kvm_lapic_state *kapic);void kvm_get_apic_state (DeviceState *d,struct kvm_lapic_state *kapic);
@@ -490,6 +489,7 @@ static inline void cpu_synchronize_post_init(CPUState *cpu)int kvm_irqchip_add_msi_route (KVMState *s,int vector,PCIDevice *dev);int kvm_irqchip_update_msi_route (KVMState *s,int virq,MSIMessage msg,PCIDevice *dev); +void kvm_irqchip_commit_routes (KVMState *s);void kvm_irqchip_release_virq (KVMState *s,int virq);int kvm_irqchip_add_adapter_route (KVMState *s,AdapterInfo *adapter);
kvm-all.c +0 -2
@@ -1034,8 +1034,6 @@ static int kvm_update_routing_entry(KVMState *s, *entry = *new_entry; -kvm_irqchip_commit_routes (s); -return 0 ; }
kvm-stub.c +4 -0
@@ -131,6 +131,10 @@ int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg,return -ENOSYS ; } +void kvm_irqchip_commit_routes (KVMState *s) +{ +} +int kvm_irqchip_add_adapter_route (KVMState *s,AdapterInfo *adapter) {return -ENOSYS ;
target-i386/kvm.c +1 -0
@@ -3382,6 +3382,7 @@ static void kvm_update_msi_routes_all(void *private, bool global,kvm_irqchip_update_msi_route (kvm_state, entry->virq, msg, entry->dev); } +kvm_irqchip_commit_routes (kvm_state);trace_kvm_x86_update_msi_routes (cnt); }
Signature · 1 line
2.4.11
Quoted 18 lines · Peter Xu
Hi, all,
This is v6 for Intel IOMMU IR support. This series introduced quite a few new patches based on v5. Sorry for that (Yes, Jan is contributing to it as well, though most of which are really good ideas for me :). Hopefully we can get its convergence in this version.
To make the review easier, I tried to keep all the existing patches and indexes (also, this is easier for me too to do the modifications, and logically I feel this make more sense and clean, please let me know if I am wrong). Patches 1-18 are v5 patches, and patches 19-26 are newly added patches.
All the new patches may need more review, many of them are outside Intel IOMMU scope, and touching other part of codes, which I am still not very sure about.
Quoted 5 lines
Testing is only covering basic smoke test for the following matrix:
- IR enabled/disable
- kernel irqchip off/split
- network device: tap with/without vhost, e1000
Again... Missing link:
https://github.com/xzpeter/qemu vtd-intr-v6
Thanks,
-- peterx
2016-05-05 11:25+0800, Peter Xu:
Quoted 18 lines
Hi, all,
This is v6 for Intel IOMMU IR support. This series introduced quite a few new patches based on v5. Sorry for that (Yes, Jan is contributing to it as well, though most of which are really good ideas for me :). Hopefully we can get its convergence in this version.
To make the review easier, I tried to keep all the existing patches and indexes (also, this is easier for me too to do the modifications, and logically I feel this make more sense and clean, please let me know if I am wrong). Patches 1-18 are v5 patches, and patches 19-26 are newly added patches.
All the new patches may need more review, many of them are outside Intel IOMMU scope, and touching other part of codes, which I am still not very sure about.
Quoted 5 lines
Testing is only covering basic smoke test for the following matrix:
- IR enabled/disable
- kernel irqchip off/split
- network device: tap with/without vhost, e1000
I noticed a bug with RHEL7 linux guest, 3.10.0-390.el7.x86_64. Kernel complains about queued invalidation and fails to boot:
virtio-pci 0000:00:01.0: virtio_pci: leaving for legacy driver
DMAR-IR: Requested mask 1 exceeds the max invalidation handle mask value 0
DMAR-IR: Unable to allocate 2 IRTE for PCI 0000:00:01.0
DMAR-IR: Requested mask 1 exceeds the max invalidation handle mask value 0
DMAR-IR: Unable to allocate 2 IRTE for PCI 0000:00:01.0 dracut-initqueue[263]: Warning: dracut-initqueue timeout - starting timeout scripts dracut-initqueue[263]: Warning: Could not boot. dracut-initqueue[263]: Warning: /dev/mapper/rhel-root does not exist dracut-initqueue[263]: Warning: /dev/rhel/root does not exist dracut-initqueue[263]: Warning: /dev/rhel/swap does not exist
The patch below helps,
---8<---
From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= <[email protected]>
Date: Mon, 9 May 2016 19:04:56 +0200
Subject: [PATCH] intel_iommu: support all masks in interrupt entry cache
invalidation
Linux guests do not gracefully handle cases when the invalidation mask they wanted is not supported, probably because real hardware always allowed all.
We can just say that all 16 masks are supported, because both ioapic_iec_notifier and kvm_update_msi_routes_all invalidate all caches.
--- hw/i386/intel_iommu.c | 2 +- hw/i386/intel_iommu_internal.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +1 -1
@@ -2359,7 +2359,7 @@ static void vtd_init(IntelIOMMUState *s) s->ecap =VTD_ECAP_QI |VTD_ECAP_IRO ;if (ms->iommu_intr) { - s->ecap |=VTD_ECAP_IR |VTD_ECAP_EIM ; + s->ecap |=VTD_ECAP_IR |VTD_ECAP_EIM |VTD_ECAP_MHVM ; }vtd_reset_context_cache (s);
hw/i386/intel_iommu_internal.h +1 -0
@@ -186,6 +186,7 @@/* Interrupt Remapping support */ #define VTD_ECAP_IR (1ULL << 3)#define VTD_ECAP_EIM (1ULL << 4) +#define VTD_ECAP_MHMV (15ULL << 20)/* CAP_REG */ /* (offset >> 4) << 24 */
2016-05-09 19:15+0200, Radim Krčmář:
Quoted 16 lines
From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= <[email protected]>
Date: Mon, 9 May 2016 19:04:56 +0200
Subject: [PATCH]intel_iommu: support all masks in interrupt entry cache
invalidationLinux guests do not gracefully handle cases when the invalidation mask they wanted is not supported, probably because real hardware always allowed all.
We can just say that all 16 masks are supported, because both
ioapic_iec_notifierandkvm_update_msi_routes_allinvalidate all caches.Signed-off-by Radim Krčmář <[email protected]>---
hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 2362
Quoted 5 lines
hw/i386/intel_iommu.c +1
-1@@ -2359,7 +2359,7 @@ static void vtd_init(IntelIOMMUState *s) s->ecap =VTD_ECAP_QI |VTD_ECAP_IRO ;if (ms->iommu_intr) { - s->ecap |=VTD_ECAP_IR |VTD_ECAP_EIM ; + s->ecap |=VTD_ECAP_IR |VTD_ECAP_EIM |VTD_ECAP_MHVM ;
Eh, I really cannot spell ... this one should read VTD_ECAP_MHMV. (... or compile after a last-second renaming.)
Quoted 3 lines
hw/i386/intel_iommu_internal.h +1
-0@@ -186,6 +186,7 @@ +#define VTD_ECAP_MHMV (15ULL << 20)
Quoted 19 lines · Radim Krčmář
2016-05-09 19:15+0200, Radim Krčmář:
Quoted 18 lines
From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= <[email protected]>
Date: Mon, 9 May 2016 19:04:56 +0200
Subject: [PATCH]intel_iommu: support all masks in interrupt entry cache
invalidationLinux guests do not gracefully handle cases when the invalidation mask they wanted is not supported, probably because real hardware always allowed all.
We can just say that all 16 masks are supported, because both
ioapic_iec_notifierandkvm_update_msi_routes_allinvalidate all caches.Signed-off-by Radim Krčmář <[email protected]>---
hw/i386/intel_iommu.c +0
-0@@ -2359,7 +2359,7 @@ static void vtd_init(IntelIOMMUState *s) s->ecap =VTD_ECAP_QI |VTD_ECAP_IRO ;
Quoted 6 lines
hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 2362
Quoted 3 lines
hw/i386/intel_iommu.c +1
-1if (ms->iommu_intr) { - s->ecap |=VTD_ECAP_IR |VTD_ECAP_EIM ; + s->ecap |=VTD_ECAP_IR |VTD_ECAP_EIM |VTD_ECAP_MHVM ;Eh, I really cannot spell ... this one should read
VTD_ECAP_MHMV. (... or compile after a last-second renaming.)
Thanks, Radim. If you would not mind, I'll pick this one up and put it in v7. :)
-- peterx