xref: /qemu/hw/intc/arm_gicv3_its.c (revision 0241f7316073f5e66b560195c36e719e369947d0)
118f6290aSShashi Mallela /*
218f6290aSShashi Mallela  * ITS emulation for a GICv3-based system
318f6290aSShashi Mallela  *
418f6290aSShashi Mallela  * Copyright Linaro.org 2021
518f6290aSShashi Mallela  *
618f6290aSShashi Mallela  * Authors:
718f6290aSShashi Mallela  *  Shashi Mallela <shashi.mallela@linaro.org>
818f6290aSShashi Mallela  *
918f6290aSShashi Mallela  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
1018f6290aSShashi Mallela  * option) any later version.  See the COPYING file in the top-level directory.
1118f6290aSShashi Mallela  *
1218f6290aSShashi Mallela  */
1318f6290aSShashi Mallela 
1418f6290aSShashi Mallela #include "qemu/osdep.h"
1518f6290aSShashi Mallela #include "qemu/log.h"
1618f6290aSShashi Mallela #include "hw/qdev-properties.h"
1718f6290aSShashi Mallela #include "hw/intc/arm_gicv3_its_common.h"
1818f6290aSShashi Mallela #include "gicv3_internal.h"
1918f6290aSShashi Mallela #include "qom/object.h"
2018f6290aSShashi Mallela #include "qapi/error.h"
2118f6290aSShashi Mallela 
2218f6290aSShashi Mallela typedef struct GICv3ITSClass GICv3ITSClass;
2318f6290aSShashi Mallela /* This is reusing the GICv3ITSState typedef from ARM_GICV3_ITS_COMMON */
2418f6290aSShashi Mallela DECLARE_OBJ_CHECKERS(GICv3ITSState, GICv3ITSClass,
2518f6290aSShashi Mallela                      ARM_GICV3_ITS, TYPE_ARM_GICV3_ITS)
2618f6290aSShashi Mallela 
2718f6290aSShashi Mallela struct GICv3ITSClass {
2818f6290aSShashi Mallela     GICv3ITSCommonClass parent_class;
2918f6290aSShashi Mallela     void (*parent_reset)(DeviceState *dev);
3018f6290aSShashi Mallela };
3118f6290aSShashi Mallela 
32c694cb4cSShashi Mallela /*
33c694cb4cSShashi Mallela  * This is an internal enum used to distinguish between LPI triggered
34c694cb4cSShashi Mallela  * via command queue and LPI triggered via gits_translater write.
35c694cb4cSShashi Mallela  */
36c694cb4cSShashi Mallela typedef enum ItsCmdType {
37c694cb4cSShashi Mallela     NONE = 0, /* internal indication for GITS_TRANSLATER write */
38c694cb4cSShashi Mallela     CLEAR = 1,
39c694cb4cSShashi Mallela     DISCARD = 2,
40c694cb4cSShashi Mallela     INTERRUPT = 3,
41c694cb4cSShashi Mallela } ItsCmdType;
42c694cb4cSShashi Mallela 
43c694cb4cSShashi Mallela typedef struct {
44c694cb4cSShashi Mallela     uint32_t iteh;
45c694cb4cSShashi Mallela     uint64_t itel;
46c694cb4cSShashi Mallela } IteEntry;
47c694cb4cSShashi Mallela 
48ef011555SPeter Maydell /*
49ef011555SPeter Maydell  * The ITS spec permits a range of CONSTRAINED UNPREDICTABLE options
50ef011555SPeter Maydell  * if a command parameter is not correct. These include both "stall
51ef011555SPeter Maydell  * processing of the command queue" and "ignore this command, and
52ef011555SPeter Maydell  * keep processing the queue". In our implementation we choose that
53ef011555SPeter Maydell  * memory transaction errors reading the command packet provoke a
54ef011555SPeter Maydell  * stall, but errors in parameters cause us to ignore the command
55ef011555SPeter Maydell  * and continue processing.
56ef011555SPeter Maydell  * The process_* functions which handle individual ITS commands all
57ef011555SPeter Maydell  * return an ItsCmdResult which tells process_cmdq() whether it should
58ef011555SPeter Maydell  * stall or keep going.
59ef011555SPeter Maydell  */
60ef011555SPeter Maydell typedef enum ItsCmdResult {
61ef011555SPeter Maydell     CMD_STALL = 0,
62ef011555SPeter Maydell     CMD_CONTINUE = 1,
63ef011555SPeter Maydell } ItsCmdResult;
64ef011555SPeter Maydell 
651b08e436SShashi Mallela static uint64_t baser_base_addr(uint64_t value, uint32_t page_sz)
661b08e436SShashi Mallela {
671b08e436SShashi Mallela     uint64_t result = 0;
681b08e436SShashi Mallela 
691b08e436SShashi Mallela     switch (page_sz) {
701b08e436SShashi Mallela     case GITS_PAGE_SIZE_4K:
711b08e436SShashi Mallela     case GITS_PAGE_SIZE_16K:
721b08e436SShashi Mallela         result = FIELD_EX64(value, GITS_BASER, PHYADDR) << 12;
731b08e436SShashi Mallela         break;
741b08e436SShashi Mallela 
751b08e436SShashi Mallela     case GITS_PAGE_SIZE_64K:
761b08e436SShashi Mallela         result = FIELD_EX64(value, GITS_BASER, PHYADDRL_64K) << 16;
771b08e436SShashi Mallela         result |= FIELD_EX64(value, GITS_BASER, PHYADDRH_64K) << 48;
781b08e436SShashi Mallela         break;
791b08e436SShashi Mallela 
801b08e436SShashi Mallela     default:
811b08e436SShashi Mallela         break;
821b08e436SShashi Mallela     }
831b08e436SShashi Mallela     return result;
841b08e436SShashi Mallela }
851b08e436SShashi Mallela 
86c694cb4cSShashi Mallela static bool get_cte(GICv3ITSState *s, uint16_t icid, uint64_t *cte,
87c694cb4cSShashi Mallela                     MemTxResult *res)
88c694cb4cSShashi Mallela {
89c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
90c694cb4cSShashi Mallela     uint64_t l2t_addr;
91c694cb4cSShashi Mallela     uint64_t value;
92c694cb4cSShashi Mallela     bool valid_l2t;
93c694cb4cSShashi Mallela     uint32_t l2t_id;
947f18ac3aSPeter Maydell     uint32_t num_l2_entries;
95c694cb4cSShashi Mallela 
96c694cb4cSShashi Mallela     if (s->ct.indirect) {
97c694cb4cSShashi Mallela         l2t_id = icid / (s->ct.page_sz / L1TABLE_ENTRY_SIZE);
98c694cb4cSShashi Mallela 
99c694cb4cSShashi Mallela         value = address_space_ldq_le(as,
100c694cb4cSShashi Mallela                                      s->ct.base_addr +
101c694cb4cSShashi Mallela                                      (l2t_id * L1TABLE_ENTRY_SIZE),
102c694cb4cSShashi Mallela                                      MEMTXATTRS_UNSPECIFIED, res);
103c694cb4cSShashi Mallela 
104c694cb4cSShashi Mallela         if (*res == MEMTX_OK) {
105c694cb4cSShashi Mallela             valid_l2t = (value & L2_TABLE_VALID_MASK) != 0;
106c694cb4cSShashi Mallela 
107c694cb4cSShashi Mallela             if (valid_l2t) {
1087f18ac3aSPeter Maydell                 num_l2_entries = s->ct.page_sz / s->ct.entry_sz;
109c694cb4cSShashi Mallela 
110c694cb4cSShashi Mallela                 l2t_addr = value & ((1ULL << 51) - 1);
111c694cb4cSShashi Mallela 
112c694cb4cSShashi Mallela                 *cte =  address_space_ldq_le(as, l2t_addr +
1137f18ac3aSPeter Maydell                                     ((icid % num_l2_entries) * GITS_CTE_SIZE),
114c694cb4cSShashi Mallela                                     MEMTXATTRS_UNSPECIFIED, res);
115c694cb4cSShashi Mallela            }
116c694cb4cSShashi Mallela        }
117c694cb4cSShashi Mallela     } else {
118c694cb4cSShashi Mallela         /* Flat level table */
119c694cb4cSShashi Mallela         *cte =  address_space_ldq_le(as, s->ct.base_addr +
120c694cb4cSShashi Mallela                                      (icid * GITS_CTE_SIZE),
121c694cb4cSShashi Mallela                                       MEMTXATTRS_UNSPECIFIED, res);
122c694cb4cSShashi Mallela     }
123c694cb4cSShashi Mallela 
124437dc0eaSPeter Maydell     return FIELD_EX64(*cte, CTE, VALID);
125c694cb4cSShashi Mallela }
126c694cb4cSShashi Mallela 
127c694cb4cSShashi Mallela static bool update_ite(GICv3ITSState *s, uint32_t eventid, uint64_t dte,
128c694cb4cSShashi Mallela                        IteEntry ite)
129c694cb4cSShashi Mallela {
130c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
131c694cb4cSShashi Mallela     uint64_t itt_addr;
132c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
133c694cb4cSShashi Mallela 
134e07f8445SPeter Maydell     itt_addr = FIELD_EX64(dte, DTE, ITTADDR);
135c694cb4cSShashi Mallela     itt_addr <<= ITTADDR_SHIFT; /* 256 byte aligned */
136c694cb4cSShashi Mallela 
137c694cb4cSShashi Mallela     address_space_stq_le(as, itt_addr + (eventid * (sizeof(uint64_t) +
138c694cb4cSShashi Mallela                          sizeof(uint32_t))), ite.itel, MEMTXATTRS_UNSPECIFIED,
139c694cb4cSShashi Mallela                          &res);
140c694cb4cSShashi Mallela 
141c694cb4cSShashi Mallela     if (res == MEMTX_OK) {
142c694cb4cSShashi Mallela         address_space_stl_le(as, itt_addr + (eventid * (sizeof(uint64_t) +
143c694cb4cSShashi Mallela                              sizeof(uint32_t))) + sizeof(uint32_t), ite.iteh,
144c694cb4cSShashi Mallela                              MEMTXATTRS_UNSPECIFIED, &res);
145c694cb4cSShashi Mallela     }
146c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
147c694cb4cSShashi Mallela         return false;
148c694cb4cSShashi Mallela     } else {
149c694cb4cSShashi Mallela         return true;
150c694cb4cSShashi Mallela     }
151c694cb4cSShashi Mallela }
152c694cb4cSShashi Mallela 
153c694cb4cSShashi Mallela static bool get_ite(GICv3ITSState *s, uint32_t eventid, uint64_t dte,
154c694cb4cSShashi Mallela                     uint16_t *icid, uint32_t *pIntid, MemTxResult *res)
155c694cb4cSShashi Mallela {
156c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
157c694cb4cSShashi Mallela     uint64_t itt_addr;
158c694cb4cSShashi Mallela     bool status = false;
159c694cb4cSShashi Mallela     IteEntry ite = {};
160c694cb4cSShashi Mallela 
161e07f8445SPeter Maydell     itt_addr = FIELD_EX64(dte, DTE, ITTADDR);
162c694cb4cSShashi Mallela     itt_addr <<= ITTADDR_SHIFT; /* 256 byte aligned */
163c694cb4cSShashi Mallela 
164c694cb4cSShashi Mallela     ite.itel = address_space_ldq_le(as, itt_addr +
165c694cb4cSShashi Mallela                                     (eventid * (sizeof(uint64_t) +
166c694cb4cSShashi Mallela                                     sizeof(uint32_t))), MEMTXATTRS_UNSPECIFIED,
167c694cb4cSShashi Mallela                                     res);
168c694cb4cSShashi Mallela 
169c694cb4cSShashi Mallela     if (*res == MEMTX_OK) {
170c694cb4cSShashi Mallela         ite.iteh = address_space_ldl_le(as, itt_addr +
171c694cb4cSShashi Mallela                                         (eventid * (sizeof(uint64_t) +
172c694cb4cSShashi Mallela                                         sizeof(uint32_t))) + sizeof(uint32_t),
173c694cb4cSShashi Mallela                                         MEMTXATTRS_UNSPECIFIED, res);
174c694cb4cSShashi Mallela 
175c694cb4cSShashi Mallela         if (*res == MEMTX_OK) {
176764d6ba1SPeter Maydell             if (FIELD_EX64(ite.itel, ITE_L, VALID)) {
177764d6ba1SPeter Maydell                 int inttype = FIELD_EX64(ite.itel, ITE_L, INTTYPE);
178764d6ba1SPeter Maydell                 if (inttype == ITE_INTTYPE_PHYSICAL) {
179764d6ba1SPeter Maydell                     *pIntid = FIELD_EX64(ite.itel, ITE_L, INTID);
180764d6ba1SPeter Maydell                     *icid = FIELD_EX32(ite.iteh, ITE_H, ICID);
181c694cb4cSShashi Mallela                     status = true;
182c694cb4cSShashi Mallela                 }
183c694cb4cSShashi Mallela             }
184c694cb4cSShashi Mallela         }
185c694cb4cSShashi Mallela     }
186c694cb4cSShashi Mallela     return status;
187c694cb4cSShashi Mallela }
188c694cb4cSShashi Mallela 
189c694cb4cSShashi Mallela static uint64_t get_dte(GICv3ITSState *s, uint32_t devid, MemTxResult *res)
190c694cb4cSShashi Mallela {
191c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
192c694cb4cSShashi Mallela     uint64_t l2t_addr;
193c694cb4cSShashi Mallela     uint64_t value;
194c694cb4cSShashi Mallela     bool valid_l2t;
195c694cb4cSShashi Mallela     uint32_t l2t_id;
1967f18ac3aSPeter Maydell     uint32_t num_l2_entries;
197c694cb4cSShashi Mallela 
198c694cb4cSShashi Mallela     if (s->dt.indirect) {
199c694cb4cSShashi Mallela         l2t_id = devid / (s->dt.page_sz / L1TABLE_ENTRY_SIZE);
200c694cb4cSShashi Mallela 
201c694cb4cSShashi Mallela         value = address_space_ldq_le(as,
202c694cb4cSShashi Mallela                                      s->dt.base_addr +
203c694cb4cSShashi Mallela                                      (l2t_id * L1TABLE_ENTRY_SIZE),
204c694cb4cSShashi Mallela                                      MEMTXATTRS_UNSPECIFIED, res);
205c694cb4cSShashi Mallela 
206c694cb4cSShashi Mallela         if (*res == MEMTX_OK) {
207c694cb4cSShashi Mallela             valid_l2t = (value & L2_TABLE_VALID_MASK) != 0;
208c694cb4cSShashi Mallela 
209c694cb4cSShashi Mallela             if (valid_l2t) {
2107f18ac3aSPeter Maydell                 num_l2_entries = s->dt.page_sz / s->dt.entry_sz;
211c694cb4cSShashi Mallela 
212c694cb4cSShashi Mallela                 l2t_addr = value & ((1ULL << 51) - 1);
213c694cb4cSShashi Mallela 
214c694cb4cSShashi Mallela                 value =  address_space_ldq_le(as, l2t_addr +
2157f18ac3aSPeter Maydell                                    ((devid % num_l2_entries) * GITS_DTE_SIZE),
216c694cb4cSShashi Mallela                                    MEMTXATTRS_UNSPECIFIED, res);
217c694cb4cSShashi Mallela             }
218c694cb4cSShashi Mallela         }
219c694cb4cSShashi Mallela     } else {
220c694cb4cSShashi Mallela         /* Flat level table */
221c694cb4cSShashi Mallela         value = address_space_ldq_le(as, s->dt.base_addr +
222c694cb4cSShashi Mallela                                      (devid * GITS_DTE_SIZE),
223c694cb4cSShashi Mallela                                      MEMTXATTRS_UNSPECIFIED, res);
224c694cb4cSShashi Mallela     }
225c694cb4cSShashi Mallela 
226c694cb4cSShashi Mallela     return value;
227c694cb4cSShashi Mallela }
228c694cb4cSShashi Mallela 
229c694cb4cSShashi Mallela /*
230c694cb4cSShashi Mallela  * This function handles the processing of following commands based on
231c694cb4cSShashi Mallela  * the ItsCmdType parameter passed:-
232c694cb4cSShashi Mallela  * 1. triggering of lpi interrupt translation via ITS INT command
233c694cb4cSShashi Mallela  * 2. triggering of lpi interrupt translation via gits_translater register
234c694cb4cSShashi Mallela  * 3. handling of ITS CLEAR command
235c694cb4cSShashi Mallela  * 4. handling of ITS DISCARD command
236c694cb4cSShashi Mallela  */
237ef011555SPeter Maydell static ItsCmdResult process_its_cmd(GICv3ITSState *s, uint64_t value,
238ef011555SPeter Maydell                                     uint32_t offset, ItsCmdType cmd)
239c694cb4cSShashi Mallela {
240c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
241c694cb4cSShashi Mallela     uint32_t devid, eventid;
242c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
243c694cb4cSShashi Mallela     bool dte_valid;
244c694cb4cSShashi Mallela     uint64_t dte = 0;
2458f809f69SPeter Maydell     uint64_t num_eventids;
246c694cb4cSShashi Mallela     uint16_t icid = 0;
247c694cb4cSShashi Mallela     uint32_t pIntid = 0;
248c694cb4cSShashi Mallela     bool ite_valid = false;
249c694cb4cSShashi Mallela     uint64_t cte = 0;
250c694cb4cSShashi Mallela     bool cte_valid = false;
25117fb5e36SShashi Mallela     uint64_t rdbase;
252c694cb4cSShashi Mallela 
253c694cb4cSShashi Mallela     if (cmd == NONE) {
254c694cb4cSShashi Mallela         devid = offset;
255c694cb4cSShashi Mallela     } else {
256c694cb4cSShashi Mallela         devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
257c694cb4cSShashi Mallela 
258c694cb4cSShashi Mallela         offset += NUM_BYTES_IN_DW;
259c694cb4cSShashi Mallela         value = address_space_ldq_le(as, s->cq.base_addr + offset,
260c694cb4cSShashi Mallela                                      MEMTXATTRS_UNSPECIFIED, &res);
261c694cb4cSShashi Mallela     }
262c694cb4cSShashi Mallela 
263c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
264593a7cc2SPeter Maydell         return CMD_STALL;
265c694cb4cSShashi Mallela     }
266c694cb4cSShashi Mallela 
267c694cb4cSShashi Mallela     eventid = (value & EVENTID_MASK);
268c694cb4cSShashi Mallela 
269c694cb4cSShashi Mallela     dte = get_dte(s, devid, &res);
270c694cb4cSShashi Mallela 
271c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
272593a7cc2SPeter Maydell         return CMD_STALL;
273c694cb4cSShashi Mallela     }
274e07f8445SPeter Maydell     dte_valid = FIELD_EX64(dte, DTE, VALID);
275c694cb4cSShashi Mallela 
276be0ed8fbSPeter Maydell     if (!dte_valid) {
277229c57b1SAlex Bennée         qemu_log_mask(LOG_GUEST_ERROR,
278229c57b1SAlex Bennée                       "%s: invalid command attributes: "
279be0ed8fbSPeter Maydell                       "invalid dte: %"PRIx64" for %d\n",
280be0ed8fbSPeter Maydell                       __func__, dte, devid);
281593a7cc2SPeter Maydell         return CMD_CONTINUE;
282c694cb4cSShashi Mallela     }
283c694cb4cSShashi Mallela 
284be0ed8fbSPeter Maydell     num_eventids = 1ULL << (FIELD_EX64(dte, DTE, SIZE) + 1);
285229c57b1SAlex Bennée 
286be0ed8fbSPeter Maydell     ite_valid = get_ite(s, eventid, dte, &icid, &pIntid, &res);
287be0ed8fbSPeter Maydell     if (res != MEMTX_OK) {
288be0ed8fbSPeter Maydell         return CMD_STALL;
289be0ed8fbSPeter Maydell     }
290be0ed8fbSPeter Maydell 
291be0ed8fbSPeter Maydell     if (!ite_valid) {
292be0ed8fbSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
293be0ed8fbSPeter Maydell                       "%s: invalid command attributes: invalid ITE\n",
294be0ed8fbSPeter Maydell                       __func__);
295be0ed8fbSPeter Maydell         return CMD_CONTINUE;
296be0ed8fbSPeter Maydell     }
297be0ed8fbSPeter Maydell 
298be0ed8fbSPeter Maydell     cte_valid = get_cte(s, icid, &cte, &res);
299be0ed8fbSPeter Maydell     if (res != MEMTX_OK) {
300be0ed8fbSPeter Maydell         return CMD_STALL;
301be0ed8fbSPeter Maydell     }
302be0ed8fbSPeter Maydell     if (!cte_valid) {
303be0ed8fbSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
304be0ed8fbSPeter Maydell                       "%s: invalid command attributes: "
305be0ed8fbSPeter Maydell                       "invalid cte: %"PRIx64"\n",
306be0ed8fbSPeter Maydell                       __func__, cte);
307be0ed8fbSPeter Maydell         return CMD_CONTINUE;
308be0ed8fbSPeter Maydell     }
309be0ed8fbSPeter Maydell 
31080dcd37fSPeter Maydell     if (devid >= s->dt.num_ids) {
311229c57b1SAlex Bennée         qemu_log_mask(LOG_GUEST_ERROR,
31280dcd37fSPeter Maydell                       "%s: invalid command attributes: devid %d>=%d",
31380dcd37fSPeter Maydell                       __func__, devid, s->dt.num_ids);
314593a7cc2SPeter Maydell         return CMD_CONTINUE;
315be0ed8fbSPeter Maydell     }
316be0ed8fbSPeter Maydell     if (eventid >= num_eventids) {
317229c57b1SAlex Bennée         qemu_log_mask(LOG_GUEST_ERROR,
3188f809f69SPeter Maydell                       "%s: invalid command attributes: eventid %d >= %"
3198f809f69SPeter Maydell                       PRId64 "\n",
3208f809f69SPeter Maydell                       __func__, eventid, num_eventids);
321593a7cc2SPeter Maydell         return CMD_CONTINUE;
322be0ed8fbSPeter Maydell     }
323be0ed8fbSPeter Maydell 
324c694cb4cSShashi Mallela     /*
325c694cb4cSShashi Mallela      * Current implementation only supports rdbase == procnum
326c694cb4cSShashi Mallela      * Hence rdbase physical address is ignored
327c694cb4cSShashi Mallela      */
328437dc0eaSPeter Maydell     rdbase = FIELD_EX64(cte, CTE, RDBASE);
32917fb5e36SShashi Mallela 
330a120157bSPeter Maydell     if (rdbase >= s->gicv3->num_cpu) {
331593a7cc2SPeter Maydell         return CMD_CONTINUE;
33217fb5e36SShashi Mallela     }
33317fb5e36SShashi Mallela 
33417fb5e36SShashi Mallela     if ((cmd == CLEAR) || (cmd == DISCARD)) {
33517fb5e36SShashi Mallela         gicv3_redist_process_lpi(&s->gicv3->cpu[rdbase], pIntid, 0);
33617fb5e36SShashi Mallela     } else {
33717fb5e36SShashi Mallela         gicv3_redist_process_lpi(&s->gicv3->cpu[rdbase], pIntid, 1);
33817fb5e36SShashi Mallela     }
33917fb5e36SShashi Mallela 
340c694cb4cSShashi Mallela     if (cmd == DISCARD) {
341c694cb4cSShashi Mallela         IteEntry ite = {};
342c694cb4cSShashi Mallela         /* remove mapping from interrupt translation table */
343593a7cc2SPeter Maydell         return update_ite(s, eventid, dte, ite) ? CMD_CONTINUE : CMD_STALL;
344c694cb4cSShashi Mallela     }
345593a7cc2SPeter Maydell     return CMD_CONTINUE;
346c694cb4cSShashi Mallela }
347c694cb4cSShashi Mallela 
348ef011555SPeter Maydell static ItsCmdResult process_mapti(GICv3ITSState *s, uint64_t value,
349ef011555SPeter Maydell                                   uint32_t offset, bool ignore_pInt)
350c694cb4cSShashi Mallela {
351c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
352c694cb4cSShashi Mallela     uint32_t devid, eventid;
353c694cb4cSShashi Mallela     uint32_t pIntid = 0;
3548f809f69SPeter Maydell     uint64_t num_eventids;
355905720f1SPeter Maydell     uint32_t num_intids;
356c694cb4cSShashi Mallela     bool dte_valid;
357c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
358c694cb4cSShashi Mallela     uint16_t icid = 0;
359c694cb4cSShashi Mallela     uint64_t dte = 0;
360*0241f731SPeter Maydell     IteEntry ite = {};
361c694cb4cSShashi Mallela 
362c694cb4cSShashi Mallela     devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
363c694cb4cSShashi Mallela     offset += NUM_BYTES_IN_DW;
364c694cb4cSShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
365c694cb4cSShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
366c694cb4cSShashi Mallela 
367c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
368*0241f731SPeter Maydell         return CMD_STALL;
369c694cb4cSShashi Mallela     }
370c694cb4cSShashi Mallela 
371c694cb4cSShashi Mallela     eventid = (value & EVENTID_MASK);
372c694cb4cSShashi Mallela 
373b87fab1cSPeter Maydell     if (ignore_pInt) {
374b87fab1cSPeter Maydell         pIntid = eventid;
375b87fab1cSPeter Maydell     } else {
376c694cb4cSShashi Mallela         pIntid = ((value & pINTID_MASK) >> pINTID_SHIFT);
377c694cb4cSShashi Mallela     }
378c694cb4cSShashi Mallela 
379c694cb4cSShashi Mallela     offset += NUM_BYTES_IN_DW;
380c694cb4cSShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
381c694cb4cSShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
382c694cb4cSShashi Mallela 
383c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
384*0241f731SPeter Maydell         return CMD_STALL;
385c694cb4cSShashi Mallela     }
386c694cb4cSShashi Mallela 
387c694cb4cSShashi Mallela     icid = value & ICID_MASK;
388c694cb4cSShashi Mallela 
389c694cb4cSShashi Mallela     dte = get_dte(s, devid, &res);
390c694cb4cSShashi Mallela 
391c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
392*0241f731SPeter Maydell         return CMD_STALL;
393c694cb4cSShashi Mallela     }
394e07f8445SPeter Maydell     dte_valid = FIELD_EX64(dte, DTE, VALID);
3958f809f69SPeter Maydell     num_eventids = 1ULL << (FIELD_EX64(dte, DTE, SIZE) + 1);
396905720f1SPeter Maydell     num_intids = 1ULL << (GICD_TYPER_IDBITS + 1);
397c694cb4cSShashi Mallela 
39880dcd37fSPeter Maydell     if ((devid >= s->dt.num_ids) || (icid >= s->ct.num_ids)
3998f809f69SPeter Maydell             || !dte_valid || (eventid >= num_eventids) ||
400905720f1SPeter Maydell             (((pIntid < GICV3_LPI_INTID_START) || (pIntid >= num_intids)) &&
401b87fab1cSPeter Maydell              (pIntid != INTID_SPURIOUS))) {
402c694cb4cSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
403c694cb4cSShashi Mallela                       "%s: invalid command attributes "
404c694cb4cSShashi Mallela                       "devid %d or icid %d or eventid %d or pIntid %d or"
405c694cb4cSShashi Mallela                       "unmapped dte %d\n", __func__, devid, icid, eventid,
406c694cb4cSShashi Mallela                       pIntid, dte_valid);
407c694cb4cSShashi Mallela         /*
408c694cb4cSShashi Mallela          * in this implementation, in case of error
409c694cb4cSShashi Mallela          * we ignore this command and move onto the next
410c694cb4cSShashi Mallela          * command in the queue
411c694cb4cSShashi Mallela          */
412*0241f731SPeter Maydell         return CMD_CONTINUE;
413*0241f731SPeter Maydell     }
414*0241f731SPeter Maydell 
415c694cb4cSShashi Mallela     /* add ite entry to interrupt translation table */
416764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, VALID, dte_valid);
417764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, INTTYPE, ITE_INTTYPE_PHYSICAL);
418764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, INTID, pIntid);
419764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, DOORBELL, INTID_SPURIOUS);
420764d6ba1SPeter Maydell     ite.iteh = FIELD_DP32(ite.iteh, ITE_H, ICID, icid);
421c694cb4cSShashi Mallela 
422*0241f731SPeter Maydell     return update_ite(s, eventid, dte, ite) ? CMD_CONTINUE : CMD_STALL;
423c694cb4cSShashi Mallela }
424c694cb4cSShashi Mallela 
4257eca39e0SShashi Mallela static bool update_cte(GICv3ITSState *s, uint16_t icid, bool valid,
4267eca39e0SShashi Mallela                        uint64_t rdbase)
4277eca39e0SShashi Mallela {
4287eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
4297eca39e0SShashi Mallela     uint64_t value;
4307eca39e0SShashi Mallela     uint64_t l2t_addr;
4317eca39e0SShashi Mallela     bool valid_l2t;
4327eca39e0SShashi Mallela     uint32_t l2t_id;
4337f18ac3aSPeter Maydell     uint32_t num_l2_entries;
4347eca39e0SShashi Mallela     uint64_t cte = 0;
4357eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
4367eca39e0SShashi Mallela 
4377eca39e0SShashi Mallela     if (!s->ct.valid) {
4387eca39e0SShashi Mallela         return true;
4397eca39e0SShashi Mallela     }
4407eca39e0SShashi Mallela 
4417eca39e0SShashi Mallela     if (valid) {
4427eca39e0SShashi Mallela         /* add mapping entry to collection table */
443437dc0eaSPeter Maydell         cte = FIELD_DP64(cte, CTE, VALID, 1);
444437dc0eaSPeter Maydell         cte = FIELD_DP64(cte, CTE, RDBASE, rdbase);
4457eca39e0SShashi Mallela     }
4467eca39e0SShashi Mallela 
4477eca39e0SShashi Mallela     /*
4487eca39e0SShashi Mallela      * The specification defines the format of level 1 entries of a
4497eca39e0SShashi Mallela      * 2-level table, but the format of level 2 entries and the format
4507eca39e0SShashi Mallela      * of flat-mapped tables is IMPDEF.
4517eca39e0SShashi Mallela      */
4527eca39e0SShashi Mallela     if (s->ct.indirect) {
4537eca39e0SShashi Mallela         l2t_id = icid / (s->ct.page_sz / L1TABLE_ENTRY_SIZE);
4547eca39e0SShashi Mallela 
4557eca39e0SShashi Mallela         value = address_space_ldq_le(as,
4567eca39e0SShashi Mallela                                      s->ct.base_addr +
4577eca39e0SShashi Mallela                                      (l2t_id * L1TABLE_ENTRY_SIZE),
4587eca39e0SShashi Mallela                                      MEMTXATTRS_UNSPECIFIED, &res);
4597eca39e0SShashi Mallela 
4607eca39e0SShashi Mallela         if (res != MEMTX_OK) {
4617eca39e0SShashi Mallela             return false;
4627eca39e0SShashi Mallela         }
4637eca39e0SShashi Mallela 
4647eca39e0SShashi Mallela         valid_l2t = (value & L2_TABLE_VALID_MASK) != 0;
4657eca39e0SShashi Mallela 
4667eca39e0SShashi Mallela         if (valid_l2t) {
4677f18ac3aSPeter Maydell             num_l2_entries = s->ct.page_sz / s->ct.entry_sz;
4687eca39e0SShashi Mallela 
4697eca39e0SShashi Mallela             l2t_addr = value & ((1ULL << 51) - 1);
4707eca39e0SShashi Mallela 
4717eca39e0SShashi Mallela             address_space_stq_le(as, l2t_addr +
4727f18ac3aSPeter Maydell                                  ((icid % num_l2_entries) * GITS_CTE_SIZE),
4737eca39e0SShashi Mallela                                  cte, MEMTXATTRS_UNSPECIFIED, &res);
4747eca39e0SShashi Mallela         }
4757eca39e0SShashi Mallela     } else {
4767eca39e0SShashi Mallela         /* Flat level table */
4777eca39e0SShashi Mallela         address_space_stq_le(as, s->ct.base_addr + (icid * GITS_CTE_SIZE),
4787eca39e0SShashi Mallela                              cte, MEMTXATTRS_UNSPECIFIED, &res);
4797eca39e0SShashi Mallela     }
4807eca39e0SShashi Mallela     if (res != MEMTX_OK) {
4817eca39e0SShashi Mallela         return false;
4827eca39e0SShashi Mallela     } else {
4837eca39e0SShashi Mallela         return true;
4847eca39e0SShashi Mallela     }
4857eca39e0SShashi Mallela }
4867eca39e0SShashi Mallela 
487ef011555SPeter Maydell static ItsCmdResult process_mapc(GICv3ITSState *s, uint32_t offset)
4887eca39e0SShashi Mallela {
4897eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
4907eca39e0SShashi Mallela     uint16_t icid;
4917eca39e0SShashi Mallela     uint64_t rdbase;
4927eca39e0SShashi Mallela     bool valid;
4937eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
494ef011555SPeter Maydell     ItsCmdResult result = CMD_STALL;
4957eca39e0SShashi Mallela     uint64_t value;
4967eca39e0SShashi Mallela 
4977eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
4987eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
4997eca39e0SShashi Mallela 
5007eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
5017eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
5027eca39e0SShashi Mallela 
5037eca39e0SShashi Mallela     if (res != MEMTX_OK) {
5047eca39e0SShashi Mallela         return result;
5057eca39e0SShashi Mallela     }
5067eca39e0SShashi Mallela 
5077eca39e0SShashi Mallela     icid = value & ICID_MASK;
5087eca39e0SShashi Mallela 
5097eca39e0SShashi Mallela     rdbase = (value & R_MAPC_RDBASE_MASK) >> R_MAPC_RDBASE_SHIFT;
5107eca39e0SShashi Mallela     rdbase &= RDBASE_PROCNUM_MASK;
5117eca39e0SShashi Mallela 
5127eca39e0SShashi Mallela     valid = (value & CMD_FIELD_VALID_MASK);
5137eca39e0SShashi Mallela 
51480dcd37fSPeter Maydell     if ((icid >= s->ct.num_ids) || (rdbase >= s->gicv3->num_cpu)) {
5157eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
5167eca39e0SShashi Mallela                       "ITS MAPC: invalid collection table attributes "
5177eca39e0SShashi Mallela                       "icid %d rdbase %" PRIu64 "\n",  icid, rdbase);
5187eca39e0SShashi Mallela         /*
5197eca39e0SShashi Mallela          * in this implementation, in case of error
5207eca39e0SShashi Mallela          * we ignore this command and move onto the next
5217eca39e0SShashi Mallela          * command in the queue
5227eca39e0SShashi Mallela          */
5237eca39e0SShashi Mallela     } else {
524ef011555SPeter Maydell         result = update_cte(s, icid, valid, rdbase) ? CMD_CONTINUE : CMD_STALL;
5257eca39e0SShashi Mallela     }
5267eca39e0SShashi Mallela 
5277eca39e0SShashi Mallela     return result;
5287eca39e0SShashi Mallela }
5297eca39e0SShashi Mallela 
5307eca39e0SShashi Mallela static bool update_dte(GICv3ITSState *s, uint32_t devid, bool valid,
5317eca39e0SShashi Mallela                        uint8_t size, uint64_t itt_addr)
5327eca39e0SShashi Mallela {
5337eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
5347eca39e0SShashi Mallela     uint64_t value;
5357eca39e0SShashi Mallela     uint64_t l2t_addr;
5367eca39e0SShashi Mallela     bool valid_l2t;
5377eca39e0SShashi Mallela     uint32_t l2t_id;
5387f18ac3aSPeter Maydell     uint32_t num_l2_entries;
5397eca39e0SShashi Mallela     uint64_t dte = 0;
5407eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
5417eca39e0SShashi Mallela 
5427eca39e0SShashi Mallela     if (s->dt.valid) {
5437eca39e0SShashi Mallela         if (valid) {
5447eca39e0SShashi Mallela             /* add mapping entry to device table */
545e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, VALID, 1);
546e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, SIZE, size);
547e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, ITTADDR, itt_addr);
5487eca39e0SShashi Mallela         }
5497eca39e0SShashi Mallela     } else {
5507eca39e0SShashi Mallela         return true;
5517eca39e0SShashi Mallela     }
5527eca39e0SShashi Mallela 
5537eca39e0SShashi Mallela     /*
5547eca39e0SShashi Mallela      * The specification defines the format of level 1 entries of a
5557eca39e0SShashi Mallela      * 2-level table, but the format of level 2 entries and the format
5567eca39e0SShashi Mallela      * of flat-mapped tables is IMPDEF.
5577eca39e0SShashi Mallela      */
5587eca39e0SShashi Mallela     if (s->dt.indirect) {
5597eca39e0SShashi Mallela         l2t_id = devid / (s->dt.page_sz / L1TABLE_ENTRY_SIZE);
5607eca39e0SShashi Mallela 
5617eca39e0SShashi Mallela         value = address_space_ldq_le(as,
5627eca39e0SShashi Mallela                                      s->dt.base_addr +
5637eca39e0SShashi Mallela                                      (l2t_id * L1TABLE_ENTRY_SIZE),
5647eca39e0SShashi Mallela                                      MEMTXATTRS_UNSPECIFIED, &res);
5657eca39e0SShashi Mallela 
5667eca39e0SShashi Mallela         if (res != MEMTX_OK) {
5677eca39e0SShashi Mallela             return false;
5687eca39e0SShashi Mallela         }
5697eca39e0SShashi Mallela 
5707eca39e0SShashi Mallela         valid_l2t = (value & L2_TABLE_VALID_MASK) != 0;
5717eca39e0SShashi Mallela 
5727eca39e0SShashi Mallela         if (valid_l2t) {
5737f18ac3aSPeter Maydell             num_l2_entries = s->dt.page_sz / s->dt.entry_sz;
5747eca39e0SShashi Mallela 
5757eca39e0SShashi Mallela             l2t_addr = value & ((1ULL << 51) - 1);
5767eca39e0SShashi Mallela 
5777eca39e0SShashi Mallela             address_space_stq_le(as, l2t_addr +
5787f18ac3aSPeter Maydell                                  ((devid % num_l2_entries) * GITS_DTE_SIZE),
5797eca39e0SShashi Mallela                                  dte, MEMTXATTRS_UNSPECIFIED, &res);
5807eca39e0SShashi Mallela         }
5817eca39e0SShashi Mallela     } else {
5827eca39e0SShashi Mallela         /* Flat level table */
5837eca39e0SShashi Mallela         address_space_stq_le(as, s->dt.base_addr + (devid * GITS_DTE_SIZE),
5847eca39e0SShashi Mallela                              dte, MEMTXATTRS_UNSPECIFIED, &res);
5857eca39e0SShashi Mallela     }
5867eca39e0SShashi Mallela     if (res != MEMTX_OK) {
5877eca39e0SShashi Mallela         return false;
5887eca39e0SShashi Mallela     } else {
5897eca39e0SShashi Mallela         return true;
5907eca39e0SShashi Mallela     }
5917eca39e0SShashi Mallela }
5927eca39e0SShashi Mallela 
593ef011555SPeter Maydell static ItsCmdResult process_mapd(GICv3ITSState *s, uint64_t value,
594ef011555SPeter Maydell                                  uint32_t offset)
5957eca39e0SShashi Mallela {
5967eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
5977eca39e0SShashi Mallela     uint32_t devid;
5987eca39e0SShashi Mallela     uint8_t size;
5997eca39e0SShashi Mallela     uint64_t itt_addr;
6007eca39e0SShashi Mallela     bool valid;
6017eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
602ef011555SPeter Maydell     ItsCmdResult result = CMD_STALL;
6037eca39e0SShashi Mallela 
6047eca39e0SShashi Mallela     devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
6057eca39e0SShashi Mallela 
6067eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
6077eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
6087eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
6097eca39e0SShashi Mallela 
6107eca39e0SShashi Mallela     if (res != MEMTX_OK) {
6117eca39e0SShashi Mallela         return result;
6127eca39e0SShashi Mallela     }
6137eca39e0SShashi Mallela 
6147eca39e0SShashi Mallela     size = (value & SIZE_MASK);
6157eca39e0SShashi Mallela 
6167eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
6177eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
6187eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
6197eca39e0SShashi Mallela 
6207eca39e0SShashi Mallela     if (res != MEMTX_OK) {
6217eca39e0SShashi Mallela         return result;
6227eca39e0SShashi Mallela     }
6237eca39e0SShashi Mallela 
6247eca39e0SShashi Mallela     itt_addr = (value & ITTADDR_MASK) >> ITTADDR_SHIFT;
6257eca39e0SShashi Mallela 
6267eca39e0SShashi Mallela     valid = (value & CMD_FIELD_VALID_MASK);
6277eca39e0SShashi Mallela 
62880dcd37fSPeter Maydell     if ((devid >= s->dt.num_ids) ||
6297eca39e0SShashi Mallela         (size > FIELD_EX64(s->typer, GITS_TYPER, IDBITS))) {
6307eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
6317eca39e0SShashi Mallela                       "ITS MAPD: invalid device table attributes "
6327eca39e0SShashi Mallela                       "devid %d or size %d\n", devid, size);
6337eca39e0SShashi Mallela         /*
6347eca39e0SShashi Mallela          * in this implementation, in case of error
6357eca39e0SShashi Mallela          * we ignore this command and move onto the next
6367eca39e0SShashi Mallela          * command in the queue
6377eca39e0SShashi Mallela          */
6387eca39e0SShashi Mallela     } else {
639ef011555SPeter Maydell         result = update_dte(s, devid, valid, size, itt_addr) ? CMD_CONTINUE : CMD_STALL;
6407eca39e0SShashi Mallela     }
6417eca39e0SShashi Mallela 
6427eca39e0SShashi Mallela     return result;
6437eca39e0SShashi Mallela }
6447eca39e0SShashi Mallela 
6457eca39e0SShashi Mallela /*
6467eca39e0SShashi Mallela  * Current implementation blocks until all
6477eca39e0SShashi Mallela  * commands are processed
6487eca39e0SShashi Mallela  */
6497eca39e0SShashi Mallela static void process_cmdq(GICv3ITSState *s)
6507eca39e0SShashi Mallela {
6517eca39e0SShashi Mallela     uint32_t wr_offset = 0;
6527eca39e0SShashi Mallela     uint32_t rd_offset = 0;
6537eca39e0SShashi Mallela     uint32_t cq_offset = 0;
6547eca39e0SShashi Mallela     uint64_t data;
6557eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
6567eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
6577eca39e0SShashi Mallela     uint8_t cmd;
65817fb5e36SShashi Mallela     int i;
6597eca39e0SShashi Mallela 
6608d2d6dd9SPeter Maydell     if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
6617eca39e0SShashi Mallela         return;
6627eca39e0SShashi Mallela     }
6637eca39e0SShashi Mallela 
6647eca39e0SShashi Mallela     wr_offset = FIELD_EX64(s->cwriter, GITS_CWRITER, OFFSET);
6657eca39e0SShashi Mallela 
66680dcd37fSPeter Maydell     if (wr_offset >= s->cq.num_entries) {
6677eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
6687eca39e0SShashi Mallela                       "%s: invalid write offset "
6697eca39e0SShashi Mallela                       "%d\n", __func__, wr_offset);
6707eca39e0SShashi Mallela         return;
6717eca39e0SShashi Mallela     }
6727eca39e0SShashi Mallela 
6737eca39e0SShashi Mallela     rd_offset = FIELD_EX64(s->creadr, GITS_CREADR, OFFSET);
6747eca39e0SShashi Mallela 
67580dcd37fSPeter Maydell     if (rd_offset >= s->cq.num_entries) {
6767eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
6777eca39e0SShashi Mallela                       "%s: invalid read offset "
6787eca39e0SShashi Mallela                       "%d\n", __func__, rd_offset);
6797eca39e0SShashi Mallela         return;
6807eca39e0SShashi Mallela     }
6817eca39e0SShashi Mallela 
6827eca39e0SShashi Mallela     while (wr_offset != rd_offset) {
683ef011555SPeter Maydell         ItsCmdResult result = CMD_CONTINUE;
684ef011555SPeter Maydell 
6857eca39e0SShashi Mallela         cq_offset = (rd_offset * GITS_CMDQ_ENTRY_SIZE);
6867eca39e0SShashi Mallela         data = address_space_ldq_le(as, s->cq.base_addr + cq_offset,
6877eca39e0SShashi Mallela                                     MEMTXATTRS_UNSPECIFIED, &res);
6887eca39e0SShashi Mallela         if (res != MEMTX_OK) {
689f0b4b2a2SPeter Maydell             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1);
690f0b4b2a2SPeter Maydell             qemu_log_mask(LOG_GUEST_ERROR,
691f0b4b2a2SPeter Maydell                           "%s: could not read command at 0x%" PRIx64 "\n",
692f0b4b2a2SPeter Maydell                           __func__, s->cq.base_addr + cq_offset);
693f0b4b2a2SPeter Maydell             break;
6947eca39e0SShashi Mallela         }
695f0b4b2a2SPeter Maydell 
6967eca39e0SShashi Mallela         cmd = (data & CMD_MASK);
6977eca39e0SShashi Mallela 
6987eca39e0SShashi Mallela         switch (cmd) {
6997eca39e0SShashi Mallela         case GITS_CMD_INT:
7007d62b2dcSPeter Maydell             result = process_its_cmd(s, data, cq_offset, INTERRUPT);
7017eca39e0SShashi Mallela             break;
7027eca39e0SShashi Mallela         case GITS_CMD_CLEAR:
7037d62b2dcSPeter Maydell             result = process_its_cmd(s, data, cq_offset, CLEAR);
7047eca39e0SShashi Mallela             break;
7057eca39e0SShashi Mallela         case GITS_CMD_SYNC:
7067eca39e0SShashi Mallela             /*
7077eca39e0SShashi Mallela              * Current implementation makes a blocking synchronous call
7087eca39e0SShashi Mallela              * for every command issued earlier, hence the internal state
7097eca39e0SShashi Mallela              * is already consistent by the time SYNC command is executed.
7107eca39e0SShashi Mallela              * Hence no further processing is required for SYNC command.
7117eca39e0SShashi Mallela              */
7127eca39e0SShashi Mallela             break;
7137eca39e0SShashi Mallela         case GITS_CMD_MAPD:
7147eca39e0SShashi Mallela             result = process_mapd(s, data, cq_offset);
7157eca39e0SShashi Mallela             break;
7167eca39e0SShashi Mallela         case GITS_CMD_MAPC:
7177eca39e0SShashi Mallela             result = process_mapc(s, cq_offset);
7187eca39e0SShashi Mallela             break;
7197eca39e0SShashi Mallela         case GITS_CMD_MAPTI:
720c694cb4cSShashi Mallela             result = process_mapti(s, data, cq_offset, false);
7217eca39e0SShashi Mallela             break;
7227eca39e0SShashi Mallela         case GITS_CMD_MAPI:
723c694cb4cSShashi Mallela             result = process_mapti(s, data, cq_offset, true);
7247eca39e0SShashi Mallela             break;
7257eca39e0SShashi Mallela         case GITS_CMD_DISCARD:
726c694cb4cSShashi Mallela             result = process_its_cmd(s, data, cq_offset, DISCARD);
7277eca39e0SShashi Mallela             break;
7287eca39e0SShashi Mallela         case GITS_CMD_INV:
7297eca39e0SShashi Mallela         case GITS_CMD_INVALL:
73017fb5e36SShashi Mallela             /*
73117fb5e36SShashi Mallela              * Current implementation doesn't cache any ITS tables,
73217fb5e36SShashi Mallela              * but the calculated lpi priority information. We only
73317fb5e36SShashi Mallela              * need to trigger lpi priority re-calculation to be in
73417fb5e36SShashi Mallela              * sync with LPI config table or pending table changes.
73517fb5e36SShashi Mallela              */
73617fb5e36SShashi Mallela             for (i = 0; i < s->gicv3->num_cpu; i++) {
73717fb5e36SShashi Mallela                 gicv3_redist_update_lpi(&s->gicv3->cpu[i]);
73817fb5e36SShashi Mallela             }
7397eca39e0SShashi Mallela             break;
7407eca39e0SShashi Mallela         default:
7417eca39e0SShashi Mallela             break;
7427eca39e0SShashi Mallela         }
743ef011555SPeter Maydell         if (result == CMD_CONTINUE) {
7447eca39e0SShashi Mallela             rd_offset++;
74580dcd37fSPeter Maydell             rd_offset %= s->cq.num_entries;
7467eca39e0SShashi Mallela             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, OFFSET, rd_offset);
7477eca39e0SShashi Mallela         } else {
748ef011555SPeter Maydell             /* CMD_STALL */
7497eca39e0SShashi Mallela             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1);
7507eca39e0SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
751ef011555SPeter Maydell                           "%s: 0x%x cmd processing failed, stalling\n",
752ef011555SPeter Maydell                           __func__, cmd);
7537eca39e0SShashi Mallela             break;
7547eca39e0SShashi Mallela         }
7557eca39e0SShashi Mallela     }
7567eca39e0SShashi Mallela }
7577eca39e0SShashi Mallela 
7581b08e436SShashi Mallela /*
7591b08e436SShashi Mallela  * This function extracts the ITS Device and Collection table specific
7601b08e436SShashi Mallela  * parameters (like base_addr, size etc) from GITS_BASER register.
7611b08e436SShashi Mallela  * It is called during ITS enable and also during post_load migration
7621b08e436SShashi Mallela  */
7631b08e436SShashi Mallela static void extract_table_params(GICv3ITSState *s)
7641b08e436SShashi Mallela {
7651b08e436SShashi Mallela     uint16_t num_pages = 0;
7661b08e436SShashi Mallela     uint8_t  page_sz_type;
7671b08e436SShashi Mallela     uint8_t type;
7681b08e436SShashi Mallela     uint32_t page_sz = 0;
7691b08e436SShashi Mallela     uint64_t value;
7701b08e436SShashi Mallela 
7711b08e436SShashi Mallela     for (int i = 0; i < 8; i++) {
772e5487a41SPeter Maydell         TableDesc *td;
773e5487a41SPeter Maydell         int idbits;
774e5487a41SPeter Maydell 
7751b08e436SShashi Mallela         value = s->baser[i];
7761b08e436SShashi Mallela 
7771b08e436SShashi Mallela         if (!value) {
7781b08e436SShashi Mallela             continue;
7791b08e436SShashi Mallela         }
7801b08e436SShashi Mallela 
7811b08e436SShashi Mallela         page_sz_type = FIELD_EX64(value, GITS_BASER, PAGESIZE);
7821b08e436SShashi Mallela 
7831b08e436SShashi Mallela         switch (page_sz_type) {
7841b08e436SShashi Mallela         case 0:
7851b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_4K;
7861b08e436SShashi Mallela             break;
7871b08e436SShashi Mallela 
7881b08e436SShashi Mallela         case 1:
7891b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_16K;
7901b08e436SShashi Mallela             break;
7911b08e436SShashi Mallela 
7921b08e436SShashi Mallela         case 2:
7931b08e436SShashi Mallela         case 3:
7941b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_64K;
7951b08e436SShashi Mallela             break;
7961b08e436SShashi Mallela 
7971b08e436SShashi Mallela         default:
7981b08e436SShashi Mallela             g_assert_not_reached();
7991b08e436SShashi Mallela         }
8001b08e436SShashi Mallela 
8011b08e436SShashi Mallela         num_pages = FIELD_EX64(value, GITS_BASER, SIZE) + 1;
8021b08e436SShashi Mallela 
8031b08e436SShashi Mallela         type = FIELD_EX64(value, GITS_BASER, TYPE);
8041b08e436SShashi Mallela 
8051b08e436SShashi Mallela         switch (type) {
8061b08e436SShashi Mallela         case GITS_BASER_TYPE_DEVICE:
807e5487a41SPeter Maydell             td = &s->dt;
808e5487a41SPeter Maydell             idbits = FIELD_EX64(s->typer, GITS_TYPER, DEVBITS) + 1;
80962df780eSPeter Maydell             break;
8101b08e436SShashi Mallela         case GITS_BASER_TYPE_COLLECTION:
811e5487a41SPeter Maydell             td = &s->ct;
8121b08e436SShashi Mallela             if (FIELD_EX64(s->typer, GITS_TYPER, CIL)) {
813e5487a41SPeter Maydell                 idbits = FIELD_EX64(s->typer, GITS_TYPER, CIDBITS) + 1;
8141b08e436SShashi Mallela             } else {
8151b08e436SShashi Mallela                 /* 16-bit CollectionId supported when CIL == 0 */
816e5487a41SPeter Maydell                 idbits = 16;
8171b08e436SShashi Mallela             }
8181b08e436SShashi Mallela             break;
8191b08e436SShashi Mallela         default:
820e5487a41SPeter Maydell             /*
821e5487a41SPeter Maydell              * GITS_BASER<n>.TYPE is read-only, so GITS_BASER_RO_MASK
822e5487a41SPeter Maydell              * ensures we will only see type values corresponding to
823e5487a41SPeter Maydell              * the values set up in gicv3_its_reset().
824e5487a41SPeter Maydell              */
825e5487a41SPeter Maydell             g_assert_not_reached();
8261b08e436SShashi Mallela         }
827e5487a41SPeter Maydell 
828e5487a41SPeter Maydell         memset(td, 0, sizeof(*td));
829e5487a41SPeter Maydell         td->valid = FIELD_EX64(value, GITS_BASER, VALID);
830e5487a41SPeter Maydell         /*
831e5487a41SPeter Maydell          * If GITS_BASER<n>.Valid is 0 for any <n> then we will not process
832e5487a41SPeter Maydell          * interrupts. (GITS_TYPER.HCC is 0 for this implementation, so we
833e5487a41SPeter Maydell          * do not have a special case where the GITS_BASER<n>.Valid bit is 0
834e5487a41SPeter Maydell          * for the register corresponding to the Collection table but we
835e5487a41SPeter Maydell          * still have to process interrupts using non-memory-backed
836e5487a41SPeter Maydell          * Collection table entries.)
837e5487a41SPeter Maydell          */
838e5487a41SPeter Maydell         if (!td->valid) {
839e5487a41SPeter Maydell             continue;
840e5487a41SPeter Maydell         }
841e5487a41SPeter Maydell         td->page_sz = page_sz;
842e5487a41SPeter Maydell         td->indirect = FIELD_EX64(value, GITS_BASER, INDIRECT);
8439ae85431SPeter Maydell         td->entry_sz = FIELD_EX64(value, GITS_BASER, ENTRYSIZE) + 1;
844e5487a41SPeter Maydell         td->base_addr = baser_base_addr(value, page_sz);
845e5487a41SPeter Maydell         if (!td->indirect) {
84680dcd37fSPeter Maydell             td->num_entries = (num_pages * page_sz) / td->entry_sz;
847e5487a41SPeter Maydell         } else {
84880dcd37fSPeter Maydell             td->num_entries = (((num_pages * page_sz) /
849e5487a41SPeter Maydell                                   L1TABLE_ENTRY_SIZE) *
850e5487a41SPeter Maydell                                  (page_sz / td->entry_sz));
851e5487a41SPeter Maydell         }
85280dcd37fSPeter Maydell         td->num_ids = 1ULL << idbits;
8531b08e436SShashi Mallela     }
8541b08e436SShashi Mallela }
8551b08e436SShashi Mallela 
8561b08e436SShashi Mallela static void extract_cmdq_params(GICv3ITSState *s)
8571b08e436SShashi Mallela {
8581b08e436SShashi Mallela     uint16_t num_pages = 0;
8591b08e436SShashi Mallela     uint64_t value = s->cbaser;
8601b08e436SShashi Mallela 
8611b08e436SShashi Mallela     num_pages = FIELD_EX64(value, GITS_CBASER, SIZE) + 1;
8621b08e436SShashi Mallela 
8631b08e436SShashi Mallela     memset(&s->cq, 0 , sizeof(s->cq));
8641b08e436SShashi Mallela     s->cq.valid = FIELD_EX64(value, GITS_CBASER, VALID);
8651b08e436SShashi Mallela 
8661b08e436SShashi Mallela     if (s->cq.valid) {
86780dcd37fSPeter Maydell         s->cq.num_entries = (num_pages * GITS_PAGE_SIZE_4K) /
8681b08e436SShashi Mallela                              GITS_CMDQ_ENTRY_SIZE;
8691b08e436SShashi Mallela         s->cq.base_addr = FIELD_EX64(value, GITS_CBASER, PHYADDR);
8701b08e436SShashi Mallela         s->cq.base_addr <<= R_GITS_CBASER_PHYADDR_SHIFT;
8711b08e436SShashi Mallela     }
8721b08e436SShashi Mallela }
8731b08e436SShashi Mallela 
87418f6290aSShashi Mallela static MemTxResult gicv3_its_translation_write(void *opaque, hwaddr offset,
87518f6290aSShashi Mallela                                                uint64_t data, unsigned size,
87618f6290aSShashi Mallela                                                MemTxAttrs attrs)
87718f6290aSShashi Mallela {
878c694cb4cSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
879c694cb4cSShashi Mallela     bool result = true;
880c694cb4cSShashi Mallela     uint32_t devid = 0;
881c694cb4cSShashi Mallela 
882c694cb4cSShashi Mallela     switch (offset) {
883c694cb4cSShashi Mallela     case GITS_TRANSLATER:
8848d2d6dd9SPeter Maydell         if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) {
885c694cb4cSShashi Mallela             devid = attrs.requester_id;
886c694cb4cSShashi Mallela             result = process_its_cmd(s, data, devid, NONE);
887c694cb4cSShashi Mallela         }
888c694cb4cSShashi Mallela         break;
889c694cb4cSShashi Mallela     default:
890c694cb4cSShashi Mallela         break;
891c694cb4cSShashi Mallela     }
892c694cb4cSShashi Mallela 
893c694cb4cSShashi Mallela     if (result) {
89418f6290aSShashi Mallela         return MEMTX_OK;
895c694cb4cSShashi Mallela     } else {
896c694cb4cSShashi Mallela         return MEMTX_ERROR;
897c694cb4cSShashi Mallela     }
89818f6290aSShashi Mallela }
89918f6290aSShashi Mallela 
90018f6290aSShashi Mallela static bool its_writel(GICv3ITSState *s, hwaddr offset,
90118f6290aSShashi Mallela                               uint64_t value, MemTxAttrs attrs)
90218f6290aSShashi Mallela {
90318f6290aSShashi Mallela     bool result = true;
9041b08e436SShashi Mallela     int index;
90518f6290aSShashi Mallela 
9061b08e436SShashi Mallela     switch (offset) {
9071b08e436SShashi Mallela     case GITS_CTLR:
9082f459cd1SShashi Mallela         if (value & R_GITS_CTLR_ENABLED_MASK) {
9098d2d6dd9SPeter Maydell             s->ctlr |= R_GITS_CTLR_ENABLED_MASK;
9101b08e436SShashi Mallela             extract_table_params(s);
9111b08e436SShashi Mallela             extract_cmdq_params(s);
9121b08e436SShashi Mallela             s->creadr = 0;
9137eca39e0SShashi Mallela             process_cmdq(s);
9142f459cd1SShashi Mallela         } else {
9158d2d6dd9SPeter Maydell             s->ctlr &= ~R_GITS_CTLR_ENABLED_MASK;
9161b08e436SShashi Mallela         }
9171b08e436SShashi Mallela         break;
9181b08e436SShashi Mallela     case GITS_CBASER:
9191b08e436SShashi Mallela         /*
9201b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
9211b08e436SShashi Mallela          *                 already enabled
9221b08e436SShashi Mallela          */
9238d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
9241b08e436SShashi Mallela             s->cbaser = deposit64(s->cbaser, 0, 32, value);
9251b08e436SShashi Mallela             s->creadr = 0;
9261b08e436SShashi Mallela             s->cwriter = s->creadr;
9271b08e436SShashi Mallela         }
9281b08e436SShashi Mallela         break;
9291b08e436SShashi Mallela     case GITS_CBASER + 4:
9301b08e436SShashi Mallela         /*
9311b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
9321b08e436SShashi Mallela          *                 already enabled
9331b08e436SShashi Mallela          */
9348d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
9351b08e436SShashi Mallela             s->cbaser = deposit64(s->cbaser, 32, 32, value);
9361b08e436SShashi Mallela             s->creadr = 0;
9371b08e436SShashi Mallela             s->cwriter = s->creadr;
9381b08e436SShashi Mallela         }
9391b08e436SShashi Mallela         break;
9401b08e436SShashi Mallela     case GITS_CWRITER:
9411b08e436SShashi Mallela         s->cwriter = deposit64(s->cwriter, 0, 32,
9421b08e436SShashi Mallela                                (value & ~R_GITS_CWRITER_RETRY_MASK));
9437eca39e0SShashi Mallela         if (s->cwriter != s->creadr) {
9447eca39e0SShashi Mallela             process_cmdq(s);
9457eca39e0SShashi Mallela         }
9461b08e436SShashi Mallela         break;
9471b08e436SShashi Mallela     case GITS_CWRITER + 4:
9481b08e436SShashi Mallela         s->cwriter = deposit64(s->cwriter, 32, 32, value);
9491b08e436SShashi Mallela         break;
9501b08e436SShashi Mallela     case GITS_CREADR:
9511b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
9521b08e436SShashi Mallela             s->creadr = deposit64(s->creadr, 0, 32,
9531b08e436SShashi Mallela                                   (value & ~R_GITS_CREADR_STALLED_MASK));
9541b08e436SShashi Mallela         } else {
9551b08e436SShashi Mallela             /* RO register, ignore the write */
9561b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
9571b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
9581b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
9591b08e436SShashi Mallela         }
9601b08e436SShashi Mallela         break;
9611b08e436SShashi Mallela     case GITS_CREADR + 4:
9621b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
9631b08e436SShashi Mallela             s->creadr = deposit64(s->creadr, 32, 32, value);
9641b08e436SShashi Mallela         } else {
9651b08e436SShashi Mallela             /* RO register, ignore the write */
9661b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
9671b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
9681b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
9691b08e436SShashi Mallela         }
9701b08e436SShashi Mallela         break;
9711b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
9721b08e436SShashi Mallela         /*
9731b08e436SShashi Mallela          * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is
9741b08e436SShashi Mallela          *                 already enabled
9751b08e436SShashi Mallela          */
9768d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
9771b08e436SShashi Mallela             index = (offset - GITS_BASER) / 8;
9781b08e436SShashi Mallela 
9791b08e436SShashi Mallela             if (offset & 7) {
9801b08e436SShashi Mallela                 value <<= 32;
9811b08e436SShashi Mallela                 value &= ~GITS_BASER_RO_MASK;
9821b08e436SShashi Mallela                 s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(0, 32);
9831b08e436SShashi Mallela                 s->baser[index] |= value;
9841b08e436SShashi Mallela             } else {
9851b08e436SShashi Mallela                 value &= ~GITS_BASER_RO_MASK;
9861b08e436SShashi Mallela                 s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(32, 32);
9871b08e436SShashi Mallela                 s->baser[index] |= value;
9881b08e436SShashi Mallela             }
9891b08e436SShashi Mallela         }
9901b08e436SShashi Mallela         break;
9911b08e436SShashi Mallela     case GITS_IIDR:
9921b08e436SShashi Mallela     case GITS_IDREGS ... GITS_IDREGS + 0x2f:
9931b08e436SShashi Mallela         /* RO registers, ignore the write */
9941b08e436SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
9951b08e436SShashi Mallela                       "%s: invalid guest write to RO register at offset "
9961b08e436SShashi Mallela                       TARGET_FMT_plx "\n", __func__, offset);
9971b08e436SShashi Mallela         break;
9981b08e436SShashi Mallela     default:
9991b08e436SShashi Mallela         result = false;
10001b08e436SShashi Mallela         break;
10011b08e436SShashi Mallela     }
100218f6290aSShashi Mallela     return result;
100318f6290aSShashi Mallela }
100418f6290aSShashi Mallela 
100518f6290aSShashi Mallela static bool its_readl(GICv3ITSState *s, hwaddr offset,
100618f6290aSShashi Mallela                              uint64_t *data, MemTxAttrs attrs)
100718f6290aSShashi Mallela {
100818f6290aSShashi Mallela     bool result = true;
10091b08e436SShashi Mallela     int index;
101018f6290aSShashi Mallela 
10111b08e436SShashi Mallela     switch (offset) {
10121b08e436SShashi Mallela     case GITS_CTLR:
10131b08e436SShashi Mallela         *data = s->ctlr;
10141b08e436SShashi Mallela         break;
10151b08e436SShashi Mallela     case GITS_IIDR:
10161b08e436SShashi Mallela         *data = gicv3_iidr();
10171b08e436SShashi Mallela         break;
10181b08e436SShashi Mallela     case GITS_IDREGS ... GITS_IDREGS + 0x2f:
10191b08e436SShashi Mallela         /* ID registers */
10201b08e436SShashi Mallela         *data = gicv3_idreg(offset - GITS_IDREGS);
10211b08e436SShashi Mallela         break;
10221b08e436SShashi Mallela     case GITS_TYPER:
10231b08e436SShashi Mallela         *data = extract64(s->typer, 0, 32);
10241b08e436SShashi Mallela         break;
10251b08e436SShashi Mallela     case GITS_TYPER + 4:
10261b08e436SShashi Mallela         *data = extract64(s->typer, 32, 32);
10271b08e436SShashi Mallela         break;
10281b08e436SShashi Mallela     case GITS_CBASER:
10291b08e436SShashi Mallela         *data = extract64(s->cbaser, 0, 32);
10301b08e436SShashi Mallela         break;
10311b08e436SShashi Mallela     case GITS_CBASER + 4:
10321b08e436SShashi Mallela         *data = extract64(s->cbaser, 32, 32);
10331b08e436SShashi Mallela         break;
10341b08e436SShashi Mallela     case GITS_CREADR:
10351b08e436SShashi Mallela         *data = extract64(s->creadr, 0, 32);
10361b08e436SShashi Mallela         break;
10371b08e436SShashi Mallela     case GITS_CREADR + 4:
10381b08e436SShashi Mallela         *data = extract64(s->creadr, 32, 32);
10391b08e436SShashi Mallela         break;
10401b08e436SShashi Mallela     case GITS_CWRITER:
10411b08e436SShashi Mallela         *data = extract64(s->cwriter, 0, 32);
10421b08e436SShashi Mallela         break;
10431b08e436SShashi Mallela     case GITS_CWRITER + 4:
10441b08e436SShashi Mallela         *data = extract64(s->cwriter, 32, 32);
10451b08e436SShashi Mallela         break;
10461b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
10471b08e436SShashi Mallela         index = (offset - GITS_BASER) / 8;
10481b08e436SShashi Mallela         if (offset & 7) {
10491b08e436SShashi Mallela             *data = extract64(s->baser[index], 32, 32);
10501b08e436SShashi Mallela         } else {
10511b08e436SShashi Mallela             *data = extract64(s->baser[index], 0, 32);
10521b08e436SShashi Mallela         }
10531b08e436SShashi Mallela         break;
10541b08e436SShashi Mallela     default:
10551b08e436SShashi Mallela         result = false;
10561b08e436SShashi Mallela         break;
10571b08e436SShashi Mallela     }
105818f6290aSShashi Mallela     return result;
105918f6290aSShashi Mallela }
106018f6290aSShashi Mallela 
106118f6290aSShashi Mallela static bool its_writell(GICv3ITSState *s, hwaddr offset,
106218f6290aSShashi Mallela                                uint64_t value, MemTxAttrs attrs)
106318f6290aSShashi Mallela {
106418f6290aSShashi Mallela     bool result = true;
10651b08e436SShashi Mallela     int index;
106618f6290aSShashi Mallela 
10671b08e436SShashi Mallela     switch (offset) {
10681b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
10691b08e436SShashi Mallela         /*
10701b08e436SShashi Mallela          * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is
10711b08e436SShashi Mallela          *                 already enabled
10721b08e436SShashi Mallela          */
10738d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
10741b08e436SShashi Mallela             index = (offset - GITS_BASER) / 8;
10751b08e436SShashi Mallela             s->baser[index] &= GITS_BASER_RO_MASK;
10761b08e436SShashi Mallela             s->baser[index] |= (value & ~GITS_BASER_RO_MASK);
10771b08e436SShashi Mallela         }
10781b08e436SShashi Mallela         break;
10791b08e436SShashi Mallela     case GITS_CBASER:
10801b08e436SShashi Mallela         /*
10811b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
10821b08e436SShashi Mallela          *                 already enabled
10831b08e436SShashi Mallela          */
10848d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
10851b08e436SShashi Mallela             s->cbaser = value;
10861b08e436SShashi Mallela             s->creadr = 0;
10871b08e436SShashi Mallela             s->cwriter = s->creadr;
10881b08e436SShashi Mallela         }
10891b08e436SShashi Mallela         break;
10901b08e436SShashi Mallela     case GITS_CWRITER:
10911b08e436SShashi Mallela         s->cwriter = value & ~R_GITS_CWRITER_RETRY_MASK;
10927eca39e0SShashi Mallela         if (s->cwriter != s->creadr) {
10937eca39e0SShashi Mallela             process_cmdq(s);
10947eca39e0SShashi Mallela         }
10951b08e436SShashi Mallela         break;
10961b08e436SShashi Mallela     case GITS_CREADR:
10971b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
10981b08e436SShashi Mallela             s->creadr = value & ~R_GITS_CREADR_STALLED_MASK;
10991b08e436SShashi Mallela         } else {
11001b08e436SShashi Mallela             /* RO register, ignore the write */
11011b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
11021b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
11031b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
11041b08e436SShashi Mallela         }
11051b08e436SShashi Mallela         break;
11061b08e436SShashi Mallela     case GITS_TYPER:
11071b08e436SShashi Mallela         /* RO registers, ignore the write */
11081b08e436SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
11091b08e436SShashi Mallela                       "%s: invalid guest write to RO register at offset "
11101b08e436SShashi Mallela                       TARGET_FMT_plx "\n", __func__, offset);
11111b08e436SShashi Mallela         break;
11121b08e436SShashi Mallela     default:
11131b08e436SShashi Mallela         result = false;
11141b08e436SShashi Mallela         break;
11151b08e436SShashi Mallela     }
111618f6290aSShashi Mallela     return result;
111718f6290aSShashi Mallela }
111818f6290aSShashi Mallela 
111918f6290aSShashi Mallela static bool its_readll(GICv3ITSState *s, hwaddr offset,
112018f6290aSShashi Mallela                               uint64_t *data, MemTxAttrs attrs)
112118f6290aSShashi Mallela {
112218f6290aSShashi Mallela     bool result = true;
11231b08e436SShashi Mallela     int index;
112418f6290aSShashi Mallela 
11251b08e436SShashi Mallela     switch (offset) {
11261b08e436SShashi Mallela     case GITS_TYPER:
11271b08e436SShashi Mallela         *data = s->typer;
11281b08e436SShashi Mallela         break;
11291b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
11301b08e436SShashi Mallela         index = (offset - GITS_BASER) / 8;
11311b08e436SShashi Mallela         *data = s->baser[index];
11321b08e436SShashi Mallela         break;
11331b08e436SShashi Mallela     case GITS_CBASER:
11341b08e436SShashi Mallela         *data = s->cbaser;
11351b08e436SShashi Mallela         break;
11361b08e436SShashi Mallela     case GITS_CREADR:
11371b08e436SShashi Mallela         *data = s->creadr;
11381b08e436SShashi Mallela         break;
11391b08e436SShashi Mallela     case GITS_CWRITER:
11401b08e436SShashi Mallela         *data = s->cwriter;
11411b08e436SShashi Mallela         break;
11421b08e436SShashi Mallela     default:
11431b08e436SShashi Mallela         result = false;
11441b08e436SShashi Mallela         break;
11451b08e436SShashi Mallela     }
114618f6290aSShashi Mallela     return result;
114718f6290aSShashi Mallela }
114818f6290aSShashi Mallela 
114918f6290aSShashi Mallela static MemTxResult gicv3_its_read(void *opaque, hwaddr offset, uint64_t *data,
115018f6290aSShashi Mallela                                   unsigned size, MemTxAttrs attrs)
115118f6290aSShashi Mallela {
115218f6290aSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
115318f6290aSShashi Mallela     bool result;
115418f6290aSShashi Mallela 
115518f6290aSShashi Mallela     switch (size) {
115618f6290aSShashi Mallela     case 4:
115718f6290aSShashi Mallela         result = its_readl(s, offset, data, attrs);
115818f6290aSShashi Mallela         break;
115918f6290aSShashi Mallela     case 8:
116018f6290aSShashi Mallela         result = its_readll(s, offset, data, attrs);
116118f6290aSShashi Mallela         break;
116218f6290aSShashi Mallela     default:
116318f6290aSShashi Mallela         result = false;
116418f6290aSShashi Mallela         break;
116518f6290aSShashi Mallela     }
116618f6290aSShashi Mallela 
116718f6290aSShashi Mallela     if (!result) {
116818f6290aSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
116918f6290aSShashi Mallela                       "%s: invalid guest read at offset " TARGET_FMT_plx
117018f6290aSShashi Mallela                       "size %u\n", __func__, offset, size);
117118f6290aSShashi Mallela         /*
117218f6290aSShashi Mallela          * The spec requires that reserved registers are RAZ/WI;
117318f6290aSShashi Mallela          * so use false returns from leaf functions as a way to
117418f6290aSShashi Mallela          * trigger the guest-error logging but don't return it to
117518f6290aSShashi Mallela          * the caller, or we'll cause a spurious guest data abort.
117618f6290aSShashi Mallela          */
117718f6290aSShashi Mallela         *data = 0;
117818f6290aSShashi Mallela     }
117918f6290aSShashi Mallela     return MEMTX_OK;
118018f6290aSShashi Mallela }
118118f6290aSShashi Mallela 
118218f6290aSShashi Mallela static MemTxResult gicv3_its_write(void *opaque, hwaddr offset, uint64_t data,
118318f6290aSShashi Mallela                                    unsigned size, MemTxAttrs attrs)
118418f6290aSShashi Mallela {
118518f6290aSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
118618f6290aSShashi Mallela     bool result;
118718f6290aSShashi Mallela 
118818f6290aSShashi Mallela     switch (size) {
118918f6290aSShashi Mallela     case 4:
119018f6290aSShashi Mallela         result = its_writel(s, offset, data, attrs);
119118f6290aSShashi Mallela         break;
119218f6290aSShashi Mallela     case 8:
119318f6290aSShashi Mallela         result = its_writell(s, offset, data, attrs);
119418f6290aSShashi Mallela         break;
119518f6290aSShashi Mallela     default:
119618f6290aSShashi Mallela         result = false;
119718f6290aSShashi Mallela         break;
119818f6290aSShashi Mallela     }
119918f6290aSShashi Mallela 
120018f6290aSShashi Mallela     if (!result) {
120118f6290aSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
120218f6290aSShashi Mallela                       "%s: invalid guest write at offset " TARGET_FMT_plx
120318f6290aSShashi Mallela                       "size %u\n", __func__, offset, size);
120418f6290aSShashi Mallela         /*
120518f6290aSShashi Mallela          * The spec requires that reserved registers are RAZ/WI;
120618f6290aSShashi Mallela          * so use false returns from leaf functions as a way to
120718f6290aSShashi Mallela          * trigger the guest-error logging but don't return it to
120818f6290aSShashi Mallela          * the caller, or we'll cause a spurious guest data abort.
120918f6290aSShashi Mallela          */
121018f6290aSShashi Mallela     }
121118f6290aSShashi Mallela     return MEMTX_OK;
121218f6290aSShashi Mallela }
121318f6290aSShashi Mallela 
121418f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_control_ops = {
121518f6290aSShashi Mallela     .read_with_attrs = gicv3_its_read,
121618f6290aSShashi Mallela     .write_with_attrs = gicv3_its_write,
121718f6290aSShashi Mallela     .valid.min_access_size = 4,
121818f6290aSShashi Mallela     .valid.max_access_size = 8,
121918f6290aSShashi Mallela     .impl.min_access_size = 4,
122018f6290aSShashi Mallela     .impl.max_access_size = 8,
122118f6290aSShashi Mallela     .endianness = DEVICE_NATIVE_ENDIAN,
122218f6290aSShashi Mallela };
122318f6290aSShashi Mallela 
122418f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_translation_ops = {
122518f6290aSShashi Mallela     .write_with_attrs = gicv3_its_translation_write,
122618f6290aSShashi Mallela     .valid.min_access_size = 2,
122718f6290aSShashi Mallela     .valid.max_access_size = 4,
122818f6290aSShashi Mallela     .impl.min_access_size = 2,
122918f6290aSShashi Mallela     .impl.max_access_size = 4,
123018f6290aSShashi Mallela     .endianness = DEVICE_NATIVE_ENDIAN,
123118f6290aSShashi Mallela };
123218f6290aSShashi Mallela 
123318f6290aSShashi Mallela static void gicv3_arm_its_realize(DeviceState *dev, Error **errp)
123418f6290aSShashi Mallela {
123518f6290aSShashi Mallela     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
123618f6290aSShashi Mallela     int i;
123718f6290aSShashi Mallela 
123818f6290aSShashi Mallela     for (i = 0; i < s->gicv3->num_cpu; i++) {
123918f6290aSShashi Mallela         if (!(s->gicv3->cpu[i].gicr_typer & GICR_TYPER_PLPIS)) {
124018f6290aSShashi Mallela             error_setg(errp, "Physical LPI not supported by CPU %d", i);
124118f6290aSShashi Mallela             return;
124218f6290aSShashi Mallela         }
124318f6290aSShashi Mallela     }
124418f6290aSShashi Mallela 
124518f6290aSShashi Mallela     gicv3_its_init_mmio(s, &gicv3_its_control_ops, &gicv3_its_translation_ops);
124618f6290aSShashi Mallela 
12471b08e436SShashi Mallela     address_space_init(&s->gicv3->dma_as, s->gicv3->dma,
12481b08e436SShashi Mallela                        "gicv3-its-sysmem");
12491b08e436SShashi Mallela 
125018f6290aSShashi Mallela     /* set the ITS default features supported */
1251764d6ba1SPeter Maydell     s->typer = FIELD_DP64(s->typer, GITS_TYPER, PHYSICAL, 1);
125218f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, ITT_ENTRY_SIZE,
125318f6290aSShashi Mallela                           ITS_ITT_ENTRY_SIZE - 1);
125418f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, IDBITS, ITS_IDBITS);
125518f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, DEVBITS, ITS_DEVBITS);
125618f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIL, 1);
125718f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIDBITS, ITS_CIDBITS);
125818f6290aSShashi Mallela }
125918f6290aSShashi Mallela 
126018f6290aSShashi Mallela static void gicv3_its_reset(DeviceState *dev)
126118f6290aSShashi Mallela {
126218f6290aSShashi Mallela     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
126318f6290aSShashi Mallela     GICv3ITSClass *c = ARM_GICV3_ITS_GET_CLASS(s);
126418f6290aSShashi Mallela 
126518f6290aSShashi Mallela     c->parent_reset(dev);
126618f6290aSShashi Mallela 
126718f6290aSShashi Mallela     /* Quiescent bit reset to 1 */
126818f6290aSShashi Mallela     s->ctlr = FIELD_DP32(s->ctlr, GITS_CTLR, QUIESCENT, 1);
126918f6290aSShashi Mallela 
127018f6290aSShashi Mallela     /*
127118f6290aSShashi Mallela      * setting GITS_BASER0.Type = 0b001 (Device)
127218f6290aSShashi Mallela      *         GITS_BASER1.Type = 0b100 (Collection Table)
127318f6290aSShashi Mallela      *         GITS_BASER<n>.Type,where n = 3 to 7 are 0b00 (Unimplemented)
127418f6290aSShashi Mallela      *         GITS_BASER<0,1>.Page_Size = 64KB
127518f6290aSShashi Mallela      * and default translation table entry size to 16 bytes
127618f6290aSShashi Mallela      */
127718f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, TYPE,
127818f6290aSShashi Mallela                              GITS_BASER_TYPE_DEVICE);
127918f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, PAGESIZE,
128018f6290aSShashi Mallela                              GITS_BASER_PAGESIZE_64K);
128118f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, ENTRYSIZE,
128218f6290aSShashi Mallela                              GITS_DTE_SIZE - 1);
128318f6290aSShashi Mallela 
128418f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, TYPE,
128518f6290aSShashi Mallela                              GITS_BASER_TYPE_COLLECTION);
128618f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, PAGESIZE,
128718f6290aSShashi Mallela                              GITS_BASER_PAGESIZE_64K);
128818f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, ENTRYSIZE,
128918f6290aSShashi Mallela                              GITS_CTE_SIZE - 1);
129018f6290aSShashi Mallela }
129118f6290aSShashi Mallela 
12921b08e436SShashi Mallela static void gicv3_its_post_load(GICv3ITSState *s)
12931b08e436SShashi Mallela {
12948d2d6dd9SPeter Maydell     if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) {
12951b08e436SShashi Mallela         extract_table_params(s);
12961b08e436SShashi Mallela         extract_cmdq_params(s);
12971b08e436SShashi Mallela     }
12981b08e436SShashi Mallela }
12991b08e436SShashi Mallela 
130018f6290aSShashi Mallela static Property gicv3_its_props[] = {
130118f6290aSShashi Mallela     DEFINE_PROP_LINK("parent-gicv3", GICv3ITSState, gicv3, "arm-gicv3",
130218f6290aSShashi Mallela                      GICv3State *),
130318f6290aSShashi Mallela     DEFINE_PROP_END_OF_LIST(),
130418f6290aSShashi Mallela };
130518f6290aSShashi Mallela 
130618f6290aSShashi Mallela static void gicv3_its_class_init(ObjectClass *klass, void *data)
130718f6290aSShashi Mallela {
130818f6290aSShashi Mallela     DeviceClass *dc = DEVICE_CLASS(klass);
130918f6290aSShashi Mallela     GICv3ITSClass *ic = ARM_GICV3_ITS_CLASS(klass);
13101b08e436SShashi Mallela     GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass);
131118f6290aSShashi Mallela 
131218f6290aSShashi Mallela     dc->realize = gicv3_arm_its_realize;
131318f6290aSShashi Mallela     device_class_set_props(dc, gicv3_its_props);
131418f6290aSShashi Mallela     device_class_set_parent_reset(dc, gicv3_its_reset, &ic->parent_reset);
13151b08e436SShashi Mallela     icc->post_load = gicv3_its_post_load;
131618f6290aSShashi Mallela }
131718f6290aSShashi Mallela 
131818f6290aSShashi Mallela static const TypeInfo gicv3_its_info = {
131918f6290aSShashi Mallela     .name = TYPE_ARM_GICV3_ITS,
132018f6290aSShashi Mallela     .parent = TYPE_ARM_GICV3_ITS_COMMON,
132118f6290aSShashi Mallela     .instance_size = sizeof(GICv3ITSState),
132218f6290aSShashi Mallela     .class_init = gicv3_its_class_init,
132318f6290aSShashi Mallela     .class_size = sizeof(GICv3ITSClass),
132418f6290aSShashi Mallela };
132518f6290aSShashi Mallela 
132618f6290aSShashi Mallela static void gicv3_its_register_types(void)
132718f6290aSShashi Mallela {
132818f6290aSShashi Mallela     type_register_static(&gicv3_its_info);
132918f6290aSShashi Mallela }
133018f6290aSShashi Mallela 
133118f6290aSShashi Mallela type_init(gicv3_its_register_types)
1332