xref: /qemu/hw/intc/loongarch_extioi_common.c (revision 8b4b668f6a3661885fcabcedcf812930d5577f7e)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Loongson extioi interrupt controller emulation
4  * Copyright (C) 2024 Loongson Technology Corporation Limited
5  */
6 #include "qemu/osdep.h"
7 #include "qemu/module.h"
8 #include "qapi/error.h"
9 #include "hw/qdev-properties.h"
10 #include "hw/intc/loongarch_extioi_common.h"
11 #include "migration/vmstate.h"
12 
13 static void loongarch_extioi_common_realize(DeviceState *dev, Error **errp)
14 {
15     LoongArchExtIOICommonState *s = (LoongArchExtIOICommonState *)dev;
16     MachineState *machine = MACHINE(qdev_get_machine());
17     MachineClass *mc = MACHINE_GET_CLASS(machine);
18     const CPUArchIdList *id_list;
19     int i, pin;
20 
21     assert(mc->possible_cpu_arch_ids);
22     id_list = mc->possible_cpu_arch_ids(machine);
23     s->num_cpu = id_list->len;
24     s->cpu = g_new0(ExtIOICore, s->num_cpu);
25     if (s->cpu == NULL) {
26         error_setg(errp, "Memory allocation for ExtIOICore faile");
27         return;
28     }
29 
30     for (i = 0; i < s->num_cpu; i++) {
31         s->cpu[i].arch_id = id_list->cpus[i].arch_id;
32         s->cpu[i].cpu = CPU(id_list->cpus[i].cpu);
33 
34         for (pin = 0; pin < LS3A_INTC_IP; pin++) {
35             qdev_init_gpio_out(dev, &s->cpu[i].parent_irq[pin], 1);
36         }
37     }
38 }
39 
40 static int loongarch_extioi_common_pre_save(void *opaque)
41 {
42     LoongArchExtIOICommonState *s = (LoongArchExtIOICommonState *)opaque;
43     LoongArchExtIOICommonClass *lecc = LOONGARCH_EXTIOI_COMMON_GET_CLASS(s);
44 
45     if (lecc->pre_save) {
46         return lecc->pre_save(s);
47     }
48 
49     return 0;
50 }
51 
52 static int loongarch_extioi_common_post_load(void *opaque, int version_id)
53 {
54     LoongArchExtIOICommonState *s = (LoongArchExtIOICommonState *)opaque;
55     LoongArchExtIOICommonClass *lecc = LOONGARCH_EXTIOI_COMMON_GET_CLASS(s);
56 
57     if (lecc->post_load) {
58         return lecc->post_load(s, version_id);
59     }
60 
61     return 0;
62 }
63 
64 static const VMStateDescription vmstate_extioi_core = {
65     .name = "extioi-core",
66     .version_id = 1,
67     .minimum_version_id = 1,
68     .fields = (const VMStateField[]) {
69         VMSTATE_UINT32_ARRAY(coreisr, ExtIOICore, EXTIOI_IRQS_GROUP_COUNT),
70         VMSTATE_END_OF_LIST()
71     }
72 };
73 
74 static const VMStateDescription vmstate_loongarch_extioi = {
75     .name = "loongarch.extioi",
76     .version_id = 3,
77     .minimum_version_id = 3,
78     .pre_save  = loongarch_extioi_common_pre_save,
79     .post_load = loongarch_extioi_common_post_load,
80     .fields = (const VMStateField[]) {
81         VMSTATE_UINT32_ARRAY(bounce, LoongArchExtIOICommonState,
82                              EXTIOI_IRQS_GROUP_COUNT),
83         VMSTATE_UINT32_ARRAY(nodetype, LoongArchExtIOICommonState,
84                              EXTIOI_IRQS_NODETYPE_COUNT / 2),
85         VMSTATE_UINT32_ARRAY(enable, LoongArchExtIOICommonState,
86                              EXTIOI_IRQS / 32),
87         VMSTATE_UINT32_ARRAY(isr, LoongArchExtIOICommonState,
88                              EXTIOI_IRQS / 32),
89         VMSTATE_UINT32_ARRAY(ipmap, LoongArchExtIOICommonState,
90                              EXTIOI_IRQS_IPMAP_SIZE / 4),
91         VMSTATE_UINT32_ARRAY(coremap, LoongArchExtIOICommonState,
92                              EXTIOI_IRQS / 4),
93         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, LoongArchExtIOICommonState,
94                              num_cpu, vmstate_extioi_core, ExtIOICore),
95         VMSTATE_UINT32(features, LoongArchExtIOICommonState),
96         VMSTATE_UINT32(status, LoongArchExtIOICommonState),
97         VMSTATE_END_OF_LIST()
98     }
99 };
100 
101 static const Property extioi_properties[] = {
102     DEFINE_PROP_BIT("has-virtualization-extension", LoongArchExtIOICommonState,
103                     features, EXTIOI_HAS_VIRT_EXTENSION, 0),
104 };
105 
106 static void loongarch_extioi_common_class_init(ObjectClass *klass, void *data)
107 {
108     DeviceClass *dc = DEVICE_CLASS(klass);
109     LoongArchExtIOICommonClass *lecc = LOONGARCH_EXTIOI_COMMON_CLASS(klass);
110 
111     device_class_set_parent_realize(dc, loongarch_extioi_common_realize,
112                                     &lecc->parent_realize);
113     device_class_set_props(dc, extioi_properties);
114     dc->vmsd = &vmstate_loongarch_extioi;
115 }
116 
117 static const TypeInfo loongarch_extioi_common_types[] = {
118     {
119         .name               = TYPE_LOONGARCH_EXTIOI_COMMON,
120         .parent             = TYPE_SYS_BUS_DEVICE,
121         .instance_size      = sizeof(LoongArchExtIOICommonState),
122         .class_size         = sizeof(LoongArchExtIOICommonClass),
123         .class_init         = loongarch_extioi_common_class_init,
124         .abstract           = true,
125     }
126 };
127 
128 DEFINE_TYPES(loongarch_extioi_common_types)
129