blob: 6c26e820e05a463d4de04c1b7688f8c2fee2aa49 [file] [log] [blame]
aliguori610626a2009-03-12 20:25:12 +00001/*
2 * ioapic.c IOAPIC emulation logic
3 *
4 * Copyright (c) 2004-2005 Fabrice Bellard
5 *
6 * Split the ioapic logic from apic.c
7 * Xiantao Zhang <xiantao.zhang@intel.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000020 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aliguori610626a2009-03-12 20:25:12 +000021 */
22
23#include "hw.h"
24#include "pc.h"
Blue Swirlaa28b9b2010-03-21 19:46:26 +000025#include "apic.h"
Jan Kiszka0280b572011-02-03 22:54:11 +010026#include "ioapic.h"
aliguori610626a2009-03-12 20:25:12 +000027#include "qemu-timer.h"
28#include "host-utils.h"
Blue Swirl96051112010-06-19 07:41:43 +000029#include "sysbus.h"
aliguori610626a2009-03-12 20:25:12 +000030
31//#define DEBUG_IOAPIC
32
Blue Swirl9af9b332010-05-31 18:59:45 +000033#ifdef DEBUG_IOAPIC
34#define DPRINTF(fmt, ...) \
35 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
36#else
37#define DPRINTF(fmt, ...)
38#endif
39
Jan Kiszka0280b572011-02-03 22:54:11 +010040#define MAX_IOAPICS 1
41
Jan Kiszka1f5e71a2011-02-03 22:54:14 +010042#define IOAPIC_VERSION 0x11
aliguori610626a2009-03-12 20:25:12 +000043
Jan Kiszka1f5e71a2011-02-03 22:54:14 +010044#define IOAPIC_LVT_DEST_SHIFT 56
45#define IOAPIC_LVT_MASKED_SHIFT 16
46#define IOAPIC_LVT_TRIGGER_MODE_SHIFT 15
47#define IOAPIC_LVT_REMOTE_IRR_SHIFT 14
48#define IOAPIC_LVT_POLARITY_SHIFT 13
49#define IOAPIC_LVT_DELIV_STATUS_SHIFT 12
50#define IOAPIC_LVT_DEST_MODE_SHIFT 11
51#define IOAPIC_LVT_DELIV_MODE_SHIFT 8
52
53#define IOAPIC_LVT_MASKED (1 << IOAPIC_LVT_MASKED_SHIFT)
54#define IOAPIC_LVT_REMOTE_IRR (1 << IOAPIC_LVT_REMOTE_IRR_SHIFT)
55
56#define IOAPIC_TRIGGER_EDGE 0
57#define IOAPIC_TRIGGER_LEVEL 1
aliguori610626a2009-03-12 20:25:12 +000058
59/*io{apic,sapic} delivery mode*/
Jan Kiszka1f5e71a2011-02-03 22:54:14 +010060#define IOAPIC_DM_FIXED 0x0
61#define IOAPIC_DM_LOWEST_PRIORITY 0x1
62#define IOAPIC_DM_PMI 0x2
63#define IOAPIC_DM_NMI 0x4
64#define IOAPIC_DM_INIT 0x5
65#define IOAPIC_DM_SIPI 0x6
66#define IOAPIC_DM_EXTINT 0x7
67#define IOAPIC_DM_MASK 0x7
68
69#define IOAPIC_VECTOR_MASK 0xff
70
71#define IOAPIC_IOREGSEL 0x00
72#define IOAPIC_IOWIN 0x10
73
74#define IOAPIC_REG_ID 0x00
75#define IOAPIC_REG_VER 0x01
76#define IOAPIC_REG_ARB 0x02
77#define IOAPIC_REG_REDTBL_BASE 0x10
78#define IOAPIC_ID 0x00
79
80#define IOAPIC_ID_SHIFT 24
81#define IOAPIC_ID_MASK 0xf
82
83#define IOAPIC_VER_ENTRIES_SHIFT 16
aliguori610626a2009-03-12 20:25:12 +000084
Blue Swirl96051112010-06-19 07:41:43 +000085typedef struct IOAPICState IOAPICState;
86
aliguori610626a2009-03-12 20:25:12 +000087struct IOAPICState {
Blue Swirl96051112010-06-19 07:41:43 +000088 SysBusDevice busdev;
aliguori610626a2009-03-12 20:25:12 +000089 uint8_t id;
90 uint8_t ioregsel;
aliguori610626a2009-03-12 20:25:12 +000091 uint32_t irr;
92 uint64_t ioredtbl[IOAPIC_NUM_PINS];
93};
94
Jan Kiszka0280b572011-02-03 22:54:11 +010095static IOAPICState *ioapics[MAX_IOAPICS];
96
aliguori610626a2009-03-12 20:25:12 +000097static void ioapic_service(IOAPICState *s)
98{
99 uint8_t i;
100 uint8_t trig_mode;
101 uint8_t vector;
102 uint8_t delivery_mode;
103 uint32_t mask;
104 uint64_t entry;
105 uint8_t dest;
106 uint8_t dest_mode;
107 uint8_t polarity;
108
109 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
110 mask = 1 << i;
111 if (s->irr & mask) {
112 entry = s->ioredtbl[i];
113 if (!(entry & IOAPIC_LVT_MASKED)) {
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100114 trig_mode = ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1);
115 dest = entry >> IOAPIC_LVT_DEST_SHIFT;
116 dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
117 delivery_mode =
118 (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) & IOAPIC_DM_MASK;
119 polarity = (entry >> IOAPIC_LVT_POLARITY_SHIFT) & 1;
Jan Kiszka0280b572011-02-03 22:54:11 +0100120 if (trig_mode == IOAPIC_TRIGGER_EDGE) {
aliguori610626a2009-03-12 20:25:12 +0000121 s->irr &= ~mask;
Jan Kiszka0280b572011-02-03 22:54:11 +0100122 } else {
123 s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
124 }
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100125 if (delivery_mode == IOAPIC_DM_EXTINT) {
aliguori610626a2009-03-12 20:25:12 +0000126 vector = pic_read_irq(isa_pic);
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100127 } else {
128 vector = entry & IOAPIC_VECTOR_MASK;
129 }
aliguori610626a2009-03-12 20:25:12 +0000130 apic_deliver_irq(dest, dest_mode, delivery_mode,
131 vector, polarity, trig_mode);
132 }
133 }
134 }
135}
136
Blue Swirl7d0500c2010-06-17 16:32:47 +0000137static void ioapic_set_irq(void *opaque, int vector, int level)
aliguori610626a2009-03-12 20:25:12 +0000138{
139 IOAPICState *s = opaque;
140
141 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
142 * to GSI 2. GSI maps to ioapic 1-1. This is not
143 * the cleanest way of doing it but it should work. */
144
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100145 DPRINTF("%s: %s vec %x\n", __func__, level ? "raise" : "lower", vector);
146 if (vector == 0) {
aliguori610626a2009-03-12 20:25:12 +0000147 vector = 2;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100148 }
aliguori610626a2009-03-12 20:25:12 +0000149 if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
150 uint32_t mask = 1 << vector;
151 uint64_t entry = s->ioredtbl[vector];
152
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100153 if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
154 IOAPIC_TRIGGER_LEVEL) {
aliguori610626a2009-03-12 20:25:12 +0000155 /* level triggered */
156 if (level) {
157 s->irr |= mask;
158 ioapic_service(s);
159 } else {
160 s->irr &= ~mask;
161 }
162 } else {
Jan Kiszka47f7be32011-04-09 13:18:59 +0200163 /* According to the 82093AA manual, we must ignore edge requests
164 * if the input pin is masked. */
165 if (level && !(entry & IOAPIC_LVT_MASKED)) {
aliguori610626a2009-03-12 20:25:12 +0000166 s->irr |= mask;
167 ioapic_service(s);
168 }
169 }
170 }
171}
172
Jan Kiszka0280b572011-02-03 22:54:11 +0100173void ioapic_eoi_broadcast(int vector)
174{
175 IOAPICState *s;
176 uint64_t entry;
177 int i, n;
178
179 for (i = 0; i < MAX_IOAPICS; i++) {
180 s = ioapics[i];
181 if (!s) {
182 continue;
183 }
184 for (n = 0; n < IOAPIC_NUM_PINS; n++) {
185 entry = s->ioredtbl[n];
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100186 if ((entry & IOAPIC_LVT_REMOTE_IRR)
187 && (entry & IOAPIC_VECTOR_MASK) == vector) {
Jan Kiszka0280b572011-02-03 22:54:11 +0100188 s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
189 if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
190 ioapic_service(s);
191 }
192 }
193 }
194 }
195}
196
Anthony Liguoric227f092009-10-01 16:12:16 -0500197static uint32_t ioapic_mem_readl(void *opaque, target_phys_addr_t addr)
aliguori610626a2009-03-12 20:25:12 +0000198{
199 IOAPICState *s = opaque;
200 int index;
201 uint32_t val = 0;
202
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100203 switch (addr & 0xff) {
204 case IOAPIC_IOREGSEL:
aliguori610626a2009-03-12 20:25:12 +0000205 val = s->ioregsel;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100206 break;
207 case IOAPIC_IOWIN:
aliguori610626a2009-03-12 20:25:12 +0000208 switch (s->ioregsel) {
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100209 case IOAPIC_REG_ID:
210 val = s->id << IOAPIC_ID_SHIFT;
211 break;
212 case IOAPIC_REG_VER:
213 val = IOAPIC_VERSION |
214 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
215 break;
216 case IOAPIC_REG_ARB:
217 val = 0;
218 break;
219 default:
220 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
221 if (index >= 0 && index < IOAPIC_NUM_PINS) {
222 if (s->ioregsel & 1) {
223 val = s->ioredtbl[index] >> 32;
224 } else {
225 val = s->ioredtbl[index] & 0xffffffff;
aliguori610626a2009-03-12 20:25:12 +0000226 }
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100227 }
aliguori610626a2009-03-12 20:25:12 +0000228 }
Blue Swirl9af9b332010-05-31 18:59:45 +0000229 DPRINTF("read: %08x = %08x\n", s->ioregsel, val);
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100230 break;
aliguori610626a2009-03-12 20:25:12 +0000231 }
232 return val;
233}
234
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100235static void
236ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
aliguori610626a2009-03-12 20:25:12 +0000237{
238 IOAPICState *s = opaque;
239 int index;
240
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100241 switch (addr & 0xff) {
242 case IOAPIC_IOREGSEL:
aliguori610626a2009-03-12 20:25:12 +0000243 s->ioregsel = val;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100244 break;
245 case IOAPIC_IOWIN:
Blue Swirl9af9b332010-05-31 18:59:45 +0000246 DPRINTF("write: %08x = %08x\n", s->ioregsel, val);
aliguori610626a2009-03-12 20:25:12 +0000247 switch (s->ioregsel) {
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100248 case IOAPIC_REG_ID:
249 s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
250 break;
251 case IOAPIC_REG_VER:
252 case IOAPIC_REG_ARB:
253 break;
254 default:
255 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
256 if (index >= 0 && index < IOAPIC_NUM_PINS) {
257 if (s->ioregsel & 1) {
258 s->ioredtbl[index] &= 0xffffffff;
259 s->ioredtbl[index] |= (uint64_t)val << 32;
260 } else {
261 s->ioredtbl[index] &= ~0xffffffffULL;
262 s->ioredtbl[index] |= val;
aliguori610626a2009-03-12 20:25:12 +0000263 }
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100264 ioapic_service(s);
265 }
aliguori610626a2009-03-12 20:25:12 +0000266 }
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100267 break;
aliguori610626a2009-03-12 20:25:12 +0000268 }
269}
270
Jan Kiszka35a74c52011-02-03 22:54:12 +0100271static int ioapic_post_load(void *opaque, int version_id)
272{
273 IOAPICState *s = opaque;
274
275 if (version_id == 1) {
276 /* set sane value */
277 s->irr = 0;
278 }
279 return 0;
280}
281
Juan Quintela3e9e9882009-09-10 03:04:43 +0200282static const VMStateDescription vmstate_ioapic = {
283 .name = "ioapic",
Jan Kiszka5dce4992011-02-03 22:54:13 +0100284 .version_id = 3,
Jan Kiszka35a74c52011-02-03 22:54:12 +0100285 .post_load = ioapic_post_load,
Juan Quintela3e9e9882009-09-10 03:04:43 +0200286 .minimum_version_id = 1,
287 .minimum_version_id_old = 1,
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100288 .fields = (VMStateField[]) {
Juan Quintela3e9e9882009-09-10 03:04:43 +0200289 VMSTATE_UINT8(id, IOAPICState),
290 VMSTATE_UINT8(ioregsel, IOAPICState),
Jan Kiszka5dce4992011-02-03 22:54:13 +0100291 VMSTATE_UNUSED_V(2, 8), /* to account for qemu-kvm's v2 format */
Jan Kiszka35a74c52011-02-03 22:54:12 +0100292 VMSTATE_UINT32_V(irr, IOAPICState, 2),
Juan Quintela3e9e9882009-09-10 03:04:43 +0200293 VMSTATE_UINT64_ARRAY(ioredtbl, IOAPICState, IOAPIC_NUM_PINS),
294 VMSTATE_END_OF_LIST()
aliguori610626a2009-03-12 20:25:12 +0000295 }
Juan Quintela3e9e9882009-09-10 03:04:43 +0200296};
aliguori610626a2009-03-12 20:25:12 +0000297
Blue Swirl96051112010-06-19 07:41:43 +0000298static void ioapic_reset(DeviceState *d)
aliguori610626a2009-03-12 20:25:12 +0000299{
Blue Swirl96051112010-06-19 07:41:43 +0000300 IOAPICState *s = DO_UPCAST(IOAPICState, busdev.qdev, d);
aliguori610626a2009-03-12 20:25:12 +0000301 int i;
302
Blue Swirl96051112010-06-19 07:41:43 +0000303 s->id = 0;
304 s->ioregsel = 0;
305 s->irr = 0;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100306 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
307 s->ioredtbl[i] = 1 << IOAPIC_LVT_MASKED_SHIFT;
308 }
aliguori610626a2009-03-12 20:25:12 +0000309}
310
Blue Swirld60efc62009-08-25 18:29:31 +0000311static CPUReadMemoryFunc * const ioapic_mem_read[3] = {
aliguori610626a2009-03-12 20:25:12 +0000312 ioapic_mem_readl,
313 ioapic_mem_readl,
314 ioapic_mem_readl,
315};
316
Blue Swirld60efc62009-08-25 18:29:31 +0000317static CPUWriteMemoryFunc * const ioapic_mem_write[3] = {
aliguori610626a2009-03-12 20:25:12 +0000318 ioapic_mem_writel,
319 ioapic_mem_writel,
320 ioapic_mem_writel,
321};
322
Blue Swirl96051112010-06-19 07:41:43 +0000323static int ioapic_init1(SysBusDevice *dev)
aliguori610626a2009-03-12 20:25:12 +0000324{
Blue Swirl96051112010-06-19 07:41:43 +0000325 IOAPICState *s = FROM_SYSBUS(IOAPICState, dev);
aliguori610626a2009-03-12 20:25:12 +0000326 int io_memory;
Jan Kiszka0280b572011-02-03 22:54:11 +0100327 static int ioapic_no;
328
329 if (ioapic_no >= MAX_IOAPICS) {
330 return -1;
331 }
aliguori610626a2009-03-12 20:25:12 +0000332
Avi Kivity1eed09c2009-06-14 11:38:51 +0300333 io_memory = cpu_register_io_memory(ioapic_mem_read,
Alexander Graf2507c122010-12-08 12:05:37 +0100334 ioapic_mem_write, s,
335 DEVICE_NATIVE_ENDIAN);
Blue Swirl96051112010-06-19 07:41:43 +0000336 sysbus_init_mmio(dev, 0x1000, io_memory);
aliguori610626a2009-03-12 20:25:12 +0000337
Blue Swirl96051112010-06-19 07:41:43 +0000338 qdev_init_gpio_in(&dev->qdev, ioapic_set_irq, IOAPIC_NUM_PINS);
aliguori610626a2009-03-12 20:25:12 +0000339
Jan Kiszka0280b572011-02-03 22:54:11 +0100340 ioapics[ioapic_no++] = s;
341
Blue Swirl96051112010-06-19 07:41:43 +0000342 return 0;
aliguori610626a2009-03-12 20:25:12 +0000343}
Blue Swirl96051112010-06-19 07:41:43 +0000344
345static SysBusDeviceInfo ioapic_info = {
346 .init = ioapic_init1,
347 .qdev.name = "ioapic",
348 .qdev.size = sizeof(IOAPICState),
349 .qdev.vmsd = &vmstate_ioapic,
350 .qdev.reset = ioapic_reset,
351 .qdev.no_user = 1,
352};
353
354static void ioapic_register_devices(void)
355{
356 sysbus_register_withprop(&ioapic_info);
357}
358
359device_init(ioapic_register_devices)