blob: 8e10383b11b157a234fb91f5271ff2126282ac64 [file] [log] [blame]
Peter Xu1c7955c2016-07-14 13:56:10 +08001/*
2 * Common IOMMU interface for X86 platform
3 *
4 * Copyright (C) 2016 Peter Xu, Red Hat <peterx@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Markus Armbruster58ea30f2019-03-15 15:51:20 +010020#ifndef HW_I386_X86_IOMMU_H
21#define HW_I386_X86_IOMMU_H
Peter Xu1c7955c2016-07-14 13:56:10 +080022
23#include "hw/sysbus.h"
Peter Xu8b5ed7d2016-07-14 13:56:25 +080024#include "hw/pci/pci.h"
Singh, Brijesh35c24502018-10-01 19:44:29 +000025#include "hw/pci/msi.h"
Peter Xu1c7955c2016-07-14 13:56:10 +080026
27#define TYPE_X86_IOMMU_DEVICE ("x86-iommu")
28#define X86_IOMMU_DEVICE(obj) \
29 OBJECT_CHECK(X86IOMMUState, (obj), TYPE_X86_IOMMU_DEVICE)
30#define X86_IOMMU_CLASS(klass) \
31 OBJECT_CLASS_CHECK(X86IOMMUClass, (klass), TYPE_X86_IOMMU_DEVICE)
32#define X86_IOMMU_GET_CLASS(obj) \
33 OBJECT_GET_CLASS(X86IOMMUClass, obj, TYPE_X86_IOMMU_DEVICE)
34
Peter Xu8b5ed7d2016-07-14 13:56:25 +080035#define X86_IOMMU_SID_INVALID (0xffff)
Peter Xu04af0e12016-07-14 13:56:11 +080036
Peter Xu1c7955c2016-07-14 13:56:10 +080037typedef struct X86IOMMUState X86IOMMUState;
38typedef struct X86IOMMUClass X86IOMMUClass;
Singh, Brijesh35c24502018-10-01 19:44:29 +000039typedef struct X86IOMMUIrq X86IOMMUIrq;
40typedef struct X86IOMMU_MSIMessage X86IOMMU_MSIMessage;
Peter Xu1c7955c2016-07-14 13:56:10 +080041
David Kiariefb9f5922016-09-20 18:42:34 +030042typedef enum IommuType {
43 TYPE_INTEL,
44 TYPE_AMD,
45 TYPE_NONE
46} IommuType;
47
Peter Xu1c7955c2016-07-14 13:56:10 +080048struct X86IOMMUClass {
49 SysBusDeviceClass parent;
50 /* Intel/AMD specific realize() hook */
51 DeviceRealize realize;
Peter Xu8b5ed7d2016-07-14 13:56:25 +080052 /* MSI-based interrupt remapping */
53 int (*int_remap)(X86IOMMUState *iommu, MSIMessage *src,
54 MSIMessage *dst, uint16_t sid);
Peter Xu1c7955c2016-07-14 13:56:10 +080055};
56
Peter Xu02a2cbc2016-07-14 13:56:26 +080057/**
58 * iec_notify_fn - IEC (Interrupt Entry Cache) notifier hook,
59 * triggered when IR invalidation happens.
60 * @private: private data
61 * @global: whether this is a global IEC invalidation
62 * @index: IRTE index to invalidate (start from)
63 * @mask: invalidation mask
64 */
65typedef void (*iec_notify_fn)(void *private, bool global,
66 uint32_t index, uint32_t mask);
67
68struct IEC_Notifier {
69 iec_notify_fn iec_notify;
70 void *private;
71 QLIST_ENTRY(IEC_Notifier) list;
72};
73typedef struct IEC_Notifier IEC_Notifier;
74
Peter Xu1c7955c2016-07-14 13:56:10 +080075struct X86IOMMUState {
76 SysBusDevice busdev;
Peter Xua924b3d2018-12-20 13:40:36 +080077 OnOffAuto intr_supported; /* Whether vIOMMU supports IR */
Jason Wang554f5e12016-12-30 18:09:14 +080078 bool dt_supported; /* Whether vIOMMU supports DT */
Peter Xudbaabb22017-05-19 11:19:47 +080079 bool pt_supported; /* Whether vIOMMU supports pass-through */
David Kiariefb9f5922016-09-20 18:42:34 +030080 IommuType type; /* IOMMU type - AMD/Intel */
Peter Xu02a2cbc2016-07-14 13:56:26 +080081 QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */
Peter Xu1c7955c2016-07-14 13:56:10 +080082};
83
Peter Xua924b3d2018-12-20 13:40:36 +080084bool x86_iommu_ir_supported(X86IOMMUState *s);
85
Singh, Brijesh35c24502018-10-01 19:44:29 +000086/* Generic IRQ entry information when interrupt remapping is enabled */
87struct X86IOMMUIrq {
88 /* Used by both IOAPIC/MSI interrupt remapping */
89 uint8_t trigger_mode;
90 uint8_t vector;
91 uint8_t delivery_mode;
92 uint32_t dest;
93 uint8_t dest_mode;
94
95 /* only used by MSI interrupt remapping */
96 uint8_t redir_hint;
97 uint8_t msi_addr_last_bits;
98};
99
100struct X86IOMMU_MSIMessage {
101 union {
102 struct {
103#ifdef HOST_WORDS_BIGENDIAN
104 uint32_t __addr_head:12; /* 0xfee */
105 uint32_t dest:8;
106 uint32_t __reserved:8;
107 uint32_t redir_hint:1;
108 uint32_t dest_mode:1;
109 uint32_t __not_used:2;
110#else
111 uint32_t __not_used:2;
112 uint32_t dest_mode:1;
113 uint32_t redir_hint:1;
114 uint32_t __reserved:8;
115 uint32_t dest:8;
116 uint32_t __addr_head:12; /* 0xfee */
117#endif
118 uint32_t __addr_hi;
119 } QEMU_PACKED;
120 uint64_t msi_addr;
121 };
122 union {
123 struct {
124#ifdef HOST_WORDS_BIGENDIAN
125 uint16_t trigger_mode:1;
126 uint16_t level:1;
127 uint16_t __resved:3;
128 uint16_t delivery_mode:3;
129 uint16_t vector:8;
130#else
131 uint16_t vector:8;
132 uint16_t delivery_mode:3;
133 uint16_t __resved:3;
134 uint16_t level:1;
135 uint16_t trigger_mode:1;
136#endif
137 uint16_t __resved1;
138 } QEMU_PACKED;
139 uint32_t msi_data;
140 };
141};
142
Peter Xu1cf5fd52016-07-14 13:56:12 +0800143/**
144 * x86_iommu_get_default - get default IOMMU device
145 * @return: pointer to default IOMMU device
146 */
147X86IOMMUState *x86_iommu_get_default(void);
148
David Kiariefb9f5922016-09-20 18:42:34 +0300149/*
150 * x86_iommu_get_type - get IOMMU type
151 */
152IommuType x86_iommu_get_type(void);
153
Peter Xu02a2cbc2016-07-14 13:56:26 +0800154/**
155 * x86_iommu_iec_register_notifier - register IEC (Interrupt Entry
156 * Cache) notifiers
157 * @iommu: IOMMU device to register
158 * @fn: IEC notifier hook function
159 * @data: notifier private data
160 */
161void x86_iommu_iec_register_notifier(X86IOMMUState *iommu,
162 iec_notify_fn fn, void *data);
163
164/**
165 * x86_iommu_iec_notify_all - Notify IEC invalidations
166 * @iommu: IOMMU device that sends the notification
167 * @global: whether this is a global invalidation. If true, @index
168 * and @mask are undefined.
169 * @index: starting index of interrupt entry to invalidate
170 * @mask: index mask for the invalidation
171 */
172void x86_iommu_iec_notify_all(X86IOMMUState *iommu, bool global,
173 uint32_t index, uint32_t mask);
174
Singh, Brijesh35c24502018-10-01 19:44:29 +0000175/**
176 * x86_iommu_irq_to_msi_message - Populate one MSIMessage from X86IOMMUIrq
177 * @X86IOMMUIrq: The IRQ information
178 * @out: Output MSI message
179 */
180void x86_iommu_irq_to_msi_message(X86IOMMUIrq *irq, MSIMessage *out);
Peter Xu1c7955c2016-07-14 13:56:10 +0800181#endif