xref: /qemu/hw/intc/arm_gicv3_its.c (revision c3c9a09073d8549b431e813ba86bd6f01c0401c3)
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"
16195209d3SPeter Maydell #include "trace.h"
1718f6290aSShashi Mallela #include "hw/qdev-properties.h"
1818f6290aSShashi Mallela #include "hw/intc/arm_gicv3_its_common.h"
1918f6290aSShashi Mallela #include "gicv3_internal.h"
2018f6290aSShashi Mallela #include "qom/object.h"
2118f6290aSShashi Mallela #include "qapi/error.h"
2218f6290aSShashi Mallela 
2318f6290aSShashi Mallela typedef struct GICv3ITSClass GICv3ITSClass;
2418f6290aSShashi Mallela /* This is reusing the GICv3ITSState typedef from ARM_GICV3_ITS_COMMON */
2518f6290aSShashi Mallela DECLARE_OBJ_CHECKERS(GICv3ITSState, GICv3ITSClass,
2618f6290aSShashi Mallela                      ARM_GICV3_ITS, TYPE_ARM_GICV3_ITS)
2718f6290aSShashi Mallela 
2818f6290aSShashi Mallela struct GICv3ITSClass {
2918f6290aSShashi Mallela     GICv3ITSCommonClass parent_class;
3018f6290aSShashi Mallela     void (*parent_reset)(DeviceState *dev);
3118f6290aSShashi Mallela };
3218f6290aSShashi Mallela 
33c694cb4cSShashi Mallela /*
34c694cb4cSShashi Mallela  * This is an internal enum used to distinguish between LPI triggered
35c694cb4cSShashi Mallela  * via command queue and LPI triggered via gits_translater write.
36c694cb4cSShashi Mallela  */
37c694cb4cSShashi Mallela typedef enum ItsCmdType {
38c694cb4cSShashi Mallela     NONE = 0, /* internal indication for GITS_TRANSLATER write */
39c694cb4cSShashi Mallela     CLEAR = 1,
40c694cb4cSShashi Mallela     DISCARD = 2,
41c694cb4cSShashi Mallela     INTERRUPT = 3,
42c694cb4cSShashi Mallela } ItsCmdType;
43c694cb4cSShashi Mallela 
444acf93e1SPeter Maydell typedef struct DTEntry {
454acf93e1SPeter Maydell     bool valid;
464acf93e1SPeter Maydell     unsigned size;
474acf93e1SPeter Maydell     uint64_t ittaddr;
484acf93e1SPeter Maydell } DTEntry;
494acf93e1SPeter Maydell 
50d37cf49bSPeter Maydell typedef struct CTEntry {
51d37cf49bSPeter Maydell     bool valid;
52d37cf49bSPeter Maydell     uint32_t rdbase;
53d37cf49bSPeter Maydell } CTEntry;
54d37cf49bSPeter Maydell 
55244194feSPeter Maydell typedef struct ITEntry {
56244194feSPeter Maydell     bool valid;
57244194feSPeter Maydell     int inttype;
58244194feSPeter Maydell     uint32_t intid;
59244194feSPeter Maydell     uint32_t doorbell;
60244194feSPeter Maydell     uint32_t icid;
61244194feSPeter Maydell     uint32_t vpeid;
62244194feSPeter Maydell } ITEntry;
63244194feSPeter Maydell 
64244194feSPeter Maydell 
65ef011555SPeter Maydell /*
66ef011555SPeter Maydell  * The ITS spec permits a range of CONSTRAINED UNPREDICTABLE options
67ef011555SPeter Maydell  * if a command parameter is not correct. These include both "stall
68ef011555SPeter Maydell  * processing of the command queue" and "ignore this command, and
69ef011555SPeter Maydell  * keep processing the queue". In our implementation we choose that
70ef011555SPeter Maydell  * memory transaction errors reading the command packet provoke a
71ef011555SPeter Maydell  * stall, but errors in parameters cause us to ignore the command
72ef011555SPeter Maydell  * and continue processing.
73ef011555SPeter Maydell  * The process_* functions which handle individual ITS commands all
74ef011555SPeter Maydell  * return an ItsCmdResult which tells process_cmdq() whether it should
75ef011555SPeter Maydell  * stall or keep going.
76ef011555SPeter Maydell  */
77ef011555SPeter Maydell typedef enum ItsCmdResult {
78ef011555SPeter Maydell     CMD_STALL = 0,
79ef011555SPeter Maydell     CMD_CONTINUE = 1,
80ef011555SPeter Maydell } ItsCmdResult;
81ef011555SPeter Maydell 
82*c3c9a090SPeter Maydell static inline bool intid_in_lpi_range(uint32_t id)
83*c3c9a090SPeter Maydell {
84*c3c9a090SPeter Maydell     return id >= GICV3_LPI_INTID_START &&
85*c3c9a090SPeter Maydell         id < (1 << (GICD_TYPER_IDBITS + 1));
86*c3c9a090SPeter Maydell }
87*c3c9a090SPeter Maydell 
881b08e436SShashi Mallela static uint64_t baser_base_addr(uint64_t value, uint32_t page_sz)
891b08e436SShashi Mallela {
901b08e436SShashi Mallela     uint64_t result = 0;
911b08e436SShashi Mallela 
921b08e436SShashi Mallela     switch (page_sz) {
931b08e436SShashi Mallela     case GITS_PAGE_SIZE_4K:
941b08e436SShashi Mallela     case GITS_PAGE_SIZE_16K:
951b08e436SShashi Mallela         result = FIELD_EX64(value, GITS_BASER, PHYADDR) << 12;
961b08e436SShashi Mallela         break;
971b08e436SShashi Mallela 
981b08e436SShashi Mallela     case GITS_PAGE_SIZE_64K:
991b08e436SShashi Mallela         result = FIELD_EX64(value, GITS_BASER, PHYADDRL_64K) << 16;
1001b08e436SShashi Mallela         result |= FIELD_EX64(value, GITS_BASER, PHYADDRH_64K) << 48;
1011b08e436SShashi Mallela         break;
1021b08e436SShashi Mallela 
1031b08e436SShashi Mallela     default:
1041b08e436SShashi Mallela         break;
1051b08e436SShashi Mallela     }
1061b08e436SShashi Mallela     return result;
1071b08e436SShashi Mallela }
1081b08e436SShashi Mallela 
109d050f80fSPeter Maydell static uint64_t table_entry_addr(GICv3ITSState *s, TableDesc *td,
110d050f80fSPeter Maydell                                  uint32_t idx, MemTxResult *res)
111d050f80fSPeter Maydell {
112d050f80fSPeter Maydell     /*
113d050f80fSPeter Maydell      * Given a TableDesc describing one of the ITS in-guest-memory
114d050f80fSPeter Maydell      * tables and an index into it, return the guest address
115d050f80fSPeter Maydell      * corresponding to that table entry.
116d050f80fSPeter Maydell      * If there was a memory error reading the L1 table of an
117d050f80fSPeter Maydell      * indirect table, *res is set accordingly, and we return -1.
118d050f80fSPeter Maydell      * If the L1 table entry is marked not valid, we return -1 with
119d050f80fSPeter Maydell      * *res set to MEMTX_OK.
120d050f80fSPeter Maydell      *
121d050f80fSPeter Maydell      * The specification defines the format of level 1 entries of a
122d050f80fSPeter Maydell      * 2-level table, but the format of level 2 entries and the format
123d050f80fSPeter Maydell      * of flat-mapped tables is IMPDEF.
124d050f80fSPeter Maydell      */
125d050f80fSPeter Maydell     AddressSpace *as = &s->gicv3->dma_as;
126d050f80fSPeter Maydell     uint32_t l2idx;
127d050f80fSPeter Maydell     uint64_t l2;
128d050f80fSPeter Maydell     uint32_t num_l2_entries;
129d050f80fSPeter Maydell 
130d050f80fSPeter Maydell     *res = MEMTX_OK;
131d050f80fSPeter Maydell 
132d050f80fSPeter Maydell     if (!td->indirect) {
133d050f80fSPeter Maydell         /* Single level table */
134d050f80fSPeter Maydell         return td->base_addr + idx * td->entry_sz;
135d050f80fSPeter Maydell     }
136d050f80fSPeter Maydell 
137d050f80fSPeter Maydell     /* Two level table */
138d050f80fSPeter Maydell     l2idx = idx / (td->page_sz / L1TABLE_ENTRY_SIZE);
139d050f80fSPeter Maydell 
140d050f80fSPeter Maydell     l2 = address_space_ldq_le(as,
141d050f80fSPeter Maydell                               td->base_addr + (l2idx * L1TABLE_ENTRY_SIZE),
142d050f80fSPeter Maydell                               MEMTXATTRS_UNSPECIFIED, res);
143d050f80fSPeter Maydell     if (*res != MEMTX_OK) {
144d050f80fSPeter Maydell         return -1;
145d050f80fSPeter Maydell     }
146d050f80fSPeter Maydell     if (!(l2 & L2_TABLE_VALID_MASK)) {
147d050f80fSPeter Maydell         return -1;
148d050f80fSPeter Maydell     }
149d050f80fSPeter Maydell 
150d050f80fSPeter Maydell     num_l2_entries = td->page_sz / td->entry_sz;
151d050f80fSPeter Maydell     return (l2 & ((1ULL << 51) - 1)) + (idx % num_l2_entries) * td->entry_sz;
152d050f80fSPeter Maydell }
153d050f80fSPeter Maydell 
154d37cf49bSPeter Maydell /*
155d37cf49bSPeter Maydell  * Read the Collection Table entry at index @icid. On success (including
156d37cf49bSPeter Maydell  * successfully determining that there is no valid CTE for this index),
157d37cf49bSPeter Maydell  * we return MEMTX_OK and populate the CTEntry struct @cte accordingly.
158d37cf49bSPeter Maydell  * If there is an error reading memory then we return the error code.
159d37cf49bSPeter Maydell  */
160d37cf49bSPeter Maydell static MemTxResult get_cte(GICv3ITSState *s, uint16_t icid, CTEntry *cte)
161c694cb4cSShashi Mallela {
162c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
163d37cf49bSPeter Maydell     MemTxResult res = MEMTX_OK;
164d37cf49bSPeter Maydell     uint64_t entry_addr = table_entry_addr(s, &s->ct, icid, &res);
165d37cf49bSPeter Maydell     uint64_t cteval;
166c694cb4cSShashi Mallela 
167d050f80fSPeter Maydell     if (entry_addr == -1) {
168d37cf49bSPeter Maydell         /* No L2 table entry, i.e. no valid CTE, or a memory error */
169d37cf49bSPeter Maydell         cte->valid = false;
170930f40e9SPeter Maydell         goto out;
171c694cb4cSShashi Mallela     }
172c694cb4cSShashi Mallela 
173d37cf49bSPeter Maydell     cteval = address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, &res);
174d37cf49bSPeter Maydell     if (res != MEMTX_OK) {
175930f40e9SPeter Maydell         goto out;
176d37cf49bSPeter Maydell     }
177d37cf49bSPeter Maydell     cte->valid = FIELD_EX64(cteval, CTE, VALID);
178d37cf49bSPeter Maydell     cte->rdbase = FIELD_EX64(cteval, CTE, RDBASE);
179930f40e9SPeter Maydell out:
180930f40e9SPeter Maydell     if (res != MEMTX_OK) {
181930f40e9SPeter Maydell         trace_gicv3_its_cte_read_fault(icid);
182930f40e9SPeter Maydell     } else {
183930f40e9SPeter Maydell         trace_gicv3_its_cte_read(icid, cte->valid, cte->rdbase);
184930f40e9SPeter Maydell     }
185930f40e9SPeter Maydell     return res;
186c694cb4cSShashi Mallela }
187c694cb4cSShashi Mallela 
1887eb54267SPeter Maydell /*
1897eb54267SPeter Maydell  * Update the Interrupt Table entry at index @evinted in the table specified
1907eb54267SPeter Maydell  * by the dte @dte. Returns true on success, false if there was a memory
1917eb54267SPeter Maydell  * access error.
1927eb54267SPeter Maydell  */
1934acf93e1SPeter Maydell static bool update_ite(GICv3ITSState *s, uint32_t eventid, const DTEntry *dte,
1947eb54267SPeter Maydell                        const ITEntry *ite)
195c694cb4cSShashi Mallela {
196c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
197c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
198a1ce993dSPeter Maydell     hwaddr iteaddr = dte->ittaddr + eventid * ITS_ITT_ENTRY_SIZE;
1997eb54267SPeter Maydell     uint64_t itel = 0;
2007eb54267SPeter Maydell     uint32_t iteh = 0;
201c694cb4cSShashi Mallela 
202930f40e9SPeter Maydell     trace_gicv3_its_ite_write(dte->ittaddr, eventid, ite->valid,
203930f40e9SPeter Maydell                               ite->inttype, ite->intid, ite->icid,
204930f40e9SPeter Maydell                               ite->vpeid, ite->doorbell);
205930f40e9SPeter Maydell 
2067eb54267SPeter Maydell     if (ite->valid) {
2077eb54267SPeter Maydell         itel = FIELD_DP64(itel, ITE_L, VALID, 1);
2087eb54267SPeter Maydell         itel = FIELD_DP64(itel, ITE_L, INTTYPE, ite->inttype);
2097eb54267SPeter Maydell         itel = FIELD_DP64(itel, ITE_L, INTID, ite->intid);
2107eb54267SPeter Maydell         itel = FIELD_DP64(itel, ITE_L, ICID, ite->icid);
2117eb54267SPeter Maydell         itel = FIELD_DP64(itel, ITE_L, VPEID, ite->vpeid);
2127eb54267SPeter Maydell         iteh = FIELD_DP32(iteh, ITE_H, DOORBELL, ite->doorbell);
213c694cb4cSShashi Mallela     }
2147eb54267SPeter Maydell 
2157eb54267SPeter Maydell     address_space_stq_le(as, iteaddr, itel, MEMTXATTRS_UNSPECIFIED, &res);
216c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
217c694cb4cSShashi Mallela         return false;
218c694cb4cSShashi Mallela     }
2197eb54267SPeter Maydell     address_space_stl_le(as, iteaddr + 8, iteh, MEMTXATTRS_UNSPECIFIED, &res);
2207eb54267SPeter Maydell     return res == MEMTX_OK;
221c694cb4cSShashi Mallela }
222c694cb4cSShashi Mallela 
223244194feSPeter Maydell /*
224244194feSPeter Maydell  * Read the Interrupt Table entry at index @eventid from the table specified
225244194feSPeter Maydell  * by the DTE @dte. On success, we return MEMTX_OK and populate the ITEntry
226244194feSPeter Maydell  * struct @ite accordingly. If there is an error reading memory then we return
227244194feSPeter Maydell  * the error code.
228244194feSPeter Maydell  */
229244194feSPeter Maydell static MemTxResult get_ite(GICv3ITSState *s, uint32_t eventid,
230244194feSPeter Maydell                            const DTEntry *dte, ITEntry *ite)
231c694cb4cSShashi Mallela {
232c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
233244194feSPeter Maydell     MemTxResult res = MEMTX_OK;
234244194feSPeter Maydell     uint64_t itel;
235244194feSPeter Maydell     uint32_t iteh;
236a1ce993dSPeter Maydell     hwaddr iteaddr = dte->ittaddr + eventid * ITS_ITT_ENTRY_SIZE;
237c694cb4cSShashi Mallela 
238244194feSPeter Maydell     itel = address_space_ldq_le(as, iteaddr, MEMTXATTRS_UNSPECIFIED, &res);
239244194feSPeter Maydell     if (res != MEMTX_OK) {
240930f40e9SPeter Maydell         trace_gicv3_its_ite_read_fault(dte->ittaddr, eventid);
241244194feSPeter Maydell         return res;
2422954b93fSPeter Maydell     }
243c694cb4cSShashi Mallela 
244244194feSPeter Maydell     iteh = address_space_ldl_le(as, iteaddr + 8, MEMTXATTRS_UNSPECIFIED, &res);
245244194feSPeter Maydell     if (res != MEMTX_OK) {
246930f40e9SPeter Maydell         trace_gicv3_its_ite_read_fault(dte->ittaddr, eventid);
247244194feSPeter Maydell         return res;
2482954b93fSPeter Maydell     }
249c694cb4cSShashi Mallela 
250244194feSPeter Maydell     ite->valid = FIELD_EX64(itel, ITE_L, VALID);
251244194feSPeter Maydell     ite->inttype = FIELD_EX64(itel, ITE_L, INTTYPE);
252244194feSPeter Maydell     ite->intid = FIELD_EX64(itel, ITE_L, INTID);
253244194feSPeter Maydell     ite->icid = FIELD_EX64(itel, ITE_L, ICID);
254244194feSPeter Maydell     ite->vpeid = FIELD_EX64(itel, ITE_L, VPEID);
255244194feSPeter Maydell     ite->doorbell = FIELD_EX64(iteh, ITE_H, DOORBELL);
256930f40e9SPeter Maydell     trace_gicv3_its_ite_read(dte->ittaddr, eventid, ite->valid,
257930f40e9SPeter Maydell                              ite->inttype, ite->intid, ite->icid,
258930f40e9SPeter Maydell                              ite->vpeid, ite->doorbell);
259244194feSPeter Maydell     return MEMTX_OK;
260c694cb4cSShashi Mallela }
261c694cb4cSShashi Mallela 
2624acf93e1SPeter Maydell /*
2634acf93e1SPeter Maydell  * Read the Device Table entry at index @devid. On success (including
2644acf93e1SPeter Maydell  * successfully determining that there is no valid DTE for this index),
2654acf93e1SPeter Maydell  * we return MEMTX_OK and populate the DTEntry struct accordingly.
2664acf93e1SPeter Maydell  * If there is an error reading memory then we return the error code.
2674acf93e1SPeter Maydell  */
2684acf93e1SPeter Maydell static MemTxResult get_dte(GICv3ITSState *s, uint32_t devid, DTEntry *dte)
269c694cb4cSShashi Mallela {
2704acf93e1SPeter Maydell     MemTxResult res = MEMTX_OK;
271c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
2724acf93e1SPeter Maydell     uint64_t entry_addr = table_entry_addr(s, &s->dt, devid, &res);
2734acf93e1SPeter Maydell     uint64_t dteval;
274c694cb4cSShashi Mallela 
275d050f80fSPeter Maydell     if (entry_addr == -1) {
2764acf93e1SPeter Maydell         /* No L2 table entry, i.e. no valid DTE, or a memory error */
2774acf93e1SPeter Maydell         dte->valid = false;
278930f40e9SPeter Maydell         goto out;
279c694cb4cSShashi Mallela     }
2804acf93e1SPeter Maydell     dteval = address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, &res);
2814acf93e1SPeter Maydell     if (res != MEMTX_OK) {
282930f40e9SPeter Maydell         goto out;
2834acf93e1SPeter Maydell     }
2844acf93e1SPeter Maydell     dte->valid = FIELD_EX64(dteval, DTE, VALID);
2854acf93e1SPeter Maydell     dte->size = FIELD_EX64(dteval, DTE, SIZE);
2864acf93e1SPeter Maydell     /* DTE word field stores bits [51:8] of the ITT address */
2874acf93e1SPeter Maydell     dte->ittaddr = FIELD_EX64(dteval, DTE, ITTADDR) << ITTADDR_SHIFT;
288930f40e9SPeter Maydell out:
289930f40e9SPeter Maydell     if (res != MEMTX_OK) {
290930f40e9SPeter Maydell         trace_gicv3_its_dte_read_fault(devid);
291930f40e9SPeter Maydell     } else {
292930f40e9SPeter Maydell         trace_gicv3_its_dte_read(devid, dte->valid, dte->size, dte->ittaddr);
293930f40e9SPeter Maydell     }
294930f40e9SPeter Maydell     return res;
295c694cb4cSShashi Mallela }
296c694cb4cSShashi Mallela 
297c694cb4cSShashi Mallela /*
298c694cb4cSShashi Mallela  * This function handles the processing of following commands based on
299c694cb4cSShashi Mallela  * the ItsCmdType parameter passed:-
300c694cb4cSShashi Mallela  * 1. triggering of lpi interrupt translation via ITS INT command
301c694cb4cSShashi Mallela  * 2. triggering of lpi interrupt translation via gits_translater register
302c694cb4cSShashi Mallela  * 3. handling of ITS CLEAR command
303c694cb4cSShashi Mallela  * 4. handling of ITS DISCARD command
304c694cb4cSShashi Mallela  */
305b6f96009SPeter Maydell static ItsCmdResult do_process_its_cmd(GICv3ITSState *s, uint32_t devid,
306b6f96009SPeter Maydell                                        uint32_t eventid, ItsCmdType cmd)
307c694cb4cSShashi Mallela {
3088f809f69SPeter Maydell     uint64_t num_eventids;
3094acf93e1SPeter Maydell     DTEntry dte;
310d37cf49bSPeter Maydell     CTEntry cte;
311244194feSPeter Maydell     ITEntry ite;
312c694cb4cSShashi Mallela 
3138b8bb014SPeter Maydell     if (devid >= s->dt.num_entries) {
314b13148d9SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
315b13148d9SPeter Maydell                       "%s: invalid command attributes: devid %d>=%d",
3168b8bb014SPeter Maydell                       __func__, devid, s->dt.num_entries);
317b13148d9SPeter Maydell         return CMD_CONTINUE;
318b13148d9SPeter Maydell     }
319b13148d9SPeter Maydell 
3204acf93e1SPeter Maydell     if (get_dte(s, devid, &dte) != MEMTX_OK) {
321593a7cc2SPeter Maydell         return CMD_STALL;
322c694cb4cSShashi Mallela     }
3234acf93e1SPeter Maydell     if (!dte.valid) {
324229c57b1SAlex Bennée         qemu_log_mask(LOG_GUEST_ERROR,
325229c57b1SAlex Bennée                       "%s: invalid command attributes: "
3264acf93e1SPeter Maydell                       "invalid dte for %d\n", __func__, devid);
327593a7cc2SPeter Maydell         return CMD_CONTINUE;
328c694cb4cSShashi Mallela     }
329c694cb4cSShashi Mallela 
3304acf93e1SPeter Maydell     num_eventids = 1ULL << (dte.size + 1);
331b13148d9SPeter Maydell     if (eventid >= num_eventids) {
332b13148d9SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
333b13148d9SPeter Maydell                       "%s: invalid command attributes: eventid %d >= %"
334b13148d9SPeter Maydell                       PRId64 "\n",
335b13148d9SPeter Maydell                       __func__, eventid, num_eventids);
336b13148d9SPeter Maydell         return CMD_CONTINUE;
337b13148d9SPeter Maydell     }
338b13148d9SPeter Maydell 
339244194feSPeter Maydell     if (get_ite(s, eventid, &dte, &ite) != MEMTX_OK) {
340be0ed8fbSPeter Maydell         return CMD_STALL;
341be0ed8fbSPeter Maydell     }
342be0ed8fbSPeter Maydell 
343244194feSPeter Maydell     if (!ite.valid || ite.inttype != ITE_INTTYPE_PHYSICAL) {
344be0ed8fbSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
345be0ed8fbSPeter Maydell                       "%s: invalid command attributes: invalid ITE\n",
346be0ed8fbSPeter Maydell                       __func__);
347be0ed8fbSPeter Maydell         return CMD_CONTINUE;
348be0ed8fbSPeter Maydell     }
349be0ed8fbSPeter Maydell 
350244194feSPeter Maydell     if (ite.icid >= s->ct.num_entries) {
35158b88779SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
35258b88779SPeter Maydell                       "%s: invalid ICID 0x%x in ITE (table corrupted?)\n",
353244194feSPeter Maydell                       __func__, ite.icid);
35458b88779SPeter Maydell         return CMD_CONTINUE;
35558b88779SPeter Maydell     }
35658b88779SPeter Maydell 
357244194feSPeter Maydell     if (get_cte(s, ite.icid, &cte) != MEMTX_OK) {
358be0ed8fbSPeter Maydell         return CMD_STALL;
359be0ed8fbSPeter Maydell     }
360d37cf49bSPeter Maydell     if (!cte.valid) {
361be0ed8fbSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
362d37cf49bSPeter Maydell                       "%s: invalid command attributes: invalid CTE\n",
363d37cf49bSPeter Maydell                       __func__);
364be0ed8fbSPeter Maydell         return CMD_CONTINUE;
365be0ed8fbSPeter Maydell     }
366be0ed8fbSPeter Maydell 
367c694cb4cSShashi Mallela     /*
368c694cb4cSShashi Mallela      * Current implementation only supports rdbase == procnum
369c694cb4cSShashi Mallela      * Hence rdbase physical address is ignored
370c694cb4cSShashi Mallela      */
371d37cf49bSPeter Maydell     if (cte.rdbase >= s->gicv3->num_cpu) {
372593a7cc2SPeter Maydell         return CMD_CONTINUE;
37317fb5e36SShashi Mallela     }
37417fb5e36SShashi Mallela 
37517fb5e36SShashi Mallela     if ((cmd == CLEAR) || (cmd == DISCARD)) {
376244194feSPeter Maydell         gicv3_redist_process_lpi(&s->gicv3->cpu[cte.rdbase], ite.intid, 0);
37717fb5e36SShashi Mallela     } else {
378244194feSPeter Maydell         gicv3_redist_process_lpi(&s->gicv3->cpu[cte.rdbase], ite.intid, 1);
37917fb5e36SShashi Mallela     }
38017fb5e36SShashi Mallela 
381c694cb4cSShashi Mallela     if (cmd == DISCARD) {
3827eb54267SPeter Maydell         ITEntry ite = {};
383c694cb4cSShashi Mallela         /* remove mapping from interrupt translation table */
3847eb54267SPeter Maydell         ite.valid = false;
3857eb54267SPeter Maydell         return update_ite(s, eventid, &dte, &ite) ? CMD_CONTINUE : CMD_STALL;
386c694cb4cSShashi Mallela     }
387593a7cc2SPeter Maydell     return CMD_CONTINUE;
388c694cb4cSShashi Mallela }
3892a199036SPeter Maydell 
390b6f96009SPeter Maydell static ItsCmdResult process_its_cmd(GICv3ITSState *s, const uint64_t *cmdpkt,
391b6f96009SPeter Maydell                                     ItsCmdType cmd)
392c694cb4cSShashi Mallela {
393b6f96009SPeter Maydell     uint32_t devid, eventid;
394b6f96009SPeter Maydell 
395b6f96009SPeter Maydell     devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT;
396b6f96009SPeter Maydell     eventid = cmdpkt[1] & EVENTID_MASK;
397e4050980SPeter Maydell     switch (cmd) {
398e4050980SPeter Maydell     case INTERRUPT:
399e4050980SPeter Maydell         trace_gicv3_its_cmd_int(devid, eventid);
400e4050980SPeter Maydell         break;
401e4050980SPeter Maydell     case CLEAR:
402e4050980SPeter Maydell         trace_gicv3_its_cmd_clear(devid, eventid);
403e4050980SPeter Maydell         break;
404e4050980SPeter Maydell     case DISCARD:
405e4050980SPeter Maydell         trace_gicv3_its_cmd_discard(devid, eventid);
406e4050980SPeter Maydell         break;
407e4050980SPeter Maydell     default:
408e4050980SPeter Maydell         g_assert_not_reached();
409e4050980SPeter Maydell     }
410b6f96009SPeter Maydell     return do_process_its_cmd(s, devid, eventid, cmd);
411b6f96009SPeter Maydell }
412b6f96009SPeter Maydell 
413b6f96009SPeter Maydell static ItsCmdResult process_mapti(GICv3ITSState *s, const uint64_t *cmdpkt,
414b6f96009SPeter Maydell                                   bool ignore_pInt)
415b6f96009SPeter Maydell {
416c694cb4cSShashi Mallela     uint32_t devid, eventid;
417c694cb4cSShashi Mallela     uint32_t pIntid = 0;
4188f809f69SPeter Maydell     uint64_t num_eventids;
419c694cb4cSShashi Mallela     uint16_t icid = 0;
4204acf93e1SPeter Maydell     DTEntry dte;
4217eb54267SPeter Maydell     ITEntry ite;
422c694cb4cSShashi Mallela 
423b6f96009SPeter Maydell     devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT;
424b6f96009SPeter Maydell     eventid = cmdpkt[1] & EVENTID_MASK;
425e4050980SPeter Maydell     icid = cmdpkt[2] & ICID_MASK;
426c694cb4cSShashi Mallela 
427b87fab1cSPeter Maydell     if (ignore_pInt) {
428b87fab1cSPeter Maydell         pIntid = eventid;
429e4050980SPeter Maydell         trace_gicv3_its_cmd_mapi(devid, eventid, icid);
430b87fab1cSPeter Maydell     } else {
431b6f96009SPeter Maydell         pIntid = (cmdpkt[1] & pINTID_MASK) >> pINTID_SHIFT;
432e4050980SPeter Maydell         trace_gicv3_its_cmd_mapti(devid, eventid, icid, pIntid);
433c694cb4cSShashi Mallela     }
434c694cb4cSShashi Mallela 
4358b8bb014SPeter Maydell     if (devid >= s->dt.num_entries) {
436b13148d9SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
437b13148d9SPeter Maydell                       "%s: invalid command attributes: devid %d>=%d",
4388b8bb014SPeter Maydell                       __func__, devid, s->dt.num_entries);
439b13148d9SPeter Maydell         return CMD_CONTINUE;
440b13148d9SPeter Maydell     }
441b13148d9SPeter Maydell 
4424acf93e1SPeter Maydell     if (get_dte(s, devid, &dte) != MEMTX_OK) {
4430241f731SPeter Maydell         return CMD_STALL;
444c694cb4cSShashi Mallela     }
4454acf93e1SPeter Maydell     num_eventids = 1ULL << (dte.size + 1);
446c694cb4cSShashi Mallela 
447d7d359c4SPeter Maydell     if (icid >= s->ct.num_entries) {
448c694cb4cSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
449d7d359c4SPeter Maydell                       "%s: invalid ICID 0x%x >= 0x%x\n",
450d7d359c4SPeter Maydell                       __func__, icid, s->ct.num_entries);
451d7d359c4SPeter Maydell         return CMD_CONTINUE;
452d7d359c4SPeter Maydell     }
453d7d359c4SPeter Maydell 
454d7d359c4SPeter Maydell     if (!dte.valid) {
455d7d359c4SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
456d7d359c4SPeter Maydell                       "%s: no valid DTE for devid 0x%x\n", __func__, devid);
457d7d359c4SPeter Maydell         return CMD_CONTINUE;
458d7d359c4SPeter Maydell     }
459d7d359c4SPeter Maydell 
460d7d359c4SPeter Maydell     if (eventid >= num_eventids) {
461d7d359c4SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
462d7d359c4SPeter Maydell                       "%s: invalid event ID 0x%x >= 0x%" PRIx64 "\n",
463d7d359c4SPeter Maydell                       __func__, eventid, num_eventids);
464d7d359c4SPeter Maydell         return CMD_CONTINUE;
465d7d359c4SPeter Maydell     }
466d7d359c4SPeter Maydell 
467*c3c9a090SPeter Maydell     if (!intid_in_lpi_range(pIntid)) {
468d7d359c4SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
469d7d359c4SPeter Maydell                       "%s: invalid interrupt ID 0x%x\n", __func__, pIntid);
4700241f731SPeter Maydell         return CMD_CONTINUE;
4710241f731SPeter Maydell     }
4720241f731SPeter Maydell 
473c694cb4cSShashi Mallela     /* add ite entry to interrupt translation table */
4747eb54267SPeter Maydell     ite.valid = true;
4757eb54267SPeter Maydell     ite.inttype = ITE_INTTYPE_PHYSICAL;
4767eb54267SPeter Maydell     ite.intid = pIntid;
4777eb54267SPeter Maydell     ite.icid = icid;
4787eb54267SPeter Maydell     ite.doorbell = INTID_SPURIOUS;
4797eb54267SPeter Maydell     ite.vpeid = 0;
4807eb54267SPeter Maydell     return update_ite(s, eventid, &dte, &ite) ? CMD_CONTINUE : CMD_STALL;
481c694cb4cSShashi Mallela }
482c694cb4cSShashi Mallela 
48306985cc3SPeter Maydell /*
48406985cc3SPeter Maydell  * Update the Collection Table entry for @icid to @cte. Returns true
48506985cc3SPeter Maydell  * on success, false if there was a memory access error.
48606985cc3SPeter Maydell  */
48706985cc3SPeter Maydell static bool update_cte(GICv3ITSState *s, uint16_t icid, const CTEntry *cte)
4887eca39e0SShashi Mallela {
4897eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
490d050f80fSPeter Maydell     uint64_t entry_addr;
49106985cc3SPeter Maydell     uint64_t cteval = 0;
4927eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
4937eca39e0SShashi Mallela 
494930f40e9SPeter Maydell     trace_gicv3_its_cte_write(icid, cte->valid, cte->rdbase);
495930f40e9SPeter Maydell 
49606985cc3SPeter Maydell     if (cte->valid) {
4977eca39e0SShashi Mallela         /* add mapping entry to collection table */
49806985cc3SPeter Maydell         cteval = FIELD_DP64(cteval, CTE, VALID, 1);
49906985cc3SPeter Maydell         cteval = FIELD_DP64(cteval, CTE, RDBASE, cte->rdbase);
5007eca39e0SShashi Mallela     }
5017eca39e0SShashi Mallela 
502d050f80fSPeter Maydell     entry_addr = table_entry_addr(s, &s->ct, icid, &res);
5037eca39e0SShashi Mallela     if (res != MEMTX_OK) {
504d050f80fSPeter Maydell         /* memory access error: stall */
5057eca39e0SShashi Mallela         return false;
5067eca39e0SShashi Mallela     }
507d050f80fSPeter Maydell     if (entry_addr == -1) {
508d050f80fSPeter Maydell         /* No L2 table for this index: discard write and continue */
5097eca39e0SShashi Mallela         return true;
5107eca39e0SShashi Mallela     }
511d050f80fSPeter Maydell 
51206985cc3SPeter Maydell     address_space_stq_le(as, entry_addr, cteval, MEMTXATTRS_UNSPECIFIED, &res);
513d050f80fSPeter Maydell     return res == MEMTX_OK;
5147eca39e0SShashi Mallela }
5157eca39e0SShashi Mallela 
516b6f96009SPeter Maydell static ItsCmdResult process_mapc(GICv3ITSState *s, const uint64_t *cmdpkt)
5177eca39e0SShashi Mallela {
5187eca39e0SShashi Mallela     uint16_t icid;
51906985cc3SPeter Maydell     CTEntry cte;
5207eca39e0SShashi Mallela 
521b6f96009SPeter Maydell     icid = cmdpkt[2] & ICID_MASK;
52284d43d2eSPeter Maydell     cte.valid = cmdpkt[2] & CMD_FIELD_VALID_MASK;
52384d43d2eSPeter Maydell     if (cte.valid) {
52406985cc3SPeter Maydell         cte.rdbase = (cmdpkt[2] & R_MAPC_RDBASE_MASK) >> R_MAPC_RDBASE_SHIFT;
52506985cc3SPeter Maydell         cte.rdbase &= RDBASE_PROCNUM_MASK;
52684d43d2eSPeter Maydell     } else {
52784d43d2eSPeter Maydell         cte.rdbase = 0;
52884d43d2eSPeter Maydell     }
529e4050980SPeter Maydell     trace_gicv3_its_cmd_mapc(icid, cte.rdbase, cte.valid);
5307eca39e0SShashi Mallela 
53184d43d2eSPeter Maydell     if (icid >= s->ct.num_entries) {
532c7ca3ad5SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "ITS MAPC: invalid ICID 0x%x\n", icid);
53384d43d2eSPeter Maydell         return CMD_CONTINUE;
53484d43d2eSPeter Maydell     }
53584d43d2eSPeter Maydell     if (cte.valid && cte.rdbase >= s->gicv3->num_cpu) {
5367eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
537c7ca3ad5SPeter Maydell                       "ITS MAPC: invalid RDBASE %u\n", cte.rdbase);
538f6675196SPeter Maydell         return CMD_CONTINUE;
5397eca39e0SShashi Mallela     }
5407eca39e0SShashi Mallela 
54106985cc3SPeter Maydell     return update_cte(s, icid, &cte) ? CMD_CONTINUE : CMD_STALL;
5427eca39e0SShashi Mallela }
5437eca39e0SShashi Mallela 
54422d62b08SPeter Maydell /*
54522d62b08SPeter Maydell  * Update the Device Table entry for @devid to @dte. Returns true
54622d62b08SPeter Maydell  * on success, false if there was a memory access error.
54722d62b08SPeter Maydell  */
54822d62b08SPeter Maydell static bool update_dte(GICv3ITSState *s, uint32_t devid, const DTEntry *dte)
5497eca39e0SShashi Mallela {
5507eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
551d050f80fSPeter Maydell     uint64_t entry_addr;
55222d62b08SPeter Maydell     uint64_t dteval = 0;
5537eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
5547eca39e0SShashi Mallela 
555930f40e9SPeter Maydell     trace_gicv3_its_dte_write(devid, dte->valid, dte->size, dte->ittaddr);
556930f40e9SPeter Maydell 
55722d62b08SPeter Maydell     if (dte->valid) {
5587eca39e0SShashi Mallela         /* add mapping entry to device table */
55922d62b08SPeter Maydell         dteval = FIELD_DP64(dteval, DTE, VALID, 1);
56022d62b08SPeter Maydell         dteval = FIELD_DP64(dteval, DTE, SIZE, dte->size);
56122d62b08SPeter Maydell         dteval = FIELD_DP64(dteval, DTE, ITTADDR, dte->ittaddr);
5627eca39e0SShashi Mallela     }
5637eca39e0SShashi Mallela 
564d050f80fSPeter Maydell     entry_addr = table_entry_addr(s, &s->dt, devid, &res);
5657eca39e0SShashi Mallela     if (res != MEMTX_OK) {
566d050f80fSPeter Maydell         /* memory access error: stall */
5677eca39e0SShashi Mallela         return false;
5687eca39e0SShashi Mallela     }
569d050f80fSPeter Maydell     if (entry_addr == -1) {
570d050f80fSPeter Maydell         /* No L2 table for this index: discard write and continue */
5717eca39e0SShashi Mallela         return true;
5727eca39e0SShashi Mallela     }
57322d62b08SPeter Maydell     address_space_stq_le(as, entry_addr, dteval, MEMTXATTRS_UNSPECIFIED, &res);
574d050f80fSPeter Maydell     return res == MEMTX_OK;
5757eca39e0SShashi Mallela }
5767eca39e0SShashi Mallela 
577b6f96009SPeter Maydell static ItsCmdResult process_mapd(GICv3ITSState *s, const uint64_t *cmdpkt)
5787eca39e0SShashi Mallela {
5797eca39e0SShashi Mallela     uint32_t devid;
58022d62b08SPeter Maydell     DTEntry dte;
5817eca39e0SShashi Mallela 
582b6f96009SPeter Maydell     devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT;
58322d62b08SPeter Maydell     dte.size = cmdpkt[1] & SIZE_MASK;
58422d62b08SPeter Maydell     dte.ittaddr = (cmdpkt[2] & ITTADDR_MASK) >> ITTADDR_SHIFT;
58522d62b08SPeter Maydell     dte.valid = cmdpkt[2] & CMD_FIELD_VALID_MASK;
5867eca39e0SShashi Mallela 
587e4050980SPeter Maydell     trace_gicv3_its_cmd_mapd(devid, dte.size, dte.ittaddr, dte.valid);
588e4050980SPeter Maydell 
589d7d359c4SPeter Maydell     if (devid >= s->dt.num_entries) {
5907eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
591d7d359c4SPeter Maydell                       "ITS MAPD: invalid device ID field 0x%x >= 0x%x\n",
592d7d359c4SPeter Maydell                       devid, s->dt.num_entries);
593d7d359c4SPeter Maydell         return CMD_CONTINUE;
594d7d359c4SPeter Maydell     }
595d7d359c4SPeter Maydell 
596d7d359c4SPeter Maydell     if (dte.size > FIELD_EX64(s->typer, GITS_TYPER, IDBITS)) {
597d7d359c4SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
598d7d359c4SPeter Maydell                       "ITS MAPD: invalid size %d\n", dte.size);
59900d46e72SPeter Maydell         return CMD_CONTINUE;
6007eca39e0SShashi Mallela     }
6017eca39e0SShashi Mallela 
60222d62b08SPeter Maydell     return update_dte(s, devid, &dte) ? CMD_CONTINUE : CMD_STALL;
6037eca39e0SShashi Mallela }
6047eca39e0SShashi Mallela 
605b6f96009SPeter Maydell static ItsCmdResult process_movall(GICv3ITSState *s, const uint64_t *cmdpkt)
606f6d1d9b4SPeter Maydell {
607f6d1d9b4SPeter Maydell     uint64_t rd1, rd2;
608f6d1d9b4SPeter Maydell 
609b6f96009SPeter Maydell     rd1 = FIELD_EX64(cmdpkt[2], MOVALL_2, RDBASE1);
610b6f96009SPeter Maydell     rd2 = FIELD_EX64(cmdpkt[3], MOVALL_3, RDBASE2);
611f6d1d9b4SPeter Maydell 
612e4050980SPeter Maydell     trace_gicv3_its_cmd_movall(rd1, rd2);
613e4050980SPeter Maydell 
614f6d1d9b4SPeter Maydell     if (rd1 >= s->gicv3->num_cpu) {
615f6d1d9b4SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
616f6d1d9b4SPeter Maydell                       "%s: RDBASE1 %" PRId64
617f6d1d9b4SPeter Maydell                       " out of range (must be less than %d)\n",
618f6d1d9b4SPeter Maydell                       __func__, rd1, s->gicv3->num_cpu);
619f6d1d9b4SPeter Maydell         return CMD_CONTINUE;
620f6d1d9b4SPeter Maydell     }
621f6d1d9b4SPeter Maydell     if (rd2 >= s->gicv3->num_cpu) {
622f6d1d9b4SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
623f6d1d9b4SPeter Maydell                       "%s: RDBASE2 %" PRId64
624f6d1d9b4SPeter Maydell                       " out of range (must be less than %d)\n",
625f6d1d9b4SPeter Maydell                       __func__, rd2, s->gicv3->num_cpu);
626f6d1d9b4SPeter Maydell         return CMD_CONTINUE;
627f6d1d9b4SPeter Maydell     }
628f6d1d9b4SPeter Maydell 
629f6d1d9b4SPeter Maydell     if (rd1 == rd2) {
630f6d1d9b4SPeter Maydell         /* Move to same target must succeed as a no-op */
631f6d1d9b4SPeter Maydell         return CMD_CONTINUE;
632f6d1d9b4SPeter Maydell     }
633f6d1d9b4SPeter Maydell 
634f6d1d9b4SPeter Maydell     /* Move all pending LPIs from redistributor 1 to redistributor 2 */
635f6d1d9b4SPeter Maydell     gicv3_redist_movall_lpis(&s->gicv3->cpu[rd1], &s->gicv3->cpu[rd2]);
636f6d1d9b4SPeter Maydell 
637f6d1d9b4SPeter Maydell     return CMD_CONTINUE;
638f6d1d9b4SPeter Maydell }
639f6d1d9b4SPeter Maydell 
640b6f96009SPeter Maydell static ItsCmdResult process_movi(GICv3ITSState *s, const uint64_t *cmdpkt)
641961b4912SPeter Maydell {
642244194feSPeter Maydell     uint32_t devid, eventid;
643244194feSPeter Maydell     uint16_t new_icid;
644961b4912SPeter Maydell     uint64_t num_eventids;
6454acf93e1SPeter Maydell     DTEntry dte;
646d37cf49bSPeter Maydell     CTEntry old_cte, new_cte;
647244194feSPeter Maydell     ITEntry old_ite;
648961b4912SPeter Maydell 
649b6f96009SPeter Maydell     devid = FIELD_EX64(cmdpkt[0], MOVI_0, DEVICEID);
650b6f96009SPeter Maydell     eventid = FIELD_EX64(cmdpkt[1], MOVI_1, EVENTID);
651b6f96009SPeter Maydell     new_icid = FIELD_EX64(cmdpkt[2], MOVI_2, ICID);
652961b4912SPeter Maydell 
653e4050980SPeter Maydell     trace_gicv3_its_cmd_movi(devid, eventid, new_icid);
654e4050980SPeter Maydell 
655961b4912SPeter Maydell     if (devid >= s->dt.num_entries) {
656961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
657961b4912SPeter Maydell                       "%s: invalid command attributes: devid %d>=%d",
658961b4912SPeter Maydell                       __func__, devid, s->dt.num_entries);
659961b4912SPeter Maydell         return CMD_CONTINUE;
660961b4912SPeter Maydell     }
6614acf93e1SPeter Maydell     if (get_dte(s, devid, &dte) != MEMTX_OK) {
662961b4912SPeter Maydell         return CMD_STALL;
663961b4912SPeter Maydell     }
664961b4912SPeter Maydell 
6654acf93e1SPeter Maydell     if (!dte.valid) {
666961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
667961b4912SPeter Maydell                       "%s: invalid command attributes: "
6684acf93e1SPeter Maydell                       "invalid dte for %d\n", __func__, devid);
669961b4912SPeter Maydell         return CMD_CONTINUE;
670961b4912SPeter Maydell     }
671961b4912SPeter Maydell 
6724acf93e1SPeter Maydell     num_eventids = 1ULL << (dte.size + 1);
673961b4912SPeter Maydell     if (eventid >= num_eventids) {
674961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
675961b4912SPeter Maydell                       "%s: invalid command attributes: eventid %d >= %"
676961b4912SPeter Maydell                       PRId64 "\n",
677961b4912SPeter Maydell                       __func__, eventid, num_eventids);
678961b4912SPeter Maydell         return CMD_CONTINUE;
679961b4912SPeter Maydell     }
680961b4912SPeter Maydell 
681244194feSPeter Maydell     if (get_ite(s, eventid, &dte, &old_ite) != MEMTX_OK) {
682961b4912SPeter Maydell         return CMD_STALL;
683961b4912SPeter Maydell     }
684961b4912SPeter Maydell 
685244194feSPeter Maydell     if (!old_ite.valid || old_ite.inttype != ITE_INTTYPE_PHYSICAL) {
686961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
687961b4912SPeter Maydell                       "%s: invalid command attributes: invalid ITE\n",
688961b4912SPeter Maydell                       __func__);
689961b4912SPeter Maydell         return CMD_CONTINUE;
690961b4912SPeter Maydell     }
691961b4912SPeter Maydell 
692244194feSPeter Maydell     if (old_ite.icid >= s->ct.num_entries) {
693961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
694961b4912SPeter Maydell                       "%s: invalid ICID 0x%x in ITE (table corrupted?)\n",
695244194feSPeter Maydell                       __func__, old_ite.icid);
696961b4912SPeter Maydell         return CMD_CONTINUE;
697961b4912SPeter Maydell     }
698961b4912SPeter Maydell 
699961b4912SPeter Maydell     if (new_icid >= s->ct.num_entries) {
700961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
701961b4912SPeter Maydell                       "%s: invalid command attributes: ICID 0x%x\n",
702961b4912SPeter Maydell                       __func__, new_icid);
703961b4912SPeter Maydell         return CMD_CONTINUE;
704961b4912SPeter Maydell     }
705961b4912SPeter Maydell 
706244194feSPeter Maydell     if (get_cte(s, old_ite.icid, &old_cte) != MEMTX_OK) {
707961b4912SPeter Maydell         return CMD_STALL;
708961b4912SPeter Maydell     }
709d37cf49bSPeter Maydell     if (!old_cte.valid) {
710961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
711961b4912SPeter Maydell                       "%s: invalid command attributes: "
712d37cf49bSPeter Maydell                       "invalid CTE for old ICID 0x%x\n",
713244194feSPeter Maydell                       __func__, old_ite.icid);
714961b4912SPeter Maydell         return CMD_CONTINUE;
715961b4912SPeter Maydell     }
716961b4912SPeter Maydell 
717d37cf49bSPeter Maydell     if (get_cte(s, new_icid, &new_cte) != MEMTX_OK) {
718961b4912SPeter Maydell         return CMD_STALL;
719961b4912SPeter Maydell     }
720d37cf49bSPeter Maydell     if (!new_cte.valid) {
721961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
722961b4912SPeter Maydell                       "%s: invalid command attributes: "
723d37cf49bSPeter Maydell                       "invalid CTE for new ICID 0x%x\n",
724d37cf49bSPeter Maydell                       __func__, new_icid);
725961b4912SPeter Maydell         return CMD_CONTINUE;
726961b4912SPeter Maydell     }
727961b4912SPeter Maydell 
728d37cf49bSPeter Maydell     if (old_cte.rdbase >= s->gicv3->num_cpu) {
729961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
730d37cf49bSPeter Maydell                       "%s: CTE has invalid rdbase 0x%x\n",
731d37cf49bSPeter Maydell                       __func__, old_cte.rdbase);
732961b4912SPeter Maydell         return CMD_CONTINUE;
733961b4912SPeter Maydell     }
734961b4912SPeter Maydell 
735d37cf49bSPeter Maydell     if (new_cte.rdbase >= s->gicv3->num_cpu) {
736961b4912SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
737d37cf49bSPeter Maydell                       "%s: CTE has invalid rdbase 0x%x\n",
738d37cf49bSPeter Maydell                       __func__, new_cte.rdbase);
739961b4912SPeter Maydell         return CMD_CONTINUE;
740961b4912SPeter Maydell     }
741961b4912SPeter Maydell 
742d37cf49bSPeter Maydell     if (old_cte.rdbase != new_cte.rdbase) {
743961b4912SPeter Maydell         /* Move the LPI from the old redistributor to the new one */
744d37cf49bSPeter Maydell         gicv3_redist_mov_lpi(&s->gicv3->cpu[old_cte.rdbase],
745d37cf49bSPeter Maydell                              &s->gicv3->cpu[new_cte.rdbase],
746244194feSPeter Maydell                              old_ite.intid);
747961b4912SPeter Maydell     }
748961b4912SPeter Maydell 
749961b4912SPeter Maydell     /* Update the ICID field in the interrupt translation table entry */
7507eb54267SPeter Maydell     old_ite.icid = new_icid;
7517eb54267SPeter Maydell     return update_ite(s, eventid, &dte, &old_ite) ? CMD_CONTINUE : CMD_STALL;
752961b4912SPeter Maydell }
753961b4912SPeter Maydell 
7547eca39e0SShashi Mallela /*
7557eca39e0SShashi Mallela  * Current implementation blocks until all
7567eca39e0SShashi Mallela  * commands are processed
7577eca39e0SShashi Mallela  */
7587eca39e0SShashi Mallela static void process_cmdq(GICv3ITSState *s)
7597eca39e0SShashi Mallela {
7607eca39e0SShashi Mallela     uint32_t wr_offset = 0;
7617eca39e0SShashi Mallela     uint32_t rd_offset = 0;
7627eca39e0SShashi Mallela     uint32_t cq_offset = 0;
7637eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
7647eca39e0SShashi Mallela     uint8_t cmd;
76517fb5e36SShashi Mallela     int i;
7667eca39e0SShashi Mallela 
7678d2d6dd9SPeter Maydell     if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
7687eca39e0SShashi Mallela         return;
7697eca39e0SShashi Mallela     }
7707eca39e0SShashi Mallela 
7717eca39e0SShashi Mallela     wr_offset = FIELD_EX64(s->cwriter, GITS_CWRITER, OFFSET);
7727eca39e0SShashi Mallela 
77380dcd37fSPeter Maydell     if (wr_offset >= s->cq.num_entries) {
7747eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
7757eca39e0SShashi Mallela                       "%s: invalid write offset "
7767eca39e0SShashi Mallela                       "%d\n", __func__, wr_offset);
7777eca39e0SShashi Mallela         return;
7787eca39e0SShashi Mallela     }
7797eca39e0SShashi Mallela 
7807eca39e0SShashi Mallela     rd_offset = FIELD_EX64(s->creadr, GITS_CREADR, OFFSET);
7817eca39e0SShashi Mallela 
78280dcd37fSPeter Maydell     if (rd_offset >= s->cq.num_entries) {
7837eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
7847eca39e0SShashi Mallela                       "%s: invalid read offset "
7857eca39e0SShashi Mallela                       "%d\n", __func__, rd_offset);
7867eca39e0SShashi Mallela         return;
7877eca39e0SShashi Mallela     }
7887eca39e0SShashi Mallela 
7897eca39e0SShashi Mallela     while (wr_offset != rd_offset) {
790ef011555SPeter Maydell         ItsCmdResult result = CMD_CONTINUE;
791b6f96009SPeter Maydell         void *hostmem;
792b6f96009SPeter Maydell         hwaddr buflen;
793b6f96009SPeter Maydell         uint64_t cmdpkt[GITS_CMDQ_ENTRY_WORDS];
794ef011555SPeter Maydell 
7957eca39e0SShashi Mallela         cq_offset = (rd_offset * GITS_CMDQ_ENTRY_SIZE);
796b6f96009SPeter Maydell 
797b6f96009SPeter Maydell         buflen = GITS_CMDQ_ENTRY_SIZE;
798b6f96009SPeter Maydell         hostmem = address_space_map(as, s->cq.base_addr + cq_offset,
799b6f96009SPeter Maydell                                     &buflen, false, MEMTXATTRS_UNSPECIFIED);
800b6f96009SPeter Maydell         if (!hostmem || buflen != GITS_CMDQ_ENTRY_SIZE) {
801b6f96009SPeter Maydell             if (hostmem) {
802b6f96009SPeter Maydell                 address_space_unmap(as, hostmem, buflen, false, 0);
803b6f96009SPeter Maydell             }
804f0b4b2a2SPeter Maydell             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1);
805f0b4b2a2SPeter Maydell             qemu_log_mask(LOG_GUEST_ERROR,
806f0b4b2a2SPeter Maydell                           "%s: could not read command at 0x%" PRIx64 "\n",
807f0b4b2a2SPeter Maydell                           __func__, s->cq.base_addr + cq_offset);
808f0b4b2a2SPeter Maydell             break;
8097eca39e0SShashi Mallela         }
810b6f96009SPeter Maydell         for (i = 0; i < ARRAY_SIZE(cmdpkt); i++) {
811b6f96009SPeter Maydell             cmdpkt[i] = ldq_le_p(hostmem + i * sizeof(uint64_t));
812b6f96009SPeter Maydell         }
813b6f96009SPeter Maydell         address_space_unmap(as, hostmem, buflen, false, 0);
814f0b4b2a2SPeter Maydell 
815b6f96009SPeter Maydell         cmd = cmdpkt[0] & CMD_MASK;
8167eca39e0SShashi Mallela 
817195209d3SPeter Maydell         trace_gicv3_its_process_command(rd_offset, cmd);
818195209d3SPeter Maydell 
8197eca39e0SShashi Mallela         switch (cmd) {
8207eca39e0SShashi Mallela         case GITS_CMD_INT:
821b6f96009SPeter Maydell             result = process_its_cmd(s, cmdpkt, INTERRUPT);
8227eca39e0SShashi Mallela             break;
8237eca39e0SShashi Mallela         case GITS_CMD_CLEAR:
824b6f96009SPeter Maydell             result = process_its_cmd(s, cmdpkt, CLEAR);
8257eca39e0SShashi Mallela             break;
8267eca39e0SShashi Mallela         case GITS_CMD_SYNC:
8277eca39e0SShashi Mallela             /*
8287eca39e0SShashi Mallela              * Current implementation makes a blocking synchronous call
8297eca39e0SShashi Mallela              * for every command issued earlier, hence the internal state
8307eca39e0SShashi Mallela              * is already consistent by the time SYNC command is executed.
8317eca39e0SShashi Mallela              * Hence no further processing is required for SYNC command.
8327eca39e0SShashi Mallela              */
833e4050980SPeter Maydell             trace_gicv3_its_cmd_sync();
8347eca39e0SShashi Mallela             break;
8357eca39e0SShashi Mallela         case GITS_CMD_MAPD:
836b6f96009SPeter Maydell             result = process_mapd(s, cmdpkt);
8377eca39e0SShashi Mallela             break;
8387eca39e0SShashi Mallela         case GITS_CMD_MAPC:
839b6f96009SPeter Maydell             result = process_mapc(s, cmdpkt);
8407eca39e0SShashi Mallela             break;
8417eca39e0SShashi Mallela         case GITS_CMD_MAPTI:
842b6f96009SPeter Maydell             result = process_mapti(s, cmdpkt, false);
8437eca39e0SShashi Mallela             break;
8447eca39e0SShashi Mallela         case GITS_CMD_MAPI:
845b6f96009SPeter Maydell             result = process_mapti(s, cmdpkt, true);
8467eca39e0SShashi Mallela             break;
8477eca39e0SShashi Mallela         case GITS_CMD_DISCARD:
848b6f96009SPeter Maydell             result = process_its_cmd(s, cmdpkt, DISCARD);
8497eca39e0SShashi Mallela             break;
8507eca39e0SShashi Mallela         case GITS_CMD_INV:
8517eca39e0SShashi Mallela         case GITS_CMD_INVALL:
85217fb5e36SShashi Mallela             /*
85317fb5e36SShashi Mallela              * Current implementation doesn't cache any ITS tables,
85417fb5e36SShashi Mallela              * but the calculated lpi priority information. We only
85517fb5e36SShashi Mallela              * need to trigger lpi priority re-calculation to be in
85617fb5e36SShashi Mallela              * sync with LPI config table or pending table changes.
85717fb5e36SShashi Mallela              */
858e4050980SPeter Maydell             trace_gicv3_its_cmd_inv();
85917fb5e36SShashi Mallela             for (i = 0; i < s->gicv3->num_cpu; i++) {
86017fb5e36SShashi Mallela                 gicv3_redist_update_lpi(&s->gicv3->cpu[i]);
86117fb5e36SShashi Mallela             }
8627eca39e0SShashi Mallela             break;
863961b4912SPeter Maydell         case GITS_CMD_MOVI:
864b6f96009SPeter Maydell             result = process_movi(s, cmdpkt);
865961b4912SPeter Maydell             break;
866f6d1d9b4SPeter Maydell         case GITS_CMD_MOVALL:
867b6f96009SPeter Maydell             result = process_movall(s, cmdpkt);
868f6d1d9b4SPeter Maydell             break;
8697eca39e0SShashi Mallela         default:
870e4050980SPeter Maydell             trace_gicv3_its_cmd_unknown(cmd);
8717eca39e0SShashi Mallela             break;
8727eca39e0SShashi Mallela         }
873ef011555SPeter Maydell         if (result == CMD_CONTINUE) {
8747eca39e0SShashi Mallela             rd_offset++;
87580dcd37fSPeter Maydell             rd_offset %= s->cq.num_entries;
8767eca39e0SShashi Mallela             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, OFFSET, rd_offset);
8777eca39e0SShashi Mallela         } else {
878ef011555SPeter Maydell             /* CMD_STALL */
8797eca39e0SShashi Mallela             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1);
8807eca39e0SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
881ef011555SPeter Maydell                           "%s: 0x%x cmd processing failed, stalling\n",
882ef011555SPeter Maydell                           __func__, cmd);
8837eca39e0SShashi Mallela             break;
8847eca39e0SShashi Mallela         }
8857eca39e0SShashi Mallela     }
8867eca39e0SShashi Mallela }
8877eca39e0SShashi Mallela 
8881b08e436SShashi Mallela /*
8891b08e436SShashi Mallela  * This function extracts the ITS Device and Collection table specific
8901b08e436SShashi Mallela  * parameters (like base_addr, size etc) from GITS_BASER register.
8911b08e436SShashi Mallela  * It is called during ITS enable and also during post_load migration
8921b08e436SShashi Mallela  */
8931b08e436SShashi Mallela static void extract_table_params(GICv3ITSState *s)
8941b08e436SShashi Mallela {
8951b08e436SShashi Mallela     uint16_t num_pages = 0;
8961b08e436SShashi Mallela     uint8_t  page_sz_type;
8971b08e436SShashi Mallela     uint8_t type;
8981b08e436SShashi Mallela     uint32_t page_sz = 0;
8991b08e436SShashi Mallela     uint64_t value;
9001b08e436SShashi Mallela 
9011b08e436SShashi Mallela     for (int i = 0; i < 8; i++) {
902e5487a41SPeter Maydell         TableDesc *td;
903e5487a41SPeter Maydell         int idbits;
904e5487a41SPeter Maydell 
9051b08e436SShashi Mallela         value = s->baser[i];
9061b08e436SShashi Mallela 
9071b08e436SShashi Mallela         if (!value) {
9081b08e436SShashi Mallela             continue;
9091b08e436SShashi Mallela         }
9101b08e436SShashi Mallela 
9111b08e436SShashi Mallela         page_sz_type = FIELD_EX64(value, GITS_BASER, PAGESIZE);
9121b08e436SShashi Mallela 
9131b08e436SShashi Mallela         switch (page_sz_type) {
9141b08e436SShashi Mallela         case 0:
9151b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_4K;
9161b08e436SShashi Mallela             break;
9171b08e436SShashi Mallela 
9181b08e436SShashi Mallela         case 1:
9191b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_16K;
9201b08e436SShashi Mallela             break;
9211b08e436SShashi Mallela 
9221b08e436SShashi Mallela         case 2:
9231b08e436SShashi Mallela         case 3:
9241b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_64K;
9251b08e436SShashi Mallela             break;
9261b08e436SShashi Mallela 
9271b08e436SShashi Mallela         default:
9281b08e436SShashi Mallela             g_assert_not_reached();
9291b08e436SShashi Mallela         }
9301b08e436SShashi Mallela 
9311b08e436SShashi Mallela         num_pages = FIELD_EX64(value, GITS_BASER, SIZE) + 1;
9321b08e436SShashi Mallela 
9331b08e436SShashi Mallela         type = FIELD_EX64(value, GITS_BASER, TYPE);
9341b08e436SShashi Mallela 
9351b08e436SShashi Mallela         switch (type) {
9361b08e436SShashi Mallela         case GITS_BASER_TYPE_DEVICE:
937e5487a41SPeter Maydell             td = &s->dt;
938e5487a41SPeter Maydell             idbits = FIELD_EX64(s->typer, GITS_TYPER, DEVBITS) + 1;
93962df780eSPeter Maydell             break;
9401b08e436SShashi Mallela         case GITS_BASER_TYPE_COLLECTION:
941e5487a41SPeter Maydell             td = &s->ct;
9421b08e436SShashi Mallela             if (FIELD_EX64(s->typer, GITS_TYPER, CIL)) {
943e5487a41SPeter Maydell                 idbits = FIELD_EX64(s->typer, GITS_TYPER, CIDBITS) + 1;
9441b08e436SShashi Mallela             } else {
9451b08e436SShashi Mallela                 /* 16-bit CollectionId supported when CIL == 0 */
946e5487a41SPeter Maydell                 idbits = 16;
9471b08e436SShashi Mallela             }
9481b08e436SShashi Mallela             break;
9491b08e436SShashi Mallela         default:
950e5487a41SPeter Maydell             /*
951e5487a41SPeter Maydell              * GITS_BASER<n>.TYPE is read-only, so GITS_BASER_RO_MASK
952e5487a41SPeter Maydell              * ensures we will only see type values corresponding to
953e5487a41SPeter Maydell              * the values set up in gicv3_its_reset().
954e5487a41SPeter Maydell              */
955e5487a41SPeter Maydell             g_assert_not_reached();
9561b08e436SShashi Mallela         }
957e5487a41SPeter Maydell 
958e5487a41SPeter Maydell         memset(td, 0, sizeof(*td));
959e5487a41SPeter Maydell         /*
960e5487a41SPeter Maydell          * If GITS_BASER<n>.Valid is 0 for any <n> then we will not process
961e5487a41SPeter Maydell          * interrupts. (GITS_TYPER.HCC is 0 for this implementation, so we
962e5487a41SPeter Maydell          * do not have a special case where the GITS_BASER<n>.Valid bit is 0
963e5487a41SPeter Maydell          * for the register corresponding to the Collection table but we
964e5487a41SPeter Maydell          * still have to process interrupts using non-memory-backed
965e5487a41SPeter Maydell          * Collection table entries.)
966da4680ceSPeter Maydell          * The specification makes it UNPREDICTABLE to enable the ITS without
967da4680ceSPeter Maydell          * marking each BASER<n> as valid. We choose to handle these as if
968da4680ceSPeter Maydell          * the table was zero-sized, so commands using the table will fail
969da4680ceSPeter Maydell          * and interrupts requested via GITS_TRANSLATER writes will be ignored.
970da4680ceSPeter Maydell          * This happens automatically by leaving the num_entries field at
971da4680ceSPeter Maydell          * zero, which will be caught by the bounds checks we have before
972da4680ceSPeter Maydell          * every table lookup anyway.
973e5487a41SPeter Maydell          */
974da4680ceSPeter Maydell         if (!FIELD_EX64(value, GITS_BASER, VALID)) {
975e5487a41SPeter Maydell             continue;
976e5487a41SPeter Maydell         }
977e5487a41SPeter Maydell         td->page_sz = page_sz;
978e5487a41SPeter Maydell         td->indirect = FIELD_EX64(value, GITS_BASER, INDIRECT);
9799ae85431SPeter Maydell         td->entry_sz = FIELD_EX64(value, GITS_BASER, ENTRYSIZE) + 1;
980e5487a41SPeter Maydell         td->base_addr = baser_base_addr(value, page_sz);
981e5487a41SPeter Maydell         if (!td->indirect) {
98280dcd37fSPeter Maydell             td->num_entries = (num_pages * page_sz) / td->entry_sz;
983e5487a41SPeter Maydell         } else {
98480dcd37fSPeter Maydell             td->num_entries = (((num_pages * page_sz) /
985e5487a41SPeter Maydell                                   L1TABLE_ENTRY_SIZE) *
986e5487a41SPeter Maydell                                  (page_sz / td->entry_sz));
987e5487a41SPeter Maydell         }
9888b8bb014SPeter Maydell         td->num_entries = MIN(td->num_entries, 1ULL << idbits);
9891b08e436SShashi Mallela     }
9901b08e436SShashi Mallela }
9911b08e436SShashi Mallela 
9921b08e436SShashi Mallela static void extract_cmdq_params(GICv3ITSState *s)
9931b08e436SShashi Mallela {
9941b08e436SShashi Mallela     uint16_t num_pages = 0;
9951b08e436SShashi Mallela     uint64_t value = s->cbaser;
9961b08e436SShashi Mallela 
9971b08e436SShashi Mallela     num_pages = FIELD_EX64(value, GITS_CBASER, SIZE) + 1;
9981b08e436SShashi Mallela 
9991b08e436SShashi Mallela     memset(&s->cq, 0 , sizeof(s->cq));
10001b08e436SShashi Mallela 
1001da4680ceSPeter Maydell     if (FIELD_EX64(value, GITS_CBASER, VALID)) {
100280dcd37fSPeter Maydell         s->cq.num_entries = (num_pages * GITS_PAGE_SIZE_4K) /
10031b08e436SShashi Mallela                              GITS_CMDQ_ENTRY_SIZE;
10041b08e436SShashi Mallela         s->cq.base_addr = FIELD_EX64(value, GITS_CBASER, PHYADDR);
10051b08e436SShashi Mallela         s->cq.base_addr <<= R_GITS_CBASER_PHYADDR_SHIFT;
10061b08e436SShashi Mallela     }
10071b08e436SShashi Mallela }
10081b08e436SShashi Mallela 
10097e062b98SPeter Maydell static MemTxResult gicv3_its_translation_read(void *opaque, hwaddr offset,
10107e062b98SPeter Maydell                                               uint64_t *data, unsigned size,
10117e062b98SPeter Maydell                                               MemTxAttrs attrs)
10127e062b98SPeter Maydell {
10137e062b98SPeter Maydell     /*
10147e062b98SPeter Maydell      * GITS_TRANSLATER is write-only, and all other addresses
10157e062b98SPeter Maydell      * in the interrupt translation space frame are RES0.
10167e062b98SPeter Maydell      */
10177e062b98SPeter Maydell     *data = 0;
10187e062b98SPeter Maydell     return MEMTX_OK;
10197e062b98SPeter Maydell }
10207e062b98SPeter Maydell 
102118f6290aSShashi Mallela static MemTxResult gicv3_its_translation_write(void *opaque, hwaddr offset,
102218f6290aSShashi Mallela                                                uint64_t data, unsigned size,
102318f6290aSShashi Mallela                                                MemTxAttrs attrs)
102418f6290aSShashi Mallela {
1025c694cb4cSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
1026c694cb4cSShashi Mallela     bool result = true;
1027c694cb4cSShashi Mallela 
1028195209d3SPeter Maydell     trace_gicv3_its_translation_write(offset, data, size, attrs.requester_id);
1029195209d3SPeter Maydell 
1030c694cb4cSShashi Mallela     switch (offset) {
1031c694cb4cSShashi Mallela     case GITS_TRANSLATER:
10328d2d6dd9SPeter Maydell         if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) {
1033b6f96009SPeter Maydell             result = do_process_its_cmd(s, attrs.requester_id, data, NONE);
1034c694cb4cSShashi Mallela         }
1035c694cb4cSShashi Mallela         break;
1036c694cb4cSShashi Mallela     default:
1037c694cb4cSShashi Mallela         break;
1038c694cb4cSShashi Mallela     }
1039c694cb4cSShashi Mallela 
1040c694cb4cSShashi Mallela     if (result) {
104118f6290aSShashi Mallela         return MEMTX_OK;
1042c694cb4cSShashi Mallela     } else {
1043c694cb4cSShashi Mallela         return MEMTX_ERROR;
1044c694cb4cSShashi Mallela     }
104518f6290aSShashi Mallela }
104618f6290aSShashi Mallela 
104718f6290aSShashi Mallela static bool its_writel(GICv3ITSState *s, hwaddr offset,
104818f6290aSShashi Mallela                               uint64_t value, MemTxAttrs attrs)
104918f6290aSShashi Mallela {
105018f6290aSShashi Mallela     bool result = true;
10511b08e436SShashi Mallela     int index;
105218f6290aSShashi Mallela 
10531b08e436SShashi Mallela     switch (offset) {
10541b08e436SShashi Mallela     case GITS_CTLR:
10552f459cd1SShashi Mallela         if (value & R_GITS_CTLR_ENABLED_MASK) {
10568d2d6dd9SPeter Maydell             s->ctlr |= R_GITS_CTLR_ENABLED_MASK;
10571b08e436SShashi Mallela             extract_table_params(s);
10581b08e436SShashi Mallela             extract_cmdq_params(s);
10597eca39e0SShashi Mallela             process_cmdq(s);
10602f459cd1SShashi Mallela         } else {
10618d2d6dd9SPeter Maydell             s->ctlr &= ~R_GITS_CTLR_ENABLED_MASK;
10621b08e436SShashi Mallela         }
10631b08e436SShashi Mallela         break;
10641b08e436SShashi Mallela     case GITS_CBASER:
10651b08e436SShashi Mallela         /*
10661b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
10671b08e436SShashi Mallela          *                 already enabled
10681b08e436SShashi Mallela          */
10698d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
10701b08e436SShashi Mallela             s->cbaser = deposit64(s->cbaser, 0, 32, value);
10711b08e436SShashi Mallela             s->creadr = 0;
10721b08e436SShashi Mallela         }
10731b08e436SShashi Mallela         break;
10741b08e436SShashi Mallela     case GITS_CBASER + 4:
10751b08e436SShashi Mallela         /*
10761b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
10771b08e436SShashi Mallela          *                 already enabled
10781b08e436SShashi Mallela          */
10798d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
10801b08e436SShashi Mallela             s->cbaser = deposit64(s->cbaser, 32, 32, value);
10811b08e436SShashi Mallela             s->creadr = 0;
10821b08e436SShashi Mallela         }
10831b08e436SShashi Mallela         break;
10841b08e436SShashi Mallela     case GITS_CWRITER:
10851b08e436SShashi Mallela         s->cwriter = deposit64(s->cwriter, 0, 32,
10861b08e436SShashi Mallela                                (value & ~R_GITS_CWRITER_RETRY_MASK));
10877eca39e0SShashi Mallela         if (s->cwriter != s->creadr) {
10887eca39e0SShashi Mallela             process_cmdq(s);
10897eca39e0SShashi Mallela         }
10901b08e436SShashi Mallela         break;
10911b08e436SShashi Mallela     case GITS_CWRITER + 4:
10921b08e436SShashi Mallela         s->cwriter = deposit64(s->cwriter, 32, 32, value);
10931b08e436SShashi Mallela         break;
10941b08e436SShashi Mallela     case GITS_CREADR:
10951b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
10961b08e436SShashi Mallela             s->creadr = deposit64(s->creadr, 0, 32,
10971b08e436SShashi Mallela                                   (value & ~R_GITS_CREADR_STALLED_MASK));
10981b08e436SShashi Mallela         } else {
10991b08e436SShashi Mallela             /* RO register, ignore the write */
11001b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
11011b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
11021b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
11031b08e436SShashi Mallela         }
11041b08e436SShashi Mallela         break;
11051b08e436SShashi Mallela     case GITS_CREADR + 4:
11061b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
11071b08e436SShashi Mallela             s->creadr = deposit64(s->creadr, 32, 32, value);
11081b08e436SShashi Mallela         } else {
11091b08e436SShashi Mallela             /* RO register, ignore the write */
11101b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
11111b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
11121b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
11131b08e436SShashi Mallela         }
11141b08e436SShashi Mallela         break;
11151b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
11161b08e436SShashi Mallela         /*
11171b08e436SShashi Mallela          * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is
11181b08e436SShashi Mallela          *                 already enabled
11191b08e436SShashi Mallela          */
11208d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
11211b08e436SShashi Mallela             index = (offset - GITS_BASER) / 8;
11221b08e436SShashi Mallela 
11230ffe88e6SPeter Maydell             if (s->baser[index] == 0) {
11240ffe88e6SPeter Maydell                 /* Unimplemented GITS_BASERn: RAZ/WI */
11250ffe88e6SPeter Maydell                 break;
11260ffe88e6SPeter Maydell             }
11271b08e436SShashi Mallela             if (offset & 7) {
11281b08e436SShashi Mallela                 value <<= 32;
11291b08e436SShashi Mallela                 value &= ~GITS_BASER_RO_MASK;
11301b08e436SShashi Mallela                 s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(0, 32);
11311b08e436SShashi Mallela                 s->baser[index] |= value;
11321b08e436SShashi Mallela             } else {
11331b08e436SShashi Mallela                 value &= ~GITS_BASER_RO_MASK;
11341b08e436SShashi Mallela                 s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(32, 32);
11351b08e436SShashi Mallela                 s->baser[index] |= value;
11361b08e436SShashi Mallela             }
11371b08e436SShashi Mallela         }
11381b08e436SShashi Mallela         break;
11391b08e436SShashi Mallela     case GITS_IIDR:
11401b08e436SShashi Mallela     case GITS_IDREGS ... GITS_IDREGS + 0x2f:
11411b08e436SShashi Mallela         /* RO registers, ignore the write */
11421b08e436SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
11431b08e436SShashi Mallela                       "%s: invalid guest write to RO register at offset "
11441b08e436SShashi Mallela                       TARGET_FMT_plx "\n", __func__, offset);
11451b08e436SShashi Mallela         break;
11461b08e436SShashi Mallela     default:
11471b08e436SShashi Mallela         result = false;
11481b08e436SShashi Mallela         break;
11491b08e436SShashi Mallela     }
115018f6290aSShashi Mallela     return result;
115118f6290aSShashi Mallela }
115218f6290aSShashi Mallela 
115318f6290aSShashi Mallela static bool its_readl(GICv3ITSState *s, hwaddr offset,
115418f6290aSShashi Mallela                              uint64_t *data, MemTxAttrs attrs)
115518f6290aSShashi Mallela {
115618f6290aSShashi Mallela     bool result = true;
11571b08e436SShashi Mallela     int index;
115818f6290aSShashi Mallela 
11591b08e436SShashi Mallela     switch (offset) {
11601b08e436SShashi Mallela     case GITS_CTLR:
11611b08e436SShashi Mallela         *data = s->ctlr;
11621b08e436SShashi Mallela         break;
11631b08e436SShashi Mallela     case GITS_IIDR:
11641b08e436SShashi Mallela         *data = gicv3_iidr();
11651b08e436SShashi Mallela         break;
11661b08e436SShashi Mallela     case GITS_IDREGS ... GITS_IDREGS + 0x2f:
11671b08e436SShashi Mallela         /* ID registers */
116850a3a309SPeter Maydell         *data = gicv3_idreg(offset - GITS_IDREGS, GICV3_PIDR0_ITS);
11691b08e436SShashi Mallela         break;
11701b08e436SShashi Mallela     case GITS_TYPER:
11711b08e436SShashi Mallela         *data = extract64(s->typer, 0, 32);
11721b08e436SShashi Mallela         break;
11731b08e436SShashi Mallela     case GITS_TYPER + 4:
11741b08e436SShashi Mallela         *data = extract64(s->typer, 32, 32);
11751b08e436SShashi Mallela         break;
11761b08e436SShashi Mallela     case GITS_CBASER:
11771b08e436SShashi Mallela         *data = extract64(s->cbaser, 0, 32);
11781b08e436SShashi Mallela         break;
11791b08e436SShashi Mallela     case GITS_CBASER + 4:
11801b08e436SShashi Mallela         *data = extract64(s->cbaser, 32, 32);
11811b08e436SShashi Mallela         break;
11821b08e436SShashi Mallela     case GITS_CREADR:
11831b08e436SShashi Mallela         *data = extract64(s->creadr, 0, 32);
11841b08e436SShashi Mallela         break;
11851b08e436SShashi Mallela     case GITS_CREADR + 4:
11861b08e436SShashi Mallela         *data = extract64(s->creadr, 32, 32);
11871b08e436SShashi Mallela         break;
11881b08e436SShashi Mallela     case GITS_CWRITER:
11891b08e436SShashi Mallela         *data = extract64(s->cwriter, 0, 32);
11901b08e436SShashi Mallela         break;
11911b08e436SShashi Mallela     case GITS_CWRITER + 4:
11921b08e436SShashi Mallela         *data = extract64(s->cwriter, 32, 32);
11931b08e436SShashi Mallela         break;
11941b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
11951b08e436SShashi Mallela         index = (offset - GITS_BASER) / 8;
11961b08e436SShashi Mallela         if (offset & 7) {
11971b08e436SShashi Mallela             *data = extract64(s->baser[index], 32, 32);
11981b08e436SShashi Mallela         } else {
11991b08e436SShashi Mallela             *data = extract64(s->baser[index], 0, 32);
12001b08e436SShashi Mallela         }
12011b08e436SShashi Mallela         break;
12021b08e436SShashi Mallela     default:
12031b08e436SShashi Mallela         result = false;
12041b08e436SShashi Mallela         break;
12051b08e436SShashi Mallela     }
120618f6290aSShashi Mallela     return result;
120718f6290aSShashi Mallela }
120818f6290aSShashi Mallela 
120918f6290aSShashi Mallela static bool its_writell(GICv3ITSState *s, hwaddr offset,
121018f6290aSShashi Mallela                                uint64_t value, MemTxAttrs attrs)
121118f6290aSShashi Mallela {
121218f6290aSShashi Mallela     bool result = true;
12131b08e436SShashi Mallela     int index;
121418f6290aSShashi Mallela 
12151b08e436SShashi Mallela     switch (offset) {
12161b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
12171b08e436SShashi Mallela         /*
12181b08e436SShashi Mallela          * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is
12191b08e436SShashi Mallela          *                 already enabled
12201b08e436SShashi Mallela          */
12218d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
12221b08e436SShashi Mallela             index = (offset - GITS_BASER) / 8;
12230ffe88e6SPeter Maydell             if (s->baser[index] == 0) {
12240ffe88e6SPeter Maydell                 /* Unimplemented GITS_BASERn: RAZ/WI */
12250ffe88e6SPeter Maydell                 break;
12260ffe88e6SPeter Maydell             }
12271b08e436SShashi Mallela             s->baser[index] &= GITS_BASER_RO_MASK;
12281b08e436SShashi Mallela             s->baser[index] |= (value & ~GITS_BASER_RO_MASK);
12291b08e436SShashi Mallela         }
12301b08e436SShashi Mallela         break;
12311b08e436SShashi Mallela     case GITS_CBASER:
12321b08e436SShashi Mallela         /*
12331b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
12341b08e436SShashi Mallela          *                 already enabled
12351b08e436SShashi Mallela          */
12368d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
12371b08e436SShashi Mallela             s->cbaser = value;
12381b08e436SShashi Mallela             s->creadr = 0;
12391b08e436SShashi Mallela         }
12401b08e436SShashi Mallela         break;
12411b08e436SShashi Mallela     case GITS_CWRITER:
12421b08e436SShashi Mallela         s->cwriter = value & ~R_GITS_CWRITER_RETRY_MASK;
12437eca39e0SShashi Mallela         if (s->cwriter != s->creadr) {
12447eca39e0SShashi Mallela             process_cmdq(s);
12457eca39e0SShashi Mallela         }
12461b08e436SShashi Mallela         break;
12471b08e436SShashi Mallela     case GITS_CREADR:
12481b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
12491b08e436SShashi Mallela             s->creadr = value & ~R_GITS_CREADR_STALLED_MASK;
12501b08e436SShashi Mallela         } else {
12511b08e436SShashi Mallela             /* RO register, ignore the write */
12521b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
12531b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
12541b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
12551b08e436SShashi Mallela         }
12561b08e436SShashi Mallela         break;
12571b08e436SShashi Mallela     case GITS_TYPER:
12581b08e436SShashi Mallela         /* RO registers, ignore the write */
12591b08e436SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
12601b08e436SShashi Mallela                       "%s: invalid guest write to RO register at offset "
12611b08e436SShashi Mallela                       TARGET_FMT_plx "\n", __func__, offset);
12621b08e436SShashi Mallela         break;
12631b08e436SShashi Mallela     default:
12641b08e436SShashi Mallela         result = false;
12651b08e436SShashi Mallela         break;
12661b08e436SShashi Mallela     }
126718f6290aSShashi Mallela     return result;
126818f6290aSShashi Mallela }
126918f6290aSShashi Mallela 
127018f6290aSShashi Mallela static bool its_readll(GICv3ITSState *s, hwaddr offset,
127118f6290aSShashi Mallela                               uint64_t *data, MemTxAttrs attrs)
127218f6290aSShashi Mallela {
127318f6290aSShashi Mallela     bool result = true;
12741b08e436SShashi Mallela     int index;
127518f6290aSShashi Mallela 
12761b08e436SShashi Mallela     switch (offset) {
12771b08e436SShashi Mallela     case GITS_TYPER:
12781b08e436SShashi Mallela         *data = s->typer;
12791b08e436SShashi Mallela         break;
12801b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
12811b08e436SShashi Mallela         index = (offset - GITS_BASER) / 8;
12821b08e436SShashi Mallela         *data = s->baser[index];
12831b08e436SShashi Mallela         break;
12841b08e436SShashi Mallela     case GITS_CBASER:
12851b08e436SShashi Mallela         *data = s->cbaser;
12861b08e436SShashi Mallela         break;
12871b08e436SShashi Mallela     case GITS_CREADR:
12881b08e436SShashi Mallela         *data = s->creadr;
12891b08e436SShashi Mallela         break;
12901b08e436SShashi Mallela     case GITS_CWRITER:
12911b08e436SShashi Mallela         *data = s->cwriter;
12921b08e436SShashi Mallela         break;
12931b08e436SShashi Mallela     default:
12941b08e436SShashi Mallela         result = false;
12951b08e436SShashi Mallela         break;
12961b08e436SShashi Mallela     }
129718f6290aSShashi Mallela     return result;
129818f6290aSShashi Mallela }
129918f6290aSShashi Mallela 
130018f6290aSShashi Mallela static MemTxResult gicv3_its_read(void *opaque, hwaddr offset, uint64_t *data,
130118f6290aSShashi Mallela                                   unsigned size, MemTxAttrs attrs)
130218f6290aSShashi Mallela {
130318f6290aSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
130418f6290aSShashi Mallela     bool result;
130518f6290aSShashi Mallela 
130618f6290aSShashi Mallela     switch (size) {
130718f6290aSShashi Mallela     case 4:
130818f6290aSShashi Mallela         result = its_readl(s, offset, data, attrs);
130918f6290aSShashi Mallela         break;
131018f6290aSShashi Mallela     case 8:
131118f6290aSShashi Mallela         result = its_readll(s, offset, data, attrs);
131218f6290aSShashi Mallela         break;
131318f6290aSShashi Mallela     default:
131418f6290aSShashi Mallela         result = false;
131518f6290aSShashi Mallela         break;
131618f6290aSShashi Mallela     }
131718f6290aSShashi Mallela 
131818f6290aSShashi Mallela     if (!result) {
131918f6290aSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
132018f6290aSShashi Mallela                       "%s: invalid guest read at offset " TARGET_FMT_plx
132118f6290aSShashi Mallela                       " size %u\n", __func__, offset, size);
1322195209d3SPeter Maydell         trace_gicv3_its_badread(offset, size);
132318f6290aSShashi Mallela         /*
132418f6290aSShashi Mallela          * The spec requires that reserved registers are RAZ/WI;
132518f6290aSShashi Mallela          * so use false returns from leaf functions as a way to
132618f6290aSShashi Mallela          * trigger the guest-error logging but don't return it to
132718f6290aSShashi Mallela          * the caller, or we'll cause a spurious guest data abort.
132818f6290aSShashi Mallela          */
132918f6290aSShashi Mallela         *data = 0;
1330195209d3SPeter Maydell     } else {
1331195209d3SPeter Maydell         trace_gicv3_its_read(offset, *data, size);
133218f6290aSShashi Mallela     }
133318f6290aSShashi Mallela     return MEMTX_OK;
133418f6290aSShashi Mallela }
133518f6290aSShashi Mallela 
133618f6290aSShashi Mallela static MemTxResult gicv3_its_write(void *opaque, hwaddr offset, uint64_t data,
133718f6290aSShashi Mallela                                    unsigned size, MemTxAttrs attrs)
133818f6290aSShashi Mallela {
133918f6290aSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
134018f6290aSShashi Mallela     bool result;
134118f6290aSShashi Mallela 
134218f6290aSShashi Mallela     switch (size) {
134318f6290aSShashi Mallela     case 4:
134418f6290aSShashi Mallela         result = its_writel(s, offset, data, attrs);
134518f6290aSShashi Mallela         break;
134618f6290aSShashi Mallela     case 8:
134718f6290aSShashi Mallela         result = its_writell(s, offset, data, attrs);
134818f6290aSShashi Mallela         break;
134918f6290aSShashi Mallela     default:
135018f6290aSShashi Mallela         result = false;
135118f6290aSShashi Mallela         break;
135218f6290aSShashi Mallela     }
135318f6290aSShashi Mallela 
135418f6290aSShashi Mallela     if (!result) {
135518f6290aSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
135618f6290aSShashi Mallela                       "%s: invalid guest write at offset " TARGET_FMT_plx
135718f6290aSShashi Mallela                       " size %u\n", __func__, offset, size);
1358195209d3SPeter Maydell         trace_gicv3_its_badwrite(offset, data, size);
135918f6290aSShashi Mallela         /*
136018f6290aSShashi Mallela          * The spec requires that reserved registers are RAZ/WI;
136118f6290aSShashi Mallela          * so use false returns from leaf functions as a way to
136218f6290aSShashi Mallela          * trigger the guest-error logging but don't return it to
136318f6290aSShashi Mallela          * the caller, or we'll cause a spurious guest data abort.
136418f6290aSShashi Mallela          */
1365195209d3SPeter Maydell     } else {
1366195209d3SPeter Maydell         trace_gicv3_its_write(offset, data, size);
136718f6290aSShashi Mallela     }
136818f6290aSShashi Mallela     return MEMTX_OK;
136918f6290aSShashi Mallela }
137018f6290aSShashi Mallela 
137118f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_control_ops = {
137218f6290aSShashi Mallela     .read_with_attrs = gicv3_its_read,
137318f6290aSShashi Mallela     .write_with_attrs = gicv3_its_write,
137418f6290aSShashi Mallela     .valid.min_access_size = 4,
137518f6290aSShashi Mallela     .valid.max_access_size = 8,
137618f6290aSShashi Mallela     .impl.min_access_size = 4,
137718f6290aSShashi Mallela     .impl.max_access_size = 8,
137818f6290aSShashi Mallela     .endianness = DEVICE_NATIVE_ENDIAN,
137918f6290aSShashi Mallela };
138018f6290aSShashi Mallela 
138118f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_translation_ops = {
13827e062b98SPeter Maydell     .read_with_attrs = gicv3_its_translation_read,
138318f6290aSShashi Mallela     .write_with_attrs = gicv3_its_translation_write,
138418f6290aSShashi Mallela     .valid.min_access_size = 2,
138518f6290aSShashi Mallela     .valid.max_access_size = 4,
138618f6290aSShashi Mallela     .impl.min_access_size = 2,
138718f6290aSShashi Mallela     .impl.max_access_size = 4,
138818f6290aSShashi Mallela     .endianness = DEVICE_NATIVE_ENDIAN,
138918f6290aSShashi Mallela };
139018f6290aSShashi Mallela 
139118f6290aSShashi Mallela static void gicv3_arm_its_realize(DeviceState *dev, Error **errp)
139218f6290aSShashi Mallela {
139318f6290aSShashi Mallela     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
139418f6290aSShashi Mallela     int i;
139518f6290aSShashi Mallela 
139618f6290aSShashi Mallela     for (i = 0; i < s->gicv3->num_cpu; i++) {
139718f6290aSShashi Mallela         if (!(s->gicv3->cpu[i].gicr_typer & GICR_TYPER_PLPIS)) {
139818f6290aSShashi Mallela             error_setg(errp, "Physical LPI not supported by CPU %d", i);
139918f6290aSShashi Mallela             return;
140018f6290aSShashi Mallela         }
140118f6290aSShashi Mallela     }
140218f6290aSShashi Mallela 
140318f6290aSShashi Mallela     gicv3_its_init_mmio(s, &gicv3_its_control_ops, &gicv3_its_translation_ops);
140418f6290aSShashi Mallela 
140518f6290aSShashi Mallela     /* set the ITS default features supported */
1406764d6ba1SPeter Maydell     s->typer = FIELD_DP64(s->typer, GITS_TYPER, PHYSICAL, 1);
140718f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, ITT_ENTRY_SIZE,
140818f6290aSShashi Mallela                           ITS_ITT_ENTRY_SIZE - 1);
140918f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, IDBITS, ITS_IDBITS);
141018f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, DEVBITS, ITS_DEVBITS);
141118f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIL, 1);
141218f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIDBITS, ITS_CIDBITS);
141318f6290aSShashi Mallela }
141418f6290aSShashi Mallela 
141518f6290aSShashi Mallela static void gicv3_its_reset(DeviceState *dev)
141618f6290aSShashi Mallela {
141718f6290aSShashi Mallela     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
141818f6290aSShashi Mallela     GICv3ITSClass *c = ARM_GICV3_ITS_GET_CLASS(s);
141918f6290aSShashi Mallela 
142018f6290aSShashi Mallela     c->parent_reset(dev);
142118f6290aSShashi Mallela 
142218f6290aSShashi Mallela     /* Quiescent bit reset to 1 */
142318f6290aSShashi Mallela     s->ctlr = FIELD_DP32(s->ctlr, GITS_CTLR, QUIESCENT, 1);
142418f6290aSShashi Mallela 
142518f6290aSShashi Mallela     /*
142618f6290aSShashi Mallela      * setting GITS_BASER0.Type = 0b001 (Device)
142718f6290aSShashi Mallela      *         GITS_BASER1.Type = 0b100 (Collection Table)
142818f6290aSShashi Mallela      *         GITS_BASER<n>.Type,where n = 3 to 7 are 0b00 (Unimplemented)
142918f6290aSShashi Mallela      *         GITS_BASER<0,1>.Page_Size = 64KB
143018f6290aSShashi Mallela      * and default translation table entry size to 16 bytes
143118f6290aSShashi Mallela      */
143218f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, TYPE,
143318f6290aSShashi Mallela                              GITS_BASER_TYPE_DEVICE);
143418f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, PAGESIZE,
143518f6290aSShashi Mallela                              GITS_BASER_PAGESIZE_64K);
143618f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, ENTRYSIZE,
143718f6290aSShashi Mallela                              GITS_DTE_SIZE - 1);
143818f6290aSShashi Mallela 
143918f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, TYPE,
144018f6290aSShashi Mallela                              GITS_BASER_TYPE_COLLECTION);
144118f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, PAGESIZE,
144218f6290aSShashi Mallela                              GITS_BASER_PAGESIZE_64K);
144318f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, ENTRYSIZE,
144418f6290aSShashi Mallela                              GITS_CTE_SIZE - 1);
144518f6290aSShashi Mallela }
144618f6290aSShashi Mallela 
14471b08e436SShashi Mallela static void gicv3_its_post_load(GICv3ITSState *s)
14481b08e436SShashi Mallela {
14498d2d6dd9SPeter Maydell     if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) {
14501b08e436SShashi Mallela         extract_table_params(s);
14511b08e436SShashi Mallela         extract_cmdq_params(s);
14521b08e436SShashi Mallela     }
14531b08e436SShashi Mallela }
14541b08e436SShashi Mallela 
145518f6290aSShashi Mallela static Property gicv3_its_props[] = {
145618f6290aSShashi Mallela     DEFINE_PROP_LINK("parent-gicv3", GICv3ITSState, gicv3, "arm-gicv3",
145718f6290aSShashi Mallela                      GICv3State *),
145818f6290aSShashi Mallela     DEFINE_PROP_END_OF_LIST(),
145918f6290aSShashi Mallela };
146018f6290aSShashi Mallela 
146118f6290aSShashi Mallela static void gicv3_its_class_init(ObjectClass *klass, void *data)
146218f6290aSShashi Mallela {
146318f6290aSShashi Mallela     DeviceClass *dc = DEVICE_CLASS(klass);
146418f6290aSShashi Mallela     GICv3ITSClass *ic = ARM_GICV3_ITS_CLASS(klass);
14651b08e436SShashi Mallela     GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass);
146618f6290aSShashi Mallela 
146718f6290aSShashi Mallela     dc->realize = gicv3_arm_its_realize;
146818f6290aSShashi Mallela     device_class_set_props(dc, gicv3_its_props);
146918f6290aSShashi Mallela     device_class_set_parent_reset(dc, gicv3_its_reset, &ic->parent_reset);
14701b08e436SShashi Mallela     icc->post_load = gicv3_its_post_load;
147118f6290aSShashi Mallela }
147218f6290aSShashi Mallela 
147318f6290aSShashi Mallela static const TypeInfo gicv3_its_info = {
147418f6290aSShashi Mallela     .name = TYPE_ARM_GICV3_ITS,
147518f6290aSShashi Mallela     .parent = TYPE_ARM_GICV3_ITS_COMMON,
147618f6290aSShashi Mallela     .instance_size = sizeof(GICv3ITSState),
147718f6290aSShashi Mallela     .class_init = gicv3_its_class_init,
147818f6290aSShashi Mallela     .class_size = sizeof(GICv3ITSClass),
147918f6290aSShashi Mallela };
148018f6290aSShashi Mallela 
148118f6290aSShashi Mallela static void gicv3_its_register_types(void)
148218f6290aSShashi Mallela {
148318f6290aSShashi Mallela     type_register_static(&gicv3_its_info);
148418f6290aSShashi Mallela }
148518f6290aSShashi Mallela 
148618f6290aSShashi Mallela type_init(gicv3_its_register_types)
1487