xref: /qemu/include/hw/i386/x86-iommu.h (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
11c7955c4SPeter Xu /*
21c7955c4SPeter Xu  * Common IOMMU interface for X86 platform
31c7955c4SPeter Xu  *
41c7955c4SPeter Xu  * Copyright (C) 2016 Peter Xu, Red Hat <peterx@redhat.com>
51c7955c4SPeter Xu  *
61c7955c4SPeter Xu  * This program is free software; you can redistribute it and/or modify
71c7955c4SPeter Xu  * it under the terms of the GNU General Public License as published by
81c7955c4SPeter Xu  * the Free Software Foundation; either version 2 of the License, or
91c7955c4SPeter Xu  * (at your option) any later version.
101c7955c4SPeter Xu 
111c7955c4SPeter Xu  * This program is distributed in the hope that it will be useful,
121c7955c4SPeter Xu  * but WITHOUT ANY WARRANTY; without even the implied warranty of
131c7955c4SPeter Xu  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141c7955c4SPeter Xu  * GNU General Public License for more details.
151c7955c4SPeter Xu 
161c7955c4SPeter Xu  * You should have received a copy of the GNU General Public License along
171c7955c4SPeter Xu  * with this program; if not, see <http://www.gnu.org/licenses/>.
181c7955c4SPeter Xu  */
191c7955c4SPeter Xu 
2058ea30f5SMarkus Armbruster #ifndef HW_I386_X86_IOMMU_H
2158ea30f5SMarkus Armbruster #define HW_I386_X86_IOMMU_H
221c7955c4SPeter Xu 
231c7955c4SPeter Xu #include "hw/sysbus.h"
248b5ed7dfSPeter Xu #include "hw/pci/pci.h"
2535c24501SSingh, Brijesh #include "hw/pci/msi.h"
26*db1015e9SEduardo Habkost #include "qom/object.h"
271c7955c4SPeter Xu 
281c7955c4SPeter Xu #define  TYPE_X86_IOMMU_DEVICE  ("x86-iommu")
29*db1015e9SEduardo Habkost typedef struct X86IOMMUClass X86IOMMUClass;
30*db1015e9SEduardo Habkost typedef struct X86IOMMUState X86IOMMUState;
311c7955c4SPeter Xu #define  X86_IOMMU_DEVICE(obj) \
321c7955c4SPeter Xu     OBJECT_CHECK(X86IOMMUState, (obj), TYPE_X86_IOMMU_DEVICE)
3330c60f77SEduardo Habkost #define  X86_IOMMU_DEVICE_CLASS(klass) \
341c7955c4SPeter Xu     OBJECT_CLASS_CHECK(X86IOMMUClass, (klass), TYPE_X86_IOMMU_DEVICE)
3530c60f77SEduardo Habkost #define  X86_IOMMU_DEVICE_GET_CLASS(obj) \
361c7955c4SPeter Xu     OBJECT_GET_CLASS(X86IOMMUClass, obj, TYPE_X86_IOMMU_DEVICE)
371c7955c4SPeter Xu 
388b5ed7dfSPeter Xu #define X86_IOMMU_SID_INVALID             (0xffff)
3904af0e18SPeter Xu 
4035c24501SSingh, Brijesh typedef struct X86IOMMUIrq X86IOMMUIrq;
4135c24501SSingh, Brijesh typedef struct X86IOMMU_MSIMessage X86IOMMU_MSIMessage;
421c7955c4SPeter Xu 
43fb9f5926SDavid Kiarie typedef enum IommuType {
44fb9f5926SDavid Kiarie     TYPE_INTEL,
45fb9f5926SDavid Kiarie     TYPE_AMD,
46fb9f5926SDavid Kiarie     TYPE_NONE
47fb9f5926SDavid Kiarie } IommuType;
48fb9f5926SDavid Kiarie 
491c7955c4SPeter Xu struct X86IOMMUClass {
501c7955c4SPeter Xu     SysBusDeviceClass parent;
511c7955c4SPeter Xu     /* Intel/AMD specific realize() hook */
521c7955c4SPeter Xu     DeviceRealize realize;
538b5ed7dfSPeter Xu     /* MSI-based interrupt remapping */
548b5ed7dfSPeter Xu     int (*int_remap)(X86IOMMUState *iommu, MSIMessage *src,
558b5ed7dfSPeter Xu                      MSIMessage *dst, uint16_t sid);
561c7955c4SPeter Xu };
571c7955c4SPeter Xu 
5802a2cbc8SPeter Xu /**
5902a2cbc8SPeter Xu  * iec_notify_fn - IEC (Interrupt Entry Cache) notifier hook,
6002a2cbc8SPeter Xu  *                 triggered when IR invalidation happens.
6102a2cbc8SPeter Xu  * @private: private data
6202a2cbc8SPeter Xu  * @global: whether this is a global IEC invalidation
6302a2cbc8SPeter Xu  * @index: IRTE index to invalidate (start from)
6402a2cbc8SPeter Xu  * @mask: invalidation mask
6502a2cbc8SPeter Xu  */
6602a2cbc8SPeter Xu typedef void (*iec_notify_fn)(void *private, bool global,
6702a2cbc8SPeter Xu                               uint32_t index, uint32_t mask);
6802a2cbc8SPeter Xu 
6902a2cbc8SPeter Xu struct IEC_Notifier {
7002a2cbc8SPeter Xu     iec_notify_fn iec_notify;
7102a2cbc8SPeter Xu     void *private;
7202a2cbc8SPeter Xu     QLIST_ENTRY(IEC_Notifier) list;
7302a2cbc8SPeter Xu };
7402a2cbc8SPeter Xu typedef struct IEC_Notifier IEC_Notifier;
7502a2cbc8SPeter Xu 
761c7955c4SPeter Xu struct X86IOMMUState {
771c7955c4SPeter Xu     SysBusDevice busdev;
78a924b3d8SPeter Xu     OnOffAuto intr_supported;   /* Whether vIOMMU supports IR */
79554f5e16SJason Wang     bool dt_supported;          /* Whether vIOMMU supports DT */
80dbaabb25SPeter Xu     bool pt_supported;          /* Whether vIOMMU supports pass-through */
81fb9f5926SDavid Kiarie     IommuType type;             /* IOMMU type - AMD/Intel     */
8202a2cbc8SPeter Xu     QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */
831c7955c4SPeter Xu };
841c7955c4SPeter Xu 
85a924b3d8SPeter Xu bool x86_iommu_ir_supported(X86IOMMUState *s);
86a924b3d8SPeter Xu 
8735c24501SSingh, Brijesh /* Generic IRQ entry information when interrupt remapping is enabled */
8835c24501SSingh, Brijesh struct X86IOMMUIrq {
8935c24501SSingh, Brijesh     /* Used by both IOAPIC/MSI interrupt remapping */
9035c24501SSingh, Brijesh     uint8_t trigger_mode;
9135c24501SSingh, Brijesh     uint8_t vector;
9235c24501SSingh, Brijesh     uint8_t delivery_mode;
9335c24501SSingh, Brijesh     uint32_t dest;
9435c24501SSingh, Brijesh     uint8_t dest_mode;
9535c24501SSingh, Brijesh 
9635c24501SSingh, Brijesh     /* only used by MSI interrupt remapping */
9735c24501SSingh, Brijesh     uint8_t redir_hint;
9835c24501SSingh, Brijesh     uint8_t msi_addr_last_bits;
9935c24501SSingh, Brijesh };
10035c24501SSingh, Brijesh 
10135c24501SSingh, Brijesh struct X86IOMMU_MSIMessage {
10235c24501SSingh, Brijesh     union {
10335c24501SSingh, Brijesh         struct {
10435c24501SSingh, Brijesh #ifdef HOST_WORDS_BIGENDIAN
10535c24501SSingh, Brijesh             uint32_t __addr_head:12; /* 0xfee */
10635c24501SSingh, Brijesh             uint32_t dest:8;
10735c24501SSingh, Brijesh             uint32_t __reserved:8;
10835c24501SSingh, Brijesh             uint32_t redir_hint:1;
10935c24501SSingh, Brijesh             uint32_t dest_mode:1;
11035c24501SSingh, Brijesh             uint32_t __not_used:2;
11135c24501SSingh, Brijesh #else
11235c24501SSingh, Brijesh             uint32_t __not_used:2;
11335c24501SSingh, Brijesh             uint32_t dest_mode:1;
11435c24501SSingh, Brijesh             uint32_t redir_hint:1;
11535c24501SSingh, Brijesh             uint32_t __reserved:8;
11635c24501SSingh, Brijesh             uint32_t dest:8;
11735c24501SSingh, Brijesh             uint32_t __addr_head:12; /* 0xfee */
11835c24501SSingh, Brijesh #endif
11935c24501SSingh, Brijesh             uint32_t __addr_hi;
12035c24501SSingh, Brijesh         } QEMU_PACKED;
12135c24501SSingh, Brijesh         uint64_t msi_addr;
12235c24501SSingh, Brijesh     };
12335c24501SSingh, Brijesh     union {
12435c24501SSingh, Brijesh         struct {
12535c24501SSingh, Brijesh #ifdef HOST_WORDS_BIGENDIAN
12635c24501SSingh, Brijesh             uint16_t trigger_mode:1;
12735c24501SSingh, Brijesh             uint16_t level:1;
12835c24501SSingh, Brijesh             uint16_t __resved:3;
12935c24501SSingh, Brijesh             uint16_t delivery_mode:3;
13035c24501SSingh, Brijesh             uint16_t vector:8;
13135c24501SSingh, Brijesh #else
13235c24501SSingh, Brijesh             uint16_t vector:8;
13335c24501SSingh, Brijesh             uint16_t delivery_mode:3;
13435c24501SSingh, Brijesh             uint16_t __resved:3;
13535c24501SSingh, Brijesh             uint16_t level:1;
13635c24501SSingh, Brijesh             uint16_t trigger_mode:1;
13735c24501SSingh, Brijesh #endif
13835c24501SSingh, Brijesh             uint16_t __resved1;
13935c24501SSingh, Brijesh         } QEMU_PACKED;
14035c24501SSingh, Brijesh         uint32_t msi_data;
14135c24501SSingh, Brijesh     };
14235c24501SSingh, Brijesh };
14335c24501SSingh, Brijesh 
1441cf5fd57SPeter Xu /**
1451cf5fd57SPeter Xu  * x86_iommu_get_default - get default IOMMU device
1461cf5fd57SPeter Xu  * @return: pointer to default IOMMU device
1471cf5fd57SPeter Xu  */
1481cf5fd57SPeter Xu X86IOMMUState *x86_iommu_get_default(void);
1491cf5fd57SPeter Xu 
150fb9f5926SDavid Kiarie /*
151fb9f5926SDavid Kiarie  * x86_iommu_get_type - get IOMMU type
152fb9f5926SDavid Kiarie  */
153fb9f5926SDavid Kiarie IommuType x86_iommu_get_type(void);
154fb9f5926SDavid Kiarie 
15502a2cbc8SPeter Xu /**
15602a2cbc8SPeter Xu  * x86_iommu_iec_register_notifier - register IEC (Interrupt Entry
15702a2cbc8SPeter Xu  *                                   Cache) notifiers
15802a2cbc8SPeter Xu  * @iommu: IOMMU device to register
15902a2cbc8SPeter Xu  * @fn: IEC notifier hook function
16002a2cbc8SPeter Xu  * @data: notifier private data
16102a2cbc8SPeter Xu  */
16202a2cbc8SPeter Xu void x86_iommu_iec_register_notifier(X86IOMMUState *iommu,
16302a2cbc8SPeter Xu                                      iec_notify_fn fn, void *data);
16402a2cbc8SPeter Xu 
16502a2cbc8SPeter Xu /**
16602a2cbc8SPeter Xu  * x86_iommu_iec_notify_all - Notify IEC invalidations
16702a2cbc8SPeter Xu  * @iommu: IOMMU device that sends the notification
16802a2cbc8SPeter Xu  * @global: whether this is a global invalidation. If true, @index
16902a2cbc8SPeter Xu  *          and @mask are undefined.
17002a2cbc8SPeter Xu  * @index: starting index of interrupt entry to invalidate
17102a2cbc8SPeter Xu  * @mask: index mask for the invalidation
17202a2cbc8SPeter Xu  */
17302a2cbc8SPeter Xu void x86_iommu_iec_notify_all(X86IOMMUState *iommu, bool global,
17402a2cbc8SPeter Xu                               uint32_t index, uint32_t mask);
17502a2cbc8SPeter Xu 
17635c24501SSingh, Brijesh /**
17735c24501SSingh, Brijesh  * x86_iommu_irq_to_msi_message - Populate one MSIMessage from X86IOMMUIrq
17835c24501SSingh, Brijesh  * @X86IOMMUIrq: The IRQ information
17935c24501SSingh, Brijesh  * @out: Output MSI message
18035c24501SSingh, Brijesh  */
18135c24501SSingh, Brijesh void x86_iommu_irq_to_msi_message(X86IOMMUIrq *irq, MSIMessage *out);
1821c7955c4SPeter Xu #endif
183