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 #include "target/mips/cpu.h"
20b4a12dfcSJiaxun Yang #include "trace.h"
21b4a12dfcSJiaxun Yang
get_iocsr_as(CPUState * cpu)22a81cd679SBibo Mao static AddressSpace *get_iocsr_as(CPUState *cpu)
23a81cd679SBibo Mao {
2491d0b151SJiaxun Yang if (ase_lcsr_available(&MIPS_CPU(cpu)->env)) {
2591d0b151SJiaxun Yang return &MIPS_CPU(cpu)->env.iocsr.as;
2691d0b151SJiaxun Yang }
27a81cd679SBibo Mao
2891d0b151SJiaxun Yang return NULL;
2991d0b151SJiaxun Yang }
3091d0b151SJiaxun Yang
3149eba52aSJiaxun Yang static const MemoryRegionOps loongson_ipi_core_ops = {
3249eba52aSJiaxun Yang .read_with_attrs = loongson_ipi_core_readl,
3349eba52aSJiaxun Yang .write_with_attrs = loongson_ipi_core_writel,
3449eba52aSJiaxun Yang .impl.min_access_size = 4,
3549eba52aSJiaxun Yang .impl.max_access_size = 4,
3649eba52aSJiaxun Yang .valid.min_access_size = 4,
3749eba52aSJiaxun Yang .valid.max_access_size = 8,
3849eba52aSJiaxun Yang .endianness = DEVICE_LITTLE_ENDIAN,
3949eba52aSJiaxun Yang };
4049eba52aSJiaxun Yang
loongson_ipi_realize(DeviceState * dev,Error ** errp)41530e6dafSBibo Mao static void loongson_ipi_realize(DeviceState *dev, Error **errp)
42530e6dafSBibo Mao {
436c8698a5SBibo Mao LoongsonIPICommonState *sc = LOONGSON_IPI_COMMON(dev);
44530e6dafSBibo Mao LoongsonIPIState *s = LOONGSON_IPI(dev);
45*ec859557SBibo Mao LoongsonIPIClass *lic = LOONGSON_IPI_GET_CLASS(dev);
46530e6dafSBibo Mao SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
47530e6dafSBibo Mao Error *local_err = NULL;
48530e6dafSBibo Mao
49*ec859557SBibo Mao lic->parent_realize(dev, &local_err);
50530e6dafSBibo Mao if (local_err) {
51530e6dafSBibo Mao error_propagate(errp, local_err);
52530e6dafSBibo Mao return;
53530e6dafSBibo Mao }
54530e6dafSBibo Mao
556c8698a5SBibo Mao s->ipi_mmio_mem = g_new0(MemoryRegion, sc->num_cpu);
566c8698a5SBibo Mao for (unsigned i = 0; i < sc->num_cpu; i++) {
57530e6dafSBibo Mao g_autofree char *name = g_strdup_printf("loongson_ipi_cpu%d_mmio", i);
586c8698a5SBibo Mao
59a022e0deSBibo Mao memory_region_init_io(&s->ipi_mmio_mem[i], OBJECT(dev),
606c8698a5SBibo Mao &loongson_ipi_core_ops, &sc->cpu[i], name, 0x48);
61a022e0deSBibo Mao sysbus_init_mmio(sbd, &s->ipi_mmio_mem[i]);
62530e6dafSBibo Mao }
63530e6dafSBibo Mao }
64530e6dafSBibo Mao
loongson_ipi_unrealize(DeviceState * dev)65530e6dafSBibo Mao static void loongson_ipi_unrealize(DeviceState *dev)
66530e6dafSBibo Mao {
67a022e0deSBibo Mao LoongsonIPIState *s = LOONGSON_IPI(dev);
68*ec859557SBibo Mao LoongsonIPIClass *k = LOONGSON_IPI_GET_CLASS(dev);
69a022e0deSBibo Mao
70a022e0deSBibo Mao g_free(s->ipi_mmio_mem);
71a022e0deSBibo Mao
72*ec859557SBibo Mao k->parent_unrealize(dev);
73530e6dafSBibo Mao }
74530e6dafSBibo Mao
loongson_ipi_class_init(ObjectClass * klass,void * data)75b4a12dfcSJiaxun Yang static void loongson_ipi_class_init(ObjectClass *klass, void *data)
76b4a12dfcSJiaxun Yang {
77b4a12dfcSJiaxun Yang DeviceClass *dc = DEVICE_CLASS(klass);
787e555781SBibo Mao LoongsonIPIClass *lic = LOONGSON_IPI_CLASS(klass);
79a81cd679SBibo Mao LoongsonIPICommonClass *licc = LOONGSON_IPI_COMMON_CLASS(klass);
80b4a12dfcSJiaxun Yang
817e555781SBibo Mao device_class_set_parent_realize(dc, loongson_ipi_realize,
827e555781SBibo Mao &lic->parent_realize);
837e555781SBibo Mao device_class_set_parent_unrealize(dc, loongson_ipi_unrealize,
847e555781SBibo Mao &lic->parent_unrealize);
85a81cd679SBibo Mao licc->get_iocsr_as = get_iocsr_as;
868f4f38fdSBibo Mao licc->cpu_by_arch_id = cpu_by_arch_id;
87b4a12dfcSJiaxun Yang }
88b4a12dfcSJiaxun Yang
8913e8ec6cSPhilippe Mathieu-Daudé static const TypeInfo loongson_ipi_types[] = {
9013e8ec6cSPhilippe Mathieu-Daudé {
91b4a12dfcSJiaxun Yang .name = TYPE_LOONGSON_IPI,
927e555781SBibo Mao .parent = TYPE_LOONGSON_IPI_COMMON,
9340a0815eSBibo Mao .instance_size = sizeof(LoongsonIPIState),
947e555781SBibo Mao .class_size = sizeof(LoongsonIPIClass),
95b4a12dfcSJiaxun Yang .class_init = loongson_ipi_class_init,
9613e8ec6cSPhilippe Mathieu-Daudé }
97b4a12dfcSJiaxun Yang };
98b4a12dfcSJiaxun Yang
9913e8ec6cSPhilippe Mathieu-Daudé DEFINE_TYPES(loongson_ipi_types)
100