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" 23da34e65cSMarkus 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 61*8b77709cSPeter Xu monitor_printf(mon, "ioapic ver=0x%x id=0x%02x sel=0x%02x", 62*8b77709cSPeter Xu s->version, s->id, s->ioregsel); 63d665d696SPavel Butsykin if (s->ioregsel) { 64d665d696SPavel Butsykin monitor_printf(mon, " (redir[%u])\n", 65d665d696SPavel Butsykin (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1); 66d665d696SPavel Butsykin } else { 67d665d696SPavel Butsykin monitor_printf(mon, "\n"); 68d665d696SPavel Butsykin } 69d665d696SPavel Butsykin for (i = 0; i < IOAPIC_NUM_PINS; i++) { 70d665d696SPavel Butsykin uint64_t entry = s->ioredtbl[i]; 71d665d696SPavel Butsykin uint32_t delm = (uint32_t)((entry & IOAPIC_LVT_DELIV_MODE) >> 72d665d696SPavel Butsykin IOAPIC_LVT_DELIV_MODE_SHIFT); 73d665d696SPavel Butsykin monitor_printf(mon, "pin %-2u 0x%016"PRIx64" dest=%"PRIx64 74d665d696SPavel Butsykin " vec=%-3"PRIu64" %s %-5s %-6s %-6s %s\n", 75d665d696SPavel Butsykin i, entry, 76d665d696SPavel Butsykin (entry >> IOAPIC_LVT_DEST_SHIFT) & 77d665d696SPavel Butsykin (entry & IOAPIC_LVT_DEST_MODE ? 0xff : 0xf), 78d665d696SPavel Butsykin entry & IOAPIC_VECTOR_MASK, 79d665d696SPavel Butsykin entry & IOAPIC_LVT_POLARITY ? "active-lo" : "active-hi", 80d665d696SPavel Butsykin entry & IOAPIC_LVT_TRIGGER_MODE ? "level" : "edge", 81d665d696SPavel Butsykin entry & IOAPIC_LVT_MASKED ? "masked" : "", 82d665d696SPavel Butsykin delm_str[delm], 83d665d696SPavel Butsykin entry & IOAPIC_LVT_DEST_MODE ? "logical" : "physical"); 84d665d696SPavel Butsykin 85d665d696SPavel Butsykin remote_irr |= entry & IOAPIC_LVT_TRIGGER_MODE ? 86d665d696SPavel Butsykin (entry & IOAPIC_LVT_REMOTE_IRR ? (1 << i) : 0) : 0; 87d665d696SPavel Butsykin } 88d665d696SPavel Butsykin ioapic_irr_dump(mon, "IRR", s->irr); 89d665d696SPavel Butsykin ioapic_irr_dump(mon, "Remote IRR", remote_irr); 90d665d696SPavel Butsykin } 91d665d696SPavel Butsykin 92244ac3afSJan Kiszka void ioapic_reset_common(DeviceState *dev) 93244ac3afSJan Kiszka { 94999e12bbSAnthony Liguori IOAPICCommonState *s = IOAPIC_COMMON(dev); 95244ac3afSJan Kiszka int i; 96244ac3afSJan Kiszka 97244ac3afSJan Kiszka s->id = 0; 98244ac3afSJan Kiszka s->ioregsel = 0; 99244ac3afSJan Kiszka s->irr = 0; 100244ac3afSJan Kiszka for (i = 0; i < IOAPIC_NUM_PINS; i++) { 101244ac3afSJan Kiszka s->ioredtbl[i] = 1 << IOAPIC_LVT_MASKED_SHIFT; 102244ac3afSJan Kiszka } 103244ac3afSJan Kiszka } 104244ac3afSJan Kiszka 105244ac3afSJan Kiszka static void ioapic_dispatch_pre_save(void *opaque) 106244ac3afSJan Kiszka { 107999e12bbSAnthony Liguori IOAPICCommonState *s = IOAPIC_COMMON(opaque); 108999e12bbSAnthony Liguori IOAPICCommonClass *info = IOAPIC_COMMON_GET_CLASS(s); 109244ac3afSJan Kiszka 110244ac3afSJan Kiszka if (info->pre_save) { 111244ac3afSJan Kiszka info->pre_save(s); 112244ac3afSJan Kiszka } 113244ac3afSJan Kiszka } 114244ac3afSJan Kiszka 115244ac3afSJan Kiszka static int ioapic_dispatch_post_load(void *opaque, int version_id) 116244ac3afSJan Kiszka { 117999e12bbSAnthony Liguori IOAPICCommonState *s = IOAPIC_COMMON(opaque); 118999e12bbSAnthony Liguori IOAPICCommonClass *info = IOAPIC_COMMON_GET_CLASS(s); 119244ac3afSJan Kiszka 120244ac3afSJan Kiszka if (info->post_load) { 121244ac3afSJan Kiszka info->post_load(s); 122244ac3afSJan Kiszka } 123244ac3afSJan Kiszka return 0; 124244ac3afSJan Kiszka } 125244ac3afSJan Kiszka 126f5ba7523SHu Tao static void ioapic_common_realize(DeviceState *dev, Error **errp) 127244ac3afSJan Kiszka { 128f16a69f7SIgor Mammedov IOAPICCommonState *s = IOAPIC_COMMON(dev); 129999e12bbSAnthony Liguori IOAPICCommonClass *info; 130244ac3afSJan Kiszka 131244ac3afSJan Kiszka if (ioapic_no >= MAX_IOAPICS) { 132f5ba7523SHu Tao error_setg(errp, "Only %d ioapics allowed", MAX_IOAPICS); 133f5ba7523SHu Tao return; 134244ac3afSJan Kiszka } 135244ac3afSJan Kiszka 136999e12bbSAnthony Liguori info = IOAPIC_COMMON_GET_CLASS(s); 137db0f8888Sxiaoqiang zhao info->realize(dev, errp); 138244ac3afSJan Kiszka 139f5ba7523SHu Tao sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->io_memory); 140244ac3afSJan Kiszka ioapic_no++; 141244ac3afSJan Kiszka } 142244ac3afSJan Kiszka 143244ac3afSJan Kiszka static const VMStateDescription vmstate_ioapic_common = { 144244ac3afSJan Kiszka .name = "ioapic", 145244ac3afSJan Kiszka .version_id = 3, 146244ac3afSJan Kiszka .minimum_version_id = 1, 147244ac3afSJan Kiszka .pre_save = ioapic_dispatch_pre_save, 148244ac3afSJan Kiszka .post_load = ioapic_dispatch_post_load, 149244ac3afSJan Kiszka .fields = (VMStateField[]) { 150244ac3afSJan Kiszka VMSTATE_UINT8(id, IOAPICCommonState), 151244ac3afSJan Kiszka VMSTATE_UINT8(ioregsel, IOAPICCommonState), 152244ac3afSJan Kiszka VMSTATE_UNUSED_V(2, 8), /* to account for qemu-kvm's v2 format */ 153244ac3afSJan Kiszka VMSTATE_UINT32_V(irr, IOAPICCommonState, 2), 154244ac3afSJan Kiszka VMSTATE_UINT64_ARRAY(ioredtbl, IOAPICCommonState, IOAPIC_NUM_PINS), 155244ac3afSJan Kiszka VMSTATE_END_OF_LIST() 156244ac3afSJan Kiszka } 157244ac3afSJan Kiszka }; 158244ac3afSJan Kiszka 159999e12bbSAnthony Liguori static void ioapic_common_class_init(ObjectClass *klass, void *data) 160244ac3afSJan Kiszka { 16139bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 162999e12bbSAnthony Liguori 163f5ba7523SHu Tao dc->realize = ioapic_common_realize; 16439bffca2SAnthony Liguori dc->vmsd = &vmstate_ioapic_common; 165244ac3afSJan Kiszka } 166999e12bbSAnthony Liguori 1678c43a6f0SAndreas Färber static const TypeInfo ioapic_common_type = { 168999e12bbSAnthony Liguori .name = TYPE_IOAPIC_COMMON, 169999e12bbSAnthony Liguori .parent = TYPE_SYS_BUS_DEVICE, 170999e12bbSAnthony Liguori .instance_size = sizeof(IOAPICCommonState), 171999e12bbSAnthony Liguori .class_size = sizeof(IOAPICCommonClass), 172999e12bbSAnthony Liguori .class_init = ioapic_common_class_init, 173999e12bbSAnthony Liguori .abstract = true, 174999e12bbSAnthony Liguori }; 175999e12bbSAnthony Liguori 176f9771858Sxiaoqiang zhao static void ioapic_common_register_types(void) 177999e12bbSAnthony Liguori { 178999e12bbSAnthony Liguori type_register_static(&ioapic_common_type); 179999e12bbSAnthony Liguori } 180999e12bbSAnthony Liguori 181f9771858Sxiaoqiang zhao type_init(ioapic_common_register_types) 182