]> Git Repo - qemu.git/blame - hw/intc/ioapic.c
Merge remote-tracking branch 'remotes/agraf/tags/signed-ppc-for-upstream' into staging
[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 <[email protected]>
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;
101 ioapic_service(s);
102 } else {
103 s->irr &= ~mask;
104 }
105 } else {
47f7be39
JK
106 /* According to the 82093AA manual, we must ignore edge requests
107 * if the input pin is masked. */
108 if (level && !(entry & IOAPIC_LVT_MASKED)) {
610626af
AL
109 s->irr |= mask;
110 ioapic_service(s);
111 }
112 }
113 }
114}
115
0280b571
JK
116void ioapic_eoi_broadcast(int vector)
117{
244ac3af 118 IOAPICCommonState *s;
0280b571
JK
119 uint64_t entry;
120 int i, n;
121
122 for (i = 0; i < MAX_IOAPICS; i++) {
123 s = ioapics[i];
124 if (!s) {
125 continue;
126 }
127 for (n = 0; n < IOAPIC_NUM_PINS; n++) {
128 entry = s->ioredtbl[n];
1f5e71a8
JK
129 if ((entry & IOAPIC_LVT_REMOTE_IRR)
130 && (entry & IOAPIC_VECTOR_MASK) == vector) {
0280b571
JK
131 s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
132 if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
133 ioapic_service(s);
134 }
135 }
136 }
137 }
138}
139
4d5bf5f6 140static uint64_t
a8170e5e 141ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
610626af 142{
244ac3af 143 IOAPICCommonState *s = opaque;
610626af
AL
144 int index;
145 uint32_t val = 0;
146
1f5e71a8
JK
147 switch (addr & 0xff) {
148 case IOAPIC_IOREGSEL:
610626af 149 val = s->ioregsel;
1f5e71a8
JK
150 break;
151 case IOAPIC_IOWIN:
1a440963
JK
152 if (size != 4) {
153 break;
154 }
610626af 155 switch (s->ioregsel) {
1f5e71a8
JK
156 case IOAPIC_REG_ID:
157 val = s->id << IOAPIC_ID_SHIFT;
158 break;
159 case IOAPIC_REG_VER:
160 val = IOAPIC_VERSION |
161 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
162 break;
163 case IOAPIC_REG_ARB:
164 val = 0;
165 break;
166 default:
167 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
168 if (index >= 0 && index < IOAPIC_NUM_PINS) {
169 if (s->ioregsel & 1) {
170 val = s->ioredtbl[index] >> 32;
171 } else {
172 val = s->ioredtbl[index] & 0xffffffff;
610626af 173 }
1f5e71a8 174 }
610626af 175 }
9af9b330 176 DPRINTF("read: %08x = %08x\n", s->ioregsel, val);
1f5e71a8 177 break;
610626af
AL
178 }
179 return val;
180}
181
1f5e71a8 182static void
a8170e5e 183ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
4d5bf5f6 184 unsigned int size)
610626af 185{
244ac3af 186 IOAPICCommonState *s = opaque;
610626af
AL
187 int index;
188
1f5e71a8
JK
189 switch (addr & 0xff) {
190 case IOAPIC_IOREGSEL:
610626af 191 s->ioregsel = val;
1f5e71a8
JK
192 break;
193 case IOAPIC_IOWIN:
1a440963
JK
194 if (size != 4) {
195 break;
196 }
0c1f781b 197 DPRINTF("write: %08x = %08" PRIx64 "\n", s->ioregsel, val);
610626af 198 switch (s->ioregsel) {
1f5e71a8
JK
199 case IOAPIC_REG_ID:
200 s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
201 break;
202 case IOAPIC_REG_VER:
203 case IOAPIC_REG_ARB:
204 break;
205 default:
206 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
207 if (index >= 0 && index < IOAPIC_NUM_PINS) {
208 if (s->ioregsel & 1) {
209 s->ioredtbl[index] &= 0xffffffff;
210 s->ioredtbl[index] |= (uint64_t)val << 32;
211 } else {
212 s->ioredtbl[index] &= ~0xffffffffULL;
213 s->ioredtbl[index] |= val;
610626af 214 }
1f5e71a8
JK
215 ioapic_service(s);
216 }
610626af 217 }
1f5e71a8 218 break;
610626af
AL
219 }
220}
221
4d5bf5f6
JK
222static const MemoryRegionOps ioapic_io_ops = {
223 .read = ioapic_mem_read,
224 .write = ioapic_mem_write,
225 .endianness = DEVICE_NATIVE_ENDIAN,
610626af
AL
226};
227
db0f8888 228static void ioapic_realize(DeviceState *dev, Error **errp)
610626af 229{
db0f8888 230 IOAPICCommonState *s = IOAPIC_COMMON(dev);
f9771858 231
1437c94b
PB
232 memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
233 "ioapic", 0x1000);
610626af 234
f9771858 235 qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
0280b571 236
db0f8888 237 ioapics[ioapic_no] = s;
610626af 238}
96051119 239
999e12bb
AL
240static void ioapic_class_init(ObjectClass *klass, void *data)
241{
242 IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
39bffca2 243 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 244
db0f8888 245 k->realize = ioapic_realize;
39bffca2 246 dc->reset = ioapic_reset_common;
999e12bb
AL
247}
248
8c43a6f0 249static const TypeInfo ioapic_info = {
39bffca2
AL
250 .name = "ioapic",
251 .parent = TYPE_IOAPIC_COMMON,
252 .instance_size = sizeof(IOAPICCommonState),
253 .class_init = ioapic_class_init,
96051119
BS
254};
255
83f7d43a 256static void ioapic_register_types(void)
96051119 257{
39bffca2 258 type_register_static(&ioapic_info);
96051119
BS
259}
260
83f7d43a 261type_init(ioapic_register_types)
This page took 0.610281 seconds and 4 git commands to generate.