1b4a12dfcSJiaxun Yang /* SPDX-License-Identifier: GPL-2.0-or-later */ 2b4a12dfcSJiaxun Yang /* 3b4a12dfcSJiaxun Yang * Loongson ipi interrupt support 4b4a12dfcSJiaxun Yang * 5b4a12dfcSJiaxun Yang * Copyright (C) 2021 Loongson Technology Corporation Limited 6b4a12dfcSJiaxun Yang */ 7b4a12dfcSJiaxun Yang 8b4a12dfcSJiaxun Yang #include "qemu/osdep.h" 9b4a12dfcSJiaxun Yang #include "hw/boards.h" 10b4a12dfcSJiaxun Yang #include "hw/sysbus.h" 11b4a12dfcSJiaxun Yang #include "hw/intc/loongson_ipi.h" 12b4a12dfcSJiaxun Yang #include "hw/irq.h" 13b4a12dfcSJiaxun Yang #include "hw/qdev-properties.h" 14b4a12dfcSJiaxun Yang #include "qapi/error.h" 15b4a12dfcSJiaxun Yang #include "qemu/log.h" 16b4a12dfcSJiaxun Yang #include "exec/address-spaces.h" 172465c89fSBibo Mao #include "exec/memory.h" 18b4a12dfcSJiaxun Yang #include "migration/vmstate.h" 1991d0b151SJiaxun Yang #ifdef TARGET_LOONGARCH64 20b4a12dfcSJiaxun Yang #include "target/loongarch/cpu.h" 2191d0b151SJiaxun Yang #endif 2291d0b151SJiaxun Yang #ifdef TARGET_MIPS 2391d0b151SJiaxun Yang #include "target/mips/cpu.h" 2491d0b151SJiaxun Yang #endif 25b4a12dfcSJiaxun Yang #include "trace.h" 26b4a12dfcSJiaxun Yang 2791d0b151SJiaxun Yang #ifdef TARGET_LOONGARCH64 28a81cd679SBibo Mao static AddressSpace *get_iocsr_as(CPUState *cpu) 29a81cd679SBibo Mao { 3091d0b151SJiaxun Yang return LOONGARCH_CPU(cpu)->env.address_space_iocsr; 31a81cd679SBibo Mao } 3291d0b151SJiaxun Yang #endif 33a81cd679SBibo Mao 3491d0b151SJiaxun Yang #ifdef TARGET_MIPS 35a81cd679SBibo Mao static AddressSpace *get_iocsr_as(CPUState *cpu) 36a81cd679SBibo Mao { 3791d0b151SJiaxun Yang if (ase_lcsr_available(&MIPS_CPU(cpu)->env)) { 3891d0b151SJiaxun Yang return &MIPS_CPU(cpu)->env.iocsr.as; 3991d0b151SJiaxun Yang } 40a81cd679SBibo Mao 4191d0b151SJiaxun Yang return NULL; 4291d0b151SJiaxun Yang } 43a81cd679SBibo Mao #endif 4491d0b151SJiaxun Yang 4549eba52aSJiaxun Yang static const MemoryRegionOps loongson_ipi_core_ops = { 4649eba52aSJiaxun Yang .read_with_attrs = loongson_ipi_core_readl, 4749eba52aSJiaxun Yang .write_with_attrs = loongson_ipi_core_writel, 4849eba52aSJiaxun Yang .impl.min_access_size = 4, 4949eba52aSJiaxun Yang .impl.max_access_size = 4, 5049eba52aSJiaxun Yang .valid.min_access_size = 4, 5149eba52aSJiaxun Yang .valid.max_access_size = 8, 5249eba52aSJiaxun Yang .endianness = DEVICE_LITTLE_ENDIAN, 5349eba52aSJiaxun Yang }; 5449eba52aSJiaxun Yang 55530e6dafSBibo Mao static void loongson_ipi_realize(DeviceState *dev, Error **errp) 56530e6dafSBibo Mao { 576c8698a5SBibo Mao LoongsonIPICommonState *sc = LOONGSON_IPI_COMMON(dev); 58530e6dafSBibo Mao LoongsonIPIState *s = LOONGSON_IPI(dev); 59*ec859557SBibo Mao LoongsonIPIClass *lic = LOONGSON_IPI_GET_CLASS(dev); 60530e6dafSBibo Mao SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 61530e6dafSBibo Mao Error *local_err = NULL; 62530e6dafSBibo Mao 63*ec859557SBibo Mao lic->parent_realize(dev, &local_err); 64530e6dafSBibo Mao if (local_err) { 65530e6dafSBibo Mao error_propagate(errp, local_err); 66530e6dafSBibo Mao return; 67530e6dafSBibo Mao } 68530e6dafSBibo Mao 696c8698a5SBibo Mao s->ipi_mmio_mem = g_new0(MemoryRegion, sc->num_cpu); 706c8698a5SBibo Mao for (unsigned i = 0; i < sc->num_cpu; i++) { 71530e6dafSBibo Mao g_autofree char *name = g_strdup_printf("loongson_ipi_cpu%d_mmio", i); 726c8698a5SBibo Mao 73a022e0deSBibo Mao memory_region_init_io(&s->ipi_mmio_mem[i], OBJECT(dev), 746c8698a5SBibo Mao &loongson_ipi_core_ops, &sc->cpu[i], name, 0x48); 75a022e0deSBibo Mao sysbus_init_mmio(sbd, &s->ipi_mmio_mem[i]); 76530e6dafSBibo Mao } 77530e6dafSBibo Mao } 78530e6dafSBibo Mao 79530e6dafSBibo Mao static void loongson_ipi_unrealize(DeviceState *dev) 80530e6dafSBibo Mao { 81a022e0deSBibo Mao LoongsonIPIState *s = LOONGSON_IPI(dev); 82*ec859557SBibo Mao LoongsonIPIClass *k = LOONGSON_IPI_GET_CLASS(dev); 83a022e0deSBibo Mao 84a022e0deSBibo Mao g_free(s->ipi_mmio_mem); 85a022e0deSBibo Mao 86*ec859557SBibo Mao k->parent_unrealize(dev); 87530e6dafSBibo Mao } 88530e6dafSBibo Mao 89b4a12dfcSJiaxun Yang static void loongson_ipi_class_init(ObjectClass *klass, void *data) 90b4a12dfcSJiaxun Yang { 91b4a12dfcSJiaxun Yang DeviceClass *dc = DEVICE_CLASS(klass); 927e555781SBibo Mao LoongsonIPIClass *lic = LOONGSON_IPI_CLASS(klass); 93a81cd679SBibo Mao LoongsonIPICommonClass *licc = LOONGSON_IPI_COMMON_CLASS(klass); 94b4a12dfcSJiaxun Yang 957e555781SBibo Mao device_class_set_parent_realize(dc, loongson_ipi_realize, 967e555781SBibo Mao &lic->parent_realize); 977e555781SBibo Mao device_class_set_parent_unrealize(dc, loongson_ipi_unrealize, 987e555781SBibo Mao &lic->parent_unrealize); 99a81cd679SBibo Mao licc->get_iocsr_as = get_iocsr_as; 1008f4f38fdSBibo Mao licc->cpu_by_arch_id = cpu_by_arch_id; 101b4a12dfcSJiaxun Yang } 102b4a12dfcSJiaxun Yang 10313e8ec6cSPhilippe Mathieu-Daudé static const TypeInfo loongson_ipi_types[] = { 10413e8ec6cSPhilippe Mathieu-Daudé { 105b4a12dfcSJiaxun Yang .name = TYPE_LOONGSON_IPI, 1067e555781SBibo Mao .parent = TYPE_LOONGSON_IPI_COMMON, 10740a0815eSBibo Mao .instance_size = sizeof(LoongsonIPIState), 1087e555781SBibo Mao .class_size = sizeof(LoongsonIPIClass), 109b4a12dfcSJiaxun Yang .class_init = loongson_ipi_class_init, 11013e8ec6cSPhilippe Mathieu-Daudé } 111b4a12dfcSJiaxun Yang }; 112b4a12dfcSJiaxun Yang 11313e8ec6cSPhilippe Mathieu-Daudé DEFINE_TYPES(loongson_ipi_types) 114