pci, pc fixes, features
The following changes since commit 187de915e8d06aaf82be206aebc551c82bf0670c:
pcie: fix trailing whitespace (2014-08-25 00:16:07 +0200)
are available in the git repository at:
git://git.kernel.org/pub/scm/virt/kvm/mst/qemu.git tags/for_upstream
for you to fetch changes up to aad4dce934649b3a398396fc2a76f215bb194ea4:
vhost_net: start/stop guest notifiers properly (2014-09-02 17:33:37 +0300)
----------------------------------------------------------------
pci, pc fixes, features
A bunch of bugfixes - these will make sense for 2.1.1
Initial Intel IOMMU support.
----------------------------------------------------------------
Gonglei (1):
ioh3420: remove unused ioh3420_init() declaration
Jason Wang (1):
vhost_net: start/stop guest notifiers properly
Knut Omang (1):
pci: avoid losing config updates to MSI/MSIX cap regs
Le Tan (8):
iommu: add is_write as a parameter to the translate function of MemoryRegionIOMMUOps
intel-iommu: introduce Intel IOMMU (VT-d) emulation
intel-iommu: add DMAR table to ACPI tables
intel-iommu: add Intel IOMMU emulation to q35 and add a machine option "iommu" as a switch
intel-iommu: fix coding style issues around in q35.c and machine.c
intel-iommu: add supports for queued invalidation interface
intel-iommu: add context-cache to cache context-entry
intel-iommu: add IOTLB using hash table
Michael S. Tsirkin (2):
vhost_net: cleanup start/stop condition
virtio-net: don't run bh on vm stopped
hw/i386/acpi-defs.h | 40 + hw/i386/intel_iommu_internal.h | 389 ++++++++ hw/pci-bridge/ioh3420.h | 4 - include/exec/memory.h | 2 +- include/hw/boards.h | 1 + include/hw/i386/intel_iommu.h | 120 +++ include/hw/pci-host/q35.h | 2 + exec.c | 2 +- hw/alpha/typhoon.c | 3 +- hw/core/machine.c | 27 +- hw/i386/acpi-build.c | 39 + hw/i386/intel_iommu.c | 1963 ++++++++++++++++++++++++++++++++++++++++ hw/net/vhost_net.c | 39 +- hw/net/virtio-net.c | 14 +- hw/pci-host/apb.c | 3 +- hw/pci-host/q35.c | 58 +- hw/pci/pci.c | 7 +- hw/ppc/spapr_iommu.c | 3 +- hw/virtio/vhost.c | 2 - vl.c | 4 + hw/i386/Makefile.objs | 1 + qemu-options.hx | 5 +- 22 files changed, 2683 insertions(+), 45 deletions(-) create mode 100644 hw/i386/intel_iommu_internal.h create mode 100644 include/hw/i386/intel_iommu.h create mode 100644 hw/i386/intel_iommu.c
[Qemu-devel] [PULL 02/13] intel-iommu: introduce Intel IOMMU (VT-d) emulation
From: Le Tan <[email protected]>
Add support for emulating Intel IOMMU according to the VT-d specification for the q35 chipset machine. Implement the logics for DMAR (DMA remapping) without PASID support. The emulation supports register-based invalidation and primary fault logging.
--- hw/i386/intel_iommu_internal.h | 333 +++++++++++ include/hw/i386/intel_iommu.h | 89 +++ hw/i386/intel_iommu.c | 1257 ++++++++++++++++++++++++++++++++++++++++ hw/i386/Makefile.objs | 1 + 4 files changed, 1680 insertions(+) create mode 100644 hw/i386/intel_iommu_internal.h create mode 100644 include/hw/i386/intel_iommu.h create mode 100644 hw/i386/intel_iommu.c
hw/i386/intel_iommu_internal.h new file +333 -0
@@ -0,0 +1,333 @@ +/* +* QEMU emulation of an Intel IOMMU (VT-d) +* (DMA Remapping device) +* +* Copyright (C) 2013 Knut Omang, Oracle <[email protected]> +* Copyright (C) 2014 Le Tan, <[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/>. +* +* Lots of defines copied from kernel/include/linux/intel-iommu.h: +* Copyright (C) 2006-2008 Intel Corporation +* Author: Ashok Raj <[email protected]> +* Author: Anil S Keshavamurthy <[email protected]> +* +*/ + +#ifndef HW_I386_INTEL_IOMMU_INTERNAL_H +#define HW_I386_INTEL_IOMMU_INTERNAL_H +#include "hw/i386/intel_iommu.h" + +/* +* Intel IOMMU register specification +*/ +#define DMAR_VER_REG 0x0/* Arch version supported by this IOMMU */ +#define DMAR_CAP_REG 0x8/* Hardware supported capabilities */ +#define DMAR_CAP_REG_HI 0xc/* High 32-bit of DMAR_CAP_REG */ +#define DMAR_ECAP_REG 0x10/* Extended capabilities supported */ +#define DMAR_ECAP_REG_HI 0X14 +#define DMAR_GCMD_REG 0x18/* Global command */ +#define DMAR_GSTS_REG 0x1c/* Global status */ +#define DMAR_RTADDR_REG 0x20/* Root entry table */ +#define DMAR_RTADDR_REG_HI 0X24 +#define DMAR_CCMD_REG 0x28/* Context command */ +#define DMAR_CCMD_REG_HI 0x2c +#define DMAR_FSTS_REG 0x34/* Fault status */ +#define DMAR_FECTL_REG 0x38/* Fault control */ +#define DMAR_FEDATA_REG 0x3c/* Fault event interrupt data */ +#define DMAR_FEADDR_REG 0x40/* Fault event interrupt addr */ +#define DMAR_FEUADDR_REG 0x44/* Upper address */ +#define DMAR_AFLOG_REG 0x58/* Advanced fault control */ +#define DMAR_AFLOG_REG_HI 0X5c +#define DMAR_PMEN_REG 0x64/* Enable protected memory region */ +#define DMAR_PLMBASE_REG 0x68/* PMRR low addr */ +#define DMAR_PLMLIMIT_REG 0x6c/* PMRR low limit */ +#define DMAR_PHMBASE_REG 0x70/* PMRR high base addr */ +#define DMAR_PHMBASE_REG_HI 0X74 +#define DMAR_PHMLIMIT_REG 0x78/* PMRR high limit */ +#define DMAR_PHMLIMIT_REG_HI 0x7c +#define DMAR_IQH_REG 0x80/* Invalidation queue head */ +#define DMAR_IQH_REG_HI 0X84 +#define DMAR_IQT_REG 0x88/* Invalidation queue tail */ +#define DMAR_IQT_REG_HI 0X8c +#define DMAR_IQA_REG 0x90/* Invalidation queue addr */ +#define DMAR_IQA_REG_HI 0x94 +#define DMAR_ICS_REG 0x9c/* Invalidation complete status */ +#define DMAR_IRTA_REG 0xb8/* Interrupt remapping table addr */ +#define DMAR_IRTA_REG_HI 0xbc +#define DMAR_IECTL_REG 0xa0/* Invalidation event control */ +#define DMAR_IEDATA_REG 0xa4/* Invalidation event data */ +#define DMAR_IEADDR_REG 0xa8/* Invalidation event address */ +#define DMAR_IEUADDR_REG 0xac/* Invalidation event address */ +#define DMAR_PQH_REG 0xc0/* Page request queue head */ +#define DMAR_PQH_REG_HI 0xc4 +#define DMAR_PQT_REG 0xc8/* Page request queue tail*/ +#define DMAR_PQT_REG_HI 0xcc +#define DMAR_PQA_REG 0xd0/* Page request queue address */ +#define DMAR_PQA_REG_HI 0xd4 +#define DMAR_PRS_REG 0xdc/* Page request status */ +#define DMAR_PECTL_REG 0xe0/* Page request event control */ +#define DMAR_PEDATA_REG 0xe4/* Page request event data */ +#define DMAR_PEADDR_REG 0xe8/* Page request event address */ +#define DMAR_PEUADDR_REG 0xec/* Page event upper address */ +#define DMAR_MTRRCAP_REG 0x100/* MTRR capability */ +#define DMAR_MTRRCAP_REG_HI 0x104 +#define DMAR_MTRRDEF_REG 0x108/* MTRR default type */ +#define DMAR_MTRRDEF_REG_HI 0x10c + +/* IOTLB registers */ +#define DMAR_IOTLB_REG_OFFSET 0xf0/* Offset to the IOTLB registers */ +#define DMAR_IVA_REG DMAR_IOTLB_REG_OFFSET/* Invalidate address */ +#define DMAR_IVA_REG_HI (DMAR_IVA_REG + 4) +/* IOTLB invalidate register */ +#define DMAR_IOTLB_REG (DMAR_IOTLB_REG_OFFSET + 0x8) +#define DMAR_IOTLB_REG_HI (DMAR_IOTLB_REG + 4) + +/* FRCD */ +#define DMAR_FRCD_REG_OFFSET 0x220/* Offset to the fault recording regs */ +/* NOTICE: If you change the DMAR_FRCD_REG_NR, please remember to change the +* DMAR_REG_SIZE in include/hw/i386/intel_iommu.h. +* #define DMAR_REG_SIZE (DMAR_FRCD_REG_OFFSET + 16 * DMAR_FRCD_REG_NR) +*/ +#define DMAR_FRCD_REG_NR 1ULL/* Num of fault recording regs */ + +#define DMAR_FRCD_REG_0_0 0x220/* The 0th fault recording regs */ +#define DMAR_FRCD_REG_0_1 0x224 +#define DMAR_FRCD_REG_0_2 0x228 +#define DMAR_FRCD_REG_0_3 0x22c + +/* Interrupt Address Range */ +#define VTD_INTERRUPT_ADDR_FIRST 0xfee00000ULL +#define VTD_INTERRUPT_ADDR_LAST 0xfeefffffULL + +/* IOTLB_REG */ +#define VTD_TLB_GLOBAL_FLUSH (1ULL << 60)/* Global invalidation */ +#define VTD_TLB_DSI_FLUSH (2ULL << 60)/* Domain-selective */ +#define VTD_TLB_PSI_FLUSH (3ULL << 60)/* Page-selective */ +#define VTD_TLB_FLUSH_GRANU_MASK (3ULL << 60) +#define VTD_TLB_GLOBAL_FLUSH_A (1ULL << 57) +#define VTD_TLB_DSI_FLUSH_A (2ULL << 57) +#define VTD_TLB_PSI_FLUSH_A (3ULL << 57) +#define VTD_TLB_FLUSH_GRANU_MASK_A (3ULL << 57) +#define VTD_TLB_IVT (1ULL << 63) + +/* GCMD_REG */ +#define VTD_GCMD_TE (1UL << 31) +#define VTD_GCMD_SRTP (1UL << 30) +#define VTD_GCMD_SFL (1UL << 29) +#define VTD_GCMD_EAFL (1UL << 28) +#define VTD_GCMD_WBF (1UL << 27) +#define VTD_GCMD_QIE (1UL << 26) +#define VTD_GCMD_IRE (1UL << 25) +#define VTD_GCMD_SIRTP (1UL << 24) +#define VTD_GCMD_CFI (1UL << 23) + +/* GSTS_REG */ +#define VTD_GSTS_TES (1UL << 31) +#define VTD_GSTS_RTPS (1UL << 30) +#define VTD_GSTS_FLS (1UL << 29) +#define VTD_GSTS_AFLS (1UL << 28) +#define VTD_GSTS_WBFS (1UL << 27) +#define VTD_GSTS_QIES (1UL << 26) +#define VTD_GSTS_IRES (1UL << 25) +#define VTD_GSTS_IRTPS (1UL << 24) +#define VTD_GSTS_CFIS (1UL << 23) + +/* CCMD_REG */ +#define VTD_CCMD_ICC (1ULL << 63) +#define VTD_CCMD_GLOBAL_INVL (1ULL << 61) +#define VTD_CCMD_DOMAIN_INVL (2ULL << 61) +#define VTD_CCMD_DEVICE_INVL (3ULL << 61) +#define VTD_CCMD_CIRG_MASK (3ULL << 61) +#define VTD_CCMD_GLOBAL_INVL_A (1ULL << 59) +#define VTD_CCMD_DOMAIN_INVL_A (2ULL << 59) +#define VTD_CCMD_DEVICE_INVL_A (3ULL << 59) +#define VTD_CCMD_CAIG_MASK (3ULL << 59) + +/* RTADDR_REG */ +#define VTD_RTADDR_RTT (1ULL << 11) +#define VTD_RTADDR_ADDR_MASK (VTD_HAW_MASK ^ 0xfffULL) + +/* ECAP_REG */ +/* (offset >> 4) << 8 */ +#define VTD_ECAP_IRO (DMAR_IOTLB_REG_OFFSET << 4) +#define VTD_ECAP_QI (1ULL << 1) + +/* CAP_REG */ +/* (offset >> 4) << 24 */ +#define VTD_CAP_FRO (DMAR_FRCD_REG_OFFSET << 20) +#define VTD_CAP_NFR ((DMAR_FRCD_REG_NR - 1) << 40) +#define VTD_DOMAIN_ID_SHIFT 16/* 16-bit domain id for 64K domains */ +#define VTD_CAP_ND (((VTD_DOMAIN_ID_SHIFT - 4) / 2) & 7ULL) +#define VTD_MGAW 39/* Maximum Guest Address Width */ +#define VTD_CAP_MGAW (((VTD_MGAW - 1) & 0x3fULL) << 16) + +/* Supported Adjusted Guest Address Widths */ +#define VTD_CAP_SAGAW_SHIFT 8 +#define VTD_CAP_SAGAW_MASK (0x1fULL << VTD_CAP_SAGAW_SHIFT) +/* 39-bit AGAW, 3-level page-table */ +#define VTD_CAP_SAGAW_39bit (0x2ULL << VTD_CAP_SAGAW_SHIFT) +/* 48-bit AGAW, 4-level page-table */ +#define VTD_CAP_SAGAW_48bit (0x4ULL << VTD_CAP_SAGAW_SHIFT) +#define VTD_CAP_SAGAW VTD_CAP_SAGAW_39bit + +/* IQT_REG */ +#define VTD_IQT_QT (val) (((val) >> 4) & 0x7fffULL) + +/* IQA_REG */ +#define VTD_IQA_IQA_MASK (VTD_HAW_MASK ^ 0xfffULL) +#define VTD_IQA_QS 0x7ULL + +/* IQH_REG */ +#define VTD_IQH_QH_SHIFT 4 +#define VTD_IQH_QH_MASK 0x7fff0ULL + +/* ICS_REG */ +#define VTD_ICS_IWC 1UL + +/* IECTL_REG */ +#define VTD_IECTL_IM (1UL << 31) +#define VTD_IECTL_IP (1UL << 30) + +/* FSTS_REG */ +#define VTD_FSTS_FRI_MASK 0xff00UL +#define VTD_FSTS_FRI (val) ((((uint32_t)(val)) << 8) & VTD_FSTS_FRI_MASK) +#define VTD_FSTS_IQE (1UL << 4) +#define VTD_FSTS_PPF (1UL << 1) +#define VTD_FSTS_PFO 1UL + +/* FECTL_REG */ +#define VTD_FECTL_IM (1UL << 31) +#define VTD_FECTL_IP (1UL << 30) + +/* Fault Recording Register */ +/* For the high 64-bit of 128-bit */ +#define VTD_FRCD_F (1ULL << 63) +#define VTD_FRCD_T (1ULL << 62) +#define VTD_FRCD_FR (val) (((val) & 0xffULL) << 32) +#define VTD_FRCD_SID_MASK 0xffffULL +#define VTD_FRCD_SID (val) ((val) & VTD_FRCD_SID_MASK) +/* For the low 64-bit of 128-bit */ +#define VTD_FRCD_FI (val) ((val) & (((1ULL << VTD_MGAW) - 1) ^ 0xfffULL)) + +/* DMA Remapping Fault Conditions */ +typedef enum VTDFaultReason { +VTD_FR_RESERVED =0 ,/* Reserved for Advanced Fault logging */ +VTD_FR_ROOT_ENTRY_P =1 ,/* The Present(P) field of root-entry is 0 */ +VTD_FR_CONTEXT_ENTRY_P ,/* The Present(P) field of context-entry is 0 */ +VTD_FR_CONTEXT_ENTRY_INV ,/* Invalid programming of a context-entry */ +VTD_FR_ADDR_BEYOND_MGAW ,/* Input-address above (2^x-1) */ +VTD_FR_WRITE ,/* No write permission */ +VTD_FR_READ ,/* No read permission */ +/* Fail to access a second-level paging entry (not SL_PML4E) */ +VTD_FR_PAGING_ENTRY_INV , +VTD_FR_ROOT_TABLE_INV ,/* Fail to access a root-entry */ +VTD_FR_CONTEXT_TABLE_INV ,/* Fail to access a context-entry */ +/* Non-zero reserved field in a present root-entry */ +VTD_FR_ROOT_ENTRY_RSVD , +/* Non-zero reserved field in a present context-entry */ +VTD_FR_CONTEXT_ENTRY_RSVD , +/* Non-zero reserved field in a second-level paging entry with at lease one +* Read(R) and Write(W) or Execute(E) field is Set. +*/ +VTD_FR_PAGING_ENTRY_RSVD , +/* Translation request or translated request explicitly blocked dut to the +* programming of the Translation Type (T) field in the present +* context-entry. +*/ +VTD_FR_CONTEXT_ENTRY_TT , +/* 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. +*/ +VTD_FR_RESERVED_ERR , +VTD_FR_MAX ,/* Guard */ +}VTDFaultReason ; + +/* Masks for Queued Invalidation Descriptor */ +#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_WAIT 0x5/* Invalidation Wait Descriptor */ +#define VTD_INV_DESC_NONE 0/* Not an Invalidate Descriptor */ + +/* Pagesize of VTD paging structures, including root and context tables */ +#define VTD_PAGE_SHIFT 12 +#define VTD_PAGE_SIZE (1ULL << VTD_PAGE_SHIFT) + +#define VTD_PAGE_SHIFT_4K 12 +#define VTD_PAGE_MASK_4K (~((1ULL << VTD_PAGE_SHIFT_4K) - 1)) +#define VTD_PAGE_SHIFT_2M 21 +#define VTD_PAGE_MASK_2M (~((1ULL << VTD_PAGE_SHIFT_2M) - 1)) +#define VTD_PAGE_SHIFT_1G 30 +#define VTD_PAGE_MASK_1G (~((1ULL << VTD_PAGE_SHIFT_1G) - 1)) + +struct VTDRootEntry { +uint64_t val; +uint64_t rsvd; +}; +typedef struct VTDRootEntry VTDRootEntry ; + +/* Masks for struct VTDRootEntry */ +#define VTD_ROOT_ENTRY_P 1ULL +#define VTD_ROOT_ENTRY_CTP (~0xfffULL) + +#define VTD_ROOT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(VTDRootEntry)) +#define VTD_ROOT_ENTRY_RSVD (0xffeULL | ~VTD_HAW_MASK) + +/* Context-Entry */ +struct VTDContextEntry { +uint64_t lo; +uint64_t hi; +}; +typedef struct VTDContextEntry VTDContextEntry ; + +/* Masks for struct VTDContextEntry */ +/* lo */ +#define VTD_CONTEXT_ENTRY_P (1ULL << 0) +#define VTD_CONTEXT_ENTRY_FPD (1ULL << 1)/* Fault Processing Disable */ +#define VTD_CONTEXT_ENTRY_TT (3ULL << 2)/* Translation Type */ +#define VTD_CONTEXT_TT_MULTI_LEVEL 0 +#define VTD_CONTEXT_TT_DEV_IOTLB 1 +#define VTD_CONTEXT_TT_PASS_THROUGH 2 +/* Second Level Page Translation Pointer*/ +#define VTD_CONTEXT_ENTRY_SLPTPTR (~0xfffULL) +#define VTD_CONTEXT_ENTRY_RSVD_LO (0xff0ULL | ~VTD_HAW_MASK) +/* hi */ +#define VTD_CONTEXT_ENTRY_AW 7ULL/* Adjusted guest-address-width */ +#define VTD_CONTEXT_ENTRY_DID (0xffffULL << 8)/* Domain Identifier */ +#define VTD_CONTEXT_ENTRY_RSVD_HI 0xffffffffff000080ULL + +#define VTD_CONTEXT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(VTDContextEntry)) + +/* Paging Structure common */ +#define VTD_SL_PT_PAGE_SIZE_MASK (1ULL << 7) +/* Bits to decide the offset for each level */ +#define VTD_SL_LEVEL_BITS 9 + +/* Second Level Paging Structure */ +#define VTD_SL_PML4_LEVEL 4 +#define VTD_SL_PDP_LEVEL 3 +#define VTD_SL_PD_LEVEL 2 +#define VTD_SL_PT_LEVEL 1 +#define VTD_SL_PT_ENTRY_NR 512 + +/* Masks for Second Level Paging Entry */ +#define VTD_SL_RW_MASK 3ULL +#define VTD_SL_R 1ULL +#define VTD_SL_W (1ULL << 1) +#define VTD_SL_PT_BASE_ADDR_MASK (~(VTD_PAGE_SIZE - 1) & VTD_HAW_MASK) +#define VTD_SL_IGN_COM 0xbff0000000000000ULL + +#endif
include/hw/i386/intel_iommu.h new file +89 -0
89 lines not on this page · raw message
hw/i386/intel_iommu.c new file +1257 -0
1,257 lines not on this page · raw message
hw/i386/Makefile.objs +1 -0
@@ -2,6 +2,7 @@ obj-$(CONFIG_KVM) += kvm/ obj-y += multiboot.o smbios.o obj-y += pc.o pc_piix.o pc_q35.o obj-y += pc_sysfw.o +obj-y += intel_iommu.o obj-$(CONFIG_XEN) += ../xenpv/ xen/ obj-y += kvmvapic.o
Signature · 1 line
MST
[Qemu-devel] [PULL 03/13] intel-iommu: add DMAR table to ACPI tables
In reply to Michael S. TsirkinFrom: Le Tan <[email protected]>
Expose Intel IOMMU to the BIOS. If object of TYPE_INTEL_IOMMU_DEVICE exists, add DMAR table to ACPI RSDT table. For now the DMAR table indicates that there is only one hardware unit without INTR_REMAP capability on the platform.
--- hw/i386/acpi-defs.h | 40 ++++++++++++++++++++++++++++++++++++++++ hw/i386/acpi-build.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+)
hw/i386/acpi-defs.h +40 -0
@@ -325,4 +325,44 @@ struct Acpi20Tcpa { } QEMU_PACKED;typedef struct Acpi20Tcpa Acpi20Tcpa ; +/* DMAR - DMA Remapping table r2.2 */ +struct AcpiTableDmar { +ACPI_TABLE_HEADER_DEF + uint8_t host_address_width; /* Maximum DMA physical addressability */ +uint8_t flags; +uint8_t reserved[10 ]; +}QEMU_PACKED ; +typedef struct AcpiTableDmar AcpiTableDmar ; + +/* Masks for Flags field above */ +#define ACPI_DMAR_INTR_REMAP 1 +#define ACPI_DMAR_X2APIC_OPT_OUT (1 << 1) + +/* Values for sub-structure type for DMAR */ +enum { +ACPI_DMAR_TYPE_HARDWARE_UNIT =0 ,/* DRHD */ +ACPI_DMAR_TYPE_RESERVED_MEMORY =1 ,/* RMRR */ +ACPI_DMAR_TYPE_ATSR =2 ,/* ATSR */ +ACPI_DMAR_TYPE_HARDWARE_AFFINITY =3 ,/* RHSR */ +ACPI_DMAR_TYPE_ANDD =4 ,/* ANDD */ +ACPI_DMAR_TYPE_RESERVED =5 /* Reserved for furture use */ +}; + +/* +* Sub-structures for DMAR +*/ +/* Type 0: Hardware Unit Definition */ +struct AcpiDmarHardwareUnit { +uint16_t type; +uint16_t length; +uint8_t flags; +uint8_t reserved; +uint16_t pci_segment;/* The PCI Segment associated with this unit */ +uint64_t address;/* Base address of remapping hardware register-set */ +}QEMU_PACKED ; +typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit ; + +/* Masks for Flags field above */ +#define ACPI_DMAR_INCLUDE_PCI_ALL 1 +#endif
hw/i386/acpi-build.c +39 -0
@@ -49,6 +49,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/q35-acpi-dsdt.hex" #include "hw/i386/acpi-dsdt.hex"
@@ -1388,6 +1389,30 @@ build_mcfg_q35(GArray *table_data, GArray *linker, AcpiMcfgInfo *info) }static void +build_dmar_q35 (GArray *table_data,GArray *linker) +{ +int dmar_start = table_data->len; + +AcpiTableDmar *dmar; +AcpiDmarHardwareUnit *drhd; + + 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 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->flags =ACPI_DMAR_INCLUDE_PCI_ALL ; + drhd->pci_segment =cpu_to_le16 (0 ); + drhd->address =cpu_to_le64 (Q35_HOST_BRIDGE_IOMMU_ADDR ); + +build_header (linker, table_data, (void *)(table_data->data + dmar_start), +"DMAR" , table_data->len - dmar_start,1 ); +} + +static void build_dsdt (GArray *table_data,GArray *linker,AcpiMiscInfo *misc) {AcpiTableHeader *dsdt;
@@ -1508,6 +1533,16 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)return true; } +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; +} +static void acpi_build (PcGuestInfo *guest_info,AcpiBuildTables *tables) {
@@ -1584,6 +1619,10 @@ void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables)acpi_add_table (table_offsets, tables->table_data);build_mcfg_q35 (tables->table_data, tables->linker, &mcfg); } +if (acpi_has_iommu ()) { +acpi_add_table (table_offsets, tables->table_data); +build_dmar_q35 (tables->table_data, tables->linker); + }/* Add tables supplied by user (if any) */ for (u =acpi_table_first (); u; u =acpi_table_next (u)) {
Signature · 1 line
MST
[Qemu-devel] [PULL 04/13] intel-iommu: add Intel IOMMU emulation to q35 and add a machine option "iommu" as a switch
In reply to Michael S. TsirkinFrom: Le Tan <[email protected]>
Add Intel IOMMU emulation to q35 chipset and expose it to the guest.
1. Add a machine option. Users can use "-machine iommu=on|off" in the command line to enable/disable Intel IOMMU. The default is off.
2. Accroding to the machine option, q35 will initialize the Intel IOMMU and use pci_setup_iommu() to setup q35_host_dma_iommu() as the IOMMU function for the pci bus.
3. q35_host_dma_iommu() will return different address space according to the bus_num and devfn of the device.
--- include/hw/boards.h | 1 + include/hw/pci-host/q35.h | 2 ++ hw/core/machine.c | 17 +++++++++++++++++ hw/pci-host/q35.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ vl.c | 4 ++++ qemu-options.hx | 5 ++++- 6 files changed, 74 insertions(+), 1 deletion(-)
include/hw/boards.h +1 -0
@@ -123,6 +123,7 @@ struct MachineState {bool mem_merge;bool usb;char *firmware; +bool iommu;ram_addr_t ram_size;ram_addr_t maxram_size;
include/hw/pci-host/q35.h +2 -0
@@ -33,6 +33,7 @@#include "hw/acpi/acpi.h" #include "hw/acpi/ich9.h" #include "hw/pci-host/pam.h" +#include "hw/i386/intel_iommu.h" #define TYPE_Q35_HOST_DEVICE "q35-pcihost"#define Q35_HOST_DEVICE (obj) \
@@ -60,6 +61,7 @@ typedef struct MCHPCIState {uint64_t pci_hole64_size;PcGuestInfo *guest_info;uint32_t short_root_bus; +IntelIOMMUState *iommu; } MCHPCIState; typedef struct Q35PCIHost {
hw/core/machine.c +17 -0
@@ -235,6 +235,20 @@ static void machine_set_firmware(Object *obj, const char *value, Error **errp) ms->firmware =g_strdup (value); } +static bool machine_get_iommu (Object *obj,Error **errp) +{ +MachineState *ms =MACHINE (obj); + +return ms->iommu; +} + +static void machine_set_iommu (Object *obj,bool value,Error **errp) +{ +MachineState *ms =MACHINE (obj); + + ms->iommu = value; +} +static void machine_initfn (Object *obj) { object_property_add_str(obj, "accel",
@@ -274,6 +288,9 @@ static void machine_initfn(Object *obj)object_property_add_bool (obj,"usb" , machine_get_usb, machine_set_usb,NULL );object_property_add_str (obj,"firmware" , machine_get_firmware, machine_set_firmware,NULL ); +object_property_add_bool (obj,"iommu" , + machine_get_iommu, + machine_set_iommu,NULL ); }static void machine_finalize (Object *obj)
hw/pci-host/q35.c +46 -0
58 lines not on this page · raw message
vl.c +4 -0
@@ -388,6 +388,10 @@ static QemuOptsList qemu_machine_opts = { .name = PC_MACHINE_MAX_RAM_BELOW_4G, .type = QEMU_OPT_SIZE, .help = "maximum ram below the 4G boundary (32bit boundary)", + },{ + .name = "iommu", + .type = QEMU_OPT_BOOL, + .help = "Set on/off to enable/disable Intel IOMMU (VT-d)", }, {/* End of list */ } },
qemu-options.hx +4 -1
@@ -35,7 +35,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \ " kernel_irqchip=on|off controls accelerated irqchip support\n" " kvm_shadow_mem=size of KVM shadow MMU\n" " dump-guest-core=on|off include guest memory in a core dump (default=on)\n" - " mem-merge=on|off controls memory merge support (default: on)\n", + " mem-merge=on|off controls memory merge support (default: on)\n" + " iommu=on|off controls emulated Intel IOMMU (VT-d) support (default=off)\n", QEMU_ARCH_ALL) STEXI @item -machine [type=]@var{name}[,prop=@var{value}[,...]]
@@ -58,6 +59,8 @@ Include guest memory in a core dump. The default is on. Enables or disables memory merge support. This feature, when supported by the host, de-duplicates identical memory pages among VMs instances (enabled by default). +@item iommu=on|off +Enables or disables emulated Intel IOMMU (VT-d) support. The default is off. @end table ETEXI
Signature · 1 line
MST
[Qemu-devel] [PULL 06/13] intel-iommu: add supports for queued invalidation interface
In reply to Michael S. TsirkinFrom: Le Tan <[email protected]>
Add supports for queued invalidation interface, an expended invalidation interface with extended capabilities.
--- hw/i386/intel_iommu_internal.h | 27 ++- hw/i386/intel_iommu.c | 373 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 393 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_internal.h +21 -6
@@ -255,12 +255,27 @@ typedef enum VTDFaultReason {VTD_FR_MAX ,/* Guard */ } VTDFaultReason; -/* Masks for Queued Invalidation Descriptor */ -#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_WAIT 0x5/* Invalidation Wait Descriptor */ -#define VTD_INV_DESC_NONE 0/* Not an Invalidate Descriptor */ +/* Queued Invalidation Descriptor */ +struct VTDInvDesc { +uint64_t lo; +uint64_t hi; +}; +typedef struct VTDInvDesc VTDInvDesc ; + +/* Masks for struct 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_WAIT 0x5/* Invalidation Wait Descriptor */ +#define VTD_INV_DESC_NONE 0/* Not an Invalidate Descriptor */ + +/* Masks for Invalidation Wait Descriptor*/ +#define VTD_INV_DESC_WAIT_SW (1ULL << 5) +#define VTD_INV_DESC_WAIT_IF (1ULL << 4) +#define VTD_INV_DESC_WAIT_FN (1ULL << 6) +#define VTD_INV_DESC_WAIT_DATA_SHIFT 32 +#define VTD_INV_DESC_WAIT_RSVD_LO 0Xffffff80ULL +#define VTD_INV_DESC_WAIT_RSVD_HI 3ULL/* Pagesize of VTD paging structures, including root and context tables */ #define VTD_PAGE_SHIFT 12
hw/i386/intel_iommu.c +372 -1
445 lines not on this page · raw message
Signature · 1 line
MST
[Qemu-devel] [PULL 05/13] intel-iommu: fix coding style issues around in q35.c and machine.c
In reply to Michael S. TsirkinFrom: Le Tan <[email protected]>
Fix coding style issues around in hw/pci-host/q35.c and hw/core/machine.c.
--- hw/core/machine.c | 10 +++++++--- hw/pci-host/q35.c | 11 ++++++----- 2 files changed, 13 insertions(+), 8 deletions(-)
hw/core/machine.c +7 -3
@@ -284,10 +284,14 @@ static void machine_initfn(Object *obj) machine_set_dump_guest_core, NULL);object_property_add_bool (obj,"mem-merge" , - machine_get_mem_merge, machine_set_mem_merge,NULL ); -object_property_add_bool (obj,"usb" , machine_get_usb, machine_set_usb,NULL ); + machine_get_mem_merge, + machine_set_mem_merge,NULL ); +object_property_add_bool (obj,"usb" , + machine_get_usb, + machine_set_usb,NULL );object_property_add_str (obj,"firmware" , - machine_get_firmware, machine_set_firmware,NULL ); + machine_get_firmware, + machine_set_firmware,NULL );object_property_add_bool (obj,"iommu" , machine_get_iommu, machine_set_iommu,NULL );
hw/pci-host/q35.c +6 -5
@@ -405,12 +405,13 @@ static int mch_init(PCIDevice *d)memory_region_add_subregion_overlap (mch->system_memory,0xa0000 , &mch->smram_region,1 );memory_region_set_enabled (&mch->smram_region, false); -init_pam (DEVICE (mch), mch->ram_memory, mch->system_memory, mch->pci_address_space, - &mch->pam_regions[0 ],PAM_BIOS_BASE ,PAM_BIOS_SIZE ); +init_pam (DEVICE (mch), mch->ram_memory, mch->system_memory, + mch->pci_address_space, &mch->pam_regions[0 ], +PAM_BIOS_BASE ,PAM_BIOS_SIZE );for (i =0 ; i <12 ; ++i) { -init_pam (DEVICE (mch), mch->ram_memory, mch->system_memory, mch->pci_address_space, - &mch->pam_regions[i+1 ],PAM_EXPAN_BASE + i *PAM_EXPAN_SIZE , -PAM_EXPAN_SIZE ); +init_pam (DEVICE (mch), mch->ram_memory, mch->system_memory, + mch->pci_address_space, &mch->pam_regions[i+1 ], +PAM_EXPAN_BASE + i *PAM_EXPAN_SIZE ,PAM_EXPAN_SIZE ); }/* Intel IOMMU (VT-d) */ if (qemu_opt_get_bool (qemu_get_machine_opts (),"iommu" , false)) {
Signature · 1 line
MST
[Qemu-devel] [PULL 01/13] iommu: add is_write as a parameter to the translate function of MemoryRegionIOMMUOps
In reply to Michael S. TsirkinFrom: Le Tan <[email protected]>
Add a bool variable is_write as a parameter to the translate function of MemoryRegionIOMMUOps to indicate the operation of the access. It can be used for correct fault reporting from within the callback. Change the interface of related functions.
--- include/exec/memory.h | 2 +- exec.c | 2 +- hw/alpha/typhoon.c | 3 ++- hw/pci-host/apb.c | 3 ++- hw/ppc/spapr_iommu.c | 3 ++- 5 files changed, 8 insertions(+), 5 deletions(-)
include/exec/memory.h +1 -1
@@ -129,7 +129,7 @@ typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;struct MemoryRegionIOMMUOps {/* Return a TLB entry that contains a given address. */ -IOMMUTLBEntry (*translate)(MemoryRegion *iommu,hwaddr addr); +IOMMUTLBEntry (*translate)(MemoryRegion *iommu,hwaddr addr,bool is_write); };typedef struct CoalescedMemoryRange CoalescedMemoryRange ;
exec.c +1 -1
@@ -373,7 +373,7 @@ MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,break ; } - iotlb = mr->iommu_ops->translate (mr, addr); + iotlb = mr->iommu_ops->translate (mr, addr, is_write); addr = ((iotlb.translated_addr & ~iotlb.addr_mask) | (addr & iotlb.addr_mask)); len =MIN (len, (addr | iotlb.addr_mask) - addr +1 );
hw/alpha/typhoon.c +2 -1
@@ -660,7 +660,8 @@ static bool window_translate(TyphoonWindow *win, hwaddr addr,/* Handle PCI-to-system address translation. */ /* TODO: A translation failure here ought to set PCI error codes on the Pchip and generate a machine check interrupt. */ -static IOMMUTLBEntry typhoon_translate_iommu (MemoryRegion *iommu,hwaddr addr) +static IOMMUTLBEntry typhoon_translate_iommu (MemoryRegion *iommu,hwaddr addr, +bool is_write) {TyphoonPchip *pchip =container_of (iommu, TyphoonPchip, iommu);IOMMUTLBEntry ret;
hw/pci-host/apb.c +2 -1
@@ -204,7 +204,8 @@ static AddressSpace *pbm_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)return &is->iommu_as; } -static IOMMUTLBEntry pbm_translate_iommu (MemoryRegion *iommu,hwaddr addr) +static IOMMUTLBEntry pbm_translate_iommu (MemoryRegion *iommu,hwaddr addr, +bool is_write) {IOMMUState *is =container_of (iommu, IOMMUState, iommu);hwaddr baseaddr, offset;
hw/ppc/spapr_iommu.c +2 -1
@@ -59,7 +59,8 @@ static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)return NULL ; } -static IOMMUTLBEntry spapr_tce_translate_iommu (MemoryRegion *iommu,hwaddr addr) +static IOMMUTLBEntry spapr_tce_translate_iommu (MemoryRegion *iommu,hwaddr addr, +bool is_write) {sPAPRTCETable *tcet =container_of (iommu, sPAPRTCETable, iommu);uint64_t tce;
Signature · 1 line
MST
[Qemu-devel] [PULL 07/13] intel-iommu: add context-cache to cache context-entry
In reply to Michael S. TsirkinFrom: Le Tan <[email protected]>
Add context-cache to cache context-entry encountered on a page-walk. Each VTDAddressSpace has a member of VTDContextCacheEntry which represents an entry in the context-cache. Since devices with different bus_num and devfn have their respective VTDAddressSpace, this will be a good way to reference the cached entries.
Each VTDContextCacheEntry will have a context_cache_gen and the cached entry is valid only when context_cache_gen equals IntelIOMMUState.context_cache_gen.
--- hw/i386/intel_iommu_internal.h | 23 +++-- include/hw/i386/intel_iommu.h | 22 +++++ hw/i386/intel_iommu.c | 188 +++++++++++++++++++++++++++++++++++------ hw/pci-host/q35.c | 1 + 4 files changed, 199 insertions(+), 35 deletions(-)
hw/i386/intel_iommu_internal.h +16 -7
@@ -154,6 +154,9 @@#define VTD_CCMD_DOMAIN_INVL_A (2ULL << 59)#define VTD_CCMD_DEVICE_INVL_A (3ULL << 59)#define VTD_CCMD_CAIG_MASK (3ULL << 59) +#define VTD_CCMD_DID (val) ((val) & VTD_DOMAIN_ID_MASK) +#define VTD_CCMD_SID (val) (((val) >> 16) & 0xffffULL) +#define VTD_CCMD_FM (val) (((val) >> 32) & 3ULL)/* RTADDR_REG */ #define VTD_RTADDR_RTT (1ULL << 11)
@@ -169,6 +172,7 @@#define VTD_CAP_FRO (DMAR_FRCD_REG_OFFSET << 20)#define VTD_CAP_NFR ((DMAR_FRCD_REG_NR - 1) << 40)#define VTD_DOMAIN_ID_SHIFT 16/* 16-bit domain id for 64K domains */ +#define VTD_DOMAIN_ID_MASK ((1UL << VTD_DOMAIN_ID_SHIFT) - 1)#define VTD_CAP_ND (((VTD_DOMAIN_ID_SHIFT - 4) / 2) & 7ULL)#define VTD_MGAW 39/* Maximum Guest Address Width */ #define VTD_CAP_MGAW (((VTD_MGAW - 1) & 0x3fULL) << 16)
@@ -255,6 +259,8 @@ typedef enum VTDFaultReason { VTD_FR_MAX, /* Guard */ } VTDFaultReason; +#define VTD_CONTEXT_CACHE_GEN_MAX 0xffffffffUL +/* Queued Invalidation Descriptor */ struct VTDInvDesc {uint64_t lo;
@@ -277,6 +283,16 @@ typedef struct VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_WAIT_RSVD_LO 0Xffffff80ULL#define VTD_INV_DESC_WAIT_RSVD_HI 3ULL +/* Masks for Context-cache Invalidation Descriptor */ +#define VTD_INV_DESC_CC_G (3ULL << 4) +#define VTD_INV_DESC_CC_GLOBAL (1ULL << 4) +#define VTD_INV_DESC_CC_DOMAIN (2ULL << 4) +#define VTD_INV_DESC_CC_DEVICE (3ULL << 4) +#define VTD_INV_DESC_CC_DID (val) (((val) >> 16) & VTD_DOMAIN_ID_MASK) +#define VTD_INV_DESC_CC_SID (val) (((val) >> 32) & 0xffffUL) +#define VTD_INV_DESC_CC_FM (val) (((val) >> 48) & 3UL) +#define VTD_INV_DESC_CC_RSVD 0xfffc00000000ffc0ULL +/* Pagesize of VTD paging structures, including root and context tables */ #define VTD_PAGE_SHIFT 12#define VTD_PAGE_SIZE (1ULL << VTD_PAGE_SHIFT)
@@ -301,13 +317,6 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_ROOT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(VTDRootEntry))#define VTD_ROOT_ENTRY_RSVD (0xffeULL | ~VTD_HAW_MASK) -/* Context-Entry */ -struct VTDContextEntry { -uint64_t lo; -uint64_t hi; -}; -typedef struct VTDContextEntry VTDContextEntry ; -/* Masks for struct VTDContextEntry */ /* lo */ #define VTD_CONTEXT_ENTRY_P (1ULL << 0)
include/hw/i386/intel_iommu.h +22 -0
@@ -37,20 +37,40 @@#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) +#define VTD_SID_TO_DEVFN (sid) ((sid) & 0xff)#define DMAR_REG_SIZE 0x230#define VTD_HOST_ADDRESS_WIDTH 39#define VTD_HAW_MASK ((1ULL << VTD_HOST_ADDRESS_WIDTH) - 1) +typedef struct VTDContextEntry VTDContextEntry ; +typedef struct VTDContextCacheEntry VTDContextCacheEntry ;typedef struct IntelIOMMUState IntelIOMMUState ;typedef struct VTDAddressSpace VTDAddressSpace ; + +/* Context-Entry */ +struct VTDContextEntry { +uint64_t lo; +uint64_t hi; +}; + +struct VTDContextCacheEntry { +/* The cache entry is obsolete if +* context_cache_gen!=IntelIOMMUState.context_cache_gen +*/ +uint32_t context_cache_gen; +struct VTDContextEntry context_entry; +}; +struct VTDAddressSpace {uint8_t bus_num;uint8_t devfn;AddressSpace as;MemoryRegion iommu;IntelIOMMUState *iommu_state; +VTDContextCacheEntry context_cache_entry; };/* The iommu (DMAR) device state struct */
@@ -82,6 +102,8 @@ struct IntelIOMMUState {uint64_t cap;/* The value of capability reg */ uint64_t ecap;/* The value of extended capability reg */ +uint32_t context_cache_gen;/* Should be in [1,MAX] */ +MemoryRegionIOMMUOps iommu_ops;VTDAddressSpace **address_spaces[VTD_PCI_BUS_MAX ]; };
hw/i386/intel_iommu.c +160 -28
270 lines not on this page · raw message
hw/pci-host/q35.c +1 -0
@@ -368,6 +368,7 @@ static AddressSpace *q35_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) pvtd_as[devfn]->bus_num = (uint8_t )bus_num; pvtd_as[devfn]->devfn = (uint8_t )devfn; pvtd_as[devfn]->iommu_state = s; + pvtd_as[devfn]->context_cache_entry.context_cache_gen =0 ;memory_region_init_iommu (&pvtd_as[devfn]->iommu,OBJECT (s), &s->iommu_ops,"intel_iommu" ,UINT64_MAX ); address_space_init(&pvtd_as[devfn]->as,
Signature · 1 line
MST
[Qemu-devel] [PULL 09/13] vhost_net: cleanup start/stop condition
In reply to Michael S. TsirkinChecking vhost device internal state in vhost_net looks like a layering violation since vhost_net does not
set this flag: it is set and tested by vhost.c. There seems to be no reason to check this:
caller in virtio net uses its own flag, vhost_started, to ensure vhost is started/stopped as appropriate.
--- hw/net/vhost_net.c | 8 -------- 1 file changed, 8 deletions(-)
hw/net/vhost_net.c +0 -8
@@ -195,10 +195,6 @@ static int vhost_net_start_one(struct vhost_net *net,struct vhost_vring_file file = { };int r; -if (net->dev.started) { -return 0 ; - } - net->dev.nvqs =2 ; net->dev.vqs = net->vqs; net->dev.vq_index = vq_index;
@@ -256,10 +252,6 @@ static void vhost_net_stop_one(struct vhost_net *net, {struct vhost_vring_file file = { .fd =-1 }; -if (!net->dev.started) { -return ; - } -if (net->nc->info->type ==NET_CLIENT_OPTIONS_KIND_TAP ) {for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) {const VhostOps *vhost_ops = net->dev.vhost_ops;
Signature · 1 line
MST
[Qemu-devel] [PULL 08/13] intel-iommu: add IOTLB using hash table
In reply to Michael S. TsirkinFrom: Le Tan <[email protected]>
Add IOTLB to cache information about the translation of input-addresses. IOTLB use a GHashTable as cache. The key of the hash table is the logical-OR of gfn and source id after left-shifting.
--- hw/i386/intel_iommu_internal.h | 34 ++++++- include/hw/i386/intel_iommu.h | 11 ++- hw/i386/intel_iommu.c | 213 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 251 insertions(+), 7 deletions(-)
hw/i386/intel_iommu_internal.h +33 -1
@@ -111,6 +111,10 @@#define VTD_INTERRUPT_ADDR_FIRST 0xfee00000ULL#define VTD_INTERRUPT_ADDR_LAST 0xfeefffffULL +/* The shift of source_id in the key of IOTLB hash table */ +#define VTD_IOTLB_SID_SHIFT 36 +#define VTD_IOTLB_MAX_SIZE 1024/* Max size of the hash table */ +/* IOTLB_REG */ #define VTD_TLB_GLOBAL_FLUSH (1ULL << 60)/* Global invalidation */ #define VTD_TLB_DSI_FLUSH (2ULL << 60)/* Domain-selective */
@@ -121,6 +125,11 @@#define VTD_TLB_PSI_FLUSH_A (3ULL << 57)#define VTD_TLB_FLUSH_GRANU_MASK_A (3ULL << 57)#define VTD_TLB_IVT (1ULL << 63) +#define VTD_TLB_DID (val) (((val) >> 32) & VTD_DOMAIN_ID_MASK) + +/* IVA_REG */ +#define VTD_IVA_ADDR (val) ((val) & ~0xfffULL & ((1ULL << VTD_MGAW) - 1)) +#define VTD_IVA_AM (val) ((val) & 0x3fULL)/* GCMD_REG */ #define VTD_GCMD_TE (1UL << 31)
@@ -176,6 +185,9 @@#define VTD_CAP_ND (((VTD_DOMAIN_ID_SHIFT - 4) / 2) & 7ULL)#define VTD_MGAW 39/* Maximum Guest Address Width */ #define VTD_CAP_MGAW (((VTD_MGAW - 1) & 0x3fULL) << 16) +#define VTD_MAMV 9ULL +#define VTD_CAP_MAMV (VTD_MAMV << 48) +#define VTD_CAP_PSI (1ULL << 39)/* Supported Adjusted Guest Address Widths */ #define VTD_CAP_SAGAW_SHIFT 8
@@ -293,6 +305,26 @@ typedef struct VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_CC_FM (val) (((val) >> 48) & 3UL)#define VTD_INV_DESC_CC_RSVD 0xfffc00000000ffc0ULL +/* Masks for IOTLB Invalidate Descriptor */ +#define VTD_INV_DESC_IOTLB_G (3ULL << 4) +#define VTD_INV_DESC_IOTLB_GLOBAL (1ULL << 4) +#define VTD_INV_DESC_IOTLB_DOMAIN (2ULL << 4) +#define VTD_INV_DESC_IOTLB_PAGE (3ULL << 4) +#define VTD_INV_DESC_IOTLB_DID (val) (((val) >> 16) & VTD_DOMAIN_ID_MASK) +#define VTD_INV_DESC_IOTLB_ADDR (val) ((val) & ~0xfffULL & \ + ((1ULL << VTD_MGAW) - 1)) +#define VTD_INV_DESC_IOTLB_AM (val) ((val) & 0x3fULL) +#define VTD_INV_DESC_IOTLB_RSVD_LO 0xffffffff0000ff00ULL +#define VTD_INV_DESC_IOTLB_RSVD_HI 0xf80ULL + +/* Information about page-selective IOTLB invalidate */ +struct VTDIOTLBPageInvInfo { +uint16_t domain_id; +uint64_t gfn; +uint8_t mask; +}; +typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo ; +/* Pagesize of VTD paging structures, including root and context tables */ #define VTD_PAGE_SHIFT 12#define VTD_PAGE_SIZE (1ULL << VTD_PAGE_SHIFT)
@@ -330,7 +362,7 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_CONTEXT_ENTRY_RSVD_LO (0xff0ULL | ~VTD_HAW_MASK)/* hi */ #define VTD_CONTEXT_ENTRY_AW 7ULL/* Adjusted guest-address-width */ -#define VTD_CONTEXT_ENTRY_DID (0xffffULL << 8)/* Domain Identifier */ +#define VTD_CONTEXT_ENTRY_DID (val) (((val) >> 8) & VTD_DOMAIN_ID_MASK)#define VTD_CONTEXT_ENTRY_RSVD_HI 0xffffffffff000080ULL#define VTD_CONTEXT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(VTDContextEntry))
include/hw/i386/intel_iommu.h +10 -1
@@ -48,7 +48,7 @@ typedef struct VTDContextEntry VTDContextEntry;typedef struct VTDContextCacheEntry VTDContextCacheEntry ;typedef struct IntelIOMMUState IntelIOMMUState ;typedef struct VTDAddressSpace VTDAddressSpace ; - +typedef struct VTDIOTLBEntry VTDIOTLBEntry ;/* Context-Entry */ struct VTDContextEntry {
@@ -73,6 +73,14 @@ struct VTDAddressSpace {VTDContextCacheEntry context_cache_entry; }; +struct VTDIOTLBEntry { +uint64_t gfn; +uint16_t domain_id; +uint64_t slpte; +bool read_flags; +bool write_flags; +}; +/* The iommu (DMAR) device state struct */ struct IntelIOMMUState {SysBusDevice busdev;
@@ -103,6 +111,7 @@ struct IntelIOMMUState {uint64_t ecap;/* The value of extended capability reg */ uint32_t context_cache_gen;/* Should be in [1,MAX] */ +GHashTable *iotlb;/* IOTLB */ MemoryRegionIOMMUOps iommu_ops;VTDAddressSpace **address_spaces[VTD_PCI_BUS_MAX ];
hw/i386/intel_iommu.c +208 -5
303 lines not on this page · raw message
Signature · 1 line
MST
[Qemu-devel] [PULL 10/13] ioh3420: remove unused ioh3420_init() declaration
In reply to Michael S. TsirkinFrom: Gonglei <[email protected]>
commit 0f9b1771ccc65873a8376c81200a437aa58c2f6d
ioh3420: Remove obsoleted, unused ioh3420_init function
removed the implementation of ioh3420_init
Drop the declaration from the header file as well.
--- hw/pci-bridge/ioh3420.h | 4 ---- 1 file changed, 4 deletions(-)
hw/pci-bridge/ioh3420.h +0 -4
@@ -3,8 +3,4 @@#include "hw/pci/pcie_port.h" -PCIESlot *ioh3420_init (PCIBus *bus,int devfn,bool multifunction, -const char *bus_name,pci_map_irq_fn map_irq, -uint8_t port,uint8_t chassis,uint16_t slot); -#endif /* QEMU_IOH3420_H */
Signature · 1 line
MST
[Qemu-devel] [PULL 11/13] virtio-net: don't run bh on vm stopped
In reply to Michael S. Tsirkincommit 783e7706937fe15523b609b545587a028a2bdd03
virtio-net: stop/start bh when appropriate
is incomplete: BH might execute within the same main loop iteration but after vmstop, so in theory, we might trigger an assertion.
I was unable to reproduce this in practice,
but it seems clear enough that the potential is there, so worth fixing.
--- hw/net/virtio-net.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +12 -2
@@ -1224,7 +1224,12 @@ static void virtio_net_tx_timer(void *opaque)VirtIONetQueue *q = opaque;VirtIONet *n = q->n;VirtIODevice *vdev =VIRTIO_DEVICE (n); -assert (vdev->vm_running); +/* This happens when device was stopped but BH wasn't. */ +if (!vdev->vm_running) { +/* Make sure tx waiting is set, so we'll run when restarted. */ +assert (q->tx_waiting); +return ; + } q->tx_waiting =0 ;
@@ -1244,7 +1249,12 @@ static void virtio_net_tx_bh(void *opaque)VirtIODevice *vdev =VIRTIO_DEVICE (n);int32_t ret; -assert (vdev->vm_running); +/* This happens when device was stopped but BH wasn't. */ +if (!vdev->vm_running) { +/* Make sure tx waiting is set, so we'll run when restarted. */ +assert (q->tx_waiting); +return ; + } q->tx_waiting =0 ;
Signature · 1 line
MST
[Qemu-devel] [PULL 12/13] pci: avoid losing config updates to MSI/MSIX cap regs
In reply to Michael S. TsirkinFrom: Knut Omang <[email protected]>
Since
commit 95d658002401e2e47a5404298ebe9508846e8a39
msi: Invoke msi/msix_write_config from PCI core
msix config writes are lost, the value written is always 0.
Fix pci_default_write_config to avoid this.
--- hw/pci/pci.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
hw/pci/pci.c +4 -3
@@ -1146,9 +1146,10 @@ uint32_t pci_default_read_config(PCIDevice *d,return le32_to_cpu (val); } -void pci_default_write_config (PCIDevice *d,uint32_t addr,uint32_t val,int l) +void pci_default_write_config (PCIDevice *d,uint32_t addr,uint32_t val_in,int l) {int i, was_irq_disabled =pci_irq_disabled (d); +uint32_t val = val_in;for (i =0 ; i < l; val >>=8 , ++i) {uint8_t wmask = d->wmask[addr + i];
@@ -1170,8 +1171,8 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) & PCI_COMMAND_MASTER); } -msi_write_config (d, addr, val, l); -msix_write_config (d, addr, val, l); +msi_write_config (d, addr, val_in, l); +msix_write_config (d, addr, val_in, l); }/***********************************************************/
Signature · 1 line
MST
[Qemu-devel] [PULL 13/13] vhost_net: start/stop guest notifiers properly
In reply to Michael S. TsirkinFrom: Jason Wang <[email protected]>
commit a9f98bb5ebe6fb1869321dcc58e72041ae626ad8 vhost: multiqueue support changed the order of stopping the device. Previously vhost_dev_stop would disable backend and only afterwards, unset guest notifiers. We now unset guest notifiers while vhost is still active. This can lose interrupts causing guest networking to fail. In particular, this has been observed during migration.
To adapt this, several other changes are needed:
- remove the hdev->started assertion in vhost.c since we may want to start the guest notifiers before vhost starts and stop the guest notifiers after vhost is stopped.
- introduce the vhost_net_set_vq_index() and call it before setting guest notifiers. This is used to guarantee vhost_net has the correct virtqueue index when setting guest notifiers.
--- hw/net/vhost_net.c | 31 +++++++++++++++++++------------ hw/virtio/vhost.c | 2 -- 2 files changed, 19 insertions(+), 14 deletions(-)
hw/net/vhost_net.c +19 -12
@@ -188,16 +188,19 @@ bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)return vhost_dev_query (&net->dev, dev); } +static void vhost_net_set_vq_index (struct vhost_net *net,int vq_index) +{ + net->dev.vq_index = vq_index; +} +static int vhost_net_start_one (struct vhost_net *net, -VirtIODevice *dev, -int vq_index) +VirtIODevice *dev) {struct vhost_vring_file file = { };int r; net->dev.nvqs =2 ; net->dev.vqs = net->vqs; - net->dev.vq_index = vq_index; r =vhost_dev_enable_notifiers (&net->dev, dev);if (r <0 ) {
@@ -301,11 +304,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, }for (i =0 ; i < total_queues; i++) { - r =vhost_net_start_one (get_vhost_net (ncs[i].peer), dev, i *2 ); - -if (r <0 ) { - goto err; - } +vhost_net_set_vq_index (get_vhost_net (ncs[i].peer), i *2 ); } r = k->set_guest_notifiers (qbus->parent, total_queues *2 , true);
@@ -314,6 +313,14 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, goto err; } +for (i =0 ; i < total_queues; i++) { + r =vhost_net_start_one (get_vhost_net (ncs[i].peer), dev); + +if (r <0 ) { + goto err; + } + } +return 0 ; err:
@@ -331,16 +338,16 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (vbus);int i, r; +for (i =0 ; i < total_queues; i++) { +vhost_net_stop_one (get_vhost_net (ncs[i].peer), dev); + } + r = k->set_guest_notifiers (qbus->parent, total_queues *2 , false);if (r <0 ) {fprintf (stderr,"vhost guest notifier cleanup failed: %d\n" , r);fflush (stderr); }assert (r >=0 ); - -for (i =0 ; i < total_queues; i++) { -vhost_net_stop_one (get_vhost_net (ncs[i].peer), dev); - } }void vhost_net_cleanup (struct vhost_net *net)
hw/virtio/vhost.c +0 -2
@@ -976,7 +976,6 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)bool vhost_virtqueue_pending (struct vhost_dev *hdev,int n) {struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index; -assert (hdev->started);assert (n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);return event_notifier_test_and_clear (&vq->masked_notifier); }
@@ -988,7 +987,6 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,struct VirtQueue *vvq =virtio_get_queue (vdev, n);int r, index = n - hdev->vq_index; -assert (hdev->started);assert (n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);struct vhost_vring_file file = {
Signature · 1 line
MST
William Dauchy
Tested-byRe: [Qemu-devel] [PULL 13/13] vhost_net: start/stop guest notifiers properly
Quoted 17 lines · Michael S. Tsirkin
From: Jason Wang <[email protected]>
commit
a9f98bb5ebe6fb1869321dcc58e72041ae626ad8vhost: multiqueue support changed the order of stopping the device. Previouslyvhost_dev_stopwould disable backend and only afterwards, unset guest notifiers. We now unset guest notifiers while vhost is still active. This can lose interrupts causing guest networking to fail. In particular, this has been observed during migration.To adapt this, several other changes are needed:
- remove thehdev->startedassertion invhost.csince we may want to start the guest notifiers before vhost starts and stop the guest notifiers after vhost is stopped.
- introduce thevhost_net_set_vq_index()and call it before setting guest notifiers. This is used to guaranteevhost_nethas the correct virtqueue index when setting guest notifiers.
Quoted 3 lines
Reported-by "Zhangjie (HZ)" <[email protected]>Tested-by William Dauchy <[email protected]>
please use:
Quoted 4 lines
Signed-off-by Michael S. Tsirkin <[email protected]>Signed-off-by Jason Wang <[email protected]>Reviewed-by Michael S. Tsirkin <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>
Thanks,
Signature · 1 line
William
Re: [Qemu-devel] [PULL 13/13] vhost_net: start/stop guest notifiers properly
Quoted 18 lines · William Dauchy
Quoted 17 lines · Michael S. Tsirkin
From: Jason Wang <[email protected]>
commit
a9f98bb5ebe6fb1869321dcc58e72041ae626ad8vhost: multiqueue support changed the order of stopping the device. Previouslyvhost_dev_stopwould disable backend and only afterwards, unset guest notifiers. We now unset guest notifiers while vhost is still active. This can lose interrupts causing guest networking to fail. In particular, this has been observed during migration.To adapt this, several other changes are needed:
- remove thehdev->startedassertion invhost.csince we may want to start the guest notifiers before vhost starts and stop the guest notifiers after vhost is stopped.
- introduce thevhost_net_set_vq_index()and call it before setting guest notifiers. This is used to guaranteevhost_nethas the correct virtqueue index when setting guest notifiers.
Quoted 7 lines
Quoted 3 lines
Reported-by "Zhangjie (HZ)" <[email protected]>Tested-by William Dauchy <[email protected]>please use:
Tested-by William Dauchy <[email protected]>
It's a pull request so not easy to fix.
I'll do it like that next time.
Quoted 8 lines
Quoted 4 lines
Signed-off-by Michael S. Tsirkin <[email protected]>Signed-off-by Jason Wang <[email protected]>Reviewed-by Michael S. Tsirkin <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>Thanks,
Signature · 1 line
William
Quoted 41 lines · Michael S. Tsirkin
The following changes since commit
187de915e8d06aaf82be206aebc551c82bf0670c:pcie: fix trailing whitespace (2014-08-25 00:16:07 +0200)
are available in the git repository at:
git://git.kernel.org/pub/scm/virt/kvm/mst/qemu.gittags/for_upstreamfor you to fetch changes up to
aad4dce934649b3a398396fc2a76f215bb194ea4:
vhost_net: start/stop guest notifiers properly (2014-09-02 17:33:37 +0300)----------------------------------------------------------------
pci, pc fixes, featuresA bunch of bugfixes - these will make sense for 2.1.1
Initial Intel IOMMU support.
Signed-off-by Michael S. Tsirkin <[email protected]>----------------------------------------------------------------
Gonglei (1):
ioh3420: remove unusedioh3420_init()declarationJason Wang (1):
vhost_net: start/stop guest notifiers properlyKnut Omang (1):
pci: avoid losing config updates to MSI/MSIX cap regsLe Tan (8):
iommu: addis_writeas a parameter to the translate function of MemoryRegionIOMMUOps
intel-iommu: introduce Intel IOMMU (VT-d) emulation
intel-iommu: add DMAR table to ACPI tables
intel-iommu: add Intel IOMMU emulation to q35 and add a machine option "iommu" as a switch
intel-iommu: fix coding style issues around inq35.candmachine.c
intel-iommu: add supports for queued invalidation interface
intel-iommu: add context-cache to cache context-entry
intel-iommu: add IOTLB using hash table
Quoted 2 lines
Michael S. Tsirkin (2):
vhost_net: cleanup start/stop condition
A problem was reported with this one.
I fixed it up, will send v2 pull.
Quoted 29 lines
virtio-net: don't run bh on vm stopped
hw/i386/acpi-defs.h | 40 + hw/i386/intel_iommu_internal.h | 389 ++++++++ hw/pci-bridge/ioh3420.h | 4 - include/exec/memory.h | 2 +- include/hw/boards.h | 1 + include/hw/i386/intel_iommu.h | 120 +++ include/hw/pci-host/q35.h | 2 + exec.c | 2 +- hw/alpha/typhoon.c | 3 +- hw/core/machine.c | 27 +- hw/i386/acpi-build.c | 39 + hw/i386/intel_iommu.c | 1963 ++++++++++++++++++++++++++++++++++++++++ hw/net/vhost_net.c | 39 +- hw/net/virtio-net.c | 14 +- hw/pci-host/apb.c | 3 +- hw/pci-host/q35.c | 58 +- hw/pci/pci.c | 7 +- hw/ppc/spapr_iommu.c | 3 +- hw/virtio/vhost.c | 2 - vl.c | 4 + hw/i386/Makefile.objs | 1 + qemu-options.hx | 5 +- 22 files changed, 2683 insertions(+), 45 deletions(-) create mode 100644 hw/i386/intel_iommu_internal.h create mode 100644 include/hw/i386/intel_iommu.h create mode 100644 hw/i386/intel_iommu.c
Quoted 2 lines · Michael S. Tsirkin
A problem was reported with this one.
I fixed it up, will send v2 pull.
I accidentally just merged the v1 by mistake :-(
Sorry about that; I'm going to merge in the v2 (it conflicts in vhost_net.c but fairly trivially so I'll fix that up) and then we should get to where we were intending to go. Apologies for the error.
-- PMM
Quoted 9 lines · Peter Maydell
Quoted 2 lines · Michael S. Tsirkin
A problem was reported with this one.
I fixed it up, will send v2 pull.I accidentally just merged the v1 by mistake :-(
Sorry about that; I'm going to merge in the v2 (it conflicts invhost_net.cbut fairly trivially so I'll fix that up) and then we should get to where we were intending to go. Apologies for the error.
As discussed on IRC, reverted the incorrect vhost_net commit and then cleanly merged in the v2 pull.
Apologies again for this mess.
-- PMM