110a83cb9SPrem Mallappa /* 210a83cb9SPrem Mallappa * Copyright (C) 2014-2016 Broadcom Corporation 310a83cb9SPrem Mallappa * Copyright (c) 2017 Red Hat, Inc. 410a83cb9SPrem Mallappa * Written by Prem Mallappa, Eric Auger 510a83cb9SPrem Mallappa * 610a83cb9SPrem Mallappa * This program is free software; you can redistribute it and/or modify 710a83cb9SPrem Mallappa * it under the terms of the GNU General Public License version 2 as 810a83cb9SPrem Mallappa * published by the Free Software Foundation. 910a83cb9SPrem Mallappa * 1010a83cb9SPrem Mallappa * This program is distributed in the hope that it will be useful, 1110a83cb9SPrem Mallappa * but WITHOUT ANY WARRANTY; without even the implied warranty of 1210a83cb9SPrem Mallappa * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1310a83cb9SPrem Mallappa * GNU General Public License for more details. 1410a83cb9SPrem Mallappa * 1510a83cb9SPrem Mallappa * You should have received a copy of the GNU General Public License along 1610a83cb9SPrem Mallappa * with this program; if not, see <http://www.gnu.org/licenses/>. 1710a83cb9SPrem Mallappa */ 1810a83cb9SPrem Mallappa 1910a83cb9SPrem Mallappa #include "qemu/osdep.h" 20744a790eSPhilippe Mathieu-Daudé #include "qemu/bitops.h" 2164552b6bSMarkus Armbruster #include "hw/irq.h" 2210a83cb9SPrem Mallappa #include "hw/sysbus.h" 23d6454270SMarkus Armbruster #include "migration/vmstate.h" 248cefcc3bSMostafa Saleh #include "hw/qdev-properties.h" 2510a83cb9SPrem Mallappa #include "hw/qdev-core.h" 2610a83cb9SPrem Mallappa #include "hw/pci/pci.h" 279122bea9SJia He #include "cpu.h" 2810a83cb9SPrem Mallappa #include "trace.h" 2910a83cb9SPrem Mallappa #include "qemu/log.h" 3010a83cb9SPrem Mallappa #include "qemu/error-report.h" 3110a83cb9SPrem Mallappa #include "qapi/error.h" 3210a83cb9SPrem Mallappa 3310a83cb9SPrem Mallappa #include "hw/arm/smmuv3.h" 3410a83cb9SPrem Mallappa #include "smmuv3-internal.h" 351194140bSEric Auger #include "smmu-internal.h" 3610a83cb9SPrem Mallappa 37f9131185SMostafa Saleh #define PTW_RECORD_FAULT(ptw_info, cfg) (((ptw_info).stage == SMMU_STAGE_1 && \ 38f9131185SMostafa Saleh (cfg)->record_faults) || \ 39f9131185SMostafa Saleh ((ptw_info).stage == SMMU_STAGE_2 && \ 40f9131185SMostafa Saleh (cfg)->s2cfg.record_faults)) 4121eb5b5cSMostafa Saleh 426a736033SEric Auger /** 436a736033SEric Auger * smmuv3_trigger_irq - pulse @irq if enabled and update 446a736033SEric Auger * GERROR register in case of GERROR interrupt 456a736033SEric Auger * 466a736033SEric Auger * @irq: irq type 476a736033SEric Auger * @gerror_mask: mask of gerrors to toggle (relevant if @irq is GERROR) 486a736033SEric Auger */ 49fae4be38SEric Auger static void smmuv3_trigger_irq(SMMUv3State *s, SMMUIrq irq, 50fae4be38SEric Auger uint32_t gerror_mask) 516a736033SEric Auger { 526a736033SEric Auger 536a736033SEric Auger bool pulse = false; 546a736033SEric Auger 556a736033SEric Auger switch (irq) { 566a736033SEric Auger case SMMU_IRQ_EVTQ: 576a736033SEric Auger pulse = smmuv3_eventq_irq_enabled(s); 586a736033SEric Auger break; 596a736033SEric Auger case SMMU_IRQ_PRIQ: 606a736033SEric Auger qemu_log_mask(LOG_UNIMP, "PRI not yet supported\n"); 616a736033SEric Auger break; 626a736033SEric Auger case SMMU_IRQ_CMD_SYNC: 636a736033SEric Auger pulse = true; 646a736033SEric Auger break; 656a736033SEric Auger case SMMU_IRQ_GERROR: 666a736033SEric Auger { 676a736033SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 686a736033SEric Auger uint32_t new_gerrors = ~pending & gerror_mask; 696a736033SEric Auger 706a736033SEric Auger if (!new_gerrors) { 716a736033SEric Auger /* only toggle non pending errors */ 726a736033SEric Auger return; 736a736033SEric Auger } 746a736033SEric Auger s->gerror ^= new_gerrors; 756a736033SEric Auger trace_smmuv3_write_gerror(new_gerrors, s->gerror); 766a736033SEric Auger 776a736033SEric Auger pulse = smmuv3_gerror_irq_enabled(s); 786a736033SEric Auger break; 796a736033SEric Auger } 806a736033SEric Auger } 816a736033SEric Auger if (pulse) { 826a736033SEric Auger trace_smmuv3_trigger_irq(irq); 836a736033SEric Auger qemu_irq_pulse(s->irq[irq]); 846a736033SEric Auger } 856a736033SEric Auger } 866a736033SEric Auger 87fae4be38SEric Auger static void smmuv3_write_gerrorn(SMMUv3State *s, uint32_t new_gerrorn) 886a736033SEric Auger { 896a736033SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 906a736033SEric Auger uint32_t toggled = s->gerrorn ^ new_gerrorn; 916a736033SEric Auger 926a736033SEric Auger if (toggled & ~pending) { 936a736033SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 946a736033SEric Auger "guest toggles non pending errors = 0x%x\n", 956a736033SEric Auger toggled & ~pending); 966a736033SEric Auger } 976a736033SEric Auger 986a736033SEric Auger /* 996a736033SEric Auger * We do not raise any error in case guest toggles bits corresponding 1006a736033SEric Auger * to not active IRQs (CONSTRAINED UNPREDICTABLE) 1016a736033SEric Auger */ 1026a736033SEric Auger s->gerrorn = new_gerrorn; 1036a736033SEric Auger 1046a736033SEric Auger trace_smmuv3_write_gerrorn(toggled & pending, s->gerrorn); 1056a736033SEric Auger } 1066a736033SEric Auger 107c6445544SPeter Maydell static inline MemTxResult queue_read(SMMUQueue *q, Cmd *cmd) 108dadd1a08SEric Auger { 109dadd1a08SEric Auger dma_addr_t addr = Q_CONS_ENTRY(q); 110c6445544SPeter Maydell MemTxResult ret; 111c6445544SPeter Maydell int i; 112dadd1a08SEric Auger 113c6445544SPeter Maydell ret = dma_memory_read(&address_space_memory, addr, cmd, sizeof(Cmd), 114ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 115c6445544SPeter Maydell if (ret != MEMTX_OK) { 116c6445544SPeter Maydell return ret; 117c6445544SPeter Maydell } 118c6445544SPeter Maydell for (i = 0; i < ARRAY_SIZE(cmd->word); i++) { 119c6445544SPeter Maydell le32_to_cpus(&cmd->word[i]); 120c6445544SPeter Maydell } 121c6445544SPeter Maydell return ret; 122dadd1a08SEric Auger } 123dadd1a08SEric Auger 124c6445544SPeter Maydell static MemTxResult queue_write(SMMUQueue *q, Evt *evt_in) 125dadd1a08SEric Auger { 126dadd1a08SEric Auger dma_addr_t addr = Q_PROD_ENTRY(q); 127dadd1a08SEric Auger MemTxResult ret; 128c6445544SPeter Maydell Evt evt = *evt_in; 129c6445544SPeter Maydell int i; 130dadd1a08SEric Auger 131c6445544SPeter Maydell for (i = 0; i < ARRAY_SIZE(evt.word); i++) { 132c6445544SPeter Maydell cpu_to_le32s(&evt.word[i]); 133c6445544SPeter Maydell } 134c6445544SPeter Maydell ret = dma_memory_write(&address_space_memory, addr, &evt, sizeof(Evt), 135ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 136dadd1a08SEric Auger if (ret != MEMTX_OK) { 137dadd1a08SEric Auger return ret; 138dadd1a08SEric Auger } 139dadd1a08SEric Auger 140dadd1a08SEric Auger queue_prod_incr(q); 141dadd1a08SEric Auger return MEMTX_OK; 142dadd1a08SEric Auger } 143dadd1a08SEric Auger 144bb981004SEric Auger static MemTxResult smmuv3_write_eventq(SMMUv3State *s, Evt *evt) 145dadd1a08SEric Auger { 146dadd1a08SEric Auger SMMUQueue *q = &s->eventq; 147bb981004SEric Auger MemTxResult r; 148bb981004SEric Auger 149bb981004SEric Auger if (!smmuv3_eventq_enabled(s)) { 150bb981004SEric Auger return MEMTX_ERROR; 151bb981004SEric Auger } 152bb981004SEric Auger 153bb981004SEric Auger if (smmuv3_q_full(q)) { 154bb981004SEric Auger return MEMTX_ERROR; 155bb981004SEric Auger } 156bb981004SEric Auger 157bb981004SEric Auger r = queue_write(q, evt); 158bb981004SEric Auger if (r != MEMTX_OK) { 159bb981004SEric Auger return r; 160bb981004SEric Auger } 161bb981004SEric Auger 1629f4d2a13SEric Auger if (!smmuv3_q_empty(q)) { 163bb981004SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_EVTQ, 0); 164bb981004SEric Auger } 165bb981004SEric Auger return MEMTX_OK; 166bb981004SEric Auger } 167bb981004SEric Auger 168bb981004SEric Auger void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info) 169bb981004SEric Auger { 17024af32e0SEric Auger Evt evt = {}; 171bb981004SEric Auger MemTxResult r; 172dadd1a08SEric Auger 173dadd1a08SEric Auger if (!smmuv3_eventq_enabled(s)) { 174dadd1a08SEric Auger return; 175dadd1a08SEric Auger } 176dadd1a08SEric Auger 177bb981004SEric Auger EVT_SET_TYPE(&evt, info->type); 178bb981004SEric Auger EVT_SET_SID(&evt, info->sid); 179bb981004SEric Auger 180bb981004SEric Auger switch (info->type) { 1819122bea9SJia He case SMMU_EVT_NONE: 182dadd1a08SEric Auger return; 183bb981004SEric Auger case SMMU_EVT_F_UUT: 184bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_uut.ssid); 185bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_uut.ssv); 186bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_uut.addr); 187bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_uut.rnw); 188bb981004SEric Auger EVT_SET_PNU(&evt, info->u.f_uut.pnu); 189bb981004SEric Auger EVT_SET_IND(&evt, info->u.f_uut.ind); 190bb981004SEric Auger break; 191bb981004SEric Auger case SMMU_EVT_C_BAD_STREAMID: 192bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_streamid.ssid); 193bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_streamid.ssv); 194bb981004SEric Auger break; 195bb981004SEric Auger case SMMU_EVT_F_STE_FETCH: 196bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_ste_fetch.ssid); 197bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_ste_fetch.ssv); 198b255cafbSSimon Veith EVT_SET_ADDR2(&evt, info->u.f_ste_fetch.addr); 199bb981004SEric Auger break; 200bb981004SEric Auger case SMMU_EVT_C_BAD_STE: 201bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_ste.ssid); 202bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_ste.ssv); 203bb981004SEric Auger break; 204bb981004SEric Auger case SMMU_EVT_F_STREAM_DISABLED: 205bb981004SEric Auger break; 206bb981004SEric Auger case SMMU_EVT_F_TRANS_FORBIDDEN: 207bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_transl_forbidden.addr); 208bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_transl_forbidden.rnw); 209bb981004SEric Auger break; 210bb981004SEric Auger case SMMU_EVT_C_BAD_SUBSTREAMID: 211bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_substream.ssid); 212bb981004SEric Auger break; 213bb981004SEric Auger case SMMU_EVT_F_CD_FETCH: 214bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_cd_fetch.ssid); 215bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_cd_fetch.ssv); 216bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_cd_fetch.addr); 217bb981004SEric Auger break; 218bb981004SEric Auger case SMMU_EVT_C_BAD_CD: 219bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_cd.ssid); 220bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_cd.ssv); 221bb981004SEric Auger break; 222bb981004SEric Auger case SMMU_EVT_F_WALK_EABT: 223bb981004SEric Auger case SMMU_EVT_F_TRANSLATION: 224bb981004SEric Auger case SMMU_EVT_F_ADDR_SIZE: 225bb981004SEric Auger case SMMU_EVT_F_ACCESS: 226bb981004SEric Auger case SMMU_EVT_F_PERMISSION: 227bb981004SEric Auger EVT_SET_STALL(&evt, info->u.f_walk_eabt.stall); 228bb981004SEric Auger EVT_SET_STAG(&evt, info->u.f_walk_eabt.stag); 229bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_walk_eabt.ssid); 230bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_walk_eabt.ssv); 231bb981004SEric Auger EVT_SET_S2(&evt, info->u.f_walk_eabt.s2); 232bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_walk_eabt.addr); 233bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_walk_eabt.rnw); 234bb981004SEric Auger EVT_SET_PNU(&evt, info->u.f_walk_eabt.pnu); 235bb981004SEric Auger EVT_SET_IND(&evt, info->u.f_walk_eabt.ind); 236bb981004SEric Auger EVT_SET_CLASS(&evt, info->u.f_walk_eabt.class); 237bb981004SEric Auger EVT_SET_ADDR2(&evt, info->u.f_walk_eabt.addr2); 238bb981004SEric Auger break; 239bb981004SEric Auger case SMMU_EVT_F_CFG_CONFLICT: 240bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_cfg_conflict.ssid); 241bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_cfg_conflict.ssv); 242bb981004SEric Auger break; 243bb981004SEric Auger /* rest is not implemented */ 244bb981004SEric Auger case SMMU_EVT_F_BAD_ATS_TREQ: 245bb981004SEric Auger case SMMU_EVT_F_TLB_CONFLICT: 246bb981004SEric Auger case SMMU_EVT_E_PAGE_REQ: 247bb981004SEric Auger default: 248bb981004SEric Auger g_assert_not_reached(); 249dadd1a08SEric Auger } 250dadd1a08SEric Auger 251bb981004SEric Auger trace_smmuv3_record_event(smmu_event_string(info->type), info->sid); 252bb981004SEric Auger r = smmuv3_write_eventq(s, &evt); 253bb981004SEric Auger if (r != MEMTX_OK) { 254bb981004SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_EVENTQ_ABT_ERR_MASK); 255dadd1a08SEric Auger } 256bb981004SEric Auger info->recorded = true; 257dadd1a08SEric Auger } 258dadd1a08SEric Auger 25910a83cb9SPrem Mallappa static void smmuv3_init_regs(SMMUv3State *s) 26010a83cb9SPrem Mallappa { 2618cefcc3bSMostafa Saleh /* Based on sys property, the stages supported in smmu will be advertised.*/ 2628cefcc3bSMostafa Saleh if (s->stage && !strcmp("2", s->stage)) { 2638cefcc3bSMostafa Saleh s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S2P, 1); 26458377c36SMostafa Saleh } else if (s->stage && !strcmp("nested", s->stage)) { 26558377c36SMostafa Saleh s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S1P, 1); 26658377c36SMostafa Saleh s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S2P, 1); 2678cefcc3bSMostafa Saleh } else { 2688cefcc3bSMostafa Saleh s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S1P, 1); 2698cefcc3bSMostafa Saleh } 2708cefcc3bSMostafa Saleh 27110a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTF, 2); /* AArch64 PTW only */ 27210a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, COHACC, 1); /* IO coherent */ 27310a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, ASID16, 1); /* 16-bit ASID */ 2748cefcc3bSMostafa Saleh s->idr[0] = FIELD_DP32(s->idr[0], IDR0, VMID16, 1); /* 16-bit VMID */ 27510a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTENDIAN, 2); /* little endian */ 27610a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STALL_MODEL, 1); /* No stall */ 27710a83cb9SPrem Mallappa /* terminated transaction will always be aborted/error returned */ 27810a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TERM_MODEL, 1); 27910a83cb9SPrem Mallappa /* 2-level stream table supported */ 28010a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STLEVEL, 1); 28110a83cb9SPrem Mallappa 28210a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, SIDSIZE, SMMU_IDR1_SIDSIZE); 28310a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, EVENTQS, SMMU_EVENTQS); 28410a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, CMDQS, SMMU_CMDQS); 28510a83cb9SPrem Mallappa 286e7c3b9d9SEric Auger s->idr[3] = FIELD_DP32(s->idr[3], IDR3, HAD, 1); 2874cdd146dSPeter Maydell if (FIELD_EX32(s->idr[0], IDR0, S2P)) { 2884cdd146dSPeter Maydell /* XNX is a stage-2-specific feature */ 2894cdd146dSPeter Maydell s->idr[3] = FIELD_DP32(s->idr[3], IDR3, XNX, 1); 2904cdd146dSPeter Maydell } 29127fd85d3SPeter Maydell s->idr[3] = FIELD_DP32(s->idr[3], IDR3, RIL, 1); 292f8e7163dSPeter Maydell s->idr[3] = FIELD_DP32(s->idr[3], IDR3, BBML, 2); 293e7c3b9d9SEric Auger 29427fd85d3SPeter Maydell s->idr[5] = FIELD_DP32(s->idr[5], IDR5, OAS, SMMU_IDR5_OAS); /* 44 bits */ 295bf559ee4SKunkun Jiang /* 4K, 16K and 64K granule support */ 29610a83cb9SPrem Mallappa s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN4K, 1); 297bf559ee4SKunkun Jiang s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN16K, 1); 29810a83cb9SPrem Mallappa s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN64K, 1); 29910a83cb9SPrem Mallappa 30010a83cb9SPrem Mallappa s->cmdq.base = deposit64(s->cmdq.base, 0, 5, SMMU_CMDQS); 30110a83cb9SPrem Mallappa s->cmdq.prod = 0; 30210a83cb9SPrem Mallappa s->cmdq.cons = 0; 30310a83cb9SPrem Mallappa s->cmdq.entry_size = sizeof(struct Cmd); 30410a83cb9SPrem Mallappa s->eventq.base = deposit64(s->eventq.base, 0, 5, SMMU_EVENTQS); 30510a83cb9SPrem Mallappa s->eventq.prod = 0; 30610a83cb9SPrem Mallappa s->eventq.cons = 0; 30710a83cb9SPrem Mallappa s->eventq.entry_size = sizeof(struct Evt); 30810a83cb9SPrem Mallappa 30910a83cb9SPrem Mallappa s->features = 0; 31010a83cb9SPrem Mallappa s->sid_split = 0; 311e7c3b9d9SEric Auger s->aidr = 0x1; 31243530095SEric Auger s->cr[0] = 0; 31343530095SEric Auger s->cr0ack = 0; 31443530095SEric Auger s->irq_ctrl = 0; 31543530095SEric Auger s->gerror = 0; 31643530095SEric Auger s->gerrorn = 0; 31743530095SEric Auger s->statusr = 0; 318c2ecb424SMostafa Saleh s->gbpa = SMMU_GBPA_RESET_VAL; 31910a83cb9SPrem Mallappa } 32010a83cb9SPrem Mallappa 3219bde7f06SEric Auger static int smmu_get_ste(SMMUv3State *s, dma_addr_t addr, STE *buf, 3229bde7f06SEric Auger SMMUEventInfo *event) 3239bde7f06SEric Auger { 324c6445544SPeter Maydell int ret, i; 3259bde7f06SEric Auger 3269bde7f06SEric Auger trace_smmuv3_get_ste(addr); 3279bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 328ba06fe8aSPhilippe Mathieu-Daudé ret = dma_memory_read(&address_space_memory, addr, buf, sizeof(*buf), 329ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 3309bde7f06SEric Auger if (ret != MEMTX_OK) { 3319bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 3329bde7f06SEric Auger "Cannot fetch pte at address=0x%"PRIx64"\n", addr); 3339bde7f06SEric Auger event->type = SMMU_EVT_F_STE_FETCH; 3349bde7f06SEric Auger event->u.f_ste_fetch.addr = addr; 3359bde7f06SEric Auger return -EINVAL; 3369bde7f06SEric Auger } 337c6445544SPeter Maydell for (i = 0; i < ARRAY_SIZE(buf->word); i++) { 338c6445544SPeter Maydell le32_to_cpus(&buf->word[i]); 339c6445544SPeter Maydell } 3409bde7f06SEric Auger return 0; 3419bde7f06SEric Auger 3429bde7f06SEric Auger } 3439bde7f06SEric Auger 3449dd6aa9bSMostafa Saleh static SMMUTranslationStatus smmuv3_do_translate(SMMUv3State *s, hwaddr addr, 3459dd6aa9bSMostafa Saleh SMMUTransCfg *cfg, 3469dd6aa9bSMostafa Saleh SMMUEventInfo *event, 3479dd6aa9bSMostafa Saleh IOMMUAccessFlags flag, 3489dd6aa9bSMostafa Saleh SMMUTLBEntry **out_entry, 3499dd6aa9bSMostafa Saleh SMMUTranslationClass class); 3509bde7f06SEric Auger /* @ssid > 0 not supported yet */ 3519dd6aa9bSMostafa Saleh static int smmu_get_cd(SMMUv3State *s, STE *ste, SMMUTransCfg *cfg, 3529dd6aa9bSMostafa Saleh uint32_t ssid, CD *buf, SMMUEventInfo *event) 3539bde7f06SEric Auger { 3549bde7f06SEric Auger dma_addr_t addr = STE_CTXPTR(ste); 355c6445544SPeter Maydell int ret, i; 3569dd6aa9bSMostafa Saleh SMMUTranslationStatus status; 3579dd6aa9bSMostafa Saleh SMMUTLBEntry *entry; 3589bde7f06SEric Auger 3599bde7f06SEric Auger trace_smmuv3_get_cd(addr); 3609dd6aa9bSMostafa Saleh 3619dd6aa9bSMostafa Saleh if (cfg->stage == SMMU_NESTED) { 3629dd6aa9bSMostafa Saleh status = smmuv3_do_translate(s, addr, cfg, event, 3639dd6aa9bSMostafa Saleh IOMMU_RO, &entry, SMMU_CLASS_CD); 3649dd6aa9bSMostafa Saleh 3659dd6aa9bSMostafa Saleh /* Same PTW faults are reported but with CLASS = CD. */ 3669dd6aa9bSMostafa Saleh if (status != SMMU_TRANS_SUCCESS) { 3679dd6aa9bSMostafa Saleh return -EINVAL; 3689dd6aa9bSMostafa Saleh } 3699dd6aa9bSMostafa Saleh 3709dd6aa9bSMostafa Saleh addr = CACHED_ENTRY_TO_ADDR(entry, addr); 3719dd6aa9bSMostafa Saleh } 3729dd6aa9bSMostafa Saleh 3739bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 374ba06fe8aSPhilippe Mathieu-Daudé ret = dma_memory_read(&address_space_memory, addr, buf, sizeof(*buf), 375ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 3769bde7f06SEric Auger if (ret != MEMTX_OK) { 3779bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 3789bde7f06SEric Auger "Cannot fetch pte at address=0x%"PRIx64"\n", addr); 3799bde7f06SEric Auger event->type = SMMU_EVT_F_CD_FETCH; 3809bde7f06SEric Auger event->u.f_ste_fetch.addr = addr; 3819bde7f06SEric Auger return -EINVAL; 3829bde7f06SEric Auger } 383c6445544SPeter Maydell for (i = 0; i < ARRAY_SIZE(buf->word); i++) { 384c6445544SPeter Maydell le32_to_cpus(&buf->word[i]); 385c6445544SPeter Maydell } 3869bde7f06SEric Auger return 0; 3879bde7f06SEric Auger } 3889bde7f06SEric Auger 38921eb5b5cSMostafa Saleh /* 39021eb5b5cSMostafa Saleh * Max valid value is 39 when SMMU_IDR3.STT == 0. 39121eb5b5cSMostafa Saleh * In architectures after SMMUv3.0: 39221eb5b5cSMostafa Saleh * - If STE.S2TG selects a 4KB or 16KB granule, the minimum valid value for this 39321eb5b5cSMostafa Saleh * field is MAX(16, 64-IAS) 39421eb5b5cSMostafa Saleh * - If STE.S2TG selects a 64KB granule, the minimum valid value for this field 39521eb5b5cSMostafa Saleh * is (64-IAS). 39621eb5b5cSMostafa Saleh * As we only support AA64, IAS = OAS. 39721eb5b5cSMostafa Saleh */ 39821eb5b5cSMostafa Saleh static bool s2t0sz_valid(SMMUTransCfg *cfg) 39921eb5b5cSMostafa Saleh { 40021eb5b5cSMostafa Saleh if (cfg->s2cfg.tsz > 39) { 40121eb5b5cSMostafa Saleh return false; 40221eb5b5cSMostafa Saleh } 40321eb5b5cSMostafa Saleh 40421eb5b5cSMostafa Saleh if (cfg->s2cfg.granule_sz == 16) { 4056783a184SMostafa Saleh return (cfg->s2cfg.tsz >= 64 - cfg->s2cfg.eff_ps); 40621eb5b5cSMostafa Saleh } 40721eb5b5cSMostafa Saleh 4086783a184SMostafa Saleh return (cfg->s2cfg.tsz >= MAX(64 - cfg->s2cfg.eff_ps, 16)); 40921eb5b5cSMostafa Saleh } 41021eb5b5cSMostafa Saleh 41121eb5b5cSMostafa Saleh /* 41221eb5b5cSMostafa Saleh * Return true if s2 page table config is valid. 41321eb5b5cSMostafa Saleh * This checks with the configured start level, ias_bits and granularity we can 41421eb5b5cSMostafa Saleh * have a valid page table as described in ARM ARM D8.2 Translation process. 41521eb5b5cSMostafa Saleh * The idea here is to see for the highest possible number of IPA bits, how 41621eb5b5cSMostafa Saleh * many concatenated tables we would need, if it is more than 16, then this is 41721eb5b5cSMostafa Saleh * not possible. 41821eb5b5cSMostafa Saleh */ 41921eb5b5cSMostafa Saleh static bool s2_pgtable_config_valid(uint8_t sl0, uint8_t t0sz, uint8_t gran) 42021eb5b5cSMostafa Saleh { 42121eb5b5cSMostafa Saleh int level = get_start_level(sl0, gran); 42221eb5b5cSMostafa Saleh uint64_t ipa_bits = 64 - t0sz; 42321eb5b5cSMostafa Saleh uint64_t max_ipa = (1ULL << ipa_bits) - 1; 42421eb5b5cSMostafa Saleh int nr_concat = pgd_concat_idx(level, gran, max_ipa) + 1; 42521eb5b5cSMostafa Saleh 42621eb5b5cSMostafa Saleh return nr_concat <= VMSA_MAX_S2_CONCAT; 42721eb5b5cSMostafa Saleh } 42821eb5b5cSMostafa Saleh 4296783a184SMostafa Saleh static int decode_ste_s2_cfg(SMMUv3State *s, SMMUTransCfg *cfg, 4306783a184SMostafa Saleh STE *ste) 43121eb5b5cSMostafa Saleh { 4326783a184SMostafa Saleh uint8_t oas = FIELD_EX32(s->idr[5], IDR5, OAS); 4336783a184SMostafa Saleh 43421eb5b5cSMostafa Saleh if (STE_S2AA64(ste) == 0x0) { 43521eb5b5cSMostafa Saleh qemu_log_mask(LOG_UNIMP, 43621eb5b5cSMostafa Saleh "SMMUv3 AArch32 tables not supported\n"); 43721eb5b5cSMostafa Saleh g_assert_not_reached(); 43821eb5b5cSMostafa Saleh } 43921eb5b5cSMostafa Saleh 44021eb5b5cSMostafa Saleh switch (STE_S2TG(ste)) { 44121eb5b5cSMostafa Saleh case 0x0: /* 4KB */ 44221eb5b5cSMostafa Saleh cfg->s2cfg.granule_sz = 12; 44321eb5b5cSMostafa Saleh break; 44421eb5b5cSMostafa Saleh case 0x1: /* 64KB */ 44521eb5b5cSMostafa Saleh cfg->s2cfg.granule_sz = 16; 44621eb5b5cSMostafa Saleh break; 44721eb5b5cSMostafa Saleh case 0x2: /* 16KB */ 44821eb5b5cSMostafa Saleh cfg->s2cfg.granule_sz = 14; 44921eb5b5cSMostafa Saleh break; 45021eb5b5cSMostafa Saleh default: 45121eb5b5cSMostafa Saleh qemu_log_mask(LOG_GUEST_ERROR, 45221eb5b5cSMostafa Saleh "SMMUv3 bad STE S2TG: %x\n", STE_S2TG(ste)); 45321eb5b5cSMostafa Saleh goto bad_ste; 45421eb5b5cSMostafa Saleh } 45521eb5b5cSMostafa Saleh 45621eb5b5cSMostafa Saleh cfg->s2cfg.vttb = STE_S2TTB(ste); 45721eb5b5cSMostafa Saleh 45821eb5b5cSMostafa Saleh cfg->s2cfg.sl0 = STE_S2SL0(ste); 45921eb5b5cSMostafa Saleh /* FEAT_TTST not supported. */ 46021eb5b5cSMostafa Saleh if (cfg->s2cfg.sl0 == 0x3) { 46121eb5b5cSMostafa Saleh qemu_log_mask(LOG_UNIMP, "SMMUv3 S2SL0 = 0x3 has no meaning!\n"); 46221eb5b5cSMostafa Saleh goto bad_ste; 46321eb5b5cSMostafa Saleh } 46421eb5b5cSMostafa Saleh 46521eb5b5cSMostafa Saleh /* For AA64, The effective S2PS size is capped to the OAS. */ 4666783a184SMostafa Saleh cfg->s2cfg.eff_ps = oas2bits(MIN(STE_S2PS(ste), oas)); 4676783a184SMostafa Saleh /* 4686783a184SMostafa Saleh * For SMMUv3.1 and later, when OAS == IAS == 52, the stage 2 input 4696783a184SMostafa Saleh * range is further limited to 48 bits unless STE.S2TG indicates a 4706783a184SMostafa Saleh * 64KB granule. 4716783a184SMostafa Saleh */ 4726783a184SMostafa Saleh if (cfg->s2cfg.granule_sz != 16) { 4736783a184SMostafa Saleh cfg->s2cfg.eff_ps = MIN(cfg->s2cfg.eff_ps, 48); 4746783a184SMostafa Saleh } 47521eb5b5cSMostafa Saleh /* 47621eb5b5cSMostafa Saleh * It is ILLEGAL for the address in S2TTB to be outside the range 47721eb5b5cSMostafa Saleh * described by the effective S2PS value. 47821eb5b5cSMostafa Saleh */ 47921eb5b5cSMostafa Saleh if (cfg->s2cfg.vttb & ~(MAKE_64BIT_MASK(0, cfg->s2cfg.eff_ps))) { 48021eb5b5cSMostafa Saleh qemu_log_mask(LOG_GUEST_ERROR, 48121eb5b5cSMostafa Saleh "SMMUv3 S2TTB too large 0x%" PRIx64 48221eb5b5cSMostafa Saleh ", effective PS %d bits\n", 48321eb5b5cSMostafa Saleh cfg->s2cfg.vttb, cfg->s2cfg.eff_ps); 48421eb5b5cSMostafa Saleh goto bad_ste; 48521eb5b5cSMostafa Saleh } 48621eb5b5cSMostafa Saleh 48721eb5b5cSMostafa Saleh cfg->s2cfg.tsz = STE_S2T0SZ(ste); 48821eb5b5cSMostafa Saleh 48921eb5b5cSMostafa Saleh if (!s2t0sz_valid(cfg)) { 49021eb5b5cSMostafa Saleh qemu_log_mask(LOG_GUEST_ERROR, "SMMUv3 bad STE S2T0SZ = %d\n", 49121eb5b5cSMostafa Saleh cfg->s2cfg.tsz); 49221eb5b5cSMostafa Saleh goto bad_ste; 49321eb5b5cSMostafa Saleh } 49421eb5b5cSMostafa Saleh 49521eb5b5cSMostafa Saleh if (!s2_pgtable_config_valid(cfg->s2cfg.sl0, cfg->s2cfg.tsz, 49621eb5b5cSMostafa Saleh cfg->s2cfg.granule_sz)) { 49721eb5b5cSMostafa Saleh qemu_log_mask(LOG_GUEST_ERROR, 49821eb5b5cSMostafa Saleh "SMMUv3 STE stage 2 config not valid!\n"); 49921eb5b5cSMostafa Saleh goto bad_ste; 50021eb5b5cSMostafa Saleh } 50121eb5b5cSMostafa Saleh 50221eb5b5cSMostafa Saleh /* Only LE supported(IDR0.TTENDIAN). */ 50321eb5b5cSMostafa Saleh if (STE_S2ENDI(ste)) { 50421eb5b5cSMostafa Saleh qemu_log_mask(LOG_GUEST_ERROR, 50521eb5b5cSMostafa Saleh "SMMUv3 STE_S2ENDI only supports LE!\n"); 50621eb5b5cSMostafa Saleh goto bad_ste; 50721eb5b5cSMostafa Saleh } 50821eb5b5cSMostafa Saleh 50921eb5b5cSMostafa Saleh cfg->s2cfg.affd = STE_S2AFFD(ste); 51021eb5b5cSMostafa Saleh 51121eb5b5cSMostafa Saleh cfg->s2cfg.record_faults = STE_S2R(ste); 51221eb5b5cSMostafa Saleh /* As stall is not supported. */ 51321eb5b5cSMostafa Saleh if (STE_S2S(ste)) { 51421eb5b5cSMostafa Saleh qemu_log_mask(LOG_UNIMP, "SMMUv3 Stall not implemented!\n"); 51521eb5b5cSMostafa Saleh goto bad_ste; 51621eb5b5cSMostafa Saleh } 51721eb5b5cSMostafa Saleh 51821eb5b5cSMostafa Saleh return 0; 51921eb5b5cSMostafa Saleh 52021eb5b5cSMostafa Saleh bad_ste: 52121eb5b5cSMostafa Saleh return -EINVAL; 52221eb5b5cSMostafa Saleh } 52321eb5b5cSMostafa Saleh 52458377c36SMostafa Saleh static void decode_ste_config(SMMUTransCfg *cfg, uint32_t config) 52558377c36SMostafa Saleh { 52658377c36SMostafa Saleh 52758377c36SMostafa Saleh if (STE_CFG_ABORT(config)) { 52858377c36SMostafa Saleh cfg->aborted = true; 52958377c36SMostafa Saleh return; 53058377c36SMostafa Saleh } 53158377c36SMostafa Saleh if (STE_CFG_BYPASS(config)) { 53258377c36SMostafa Saleh cfg->bypassed = true; 53358377c36SMostafa Saleh return; 53458377c36SMostafa Saleh } 53558377c36SMostafa Saleh 53658377c36SMostafa Saleh if (STE_CFG_S1_ENABLED(config)) { 53758377c36SMostafa Saleh cfg->stage = SMMU_STAGE_1; 53858377c36SMostafa Saleh } 53958377c36SMostafa Saleh 54058377c36SMostafa Saleh if (STE_CFG_S2_ENABLED(config)) { 54158377c36SMostafa Saleh cfg->stage |= SMMU_STAGE_2; 54258377c36SMostafa Saleh } 54358377c36SMostafa Saleh } 54458377c36SMostafa Saleh 5459122bea9SJia He /* Returns < 0 in case of invalid STE, 0 otherwise */ 5469bde7f06SEric Auger static int decode_ste(SMMUv3State *s, SMMUTransCfg *cfg, 5479bde7f06SEric Auger STE *ste, SMMUEventInfo *event) 5489bde7f06SEric Auger { 5499bde7f06SEric Auger uint32_t config; 5506783a184SMostafa Saleh uint8_t oas = FIELD_EX32(s->idr[5], IDR5, OAS); 55121eb5b5cSMostafa Saleh int ret; 5529bde7f06SEric Auger 5539bde7f06SEric Auger if (!STE_VALID(ste)) { 5543499ec08SEric Auger if (!event->inval_ste_allowed) { 55551b6d368SEric Auger qemu_log_mask(LOG_GUEST_ERROR, "invalid STE\n"); 5563499ec08SEric Auger } 5579bde7f06SEric Auger goto bad_ste; 5589bde7f06SEric Auger } 5599bde7f06SEric Auger 5609bde7f06SEric Auger config = STE_CONFIG(ste); 5619bde7f06SEric Auger 56258377c36SMostafa Saleh decode_ste_config(cfg, config); 5639bde7f06SEric Auger 56458377c36SMostafa Saleh if (cfg->aborted || cfg->bypassed) { 5659122bea9SJia He return 0; 5669bde7f06SEric Auger } 5679bde7f06SEric Auger 56821eb5b5cSMostafa Saleh /* 56921eb5b5cSMostafa Saleh * If a stage is enabled in SW while not advertised, throw bad ste 57021eb5b5cSMostafa Saleh * according to user manual(IHI0070E) "5.2 Stream Table Entry". 57121eb5b5cSMostafa Saleh */ 57221eb5b5cSMostafa Saleh if (!STAGE1_SUPPORTED(s) && STE_CFG_S1_ENABLED(config)) { 57321eb5b5cSMostafa Saleh qemu_log_mask(LOG_GUEST_ERROR, "SMMUv3 S1 used but not supported.\n"); 5749bde7f06SEric Auger goto bad_ste; 5759bde7f06SEric Auger } 57621eb5b5cSMostafa Saleh if (!STAGE2_SUPPORTED(s) && STE_CFG_S2_ENABLED(config)) { 57721eb5b5cSMostafa Saleh qemu_log_mask(LOG_GUEST_ERROR, "SMMUv3 S2 used but not supported.\n"); 57821eb5b5cSMostafa Saleh goto bad_ste; 57921eb5b5cSMostafa Saleh } 58021eb5b5cSMostafa Saleh 58121eb5b5cSMostafa Saleh if (STAGE2_SUPPORTED(s)) { 58221eb5b5cSMostafa Saleh /* VMID is considered even if s2 is disabled. */ 58321eb5b5cSMostafa Saleh cfg->s2cfg.vmid = STE_S2VMID(ste); 58421eb5b5cSMostafa Saleh } else { 58521eb5b5cSMostafa Saleh /* Default to -1 */ 58621eb5b5cSMostafa Saleh cfg->s2cfg.vmid = -1; 58721eb5b5cSMostafa Saleh } 58821eb5b5cSMostafa Saleh 58921eb5b5cSMostafa Saleh if (STE_CFG_S2_ENABLED(config)) { 59021eb5b5cSMostafa Saleh /* 59121eb5b5cSMostafa Saleh * Stage-1 OAS defaults to OAS even if not enabled as it would be used 59221eb5b5cSMostafa Saleh * in input address check for stage-2. 59321eb5b5cSMostafa Saleh */ 5946783a184SMostafa Saleh cfg->oas = oas2bits(oas); 5956783a184SMostafa Saleh ret = decode_ste_s2_cfg(s, cfg, ste); 59621eb5b5cSMostafa Saleh if (ret) { 59721eb5b5cSMostafa Saleh goto bad_ste; 59821eb5b5cSMostafa Saleh } 59921eb5b5cSMostafa Saleh } 6009bde7f06SEric Auger 6019bde7f06SEric Auger if (STE_S1CDMAX(ste) != 0) { 6029bde7f06SEric Auger qemu_log_mask(LOG_UNIMP, 6039bde7f06SEric Auger "SMMUv3 does not support multiple context descriptors yet\n"); 6049bde7f06SEric Auger goto bad_ste; 6059bde7f06SEric Auger } 6069bde7f06SEric Auger 6079bde7f06SEric Auger if (STE_S1STALLD(ste)) { 6089bde7f06SEric Auger qemu_log_mask(LOG_UNIMP, 6099bde7f06SEric Auger "SMMUv3 S1 stalling fault model not allowed yet\n"); 6109bde7f06SEric Auger goto bad_ste; 6119bde7f06SEric Auger } 6129bde7f06SEric Auger return 0; 6139bde7f06SEric Auger 6149bde7f06SEric Auger bad_ste: 6159bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STE; 6169bde7f06SEric Auger return -EINVAL; 6179bde7f06SEric Auger } 6189bde7f06SEric Auger 6199bde7f06SEric Auger /** 6209bde7f06SEric Auger * smmu_find_ste - Return the stream table entry associated 6219bde7f06SEric Auger * to the sid 6229bde7f06SEric Auger * 6239bde7f06SEric Auger * @s: smmuv3 handle 6249bde7f06SEric Auger * @sid: stream ID 6259bde7f06SEric Auger * @ste: returned stream table entry 6269bde7f06SEric Auger * @event: handle to an event info 6279bde7f06SEric Auger * 6289bde7f06SEric Auger * Supports linear and 2-level stream table 6299bde7f06SEric Auger * Return 0 on success, -EINVAL otherwise 6309bde7f06SEric Auger */ 6319bde7f06SEric Auger static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, 6329bde7f06SEric Auger SMMUEventInfo *event) 6339bde7f06SEric Auger { 63441678c33SSimon Veith dma_addr_t addr, strtab_base; 63505ff2fb8SSimon Veith uint32_t log2size; 63641678c33SSimon Veith int strtab_size_shift; 6379bde7f06SEric Auger int ret; 6389bde7f06SEric Auger 6399bde7f06SEric Auger trace_smmuv3_find_ste(sid, s->features, s->sid_split); 64005ff2fb8SSimon Veith log2size = FIELD_EX32(s->strtab_base_cfg, STRTAB_BASE_CFG, LOG2SIZE); 64105ff2fb8SSimon Veith /* 64205ff2fb8SSimon Veith * Check SID range against both guest-configured and implementation limits 64305ff2fb8SSimon Veith */ 64405ff2fb8SSimon Veith if (sid >= (1 << MIN(log2size, SMMU_IDR1_SIDSIZE))) { 6459bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STREAMID; 6469bde7f06SEric Auger return -EINVAL; 6479bde7f06SEric Auger } 6489bde7f06SEric Auger if (s->features & SMMU_FEATURE_2LVL_STE) { 649c6445544SPeter Maydell int l1_ste_offset, l2_ste_offset, max_l2_ste, span, i; 65041678c33SSimon Veith dma_addr_t l1ptr, l2ptr; 6519bde7f06SEric Auger STEDesc l1std; 6529bde7f06SEric Auger 65341678c33SSimon Veith /* 65441678c33SSimon Veith * Align strtab base address to table size. For this purpose, assume it 65541678c33SSimon Veith * is not bounded by SMMU_IDR1_SIDSIZE. 65641678c33SSimon Veith */ 65741678c33SSimon Veith strtab_size_shift = MAX(5, (int)log2size - s->sid_split - 1 + 3); 65841678c33SSimon Veith strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK & 65941678c33SSimon Veith ~MAKE_64BIT_MASK(0, strtab_size_shift); 6609bde7f06SEric Auger l1_ste_offset = sid >> s->sid_split; 6619bde7f06SEric Auger l2_ste_offset = sid & ((1 << s->sid_split) - 1); 6629bde7f06SEric Auger l1ptr = (dma_addr_t)(strtab_base + l1_ste_offset * sizeof(l1std)); 6639bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 66418610bfdSPhilippe Mathieu-Daudé ret = dma_memory_read(&address_space_memory, l1ptr, &l1std, 665ba06fe8aSPhilippe Mathieu-Daudé sizeof(l1std), MEMTXATTRS_UNSPECIFIED); 6669bde7f06SEric Auger if (ret != MEMTX_OK) { 6679bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 6689bde7f06SEric Auger "Could not read L1PTR at 0X%"PRIx64"\n", l1ptr); 6699bde7f06SEric Auger event->type = SMMU_EVT_F_STE_FETCH; 6709bde7f06SEric Auger event->u.f_ste_fetch.addr = l1ptr; 6719bde7f06SEric Auger return -EINVAL; 6729bde7f06SEric Auger } 673c6445544SPeter Maydell for (i = 0; i < ARRAY_SIZE(l1std.word); i++) { 674c6445544SPeter Maydell le32_to_cpus(&l1std.word[i]); 675c6445544SPeter Maydell } 6769bde7f06SEric Auger 6779bde7f06SEric Auger span = L1STD_SPAN(&l1std); 6789bde7f06SEric Auger 6799bde7f06SEric Auger if (!span) { 6809bde7f06SEric Auger /* l2ptr is not valid */ 6813499ec08SEric Auger if (!event->inval_ste_allowed) { 6829bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 6839bde7f06SEric Auger "invalid sid=%d (L1STD span=0)\n", sid); 6843499ec08SEric Auger } 6859bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STREAMID; 6869bde7f06SEric Auger return -EINVAL; 6879bde7f06SEric Auger } 6889bde7f06SEric Auger max_l2_ste = (1 << span) - 1; 6899bde7f06SEric Auger l2ptr = l1std_l2ptr(&l1std); 6909bde7f06SEric Auger trace_smmuv3_find_ste_2lvl(s->strtab_base, l1ptr, l1_ste_offset, 6919bde7f06SEric Auger l2ptr, l2_ste_offset, max_l2_ste); 6929bde7f06SEric Auger if (l2_ste_offset > max_l2_ste) { 6939bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 6949bde7f06SEric Auger "l2_ste_offset=%d > max_l2_ste=%d\n", 6959bde7f06SEric Auger l2_ste_offset, max_l2_ste); 6969bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STE; 6979bde7f06SEric Auger return -EINVAL; 6989bde7f06SEric Auger } 6999bde7f06SEric Auger addr = l2ptr + l2_ste_offset * sizeof(*ste); 7009bde7f06SEric Auger } else { 70141678c33SSimon Veith strtab_size_shift = log2size + 5; 70241678c33SSimon Veith strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK & 70341678c33SSimon Veith ~MAKE_64BIT_MASK(0, strtab_size_shift); 70441678c33SSimon Veith addr = strtab_base + sid * sizeof(*ste); 7059bde7f06SEric Auger } 7069bde7f06SEric Auger 7079bde7f06SEric Auger if (smmu_get_ste(s, addr, ste, event)) { 7089bde7f06SEric Auger return -EINVAL; 7099bde7f06SEric Auger } 7109bde7f06SEric Auger 7119bde7f06SEric Auger return 0; 7129bde7f06SEric Auger } 7139bde7f06SEric Auger 7149dd6aa9bSMostafa Saleh static int decode_cd(SMMUv3State *s, SMMUTransCfg *cfg, 7159dd6aa9bSMostafa Saleh CD *cd, SMMUEventInfo *event) 7169bde7f06SEric Auger { 7179bde7f06SEric Auger int ret = -EINVAL; 7189bde7f06SEric Auger int i; 7199dd6aa9bSMostafa Saleh SMMUTranslationStatus status; 7209dd6aa9bSMostafa Saleh SMMUTLBEntry *entry; 7216783a184SMostafa Saleh uint8_t oas = FIELD_EX32(s->idr[5], IDR5, OAS); 7229bde7f06SEric Auger 7239bde7f06SEric Auger if (!CD_VALID(cd) || !CD_AARCH64(cd)) { 7249bde7f06SEric Auger goto bad_cd; 7259bde7f06SEric Auger } 7269bde7f06SEric Auger if (!CD_A(cd)) { 7279bde7f06SEric Auger goto bad_cd; /* SMMU_IDR0.TERM_MODEL == 1 */ 7289bde7f06SEric Auger } 7299bde7f06SEric Auger if (CD_S(cd)) { 7309bde7f06SEric Auger goto bad_cd; /* !STE_SECURE && SMMU_IDR0.STALL_MODEL == 1 */ 7319bde7f06SEric Auger } 7329bde7f06SEric Auger if (CD_HA(cd) || CD_HD(cd)) { 7339bde7f06SEric Auger goto bad_cd; /* HTTU = 0 */ 7349bde7f06SEric Auger } 7359bde7f06SEric Auger 7369bde7f06SEric Auger /* we support only those at the moment */ 7379bde7f06SEric Auger cfg->aa64 = true; 7389bde7f06SEric Auger 7399bde7f06SEric Auger cfg->oas = oas2bits(CD_IPS(cd)); 7406783a184SMostafa Saleh cfg->oas = MIN(oas2bits(oas), cfg->oas); 7419bde7f06SEric Auger cfg->tbi = CD_TBI(cd); 7429bde7f06SEric Auger cfg->asid = CD_ASID(cd); 74315f6c16eSLuc Michel cfg->affd = CD_AFFD(cd); 7449bde7f06SEric Auger 7459bde7f06SEric Auger trace_smmuv3_decode_cd(cfg->oas); 7469bde7f06SEric Auger 7479bde7f06SEric Auger /* decode data dependent on TT */ 7489bde7f06SEric Auger for (i = 0; i <= 1; i++) { 7499bde7f06SEric Auger int tg, tsz; 7509bde7f06SEric Auger SMMUTransTableInfo *tt = &cfg->tt[i]; 7519bde7f06SEric Auger 7529bde7f06SEric Auger cfg->tt[i].disabled = CD_EPD(cd, i); 7539bde7f06SEric Auger if (cfg->tt[i].disabled) { 7549bde7f06SEric Auger continue; 7559bde7f06SEric Auger } 7569bde7f06SEric Auger 7579bde7f06SEric Auger tsz = CD_TSZ(cd, i); 7589bde7f06SEric Auger if (tsz < 16 || tsz > 39) { 7599bde7f06SEric Auger goto bad_cd; 7609bde7f06SEric Auger } 7619bde7f06SEric Auger 7629bde7f06SEric Auger tg = CD_TG(cd, i); 7639bde7f06SEric Auger tt->granule_sz = tg2granule(tg, i); 764bf559ee4SKunkun Jiang if ((tt->granule_sz != 12 && tt->granule_sz != 14 && 765bf559ee4SKunkun Jiang tt->granule_sz != 16) || CD_ENDI(cd)) { 7669bde7f06SEric Auger goto bad_cd; 7679bde7f06SEric Auger } 7689bde7f06SEric Auger 7696783a184SMostafa Saleh /* 7706783a184SMostafa Saleh * An address greater than 48 bits in size can only be output from a 7716783a184SMostafa Saleh * TTD when, in SMMUv3.1 and later, the effective IPS is 52 and a 64KB 7726783a184SMostafa Saleh * granule is in use for that translation table 7736783a184SMostafa Saleh */ 7746783a184SMostafa Saleh if (tt->granule_sz != 16) { 7756783a184SMostafa Saleh cfg->oas = MIN(cfg->oas, 48); 7766783a184SMostafa Saleh } 7779bde7f06SEric Auger tt->tsz = tsz; 7789bde7f06SEric Auger tt->ttb = CD_TTB(cd, i); 7799dd6aa9bSMostafa Saleh 7809bde7f06SEric Auger if (tt->ttb & ~(MAKE_64BIT_MASK(0, cfg->oas))) { 7819bde7f06SEric Auger goto bad_cd; 7829bde7f06SEric Auger } 7839dd6aa9bSMostafa Saleh 7849dd6aa9bSMostafa Saleh /* Translate the TTBx, from IPA to PA if nesting is enabled. */ 7859dd6aa9bSMostafa Saleh if (cfg->stage == SMMU_NESTED) { 7869dd6aa9bSMostafa Saleh status = smmuv3_do_translate(s, tt->ttb, cfg, event, IOMMU_RO, 7879dd6aa9bSMostafa Saleh &entry, SMMU_CLASS_TT); 7889dd6aa9bSMostafa Saleh /* 7899dd6aa9bSMostafa Saleh * Same PTW faults are reported but with CLASS = TT. 7909dd6aa9bSMostafa Saleh * If TTBx is larger than the effective stage 1 output addres 7919dd6aa9bSMostafa Saleh * size, it reports C_BAD_CD, which is handled by the above case. 7929dd6aa9bSMostafa Saleh */ 7939dd6aa9bSMostafa Saleh if (status != SMMU_TRANS_SUCCESS) { 7949dd6aa9bSMostafa Saleh return -EINVAL; 7959dd6aa9bSMostafa Saleh } 7969dd6aa9bSMostafa Saleh tt->ttb = CACHED_ENTRY_TO_ADDR(entry, tt->ttb); 7979dd6aa9bSMostafa Saleh } 7989dd6aa9bSMostafa Saleh 799e7c3b9d9SEric Auger tt->had = CD_HAD(cd, i); 800e7c3b9d9SEric Auger trace_smmuv3_decode_cd_tt(i, tt->tsz, tt->ttb, tt->granule_sz, tt->had); 8019bde7f06SEric Auger } 8029bde7f06SEric Auger 803ced71694SJean-Philippe Brucker cfg->record_faults = CD_R(cd); 8049bde7f06SEric Auger 8059bde7f06SEric Auger return 0; 8069bde7f06SEric Auger 8079bde7f06SEric Auger bad_cd: 8089bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_CD; 8099bde7f06SEric Auger return ret; 8109bde7f06SEric Auger } 8119bde7f06SEric Auger 8129bde7f06SEric Auger /** 8139bde7f06SEric Auger * smmuv3_decode_config - Prepare the translation configuration 8149bde7f06SEric Auger * for the @mr iommu region 8159bde7f06SEric Auger * @mr: iommu memory region the translation config must be prepared for 8169bde7f06SEric Auger * @cfg: output translation configuration which is populated through 8179bde7f06SEric Auger * the different configuration decoding steps 8189bde7f06SEric Auger * @event: must be zero'ed by the caller 8199bde7f06SEric Auger * 8209122bea9SJia He * return < 0 in case of config decoding error (@event is filled 8219bde7f06SEric Auger * accordingly). Return 0 otherwise. 8229bde7f06SEric Auger */ 8239bde7f06SEric Auger static int smmuv3_decode_config(IOMMUMemoryRegion *mr, SMMUTransCfg *cfg, 8249bde7f06SEric Auger SMMUEventInfo *event) 8259bde7f06SEric Auger { 8269bde7f06SEric Auger SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu); 8279bde7f06SEric Auger uint32_t sid = smmu_get_sid(sdev); 8289bde7f06SEric Auger SMMUv3State *s = sdev->smmu; 8299122bea9SJia He int ret; 8309bde7f06SEric Auger STE ste; 8319bde7f06SEric Auger CD cd; 8329bde7f06SEric Auger 833cd617556SMostafa Saleh /* ASID defaults to -1 (if s1 is not supported). */ 834cd617556SMostafa Saleh cfg->asid = -1; 835cd617556SMostafa Saleh 8369122bea9SJia He ret = smmu_find_ste(s, sid, &ste, event); 8379122bea9SJia He if (ret) { 8389bde7f06SEric Auger return ret; 8399bde7f06SEric Auger } 8409bde7f06SEric Auger 8419122bea9SJia He ret = decode_ste(s, cfg, &ste, event); 8429122bea9SJia He if (ret) { 8439bde7f06SEric Auger return ret; 8449bde7f06SEric Auger } 8459bde7f06SEric Auger 846f6cc1980SMostafa Saleh if (cfg->aborted || cfg->bypassed || (cfg->stage == SMMU_STAGE_2)) { 8479122bea9SJia He return 0; 8489122bea9SJia He } 8499122bea9SJia He 8509dd6aa9bSMostafa Saleh ret = smmu_get_cd(s, &ste, cfg, 0 /* ssid */, &cd, event); 8519122bea9SJia He if (ret) { 8529bde7f06SEric Auger return ret; 8539bde7f06SEric Auger } 8549bde7f06SEric Auger 8559dd6aa9bSMostafa Saleh return decode_cd(s, cfg, &cd, event); 8569bde7f06SEric Auger } 8579bde7f06SEric Auger 85832cfd7f3SEric Auger /** 85932cfd7f3SEric Auger * smmuv3_get_config - Look up for a cached copy of configuration data for 86032cfd7f3SEric Auger * @sdev and on cache miss performs a configuration structure decoding from 86132cfd7f3SEric Auger * guest RAM. 86232cfd7f3SEric Auger * 86332cfd7f3SEric Auger * @sdev: SMMUDevice handle 86432cfd7f3SEric Auger * @event: output event info 86532cfd7f3SEric Auger * 86632cfd7f3SEric Auger * The configuration cache contains data resulting from both STE and CD 86732cfd7f3SEric Auger * decoding under the form of an SMMUTransCfg struct. The hash table is indexed 86832cfd7f3SEric Auger * by the SMMUDevice handle. 86932cfd7f3SEric Auger */ 87032cfd7f3SEric Auger static SMMUTransCfg *smmuv3_get_config(SMMUDevice *sdev, SMMUEventInfo *event) 87132cfd7f3SEric Auger { 87232cfd7f3SEric Auger SMMUv3State *s = sdev->smmu; 87332cfd7f3SEric Auger SMMUState *bc = &s->smmu_state; 87432cfd7f3SEric Auger SMMUTransCfg *cfg; 87532cfd7f3SEric Auger 87632cfd7f3SEric Auger cfg = g_hash_table_lookup(bc->configs, sdev); 87732cfd7f3SEric Auger if (cfg) { 87832cfd7f3SEric Auger sdev->cfg_cache_hits++; 87932cfd7f3SEric Auger trace_smmuv3_config_cache_hit(smmu_get_sid(sdev), 88032cfd7f3SEric Auger sdev->cfg_cache_hits, sdev->cfg_cache_misses, 88132cfd7f3SEric Auger 100 * sdev->cfg_cache_hits / 88232cfd7f3SEric Auger (sdev->cfg_cache_hits + sdev->cfg_cache_misses)); 88332cfd7f3SEric Auger } else { 88432cfd7f3SEric Auger sdev->cfg_cache_misses++; 88532cfd7f3SEric Auger trace_smmuv3_config_cache_miss(smmu_get_sid(sdev), 88632cfd7f3SEric Auger sdev->cfg_cache_hits, sdev->cfg_cache_misses, 88732cfd7f3SEric Auger 100 * sdev->cfg_cache_hits / 88832cfd7f3SEric Auger (sdev->cfg_cache_hits + sdev->cfg_cache_misses)); 88932cfd7f3SEric Auger cfg = g_new0(SMMUTransCfg, 1); 89032cfd7f3SEric Auger 89132cfd7f3SEric Auger if (!smmuv3_decode_config(&sdev->iommu, cfg, event)) { 89232cfd7f3SEric Auger g_hash_table_insert(bc->configs, sdev, cfg); 89332cfd7f3SEric Auger } else { 89432cfd7f3SEric Auger g_free(cfg); 89532cfd7f3SEric Auger cfg = NULL; 89632cfd7f3SEric Auger } 89732cfd7f3SEric Auger } 89832cfd7f3SEric Auger return cfg; 89932cfd7f3SEric Auger } 90032cfd7f3SEric Auger 90132cfd7f3SEric Auger static void smmuv3_flush_config(SMMUDevice *sdev) 90232cfd7f3SEric Auger { 90332cfd7f3SEric Auger SMMUv3State *s = sdev->smmu; 90432cfd7f3SEric Auger SMMUState *bc = &s->smmu_state; 90532cfd7f3SEric Auger 90632cfd7f3SEric Auger trace_smmuv3_config_cache_inv(smmu_get_sid(sdev)); 90732cfd7f3SEric Auger g_hash_table_remove(bc->configs, sdev); 90832cfd7f3SEric Auger } 90932cfd7f3SEric Auger 910a9e3f4c1SMostafa Saleh /* Do translation with TLB lookup. */ 911a9e3f4c1SMostafa Saleh static SMMUTranslationStatus smmuv3_do_translate(SMMUv3State *s, hwaddr addr, 912a9e3f4c1SMostafa Saleh SMMUTransCfg *cfg, 913a9e3f4c1SMostafa Saleh SMMUEventInfo *event, 914a9e3f4c1SMostafa Saleh IOMMUAccessFlags flag, 9159dd6aa9bSMostafa Saleh SMMUTLBEntry **out_entry, 9169dd6aa9bSMostafa Saleh SMMUTranslationClass class) 917a9e3f4c1SMostafa Saleh { 918a9e3f4c1SMostafa Saleh SMMUPTWEventInfo ptw_info = {}; 919a9e3f4c1SMostafa Saleh SMMUState *bs = ARM_SMMU(s); 920a9e3f4c1SMostafa Saleh SMMUTLBEntry *cached_entry = NULL; 9219dd6aa9bSMostafa Saleh int asid, stage; 9229dd6aa9bSMostafa Saleh bool desc_s2_translation = class != SMMU_CLASS_IN; 9239dd6aa9bSMostafa Saleh 9249dd6aa9bSMostafa Saleh /* 9259dd6aa9bSMostafa Saleh * The function uses the argument class to identify which stage is used: 9269dd6aa9bSMostafa Saleh * - CLASS = IN: Means an input translation, determine the stage from STE. 9279dd6aa9bSMostafa Saleh * - CLASS = CD: Means the addr is an IPA of the CD, and it would be 9289dd6aa9bSMostafa Saleh * translated using the stage-2. 9299dd6aa9bSMostafa Saleh * - CLASS = TT: Means the addr is an IPA of the stage-1 translation table 9309dd6aa9bSMostafa Saleh * and it would be translated using the stage-2. 9319dd6aa9bSMostafa Saleh * For the last 2 cases instead of having intrusive changes in the common 9329dd6aa9bSMostafa Saleh * logic, we modify the cfg to be a stage-2 translation only in case of 9339dd6aa9bSMostafa Saleh * nested, and then restore it after. 9349dd6aa9bSMostafa Saleh */ 9359dd6aa9bSMostafa Saleh if (desc_s2_translation) { 9369dd6aa9bSMostafa Saleh asid = cfg->asid; 9379dd6aa9bSMostafa Saleh stage = cfg->stage; 9389dd6aa9bSMostafa Saleh cfg->asid = -1; 9399dd6aa9bSMostafa Saleh cfg->stage = SMMU_STAGE_2; 9409dd6aa9bSMostafa Saleh } 941a9e3f4c1SMostafa Saleh 942a9e3f4c1SMostafa Saleh cached_entry = smmu_translate(bs, cfg, addr, flag, &ptw_info); 9439dd6aa9bSMostafa Saleh 9449dd6aa9bSMostafa Saleh if (desc_s2_translation) { 9459dd6aa9bSMostafa Saleh cfg->asid = asid; 9469dd6aa9bSMostafa Saleh cfg->stage = stage; 9479dd6aa9bSMostafa Saleh } 9489dd6aa9bSMostafa Saleh 949a9e3f4c1SMostafa Saleh if (!cached_entry) { 950a9e3f4c1SMostafa Saleh /* All faults from PTW has S2 field. */ 951a9e3f4c1SMostafa Saleh event->u.f_walk_eabt.s2 = (ptw_info.stage == SMMU_STAGE_2); 952f42a0a57SMostafa Saleh /* 953f42a0a57SMostafa Saleh * Fault class is set as follows based on "class" input to 954f42a0a57SMostafa Saleh * the function and to "ptw_info" from "smmu_translate()" 955f42a0a57SMostafa Saleh * For stage-1: 956f42a0a57SMostafa Saleh * - EABT => CLASS_TT (hardcoded) 957f42a0a57SMostafa Saleh * - other events => CLASS_IN (input to function) 958f42a0a57SMostafa Saleh * For stage-2 => CLASS_IN (input to function) 959f42a0a57SMostafa Saleh * For nested, for all events: 960f42a0a57SMostafa Saleh * - CD fetch => CLASS_CD (input to function) 961f42a0a57SMostafa Saleh * - walking stage 1 translation table => CLASS_TT (from 962f42a0a57SMostafa Saleh * is_ipa_descriptor or input in case of TTBx) 963f42a0a57SMostafa Saleh * - s2 translation => CLASS_IN (input to function) 964f42a0a57SMostafa Saleh */ 965f42a0a57SMostafa Saleh class = ptw_info.is_ipa_descriptor ? SMMU_CLASS_TT : class; 966a9e3f4c1SMostafa Saleh switch (ptw_info.type) { 967a9e3f4c1SMostafa Saleh case SMMU_PTW_ERR_WALK_EABT: 968a9e3f4c1SMostafa Saleh event->type = SMMU_EVT_F_WALK_EABT; 969a9e3f4c1SMostafa Saleh event->u.f_walk_eabt.rnw = flag & 0x1; 970a9e3f4c1SMostafa Saleh event->u.f_walk_eabt.class = (ptw_info.stage == SMMU_STAGE_2) ? 9719dd6aa9bSMostafa Saleh class : SMMU_CLASS_TT; 972a9e3f4c1SMostafa Saleh event->u.f_walk_eabt.addr2 = ptw_info.addr; 973a9e3f4c1SMostafa Saleh break; 974a9e3f4c1SMostafa Saleh case SMMU_PTW_ERR_TRANSLATION: 975f9131185SMostafa Saleh if (PTW_RECORD_FAULT(ptw_info, cfg)) { 976a9e3f4c1SMostafa Saleh event->type = SMMU_EVT_F_TRANSLATION; 977a9e3f4c1SMostafa Saleh event->u.f_translation.addr2 = ptw_info.addr; 9789dd6aa9bSMostafa Saleh event->u.f_translation.class = class; 979a9e3f4c1SMostafa Saleh event->u.f_translation.rnw = flag & 0x1; 980a9e3f4c1SMostafa Saleh } 981a9e3f4c1SMostafa Saleh break; 982a9e3f4c1SMostafa Saleh case SMMU_PTW_ERR_ADDR_SIZE: 983f9131185SMostafa Saleh if (PTW_RECORD_FAULT(ptw_info, cfg)) { 984a9e3f4c1SMostafa Saleh event->type = SMMU_EVT_F_ADDR_SIZE; 985a9e3f4c1SMostafa Saleh event->u.f_addr_size.addr2 = ptw_info.addr; 9869dd6aa9bSMostafa Saleh event->u.f_addr_size.class = class; 987a9e3f4c1SMostafa Saleh event->u.f_addr_size.rnw = flag & 0x1; 988a9e3f4c1SMostafa Saleh } 989a9e3f4c1SMostafa Saleh break; 990a9e3f4c1SMostafa Saleh case SMMU_PTW_ERR_ACCESS: 991f9131185SMostafa Saleh if (PTW_RECORD_FAULT(ptw_info, cfg)) { 992a9e3f4c1SMostafa Saleh event->type = SMMU_EVT_F_ACCESS; 993a9e3f4c1SMostafa Saleh event->u.f_access.addr2 = ptw_info.addr; 9949dd6aa9bSMostafa Saleh event->u.f_access.class = class; 995a9e3f4c1SMostafa Saleh event->u.f_access.rnw = flag & 0x1; 996a9e3f4c1SMostafa Saleh } 997a9e3f4c1SMostafa Saleh break; 998a9e3f4c1SMostafa Saleh case SMMU_PTW_ERR_PERMISSION: 999f9131185SMostafa Saleh if (PTW_RECORD_FAULT(ptw_info, cfg)) { 1000a9e3f4c1SMostafa Saleh event->type = SMMU_EVT_F_PERMISSION; 1001a9e3f4c1SMostafa Saleh event->u.f_permission.addr2 = ptw_info.addr; 10029dd6aa9bSMostafa Saleh event->u.f_permission.class = class; 1003a9e3f4c1SMostafa Saleh event->u.f_permission.rnw = flag & 0x1; 1004a9e3f4c1SMostafa Saleh } 1005a9e3f4c1SMostafa Saleh break; 1006a9e3f4c1SMostafa Saleh default: 1007a9e3f4c1SMostafa Saleh g_assert_not_reached(); 1008a9e3f4c1SMostafa Saleh } 1009a9e3f4c1SMostafa Saleh return SMMU_TRANS_ERROR; 1010a9e3f4c1SMostafa Saleh } 1011a9e3f4c1SMostafa Saleh *out_entry = cached_entry; 1012a9e3f4c1SMostafa Saleh return SMMU_TRANS_SUCCESS; 1013a9e3f4c1SMostafa Saleh } 1014a9e3f4c1SMostafa Saleh 10159dd6aa9bSMostafa Saleh /* 10169dd6aa9bSMostafa Saleh * Sets the InputAddr for an SMMU_TRANS_ERROR, as it can't be 10179dd6aa9bSMostafa Saleh * set from all contexts, as smmuv3_get_config() can return 10189dd6aa9bSMostafa Saleh * translation faults in case of nested translation (for CD 10199dd6aa9bSMostafa Saleh * and TTBx). But in that case the iova is not known. 10209dd6aa9bSMostafa Saleh */ 10219dd6aa9bSMostafa Saleh static void smmuv3_fixup_event(SMMUEventInfo *event, hwaddr iova) 10229dd6aa9bSMostafa Saleh { 10239dd6aa9bSMostafa Saleh switch (event->type) { 10249dd6aa9bSMostafa Saleh case SMMU_EVT_F_WALK_EABT: 10259dd6aa9bSMostafa Saleh case SMMU_EVT_F_TRANSLATION: 10269dd6aa9bSMostafa Saleh case SMMU_EVT_F_ADDR_SIZE: 10279dd6aa9bSMostafa Saleh case SMMU_EVT_F_ACCESS: 10289dd6aa9bSMostafa Saleh case SMMU_EVT_F_PERMISSION: 10299dd6aa9bSMostafa Saleh event->u.f_walk_eabt.addr = iova; 10309dd6aa9bSMostafa Saleh break; 10319dd6aa9bSMostafa Saleh default: 10329dd6aa9bSMostafa Saleh break; 10339dd6aa9bSMostafa Saleh } 10349dd6aa9bSMostafa Saleh } 10359dd6aa9bSMostafa Saleh 1036a9e3f4c1SMostafa Saleh /* Entry point to SMMU, does everything. */ 10379bde7f06SEric Auger static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr, 10382c91bcf2SPeter Maydell IOMMUAccessFlags flag, int iommu_idx) 10399bde7f06SEric Auger { 10409bde7f06SEric Auger SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu); 10419bde7f06SEric Auger SMMUv3State *s = sdev->smmu; 10429bde7f06SEric Auger uint32_t sid = smmu_get_sid(sdev); 10433499ec08SEric Auger SMMUEventInfo event = {.type = SMMU_EVT_NONE, 10443499ec08SEric Auger .sid = sid, 10453499ec08SEric Auger .inval_ste_allowed = false}; 10469122bea9SJia He SMMUTranslationStatus status; 104732cfd7f3SEric Auger SMMUTransCfg *cfg = NULL; 10489bde7f06SEric Auger IOMMUTLBEntry entry = { 10499bde7f06SEric Auger .target_as = &address_space_memory, 10509bde7f06SEric Auger .iova = addr, 10519bde7f06SEric Auger .translated_addr = addr, 10529bde7f06SEric Auger .addr_mask = ~(hwaddr)0, 10539bde7f06SEric Auger .perm = IOMMU_NONE, 10549bde7f06SEric Auger }; 1055a9e3f4c1SMostafa Saleh SMMUTLBEntry *cached_entry = NULL; 10569bde7f06SEric Auger 105732cfd7f3SEric Auger qemu_mutex_lock(&s->mutex); 105832cfd7f3SEric Auger 10599bde7f06SEric Auger if (!smmu_enabled(s)) { 1060c2ecb424SMostafa Saleh if (FIELD_EX32(s->gbpa, GBPA, ABORT)) { 1061c2ecb424SMostafa Saleh status = SMMU_TRANS_ABORT; 1062c2ecb424SMostafa Saleh } else { 10639122bea9SJia He status = SMMU_TRANS_DISABLE; 1064c2ecb424SMostafa Saleh } 10659122bea9SJia He goto epilogue; 10669bde7f06SEric Auger } 10679bde7f06SEric Auger 106832cfd7f3SEric Auger cfg = smmuv3_get_config(sdev, &event); 106932cfd7f3SEric Auger if (!cfg) { 10709122bea9SJia He status = SMMU_TRANS_ERROR; 10719122bea9SJia He goto epilogue; 10729bde7f06SEric Auger } 10739bde7f06SEric Auger 107432cfd7f3SEric Auger if (cfg->aborted) { 10759122bea9SJia He status = SMMU_TRANS_ABORT; 10769122bea9SJia He goto epilogue; 10779bde7f06SEric Auger } 10789bde7f06SEric Auger 107932cfd7f3SEric Auger if (cfg->bypassed) { 10809122bea9SJia He status = SMMU_TRANS_BYPASS; 10819122bea9SJia He goto epilogue; 10829122bea9SJia He } 10839122bea9SJia He 10849dd6aa9bSMostafa Saleh status = smmuv3_do_translate(s, addr, cfg, &event, flag, 10859dd6aa9bSMostafa Saleh &cached_entry, SMMU_CLASS_IN); 10869122bea9SJia He 10879122bea9SJia He epilogue: 108832cfd7f3SEric Auger qemu_mutex_unlock(&s->mutex); 10899122bea9SJia He switch (status) { 10909122bea9SJia He case SMMU_TRANS_SUCCESS: 1091c3ca7d56SXiang Chen entry.perm = cached_entry->entry.perm; 1092ec31ef91SMostafa Saleh entry.translated_addr = CACHED_ENTRY_TO_ADDR(cached_entry, addr); 1093a7550158SEric Auger entry.addr_mask = cached_entry->entry.addr_mask; 10949122bea9SJia He trace_smmuv3_translate_success(mr->parent_obj.name, sid, addr, 1095a9e3f4c1SMostafa Saleh entry.translated_addr, entry.perm, 1096a9e3f4c1SMostafa Saleh cfg->stage); 10979122bea9SJia He break; 10989122bea9SJia He case SMMU_TRANS_DISABLE: 10999122bea9SJia He entry.perm = flag; 11009122bea9SJia He entry.addr_mask = ~TARGET_PAGE_MASK; 11019122bea9SJia He trace_smmuv3_translate_disable(mr->parent_obj.name, sid, addr, 11029122bea9SJia He entry.perm); 11039122bea9SJia He break; 11049122bea9SJia He case SMMU_TRANS_BYPASS: 11059122bea9SJia He entry.perm = flag; 11069122bea9SJia He entry.addr_mask = ~TARGET_PAGE_MASK; 11079122bea9SJia He trace_smmuv3_translate_bypass(mr->parent_obj.name, sid, addr, 11089122bea9SJia He entry.perm); 11099122bea9SJia He break; 11109122bea9SJia He case SMMU_TRANS_ABORT: 11119122bea9SJia He /* no event is recorded on abort */ 11129122bea9SJia He trace_smmuv3_translate_abort(mr->parent_obj.name, sid, addr, 11139122bea9SJia He entry.perm); 11149122bea9SJia He break; 11159122bea9SJia He case SMMU_TRANS_ERROR: 11169dd6aa9bSMostafa Saleh smmuv3_fixup_event(&event, addr); 11179122bea9SJia He qemu_log_mask(LOG_GUEST_ERROR, 11189122bea9SJia He "%s translation failed for iova=0x%"PRIx64" (%s)\n", 11199122bea9SJia He mr->parent_obj.name, addr, smmu_event_string(event.type)); 11209122bea9SJia He smmuv3_record_event(s, &event); 11219122bea9SJia He break; 11229bde7f06SEric Auger } 11239bde7f06SEric Auger 11249bde7f06SEric Auger return entry; 11259bde7f06SEric Auger } 11269bde7f06SEric Auger 1127832e4222SEric Auger /** 1128832e4222SEric Auger * smmuv3_notify_iova - call the notifier @n for a given 1129832e4222SEric Auger * @asid and @iova tuple. 1130832e4222SEric Auger * 1131832e4222SEric Auger * @mr: IOMMU mr region handle 1132832e4222SEric Auger * @n: notifier to be called 1133832e4222SEric Auger * @asid: address space ID or negative value if we don't care 113432bd7baeSMostafa Saleh * @vmid: virtual machine ID or negative value if we don't care 1135832e4222SEric Auger * @iova: iova 1136d5291561SEric Auger * @tg: translation granule (if communicated through range invalidation) 1137d5291561SEric Auger * @num_pages: number of @granule sized pages (if tg != 0), otherwise 1 113846727727SMostafa Saleh * @stage: Which stage(1 or 2) is used 1139832e4222SEric Auger */ 1140832e4222SEric Auger static void smmuv3_notify_iova(IOMMUMemoryRegion *mr, 1141832e4222SEric Auger IOMMUNotifier *n, 114232bd7baeSMostafa Saleh int asid, int vmid, 114332bd7baeSMostafa Saleh dma_addr_t iova, uint8_t tg, 114446727727SMostafa Saleh uint64_t num_pages, int stage) 1145832e4222SEric Auger { 1146832e4222SEric Auger SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu); 11479e2135eeSPeter Maydell SMMUEventInfo eventinfo = {.inval_ste_allowed = true}; 11489e2135eeSPeter Maydell SMMUTransCfg *cfg = smmuv3_get_config(sdev, &eventinfo); 114946727727SMostafa Saleh IOMMUTLBEvent event; 115046727727SMostafa Saleh uint8_t granule; 1151d5291561SEric Auger 1152832e4222SEric Auger if (!cfg) { 1153832e4222SEric Auger return; 1154832e4222SEric Auger } 1155832e4222SEric Auger 115646727727SMostafa Saleh /* 115746727727SMostafa Saleh * stage is passed from TLB invalidation commands which can be either 115846727727SMostafa Saleh * stage-1 or stage-2. 115946727727SMostafa Saleh * However, IOMMUTLBEvent only understands IOVA, for stage-1 or stage-2 116046727727SMostafa Saleh * SMMU instances we consider the input address as the IOVA, but when 116146727727SMostafa Saleh * nesting is used, we can't mix stage-1 and stage-2 addresses, so for 116246727727SMostafa Saleh * nesting only stage-1 is considered the IOVA and would be notified. 116346727727SMostafa Saleh */ 116446727727SMostafa Saleh if ((stage == SMMU_STAGE_2) && (cfg->stage == SMMU_NESTED)) 116546727727SMostafa Saleh return; 116646727727SMostafa Saleh 116746727727SMostafa Saleh if (!tg) { 116846727727SMostafa Saleh SMMUTransTableInfo *tt; 116946727727SMostafa Saleh 1170832e4222SEric Auger if (asid >= 0 && cfg->asid != asid) { 1171832e4222SEric Auger return; 1172832e4222SEric Auger } 1173832e4222SEric Auger 117432bd7baeSMostafa Saleh if (vmid >= 0 && cfg->s2cfg.vmid != vmid) { 117532bd7baeSMostafa Saleh return; 117632bd7baeSMostafa Saleh } 117732bd7baeSMostafa Saleh 117846727727SMostafa Saleh if (stage == SMMU_STAGE_1) { 1179832e4222SEric Auger tt = select_tt(cfg, iova); 1180832e4222SEric Auger if (!tt) { 1181832e4222SEric Auger return; 1182832e4222SEric Auger } 1183d5291561SEric Auger granule = tt->granule_sz; 1184dcda883cSZenghui Yu } else { 118532bd7baeSMostafa Saleh granule = cfg->s2cfg.granule_sz; 118632bd7baeSMostafa Saleh } 118732bd7baeSMostafa Saleh 118832bd7baeSMostafa Saleh } else { 1189dcda883cSZenghui Yu granule = tg * 2 + 10; 1190d5291561SEric Auger } 1191832e4222SEric Auger 11925039caf3SEugenio Pérez event.type = IOMMU_NOTIFIER_UNMAP; 11935039caf3SEugenio Pérez event.entry.target_as = &address_space_memory; 11945039caf3SEugenio Pérez event.entry.iova = iova; 11955039caf3SEugenio Pérez event.entry.addr_mask = num_pages * (1 << granule) - 1; 11965039caf3SEugenio Pérez event.entry.perm = IOMMU_NONE; 1197832e4222SEric Auger 11985039caf3SEugenio Pérez memory_region_notify_iommu_one(n, &event); 1199832e4222SEric Auger } 1200832e4222SEric Auger 120132bd7baeSMostafa Saleh /* invalidate an asid/vmid/iova range tuple in all mr's */ 120232bd7baeSMostafa Saleh static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, int vmid, 120332bd7baeSMostafa Saleh dma_addr_t iova, uint8_t tg, 120446727727SMostafa Saleh uint64_t num_pages, int stage) 1205832e4222SEric Auger { 1206c6370441SEric Auger SMMUDevice *sdev; 1207832e4222SEric Auger 1208c6370441SEric Auger QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) { 1209c6370441SEric Auger IOMMUMemoryRegion *mr = &sdev->iommu; 1210832e4222SEric Auger IOMMUNotifier *n; 1211832e4222SEric Auger 121232bd7baeSMostafa Saleh trace_smmuv3_inv_notifiers_iova(mr->parent_obj.name, asid, vmid, 121346727727SMostafa Saleh iova, tg, num_pages, stage); 1214832e4222SEric Auger 1215832e4222SEric Auger IOMMU_NOTIFIER_FOREACH(n, mr) { 121646727727SMostafa Saleh smmuv3_notify_iova(mr, n, asid, vmid, iova, tg, num_pages, stage); 1217832e4222SEric Auger } 1218832e4222SEric Auger } 1219832e4222SEric Auger } 1220832e4222SEric Auger 12211ea8a6f5SMostafa Saleh static void smmuv3_range_inval(SMMUState *s, Cmd *cmd, SMMUStage stage) 1222c0f9ef70SEric Auger { 1223219729cfSEric Auger dma_addr_t end, addr = CMD_ADDR(cmd); 1224c0f9ef70SEric Auger uint8_t type = CMD_TYPE(cmd); 12252eaeb7d5SMostafa Saleh int vmid = -1; 1226219729cfSEric Auger uint8_t scale = CMD_SCALE(cmd); 1227219729cfSEric Auger uint8_t num = CMD_NUM(cmd); 1228219729cfSEric Auger uint8_t ttl = CMD_TTL(cmd); 1229c0f9ef70SEric Auger bool leaf = CMD_LEAF(cmd); 1230d5291561SEric Auger uint8_t tg = CMD_TG(cmd); 1231219729cfSEric Auger uint64_t num_pages; 1232219729cfSEric Auger uint8_t granule; 1233c0f9ef70SEric Auger int asid = -1; 12342eaeb7d5SMostafa Saleh SMMUv3State *smmuv3 = ARM_SMMUV3(s); 12352eaeb7d5SMostafa Saleh 12362eaeb7d5SMostafa Saleh /* Only consider VMID if stage-2 is supported. */ 12372eaeb7d5SMostafa Saleh if (STAGE2_SUPPORTED(smmuv3)) { 12382eaeb7d5SMostafa Saleh vmid = CMD_VMID(cmd); 12392eaeb7d5SMostafa Saleh } 1240c0f9ef70SEric Auger 1241c0f9ef70SEric Auger if (type == SMMU_CMD_TLBI_NH_VA) { 1242c0f9ef70SEric Auger asid = CMD_ASID(cmd); 1243c0f9ef70SEric Auger } 12446d9cd115SEric Auger 1245219729cfSEric Auger if (!tg) { 12461ea8a6f5SMostafa Saleh trace_smmuv3_range_inval(vmid, asid, addr, tg, 1, ttl, leaf, stage); 124746727727SMostafa Saleh smmuv3_inv_notifiers_iova(s, asid, vmid, addr, tg, 1, stage); 12481ea8a6f5SMostafa Saleh if (stage == SMMU_STAGE_1) { 12492eaeb7d5SMostafa Saleh smmu_iotlb_inv_iova(s, asid, vmid, addr, tg, 1, ttl); 12501ea8a6f5SMostafa Saleh } else { 12511ea8a6f5SMostafa Saleh smmu_iotlb_inv_ipa(s, vmid, addr, tg, 1, ttl); 12521ea8a6f5SMostafa Saleh } 1253219729cfSEric Auger return; 1254219729cfSEric Auger } 1255219729cfSEric Auger 1256219729cfSEric Auger /* RIL in use */ 1257219729cfSEric Auger 1258219729cfSEric Auger num_pages = (num + 1) * BIT_ULL(scale); 1259219729cfSEric Auger granule = tg * 2 + 10; 1260219729cfSEric Auger 12616d9cd115SEric Auger /* Split invalidations into ^2 range invalidations */ 1262219729cfSEric Auger end = addr + (num_pages << granule) - 1; 12636d9cd115SEric Auger 1264219729cfSEric Auger while (addr != end + 1) { 1265219729cfSEric Auger uint64_t mask = dma_aligned_pow2_mask(addr, end, 64); 12666d9cd115SEric Auger 1267219729cfSEric Auger num_pages = (mask + 1) >> granule; 12681ea8a6f5SMostafa Saleh trace_smmuv3_range_inval(vmid, asid, addr, tg, num_pages, 12691ea8a6f5SMostafa Saleh ttl, leaf, stage); 127046727727SMostafa Saleh smmuv3_inv_notifiers_iova(s, asid, vmid, addr, tg, num_pages, stage); 12711ea8a6f5SMostafa Saleh if (stage == SMMU_STAGE_1) { 12722eaeb7d5SMostafa Saleh smmu_iotlb_inv_iova(s, asid, vmid, addr, tg, num_pages, ttl); 12731ea8a6f5SMostafa Saleh } else { 12741ea8a6f5SMostafa Saleh smmu_iotlb_inv_ipa(s, vmid, addr, tg, num_pages, ttl); 12751ea8a6f5SMostafa Saleh } 1276219729cfSEric Auger addr += mask + 1; 12776d9cd115SEric Auger } 1278c0f9ef70SEric Auger } 1279c0f9ef70SEric Auger 12801194140bSEric Auger static gboolean 12811194140bSEric Auger smmuv3_invalidate_ste(gpointer key, gpointer value, gpointer user_data) 12821194140bSEric Auger { 12831194140bSEric Auger SMMUDevice *sdev = (SMMUDevice *)key; 12841194140bSEric Auger uint32_t sid = smmu_get_sid(sdev); 12851194140bSEric Auger SMMUSIDRange *sid_range = (SMMUSIDRange *)user_data; 12861194140bSEric Auger 12871194140bSEric Auger if (sid < sid_range->start || sid > sid_range->end) { 12881194140bSEric Auger return false; 12891194140bSEric Auger } 12901194140bSEric Auger trace_smmuv3_config_cache_inv(sid); 12911194140bSEric Auger return true; 12921194140bSEric Auger } 12931194140bSEric Auger 1294fae4be38SEric Auger static int smmuv3_cmdq_consume(SMMUv3State *s) 1295dadd1a08SEric Auger { 129632cfd7f3SEric Auger SMMUState *bs = ARM_SMMU(s); 1297dadd1a08SEric Auger SMMUCmdError cmd_error = SMMU_CERROR_NONE; 1298dadd1a08SEric Auger SMMUQueue *q = &s->cmdq; 1299dadd1a08SEric Auger SMMUCommandType type = 0; 1300dadd1a08SEric Auger 1301dadd1a08SEric Auger if (!smmuv3_cmdq_enabled(s)) { 1302dadd1a08SEric Auger return 0; 1303dadd1a08SEric Auger } 1304dadd1a08SEric Auger /* 1305dadd1a08SEric Auger * some commands depend on register values, typically CR0. In case those 1306dadd1a08SEric Auger * register values change while handling the command, spec says it 1307dadd1a08SEric Auger * is UNPREDICTABLE whether the command is interpreted under the new 1308dadd1a08SEric Auger * or old value. 1309dadd1a08SEric Auger */ 1310dadd1a08SEric Auger 1311dadd1a08SEric Auger while (!smmuv3_q_empty(q)) { 1312dadd1a08SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 1313dadd1a08SEric Auger Cmd cmd; 1314dadd1a08SEric Auger 1315dadd1a08SEric Auger trace_smmuv3_cmdq_consume(Q_PROD(q), Q_CONS(q), 1316dadd1a08SEric Auger Q_PROD_WRAP(q), Q_CONS_WRAP(q)); 1317dadd1a08SEric Auger 1318dadd1a08SEric Auger if (FIELD_EX32(pending, GERROR, CMDQ_ERR)) { 1319dadd1a08SEric Auger break; 1320dadd1a08SEric Auger } 1321dadd1a08SEric Auger 1322dadd1a08SEric Auger if (queue_read(q, &cmd) != MEMTX_OK) { 1323dadd1a08SEric Auger cmd_error = SMMU_CERROR_ABT; 1324dadd1a08SEric Auger break; 1325dadd1a08SEric Auger } 1326dadd1a08SEric Auger 1327dadd1a08SEric Auger type = CMD_TYPE(&cmd); 1328dadd1a08SEric Auger 1329dadd1a08SEric Auger trace_smmuv3_cmdq_opcode(smmu_cmd_string(type)); 1330dadd1a08SEric Auger 133132cfd7f3SEric Auger qemu_mutex_lock(&s->mutex); 1332dadd1a08SEric Auger switch (type) { 1333dadd1a08SEric Auger case SMMU_CMD_SYNC: 1334dadd1a08SEric Auger if (CMD_SYNC_CS(&cmd) & CMD_SYNC_SIG_IRQ) { 1335dadd1a08SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_CMD_SYNC, 0); 1336dadd1a08SEric Auger } 1337dadd1a08SEric Auger break; 1338dadd1a08SEric Auger case SMMU_CMD_PREFETCH_CONFIG: 1339dadd1a08SEric Auger case SMMU_CMD_PREFETCH_ADDR: 134032cfd7f3SEric Auger break; 1341dadd1a08SEric Auger case SMMU_CMD_CFGI_STE: 134232cfd7f3SEric Auger { 134332cfd7f3SEric Auger uint32_t sid = CMD_SID(&cmd); 134469970205SNicolin Chen SMMUDevice *sdev = smmu_find_sdev(bs, sid); 134532cfd7f3SEric Auger 134632cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 134732cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 134832cfd7f3SEric Auger break; 134932cfd7f3SEric Auger } 135032cfd7f3SEric Auger 135169970205SNicolin Chen if (!sdev) { 135232cfd7f3SEric Auger break; 135332cfd7f3SEric Auger } 135432cfd7f3SEric Auger 135532cfd7f3SEric Auger trace_smmuv3_cmdq_cfgi_ste(sid); 135632cfd7f3SEric Auger smmuv3_flush_config(sdev); 135732cfd7f3SEric Auger 135832cfd7f3SEric Auger break; 135932cfd7f3SEric Auger } 1360dadd1a08SEric Auger case SMMU_CMD_CFGI_STE_RANGE: /* same as SMMU_CMD_CFGI_ALL */ 136132cfd7f3SEric Auger { 1362017a913aSZenghui Yu uint32_t sid = CMD_SID(&cmd), mask; 136332cfd7f3SEric Auger uint8_t range = CMD_STE_RANGE(&cmd); 1364017a913aSZenghui Yu SMMUSIDRange sid_range; 136532cfd7f3SEric Auger 136632cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 136732cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 136832cfd7f3SEric Auger break; 136932cfd7f3SEric Auger } 1370017a913aSZenghui Yu 1371017a913aSZenghui Yu mask = (1ULL << (range + 1)) - 1; 1372017a913aSZenghui Yu sid_range.start = sid & ~mask; 1373017a913aSZenghui Yu sid_range.end = sid_range.start + mask; 1374017a913aSZenghui Yu 1375017a913aSZenghui Yu trace_smmuv3_cmdq_cfgi_ste_range(sid_range.start, sid_range.end); 13761194140bSEric Auger g_hash_table_foreach_remove(bs->configs, smmuv3_invalidate_ste, 13771194140bSEric Auger &sid_range); 137832cfd7f3SEric Auger break; 137932cfd7f3SEric Auger } 1380dadd1a08SEric Auger case SMMU_CMD_CFGI_CD: 1381dadd1a08SEric Auger case SMMU_CMD_CFGI_CD_ALL: 138232cfd7f3SEric Auger { 138332cfd7f3SEric Auger uint32_t sid = CMD_SID(&cmd); 138469970205SNicolin Chen SMMUDevice *sdev = smmu_find_sdev(bs, sid); 138532cfd7f3SEric Auger 138632cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 138732cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 138832cfd7f3SEric Auger break; 138932cfd7f3SEric Auger } 139032cfd7f3SEric Auger 139169970205SNicolin Chen if (!sdev) { 139232cfd7f3SEric Auger break; 139332cfd7f3SEric Auger } 139432cfd7f3SEric Auger 139532cfd7f3SEric Auger trace_smmuv3_cmdq_cfgi_cd(sid); 139632cfd7f3SEric Auger smmuv3_flush_config(sdev); 139732cfd7f3SEric Auger break; 139832cfd7f3SEric Auger } 1399dadd1a08SEric Auger case SMMU_CMD_TLBI_NH_ASID: 1400cc27ed81SEric Auger { 1401d8838226SMostafa Saleh int asid = CMD_ASID(&cmd); 1402b8fa4c23SMostafa Saleh int vmid = -1; 1403cc27ed81SEric Auger 1404ccc3ee38SMostafa Saleh if (!STAGE1_SUPPORTED(s)) { 1405ccc3ee38SMostafa Saleh cmd_error = SMMU_CERROR_ILL; 1406ccc3ee38SMostafa Saleh break; 1407ccc3ee38SMostafa Saleh } 1408ccc3ee38SMostafa Saleh 1409b8fa4c23SMostafa Saleh /* 1410b8fa4c23SMostafa Saleh * VMID is only matched when stage 2 is supported, otherwise set it 1411b8fa4c23SMostafa Saleh * to -1 as the value used for stage-1 only VMIDs. 1412b8fa4c23SMostafa Saleh */ 1413b8fa4c23SMostafa Saleh if (STAGE2_SUPPORTED(s)) { 1414b8fa4c23SMostafa Saleh vmid = CMD_VMID(&cmd); 1415b8fa4c23SMostafa Saleh } 1416b8fa4c23SMostafa Saleh 1417cc27ed81SEric Auger trace_smmuv3_cmdq_tlbi_nh_asid(asid); 1418832e4222SEric Auger smmu_inv_notifiers_all(&s->smmu_state); 1419b8fa4c23SMostafa Saleh smmu_iotlb_inv_asid_vmid(bs, asid, vmid); 1420cc27ed81SEric Auger break; 1421cc27ed81SEric Auger } 1422cc27ed81SEric Auger case SMMU_CMD_TLBI_NH_ALL: 1423b8fa4c23SMostafa Saleh { 1424b8fa4c23SMostafa Saleh int vmid = -1; 1425b8fa4c23SMostafa Saleh 1426ccc3ee38SMostafa Saleh if (!STAGE1_SUPPORTED(s)) { 1427ccc3ee38SMostafa Saleh cmd_error = SMMU_CERROR_ILL; 1428ccc3ee38SMostafa Saleh break; 1429ccc3ee38SMostafa Saleh } 1430b8fa4c23SMostafa Saleh 1431b8fa4c23SMostafa Saleh /* 1432b8fa4c23SMostafa Saleh * If stage-2 is supported, invalidate for this VMID only, otherwise 1433b8fa4c23SMostafa Saleh * invalidate the whole thing. 1434b8fa4c23SMostafa Saleh */ 1435b8fa4c23SMostafa Saleh if (STAGE2_SUPPORTED(s)) { 1436b8fa4c23SMostafa Saleh vmid = CMD_VMID(&cmd); 1437b8fa4c23SMostafa Saleh trace_smmuv3_cmdq_tlbi_nh(vmid); 1438b8fa4c23SMostafa Saleh smmu_iotlb_inv_vmid_s1(bs, vmid); 1439b8fa4c23SMostafa Saleh break; 1440b8fa4c23SMostafa Saleh } 1441ccc3ee38SMostafa Saleh QEMU_FALLTHROUGH; 1442b8fa4c23SMostafa Saleh } 1443cc27ed81SEric Auger case SMMU_CMD_TLBI_NSNH_ALL: 1444b8fa4c23SMostafa Saleh trace_smmuv3_cmdq_tlbi_nsnh(); 1445832e4222SEric Auger smmu_inv_notifiers_all(&s->smmu_state); 1446cc27ed81SEric Auger smmu_iotlb_inv_all(bs); 1447cc27ed81SEric Auger break; 1448dadd1a08SEric Auger case SMMU_CMD_TLBI_NH_VAA: 1449cc27ed81SEric Auger case SMMU_CMD_TLBI_NH_VA: 1450ccc3ee38SMostafa Saleh if (!STAGE1_SUPPORTED(s)) { 1451ccc3ee38SMostafa Saleh cmd_error = SMMU_CERROR_ILL; 1452ccc3ee38SMostafa Saleh break; 1453ccc3ee38SMostafa Saleh } 14541ea8a6f5SMostafa Saleh smmuv3_range_inval(bs, &cmd, SMMU_STAGE_1); 1455ccc3ee38SMostafa Saleh break; 1456ccc3ee38SMostafa Saleh case SMMU_CMD_TLBI_S12_VMALL: 1457ccc3ee38SMostafa Saleh { 1458d8838226SMostafa Saleh int vmid = CMD_VMID(&cmd); 1459ccc3ee38SMostafa Saleh 1460ccc3ee38SMostafa Saleh if (!STAGE2_SUPPORTED(s)) { 1461ccc3ee38SMostafa Saleh cmd_error = SMMU_CERROR_ILL; 1462ccc3ee38SMostafa Saleh break; 1463ccc3ee38SMostafa Saleh } 1464ccc3ee38SMostafa Saleh 1465ccc3ee38SMostafa Saleh trace_smmuv3_cmdq_tlbi_s12_vmid(vmid); 1466ccc3ee38SMostafa Saleh smmu_inv_notifiers_all(&s->smmu_state); 1467ccc3ee38SMostafa Saleh smmu_iotlb_inv_vmid(bs, vmid); 1468ccc3ee38SMostafa Saleh break; 1469ccc3ee38SMostafa Saleh } 1470ccc3ee38SMostafa Saleh case SMMU_CMD_TLBI_S2_IPA: 1471ccc3ee38SMostafa Saleh if (!STAGE2_SUPPORTED(s)) { 1472ccc3ee38SMostafa Saleh cmd_error = SMMU_CERROR_ILL; 1473ccc3ee38SMostafa Saleh break; 1474ccc3ee38SMostafa Saleh } 1475ccc3ee38SMostafa Saleh /* 1476ccc3ee38SMostafa Saleh * As currently only either s1 or s2 are supported 1477ccc3ee38SMostafa Saleh * we can reuse same function for s2. 1478ccc3ee38SMostafa Saleh */ 14791ea8a6f5SMostafa Saleh smmuv3_range_inval(bs, &cmd, SMMU_STAGE_2); 1480cc27ed81SEric Auger break; 1481dadd1a08SEric Auger case SMMU_CMD_TLBI_EL3_ALL: 1482dadd1a08SEric Auger case SMMU_CMD_TLBI_EL3_VA: 1483dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_ALL: 1484dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_ASID: 1485dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_VA: 1486dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_VAA: 1487dadd1a08SEric Auger case SMMU_CMD_ATC_INV: 1488dadd1a08SEric Auger case SMMU_CMD_PRI_RESP: 1489dadd1a08SEric Auger case SMMU_CMD_RESUME: 1490dadd1a08SEric Auger case SMMU_CMD_STALL_TERM: 1491dadd1a08SEric Auger trace_smmuv3_unhandled_cmd(type); 1492dadd1a08SEric Auger break; 1493dadd1a08SEric Auger default: 1494dadd1a08SEric Auger cmd_error = SMMU_CERROR_ILL; 1495dadd1a08SEric Auger break; 1496dadd1a08SEric Auger } 149732cfd7f3SEric Auger qemu_mutex_unlock(&s->mutex); 1498dadd1a08SEric Auger if (cmd_error) { 1499ccc3ee38SMostafa Saleh if (cmd_error == SMMU_CERROR_ILL) { 1500ccc3ee38SMostafa Saleh qemu_log_mask(LOG_GUEST_ERROR, 1501ccc3ee38SMostafa Saleh "Illegal command type: %d\n", CMD_TYPE(&cmd)); 1502ccc3ee38SMostafa Saleh } 1503dadd1a08SEric Auger break; 1504dadd1a08SEric Auger } 1505dadd1a08SEric Auger /* 1506dadd1a08SEric Auger * We only increment the cons index after the completion of 1507dadd1a08SEric Auger * the command. We do that because the SYNC returns immediately 1508dadd1a08SEric Auger * and does not check the completion of previous commands 1509dadd1a08SEric Auger */ 1510dadd1a08SEric Auger queue_cons_incr(q); 1511dadd1a08SEric Auger } 1512dadd1a08SEric Auger 1513dadd1a08SEric Auger if (cmd_error) { 1514dadd1a08SEric Auger trace_smmuv3_cmdq_consume_error(smmu_cmd_string(type), cmd_error); 1515dadd1a08SEric Auger smmu_write_cmdq_err(s, cmd_error); 1516dadd1a08SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_CMDQ_ERR_MASK); 1517dadd1a08SEric Auger } 1518dadd1a08SEric Auger 1519dadd1a08SEric Auger trace_smmuv3_cmdq_consume_out(Q_PROD(q), Q_CONS(q), 1520dadd1a08SEric Auger Q_PROD_WRAP(q), Q_CONS_WRAP(q)); 1521dadd1a08SEric Auger 1522dadd1a08SEric Auger return 0; 1523dadd1a08SEric Auger } 1524dadd1a08SEric Auger 1525fae4be38SEric Auger static MemTxResult smmu_writell(SMMUv3State *s, hwaddr offset, 1526fae4be38SEric Auger uint64_t data, MemTxAttrs attrs) 1527fae4be38SEric Auger { 1528fae4be38SEric Auger switch (offset) { 1529fae4be38SEric Auger case A_GERROR_IRQ_CFG0: 1530fae4be38SEric Auger s->gerror_irq_cfg0 = data; 1531fae4be38SEric Auger return MEMTX_OK; 1532fae4be38SEric Auger case A_STRTAB_BASE: 1533fae4be38SEric Auger s->strtab_base = data; 1534fae4be38SEric Auger return MEMTX_OK; 1535fae4be38SEric Auger case A_CMDQ_BASE: 1536fae4be38SEric Auger s->cmdq.base = data; 1537fae4be38SEric Auger s->cmdq.log2size = extract64(s->cmdq.base, 0, 5); 1538fae4be38SEric Auger if (s->cmdq.log2size > SMMU_CMDQS) { 1539fae4be38SEric Auger s->cmdq.log2size = SMMU_CMDQS; 1540fae4be38SEric Auger } 1541fae4be38SEric Auger return MEMTX_OK; 1542fae4be38SEric Auger case A_EVENTQ_BASE: 1543fae4be38SEric Auger s->eventq.base = data; 1544fae4be38SEric Auger s->eventq.log2size = extract64(s->eventq.base, 0, 5); 1545fae4be38SEric Auger if (s->eventq.log2size > SMMU_EVENTQS) { 1546fae4be38SEric Auger s->eventq.log2size = SMMU_EVENTQS; 1547fae4be38SEric Auger } 1548fae4be38SEric Auger return MEMTX_OK; 1549fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0: 1550fae4be38SEric Auger s->eventq_irq_cfg0 = data; 1551fae4be38SEric Auger return MEMTX_OK; 1552fae4be38SEric Auger default: 1553fae4be38SEric Auger qemu_log_mask(LOG_UNIMP, 1554fae4be38SEric Auger "%s Unexpected 64-bit access to 0x%"PRIx64" (WI)\n", 1555fae4be38SEric Auger __func__, offset); 1556fae4be38SEric Auger return MEMTX_OK; 1557fae4be38SEric Auger } 1558fae4be38SEric Auger } 1559fae4be38SEric Auger 1560fae4be38SEric Auger static MemTxResult smmu_writel(SMMUv3State *s, hwaddr offset, 1561fae4be38SEric Auger uint64_t data, MemTxAttrs attrs) 1562fae4be38SEric Auger { 1563fae4be38SEric Auger switch (offset) { 1564fae4be38SEric Auger case A_CR0: 1565fae4be38SEric Auger s->cr[0] = data; 1566fae4be38SEric Auger s->cr0ack = data & ~SMMU_CR0_RESERVED; 1567fae4be38SEric Auger /* in case the command queue has been enabled */ 1568fae4be38SEric Auger smmuv3_cmdq_consume(s); 1569fae4be38SEric Auger return MEMTX_OK; 1570fae4be38SEric Auger case A_CR1: 1571fae4be38SEric Auger s->cr[1] = data; 1572fae4be38SEric Auger return MEMTX_OK; 1573fae4be38SEric Auger case A_CR2: 1574fae4be38SEric Auger s->cr[2] = data; 1575fae4be38SEric Auger return MEMTX_OK; 1576fae4be38SEric Auger case A_IRQ_CTRL: 1577fae4be38SEric Auger s->irq_ctrl = data; 1578fae4be38SEric Auger return MEMTX_OK; 1579fae4be38SEric Auger case A_GERRORN: 1580fae4be38SEric Auger smmuv3_write_gerrorn(s, data); 1581fae4be38SEric Auger /* 1582fae4be38SEric Auger * By acknowledging the CMDQ_ERR, SW may notify cmds can 1583fae4be38SEric Auger * be processed again 1584fae4be38SEric Auger */ 1585fae4be38SEric Auger smmuv3_cmdq_consume(s); 1586fae4be38SEric Auger return MEMTX_OK; 1587fae4be38SEric Auger case A_GERROR_IRQ_CFG0: /* 64b */ 1588fae4be38SEric Auger s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 0, 32, data); 1589fae4be38SEric Auger return MEMTX_OK; 1590fae4be38SEric Auger case A_GERROR_IRQ_CFG0 + 4: 1591fae4be38SEric Auger s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 32, 32, data); 1592fae4be38SEric Auger return MEMTX_OK; 1593fae4be38SEric Auger case A_GERROR_IRQ_CFG1: 1594fae4be38SEric Auger s->gerror_irq_cfg1 = data; 1595fae4be38SEric Auger return MEMTX_OK; 1596fae4be38SEric Auger case A_GERROR_IRQ_CFG2: 1597fae4be38SEric Auger s->gerror_irq_cfg2 = data; 1598fae4be38SEric Auger return MEMTX_OK; 1599c2ecb424SMostafa Saleh case A_GBPA: 1600c2ecb424SMostafa Saleh /* 1601c2ecb424SMostafa Saleh * If UPDATE is not set, the write is ignored. This is the only 1602c2ecb424SMostafa Saleh * permitted behavior in SMMUv3.2 and later. 1603c2ecb424SMostafa Saleh */ 1604c2ecb424SMostafa Saleh if (data & R_GBPA_UPDATE_MASK) { 1605c2ecb424SMostafa Saleh /* Ignore update bit as write is synchronous. */ 1606c2ecb424SMostafa Saleh s->gbpa = data & ~R_GBPA_UPDATE_MASK; 1607c2ecb424SMostafa Saleh } 1608c2ecb424SMostafa Saleh return MEMTX_OK; 1609fae4be38SEric Auger case A_STRTAB_BASE: /* 64b */ 1610fae4be38SEric Auger s->strtab_base = deposit64(s->strtab_base, 0, 32, data); 1611fae4be38SEric Auger return MEMTX_OK; 1612fae4be38SEric Auger case A_STRTAB_BASE + 4: 1613fae4be38SEric Auger s->strtab_base = deposit64(s->strtab_base, 32, 32, data); 1614fae4be38SEric Auger return MEMTX_OK; 1615fae4be38SEric Auger case A_STRTAB_BASE_CFG: 1616fae4be38SEric Auger s->strtab_base_cfg = data; 1617fae4be38SEric Auger if (FIELD_EX32(data, STRTAB_BASE_CFG, FMT) == 1) { 1618fae4be38SEric Auger s->sid_split = FIELD_EX32(data, STRTAB_BASE_CFG, SPLIT); 1619fae4be38SEric Auger s->features |= SMMU_FEATURE_2LVL_STE; 1620fae4be38SEric Auger } 1621fae4be38SEric Auger return MEMTX_OK; 1622fae4be38SEric Auger case A_CMDQ_BASE: /* 64b */ 1623fae4be38SEric Auger s->cmdq.base = deposit64(s->cmdq.base, 0, 32, data); 1624fae4be38SEric Auger s->cmdq.log2size = extract64(s->cmdq.base, 0, 5); 1625fae4be38SEric Auger if (s->cmdq.log2size > SMMU_CMDQS) { 1626fae4be38SEric Auger s->cmdq.log2size = SMMU_CMDQS; 1627fae4be38SEric Auger } 1628fae4be38SEric Auger return MEMTX_OK; 1629fae4be38SEric Auger case A_CMDQ_BASE + 4: /* 64b */ 1630fae4be38SEric Auger s->cmdq.base = deposit64(s->cmdq.base, 32, 32, data); 1631fae4be38SEric Auger return MEMTX_OK; 1632fae4be38SEric Auger case A_CMDQ_PROD: 1633fae4be38SEric Auger s->cmdq.prod = data; 1634fae4be38SEric Auger smmuv3_cmdq_consume(s); 1635fae4be38SEric Auger return MEMTX_OK; 1636fae4be38SEric Auger case A_CMDQ_CONS: 1637fae4be38SEric Auger s->cmdq.cons = data; 1638fae4be38SEric Auger return MEMTX_OK; 1639fae4be38SEric Auger case A_EVENTQ_BASE: /* 64b */ 1640fae4be38SEric Auger s->eventq.base = deposit64(s->eventq.base, 0, 32, data); 1641fae4be38SEric Auger s->eventq.log2size = extract64(s->eventq.base, 0, 5); 1642fae4be38SEric Auger if (s->eventq.log2size > SMMU_EVENTQS) { 1643fae4be38SEric Auger s->eventq.log2size = SMMU_EVENTQS; 1644fae4be38SEric Auger } 1645fae4be38SEric Auger return MEMTX_OK; 1646fae4be38SEric Auger case A_EVENTQ_BASE + 4: 1647fae4be38SEric Auger s->eventq.base = deposit64(s->eventq.base, 32, 32, data); 1648fae4be38SEric Auger return MEMTX_OK; 1649fae4be38SEric Auger case A_EVENTQ_PROD: 1650fae4be38SEric Auger s->eventq.prod = data; 1651fae4be38SEric Auger return MEMTX_OK; 1652fae4be38SEric Auger case A_EVENTQ_CONS: 1653fae4be38SEric Auger s->eventq.cons = data; 1654fae4be38SEric Auger return MEMTX_OK; 1655fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0: /* 64b */ 1656fae4be38SEric Auger s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 0, 32, data); 1657fae4be38SEric Auger return MEMTX_OK; 1658fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0 + 4: 1659fae4be38SEric Auger s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 32, 32, data); 1660fae4be38SEric Auger return MEMTX_OK; 1661fae4be38SEric Auger case A_EVENTQ_IRQ_CFG1: 1662fae4be38SEric Auger s->eventq_irq_cfg1 = data; 1663fae4be38SEric Auger return MEMTX_OK; 1664fae4be38SEric Auger case A_EVENTQ_IRQ_CFG2: 1665fae4be38SEric Auger s->eventq_irq_cfg2 = data; 1666fae4be38SEric Auger return MEMTX_OK; 1667fae4be38SEric Auger default: 1668fae4be38SEric Auger qemu_log_mask(LOG_UNIMP, 1669fae4be38SEric Auger "%s Unexpected 32-bit access to 0x%"PRIx64" (WI)\n", 1670fae4be38SEric Auger __func__, offset); 1671fae4be38SEric Auger return MEMTX_OK; 1672fae4be38SEric Auger } 1673fae4be38SEric Auger } 1674fae4be38SEric Auger 167510a83cb9SPrem Mallappa static MemTxResult smmu_write_mmio(void *opaque, hwaddr offset, uint64_t data, 167610a83cb9SPrem Mallappa unsigned size, MemTxAttrs attrs) 167710a83cb9SPrem Mallappa { 1678fae4be38SEric Auger SMMUState *sys = opaque; 1679fae4be38SEric Auger SMMUv3State *s = ARM_SMMUV3(sys); 1680fae4be38SEric Auger MemTxResult r; 1681fae4be38SEric Auger 1682fae4be38SEric Auger /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */ 1683fae4be38SEric Auger offset &= ~0x10000; 1684fae4be38SEric Auger 1685fae4be38SEric Auger switch (size) { 1686fae4be38SEric Auger case 8: 1687fae4be38SEric Auger r = smmu_writell(s, offset, data, attrs); 1688fae4be38SEric Auger break; 1689fae4be38SEric Auger case 4: 1690fae4be38SEric Auger r = smmu_writel(s, offset, data, attrs); 1691fae4be38SEric Auger break; 1692fae4be38SEric Auger default: 1693fae4be38SEric Auger r = MEMTX_ERROR; 1694fae4be38SEric Auger break; 1695fae4be38SEric Auger } 1696fae4be38SEric Auger 1697fae4be38SEric Auger trace_smmuv3_write_mmio(offset, data, size, r); 1698fae4be38SEric Auger return r; 169910a83cb9SPrem Mallappa } 170010a83cb9SPrem Mallappa 170110a83cb9SPrem Mallappa static MemTxResult smmu_readll(SMMUv3State *s, hwaddr offset, 170210a83cb9SPrem Mallappa uint64_t *data, MemTxAttrs attrs) 170310a83cb9SPrem Mallappa { 170410a83cb9SPrem Mallappa switch (offset) { 170510a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0: 170610a83cb9SPrem Mallappa *data = s->gerror_irq_cfg0; 170710a83cb9SPrem Mallappa return MEMTX_OK; 170810a83cb9SPrem Mallappa case A_STRTAB_BASE: 170910a83cb9SPrem Mallappa *data = s->strtab_base; 171010a83cb9SPrem Mallappa return MEMTX_OK; 171110a83cb9SPrem Mallappa case A_CMDQ_BASE: 171210a83cb9SPrem Mallappa *data = s->cmdq.base; 171310a83cb9SPrem Mallappa return MEMTX_OK; 171410a83cb9SPrem Mallappa case A_EVENTQ_BASE: 171510a83cb9SPrem Mallappa *data = s->eventq.base; 171610a83cb9SPrem Mallappa return MEMTX_OK; 171710a83cb9SPrem Mallappa default: 171810a83cb9SPrem Mallappa *data = 0; 171910a83cb9SPrem Mallappa qemu_log_mask(LOG_UNIMP, 172010a83cb9SPrem Mallappa "%s Unexpected 64-bit access to 0x%"PRIx64" (RAZ)\n", 172110a83cb9SPrem Mallappa __func__, offset); 172210a83cb9SPrem Mallappa return MEMTX_OK; 172310a83cb9SPrem Mallappa } 172410a83cb9SPrem Mallappa } 172510a83cb9SPrem Mallappa 172610a83cb9SPrem Mallappa static MemTxResult smmu_readl(SMMUv3State *s, hwaddr offset, 172710a83cb9SPrem Mallappa uint64_t *data, MemTxAttrs attrs) 172810a83cb9SPrem Mallappa { 172910a83cb9SPrem Mallappa switch (offset) { 173097fb318dSPeter Maydell case A_IDREGS ... A_IDREGS + 0x2f: 173110a83cb9SPrem Mallappa *data = smmuv3_idreg(offset - A_IDREGS); 173210a83cb9SPrem Mallappa return MEMTX_OK; 173310a83cb9SPrem Mallappa case A_IDR0 ... A_IDR5: 173410a83cb9SPrem Mallappa *data = s->idr[(offset - A_IDR0) / 4]; 173510a83cb9SPrem Mallappa return MEMTX_OK; 173610a83cb9SPrem Mallappa case A_IIDR: 173710a83cb9SPrem Mallappa *data = s->iidr; 173810a83cb9SPrem Mallappa return MEMTX_OK; 17395888f0adSEric Auger case A_AIDR: 17405888f0adSEric Auger *data = s->aidr; 17415888f0adSEric Auger return MEMTX_OK; 174210a83cb9SPrem Mallappa case A_CR0: 174310a83cb9SPrem Mallappa *data = s->cr[0]; 174410a83cb9SPrem Mallappa return MEMTX_OK; 174510a83cb9SPrem Mallappa case A_CR0ACK: 174610a83cb9SPrem Mallappa *data = s->cr0ack; 174710a83cb9SPrem Mallappa return MEMTX_OK; 174810a83cb9SPrem Mallappa case A_CR1: 174910a83cb9SPrem Mallappa *data = s->cr[1]; 175010a83cb9SPrem Mallappa return MEMTX_OK; 175110a83cb9SPrem Mallappa case A_CR2: 175210a83cb9SPrem Mallappa *data = s->cr[2]; 175310a83cb9SPrem Mallappa return MEMTX_OK; 175410a83cb9SPrem Mallappa case A_STATUSR: 175510a83cb9SPrem Mallappa *data = s->statusr; 175610a83cb9SPrem Mallappa return MEMTX_OK; 1757c2ecb424SMostafa Saleh case A_GBPA: 1758c2ecb424SMostafa Saleh *data = s->gbpa; 1759c2ecb424SMostafa Saleh return MEMTX_OK; 176010a83cb9SPrem Mallappa case A_IRQ_CTRL: 176110a83cb9SPrem Mallappa case A_IRQ_CTRL_ACK: 176210a83cb9SPrem Mallappa *data = s->irq_ctrl; 176310a83cb9SPrem Mallappa return MEMTX_OK; 176410a83cb9SPrem Mallappa case A_GERROR: 176510a83cb9SPrem Mallappa *data = s->gerror; 176610a83cb9SPrem Mallappa return MEMTX_OK; 176710a83cb9SPrem Mallappa case A_GERRORN: 176810a83cb9SPrem Mallappa *data = s->gerrorn; 176910a83cb9SPrem Mallappa return MEMTX_OK; 177010a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0: /* 64b */ 177110a83cb9SPrem Mallappa *data = extract64(s->gerror_irq_cfg0, 0, 32); 177210a83cb9SPrem Mallappa return MEMTX_OK; 177310a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0 + 4: 177410a83cb9SPrem Mallappa *data = extract64(s->gerror_irq_cfg0, 32, 32); 177510a83cb9SPrem Mallappa return MEMTX_OK; 177610a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG1: 177710a83cb9SPrem Mallappa *data = s->gerror_irq_cfg1; 177810a83cb9SPrem Mallappa return MEMTX_OK; 177910a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG2: 178010a83cb9SPrem Mallappa *data = s->gerror_irq_cfg2; 178110a83cb9SPrem Mallappa return MEMTX_OK; 178210a83cb9SPrem Mallappa case A_STRTAB_BASE: /* 64b */ 178310a83cb9SPrem Mallappa *data = extract64(s->strtab_base, 0, 32); 178410a83cb9SPrem Mallappa return MEMTX_OK; 178510a83cb9SPrem Mallappa case A_STRTAB_BASE + 4: /* 64b */ 178610a83cb9SPrem Mallappa *data = extract64(s->strtab_base, 32, 32); 178710a83cb9SPrem Mallappa return MEMTX_OK; 178810a83cb9SPrem Mallappa case A_STRTAB_BASE_CFG: 178910a83cb9SPrem Mallappa *data = s->strtab_base_cfg; 179010a83cb9SPrem Mallappa return MEMTX_OK; 179110a83cb9SPrem Mallappa case A_CMDQ_BASE: /* 64b */ 179210a83cb9SPrem Mallappa *data = extract64(s->cmdq.base, 0, 32); 179310a83cb9SPrem Mallappa return MEMTX_OK; 179410a83cb9SPrem Mallappa case A_CMDQ_BASE + 4: 179510a83cb9SPrem Mallappa *data = extract64(s->cmdq.base, 32, 32); 179610a83cb9SPrem Mallappa return MEMTX_OK; 179710a83cb9SPrem Mallappa case A_CMDQ_PROD: 179810a83cb9SPrem Mallappa *data = s->cmdq.prod; 179910a83cb9SPrem Mallappa return MEMTX_OK; 180010a83cb9SPrem Mallappa case A_CMDQ_CONS: 180110a83cb9SPrem Mallappa *data = s->cmdq.cons; 180210a83cb9SPrem Mallappa return MEMTX_OK; 180310a83cb9SPrem Mallappa case A_EVENTQ_BASE: /* 64b */ 180410a83cb9SPrem Mallappa *data = extract64(s->eventq.base, 0, 32); 180510a83cb9SPrem Mallappa return MEMTX_OK; 180610a83cb9SPrem Mallappa case A_EVENTQ_BASE + 4: /* 64b */ 180710a83cb9SPrem Mallappa *data = extract64(s->eventq.base, 32, 32); 180810a83cb9SPrem Mallappa return MEMTX_OK; 180910a83cb9SPrem Mallappa case A_EVENTQ_PROD: 181010a83cb9SPrem Mallappa *data = s->eventq.prod; 181110a83cb9SPrem Mallappa return MEMTX_OK; 181210a83cb9SPrem Mallappa case A_EVENTQ_CONS: 181310a83cb9SPrem Mallappa *data = s->eventq.cons; 181410a83cb9SPrem Mallappa return MEMTX_OK; 181510a83cb9SPrem Mallappa default: 181610a83cb9SPrem Mallappa *data = 0; 181710a83cb9SPrem Mallappa qemu_log_mask(LOG_UNIMP, 181810a83cb9SPrem Mallappa "%s unhandled 32-bit access at 0x%"PRIx64" (RAZ)\n", 181910a83cb9SPrem Mallappa __func__, offset); 182010a83cb9SPrem Mallappa return MEMTX_OK; 182110a83cb9SPrem Mallappa } 182210a83cb9SPrem Mallappa } 182310a83cb9SPrem Mallappa 182410a83cb9SPrem Mallappa static MemTxResult smmu_read_mmio(void *opaque, hwaddr offset, uint64_t *data, 182510a83cb9SPrem Mallappa unsigned size, MemTxAttrs attrs) 182610a83cb9SPrem Mallappa { 182710a83cb9SPrem Mallappa SMMUState *sys = opaque; 182810a83cb9SPrem Mallappa SMMUv3State *s = ARM_SMMUV3(sys); 182910a83cb9SPrem Mallappa MemTxResult r; 183010a83cb9SPrem Mallappa 183110a83cb9SPrem Mallappa /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */ 183210a83cb9SPrem Mallappa offset &= ~0x10000; 183310a83cb9SPrem Mallappa 183410a83cb9SPrem Mallappa switch (size) { 183510a83cb9SPrem Mallappa case 8: 183610a83cb9SPrem Mallappa r = smmu_readll(s, offset, data, attrs); 183710a83cb9SPrem Mallappa break; 183810a83cb9SPrem Mallappa case 4: 183910a83cb9SPrem Mallappa r = smmu_readl(s, offset, data, attrs); 184010a83cb9SPrem Mallappa break; 184110a83cb9SPrem Mallappa default: 184210a83cb9SPrem Mallappa r = MEMTX_ERROR; 184310a83cb9SPrem Mallappa break; 184410a83cb9SPrem Mallappa } 184510a83cb9SPrem Mallappa 184610a83cb9SPrem Mallappa trace_smmuv3_read_mmio(offset, *data, size, r); 184710a83cb9SPrem Mallappa return r; 184810a83cb9SPrem Mallappa } 184910a83cb9SPrem Mallappa 185010a83cb9SPrem Mallappa static const MemoryRegionOps smmu_mem_ops = { 185110a83cb9SPrem Mallappa .read_with_attrs = smmu_read_mmio, 185210a83cb9SPrem Mallappa .write_with_attrs = smmu_write_mmio, 185310a83cb9SPrem Mallappa .endianness = DEVICE_LITTLE_ENDIAN, 185410a83cb9SPrem Mallappa .valid = { 185510a83cb9SPrem Mallappa .min_access_size = 4, 185610a83cb9SPrem Mallappa .max_access_size = 8, 185710a83cb9SPrem Mallappa }, 185810a83cb9SPrem Mallappa .impl = { 185910a83cb9SPrem Mallappa .min_access_size = 4, 186010a83cb9SPrem Mallappa .max_access_size = 8, 186110a83cb9SPrem Mallappa }, 186210a83cb9SPrem Mallappa }; 186310a83cb9SPrem Mallappa 186410a83cb9SPrem Mallappa static void smmu_init_irq(SMMUv3State *s, SysBusDevice *dev) 186510a83cb9SPrem Mallappa { 186610a83cb9SPrem Mallappa int i; 186710a83cb9SPrem Mallappa 186810a83cb9SPrem Mallappa for (i = 0; i < ARRAY_SIZE(s->irq); i++) { 186910a83cb9SPrem Mallappa sysbus_init_irq(dev, &s->irq[i]); 187010a83cb9SPrem Mallappa } 187110a83cb9SPrem Mallappa } 187210a83cb9SPrem Mallappa 1873ad80e367SPeter Maydell static void smmu_reset_hold(Object *obj, ResetType type) 187410a83cb9SPrem Mallappa { 1875503819a3SPeter Maydell SMMUv3State *s = ARM_SMMUV3(obj); 187610a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s); 187710a83cb9SPrem Mallappa 1878503819a3SPeter Maydell if (c->parent_phases.hold) { 1879ad80e367SPeter Maydell c->parent_phases.hold(obj, type); 1880503819a3SPeter Maydell } 188110a83cb9SPrem Mallappa 188210a83cb9SPrem Mallappa smmuv3_init_regs(s); 188310a83cb9SPrem Mallappa } 188410a83cb9SPrem Mallappa 188510a83cb9SPrem Mallappa static void smmu_realize(DeviceState *d, Error **errp) 188610a83cb9SPrem Mallappa { 188710a83cb9SPrem Mallappa SMMUState *sys = ARM_SMMU(d); 188810a83cb9SPrem Mallappa SMMUv3State *s = ARM_SMMUV3(sys); 188910a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s); 189010a83cb9SPrem Mallappa SysBusDevice *dev = SYS_BUS_DEVICE(d); 189110a83cb9SPrem Mallappa Error *local_err = NULL; 189210a83cb9SPrem Mallappa 189310a83cb9SPrem Mallappa c->parent_realize(d, &local_err); 189410a83cb9SPrem Mallappa if (local_err) { 189510a83cb9SPrem Mallappa error_propagate(errp, local_err); 189610a83cb9SPrem Mallappa return; 189710a83cb9SPrem Mallappa } 189810a83cb9SPrem Mallappa 189932cfd7f3SEric Auger qemu_mutex_init(&s->mutex); 190032cfd7f3SEric Auger 190110a83cb9SPrem Mallappa memory_region_init_io(&sys->iomem, OBJECT(s), 190210a83cb9SPrem Mallappa &smmu_mem_ops, sys, TYPE_ARM_SMMUV3, 0x20000); 190310a83cb9SPrem Mallappa 190410a83cb9SPrem Mallappa sys->mrtypename = TYPE_SMMUV3_IOMMU_MEMORY_REGION; 190510a83cb9SPrem Mallappa 190610a83cb9SPrem Mallappa sysbus_init_mmio(dev, &sys->iomem); 190710a83cb9SPrem Mallappa 190810a83cb9SPrem Mallappa smmu_init_irq(s, dev); 190910a83cb9SPrem Mallappa } 191010a83cb9SPrem Mallappa 191110a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3_queue = { 191210a83cb9SPrem Mallappa .name = "smmuv3_queue", 191310a83cb9SPrem Mallappa .version_id = 1, 191410a83cb9SPrem Mallappa .minimum_version_id = 1, 1915607ef570SRichard Henderson .fields = (const VMStateField[]) { 191610a83cb9SPrem Mallappa VMSTATE_UINT64(base, SMMUQueue), 191710a83cb9SPrem Mallappa VMSTATE_UINT32(prod, SMMUQueue), 191810a83cb9SPrem Mallappa VMSTATE_UINT32(cons, SMMUQueue), 191910a83cb9SPrem Mallappa VMSTATE_UINT8(log2size, SMMUQueue), 1920758b71f7SDr. David Alan Gilbert VMSTATE_END_OF_LIST(), 192110a83cb9SPrem Mallappa }, 192210a83cb9SPrem Mallappa }; 192310a83cb9SPrem Mallappa 1924c2ecb424SMostafa Saleh static bool smmuv3_gbpa_needed(void *opaque) 1925c2ecb424SMostafa Saleh { 1926c2ecb424SMostafa Saleh SMMUv3State *s = opaque; 1927c2ecb424SMostafa Saleh 1928c2ecb424SMostafa Saleh /* Only migrate GBPA if it has different reset value. */ 1929c2ecb424SMostafa Saleh return s->gbpa != SMMU_GBPA_RESET_VAL; 1930c2ecb424SMostafa Saleh } 1931c2ecb424SMostafa Saleh 1932c2ecb424SMostafa Saleh static const VMStateDescription vmstate_gbpa = { 1933c2ecb424SMostafa Saleh .name = "smmuv3/gbpa", 1934c2ecb424SMostafa Saleh .version_id = 1, 1935c2ecb424SMostafa Saleh .minimum_version_id = 1, 1936c2ecb424SMostafa Saleh .needed = smmuv3_gbpa_needed, 1937607ef570SRichard Henderson .fields = (const VMStateField[]) { 1938c2ecb424SMostafa Saleh VMSTATE_UINT32(gbpa, SMMUv3State), 1939c2ecb424SMostafa Saleh VMSTATE_END_OF_LIST() 1940c2ecb424SMostafa Saleh } 1941c2ecb424SMostafa Saleh }; 1942c2ecb424SMostafa Saleh 194310a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3 = { 194410a83cb9SPrem Mallappa .name = "smmuv3", 194510a83cb9SPrem Mallappa .version_id = 1, 194610a83cb9SPrem Mallappa .minimum_version_id = 1, 1947a55aab61SZenghui Yu .priority = MIG_PRI_IOMMU, 1948607ef570SRichard Henderson .fields = (const VMStateField[]) { 194910a83cb9SPrem Mallappa VMSTATE_UINT32(features, SMMUv3State), 195010a83cb9SPrem Mallappa VMSTATE_UINT8(sid_size, SMMUv3State), 195110a83cb9SPrem Mallappa VMSTATE_UINT8(sid_split, SMMUv3State), 195210a83cb9SPrem Mallappa 195310a83cb9SPrem Mallappa VMSTATE_UINT32_ARRAY(cr, SMMUv3State, 3), 195410a83cb9SPrem Mallappa VMSTATE_UINT32(cr0ack, SMMUv3State), 195510a83cb9SPrem Mallappa VMSTATE_UINT32(statusr, SMMUv3State), 195610a83cb9SPrem Mallappa VMSTATE_UINT32(irq_ctrl, SMMUv3State), 195710a83cb9SPrem Mallappa VMSTATE_UINT32(gerror, SMMUv3State), 195810a83cb9SPrem Mallappa VMSTATE_UINT32(gerrorn, SMMUv3State), 195910a83cb9SPrem Mallappa VMSTATE_UINT64(gerror_irq_cfg0, SMMUv3State), 196010a83cb9SPrem Mallappa VMSTATE_UINT32(gerror_irq_cfg1, SMMUv3State), 196110a83cb9SPrem Mallappa VMSTATE_UINT32(gerror_irq_cfg2, SMMUv3State), 196210a83cb9SPrem Mallappa VMSTATE_UINT64(strtab_base, SMMUv3State), 196310a83cb9SPrem Mallappa VMSTATE_UINT32(strtab_base_cfg, SMMUv3State), 196410a83cb9SPrem Mallappa VMSTATE_UINT64(eventq_irq_cfg0, SMMUv3State), 196510a83cb9SPrem Mallappa VMSTATE_UINT32(eventq_irq_cfg1, SMMUv3State), 196610a83cb9SPrem Mallappa VMSTATE_UINT32(eventq_irq_cfg2, SMMUv3State), 196710a83cb9SPrem Mallappa 196810a83cb9SPrem Mallappa VMSTATE_STRUCT(cmdq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue), 196910a83cb9SPrem Mallappa VMSTATE_STRUCT(eventq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue), 197010a83cb9SPrem Mallappa 197110a83cb9SPrem Mallappa VMSTATE_END_OF_LIST(), 197210a83cb9SPrem Mallappa }, 1973607ef570SRichard Henderson .subsections = (const VMStateDescription * const []) { 1974c2ecb424SMostafa Saleh &vmstate_gbpa, 1975c2ecb424SMostafa Saleh NULL 1976c2ecb424SMostafa Saleh } 197710a83cb9SPrem Mallappa }; 197810a83cb9SPrem Mallappa 19798cefcc3bSMostafa Saleh static Property smmuv3_properties[] = { 19808cefcc3bSMostafa Saleh /* 19818cefcc3bSMostafa Saleh * Stages of translation advertised. 19828cefcc3bSMostafa Saleh * "1": Stage 1 19838cefcc3bSMostafa Saleh * "2": Stage 2 198444df6d06SPeter Maydell * "nested": Both stage 1 and stage 2 19858cefcc3bSMostafa Saleh * Defaults to stage 1 19868cefcc3bSMostafa Saleh */ 19878cefcc3bSMostafa Saleh DEFINE_PROP_STRING("stage", SMMUv3State, stage), 19888cefcc3bSMostafa Saleh DEFINE_PROP_END_OF_LIST() 19898cefcc3bSMostafa Saleh }; 19908cefcc3bSMostafa Saleh 199110a83cb9SPrem Mallappa static void smmuv3_instance_init(Object *obj) 199210a83cb9SPrem Mallappa { 199310a83cb9SPrem Mallappa /* Nothing much to do here as of now */ 199410a83cb9SPrem Mallappa } 199510a83cb9SPrem Mallappa 199610a83cb9SPrem Mallappa static void smmuv3_class_init(ObjectClass *klass, void *data) 199710a83cb9SPrem Mallappa { 199810a83cb9SPrem Mallappa DeviceClass *dc = DEVICE_CLASS(klass); 1999503819a3SPeter Maydell ResettableClass *rc = RESETTABLE_CLASS(klass); 200010a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_CLASS(klass); 200110a83cb9SPrem Mallappa 200210a83cb9SPrem Mallappa dc->vmsd = &vmstate_smmuv3; 2003503819a3SPeter Maydell resettable_class_set_parent_phases(rc, NULL, smmu_reset_hold, NULL, 2004503819a3SPeter Maydell &c->parent_phases); 20059953bf34SZhao Liu device_class_set_parent_realize(dc, smmu_realize, 20069953bf34SZhao Liu &c->parent_realize); 20078cefcc3bSMostafa Saleh device_class_set_props(dc, smmuv3_properties); 200810a83cb9SPrem Mallappa } 200910a83cb9SPrem Mallappa 2010549d4005SEric Auger static int smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu, 20110d1ac82eSEric Auger IOMMUNotifierFlag old, 2012549d4005SEric Auger IOMMUNotifierFlag new, 2013549d4005SEric Auger Error **errp) 20140d1ac82eSEric Auger { 2015832e4222SEric Auger SMMUDevice *sdev = container_of(iommu, SMMUDevice, iommu); 2016832e4222SEric Auger SMMUv3State *s3 = sdev->smmu; 2017832e4222SEric Auger SMMUState *s = &(s3->smmu_state); 2018832e4222SEric Auger 2019958ec334SPeter Xu if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP) { 2020958ec334SPeter Xu error_setg(errp, "SMMUv3 does not support dev-iotlb yet"); 2021958ec334SPeter Xu return -EINVAL; 2022958ec334SPeter Xu } 2023958ec334SPeter Xu 2024832e4222SEric Auger if (new & IOMMU_NOTIFIER_MAP) { 2025549d4005SEric Auger error_setg(errp, 2026549d4005SEric Auger "device %02x.%02x.%x requires iommu MAP notifier which is " 2027549d4005SEric Auger "not currently supported", pci_bus_num(sdev->bus), 2028549d4005SEric Auger PCI_SLOT(sdev->devfn), PCI_FUNC(sdev->devfn)); 2029549d4005SEric Auger return -EINVAL; 2030832e4222SEric Auger } 2031832e4222SEric Auger 20320d1ac82eSEric Auger if (old == IOMMU_NOTIFIER_NONE) { 2033832e4222SEric Auger trace_smmuv3_notify_flag_add(iommu->parent_obj.name); 2034c6370441SEric Auger QLIST_INSERT_HEAD(&s->devices_with_notifiers, sdev, next); 2035c6370441SEric Auger } else if (new == IOMMU_NOTIFIER_NONE) { 2036832e4222SEric Auger trace_smmuv3_notify_flag_del(iommu->parent_obj.name); 2037c6370441SEric Auger QLIST_REMOVE(sdev, next); 20380d1ac82eSEric Auger } 2039549d4005SEric Auger return 0; 20400d1ac82eSEric Auger } 20410d1ac82eSEric Auger 204210a83cb9SPrem Mallappa static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass, 204310a83cb9SPrem Mallappa void *data) 204410a83cb9SPrem Mallappa { 20459bde7f06SEric Auger IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass); 20469bde7f06SEric Auger 20479bde7f06SEric Auger imrc->translate = smmuv3_translate; 20480d1ac82eSEric Auger imrc->notify_flag_changed = smmuv3_notify_flag_changed; 204910a83cb9SPrem Mallappa } 205010a83cb9SPrem Mallappa 205110a83cb9SPrem Mallappa static const TypeInfo smmuv3_type_info = { 205210a83cb9SPrem Mallappa .name = TYPE_ARM_SMMUV3, 205310a83cb9SPrem Mallappa .parent = TYPE_ARM_SMMU, 205410a83cb9SPrem Mallappa .instance_size = sizeof(SMMUv3State), 205510a83cb9SPrem Mallappa .instance_init = smmuv3_instance_init, 205610a83cb9SPrem Mallappa .class_size = sizeof(SMMUv3Class), 205710a83cb9SPrem Mallappa .class_init = smmuv3_class_init, 205810a83cb9SPrem Mallappa }; 205910a83cb9SPrem Mallappa 206010a83cb9SPrem Mallappa static const TypeInfo smmuv3_iommu_memory_region_info = { 206110a83cb9SPrem Mallappa .parent = TYPE_IOMMU_MEMORY_REGION, 206210a83cb9SPrem Mallappa .name = TYPE_SMMUV3_IOMMU_MEMORY_REGION, 206310a83cb9SPrem Mallappa .class_init = smmuv3_iommu_memory_region_class_init, 206410a83cb9SPrem Mallappa }; 206510a83cb9SPrem Mallappa 206610a83cb9SPrem Mallappa static void smmuv3_register_types(void) 206710a83cb9SPrem Mallappa { 2068*f3456276SZhao Liu type_register_static(&smmuv3_type_info); 2069*f3456276SZhao Liu type_register_static(&smmuv3_iommu_memory_region_info); 207010a83cb9SPrem Mallappa } 207110a83cb9SPrem Mallappa 207210a83cb9SPrem Mallappa type_init(smmuv3_register_types) 207310a83cb9SPrem Mallappa 2074