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/>.
24 #include "hw/i386/pc.h"
25 #include "hw/i386/ioapic.h"
26 #include "hw/i386/ioapic_internal.h"
28 //#define DEBUG_IOAPIC
31 #define DPRINTF(fmt, ...) \
32 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...)
37 static IOAPICCommonState
*ioapics
[MAX_IOAPICS
];
39 static void ioapic_service(IOAPICCommonState
*s
)
44 uint8_t delivery_mode
;
50 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
53 entry
= s
->ioredtbl
[i
];
54 if (!(entry
& IOAPIC_LVT_MASKED
)) {
55 trig_mode
= ((entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1);
56 dest
= entry
>> IOAPIC_LVT_DEST_SHIFT
;
57 dest_mode
= (entry
>> IOAPIC_LVT_DEST_MODE_SHIFT
) & 1;
59 (entry
>> IOAPIC_LVT_DELIV_MODE_SHIFT
) & IOAPIC_DM_MASK
;
60 if (trig_mode
== IOAPIC_TRIGGER_EDGE
) {
63 s
->ioredtbl
[i
] |= IOAPIC_LVT_REMOTE_IRR
;
65 if (delivery_mode
== IOAPIC_DM_EXTINT
) {
66 vector
= pic_read_irq(isa_pic
);
68 vector
= entry
& IOAPIC_VECTOR_MASK
;
70 apic_deliver_irq(dest
, dest_mode
, delivery_mode
,
77 static void ioapic_set_irq(void *opaque
, int vector
, int level
)
79 IOAPICCommonState
*s
= opaque
;
81 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
82 * to GSI 2. GSI maps to ioapic 1-1. This is not
83 * the cleanest way of doing it but it should work. */
85 DPRINTF("%s: %s vec %x\n", __func__
, level
? "raise" : "lower", vector
);
89 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
90 uint32_t mask
= 1 << vector
;
91 uint64_t entry
= s
->ioredtbl
[vector
];
93 if (entry
& (1 << IOAPIC_LVT_POLARITY_SHIFT
)) {
96 if (((entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1) ==
97 IOAPIC_TRIGGER_LEVEL
) {
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
)) {
116 void ioapic_eoi_broadcast(int vector
)
118 IOAPICCommonState
*s
;
122 for (i
= 0; i
< MAX_IOAPICS
; i
++) {
127 for (n
= 0; n
< IOAPIC_NUM_PINS
; n
++) {
128 entry
= s
->ioredtbl
[n
];
129 if ((entry
& IOAPIC_LVT_REMOTE_IRR
)
130 && (entry
& IOAPIC_VECTOR_MASK
) == vector
) {
131 s
->ioredtbl
[n
] = entry
& ~IOAPIC_LVT_REMOTE_IRR
;
132 if (!(entry
& IOAPIC_LVT_MASKED
) && (s
->irr
& (1 << n
))) {
141 ioapic_mem_read(void *opaque
, hwaddr addr
, unsigned int size
)
143 IOAPICCommonState
*s
= opaque
;
147 switch (addr
& 0xff) {
148 case IOAPIC_IOREGSEL
:
155 switch (s
->ioregsel
) {
157 val
= s
->id
<< IOAPIC_ID_SHIFT
;
160 val
= IOAPIC_VERSION
|
161 ((IOAPIC_NUM_PINS
- 1) << IOAPIC_VER_ENTRIES_SHIFT
);
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;
172 val
= s
->ioredtbl
[index
] & 0xffffffff;
176 DPRINTF("read: %08x = %08x\n", s
->ioregsel
, val
);
183 ioapic_mem_write(void *opaque
, hwaddr addr
, uint64_t val
,
186 IOAPICCommonState
*s
= opaque
;
189 switch (addr
& 0xff) {
190 case IOAPIC_IOREGSEL
:
197 DPRINTF("write: %08x = %08" PRIx64
"\n", s
->ioregsel
, val
);
198 switch (s
->ioregsel
) {
200 s
->id
= (val
>> IOAPIC_ID_SHIFT
) & IOAPIC_ID_MASK
;
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;
212 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
213 s
->ioredtbl
[index
] |= val
;
222 static const MemoryRegionOps ioapic_io_ops
= {
223 .read
= ioapic_mem_read
,
224 .write
= ioapic_mem_write
,
225 .endianness
= DEVICE_NATIVE_ENDIAN
,
228 static void ioapic_init(IOAPICCommonState
*s
, int instance_no
)
230 memory_region_init_io(&s
->io_memory
, &ioapic_io_ops
, s
, "ioapic", 0x1000);
232 qdev_init_gpio_in(&s
->busdev
.qdev
, ioapic_set_irq
, IOAPIC_NUM_PINS
);
234 ioapics
[instance_no
] = s
;
237 static void ioapic_class_init(ObjectClass
*klass
, void *data
)
239 IOAPICCommonClass
*k
= IOAPIC_COMMON_CLASS(klass
);
240 DeviceClass
*dc
= DEVICE_CLASS(klass
);
242 k
->init
= ioapic_init
;
243 dc
->reset
= ioapic_reset_common
;
246 static const TypeInfo ioapic_info
= {
248 .parent
= TYPE_IOAPIC_COMMON
,
249 .instance_size
= sizeof(IOAPICCommonState
),
250 .class_init
= ioapic_class_init
,
253 static void ioapic_register_types(void)
255 type_register_static(&ioapic_info
);
258 type_init(ioapic_register_types
)