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