1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * LoongArch IPI interrupt support 4 * 5 * Copyright (C) 2024 Loongson Technology Corporation Limited 6 */ 7 8 #include "qemu/osdep.h" 9 #include "qemu/error-report.h" 10 #include "hw/boards.h" 11 #include "qapi/error.h" 12 #include "hw/intc/loongarch_ipi.h" 13 #include "hw/qdev-properties.h" 14 #include "target/loongarch/cpu.h" 15 16 static AddressSpace *get_iocsr_as(CPUState *cpu) 17 { 18 return LOONGARCH_CPU(cpu)->env.address_space_iocsr; 19 } 20 21 static int loongarch_ipi_cmp(const void *a, const void *b) 22 { 23 IPICore *ipi_a = (IPICore *)a; 24 IPICore *ipi_b = (IPICore *)b; 25 26 return ipi_a->arch_id - ipi_b->arch_id; 27 } 28 29 static int loongarch_cpu_by_arch_id(LoongsonIPICommonState *lics, 30 int64_t arch_id, int *index, CPUState **pcs) 31 { 32 IPICore ipi, *found; 33 34 ipi.arch_id = arch_id; 35 found = bsearch(&ipi, lics->cpu, lics->num_cpu, sizeof(IPICore), 36 loongarch_ipi_cmp); 37 if (found && found->cpu) { 38 if (index) { 39 *index = found - lics->cpu; 40 } 41 42 if (pcs) { 43 *pcs = found->cpu; 44 } 45 46 return MEMTX_OK; 47 } 48 49 return MEMTX_ERROR; 50 } 51 52 static void loongarch_ipi_realize(DeviceState *dev, Error **errp) 53 { 54 LoongsonIPICommonState *lics = LOONGSON_IPI_COMMON(dev); 55 LoongarchIPIClass *lic = LOONGARCH_IPI_GET_CLASS(dev); 56 MachineState *machine = MACHINE(qdev_get_machine()); 57 MachineClass *mc = MACHINE_GET_CLASS(machine); 58 const CPUArchIdList *id_list; 59 Error *local_err = NULL; 60 int i; 61 62 lic->parent_realize(dev, &local_err); 63 if (local_err) { 64 error_propagate(errp, local_err); 65 return; 66 } 67 68 assert(mc->possible_cpu_arch_ids); 69 id_list = mc->possible_cpu_arch_ids(machine); 70 lics->num_cpu = id_list->len; 71 lics->cpu = g_new0(IPICore, lics->num_cpu); 72 for (i = 0; i < lics->num_cpu; i++) { 73 lics->cpu[i].arch_id = id_list->cpus[i].arch_id; 74 lics->cpu[i].cpu = CPU(id_list->cpus[i].cpu); 75 lics->cpu[i].ipi = lics; 76 qdev_init_gpio_out(dev, &lics->cpu[i].irq, 1); 77 } 78 } 79 80 static void loongarch_ipi_cpu_plug(HotplugHandler *hotplug_dev, 81 DeviceState *dev, Error **errp) 82 { 83 Object *obj = OBJECT(dev); 84 85 if (!object_dynamic_cast(obj, TYPE_LOONGARCH_CPU)) { 86 warn_report("LoongArch extioi: Invalid %s device type", 87 object_get_typename(obj)); 88 return; 89 } 90 } 91 92 static void loongarch_ipi_cpu_unplug(HotplugHandler *hotplug_dev, 93 DeviceState *dev, Error **errp) 94 { 95 Object *obj = OBJECT(dev); 96 97 if (!object_dynamic_cast(obj, TYPE_LOONGARCH_CPU)) { 98 warn_report("LoongArch extioi: Invalid %s device type", 99 object_get_typename(obj)); 100 return; 101 } 102 } 103 104 static void loongarch_ipi_class_init(ObjectClass *klass, void *data) 105 { 106 LoongsonIPICommonClass *licc = LOONGSON_IPI_COMMON_CLASS(klass); 107 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 108 LoongarchIPIClass *lic = LOONGARCH_IPI_CLASS(klass); 109 DeviceClass *dc = DEVICE_CLASS(klass); 110 111 device_class_set_parent_realize(dc, loongarch_ipi_realize, 112 &lic->parent_realize); 113 licc->get_iocsr_as = get_iocsr_as; 114 licc->cpu_by_arch_id = loongarch_cpu_by_arch_id; 115 hc->plug = loongarch_ipi_cpu_plug; 116 hc->unplug = loongarch_ipi_cpu_unplug; 117 } 118 119 static const TypeInfo loongarch_ipi_types[] = { 120 { 121 .name = TYPE_LOONGARCH_IPI, 122 .parent = TYPE_LOONGSON_IPI_COMMON, 123 .instance_size = sizeof(LoongarchIPIState), 124 .class_size = sizeof(LoongarchIPIClass), 125 .class_init = loongarch_ipi_class_init, 126 .interfaces = (InterfaceInfo[]) { 127 { TYPE_HOTPLUG_HANDLER }, 128 { } 129 }, 130 } 131 }; 132 133 DEFINE_TYPES(loongarch_ipi_types) 134