xref: /openbmc/qemu/hw/misc/mips_cpc.c (revision f5117fd28552fe3fe32ef0495582b1caaef7a28d)
11f93a6e4SLeon Alrae /*
21f93a6e4SLeon Alrae  * Cluster Power Controller emulation
31f93a6e4SLeon Alrae  *
41f93a6e4SLeon Alrae  * Copyright (c) 2016 Imagination Technologies
51f93a6e4SLeon Alrae  *
61f93a6e4SLeon Alrae  * This library is free software; you can redistribute it and/or
71f93a6e4SLeon Alrae  * modify it under the terms of the GNU Lesser General Public
81f93a6e4SLeon Alrae  * License as published by the Free Software Foundation; either
91f93a6e4SLeon Alrae  * version 2 of the License, or (at your option) any later version.
101f93a6e4SLeon Alrae  *
111f93a6e4SLeon Alrae  * This library is distributed in the hope that it will be useful,
121f93a6e4SLeon Alrae  * but WITHOUT ANY WARRANTY; without even the implied warranty of
131f93a6e4SLeon Alrae  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
141f93a6e4SLeon Alrae  * Lesser General Public License for more details.
151f93a6e4SLeon Alrae  *
161f93a6e4SLeon Alrae  * You should have received a copy of the GNU Lesser General Public
171f93a6e4SLeon Alrae  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
181f93a6e4SLeon Alrae  */
191f93a6e4SLeon Alrae 
201f93a6e4SLeon Alrae #include "qemu/osdep.h"
211f93a6e4SLeon Alrae #include "qapi/error.h"
2233c11879SPaolo Bonzini #include "cpu.h"
2303dd024fSPaolo Bonzini #include "qemu/log.h"
241f93a6e4SLeon Alrae #include "hw/sysbus.h"
251f93a6e4SLeon Alrae 
261f93a6e4SLeon Alrae #include "hw/misc/mips_cpc.h"
271f93a6e4SLeon Alrae 
281f93a6e4SLeon Alrae static inline uint64_t cpc_vp_run_mask(MIPSCPCState *cpc)
291f93a6e4SLeon Alrae {
301f93a6e4SLeon Alrae     return (1ULL << cpc->num_vp) - 1;
311f93a6e4SLeon Alrae }
321f93a6e4SLeon Alrae 
33*f5117fd2SMiodrag Dinic static void mips_cpu_reset_async_work(CPUState *cs, run_on_cpu_data data)
34*f5117fd2SMiodrag Dinic {
35*f5117fd2SMiodrag Dinic     MIPSCPCState *cpc = (MIPSCPCState *) data.host_ptr;
36*f5117fd2SMiodrag Dinic 
37*f5117fd2SMiodrag Dinic     cpu_reset(cs);
38*f5117fd2SMiodrag Dinic     cpc->vp_running |= 1ULL << cs->cpu_index;
39*f5117fd2SMiodrag Dinic }
40*f5117fd2SMiodrag Dinic 
411f93a6e4SLeon Alrae static void cpc_run_vp(MIPSCPCState *cpc, uint64_t vp_run)
421f93a6e4SLeon Alrae {
431f93a6e4SLeon Alrae     CPUState *cs = first_cpu;
441f93a6e4SLeon Alrae 
451f93a6e4SLeon Alrae     CPU_FOREACH(cs) {
461f93a6e4SLeon Alrae         uint64_t i = 1ULL << cs->cpu_index;
471f93a6e4SLeon Alrae         if (i & vp_run & ~cpc->vp_running) {
48*f5117fd2SMiodrag Dinic             /*
49*f5117fd2SMiodrag Dinic              * To avoid racing with a CPU we are just kicking off.
50*f5117fd2SMiodrag Dinic              * We do the final bit of preparation for the work in
51*f5117fd2SMiodrag Dinic              * the target CPUs context.
52*f5117fd2SMiodrag Dinic              */
53*f5117fd2SMiodrag Dinic             async_safe_run_on_cpu(cs, mips_cpu_reset_async_work,
54*f5117fd2SMiodrag Dinic                                   RUN_ON_CPU_HOST_PTR(cpc));
551f93a6e4SLeon Alrae         }
561f93a6e4SLeon Alrae     }
571f93a6e4SLeon Alrae }
581f93a6e4SLeon Alrae 
591f93a6e4SLeon Alrae static void cpc_stop_vp(MIPSCPCState *cpc, uint64_t vp_stop)
601f93a6e4SLeon Alrae {
611f93a6e4SLeon Alrae     CPUState *cs = first_cpu;
621f93a6e4SLeon Alrae 
631f93a6e4SLeon Alrae     CPU_FOREACH(cs) {
641f93a6e4SLeon Alrae         uint64_t i = 1ULL << cs->cpu_index;
651f93a6e4SLeon Alrae         if (i & vp_stop & cpc->vp_running) {
66dff94251SLeon Alrae             cpu_interrupt(cs, CPU_INTERRUPT_HALT);
671f93a6e4SLeon Alrae             cpc->vp_running &= ~i;
681f93a6e4SLeon Alrae         }
691f93a6e4SLeon Alrae     }
701f93a6e4SLeon Alrae }
711f93a6e4SLeon Alrae 
721f93a6e4SLeon Alrae static void cpc_write(void *opaque, hwaddr offset, uint64_t data,
731f93a6e4SLeon Alrae                       unsigned size)
741f93a6e4SLeon Alrae {
751f93a6e4SLeon Alrae     MIPSCPCState *s = opaque;
761f93a6e4SLeon Alrae 
771f93a6e4SLeon Alrae     switch (offset) {
781f93a6e4SLeon Alrae     case CPC_CL_BASE_OFS + CPC_VP_RUN_OFS:
791f93a6e4SLeon Alrae     case CPC_CO_BASE_OFS + CPC_VP_RUN_OFS:
801f93a6e4SLeon Alrae         cpc_run_vp(s, data & cpc_vp_run_mask(s));
811f93a6e4SLeon Alrae         break;
821f93a6e4SLeon Alrae     case CPC_CL_BASE_OFS + CPC_VP_STOP_OFS:
831f93a6e4SLeon Alrae     case CPC_CO_BASE_OFS + CPC_VP_STOP_OFS:
841f93a6e4SLeon Alrae         cpc_stop_vp(s, data & cpc_vp_run_mask(s));
851f93a6e4SLeon Alrae         break;
861f93a6e4SLeon Alrae     default:
871f93a6e4SLeon Alrae         qemu_log_mask(LOG_UNIMP,
881f93a6e4SLeon Alrae                       "%s: Bad offset 0x%x\n",  __func__, (int)offset);
891f93a6e4SLeon Alrae         break;
901f93a6e4SLeon Alrae     }
911f93a6e4SLeon Alrae 
921f93a6e4SLeon Alrae     return;
931f93a6e4SLeon Alrae }
941f93a6e4SLeon Alrae 
951f93a6e4SLeon Alrae static uint64_t cpc_read(void *opaque, hwaddr offset, unsigned size)
961f93a6e4SLeon Alrae {
971f93a6e4SLeon Alrae     MIPSCPCState *s = opaque;
981f93a6e4SLeon Alrae 
991f93a6e4SLeon Alrae     switch (offset) {
1001f93a6e4SLeon Alrae     case CPC_CL_BASE_OFS + CPC_VP_RUNNING_OFS:
1011f93a6e4SLeon Alrae     case CPC_CO_BASE_OFS + CPC_VP_RUNNING_OFS:
1021f93a6e4SLeon Alrae         return s->vp_running;
1031f93a6e4SLeon Alrae     default:
1041f93a6e4SLeon Alrae         qemu_log_mask(LOG_UNIMP,
1051f93a6e4SLeon Alrae                       "%s: Bad offset 0x%x\n",  __func__, (int)offset);
1061f93a6e4SLeon Alrae         return 0;
1071f93a6e4SLeon Alrae     }
1081f93a6e4SLeon Alrae }
1091f93a6e4SLeon Alrae 
1101f93a6e4SLeon Alrae static const MemoryRegionOps cpc_ops = {
1111f93a6e4SLeon Alrae     .read = cpc_read,
1121f93a6e4SLeon Alrae     .write = cpc_write,
1131f93a6e4SLeon Alrae     .endianness = DEVICE_NATIVE_ENDIAN,
1141f93a6e4SLeon Alrae     .impl = {
1151f93a6e4SLeon Alrae         .max_access_size = 8,
1161f93a6e4SLeon Alrae     },
1171f93a6e4SLeon Alrae };
1181f93a6e4SLeon Alrae 
1191f93a6e4SLeon Alrae static void mips_cpc_init(Object *obj)
1201f93a6e4SLeon Alrae {
1211f93a6e4SLeon Alrae     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
1221f93a6e4SLeon Alrae     MIPSCPCState *s = MIPS_CPC(obj);
1231f93a6e4SLeon Alrae 
1241f93a6e4SLeon Alrae     memory_region_init_io(&s->mr, OBJECT(s), &cpc_ops, s, "mips-cpc",
1251f93a6e4SLeon Alrae                           CPC_ADDRSPACE_SZ);
1261f93a6e4SLeon Alrae     sysbus_init_mmio(sbd, &s->mr);
1271f93a6e4SLeon Alrae }
1281f93a6e4SLeon Alrae 
1291f93a6e4SLeon Alrae static void mips_cpc_realize(DeviceState *dev, Error **errp)
1301f93a6e4SLeon Alrae {
1311f93a6e4SLeon Alrae     MIPSCPCState *s = MIPS_CPC(dev);
1321f93a6e4SLeon Alrae 
1331f93a6e4SLeon Alrae     if (s->vp_start_running > cpc_vp_run_mask(s)) {
1341f93a6e4SLeon Alrae         error_setg(errp,
1351f93a6e4SLeon Alrae                    "incorrect vp_start_running 0x%" PRIx64 " for num_vp = %d",
1361f93a6e4SLeon Alrae                    s->vp_running, s->num_vp);
1371f93a6e4SLeon Alrae         return;
1381f93a6e4SLeon Alrae     }
1391f93a6e4SLeon Alrae }
1401f93a6e4SLeon Alrae 
1411f93a6e4SLeon Alrae static void mips_cpc_reset(DeviceState *dev)
1421f93a6e4SLeon Alrae {
1431f93a6e4SLeon Alrae     MIPSCPCState *s = MIPS_CPC(dev);
1441f93a6e4SLeon Alrae 
1451f93a6e4SLeon Alrae     /* Reflect the fact that all VPs are halted on reset */
1461f93a6e4SLeon Alrae     s->vp_running = 0;
1471f93a6e4SLeon Alrae 
1481f93a6e4SLeon Alrae     /* Put selected VPs into run state */
1491f93a6e4SLeon Alrae     cpc_run_vp(s, s->vp_start_running);
1501f93a6e4SLeon Alrae }
1511f93a6e4SLeon Alrae 
1521f93a6e4SLeon Alrae static const VMStateDescription vmstate_mips_cpc = {
1531f93a6e4SLeon Alrae     .name = "mips-cpc",
1541f93a6e4SLeon Alrae     .version_id = 0,
1551f93a6e4SLeon Alrae     .minimum_version_id = 0,
1561f93a6e4SLeon Alrae     .fields = (VMStateField[]) {
1571f93a6e4SLeon Alrae         VMSTATE_UINT64(vp_running, MIPSCPCState),
1581f93a6e4SLeon Alrae         VMSTATE_END_OF_LIST()
1591f93a6e4SLeon Alrae     },
1601f93a6e4SLeon Alrae };
1611f93a6e4SLeon Alrae 
1621f93a6e4SLeon Alrae static Property mips_cpc_properties[] = {
1631f93a6e4SLeon Alrae     DEFINE_PROP_UINT32("num-vp", MIPSCPCState, num_vp, 0x1),
1641f93a6e4SLeon Alrae     DEFINE_PROP_UINT64("vp-start-running", MIPSCPCState, vp_start_running, 0x1),
1651f93a6e4SLeon Alrae     DEFINE_PROP_END_OF_LIST(),
1661f93a6e4SLeon Alrae };
1671f93a6e4SLeon Alrae 
1681f93a6e4SLeon Alrae static void mips_cpc_class_init(ObjectClass *klass, void *data)
1691f93a6e4SLeon Alrae {
1701f93a6e4SLeon Alrae     DeviceClass *dc = DEVICE_CLASS(klass);
1711f93a6e4SLeon Alrae 
1721f93a6e4SLeon Alrae     dc->realize = mips_cpc_realize;
1731f93a6e4SLeon Alrae     dc->reset = mips_cpc_reset;
1741f93a6e4SLeon Alrae     dc->vmsd = &vmstate_mips_cpc;
1751f93a6e4SLeon Alrae     dc->props = mips_cpc_properties;
1761f93a6e4SLeon Alrae }
1771f93a6e4SLeon Alrae 
1781f93a6e4SLeon Alrae static const TypeInfo mips_cpc_info = {
1791f93a6e4SLeon Alrae     .name          = TYPE_MIPS_CPC,
1801f93a6e4SLeon Alrae     .parent        = TYPE_SYS_BUS_DEVICE,
1811f93a6e4SLeon Alrae     .instance_size = sizeof(MIPSCPCState),
1821f93a6e4SLeon Alrae     .instance_init = mips_cpc_init,
1831f93a6e4SLeon Alrae     .class_init    = mips_cpc_class_init,
1841f93a6e4SLeon Alrae };
1851f93a6e4SLeon Alrae 
1861f93a6e4SLeon Alrae static void mips_cpc_register_types(void)
1871f93a6e4SLeon Alrae {
1881f93a6e4SLeon Alrae     type_register_static(&mips_cpc_info);
1891f93a6e4SLeon Alrae }
1901f93a6e4SLeon Alrae 
1911f93a6e4SLeon Alrae type_init(mips_cpc_register_types)
192