xref: /qemu/hw/intc/ioapic_common.c (revision 06b40d250ecfa1633209c2e431a7a38acfd03a98)
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
1161f3c91aSChetan Pant  * version 2.1 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"
240b8fa32fSMarkus Armbruster #include "qemu/module.h"
25d6454270SMarkus Armbruster #include "migration/vmstate.h"
264a499ad2SPeter Xu #include "hw/intc/intc.h"
277f54640bSBernhard Beschow #include "hw/intc/ioapic.h"
287f54640bSBernhard Beschow #include "hw/intc/ioapic_internal.h"
2983c9f4caSPaolo Bonzini #include "hw/sysbus.h"
30244ac3afSJan Kiszka 
31db0f8888Sxiaoqiang zhao /* ioapic_no count start from 0 to MAX_IOAPICS,
32db0f8888Sxiaoqiang zhao  * remove as static variable from ioapic_common_init.
33db0f8888Sxiaoqiang zhao  * now as a global variable, let child to increase the counter
34db0f8888Sxiaoqiang zhao  * then we can drop the 'instance_no' argument
35db0f8888Sxiaoqiang zhao  * and convert to our QOM's realize function
36db0f8888Sxiaoqiang zhao  */
37db0f8888Sxiaoqiang zhao int ioapic_no;
38db0f8888Sxiaoqiang zhao 
ioapic_stat_update_irq(IOAPICCommonState * s,int irq,int level)39cce5405eSPeter Xu void ioapic_stat_update_irq(IOAPICCommonState *s, int irq, int level)
40cce5405eSPeter Xu {
41cce5405eSPeter Xu     if (level != s->irq_level[irq]) {
42cce5405eSPeter Xu         s->irq_level[irq] = level;
43cce5405eSPeter Xu         if (level == 1) {
44cce5405eSPeter Xu             s->irq_count[irq]++;
45cce5405eSPeter Xu         }
46cce5405eSPeter Xu     }
47cce5405eSPeter Xu }
48cce5405eSPeter Xu 
ioapic_get_statistics(InterruptStatsProvider * obj,uint64_t ** irq_counts,unsigned int * nb_irqs)49cce5405eSPeter Xu static bool ioapic_get_statistics(InterruptStatsProvider *obj,
50cce5405eSPeter Xu                                   uint64_t **irq_counts,
51cce5405eSPeter Xu                                   unsigned int *nb_irqs)
52cce5405eSPeter Xu {
53cce5405eSPeter Xu     IOAPICCommonState *s = IOAPIC_COMMON(obj);
54cce5405eSPeter Xu 
55cce5405eSPeter Xu     *irq_counts = s->irq_count;
56cce5405eSPeter Xu     *nb_irqs = IOAPIC_NUM_PINS;
57cce5405eSPeter Xu 
58cce5405eSPeter Xu     return true;
59cce5405eSPeter Xu }
60cce5405eSPeter Xu 
ioapic_irr_dump(GString * buf,const char * name,uint32_t bitmap)61b2580720SPhilippe Mathieu-Daudé static void ioapic_irr_dump(GString *buf, const char *name, uint32_t bitmap)
62d665d696SPavel Butsykin {
63d665d696SPavel Butsykin     int i;
64d665d696SPavel Butsykin 
65b2580720SPhilippe Mathieu-Daudé     g_string_append_printf(buf, "%-10s ", name);
66d665d696SPavel Butsykin     if (bitmap == 0) {
67b2580720SPhilippe Mathieu-Daudé         g_string_append_printf(buf, "(none)\n");
68d665d696SPavel Butsykin         return;
69d665d696SPavel Butsykin     }
70d665d696SPavel Butsykin     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
71d665d696SPavel Butsykin         if (bitmap & (1 << i)) {
72b2580720SPhilippe Mathieu-Daudé             g_string_append_printf(buf, "%-2u ", i);
73d665d696SPavel Butsykin         }
74d665d696SPavel Butsykin     }
75b2580720SPhilippe Mathieu-Daudé     g_string_append_c(buf, '\n');
76d665d696SPavel Butsykin }
77d665d696SPavel Butsykin 
ioapic_print_redtbl(GString * buf,IOAPICCommonState * s)78b2580720SPhilippe Mathieu-Daudé static void ioapic_print_redtbl(GString *buf, IOAPICCommonState *s)
79d665d696SPavel Butsykin {
80d665d696SPavel Butsykin     static const char *delm_str[] = {
81d665d696SPavel Butsykin         "fixed", "lowest", "SMI", "...", "NMI", "INIT", "...", "extINT"};
82d665d696SPavel Butsykin     uint32_t remote_irr = 0;
83d665d696SPavel Butsykin     int i;
84d665d696SPavel Butsykin 
85b2580720SPhilippe Mathieu-Daudé     g_string_append_printf(buf, "ioapic0: ver=0x%x id=0x%02x sel=0x%02x",
868b77709cSPeter Xu                            s->version, s->id, s->ioregsel);
87d665d696SPavel Butsykin     if (s->ioregsel) {
88b2580720SPhilippe Mathieu-Daudé         g_string_append_printf(buf, " (redir[%u])\n",
89d665d696SPavel Butsykin                                (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1);
90d665d696SPavel Butsykin     } else {
91b2580720SPhilippe Mathieu-Daudé         g_string_append_c(buf, '\n');
92d665d696SPavel Butsykin     }
93d665d696SPavel Butsykin     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
94d665d696SPavel Butsykin         uint64_t entry = s->ioredtbl[i];
95d665d696SPavel Butsykin         uint32_t delm = (uint32_t)((entry & IOAPIC_LVT_DELIV_MODE) >>
96d665d696SPavel Butsykin                                    IOAPIC_LVT_DELIV_MODE_SHIFT);
97b2580720SPhilippe Mathieu-Daudé         g_string_append_printf(buf, "  pin %-2u 0x%016"PRIx64" dest=%"PRIx64
98d665d696SPavel Butsykin                                " vec=%-3"PRIu64" %s %-5s %-6s %-6s %s\n",
99d665d696SPavel Butsykin                                i, entry,
100d665d696SPavel Butsykin                                (entry >> IOAPIC_LVT_DEST_SHIFT) &
101d665d696SPavel Butsykin                                     (entry & IOAPIC_LVT_DEST_MODE ? 0xff : 0xf),
102d665d696SPavel Butsykin                                entry & IOAPIC_VECTOR_MASK,
103b2580720SPhilippe Mathieu-Daudé                                entry & IOAPIC_LVT_POLARITY
104b2580720SPhilippe Mathieu-Daudé                                     ? "active-lo" : "active-hi",
105b2580720SPhilippe Mathieu-Daudé                                entry & IOAPIC_LVT_TRIGGER_MODE
106b2580720SPhilippe Mathieu-Daudé                                     ? "level" : "edge",
107d665d696SPavel Butsykin                                entry & IOAPIC_LVT_MASKED ? "masked" : "",
108d665d696SPavel Butsykin                                delm_str[delm],
109b2580720SPhilippe Mathieu-Daudé                                entry & IOAPIC_LVT_DEST_MODE
110b2580720SPhilippe Mathieu-Daudé                                     ? "logical" : "physical");
111d665d696SPavel Butsykin 
112d665d696SPavel Butsykin         remote_irr |= entry & IOAPIC_LVT_TRIGGER_MODE ?
113d665d696SPavel Butsykin                         (entry & IOAPIC_LVT_REMOTE_IRR ? (1 << i) : 0) : 0;
114d665d696SPavel Butsykin     }
115b2580720SPhilippe Mathieu-Daudé     ioapic_irr_dump(buf, "  IRR", s->irr);
116b2580720SPhilippe Mathieu-Daudé     ioapic_irr_dump(buf, "  Remote IRR", remote_irr);
117d665d696SPavel Butsykin }
118d665d696SPavel Butsykin 
ioapic_reset_common(DeviceState * dev)119244ac3afSJan Kiszka void ioapic_reset_common(DeviceState *dev)
120244ac3afSJan Kiszka {
121999e12bbSAnthony Liguori     IOAPICCommonState *s = IOAPIC_COMMON(dev);
122244ac3afSJan Kiszka     int i;
123244ac3afSJan Kiszka 
124244ac3afSJan Kiszka     s->id = 0;
125244ac3afSJan Kiszka     s->ioregsel = 0;
126244ac3afSJan Kiszka     s->irr = 0;
127244ac3afSJan Kiszka     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
128244ac3afSJan Kiszka         s->ioredtbl[i] = 1 << IOAPIC_LVT_MASKED_SHIFT;
129244ac3afSJan Kiszka     }
130244ac3afSJan Kiszka }
131244ac3afSJan Kiszka 
ioapic_dispatch_pre_save(void * opaque)13244b1ff31SDr. David Alan Gilbert static int ioapic_dispatch_pre_save(void *opaque)
133244ac3afSJan Kiszka {
134999e12bbSAnthony Liguori     IOAPICCommonState *s = IOAPIC_COMMON(opaque);
135999e12bbSAnthony Liguori     IOAPICCommonClass *info = IOAPIC_COMMON_GET_CLASS(s);
136244ac3afSJan Kiszka 
137244ac3afSJan Kiszka     if (info->pre_save) {
138244ac3afSJan Kiszka         info->pre_save(s);
139244ac3afSJan Kiszka     }
14044b1ff31SDr. David Alan Gilbert 
14144b1ff31SDr. David Alan Gilbert     return 0;
142244ac3afSJan Kiszka }
143244ac3afSJan Kiszka 
ioapic_dispatch_post_load(void * opaque,int version_id)144244ac3afSJan Kiszka static int ioapic_dispatch_post_load(void *opaque, int version_id)
145244ac3afSJan Kiszka {
146999e12bbSAnthony Liguori     IOAPICCommonState *s = IOAPIC_COMMON(opaque);
147999e12bbSAnthony Liguori     IOAPICCommonClass *info = IOAPIC_COMMON_GET_CLASS(s);
148244ac3afSJan Kiszka 
149244ac3afSJan Kiszka     if (info->post_load) {
150244ac3afSJan Kiszka         info->post_load(s);
151244ac3afSJan Kiszka     }
152244ac3afSJan Kiszka     return 0;
153244ac3afSJan Kiszka }
154244ac3afSJan Kiszka 
ioapic_common_realize(DeviceState * dev,Error ** errp)155f5ba7523SHu Tao static void ioapic_common_realize(DeviceState *dev, Error **errp)
156244ac3afSJan Kiszka {
1574a9fafb4SZhao Liu     ERRP_GUARD();
158f16a69f7SIgor Mammedov     IOAPICCommonState *s = IOAPIC_COMMON(dev);
159999e12bbSAnthony Liguori     IOAPICCommonClass *info;
160244ac3afSJan Kiszka 
161244ac3afSJan Kiszka     if (ioapic_no >= MAX_IOAPICS) {
162f5ba7523SHu Tao         error_setg(errp, "Only %d ioapics allowed", MAX_IOAPICS);
163f5ba7523SHu Tao         return;
164244ac3afSJan Kiszka     }
165244ac3afSJan Kiszka 
166999e12bbSAnthony Liguori     info = IOAPIC_COMMON_GET_CLASS(s);
167db0f8888Sxiaoqiang zhao     info->realize(dev, errp);
1684a9fafb4SZhao Liu     if (*errp) {
1694a9fafb4SZhao Liu         return;
1704a9fafb4SZhao Liu     }
171244ac3afSJan Kiszka 
172f5ba7523SHu Tao     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->io_memory);
173244ac3afSJan Kiszka     ioapic_no++;
174244ac3afSJan Kiszka }
175244ac3afSJan Kiszka 
ioapic_print_info(InterruptStatsProvider * obj,GString * buf)176b2580720SPhilippe Mathieu-Daudé static void ioapic_print_info(InterruptStatsProvider *obj, GString *buf)
1774a499ad2SPeter Xu {
1784a499ad2SPeter Xu     IOAPICCommonState *s = IOAPIC_COMMON(obj);
1794a499ad2SPeter Xu 
1804a499ad2SPeter Xu     ioapic_dispatch_pre_save(s);
181b2580720SPhilippe Mathieu-Daudé     ioapic_print_redtbl(buf, s);
1824a499ad2SPeter Xu }
1834a499ad2SPeter Xu 
184244ac3afSJan Kiszka static const VMStateDescription vmstate_ioapic_common = {
185244ac3afSJan Kiszka     .name = "ioapic",
186244ac3afSJan Kiszka     .version_id = 3,
187244ac3afSJan Kiszka     .minimum_version_id = 1,
188244ac3afSJan Kiszka     .pre_save = ioapic_dispatch_pre_save,
189244ac3afSJan Kiszka     .post_load = ioapic_dispatch_post_load,
19045b1f81dSRichard Henderson     .fields = (const VMStateField[]) {
191244ac3afSJan Kiszka         VMSTATE_UINT8(id, IOAPICCommonState),
192244ac3afSJan Kiszka         VMSTATE_UINT8(ioregsel, IOAPICCommonState),
193244ac3afSJan Kiszka         VMSTATE_UNUSED_V(2, 8), /* to account for qemu-kvm's v2 format */
194244ac3afSJan Kiszka         VMSTATE_UINT32_V(irr, IOAPICCommonState, 2),
195244ac3afSJan Kiszka         VMSTATE_UINT64_ARRAY(ioredtbl, IOAPICCommonState, IOAPIC_NUM_PINS),
196244ac3afSJan Kiszka         VMSTATE_END_OF_LIST()
197244ac3afSJan Kiszka     }
198244ac3afSJan Kiszka };
199244ac3afSJan Kiszka 
ioapic_common_class_init(ObjectClass * klass,const void * data)20012d1a768SPhilippe Mathieu-Daudé static void ioapic_common_class_init(ObjectClass *klass, const void *data)
201244ac3afSJan Kiszka {
20239bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
2034a499ad2SPeter Xu     InterruptStatsProviderClass *ic = INTERRUPT_STATS_PROVIDER_CLASS(klass);
204999e12bbSAnthony Liguori 
205f5ba7523SHu Tao     dc->realize = ioapic_common_realize;
20639bffca2SAnthony Liguori     dc->vmsd = &vmstate_ioapic_common;
2074a499ad2SPeter Xu     ic->print_info = ioapic_print_info;
208cce5405eSPeter Xu     ic->get_statistics = ioapic_get_statistics;
209244ac3afSJan Kiszka }
210999e12bbSAnthony Liguori 
2118c43a6f0SAndreas Färber static const TypeInfo ioapic_common_type = {
212999e12bbSAnthony Liguori     .name = TYPE_IOAPIC_COMMON,
213999e12bbSAnthony Liguori     .parent = TYPE_SYS_BUS_DEVICE,
214999e12bbSAnthony Liguori     .instance_size = sizeof(IOAPICCommonState),
215999e12bbSAnthony Liguori     .class_size = sizeof(IOAPICCommonClass),
216999e12bbSAnthony Liguori     .class_init = ioapic_common_class_init,
217999e12bbSAnthony Liguori     .abstract = true,
218*2cd09e47SPhilippe Mathieu-Daudé     .interfaces = (const InterfaceInfo[]) {
2194a499ad2SPeter Xu         { TYPE_INTERRUPT_STATS_PROVIDER },
2204a499ad2SPeter Xu         { }
2214a499ad2SPeter Xu     },
222999e12bbSAnthony Liguori };
223999e12bbSAnthony Liguori 
ioapic_common_register_types(void)224f9771858Sxiaoqiang zhao static void ioapic_common_register_types(void)
225999e12bbSAnthony Liguori {
226999e12bbSAnthony Liguori     type_register_static(&ioapic_common_type);
227999e12bbSAnthony Liguori }
228999e12bbSAnthony Liguori 
229f9771858Sxiaoqiang zhao type_init(ioapic_common_register_types)
230