xref: /qemu/hw/intc/arm_gicv3_its.c (revision d050f80f8c9b4e535bf2f20d5a400c231e7a5838)
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 
86*d050f80fSPeter Maydell static uint64_t table_entry_addr(GICv3ITSState *s, TableDesc *td,
87*d050f80fSPeter Maydell                                  uint32_t idx, MemTxResult *res)
88*d050f80fSPeter Maydell {
89*d050f80fSPeter Maydell     /*
90*d050f80fSPeter Maydell      * Given a TableDesc describing one of the ITS in-guest-memory
91*d050f80fSPeter Maydell      * tables and an index into it, return the guest address
92*d050f80fSPeter Maydell      * corresponding to that table entry.
93*d050f80fSPeter Maydell      * If there was a memory error reading the L1 table of an
94*d050f80fSPeter Maydell      * indirect table, *res is set accordingly, and we return -1.
95*d050f80fSPeter Maydell      * If the L1 table entry is marked not valid, we return -1 with
96*d050f80fSPeter Maydell      * *res set to MEMTX_OK.
97*d050f80fSPeter Maydell      *
98*d050f80fSPeter Maydell      * The specification defines the format of level 1 entries of a
99*d050f80fSPeter Maydell      * 2-level table, but the format of level 2 entries and the format
100*d050f80fSPeter Maydell      * of flat-mapped tables is IMPDEF.
101*d050f80fSPeter Maydell      */
102*d050f80fSPeter Maydell     AddressSpace *as = &s->gicv3->dma_as;
103*d050f80fSPeter Maydell     uint32_t l2idx;
104*d050f80fSPeter Maydell     uint64_t l2;
105*d050f80fSPeter Maydell     uint32_t num_l2_entries;
106*d050f80fSPeter Maydell 
107*d050f80fSPeter Maydell     *res = MEMTX_OK;
108*d050f80fSPeter Maydell 
109*d050f80fSPeter Maydell     if (!td->indirect) {
110*d050f80fSPeter Maydell         /* Single level table */
111*d050f80fSPeter Maydell         return td->base_addr + idx * td->entry_sz;
112*d050f80fSPeter Maydell     }
113*d050f80fSPeter Maydell 
114*d050f80fSPeter Maydell     /* Two level table */
115*d050f80fSPeter Maydell     l2idx = idx / (td->page_sz / L1TABLE_ENTRY_SIZE);
116*d050f80fSPeter Maydell 
117*d050f80fSPeter Maydell     l2 = address_space_ldq_le(as,
118*d050f80fSPeter Maydell                               td->base_addr + (l2idx * L1TABLE_ENTRY_SIZE),
119*d050f80fSPeter Maydell                               MEMTXATTRS_UNSPECIFIED, res);
120*d050f80fSPeter Maydell     if (*res != MEMTX_OK) {
121*d050f80fSPeter Maydell         return -1;
122*d050f80fSPeter Maydell     }
123*d050f80fSPeter Maydell     if (!(l2 & L2_TABLE_VALID_MASK)) {
124*d050f80fSPeter Maydell         return -1;
125*d050f80fSPeter Maydell     }
126*d050f80fSPeter Maydell 
127*d050f80fSPeter Maydell     num_l2_entries = td->page_sz / td->entry_sz;
128*d050f80fSPeter Maydell     return (l2 & ((1ULL << 51) - 1)) + (idx % num_l2_entries) * td->entry_sz;
129*d050f80fSPeter Maydell }
130*d050f80fSPeter Maydell 
131c694cb4cSShashi Mallela static bool get_cte(GICv3ITSState *s, uint16_t icid, uint64_t *cte,
132c694cb4cSShashi Mallela                     MemTxResult *res)
133c694cb4cSShashi Mallela {
134c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
135*d050f80fSPeter Maydell     uint64_t entry_addr = table_entry_addr(s, &s->ct, icid, res);
136c694cb4cSShashi Mallela 
137*d050f80fSPeter Maydell     if (entry_addr == -1) {
138*d050f80fSPeter Maydell         return false; /* not valid */
139c694cb4cSShashi Mallela     }
140c694cb4cSShashi Mallela 
141*d050f80fSPeter Maydell     *cte = address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, res);
142437dc0eaSPeter Maydell     return FIELD_EX64(*cte, CTE, VALID);
143c694cb4cSShashi Mallela }
144c694cb4cSShashi Mallela 
145c694cb4cSShashi Mallela static bool update_ite(GICv3ITSState *s, uint32_t eventid, uint64_t dte,
146c694cb4cSShashi Mallela                        IteEntry ite)
147c694cb4cSShashi Mallela {
148c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
149c694cb4cSShashi Mallela     uint64_t itt_addr;
150c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
151c694cb4cSShashi Mallela 
152e07f8445SPeter Maydell     itt_addr = FIELD_EX64(dte, DTE, ITTADDR);
153c694cb4cSShashi Mallela     itt_addr <<= ITTADDR_SHIFT; /* 256 byte aligned */
154c694cb4cSShashi Mallela 
155c694cb4cSShashi Mallela     address_space_stq_le(as, itt_addr + (eventid * (sizeof(uint64_t) +
156c694cb4cSShashi Mallela                          sizeof(uint32_t))), ite.itel, MEMTXATTRS_UNSPECIFIED,
157c694cb4cSShashi Mallela                          &res);
158c694cb4cSShashi Mallela 
159c694cb4cSShashi Mallela     if (res == MEMTX_OK) {
160c694cb4cSShashi Mallela         address_space_stl_le(as, itt_addr + (eventid * (sizeof(uint64_t) +
161c694cb4cSShashi Mallela                              sizeof(uint32_t))) + sizeof(uint32_t), ite.iteh,
162c694cb4cSShashi Mallela                              MEMTXATTRS_UNSPECIFIED, &res);
163c694cb4cSShashi Mallela     }
164c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
165c694cb4cSShashi Mallela         return false;
166c694cb4cSShashi Mallela     } else {
167c694cb4cSShashi Mallela         return true;
168c694cb4cSShashi Mallela     }
169c694cb4cSShashi Mallela }
170c694cb4cSShashi Mallela 
171c694cb4cSShashi Mallela static bool get_ite(GICv3ITSState *s, uint32_t eventid, uint64_t dte,
172c694cb4cSShashi Mallela                     uint16_t *icid, uint32_t *pIntid, MemTxResult *res)
173c694cb4cSShashi Mallela {
174c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
175c694cb4cSShashi Mallela     uint64_t itt_addr;
176c694cb4cSShashi Mallela     bool status = false;
177c694cb4cSShashi Mallela     IteEntry ite = {};
178c694cb4cSShashi Mallela 
179e07f8445SPeter Maydell     itt_addr = FIELD_EX64(dte, DTE, ITTADDR);
180c694cb4cSShashi Mallela     itt_addr <<= ITTADDR_SHIFT; /* 256 byte aligned */
181c694cb4cSShashi Mallela 
182c694cb4cSShashi Mallela     ite.itel = address_space_ldq_le(as, itt_addr +
183c694cb4cSShashi Mallela                                     (eventid * (sizeof(uint64_t) +
184c694cb4cSShashi Mallela                                     sizeof(uint32_t))), MEMTXATTRS_UNSPECIFIED,
185c694cb4cSShashi Mallela                                     res);
186c694cb4cSShashi Mallela 
187c694cb4cSShashi Mallela     if (*res == MEMTX_OK) {
188c694cb4cSShashi Mallela         ite.iteh = address_space_ldl_le(as, itt_addr +
189c694cb4cSShashi Mallela                                         (eventid * (sizeof(uint64_t) +
190c694cb4cSShashi Mallela                                         sizeof(uint32_t))) + sizeof(uint32_t),
191c694cb4cSShashi Mallela                                         MEMTXATTRS_UNSPECIFIED, res);
192c694cb4cSShashi Mallela 
193c694cb4cSShashi Mallela         if (*res == MEMTX_OK) {
194764d6ba1SPeter Maydell             if (FIELD_EX64(ite.itel, ITE_L, VALID)) {
195764d6ba1SPeter Maydell                 int inttype = FIELD_EX64(ite.itel, ITE_L, INTTYPE);
196764d6ba1SPeter Maydell                 if (inttype == ITE_INTTYPE_PHYSICAL) {
197764d6ba1SPeter Maydell                     *pIntid = FIELD_EX64(ite.itel, ITE_L, INTID);
198764d6ba1SPeter Maydell                     *icid = FIELD_EX32(ite.iteh, ITE_H, ICID);
199c694cb4cSShashi Mallela                     status = true;
200c694cb4cSShashi Mallela                 }
201c694cb4cSShashi Mallela             }
202c694cb4cSShashi Mallela         }
203c694cb4cSShashi Mallela     }
204c694cb4cSShashi Mallela     return status;
205c694cb4cSShashi Mallela }
206c694cb4cSShashi Mallela 
207c694cb4cSShashi Mallela static uint64_t get_dte(GICv3ITSState *s, uint32_t devid, MemTxResult *res)
208c694cb4cSShashi Mallela {
209c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
210*d050f80fSPeter Maydell     uint64_t entry_addr = table_entry_addr(s, &s->dt, devid, res);
211c694cb4cSShashi Mallela 
212*d050f80fSPeter Maydell     if (entry_addr == -1) {
213*d050f80fSPeter Maydell         return 0; /* a DTE entry with the Valid bit clear */
214c694cb4cSShashi Mallela     }
215*d050f80fSPeter Maydell     return address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, res);
216c694cb4cSShashi Mallela }
217c694cb4cSShashi Mallela 
218c694cb4cSShashi Mallela /*
219c694cb4cSShashi Mallela  * This function handles the processing of following commands based on
220c694cb4cSShashi Mallela  * the ItsCmdType parameter passed:-
221c694cb4cSShashi Mallela  * 1. triggering of lpi interrupt translation via ITS INT command
222c694cb4cSShashi Mallela  * 2. triggering of lpi interrupt translation via gits_translater register
223c694cb4cSShashi Mallela  * 3. handling of ITS CLEAR command
224c694cb4cSShashi Mallela  * 4. handling of ITS DISCARD command
225c694cb4cSShashi Mallela  */
226ef011555SPeter Maydell static ItsCmdResult process_its_cmd(GICv3ITSState *s, uint64_t value,
227ef011555SPeter Maydell                                     uint32_t offset, ItsCmdType cmd)
228c694cb4cSShashi Mallela {
229c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
230c694cb4cSShashi Mallela     uint32_t devid, eventid;
231c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
232c694cb4cSShashi Mallela     bool dte_valid;
233c694cb4cSShashi Mallela     uint64_t dte = 0;
2348f809f69SPeter Maydell     uint64_t num_eventids;
235c694cb4cSShashi Mallela     uint16_t icid = 0;
236c694cb4cSShashi Mallela     uint32_t pIntid = 0;
237c694cb4cSShashi Mallela     bool ite_valid = false;
238c694cb4cSShashi Mallela     uint64_t cte = 0;
239c694cb4cSShashi Mallela     bool cte_valid = false;
24017fb5e36SShashi Mallela     uint64_t rdbase;
241c694cb4cSShashi Mallela 
242c694cb4cSShashi Mallela     if (cmd == NONE) {
243c694cb4cSShashi Mallela         devid = offset;
244c694cb4cSShashi Mallela     } else {
245c694cb4cSShashi Mallela         devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
246c694cb4cSShashi Mallela 
247c694cb4cSShashi Mallela         offset += NUM_BYTES_IN_DW;
248c694cb4cSShashi Mallela         value = address_space_ldq_le(as, s->cq.base_addr + offset,
249c694cb4cSShashi Mallela                                      MEMTXATTRS_UNSPECIFIED, &res);
250c694cb4cSShashi Mallela     }
251c694cb4cSShashi Mallela 
252c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
253593a7cc2SPeter Maydell         return CMD_STALL;
254c694cb4cSShashi Mallela     }
255c694cb4cSShashi Mallela 
256c694cb4cSShashi Mallela     eventid = (value & EVENTID_MASK);
257c694cb4cSShashi Mallela 
258c694cb4cSShashi Mallela     dte = get_dte(s, devid, &res);
259c694cb4cSShashi Mallela 
260c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
261593a7cc2SPeter Maydell         return CMD_STALL;
262c694cb4cSShashi Mallela     }
263e07f8445SPeter Maydell     dte_valid = FIELD_EX64(dte, DTE, VALID);
264c694cb4cSShashi Mallela 
265be0ed8fbSPeter Maydell     if (!dte_valid) {
266229c57b1SAlex Bennée         qemu_log_mask(LOG_GUEST_ERROR,
267229c57b1SAlex Bennée                       "%s: invalid command attributes: "
268be0ed8fbSPeter Maydell                       "invalid dte: %"PRIx64" for %d\n",
269be0ed8fbSPeter Maydell                       __func__, dte, devid);
270593a7cc2SPeter Maydell         return CMD_CONTINUE;
271c694cb4cSShashi Mallela     }
272c694cb4cSShashi Mallela 
273be0ed8fbSPeter Maydell     num_eventids = 1ULL << (FIELD_EX64(dte, DTE, SIZE) + 1);
274229c57b1SAlex Bennée 
275be0ed8fbSPeter Maydell     ite_valid = get_ite(s, eventid, dte, &icid, &pIntid, &res);
276be0ed8fbSPeter Maydell     if (res != MEMTX_OK) {
277be0ed8fbSPeter Maydell         return CMD_STALL;
278be0ed8fbSPeter Maydell     }
279be0ed8fbSPeter Maydell 
280be0ed8fbSPeter Maydell     if (!ite_valid) {
281be0ed8fbSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
282be0ed8fbSPeter Maydell                       "%s: invalid command attributes: invalid ITE\n",
283be0ed8fbSPeter Maydell                       __func__);
284be0ed8fbSPeter Maydell         return CMD_CONTINUE;
285be0ed8fbSPeter Maydell     }
286be0ed8fbSPeter Maydell 
287be0ed8fbSPeter Maydell     cte_valid = get_cte(s, icid, &cte, &res);
288be0ed8fbSPeter Maydell     if (res != MEMTX_OK) {
289be0ed8fbSPeter Maydell         return CMD_STALL;
290be0ed8fbSPeter Maydell     }
291be0ed8fbSPeter Maydell     if (!cte_valid) {
292be0ed8fbSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
293be0ed8fbSPeter Maydell                       "%s: invalid command attributes: "
294be0ed8fbSPeter Maydell                       "invalid cte: %"PRIx64"\n",
295be0ed8fbSPeter Maydell                       __func__, cte);
296be0ed8fbSPeter Maydell         return CMD_CONTINUE;
297be0ed8fbSPeter Maydell     }
298be0ed8fbSPeter Maydell 
29980dcd37fSPeter Maydell     if (devid >= s->dt.num_ids) {
300229c57b1SAlex Bennée         qemu_log_mask(LOG_GUEST_ERROR,
30180dcd37fSPeter Maydell                       "%s: invalid command attributes: devid %d>=%d",
30280dcd37fSPeter Maydell                       __func__, devid, s->dt.num_ids);
303593a7cc2SPeter Maydell         return CMD_CONTINUE;
304be0ed8fbSPeter Maydell     }
305be0ed8fbSPeter Maydell     if (eventid >= num_eventids) {
306229c57b1SAlex Bennée         qemu_log_mask(LOG_GUEST_ERROR,
3078f809f69SPeter Maydell                       "%s: invalid command attributes: eventid %d >= %"
3088f809f69SPeter Maydell                       PRId64 "\n",
3098f809f69SPeter Maydell                       __func__, eventid, num_eventids);
310593a7cc2SPeter Maydell         return CMD_CONTINUE;
311be0ed8fbSPeter Maydell     }
312be0ed8fbSPeter Maydell 
313c694cb4cSShashi Mallela     /*
314c694cb4cSShashi Mallela      * Current implementation only supports rdbase == procnum
315c694cb4cSShashi Mallela      * Hence rdbase physical address is ignored
316c694cb4cSShashi Mallela      */
317437dc0eaSPeter Maydell     rdbase = FIELD_EX64(cte, CTE, RDBASE);
31817fb5e36SShashi Mallela 
319a120157bSPeter Maydell     if (rdbase >= s->gicv3->num_cpu) {
320593a7cc2SPeter Maydell         return CMD_CONTINUE;
32117fb5e36SShashi Mallela     }
32217fb5e36SShashi Mallela 
32317fb5e36SShashi Mallela     if ((cmd == CLEAR) || (cmd == DISCARD)) {
32417fb5e36SShashi Mallela         gicv3_redist_process_lpi(&s->gicv3->cpu[rdbase], pIntid, 0);
32517fb5e36SShashi Mallela     } else {
32617fb5e36SShashi Mallela         gicv3_redist_process_lpi(&s->gicv3->cpu[rdbase], pIntid, 1);
32717fb5e36SShashi Mallela     }
32817fb5e36SShashi Mallela 
329c694cb4cSShashi Mallela     if (cmd == DISCARD) {
330c694cb4cSShashi Mallela         IteEntry ite = {};
331c694cb4cSShashi Mallela         /* remove mapping from interrupt translation table */
332593a7cc2SPeter Maydell         return update_ite(s, eventid, dte, ite) ? CMD_CONTINUE : CMD_STALL;
333c694cb4cSShashi Mallela     }
334593a7cc2SPeter Maydell     return CMD_CONTINUE;
335c694cb4cSShashi Mallela }
336c694cb4cSShashi Mallela 
337ef011555SPeter Maydell static ItsCmdResult process_mapti(GICv3ITSState *s, uint64_t value,
338ef011555SPeter Maydell                                   uint32_t offset, bool ignore_pInt)
339c694cb4cSShashi Mallela {
340c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
341c694cb4cSShashi Mallela     uint32_t devid, eventid;
342c694cb4cSShashi Mallela     uint32_t pIntid = 0;
3438f809f69SPeter Maydell     uint64_t num_eventids;
344905720f1SPeter Maydell     uint32_t num_intids;
345c694cb4cSShashi Mallela     bool dte_valid;
346c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
347c694cb4cSShashi Mallela     uint16_t icid = 0;
348c694cb4cSShashi Mallela     uint64_t dte = 0;
3490241f731SPeter Maydell     IteEntry ite = {};
350c694cb4cSShashi Mallela 
351c694cb4cSShashi Mallela     devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
352c694cb4cSShashi Mallela     offset += NUM_BYTES_IN_DW;
353c694cb4cSShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
354c694cb4cSShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
355c694cb4cSShashi Mallela 
356c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
3570241f731SPeter Maydell         return CMD_STALL;
358c694cb4cSShashi Mallela     }
359c694cb4cSShashi Mallela 
360c694cb4cSShashi Mallela     eventid = (value & EVENTID_MASK);
361c694cb4cSShashi Mallela 
362b87fab1cSPeter Maydell     if (ignore_pInt) {
363b87fab1cSPeter Maydell         pIntid = eventid;
364b87fab1cSPeter Maydell     } else {
365c694cb4cSShashi Mallela         pIntid = ((value & pINTID_MASK) >> pINTID_SHIFT);
366c694cb4cSShashi Mallela     }
367c694cb4cSShashi Mallela 
368c694cb4cSShashi Mallela     offset += NUM_BYTES_IN_DW;
369c694cb4cSShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
370c694cb4cSShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
371c694cb4cSShashi Mallela 
372c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
3730241f731SPeter Maydell         return CMD_STALL;
374c694cb4cSShashi Mallela     }
375c694cb4cSShashi Mallela 
376c694cb4cSShashi Mallela     icid = value & ICID_MASK;
377c694cb4cSShashi Mallela 
378c694cb4cSShashi Mallela     dte = get_dte(s, devid, &res);
379c694cb4cSShashi Mallela 
380c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
3810241f731SPeter Maydell         return CMD_STALL;
382c694cb4cSShashi Mallela     }
383e07f8445SPeter Maydell     dte_valid = FIELD_EX64(dte, DTE, VALID);
3848f809f69SPeter Maydell     num_eventids = 1ULL << (FIELD_EX64(dte, DTE, SIZE) + 1);
385905720f1SPeter Maydell     num_intids = 1ULL << (GICD_TYPER_IDBITS + 1);
386c694cb4cSShashi Mallela 
38780dcd37fSPeter Maydell     if ((devid >= s->dt.num_ids) || (icid >= s->ct.num_ids)
3888f809f69SPeter Maydell             || !dte_valid || (eventid >= num_eventids) ||
389905720f1SPeter Maydell             (((pIntid < GICV3_LPI_INTID_START) || (pIntid >= num_intids)) &&
390b87fab1cSPeter Maydell              (pIntid != INTID_SPURIOUS))) {
391c694cb4cSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
392c694cb4cSShashi Mallela                       "%s: invalid command attributes "
393c694cb4cSShashi Mallela                       "devid %d or icid %d or eventid %d or pIntid %d or"
394c694cb4cSShashi Mallela                       "unmapped dte %d\n", __func__, devid, icid, eventid,
395c694cb4cSShashi Mallela                       pIntid, dte_valid);
396c694cb4cSShashi Mallela         /*
397c694cb4cSShashi Mallela          * in this implementation, in case of error
398c694cb4cSShashi Mallela          * we ignore this command and move onto the next
399c694cb4cSShashi Mallela          * command in the queue
400c694cb4cSShashi Mallela          */
4010241f731SPeter Maydell         return CMD_CONTINUE;
4020241f731SPeter Maydell     }
4030241f731SPeter Maydell 
404c694cb4cSShashi Mallela     /* add ite entry to interrupt translation table */
405764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, VALID, dte_valid);
406764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, INTTYPE, ITE_INTTYPE_PHYSICAL);
407764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, INTID, pIntid);
408764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, DOORBELL, INTID_SPURIOUS);
409764d6ba1SPeter Maydell     ite.iteh = FIELD_DP32(ite.iteh, ITE_H, ICID, icid);
410c694cb4cSShashi Mallela 
4110241f731SPeter Maydell     return update_ite(s, eventid, dte, ite) ? CMD_CONTINUE : CMD_STALL;
412c694cb4cSShashi Mallela }
413c694cb4cSShashi Mallela 
4147eca39e0SShashi Mallela static bool update_cte(GICv3ITSState *s, uint16_t icid, bool valid,
4157eca39e0SShashi Mallela                        uint64_t rdbase)
4167eca39e0SShashi Mallela {
4177eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
418*d050f80fSPeter Maydell     uint64_t entry_addr;
4197eca39e0SShashi Mallela     uint64_t cte = 0;
4207eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
4217eca39e0SShashi Mallela 
4227eca39e0SShashi Mallela     if (!s->ct.valid) {
4237eca39e0SShashi Mallela         return true;
4247eca39e0SShashi Mallela     }
4257eca39e0SShashi Mallela 
4267eca39e0SShashi Mallela     if (valid) {
4277eca39e0SShashi Mallela         /* add mapping entry to collection table */
428437dc0eaSPeter Maydell         cte = FIELD_DP64(cte, CTE, VALID, 1);
429437dc0eaSPeter Maydell         cte = FIELD_DP64(cte, CTE, RDBASE, rdbase);
4307eca39e0SShashi Mallela     }
4317eca39e0SShashi Mallela 
432*d050f80fSPeter Maydell     entry_addr = table_entry_addr(s, &s->ct, icid, &res);
4337eca39e0SShashi Mallela     if (res != MEMTX_OK) {
434*d050f80fSPeter Maydell         /* memory access error: stall */
4357eca39e0SShashi Mallela         return false;
4367eca39e0SShashi Mallela     }
437*d050f80fSPeter Maydell     if (entry_addr == -1) {
438*d050f80fSPeter Maydell         /* No L2 table for this index: discard write and continue */
4397eca39e0SShashi Mallela         return true;
4407eca39e0SShashi Mallela     }
441*d050f80fSPeter Maydell 
442*d050f80fSPeter Maydell     address_space_stq_le(as, entry_addr, cte, MEMTXATTRS_UNSPECIFIED, &res);
443*d050f80fSPeter Maydell     return res == MEMTX_OK;
4447eca39e0SShashi Mallela }
4457eca39e0SShashi Mallela 
446ef011555SPeter Maydell static ItsCmdResult process_mapc(GICv3ITSState *s, uint32_t offset)
4477eca39e0SShashi Mallela {
4487eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
4497eca39e0SShashi Mallela     uint16_t icid;
4507eca39e0SShashi Mallela     uint64_t rdbase;
4517eca39e0SShashi Mallela     bool valid;
4527eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
4537eca39e0SShashi Mallela     uint64_t value;
4547eca39e0SShashi Mallela 
4557eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
4567eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
4577eca39e0SShashi Mallela 
4587eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
4597eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
4607eca39e0SShashi Mallela 
4617eca39e0SShashi Mallela     if (res != MEMTX_OK) {
462f6675196SPeter Maydell         return CMD_STALL;
4637eca39e0SShashi Mallela     }
4647eca39e0SShashi Mallela 
4657eca39e0SShashi Mallela     icid = value & ICID_MASK;
4667eca39e0SShashi Mallela 
4677eca39e0SShashi Mallela     rdbase = (value & R_MAPC_RDBASE_MASK) >> R_MAPC_RDBASE_SHIFT;
4687eca39e0SShashi Mallela     rdbase &= RDBASE_PROCNUM_MASK;
4697eca39e0SShashi Mallela 
4707eca39e0SShashi Mallela     valid = (value & CMD_FIELD_VALID_MASK);
4717eca39e0SShashi Mallela 
47280dcd37fSPeter Maydell     if ((icid >= s->ct.num_ids) || (rdbase >= s->gicv3->num_cpu)) {
4737eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
4747eca39e0SShashi Mallela                       "ITS MAPC: invalid collection table attributes "
4757eca39e0SShashi Mallela                       "icid %d rdbase %" PRIu64 "\n",  icid, rdbase);
4767eca39e0SShashi Mallela         /*
4777eca39e0SShashi Mallela          * in this implementation, in case of error
4787eca39e0SShashi Mallela          * we ignore this command and move onto the next
4797eca39e0SShashi Mallela          * command in the queue
4807eca39e0SShashi Mallela          */
481f6675196SPeter Maydell         return CMD_CONTINUE;
4827eca39e0SShashi Mallela     }
4837eca39e0SShashi Mallela 
484f6675196SPeter Maydell     return update_cte(s, icid, valid, rdbase) ? CMD_CONTINUE : CMD_STALL;
4857eca39e0SShashi Mallela }
4867eca39e0SShashi Mallela 
4877eca39e0SShashi Mallela static bool update_dte(GICv3ITSState *s, uint32_t devid, bool valid,
4887eca39e0SShashi Mallela                        uint8_t size, uint64_t itt_addr)
4897eca39e0SShashi Mallela {
4907eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
491*d050f80fSPeter Maydell     uint64_t entry_addr;
4927eca39e0SShashi Mallela     uint64_t dte = 0;
4937eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
4947eca39e0SShashi Mallela 
4957eca39e0SShashi Mallela     if (s->dt.valid) {
4967eca39e0SShashi Mallela         if (valid) {
4977eca39e0SShashi Mallela             /* add mapping entry to device table */
498e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, VALID, 1);
499e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, SIZE, size);
500e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, ITTADDR, itt_addr);
5017eca39e0SShashi Mallela         }
5027eca39e0SShashi Mallela     } else {
5037eca39e0SShashi Mallela         return true;
5047eca39e0SShashi Mallela     }
5057eca39e0SShashi Mallela 
506*d050f80fSPeter Maydell     entry_addr = table_entry_addr(s, &s->dt, devid, &res);
5077eca39e0SShashi Mallela     if (res != MEMTX_OK) {
508*d050f80fSPeter Maydell         /* memory access error: stall */
5097eca39e0SShashi Mallela         return false;
5107eca39e0SShashi Mallela     }
511*d050f80fSPeter Maydell     if (entry_addr == -1) {
512*d050f80fSPeter Maydell         /* No L2 table for this index: discard write and continue */
5137eca39e0SShashi Mallela         return true;
5147eca39e0SShashi Mallela     }
515*d050f80fSPeter Maydell     address_space_stq_le(as, entry_addr, dte, MEMTXATTRS_UNSPECIFIED, &res);
516*d050f80fSPeter Maydell     return res == MEMTX_OK;
5177eca39e0SShashi Mallela }
5187eca39e0SShashi Mallela 
519ef011555SPeter Maydell static ItsCmdResult process_mapd(GICv3ITSState *s, uint64_t value,
520ef011555SPeter Maydell                                  uint32_t offset)
5217eca39e0SShashi Mallela {
5227eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
5237eca39e0SShashi Mallela     uint32_t devid;
5247eca39e0SShashi Mallela     uint8_t size;
5257eca39e0SShashi Mallela     uint64_t itt_addr;
5267eca39e0SShashi Mallela     bool valid;
5277eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
5287eca39e0SShashi Mallela 
5297eca39e0SShashi Mallela     devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
5307eca39e0SShashi Mallela 
5317eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
5327eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
5337eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
5347eca39e0SShashi Mallela 
5357eca39e0SShashi Mallela     if (res != MEMTX_OK) {
53600d46e72SPeter Maydell         return CMD_STALL;
5377eca39e0SShashi Mallela     }
5387eca39e0SShashi Mallela 
5397eca39e0SShashi Mallela     size = (value & SIZE_MASK);
5407eca39e0SShashi Mallela 
5417eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
5427eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
5437eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
5447eca39e0SShashi Mallela 
5457eca39e0SShashi Mallela     if (res != MEMTX_OK) {
54600d46e72SPeter Maydell         return CMD_STALL;
5477eca39e0SShashi Mallela     }
5487eca39e0SShashi Mallela 
5497eca39e0SShashi Mallela     itt_addr = (value & ITTADDR_MASK) >> ITTADDR_SHIFT;
5507eca39e0SShashi Mallela 
5517eca39e0SShashi Mallela     valid = (value & CMD_FIELD_VALID_MASK);
5527eca39e0SShashi Mallela 
55380dcd37fSPeter Maydell     if ((devid >= s->dt.num_ids) ||
5547eca39e0SShashi Mallela         (size > FIELD_EX64(s->typer, GITS_TYPER, IDBITS))) {
5557eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
5567eca39e0SShashi Mallela                       "ITS MAPD: invalid device table attributes "
5577eca39e0SShashi Mallela                       "devid %d or size %d\n", devid, size);
5587eca39e0SShashi Mallela         /*
5597eca39e0SShashi Mallela          * in this implementation, in case of error
5607eca39e0SShashi Mallela          * we ignore this command and move onto the next
5617eca39e0SShashi Mallela          * command in the queue
5627eca39e0SShashi Mallela          */
56300d46e72SPeter Maydell         return CMD_CONTINUE;
5647eca39e0SShashi Mallela     }
5657eca39e0SShashi Mallela 
56600d46e72SPeter Maydell     return update_dte(s, devid, valid, size, itt_addr) ? CMD_CONTINUE : CMD_STALL;
5677eca39e0SShashi Mallela }
5687eca39e0SShashi Mallela 
5697eca39e0SShashi Mallela /*
5707eca39e0SShashi Mallela  * Current implementation blocks until all
5717eca39e0SShashi Mallela  * commands are processed
5727eca39e0SShashi Mallela  */
5737eca39e0SShashi Mallela static void process_cmdq(GICv3ITSState *s)
5747eca39e0SShashi Mallela {
5757eca39e0SShashi Mallela     uint32_t wr_offset = 0;
5767eca39e0SShashi Mallela     uint32_t rd_offset = 0;
5777eca39e0SShashi Mallela     uint32_t cq_offset = 0;
5787eca39e0SShashi Mallela     uint64_t data;
5797eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
5807eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
5817eca39e0SShashi Mallela     uint8_t cmd;
58217fb5e36SShashi Mallela     int i;
5837eca39e0SShashi Mallela 
5848d2d6dd9SPeter Maydell     if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
5857eca39e0SShashi Mallela         return;
5867eca39e0SShashi Mallela     }
5877eca39e0SShashi Mallela 
5887eca39e0SShashi Mallela     wr_offset = FIELD_EX64(s->cwriter, GITS_CWRITER, OFFSET);
5897eca39e0SShashi Mallela 
59080dcd37fSPeter Maydell     if (wr_offset >= s->cq.num_entries) {
5917eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
5927eca39e0SShashi Mallela                       "%s: invalid write offset "
5937eca39e0SShashi Mallela                       "%d\n", __func__, wr_offset);
5947eca39e0SShashi Mallela         return;
5957eca39e0SShashi Mallela     }
5967eca39e0SShashi Mallela 
5977eca39e0SShashi Mallela     rd_offset = FIELD_EX64(s->creadr, GITS_CREADR, OFFSET);
5987eca39e0SShashi Mallela 
59980dcd37fSPeter Maydell     if (rd_offset >= s->cq.num_entries) {
6007eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
6017eca39e0SShashi Mallela                       "%s: invalid read offset "
6027eca39e0SShashi Mallela                       "%d\n", __func__, rd_offset);
6037eca39e0SShashi Mallela         return;
6047eca39e0SShashi Mallela     }
6057eca39e0SShashi Mallela 
6067eca39e0SShashi Mallela     while (wr_offset != rd_offset) {
607ef011555SPeter Maydell         ItsCmdResult result = CMD_CONTINUE;
608ef011555SPeter Maydell 
6097eca39e0SShashi Mallela         cq_offset = (rd_offset * GITS_CMDQ_ENTRY_SIZE);
6107eca39e0SShashi Mallela         data = address_space_ldq_le(as, s->cq.base_addr + cq_offset,
6117eca39e0SShashi Mallela                                     MEMTXATTRS_UNSPECIFIED, &res);
6127eca39e0SShashi Mallela         if (res != MEMTX_OK) {
613f0b4b2a2SPeter Maydell             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1);
614f0b4b2a2SPeter Maydell             qemu_log_mask(LOG_GUEST_ERROR,
615f0b4b2a2SPeter Maydell                           "%s: could not read command at 0x%" PRIx64 "\n",
616f0b4b2a2SPeter Maydell                           __func__, s->cq.base_addr + cq_offset);
617f0b4b2a2SPeter Maydell             break;
6187eca39e0SShashi Mallela         }
619f0b4b2a2SPeter Maydell 
6207eca39e0SShashi Mallela         cmd = (data & CMD_MASK);
6217eca39e0SShashi Mallela 
6227eca39e0SShashi Mallela         switch (cmd) {
6237eca39e0SShashi Mallela         case GITS_CMD_INT:
6247d62b2dcSPeter Maydell             result = process_its_cmd(s, data, cq_offset, INTERRUPT);
6257eca39e0SShashi Mallela             break;
6267eca39e0SShashi Mallela         case GITS_CMD_CLEAR:
6277d62b2dcSPeter Maydell             result = process_its_cmd(s, data, cq_offset, CLEAR);
6287eca39e0SShashi Mallela             break;
6297eca39e0SShashi Mallela         case GITS_CMD_SYNC:
6307eca39e0SShashi Mallela             /*
6317eca39e0SShashi Mallela              * Current implementation makes a blocking synchronous call
6327eca39e0SShashi Mallela              * for every command issued earlier, hence the internal state
6337eca39e0SShashi Mallela              * is already consistent by the time SYNC command is executed.
6347eca39e0SShashi Mallela              * Hence no further processing is required for SYNC command.
6357eca39e0SShashi Mallela              */
6367eca39e0SShashi Mallela             break;
6377eca39e0SShashi Mallela         case GITS_CMD_MAPD:
6387eca39e0SShashi Mallela             result = process_mapd(s, data, cq_offset);
6397eca39e0SShashi Mallela             break;
6407eca39e0SShashi Mallela         case GITS_CMD_MAPC:
6417eca39e0SShashi Mallela             result = process_mapc(s, cq_offset);
6427eca39e0SShashi Mallela             break;
6437eca39e0SShashi Mallela         case GITS_CMD_MAPTI:
644c694cb4cSShashi Mallela             result = process_mapti(s, data, cq_offset, false);
6457eca39e0SShashi Mallela             break;
6467eca39e0SShashi Mallela         case GITS_CMD_MAPI:
647c694cb4cSShashi Mallela             result = process_mapti(s, data, cq_offset, true);
6487eca39e0SShashi Mallela             break;
6497eca39e0SShashi Mallela         case GITS_CMD_DISCARD:
650c694cb4cSShashi Mallela             result = process_its_cmd(s, data, cq_offset, DISCARD);
6517eca39e0SShashi Mallela             break;
6527eca39e0SShashi Mallela         case GITS_CMD_INV:
6537eca39e0SShashi Mallela         case GITS_CMD_INVALL:
65417fb5e36SShashi Mallela             /*
65517fb5e36SShashi Mallela              * Current implementation doesn't cache any ITS tables,
65617fb5e36SShashi Mallela              * but the calculated lpi priority information. We only
65717fb5e36SShashi Mallela              * need to trigger lpi priority re-calculation to be in
65817fb5e36SShashi Mallela              * sync with LPI config table or pending table changes.
65917fb5e36SShashi Mallela              */
66017fb5e36SShashi Mallela             for (i = 0; i < s->gicv3->num_cpu; i++) {
66117fb5e36SShashi Mallela                 gicv3_redist_update_lpi(&s->gicv3->cpu[i]);
66217fb5e36SShashi Mallela             }
6637eca39e0SShashi Mallela             break;
6647eca39e0SShashi Mallela         default:
6657eca39e0SShashi Mallela             break;
6667eca39e0SShashi Mallela         }
667ef011555SPeter Maydell         if (result == CMD_CONTINUE) {
6687eca39e0SShashi Mallela             rd_offset++;
66980dcd37fSPeter Maydell             rd_offset %= s->cq.num_entries;
6707eca39e0SShashi Mallela             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, OFFSET, rd_offset);
6717eca39e0SShashi Mallela         } else {
672ef011555SPeter Maydell             /* CMD_STALL */
6737eca39e0SShashi Mallela             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1);
6747eca39e0SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
675ef011555SPeter Maydell                           "%s: 0x%x cmd processing failed, stalling\n",
676ef011555SPeter Maydell                           __func__, cmd);
6777eca39e0SShashi Mallela             break;
6787eca39e0SShashi Mallela         }
6797eca39e0SShashi Mallela     }
6807eca39e0SShashi Mallela }
6817eca39e0SShashi Mallela 
6821b08e436SShashi Mallela /*
6831b08e436SShashi Mallela  * This function extracts the ITS Device and Collection table specific
6841b08e436SShashi Mallela  * parameters (like base_addr, size etc) from GITS_BASER register.
6851b08e436SShashi Mallela  * It is called during ITS enable and also during post_load migration
6861b08e436SShashi Mallela  */
6871b08e436SShashi Mallela static void extract_table_params(GICv3ITSState *s)
6881b08e436SShashi Mallela {
6891b08e436SShashi Mallela     uint16_t num_pages = 0;
6901b08e436SShashi Mallela     uint8_t  page_sz_type;
6911b08e436SShashi Mallela     uint8_t type;
6921b08e436SShashi Mallela     uint32_t page_sz = 0;
6931b08e436SShashi Mallela     uint64_t value;
6941b08e436SShashi Mallela 
6951b08e436SShashi Mallela     for (int i = 0; i < 8; i++) {
696e5487a41SPeter Maydell         TableDesc *td;
697e5487a41SPeter Maydell         int idbits;
698e5487a41SPeter Maydell 
6991b08e436SShashi Mallela         value = s->baser[i];
7001b08e436SShashi Mallela 
7011b08e436SShashi Mallela         if (!value) {
7021b08e436SShashi Mallela             continue;
7031b08e436SShashi Mallela         }
7041b08e436SShashi Mallela 
7051b08e436SShashi Mallela         page_sz_type = FIELD_EX64(value, GITS_BASER, PAGESIZE);
7061b08e436SShashi Mallela 
7071b08e436SShashi Mallela         switch (page_sz_type) {
7081b08e436SShashi Mallela         case 0:
7091b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_4K;
7101b08e436SShashi Mallela             break;
7111b08e436SShashi Mallela 
7121b08e436SShashi Mallela         case 1:
7131b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_16K;
7141b08e436SShashi Mallela             break;
7151b08e436SShashi Mallela 
7161b08e436SShashi Mallela         case 2:
7171b08e436SShashi Mallela         case 3:
7181b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_64K;
7191b08e436SShashi Mallela             break;
7201b08e436SShashi Mallela 
7211b08e436SShashi Mallela         default:
7221b08e436SShashi Mallela             g_assert_not_reached();
7231b08e436SShashi Mallela         }
7241b08e436SShashi Mallela 
7251b08e436SShashi Mallela         num_pages = FIELD_EX64(value, GITS_BASER, SIZE) + 1;
7261b08e436SShashi Mallela 
7271b08e436SShashi Mallela         type = FIELD_EX64(value, GITS_BASER, TYPE);
7281b08e436SShashi Mallela 
7291b08e436SShashi Mallela         switch (type) {
7301b08e436SShashi Mallela         case GITS_BASER_TYPE_DEVICE:
731e5487a41SPeter Maydell             td = &s->dt;
732e5487a41SPeter Maydell             idbits = FIELD_EX64(s->typer, GITS_TYPER, DEVBITS) + 1;
73362df780eSPeter Maydell             break;
7341b08e436SShashi Mallela         case GITS_BASER_TYPE_COLLECTION:
735e5487a41SPeter Maydell             td = &s->ct;
7361b08e436SShashi Mallela             if (FIELD_EX64(s->typer, GITS_TYPER, CIL)) {
737e5487a41SPeter Maydell                 idbits = FIELD_EX64(s->typer, GITS_TYPER, CIDBITS) + 1;
7381b08e436SShashi Mallela             } else {
7391b08e436SShashi Mallela                 /* 16-bit CollectionId supported when CIL == 0 */
740e5487a41SPeter Maydell                 idbits = 16;
7411b08e436SShashi Mallela             }
7421b08e436SShashi Mallela             break;
7431b08e436SShashi Mallela         default:
744e5487a41SPeter Maydell             /*
745e5487a41SPeter Maydell              * GITS_BASER<n>.TYPE is read-only, so GITS_BASER_RO_MASK
746e5487a41SPeter Maydell              * ensures we will only see type values corresponding to
747e5487a41SPeter Maydell              * the values set up in gicv3_its_reset().
748e5487a41SPeter Maydell              */
749e5487a41SPeter Maydell             g_assert_not_reached();
7501b08e436SShashi Mallela         }
751e5487a41SPeter Maydell 
752e5487a41SPeter Maydell         memset(td, 0, sizeof(*td));
753e5487a41SPeter Maydell         td->valid = FIELD_EX64(value, GITS_BASER, VALID);
754e5487a41SPeter Maydell         /*
755e5487a41SPeter Maydell          * If GITS_BASER<n>.Valid is 0 for any <n> then we will not process
756e5487a41SPeter Maydell          * interrupts. (GITS_TYPER.HCC is 0 for this implementation, so we
757e5487a41SPeter Maydell          * do not have a special case where the GITS_BASER<n>.Valid bit is 0
758e5487a41SPeter Maydell          * for the register corresponding to the Collection table but we
759e5487a41SPeter Maydell          * still have to process interrupts using non-memory-backed
760e5487a41SPeter Maydell          * Collection table entries.)
761e5487a41SPeter Maydell          */
762e5487a41SPeter Maydell         if (!td->valid) {
763e5487a41SPeter Maydell             continue;
764e5487a41SPeter Maydell         }
765e5487a41SPeter Maydell         td->page_sz = page_sz;
766e5487a41SPeter Maydell         td->indirect = FIELD_EX64(value, GITS_BASER, INDIRECT);
7679ae85431SPeter Maydell         td->entry_sz = FIELD_EX64(value, GITS_BASER, ENTRYSIZE) + 1;
768e5487a41SPeter Maydell         td->base_addr = baser_base_addr(value, page_sz);
769e5487a41SPeter Maydell         if (!td->indirect) {
77080dcd37fSPeter Maydell             td->num_entries = (num_pages * page_sz) / td->entry_sz;
771e5487a41SPeter Maydell         } else {
77280dcd37fSPeter Maydell             td->num_entries = (((num_pages * page_sz) /
773e5487a41SPeter Maydell                                   L1TABLE_ENTRY_SIZE) *
774e5487a41SPeter Maydell                                  (page_sz / td->entry_sz));
775e5487a41SPeter Maydell         }
77680dcd37fSPeter Maydell         td->num_ids = 1ULL << idbits;
7771b08e436SShashi Mallela     }
7781b08e436SShashi Mallela }
7791b08e436SShashi Mallela 
7801b08e436SShashi Mallela static void extract_cmdq_params(GICv3ITSState *s)
7811b08e436SShashi Mallela {
7821b08e436SShashi Mallela     uint16_t num_pages = 0;
7831b08e436SShashi Mallela     uint64_t value = s->cbaser;
7841b08e436SShashi Mallela 
7851b08e436SShashi Mallela     num_pages = FIELD_EX64(value, GITS_CBASER, SIZE) + 1;
7861b08e436SShashi Mallela 
7871b08e436SShashi Mallela     memset(&s->cq, 0 , sizeof(s->cq));
7881b08e436SShashi Mallela     s->cq.valid = FIELD_EX64(value, GITS_CBASER, VALID);
7891b08e436SShashi Mallela 
7901b08e436SShashi Mallela     if (s->cq.valid) {
79180dcd37fSPeter Maydell         s->cq.num_entries = (num_pages * GITS_PAGE_SIZE_4K) /
7921b08e436SShashi Mallela                              GITS_CMDQ_ENTRY_SIZE;
7931b08e436SShashi Mallela         s->cq.base_addr = FIELD_EX64(value, GITS_CBASER, PHYADDR);
7941b08e436SShashi Mallela         s->cq.base_addr <<= R_GITS_CBASER_PHYADDR_SHIFT;
7951b08e436SShashi Mallela     }
7961b08e436SShashi Mallela }
7971b08e436SShashi Mallela 
79818f6290aSShashi Mallela static MemTxResult gicv3_its_translation_write(void *opaque, hwaddr offset,
79918f6290aSShashi Mallela                                                uint64_t data, unsigned size,
80018f6290aSShashi Mallela                                                MemTxAttrs attrs)
80118f6290aSShashi Mallela {
802c694cb4cSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
803c694cb4cSShashi Mallela     bool result = true;
804c694cb4cSShashi Mallela     uint32_t devid = 0;
805c694cb4cSShashi Mallela 
806c694cb4cSShashi Mallela     switch (offset) {
807c694cb4cSShashi Mallela     case GITS_TRANSLATER:
8088d2d6dd9SPeter Maydell         if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) {
809c694cb4cSShashi Mallela             devid = attrs.requester_id;
810c694cb4cSShashi Mallela             result = process_its_cmd(s, data, devid, NONE);
811c694cb4cSShashi Mallela         }
812c694cb4cSShashi Mallela         break;
813c694cb4cSShashi Mallela     default:
814c694cb4cSShashi Mallela         break;
815c694cb4cSShashi Mallela     }
816c694cb4cSShashi Mallela 
817c694cb4cSShashi Mallela     if (result) {
81818f6290aSShashi Mallela         return MEMTX_OK;
819c694cb4cSShashi Mallela     } else {
820c694cb4cSShashi Mallela         return MEMTX_ERROR;
821c694cb4cSShashi Mallela     }
82218f6290aSShashi Mallela }
82318f6290aSShashi Mallela 
82418f6290aSShashi Mallela static bool its_writel(GICv3ITSState *s, hwaddr offset,
82518f6290aSShashi Mallela                               uint64_t value, MemTxAttrs attrs)
82618f6290aSShashi Mallela {
82718f6290aSShashi Mallela     bool result = true;
8281b08e436SShashi Mallela     int index;
82918f6290aSShashi Mallela 
8301b08e436SShashi Mallela     switch (offset) {
8311b08e436SShashi Mallela     case GITS_CTLR:
8322f459cd1SShashi Mallela         if (value & R_GITS_CTLR_ENABLED_MASK) {
8338d2d6dd9SPeter Maydell             s->ctlr |= R_GITS_CTLR_ENABLED_MASK;
8341b08e436SShashi Mallela             extract_table_params(s);
8351b08e436SShashi Mallela             extract_cmdq_params(s);
8361b08e436SShashi Mallela             s->creadr = 0;
8377eca39e0SShashi Mallela             process_cmdq(s);
8382f459cd1SShashi Mallela         } else {
8398d2d6dd9SPeter Maydell             s->ctlr &= ~R_GITS_CTLR_ENABLED_MASK;
8401b08e436SShashi Mallela         }
8411b08e436SShashi Mallela         break;
8421b08e436SShashi Mallela     case GITS_CBASER:
8431b08e436SShashi Mallela         /*
8441b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
8451b08e436SShashi Mallela          *                 already enabled
8461b08e436SShashi Mallela          */
8478d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
8481b08e436SShashi Mallela             s->cbaser = deposit64(s->cbaser, 0, 32, value);
8491b08e436SShashi Mallela             s->creadr = 0;
8501b08e436SShashi Mallela             s->cwriter = s->creadr;
8511b08e436SShashi Mallela         }
8521b08e436SShashi Mallela         break;
8531b08e436SShashi Mallela     case GITS_CBASER + 4:
8541b08e436SShashi Mallela         /*
8551b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
8561b08e436SShashi Mallela          *                 already enabled
8571b08e436SShashi Mallela          */
8588d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
8591b08e436SShashi Mallela             s->cbaser = deposit64(s->cbaser, 32, 32, value);
8601b08e436SShashi Mallela             s->creadr = 0;
8611b08e436SShashi Mallela             s->cwriter = s->creadr;
8621b08e436SShashi Mallela         }
8631b08e436SShashi Mallela         break;
8641b08e436SShashi Mallela     case GITS_CWRITER:
8651b08e436SShashi Mallela         s->cwriter = deposit64(s->cwriter, 0, 32,
8661b08e436SShashi Mallela                                (value & ~R_GITS_CWRITER_RETRY_MASK));
8677eca39e0SShashi Mallela         if (s->cwriter != s->creadr) {
8687eca39e0SShashi Mallela             process_cmdq(s);
8697eca39e0SShashi Mallela         }
8701b08e436SShashi Mallela         break;
8711b08e436SShashi Mallela     case GITS_CWRITER + 4:
8721b08e436SShashi Mallela         s->cwriter = deposit64(s->cwriter, 32, 32, value);
8731b08e436SShashi Mallela         break;
8741b08e436SShashi Mallela     case GITS_CREADR:
8751b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
8761b08e436SShashi Mallela             s->creadr = deposit64(s->creadr, 0, 32,
8771b08e436SShashi Mallela                                   (value & ~R_GITS_CREADR_STALLED_MASK));
8781b08e436SShashi Mallela         } else {
8791b08e436SShashi Mallela             /* RO register, ignore the write */
8801b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
8811b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
8821b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
8831b08e436SShashi Mallela         }
8841b08e436SShashi Mallela         break;
8851b08e436SShashi Mallela     case GITS_CREADR + 4:
8861b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
8871b08e436SShashi Mallela             s->creadr = deposit64(s->creadr, 32, 32, value);
8881b08e436SShashi Mallela         } else {
8891b08e436SShashi Mallela             /* RO register, ignore the write */
8901b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
8911b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
8921b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
8931b08e436SShashi Mallela         }
8941b08e436SShashi Mallela         break;
8951b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
8961b08e436SShashi Mallela         /*
8971b08e436SShashi Mallela          * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is
8981b08e436SShashi Mallela          *                 already enabled
8991b08e436SShashi Mallela          */
9008d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
9011b08e436SShashi Mallela             index = (offset - GITS_BASER) / 8;
9021b08e436SShashi Mallela 
9031b08e436SShashi Mallela             if (offset & 7) {
9041b08e436SShashi Mallela                 value <<= 32;
9051b08e436SShashi Mallela                 value &= ~GITS_BASER_RO_MASK;
9061b08e436SShashi Mallela                 s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(0, 32);
9071b08e436SShashi Mallela                 s->baser[index] |= value;
9081b08e436SShashi Mallela             } else {
9091b08e436SShashi Mallela                 value &= ~GITS_BASER_RO_MASK;
9101b08e436SShashi Mallela                 s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(32, 32);
9111b08e436SShashi Mallela                 s->baser[index] |= value;
9121b08e436SShashi Mallela             }
9131b08e436SShashi Mallela         }
9141b08e436SShashi Mallela         break;
9151b08e436SShashi Mallela     case GITS_IIDR:
9161b08e436SShashi Mallela     case GITS_IDREGS ... GITS_IDREGS + 0x2f:
9171b08e436SShashi Mallela         /* RO registers, ignore the write */
9181b08e436SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
9191b08e436SShashi Mallela                       "%s: invalid guest write to RO register at offset "
9201b08e436SShashi Mallela                       TARGET_FMT_plx "\n", __func__, offset);
9211b08e436SShashi Mallela         break;
9221b08e436SShashi Mallela     default:
9231b08e436SShashi Mallela         result = false;
9241b08e436SShashi Mallela         break;
9251b08e436SShashi Mallela     }
92618f6290aSShashi Mallela     return result;
92718f6290aSShashi Mallela }
92818f6290aSShashi Mallela 
92918f6290aSShashi Mallela static bool its_readl(GICv3ITSState *s, hwaddr offset,
93018f6290aSShashi Mallela                              uint64_t *data, MemTxAttrs attrs)
93118f6290aSShashi Mallela {
93218f6290aSShashi Mallela     bool result = true;
9331b08e436SShashi Mallela     int index;
93418f6290aSShashi Mallela 
9351b08e436SShashi Mallela     switch (offset) {
9361b08e436SShashi Mallela     case GITS_CTLR:
9371b08e436SShashi Mallela         *data = s->ctlr;
9381b08e436SShashi Mallela         break;
9391b08e436SShashi Mallela     case GITS_IIDR:
9401b08e436SShashi Mallela         *data = gicv3_iidr();
9411b08e436SShashi Mallela         break;
9421b08e436SShashi Mallela     case GITS_IDREGS ... GITS_IDREGS + 0x2f:
9431b08e436SShashi Mallela         /* ID registers */
9441b08e436SShashi Mallela         *data = gicv3_idreg(offset - GITS_IDREGS);
9451b08e436SShashi Mallela         break;
9461b08e436SShashi Mallela     case GITS_TYPER:
9471b08e436SShashi Mallela         *data = extract64(s->typer, 0, 32);
9481b08e436SShashi Mallela         break;
9491b08e436SShashi Mallela     case GITS_TYPER + 4:
9501b08e436SShashi Mallela         *data = extract64(s->typer, 32, 32);
9511b08e436SShashi Mallela         break;
9521b08e436SShashi Mallela     case GITS_CBASER:
9531b08e436SShashi Mallela         *data = extract64(s->cbaser, 0, 32);
9541b08e436SShashi Mallela         break;
9551b08e436SShashi Mallela     case GITS_CBASER + 4:
9561b08e436SShashi Mallela         *data = extract64(s->cbaser, 32, 32);
9571b08e436SShashi Mallela         break;
9581b08e436SShashi Mallela     case GITS_CREADR:
9591b08e436SShashi Mallela         *data = extract64(s->creadr, 0, 32);
9601b08e436SShashi Mallela         break;
9611b08e436SShashi Mallela     case GITS_CREADR + 4:
9621b08e436SShashi Mallela         *data = extract64(s->creadr, 32, 32);
9631b08e436SShashi Mallela         break;
9641b08e436SShashi Mallela     case GITS_CWRITER:
9651b08e436SShashi Mallela         *data = extract64(s->cwriter, 0, 32);
9661b08e436SShashi Mallela         break;
9671b08e436SShashi Mallela     case GITS_CWRITER + 4:
9681b08e436SShashi Mallela         *data = extract64(s->cwriter, 32, 32);
9691b08e436SShashi Mallela         break;
9701b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
9711b08e436SShashi Mallela         index = (offset - GITS_BASER) / 8;
9721b08e436SShashi Mallela         if (offset & 7) {
9731b08e436SShashi Mallela             *data = extract64(s->baser[index], 32, 32);
9741b08e436SShashi Mallela         } else {
9751b08e436SShashi Mallela             *data = extract64(s->baser[index], 0, 32);
9761b08e436SShashi Mallela         }
9771b08e436SShashi Mallela         break;
9781b08e436SShashi Mallela     default:
9791b08e436SShashi Mallela         result = false;
9801b08e436SShashi Mallela         break;
9811b08e436SShashi Mallela     }
98218f6290aSShashi Mallela     return result;
98318f6290aSShashi Mallela }
98418f6290aSShashi Mallela 
98518f6290aSShashi Mallela static bool its_writell(GICv3ITSState *s, hwaddr offset,
98618f6290aSShashi Mallela                                uint64_t value, MemTxAttrs attrs)
98718f6290aSShashi Mallela {
98818f6290aSShashi Mallela     bool result = true;
9891b08e436SShashi Mallela     int index;
99018f6290aSShashi Mallela 
9911b08e436SShashi Mallela     switch (offset) {
9921b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
9931b08e436SShashi Mallela         /*
9941b08e436SShashi Mallela          * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is
9951b08e436SShashi Mallela          *                 already enabled
9961b08e436SShashi Mallela          */
9978d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
9981b08e436SShashi Mallela             index = (offset - GITS_BASER) / 8;
9991b08e436SShashi Mallela             s->baser[index] &= GITS_BASER_RO_MASK;
10001b08e436SShashi Mallela             s->baser[index] |= (value & ~GITS_BASER_RO_MASK);
10011b08e436SShashi Mallela         }
10021b08e436SShashi Mallela         break;
10031b08e436SShashi Mallela     case GITS_CBASER:
10041b08e436SShashi Mallela         /*
10051b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
10061b08e436SShashi Mallela          *                 already enabled
10071b08e436SShashi Mallela          */
10088d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
10091b08e436SShashi Mallela             s->cbaser = value;
10101b08e436SShashi Mallela             s->creadr = 0;
10111b08e436SShashi Mallela             s->cwriter = s->creadr;
10121b08e436SShashi Mallela         }
10131b08e436SShashi Mallela         break;
10141b08e436SShashi Mallela     case GITS_CWRITER:
10151b08e436SShashi Mallela         s->cwriter = value & ~R_GITS_CWRITER_RETRY_MASK;
10167eca39e0SShashi Mallela         if (s->cwriter != s->creadr) {
10177eca39e0SShashi Mallela             process_cmdq(s);
10187eca39e0SShashi Mallela         }
10191b08e436SShashi Mallela         break;
10201b08e436SShashi Mallela     case GITS_CREADR:
10211b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
10221b08e436SShashi Mallela             s->creadr = value & ~R_GITS_CREADR_STALLED_MASK;
10231b08e436SShashi Mallela         } else {
10241b08e436SShashi Mallela             /* RO register, ignore the write */
10251b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
10261b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
10271b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
10281b08e436SShashi Mallela         }
10291b08e436SShashi Mallela         break;
10301b08e436SShashi Mallela     case GITS_TYPER:
10311b08e436SShashi Mallela         /* RO registers, ignore the write */
10321b08e436SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
10331b08e436SShashi Mallela                       "%s: invalid guest write to RO register at offset "
10341b08e436SShashi Mallela                       TARGET_FMT_plx "\n", __func__, offset);
10351b08e436SShashi Mallela         break;
10361b08e436SShashi Mallela     default:
10371b08e436SShashi Mallela         result = false;
10381b08e436SShashi Mallela         break;
10391b08e436SShashi Mallela     }
104018f6290aSShashi Mallela     return result;
104118f6290aSShashi Mallela }
104218f6290aSShashi Mallela 
104318f6290aSShashi Mallela static bool its_readll(GICv3ITSState *s, hwaddr offset,
104418f6290aSShashi Mallela                               uint64_t *data, MemTxAttrs attrs)
104518f6290aSShashi Mallela {
104618f6290aSShashi Mallela     bool result = true;
10471b08e436SShashi Mallela     int index;
104818f6290aSShashi Mallela 
10491b08e436SShashi Mallela     switch (offset) {
10501b08e436SShashi Mallela     case GITS_TYPER:
10511b08e436SShashi Mallela         *data = s->typer;
10521b08e436SShashi Mallela         break;
10531b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
10541b08e436SShashi Mallela         index = (offset - GITS_BASER) / 8;
10551b08e436SShashi Mallela         *data = s->baser[index];
10561b08e436SShashi Mallela         break;
10571b08e436SShashi Mallela     case GITS_CBASER:
10581b08e436SShashi Mallela         *data = s->cbaser;
10591b08e436SShashi Mallela         break;
10601b08e436SShashi Mallela     case GITS_CREADR:
10611b08e436SShashi Mallela         *data = s->creadr;
10621b08e436SShashi Mallela         break;
10631b08e436SShashi Mallela     case GITS_CWRITER:
10641b08e436SShashi Mallela         *data = s->cwriter;
10651b08e436SShashi Mallela         break;
10661b08e436SShashi Mallela     default:
10671b08e436SShashi Mallela         result = false;
10681b08e436SShashi Mallela         break;
10691b08e436SShashi Mallela     }
107018f6290aSShashi Mallela     return result;
107118f6290aSShashi Mallela }
107218f6290aSShashi Mallela 
107318f6290aSShashi Mallela static MemTxResult gicv3_its_read(void *opaque, hwaddr offset, uint64_t *data,
107418f6290aSShashi Mallela                                   unsigned size, MemTxAttrs attrs)
107518f6290aSShashi Mallela {
107618f6290aSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
107718f6290aSShashi Mallela     bool result;
107818f6290aSShashi Mallela 
107918f6290aSShashi Mallela     switch (size) {
108018f6290aSShashi Mallela     case 4:
108118f6290aSShashi Mallela         result = its_readl(s, offset, data, attrs);
108218f6290aSShashi Mallela         break;
108318f6290aSShashi Mallela     case 8:
108418f6290aSShashi Mallela         result = its_readll(s, offset, data, attrs);
108518f6290aSShashi Mallela         break;
108618f6290aSShashi Mallela     default:
108718f6290aSShashi Mallela         result = false;
108818f6290aSShashi Mallela         break;
108918f6290aSShashi Mallela     }
109018f6290aSShashi Mallela 
109118f6290aSShashi Mallela     if (!result) {
109218f6290aSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
109318f6290aSShashi Mallela                       "%s: invalid guest read at offset " TARGET_FMT_plx
109418f6290aSShashi Mallela                       "size %u\n", __func__, offset, size);
109518f6290aSShashi Mallela         /*
109618f6290aSShashi Mallela          * The spec requires that reserved registers are RAZ/WI;
109718f6290aSShashi Mallela          * so use false returns from leaf functions as a way to
109818f6290aSShashi Mallela          * trigger the guest-error logging but don't return it to
109918f6290aSShashi Mallela          * the caller, or we'll cause a spurious guest data abort.
110018f6290aSShashi Mallela          */
110118f6290aSShashi Mallela         *data = 0;
110218f6290aSShashi Mallela     }
110318f6290aSShashi Mallela     return MEMTX_OK;
110418f6290aSShashi Mallela }
110518f6290aSShashi Mallela 
110618f6290aSShashi Mallela static MemTxResult gicv3_its_write(void *opaque, hwaddr offset, uint64_t data,
110718f6290aSShashi Mallela                                    unsigned size, MemTxAttrs attrs)
110818f6290aSShashi Mallela {
110918f6290aSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
111018f6290aSShashi Mallela     bool result;
111118f6290aSShashi Mallela 
111218f6290aSShashi Mallela     switch (size) {
111318f6290aSShashi Mallela     case 4:
111418f6290aSShashi Mallela         result = its_writel(s, offset, data, attrs);
111518f6290aSShashi Mallela         break;
111618f6290aSShashi Mallela     case 8:
111718f6290aSShashi Mallela         result = its_writell(s, offset, data, attrs);
111818f6290aSShashi Mallela         break;
111918f6290aSShashi Mallela     default:
112018f6290aSShashi Mallela         result = false;
112118f6290aSShashi Mallela         break;
112218f6290aSShashi Mallela     }
112318f6290aSShashi Mallela 
112418f6290aSShashi Mallela     if (!result) {
112518f6290aSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
112618f6290aSShashi Mallela                       "%s: invalid guest write at offset " TARGET_FMT_plx
112718f6290aSShashi Mallela                       "size %u\n", __func__, offset, size);
112818f6290aSShashi Mallela         /*
112918f6290aSShashi Mallela          * The spec requires that reserved registers are RAZ/WI;
113018f6290aSShashi Mallela          * so use false returns from leaf functions as a way to
113118f6290aSShashi Mallela          * trigger the guest-error logging but don't return it to
113218f6290aSShashi Mallela          * the caller, or we'll cause a spurious guest data abort.
113318f6290aSShashi Mallela          */
113418f6290aSShashi Mallela     }
113518f6290aSShashi Mallela     return MEMTX_OK;
113618f6290aSShashi Mallela }
113718f6290aSShashi Mallela 
113818f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_control_ops = {
113918f6290aSShashi Mallela     .read_with_attrs = gicv3_its_read,
114018f6290aSShashi Mallela     .write_with_attrs = gicv3_its_write,
114118f6290aSShashi Mallela     .valid.min_access_size = 4,
114218f6290aSShashi Mallela     .valid.max_access_size = 8,
114318f6290aSShashi Mallela     .impl.min_access_size = 4,
114418f6290aSShashi Mallela     .impl.max_access_size = 8,
114518f6290aSShashi Mallela     .endianness = DEVICE_NATIVE_ENDIAN,
114618f6290aSShashi Mallela };
114718f6290aSShashi Mallela 
114818f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_translation_ops = {
114918f6290aSShashi Mallela     .write_with_attrs = gicv3_its_translation_write,
115018f6290aSShashi Mallela     .valid.min_access_size = 2,
115118f6290aSShashi Mallela     .valid.max_access_size = 4,
115218f6290aSShashi Mallela     .impl.min_access_size = 2,
115318f6290aSShashi Mallela     .impl.max_access_size = 4,
115418f6290aSShashi Mallela     .endianness = DEVICE_NATIVE_ENDIAN,
115518f6290aSShashi Mallela };
115618f6290aSShashi Mallela 
115718f6290aSShashi Mallela static void gicv3_arm_its_realize(DeviceState *dev, Error **errp)
115818f6290aSShashi Mallela {
115918f6290aSShashi Mallela     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
116018f6290aSShashi Mallela     int i;
116118f6290aSShashi Mallela 
116218f6290aSShashi Mallela     for (i = 0; i < s->gicv3->num_cpu; i++) {
116318f6290aSShashi Mallela         if (!(s->gicv3->cpu[i].gicr_typer & GICR_TYPER_PLPIS)) {
116418f6290aSShashi Mallela             error_setg(errp, "Physical LPI not supported by CPU %d", i);
116518f6290aSShashi Mallela             return;
116618f6290aSShashi Mallela         }
116718f6290aSShashi Mallela     }
116818f6290aSShashi Mallela 
116918f6290aSShashi Mallela     gicv3_its_init_mmio(s, &gicv3_its_control_ops, &gicv3_its_translation_ops);
117018f6290aSShashi Mallela 
11711b08e436SShashi Mallela     address_space_init(&s->gicv3->dma_as, s->gicv3->dma,
11721b08e436SShashi Mallela                        "gicv3-its-sysmem");
11731b08e436SShashi Mallela 
117418f6290aSShashi Mallela     /* set the ITS default features supported */
1175764d6ba1SPeter Maydell     s->typer = FIELD_DP64(s->typer, GITS_TYPER, PHYSICAL, 1);
117618f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, ITT_ENTRY_SIZE,
117718f6290aSShashi Mallela                           ITS_ITT_ENTRY_SIZE - 1);
117818f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, IDBITS, ITS_IDBITS);
117918f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, DEVBITS, ITS_DEVBITS);
118018f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIL, 1);
118118f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIDBITS, ITS_CIDBITS);
118218f6290aSShashi Mallela }
118318f6290aSShashi Mallela 
118418f6290aSShashi Mallela static void gicv3_its_reset(DeviceState *dev)
118518f6290aSShashi Mallela {
118618f6290aSShashi Mallela     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
118718f6290aSShashi Mallela     GICv3ITSClass *c = ARM_GICV3_ITS_GET_CLASS(s);
118818f6290aSShashi Mallela 
118918f6290aSShashi Mallela     c->parent_reset(dev);
119018f6290aSShashi Mallela 
119118f6290aSShashi Mallela     /* Quiescent bit reset to 1 */
119218f6290aSShashi Mallela     s->ctlr = FIELD_DP32(s->ctlr, GITS_CTLR, QUIESCENT, 1);
119318f6290aSShashi Mallela 
119418f6290aSShashi Mallela     /*
119518f6290aSShashi Mallela      * setting GITS_BASER0.Type = 0b001 (Device)
119618f6290aSShashi Mallela      *         GITS_BASER1.Type = 0b100 (Collection Table)
119718f6290aSShashi Mallela      *         GITS_BASER<n>.Type,where n = 3 to 7 are 0b00 (Unimplemented)
119818f6290aSShashi Mallela      *         GITS_BASER<0,1>.Page_Size = 64KB
119918f6290aSShashi Mallela      * and default translation table entry size to 16 bytes
120018f6290aSShashi Mallela      */
120118f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, TYPE,
120218f6290aSShashi Mallela                              GITS_BASER_TYPE_DEVICE);
120318f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, PAGESIZE,
120418f6290aSShashi Mallela                              GITS_BASER_PAGESIZE_64K);
120518f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, ENTRYSIZE,
120618f6290aSShashi Mallela                              GITS_DTE_SIZE - 1);
120718f6290aSShashi Mallela 
120818f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, TYPE,
120918f6290aSShashi Mallela                              GITS_BASER_TYPE_COLLECTION);
121018f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, PAGESIZE,
121118f6290aSShashi Mallela                              GITS_BASER_PAGESIZE_64K);
121218f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, ENTRYSIZE,
121318f6290aSShashi Mallela                              GITS_CTE_SIZE - 1);
121418f6290aSShashi Mallela }
121518f6290aSShashi Mallela 
12161b08e436SShashi Mallela static void gicv3_its_post_load(GICv3ITSState *s)
12171b08e436SShashi Mallela {
12188d2d6dd9SPeter Maydell     if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) {
12191b08e436SShashi Mallela         extract_table_params(s);
12201b08e436SShashi Mallela         extract_cmdq_params(s);
12211b08e436SShashi Mallela     }
12221b08e436SShashi Mallela }
12231b08e436SShashi Mallela 
122418f6290aSShashi Mallela static Property gicv3_its_props[] = {
122518f6290aSShashi Mallela     DEFINE_PROP_LINK("parent-gicv3", GICv3ITSState, gicv3, "arm-gicv3",
122618f6290aSShashi Mallela                      GICv3State *),
122718f6290aSShashi Mallela     DEFINE_PROP_END_OF_LIST(),
122818f6290aSShashi Mallela };
122918f6290aSShashi Mallela 
123018f6290aSShashi Mallela static void gicv3_its_class_init(ObjectClass *klass, void *data)
123118f6290aSShashi Mallela {
123218f6290aSShashi Mallela     DeviceClass *dc = DEVICE_CLASS(klass);
123318f6290aSShashi Mallela     GICv3ITSClass *ic = ARM_GICV3_ITS_CLASS(klass);
12341b08e436SShashi Mallela     GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass);
123518f6290aSShashi Mallela 
123618f6290aSShashi Mallela     dc->realize = gicv3_arm_its_realize;
123718f6290aSShashi Mallela     device_class_set_props(dc, gicv3_its_props);
123818f6290aSShashi Mallela     device_class_set_parent_reset(dc, gicv3_its_reset, &ic->parent_reset);
12391b08e436SShashi Mallela     icc->post_load = gicv3_its_post_load;
124018f6290aSShashi Mallela }
124118f6290aSShashi Mallela 
124218f6290aSShashi Mallela static const TypeInfo gicv3_its_info = {
124318f6290aSShashi Mallela     .name = TYPE_ARM_GICV3_ITS,
124418f6290aSShashi Mallela     .parent = TYPE_ARM_GICV3_ITS_COMMON,
124518f6290aSShashi Mallela     .instance_size = sizeof(GICv3ITSState),
124618f6290aSShashi Mallela     .class_init = gicv3_its_class_init,
124718f6290aSShashi Mallela     .class_size = sizeof(GICv3ITSClass),
124818f6290aSShashi Mallela };
124918f6290aSShashi Mallela 
125018f6290aSShashi Mallela static void gicv3_its_register_types(void)
125118f6290aSShashi Mallela {
125218f6290aSShashi Mallela     type_register_static(&gicv3_its_info);
125318f6290aSShashi Mallela }
125418f6290aSShashi Mallela 
125518f6290aSShashi Mallela type_init(gicv3_its_register_types)
1256