blob: b3ba16694e10e1b68a139e54ad15e8cb0ccf6d9d [file] [log] [blame]
Yongbok Kim39942152016-03-15 09:59:28 +00001/*
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 Bonzini03dd0242015-12-15 13:16:16 +010014#include "qemu/log.h"
Yongbok Kim39942152016-03-15 09:59:28 +000015#include "hw/hw.h"
16#include "hw/sysbus.h"
17#include "sysemu/sysemu.h"
18#include "hw/misc/mips_cmgcr.h"
Leon Alrae2edd5262016-03-15 09:59:31 +000019#include "hw/misc/mips_cpc.h"
Leon Alrae19494f82016-03-28 19:35:52 -070020#include "hw/intc/mips_gic.h"
Leon Alrae2edd5262016-03-15 09:59:31 +000021
22static inline bool is_cpc_connected(MIPSGCRState *s)
23{
24 return s->cpc_mr != NULL;
25}
26
Leon Alrae19494f82016-03-28 19:35:52 -070027static inline bool is_gic_connected(MIPSGCRState *s)
28{
29 return s->gic_mr != NULL;
30}
31
Leon Alrae2edd5262016-03-15 09:59:31 +000032static 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 Kim39942152016-03-15 09:59:28 +000044
Leon Alrae19494f82016-03-28 19:35:52 -070045static 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 Kim39942152016-03-15 09:59:28 +000058/* Read GCR registers */
59static uint64_t gcr_read(void *opaque, hwaddr addr, unsigned size)
60{
61 MIPSGCRState *gcr = (MIPSGCRState *) opaque;
Leon Alraec09199f2016-06-09 10:46:52 +010062 MIPSGCRVPState *current_vps = &gcr->vps[current_cpu->cpu_index];
63 MIPSGCRVPState *other_vps = &gcr->vps[current_vps->other];
Yongbok Kim39942152016-03-15 09:59:28 +000064
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 Alrae19494f82016-03-28 19:35:52 -070074 case GCR_GIC_BASE_OFS:
75 return gcr->gic_base;
Leon Alrae2edd5262016-03-15 09:59:31 +000076 case GCR_CPC_BASE_OFS:
77 return gcr->cpc_base;
Leon Alrae19494f82016-03-28 19:35:52 -070078 case GCR_GIC_STATUS_OFS:
79 return is_gic_connected(gcr);
Leon Alrae2edd5262016-03-15 09:59:31 +000080 case GCR_CPC_STATUS_OFS:
81 return is_cpc_connected(gcr);
Yongbok Kim39942152016-03-15 09:59:28 +000082 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 Alraec09199f2016-06-09 10:46:52 +010090 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 Kim39942152016-03-15 09:59:28 +000094 case MIPS_CLCB_OFS + GCR_CL_OTHER_OFS:
Leon Alraec09199f2016-06-09 10:46:52 +010095 return current_vps->other;
96 case MIPS_COCB_OFS + GCR_CL_OTHER_OFS:
97 return other_vps->other;
Yongbok Kim39942152016-03-15 09:59:28 +000098 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 Alraec09199f2016-06-09 10:46:52 +0100106static 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 Kim39942152016-03-15 09:59:28 +0000112/* Write GCR registers */
113static void gcr_write(void *opaque, hwaddr addr, uint64_t data, unsigned size)
114{
Leon Alrae2edd5262016-03-15 09:59:31 +0000115 MIPSGCRState *gcr = (MIPSGCRState *)opaque;
Leon Alraec09199f2016-06-09 10:46:52 +0100116 MIPSGCRVPState *current_vps = &gcr->vps[current_cpu->cpu_index];
117 MIPSGCRVPState *other_vps = &gcr->vps[current_vps->other];
Leon Alrae2edd5262016-03-15 09:59:31 +0000118
Yongbok Kim39942152016-03-15 09:59:28 +0000119 switch (addr) {
Leon Alrae19494f82016-03-28 19:35:52 -0700120 case GCR_GIC_BASE_OFS:
121 update_gic_base(gcr, data);
122 break;
Leon Alrae2edd5262016-03-15 09:59:31 +0000123 case GCR_CPC_BASE_OFS:
124 update_cpc_base(gcr, data);
125 break;
Leon Alraec09199f2016-06-09 10:46:52 +0100126 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 Kim39942152016-03-15 09:59:28 +0000146 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
153static 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
162static void mips_gcr_init(Object *obj)
163{
164 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
165 MIPSGCRState *s = MIPS_GCR(obj);
166
Leon Alrae19494f82016-03-28 19:35:52 -0700167 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 Alrae2edd5262016-03-15 09:59:31 +0000173 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 Kim39942152016-03-15 09:59:28 +0000179 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 Alrae2edd5262016-03-15 09:59:31 +0000184static void mips_gcr_reset(DeviceState *dev)
185{
186 MIPSGCRState *s = MIPS_GCR(dev);
Leon Alraec09199f2016-06-09 10:46:52 +0100187 int i;
Leon Alrae2edd5262016-03-15 09:59:31 +0000188
Leon Alrae19494f82016-03-28 19:35:52 -0700189 update_gic_base(s, 0);
Leon Alrae2edd5262016-03-15 09:59:31 +0000190 update_cpc_base(s, 0);
Leon Alraec09199f2016-06-09 10:46:52 +0100191
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 Alrae2edd5262016-03-15 09:59:31 +0000197}
198
199static 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 Kim39942152016-03-15 09:59:28 +0000209static 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 Alraec09199f2016-06-09 10:46:52 +0100216static 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 Kim39942152016-03-15 09:59:28 +0000224static void mips_gcr_class_init(ObjectClass *klass, void *data)
225{
226 DeviceClass *dc = DEVICE_CLASS(klass);
227 dc->props = mips_gcr_properties;
Leon Alrae2edd5262016-03-15 09:59:31 +0000228 dc->vmsd = &vmstate_mips_gcr;
229 dc->reset = mips_gcr_reset;
Leon Alraec09199f2016-06-09 10:46:52 +0100230 dc->realize = mips_gcr_realize;
Yongbok Kim39942152016-03-15 09:59:28 +0000231}
232
233static 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
241static void mips_gcr_register_types(void)
242{
243 type_register_static(&mips_gcr_info);
244}
245
246type_init(mips_gcr_register_types)