1 /* 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 20 #ifndef IOMMU_COMMON_H 21 #define IOMMU_COMMON_H 22 23 #include "hw/sysbus.h" 24 #include "hw/pci/pci.h" 25 #include "hw/pci/msi.h" 26 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 35 #define X86_IOMMU_SID_INVALID (0xffff) 36 37 typedef struct X86IOMMUState X86IOMMUState; 38 typedef struct X86IOMMUClass X86IOMMUClass; 39 typedef struct X86IOMMUIrq X86IOMMUIrq; 40 typedef struct X86IOMMU_MSIMessage X86IOMMU_MSIMessage; 41 42 typedef enum IommuType { 43 TYPE_INTEL, 44 TYPE_AMD, 45 TYPE_NONE 46 } IommuType; 47 48 struct X86IOMMUClass { 49 SysBusDeviceClass parent; 50 /* Intel/AMD specific realize() hook */ 51 DeviceRealize realize; 52 /* MSI-based interrupt remapping */ 53 int (*int_remap)(X86IOMMUState *iommu, MSIMessage *src, 54 MSIMessage *dst, uint16_t sid); 55 }; 56 57 /** 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 */ 65 typedef void (*iec_notify_fn)(void *private, bool global, 66 uint32_t index, uint32_t mask); 67 68 struct IEC_Notifier { 69 iec_notify_fn iec_notify; 70 void *private; 71 QLIST_ENTRY(IEC_Notifier) list; 72 }; 73 typedef struct IEC_Notifier IEC_Notifier; 74 75 struct X86IOMMUState { 76 SysBusDevice busdev; 77 bool intr_supported; /* Whether vIOMMU supports IR */ 78 bool dt_supported; /* Whether vIOMMU supports DT */ 79 bool pt_supported; /* Whether vIOMMU supports pass-through */ 80 IommuType type; /* IOMMU type - AMD/Intel */ 81 QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */ 82 }; 83 84 /* Generic IRQ entry information when interrupt remapping is enabled */ 85 struct X86IOMMUIrq { 86 /* Used by both IOAPIC/MSI interrupt remapping */ 87 uint8_t trigger_mode; 88 uint8_t vector; 89 uint8_t delivery_mode; 90 uint32_t dest; 91 uint8_t dest_mode; 92 93 /* only used by MSI interrupt remapping */ 94 uint8_t redir_hint; 95 uint8_t msi_addr_last_bits; 96 }; 97 98 struct X86IOMMU_MSIMessage { 99 union { 100 struct { 101 #ifdef HOST_WORDS_BIGENDIAN 102 uint32_t __addr_head:12; /* 0xfee */ 103 uint32_t dest:8; 104 uint32_t __reserved:8; 105 uint32_t redir_hint:1; 106 uint32_t dest_mode:1; 107 uint32_t __not_used:2; 108 #else 109 uint32_t __not_used:2; 110 uint32_t dest_mode:1; 111 uint32_t redir_hint:1; 112 uint32_t __reserved:8; 113 uint32_t dest:8; 114 uint32_t __addr_head:12; /* 0xfee */ 115 #endif 116 uint32_t __addr_hi; 117 } QEMU_PACKED; 118 uint64_t msi_addr; 119 }; 120 union { 121 struct { 122 #ifdef HOST_WORDS_BIGENDIAN 123 uint16_t trigger_mode:1; 124 uint16_t level:1; 125 uint16_t __resved:3; 126 uint16_t delivery_mode:3; 127 uint16_t vector:8; 128 #else 129 uint16_t vector:8; 130 uint16_t delivery_mode:3; 131 uint16_t __resved:3; 132 uint16_t level:1; 133 uint16_t trigger_mode:1; 134 #endif 135 uint16_t __resved1; 136 } QEMU_PACKED; 137 uint32_t msi_data; 138 }; 139 }; 140 141 /** 142 * x86_iommu_get_default - get default IOMMU device 143 * @return: pointer to default IOMMU device 144 */ 145 X86IOMMUState *x86_iommu_get_default(void); 146 147 /* 148 * x86_iommu_get_type - get IOMMU type 149 */ 150 IommuType x86_iommu_get_type(void); 151 152 /** 153 * x86_iommu_iec_register_notifier - register IEC (Interrupt Entry 154 * Cache) notifiers 155 * @iommu: IOMMU device to register 156 * @fn: IEC notifier hook function 157 * @data: notifier private data 158 */ 159 void x86_iommu_iec_register_notifier(X86IOMMUState *iommu, 160 iec_notify_fn fn, void *data); 161 162 /** 163 * x86_iommu_iec_notify_all - Notify IEC invalidations 164 * @iommu: IOMMU device that sends the notification 165 * @global: whether this is a global invalidation. If true, @index 166 * and @mask are undefined. 167 * @index: starting index of interrupt entry to invalidate 168 * @mask: index mask for the invalidation 169 */ 170 void x86_iommu_iec_notify_all(X86IOMMUState *iommu, bool global, 171 uint32_t index, uint32_t mask); 172 173 /** 174 * x86_iommu_irq_to_msi_message - Populate one MSIMessage from X86IOMMUIrq 175 * @X86IOMMUIrq: The IRQ information 176 * @out: Output MSI message 177 */ 178 void x86_iommu_irq_to_msi_message(X86IOMMUIrq *irq, MSIMessage *out); 179 #endif 180