Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 7 | * Authors: Sanjay Lal <sanjayl@kymasys.com> |
| 8 | * |
| 9 | * Copyright (C) 2015 Imagination Technologies |
| 10 | */ |
| 11 | |
| 12 | #include "qemu/osdep.h" |
| 13 | #include "qapi/error.h" |
Paolo Bonzini | 03dd024 | 2015-12-15 13:16:16 +0100 | [diff] [blame] | 14 | #include "qemu/log.h" |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 15 | #include "hw/hw.h" |
| 16 | #include "hw/sysbus.h" |
| 17 | #include "sysemu/sysemu.h" |
| 18 | #include "hw/misc/mips_cmgcr.h" |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 19 | #include "hw/misc/mips_cpc.h" |
Leon Alrae | 19494f8 | 2016-03-28 19:35:52 -0700 | [diff] [blame] | 20 | #include "hw/intc/mips_gic.h" |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 21 | |
| 22 | static inline bool is_cpc_connected(MIPSGCRState *s) |
| 23 | { |
| 24 | return s->cpc_mr != NULL; |
| 25 | } |
| 26 | |
Leon Alrae | 19494f8 | 2016-03-28 19:35:52 -0700 | [diff] [blame] | 27 | static inline bool is_gic_connected(MIPSGCRState *s) |
| 28 | { |
| 29 | return s->gic_mr != NULL; |
| 30 | } |
| 31 | |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 32 | static inline void update_cpc_base(MIPSGCRState *gcr, uint64_t val) |
| 33 | { |
| 34 | if (is_cpc_connected(gcr)) { |
| 35 | gcr->cpc_base = val & GCR_CPC_BASE_MSK; |
| 36 | memory_region_transaction_begin(); |
| 37 | memory_region_set_address(gcr->cpc_mr, |
| 38 | gcr->cpc_base & GCR_CPC_BASE_CPCBASE_MSK); |
| 39 | memory_region_set_enabled(gcr->cpc_mr, |
| 40 | gcr->cpc_base & GCR_CPC_BASE_CPCEN_MSK); |
| 41 | memory_region_transaction_commit(); |
| 42 | } |
| 43 | } |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 44 | |
Leon Alrae | 19494f8 | 2016-03-28 19:35:52 -0700 | [diff] [blame] | 45 | static inline void update_gic_base(MIPSGCRState *gcr, uint64_t val) |
| 46 | { |
| 47 | if (is_gic_connected(gcr)) { |
| 48 | gcr->gic_base = val & GCR_GIC_BASE_MSK; |
| 49 | memory_region_transaction_begin(); |
| 50 | memory_region_set_address(gcr->gic_mr, |
| 51 | gcr->gic_base & GCR_GIC_BASE_GICBASE_MSK); |
| 52 | memory_region_set_enabled(gcr->gic_mr, |
| 53 | gcr->gic_base & GCR_GIC_BASE_GICEN_MSK); |
| 54 | memory_region_transaction_commit(); |
| 55 | } |
| 56 | } |
| 57 | |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 58 | /* Read GCR registers */ |
| 59 | static uint64_t gcr_read(void *opaque, hwaddr addr, unsigned size) |
| 60 | { |
| 61 | MIPSGCRState *gcr = (MIPSGCRState *) opaque; |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 62 | MIPSGCRVPState *current_vps = &gcr->vps[current_cpu->cpu_index]; |
| 63 | MIPSGCRVPState *other_vps = &gcr->vps[current_vps->other]; |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 64 | |
| 65 | switch (addr) { |
| 66 | /* Global Control Block Register */ |
| 67 | case GCR_CONFIG_OFS: |
| 68 | /* Set PCORES to 0 */ |
| 69 | return 0; |
| 70 | case GCR_BASE_OFS: |
| 71 | return gcr->gcr_base; |
| 72 | case GCR_REV_OFS: |
| 73 | return gcr->gcr_rev; |
Leon Alrae | 19494f8 | 2016-03-28 19:35:52 -0700 | [diff] [blame] | 74 | case GCR_GIC_BASE_OFS: |
| 75 | return gcr->gic_base; |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 76 | case GCR_CPC_BASE_OFS: |
| 77 | return gcr->cpc_base; |
Leon Alrae | 19494f8 | 2016-03-28 19:35:52 -0700 | [diff] [blame] | 78 | case GCR_GIC_STATUS_OFS: |
| 79 | return is_gic_connected(gcr); |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 80 | case GCR_CPC_STATUS_OFS: |
| 81 | return is_cpc_connected(gcr); |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 82 | case GCR_L2_CONFIG_OFS: |
| 83 | /* L2 BYPASS */ |
| 84 | return GCR_L2_CONFIG_BYPASS_MSK; |
| 85 | /* Core-Local and Core-Other Control Blocks */ |
| 86 | case MIPS_CLCB_OFS + GCR_CL_CONFIG_OFS: |
| 87 | case MIPS_COCB_OFS + GCR_CL_CONFIG_OFS: |
| 88 | /* Set PVP to # of VPs - 1 */ |
| 89 | return gcr->num_vps - 1; |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 90 | case MIPS_CLCB_OFS + GCR_CL_RESETBASE_OFS: |
| 91 | return current_vps->reset_base; |
| 92 | case MIPS_COCB_OFS + GCR_CL_RESETBASE_OFS: |
| 93 | return other_vps->reset_base; |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 94 | case MIPS_CLCB_OFS + GCR_CL_OTHER_OFS: |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 95 | return current_vps->other; |
| 96 | case MIPS_COCB_OFS + GCR_CL_OTHER_OFS: |
| 97 | return other_vps->other; |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 98 | default: |
| 99 | qemu_log_mask(LOG_UNIMP, "Read %d bytes at GCR offset 0x%" HWADDR_PRIx |
| 100 | "\n", size, addr); |
| 101 | return 0; |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 106 | static inline target_ulong get_exception_base(MIPSGCRVPState *vps) |
| 107 | { |
| 108 | /* TODO: BEV_BASE and SELECT_BEV */ |
| 109 | return (int32_t)(vps->reset_base & GCR_CL_RESET_BASE_RESETBASE_MSK); |
| 110 | } |
| 111 | |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 112 | /* Write GCR registers */ |
| 113 | static void gcr_write(void *opaque, hwaddr addr, uint64_t data, unsigned size) |
| 114 | { |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 115 | MIPSGCRState *gcr = (MIPSGCRState *)opaque; |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 116 | MIPSGCRVPState *current_vps = &gcr->vps[current_cpu->cpu_index]; |
| 117 | MIPSGCRVPState *other_vps = &gcr->vps[current_vps->other]; |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 118 | |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 119 | switch (addr) { |
Leon Alrae | 19494f8 | 2016-03-28 19:35:52 -0700 | [diff] [blame] | 120 | case GCR_GIC_BASE_OFS: |
| 121 | update_gic_base(gcr, data); |
| 122 | break; |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 123 | case GCR_CPC_BASE_OFS: |
| 124 | update_cpc_base(gcr, data); |
| 125 | break; |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 126 | case MIPS_CLCB_OFS + GCR_CL_RESETBASE_OFS: |
| 127 | current_vps->reset_base = data & GCR_CL_RESET_BASE_MSK; |
| 128 | cpu_set_exception_base(current_cpu->cpu_index, |
| 129 | get_exception_base(current_vps)); |
| 130 | break; |
| 131 | case MIPS_COCB_OFS + GCR_CL_RESETBASE_OFS: |
| 132 | other_vps->reset_base = data & GCR_CL_RESET_BASE_MSK; |
| 133 | cpu_set_exception_base(current_vps->other, |
| 134 | get_exception_base(other_vps)); |
| 135 | break; |
| 136 | case MIPS_CLCB_OFS + GCR_CL_OTHER_OFS: |
| 137 | if ((data & GCR_CL_OTHER_MSK) < gcr->num_vps) { |
| 138 | current_vps->other = data & GCR_CL_OTHER_MSK; |
| 139 | } |
| 140 | break; |
| 141 | case MIPS_COCB_OFS + GCR_CL_OTHER_OFS: |
| 142 | if ((data & GCR_CL_OTHER_MSK) < gcr->num_vps) { |
| 143 | other_vps->other = data & GCR_CL_OTHER_MSK; |
| 144 | } |
| 145 | break; |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 146 | default: |
| 147 | qemu_log_mask(LOG_UNIMP, "Write %d bytes at GCR offset 0x%" HWADDR_PRIx |
| 148 | " 0x%" PRIx64 "\n", size, addr, data); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static const MemoryRegionOps gcr_ops = { |
| 154 | .read = gcr_read, |
| 155 | .write = gcr_write, |
| 156 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 157 | .impl = { |
| 158 | .max_access_size = 8, |
| 159 | }, |
| 160 | }; |
| 161 | |
| 162 | static void mips_gcr_init(Object *obj) |
| 163 | { |
| 164 | SysBusDevice *sbd = SYS_BUS_DEVICE(obj); |
| 165 | MIPSGCRState *s = MIPS_GCR(obj); |
| 166 | |
Leon Alrae | 19494f8 | 2016-03-28 19:35:52 -0700 | [diff] [blame] | 167 | object_property_add_link(obj, "gic", TYPE_MEMORY_REGION, |
| 168 | (Object **)&s->gic_mr, |
| 169 | qdev_prop_allow_set_link_before_realize, |
| 170 | OBJ_PROP_LINK_UNREF_ON_RELEASE, |
| 171 | &error_abort); |
| 172 | |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 173 | object_property_add_link(obj, "cpc", TYPE_MEMORY_REGION, |
| 174 | (Object **)&s->cpc_mr, |
| 175 | qdev_prop_allow_set_link_before_realize, |
| 176 | OBJ_PROP_LINK_UNREF_ON_RELEASE, |
| 177 | &error_abort); |
| 178 | |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 179 | memory_region_init_io(&s->iomem, OBJECT(s), &gcr_ops, s, |
| 180 | "mips-gcr", GCR_ADDRSPACE_SZ); |
| 181 | sysbus_init_mmio(sbd, &s->iomem); |
| 182 | } |
| 183 | |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 184 | static void mips_gcr_reset(DeviceState *dev) |
| 185 | { |
| 186 | MIPSGCRState *s = MIPS_GCR(dev); |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 187 | int i; |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 188 | |
Leon Alrae | 19494f8 | 2016-03-28 19:35:52 -0700 | [diff] [blame] | 189 | update_gic_base(s, 0); |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 190 | update_cpc_base(s, 0); |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 191 | |
| 192 | for (i = 0; i < s->num_vps; i++) { |
| 193 | s->vps[i].other = 0; |
| 194 | s->vps[i].reset_base = 0xBFC00000 & GCR_CL_RESET_BASE_MSK; |
| 195 | cpu_set_exception_base(i, get_exception_base(&s->vps[i])); |
| 196 | } |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static const VMStateDescription vmstate_mips_gcr = { |
| 200 | .name = "mips-gcr", |
| 201 | .version_id = 0, |
| 202 | .minimum_version_id = 0, |
| 203 | .fields = (VMStateField[]) { |
| 204 | VMSTATE_UINT64(cpc_base, MIPSGCRState), |
| 205 | VMSTATE_END_OF_LIST() |
| 206 | }, |
| 207 | }; |
| 208 | |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 209 | static Property mips_gcr_properties[] = { |
| 210 | DEFINE_PROP_INT32("num-vp", MIPSGCRState, num_vps, 1), |
| 211 | DEFINE_PROP_INT32("gcr-rev", MIPSGCRState, gcr_rev, 0x800), |
| 212 | DEFINE_PROP_UINT64("gcr-base", MIPSGCRState, gcr_base, GCR_BASE_ADDR), |
| 213 | DEFINE_PROP_END_OF_LIST(), |
| 214 | }; |
| 215 | |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 216 | static void mips_gcr_realize(DeviceState *dev, Error **errp) |
| 217 | { |
| 218 | MIPSGCRState *s = MIPS_GCR(dev); |
| 219 | |
| 220 | /* Create local set of registers for each VP */ |
| 221 | s->vps = g_new(MIPSGCRVPState, s->num_vps); |
| 222 | } |
| 223 | |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 224 | static void mips_gcr_class_init(ObjectClass *klass, void *data) |
| 225 | { |
| 226 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 227 | dc->props = mips_gcr_properties; |
Leon Alrae | 2edd526 | 2016-03-15 09:59:31 +0000 | [diff] [blame] | 228 | dc->vmsd = &vmstate_mips_gcr; |
| 229 | dc->reset = mips_gcr_reset; |
Leon Alrae | c09199f | 2016-06-09 10:46:52 +0100 | [diff] [blame] | 230 | dc->realize = mips_gcr_realize; |
Yongbok Kim | 3994215 | 2016-03-15 09:59:28 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static const TypeInfo mips_gcr_info = { |
| 234 | .name = TYPE_MIPS_GCR, |
| 235 | .parent = TYPE_SYS_BUS_DEVICE, |
| 236 | .instance_size = sizeof(MIPSGCRState), |
| 237 | .instance_init = mips_gcr_init, |
| 238 | .class_init = mips_gcr_class_init, |
| 239 | }; |
| 240 | |
| 241 | static void mips_gcr_register_types(void) |
| 242 | { |
| 243 | type_register_static(&mips_gcr_info); |
| 244 | } |
| 245 | |
| 246 | type_init(mips_gcr_register_types) |