1e52af513SShlomo Pongratz /*
2e52af513SShlomo Pongratz * ARM GICv3 emulation: Distributor
3e52af513SShlomo Pongratz *
4e52af513SShlomo Pongratz * Copyright (c) 2015 Huawei.
5e52af513SShlomo Pongratz * Copyright (c) 2016 Linaro Limited.
6e52af513SShlomo Pongratz * Written by Shlomo Pongratz, Peter Maydell
7e52af513SShlomo Pongratz *
8e52af513SShlomo Pongratz * This code is licensed under the GPL, version 2 or (at your option)
9e52af513SShlomo Pongratz * any later version.
10e52af513SShlomo Pongratz */
11e52af513SShlomo Pongratz
12e52af513SShlomo Pongratz #include "qemu/osdep.h"
13b1e3493bSPeter Maydell #include "qemu/log.h"
14e52af513SShlomo Pongratz #include "trace.h"
15e52af513SShlomo Pongratz #include "gicv3_internal.h"
16e52af513SShlomo Pongratz
17e52af513SShlomo Pongratz /* The GICD_NSACR registers contain a two bit field for each interrupt which
18e52af513SShlomo Pongratz * allows the guest to give NonSecure code access to registers controlling
19e52af513SShlomo Pongratz * Secure interrupts:
20e52af513SShlomo Pongratz * 0b00: no access (NS accesses to bits for Secure interrupts will RAZ/WI)
21e52af513SShlomo Pongratz * 0b01: NS r/w accesses permitted to ISPENDR, SETSPI_NSR, SGIR
22e52af513SShlomo Pongratz * 0b10: as 0b01, and also r/w to ICPENDR, r/o to ISACTIVER/ICACTIVER,
23e52af513SShlomo Pongratz * and w/o to CLRSPI_NSR
24e52af513SShlomo Pongratz * 0b11: as 0b10, and also r/w to IROUTER and ITARGETSR
25e52af513SShlomo Pongratz *
26e52af513SShlomo Pongratz * Given a (multiple-of-32) interrupt number, these mask functions return
27e52af513SShlomo Pongratz * a mask word where each bit is 1 if the NSACR settings permit access
28e52af513SShlomo Pongratz * to the interrupt. The mask returned can then be ORed with the GICD_GROUP
29e52af513SShlomo Pongratz * word for this set of interrupts to give an overall mask.
30e52af513SShlomo Pongratz */
31e52af513SShlomo Pongratz
32e52af513SShlomo Pongratz typedef uint32_t maskfn(GICv3State *s, int irq);
33e52af513SShlomo Pongratz
mask_nsacr_ge1(GICv3State * s,int irq)34e52af513SShlomo Pongratz static uint32_t mask_nsacr_ge1(GICv3State *s, int irq)
35e52af513SShlomo Pongratz {
36e52af513SShlomo Pongratz /* Return a mask where each bit is set if the NSACR field is >= 1 */
37e52af513SShlomo Pongratz uint64_t raw_nsacr = s->gicd_nsacr[irq / 16 + 1];
38e52af513SShlomo Pongratz
39e52af513SShlomo Pongratz raw_nsacr = raw_nsacr << 32 | s->gicd_nsacr[irq / 16];
40e52af513SShlomo Pongratz raw_nsacr = (raw_nsacr >> 1) | raw_nsacr;
41e52af513SShlomo Pongratz return half_unshuffle64(raw_nsacr);
42e52af513SShlomo Pongratz }
43e52af513SShlomo Pongratz
mask_nsacr_ge2(GICv3State * s,int irq)44e52af513SShlomo Pongratz static uint32_t mask_nsacr_ge2(GICv3State *s, int irq)
45e52af513SShlomo Pongratz {
46e52af513SShlomo Pongratz /* Return a mask where each bit is set if the NSACR field is >= 2 */
47e52af513SShlomo Pongratz uint64_t raw_nsacr = s->gicd_nsacr[irq / 16 + 1];
48e52af513SShlomo Pongratz
49e52af513SShlomo Pongratz raw_nsacr = raw_nsacr << 32 | s->gicd_nsacr[irq / 16];
50e52af513SShlomo Pongratz raw_nsacr = raw_nsacr >> 1;
51e52af513SShlomo Pongratz return half_unshuffle64(raw_nsacr);
52e52af513SShlomo Pongratz }
53e52af513SShlomo Pongratz
54e52af513SShlomo Pongratz /* We don't need a mask_nsacr_ge3() because IROUTER<n> isn't a bitmap register,
55e52af513SShlomo Pongratz * but it would be implemented using:
56e52af513SShlomo Pongratz * raw_nsacr = (raw_nsacr >> 1) & raw_nsacr;
57e52af513SShlomo Pongratz */
58e52af513SShlomo Pongratz
mask_group_and_nsacr(GICv3State * s,MemTxAttrs attrs,maskfn * maskfn,int irq)59e52af513SShlomo Pongratz static uint32_t mask_group_and_nsacr(GICv3State *s, MemTxAttrs attrs,
60e52af513SShlomo Pongratz maskfn *maskfn, int irq)
61e52af513SShlomo Pongratz {
62e52af513SShlomo Pongratz /* Return a 32-bit mask which should be applied for this set of 32
63e52af513SShlomo Pongratz * interrupts; each bit is 1 if access is permitted by the
64e52af513SShlomo Pongratz * combination of attrs.secure, GICD_GROUPR and GICD_NSACR.
65e52af513SShlomo Pongratz */
66e52af513SShlomo Pongratz uint32_t mask;
67e52af513SShlomo Pongratz
68e52af513SShlomo Pongratz if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
69e52af513SShlomo Pongratz /* bits for Group 0 or Secure Group 1 interrupts are RAZ/WI
70e52af513SShlomo Pongratz * unless the NSACR bits permit access.
71e52af513SShlomo Pongratz */
72e52af513SShlomo Pongratz mask = *gic_bmp_ptr32(s->group, irq);
73e52af513SShlomo Pongratz if (maskfn) {
74e52af513SShlomo Pongratz mask |= maskfn(s, irq);
75e52af513SShlomo Pongratz }
76e52af513SShlomo Pongratz return mask;
77e52af513SShlomo Pongratz }
78e52af513SShlomo Pongratz return 0xFFFFFFFFU;
79e52af513SShlomo Pongratz }
80e52af513SShlomo Pongratz
gicd_ns_access(GICv3State * s,int irq)81e52af513SShlomo Pongratz static int gicd_ns_access(GICv3State *s, int irq)
82e52af513SShlomo Pongratz {
83e52af513SShlomo Pongratz /* Return the 2 bit NS_access<x> field from GICD_NSACR<n> for the
84e52af513SShlomo Pongratz * specified interrupt.
85e52af513SShlomo Pongratz */
86e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
87e52af513SShlomo Pongratz return 0;
88e52af513SShlomo Pongratz }
89e52af513SShlomo Pongratz return extract32(s->gicd_nsacr[irq / 16], (irq % 16) * 2, 2);
90e52af513SShlomo Pongratz }
91e52af513SShlomo Pongratz
gicd_write_bitmap_reg(GICv3State * s,MemTxAttrs attrs,uint32_t * bmp,maskfn * maskfn,int offset,uint32_t val)92*44ed1e4bSJinjie Ruan static void gicd_write_bitmap_reg(GICv3State *s, MemTxAttrs attrs,
93*44ed1e4bSJinjie Ruan uint32_t *bmp, maskfn *maskfn,
94*44ed1e4bSJinjie Ruan int offset, uint32_t val)
95*44ed1e4bSJinjie Ruan {
96*44ed1e4bSJinjie Ruan /*
97*44ed1e4bSJinjie Ruan * Helper routine to implement writing to a "set" register
98*44ed1e4bSJinjie Ruan * (GICD_INMIR, etc).
99*44ed1e4bSJinjie Ruan * Semantics implemented here:
100*44ed1e4bSJinjie Ruan * RAZ/WI for SGIs, PPIs, unimplemented IRQs
101*44ed1e4bSJinjie Ruan * Bits corresponding to Group 0 or Secure Group 1 interrupts RAZ/WI.
102*44ed1e4bSJinjie Ruan * offset should be the offset in bytes of the register from the start
103*44ed1e4bSJinjie Ruan * of its group.
104*44ed1e4bSJinjie Ruan */
105*44ed1e4bSJinjie Ruan int irq = offset * 8;
106*44ed1e4bSJinjie Ruan
107*44ed1e4bSJinjie Ruan if (irq < GIC_INTERNAL || irq >= s->num_irq) {
108*44ed1e4bSJinjie Ruan return;
109*44ed1e4bSJinjie Ruan }
110*44ed1e4bSJinjie Ruan val &= mask_group_and_nsacr(s, attrs, maskfn, irq);
111*44ed1e4bSJinjie Ruan *gic_bmp_ptr32(bmp, irq) = val;
112*44ed1e4bSJinjie Ruan gicv3_update(s, irq, 32);
113*44ed1e4bSJinjie Ruan }
114*44ed1e4bSJinjie Ruan
gicd_write_set_bitmap_reg(GICv3State * s,MemTxAttrs attrs,uint32_t * bmp,maskfn * maskfn,int offset,uint32_t val)115e52af513SShlomo Pongratz static void gicd_write_set_bitmap_reg(GICv3State *s, MemTxAttrs attrs,
116e52af513SShlomo Pongratz uint32_t *bmp,
117e52af513SShlomo Pongratz maskfn *maskfn,
118e52af513SShlomo Pongratz int offset, uint32_t val)
119e52af513SShlomo Pongratz {
120e52af513SShlomo Pongratz /* Helper routine to implement writing to a "set-bitmap" register
121e52af513SShlomo Pongratz * (GICD_ISENABLER, GICD_ISPENDR, etc).
122e52af513SShlomo Pongratz * Semantics implemented here:
123e52af513SShlomo Pongratz * RAZ/WI for SGIs, PPIs, unimplemented IRQs
124e52af513SShlomo Pongratz * Bits corresponding to Group 0 or Secure Group 1 interrupts RAZ/WI.
125e52af513SShlomo Pongratz * Writing 1 means "set bit in bitmap"; writing 0 is ignored.
126e52af513SShlomo Pongratz * offset should be the offset in bytes of the register from the start
127e52af513SShlomo Pongratz * of its group.
128e52af513SShlomo Pongratz */
129e52af513SShlomo Pongratz int irq = offset * 8;
130e52af513SShlomo Pongratz
131e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
132e52af513SShlomo Pongratz return;
133e52af513SShlomo Pongratz }
134e52af513SShlomo Pongratz val &= mask_group_and_nsacr(s, attrs, maskfn, irq);
135e52af513SShlomo Pongratz *gic_bmp_ptr32(bmp, irq) |= val;
136e52af513SShlomo Pongratz gicv3_update(s, irq, 32);
137e52af513SShlomo Pongratz }
138e52af513SShlomo Pongratz
gicd_write_clear_bitmap_reg(GICv3State * s,MemTxAttrs attrs,uint32_t * bmp,maskfn * maskfn,int offset,uint32_t val)139e52af513SShlomo Pongratz static void gicd_write_clear_bitmap_reg(GICv3State *s, MemTxAttrs attrs,
140e52af513SShlomo Pongratz uint32_t *bmp,
141e52af513SShlomo Pongratz maskfn *maskfn,
142e52af513SShlomo Pongratz int offset, uint32_t val)
143e52af513SShlomo Pongratz {
144e52af513SShlomo Pongratz /* Helper routine to implement writing to a "clear-bitmap" register
145e52af513SShlomo Pongratz * (GICD_ICENABLER, GICD_ICPENDR, etc).
146e52af513SShlomo Pongratz * Semantics implemented here:
147e52af513SShlomo Pongratz * RAZ/WI for SGIs, PPIs, unimplemented IRQs
148e52af513SShlomo Pongratz * Bits corresponding to Group 0 or Secure Group 1 interrupts RAZ/WI.
149e52af513SShlomo Pongratz * Writing 1 means "clear bit in bitmap"; writing 0 is ignored.
150e52af513SShlomo Pongratz * offset should be the offset in bytes of the register from the start
151e52af513SShlomo Pongratz * of its group.
152e52af513SShlomo Pongratz */
153e52af513SShlomo Pongratz int irq = offset * 8;
154e52af513SShlomo Pongratz
155e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
156e52af513SShlomo Pongratz return;
157e52af513SShlomo Pongratz }
158e52af513SShlomo Pongratz val &= mask_group_and_nsacr(s, attrs, maskfn, irq);
159e52af513SShlomo Pongratz *gic_bmp_ptr32(bmp, irq) &= ~val;
160e52af513SShlomo Pongratz gicv3_update(s, irq, 32);
161e52af513SShlomo Pongratz }
162e52af513SShlomo Pongratz
gicd_read_bitmap_reg(GICv3State * s,MemTxAttrs attrs,uint32_t * bmp,maskfn * maskfn,int offset)163e52af513SShlomo Pongratz static uint32_t gicd_read_bitmap_reg(GICv3State *s, MemTxAttrs attrs,
164e52af513SShlomo Pongratz uint32_t *bmp,
165e52af513SShlomo Pongratz maskfn *maskfn,
166e52af513SShlomo Pongratz int offset)
167e52af513SShlomo Pongratz {
168e52af513SShlomo Pongratz /* Helper routine to implement reading a "set/clear-bitmap" register
169e52af513SShlomo Pongratz * (GICD_ICENABLER, GICD_ISENABLER, GICD_ICPENDR, etc).
170e52af513SShlomo Pongratz * Semantics implemented here:
171e52af513SShlomo Pongratz * RAZ/WI for SGIs, PPIs, unimplemented IRQs
172e52af513SShlomo Pongratz * Bits corresponding to Group 0 or Secure Group 1 interrupts RAZ/WI.
173e52af513SShlomo Pongratz * offset should be the offset in bytes of the register from the start
174e52af513SShlomo Pongratz * of its group.
175e52af513SShlomo Pongratz */
176e52af513SShlomo Pongratz int irq = offset * 8;
177e52af513SShlomo Pongratz uint32_t val;
178e52af513SShlomo Pongratz
179e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
180e52af513SShlomo Pongratz return 0;
181e52af513SShlomo Pongratz }
182e52af513SShlomo Pongratz val = *gic_bmp_ptr32(bmp, irq);
183e52af513SShlomo Pongratz if (bmp == s->pending) {
184e52af513SShlomo Pongratz /* The PENDING register is a special case -- for level triggered
185e52af513SShlomo Pongratz * interrupts, the PENDING state is the logical OR of the state of
186e52af513SShlomo Pongratz * the PENDING latch with the input line level.
187e52af513SShlomo Pongratz */
188e52af513SShlomo Pongratz uint32_t edge = *gic_bmp_ptr32(s->edge_trigger, irq);
189e52af513SShlomo Pongratz uint32_t level = *gic_bmp_ptr32(s->level, irq);
190e52af513SShlomo Pongratz val |= (~edge & level);
191e52af513SShlomo Pongratz }
192e52af513SShlomo Pongratz val &= mask_group_and_nsacr(s, attrs, maskfn, irq);
193e52af513SShlomo Pongratz return val;
194e52af513SShlomo Pongratz }
195e52af513SShlomo Pongratz
gicd_read_ipriorityr(GICv3State * s,MemTxAttrs attrs,int irq)196e52af513SShlomo Pongratz static uint8_t gicd_read_ipriorityr(GICv3State *s, MemTxAttrs attrs, int irq)
197e52af513SShlomo Pongratz {
198e52af513SShlomo Pongratz /* Read the value of GICD_IPRIORITYR<n> for the specified interrupt,
199e52af513SShlomo Pongratz * honouring security state (these are RAZ/WI for Group 0 or Secure
200e52af513SShlomo Pongratz * Group 1 interrupts).
201e52af513SShlomo Pongratz */
202e52af513SShlomo Pongratz uint32_t prio;
203e52af513SShlomo Pongratz
204e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
205e52af513SShlomo Pongratz return 0;
206e52af513SShlomo Pongratz }
207e52af513SShlomo Pongratz
208e52af513SShlomo Pongratz prio = s->gicd_ipriority[irq];
209e52af513SShlomo Pongratz
210e52af513SShlomo Pongratz if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
211e52af513SShlomo Pongratz if (!gicv3_gicd_group_test(s, irq)) {
212e52af513SShlomo Pongratz /* Fields for Group 0 or Secure Group 1 interrupts are RAZ/WI */
213e52af513SShlomo Pongratz return 0;
214e52af513SShlomo Pongratz }
215e52af513SShlomo Pongratz /* NS view of the interrupt priority */
216e52af513SShlomo Pongratz prio = (prio << 1) & 0xff;
217e52af513SShlomo Pongratz }
218e52af513SShlomo Pongratz return prio;
219e52af513SShlomo Pongratz }
220e52af513SShlomo Pongratz
gicd_write_ipriorityr(GICv3State * s,MemTxAttrs attrs,int irq,uint8_t value)221e52af513SShlomo Pongratz static void gicd_write_ipriorityr(GICv3State *s, MemTxAttrs attrs, int irq,
222e52af513SShlomo Pongratz uint8_t value)
223e52af513SShlomo Pongratz {
224e52af513SShlomo Pongratz /* Write the value of GICD_IPRIORITYR<n> for the specified interrupt,
225e52af513SShlomo Pongratz * honouring security state (these are RAZ/WI for Group 0 or Secure
226e52af513SShlomo Pongratz * Group 1 interrupts).
227e52af513SShlomo Pongratz */
228e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
229e52af513SShlomo Pongratz return;
230e52af513SShlomo Pongratz }
231e52af513SShlomo Pongratz
232e52af513SShlomo Pongratz if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
233e52af513SShlomo Pongratz if (!gicv3_gicd_group_test(s, irq)) {
234e52af513SShlomo Pongratz /* Fields for Group 0 or Secure Group 1 interrupts are RAZ/WI */
235e52af513SShlomo Pongratz return;
236e52af513SShlomo Pongratz }
237e52af513SShlomo Pongratz /* NS view of the interrupt priority */
238e52af513SShlomo Pongratz value = 0x80 | (value >> 1);
239e52af513SShlomo Pongratz }
240e52af513SShlomo Pongratz s->gicd_ipriority[irq] = value;
241e52af513SShlomo Pongratz }
242e52af513SShlomo Pongratz
gicd_read_irouter(GICv3State * s,MemTxAttrs attrs,int irq)243e52af513SShlomo Pongratz static uint64_t gicd_read_irouter(GICv3State *s, MemTxAttrs attrs, int irq)
244e52af513SShlomo Pongratz {
245e52af513SShlomo Pongratz /* Read the value of GICD_IROUTER<n> for the specified interrupt,
246e52af513SShlomo Pongratz * honouring security state.
247e52af513SShlomo Pongratz */
248e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
249e52af513SShlomo Pongratz return 0;
250e52af513SShlomo Pongratz }
251e52af513SShlomo Pongratz
252e52af513SShlomo Pongratz if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
253e52af513SShlomo Pongratz /* RAZ/WI for NS accesses to secure interrupts */
254e52af513SShlomo Pongratz if (!gicv3_gicd_group_test(s, irq)) {
255e52af513SShlomo Pongratz if (gicd_ns_access(s, irq) != 3) {
256e52af513SShlomo Pongratz return 0;
257e52af513SShlomo Pongratz }
258e52af513SShlomo Pongratz }
259e52af513SShlomo Pongratz }
260e52af513SShlomo Pongratz
261e52af513SShlomo Pongratz return s->gicd_irouter[irq];
262e52af513SShlomo Pongratz }
263e52af513SShlomo Pongratz
gicd_write_irouter(GICv3State * s,MemTxAttrs attrs,int irq,uint64_t val)264e52af513SShlomo Pongratz static void gicd_write_irouter(GICv3State *s, MemTxAttrs attrs, int irq,
265e52af513SShlomo Pongratz uint64_t val)
266e52af513SShlomo Pongratz {
267e52af513SShlomo Pongratz /* Write the value of GICD_IROUTER<n> for the specified interrupt,
268e52af513SShlomo Pongratz * honouring security state.
269e52af513SShlomo Pongratz */
270e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
271e52af513SShlomo Pongratz return;
272e52af513SShlomo Pongratz }
273e52af513SShlomo Pongratz
274e52af513SShlomo Pongratz if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
275e52af513SShlomo Pongratz /* RAZ/WI for NS accesses to secure interrupts */
276e52af513SShlomo Pongratz if (!gicv3_gicd_group_test(s, irq)) {
277e52af513SShlomo Pongratz if (gicd_ns_access(s, irq) != 3) {
278e52af513SShlomo Pongratz return;
279e52af513SShlomo Pongratz }
280e52af513SShlomo Pongratz }
281e52af513SShlomo Pongratz }
282e52af513SShlomo Pongratz
283e52af513SShlomo Pongratz s->gicd_irouter[irq] = val;
284e52af513SShlomo Pongratz gicv3_cache_target_cpustate(s, irq);
285e52af513SShlomo Pongratz gicv3_update(s, irq, 1);
286e52af513SShlomo Pongratz }
287e52af513SShlomo Pongratz
2885dcf0d3aSPhilippe Mathieu-Daudé /**
2895dcf0d3aSPhilippe Mathieu-Daudé * gicd_readb
2905dcf0d3aSPhilippe Mathieu-Daudé * gicd_readw
2915dcf0d3aSPhilippe Mathieu-Daudé * gicd_readl
2925dcf0d3aSPhilippe Mathieu-Daudé * gicd_readq
2935dcf0d3aSPhilippe Mathieu-Daudé * gicd_writeb
2945dcf0d3aSPhilippe Mathieu-Daudé * gicd_writew
2955dcf0d3aSPhilippe Mathieu-Daudé * gicd_writel
2965dcf0d3aSPhilippe Mathieu-Daudé * gicd_writeq
2975dcf0d3aSPhilippe Mathieu-Daudé *
2985dcf0d3aSPhilippe Mathieu-Daudé * Return %true if the operation succeeded, %false otherwise.
2995dcf0d3aSPhilippe Mathieu-Daudé */
3005dcf0d3aSPhilippe Mathieu-Daudé
gicd_readb(GICv3State * s,hwaddr offset,uint64_t * data,MemTxAttrs attrs)3015dcf0d3aSPhilippe Mathieu-Daudé static bool gicd_readb(GICv3State *s, hwaddr offset,
302e52af513SShlomo Pongratz uint64_t *data, MemTxAttrs attrs)
303e52af513SShlomo Pongratz {
304e52af513SShlomo Pongratz /* Most GICv3 distributor registers do not support byte accesses. */
305e52af513SShlomo Pongratz switch (offset) {
306e52af513SShlomo Pongratz case GICD_CPENDSGIR ... GICD_CPENDSGIR + 0xf:
307e52af513SShlomo Pongratz case GICD_SPENDSGIR ... GICD_SPENDSGIR + 0xf:
308e52af513SShlomo Pongratz case GICD_ITARGETSR ... GICD_ITARGETSR + 0x3ff:
309e52af513SShlomo Pongratz /* This GIC implementation always has affinity routing enabled,
310e52af513SShlomo Pongratz * so these registers are all RAZ/WI.
311e52af513SShlomo Pongratz */
3125dcf0d3aSPhilippe Mathieu-Daudé return true;
313e52af513SShlomo Pongratz case GICD_IPRIORITYR ... GICD_IPRIORITYR + 0x3ff:
314e52af513SShlomo Pongratz *data = gicd_read_ipriorityr(s, attrs, offset - GICD_IPRIORITYR);
3155dcf0d3aSPhilippe Mathieu-Daudé return true;
316e52af513SShlomo Pongratz default:
3175dcf0d3aSPhilippe Mathieu-Daudé return false;
318e52af513SShlomo Pongratz }
319e52af513SShlomo Pongratz }
320e52af513SShlomo Pongratz
gicd_writeb(GICv3State * s,hwaddr offset,uint64_t value,MemTxAttrs attrs)3215dcf0d3aSPhilippe Mathieu-Daudé static bool gicd_writeb(GICv3State *s, hwaddr offset,
322e52af513SShlomo Pongratz uint64_t value, MemTxAttrs attrs)
323e52af513SShlomo Pongratz {
324e52af513SShlomo Pongratz /* Most GICv3 distributor registers do not support byte accesses. */
325e52af513SShlomo Pongratz switch (offset) {
326e52af513SShlomo Pongratz case GICD_CPENDSGIR ... GICD_CPENDSGIR + 0xf:
327e52af513SShlomo Pongratz case GICD_SPENDSGIR ... GICD_SPENDSGIR + 0xf:
328e52af513SShlomo Pongratz case GICD_ITARGETSR ... GICD_ITARGETSR + 0x3ff:
329e52af513SShlomo Pongratz /* This GIC implementation always has affinity routing enabled,
330e52af513SShlomo Pongratz * so these registers are all RAZ/WI.
331e52af513SShlomo Pongratz */
3325dcf0d3aSPhilippe Mathieu-Daudé return true;
333e52af513SShlomo Pongratz case GICD_IPRIORITYR ... GICD_IPRIORITYR + 0x3ff:
334e52af513SShlomo Pongratz {
335e52af513SShlomo Pongratz int irq = offset - GICD_IPRIORITYR;
336e52af513SShlomo Pongratz
337e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
3385dcf0d3aSPhilippe Mathieu-Daudé return true;
339e52af513SShlomo Pongratz }
340e52af513SShlomo Pongratz gicd_write_ipriorityr(s, attrs, irq, value);
341e52af513SShlomo Pongratz gicv3_update(s, irq, 1);
3425dcf0d3aSPhilippe Mathieu-Daudé return true;
343e52af513SShlomo Pongratz }
344e52af513SShlomo Pongratz default:
3455dcf0d3aSPhilippe Mathieu-Daudé return false;
346e52af513SShlomo Pongratz }
347e52af513SShlomo Pongratz }
348e52af513SShlomo Pongratz
gicd_readw(GICv3State * s,hwaddr offset,uint64_t * data,MemTxAttrs attrs)3495dcf0d3aSPhilippe Mathieu-Daudé static bool gicd_readw(GICv3State *s, hwaddr offset,
350e52af513SShlomo Pongratz uint64_t *data, MemTxAttrs attrs)
351e52af513SShlomo Pongratz {
352e52af513SShlomo Pongratz /* Only GICD_SETSPI_NSR, GICD_CLRSPI_NSR, GICD_SETSPI_SR and GICD_SETSPI_NSR
353e52af513SShlomo Pongratz * support 16 bit accesses, and those registers are all part of the
354e52af513SShlomo Pongratz * optional message-based SPI feature which this GIC does not currently
355e52af513SShlomo Pongratz * implement (ie for us GICD_TYPER.MBIS == 0), so for us they are
356e52af513SShlomo Pongratz * reserved.
357e52af513SShlomo Pongratz */
3585dcf0d3aSPhilippe Mathieu-Daudé return false;
359e52af513SShlomo Pongratz }
360e52af513SShlomo Pongratz
gicd_writew(GICv3State * s,hwaddr offset,uint64_t value,MemTxAttrs attrs)3615dcf0d3aSPhilippe Mathieu-Daudé static bool gicd_writew(GICv3State *s, hwaddr offset,
362e52af513SShlomo Pongratz uint64_t value, MemTxAttrs attrs)
363e52af513SShlomo Pongratz {
364e52af513SShlomo Pongratz /* Only GICD_SETSPI_NSR, GICD_CLRSPI_NSR, GICD_SETSPI_SR and GICD_SETSPI_NSR
365e52af513SShlomo Pongratz * support 16 bit accesses, and those registers are all part of the
366e52af513SShlomo Pongratz * optional message-based SPI feature which this GIC does not currently
367e52af513SShlomo Pongratz * implement (ie for us GICD_TYPER.MBIS == 0), so for us they are
368e52af513SShlomo Pongratz * reserved.
369e52af513SShlomo Pongratz */
3705dcf0d3aSPhilippe Mathieu-Daudé return false;
371e52af513SShlomo Pongratz }
372e52af513SShlomo Pongratz
gicd_readl(GICv3State * s,hwaddr offset,uint64_t * data,MemTxAttrs attrs)3735dcf0d3aSPhilippe Mathieu-Daudé static bool gicd_readl(GICv3State *s, hwaddr offset,
374e52af513SShlomo Pongratz uint64_t *data, MemTxAttrs attrs)
375e52af513SShlomo Pongratz {
376e52af513SShlomo Pongratz /* Almost all GICv3 distributor registers are 32-bit.
377e52af513SShlomo Pongratz * Note that WO registers must return an UNKNOWN value on reads,
378e52af513SShlomo Pongratz * not an abort.
379e52af513SShlomo Pongratz */
380e52af513SShlomo Pongratz
381e52af513SShlomo Pongratz switch (offset) {
382e52af513SShlomo Pongratz case GICD_CTLR:
383e52af513SShlomo Pongratz if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
384e52af513SShlomo Pongratz /* The NS view of the GICD_CTLR sees only certain bits:
385e52af513SShlomo Pongratz * + bit [31] (RWP) is an alias of the Secure bit [31]
386e52af513SShlomo Pongratz * + bit [4] (ARE_NS) is an alias of Secure bit [5]
387e52af513SShlomo Pongratz * + bit [1] (EnableGrp1A) is an alias of Secure bit [1] if
388e52af513SShlomo Pongratz * NS affinity routing is enabled, otherwise RES0
389e52af513SShlomo Pongratz * + bit [0] (EnableGrp1) is an alias of Secure bit [1] if
390e52af513SShlomo Pongratz * NS affinity routing is not enabled, otherwise RES0
391e52af513SShlomo Pongratz * Since for QEMU affinity routing is always enabled
392e52af513SShlomo Pongratz * for both S and NS this means that bits [4] and [5] are
393e52af513SShlomo Pongratz * both always 1, and we can simply make the NS view
394e52af513SShlomo Pongratz * be bits 31, 4 and 1 of the S view.
395e52af513SShlomo Pongratz */
396e52af513SShlomo Pongratz *data = s->gicd_ctlr & (GICD_CTLR_ARE_S |
397e52af513SShlomo Pongratz GICD_CTLR_EN_GRP1NS |
398e52af513SShlomo Pongratz GICD_CTLR_RWP);
399e52af513SShlomo Pongratz } else {
400e52af513SShlomo Pongratz *data = s->gicd_ctlr;
401e52af513SShlomo Pongratz }
4025dcf0d3aSPhilippe Mathieu-Daudé return true;
403e52af513SShlomo Pongratz case GICD_TYPER:
404e52af513SShlomo Pongratz {
405e52af513SShlomo Pongratz /* For this implementation:
406e52af513SShlomo Pongratz * No1N == 1 (1-of-N SPI interrupts not supported)
407e52af513SShlomo Pongratz * A3V == 1 (non-zero values of Affinity level 3 supported)
408e52af513SShlomo Pongratz * IDbits == 0xf (we support 16-bit interrupt identifiers)
409e2d5e189SPeter Maydell * DVIS == 1 (Direct virtual LPI injection supported) if GICv4
410ac30dec3SShashi Mallela * LPIS == 1 (LPIs are supported if affinity routing is enabled)
411ac30dec3SShashi Mallela * num_LPIs == 0b00000 (bits [15:11],Number of LPIs as indicated
412ac30dec3SShashi Mallela * by GICD_TYPER.IDbits)
413e52af513SShlomo Pongratz * MBIS == 0 (message-based SPIs not supported)
414e52af513SShlomo Pongratz * SecurityExtn == 1 if security extns supported
415c9e86cbdSJinjie Ruan * NMI = 1 if Non-maskable interrupt property is supported
416e52af513SShlomo Pongratz * CPUNumber == 0 since for us ARE is always 1
41758dff8f7SLuke Starrett * ITLinesNumber == (((max SPI IntID + 1) / 32) - 1)
418e52af513SShlomo Pongratz */
41958dff8f7SLuke Starrett int itlinesnumber = (s->num_irq / 32) - 1;
4200edfcc9eSPeter Maydell /*
4210edfcc9eSPeter Maydell * SecurityExtn must be RAZ if GICD_CTLR.DS == 1, and
4220edfcc9eSPeter Maydell * "security extensions not supported" always implies DS == 1,
4230edfcc9eSPeter Maydell * so we only need to check the DS bit.
4240edfcc9eSPeter Maydell */
4250edfcc9eSPeter Maydell bool sec_extn = !(s->gicd_ctlr & GICD_CTLR_DS);
426e2d5e189SPeter Maydell bool dvis = s->revision >= 4;
427e52af513SShlomo Pongratz
428e2d5e189SPeter Maydell *data = (1 << 25) | (1 << 24) | (dvis << 18) | (sec_extn << 10) |
429c9e86cbdSJinjie Ruan (s->nmi_support << GICD_TYPER_NMI_SHIFT) |
430ac30dec3SShashi Mallela (s->lpi_enable << GICD_TYPER_LPIS_SHIFT) |
431e52af513SShlomo Pongratz (0xf << 19) | itlinesnumber;
4325dcf0d3aSPhilippe Mathieu-Daudé return true;
433e52af513SShlomo Pongratz }
434e52af513SShlomo Pongratz case GICD_IIDR:
435e52af513SShlomo Pongratz /* We claim to be an ARM r0p0 with a zero ProductID.
436e52af513SShlomo Pongratz * This is the same as an r0p0 GIC-500.
437e52af513SShlomo Pongratz */
438e52af513SShlomo Pongratz *data = gicv3_iidr();
4395dcf0d3aSPhilippe Mathieu-Daudé return true;
440e52af513SShlomo Pongratz case GICD_STATUSR:
441e52af513SShlomo Pongratz /* RAZ/WI for us (this is an optional register and our implementation
442e52af513SShlomo Pongratz * does not track RO/WO/reserved violations to report them to the guest)
443e52af513SShlomo Pongratz */
444e52af513SShlomo Pongratz *data = 0;
4455dcf0d3aSPhilippe Mathieu-Daudé return true;
446e52af513SShlomo Pongratz case GICD_IGROUPR ... GICD_IGROUPR + 0x7f:
447e52af513SShlomo Pongratz {
448e52af513SShlomo Pongratz int irq;
449e52af513SShlomo Pongratz
450e52af513SShlomo Pongratz if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
451e52af513SShlomo Pongratz *data = 0;
4525dcf0d3aSPhilippe Mathieu-Daudé return true;
453e52af513SShlomo Pongratz }
454e52af513SShlomo Pongratz /* RAZ/WI for SGIs, PPIs, unimplemented irqs */
455e52af513SShlomo Pongratz irq = (offset - GICD_IGROUPR) * 8;
456e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
457e52af513SShlomo Pongratz *data = 0;
4585dcf0d3aSPhilippe Mathieu-Daudé return true;
459e52af513SShlomo Pongratz }
460e52af513SShlomo Pongratz *data = *gic_bmp_ptr32(s->group, irq);
4615dcf0d3aSPhilippe Mathieu-Daudé return true;
462e52af513SShlomo Pongratz }
463e52af513SShlomo Pongratz case GICD_ISENABLER ... GICD_ISENABLER + 0x7f:
464e52af513SShlomo Pongratz *data = gicd_read_bitmap_reg(s, attrs, s->enabled, NULL,
465e52af513SShlomo Pongratz offset - GICD_ISENABLER);
4665dcf0d3aSPhilippe Mathieu-Daudé return true;
467e52af513SShlomo Pongratz case GICD_ICENABLER ... GICD_ICENABLER + 0x7f:
468e52af513SShlomo Pongratz *data = gicd_read_bitmap_reg(s, attrs, s->enabled, NULL,
469e52af513SShlomo Pongratz offset - GICD_ICENABLER);
4705dcf0d3aSPhilippe Mathieu-Daudé return true;
471e52af513SShlomo Pongratz case GICD_ISPENDR ... GICD_ISPENDR + 0x7f:
472e52af513SShlomo Pongratz *data = gicd_read_bitmap_reg(s, attrs, s->pending, mask_nsacr_ge1,
473e52af513SShlomo Pongratz offset - GICD_ISPENDR);
4745dcf0d3aSPhilippe Mathieu-Daudé return true;
475e52af513SShlomo Pongratz case GICD_ICPENDR ... GICD_ICPENDR + 0x7f:
476e52af513SShlomo Pongratz *data = gicd_read_bitmap_reg(s, attrs, s->pending, mask_nsacr_ge2,
477e52af513SShlomo Pongratz offset - GICD_ICPENDR);
4785dcf0d3aSPhilippe Mathieu-Daudé return true;
479e52af513SShlomo Pongratz case GICD_ISACTIVER ... GICD_ISACTIVER + 0x7f:
480e52af513SShlomo Pongratz *data = gicd_read_bitmap_reg(s, attrs, s->active, mask_nsacr_ge2,
481e52af513SShlomo Pongratz offset - GICD_ISACTIVER);
4825dcf0d3aSPhilippe Mathieu-Daudé return true;
483e52af513SShlomo Pongratz case GICD_ICACTIVER ... GICD_ICACTIVER + 0x7f:
484e52af513SShlomo Pongratz *data = gicd_read_bitmap_reg(s, attrs, s->active, mask_nsacr_ge2,
485e52af513SShlomo Pongratz offset - GICD_ICACTIVER);
4865dcf0d3aSPhilippe Mathieu-Daudé return true;
487e52af513SShlomo Pongratz case GICD_IPRIORITYR ... GICD_IPRIORITYR + 0x3ff:
488e52af513SShlomo Pongratz {
489e52af513SShlomo Pongratz int i, irq = offset - GICD_IPRIORITYR;
490e52af513SShlomo Pongratz uint32_t value = 0;
491e52af513SShlomo Pongratz
492d419890cSAmol Surati for (i = irq + 3; i >= irq; i--) {
493d419890cSAmol Surati value <<= 8;
494e52af513SShlomo Pongratz value |= gicd_read_ipriorityr(s, attrs, i);
495e52af513SShlomo Pongratz }
496e52af513SShlomo Pongratz *data = value;
4975dcf0d3aSPhilippe Mathieu-Daudé return true;
498e52af513SShlomo Pongratz }
499e52af513SShlomo Pongratz case GICD_ITARGETSR ... GICD_ITARGETSR + 0x3ff:
500e52af513SShlomo Pongratz /* RAZ/WI since affinity routing is always enabled */
501e52af513SShlomo Pongratz *data = 0;
5025dcf0d3aSPhilippe Mathieu-Daudé return true;
503e52af513SShlomo Pongratz case GICD_ICFGR ... GICD_ICFGR + 0xff:
504e52af513SShlomo Pongratz {
505e52af513SShlomo Pongratz /* Here only the even bits are used; odd bits are RES0 */
506e52af513SShlomo Pongratz int irq = (offset - GICD_ICFGR) * 4;
507e52af513SShlomo Pongratz uint32_t value = 0;
508e52af513SShlomo Pongratz
509e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
510e52af513SShlomo Pongratz *data = 0;
5115dcf0d3aSPhilippe Mathieu-Daudé return true;
512e52af513SShlomo Pongratz }
513e52af513SShlomo Pongratz
514e52af513SShlomo Pongratz /* Since our edge_trigger bitmap is one bit per irq, we only need
515e52af513SShlomo Pongratz * half of the 32-bit word, which we can then spread out
516e52af513SShlomo Pongratz * into the odd bits.
517e52af513SShlomo Pongratz */
518e52af513SShlomo Pongratz value = *gic_bmp_ptr32(s->edge_trigger, irq & ~0x1f);
519e52af513SShlomo Pongratz value &= mask_group_and_nsacr(s, attrs, NULL, irq & ~0x1f);
520e52af513SShlomo Pongratz value = extract32(value, (irq & 0x1f) ? 16 : 0, 16);
521e52af513SShlomo Pongratz value = half_shuffle32(value) << 1;
522e52af513SShlomo Pongratz *data = value;
5235dcf0d3aSPhilippe Mathieu-Daudé return true;
524e52af513SShlomo Pongratz }
525e52af513SShlomo Pongratz case GICD_IGRPMODR ... GICD_IGRPMODR + 0xff:
526e52af513SShlomo Pongratz {
527e52af513SShlomo Pongratz int irq;
528e52af513SShlomo Pongratz
529e52af513SShlomo Pongratz if ((s->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
530e52af513SShlomo Pongratz /* RAZ/WI if security disabled, or if
531e52af513SShlomo Pongratz * security enabled and this is an NS access
532e52af513SShlomo Pongratz */
533e52af513SShlomo Pongratz *data = 0;
5345dcf0d3aSPhilippe Mathieu-Daudé return true;
535e52af513SShlomo Pongratz }
536e52af513SShlomo Pongratz /* RAZ/WI for SGIs, PPIs, unimplemented irqs */
537e52af513SShlomo Pongratz irq = (offset - GICD_IGRPMODR) * 8;
538e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
539e52af513SShlomo Pongratz *data = 0;
5405dcf0d3aSPhilippe Mathieu-Daudé return true;
541e52af513SShlomo Pongratz }
542e52af513SShlomo Pongratz *data = *gic_bmp_ptr32(s->grpmod, irq);
5435dcf0d3aSPhilippe Mathieu-Daudé return true;
544e52af513SShlomo Pongratz }
545e52af513SShlomo Pongratz case GICD_NSACR ... GICD_NSACR + 0xff:
546e52af513SShlomo Pongratz {
547e52af513SShlomo Pongratz /* Two bits per interrupt */
548e52af513SShlomo Pongratz int irq = (offset - GICD_NSACR) * 4;
549e52af513SShlomo Pongratz
550e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
551e52af513SShlomo Pongratz *data = 0;
5525dcf0d3aSPhilippe Mathieu-Daudé return true;
553e52af513SShlomo Pongratz }
554e52af513SShlomo Pongratz
555e52af513SShlomo Pongratz if ((s->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
556e52af513SShlomo Pongratz /* RAZ/WI if security disabled, or if
557e52af513SShlomo Pongratz * security enabled and this is an NS access
558e52af513SShlomo Pongratz */
559e52af513SShlomo Pongratz *data = 0;
5605dcf0d3aSPhilippe Mathieu-Daudé return true;
561e52af513SShlomo Pongratz }
562e52af513SShlomo Pongratz
563e52af513SShlomo Pongratz *data = s->gicd_nsacr[irq / 16];
5645dcf0d3aSPhilippe Mathieu-Daudé return true;
565e52af513SShlomo Pongratz }
566e52af513SShlomo Pongratz case GICD_CPENDSGIR ... GICD_CPENDSGIR + 0xf:
567e52af513SShlomo Pongratz case GICD_SPENDSGIR ... GICD_SPENDSGIR + 0xf:
568e52af513SShlomo Pongratz /* RAZ/WI since affinity routing is always enabled */
569e52af513SShlomo Pongratz *data = 0;
5705dcf0d3aSPhilippe Mathieu-Daudé return true;
571*44ed1e4bSJinjie Ruan case GICD_INMIR ... GICD_INMIR + 0x7f:
572*44ed1e4bSJinjie Ruan *data = (!s->nmi_support) ? 0 :
573*44ed1e4bSJinjie Ruan gicd_read_bitmap_reg(s, attrs, s->nmi, NULL,
574*44ed1e4bSJinjie Ruan offset - GICD_INMIR);
575*44ed1e4bSJinjie Ruan return true;
576e52af513SShlomo Pongratz case GICD_IROUTER ... GICD_IROUTER + 0x1fdf:
577e52af513SShlomo Pongratz {
578e52af513SShlomo Pongratz uint64_t r;
579e52af513SShlomo Pongratz int irq = (offset - GICD_IROUTER) / 8;
580e52af513SShlomo Pongratz
581e52af513SShlomo Pongratz r = gicd_read_irouter(s, attrs, irq);
582e52af513SShlomo Pongratz if (offset & 7) {
583e52af513SShlomo Pongratz *data = r >> 32;
584e52af513SShlomo Pongratz } else {
585e52af513SShlomo Pongratz *data = (uint32_t)r;
586e52af513SShlomo Pongratz }
5875dcf0d3aSPhilippe Mathieu-Daudé return true;
588e52af513SShlomo Pongratz }
589e40f6073SPeter Maydell case GICD_IDREGS ... GICD_IDREGS + 0x2f:
590e52af513SShlomo Pongratz /* ID registers */
591e2d5e189SPeter Maydell *data = gicv3_idreg(s, offset - GICD_IDREGS, GICV3_PIDR0_DIST);
5925dcf0d3aSPhilippe Mathieu-Daudé return true;
593e52af513SShlomo Pongratz case GICD_SGIR:
594e52af513SShlomo Pongratz /* WO registers, return unknown value */
595e52af513SShlomo Pongratz qemu_log_mask(LOG_GUEST_ERROR,
596e52af513SShlomo Pongratz "%s: invalid guest read from WO register at offset "
597883f2c59SPhilippe Mathieu-Daudé HWADDR_FMT_plx "\n", __func__, offset);
598e52af513SShlomo Pongratz *data = 0;
5995dcf0d3aSPhilippe Mathieu-Daudé return true;
600e52af513SShlomo Pongratz default:
6015dcf0d3aSPhilippe Mathieu-Daudé return false;
602e52af513SShlomo Pongratz }
603e52af513SShlomo Pongratz }
604e52af513SShlomo Pongratz
gicd_writel(GICv3State * s,hwaddr offset,uint64_t value,MemTxAttrs attrs)6055dcf0d3aSPhilippe Mathieu-Daudé static bool gicd_writel(GICv3State *s, hwaddr offset,
606e52af513SShlomo Pongratz uint64_t value, MemTxAttrs attrs)
607e52af513SShlomo Pongratz {
608e52af513SShlomo Pongratz /* Almost all GICv3 distributor registers are 32-bit. Note that
609e52af513SShlomo Pongratz * RO registers must ignore writes, not abort.
610e52af513SShlomo Pongratz */
611e52af513SShlomo Pongratz
612e52af513SShlomo Pongratz switch (offset) {
613e52af513SShlomo Pongratz case GICD_CTLR:
614e52af513SShlomo Pongratz {
615e52af513SShlomo Pongratz uint32_t mask;
616e52af513SShlomo Pongratz /* GICv3 5.3.20 */
617e52af513SShlomo Pongratz if (s->gicd_ctlr & GICD_CTLR_DS) {
618e52af513SShlomo Pongratz /* With only one security state, E1NWF is RAZ/WI, DS is RAO/WI,
619e52af513SShlomo Pongratz * ARE is RAO/WI (affinity routing always on), and only
620e52af513SShlomo Pongratz * bits 0 and 1 (group enables) are writable.
621e52af513SShlomo Pongratz */
622e52af513SShlomo Pongratz mask = GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1NS;
623e52af513SShlomo Pongratz } else {
624e52af513SShlomo Pongratz if (attrs.secure) {
625e52af513SShlomo Pongratz /* for secure access:
626e52af513SShlomo Pongratz * ARE_NS and ARE_S are RAO/WI (affinity routing always on)
627e52af513SShlomo Pongratz * E1NWF is RAZ/WI (we don't support enable-1-of-n-wakeup)
628e52af513SShlomo Pongratz *
629e52af513SShlomo Pongratz * We can only modify bits[2:0] (the group enables).
630e52af513SShlomo Pongratz */
631e52af513SShlomo Pongratz mask = GICD_CTLR_DS | GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1_ALL;
632e52af513SShlomo Pongratz } else {
633e52af513SShlomo Pongratz /* For non secure access ARE_NS is RAO/WI and EnableGrp1
634e52af513SShlomo Pongratz * is RES0. The only writable bit is [1] (EnableGrp1A), which
635e52af513SShlomo Pongratz * is an alias of the Secure bit [1].
636e52af513SShlomo Pongratz */
637e52af513SShlomo Pongratz mask = GICD_CTLR_EN_GRP1NS;
638e52af513SShlomo Pongratz }
639e52af513SShlomo Pongratz }
640e52af513SShlomo Pongratz s->gicd_ctlr = (s->gicd_ctlr & ~mask) | (value & mask);
641e52af513SShlomo Pongratz if (value & mask & GICD_CTLR_DS) {
642e52af513SShlomo Pongratz /* We just set DS, so the ARE_NS and EnG1S bits are now RES0.
643e52af513SShlomo Pongratz * Note that this is a one-way transition because if DS is set
6449323e79fSPeter Maydell * then it's not writable, so it can only go back to 0 with a
645e52af513SShlomo Pongratz * hardware reset.
646e52af513SShlomo Pongratz */
647e52af513SShlomo Pongratz s->gicd_ctlr &= ~(GICD_CTLR_EN_GRP1S | GICD_CTLR_ARE_NS);
648e52af513SShlomo Pongratz }
649e52af513SShlomo Pongratz gicv3_full_update(s);
6505dcf0d3aSPhilippe Mathieu-Daudé return true;
651e52af513SShlomo Pongratz }
652e52af513SShlomo Pongratz case GICD_STATUSR:
653e52af513SShlomo Pongratz /* RAZ/WI for our implementation */
6545dcf0d3aSPhilippe Mathieu-Daudé return true;
655e52af513SShlomo Pongratz case GICD_IGROUPR ... GICD_IGROUPR + 0x7f:
656e52af513SShlomo Pongratz {
657e52af513SShlomo Pongratz int irq;
658e52af513SShlomo Pongratz
659e52af513SShlomo Pongratz if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
6605dcf0d3aSPhilippe Mathieu-Daudé return true;
661e52af513SShlomo Pongratz }
662e52af513SShlomo Pongratz /* RAZ/WI for SGIs, PPIs, unimplemented irqs */
663e52af513SShlomo Pongratz irq = (offset - GICD_IGROUPR) * 8;
664e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
6655dcf0d3aSPhilippe Mathieu-Daudé return true;
666e52af513SShlomo Pongratz }
667e52af513SShlomo Pongratz *gic_bmp_ptr32(s->group, irq) = value;
668e52af513SShlomo Pongratz gicv3_update(s, irq, 32);
6695dcf0d3aSPhilippe Mathieu-Daudé return true;
670e52af513SShlomo Pongratz }
671e52af513SShlomo Pongratz case GICD_ISENABLER ... GICD_ISENABLER + 0x7f:
672e52af513SShlomo Pongratz gicd_write_set_bitmap_reg(s, attrs, s->enabled, NULL,
673e52af513SShlomo Pongratz offset - GICD_ISENABLER, value);
6745dcf0d3aSPhilippe Mathieu-Daudé return true;
675e52af513SShlomo Pongratz case GICD_ICENABLER ... GICD_ICENABLER + 0x7f:
676e52af513SShlomo Pongratz gicd_write_clear_bitmap_reg(s, attrs, s->enabled, NULL,
677e52af513SShlomo Pongratz offset - GICD_ICENABLER, value);
6785dcf0d3aSPhilippe Mathieu-Daudé return true;
679e52af513SShlomo Pongratz case GICD_ISPENDR ... GICD_ISPENDR + 0x7f:
680e52af513SShlomo Pongratz gicd_write_set_bitmap_reg(s, attrs, s->pending, mask_nsacr_ge1,
681e52af513SShlomo Pongratz offset - GICD_ISPENDR, value);
6825dcf0d3aSPhilippe Mathieu-Daudé return true;
683e52af513SShlomo Pongratz case GICD_ICPENDR ... GICD_ICPENDR + 0x7f:
684e52af513SShlomo Pongratz gicd_write_clear_bitmap_reg(s, attrs, s->pending, mask_nsacr_ge2,
685e52af513SShlomo Pongratz offset - GICD_ICPENDR, value);
6865dcf0d3aSPhilippe Mathieu-Daudé return true;
687e52af513SShlomo Pongratz case GICD_ISACTIVER ... GICD_ISACTIVER + 0x7f:
688e52af513SShlomo Pongratz gicd_write_set_bitmap_reg(s, attrs, s->active, NULL,
689e52af513SShlomo Pongratz offset - GICD_ISACTIVER, value);
6905dcf0d3aSPhilippe Mathieu-Daudé return true;
691e52af513SShlomo Pongratz case GICD_ICACTIVER ... GICD_ICACTIVER + 0x7f:
692e52af513SShlomo Pongratz gicd_write_clear_bitmap_reg(s, attrs, s->active, NULL,
693e52af513SShlomo Pongratz offset - GICD_ICACTIVER, value);
6945dcf0d3aSPhilippe Mathieu-Daudé return true;
695e52af513SShlomo Pongratz case GICD_IPRIORITYR ... GICD_IPRIORITYR + 0x3ff:
696e52af513SShlomo Pongratz {
697e52af513SShlomo Pongratz int i, irq = offset - GICD_IPRIORITYR;
698e52af513SShlomo Pongratz
699e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq + 3 >= s->num_irq) {
7005dcf0d3aSPhilippe Mathieu-Daudé return true;
701e52af513SShlomo Pongratz }
702e52af513SShlomo Pongratz
703e52af513SShlomo Pongratz for (i = irq; i < irq + 4; i++, value >>= 8) {
704e52af513SShlomo Pongratz gicd_write_ipriorityr(s, attrs, i, value);
705e52af513SShlomo Pongratz }
706e52af513SShlomo Pongratz gicv3_update(s, irq, 4);
7075dcf0d3aSPhilippe Mathieu-Daudé return true;
708e52af513SShlomo Pongratz }
709e52af513SShlomo Pongratz case GICD_ITARGETSR ... GICD_ITARGETSR + 0x3ff:
710e52af513SShlomo Pongratz /* RAZ/WI since affinity routing is always enabled */
7115dcf0d3aSPhilippe Mathieu-Daudé return true;
712e52af513SShlomo Pongratz case GICD_ICFGR ... GICD_ICFGR + 0xff:
713e52af513SShlomo Pongratz {
714e52af513SShlomo Pongratz /* Here only the odd bits are used; even bits are RES0 */
715e52af513SShlomo Pongratz int irq = (offset - GICD_ICFGR) * 4;
716e52af513SShlomo Pongratz uint32_t mask, oldval;
717e52af513SShlomo Pongratz
718e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
7195dcf0d3aSPhilippe Mathieu-Daudé return true;
720e52af513SShlomo Pongratz }
721e52af513SShlomo Pongratz
722e52af513SShlomo Pongratz /* Since our edge_trigger bitmap is one bit per irq, our input
723e52af513SShlomo Pongratz * 32-bits will compress down into 16 bits which we need
724e52af513SShlomo Pongratz * to write into the bitmap.
725e52af513SShlomo Pongratz */
726e52af513SShlomo Pongratz value = half_unshuffle32(value >> 1);
727e52af513SShlomo Pongratz mask = mask_group_and_nsacr(s, attrs, NULL, irq & ~0x1f);
728e52af513SShlomo Pongratz if (irq & 0x1f) {
729e52af513SShlomo Pongratz value <<= 16;
730e52af513SShlomo Pongratz mask &= 0xffff0000U;
731e52af513SShlomo Pongratz } else {
732e52af513SShlomo Pongratz mask &= 0xffff;
733e52af513SShlomo Pongratz }
734e52af513SShlomo Pongratz oldval = *gic_bmp_ptr32(s->edge_trigger, (irq & ~0x1f));
735e52af513SShlomo Pongratz value = (oldval & ~mask) | (value & mask);
736e52af513SShlomo Pongratz *gic_bmp_ptr32(s->edge_trigger, irq & ~0x1f) = value;
7375dcf0d3aSPhilippe Mathieu-Daudé return true;
738e52af513SShlomo Pongratz }
739e52af513SShlomo Pongratz case GICD_IGRPMODR ... GICD_IGRPMODR + 0xff:
740e52af513SShlomo Pongratz {
741e52af513SShlomo Pongratz int irq;
742e52af513SShlomo Pongratz
743e52af513SShlomo Pongratz if ((s->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
744e52af513SShlomo Pongratz /* RAZ/WI if security disabled, or if
745e52af513SShlomo Pongratz * security enabled and this is an NS access
746e52af513SShlomo Pongratz */
7475dcf0d3aSPhilippe Mathieu-Daudé return true;
748e52af513SShlomo Pongratz }
749e52af513SShlomo Pongratz /* RAZ/WI for SGIs, PPIs, unimplemented irqs */
750e52af513SShlomo Pongratz irq = (offset - GICD_IGRPMODR) * 8;
751e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
7525dcf0d3aSPhilippe Mathieu-Daudé return true;
753e52af513SShlomo Pongratz }
754e52af513SShlomo Pongratz *gic_bmp_ptr32(s->grpmod, irq) = value;
755e52af513SShlomo Pongratz gicv3_update(s, irq, 32);
7565dcf0d3aSPhilippe Mathieu-Daudé return true;
757e52af513SShlomo Pongratz }
758e52af513SShlomo Pongratz case GICD_NSACR ... GICD_NSACR + 0xff:
759e52af513SShlomo Pongratz {
760e52af513SShlomo Pongratz /* Two bits per interrupt */
761e52af513SShlomo Pongratz int irq = (offset - GICD_NSACR) * 4;
762e52af513SShlomo Pongratz
763e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
7645dcf0d3aSPhilippe Mathieu-Daudé return true;
765e52af513SShlomo Pongratz }
766e52af513SShlomo Pongratz
767e52af513SShlomo Pongratz if ((s->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
768e52af513SShlomo Pongratz /* RAZ/WI if security disabled, or if
769e52af513SShlomo Pongratz * security enabled and this is an NS access
770e52af513SShlomo Pongratz */
7715dcf0d3aSPhilippe Mathieu-Daudé return true;
772e52af513SShlomo Pongratz }
773e52af513SShlomo Pongratz
774e52af513SShlomo Pongratz s->gicd_nsacr[irq / 16] = value;
775e52af513SShlomo Pongratz /* No update required as this only affects access permission checks */
7765dcf0d3aSPhilippe Mathieu-Daudé return true;
777e52af513SShlomo Pongratz }
778e52af513SShlomo Pongratz case GICD_SGIR:
779e52af513SShlomo Pongratz /* RES0 if affinity routing is enabled */
7805dcf0d3aSPhilippe Mathieu-Daudé return true;
781e52af513SShlomo Pongratz case GICD_CPENDSGIR ... GICD_CPENDSGIR + 0xf:
782e52af513SShlomo Pongratz case GICD_SPENDSGIR ... GICD_SPENDSGIR + 0xf:
783e52af513SShlomo Pongratz /* RAZ/WI since affinity routing is always enabled */
7845dcf0d3aSPhilippe Mathieu-Daudé return true;
785*44ed1e4bSJinjie Ruan case GICD_INMIR ... GICD_INMIR + 0x7f:
786*44ed1e4bSJinjie Ruan if (s->nmi_support) {
787*44ed1e4bSJinjie Ruan gicd_write_bitmap_reg(s, attrs, s->nmi, NULL,
788*44ed1e4bSJinjie Ruan offset - GICD_INMIR, value);
789*44ed1e4bSJinjie Ruan }
790*44ed1e4bSJinjie Ruan return true;
791e52af513SShlomo Pongratz case GICD_IROUTER ... GICD_IROUTER + 0x1fdf:
792e52af513SShlomo Pongratz {
793e52af513SShlomo Pongratz uint64_t r;
794e52af513SShlomo Pongratz int irq = (offset - GICD_IROUTER) / 8;
795e52af513SShlomo Pongratz
796e52af513SShlomo Pongratz if (irq < GIC_INTERNAL || irq >= s->num_irq) {
7975dcf0d3aSPhilippe Mathieu-Daudé return true;
798e52af513SShlomo Pongratz }
799e52af513SShlomo Pongratz
800e52af513SShlomo Pongratz /* Write half of the 64-bit register */
801e52af513SShlomo Pongratz r = gicd_read_irouter(s, attrs, irq);
802e52af513SShlomo Pongratz r = deposit64(r, (offset & 7) ? 32 : 0, 32, value);
803e52af513SShlomo Pongratz gicd_write_irouter(s, attrs, irq, r);
8045dcf0d3aSPhilippe Mathieu-Daudé return true;
805e52af513SShlomo Pongratz }
806e40f6073SPeter Maydell case GICD_IDREGS ... GICD_IDREGS + 0x2f:
807e52af513SShlomo Pongratz case GICD_TYPER:
808e52af513SShlomo Pongratz case GICD_IIDR:
809e52af513SShlomo Pongratz /* RO registers, ignore the write */
810e52af513SShlomo Pongratz qemu_log_mask(LOG_GUEST_ERROR,
811e52af513SShlomo Pongratz "%s: invalid guest write to RO register at offset "
812883f2c59SPhilippe Mathieu-Daudé HWADDR_FMT_plx "\n", __func__, offset);
8135dcf0d3aSPhilippe Mathieu-Daudé return true;
814e52af513SShlomo Pongratz default:
8155dcf0d3aSPhilippe Mathieu-Daudé return false;
816e52af513SShlomo Pongratz }
817e52af513SShlomo Pongratz }
818e52af513SShlomo Pongratz
gicd_writeq(GICv3State * s,hwaddr offset,uint64_t value,MemTxAttrs attrs)8195dcf0d3aSPhilippe Mathieu-Daudé static bool gicd_writeq(GICv3State *s, hwaddr offset,
820e52af513SShlomo Pongratz uint64_t value, MemTxAttrs attrs)
821e52af513SShlomo Pongratz {
822e52af513SShlomo Pongratz /* Our only 64-bit registers are GICD_IROUTER<n> */
823e52af513SShlomo Pongratz int irq;
824e52af513SShlomo Pongratz
825e52af513SShlomo Pongratz switch (offset) {
826e52af513SShlomo Pongratz case GICD_IROUTER ... GICD_IROUTER + 0x1fdf:
827e52af513SShlomo Pongratz irq = (offset - GICD_IROUTER) / 8;
828e52af513SShlomo Pongratz gicd_write_irouter(s, attrs, irq, value);
8295dcf0d3aSPhilippe Mathieu-Daudé return true;
830e52af513SShlomo Pongratz default:
8315dcf0d3aSPhilippe Mathieu-Daudé return false;
832e52af513SShlomo Pongratz }
833e52af513SShlomo Pongratz }
834e52af513SShlomo Pongratz
gicd_readq(GICv3State * s,hwaddr offset,uint64_t * data,MemTxAttrs attrs)8355dcf0d3aSPhilippe Mathieu-Daudé static bool gicd_readq(GICv3State *s, hwaddr offset,
836e52af513SShlomo Pongratz uint64_t *data, MemTxAttrs attrs)
837e52af513SShlomo Pongratz {
838e52af513SShlomo Pongratz /* Our only 64-bit registers are GICD_IROUTER<n> */
839e52af513SShlomo Pongratz int irq;
840e52af513SShlomo Pongratz
841e52af513SShlomo Pongratz switch (offset) {
842e52af513SShlomo Pongratz case GICD_IROUTER ... GICD_IROUTER + 0x1fdf:
843e52af513SShlomo Pongratz irq = (offset - GICD_IROUTER) / 8;
844e52af513SShlomo Pongratz *data = gicd_read_irouter(s, attrs, irq);
8455dcf0d3aSPhilippe Mathieu-Daudé return true;
846e52af513SShlomo Pongratz default:
8475dcf0d3aSPhilippe Mathieu-Daudé return false;
848e52af513SShlomo Pongratz }
849e52af513SShlomo Pongratz }
850e52af513SShlomo Pongratz
gicv3_dist_read(void * opaque,hwaddr offset,uint64_t * data,unsigned size,MemTxAttrs attrs)851e52af513SShlomo Pongratz MemTxResult gicv3_dist_read(void *opaque, hwaddr offset, uint64_t *data,
852e52af513SShlomo Pongratz unsigned size, MemTxAttrs attrs)
853e52af513SShlomo Pongratz {
854e52af513SShlomo Pongratz GICv3State *s = (GICv3State *)opaque;
8555dcf0d3aSPhilippe Mathieu-Daudé bool r;
856e52af513SShlomo Pongratz
857e52af513SShlomo Pongratz switch (size) {
858e52af513SShlomo Pongratz case 1:
859e52af513SShlomo Pongratz r = gicd_readb(s, offset, data, attrs);
860e52af513SShlomo Pongratz break;
861e52af513SShlomo Pongratz case 2:
862e52af513SShlomo Pongratz r = gicd_readw(s, offset, data, attrs);
863e52af513SShlomo Pongratz break;
864e52af513SShlomo Pongratz case 4:
865e52af513SShlomo Pongratz r = gicd_readl(s, offset, data, attrs);
866e52af513SShlomo Pongratz break;
867e52af513SShlomo Pongratz case 8:
868c0bb7d61SPhilippe Mathieu-Daudé r = gicd_readq(s, offset, data, attrs);
869e52af513SShlomo Pongratz break;
870e52af513SShlomo Pongratz default:
8715dcf0d3aSPhilippe Mathieu-Daudé r = false;
872e52af513SShlomo Pongratz break;
873e52af513SShlomo Pongratz }
874e52af513SShlomo Pongratz
8755dcf0d3aSPhilippe Mathieu-Daudé if (!r) {
876e52af513SShlomo Pongratz qemu_log_mask(LOG_GUEST_ERROR,
877883f2c59SPhilippe Mathieu-Daudé "%s: invalid guest read at offset " HWADDR_FMT_plx
878e52af513SShlomo Pongratz " size %u\n", __func__, offset, size);
879e52af513SShlomo Pongratz trace_gicv3_dist_badread(offset, size, attrs.secure);
880f1945632SPeter Maydell /* The spec requires that reserved registers are RAZ/WI;
881f1945632SPeter Maydell * so use MEMTX_ERROR returns from leaf functions as a way to
882f1945632SPeter Maydell * trigger the guest-error logging but don't return it to
883f1945632SPeter Maydell * the caller, or we'll cause a spurious guest data abort.
884f1945632SPeter Maydell */
885f1945632SPeter Maydell *data = 0;
886e52af513SShlomo Pongratz } else {
887e52af513SShlomo Pongratz trace_gicv3_dist_read(offset, *data, size, attrs.secure);
888e52af513SShlomo Pongratz }
8895dcf0d3aSPhilippe Mathieu-Daudé return MEMTX_OK;
890e52af513SShlomo Pongratz }
891e52af513SShlomo Pongratz
gicv3_dist_write(void * opaque,hwaddr offset,uint64_t data,unsigned size,MemTxAttrs attrs)892e52af513SShlomo Pongratz MemTxResult gicv3_dist_write(void *opaque, hwaddr offset, uint64_t data,
893e52af513SShlomo Pongratz unsigned size, MemTxAttrs attrs)
894e52af513SShlomo Pongratz {
895e52af513SShlomo Pongratz GICv3State *s = (GICv3State *)opaque;
8965dcf0d3aSPhilippe Mathieu-Daudé bool r;
897e52af513SShlomo Pongratz
898e52af513SShlomo Pongratz switch (size) {
899e52af513SShlomo Pongratz case 1:
900e52af513SShlomo Pongratz r = gicd_writeb(s, offset, data, attrs);
901e52af513SShlomo Pongratz break;
902e52af513SShlomo Pongratz case 2:
903e52af513SShlomo Pongratz r = gicd_writew(s, offset, data, attrs);
904e52af513SShlomo Pongratz break;
905e52af513SShlomo Pongratz case 4:
906e52af513SShlomo Pongratz r = gicd_writel(s, offset, data, attrs);
907e52af513SShlomo Pongratz break;
908e52af513SShlomo Pongratz case 8:
909c0bb7d61SPhilippe Mathieu-Daudé r = gicd_writeq(s, offset, data, attrs);
910e52af513SShlomo Pongratz break;
911e52af513SShlomo Pongratz default:
9125dcf0d3aSPhilippe Mathieu-Daudé r = false;
913e52af513SShlomo Pongratz break;
914e52af513SShlomo Pongratz }
915e52af513SShlomo Pongratz
9165dcf0d3aSPhilippe Mathieu-Daudé if (!r) {
917e52af513SShlomo Pongratz qemu_log_mask(LOG_GUEST_ERROR,
918883f2c59SPhilippe Mathieu-Daudé "%s: invalid guest write at offset " HWADDR_FMT_plx
919e52af513SShlomo Pongratz " size %u\n", __func__, offset, size);
920e52af513SShlomo Pongratz trace_gicv3_dist_badwrite(offset, data, size, attrs.secure);
921f1945632SPeter Maydell /* The spec requires that reserved registers are RAZ/WI;
922f1945632SPeter Maydell * so use MEMTX_ERROR returns from leaf functions as a way to
923f1945632SPeter Maydell * trigger the guest-error logging but don't return it to
924f1945632SPeter Maydell * the caller, or we'll cause a spurious guest data abort.
925f1945632SPeter Maydell */
926e52af513SShlomo Pongratz } else {
927e52af513SShlomo Pongratz trace_gicv3_dist_write(offset, data, size, attrs.secure);
928e52af513SShlomo Pongratz }
9295dcf0d3aSPhilippe Mathieu-Daudé return MEMTX_OK;
930e52af513SShlomo Pongratz }
931c84428b3SPeter Maydell
gicv3_dist_set_irq(GICv3State * s,int irq,int level)932c84428b3SPeter Maydell void gicv3_dist_set_irq(GICv3State *s, int irq, int level)
933c84428b3SPeter Maydell {
934c84428b3SPeter Maydell /* Update distributor state for a change in an external SPI input line */
935c84428b3SPeter Maydell if (level == gicv3_gicd_level_test(s, irq)) {
936c84428b3SPeter Maydell return;
937c84428b3SPeter Maydell }
938c84428b3SPeter Maydell
939c84428b3SPeter Maydell trace_gicv3_dist_set_irq(irq, level);
940c84428b3SPeter Maydell
941c84428b3SPeter Maydell gicv3_gicd_level_replace(s, irq, level);
942c84428b3SPeter Maydell
943c84428b3SPeter Maydell if (level) {
944c84428b3SPeter Maydell /* 0->1 edges latch the pending bit for edge-triggered interrupts */
945c84428b3SPeter Maydell if (gicv3_gicd_edge_trigger_test(s, irq)) {
946c84428b3SPeter Maydell gicv3_gicd_pending_set(s, irq);
947c84428b3SPeter Maydell }
948c84428b3SPeter Maydell }
949c84428b3SPeter Maydell
950c84428b3SPeter Maydell gicv3_update(s, irq, 1);
951c84428b3SPeter Maydell }
952