xref: /qemu/hw/mips/cps.c (revision ba25670c1d3e122bfa5a43cd785f5eb4988861d9)
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"
25e8373c56SPhilippe Mathieu-Daudé #include "hw/qdev-clock.h"
26a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
278e7e8a5bSLeon Alrae #include "hw/mips/cpudevs.h"
2840829435SLeon Alrae #include "sysemu/kvm.h"
2971e8a915SMarkus Armbruster #include "sysemu/reset.h"
308e7e8a5bSLeon Alrae 
318e7e8a5bSLeon Alrae qemu_irq get_cps_irq(MIPSCPSState *s, int pin_number)
328e7e8a5bSLeon Alrae {
338e7e8a5bSLeon Alrae     assert(pin_number < s->num_irq);
3419494f81SLeon Alrae     return s->gic.irq_state[pin_number].irq;
358e7e8a5bSLeon Alrae }
368e7e8a5bSLeon Alrae 
378e7e8a5bSLeon Alrae static void mips_cps_init(Object *obj)
388e7e8a5bSLeon Alrae {
398e7e8a5bSLeon Alrae     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
408e7e8a5bSLeon Alrae     MIPSCPSState *s = MIPS_CPS(obj);
418e7e8a5bSLeon Alrae 
42e8373c56SPhilippe Mathieu-Daudé     s->clock = qdev_init_clock_in(DEVICE(obj), "clk-in", NULL, NULL);
43f5c3fbfcSAleksandar Markovic     /*
44f5c3fbfcSAleksandar Markovic      * Cover entire address space as there do not seem to be any
45f5c3fbfcSAleksandar Markovic      * constraints for the base address of CPC and GIC.
46f5c3fbfcSAleksandar Markovic      */
478e7e8a5bSLeon Alrae     memory_region_init(&s->container, obj, "mips-cps-container", UINT64_MAX);
488e7e8a5bSLeon Alrae     sysbus_init_mmio(sbd, &s->container);
498e7e8a5bSLeon Alrae }
508e7e8a5bSLeon Alrae 
518e7e8a5bSLeon Alrae static void main_cpu_reset(void *opaque)
528e7e8a5bSLeon Alrae {
538e7e8a5bSLeon Alrae     MIPSCPU *cpu = opaque;
548e7e8a5bSLeon Alrae     CPUState *cs = CPU(cpu);
558e7e8a5bSLeon Alrae 
568e7e8a5bSLeon Alrae     cpu_reset(cs);
578e7e8a5bSLeon Alrae }
588e7e8a5bSLeon Alrae 
5940829435SLeon Alrae static bool cpu_mips_itu_supported(CPUMIPSState *env)
6040829435SLeon Alrae {
6140829435SLeon Alrae     bool is_mt = (env->CP0_Config5 & (1 << CP0C5_VP)) ||
6240829435SLeon Alrae                  (env->CP0_Config3 & (1 << CP0C3_MT));
6340829435SLeon Alrae 
6440829435SLeon Alrae     return is_mt && !kvm_enabled();
6540829435SLeon Alrae }
6640829435SLeon Alrae 
678e7e8a5bSLeon Alrae static void mips_cps_realize(DeviceState *dev, Error **errp)
688e7e8a5bSLeon Alrae {
698e7e8a5bSLeon Alrae     MIPSCPSState *s = MIPS_CPS(dev);
708e7e8a5bSLeon Alrae     CPUMIPSState *env;
718e7e8a5bSLeon Alrae     MIPSCPU *cpu;
728e7e8a5bSLeon Alrae     int i;
73a9bd9b5aSLeon Alrae     target_ulong gcr_base;
7440829435SLeon Alrae     bool itu_present = false;
75043715d1SYongbok Kim     bool saar_present = false;
768e7e8a5bSLeon Alrae 
77*ba25670cSPhilippe Mathieu-Daudé     if (!clock_get(s->clock)) {
78*ba25670cSPhilippe Mathieu-Daudé         error_setg(errp, "CPS input clock is not connected to an output clock");
79*ba25670cSPhilippe Mathieu-Daudé         return;
80*ba25670cSPhilippe Mathieu-Daudé     }
81*ba25670cSPhilippe Mathieu-Daudé 
828e7e8a5bSLeon Alrae     for (i = 0; i < s->num_vp; i++) {
83102ca966SThiago Jung Bauermann         cpu = MIPS_CPU(object_new(s->cpu_type));
84102ca966SThiago Jung Bauermann 
85102ca966SThiago Jung Bauermann         /* All VPs are halted on reset. Leave powering up to CPC. */
86102ca966SThiago Jung Bauermann         if (!object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
87102ca966SThiago Jung Bauermann                                       errp)) {
88102ca966SThiago Jung Bauermann             return;
89102ca966SThiago Jung Bauermann         }
90e8373c56SPhilippe Mathieu-Daudé         /* All cores use the same clock tree */
91e8373c56SPhilippe Mathieu-Daudé         qdev_connect_clock_in(DEVICE(cpu), "clk-in", s->clock);
92102ca966SThiago Jung Bauermann 
93102ca966SThiago Jung Bauermann         if (!qdev_realize_and_unref(DEVICE(cpu), NULL, errp)) {
94102ca966SThiago Jung Bauermann             return;
95102ca966SThiago Jung Bauermann         }
968e7e8a5bSLeon Alrae 
978e7e8a5bSLeon Alrae         /* Init internal devices */
985a975d43SPaolo Bonzini         cpu_mips_irq_init_cpu(cpu);
995a975d43SPaolo Bonzini         cpu_mips_clock_init(cpu);
1005a975d43SPaolo Bonzini 
1015a975d43SPaolo Bonzini         env = &cpu->env;
10240829435SLeon Alrae         if (cpu_mips_itu_supported(env)) {
10340829435SLeon Alrae             itu_present = true;
10440829435SLeon Alrae             /* Attach ITC Tag to the VP */
10540829435SLeon Alrae             env->itc_tag = mips_itu_get_tag_region(&s->itu);
106043715d1SYongbok Kim             env->itu = &s->itu;
10740829435SLeon Alrae         }
1088e7e8a5bSLeon Alrae         qemu_register_reset(main_cpu_reset, cpu);
1098e7e8a5bSLeon Alrae     }
110a9bd9b5aSLeon Alrae 
111a9bd9b5aSLeon Alrae     cpu = MIPS_CPU(first_cpu);
112a9bd9b5aSLeon Alrae     env = &cpu->env;
113043715d1SYongbok Kim     saar_present = (bool)env->saarp;
114a9bd9b5aSLeon Alrae 
11540829435SLeon Alrae     /* Inter-Thread Communication Unit */
11640829435SLeon Alrae     if (itu_present) {
1170074fce6SMarkus Armbruster         object_initialize_child(OBJECT(dev), "itu", &s->itu, TYPE_MIPS_ITU);
1185325cc34SMarkus Armbruster         object_property_set_int(OBJECT(&s->itu), "num-fifo", 16,
11981f66cfdSMarkus Armbruster                                 &error_abort);
1205325cc34SMarkus Armbruster         object_property_set_int(OBJECT(&s->itu), "num-semaphores", 16,
12181f66cfdSMarkus Armbruster                                 &error_abort);
1225325cc34SMarkus Armbruster         object_property_set_bool(OBJECT(&s->itu), "saar-present", saar_present,
12381f66cfdSMarkus Armbruster                                  &error_abort);
124043715d1SYongbok Kim         if (saar_present) {
1253cff8173SMarc-André Lureau             s->itu.saar = &env->CP0_SAAR;
126043715d1SYongbok Kim         }
127668f62ecSMarkus Armbruster         if (!sysbus_realize(SYS_BUS_DEVICE(&s->itu), errp)) {
12840829435SLeon Alrae             return;
12940829435SLeon Alrae         }
13040829435SLeon Alrae 
13140829435SLeon Alrae         memory_region_add_subregion(&s->container, 0,
13240829435SLeon Alrae                            sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->itu), 0));
13340829435SLeon Alrae     }
13440829435SLeon Alrae 
1352edd5261SLeon Alrae     /* Cluster Power Controller */
1360074fce6SMarkus Armbruster     object_initialize_child(OBJECT(dev), "cpc", &s->cpc, TYPE_MIPS_CPC);
1375325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->cpc), "num-vp", s->num_vp,
13881f66cfdSMarkus Armbruster                             &error_abort);
1395325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->cpc), "vp-start-running", 1,
14081f66cfdSMarkus Armbruster                             &error_abort);
141668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->cpc), errp)) {
1422edd5261SLeon Alrae         return;
1432edd5261SLeon Alrae     }
1442edd5261SLeon Alrae 
1452edd5261SLeon Alrae     memory_region_add_subregion(&s->container, 0,
1462edd5261SLeon Alrae                             sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpc), 0));
1472edd5261SLeon Alrae 
14819494f81SLeon Alrae     /* Global Interrupt Controller */
1490074fce6SMarkus Armbruster     object_initialize_child(OBJECT(dev), "gic", &s->gic, TYPE_MIPS_GIC);
1505325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gic), "num-vp", s->num_vp,
15181f66cfdSMarkus Armbruster                             &error_abort);
1525325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gic), "num-irq", 128,
15381f66cfdSMarkus Armbruster                             &error_abort);
154668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gic), errp)) {
15519494f81SLeon Alrae         return;
15619494f81SLeon Alrae     }
15719494f81SLeon Alrae 
15819494f81SLeon Alrae     memory_region_add_subregion(&s->container, 0,
15919494f81SLeon Alrae                             sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gic), 0));
16019494f81SLeon Alrae 
161a9bd9b5aSLeon Alrae     /* Global Configuration Registers */
162a9bd9b5aSLeon Alrae     gcr_base = env->CP0_CMGCRBase << 4;
163a9bd9b5aSLeon Alrae 
1640074fce6SMarkus Armbruster     object_initialize_child(OBJECT(dev), "gcr", &s->gcr, TYPE_MIPS_GCR);
1655325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gcr), "num-vp", s->num_vp,
16681f66cfdSMarkus Armbruster                             &error_abort);
1675325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gcr), "gcr-rev", 0x800,
16881f66cfdSMarkus Armbruster                             &error_abort);
1695325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gcr), "gcr-base", gcr_base,
17081f66cfdSMarkus Armbruster                             &error_abort);
1715325cc34SMarkus Armbruster     object_property_set_link(OBJECT(&s->gcr), "gic", OBJECT(&s->gic.mr),
1722726dc51SMarkus Armbruster                              &error_abort);
1735325cc34SMarkus Armbruster     object_property_set_link(OBJECT(&s->gcr), "cpc", OBJECT(&s->cpc.mr),
1742726dc51SMarkus Armbruster                              &error_abort);
175668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gcr), errp)) {
176a9bd9b5aSLeon Alrae         return;
177a9bd9b5aSLeon Alrae     }
178a9bd9b5aSLeon Alrae 
179a9bd9b5aSLeon Alrae     memory_region_add_subregion(&s->container, gcr_base,
180a9bd9b5aSLeon Alrae                             sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gcr), 0));
1818e7e8a5bSLeon Alrae }
1828e7e8a5bSLeon Alrae 
1838e7e8a5bSLeon Alrae static Property mips_cps_properties[] = {
1848e7e8a5bSLeon Alrae     DEFINE_PROP_UINT32("num-vp", MIPSCPSState, num_vp, 1),
18519494f81SLeon Alrae     DEFINE_PROP_UINT32("num-irq", MIPSCPSState, num_irq, 256),
186a7519f2bSIgor Mammedov     DEFINE_PROP_STRING("cpu-type", MIPSCPSState, cpu_type),
1878e7e8a5bSLeon Alrae     DEFINE_PROP_END_OF_LIST()
1888e7e8a5bSLeon Alrae };
1898e7e8a5bSLeon Alrae 
1908e7e8a5bSLeon Alrae static void mips_cps_class_init(ObjectClass *klass, void *data)
1918e7e8a5bSLeon Alrae {
1928e7e8a5bSLeon Alrae     DeviceClass *dc = DEVICE_CLASS(klass);
1938e7e8a5bSLeon Alrae 
1948e7e8a5bSLeon Alrae     dc->realize = mips_cps_realize;
1954f67d30bSMarc-André Lureau     device_class_set_props(dc, mips_cps_properties);
1968e7e8a5bSLeon Alrae }
1978e7e8a5bSLeon Alrae 
1988e7e8a5bSLeon Alrae static const TypeInfo mips_cps_info = {
1998e7e8a5bSLeon Alrae     .name = TYPE_MIPS_CPS,
2008e7e8a5bSLeon Alrae     .parent = TYPE_SYS_BUS_DEVICE,
2018e7e8a5bSLeon Alrae     .instance_size = sizeof(MIPSCPSState),
2028e7e8a5bSLeon Alrae     .instance_init = mips_cps_init,
2038e7e8a5bSLeon Alrae     .class_init = mips_cps_class_init,
2048e7e8a5bSLeon Alrae };
2058e7e8a5bSLeon Alrae 
2068e7e8a5bSLeon Alrae static void mips_cps_register_types(void)
2078e7e8a5bSLeon Alrae {
2088e7e8a5bSLeon Alrae     type_register_static(&mips_cps_info);
2098e7e8a5bSLeon Alrae }
2108e7e8a5bSLeon Alrae 
2118e7e8a5bSLeon Alrae type_init(mips_cps_register_types)
212