gic_v3_cpu_interface.cc (13926:d6ebddee93a7) gic_v3_cpu_interface.cc (14057:786dbd2c3bfc)
1/*
2 * Copyright (c) 2018 Metempsy Technology Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Jairo Balart
29 */
30
31#include "dev/arm/gic_v3_cpu_interface.hh"
32
33#include "arch/arm/isa.hh"
34#include "debug/GIC.hh"
35#include "dev/arm/gic_v3.hh"
36#include "dev/arm/gic_v3_distributor.hh"
37#include "dev/arm/gic_v3_redistributor.hh"
38
39const uint8_t Gicv3CPUInterface::GIC_MIN_BPR;
40const uint8_t Gicv3CPUInterface::GIC_MIN_BPR_NS;
41
42Gicv3CPUInterface::Gicv3CPUInterface(Gicv3 * gic, uint32_t cpu_id)
43 : BaseISADevice(),
44 gic(gic),
45 redistributor(nullptr),
46 distributor(nullptr),
47 cpuId(cpu_id)
48{
49}
50
51void
52Gicv3CPUInterface::init()
53{
54 redistributor = gic->getRedistributor(cpuId);
55 distributor = gic->getDistributor();
56}
57
58void
59Gicv3CPUInterface::initState()
60{
61 reset();
62}
63
64void
65Gicv3CPUInterface::reset()
66{
67 hppi.prio = 0xff;
68}
69
70void
71Gicv3CPUInterface::setThreadContext(ThreadContext *tc)
72{
73 maintenanceInterrupt = gic->params()->maint_int->get(tc);
74}
75
76bool
77Gicv3CPUInterface::getHCREL2FMO() const
78{
79 HCR hcr = isa->readMiscRegNoEffect(MISCREG_HCR_EL2);
80
81 if (hcr.tge && hcr.e2h) {
82 return false;
83 } else if (hcr.tge) {
84 return true;
85 } else {
86 return hcr.fmo;
87 }
88}
89
90bool
91Gicv3CPUInterface::getHCREL2IMO() const
92{
93 HCR hcr = isa->readMiscRegNoEffect(MISCREG_HCR_EL2);
94
95 if (hcr.tge && hcr.e2h) {
96 return false;
97 } else if (hcr.tge) {
98 return true;
99 } else {
100 return hcr.imo;
101 }
102}
103
104RegVal
105Gicv3CPUInterface::readMiscReg(int misc_reg)
106{
107 RegVal value = isa->readMiscRegNoEffect(misc_reg);
108 bool hcr_fmo = getHCREL2FMO();
109 bool hcr_imo = getHCREL2IMO();
110
111 switch (misc_reg) {
112 // Active Priorities Group 1 Registers
113 case MISCREG_ICC_AP1R0:
114 case MISCREG_ICC_AP1R0_EL1: {
115 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
116 return isa->readMiscRegNoEffect(MISCREG_ICV_AP1R0_EL1);
117 }
118
119 break;
120 }
121
122 case MISCREG_ICC_AP1R1:
123 case MISCREG_ICC_AP1R1_EL1:
124
125 // only implemented if supporting 6 or more bits of priority
126 case MISCREG_ICC_AP1R2:
127 case MISCREG_ICC_AP1R2_EL1:
128
129 // only implemented if supporting 7 or more bits of priority
130 case MISCREG_ICC_AP1R3:
131 case MISCREG_ICC_AP1R3_EL1:
132 // only implemented if supporting 7 or more bits of priority
133 return 0;
134
135 // Active Priorities Group 0 Registers
136 case MISCREG_ICC_AP0R0:
137 case MISCREG_ICC_AP0R0_EL1: {
138 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
139 return isa->readMiscRegNoEffect(MISCREG_ICV_AP0R0_EL1);
140 }
141
142 break;
143 }
144
145 case MISCREG_ICC_AP0R1:
146 case MISCREG_ICC_AP0R1_EL1:
147
148 // only implemented if supporting 6 or more bits of priority
149 case MISCREG_ICC_AP0R2:
150 case MISCREG_ICC_AP0R2_EL1:
151
152 // only implemented if supporting 7 or more bits of priority
153 case MISCREG_ICC_AP0R3:
154 case MISCREG_ICC_AP0R3_EL1:
155 // only implemented if supporting 7 or more bits of priority
156 return 0;
157
158 // Interrupt Group 0 Enable register EL1
159 case MISCREG_ICC_IGRPEN0:
160 case MISCREG_ICC_IGRPEN0_EL1: {
161 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
1/*
2 * Copyright (c) 2018 Metempsy Technology Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Jairo Balart
29 */
30
31#include "dev/arm/gic_v3_cpu_interface.hh"
32
33#include "arch/arm/isa.hh"
34#include "debug/GIC.hh"
35#include "dev/arm/gic_v3.hh"
36#include "dev/arm/gic_v3_distributor.hh"
37#include "dev/arm/gic_v3_redistributor.hh"
38
39const uint8_t Gicv3CPUInterface::GIC_MIN_BPR;
40const uint8_t Gicv3CPUInterface::GIC_MIN_BPR_NS;
41
42Gicv3CPUInterface::Gicv3CPUInterface(Gicv3 * gic, uint32_t cpu_id)
43 : BaseISADevice(),
44 gic(gic),
45 redistributor(nullptr),
46 distributor(nullptr),
47 cpuId(cpu_id)
48{
49}
50
51void
52Gicv3CPUInterface::init()
53{
54 redistributor = gic->getRedistributor(cpuId);
55 distributor = gic->getDistributor();
56}
57
58void
59Gicv3CPUInterface::initState()
60{
61 reset();
62}
63
64void
65Gicv3CPUInterface::reset()
66{
67 hppi.prio = 0xff;
68}
69
70void
71Gicv3CPUInterface::setThreadContext(ThreadContext *tc)
72{
73 maintenanceInterrupt = gic->params()->maint_int->get(tc);
74}
75
76bool
77Gicv3CPUInterface::getHCREL2FMO() const
78{
79 HCR hcr = isa->readMiscRegNoEffect(MISCREG_HCR_EL2);
80
81 if (hcr.tge && hcr.e2h) {
82 return false;
83 } else if (hcr.tge) {
84 return true;
85 } else {
86 return hcr.fmo;
87 }
88}
89
90bool
91Gicv3CPUInterface::getHCREL2IMO() const
92{
93 HCR hcr = isa->readMiscRegNoEffect(MISCREG_HCR_EL2);
94
95 if (hcr.tge && hcr.e2h) {
96 return false;
97 } else if (hcr.tge) {
98 return true;
99 } else {
100 return hcr.imo;
101 }
102}
103
104RegVal
105Gicv3CPUInterface::readMiscReg(int misc_reg)
106{
107 RegVal value = isa->readMiscRegNoEffect(misc_reg);
108 bool hcr_fmo = getHCREL2FMO();
109 bool hcr_imo = getHCREL2IMO();
110
111 switch (misc_reg) {
112 // Active Priorities Group 1 Registers
113 case MISCREG_ICC_AP1R0:
114 case MISCREG_ICC_AP1R0_EL1: {
115 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
116 return isa->readMiscRegNoEffect(MISCREG_ICV_AP1R0_EL1);
117 }
118
119 break;
120 }
121
122 case MISCREG_ICC_AP1R1:
123 case MISCREG_ICC_AP1R1_EL1:
124
125 // only implemented if supporting 6 or more bits of priority
126 case MISCREG_ICC_AP1R2:
127 case MISCREG_ICC_AP1R2_EL1:
128
129 // only implemented if supporting 7 or more bits of priority
130 case MISCREG_ICC_AP1R3:
131 case MISCREG_ICC_AP1R3_EL1:
132 // only implemented if supporting 7 or more bits of priority
133 return 0;
134
135 // Active Priorities Group 0 Registers
136 case MISCREG_ICC_AP0R0:
137 case MISCREG_ICC_AP0R0_EL1: {
138 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
139 return isa->readMiscRegNoEffect(MISCREG_ICV_AP0R0_EL1);
140 }
141
142 break;
143 }
144
145 case MISCREG_ICC_AP0R1:
146 case MISCREG_ICC_AP0R1_EL1:
147
148 // only implemented if supporting 6 or more bits of priority
149 case MISCREG_ICC_AP0R2:
150 case MISCREG_ICC_AP0R2_EL1:
151
152 // only implemented if supporting 7 or more bits of priority
153 case MISCREG_ICC_AP0R3:
154 case MISCREG_ICC_AP0R3_EL1:
155 // only implemented if supporting 7 or more bits of priority
156 return 0;
157
158 // Interrupt Group 0 Enable register EL1
159 case MISCREG_ICC_IGRPEN0:
160 case MISCREG_ICC_IGRPEN0_EL1: {
161 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
162 return isa->readMiscRegNoEffect(MISCREG_ICV_IGRPEN0_EL1);
162 return readMiscReg(MISCREG_ICV_IGRPEN0_EL1);
163 }
164
165 break;
166 }
167
163 }
164
165 break;
166 }
167
168 case MISCREG_ICV_IGRPEN0_EL1: {
169 ICH_VMCR_EL2 ich_vmcr_el2 =
170 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
171 value = ich_vmcr_el2.VENG0;
172 break;
173 }
174
168 // Interrupt Group 1 Enable register EL1
169 case MISCREG_ICC_IGRPEN1:
170 case MISCREG_ICC_IGRPEN1_EL1: {
171 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
175 // Interrupt Group 1 Enable register EL1
176 case MISCREG_ICC_IGRPEN1:
177 case MISCREG_ICC_IGRPEN1_EL1: {
178 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
172 return isa->readMiscRegNoEffect(MISCREG_ICV_IGRPEN1_EL1);
179 return readMiscReg(MISCREG_ICV_IGRPEN1_EL1);
173 }
174
175 break;
176 }
177
180 }
181
182 break;
183 }
184
185 case MISCREG_ICV_IGRPEN1_EL1: {
186 ICH_VMCR_EL2 ich_vmcr_el2 =
187 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
188 value = ich_vmcr_el2.VENG1;
189 break;
190 }
191
178 // Interrupt Group 1 Enable register EL3
179 case MISCREG_ICC_MGRPEN1:
180 case MISCREG_ICC_IGRPEN1_EL3:
181 break;
182
183 // Running Priority Register
184 case MISCREG_ICC_RPR:
185 case MISCREG_ICC_RPR_EL1: {
186 if ((currEL() == EL1) && !inSecureState() &&
187 (hcr_imo || hcr_fmo)) {
188 return readMiscReg(MISCREG_ICV_RPR_EL1);
189 }
190
191 uint8_t rprio = highestActivePriority();
192
193 if (haveEL(EL3) && !inSecureState() &&
194 (isa->readMiscRegNoEffect(MISCREG_SCR_EL3) & (1U << 2))) {
195 // Spec section 4.8.1
196 // For Non-secure access to ICC_RPR_EL1 when SCR_EL3.FIQ == 1
197 if ((rprio & 0x80) == 0) {
198 // If the current priority mask value is in the range of
199 // 0x00-0x7F a read access returns the value 0x0
200 rprio = 0;
201 } else if (rprio != 0xff) {
202 // If the current priority mask value is in the range of
203 // 0x80-0xFF a read access returns the Non-secure read of
204 // the current value
205 rprio = (rprio << 1) & 0xff;
206 }
207 }
208
209 value = rprio;
210 break;
211 }
212
213 // Virtual Running Priority Register
214 case MISCREG_ICV_RPR_EL1: {
215 value = virtualHighestActivePriority();
216 break;
217 }
218
219 // Highest Priority Pending Interrupt Register 0
220 case MISCREG_ICC_HPPIR0:
221 case MISCREG_ICC_HPPIR0_EL1: {
222 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
223 return readMiscReg(MISCREG_ICV_HPPIR0_EL1);
224 }
225
226 value = getHPPIR0();
227 break;
228 }
229
230 // Virtual Highest Priority Pending Interrupt Register 0
231 case MISCREG_ICV_HPPIR0_EL1: {
232 value = Gicv3::INTID_SPURIOUS;
233 int lr_idx = getHPPVILR();
234
235 if (lr_idx >= 0) {
236 ICH_LR_EL2 ich_lr_el2 =
237 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
238 Gicv3::GroupId group =
239 ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
240
241 if (group == Gicv3::G0S) {
242 value = ich_lr_el2.vINTID;
243 }
244 }
245
246 break;
247 }
248
249 // Highest Priority Pending Interrupt Register 1
250 case MISCREG_ICC_HPPIR1:
251 case MISCREG_ICC_HPPIR1_EL1: {
252 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
253 return readMiscReg(MISCREG_ICV_HPPIR1_EL1);
254 }
255
256 value = getHPPIR1();
257 break;
258 }
259
260 // Virtual Highest Priority Pending Interrupt Register 1
261 case MISCREG_ICV_HPPIR1_EL1: {
262 value = Gicv3::INTID_SPURIOUS;
263 int lr_idx = getHPPVILR();
264
265 if (lr_idx >= 0) {
266 ICH_LR_EL2 ich_lr_el2 =
267 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
268 Gicv3::GroupId group =
269 ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
270
271 if (group == Gicv3::G1NS) {
272 value = ich_lr_el2.vINTID;
273 }
274 }
275
276 break;
277 }
278
279 // Binary Point Register 0
280 case MISCREG_ICC_BPR0:
281 case MISCREG_ICC_BPR0_EL1:
282 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
283 return readMiscReg(MISCREG_ICV_BPR0_EL1);
284 }
285
286 M5_FALLTHROUGH;
287
288 // Binary Point Register 1
289 case MISCREG_ICC_BPR1:
290 case MISCREG_ICC_BPR1_EL1: {
291 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
292 return readMiscReg(MISCREG_ICV_BPR1_EL1);
293 }
294
295 Gicv3::GroupId group =
296 misc_reg == MISCREG_ICC_BPR0_EL1 ? Gicv3::G0S : Gicv3::G1S;
297
298 if (group == Gicv3::G1S && !inSecureState()) {
299 group = Gicv3::G1NS;
300 }
301
302 ICC_CTLR_EL1 icc_ctlr_el1_s =
303 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S);
304
305 if ((group == Gicv3::G1S) && !isEL3OrMon() &&
306 icc_ctlr_el1_s.CBPR) {
307 group = Gicv3::G0S;
308 }
309
310 bool sat_inc = false;
311
312 ICC_CTLR_EL1 icc_ctlr_el1_ns =
313 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS);
314
315 if ((group == Gicv3::G1NS) && (currEL() < EL3) &&
316 icc_ctlr_el1_ns.CBPR) {
317 // Reads return BPR0 + 1 saturated to 7, WI
318 group = Gicv3::G0S;
319 sat_inc = true;
320 }
321
322 uint8_t bpr;
323
324 if (group == Gicv3::G0S) {
325 bpr = isa->readMiscRegNoEffect(MISCREG_ICC_BPR0_EL1);
326 } else {
327 bpr = isa->readMiscRegNoEffect(MISCREG_ICC_BPR1_EL1);
328 bpr = std::max(bpr, group == Gicv3::G1S ?
329 GIC_MIN_BPR : GIC_MIN_BPR_NS);
330 }
331
332 if (sat_inc) {
333 bpr++;
334
335 if (bpr > 7) {
336 bpr = 7;
337 }
338 }
339
340 value = bpr;
341 break;
342 }
343
344 // Virtual Binary Point Register 1
345 case MISCREG_ICV_BPR0_EL1:
346 case MISCREG_ICV_BPR1_EL1: {
347 Gicv3::GroupId group =
348 misc_reg == MISCREG_ICV_BPR0_EL1 ? Gicv3::G0S : Gicv3::G1NS;
349 ICH_VMCR_EL2 ich_vmcr_el2 =
350 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
351 bool sat_inc = false;
352
353 if ((group == Gicv3::G1NS) && ich_vmcr_el2.VCBPR) {
354 // bpr0 + 1 saturated to 7, WI
355 group = Gicv3::G0S;
356 sat_inc = true;
357 }
358
359 uint8_t vbpr;
360
361 if (group == Gicv3::G0S) {
362 vbpr = ich_vmcr_el2.VBPR0;
363 } else {
364 vbpr = ich_vmcr_el2.VBPR1;
365 }
366
367 if (sat_inc) {
368 vbpr++;
369
370 if (vbpr > 7) {
371 vbpr = 7;
372 }
373 }
374
375 value = vbpr;
376 break;
377 }
378
379 // Interrupt Priority Mask Register
380 case MISCREG_ICC_PMR:
381 case MISCREG_ICC_PMR_EL1:
382 if ((currEL() == EL1) && !inSecureState() && (hcr_imo || hcr_fmo)) {
192 // Interrupt Group 1 Enable register EL3
193 case MISCREG_ICC_MGRPEN1:
194 case MISCREG_ICC_IGRPEN1_EL3:
195 break;
196
197 // Running Priority Register
198 case MISCREG_ICC_RPR:
199 case MISCREG_ICC_RPR_EL1: {
200 if ((currEL() == EL1) && !inSecureState() &&
201 (hcr_imo || hcr_fmo)) {
202 return readMiscReg(MISCREG_ICV_RPR_EL1);
203 }
204
205 uint8_t rprio = highestActivePriority();
206
207 if (haveEL(EL3) && !inSecureState() &&
208 (isa->readMiscRegNoEffect(MISCREG_SCR_EL3) & (1U << 2))) {
209 // Spec section 4.8.1
210 // For Non-secure access to ICC_RPR_EL1 when SCR_EL3.FIQ == 1
211 if ((rprio & 0x80) == 0) {
212 // If the current priority mask value is in the range of
213 // 0x00-0x7F a read access returns the value 0x0
214 rprio = 0;
215 } else if (rprio != 0xff) {
216 // If the current priority mask value is in the range of
217 // 0x80-0xFF a read access returns the Non-secure read of
218 // the current value
219 rprio = (rprio << 1) & 0xff;
220 }
221 }
222
223 value = rprio;
224 break;
225 }
226
227 // Virtual Running Priority Register
228 case MISCREG_ICV_RPR_EL1: {
229 value = virtualHighestActivePriority();
230 break;
231 }
232
233 // Highest Priority Pending Interrupt Register 0
234 case MISCREG_ICC_HPPIR0:
235 case MISCREG_ICC_HPPIR0_EL1: {
236 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
237 return readMiscReg(MISCREG_ICV_HPPIR0_EL1);
238 }
239
240 value = getHPPIR0();
241 break;
242 }
243
244 // Virtual Highest Priority Pending Interrupt Register 0
245 case MISCREG_ICV_HPPIR0_EL1: {
246 value = Gicv3::INTID_SPURIOUS;
247 int lr_idx = getHPPVILR();
248
249 if (lr_idx >= 0) {
250 ICH_LR_EL2 ich_lr_el2 =
251 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
252 Gicv3::GroupId group =
253 ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
254
255 if (group == Gicv3::G0S) {
256 value = ich_lr_el2.vINTID;
257 }
258 }
259
260 break;
261 }
262
263 // Highest Priority Pending Interrupt Register 1
264 case MISCREG_ICC_HPPIR1:
265 case MISCREG_ICC_HPPIR1_EL1: {
266 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
267 return readMiscReg(MISCREG_ICV_HPPIR1_EL1);
268 }
269
270 value = getHPPIR1();
271 break;
272 }
273
274 // Virtual Highest Priority Pending Interrupt Register 1
275 case MISCREG_ICV_HPPIR1_EL1: {
276 value = Gicv3::INTID_SPURIOUS;
277 int lr_idx = getHPPVILR();
278
279 if (lr_idx >= 0) {
280 ICH_LR_EL2 ich_lr_el2 =
281 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
282 Gicv3::GroupId group =
283 ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
284
285 if (group == Gicv3::G1NS) {
286 value = ich_lr_el2.vINTID;
287 }
288 }
289
290 break;
291 }
292
293 // Binary Point Register 0
294 case MISCREG_ICC_BPR0:
295 case MISCREG_ICC_BPR0_EL1:
296 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
297 return readMiscReg(MISCREG_ICV_BPR0_EL1);
298 }
299
300 M5_FALLTHROUGH;
301
302 // Binary Point Register 1
303 case MISCREG_ICC_BPR1:
304 case MISCREG_ICC_BPR1_EL1: {
305 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
306 return readMiscReg(MISCREG_ICV_BPR1_EL1);
307 }
308
309 Gicv3::GroupId group =
310 misc_reg == MISCREG_ICC_BPR0_EL1 ? Gicv3::G0S : Gicv3::G1S;
311
312 if (group == Gicv3::G1S && !inSecureState()) {
313 group = Gicv3::G1NS;
314 }
315
316 ICC_CTLR_EL1 icc_ctlr_el1_s =
317 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S);
318
319 if ((group == Gicv3::G1S) && !isEL3OrMon() &&
320 icc_ctlr_el1_s.CBPR) {
321 group = Gicv3::G0S;
322 }
323
324 bool sat_inc = false;
325
326 ICC_CTLR_EL1 icc_ctlr_el1_ns =
327 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS);
328
329 if ((group == Gicv3::G1NS) && (currEL() < EL3) &&
330 icc_ctlr_el1_ns.CBPR) {
331 // Reads return BPR0 + 1 saturated to 7, WI
332 group = Gicv3::G0S;
333 sat_inc = true;
334 }
335
336 uint8_t bpr;
337
338 if (group == Gicv3::G0S) {
339 bpr = isa->readMiscRegNoEffect(MISCREG_ICC_BPR0_EL1);
340 } else {
341 bpr = isa->readMiscRegNoEffect(MISCREG_ICC_BPR1_EL1);
342 bpr = std::max(bpr, group == Gicv3::G1S ?
343 GIC_MIN_BPR : GIC_MIN_BPR_NS);
344 }
345
346 if (sat_inc) {
347 bpr++;
348
349 if (bpr > 7) {
350 bpr = 7;
351 }
352 }
353
354 value = bpr;
355 break;
356 }
357
358 // Virtual Binary Point Register 1
359 case MISCREG_ICV_BPR0_EL1:
360 case MISCREG_ICV_BPR1_EL1: {
361 Gicv3::GroupId group =
362 misc_reg == MISCREG_ICV_BPR0_EL1 ? Gicv3::G0S : Gicv3::G1NS;
363 ICH_VMCR_EL2 ich_vmcr_el2 =
364 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
365 bool sat_inc = false;
366
367 if ((group == Gicv3::G1NS) && ich_vmcr_el2.VCBPR) {
368 // bpr0 + 1 saturated to 7, WI
369 group = Gicv3::G0S;
370 sat_inc = true;
371 }
372
373 uint8_t vbpr;
374
375 if (group == Gicv3::G0S) {
376 vbpr = ich_vmcr_el2.VBPR0;
377 } else {
378 vbpr = ich_vmcr_el2.VBPR1;
379 }
380
381 if (sat_inc) {
382 vbpr++;
383
384 if (vbpr > 7) {
385 vbpr = 7;
386 }
387 }
388
389 value = vbpr;
390 break;
391 }
392
393 // Interrupt Priority Mask Register
394 case MISCREG_ICC_PMR:
395 case MISCREG_ICC_PMR_EL1:
396 if ((currEL() == EL1) && !inSecureState() && (hcr_imo || hcr_fmo)) {
383 return isa->readMiscRegNoEffect(MISCREG_ICV_PMR_EL1);
397 return readMiscReg(MISCREG_ICV_PMR_EL1);
384 }
385
386 if (haveEL(EL3) && !inSecureState() &&
387 (isa->readMiscRegNoEffect(MISCREG_SCR_EL3) & (1U << 2))) {
388 // Spec section 4.8.1
389 // For Non-secure access to ICC_PMR_EL1 when SCR_EL3.FIQ == 1:
390 if ((value & 0x80) == 0) {
391 // If the current priority mask value is in the range of
392 // 0x00-0x7F a read access returns the value 0x00.
393 value = 0;
394 } else if (value != 0xff) {
395 // If the current priority mask value is in the range of
396 // 0x80-0xFF a read access returns the Non-secure read of the
397 // current value.
398 value = (value << 1) & 0xff;
399 }
400 }
401
402 break;
403
398 }
399
400 if (haveEL(EL3) && !inSecureState() &&
401 (isa->readMiscRegNoEffect(MISCREG_SCR_EL3) & (1U << 2))) {
402 // Spec section 4.8.1
403 // For Non-secure access to ICC_PMR_EL1 when SCR_EL3.FIQ == 1:
404 if ((value & 0x80) == 0) {
405 // If the current priority mask value is in the range of
406 // 0x00-0x7F a read access returns the value 0x00.
407 value = 0;
408 } else if (value != 0xff) {
409 // If the current priority mask value is in the range of
410 // 0x80-0xFF a read access returns the Non-secure read of the
411 // current value.
412 value = (value << 1) & 0xff;
413 }
414 }
415
416 break;
417
418 case MISCREG_ICV_PMR_EL1: { // Priority Mask Register
419 ICH_VMCR_EL2 ich_vmcr_el2 =
420 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
421
422 value = ich_vmcr_el2.VPMR;
423 break;
424 }
425
404 // Interrupt Acknowledge Register 0
405 case MISCREG_ICC_IAR0:
406 case MISCREG_ICC_IAR0_EL1: {
407 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
408 return readMiscReg(MISCREG_ICV_IAR0_EL1);
409 }
410
411 uint32_t int_id;
412
413 if (hppiCanPreempt()) {
414 int_id = getHPPIR0();
415
416 // avoid activation for special interrupts
417 if (int_id < Gicv3::INTID_SECURE ||
418 int_id >= Gicv3Redistributor::SMALLEST_LPI_ID) {
419 activateIRQ(int_id, hppi.group);
420 }
421 } else {
422 int_id = Gicv3::INTID_SPURIOUS;
423 }
424
425 value = int_id;
426 break;
427 }
428
429 // Virtual Interrupt Acknowledge Register 0
430 case MISCREG_ICV_IAR0_EL1: {
431 int lr_idx = getHPPVILR();
432 uint32_t int_id = Gicv3::INTID_SPURIOUS;
433
434 if (lr_idx >= 0) {
435 ICH_LR_EL2 ich_lr_el2 =
436 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
437
438 if (!ich_lr_el2.Group && hppviCanPreempt(lr_idx)) {
439 int_id = ich_lr_el2.vINTID;
440
441 if (int_id < Gicv3::INTID_SECURE ||
442 int_id > Gicv3::INTID_SPURIOUS) {
443 virtualActivateIRQ(lr_idx);
444 } else {
445 // Bogus... Pseudocode says:
446 // - Move from pending to invalid...
447 // - Return de bogus id...
448 ich_lr_el2.State = ICH_LR_EL2_STATE_INVALID;
449 isa->setMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx,
450 ich_lr_el2);
451 }
452 }
453 }
454
455 value = int_id;
456 virtualUpdate();
457 break;
458 }
459
460 // Interrupt Acknowledge Register 1
461 case MISCREG_ICC_IAR1:
462 case MISCREG_ICC_IAR1_EL1: {
463 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
464 return readMiscReg(MISCREG_ICV_IAR1_EL1);
465 }
466
467 uint32_t int_id;
468
469 if (hppiCanPreempt()) {
470 int_id = getHPPIR1();
471
472 // avoid activation for special interrupts
473 if (int_id < Gicv3::INTID_SECURE ||
474 int_id >= Gicv3Redistributor::SMALLEST_LPI_ID) {
475 activateIRQ(int_id, hppi.group);
476 }
477 } else {
478 int_id = Gicv3::INTID_SPURIOUS;
479 }
480
481 value = int_id;
482 break;
483 }
484
485 // Virtual Interrupt Acknowledge Register 1
486 case MISCREG_ICV_IAR1_EL1: {
487 int lr_idx = getHPPVILR();
488 uint32_t int_id = Gicv3::INTID_SPURIOUS;
489
490 if (lr_idx >= 0) {
491 ICH_LR_EL2 ich_lr_el2 =
492 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
493
494 if (ich_lr_el2.Group && hppviCanPreempt(lr_idx)) {
495 int_id = ich_lr_el2.vINTID;
496
497 if (int_id < Gicv3::INTID_SECURE ||
498 int_id > Gicv3::INTID_SPURIOUS) {
499 virtualActivateIRQ(lr_idx);
500 } else {
501 // Bogus... Pseudocode says:
502 // - Move from pending to invalid...
503 // - Return de bogus id...
504 ich_lr_el2.State = ICH_LR_EL2_STATE_INVALID;
505 isa->setMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx,
506 ich_lr_el2);
507 }
508 }
509 }
510
511 value = int_id;
512 virtualUpdate();
513 break;
514 }
515
516 // System Register Enable Register EL1
517 case MISCREG_ICC_SRE:
518 case MISCREG_ICC_SRE_EL1: {
519 /*
520 * DIB [2] == 1 (IRQ bypass not supported, RAO/WI)
521 * DFB [1] == 1 (FIQ bypass not supported, RAO/WI)
522 * SRE [0] == 1 (Only system register interface supported, RAO/WI)
523 */
524 ICC_SRE_EL1 icc_sre_el1 = 0;
525 icc_sre_el1.SRE = 1;
526 icc_sre_el1.DIB = 1;
527 icc_sre_el1.DFB = 1;
528 value = icc_sre_el1;
529 break;
530 }
531
532 // System Register Enable Register EL2
533 case MISCREG_ICC_HSRE:
534 case MISCREG_ICC_SRE_EL2: {
535 /*
536 * Enable [3] == 1
537 * (EL1 accesses to ICC_SRE_EL1 do not trap to EL2, RAO/WI)
538 * DIB [2] == 1 (IRQ bypass not supported, RAO/WI)
539 * DFB [1] == 1 (FIQ bypass not supported, RAO/WI)
540 * SRE [0] == 1 (Only system register interface supported, RAO/WI)
541 */
542 ICC_SRE_EL2 icc_sre_el2 = 0;
543 icc_sre_el2.SRE = 1;
544 icc_sre_el2.DIB = 1;
545 icc_sre_el2.DFB = 1;
546 icc_sre_el2.Enable = 1;
547 value = icc_sre_el2;
548 break;
549 }
550
551 // System Register Enable Register EL3
552 case MISCREG_ICC_MSRE:
553 case MISCREG_ICC_SRE_EL3: {
554 /*
555 * Enable [3] == 1
556 * (EL1 accesses to ICC_SRE_EL1 do not trap to EL3.
557 * EL2 accesses to ICC_SRE_EL1 and ICC_SRE_EL2 do not trap to EL3.
558 * RAO/WI)
559 * DIB [2] == 1 (IRQ bypass not supported, RAO/WI)
560 * DFB [1] == 1 (FIQ bypass not supported, RAO/WI)
561 * SRE [0] == 1 (Only system register interface supported, RAO/WI)
562 */
563 ICC_SRE_EL3 icc_sre_el3 = 0;
564 icc_sre_el3.SRE = 1;
565 icc_sre_el3.DIB = 1;
566 icc_sre_el3.DFB = 1;
567 icc_sre_el3.Enable = 1;
568 value = icc_sre_el3;
569 break;
570 }
571
572 // Control Register
573 case MISCREG_ICC_CTLR:
574 case MISCREG_ICC_CTLR_EL1: {
575 if ((currEL() == EL1) && !inSecureState() && (hcr_imo || hcr_fmo)) {
576 return readMiscReg(MISCREG_ICV_CTLR_EL1);
577 }
578
579 // Enforce value for RO bits
580 // ExtRange [19], INTIDs in the range 1024..8191 not supported
581 // RSS [18], SGIs with affinity level 0 values of 0-255 are supported
582 // A3V [15], supports non-zero values of the Aff3 field in SGI
583 // generation System registers
584 // SEIS [14], does not support generation of SEIs (deprecated)
585 // IDbits [13:11], 001 = 24 bits | 000 = 16 bits
586 // PRIbits [10:8], number of priority bits implemented, minus one
587 ICC_CTLR_EL1 icc_ctlr_el1 = value;
588 icc_ctlr_el1.ExtRange = 0;
589 icc_ctlr_el1.RSS = 1;
590 icc_ctlr_el1.A3V = 1;
591 icc_ctlr_el1.SEIS = 0;
592 icc_ctlr_el1.IDbits = 1;
593 icc_ctlr_el1.PRIbits = PRIORITY_BITS - 1;
594 value = icc_ctlr_el1;
595 break;
596 }
597
598 // Virtual Control Register
599 case MISCREG_ICV_CTLR_EL1: {
600 ICV_CTLR_EL1 icv_ctlr_el1 = value;
601 icv_ctlr_el1.RSS = 0;
602 icv_ctlr_el1.A3V = 1;
603 icv_ctlr_el1.SEIS = 0;
604 icv_ctlr_el1.IDbits = 1;
605 icv_ctlr_el1.PRIbits = 7;
606 value = icv_ctlr_el1;
607 break;
608 }
609
610 // Control Register
611 case MISCREG_ICC_MCTLR:
612 case MISCREG_ICC_CTLR_EL3: {
613 // Enforce value for RO bits
614 // ExtRange [19], INTIDs in the range 1024..8191 not supported
615 // RSS [18], SGIs with affinity level 0 values of 0-255 are supported
616 // nDS [17], supports disabling of security
617 // A3V [15], supports non-zero values of the Aff3 field in SGI
618 // generation System registers
619 // SEIS [14], does not support generation of SEIs (deprecated)
620 // IDbits [13:11], 001 = 24 bits | 000 = 16 bits
621 // PRIbits [10:8], number of priority bits implemented, minus one
622 ICC_CTLR_EL3 icc_ctlr_el3 = value;
623 icc_ctlr_el3.ExtRange = 0;
624 icc_ctlr_el3.RSS = 1;
625 icc_ctlr_el3.nDS = 0;
626 icc_ctlr_el3.A3V = 1;
627 icc_ctlr_el3.SEIS = 0;
628 icc_ctlr_el3.IDbits = 0;
629 icc_ctlr_el3.PRIbits = PRIORITY_BITS - 1;
630 value = icc_ctlr_el3;
631 break;
632 }
633
634 // Hyp Control Register
635 case MISCREG_ICH_HCR:
636 case MISCREG_ICH_HCR_EL2:
637 break;
638
639 // Hyp Active Priorities Group 0 Registers
640 case MISCREG_ICH_AP0R0:
641 case MISCREG_ICH_AP0R0_EL2:
642 break;
643
644 // Hyp Active Priorities Group 1 Registers
645 case MISCREG_ICH_AP1R0:
646 case MISCREG_ICH_AP1R0_EL2:
647 break;
648
649 // Maintenance Interrupt State Register
650 case MISCREG_ICH_MISR:
651 case MISCREG_ICH_MISR_EL2:
652 value = maintenanceInterruptStatus();
653 break;
654
655 // VGIC Type Register
656 case MISCREG_ICH_VTR:
657 case MISCREG_ICH_VTR_EL2: {
658 ICH_VTR_EL2 ich_vtr_el2 = value;
659
660 ich_vtr_el2.ListRegs = VIRTUAL_NUM_LIST_REGS - 1;
661 ich_vtr_el2.A3V = 1;
662 ich_vtr_el2.IDbits = 1;
663 ich_vtr_el2.PREbits = VIRTUAL_PREEMPTION_BITS - 1;
664 ich_vtr_el2.PRIbits = VIRTUAL_PRIORITY_BITS - 1;
665
666 value = ich_vtr_el2;
667 break;
668 }
669
670 // End of Interrupt Status Register
671 case MISCREG_ICH_EISR:
672 case MISCREG_ICH_EISR_EL2:
673 value = eoiMaintenanceInterruptStatus();
674 break;
675
676 // Empty List Register Status Register
677 case MISCREG_ICH_ELRSR:
678 case MISCREG_ICH_ELRSR_EL2:
679 value = 0;
680
681 for (int lr_idx = 0; lr_idx < VIRTUAL_NUM_LIST_REGS; lr_idx++) {
682 ICH_LR_EL2 ich_lr_el2 =
683 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
684
685 if ((ich_lr_el2.State == ICH_LR_EL2_STATE_INVALID) &&
686 (ich_lr_el2.HW || !ich_lr_el2.EOI)) {
687 value |= (1 << lr_idx);
688 }
689 }
690
691 break;
692
693 // List Registers
694 case MISCREG_ICH_LRC0 ... MISCREG_ICH_LRC15:
695 // AArch32 (maps to AArch64 MISCREG_ICH_LR<n>_EL2 high half part)
696 value = value >> 32;
697 break;
698
699 // List Registers
700 case MISCREG_ICH_LR0 ... MISCREG_ICH_LR15:
701 // AArch32 (maps to AArch64 MISCREG_ICH_LR<n>_EL2 low half part)
702 value = value & 0xffffffff;
703 break;
704
705 // List Registers
706 case MISCREG_ICH_LR0_EL2 ... MISCREG_ICH_LR15_EL2:
707 break;
708
709 // Virtual Machine Control Register
710 case MISCREG_ICH_VMCR:
711 case MISCREG_ICH_VMCR_EL2:
712 break;
713
714 default:
715 panic("Gicv3CPUInterface::readMiscReg(): unknown register %d (%s)",
716 misc_reg, miscRegName[misc_reg]);
717 }
718
719 DPRINTF(GIC, "Gicv3CPUInterface::readMiscReg(): register %s value %#x\n",
720 miscRegName[misc_reg], value);
721 return value;
722}
723
724void
725Gicv3CPUInterface::setMiscReg(int misc_reg, RegVal val)
726{
727 bool do_virtual_update = false;
728 DPRINTF(GIC, "Gicv3CPUInterface::setMiscReg(): register %s value %#x\n",
729 miscRegName[misc_reg], val);
730 bool hcr_fmo = getHCREL2FMO();
731 bool hcr_imo = getHCREL2IMO();
732
733 switch (misc_reg) {
734 // Active Priorities Group 1 Registers
735 case MISCREG_ICC_AP1R0:
736 case MISCREG_ICC_AP1R0_EL1:
737 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
738 return isa->setMiscRegNoEffect(MISCREG_ICV_AP1R0_EL1, val);
739 }
740
741 break;
742
743 case MISCREG_ICC_AP1R1:
744 case MISCREG_ICC_AP1R1_EL1:
745
746 // only implemented if supporting 6 or more bits of priority
747 case MISCREG_ICC_AP1R2:
748 case MISCREG_ICC_AP1R2_EL1:
749
750 // only implemented if supporting 7 or more bits of priority
751 case MISCREG_ICC_AP1R3:
752 case MISCREG_ICC_AP1R3_EL1:
753 // only implemented if supporting 7 or more bits of priority
754 break;
755
756 // Active Priorities Group 0 Registers
757 case MISCREG_ICC_AP0R0:
758 case MISCREG_ICC_AP0R0_EL1:
759 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
760 return isa->setMiscRegNoEffect(MISCREG_ICV_AP0R0_EL1, val);
761 }
762
763 break;
764
765 case MISCREG_ICC_AP0R1:
766 case MISCREG_ICC_AP0R1_EL1:
767
768 // only implemented if supporting 6 or more bits of priority
769 case MISCREG_ICC_AP0R2:
770 case MISCREG_ICC_AP0R2_EL1:
771
772 // only implemented if supporting 7 or more bits of priority
773 case MISCREG_ICC_AP0R3:
774 case MISCREG_ICC_AP0R3_EL1:
775 // only implemented if supporting 7 or more bits of priority
776 break;
777
778 // End Of Interrupt Register 0
779 case MISCREG_ICC_EOIR0:
780 case MISCREG_ICC_EOIR0_EL1: { // End Of Interrupt Register 0
781 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
782 return setMiscReg(MISCREG_ICV_EOIR0_EL1, val);
783 }
784
785 int int_id = val & 0xffffff;
786
787 // avoid activation for special interrupts
788 if (int_id >= Gicv3::INTID_SECURE &&
789 int_id <= Gicv3::INTID_SPURIOUS) {
790 return;
791 }
792
793 Gicv3::GroupId group = Gicv3::G0S;
794
795 if (highestActiveGroup() != group) {
796 return;
797 }
798
799 dropPriority(group);
800
801 if (!isEOISplitMode()) {
802 deactivateIRQ(int_id, group);
803 }
804
805 break;
806 }
807
808 // Virtual End Of Interrupt Register 0
809 case MISCREG_ICV_EOIR0_EL1: {
810 int int_id = val & 0xffffff;
811
812 // avoid deactivation for special interrupts
813 if (int_id >= Gicv3::INTID_SECURE &&
814 int_id <= Gicv3::INTID_SPURIOUS) {
815 return;
816 }
817
818 uint8_t drop_prio = virtualDropPriority();
819
820 if (drop_prio == 0xff) {
821 return;
822 }
823
824 int lr_idx = virtualFindActive(int_id);
825
826 if (lr_idx < 0) {
827 // No LR found matching
828 virtualIncrementEOICount();
829 } else {
830 ICH_LR_EL2 ich_lr_el2 =
831 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
832 Gicv3::GroupId lr_group =
833 ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
834 uint8_t lr_group_prio = ich_lr_el2.Priority & 0xf8;
835
836 if (lr_group == Gicv3::G0S && lr_group_prio == drop_prio) {
837 //if (!virtualIsEOISplitMode())
838 {
839 virtualDeactivateIRQ(lr_idx);
840 }
841 }
842 }
843
844 virtualUpdate();
845 break;
846 }
847
848 // End Of Interrupt Register 1
849 case MISCREG_ICC_EOIR1:
850 case MISCREG_ICC_EOIR1_EL1: {
851 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
852 return setMiscReg(MISCREG_ICV_EOIR1_EL1, val);
853 }
854
855 int int_id = val & 0xffffff;
856
857 // avoid deactivation for special interrupts
858 if (int_id >= Gicv3::INTID_SECURE &&
859 int_id <= Gicv3::INTID_SPURIOUS) {
860 return;
861 }
862
863 Gicv3::GroupId group = inSecureState() ? Gicv3::G1S : Gicv3::G1NS;
864
865 if (highestActiveGroup() == Gicv3::G0S) {
866 return;
867 }
868
869 if (distributor->DS == 0) {
870 if (highestActiveGroup() == Gicv3::G1S && !inSecureState()) {
871 return;
872 } else if (highestActiveGroup() == Gicv3::G1NS &&
873 !(!inSecureState() or (currEL() == EL3))) {
874 return;
875 }
876 }
877
878 dropPriority(group);
879
880 if (!isEOISplitMode()) {
881 deactivateIRQ(int_id, group);
882 }
883
884 break;
885 }
886
887 // Virtual End Of Interrupt Register 1
888 case MISCREG_ICV_EOIR1_EL1: {
889 int int_id = val & 0xffffff;
890
891 // avoid deactivation for special interrupts
892 if (int_id >= Gicv3::INTID_SECURE &&
893 int_id <= Gicv3::INTID_SPURIOUS) {
894 return;
895 }
896
897 uint8_t drop_prio = virtualDropPriority();
898
899 if (drop_prio == 0xff) {
900 return;
901 }
902
903 int lr_idx = virtualFindActive(int_id);
904
905 if (lr_idx < 0) {
906 // No matching LR found
907 virtualIncrementEOICount();
908 } else {
909 ICH_LR_EL2 ich_lr_el2 =
910 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
911 Gicv3::GroupId lr_group =
912 ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
913 uint8_t lr_group_prio = ich_lr_el2.Priority & 0xf8;
914
915 if (lr_group == Gicv3::G1NS && lr_group_prio == drop_prio) {
916 if (!virtualIsEOISplitMode()) {
917 virtualDeactivateIRQ(lr_idx);
918 }
919 }
920 }
921
922 virtualUpdate();
923 break;
924 }
925
926 // Deactivate Interrupt Register
927 case MISCREG_ICC_DIR:
928 case MISCREG_ICC_DIR_EL1: {
929 if ((currEL() == EL1) && !inSecureState() &&
930 (hcr_imo || hcr_fmo)) {
931 return setMiscReg(MISCREG_ICV_DIR_EL1, val);
932 }
933
934 int int_id = val & 0xffffff;
935
936 // The following checks are as per spec pseudocode
937 // aarch64/support/ICC_DIR_EL1
938
939 // Check for spurious ID
940 if (int_id >= Gicv3::INTID_SECURE) {
941 return;
942 }
943
944 // EOI mode is not set, so don't deactivate
945 if (!isEOISplitMode()) {
946 return;
947 }
948
949 Gicv3::GroupId group =
950 int_id >= 32 ? distributor->getIntGroup(int_id) :
951 redistributor->getIntGroup(int_id);
952 bool irq_is_grp0 = group == Gicv3::G0S;
953 bool single_sec_state = distributor->DS;
954 bool irq_is_secure = !single_sec_state && (group != Gicv3::G1NS);
955 SCR scr_el3 = isa->readMiscRegNoEffect(MISCREG_SCR_EL3);
956 bool route_fiq_to_el3 = scr_el3.fiq;
957 bool route_irq_to_el3 = scr_el3.irq;
958 bool route_fiq_to_el2 = hcr_fmo;
959 bool route_irq_to_el2 = hcr_imo;
960
961 switch (currEL()) {
962 case EL3:
963 break;
964
965 case EL2:
966 if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
967 break;
968 }
969
970 if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
971 break;
972 }
973
974 return;
975
976 case EL1:
977 if (!isSecureBelowEL3()) {
978 if (single_sec_state && irq_is_grp0 &&
979 !route_fiq_to_el3 && !route_fiq_to_el2) {
980 break;
981 }
982
983 if (!irq_is_secure && !irq_is_grp0 &&
984 !route_irq_to_el3 && !route_irq_to_el2) {
985 break;
986 }
987 } else {
988 if (irq_is_grp0 && !route_fiq_to_el3) {
989 break;
990 }
991
992 if (!irq_is_grp0 &&
993 (!irq_is_secure || !single_sec_state) &&
994 !route_irq_to_el3) {
995 break;
996 }
997 }
998
999 return;
1000
1001 default:
1002 break;
1003 }
1004
1005 deactivateIRQ(int_id, group);
1006 break;
1007 }
1008
1009 // Deactivate Virtual Interrupt Register
1010 case MISCREG_ICV_DIR_EL1: {
1011 int int_id = val & 0xffffff;
1012
1013 // avoid deactivation for special interrupts
1014 if (int_id >= Gicv3::INTID_SECURE &&
1015 int_id <= Gicv3::INTID_SPURIOUS) {
1016 return;
1017 }
1018
1019 if (!virtualIsEOISplitMode()) {
1020 return;
1021 }
1022
1023 int lr_idx = virtualFindActive(int_id);
1024
1025 if (lr_idx < 0) {
1026 // No matching LR found
1027 virtualIncrementEOICount();
1028 } else {
1029 virtualDeactivateIRQ(lr_idx);
1030 }
1031
1032 virtualUpdate();
1033 break;
1034 }
1035
1036 // Binary Point Register 0
1037 case MISCREG_ICC_BPR0:
1038 case MISCREG_ICC_BPR0_EL1:
1039 // Binary Point Register 1
1040 case MISCREG_ICC_BPR1:
1041 case MISCREG_ICC_BPR1_EL1: {
1042 if ((currEL() == EL1) && !inSecureState()) {
1043 if (misc_reg == MISCREG_ICC_BPR0_EL1 && hcr_fmo) {
1044 return setMiscReg(MISCREG_ICV_BPR0_EL1, val);
1045 } else if (misc_reg == MISCREG_ICC_BPR1_EL1 && hcr_imo) {
1046 return setMiscReg(MISCREG_ICV_BPR1_EL1, val);
1047 }
1048 }
1049
1050 Gicv3::GroupId group =
1051 misc_reg == MISCREG_ICC_BPR0_EL1 ? Gicv3::G0S : Gicv3::G1S;
1052
1053 if (group == Gicv3::G1S && !inSecureState()) {
1054 group = Gicv3::G1NS;
1055 }
1056
1057 ICC_CTLR_EL1 icc_ctlr_el1_s =
1058 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S);
1059
1060 if ((group == Gicv3::G1S) && !isEL3OrMon() &&
1061 icc_ctlr_el1_s.CBPR) {
1062 group = Gicv3::G0S;
1063 }
1064
1065 ICC_CTLR_EL1 icc_ctlr_el1_ns =
1066 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS);
1067
1068 if ((group == Gicv3::G1NS) && (currEL() < EL3) &&
1069 icc_ctlr_el1_ns.CBPR) {
1070 // BPR0 + 1 saturated to 7, WI
1071 return;
1072 }
1073
1074 uint8_t min_val = (group == Gicv3::G1NS) ?
1075 GIC_MIN_BPR_NS : GIC_MIN_BPR;
1076 val &= 0x7;
1077
1078 if (val < min_val) {
1079 val = min_val;
1080 }
1081
1082 break;
1083 }
1084
1085 // Virtual Binary Point Register 0
1086 case MISCREG_ICV_BPR0_EL1:
1087 // Virtual Binary Point Register 1
1088 case MISCREG_ICV_BPR1_EL1: {
1089 Gicv3::GroupId group =
1090 misc_reg == MISCREG_ICV_BPR0_EL1 ? Gicv3::G0S : Gicv3::G1NS;
1091 ICH_VMCR_EL2 ich_vmcr_el2 =
1092 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1093
1094 if ((group == Gicv3::G1NS) && ich_vmcr_el2.VCBPR) {
1095 // BPR0 + 1 saturated to 7, WI
1096 return;
1097 }
1098
1099 uint8_t min_VPBR = 7 - VIRTUAL_PREEMPTION_BITS;
1100
1101 if (group != Gicv3::G0S) {
1102 min_VPBR++;
1103 }
1104
1105 if (val < min_VPBR) {
1106 val = min_VPBR;
1107 }
1108
1109 if (group == Gicv3::G0S) {
1110 ich_vmcr_el2.VBPR0 = val;
1111 } else {
1112 ich_vmcr_el2.VBPR1 = val;
1113 }
1114
1115 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1116 do_virtual_update = true;
1117 break;
1118 }
1119
1120 // Control Register EL1
1121 case MISCREG_ICC_CTLR:
1122 case MISCREG_ICC_CTLR_EL1: {
1123 if ((currEL() == EL1) && !inSecureState() && (hcr_imo || hcr_fmo)) {
1124 return setMiscReg(MISCREG_ICV_CTLR_EL1, val);
1125 }
1126
1127 /*
1128 * ExtRange is RO.
1129 * RSS is RO.
1130 * A3V is RO.
1131 * SEIS is RO.
1132 * IDbits is RO.
1133 * PRIbits is RO.
1134 */
1135 ICC_CTLR_EL1 requested_icc_ctlr_el1 = val;
1136 ICC_CTLR_EL1 icc_ctlr_el1 =
1137 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1);
1138
1139 ICC_CTLR_EL3 icc_ctlr_el3 =
1140 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL3);
1141
1142 // The following could be refactored but it is following
1143 // spec description section 9.2.6 point by point.
1144
1145 // PMHE
1146 if (haveEL(EL3)) {
1147 // PMHE is alias of ICC_CTLR_EL3.PMHE
1148
1149 if (distributor->DS == 0) {
1150 // PMHE is RO
1151 } else if (distributor->DS == 1) {
1152 // PMHE is RW
1153 icc_ctlr_el1.PMHE = requested_icc_ctlr_el1.PMHE;
1154 icc_ctlr_el3.PMHE = icc_ctlr_el1.PMHE;
1155 }
1156 } else {
1157 // PMHE is RW (by implementation choice)
1158 icc_ctlr_el1.PMHE = requested_icc_ctlr_el1.PMHE;
1159 }
1160
1161 // EOImode
1162 icc_ctlr_el1.EOImode = requested_icc_ctlr_el1.EOImode;
1163
1164 if (inSecureState()) {
1165 // EOIMode is alias of ICC_CTLR_EL3.EOImode_EL1S
1166 icc_ctlr_el3.EOImode_EL1S = icc_ctlr_el1.EOImode;
1167 } else {
1168 // EOIMode is alias of ICC_CTLR_EL3.EOImode_EL1NS
1169 icc_ctlr_el3.EOImode_EL1NS = icc_ctlr_el1.EOImode;
1170 }
1171
1172 // CBPR
1173 if (haveEL(EL3)) {
1174 // CBPR is alias of ICC_CTLR_EL3.CBPR_EL1{S,NS}
1175
1176 if (distributor->DS == 0) {
1177 // CBPR is RO
1178 } else {
1179 // CBPR is RW
1180 icc_ctlr_el1.CBPR = requested_icc_ctlr_el1.CBPR;
1181
1182 if (inSecureState()) {
1183 icc_ctlr_el3.CBPR_EL1S = icc_ctlr_el1.CBPR;
1184 } else {
1185 icc_ctlr_el3.CBPR_EL1NS = icc_ctlr_el1.CBPR;
1186 }
1187 }
1188 } else {
1189 // CBPR is RW
1190 icc_ctlr_el1.CBPR = requested_icc_ctlr_el1.CBPR;
1191 }
1192
1193 isa->setMiscRegNoEffect(MISCREG_ICC_CTLR_EL3, icc_ctlr_el3);
1194
1195 val = icc_ctlr_el1;
1196 break;
1197 }
1198
1199 // Virtual Control Register
1200 case MISCREG_ICV_CTLR_EL1: {
1201 ICV_CTLR_EL1 requested_icv_ctlr_el1 = val;
1202 ICV_CTLR_EL1 icv_ctlr_el1 =
1203 isa->readMiscRegNoEffect(MISCREG_ICV_CTLR_EL1);
1204 icv_ctlr_el1.EOImode = requested_icv_ctlr_el1.EOImode;
1205 icv_ctlr_el1.CBPR = requested_icv_ctlr_el1.CBPR;
1206 val = icv_ctlr_el1;
1207
1208 // Aliases
1209 // ICV_CTLR_EL1.CBPR aliases ICH_VMCR_EL2.VCBPR.
1210 // ICV_CTLR_EL1.EOImode aliases ICH_VMCR_EL2.VEOIM.
1211 ICH_VMCR_EL2 ich_vmcr_el2 =
1212 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1213 ich_vmcr_el2.VCBPR = icv_ctlr_el1.CBPR;
1214 ich_vmcr_el2.VEOIM = icv_ctlr_el1.EOImode;
1215 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1216 break;
1217 }
1218
1219 // Control Register EL3
1220 case MISCREG_ICC_MCTLR:
1221 case MISCREG_ICC_CTLR_EL3: {
1222 /*
1223 * ExtRange is RO.
1224 * RSS is RO.
1225 * nDS is RO.
1226 * A3V is RO.
1227 * SEIS is RO.
1228 * IDbits is RO.
1229 * PRIbits is RO.
1230 * PMHE is RAO/WI, priority-based routing is always used.
1231 */
1232 ICC_CTLR_EL3 requested_icc_ctlr_el3 = val;
1233
1234 // Aliases
1235 if (haveEL(EL3))
1236 {
1237 ICC_CTLR_EL1 icc_ctlr_el1_s =
1238 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S);
1239 ICC_CTLR_EL1 icc_ctlr_el1_ns =
1240 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS);
1241
1242 // ICC_CTLR_EL1(NS).EOImode is an alias of
1243 // ICC_CTLR_EL3.EOImode_EL1NS
1244 icc_ctlr_el1_ns.EOImode = requested_icc_ctlr_el3.EOImode_EL1NS;
1245 // ICC_CTLR_EL1(S).EOImode is an alias of
1246 // ICC_CTLR_EL3.EOImode_EL1S
1247 icc_ctlr_el1_s.EOImode = requested_icc_ctlr_el3.EOImode_EL1S;
1248 // ICC_CTLR_EL1(NS).CBPR is an alias of ICC_CTLR_EL3.CBPR_EL1NS
1249 icc_ctlr_el1_ns.CBPR = requested_icc_ctlr_el3.CBPR_EL1NS;
1250 // ICC_CTLR_EL1(S).CBPR is an alias of ICC_CTLR_EL3.CBPR_EL1S
1251 icc_ctlr_el1_s.CBPR = requested_icc_ctlr_el3.CBPR_EL1S;
1252
1253 isa->setMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S, icc_ctlr_el1_s);
1254 isa->setMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS,
1255 icc_ctlr_el1_ns);
1256 }
1257
1258 ICC_CTLR_EL3 icc_ctlr_el3 =
1259 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL3);
1260
1261 icc_ctlr_el3.RM = requested_icc_ctlr_el3.RM;
1262 icc_ctlr_el3.EOImode_EL1NS = requested_icc_ctlr_el3.EOImode_EL1NS;
1263 icc_ctlr_el3.EOImode_EL1S = requested_icc_ctlr_el3.EOImode_EL1S;
1264 icc_ctlr_el3.EOImode_EL3 = requested_icc_ctlr_el3.EOImode_EL3;
1265 icc_ctlr_el3.CBPR_EL1NS = requested_icc_ctlr_el3.CBPR_EL1NS;
1266 icc_ctlr_el3.CBPR_EL1S = requested_icc_ctlr_el3.CBPR_EL1S;
1267
1268 val = icc_ctlr_el3;
1269 break;
1270 }
1271
1272 // Priority Mask Register
1273 case MISCREG_ICC_PMR:
1274 case MISCREG_ICC_PMR_EL1: {
1275 if ((currEL() == EL1) && !inSecureState() && (hcr_imo || hcr_fmo)) {
426 // Interrupt Acknowledge Register 0
427 case MISCREG_ICC_IAR0:
428 case MISCREG_ICC_IAR0_EL1: {
429 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
430 return readMiscReg(MISCREG_ICV_IAR0_EL1);
431 }
432
433 uint32_t int_id;
434
435 if (hppiCanPreempt()) {
436 int_id = getHPPIR0();
437
438 // avoid activation for special interrupts
439 if (int_id < Gicv3::INTID_SECURE ||
440 int_id >= Gicv3Redistributor::SMALLEST_LPI_ID) {
441 activateIRQ(int_id, hppi.group);
442 }
443 } else {
444 int_id = Gicv3::INTID_SPURIOUS;
445 }
446
447 value = int_id;
448 break;
449 }
450
451 // Virtual Interrupt Acknowledge Register 0
452 case MISCREG_ICV_IAR0_EL1: {
453 int lr_idx = getHPPVILR();
454 uint32_t int_id = Gicv3::INTID_SPURIOUS;
455
456 if (lr_idx >= 0) {
457 ICH_LR_EL2 ich_lr_el2 =
458 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
459
460 if (!ich_lr_el2.Group && hppviCanPreempt(lr_idx)) {
461 int_id = ich_lr_el2.vINTID;
462
463 if (int_id < Gicv3::INTID_SECURE ||
464 int_id > Gicv3::INTID_SPURIOUS) {
465 virtualActivateIRQ(lr_idx);
466 } else {
467 // Bogus... Pseudocode says:
468 // - Move from pending to invalid...
469 // - Return de bogus id...
470 ich_lr_el2.State = ICH_LR_EL2_STATE_INVALID;
471 isa->setMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx,
472 ich_lr_el2);
473 }
474 }
475 }
476
477 value = int_id;
478 virtualUpdate();
479 break;
480 }
481
482 // Interrupt Acknowledge Register 1
483 case MISCREG_ICC_IAR1:
484 case MISCREG_ICC_IAR1_EL1: {
485 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
486 return readMiscReg(MISCREG_ICV_IAR1_EL1);
487 }
488
489 uint32_t int_id;
490
491 if (hppiCanPreempt()) {
492 int_id = getHPPIR1();
493
494 // avoid activation for special interrupts
495 if (int_id < Gicv3::INTID_SECURE ||
496 int_id >= Gicv3Redistributor::SMALLEST_LPI_ID) {
497 activateIRQ(int_id, hppi.group);
498 }
499 } else {
500 int_id = Gicv3::INTID_SPURIOUS;
501 }
502
503 value = int_id;
504 break;
505 }
506
507 // Virtual Interrupt Acknowledge Register 1
508 case MISCREG_ICV_IAR1_EL1: {
509 int lr_idx = getHPPVILR();
510 uint32_t int_id = Gicv3::INTID_SPURIOUS;
511
512 if (lr_idx >= 0) {
513 ICH_LR_EL2 ich_lr_el2 =
514 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
515
516 if (ich_lr_el2.Group && hppviCanPreempt(lr_idx)) {
517 int_id = ich_lr_el2.vINTID;
518
519 if (int_id < Gicv3::INTID_SECURE ||
520 int_id > Gicv3::INTID_SPURIOUS) {
521 virtualActivateIRQ(lr_idx);
522 } else {
523 // Bogus... Pseudocode says:
524 // - Move from pending to invalid...
525 // - Return de bogus id...
526 ich_lr_el2.State = ICH_LR_EL2_STATE_INVALID;
527 isa->setMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx,
528 ich_lr_el2);
529 }
530 }
531 }
532
533 value = int_id;
534 virtualUpdate();
535 break;
536 }
537
538 // System Register Enable Register EL1
539 case MISCREG_ICC_SRE:
540 case MISCREG_ICC_SRE_EL1: {
541 /*
542 * DIB [2] == 1 (IRQ bypass not supported, RAO/WI)
543 * DFB [1] == 1 (FIQ bypass not supported, RAO/WI)
544 * SRE [0] == 1 (Only system register interface supported, RAO/WI)
545 */
546 ICC_SRE_EL1 icc_sre_el1 = 0;
547 icc_sre_el1.SRE = 1;
548 icc_sre_el1.DIB = 1;
549 icc_sre_el1.DFB = 1;
550 value = icc_sre_el1;
551 break;
552 }
553
554 // System Register Enable Register EL2
555 case MISCREG_ICC_HSRE:
556 case MISCREG_ICC_SRE_EL2: {
557 /*
558 * Enable [3] == 1
559 * (EL1 accesses to ICC_SRE_EL1 do not trap to EL2, RAO/WI)
560 * DIB [2] == 1 (IRQ bypass not supported, RAO/WI)
561 * DFB [1] == 1 (FIQ bypass not supported, RAO/WI)
562 * SRE [0] == 1 (Only system register interface supported, RAO/WI)
563 */
564 ICC_SRE_EL2 icc_sre_el2 = 0;
565 icc_sre_el2.SRE = 1;
566 icc_sre_el2.DIB = 1;
567 icc_sre_el2.DFB = 1;
568 icc_sre_el2.Enable = 1;
569 value = icc_sre_el2;
570 break;
571 }
572
573 // System Register Enable Register EL3
574 case MISCREG_ICC_MSRE:
575 case MISCREG_ICC_SRE_EL3: {
576 /*
577 * Enable [3] == 1
578 * (EL1 accesses to ICC_SRE_EL1 do not trap to EL3.
579 * EL2 accesses to ICC_SRE_EL1 and ICC_SRE_EL2 do not trap to EL3.
580 * RAO/WI)
581 * DIB [2] == 1 (IRQ bypass not supported, RAO/WI)
582 * DFB [1] == 1 (FIQ bypass not supported, RAO/WI)
583 * SRE [0] == 1 (Only system register interface supported, RAO/WI)
584 */
585 ICC_SRE_EL3 icc_sre_el3 = 0;
586 icc_sre_el3.SRE = 1;
587 icc_sre_el3.DIB = 1;
588 icc_sre_el3.DFB = 1;
589 icc_sre_el3.Enable = 1;
590 value = icc_sre_el3;
591 break;
592 }
593
594 // Control Register
595 case MISCREG_ICC_CTLR:
596 case MISCREG_ICC_CTLR_EL1: {
597 if ((currEL() == EL1) && !inSecureState() && (hcr_imo || hcr_fmo)) {
598 return readMiscReg(MISCREG_ICV_CTLR_EL1);
599 }
600
601 // Enforce value for RO bits
602 // ExtRange [19], INTIDs in the range 1024..8191 not supported
603 // RSS [18], SGIs with affinity level 0 values of 0-255 are supported
604 // A3V [15], supports non-zero values of the Aff3 field in SGI
605 // generation System registers
606 // SEIS [14], does not support generation of SEIs (deprecated)
607 // IDbits [13:11], 001 = 24 bits | 000 = 16 bits
608 // PRIbits [10:8], number of priority bits implemented, minus one
609 ICC_CTLR_EL1 icc_ctlr_el1 = value;
610 icc_ctlr_el1.ExtRange = 0;
611 icc_ctlr_el1.RSS = 1;
612 icc_ctlr_el1.A3V = 1;
613 icc_ctlr_el1.SEIS = 0;
614 icc_ctlr_el1.IDbits = 1;
615 icc_ctlr_el1.PRIbits = PRIORITY_BITS - 1;
616 value = icc_ctlr_el1;
617 break;
618 }
619
620 // Virtual Control Register
621 case MISCREG_ICV_CTLR_EL1: {
622 ICV_CTLR_EL1 icv_ctlr_el1 = value;
623 icv_ctlr_el1.RSS = 0;
624 icv_ctlr_el1.A3V = 1;
625 icv_ctlr_el1.SEIS = 0;
626 icv_ctlr_el1.IDbits = 1;
627 icv_ctlr_el1.PRIbits = 7;
628 value = icv_ctlr_el1;
629 break;
630 }
631
632 // Control Register
633 case MISCREG_ICC_MCTLR:
634 case MISCREG_ICC_CTLR_EL3: {
635 // Enforce value for RO bits
636 // ExtRange [19], INTIDs in the range 1024..8191 not supported
637 // RSS [18], SGIs with affinity level 0 values of 0-255 are supported
638 // nDS [17], supports disabling of security
639 // A3V [15], supports non-zero values of the Aff3 field in SGI
640 // generation System registers
641 // SEIS [14], does not support generation of SEIs (deprecated)
642 // IDbits [13:11], 001 = 24 bits | 000 = 16 bits
643 // PRIbits [10:8], number of priority bits implemented, minus one
644 ICC_CTLR_EL3 icc_ctlr_el3 = value;
645 icc_ctlr_el3.ExtRange = 0;
646 icc_ctlr_el3.RSS = 1;
647 icc_ctlr_el3.nDS = 0;
648 icc_ctlr_el3.A3V = 1;
649 icc_ctlr_el3.SEIS = 0;
650 icc_ctlr_el3.IDbits = 0;
651 icc_ctlr_el3.PRIbits = PRIORITY_BITS - 1;
652 value = icc_ctlr_el3;
653 break;
654 }
655
656 // Hyp Control Register
657 case MISCREG_ICH_HCR:
658 case MISCREG_ICH_HCR_EL2:
659 break;
660
661 // Hyp Active Priorities Group 0 Registers
662 case MISCREG_ICH_AP0R0:
663 case MISCREG_ICH_AP0R0_EL2:
664 break;
665
666 // Hyp Active Priorities Group 1 Registers
667 case MISCREG_ICH_AP1R0:
668 case MISCREG_ICH_AP1R0_EL2:
669 break;
670
671 // Maintenance Interrupt State Register
672 case MISCREG_ICH_MISR:
673 case MISCREG_ICH_MISR_EL2:
674 value = maintenanceInterruptStatus();
675 break;
676
677 // VGIC Type Register
678 case MISCREG_ICH_VTR:
679 case MISCREG_ICH_VTR_EL2: {
680 ICH_VTR_EL2 ich_vtr_el2 = value;
681
682 ich_vtr_el2.ListRegs = VIRTUAL_NUM_LIST_REGS - 1;
683 ich_vtr_el2.A3V = 1;
684 ich_vtr_el2.IDbits = 1;
685 ich_vtr_el2.PREbits = VIRTUAL_PREEMPTION_BITS - 1;
686 ich_vtr_el2.PRIbits = VIRTUAL_PRIORITY_BITS - 1;
687
688 value = ich_vtr_el2;
689 break;
690 }
691
692 // End of Interrupt Status Register
693 case MISCREG_ICH_EISR:
694 case MISCREG_ICH_EISR_EL2:
695 value = eoiMaintenanceInterruptStatus();
696 break;
697
698 // Empty List Register Status Register
699 case MISCREG_ICH_ELRSR:
700 case MISCREG_ICH_ELRSR_EL2:
701 value = 0;
702
703 for (int lr_idx = 0; lr_idx < VIRTUAL_NUM_LIST_REGS; lr_idx++) {
704 ICH_LR_EL2 ich_lr_el2 =
705 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
706
707 if ((ich_lr_el2.State == ICH_LR_EL2_STATE_INVALID) &&
708 (ich_lr_el2.HW || !ich_lr_el2.EOI)) {
709 value |= (1 << lr_idx);
710 }
711 }
712
713 break;
714
715 // List Registers
716 case MISCREG_ICH_LRC0 ... MISCREG_ICH_LRC15:
717 // AArch32 (maps to AArch64 MISCREG_ICH_LR<n>_EL2 high half part)
718 value = value >> 32;
719 break;
720
721 // List Registers
722 case MISCREG_ICH_LR0 ... MISCREG_ICH_LR15:
723 // AArch32 (maps to AArch64 MISCREG_ICH_LR<n>_EL2 low half part)
724 value = value & 0xffffffff;
725 break;
726
727 // List Registers
728 case MISCREG_ICH_LR0_EL2 ... MISCREG_ICH_LR15_EL2:
729 break;
730
731 // Virtual Machine Control Register
732 case MISCREG_ICH_VMCR:
733 case MISCREG_ICH_VMCR_EL2:
734 break;
735
736 default:
737 panic("Gicv3CPUInterface::readMiscReg(): unknown register %d (%s)",
738 misc_reg, miscRegName[misc_reg]);
739 }
740
741 DPRINTF(GIC, "Gicv3CPUInterface::readMiscReg(): register %s value %#x\n",
742 miscRegName[misc_reg], value);
743 return value;
744}
745
746void
747Gicv3CPUInterface::setMiscReg(int misc_reg, RegVal val)
748{
749 bool do_virtual_update = false;
750 DPRINTF(GIC, "Gicv3CPUInterface::setMiscReg(): register %s value %#x\n",
751 miscRegName[misc_reg], val);
752 bool hcr_fmo = getHCREL2FMO();
753 bool hcr_imo = getHCREL2IMO();
754
755 switch (misc_reg) {
756 // Active Priorities Group 1 Registers
757 case MISCREG_ICC_AP1R0:
758 case MISCREG_ICC_AP1R0_EL1:
759 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
760 return isa->setMiscRegNoEffect(MISCREG_ICV_AP1R0_EL1, val);
761 }
762
763 break;
764
765 case MISCREG_ICC_AP1R1:
766 case MISCREG_ICC_AP1R1_EL1:
767
768 // only implemented if supporting 6 or more bits of priority
769 case MISCREG_ICC_AP1R2:
770 case MISCREG_ICC_AP1R2_EL1:
771
772 // only implemented if supporting 7 or more bits of priority
773 case MISCREG_ICC_AP1R3:
774 case MISCREG_ICC_AP1R3_EL1:
775 // only implemented if supporting 7 or more bits of priority
776 break;
777
778 // Active Priorities Group 0 Registers
779 case MISCREG_ICC_AP0R0:
780 case MISCREG_ICC_AP0R0_EL1:
781 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
782 return isa->setMiscRegNoEffect(MISCREG_ICV_AP0R0_EL1, val);
783 }
784
785 break;
786
787 case MISCREG_ICC_AP0R1:
788 case MISCREG_ICC_AP0R1_EL1:
789
790 // only implemented if supporting 6 or more bits of priority
791 case MISCREG_ICC_AP0R2:
792 case MISCREG_ICC_AP0R2_EL1:
793
794 // only implemented if supporting 7 or more bits of priority
795 case MISCREG_ICC_AP0R3:
796 case MISCREG_ICC_AP0R3_EL1:
797 // only implemented if supporting 7 or more bits of priority
798 break;
799
800 // End Of Interrupt Register 0
801 case MISCREG_ICC_EOIR0:
802 case MISCREG_ICC_EOIR0_EL1: { // End Of Interrupt Register 0
803 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
804 return setMiscReg(MISCREG_ICV_EOIR0_EL1, val);
805 }
806
807 int int_id = val & 0xffffff;
808
809 // avoid activation for special interrupts
810 if (int_id >= Gicv3::INTID_SECURE &&
811 int_id <= Gicv3::INTID_SPURIOUS) {
812 return;
813 }
814
815 Gicv3::GroupId group = Gicv3::G0S;
816
817 if (highestActiveGroup() != group) {
818 return;
819 }
820
821 dropPriority(group);
822
823 if (!isEOISplitMode()) {
824 deactivateIRQ(int_id, group);
825 }
826
827 break;
828 }
829
830 // Virtual End Of Interrupt Register 0
831 case MISCREG_ICV_EOIR0_EL1: {
832 int int_id = val & 0xffffff;
833
834 // avoid deactivation for special interrupts
835 if (int_id >= Gicv3::INTID_SECURE &&
836 int_id <= Gicv3::INTID_SPURIOUS) {
837 return;
838 }
839
840 uint8_t drop_prio = virtualDropPriority();
841
842 if (drop_prio == 0xff) {
843 return;
844 }
845
846 int lr_idx = virtualFindActive(int_id);
847
848 if (lr_idx < 0) {
849 // No LR found matching
850 virtualIncrementEOICount();
851 } else {
852 ICH_LR_EL2 ich_lr_el2 =
853 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
854 Gicv3::GroupId lr_group =
855 ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
856 uint8_t lr_group_prio = ich_lr_el2.Priority & 0xf8;
857
858 if (lr_group == Gicv3::G0S && lr_group_prio == drop_prio) {
859 //if (!virtualIsEOISplitMode())
860 {
861 virtualDeactivateIRQ(lr_idx);
862 }
863 }
864 }
865
866 virtualUpdate();
867 break;
868 }
869
870 // End Of Interrupt Register 1
871 case MISCREG_ICC_EOIR1:
872 case MISCREG_ICC_EOIR1_EL1: {
873 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
874 return setMiscReg(MISCREG_ICV_EOIR1_EL1, val);
875 }
876
877 int int_id = val & 0xffffff;
878
879 // avoid deactivation for special interrupts
880 if (int_id >= Gicv3::INTID_SECURE &&
881 int_id <= Gicv3::INTID_SPURIOUS) {
882 return;
883 }
884
885 Gicv3::GroupId group = inSecureState() ? Gicv3::G1S : Gicv3::G1NS;
886
887 if (highestActiveGroup() == Gicv3::G0S) {
888 return;
889 }
890
891 if (distributor->DS == 0) {
892 if (highestActiveGroup() == Gicv3::G1S && !inSecureState()) {
893 return;
894 } else if (highestActiveGroup() == Gicv3::G1NS &&
895 !(!inSecureState() or (currEL() == EL3))) {
896 return;
897 }
898 }
899
900 dropPriority(group);
901
902 if (!isEOISplitMode()) {
903 deactivateIRQ(int_id, group);
904 }
905
906 break;
907 }
908
909 // Virtual End Of Interrupt Register 1
910 case MISCREG_ICV_EOIR1_EL1: {
911 int int_id = val & 0xffffff;
912
913 // avoid deactivation for special interrupts
914 if (int_id >= Gicv3::INTID_SECURE &&
915 int_id <= Gicv3::INTID_SPURIOUS) {
916 return;
917 }
918
919 uint8_t drop_prio = virtualDropPriority();
920
921 if (drop_prio == 0xff) {
922 return;
923 }
924
925 int lr_idx = virtualFindActive(int_id);
926
927 if (lr_idx < 0) {
928 // No matching LR found
929 virtualIncrementEOICount();
930 } else {
931 ICH_LR_EL2 ich_lr_el2 =
932 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
933 Gicv3::GroupId lr_group =
934 ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
935 uint8_t lr_group_prio = ich_lr_el2.Priority & 0xf8;
936
937 if (lr_group == Gicv3::G1NS && lr_group_prio == drop_prio) {
938 if (!virtualIsEOISplitMode()) {
939 virtualDeactivateIRQ(lr_idx);
940 }
941 }
942 }
943
944 virtualUpdate();
945 break;
946 }
947
948 // Deactivate Interrupt Register
949 case MISCREG_ICC_DIR:
950 case MISCREG_ICC_DIR_EL1: {
951 if ((currEL() == EL1) && !inSecureState() &&
952 (hcr_imo || hcr_fmo)) {
953 return setMiscReg(MISCREG_ICV_DIR_EL1, val);
954 }
955
956 int int_id = val & 0xffffff;
957
958 // The following checks are as per spec pseudocode
959 // aarch64/support/ICC_DIR_EL1
960
961 // Check for spurious ID
962 if (int_id >= Gicv3::INTID_SECURE) {
963 return;
964 }
965
966 // EOI mode is not set, so don't deactivate
967 if (!isEOISplitMode()) {
968 return;
969 }
970
971 Gicv3::GroupId group =
972 int_id >= 32 ? distributor->getIntGroup(int_id) :
973 redistributor->getIntGroup(int_id);
974 bool irq_is_grp0 = group == Gicv3::G0S;
975 bool single_sec_state = distributor->DS;
976 bool irq_is_secure = !single_sec_state && (group != Gicv3::G1NS);
977 SCR scr_el3 = isa->readMiscRegNoEffect(MISCREG_SCR_EL3);
978 bool route_fiq_to_el3 = scr_el3.fiq;
979 bool route_irq_to_el3 = scr_el3.irq;
980 bool route_fiq_to_el2 = hcr_fmo;
981 bool route_irq_to_el2 = hcr_imo;
982
983 switch (currEL()) {
984 case EL3:
985 break;
986
987 case EL2:
988 if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
989 break;
990 }
991
992 if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
993 break;
994 }
995
996 return;
997
998 case EL1:
999 if (!isSecureBelowEL3()) {
1000 if (single_sec_state && irq_is_grp0 &&
1001 !route_fiq_to_el3 && !route_fiq_to_el2) {
1002 break;
1003 }
1004
1005 if (!irq_is_secure && !irq_is_grp0 &&
1006 !route_irq_to_el3 && !route_irq_to_el2) {
1007 break;
1008 }
1009 } else {
1010 if (irq_is_grp0 && !route_fiq_to_el3) {
1011 break;
1012 }
1013
1014 if (!irq_is_grp0 &&
1015 (!irq_is_secure || !single_sec_state) &&
1016 !route_irq_to_el3) {
1017 break;
1018 }
1019 }
1020
1021 return;
1022
1023 default:
1024 break;
1025 }
1026
1027 deactivateIRQ(int_id, group);
1028 break;
1029 }
1030
1031 // Deactivate Virtual Interrupt Register
1032 case MISCREG_ICV_DIR_EL1: {
1033 int int_id = val & 0xffffff;
1034
1035 // avoid deactivation for special interrupts
1036 if (int_id >= Gicv3::INTID_SECURE &&
1037 int_id <= Gicv3::INTID_SPURIOUS) {
1038 return;
1039 }
1040
1041 if (!virtualIsEOISplitMode()) {
1042 return;
1043 }
1044
1045 int lr_idx = virtualFindActive(int_id);
1046
1047 if (lr_idx < 0) {
1048 // No matching LR found
1049 virtualIncrementEOICount();
1050 } else {
1051 virtualDeactivateIRQ(lr_idx);
1052 }
1053
1054 virtualUpdate();
1055 break;
1056 }
1057
1058 // Binary Point Register 0
1059 case MISCREG_ICC_BPR0:
1060 case MISCREG_ICC_BPR0_EL1:
1061 // Binary Point Register 1
1062 case MISCREG_ICC_BPR1:
1063 case MISCREG_ICC_BPR1_EL1: {
1064 if ((currEL() == EL1) && !inSecureState()) {
1065 if (misc_reg == MISCREG_ICC_BPR0_EL1 && hcr_fmo) {
1066 return setMiscReg(MISCREG_ICV_BPR0_EL1, val);
1067 } else if (misc_reg == MISCREG_ICC_BPR1_EL1 && hcr_imo) {
1068 return setMiscReg(MISCREG_ICV_BPR1_EL1, val);
1069 }
1070 }
1071
1072 Gicv3::GroupId group =
1073 misc_reg == MISCREG_ICC_BPR0_EL1 ? Gicv3::G0S : Gicv3::G1S;
1074
1075 if (group == Gicv3::G1S && !inSecureState()) {
1076 group = Gicv3::G1NS;
1077 }
1078
1079 ICC_CTLR_EL1 icc_ctlr_el1_s =
1080 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S);
1081
1082 if ((group == Gicv3::G1S) && !isEL3OrMon() &&
1083 icc_ctlr_el1_s.CBPR) {
1084 group = Gicv3::G0S;
1085 }
1086
1087 ICC_CTLR_EL1 icc_ctlr_el1_ns =
1088 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS);
1089
1090 if ((group == Gicv3::G1NS) && (currEL() < EL3) &&
1091 icc_ctlr_el1_ns.CBPR) {
1092 // BPR0 + 1 saturated to 7, WI
1093 return;
1094 }
1095
1096 uint8_t min_val = (group == Gicv3::G1NS) ?
1097 GIC_MIN_BPR_NS : GIC_MIN_BPR;
1098 val &= 0x7;
1099
1100 if (val < min_val) {
1101 val = min_val;
1102 }
1103
1104 break;
1105 }
1106
1107 // Virtual Binary Point Register 0
1108 case MISCREG_ICV_BPR0_EL1:
1109 // Virtual Binary Point Register 1
1110 case MISCREG_ICV_BPR1_EL1: {
1111 Gicv3::GroupId group =
1112 misc_reg == MISCREG_ICV_BPR0_EL1 ? Gicv3::G0S : Gicv3::G1NS;
1113 ICH_VMCR_EL2 ich_vmcr_el2 =
1114 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1115
1116 if ((group == Gicv3::G1NS) && ich_vmcr_el2.VCBPR) {
1117 // BPR0 + 1 saturated to 7, WI
1118 return;
1119 }
1120
1121 uint8_t min_VPBR = 7 - VIRTUAL_PREEMPTION_BITS;
1122
1123 if (group != Gicv3::G0S) {
1124 min_VPBR++;
1125 }
1126
1127 if (val < min_VPBR) {
1128 val = min_VPBR;
1129 }
1130
1131 if (group == Gicv3::G0S) {
1132 ich_vmcr_el2.VBPR0 = val;
1133 } else {
1134 ich_vmcr_el2.VBPR1 = val;
1135 }
1136
1137 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1138 do_virtual_update = true;
1139 break;
1140 }
1141
1142 // Control Register EL1
1143 case MISCREG_ICC_CTLR:
1144 case MISCREG_ICC_CTLR_EL1: {
1145 if ((currEL() == EL1) && !inSecureState() && (hcr_imo || hcr_fmo)) {
1146 return setMiscReg(MISCREG_ICV_CTLR_EL1, val);
1147 }
1148
1149 /*
1150 * ExtRange is RO.
1151 * RSS is RO.
1152 * A3V is RO.
1153 * SEIS is RO.
1154 * IDbits is RO.
1155 * PRIbits is RO.
1156 */
1157 ICC_CTLR_EL1 requested_icc_ctlr_el1 = val;
1158 ICC_CTLR_EL1 icc_ctlr_el1 =
1159 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1);
1160
1161 ICC_CTLR_EL3 icc_ctlr_el3 =
1162 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL3);
1163
1164 // The following could be refactored but it is following
1165 // spec description section 9.2.6 point by point.
1166
1167 // PMHE
1168 if (haveEL(EL3)) {
1169 // PMHE is alias of ICC_CTLR_EL3.PMHE
1170
1171 if (distributor->DS == 0) {
1172 // PMHE is RO
1173 } else if (distributor->DS == 1) {
1174 // PMHE is RW
1175 icc_ctlr_el1.PMHE = requested_icc_ctlr_el1.PMHE;
1176 icc_ctlr_el3.PMHE = icc_ctlr_el1.PMHE;
1177 }
1178 } else {
1179 // PMHE is RW (by implementation choice)
1180 icc_ctlr_el1.PMHE = requested_icc_ctlr_el1.PMHE;
1181 }
1182
1183 // EOImode
1184 icc_ctlr_el1.EOImode = requested_icc_ctlr_el1.EOImode;
1185
1186 if (inSecureState()) {
1187 // EOIMode is alias of ICC_CTLR_EL3.EOImode_EL1S
1188 icc_ctlr_el3.EOImode_EL1S = icc_ctlr_el1.EOImode;
1189 } else {
1190 // EOIMode is alias of ICC_CTLR_EL3.EOImode_EL1NS
1191 icc_ctlr_el3.EOImode_EL1NS = icc_ctlr_el1.EOImode;
1192 }
1193
1194 // CBPR
1195 if (haveEL(EL3)) {
1196 // CBPR is alias of ICC_CTLR_EL3.CBPR_EL1{S,NS}
1197
1198 if (distributor->DS == 0) {
1199 // CBPR is RO
1200 } else {
1201 // CBPR is RW
1202 icc_ctlr_el1.CBPR = requested_icc_ctlr_el1.CBPR;
1203
1204 if (inSecureState()) {
1205 icc_ctlr_el3.CBPR_EL1S = icc_ctlr_el1.CBPR;
1206 } else {
1207 icc_ctlr_el3.CBPR_EL1NS = icc_ctlr_el1.CBPR;
1208 }
1209 }
1210 } else {
1211 // CBPR is RW
1212 icc_ctlr_el1.CBPR = requested_icc_ctlr_el1.CBPR;
1213 }
1214
1215 isa->setMiscRegNoEffect(MISCREG_ICC_CTLR_EL3, icc_ctlr_el3);
1216
1217 val = icc_ctlr_el1;
1218 break;
1219 }
1220
1221 // Virtual Control Register
1222 case MISCREG_ICV_CTLR_EL1: {
1223 ICV_CTLR_EL1 requested_icv_ctlr_el1 = val;
1224 ICV_CTLR_EL1 icv_ctlr_el1 =
1225 isa->readMiscRegNoEffect(MISCREG_ICV_CTLR_EL1);
1226 icv_ctlr_el1.EOImode = requested_icv_ctlr_el1.EOImode;
1227 icv_ctlr_el1.CBPR = requested_icv_ctlr_el1.CBPR;
1228 val = icv_ctlr_el1;
1229
1230 // Aliases
1231 // ICV_CTLR_EL1.CBPR aliases ICH_VMCR_EL2.VCBPR.
1232 // ICV_CTLR_EL1.EOImode aliases ICH_VMCR_EL2.VEOIM.
1233 ICH_VMCR_EL2 ich_vmcr_el2 =
1234 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1235 ich_vmcr_el2.VCBPR = icv_ctlr_el1.CBPR;
1236 ich_vmcr_el2.VEOIM = icv_ctlr_el1.EOImode;
1237 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1238 break;
1239 }
1240
1241 // Control Register EL3
1242 case MISCREG_ICC_MCTLR:
1243 case MISCREG_ICC_CTLR_EL3: {
1244 /*
1245 * ExtRange is RO.
1246 * RSS is RO.
1247 * nDS is RO.
1248 * A3V is RO.
1249 * SEIS is RO.
1250 * IDbits is RO.
1251 * PRIbits is RO.
1252 * PMHE is RAO/WI, priority-based routing is always used.
1253 */
1254 ICC_CTLR_EL3 requested_icc_ctlr_el3 = val;
1255
1256 // Aliases
1257 if (haveEL(EL3))
1258 {
1259 ICC_CTLR_EL1 icc_ctlr_el1_s =
1260 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S);
1261 ICC_CTLR_EL1 icc_ctlr_el1_ns =
1262 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS);
1263
1264 // ICC_CTLR_EL1(NS).EOImode is an alias of
1265 // ICC_CTLR_EL3.EOImode_EL1NS
1266 icc_ctlr_el1_ns.EOImode = requested_icc_ctlr_el3.EOImode_EL1NS;
1267 // ICC_CTLR_EL1(S).EOImode is an alias of
1268 // ICC_CTLR_EL3.EOImode_EL1S
1269 icc_ctlr_el1_s.EOImode = requested_icc_ctlr_el3.EOImode_EL1S;
1270 // ICC_CTLR_EL1(NS).CBPR is an alias of ICC_CTLR_EL3.CBPR_EL1NS
1271 icc_ctlr_el1_ns.CBPR = requested_icc_ctlr_el3.CBPR_EL1NS;
1272 // ICC_CTLR_EL1(S).CBPR is an alias of ICC_CTLR_EL3.CBPR_EL1S
1273 icc_ctlr_el1_s.CBPR = requested_icc_ctlr_el3.CBPR_EL1S;
1274
1275 isa->setMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S, icc_ctlr_el1_s);
1276 isa->setMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS,
1277 icc_ctlr_el1_ns);
1278 }
1279
1280 ICC_CTLR_EL3 icc_ctlr_el3 =
1281 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL3);
1282
1283 icc_ctlr_el3.RM = requested_icc_ctlr_el3.RM;
1284 icc_ctlr_el3.EOImode_EL1NS = requested_icc_ctlr_el3.EOImode_EL1NS;
1285 icc_ctlr_el3.EOImode_EL1S = requested_icc_ctlr_el3.EOImode_EL1S;
1286 icc_ctlr_el3.EOImode_EL3 = requested_icc_ctlr_el3.EOImode_EL3;
1287 icc_ctlr_el3.CBPR_EL1NS = requested_icc_ctlr_el3.CBPR_EL1NS;
1288 icc_ctlr_el3.CBPR_EL1S = requested_icc_ctlr_el3.CBPR_EL1S;
1289
1290 val = icc_ctlr_el3;
1291 break;
1292 }
1293
1294 // Priority Mask Register
1295 case MISCREG_ICC_PMR:
1296 case MISCREG_ICC_PMR_EL1: {
1297 if ((currEL() == EL1) && !inSecureState() && (hcr_imo || hcr_fmo)) {
1276 return isa->setMiscRegNoEffect(MISCREG_ICV_PMR_EL1, val);
1298 return setMiscReg(MISCREG_ICV_PMR_EL1, val);
1277 }
1278
1279 val &= 0xff;
1280 SCR scr_el3 = isa->readMiscRegNoEffect(MISCREG_SCR_EL3);
1281
1282 if (haveEL(EL3) && !inSecureState() && (scr_el3.fiq)) {
1283 // Spec section 4.8.1
1284 // For Non-secure access to ICC_PMR_EL1 SCR_EL3.FIQ == 1:
1285 RegVal old_icc_pmr_el1 =
1286 isa->readMiscRegNoEffect(MISCREG_ICC_PMR_EL1);
1287
1288 if (!(old_icc_pmr_el1 & 0x80)) {
1289 // If the current priority mask value is in the range of
1290 // 0x00-0x7F then WI
1291 return;
1292 }
1293
1294 // If the current priority mask value is in the range of
1295 // 0x80-0xFF then a write access to ICC_PMR_EL1 succeeds,
1296 // based on the Non-secure read of the priority mask value
1297 // written to the register.
1298
1299 val = (val >> 1) | 0x80;
1300 }
1301
1302 val &= ~0U << (8 - PRIORITY_BITS);
1303 break;
1304 }
1305
1299 }
1300
1301 val &= 0xff;
1302 SCR scr_el3 = isa->readMiscRegNoEffect(MISCREG_SCR_EL3);
1303
1304 if (haveEL(EL3) && !inSecureState() && (scr_el3.fiq)) {
1305 // Spec section 4.8.1
1306 // For Non-secure access to ICC_PMR_EL1 SCR_EL3.FIQ == 1:
1307 RegVal old_icc_pmr_el1 =
1308 isa->readMiscRegNoEffect(MISCREG_ICC_PMR_EL1);
1309
1310 if (!(old_icc_pmr_el1 & 0x80)) {
1311 // If the current priority mask value is in the range of
1312 // 0x00-0x7F then WI
1313 return;
1314 }
1315
1316 // If the current priority mask value is in the range of
1317 // 0x80-0xFF then a write access to ICC_PMR_EL1 succeeds,
1318 // based on the Non-secure read of the priority mask value
1319 // written to the register.
1320
1321 val = (val >> 1) | 0x80;
1322 }
1323
1324 val &= ~0U << (8 - PRIORITY_BITS);
1325 break;
1326 }
1327
1328 case MISCREG_ICV_PMR_EL1: { // Priority Mask Register
1329 ICH_VMCR_EL2 ich_vmcr_el2 =
1330 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1331 ich_vmcr_el2.VPMR = val & 0xff;
1332
1333 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1334 virtualUpdate();
1335 return;
1336 }
1337
1306 // Interrupt Group 0 Enable Register EL1
1307 case MISCREG_ICC_IGRPEN0:
1308 case MISCREG_ICC_IGRPEN0_EL1: {
1309 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
1310 return setMiscReg(MISCREG_ICV_IGRPEN0_EL1, val);
1311 }
1312
1313 break;
1314 }
1315
1316 // Virtual Interrupt Group 0 Enable register
1317 case MISCREG_ICV_IGRPEN0_EL1: {
1318 bool enable = val & 0x1;
1319 ICH_VMCR_EL2 ich_vmcr_el2 =
1320 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1321 ich_vmcr_el2.VENG0 = enable;
1322 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1323 virtualUpdate();
1324 return;
1325 }
1326
1327 // Interrupt Group 1 Enable register EL1
1328 case MISCREG_ICC_IGRPEN1:
1329 case MISCREG_ICC_IGRPEN1_EL1: {
1330 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
1331 return setMiscReg(MISCREG_ICV_IGRPEN1_EL1, val);
1332 }
1333
1334 if (haveEL(EL3)) {
1335 ICC_IGRPEN1_EL1 icc_igrpen1_el1 = val;
1336 ICC_IGRPEN1_EL3 icc_igrpen1_el3 =
1337 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL3);
1338
1339 if (inSecureState()) {
1340 // Enable is RW alias of ICC_IGRPEN1_EL3.EnableGrp1S
1341 icc_igrpen1_el3.EnableGrp1S = icc_igrpen1_el1.Enable;
1342 } else {
1343 // Enable is RW alias of ICC_IGRPEN1_EL3.EnableGrp1NS
1344 icc_igrpen1_el3.EnableGrp1NS = icc_igrpen1_el1.Enable;
1345 }
1346
1347 isa->setMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL3,
1348 icc_igrpen1_el3);
1349 }
1350
1351 break;
1352 }
1353
1354 // Virtual Interrupt Group 1 Enable register
1355 case MISCREG_ICV_IGRPEN1_EL1: {
1356 bool enable = val & 0x1;
1357 ICH_VMCR_EL2 ich_vmcr_el2 =
1358 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1359 ich_vmcr_el2.VENG1 = enable;
1360 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1361 virtualUpdate();
1362 return;
1363 }
1364
1365 // Interrupt Group 1 Enable register
1366 case MISCREG_ICC_MGRPEN1:
1367 case MISCREG_ICC_IGRPEN1_EL3: {
1368 ICC_IGRPEN1_EL3 icc_igrpen1_el3 = val;
1369 ICC_IGRPEN1_EL1 icc_igrpen1_el1 =
1370 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL1);
1371
1372 if (inSecureState()) {
1373 // ICC_IGRPEN1_EL1.Enable is RW alias of EnableGrp1S
1374 icc_igrpen1_el1.Enable = icc_igrpen1_el3.EnableGrp1S;
1375 } else {
1376 // ICC_IGRPEN1_EL1.Enable is RW alias of EnableGrp1NS
1377 icc_igrpen1_el1.Enable = icc_igrpen1_el3.EnableGrp1NS;
1378 }
1379
1380 isa->setMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL1, icc_igrpen1_el1);
1381 break;
1382 }
1383
1384 // Software Generated Interrupt Group 0 Register
1385 case MISCREG_ICC_SGI0R:
1386 case MISCREG_ICC_SGI0R_EL1:
1387
1388 // Software Generated Interrupt Group 1 Register
1389 case MISCREG_ICC_SGI1R:
1390 case MISCREG_ICC_SGI1R_EL1:
1391
1392 // Alias Software Generated Interrupt Group 1 Register
1393 case MISCREG_ICC_ASGI1R:
1394 case MISCREG_ICC_ASGI1R_EL1: {
1395 bool ns = !inSecureState();
1396 Gicv3::GroupId group;
1397
1398 if (misc_reg == MISCREG_ICC_SGI1R_EL1) {
1399 group = ns ? Gicv3::G1NS : Gicv3::G1S;
1400 } else if (misc_reg == MISCREG_ICC_ASGI1R_EL1) {
1401 group = ns ? Gicv3::G1S : Gicv3::G1NS;
1402 } else {
1403 group = Gicv3::G0S;
1404 }
1405
1406 if (distributor->DS && group == Gicv3::G1S) {
1407 group = Gicv3::G0S;
1408 }
1409
1410 uint8_t aff3 = bits(val, 55, 48);
1411 uint8_t aff2 = bits(val, 39, 32);
1412 uint8_t aff1 = bits(val, 23, 16);;
1413 uint16_t target_list = bits(val, 15, 0);
1414 uint32_t int_id = bits(val, 27, 24);
1415 bool irm = bits(val, 40, 40);
1416 uint8_t rs = bits(val, 47, 44);
1417
1418 for (int i = 0; i < gic->getSystem()->numContexts(); i++) {
1419 Gicv3Redistributor * redistributor_i =
1420 gic->getRedistributor(i);
1421 uint32_t affinity_i = redistributor_i->getAffinity();
1422
1423 if (irm) {
1424 // Interrupts routed to all PEs in the system,
1425 // excluding "self"
1426 if (affinity_i == redistributor->getAffinity()) {
1427 continue;
1428 }
1429 } else {
1430 // Interrupts routed to the PEs specified by
1431 // Aff3.Aff2.Aff1.<target list>
1432 if ((affinity_i >> 8) !=
1433 ((aff3 << 16) | (aff2 << 8) | (aff1 << 0))) {
1434 continue;
1435 }
1436
1437 uint8_t aff0_i = bits(affinity_i, 7, 0);
1438
1439 if (!(aff0_i >= rs * 16 && aff0_i < (rs + 1) * 16 &&
1440 ((0x1 << (aff0_i - rs * 16)) & target_list))) {
1441 continue;
1442 }
1443 }
1444
1445 redistributor_i->sendSGI(int_id, group, ns);
1446 }
1447
1448 break;
1449 }
1450
1451 // System Register Enable Register EL1
1452 case MISCREG_ICC_SRE:
1453 case MISCREG_ICC_SRE_EL1:
1454 // System Register Enable Register EL2
1455 case MISCREG_ICC_HSRE:
1456 case MISCREG_ICC_SRE_EL2:
1457 // System Register Enable Register EL3
1458 case MISCREG_ICC_MSRE:
1459 case MISCREG_ICC_SRE_EL3:
1460 // All bits are RAO/WI
1461 return;
1462
1463 // Hyp Control Register
1464 case MISCREG_ICH_HCR:
1465 case MISCREG_ICH_HCR_EL2: {
1466 ICH_HCR_EL2 requested_ich_hcr_el2 = val;
1467 ICH_HCR_EL2 ich_hcr_el2 =
1468 isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
1469
1470 if (requested_ich_hcr_el2.EOIcount >= ich_hcr_el2.EOIcount)
1471 {
1472 // EOIcount - Permitted behaviors are:
1473 // - Increment EOIcount.
1474 // - Leave EOIcount unchanged.
1475 ich_hcr_el2.EOIcount = requested_ich_hcr_el2.EOIcount;
1476 }
1477
1478 ich_hcr_el2.TDIR = requested_ich_hcr_el2.TDIR;
1479 ich_hcr_el2.TSEI = requested_ich_hcr_el2.TSEI;
1480 ich_hcr_el2.TALL1 = requested_ich_hcr_el2.TALL1;;
1481 ich_hcr_el2.TALL0 = requested_ich_hcr_el2.TALL0;;
1482 ich_hcr_el2.TC = requested_ich_hcr_el2.TC;
1483 ich_hcr_el2.VGrp1DIE = requested_ich_hcr_el2.VGrp1DIE;
1484 ich_hcr_el2.VGrp1EIE = requested_ich_hcr_el2.VGrp1EIE;
1485 ich_hcr_el2.VGrp0DIE = requested_ich_hcr_el2.VGrp0DIE;
1486 ich_hcr_el2.VGrp0EIE = requested_ich_hcr_el2.VGrp0EIE;
1487 ich_hcr_el2.NPIE = requested_ich_hcr_el2.NPIE;
1488 ich_hcr_el2.LRENPIE = requested_ich_hcr_el2.LRENPIE;
1489 ich_hcr_el2.UIE = requested_ich_hcr_el2.UIE;
1490 ich_hcr_el2.En = requested_ich_hcr_el2.En;
1491 val = ich_hcr_el2;
1492 do_virtual_update = true;
1493 break;
1494 }
1495
1496 // List Registers
1497 case MISCREG_ICH_LRC0 ... MISCREG_ICH_LRC15: {
1498 // AArch32 (maps to AArch64 MISCREG_ICH_LR<n>_EL2 high half part)
1499 ICH_LRC requested_ich_lrc = val;
1500 ICH_LRC ich_lrc = isa->readMiscRegNoEffect(misc_reg);
1501
1502 ich_lrc.State = requested_ich_lrc.State;
1503 ich_lrc.HW = requested_ich_lrc.HW;
1504 ich_lrc.Group = requested_ich_lrc.Group;
1505
1506 // Priority, bits [23:16]
1507 // At least five bits must be implemented.
1508 // Unimplemented bits are RES0 and start from bit[16] up to bit[18].
1509 // We implement 5 bits.
1510 ich_lrc.Priority = (requested_ich_lrc.Priority & 0xf8) |
1511 (ich_lrc.Priority & 0x07);
1512
1513 // pINTID, bits [12:0]
1514 // When ICH_LR<n>.HW is 0 this field has the following meaning:
1515 // - Bits[12:10] : RES0.
1516 // - Bit[9] : EOI.
1517 // - Bits[8:0] : RES0.
1518 // When ICH_LR<n>.HW is 1:
1519 // - This field is only required to implement enough bits to hold a
1520 // valid value for the implemented INTID size. Any unused higher
1521 // order bits are RES0.
1522 if (requested_ich_lrc.HW == 0) {
1523 ich_lrc.EOI = requested_ich_lrc.EOI;
1524 } else {
1525 ich_lrc.pINTID = requested_ich_lrc.pINTID;
1526 }
1527
1528 val = ich_lrc;
1529 do_virtual_update = true;
1530 break;
1531 }
1532
1533 // List Registers
1534 case MISCREG_ICH_LR0 ... MISCREG_ICH_LR15: {
1535 // AArch32 (maps to AArch64 MISCREG_ICH_LR<n>_EL2 low half part)
1536 RegVal old_val = isa->readMiscRegNoEffect(misc_reg);
1537 val = (old_val & 0xffffffff00000000) | (val & 0xffffffff);
1538 do_virtual_update = true;
1539 break;
1540 }
1541
1542 // List Registers
1543 case MISCREG_ICH_LR0_EL2 ... MISCREG_ICH_LR15_EL2: { // AArch64
1544 ICH_LR_EL2 requested_ich_lr_el2 = val;
1545 ICH_LR_EL2 ich_lr_el2 = isa->readMiscRegNoEffect(misc_reg);
1546
1547 ich_lr_el2.State = requested_ich_lr_el2.State;
1548 ich_lr_el2.HW = requested_ich_lr_el2.HW;
1549 ich_lr_el2.Group = requested_ich_lr_el2.Group;
1550
1551 // Priority, bits [55:48]
1552 // At least five bits must be implemented.
1553 // Unimplemented bits are RES0 and start from bit[48] up to bit[50].
1554 // We implement 5 bits.
1555 ich_lr_el2.Priority = (requested_ich_lr_el2.Priority & 0xf8) |
1556 (ich_lr_el2.Priority & 0x07);
1557
1558 // pINTID, bits [44:32]
1559 // When ICH_LR<n>_EL2.HW is 0 this field has the following meaning:
1560 // - Bits[44:42] : RES0.
1561 // - Bit[41] : EOI.
1562 // - Bits[40:32] : RES0.
1563 // When ICH_LR<n>_EL2.HW is 1:
1564 // - This field is only required to implement enough bits to hold a
1565 // valid value for the implemented INTID size. Any unused higher
1566 // order bits are RES0.
1567 if (requested_ich_lr_el2.HW == 0) {
1568 ich_lr_el2.EOI = requested_ich_lr_el2.EOI;
1569 } else {
1570 ich_lr_el2.pINTID = requested_ich_lr_el2.pINTID;
1571 }
1572
1573 // vINTID, bits [31:0]
1574 // It is IMPLEMENTATION DEFINED how many bits are implemented,
1575 // though at least 16 bits must be implemented.
1576 // Unimplemented bits are RES0.
1577 ich_lr_el2.vINTID = requested_ich_lr_el2.vINTID;
1578
1579 val = ich_lr_el2;
1580 do_virtual_update = true;
1581 break;
1582 }
1583
1584 // Virtual Machine Control Register
1585 case MISCREG_ICH_VMCR:
1586 case MISCREG_ICH_VMCR_EL2: {
1587 ICH_VMCR_EL2 requested_ich_vmcr_el2 = val;
1588 ICH_VMCR_EL2 ich_vmcr_el2 =
1589 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1590 ich_vmcr_el2.VPMR = requested_ich_vmcr_el2.VPMR;
1591 uint8_t min_vpr0 = 7 - VIRTUAL_PREEMPTION_BITS;
1592
1593 if (requested_ich_vmcr_el2.VBPR0 < min_vpr0) {
1594 ich_vmcr_el2.VBPR0 = min_vpr0;
1595 } else {
1596 ich_vmcr_el2.VBPR0 = requested_ich_vmcr_el2.VBPR0;
1597 }
1598
1599 uint8_t min_vpr1 = min_vpr0 + 1;
1600
1601 if (requested_ich_vmcr_el2.VBPR1 < min_vpr1) {
1602 ich_vmcr_el2.VBPR1 = min_vpr1;
1603 } else {
1604 ich_vmcr_el2.VBPR1 = requested_ich_vmcr_el2.VBPR1;
1605 }
1606
1607 ich_vmcr_el2.VEOIM = requested_ich_vmcr_el2.VEOIM;
1608 ich_vmcr_el2.VCBPR = requested_ich_vmcr_el2.VCBPR;
1609 ich_vmcr_el2.VENG1 = requested_ich_vmcr_el2.VENG1;
1610 ich_vmcr_el2.VENG0 = requested_ich_vmcr_el2.VENG0;
1611 val = ich_vmcr_el2;
1612 break;
1613 }
1614
1615 // Hyp Active Priorities Group 0 Registers
1616 case MISCREG_ICH_AP0R0 ... MISCREG_ICH_AP0R3:
1617 case MISCREG_ICH_AP0R0_EL2 ... MISCREG_ICH_AP0R3_EL2:
1618 // Hyp Active Priorities Group 1 Registers
1619 case MISCREG_ICH_AP1R0 ... MISCREG_ICH_AP1R3:
1620 case MISCREG_ICH_AP1R0_EL2 ... MISCREG_ICH_AP1R3_EL2:
1621 break;
1622
1623 default:
1624 panic("Gicv3CPUInterface::setMiscReg(): unknown register %d (%s)",
1625 misc_reg, miscRegName[misc_reg]);
1626 }
1627
1628 isa->setMiscRegNoEffect(misc_reg, val);
1629
1630 if (do_virtual_update) {
1631 virtualUpdate();
1632 }
1633}
1634
1635int
1636Gicv3CPUInterface::virtualFindActive(uint32_t int_id) const
1637{
1638 for (uint32_t lr_idx = 0; lr_idx < VIRTUAL_NUM_LIST_REGS; lr_idx++) {
1639 ICH_LR_EL2 ich_lr_el2 =
1640 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
1641
1642 if (((ich_lr_el2.State == ICH_LR_EL2_STATE_ACTIVE) ||
1643 (ich_lr_el2.State == ICH_LR_EL2_STATE_ACTIVE_PENDING)) &&
1644 (ich_lr_el2.vINTID == int_id)) {
1645 return lr_idx;
1646 }
1647 }
1648
1649 return -1;
1650}
1651
1652uint32_t
1653Gicv3CPUInterface::getHPPIR0() const
1654{
1655 if (hppi.prio == 0xff) {
1656 return Gicv3::INTID_SPURIOUS;
1657 }
1658
1659 bool irq_is_secure = !distributor->DS && hppi.group != Gicv3::G1NS;
1660
1661 if ((hppi.group != Gicv3::G0S) && isEL3OrMon()) {
1662 // interrupt for the other state pending
1663 return irq_is_secure ? Gicv3::INTID_SECURE : Gicv3::INTID_NONSECURE;
1664 }
1665
1666 if ((hppi.group != Gicv3::G0S)) { // && !isEL3OrMon())
1667 return Gicv3::INTID_SPURIOUS;
1668 }
1669
1670 if (irq_is_secure && !inSecureState()) {
1671 // Secure interrupts not visible in Non-secure
1672 return Gicv3::INTID_SPURIOUS;
1673 }
1674
1675 return hppi.intid;
1676}
1677
1678uint32_t
1679Gicv3CPUInterface::getHPPIR1() const
1680{
1681 if (hppi.prio == 0xff) {
1682 return Gicv3::INTID_SPURIOUS;
1683 }
1684
1685 ICC_CTLR_EL3 icc_ctlr_el3 = isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL3);
1686 if ((currEL() == EL3) && icc_ctlr_el3.RM) {
1687 if (hppi.group == Gicv3::G0S) {
1688 return Gicv3::INTID_SECURE;
1689 } else if (hppi.group == Gicv3::G1NS) {
1690 return Gicv3::INTID_NONSECURE;
1691 }
1692 }
1693
1694 if (hppi.group == Gicv3::G0S) {
1695 return Gicv3::INTID_SPURIOUS;
1696 }
1697
1698 bool irq_is_secure = (distributor->DS == 0) && (hppi.group != Gicv3::G1NS);
1699
1700 if (irq_is_secure) {
1701 if (!inSecureState()) {
1702 // Secure interrupts not visible in Non-secure
1703 return Gicv3::INTID_SPURIOUS;
1704 }
1705 } else if (!isEL3OrMon() && inSecureState()) {
1706 // Group 1 non-secure interrupts not visible in Secure EL1
1707 return Gicv3::INTID_SPURIOUS;
1708 }
1709
1710 return hppi.intid;
1711}
1712
1713void
1714Gicv3CPUInterface::dropPriority(Gicv3::GroupId group)
1715{
1716 int apr_misc_reg;
1717 RegVal apr;
1718 apr_misc_reg = group == Gicv3::G0S ?
1719 MISCREG_ICC_AP0R0_EL1 : MISCREG_ICC_AP1R0_EL1;
1720 apr = isa->readMiscRegNoEffect(apr_misc_reg);
1721
1722 if (apr) {
1723 apr &= apr - 1;
1724 isa->setMiscRegNoEffect(apr_misc_reg, apr);
1725 }
1726
1727 update();
1728}
1729
1730uint8_t
1731Gicv3CPUInterface::virtualDropPriority()
1732{
1733 int apr_max = 1 << (VIRTUAL_PREEMPTION_BITS - 5);
1734
1735 for (int i = 0; i < apr_max; i++) {
1736 RegVal vapr0 = isa->readMiscRegNoEffect(MISCREG_ICH_AP0R0_EL2 + i);
1737 RegVal vapr1 = isa->readMiscRegNoEffect(MISCREG_ICH_AP1R0_EL2 + i);
1738
1739 if (!vapr0 && !vapr1) {
1740 continue;
1741 }
1742
1743 int vapr0_count = ctz32(vapr0);
1744 int vapr1_count = ctz32(vapr1);
1745
1746 if (vapr0_count <= vapr1_count) {
1747 vapr0 &= vapr0 - 1;
1748 isa->setMiscRegNoEffect(MISCREG_ICH_AP0R0_EL2 + i, vapr0);
1749 return (vapr0_count + i * 32) << (GIC_MIN_VBPR + 1);
1750 } else {
1751 vapr1 &= vapr1 - 1;
1752 isa->setMiscRegNoEffect(MISCREG_ICH_AP1R0_EL2 + i, vapr1);
1753 return (vapr1_count + i * 32) << (GIC_MIN_VBPR + 1);
1754 }
1755 }
1756
1757 return 0xff;
1758}
1759
1760void
1761Gicv3CPUInterface::activateIRQ(uint32_t int_id, Gicv3::GroupId group)
1762{
1763 // Update active priority registers.
1764 uint32_t prio = hppi.prio & 0xf8;
1765 int apr_bit = prio >> (8 - PRIORITY_BITS);
1766 int reg_bit = apr_bit % 32;
1767 int apr_idx = group == Gicv3::G0S ?
1768 MISCREG_ICC_AP0R0_EL1 : MISCREG_ICC_AP1R0_EL1;
1769 RegVal apr = isa->readMiscRegNoEffect(apr_idx);
1770 apr |= (1 << reg_bit);
1771 isa->setMiscRegNoEffect(apr_idx, apr);
1772
1773 // Move interrupt state from pending to active.
1774 if (int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX) {
1775 // SGI or PPI, redistributor
1776 redistributor->activateIRQ(int_id);
1777 redistributor->updateAndInformCPUInterface();
1778 } else if (int_id < Gicv3::INTID_SECURE) {
1779 // SPI, distributor
1780 distributor->activateIRQ(int_id);
1781 distributor->updateAndInformCPUInterfaces();
1782 } else if (int_id >= Gicv3Redistributor::SMALLEST_LPI_ID) {
1783 // LPI, Redistributor
1784 redistributor->setClrLPI(int_id, false);
1785 }
1786}
1787
1788void
1789Gicv3CPUInterface::virtualActivateIRQ(uint32_t lr_idx)
1790{
1791 // Update active priority registers.
1792 ICH_LR_EL2 ich_lr_el = isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 +
1793 lr_idx);
1794 Gicv3::GroupId group = ich_lr_el.Group ? Gicv3::G1NS : Gicv3::G0S;
1795 uint8_t prio = ich_lr_el.Priority & 0xf8;
1796 int apr_bit = prio >> (8 - VIRTUAL_PREEMPTION_BITS);
1797 int reg_no = apr_bit / 32;
1798 int reg_bit = apr_bit % 32;
1799 int apr_idx = group == Gicv3::G0S ?
1800 MISCREG_ICH_AP0R0_EL2 + reg_no : MISCREG_ICH_AP1R0_EL2 + reg_no;
1801 RegVal apr = isa->readMiscRegNoEffect(apr_idx);
1802 apr |= (1 << reg_bit);
1803 isa->setMiscRegNoEffect(apr_idx, apr);
1804 // Move interrupt state from pending to active.
1805 ich_lr_el.State = ICH_LR_EL2_STATE_ACTIVE;
1806 isa->setMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx, ich_lr_el);
1807}
1808
1809void
1810Gicv3CPUInterface::deactivateIRQ(uint32_t int_id, Gicv3::GroupId group)
1811{
1812 if (int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX) {
1813 // SGI or PPI, redistributor
1814 redistributor->deactivateIRQ(int_id);
1815 redistributor->updateAndInformCPUInterface();
1816 } else if (int_id < Gicv3::INTID_SECURE) {
1817 // SPI, distributor
1818 distributor->deactivateIRQ(int_id);
1819 distributor->updateAndInformCPUInterfaces();
1820 } else {
1821 // LPI, redistributor, shouldn't deactivate
1822 redistributor->updateAndInformCPUInterface();
1823 }
1824}
1825
1826void
1827Gicv3CPUInterface::virtualDeactivateIRQ(int lr_idx)
1828{
1829 ICH_LR_EL2 ich_lr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 +
1830 lr_idx);
1831
1832 if (ich_lr_el2.HW) {
1833 // Deactivate the associated physical interrupt
1834 if (ich_lr_el2.pINTID < Gicv3::INTID_SECURE) {
1835 Gicv3::GroupId group = ich_lr_el2.pINTID >= 32 ?
1836 distributor->getIntGroup(ich_lr_el2.pINTID) :
1837 redistributor->getIntGroup(ich_lr_el2.pINTID);
1838 deactivateIRQ(ich_lr_el2.pINTID, group);
1839 }
1840 }
1841
1842 // Remove the active bit
1843 ich_lr_el2.State = ich_lr_el2.State & ~ICH_LR_EL2_STATE_ACTIVE;
1844 isa->setMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx, ich_lr_el2);
1845}
1846
1847/*
1848 * Returns the priority group field for the current BPR value for the group.
1849 * GroupBits() Pseudocode from spec.
1850 */
1851uint32_t
1852Gicv3CPUInterface::groupPriorityMask(Gicv3::GroupId group)
1853{
1854 ICC_CTLR_EL1 icc_ctlr_el1_s =
1855 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S);
1856 ICC_CTLR_EL1 icc_ctlr_el1_ns =
1857 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS);
1858
1859 if ((group == Gicv3::G1S && icc_ctlr_el1_s.CBPR) ||
1860 (group == Gicv3::G1NS && icc_ctlr_el1_ns.CBPR)) {
1861 group = Gicv3::G0S;
1862 }
1863
1864 int bpr;
1865
1866 if (group == Gicv3::G0S) {
1867 bpr = readMiscReg(MISCREG_ICC_BPR0_EL1) & 0x7;
1868 } else {
1869 bpr = readMiscReg(MISCREG_ICC_BPR1_EL1) & 0x7;
1870 }
1871
1872 if (group == Gicv3::G1NS) {
1873 assert(bpr > 0);
1874 bpr--;
1875 }
1876
1877 return ~0U << (bpr + 1);
1878}
1879
1880uint32_t
1881Gicv3CPUInterface::virtualGroupPriorityMask(Gicv3::GroupId group) const
1882{
1883 ICH_VMCR_EL2 ich_vmcr_el2 =
1884 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1885
1886 if ((group == Gicv3::G1NS) && ich_vmcr_el2.VCBPR) {
1887 group = Gicv3::G0S;
1888 }
1889
1890 int bpr;
1891
1892 if (group == Gicv3::G0S) {
1893 bpr = ich_vmcr_el2.VBPR0;
1894 } else {
1895 bpr = ich_vmcr_el2.VBPR1;
1896 }
1897
1898 if (group == Gicv3::G1NS) {
1899 assert(bpr > 0);
1900 bpr--;
1901 }
1902
1903 return ~0U << (bpr + 1);
1904}
1905
1906bool
1907Gicv3CPUInterface::isEOISplitMode() const
1908{
1909 if (isEL3OrMon()) {
1910 ICC_CTLR_EL3 icc_ctlr_el3 =
1911 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL3);
1912 return icc_ctlr_el3.EOImode_EL3;
1913 } else {
1914 ICC_CTLR_EL1 icc_ctlr_el1 =
1915 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1);
1916 return icc_ctlr_el1.EOImode;
1917 }
1918}
1919
1920bool
1921Gicv3CPUInterface::virtualIsEOISplitMode() const
1922{
1923 ICH_VMCR_EL2 ich_vmcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1924 return ich_vmcr_el2.VEOIM;
1925}
1926
1927int
1928Gicv3CPUInterface::highestActiveGroup() const
1929{
1930 int g0_ctz = ctz32(isa->readMiscRegNoEffect(MISCREG_ICC_AP0R0_EL1));
1931 int gq_ctz = ctz32(isa->readMiscRegNoEffect(MISCREG_ICC_AP1R0_EL1_S));
1932 int g1nz_ctz = ctz32(isa->readMiscRegNoEffect(MISCREG_ICC_AP1R0_EL1_NS));
1933
1934 if (g1nz_ctz < g0_ctz && g1nz_ctz < gq_ctz) {
1935 return Gicv3::G1NS;
1936 }
1937
1938 if (gq_ctz < g0_ctz) {
1939 return Gicv3::G1S;
1940 }
1941
1942 if (g0_ctz < 32) {
1943 return Gicv3::G0S;
1944 }
1945
1946 return -1;
1947}
1948
1949void
1950Gicv3CPUInterface::update()
1951{
1952 bool signal_IRQ = false;
1953 bool signal_FIQ = false;
1954
1955 if (hppi.group == Gicv3::G1S && !haveEL(EL3)) {
1956 /*
1957 * Secure enabled GIC sending a G1S IRQ to a secure disabled
1958 * CPU -> send G0 IRQ
1959 */
1960 hppi.group = Gicv3::G0S;
1961 }
1962
1963 if (hppiCanPreempt()) {
1964 ArmISA::InterruptTypes int_type = intSignalType(hppi.group);
1965 DPRINTF(GIC, "Gicv3CPUInterface::update(): "
1966 "posting int as %d!\n", int_type);
1967 int_type == ArmISA::INT_IRQ ? signal_IRQ = true : signal_FIQ = true;
1968 }
1969
1970 if (signal_IRQ) {
1971 gic->postInt(cpuId, ArmISA::INT_IRQ);
1972 } else {
1973 gic->deassertInt(cpuId, ArmISA::INT_IRQ);
1974 }
1975
1976 if (signal_FIQ) {
1977 gic->postInt(cpuId, ArmISA::INT_FIQ);
1978 } else {
1979 gic->deassertInt(cpuId, ArmISA::INT_FIQ);
1980 }
1981}
1982
1983void
1984Gicv3CPUInterface::virtualUpdate()
1985{
1986 bool signal_IRQ = false;
1987 bool signal_FIQ = false;
1988 int lr_idx = getHPPVILR();
1989
1990 if (lr_idx >= 0) {
1991 ICH_LR_EL2 ich_lr_el2 =
1992 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
1993
1994 if (hppviCanPreempt(lr_idx)) {
1995 if (ich_lr_el2.Group) {
1996 signal_IRQ = true;
1997 } else {
1998 signal_FIQ = true;
1999 }
2000 }
2001 }
2002
2003 ICH_HCR_EL2 ich_hcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
2004
2005 if (ich_hcr_el2.En) {
2006 if (maintenanceInterruptStatus()) {
2007 maintenanceInterrupt->raise();
2008 }
2009 }
2010
2011 if (signal_IRQ) {
2012 DPRINTF(GIC, "Gicv3CPUInterface::virtualUpdate(): "
2013 "posting int as %d!\n", ArmISA::INT_VIRT_IRQ);
2014 gic->postInt(cpuId, ArmISA::INT_VIRT_IRQ);
2015 } else {
2016 gic->deassertInt(cpuId, ArmISA::INT_VIRT_IRQ);
2017 }
2018
2019 if (signal_FIQ) {
2020 DPRINTF(GIC, "Gicv3CPUInterface::virtualUpdate(): "
2021 "posting int as %d!\n", ArmISA::INT_VIRT_FIQ);
2022 gic->postInt(cpuId, ArmISA::INT_VIRT_FIQ);
2023 } else {
2024 gic->deassertInt(cpuId, ArmISA::INT_VIRT_FIQ);
2025 }
2026}
2027
2028// Returns the index of the LR with the HPPI
2029int
2030Gicv3CPUInterface::getHPPVILR() const
2031{
2032 int idx = -1;
2033 ICH_VMCR_EL2 ich_vmcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
2034
2035 if (!ich_vmcr_el2.VENG0 && !ich_vmcr_el2.VENG1) {
2036 // VG0 and VG1 disabled...
2037 return idx;
2038 }
2039
2040 uint8_t highest_prio = 0xff;
2041
2042 for (int i = 0; i < 16; i++) {
2043 ICH_LR_EL2 ich_lr_el2 =
2044 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + i);
2045
2046 if (ich_lr_el2.State != Gicv3::INT_PENDING) {
2047 continue;
2048 }
2049
2050 if (ich_lr_el2.Group) {
2051 // VG1
2052 if (!ich_vmcr_el2.VENG1) {
2053 continue;
2054 }
2055 } else {
2056 // VG0
2057 if (!ich_vmcr_el2.VENG0) {
2058 continue;
2059 }
2060 }
2061
2062 uint8_t prio = ich_lr_el2.Priority;
2063
2064 if (prio < highest_prio) {
2065 highest_prio = prio;
2066 idx = i;
2067 }
2068 }
2069
2070 return idx;
2071}
2072
2073bool
2074Gicv3CPUInterface::hppviCanPreempt(int lr_idx) const
2075{
2076 ICH_HCR_EL2 ich_hcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
2077 if (!ich_hcr_el2.En) {
2078 // virtual interface is disabled
2079 return false;
2080 }
2081
2082 ICH_LR_EL2 ich_lr_el2 =
2083 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
2084 uint8_t prio = ich_lr_el2.Priority;
2085 uint8_t vpmr =
2086 bits(isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2), 31, 24);
2087
2088 if (prio >= vpmr) {
2089 // prioriry masked
2090 return false;
2091 }
2092
2093 uint8_t rprio = virtualHighestActivePriority();
2094
2095 if (rprio == 0xff) {
2096 return true;
2097 }
2098
2099 Gicv3::GroupId group = ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
2100 uint32_t prio_mask = virtualGroupPriorityMask(group);
2101
2102 if ((prio & prio_mask) < (rprio & prio_mask)) {
2103 return true;
2104 }
2105
2106 return false;
2107}
2108
2109uint8_t
2110Gicv3CPUInterface::virtualHighestActivePriority() const
2111{
2112 uint8_t num_aprs = 1 << (VIRTUAL_PRIORITY_BITS - 5);
2113
2114 for (int i = 0; i < num_aprs; i++) {
2115 RegVal vapr =
2116 isa->readMiscRegNoEffect(MISCREG_ICH_AP0R0_EL2 + i) |
2117 isa->readMiscRegNoEffect(MISCREG_ICH_AP1R0_EL2 + i);
2118
2119 if (!vapr) {
2120 continue;
2121 }
2122
2123 return (i * 32 + ctz32(vapr)) << (GIC_MIN_VBPR + 1);
2124 }
2125
2126 // no active interrups, return idle priority
2127 return 0xff;
2128}
2129
2130void
2131Gicv3CPUInterface::virtualIncrementEOICount()
2132{
2133 // Increment the EOICOUNT field in ICH_HCR_EL2
2134 RegVal ich_hcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
2135 uint32_t EOI_cout = bits(ich_hcr_el2, 31, 27);
2136 EOI_cout++;
2137 ich_hcr_el2 = insertBits(ich_hcr_el2, 31, 27, EOI_cout);
2138 isa->setMiscRegNoEffect(MISCREG_ICH_HCR_EL2, ich_hcr_el2);
2139}
2140
2141// spec section 4.6.2
2142ArmISA::InterruptTypes
2143Gicv3CPUInterface::intSignalType(Gicv3::GroupId group) const
2144{
2145 bool is_fiq = false;
2146
2147 switch (group) {
2148 case Gicv3::G0S:
2149 is_fiq = true;
2150 break;
2151
2152 case Gicv3::G1S:
2153 is_fiq = (distributor->DS == 0) &&
2154 (!inSecureState() || ((currEL() == EL3) && isAA64()));
2155 break;
2156
2157 case Gicv3::G1NS:
2158 is_fiq = (distributor->DS == 0) && inSecureState();
2159 break;
2160
2161 default:
2162 panic("Gicv3CPUInterface::intSignalType(): invalid group!");
2163 }
2164
2165 if (is_fiq) {
2166 return ArmISA::INT_FIQ;
2167 } else {
2168 return ArmISA::INT_IRQ;
2169 }
2170}
2171
2172bool
2173Gicv3CPUInterface::hppiCanPreempt()
2174{
2175 if (hppi.prio == 0xff) {
2176 // there is no pending interrupt
2177 return false;
2178 }
2179
2180 if (!groupEnabled(hppi.group)) {
2181 // group disabled at CPU interface
2182 return false;
2183 }
2184
2185 if (hppi.prio >= isa->readMiscRegNoEffect(MISCREG_ICC_PMR_EL1)) {
2186 // priority masked
2187 return false;
2188 }
2189
2190 uint8_t rprio = highestActivePriority();
2191
2192 if (rprio == 0xff) {
2193 return true;
2194 }
2195
2196 uint32_t prio_mask = groupPriorityMask(hppi.group);
2197
2198 if ((hppi.prio & prio_mask) < (rprio & prio_mask)) {
2199 return true;
2200 }
2201
2202 return false;
2203}
2204
2205uint8_t
2206Gicv3CPUInterface::highestActivePriority() const
2207{
2208 uint32_t apr = isa->readMiscRegNoEffect(MISCREG_ICC_AP0R0_EL1) |
2209 isa->readMiscRegNoEffect(MISCREG_ICC_AP1R0_EL1_NS) |
2210 isa->readMiscRegNoEffect(MISCREG_ICC_AP1R0_EL1_S);
2211
2212 if (apr) {
2213 return ctz32(apr) << (GIC_MIN_BPR + 1);
2214 }
2215
2216 // no active interrups, return idle priority
2217 return 0xff;
2218}
2219
2220bool
2221Gicv3CPUInterface::groupEnabled(Gicv3::GroupId group) const
2222{
2223 switch (group) {
2224 case Gicv3::G0S: {
2225 ICC_IGRPEN0_EL1 icc_igrpen0_el1 =
2226 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN0_EL1);
2227 return icc_igrpen0_el1.Enable;
2228 }
2229
2230 case Gicv3::G1S: {
2231 ICC_IGRPEN1_EL1 icc_igrpen1_el1_s =
2232 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL1_S);
2233 return icc_igrpen1_el1_s.Enable;
2234 }
2235
2236 case Gicv3::G1NS: {
2237 ICC_IGRPEN1_EL1 icc_igrpen1_el1_ns =
2238 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL1_NS);
2239 return icc_igrpen1_el1_ns.Enable;
2240 }
2241
2242 default:
2243 panic("Gicv3CPUInterface::groupEnable(): invalid group!\n");
2244 }
2245}
2246
2247bool
2248Gicv3CPUInterface::inSecureState() const
2249{
2250 if (!gic->getSystem()->haveSecurity()) {
2251 return false;
2252 }
2253
2254 CPSR cpsr = isa->readMiscRegNoEffect(MISCREG_CPSR);
2255 SCR scr = isa->readMiscRegNoEffect(MISCREG_SCR);
2256 return ArmISA::inSecureState(scr, cpsr);
2257}
2258
2259int
2260Gicv3CPUInterface::currEL() const
2261{
2262 CPSR cpsr = isa->readMiscRegNoEffect(MISCREG_CPSR);
2263 bool is_64 = opModeIs64((OperatingMode)(uint8_t) cpsr.mode);
2264
2265 if (is_64) {
2266 return (ExceptionLevel)(uint8_t) cpsr.el;
2267 } else {
2268 switch (cpsr.mode) {
2269 case MODE_USER:
2270 return 0;
2271
2272 case MODE_HYP:
2273 return 2;
2274
2275 case MODE_MON:
2276 return 3;
2277
2278 default:
2279 return 1;
2280 }
2281 }
2282}
2283
2284bool
2285Gicv3CPUInterface::haveEL(ExceptionLevel el) const
2286{
2287 switch (el) {
2288 case EL0:
2289 case EL1:
2290 return true;
2291
2292 case EL2:
2293 return gic->getSystem()->haveVirtualization();
2294
2295 case EL3:
2296 return gic->getSystem()->haveSecurity();
2297
2298 default:
2299 warn("Unimplemented Exception Level\n");
2300 return false;
2301 }
2302}
2303
2304bool
2305Gicv3CPUInterface::isSecureBelowEL3() const
2306{
2307 SCR scr = isa->readMiscRegNoEffect(MISCREG_SCR_EL3);
2308 return haveEL(EL3) && scr.ns == 0;
2309}
2310
2311bool
2312Gicv3CPUInterface::isAA64() const
2313{
2314 CPSR cpsr = isa->readMiscRegNoEffect(MISCREG_CPSR);
2315 return opModeIs64((OperatingMode)(uint8_t) cpsr.mode);
2316}
2317
2318bool
2319Gicv3CPUInterface::isEL3OrMon() const
2320{
2321 if (haveEL(EL3)) {
2322 CPSR cpsr = isa->readMiscRegNoEffect(MISCREG_CPSR);
2323 bool is_64 = opModeIs64((OperatingMode)(uint8_t) cpsr.mode);
2324
2325 if (is_64 && (cpsr.el == EL3)) {
2326 return true;
2327 } else if (!is_64 && (cpsr.mode == MODE_MON)) {
2328 return true;
2329 }
2330 }
2331
2332 return false;
2333}
2334
2335// Computes ICH_EISR_EL2
2336uint64_t
2337Gicv3CPUInterface::eoiMaintenanceInterruptStatus() const
2338{
2339 // ICH_EISR_EL2
2340 // Bits [63:16] - RES0
2341 // Status<n>, bit [n], for n = 0 to 15
2342 // EOI maintenance interrupt status bit for List register <n>:
2343 // 0 if List register <n>, ICH_LR<n>_EL2, does not have an EOI
2344 // maintenance interrupt.
2345 // 1 if List register <n>, ICH_LR<n>_EL2, has an EOI maintenance
2346 // interrupt that has not been handled.
2347 //
2348 // For any ICH_LR<n>_EL2, the corresponding status bit is set to 1 if all
2349 // of the following are true:
2350 // - ICH_LR<n>_EL2.State is 0b00 (ICH_LR_EL2_STATE_INVALID).
2351 // - ICH_LR<n>_EL2.HW is 0.
2352 // - ICH_LR<n>_EL2.EOI (bit [41]) is 1.
2353
2354 uint64_t value = 0;
2355
2356 for (int lr_idx = 0; lr_idx < VIRTUAL_NUM_LIST_REGS; lr_idx++) {
2357 ICH_LR_EL2 ich_lr_el2 =
2358 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
2359
2360 if ((ich_lr_el2.State == ICH_LR_EL2_STATE_INVALID) &&
2361 !ich_lr_el2.HW && ich_lr_el2.EOI) {
2362 value |= (1 << lr_idx);
2363 }
2364 }
2365
2366 return value;
2367}
2368
2369Gicv3CPUInterface::ICH_MISR_EL2
2370Gicv3CPUInterface::maintenanceInterruptStatus() const
2371{
2372 // Comments are copied from SPEC section 9.4.7 (ID012119)
2373 ICH_MISR_EL2 ich_misr_el2 = 0;
2374 ICH_HCR_EL2 ich_hcr_el2 =
2375 isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
2376 ICH_VMCR_EL2 ich_vmcr_el2 =
2377 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
2378
2379 // End Of Interrupt. [bit 0]
2380 // This maintenance interrupt is asserted when at least one bit in
2381 // ICH_EISR_EL2 is 1.
2382
2383 if (eoiMaintenanceInterruptStatus()) {
2384 ich_misr_el2.EOI = 1;
2385 }
2386
2387 // Underflow. [bit 1]
2388 // This maintenance interrupt is asserted when ICH_HCR_EL2.UIE==1 and
2389 // zero or one of the List register entries are marked as a valid
2390 // interrupt, that is, if the corresponding ICH_LR<n>_EL2.State bits
2391 // do not equal 0x0.
2392 uint32_t num_valid_interrupts = 0;
2393 uint32_t num_pending_interrupts = 0;
2394
2395 for (int lr_idx = 0; lr_idx < VIRTUAL_NUM_LIST_REGS; lr_idx++) {
2396 ICH_LR_EL2 ich_lr_el2 =
2397 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
2398
2399 if (ich_lr_el2.State != ICH_LR_EL2_STATE_INVALID) {
2400 num_valid_interrupts++;
2401 }
2402
2403 if (ich_lr_el2.State == ICH_LR_EL2_STATE_PENDING) {
2404 num_pending_interrupts++;
2405 }
2406 }
2407
2408 if (ich_hcr_el2.UIE && (num_valid_interrupts < 2)) {
2409 ich_misr_el2.U = 1;
2410 }
2411
2412 // List Register Entry Not Present. [bit 2]
2413 // This maintenance interrupt is asserted when ICH_HCR_EL2.LRENPIE==1
2414 // and ICH_HCR_EL2.EOIcount is non-zero.
2415 if (ich_hcr_el2.LRENPIE && ich_hcr_el2.EOIcount) {
2416 ich_misr_el2.LRENP = 1;
2417 }
2418
2419 // No Pending. [bit 3]
2420 // This maintenance interrupt is asserted when ICH_HCR_EL2.NPIE==1 and
2421 // no List register is in pending state.
2422 if (ich_hcr_el2.NPIE && (num_pending_interrupts == 0)) {
2423 ich_misr_el2.NP = 1;
2424 }
2425
2426 // vPE Group 0 Enabled. [bit 4]
2427 // This maintenance interrupt is asserted when
2428 // ICH_HCR_EL2.VGrp0EIE==1 and ICH_VMCR_EL2.VENG0==1.
2429 if (ich_hcr_el2.VGrp0EIE && ich_vmcr_el2.VENG0) {
2430 ich_misr_el2.VGrp0E = 1;
2431 }
2432
2433 // vPE Group 0 Disabled. [bit 5]
2434 // This maintenance interrupt is asserted when
2435 // ICH_HCR_EL2.VGrp0DIE==1 and ICH_VMCR_EL2.VENG0==0.
2436 if (ich_hcr_el2.VGrp0DIE && !ich_vmcr_el2.VENG0) {
2437 ich_misr_el2.VGrp0D = 1;
2438 }
2439
2440 // vPE Group 1 Enabled. [bit 6]
2441 // This maintenance interrupt is asserted when
2442 // ICH_HCR_EL2.VGrp1EIE==1 and ICH_VMCR_EL2.VENG1==is 1.
2443 if (ich_hcr_el2.VGrp1EIE && ich_vmcr_el2.VENG1) {
2444 ich_misr_el2.VGrp1E = 1;
2445 }
2446
2447 // vPE Group 1 Disabled. [bit 7]
2448 // This maintenance interrupt is asserted when
2449 // ICH_HCR_EL2.VGrp1DIE==1 and ICH_VMCR_EL2.VENG1==is 0.
2450 if (ich_hcr_el2.VGrp1DIE && !ich_vmcr_el2.VENG1) {
2451 ich_misr_el2.VGrp1D = 1;
2452 }
2453
2454 return ich_misr_el2;
2455}
2456
2457void
2458Gicv3CPUInterface::serialize(CheckpointOut & cp) const
2459{
2460 SERIALIZE_SCALAR(hppi.intid);
2461 SERIALIZE_SCALAR(hppi.prio);
2462 SERIALIZE_ENUM(hppi.group);
2463}
2464
2465void
2466Gicv3CPUInterface::unserialize(CheckpointIn & cp)
2467{
2468 UNSERIALIZE_SCALAR(hppi.intid);
2469 UNSERIALIZE_SCALAR(hppi.prio);
2470 UNSERIALIZE_ENUM(hppi.group);
2471}
1338 // Interrupt Group 0 Enable Register EL1
1339 case MISCREG_ICC_IGRPEN0:
1340 case MISCREG_ICC_IGRPEN0_EL1: {
1341 if ((currEL() == EL1) && !inSecureState() && hcr_fmo) {
1342 return setMiscReg(MISCREG_ICV_IGRPEN0_EL1, val);
1343 }
1344
1345 break;
1346 }
1347
1348 // Virtual Interrupt Group 0 Enable register
1349 case MISCREG_ICV_IGRPEN0_EL1: {
1350 bool enable = val & 0x1;
1351 ICH_VMCR_EL2 ich_vmcr_el2 =
1352 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1353 ich_vmcr_el2.VENG0 = enable;
1354 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1355 virtualUpdate();
1356 return;
1357 }
1358
1359 // Interrupt Group 1 Enable register EL1
1360 case MISCREG_ICC_IGRPEN1:
1361 case MISCREG_ICC_IGRPEN1_EL1: {
1362 if ((currEL() == EL1) && !inSecureState() && hcr_imo) {
1363 return setMiscReg(MISCREG_ICV_IGRPEN1_EL1, val);
1364 }
1365
1366 if (haveEL(EL3)) {
1367 ICC_IGRPEN1_EL1 icc_igrpen1_el1 = val;
1368 ICC_IGRPEN1_EL3 icc_igrpen1_el3 =
1369 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL3);
1370
1371 if (inSecureState()) {
1372 // Enable is RW alias of ICC_IGRPEN1_EL3.EnableGrp1S
1373 icc_igrpen1_el3.EnableGrp1S = icc_igrpen1_el1.Enable;
1374 } else {
1375 // Enable is RW alias of ICC_IGRPEN1_EL3.EnableGrp1NS
1376 icc_igrpen1_el3.EnableGrp1NS = icc_igrpen1_el1.Enable;
1377 }
1378
1379 isa->setMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL3,
1380 icc_igrpen1_el3);
1381 }
1382
1383 break;
1384 }
1385
1386 // Virtual Interrupt Group 1 Enable register
1387 case MISCREG_ICV_IGRPEN1_EL1: {
1388 bool enable = val & 0x1;
1389 ICH_VMCR_EL2 ich_vmcr_el2 =
1390 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1391 ich_vmcr_el2.VENG1 = enable;
1392 isa->setMiscRegNoEffect(MISCREG_ICH_VMCR_EL2, ich_vmcr_el2);
1393 virtualUpdate();
1394 return;
1395 }
1396
1397 // Interrupt Group 1 Enable register
1398 case MISCREG_ICC_MGRPEN1:
1399 case MISCREG_ICC_IGRPEN1_EL3: {
1400 ICC_IGRPEN1_EL3 icc_igrpen1_el3 = val;
1401 ICC_IGRPEN1_EL1 icc_igrpen1_el1 =
1402 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL1);
1403
1404 if (inSecureState()) {
1405 // ICC_IGRPEN1_EL1.Enable is RW alias of EnableGrp1S
1406 icc_igrpen1_el1.Enable = icc_igrpen1_el3.EnableGrp1S;
1407 } else {
1408 // ICC_IGRPEN1_EL1.Enable is RW alias of EnableGrp1NS
1409 icc_igrpen1_el1.Enable = icc_igrpen1_el3.EnableGrp1NS;
1410 }
1411
1412 isa->setMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL1, icc_igrpen1_el1);
1413 break;
1414 }
1415
1416 // Software Generated Interrupt Group 0 Register
1417 case MISCREG_ICC_SGI0R:
1418 case MISCREG_ICC_SGI0R_EL1:
1419
1420 // Software Generated Interrupt Group 1 Register
1421 case MISCREG_ICC_SGI1R:
1422 case MISCREG_ICC_SGI1R_EL1:
1423
1424 // Alias Software Generated Interrupt Group 1 Register
1425 case MISCREG_ICC_ASGI1R:
1426 case MISCREG_ICC_ASGI1R_EL1: {
1427 bool ns = !inSecureState();
1428 Gicv3::GroupId group;
1429
1430 if (misc_reg == MISCREG_ICC_SGI1R_EL1) {
1431 group = ns ? Gicv3::G1NS : Gicv3::G1S;
1432 } else if (misc_reg == MISCREG_ICC_ASGI1R_EL1) {
1433 group = ns ? Gicv3::G1S : Gicv3::G1NS;
1434 } else {
1435 group = Gicv3::G0S;
1436 }
1437
1438 if (distributor->DS && group == Gicv3::G1S) {
1439 group = Gicv3::G0S;
1440 }
1441
1442 uint8_t aff3 = bits(val, 55, 48);
1443 uint8_t aff2 = bits(val, 39, 32);
1444 uint8_t aff1 = bits(val, 23, 16);;
1445 uint16_t target_list = bits(val, 15, 0);
1446 uint32_t int_id = bits(val, 27, 24);
1447 bool irm = bits(val, 40, 40);
1448 uint8_t rs = bits(val, 47, 44);
1449
1450 for (int i = 0; i < gic->getSystem()->numContexts(); i++) {
1451 Gicv3Redistributor * redistributor_i =
1452 gic->getRedistributor(i);
1453 uint32_t affinity_i = redistributor_i->getAffinity();
1454
1455 if (irm) {
1456 // Interrupts routed to all PEs in the system,
1457 // excluding "self"
1458 if (affinity_i == redistributor->getAffinity()) {
1459 continue;
1460 }
1461 } else {
1462 // Interrupts routed to the PEs specified by
1463 // Aff3.Aff2.Aff1.<target list>
1464 if ((affinity_i >> 8) !=
1465 ((aff3 << 16) | (aff2 << 8) | (aff1 << 0))) {
1466 continue;
1467 }
1468
1469 uint8_t aff0_i = bits(affinity_i, 7, 0);
1470
1471 if (!(aff0_i >= rs * 16 && aff0_i < (rs + 1) * 16 &&
1472 ((0x1 << (aff0_i - rs * 16)) & target_list))) {
1473 continue;
1474 }
1475 }
1476
1477 redistributor_i->sendSGI(int_id, group, ns);
1478 }
1479
1480 break;
1481 }
1482
1483 // System Register Enable Register EL1
1484 case MISCREG_ICC_SRE:
1485 case MISCREG_ICC_SRE_EL1:
1486 // System Register Enable Register EL2
1487 case MISCREG_ICC_HSRE:
1488 case MISCREG_ICC_SRE_EL2:
1489 // System Register Enable Register EL3
1490 case MISCREG_ICC_MSRE:
1491 case MISCREG_ICC_SRE_EL3:
1492 // All bits are RAO/WI
1493 return;
1494
1495 // Hyp Control Register
1496 case MISCREG_ICH_HCR:
1497 case MISCREG_ICH_HCR_EL2: {
1498 ICH_HCR_EL2 requested_ich_hcr_el2 = val;
1499 ICH_HCR_EL2 ich_hcr_el2 =
1500 isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
1501
1502 if (requested_ich_hcr_el2.EOIcount >= ich_hcr_el2.EOIcount)
1503 {
1504 // EOIcount - Permitted behaviors are:
1505 // - Increment EOIcount.
1506 // - Leave EOIcount unchanged.
1507 ich_hcr_el2.EOIcount = requested_ich_hcr_el2.EOIcount;
1508 }
1509
1510 ich_hcr_el2.TDIR = requested_ich_hcr_el2.TDIR;
1511 ich_hcr_el2.TSEI = requested_ich_hcr_el2.TSEI;
1512 ich_hcr_el2.TALL1 = requested_ich_hcr_el2.TALL1;;
1513 ich_hcr_el2.TALL0 = requested_ich_hcr_el2.TALL0;;
1514 ich_hcr_el2.TC = requested_ich_hcr_el2.TC;
1515 ich_hcr_el2.VGrp1DIE = requested_ich_hcr_el2.VGrp1DIE;
1516 ich_hcr_el2.VGrp1EIE = requested_ich_hcr_el2.VGrp1EIE;
1517 ich_hcr_el2.VGrp0DIE = requested_ich_hcr_el2.VGrp0DIE;
1518 ich_hcr_el2.VGrp0EIE = requested_ich_hcr_el2.VGrp0EIE;
1519 ich_hcr_el2.NPIE = requested_ich_hcr_el2.NPIE;
1520 ich_hcr_el2.LRENPIE = requested_ich_hcr_el2.LRENPIE;
1521 ich_hcr_el2.UIE = requested_ich_hcr_el2.UIE;
1522 ich_hcr_el2.En = requested_ich_hcr_el2.En;
1523 val = ich_hcr_el2;
1524 do_virtual_update = true;
1525 break;
1526 }
1527
1528 // List Registers
1529 case MISCREG_ICH_LRC0 ... MISCREG_ICH_LRC15: {
1530 // AArch32 (maps to AArch64 MISCREG_ICH_LR<n>_EL2 high half part)
1531 ICH_LRC requested_ich_lrc = val;
1532 ICH_LRC ich_lrc = isa->readMiscRegNoEffect(misc_reg);
1533
1534 ich_lrc.State = requested_ich_lrc.State;
1535 ich_lrc.HW = requested_ich_lrc.HW;
1536 ich_lrc.Group = requested_ich_lrc.Group;
1537
1538 // Priority, bits [23:16]
1539 // At least five bits must be implemented.
1540 // Unimplemented bits are RES0 and start from bit[16] up to bit[18].
1541 // We implement 5 bits.
1542 ich_lrc.Priority = (requested_ich_lrc.Priority & 0xf8) |
1543 (ich_lrc.Priority & 0x07);
1544
1545 // pINTID, bits [12:0]
1546 // When ICH_LR<n>.HW is 0 this field has the following meaning:
1547 // - Bits[12:10] : RES0.
1548 // - Bit[9] : EOI.
1549 // - Bits[8:0] : RES0.
1550 // When ICH_LR<n>.HW is 1:
1551 // - This field is only required to implement enough bits to hold a
1552 // valid value for the implemented INTID size. Any unused higher
1553 // order bits are RES0.
1554 if (requested_ich_lrc.HW == 0) {
1555 ich_lrc.EOI = requested_ich_lrc.EOI;
1556 } else {
1557 ich_lrc.pINTID = requested_ich_lrc.pINTID;
1558 }
1559
1560 val = ich_lrc;
1561 do_virtual_update = true;
1562 break;
1563 }
1564
1565 // List Registers
1566 case MISCREG_ICH_LR0 ... MISCREG_ICH_LR15: {
1567 // AArch32 (maps to AArch64 MISCREG_ICH_LR<n>_EL2 low half part)
1568 RegVal old_val = isa->readMiscRegNoEffect(misc_reg);
1569 val = (old_val & 0xffffffff00000000) | (val & 0xffffffff);
1570 do_virtual_update = true;
1571 break;
1572 }
1573
1574 // List Registers
1575 case MISCREG_ICH_LR0_EL2 ... MISCREG_ICH_LR15_EL2: { // AArch64
1576 ICH_LR_EL2 requested_ich_lr_el2 = val;
1577 ICH_LR_EL2 ich_lr_el2 = isa->readMiscRegNoEffect(misc_reg);
1578
1579 ich_lr_el2.State = requested_ich_lr_el2.State;
1580 ich_lr_el2.HW = requested_ich_lr_el2.HW;
1581 ich_lr_el2.Group = requested_ich_lr_el2.Group;
1582
1583 // Priority, bits [55:48]
1584 // At least five bits must be implemented.
1585 // Unimplemented bits are RES0 and start from bit[48] up to bit[50].
1586 // We implement 5 bits.
1587 ich_lr_el2.Priority = (requested_ich_lr_el2.Priority & 0xf8) |
1588 (ich_lr_el2.Priority & 0x07);
1589
1590 // pINTID, bits [44:32]
1591 // When ICH_LR<n>_EL2.HW is 0 this field has the following meaning:
1592 // - Bits[44:42] : RES0.
1593 // - Bit[41] : EOI.
1594 // - Bits[40:32] : RES0.
1595 // When ICH_LR<n>_EL2.HW is 1:
1596 // - This field is only required to implement enough bits to hold a
1597 // valid value for the implemented INTID size. Any unused higher
1598 // order bits are RES0.
1599 if (requested_ich_lr_el2.HW == 0) {
1600 ich_lr_el2.EOI = requested_ich_lr_el2.EOI;
1601 } else {
1602 ich_lr_el2.pINTID = requested_ich_lr_el2.pINTID;
1603 }
1604
1605 // vINTID, bits [31:0]
1606 // It is IMPLEMENTATION DEFINED how many bits are implemented,
1607 // though at least 16 bits must be implemented.
1608 // Unimplemented bits are RES0.
1609 ich_lr_el2.vINTID = requested_ich_lr_el2.vINTID;
1610
1611 val = ich_lr_el2;
1612 do_virtual_update = true;
1613 break;
1614 }
1615
1616 // Virtual Machine Control Register
1617 case MISCREG_ICH_VMCR:
1618 case MISCREG_ICH_VMCR_EL2: {
1619 ICH_VMCR_EL2 requested_ich_vmcr_el2 = val;
1620 ICH_VMCR_EL2 ich_vmcr_el2 =
1621 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1622 ich_vmcr_el2.VPMR = requested_ich_vmcr_el2.VPMR;
1623 uint8_t min_vpr0 = 7 - VIRTUAL_PREEMPTION_BITS;
1624
1625 if (requested_ich_vmcr_el2.VBPR0 < min_vpr0) {
1626 ich_vmcr_el2.VBPR0 = min_vpr0;
1627 } else {
1628 ich_vmcr_el2.VBPR0 = requested_ich_vmcr_el2.VBPR0;
1629 }
1630
1631 uint8_t min_vpr1 = min_vpr0 + 1;
1632
1633 if (requested_ich_vmcr_el2.VBPR1 < min_vpr1) {
1634 ich_vmcr_el2.VBPR1 = min_vpr1;
1635 } else {
1636 ich_vmcr_el2.VBPR1 = requested_ich_vmcr_el2.VBPR1;
1637 }
1638
1639 ich_vmcr_el2.VEOIM = requested_ich_vmcr_el2.VEOIM;
1640 ich_vmcr_el2.VCBPR = requested_ich_vmcr_el2.VCBPR;
1641 ich_vmcr_el2.VENG1 = requested_ich_vmcr_el2.VENG1;
1642 ich_vmcr_el2.VENG0 = requested_ich_vmcr_el2.VENG0;
1643 val = ich_vmcr_el2;
1644 break;
1645 }
1646
1647 // Hyp Active Priorities Group 0 Registers
1648 case MISCREG_ICH_AP0R0 ... MISCREG_ICH_AP0R3:
1649 case MISCREG_ICH_AP0R0_EL2 ... MISCREG_ICH_AP0R3_EL2:
1650 // Hyp Active Priorities Group 1 Registers
1651 case MISCREG_ICH_AP1R0 ... MISCREG_ICH_AP1R3:
1652 case MISCREG_ICH_AP1R0_EL2 ... MISCREG_ICH_AP1R3_EL2:
1653 break;
1654
1655 default:
1656 panic("Gicv3CPUInterface::setMiscReg(): unknown register %d (%s)",
1657 misc_reg, miscRegName[misc_reg]);
1658 }
1659
1660 isa->setMiscRegNoEffect(misc_reg, val);
1661
1662 if (do_virtual_update) {
1663 virtualUpdate();
1664 }
1665}
1666
1667int
1668Gicv3CPUInterface::virtualFindActive(uint32_t int_id) const
1669{
1670 for (uint32_t lr_idx = 0; lr_idx < VIRTUAL_NUM_LIST_REGS; lr_idx++) {
1671 ICH_LR_EL2 ich_lr_el2 =
1672 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
1673
1674 if (((ich_lr_el2.State == ICH_LR_EL2_STATE_ACTIVE) ||
1675 (ich_lr_el2.State == ICH_LR_EL2_STATE_ACTIVE_PENDING)) &&
1676 (ich_lr_el2.vINTID == int_id)) {
1677 return lr_idx;
1678 }
1679 }
1680
1681 return -1;
1682}
1683
1684uint32_t
1685Gicv3CPUInterface::getHPPIR0() const
1686{
1687 if (hppi.prio == 0xff) {
1688 return Gicv3::INTID_SPURIOUS;
1689 }
1690
1691 bool irq_is_secure = !distributor->DS && hppi.group != Gicv3::G1NS;
1692
1693 if ((hppi.group != Gicv3::G0S) && isEL3OrMon()) {
1694 // interrupt for the other state pending
1695 return irq_is_secure ? Gicv3::INTID_SECURE : Gicv3::INTID_NONSECURE;
1696 }
1697
1698 if ((hppi.group != Gicv3::G0S)) { // && !isEL3OrMon())
1699 return Gicv3::INTID_SPURIOUS;
1700 }
1701
1702 if (irq_is_secure && !inSecureState()) {
1703 // Secure interrupts not visible in Non-secure
1704 return Gicv3::INTID_SPURIOUS;
1705 }
1706
1707 return hppi.intid;
1708}
1709
1710uint32_t
1711Gicv3CPUInterface::getHPPIR1() const
1712{
1713 if (hppi.prio == 0xff) {
1714 return Gicv3::INTID_SPURIOUS;
1715 }
1716
1717 ICC_CTLR_EL3 icc_ctlr_el3 = isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL3);
1718 if ((currEL() == EL3) && icc_ctlr_el3.RM) {
1719 if (hppi.group == Gicv3::G0S) {
1720 return Gicv3::INTID_SECURE;
1721 } else if (hppi.group == Gicv3::G1NS) {
1722 return Gicv3::INTID_NONSECURE;
1723 }
1724 }
1725
1726 if (hppi.group == Gicv3::G0S) {
1727 return Gicv3::INTID_SPURIOUS;
1728 }
1729
1730 bool irq_is_secure = (distributor->DS == 0) && (hppi.group != Gicv3::G1NS);
1731
1732 if (irq_is_secure) {
1733 if (!inSecureState()) {
1734 // Secure interrupts not visible in Non-secure
1735 return Gicv3::INTID_SPURIOUS;
1736 }
1737 } else if (!isEL3OrMon() && inSecureState()) {
1738 // Group 1 non-secure interrupts not visible in Secure EL1
1739 return Gicv3::INTID_SPURIOUS;
1740 }
1741
1742 return hppi.intid;
1743}
1744
1745void
1746Gicv3CPUInterface::dropPriority(Gicv3::GroupId group)
1747{
1748 int apr_misc_reg;
1749 RegVal apr;
1750 apr_misc_reg = group == Gicv3::G0S ?
1751 MISCREG_ICC_AP0R0_EL1 : MISCREG_ICC_AP1R0_EL1;
1752 apr = isa->readMiscRegNoEffect(apr_misc_reg);
1753
1754 if (apr) {
1755 apr &= apr - 1;
1756 isa->setMiscRegNoEffect(apr_misc_reg, apr);
1757 }
1758
1759 update();
1760}
1761
1762uint8_t
1763Gicv3CPUInterface::virtualDropPriority()
1764{
1765 int apr_max = 1 << (VIRTUAL_PREEMPTION_BITS - 5);
1766
1767 for (int i = 0; i < apr_max; i++) {
1768 RegVal vapr0 = isa->readMiscRegNoEffect(MISCREG_ICH_AP0R0_EL2 + i);
1769 RegVal vapr1 = isa->readMiscRegNoEffect(MISCREG_ICH_AP1R0_EL2 + i);
1770
1771 if (!vapr0 && !vapr1) {
1772 continue;
1773 }
1774
1775 int vapr0_count = ctz32(vapr0);
1776 int vapr1_count = ctz32(vapr1);
1777
1778 if (vapr0_count <= vapr1_count) {
1779 vapr0 &= vapr0 - 1;
1780 isa->setMiscRegNoEffect(MISCREG_ICH_AP0R0_EL2 + i, vapr0);
1781 return (vapr0_count + i * 32) << (GIC_MIN_VBPR + 1);
1782 } else {
1783 vapr1 &= vapr1 - 1;
1784 isa->setMiscRegNoEffect(MISCREG_ICH_AP1R0_EL2 + i, vapr1);
1785 return (vapr1_count + i * 32) << (GIC_MIN_VBPR + 1);
1786 }
1787 }
1788
1789 return 0xff;
1790}
1791
1792void
1793Gicv3CPUInterface::activateIRQ(uint32_t int_id, Gicv3::GroupId group)
1794{
1795 // Update active priority registers.
1796 uint32_t prio = hppi.prio & 0xf8;
1797 int apr_bit = prio >> (8 - PRIORITY_BITS);
1798 int reg_bit = apr_bit % 32;
1799 int apr_idx = group == Gicv3::G0S ?
1800 MISCREG_ICC_AP0R0_EL1 : MISCREG_ICC_AP1R0_EL1;
1801 RegVal apr = isa->readMiscRegNoEffect(apr_idx);
1802 apr |= (1 << reg_bit);
1803 isa->setMiscRegNoEffect(apr_idx, apr);
1804
1805 // Move interrupt state from pending to active.
1806 if (int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX) {
1807 // SGI or PPI, redistributor
1808 redistributor->activateIRQ(int_id);
1809 redistributor->updateAndInformCPUInterface();
1810 } else if (int_id < Gicv3::INTID_SECURE) {
1811 // SPI, distributor
1812 distributor->activateIRQ(int_id);
1813 distributor->updateAndInformCPUInterfaces();
1814 } else if (int_id >= Gicv3Redistributor::SMALLEST_LPI_ID) {
1815 // LPI, Redistributor
1816 redistributor->setClrLPI(int_id, false);
1817 }
1818}
1819
1820void
1821Gicv3CPUInterface::virtualActivateIRQ(uint32_t lr_idx)
1822{
1823 // Update active priority registers.
1824 ICH_LR_EL2 ich_lr_el = isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 +
1825 lr_idx);
1826 Gicv3::GroupId group = ich_lr_el.Group ? Gicv3::G1NS : Gicv3::G0S;
1827 uint8_t prio = ich_lr_el.Priority & 0xf8;
1828 int apr_bit = prio >> (8 - VIRTUAL_PREEMPTION_BITS);
1829 int reg_no = apr_bit / 32;
1830 int reg_bit = apr_bit % 32;
1831 int apr_idx = group == Gicv3::G0S ?
1832 MISCREG_ICH_AP0R0_EL2 + reg_no : MISCREG_ICH_AP1R0_EL2 + reg_no;
1833 RegVal apr = isa->readMiscRegNoEffect(apr_idx);
1834 apr |= (1 << reg_bit);
1835 isa->setMiscRegNoEffect(apr_idx, apr);
1836 // Move interrupt state from pending to active.
1837 ich_lr_el.State = ICH_LR_EL2_STATE_ACTIVE;
1838 isa->setMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx, ich_lr_el);
1839}
1840
1841void
1842Gicv3CPUInterface::deactivateIRQ(uint32_t int_id, Gicv3::GroupId group)
1843{
1844 if (int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX) {
1845 // SGI or PPI, redistributor
1846 redistributor->deactivateIRQ(int_id);
1847 redistributor->updateAndInformCPUInterface();
1848 } else if (int_id < Gicv3::INTID_SECURE) {
1849 // SPI, distributor
1850 distributor->deactivateIRQ(int_id);
1851 distributor->updateAndInformCPUInterfaces();
1852 } else {
1853 // LPI, redistributor, shouldn't deactivate
1854 redistributor->updateAndInformCPUInterface();
1855 }
1856}
1857
1858void
1859Gicv3CPUInterface::virtualDeactivateIRQ(int lr_idx)
1860{
1861 ICH_LR_EL2 ich_lr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 +
1862 lr_idx);
1863
1864 if (ich_lr_el2.HW) {
1865 // Deactivate the associated physical interrupt
1866 if (ich_lr_el2.pINTID < Gicv3::INTID_SECURE) {
1867 Gicv3::GroupId group = ich_lr_el2.pINTID >= 32 ?
1868 distributor->getIntGroup(ich_lr_el2.pINTID) :
1869 redistributor->getIntGroup(ich_lr_el2.pINTID);
1870 deactivateIRQ(ich_lr_el2.pINTID, group);
1871 }
1872 }
1873
1874 // Remove the active bit
1875 ich_lr_el2.State = ich_lr_el2.State & ~ICH_LR_EL2_STATE_ACTIVE;
1876 isa->setMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx, ich_lr_el2);
1877}
1878
1879/*
1880 * Returns the priority group field for the current BPR value for the group.
1881 * GroupBits() Pseudocode from spec.
1882 */
1883uint32_t
1884Gicv3CPUInterface::groupPriorityMask(Gicv3::GroupId group)
1885{
1886 ICC_CTLR_EL1 icc_ctlr_el1_s =
1887 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_S);
1888 ICC_CTLR_EL1 icc_ctlr_el1_ns =
1889 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1_NS);
1890
1891 if ((group == Gicv3::G1S && icc_ctlr_el1_s.CBPR) ||
1892 (group == Gicv3::G1NS && icc_ctlr_el1_ns.CBPR)) {
1893 group = Gicv3::G0S;
1894 }
1895
1896 int bpr;
1897
1898 if (group == Gicv3::G0S) {
1899 bpr = readMiscReg(MISCREG_ICC_BPR0_EL1) & 0x7;
1900 } else {
1901 bpr = readMiscReg(MISCREG_ICC_BPR1_EL1) & 0x7;
1902 }
1903
1904 if (group == Gicv3::G1NS) {
1905 assert(bpr > 0);
1906 bpr--;
1907 }
1908
1909 return ~0U << (bpr + 1);
1910}
1911
1912uint32_t
1913Gicv3CPUInterface::virtualGroupPriorityMask(Gicv3::GroupId group) const
1914{
1915 ICH_VMCR_EL2 ich_vmcr_el2 =
1916 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1917
1918 if ((group == Gicv3::G1NS) && ich_vmcr_el2.VCBPR) {
1919 group = Gicv3::G0S;
1920 }
1921
1922 int bpr;
1923
1924 if (group == Gicv3::G0S) {
1925 bpr = ich_vmcr_el2.VBPR0;
1926 } else {
1927 bpr = ich_vmcr_el2.VBPR1;
1928 }
1929
1930 if (group == Gicv3::G1NS) {
1931 assert(bpr > 0);
1932 bpr--;
1933 }
1934
1935 return ~0U << (bpr + 1);
1936}
1937
1938bool
1939Gicv3CPUInterface::isEOISplitMode() const
1940{
1941 if (isEL3OrMon()) {
1942 ICC_CTLR_EL3 icc_ctlr_el3 =
1943 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL3);
1944 return icc_ctlr_el3.EOImode_EL3;
1945 } else {
1946 ICC_CTLR_EL1 icc_ctlr_el1 =
1947 isa->readMiscRegNoEffect(MISCREG_ICC_CTLR_EL1);
1948 return icc_ctlr_el1.EOImode;
1949 }
1950}
1951
1952bool
1953Gicv3CPUInterface::virtualIsEOISplitMode() const
1954{
1955 ICH_VMCR_EL2 ich_vmcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
1956 return ich_vmcr_el2.VEOIM;
1957}
1958
1959int
1960Gicv3CPUInterface::highestActiveGroup() const
1961{
1962 int g0_ctz = ctz32(isa->readMiscRegNoEffect(MISCREG_ICC_AP0R0_EL1));
1963 int gq_ctz = ctz32(isa->readMiscRegNoEffect(MISCREG_ICC_AP1R0_EL1_S));
1964 int g1nz_ctz = ctz32(isa->readMiscRegNoEffect(MISCREG_ICC_AP1R0_EL1_NS));
1965
1966 if (g1nz_ctz < g0_ctz && g1nz_ctz < gq_ctz) {
1967 return Gicv3::G1NS;
1968 }
1969
1970 if (gq_ctz < g0_ctz) {
1971 return Gicv3::G1S;
1972 }
1973
1974 if (g0_ctz < 32) {
1975 return Gicv3::G0S;
1976 }
1977
1978 return -1;
1979}
1980
1981void
1982Gicv3CPUInterface::update()
1983{
1984 bool signal_IRQ = false;
1985 bool signal_FIQ = false;
1986
1987 if (hppi.group == Gicv3::G1S && !haveEL(EL3)) {
1988 /*
1989 * Secure enabled GIC sending a G1S IRQ to a secure disabled
1990 * CPU -> send G0 IRQ
1991 */
1992 hppi.group = Gicv3::G0S;
1993 }
1994
1995 if (hppiCanPreempt()) {
1996 ArmISA::InterruptTypes int_type = intSignalType(hppi.group);
1997 DPRINTF(GIC, "Gicv3CPUInterface::update(): "
1998 "posting int as %d!\n", int_type);
1999 int_type == ArmISA::INT_IRQ ? signal_IRQ = true : signal_FIQ = true;
2000 }
2001
2002 if (signal_IRQ) {
2003 gic->postInt(cpuId, ArmISA::INT_IRQ);
2004 } else {
2005 gic->deassertInt(cpuId, ArmISA::INT_IRQ);
2006 }
2007
2008 if (signal_FIQ) {
2009 gic->postInt(cpuId, ArmISA::INT_FIQ);
2010 } else {
2011 gic->deassertInt(cpuId, ArmISA::INT_FIQ);
2012 }
2013}
2014
2015void
2016Gicv3CPUInterface::virtualUpdate()
2017{
2018 bool signal_IRQ = false;
2019 bool signal_FIQ = false;
2020 int lr_idx = getHPPVILR();
2021
2022 if (lr_idx >= 0) {
2023 ICH_LR_EL2 ich_lr_el2 =
2024 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
2025
2026 if (hppviCanPreempt(lr_idx)) {
2027 if (ich_lr_el2.Group) {
2028 signal_IRQ = true;
2029 } else {
2030 signal_FIQ = true;
2031 }
2032 }
2033 }
2034
2035 ICH_HCR_EL2 ich_hcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
2036
2037 if (ich_hcr_el2.En) {
2038 if (maintenanceInterruptStatus()) {
2039 maintenanceInterrupt->raise();
2040 }
2041 }
2042
2043 if (signal_IRQ) {
2044 DPRINTF(GIC, "Gicv3CPUInterface::virtualUpdate(): "
2045 "posting int as %d!\n", ArmISA::INT_VIRT_IRQ);
2046 gic->postInt(cpuId, ArmISA::INT_VIRT_IRQ);
2047 } else {
2048 gic->deassertInt(cpuId, ArmISA::INT_VIRT_IRQ);
2049 }
2050
2051 if (signal_FIQ) {
2052 DPRINTF(GIC, "Gicv3CPUInterface::virtualUpdate(): "
2053 "posting int as %d!\n", ArmISA::INT_VIRT_FIQ);
2054 gic->postInt(cpuId, ArmISA::INT_VIRT_FIQ);
2055 } else {
2056 gic->deassertInt(cpuId, ArmISA::INT_VIRT_FIQ);
2057 }
2058}
2059
2060// Returns the index of the LR with the HPPI
2061int
2062Gicv3CPUInterface::getHPPVILR() const
2063{
2064 int idx = -1;
2065 ICH_VMCR_EL2 ich_vmcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
2066
2067 if (!ich_vmcr_el2.VENG0 && !ich_vmcr_el2.VENG1) {
2068 // VG0 and VG1 disabled...
2069 return idx;
2070 }
2071
2072 uint8_t highest_prio = 0xff;
2073
2074 for (int i = 0; i < 16; i++) {
2075 ICH_LR_EL2 ich_lr_el2 =
2076 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + i);
2077
2078 if (ich_lr_el2.State != Gicv3::INT_PENDING) {
2079 continue;
2080 }
2081
2082 if (ich_lr_el2.Group) {
2083 // VG1
2084 if (!ich_vmcr_el2.VENG1) {
2085 continue;
2086 }
2087 } else {
2088 // VG0
2089 if (!ich_vmcr_el2.VENG0) {
2090 continue;
2091 }
2092 }
2093
2094 uint8_t prio = ich_lr_el2.Priority;
2095
2096 if (prio < highest_prio) {
2097 highest_prio = prio;
2098 idx = i;
2099 }
2100 }
2101
2102 return idx;
2103}
2104
2105bool
2106Gicv3CPUInterface::hppviCanPreempt(int lr_idx) const
2107{
2108 ICH_HCR_EL2 ich_hcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
2109 if (!ich_hcr_el2.En) {
2110 // virtual interface is disabled
2111 return false;
2112 }
2113
2114 ICH_LR_EL2 ich_lr_el2 =
2115 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
2116 uint8_t prio = ich_lr_el2.Priority;
2117 uint8_t vpmr =
2118 bits(isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2), 31, 24);
2119
2120 if (prio >= vpmr) {
2121 // prioriry masked
2122 return false;
2123 }
2124
2125 uint8_t rprio = virtualHighestActivePriority();
2126
2127 if (rprio == 0xff) {
2128 return true;
2129 }
2130
2131 Gicv3::GroupId group = ich_lr_el2.Group ? Gicv3::G1NS : Gicv3::G0S;
2132 uint32_t prio_mask = virtualGroupPriorityMask(group);
2133
2134 if ((prio & prio_mask) < (rprio & prio_mask)) {
2135 return true;
2136 }
2137
2138 return false;
2139}
2140
2141uint8_t
2142Gicv3CPUInterface::virtualHighestActivePriority() const
2143{
2144 uint8_t num_aprs = 1 << (VIRTUAL_PRIORITY_BITS - 5);
2145
2146 for (int i = 0; i < num_aprs; i++) {
2147 RegVal vapr =
2148 isa->readMiscRegNoEffect(MISCREG_ICH_AP0R0_EL2 + i) |
2149 isa->readMiscRegNoEffect(MISCREG_ICH_AP1R0_EL2 + i);
2150
2151 if (!vapr) {
2152 continue;
2153 }
2154
2155 return (i * 32 + ctz32(vapr)) << (GIC_MIN_VBPR + 1);
2156 }
2157
2158 // no active interrups, return idle priority
2159 return 0xff;
2160}
2161
2162void
2163Gicv3CPUInterface::virtualIncrementEOICount()
2164{
2165 // Increment the EOICOUNT field in ICH_HCR_EL2
2166 RegVal ich_hcr_el2 = isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
2167 uint32_t EOI_cout = bits(ich_hcr_el2, 31, 27);
2168 EOI_cout++;
2169 ich_hcr_el2 = insertBits(ich_hcr_el2, 31, 27, EOI_cout);
2170 isa->setMiscRegNoEffect(MISCREG_ICH_HCR_EL2, ich_hcr_el2);
2171}
2172
2173// spec section 4.6.2
2174ArmISA::InterruptTypes
2175Gicv3CPUInterface::intSignalType(Gicv3::GroupId group) const
2176{
2177 bool is_fiq = false;
2178
2179 switch (group) {
2180 case Gicv3::G0S:
2181 is_fiq = true;
2182 break;
2183
2184 case Gicv3::G1S:
2185 is_fiq = (distributor->DS == 0) &&
2186 (!inSecureState() || ((currEL() == EL3) && isAA64()));
2187 break;
2188
2189 case Gicv3::G1NS:
2190 is_fiq = (distributor->DS == 0) && inSecureState();
2191 break;
2192
2193 default:
2194 panic("Gicv3CPUInterface::intSignalType(): invalid group!");
2195 }
2196
2197 if (is_fiq) {
2198 return ArmISA::INT_FIQ;
2199 } else {
2200 return ArmISA::INT_IRQ;
2201 }
2202}
2203
2204bool
2205Gicv3CPUInterface::hppiCanPreempt()
2206{
2207 if (hppi.prio == 0xff) {
2208 // there is no pending interrupt
2209 return false;
2210 }
2211
2212 if (!groupEnabled(hppi.group)) {
2213 // group disabled at CPU interface
2214 return false;
2215 }
2216
2217 if (hppi.prio >= isa->readMiscRegNoEffect(MISCREG_ICC_PMR_EL1)) {
2218 // priority masked
2219 return false;
2220 }
2221
2222 uint8_t rprio = highestActivePriority();
2223
2224 if (rprio == 0xff) {
2225 return true;
2226 }
2227
2228 uint32_t prio_mask = groupPriorityMask(hppi.group);
2229
2230 if ((hppi.prio & prio_mask) < (rprio & prio_mask)) {
2231 return true;
2232 }
2233
2234 return false;
2235}
2236
2237uint8_t
2238Gicv3CPUInterface::highestActivePriority() const
2239{
2240 uint32_t apr = isa->readMiscRegNoEffect(MISCREG_ICC_AP0R0_EL1) |
2241 isa->readMiscRegNoEffect(MISCREG_ICC_AP1R0_EL1_NS) |
2242 isa->readMiscRegNoEffect(MISCREG_ICC_AP1R0_EL1_S);
2243
2244 if (apr) {
2245 return ctz32(apr) << (GIC_MIN_BPR + 1);
2246 }
2247
2248 // no active interrups, return idle priority
2249 return 0xff;
2250}
2251
2252bool
2253Gicv3CPUInterface::groupEnabled(Gicv3::GroupId group) const
2254{
2255 switch (group) {
2256 case Gicv3::G0S: {
2257 ICC_IGRPEN0_EL1 icc_igrpen0_el1 =
2258 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN0_EL1);
2259 return icc_igrpen0_el1.Enable;
2260 }
2261
2262 case Gicv3::G1S: {
2263 ICC_IGRPEN1_EL1 icc_igrpen1_el1_s =
2264 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL1_S);
2265 return icc_igrpen1_el1_s.Enable;
2266 }
2267
2268 case Gicv3::G1NS: {
2269 ICC_IGRPEN1_EL1 icc_igrpen1_el1_ns =
2270 isa->readMiscRegNoEffect(MISCREG_ICC_IGRPEN1_EL1_NS);
2271 return icc_igrpen1_el1_ns.Enable;
2272 }
2273
2274 default:
2275 panic("Gicv3CPUInterface::groupEnable(): invalid group!\n");
2276 }
2277}
2278
2279bool
2280Gicv3CPUInterface::inSecureState() const
2281{
2282 if (!gic->getSystem()->haveSecurity()) {
2283 return false;
2284 }
2285
2286 CPSR cpsr = isa->readMiscRegNoEffect(MISCREG_CPSR);
2287 SCR scr = isa->readMiscRegNoEffect(MISCREG_SCR);
2288 return ArmISA::inSecureState(scr, cpsr);
2289}
2290
2291int
2292Gicv3CPUInterface::currEL() const
2293{
2294 CPSR cpsr = isa->readMiscRegNoEffect(MISCREG_CPSR);
2295 bool is_64 = opModeIs64((OperatingMode)(uint8_t) cpsr.mode);
2296
2297 if (is_64) {
2298 return (ExceptionLevel)(uint8_t) cpsr.el;
2299 } else {
2300 switch (cpsr.mode) {
2301 case MODE_USER:
2302 return 0;
2303
2304 case MODE_HYP:
2305 return 2;
2306
2307 case MODE_MON:
2308 return 3;
2309
2310 default:
2311 return 1;
2312 }
2313 }
2314}
2315
2316bool
2317Gicv3CPUInterface::haveEL(ExceptionLevel el) const
2318{
2319 switch (el) {
2320 case EL0:
2321 case EL1:
2322 return true;
2323
2324 case EL2:
2325 return gic->getSystem()->haveVirtualization();
2326
2327 case EL3:
2328 return gic->getSystem()->haveSecurity();
2329
2330 default:
2331 warn("Unimplemented Exception Level\n");
2332 return false;
2333 }
2334}
2335
2336bool
2337Gicv3CPUInterface::isSecureBelowEL3() const
2338{
2339 SCR scr = isa->readMiscRegNoEffect(MISCREG_SCR_EL3);
2340 return haveEL(EL3) && scr.ns == 0;
2341}
2342
2343bool
2344Gicv3CPUInterface::isAA64() const
2345{
2346 CPSR cpsr = isa->readMiscRegNoEffect(MISCREG_CPSR);
2347 return opModeIs64((OperatingMode)(uint8_t) cpsr.mode);
2348}
2349
2350bool
2351Gicv3CPUInterface::isEL3OrMon() const
2352{
2353 if (haveEL(EL3)) {
2354 CPSR cpsr = isa->readMiscRegNoEffect(MISCREG_CPSR);
2355 bool is_64 = opModeIs64((OperatingMode)(uint8_t) cpsr.mode);
2356
2357 if (is_64 && (cpsr.el == EL3)) {
2358 return true;
2359 } else if (!is_64 && (cpsr.mode == MODE_MON)) {
2360 return true;
2361 }
2362 }
2363
2364 return false;
2365}
2366
2367// Computes ICH_EISR_EL2
2368uint64_t
2369Gicv3CPUInterface::eoiMaintenanceInterruptStatus() const
2370{
2371 // ICH_EISR_EL2
2372 // Bits [63:16] - RES0
2373 // Status<n>, bit [n], for n = 0 to 15
2374 // EOI maintenance interrupt status bit for List register <n>:
2375 // 0 if List register <n>, ICH_LR<n>_EL2, does not have an EOI
2376 // maintenance interrupt.
2377 // 1 if List register <n>, ICH_LR<n>_EL2, has an EOI maintenance
2378 // interrupt that has not been handled.
2379 //
2380 // For any ICH_LR<n>_EL2, the corresponding status bit is set to 1 if all
2381 // of the following are true:
2382 // - ICH_LR<n>_EL2.State is 0b00 (ICH_LR_EL2_STATE_INVALID).
2383 // - ICH_LR<n>_EL2.HW is 0.
2384 // - ICH_LR<n>_EL2.EOI (bit [41]) is 1.
2385
2386 uint64_t value = 0;
2387
2388 for (int lr_idx = 0; lr_idx < VIRTUAL_NUM_LIST_REGS; lr_idx++) {
2389 ICH_LR_EL2 ich_lr_el2 =
2390 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
2391
2392 if ((ich_lr_el2.State == ICH_LR_EL2_STATE_INVALID) &&
2393 !ich_lr_el2.HW && ich_lr_el2.EOI) {
2394 value |= (1 << lr_idx);
2395 }
2396 }
2397
2398 return value;
2399}
2400
2401Gicv3CPUInterface::ICH_MISR_EL2
2402Gicv3CPUInterface::maintenanceInterruptStatus() const
2403{
2404 // Comments are copied from SPEC section 9.4.7 (ID012119)
2405 ICH_MISR_EL2 ich_misr_el2 = 0;
2406 ICH_HCR_EL2 ich_hcr_el2 =
2407 isa->readMiscRegNoEffect(MISCREG_ICH_HCR_EL2);
2408 ICH_VMCR_EL2 ich_vmcr_el2 =
2409 isa->readMiscRegNoEffect(MISCREG_ICH_VMCR_EL2);
2410
2411 // End Of Interrupt. [bit 0]
2412 // This maintenance interrupt is asserted when at least one bit in
2413 // ICH_EISR_EL2 is 1.
2414
2415 if (eoiMaintenanceInterruptStatus()) {
2416 ich_misr_el2.EOI = 1;
2417 }
2418
2419 // Underflow. [bit 1]
2420 // This maintenance interrupt is asserted when ICH_HCR_EL2.UIE==1 and
2421 // zero or one of the List register entries are marked as a valid
2422 // interrupt, that is, if the corresponding ICH_LR<n>_EL2.State bits
2423 // do not equal 0x0.
2424 uint32_t num_valid_interrupts = 0;
2425 uint32_t num_pending_interrupts = 0;
2426
2427 for (int lr_idx = 0; lr_idx < VIRTUAL_NUM_LIST_REGS; lr_idx++) {
2428 ICH_LR_EL2 ich_lr_el2 =
2429 isa->readMiscRegNoEffect(MISCREG_ICH_LR0_EL2 + lr_idx);
2430
2431 if (ich_lr_el2.State != ICH_LR_EL2_STATE_INVALID) {
2432 num_valid_interrupts++;
2433 }
2434
2435 if (ich_lr_el2.State == ICH_LR_EL2_STATE_PENDING) {
2436 num_pending_interrupts++;
2437 }
2438 }
2439
2440 if (ich_hcr_el2.UIE && (num_valid_interrupts < 2)) {
2441 ich_misr_el2.U = 1;
2442 }
2443
2444 // List Register Entry Not Present. [bit 2]
2445 // This maintenance interrupt is asserted when ICH_HCR_EL2.LRENPIE==1
2446 // and ICH_HCR_EL2.EOIcount is non-zero.
2447 if (ich_hcr_el2.LRENPIE && ich_hcr_el2.EOIcount) {
2448 ich_misr_el2.LRENP = 1;
2449 }
2450
2451 // No Pending. [bit 3]
2452 // This maintenance interrupt is asserted when ICH_HCR_EL2.NPIE==1 and
2453 // no List register is in pending state.
2454 if (ich_hcr_el2.NPIE && (num_pending_interrupts == 0)) {
2455 ich_misr_el2.NP = 1;
2456 }
2457
2458 // vPE Group 0 Enabled. [bit 4]
2459 // This maintenance interrupt is asserted when
2460 // ICH_HCR_EL2.VGrp0EIE==1 and ICH_VMCR_EL2.VENG0==1.
2461 if (ich_hcr_el2.VGrp0EIE && ich_vmcr_el2.VENG0) {
2462 ich_misr_el2.VGrp0E = 1;
2463 }
2464
2465 // vPE Group 0 Disabled. [bit 5]
2466 // This maintenance interrupt is asserted when
2467 // ICH_HCR_EL2.VGrp0DIE==1 and ICH_VMCR_EL2.VENG0==0.
2468 if (ich_hcr_el2.VGrp0DIE && !ich_vmcr_el2.VENG0) {
2469 ich_misr_el2.VGrp0D = 1;
2470 }
2471
2472 // vPE Group 1 Enabled. [bit 6]
2473 // This maintenance interrupt is asserted when
2474 // ICH_HCR_EL2.VGrp1EIE==1 and ICH_VMCR_EL2.VENG1==is 1.
2475 if (ich_hcr_el2.VGrp1EIE && ich_vmcr_el2.VENG1) {
2476 ich_misr_el2.VGrp1E = 1;
2477 }
2478
2479 // vPE Group 1 Disabled. [bit 7]
2480 // This maintenance interrupt is asserted when
2481 // ICH_HCR_EL2.VGrp1DIE==1 and ICH_VMCR_EL2.VENG1==is 0.
2482 if (ich_hcr_el2.VGrp1DIE && !ich_vmcr_el2.VENG1) {
2483 ich_misr_el2.VGrp1D = 1;
2484 }
2485
2486 return ich_misr_el2;
2487}
2488
2489void
2490Gicv3CPUInterface::serialize(CheckpointOut & cp) const
2491{
2492 SERIALIZE_SCALAR(hppi.intid);
2493 SERIALIZE_SCALAR(hppi.prio);
2494 SERIALIZE_ENUM(hppi.group);
2495}
2496
2497void
2498Gicv3CPUInterface::unserialize(CheckpointIn & cp)
2499{
2500 UNSERIALIZE_SCALAR(hppi.intid);
2501 UNSERIALIZE_SCALAR(hppi.prio);
2502 UNSERIALIZE_ENUM(hppi.group);
2503}