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/>.
27 #include "ioapic_internal.h"
29 //#define DEBUG_IOAPIC
32 #define DPRINTF(fmt, ...) \
33 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
35 #define DPRINTF(fmt, ...)
38 static IOAPICCommonState
*ioapics
[MAX_IOAPICS
];
40 static void ioapic_service(IOAPICCommonState
*s
)
45 uint8_t delivery_mode
;
51 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
54 entry
= s
->ioredtbl
[i
];
55 if (!(entry
& IOAPIC_LVT_MASKED
)) {
56 trig_mode
= ((entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1);
57 dest
= entry
>> IOAPIC_LVT_DEST_SHIFT
;
58 dest_mode
= (entry
>> IOAPIC_LVT_DEST_MODE_SHIFT
) & 1;
60 (entry
>> IOAPIC_LVT_DELIV_MODE_SHIFT
) & IOAPIC_DM_MASK
;
61 if (trig_mode
== IOAPIC_TRIGGER_EDGE
) {
64 s
->ioredtbl
[i
] |= IOAPIC_LVT_REMOTE_IRR
;
66 if (delivery_mode
== IOAPIC_DM_EXTINT
) {
67 vector
= pic_read_irq(isa_pic
);
69 vector
= entry
& IOAPIC_VECTOR_MASK
;
71 apic_deliver_irq(dest
, dest_mode
, delivery_mode
,
78 static void ioapic_set_irq(void *opaque
, int vector
, int level
)
80 IOAPICCommonState
*s
= opaque
;
82 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
83 * to GSI 2. GSI maps to ioapic 1-1. This is not
84 * the cleanest way of doing it but it should work. */
86 DPRINTF("%s: %s vec %x\n", __func__
, level
? "raise" : "lower", vector
);
90 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
91 uint32_t mask
= 1 << vector
;
92 uint64_t entry
= s
->ioredtbl
[vector
];
94 if (entry
& (1 << IOAPIC_LVT_POLARITY_SHIFT
)) {
97 if (((entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1) ==
98 IOAPIC_TRIGGER_LEVEL
) {
107 /* According to the 82093AA manual, we must ignore edge requests
108 * if the input pin is masked. */
109 if (level
&& !(entry
& IOAPIC_LVT_MASKED
)) {
117 void ioapic_eoi_broadcast(int vector
)
119 IOAPICCommonState
*s
;
123 for (i
= 0; i
< MAX_IOAPICS
; i
++) {
128 for (n
= 0; n
< IOAPIC_NUM_PINS
; n
++) {
129 entry
= s
->ioredtbl
[n
];
130 if ((entry
& IOAPIC_LVT_REMOTE_IRR
)
131 && (entry
& IOAPIC_VECTOR_MASK
) == vector
) {
132 s
->ioredtbl
[n
] = entry
& ~IOAPIC_LVT_REMOTE_IRR
;
133 if (!(entry
& IOAPIC_LVT_MASKED
) && (s
->irr
& (1 << n
))) {
142 ioapic_mem_read(void *opaque
, hwaddr addr
, unsigned int size
)
144 IOAPICCommonState
*s
= opaque
;
148 switch (addr
& 0xff) {
149 case IOAPIC_IOREGSEL
:
156 switch (s
->ioregsel
) {
158 val
= s
->id
<< IOAPIC_ID_SHIFT
;
161 val
= IOAPIC_VERSION
|
162 ((IOAPIC_NUM_PINS
- 1) << IOAPIC_VER_ENTRIES_SHIFT
);
168 index
= (s
->ioregsel
- IOAPIC_REG_REDTBL_BASE
) >> 1;
169 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
170 if (s
->ioregsel
& 1) {
171 val
= s
->ioredtbl
[index
] >> 32;
173 val
= s
->ioredtbl
[index
] & 0xffffffff;
177 DPRINTF("read: %08x = %08x\n", s
->ioregsel
, val
);
184 ioapic_mem_write(void *opaque
, hwaddr addr
, uint64_t val
,
187 IOAPICCommonState
*s
= opaque
;
190 switch (addr
& 0xff) {
191 case IOAPIC_IOREGSEL
:
198 DPRINTF("write: %08x = %08" PRIx64
"\n", s
->ioregsel
, val
);
199 switch (s
->ioregsel
) {
201 s
->id
= (val
>> IOAPIC_ID_SHIFT
) & IOAPIC_ID_MASK
;
207 index
= (s
->ioregsel
- IOAPIC_REG_REDTBL_BASE
) >> 1;
208 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
209 if (s
->ioregsel
& 1) {
210 s
->ioredtbl
[index
] &= 0xffffffff;
211 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
213 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
214 s
->ioredtbl
[index
] |= val
;
223 static const MemoryRegionOps ioapic_io_ops
= {
224 .read
= ioapic_mem_read
,
225 .write
= ioapic_mem_write
,
226 .endianness
= DEVICE_NATIVE_ENDIAN
,
229 static void ioapic_init(IOAPICCommonState
*s
, int instance_no
)
231 memory_region_init_io(&s
->io_memory
, &ioapic_io_ops
, s
, "ioapic", 0x1000);
233 qdev_init_gpio_in(&s
->busdev
.qdev
, ioapic_set_irq
, IOAPIC_NUM_PINS
);
235 ioapics
[instance_no
] = s
;
238 static void ioapic_class_init(ObjectClass
*klass
, void *data
)
240 IOAPICCommonClass
*k
= IOAPIC_COMMON_CLASS(klass
);
241 DeviceClass
*dc
= DEVICE_CLASS(klass
);
243 k
->init
= ioapic_init
;
244 dc
->reset
= ioapic_reset_common
;
247 static const TypeInfo ioapic_info
= {
249 .parent
= TYPE_IOAPIC_COMMON
,
250 .instance_size
= sizeof(IOAPICCommonState
),
251 .class_init
= ioapic_class_init
,
254 static void ioapic_register_types(void)
256 type_register_static(&ioapic_info
);
259 type_init(ioapic_register_types
)