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
9d136ecc0SChetan Pant * version 2.1 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"
2740829435SLeon Alrae #include "sysemu/kvm.h"
2871e8a915SMarkus Armbruster #include "sysemu/reset.h"
298e7e8a5bSLeon Alrae
get_cps_irq(MIPSCPSState * s,int pin_number)308e7e8a5bSLeon Alrae qemu_irq get_cps_irq(MIPSCPSState *s, int pin_number)
318e7e8a5bSLeon Alrae {
328e7e8a5bSLeon Alrae assert(pin_number < s->num_irq);
3319494f81SLeon Alrae return s->gic.irq_state[pin_number].irq;
348e7e8a5bSLeon Alrae }
358e7e8a5bSLeon Alrae
mips_cps_init(Object * obj)368e7e8a5bSLeon Alrae static void mips_cps_init(Object *obj)
378e7e8a5bSLeon Alrae {
388e7e8a5bSLeon Alrae SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
398e7e8a5bSLeon Alrae MIPSCPSState *s = MIPS_CPS(obj);
408e7e8a5bSLeon Alrae
415ee0abedSPeter Maydell s->clock = qdev_init_clock_in(DEVICE(obj), "clk-in", NULL, NULL, 0);
42f5c3fbfcSAleksandar Markovic /*
43f5c3fbfcSAleksandar Markovic * Cover entire address space as there do not seem to be any
44f5c3fbfcSAleksandar Markovic * constraints for the base address of CPC and GIC.
45f5c3fbfcSAleksandar Markovic */
468e7e8a5bSLeon Alrae memory_region_init(&s->container, obj, "mips-cps-container", UINT64_MAX);
478e7e8a5bSLeon Alrae sysbus_init_mmio(sbd, &s->container);
488e7e8a5bSLeon Alrae }
498e7e8a5bSLeon Alrae
main_cpu_reset(void * opaque)508e7e8a5bSLeon Alrae static void main_cpu_reset(void *opaque)
518e7e8a5bSLeon Alrae {
528e7e8a5bSLeon Alrae MIPSCPU *cpu = opaque;
538e7e8a5bSLeon Alrae CPUState *cs = CPU(cpu);
548e7e8a5bSLeon Alrae
558e7e8a5bSLeon Alrae cpu_reset(cs);
568e7e8a5bSLeon Alrae }
578e7e8a5bSLeon Alrae
cpu_mips_itu_supported(CPUMIPSState * env)5840829435SLeon Alrae static bool cpu_mips_itu_supported(CPUMIPSState *env)
5940829435SLeon Alrae {
6017c2c320SPhilippe Mathieu-Daudé bool is_mt = (env->CP0_Config5 & (1 << CP0C5_VP)) || ase_mt_available(env);
6140829435SLeon Alrae
6240829435SLeon Alrae return is_mt && !kvm_enabled();
6340829435SLeon Alrae }
6440829435SLeon Alrae
mips_cps_realize(DeviceState * dev,Error ** errp)658e7e8a5bSLeon Alrae static void mips_cps_realize(DeviceState *dev, Error **errp)
668e7e8a5bSLeon Alrae {
678e7e8a5bSLeon Alrae MIPSCPSState *s = MIPS_CPS(dev);
68a9bd9b5aSLeon Alrae target_ulong gcr_base;
6940829435SLeon Alrae bool itu_present = false;
708e7e8a5bSLeon Alrae
71ba25670cSPhilippe Mathieu-Daudé if (!clock_get(s->clock)) {
72ba25670cSPhilippe Mathieu-Daudé error_setg(errp, "CPS input clock is not connected to an output clock");
73ba25670cSPhilippe Mathieu-Daudé return;
74ba25670cSPhilippe Mathieu-Daudé }
75ba25670cSPhilippe Mathieu-Daudé
764c921e3fSPhilippe Mathieu-Daudé for (int i = 0; i < s->num_vp; i++) {
774c921e3fSPhilippe Mathieu-Daudé MIPSCPU *cpu = MIPS_CPU(object_new(s->cpu_type));
784c921e3fSPhilippe Mathieu-Daudé CPUMIPSState *env = &cpu->env;
79102ca966SThiago Jung Bauermann
80*805659a8SPhilippe Mathieu-Daudé object_property_set_bool(OBJECT(cpu), "big-endian", s->cpu_is_bigendian,
81*805659a8SPhilippe Mathieu-Daudé &error_abort);
82*805659a8SPhilippe Mathieu-Daudé
83102ca966SThiago Jung Bauermann /* All VPs are halted on reset. Leave powering up to CPC. */
84287fa323SPhilippe Mathieu-Daudé object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
85287fa323SPhilippe Mathieu-Daudé &error_abort);
86287fa323SPhilippe Mathieu-Daudé
87e8373c56SPhilippe Mathieu-Daudé /* All cores use the same clock tree */
88e8373c56SPhilippe Mathieu-Daudé qdev_connect_clock_in(DEVICE(cpu), "clk-in", s->clock);
89102ca966SThiago Jung Bauermann
90102ca966SThiago Jung Bauermann if (!qdev_realize_and_unref(DEVICE(cpu), NULL, errp)) {
91102ca966SThiago Jung Bauermann return;
92102ca966SThiago Jung Bauermann }
938e7e8a5bSLeon Alrae
948e7e8a5bSLeon Alrae /* Init internal devices */
955a975d43SPaolo Bonzini cpu_mips_irq_init_cpu(cpu);
965a975d43SPaolo Bonzini cpu_mips_clock_init(cpu);
975a975d43SPaolo Bonzini
9840829435SLeon Alrae if (cpu_mips_itu_supported(env)) {
9940829435SLeon Alrae itu_present = true;
10040829435SLeon Alrae /* Attach ITC Tag to the VP */
10140829435SLeon Alrae env->itc_tag = mips_itu_get_tag_region(&s->itu);
10240829435SLeon Alrae }
1038e7e8a5bSLeon Alrae qemu_register_reset(main_cpu_reset, cpu);
1048e7e8a5bSLeon Alrae }
105a9bd9b5aSLeon Alrae
10640829435SLeon Alrae /* Inter-Thread Communication Unit */
10740829435SLeon Alrae if (itu_present) {
1080074fce6SMarkus Armbruster object_initialize_child(OBJECT(dev), "itu", &s->itu, TYPE_MIPS_ITU);
10910997f2dSPhilippe Mathieu-Daudé object_property_set_uint(OBJECT(&s->itu), "num-fifo", 16,
11081f66cfdSMarkus Armbruster &error_abort);
11110997f2dSPhilippe Mathieu-Daudé object_property_set_uint(OBJECT(&s->itu), "num-semaphores", 16,
11281f66cfdSMarkus Armbruster &error_abort);
113668f62ecSMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->itu), errp)) {
11440829435SLeon Alrae return;
11540829435SLeon Alrae }
11640829435SLeon Alrae
11740829435SLeon Alrae memory_region_add_subregion(&s->container, 0,
11840829435SLeon Alrae sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->itu), 0));
11940829435SLeon Alrae }
12040829435SLeon Alrae
1212edd5261SLeon Alrae /* Cluster Power Controller */
1220074fce6SMarkus Armbruster object_initialize_child(OBJECT(dev), "cpc", &s->cpc, TYPE_MIPS_CPC);
12310997f2dSPhilippe Mathieu-Daudé object_property_set_uint(OBJECT(&s->cpc), "num-vp", s->num_vp,
12481f66cfdSMarkus Armbruster &error_abort);
1255325cc34SMarkus Armbruster object_property_set_int(OBJECT(&s->cpc), "vp-start-running", 1,
12681f66cfdSMarkus Armbruster &error_abort);
127668f62ecSMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->cpc), errp)) {
1282edd5261SLeon Alrae return;
1292edd5261SLeon Alrae }
1302edd5261SLeon Alrae
1312edd5261SLeon Alrae memory_region_add_subregion(&s->container, 0,
1322edd5261SLeon Alrae sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpc), 0));
1332edd5261SLeon Alrae
13419494f81SLeon Alrae /* Global Interrupt Controller */
1350074fce6SMarkus Armbruster object_initialize_child(OBJECT(dev), "gic", &s->gic, TYPE_MIPS_GIC);
13610997f2dSPhilippe Mathieu-Daudé object_property_set_uint(OBJECT(&s->gic), "num-vp", s->num_vp,
13781f66cfdSMarkus Armbruster &error_abort);
13810997f2dSPhilippe Mathieu-Daudé object_property_set_uint(OBJECT(&s->gic), "num-irq", 128,
13981f66cfdSMarkus Armbruster &error_abort);
140668f62ecSMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->gic), errp)) {
14119494f81SLeon Alrae return;
14219494f81SLeon Alrae }
14319494f81SLeon Alrae
14419494f81SLeon Alrae memory_region_add_subregion(&s->container, 0,
14519494f81SLeon Alrae sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gic), 0));
14619494f81SLeon Alrae
147a9bd9b5aSLeon Alrae /* Global Configuration Registers */
1484c921e3fSPhilippe Mathieu-Daudé gcr_base = MIPS_CPU(first_cpu)->env.CP0_CMGCRBase << 4;
149a9bd9b5aSLeon Alrae
1500074fce6SMarkus Armbruster object_initialize_child(OBJECT(dev), "gcr", &s->gcr, TYPE_MIPS_GCR);
15110997f2dSPhilippe Mathieu-Daudé object_property_set_uint(OBJECT(&s->gcr), "num-vp", s->num_vp,
15281f66cfdSMarkus Armbruster &error_abort);
1535325cc34SMarkus Armbruster object_property_set_int(OBJECT(&s->gcr), "gcr-rev", 0x800,
15481f66cfdSMarkus Armbruster &error_abort);
1555325cc34SMarkus Armbruster object_property_set_int(OBJECT(&s->gcr), "gcr-base", gcr_base,
15681f66cfdSMarkus Armbruster &error_abort);
1575325cc34SMarkus Armbruster object_property_set_link(OBJECT(&s->gcr), "gic", OBJECT(&s->gic.mr),
1582726dc51SMarkus Armbruster &error_abort);
1595325cc34SMarkus Armbruster object_property_set_link(OBJECT(&s->gcr), "cpc", OBJECT(&s->cpc.mr),
1602726dc51SMarkus Armbruster &error_abort);
161668f62ecSMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->gcr), errp)) {
162a9bd9b5aSLeon Alrae return;
163a9bd9b5aSLeon Alrae }
164a9bd9b5aSLeon Alrae
165a9bd9b5aSLeon Alrae memory_region_add_subregion(&s->container, gcr_base,
166a9bd9b5aSLeon Alrae sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gcr), 0));
1678e7e8a5bSLeon Alrae }
1688e7e8a5bSLeon Alrae
1698e7e8a5bSLeon Alrae static Property mips_cps_properties[] = {
1708e7e8a5bSLeon Alrae DEFINE_PROP_UINT32("num-vp", MIPSCPSState, num_vp, 1),
17119494f81SLeon Alrae DEFINE_PROP_UINT32("num-irq", MIPSCPSState, num_irq, 256),
172a7519f2bSIgor Mammedov DEFINE_PROP_STRING("cpu-type", MIPSCPSState, cpu_type),
173*805659a8SPhilippe Mathieu-Daudé DEFINE_PROP_BOOL("cpu-big-endian", MIPSCPSState, cpu_is_bigendian, false),
1748e7e8a5bSLeon Alrae DEFINE_PROP_END_OF_LIST()
1758e7e8a5bSLeon Alrae };
1768e7e8a5bSLeon Alrae
mips_cps_class_init(ObjectClass * klass,void * data)1778e7e8a5bSLeon Alrae static void mips_cps_class_init(ObjectClass *klass, void *data)
1788e7e8a5bSLeon Alrae {
1798e7e8a5bSLeon Alrae DeviceClass *dc = DEVICE_CLASS(klass);
1808e7e8a5bSLeon Alrae
1818e7e8a5bSLeon Alrae dc->realize = mips_cps_realize;
1824f67d30bSMarc-André Lureau device_class_set_props(dc, mips_cps_properties);
1838e7e8a5bSLeon Alrae }
1848e7e8a5bSLeon Alrae
1858e7e8a5bSLeon Alrae static const TypeInfo mips_cps_info = {
1868e7e8a5bSLeon Alrae .name = TYPE_MIPS_CPS,
1878e7e8a5bSLeon Alrae .parent = TYPE_SYS_BUS_DEVICE,
1888e7e8a5bSLeon Alrae .instance_size = sizeof(MIPSCPSState),
1898e7e8a5bSLeon Alrae .instance_init = mips_cps_init,
1908e7e8a5bSLeon Alrae .class_init = mips_cps_class_init,
1918e7e8a5bSLeon Alrae };
1928e7e8a5bSLeon Alrae
mips_cps_register_types(void)1938e7e8a5bSLeon Alrae static void mips_cps_register_types(void)
1948e7e8a5bSLeon Alrae {
1958e7e8a5bSLeon Alrae type_register_static(&mips_cps_info);
1968e7e8a5bSLeon Alrae }
1978e7e8a5bSLeon Alrae
1988e7e8a5bSLeon Alrae type_init(mips_cps_register_types)
199