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