xref: /qemu/include/hw/i386/intel_iommu.h (revision 1e06f131fd9a44dd4af223875660802fd2a3441f)
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)
40*1e06f131SMichael 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;
52d92fa2dcSLe Tan 
53d92fa2dcSLe Tan /* Context-Entry */
54d92fa2dcSLe Tan struct VTDContextEntry {
55d92fa2dcSLe Tan     uint64_t lo;
56d92fa2dcSLe Tan     uint64_t hi;
57d92fa2dcSLe Tan };
58d92fa2dcSLe Tan 
59d92fa2dcSLe Tan struct VTDContextCacheEntry {
60d92fa2dcSLe Tan     /* The cache entry is obsolete if
61d92fa2dcSLe Tan      * context_cache_gen!=IntelIOMMUState.context_cache_gen
62d92fa2dcSLe Tan      */
63d92fa2dcSLe Tan     uint32_t context_cache_gen;
64d92fa2dcSLe Tan     struct VTDContextEntry context_entry;
65d92fa2dcSLe Tan };
66d92fa2dcSLe Tan 
671da12ec4SLe Tan struct VTDAddressSpace {
681da12ec4SLe Tan     uint8_t bus_num;
691da12ec4SLe Tan     uint8_t devfn;
701da12ec4SLe Tan     AddressSpace as;
711da12ec4SLe Tan     MemoryRegion iommu;
721da12ec4SLe Tan     IntelIOMMUState *iommu_state;
73d92fa2dcSLe Tan     VTDContextCacheEntry context_cache_entry;
741da12ec4SLe Tan };
751da12ec4SLe Tan 
76b5a280c0SLe Tan struct VTDIOTLBEntry {
77b5a280c0SLe Tan     uint64_t gfn;
78b5a280c0SLe Tan     uint16_t domain_id;
79b5a280c0SLe Tan     uint64_t slpte;
80b5a280c0SLe Tan     bool read_flags;
81b5a280c0SLe Tan     bool write_flags;
82b5a280c0SLe Tan };
83b5a280c0SLe Tan 
841da12ec4SLe Tan /* The iommu (DMAR) device state struct */
851da12ec4SLe Tan struct IntelIOMMUState {
861da12ec4SLe Tan     SysBusDevice busdev;
871da12ec4SLe Tan     MemoryRegion csrmem;
881da12ec4SLe Tan     uint8_t csr[DMAR_REG_SIZE];     /* register values */
891da12ec4SLe Tan     uint8_t wmask[DMAR_REG_SIZE];   /* R/W bytes */
901da12ec4SLe Tan     uint8_t w1cmask[DMAR_REG_SIZE]; /* RW1C(Write 1 to Clear) bytes */
911da12ec4SLe Tan     uint8_t womask[DMAR_REG_SIZE];  /* WO (write only - read returns 0) */
921da12ec4SLe Tan     uint32_t version;
931da12ec4SLe Tan 
941da12ec4SLe Tan     dma_addr_t root;                /* Current root table pointer */
951da12ec4SLe Tan     bool root_extended;             /* Type of root table (extended or not) */
961da12ec4SLe Tan     bool dmar_enabled;              /* Set if DMA remapping is enabled */
971da12ec4SLe Tan 
981da12ec4SLe Tan     uint16_t iq_head;               /* Current invalidation queue head */
991da12ec4SLe Tan     uint16_t iq_tail;               /* Current invalidation queue tail */
1001da12ec4SLe Tan     dma_addr_t iq;                  /* Current invalidation queue pointer */
1011da12ec4SLe Tan     uint16_t iq_size;               /* IQ Size in number of entries */
1021da12ec4SLe Tan     bool qi_enabled;                /* Set if the QI is enabled */
1031da12ec4SLe Tan     uint8_t iq_last_desc_type;      /* The type of last completed descriptor */
1041da12ec4SLe Tan 
1051da12ec4SLe Tan     /* The index of the Fault Recording Register to be used next.
1061da12ec4SLe Tan      * Wraps around from N-1 to 0, where N is the number of FRCD_REG.
1071da12ec4SLe Tan      */
1081da12ec4SLe Tan     uint16_t next_frcd_reg;
1091da12ec4SLe Tan 
1101da12ec4SLe Tan     uint64_t cap;                   /* The value of capability reg */
1111da12ec4SLe Tan     uint64_t ecap;                  /* The value of extended capability reg */
1121da12ec4SLe Tan 
113d92fa2dcSLe Tan     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
114b5a280c0SLe Tan     GHashTable *iotlb;              /* IOTLB */
115d92fa2dcSLe Tan 
1161da12ec4SLe Tan     MemoryRegionIOMMUOps iommu_ops;
1171da12ec4SLe Tan     VTDAddressSpace **address_spaces[VTD_PCI_BUS_MAX];
1181da12ec4SLe Tan };
1191da12ec4SLe Tan 
1201da12ec4SLe Tan #endif
121