272
280
apic_set_irq(apic_iter, vector_num, trigger_mode) );
283
void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
284
uint8_t delivery_mode, uint8_t vector_num,
285
uint8_t polarity, uint8_t trigger_mode)
287
uint32_t deliver_bitmask[MAX_APIC_WORDS];
289
apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
290
apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
275
294
void cpu_set_apic_base(CPUState *env, uint64_t val)
277
296
APICState *s = env->apic_state;
278
297
#ifdef DEBUG_APIC
279
298
printf("cpu_set_apic_base: %016" PRIx64 "\n", val);
281
302
s->apicbase = (val & 0xfffff000) |
282
303
(s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
283
304
/* if disabled, cannot be enabled again */
447
494
s->initial_count = 0;
448
495
s->initial_count_load_time = 0;
449
496
s->next_time = 0;
451
cpu_reset(s->cpu_env);
453
if (!(s->apicbase & MSR_IA32_APICBASE_BSP))
454
s->cpu_env->halted = 1;
497
s->wait_for_sipi = 1;
499
env->halted = !(s->apicbase & MSR_IA32_APICBASE_BSP);
457
/* send a SIPI message to the CPU to start it */
458
502
static void apic_startup(APICState *s, int vector_num)
460
CPUState *env = s->cpu_env;
504
s->sipi_vector = vector_num;
505
cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
508
void apic_sipi(CPUState *env)
510
APICState *s = env->apic_state;
512
cpu_reset_interrupt(env, CPU_INTERRUPT_SIPI);
514
if (!s->wait_for_sipi)
464
cpu_x86_load_seg_cache(env, R_CS, vector_num << 8, vector_num << 12,
518
cpu_x86_load_seg_cache(env, R_CS, s->sipi_vector << 8, s->sipi_vector << 12,
521
s->wait_for_sipi = 0;
469
524
static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
763
static void apic_send_msi(target_phys_addr_t addr, uint32 data)
765
uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
766
uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
767
uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
768
uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
769
uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
770
/* XXX: Ignore redirection hint. */
771
apic_deliver_irq(dest, dest_mode, delivery, vector, 0, trigger_mode);
708
774
static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
778
int index = (addr >> 4) & 0xff;
779
if (addr > 0xfff || !index) {
780
/* MSI and MMIO APIC are at the same memory location,
781
* but actually not on the global bus: MSI is on PCI bus
782
* APIC is connected directly to the CPU.
783
* Mapping them on the global bus happens to work because
784
* MSI registers are reserved in APIC MMIO and vice versa. */
785
apic_send_msi(addr, val);
714
789
env = cpu_single_env;
898
if (last_apic_id >= MAX_APICS)
976
if (last_apic_idx >= MAX_APICS)
900
978
s = qemu_mallocz(sizeof(APICState));
901
979
env->apic_state = s;
902
s->id = last_apic_id++;
903
env->cpuid_apic_id = s->id;
980
s->idx = last_apic_idx++;
981
s->id = env->cpuid_apic_id;
904
982
s->cpu_env = env;
908
987
/* XXX: mapping more APICs at the same memory location */
909
988
if (apic_io_memory == 0) {
910
989
/* NOTE: the APIC is directly connected to the CPU - it is not
911
990
on the global memory bus. */
912
apic_io_memory = cpu_register_io_memory(0, apic_mem_read,
991
apic_io_memory = cpu_register_io_memory(apic_mem_read,
913
992
apic_mem_write, NULL);
914
cpu_register_physical_memory(s->apicbase & ~0xfff, 0x1000,
993
/* XXX: what if the base changes? */
994
cpu_register_physical_memory(MSI_ADDR_BASE, MSI_ADDR_SIZE,
917
997
s->timer = qemu_new_timer(vm_clock, apic_timer, s);
919
register_savevm("apic", s->id, 2, apic_save, apic_load, s);
999
register_savevm("apic", s->idx, 2, apic_save, apic_load, s);
920
1000
qemu_register_reset(apic_reset, s);
922
local_apics[s->id] = s;
926
static void ioapic_service(IOAPICState *s)
931
uint8_t delivery_mode;
937
uint32_t deliver_bitmask[MAX_APIC_WORDS];
939
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
942
entry = s->ioredtbl[i];
943
if (!(entry & APIC_LVT_MASKED)) {
944
trig_mode = ((entry >> 15) & 1);
946
dest_mode = (entry >> 11) & 1;
947
delivery_mode = (entry >> 8) & 7;
948
polarity = (entry >> 13) & 1;
949
if (trig_mode == APIC_TRIGGER_EDGE)
951
if (delivery_mode == APIC_DM_EXTINT)
952
vector = pic_read_irq(isa_pic);
954
vector = entry & 0xff;
956
apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
957
apic_bus_deliver(deliver_bitmask, delivery_mode,
958
vector, polarity, trig_mode);
964
void ioapic_set_irq(void *opaque, int vector, int level)
966
IOAPICState *s = opaque;
968
/* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
969
* to GSI 2. GSI maps to ioapic 1-1. This is not
970
* the cleanest way of doing it but it should work. */
975
if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
976
uint32_t mask = 1 << vector;
977
uint64_t entry = s->ioredtbl[vector];
979
if ((entry >> 15) & 1) {
980
/* level triggered */
997
static uint32_t ioapic_mem_readl(void *opaque, target_phys_addr_t addr)
999
IOAPICState *s = opaque;
1006
} else if (addr == 0x10) {
1007
switch (s->ioregsel) {
1012
val = 0x11 | ((IOAPIC_NUM_PINS - 1) << 16); /* version 0x11 */
1018
index = (s->ioregsel - 0x10) >> 1;
1019
if (index >= 0 && index < IOAPIC_NUM_PINS) {
1020
if (s->ioregsel & 1)
1021
val = s->ioredtbl[index] >> 32;
1023
val = s->ioredtbl[index] & 0xffffffff;
1027
printf("I/O APIC read: %08x = %08x\n", s->ioregsel, val);
1033
static void ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1035
IOAPICState *s = opaque;
1042
} else if (addr == 0x10) {
1044
printf("I/O APIC write: %08x = %08x\n", s->ioregsel, val);
1046
switch (s->ioregsel) {
1048
s->id = (val >> 24) & 0xff;
1054
index = (s->ioregsel - 0x10) >> 1;
1055
if (index >= 0 && index < IOAPIC_NUM_PINS) {
1056
if (s->ioregsel & 1) {
1057
s->ioredtbl[index] &= 0xffffffff;
1058
s->ioredtbl[index] |= (uint64_t)val << 32;
1060
s->ioredtbl[index] &= ~0xffffffffULL;
1061
s->ioredtbl[index] |= val;
1069
static void ioapic_save(QEMUFile *f, void *opaque)
1071
IOAPICState *s = opaque;
1074
qemu_put_8s(f, &s->id);
1075
qemu_put_8s(f, &s->ioregsel);
1076
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1077
qemu_put_be64s(f, &s->ioredtbl[i]);
1081
static int ioapic_load(QEMUFile *f, void *opaque, int version_id)
1083
IOAPICState *s = opaque;
1086
if (version_id != 1)
1089
qemu_get_8s(f, &s->id);
1090
qemu_get_8s(f, &s->ioregsel);
1091
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1092
qemu_get_be64s(f, &s->ioredtbl[i]);
1097
static void ioapic_reset(void *opaque)
1099
IOAPICState *s = opaque;
1102
memset(s, 0, sizeof(*s));
1103
for(i = 0; i < IOAPIC_NUM_PINS; i++)
1104
s->ioredtbl[i] = 1 << 16; /* mask LVT */
1107
static CPUReadMemoryFunc *ioapic_mem_read[3] = {
1113
static CPUWriteMemoryFunc *ioapic_mem_write[3] = {
1119
IOAPICState *ioapic_init(void)
1124
s = qemu_mallocz(sizeof(IOAPICState));
1126
s->id = last_apic_id++;
1128
io_memory = cpu_register_io_memory(0, ioapic_mem_read,
1129
ioapic_mem_write, s);
1130
cpu_register_physical_memory(0xfec00000, 0x1000, io_memory);
1132
register_savevm("ioapic", 0, 1, ioapic_save, ioapic_load, s);
1133
qemu_register_reset(ioapic_reset, s);
1002
local_apics[s->idx] = s;