Disable preadv/pwritev support
[qemu-kvm/fedora.git] / hw / ioapic.c
blob32019de66c1ae27bc8ed27b71ecedc2db95db0f4
1 /*
2 * ioapic.c IOAPIC emulation logic
4 * Copyright (c) 2004-2005 Fabrice Bellard
6 * Split the ioapic logic from apic.c
7 * Xiantao Zhang <xiantao.zhang@intel.com>
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.
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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "hw.h"
24 #include "pc.h"
25 #include "sysemu.h"
26 #include "qemu-timer.h"
27 #include "host-utils.h"
29 #include "qemu-kvm.h"
31 //#define DEBUG_IOAPIC
33 #define IOAPIC_NUM_PINS 0x18
34 #define IOAPIC_DEFAULT_BASE_ADDRESS 0xfec00000
35 #define IOAPIC_LVT_MASKED (1<<16)
37 #define IOAPIC_TRIGGER_EDGE 0
38 #define IOAPIC_TRIGGER_LEVEL 1
40 /*io{apic,sapic} delivery mode*/
41 #define IOAPIC_DM_FIXED 0x0
42 #define IOAPIC_DM_LOWEST_PRIORITY 0x1
43 #define IOAPIC_DM_PMI 0x2
44 #define IOAPIC_DM_NMI 0x4
45 #define IOAPIC_DM_INIT 0x5
46 #define IOAPIC_DM_SIPI 0x5
47 #define IOAPIC_DM_EXTINT 0x7
49 struct IOAPICState {
50 uint8_t id;
51 uint8_t ioregsel;
52 uint64_t base_address;
54 uint32_t irr;
55 uint64_t ioredtbl[IOAPIC_NUM_PINS];
58 static void ioapic_service(IOAPICState *s)
60 uint8_t i;
61 uint8_t trig_mode;
62 uint8_t vector;
63 uint8_t delivery_mode;
64 uint32_t mask;
65 uint64_t entry;
66 uint8_t dest;
67 uint8_t dest_mode;
68 uint8_t polarity;
70 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
71 mask = 1 << i;
72 if (s->irr & mask) {
73 entry = s->ioredtbl[i];
74 if (!(entry & IOAPIC_LVT_MASKED)) {
75 trig_mode = ((entry >> 15) & 1);
76 dest = entry >> 56;
77 dest_mode = (entry >> 11) & 1;
78 delivery_mode = (entry >> 8) & 7;
79 polarity = (entry >> 13) & 1;
80 if (trig_mode == IOAPIC_TRIGGER_EDGE)
81 s->irr &= ~mask;
82 if (delivery_mode == IOAPIC_DM_EXTINT)
83 vector = pic_read_irq(isa_pic);
84 else
85 vector = entry & 0xff;
87 apic_deliver_irq(dest, dest_mode, delivery_mode,
88 vector, polarity, trig_mode);
94 void ioapic_set_irq(void *opaque, int vector, int level)
96 IOAPICState *s = opaque;
98 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
99 * to GSI 2. GSI maps to ioapic 1-1. This is not
100 * the cleanest way of doing it but it should work. */
102 if (vector == 0 && irq0override) {
103 vector = 2;
106 if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
107 uint32_t mask = 1 << vector;
108 uint64_t entry = s->ioredtbl[vector];
110 if ((entry >> 15) & 1) {
111 /* level triggered */
112 if (level) {
113 s->irr |= mask;
114 ioapic_service(s);
115 } else {
116 s->irr &= ~mask;
118 } else {
119 /* edge triggered */
120 if (level) {
121 s->irr |= mask;
122 ioapic_service(s);
128 static uint32_t ioapic_mem_readl(void *opaque, target_phys_addr_t addr)
130 IOAPICState *s = opaque;
131 int index;
132 uint32_t val = 0;
134 addr &= 0xff;
135 if (addr == 0x00) {
136 val = s->ioregsel;
137 } else if (addr == 0x10) {
138 switch (s->ioregsel) {
139 case 0x00:
140 val = s->id << 24;
141 break;
142 case 0x01:
143 val = 0x11 | ((IOAPIC_NUM_PINS - 1) << 16); /* version 0x11 */
144 break;
145 case 0x02:
146 val = 0;
147 break;
148 default:
149 index = (s->ioregsel - 0x10) >> 1;
150 if (index >= 0 && index < IOAPIC_NUM_PINS) {
151 if (s->ioregsel & 1)
152 val = s->ioredtbl[index] >> 32;
153 else
154 val = s->ioredtbl[index] & 0xffffffff;
157 #ifdef DEBUG_IOAPIC
158 printf("I/O APIC read: %08x = %08x\n", s->ioregsel, val);
159 #endif
161 return val;
164 static void ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
166 IOAPICState *s = opaque;
167 int index;
169 addr &= 0xff;
170 if (addr == 0x00) {
171 s->ioregsel = val;
172 return;
173 } else if (addr == 0x10) {
174 #ifdef DEBUG_IOAPIC
175 printf("I/O APIC write: %08x = %08x\n", s->ioregsel, val);
176 #endif
177 switch (s->ioregsel) {
178 case 0x00:
179 s->id = (val >> 24) & 0xff;
180 return;
181 case 0x01:
182 case 0x02:
183 return;
184 default:
185 index = (s->ioregsel - 0x10) >> 1;
186 if (index >= 0 && index < IOAPIC_NUM_PINS) {
187 if (s->ioregsel & 1) {
188 s->ioredtbl[index] &= 0xffffffff;
189 s->ioredtbl[index] |= (uint64_t)val << 32;
190 } else {
191 s->ioredtbl[index] &= ~0xffffffffULL;
192 s->ioredtbl[index] |= val;
194 ioapic_service(s);
200 static void kvm_kernel_ioapic_save_to_user(IOAPICState *s)
202 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
203 struct kvm_irqchip chip;
204 struct kvm_ioapic_state *kioapic;
205 int i;
207 chip.chip_id = KVM_IRQCHIP_IOAPIC;
208 kvm_get_irqchip(kvm_context, &chip);
209 kioapic = &chip.chip.ioapic;
211 s->id = kioapic->id;
212 s->ioregsel = kioapic->ioregsel;
213 s->base_address = kioapic->base_address;
214 s->irr = kioapic->irr;
215 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
216 s->ioredtbl[i] = kioapic->redirtbl[i].bits;
218 #endif
221 static void kvm_kernel_ioapic_load_from_user(IOAPICState *s)
223 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
224 struct kvm_irqchip chip;
225 struct kvm_ioapic_state *kioapic;
226 int i;
228 chip.chip_id = KVM_IRQCHIP_IOAPIC;
229 kioapic = &chip.chip.ioapic;
231 kioapic->id = s->id;
232 kioapic->ioregsel = s->ioregsel;
233 kioapic->base_address = s->base_address;
234 kioapic->irr = s->irr;
235 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
236 kioapic->redirtbl[i].bits = s->ioredtbl[i];
239 kvm_set_irqchip(kvm_context, &chip);
240 #endif
243 static void ioapic_save(QEMUFile *f, void *opaque)
245 IOAPICState *s = opaque;
246 int i;
248 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
249 kvm_kernel_ioapic_save_to_user(s);
252 qemu_put_8s(f, &s->id);
253 qemu_put_8s(f, &s->ioregsel);
254 qemu_put_be64s(f, &s->base_address);
255 qemu_put_be32s(f, &s->irr);
256 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
257 qemu_put_be64s(f, &s->ioredtbl[i]);
261 static int ioapic_load(QEMUFile *f, void *opaque, int version_id)
263 IOAPICState *s = opaque;
264 int i;
266 if (version_id < 1 || version_id > 2)
267 return -EINVAL;
269 qemu_get_8s(f, &s->id);
270 qemu_get_8s(f, &s->ioregsel);
271 if (version_id == 2) {
272 /* for version 2, we get this data off of the wire */
273 qemu_get_be64s(f, &s->base_address);
274 qemu_get_be32s(f, &s->irr);
276 else {
277 /* in case we are doing version 1, we just set these to sane values */
278 s->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
279 s->irr = 0;
281 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
282 qemu_get_be64s(f, &s->ioredtbl[i]);
285 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
286 kvm_kernel_ioapic_load_from_user(s);
289 return 0;
292 static void ioapic_reset(void *opaque)
294 IOAPICState *s = opaque;
295 int i;
297 memset(s, 0, sizeof(*s));
298 s->base_address = IOAPIC_DEFAULT_BASE_ADDRESS;
299 for(i = 0; i < IOAPIC_NUM_PINS; i++)
300 s->ioredtbl[i] = 1 << 16; /* mask LVT */
301 #ifdef KVM_CAP_IRQCHIP
302 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
303 kvm_kernel_ioapic_load_from_user(s);
305 #endif
308 static CPUReadMemoryFunc *ioapic_mem_read[3] = {
309 ioapic_mem_readl,
310 ioapic_mem_readl,
311 ioapic_mem_readl,
314 static CPUWriteMemoryFunc *ioapic_mem_write[3] = {
315 ioapic_mem_writel,
316 ioapic_mem_writel,
317 ioapic_mem_writel,
320 IOAPICState *ioapic_init(void)
322 IOAPICState *s;
323 int io_memory;
325 s = qemu_mallocz(sizeof(IOAPICState));
326 ioapic_reset(s);
328 io_memory = cpu_register_io_memory(ioapic_mem_read,
329 ioapic_mem_write, s);
330 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory);
332 register_savevm("ioapic", 0, 2, ioapic_save, ioapic_load, s);
333 qemu_register_reset(ioapic_reset, s);
335 return s;