xref: /openbmc/qemu/hw/misc/mips_cmgcr.c (revision 03dd024ff57733a55cd2e455f361d053c81b1b29)
13994215dSYongbok Kim /*
23994215dSYongbok Kim  * This file is subject to the terms and conditions of the GNU General Public
33994215dSYongbok Kim  * License.  See the file "COPYING" in the main directory of this archive
43994215dSYongbok Kim  * for more details.
53994215dSYongbok Kim  *
63994215dSYongbok Kim  * Copyright (C) 2012  MIPS Technologies, Inc.  All rights reserved.
73994215dSYongbok Kim  * Authors: Sanjay Lal <sanjayl@kymasys.com>
83994215dSYongbok Kim  *
93994215dSYongbok Kim  * Copyright (C) 2015 Imagination Technologies
103994215dSYongbok Kim  */
113994215dSYongbok Kim 
123994215dSYongbok Kim #include "qemu/osdep.h"
133994215dSYongbok Kim #include "qapi/error.h"
14*03dd024fSPaolo Bonzini #include "qemu/log.h"
153994215dSYongbok Kim #include "hw/hw.h"
163994215dSYongbok Kim #include "hw/sysbus.h"
173994215dSYongbok Kim #include "sysemu/sysemu.h"
183994215dSYongbok Kim #include "hw/misc/mips_cmgcr.h"
192edd5261SLeon Alrae #include "hw/misc/mips_cpc.h"
202edd5261SLeon Alrae 
212edd5261SLeon Alrae static inline bool is_cpc_connected(MIPSGCRState *s)
222edd5261SLeon Alrae {
232edd5261SLeon Alrae     return s->cpc_mr != NULL;
242edd5261SLeon Alrae }
252edd5261SLeon Alrae 
262edd5261SLeon Alrae static inline void update_cpc_base(MIPSGCRState *gcr, uint64_t val)
272edd5261SLeon Alrae {
282edd5261SLeon Alrae     if (is_cpc_connected(gcr)) {
292edd5261SLeon Alrae         gcr->cpc_base = val & GCR_CPC_BASE_MSK;
302edd5261SLeon Alrae         memory_region_transaction_begin();
312edd5261SLeon Alrae         memory_region_set_address(gcr->cpc_mr,
322edd5261SLeon Alrae                                   gcr->cpc_base & GCR_CPC_BASE_CPCBASE_MSK);
332edd5261SLeon Alrae         memory_region_set_enabled(gcr->cpc_mr,
342edd5261SLeon Alrae                                   gcr->cpc_base & GCR_CPC_BASE_CPCEN_MSK);
352edd5261SLeon Alrae         memory_region_transaction_commit();
362edd5261SLeon Alrae     }
372edd5261SLeon Alrae }
383994215dSYongbok Kim 
393994215dSYongbok Kim /* Read GCR registers */
403994215dSYongbok Kim static uint64_t gcr_read(void *opaque, hwaddr addr, unsigned size)
413994215dSYongbok Kim {
423994215dSYongbok Kim     MIPSGCRState *gcr = (MIPSGCRState *) opaque;
433994215dSYongbok Kim 
443994215dSYongbok Kim     switch (addr) {
453994215dSYongbok Kim     /* Global Control Block Register */
463994215dSYongbok Kim     case GCR_CONFIG_OFS:
473994215dSYongbok Kim         /* Set PCORES to 0 */
483994215dSYongbok Kim         return 0;
493994215dSYongbok Kim     case GCR_BASE_OFS:
503994215dSYongbok Kim         return gcr->gcr_base;
513994215dSYongbok Kim     case GCR_REV_OFS:
523994215dSYongbok Kim         return gcr->gcr_rev;
532edd5261SLeon Alrae     case GCR_CPC_BASE_OFS:
542edd5261SLeon Alrae         return gcr->cpc_base;
552edd5261SLeon Alrae     case GCR_CPC_STATUS_OFS:
562edd5261SLeon Alrae         return is_cpc_connected(gcr);
573994215dSYongbok Kim     case GCR_L2_CONFIG_OFS:
583994215dSYongbok Kim         /* L2 BYPASS */
593994215dSYongbok Kim         return GCR_L2_CONFIG_BYPASS_MSK;
603994215dSYongbok Kim         /* Core-Local and Core-Other Control Blocks */
613994215dSYongbok Kim     case MIPS_CLCB_OFS + GCR_CL_CONFIG_OFS:
623994215dSYongbok Kim     case MIPS_COCB_OFS + GCR_CL_CONFIG_OFS:
633994215dSYongbok Kim         /* Set PVP to # of VPs - 1 */
643994215dSYongbok Kim         return gcr->num_vps - 1;
653994215dSYongbok Kim     case MIPS_CLCB_OFS + GCR_CL_OTHER_OFS:
663994215dSYongbok Kim         return 0;
673994215dSYongbok Kim     default:
683994215dSYongbok Kim         qemu_log_mask(LOG_UNIMP, "Read %d bytes at GCR offset 0x%" HWADDR_PRIx
693994215dSYongbok Kim                       "\n", size, addr);
703994215dSYongbok Kim         return 0;
713994215dSYongbok Kim     }
723994215dSYongbok Kim     return 0;
733994215dSYongbok Kim }
743994215dSYongbok Kim 
753994215dSYongbok Kim /* Write GCR registers */
763994215dSYongbok Kim static void gcr_write(void *opaque, hwaddr addr, uint64_t data, unsigned size)
773994215dSYongbok Kim {
782edd5261SLeon Alrae     MIPSGCRState *gcr = (MIPSGCRState *)opaque;
792edd5261SLeon Alrae 
803994215dSYongbok Kim     switch (addr) {
812edd5261SLeon Alrae     case GCR_CPC_BASE_OFS:
822edd5261SLeon Alrae         update_cpc_base(gcr, data);
832edd5261SLeon Alrae         break;
843994215dSYongbok Kim     default:
853994215dSYongbok Kim         qemu_log_mask(LOG_UNIMP, "Write %d bytes at GCR offset 0x%" HWADDR_PRIx
863994215dSYongbok Kim                       " 0x%" PRIx64 "\n", size, addr, data);
873994215dSYongbok Kim         break;
883994215dSYongbok Kim     }
893994215dSYongbok Kim }
903994215dSYongbok Kim 
913994215dSYongbok Kim static const MemoryRegionOps gcr_ops = {
923994215dSYongbok Kim     .read = gcr_read,
933994215dSYongbok Kim     .write = gcr_write,
943994215dSYongbok Kim     .endianness = DEVICE_NATIVE_ENDIAN,
953994215dSYongbok Kim     .impl = {
963994215dSYongbok Kim         .max_access_size = 8,
973994215dSYongbok Kim     },
983994215dSYongbok Kim };
993994215dSYongbok Kim 
1003994215dSYongbok Kim static void mips_gcr_init(Object *obj)
1013994215dSYongbok Kim {
1023994215dSYongbok Kim     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
1033994215dSYongbok Kim     MIPSGCRState *s = MIPS_GCR(obj);
1043994215dSYongbok Kim 
1052edd5261SLeon Alrae     object_property_add_link(obj, "cpc", TYPE_MEMORY_REGION,
1062edd5261SLeon Alrae                              (Object **)&s->cpc_mr,
1072edd5261SLeon Alrae                              qdev_prop_allow_set_link_before_realize,
1082edd5261SLeon Alrae                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
1092edd5261SLeon Alrae                              &error_abort);
1102edd5261SLeon Alrae 
1113994215dSYongbok Kim     memory_region_init_io(&s->iomem, OBJECT(s), &gcr_ops, s,
1123994215dSYongbok Kim                           "mips-gcr", GCR_ADDRSPACE_SZ);
1133994215dSYongbok Kim     sysbus_init_mmio(sbd, &s->iomem);
1143994215dSYongbok Kim }
1153994215dSYongbok Kim 
1162edd5261SLeon Alrae static void mips_gcr_reset(DeviceState *dev)
1172edd5261SLeon Alrae {
1182edd5261SLeon Alrae     MIPSGCRState *s = MIPS_GCR(dev);
1192edd5261SLeon Alrae 
1202edd5261SLeon Alrae     update_cpc_base(s, 0);
1212edd5261SLeon Alrae }
1222edd5261SLeon Alrae 
1232edd5261SLeon Alrae static const VMStateDescription vmstate_mips_gcr = {
1242edd5261SLeon Alrae     .name = "mips-gcr",
1252edd5261SLeon Alrae     .version_id = 0,
1262edd5261SLeon Alrae     .minimum_version_id = 0,
1272edd5261SLeon Alrae     .fields = (VMStateField[]) {
1282edd5261SLeon Alrae         VMSTATE_UINT64(cpc_base, MIPSGCRState),
1292edd5261SLeon Alrae         VMSTATE_END_OF_LIST()
1302edd5261SLeon Alrae     },
1312edd5261SLeon Alrae };
1322edd5261SLeon Alrae 
1333994215dSYongbok Kim static Property mips_gcr_properties[] = {
1343994215dSYongbok Kim     DEFINE_PROP_INT32("num-vp", MIPSGCRState, num_vps, 1),
1353994215dSYongbok Kim     DEFINE_PROP_INT32("gcr-rev", MIPSGCRState, gcr_rev, 0x800),
1363994215dSYongbok Kim     DEFINE_PROP_UINT64("gcr-base", MIPSGCRState, gcr_base, GCR_BASE_ADDR),
1373994215dSYongbok Kim     DEFINE_PROP_END_OF_LIST(),
1383994215dSYongbok Kim };
1393994215dSYongbok Kim 
1403994215dSYongbok Kim static void mips_gcr_class_init(ObjectClass *klass, void *data)
1413994215dSYongbok Kim {
1423994215dSYongbok Kim     DeviceClass *dc = DEVICE_CLASS(klass);
1433994215dSYongbok Kim     dc->props = mips_gcr_properties;
1442edd5261SLeon Alrae     dc->vmsd = &vmstate_mips_gcr;
1452edd5261SLeon Alrae     dc->reset = mips_gcr_reset;
1463994215dSYongbok Kim }
1473994215dSYongbok Kim 
1483994215dSYongbok Kim static const TypeInfo mips_gcr_info = {
1493994215dSYongbok Kim     .name          = TYPE_MIPS_GCR,
1503994215dSYongbok Kim     .parent        = TYPE_SYS_BUS_DEVICE,
1513994215dSYongbok Kim     .instance_size = sizeof(MIPSGCRState),
1523994215dSYongbok Kim     .instance_init = mips_gcr_init,
1533994215dSYongbok Kim     .class_init    = mips_gcr_class_init,
1543994215dSYongbok Kim };
1553994215dSYongbok Kim 
1563994215dSYongbok Kim static void mips_gcr_register_types(void)
1573994215dSYongbok Kim {
1583994215dSYongbok Kim     type_register_static(&mips_gcr_info);
1593994215dSYongbok Kim }
1603994215dSYongbok Kim 
1613994215dSYongbok Kim type_init(mips_gcr_register_types)
162