2009-03-12 23:25:12 +03:00
|
|
|
/*
|
|
|
|
* ioapic.c IOAPIC emulation logic
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004-2005 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Split the ioapic logic from apic.c
|
|
|
|
* Xiantao Zhang <xiantao.zhang@intel.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-07-17 00:47:01 +04:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2009-03-12 23:25:12 +03:00
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:03 +03:00
|
|
|
#include "qemu/osdep.h"
|
2015-09-22 16:18:21 +03:00
|
|
|
#include "monitor/monitor.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/hw.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/i386/pc.h"
|
2015-12-04 13:04:13 +03:00
|
|
|
#include "hw/i386/apic.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/i386/ioapic.h"
|
|
|
|
#include "hw/i386/ioapic_internal.h"
|
2015-12-17 19:16:08 +03:00
|
|
|
#include "include/hw/pci/msi.h"
|
|
|
|
#include "sysemu/kvm.h"
|
2009-03-12 23:25:12 +03:00
|
|
|
|
|
|
|
//#define DEBUG_IOAPIC
|
|
|
|
|
2010-05-31 22:59:45 +04:00
|
|
|
#ifdef DEBUG_IOAPIC
|
|
|
|
#define DPRINTF(fmt, ...) \
|
|
|
|
do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
|
|
|
|
#else
|
|
|
|
#define DPRINTF(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
2015-12-17 19:16:08 +03:00
|
|
|
#define APIC_DELIVERY_MODE_SHIFT 8
|
|
|
|
#define APIC_POLARITY_SHIFT 14
|
|
|
|
#define APIC_TRIG_MODE_SHIFT 15
|
|
|
|
|
2011-10-16 21:38:22 +04:00
|
|
|
static IOAPICCommonState *ioapics[MAX_IOAPICS];
|
2011-02-04 00:54:11 +03:00
|
|
|
|
2013-11-05 14:16:05 +04:00
|
|
|
/* global variable from ioapic_common.c */
|
|
|
|
extern int ioapic_no;
|
|
|
|
|
2011-10-16 21:38:22 +04:00
|
|
|
static void ioapic_service(IOAPICCommonState *s)
|
2009-03-12 23:25:12 +03:00
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
uint8_t trig_mode;
|
|
|
|
uint8_t vector;
|
|
|
|
uint8_t delivery_mode;
|
|
|
|
uint32_t mask;
|
|
|
|
uint64_t entry;
|
|
|
|
uint8_t dest;
|
|
|
|
uint8_t dest_mode;
|
|
|
|
|
|
|
|
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
|
|
|
|
mask = 1 << i;
|
|
|
|
if (s->irr & mask) {
|
2015-12-17 19:16:08 +03:00
|
|
|
int coalesce = 0;
|
|
|
|
|
2009-03-12 23:25:12 +03:00
|
|
|
entry = s->ioredtbl[i];
|
|
|
|
if (!(entry & IOAPIC_LVT_MASKED)) {
|
2011-02-04 00:54:14 +03:00
|
|
|
trig_mode = ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1);
|
|
|
|
dest = entry >> IOAPIC_LVT_DEST_SHIFT;
|
|
|
|
dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
|
|
|
|
delivery_mode =
|
|
|
|
(entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) & IOAPIC_DM_MASK;
|
2011-02-04 00:54:11 +03:00
|
|
|
if (trig_mode == IOAPIC_TRIGGER_EDGE) {
|
2009-03-12 23:25:12 +03:00
|
|
|
s->irr &= ~mask;
|
2011-02-04 00:54:11 +03:00
|
|
|
} else {
|
2015-12-17 19:16:08 +03:00
|
|
|
coalesce = s->ioredtbl[i] & IOAPIC_LVT_REMOTE_IRR;
|
2011-02-04 00:54:11 +03:00
|
|
|
s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
|
|
|
|
}
|
2011-02-04 00:54:14 +03:00
|
|
|
if (delivery_mode == IOAPIC_DM_EXTINT) {
|
2009-03-12 23:25:12 +03:00
|
|
|
vector = pic_read_irq(isa_pic);
|
2011-02-04 00:54:14 +03:00
|
|
|
} else {
|
|
|
|
vector = entry & IOAPIC_VECTOR_MASK;
|
|
|
|
}
|
2015-12-17 19:16:08 +03:00
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
if (kvm_irqchip_is_split()) {
|
|
|
|
if (trig_mode == IOAPIC_TRIGGER_EDGE) {
|
|
|
|
kvm_set_irq(kvm_state, i, 1);
|
|
|
|
kvm_set_irq(kvm_state, i, 0);
|
|
|
|
} else {
|
|
|
|
if (!coalesce) {
|
|
|
|
kvm_set_irq(kvm_state, i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)coalesce;
|
|
|
|
#endif
|
|
|
|
apic_deliver_irq(dest, dest_mode, delivery_mode, vector,
|
|
|
|
trig_mode);
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 20:32:47 +04:00
|
|
|
static void ioapic_set_irq(void *opaque, int vector, int level)
|
2009-03-12 23:25:12 +03:00
|
|
|
{
|
2011-10-16 21:38:22 +04:00
|
|
|
IOAPICCommonState *s = opaque;
|
2009-03-12 23:25:12 +03:00
|
|
|
|
|
|
|
/* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
|
|
|
|
* to GSI 2. GSI maps to ioapic 1-1. This is not
|
|
|
|
* the cleanest way of doing it but it should work. */
|
|
|
|
|
2011-02-04 00:54:14 +03:00
|
|
|
DPRINTF("%s: %s vec %x\n", __func__, level ? "raise" : "lower", vector);
|
|
|
|
if (vector == 0) {
|
2009-03-12 23:25:12 +03:00
|
|
|
vector = 2;
|
2011-02-04 00:54:14 +03:00
|
|
|
}
|
2009-03-12 23:25:12 +03:00
|
|
|
if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
|
|
|
|
uint32_t mask = 1 << vector;
|
|
|
|
uint64_t entry = s->ioredtbl[vector];
|
|
|
|
|
2011-02-04 00:54:14 +03:00
|
|
|
if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
|
|
|
|
IOAPIC_TRIGGER_LEVEL) {
|
2009-03-12 23:25:12 +03:00
|
|
|
/* level triggered */
|
|
|
|
if (level) {
|
|
|
|
s->irr |= mask;
|
2015-07-30 11:19:24 +03:00
|
|
|
if (!(entry & IOAPIC_LVT_REMOTE_IRR)) {
|
|
|
|
ioapic_service(s);
|
|
|
|
}
|
2009-03-12 23:25:12 +03:00
|
|
|
} else {
|
|
|
|
s->irr &= ~mask;
|
|
|
|
}
|
|
|
|
} else {
|
2011-04-09 15:18:59 +04:00
|
|
|
/* According to the 82093AA manual, we must ignore edge requests
|
|
|
|
* if the input pin is masked. */
|
|
|
|
if (level && !(entry & IOAPIC_LVT_MASKED)) {
|
2009-03-12 23:25:12 +03:00
|
|
|
s->irr |= mask;
|
|
|
|
ioapic_service(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 19:16:08 +03:00
|
|
|
static void ioapic_update_kvm_routes(IOAPICCommonState *s)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (kvm_irqchip_is_split()) {
|
|
|
|
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
|
|
|
|
uint64_t entry = s->ioredtbl[i];
|
|
|
|
uint8_t trig_mode;
|
|
|
|
uint8_t delivery_mode;
|
|
|
|
uint8_t dest;
|
|
|
|
uint8_t dest_mode;
|
|
|
|
uint64_t pin_polarity;
|
|
|
|
MSIMessage msg;
|
|
|
|
|
|
|
|
trig_mode = ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1);
|
|
|
|
dest = entry >> IOAPIC_LVT_DEST_SHIFT;
|
|
|
|
dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
|
|
|
|
pin_polarity = (entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1;
|
|
|
|
delivery_mode =
|
|
|
|
(entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) & IOAPIC_DM_MASK;
|
|
|
|
|
|
|
|
msg.address = APIC_DEFAULT_ADDRESS;
|
|
|
|
msg.address |= dest_mode << 2;
|
|
|
|
msg.address |= dest << 12;
|
|
|
|
|
|
|
|
msg.data = entry & IOAPIC_VECTOR_MASK;
|
|
|
|
msg.data |= delivery_mode << APIC_DELIVERY_MODE_SHIFT;
|
|
|
|
msg.data |= pin_polarity << APIC_POLARITY_SHIFT;
|
|
|
|
msg.data |= trig_mode << APIC_TRIG_MODE_SHIFT;
|
|
|
|
|
|
|
|
kvm_irqchip_update_msi_route(kvm_state, i, msg, NULL);
|
|
|
|
}
|
|
|
|
kvm_irqchip_commit_routes(kvm_state);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-02-04 00:54:11 +03:00
|
|
|
void ioapic_eoi_broadcast(int vector)
|
|
|
|
{
|
2011-10-16 21:38:22 +04:00
|
|
|
IOAPICCommonState *s;
|
2011-02-04 00:54:11 +03:00
|
|
|
uint64_t entry;
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_IOAPICS; i++) {
|
|
|
|
s = ioapics[i];
|
|
|
|
if (!s) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (n = 0; n < IOAPIC_NUM_PINS; n++) {
|
|
|
|
entry = s->ioredtbl[n];
|
2011-02-04 00:54:14 +03:00
|
|
|
if ((entry & IOAPIC_LVT_REMOTE_IRR)
|
|
|
|
&& (entry & IOAPIC_VECTOR_MASK) == vector) {
|
2011-02-04 00:54:11 +03:00
|
|
|
s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
|
|
|
|
if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
|
|
|
|
ioapic_service(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 16:18:21 +03:00
|
|
|
void ioapic_dump_state(Monitor *mon, const QDict *qdict)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_IOAPICS; i++) {
|
|
|
|
if (ioapics[i] != 0) {
|
|
|
|
ioapic_print_redtbl(mon, ioapics[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 15:11:27 +04:00
|
|
|
static uint64_t
|
2012-10-23 14:30:10 +04:00
|
|
|
ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
|
2009-03-12 23:25:12 +03:00
|
|
|
{
|
2011-10-16 21:38:22 +04:00
|
|
|
IOAPICCommonState *s = opaque;
|
2009-03-12 23:25:12 +03:00
|
|
|
int index;
|
|
|
|
uint32_t val = 0;
|
|
|
|
|
2011-02-04 00:54:14 +03:00
|
|
|
switch (addr & 0xff) {
|
|
|
|
case IOAPIC_IOREGSEL:
|
2009-03-12 23:25:12 +03:00
|
|
|
val = s->ioregsel;
|
2011-02-04 00:54:14 +03:00
|
|
|
break;
|
|
|
|
case IOAPIC_IOWIN:
|
2011-10-17 15:11:29 +04:00
|
|
|
if (size != 4) {
|
|
|
|
break;
|
|
|
|
}
|
2009-03-12 23:25:12 +03:00
|
|
|
switch (s->ioregsel) {
|
2011-02-04 00:54:14 +03:00
|
|
|
case IOAPIC_REG_ID:
|
2015-07-30 11:21:00 +03:00
|
|
|
case IOAPIC_REG_ARB:
|
2011-02-04 00:54:14 +03:00
|
|
|
val = s->id << IOAPIC_ID_SHIFT;
|
|
|
|
break;
|
|
|
|
case IOAPIC_REG_VER:
|
|
|
|
val = IOAPIC_VERSION |
|
|
|
|
((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
|
|
|
|
if (index >= 0 && index < IOAPIC_NUM_PINS) {
|
|
|
|
if (s->ioregsel & 1) {
|
|
|
|
val = s->ioredtbl[index] >> 32;
|
|
|
|
} else {
|
|
|
|
val = s->ioredtbl[index] & 0xffffffff;
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
2011-02-04 00:54:14 +03:00
|
|
|
}
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
2010-05-31 22:59:45 +04:00
|
|
|
DPRINTF("read: %08x = %08x\n", s->ioregsel, val);
|
2011-02-04 00:54:14 +03:00
|
|
|
break;
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2016-05-10 13:21:22 +03:00
|
|
|
/*
|
|
|
|
* This is to satisfy the hack in Linux kernel. One hack of it is to
|
|
|
|
* simulate clearing the Remote IRR bit of IOAPIC entry using the
|
|
|
|
* following:
|
|
|
|
*
|
|
|
|
* "For IO-APIC's with EOI register, we use that to do an explicit EOI.
|
|
|
|
* Otherwise, we simulate the EOI message manually by changing the trigger
|
|
|
|
* mode to edge and then back to level, with RTE being masked during
|
|
|
|
* this."
|
|
|
|
*
|
|
|
|
* (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
|
|
|
|
*
|
|
|
|
* This is based on the assumption that, Remote IRR bit will be
|
|
|
|
* cleared by IOAPIC hardware when configured as edge-triggered
|
|
|
|
* interrupts.
|
|
|
|
*
|
|
|
|
* Without this, level-triggered interrupts in IR mode might fail to
|
|
|
|
* work correctly.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
ioapic_fix_edge_remote_irr(uint64_t *entry)
|
|
|
|
{
|
|
|
|
if (!(*entry & IOAPIC_LVT_TRIGGER_MODE)) {
|
|
|
|
/* Edge-triggered interrupts, make sure remote IRR is zero */
|
|
|
|
*entry &= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 00:54:14 +03:00
|
|
|
static void
|
2012-10-23 14:30:10 +04:00
|
|
|
ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
|
2011-10-17 15:11:27 +04:00
|
|
|
unsigned int size)
|
2009-03-12 23:25:12 +03:00
|
|
|
{
|
2011-10-16 21:38:22 +04:00
|
|
|
IOAPICCommonState *s = opaque;
|
2009-03-12 23:25:12 +03:00
|
|
|
int index;
|
|
|
|
|
2011-02-04 00:54:14 +03:00
|
|
|
switch (addr & 0xff) {
|
|
|
|
case IOAPIC_IOREGSEL:
|
2009-03-12 23:25:12 +03:00
|
|
|
s->ioregsel = val;
|
2011-02-04 00:54:14 +03:00
|
|
|
break;
|
|
|
|
case IOAPIC_IOWIN:
|
2011-10-17 15:11:29 +04:00
|
|
|
if (size != 4) {
|
|
|
|
break;
|
|
|
|
}
|
2012-03-19 07:19:57 +04:00
|
|
|
DPRINTF("write: %08x = %08" PRIx64 "\n", s->ioregsel, val);
|
2009-03-12 23:25:12 +03:00
|
|
|
switch (s->ioregsel) {
|
2011-02-04 00:54:14 +03:00
|
|
|
case IOAPIC_REG_ID:
|
|
|
|
s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
|
|
|
|
break;
|
|
|
|
case IOAPIC_REG_VER:
|
|
|
|
case IOAPIC_REG_ARB:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
|
|
|
|
if (index >= 0 && index < IOAPIC_NUM_PINS) {
|
2016-05-10 13:21:21 +03:00
|
|
|
uint64_t ro_bits = s->ioredtbl[index] & IOAPIC_RO_BITS;
|
2011-02-04 00:54:14 +03:00
|
|
|
if (s->ioregsel & 1) {
|
|
|
|
s->ioredtbl[index] &= 0xffffffff;
|
|
|
|
s->ioredtbl[index] |= (uint64_t)val << 32;
|
|
|
|
} else {
|
|
|
|
s->ioredtbl[index] &= ~0xffffffffULL;
|
|
|
|
s->ioredtbl[index] |= val;
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
2016-05-10 13:21:21 +03:00
|
|
|
/* restore RO bits */
|
|
|
|
s->ioredtbl[index] &= IOAPIC_RW_BITS;
|
|
|
|
s->ioredtbl[index] |= ro_bits;
|
2016-05-10 13:21:22 +03:00
|
|
|
ioapic_fix_edge_remote_irr(&s->ioredtbl[index]);
|
2011-02-04 00:54:14 +03:00
|
|
|
ioapic_service(s);
|
|
|
|
}
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
2011-02-04 00:54:14 +03:00
|
|
|
break;
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
2015-12-17 19:16:08 +03:00
|
|
|
|
|
|
|
ioapic_update_kvm_routes(s);
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 15:11:27 +04:00
|
|
|
static const MemoryRegionOps ioapic_io_ops = {
|
|
|
|
.read = ioapic_mem_read,
|
|
|
|
.write = ioapic_mem_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2009-03-12 23:25:12 +03:00
|
|
|
};
|
|
|
|
|
2013-11-05 14:16:05 +04:00
|
|
|
static void ioapic_realize(DeviceState *dev, Error **errp)
|
2009-03-12 23:25:12 +03:00
|
|
|
{
|
2013-11-05 14:16:05 +04:00
|
|
|
IOAPICCommonState *s = IOAPIC_COMMON(dev);
|
2013-11-05 14:16:04 +04:00
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
|
|
|
|
"ioapic", 0x1000);
|
2009-03-12 23:25:12 +03:00
|
|
|
|
2013-11-05 14:16:04 +04:00
|
|
|
qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
|
2011-02-04 00:54:11 +03:00
|
|
|
|
2013-11-05 14:16:05 +04:00
|
|
|
ioapics[ioapic_no] = s;
|
2009-03-12 23:25:12 +03:00
|
|
|
}
|
2010-06-19 11:41:43 +04:00
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static void ioapic_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2013-11-05 14:16:05 +04:00
|
|
|
k->realize = ioapic_realize;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->reset = ioapic_reset_common;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo ioapic_info = {
|
2011-12-08 07:34:16 +04:00
|
|
|
.name = "ioapic",
|
|
|
|
.parent = TYPE_IOAPIC_COMMON,
|
|
|
|
.instance_size = sizeof(IOAPICCommonState),
|
|
|
|
.class_init = ioapic_class_init,
|
2010-06-19 11:41:43 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void ioapic_register_types(void)
|
2010-06-19 11:41:43 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&ioapic_info);
|
2010-06-19 11:41:43 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(ioapic_register_types)
|