xref: /qemu/hw/mips/cps.c (revision 71e8a915855857e0d45b322826778516cc3e3055)
18e7e8a5bSLeon Alrae /*
28e7e8a5bSLeon Alrae  * Coherent Processing System emulation.
38e7e8a5bSLeon Alrae  *
48e7e8a5bSLeon Alrae  * Copyright (c) 2016 Imagination Technologies
58e7e8a5bSLeon Alrae  *
68e7e8a5bSLeon Alrae  * This library is free software; you can redistribute it and/or
78e7e8a5bSLeon Alrae  * modify it under the terms of the GNU Lesser General Public
88e7e8a5bSLeon Alrae  * License as published by the Free Software Foundation; either
98e7e8a5bSLeon Alrae  * version 2 of the License, or (at your option) any later version.
108e7e8a5bSLeon Alrae  *
118e7e8a5bSLeon Alrae  * This library is distributed in the hope that it will be useful,
128e7e8a5bSLeon Alrae  * but WITHOUT ANY WARRANTY; without even the implied warranty of
138e7e8a5bSLeon Alrae  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
148e7e8a5bSLeon Alrae  * Lesser General Public License for more details.
158e7e8a5bSLeon Alrae  *
168e7e8a5bSLeon Alrae  * You should have received a copy of the GNU Lesser General Public
178e7e8a5bSLeon Alrae  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
188e7e8a5bSLeon Alrae  */
198e7e8a5bSLeon Alrae 
208e7e8a5bSLeon Alrae #include "qemu/osdep.h"
218e7e8a5bSLeon Alrae #include "qapi/error.h"
220b8fa32fSMarkus Armbruster #include "qemu/module.h"
238e7e8a5bSLeon Alrae #include "hw/mips/cps.h"
248e7e8a5bSLeon Alrae #include "hw/mips/mips.h"
258e7e8a5bSLeon Alrae #include "hw/mips/cpudevs.h"
2640829435SLeon Alrae #include "sysemu/kvm.h"
27*71e8a915SMarkus Armbruster #include "sysemu/reset.h"
288e7e8a5bSLeon Alrae 
298e7e8a5bSLeon Alrae qemu_irq get_cps_irq(MIPSCPSState *s, int pin_number)
308e7e8a5bSLeon Alrae {
318e7e8a5bSLeon Alrae     assert(pin_number < s->num_irq);
3219494f81SLeon Alrae     return s->gic.irq_state[pin_number].irq;
338e7e8a5bSLeon Alrae }
348e7e8a5bSLeon Alrae 
358e7e8a5bSLeon Alrae static void mips_cps_init(Object *obj)
368e7e8a5bSLeon Alrae {
378e7e8a5bSLeon Alrae     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
388e7e8a5bSLeon Alrae     MIPSCPSState *s = MIPS_CPS(obj);
398e7e8a5bSLeon Alrae 
408e7e8a5bSLeon Alrae     /* Cover entire address space as there do not seem to be any
418e7e8a5bSLeon Alrae      * constraints for the base address of CPC and GIC. */
428e7e8a5bSLeon Alrae     memory_region_init(&s->container, obj, "mips-cps-container", UINT64_MAX);
438e7e8a5bSLeon Alrae     sysbus_init_mmio(sbd, &s->container);
448e7e8a5bSLeon Alrae }
458e7e8a5bSLeon Alrae 
468e7e8a5bSLeon Alrae static void main_cpu_reset(void *opaque)
478e7e8a5bSLeon Alrae {
488e7e8a5bSLeon Alrae     MIPSCPU *cpu = opaque;
498e7e8a5bSLeon Alrae     CPUState *cs = CPU(cpu);
508e7e8a5bSLeon Alrae 
518e7e8a5bSLeon Alrae     cpu_reset(cs);
528e7e8a5bSLeon Alrae 
538e7e8a5bSLeon Alrae     /* All VPs are halted on reset. Leave powering up to CPC. */
548e7e8a5bSLeon Alrae     cs->halted = 1;
558e7e8a5bSLeon Alrae }
568e7e8a5bSLeon Alrae 
5740829435SLeon Alrae static bool cpu_mips_itu_supported(CPUMIPSState *env)
5840829435SLeon Alrae {
5940829435SLeon Alrae     bool is_mt = (env->CP0_Config5 & (1 << CP0C5_VP)) ||
6040829435SLeon Alrae                  (env->CP0_Config3 & (1 << CP0C3_MT));
6140829435SLeon Alrae 
6240829435SLeon Alrae     return is_mt && !kvm_enabled();
6340829435SLeon Alrae }
6440829435SLeon Alrae 
658e7e8a5bSLeon Alrae static void mips_cps_realize(DeviceState *dev, Error **errp)
668e7e8a5bSLeon Alrae {
678e7e8a5bSLeon Alrae     MIPSCPSState *s = MIPS_CPS(dev);
688e7e8a5bSLeon Alrae     CPUMIPSState *env;
698e7e8a5bSLeon Alrae     MIPSCPU *cpu;
708e7e8a5bSLeon Alrae     int i;
71a9bd9b5aSLeon Alrae     Error *err = NULL;
72a9bd9b5aSLeon Alrae     target_ulong gcr_base;
7340829435SLeon Alrae     bool itu_present = false;
74043715d1SYongbok Kim     bool saar_present = false;
758e7e8a5bSLeon Alrae 
768e7e8a5bSLeon Alrae     for (i = 0; i < s->num_vp; i++) {
77a7519f2bSIgor Mammedov         cpu = MIPS_CPU(cpu_create(s->cpu_type));
788e7e8a5bSLeon Alrae 
798e7e8a5bSLeon Alrae         /* Init internal devices */
805a975d43SPaolo Bonzini         cpu_mips_irq_init_cpu(cpu);
815a975d43SPaolo Bonzini         cpu_mips_clock_init(cpu);
825a975d43SPaolo Bonzini 
835a975d43SPaolo Bonzini         env = &cpu->env;
8440829435SLeon Alrae         if (cpu_mips_itu_supported(env)) {
8540829435SLeon Alrae             itu_present = true;
8640829435SLeon Alrae             /* Attach ITC Tag to the VP */
8740829435SLeon Alrae             env->itc_tag = mips_itu_get_tag_region(&s->itu);
88043715d1SYongbok Kim             env->itu = &s->itu;
8940829435SLeon Alrae         }
908e7e8a5bSLeon Alrae         qemu_register_reset(main_cpu_reset, cpu);
918e7e8a5bSLeon Alrae     }
92a9bd9b5aSLeon Alrae 
93a9bd9b5aSLeon Alrae     cpu = MIPS_CPU(first_cpu);
94a9bd9b5aSLeon Alrae     env = &cpu->env;
95043715d1SYongbok Kim     saar_present = (bool)env->saarp;
96a9bd9b5aSLeon Alrae 
9740829435SLeon Alrae     /* Inter-Thread Communication Unit */
9840829435SLeon Alrae     if (itu_present) {
994626548bSPhilippe Mathieu-Daudé         sysbus_init_child_obj(OBJECT(dev), "itu", &s->itu, sizeof(s->itu),
1004626548bSPhilippe Mathieu-Daudé                               TYPE_MIPS_ITU);
10140829435SLeon Alrae         object_property_set_int(OBJECT(&s->itu), 16, "num-fifo", &err);
10240829435SLeon Alrae         object_property_set_int(OBJECT(&s->itu), 16, "num-semaphores", &err);
103043715d1SYongbok Kim         object_property_set_bool(OBJECT(&s->itu), saar_present, "saar-present",
104043715d1SYongbok Kim                                  &err);
105043715d1SYongbok Kim         if (saar_present) {
106043715d1SYongbok Kim             qdev_prop_set_ptr(DEVICE(&s->itu), "saar", (void *)&env->CP0_SAAR);
107043715d1SYongbok Kim         }
10840829435SLeon Alrae         object_property_set_bool(OBJECT(&s->itu), true, "realized", &err);
10940829435SLeon Alrae         if (err != NULL) {
11040829435SLeon Alrae             error_propagate(errp, err);
11140829435SLeon Alrae             return;
11240829435SLeon Alrae         }
11340829435SLeon Alrae 
11440829435SLeon Alrae         memory_region_add_subregion(&s->container, 0,
11540829435SLeon Alrae                            sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->itu), 0));
11640829435SLeon Alrae     }
11740829435SLeon Alrae 
1182edd5261SLeon Alrae     /* Cluster Power Controller */
1194626548bSPhilippe Mathieu-Daudé     sysbus_init_child_obj(OBJECT(dev), "cpc", &s->cpc, sizeof(s->cpc),
1204626548bSPhilippe Mathieu-Daudé                           TYPE_MIPS_CPC);
1212edd5261SLeon Alrae     object_property_set_int(OBJECT(&s->cpc), s->num_vp, "num-vp", &err);
1222edd5261SLeon Alrae     object_property_set_int(OBJECT(&s->cpc), 1, "vp-start-running", &err);
1232edd5261SLeon Alrae     object_property_set_bool(OBJECT(&s->cpc), true, "realized", &err);
1242edd5261SLeon Alrae     if (err != NULL) {
1252edd5261SLeon Alrae         error_propagate(errp, err);
1262edd5261SLeon Alrae         return;
1272edd5261SLeon Alrae     }
1282edd5261SLeon Alrae 
1292edd5261SLeon Alrae     memory_region_add_subregion(&s->container, 0,
1302edd5261SLeon Alrae                             sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpc), 0));
1312edd5261SLeon Alrae 
13219494f81SLeon Alrae     /* Global Interrupt Controller */
1334626548bSPhilippe Mathieu-Daudé     sysbus_init_child_obj(OBJECT(dev), "gic", &s->gic, sizeof(s->gic),
1344626548bSPhilippe Mathieu-Daudé                           TYPE_MIPS_GIC);
13519494f81SLeon Alrae     object_property_set_int(OBJECT(&s->gic), s->num_vp, "num-vp", &err);
13619494f81SLeon Alrae     object_property_set_int(OBJECT(&s->gic), 128, "num-irq", &err);
13719494f81SLeon Alrae     object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
13819494f81SLeon Alrae     if (err != NULL) {
13919494f81SLeon Alrae         error_propagate(errp, err);
14019494f81SLeon Alrae         return;
14119494f81SLeon Alrae     }
14219494f81SLeon Alrae 
14319494f81SLeon Alrae     memory_region_add_subregion(&s->container, 0,
14419494f81SLeon Alrae                             sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gic), 0));
14519494f81SLeon Alrae 
146a9bd9b5aSLeon Alrae     /* Global Configuration Registers */
147a9bd9b5aSLeon Alrae     gcr_base = env->CP0_CMGCRBase << 4;
148a9bd9b5aSLeon Alrae 
1494626548bSPhilippe Mathieu-Daudé     sysbus_init_child_obj(OBJECT(dev), "gcr", &s->gcr, sizeof(s->gcr),
1504626548bSPhilippe Mathieu-Daudé                           TYPE_MIPS_GCR);
151a9bd9b5aSLeon Alrae     object_property_set_int(OBJECT(&s->gcr), s->num_vp, "num-vp", &err);
152a9bd9b5aSLeon Alrae     object_property_set_int(OBJECT(&s->gcr), 0x800, "gcr-rev", &err);
153a9bd9b5aSLeon Alrae     object_property_set_int(OBJECT(&s->gcr), gcr_base, "gcr-base", &err);
15419494f81SLeon Alrae     object_property_set_link(OBJECT(&s->gcr), OBJECT(&s->gic.mr), "gic", &err);
1552edd5261SLeon Alrae     object_property_set_link(OBJECT(&s->gcr), OBJECT(&s->cpc.mr), "cpc", &err);
156a9bd9b5aSLeon Alrae     object_property_set_bool(OBJECT(&s->gcr), true, "realized", &err);
157a9bd9b5aSLeon Alrae     if (err != NULL) {
158a9bd9b5aSLeon Alrae         error_propagate(errp, err);
159a9bd9b5aSLeon Alrae         return;
160a9bd9b5aSLeon Alrae     }
161a9bd9b5aSLeon Alrae 
162a9bd9b5aSLeon Alrae     memory_region_add_subregion(&s->container, gcr_base,
163a9bd9b5aSLeon Alrae                             sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gcr), 0));
1648e7e8a5bSLeon Alrae }
1658e7e8a5bSLeon Alrae 
1668e7e8a5bSLeon Alrae static Property mips_cps_properties[] = {
1678e7e8a5bSLeon Alrae     DEFINE_PROP_UINT32("num-vp", MIPSCPSState, num_vp, 1),
16819494f81SLeon Alrae     DEFINE_PROP_UINT32("num-irq", MIPSCPSState, num_irq, 256),
169a7519f2bSIgor Mammedov     DEFINE_PROP_STRING("cpu-type", MIPSCPSState, cpu_type),
1708e7e8a5bSLeon Alrae     DEFINE_PROP_END_OF_LIST()
1718e7e8a5bSLeon Alrae };
1728e7e8a5bSLeon Alrae 
1738e7e8a5bSLeon Alrae static void mips_cps_class_init(ObjectClass *klass, void *data)
1748e7e8a5bSLeon Alrae {
1758e7e8a5bSLeon Alrae     DeviceClass *dc = DEVICE_CLASS(klass);
1768e7e8a5bSLeon Alrae 
1778e7e8a5bSLeon Alrae     dc->realize = mips_cps_realize;
1788e7e8a5bSLeon Alrae     dc->props = mips_cps_properties;
1798e7e8a5bSLeon Alrae }
1808e7e8a5bSLeon Alrae 
1818e7e8a5bSLeon Alrae static const TypeInfo mips_cps_info = {
1828e7e8a5bSLeon Alrae     .name = TYPE_MIPS_CPS,
1838e7e8a5bSLeon Alrae     .parent = TYPE_SYS_BUS_DEVICE,
1848e7e8a5bSLeon Alrae     .instance_size = sizeof(MIPSCPSState),
1858e7e8a5bSLeon Alrae     .instance_init = mips_cps_init,
1868e7e8a5bSLeon Alrae     .class_init = mips_cps_class_init,
1878e7e8a5bSLeon Alrae };
1888e7e8a5bSLeon Alrae 
1898e7e8a5bSLeon Alrae static void mips_cps_register_types(void)
1908e7e8a5bSLeon Alrae {
1918e7e8a5bSLeon Alrae     type_register_static(&mips_cps_info);
1928e7e8a5bSLeon Alrae }
1938e7e8a5bSLeon Alrae 
1948e7e8a5bSLeon Alrae type_init(mips_cps_register_types)
195