]> git.proxmox.com Git - mirror_qemu.git/blame - hw/intc/ioapic.c
ioapic: coalesce level interrupts
[mirror_qemu.git] / hw / intc / ioapic.c
CommitLineData
610626af
AL
1/*
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
8167ee88 20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
610626af
AL
21 */
22
83c9f4ca 23#include "hw/hw.h"
0d09e41a 24#include "hw/i386/pc.h"
0d09e41a
PB
25#include "hw/i386/ioapic.h"
26#include "hw/i386/ioapic_internal.h"
610626af
AL
27
28//#define DEBUG_IOAPIC
29
9af9b330
BS
30#ifdef DEBUG_IOAPIC
31#define DPRINTF(fmt, ...) \
32 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
33#else
34#define DPRINTF(fmt, ...)
35#endif
36
244ac3af 37static IOAPICCommonState *ioapics[MAX_IOAPICS];
0280b571 38
db0f8888
XZ
39/* global variable from ioapic_common.c */
40extern int ioapic_no;
41
244ac3af 42static void ioapic_service(IOAPICCommonState *s)
610626af
AL
43{
44 uint8_t i;
45 uint8_t trig_mode;
46 uint8_t vector;
47 uint8_t delivery_mode;
48 uint32_t mask;
49 uint64_t entry;
50 uint8_t dest;
51 uint8_t dest_mode;
610626af
AL
52
53 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
54 mask = 1 << i;
55 if (s->irr & mask) {
56 entry = s->ioredtbl[i];
57 if (!(entry & IOAPIC_LVT_MASKED)) {
1f5e71a8
JK
58 trig_mode = ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1);
59 dest = entry >> IOAPIC_LVT_DEST_SHIFT;
60 dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
61 delivery_mode =
62 (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) & IOAPIC_DM_MASK;
0280b571 63 if (trig_mode == IOAPIC_TRIGGER_EDGE) {
610626af 64 s->irr &= ~mask;
0280b571
JK
65 } else {
66 s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
67 }
1f5e71a8 68 if (delivery_mode == IOAPIC_DM_EXTINT) {
610626af 69 vector = pic_read_irq(isa_pic);
1f5e71a8
JK
70 } else {
71 vector = entry & IOAPIC_VECTOR_MASK;
72 }
610626af 73 apic_deliver_irq(dest, dest_mode, delivery_mode,
1f6f408c 74 vector, trig_mode);
610626af
AL
75 }
76 }
77 }
78}
79
7d0500c4 80static void ioapic_set_irq(void *opaque, int vector, int level)
610626af 81{
244ac3af 82 IOAPICCommonState *s = opaque;
610626af
AL
83
84 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
85 * to GSI 2. GSI maps to ioapic 1-1. This is not
86 * the cleanest way of doing it but it should work. */
87
1f5e71a8
JK
88 DPRINTF("%s: %s vec %x\n", __func__, level ? "raise" : "lower", vector);
89 if (vector == 0) {
610626af 90 vector = 2;
1f5e71a8 91 }
610626af
AL
92 if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
93 uint32_t mask = 1 << vector;
94 uint64_t entry = s->ioredtbl[vector];
95
1f5e71a8
JK
96 if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
97 IOAPIC_TRIGGER_LEVEL) {
610626af
AL
98 /* level triggered */
99 if (level) {
100 s->irr |= mask;
c5955a56
PB
101 if (!(entry & IOAPIC_LVT_REMOTE_IRR)) {
102 ioapic_service(s);
103 }
610626af
AL
104 } else {
105 s->irr &= ~mask;
106 }
107 } else {
47f7be39
JK
108 /* According to the 82093AA manual, we must ignore edge requests
109 * if the input pin is masked. */
110 if (level && !(entry & IOAPIC_LVT_MASKED)) {
610626af
AL
111 s->irr |= mask;
112 ioapic_service(s);
113 }
114 }
115 }
116}
117
0280b571
JK
118void ioapic_eoi_broadcast(int vector)
119{
244ac3af 120 IOAPICCommonState *s;
0280b571
JK
121 uint64_t entry;
122 int i, n;
123
124 for (i = 0; i < MAX_IOAPICS; i++) {
125 s = ioapics[i];
126 if (!s) {
127 continue;
128 }
129 for (n = 0; n < IOAPIC_NUM_PINS; n++) {
130 entry = s->ioredtbl[n];
1f5e71a8
JK
131 if ((entry & IOAPIC_LVT_REMOTE_IRR)
132 && (entry & IOAPIC_VECTOR_MASK) == vector) {
0280b571
JK
133 s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
134 if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
135 ioapic_service(s);
136 }
137 }
138 }
139 }
140}
141
4d5bf5f6 142static uint64_t
a8170e5e 143ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
610626af 144{
244ac3af 145 IOAPICCommonState *s = opaque;
610626af
AL
146 int index;
147 uint32_t val = 0;
148
1f5e71a8
JK
149 switch (addr & 0xff) {
150 case IOAPIC_IOREGSEL:
610626af 151 val = s->ioregsel;
1f5e71a8
JK
152 break;
153 case IOAPIC_IOWIN:
1a440963
JK
154 if (size != 4) {
155 break;
156 }
610626af 157 switch (s->ioregsel) {
1f5e71a8
JK
158 case IOAPIC_REG_ID:
159 val = s->id << IOAPIC_ID_SHIFT;
160 break;
161 case IOAPIC_REG_VER:
162 val = IOAPIC_VERSION |
163 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
164 break;
165 case IOAPIC_REG_ARB:
166 val = 0;
167 break;
168 default:
169 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
170 if (index >= 0 && index < IOAPIC_NUM_PINS) {
171 if (s->ioregsel & 1) {
172 val = s->ioredtbl[index] >> 32;
173 } else {
174 val = s->ioredtbl[index] & 0xffffffff;
610626af 175 }
1f5e71a8 176 }
610626af 177 }
9af9b330 178 DPRINTF("read: %08x = %08x\n", s->ioregsel, val);
1f5e71a8 179 break;
610626af
AL
180 }
181 return val;
182}
183
1f5e71a8 184static void
a8170e5e 185ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
4d5bf5f6 186 unsigned int size)
610626af 187{
244ac3af 188 IOAPICCommonState *s = opaque;
610626af
AL
189 int index;
190
1f5e71a8
JK
191 switch (addr & 0xff) {
192 case IOAPIC_IOREGSEL:
610626af 193 s->ioregsel = val;
1f5e71a8
JK
194 break;
195 case IOAPIC_IOWIN:
1a440963
JK
196 if (size != 4) {
197 break;
198 }
0c1f781b 199 DPRINTF("write: %08x = %08" PRIx64 "\n", s->ioregsel, val);
610626af 200 switch (s->ioregsel) {
1f5e71a8
JK
201 case IOAPIC_REG_ID:
202 s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
203 break;
204 case IOAPIC_REG_VER:
205 case IOAPIC_REG_ARB:
206 break;
207 default:
208 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
209 if (index >= 0 && index < IOAPIC_NUM_PINS) {
210 if (s->ioregsel & 1) {
211 s->ioredtbl[index] &= 0xffffffff;
212 s->ioredtbl[index] |= (uint64_t)val << 32;
213 } else {
214 s->ioredtbl[index] &= ~0xffffffffULL;
215 s->ioredtbl[index] |= val;
610626af 216 }
1f5e71a8
JK
217 ioapic_service(s);
218 }
610626af 219 }
1f5e71a8 220 break;
610626af
AL
221 }
222}
223
4d5bf5f6
JK
224static const MemoryRegionOps ioapic_io_ops = {
225 .read = ioapic_mem_read,
226 .write = ioapic_mem_write,
227 .endianness = DEVICE_NATIVE_ENDIAN,
610626af
AL
228};
229
db0f8888 230static void ioapic_realize(DeviceState *dev, Error **errp)
610626af 231{
db0f8888 232 IOAPICCommonState *s = IOAPIC_COMMON(dev);
f9771858 233
1437c94b
PB
234 memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
235 "ioapic", 0x1000);
610626af 236
f9771858 237 qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
0280b571 238
db0f8888 239 ioapics[ioapic_no] = s;
610626af 240}
96051119 241
999e12bb
AL
242static void ioapic_class_init(ObjectClass *klass, void *data)
243{
244 IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
39bffca2 245 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 246
db0f8888 247 k->realize = ioapic_realize;
39bffca2 248 dc->reset = ioapic_reset_common;
999e12bb
AL
249}
250
8c43a6f0 251static const TypeInfo ioapic_info = {
39bffca2
AL
252 .name = "ioapic",
253 .parent = TYPE_IOAPIC_COMMON,
254 .instance_size = sizeof(IOAPICCommonState),
255 .class_init = ioapic_class_init,
96051119
BS
256};
257
83f7d43a 258static void ioapic_register_types(void)
96051119 259{
39bffca2 260 type_register_static(&ioapic_info);
96051119
BS
261}
262
83f7d43a 263type_init(ioapic_register_types)