11da12ec4SLe Tan /* 21da12ec4SLe Tan * QEMU emulation of an Intel IOMMU (VT-d) 31da12ec4SLe Tan * (DMA Remapping device) 41da12ec4SLe Tan * 51da12ec4SLe Tan * Copyright (C) 2013 Knut Omang, Oracle <knut.omang@oracle.com> 61da12ec4SLe Tan * Copyright (C) 2014 Le Tan, <tamlokveer@gmail.com> 71da12ec4SLe Tan * 81da12ec4SLe Tan * This program is free software; you can redistribute it and/or modify 91da12ec4SLe Tan * it under the terms of the GNU General Public License as published by 101da12ec4SLe Tan * the Free Software Foundation; either version 2 of the License, or 111da12ec4SLe Tan * (at your option) any later version. 121da12ec4SLe Tan 131da12ec4SLe Tan * This program is distributed in the hope that it will be useful, 141da12ec4SLe Tan * but WITHOUT ANY WARRANTY; without even the implied warranty of 151da12ec4SLe Tan * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 161da12ec4SLe Tan * GNU General Public License for more details. 171da12ec4SLe Tan 181da12ec4SLe Tan * You should have received a copy of the GNU General Public License along 191da12ec4SLe Tan * with this program; if not, see <http://www.gnu.org/licenses/>. 201da12ec4SLe Tan */ 211da12ec4SLe Tan 221da12ec4SLe Tan #ifndef INTEL_IOMMU_H 231da12ec4SLe Tan #define INTEL_IOMMU_H 241da12ec4SLe Tan #include "hw/qdev.h" 251da12ec4SLe Tan #include "sysemu/dma.h" 261da12ec4SLe Tan 271da12ec4SLe Tan #define TYPE_INTEL_IOMMU_DEVICE "intel-iommu" 281da12ec4SLe Tan #define INTEL_IOMMU_DEVICE(obj) \ 291da12ec4SLe Tan OBJECT_CHECK(IntelIOMMUState, (obj), TYPE_INTEL_IOMMU_DEVICE) 301da12ec4SLe Tan 311da12ec4SLe Tan /* DMAR Hardware Unit Definition address (IOMMU unit) */ 321da12ec4SLe Tan #define Q35_HOST_BRIDGE_IOMMU_ADDR 0xfed90000ULL 331da12ec4SLe Tan 341da12ec4SLe Tan #define VTD_PCI_BUS_MAX 256 351da12ec4SLe Tan #define VTD_PCI_SLOT_MAX 32 361da12ec4SLe Tan #define VTD_PCI_FUNC_MAX 8 371da12ec4SLe Tan #define VTD_PCI_DEVFN_MAX 256 381da12ec4SLe Tan #define VTD_PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f) 391da12ec4SLe Tan #define VTD_PCI_FUNC(devfn) ((devfn) & 0x07) 401e06f131SMichael S. Tsirkin #define VTD_SID_TO_BUS(sid) (((sid) >> 8) & 0xff) 41d92fa2dcSLe Tan #define VTD_SID_TO_DEVFN(sid) ((sid) & 0xff) 421da12ec4SLe Tan 431da12ec4SLe Tan #define DMAR_REG_SIZE 0x230 441da12ec4SLe Tan #define VTD_HOST_ADDRESS_WIDTH 39 451da12ec4SLe Tan #define VTD_HAW_MASK ((1ULL << VTD_HOST_ADDRESS_WIDTH) - 1) 461da12ec4SLe Tan 47d92fa2dcSLe Tan typedef struct VTDContextEntry VTDContextEntry; 48d92fa2dcSLe Tan typedef struct VTDContextCacheEntry VTDContextCacheEntry; 491da12ec4SLe Tan typedef struct IntelIOMMUState IntelIOMMUState; 501da12ec4SLe Tan typedef struct VTDAddressSpace VTDAddressSpace; 51b5a280c0SLe Tan typedef struct VTDIOTLBEntry VTDIOTLBEntry; 52*7df953bdSKnut Omang typedef struct VTDBus VTDBus; 53d92fa2dcSLe Tan 54d92fa2dcSLe Tan /* Context-Entry */ 55d92fa2dcSLe Tan struct VTDContextEntry { 56d92fa2dcSLe Tan uint64_t lo; 57d92fa2dcSLe Tan uint64_t hi; 58d92fa2dcSLe Tan }; 59d92fa2dcSLe Tan 60d92fa2dcSLe Tan struct VTDContextCacheEntry { 61d92fa2dcSLe Tan /* The cache entry is obsolete if 62d92fa2dcSLe Tan * context_cache_gen!=IntelIOMMUState.context_cache_gen 63d92fa2dcSLe Tan */ 64d92fa2dcSLe Tan uint32_t context_cache_gen; 65d92fa2dcSLe Tan struct VTDContextEntry context_entry; 66d92fa2dcSLe Tan }; 67d92fa2dcSLe Tan 681da12ec4SLe Tan struct VTDAddressSpace { 69*7df953bdSKnut Omang PCIBus *bus; 701da12ec4SLe Tan uint8_t devfn; 711da12ec4SLe Tan AddressSpace as; 721da12ec4SLe Tan MemoryRegion iommu; 731da12ec4SLe Tan IntelIOMMUState *iommu_state; 74d92fa2dcSLe Tan VTDContextCacheEntry context_cache_entry; 751da12ec4SLe Tan }; 761da12ec4SLe Tan 77*7df953bdSKnut Omang struct VTDBus { 78*7df953bdSKnut Omang PCIBus* bus; /* A reference to the bus to provide translation for */ 79*7df953bdSKnut Omang VTDAddressSpace *dev_as[0]; /* A table of VTDAddressSpace objects indexed by devfn */ 80*7df953bdSKnut Omang }; 81*7df953bdSKnut Omang 82b5a280c0SLe Tan struct VTDIOTLBEntry { 83b5a280c0SLe Tan uint64_t gfn; 84b5a280c0SLe Tan uint16_t domain_id; 85b5a280c0SLe Tan uint64_t slpte; 86b5a280c0SLe Tan bool read_flags; 87b5a280c0SLe Tan bool write_flags; 88b5a280c0SLe Tan }; 89b5a280c0SLe Tan 901da12ec4SLe Tan /* The iommu (DMAR) device state struct */ 911da12ec4SLe Tan struct IntelIOMMUState { 921da12ec4SLe Tan SysBusDevice busdev; 931da12ec4SLe Tan MemoryRegion csrmem; 941da12ec4SLe Tan uint8_t csr[DMAR_REG_SIZE]; /* register values */ 951da12ec4SLe Tan uint8_t wmask[DMAR_REG_SIZE]; /* R/W bytes */ 961da12ec4SLe Tan uint8_t w1cmask[DMAR_REG_SIZE]; /* RW1C(Write 1 to Clear) bytes */ 971da12ec4SLe Tan uint8_t womask[DMAR_REG_SIZE]; /* WO (write only - read returns 0) */ 981da12ec4SLe Tan uint32_t version; 991da12ec4SLe Tan 1001da12ec4SLe Tan dma_addr_t root; /* Current root table pointer */ 1011da12ec4SLe Tan bool root_extended; /* Type of root table (extended or not) */ 1021da12ec4SLe Tan bool dmar_enabled; /* Set if DMA remapping is enabled */ 1031da12ec4SLe Tan 1041da12ec4SLe Tan uint16_t iq_head; /* Current invalidation queue head */ 1051da12ec4SLe Tan uint16_t iq_tail; /* Current invalidation queue tail */ 1061da12ec4SLe Tan dma_addr_t iq; /* Current invalidation queue pointer */ 1071da12ec4SLe Tan uint16_t iq_size; /* IQ Size in number of entries */ 1081da12ec4SLe Tan bool qi_enabled; /* Set if the QI is enabled */ 1091da12ec4SLe Tan uint8_t iq_last_desc_type; /* The type of last completed descriptor */ 1101da12ec4SLe Tan 1111da12ec4SLe Tan /* The index of the Fault Recording Register to be used next. 1121da12ec4SLe Tan * Wraps around from N-1 to 0, where N is the number of FRCD_REG. 1131da12ec4SLe Tan */ 1141da12ec4SLe Tan uint16_t next_frcd_reg; 1151da12ec4SLe Tan 1161da12ec4SLe Tan uint64_t cap; /* The value of capability reg */ 1171da12ec4SLe Tan uint64_t ecap; /* The value of extended capability reg */ 1181da12ec4SLe Tan 119d92fa2dcSLe Tan uint32_t context_cache_gen; /* Should be in [1,MAX] */ 120b5a280c0SLe Tan GHashTable *iotlb; /* IOTLB */ 121d92fa2dcSLe Tan 1221da12ec4SLe Tan MemoryRegionIOMMUOps iommu_ops; 123*7df953bdSKnut Omang GHashTable *vtd_as_by_busptr; /* VTDBus objects indexed by PCIBus* reference */ 124*7df953bdSKnut Omang VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX]; /* VTDBus objects indexed by bus number */ 1251da12ec4SLe Tan }; 1261da12ec4SLe Tan 127*7df953bdSKnut Omang /* Find the VTD Address space associated with the given bus pointer, 128*7df953bdSKnut Omang * create a new one if none exists 129*7df953bdSKnut Omang */ 130*7df953bdSKnut Omang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn); 131*7df953bdSKnut Omang 1321da12ec4SLe Tan #endif 133