xref: /qemu/hw/intc/loongarch_extioi_common.c (revision 70ce076fa6dff60585c229a4b641b13e64bf03cf)
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;
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 }
35 
36 static int loongarch_extioi_common_pre_save(void *opaque)
37 {
38     LoongArchExtIOICommonState *s = (LoongArchExtIOICommonState *)opaque;
39     LoongArchExtIOICommonClass *lecc = LOONGARCH_EXTIOI_COMMON_GET_CLASS(s);
40 
41     if (lecc->pre_save) {
42         return lecc->pre_save(s);
43     }
44 
45     return 0;
46 }
47 
48 static int loongarch_extioi_common_post_load(void *opaque, int version_id)
49 {
50     LoongArchExtIOICommonState *s = (LoongArchExtIOICommonState *)opaque;
51     LoongArchExtIOICommonClass *lecc = LOONGARCH_EXTIOI_COMMON_GET_CLASS(s);
52 
53     if (lecc->post_load) {
54         return lecc->post_load(s, version_id);
55     }
56 
57     return 0;
58 }
59 
60 static const VMStateDescription vmstate_extioi_core = {
61     .name = "extioi-core",
62     .version_id = 1,
63     .minimum_version_id = 1,
64     .fields = (const VMStateField[]) {
65         VMSTATE_UINT32_ARRAY(coreisr, ExtIOICore, EXTIOI_IRQS_GROUP_COUNT),
66         VMSTATE_END_OF_LIST()
67     }
68 };
69 
70 static const VMStateDescription vmstate_loongarch_extioi = {
71     .name = "loongarch.extioi",
72     .version_id = 3,
73     .minimum_version_id = 3,
74     .pre_save  = loongarch_extioi_common_pre_save,
75     .post_load = loongarch_extioi_common_post_load,
76     .fields = (const VMStateField[]) {
77         VMSTATE_UINT32_ARRAY(bounce, LoongArchExtIOICommonState,
78                              EXTIOI_IRQS_GROUP_COUNT),
79         VMSTATE_UINT32_ARRAY(nodetype, LoongArchExtIOICommonState,
80                              EXTIOI_IRQS_NODETYPE_COUNT / 2),
81         VMSTATE_UINT32_ARRAY(enable, LoongArchExtIOICommonState,
82                              EXTIOI_IRQS / 32),
83         VMSTATE_UINT32_ARRAY(isr, LoongArchExtIOICommonState,
84                              EXTIOI_IRQS / 32),
85         VMSTATE_UINT32_ARRAY(ipmap, LoongArchExtIOICommonState,
86                              EXTIOI_IRQS_IPMAP_SIZE / 4),
87         VMSTATE_UINT32_ARRAY(coremap, LoongArchExtIOICommonState,
88                              EXTIOI_IRQS / 4),
89         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, LoongArchExtIOICommonState,
90                              num_cpu, vmstate_extioi_core, ExtIOICore),
91         VMSTATE_UINT32(features, LoongArchExtIOICommonState),
92         VMSTATE_UINT32(status, LoongArchExtIOICommonState),
93         VMSTATE_END_OF_LIST()
94     }
95 };
96 
97 static const Property extioi_properties[] = {
98     DEFINE_PROP_BIT("has-virtualization-extension", LoongArchExtIOICommonState,
99                     features, EXTIOI_HAS_VIRT_EXTENSION, 0),
100 };
101 
102 static void loongarch_extioi_common_class_init(ObjectClass *klass, void *data)
103 {
104     DeviceClass *dc = DEVICE_CLASS(klass);
105     LoongArchExtIOICommonClass *lecc = LOONGARCH_EXTIOI_COMMON_CLASS(klass);
106 
107     device_class_set_parent_realize(dc, loongarch_extioi_common_realize,
108                                     &lecc->parent_realize);
109     device_class_set_props(dc, extioi_properties);
110     dc->vmsd = &vmstate_loongarch_extioi;
111 }
112 
113 static const TypeInfo loongarch_extioi_common_types[] = {
114     {
115         .name               = TYPE_LOONGARCH_EXTIOI_COMMON,
116         .parent             = TYPE_SYS_BUS_DEVICE,
117         .instance_size      = sizeof(LoongArchExtIOICommonState),
118         .class_size         = sizeof(LoongArchExtIOICommonClass),
119         .class_init         = loongarch_extioi_common_class_init,
120         .abstract           = true,
121     }
122 };
123 
124 DEFINE_TYPES(loongarch_extioi_common_types)
125