xref: /qemu/include/hw/i386/intel_iommu.h (revision d92fa2dc6e42e8bd8470e69d85141176f98feaea)
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*d92fa2dcSLe Tan #define VTD_SID_TO_BUS(sid)         (((sid) >> 8) && 0xff)
41*d92fa2dcSLe 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 
47*d92fa2dcSLe Tan typedef struct VTDContextEntry VTDContextEntry;
48*d92fa2dcSLe Tan typedef struct VTDContextCacheEntry VTDContextCacheEntry;
491da12ec4SLe Tan typedef struct IntelIOMMUState IntelIOMMUState;
501da12ec4SLe Tan typedef struct VTDAddressSpace VTDAddressSpace;
511da12ec4SLe Tan 
52*d92fa2dcSLe Tan 
53*d92fa2dcSLe Tan /* Context-Entry */
54*d92fa2dcSLe Tan struct VTDContextEntry {
55*d92fa2dcSLe Tan     uint64_t lo;
56*d92fa2dcSLe Tan     uint64_t hi;
57*d92fa2dcSLe Tan };
58*d92fa2dcSLe Tan 
59*d92fa2dcSLe Tan struct VTDContextCacheEntry {
60*d92fa2dcSLe Tan     /* The cache entry is obsolete if
61*d92fa2dcSLe Tan      * context_cache_gen!=IntelIOMMUState.context_cache_gen
62*d92fa2dcSLe Tan      */
63*d92fa2dcSLe Tan     uint32_t context_cache_gen;
64*d92fa2dcSLe Tan     struct VTDContextEntry context_entry;
65*d92fa2dcSLe Tan };
66*d92fa2dcSLe 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;
73*d92fa2dcSLe Tan     VTDContextCacheEntry context_cache_entry;
741da12ec4SLe Tan };
751da12ec4SLe Tan 
761da12ec4SLe Tan /* The iommu (DMAR) device state struct */
771da12ec4SLe Tan struct IntelIOMMUState {
781da12ec4SLe Tan     SysBusDevice busdev;
791da12ec4SLe Tan     MemoryRegion csrmem;
801da12ec4SLe Tan     uint8_t csr[DMAR_REG_SIZE];     /* register values */
811da12ec4SLe Tan     uint8_t wmask[DMAR_REG_SIZE];   /* R/W bytes */
821da12ec4SLe Tan     uint8_t w1cmask[DMAR_REG_SIZE]; /* RW1C(Write 1 to Clear) bytes */
831da12ec4SLe Tan     uint8_t womask[DMAR_REG_SIZE];  /* WO (write only - read returns 0) */
841da12ec4SLe Tan     uint32_t version;
851da12ec4SLe Tan 
861da12ec4SLe Tan     dma_addr_t root;                /* Current root table pointer */
871da12ec4SLe Tan     bool root_extended;             /* Type of root table (extended or not) */
881da12ec4SLe Tan     bool dmar_enabled;              /* Set if DMA remapping is enabled */
891da12ec4SLe Tan 
901da12ec4SLe Tan     uint16_t iq_head;               /* Current invalidation queue head */
911da12ec4SLe Tan     uint16_t iq_tail;               /* Current invalidation queue tail */
921da12ec4SLe Tan     dma_addr_t iq;                  /* Current invalidation queue pointer */
931da12ec4SLe Tan     uint16_t iq_size;               /* IQ Size in number of entries */
941da12ec4SLe Tan     bool qi_enabled;                /* Set if the QI is enabled */
951da12ec4SLe Tan     uint8_t iq_last_desc_type;      /* The type of last completed descriptor */
961da12ec4SLe Tan 
971da12ec4SLe Tan     /* The index of the Fault Recording Register to be used next.
981da12ec4SLe Tan      * Wraps around from N-1 to 0, where N is the number of FRCD_REG.
991da12ec4SLe Tan      */
1001da12ec4SLe Tan     uint16_t next_frcd_reg;
1011da12ec4SLe Tan 
1021da12ec4SLe Tan     uint64_t cap;                   /* The value of capability reg */
1031da12ec4SLe Tan     uint64_t ecap;                  /* The value of extended capability reg */
1041da12ec4SLe Tan 
105*d92fa2dcSLe Tan     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
106*d92fa2dcSLe Tan 
1071da12ec4SLe Tan     MemoryRegionIOMMUOps iommu_ops;
1081da12ec4SLe Tan     VTDAddressSpace **address_spaces[VTD_PCI_BUS_MAX];
1091da12ec4SLe Tan };
1101da12ec4SLe Tan 
1111da12ec4SLe Tan #endif
112