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" 2410a83cb9SPrem Mallappa #include "hw/qdev-core.h" 2510a83cb9SPrem Mallappa #include "hw/pci/pci.h" 269122bea9SJia He #include "cpu.h" 2710a83cb9SPrem Mallappa #include "trace.h" 2810a83cb9SPrem Mallappa #include "qemu/log.h" 2910a83cb9SPrem Mallappa #include "qemu/error-report.h" 3010a83cb9SPrem Mallappa #include "qapi/error.h" 3110a83cb9SPrem Mallappa 3210a83cb9SPrem Mallappa #include "hw/arm/smmuv3.h" 3310a83cb9SPrem Mallappa #include "smmuv3-internal.h" 341194140bSEric Auger #include "smmu-internal.h" 3510a83cb9SPrem Mallappa 366a736033SEric Auger /** 376a736033SEric Auger * smmuv3_trigger_irq - pulse @irq if enabled and update 386a736033SEric Auger * GERROR register in case of GERROR interrupt 396a736033SEric Auger * 406a736033SEric Auger * @irq: irq type 416a736033SEric Auger * @gerror_mask: mask of gerrors to toggle (relevant if @irq is GERROR) 426a736033SEric Auger */ 43fae4be38SEric Auger static void smmuv3_trigger_irq(SMMUv3State *s, SMMUIrq irq, 44fae4be38SEric Auger uint32_t gerror_mask) 456a736033SEric Auger { 466a736033SEric Auger 476a736033SEric Auger bool pulse = false; 486a736033SEric Auger 496a736033SEric Auger switch (irq) { 506a736033SEric Auger case SMMU_IRQ_EVTQ: 516a736033SEric Auger pulse = smmuv3_eventq_irq_enabled(s); 526a736033SEric Auger break; 536a736033SEric Auger case SMMU_IRQ_PRIQ: 546a736033SEric Auger qemu_log_mask(LOG_UNIMP, "PRI not yet supported\n"); 556a736033SEric Auger break; 566a736033SEric Auger case SMMU_IRQ_CMD_SYNC: 576a736033SEric Auger pulse = true; 586a736033SEric Auger break; 596a736033SEric Auger case SMMU_IRQ_GERROR: 606a736033SEric Auger { 616a736033SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 626a736033SEric Auger uint32_t new_gerrors = ~pending & gerror_mask; 636a736033SEric Auger 646a736033SEric Auger if (!new_gerrors) { 656a736033SEric Auger /* only toggle non pending errors */ 666a736033SEric Auger return; 676a736033SEric Auger } 686a736033SEric Auger s->gerror ^= new_gerrors; 696a736033SEric Auger trace_smmuv3_write_gerror(new_gerrors, s->gerror); 706a736033SEric Auger 716a736033SEric Auger pulse = smmuv3_gerror_irq_enabled(s); 726a736033SEric Auger break; 736a736033SEric Auger } 746a736033SEric Auger } 756a736033SEric Auger if (pulse) { 766a736033SEric Auger trace_smmuv3_trigger_irq(irq); 776a736033SEric Auger qemu_irq_pulse(s->irq[irq]); 786a736033SEric Auger } 796a736033SEric Auger } 806a736033SEric Auger 81fae4be38SEric Auger static void smmuv3_write_gerrorn(SMMUv3State *s, uint32_t new_gerrorn) 826a736033SEric Auger { 836a736033SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 846a736033SEric Auger uint32_t toggled = s->gerrorn ^ new_gerrorn; 856a736033SEric Auger 866a736033SEric Auger if (toggled & ~pending) { 876a736033SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 886a736033SEric Auger "guest toggles non pending errors = 0x%x\n", 896a736033SEric Auger toggled & ~pending); 906a736033SEric Auger } 916a736033SEric Auger 926a736033SEric Auger /* 936a736033SEric Auger * We do not raise any error in case guest toggles bits corresponding 946a736033SEric Auger * to not active IRQs (CONSTRAINED UNPREDICTABLE) 956a736033SEric Auger */ 966a736033SEric Auger s->gerrorn = new_gerrorn; 976a736033SEric Auger 986a736033SEric Auger trace_smmuv3_write_gerrorn(toggled & pending, s->gerrorn); 996a736033SEric Auger } 1006a736033SEric Auger 101dadd1a08SEric Auger static inline MemTxResult queue_read(SMMUQueue *q, void *data) 102dadd1a08SEric Auger { 103dadd1a08SEric Auger dma_addr_t addr = Q_CONS_ENTRY(q); 104dadd1a08SEric Auger 105ba06fe8aSPhilippe Mathieu-Daudé return dma_memory_read(&address_space_memory, addr, data, q->entry_size, 106ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 107dadd1a08SEric Auger } 108dadd1a08SEric Auger 109dadd1a08SEric Auger static MemTxResult queue_write(SMMUQueue *q, void *data) 110dadd1a08SEric Auger { 111dadd1a08SEric Auger dma_addr_t addr = Q_PROD_ENTRY(q); 112dadd1a08SEric Auger MemTxResult ret; 113dadd1a08SEric Auger 114ba06fe8aSPhilippe Mathieu-Daudé ret = dma_memory_write(&address_space_memory, addr, data, q->entry_size, 115ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 116dadd1a08SEric Auger if (ret != MEMTX_OK) { 117dadd1a08SEric Auger return ret; 118dadd1a08SEric Auger } 119dadd1a08SEric Auger 120dadd1a08SEric Auger queue_prod_incr(q); 121dadd1a08SEric Auger return MEMTX_OK; 122dadd1a08SEric Auger } 123dadd1a08SEric Auger 124bb981004SEric Auger static MemTxResult smmuv3_write_eventq(SMMUv3State *s, Evt *evt) 125dadd1a08SEric Auger { 126dadd1a08SEric Auger SMMUQueue *q = &s->eventq; 127bb981004SEric Auger MemTxResult r; 128bb981004SEric Auger 129bb981004SEric Auger if (!smmuv3_eventq_enabled(s)) { 130bb981004SEric Auger return MEMTX_ERROR; 131bb981004SEric Auger } 132bb981004SEric Auger 133bb981004SEric Auger if (smmuv3_q_full(q)) { 134bb981004SEric Auger return MEMTX_ERROR; 135bb981004SEric Auger } 136bb981004SEric Auger 137bb981004SEric Auger r = queue_write(q, evt); 138bb981004SEric Auger if (r != MEMTX_OK) { 139bb981004SEric Auger return r; 140bb981004SEric Auger } 141bb981004SEric Auger 1429f4d2a13SEric Auger if (!smmuv3_q_empty(q)) { 143bb981004SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_EVTQ, 0); 144bb981004SEric Auger } 145bb981004SEric Auger return MEMTX_OK; 146bb981004SEric Auger } 147bb981004SEric Auger 148bb981004SEric Auger void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info) 149bb981004SEric Auger { 15024af32e0SEric Auger Evt evt = {}; 151bb981004SEric Auger MemTxResult r; 152dadd1a08SEric Auger 153dadd1a08SEric Auger if (!smmuv3_eventq_enabled(s)) { 154dadd1a08SEric Auger return; 155dadd1a08SEric Auger } 156dadd1a08SEric Auger 157bb981004SEric Auger EVT_SET_TYPE(&evt, info->type); 158bb981004SEric Auger EVT_SET_SID(&evt, info->sid); 159bb981004SEric Auger 160bb981004SEric Auger switch (info->type) { 1619122bea9SJia He case SMMU_EVT_NONE: 162dadd1a08SEric Auger return; 163bb981004SEric Auger case SMMU_EVT_F_UUT: 164bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_uut.ssid); 165bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_uut.ssv); 166bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_uut.addr); 167bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_uut.rnw); 168bb981004SEric Auger EVT_SET_PNU(&evt, info->u.f_uut.pnu); 169bb981004SEric Auger EVT_SET_IND(&evt, info->u.f_uut.ind); 170bb981004SEric Auger break; 171bb981004SEric Auger case SMMU_EVT_C_BAD_STREAMID: 172bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_streamid.ssid); 173bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_streamid.ssv); 174bb981004SEric Auger break; 175bb981004SEric Auger case SMMU_EVT_F_STE_FETCH: 176bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_ste_fetch.ssid); 177bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_ste_fetch.ssv); 178b255cafbSSimon Veith EVT_SET_ADDR2(&evt, info->u.f_ste_fetch.addr); 179bb981004SEric Auger break; 180bb981004SEric Auger case SMMU_EVT_C_BAD_STE: 181bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_ste.ssid); 182bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_ste.ssv); 183bb981004SEric Auger break; 184bb981004SEric Auger case SMMU_EVT_F_STREAM_DISABLED: 185bb981004SEric Auger break; 186bb981004SEric Auger case SMMU_EVT_F_TRANS_FORBIDDEN: 187bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_transl_forbidden.addr); 188bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_transl_forbidden.rnw); 189bb981004SEric Auger break; 190bb981004SEric Auger case SMMU_EVT_C_BAD_SUBSTREAMID: 191bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_substream.ssid); 192bb981004SEric Auger break; 193bb981004SEric Auger case SMMU_EVT_F_CD_FETCH: 194bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_cd_fetch.ssid); 195bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_cd_fetch.ssv); 196bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_cd_fetch.addr); 197bb981004SEric Auger break; 198bb981004SEric Auger case SMMU_EVT_C_BAD_CD: 199bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_cd.ssid); 200bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_cd.ssv); 201bb981004SEric Auger break; 202bb981004SEric Auger case SMMU_EVT_F_WALK_EABT: 203bb981004SEric Auger case SMMU_EVT_F_TRANSLATION: 204bb981004SEric Auger case SMMU_EVT_F_ADDR_SIZE: 205bb981004SEric Auger case SMMU_EVT_F_ACCESS: 206bb981004SEric Auger case SMMU_EVT_F_PERMISSION: 207bb981004SEric Auger EVT_SET_STALL(&evt, info->u.f_walk_eabt.stall); 208bb981004SEric Auger EVT_SET_STAG(&evt, info->u.f_walk_eabt.stag); 209bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_walk_eabt.ssid); 210bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_walk_eabt.ssv); 211bb981004SEric Auger EVT_SET_S2(&evt, info->u.f_walk_eabt.s2); 212bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_walk_eabt.addr); 213bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_walk_eabt.rnw); 214bb981004SEric Auger EVT_SET_PNU(&evt, info->u.f_walk_eabt.pnu); 215bb981004SEric Auger EVT_SET_IND(&evt, info->u.f_walk_eabt.ind); 216bb981004SEric Auger EVT_SET_CLASS(&evt, info->u.f_walk_eabt.class); 217bb981004SEric Auger EVT_SET_ADDR2(&evt, info->u.f_walk_eabt.addr2); 218bb981004SEric Auger break; 219bb981004SEric Auger case SMMU_EVT_F_CFG_CONFLICT: 220bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_cfg_conflict.ssid); 221bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_cfg_conflict.ssv); 222bb981004SEric Auger break; 223bb981004SEric Auger /* rest is not implemented */ 224bb981004SEric Auger case SMMU_EVT_F_BAD_ATS_TREQ: 225bb981004SEric Auger case SMMU_EVT_F_TLB_CONFLICT: 226bb981004SEric Auger case SMMU_EVT_E_PAGE_REQ: 227bb981004SEric Auger default: 228bb981004SEric Auger g_assert_not_reached(); 229dadd1a08SEric Auger } 230dadd1a08SEric Auger 231bb981004SEric Auger trace_smmuv3_record_event(smmu_event_string(info->type), info->sid); 232bb981004SEric Auger r = smmuv3_write_eventq(s, &evt); 233bb981004SEric Auger if (r != MEMTX_OK) { 234bb981004SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_EVENTQ_ABT_ERR_MASK); 235dadd1a08SEric Auger } 236bb981004SEric Auger info->recorded = true; 237dadd1a08SEric Auger } 238dadd1a08SEric Auger 23910a83cb9SPrem Mallappa static void smmuv3_init_regs(SMMUv3State *s) 24010a83cb9SPrem Mallappa { 24110a83cb9SPrem Mallappa /** 24210a83cb9SPrem Mallappa * IDR0: stage1 only, AArch64 only, coherent access, 16b ASID, 24310a83cb9SPrem Mallappa * multi-level stream table 24410a83cb9SPrem Mallappa */ 24510a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S1P, 1); /* stage 1 supported */ 24610a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTF, 2); /* AArch64 PTW only */ 24710a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, COHACC, 1); /* IO coherent */ 24810a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, ASID16, 1); /* 16-bit ASID */ 24910a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTENDIAN, 2); /* little endian */ 25010a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STALL_MODEL, 1); /* No stall */ 25110a83cb9SPrem Mallappa /* terminated transaction will always be aborted/error returned */ 25210a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TERM_MODEL, 1); 25310a83cb9SPrem Mallappa /* 2-level stream table supported */ 25410a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STLEVEL, 1); 25510a83cb9SPrem Mallappa 25610a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, SIDSIZE, SMMU_IDR1_SIDSIZE); 25710a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, EVENTQS, SMMU_EVENTQS); 25810a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, CMDQS, SMMU_CMDQS); 25910a83cb9SPrem Mallappa 260de206dfdSEric Auger s->idr[3] = FIELD_DP32(s->idr[3], IDR3, RIL, 1); 261e7c3b9d9SEric Auger s->idr[3] = FIELD_DP32(s->idr[3], IDR3, HAD, 1); 262f8e7163dSPeter Maydell s->idr[3] = FIELD_DP32(s->idr[3], IDR3, BBML, 2); 263e7c3b9d9SEric Auger 264bf559ee4SKunkun Jiang /* 4K, 16K and 64K granule support */ 26510a83cb9SPrem Mallappa s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN4K, 1); 266bf559ee4SKunkun Jiang s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN16K, 1); 26710a83cb9SPrem Mallappa s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN64K, 1); 26810a83cb9SPrem Mallappa s->idr[5] = FIELD_DP32(s->idr[5], IDR5, OAS, SMMU_IDR5_OAS); /* 44 bits */ 26910a83cb9SPrem Mallappa 27010a83cb9SPrem Mallappa s->cmdq.base = deposit64(s->cmdq.base, 0, 5, SMMU_CMDQS); 27110a83cb9SPrem Mallappa s->cmdq.prod = 0; 27210a83cb9SPrem Mallappa s->cmdq.cons = 0; 27310a83cb9SPrem Mallappa s->cmdq.entry_size = sizeof(struct Cmd); 27410a83cb9SPrem Mallappa s->eventq.base = deposit64(s->eventq.base, 0, 5, SMMU_EVENTQS); 27510a83cb9SPrem Mallappa s->eventq.prod = 0; 27610a83cb9SPrem Mallappa s->eventq.cons = 0; 27710a83cb9SPrem Mallappa s->eventq.entry_size = sizeof(struct Evt); 27810a83cb9SPrem Mallappa 27910a83cb9SPrem Mallappa s->features = 0; 28010a83cb9SPrem Mallappa s->sid_split = 0; 281e7c3b9d9SEric Auger s->aidr = 0x1; 28243530095SEric Auger s->cr[0] = 0; 28343530095SEric Auger s->cr0ack = 0; 28443530095SEric Auger s->irq_ctrl = 0; 28543530095SEric Auger s->gerror = 0; 28643530095SEric Auger s->gerrorn = 0; 28743530095SEric Auger s->statusr = 0; 288c2ecb424SMostafa Saleh s->gbpa = SMMU_GBPA_RESET_VAL; 28910a83cb9SPrem Mallappa } 29010a83cb9SPrem Mallappa 2919bde7f06SEric Auger static int smmu_get_ste(SMMUv3State *s, dma_addr_t addr, STE *buf, 2929bde7f06SEric Auger SMMUEventInfo *event) 2939bde7f06SEric Auger { 2949bde7f06SEric Auger int ret; 2959bde7f06SEric Auger 2969bde7f06SEric Auger trace_smmuv3_get_ste(addr); 2979bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 298ba06fe8aSPhilippe Mathieu-Daudé ret = dma_memory_read(&address_space_memory, addr, buf, sizeof(*buf), 299ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 3009bde7f06SEric Auger if (ret != MEMTX_OK) { 3019bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 3029bde7f06SEric Auger "Cannot fetch pte at address=0x%"PRIx64"\n", addr); 3039bde7f06SEric Auger event->type = SMMU_EVT_F_STE_FETCH; 3049bde7f06SEric Auger event->u.f_ste_fetch.addr = addr; 3059bde7f06SEric Auger return -EINVAL; 3069bde7f06SEric Auger } 3079bde7f06SEric Auger return 0; 3089bde7f06SEric Auger 3099bde7f06SEric Auger } 3109bde7f06SEric Auger 3119bde7f06SEric Auger /* @ssid > 0 not supported yet */ 3129bde7f06SEric Auger static int smmu_get_cd(SMMUv3State *s, STE *ste, uint32_t ssid, 3139bde7f06SEric Auger CD *buf, SMMUEventInfo *event) 3149bde7f06SEric Auger { 3159bde7f06SEric Auger dma_addr_t addr = STE_CTXPTR(ste); 3169bde7f06SEric Auger int ret; 3179bde7f06SEric Auger 3189bde7f06SEric Auger trace_smmuv3_get_cd(addr); 3199bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 320ba06fe8aSPhilippe Mathieu-Daudé ret = dma_memory_read(&address_space_memory, addr, buf, sizeof(*buf), 321ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 3229bde7f06SEric Auger if (ret != MEMTX_OK) { 3239bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 3249bde7f06SEric Auger "Cannot fetch pte at address=0x%"PRIx64"\n", addr); 3259bde7f06SEric Auger event->type = SMMU_EVT_F_CD_FETCH; 3269bde7f06SEric Auger event->u.f_ste_fetch.addr = addr; 3279bde7f06SEric Auger return -EINVAL; 3289bde7f06SEric Auger } 3299bde7f06SEric Auger return 0; 3309bde7f06SEric Auger } 3319bde7f06SEric Auger 3329122bea9SJia He /* Returns < 0 in case of invalid STE, 0 otherwise */ 3339bde7f06SEric Auger static int decode_ste(SMMUv3State *s, SMMUTransCfg *cfg, 3349bde7f06SEric Auger STE *ste, SMMUEventInfo *event) 3359bde7f06SEric Auger { 3369bde7f06SEric Auger uint32_t config; 3379bde7f06SEric Auger 3389bde7f06SEric Auger if (!STE_VALID(ste)) { 3393499ec08SEric Auger if (!event->inval_ste_allowed) { 34051b6d368SEric Auger qemu_log_mask(LOG_GUEST_ERROR, "invalid STE\n"); 3413499ec08SEric Auger } 3429bde7f06SEric Auger goto bad_ste; 3439bde7f06SEric Auger } 3449bde7f06SEric Auger 3459bde7f06SEric Auger config = STE_CONFIG(ste); 3469bde7f06SEric Auger 3479bde7f06SEric Auger if (STE_CFG_ABORT(config)) { 3489122bea9SJia He cfg->aborted = true; 3499122bea9SJia He return 0; 3509bde7f06SEric Auger } 3519bde7f06SEric Auger 3529bde7f06SEric Auger if (STE_CFG_BYPASS(config)) { 3539bde7f06SEric Auger cfg->bypassed = true; 3549122bea9SJia He return 0; 3559bde7f06SEric Auger } 3569bde7f06SEric Auger 3579bde7f06SEric Auger if (STE_CFG_S2_ENABLED(config)) { 3589bde7f06SEric Auger qemu_log_mask(LOG_UNIMP, "SMMUv3 does not support stage 2 yet\n"); 3599bde7f06SEric Auger goto bad_ste; 3609bde7f06SEric Auger } 3619bde7f06SEric Auger 3629bde7f06SEric Auger if (STE_S1CDMAX(ste) != 0) { 3639bde7f06SEric Auger qemu_log_mask(LOG_UNIMP, 3649bde7f06SEric Auger "SMMUv3 does not support multiple context descriptors yet\n"); 3659bde7f06SEric Auger goto bad_ste; 3669bde7f06SEric Auger } 3679bde7f06SEric Auger 3689bde7f06SEric Auger if (STE_S1STALLD(ste)) { 3699bde7f06SEric Auger qemu_log_mask(LOG_UNIMP, 3709bde7f06SEric Auger "SMMUv3 S1 stalling fault model not allowed yet\n"); 3719bde7f06SEric Auger goto bad_ste; 3729bde7f06SEric Auger } 3739bde7f06SEric Auger return 0; 3749bde7f06SEric Auger 3759bde7f06SEric Auger bad_ste: 3769bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STE; 3779bde7f06SEric Auger return -EINVAL; 3789bde7f06SEric Auger } 3799bde7f06SEric Auger 3809bde7f06SEric Auger /** 3819bde7f06SEric Auger * smmu_find_ste - Return the stream table entry associated 3829bde7f06SEric Auger * to the sid 3839bde7f06SEric Auger * 3849bde7f06SEric Auger * @s: smmuv3 handle 3859bde7f06SEric Auger * @sid: stream ID 3869bde7f06SEric Auger * @ste: returned stream table entry 3879bde7f06SEric Auger * @event: handle to an event info 3889bde7f06SEric Auger * 3899bde7f06SEric Auger * Supports linear and 2-level stream table 3909bde7f06SEric Auger * Return 0 on success, -EINVAL otherwise 3919bde7f06SEric Auger */ 3929bde7f06SEric Auger static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, 3939bde7f06SEric Auger SMMUEventInfo *event) 3949bde7f06SEric Auger { 39541678c33SSimon Veith dma_addr_t addr, strtab_base; 39605ff2fb8SSimon Veith uint32_t log2size; 39741678c33SSimon Veith int strtab_size_shift; 3989bde7f06SEric Auger int ret; 3999bde7f06SEric Auger 4009bde7f06SEric Auger trace_smmuv3_find_ste(sid, s->features, s->sid_split); 40105ff2fb8SSimon Veith log2size = FIELD_EX32(s->strtab_base_cfg, STRTAB_BASE_CFG, LOG2SIZE); 40205ff2fb8SSimon Veith /* 40305ff2fb8SSimon Veith * Check SID range against both guest-configured and implementation limits 40405ff2fb8SSimon Veith */ 40505ff2fb8SSimon Veith if (sid >= (1 << MIN(log2size, SMMU_IDR1_SIDSIZE))) { 4069bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STREAMID; 4079bde7f06SEric Auger return -EINVAL; 4089bde7f06SEric Auger } 4099bde7f06SEric Auger if (s->features & SMMU_FEATURE_2LVL_STE) { 4109bde7f06SEric Auger int l1_ste_offset, l2_ste_offset, max_l2_ste, span; 41141678c33SSimon Veith dma_addr_t l1ptr, l2ptr; 4129bde7f06SEric Auger STEDesc l1std; 4139bde7f06SEric Auger 41441678c33SSimon Veith /* 41541678c33SSimon Veith * Align strtab base address to table size. For this purpose, assume it 41641678c33SSimon Veith * is not bounded by SMMU_IDR1_SIDSIZE. 41741678c33SSimon Veith */ 41841678c33SSimon Veith strtab_size_shift = MAX(5, (int)log2size - s->sid_split - 1 + 3); 41941678c33SSimon Veith strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK & 42041678c33SSimon Veith ~MAKE_64BIT_MASK(0, strtab_size_shift); 4219bde7f06SEric Auger l1_ste_offset = sid >> s->sid_split; 4229bde7f06SEric Auger l2_ste_offset = sid & ((1 << s->sid_split) - 1); 4239bde7f06SEric Auger l1ptr = (dma_addr_t)(strtab_base + l1_ste_offset * sizeof(l1std)); 4249bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 42518610bfdSPhilippe Mathieu-Daudé ret = dma_memory_read(&address_space_memory, l1ptr, &l1std, 426ba06fe8aSPhilippe Mathieu-Daudé sizeof(l1std), MEMTXATTRS_UNSPECIFIED); 4279bde7f06SEric Auger if (ret != MEMTX_OK) { 4289bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 4299bde7f06SEric Auger "Could not read L1PTR at 0X%"PRIx64"\n", l1ptr); 4309bde7f06SEric Auger event->type = SMMU_EVT_F_STE_FETCH; 4319bde7f06SEric Auger event->u.f_ste_fetch.addr = l1ptr; 4329bde7f06SEric Auger return -EINVAL; 4339bde7f06SEric Auger } 4349bde7f06SEric Auger 4359bde7f06SEric Auger span = L1STD_SPAN(&l1std); 4369bde7f06SEric Auger 4379bde7f06SEric Auger if (!span) { 4389bde7f06SEric Auger /* l2ptr is not valid */ 4393499ec08SEric Auger if (!event->inval_ste_allowed) { 4409bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 4419bde7f06SEric Auger "invalid sid=%d (L1STD span=0)\n", sid); 4423499ec08SEric Auger } 4439bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STREAMID; 4449bde7f06SEric Auger return -EINVAL; 4459bde7f06SEric Auger } 4469bde7f06SEric Auger max_l2_ste = (1 << span) - 1; 4479bde7f06SEric Auger l2ptr = l1std_l2ptr(&l1std); 4489bde7f06SEric Auger trace_smmuv3_find_ste_2lvl(s->strtab_base, l1ptr, l1_ste_offset, 4499bde7f06SEric Auger l2ptr, l2_ste_offset, max_l2_ste); 4509bde7f06SEric Auger if (l2_ste_offset > max_l2_ste) { 4519bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 4529bde7f06SEric Auger "l2_ste_offset=%d > max_l2_ste=%d\n", 4539bde7f06SEric Auger l2_ste_offset, max_l2_ste); 4549bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STE; 4559bde7f06SEric Auger return -EINVAL; 4569bde7f06SEric Auger } 4579bde7f06SEric Auger addr = l2ptr + l2_ste_offset * sizeof(*ste); 4589bde7f06SEric Auger } else { 45941678c33SSimon Veith strtab_size_shift = log2size + 5; 46041678c33SSimon Veith strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK & 46141678c33SSimon Veith ~MAKE_64BIT_MASK(0, strtab_size_shift); 46241678c33SSimon Veith addr = strtab_base + sid * sizeof(*ste); 4639bde7f06SEric Auger } 4649bde7f06SEric Auger 4659bde7f06SEric Auger if (smmu_get_ste(s, addr, ste, event)) { 4669bde7f06SEric Auger return -EINVAL; 4679bde7f06SEric Auger } 4689bde7f06SEric Auger 4699bde7f06SEric Auger return 0; 4709bde7f06SEric Auger } 4719bde7f06SEric Auger 4729bde7f06SEric Auger static int decode_cd(SMMUTransCfg *cfg, CD *cd, SMMUEventInfo *event) 4739bde7f06SEric Auger { 4749bde7f06SEric Auger int ret = -EINVAL; 4759bde7f06SEric Auger int i; 4769bde7f06SEric Auger 4779bde7f06SEric Auger if (!CD_VALID(cd) || !CD_AARCH64(cd)) { 4789bde7f06SEric Auger goto bad_cd; 4799bde7f06SEric Auger } 4809bde7f06SEric Auger if (!CD_A(cd)) { 4819bde7f06SEric Auger goto bad_cd; /* SMMU_IDR0.TERM_MODEL == 1 */ 4829bde7f06SEric Auger } 4839bde7f06SEric Auger if (CD_S(cd)) { 4849bde7f06SEric Auger goto bad_cd; /* !STE_SECURE && SMMU_IDR0.STALL_MODEL == 1 */ 4859bde7f06SEric Auger } 4869bde7f06SEric Auger if (CD_HA(cd) || CD_HD(cd)) { 4879bde7f06SEric Auger goto bad_cd; /* HTTU = 0 */ 4889bde7f06SEric Auger } 4899bde7f06SEric Auger 4909bde7f06SEric Auger /* we support only those at the moment */ 4919bde7f06SEric Auger cfg->aa64 = true; 4929bde7f06SEric Auger cfg->stage = 1; 4939bde7f06SEric Auger 4949bde7f06SEric Auger cfg->oas = oas2bits(CD_IPS(cd)); 4959bde7f06SEric Auger cfg->oas = MIN(oas2bits(SMMU_IDR5_OAS), cfg->oas); 4969bde7f06SEric Auger cfg->tbi = CD_TBI(cd); 4979bde7f06SEric Auger cfg->asid = CD_ASID(cd); 4989bde7f06SEric Auger 4999bde7f06SEric Auger trace_smmuv3_decode_cd(cfg->oas); 5009bde7f06SEric Auger 5019bde7f06SEric Auger /* decode data dependent on TT */ 5029bde7f06SEric Auger for (i = 0; i <= 1; i++) { 5039bde7f06SEric Auger int tg, tsz; 5049bde7f06SEric Auger SMMUTransTableInfo *tt = &cfg->tt[i]; 5059bde7f06SEric Auger 5069bde7f06SEric Auger cfg->tt[i].disabled = CD_EPD(cd, i); 5079bde7f06SEric Auger if (cfg->tt[i].disabled) { 5089bde7f06SEric Auger continue; 5099bde7f06SEric Auger } 5109bde7f06SEric Auger 5119bde7f06SEric Auger tsz = CD_TSZ(cd, i); 5129bde7f06SEric Auger if (tsz < 16 || tsz > 39) { 5139bde7f06SEric Auger goto bad_cd; 5149bde7f06SEric Auger } 5159bde7f06SEric Auger 5169bde7f06SEric Auger tg = CD_TG(cd, i); 5179bde7f06SEric Auger tt->granule_sz = tg2granule(tg, i); 518bf559ee4SKunkun Jiang if ((tt->granule_sz != 12 && tt->granule_sz != 14 && 519bf559ee4SKunkun Jiang tt->granule_sz != 16) || CD_ENDI(cd)) { 5209bde7f06SEric Auger goto bad_cd; 5219bde7f06SEric Auger } 5229bde7f06SEric Auger 5239bde7f06SEric Auger tt->tsz = tsz; 5249bde7f06SEric Auger tt->ttb = CD_TTB(cd, i); 5259bde7f06SEric Auger if (tt->ttb & ~(MAKE_64BIT_MASK(0, cfg->oas))) { 5269bde7f06SEric Auger goto bad_cd; 5279bde7f06SEric Auger } 528e7c3b9d9SEric Auger tt->had = CD_HAD(cd, i); 529e7c3b9d9SEric Auger trace_smmuv3_decode_cd_tt(i, tt->tsz, tt->ttb, tt->granule_sz, tt->had); 5309bde7f06SEric Auger } 5319bde7f06SEric Auger 532ced71694SJean-Philippe Brucker cfg->record_faults = CD_R(cd); 5339bde7f06SEric Auger 5349bde7f06SEric Auger return 0; 5359bde7f06SEric Auger 5369bde7f06SEric Auger bad_cd: 5379bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_CD; 5389bde7f06SEric Auger return ret; 5399bde7f06SEric Auger } 5409bde7f06SEric Auger 5419bde7f06SEric Auger /** 5429bde7f06SEric Auger * smmuv3_decode_config - Prepare the translation configuration 5439bde7f06SEric Auger * for the @mr iommu region 5449bde7f06SEric Auger * @mr: iommu memory region the translation config must be prepared for 5459bde7f06SEric Auger * @cfg: output translation configuration which is populated through 5469bde7f06SEric Auger * the different configuration decoding steps 5479bde7f06SEric Auger * @event: must be zero'ed by the caller 5489bde7f06SEric Auger * 5499122bea9SJia He * return < 0 in case of config decoding error (@event is filled 5509bde7f06SEric Auger * accordingly). Return 0 otherwise. 5519bde7f06SEric Auger */ 5529bde7f06SEric Auger static int smmuv3_decode_config(IOMMUMemoryRegion *mr, SMMUTransCfg *cfg, 5539bde7f06SEric Auger SMMUEventInfo *event) 5549bde7f06SEric Auger { 5559bde7f06SEric Auger SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu); 5569bde7f06SEric Auger uint32_t sid = smmu_get_sid(sdev); 5579bde7f06SEric Auger SMMUv3State *s = sdev->smmu; 5589122bea9SJia He int ret; 5599bde7f06SEric Auger STE ste; 5609bde7f06SEric Auger CD cd; 5619bde7f06SEric Auger 5629122bea9SJia He ret = smmu_find_ste(s, sid, &ste, event); 5639122bea9SJia He if (ret) { 5649bde7f06SEric Auger return ret; 5659bde7f06SEric Auger } 5669bde7f06SEric Auger 5679122bea9SJia He ret = decode_ste(s, cfg, &ste, event); 5689122bea9SJia He if (ret) { 5699bde7f06SEric Auger return ret; 5709bde7f06SEric Auger } 5719bde7f06SEric Auger 5729122bea9SJia He if (cfg->aborted || cfg->bypassed) { 5739122bea9SJia He return 0; 5749122bea9SJia He } 5759122bea9SJia He 5769122bea9SJia He ret = smmu_get_cd(s, &ste, 0 /* ssid */, &cd, event); 5779122bea9SJia He if (ret) { 5789bde7f06SEric Auger return ret; 5799bde7f06SEric Auger } 5809bde7f06SEric Auger 5819bde7f06SEric Auger return decode_cd(cfg, &cd, event); 5829bde7f06SEric Auger } 5839bde7f06SEric Auger 58432cfd7f3SEric Auger /** 58532cfd7f3SEric Auger * smmuv3_get_config - Look up for a cached copy of configuration data for 58632cfd7f3SEric Auger * @sdev and on cache miss performs a configuration structure decoding from 58732cfd7f3SEric Auger * guest RAM. 58832cfd7f3SEric Auger * 58932cfd7f3SEric Auger * @sdev: SMMUDevice handle 59032cfd7f3SEric Auger * @event: output event info 59132cfd7f3SEric Auger * 59232cfd7f3SEric Auger * The configuration cache contains data resulting from both STE and CD 59332cfd7f3SEric Auger * decoding under the form of an SMMUTransCfg struct. The hash table is indexed 59432cfd7f3SEric Auger * by the SMMUDevice handle. 59532cfd7f3SEric Auger */ 59632cfd7f3SEric Auger static SMMUTransCfg *smmuv3_get_config(SMMUDevice *sdev, SMMUEventInfo *event) 59732cfd7f3SEric Auger { 59832cfd7f3SEric Auger SMMUv3State *s = sdev->smmu; 59932cfd7f3SEric Auger SMMUState *bc = &s->smmu_state; 60032cfd7f3SEric Auger SMMUTransCfg *cfg; 60132cfd7f3SEric Auger 60232cfd7f3SEric Auger cfg = g_hash_table_lookup(bc->configs, sdev); 60332cfd7f3SEric Auger if (cfg) { 60432cfd7f3SEric Auger sdev->cfg_cache_hits++; 60532cfd7f3SEric Auger trace_smmuv3_config_cache_hit(smmu_get_sid(sdev), 60632cfd7f3SEric Auger sdev->cfg_cache_hits, sdev->cfg_cache_misses, 60732cfd7f3SEric Auger 100 * sdev->cfg_cache_hits / 60832cfd7f3SEric Auger (sdev->cfg_cache_hits + sdev->cfg_cache_misses)); 60932cfd7f3SEric Auger } else { 61032cfd7f3SEric Auger sdev->cfg_cache_misses++; 61132cfd7f3SEric Auger trace_smmuv3_config_cache_miss(smmu_get_sid(sdev), 61232cfd7f3SEric Auger sdev->cfg_cache_hits, sdev->cfg_cache_misses, 61332cfd7f3SEric Auger 100 * sdev->cfg_cache_hits / 61432cfd7f3SEric Auger (sdev->cfg_cache_hits + sdev->cfg_cache_misses)); 61532cfd7f3SEric Auger cfg = g_new0(SMMUTransCfg, 1); 61632cfd7f3SEric Auger 61732cfd7f3SEric Auger if (!smmuv3_decode_config(&sdev->iommu, cfg, event)) { 61832cfd7f3SEric Auger g_hash_table_insert(bc->configs, sdev, cfg); 61932cfd7f3SEric Auger } else { 62032cfd7f3SEric Auger g_free(cfg); 62132cfd7f3SEric Auger cfg = NULL; 62232cfd7f3SEric Auger } 62332cfd7f3SEric Auger } 62432cfd7f3SEric Auger return cfg; 62532cfd7f3SEric Auger } 62632cfd7f3SEric Auger 62732cfd7f3SEric Auger static void smmuv3_flush_config(SMMUDevice *sdev) 62832cfd7f3SEric Auger { 62932cfd7f3SEric Auger SMMUv3State *s = sdev->smmu; 63032cfd7f3SEric Auger SMMUState *bc = &s->smmu_state; 63132cfd7f3SEric Auger 63232cfd7f3SEric Auger trace_smmuv3_config_cache_inv(smmu_get_sid(sdev)); 63332cfd7f3SEric Auger g_hash_table_remove(bc->configs, sdev); 63432cfd7f3SEric Auger } 63532cfd7f3SEric Auger 6369bde7f06SEric Auger static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr, 6372c91bcf2SPeter Maydell IOMMUAccessFlags flag, int iommu_idx) 6389bde7f06SEric Auger { 6399bde7f06SEric Auger SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu); 6409bde7f06SEric Auger SMMUv3State *s = sdev->smmu; 6419bde7f06SEric Auger uint32_t sid = smmu_get_sid(sdev); 6423499ec08SEric Auger SMMUEventInfo event = {.type = SMMU_EVT_NONE, 6433499ec08SEric Auger .sid = sid, 6443499ec08SEric Auger .inval_ste_allowed = false}; 6459bde7f06SEric Auger SMMUPTWEventInfo ptw_info = {}; 6469122bea9SJia He SMMUTranslationStatus status; 647cc27ed81SEric Auger SMMUState *bs = ARM_SMMU(s); 648cc27ed81SEric Auger uint64_t page_mask, aligned_addr; 649a7550158SEric Auger SMMUTLBEntry *cached_entry = NULL; 650cc27ed81SEric Auger SMMUTransTableInfo *tt; 65132cfd7f3SEric Auger SMMUTransCfg *cfg = NULL; 6529bde7f06SEric Auger IOMMUTLBEntry entry = { 6539bde7f06SEric Auger .target_as = &address_space_memory, 6549bde7f06SEric Auger .iova = addr, 6559bde7f06SEric Auger .translated_addr = addr, 6569bde7f06SEric Auger .addr_mask = ~(hwaddr)0, 6579bde7f06SEric Auger .perm = IOMMU_NONE, 6589bde7f06SEric Auger }; 6599bde7f06SEric Auger 66032cfd7f3SEric Auger qemu_mutex_lock(&s->mutex); 66132cfd7f3SEric Auger 6629bde7f06SEric Auger if (!smmu_enabled(s)) { 663c2ecb424SMostafa Saleh if (FIELD_EX32(s->gbpa, GBPA, ABORT)) { 664c2ecb424SMostafa Saleh status = SMMU_TRANS_ABORT; 665c2ecb424SMostafa Saleh } else { 6669122bea9SJia He status = SMMU_TRANS_DISABLE; 667c2ecb424SMostafa Saleh } 6689122bea9SJia He goto epilogue; 6699bde7f06SEric Auger } 6709bde7f06SEric Auger 67132cfd7f3SEric Auger cfg = smmuv3_get_config(sdev, &event); 67232cfd7f3SEric Auger if (!cfg) { 6739122bea9SJia He status = SMMU_TRANS_ERROR; 6749122bea9SJia He goto epilogue; 6759bde7f06SEric Auger } 6769bde7f06SEric Auger 67732cfd7f3SEric Auger if (cfg->aborted) { 6789122bea9SJia He status = SMMU_TRANS_ABORT; 6799122bea9SJia He goto epilogue; 6809bde7f06SEric Auger } 6819bde7f06SEric Auger 68232cfd7f3SEric Auger if (cfg->bypassed) { 6839122bea9SJia He status = SMMU_TRANS_BYPASS; 6849122bea9SJia He goto epilogue; 6859122bea9SJia He } 6869122bea9SJia He 687cc27ed81SEric Auger tt = select_tt(cfg, addr); 688cc27ed81SEric Auger if (!tt) { 689ced71694SJean-Philippe Brucker if (cfg->record_faults) { 690cc27ed81SEric Auger event.type = SMMU_EVT_F_TRANSLATION; 691cc27ed81SEric Auger event.u.f_translation.addr = addr; 692cc27ed81SEric Auger event.u.f_translation.rnw = flag & 0x1; 693cc27ed81SEric Auger } 694cc27ed81SEric Auger status = SMMU_TRANS_ERROR; 695cc27ed81SEric Auger goto epilogue; 696cc27ed81SEric Auger } 697cc27ed81SEric Auger 698cc27ed81SEric Auger page_mask = (1ULL << (tt->granule_sz)) - 1; 699cc27ed81SEric Auger aligned_addr = addr & ~page_mask; 700cc27ed81SEric Auger 7019e54dee7SEric Auger cached_entry = smmu_iotlb_lookup(bs, cfg, tt, aligned_addr); 702cc27ed81SEric Auger if (cached_entry) { 703a7550158SEric Auger if ((flag & IOMMU_WO) && !(cached_entry->entry.perm & IOMMU_WO)) { 704cc27ed81SEric Auger status = SMMU_TRANS_ERROR; 705ced71694SJean-Philippe Brucker if (cfg->record_faults) { 706cc27ed81SEric Auger event.type = SMMU_EVT_F_PERMISSION; 707cc27ed81SEric Auger event.u.f_permission.addr = addr; 708cc27ed81SEric Auger event.u.f_permission.rnw = flag & 0x1; 709cc27ed81SEric Auger } 710cc27ed81SEric Auger } else { 711cc27ed81SEric Auger status = SMMU_TRANS_SUCCESS; 712cc27ed81SEric Auger } 713cc27ed81SEric Auger goto epilogue; 714cc27ed81SEric Auger } 715cc27ed81SEric Auger 716a7550158SEric Auger cached_entry = g_new0(SMMUTLBEntry, 1); 717cc27ed81SEric Auger 718cc27ed81SEric Auger if (smmu_ptw(cfg, aligned_addr, flag, cached_entry, &ptw_info)) { 719*bcc919e7SMostafa Saleh /* All faults from PTW has S2 field. */ 720*bcc919e7SMostafa Saleh event.u.f_walk_eabt.s2 = (ptw_info.stage == 2); 721cc27ed81SEric Auger g_free(cached_entry); 7229bde7f06SEric Auger switch (ptw_info.type) { 7239bde7f06SEric Auger case SMMU_PTW_ERR_WALK_EABT: 7249bde7f06SEric Auger event.type = SMMU_EVT_F_WALK_EABT; 7259bde7f06SEric Auger event.u.f_walk_eabt.addr = addr; 7269bde7f06SEric Auger event.u.f_walk_eabt.rnw = flag & 0x1; 7279bde7f06SEric Auger event.u.f_walk_eabt.class = 0x1; 7289bde7f06SEric Auger event.u.f_walk_eabt.addr2 = ptw_info.addr; 7299bde7f06SEric Auger break; 7309bde7f06SEric Auger case SMMU_PTW_ERR_TRANSLATION: 731ced71694SJean-Philippe Brucker if (cfg->record_faults) { 7329bde7f06SEric Auger event.type = SMMU_EVT_F_TRANSLATION; 7339bde7f06SEric Auger event.u.f_translation.addr = addr; 7349bde7f06SEric Auger event.u.f_translation.rnw = flag & 0x1; 7359bde7f06SEric Auger } 7369bde7f06SEric Auger break; 7379bde7f06SEric Auger case SMMU_PTW_ERR_ADDR_SIZE: 738ced71694SJean-Philippe Brucker if (cfg->record_faults) { 7399bde7f06SEric Auger event.type = SMMU_EVT_F_ADDR_SIZE; 7409bde7f06SEric Auger event.u.f_addr_size.addr = addr; 7419bde7f06SEric Auger event.u.f_addr_size.rnw = flag & 0x1; 7429bde7f06SEric Auger } 7439bde7f06SEric Auger break; 7449bde7f06SEric Auger case SMMU_PTW_ERR_ACCESS: 745ced71694SJean-Philippe Brucker if (cfg->record_faults) { 7469bde7f06SEric Auger event.type = SMMU_EVT_F_ACCESS; 7479bde7f06SEric Auger event.u.f_access.addr = addr; 7489bde7f06SEric Auger event.u.f_access.rnw = flag & 0x1; 7499bde7f06SEric Auger } 7509bde7f06SEric Auger break; 7519bde7f06SEric Auger case SMMU_PTW_ERR_PERMISSION: 752ced71694SJean-Philippe Brucker if (cfg->record_faults) { 7539bde7f06SEric Auger event.type = SMMU_EVT_F_PERMISSION; 7549bde7f06SEric Auger event.u.f_permission.addr = addr; 7559bde7f06SEric Auger event.u.f_permission.rnw = flag & 0x1; 7569bde7f06SEric Auger } 7579bde7f06SEric Auger break; 7589bde7f06SEric Auger default: 7599bde7f06SEric Auger g_assert_not_reached(); 7609bde7f06SEric Auger } 7619122bea9SJia He status = SMMU_TRANS_ERROR; 7629122bea9SJia He } else { 7636808bca9SEric Auger smmu_iotlb_insert(bs, cfg, cached_entry); 7649122bea9SJia He status = SMMU_TRANS_SUCCESS; 7659bde7f06SEric Auger } 7669122bea9SJia He 7679122bea9SJia He epilogue: 76832cfd7f3SEric Auger qemu_mutex_unlock(&s->mutex); 7699122bea9SJia He switch (status) { 7709122bea9SJia He case SMMU_TRANS_SUCCESS: 771c3ca7d56SXiang Chen entry.perm = cached_entry->entry.perm; 772a7550158SEric Auger entry.translated_addr = cached_entry->entry.translated_addr + 7739e54dee7SEric Auger (addr & cached_entry->entry.addr_mask); 774a7550158SEric Auger entry.addr_mask = cached_entry->entry.addr_mask; 7759122bea9SJia He trace_smmuv3_translate_success(mr->parent_obj.name, sid, addr, 7769bde7f06SEric Auger entry.translated_addr, entry.perm); 7779122bea9SJia He break; 7789122bea9SJia He case SMMU_TRANS_DISABLE: 7799122bea9SJia He entry.perm = flag; 7809122bea9SJia He entry.addr_mask = ~TARGET_PAGE_MASK; 7819122bea9SJia He trace_smmuv3_translate_disable(mr->parent_obj.name, sid, addr, 7829122bea9SJia He entry.perm); 7839122bea9SJia He break; 7849122bea9SJia He case SMMU_TRANS_BYPASS: 7859122bea9SJia He entry.perm = flag; 7869122bea9SJia He entry.addr_mask = ~TARGET_PAGE_MASK; 7879122bea9SJia He trace_smmuv3_translate_bypass(mr->parent_obj.name, sid, addr, 7889122bea9SJia He entry.perm); 7899122bea9SJia He break; 7909122bea9SJia He case SMMU_TRANS_ABORT: 7919122bea9SJia He /* no event is recorded on abort */ 7929122bea9SJia He trace_smmuv3_translate_abort(mr->parent_obj.name, sid, addr, 7939122bea9SJia He entry.perm); 7949122bea9SJia He break; 7959122bea9SJia He case SMMU_TRANS_ERROR: 7969122bea9SJia He qemu_log_mask(LOG_GUEST_ERROR, 7979122bea9SJia He "%s translation failed for iova=0x%"PRIx64" (%s)\n", 7989122bea9SJia He mr->parent_obj.name, addr, smmu_event_string(event.type)); 7999122bea9SJia He smmuv3_record_event(s, &event); 8009122bea9SJia He break; 8019bde7f06SEric Auger } 8029bde7f06SEric Auger 8039bde7f06SEric Auger return entry; 8049bde7f06SEric Auger } 8059bde7f06SEric Auger 806832e4222SEric Auger /** 807832e4222SEric Auger * smmuv3_notify_iova - call the notifier @n for a given 808832e4222SEric Auger * @asid and @iova tuple. 809832e4222SEric Auger * 810832e4222SEric Auger * @mr: IOMMU mr region handle 811832e4222SEric Auger * @n: notifier to be called 812832e4222SEric Auger * @asid: address space ID or negative value if we don't care 813832e4222SEric Auger * @iova: iova 814d5291561SEric Auger * @tg: translation granule (if communicated through range invalidation) 815d5291561SEric Auger * @num_pages: number of @granule sized pages (if tg != 0), otherwise 1 816832e4222SEric Auger */ 817832e4222SEric Auger static void smmuv3_notify_iova(IOMMUMemoryRegion *mr, 818832e4222SEric Auger IOMMUNotifier *n, 819d5291561SEric Auger int asid, dma_addr_t iova, 820d5291561SEric Auger uint8_t tg, uint64_t num_pages) 821832e4222SEric Auger { 822832e4222SEric Auger SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu); 8235039caf3SEugenio Pérez IOMMUTLBEvent event; 824dcda883cSZenghui Yu uint8_t granule; 825832e4222SEric Auger 826d5291561SEric Auger if (!tg) { 827d5291561SEric Auger SMMUEventInfo event = {.inval_ste_allowed = true}; 828d5291561SEric Auger SMMUTransCfg *cfg = smmuv3_get_config(sdev, &event); 829d5291561SEric Auger SMMUTransTableInfo *tt; 830d5291561SEric Auger 831832e4222SEric Auger if (!cfg) { 832832e4222SEric Auger return; 833832e4222SEric Auger } 834832e4222SEric Auger 835832e4222SEric Auger if (asid >= 0 && cfg->asid != asid) { 836832e4222SEric Auger return; 837832e4222SEric Auger } 838832e4222SEric Auger 839832e4222SEric Auger tt = select_tt(cfg, iova); 840832e4222SEric Auger if (!tt) { 841832e4222SEric Auger return; 842832e4222SEric Auger } 843d5291561SEric Auger granule = tt->granule_sz; 844dcda883cSZenghui Yu } else { 845dcda883cSZenghui Yu granule = tg * 2 + 10; 846d5291561SEric Auger } 847832e4222SEric Auger 8485039caf3SEugenio Pérez event.type = IOMMU_NOTIFIER_UNMAP; 8495039caf3SEugenio Pérez event.entry.target_as = &address_space_memory; 8505039caf3SEugenio Pérez event.entry.iova = iova; 8515039caf3SEugenio Pérez event.entry.addr_mask = num_pages * (1 << granule) - 1; 8525039caf3SEugenio Pérez event.entry.perm = IOMMU_NONE; 853832e4222SEric Auger 8545039caf3SEugenio Pérez memory_region_notify_iommu_one(n, &event); 855832e4222SEric Auger } 856832e4222SEric Auger 857d5291561SEric Auger /* invalidate an asid/iova range tuple in all mr's */ 858d5291561SEric Auger static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, dma_addr_t iova, 859d5291561SEric Auger uint8_t tg, uint64_t num_pages) 860832e4222SEric Auger { 861c6370441SEric Auger SMMUDevice *sdev; 862832e4222SEric Auger 863c6370441SEric Auger QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) { 864c6370441SEric Auger IOMMUMemoryRegion *mr = &sdev->iommu; 865832e4222SEric Auger IOMMUNotifier *n; 866832e4222SEric Auger 867d5291561SEric Auger trace_smmuv3_inv_notifiers_iova(mr->parent_obj.name, asid, iova, 868d5291561SEric Auger tg, num_pages); 869832e4222SEric Auger 870832e4222SEric Auger IOMMU_NOTIFIER_FOREACH(n, mr) { 871d5291561SEric Auger smmuv3_notify_iova(mr, n, asid, iova, tg, num_pages); 872832e4222SEric Auger } 873832e4222SEric Auger } 874832e4222SEric Auger } 875832e4222SEric Auger 876c0f9ef70SEric Auger static void smmuv3_s1_range_inval(SMMUState *s, Cmd *cmd) 877c0f9ef70SEric Auger { 878219729cfSEric Auger dma_addr_t end, addr = CMD_ADDR(cmd); 879c0f9ef70SEric Auger uint8_t type = CMD_TYPE(cmd); 880c0f9ef70SEric Auger uint16_t vmid = CMD_VMID(cmd); 881219729cfSEric Auger uint8_t scale = CMD_SCALE(cmd); 882219729cfSEric Auger uint8_t num = CMD_NUM(cmd); 883219729cfSEric Auger uint8_t ttl = CMD_TTL(cmd); 884c0f9ef70SEric Auger bool leaf = CMD_LEAF(cmd); 885d5291561SEric Auger uint8_t tg = CMD_TG(cmd); 886219729cfSEric Auger uint64_t num_pages; 887219729cfSEric Auger uint8_t granule; 888c0f9ef70SEric Auger int asid = -1; 889c0f9ef70SEric Auger 890c0f9ef70SEric Auger if (type == SMMU_CMD_TLBI_NH_VA) { 891c0f9ef70SEric Auger asid = CMD_ASID(cmd); 892c0f9ef70SEric Auger } 8936d9cd115SEric Auger 894219729cfSEric Auger if (!tg) { 895219729cfSEric Auger trace_smmuv3_s1_range_inval(vmid, asid, addr, tg, 1, ttl, leaf); 896219729cfSEric Auger smmuv3_inv_notifiers_iova(s, asid, addr, tg, 1); 897219729cfSEric Auger smmu_iotlb_inv_iova(s, asid, addr, tg, 1, ttl); 898219729cfSEric Auger return; 899219729cfSEric Auger } 900219729cfSEric Auger 901219729cfSEric Auger /* RIL in use */ 902219729cfSEric Auger 903219729cfSEric Auger num_pages = (num + 1) * BIT_ULL(scale); 904219729cfSEric Auger granule = tg * 2 + 10; 905219729cfSEric Auger 9066d9cd115SEric Auger /* Split invalidations into ^2 range invalidations */ 907219729cfSEric Auger end = addr + (num_pages << granule) - 1; 9086d9cd115SEric Auger 909219729cfSEric Auger while (addr != end + 1) { 910219729cfSEric Auger uint64_t mask = dma_aligned_pow2_mask(addr, end, 64); 9116d9cd115SEric Auger 912219729cfSEric Auger num_pages = (mask + 1) >> granule; 913219729cfSEric Auger trace_smmuv3_s1_range_inval(vmid, asid, addr, tg, num_pages, ttl, leaf); 914219729cfSEric Auger smmuv3_inv_notifiers_iova(s, asid, addr, tg, num_pages); 915219729cfSEric Auger smmu_iotlb_inv_iova(s, asid, addr, tg, num_pages, ttl); 916219729cfSEric Auger addr += mask + 1; 9176d9cd115SEric Auger } 918c0f9ef70SEric Auger } 919c0f9ef70SEric Auger 9201194140bSEric Auger static gboolean 9211194140bSEric Auger smmuv3_invalidate_ste(gpointer key, gpointer value, gpointer user_data) 9221194140bSEric Auger { 9231194140bSEric Auger SMMUDevice *sdev = (SMMUDevice *)key; 9241194140bSEric Auger uint32_t sid = smmu_get_sid(sdev); 9251194140bSEric Auger SMMUSIDRange *sid_range = (SMMUSIDRange *)user_data; 9261194140bSEric Auger 9271194140bSEric Auger if (sid < sid_range->start || sid > sid_range->end) { 9281194140bSEric Auger return false; 9291194140bSEric Auger } 9301194140bSEric Auger trace_smmuv3_config_cache_inv(sid); 9311194140bSEric Auger return true; 9321194140bSEric Auger } 9331194140bSEric Auger 934fae4be38SEric Auger static int smmuv3_cmdq_consume(SMMUv3State *s) 935dadd1a08SEric Auger { 93632cfd7f3SEric Auger SMMUState *bs = ARM_SMMU(s); 937dadd1a08SEric Auger SMMUCmdError cmd_error = SMMU_CERROR_NONE; 938dadd1a08SEric Auger SMMUQueue *q = &s->cmdq; 939dadd1a08SEric Auger SMMUCommandType type = 0; 940dadd1a08SEric Auger 941dadd1a08SEric Auger if (!smmuv3_cmdq_enabled(s)) { 942dadd1a08SEric Auger return 0; 943dadd1a08SEric Auger } 944dadd1a08SEric Auger /* 945dadd1a08SEric Auger * some commands depend on register values, typically CR0. In case those 946dadd1a08SEric Auger * register values change while handling the command, spec says it 947dadd1a08SEric Auger * is UNPREDICTABLE whether the command is interpreted under the new 948dadd1a08SEric Auger * or old value. 949dadd1a08SEric Auger */ 950dadd1a08SEric Auger 951dadd1a08SEric Auger while (!smmuv3_q_empty(q)) { 952dadd1a08SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 953dadd1a08SEric Auger Cmd cmd; 954dadd1a08SEric Auger 955dadd1a08SEric Auger trace_smmuv3_cmdq_consume(Q_PROD(q), Q_CONS(q), 956dadd1a08SEric Auger Q_PROD_WRAP(q), Q_CONS_WRAP(q)); 957dadd1a08SEric Auger 958dadd1a08SEric Auger if (FIELD_EX32(pending, GERROR, CMDQ_ERR)) { 959dadd1a08SEric Auger break; 960dadd1a08SEric Auger } 961dadd1a08SEric Auger 962dadd1a08SEric Auger if (queue_read(q, &cmd) != MEMTX_OK) { 963dadd1a08SEric Auger cmd_error = SMMU_CERROR_ABT; 964dadd1a08SEric Auger break; 965dadd1a08SEric Auger } 966dadd1a08SEric Auger 967dadd1a08SEric Auger type = CMD_TYPE(&cmd); 968dadd1a08SEric Auger 969dadd1a08SEric Auger trace_smmuv3_cmdq_opcode(smmu_cmd_string(type)); 970dadd1a08SEric Auger 97132cfd7f3SEric Auger qemu_mutex_lock(&s->mutex); 972dadd1a08SEric Auger switch (type) { 973dadd1a08SEric Auger case SMMU_CMD_SYNC: 974dadd1a08SEric Auger if (CMD_SYNC_CS(&cmd) & CMD_SYNC_SIG_IRQ) { 975dadd1a08SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_CMD_SYNC, 0); 976dadd1a08SEric Auger } 977dadd1a08SEric Auger break; 978dadd1a08SEric Auger case SMMU_CMD_PREFETCH_CONFIG: 979dadd1a08SEric Auger case SMMU_CMD_PREFETCH_ADDR: 98032cfd7f3SEric Auger break; 981dadd1a08SEric Auger case SMMU_CMD_CFGI_STE: 98232cfd7f3SEric Auger { 98332cfd7f3SEric Auger uint32_t sid = CMD_SID(&cmd); 98432cfd7f3SEric Auger IOMMUMemoryRegion *mr = smmu_iommu_mr(bs, sid); 98532cfd7f3SEric Auger SMMUDevice *sdev; 98632cfd7f3SEric Auger 98732cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 98832cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 98932cfd7f3SEric Auger break; 99032cfd7f3SEric Auger } 99132cfd7f3SEric Auger 99232cfd7f3SEric Auger if (!mr) { 99332cfd7f3SEric Auger break; 99432cfd7f3SEric Auger } 99532cfd7f3SEric Auger 99632cfd7f3SEric Auger trace_smmuv3_cmdq_cfgi_ste(sid); 99732cfd7f3SEric Auger sdev = container_of(mr, SMMUDevice, iommu); 99832cfd7f3SEric Auger smmuv3_flush_config(sdev); 99932cfd7f3SEric Auger 100032cfd7f3SEric Auger break; 100132cfd7f3SEric Auger } 1002dadd1a08SEric Auger case SMMU_CMD_CFGI_STE_RANGE: /* same as SMMU_CMD_CFGI_ALL */ 100332cfd7f3SEric Auger { 1004017a913aSZenghui Yu uint32_t sid = CMD_SID(&cmd), mask; 100532cfd7f3SEric Auger uint8_t range = CMD_STE_RANGE(&cmd); 1006017a913aSZenghui Yu SMMUSIDRange sid_range; 100732cfd7f3SEric Auger 100832cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 100932cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 101032cfd7f3SEric Auger break; 101132cfd7f3SEric Auger } 1012017a913aSZenghui Yu 1013017a913aSZenghui Yu mask = (1ULL << (range + 1)) - 1; 1014017a913aSZenghui Yu sid_range.start = sid & ~mask; 1015017a913aSZenghui Yu sid_range.end = sid_range.start + mask; 1016017a913aSZenghui Yu 1017017a913aSZenghui Yu trace_smmuv3_cmdq_cfgi_ste_range(sid_range.start, sid_range.end); 10181194140bSEric Auger g_hash_table_foreach_remove(bs->configs, smmuv3_invalidate_ste, 10191194140bSEric Auger &sid_range); 102032cfd7f3SEric Auger break; 102132cfd7f3SEric Auger } 1022dadd1a08SEric Auger case SMMU_CMD_CFGI_CD: 1023dadd1a08SEric Auger case SMMU_CMD_CFGI_CD_ALL: 102432cfd7f3SEric Auger { 102532cfd7f3SEric Auger uint32_t sid = CMD_SID(&cmd); 102632cfd7f3SEric Auger IOMMUMemoryRegion *mr = smmu_iommu_mr(bs, sid); 102732cfd7f3SEric Auger SMMUDevice *sdev; 102832cfd7f3SEric Auger 102932cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 103032cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 103132cfd7f3SEric Auger break; 103232cfd7f3SEric Auger } 103332cfd7f3SEric Auger 103432cfd7f3SEric Auger if (!mr) { 103532cfd7f3SEric Auger break; 103632cfd7f3SEric Auger } 103732cfd7f3SEric Auger 103832cfd7f3SEric Auger trace_smmuv3_cmdq_cfgi_cd(sid); 103932cfd7f3SEric Auger sdev = container_of(mr, SMMUDevice, iommu); 104032cfd7f3SEric Auger smmuv3_flush_config(sdev); 104132cfd7f3SEric Auger break; 104232cfd7f3SEric Auger } 1043dadd1a08SEric Auger case SMMU_CMD_TLBI_NH_ASID: 1044cc27ed81SEric Auger { 1045cc27ed81SEric Auger uint16_t asid = CMD_ASID(&cmd); 1046cc27ed81SEric Auger 1047cc27ed81SEric Auger trace_smmuv3_cmdq_tlbi_nh_asid(asid); 1048832e4222SEric Auger smmu_inv_notifiers_all(&s->smmu_state); 1049cc27ed81SEric Auger smmu_iotlb_inv_asid(bs, asid); 1050cc27ed81SEric Auger break; 1051cc27ed81SEric Auger } 1052cc27ed81SEric Auger case SMMU_CMD_TLBI_NH_ALL: 1053cc27ed81SEric Auger case SMMU_CMD_TLBI_NSNH_ALL: 1054cc27ed81SEric Auger trace_smmuv3_cmdq_tlbi_nh(); 1055832e4222SEric Auger smmu_inv_notifiers_all(&s->smmu_state); 1056cc27ed81SEric Auger smmu_iotlb_inv_all(bs); 1057cc27ed81SEric Auger break; 1058dadd1a08SEric Auger case SMMU_CMD_TLBI_NH_VAA: 1059cc27ed81SEric Auger case SMMU_CMD_TLBI_NH_VA: 1060c0f9ef70SEric Auger smmuv3_s1_range_inval(bs, &cmd); 1061cc27ed81SEric Auger break; 1062dadd1a08SEric Auger case SMMU_CMD_TLBI_EL3_ALL: 1063dadd1a08SEric Auger case SMMU_CMD_TLBI_EL3_VA: 1064dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_ALL: 1065dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_ASID: 1066dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_VA: 1067dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_VAA: 1068dadd1a08SEric Auger case SMMU_CMD_TLBI_S12_VMALL: 1069dadd1a08SEric Auger case SMMU_CMD_TLBI_S2_IPA: 1070dadd1a08SEric Auger case SMMU_CMD_ATC_INV: 1071dadd1a08SEric Auger case SMMU_CMD_PRI_RESP: 1072dadd1a08SEric Auger case SMMU_CMD_RESUME: 1073dadd1a08SEric Auger case SMMU_CMD_STALL_TERM: 1074dadd1a08SEric Auger trace_smmuv3_unhandled_cmd(type); 1075dadd1a08SEric Auger break; 1076dadd1a08SEric Auger default: 1077dadd1a08SEric Auger cmd_error = SMMU_CERROR_ILL; 1078dadd1a08SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 1079dadd1a08SEric Auger "Illegal command type: %d\n", CMD_TYPE(&cmd)); 1080dadd1a08SEric Auger break; 1081dadd1a08SEric Auger } 108232cfd7f3SEric Auger qemu_mutex_unlock(&s->mutex); 1083dadd1a08SEric Auger if (cmd_error) { 1084dadd1a08SEric Auger break; 1085dadd1a08SEric Auger } 1086dadd1a08SEric Auger /* 1087dadd1a08SEric Auger * We only increment the cons index after the completion of 1088dadd1a08SEric Auger * the command. We do that because the SYNC returns immediately 1089dadd1a08SEric Auger * and does not check the completion of previous commands 1090dadd1a08SEric Auger */ 1091dadd1a08SEric Auger queue_cons_incr(q); 1092dadd1a08SEric Auger } 1093dadd1a08SEric Auger 1094dadd1a08SEric Auger if (cmd_error) { 1095dadd1a08SEric Auger trace_smmuv3_cmdq_consume_error(smmu_cmd_string(type), cmd_error); 1096dadd1a08SEric Auger smmu_write_cmdq_err(s, cmd_error); 1097dadd1a08SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_CMDQ_ERR_MASK); 1098dadd1a08SEric Auger } 1099dadd1a08SEric Auger 1100dadd1a08SEric Auger trace_smmuv3_cmdq_consume_out(Q_PROD(q), Q_CONS(q), 1101dadd1a08SEric Auger Q_PROD_WRAP(q), Q_CONS_WRAP(q)); 1102dadd1a08SEric Auger 1103dadd1a08SEric Auger return 0; 1104dadd1a08SEric Auger } 1105dadd1a08SEric Auger 1106fae4be38SEric Auger static MemTxResult smmu_writell(SMMUv3State *s, hwaddr offset, 1107fae4be38SEric Auger uint64_t data, MemTxAttrs attrs) 1108fae4be38SEric Auger { 1109fae4be38SEric Auger switch (offset) { 1110fae4be38SEric Auger case A_GERROR_IRQ_CFG0: 1111fae4be38SEric Auger s->gerror_irq_cfg0 = data; 1112fae4be38SEric Auger return MEMTX_OK; 1113fae4be38SEric Auger case A_STRTAB_BASE: 1114fae4be38SEric Auger s->strtab_base = data; 1115fae4be38SEric Auger return MEMTX_OK; 1116fae4be38SEric Auger case A_CMDQ_BASE: 1117fae4be38SEric Auger s->cmdq.base = data; 1118fae4be38SEric Auger s->cmdq.log2size = extract64(s->cmdq.base, 0, 5); 1119fae4be38SEric Auger if (s->cmdq.log2size > SMMU_CMDQS) { 1120fae4be38SEric Auger s->cmdq.log2size = SMMU_CMDQS; 1121fae4be38SEric Auger } 1122fae4be38SEric Auger return MEMTX_OK; 1123fae4be38SEric Auger case A_EVENTQ_BASE: 1124fae4be38SEric Auger s->eventq.base = data; 1125fae4be38SEric Auger s->eventq.log2size = extract64(s->eventq.base, 0, 5); 1126fae4be38SEric Auger if (s->eventq.log2size > SMMU_EVENTQS) { 1127fae4be38SEric Auger s->eventq.log2size = SMMU_EVENTQS; 1128fae4be38SEric Auger } 1129fae4be38SEric Auger return MEMTX_OK; 1130fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0: 1131fae4be38SEric Auger s->eventq_irq_cfg0 = data; 1132fae4be38SEric Auger return MEMTX_OK; 1133fae4be38SEric Auger default: 1134fae4be38SEric Auger qemu_log_mask(LOG_UNIMP, 1135fae4be38SEric Auger "%s Unexpected 64-bit access to 0x%"PRIx64" (WI)\n", 1136fae4be38SEric Auger __func__, offset); 1137fae4be38SEric Auger return MEMTX_OK; 1138fae4be38SEric Auger } 1139fae4be38SEric Auger } 1140fae4be38SEric Auger 1141fae4be38SEric Auger static MemTxResult smmu_writel(SMMUv3State *s, hwaddr offset, 1142fae4be38SEric Auger uint64_t data, MemTxAttrs attrs) 1143fae4be38SEric Auger { 1144fae4be38SEric Auger switch (offset) { 1145fae4be38SEric Auger case A_CR0: 1146fae4be38SEric Auger s->cr[0] = data; 1147fae4be38SEric Auger s->cr0ack = data & ~SMMU_CR0_RESERVED; 1148fae4be38SEric Auger /* in case the command queue has been enabled */ 1149fae4be38SEric Auger smmuv3_cmdq_consume(s); 1150fae4be38SEric Auger return MEMTX_OK; 1151fae4be38SEric Auger case A_CR1: 1152fae4be38SEric Auger s->cr[1] = data; 1153fae4be38SEric Auger return MEMTX_OK; 1154fae4be38SEric Auger case A_CR2: 1155fae4be38SEric Auger s->cr[2] = data; 1156fae4be38SEric Auger return MEMTX_OK; 1157fae4be38SEric Auger case A_IRQ_CTRL: 1158fae4be38SEric Auger s->irq_ctrl = data; 1159fae4be38SEric Auger return MEMTX_OK; 1160fae4be38SEric Auger case A_GERRORN: 1161fae4be38SEric Auger smmuv3_write_gerrorn(s, data); 1162fae4be38SEric Auger /* 1163fae4be38SEric Auger * By acknowledging the CMDQ_ERR, SW may notify cmds can 1164fae4be38SEric Auger * be processed again 1165fae4be38SEric Auger */ 1166fae4be38SEric Auger smmuv3_cmdq_consume(s); 1167fae4be38SEric Auger return MEMTX_OK; 1168fae4be38SEric Auger case A_GERROR_IRQ_CFG0: /* 64b */ 1169fae4be38SEric Auger s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 0, 32, data); 1170fae4be38SEric Auger return MEMTX_OK; 1171fae4be38SEric Auger case A_GERROR_IRQ_CFG0 + 4: 1172fae4be38SEric Auger s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 32, 32, data); 1173fae4be38SEric Auger return MEMTX_OK; 1174fae4be38SEric Auger case A_GERROR_IRQ_CFG1: 1175fae4be38SEric Auger s->gerror_irq_cfg1 = data; 1176fae4be38SEric Auger return MEMTX_OK; 1177fae4be38SEric Auger case A_GERROR_IRQ_CFG2: 1178fae4be38SEric Auger s->gerror_irq_cfg2 = data; 1179fae4be38SEric Auger return MEMTX_OK; 1180c2ecb424SMostafa Saleh case A_GBPA: 1181c2ecb424SMostafa Saleh /* 1182c2ecb424SMostafa Saleh * If UPDATE is not set, the write is ignored. This is the only 1183c2ecb424SMostafa Saleh * permitted behavior in SMMUv3.2 and later. 1184c2ecb424SMostafa Saleh */ 1185c2ecb424SMostafa Saleh if (data & R_GBPA_UPDATE_MASK) { 1186c2ecb424SMostafa Saleh /* Ignore update bit as write is synchronous. */ 1187c2ecb424SMostafa Saleh s->gbpa = data & ~R_GBPA_UPDATE_MASK; 1188c2ecb424SMostafa Saleh } 1189c2ecb424SMostafa Saleh return MEMTX_OK; 1190fae4be38SEric Auger case A_STRTAB_BASE: /* 64b */ 1191fae4be38SEric Auger s->strtab_base = deposit64(s->strtab_base, 0, 32, data); 1192fae4be38SEric Auger return MEMTX_OK; 1193fae4be38SEric Auger case A_STRTAB_BASE + 4: 1194fae4be38SEric Auger s->strtab_base = deposit64(s->strtab_base, 32, 32, data); 1195fae4be38SEric Auger return MEMTX_OK; 1196fae4be38SEric Auger case A_STRTAB_BASE_CFG: 1197fae4be38SEric Auger s->strtab_base_cfg = data; 1198fae4be38SEric Auger if (FIELD_EX32(data, STRTAB_BASE_CFG, FMT) == 1) { 1199fae4be38SEric Auger s->sid_split = FIELD_EX32(data, STRTAB_BASE_CFG, SPLIT); 1200fae4be38SEric Auger s->features |= SMMU_FEATURE_2LVL_STE; 1201fae4be38SEric Auger } 1202fae4be38SEric Auger return MEMTX_OK; 1203fae4be38SEric Auger case A_CMDQ_BASE: /* 64b */ 1204fae4be38SEric Auger s->cmdq.base = deposit64(s->cmdq.base, 0, 32, data); 1205fae4be38SEric Auger s->cmdq.log2size = extract64(s->cmdq.base, 0, 5); 1206fae4be38SEric Auger if (s->cmdq.log2size > SMMU_CMDQS) { 1207fae4be38SEric Auger s->cmdq.log2size = SMMU_CMDQS; 1208fae4be38SEric Auger } 1209fae4be38SEric Auger return MEMTX_OK; 1210fae4be38SEric Auger case A_CMDQ_BASE + 4: /* 64b */ 1211fae4be38SEric Auger s->cmdq.base = deposit64(s->cmdq.base, 32, 32, data); 1212fae4be38SEric Auger return MEMTX_OK; 1213fae4be38SEric Auger case A_CMDQ_PROD: 1214fae4be38SEric Auger s->cmdq.prod = data; 1215fae4be38SEric Auger smmuv3_cmdq_consume(s); 1216fae4be38SEric Auger return MEMTX_OK; 1217fae4be38SEric Auger case A_CMDQ_CONS: 1218fae4be38SEric Auger s->cmdq.cons = data; 1219fae4be38SEric Auger return MEMTX_OK; 1220fae4be38SEric Auger case A_EVENTQ_BASE: /* 64b */ 1221fae4be38SEric Auger s->eventq.base = deposit64(s->eventq.base, 0, 32, data); 1222fae4be38SEric Auger s->eventq.log2size = extract64(s->eventq.base, 0, 5); 1223fae4be38SEric Auger if (s->eventq.log2size > SMMU_EVENTQS) { 1224fae4be38SEric Auger s->eventq.log2size = SMMU_EVENTQS; 1225fae4be38SEric Auger } 1226fae4be38SEric Auger return MEMTX_OK; 1227fae4be38SEric Auger case A_EVENTQ_BASE + 4: 1228fae4be38SEric Auger s->eventq.base = deposit64(s->eventq.base, 32, 32, data); 1229fae4be38SEric Auger return MEMTX_OK; 1230fae4be38SEric Auger case A_EVENTQ_PROD: 1231fae4be38SEric Auger s->eventq.prod = data; 1232fae4be38SEric Auger return MEMTX_OK; 1233fae4be38SEric Auger case A_EVENTQ_CONS: 1234fae4be38SEric Auger s->eventq.cons = data; 1235fae4be38SEric Auger return MEMTX_OK; 1236fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0: /* 64b */ 1237fae4be38SEric Auger s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 0, 32, data); 1238fae4be38SEric Auger return MEMTX_OK; 1239fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0 + 4: 1240fae4be38SEric Auger s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 32, 32, data); 1241fae4be38SEric Auger return MEMTX_OK; 1242fae4be38SEric Auger case A_EVENTQ_IRQ_CFG1: 1243fae4be38SEric Auger s->eventq_irq_cfg1 = data; 1244fae4be38SEric Auger return MEMTX_OK; 1245fae4be38SEric Auger case A_EVENTQ_IRQ_CFG2: 1246fae4be38SEric Auger s->eventq_irq_cfg2 = data; 1247fae4be38SEric Auger return MEMTX_OK; 1248fae4be38SEric Auger default: 1249fae4be38SEric Auger qemu_log_mask(LOG_UNIMP, 1250fae4be38SEric Auger "%s Unexpected 32-bit access to 0x%"PRIx64" (WI)\n", 1251fae4be38SEric Auger __func__, offset); 1252fae4be38SEric Auger return MEMTX_OK; 1253fae4be38SEric Auger } 1254fae4be38SEric Auger } 1255fae4be38SEric Auger 125610a83cb9SPrem Mallappa static MemTxResult smmu_write_mmio(void *opaque, hwaddr offset, uint64_t data, 125710a83cb9SPrem Mallappa unsigned size, MemTxAttrs attrs) 125810a83cb9SPrem Mallappa { 1259fae4be38SEric Auger SMMUState *sys = opaque; 1260fae4be38SEric Auger SMMUv3State *s = ARM_SMMUV3(sys); 1261fae4be38SEric Auger MemTxResult r; 1262fae4be38SEric Auger 1263fae4be38SEric Auger /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */ 1264fae4be38SEric Auger offset &= ~0x10000; 1265fae4be38SEric Auger 1266fae4be38SEric Auger switch (size) { 1267fae4be38SEric Auger case 8: 1268fae4be38SEric Auger r = smmu_writell(s, offset, data, attrs); 1269fae4be38SEric Auger break; 1270fae4be38SEric Auger case 4: 1271fae4be38SEric Auger r = smmu_writel(s, offset, data, attrs); 1272fae4be38SEric Auger break; 1273fae4be38SEric Auger default: 1274fae4be38SEric Auger r = MEMTX_ERROR; 1275fae4be38SEric Auger break; 1276fae4be38SEric Auger } 1277fae4be38SEric Auger 1278fae4be38SEric Auger trace_smmuv3_write_mmio(offset, data, size, r); 1279fae4be38SEric Auger return r; 128010a83cb9SPrem Mallappa } 128110a83cb9SPrem Mallappa 128210a83cb9SPrem Mallappa static MemTxResult smmu_readll(SMMUv3State *s, hwaddr offset, 128310a83cb9SPrem Mallappa uint64_t *data, MemTxAttrs attrs) 128410a83cb9SPrem Mallappa { 128510a83cb9SPrem Mallappa switch (offset) { 128610a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0: 128710a83cb9SPrem Mallappa *data = s->gerror_irq_cfg0; 128810a83cb9SPrem Mallappa return MEMTX_OK; 128910a83cb9SPrem Mallappa case A_STRTAB_BASE: 129010a83cb9SPrem Mallappa *data = s->strtab_base; 129110a83cb9SPrem Mallappa return MEMTX_OK; 129210a83cb9SPrem Mallappa case A_CMDQ_BASE: 129310a83cb9SPrem Mallappa *data = s->cmdq.base; 129410a83cb9SPrem Mallappa return MEMTX_OK; 129510a83cb9SPrem Mallappa case A_EVENTQ_BASE: 129610a83cb9SPrem Mallappa *data = s->eventq.base; 129710a83cb9SPrem Mallappa return MEMTX_OK; 129810a83cb9SPrem Mallappa default: 129910a83cb9SPrem Mallappa *data = 0; 130010a83cb9SPrem Mallappa qemu_log_mask(LOG_UNIMP, 130110a83cb9SPrem Mallappa "%s Unexpected 64-bit access to 0x%"PRIx64" (RAZ)\n", 130210a83cb9SPrem Mallappa __func__, offset); 130310a83cb9SPrem Mallappa return MEMTX_OK; 130410a83cb9SPrem Mallappa } 130510a83cb9SPrem Mallappa } 130610a83cb9SPrem Mallappa 130710a83cb9SPrem Mallappa static MemTxResult smmu_readl(SMMUv3State *s, hwaddr offset, 130810a83cb9SPrem Mallappa uint64_t *data, MemTxAttrs attrs) 130910a83cb9SPrem Mallappa { 131010a83cb9SPrem Mallappa switch (offset) { 131197fb318dSPeter Maydell case A_IDREGS ... A_IDREGS + 0x2f: 131210a83cb9SPrem Mallappa *data = smmuv3_idreg(offset - A_IDREGS); 131310a83cb9SPrem Mallappa return MEMTX_OK; 131410a83cb9SPrem Mallappa case A_IDR0 ... A_IDR5: 131510a83cb9SPrem Mallappa *data = s->idr[(offset - A_IDR0) / 4]; 131610a83cb9SPrem Mallappa return MEMTX_OK; 131710a83cb9SPrem Mallappa case A_IIDR: 131810a83cb9SPrem Mallappa *data = s->iidr; 131910a83cb9SPrem Mallappa return MEMTX_OK; 13205888f0adSEric Auger case A_AIDR: 13215888f0adSEric Auger *data = s->aidr; 13225888f0adSEric Auger return MEMTX_OK; 132310a83cb9SPrem Mallappa case A_CR0: 132410a83cb9SPrem Mallappa *data = s->cr[0]; 132510a83cb9SPrem Mallappa return MEMTX_OK; 132610a83cb9SPrem Mallappa case A_CR0ACK: 132710a83cb9SPrem Mallappa *data = s->cr0ack; 132810a83cb9SPrem Mallappa return MEMTX_OK; 132910a83cb9SPrem Mallappa case A_CR1: 133010a83cb9SPrem Mallappa *data = s->cr[1]; 133110a83cb9SPrem Mallappa return MEMTX_OK; 133210a83cb9SPrem Mallappa case A_CR2: 133310a83cb9SPrem Mallappa *data = s->cr[2]; 133410a83cb9SPrem Mallappa return MEMTX_OK; 133510a83cb9SPrem Mallappa case A_STATUSR: 133610a83cb9SPrem Mallappa *data = s->statusr; 133710a83cb9SPrem Mallappa return MEMTX_OK; 1338c2ecb424SMostafa Saleh case A_GBPA: 1339c2ecb424SMostafa Saleh *data = s->gbpa; 1340c2ecb424SMostafa Saleh return MEMTX_OK; 134110a83cb9SPrem Mallappa case A_IRQ_CTRL: 134210a83cb9SPrem Mallappa case A_IRQ_CTRL_ACK: 134310a83cb9SPrem Mallappa *data = s->irq_ctrl; 134410a83cb9SPrem Mallappa return MEMTX_OK; 134510a83cb9SPrem Mallappa case A_GERROR: 134610a83cb9SPrem Mallappa *data = s->gerror; 134710a83cb9SPrem Mallappa return MEMTX_OK; 134810a83cb9SPrem Mallappa case A_GERRORN: 134910a83cb9SPrem Mallappa *data = s->gerrorn; 135010a83cb9SPrem Mallappa return MEMTX_OK; 135110a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0: /* 64b */ 135210a83cb9SPrem Mallappa *data = extract64(s->gerror_irq_cfg0, 0, 32); 135310a83cb9SPrem Mallappa return MEMTX_OK; 135410a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0 + 4: 135510a83cb9SPrem Mallappa *data = extract64(s->gerror_irq_cfg0, 32, 32); 135610a83cb9SPrem Mallappa return MEMTX_OK; 135710a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG1: 135810a83cb9SPrem Mallappa *data = s->gerror_irq_cfg1; 135910a83cb9SPrem Mallappa return MEMTX_OK; 136010a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG2: 136110a83cb9SPrem Mallappa *data = s->gerror_irq_cfg2; 136210a83cb9SPrem Mallappa return MEMTX_OK; 136310a83cb9SPrem Mallappa case A_STRTAB_BASE: /* 64b */ 136410a83cb9SPrem Mallappa *data = extract64(s->strtab_base, 0, 32); 136510a83cb9SPrem Mallappa return MEMTX_OK; 136610a83cb9SPrem Mallappa case A_STRTAB_BASE + 4: /* 64b */ 136710a83cb9SPrem Mallappa *data = extract64(s->strtab_base, 32, 32); 136810a83cb9SPrem Mallappa return MEMTX_OK; 136910a83cb9SPrem Mallappa case A_STRTAB_BASE_CFG: 137010a83cb9SPrem Mallappa *data = s->strtab_base_cfg; 137110a83cb9SPrem Mallappa return MEMTX_OK; 137210a83cb9SPrem Mallappa case A_CMDQ_BASE: /* 64b */ 137310a83cb9SPrem Mallappa *data = extract64(s->cmdq.base, 0, 32); 137410a83cb9SPrem Mallappa return MEMTX_OK; 137510a83cb9SPrem Mallappa case A_CMDQ_BASE + 4: 137610a83cb9SPrem Mallappa *data = extract64(s->cmdq.base, 32, 32); 137710a83cb9SPrem Mallappa return MEMTX_OK; 137810a83cb9SPrem Mallappa case A_CMDQ_PROD: 137910a83cb9SPrem Mallappa *data = s->cmdq.prod; 138010a83cb9SPrem Mallappa return MEMTX_OK; 138110a83cb9SPrem Mallappa case A_CMDQ_CONS: 138210a83cb9SPrem Mallappa *data = s->cmdq.cons; 138310a83cb9SPrem Mallappa return MEMTX_OK; 138410a83cb9SPrem Mallappa case A_EVENTQ_BASE: /* 64b */ 138510a83cb9SPrem Mallappa *data = extract64(s->eventq.base, 0, 32); 138610a83cb9SPrem Mallappa return MEMTX_OK; 138710a83cb9SPrem Mallappa case A_EVENTQ_BASE + 4: /* 64b */ 138810a83cb9SPrem Mallappa *data = extract64(s->eventq.base, 32, 32); 138910a83cb9SPrem Mallappa return MEMTX_OK; 139010a83cb9SPrem Mallappa case A_EVENTQ_PROD: 139110a83cb9SPrem Mallappa *data = s->eventq.prod; 139210a83cb9SPrem Mallappa return MEMTX_OK; 139310a83cb9SPrem Mallappa case A_EVENTQ_CONS: 139410a83cb9SPrem Mallappa *data = s->eventq.cons; 139510a83cb9SPrem Mallappa return MEMTX_OK; 139610a83cb9SPrem Mallappa default: 139710a83cb9SPrem Mallappa *data = 0; 139810a83cb9SPrem Mallappa qemu_log_mask(LOG_UNIMP, 139910a83cb9SPrem Mallappa "%s unhandled 32-bit access at 0x%"PRIx64" (RAZ)\n", 140010a83cb9SPrem Mallappa __func__, offset); 140110a83cb9SPrem Mallappa return MEMTX_OK; 140210a83cb9SPrem Mallappa } 140310a83cb9SPrem Mallappa } 140410a83cb9SPrem Mallappa 140510a83cb9SPrem Mallappa static MemTxResult smmu_read_mmio(void *opaque, hwaddr offset, uint64_t *data, 140610a83cb9SPrem Mallappa unsigned size, MemTxAttrs attrs) 140710a83cb9SPrem Mallappa { 140810a83cb9SPrem Mallappa SMMUState *sys = opaque; 140910a83cb9SPrem Mallappa SMMUv3State *s = ARM_SMMUV3(sys); 141010a83cb9SPrem Mallappa MemTxResult r; 141110a83cb9SPrem Mallappa 141210a83cb9SPrem Mallappa /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */ 141310a83cb9SPrem Mallappa offset &= ~0x10000; 141410a83cb9SPrem Mallappa 141510a83cb9SPrem Mallappa switch (size) { 141610a83cb9SPrem Mallappa case 8: 141710a83cb9SPrem Mallappa r = smmu_readll(s, offset, data, attrs); 141810a83cb9SPrem Mallappa break; 141910a83cb9SPrem Mallappa case 4: 142010a83cb9SPrem Mallappa r = smmu_readl(s, offset, data, attrs); 142110a83cb9SPrem Mallappa break; 142210a83cb9SPrem Mallappa default: 142310a83cb9SPrem Mallappa r = MEMTX_ERROR; 142410a83cb9SPrem Mallappa break; 142510a83cb9SPrem Mallappa } 142610a83cb9SPrem Mallappa 142710a83cb9SPrem Mallappa trace_smmuv3_read_mmio(offset, *data, size, r); 142810a83cb9SPrem Mallappa return r; 142910a83cb9SPrem Mallappa } 143010a83cb9SPrem Mallappa 143110a83cb9SPrem Mallappa static const MemoryRegionOps smmu_mem_ops = { 143210a83cb9SPrem Mallappa .read_with_attrs = smmu_read_mmio, 143310a83cb9SPrem Mallappa .write_with_attrs = smmu_write_mmio, 143410a83cb9SPrem Mallappa .endianness = DEVICE_LITTLE_ENDIAN, 143510a83cb9SPrem Mallappa .valid = { 143610a83cb9SPrem Mallappa .min_access_size = 4, 143710a83cb9SPrem Mallappa .max_access_size = 8, 143810a83cb9SPrem Mallappa }, 143910a83cb9SPrem Mallappa .impl = { 144010a83cb9SPrem Mallappa .min_access_size = 4, 144110a83cb9SPrem Mallappa .max_access_size = 8, 144210a83cb9SPrem Mallappa }, 144310a83cb9SPrem Mallappa }; 144410a83cb9SPrem Mallappa 144510a83cb9SPrem Mallappa static void smmu_init_irq(SMMUv3State *s, SysBusDevice *dev) 144610a83cb9SPrem Mallappa { 144710a83cb9SPrem Mallappa int i; 144810a83cb9SPrem Mallappa 144910a83cb9SPrem Mallappa for (i = 0; i < ARRAY_SIZE(s->irq); i++) { 145010a83cb9SPrem Mallappa sysbus_init_irq(dev, &s->irq[i]); 145110a83cb9SPrem Mallappa } 145210a83cb9SPrem Mallappa } 145310a83cb9SPrem Mallappa 1454503819a3SPeter Maydell static void smmu_reset_hold(Object *obj) 145510a83cb9SPrem Mallappa { 1456503819a3SPeter Maydell SMMUv3State *s = ARM_SMMUV3(obj); 145710a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s); 145810a83cb9SPrem Mallappa 1459503819a3SPeter Maydell if (c->parent_phases.hold) { 1460503819a3SPeter Maydell c->parent_phases.hold(obj); 1461503819a3SPeter Maydell } 146210a83cb9SPrem Mallappa 146310a83cb9SPrem Mallappa smmuv3_init_regs(s); 146410a83cb9SPrem Mallappa } 146510a83cb9SPrem Mallappa 146610a83cb9SPrem Mallappa static void smmu_realize(DeviceState *d, Error **errp) 146710a83cb9SPrem Mallappa { 146810a83cb9SPrem Mallappa SMMUState *sys = ARM_SMMU(d); 146910a83cb9SPrem Mallappa SMMUv3State *s = ARM_SMMUV3(sys); 147010a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s); 147110a83cb9SPrem Mallappa SysBusDevice *dev = SYS_BUS_DEVICE(d); 147210a83cb9SPrem Mallappa Error *local_err = NULL; 147310a83cb9SPrem Mallappa 147410a83cb9SPrem Mallappa c->parent_realize(d, &local_err); 147510a83cb9SPrem Mallappa if (local_err) { 147610a83cb9SPrem Mallappa error_propagate(errp, local_err); 147710a83cb9SPrem Mallappa return; 147810a83cb9SPrem Mallappa } 147910a83cb9SPrem Mallappa 148032cfd7f3SEric Auger qemu_mutex_init(&s->mutex); 148132cfd7f3SEric Auger 148210a83cb9SPrem Mallappa memory_region_init_io(&sys->iomem, OBJECT(s), 148310a83cb9SPrem Mallappa &smmu_mem_ops, sys, TYPE_ARM_SMMUV3, 0x20000); 148410a83cb9SPrem Mallappa 148510a83cb9SPrem Mallappa sys->mrtypename = TYPE_SMMUV3_IOMMU_MEMORY_REGION; 148610a83cb9SPrem Mallappa 148710a83cb9SPrem Mallappa sysbus_init_mmio(dev, &sys->iomem); 148810a83cb9SPrem Mallappa 148910a83cb9SPrem Mallappa smmu_init_irq(s, dev); 149010a83cb9SPrem Mallappa } 149110a83cb9SPrem Mallappa 149210a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3_queue = { 149310a83cb9SPrem Mallappa .name = "smmuv3_queue", 149410a83cb9SPrem Mallappa .version_id = 1, 149510a83cb9SPrem Mallappa .minimum_version_id = 1, 149610a83cb9SPrem Mallappa .fields = (VMStateField[]) { 149710a83cb9SPrem Mallappa VMSTATE_UINT64(base, SMMUQueue), 149810a83cb9SPrem Mallappa VMSTATE_UINT32(prod, SMMUQueue), 149910a83cb9SPrem Mallappa VMSTATE_UINT32(cons, SMMUQueue), 150010a83cb9SPrem Mallappa VMSTATE_UINT8(log2size, SMMUQueue), 1501758b71f7SDr. David Alan Gilbert VMSTATE_END_OF_LIST(), 150210a83cb9SPrem Mallappa }, 150310a83cb9SPrem Mallappa }; 150410a83cb9SPrem Mallappa 1505c2ecb424SMostafa Saleh static bool smmuv3_gbpa_needed(void *opaque) 1506c2ecb424SMostafa Saleh { 1507c2ecb424SMostafa Saleh SMMUv3State *s = opaque; 1508c2ecb424SMostafa Saleh 1509c2ecb424SMostafa Saleh /* Only migrate GBPA if it has different reset value. */ 1510c2ecb424SMostafa Saleh return s->gbpa != SMMU_GBPA_RESET_VAL; 1511c2ecb424SMostafa Saleh } 1512c2ecb424SMostafa Saleh 1513c2ecb424SMostafa Saleh static const VMStateDescription vmstate_gbpa = { 1514c2ecb424SMostafa Saleh .name = "smmuv3/gbpa", 1515c2ecb424SMostafa Saleh .version_id = 1, 1516c2ecb424SMostafa Saleh .minimum_version_id = 1, 1517c2ecb424SMostafa Saleh .needed = smmuv3_gbpa_needed, 1518c2ecb424SMostafa Saleh .fields = (VMStateField[]) { 1519c2ecb424SMostafa Saleh VMSTATE_UINT32(gbpa, SMMUv3State), 1520c2ecb424SMostafa Saleh VMSTATE_END_OF_LIST() 1521c2ecb424SMostafa Saleh } 1522c2ecb424SMostafa Saleh }; 1523c2ecb424SMostafa Saleh 152410a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3 = { 152510a83cb9SPrem Mallappa .name = "smmuv3", 152610a83cb9SPrem Mallappa .version_id = 1, 152710a83cb9SPrem Mallappa .minimum_version_id = 1, 1528a55aab61SZenghui Yu .priority = MIG_PRI_IOMMU, 152910a83cb9SPrem Mallappa .fields = (VMStateField[]) { 153010a83cb9SPrem Mallappa VMSTATE_UINT32(features, SMMUv3State), 153110a83cb9SPrem Mallappa VMSTATE_UINT8(sid_size, SMMUv3State), 153210a83cb9SPrem Mallappa VMSTATE_UINT8(sid_split, SMMUv3State), 153310a83cb9SPrem Mallappa 153410a83cb9SPrem Mallappa VMSTATE_UINT32_ARRAY(cr, SMMUv3State, 3), 153510a83cb9SPrem Mallappa VMSTATE_UINT32(cr0ack, SMMUv3State), 153610a83cb9SPrem Mallappa VMSTATE_UINT32(statusr, SMMUv3State), 153710a83cb9SPrem Mallappa VMSTATE_UINT32(irq_ctrl, SMMUv3State), 153810a83cb9SPrem Mallappa VMSTATE_UINT32(gerror, SMMUv3State), 153910a83cb9SPrem Mallappa VMSTATE_UINT32(gerrorn, SMMUv3State), 154010a83cb9SPrem Mallappa VMSTATE_UINT64(gerror_irq_cfg0, SMMUv3State), 154110a83cb9SPrem Mallappa VMSTATE_UINT32(gerror_irq_cfg1, SMMUv3State), 154210a83cb9SPrem Mallappa VMSTATE_UINT32(gerror_irq_cfg2, SMMUv3State), 154310a83cb9SPrem Mallappa VMSTATE_UINT64(strtab_base, SMMUv3State), 154410a83cb9SPrem Mallappa VMSTATE_UINT32(strtab_base_cfg, SMMUv3State), 154510a83cb9SPrem Mallappa VMSTATE_UINT64(eventq_irq_cfg0, SMMUv3State), 154610a83cb9SPrem Mallappa VMSTATE_UINT32(eventq_irq_cfg1, SMMUv3State), 154710a83cb9SPrem Mallappa VMSTATE_UINT32(eventq_irq_cfg2, SMMUv3State), 154810a83cb9SPrem Mallappa 154910a83cb9SPrem Mallappa VMSTATE_STRUCT(cmdq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue), 155010a83cb9SPrem Mallappa VMSTATE_STRUCT(eventq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue), 155110a83cb9SPrem Mallappa 155210a83cb9SPrem Mallappa VMSTATE_END_OF_LIST(), 155310a83cb9SPrem Mallappa }, 1554c2ecb424SMostafa Saleh .subsections = (const VMStateDescription * []) { 1555c2ecb424SMostafa Saleh &vmstate_gbpa, 1556c2ecb424SMostafa Saleh NULL 1557c2ecb424SMostafa Saleh } 155810a83cb9SPrem Mallappa }; 155910a83cb9SPrem Mallappa 156010a83cb9SPrem Mallappa static void smmuv3_instance_init(Object *obj) 156110a83cb9SPrem Mallappa { 156210a83cb9SPrem Mallappa /* Nothing much to do here as of now */ 156310a83cb9SPrem Mallappa } 156410a83cb9SPrem Mallappa 156510a83cb9SPrem Mallappa static void smmuv3_class_init(ObjectClass *klass, void *data) 156610a83cb9SPrem Mallappa { 156710a83cb9SPrem Mallappa DeviceClass *dc = DEVICE_CLASS(klass); 1568503819a3SPeter Maydell ResettableClass *rc = RESETTABLE_CLASS(klass); 156910a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_CLASS(klass); 157010a83cb9SPrem Mallappa 157110a83cb9SPrem Mallappa dc->vmsd = &vmstate_smmuv3; 1572503819a3SPeter Maydell resettable_class_set_parent_phases(rc, NULL, smmu_reset_hold, NULL, 1573503819a3SPeter Maydell &c->parent_phases); 157410a83cb9SPrem Mallappa c->parent_realize = dc->realize; 157510a83cb9SPrem Mallappa dc->realize = smmu_realize; 157610a83cb9SPrem Mallappa } 157710a83cb9SPrem Mallappa 1578549d4005SEric Auger static int smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu, 15790d1ac82eSEric Auger IOMMUNotifierFlag old, 1580549d4005SEric Auger IOMMUNotifierFlag new, 1581549d4005SEric Auger Error **errp) 15820d1ac82eSEric Auger { 1583832e4222SEric Auger SMMUDevice *sdev = container_of(iommu, SMMUDevice, iommu); 1584832e4222SEric Auger SMMUv3State *s3 = sdev->smmu; 1585832e4222SEric Auger SMMUState *s = &(s3->smmu_state); 1586832e4222SEric Auger 1587958ec334SPeter Xu if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP) { 1588958ec334SPeter Xu error_setg(errp, "SMMUv3 does not support dev-iotlb yet"); 1589958ec334SPeter Xu return -EINVAL; 1590958ec334SPeter Xu } 1591958ec334SPeter Xu 1592832e4222SEric Auger if (new & IOMMU_NOTIFIER_MAP) { 1593549d4005SEric Auger error_setg(errp, 1594549d4005SEric Auger "device %02x.%02x.%x requires iommu MAP notifier which is " 1595549d4005SEric Auger "not currently supported", pci_bus_num(sdev->bus), 1596549d4005SEric Auger PCI_SLOT(sdev->devfn), PCI_FUNC(sdev->devfn)); 1597549d4005SEric Auger return -EINVAL; 1598832e4222SEric Auger } 1599832e4222SEric Auger 16000d1ac82eSEric Auger if (old == IOMMU_NOTIFIER_NONE) { 1601832e4222SEric Auger trace_smmuv3_notify_flag_add(iommu->parent_obj.name); 1602c6370441SEric Auger QLIST_INSERT_HEAD(&s->devices_with_notifiers, sdev, next); 1603c6370441SEric Auger } else if (new == IOMMU_NOTIFIER_NONE) { 1604832e4222SEric Auger trace_smmuv3_notify_flag_del(iommu->parent_obj.name); 1605c6370441SEric Auger QLIST_REMOVE(sdev, next); 16060d1ac82eSEric Auger } 1607549d4005SEric Auger return 0; 16080d1ac82eSEric Auger } 16090d1ac82eSEric Auger 161010a83cb9SPrem Mallappa static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass, 161110a83cb9SPrem Mallappa void *data) 161210a83cb9SPrem Mallappa { 16139bde7f06SEric Auger IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass); 16149bde7f06SEric Auger 16159bde7f06SEric Auger imrc->translate = smmuv3_translate; 16160d1ac82eSEric Auger imrc->notify_flag_changed = smmuv3_notify_flag_changed; 161710a83cb9SPrem Mallappa } 161810a83cb9SPrem Mallappa 161910a83cb9SPrem Mallappa static const TypeInfo smmuv3_type_info = { 162010a83cb9SPrem Mallappa .name = TYPE_ARM_SMMUV3, 162110a83cb9SPrem Mallappa .parent = TYPE_ARM_SMMU, 162210a83cb9SPrem Mallappa .instance_size = sizeof(SMMUv3State), 162310a83cb9SPrem Mallappa .instance_init = smmuv3_instance_init, 162410a83cb9SPrem Mallappa .class_size = sizeof(SMMUv3Class), 162510a83cb9SPrem Mallappa .class_init = smmuv3_class_init, 162610a83cb9SPrem Mallappa }; 162710a83cb9SPrem Mallappa 162810a83cb9SPrem Mallappa static const TypeInfo smmuv3_iommu_memory_region_info = { 162910a83cb9SPrem Mallappa .parent = TYPE_IOMMU_MEMORY_REGION, 163010a83cb9SPrem Mallappa .name = TYPE_SMMUV3_IOMMU_MEMORY_REGION, 163110a83cb9SPrem Mallappa .class_init = smmuv3_iommu_memory_region_class_init, 163210a83cb9SPrem Mallappa }; 163310a83cb9SPrem Mallappa 163410a83cb9SPrem Mallappa static void smmuv3_register_types(void) 163510a83cb9SPrem Mallappa { 163610a83cb9SPrem Mallappa type_register(&smmuv3_type_info); 163710a83cb9SPrem Mallappa type_register(&smmuv3_iommu_memory_region_info); 163810a83cb9SPrem Mallappa } 163910a83cb9SPrem Mallappa 164010a83cb9SPrem Mallappa type_init(smmuv3_register_types) 164110a83cb9SPrem Mallappa 1642