xref: /qemu/hw/intc/arm_gicv3_its.c (revision f6d1d9b4074d64de92f3ab4dfa50dc19548fdfd7)
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 
44c694cb4cSShashi Mallela typedef struct {
45c694cb4cSShashi Mallela     uint32_t iteh;
46c694cb4cSShashi Mallela     uint64_t itel;
47c694cb4cSShashi Mallela } IteEntry;
48c694cb4cSShashi Mallela 
49ef011555SPeter Maydell /*
50ef011555SPeter Maydell  * The ITS spec permits a range of CONSTRAINED UNPREDICTABLE options
51ef011555SPeter Maydell  * if a command parameter is not correct. These include both "stall
52ef011555SPeter Maydell  * processing of the command queue" and "ignore this command, and
53ef011555SPeter Maydell  * keep processing the queue". In our implementation we choose that
54ef011555SPeter Maydell  * memory transaction errors reading the command packet provoke a
55ef011555SPeter Maydell  * stall, but errors in parameters cause us to ignore the command
56ef011555SPeter Maydell  * and continue processing.
57ef011555SPeter Maydell  * The process_* functions which handle individual ITS commands all
58ef011555SPeter Maydell  * return an ItsCmdResult which tells process_cmdq() whether it should
59ef011555SPeter Maydell  * stall or keep going.
60ef011555SPeter Maydell  */
61ef011555SPeter Maydell typedef enum ItsCmdResult {
62ef011555SPeter Maydell     CMD_STALL = 0,
63ef011555SPeter Maydell     CMD_CONTINUE = 1,
64ef011555SPeter Maydell } ItsCmdResult;
65ef011555SPeter Maydell 
661b08e436SShashi Mallela static uint64_t baser_base_addr(uint64_t value, uint32_t page_sz)
671b08e436SShashi Mallela {
681b08e436SShashi Mallela     uint64_t result = 0;
691b08e436SShashi Mallela 
701b08e436SShashi Mallela     switch (page_sz) {
711b08e436SShashi Mallela     case GITS_PAGE_SIZE_4K:
721b08e436SShashi Mallela     case GITS_PAGE_SIZE_16K:
731b08e436SShashi Mallela         result = FIELD_EX64(value, GITS_BASER, PHYADDR) << 12;
741b08e436SShashi Mallela         break;
751b08e436SShashi Mallela 
761b08e436SShashi Mallela     case GITS_PAGE_SIZE_64K:
771b08e436SShashi Mallela         result = FIELD_EX64(value, GITS_BASER, PHYADDRL_64K) << 16;
781b08e436SShashi Mallela         result |= FIELD_EX64(value, GITS_BASER, PHYADDRH_64K) << 48;
791b08e436SShashi Mallela         break;
801b08e436SShashi Mallela 
811b08e436SShashi Mallela     default:
821b08e436SShashi Mallela         break;
831b08e436SShashi Mallela     }
841b08e436SShashi Mallela     return result;
851b08e436SShashi Mallela }
861b08e436SShashi Mallela 
87d050f80fSPeter Maydell static uint64_t table_entry_addr(GICv3ITSState *s, TableDesc *td,
88d050f80fSPeter Maydell                                  uint32_t idx, MemTxResult *res)
89d050f80fSPeter Maydell {
90d050f80fSPeter Maydell     /*
91d050f80fSPeter Maydell      * Given a TableDesc describing one of the ITS in-guest-memory
92d050f80fSPeter Maydell      * tables and an index into it, return the guest address
93d050f80fSPeter Maydell      * corresponding to that table entry.
94d050f80fSPeter Maydell      * If there was a memory error reading the L1 table of an
95d050f80fSPeter Maydell      * indirect table, *res is set accordingly, and we return -1.
96d050f80fSPeter Maydell      * If the L1 table entry is marked not valid, we return -1 with
97d050f80fSPeter Maydell      * *res set to MEMTX_OK.
98d050f80fSPeter Maydell      *
99d050f80fSPeter Maydell      * The specification defines the format of level 1 entries of a
100d050f80fSPeter Maydell      * 2-level table, but the format of level 2 entries and the format
101d050f80fSPeter Maydell      * of flat-mapped tables is IMPDEF.
102d050f80fSPeter Maydell      */
103d050f80fSPeter Maydell     AddressSpace *as = &s->gicv3->dma_as;
104d050f80fSPeter Maydell     uint32_t l2idx;
105d050f80fSPeter Maydell     uint64_t l2;
106d050f80fSPeter Maydell     uint32_t num_l2_entries;
107d050f80fSPeter Maydell 
108d050f80fSPeter Maydell     *res = MEMTX_OK;
109d050f80fSPeter Maydell 
110d050f80fSPeter Maydell     if (!td->indirect) {
111d050f80fSPeter Maydell         /* Single level table */
112d050f80fSPeter Maydell         return td->base_addr + idx * td->entry_sz;
113d050f80fSPeter Maydell     }
114d050f80fSPeter Maydell 
115d050f80fSPeter Maydell     /* Two level table */
116d050f80fSPeter Maydell     l2idx = idx / (td->page_sz / L1TABLE_ENTRY_SIZE);
117d050f80fSPeter Maydell 
118d050f80fSPeter Maydell     l2 = address_space_ldq_le(as,
119d050f80fSPeter Maydell                               td->base_addr + (l2idx * L1TABLE_ENTRY_SIZE),
120d050f80fSPeter Maydell                               MEMTXATTRS_UNSPECIFIED, res);
121d050f80fSPeter Maydell     if (*res != MEMTX_OK) {
122d050f80fSPeter Maydell         return -1;
123d050f80fSPeter Maydell     }
124d050f80fSPeter Maydell     if (!(l2 & L2_TABLE_VALID_MASK)) {
125d050f80fSPeter Maydell         return -1;
126d050f80fSPeter Maydell     }
127d050f80fSPeter Maydell 
128d050f80fSPeter Maydell     num_l2_entries = td->page_sz / td->entry_sz;
129d050f80fSPeter Maydell     return (l2 & ((1ULL << 51) - 1)) + (idx % num_l2_entries) * td->entry_sz;
130d050f80fSPeter Maydell }
131d050f80fSPeter Maydell 
132c694cb4cSShashi Mallela static bool get_cte(GICv3ITSState *s, uint16_t icid, uint64_t *cte,
133c694cb4cSShashi Mallela                     MemTxResult *res)
134c694cb4cSShashi Mallela {
135c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
136d050f80fSPeter Maydell     uint64_t entry_addr = table_entry_addr(s, &s->ct, icid, res);
137c694cb4cSShashi Mallela 
138d050f80fSPeter Maydell     if (entry_addr == -1) {
139d050f80fSPeter Maydell         return false; /* not valid */
140c694cb4cSShashi Mallela     }
141c694cb4cSShashi Mallela 
142d050f80fSPeter Maydell     *cte = address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, res);
143437dc0eaSPeter Maydell     return FIELD_EX64(*cte, CTE, VALID);
144c694cb4cSShashi Mallela }
145c694cb4cSShashi Mallela 
146c694cb4cSShashi Mallela static bool update_ite(GICv3ITSState *s, uint32_t eventid, uint64_t dte,
147c694cb4cSShashi Mallela                        IteEntry ite)
148c694cb4cSShashi Mallela {
149c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
150c694cb4cSShashi Mallela     uint64_t itt_addr;
151c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
152c694cb4cSShashi Mallela 
153e07f8445SPeter Maydell     itt_addr = FIELD_EX64(dte, DTE, ITTADDR);
154c694cb4cSShashi Mallela     itt_addr <<= ITTADDR_SHIFT; /* 256 byte aligned */
155c694cb4cSShashi Mallela 
156c694cb4cSShashi Mallela     address_space_stq_le(as, itt_addr + (eventid * (sizeof(uint64_t) +
157c694cb4cSShashi Mallela                          sizeof(uint32_t))), ite.itel, MEMTXATTRS_UNSPECIFIED,
158c694cb4cSShashi Mallela                          &res);
159c694cb4cSShashi Mallela 
160c694cb4cSShashi Mallela     if (res == MEMTX_OK) {
161c694cb4cSShashi Mallela         address_space_stl_le(as, itt_addr + (eventid * (sizeof(uint64_t) +
162c694cb4cSShashi Mallela                              sizeof(uint32_t))) + sizeof(uint32_t), ite.iteh,
163c694cb4cSShashi Mallela                              MEMTXATTRS_UNSPECIFIED, &res);
164c694cb4cSShashi Mallela     }
165c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
166c694cb4cSShashi Mallela         return false;
167c694cb4cSShashi Mallela     } else {
168c694cb4cSShashi Mallela         return true;
169c694cb4cSShashi Mallela     }
170c694cb4cSShashi Mallela }
171c694cb4cSShashi Mallela 
172c694cb4cSShashi Mallela static bool get_ite(GICv3ITSState *s, uint32_t eventid, uint64_t dte,
173c694cb4cSShashi Mallela                     uint16_t *icid, uint32_t *pIntid, MemTxResult *res)
174c694cb4cSShashi Mallela {
175c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
176c694cb4cSShashi Mallela     uint64_t itt_addr;
177c694cb4cSShashi Mallela     bool status = false;
178c694cb4cSShashi Mallela     IteEntry ite = {};
179c694cb4cSShashi Mallela 
180e07f8445SPeter Maydell     itt_addr = FIELD_EX64(dte, DTE, ITTADDR);
181c694cb4cSShashi Mallela     itt_addr <<= ITTADDR_SHIFT; /* 256 byte aligned */
182c694cb4cSShashi Mallela 
183c694cb4cSShashi Mallela     ite.itel = address_space_ldq_le(as, itt_addr +
184c694cb4cSShashi Mallela                                     (eventid * (sizeof(uint64_t) +
185c694cb4cSShashi Mallela                                     sizeof(uint32_t))), MEMTXATTRS_UNSPECIFIED,
186c694cb4cSShashi Mallela                                     res);
187c694cb4cSShashi Mallela 
188c694cb4cSShashi Mallela     if (*res == MEMTX_OK) {
189c694cb4cSShashi Mallela         ite.iteh = address_space_ldl_le(as, itt_addr +
190c694cb4cSShashi Mallela                                         (eventid * (sizeof(uint64_t) +
191c694cb4cSShashi Mallela                                         sizeof(uint32_t))) + sizeof(uint32_t),
192c694cb4cSShashi Mallela                                         MEMTXATTRS_UNSPECIFIED, res);
193c694cb4cSShashi Mallela 
194c694cb4cSShashi Mallela         if (*res == MEMTX_OK) {
195764d6ba1SPeter Maydell             if (FIELD_EX64(ite.itel, ITE_L, VALID)) {
196764d6ba1SPeter Maydell                 int inttype = FIELD_EX64(ite.itel, ITE_L, INTTYPE);
197764d6ba1SPeter Maydell                 if (inttype == ITE_INTTYPE_PHYSICAL) {
198764d6ba1SPeter Maydell                     *pIntid = FIELD_EX64(ite.itel, ITE_L, INTID);
199764d6ba1SPeter Maydell                     *icid = FIELD_EX32(ite.iteh, ITE_H, ICID);
200c694cb4cSShashi Mallela                     status = true;
201c694cb4cSShashi Mallela                 }
202c694cb4cSShashi Mallela             }
203c694cb4cSShashi Mallela         }
204c694cb4cSShashi Mallela     }
205c694cb4cSShashi Mallela     return status;
206c694cb4cSShashi Mallela }
207c694cb4cSShashi Mallela 
208c694cb4cSShashi Mallela static uint64_t get_dte(GICv3ITSState *s, uint32_t devid, MemTxResult *res)
209c694cb4cSShashi Mallela {
210c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
211d050f80fSPeter Maydell     uint64_t entry_addr = table_entry_addr(s, &s->dt, devid, res);
212c694cb4cSShashi Mallela 
213d050f80fSPeter Maydell     if (entry_addr == -1) {
214d050f80fSPeter Maydell         return 0; /* a DTE entry with the Valid bit clear */
215c694cb4cSShashi Mallela     }
216d050f80fSPeter Maydell     return address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, res);
217c694cb4cSShashi Mallela }
218c694cb4cSShashi Mallela 
219c694cb4cSShashi Mallela /*
220c694cb4cSShashi Mallela  * This function handles the processing of following commands based on
221c694cb4cSShashi Mallela  * the ItsCmdType parameter passed:-
222c694cb4cSShashi Mallela  * 1. triggering of lpi interrupt translation via ITS INT command
223c694cb4cSShashi Mallela  * 2. triggering of lpi interrupt translation via gits_translater register
224c694cb4cSShashi Mallela  * 3. handling of ITS CLEAR command
225c694cb4cSShashi Mallela  * 4. handling of ITS DISCARD command
226c694cb4cSShashi Mallela  */
227ef011555SPeter Maydell static ItsCmdResult process_its_cmd(GICv3ITSState *s, uint64_t value,
228ef011555SPeter Maydell                                     uint32_t offset, ItsCmdType cmd)
229c694cb4cSShashi Mallela {
230c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
231c694cb4cSShashi Mallela     uint32_t devid, eventid;
232c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
233c694cb4cSShashi Mallela     bool dte_valid;
234c694cb4cSShashi Mallela     uint64_t dte = 0;
2358f809f69SPeter Maydell     uint64_t num_eventids;
236c694cb4cSShashi Mallela     uint16_t icid = 0;
237c694cb4cSShashi Mallela     uint32_t pIntid = 0;
238c694cb4cSShashi Mallela     bool ite_valid = false;
239c694cb4cSShashi Mallela     uint64_t cte = 0;
240c694cb4cSShashi Mallela     bool cte_valid = false;
24117fb5e36SShashi Mallela     uint64_t rdbase;
242c694cb4cSShashi Mallela 
243c694cb4cSShashi Mallela     if (cmd == NONE) {
244c694cb4cSShashi Mallela         devid = offset;
245c694cb4cSShashi Mallela     } else {
246c694cb4cSShashi Mallela         devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
247c694cb4cSShashi Mallela 
248c694cb4cSShashi Mallela         offset += NUM_BYTES_IN_DW;
249c694cb4cSShashi Mallela         value = address_space_ldq_le(as, s->cq.base_addr + offset,
250c694cb4cSShashi Mallela                                      MEMTXATTRS_UNSPECIFIED, &res);
251c694cb4cSShashi Mallela     }
252c694cb4cSShashi Mallela 
253c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
254593a7cc2SPeter Maydell         return CMD_STALL;
255c694cb4cSShashi Mallela     }
256c694cb4cSShashi Mallela 
257c694cb4cSShashi Mallela     eventid = (value & EVENTID_MASK);
258c694cb4cSShashi Mallela 
2598b8bb014SPeter Maydell     if (devid >= s->dt.num_entries) {
260b13148d9SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
261b13148d9SPeter Maydell                       "%s: invalid command attributes: devid %d>=%d",
2628b8bb014SPeter Maydell                       __func__, devid, s->dt.num_entries);
263b13148d9SPeter Maydell         return CMD_CONTINUE;
264b13148d9SPeter Maydell     }
265b13148d9SPeter Maydell 
266c694cb4cSShashi Mallela     dte = get_dte(s, devid, &res);
267c694cb4cSShashi Mallela 
268c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
269593a7cc2SPeter Maydell         return CMD_STALL;
270c694cb4cSShashi Mallela     }
271e07f8445SPeter Maydell     dte_valid = FIELD_EX64(dte, DTE, VALID);
272c694cb4cSShashi Mallela 
273be0ed8fbSPeter Maydell     if (!dte_valid) {
274229c57b1SAlex Bennée         qemu_log_mask(LOG_GUEST_ERROR,
275229c57b1SAlex Bennée                       "%s: invalid command attributes: "
276be0ed8fbSPeter Maydell                       "invalid dte: %"PRIx64" for %d\n",
277be0ed8fbSPeter Maydell                       __func__, dte, devid);
278593a7cc2SPeter Maydell         return CMD_CONTINUE;
279c694cb4cSShashi Mallela     }
280c694cb4cSShashi Mallela 
281be0ed8fbSPeter Maydell     num_eventids = 1ULL << (FIELD_EX64(dte, DTE, SIZE) + 1);
282229c57b1SAlex Bennée 
283b13148d9SPeter Maydell     if (eventid >= num_eventids) {
284b13148d9SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
285b13148d9SPeter Maydell                       "%s: invalid command attributes: eventid %d >= %"
286b13148d9SPeter Maydell                       PRId64 "\n",
287b13148d9SPeter Maydell                       __func__, eventid, num_eventids);
288b13148d9SPeter Maydell         return CMD_CONTINUE;
289b13148d9SPeter Maydell     }
290b13148d9SPeter Maydell 
291be0ed8fbSPeter Maydell     ite_valid = get_ite(s, eventid, dte, &icid, &pIntid, &res);
292be0ed8fbSPeter Maydell     if (res != MEMTX_OK) {
293be0ed8fbSPeter Maydell         return CMD_STALL;
294be0ed8fbSPeter Maydell     }
295be0ed8fbSPeter Maydell 
296be0ed8fbSPeter Maydell     if (!ite_valid) {
297be0ed8fbSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
298be0ed8fbSPeter Maydell                       "%s: invalid command attributes: invalid ITE\n",
299be0ed8fbSPeter Maydell                       __func__);
300be0ed8fbSPeter Maydell         return CMD_CONTINUE;
301be0ed8fbSPeter Maydell     }
302be0ed8fbSPeter Maydell 
3038b8bb014SPeter Maydell     if (icid >= s->ct.num_entries) {
30458b88779SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
30558b88779SPeter Maydell                       "%s: invalid ICID 0x%x in ITE (table corrupted?)\n",
30658b88779SPeter Maydell                       __func__, icid);
30758b88779SPeter Maydell         return CMD_CONTINUE;
30858b88779SPeter Maydell     }
30958b88779SPeter Maydell 
310be0ed8fbSPeter Maydell     cte_valid = get_cte(s, icid, &cte, &res);
311be0ed8fbSPeter Maydell     if (res != MEMTX_OK) {
312be0ed8fbSPeter Maydell         return CMD_STALL;
313be0ed8fbSPeter Maydell     }
314be0ed8fbSPeter Maydell     if (!cte_valid) {
315be0ed8fbSPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
316be0ed8fbSPeter Maydell                       "%s: invalid command attributes: "
317be0ed8fbSPeter Maydell                       "invalid cte: %"PRIx64"\n",
318be0ed8fbSPeter Maydell                       __func__, cte);
319be0ed8fbSPeter Maydell         return CMD_CONTINUE;
320be0ed8fbSPeter Maydell     }
321be0ed8fbSPeter Maydell 
322c694cb4cSShashi Mallela     /*
323c694cb4cSShashi Mallela      * Current implementation only supports rdbase == procnum
324c694cb4cSShashi Mallela      * Hence rdbase physical address is ignored
325c694cb4cSShashi Mallela      */
326437dc0eaSPeter Maydell     rdbase = FIELD_EX64(cte, CTE, RDBASE);
32717fb5e36SShashi Mallela 
328a120157bSPeter Maydell     if (rdbase >= s->gicv3->num_cpu) {
329593a7cc2SPeter Maydell         return CMD_CONTINUE;
33017fb5e36SShashi Mallela     }
33117fb5e36SShashi Mallela 
33217fb5e36SShashi Mallela     if ((cmd == CLEAR) || (cmd == DISCARD)) {
33317fb5e36SShashi Mallela         gicv3_redist_process_lpi(&s->gicv3->cpu[rdbase], pIntid, 0);
33417fb5e36SShashi Mallela     } else {
33517fb5e36SShashi Mallela         gicv3_redist_process_lpi(&s->gicv3->cpu[rdbase], pIntid, 1);
33617fb5e36SShashi Mallela     }
33717fb5e36SShashi Mallela 
338c694cb4cSShashi Mallela     if (cmd == DISCARD) {
339c694cb4cSShashi Mallela         IteEntry ite = {};
340c694cb4cSShashi Mallela         /* remove mapping from interrupt translation table */
341593a7cc2SPeter Maydell         return update_ite(s, eventid, dte, ite) ? CMD_CONTINUE : CMD_STALL;
342c694cb4cSShashi Mallela     }
343593a7cc2SPeter Maydell     return CMD_CONTINUE;
344c694cb4cSShashi Mallela }
345c694cb4cSShashi Mallela 
346ef011555SPeter Maydell static ItsCmdResult process_mapti(GICv3ITSState *s, uint64_t value,
347ef011555SPeter Maydell                                   uint32_t offset, bool ignore_pInt)
348c694cb4cSShashi Mallela {
349c694cb4cSShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
350c694cb4cSShashi Mallela     uint32_t devid, eventid;
351c694cb4cSShashi Mallela     uint32_t pIntid = 0;
3528f809f69SPeter Maydell     uint64_t num_eventids;
353905720f1SPeter Maydell     uint32_t num_intids;
354c694cb4cSShashi Mallela     bool dte_valid;
355c694cb4cSShashi Mallela     MemTxResult res = MEMTX_OK;
356c694cb4cSShashi Mallela     uint16_t icid = 0;
357c694cb4cSShashi Mallela     uint64_t dte = 0;
3580241f731SPeter Maydell     IteEntry ite = {};
359c694cb4cSShashi Mallela 
360c694cb4cSShashi Mallela     devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
361c694cb4cSShashi Mallela     offset += NUM_BYTES_IN_DW;
362c694cb4cSShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
363c694cb4cSShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
364c694cb4cSShashi Mallela 
365c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
3660241f731SPeter Maydell         return CMD_STALL;
367c694cb4cSShashi Mallela     }
368c694cb4cSShashi Mallela 
369c694cb4cSShashi Mallela     eventid = (value & EVENTID_MASK);
370c694cb4cSShashi Mallela 
371b87fab1cSPeter Maydell     if (ignore_pInt) {
372b87fab1cSPeter Maydell         pIntid = eventid;
373b87fab1cSPeter Maydell     } else {
374c694cb4cSShashi Mallela         pIntid = ((value & pINTID_MASK) >> pINTID_SHIFT);
375c694cb4cSShashi Mallela     }
376c694cb4cSShashi Mallela 
377c694cb4cSShashi Mallela     offset += NUM_BYTES_IN_DW;
378c694cb4cSShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
379c694cb4cSShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
380c694cb4cSShashi Mallela 
381c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
3820241f731SPeter Maydell         return CMD_STALL;
383c694cb4cSShashi Mallela     }
384c694cb4cSShashi Mallela 
385c694cb4cSShashi Mallela     icid = value & ICID_MASK;
386c694cb4cSShashi Mallela 
3878b8bb014SPeter Maydell     if (devid >= s->dt.num_entries) {
388b13148d9SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
389b13148d9SPeter Maydell                       "%s: invalid command attributes: devid %d>=%d",
3908b8bb014SPeter Maydell                       __func__, devid, s->dt.num_entries);
391b13148d9SPeter Maydell         return CMD_CONTINUE;
392b13148d9SPeter Maydell     }
393b13148d9SPeter Maydell 
394c694cb4cSShashi Mallela     dte = get_dte(s, devid, &res);
395c694cb4cSShashi Mallela 
396c694cb4cSShashi Mallela     if (res != MEMTX_OK) {
3970241f731SPeter Maydell         return CMD_STALL;
398c694cb4cSShashi Mallela     }
399e07f8445SPeter Maydell     dte_valid = FIELD_EX64(dte, DTE, VALID);
4008f809f69SPeter Maydell     num_eventids = 1ULL << (FIELD_EX64(dte, DTE, SIZE) + 1);
401905720f1SPeter Maydell     num_intids = 1ULL << (GICD_TYPER_IDBITS + 1);
402c694cb4cSShashi Mallela 
4038b8bb014SPeter Maydell     if ((icid >= s->ct.num_entries)
4048f809f69SPeter Maydell             || !dte_valid || (eventid >= num_eventids) ||
405905720f1SPeter Maydell             (((pIntid < GICV3_LPI_INTID_START) || (pIntid >= num_intids)) &&
406b87fab1cSPeter Maydell              (pIntid != INTID_SPURIOUS))) {
407c694cb4cSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
408c694cb4cSShashi Mallela                       "%s: invalid command attributes "
409b13148d9SPeter Maydell                       "icid %d or eventid %d or pIntid %d or"
410b13148d9SPeter Maydell                       "unmapped dte %d\n", __func__, icid, eventid,
411c694cb4cSShashi Mallela                       pIntid, dte_valid);
412c694cb4cSShashi Mallela         /*
413c694cb4cSShashi Mallela          * in this implementation, in case of error
414c694cb4cSShashi Mallela          * we ignore this command and move onto the next
415c694cb4cSShashi Mallela          * command in the queue
416c694cb4cSShashi Mallela          */
4170241f731SPeter Maydell         return CMD_CONTINUE;
4180241f731SPeter Maydell     }
4190241f731SPeter Maydell 
420c694cb4cSShashi Mallela     /* add ite entry to interrupt translation table */
421764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, VALID, dte_valid);
422764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, INTTYPE, ITE_INTTYPE_PHYSICAL);
423764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, INTID, pIntid);
424764d6ba1SPeter Maydell     ite.itel = FIELD_DP64(ite.itel, ITE_L, DOORBELL, INTID_SPURIOUS);
425764d6ba1SPeter Maydell     ite.iteh = FIELD_DP32(ite.iteh, ITE_H, ICID, icid);
426c694cb4cSShashi Mallela 
4270241f731SPeter Maydell     return update_ite(s, eventid, dte, ite) ? CMD_CONTINUE : CMD_STALL;
428c694cb4cSShashi Mallela }
429c694cb4cSShashi Mallela 
4307eca39e0SShashi Mallela static bool update_cte(GICv3ITSState *s, uint16_t icid, bool valid,
4317eca39e0SShashi Mallela                        uint64_t rdbase)
4327eca39e0SShashi Mallela {
4337eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
434d050f80fSPeter Maydell     uint64_t entry_addr;
4357eca39e0SShashi Mallela     uint64_t cte = 0;
4367eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
4377eca39e0SShashi Mallela 
4387eca39e0SShashi Mallela     if (!s->ct.valid) {
4397eca39e0SShashi Mallela         return true;
4407eca39e0SShashi Mallela     }
4417eca39e0SShashi Mallela 
4427eca39e0SShashi Mallela     if (valid) {
4437eca39e0SShashi Mallela         /* add mapping entry to collection table */
444437dc0eaSPeter Maydell         cte = FIELD_DP64(cte, CTE, VALID, 1);
445437dc0eaSPeter Maydell         cte = FIELD_DP64(cte, CTE, RDBASE, rdbase);
4467eca39e0SShashi Mallela     }
4477eca39e0SShashi Mallela 
448d050f80fSPeter Maydell     entry_addr = table_entry_addr(s, &s->ct, icid, &res);
4497eca39e0SShashi Mallela     if (res != MEMTX_OK) {
450d050f80fSPeter Maydell         /* memory access error: stall */
4517eca39e0SShashi Mallela         return false;
4527eca39e0SShashi Mallela     }
453d050f80fSPeter Maydell     if (entry_addr == -1) {
454d050f80fSPeter Maydell         /* No L2 table for this index: discard write and continue */
4557eca39e0SShashi Mallela         return true;
4567eca39e0SShashi Mallela     }
457d050f80fSPeter Maydell 
458d050f80fSPeter Maydell     address_space_stq_le(as, entry_addr, cte, MEMTXATTRS_UNSPECIFIED, &res);
459d050f80fSPeter Maydell     return res == MEMTX_OK;
4607eca39e0SShashi Mallela }
4617eca39e0SShashi Mallela 
462ef011555SPeter Maydell static ItsCmdResult process_mapc(GICv3ITSState *s, uint32_t offset)
4637eca39e0SShashi Mallela {
4647eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
4657eca39e0SShashi Mallela     uint16_t icid;
4667eca39e0SShashi Mallela     uint64_t rdbase;
4677eca39e0SShashi Mallela     bool valid;
4687eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
4697eca39e0SShashi Mallela     uint64_t value;
4707eca39e0SShashi Mallela 
4717eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
4727eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
4737eca39e0SShashi Mallela 
4747eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
4757eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
4767eca39e0SShashi Mallela 
4777eca39e0SShashi Mallela     if (res != MEMTX_OK) {
478f6675196SPeter Maydell         return CMD_STALL;
4797eca39e0SShashi Mallela     }
4807eca39e0SShashi Mallela 
4817eca39e0SShashi Mallela     icid = value & ICID_MASK;
4827eca39e0SShashi Mallela 
4837eca39e0SShashi Mallela     rdbase = (value & R_MAPC_RDBASE_MASK) >> R_MAPC_RDBASE_SHIFT;
4847eca39e0SShashi Mallela     rdbase &= RDBASE_PROCNUM_MASK;
4857eca39e0SShashi Mallela 
4867eca39e0SShashi Mallela     valid = (value & CMD_FIELD_VALID_MASK);
4877eca39e0SShashi Mallela 
4888b8bb014SPeter Maydell     if ((icid >= s->ct.num_entries) || (rdbase >= s->gicv3->num_cpu)) {
4897eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
4907eca39e0SShashi Mallela                       "ITS MAPC: invalid collection table attributes "
4917eca39e0SShashi Mallela                       "icid %d rdbase %" PRIu64 "\n",  icid, rdbase);
4927eca39e0SShashi Mallela         /*
4937eca39e0SShashi Mallela          * in this implementation, in case of error
4947eca39e0SShashi Mallela          * we ignore this command and move onto the next
4957eca39e0SShashi Mallela          * command in the queue
4967eca39e0SShashi Mallela          */
497f6675196SPeter Maydell         return CMD_CONTINUE;
4987eca39e0SShashi Mallela     }
4997eca39e0SShashi Mallela 
500f6675196SPeter Maydell     return update_cte(s, icid, valid, rdbase) ? CMD_CONTINUE : CMD_STALL;
5017eca39e0SShashi Mallela }
5027eca39e0SShashi Mallela 
5037eca39e0SShashi Mallela static bool update_dte(GICv3ITSState *s, uint32_t devid, bool valid,
5047eca39e0SShashi Mallela                        uint8_t size, uint64_t itt_addr)
5057eca39e0SShashi Mallela {
5067eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
507d050f80fSPeter Maydell     uint64_t entry_addr;
5087eca39e0SShashi Mallela     uint64_t dte = 0;
5097eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
5107eca39e0SShashi Mallela 
5117eca39e0SShashi Mallela     if (s->dt.valid) {
5127eca39e0SShashi Mallela         if (valid) {
5137eca39e0SShashi Mallela             /* add mapping entry to device table */
514e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, VALID, 1);
515e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, SIZE, size);
516e07f8445SPeter Maydell             dte = FIELD_DP64(dte, DTE, ITTADDR, itt_addr);
5177eca39e0SShashi Mallela         }
5187eca39e0SShashi Mallela     } else {
5197eca39e0SShashi Mallela         return true;
5207eca39e0SShashi Mallela     }
5217eca39e0SShashi Mallela 
522d050f80fSPeter Maydell     entry_addr = table_entry_addr(s, &s->dt, devid, &res);
5237eca39e0SShashi Mallela     if (res != MEMTX_OK) {
524d050f80fSPeter Maydell         /* memory access error: stall */
5257eca39e0SShashi Mallela         return false;
5267eca39e0SShashi Mallela     }
527d050f80fSPeter Maydell     if (entry_addr == -1) {
528d050f80fSPeter Maydell         /* No L2 table for this index: discard write and continue */
5297eca39e0SShashi Mallela         return true;
5307eca39e0SShashi Mallela     }
531d050f80fSPeter Maydell     address_space_stq_le(as, entry_addr, dte, MEMTXATTRS_UNSPECIFIED, &res);
532d050f80fSPeter Maydell     return res == MEMTX_OK;
5337eca39e0SShashi Mallela }
5347eca39e0SShashi Mallela 
535ef011555SPeter Maydell static ItsCmdResult process_mapd(GICv3ITSState *s, uint64_t value,
536ef011555SPeter Maydell                                  uint32_t offset)
5377eca39e0SShashi Mallela {
5387eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
5397eca39e0SShashi Mallela     uint32_t devid;
5407eca39e0SShashi Mallela     uint8_t size;
5417eca39e0SShashi Mallela     uint64_t itt_addr;
5427eca39e0SShashi Mallela     bool valid;
5437eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
5447eca39e0SShashi Mallela 
5457eca39e0SShashi Mallela     devid = ((value & DEVID_MASK) >> DEVID_SHIFT);
5467eca39e0SShashi Mallela 
5477eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
5487eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
5497eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
5507eca39e0SShashi Mallela 
5517eca39e0SShashi Mallela     if (res != MEMTX_OK) {
55200d46e72SPeter Maydell         return CMD_STALL;
5537eca39e0SShashi Mallela     }
5547eca39e0SShashi Mallela 
5557eca39e0SShashi Mallela     size = (value & SIZE_MASK);
5567eca39e0SShashi Mallela 
5577eca39e0SShashi Mallela     offset += NUM_BYTES_IN_DW;
5587eca39e0SShashi Mallela     value = address_space_ldq_le(as, s->cq.base_addr + offset,
5597eca39e0SShashi Mallela                                  MEMTXATTRS_UNSPECIFIED, &res);
5607eca39e0SShashi Mallela 
5617eca39e0SShashi Mallela     if (res != MEMTX_OK) {
56200d46e72SPeter Maydell         return CMD_STALL;
5637eca39e0SShashi Mallela     }
5647eca39e0SShashi Mallela 
5657eca39e0SShashi Mallela     itt_addr = (value & ITTADDR_MASK) >> ITTADDR_SHIFT;
5667eca39e0SShashi Mallela 
5677eca39e0SShashi Mallela     valid = (value & CMD_FIELD_VALID_MASK);
5687eca39e0SShashi Mallela 
5698b8bb014SPeter Maydell     if ((devid >= s->dt.num_entries) ||
5707eca39e0SShashi Mallela         (size > FIELD_EX64(s->typer, GITS_TYPER, IDBITS))) {
5717eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
5727eca39e0SShashi Mallela                       "ITS MAPD: invalid device table attributes "
5737eca39e0SShashi Mallela                       "devid %d or size %d\n", devid, size);
5747eca39e0SShashi Mallela         /*
5757eca39e0SShashi Mallela          * in this implementation, in case of error
5767eca39e0SShashi Mallela          * we ignore this command and move onto the next
5777eca39e0SShashi Mallela          * command in the queue
5787eca39e0SShashi Mallela          */
57900d46e72SPeter Maydell         return CMD_CONTINUE;
5807eca39e0SShashi Mallela     }
5817eca39e0SShashi Mallela 
58200d46e72SPeter Maydell     return update_dte(s, devid, valid, size, itt_addr) ? CMD_CONTINUE : CMD_STALL;
5837eca39e0SShashi Mallela }
5847eca39e0SShashi Mallela 
585*f6d1d9b4SPeter Maydell static ItsCmdResult process_movall(GICv3ITSState *s, uint64_t value,
586*f6d1d9b4SPeter Maydell                                    uint32_t offset)
587*f6d1d9b4SPeter Maydell {
588*f6d1d9b4SPeter Maydell     AddressSpace *as = &s->gicv3->dma_as;
589*f6d1d9b4SPeter Maydell     MemTxResult res = MEMTX_OK;
590*f6d1d9b4SPeter Maydell     uint64_t rd1, rd2;
591*f6d1d9b4SPeter Maydell 
592*f6d1d9b4SPeter Maydell     /* No fields in dwords 0 or 1 */
593*f6d1d9b4SPeter Maydell     offset += NUM_BYTES_IN_DW;
594*f6d1d9b4SPeter Maydell     offset += NUM_BYTES_IN_DW;
595*f6d1d9b4SPeter Maydell     value = address_space_ldq_le(as, s->cq.base_addr + offset,
596*f6d1d9b4SPeter Maydell                                  MEMTXATTRS_UNSPECIFIED, &res);
597*f6d1d9b4SPeter Maydell     if (res != MEMTX_OK) {
598*f6d1d9b4SPeter Maydell         return CMD_STALL;
599*f6d1d9b4SPeter Maydell     }
600*f6d1d9b4SPeter Maydell 
601*f6d1d9b4SPeter Maydell     rd1 = FIELD_EX64(value, MOVALL_2, RDBASE1);
602*f6d1d9b4SPeter Maydell     if (rd1 >= s->gicv3->num_cpu) {
603*f6d1d9b4SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
604*f6d1d9b4SPeter Maydell                       "%s: RDBASE1 %" PRId64
605*f6d1d9b4SPeter Maydell                       " out of range (must be less than %d)\n",
606*f6d1d9b4SPeter Maydell                       __func__, rd1, s->gicv3->num_cpu);
607*f6d1d9b4SPeter Maydell         return CMD_CONTINUE;
608*f6d1d9b4SPeter Maydell     }
609*f6d1d9b4SPeter Maydell 
610*f6d1d9b4SPeter Maydell     offset += NUM_BYTES_IN_DW;
611*f6d1d9b4SPeter Maydell     value = address_space_ldq_le(as, s->cq.base_addr + offset,
612*f6d1d9b4SPeter Maydell                                  MEMTXATTRS_UNSPECIFIED, &res);
613*f6d1d9b4SPeter Maydell     if (res != MEMTX_OK) {
614*f6d1d9b4SPeter Maydell         return CMD_STALL;
615*f6d1d9b4SPeter Maydell     }
616*f6d1d9b4SPeter Maydell 
617*f6d1d9b4SPeter Maydell     rd2 = FIELD_EX64(value, MOVALL_3, RDBASE2);
618*f6d1d9b4SPeter Maydell     if (rd2 >= s->gicv3->num_cpu) {
619*f6d1d9b4SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR,
620*f6d1d9b4SPeter Maydell                       "%s: RDBASE2 %" PRId64
621*f6d1d9b4SPeter Maydell                       " out of range (must be less than %d)\n",
622*f6d1d9b4SPeter Maydell                       __func__, rd2, s->gicv3->num_cpu);
623*f6d1d9b4SPeter Maydell         return CMD_CONTINUE;
624*f6d1d9b4SPeter Maydell     }
625*f6d1d9b4SPeter Maydell 
626*f6d1d9b4SPeter Maydell     if (rd1 == rd2) {
627*f6d1d9b4SPeter Maydell         /* Move to same target must succeed as a no-op */
628*f6d1d9b4SPeter Maydell         return CMD_CONTINUE;
629*f6d1d9b4SPeter Maydell     }
630*f6d1d9b4SPeter Maydell 
631*f6d1d9b4SPeter Maydell     /* Move all pending LPIs from redistributor 1 to redistributor 2 */
632*f6d1d9b4SPeter Maydell     gicv3_redist_movall_lpis(&s->gicv3->cpu[rd1], &s->gicv3->cpu[rd2]);
633*f6d1d9b4SPeter Maydell 
634*f6d1d9b4SPeter Maydell     return CMD_CONTINUE;
635*f6d1d9b4SPeter Maydell }
636*f6d1d9b4SPeter Maydell 
6377eca39e0SShashi Mallela /*
6387eca39e0SShashi Mallela  * Current implementation blocks until all
6397eca39e0SShashi Mallela  * commands are processed
6407eca39e0SShashi Mallela  */
6417eca39e0SShashi Mallela static void process_cmdq(GICv3ITSState *s)
6427eca39e0SShashi Mallela {
6437eca39e0SShashi Mallela     uint32_t wr_offset = 0;
6447eca39e0SShashi Mallela     uint32_t rd_offset = 0;
6457eca39e0SShashi Mallela     uint32_t cq_offset = 0;
6467eca39e0SShashi Mallela     uint64_t data;
6477eca39e0SShashi Mallela     AddressSpace *as = &s->gicv3->dma_as;
6487eca39e0SShashi Mallela     MemTxResult res = MEMTX_OK;
6497eca39e0SShashi Mallela     uint8_t cmd;
65017fb5e36SShashi Mallela     int i;
6517eca39e0SShashi Mallela 
6528d2d6dd9SPeter Maydell     if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
6537eca39e0SShashi Mallela         return;
6547eca39e0SShashi Mallela     }
6557eca39e0SShashi Mallela 
6567eca39e0SShashi Mallela     wr_offset = FIELD_EX64(s->cwriter, GITS_CWRITER, OFFSET);
6577eca39e0SShashi Mallela 
65880dcd37fSPeter Maydell     if (wr_offset >= s->cq.num_entries) {
6597eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
6607eca39e0SShashi Mallela                       "%s: invalid write offset "
6617eca39e0SShashi Mallela                       "%d\n", __func__, wr_offset);
6627eca39e0SShashi Mallela         return;
6637eca39e0SShashi Mallela     }
6647eca39e0SShashi Mallela 
6657eca39e0SShashi Mallela     rd_offset = FIELD_EX64(s->creadr, GITS_CREADR, OFFSET);
6667eca39e0SShashi Mallela 
66780dcd37fSPeter Maydell     if (rd_offset >= s->cq.num_entries) {
6687eca39e0SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
6697eca39e0SShashi Mallela                       "%s: invalid read offset "
6707eca39e0SShashi Mallela                       "%d\n", __func__, rd_offset);
6717eca39e0SShashi Mallela         return;
6727eca39e0SShashi Mallela     }
6737eca39e0SShashi Mallela 
6747eca39e0SShashi Mallela     while (wr_offset != rd_offset) {
675ef011555SPeter Maydell         ItsCmdResult result = CMD_CONTINUE;
676ef011555SPeter Maydell 
6777eca39e0SShashi Mallela         cq_offset = (rd_offset * GITS_CMDQ_ENTRY_SIZE);
6787eca39e0SShashi Mallela         data = address_space_ldq_le(as, s->cq.base_addr + cq_offset,
6797eca39e0SShashi Mallela                                     MEMTXATTRS_UNSPECIFIED, &res);
6807eca39e0SShashi Mallela         if (res != MEMTX_OK) {
681f0b4b2a2SPeter Maydell             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1);
682f0b4b2a2SPeter Maydell             qemu_log_mask(LOG_GUEST_ERROR,
683f0b4b2a2SPeter Maydell                           "%s: could not read command at 0x%" PRIx64 "\n",
684f0b4b2a2SPeter Maydell                           __func__, s->cq.base_addr + cq_offset);
685f0b4b2a2SPeter Maydell             break;
6867eca39e0SShashi Mallela         }
687f0b4b2a2SPeter Maydell 
6887eca39e0SShashi Mallela         cmd = (data & CMD_MASK);
6897eca39e0SShashi Mallela 
690195209d3SPeter Maydell         trace_gicv3_its_process_command(rd_offset, cmd);
691195209d3SPeter Maydell 
6927eca39e0SShashi Mallela         switch (cmd) {
6937eca39e0SShashi Mallela         case GITS_CMD_INT:
6947d62b2dcSPeter Maydell             result = process_its_cmd(s, data, cq_offset, INTERRUPT);
6957eca39e0SShashi Mallela             break;
6967eca39e0SShashi Mallela         case GITS_CMD_CLEAR:
6977d62b2dcSPeter Maydell             result = process_its_cmd(s, data, cq_offset, CLEAR);
6987eca39e0SShashi Mallela             break;
6997eca39e0SShashi Mallela         case GITS_CMD_SYNC:
7007eca39e0SShashi Mallela             /*
7017eca39e0SShashi Mallela              * Current implementation makes a blocking synchronous call
7027eca39e0SShashi Mallela              * for every command issued earlier, hence the internal state
7037eca39e0SShashi Mallela              * is already consistent by the time SYNC command is executed.
7047eca39e0SShashi Mallela              * Hence no further processing is required for SYNC command.
7057eca39e0SShashi Mallela              */
7067eca39e0SShashi Mallela             break;
7077eca39e0SShashi Mallela         case GITS_CMD_MAPD:
7087eca39e0SShashi Mallela             result = process_mapd(s, data, cq_offset);
7097eca39e0SShashi Mallela             break;
7107eca39e0SShashi Mallela         case GITS_CMD_MAPC:
7117eca39e0SShashi Mallela             result = process_mapc(s, cq_offset);
7127eca39e0SShashi Mallela             break;
7137eca39e0SShashi Mallela         case GITS_CMD_MAPTI:
714c694cb4cSShashi Mallela             result = process_mapti(s, data, cq_offset, false);
7157eca39e0SShashi Mallela             break;
7167eca39e0SShashi Mallela         case GITS_CMD_MAPI:
717c694cb4cSShashi Mallela             result = process_mapti(s, data, cq_offset, true);
7187eca39e0SShashi Mallela             break;
7197eca39e0SShashi Mallela         case GITS_CMD_DISCARD:
720c694cb4cSShashi Mallela             result = process_its_cmd(s, data, cq_offset, DISCARD);
7217eca39e0SShashi Mallela             break;
7227eca39e0SShashi Mallela         case GITS_CMD_INV:
7237eca39e0SShashi Mallela         case GITS_CMD_INVALL:
72417fb5e36SShashi Mallela             /*
72517fb5e36SShashi Mallela              * Current implementation doesn't cache any ITS tables,
72617fb5e36SShashi Mallela              * but the calculated lpi priority information. We only
72717fb5e36SShashi Mallela              * need to trigger lpi priority re-calculation to be in
72817fb5e36SShashi Mallela              * sync with LPI config table or pending table changes.
72917fb5e36SShashi Mallela              */
73017fb5e36SShashi Mallela             for (i = 0; i < s->gicv3->num_cpu; i++) {
73117fb5e36SShashi Mallela                 gicv3_redist_update_lpi(&s->gicv3->cpu[i]);
73217fb5e36SShashi Mallela             }
7337eca39e0SShashi Mallela             break;
734*f6d1d9b4SPeter Maydell         case GITS_CMD_MOVALL:
735*f6d1d9b4SPeter Maydell             result = process_movall(s, data, cq_offset);
736*f6d1d9b4SPeter Maydell             break;
7377eca39e0SShashi Mallela         default:
7387eca39e0SShashi Mallela             break;
7397eca39e0SShashi Mallela         }
740ef011555SPeter Maydell         if (result == CMD_CONTINUE) {
7417eca39e0SShashi Mallela             rd_offset++;
74280dcd37fSPeter Maydell             rd_offset %= s->cq.num_entries;
7437eca39e0SShashi Mallela             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, OFFSET, rd_offset);
7447eca39e0SShashi Mallela         } else {
745ef011555SPeter Maydell             /* CMD_STALL */
7467eca39e0SShashi Mallela             s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1);
7477eca39e0SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
748ef011555SPeter Maydell                           "%s: 0x%x cmd processing failed, stalling\n",
749ef011555SPeter Maydell                           __func__, cmd);
7507eca39e0SShashi Mallela             break;
7517eca39e0SShashi Mallela         }
7527eca39e0SShashi Mallela     }
7537eca39e0SShashi Mallela }
7547eca39e0SShashi Mallela 
7551b08e436SShashi Mallela /*
7561b08e436SShashi Mallela  * This function extracts the ITS Device and Collection table specific
7571b08e436SShashi Mallela  * parameters (like base_addr, size etc) from GITS_BASER register.
7581b08e436SShashi Mallela  * It is called during ITS enable and also during post_load migration
7591b08e436SShashi Mallela  */
7601b08e436SShashi Mallela static void extract_table_params(GICv3ITSState *s)
7611b08e436SShashi Mallela {
7621b08e436SShashi Mallela     uint16_t num_pages = 0;
7631b08e436SShashi Mallela     uint8_t  page_sz_type;
7641b08e436SShashi Mallela     uint8_t type;
7651b08e436SShashi Mallela     uint32_t page_sz = 0;
7661b08e436SShashi Mallela     uint64_t value;
7671b08e436SShashi Mallela 
7681b08e436SShashi Mallela     for (int i = 0; i < 8; i++) {
769e5487a41SPeter Maydell         TableDesc *td;
770e5487a41SPeter Maydell         int idbits;
771e5487a41SPeter Maydell 
7721b08e436SShashi Mallela         value = s->baser[i];
7731b08e436SShashi Mallela 
7741b08e436SShashi Mallela         if (!value) {
7751b08e436SShashi Mallela             continue;
7761b08e436SShashi Mallela         }
7771b08e436SShashi Mallela 
7781b08e436SShashi Mallela         page_sz_type = FIELD_EX64(value, GITS_BASER, PAGESIZE);
7791b08e436SShashi Mallela 
7801b08e436SShashi Mallela         switch (page_sz_type) {
7811b08e436SShashi Mallela         case 0:
7821b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_4K;
7831b08e436SShashi Mallela             break;
7841b08e436SShashi Mallela 
7851b08e436SShashi Mallela         case 1:
7861b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_16K;
7871b08e436SShashi Mallela             break;
7881b08e436SShashi Mallela 
7891b08e436SShashi Mallela         case 2:
7901b08e436SShashi Mallela         case 3:
7911b08e436SShashi Mallela             page_sz = GITS_PAGE_SIZE_64K;
7921b08e436SShashi Mallela             break;
7931b08e436SShashi Mallela 
7941b08e436SShashi Mallela         default:
7951b08e436SShashi Mallela             g_assert_not_reached();
7961b08e436SShashi Mallela         }
7971b08e436SShashi Mallela 
7981b08e436SShashi Mallela         num_pages = FIELD_EX64(value, GITS_BASER, SIZE) + 1;
7991b08e436SShashi Mallela 
8001b08e436SShashi Mallela         type = FIELD_EX64(value, GITS_BASER, TYPE);
8011b08e436SShashi Mallela 
8021b08e436SShashi Mallela         switch (type) {
8031b08e436SShashi Mallela         case GITS_BASER_TYPE_DEVICE:
804e5487a41SPeter Maydell             td = &s->dt;
805e5487a41SPeter Maydell             idbits = FIELD_EX64(s->typer, GITS_TYPER, DEVBITS) + 1;
80662df780eSPeter Maydell             break;
8071b08e436SShashi Mallela         case GITS_BASER_TYPE_COLLECTION:
808e5487a41SPeter Maydell             td = &s->ct;
8091b08e436SShashi Mallela             if (FIELD_EX64(s->typer, GITS_TYPER, CIL)) {
810e5487a41SPeter Maydell                 idbits = FIELD_EX64(s->typer, GITS_TYPER, CIDBITS) + 1;
8111b08e436SShashi Mallela             } else {
8121b08e436SShashi Mallela                 /* 16-bit CollectionId supported when CIL == 0 */
813e5487a41SPeter Maydell                 idbits = 16;
8141b08e436SShashi Mallela             }
8151b08e436SShashi Mallela             break;
8161b08e436SShashi Mallela         default:
817e5487a41SPeter Maydell             /*
818e5487a41SPeter Maydell              * GITS_BASER<n>.TYPE is read-only, so GITS_BASER_RO_MASK
819e5487a41SPeter Maydell              * ensures we will only see type values corresponding to
820e5487a41SPeter Maydell              * the values set up in gicv3_its_reset().
821e5487a41SPeter Maydell              */
822e5487a41SPeter Maydell             g_assert_not_reached();
8231b08e436SShashi Mallela         }
824e5487a41SPeter Maydell 
825e5487a41SPeter Maydell         memset(td, 0, sizeof(*td));
826e5487a41SPeter Maydell         td->valid = FIELD_EX64(value, GITS_BASER, VALID);
827e5487a41SPeter Maydell         /*
828e5487a41SPeter Maydell          * If GITS_BASER<n>.Valid is 0 for any <n> then we will not process
829e5487a41SPeter Maydell          * interrupts. (GITS_TYPER.HCC is 0 for this implementation, so we
830e5487a41SPeter Maydell          * do not have a special case where the GITS_BASER<n>.Valid bit is 0
831e5487a41SPeter Maydell          * for the register corresponding to the Collection table but we
832e5487a41SPeter Maydell          * still have to process interrupts using non-memory-backed
833e5487a41SPeter Maydell          * Collection table entries.)
834e5487a41SPeter Maydell          */
835e5487a41SPeter Maydell         if (!td->valid) {
836e5487a41SPeter Maydell             continue;
837e5487a41SPeter Maydell         }
838e5487a41SPeter Maydell         td->page_sz = page_sz;
839e5487a41SPeter Maydell         td->indirect = FIELD_EX64(value, GITS_BASER, INDIRECT);
8409ae85431SPeter Maydell         td->entry_sz = FIELD_EX64(value, GITS_BASER, ENTRYSIZE) + 1;
841e5487a41SPeter Maydell         td->base_addr = baser_base_addr(value, page_sz);
842e5487a41SPeter Maydell         if (!td->indirect) {
84380dcd37fSPeter Maydell             td->num_entries = (num_pages * page_sz) / td->entry_sz;
844e5487a41SPeter Maydell         } else {
84580dcd37fSPeter Maydell             td->num_entries = (((num_pages * page_sz) /
846e5487a41SPeter Maydell                                   L1TABLE_ENTRY_SIZE) *
847e5487a41SPeter Maydell                                  (page_sz / td->entry_sz));
848e5487a41SPeter Maydell         }
8498b8bb014SPeter Maydell         td->num_entries = MIN(td->num_entries, 1ULL << idbits);
8501b08e436SShashi Mallela     }
8511b08e436SShashi Mallela }
8521b08e436SShashi Mallela 
8531b08e436SShashi Mallela static void extract_cmdq_params(GICv3ITSState *s)
8541b08e436SShashi Mallela {
8551b08e436SShashi Mallela     uint16_t num_pages = 0;
8561b08e436SShashi Mallela     uint64_t value = s->cbaser;
8571b08e436SShashi Mallela 
8581b08e436SShashi Mallela     num_pages = FIELD_EX64(value, GITS_CBASER, SIZE) + 1;
8591b08e436SShashi Mallela 
8601b08e436SShashi Mallela     memset(&s->cq, 0 , sizeof(s->cq));
8611b08e436SShashi Mallela     s->cq.valid = FIELD_EX64(value, GITS_CBASER, VALID);
8621b08e436SShashi Mallela 
8631b08e436SShashi Mallela     if (s->cq.valid) {
86480dcd37fSPeter Maydell         s->cq.num_entries = (num_pages * GITS_PAGE_SIZE_4K) /
8651b08e436SShashi Mallela                              GITS_CMDQ_ENTRY_SIZE;
8661b08e436SShashi Mallela         s->cq.base_addr = FIELD_EX64(value, GITS_CBASER, PHYADDR);
8671b08e436SShashi Mallela         s->cq.base_addr <<= R_GITS_CBASER_PHYADDR_SHIFT;
8681b08e436SShashi Mallela     }
8691b08e436SShashi Mallela }
8701b08e436SShashi Mallela 
8717e062b98SPeter Maydell static MemTxResult gicv3_its_translation_read(void *opaque, hwaddr offset,
8727e062b98SPeter Maydell                                               uint64_t *data, unsigned size,
8737e062b98SPeter Maydell                                               MemTxAttrs attrs)
8747e062b98SPeter Maydell {
8757e062b98SPeter Maydell     /*
8767e062b98SPeter Maydell      * GITS_TRANSLATER is write-only, and all other addresses
8777e062b98SPeter Maydell      * in the interrupt translation space frame are RES0.
8787e062b98SPeter Maydell      */
8797e062b98SPeter Maydell     *data = 0;
8807e062b98SPeter Maydell     return MEMTX_OK;
8817e062b98SPeter Maydell }
8827e062b98SPeter Maydell 
88318f6290aSShashi Mallela static MemTxResult gicv3_its_translation_write(void *opaque, hwaddr offset,
88418f6290aSShashi Mallela                                                uint64_t data, unsigned size,
88518f6290aSShashi Mallela                                                MemTxAttrs attrs)
88618f6290aSShashi Mallela {
887c694cb4cSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
888c694cb4cSShashi Mallela     bool result = true;
889c694cb4cSShashi Mallela     uint32_t devid = 0;
890c694cb4cSShashi Mallela 
891195209d3SPeter Maydell     trace_gicv3_its_translation_write(offset, data, size, attrs.requester_id);
892195209d3SPeter Maydell 
893c694cb4cSShashi Mallela     switch (offset) {
894c694cb4cSShashi Mallela     case GITS_TRANSLATER:
8958d2d6dd9SPeter Maydell         if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) {
896c694cb4cSShashi Mallela             devid = attrs.requester_id;
897c694cb4cSShashi Mallela             result = process_its_cmd(s, data, devid, NONE);
898c694cb4cSShashi Mallela         }
899c694cb4cSShashi Mallela         break;
900c694cb4cSShashi Mallela     default:
901c694cb4cSShashi Mallela         break;
902c694cb4cSShashi Mallela     }
903c694cb4cSShashi Mallela 
904c694cb4cSShashi Mallela     if (result) {
90518f6290aSShashi Mallela         return MEMTX_OK;
906c694cb4cSShashi Mallela     } else {
907c694cb4cSShashi Mallela         return MEMTX_ERROR;
908c694cb4cSShashi Mallela     }
90918f6290aSShashi Mallela }
91018f6290aSShashi Mallela 
91118f6290aSShashi Mallela static bool its_writel(GICv3ITSState *s, hwaddr offset,
91218f6290aSShashi Mallela                               uint64_t value, MemTxAttrs attrs)
91318f6290aSShashi Mallela {
91418f6290aSShashi Mallela     bool result = true;
9151b08e436SShashi Mallela     int index;
91618f6290aSShashi Mallela 
9171b08e436SShashi Mallela     switch (offset) {
9181b08e436SShashi Mallela     case GITS_CTLR:
9192f459cd1SShashi Mallela         if (value & R_GITS_CTLR_ENABLED_MASK) {
9208d2d6dd9SPeter Maydell             s->ctlr |= R_GITS_CTLR_ENABLED_MASK;
9211b08e436SShashi Mallela             extract_table_params(s);
9221b08e436SShashi Mallela             extract_cmdq_params(s);
9237eca39e0SShashi Mallela             process_cmdq(s);
9242f459cd1SShashi Mallela         } else {
9258d2d6dd9SPeter Maydell             s->ctlr &= ~R_GITS_CTLR_ENABLED_MASK;
9261b08e436SShashi Mallela         }
9271b08e436SShashi Mallela         break;
9281b08e436SShashi Mallela     case GITS_CBASER:
9291b08e436SShashi Mallela         /*
9301b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
9311b08e436SShashi Mallela          *                 already enabled
9321b08e436SShashi Mallela          */
9338d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
9341b08e436SShashi Mallela             s->cbaser = deposit64(s->cbaser, 0, 32, value);
9351b08e436SShashi Mallela             s->creadr = 0;
9361b08e436SShashi Mallela         }
9371b08e436SShashi Mallela         break;
9381b08e436SShashi Mallela     case GITS_CBASER + 4:
9391b08e436SShashi Mallela         /*
9401b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
9411b08e436SShashi Mallela          *                 already enabled
9421b08e436SShashi Mallela          */
9438d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
9441b08e436SShashi Mallela             s->cbaser = deposit64(s->cbaser, 32, 32, value);
9451b08e436SShashi Mallela             s->creadr = 0;
9461b08e436SShashi Mallela         }
9471b08e436SShashi Mallela         break;
9481b08e436SShashi Mallela     case GITS_CWRITER:
9491b08e436SShashi Mallela         s->cwriter = deposit64(s->cwriter, 0, 32,
9501b08e436SShashi Mallela                                (value & ~R_GITS_CWRITER_RETRY_MASK));
9517eca39e0SShashi Mallela         if (s->cwriter != s->creadr) {
9527eca39e0SShashi Mallela             process_cmdq(s);
9537eca39e0SShashi Mallela         }
9541b08e436SShashi Mallela         break;
9551b08e436SShashi Mallela     case GITS_CWRITER + 4:
9561b08e436SShashi Mallela         s->cwriter = deposit64(s->cwriter, 32, 32, value);
9571b08e436SShashi Mallela         break;
9581b08e436SShashi Mallela     case GITS_CREADR:
9591b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
9601b08e436SShashi Mallela             s->creadr = deposit64(s->creadr, 0, 32,
9611b08e436SShashi Mallela                                   (value & ~R_GITS_CREADR_STALLED_MASK));
9621b08e436SShashi Mallela         } else {
9631b08e436SShashi Mallela             /* RO register, ignore the write */
9641b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
9651b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
9661b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
9671b08e436SShashi Mallela         }
9681b08e436SShashi Mallela         break;
9691b08e436SShashi Mallela     case GITS_CREADR + 4:
9701b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
9711b08e436SShashi Mallela             s->creadr = deposit64(s->creadr, 32, 32, value);
9721b08e436SShashi Mallela         } else {
9731b08e436SShashi Mallela             /* RO register, ignore the write */
9741b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
9751b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
9761b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
9771b08e436SShashi Mallela         }
9781b08e436SShashi Mallela         break;
9791b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
9801b08e436SShashi Mallela         /*
9811b08e436SShashi Mallela          * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is
9821b08e436SShashi Mallela          *                 already enabled
9831b08e436SShashi Mallela          */
9848d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
9851b08e436SShashi Mallela             index = (offset - GITS_BASER) / 8;
9861b08e436SShashi Mallela 
9870ffe88e6SPeter Maydell             if (s->baser[index] == 0) {
9880ffe88e6SPeter Maydell                 /* Unimplemented GITS_BASERn: RAZ/WI */
9890ffe88e6SPeter Maydell                 break;
9900ffe88e6SPeter Maydell             }
9911b08e436SShashi Mallela             if (offset & 7) {
9921b08e436SShashi Mallela                 value <<= 32;
9931b08e436SShashi Mallela                 value &= ~GITS_BASER_RO_MASK;
9941b08e436SShashi Mallela                 s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(0, 32);
9951b08e436SShashi Mallela                 s->baser[index] |= value;
9961b08e436SShashi Mallela             } else {
9971b08e436SShashi Mallela                 value &= ~GITS_BASER_RO_MASK;
9981b08e436SShashi Mallela                 s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(32, 32);
9991b08e436SShashi Mallela                 s->baser[index] |= value;
10001b08e436SShashi Mallela             }
10011b08e436SShashi Mallela         }
10021b08e436SShashi Mallela         break;
10031b08e436SShashi Mallela     case GITS_IIDR:
10041b08e436SShashi Mallela     case GITS_IDREGS ... GITS_IDREGS + 0x2f:
10051b08e436SShashi Mallela         /* RO registers, ignore the write */
10061b08e436SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
10071b08e436SShashi Mallela                       "%s: invalid guest write to RO register at offset "
10081b08e436SShashi Mallela                       TARGET_FMT_plx "\n", __func__, offset);
10091b08e436SShashi Mallela         break;
10101b08e436SShashi Mallela     default:
10111b08e436SShashi Mallela         result = false;
10121b08e436SShashi Mallela         break;
10131b08e436SShashi Mallela     }
101418f6290aSShashi Mallela     return result;
101518f6290aSShashi Mallela }
101618f6290aSShashi Mallela 
101718f6290aSShashi Mallela static bool its_readl(GICv3ITSState *s, hwaddr offset,
101818f6290aSShashi Mallela                              uint64_t *data, MemTxAttrs attrs)
101918f6290aSShashi Mallela {
102018f6290aSShashi Mallela     bool result = true;
10211b08e436SShashi Mallela     int index;
102218f6290aSShashi Mallela 
10231b08e436SShashi Mallela     switch (offset) {
10241b08e436SShashi Mallela     case GITS_CTLR:
10251b08e436SShashi Mallela         *data = s->ctlr;
10261b08e436SShashi Mallela         break;
10271b08e436SShashi Mallela     case GITS_IIDR:
10281b08e436SShashi Mallela         *data = gicv3_iidr();
10291b08e436SShashi Mallela         break;
10301b08e436SShashi Mallela     case GITS_IDREGS ... GITS_IDREGS + 0x2f:
10311b08e436SShashi Mallela         /* ID registers */
10321b08e436SShashi Mallela         *data = gicv3_idreg(offset - GITS_IDREGS);
10331b08e436SShashi Mallela         break;
10341b08e436SShashi Mallela     case GITS_TYPER:
10351b08e436SShashi Mallela         *data = extract64(s->typer, 0, 32);
10361b08e436SShashi Mallela         break;
10371b08e436SShashi Mallela     case GITS_TYPER + 4:
10381b08e436SShashi Mallela         *data = extract64(s->typer, 32, 32);
10391b08e436SShashi Mallela         break;
10401b08e436SShashi Mallela     case GITS_CBASER:
10411b08e436SShashi Mallela         *data = extract64(s->cbaser, 0, 32);
10421b08e436SShashi Mallela         break;
10431b08e436SShashi Mallela     case GITS_CBASER + 4:
10441b08e436SShashi Mallela         *data = extract64(s->cbaser, 32, 32);
10451b08e436SShashi Mallela         break;
10461b08e436SShashi Mallela     case GITS_CREADR:
10471b08e436SShashi Mallela         *data = extract64(s->creadr, 0, 32);
10481b08e436SShashi Mallela         break;
10491b08e436SShashi Mallela     case GITS_CREADR + 4:
10501b08e436SShashi Mallela         *data = extract64(s->creadr, 32, 32);
10511b08e436SShashi Mallela         break;
10521b08e436SShashi Mallela     case GITS_CWRITER:
10531b08e436SShashi Mallela         *data = extract64(s->cwriter, 0, 32);
10541b08e436SShashi Mallela         break;
10551b08e436SShashi Mallela     case GITS_CWRITER + 4:
10561b08e436SShashi Mallela         *data = extract64(s->cwriter, 32, 32);
10571b08e436SShashi Mallela         break;
10581b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
10591b08e436SShashi Mallela         index = (offset - GITS_BASER) / 8;
10601b08e436SShashi Mallela         if (offset & 7) {
10611b08e436SShashi Mallela             *data = extract64(s->baser[index], 32, 32);
10621b08e436SShashi Mallela         } else {
10631b08e436SShashi Mallela             *data = extract64(s->baser[index], 0, 32);
10641b08e436SShashi Mallela         }
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 bool its_writell(GICv3ITSState *s, hwaddr offset,
107418f6290aSShashi Mallela                                uint64_t value, MemTxAttrs attrs)
107518f6290aSShashi Mallela {
107618f6290aSShashi Mallela     bool result = true;
10771b08e436SShashi Mallela     int index;
107818f6290aSShashi Mallela 
10791b08e436SShashi Mallela     switch (offset) {
10801b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
10811b08e436SShashi Mallela         /*
10821b08e436SShashi Mallela          * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is
10831b08e436SShashi Mallela          *                 already enabled
10841b08e436SShashi Mallela          */
10858d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
10861b08e436SShashi Mallela             index = (offset - GITS_BASER) / 8;
10870ffe88e6SPeter Maydell             if (s->baser[index] == 0) {
10880ffe88e6SPeter Maydell                 /* Unimplemented GITS_BASERn: RAZ/WI */
10890ffe88e6SPeter Maydell                 break;
10900ffe88e6SPeter Maydell             }
10911b08e436SShashi Mallela             s->baser[index] &= GITS_BASER_RO_MASK;
10921b08e436SShashi Mallela             s->baser[index] |= (value & ~GITS_BASER_RO_MASK);
10931b08e436SShashi Mallela         }
10941b08e436SShashi Mallela         break;
10951b08e436SShashi Mallela     case GITS_CBASER:
10961b08e436SShashi Mallela         /*
10971b08e436SShashi Mallela          * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is
10981b08e436SShashi Mallela          *                 already enabled
10991b08e436SShashi Mallela          */
11008d2d6dd9SPeter Maydell         if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) {
11011b08e436SShashi Mallela             s->cbaser = value;
11021b08e436SShashi Mallela             s->creadr = 0;
11031b08e436SShashi Mallela         }
11041b08e436SShashi Mallela         break;
11051b08e436SShashi Mallela     case GITS_CWRITER:
11061b08e436SShashi Mallela         s->cwriter = value & ~R_GITS_CWRITER_RETRY_MASK;
11077eca39e0SShashi Mallela         if (s->cwriter != s->creadr) {
11087eca39e0SShashi Mallela             process_cmdq(s);
11097eca39e0SShashi Mallela         }
11101b08e436SShashi Mallela         break;
11111b08e436SShashi Mallela     case GITS_CREADR:
11121b08e436SShashi Mallela         if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) {
11131b08e436SShashi Mallela             s->creadr = value & ~R_GITS_CREADR_STALLED_MASK;
11141b08e436SShashi Mallela         } else {
11151b08e436SShashi Mallela             /* RO register, ignore the write */
11161b08e436SShashi Mallela             qemu_log_mask(LOG_GUEST_ERROR,
11171b08e436SShashi Mallela                           "%s: invalid guest write to RO register at offset "
11181b08e436SShashi Mallela                           TARGET_FMT_plx "\n", __func__, offset);
11191b08e436SShashi Mallela         }
11201b08e436SShashi Mallela         break;
11211b08e436SShashi Mallela     case GITS_TYPER:
11221b08e436SShashi Mallela         /* RO registers, ignore the write */
11231b08e436SShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
11241b08e436SShashi Mallela                       "%s: invalid guest write to RO register at offset "
11251b08e436SShashi Mallela                       TARGET_FMT_plx "\n", __func__, offset);
11261b08e436SShashi Mallela         break;
11271b08e436SShashi Mallela     default:
11281b08e436SShashi Mallela         result = false;
11291b08e436SShashi Mallela         break;
11301b08e436SShashi Mallela     }
113118f6290aSShashi Mallela     return result;
113218f6290aSShashi Mallela }
113318f6290aSShashi Mallela 
113418f6290aSShashi Mallela static bool its_readll(GICv3ITSState *s, hwaddr offset,
113518f6290aSShashi Mallela                               uint64_t *data, MemTxAttrs attrs)
113618f6290aSShashi Mallela {
113718f6290aSShashi Mallela     bool result = true;
11381b08e436SShashi Mallela     int index;
113918f6290aSShashi Mallela 
11401b08e436SShashi Mallela     switch (offset) {
11411b08e436SShashi Mallela     case GITS_TYPER:
11421b08e436SShashi Mallela         *data = s->typer;
11431b08e436SShashi Mallela         break;
11441b08e436SShashi Mallela     case GITS_BASER ... GITS_BASER + 0x3f:
11451b08e436SShashi Mallela         index = (offset - GITS_BASER) / 8;
11461b08e436SShashi Mallela         *data = s->baser[index];
11471b08e436SShashi Mallela         break;
11481b08e436SShashi Mallela     case GITS_CBASER:
11491b08e436SShashi Mallela         *data = s->cbaser;
11501b08e436SShashi Mallela         break;
11511b08e436SShashi Mallela     case GITS_CREADR:
11521b08e436SShashi Mallela         *data = s->creadr;
11531b08e436SShashi Mallela         break;
11541b08e436SShashi Mallela     case GITS_CWRITER:
11551b08e436SShashi Mallela         *data = s->cwriter;
11561b08e436SShashi Mallela         break;
11571b08e436SShashi Mallela     default:
11581b08e436SShashi Mallela         result = false;
11591b08e436SShashi Mallela         break;
11601b08e436SShashi Mallela     }
116118f6290aSShashi Mallela     return result;
116218f6290aSShashi Mallela }
116318f6290aSShashi Mallela 
116418f6290aSShashi Mallela static MemTxResult gicv3_its_read(void *opaque, hwaddr offset, uint64_t *data,
116518f6290aSShashi Mallela                                   unsigned size, MemTxAttrs attrs)
116618f6290aSShashi Mallela {
116718f6290aSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
116818f6290aSShashi Mallela     bool result;
116918f6290aSShashi Mallela 
117018f6290aSShashi Mallela     switch (size) {
117118f6290aSShashi Mallela     case 4:
117218f6290aSShashi Mallela         result = its_readl(s, offset, data, attrs);
117318f6290aSShashi Mallela         break;
117418f6290aSShashi Mallela     case 8:
117518f6290aSShashi Mallela         result = its_readll(s, offset, data, attrs);
117618f6290aSShashi Mallela         break;
117718f6290aSShashi Mallela     default:
117818f6290aSShashi Mallela         result = false;
117918f6290aSShashi Mallela         break;
118018f6290aSShashi Mallela     }
118118f6290aSShashi Mallela 
118218f6290aSShashi Mallela     if (!result) {
118318f6290aSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
118418f6290aSShashi Mallela                       "%s: invalid guest read at offset " TARGET_FMT_plx
118518f6290aSShashi Mallela                       "size %u\n", __func__, offset, size);
1186195209d3SPeter Maydell         trace_gicv3_its_badread(offset, size);
118718f6290aSShashi Mallela         /*
118818f6290aSShashi Mallela          * The spec requires that reserved registers are RAZ/WI;
118918f6290aSShashi Mallela          * so use false returns from leaf functions as a way to
119018f6290aSShashi Mallela          * trigger the guest-error logging but don't return it to
119118f6290aSShashi Mallela          * the caller, or we'll cause a spurious guest data abort.
119218f6290aSShashi Mallela          */
119318f6290aSShashi Mallela         *data = 0;
1194195209d3SPeter Maydell     } else {
1195195209d3SPeter Maydell         trace_gicv3_its_read(offset, *data, size);
119618f6290aSShashi Mallela     }
119718f6290aSShashi Mallela     return MEMTX_OK;
119818f6290aSShashi Mallela }
119918f6290aSShashi Mallela 
120018f6290aSShashi Mallela static MemTxResult gicv3_its_write(void *opaque, hwaddr offset, uint64_t data,
120118f6290aSShashi Mallela                                    unsigned size, MemTxAttrs attrs)
120218f6290aSShashi Mallela {
120318f6290aSShashi Mallela     GICv3ITSState *s = (GICv3ITSState *)opaque;
120418f6290aSShashi Mallela     bool result;
120518f6290aSShashi Mallela 
120618f6290aSShashi Mallela     switch (size) {
120718f6290aSShashi Mallela     case 4:
120818f6290aSShashi Mallela         result = its_writel(s, offset, data, attrs);
120918f6290aSShashi Mallela         break;
121018f6290aSShashi Mallela     case 8:
121118f6290aSShashi Mallela         result = its_writell(s, offset, data, attrs);
121218f6290aSShashi Mallela         break;
121318f6290aSShashi Mallela     default:
121418f6290aSShashi Mallela         result = false;
121518f6290aSShashi Mallela         break;
121618f6290aSShashi Mallela     }
121718f6290aSShashi Mallela 
121818f6290aSShashi Mallela     if (!result) {
121918f6290aSShashi Mallela         qemu_log_mask(LOG_GUEST_ERROR,
122018f6290aSShashi Mallela                       "%s: invalid guest write at offset " TARGET_FMT_plx
122118f6290aSShashi Mallela                       "size %u\n", __func__, offset, size);
1222195209d3SPeter Maydell         trace_gicv3_its_badwrite(offset, data, size);
122318f6290aSShashi Mallela         /*
122418f6290aSShashi Mallela          * The spec requires that reserved registers are RAZ/WI;
122518f6290aSShashi Mallela          * so use false returns from leaf functions as a way to
122618f6290aSShashi Mallela          * trigger the guest-error logging but don't return it to
122718f6290aSShashi Mallela          * the caller, or we'll cause a spurious guest data abort.
122818f6290aSShashi Mallela          */
1229195209d3SPeter Maydell     } else {
1230195209d3SPeter Maydell         trace_gicv3_its_write(offset, data, size);
123118f6290aSShashi Mallela     }
123218f6290aSShashi Mallela     return MEMTX_OK;
123318f6290aSShashi Mallela }
123418f6290aSShashi Mallela 
123518f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_control_ops = {
123618f6290aSShashi Mallela     .read_with_attrs = gicv3_its_read,
123718f6290aSShashi Mallela     .write_with_attrs = gicv3_its_write,
123818f6290aSShashi Mallela     .valid.min_access_size = 4,
123918f6290aSShashi Mallela     .valid.max_access_size = 8,
124018f6290aSShashi Mallela     .impl.min_access_size = 4,
124118f6290aSShashi Mallela     .impl.max_access_size = 8,
124218f6290aSShashi Mallela     .endianness = DEVICE_NATIVE_ENDIAN,
124318f6290aSShashi Mallela };
124418f6290aSShashi Mallela 
124518f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_translation_ops = {
12467e062b98SPeter Maydell     .read_with_attrs = gicv3_its_translation_read,
124718f6290aSShashi Mallela     .write_with_attrs = gicv3_its_translation_write,
124818f6290aSShashi Mallela     .valid.min_access_size = 2,
124918f6290aSShashi Mallela     .valid.max_access_size = 4,
125018f6290aSShashi Mallela     .impl.min_access_size = 2,
125118f6290aSShashi Mallela     .impl.max_access_size = 4,
125218f6290aSShashi Mallela     .endianness = DEVICE_NATIVE_ENDIAN,
125318f6290aSShashi Mallela };
125418f6290aSShashi Mallela 
125518f6290aSShashi Mallela static void gicv3_arm_its_realize(DeviceState *dev, Error **errp)
125618f6290aSShashi Mallela {
125718f6290aSShashi Mallela     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
125818f6290aSShashi Mallela     int i;
125918f6290aSShashi Mallela 
126018f6290aSShashi Mallela     for (i = 0; i < s->gicv3->num_cpu; i++) {
126118f6290aSShashi Mallela         if (!(s->gicv3->cpu[i].gicr_typer & GICR_TYPER_PLPIS)) {
126218f6290aSShashi Mallela             error_setg(errp, "Physical LPI not supported by CPU %d", i);
126318f6290aSShashi Mallela             return;
126418f6290aSShashi Mallela         }
126518f6290aSShashi Mallela     }
126618f6290aSShashi Mallela 
126718f6290aSShashi Mallela     gicv3_its_init_mmio(s, &gicv3_its_control_ops, &gicv3_its_translation_ops);
126818f6290aSShashi Mallela 
126918f6290aSShashi Mallela     /* set the ITS default features supported */
1270764d6ba1SPeter Maydell     s->typer = FIELD_DP64(s->typer, GITS_TYPER, PHYSICAL, 1);
127118f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, ITT_ENTRY_SIZE,
127218f6290aSShashi Mallela                           ITS_ITT_ENTRY_SIZE - 1);
127318f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, IDBITS, ITS_IDBITS);
127418f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, DEVBITS, ITS_DEVBITS);
127518f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIL, 1);
127618f6290aSShashi Mallela     s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIDBITS, ITS_CIDBITS);
127718f6290aSShashi Mallela }
127818f6290aSShashi Mallela 
127918f6290aSShashi Mallela static void gicv3_its_reset(DeviceState *dev)
128018f6290aSShashi Mallela {
128118f6290aSShashi Mallela     GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
128218f6290aSShashi Mallela     GICv3ITSClass *c = ARM_GICV3_ITS_GET_CLASS(s);
128318f6290aSShashi Mallela 
128418f6290aSShashi Mallela     c->parent_reset(dev);
128518f6290aSShashi Mallela 
128618f6290aSShashi Mallela     /* Quiescent bit reset to 1 */
128718f6290aSShashi Mallela     s->ctlr = FIELD_DP32(s->ctlr, GITS_CTLR, QUIESCENT, 1);
128818f6290aSShashi Mallela 
128918f6290aSShashi Mallela     /*
129018f6290aSShashi Mallela      * setting GITS_BASER0.Type = 0b001 (Device)
129118f6290aSShashi Mallela      *         GITS_BASER1.Type = 0b100 (Collection Table)
129218f6290aSShashi Mallela      *         GITS_BASER<n>.Type,where n = 3 to 7 are 0b00 (Unimplemented)
129318f6290aSShashi Mallela      *         GITS_BASER<0,1>.Page_Size = 64KB
129418f6290aSShashi Mallela      * and default translation table entry size to 16 bytes
129518f6290aSShashi Mallela      */
129618f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, TYPE,
129718f6290aSShashi Mallela                              GITS_BASER_TYPE_DEVICE);
129818f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, PAGESIZE,
129918f6290aSShashi Mallela                              GITS_BASER_PAGESIZE_64K);
130018f6290aSShashi Mallela     s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, ENTRYSIZE,
130118f6290aSShashi Mallela                              GITS_DTE_SIZE - 1);
130218f6290aSShashi Mallela 
130318f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, TYPE,
130418f6290aSShashi Mallela                              GITS_BASER_TYPE_COLLECTION);
130518f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, PAGESIZE,
130618f6290aSShashi Mallela                              GITS_BASER_PAGESIZE_64K);
130718f6290aSShashi Mallela     s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, ENTRYSIZE,
130818f6290aSShashi Mallela                              GITS_CTE_SIZE - 1);
130918f6290aSShashi Mallela }
131018f6290aSShashi Mallela 
13111b08e436SShashi Mallela static void gicv3_its_post_load(GICv3ITSState *s)
13121b08e436SShashi Mallela {
13138d2d6dd9SPeter Maydell     if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) {
13141b08e436SShashi Mallela         extract_table_params(s);
13151b08e436SShashi Mallela         extract_cmdq_params(s);
13161b08e436SShashi Mallela     }
13171b08e436SShashi Mallela }
13181b08e436SShashi Mallela 
131918f6290aSShashi Mallela static Property gicv3_its_props[] = {
132018f6290aSShashi Mallela     DEFINE_PROP_LINK("parent-gicv3", GICv3ITSState, gicv3, "arm-gicv3",
132118f6290aSShashi Mallela                      GICv3State *),
132218f6290aSShashi Mallela     DEFINE_PROP_END_OF_LIST(),
132318f6290aSShashi Mallela };
132418f6290aSShashi Mallela 
132518f6290aSShashi Mallela static void gicv3_its_class_init(ObjectClass *klass, void *data)
132618f6290aSShashi Mallela {
132718f6290aSShashi Mallela     DeviceClass *dc = DEVICE_CLASS(klass);
132818f6290aSShashi Mallela     GICv3ITSClass *ic = ARM_GICV3_ITS_CLASS(klass);
13291b08e436SShashi Mallela     GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass);
133018f6290aSShashi Mallela 
133118f6290aSShashi Mallela     dc->realize = gicv3_arm_its_realize;
133218f6290aSShashi Mallela     device_class_set_props(dc, gicv3_its_props);
133318f6290aSShashi Mallela     device_class_set_parent_reset(dc, gicv3_its_reset, &ic->parent_reset);
13341b08e436SShashi Mallela     icc->post_load = gicv3_its_post_load;
133518f6290aSShashi Mallela }
133618f6290aSShashi Mallela 
133718f6290aSShashi Mallela static const TypeInfo gicv3_its_info = {
133818f6290aSShashi Mallela     .name = TYPE_ARM_GICV3_ITS,
133918f6290aSShashi Mallela     .parent = TYPE_ARM_GICV3_ITS_COMMON,
134018f6290aSShashi Mallela     .instance_size = sizeof(GICv3ITSState),
134118f6290aSShashi Mallela     .class_init = gicv3_its_class_init,
134218f6290aSShashi Mallela     .class_size = sizeof(GICv3ITSClass),
134318f6290aSShashi Mallela };
134418f6290aSShashi Mallela 
134518f6290aSShashi Mallela static void gicv3_its_register_types(void)
134618f6290aSShashi Mallela {
134718f6290aSShashi Mallela     type_register_static(&gicv3_its_info);
134818f6290aSShashi Mallela }
134918f6290aSShashi Mallela 
135018f6290aSShashi Mallela type_init(gicv3_its_register_types)
1351