Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 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 | |
Markus Armbruster | 58ea30f | 2019-03-15 15:51:20 +0100 | [diff] [blame] | 20 | #ifndef HW_I386_X86_IOMMU_H |
| 21 | #define HW_I386_X86_IOMMU_H |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 22 | |
| 23 | #include "hw/sysbus.h" |
Peter Xu | 8b5ed7d | 2016-07-14 13:56:25 +0800 | [diff] [blame] | 24 | #include "hw/pci/pci.h" |
Singh, Brijesh | 35c2450 | 2018-10-01 19:44:29 +0000 | [diff] [blame] | 25 | #include "hw/pci/msi.h" |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 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 | |
Peter Xu | 8b5ed7d | 2016-07-14 13:56:25 +0800 | [diff] [blame] | 35 | #define X86_IOMMU_SID_INVALID (0xffff) |
Peter Xu | 04af0e1 | 2016-07-14 13:56:11 +0800 | [diff] [blame] | 36 | |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 37 | typedef struct X86IOMMUState X86IOMMUState; |
| 38 | typedef struct X86IOMMUClass X86IOMMUClass; |
Singh, Brijesh | 35c2450 | 2018-10-01 19:44:29 +0000 | [diff] [blame] | 39 | typedef struct X86IOMMUIrq X86IOMMUIrq; |
| 40 | typedef struct X86IOMMU_MSIMessage X86IOMMU_MSIMessage; |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 41 | |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 42 | typedef enum IommuType { |
| 43 | TYPE_INTEL, |
| 44 | TYPE_AMD, |
| 45 | TYPE_NONE |
| 46 | } IommuType; |
| 47 | |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 48 | struct X86IOMMUClass { |
| 49 | SysBusDeviceClass parent; |
| 50 | /* Intel/AMD specific realize() hook */ |
| 51 | DeviceRealize realize; |
Peter Xu | 8b5ed7d | 2016-07-14 13:56:25 +0800 | [diff] [blame] | 52 | /* MSI-based interrupt remapping */ |
| 53 | int (*int_remap)(X86IOMMUState *iommu, MSIMessage *src, |
| 54 | MSIMessage *dst, uint16_t sid); |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 55 | }; |
| 56 | |
Peter Xu | 02a2cbc | 2016-07-14 13:56:26 +0800 | [diff] [blame] | 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 | |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 75 | struct X86IOMMUState { |
| 76 | SysBusDevice busdev; |
Peter Xu | a924b3d | 2018-12-20 13:40:36 +0800 | [diff] [blame] | 77 | OnOffAuto intr_supported; /* Whether vIOMMU supports IR */ |
Jason Wang | 554f5e1 | 2016-12-30 18:09:14 +0800 | [diff] [blame] | 78 | bool dt_supported; /* Whether vIOMMU supports DT */ |
Peter Xu | dbaabb2 | 2017-05-19 11:19:47 +0800 | [diff] [blame] | 79 | bool pt_supported; /* Whether vIOMMU supports pass-through */ |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 80 | IommuType type; /* IOMMU type - AMD/Intel */ |
Peter Xu | 02a2cbc | 2016-07-14 13:56:26 +0800 | [diff] [blame] | 81 | QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */ |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 82 | }; |
| 83 | |
Peter Xu | a924b3d | 2018-12-20 13:40:36 +0800 | [diff] [blame] | 84 | bool x86_iommu_ir_supported(X86IOMMUState *s); |
| 85 | |
Singh, Brijesh | 35c2450 | 2018-10-01 19:44:29 +0000 | [diff] [blame] | 86 | /* Generic IRQ entry information when interrupt remapping is enabled */ |
| 87 | struct 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 | |
| 100 | struct 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 Xu | 1cf5fd5 | 2016-07-14 13:56:12 +0800 | [diff] [blame] | 143 | /** |
| 144 | * x86_iommu_get_default - get default IOMMU device |
| 145 | * @return: pointer to default IOMMU device |
| 146 | */ |
| 147 | X86IOMMUState *x86_iommu_get_default(void); |
| 148 | |
David Kiarie | fb9f592 | 2016-09-20 18:42:34 +0300 | [diff] [blame] | 149 | /* |
| 150 | * x86_iommu_get_type - get IOMMU type |
| 151 | */ |
| 152 | IommuType x86_iommu_get_type(void); |
| 153 | |
Peter Xu | 02a2cbc | 2016-07-14 13:56:26 +0800 | [diff] [blame] | 154 | /** |
| 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 | */ |
| 161 | void 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 | */ |
| 172 | void x86_iommu_iec_notify_all(X86IOMMUState *iommu, bool global, |
| 173 | uint32_t index, uint32_t mask); |
| 174 | |
Singh, Brijesh | 35c2450 | 2018-10-01 19:44:29 +0000 | [diff] [blame] | 175 | /** |
| 176 | * x86_iommu_irq_to_msi_message - Populate one MSIMessage from X86IOMMUIrq |
| 177 | * @X86IOMMUIrq: The IRQ information |
| 178 | * @out: Output MSI message |
| 179 | */ |
| 180 | void x86_iommu_irq_to_msi_message(X86IOMMUIrq *irq, MSIMessage *out); |
Peter Xu | 1c7955c | 2016-07-14 13:56:10 +0800 | [diff] [blame] | 181 | #endif |