i386: Interrupt remapping support for VT-d
PATCH 2/2 i386: Interrupt remapping support for VT-d
From: Jan Kiszka <[email protected]>
Still a bit hacky, unconditionally enabled (must become opt-in, not available with in-kernel irqchip), not reporting faults properly - but it works! And revealed a Linux bug [1]
[1] http://thread.gmane.org/gmane.linux.kernel/1766261
--- hw/i386/acpi-build.c | 28 ++++++- hw/i386/intel_iommu.c | 162 ++++++++++++++++++++++++++++++++++++++++- hw/i386/intel_iommu_internal.h | 27 +++++++ hw/intc/apic.c | 1 + hw/pci-host/q35.c | 11 +++ include/hw/acpi/acpi-defs.h | 22 ++++++ include/hw/i386/intel_iommu.h | 7 ++ 7 files changed, 252 insertions(+), 6 deletions(-)
hw/i386/acpi-build.c +25 -3
@@ -68,6 +68,9 @@#define ACPI_BUILD_TABLE_SIZE 0x20000 +#define ACPI_BUILD_IOAPIC_ID 0x0 +#define ACPI_BUILD_HPET_ID 0x0 +/* #define DEBUG_ACPI_BUILD */ #ifdef DEBUG_ACPI_BUILD #define ACPI_BUILD_DPRINTF (fmt, ...) \
@@ -392,7 +395,6 @@ build_madt(GArray *table_data, GArray *linker, AcpiCpuInfo *cpu) 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 );
@@ -2302,6 +2304,7 @@ build_hpet(GArray *table_data, GArray *linker)*/ hpet->timer_block_id =cpu_to_le32 (0x8086a201 ); hpet->addr.address =cpu_to_le64 (HPET_BASE ); + hpet->hpet_number =ACPI_BUILD_HPET_ID ;build_header (linker, table_data, (void *)hpet,"HPET" ,sizeof (*hpet),1 ,NULL ,NULL ); }
@@ -2496,19 +2499,38 @@ build_dmar_q35(GArray *table_data, GArray *linker)AcpiTableDmar *dmar;AcpiDmarHardwareUnit *drhd; +AcpiDmarDeviceScope *dev_scope; 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 =ACPI_DMAR_INTR_REMAP ;/* 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) + (sizeof (*dev_scope) +2 ) *2 ); drhd->flags =ACPI_DMAR_INCLUDE_PCI_ALL ; drhd->pci_segment =cpu_to_le16 (0 ); drhd->address =cpu_to_le64 (Q35_HOST_BRIDGE_IOMMU_ADDR ); +/* Device Scope structures for IOAPIC */ + dev_scope =acpi_data_push (table_data,sizeof (*dev_scope) +2 ); + dev_scope->type =ACPI_DMAR_SCOPE_TYPE_IOAPIC ; + dev_scope->length =sizeof (*dev_scope) +2 ; + dev_scope->enumeration_id =ACPI_BUILD_IOAPIC_ID ; + dev_scope->start_bus_number =Q35_PSEUDO_BUS_PLATFORM ; + dev_scope->path[0 ] =PCI_SLOT (Q35_PSEUDO_DEVFN_IOAPIC ); + dev_scope->path[1 ] =PCI_FUNC (Q35_PSEUDO_DEVFN_IOAPIC ); + +/* Device Scope structures for HPET */ + dev_scope =acpi_data_push (table_data,sizeof (*dev_scope) +2 ); + dev_scope->type =ACPI_DMAR_SCOPE_TYPE_HPET ; + dev_scope->length =sizeof (*dev_scope) +2 ; + dev_scope->enumeration_id =ACPI_BUILD_HPET_ID ; + dev_scope->start_bus_number =Q35_PSEUDO_BUS_PLATFORM ; + dev_scope->path[0 ] =PCI_SLOT (Q35_PSEUDO_DEVFN_HPET ); + dev_scope->path[1 ] =PCI_FUNC (Q35_PSEUDO_DEVFN_HPET ); +build_header (linker, table_data, (void *)(table_data->data + dmar_start),"DMAR" , table_data->len - dmar_start,1 ,NULL ,NULL ); }
hw/i386/intel_iommu.c +159 -3
226 lines not on this page · raw message
hw/i386/intel_iommu_internal.h +27 -0
@@ -173,8 +173,10 @@#define VTD_RTADDR_ADDR_MASK (VTD_HAW_MASK ^ 0xfffULL)/* ECAP_REG */ +#define VTD_ECAP_MHMV (0xf << 20)/* (offset >> 4) << 8 */ #define VTD_ECAP_IRO (DMAR_IOTLB_REG_OFFSET << 4) +#define VTD_ECAP_IR (1ULL << 3)#define VTD_ECAP_QI (1ULL << 1)/* CAP_REG */
@@ -214,6 +216,11 @@/* ICS_REG */ #define VTD_ICS_IWC 1UL +/* IRTA_REG */ +#define VTD_IRTA_SIZE_MASK (0xf) +#define VTD_IRTA_EIME (1ULL << 11) +#define VTD_IRTA_ADDR_MASK (VTD_HAW_MASK ^ 0xfffULL) +/* IECTL_REG */ #define VTD_IECTL_IM (1UL << 31)#define VTD_IECTL_IP (1UL << 30)
@@ -286,6 +293,7 @@ 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_INT 0x4#define VTD_INV_DESC_WAIT 0x5/* Invalidation Wait Descriptor */ #define VTD_INV_DESC_NONE 0/* Not an Invalidate Descriptor */
@@ -388,4 +396,23 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_SL_PT_BASE_ADDR_MASK (~(VTD_PAGE_SIZE - 1) & VTD_HAW_MASK)#define VTD_SL_IGN_COM 0xbff0000000000000ULL +/* Interrupt Remapping Table Entry */ +struct VTDIntRemapEntry { +uint64_t lo; +uint64_t hi; +}; +typedef struct VTDIntRemapEntry VTDIntRemapEntry ; + +/* IRTE low word */ +#define VTD_IRTE_LO_P (val) (((val) >> 0) & 0x01) +#define VTD_IRTE_LO_DEST_MODE (val) (((val) >> 2) & 0x01) +#define VTD_IRTE_LO_REDIR_HINT (val) (((val) >> 3) & 0x01) +#define VTD_IRTE_LO_TRIGGER_MODE (val) (((val) >> 4) & 0x01) +#define VTD_IRTE_LO_DELIVERY (val) (((val) >> 5) & 0x07) +#define VTD_IRTE_LO_VECTOR (val) (((val) >> 16) & 0xff) +#define VTD_IRTE_LO_DEST (val) (((val) >> 32) & 0xffffffff) + +/* IRTE high word */ +#define VTD_IRTE_HI_SID (val) (((val) >> 0) & 0xffff) +#endif
hw/intc/apic.c +1 -0
@@ -846,6 +846,7 @@ static void msi_region_write(void *opaque, hwaddr addr, uint64_t data,uint8_t delivery = (data >>MSI_DATA_DELIVERY_MODE_SHIFT ) &0x7 ;/* FIXME: Ignoring redirection hint. */ +/*printf("APIC: %08lx:%04lx\n", addr, data);*/ apic_deliver_irq (dest, dest_mode, delivery, vector, trigger_mode); }
hw/pci-host/q35.c +11 -0
@@ -431,6 +431,17 @@ static AddressSpace *q35_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)assert (0 <= devfn && devfn <=VTD_PCI_DEVFN_MAX ); vtd_as =vtd_find_add_as (s, bus, devfn); + +memory_region_init_iommu (&vtd_as->iommu,OBJECT (s), + &s->iommu_ops,"intel_iommu" ,UINT64_MAX ); +address_space_init (&vtd_as->as, + &vtd_as->iommu,"intel_iommu" ); +memory_region_init_io (&vtd_as->int_remap_region,OBJECT (s), + &vtd_int_remap_ops, vtd_as, +"intel_int_remap" ,UINT64_MAX ); +address_space_init (&vtd_as->int_remap_as, + &vtd_as->int_remap_region, +"intel_int_remap" );return &vtd_as->as; }
include/hw/acpi/acpi-defs.h +22 -0
@@ -570,4 +570,26 @@ typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit;/* Masks for Flags field above */ #define ACPI_DMAR_INCLUDE_PCI_ALL 1 +/* DMAR Device Scope structures */ +struct AcpiDmarDeviceScope { +uint8_t type; +uint8_t length; +uint16_t reserved; +uint8_t enumeration_id; +uint8_t start_bus_number; +uint8_t path[0 ]; +}QEMU_PACKED ; +typedef struct AcpiDmarDeviceScope AcpiDmarDeviceScope ; + +/* Values for type in struct AcpiDmarDeviceScope */ +enum { +ACPI_DMAR_SCOPE_TYPE_NOT_USED =0 , +ACPI_DMAR_SCOPE_TYPE_ENDPOINT =1 , +ACPI_DMAR_SCOPE_TYPE_BRIDGE =2 , +ACPI_DMAR_SCOPE_TYPE_IOAPIC =3 , +ACPI_DMAR_SCOPE_TYPE_HPET =4 , +ACPI_DMAR_SCOPE_TYPE_ACPI =5 , +ACPI_DMAR_SCOPE_TYPE_RESERVED =6 /* Reserved for future use */ +}; +#endif
include/hw/i386/intel_iommu.h +7 -0
@@ -70,6 +70,8 @@ struct VTDAddressSpace {uint8_t devfn;AddressSpace as;MemoryRegion iommu; +AddressSpace int_remap_as; +MemoryRegion int_remap_region;IntelIOMMUState *iommu_state;VTDContextCacheEntry context_cache_entry; };
@@ -109,6 +111,10 @@ struct IntelIOMMUState {bool qi_enabled;/* Set if the QI is enabled */ uint8_t iq_last_desc_type;/* The type of last completed descriptor */ +dma_addr_t irta; +unsigned int irt_size; +bool ir_enabled; +/* The index of the Fault Recording Register to be used next. * Wraps around from N-1 to 0, where N is the number of FRCD_REG. */
@@ -125,6 +131,7 @@ struct IntelIOMMUState {VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX ];/* VTDBus objects indexed by bus number */ }; +extern const MemoryRegionOps vtd_int_remap_ops;/* Find the VTD Address space associated with the given bus pointer, * create a new one if none exists */
Signature · 1 line
2.7.2
Quoted 5 lines · Rita Sinha
From: Jan Kiszka <[email protected]>
Still a bit hacky, unconditionally enabled (must become opt-in, not available with in-kernel irqchip), not reporting faults properly - but it works! And revealed a Linux bug [1]
If the patch is to be merged finally, shall we better add a parameter to disable this feature for people do not need this? Also, shall we make sure:
- make sure patches' in-reply-to are correct (so that it's in a series, as mentioned by Eric before)
- remove useless lines like "/* printf(...) */"
- add one-line subject for each patch (possibly)?
- ...
[...]
Quoted 9 lines
hw/pci-host/q35.c +1
-0@@ -431,6 +431,17 @@ static AddressSpace *q35_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)assert (0 <= devfn && devfn <=VTD_PCI_DEVFN_MAX ); vtd_as =vtd_find_add_as (s, bus, devfn); +
hw/pci-host/q35.c static AddressSpace *q35_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) · line 444
Quoted 10 lines
hw/pci-host/q35.c +10
-0+memory_region_init_iommu (&vtd_as->iommu,OBJECT (s), + &s->iommu_ops,"intel_iommu" ,UINT64_MAX ); +address_space_init (&vtd_as->as, + &vtd_as->iommu,"intel_iommu" ); +memory_region_init_io (&vtd_as->int_remap_region,OBJECT (s), + &vtd_int_remap_ops, vtd_as, +"intel_int_remap" ,UINT64_MAX ); +address_space_init (&vtd_as->int_remap_as, + &vtd_as->int_remap_region, +"intel_int_remap" );
One more thing... vtd_as->{as|iommu} should have been inited in vtd_find_add_as() already.
Peter
Quoted 9 lines · Peter Xu
Quoted 5 lines · Rita Sinha
From: Jan Kiszka <[email protected]>
Still a bit hacky, unconditionally enabled (must become opt-in, not available with in-kernel irqchip), not reporting faults properly - but it works! And revealed a Linux bug [1]
If the patch is to be merged finally, shall we better add a parameter to disable this feature for people do not need this?
Yes, we need a switch. The original IOMMU in the Q35 chipset doesn't support IR, and already to be emulation-wise precise, we should allow to user to turn this off (or on).
Quoted 17 lines
Also, shall we make sure:
- make sure patches' in-reply-to are correct (so that it's in a series, as mentioned by Eric before)
- remove useless lines like "/* printf(...) */"
- add one-line subject for each patch (possibly)?
- ...[...]
Quoted 7 lines
hw/pci-host/q35.c +0
-0@@ -431,6 +431,17 @@ static AddressSpace *q35_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)assert (0 <= devfn && devfn <=VTD_PCI_DEVFN_MAX );
Quoted 16 lines
hw/pci-host/q35.c static AddressSpace *q35_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) · line 444
Quoted 12 lines
hw/pci-host/q35.c +11
-0vtd_as =vtd_find_add_as (s, bus, devfn); + +memory_region_init_iommu (&vtd_as->iommu,OBJECT (s), + &s->iommu_ops,"intel_iommu" ,UINT64_MAX ); +address_space_init (&vtd_as->as, + &vtd_as->iommu,"intel_iommu" ); +memory_region_init_io (&vtd_as->int_remap_region,OBJECT (s), + &vtd_int_remap_ops, vtd_as, +"intel_int_remap" ,UINT64_MAX ); +address_space_init (&vtd_as->int_remap_as, + &vtd_as->int_remap_region, +"intel_int_remap" );One more thing... vtd_as->{as|iommu} should have been inited in
vtd_find_add_as()already.
Good point. Logical merge artefact.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA ITP SES-DE Corporate Competence Center Embedded Linux