Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * APIC support - internal interfaces |
| 3 | * |
| 4 | * Copyright (c) 2004-2005 Fabrice Bellard |
| 5 | * Copyright (c) 2011 Jan Kiszka, Siemens AG |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, see <http://www.gnu.org/licenses/> |
| 19 | */ |
Markus Armbruster | 175de52 | 2016-06-29 15:29:06 +0200 | [diff] [blame] | 20 | |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 21 | #ifndef QEMU_APIC_INTERNAL_H |
| 22 | #define QEMU_APIC_INTERNAL_H |
| 23 | |
Peter Maydell | 20fbcfd | 2015-08-19 16:20:20 +0100 | [diff] [blame] | 24 | #include "cpu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 25 | #include "exec/memory.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 26 | #include "qemu/timer.h" |
Markus Armbruster | ec150c7 | 2019-08-12 07:23:31 +0200 | [diff] [blame] | 27 | #include "target/i386/cpu-qom.h" |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 28 | |
| 29 | /* APIC Local Vector Table */ |
| 30 | #define APIC_LVT_TIMER 0 |
| 31 | #define APIC_LVT_THERMAL 1 |
| 32 | #define APIC_LVT_PERFORM 2 |
| 33 | #define APIC_LVT_LINT0 3 |
| 34 | #define APIC_LVT_LINT1 4 |
| 35 | #define APIC_LVT_ERROR 5 |
| 36 | #define APIC_LVT_NB 6 |
| 37 | |
| 38 | /* APIC delivery modes */ |
| 39 | #define APIC_DM_FIXED 0 |
| 40 | #define APIC_DM_LOWPRI 1 |
| 41 | #define APIC_DM_SMI 2 |
| 42 | #define APIC_DM_NMI 4 |
| 43 | #define APIC_DM_INIT 5 |
| 44 | #define APIC_DM_SIPI 6 |
| 45 | #define APIC_DM_EXTINT 7 |
| 46 | |
| 47 | /* APIC destination mode */ |
| 48 | #define APIC_DESTMODE_FLAT 0xf |
| 49 | #define APIC_DESTMODE_CLUSTER 1 |
| 50 | |
| 51 | #define APIC_TRIGGER_EDGE 0 |
| 52 | #define APIC_TRIGGER_LEVEL 1 |
| 53 | |
Pavel Butsykin | 6519d18 | 2015-09-22 16:18:15 +0300 | [diff] [blame] | 54 | #define APIC_VECTOR_MASK 0xff |
| 55 | #define APIC_DCR_MASK 0xf |
| 56 | |
| 57 | #define APIC_LVT_TIMER_SHIFT 17 |
| 58 | #define APIC_LVT_MASKED_SHIFT 16 |
| 59 | #define APIC_LVT_LEVEL_TRIGGER_SHIFT 15 |
| 60 | #define APIC_LVT_REMOTE_IRR_SHIFT 14 |
| 61 | #define APIC_LVT_INT_POLARITY_SHIFT 13 |
| 62 | #define APIC_LVT_DELIV_STS_SHIFT 12 |
| 63 | #define APIC_LVT_DELIV_MOD_SHIFT 8 |
| 64 | |
| 65 | #define APIC_LVT_TIMER_TSCDEADLINE (2 << APIC_LVT_TIMER_SHIFT) |
Pavel Butsykin | b6cfc3c | 2015-09-22 16:18:16 +0300 | [diff] [blame] | 66 | #define APIC_LVT_TIMER_PERIODIC (1 << APIC_LVT_TIMER_SHIFT) |
| 67 | #define APIC_LVT_MASKED (1 << APIC_LVT_MASKED_SHIFT) |
| 68 | #define APIC_LVT_LEVEL_TRIGGER (1 << APIC_LVT_LEVEL_TRIGGER_SHIFT) |
| 69 | #define APIC_LVT_REMOTE_IRR (1 << APIC_LVT_REMOTE_IRR_SHIFT) |
Pavel Butsykin | 6519d18 | 2015-09-22 16:18:15 +0300 | [diff] [blame] | 70 | #define APIC_LVT_INT_POLARITY (1 << APIC_LVT_INT_POLARITY_SHIFT) |
| 71 | #define APIC_LVT_DELIV_STS (1 << APIC_LVT_DELIV_STS_SHIFT) |
| 72 | #define APIC_LVT_DELIV_MOD (7 << APIC_LVT_DELIV_MOD_SHIFT) |
| 73 | |
| 74 | #define APIC_ESR_ILL_ADDRESS_SHIFT 7 |
| 75 | #define APIC_ESR_RECV_ILL_VECT_SHIFT 6 |
| 76 | #define APIC_ESR_SEND_ILL_VECT_SHIFT 5 |
| 77 | #define APIC_ESR_RECV_ACCEPT_SHIFT 3 |
| 78 | #define APIC_ESR_SEND_ACCEPT_SHIFT 2 |
| 79 | #define APIC_ESR_RECV_CHECK_SUM_SHIFT 1 |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 80 | |
Pavel Butsykin | b6cfc3c | 2015-09-22 16:18:16 +0300 | [diff] [blame] | 81 | #define APIC_ESR_ILLEGAL_ADDRESS (1 << APIC_ESR_ILL_ADDRESS_SHIFT) |
Pavel Butsykin | 6519d18 | 2015-09-22 16:18:15 +0300 | [diff] [blame] | 82 | #define APIC_ESR_RECV_ILLEGAL_VECT (1 << APIC_ESR_RECV_ILL_VECT_SHIFT) |
| 83 | #define APIC_ESR_SEND_ILLEGAL_VECT (1 << APIC_ESR_SEND_ILL_VECT_SHIFT) |
| 84 | #define APIC_ESR_RECV_ACCEPT (1 << APIC_ESR_RECV_ACCEPT_SHIFT) |
| 85 | #define APIC_ESR_SEND_ACCEPT (1 << APIC_ESR_SEND_ACCEPT_SHIFT) |
| 86 | #define APIC_ESR_RECV_CHECK_SUM (1 << APIC_ESR_RECV_CHECK_SUM_SHIFT) |
| 87 | #define APIC_ESR_SEND_CHECK_SUM 1 |
| 88 | |
| 89 | #define APIC_ICR_DEST_SHIFT 24 |
| 90 | #define APIC_ICR_DEST_SHORT_SHIFT 18 |
| 91 | #define APIC_ICR_TRIGGER_MOD_SHIFT 15 |
| 92 | #define APIC_ICR_LEVEL_SHIFT 14 |
| 93 | #define APIC_ICR_DELIV_STS_SHIFT 12 |
| 94 | #define APIC_ICR_DEST_MOD_SHIFT 11 |
| 95 | #define APIC_ICR_DELIV_MOD_SHIFT 8 |
| 96 | |
| 97 | #define APIC_ICR_DEST_SHORT (3 << APIC_ICR_DEST_SHORT_SHIFT) |
| 98 | #define APIC_ICR_TRIGGER_MOD (1 << APIC_ICR_TRIGGER_MOD_SHIFT) |
| 99 | #define APIC_ICR_LEVEL (1 << APIC_ICR_LEVEL_SHIFT) |
| 100 | #define APIC_ICR_DELIV_STS (1 << APIC_ICR_DELIV_STS_SHIFT) |
| 101 | #define APIC_ICR_DEST_MOD (1 << APIC_ICR_DEST_MOD_SHIFT) |
| 102 | #define APIC_ICR_DELIV_MOD (7 << APIC_ICR_DELIV_MOD_SHIFT) |
| 103 | |
| 104 | #define APIC_PR_CLASS_SHIFT 4 |
| 105 | #define APIC_PR_SUB_CLASS 0xf |
| 106 | |
| 107 | #define APIC_LOGDEST_XAPIC_SHIFT 4 |
| 108 | #define APIC_LOGDEST_XAPIC_ID 0xf |
| 109 | |
| 110 | #define APIC_LOGDEST_X2APIC_SHIFT 16 |
| 111 | #define APIC_LOGDEST_X2APIC_ID 0xffff |
| 112 | |
| 113 | #define APIC_SPURIO_FOCUS_SHIFT 9 |
| 114 | #define APIC_SPURIO_ENABLED_SHIFT 8 |
| 115 | |
| 116 | #define APIC_SPURIO_FOCUS (1 << APIC_SPURIO_FOCUS_SHIFT) |
| 117 | #define APIC_SPURIO_ENABLED (1 << APIC_SPURIO_ENABLED_SHIFT) |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 118 | |
Pavel Butsykin | b6cfc3c | 2015-09-22 16:18:16 +0300 | [diff] [blame] | 119 | #define APIC_SV_DIRECTED_IO (1 << 12) |
| 120 | #define APIC_SV_ENABLE (1 << 8) |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 121 | |
Jan Kiszka | e5ad936 | 2012-02-17 18:31:19 +0100 | [diff] [blame] | 122 | #define VAPIC_ENABLE_BIT 0 |
| 123 | #define VAPIC_ENABLE_MASK (1 << VAPIC_ENABLE_BIT) |
| 124 | |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 125 | typedef struct APICCommonState APICCommonState; |
| 126 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 127 | #define TYPE_APIC_COMMON "apic-common" |
| 128 | #define APIC_COMMON(obj) \ |
| 129 | OBJECT_CHECK(APICCommonState, (obj), TYPE_APIC_COMMON) |
| 130 | #define APIC_COMMON_CLASS(klass) \ |
| 131 | OBJECT_CLASS_CHECK(APICCommonClass, (klass), TYPE_APIC_COMMON) |
| 132 | #define APIC_COMMON_GET_CLASS(obj) \ |
| 133 | OBJECT_GET_CLASS(APICCommonClass, (obj), TYPE_APIC_COMMON) |
| 134 | |
| 135 | typedef struct APICCommonClass |
| 136 | { |
Chen Fan | 46232aa | 2015-09-16 17:19:14 +0800 | [diff] [blame] | 137 | DeviceClass parent_class; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 138 | |
xiaoqiang zhao | ff6986c | 2013-11-05 18:16:03 +0800 | [diff] [blame] | 139 | DeviceRealize realize; |
Igor Mammedov | 9c156f9 | 2016-06-23 17:30:14 +0200 | [diff] [blame] | 140 | DeviceUnrealize unrealize; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 141 | void (*set_base)(APICCommonState *s, uint64_t val); |
| 142 | void (*set_tpr)(APICCommonState *s, uint8_t val); |
Jan Kiszka | e5ad936 | 2012-02-17 18:31:19 +0100 | [diff] [blame] | 143 | uint8_t (*get_tpr)(APICCommonState *s); |
| 144 | void (*enable_tpr_reporting)(APICCommonState *s, bool enable); |
| 145 | void (*vapic_base_update)(APICCommonState *s); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 146 | void (*external_nmi)(APICCommonState *s); |
Jan Kiszka | e5ad936 | 2012-02-17 18:31:19 +0100 | [diff] [blame] | 147 | void (*pre_save)(APICCommonState *s); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 148 | void (*post_load)(APICCommonState *s); |
Paolo Bonzini | 575a6f4 | 2014-12-10 16:56:46 +0100 | [diff] [blame] | 149 | void (*reset)(APICCommonState *s); |
Radim Krčmář | 267ee35 | 2016-10-10 17:28:43 +0200 | [diff] [blame] | 150 | /* send_msi emulates an APIC bus and its proper place would be in a new |
| 151 | * device, but it's convenient to have it here for now. |
| 152 | */ |
| 153 | void (*send_msi)(MSIMessage *msi); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 154 | } APICCommonClass; |
| 155 | |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 156 | struct APICCommonState { |
Chen Fan | 46232aa | 2015-09-16 17:19:14 +0800 | [diff] [blame] | 157 | /*< private >*/ |
| 158 | DeviceState parent_obj; |
| 159 | /*< public >*/ |
Andreas Färber | 60671e5 | 2012-10-10 14:10:07 +0200 | [diff] [blame] | 160 | |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 161 | MemoryRegion io_memory; |
Andreas Färber | 60671e5 | 2012-10-10 14:10:07 +0200 | [diff] [blame] | 162 | X86CPU *cpu; |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 163 | uint32_t apicbase; |
Igor Mammedov | 33d7a28 | 2016-10-19 14:05:35 +0200 | [diff] [blame] | 164 | uint8_t id; /* legacy APIC ID */ |
| 165 | uint32_t initial_apic_id; |
Gabriel L. Somlo | aa93200 | 2014-05-05 10:52:51 -0400 | [diff] [blame] | 166 | uint8_t version; |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 167 | uint8_t arb_id; |
| 168 | uint8_t tpr; |
| 169 | uint32_t spurious_vec; |
| 170 | uint8_t log_dest; |
| 171 | uint8_t dest_mode; |
| 172 | uint32_t isr[8]; /* in service register */ |
| 173 | uint32_t tmr[8]; /* trigger mode register */ |
| 174 | uint32_t irr[8]; /* interrupt request register */ |
| 175 | uint32_t lvt[APIC_LVT_NB]; |
| 176 | uint32_t esr; /* error register */ |
| 177 | uint32_t icr[2]; |
| 178 | |
| 179 | uint32_t divide_conf; |
| 180 | int count_shift; |
| 181 | uint32_t initial_count; |
| 182 | int64_t initial_count_load_time; |
| 183 | int64_t next_time; |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 184 | QEMUTimer *timer; |
Jan Kiszka | 7a380ca | 2011-10-16 12:19:12 +0200 | [diff] [blame] | 185 | int64_t timer_expiry; |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 186 | int sipi_vector; |
| 187 | int wait_for_sipi; |
Jan Kiszka | e5ad936 | 2012-02-17 18:31:19 +0100 | [diff] [blame] | 188 | |
| 189 | uint32_t vapic_control; |
| 190 | DeviceState *vapic; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 191 | hwaddr vapic_paddr; /* note: persistence via kvmvapic */ |
Igor Mammedov | f6e9844 | 2016-07-01 17:53:56 +0200 | [diff] [blame] | 192 | bool legacy_instance_id; |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 193 | }; |
| 194 | |
Jan Kiszka | e5ad936 | 2012-02-17 18:31:19 +0100 | [diff] [blame] | 195 | typedef struct VAPICState { |
| 196 | uint8_t tpr; |
| 197 | uint8_t isr; |
| 198 | uint8_t zero; |
| 199 | uint8_t irr; |
| 200 | uint8_t enabled; |
| 201 | } QEMU_PACKED VAPICState; |
| 202 | |
| 203 | extern bool apic_report_tpr_access; |
| 204 | |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 205 | void apic_report_irq_delivered(int delivered); |
Jan Kiszka | 7a380ca | 2011-10-16 12:19:12 +0200 | [diff] [blame] | 206 | bool apic_next_timer(APICCommonState *s, int64_t current_time); |
Jan Kiszka | e5ad936 | 2012-02-17 18:31:19 +0100 | [diff] [blame] | 207 | void apic_enable_tpr_access_reporting(DeviceState *d, bool enable); |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 208 | void apic_enable_vapic(DeviceState *d, hwaddr paddr); |
Jan Kiszka | e5ad936 | 2012-02-17 18:31:19 +0100 | [diff] [blame] | 209 | |
Andreas Färber | d77953b | 2013-01-16 19:29:31 +0100 | [diff] [blame] | 210 | void vapic_report_tpr_access(DeviceState *dev, CPUState *cpu, target_ulong ip, |
Jan Kiszka | e5ad936 | 2012-02-17 18:31:19 +0100 | [diff] [blame] | 211 | TPRAccess access); |
Jan Kiszka | dae0168 | 2011-10-16 11:16:36 +0200 | [diff] [blame] | 212 | |
Pavel Butsykin | 82a5e04 | 2015-09-22 16:18:13 +0300 | [diff] [blame] | 213 | int apic_get_ppr(APICCommonState *s); |
Jan Kiszka | 6e083c0 | 2020-02-07 07:43:42 +0100 | [diff] [blame] | 214 | uint32_t apic_get_current_count(APICCommonState *s); |
Pavel Butsykin | 82a5e04 | 2015-09-22 16:18:13 +0300 | [diff] [blame] | 215 | |
| 216 | static inline void apic_set_bit(uint32_t *tab, int index) |
| 217 | { |
| 218 | int i, mask; |
| 219 | i = index >> 5; |
| 220 | mask = 1 << (index & 0x1f); |
| 221 | tab[i] |= mask; |
| 222 | } |
| 223 | |
| 224 | static inline int apic_get_bit(uint32_t *tab, int index) |
| 225 | { |
| 226 | int i, mask; |
| 227 | i = index >> 5; |
| 228 | mask = 1 << (index & 0x1f); |
| 229 | return !!(tab[i] & mask); |
| 230 | } |
| 231 | |
Radim Krčmář | 2f11431 | 2016-10-10 17:28:42 +0200 | [diff] [blame] | 232 | APICCommonClass *apic_get_class(void); |
| 233 | |
Markus Armbruster | 175de52 | 2016-06-29 15:29:06 +0200 | [diff] [blame] | 234 | #endif /* QEMU_APIC_INTERNAL_H */ |