1244ac3afSJan Kiszka /* 2244ac3afSJan Kiszka * IOAPIC emulation logic - common bits of emulated and KVM kernel model 3244ac3afSJan Kiszka * 4244ac3afSJan Kiszka * Copyright (c) 2004-2005 Fabrice Bellard 5244ac3afSJan Kiszka * Copyright (c) 2009 Xiantao Zhang, Intel 6244ac3afSJan Kiszka * Copyright (c) 2011 Jan Kiszka, Siemens AG 7244ac3afSJan Kiszka * 8244ac3afSJan Kiszka * This library is free software; you can redistribute it and/or 9244ac3afSJan Kiszka * modify it under the terms of the GNU Lesser General Public 10244ac3afSJan Kiszka * License as published by the Free Software Foundation; either 11244ac3afSJan Kiszka * version 2 of the License, or (at your option) any later version. 12244ac3afSJan Kiszka * 13244ac3afSJan Kiszka * This library is distributed in the hope that it will be useful, 14244ac3afSJan Kiszka * but WITHOUT ANY WARRANTY; without even the implied warranty of 15244ac3afSJan Kiszka * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16244ac3afSJan Kiszka * Lesser General Public License for more details. 17244ac3afSJan Kiszka * 18244ac3afSJan Kiszka * You should have received a copy of the GNU Lesser General Public 19244ac3afSJan Kiszka * License along with this library; if not, see <http://www.gnu.org/licenses/>. 20244ac3afSJan Kiszka */ 21244ac3afSJan Kiszka 22b6a0aa05SPeter Maydell #include "qemu/osdep.h" 23*da34e65cSMarkus Armbruster #include "qapi/error.h" 24d665d696SPavel Butsykin #include "monitor/monitor.h" 250d09e41aSPaolo Bonzini #include "hw/i386/ioapic.h" 260d09e41aSPaolo Bonzini #include "hw/i386/ioapic_internal.h" 2783c9f4caSPaolo Bonzini #include "hw/sysbus.h" 28244ac3afSJan Kiszka 29db0f8888Sxiaoqiang zhao /* ioapic_no count start from 0 to MAX_IOAPICS, 30db0f8888Sxiaoqiang zhao * remove as static variable from ioapic_common_init. 31db0f8888Sxiaoqiang zhao * now as a global variable, let child to increase the counter 32db0f8888Sxiaoqiang zhao * then we can drop the 'instance_no' argument 33db0f8888Sxiaoqiang zhao * and convert to our QOM's realize function 34db0f8888Sxiaoqiang zhao */ 35db0f8888Sxiaoqiang zhao int ioapic_no; 36db0f8888Sxiaoqiang zhao 37d665d696SPavel Butsykin static void ioapic_irr_dump(Monitor *mon, const char *name, uint32_t bitmap) 38d665d696SPavel Butsykin { 39d665d696SPavel Butsykin int i; 40d665d696SPavel Butsykin 41d665d696SPavel Butsykin monitor_printf(mon, "%-10s ", name); 42d665d696SPavel Butsykin if (bitmap == 0) { 43d665d696SPavel Butsykin monitor_printf(mon, "(none)\n"); 44d665d696SPavel Butsykin return; 45d665d696SPavel Butsykin } 46d665d696SPavel Butsykin for (i = 0; i < IOAPIC_NUM_PINS; i++) { 47d665d696SPavel Butsykin if (bitmap & (1 << i)) { 48d665d696SPavel Butsykin monitor_printf(mon, "%-2u ", i); 49d665d696SPavel Butsykin } 50d665d696SPavel Butsykin } 51d665d696SPavel Butsykin monitor_printf(mon, "\n"); 52d665d696SPavel Butsykin } 53d665d696SPavel Butsykin 54d665d696SPavel Butsykin void ioapic_print_redtbl(Monitor *mon, IOAPICCommonState *s) 55d665d696SPavel Butsykin { 56d665d696SPavel Butsykin static const char *delm_str[] = { 57d665d696SPavel Butsykin "fixed", "lowest", "SMI", "...", "NMI", "INIT", "...", "extINT"}; 58d665d696SPavel Butsykin uint32_t remote_irr = 0; 59d665d696SPavel Butsykin int i; 60d665d696SPavel Butsykin 61d665d696SPavel Butsykin monitor_printf(mon, "ioapic id=0x%02x sel=0x%02x", s->id, s->ioregsel); 62d665d696SPavel Butsykin if (s->ioregsel) { 63d665d696SPavel Butsykin monitor_printf(mon, " (redir[%u])\n", 64d665d696SPavel Butsykin (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1); 65d665d696SPavel Butsykin } else { 66d665d696SPavel Butsykin monitor_printf(mon, "\n"); 67d665d696SPavel Butsykin } 68d665d696SPavel Butsykin for (i = 0; i < IOAPIC_NUM_PINS; i++) { 69d665d696SPavel Butsykin uint64_t entry = s->ioredtbl[i]; 70d665d696SPavel Butsykin uint32_t delm = (uint32_t)((entry & IOAPIC_LVT_DELIV_MODE) >> 71d665d696SPavel Butsykin IOAPIC_LVT_DELIV_MODE_SHIFT); 72d665d696SPavel Butsykin monitor_printf(mon, "pin %-2u 0x%016"PRIx64" dest=%"PRIx64 73d665d696SPavel Butsykin " vec=%-3"PRIu64" %s %-5s %-6s %-6s %s\n", 74d665d696SPavel Butsykin i, entry, 75d665d696SPavel Butsykin (entry >> IOAPIC_LVT_DEST_SHIFT) & 76d665d696SPavel Butsykin (entry & IOAPIC_LVT_DEST_MODE ? 0xff : 0xf), 77d665d696SPavel Butsykin entry & IOAPIC_VECTOR_MASK, 78d665d696SPavel Butsykin entry & IOAPIC_LVT_POLARITY ? "active-lo" : "active-hi", 79d665d696SPavel Butsykin entry & IOAPIC_LVT_TRIGGER_MODE ? "level" : "edge", 80d665d696SPavel Butsykin entry & IOAPIC_LVT_MASKED ? "masked" : "", 81d665d696SPavel Butsykin delm_str[delm], 82d665d696SPavel Butsykin entry & IOAPIC_LVT_DEST_MODE ? "logical" : "physical"); 83d665d696SPavel Butsykin 84d665d696SPavel Butsykin remote_irr |= entry & IOAPIC_LVT_TRIGGER_MODE ? 85d665d696SPavel Butsykin (entry & IOAPIC_LVT_REMOTE_IRR ? (1 << i) : 0) : 0; 86d665d696SPavel Butsykin } 87d665d696SPavel Butsykin ioapic_irr_dump(mon, "IRR", s->irr); 88d665d696SPavel Butsykin ioapic_irr_dump(mon, "Remote IRR", remote_irr); 89d665d696SPavel Butsykin } 90d665d696SPavel Butsykin 91244ac3afSJan Kiszka void ioapic_reset_common(DeviceState *dev) 92244ac3afSJan Kiszka { 93999e12bbSAnthony Liguori IOAPICCommonState *s = IOAPIC_COMMON(dev); 94244ac3afSJan Kiszka int i; 95244ac3afSJan Kiszka 96244ac3afSJan Kiszka s->id = 0; 97244ac3afSJan Kiszka s->ioregsel = 0; 98244ac3afSJan Kiszka s->irr = 0; 99244ac3afSJan Kiszka for (i = 0; i < IOAPIC_NUM_PINS; i++) { 100244ac3afSJan Kiszka s->ioredtbl[i] = 1 << IOAPIC_LVT_MASKED_SHIFT; 101244ac3afSJan Kiszka } 102244ac3afSJan Kiszka } 103244ac3afSJan Kiszka 104244ac3afSJan Kiszka static void ioapic_dispatch_pre_save(void *opaque) 105244ac3afSJan Kiszka { 106999e12bbSAnthony Liguori IOAPICCommonState *s = IOAPIC_COMMON(opaque); 107999e12bbSAnthony Liguori IOAPICCommonClass *info = IOAPIC_COMMON_GET_CLASS(s); 108244ac3afSJan Kiszka 109244ac3afSJan Kiszka if (info->pre_save) { 110244ac3afSJan Kiszka info->pre_save(s); 111244ac3afSJan Kiszka } 112244ac3afSJan Kiszka } 113244ac3afSJan Kiszka 114244ac3afSJan Kiszka static int ioapic_dispatch_post_load(void *opaque, int version_id) 115244ac3afSJan Kiszka { 116999e12bbSAnthony Liguori IOAPICCommonState *s = IOAPIC_COMMON(opaque); 117999e12bbSAnthony Liguori IOAPICCommonClass *info = IOAPIC_COMMON_GET_CLASS(s); 118244ac3afSJan Kiszka 119244ac3afSJan Kiszka if (info->post_load) { 120244ac3afSJan Kiszka info->post_load(s); 121244ac3afSJan Kiszka } 122244ac3afSJan Kiszka return 0; 123244ac3afSJan Kiszka } 124244ac3afSJan Kiszka 125f5ba7523SHu Tao static void ioapic_common_realize(DeviceState *dev, Error **errp) 126244ac3afSJan Kiszka { 127f16a69f7SIgor Mammedov IOAPICCommonState *s = IOAPIC_COMMON(dev); 128999e12bbSAnthony Liguori IOAPICCommonClass *info; 129244ac3afSJan Kiszka 130244ac3afSJan Kiszka if (ioapic_no >= MAX_IOAPICS) { 131f5ba7523SHu Tao error_setg(errp, "Only %d ioapics allowed", MAX_IOAPICS); 132f5ba7523SHu Tao return; 133244ac3afSJan Kiszka } 134244ac3afSJan Kiszka 135999e12bbSAnthony Liguori info = IOAPIC_COMMON_GET_CLASS(s); 136db0f8888Sxiaoqiang zhao info->realize(dev, errp); 137244ac3afSJan Kiszka 138f5ba7523SHu Tao sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->io_memory); 139244ac3afSJan Kiszka ioapic_no++; 140244ac3afSJan Kiszka } 141244ac3afSJan Kiszka 142244ac3afSJan Kiszka static const VMStateDescription vmstate_ioapic_common = { 143244ac3afSJan Kiszka .name = "ioapic", 144244ac3afSJan Kiszka .version_id = 3, 145244ac3afSJan Kiszka .minimum_version_id = 1, 146244ac3afSJan Kiszka .pre_save = ioapic_dispatch_pre_save, 147244ac3afSJan Kiszka .post_load = ioapic_dispatch_post_load, 148244ac3afSJan Kiszka .fields = (VMStateField[]) { 149244ac3afSJan Kiszka VMSTATE_UINT8(id, IOAPICCommonState), 150244ac3afSJan Kiszka VMSTATE_UINT8(ioregsel, IOAPICCommonState), 151244ac3afSJan Kiszka VMSTATE_UNUSED_V(2, 8), /* to account for qemu-kvm's v2 format */ 152244ac3afSJan Kiszka VMSTATE_UINT32_V(irr, IOAPICCommonState, 2), 153244ac3afSJan Kiszka VMSTATE_UINT64_ARRAY(ioredtbl, IOAPICCommonState, IOAPIC_NUM_PINS), 154244ac3afSJan Kiszka VMSTATE_END_OF_LIST() 155244ac3afSJan Kiszka } 156244ac3afSJan Kiszka }; 157244ac3afSJan Kiszka 158999e12bbSAnthony Liguori static void ioapic_common_class_init(ObjectClass *klass, void *data) 159244ac3afSJan Kiszka { 16039bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 161999e12bbSAnthony Liguori 162f5ba7523SHu Tao dc->realize = ioapic_common_realize; 16339bffca2SAnthony Liguori dc->vmsd = &vmstate_ioapic_common; 164244ac3afSJan Kiszka } 165999e12bbSAnthony Liguori 1668c43a6f0SAndreas Färber static const TypeInfo ioapic_common_type = { 167999e12bbSAnthony Liguori .name = TYPE_IOAPIC_COMMON, 168999e12bbSAnthony Liguori .parent = TYPE_SYS_BUS_DEVICE, 169999e12bbSAnthony Liguori .instance_size = sizeof(IOAPICCommonState), 170999e12bbSAnthony Liguori .class_size = sizeof(IOAPICCommonClass), 171999e12bbSAnthony Liguori .class_init = ioapic_common_class_init, 172999e12bbSAnthony Liguori .abstract = true, 173999e12bbSAnthony Liguori }; 174999e12bbSAnthony Liguori 175f9771858Sxiaoqiang zhao static void ioapic_common_register_types(void) 176999e12bbSAnthony Liguori { 177999e12bbSAnthony Liguori type_register_static(&ioapic_common_type); 178999e12bbSAnthony Liguori } 179999e12bbSAnthony Liguori 180f9771858Sxiaoqiang zhao type_init(ioapic_common_register_types) 181