xref: /qemu/include/hw/i386/x86-iommu.h (revision a924b3d8df55a395891fd5ed341d0deb135d9aa6)
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 
201c7955c4SPeter Xu #ifndef IOMMU_COMMON_H
211c7955c4SPeter Xu #define IOMMU_COMMON_H
221c7955c4SPeter Xu 
231c7955c4SPeter Xu #include "hw/sysbus.h"
248b5ed7dfSPeter Xu #include "hw/pci/pci.h"
2535c24501SSingh, Brijesh #include "hw/pci/msi.h"
261c7955c4SPeter Xu 
271c7955c4SPeter Xu #define  TYPE_X86_IOMMU_DEVICE  ("x86-iommu")
281c7955c4SPeter Xu #define  X86_IOMMU_DEVICE(obj) \
291c7955c4SPeter Xu     OBJECT_CHECK(X86IOMMUState, (obj), TYPE_X86_IOMMU_DEVICE)
301c7955c4SPeter Xu #define  X86_IOMMU_CLASS(klass) \
311c7955c4SPeter Xu     OBJECT_CLASS_CHECK(X86IOMMUClass, (klass), TYPE_X86_IOMMU_DEVICE)
321c7955c4SPeter Xu #define  X86_IOMMU_GET_CLASS(obj) \
331c7955c4SPeter Xu     OBJECT_GET_CLASS(X86IOMMUClass, obj, TYPE_X86_IOMMU_DEVICE)
341c7955c4SPeter Xu 
358b5ed7dfSPeter Xu #define X86_IOMMU_SID_INVALID             (0xffff)
3604af0e18SPeter Xu 
371c7955c4SPeter Xu typedef struct X86IOMMUState X86IOMMUState;
381c7955c4SPeter Xu typedef struct X86IOMMUClass X86IOMMUClass;
3935c24501SSingh, Brijesh typedef struct X86IOMMUIrq X86IOMMUIrq;
4035c24501SSingh, Brijesh typedef struct X86IOMMU_MSIMessage X86IOMMU_MSIMessage;
411c7955c4SPeter Xu 
42fb9f5926SDavid Kiarie typedef enum IommuType {
43fb9f5926SDavid Kiarie     TYPE_INTEL,
44fb9f5926SDavid Kiarie     TYPE_AMD,
45fb9f5926SDavid Kiarie     TYPE_NONE
46fb9f5926SDavid Kiarie } IommuType;
47fb9f5926SDavid Kiarie 
481c7955c4SPeter Xu struct X86IOMMUClass {
491c7955c4SPeter Xu     SysBusDeviceClass parent;
501c7955c4SPeter Xu     /* Intel/AMD specific realize() hook */
511c7955c4SPeter Xu     DeviceRealize realize;
528b5ed7dfSPeter Xu     /* MSI-based interrupt remapping */
538b5ed7dfSPeter Xu     int (*int_remap)(X86IOMMUState *iommu, MSIMessage *src,
548b5ed7dfSPeter Xu                      MSIMessage *dst, uint16_t sid);
551c7955c4SPeter Xu };
561c7955c4SPeter Xu 
5702a2cbc8SPeter Xu /**
5802a2cbc8SPeter Xu  * iec_notify_fn - IEC (Interrupt Entry Cache) notifier hook,
5902a2cbc8SPeter Xu  *                 triggered when IR invalidation happens.
6002a2cbc8SPeter Xu  * @private: private data
6102a2cbc8SPeter Xu  * @global: whether this is a global IEC invalidation
6202a2cbc8SPeter Xu  * @index: IRTE index to invalidate (start from)
6302a2cbc8SPeter Xu  * @mask: invalidation mask
6402a2cbc8SPeter Xu  */
6502a2cbc8SPeter Xu typedef void (*iec_notify_fn)(void *private, bool global,
6602a2cbc8SPeter Xu                               uint32_t index, uint32_t mask);
6702a2cbc8SPeter Xu 
6802a2cbc8SPeter Xu struct IEC_Notifier {
6902a2cbc8SPeter Xu     iec_notify_fn iec_notify;
7002a2cbc8SPeter Xu     void *private;
7102a2cbc8SPeter Xu     QLIST_ENTRY(IEC_Notifier) list;
7202a2cbc8SPeter Xu };
7302a2cbc8SPeter Xu typedef struct IEC_Notifier IEC_Notifier;
7402a2cbc8SPeter Xu 
751c7955c4SPeter Xu struct X86IOMMUState {
761c7955c4SPeter Xu     SysBusDevice busdev;
77*a924b3d8SPeter Xu     OnOffAuto intr_supported;   /* Whether vIOMMU supports IR */
78554f5e16SJason Wang     bool dt_supported;          /* Whether vIOMMU supports DT */
79dbaabb25SPeter Xu     bool pt_supported;          /* Whether vIOMMU supports pass-through */
80fb9f5926SDavid Kiarie     IommuType type;             /* IOMMU type - AMD/Intel     */
8102a2cbc8SPeter Xu     QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */
821c7955c4SPeter Xu };
831c7955c4SPeter Xu 
84*a924b3d8SPeter Xu bool x86_iommu_ir_supported(X86IOMMUState *s);
85*a924b3d8SPeter Xu 
8635c24501SSingh, Brijesh /* Generic IRQ entry information when interrupt remapping is enabled */
8735c24501SSingh, Brijesh struct X86IOMMUIrq {
8835c24501SSingh, Brijesh     /* Used by both IOAPIC/MSI interrupt remapping */
8935c24501SSingh, Brijesh     uint8_t trigger_mode;
9035c24501SSingh, Brijesh     uint8_t vector;
9135c24501SSingh, Brijesh     uint8_t delivery_mode;
9235c24501SSingh, Brijesh     uint32_t dest;
9335c24501SSingh, Brijesh     uint8_t dest_mode;
9435c24501SSingh, Brijesh 
9535c24501SSingh, Brijesh     /* only used by MSI interrupt remapping */
9635c24501SSingh, Brijesh     uint8_t redir_hint;
9735c24501SSingh, Brijesh     uint8_t msi_addr_last_bits;
9835c24501SSingh, Brijesh };
9935c24501SSingh, Brijesh 
10035c24501SSingh, Brijesh struct X86IOMMU_MSIMessage {
10135c24501SSingh, Brijesh     union {
10235c24501SSingh, Brijesh         struct {
10335c24501SSingh, Brijesh #ifdef HOST_WORDS_BIGENDIAN
10435c24501SSingh, Brijesh             uint32_t __addr_head:12; /* 0xfee */
10535c24501SSingh, Brijesh             uint32_t dest:8;
10635c24501SSingh, Brijesh             uint32_t __reserved:8;
10735c24501SSingh, Brijesh             uint32_t redir_hint:1;
10835c24501SSingh, Brijesh             uint32_t dest_mode:1;
10935c24501SSingh, Brijesh             uint32_t __not_used:2;
11035c24501SSingh, Brijesh #else
11135c24501SSingh, Brijesh             uint32_t __not_used:2;
11235c24501SSingh, Brijesh             uint32_t dest_mode:1;
11335c24501SSingh, Brijesh             uint32_t redir_hint:1;
11435c24501SSingh, Brijesh             uint32_t __reserved:8;
11535c24501SSingh, Brijesh             uint32_t dest:8;
11635c24501SSingh, Brijesh             uint32_t __addr_head:12; /* 0xfee */
11735c24501SSingh, Brijesh #endif
11835c24501SSingh, Brijesh             uint32_t __addr_hi;
11935c24501SSingh, Brijesh         } QEMU_PACKED;
12035c24501SSingh, Brijesh         uint64_t msi_addr;
12135c24501SSingh, Brijesh     };
12235c24501SSingh, Brijesh     union {
12335c24501SSingh, Brijesh         struct {
12435c24501SSingh, Brijesh #ifdef HOST_WORDS_BIGENDIAN
12535c24501SSingh, Brijesh             uint16_t trigger_mode:1;
12635c24501SSingh, Brijesh             uint16_t level:1;
12735c24501SSingh, Brijesh             uint16_t __resved:3;
12835c24501SSingh, Brijesh             uint16_t delivery_mode:3;
12935c24501SSingh, Brijesh             uint16_t vector:8;
13035c24501SSingh, Brijesh #else
13135c24501SSingh, Brijesh             uint16_t vector:8;
13235c24501SSingh, Brijesh             uint16_t delivery_mode:3;
13335c24501SSingh, Brijesh             uint16_t __resved:3;
13435c24501SSingh, Brijesh             uint16_t level:1;
13535c24501SSingh, Brijesh             uint16_t trigger_mode:1;
13635c24501SSingh, Brijesh #endif
13735c24501SSingh, Brijesh             uint16_t __resved1;
13835c24501SSingh, Brijesh         } QEMU_PACKED;
13935c24501SSingh, Brijesh         uint32_t msi_data;
14035c24501SSingh, Brijesh     };
14135c24501SSingh, Brijesh };
14235c24501SSingh, Brijesh 
1431cf5fd57SPeter Xu /**
1441cf5fd57SPeter Xu  * x86_iommu_get_default - get default IOMMU device
1451cf5fd57SPeter Xu  * @return: pointer to default IOMMU device
1461cf5fd57SPeter Xu  */
1471cf5fd57SPeter Xu X86IOMMUState *x86_iommu_get_default(void);
1481cf5fd57SPeter Xu 
149fb9f5926SDavid Kiarie /*
150fb9f5926SDavid Kiarie  * x86_iommu_get_type - get IOMMU type
151fb9f5926SDavid Kiarie  */
152fb9f5926SDavid Kiarie IommuType x86_iommu_get_type(void);
153fb9f5926SDavid Kiarie 
15402a2cbc8SPeter Xu /**
15502a2cbc8SPeter Xu  * x86_iommu_iec_register_notifier - register IEC (Interrupt Entry
15602a2cbc8SPeter Xu  *                                   Cache) notifiers
15702a2cbc8SPeter Xu  * @iommu: IOMMU device to register
15802a2cbc8SPeter Xu  * @fn: IEC notifier hook function
15902a2cbc8SPeter Xu  * @data: notifier private data
16002a2cbc8SPeter Xu  */
16102a2cbc8SPeter Xu void x86_iommu_iec_register_notifier(X86IOMMUState *iommu,
16202a2cbc8SPeter Xu                                      iec_notify_fn fn, void *data);
16302a2cbc8SPeter Xu 
16402a2cbc8SPeter Xu /**
16502a2cbc8SPeter Xu  * x86_iommu_iec_notify_all - Notify IEC invalidations
16602a2cbc8SPeter Xu  * @iommu: IOMMU device that sends the notification
16702a2cbc8SPeter Xu  * @global: whether this is a global invalidation. If true, @index
16802a2cbc8SPeter Xu  *          and @mask are undefined.
16902a2cbc8SPeter Xu  * @index: starting index of interrupt entry to invalidate
17002a2cbc8SPeter Xu  * @mask: index mask for the invalidation
17102a2cbc8SPeter Xu  */
17202a2cbc8SPeter Xu void x86_iommu_iec_notify_all(X86IOMMUState *iommu, bool global,
17302a2cbc8SPeter Xu                               uint32_t index, uint32_t mask);
17402a2cbc8SPeter Xu 
17535c24501SSingh, Brijesh /**
17635c24501SSingh, Brijesh  * x86_iommu_irq_to_msi_message - Populate one MSIMessage from X86IOMMUIrq
17735c24501SSingh, Brijesh  * @X86IOMMUIrq: The IRQ information
17835c24501SSingh, Brijesh  * @out: Output MSI message
17935c24501SSingh, Brijesh  */
18035c24501SSingh, Brijesh void x86_iommu_irq_to_msi_message(X86IOMMUIrq *irq, MSIMessage *out);
1811c7955c4SPeter Xu #endif
182