118f6290aSShashi Mallela /* 218f6290aSShashi Mallela * ITS emulation for a GICv3-based system 318f6290aSShashi Mallela * 418f6290aSShashi Mallela * Copyright Linaro.org 2021 518f6290aSShashi Mallela * 618f6290aSShashi Mallela * Authors: 718f6290aSShashi Mallela * Shashi Mallela <shashi.mallela@linaro.org> 818f6290aSShashi Mallela * 918f6290aSShashi Mallela * This work is licensed under the terms of the GNU GPL, version 2 or (at your 1018f6290aSShashi Mallela * option) any later version. See the COPYING file in the top-level directory. 1118f6290aSShashi Mallela * 1218f6290aSShashi Mallela */ 1318f6290aSShashi Mallela 1418f6290aSShashi Mallela #include "qemu/osdep.h" 1518f6290aSShashi Mallela #include "qemu/log.h" 16195209d3SPeter Maydell #include "trace.h" 1718f6290aSShashi Mallela #include "hw/qdev-properties.h" 1818f6290aSShashi Mallela #include "hw/intc/arm_gicv3_its_common.h" 1918f6290aSShashi Mallela #include "gicv3_internal.h" 2018f6290aSShashi Mallela #include "qom/object.h" 2118f6290aSShashi Mallela #include "qapi/error.h" 2218f6290aSShashi Mallela 2318f6290aSShashi Mallela typedef struct GICv3ITSClass GICv3ITSClass; 2418f6290aSShashi Mallela /* This is reusing the GICv3ITSState typedef from ARM_GICV3_ITS_COMMON */ 2518f6290aSShashi Mallela DECLARE_OBJ_CHECKERS(GICv3ITSState, GICv3ITSClass, 2618f6290aSShashi Mallela ARM_GICV3_ITS, TYPE_ARM_GICV3_ITS) 2718f6290aSShashi Mallela 2818f6290aSShashi Mallela struct GICv3ITSClass { 2918f6290aSShashi Mallela GICv3ITSCommonClass parent_class; 3018f6290aSShashi Mallela void (*parent_reset)(DeviceState *dev); 3118f6290aSShashi Mallela }; 3218f6290aSShashi Mallela 33c694cb4cSShashi Mallela /* 34c694cb4cSShashi Mallela * This is an internal enum used to distinguish between LPI triggered 35c694cb4cSShashi Mallela * via command queue and LPI triggered via gits_translater write. 36c694cb4cSShashi Mallela */ 37c694cb4cSShashi Mallela typedef enum ItsCmdType { 38c694cb4cSShashi Mallela NONE = 0, /* internal indication for GITS_TRANSLATER write */ 39c694cb4cSShashi Mallela CLEAR = 1, 40c694cb4cSShashi Mallela DISCARD = 2, 41c694cb4cSShashi Mallela INTERRUPT = 3, 42c694cb4cSShashi Mallela } ItsCmdType; 43c694cb4cSShashi Mallela 444acf93e1SPeter Maydell typedef struct DTEntry { 454acf93e1SPeter Maydell bool valid; 464acf93e1SPeter Maydell unsigned size; 474acf93e1SPeter Maydell uint64_t ittaddr; 484acf93e1SPeter Maydell } DTEntry; 494acf93e1SPeter Maydell 50d37cf49bSPeter Maydell typedef struct CTEntry { 51d37cf49bSPeter Maydell bool valid; 52d37cf49bSPeter Maydell uint32_t rdbase; 53d37cf49bSPeter Maydell } CTEntry; 54d37cf49bSPeter Maydell 55244194feSPeter Maydell typedef struct ITEntry { 56244194feSPeter Maydell bool valid; 57244194feSPeter Maydell int inttype; 58244194feSPeter Maydell uint32_t intid; 59244194feSPeter Maydell uint32_t doorbell; 60244194feSPeter Maydell uint32_t icid; 61244194feSPeter Maydell uint32_t vpeid; 62244194feSPeter Maydell } ITEntry; 63244194feSPeter Maydell 64244194feSPeter Maydell 65ef011555SPeter Maydell /* 66ef011555SPeter Maydell * The ITS spec permits a range of CONSTRAINED UNPREDICTABLE options 67ef011555SPeter Maydell * if a command parameter is not correct. These include both "stall 68ef011555SPeter Maydell * processing of the command queue" and "ignore this command, and 69ef011555SPeter Maydell * keep processing the queue". In our implementation we choose that 70ef011555SPeter Maydell * memory transaction errors reading the command packet provoke a 71ef011555SPeter Maydell * stall, but errors in parameters cause us to ignore the command 72ef011555SPeter Maydell * and continue processing. 73ef011555SPeter Maydell * The process_* functions which handle individual ITS commands all 74ef011555SPeter Maydell * return an ItsCmdResult which tells process_cmdq() whether it should 75ef011555SPeter Maydell * stall or keep going. 76ef011555SPeter Maydell */ 77ef011555SPeter Maydell typedef enum ItsCmdResult { 78ef011555SPeter Maydell CMD_STALL = 0, 79ef011555SPeter Maydell CMD_CONTINUE = 1, 80ef011555SPeter Maydell } ItsCmdResult; 81ef011555SPeter Maydell 821b08e436SShashi Mallela static uint64_t baser_base_addr(uint64_t value, uint32_t page_sz) 831b08e436SShashi Mallela { 841b08e436SShashi Mallela uint64_t result = 0; 851b08e436SShashi Mallela 861b08e436SShashi Mallela switch (page_sz) { 871b08e436SShashi Mallela case GITS_PAGE_SIZE_4K: 881b08e436SShashi Mallela case GITS_PAGE_SIZE_16K: 891b08e436SShashi Mallela result = FIELD_EX64(value, GITS_BASER, PHYADDR) << 12; 901b08e436SShashi Mallela break; 911b08e436SShashi Mallela 921b08e436SShashi Mallela case GITS_PAGE_SIZE_64K: 931b08e436SShashi Mallela result = FIELD_EX64(value, GITS_BASER, PHYADDRL_64K) << 16; 941b08e436SShashi Mallela result |= FIELD_EX64(value, GITS_BASER, PHYADDRH_64K) << 48; 951b08e436SShashi Mallela break; 961b08e436SShashi Mallela 971b08e436SShashi Mallela default: 981b08e436SShashi Mallela break; 991b08e436SShashi Mallela } 1001b08e436SShashi Mallela return result; 1011b08e436SShashi Mallela } 1021b08e436SShashi Mallela 103d050f80fSPeter Maydell static uint64_t table_entry_addr(GICv3ITSState *s, TableDesc *td, 104d050f80fSPeter Maydell uint32_t idx, MemTxResult *res) 105d050f80fSPeter Maydell { 106d050f80fSPeter Maydell /* 107d050f80fSPeter Maydell * Given a TableDesc describing one of the ITS in-guest-memory 108d050f80fSPeter Maydell * tables and an index into it, return the guest address 109d050f80fSPeter Maydell * corresponding to that table entry. 110d050f80fSPeter Maydell * If there was a memory error reading the L1 table of an 111d050f80fSPeter Maydell * indirect table, *res is set accordingly, and we return -1. 112d050f80fSPeter Maydell * If the L1 table entry is marked not valid, we return -1 with 113d050f80fSPeter Maydell * *res set to MEMTX_OK. 114d050f80fSPeter Maydell * 115d050f80fSPeter Maydell * The specification defines the format of level 1 entries of a 116d050f80fSPeter Maydell * 2-level table, but the format of level 2 entries and the format 117d050f80fSPeter Maydell * of flat-mapped tables is IMPDEF. 118d050f80fSPeter Maydell */ 119d050f80fSPeter Maydell AddressSpace *as = &s->gicv3->dma_as; 120d050f80fSPeter Maydell uint32_t l2idx; 121d050f80fSPeter Maydell uint64_t l2; 122d050f80fSPeter Maydell uint32_t num_l2_entries; 123d050f80fSPeter Maydell 124d050f80fSPeter Maydell *res = MEMTX_OK; 125d050f80fSPeter Maydell 126d050f80fSPeter Maydell if (!td->indirect) { 127d050f80fSPeter Maydell /* Single level table */ 128d050f80fSPeter Maydell return td->base_addr + idx * td->entry_sz; 129d050f80fSPeter Maydell } 130d050f80fSPeter Maydell 131d050f80fSPeter Maydell /* Two level table */ 132d050f80fSPeter Maydell l2idx = idx / (td->page_sz / L1TABLE_ENTRY_SIZE); 133d050f80fSPeter Maydell 134d050f80fSPeter Maydell l2 = address_space_ldq_le(as, 135d050f80fSPeter Maydell td->base_addr + (l2idx * L1TABLE_ENTRY_SIZE), 136d050f80fSPeter Maydell MEMTXATTRS_UNSPECIFIED, res); 137d050f80fSPeter Maydell if (*res != MEMTX_OK) { 138d050f80fSPeter Maydell return -1; 139d050f80fSPeter Maydell } 140d050f80fSPeter Maydell if (!(l2 & L2_TABLE_VALID_MASK)) { 141d050f80fSPeter Maydell return -1; 142d050f80fSPeter Maydell } 143d050f80fSPeter Maydell 144d050f80fSPeter Maydell num_l2_entries = td->page_sz / td->entry_sz; 145d050f80fSPeter Maydell return (l2 & ((1ULL << 51) - 1)) + (idx % num_l2_entries) * td->entry_sz; 146d050f80fSPeter Maydell } 147d050f80fSPeter Maydell 148d37cf49bSPeter Maydell /* 149d37cf49bSPeter Maydell * Read the Collection Table entry at index @icid. On success (including 150d37cf49bSPeter Maydell * successfully determining that there is no valid CTE for this index), 151d37cf49bSPeter Maydell * we return MEMTX_OK and populate the CTEntry struct @cte accordingly. 152d37cf49bSPeter Maydell * If there is an error reading memory then we return the error code. 153d37cf49bSPeter Maydell */ 154d37cf49bSPeter Maydell static MemTxResult get_cte(GICv3ITSState *s, uint16_t icid, CTEntry *cte) 155c694cb4cSShashi Mallela { 156c694cb4cSShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 157d37cf49bSPeter Maydell MemTxResult res = MEMTX_OK; 158d37cf49bSPeter Maydell uint64_t entry_addr = table_entry_addr(s, &s->ct, icid, &res); 159d37cf49bSPeter Maydell uint64_t cteval; 160c694cb4cSShashi Mallela 161d050f80fSPeter Maydell if (entry_addr == -1) { 162d37cf49bSPeter Maydell /* No L2 table entry, i.e. no valid CTE, or a memory error */ 163d37cf49bSPeter Maydell cte->valid = false; 164d37cf49bSPeter Maydell return res; 165c694cb4cSShashi Mallela } 166c694cb4cSShashi Mallela 167d37cf49bSPeter Maydell cteval = address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, &res); 168d37cf49bSPeter Maydell if (res != MEMTX_OK) { 169d37cf49bSPeter Maydell return res; 170d37cf49bSPeter Maydell } 171d37cf49bSPeter Maydell cte->valid = FIELD_EX64(cteval, CTE, VALID); 172d37cf49bSPeter Maydell cte->rdbase = FIELD_EX64(cteval, CTE, RDBASE); 173d37cf49bSPeter Maydell return MEMTX_OK; 174c694cb4cSShashi Mallela } 175c694cb4cSShashi Mallela 176*7eb54267SPeter Maydell /* 177*7eb54267SPeter Maydell * Update the Interrupt Table entry at index @evinted in the table specified 178*7eb54267SPeter Maydell * by the dte @dte. Returns true on success, false if there was a memory 179*7eb54267SPeter Maydell * access error. 180*7eb54267SPeter Maydell */ 1814acf93e1SPeter Maydell static bool update_ite(GICv3ITSState *s, uint32_t eventid, const DTEntry *dte, 182*7eb54267SPeter Maydell const ITEntry *ite) 183c694cb4cSShashi Mallela { 184c694cb4cSShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 185c694cb4cSShashi Mallela MemTxResult res = MEMTX_OK; 186a1ce993dSPeter Maydell hwaddr iteaddr = dte->ittaddr + eventid * ITS_ITT_ENTRY_SIZE; 187*7eb54267SPeter Maydell uint64_t itel = 0; 188*7eb54267SPeter Maydell uint32_t iteh = 0; 189c694cb4cSShashi Mallela 190*7eb54267SPeter Maydell if (ite->valid) { 191*7eb54267SPeter Maydell itel = FIELD_DP64(itel, ITE_L, VALID, 1); 192*7eb54267SPeter Maydell itel = FIELD_DP64(itel, ITE_L, INTTYPE, ite->inttype); 193*7eb54267SPeter Maydell itel = FIELD_DP64(itel, ITE_L, INTID, ite->intid); 194*7eb54267SPeter Maydell itel = FIELD_DP64(itel, ITE_L, ICID, ite->icid); 195*7eb54267SPeter Maydell itel = FIELD_DP64(itel, ITE_L, VPEID, ite->vpeid); 196*7eb54267SPeter Maydell iteh = FIELD_DP32(iteh, ITE_H, DOORBELL, ite->doorbell); 197c694cb4cSShashi Mallela } 198*7eb54267SPeter Maydell 199*7eb54267SPeter Maydell address_space_stq_le(as, iteaddr, itel, MEMTXATTRS_UNSPECIFIED, &res); 200c694cb4cSShashi Mallela if (res != MEMTX_OK) { 201c694cb4cSShashi Mallela return false; 202c694cb4cSShashi Mallela } 203*7eb54267SPeter Maydell address_space_stl_le(as, iteaddr + 8, iteh, MEMTXATTRS_UNSPECIFIED, &res); 204*7eb54267SPeter Maydell return res == MEMTX_OK; 205c694cb4cSShashi Mallela } 206c694cb4cSShashi Mallela 207244194feSPeter Maydell /* 208244194feSPeter Maydell * Read the Interrupt Table entry at index @eventid from the table specified 209244194feSPeter Maydell * by the DTE @dte. On success, we return MEMTX_OK and populate the ITEntry 210244194feSPeter Maydell * struct @ite accordingly. If there is an error reading memory then we return 211244194feSPeter Maydell * the error code. 212244194feSPeter Maydell */ 213244194feSPeter Maydell static MemTxResult get_ite(GICv3ITSState *s, uint32_t eventid, 214244194feSPeter Maydell const DTEntry *dte, ITEntry *ite) 215c694cb4cSShashi Mallela { 216c694cb4cSShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 217244194feSPeter Maydell MemTxResult res = MEMTX_OK; 218244194feSPeter Maydell uint64_t itel; 219244194feSPeter Maydell uint32_t iteh; 220a1ce993dSPeter Maydell hwaddr iteaddr = dte->ittaddr + eventid * ITS_ITT_ENTRY_SIZE; 221c694cb4cSShashi Mallela 222244194feSPeter Maydell itel = address_space_ldq_le(as, iteaddr, MEMTXATTRS_UNSPECIFIED, &res); 223244194feSPeter Maydell if (res != MEMTX_OK) { 224244194feSPeter Maydell return res; 2252954b93fSPeter Maydell } 226c694cb4cSShashi Mallela 227244194feSPeter Maydell iteh = address_space_ldl_le(as, iteaddr + 8, MEMTXATTRS_UNSPECIFIED, &res); 228244194feSPeter Maydell if (res != MEMTX_OK) { 229244194feSPeter Maydell return res; 2302954b93fSPeter Maydell } 231c694cb4cSShashi Mallela 232244194feSPeter Maydell ite->valid = FIELD_EX64(itel, ITE_L, VALID); 233244194feSPeter Maydell ite->inttype = FIELD_EX64(itel, ITE_L, INTTYPE); 234244194feSPeter Maydell ite->intid = FIELD_EX64(itel, ITE_L, INTID); 235244194feSPeter Maydell ite->icid = FIELD_EX64(itel, ITE_L, ICID); 236244194feSPeter Maydell ite->vpeid = FIELD_EX64(itel, ITE_L, VPEID); 237244194feSPeter Maydell ite->doorbell = FIELD_EX64(iteh, ITE_H, DOORBELL); 238244194feSPeter Maydell return MEMTX_OK; 239c694cb4cSShashi Mallela } 240c694cb4cSShashi Mallela 2414acf93e1SPeter Maydell /* 2424acf93e1SPeter Maydell * Read the Device Table entry at index @devid. On success (including 2434acf93e1SPeter Maydell * successfully determining that there is no valid DTE for this index), 2444acf93e1SPeter Maydell * we return MEMTX_OK and populate the DTEntry struct accordingly. 2454acf93e1SPeter Maydell * If there is an error reading memory then we return the error code. 2464acf93e1SPeter Maydell */ 2474acf93e1SPeter Maydell static MemTxResult get_dte(GICv3ITSState *s, uint32_t devid, DTEntry *dte) 248c694cb4cSShashi Mallela { 2494acf93e1SPeter Maydell MemTxResult res = MEMTX_OK; 250c694cb4cSShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 2514acf93e1SPeter Maydell uint64_t entry_addr = table_entry_addr(s, &s->dt, devid, &res); 2524acf93e1SPeter Maydell uint64_t dteval; 253c694cb4cSShashi Mallela 254d050f80fSPeter Maydell if (entry_addr == -1) { 2554acf93e1SPeter Maydell /* No L2 table entry, i.e. no valid DTE, or a memory error */ 2564acf93e1SPeter Maydell dte->valid = false; 2574acf93e1SPeter Maydell return res; 258c694cb4cSShashi Mallela } 2594acf93e1SPeter Maydell dteval = address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, &res); 2604acf93e1SPeter Maydell if (res != MEMTX_OK) { 2614acf93e1SPeter Maydell return res; 2624acf93e1SPeter Maydell } 2634acf93e1SPeter Maydell dte->valid = FIELD_EX64(dteval, DTE, VALID); 2644acf93e1SPeter Maydell dte->size = FIELD_EX64(dteval, DTE, SIZE); 2654acf93e1SPeter Maydell /* DTE word field stores bits [51:8] of the ITT address */ 2664acf93e1SPeter Maydell dte->ittaddr = FIELD_EX64(dteval, DTE, ITTADDR) << ITTADDR_SHIFT; 2674acf93e1SPeter Maydell return MEMTX_OK; 268c694cb4cSShashi Mallela } 269c694cb4cSShashi Mallela 270c694cb4cSShashi Mallela /* 271c694cb4cSShashi Mallela * This function handles the processing of following commands based on 272c694cb4cSShashi Mallela * the ItsCmdType parameter passed:- 273c694cb4cSShashi Mallela * 1. triggering of lpi interrupt translation via ITS INT command 274c694cb4cSShashi Mallela * 2. triggering of lpi interrupt translation via gits_translater register 275c694cb4cSShashi Mallela * 3. handling of ITS CLEAR command 276c694cb4cSShashi Mallela * 4. handling of ITS DISCARD command 277c694cb4cSShashi Mallela */ 278b6f96009SPeter Maydell static ItsCmdResult do_process_its_cmd(GICv3ITSState *s, uint32_t devid, 279b6f96009SPeter Maydell uint32_t eventid, ItsCmdType cmd) 280c694cb4cSShashi Mallela { 2818f809f69SPeter Maydell uint64_t num_eventids; 2824acf93e1SPeter Maydell DTEntry dte; 283d37cf49bSPeter Maydell CTEntry cte; 284244194feSPeter Maydell ITEntry ite; 285c694cb4cSShashi Mallela 2868b8bb014SPeter Maydell if (devid >= s->dt.num_entries) { 287b13148d9SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 288b13148d9SPeter Maydell "%s: invalid command attributes: devid %d>=%d", 2898b8bb014SPeter Maydell __func__, devid, s->dt.num_entries); 290b13148d9SPeter Maydell return CMD_CONTINUE; 291b13148d9SPeter Maydell } 292b13148d9SPeter Maydell 2934acf93e1SPeter Maydell if (get_dte(s, devid, &dte) != MEMTX_OK) { 294593a7cc2SPeter Maydell return CMD_STALL; 295c694cb4cSShashi Mallela } 2964acf93e1SPeter Maydell if (!dte.valid) { 297229c57b1SAlex Bennée qemu_log_mask(LOG_GUEST_ERROR, 298229c57b1SAlex Bennée "%s: invalid command attributes: " 2994acf93e1SPeter Maydell "invalid dte for %d\n", __func__, devid); 300593a7cc2SPeter Maydell return CMD_CONTINUE; 301c694cb4cSShashi Mallela } 302c694cb4cSShashi Mallela 3034acf93e1SPeter Maydell num_eventids = 1ULL << (dte.size + 1); 304b13148d9SPeter Maydell if (eventid >= num_eventids) { 305b13148d9SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 306b13148d9SPeter Maydell "%s: invalid command attributes: eventid %d >= %" 307b13148d9SPeter Maydell PRId64 "\n", 308b13148d9SPeter Maydell __func__, eventid, num_eventids); 309b13148d9SPeter Maydell return CMD_CONTINUE; 310b13148d9SPeter Maydell } 311b13148d9SPeter Maydell 312244194feSPeter Maydell if (get_ite(s, eventid, &dte, &ite) != MEMTX_OK) { 313be0ed8fbSPeter Maydell return CMD_STALL; 314be0ed8fbSPeter Maydell } 315be0ed8fbSPeter Maydell 316244194feSPeter Maydell if (!ite.valid || ite.inttype != ITE_INTTYPE_PHYSICAL) { 317be0ed8fbSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 318be0ed8fbSPeter Maydell "%s: invalid command attributes: invalid ITE\n", 319be0ed8fbSPeter Maydell __func__); 320be0ed8fbSPeter Maydell return CMD_CONTINUE; 321be0ed8fbSPeter Maydell } 322be0ed8fbSPeter Maydell 323244194feSPeter Maydell if (ite.icid >= s->ct.num_entries) { 32458b88779SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 32558b88779SPeter Maydell "%s: invalid ICID 0x%x in ITE (table corrupted?)\n", 326244194feSPeter Maydell __func__, ite.icid); 32758b88779SPeter Maydell return CMD_CONTINUE; 32858b88779SPeter Maydell } 32958b88779SPeter Maydell 330244194feSPeter Maydell if (get_cte(s, ite.icid, &cte) != MEMTX_OK) { 331be0ed8fbSPeter Maydell return CMD_STALL; 332be0ed8fbSPeter Maydell } 333d37cf49bSPeter Maydell if (!cte.valid) { 334be0ed8fbSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 335d37cf49bSPeter Maydell "%s: invalid command attributes: invalid CTE\n", 336d37cf49bSPeter Maydell __func__); 337be0ed8fbSPeter Maydell return CMD_CONTINUE; 338be0ed8fbSPeter Maydell } 339be0ed8fbSPeter Maydell 340c694cb4cSShashi Mallela /* 341c694cb4cSShashi Mallela * Current implementation only supports rdbase == procnum 342c694cb4cSShashi Mallela * Hence rdbase physical address is ignored 343c694cb4cSShashi Mallela */ 344d37cf49bSPeter Maydell if (cte.rdbase >= s->gicv3->num_cpu) { 345593a7cc2SPeter Maydell return CMD_CONTINUE; 34617fb5e36SShashi Mallela } 34717fb5e36SShashi Mallela 34817fb5e36SShashi Mallela if ((cmd == CLEAR) || (cmd == DISCARD)) { 349244194feSPeter Maydell gicv3_redist_process_lpi(&s->gicv3->cpu[cte.rdbase], ite.intid, 0); 35017fb5e36SShashi Mallela } else { 351244194feSPeter Maydell gicv3_redist_process_lpi(&s->gicv3->cpu[cte.rdbase], ite.intid, 1); 35217fb5e36SShashi Mallela } 35317fb5e36SShashi Mallela 354c694cb4cSShashi Mallela if (cmd == DISCARD) { 355*7eb54267SPeter Maydell ITEntry ite = {}; 356c694cb4cSShashi Mallela /* remove mapping from interrupt translation table */ 357*7eb54267SPeter Maydell ite.valid = false; 358*7eb54267SPeter Maydell return update_ite(s, eventid, &dte, &ite) ? CMD_CONTINUE : CMD_STALL; 359c694cb4cSShashi Mallela } 360593a7cc2SPeter Maydell return CMD_CONTINUE; 361c694cb4cSShashi Mallela } 362b6f96009SPeter Maydell static ItsCmdResult process_its_cmd(GICv3ITSState *s, const uint64_t *cmdpkt, 363b6f96009SPeter Maydell ItsCmdType cmd) 364c694cb4cSShashi Mallela { 365b6f96009SPeter Maydell uint32_t devid, eventid; 366b6f96009SPeter Maydell 367b6f96009SPeter Maydell devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT; 368b6f96009SPeter Maydell eventid = cmdpkt[1] & EVENTID_MASK; 369b6f96009SPeter Maydell return do_process_its_cmd(s, devid, eventid, cmd); 370b6f96009SPeter Maydell } 371b6f96009SPeter Maydell 372b6f96009SPeter Maydell static ItsCmdResult process_mapti(GICv3ITSState *s, const uint64_t *cmdpkt, 373b6f96009SPeter Maydell bool ignore_pInt) 374b6f96009SPeter Maydell { 375c694cb4cSShashi Mallela uint32_t devid, eventid; 376c694cb4cSShashi Mallela uint32_t pIntid = 0; 3778f809f69SPeter Maydell uint64_t num_eventids; 378905720f1SPeter Maydell uint32_t num_intids; 379c694cb4cSShashi Mallela uint16_t icid = 0; 3804acf93e1SPeter Maydell DTEntry dte; 381*7eb54267SPeter Maydell ITEntry ite; 382c694cb4cSShashi Mallela 383b6f96009SPeter Maydell devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT; 384b6f96009SPeter Maydell eventid = cmdpkt[1] & EVENTID_MASK; 385c694cb4cSShashi Mallela 386b87fab1cSPeter Maydell if (ignore_pInt) { 387b87fab1cSPeter Maydell pIntid = eventid; 388b87fab1cSPeter Maydell } else { 389b6f96009SPeter Maydell pIntid = (cmdpkt[1] & pINTID_MASK) >> pINTID_SHIFT; 390c694cb4cSShashi Mallela } 391c694cb4cSShashi Mallela 392b6f96009SPeter Maydell icid = cmdpkt[2] & ICID_MASK; 393c694cb4cSShashi Mallela 3948b8bb014SPeter Maydell if (devid >= s->dt.num_entries) { 395b13148d9SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 396b13148d9SPeter Maydell "%s: invalid command attributes: devid %d>=%d", 3978b8bb014SPeter Maydell __func__, devid, s->dt.num_entries); 398b13148d9SPeter Maydell return CMD_CONTINUE; 399b13148d9SPeter Maydell } 400b13148d9SPeter Maydell 4014acf93e1SPeter Maydell if (get_dte(s, devid, &dte) != MEMTX_OK) { 4020241f731SPeter Maydell return CMD_STALL; 403c694cb4cSShashi Mallela } 4044acf93e1SPeter Maydell num_eventids = 1ULL << (dte.size + 1); 405905720f1SPeter Maydell num_intids = 1ULL << (GICD_TYPER_IDBITS + 1); 406c694cb4cSShashi Mallela 4078b8bb014SPeter Maydell if ((icid >= s->ct.num_entries) 4084acf93e1SPeter Maydell || !dte.valid || (eventid >= num_eventids) || 409905720f1SPeter Maydell (((pIntid < GICV3_LPI_INTID_START) || (pIntid >= num_intids)) && 410b87fab1cSPeter Maydell (pIntid != INTID_SPURIOUS))) { 411c694cb4cSShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 412c694cb4cSShashi Mallela "%s: invalid command attributes " 413b13148d9SPeter Maydell "icid %d or eventid %d or pIntid %d or" 414b13148d9SPeter Maydell "unmapped dte %d\n", __func__, icid, eventid, 4154acf93e1SPeter Maydell pIntid, dte.valid); 416c694cb4cSShashi Mallela /* 417c694cb4cSShashi Mallela * in this implementation, in case of error 418c694cb4cSShashi Mallela * we ignore this command and move onto the next 419c694cb4cSShashi Mallela * command in the queue 420c694cb4cSShashi Mallela */ 4210241f731SPeter Maydell return CMD_CONTINUE; 4220241f731SPeter Maydell } 4230241f731SPeter Maydell 424c694cb4cSShashi Mallela /* add ite entry to interrupt translation table */ 425*7eb54267SPeter Maydell ite.valid = true; 426*7eb54267SPeter Maydell ite.inttype = ITE_INTTYPE_PHYSICAL; 427*7eb54267SPeter Maydell ite.intid = pIntid; 428*7eb54267SPeter Maydell ite.icid = icid; 429*7eb54267SPeter Maydell ite.doorbell = INTID_SPURIOUS; 430*7eb54267SPeter Maydell ite.vpeid = 0; 431*7eb54267SPeter Maydell return update_ite(s, eventid, &dte, &ite) ? CMD_CONTINUE : CMD_STALL; 432c694cb4cSShashi Mallela } 433c694cb4cSShashi Mallela 43406985cc3SPeter Maydell /* 43506985cc3SPeter Maydell * Update the Collection Table entry for @icid to @cte. Returns true 43606985cc3SPeter Maydell * on success, false if there was a memory access error. 43706985cc3SPeter Maydell */ 43806985cc3SPeter Maydell static bool update_cte(GICv3ITSState *s, uint16_t icid, const CTEntry *cte) 4397eca39e0SShashi Mallela { 4407eca39e0SShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 441d050f80fSPeter Maydell uint64_t entry_addr; 44206985cc3SPeter Maydell uint64_t cteval = 0; 4437eca39e0SShashi Mallela MemTxResult res = MEMTX_OK; 4447eca39e0SShashi Mallela 4457eca39e0SShashi Mallela if (!s->ct.valid) { 4467eca39e0SShashi Mallela return true; 4477eca39e0SShashi Mallela } 4487eca39e0SShashi Mallela 44906985cc3SPeter Maydell if (cte->valid) { 4507eca39e0SShashi Mallela /* add mapping entry to collection table */ 45106985cc3SPeter Maydell cteval = FIELD_DP64(cteval, CTE, VALID, 1); 45206985cc3SPeter Maydell cteval = FIELD_DP64(cteval, CTE, RDBASE, cte->rdbase); 4537eca39e0SShashi Mallela } 4547eca39e0SShashi Mallela 455d050f80fSPeter Maydell entry_addr = table_entry_addr(s, &s->ct, icid, &res); 4567eca39e0SShashi Mallela if (res != MEMTX_OK) { 457d050f80fSPeter Maydell /* memory access error: stall */ 4587eca39e0SShashi Mallela return false; 4597eca39e0SShashi Mallela } 460d050f80fSPeter Maydell if (entry_addr == -1) { 461d050f80fSPeter Maydell /* No L2 table for this index: discard write and continue */ 4627eca39e0SShashi Mallela return true; 4637eca39e0SShashi Mallela } 464d050f80fSPeter Maydell 46506985cc3SPeter Maydell address_space_stq_le(as, entry_addr, cteval, MEMTXATTRS_UNSPECIFIED, &res); 466d050f80fSPeter Maydell return res == MEMTX_OK; 4677eca39e0SShashi Mallela } 4687eca39e0SShashi Mallela 469b6f96009SPeter Maydell static ItsCmdResult process_mapc(GICv3ITSState *s, const uint64_t *cmdpkt) 4707eca39e0SShashi Mallela { 4717eca39e0SShashi Mallela uint16_t icid; 47206985cc3SPeter Maydell CTEntry cte; 4737eca39e0SShashi Mallela 474b6f96009SPeter Maydell icid = cmdpkt[2] & ICID_MASK; 4757eca39e0SShashi Mallela 47606985cc3SPeter Maydell cte.rdbase = (cmdpkt[2] & R_MAPC_RDBASE_MASK) >> R_MAPC_RDBASE_SHIFT; 47706985cc3SPeter Maydell cte.rdbase &= RDBASE_PROCNUM_MASK; 4787eca39e0SShashi Mallela 47906985cc3SPeter Maydell cte.valid = cmdpkt[2] & CMD_FIELD_VALID_MASK; 4807eca39e0SShashi Mallela 48106985cc3SPeter Maydell if ((icid >= s->ct.num_entries) || (cte.rdbase >= s->gicv3->num_cpu)) { 4827eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 4837eca39e0SShashi Mallela "ITS MAPC: invalid collection table attributes " 48406985cc3SPeter Maydell "icid %d rdbase %u\n", icid, cte.rdbase); 4857eca39e0SShashi Mallela /* 4867eca39e0SShashi Mallela * in this implementation, in case of error 4877eca39e0SShashi Mallela * we ignore this command and move onto the next 4887eca39e0SShashi Mallela * command in the queue 4897eca39e0SShashi Mallela */ 490f6675196SPeter Maydell return CMD_CONTINUE; 4917eca39e0SShashi Mallela } 4927eca39e0SShashi Mallela 49306985cc3SPeter Maydell return update_cte(s, icid, &cte) ? CMD_CONTINUE : CMD_STALL; 4947eca39e0SShashi Mallela } 4957eca39e0SShashi Mallela 49622d62b08SPeter Maydell /* 49722d62b08SPeter Maydell * Update the Device Table entry for @devid to @dte. Returns true 49822d62b08SPeter Maydell * on success, false if there was a memory access error. 49922d62b08SPeter Maydell */ 50022d62b08SPeter Maydell static bool update_dte(GICv3ITSState *s, uint32_t devid, const DTEntry *dte) 5017eca39e0SShashi Mallela { 5027eca39e0SShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 503d050f80fSPeter Maydell uint64_t entry_addr; 50422d62b08SPeter Maydell uint64_t dteval = 0; 5057eca39e0SShashi Mallela MemTxResult res = MEMTX_OK; 5067eca39e0SShashi Mallela 5077eca39e0SShashi Mallela if (s->dt.valid) { 50822d62b08SPeter Maydell if (dte->valid) { 5097eca39e0SShashi Mallela /* add mapping entry to device table */ 51022d62b08SPeter Maydell dteval = FIELD_DP64(dteval, DTE, VALID, 1); 51122d62b08SPeter Maydell dteval = FIELD_DP64(dteval, DTE, SIZE, dte->size); 51222d62b08SPeter Maydell dteval = FIELD_DP64(dteval, DTE, ITTADDR, dte->ittaddr); 5137eca39e0SShashi Mallela } 5147eca39e0SShashi Mallela } else { 5157eca39e0SShashi Mallela return true; 5167eca39e0SShashi Mallela } 5177eca39e0SShashi Mallela 518d050f80fSPeter Maydell entry_addr = table_entry_addr(s, &s->dt, devid, &res); 5197eca39e0SShashi Mallela if (res != MEMTX_OK) { 520d050f80fSPeter Maydell /* memory access error: stall */ 5217eca39e0SShashi Mallela return false; 5227eca39e0SShashi Mallela } 523d050f80fSPeter Maydell if (entry_addr == -1) { 524d050f80fSPeter Maydell /* No L2 table for this index: discard write and continue */ 5257eca39e0SShashi Mallela return true; 5267eca39e0SShashi Mallela } 52722d62b08SPeter Maydell address_space_stq_le(as, entry_addr, dteval, MEMTXATTRS_UNSPECIFIED, &res); 528d050f80fSPeter Maydell return res == MEMTX_OK; 5297eca39e0SShashi Mallela } 5307eca39e0SShashi Mallela 531b6f96009SPeter Maydell static ItsCmdResult process_mapd(GICv3ITSState *s, const uint64_t *cmdpkt) 5327eca39e0SShashi Mallela { 5337eca39e0SShashi Mallela uint32_t devid; 53422d62b08SPeter Maydell DTEntry dte; 5357eca39e0SShashi Mallela 536b6f96009SPeter Maydell devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT; 53722d62b08SPeter Maydell dte.size = cmdpkt[1] & SIZE_MASK; 53822d62b08SPeter Maydell dte.ittaddr = (cmdpkt[2] & ITTADDR_MASK) >> ITTADDR_SHIFT; 53922d62b08SPeter Maydell dte.valid = cmdpkt[2] & CMD_FIELD_VALID_MASK; 5407eca39e0SShashi Mallela 5418b8bb014SPeter Maydell if ((devid >= s->dt.num_entries) || 54222d62b08SPeter Maydell (dte.size > FIELD_EX64(s->typer, GITS_TYPER, IDBITS))) { 5437eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 5447eca39e0SShashi Mallela "ITS MAPD: invalid device table attributes " 54522d62b08SPeter Maydell "devid %d or size %d\n", devid, dte.size); 5467eca39e0SShashi Mallela /* 5477eca39e0SShashi Mallela * in this implementation, in case of error 5487eca39e0SShashi Mallela * we ignore this command and move onto the next 5497eca39e0SShashi Mallela * command in the queue 5507eca39e0SShashi Mallela */ 55100d46e72SPeter Maydell return CMD_CONTINUE; 5527eca39e0SShashi Mallela } 5537eca39e0SShashi Mallela 55422d62b08SPeter Maydell return update_dte(s, devid, &dte) ? CMD_CONTINUE : CMD_STALL; 5557eca39e0SShashi Mallela } 5567eca39e0SShashi Mallela 557b6f96009SPeter Maydell static ItsCmdResult process_movall(GICv3ITSState *s, const uint64_t *cmdpkt) 558f6d1d9b4SPeter Maydell { 559f6d1d9b4SPeter Maydell uint64_t rd1, rd2; 560f6d1d9b4SPeter Maydell 561b6f96009SPeter Maydell rd1 = FIELD_EX64(cmdpkt[2], MOVALL_2, RDBASE1); 562b6f96009SPeter Maydell rd2 = FIELD_EX64(cmdpkt[3], MOVALL_3, RDBASE2); 563f6d1d9b4SPeter Maydell 564f6d1d9b4SPeter Maydell if (rd1 >= s->gicv3->num_cpu) { 565f6d1d9b4SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 566f6d1d9b4SPeter Maydell "%s: RDBASE1 %" PRId64 567f6d1d9b4SPeter Maydell " out of range (must be less than %d)\n", 568f6d1d9b4SPeter Maydell __func__, rd1, s->gicv3->num_cpu); 569f6d1d9b4SPeter Maydell return CMD_CONTINUE; 570f6d1d9b4SPeter Maydell } 571f6d1d9b4SPeter Maydell if (rd2 >= s->gicv3->num_cpu) { 572f6d1d9b4SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 573f6d1d9b4SPeter Maydell "%s: RDBASE2 %" PRId64 574f6d1d9b4SPeter Maydell " out of range (must be less than %d)\n", 575f6d1d9b4SPeter Maydell __func__, rd2, s->gicv3->num_cpu); 576f6d1d9b4SPeter Maydell return CMD_CONTINUE; 577f6d1d9b4SPeter Maydell } 578f6d1d9b4SPeter Maydell 579f6d1d9b4SPeter Maydell if (rd1 == rd2) { 580f6d1d9b4SPeter Maydell /* Move to same target must succeed as a no-op */ 581f6d1d9b4SPeter Maydell return CMD_CONTINUE; 582f6d1d9b4SPeter Maydell } 583f6d1d9b4SPeter Maydell 584f6d1d9b4SPeter Maydell /* Move all pending LPIs from redistributor 1 to redistributor 2 */ 585f6d1d9b4SPeter Maydell gicv3_redist_movall_lpis(&s->gicv3->cpu[rd1], &s->gicv3->cpu[rd2]); 586f6d1d9b4SPeter Maydell 587f6d1d9b4SPeter Maydell return CMD_CONTINUE; 588f6d1d9b4SPeter Maydell } 589f6d1d9b4SPeter Maydell 590b6f96009SPeter Maydell static ItsCmdResult process_movi(GICv3ITSState *s, const uint64_t *cmdpkt) 591961b4912SPeter Maydell { 592244194feSPeter Maydell uint32_t devid, eventid; 593244194feSPeter Maydell uint16_t new_icid; 594961b4912SPeter Maydell uint64_t num_eventids; 5954acf93e1SPeter Maydell DTEntry dte; 596d37cf49bSPeter Maydell CTEntry old_cte, new_cte; 597244194feSPeter Maydell ITEntry old_ite; 598961b4912SPeter Maydell 599b6f96009SPeter Maydell devid = FIELD_EX64(cmdpkt[0], MOVI_0, DEVICEID); 600b6f96009SPeter Maydell eventid = FIELD_EX64(cmdpkt[1], MOVI_1, EVENTID); 601b6f96009SPeter Maydell new_icid = FIELD_EX64(cmdpkt[2], MOVI_2, ICID); 602961b4912SPeter Maydell 603961b4912SPeter Maydell if (devid >= s->dt.num_entries) { 604961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 605961b4912SPeter Maydell "%s: invalid command attributes: devid %d>=%d", 606961b4912SPeter Maydell __func__, devid, s->dt.num_entries); 607961b4912SPeter Maydell return CMD_CONTINUE; 608961b4912SPeter Maydell } 6094acf93e1SPeter Maydell if (get_dte(s, devid, &dte) != MEMTX_OK) { 610961b4912SPeter Maydell return CMD_STALL; 611961b4912SPeter Maydell } 612961b4912SPeter Maydell 6134acf93e1SPeter Maydell if (!dte.valid) { 614961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 615961b4912SPeter Maydell "%s: invalid command attributes: " 6164acf93e1SPeter Maydell "invalid dte for %d\n", __func__, devid); 617961b4912SPeter Maydell return CMD_CONTINUE; 618961b4912SPeter Maydell } 619961b4912SPeter Maydell 6204acf93e1SPeter Maydell num_eventids = 1ULL << (dte.size + 1); 621961b4912SPeter Maydell if (eventid >= num_eventids) { 622961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 623961b4912SPeter Maydell "%s: invalid command attributes: eventid %d >= %" 624961b4912SPeter Maydell PRId64 "\n", 625961b4912SPeter Maydell __func__, eventid, num_eventids); 626961b4912SPeter Maydell return CMD_CONTINUE; 627961b4912SPeter Maydell } 628961b4912SPeter Maydell 629244194feSPeter Maydell if (get_ite(s, eventid, &dte, &old_ite) != MEMTX_OK) { 630961b4912SPeter Maydell return CMD_STALL; 631961b4912SPeter Maydell } 632961b4912SPeter Maydell 633244194feSPeter Maydell if (!old_ite.valid || old_ite.inttype != ITE_INTTYPE_PHYSICAL) { 634961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 635961b4912SPeter Maydell "%s: invalid command attributes: invalid ITE\n", 636961b4912SPeter Maydell __func__); 637961b4912SPeter Maydell return CMD_CONTINUE; 638961b4912SPeter Maydell } 639961b4912SPeter Maydell 640244194feSPeter Maydell if (old_ite.icid >= s->ct.num_entries) { 641961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 642961b4912SPeter Maydell "%s: invalid ICID 0x%x in ITE (table corrupted?)\n", 643244194feSPeter Maydell __func__, old_ite.icid); 644961b4912SPeter Maydell return CMD_CONTINUE; 645961b4912SPeter Maydell } 646961b4912SPeter Maydell 647961b4912SPeter Maydell if (new_icid >= s->ct.num_entries) { 648961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 649961b4912SPeter Maydell "%s: invalid command attributes: ICID 0x%x\n", 650961b4912SPeter Maydell __func__, new_icid); 651961b4912SPeter Maydell return CMD_CONTINUE; 652961b4912SPeter Maydell } 653961b4912SPeter Maydell 654244194feSPeter Maydell if (get_cte(s, old_ite.icid, &old_cte) != MEMTX_OK) { 655961b4912SPeter Maydell return CMD_STALL; 656961b4912SPeter Maydell } 657d37cf49bSPeter Maydell if (!old_cte.valid) { 658961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 659961b4912SPeter Maydell "%s: invalid command attributes: " 660d37cf49bSPeter Maydell "invalid CTE for old ICID 0x%x\n", 661244194feSPeter Maydell __func__, old_ite.icid); 662961b4912SPeter Maydell return CMD_CONTINUE; 663961b4912SPeter Maydell } 664961b4912SPeter Maydell 665d37cf49bSPeter Maydell if (get_cte(s, new_icid, &new_cte) != MEMTX_OK) { 666961b4912SPeter Maydell return CMD_STALL; 667961b4912SPeter Maydell } 668d37cf49bSPeter Maydell if (!new_cte.valid) { 669961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 670961b4912SPeter Maydell "%s: invalid command attributes: " 671d37cf49bSPeter Maydell "invalid CTE for new ICID 0x%x\n", 672d37cf49bSPeter Maydell __func__, new_icid); 673961b4912SPeter Maydell return CMD_CONTINUE; 674961b4912SPeter Maydell } 675961b4912SPeter Maydell 676d37cf49bSPeter Maydell if (old_cte.rdbase >= s->gicv3->num_cpu) { 677961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 678d37cf49bSPeter Maydell "%s: CTE has invalid rdbase 0x%x\n", 679d37cf49bSPeter Maydell __func__, old_cte.rdbase); 680961b4912SPeter Maydell return CMD_CONTINUE; 681961b4912SPeter Maydell } 682961b4912SPeter Maydell 683d37cf49bSPeter Maydell if (new_cte.rdbase >= s->gicv3->num_cpu) { 684961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 685d37cf49bSPeter Maydell "%s: CTE has invalid rdbase 0x%x\n", 686d37cf49bSPeter Maydell __func__, new_cte.rdbase); 687961b4912SPeter Maydell return CMD_CONTINUE; 688961b4912SPeter Maydell } 689961b4912SPeter Maydell 690d37cf49bSPeter Maydell if (old_cte.rdbase != new_cte.rdbase) { 691961b4912SPeter Maydell /* Move the LPI from the old redistributor to the new one */ 692d37cf49bSPeter Maydell gicv3_redist_mov_lpi(&s->gicv3->cpu[old_cte.rdbase], 693d37cf49bSPeter Maydell &s->gicv3->cpu[new_cte.rdbase], 694244194feSPeter Maydell old_ite.intid); 695961b4912SPeter Maydell } 696961b4912SPeter Maydell 697961b4912SPeter Maydell /* Update the ICID field in the interrupt translation table entry */ 698*7eb54267SPeter Maydell old_ite.icid = new_icid; 699*7eb54267SPeter Maydell return update_ite(s, eventid, &dte, &old_ite) ? CMD_CONTINUE : CMD_STALL; 700961b4912SPeter Maydell } 701961b4912SPeter Maydell 7027eca39e0SShashi Mallela /* 7037eca39e0SShashi Mallela * Current implementation blocks until all 7047eca39e0SShashi Mallela * commands are processed 7057eca39e0SShashi Mallela */ 7067eca39e0SShashi Mallela static void process_cmdq(GICv3ITSState *s) 7077eca39e0SShashi Mallela { 7087eca39e0SShashi Mallela uint32_t wr_offset = 0; 7097eca39e0SShashi Mallela uint32_t rd_offset = 0; 7107eca39e0SShashi Mallela uint32_t cq_offset = 0; 7117eca39e0SShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 7127eca39e0SShashi Mallela uint8_t cmd; 71317fb5e36SShashi Mallela int i; 7147eca39e0SShashi Mallela 7158d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 7167eca39e0SShashi Mallela return; 7177eca39e0SShashi Mallela } 7187eca39e0SShashi Mallela 7197eca39e0SShashi Mallela wr_offset = FIELD_EX64(s->cwriter, GITS_CWRITER, OFFSET); 7207eca39e0SShashi Mallela 72180dcd37fSPeter Maydell if (wr_offset >= s->cq.num_entries) { 7227eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 7237eca39e0SShashi Mallela "%s: invalid write offset " 7247eca39e0SShashi Mallela "%d\n", __func__, wr_offset); 7257eca39e0SShashi Mallela return; 7267eca39e0SShashi Mallela } 7277eca39e0SShashi Mallela 7287eca39e0SShashi Mallela rd_offset = FIELD_EX64(s->creadr, GITS_CREADR, OFFSET); 7297eca39e0SShashi Mallela 73080dcd37fSPeter Maydell if (rd_offset >= s->cq.num_entries) { 7317eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 7327eca39e0SShashi Mallela "%s: invalid read offset " 7337eca39e0SShashi Mallela "%d\n", __func__, rd_offset); 7347eca39e0SShashi Mallela return; 7357eca39e0SShashi Mallela } 7367eca39e0SShashi Mallela 7377eca39e0SShashi Mallela while (wr_offset != rd_offset) { 738ef011555SPeter Maydell ItsCmdResult result = CMD_CONTINUE; 739b6f96009SPeter Maydell void *hostmem; 740b6f96009SPeter Maydell hwaddr buflen; 741b6f96009SPeter Maydell uint64_t cmdpkt[GITS_CMDQ_ENTRY_WORDS]; 742ef011555SPeter Maydell 7437eca39e0SShashi Mallela cq_offset = (rd_offset * GITS_CMDQ_ENTRY_SIZE); 744b6f96009SPeter Maydell 745b6f96009SPeter Maydell buflen = GITS_CMDQ_ENTRY_SIZE; 746b6f96009SPeter Maydell hostmem = address_space_map(as, s->cq.base_addr + cq_offset, 747b6f96009SPeter Maydell &buflen, false, MEMTXATTRS_UNSPECIFIED); 748b6f96009SPeter Maydell if (!hostmem || buflen != GITS_CMDQ_ENTRY_SIZE) { 749b6f96009SPeter Maydell if (hostmem) { 750b6f96009SPeter Maydell address_space_unmap(as, hostmem, buflen, false, 0); 751b6f96009SPeter Maydell } 752f0b4b2a2SPeter Maydell s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1); 753f0b4b2a2SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 754f0b4b2a2SPeter Maydell "%s: could not read command at 0x%" PRIx64 "\n", 755f0b4b2a2SPeter Maydell __func__, s->cq.base_addr + cq_offset); 756f0b4b2a2SPeter Maydell break; 7577eca39e0SShashi Mallela } 758b6f96009SPeter Maydell for (i = 0; i < ARRAY_SIZE(cmdpkt); i++) { 759b6f96009SPeter Maydell cmdpkt[i] = ldq_le_p(hostmem + i * sizeof(uint64_t)); 760b6f96009SPeter Maydell } 761b6f96009SPeter Maydell address_space_unmap(as, hostmem, buflen, false, 0); 762f0b4b2a2SPeter Maydell 763b6f96009SPeter Maydell cmd = cmdpkt[0] & CMD_MASK; 7647eca39e0SShashi Mallela 765195209d3SPeter Maydell trace_gicv3_its_process_command(rd_offset, cmd); 766195209d3SPeter Maydell 7677eca39e0SShashi Mallela switch (cmd) { 7687eca39e0SShashi Mallela case GITS_CMD_INT: 769b6f96009SPeter Maydell result = process_its_cmd(s, cmdpkt, INTERRUPT); 7707eca39e0SShashi Mallela break; 7717eca39e0SShashi Mallela case GITS_CMD_CLEAR: 772b6f96009SPeter Maydell result = process_its_cmd(s, cmdpkt, CLEAR); 7737eca39e0SShashi Mallela break; 7747eca39e0SShashi Mallela case GITS_CMD_SYNC: 7757eca39e0SShashi Mallela /* 7767eca39e0SShashi Mallela * Current implementation makes a blocking synchronous call 7777eca39e0SShashi Mallela * for every command issued earlier, hence the internal state 7787eca39e0SShashi Mallela * is already consistent by the time SYNC command is executed. 7797eca39e0SShashi Mallela * Hence no further processing is required for SYNC command. 7807eca39e0SShashi Mallela */ 7817eca39e0SShashi Mallela break; 7827eca39e0SShashi Mallela case GITS_CMD_MAPD: 783b6f96009SPeter Maydell result = process_mapd(s, cmdpkt); 7847eca39e0SShashi Mallela break; 7857eca39e0SShashi Mallela case GITS_CMD_MAPC: 786b6f96009SPeter Maydell result = process_mapc(s, cmdpkt); 7877eca39e0SShashi Mallela break; 7887eca39e0SShashi Mallela case GITS_CMD_MAPTI: 789b6f96009SPeter Maydell result = process_mapti(s, cmdpkt, false); 7907eca39e0SShashi Mallela break; 7917eca39e0SShashi Mallela case GITS_CMD_MAPI: 792b6f96009SPeter Maydell result = process_mapti(s, cmdpkt, true); 7937eca39e0SShashi Mallela break; 7947eca39e0SShashi Mallela case GITS_CMD_DISCARD: 795b6f96009SPeter Maydell result = process_its_cmd(s, cmdpkt, DISCARD); 7967eca39e0SShashi Mallela break; 7977eca39e0SShashi Mallela case GITS_CMD_INV: 7987eca39e0SShashi Mallela case GITS_CMD_INVALL: 79917fb5e36SShashi Mallela /* 80017fb5e36SShashi Mallela * Current implementation doesn't cache any ITS tables, 80117fb5e36SShashi Mallela * but the calculated lpi priority information. We only 80217fb5e36SShashi Mallela * need to trigger lpi priority re-calculation to be in 80317fb5e36SShashi Mallela * sync with LPI config table or pending table changes. 80417fb5e36SShashi Mallela */ 80517fb5e36SShashi Mallela for (i = 0; i < s->gicv3->num_cpu; i++) { 80617fb5e36SShashi Mallela gicv3_redist_update_lpi(&s->gicv3->cpu[i]); 80717fb5e36SShashi Mallela } 8087eca39e0SShashi Mallela break; 809961b4912SPeter Maydell case GITS_CMD_MOVI: 810b6f96009SPeter Maydell result = process_movi(s, cmdpkt); 811961b4912SPeter Maydell break; 812f6d1d9b4SPeter Maydell case GITS_CMD_MOVALL: 813b6f96009SPeter Maydell result = process_movall(s, cmdpkt); 814f6d1d9b4SPeter Maydell break; 8157eca39e0SShashi Mallela default: 8167eca39e0SShashi Mallela break; 8177eca39e0SShashi Mallela } 818ef011555SPeter Maydell if (result == CMD_CONTINUE) { 8197eca39e0SShashi Mallela rd_offset++; 82080dcd37fSPeter Maydell rd_offset %= s->cq.num_entries; 8217eca39e0SShashi Mallela s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, OFFSET, rd_offset); 8227eca39e0SShashi Mallela } else { 823ef011555SPeter Maydell /* CMD_STALL */ 8247eca39e0SShashi Mallela s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1); 8257eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 826ef011555SPeter Maydell "%s: 0x%x cmd processing failed, stalling\n", 827ef011555SPeter Maydell __func__, cmd); 8287eca39e0SShashi Mallela break; 8297eca39e0SShashi Mallela } 8307eca39e0SShashi Mallela } 8317eca39e0SShashi Mallela } 8327eca39e0SShashi Mallela 8331b08e436SShashi Mallela /* 8341b08e436SShashi Mallela * This function extracts the ITS Device and Collection table specific 8351b08e436SShashi Mallela * parameters (like base_addr, size etc) from GITS_BASER register. 8361b08e436SShashi Mallela * It is called during ITS enable and also during post_load migration 8371b08e436SShashi Mallela */ 8381b08e436SShashi Mallela static void extract_table_params(GICv3ITSState *s) 8391b08e436SShashi Mallela { 8401b08e436SShashi Mallela uint16_t num_pages = 0; 8411b08e436SShashi Mallela uint8_t page_sz_type; 8421b08e436SShashi Mallela uint8_t type; 8431b08e436SShashi Mallela uint32_t page_sz = 0; 8441b08e436SShashi Mallela uint64_t value; 8451b08e436SShashi Mallela 8461b08e436SShashi Mallela for (int i = 0; i < 8; i++) { 847e5487a41SPeter Maydell TableDesc *td; 848e5487a41SPeter Maydell int idbits; 849e5487a41SPeter Maydell 8501b08e436SShashi Mallela value = s->baser[i]; 8511b08e436SShashi Mallela 8521b08e436SShashi Mallela if (!value) { 8531b08e436SShashi Mallela continue; 8541b08e436SShashi Mallela } 8551b08e436SShashi Mallela 8561b08e436SShashi Mallela page_sz_type = FIELD_EX64(value, GITS_BASER, PAGESIZE); 8571b08e436SShashi Mallela 8581b08e436SShashi Mallela switch (page_sz_type) { 8591b08e436SShashi Mallela case 0: 8601b08e436SShashi Mallela page_sz = GITS_PAGE_SIZE_4K; 8611b08e436SShashi Mallela break; 8621b08e436SShashi Mallela 8631b08e436SShashi Mallela case 1: 8641b08e436SShashi Mallela page_sz = GITS_PAGE_SIZE_16K; 8651b08e436SShashi Mallela break; 8661b08e436SShashi Mallela 8671b08e436SShashi Mallela case 2: 8681b08e436SShashi Mallela case 3: 8691b08e436SShashi Mallela page_sz = GITS_PAGE_SIZE_64K; 8701b08e436SShashi Mallela break; 8711b08e436SShashi Mallela 8721b08e436SShashi Mallela default: 8731b08e436SShashi Mallela g_assert_not_reached(); 8741b08e436SShashi Mallela } 8751b08e436SShashi Mallela 8761b08e436SShashi Mallela num_pages = FIELD_EX64(value, GITS_BASER, SIZE) + 1; 8771b08e436SShashi Mallela 8781b08e436SShashi Mallela type = FIELD_EX64(value, GITS_BASER, TYPE); 8791b08e436SShashi Mallela 8801b08e436SShashi Mallela switch (type) { 8811b08e436SShashi Mallela case GITS_BASER_TYPE_DEVICE: 882e5487a41SPeter Maydell td = &s->dt; 883e5487a41SPeter Maydell idbits = FIELD_EX64(s->typer, GITS_TYPER, DEVBITS) + 1; 88462df780eSPeter Maydell break; 8851b08e436SShashi Mallela case GITS_BASER_TYPE_COLLECTION: 886e5487a41SPeter Maydell td = &s->ct; 8871b08e436SShashi Mallela if (FIELD_EX64(s->typer, GITS_TYPER, CIL)) { 888e5487a41SPeter Maydell idbits = FIELD_EX64(s->typer, GITS_TYPER, CIDBITS) + 1; 8891b08e436SShashi Mallela } else { 8901b08e436SShashi Mallela /* 16-bit CollectionId supported when CIL == 0 */ 891e5487a41SPeter Maydell idbits = 16; 8921b08e436SShashi Mallela } 8931b08e436SShashi Mallela break; 8941b08e436SShashi Mallela default: 895e5487a41SPeter Maydell /* 896e5487a41SPeter Maydell * GITS_BASER<n>.TYPE is read-only, so GITS_BASER_RO_MASK 897e5487a41SPeter Maydell * ensures we will only see type values corresponding to 898e5487a41SPeter Maydell * the values set up in gicv3_its_reset(). 899e5487a41SPeter Maydell */ 900e5487a41SPeter Maydell g_assert_not_reached(); 9011b08e436SShashi Mallela } 902e5487a41SPeter Maydell 903e5487a41SPeter Maydell memset(td, 0, sizeof(*td)); 904e5487a41SPeter Maydell td->valid = FIELD_EX64(value, GITS_BASER, VALID); 905e5487a41SPeter Maydell /* 906e5487a41SPeter Maydell * If GITS_BASER<n>.Valid is 0 for any <n> then we will not process 907e5487a41SPeter Maydell * interrupts. (GITS_TYPER.HCC is 0 for this implementation, so we 908e5487a41SPeter Maydell * do not have a special case where the GITS_BASER<n>.Valid bit is 0 909e5487a41SPeter Maydell * for the register corresponding to the Collection table but we 910e5487a41SPeter Maydell * still have to process interrupts using non-memory-backed 911e5487a41SPeter Maydell * Collection table entries.) 912e5487a41SPeter Maydell */ 913e5487a41SPeter Maydell if (!td->valid) { 914e5487a41SPeter Maydell continue; 915e5487a41SPeter Maydell } 916e5487a41SPeter Maydell td->page_sz = page_sz; 917e5487a41SPeter Maydell td->indirect = FIELD_EX64(value, GITS_BASER, INDIRECT); 9189ae85431SPeter Maydell td->entry_sz = FIELD_EX64(value, GITS_BASER, ENTRYSIZE) + 1; 919e5487a41SPeter Maydell td->base_addr = baser_base_addr(value, page_sz); 920e5487a41SPeter Maydell if (!td->indirect) { 92180dcd37fSPeter Maydell td->num_entries = (num_pages * page_sz) / td->entry_sz; 922e5487a41SPeter Maydell } else { 92380dcd37fSPeter Maydell td->num_entries = (((num_pages * page_sz) / 924e5487a41SPeter Maydell L1TABLE_ENTRY_SIZE) * 925e5487a41SPeter Maydell (page_sz / td->entry_sz)); 926e5487a41SPeter Maydell } 9278b8bb014SPeter Maydell td->num_entries = MIN(td->num_entries, 1ULL << idbits); 9281b08e436SShashi Mallela } 9291b08e436SShashi Mallela } 9301b08e436SShashi Mallela 9311b08e436SShashi Mallela static void extract_cmdq_params(GICv3ITSState *s) 9321b08e436SShashi Mallela { 9331b08e436SShashi Mallela uint16_t num_pages = 0; 9341b08e436SShashi Mallela uint64_t value = s->cbaser; 9351b08e436SShashi Mallela 9361b08e436SShashi Mallela num_pages = FIELD_EX64(value, GITS_CBASER, SIZE) + 1; 9371b08e436SShashi Mallela 9381b08e436SShashi Mallela memset(&s->cq, 0 , sizeof(s->cq)); 9391b08e436SShashi Mallela s->cq.valid = FIELD_EX64(value, GITS_CBASER, VALID); 9401b08e436SShashi Mallela 9411b08e436SShashi Mallela if (s->cq.valid) { 94280dcd37fSPeter Maydell s->cq.num_entries = (num_pages * GITS_PAGE_SIZE_4K) / 9431b08e436SShashi Mallela GITS_CMDQ_ENTRY_SIZE; 9441b08e436SShashi Mallela s->cq.base_addr = FIELD_EX64(value, GITS_CBASER, PHYADDR); 9451b08e436SShashi Mallela s->cq.base_addr <<= R_GITS_CBASER_PHYADDR_SHIFT; 9461b08e436SShashi Mallela } 9471b08e436SShashi Mallela } 9481b08e436SShashi Mallela 9497e062b98SPeter Maydell static MemTxResult gicv3_its_translation_read(void *opaque, hwaddr offset, 9507e062b98SPeter Maydell uint64_t *data, unsigned size, 9517e062b98SPeter Maydell MemTxAttrs attrs) 9527e062b98SPeter Maydell { 9537e062b98SPeter Maydell /* 9547e062b98SPeter Maydell * GITS_TRANSLATER is write-only, and all other addresses 9557e062b98SPeter Maydell * in the interrupt translation space frame are RES0. 9567e062b98SPeter Maydell */ 9577e062b98SPeter Maydell *data = 0; 9587e062b98SPeter Maydell return MEMTX_OK; 9597e062b98SPeter Maydell } 9607e062b98SPeter Maydell 96118f6290aSShashi Mallela static MemTxResult gicv3_its_translation_write(void *opaque, hwaddr offset, 96218f6290aSShashi Mallela uint64_t data, unsigned size, 96318f6290aSShashi Mallela MemTxAttrs attrs) 96418f6290aSShashi Mallela { 965c694cb4cSShashi Mallela GICv3ITSState *s = (GICv3ITSState *)opaque; 966c694cb4cSShashi Mallela bool result = true; 967c694cb4cSShashi Mallela 968195209d3SPeter Maydell trace_gicv3_its_translation_write(offset, data, size, attrs.requester_id); 969195209d3SPeter Maydell 970c694cb4cSShashi Mallela switch (offset) { 971c694cb4cSShashi Mallela case GITS_TRANSLATER: 9728d2d6dd9SPeter Maydell if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) { 973b6f96009SPeter Maydell result = do_process_its_cmd(s, attrs.requester_id, data, NONE); 974c694cb4cSShashi Mallela } 975c694cb4cSShashi Mallela break; 976c694cb4cSShashi Mallela default: 977c694cb4cSShashi Mallela break; 978c694cb4cSShashi Mallela } 979c694cb4cSShashi Mallela 980c694cb4cSShashi Mallela if (result) { 98118f6290aSShashi Mallela return MEMTX_OK; 982c694cb4cSShashi Mallela } else { 983c694cb4cSShashi Mallela return MEMTX_ERROR; 984c694cb4cSShashi Mallela } 98518f6290aSShashi Mallela } 98618f6290aSShashi Mallela 98718f6290aSShashi Mallela static bool its_writel(GICv3ITSState *s, hwaddr offset, 98818f6290aSShashi Mallela uint64_t value, MemTxAttrs attrs) 98918f6290aSShashi Mallela { 99018f6290aSShashi Mallela bool result = true; 9911b08e436SShashi Mallela int index; 99218f6290aSShashi Mallela 9931b08e436SShashi Mallela switch (offset) { 9941b08e436SShashi Mallela case GITS_CTLR: 9952f459cd1SShashi Mallela if (value & R_GITS_CTLR_ENABLED_MASK) { 9968d2d6dd9SPeter Maydell s->ctlr |= R_GITS_CTLR_ENABLED_MASK; 9971b08e436SShashi Mallela extract_table_params(s); 9981b08e436SShashi Mallela extract_cmdq_params(s); 9997eca39e0SShashi Mallela process_cmdq(s); 10002f459cd1SShashi Mallela } else { 10018d2d6dd9SPeter Maydell s->ctlr &= ~R_GITS_CTLR_ENABLED_MASK; 10021b08e436SShashi Mallela } 10031b08e436SShashi Mallela break; 10041b08e436SShashi Mallela case GITS_CBASER: 10051b08e436SShashi Mallela /* 10061b08e436SShashi Mallela * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is 10071b08e436SShashi Mallela * already enabled 10081b08e436SShashi Mallela */ 10098d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 10101b08e436SShashi Mallela s->cbaser = deposit64(s->cbaser, 0, 32, value); 10111b08e436SShashi Mallela s->creadr = 0; 10121b08e436SShashi Mallela } 10131b08e436SShashi Mallela break; 10141b08e436SShashi Mallela case GITS_CBASER + 4: 10151b08e436SShashi Mallela /* 10161b08e436SShashi Mallela * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is 10171b08e436SShashi Mallela * already enabled 10181b08e436SShashi Mallela */ 10198d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 10201b08e436SShashi Mallela s->cbaser = deposit64(s->cbaser, 32, 32, value); 10211b08e436SShashi Mallela s->creadr = 0; 10221b08e436SShashi Mallela } 10231b08e436SShashi Mallela break; 10241b08e436SShashi Mallela case GITS_CWRITER: 10251b08e436SShashi Mallela s->cwriter = deposit64(s->cwriter, 0, 32, 10261b08e436SShashi Mallela (value & ~R_GITS_CWRITER_RETRY_MASK)); 10277eca39e0SShashi Mallela if (s->cwriter != s->creadr) { 10287eca39e0SShashi Mallela process_cmdq(s); 10297eca39e0SShashi Mallela } 10301b08e436SShashi Mallela break; 10311b08e436SShashi Mallela case GITS_CWRITER + 4: 10321b08e436SShashi Mallela s->cwriter = deposit64(s->cwriter, 32, 32, value); 10331b08e436SShashi Mallela break; 10341b08e436SShashi Mallela case GITS_CREADR: 10351b08e436SShashi Mallela if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) { 10361b08e436SShashi Mallela s->creadr = deposit64(s->creadr, 0, 32, 10371b08e436SShashi Mallela (value & ~R_GITS_CREADR_STALLED_MASK)); 10381b08e436SShashi Mallela } else { 10391b08e436SShashi Mallela /* RO register, ignore the write */ 10401b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 10411b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 10421b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 10431b08e436SShashi Mallela } 10441b08e436SShashi Mallela break; 10451b08e436SShashi Mallela case GITS_CREADR + 4: 10461b08e436SShashi Mallela if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) { 10471b08e436SShashi Mallela s->creadr = deposit64(s->creadr, 32, 32, value); 10481b08e436SShashi Mallela } else { 10491b08e436SShashi Mallela /* RO register, ignore the write */ 10501b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 10511b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 10521b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 10531b08e436SShashi Mallela } 10541b08e436SShashi Mallela break; 10551b08e436SShashi Mallela case GITS_BASER ... GITS_BASER + 0x3f: 10561b08e436SShashi Mallela /* 10571b08e436SShashi Mallela * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is 10581b08e436SShashi Mallela * already enabled 10591b08e436SShashi Mallela */ 10608d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 10611b08e436SShashi Mallela index = (offset - GITS_BASER) / 8; 10621b08e436SShashi Mallela 10630ffe88e6SPeter Maydell if (s->baser[index] == 0) { 10640ffe88e6SPeter Maydell /* Unimplemented GITS_BASERn: RAZ/WI */ 10650ffe88e6SPeter Maydell break; 10660ffe88e6SPeter Maydell } 10671b08e436SShashi Mallela if (offset & 7) { 10681b08e436SShashi Mallela value <<= 32; 10691b08e436SShashi Mallela value &= ~GITS_BASER_RO_MASK; 10701b08e436SShashi Mallela s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(0, 32); 10711b08e436SShashi Mallela s->baser[index] |= value; 10721b08e436SShashi Mallela } else { 10731b08e436SShashi Mallela value &= ~GITS_BASER_RO_MASK; 10741b08e436SShashi Mallela s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(32, 32); 10751b08e436SShashi Mallela s->baser[index] |= value; 10761b08e436SShashi Mallela } 10771b08e436SShashi Mallela } 10781b08e436SShashi Mallela break; 10791b08e436SShashi Mallela case GITS_IIDR: 10801b08e436SShashi Mallela case GITS_IDREGS ... GITS_IDREGS + 0x2f: 10811b08e436SShashi Mallela /* RO registers, ignore the write */ 10821b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 10831b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 10841b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 10851b08e436SShashi Mallela break; 10861b08e436SShashi Mallela default: 10871b08e436SShashi Mallela result = false; 10881b08e436SShashi Mallela break; 10891b08e436SShashi Mallela } 109018f6290aSShashi Mallela return result; 109118f6290aSShashi Mallela } 109218f6290aSShashi Mallela 109318f6290aSShashi Mallela static bool its_readl(GICv3ITSState *s, hwaddr offset, 109418f6290aSShashi Mallela uint64_t *data, MemTxAttrs attrs) 109518f6290aSShashi Mallela { 109618f6290aSShashi Mallela bool result = true; 10971b08e436SShashi Mallela int index; 109818f6290aSShashi Mallela 10991b08e436SShashi Mallela switch (offset) { 11001b08e436SShashi Mallela case GITS_CTLR: 11011b08e436SShashi Mallela *data = s->ctlr; 11021b08e436SShashi Mallela break; 11031b08e436SShashi Mallela case GITS_IIDR: 11041b08e436SShashi Mallela *data = gicv3_iidr(); 11051b08e436SShashi Mallela break; 11061b08e436SShashi Mallela case GITS_IDREGS ... GITS_IDREGS + 0x2f: 11071b08e436SShashi Mallela /* ID registers */ 11081b08e436SShashi Mallela *data = gicv3_idreg(offset - GITS_IDREGS); 11091b08e436SShashi Mallela break; 11101b08e436SShashi Mallela case GITS_TYPER: 11111b08e436SShashi Mallela *data = extract64(s->typer, 0, 32); 11121b08e436SShashi Mallela break; 11131b08e436SShashi Mallela case GITS_TYPER + 4: 11141b08e436SShashi Mallela *data = extract64(s->typer, 32, 32); 11151b08e436SShashi Mallela break; 11161b08e436SShashi Mallela case GITS_CBASER: 11171b08e436SShashi Mallela *data = extract64(s->cbaser, 0, 32); 11181b08e436SShashi Mallela break; 11191b08e436SShashi Mallela case GITS_CBASER + 4: 11201b08e436SShashi Mallela *data = extract64(s->cbaser, 32, 32); 11211b08e436SShashi Mallela break; 11221b08e436SShashi Mallela case GITS_CREADR: 11231b08e436SShashi Mallela *data = extract64(s->creadr, 0, 32); 11241b08e436SShashi Mallela break; 11251b08e436SShashi Mallela case GITS_CREADR + 4: 11261b08e436SShashi Mallela *data = extract64(s->creadr, 32, 32); 11271b08e436SShashi Mallela break; 11281b08e436SShashi Mallela case GITS_CWRITER: 11291b08e436SShashi Mallela *data = extract64(s->cwriter, 0, 32); 11301b08e436SShashi Mallela break; 11311b08e436SShashi Mallela case GITS_CWRITER + 4: 11321b08e436SShashi Mallela *data = extract64(s->cwriter, 32, 32); 11331b08e436SShashi Mallela break; 11341b08e436SShashi Mallela case GITS_BASER ... GITS_BASER + 0x3f: 11351b08e436SShashi Mallela index = (offset - GITS_BASER) / 8; 11361b08e436SShashi Mallela if (offset & 7) { 11371b08e436SShashi Mallela *data = extract64(s->baser[index], 32, 32); 11381b08e436SShashi Mallela } else { 11391b08e436SShashi Mallela *data = extract64(s->baser[index], 0, 32); 11401b08e436SShashi Mallela } 11411b08e436SShashi Mallela break; 11421b08e436SShashi Mallela default: 11431b08e436SShashi Mallela result = false; 11441b08e436SShashi Mallela break; 11451b08e436SShashi Mallela } 114618f6290aSShashi Mallela return result; 114718f6290aSShashi Mallela } 114818f6290aSShashi Mallela 114918f6290aSShashi Mallela static bool its_writell(GICv3ITSState *s, hwaddr offset, 115018f6290aSShashi Mallela uint64_t value, MemTxAttrs attrs) 115118f6290aSShashi Mallela { 115218f6290aSShashi Mallela bool result = true; 11531b08e436SShashi Mallela int index; 115418f6290aSShashi Mallela 11551b08e436SShashi Mallela switch (offset) { 11561b08e436SShashi Mallela case GITS_BASER ... GITS_BASER + 0x3f: 11571b08e436SShashi Mallela /* 11581b08e436SShashi Mallela * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is 11591b08e436SShashi Mallela * already enabled 11601b08e436SShashi Mallela */ 11618d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 11621b08e436SShashi Mallela index = (offset - GITS_BASER) / 8; 11630ffe88e6SPeter Maydell if (s->baser[index] == 0) { 11640ffe88e6SPeter Maydell /* Unimplemented GITS_BASERn: RAZ/WI */ 11650ffe88e6SPeter Maydell break; 11660ffe88e6SPeter Maydell } 11671b08e436SShashi Mallela s->baser[index] &= GITS_BASER_RO_MASK; 11681b08e436SShashi Mallela s->baser[index] |= (value & ~GITS_BASER_RO_MASK); 11691b08e436SShashi Mallela } 11701b08e436SShashi Mallela break; 11711b08e436SShashi Mallela case GITS_CBASER: 11721b08e436SShashi Mallela /* 11731b08e436SShashi Mallela * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is 11741b08e436SShashi Mallela * already enabled 11751b08e436SShashi Mallela */ 11768d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 11771b08e436SShashi Mallela s->cbaser = value; 11781b08e436SShashi Mallela s->creadr = 0; 11791b08e436SShashi Mallela } 11801b08e436SShashi Mallela break; 11811b08e436SShashi Mallela case GITS_CWRITER: 11821b08e436SShashi Mallela s->cwriter = value & ~R_GITS_CWRITER_RETRY_MASK; 11837eca39e0SShashi Mallela if (s->cwriter != s->creadr) { 11847eca39e0SShashi Mallela process_cmdq(s); 11857eca39e0SShashi Mallela } 11861b08e436SShashi Mallela break; 11871b08e436SShashi Mallela case GITS_CREADR: 11881b08e436SShashi Mallela if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) { 11891b08e436SShashi Mallela s->creadr = value & ~R_GITS_CREADR_STALLED_MASK; 11901b08e436SShashi Mallela } else { 11911b08e436SShashi Mallela /* RO register, ignore the write */ 11921b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 11931b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 11941b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 11951b08e436SShashi Mallela } 11961b08e436SShashi Mallela break; 11971b08e436SShashi Mallela case GITS_TYPER: 11981b08e436SShashi Mallela /* RO registers, ignore the write */ 11991b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 12001b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 12011b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 12021b08e436SShashi Mallela break; 12031b08e436SShashi Mallela default: 12041b08e436SShashi Mallela result = false; 12051b08e436SShashi Mallela break; 12061b08e436SShashi Mallela } 120718f6290aSShashi Mallela return result; 120818f6290aSShashi Mallela } 120918f6290aSShashi Mallela 121018f6290aSShashi Mallela static bool its_readll(GICv3ITSState *s, hwaddr offset, 121118f6290aSShashi Mallela uint64_t *data, MemTxAttrs attrs) 121218f6290aSShashi Mallela { 121318f6290aSShashi Mallela bool result = true; 12141b08e436SShashi Mallela int index; 121518f6290aSShashi Mallela 12161b08e436SShashi Mallela switch (offset) { 12171b08e436SShashi Mallela case GITS_TYPER: 12181b08e436SShashi Mallela *data = s->typer; 12191b08e436SShashi Mallela break; 12201b08e436SShashi Mallela case GITS_BASER ... GITS_BASER + 0x3f: 12211b08e436SShashi Mallela index = (offset - GITS_BASER) / 8; 12221b08e436SShashi Mallela *data = s->baser[index]; 12231b08e436SShashi Mallela break; 12241b08e436SShashi Mallela case GITS_CBASER: 12251b08e436SShashi Mallela *data = s->cbaser; 12261b08e436SShashi Mallela break; 12271b08e436SShashi Mallela case GITS_CREADR: 12281b08e436SShashi Mallela *data = s->creadr; 12291b08e436SShashi Mallela break; 12301b08e436SShashi Mallela case GITS_CWRITER: 12311b08e436SShashi Mallela *data = s->cwriter; 12321b08e436SShashi Mallela break; 12331b08e436SShashi Mallela default: 12341b08e436SShashi Mallela result = false; 12351b08e436SShashi Mallela break; 12361b08e436SShashi Mallela } 123718f6290aSShashi Mallela return result; 123818f6290aSShashi Mallela } 123918f6290aSShashi Mallela 124018f6290aSShashi Mallela static MemTxResult gicv3_its_read(void *opaque, hwaddr offset, uint64_t *data, 124118f6290aSShashi Mallela unsigned size, MemTxAttrs attrs) 124218f6290aSShashi Mallela { 124318f6290aSShashi Mallela GICv3ITSState *s = (GICv3ITSState *)opaque; 124418f6290aSShashi Mallela bool result; 124518f6290aSShashi Mallela 124618f6290aSShashi Mallela switch (size) { 124718f6290aSShashi Mallela case 4: 124818f6290aSShashi Mallela result = its_readl(s, offset, data, attrs); 124918f6290aSShashi Mallela break; 125018f6290aSShashi Mallela case 8: 125118f6290aSShashi Mallela result = its_readll(s, offset, data, attrs); 125218f6290aSShashi Mallela break; 125318f6290aSShashi Mallela default: 125418f6290aSShashi Mallela result = false; 125518f6290aSShashi Mallela break; 125618f6290aSShashi Mallela } 125718f6290aSShashi Mallela 125818f6290aSShashi Mallela if (!result) { 125918f6290aSShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 126018f6290aSShashi Mallela "%s: invalid guest read at offset " TARGET_FMT_plx 126118f6290aSShashi Mallela "size %u\n", __func__, offset, size); 1262195209d3SPeter Maydell trace_gicv3_its_badread(offset, size); 126318f6290aSShashi Mallela /* 126418f6290aSShashi Mallela * The spec requires that reserved registers are RAZ/WI; 126518f6290aSShashi Mallela * so use false returns from leaf functions as a way to 126618f6290aSShashi Mallela * trigger the guest-error logging but don't return it to 126718f6290aSShashi Mallela * the caller, or we'll cause a spurious guest data abort. 126818f6290aSShashi Mallela */ 126918f6290aSShashi Mallela *data = 0; 1270195209d3SPeter Maydell } else { 1271195209d3SPeter Maydell trace_gicv3_its_read(offset, *data, size); 127218f6290aSShashi Mallela } 127318f6290aSShashi Mallela return MEMTX_OK; 127418f6290aSShashi Mallela } 127518f6290aSShashi Mallela 127618f6290aSShashi Mallela static MemTxResult gicv3_its_write(void *opaque, hwaddr offset, uint64_t data, 127718f6290aSShashi Mallela unsigned size, MemTxAttrs attrs) 127818f6290aSShashi Mallela { 127918f6290aSShashi Mallela GICv3ITSState *s = (GICv3ITSState *)opaque; 128018f6290aSShashi Mallela bool result; 128118f6290aSShashi Mallela 128218f6290aSShashi Mallela switch (size) { 128318f6290aSShashi Mallela case 4: 128418f6290aSShashi Mallela result = its_writel(s, offset, data, attrs); 128518f6290aSShashi Mallela break; 128618f6290aSShashi Mallela case 8: 128718f6290aSShashi Mallela result = its_writell(s, offset, data, attrs); 128818f6290aSShashi Mallela break; 128918f6290aSShashi Mallela default: 129018f6290aSShashi Mallela result = false; 129118f6290aSShashi Mallela break; 129218f6290aSShashi Mallela } 129318f6290aSShashi Mallela 129418f6290aSShashi Mallela if (!result) { 129518f6290aSShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 129618f6290aSShashi Mallela "%s: invalid guest write at offset " TARGET_FMT_plx 129718f6290aSShashi Mallela "size %u\n", __func__, offset, size); 1298195209d3SPeter Maydell trace_gicv3_its_badwrite(offset, data, size); 129918f6290aSShashi Mallela /* 130018f6290aSShashi Mallela * The spec requires that reserved registers are RAZ/WI; 130118f6290aSShashi Mallela * so use false returns from leaf functions as a way to 130218f6290aSShashi Mallela * trigger the guest-error logging but don't return it to 130318f6290aSShashi Mallela * the caller, or we'll cause a spurious guest data abort. 130418f6290aSShashi Mallela */ 1305195209d3SPeter Maydell } else { 1306195209d3SPeter Maydell trace_gicv3_its_write(offset, data, size); 130718f6290aSShashi Mallela } 130818f6290aSShashi Mallela return MEMTX_OK; 130918f6290aSShashi Mallela } 131018f6290aSShashi Mallela 131118f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_control_ops = { 131218f6290aSShashi Mallela .read_with_attrs = gicv3_its_read, 131318f6290aSShashi Mallela .write_with_attrs = gicv3_its_write, 131418f6290aSShashi Mallela .valid.min_access_size = 4, 131518f6290aSShashi Mallela .valid.max_access_size = 8, 131618f6290aSShashi Mallela .impl.min_access_size = 4, 131718f6290aSShashi Mallela .impl.max_access_size = 8, 131818f6290aSShashi Mallela .endianness = DEVICE_NATIVE_ENDIAN, 131918f6290aSShashi Mallela }; 132018f6290aSShashi Mallela 132118f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_translation_ops = { 13227e062b98SPeter Maydell .read_with_attrs = gicv3_its_translation_read, 132318f6290aSShashi Mallela .write_with_attrs = gicv3_its_translation_write, 132418f6290aSShashi Mallela .valid.min_access_size = 2, 132518f6290aSShashi Mallela .valid.max_access_size = 4, 132618f6290aSShashi Mallela .impl.min_access_size = 2, 132718f6290aSShashi Mallela .impl.max_access_size = 4, 132818f6290aSShashi Mallela .endianness = DEVICE_NATIVE_ENDIAN, 132918f6290aSShashi Mallela }; 133018f6290aSShashi Mallela 133118f6290aSShashi Mallela static void gicv3_arm_its_realize(DeviceState *dev, Error **errp) 133218f6290aSShashi Mallela { 133318f6290aSShashi Mallela GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev); 133418f6290aSShashi Mallela int i; 133518f6290aSShashi Mallela 133618f6290aSShashi Mallela for (i = 0; i < s->gicv3->num_cpu; i++) { 133718f6290aSShashi Mallela if (!(s->gicv3->cpu[i].gicr_typer & GICR_TYPER_PLPIS)) { 133818f6290aSShashi Mallela error_setg(errp, "Physical LPI not supported by CPU %d", i); 133918f6290aSShashi Mallela return; 134018f6290aSShashi Mallela } 134118f6290aSShashi Mallela } 134218f6290aSShashi Mallela 134318f6290aSShashi Mallela gicv3_its_init_mmio(s, &gicv3_its_control_ops, &gicv3_its_translation_ops); 134418f6290aSShashi Mallela 134518f6290aSShashi Mallela /* set the ITS default features supported */ 1346764d6ba1SPeter Maydell s->typer = FIELD_DP64(s->typer, GITS_TYPER, PHYSICAL, 1); 134718f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, ITT_ENTRY_SIZE, 134818f6290aSShashi Mallela ITS_ITT_ENTRY_SIZE - 1); 134918f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, IDBITS, ITS_IDBITS); 135018f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, DEVBITS, ITS_DEVBITS); 135118f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIL, 1); 135218f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIDBITS, ITS_CIDBITS); 135318f6290aSShashi Mallela } 135418f6290aSShashi Mallela 135518f6290aSShashi Mallela static void gicv3_its_reset(DeviceState *dev) 135618f6290aSShashi Mallela { 135718f6290aSShashi Mallela GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev); 135818f6290aSShashi Mallela GICv3ITSClass *c = ARM_GICV3_ITS_GET_CLASS(s); 135918f6290aSShashi Mallela 136018f6290aSShashi Mallela c->parent_reset(dev); 136118f6290aSShashi Mallela 136218f6290aSShashi Mallela /* Quiescent bit reset to 1 */ 136318f6290aSShashi Mallela s->ctlr = FIELD_DP32(s->ctlr, GITS_CTLR, QUIESCENT, 1); 136418f6290aSShashi Mallela 136518f6290aSShashi Mallela /* 136618f6290aSShashi Mallela * setting GITS_BASER0.Type = 0b001 (Device) 136718f6290aSShashi Mallela * GITS_BASER1.Type = 0b100 (Collection Table) 136818f6290aSShashi Mallela * GITS_BASER<n>.Type,where n = 3 to 7 are 0b00 (Unimplemented) 136918f6290aSShashi Mallela * GITS_BASER<0,1>.Page_Size = 64KB 137018f6290aSShashi Mallela * and default translation table entry size to 16 bytes 137118f6290aSShashi Mallela */ 137218f6290aSShashi Mallela s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, TYPE, 137318f6290aSShashi Mallela GITS_BASER_TYPE_DEVICE); 137418f6290aSShashi Mallela s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, PAGESIZE, 137518f6290aSShashi Mallela GITS_BASER_PAGESIZE_64K); 137618f6290aSShashi Mallela s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, ENTRYSIZE, 137718f6290aSShashi Mallela GITS_DTE_SIZE - 1); 137818f6290aSShashi Mallela 137918f6290aSShashi Mallela s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, TYPE, 138018f6290aSShashi Mallela GITS_BASER_TYPE_COLLECTION); 138118f6290aSShashi Mallela s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, PAGESIZE, 138218f6290aSShashi Mallela GITS_BASER_PAGESIZE_64K); 138318f6290aSShashi Mallela s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, ENTRYSIZE, 138418f6290aSShashi Mallela GITS_CTE_SIZE - 1); 138518f6290aSShashi Mallela } 138618f6290aSShashi Mallela 13871b08e436SShashi Mallela static void gicv3_its_post_load(GICv3ITSState *s) 13881b08e436SShashi Mallela { 13898d2d6dd9SPeter Maydell if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) { 13901b08e436SShashi Mallela extract_table_params(s); 13911b08e436SShashi Mallela extract_cmdq_params(s); 13921b08e436SShashi Mallela } 13931b08e436SShashi Mallela } 13941b08e436SShashi Mallela 139518f6290aSShashi Mallela static Property gicv3_its_props[] = { 139618f6290aSShashi Mallela DEFINE_PROP_LINK("parent-gicv3", GICv3ITSState, gicv3, "arm-gicv3", 139718f6290aSShashi Mallela GICv3State *), 139818f6290aSShashi Mallela DEFINE_PROP_END_OF_LIST(), 139918f6290aSShashi Mallela }; 140018f6290aSShashi Mallela 140118f6290aSShashi Mallela static void gicv3_its_class_init(ObjectClass *klass, void *data) 140218f6290aSShashi Mallela { 140318f6290aSShashi Mallela DeviceClass *dc = DEVICE_CLASS(klass); 140418f6290aSShashi Mallela GICv3ITSClass *ic = ARM_GICV3_ITS_CLASS(klass); 14051b08e436SShashi Mallela GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass); 140618f6290aSShashi Mallela 140718f6290aSShashi Mallela dc->realize = gicv3_arm_its_realize; 140818f6290aSShashi Mallela device_class_set_props(dc, gicv3_its_props); 140918f6290aSShashi Mallela device_class_set_parent_reset(dc, gicv3_its_reset, &ic->parent_reset); 14101b08e436SShashi Mallela icc->post_load = gicv3_its_post_load; 141118f6290aSShashi Mallela } 141218f6290aSShashi Mallela 141318f6290aSShashi Mallela static const TypeInfo gicv3_its_info = { 141418f6290aSShashi Mallela .name = TYPE_ARM_GICV3_ITS, 141518f6290aSShashi Mallela .parent = TYPE_ARM_GICV3_ITS_COMMON, 141618f6290aSShashi Mallela .instance_size = sizeof(GICv3ITSState), 141718f6290aSShashi Mallela .class_init = gicv3_its_class_init, 141818f6290aSShashi Mallela .class_size = sizeof(GICv3ITSClass), 141918f6290aSShashi Mallela }; 142018f6290aSShashi Mallela 142118f6290aSShashi Mallela static void gicv3_its_register_types(void) 142218f6290aSShashi Mallela { 142318f6290aSShashi Mallela type_register_static(&gicv3_its_info); 142418f6290aSShashi Mallela } 142518f6290aSShashi Mallela 142618f6290aSShashi Mallela type_init(gicv3_its_register_types) 1427