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 494acf93e1SPeter Maydell typedef struct DTEntry { 504acf93e1SPeter Maydell bool valid; 514acf93e1SPeter Maydell unsigned size; 524acf93e1SPeter Maydell uint64_t ittaddr; 534acf93e1SPeter Maydell } DTEntry; 544acf93e1SPeter Maydell 55*d37cf49bSPeter Maydell typedef struct CTEntry { 56*d37cf49bSPeter Maydell bool valid; 57*d37cf49bSPeter Maydell uint32_t rdbase; 58*d37cf49bSPeter Maydell } CTEntry; 59*d37cf49bSPeter Maydell 60ef011555SPeter Maydell /* 61ef011555SPeter Maydell * The ITS spec permits a range of CONSTRAINED UNPREDICTABLE options 62ef011555SPeter Maydell * if a command parameter is not correct. These include both "stall 63ef011555SPeter Maydell * processing of the command queue" and "ignore this command, and 64ef011555SPeter Maydell * keep processing the queue". In our implementation we choose that 65ef011555SPeter Maydell * memory transaction errors reading the command packet provoke a 66ef011555SPeter Maydell * stall, but errors in parameters cause us to ignore the command 67ef011555SPeter Maydell * and continue processing. 68ef011555SPeter Maydell * The process_* functions which handle individual ITS commands all 69ef011555SPeter Maydell * return an ItsCmdResult which tells process_cmdq() whether it should 70ef011555SPeter Maydell * stall or keep going. 71ef011555SPeter Maydell */ 72ef011555SPeter Maydell typedef enum ItsCmdResult { 73ef011555SPeter Maydell CMD_STALL = 0, 74ef011555SPeter Maydell CMD_CONTINUE = 1, 75ef011555SPeter Maydell } ItsCmdResult; 76ef011555SPeter Maydell 771b08e436SShashi Mallela static uint64_t baser_base_addr(uint64_t value, uint32_t page_sz) 781b08e436SShashi Mallela { 791b08e436SShashi Mallela uint64_t result = 0; 801b08e436SShashi Mallela 811b08e436SShashi Mallela switch (page_sz) { 821b08e436SShashi Mallela case GITS_PAGE_SIZE_4K: 831b08e436SShashi Mallela case GITS_PAGE_SIZE_16K: 841b08e436SShashi Mallela result = FIELD_EX64(value, GITS_BASER, PHYADDR) << 12; 851b08e436SShashi Mallela break; 861b08e436SShashi Mallela 871b08e436SShashi Mallela case GITS_PAGE_SIZE_64K: 881b08e436SShashi Mallela result = FIELD_EX64(value, GITS_BASER, PHYADDRL_64K) << 16; 891b08e436SShashi Mallela result |= FIELD_EX64(value, GITS_BASER, PHYADDRH_64K) << 48; 901b08e436SShashi Mallela break; 911b08e436SShashi Mallela 921b08e436SShashi Mallela default: 931b08e436SShashi Mallela break; 941b08e436SShashi Mallela } 951b08e436SShashi Mallela return result; 961b08e436SShashi Mallela } 971b08e436SShashi Mallela 98d050f80fSPeter Maydell static uint64_t table_entry_addr(GICv3ITSState *s, TableDesc *td, 99d050f80fSPeter Maydell uint32_t idx, MemTxResult *res) 100d050f80fSPeter Maydell { 101d050f80fSPeter Maydell /* 102d050f80fSPeter Maydell * Given a TableDesc describing one of the ITS in-guest-memory 103d050f80fSPeter Maydell * tables and an index into it, return the guest address 104d050f80fSPeter Maydell * corresponding to that table entry. 105d050f80fSPeter Maydell * If there was a memory error reading the L1 table of an 106d050f80fSPeter Maydell * indirect table, *res is set accordingly, and we return -1. 107d050f80fSPeter Maydell * If the L1 table entry is marked not valid, we return -1 with 108d050f80fSPeter Maydell * *res set to MEMTX_OK. 109d050f80fSPeter Maydell * 110d050f80fSPeter Maydell * The specification defines the format of level 1 entries of a 111d050f80fSPeter Maydell * 2-level table, but the format of level 2 entries and the format 112d050f80fSPeter Maydell * of flat-mapped tables is IMPDEF. 113d050f80fSPeter Maydell */ 114d050f80fSPeter Maydell AddressSpace *as = &s->gicv3->dma_as; 115d050f80fSPeter Maydell uint32_t l2idx; 116d050f80fSPeter Maydell uint64_t l2; 117d050f80fSPeter Maydell uint32_t num_l2_entries; 118d050f80fSPeter Maydell 119d050f80fSPeter Maydell *res = MEMTX_OK; 120d050f80fSPeter Maydell 121d050f80fSPeter Maydell if (!td->indirect) { 122d050f80fSPeter Maydell /* Single level table */ 123d050f80fSPeter Maydell return td->base_addr + idx * td->entry_sz; 124d050f80fSPeter Maydell } 125d050f80fSPeter Maydell 126d050f80fSPeter Maydell /* Two level table */ 127d050f80fSPeter Maydell l2idx = idx / (td->page_sz / L1TABLE_ENTRY_SIZE); 128d050f80fSPeter Maydell 129d050f80fSPeter Maydell l2 = address_space_ldq_le(as, 130d050f80fSPeter Maydell td->base_addr + (l2idx * L1TABLE_ENTRY_SIZE), 131d050f80fSPeter Maydell MEMTXATTRS_UNSPECIFIED, res); 132d050f80fSPeter Maydell if (*res != MEMTX_OK) { 133d050f80fSPeter Maydell return -1; 134d050f80fSPeter Maydell } 135d050f80fSPeter Maydell if (!(l2 & L2_TABLE_VALID_MASK)) { 136d050f80fSPeter Maydell return -1; 137d050f80fSPeter Maydell } 138d050f80fSPeter Maydell 139d050f80fSPeter Maydell num_l2_entries = td->page_sz / td->entry_sz; 140d050f80fSPeter Maydell return (l2 & ((1ULL << 51) - 1)) + (idx % num_l2_entries) * td->entry_sz; 141d050f80fSPeter Maydell } 142d050f80fSPeter Maydell 143*d37cf49bSPeter Maydell /* 144*d37cf49bSPeter Maydell * Read the Collection Table entry at index @icid. On success (including 145*d37cf49bSPeter Maydell * successfully determining that there is no valid CTE for this index), 146*d37cf49bSPeter Maydell * we return MEMTX_OK and populate the CTEntry struct @cte accordingly. 147*d37cf49bSPeter Maydell * If there is an error reading memory then we return the error code. 148*d37cf49bSPeter Maydell */ 149*d37cf49bSPeter Maydell static MemTxResult get_cte(GICv3ITSState *s, uint16_t icid, CTEntry *cte) 150c694cb4cSShashi Mallela { 151c694cb4cSShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 152*d37cf49bSPeter Maydell MemTxResult res = MEMTX_OK; 153*d37cf49bSPeter Maydell uint64_t entry_addr = table_entry_addr(s, &s->ct, icid, &res); 154*d37cf49bSPeter Maydell uint64_t cteval; 155c694cb4cSShashi Mallela 156d050f80fSPeter Maydell if (entry_addr == -1) { 157*d37cf49bSPeter Maydell /* No L2 table entry, i.e. no valid CTE, or a memory error */ 158*d37cf49bSPeter Maydell cte->valid = false; 159*d37cf49bSPeter Maydell return res; 160c694cb4cSShashi Mallela } 161c694cb4cSShashi Mallela 162*d37cf49bSPeter Maydell cteval = address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, &res); 163*d37cf49bSPeter Maydell if (res != MEMTX_OK) { 164*d37cf49bSPeter Maydell return res; 165*d37cf49bSPeter Maydell } 166*d37cf49bSPeter Maydell cte->valid = FIELD_EX64(cteval, CTE, VALID); 167*d37cf49bSPeter Maydell cte->rdbase = FIELD_EX64(cteval, CTE, RDBASE); 168*d37cf49bSPeter Maydell return MEMTX_OK; 169c694cb4cSShashi Mallela } 170c694cb4cSShashi Mallela 1714acf93e1SPeter Maydell static bool update_ite(GICv3ITSState *s, uint32_t eventid, const DTEntry *dte, 172c694cb4cSShashi Mallela IteEntry ite) 173c694cb4cSShashi Mallela { 174c694cb4cSShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 175c694cb4cSShashi Mallela MemTxResult res = MEMTX_OK; 176c694cb4cSShashi Mallela 1774acf93e1SPeter Maydell address_space_stq_le(as, dte->ittaddr + (eventid * (sizeof(uint64_t) + 178c694cb4cSShashi Mallela sizeof(uint32_t))), ite.itel, MEMTXATTRS_UNSPECIFIED, 179c694cb4cSShashi Mallela &res); 180c694cb4cSShashi Mallela 181c694cb4cSShashi Mallela if (res == MEMTX_OK) { 1824acf93e1SPeter Maydell address_space_stl_le(as, dte->ittaddr + (eventid * (sizeof(uint64_t) + 183c694cb4cSShashi Mallela sizeof(uint32_t))) + sizeof(uint32_t), ite.iteh, 184c694cb4cSShashi Mallela MEMTXATTRS_UNSPECIFIED, &res); 185c694cb4cSShashi Mallela } 186c694cb4cSShashi Mallela if (res != MEMTX_OK) { 187c694cb4cSShashi Mallela return false; 188c694cb4cSShashi Mallela } else { 189c694cb4cSShashi Mallela return true; 190c694cb4cSShashi Mallela } 191c694cb4cSShashi Mallela } 192c694cb4cSShashi Mallela 1934acf93e1SPeter Maydell static bool get_ite(GICv3ITSState *s, uint32_t eventid, const DTEntry *dte, 194c694cb4cSShashi Mallela uint16_t *icid, uint32_t *pIntid, MemTxResult *res) 195c694cb4cSShashi Mallela { 196c694cb4cSShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 197c694cb4cSShashi Mallela bool status = false; 198c694cb4cSShashi Mallela IteEntry ite = {}; 199c694cb4cSShashi Mallela 2004acf93e1SPeter Maydell ite.itel = address_space_ldq_le(as, dte->ittaddr + 201c694cb4cSShashi Mallela (eventid * (sizeof(uint64_t) + 202c694cb4cSShashi Mallela sizeof(uint32_t))), MEMTXATTRS_UNSPECIFIED, 203c694cb4cSShashi Mallela res); 204c694cb4cSShashi Mallela 205c694cb4cSShashi Mallela if (*res == MEMTX_OK) { 2064acf93e1SPeter Maydell ite.iteh = address_space_ldl_le(as, dte->ittaddr + 207c694cb4cSShashi Mallela (eventid * (sizeof(uint64_t) + 208c694cb4cSShashi Mallela sizeof(uint32_t))) + sizeof(uint32_t), 209c694cb4cSShashi Mallela MEMTXATTRS_UNSPECIFIED, res); 210c694cb4cSShashi Mallela 211c694cb4cSShashi Mallela if (*res == MEMTX_OK) { 212764d6ba1SPeter Maydell if (FIELD_EX64(ite.itel, ITE_L, VALID)) { 213764d6ba1SPeter Maydell int inttype = FIELD_EX64(ite.itel, ITE_L, INTTYPE); 214764d6ba1SPeter Maydell if (inttype == ITE_INTTYPE_PHYSICAL) { 215764d6ba1SPeter Maydell *pIntid = FIELD_EX64(ite.itel, ITE_L, INTID); 216764d6ba1SPeter Maydell *icid = FIELD_EX32(ite.iteh, ITE_H, ICID); 217c694cb4cSShashi Mallela status = true; 218c694cb4cSShashi Mallela } 219c694cb4cSShashi Mallela } 220c694cb4cSShashi Mallela } 221c694cb4cSShashi Mallela } 222c694cb4cSShashi Mallela return status; 223c694cb4cSShashi Mallela } 224c694cb4cSShashi Mallela 2254acf93e1SPeter Maydell /* 2264acf93e1SPeter Maydell * Read the Device Table entry at index @devid. On success (including 2274acf93e1SPeter Maydell * successfully determining that there is no valid DTE for this index), 2284acf93e1SPeter Maydell * we return MEMTX_OK and populate the DTEntry struct accordingly. 2294acf93e1SPeter Maydell * If there is an error reading memory then we return the error code. 2304acf93e1SPeter Maydell */ 2314acf93e1SPeter Maydell static MemTxResult get_dte(GICv3ITSState *s, uint32_t devid, DTEntry *dte) 232c694cb4cSShashi Mallela { 2334acf93e1SPeter Maydell MemTxResult res = MEMTX_OK; 234c694cb4cSShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 2354acf93e1SPeter Maydell uint64_t entry_addr = table_entry_addr(s, &s->dt, devid, &res); 2364acf93e1SPeter Maydell uint64_t dteval; 237c694cb4cSShashi Mallela 238d050f80fSPeter Maydell if (entry_addr == -1) { 2394acf93e1SPeter Maydell /* No L2 table entry, i.e. no valid DTE, or a memory error */ 2404acf93e1SPeter Maydell dte->valid = false; 2414acf93e1SPeter Maydell return res; 242c694cb4cSShashi Mallela } 2434acf93e1SPeter Maydell dteval = address_space_ldq_le(as, entry_addr, MEMTXATTRS_UNSPECIFIED, &res); 2444acf93e1SPeter Maydell if (res != MEMTX_OK) { 2454acf93e1SPeter Maydell return res; 2464acf93e1SPeter Maydell } 2474acf93e1SPeter Maydell dte->valid = FIELD_EX64(dteval, DTE, VALID); 2484acf93e1SPeter Maydell dte->size = FIELD_EX64(dteval, DTE, SIZE); 2494acf93e1SPeter Maydell /* DTE word field stores bits [51:8] of the ITT address */ 2504acf93e1SPeter Maydell dte->ittaddr = FIELD_EX64(dteval, DTE, ITTADDR) << ITTADDR_SHIFT; 2514acf93e1SPeter Maydell return MEMTX_OK; 252c694cb4cSShashi Mallela } 253c694cb4cSShashi Mallela 254c694cb4cSShashi Mallela /* 255c694cb4cSShashi Mallela * This function handles the processing of following commands based on 256c694cb4cSShashi Mallela * the ItsCmdType parameter passed:- 257c694cb4cSShashi Mallela * 1. triggering of lpi interrupt translation via ITS INT command 258c694cb4cSShashi Mallela * 2. triggering of lpi interrupt translation via gits_translater register 259c694cb4cSShashi Mallela * 3. handling of ITS CLEAR command 260c694cb4cSShashi Mallela * 4. handling of ITS DISCARD command 261c694cb4cSShashi Mallela */ 262b6f96009SPeter Maydell static ItsCmdResult do_process_its_cmd(GICv3ITSState *s, uint32_t devid, 263b6f96009SPeter Maydell uint32_t eventid, ItsCmdType cmd) 264c694cb4cSShashi Mallela { 265c694cb4cSShashi Mallela MemTxResult res = MEMTX_OK; 2668f809f69SPeter Maydell uint64_t num_eventids; 267c694cb4cSShashi Mallela uint16_t icid = 0; 268c694cb4cSShashi Mallela uint32_t pIntid = 0; 269c694cb4cSShashi Mallela bool ite_valid = false; 2704acf93e1SPeter Maydell DTEntry dte; 271*d37cf49bSPeter Maydell CTEntry cte; 272c694cb4cSShashi Mallela 2738b8bb014SPeter Maydell if (devid >= s->dt.num_entries) { 274b13148d9SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 275b13148d9SPeter Maydell "%s: invalid command attributes: devid %d>=%d", 2768b8bb014SPeter Maydell __func__, devid, s->dt.num_entries); 277b13148d9SPeter Maydell return CMD_CONTINUE; 278b13148d9SPeter Maydell } 279b13148d9SPeter Maydell 2804acf93e1SPeter Maydell if (get_dte(s, devid, &dte) != MEMTX_OK) { 281593a7cc2SPeter Maydell return CMD_STALL; 282c694cb4cSShashi Mallela } 2834acf93e1SPeter Maydell if (!dte.valid) { 284229c57b1SAlex Bennée qemu_log_mask(LOG_GUEST_ERROR, 285229c57b1SAlex Bennée "%s: invalid command attributes: " 2864acf93e1SPeter Maydell "invalid dte for %d\n", __func__, devid); 287593a7cc2SPeter Maydell return CMD_CONTINUE; 288c694cb4cSShashi Mallela } 289c694cb4cSShashi Mallela 2904acf93e1SPeter Maydell num_eventids = 1ULL << (dte.size + 1); 291b13148d9SPeter Maydell if (eventid >= num_eventids) { 292b13148d9SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 293b13148d9SPeter Maydell "%s: invalid command attributes: eventid %d >= %" 294b13148d9SPeter Maydell PRId64 "\n", 295b13148d9SPeter Maydell __func__, eventid, num_eventids); 296b13148d9SPeter Maydell return CMD_CONTINUE; 297b13148d9SPeter Maydell } 298b13148d9SPeter Maydell 2994acf93e1SPeter Maydell ite_valid = get_ite(s, eventid, &dte, &icid, &pIntid, &res); 300be0ed8fbSPeter Maydell if (res != MEMTX_OK) { 301be0ed8fbSPeter Maydell return CMD_STALL; 302be0ed8fbSPeter Maydell } 303be0ed8fbSPeter Maydell 304be0ed8fbSPeter Maydell if (!ite_valid) { 305be0ed8fbSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 306be0ed8fbSPeter Maydell "%s: invalid command attributes: invalid ITE\n", 307be0ed8fbSPeter Maydell __func__); 308be0ed8fbSPeter Maydell return CMD_CONTINUE; 309be0ed8fbSPeter Maydell } 310be0ed8fbSPeter Maydell 3118b8bb014SPeter Maydell if (icid >= s->ct.num_entries) { 31258b88779SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 31358b88779SPeter Maydell "%s: invalid ICID 0x%x in ITE (table corrupted?)\n", 31458b88779SPeter Maydell __func__, icid); 31558b88779SPeter Maydell return CMD_CONTINUE; 31658b88779SPeter Maydell } 31758b88779SPeter Maydell 318*d37cf49bSPeter Maydell if (get_cte(s, icid, &cte) != MEMTX_OK) { 319be0ed8fbSPeter Maydell return CMD_STALL; 320be0ed8fbSPeter Maydell } 321*d37cf49bSPeter Maydell if (!cte.valid) { 322be0ed8fbSPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 323*d37cf49bSPeter Maydell "%s: invalid command attributes: invalid CTE\n", 324*d37cf49bSPeter Maydell __func__); 325be0ed8fbSPeter Maydell return CMD_CONTINUE; 326be0ed8fbSPeter Maydell } 327be0ed8fbSPeter Maydell 328c694cb4cSShashi Mallela /* 329c694cb4cSShashi Mallela * Current implementation only supports rdbase == procnum 330c694cb4cSShashi Mallela * Hence rdbase physical address is ignored 331c694cb4cSShashi Mallela */ 332*d37cf49bSPeter Maydell if (cte.rdbase >= s->gicv3->num_cpu) { 333593a7cc2SPeter Maydell return CMD_CONTINUE; 33417fb5e36SShashi Mallela } 33517fb5e36SShashi Mallela 33617fb5e36SShashi Mallela if ((cmd == CLEAR) || (cmd == DISCARD)) { 337*d37cf49bSPeter Maydell gicv3_redist_process_lpi(&s->gicv3->cpu[cte.rdbase], pIntid, 0); 33817fb5e36SShashi Mallela } else { 339*d37cf49bSPeter Maydell gicv3_redist_process_lpi(&s->gicv3->cpu[cte.rdbase], pIntid, 1); 34017fb5e36SShashi Mallela } 34117fb5e36SShashi Mallela 342c694cb4cSShashi Mallela if (cmd == DISCARD) { 343c694cb4cSShashi Mallela IteEntry ite = {}; 344c694cb4cSShashi Mallela /* remove mapping from interrupt translation table */ 3454acf93e1SPeter Maydell return update_ite(s, eventid, &dte, ite) ? CMD_CONTINUE : CMD_STALL; 346c694cb4cSShashi Mallela } 347593a7cc2SPeter Maydell return CMD_CONTINUE; 348c694cb4cSShashi Mallela } 349b6f96009SPeter Maydell static ItsCmdResult process_its_cmd(GICv3ITSState *s, const uint64_t *cmdpkt, 350b6f96009SPeter Maydell ItsCmdType cmd) 351c694cb4cSShashi Mallela { 352b6f96009SPeter Maydell uint32_t devid, eventid; 353b6f96009SPeter Maydell 354b6f96009SPeter Maydell devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT; 355b6f96009SPeter Maydell eventid = cmdpkt[1] & EVENTID_MASK; 356b6f96009SPeter Maydell return do_process_its_cmd(s, devid, eventid, cmd); 357b6f96009SPeter Maydell } 358b6f96009SPeter Maydell 359b6f96009SPeter Maydell static ItsCmdResult process_mapti(GICv3ITSState *s, const uint64_t *cmdpkt, 360b6f96009SPeter Maydell bool ignore_pInt) 361b6f96009SPeter Maydell { 362c694cb4cSShashi Mallela uint32_t devid, eventid; 363c694cb4cSShashi Mallela uint32_t pIntid = 0; 3648f809f69SPeter Maydell uint64_t num_eventids; 365905720f1SPeter Maydell uint32_t num_intids; 366c694cb4cSShashi Mallela uint16_t icid = 0; 3670241f731SPeter Maydell IteEntry ite = {}; 3684acf93e1SPeter Maydell DTEntry dte; 369c694cb4cSShashi Mallela 370b6f96009SPeter Maydell devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT; 371b6f96009SPeter Maydell eventid = cmdpkt[1] & EVENTID_MASK; 372c694cb4cSShashi Mallela 373b87fab1cSPeter Maydell if (ignore_pInt) { 374b87fab1cSPeter Maydell pIntid = eventid; 375b87fab1cSPeter Maydell } else { 376b6f96009SPeter Maydell pIntid = (cmdpkt[1] & pINTID_MASK) >> pINTID_SHIFT; 377c694cb4cSShashi Mallela } 378c694cb4cSShashi Mallela 379b6f96009SPeter Maydell icid = cmdpkt[2] & ICID_MASK; 380c694cb4cSShashi Mallela 3818b8bb014SPeter Maydell if (devid >= s->dt.num_entries) { 382b13148d9SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 383b13148d9SPeter Maydell "%s: invalid command attributes: devid %d>=%d", 3848b8bb014SPeter Maydell __func__, devid, s->dt.num_entries); 385b13148d9SPeter Maydell return CMD_CONTINUE; 386b13148d9SPeter Maydell } 387b13148d9SPeter Maydell 3884acf93e1SPeter Maydell if (get_dte(s, devid, &dte) != MEMTX_OK) { 3890241f731SPeter Maydell return CMD_STALL; 390c694cb4cSShashi Mallela } 3914acf93e1SPeter Maydell num_eventids = 1ULL << (dte.size + 1); 392905720f1SPeter Maydell num_intids = 1ULL << (GICD_TYPER_IDBITS + 1); 393c694cb4cSShashi Mallela 3948b8bb014SPeter Maydell if ((icid >= s->ct.num_entries) 3954acf93e1SPeter Maydell || !dte.valid || (eventid >= num_eventids) || 396905720f1SPeter Maydell (((pIntid < GICV3_LPI_INTID_START) || (pIntid >= num_intids)) && 397b87fab1cSPeter Maydell (pIntid != INTID_SPURIOUS))) { 398c694cb4cSShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 399c694cb4cSShashi Mallela "%s: invalid command attributes " 400b13148d9SPeter Maydell "icid %d or eventid %d or pIntid %d or" 401b13148d9SPeter Maydell "unmapped dte %d\n", __func__, icid, eventid, 4024acf93e1SPeter Maydell pIntid, dte.valid); 403c694cb4cSShashi Mallela /* 404c694cb4cSShashi Mallela * in this implementation, in case of error 405c694cb4cSShashi Mallela * we ignore this command and move onto the next 406c694cb4cSShashi Mallela * command in the queue 407c694cb4cSShashi Mallela */ 4080241f731SPeter Maydell return CMD_CONTINUE; 4090241f731SPeter Maydell } 4100241f731SPeter Maydell 411c694cb4cSShashi Mallela /* add ite entry to interrupt translation table */ 4124acf93e1SPeter Maydell ite.itel = FIELD_DP64(ite.itel, ITE_L, VALID, true); 413764d6ba1SPeter Maydell ite.itel = FIELD_DP64(ite.itel, ITE_L, INTTYPE, ITE_INTTYPE_PHYSICAL); 414764d6ba1SPeter Maydell ite.itel = FIELD_DP64(ite.itel, ITE_L, INTID, pIntid); 415764d6ba1SPeter Maydell ite.itel = FIELD_DP64(ite.itel, ITE_L, DOORBELL, INTID_SPURIOUS); 416764d6ba1SPeter Maydell ite.iteh = FIELD_DP32(ite.iteh, ITE_H, ICID, icid); 417c694cb4cSShashi Mallela 4184acf93e1SPeter Maydell return update_ite(s, eventid, &dte, ite) ? CMD_CONTINUE : CMD_STALL; 419c694cb4cSShashi Mallela } 420c694cb4cSShashi Mallela 4217eca39e0SShashi Mallela static bool update_cte(GICv3ITSState *s, uint16_t icid, bool valid, 4227eca39e0SShashi Mallela uint64_t rdbase) 4237eca39e0SShashi Mallela { 4247eca39e0SShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 425d050f80fSPeter Maydell uint64_t entry_addr; 4267eca39e0SShashi Mallela uint64_t cte = 0; 4277eca39e0SShashi Mallela MemTxResult res = MEMTX_OK; 4287eca39e0SShashi Mallela 4297eca39e0SShashi Mallela if (!s->ct.valid) { 4307eca39e0SShashi Mallela return true; 4317eca39e0SShashi Mallela } 4327eca39e0SShashi Mallela 4337eca39e0SShashi Mallela if (valid) { 4347eca39e0SShashi Mallela /* add mapping entry to collection table */ 435437dc0eaSPeter Maydell cte = FIELD_DP64(cte, CTE, VALID, 1); 436437dc0eaSPeter Maydell cte = FIELD_DP64(cte, CTE, RDBASE, rdbase); 4377eca39e0SShashi Mallela } 4387eca39e0SShashi Mallela 439d050f80fSPeter Maydell entry_addr = table_entry_addr(s, &s->ct, icid, &res); 4407eca39e0SShashi Mallela if (res != MEMTX_OK) { 441d050f80fSPeter Maydell /* memory access error: stall */ 4427eca39e0SShashi Mallela return false; 4437eca39e0SShashi Mallela } 444d050f80fSPeter Maydell if (entry_addr == -1) { 445d050f80fSPeter Maydell /* No L2 table for this index: discard write and continue */ 4467eca39e0SShashi Mallela return true; 4477eca39e0SShashi Mallela } 448d050f80fSPeter Maydell 449d050f80fSPeter Maydell address_space_stq_le(as, entry_addr, cte, MEMTXATTRS_UNSPECIFIED, &res); 450d050f80fSPeter Maydell return res == MEMTX_OK; 4517eca39e0SShashi Mallela } 4527eca39e0SShashi Mallela 453b6f96009SPeter Maydell static ItsCmdResult process_mapc(GICv3ITSState *s, const uint64_t *cmdpkt) 4547eca39e0SShashi Mallela { 4557eca39e0SShashi Mallela uint16_t icid; 4567eca39e0SShashi Mallela uint64_t rdbase; 4577eca39e0SShashi Mallela bool valid; 4587eca39e0SShashi Mallela 459b6f96009SPeter Maydell icid = cmdpkt[2] & ICID_MASK; 4607eca39e0SShashi Mallela 461b6f96009SPeter Maydell rdbase = (cmdpkt[2] & R_MAPC_RDBASE_MASK) >> R_MAPC_RDBASE_SHIFT; 4627eca39e0SShashi Mallela rdbase &= RDBASE_PROCNUM_MASK; 4637eca39e0SShashi Mallela 464b6f96009SPeter Maydell valid = cmdpkt[2] & CMD_FIELD_VALID_MASK; 4657eca39e0SShashi Mallela 4668b8bb014SPeter Maydell if ((icid >= s->ct.num_entries) || (rdbase >= s->gicv3->num_cpu)) { 4677eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 4687eca39e0SShashi Mallela "ITS MAPC: invalid collection table attributes " 4697eca39e0SShashi Mallela "icid %d rdbase %" PRIu64 "\n", icid, rdbase); 4707eca39e0SShashi Mallela /* 4717eca39e0SShashi Mallela * in this implementation, in case of error 4727eca39e0SShashi Mallela * we ignore this command and move onto the next 4737eca39e0SShashi Mallela * command in the queue 4747eca39e0SShashi Mallela */ 475f6675196SPeter Maydell return CMD_CONTINUE; 4767eca39e0SShashi Mallela } 4777eca39e0SShashi Mallela 478f6675196SPeter Maydell return update_cte(s, icid, valid, rdbase) ? CMD_CONTINUE : CMD_STALL; 4797eca39e0SShashi Mallela } 4807eca39e0SShashi Mallela 48122d62b08SPeter Maydell /* 48222d62b08SPeter Maydell * Update the Device Table entry for @devid to @dte. Returns true 48322d62b08SPeter Maydell * on success, false if there was a memory access error. 48422d62b08SPeter Maydell */ 48522d62b08SPeter Maydell static bool update_dte(GICv3ITSState *s, uint32_t devid, const DTEntry *dte) 4867eca39e0SShashi Mallela { 4877eca39e0SShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 488d050f80fSPeter Maydell uint64_t entry_addr; 48922d62b08SPeter Maydell uint64_t dteval = 0; 4907eca39e0SShashi Mallela MemTxResult res = MEMTX_OK; 4917eca39e0SShashi Mallela 4927eca39e0SShashi Mallela if (s->dt.valid) { 49322d62b08SPeter Maydell if (dte->valid) { 4947eca39e0SShashi Mallela /* add mapping entry to device table */ 49522d62b08SPeter Maydell dteval = FIELD_DP64(dteval, DTE, VALID, 1); 49622d62b08SPeter Maydell dteval = FIELD_DP64(dteval, DTE, SIZE, dte->size); 49722d62b08SPeter Maydell dteval = FIELD_DP64(dteval, DTE, ITTADDR, dte->ittaddr); 4987eca39e0SShashi Mallela } 4997eca39e0SShashi Mallela } else { 5007eca39e0SShashi Mallela return true; 5017eca39e0SShashi Mallela } 5027eca39e0SShashi Mallela 503d050f80fSPeter Maydell entry_addr = table_entry_addr(s, &s->dt, devid, &res); 5047eca39e0SShashi Mallela if (res != MEMTX_OK) { 505d050f80fSPeter Maydell /* memory access error: stall */ 5067eca39e0SShashi Mallela return false; 5077eca39e0SShashi Mallela } 508d050f80fSPeter Maydell if (entry_addr == -1) { 509d050f80fSPeter Maydell /* No L2 table for this index: discard write and continue */ 5107eca39e0SShashi Mallela return true; 5117eca39e0SShashi Mallela } 51222d62b08SPeter Maydell address_space_stq_le(as, entry_addr, dteval, MEMTXATTRS_UNSPECIFIED, &res); 513d050f80fSPeter Maydell return res == MEMTX_OK; 5147eca39e0SShashi Mallela } 5157eca39e0SShashi Mallela 516b6f96009SPeter Maydell static ItsCmdResult process_mapd(GICv3ITSState *s, const uint64_t *cmdpkt) 5177eca39e0SShashi Mallela { 5187eca39e0SShashi Mallela uint32_t devid; 51922d62b08SPeter Maydell DTEntry dte; 5207eca39e0SShashi Mallela 521b6f96009SPeter Maydell devid = (cmdpkt[0] & DEVID_MASK) >> DEVID_SHIFT; 52222d62b08SPeter Maydell dte.size = cmdpkt[1] & SIZE_MASK; 52322d62b08SPeter Maydell dte.ittaddr = (cmdpkt[2] & ITTADDR_MASK) >> ITTADDR_SHIFT; 52422d62b08SPeter Maydell dte.valid = cmdpkt[2] & CMD_FIELD_VALID_MASK; 5257eca39e0SShashi Mallela 5268b8bb014SPeter Maydell if ((devid >= s->dt.num_entries) || 52722d62b08SPeter Maydell (dte.size > FIELD_EX64(s->typer, GITS_TYPER, IDBITS))) { 5287eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 5297eca39e0SShashi Mallela "ITS MAPD: invalid device table attributes " 53022d62b08SPeter Maydell "devid %d or size %d\n", devid, dte.size); 5317eca39e0SShashi Mallela /* 5327eca39e0SShashi Mallela * in this implementation, in case of error 5337eca39e0SShashi Mallela * we ignore this command and move onto the next 5347eca39e0SShashi Mallela * command in the queue 5357eca39e0SShashi Mallela */ 53600d46e72SPeter Maydell return CMD_CONTINUE; 5377eca39e0SShashi Mallela } 5387eca39e0SShashi Mallela 53922d62b08SPeter Maydell return update_dte(s, devid, &dte) ? CMD_CONTINUE : CMD_STALL; 5407eca39e0SShashi Mallela } 5417eca39e0SShashi Mallela 542b6f96009SPeter Maydell static ItsCmdResult process_movall(GICv3ITSState *s, const uint64_t *cmdpkt) 543f6d1d9b4SPeter Maydell { 544f6d1d9b4SPeter Maydell uint64_t rd1, rd2; 545f6d1d9b4SPeter Maydell 546b6f96009SPeter Maydell rd1 = FIELD_EX64(cmdpkt[2], MOVALL_2, RDBASE1); 547b6f96009SPeter Maydell rd2 = FIELD_EX64(cmdpkt[3], MOVALL_3, RDBASE2); 548f6d1d9b4SPeter Maydell 549f6d1d9b4SPeter Maydell if (rd1 >= s->gicv3->num_cpu) { 550f6d1d9b4SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 551f6d1d9b4SPeter Maydell "%s: RDBASE1 %" PRId64 552f6d1d9b4SPeter Maydell " out of range (must be less than %d)\n", 553f6d1d9b4SPeter Maydell __func__, rd1, s->gicv3->num_cpu); 554f6d1d9b4SPeter Maydell return CMD_CONTINUE; 555f6d1d9b4SPeter Maydell } 556f6d1d9b4SPeter Maydell if (rd2 >= s->gicv3->num_cpu) { 557f6d1d9b4SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 558f6d1d9b4SPeter Maydell "%s: RDBASE2 %" PRId64 559f6d1d9b4SPeter Maydell " out of range (must be less than %d)\n", 560f6d1d9b4SPeter Maydell __func__, rd2, s->gicv3->num_cpu); 561f6d1d9b4SPeter Maydell return CMD_CONTINUE; 562f6d1d9b4SPeter Maydell } 563f6d1d9b4SPeter Maydell 564f6d1d9b4SPeter Maydell if (rd1 == rd2) { 565f6d1d9b4SPeter Maydell /* Move to same target must succeed as a no-op */ 566f6d1d9b4SPeter Maydell return CMD_CONTINUE; 567f6d1d9b4SPeter Maydell } 568f6d1d9b4SPeter Maydell 569f6d1d9b4SPeter Maydell /* Move all pending LPIs from redistributor 1 to redistributor 2 */ 570f6d1d9b4SPeter Maydell gicv3_redist_movall_lpis(&s->gicv3->cpu[rd1], &s->gicv3->cpu[rd2]); 571f6d1d9b4SPeter Maydell 572f6d1d9b4SPeter Maydell return CMD_CONTINUE; 573f6d1d9b4SPeter Maydell } 574f6d1d9b4SPeter Maydell 575b6f96009SPeter Maydell static ItsCmdResult process_movi(GICv3ITSState *s, const uint64_t *cmdpkt) 576961b4912SPeter Maydell { 577961b4912SPeter Maydell MemTxResult res = MEMTX_OK; 578961b4912SPeter Maydell uint32_t devid, eventid, intid; 579961b4912SPeter Maydell uint16_t old_icid, new_icid; 580*d37cf49bSPeter Maydell bool ite_valid; 581961b4912SPeter Maydell uint64_t num_eventids; 582961b4912SPeter Maydell IteEntry ite = {}; 5834acf93e1SPeter Maydell DTEntry dte; 584*d37cf49bSPeter Maydell CTEntry old_cte, new_cte; 585961b4912SPeter Maydell 586b6f96009SPeter Maydell devid = FIELD_EX64(cmdpkt[0], MOVI_0, DEVICEID); 587b6f96009SPeter Maydell eventid = FIELD_EX64(cmdpkt[1], MOVI_1, EVENTID); 588b6f96009SPeter Maydell new_icid = FIELD_EX64(cmdpkt[2], MOVI_2, ICID); 589961b4912SPeter Maydell 590961b4912SPeter Maydell if (devid >= s->dt.num_entries) { 591961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 592961b4912SPeter Maydell "%s: invalid command attributes: devid %d>=%d", 593961b4912SPeter Maydell __func__, devid, s->dt.num_entries); 594961b4912SPeter Maydell return CMD_CONTINUE; 595961b4912SPeter Maydell } 5964acf93e1SPeter Maydell if (get_dte(s, devid, &dte) != MEMTX_OK) { 597961b4912SPeter Maydell return CMD_STALL; 598961b4912SPeter Maydell } 599961b4912SPeter Maydell 6004acf93e1SPeter Maydell if (!dte.valid) { 601961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 602961b4912SPeter Maydell "%s: invalid command attributes: " 6034acf93e1SPeter Maydell "invalid dte for %d\n", __func__, devid); 604961b4912SPeter Maydell return CMD_CONTINUE; 605961b4912SPeter Maydell } 606961b4912SPeter Maydell 6074acf93e1SPeter Maydell num_eventids = 1ULL << (dte.size + 1); 608961b4912SPeter Maydell if (eventid >= num_eventids) { 609961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 610961b4912SPeter Maydell "%s: invalid command attributes: eventid %d >= %" 611961b4912SPeter Maydell PRId64 "\n", 612961b4912SPeter Maydell __func__, eventid, num_eventids); 613961b4912SPeter Maydell return CMD_CONTINUE; 614961b4912SPeter Maydell } 615961b4912SPeter Maydell 6164acf93e1SPeter Maydell ite_valid = get_ite(s, eventid, &dte, &old_icid, &intid, &res); 617961b4912SPeter Maydell if (res != MEMTX_OK) { 618961b4912SPeter Maydell return CMD_STALL; 619961b4912SPeter Maydell } 620961b4912SPeter Maydell 621961b4912SPeter Maydell if (!ite_valid) { 622961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 623961b4912SPeter Maydell "%s: invalid command attributes: invalid ITE\n", 624961b4912SPeter Maydell __func__); 625961b4912SPeter Maydell return CMD_CONTINUE; 626961b4912SPeter Maydell } 627961b4912SPeter Maydell 628961b4912SPeter Maydell if (old_icid >= s->ct.num_entries) { 629961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 630961b4912SPeter Maydell "%s: invalid ICID 0x%x in ITE (table corrupted?)\n", 631961b4912SPeter Maydell __func__, old_icid); 632961b4912SPeter Maydell return CMD_CONTINUE; 633961b4912SPeter Maydell } 634961b4912SPeter Maydell 635961b4912SPeter Maydell if (new_icid >= s->ct.num_entries) { 636961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 637961b4912SPeter Maydell "%s: invalid command attributes: ICID 0x%x\n", 638961b4912SPeter Maydell __func__, new_icid); 639961b4912SPeter Maydell return CMD_CONTINUE; 640961b4912SPeter Maydell } 641961b4912SPeter Maydell 642*d37cf49bSPeter Maydell if (get_cte(s, old_icid, &old_cte) != MEMTX_OK) { 643961b4912SPeter Maydell return CMD_STALL; 644961b4912SPeter Maydell } 645*d37cf49bSPeter Maydell if (!old_cte.valid) { 646961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 647961b4912SPeter Maydell "%s: invalid command attributes: " 648*d37cf49bSPeter Maydell "invalid CTE for old ICID 0x%x\n", 649*d37cf49bSPeter Maydell __func__, old_icid); 650961b4912SPeter Maydell return CMD_CONTINUE; 651961b4912SPeter Maydell } 652961b4912SPeter Maydell 653*d37cf49bSPeter Maydell if (get_cte(s, new_icid, &new_cte) != MEMTX_OK) { 654961b4912SPeter Maydell return CMD_STALL; 655961b4912SPeter Maydell } 656*d37cf49bSPeter Maydell if (!new_cte.valid) { 657961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 658961b4912SPeter Maydell "%s: invalid command attributes: " 659*d37cf49bSPeter Maydell "invalid CTE for new ICID 0x%x\n", 660*d37cf49bSPeter Maydell __func__, new_icid); 661961b4912SPeter Maydell return CMD_CONTINUE; 662961b4912SPeter Maydell } 663961b4912SPeter Maydell 664*d37cf49bSPeter Maydell if (old_cte.rdbase >= s->gicv3->num_cpu) { 665961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 666*d37cf49bSPeter Maydell "%s: CTE has invalid rdbase 0x%x\n", 667*d37cf49bSPeter Maydell __func__, old_cte.rdbase); 668961b4912SPeter Maydell return CMD_CONTINUE; 669961b4912SPeter Maydell } 670961b4912SPeter Maydell 671*d37cf49bSPeter Maydell if (new_cte.rdbase >= s->gicv3->num_cpu) { 672961b4912SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 673*d37cf49bSPeter Maydell "%s: CTE has invalid rdbase 0x%x\n", 674*d37cf49bSPeter Maydell __func__, new_cte.rdbase); 675961b4912SPeter Maydell return CMD_CONTINUE; 676961b4912SPeter Maydell } 677961b4912SPeter Maydell 678*d37cf49bSPeter Maydell if (old_cte.rdbase != new_cte.rdbase) { 679961b4912SPeter Maydell /* Move the LPI from the old redistributor to the new one */ 680*d37cf49bSPeter Maydell gicv3_redist_mov_lpi(&s->gicv3->cpu[old_cte.rdbase], 681*d37cf49bSPeter Maydell &s->gicv3->cpu[new_cte.rdbase], 682961b4912SPeter Maydell intid); 683961b4912SPeter Maydell } 684961b4912SPeter Maydell 685961b4912SPeter Maydell /* Update the ICID field in the interrupt translation table entry */ 686961b4912SPeter Maydell ite.itel = FIELD_DP64(ite.itel, ITE_L, VALID, 1); 687961b4912SPeter Maydell ite.itel = FIELD_DP64(ite.itel, ITE_L, INTTYPE, ITE_INTTYPE_PHYSICAL); 688961b4912SPeter Maydell ite.itel = FIELD_DP64(ite.itel, ITE_L, INTID, intid); 689961b4912SPeter Maydell ite.itel = FIELD_DP64(ite.itel, ITE_L, DOORBELL, INTID_SPURIOUS); 690961b4912SPeter Maydell ite.iteh = FIELD_DP32(ite.iteh, ITE_H, ICID, new_icid); 6914acf93e1SPeter Maydell return update_ite(s, eventid, &dte, ite) ? CMD_CONTINUE : CMD_STALL; 692961b4912SPeter Maydell } 693961b4912SPeter Maydell 6947eca39e0SShashi Mallela /* 6957eca39e0SShashi Mallela * Current implementation blocks until all 6967eca39e0SShashi Mallela * commands are processed 6977eca39e0SShashi Mallela */ 6987eca39e0SShashi Mallela static void process_cmdq(GICv3ITSState *s) 6997eca39e0SShashi Mallela { 7007eca39e0SShashi Mallela uint32_t wr_offset = 0; 7017eca39e0SShashi Mallela uint32_t rd_offset = 0; 7027eca39e0SShashi Mallela uint32_t cq_offset = 0; 7037eca39e0SShashi Mallela AddressSpace *as = &s->gicv3->dma_as; 7047eca39e0SShashi Mallela uint8_t cmd; 70517fb5e36SShashi Mallela int i; 7067eca39e0SShashi Mallela 7078d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 7087eca39e0SShashi Mallela return; 7097eca39e0SShashi Mallela } 7107eca39e0SShashi Mallela 7117eca39e0SShashi Mallela wr_offset = FIELD_EX64(s->cwriter, GITS_CWRITER, OFFSET); 7127eca39e0SShashi Mallela 71380dcd37fSPeter Maydell if (wr_offset >= s->cq.num_entries) { 7147eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 7157eca39e0SShashi Mallela "%s: invalid write offset " 7167eca39e0SShashi Mallela "%d\n", __func__, wr_offset); 7177eca39e0SShashi Mallela return; 7187eca39e0SShashi Mallela } 7197eca39e0SShashi Mallela 7207eca39e0SShashi Mallela rd_offset = FIELD_EX64(s->creadr, GITS_CREADR, OFFSET); 7217eca39e0SShashi Mallela 72280dcd37fSPeter Maydell if (rd_offset >= s->cq.num_entries) { 7237eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 7247eca39e0SShashi Mallela "%s: invalid read offset " 7257eca39e0SShashi Mallela "%d\n", __func__, rd_offset); 7267eca39e0SShashi Mallela return; 7277eca39e0SShashi Mallela } 7287eca39e0SShashi Mallela 7297eca39e0SShashi Mallela while (wr_offset != rd_offset) { 730ef011555SPeter Maydell ItsCmdResult result = CMD_CONTINUE; 731b6f96009SPeter Maydell void *hostmem; 732b6f96009SPeter Maydell hwaddr buflen; 733b6f96009SPeter Maydell uint64_t cmdpkt[GITS_CMDQ_ENTRY_WORDS]; 734ef011555SPeter Maydell 7357eca39e0SShashi Mallela cq_offset = (rd_offset * GITS_CMDQ_ENTRY_SIZE); 736b6f96009SPeter Maydell 737b6f96009SPeter Maydell buflen = GITS_CMDQ_ENTRY_SIZE; 738b6f96009SPeter Maydell hostmem = address_space_map(as, s->cq.base_addr + cq_offset, 739b6f96009SPeter Maydell &buflen, false, MEMTXATTRS_UNSPECIFIED); 740b6f96009SPeter Maydell if (!hostmem || buflen != GITS_CMDQ_ENTRY_SIZE) { 741b6f96009SPeter Maydell if (hostmem) { 742b6f96009SPeter Maydell address_space_unmap(as, hostmem, buflen, false, 0); 743b6f96009SPeter Maydell } 744f0b4b2a2SPeter Maydell s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1); 745f0b4b2a2SPeter Maydell qemu_log_mask(LOG_GUEST_ERROR, 746f0b4b2a2SPeter Maydell "%s: could not read command at 0x%" PRIx64 "\n", 747f0b4b2a2SPeter Maydell __func__, s->cq.base_addr + cq_offset); 748f0b4b2a2SPeter Maydell break; 7497eca39e0SShashi Mallela } 750b6f96009SPeter Maydell for (i = 0; i < ARRAY_SIZE(cmdpkt); i++) { 751b6f96009SPeter Maydell cmdpkt[i] = ldq_le_p(hostmem + i * sizeof(uint64_t)); 752b6f96009SPeter Maydell } 753b6f96009SPeter Maydell address_space_unmap(as, hostmem, buflen, false, 0); 754f0b4b2a2SPeter Maydell 755b6f96009SPeter Maydell cmd = cmdpkt[0] & CMD_MASK; 7567eca39e0SShashi Mallela 757195209d3SPeter Maydell trace_gicv3_its_process_command(rd_offset, cmd); 758195209d3SPeter Maydell 7597eca39e0SShashi Mallela switch (cmd) { 7607eca39e0SShashi Mallela case GITS_CMD_INT: 761b6f96009SPeter Maydell result = process_its_cmd(s, cmdpkt, INTERRUPT); 7627eca39e0SShashi Mallela break; 7637eca39e0SShashi Mallela case GITS_CMD_CLEAR: 764b6f96009SPeter Maydell result = process_its_cmd(s, cmdpkt, CLEAR); 7657eca39e0SShashi Mallela break; 7667eca39e0SShashi Mallela case GITS_CMD_SYNC: 7677eca39e0SShashi Mallela /* 7687eca39e0SShashi Mallela * Current implementation makes a blocking synchronous call 7697eca39e0SShashi Mallela * for every command issued earlier, hence the internal state 7707eca39e0SShashi Mallela * is already consistent by the time SYNC command is executed. 7717eca39e0SShashi Mallela * Hence no further processing is required for SYNC command. 7727eca39e0SShashi Mallela */ 7737eca39e0SShashi Mallela break; 7747eca39e0SShashi Mallela case GITS_CMD_MAPD: 775b6f96009SPeter Maydell result = process_mapd(s, cmdpkt); 7767eca39e0SShashi Mallela break; 7777eca39e0SShashi Mallela case GITS_CMD_MAPC: 778b6f96009SPeter Maydell result = process_mapc(s, cmdpkt); 7797eca39e0SShashi Mallela break; 7807eca39e0SShashi Mallela case GITS_CMD_MAPTI: 781b6f96009SPeter Maydell result = process_mapti(s, cmdpkt, false); 7827eca39e0SShashi Mallela break; 7837eca39e0SShashi Mallela case GITS_CMD_MAPI: 784b6f96009SPeter Maydell result = process_mapti(s, cmdpkt, true); 7857eca39e0SShashi Mallela break; 7867eca39e0SShashi Mallela case GITS_CMD_DISCARD: 787b6f96009SPeter Maydell result = process_its_cmd(s, cmdpkt, DISCARD); 7887eca39e0SShashi Mallela break; 7897eca39e0SShashi Mallela case GITS_CMD_INV: 7907eca39e0SShashi Mallela case GITS_CMD_INVALL: 79117fb5e36SShashi Mallela /* 79217fb5e36SShashi Mallela * Current implementation doesn't cache any ITS tables, 79317fb5e36SShashi Mallela * but the calculated lpi priority information. We only 79417fb5e36SShashi Mallela * need to trigger lpi priority re-calculation to be in 79517fb5e36SShashi Mallela * sync with LPI config table or pending table changes. 79617fb5e36SShashi Mallela */ 79717fb5e36SShashi Mallela for (i = 0; i < s->gicv3->num_cpu; i++) { 79817fb5e36SShashi Mallela gicv3_redist_update_lpi(&s->gicv3->cpu[i]); 79917fb5e36SShashi Mallela } 8007eca39e0SShashi Mallela break; 801961b4912SPeter Maydell case GITS_CMD_MOVI: 802b6f96009SPeter Maydell result = process_movi(s, cmdpkt); 803961b4912SPeter Maydell break; 804f6d1d9b4SPeter Maydell case GITS_CMD_MOVALL: 805b6f96009SPeter Maydell result = process_movall(s, cmdpkt); 806f6d1d9b4SPeter Maydell break; 8077eca39e0SShashi Mallela default: 8087eca39e0SShashi Mallela break; 8097eca39e0SShashi Mallela } 810ef011555SPeter Maydell if (result == CMD_CONTINUE) { 8117eca39e0SShashi Mallela rd_offset++; 81280dcd37fSPeter Maydell rd_offset %= s->cq.num_entries; 8137eca39e0SShashi Mallela s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, OFFSET, rd_offset); 8147eca39e0SShashi Mallela } else { 815ef011555SPeter Maydell /* CMD_STALL */ 8167eca39e0SShashi Mallela s->creadr = FIELD_DP64(s->creadr, GITS_CREADR, STALLED, 1); 8177eca39e0SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 818ef011555SPeter Maydell "%s: 0x%x cmd processing failed, stalling\n", 819ef011555SPeter Maydell __func__, cmd); 8207eca39e0SShashi Mallela break; 8217eca39e0SShashi Mallela } 8227eca39e0SShashi Mallela } 8237eca39e0SShashi Mallela } 8247eca39e0SShashi Mallela 8251b08e436SShashi Mallela /* 8261b08e436SShashi Mallela * This function extracts the ITS Device and Collection table specific 8271b08e436SShashi Mallela * parameters (like base_addr, size etc) from GITS_BASER register. 8281b08e436SShashi Mallela * It is called during ITS enable and also during post_load migration 8291b08e436SShashi Mallela */ 8301b08e436SShashi Mallela static void extract_table_params(GICv3ITSState *s) 8311b08e436SShashi Mallela { 8321b08e436SShashi Mallela uint16_t num_pages = 0; 8331b08e436SShashi Mallela uint8_t page_sz_type; 8341b08e436SShashi Mallela uint8_t type; 8351b08e436SShashi Mallela uint32_t page_sz = 0; 8361b08e436SShashi Mallela uint64_t value; 8371b08e436SShashi Mallela 8381b08e436SShashi Mallela for (int i = 0; i < 8; i++) { 839e5487a41SPeter Maydell TableDesc *td; 840e5487a41SPeter Maydell int idbits; 841e5487a41SPeter Maydell 8421b08e436SShashi Mallela value = s->baser[i]; 8431b08e436SShashi Mallela 8441b08e436SShashi Mallela if (!value) { 8451b08e436SShashi Mallela continue; 8461b08e436SShashi Mallela } 8471b08e436SShashi Mallela 8481b08e436SShashi Mallela page_sz_type = FIELD_EX64(value, GITS_BASER, PAGESIZE); 8491b08e436SShashi Mallela 8501b08e436SShashi Mallela switch (page_sz_type) { 8511b08e436SShashi Mallela case 0: 8521b08e436SShashi Mallela page_sz = GITS_PAGE_SIZE_4K; 8531b08e436SShashi Mallela break; 8541b08e436SShashi Mallela 8551b08e436SShashi Mallela case 1: 8561b08e436SShashi Mallela page_sz = GITS_PAGE_SIZE_16K; 8571b08e436SShashi Mallela break; 8581b08e436SShashi Mallela 8591b08e436SShashi Mallela case 2: 8601b08e436SShashi Mallela case 3: 8611b08e436SShashi Mallela page_sz = GITS_PAGE_SIZE_64K; 8621b08e436SShashi Mallela break; 8631b08e436SShashi Mallela 8641b08e436SShashi Mallela default: 8651b08e436SShashi Mallela g_assert_not_reached(); 8661b08e436SShashi Mallela } 8671b08e436SShashi Mallela 8681b08e436SShashi Mallela num_pages = FIELD_EX64(value, GITS_BASER, SIZE) + 1; 8691b08e436SShashi Mallela 8701b08e436SShashi Mallela type = FIELD_EX64(value, GITS_BASER, TYPE); 8711b08e436SShashi Mallela 8721b08e436SShashi Mallela switch (type) { 8731b08e436SShashi Mallela case GITS_BASER_TYPE_DEVICE: 874e5487a41SPeter Maydell td = &s->dt; 875e5487a41SPeter Maydell idbits = FIELD_EX64(s->typer, GITS_TYPER, DEVBITS) + 1; 87662df780eSPeter Maydell break; 8771b08e436SShashi Mallela case GITS_BASER_TYPE_COLLECTION: 878e5487a41SPeter Maydell td = &s->ct; 8791b08e436SShashi Mallela if (FIELD_EX64(s->typer, GITS_TYPER, CIL)) { 880e5487a41SPeter Maydell idbits = FIELD_EX64(s->typer, GITS_TYPER, CIDBITS) + 1; 8811b08e436SShashi Mallela } else { 8821b08e436SShashi Mallela /* 16-bit CollectionId supported when CIL == 0 */ 883e5487a41SPeter Maydell idbits = 16; 8841b08e436SShashi Mallela } 8851b08e436SShashi Mallela break; 8861b08e436SShashi Mallela default: 887e5487a41SPeter Maydell /* 888e5487a41SPeter Maydell * GITS_BASER<n>.TYPE is read-only, so GITS_BASER_RO_MASK 889e5487a41SPeter Maydell * ensures we will only see type values corresponding to 890e5487a41SPeter Maydell * the values set up in gicv3_its_reset(). 891e5487a41SPeter Maydell */ 892e5487a41SPeter Maydell g_assert_not_reached(); 8931b08e436SShashi Mallela } 894e5487a41SPeter Maydell 895e5487a41SPeter Maydell memset(td, 0, sizeof(*td)); 896e5487a41SPeter Maydell td->valid = FIELD_EX64(value, GITS_BASER, VALID); 897e5487a41SPeter Maydell /* 898e5487a41SPeter Maydell * If GITS_BASER<n>.Valid is 0 for any <n> then we will not process 899e5487a41SPeter Maydell * interrupts. (GITS_TYPER.HCC is 0 for this implementation, so we 900e5487a41SPeter Maydell * do not have a special case where the GITS_BASER<n>.Valid bit is 0 901e5487a41SPeter Maydell * for the register corresponding to the Collection table but we 902e5487a41SPeter Maydell * still have to process interrupts using non-memory-backed 903e5487a41SPeter Maydell * Collection table entries.) 904e5487a41SPeter Maydell */ 905e5487a41SPeter Maydell if (!td->valid) { 906e5487a41SPeter Maydell continue; 907e5487a41SPeter Maydell } 908e5487a41SPeter Maydell td->page_sz = page_sz; 909e5487a41SPeter Maydell td->indirect = FIELD_EX64(value, GITS_BASER, INDIRECT); 9109ae85431SPeter Maydell td->entry_sz = FIELD_EX64(value, GITS_BASER, ENTRYSIZE) + 1; 911e5487a41SPeter Maydell td->base_addr = baser_base_addr(value, page_sz); 912e5487a41SPeter Maydell if (!td->indirect) { 91380dcd37fSPeter Maydell td->num_entries = (num_pages * page_sz) / td->entry_sz; 914e5487a41SPeter Maydell } else { 91580dcd37fSPeter Maydell td->num_entries = (((num_pages * page_sz) / 916e5487a41SPeter Maydell L1TABLE_ENTRY_SIZE) * 917e5487a41SPeter Maydell (page_sz / td->entry_sz)); 918e5487a41SPeter Maydell } 9198b8bb014SPeter Maydell td->num_entries = MIN(td->num_entries, 1ULL << idbits); 9201b08e436SShashi Mallela } 9211b08e436SShashi Mallela } 9221b08e436SShashi Mallela 9231b08e436SShashi Mallela static void extract_cmdq_params(GICv3ITSState *s) 9241b08e436SShashi Mallela { 9251b08e436SShashi Mallela uint16_t num_pages = 0; 9261b08e436SShashi Mallela uint64_t value = s->cbaser; 9271b08e436SShashi Mallela 9281b08e436SShashi Mallela num_pages = FIELD_EX64(value, GITS_CBASER, SIZE) + 1; 9291b08e436SShashi Mallela 9301b08e436SShashi Mallela memset(&s->cq, 0 , sizeof(s->cq)); 9311b08e436SShashi Mallela s->cq.valid = FIELD_EX64(value, GITS_CBASER, VALID); 9321b08e436SShashi Mallela 9331b08e436SShashi Mallela if (s->cq.valid) { 93480dcd37fSPeter Maydell s->cq.num_entries = (num_pages * GITS_PAGE_SIZE_4K) / 9351b08e436SShashi Mallela GITS_CMDQ_ENTRY_SIZE; 9361b08e436SShashi Mallela s->cq.base_addr = FIELD_EX64(value, GITS_CBASER, PHYADDR); 9371b08e436SShashi Mallela s->cq.base_addr <<= R_GITS_CBASER_PHYADDR_SHIFT; 9381b08e436SShashi Mallela } 9391b08e436SShashi Mallela } 9401b08e436SShashi Mallela 9417e062b98SPeter Maydell static MemTxResult gicv3_its_translation_read(void *opaque, hwaddr offset, 9427e062b98SPeter Maydell uint64_t *data, unsigned size, 9437e062b98SPeter Maydell MemTxAttrs attrs) 9447e062b98SPeter Maydell { 9457e062b98SPeter Maydell /* 9467e062b98SPeter Maydell * GITS_TRANSLATER is write-only, and all other addresses 9477e062b98SPeter Maydell * in the interrupt translation space frame are RES0. 9487e062b98SPeter Maydell */ 9497e062b98SPeter Maydell *data = 0; 9507e062b98SPeter Maydell return MEMTX_OK; 9517e062b98SPeter Maydell } 9527e062b98SPeter Maydell 95318f6290aSShashi Mallela static MemTxResult gicv3_its_translation_write(void *opaque, hwaddr offset, 95418f6290aSShashi Mallela uint64_t data, unsigned size, 95518f6290aSShashi Mallela MemTxAttrs attrs) 95618f6290aSShashi Mallela { 957c694cb4cSShashi Mallela GICv3ITSState *s = (GICv3ITSState *)opaque; 958c694cb4cSShashi Mallela bool result = true; 959c694cb4cSShashi Mallela 960195209d3SPeter Maydell trace_gicv3_its_translation_write(offset, data, size, attrs.requester_id); 961195209d3SPeter Maydell 962c694cb4cSShashi Mallela switch (offset) { 963c694cb4cSShashi Mallela case GITS_TRANSLATER: 9648d2d6dd9SPeter Maydell if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) { 965b6f96009SPeter Maydell result = do_process_its_cmd(s, attrs.requester_id, data, NONE); 966c694cb4cSShashi Mallela } 967c694cb4cSShashi Mallela break; 968c694cb4cSShashi Mallela default: 969c694cb4cSShashi Mallela break; 970c694cb4cSShashi Mallela } 971c694cb4cSShashi Mallela 972c694cb4cSShashi Mallela if (result) { 97318f6290aSShashi Mallela return MEMTX_OK; 974c694cb4cSShashi Mallela } else { 975c694cb4cSShashi Mallela return MEMTX_ERROR; 976c694cb4cSShashi Mallela } 97718f6290aSShashi Mallela } 97818f6290aSShashi Mallela 97918f6290aSShashi Mallela static bool its_writel(GICv3ITSState *s, hwaddr offset, 98018f6290aSShashi Mallela uint64_t value, MemTxAttrs attrs) 98118f6290aSShashi Mallela { 98218f6290aSShashi Mallela bool result = true; 9831b08e436SShashi Mallela int index; 98418f6290aSShashi Mallela 9851b08e436SShashi Mallela switch (offset) { 9861b08e436SShashi Mallela case GITS_CTLR: 9872f459cd1SShashi Mallela if (value & R_GITS_CTLR_ENABLED_MASK) { 9888d2d6dd9SPeter Maydell s->ctlr |= R_GITS_CTLR_ENABLED_MASK; 9891b08e436SShashi Mallela extract_table_params(s); 9901b08e436SShashi Mallela extract_cmdq_params(s); 9917eca39e0SShashi Mallela process_cmdq(s); 9922f459cd1SShashi Mallela } else { 9938d2d6dd9SPeter Maydell s->ctlr &= ~R_GITS_CTLR_ENABLED_MASK; 9941b08e436SShashi Mallela } 9951b08e436SShashi Mallela break; 9961b08e436SShashi Mallela case GITS_CBASER: 9971b08e436SShashi Mallela /* 9981b08e436SShashi Mallela * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is 9991b08e436SShashi Mallela * already enabled 10001b08e436SShashi Mallela */ 10018d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 10021b08e436SShashi Mallela s->cbaser = deposit64(s->cbaser, 0, 32, value); 10031b08e436SShashi Mallela s->creadr = 0; 10041b08e436SShashi Mallela } 10051b08e436SShashi Mallela break; 10061b08e436SShashi Mallela case GITS_CBASER + 4: 10071b08e436SShashi Mallela /* 10081b08e436SShashi Mallela * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is 10091b08e436SShashi Mallela * already enabled 10101b08e436SShashi Mallela */ 10118d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 10121b08e436SShashi Mallela s->cbaser = deposit64(s->cbaser, 32, 32, value); 10131b08e436SShashi Mallela s->creadr = 0; 10141b08e436SShashi Mallela } 10151b08e436SShashi Mallela break; 10161b08e436SShashi Mallela case GITS_CWRITER: 10171b08e436SShashi Mallela s->cwriter = deposit64(s->cwriter, 0, 32, 10181b08e436SShashi Mallela (value & ~R_GITS_CWRITER_RETRY_MASK)); 10197eca39e0SShashi Mallela if (s->cwriter != s->creadr) { 10207eca39e0SShashi Mallela process_cmdq(s); 10217eca39e0SShashi Mallela } 10221b08e436SShashi Mallela break; 10231b08e436SShashi Mallela case GITS_CWRITER + 4: 10241b08e436SShashi Mallela s->cwriter = deposit64(s->cwriter, 32, 32, value); 10251b08e436SShashi Mallela break; 10261b08e436SShashi Mallela case GITS_CREADR: 10271b08e436SShashi Mallela if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) { 10281b08e436SShashi Mallela s->creadr = deposit64(s->creadr, 0, 32, 10291b08e436SShashi Mallela (value & ~R_GITS_CREADR_STALLED_MASK)); 10301b08e436SShashi Mallela } else { 10311b08e436SShashi Mallela /* RO register, ignore the write */ 10321b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 10331b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 10341b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 10351b08e436SShashi Mallela } 10361b08e436SShashi Mallela break; 10371b08e436SShashi Mallela case GITS_CREADR + 4: 10381b08e436SShashi Mallela if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) { 10391b08e436SShashi Mallela s->creadr = deposit64(s->creadr, 32, 32, value); 10401b08e436SShashi Mallela } else { 10411b08e436SShashi Mallela /* RO register, ignore the write */ 10421b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 10431b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 10441b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 10451b08e436SShashi Mallela } 10461b08e436SShashi Mallela break; 10471b08e436SShashi Mallela case GITS_BASER ... GITS_BASER + 0x3f: 10481b08e436SShashi Mallela /* 10491b08e436SShashi Mallela * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is 10501b08e436SShashi Mallela * already enabled 10511b08e436SShashi Mallela */ 10528d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 10531b08e436SShashi Mallela index = (offset - GITS_BASER) / 8; 10541b08e436SShashi Mallela 10550ffe88e6SPeter Maydell if (s->baser[index] == 0) { 10560ffe88e6SPeter Maydell /* Unimplemented GITS_BASERn: RAZ/WI */ 10570ffe88e6SPeter Maydell break; 10580ffe88e6SPeter Maydell } 10591b08e436SShashi Mallela if (offset & 7) { 10601b08e436SShashi Mallela value <<= 32; 10611b08e436SShashi Mallela value &= ~GITS_BASER_RO_MASK; 10621b08e436SShashi Mallela s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(0, 32); 10631b08e436SShashi Mallela s->baser[index] |= value; 10641b08e436SShashi Mallela } else { 10651b08e436SShashi Mallela value &= ~GITS_BASER_RO_MASK; 10661b08e436SShashi Mallela s->baser[index] &= GITS_BASER_RO_MASK | MAKE_64BIT_MASK(32, 32); 10671b08e436SShashi Mallela s->baser[index] |= value; 10681b08e436SShashi Mallela } 10691b08e436SShashi Mallela } 10701b08e436SShashi Mallela break; 10711b08e436SShashi Mallela case GITS_IIDR: 10721b08e436SShashi Mallela case GITS_IDREGS ... GITS_IDREGS + 0x2f: 10731b08e436SShashi Mallela /* RO registers, ignore the write */ 10741b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 10751b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 10761b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 10771b08e436SShashi Mallela break; 10781b08e436SShashi Mallela default: 10791b08e436SShashi Mallela result = false; 10801b08e436SShashi Mallela break; 10811b08e436SShashi Mallela } 108218f6290aSShashi Mallela return result; 108318f6290aSShashi Mallela } 108418f6290aSShashi Mallela 108518f6290aSShashi Mallela static bool its_readl(GICv3ITSState *s, hwaddr offset, 108618f6290aSShashi Mallela uint64_t *data, MemTxAttrs attrs) 108718f6290aSShashi Mallela { 108818f6290aSShashi Mallela bool result = true; 10891b08e436SShashi Mallela int index; 109018f6290aSShashi Mallela 10911b08e436SShashi Mallela switch (offset) { 10921b08e436SShashi Mallela case GITS_CTLR: 10931b08e436SShashi Mallela *data = s->ctlr; 10941b08e436SShashi Mallela break; 10951b08e436SShashi Mallela case GITS_IIDR: 10961b08e436SShashi Mallela *data = gicv3_iidr(); 10971b08e436SShashi Mallela break; 10981b08e436SShashi Mallela case GITS_IDREGS ... GITS_IDREGS + 0x2f: 10991b08e436SShashi Mallela /* ID registers */ 11001b08e436SShashi Mallela *data = gicv3_idreg(offset - GITS_IDREGS); 11011b08e436SShashi Mallela break; 11021b08e436SShashi Mallela case GITS_TYPER: 11031b08e436SShashi Mallela *data = extract64(s->typer, 0, 32); 11041b08e436SShashi Mallela break; 11051b08e436SShashi Mallela case GITS_TYPER + 4: 11061b08e436SShashi Mallela *data = extract64(s->typer, 32, 32); 11071b08e436SShashi Mallela break; 11081b08e436SShashi Mallela case GITS_CBASER: 11091b08e436SShashi Mallela *data = extract64(s->cbaser, 0, 32); 11101b08e436SShashi Mallela break; 11111b08e436SShashi Mallela case GITS_CBASER + 4: 11121b08e436SShashi Mallela *data = extract64(s->cbaser, 32, 32); 11131b08e436SShashi Mallela break; 11141b08e436SShashi Mallela case GITS_CREADR: 11151b08e436SShashi Mallela *data = extract64(s->creadr, 0, 32); 11161b08e436SShashi Mallela break; 11171b08e436SShashi Mallela case GITS_CREADR + 4: 11181b08e436SShashi Mallela *data = extract64(s->creadr, 32, 32); 11191b08e436SShashi Mallela break; 11201b08e436SShashi Mallela case GITS_CWRITER: 11211b08e436SShashi Mallela *data = extract64(s->cwriter, 0, 32); 11221b08e436SShashi Mallela break; 11231b08e436SShashi Mallela case GITS_CWRITER + 4: 11241b08e436SShashi Mallela *data = extract64(s->cwriter, 32, 32); 11251b08e436SShashi Mallela break; 11261b08e436SShashi Mallela case GITS_BASER ... GITS_BASER + 0x3f: 11271b08e436SShashi Mallela index = (offset - GITS_BASER) / 8; 11281b08e436SShashi Mallela if (offset & 7) { 11291b08e436SShashi Mallela *data = extract64(s->baser[index], 32, 32); 11301b08e436SShashi Mallela } else { 11311b08e436SShashi Mallela *data = extract64(s->baser[index], 0, 32); 11321b08e436SShashi Mallela } 11331b08e436SShashi Mallela break; 11341b08e436SShashi Mallela default: 11351b08e436SShashi Mallela result = false; 11361b08e436SShashi Mallela break; 11371b08e436SShashi Mallela } 113818f6290aSShashi Mallela return result; 113918f6290aSShashi Mallela } 114018f6290aSShashi Mallela 114118f6290aSShashi Mallela static bool its_writell(GICv3ITSState *s, hwaddr offset, 114218f6290aSShashi Mallela uint64_t value, MemTxAttrs attrs) 114318f6290aSShashi Mallela { 114418f6290aSShashi Mallela bool result = true; 11451b08e436SShashi Mallela int index; 114618f6290aSShashi Mallela 11471b08e436SShashi Mallela switch (offset) { 11481b08e436SShashi Mallela case GITS_BASER ... GITS_BASER + 0x3f: 11491b08e436SShashi Mallela /* 11501b08e436SShashi Mallela * IMPDEF choice:- GITS_BASERn register becomes RO if ITS is 11511b08e436SShashi Mallela * already enabled 11521b08e436SShashi Mallela */ 11538d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 11541b08e436SShashi Mallela index = (offset - GITS_BASER) / 8; 11550ffe88e6SPeter Maydell if (s->baser[index] == 0) { 11560ffe88e6SPeter Maydell /* Unimplemented GITS_BASERn: RAZ/WI */ 11570ffe88e6SPeter Maydell break; 11580ffe88e6SPeter Maydell } 11591b08e436SShashi Mallela s->baser[index] &= GITS_BASER_RO_MASK; 11601b08e436SShashi Mallela s->baser[index] |= (value & ~GITS_BASER_RO_MASK); 11611b08e436SShashi Mallela } 11621b08e436SShashi Mallela break; 11631b08e436SShashi Mallela case GITS_CBASER: 11641b08e436SShashi Mallela /* 11651b08e436SShashi Mallela * IMPDEF choice:- GITS_CBASER register becomes RO if ITS is 11661b08e436SShashi Mallela * already enabled 11671b08e436SShashi Mallela */ 11688d2d6dd9SPeter Maydell if (!(s->ctlr & R_GITS_CTLR_ENABLED_MASK)) { 11691b08e436SShashi Mallela s->cbaser = value; 11701b08e436SShashi Mallela s->creadr = 0; 11711b08e436SShashi Mallela } 11721b08e436SShashi Mallela break; 11731b08e436SShashi Mallela case GITS_CWRITER: 11741b08e436SShashi Mallela s->cwriter = value & ~R_GITS_CWRITER_RETRY_MASK; 11757eca39e0SShashi Mallela if (s->cwriter != s->creadr) { 11767eca39e0SShashi Mallela process_cmdq(s); 11777eca39e0SShashi Mallela } 11781b08e436SShashi Mallela break; 11791b08e436SShashi Mallela case GITS_CREADR: 11801b08e436SShashi Mallela if (s->gicv3->gicd_ctlr & GICD_CTLR_DS) { 11811b08e436SShashi Mallela s->creadr = value & ~R_GITS_CREADR_STALLED_MASK; 11821b08e436SShashi Mallela } else { 11831b08e436SShashi Mallela /* RO register, ignore the write */ 11841b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 11851b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 11861b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 11871b08e436SShashi Mallela } 11881b08e436SShashi Mallela break; 11891b08e436SShashi Mallela case GITS_TYPER: 11901b08e436SShashi Mallela /* RO registers, ignore the write */ 11911b08e436SShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 11921b08e436SShashi Mallela "%s: invalid guest write to RO register at offset " 11931b08e436SShashi Mallela TARGET_FMT_plx "\n", __func__, offset); 11941b08e436SShashi Mallela break; 11951b08e436SShashi Mallela default: 11961b08e436SShashi Mallela result = false; 11971b08e436SShashi Mallela break; 11981b08e436SShashi Mallela } 119918f6290aSShashi Mallela return result; 120018f6290aSShashi Mallela } 120118f6290aSShashi Mallela 120218f6290aSShashi Mallela static bool its_readll(GICv3ITSState *s, hwaddr offset, 120318f6290aSShashi Mallela uint64_t *data, MemTxAttrs attrs) 120418f6290aSShashi Mallela { 120518f6290aSShashi Mallela bool result = true; 12061b08e436SShashi Mallela int index; 120718f6290aSShashi Mallela 12081b08e436SShashi Mallela switch (offset) { 12091b08e436SShashi Mallela case GITS_TYPER: 12101b08e436SShashi Mallela *data = s->typer; 12111b08e436SShashi Mallela break; 12121b08e436SShashi Mallela case GITS_BASER ... GITS_BASER + 0x3f: 12131b08e436SShashi Mallela index = (offset - GITS_BASER) / 8; 12141b08e436SShashi Mallela *data = s->baser[index]; 12151b08e436SShashi Mallela break; 12161b08e436SShashi Mallela case GITS_CBASER: 12171b08e436SShashi Mallela *data = s->cbaser; 12181b08e436SShashi Mallela break; 12191b08e436SShashi Mallela case GITS_CREADR: 12201b08e436SShashi Mallela *data = s->creadr; 12211b08e436SShashi Mallela break; 12221b08e436SShashi Mallela case GITS_CWRITER: 12231b08e436SShashi Mallela *data = s->cwriter; 12241b08e436SShashi Mallela break; 12251b08e436SShashi Mallela default: 12261b08e436SShashi Mallela result = false; 12271b08e436SShashi Mallela break; 12281b08e436SShashi Mallela } 122918f6290aSShashi Mallela return result; 123018f6290aSShashi Mallela } 123118f6290aSShashi Mallela 123218f6290aSShashi Mallela static MemTxResult gicv3_its_read(void *opaque, hwaddr offset, uint64_t *data, 123318f6290aSShashi Mallela unsigned size, MemTxAttrs attrs) 123418f6290aSShashi Mallela { 123518f6290aSShashi Mallela GICv3ITSState *s = (GICv3ITSState *)opaque; 123618f6290aSShashi Mallela bool result; 123718f6290aSShashi Mallela 123818f6290aSShashi Mallela switch (size) { 123918f6290aSShashi Mallela case 4: 124018f6290aSShashi Mallela result = its_readl(s, offset, data, attrs); 124118f6290aSShashi Mallela break; 124218f6290aSShashi Mallela case 8: 124318f6290aSShashi Mallela result = its_readll(s, offset, data, attrs); 124418f6290aSShashi Mallela break; 124518f6290aSShashi Mallela default: 124618f6290aSShashi Mallela result = false; 124718f6290aSShashi Mallela break; 124818f6290aSShashi Mallela } 124918f6290aSShashi Mallela 125018f6290aSShashi Mallela if (!result) { 125118f6290aSShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 125218f6290aSShashi Mallela "%s: invalid guest read at offset " TARGET_FMT_plx 125318f6290aSShashi Mallela "size %u\n", __func__, offset, size); 1254195209d3SPeter Maydell trace_gicv3_its_badread(offset, size); 125518f6290aSShashi Mallela /* 125618f6290aSShashi Mallela * The spec requires that reserved registers are RAZ/WI; 125718f6290aSShashi Mallela * so use false returns from leaf functions as a way to 125818f6290aSShashi Mallela * trigger the guest-error logging but don't return it to 125918f6290aSShashi Mallela * the caller, or we'll cause a spurious guest data abort. 126018f6290aSShashi Mallela */ 126118f6290aSShashi Mallela *data = 0; 1262195209d3SPeter Maydell } else { 1263195209d3SPeter Maydell trace_gicv3_its_read(offset, *data, size); 126418f6290aSShashi Mallela } 126518f6290aSShashi Mallela return MEMTX_OK; 126618f6290aSShashi Mallela } 126718f6290aSShashi Mallela 126818f6290aSShashi Mallela static MemTxResult gicv3_its_write(void *opaque, hwaddr offset, uint64_t data, 126918f6290aSShashi Mallela unsigned size, MemTxAttrs attrs) 127018f6290aSShashi Mallela { 127118f6290aSShashi Mallela GICv3ITSState *s = (GICv3ITSState *)opaque; 127218f6290aSShashi Mallela bool result; 127318f6290aSShashi Mallela 127418f6290aSShashi Mallela switch (size) { 127518f6290aSShashi Mallela case 4: 127618f6290aSShashi Mallela result = its_writel(s, offset, data, attrs); 127718f6290aSShashi Mallela break; 127818f6290aSShashi Mallela case 8: 127918f6290aSShashi Mallela result = its_writell(s, offset, data, attrs); 128018f6290aSShashi Mallela break; 128118f6290aSShashi Mallela default: 128218f6290aSShashi Mallela result = false; 128318f6290aSShashi Mallela break; 128418f6290aSShashi Mallela } 128518f6290aSShashi Mallela 128618f6290aSShashi Mallela if (!result) { 128718f6290aSShashi Mallela qemu_log_mask(LOG_GUEST_ERROR, 128818f6290aSShashi Mallela "%s: invalid guest write at offset " TARGET_FMT_plx 128918f6290aSShashi Mallela "size %u\n", __func__, offset, size); 1290195209d3SPeter Maydell trace_gicv3_its_badwrite(offset, data, size); 129118f6290aSShashi Mallela /* 129218f6290aSShashi Mallela * The spec requires that reserved registers are RAZ/WI; 129318f6290aSShashi Mallela * so use false returns from leaf functions as a way to 129418f6290aSShashi Mallela * trigger the guest-error logging but don't return it to 129518f6290aSShashi Mallela * the caller, or we'll cause a spurious guest data abort. 129618f6290aSShashi Mallela */ 1297195209d3SPeter Maydell } else { 1298195209d3SPeter Maydell trace_gicv3_its_write(offset, data, size); 129918f6290aSShashi Mallela } 130018f6290aSShashi Mallela return MEMTX_OK; 130118f6290aSShashi Mallela } 130218f6290aSShashi Mallela 130318f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_control_ops = { 130418f6290aSShashi Mallela .read_with_attrs = gicv3_its_read, 130518f6290aSShashi Mallela .write_with_attrs = gicv3_its_write, 130618f6290aSShashi Mallela .valid.min_access_size = 4, 130718f6290aSShashi Mallela .valid.max_access_size = 8, 130818f6290aSShashi Mallela .impl.min_access_size = 4, 130918f6290aSShashi Mallela .impl.max_access_size = 8, 131018f6290aSShashi Mallela .endianness = DEVICE_NATIVE_ENDIAN, 131118f6290aSShashi Mallela }; 131218f6290aSShashi Mallela 131318f6290aSShashi Mallela static const MemoryRegionOps gicv3_its_translation_ops = { 13147e062b98SPeter Maydell .read_with_attrs = gicv3_its_translation_read, 131518f6290aSShashi Mallela .write_with_attrs = gicv3_its_translation_write, 131618f6290aSShashi Mallela .valid.min_access_size = 2, 131718f6290aSShashi Mallela .valid.max_access_size = 4, 131818f6290aSShashi Mallela .impl.min_access_size = 2, 131918f6290aSShashi Mallela .impl.max_access_size = 4, 132018f6290aSShashi Mallela .endianness = DEVICE_NATIVE_ENDIAN, 132118f6290aSShashi Mallela }; 132218f6290aSShashi Mallela 132318f6290aSShashi Mallela static void gicv3_arm_its_realize(DeviceState *dev, Error **errp) 132418f6290aSShashi Mallela { 132518f6290aSShashi Mallela GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev); 132618f6290aSShashi Mallela int i; 132718f6290aSShashi Mallela 132818f6290aSShashi Mallela for (i = 0; i < s->gicv3->num_cpu; i++) { 132918f6290aSShashi Mallela if (!(s->gicv3->cpu[i].gicr_typer & GICR_TYPER_PLPIS)) { 133018f6290aSShashi Mallela error_setg(errp, "Physical LPI not supported by CPU %d", i); 133118f6290aSShashi Mallela return; 133218f6290aSShashi Mallela } 133318f6290aSShashi Mallela } 133418f6290aSShashi Mallela 133518f6290aSShashi Mallela gicv3_its_init_mmio(s, &gicv3_its_control_ops, &gicv3_its_translation_ops); 133618f6290aSShashi Mallela 133718f6290aSShashi Mallela /* set the ITS default features supported */ 1338764d6ba1SPeter Maydell s->typer = FIELD_DP64(s->typer, GITS_TYPER, PHYSICAL, 1); 133918f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, ITT_ENTRY_SIZE, 134018f6290aSShashi Mallela ITS_ITT_ENTRY_SIZE - 1); 134118f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, IDBITS, ITS_IDBITS); 134218f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, DEVBITS, ITS_DEVBITS); 134318f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIL, 1); 134418f6290aSShashi Mallela s->typer = FIELD_DP64(s->typer, GITS_TYPER, CIDBITS, ITS_CIDBITS); 134518f6290aSShashi Mallela } 134618f6290aSShashi Mallela 134718f6290aSShashi Mallela static void gicv3_its_reset(DeviceState *dev) 134818f6290aSShashi Mallela { 134918f6290aSShashi Mallela GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev); 135018f6290aSShashi Mallela GICv3ITSClass *c = ARM_GICV3_ITS_GET_CLASS(s); 135118f6290aSShashi Mallela 135218f6290aSShashi Mallela c->parent_reset(dev); 135318f6290aSShashi Mallela 135418f6290aSShashi Mallela /* Quiescent bit reset to 1 */ 135518f6290aSShashi Mallela s->ctlr = FIELD_DP32(s->ctlr, GITS_CTLR, QUIESCENT, 1); 135618f6290aSShashi Mallela 135718f6290aSShashi Mallela /* 135818f6290aSShashi Mallela * setting GITS_BASER0.Type = 0b001 (Device) 135918f6290aSShashi Mallela * GITS_BASER1.Type = 0b100 (Collection Table) 136018f6290aSShashi Mallela * GITS_BASER<n>.Type,where n = 3 to 7 are 0b00 (Unimplemented) 136118f6290aSShashi Mallela * GITS_BASER<0,1>.Page_Size = 64KB 136218f6290aSShashi Mallela * and default translation table entry size to 16 bytes 136318f6290aSShashi Mallela */ 136418f6290aSShashi Mallela s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, TYPE, 136518f6290aSShashi Mallela GITS_BASER_TYPE_DEVICE); 136618f6290aSShashi Mallela s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, PAGESIZE, 136718f6290aSShashi Mallela GITS_BASER_PAGESIZE_64K); 136818f6290aSShashi Mallela s->baser[0] = FIELD_DP64(s->baser[0], GITS_BASER, ENTRYSIZE, 136918f6290aSShashi Mallela GITS_DTE_SIZE - 1); 137018f6290aSShashi Mallela 137118f6290aSShashi Mallela s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, TYPE, 137218f6290aSShashi Mallela GITS_BASER_TYPE_COLLECTION); 137318f6290aSShashi Mallela s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, PAGESIZE, 137418f6290aSShashi Mallela GITS_BASER_PAGESIZE_64K); 137518f6290aSShashi Mallela s->baser[1] = FIELD_DP64(s->baser[1], GITS_BASER, ENTRYSIZE, 137618f6290aSShashi Mallela GITS_CTE_SIZE - 1); 137718f6290aSShashi Mallela } 137818f6290aSShashi Mallela 13791b08e436SShashi Mallela static void gicv3_its_post_load(GICv3ITSState *s) 13801b08e436SShashi Mallela { 13818d2d6dd9SPeter Maydell if (s->ctlr & R_GITS_CTLR_ENABLED_MASK) { 13821b08e436SShashi Mallela extract_table_params(s); 13831b08e436SShashi Mallela extract_cmdq_params(s); 13841b08e436SShashi Mallela } 13851b08e436SShashi Mallela } 13861b08e436SShashi Mallela 138718f6290aSShashi Mallela static Property gicv3_its_props[] = { 138818f6290aSShashi Mallela DEFINE_PROP_LINK("parent-gicv3", GICv3ITSState, gicv3, "arm-gicv3", 138918f6290aSShashi Mallela GICv3State *), 139018f6290aSShashi Mallela DEFINE_PROP_END_OF_LIST(), 139118f6290aSShashi Mallela }; 139218f6290aSShashi Mallela 139318f6290aSShashi Mallela static void gicv3_its_class_init(ObjectClass *klass, void *data) 139418f6290aSShashi Mallela { 139518f6290aSShashi Mallela DeviceClass *dc = DEVICE_CLASS(klass); 139618f6290aSShashi Mallela GICv3ITSClass *ic = ARM_GICV3_ITS_CLASS(klass); 13971b08e436SShashi Mallela GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass); 139818f6290aSShashi Mallela 139918f6290aSShashi Mallela dc->realize = gicv3_arm_its_realize; 140018f6290aSShashi Mallela device_class_set_props(dc, gicv3_its_props); 140118f6290aSShashi Mallela device_class_set_parent_reset(dc, gicv3_its_reset, &ic->parent_reset); 14021b08e436SShashi Mallela icc->post_load = gicv3_its_post_load; 140318f6290aSShashi Mallela } 140418f6290aSShashi Mallela 140518f6290aSShashi Mallela static const TypeInfo gicv3_its_info = { 140618f6290aSShashi Mallela .name = TYPE_ARM_GICV3_ITS, 140718f6290aSShashi Mallela .parent = TYPE_ARM_GICV3_ITS_COMMON, 140818f6290aSShashi Mallela .instance_size = sizeof(GICv3ITSState), 140918f6290aSShashi Mallela .class_init = gicv3_its_class_init, 141018f6290aSShashi Mallela .class_size = sizeof(GICv3ITSClass), 141118f6290aSShashi Mallela }; 141218f6290aSShashi Mallela 141318f6290aSShashi Mallela static void gicv3_its_register_types(void) 141418f6290aSShashi Mallela { 141518f6290aSShashi Mallela type_register_static(&gicv3_its_info); 141618f6290aSShashi Mallela } 141718f6290aSShashi Mallela 141818f6290aSShashi Mallela type_init(gicv3_its_register_types) 1419