xref: /qemu/include/hw/i386/intel_iommu.h (revision 1c7955c4503211f45a83e6480bd876f079ba5cd8)
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"
26*1c7955c4SPeter Xu #include "hw/i386/x86-iommu.h"
271da12ec4SLe Tan 
281da12ec4SLe Tan #define TYPE_INTEL_IOMMU_DEVICE "intel-iommu"
291da12ec4SLe Tan #define INTEL_IOMMU_DEVICE(obj) \
301da12ec4SLe Tan      OBJECT_CHECK(IntelIOMMUState, (obj), TYPE_INTEL_IOMMU_DEVICE)
311da12ec4SLe Tan 
321da12ec4SLe Tan /* DMAR Hardware Unit Definition address (IOMMU unit) */
331da12ec4SLe Tan #define Q35_HOST_BRIDGE_IOMMU_ADDR  0xfed90000ULL
341da12ec4SLe Tan 
351da12ec4SLe Tan #define VTD_PCI_BUS_MAX             256
361da12ec4SLe Tan #define VTD_PCI_SLOT_MAX            32
371da12ec4SLe Tan #define VTD_PCI_FUNC_MAX            8
381da12ec4SLe Tan #define VTD_PCI_DEVFN_MAX           256
391da12ec4SLe Tan #define VTD_PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
401da12ec4SLe Tan #define VTD_PCI_FUNC(devfn)         ((devfn) & 0x07)
411e06f131SMichael S. Tsirkin #define VTD_SID_TO_BUS(sid)         (((sid) >> 8) & 0xff)
42d92fa2dcSLe Tan #define VTD_SID_TO_DEVFN(sid)       ((sid) & 0xff)
431da12ec4SLe Tan 
441da12ec4SLe Tan #define DMAR_REG_SIZE               0x230
451da12ec4SLe Tan #define VTD_HOST_ADDRESS_WIDTH      39
461da12ec4SLe Tan #define VTD_HAW_MASK                ((1ULL << VTD_HOST_ADDRESS_WIDTH) - 1)
471da12ec4SLe Tan 
48d92fa2dcSLe Tan typedef struct VTDContextEntry VTDContextEntry;
49d92fa2dcSLe Tan typedef struct VTDContextCacheEntry VTDContextCacheEntry;
501da12ec4SLe Tan typedef struct IntelIOMMUState IntelIOMMUState;
511da12ec4SLe Tan typedef struct VTDAddressSpace VTDAddressSpace;
52b5a280c0SLe Tan typedef struct VTDIOTLBEntry VTDIOTLBEntry;
537df953bdSKnut Omang typedef struct VTDBus VTDBus;
54d92fa2dcSLe Tan 
55d92fa2dcSLe Tan /* Context-Entry */
56d92fa2dcSLe Tan struct VTDContextEntry {
57d92fa2dcSLe Tan     uint64_t lo;
58d92fa2dcSLe Tan     uint64_t hi;
59d92fa2dcSLe Tan };
60d92fa2dcSLe Tan 
61d92fa2dcSLe Tan struct VTDContextCacheEntry {
62d92fa2dcSLe Tan     /* The cache entry is obsolete if
63d92fa2dcSLe Tan      * context_cache_gen!=IntelIOMMUState.context_cache_gen
64d92fa2dcSLe Tan      */
65d92fa2dcSLe Tan     uint32_t context_cache_gen;
66d92fa2dcSLe Tan     struct VTDContextEntry context_entry;
67d92fa2dcSLe Tan };
68d92fa2dcSLe Tan 
691da12ec4SLe Tan struct VTDAddressSpace {
707df953bdSKnut Omang     PCIBus *bus;
711da12ec4SLe Tan     uint8_t devfn;
721da12ec4SLe Tan     AddressSpace as;
731da12ec4SLe Tan     MemoryRegion iommu;
741da12ec4SLe Tan     IntelIOMMUState *iommu_state;
75d92fa2dcSLe Tan     VTDContextCacheEntry context_cache_entry;
761da12ec4SLe Tan };
771da12ec4SLe Tan 
787df953bdSKnut Omang struct VTDBus {
797df953bdSKnut Omang     PCIBus* bus;		/* A reference to the bus to provide translation for */
807df953bdSKnut Omang     VTDAddressSpace *dev_as[0];	/* A table of VTDAddressSpace objects indexed by devfn */
817df953bdSKnut Omang };
827df953bdSKnut Omang 
83b5a280c0SLe Tan struct VTDIOTLBEntry {
84b5a280c0SLe Tan     uint64_t gfn;
85b5a280c0SLe Tan     uint16_t domain_id;
86b5a280c0SLe Tan     uint64_t slpte;
87d66b969bSJason Wang     uint64_t mask;
88b5a280c0SLe Tan     bool read_flags;
89b5a280c0SLe Tan     bool write_flags;
90b5a280c0SLe Tan };
91b5a280c0SLe Tan 
921da12ec4SLe Tan /* The iommu (DMAR) device state struct */
931da12ec4SLe Tan struct IntelIOMMUState {
94*1c7955c4SPeter Xu     X86IOMMUState x86_iommu;
951da12ec4SLe Tan     MemoryRegion csrmem;
961da12ec4SLe Tan     uint8_t csr[DMAR_REG_SIZE];     /* register values */
971da12ec4SLe Tan     uint8_t wmask[DMAR_REG_SIZE];   /* R/W bytes */
981da12ec4SLe Tan     uint8_t w1cmask[DMAR_REG_SIZE]; /* RW1C(Write 1 to Clear) bytes */
991da12ec4SLe Tan     uint8_t womask[DMAR_REG_SIZE];  /* WO (write only - read returns 0) */
1001da12ec4SLe Tan     uint32_t version;
1011da12ec4SLe Tan 
1021da12ec4SLe Tan     dma_addr_t root;                /* Current root table pointer */
1031da12ec4SLe Tan     bool root_extended;             /* Type of root table (extended or not) */
1041da12ec4SLe Tan     bool dmar_enabled;              /* Set if DMA remapping is enabled */
1051da12ec4SLe Tan 
1061da12ec4SLe Tan     uint16_t iq_head;               /* Current invalidation queue head */
1071da12ec4SLe Tan     uint16_t iq_tail;               /* Current invalidation queue tail */
1081da12ec4SLe Tan     dma_addr_t iq;                  /* Current invalidation queue pointer */
1091da12ec4SLe Tan     uint16_t iq_size;               /* IQ Size in number of entries */
1101da12ec4SLe Tan     bool qi_enabled;                /* Set if the QI is enabled */
1111da12ec4SLe Tan     uint8_t iq_last_desc_type;      /* The type of last completed descriptor */
1121da12ec4SLe Tan 
1131da12ec4SLe Tan     /* The index of the Fault Recording Register to be used next.
1141da12ec4SLe Tan      * Wraps around from N-1 to 0, where N is the number of FRCD_REG.
1151da12ec4SLe Tan      */
1161da12ec4SLe Tan     uint16_t next_frcd_reg;
1171da12ec4SLe Tan 
1181da12ec4SLe Tan     uint64_t cap;                   /* The value of capability reg */
1191da12ec4SLe Tan     uint64_t ecap;                  /* The value of extended capability reg */
1201da12ec4SLe Tan 
121d92fa2dcSLe Tan     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
122b5a280c0SLe Tan     GHashTable *iotlb;              /* IOTLB */
123d92fa2dcSLe Tan 
1241da12ec4SLe Tan     MemoryRegionIOMMUOps iommu_ops;
1257df953bdSKnut Omang     GHashTable *vtd_as_by_busptr;   /* VTDBus objects indexed by PCIBus* reference */
1267df953bdSKnut Omang     VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX]; /* VTDBus objects indexed by bus number */
1271da12ec4SLe Tan };
1281da12ec4SLe Tan 
1297df953bdSKnut Omang /* Find the VTD Address space associated with the given bus pointer,
1307df953bdSKnut Omang  * create a new one if none exists
1317df953bdSKnut Omang  */
1327df953bdSKnut Omang VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn);
1337df953bdSKnut Omang 
1341da12ec4SLe Tan #endif
135