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" 2010a83cb9SPrem Mallappa #include "hw/boards.h" 2110a83cb9SPrem Mallappa #include "sysemu/sysemu.h" 2210a83cb9SPrem Mallappa #include "hw/sysbus.h" 2310a83cb9SPrem Mallappa #include "hw/qdev-core.h" 2410a83cb9SPrem Mallappa #include "hw/pci/pci.h" 2510a83cb9SPrem Mallappa #include "exec/address-spaces.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" 3410a83cb9SPrem Mallappa 356a736033SEric Auger /** 366a736033SEric Auger * smmuv3_trigger_irq - pulse @irq if enabled and update 376a736033SEric Auger * GERROR register in case of GERROR interrupt 386a736033SEric Auger * 396a736033SEric Auger * @irq: irq type 406a736033SEric Auger * @gerror_mask: mask of gerrors to toggle (relevant if @irq is GERROR) 416a736033SEric Auger */ 42fae4be38SEric Auger static void smmuv3_trigger_irq(SMMUv3State *s, SMMUIrq irq, 43fae4be38SEric Auger uint32_t gerror_mask) 446a736033SEric Auger { 456a736033SEric Auger 466a736033SEric Auger bool pulse = false; 476a736033SEric Auger 486a736033SEric Auger switch (irq) { 496a736033SEric Auger case SMMU_IRQ_EVTQ: 506a736033SEric Auger pulse = smmuv3_eventq_irq_enabled(s); 516a736033SEric Auger break; 526a736033SEric Auger case SMMU_IRQ_PRIQ: 536a736033SEric Auger qemu_log_mask(LOG_UNIMP, "PRI not yet supported\n"); 546a736033SEric Auger break; 556a736033SEric Auger case SMMU_IRQ_CMD_SYNC: 566a736033SEric Auger pulse = true; 576a736033SEric Auger break; 586a736033SEric Auger case SMMU_IRQ_GERROR: 596a736033SEric Auger { 606a736033SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 616a736033SEric Auger uint32_t new_gerrors = ~pending & gerror_mask; 626a736033SEric Auger 636a736033SEric Auger if (!new_gerrors) { 646a736033SEric Auger /* only toggle non pending errors */ 656a736033SEric Auger return; 666a736033SEric Auger } 676a736033SEric Auger s->gerror ^= new_gerrors; 686a736033SEric Auger trace_smmuv3_write_gerror(new_gerrors, s->gerror); 696a736033SEric Auger 706a736033SEric Auger pulse = smmuv3_gerror_irq_enabled(s); 716a736033SEric Auger break; 726a736033SEric Auger } 736a736033SEric Auger } 746a736033SEric Auger if (pulse) { 756a736033SEric Auger trace_smmuv3_trigger_irq(irq); 766a736033SEric Auger qemu_irq_pulse(s->irq[irq]); 776a736033SEric Auger } 786a736033SEric Auger } 796a736033SEric Auger 80fae4be38SEric Auger static void smmuv3_write_gerrorn(SMMUv3State *s, uint32_t new_gerrorn) 816a736033SEric Auger { 826a736033SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 836a736033SEric Auger uint32_t toggled = s->gerrorn ^ new_gerrorn; 846a736033SEric Auger 856a736033SEric Auger if (toggled & ~pending) { 866a736033SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 876a736033SEric Auger "guest toggles non pending errors = 0x%x\n", 886a736033SEric Auger toggled & ~pending); 896a736033SEric Auger } 906a736033SEric Auger 916a736033SEric Auger /* 926a736033SEric Auger * We do not raise any error in case guest toggles bits corresponding 936a736033SEric Auger * to not active IRQs (CONSTRAINED UNPREDICTABLE) 946a736033SEric Auger */ 956a736033SEric Auger s->gerrorn = new_gerrorn; 966a736033SEric Auger 976a736033SEric Auger trace_smmuv3_write_gerrorn(toggled & pending, s->gerrorn); 986a736033SEric Auger } 996a736033SEric Auger 100dadd1a08SEric Auger static inline MemTxResult queue_read(SMMUQueue *q, void *data) 101dadd1a08SEric Auger { 102dadd1a08SEric Auger dma_addr_t addr = Q_CONS_ENTRY(q); 103dadd1a08SEric Auger 104dadd1a08SEric Auger return dma_memory_read(&address_space_memory, addr, data, q->entry_size); 105dadd1a08SEric Auger } 106dadd1a08SEric Auger 107dadd1a08SEric Auger static MemTxResult queue_write(SMMUQueue *q, void *data) 108dadd1a08SEric Auger { 109dadd1a08SEric Auger dma_addr_t addr = Q_PROD_ENTRY(q); 110dadd1a08SEric Auger MemTxResult ret; 111dadd1a08SEric Auger 112dadd1a08SEric Auger ret = dma_memory_write(&address_space_memory, addr, data, q->entry_size); 113dadd1a08SEric Auger if (ret != MEMTX_OK) { 114dadd1a08SEric Auger return ret; 115dadd1a08SEric Auger } 116dadd1a08SEric Auger 117dadd1a08SEric Auger queue_prod_incr(q); 118dadd1a08SEric Auger return MEMTX_OK; 119dadd1a08SEric Auger } 120dadd1a08SEric Auger 121bb981004SEric Auger static MemTxResult smmuv3_write_eventq(SMMUv3State *s, Evt *evt) 122dadd1a08SEric Auger { 123dadd1a08SEric Auger SMMUQueue *q = &s->eventq; 124bb981004SEric Auger MemTxResult r; 125bb981004SEric Auger 126bb981004SEric Auger if (!smmuv3_eventq_enabled(s)) { 127bb981004SEric Auger return MEMTX_ERROR; 128bb981004SEric Auger } 129bb981004SEric Auger 130bb981004SEric Auger if (smmuv3_q_full(q)) { 131bb981004SEric Auger return MEMTX_ERROR; 132bb981004SEric Auger } 133bb981004SEric Auger 134bb981004SEric Auger r = queue_write(q, evt); 135bb981004SEric Auger if (r != MEMTX_OK) { 136bb981004SEric Auger return r; 137bb981004SEric Auger } 138bb981004SEric Auger 139bb981004SEric Auger if (smmuv3_q_empty(q)) { 140bb981004SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_EVTQ, 0); 141bb981004SEric Auger } 142bb981004SEric Auger return MEMTX_OK; 143bb981004SEric Auger } 144bb981004SEric Auger 145bb981004SEric Auger void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info) 146bb981004SEric Auger { 14724af32e0SEric Auger Evt evt = {}; 148bb981004SEric Auger MemTxResult r; 149dadd1a08SEric Auger 150dadd1a08SEric Auger if (!smmuv3_eventq_enabled(s)) { 151dadd1a08SEric Auger return; 152dadd1a08SEric Auger } 153dadd1a08SEric Auger 154bb981004SEric Auger EVT_SET_TYPE(&evt, info->type); 155bb981004SEric Auger EVT_SET_SID(&evt, info->sid); 156bb981004SEric Auger 157bb981004SEric Auger switch (info->type) { 1589122bea9SJia He case SMMU_EVT_NONE: 159dadd1a08SEric Auger return; 160bb981004SEric Auger case SMMU_EVT_F_UUT: 161bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_uut.ssid); 162bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_uut.ssv); 163bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_uut.addr); 164bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_uut.rnw); 165bb981004SEric Auger EVT_SET_PNU(&evt, info->u.f_uut.pnu); 166bb981004SEric Auger EVT_SET_IND(&evt, info->u.f_uut.ind); 167bb981004SEric Auger break; 168bb981004SEric Auger case SMMU_EVT_C_BAD_STREAMID: 169bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_streamid.ssid); 170bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_streamid.ssv); 171bb981004SEric Auger break; 172bb981004SEric Auger case SMMU_EVT_F_STE_FETCH: 173bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_ste_fetch.ssid); 174bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_ste_fetch.ssv); 175bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_ste_fetch.addr); 176bb981004SEric Auger break; 177bb981004SEric Auger case SMMU_EVT_C_BAD_STE: 178bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_ste.ssid); 179bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_ste.ssv); 180bb981004SEric Auger break; 181bb981004SEric Auger case SMMU_EVT_F_STREAM_DISABLED: 182bb981004SEric Auger break; 183bb981004SEric Auger case SMMU_EVT_F_TRANS_FORBIDDEN: 184bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_transl_forbidden.addr); 185bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_transl_forbidden.rnw); 186bb981004SEric Auger break; 187bb981004SEric Auger case SMMU_EVT_C_BAD_SUBSTREAMID: 188bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_substream.ssid); 189bb981004SEric Auger break; 190bb981004SEric Auger case SMMU_EVT_F_CD_FETCH: 191bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_cd_fetch.ssid); 192bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_cd_fetch.ssv); 193bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_cd_fetch.addr); 194bb981004SEric Auger break; 195bb981004SEric Auger case SMMU_EVT_C_BAD_CD: 196bb981004SEric Auger EVT_SET_SSID(&evt, info->u.c_bad_cd.ssid); 197bb981004SEric Auger EVT_SET_SSV(&evt, info->u.c_bad_cd.ssv); 198bb981004SEric Auger break; 199bb981004SEric Auger case SMMU_EVT_F_WALK_EABT: 200bb981004SEric Auger case SMMU_EVT_F_TRANSLATION: 201bb981004SEric Auger case SMMU_EVT_F_ADDR_SIZE: 202bb981004SEric Auger case SMMU_EVT_F_ACCESS: 203bb981004SEric Auger case SMMU_EVT_F_PERMISSION: 204bb981004SEric Auger EVT_SET_STALL(&evt, info->u.f_walk_eabt.stall); 205bb981004SEric Auger EVT_SET_STAG(&evt, info->u.f_walk_eabt.stag); 206bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_walk_eabt.ssid); 207bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_walk_eabt.ssv); 208bb981004SEric Auger EVT_SET_S2(&evt, info->u.f_walk_eabt.s2); 209bb981004SEric Auger EVT_SET_ADDR(&evt, info->u.f_walk_eabt.addr); 210bb981004SEric Auger EVT_SET_RNW(&evt, info->u.f_walk_eabt.rnw); 211bb981004SEric Auger EVT_SET_PNU(&evt, info->u.f_walk_eabt.pnu); 212bb981004SEric Auger EVT_SET_IND(&evt, info->u.f_walk_eabt.ind); 213bb981004SEric Auger EVT_SET_CLASS(&evt, info->u.f_walk_eabt.class); 214bb981004SEric Auger EVT_SET_ADDR2(&evt, info->u.f_walk_eabt.addr2); 215bb981004SEric Auger break; 216bb981004SEric Auger case SMMU_EVT_F_CFG_CONFLICT: 217bb981004SEric Auger EVT_SET_SSID(&evt, info->u.f_cfg_conflict.ssid); 218bb981004SEric Auger EVT_SET_SSV(&evt, info->u.f_cfg_conflict.ssv); 219bb981004SEric Auger break; 220bb981004SEric Auger /* rest is not implemented */ 221bb981004SEric Auger case SMMU_EVT_F_BAD_ATS_TREQ: 222bb981004SEric Auger case SMMU_EVT_F_TLB_CONFLICT: 223bb981004SEric Auger case SMMU_EVT_E_PAGE_REQ: 224bb981004SEric Auger default: 225bb981004SEric Auger g_assert_not_reached(); 226dadd1a08SEric Auger } 227dadd1a08SEric Auger 228bb981004SEric Auger trace_smmuv3_record_event(smmu_event_string(info->type), info->sid); 229bb981004SEric Auger r = smmuv3_write_eventq(s, &evt); 230bb981004SEric Auger if (r != MEMTX_OK) { 231bb981004SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_EVENTQ_ABT_ERR_MASK); 232dadd1a08SEric Auger } 233bb981004SEric Auger info->recorded = true; 234dadd1a08SEric Auger } 235dadd1a08SEric Auger 23610a83cb9SPrem Mallappa static void smmuv3_init_regs(SMMUv3State *s) 23710a83cb9SPrem Mallappa { 23810a83cb9SPrem Mallappa /** 23910a83cb9SPrem Mallappa * IDR0: stage1 only, AArch64 only, coherent access, 16b ASID, 24010a83cb9SPrem Mallappa * multi-level stream table 24110a83cb9SPrem Mallappa */ 24210a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S1P, 1); /* stage 1 supported */ 24310a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTF, 2); /* AArch64 PTW only */ 24410a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, COHACC, 1); /* IO coherent */ 24510a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, ASID16, 1); /* 16-bit ASID */ 24610a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTENDIAN, 2); /* little endian */ 24710a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STALL_MODEL, 1); /* No stall */ 24810a83cb9SPrem Mallappa /* terminated transaction will always be aborted/error returned */ 24910a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TERM_MODEL, 1); 25010a83cb9SPrem Mallappa /* 2-level stream table supported */ 25110a83cb9SPrem Mallappa s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STLEVEL, 1); 25210a83cb9SPrem Mallappa 25310a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, SIDSIZE, SMMU_IDR1_SIDSIZE); 25410a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, EVENTQS, SMMU_EVENTQS); 25510a83cb9SPrem Mallappa s->idr[1] = FIELD_DP32(s->idr[1], IDR1, CMDQS, SMMU_CMDQS); 25610a83cb9SPrem Mallappa 25710a83cb9SPrem Mallappa /* 4K and 64K granule support */ 25810a83cb9SPrem Mallappa s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN4K, 1); 25910a83cb9SPrem Mallappa s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN64K, 1); 26010a83cb9SPrem Mallappa s->idr[5] = FIELD_DP32(s->idr[5], IDR5, OAS, SMMU_IDR5_OAS); /* 44 bits */ 26110a83cb9SPrem Mallappa 26210a83cb9SPrem Mallappa s->cmdq.base = deposit64(s->cmdq.base, 0, 5, SMMU_CMDQS); 26310a83cb9SPrem Mallappa s->cmdq.prod = 0; 26410a83cb9SPrem Mallappa s->cmdq.cons = 0; 26510a83cb9SPrem Mallappa s->cmdq.entry_size = sizeof(struct Cmd); 26610a83cb9SPrem Mallappa s->eventq.base = deposit64(s->eventq.base, 0, 5, SMMU_EVENTQS); 26710a83cb9SPrem Mallappa s->eventq.prod = 0; 26810a83cb9SPrem Mallappa s->eventq.cons = 0; 26910a83cb9SPrem Mallappa s->eventq.entry_size = sizeof(struct Evt); 27010a83cb9SPrem Mallappa 27110a83cb9SPrem Mallappa s->features = 0; 27210a83cb9SPrem Mallappa s->sid_split = 0; 27310a83cb9SPrem Mallappa } 27410a83cb9SPrem Mallappa 2759bde7f06SEric Auger static int smmu_get_ste(SMMUv3State *s, dma_addr_t addr, STE *buf, 2769bde7f06SEric Auger SMMUEventInfo *event) 2779bde7f06SEric Auger { 2789bde7f06SEric Auger int ret; 2799bde7f06SEric Auger 2809bde7f06SEric Auger trace_smmuv3_get_ste(addr); 2819bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 2829bde7f06SEric Auger ret = dma_memory_read(&address_space_memory, addr, 2839bde7f06SEric Auger (void *)buf, sizeof(*buf)); 2849bde7f06SEric Auger if (ret != MEMTX_OK) { 2859bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 2869bde7f06SEric Auger "Cannot fetch pte at address=0x%"PRIx64"\n", addr); 2879bde7f06SEric Auger event->type = SMMU_EVT_F_STE_FETCH; 2889bde7f06SEric Auger event->u.f_ste_fetch.addr = addr; 2899bde7f06SEric Auger return -EINVAL; 2909bde7f06SEric Auger } 2919bde7f06SEric Auger return 0; 2929bde7f06SEric Auger 2939bde7f06SEric Auger } 2949bde7f06SEric Auger 2959bde7f06SEric Auger /* @ssid > 0 not supported yet */ 2969bde7f06SEric Auger static int smmu_get_cd(SMMUv3State *s, STE *ste, uint32_t ssid, 2979bde7f06SEric Auger CD *buf, SMMUEventInfo *event) 2989bde7f06SEric Auger { 2999bde7f06SEric Auger dma_addr_t addr = STE_CTXPTR(ste); 3009bde7f06SEric Auger int ret; 3019bde7f06SEric Auger 3029bde7f06SEric Auger trace_smmuv3_get_cd(addr); 3039bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 3049bde7f06SEric Auger ret = dma_memory_read(&address_space_memory, addr, 3059bde7f06SEric Auger (void *)buf, sizeof(*buf)); 3069bde7f06SEric Auger if (ret != MEMTX_OK) { 3079bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 3089bde7f06SEric Auger "Cannot fetch pte at address=0x%"PRIx64"\n", addr); 3099bde7f06SEric Auger event->type = SMMU_EVT_F_CD_FETCH; 3109bde7f06SEric Auger event->u.f_ste_fetch.addr = addr; 3119bde7f06SEric Auger return -EINVAL; 3129bde7f06SEric Auger } 3139bde7f06SEric Auger return 0; 3149bde7f06SEric Auger } 3159bde7f06SEric Auger 3169122bea9SJia He /* Returns < 0 in case of invalid STE, 0 otherwise */ 3179bde7f06SEric Auger static int decode_ste(SMMUv3State *s, SMMUTransCfg *cfg, 3189bde7f06SEric Auger STE *ste, SMMUEventInfo *event) 3199bde7f06SEric Auger { 3209bde7f06SEric Auger uint32_t config; 3219bde7f06SEric Auger 3229bde7f06SEric Auger if (!STE_VALID(ste)) { 3239bde7f06SEric Auger goto bad_ste; 3249bde7f06SEric Auger } 3259bde7f06SEric Auger 3269bde7f06SEric Auger config = STE_CONFIG(ste); 3279bde7f06SEric Auger 3289bde7f06SEric Auger if (STE_CFG_ABORT(config)) { 3299122bea9SJia He cfg->aborted = true; 3309122bea9SJia He return 0; 3319bde7f06SEric Auger } 3329bde7f06SEric Auger 3339bde7f06SEric Auger if (STE_CFG_BYPASS(config)) { 3349bde7f06SEric Auger cfg->bypassed = true; 3359122bea9SJia He return 0; 3369bde7f06SEric Auger } 3379bde7f06SEric Auger 3389bde7f06SEric Auger if (STE_CFG_S2_ENABLED(config)) { 3399bde7f06SEric Auger qemu_log_mask(LOG_UNIMP, "SMMUv3 does not support stage 2 yet\n"); 3409bde7f06SEric Auger goto bad_ste; 3419bde7f06SEric Auger } 3429bde7f06SEric Auger 3439bde7f06SEric Auger if (STE_S1CDMAX(ste) != 0) { 3449bde7f06SEric Auger qemu_log_mask(LOG_UNIMP, 3459bde7f06SEric Auger "SMMUv3 does not support multiple context descriptors yet\n"); 3469bde7f06SEric Auger goto bad_ste; 3479bde7f06SEric Auger } 3489bde7f06SEric Auger 3499bde7f06SEric Auger if (STE_S1STALLD(ste)) { 3509bde7f06SEric Auger qemu_log_mask(LOG_UNIMP, 3519bde7f06SEric Auger "SMMUv3 S1 stalling fault model not allowed yet\n"); 3529bde7f06SEric Auger goto bad_ste; 3539bde7f06SEric Auger } 3549bde7f06SEric Auger return 0; 3559bde7f06SEric Auger 3569bde7f06SEric Auger bad_ste: 3579bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STE; 3589bde7f06SEric Auger return -EINVAL; 3599bde7f06SEric Auger } 3609bde7f06SEric Auger 3619bde7f06SEric Auger /** 3629bde7f06SEric Auger * smmu_find_ste - Return the stream table entry associated 3639bde7f06SEric Auger * to the sid 3649bde7f06SEric Auger * 3659bde7f06SEric Auger * @s: smmuv3 handle 3669bde7f06SEric Auger * @sid: stream ID 3679bde7f06SEric Auger * @ste: returned stream table entry 3689bde7f06SEric Auger * @event: handle to an event info 3699bde7f06SEric Auger * 3709bde7f06SEric Auger * Supports linear and 2-level stream table 3719bde7f06SEric Auger * Return 0 on success, -EINVAL otherwise 3729bde7f06SEric Auger */ 3739bde7f06SEric Auger static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, 3749bde7f06SEric Auger SMMUEventInfo *event) 3759bde7f06SEric Auger { 3769bde7f06SEric Auger dma_addr_t addr; 3779bde7f06SEric Auger int ret; 3789bde7f06SEric Auger 3799bde7f06SEric Auger trace_smmuv3_find_ste(sid, s->features, s->sid_split); 3809bde7f06SEric Auger /* Check SID range */ 3819bde7f06SEric Auger if (sid > (1 << SMMU_IDR1_SIDSIZE)) { 3829bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STREAMID; 3839bde7f06SEric Auger return -EINVAL; 3849bde7f06SEric Auger } 3859bde7f06SEric Auger if (s->features & SMMU_FEATURE_2LVL_STE) { 3869bde7f06SEric Auger int l1_ste_offset, l2_ste_offset, max_l2_ste, span; 3879bde7f06SEric Auger dma_addr_t strtab_base, l1ptr, l2ptr; 3889bde7f06SEric Auger STEDesc l1std; 3899bde7f06SEric Auger 3909bde7f06SEric Auger strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK; 3919bde7f06SEric Auger l1_ste_offset = sid >> s->sid_split; 3929bde7f06SEric Auger l2_ste_offset = sid & ((1 << s->sid_split) - 1); 3939bde7f06SEric Auger l1ptr = (dma_addr_t)(strtab_base + l1_ste_offset * sizeof(l1std)); 3949bde7f06SEric Auger /* TODO: guarantee 64-bit single-copy atomicity */ 3959bde7f06SEric Auger ret = dma_memory_read(&address_space_memory, l1ptr, 3969bde7f06SEric Auger (uint8_t *)&l1std, sizeof(l1std)); 3979bde7f06SEric Auger if (ret != MEMTX_OK) { 3989bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 3999bde7f06SEric Auger "Could not read L1PTR at 0X%"PRIx64"\n", l1ptr); 4009bde7f06SEric Auger event->type = SMMU_EVT_F_STE_FETCH; 4019bde7f06SEric Auger event->u.f_ste_fetch.addr = l1ptr; 4029bde7f06SEric Auger return -EINVAL; 4039bde7f06SEric Auger } 4049bde7f06SEric Auger 4059bde7f06SEric Auger span = L1STD_SPAN(&l1std); 4069bde7f06SEric Auger 4079bde7f06SEric Auger if (!span) { 4089bde7f06SEric Auger /* l2ptr is not valid */ 4099bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 4109bde7f06SEric Auger "invalid sid=%d (L1STD span=0)\n", sid); 4119bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STREAMID; 4129bde7f06SEric Auger return -EINVAL; 4139bde7f06SEric Auger } 4149bde7f06SEric Auger max_l2_ste = (1 << span) - 1; 4159bde7f06SEric Auger l2ptr = l1std_l2ptr(&l1std); 4169bde7f06SEric Auger trace_smmuv3_find_ste_2lvl(s->strtab_base, l1ptr, l1_ste_offset, 4179bde7f06SEric Auger l2ptr, l2_ste_offset, max_l2_ste); 4189bde7f06SEric Auger if (l2_ste_offset > max_l2_ste) { 4199bde7f06SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 4209bde7f06SEric Auger "l2_ste_offset=%d > max_l2_ste=%d\n", 4219bde7f06SEric Auger l2_ste_offset, max_l2_ste); 4229bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_STE; 4239bde7f06SEric Auger return -EINVAL; 4249bde7f06SEric Auger } 4259bde7f06SEric Auger addr = l2ptr + l2_ste_offset * sizeof(*ste); 4269bde7f06SEric Auger } else { 4279bde7f06SEric Auger addr = s->strtab_base + sid * sizeof(*ste); 4289bde7f06SEric Auger } 4299bde7f06SEric Auger 4309bde7f06SEric Auger if (smmu_get_ste(s, addr, ste, event)) { 4319bde7f06SEric Auger return -EINVAL; 4329bde7f06SEric Auger } 4339bde7f06SEric Auger 4349bde7f06SEric Auger return 0; 4359bde7f06SEric Auger } 4369bde7f06SEric Auger 4379bde7f06SEric Auger static int decode_cd(SMMUTransCfg *cfg, CD *cd, SMMUEventInfo *event) 4389bde7f06SEric Auger { 4399bde7f06SEric Auger int ret = -EINVAL; 4409bde7f06SEric Auger int i; 4419bde7f06SEric Auger 4429bde7f06SEric Auger if (!CD_VALID(cd) || !CD_AARCH64(cd)) { 4439bde7f06SEric Auger goto bad_cd; 4449bde7f06SEric Auger } 4459bde7f06SEric Auger if (!CD_A(cd)) { 4469bde7f06SEric Auger goto bad_cd; /* SMMU_IDR0.TERM_MODEL == 1 */ 4479bde7f06SEric Auger } 4489bde7f06SEric Auger if (CD_S(cd)) { 4499bde7f06SEric Auger goto bad_cd; /* !STE_SECURE && SMMU_IDR0.STALL_MODEL == 1 */ 4509bde7f06SEric Auger } 4519bde7f06SEric Auger if (CD_HA(cd) || CD_HD(cd)) { 4529bde7f06SEric Auger goto bad_cd; /* HTTU = 0 */ 4539bde7f06SEric Auger } 4549bde7f06SEric Auger 4559bde7f06SEric Auger /* we support only those at the moment */ 4569bde7f06SEric Auger cfg->aa64 = true; 4579bde7f06SEric Auger cfg->stage = 1; 4589bde7f06SEric Auger 4599bde7f06SEric Auger cfg->oas = oas2bits(CD_IPS(cd)); 4609bde7f06SEric Auger cfg->oas = MIN(oas2bits(SMMU_IDR5_OAS), cfg->oas); 4619bde7f06SEric Auger cfg->tbi = CD_TBI(cd); 4629bde7f06SEric Auger cfg->asid = CD_ASID(cd); 4639bde7f06SEric Auger 4649bde7f06SEric Auger trace_smmuv3_decode_cd(cfg->oas); 4659bde7f06SEric Auger 4669bde7f06SEric Auger /* decode data dependent on TT */ 4679bde7f06SEric Auger for (i = 0; i <= 1; i++) { 4689bde7f06SEric Auger int tg, tsz; 4699bde7f06SEric Auger SMMUTransTableInfo *tt = &cfg->tt[i]; 4709bde7f06SEric Auger 4719bde7f06SEric Auger cfg->tt[i].disabled = CD_EPD(cd, i); 4729bde7f06SEric Auger if (cfg->tt[i].disabled) { 4739bde7f06SEric Auger continue; 4749bde7f06SEric Auger } 4759bde7f06SEric Auger 4769bde7f06SEric Auger tsz = CD_TSZ(cd, i); 4779bde7f06SEric Auger if (tsz < 16 || tsz > 39) { 4789bde7f06SEric Auger goto bad_cd; 4799bde7f06SEric Auger } 4809bde7f06SEric Auger 4819bde7f06SEric Auger tg = CD_TG(cd, i); 4829bde7f06SEric Auger tt->granule_sz = tg2granule(tg, i); 4839bde7f06SEric Auger if ((tt->granule_sz != 12 && tt->granule_sz != 16) || CD_ENDI(cd)) { 4849bde7f06SEric Auger goto bad_cd; 4859bde7f06SEric Auger } 4869bde7f06SEric Auger 4879bde7f06SEric Auger tt->tsz = tsz; 4889bde7f06SEric Auger tt->ttb = CD_TTB(cd, i); 4899bde7f06SEric Auger if (tt->ttb & ~(MAKE_64BIT_MASK(0, cfg->oas))) { 4909bde7f06SEric Auger goto bad_cd; 4919bde7f06SEric Auger } 4929bde7f06SEric Auger trace_smmuv3_decode_cd_tt(i, tt->tsz, tt->ttb, tt->granule_sz); 4939bde7f06SEric Auger } 4949bde7f06SEric Auger 4959bde7f06SEric Auger event->record_trans_faults = CD_R(cd); 4969bde7f06SEric Auger 4979bde7f06SEric Auger return 0; 4989bde7f06SEric Auger 4999bde7f06SEric Auger bad_cd: 5009bde7f06SEric Auger event->type = SMMU_EVT_C_BAD_CD; 5019bde7f06SEric Auger return ret; 5029bde7f06SEric Auger } 5039bde7f06SEric Auger 5049bde7f06SEric Auger /** 5059bde7f06SEric Auger * smmuv3_decode_config - Prepare the translation configuration 5069bde7f06SEric Auger * for the @mr iommu region 5079bde7f06SEric Auger * @mr: iommu memory region the translation config must be prepared for 5089bde7f06SEric Auger * @cfg: output translation configuration which is populated through 5099bde7f06SEric Auger * the different configuration decoding steps 5109bde7f06SEric Auger * @event: must be zero'ed by the caller 5119bde7f06SEric Auger * 5129122bea9SJia He * return < 0 in case of config decoding error (@event is filled 5139bde7f06SEric Auger * accordingly). Return 0 otherwise. 5149bde7f06SEric Auger */ 5159bde7f06SEric Auger static int smmuv3_decode_config(IOMMUMemoryRegion *mr, SMMUTransCfg *cfg, 5169bde7f06SEric Auger SMMUEventInfo *event) 5179bde7f06SEric Auger { 5189bde7f06SEric Auger SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu); 5199bde7f06SEric Auger uint32_t sid = smmu_get_sid(sdev); 5209bde7f06SEric Auger SMMUv3State *s = sdev->smmu; 5219122bea9SJia He int ret; 5229bde7f06SEric Auger STE ste; 5239bde7f06SEric Auger CD cd; 5249bde7f06SEric Auger 5259122bea9SJia He ret = smmu_find_ste(s, sid, &ste, event); 5269122bea9SJia He if (ret) { 5279bde7f06SEric Auger return ret; 5289bde7f06SEric Auger } 5299bde7f06SEric Auger 5309122bea9SJia He ret = decode_ste(s, cfg, &ste, event); 5319122bea9SJia He if (ret) { 5329bde7f06SEric Auger return ret; 5339bde7f06SEric Auger } 5349bde7f06SEric Auger 5359122bea9SJia He if (cfg->aborted || cfg->bypassed) { 5369122bea9SJia He return 0; 5379122bea9SJia He } 5389122bea9SJia He 5399122bea9SJia He ret = smmu_get_cd(s, &ste, 0 /* ssid */, &cd, event); 5409122bea9SJia He if (ret) { 5419bde7f06SEric Auger return ret; 5429bde7f06SEric Auger } 5439bde7f06SEric Auger 5449bde7f06SEric Auger return decode_cd(cfg, &cd, event); 5459bde7f06SEric Auger } 5469bde7f06SEric Auger 547*32cfd7f3SEric Auger /** 548*32cfd7f3SEric Auger * smmuv3_get_config - Look up for a cached copy of configuration data for 549*32cfd7f3SEric Auger * @sdev and on cache miss performs a configuration structure decoding from 550*32cfd7f3SEric Auger * guest RAM. 551*32cfd7f3SEric Auger * 552*32cfd7f3SEric Auger * @sdev: SMMUDevice handle 553*32cfd7f3SEric Auger * @event: output event info 554*32cfd7f3SEric Auger * 555*32cfd7f3SEric Auger * The configuration cache contains data resulting from both STE and CD 556*32cfd7f3SEric Auger * decoding under the form of an SMMUTransCfg struct. The hash table is indexed 557*32cfd7f3SEric Auger * by the SMMUDevice handle. 558*32cfd7f3SEric Auger */ 559*32cfd7f3SEric Auger static SMMUTransCfg *smmuv3_get_config(SMMUDevice *sdev, SMMUEventInfo *event) 560*32cfd7f3SEric Auger { 561*32cfd7f3SEric Auger SMMUv3State *s = sdev->smmu; 562*32cfd7f3SEric Auger SMMUState *bc = &s->smmu_state; 563*32cfd7f3SEric Auger SMMUTransCfg *cfg; 564*32cfd7f3SEric Auger 565*32cfd7f3SEric Auger cfg = g_hash_table_lookup(bc->configs, sdev); 566*32cfd7f3SEric Auger if (cfg) { 567*32cfd7f3SEric Auger sdev->cfg_cache_hits++; 568*32cfd7f3SEric Auger trace_smmuv3_config_cache_hit(smmu_get_sid(sdev), 569*32cfd7f3SEric Auger sdev->cfg_cache_hits, sdev->cfg_cache_misses, 570*32cfd7f3SEric Auger 100 * sdev->cfg_cache_hits / 571*32cfd7f3SEric Auger (sdev->cfg_cache_hits + sdev->cfg_cache_misses)); 572*32cfd7f3SEric Auger } else { 573*32cfd7f3SEric Auger sdev->cfg_cache_misses++; 574*32cfd7f3SEric Auger trace_smmuv3_config_cache_miss(smmu_get_sid(sdev), 575*32cfd7f3SEric Auger sdev->cfg_cache_hits, sdev->cfg_cache_misses, 576*32cfd7f3SEric Auger 100 * sdev->cfg_cache_hits / 577*32cfd7f3SEric Auger (sdev->cfg_cache_hits + sdev->cfg_cache_misses)); 578*32cfd7f3SEric Auger cfg = g_new0(SMMUTransCfg, 1); 579*32cfd7f3SEric Auger 580*32cfd7f3SEric Auger if (!smmuv3_decode_config(&sdev->iommu, cfg, event)) { 581*32cfd7f3SEric Auger g_hash_table_insert(bc->configs, sdev, cfg); 582*32cfd7f3SEric Auger } else { 583*32cfd7f3SEric Auger g_free(cfg); 584*32cfd7f3SEric Auger cfg = NULL; 585*32cfd7f3SEric Auger } 586*32cfd7f3SEric Auger } 587*32cfd7f3SEric Auger return cfg; 588*32cfd7f3SEric Auger } 589*32cfd7f3SEric Auger 590*32cfd7f3SEric Auger static void smmuv3_flush_config(SMMUDevice *sdev) 591*32cfd7f3SEric Auger { 592*32cfd7f3SEric Auger SMMUv3State *s = sdev->smmu; 593*32cfd7f3SEric Auger SMMUState *bc = &s->smmu_state; 594*32cfd7f3SEric Auger 595*32cfd7f3SEric Auger trace_smmuv3_config_cache_inv(smmu_get_sid(sdev)); 596*32cfd7f3SEric Auger g_hash_table_remove(bc->configs, sdev); 597*32cfd7f3SEric Auger } 598*32cfd7f3SEric Auger 5999bde7f06SEric Auger static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr, 6002c91bcf2SPeter Maydell IOMMUAccessFlags flag, int iommu_idx) 6019bde7f06SEric Auger { 6029bde7f06SEric Auger SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu); 6039bde7f06SEric Auger SMMUv3State *s = sdev->smmu; 6049bde7f06SEric Auger uint32_t sid = smmu_get_sid(sdev); 6059122bea9SJia He SMMUEventInfo event = {.type = SMMU_EVT_NONE, .sid = sid}; 6069bde7f06SEric Auger SMMUPTWEventInfo ptw_info = {}; 6079122bea9SJia He SMMUTranslationStatus status; 608*32cfd7f3SEric Auger SMMUTransCfg *cfg = NULL; 6099bde7f06SEric Auger IOMMUTLBEntry entry = { 6109bde7f06SEric Auger .target_as = &address_space_memory, 6119bde7f06SEric Auger .iova = addr, 6129bde7f06SEric Auger .translated_addr = addr, 6139bde7f06SEric Auger .addr_mask = ~(hwaddr)0, 6149bde7f06SEric Auger .perm = IOMMU_NONE, 6159bde7f06SEric Auger }; 6169bde7f06SEric Auger 617*32cfd7f3SEric Auger qemu_mutex_lock(&s->mutex); 618*32cfd7f3SEric Auger 6199bde7f06SEric Auger if (!smmu_enabled(s)) { 6209122bea9SJia He status = SMMU_TRANS_DISABLE; 6219122bea9SJia He goto epilogue; 6229bde7f06SEric Auger } 6239bde7f06SEric Auger 624*32cfd7f3SEric Auger cfg = smmuv3_get_config(sdev, &event); 625*32cfd7f3SEric Auger if (!cfg) { 6269122bea9SJia He status = SMMU_TRANS_ERROR; 6279122bea9SJia He goto epilogue; 6289bde7f06SEric Auger } 6299bde7f06SEric Auger 630*32cfd7f3SEric Auger if (cfg->aborted) { 6319122bea9SJia He status = SMMU_TRANS_ABORT; 6329122bea9SJia He goto epilogue; 6339bde7f06SEric Auger } 6349bde7f06SEric Auger 635*32cfd7f3SEric Auger if (cfg->bypassed) { 6369122bea9SJia He status = SMMU_TRANS_BYPASS; 6379122bea9SJia He goto epilogue; 6389122bea9SJia He } 6399122bea9SJia He 640*32cfd7f3SEric Auger if (smmu_ptw(cfg, addr, flag, &entry, &ptw_info)) { 6419bde7f06SEric Auger switch (ptw_info.type) { 6429bde7f06SEric Auger case SMMU_PTW_ERR_WALK_EABT: 6439bde7f06SEric Auger event.type = SMMU_EVT_F_WALK_EABT; 6449bde7f06SEric Auger event.u.f_walk_eabt.addr = addr; 6459bde7f06SEric Auger event.u.f_walk_eabt.rnw = flag & 0x1; 6469bde7f06SEric Auger event.u.f_walk_eabt.class = 0x1; 6479bde7f06SEric Auger event.u.f_walk_eabt.addr2 = ptw_info.addr; 6489bde7f06SEric Auger break; 6499bde7f06SEric Auger case SMMU_PTW_ERR_TRANSLATION: 6509bde7f06SEric Auger if (event.record_trans_faults) { 6519bde7f06SEric Auger event.type = SMMU_EVT_F_TRANSLATION; 6529bde7f06SEric Auger event.u.f_translation.addr = addr; 6539bde7f06SEric Auger event.u.f_translation.rnw = flag & 0x1; 6549bde7f06SEric Auger } 6559bde7f06SEric Auger break; 6569bde7f06SEric Auger case SMMU_PTW_ERR_ADDR_SIZE: 6579bde7f06SEric Auger if (event.record_trans_faults) { 6589bde7f06SEric Auger event.type = SMMU_EVT_F_ADDR_SIZE; 6599bde7f06SEric Auger event.u.f_addr_size.addr = addr; 6609bde7f06SEric Auger event.u.f_addr_size.rnw = flag & 0x1; 6619bde7f06SEric Auger } 6629bde7f06SEric Auger break; 6639bde7f06SEric Auger case SMMU_PTW_ERR_ACCESS: 6649bde7f06SEric Auger if (event.record_trans_faults) { 6659bde7f06SEric Auger event.type = SMMU_EVT_F_ACCESS; 6669bde7f06SEric Auger event.u.f_access.addr = addr; 6679bde7f06SEric Auger event.u.f_access.rnw = flag & 0x1; 6689bde7f06SEric Auger } 6699bde7f06SEric Auger break; 6709bde7f06SEric Auger case SMMU_PTW_ERR_PERMISSION: 6719bde7f06SEric Auger if (event.record_trans_faults) { 6729bde7f06SEric Auger event.type = SMMU_EVT_F_PERMISSION; 6739bde7f06SEric Auger event.u.f_permission.addr = addr; 6749bde7f06SEric Auger event.u.f_permission.rnw = flag & 0x1; 6759bde7f06SEric Auger } 6769bde7f06SEric Auger break; 6779bde7f06SEric Auger default: 6789bde7f06SEric Auger g_assert_not_reached(); 6799bde7f06SEric Auger } 6809122bea9SJia He status = SMMU_TRANS_ERROR; 6819122bea9SJia He } else { 6829122bea9SJia He status = SMMU_TRANS_SUCCESS; 6839bde7f06SEric Auger } 6849122bea9SJia He 6859122bea9SJia He epilogue: 686*32cfd7f3SEric Auger qemu_mutex_unlock(&s->mutex); 6879122bea9SJia He switch (status) { 6889122bea9SJia He case SMMU_TRANS_SUCCESS: 6899bde7f06SEric Auger entry.perm = flag; 6909122bea9SJia He trace_smmuv3_translate_success(mr->parent_obj.name, sid, addr, 6919bde7f06SEric Auger entry.translated_addr, entry.perm); 6929122bea9SJia He break; 6939122bea9SJia He case SMMU_TRANS_DISABLE: 6949122bea9SJia He entry.perm = flag; 6959122bea9SJia He entry.addr_mask = ~TARGET_PAGE_MASK; 6969122bea9SJia He trace_smmuv3_translate_disable(mr->parent_obj.name, sid, addr, 6979122bea9SJia He entry.perm); 6989122bea9SJia He break; 6999122bea9SJia He case SMMU_TRANS_BYPASS: 7009122bea9SJia He entry.perm = flag; 7019122bea9SJia He entry.addr_mask = ~TARGET_PAGE_MASK; 7029122bea9SJia He trace_smmuv3_translate_bypass(mr->parent_obj.name, sid, addr, 7039122bea9SJia He entry.perm); 7049122bea9SJia He break; 7059122bea9SJia He case SMMU_TRANS_ABORT: 7069122bea9SJia He /* no event is recorded on abort */ 7079122bea9SJia He trace_smmuv3_translate_abort(mr->parent_obj.name, sid, addr, 7089122bea9SJia He entry.perm); 7099122bea9SJia He break; 7109122bea9SJia He case SMMU_TRANS_ERROR: 7119122bea9SJia He qemu_log_mask(LOG_GUEST_ERROR, 7129122bea9SJia He "%s translation failed for iova=0x%"PRIx64"(%s)\n", 7139122bea9SJia He mr->parent_obj.name, addr, smmu_event_string(event.type)); 7149122bea9SJia He smmuv3_record_event(s, &event); 7159122bea9SJia He break; 7169bde7f06SEric Auger } 7179bde7f06SEric Auger 7189bde7f06SEric Auger return entry; 7199bde7f06SEric Auger } 7209bde7f06SEric Auger 721fae4be38SEric Auger static int smmuv3_cmdq_consume(SMMUv3State *s) 722dadd1a08SEric Auger { 723*32cfd7f3SEric Auger SMMUState *bs = ARM_SMMU(s); 724dadd1a08SEric Auger SMMUCmdError cmd_error = SMMU_CERROR_NONE; 725dadd1a08SEric Auger SMMUQueue *q = &s->cmdq; 726dadd1a08SEric Auger SMMUCommandType type = 0; 727dadd1a08SEric Auger 728dadd1a08SEric Auger if (!smmuv3_cmdq_enabled(s)) { 729dadd1a08SEric Auger return 0; 730dadd1a08SEric Auger } 731dadd1a08SEric Auger /* 732dadd1a08SEric Auger * some commands depend on register values, typically CR0. In case those 733dadd1a08SEric Auger * register values change while handling the command, spec says it 734dadd1a08SEric Auger * is UNPREDICTABLE whether the command is interpreted under the new 735dadd1a08SEric Auger * or old value. 736dadd1a08SEric Auger */ 737dadd1a08SEric Auger 738dadd1a08SEric Auger while (!smmuv3_q_empty(q)) { 739dadd1a08SEric Auger uint32_t pending = s->gerror ^ s->gerrorn; 740dadd1a08SEric Auger Cmd cmd; 741dadd1a08SEric Auger 742dadd1a08SEric Auger trace_smmuv3_cmdq_consume(Q_PROD(q), Q_CONS(q), 743dadd1a08SEric Auger Q_PROD_WRAP(q), Q_CONS_WRAP(q)); 744dadd1a08SEric Auger 745dadd1a08SEric Auger if (FIELD_EX32(pending, GERROR, CMDQ_ERR)) { 746dadd1a08SEric Auger break; 747dadd1a08SEric Auger } 748dadd1a08SEric Auger 749dadd1a08SEric Auger if (queue_read(q, &cmd) != MEMTX_OK) { 750dadd1a08SEric Auger cmd_error = SMMU_CERROR_ABT; 751dadd1a08SEric Auger break; 752dadd1a08SEric Auger } 753dadd1a08SEric Auger 754dadd1a08SEric Auger type = CMD_TYPE(&cmd); 755dadd1a08SEric Auger 756dadd1a08SEric Auger trace_smmuv3_cmdq_opcode(smmu_cmd_string(type)); 757dadd1a08SEric Auger 758*32cfd7f3SEric Auger qemu_mutex_lock(&s->mutex); 759dadd1a08SEric Auger switch (type) { 760dadd1a08SEric Auger case SMMU_CMD_SYNC: 761dadd1a08SEric Auger if (CMD_SYNC_CS(&cmd) & CMD_SYNC_SIG_IRQ) { 762dadd1a08SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_CMD_SYNC, 0); 763dadd1a08SEric Auger } 764dadd1a08SEric Auger break; 765dadd1a08SEric Auger case SMMU_CMD_PREFETCH_CONFIG: 766dadd1a08SEric Auger case SMMU_CMD_PREFETCH_ADDR: 767*32cfd7f3SEric Auger break; 768dadd1a08SEric Auger case SMMU_CMD_CFGI_STE: 769*32cfd7f3SEric Auger { 770*32cfd7f3SEric Auger uint32_t sid = CMD_SID(&cmd); 771*32cfd7f3SEric Auger IOMMUMemoryRegion *mr = smmu_iommu_mr(bs, sid); 772*32cfd7f3SEric Auger SMMUDevice *sdev; 773*32cfd7f3SEric Auger 774*32cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 775*32cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 776*32cfd7f3SEric Auger break; 777*32cfd7f3SEric Auger } 778*32cfd7f3SEric Auger 779*32cfd7f3SEric Auger if (!mr) { 780*32cfd7f3SEric Auger break; 781*32cfd7f3SEric Auger } 782*32cfd7f3SEric Auger 783*32cfd7f3SEric Auger trace_smmuv3_cmdq_cfgi_ste(sid); 784*32cfd7f3SEric Auger sdev = container_of(mr, SMMUDevice, iommu); 785*32cfd7f3SEric Auger smmuv3_flush_config(sdev); 786*32cfd7f3SEric Auger 787*32cfd7f3SEric Auger break; 788*32cfd7f3SEric Auger } 789dadd1a08SEric Auger case SMMU_CMD_CFGI_STE_RANGE: /* same as SMMU_CMD_CFGI_ALL */ 790*32cfd7f3SEric Auger { 791*32cfd7f3SEric Auger uint32_t start = CMD_SID(&cmd), end, i; 792*32cfd7f3SEric Auger uint8_t range = CMD_STE_RANGE(&cmd); 793*32cfd7f3SEric Auger 794*32cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 795*32cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 796*32cfd7f3SEric Auger break; 797*32cfd7f3SEric Auger } 798*32cfd7f3SEric Auger 799*32cfd7f3SEric Auger end = start + (1 << (range + 1)) - 1; 800*32cfd7f3SEric Auger trace_smmuv3_cmdq_cfgi_ste_range(start, end); 801*32cfd7f3SEric Auger 802*32cfd7f3SEric Auger for (i = start; i <= end; i++) { 803*32cfd7f3SEric Auger IOMMUMemoryRegion *mr = smmu_iommu_mr(bs, i); 804*32cfd7f3SEric Auger SMMUDevice *sdev; 805*32cfd7f3SEric Auger 806*32cfd7f3SEric Auger if (!mr) { 807*32cfd7f3SEric Auger continue; 808*32cfd7f3SEric Auger } 809*32cfd7f3SEric Auger sdev = container_of(mr, SMMUDevice, iommu); 810*32cfd7f3SEric Auger smmuv3_flush_config(sdev); 811*32cfd7f3SEric Auger } 812*32cfd7f3SEric Auger break; 813*32cfd7f3SEric Auger } 814dadd1a08SEric Auger case SMMU_CMD_CFGI_CD: 815dadd1a08SEric Auger case SMMU_CMD_CFGI_CD_ALL: 816*32cfd7f3SEric Auger { 817*32cfd7f3SEric Auger uint32_t sid = CMD_SID(&cmd); 818*32cfd7f3SEric Auger IOMMUMemoryRegion *mr = smmu_iommu_mr(bs, sid); 819*32cfd7f3SEric Auger SMMUDevice *sdev; 820*32cfd7f3SEric Auger 821*32cfd7f3SEric Auger if (CMD_SSEC(&cmd)) { 822*32cfd7f3SEric Auger cmd_error = SMMU_CERROR_ILL; 823*32cfd7f3SEric Auger break; 824*32cfd7f3SEric Auger } 825*32cfd7f3SEric Auger 826*32cfd7f3SEric Auger if (!mr) { 827*32cfd7f3SEric Auger break; 828*32cfd7f3SEric Auger } 829*32cfd7f3SEric Auger 830*32cfd7f3SEric Auger trace_smmuv3_cmdq_cfgi_cd(sid); 831*32cfd7f3SEric Auger sdev = container_of(mr, SMMUDevice, iommu); 832*32cfd7f3SEric Auger smmuv3_flush_config(sdev); 833*32cfd7f3SEric Auger break; 834*32cfd7f3SEric Auger } 835dadd1a08SEric Auger case SMMU_CMD_TLBI_NH_ALL: 836dadd1a08SEric Auger case SMMU_CMD_TLBI_NH_ASID: 837dadd1a08SEric Auger case SMMU_CMD_TLBI_NH_VA: 838dadd1a08SEric Auger case SMMU_CMD_TLBI_NH_VAA: 839dadd1a08SEric Auger case SMMU_CMD_TLBI_EL3_ALL: 840dadd1a08SEric Auger case SMMU_CMD_TLBI_EL3_VA: 841dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_ALL: 842dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_ASID: 843dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_VA: 844dadd1a08SEric Auger case SMMU_CMD_TLBI_EL2_VAA: 845dadd1a08SEric Auger case SMMU_CMD_TLBI_S12_VMALL: 846dadd1a08SEric Auger case SMMU_CMD_TLBI_S2_IPA: 847dadd1a08SEric Auger case SMMU_CMD_TLBI_NSNH_ALL: 848dadd1a08SEric Auger case SMMU_CMD_ATC_INV: 849dadd1a08SEric Auger case SMMU_CMD_PRI_RESP: 850dadd1a08SEric Auger case SMMU_CMD_RESUME: 851dadd1a08SEric Auger case SMMU_CMD_STALL_TERM: 852dadd1a08SEric Auger trace_smmuv3_unhandled_cmd(type); 853dadd1a08SEric Auger break; 854dadd1a08SEric Auger default: 855dadd1a08SEric Auger cmd_error = SMMU_CERROR_ILL; 856dadd1a08SEric Auger qemu_log_mask(LOG_GUEST_ERROR, 857dadd1a08SEric Auger "Illegal command type: %d\n", CMD_TYPE(&cmd)); 858dadd1a08SEric Auger break; 859dadd1a08SEric Auger } 860*32cfd7f3SEric Auger qemu_mutex_unlock(&s->mutex); 861dadd1a08SEric Auger if (cmd_error) { 862dadd1a08SEric Auger break; 863dadd1a08SEric Auger } 864dadd1a08SEric Auger /* 865dadd1a08SEric Auger * We only increment the cons index after the completion of 866dadd1a08SEric Auger * the command. We do that because the SYNC returns immediately 867dadd1a08SEric Auger * and does not check the completion of previous commands 868dadd1a08SEric Auger */ 869dadd1a08SEric Auger queue_cons_incr(q); 870dadd1a08SEric Auger } 871dadd1a08SEric Auger 872dadd1a08SEric Auger if (cmd_error) { 873dadd1a08SEric Auger trace_smmuv3_cmdq_consume_error(smmu_cmd_string(type), cmd_error); 874dadd1a08SEric Auger smmu_write_cmdq_err(s, cmd_error); 875dadd1a08SEric Auger smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_CMDQ_ERR_MASK); 876dadd1a08SEric Auger } 877dadd1a08SEric Auger 878dadd1a08SEric Auger trace_smmuv3_cmdq_consume_out(Q_PROD(q), Q_CONS(q), 879dadd1a08SEric Auger Q_PROD_WRAP(q), Q_CONS_WRAP(q)); 880dadd1a08SEric Auger 881dadd1a08SEric Auger return 0; 882dadd1a08SEric Auger } 883dadd1a08SEric Auger 884fae4be38SEric Auger static MemTxResult smmu_writell(SMMUv3State *s, hwaddr offset, 885fae4be38SEric Auger uint64_t data, MemTxAttrs attrs) 886fae4be38SEric Auger { 887fae4be38SEric Auger switch (offset) { 888fae4be38SEric Auger case A_GERROR_IRQ_CFG0: 889fae4be38SEric Auger s->gerror_irq_cfg0 = data; 890fae4be38SEric Auger return MEMTX_OK; 891fae4be38SEric Auger case A_STRTAB_BASE: 892fae4be38SEric Auger s->strtab_base = data; 893fae4be38SEric Auger return MEMTX_OK; 894fae4be38SEric Auger case A_CMDQ_BASE: 895fae4be38SEric Auger s->cmdq.base = data; 896fae4be38SEric Auger s->cmdq.log2size = extract64(s->cmdq.base, 0, 5); 897fae4be38SEric Auger if (s->cmdq.log2size > SMMU_CMDQS) { 898fae4be38SEric Auger s->cmdq.log2size = SMMU_CMDQS; 899fae4be38SEric Auger } 900fae4be38SEric Auger return MEMTX_OK; 901fae4be38SEric Auger case A_EVENTQ_BASE: 902fae4be38SEric Auger s->eventq.base = data; 903fae4be38SEric Auger s->eventq.log2size = extract64(s->eventq.base, 0, 5); 904fae4be38SEric Auger if (s->eventq.log2size > SMMU_EVENTQS) { 905fae4be38SEric Auger s->eventq.log2size = SMMU_EVENTQS; 906fae4be38SEric Auger } 907fae4be38SEric Auger return MEMTX_OK; 908fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0: 909fae4be38SEric Auger s->eventq_irq_cfg0 = data; 910fae4be38SEric Auger return MEMTX_OK; 911fae4be38SEric Auger default: 912fae4be38SEric Auger qemu_log_mask(LOG_UNIMP, 913fae4be38SEric Auger "%s Unexpected 64-bit access to 0x%"PRIx64" (WI)\n", 914fae4be38SEric Auger __func__, offset); 915fae4be38SEric Auger return MEMTX_OK; 916fae4be38SEric Auger } 917fae4be38SEric Auger } 918fae4be38SEric Auger 919fae4be38SEric Auger static MemTxResult smmu_writel(SMMUv3State *s, hwaddr offset, 920fae4be38SEric Auger uint64_t data, MemTxAttrs attrs) 921fae4be38SEric Auger { 922fae4be38SEric Auger switch (offset) { 923fae4be38SEric Auger case A_CR0: 924fae4be38SEric Auger s->cr[0] = data; 925fae4be38SEric Auger s->cr0ack = data & ~SMMU_CR0_RESERVED; 926fae4be38SEric Auger /* in case the command queue has been enabled */ 927fae4be38SEric Auger smmuv3_cmdq_consume(s); 928fae4be38SEric Auger return MEMTX_OK; 929fae4be38SEric Auger case A_CR1: 930fae4be38SEric Auger s->cr[1] = data; 931fae4be38SEric Auger return MEMTX_OK; 932fae4be38SEric Auger case A_CR2: 933fae4be38SEric Auger s->cr[2] = data; 934fae4be38SEric Auger return MEMTX_OK; 935fae4be38SEric Auger case A_IRQ_CTRL: 936fae4be38SEric Auger s->irq_ctrl = data; 937fae4be38SEric Auger return MEMTX_OK; 938fae4be38SEric Auger case A_GERRORN: 939fae4be38SEric Auger smmuv3_write_gerrorn(s, data); 940fae4be38SEric Auger /* 941fae4be38SEric Auger * By acknowledging the CMDQ_ERR, SW may notify cmds can 942fae4be38SEric Auger * be processed again 943fae4be38SEric Auger */ 944fae4be38SEric Auger smmuv3_cmdq_consume(s); 945fae4be38SEric Auger return MEMTX_OK; 946fae4be38SEric Auger case A_GERROR_IRQ_CFG0: /* 64b */ 947fae4be38SEric Auger s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 0, 32, data); 948fae4be38SEric Auger return MEMTX_OK; 949fae4be38SEric Auger case A_GERROR_IRQ_CFG0 + 4: 950fae4be38SEric Auger s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 32, 32, data); 951fae4be38SEric Auger return MEMTX_OK; 952fae4be38SEric Auger case A_GERROR_IRQ_CFG1: 953fae4be38SEric Auger s->gerror_irq_cfg1 = data; 954fae4be38SEric Auger return MEMTX_OK; 955fae4be38SEric Auger case A_GERROR_IRQ_CFG2: 956fae4be38SEric Auger s->gerror_irq_cfg2 = data; 957fae4be38SEric Auger return MEMTX_OK; 958fae4be38SEric Auger case A_STRTAB_BASE: /* 64b */ 959fae4be38SEric Auger s->strtab_base = deposit64(s->strtab_base, 0, 32, data); 960fae4be38SEric Auger return MEMTX_OK; 961fae4be38SEric Auger case A_STRTAB_BASE + 4: 962fae4be38SEric Auger s->strtab_base = deposit64(s->strtab_base, 32, 32, data); 963fae4be38SEric Auger return MEMTX_OK; 964fae4be38SEric Auger case A_STRTAB_BASE_CFG: 965fae4be38SEric Auger s->strtab_base_cfg = data; 966fae4be38SEric Auger if (FIELD_EX32(data, STRTAB_BASE_CFG, FMT) == 1) { 967fae4be38SEric Auger s->sid_split = FIELD_EX32(data, STRTAB_BASE_CFG, SPLIT); 968fae4be38SEric Auger s->features |= SMMU_FEATURE_2LVL_STE; 969fae4be38SEric Auger } 970fae4be38SEric Auger return MEMTX_OK; 971fae4be38SEric Auger case A_CMDQ_BASE: /* 64b */ 972fae4be38SEric Auger s->cmdq.base = deposit64(s->cmdq.base, 0, 32, data); 973fae4be38SEric Auger s->cmdq.log2size = extract64(s->cmdq.base, 0, 5); 974fae4be38SEric Auger if (s->cmdq.log2size > SMMU_CMDQS) { 975fae4be38SEric Auger s->cmdq.log2size = SMMU_CMDQS; 976fae4be38SEric Auger } 977fae4be38SEric Auger return MEMTX_OK; 978fae4be38SEric Auger case A_CMDQ_BASE + 4: /* 64b */ 979fae4be38SEric Auger s->cmdq.base = deposit64(s->cmdq.base, 32, 32, data); 980fae4be38SEric Auger return MEMTX_OK; 981fae4be38SEric Auger case A_CMDQ_PROD: 982fae4be38SEric Auger s->cmdq.prod = data; 983fae4be38SEric Auger smmuv3_cmdq_consume(s); 984fae4be38SEric Auger return MEMTX_OK; 985fae4be38SEric Auger case A_CMDQ_CONS: 986fae4be38SEric Auger s->cmdq.cons = data; 987fae4be38SEric Auger return MEMTX_OK; 988fae4be38SEric Auger case A_EVENTQ_BASE: /* 64b */ 989fae4be38SEric Auger s->eventq.base = deposit64(s->eventq.base, 0, 32, data); 990fae4be38SEric Auger s->eventq.log2size = extract64(s->eventq.base, 0, 5); 991fae4be38SEric Auger if (s->eventq.log2size > SMMU_EVENTQS) { 992fae4be38SEric Auger s->eventq.log2size = SMMU_EVENTQS; 993fae4be38SEric Auger } 994fae4be38SEric Auger return MEMTX_OK; 995fae4be38SEric Auger case A_EVENTQ_BASE + 4: 996fae4be38SEric Auger s->eventq.base = deposit64(s->eventq.base, 32, 32, data); 997fae4be38SEric Auger return MEMTX_OK; 998fae4be38SEric Auger case A_EVENTQ_PROD: 999fae4be38SEric Auger s->eventq.prod = data; 1000fae4be38SEric Auger return MEMTX_OK; 1001fae4be38SEric Auger case A_EVENTQ_CONS: 1002fae4be38SEric Auger s->eventq.cons = data; 1003fae4be38SEric Auger return MEMTX_OK; 1004fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0: /* 64b */ 1005fae4be38SEric Auger s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 0, 32, data); 1006fae4be38SEric Auger return MEMTX_OK; 1007fae4be38SEric Auger case A_EVENTQ_IRQ_CFG0 + 4: 1008fae4be38SEric Auger s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 32, 32, data); 1009fae4be38SEric Auger return MEMTX_OK; 1010fae4be38SEric Auger case A_EVENTQ_IRQ_CFG1: 1011fae4be38SEric Auger s->eventq_irq_cfg1 = data; 1012fae4be38SEric Auger return MEMTX_OK; 1013fae4be38SEric Auger case A_EVENTQ_IRQ_CFG2: 1014fae4be38SEric Auger s->eventq_irq_cfg2 = data; 1015fae4be38SEric Auger return MEMTX_OK; 1016fae4be38SEric Auger default: 1017fae4be38SEric Auger qemu_log_mask(LOG_UNIMP, 1018fae4be38SEric Auger "%s Unexpected 32-bit access to 0x%"PRIx64" (WI)\n", 1019fae4be38SEric Auger __func__, offset); 1020fae4be38SEric Auger return MEMTX_OK; 1021fae4be38SEric Auger } 1022fae4be38SEric Auger } 1023fae4be38SEric Auger 102410a83cb9SPrem Mallappa static MemTxResult smmu_write_mmio(void *opaque, hwaddr offset, uint64_t data, 102510a83cb9SPrem Mallappa unsigned size, MemTxAttrs attrs) 102610a83cb9SPrem Mallappa { 1027fae4be38SEric Auger SMMUState *sys = opaque; 1028fae4be38SEric Auger SMMUv3State *s = ARM_SMMUV3(sys); 1029fae4be38SEric Auger MemTxResult r; 1030fae4be38SEric Auger 1031fae4be38SEric Auger /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */ 1032fae4be38SEric Auger offset &= ~0x10000; 1033fae4be38SEric Auger 1034fae4be38SEric Auger switch (size) { 1035fae4be38SEric Auger case 8: 1036fae4be38SEric Auger r = smmu_writell(s, offset, data, attrs); 1037fae4be38SEric Auger break; 1038fae4be38SEric Auger case 4: 1039fae4be38SEric Auger r = smmu_writel(s, offset, data, attrs); 1040fae4be38SEric Auger break; 1041fae4be38SEric Auger default: 1042fae4be38SEric Auger r = MEMTX_ERROR; 1043fae4be38SEric Auger break; 1044fae4be38SEric Auger } 1045fae4be38SEric Auger 1046fae4be38SEric Auger trace_smmuv3_write_mmio(offset, data, size, r); 1047fae4be38SEric Auger return r; 104810a83cb9SPrem Mallappa } 104910a83cb9SPrem Mallappa 105010a83cb9SPrem Mallappa static MemTxResult smmu_readll(SMMUv3State *s, hwaddr offset, 105110a83cb9SPrem Mallappa uint64_t *data, MemTxAttrs attrs) 105210a83cb9SPrem Mallappa { 105310a83cb9SPrem Mallappa switch (offset) { 105410a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0: 105510a83cb9SPrem Mallappa *data = s->gerror_irq_cfg0; 105610a83cb9SPrem Mallappa return MEMTX_OK; 105710a83cb9SPrem Mallappa case A_STRTAB_BASE: 105810a83cb9SPrem Mallappa *data = s->strtab_base; 105910a83cb9SPrem Mallappa return MEMTX_OK; 106010a83cb9SPrem Mallappa case A_CMDQ_BASE: 106110a83cb9SPrem Mallappa *data = s->cmdq.base; 106210a83cb9SPrem Mallappa return MEMTX_OK; 106310a83cb9SPrem Mallappa case A_EVENTQ_BASE: 106410a83cb9SPrem Mallappa *data = s->eventq.base; 106510a83cb9SPrem Mallappa return MEMTX_OK; 106610a83cb9SPrem Mallappa default: 106710a83cb9SPrem Mallappa *data = 0; 106810a83cb9SPrem Mallappa qemu_log_mask(LOG_UNIMP, 106910a83cb9SPrem Mallappa "%s Unexpected 64-bit access to 0x%"PRIx64" (RAZ)\n", 107010a83cb9SPrem Mallappa __func__, offset); 107110a83cb9SPrem Mallappa return MEMTX_OK; 107210a83cb9SPrem Mallappa } 107310a83cb9SPrem Mallappa } 107410a83cb9SPrem Mallappa 107510a83cb9SPrem Mallappa static MemTxResult smmu_readl(SMMUv3State *s, hwaddr offset, 107610a83cb9SPrem Mallappa uint64_t *data, MemTxAttrs attrs) 107710a83cb9SPrem Mallappa { 107810a83cb9SPrem Mallappa switch (offset) { 107910a83cb9SPrem Mallappa case A_IDREGS ... A_IDREGS + 0x1f: 108010a83cb9SPrem Mallappa *data = smmuv3_idreg(offset - A_IDREGS); 108110a83cb9SPrem Mallappa return MEMTX_OK; 108210a83cb9SPrem Mallappa case A_IDR0 ... A_IDR5: 108310a83cb9SPrem Mallappa *data = s->idr[(offset - A_IDR0) / 4]; 108410a83cb9SPrem Mallappa return MEMTX_OK; 108510a83cb9SPrem Mallappa case A_IIDR: 108610a83cb9SPrem Mallappa *data = s->iidr; 108710a83cb9SPrem Mallappa return MEMTX_OK; 108810a83cb9SPrem Mallappa case A_CR0: 108910a83cb9SPrem Mallappa *data = s->cr[0]; 109010a83cb9SPrem Mallappa return MEMTX_OK; 109110a83cb9SPrem Mallappa case A_CR0ACK: 109210a83cb9SPrem Mallappa *data = s->cr0ack; 109310a83cb9SPrem Mallappa return MEMTX_OK; 109410a83cb9SPrem Mallappa case A_CR1: 109510a83cb9SPrem Mallappa *data = s->cr[1]; 109610a83cb9SPrem Mallappa return MEMTX_OK; 109710a83cb9SPrem Mallappa case A_CR2: 109810a83cb9SPrem Mallappa *data = s->cr[2]; 109910a83cb9SPrem Mallappa return MEMTX_OK; 110010a83cb9SPrem Mallappa case A_STATUSR: 110110a83cb9SPrem Mallappa *data = s->statusr; 110210a83cb9SPrem Mallappa return MEMTX_OK; 110310a83cb9SPrem Mallappa case A_IRQ_CTRL: 110410a83cb9SPrem Mallappa case A_IRQ_CTRL_ACK: 110510a83cb9SPrem Mallappa *data = s->irq_ctrl; 110610a83cb9SPrem Mallappa return MEMTX_OK; 110710a83cb9SPrem Mallappa case A_GERROR: 110810a83cb9SPrem Mallappa *data = s->gerror; 110910a83cb9SPrem Mallappa return MEMTX_OK; 111010a83cb9SPrem Mallappa case A_GERRORN: 111110a83cb9SPrem Mallappa *data = s->gerrorn; 111210a83cb9SPrem Mallappa return MEMTX_OK; 111310a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0: /* 64b */ 111410a83cb9SPrem Mallappa *data = extract64(s->gerror_irq_cfg0, 0, 32); 111510a83cb9SPrem Mallappa return MEMTX_OK; 111610a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG0 + 4: 111710a83cb9SPrem Mallappa *data = extract64(s->gerror_irq_cfg0, 32, 32); 111810a83cb9SPrem Mallappa return MEMTX_OK; 111910a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG1: 112010a83cb9SPrem Mallappa *data = s->gerror_irq_cfg1; 112110a83cb9SPrem Mallappa return MEMTX_OK; 112210a83cb9SPrem Mallappa case A_GERROR_IRQ_CFG2: 112310a83cb9SPrem Mallappa *data = s->gerror_irq_cfg2; 112410a83cb9SPrem Mallappa return MEMTX_OK; 112510a83cb9SPrem Mallappa case A_STRTAB_BASE: /* 64b */ 112610a83cb9SPrem Mallappa *data = extract64(s->strtab_base, 0, 32); 112710a83cb9SPrem Mallappa return MEMTX_OK; 112810a83cb9SPrem Mallappa case A_STRTAB_BASE + 4: /* 64b */ 112910a83cb9SPrem Mallappa *data = extract64(s->strtab_base, 32, 32); 113010a83cb9SPrem Mallappa return MEMTX_OK; 113110a83cb9SPrem Mallappa case A_STRTAB_BASE_CFG: 113210a83cb9SPrem Mallappa *data = s->strtab_base_cfg; 113310a83cb9SPrem Mallappa return MEMTX_OK; 113410a83cb9SPrem Mallappa case A_CMDQ_BASE: /* 64b */ 113510a83cb9SPrem Mallappa *data = extract64(s->cmdq.base, 0, 32); 113610a83cb9SPrem Mallappa return MEMTX_OK; 113710a83cb9SPrem Mallappa case A_CMDQ_BASE + 4: 113810a83cb9SPrem Mallappa *data = extract64(s->cmdq.base, 32, 32); 113910a83cb9SPrem Mallappa return MEMTX_OK; 114010a83cb9SPrem Mallappa case A_CMDQ_PROD: 114110a83cb9SPrem Mallappa *data = s->cmdq.prod; 114210a83cb9SPrem Mallappa return MEMTX_OK; 114310a83cb9SPrem Mallappa case A_CMDQ_CONS: 114410a83cb9SPrem Mallappa *data = s->cmdq.cons; 114510a83cb9SPrem Mallappa return MEMTX_OK; 114610a83cb9SPrem Mallappa case A_EVENTQ_BASE: /* 64b */ 114710a83cb9SPrem Mallappa *data = extract64(s->eventq.base, 0, 32); 114810a83cb9SPrem Mallappa return MEMTX_OK; 114910a83cb9SPrem Mallappa case A_EVENTQ_BASE + 4: /* 64b */ 115010a83cb9SPrem Mallappa *data = extract64(s->eventq.base, 32, 32); 115110a83cb9SPrem Mallappa return MEMTX_OK; 115210a83cb9SPrem Mallappa case A_EVENTQ_PROD: 115310a83cb9SPrem Mallappa *data = s->eventq.prod; 115410a83cb9SPrem Mallappa return MEMTX_OK; 115510a83cb9SPrem Mallappa case A_EVENTQ_CONS: 115610a83cb9SPrem Mallappa *data = s->eventq.cons; 115710a83cb9SPrem Mallappa return MEMTX_OK; 115810a83cb9SPrem Mallappa default: 115910a83cb9SPrem Mallappa *data = 0; 116010a83cb9SPrem Mallappa qemu_log_mask(LOG_UNIMP, 116110a83cb9SPrem Mallappa "%s unhandled 32-bit access at 0x%"PRIx64" (RAZ)\n", 116210a83cb9SPrem Mallappa __func__, offset); 116310a83cb9SPrem Mallappa return MEMTX_OK; 116410a83cb9SPrem Mallappa } 116510a83cb9SPrem Mallappa } 116610a83cb9SPrem Mallappa 116710a83cb9SPrem Mallappa static MemTxResult smmu_read_mmio(void *opaque, hwaddr offset, uint64_t *data, 116810a83cb9SPrem Mallappa unsigned size, MemTxAttrs attrs) 116910a83cb9SPrem Mallappa { 117010a83cb9SPrem Mallappa SMMUState *sys = opaque; 117110a83cb9SPrem Mallappa SMMUv3State *s = ARM_SMMUV3(sys); 117210a83cb9SPrem Mallappa MemTxResult r; 117310a83cb9SPrem Mallappa 117410a83cb9SPrem Mallappa /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */ 117510a83cb9SPrem Mallappa offset &= ~0x10000; 117610a83cb9SPrem Mallappa 117710a83cb9SPrem Mallappa switch (size) { 117810a83cb9SPrem Mallappa case 8: 117910a83cb9SPrem Mallappa r = smmu_readll(s, offset, data, attrs); 118010a83cb9SPrem Mallappa break; 118110a83cb9SPrem Mallappa case 4: 118210a83cb9SPrem Mallappa r = smmu_readl(s, offset, data, attrs); 118310a83cb9SPrem Mallappa break; 118410a83cb9SPrem Mallappa default: 118510a83cb9SPrem Mallappa r = MEMTX_ERROR; 118610a83cb9SPrem Mallappa break; 118710a83cb9SPrem Mallappa } 118810a83cb9SPrem Mallappa 118910a83cb9SPrem Mallappa trace_smmuv3_read_mmio(offset, *data, size, r); 119010a83cb9SPrem Mallappa return r; 119110a83cb9SPrem Mallappa } 119210a83cb9SPrem Mallappa 119310a83cb9SPrem Mallappa static const MemoryRegionOps smmu_mem_ops = { 119410a83cb9SPrem Mallappa .read_with_attrs = smmu_read_mmio, 119510a83cb9SPrem Mallappa .write_with_attrs = smmu_write_mmio, 119610a83cb9SPrem Mallappa .endianness = DEVICE_LITTLE_ENDIAN, 119710a83cb9SPrem Mallappa .valid = { 119810a83cb9SPrem Mallappa .min_access_size = 4, 119910a83cb9SPrem Mallappa .max_access_size = 8, 120010a83cb9SPrem Mallappa }, 120110a83cb9SPrem Mallappa .impl = { 120210a83cb9SPrem Mallappa .min_access_size = 4, 120310a83cb9SPrem Mallappa .max_access_size = 8, 120410a83cb9SPrem Mallappa }, 120510a83cb9SPrem Mallappa }; 120610a83cb9SPrem Mallappa 120710a83cb9SPrem Mallappa static void smmu_init_irq(SMMUv3State *s, SysBusDevice *dev) 120810a83cb9SPrem Mallappa { 120910a83cb9SPrem Mallappa int i; 121010a83cb9SPrem Mallappa 121110a83cb9SPrem Mallappa for (i = 0; i < ARRAY_SIZE(s->irq); i++) { 121210a83cb9SPrem Mallappa sysbus_init_irq(dev, &s->irq[i]); 121310a83cb9SPrem Mallappa } 121410a83cb9SPrem Mallappa } 121510a83cb9SPrem Mallappa 121610a83cb9SPrem Mallappa static void smmu_reset(DeviceState *dev) 121710a83cb9SPrem Mallappa { 121810a83cb9SPrem Mallappa SMMUv3State *s = ARM_SMMUV3(dev); 121910a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s); 122010a83cb9SPrem Mallappa 122110a83cb9SPrem Mallappa c->parent_reset(dev); 122210a83cb9SPrem Mallappa 122310a83cb9SPrem Mallappa smmuv3_init_regs(s); 122410a83cb9SPrem Mallappa } 122510a83cb9SPrem Mallappa 122610a83cb9SPrem Mallappa static void smmu_realize(DeviceState *d, Error **errp) 122710a83cb9SPrem Mallappa { 122810a83cb9SPrem Mallappa SMMUState *sys = ARM_SMMU(d); 122910a83cb9SPrem Mallappa SMMUv3State *s = ARM_SMMUV3(sys); 123010a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s); 123110a83cb9SPrem Mallappa SysBusDevice *dev = SYS_BUS_DEVICE(d); 123210a83cb9SPrem Mallappa Error *local_err = NULL; 123310a83cb9SPrem Mallappa 123410a83cb9SPrem Mallappa c->parent_realize(d, &local_err); 123510a83cb9SPrem Mallappa if (local_err) { 123610a83cb9SPrem Mallappa error_propagate(errp, local_err); 123710a83cb9SPrem Mallappa return; 123810a83cb9SPrem Mallappa } 123910a83cb9SPrem Mallappa 1240*32cfd7f3SEric Auger qemu_mutex_init(&s->mutex); 1241*32cfd7f3SEric Auger 124210a83cb9SPrem Mallappa memory_region_init_io(&sys->iomem, OBJECT(s), 124310a83cb9SPrem Mallappa &smmu_mem_ops, sys, TYPE_ARM_SMMUV3, 0x20000); 124410a83cb9SPrem Mallappa 124510a83cb9SPrem Mallappa sys->mrtypename = TYPE_SMMUV3_IOMMU_MEMORY_REGION; 124610a83cb9SPrem Mallappa 124710a83cb9SPrem Mallappa sysbus_init_mmio(dev, &sys->iomem); 124810a83cb9SPrem Mallappa 124910a83cb9SPrem Mallappa smmu_init_irq(s, dev); 125010a83cb9SPrem Mallappa } 125110a83cb9SPrem Mallappa 125210a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3_queue = { 125310a83cb9SPrem Mallappa .name = "smmuv3_queue", 125410a83cb9SPrem Mallappa .version_id = 1, 125510a83cb9SPrem Mallappa .minimum_version_id = 1, 125610a83cb9SPrem Mallappa .fields = (VMStateField[]) { 125710a83cb9SPrem Mallappa VMSTATE_UINT64(base, SMMUQueue), 125810a83cb9SPrem Mallappa VMSTATE_UINT32(prod, SMMUQueue), 125910a83cb9SPrem Mallappa VMSTATE_UINT32(cons, SMMUQueue), 126010a83cb9SPrem Mallappa VMSTATE_UINT8(log2size, SMMUQueue), 126110a83cb9SPrem Mallappa }, 126210a83cb9SPrem Mallappa }; 126310a83cb9SPrem Mallappa 126410a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3 = { 126510a83cb9SPrem Mallappa .name = "smmuv3", 126610a83cb9SPrem Mallappa .version_id = 1, 126710a83cb9SPrem Mallappa .minimum_version_id = 1, 126810a83cb9SPrem Mallappa .fields = (VMStateField[]) { 126910a83cb9SPrem Mallappa VMSTATE_UINT32(features, SMMUv3State), 127010a83cb9SPrem Mallappa VMSTATE_UINT8(sid_size, SMMUv3State), 127110a83cb9SPrem Mallappa VMSTATE_UINT8(sid_split, SMMUv3State), 127210a83cb9SPrem Mallappa 127310a83cb9SPrem Mallappa VMSTATE_UINT32_ARRAY(cr, SMMUv3State, 3), 127410a83cb9SPrem Mallappa VMSTATE_UINT32(cr0ack, SMMUv3State), 127510a83cb9SPrem Mallappa VMSTATE_UINT32(statusr, SMMUv3State), 127610a83cb9SPrem Mallappa VMSTATE_UINT32(irq_ctrl, SMMUv3State), 127710a83cb9SPrem Mallappa VMSTATE_UINT32(gerror, SMMUv3State), 127810a83cb9SPrem Mallappa VMSTATE_UINT32(gerrorn, SMMUv3State), 127910a83cb9SPrem Mallappa VMSTATE_UINT64(gerror_irq_cfg0, SMMUv3State), 128010a83cb9SPrem Mallappa VMSTATE_UINT32(gerror_irq_cfg1, SMMUv3State), 128110a83cb9SPrem Mallappa VMSTATE_UINT32(gerror_irq_cfg2, SMMUv3State), 128210a83cb9SPrem Mallappa VMSTATE_UINT64(strtab_base, SMMUv3State), 128310a83cb9SPrem Mallappa VMSTATE_UINT32(strtab_base_cfg, SMMUv3State), 128410a83cb9SPrem Mallappa VMSTATE_UINT64(eventq_irq_cfg0, SMMUv3State), 128510a83cb9SPrem Mallappa VMSTATE_UINT32(eventq_irq_cfg1, SMMUv3State), 128610a83cb9SPrem Mallappa VMSTATE_UINT32(eventq_irq_cfg2, SMMUv3State), 128710a83cb9SPrem Mallappa 128810a83cb9SPrem Mallappa VMSTATE_STRUCT(cmdq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue), 128910a83cb9SPrem Mallappa VMSTATE_STRUCT(eventq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue), 129010a83cb9SPrem Mallappa 129110a83cb9SPrem Mallappa VMSTATE_END_OF_LIST(), 129210a83cb9SPrem Mallappa }, 129310a83cb9SPrem Mallappa }; 129410a83cb9SPrem Mallappa 129510a83cb9SPrem Mallappa static void smmuv3_instance_init(Object *obj) 129610a83cb9SPrem Mallappa { 129710a83cb9SPrem Mallappa /* Nothing much to do here as of now */ 129810a83cb9SPrem Mallappa } 129910a83cb9SPrem Mallappa 130010a83cb9SPrem Mallappa static void smmuv3_class_init(ObjectClass *klass, void *data) 130110a83cb9SPrem Mallappa { 130210a83cb9SPrem Mallappa DeviceClass *dc = DEVICE_CLASS(klass); 130310a83cb9SPrem Mallappa SMMUv3Class *c = ARM_SMMUV3_CLASS(klass); 130410a83cb9SPrem Mallappa 130510a83cb9SPrem Mallappa dc->vmsd = &vmstate_smmuv3; 130610a83cb9SPrem Mallappa device_class_set_parent_reset(dc, smmu_reset, &c->parent_reset); 130710a83cb9SPrem Mallappa c->parent_realize = dc->realize; 130810a83cb9SPrem Mallappa dc->realize = smmu_realize; 130910a83cb9SPrem Mallappa } 131010a83cb9SPrem Mallappa 13110d1ac82eSEric Auger static void smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu, 13120d1ac82eSEric Auger IOMMUNotifierFlag old, 13130d1ac82eSEric Auger IOMMUNotifierFlag new) 13140d1ac82eSEric Auger { 13150d1ac82eSEric Auger if (old == IOMMU_NOTIFIER_NONE) { 13160d1ac82eSEric Auger warn_report("SMMUV3 does not support vhost/vfio integration yet: " 13170d1ac82eSEric Auger "devices of those types will not function properly"); 13180d1ac82eSEric Auger } 13190d1ac82eSEric Auger } 13200d1ac82eSEric Auger 132110a83cb9SPrem Mallappa static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass, 132210a83cb9SPrem Mallappa void *data) 132310a83cb9SPrem Mallappa { 13249bde7f06SEric Auger IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass); 13259bde7f06SEric Auger 13269bde7f06SEric Auger imrc->translate = smmuv3_translate; 13270d1ac82eSEric Auger imrc->notify_flag_changed = smmuv3_notify_flag_changed; 132810a83cb9SPrem Mallappa } 132910a83cb9SPrem Mallappa 133010a83cb9SPrem Mallappa static const TypeInfo smmuv3_type_info = { 133110a83cb9SPrem Mallappa .name = TYPE_ARM_SMMUV3, 133210a83cb9SPrem Mallappa .parent = TYPE_ARM_SMMU, 133310a83cb9SPrem Mallappa .instance_size = sizeof(SMMUv3State), 133410a83cb9SPrem Mallappa .instance_init = smmuv3_instance_init, 133510a83cb9SPrem Mallappa .class_size = sizeof(SMMUv3Class), 133610a83cb9SPrem Mallappa .class_init = smmuv3_class_init, 133710a83cb9SPrem Mallappa }; 133810a83cb9SPrem Mallappa 133910a83cb9SPrem Mallappa static const TypeInfo smmuv3_iommu_memory_region_info = { 134010a83cb9SPrem Mallappa .parent = TYPE_IOMMU_MEMORY_REGION, 134110a83cb9SPrem Mallappa .name = TYPE_SMMUV3_IOMMU_MEMORY_REGION, 134210a83cb9SPrem Mallappa .class_init = smmuv3_iommu_memory_region_class_init, 134310a83cb9SPrem Mallappa }; 134410a83cb9SPrem Mallappa 134510a83cb9SPrem Mallappa static void smmuv3_register_types(void) 134610a83cb9SPrem Mallappa { 134710a83cb9SPrem Mallappa type_register(&smmuv3_type_info); 134810a83cb9SPrem Mallappa type_register(&smmuv3_iommu_memory_region_info); 134910a83cb9SPrem Mallappa } 135010a83cb9SPrem Mallappa 135110a83cb9SPrem Mallappa type_init(smmuv3_register_types) 135210a83cb9SPrem Mallappa 1353