xref: /qemu/hw/arm/smmuv3.c (revision 64552b6be4758d3a774f7787b294543ccebd5358)
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"
21*64552b6bSMarkus Armbruster #include "hw/irq.h"
2210a83cb9SPrem Mallappa #include "sysemu/sysemu.h"
2310a83cb9SPrem Mallappa #include "hw/sysbus.h"
2410a83cb9SPrem Mallappa #include "hw/qdev-core.h"
2510a83cb9SPrem Mallappa #include "hw/pci/pci.h"
2610a83cb9SPrem Mallappa #include "exec/address-spaces.h"
279122bea9SJia He #include "cpu.h"
2810a83cb9SPrem Mallappa #include "trace.h"
2910a83cb9SPrem Mallappa #include "qemu/log.h"
3010a83cb9SPrem Mallappa #include "qemu/error-report.h"
3110a83cb9SPrem Mallappa #include "qapi/error.h"
3210a83cb9SPrem Mallappa 
3310a83cb9SPrem Mallappa #include "hw/arm/smmuv3.h"
3410a83cb9SPrem Mallappa #include "smmuv3-internal.h"
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 
105dadd1a08SEric Auger     return dma_memory_read(&address_space_memory, addr, data, q->entry_size);
106dadd1a08SEric Auger }
107dadd1a08SEric Auger 
108dadd1a08SEric Auger static MemTxResult queue_write(SMMUQueue *q, void *data)
109dadd1a08SEric Auger {
110dadd1a08SEric Auger     dma_addr_t addr = Q_PROD_ENTRY(q);
111dadd1a08SEric Auger     MemTxResult ret;
112dadd1a08SEric Auger 
113dadd1a08SEric Auger     ret = dma_memory_write(&address_space_memory, addr, data, q->entry_size);
114dadd1a08SEric Auger     if (ret != MEMTX_OK) {
115dadd1a08SEric Auger         return ret;
116dadd1a08SEric Auger     }
117dadd1a08SEric Auger 
118dadd1a08SEric Auger     queue_prod_incr(q);
119dadd1a08SEric Auger     return MEMTX_OK;
120dadd1a08SEric Auger }
121dadd1a08SEric Auger 
122bb981004SEric Auger static MemTxResult smmuv3_write_eventq(SMMUv3State *s, Evt *evt)
123dadd1a08SEric Auger {
124dadd1a08SEric Auger     SMMUQueue *q = &s->eventq;
125bb981004SEric Auger     MemTxResult r;
126bb981004SEric Auger 
127bb981004SEric Auger     if (!smmuv3_eventq_enabled(s)) {
128bb981004SEric Auger         return MEMTX_ERROR;
129bb981004SEric Auger     }
130bb981004SEric Auger 
131bb981004SEric Auger     if (smmuv3_q_full(q)) {
132bb981004SEric Auger         return MEMTX_ERROR;
133bb981004SEric Auger     }
134bb981004SEric Auger 
135bb981004SEric Auger     r = queue_write(q, evt);
136bb981004SEric Auger     if (r != MEMTX_OK) {
137bb981004SEric Auger         return r;
138bb981004SEric Auger     }
139bb981004SEric Auger 
1409f4d2a13SEric Auger     if (!smmuv3_q_empty(q)) {
141bb981004SEric Auger         smmuv3_trigger_irq(s, SMMU_IRQ_EVTQ, 0);
142bb981004SEric Auger     }
143bb981004SEric Auger     return MEMTX_OK;
144bb981004SEric Auger }
145bb981004SEric Auger 
146bb981004SEric Auger void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info)
147bb981004SEric Auger {
14824af32e0SEric Auger     Evt evt = {};
149bb981004SEric Auger     MemTxResult r;
150dadd1a08SEric Auger 
151dadd1a08SEric Auger     if (!smmuv3_eventq_enabled(s)) {
152dadd1a08SEric Auger         return;
153dadd1a08SEric Auger     }
154dadd1a08SEric Auger 
155bb981004SEric Auger     EVT_SET_TYPE(&evt, info->type);
156bb981004SEric Auger     EVT_SET_SID(&evt, info->sid);
157bb981004SEric Auger 
158bb981004SEric Auger     switch (info->type) {
1599122bea9SJia He     case SMMU_EVT_NONE:
160dadd1a08SEric Auger         return;
161bb981004SEric Auger     case SMMU_EVT_F_UUT:
162bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_uut.ssid);
163bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.f_uut.ssv);
164bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_uut.addr);
165bb981004SEric Auger         EVT_SET_RNW(&evt,  info->u.f_uut.rnw);
166bb981004SEric Auger         EVT_SET_PNU(&evt,  info->u.f_uut.pnu);
167bb981004SEric Auger         EVT_SET_IND(&evt,  info->u.f_uut.ind);
168bb981004SEric Auger         break;
169bb981004SEric Auger     case SMMU_EVT_C_BAD_STREAMID:
170bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.c_bad_streamid.ssid);
171bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.c_bad_streamid.ssv);
172bb981004SEric Auger         break;
173bb981004SEric Auger     case SMMU_EVT_F_STE_FETCH:
174bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_ste_fetch.ssid);
175bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.f_ste_fetch.ssv);
176bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_ste_fetch.addr);
177bb981004SEric Auger         break;
178bb981004SEric Auger     case SMMU_EVT_C_BAD_STE:
179bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.c_bad_ste.ssid);
180bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.c_bad_ste.ssv);
181bb981004SEric Auger         break;
182bb981004SEric Auger     case SMMU_EVT_F_STREAM_DISABLED:
183bb981004SEric Auger         break;
184bb981004SEric Auger     case SMMU_EVT_F_TRANS_FORBIDDEN:
185bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_transl_forbidden.addr);
186bb981004SEric Auger         EVT_SET_RNW(&evt, info->u.f_transl_forbidden.rnw);
187bb981004SEric Auger         break;
188bb981004SEric Auger     case SMMU_EVT_C_BAD_SUBSTREAMID:
189bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.c_bad_substream.ssid);
190bb981004SEric Auger         break;
191bb981004SEric Auger     case SMMU_EVT_F_CD_FETCH:
192bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_cd_fetch.ssid);
193bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.f_cd_fetch.ssv);
194bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_cd_fetch.addr);
195bb981004SEric Auger         break;
196bb981004SEric Auger     case SMMU_EVT_C_BAD_CD:
197bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.c_bad_cd.ssid);
198bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.c_bad_cd.ssv);
199bb981004SEric Auger         break;
200bb981004SEric Auger     case SMMU_EVT_F_WALK_EABT:
201bb981004SEric Auger     case SMMU_EVT_F_TRANSLATION:
202bb981004SEric Auger     case SMMU_EVT_F_ADDR_SIZE:
203bb981004SEric Auger     case SMMU_EVT_F_ACCESS:
204bb981004SEric Auger     case SMMU_EVT_F_PERMISSION:
205bb981004SEric Auger         EVT_SET_STALL(&evt, info->u.f_walk_eabt.stall);
206bb981004SEric Auger         EVT_SET_STAG(&evt, info->u.f_walk_eabt.stag);
207bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_walk_eabt.ssid);
208bb981004SEric Auger         EVT_SET_SSV(&evt, info->u.f_walk_eabt.ssv);
209bb981004SEric Auger         EVT_SET_S2(&evt, info->u.f_walk_eabt.s2);
210bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_walk_eabt.addr);
211bb981004SEric Auger         EVT_SET_RNW(&evt, info->u.f_walk_eabt.rnw);
212bb981004SEric Auger         EVT_SET_PNU(&evt, info->u.f_walk_eabt.pnu);
213bb981004SEric Auger         EVT_SET_IND(&evt, info->u.f_walk_eabt.ind);
214bb981004SEric Auger         EVT_SET_CLASS(&evt, info->u.f_walk_eabt.class);
215bb981004SEric Auger         EVT_SET_ADDR2(&evt, info->u.f_walk_eabt.addr2);
216bb981004SEric Auger         break;
217bb981004SEric Auger     case SMMU_EVT_F_CFG_CONFLICT:
218bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_cfg_conflict.ssid);
219bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.f_cfg_conflict.ssv);
220bb981004SEric Auger         break;
221bb981004SEric Auger     /* rest is not implemented */
222bb981004SEric Auger     case SMMU_EVT_F_BAD_ATS_TREQ:
223bb981004SEric Auger     case SMMU_EVT_F_TLB_CONFLICT:
224bb981004SEric Auger     case SMMU_EVT_E_PAGE_REQ:
225bb981004SEric Auger     default:
226bb981004SEric Auger         g_assert_not_reached();
227dadd1a08SEric Auger     }
228dadd1a08SEric Auger 
229bb981004SEric Auger     trace_smmuv3_record_event(smmu_event_string(info->type), info->sid);
230bb981004SEric Auger     r = smmuv3_write_eventq(s, &evt);
231bb981004SEric Auger     if (r != MEMTX_OK) {
232bb981004SEric Auger         smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_EVENTQ_ABT_ERR_MASK);
233dadd1a08SEric Auger     }
234bb981004SEric Auger     info->recorded = true;
235dadd1a08SEric Auger }
236dadd1a08SEric Auger 
23710a83cb9SPrem Mallappa static void smmuv3_init_regs(SMMUv3State *s)
23810a83cb9SPrem Mallappa {
23910a83cb9SPrem Mallappa     /**
24010a83cb9SPrem Mallappa      * IDR0: stage1 only, AArch64 only, coherent access, 16b ASID,
24110a83cb9SPrem Mallappa      *       multi-level stream table
24210a83cb9SPrem Mallappa      */
24310a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S1P, 1); /* stage 1 supported */
24410a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTF, 2); /* AArch64 PTW only */
24510a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, COHACC, 1); /* IO coherent */
24610a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, ASID16, 1); /* 16-bit ASID */
24710a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTENDIAN, 2); /* little endian */
24810a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STALL_MODEL, 1); /* No stall */
24910a83cb9SPrem Mallappa     /* terminated transaction will always be aborted/error returned */
25010a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TERM_MODEL, 1);
25110a83cb9SPrem Mallappa     /* 2-level stream table supported */
25210a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STLEVEL, 1);
25310a83cb9SPrem Mallappa 
25410a83cb9SPrem Mallappa     s->idr[1] = FIELD_DP32(s->idr[1], IDR1, SIDSIZE, SMMU_IDR1_SIDSIZE);
25510a83cb9SPrem Mallappa     s->idr[1] = FIELD_DP32(s->idr[1], IDR1, EVENTQS, SMMU_EVENTQS);
25610a83cb9SPrem Mallappa     s->idr[1] = FIELD_DP32(s->idr[1], IDR1, CMDQS,   SMMU_CMDQS);
25710a83cb9SPrem Mallappa 
25810a83cb9SPrem Mallappa    /* 4K and 64K granule support */
25910a83cb9SPrem Mallappa     s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN4K, 1);
26010a83cb9SPrem Mallappa     s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN64K, 1);
26110a83cb9SPrem Mallappa     s->idr[5] = FIELD_DP32(s->idr[5], IDR5, OAS, SMMU_IDR5_OAS); /* 44 bits */
26210a83cb9SPrem Mallappa 
26310a83cb9SPrem Mallappa     s->cmdq.base = deposit64(s->cmdq.base, 0, 5, SMMU_CMDQS);
26410a83cb9SPrem Mallappa     s->cmdq.prod = 0;
26510a83cb9SPrem Mallappa     s->cmdq.cons = 0;
26610a83cb9SPrem Mallappa     s->cmdq.entry_size = sizeof(struct Cmd);
26710a83cb9SPrem Mallappa     s->eventq.base = deposit64(s->eventq.base, 0, 5, SMMU_EVENTQS);
26810a83cb9SPrem Mallappa     s->eventq.prod = 0;
26910a83cb9SPrem Mallappa     s->eventq.cons = 0;
27010a83cb9SPrem Mallappa     s->eventq.entry_size = sizeof(struct Evt);
27110a83cb9SPrem Mallappa 
27210a83cb9SPrem Mallappa     s->features = 0;
27310a83cb9SPrem Mallappa     s->sid_split = 0;
27410a83cb9SPrem Mallappa }
27510a83cb9SPrem Mallappa 
2769bde7f06SEric Auger static int smmu_get_ste(SMMUv3State *s, dma_addr_t addr, STE *buf,
2779bde7f06SEric Auger                         SMMUEventInfo *event)
2789bde7f06SEric Auger {
2799bde7f06SEric Auger     int ret;
2809bde7f06SEric Auger 
2819bde7f06SEric Auger     trace_smmuv3_get_ste(addr);
2829bde7f06SEric Auger     /* TODO: guarantee 64-bit single-copy atomicity */
2839bde7f06SEric Auger     ret = dma_memory_read(&address_space_memory, addr,
2849bde7f06SEric Auger                           (void *)buf, sizeof(*buf));
2859bde7f06SEric Auger     if (ret != MEMTX_OK) {
2869bde7f06SEric Auger         qemu_log_mask(LOG_GUEST_ERROR,
2879bde7f06SEric Auger                       "Cannot fetch pte at address=0x%"PRIx64"\n", addr);
2889bde7f06SEric Auger         event->type = SMMU_EVT_F_STE_FETCH;
2899bde7f06SEric Auger         event->u.f_ste_fetch.addr = addr;
2909bde7f06SEric Auger         return -EINVAL;
2919bde7f06SEric Auger     }
2929bde7f06SEric Auger     return 0;
2939bde7f06SEric Auger 
2949bde7f06SEric Auger }
2959bde7f06SEric Auger 
2969bde7f06SEric Auger /* @ssid > 0 not supported yet */
2979bde7f06SEric Auger static int smmu_get_cd(SMMUv3State *s, STE *ste, uint32_t ssid,
2989bde7f06SEric Auger                        CD *buf, SMMUEventInfo *event)
2999bde7f06SEric Auger {
3009bde7f06SEric Auger     dma_addr_t addr = STE_CTXPTR(ste);
3019bde7f06SEric Auger     int ret;
3029bde7f06SEric Auger 
3039bde7f06SEric Auger     trace_smmuv3_get_cd(addr);
3049bde7f06SEric Auger     /* TODO: guarantee 64-bit single-copy atomicity */
3059bde7f06SEric Auger     ret = dma_memory_read(&address_space_memory, addr,
3069bde7f06SEric Auger                            (void *)buf, sizeof(*buf));
3079bde7f06SEric Auger     if (ret != MEMTX_OK) {
3089bde7f06SEric Auger         qemu_log_mask(LOG_GUEST_ERROR,
3099bde7f06SEric Auger                       "Cannot fetch pte at address=0x%"PRIx64"\n", addr);
3109bde7f06SEric Auger         event->type = SMMU_EVT_F_CD_FETCH;
3119bde7f06SEric Auger         event->u.f_ste_fetch.addr = addr;
3129bde7f06SEric Auger         return -EINVAL;
3139bde7f06SEric Auger     }
3149bde7f06SEric Auger     return 0;
3159bde7f06SEric Auger }
3169bde7f06SEric Auger 
3179122bea9SJia He /* Returns < 0 in case of invalid STE, 0 otherwise */
3189bde7f06SEric Auger static int decode_ste(SMMUv3State *s, SMMUTransCfg *cfg,
3199bde7f06SEric Auger                       STE *ste, SMMUEventInfo *event)
3209bde7f06SEric Auger {
3219bde7f06SEric Auger     uint32_t config;
3229bde7f06SEric Auger 
3239bde7f06SEric Auger     if (!STE_VALID(ste)) {
3249bde7f06SEric Auger         goto bad_ste;
3259bde7f06SEric Auger     }
3269bde7f06SEric Auger 
3279bde7f06SEric Auger     config = STE_CONFIG(ste);
3289bde7f06SEric Auger 
3299bde7f06SEric Auger     if (STE_CFG_ABORT(config)) {
3309122bea9SJia He         cfg->aborted = true;
3319122bea9SJia He         return 0;
3329bde7f06SEric Auger     }
3339bde7f06SEric Auger 
3349bde7f06SEric Auger     if (STE_CFG_BYPASS(config)) {
3359bde7f06SEric Auger         cfg->bypassed = true;
3369122bea9SJia He         return 0;
3379bde7f06SEric Auger     }
3389bde7f06SEric Auger 
3399bde7f06SEric Auger     if (STE_CFG_S2_ENABLED(config)) {
3409bde7f06SEric Auger         qemu_log_mask(LOG_UNIMP, "SMMUv3 does not support stage 2 yet\n");
3419bde7f06SEric Auger         goto bad_ste;
3429bde7f06SEric Auger     }
3439bde7f06SEric Auger 
3449bde7f06SEric Auger     if (STE_S1CDMAX(ste) != 0) {
3459bde7f06SEric Auger         qemu_log_mask(LOG_UNIMP,
3469bde7f06SEric Auger                       "SMMUv3 does not support multiple context descriptors yet\n");
3479bde7f06SEric Auger         goto bad_ste;
3489bde7f06SEric Auger     }
3499bde7f06SEric Auger 
3509bde7f06SEric Auger     if (STE_S1STALLD(ste)) {
3519bde7f06SEric Auger         qemu_log_mask(LOG_UNIMP,
3529bde7f06SEric Auger                       "SMMUv3 S1 stalling fault model not allowed yet\n");
3539bde7f06SEric Auger         goto bad_ste;
3549bde7f06SEric Auger     }
3559bde7f06SEric Auger     return 0;
3569bde7f06SEric Auger 
3579bde7f06SEric Auger bad_ste:
3589bde7f06SEric Auger     event->type = SMMU_EVT_C_BAD_STE;
3599bde7f06SEric Auger     return -EINVAL;
3609bde7f06SEric Auger }
3619bde7f06SEric Auger 
3629bde7f06SEric Auger /**
3639bde7f06SEric Auger  * smmu_find_ste - Return the stream table entry associated
3649bde7f06SEric Auger  * to the sid
3659bde7f06SEric Auger  *
3669bde7f06SEric Auger  * @s: smmuv3 handle
3679bde7f06SEric Auger  * @sid: stream ID
3689bde7f06SEric Auger  * @ste: returned stream table entry
3699bde7f06SEric Auger  * @event: handle to an event info
3709bde7f06SEric Auger  *
3719bde7f06SEric Auger  * Supports linear and 2-level stream table
3729bde7f06SEric Auger  * Return 0 on success, -EINVAL otherwise
3739bde7f06SEric Auger  */
3749bde7f06SEric Auger static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste,
3759bde7f06SEric Auger                          SMMUEventInfo *event)
3769bde7f06SEric Auger {
3779bde7f06SEric Auger     dma_addr_t addr;
3789bde7f06SEric Auger     int ret;
3799bde7f06SEric Auger 
3809bde7f06SEric Auger     trace_smmuv3_find_ste(sid, s->features, s->sid_split);
3819bde7f06SEric Auger     /* Check SID range */
3829bde7f06SEric Auger     if (sid > (1 << SMMU_IDR1_SIDSIZE)) {
3839bde7f06SEric Auger         event->type = SMMU_EVT_C_BAD_STREAMID;
3849bde7f06SEric Auger         return -EINVAL;
3859bde7f06SEric Auger     }
3869bde7f06SEric Auger     if (s->features & SMMU_FEATURE_2LVL_STE) {
3879bde7f06SEric Auger         int l1_ste_offset, l2_ste_offset, max_l2_ste, span;
3889bde7f06SEric Auger         dma_addr_t strtab_base, l1ptr, l2ptr;
3899bde7f06SEric Auger         STEDesc l1std;
3909bde7f06SEric Auger 
3919bde7f06SEric Auger         strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK;
3929bde7f06SEric Auger         l1_ste_offset = sid >> s->sid_split;
3939bde7f06SEric Auger         l2_ste_offset = sid & ((1 << s->sid_split) - 1);
3949bde7f06SEric Auger         l1ptr = (dma_addr_t)(strtab_base + l1_ste_offset * sizeof(l1std));
3959bde7f06SEric Auger         /* TODO: guarantee 64-bit single-copy atomicity */
3969bde7f06SEric Auger         ret = dma_memory_read(&address_space_memory, l1ptr,
3979bde7f06SEric Auger                               (uint8_t *)&l1std, sizeof(l1std));
3989bde7f06SEric Auger         if (ret != MEMTX_OK) {
3999bde7f06SEric Auger             qemu_log_mask(LOG_GUEST_ERROR,
4009bde7f06SEric Auger                           "Could not read L1PTR at 0X%"PRIx64"\n", l1ptr);
4019bde7f06SEric Auger             event->type = SMMU_EVT_F_STE_FETCH;
4029bde7f06SEric Auger             event->u.f_ste_fetch.addr = l1ptr;
4039bde7f06SEric Auger             return -EINVAL;
4049bde7f06SEric Auger         }
4059bde7f06SEric Auger 
4069bde7f06SEric Auger         span = L1STD_SPAN(&l1std);
4079bde7f06SEric Auger 
4089bde7f06SEric Auger         if (!span) {
4099bde7f06SEric Auger             /* l2ptr is not valid */
4109bde7f06SEric Auger             qemu_log_mask(LOG_GUEST_ERROR,
4119bde7f06SEric Auger                           "invalid sid=%d (L1STD span=0)\n", sid);
4129bde7f06SEric Auger             event->type = SMMU_EVT_C_BAD_STREAMID;
4139bde7f06SEric Auger             return -EINVAL;
4149bde7f06SEric Auger         }
4159bde7f06SEric Auger         max_l2_ste = (1 << span) - 1;
4169bde7f06SEric Auger         l2ptr = l1std_l2ptr(&l1std);
4179bde7f06SEric Auger         trace_smmuv3_find_ste_2lvl(s->strtab_base, l1ptr, l1_ste_offset,
4189bde7f06SEric Auger                                    l2ptr, l2_ste_offset, max_l2_ste);
4199bde7f06SEric Auger         if (l2_ste_offset > max_l2_ste) {
4209bde7f06SEric Auger             qemu_log_mask(LOG_GUEST_ERROR,
4219bde7f06SEric Auger                           "l2_ste_offset=%d > max_l2_ste=%d\n",
4229bde7f06SEric Auger                           l2_ste_offset, max_l2_ste);
4239bde7f06SEric Auger             event->type = SMMU_EVT_C_BAD_STE;
4249bde7f06SEric Auger             return -EINVAL;
4259bde7f06SEric Auger         }
4269bde7f06SEric Auger         addr = l2ptr + l2_ste_offset * sizeof(*ste);
4279bde7f06SEric Auger     } else {
4289bde7f06SEric Auger         addr = s->strtab_base + sid * sizeof(*ste);
4299bde7f06SEric Auger     }
4309bde7f06SEric Auger 
4319bde7f06SEric Auger     if (smmu_get_ste(s, addr, ste, event)) {
4329bde7f06SEric Auger         return -EINVAL;
4339bde7f06SEric Auger     }
4349bde7f06SEric Auger 
4359bde7f06SEric Auger     return 0;
4369bde7f06SEric Auger }
4379bde7f06SEric Auger 
4389bde7f06SEric Auger static int decode_cd(SMMUTransCfg *cfg, CD *cd, SMMUEventInfo *event)
4399bde7f06SEric Auger {
4409bde7f06SEric Auger     int ret = -EINVAL;
4419bde7f06SEric Auger     int i;
4429bde7f06SEric Auger 
4439bde7f06SEric Auger     if (!CD_VALID(cd) || !CD_AARCH64(cd)) {
4449bde7f06SEric Auger         goto bad_cd;
4459bde7f06SEric Auger     }
4469bde7f06SEric Auger     if (!CD_A(cd)) {
4479bde7f06SEric Auger         goto bad_cd; /* SMMU_IDR0.TERM_MODEL == 1 */
4489bde7f06SEric Auger     }
4499bde7f06SEric Auger     if (CD_S(cd)) {
4509bde7f06SEric Auger         goto bad_cd; /* !STE_SECURE && SMMU_IDR0.STALL_MODEL == 1 */
4519bde7f06SEric Auger     }
4529bde7f06SEric Auger     if (CD_HA(cd) || CD_HD(cd)) {
4539bde7f06SEric Auger         goto bad_cd; /* HTTU = 0 */
4549bde7f06SEric Auger     }
4559bde7f06SEric Auger 
4569bde7f06SEric Auger     /* we support only those at the moment */
4579bde7f06SEric Auger     cfg->aa64 = true;
4589bde7f06SEric Auger     cfg->stage = 1;
4599bde7f06SEric Auger 
4609bde7f06SEric Auger     cfg->oas = oas2bits(CD_IPS(cd));
4619bde7f06SEric Auger     cfg->oas = MIN(oas2bits(SMMU_IDR5_OAS), cfg->oas);
4629bde7f06SEric Auger     cfg->tbi = CD_TBI(cd);
4639bde7f06SEric Auger     cfg->asid = CD_ASID(cd);
4649bde7f06SEric Auger 
4659bde7f06SEric Auger     trace_smmuv3_decode_cd(cfg->oas);
4669bde7f06SEric Auger 
4679bde7f06SEric Auger     /* decode data dependent on TT */
4689bde7f06SEric Auger     for (i = 0; i <= 1; i++) {
4699bde7f06SEric Auger         int tg, tsz;
4709bde7f06SEric Auger         SMMUTransTableInfo *tt = &cfg->tt[i];
4719bde7f06SEric Auger 
4729bde7f06SEric Auger         cfg->tt[i].disabled = CD_EPD(cd, i);
4739bde7f06SEric Auger         if (cfg->tt[i].disabled) {
4749bde7f06SEric Auger             continue;
4759bde7f06SEric Auger         }
4769bde7f06SEric Auger 
4779bde7f06SEric Auger         tsz = CD_TSZ(cd, i);
4789bde7f06SEric Auger         if (tsz < 16 || tsz > 39) {
4799bde7f06SEric Auger             goto bad_cd;
4809bde7f06SEric Auger         }
4819bde7f06SEric Auger 
4829bde7f06SEric Auger         tg = CD_TG(cd, i);
4839bde7f06SEric Auger         tt->granule_sz = tg2granule(tg, i);
4849bde7f06SEric Auger         if ((tt->granule_sz != 12 && tt->granule_sz != 16) || CD_ENDI(cd)) {
4859bde7f06SEric Auger             goto bad_cd;
4869bde7f06SEric Auger         }
4879bde7f06SEric Auger 
4889bde7f06SEric Auger         tt->tsz = tsz;
4899bde7f06SEric Auger         tt->ttb = CD_TTB(cd, i);
4909bde7f06SEric Auger         if (tt->ttb & ~(MAKE_64BIT_MASK(0, cfg->oas))) {
4919bde7f06SEric Auger             goto bad_cd;
4929bde7f06SEric Auger         }
4939bde7f06SEric Auger         trace_smmuv3_decode_cd_tt(i, tt->tsz, tt->ttb, tt->granule_sz);
4949bde7f06SEric Auger     }
4959bde7f06SEric Auger 
4969bde7f06SEric Auger     event->record_trans_faults = CD_R(cd);
4979bde7f06SEric Auger 
4989bde7f06SEric Auger     return 0;
4999bde7f06SEric Auger 
5009bde7f06SEric Auger bad_cd:
5019bde7f06SEric Auger     event->type = SMMU_EVT_C_BAD_CD;
5029bde7f06SEric Auger     return ret;
5039bde7f06SEric Auger }
5049bde7f06SEric Auger 
5059bde7f06SEric Auger /**
5069bde7f06SEric Auger  * smmuv3_decode_config - Prepare the translation configuration
5079bde7f06SEric Auger  * for the @mr iommu region
5089bde7f06SEric Auger  * @mr: iommu memory region the translation config must be prepared for
5099bde7f06SEric Auger  * @cfg: output translation configuration which is populated through
5109bde7f06SEric Auger  *       the different configuration decoding steps
5119bde7f06SEric Auger  * @event: must be zero'ed by the caller
5129bde7f06SEric Auger  *
5139122bea9SJia He  * return < 0 in case of config decoding error (@event is filled
5149bde7f06SEric Auger  * accordingly). Return 0 otherwise.
5159bde7f06SEric Auger  */
5169bde7f06SEric Auger static int smmuv3_decode_config(IOMMUMemoryRegion *mr, SMMUTransCfg *cfg,
5179bde7f06SEric Auger                                 SMMUEventInfo *event)
5189bde7f06SEric Auger {
5199bde7f06SEric Auger     SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
5209bde7f06SEric Auger     uint32_t sid = smmu_get_sid(sdev);
5219bde7f06SEric Auger     SMMUv3State *s = sdev->smmu;
5229122bea9SJia He     int ret;
5239bde7f06SEric Auger     STE ste;
5249bde7f06SEric Auger     CD cd;
5259bde7f06SEric Auger 
5269122bea9SJia He     ret = smmu_find_ste(s, sid, &ste, event);
5279122bea9SJia He     if (ret) {
5289bde7f06SEric Auger         return ret;
5299bde7f06SEric Auger     }
5309bde7f06SEric Auger 
5319122bea9SJia He     ret = decode_ste(s, cfg, &ste, event);
5329122bea9SJia He     if (ret) {
5339bde7f06SEric Auger         return ret;
5349bde7f06SEric Auger     }
5359bde7f06SEric Auger 
5369122bea9SJia He     if (cfg->aborted || cfg->bypassed) {
5379122bea9SJia He         return 0;
5389122bea9SJia He     }
5399122bea9SJia He 
5409122bea9SJia He     ret = smmu_get_cd(s, &ste, 0 /* ssid */, &cd, event);
5419122bea9SJia He     if (ret) {
5429bde7f06SEric Auger         return ret;
5439bde7f06SEric Auger     }
5449bde7f06SEric Auger 
5459bde7f06SEric Auger     return decode_cd(cfg, &cd, event);
5469bde7f06SEric Auger }
5479bde7f06SEric Auger 
54832cfd7f3SEric Auger /**
54932cfd7f3SEric Auger  * smmuv3_get_config - Look up for a cached copy of configuration data for
55032cfd7f3SEric Auger  * @sdev and on cache miss performs a configuration structure decoding from
55132cfd7f3SEric Auger  * guest RAM.
55232cfd7f3SEric Auger  *
55332cfd7f3SEric Auger  * @sdev: SMMUDevice handle
55432cfd7f3SEric Auger  * @event: output event info
55532cfd7f3SEric Auger  *
55632cfd7f3SEric Auger  * The configuration cache contains data resulting from both STE and CD
55732cfd7f3SEric Auger  * decoding under the form of an SMMUTransCfg struct. The hash table is indexed
55832cfd7f3SEric Auger  * by the SMMUDevice handle.
55932cfd7f3SEric Auger  */
56032cfd7f3SEric Auger static SMMUTransCfg *smmuv3_get_config(SMMUDevice *sdev, SMMUEventInfo *event)
56132cfd7f3SEric Auger {
56232cfd7f3SEric Auger     SMMUv3State *s = sdev->smmu;
56332cfd7f3SEric Auger     SMMUState *bc = &s->smmu_state;
56432cfd7f3SEric Auger     SMMUTransCfg *cfg;
56532cfd7f3SEric Auger 
56632cfd7f3SEric Auger     cfg = g_hash_table_lookup(bc->configs, sdev);
56732cfd7f3SEric Auger     if (cfg) {
56832cfd7f3SEric Auger         sdev->cfg_cache_hits++;
56932cfd7f3SEric Auger         trace_smmuv3_config_cache_hit(smmu_get_sid(sdev),
57032cfd7f3SEric Auger                             sdev->cfg_cache_hits, sdev->cfg_cache_misses,
57132cfd7f3SEric Auger                             100 * sdev->cfg_cache_hits /
57232cfd7f3SEric Auger                             (sdev->cfg_cache_hits + sdev->cfg_cache_misses));
57332cfd7f3SEric Auger     } else {
57432cfd7f3SEric Auger         sdev->cfg_cache_misses++;
57532cfd7f3SEric Auger         trace_smmuv3_config_cache_miss(smmu_get_sid(sdev),
57632cfd7f3SEric Auger                             sdev->cfg_cache_hits, sdev->cfg_cache_misses,
57732cfd7f3SEric Auger                             100 * sdev->cfg_cache_hits /
57832cfd7f3SEric Auger                             (sdev->cfg_cache_hits + sdev->cfg_cache_misses));
57932cfd7f3SEric Auger         cfg = g_new0(SMMUTransCfg, 1);
58032cfd7f3SEric Auger 
58132cfd7f3SEric Auger         if (!smmuv3_decode_config(&sdev->iommu, cfg, event)) {
58232cfd7f3SEric Auger             g_hash_table_insert(bc->configs, sdev, cfg);
58332cfd7f3SEric Auger         } else {
58432cfd7f3SEric Auger             g_free(cfg);
58532cfd7f3SEric Auger             cfg = NULL;
58632cfd7f3SEric Auger         }
58732cfd7f3SEric Auger     }
58832cfd7f3SEric Auger     return cfg;
58932cfd7f3SEric Auger }
59032cfd7f3SEric Auger 
59132cfd7f3SEric Auger static void smmuv3_flush_config(SMMUDevice *sdev)
59232cfd7f3SEric Auger {
59332cfd7f3SEric Auger     SMMUv3State *s = sdev->smmu;
59432cfd7f3SEric Auger     SMMUState *bc = &s->smmu_state;
59532cfd7f3SEric Auger 
59632cfd7f3SEric Auger     trace_smmuv3_config_cache_inv(smmu_get_sid(sdev));
59732cfd7f3SEric Auger     g_hash_table_remove(bc->configs, sdev);
59832cfd7f3SEric Auger }
59932cfd7f3SEric Auger 
6009bde7f06SEric Auger static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr,
6012c91bcf2SPeter Maydell                                       IOMMUAccessFlags flag, int iommu_idx)
6029bde7f06SEric Auger {
6039bde7f06SEric Auger     SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
6049bde7f06SEric Auger     SMMUv3State *s = sdev->smmu;
6059bde7f06SEric Auger     uint32_t sid = smmu_get_sid(sdev);
6069122bea9SJia He     SMMUEventInfo event = {.type = SMMU_EVT_NONE, .sid = sid};
6079bde7f06SEric Auger     SMMUPTWEventInfo ptw_info = {};
6089122bea9SJia He     SMMUTranslationStatus status;
609cc27ed81SEric Auger     SMMUState *bs = ARM_SMMU(s);
610cc27ed81SEric Auger     uint64_t page_mask, aligned_addr;
611cc27ed81SEric Auger     IOMMUTLBEntry *cached_entry = NULL;
612cc27ed81SEric Auger     SMMUTransTableInfo *tt;
61332cfd7f3SEric Auger     SMMUTransCfg *cfg = NULL;
6149bde7f06SEric Auger     IOMMUTLBEntry entry = {
6159bde7f06SEric Auger         .target_as = &address_space_memory,
6169bde7f06SEric Auger         .iova = addr,
6179bde7f06SEric Auger         .translated_addr = addr,
6189bde7f06SEric Auger         .addr_mask = ~(hwaddr)0,
6199bde7f06SEric Auger         .perm = IOMMU_NONE,
6209bde7f06SEric Auger     };
621cc27ed81SEric Auger     SMMUIOTLBKey key, *new_key;
6229bde7f06SEric Auger 
62332cfd7f3SEric Auger     qemu_mutex_lock(&s->mutex);
62432cfd7f3SEric Auger 
6259bde7f06SEric Auger     if (!smmu_enabled(s)) {
6269122bea9SJia He         status = SMMU_TRANS_DISABLE;
6279122bea9SJia He         goto epilogue;
6289bde7f06SEric Auger     }
6299bde7f06SEric Auger 
63032cfd7f3SEric Auger     cfg = smmuv3_get_config(sdev, &event);
63132cfd7f3SEric Auger     if (!cfg) {
6329122bea9SJia He         status = SMMU_TRANS_ERROR;
6339122bea9SJia He         goto epilogue;
6349bde7f06SEric Auger     }
6359bde7f06SEric Auger 
63632cfd7f3SEric Auger     if (cfg->aborted) {
6379122bea9SJia He         status = SMMU_TRANS_ABORT;
6389122bea9SJia He         goto epilogue;
6399bde7f06SEric Auger     }
6409bde7f06SEric Auger 
64132cfd7f3SEric Auger     if (cfg->bypassed) {
6429122bea9SJia He         status = SMMU_TRANS_BYPASS;
6439122bea9SJia He         goto epilogue;
6449122bea9SJia He     }
6459122bea9SJia He 
646cc27ed81SEric Auger     tt = select_tt(cfg, addr);
647cc27ed81SEric Auger     if (!tt) {
648cc27ed81SEric Auger         if (event.record_trans_faults) {
649cc27ed81SEric Auger             event.type = SMMU_EVT_F_TRANSLATION;
650cc27ed81SEric Auger             event.u.f_translation.addr = addr;
651cc27ed81SEric Auger             event.u.f_translation.rnw = flag & 0x1;
652cc27ed81SEric Auger         }
653cc27ed81SEric Auger         status = SMMU_TRANS_ERROR;
654cc27ed81SEric Auger         goto epilogue;
655cc27ed81SEric Auger     }
656cc27ed81SEric Auger 
657cc27ed81SEric Auger     page_mask = (1ULL << (tt->granule_sz)) - 1;
658cc27ed81SEric Auger     aligned_addr = addr & ~page_mask;
659cc27ed81SEric Auger 
660cc27ed81SEric Auger     key.asid = cfg->asid;
661cc27ed81SEric Auger     key.iova = aligned_addr;
662cc27ed81SEric Auger 
663cc27ed81SEric Auger     cached_entry = g_hash_table_lookup(bs->iotlb, &key);
664cc27ed81SEric Auger     if (cached_entry) {
665cc27ed81SEric Auger         cfg->iotlb_hits++;
666cc27ed81SEric Auger         trace_smmu_iotlb_cache_hit(cfg->asid, aligned_addr,
667cc27ed81SEric Auger                                    cfg->iotlb_hits, cfg->iotlb_misses,
668cc27ed81SEric Auger                                    100 * cfg->iotlb_hits /
669cc27ed81SEric Auger                                    (cfg->iotlb_hits + cfg->iotlb_misses));
670cc27ed81SEric Auger         if ((flag & IOMMU_WO) && !(cached_entry->perm & IOMMU_WO)) {
671cc27ed81SEric Auger             status = SMMU_TRANS_ERROR;
672cc27ed81SEric Auger             if (event.record_trans_faults) {
673cc27ed81SEric Auger                 event.type = SMMU_EVT_F_PERMISSION;
674cc27ed81SEric Auger                 event.u.f_permission.addr = addr;
675cc27ed81SEric Auger                 event.u.f_permission.rnw = flag & 0x1;
676cc27ed81SEric Auger             }
677cc27ed81SEric Auger         } else {
678cc27ed81SEric Auger             status = SMMU_TRANS_SUCCESS;
679cc27ed81SEric Auger         }
680cc27ed81SEric Auger         goto epilogue;
681cc27ed81SEric Auger     }
682cc27ed81SEric Auger 
683cc27ed81SEric Auger     cfg->iotlb_misses++;
684cc27ed81SEric Auger     trace_smmu_iotlb_cache_miss(cfg->asid, addr & ~page_mask,
685cc27ed81SEric Auger                                 cfg->iotlb_hits, cfg->iotlb_misses,
686cc27ed81SEric Auger                                 100 * cfg->iotlb_hits /
687cc27ed81SEric Auger                                 (cfg->iotlb_hits + cfg->iotlb_misses));
688cc27ed81SEric Auger 
689cc27ed81SEric Auger     if (g_hash_table_size(bs->iotlb) >= SMMU_IOTLB_MAX_SIZE) {
690cc27ed81SEric Auger         smmu_iotlb_inv_all(bs);
691cc27ed81SEric Auger     }
692cc27ed81SEric Auger 
693cc27ed81SEric Auger     cached_entry = g_new0(IOMMUTLBEntry, 1);
694cc27ed81SEric Auger 
695cc27ed81SEric Auger     if (smmu_ptw(cfg, aligned_addr, flag, cached_entry, &ptw_info)) {
696cc27ed81SEric Auger         g_free(cached_entry);
6979bde7f06SEric Auger         switch (ptw_info.type) {
6989bde7f06SEric Auger         case SMMU_PTW_ERR_WALK_EABT:
6999bde7f06SEric Auger             event.type = SMMU_EVT_F_WALK_EABT;
7009bde7f06SEric Auger             event.u.f_walk_eabt.addr = addr;
7019bde7f06SEric Auger             event.u.f_walk_eabt.rnw = flag & 0x1;
7029bde7f06SEric Auger             event.u.f_walk_eabt.class = 0x1;
7039bde7f06SEric Auger             event.u.f_walk_eabt.addr2 = ptw_info.addr;
7049bde7f06SEric Auger             break;
7059bde7f06SEric Auger         case SMMU_PTW_ERR_TRANSLATION:
7069bde7f06SEric Auger             if (event.record_trans_faults) {
7079bde7f06SEric Auger                 event.type = SMMU_EVT_F_TRANSLATION;
7089bde7f06SEric Auger                 event.u.f_translation.addr = addr;
7099bde7f06SEric Auger                 event.u.f_translation.rnw = flag & 0x1;
7109bde7f06SEric Auger             }
7119bde7f06SEric Auger             break;
7129bde7f06SEric Auger         case SMMU_PTW_ERR_ADDR_SIZE:
7139bde7f06SEric Auger             if (event.record_trans_faults) {
7149bde7f06SEric Auger                 event.type = SMMU_EVT_F_ADDR_SIZE;
7159bde7f06SEric Auger                 event.u.f_addr_size.addr = addr;
7169bde7f06SEric Auger                 event.u.f_addr_size.rnw = flag & 0x1;
7179bde7f06SEric Auger             }
7189bde7f06SEric Auger             break;
7199bde7f06SEric Auger         case SMMU_PTW_ERR_ACCESS:
7209bde7f06SEric Auger             if (event.record_trans_faults) {
7219bde7f06SEric Auger                 event.type = SMMU_EVT_F_ACCESS;
7229bde7f06SEric Auger                 event.u.f_access.addr = addr;
7239bde7f06SEric Auger                 event.u.f_access.rnw = flag & 0x1;
7249bde7f06SEric Auger             }
7259bde7f06SEric Auger             break;
7269bde7f06SEric Auger         case SMMU_PTW_ERR_PERMISSION:
7279bde7f06SEric Auger             if (event.record_trans_faults) {
7289bde7f06SEric Auger                 event.type = SMMU_EVT_F_PERMISSION;
7299bde7f06SEric Auger                 event.u.f_permission.addr = addr;
7309bde7f06SEric Auger                 event.u.f_permission.rnw = flag & 0x1;
7319bde7f06SEric Auger             }
7329bde7f06SEric Auger             break;
7339bde7f06SEric Auger         default:
7349bde7f06SEric Auger             g_assert_not_reached();
7359bde7f06SEric Auger         }
7369122bea9SJia He         status = SMMU_TRANS_ERROR;
7379122bea9SJia He     } else {
738cc27ed81SEric Auger         new_key = g_new0(SMMUIOTLBKey, 1);
739cc27ed81SEric Auger         new_key->asid = cfg->asid;
740cc27ed81SEric Auger         new_key->iova = aligned_addr;
741cc27ed81SEric Auger         g_hash_table_insert(bs->iotlb, new_key, cached_entry);
7429122bea9SJia He         status = SMMU_TRANS_SUCCESS;
7439bde7f06SEric Auger     }
7449122bea9SJia He 
7459122bea9SJia He epilogue:
74632cfd7f3SEric Auger     qemu_mutex_unlock(&s->mutex);
7479122bea9SJia He     switch (status) {
7489122bea9SJia He     case SMMU_TRANS_SUCCESS:
7499bde7f06SEric Auger         entry.perm = flag;
750cc27ed81SEric Auger         entry.translated_addr = cached_entry->translated_addr +
751cc27ed81SEric Auger                                     (addr & page_mask);
752cc27ed81SEric Auger         entry.addr_mask = cached_entry->addr_mask;
7539122bea9SJia He         trace_smmuv3_translate_success(mr->parent_obj.name, sid, addr,
7549bde7f06SEric Auger                                        entry.translated_addr, entry.perm);
7559122bea9SJia He         break;
7569122bea9SJia He     case SMMU_TRANS_DISABLE:
7579122bea9SJia He         entry.perm = flag;
7589122bea9SJia He         entry.addr_mask = ~TARGET_PAGE_MASK;
7599122bea9SJia He         trace_smmuv3_translate_disable(mr->parent_obj.name, sid, addr,
7609122bea9SJia He                                       entry.perm);
7619122bea9SJia He         break;
7629122bea9SJia He     case SMMU_TRANS_BYPASS:
7639122bea9SJia He         entry.perm = flag;
7649122bea9SJia He         entry.addr_mask = ~TARGET_PAGE_MASK;
7659122bea9SJia He         trace_smmuv3_translate_bypass(mr->parent_obj.name, sid, addr,
7669122bea9SJia He                                       entry.perm);
7679122bea9SJia He         break;
7689122bea9SJia He     case SMMU_TRANS_ABORT:
7699122bea9SJia He         /* no event is recorded on abort */
7709122bea9SJia He         trace_smmuv3_translate_abort(mr->parent_obj.name, sid, addr,
7719122bea9SJia He                                      entry.perm);
7729122bea9SJia He         break;
7739122bea9SJia He     case SMMU_TRANS_ERROR:
7749122bea9SJia He         qemu_log_mask(LOG_GUEST_ERROR,
7759122bea9SJia He                       "%s translation failed for iova=0x%"PRIx64"(%s)\n",
7769122bea9SJia He                       mr->parent_obj.name, addr, smmu_event_string(event.type));
7779122bea9SJia He         smmuv3_record_event(s, &event);
7789122bea9SJia He         break;
7799bde7f06SEric Auger     }
7809bde7f06SEric Auger 
7819bde7f06SEric Auger     return entry;
7829bde7f06SEric Auger }
7839bde7f06SEric Auger 
784832e4222SEric Auger /**
785832e4222SEric Auger  * smmuv3_notify_iova - call the notifier @n for a given
786832e4222SEric Auger  * @asid and @iova tuple.
787832e4222SEric Auger  *
788832e4222SEric Auger  * @mr: IOMMU mr region handle
789832e4222SEric Auger  * @n: notifier to be called
790832e4222SEric Auger  * @asid: address space ID or negative value if we don't care
791832e4222SEric Auger  * @iova: iova
792832e4222SEric Auger  */
793832e4222SEric Auger static void smmuv3_notify_iova(IOMMUMemoryRegion *mr,
794832e4222SEric Auger                                IOMMUNotifier *n,
795832e4222SEric Auger                                int asid,
796832e4222SEric Auger                                dma_addr_t iova)
797832e4222SEric Auger {
798832e4222SEric Auger     SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
799832e4222SEric Auger     SMMUEventInfo event = {};
800832e4222SEric Auger     SMMUTransTableInfo *tt;
801832e4222SEric Auger     SMMUTransCfg *cfg;
802832e4222SEric Auger     IOMMUTLBEntry entry;
803832e4222SEric Auger 
804832e4222SEric Auger     cfg = smmuv3_get_config(sdev, &event);
805832e4222SEric Auger     if (!cfg) {
806832e4222SEric Auger         qemu_log_mask(LOG_GUEST_ERROR,
807832e4222SEric Auger                       "%s error decoding the configuration for iommu mr=%s\n",
808832e4222SEric Auger                       __func__, mr->parent_obj.name);
809832e4222SEric Auger         return;
810832e4222SEric Auger     }
811832e4222SEric Auger 
812832e4222SEric Auger     if (asid >= 0 && cfg->asid != asid) {
813832e4222SEric Auger         return;
814832e4222SEric Auger     }
815832e4222SEric Auger 
816832e4222SEric Auger     tt = select_tt(cfg, iova);
817832e4222SEric Auger     if (!tt) {
818832e4222SEric Auger         return;
819832e4222SEric Auger     }
820832e4222SEric Auger 
821832e4222SEric Auger     entry.target_as = &address_space_memory;
822832e4222SEric Auger     entry.iova = iova;
823832e4222SEric Auger     entry.addr_mask = (1 << tt->granule_sz) - 1;
824832e4222SEric Auger     entry.perm = IOMMU_NONE;
825832e4222SEric Auger 
826832e4222SEric Auger     memory_region_notify_one(n, &entry);
827832e4222SEric Auger }
828832e4222SEric Auger 
829832e4222SEric Auger /* invalidate an asid/iova tuple in all mr's */
830832e4222SEric Auger static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, dma_addr_t iova)
831832e4222SEric Auger {
832c6370441SEric Auger     SMMUDevice *sdev;
833832e4222SEric Auger 
834c6370441SEric Auger     QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) {
835c6370441SEric Auger         IOMMUMemoryRegion *mr = &sdev->iommu;
836832e4222SEric Auger         IOMMUNotifier *n;
837832e4222SEric Auger 
838832e4222SEric Auger         trace_smmuv3_inv_notifiers_iova(mr->parent_obj.name, asid, iova);
839832e4222SEric Auger 
840832e4222SEric Auger         IOMMU_NOTIFIER_FOREACH(n, mr) {
841832e4222SEric Auger             smmuv3_notify_iova(mr, n, asid, iova);
842832e4222SEric Auger         }
843832e4222SEric Auger     }
844832e4222SEric Auger }
845832e4222SEric Auger 
846fae4be38SEric Auger static int smmuv3_cmdq_consume(SMMUv3State *s)
847dadd1a08SEric Auger {
84832cfd7f3SEric Auger     SMMUState *bs = ARM_SMMU(s);
849dadd1a08SEric Auger     SMMUCmdError cmd_error = SMMU_CERROR_NONE;
850dadd1a08SEric Auger     SMMUQueue *q = &s->cmdq;
851dadd1a08SEric Auger     SMMUCommandType type = 0;
852dadd1a08SEric Auger 
853dadd1a08SEric Auger     if (!smmuv3_cmdq_enabled(s)) {
854dadd1a08SEric Auger         return 0;
855dadd1a08SEric Auger     }
856dadd1a08SEric Auger     /*
857dadd1a08SEric Auger      * some commands depend on register values, typically CR0. In case those
858dadd1a08SEric Auger      * register values change while handling the command, spec says it
859dadd1a08SEric Auger      * is UNPREDICTABLE whether the command is interpreted under the new
860dadd1a08SEric Auger      * or old value.
861dadd1a08SEric Auger      */
862dadd1a08SEric Auger 
863dadd1a08SEric Auger     while (!smmuv3_q_empty(q)) {
864dadd1a08SEric Auger         uint32_t pending = s->gerror ^ s->gerrorn;
865dadd1a08SEric Auger         Cmd cmd;
866dadd1a08SEric Auger 
867dadd1a08SEric Auger         trace_smmuv3_cmdq_consume(Q_PROD(q), Q_CONS(q),
868dadd1a08SEric Auger                                   Q_PROD_WRAP(q), Q_CONS_WRAP(q));
869dadd1a08SEric Auger 
870dadd1a08SEric Auger         if (FIELD_EX32(pending, GERROR, CMDQ_ERR)) {
871dadd1a08SEric Auger             break;
872dadd1a08SEric Auger         }
873dadd1a08SEric Auger 
874dadd1a08SEric Auger         if (queue_read(q, &cmd) != MEMTX_OK) {
875dadd1a08SEric Auger             cmd_error = SMMU_CERROR_ABT;
876dadd1a08SEric Auger             break;
877dadd1a08SEric Auger         }
878dadd1a08SEric Auger 
879dadd1a08SEric Auger         type = CMD_TYPE(&cmd);
880dadd1a08SEric Auger 
881dadd1a08SEric Auger         trace_smmuv3_cmdq_opcode(smmu_cmd_string(type));
882dadd1a08SEric Auger 
88332cfd7f3SEric Auger         qemu_mutex_lock(&s->mutex);
884dadd1a08SEric Auger         switch (type) {
885dadd1a08SEric Auger         case SMMU_CMD_SYNC:
886dadd1a08SEric Auger             if (CMD_SYNC_CS(&cmd) & CMD_SYNC_SIG_IRQ) {
887dadd1a08SEric Auger                 smmuv3_trigger_irq(s, SMMU_IRQ_CMD_SYNC, 0);
888dadd1a08SEric Auger             }
889dadd1a08SEric Auger             break;
890dadd1a08SEric Auger         case SMMU_CMD_PREFETCH_CONFIG:
891dadd1a08SEric Auger         case SMMU_CMD_PREFETCH_ADDR:
89232cfd7f3SEric Auger             break;
893dadd1a08SEric Auger         case SMMU_CMD_CFGI_STE:
89432cfd7f3SEric Auger         {
89532cfd7f3SEric Auger             uint32_t sid = CMD_SID(&cmd);
89632cfd7f3SEric Auger             IOMMUMemoryRegion *mr = smmu_iommu_mr(bs, sid);
89732cfd7f3SEric Auger             SMMUDevice *sdev;
89832cfd7f3SEric Auger 
89932cfd7f3SEric Auger             if (CMD_SSEC(&cmd)) {
90032cfd7f3SEric Auger                 cmd_error = SMMU_CERROR_ILL;
90132cfd7f3SEric Auger                 break;
90232cfd7f3SEric Auger             }
90332cfd7f3SEric Auger 
90432cfd7f3SEric Auger             if (!mr) {
90532cfd7f3SEric Auger                 break;
90632cfd7f3SEric Auger             }
90732cfd7f3SEric Auger 
90832cfd7f3SEric Auger             trace_smmuv3_cmdq_cfgi_ste(sid);
90932cfd7f3SEric Auger             sdev = container_of(mr, SMMUDevice, iommu);
91032cfd7f3SEric Auger             smmuv3_flush_config(sdev);
91132cfd7f3SEric Auger 
91232cfd7f3SEric Auger             break;
91332cfd7f3SEric Auger         }
914dadd1a08SEric Auger         case SMMU_CMD_CFGI_STE_RANGE: /* same as SMMU_CMD_CFGI_ALL */
91532cfd7f3SEric Auger         {
91632cfd7f3SEric Auger             uint32_t start = CMD_SID(&cmd), end, i;
91732cfd7f3SEric Auger             uint8_t range = CMD_STE_RANGE(&cmd);
91832cfd7f3SEric Auger 
91932cfd7f3SEric Auger             if (CMD_SSEC(&cmd)) {
92032cfd7f3SEric Auger                 cmd_error = SMMU_CERROR_ILL;
92132cfd7f3SEric Auger                 break;
92232cfd7f3SEric Auger             }
92332cfd7f3SEric Auger 
92432cfd7f3SEric Auger             end = start + (1 << (range + 1)) - 1;
92532cfd7f3SEric Auger             trace_smmuv3_cmdq_cfgi_ste_range(start, end);
92632cfd7f3SEric Auger 
92732cfd7f3SEric Auger             for (i = start; i <= end; i++) {
92832cfd7f3SEric Auger                 IOMMUMemoryRegion *mr = smmu_iommu_mr(bs, i);
92932cfd7f3SEric Auger                 SMMUDevice *sdev;
93032cfd7f3SEric Auger 
93132cfd7f3SEric Auger                 if (!mr) {
93232cfd7f3SEric Auger                     continue;
93332cfd7f3SEric Auger                 }
93432cfd7f3SEric Auger                 sdev = container_of(mr, SMMUDevice, iommu);
93532cfd7f3SEric Auger                 smmuv3_flush_config(sdev);
93632cfd7f3SEric Auger             }
93732cfd7f3SEric Auger             break;
93832cfd7f3SEric Auger         }
939dadd1a08SEric Auger         case SMMU_CMD_CFGI_CD:
940dadd1a08SEric Auger         case SMMU_CMD_CFGI_CD_ALL:
94132cfd7f3SEric Auger         {
94232cfd7f3SEric Auger             uint32_t sid = CMD_SID(&cmd);
94332cfd7f3SEric Auger             IOMMUMemoryRegion *mr = smmu_iommu_mr(bs, sid);
94432cfd7f3SEric Auger             SMMUDevice *sdev;
94532cfd7f3SEric Auger 
94632cfd7f3SEric Auger             if (CMD_SSEC(&cmd)) {
94732cfd7f3SEric Auger                 cmd_error = SMMU_CERROR_ILL;
94832cfd7f3SEric Auger                 break;
94932cfd7f3SEric Auger             }
95032cfd7f3SEric Auger 
95132cfd7f3SEric Auger             if (!mr) {
95232cfd7f3SEric Auger                 break;
95332cfd7f3SEric Auger             }
95432cfd7f3SEric Auger 
95532cfd7f3SEric Auger             trace_smmuv3_cmdq_cfgi_cd(sid);
95632cfd7f3SEric Auger             sdev = container_of(mr, SMMUDevice, iommu);
95732cfd7f3SEric Auger             smmuv3_flush_config(sdev);
95832cfd7f3SEric Auger             break;
95932cfd7f3SEric Auger         }
960dadd1a08SEric Auger         case SMMU_CMD_TLBI_NH_ASID:
961cc27ed81SEric Auger         {
962cc27ed81SEric Auger             uint16_t asid = CMD_ASID(&cmd);
963cc27ed81SEric Auger 
964cc27ed81SEric Auger             trace_smmuv3_cmdq_tlbi_nh_asid(asid);
965832e4222SEric Auger             smmu_inv_notifiers_all(&s->smmu_state);
966cc27ed81SEric Auger             smmu_iotlb_inv_asid(bs, asid);
967cc27ed81SEric Auger             break;
968cc27ed81SEric Auger         }
969cc27ed81SEric Auger         case SMMU_CMD_TLBI_NH_ALL:
970cc27ed81SEric Auger         case SMMU_CMD_TLBI_NSNH_ALL:
971cc27ed81SEric Auger             trace_smmuv3_cmdq_tlbi_nh();
972832e4222SEric Auger             smmu_inv_notifiers_all(&s->smmu_state);
973cc27ed81SEric Auger             smmu_iotlb_inv_all(bs);
974cc27ed81SEric Auger             break;
975dadd1a08SEric Auger         case SMMU_CMD_TLBI_NH_VAA:
976cc27ed81SEric Auger         {
977cc27ed81SEric Auger             dma_addr_t addr = CMD_ADDR(&cmd);
978cc27ed81SEric Auger             uint16_t vmid = CMD_VMID(&cmd);
979cc27ed81SEric Auger 
980cc27ed81SEric Auger             trace_smmuv3_cmdq_tlbi_nh_vaa(vmid, addr);
981832e4222SEric Auger             smmuv3_inv_notifiers_iova(bs, -1, addr);
982cc27ed81SEric Auger             smmu_iotlb_inv_all(bs);
983cc27ed81SEric Auger             break;
984cc27ed81SEric Auger         }
985cc27ed81SEric Auger         case SMMU_CMD_TLBI_NH_VA:
986cc27ed81SEric Auger         {
987cc27ed81SEric Auger             uint16_t asid = CMD_ASID(&cmd);
988cc27ed81SEric Auger             uint16_t vmid = CMD_VMID(&cmd);
989cc27ed81SEric Auger             dma_addr_t addr = CMD_ADDR(&cmd);
990cc27ed81SEric Auger             bool leaf = CMD_LEAF(&cmd);
991cc27ed81SEric Auger 
992cc27ed81SEric Auger             trace_smmuv3_cmdq_tlbi_nh_va(vmid, asid, addr, leaf);
993832e4222SEric Auger             smmuv3_inv_notifiers_iova(bs, asid, addr);
994cc27ed81SEric Auger             smmu_iotlb_inv_iova(bs, asid, addr);
995cc27ed81SEric Auger             break;
996cc27ed81SEric Auger         }
997dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL3_ALL:
998dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL3_VA:
999dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL2_ALL:
1000dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL2_ASID:
1001dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL2_VA:
1002dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL2_VAA:
1003dadd1a08SEric Auger         case SMMU_CMD_TLBI_S12_VMALL:
1004dadd1a08SEric Auger         case SMMU_CMD_TLBI_S2_IPA:
1005dadd1a08SEric Auger         case SMMU_CMD_ATC_INV:
1006dadd1a08SEric Auger         case SMMU_CMD_PRI_RESP:
1007dadd1a08SEric Auger         case SMMU_CMD_RESUME:
1008dadd1a08SEric Auger         case SMMU_CMD_STALL_TERM:
1009dadd1a08SEric Auger             trace_smmuv3_unhandled_cmd(type);
1010dadd1a08SEric Auger             break;
1011dadd1a08SEric Auger         default:
1012dadd1a08SEric Auger             cmd_error = SMMU_CERROR_ILL;
1013dadd1a08SEric Auger             qemu_log_mask(LOG_GUEST_ERROR,
1014dadd1a08SEric Auger                           "Illegal command type: %d\n", CMD_TYPE(&cmd));
1015dadd1a08SEric Auger             break;
1016dadd1a08SEric Auger         }
101732cfd7f3SEric Auger         qemu_mutex_unlock(&s->mutex);
1018dadd1a08SEric Auger         if (cmd_error) {
1019dadd1a08SEric Auger             break;
1020dadd1a08SEric Auger         }
1021dadd1a08SEric Auger         /*
1022dadd1a08SEric Auger          * We only increment the cons index after the completion of
1023dadd1a08SEric Auger          * the command. We do that because the SYNC returns immediately
1024dadd1a08SEric Auger          * and does not check the completion of previous commands
1025dadd1a08SEric Auger          */
1026dadd1a08SEric Auger         queue_cons_incr(q);
1027dadd1a08SEric Auger     }
1028dadd1a08SEric Auger 
1029dadd1a08SEric Auger     if (cmd_error) {
1030dadd1a08SEric Auger         trace_smmuv3_cmdq_consume_error(smmu_cmd_string(type), cmd_error);
1031dadd1a08SEric Auger         smmu_write_cmdq_err(s, cmd_error);
1032dadd1a08SEric Auger         smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_CMDQ_ERR_MASK);
1033dadd1a08SEric Auger     }
1034dadd1a08SEric Auger 
1035dadd1a08SEric Auger     trace_smmuv3_cmdq_consume_out(Q_PROD(q), Q_CONS(q),
1036dadd1a08SEric Auger                                   Q_PROD_WRAP(q), Q_CONS_WRAP(q));
1037dadd1a08SEric Auger 
1038dadd1a08SEric Auger     return 0;
1039dadd1a08SEric Auger }
1040dadd1a08SEric Auger 
1041fae4be38SEric Auger static MemTxResult smmu_writell(SMMUv3State *s, hwaddr offset,
1042fae4be38SEric Auger                                uint64_t data, MemTxAttrs attrs)
1043fae4be38SEric Auger {
1044fae4be38SEric Auger     switch (offset) {
1045fae4be38SEric Auger     case A_GERROR_IRQ_CFG0:
1046fae4be38SEric Auger         s->gerror_irq_cfg0 = data;
1047fae4be38SEric Auger         return MEMTX_OK;
1048fae4be38SEric Auger     case A_STRTAB_BASE:
1049fae4be38SEric Auger         s->strtab_base = data;
1050fae4be38SEric Auger         return MEMTX_OK;
1051fae4be38SEric Auger     case A_CMDQ_BASE:
1052fae4be38SEric Auger         s->cmdq.base = data;
1053fae4be38SEric Auger         s->cmdq.log2size = extract64(s->cmdq.base, 0, 5);
1054fae4be38SEric Auger         if (s->cmdq.log2size > SMMU_CMDQS) {
1055fae4be38SEric Auger             s->cmdq.log2size = SMMU_CMDQS;
1056fae4be38SEric Auger         }
1057fae4be38SEric Auger         return MEMTX_OK;
1058fae4be38SEric Auger     case A_EVENTQ_BASE:
1059fae4be38SEric Auger         s->eventq.base = data;
1060fae4be38SEric Auger         s->eventq.log2size = extract64(s->eventq.base, 0, 5);
1061fae4be38SEric Auger         if (s->eventq.log2size > SMMU_EVENTQS) {
1062fae4be38SEric Auger             s->eventq.log2size = SMMU_EVENTQS;
1063fae4be38SEric Auger         }
1064fae4be38SEric Auger         return MEMTX_OK;
1065fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG0:
1066fae4be38SEric Auger         s->eventq_irq_cfg0 = data;
1067fae4be38SEric Auger         return MEMTX_OK;
1068fae4be38SEric Auger     default:
1069fae4be38SEric Auger         qemu_log_mask(LOG_UNIMP,
1070fae4be38SEric Auger                       "%s Unexpected 64-bit access to 0x%"PRIx64" (WI)\n",
1071fae4be38SEric Auger                       __func__, offset);
1072fae4be38SEric Auger         return MEMTX_OK;
1073fae4be38SEric Auger     }
1074fae4be38SEric Auger }
1075fae4be38SEric Auger 
1076fae4be38SEric Auger static MemTxResult smmu_writel(SMMUv3State *s, hwaddr offset,
1077fae4be38SEric Auger                                uint64_t data, MemTxAttrs attrs)
1078fae4be38SEric Auger {
1079fae4be38SEric Auger     switch (offset) {
1080fae4be38SEric Auger     case A_CR0:
1081fae4be38SEric Auger         s->cr[0] = data;
1082fae4be38SEric Auger         s->cr0ack = data & ~SMMU_CR0_RESERVED;
1083fae4be38SEric Auger         /* in case the command queue has been enabled */
1084fae4be38SEric Auger         smmuv3_cmdq_consume(s);
1085fae4be38SEric Auger         return MEMTX_OK;
1086fae4be38SEric Auger     case A_CR1:
1087fae4be38SEric Auger         s->cr[1] = data;
1088fae4be38SEric Auger         return MEMTX_OK;
1089fae4be38SEric Auger     case A_CR2:
1090fae4be38SEric Auger         s->cr[2] = data;
1091fae4be38SEric Auger         return MEMTX_OK;
1092fae4be38SEric Auger     case A_IRQ_CTRL:
1093fae4be38SEric Auger         s->irq_ctrl = data;
1094fae4be38SEric Auger         return MEMTX_OK;
1095fae4be38SEric Auger     case A_GERRORN:
1096fae4be38SEric Auger         smmuv3_write_gerrorn(s, data);
1097fae4be38SEric Auger         /*
1098fae4be38SEric Auger          * By acknowledging the CMDQ_ERR, SW may notify cmds can
1099fae4be38SEric Auger          * be processed again
1100fae4be38SEric Auger          */
1101fae4be38SEric Auger         smmuv3_cmdq_consume(s);
1102fae4be38SEric Auger         return MEMTX_OK;
1103fae4be38SEric Auger     case A_GERROR_IRQ_CFG0: /* 64b */
1104fae4be38SEric Auger         s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 0, 32, data);
1105fae4be38SEric Auger         return MEMTX_OK;
1106fae4be38SEric Auger     case A_GERROR_IRQ_CFG0 + 4:
1107fae4be38SEric Auger         s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 32, 32, data);
1108fae4be38SEric Auger         return MEMTX_OK;
1109fae4be38SEric Auger     case A_GERROR_IRQ_CFG1:
1110fae4be38SEric Auger         s->gerror_irq_cfg1 = data;
1111fae4be38SEric Auger         return MEMTX_OK;
1112fae4be38SEric Auger     case A_GERROR_IRQ_CFG2:
1113fae4be38SEric Auger         s->gerror_irq_cfg2 = data;
1114fae4be38SEric Auger         return MEMTX_OK;
1115fae4be38SEric Auger     case A_STRTAB_BASE: /* 64b */
1116fae4be38SEric Auger         s->strtab_base = deposit64(s->strtab_base, 0, 32, data);
1117fae4be38SEric Auger         return MEMTX_OK;
1118fae4be38SEric Auger     case A_STRTAB_BASE + 4:
1119fae4be38SEric Auger         s->strtab_base = deposit64(s->strtab_base, 32, 32, data);
1120fae4be38SEric Auger         return MEMTX_OK;
1121fae4be38SEric Auger     case A_STRTAB_BASE_CFG:
1122fae4be38SEric Auger         s->strtab_base_cfg = data;
1123fae4be38SEric Auger         if (FIELD_EX32(data, STRTAB_BASE_CFG, FMT) == 1) {
1124fae4be38SEric Auger             s->sid_split = FIELD_EX32(data, STRTAB_BASE_CFG, SPLIT);
1125fae4be38SEric Auger             s->features |= SMMU_FEATURE_2LVL_STE;
1126fae4be38SEric Auger         }
1127fae4be38SEric Auger         return MEMTX_OK;
1128fae4be38SEric Auger     case A_CMDQ_BASE: /* 64b */
1129fae4be38SEric Auger         s->cmdq.base = deposit64(s->cmdq.base, 0, 32, data);
1130fae4be38SEric Auger         s->cmdq.log2size = extract64(s->cmdq.base, 0, 5);
1131fae4be38SEric Auger         if (s->cmdq.log2size > SMMU_CMDQS) {
1132fae4be38SEric Auger             s->cmdq.log2size = SMMU_CMDQS;
1133fae4be38SEric Auger         }
1134fae4be38SEric Auger         return MEMTX_OK;
1135fae4be38SEric Auger     case A_CMDQ_BASE + 4: /* 64b */
1136fae4be38SEric Auger         s->cmdq.base = deposit64(s->cmdq.base, 32, 32, data);
1137fae4be38SEric Auger         return MEMTX_OK;
1138fae4be38SEric Auger     case A_CMDQ_PROD:
1139fae4be38SEric Auger         s->cmdq.prod = data;
1140fae4be38SEric Auger         smmuv3_cmdq_consume(s);
1141fae4be38SEric Auger         return MEMTX_OK;
1142fae4be38SEric Auger     case A_CMDQ_CONS:
1143fae4be38SEric Auger         s->cmdq.cons = data;
1144fae4be38SEric Auger         return MEMTX_OK;
1145fae4be38SEric Auger     case A_EVENTQ_BASE: /* 64b */
1146fae4be38SEric Auger         s->eventq.base = deposit64(s->eventq.base, 0, 32, data);
1147fae4be38SEric Auger         s->eventq.log2size = extract64(s->eventq.base, 0, 5);
1148fae4be38SEric Auger         if (s->eventq.log2size > SMMU_EVENTQS) {
1149fae4be38SEric Auger             s->eventq.log2size = SMMU_EVENTQS;
1150fae4be38SEric Auger         }
1151fae4be38SEric Auger         return MEMTX_OK;
1152fae4be38SEric Auger     case A_EVENTQ_BASE + 4:
1153fae4be38SEric Auger         s->eventq.base = deposit64(s->eventq.base, 32, 32, data);
1154fae4be38SEric Auger         return MEMTX_OK;
1155fae4be38SEric Auger     case A_EVENTQ_PROD:
1156fae4be38SEric Auger         s->eventq.prod = data;
1157fae4be38SEric Auger         return MEMTX_OK;
1158fae4be38SEric Auger     case A_EVENTQ_CONS:
1159fae4be38SEric Auger         s->eventq.cons = data;
1160fae4be38SEric Auger         return MEMTX_OK;
1161fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG0: /* 64b */
1162fae4be38SEric Auger         s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 0, 32, data);
1163fae4be38SEric Auger         return MEMTX_OK;
1164fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG0 + 4:
1165fae4be38SEric Auger         s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 32, 32, data);
1166fae4be38SEric Auger         return MEMTX_OK;
1167fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG1:
1168fae4be38SEric Auger         s->eventq_irq_cfg1 = data;
1169fae4be38SEric Auger         return MEMTX_OK;
1170fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG2:
1171fae4be38SEric Auger         s->eventq_irq_cfg2 = data;
1172fae4be38SEric Auger         return MEMTX_OK;
1173fae4be38SEric Auger     default:
1174fae4be38SEric Auger         qemu_log_mask(LOG_UNIMP,
1175fae4be38SEric Auger                       "%s Unexpected 32-bit access to 0x%"PRIx64" (WI)\n",
1176fae4be38SEric Auger                       __func__, offset);
1177fae4be38SEric Auger         return MEMTX_OK;
1178fae4be38SEric Auger     }
1179fae4be38SEric Auger }
1180fae4be38SEric Auger 
118110a83cb9SPrem Mallappa static MemTxResult smmu_write_mmio(void *opaque, hwaddr offset, uint64_t data,
118210a83cb9SPrem Mallappa                                    unsigned size, MemTxAttrs attrs)
118310a83cb9SPrem Mallappa {
1184fae4be38SEric Auger     SMMUState *sys = opaque;
1185fae4be38SEric Auger     SMMUv3State *s = ARM_SMMUV3(sys);
1186fae4be38SEric Auger     MemTxResult r;
1187fae4be38SEric Auger 
1188fae4be38SEric Auger     /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */
1189fae4be38SEric Auger     offset &= ~0x10000;
1190fae4be38SEric Auger 
1191fae4be38SEric Auger     switch (size) {
1192fae4be38SEric Auger     case 8:
1193fae4be38SEric Auger         r = smmu_writell(s, offset, data, attrs);
1194fae4be38SEric Auger         break;
1195fae4be38SEric Auger     case 4:
1196fae4be38SEric Auger         r = smmu_writel(s, offset, data, attrs);
1197fae4be38SEric Auger         break;
1198fae4be38SEric Auger     default:
1199fae4be38SEric Auger         r = MEMTX_ERROR;
1200fae4be38SEric Auger         break;
1201fae4be38SEric Auger     }
1202fae4be38SEric Auger 
1203fae4be38SEric Auger     trace_smmuv3_write_mmio(offset, data, size, r);
1204fae4be38SEric Auger     return r;
120510a83cb9SPrem Mallappa }
120610a83cb9SPrem Mallappa 
120710a83cb9SPrem Mallappa static MemTxResult smmu_readll(SMMUv3State *s, hwaddr offset,
120810a83cb9SPrem Mallappa                                uint64_t *data, MemTxAttrs attrs)
120910a83cb9SPrem Mallappa {
121010a83cb9SPrem Mallappa     switch (offset) {
121110a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG0:
121210a83cb9SPrem Mallappa         *data = s->gerror_irq_cfg0;
121310a83cb9SPrem Mallappa         return MEMTX_OK;
121410a83cb9SPrem Mallappa     case A_STRTAB_BASE:
121510a83cb9SPrem Mallappa         *data = s->strtab_base;
121610a83cb9SPrem Mallappa         return MEMTX_OK;
121710a83cb9SPrem Mallappa     case A_CMDQ_BASE:
121810a83cb9SPrem Mallappa         *data = s->cmdq.base;
121910a83cb9SPrem Mallappa         return MEMTX_OK;
122010a83cb9SPrem Mallappa     case A_EVENTQ_BASE:
122110a83cb9SPrem Mallappa         *data = s->eventq.base;
122210a83cb9SPrem Mallappa         return MEMTX_OK;
122310a83cb9SPrem Mallappa     default:
122410a83cb9SPrem Mallappa         *data = 0;
122510a83cb9SPrem Mallappa         qemu_log_mask(LOG_UNIMP,
122610a83cb9SPrem Mallappa                       "%s Unexpected 64-bit access to 0x%"PRIx64" (RAZ)\n",
122710a83cb9SPrem Mallappa                       __func__, offset);
122810a83cb9SPrem Mallappa         return MEMTX_OK;
122910a83cb9SPrem Mallappa     }
123010a83cb9SPrem Mallappa }
123110a83cb9SPrem Mallappa 
123210a83cb9SPrem Mallappa static MemTxResult smmu_readl(SMMUv3State *s, hwaddr offset,
123310a83cb9SPrem Mallappa                               uint64_t *data, MemTxAttrs attrs)
123410a83cb9SPrem Mallappa {
123510a83cb9SPrem Mallappa     switch (offset) {
123697fb318dSPeter Maydell     case A_IDREGS ... A_IDREGS + 0x2f:
123710a83cb9SPrem Mallappa         *data = smmuv3_idreg(offset - A_IDREGS);
123810a83cb9SPrem Mallappa         return MEMTX_OK;
123910a83cb9SPrem Mallappa     case A_IDR0 ... A_IDR5:
124010a83cb9SPrem Mallappa         *data = s->idr[(offset - A_IDR0) / 4];
124110a83cb9SPrem Mallappa         return MEMTX_OK;
124210a83cb9SPrem Mallappa     case A_IIDR:
124310a83cb9SPrem Mallappa         *data = s->iidr;
124410a83cb9SPrem Mallappa         return MEMTX_OK;
124510a83cb9SPrem Mallappa     case A_CR0:
124610a83cb9SPrem Mallappa         *data = s->cr[0];
124710a83cb9SPrem Mallappa         return MEMTX_OK;
124810a83cb9SPrem Mallappa     case A_CR0ACK:
124910a83cb9SPrem Mallappa         *data = s->cr0ack;
125010a83cb9SPrem Mallappa         return MEMTX_OK;
125110a83cb9SPrem Mallappa     case A_CR1:
125210a83cb9SPrem Mallappa         *data = s->cr[1];
125310a83cb9SPrem Mallappa         return MEMTX_OK;
125410a83cb9SPrem Mallappa     case A_CR2:
125510a83cb9SPrem Mallappa         *data = s->cr[2];
125610a83cb9SPrem Mallappa         return MEMTX_OK;
125710a83cb9SPrem Mallappa     case A_STATUSR:
125810a83cb9SPrem Mallappa         *data = s->statusr;
125910a83cb9SPrem Mallappa         return MEMTX_OK;
126010a83cb9SPrem Mallappa     case A_IRQ_CTRL:
126110a83cb9SPrem Mallappa     case A_IRQ_CTRL_ACK:
126210a83cb9SPrem Mallappa         *data = s->irq_ctrl;
126310a83cb9SPrem Mallappa         return MEMTX_OK;
126410a83cb9SPrem Mallappa     case A_GERROR:
126510a83cb9SPrem Mallappa         *data = s->gerror;
126610a83cb9SPrem Mallappa         return MEMTX_OK;
126710a83cb9SPrem Mallappa     case A_GERRORN:
126810a83cb9SPrem Mallappa         *data = s->gerrorn;
126910a83cb9SPrem Mallappa         return MEMTX_OK;
127010a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG0: /* 64b */
127110a83cb9SPrem Mallappa         *data = extract64(s->gerror_irq_cfg0, 0, 32);
127210a83cb9SPrem Mallappa         return MEMTX_OK;
127310a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG0 + 4:
127410a83cb9SPrem Mallappa         *data = extract64(s->gerror_irq_cfg0, 32, 32);
127510a83cb9SPrem Mallappa         return MEMTX_OK;
127610a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG1:
127710a83cb9SPrem Mallappa         *data = s->gerror_irq_cfg1;
127810a83cb9SPrem Mallappa         return MEMTX_OK;
127910a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG2:
128010a83cb9SPrem Mallappa         *data = s->gerror_irq_cfg2;
128110a83cb9SPrem Mallappa         return MEMTX_OK;
128210a83cb9SPrem Mallappa     case A_STRTAB_BASE: /* 64b */
128310a83cb9SPrem Mallappa         *data = extract64(s->strtab_base, 0, 32);
128410a83cb9SPrem Mallappa         return MEMTX_OK;
128510a83cb9SPrem Mallappa     case A_STRTAB_BASE + 4: /* 64b */
128610a83cb9SPrem Mallappa         *data = extract64(s->strtab_base, 32, 32);
128710a83cb9SPrem Mallappa         return MEMTX_OK;
128810a83cb9SPrem Mallappa     case A_STRTAB_BASE_CFG:
128910a83cb9SPrem Mallappa         *data = s->strtab_base_cfg;
129010a83cb9SPrem Mallappa         return MEMTX_OK;
129110a83cb9SPrem Mallappa     case A_CMDQ_BASE: /* 64b */
129210a83cb9SPrem Mallappa         *data = extract64(s->cmdq.base, 0, 32);
129310a83cb9SPrem Mallappa         return MEMTX_OK;
129410a83cb9SPrem Mallappa     case A_CMDQ_BASE + 4:
129510a83cb9SPrem Mallappa         *data = extract64(s->cmdq.base, 32, 32);
129610a83cb9SPrem Mallappa         return MEMTX_OK;
129710a83cb9SPrem Mallappa     case A_CMDQ_PROD:
129810a83cb9SPrem Mallappa         *data = s->cmdq.prod;
129910a83cb9SPrem Mallappa         return MEMTX_OK;
130010a83cb9SPrem Mallappa     case A_CMDQ_CONS:
130110a83cb9SPrem Mallappa         *data = s->cmdq.cons;
130210a83cb9SPrem Mallappa         return MEMTX_OK;
130310a83cb9SPrem Mallappa     case A_EVENTQ_BASE: /* 64b */
130410a83cb9SPrem Mallappa         *data = extract64(s->eventq.base, 0, 32);
130510a83cb9SPrem Mallappa         return MEMTX_OK;
130610a83cb9SPrem Mallappa     case A_EVENTQ_BASE + 4: /* 64b */
130710a83cb9SPrem Mallappa         *data = extract64(s->eventq.base, 32, 32);
130810a83cb9SPrem Mallappa         return MEMTX_OK;
130910a83cb9SPrem Mallappa     case A_EVENTQ_PROD:
131010a83cb9SPrem Mallappa         *data = s->eventq.prod;
131110a83cb9SPrem Mallappa         return MEMTX_OK;
131210a83cb9SPrem Mallappa     case A_EVENTQ_CONS:
131310a83cb9SPrem Mallappa         *data = s->eventq.cons;
131410a83cb9SPrem Mallappa         return MEMTX_OK;
131510a83cb9SPrem Mallappa     default:
131610a83cb9SPrem Mallappa         *data = 0;
131710a83cb9SPrem Mallappa         qemu_log_mask(LOG_UNIMP,
131810a83cb9SPrem Mallappa                       "%s unhandled 32-bit access at 0x%"PRIx64" (RAZ)\n",
131910a83cb9SPrem Mallappa                       __func__, offset);
132010a83cb9SPrem Mallappa         return MEMTX_OK;
132110a83cb9SPrem Mallappa     }
132210a83cb9SPrem Mallappa }
132310a83cb9SPrem Mallappa 
132410a83cb9SPrem Mallappa static MemTxResult smmu_read_mmio(void *opaque, hwaddr offset, uint64_t *data,
132510a83cb9SPrem Mallappa                                   unsigned size, MemTxAttrs attrs)
132610a83cb9SPrem Mallappa {
132710a83cb9SPrem Mallappa     SMMUState *sys = opaque;
132810a83cb9SPrem Mallappa     SMMUv3State *s = ARM_SMMUV3(sys);
132910a83cb9SPrem Mallappa     MemTxResult r;
133010a83cb9SPrem Mallappa 
133110a83cb9SPrem Mallappa     /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */
133210a83cb9SPrem Mallappa     offset &= ~0x10000;
133310a83cb9SPrem Mallappa 
133410a83cb9SPrem Mallappa     switch (size) {
133510a83cb9SPrem Mallappa     case 8:
133610a83cb9SPrem Mallappa         r = smmu_readll(s, offset, data, attrs);
133710a83cb9SPrem Mallappa         break;
133810a83cb9SPrem Mallappa     case 4:
133910a83cb9SPrem Mallappa         r = smmu_readl(s, offset, data, attrs);
134010a83cb9SPrem Mallappa         break;
134110a83cb9SPrem Mallappa     default:
134210a83cb9SPrem Mallappa         r = MEMTX_ERROR;
134310a83cb9SPrem Mallappa         break;
134410a83cb9SPrem Mallappa     }
134510a83cb9SPrem Mallappa 
134610a83cb9SPrem Mallappa     trace_smmuv3_read_mmio(offset, *data, size, r);
134710a83cb9SPrem Mallappa     return r;
134810a83cb9SPrem Mallappa }
134910a83cb9SPrem Mallappa 
135010a83cb9SPrem Mallappa static const MemoryRegionOps smmu_mem_ops = {
135110a83cb9SPrem Mallappa     .read_with_attrs = smmu_read_mmio,
135210a83cb9SPrem Mallappa     .write_with_attrs = smmu_write_mmio,
135310a83cb9SPrem Mallappa     .endianness = DEVICE_LITTLE_ENDIAN,
135410a83cb9SPrem Mallappa     .valid = {
135510a83cb9SPrem Mallappa         .min_access_size = 4,
135610a83cb9SPrem Mallappa         .max_access_size = 8,
135710a83cb9SPrem Mallappa     },
135810a83cb9SPrem Mallappa     .impl = {
135910a83cb9SPrem Mallappa         .min_access_size = 4,
136010a83cb9SPrem Mallappa         .max_access_size = 8,
136110a83cb9SPrem Mallappa     },
136210a83cb9SPrem Mallappa };
136310a83cb9SPrem Mallappa 
136410a83cb9SPrem Mallappa static void smmu_init_irq(SMMUv3State *s, SysBusDevice *dev)
136510a83cb9SPrem Mallappa {
136610a83cb9SPrem Mallappa     int i;
136710a83cb9SPrem Mallappa 
136810a83cb9SPrem Mallappa     for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
136910a83cb9SPrem Mallappa         sysbus_init_irq(dev, &s->irq[i]);
137010a83cb9SPrem Mallappa     }
137110a83cb9SPrem Mallappa }
137210a83cb9SPrem Mallappa 
137310a83cb9SPrem Mallappa static void smmu_reset(DeviceState *dev)
137410a83cb9SPrem Mallappa {
137510a83cb9SPrem Mallappa     SMMUv3State *s = ARM_SMMUV3(dev);
137610a83cb9SPrem Mallappa     SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s);
137710a83cb9SPrem Mallappa 
137810a83cb9SPrem Mallappa     c->parent_reset(dev);
137910a83cb9SPrem Mallappa 
138010a83cb9SPrem Mallappa     smmuv3_init_regs(s);
138110a83cb9SPrem Mallappa }
138210a83cb9SPrem Mallappa 
138310a83cb9SPrem Mallappa static void smmu_realize(DeviceState *d, Error **errp)
138410a83cb9SPrem Mallappa {
138510a83cb9SPrem Mallappa     SMMUState *sys = ARM_SMMU(d);
138610a83cb9SPrem Mallappa     SMMUv3State *s = ARM_SMMUV3(sys);
138710a83cb9SPrem Mallappa     SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s);
138810a83cb9SPrem Mallappa     SysBusDevice *dev = SYS_BUS_DEVICE(d);
138910a83cb9SPrem Mallappa     Error *local_err = NULL;
139010a83cb9SPrem Mallappa 
139110a83cb9SPrem Mallappa     c->parent_realize(d, &local_err);
139210a83cb9SPrem Mallappa     if (local_err) {
139310a83cb9SPrem Mallappa         error_propagate(errp, local_err);
139410a83cb9SPrem Mallappa         return;
139510a83cb9SPrem Mallappa     }
139610a83cb9SPrem Mallappa 
139732cfd7f3SEric Auger     qemu_mutex_init(&s->mutex);
139832cfd7f3SEric Auger 
139910a83cb9SPrem Mallappa     memory_region_init_io(&sys->iomem, OBJECT(s),
140010a83cb9SPrem Mallappa                           &smmu_mem_ops, sys, TYPE_ARM_SMMUV3, 0x20000);
140110a83cb9SPrem Mallappa 
140210a83cb9SPrem Mallappa     sys->mrtypename = TYPE_SMMUV3_IOMMU_MEMORY_REGION;
140310a83cb9SPrem Mallappa 
140410a83cb9SPrem Mallappa     sysbus_init_mmio(dev, &sys->iomem);
140510a83cb9SPrem Mallappa 
140610a83cb9SPrem Mallappa     smmu_init_irq(s, dev);
140710a83cb9SPrem Mallappa }
140810a83cb9SPrem Mallappa 
140910a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3_queue = {
141010a83cb9SPrem Mallappa     .name = "smmuv3_queue",
141110a83cb9SPrem Mallappa     .version_id = 1,
141210a83cb9SPrem Mallappa     .minimum_version_id = 1,
141310a83cb9SPrem Mallappa     .fields = (VMStateField[]) {
141410a83cb9SPrem Mallappa         VMSTATE_UINT64(base, SMMUQueue),
141510a83cb9SPrem Mallappa         VMSTATE_UINT32(prod, SMMUQueue),
141610a83cb9SPrem Mallappa         VMSTATE_UINT32(cons, SMMUQueue),
141710a83cb9SPrem Mallappa         VMSTATE_UINT8(log2size, SMMUQueue),
1418758b71f7SDr. David Alan Gilbert         VMSTATE_END_OF_LIST(),
141910a83cb9SPrem Mallappa     },
142010a83cb9SPrem Mallappa };
142110a83cb9SPrem Mallappa 
142210a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3 = {
142310a83cb9SPrem Mallappa     .name = "smmuv3",
142410a83cb9SPrem Mallappa     .version_id = 1,
142510a83cb9SPrem Mallappa     .minimum_version_id = 1,
142610a83cb9SPrem Mallappa     .fields = (VMStateField[]) {
142710a83cb9SPrem Mallappa         VMSTATE_UINT32(features, SMMUv3State),
142810a83cb9SPrem Mallappa         VMSTATE_UINT8(sid_size, SMMUv3State),
142910a83cb9SPrem Mallappa         VMSTATE_UINT8(sid_split, SMMUv3State),
143010a83cb9SPrem Mallappa 
143110a83cb9SPrem Mallappa         VMSTATE_UINT32_ARRAY(cr, SMMUv3State, 3),
143210a83cb9SPrem Mallappa         VMSTATE_UINT32(cr0ack, SMMUv3State),
143310a83cb9SPrem Mallappa         VMSTATE_UINT32(statusr, SMMUv3State),
143410a83cb9SPrem Mallappa         VMSTATE_UINT32(irq_ctrl, SMMUv3State),
143510a83cb9SPrem Mallappa         VMSTATE_UINT32(gerror, SMMUv3State),
143610a83cb9SPrem Mallappa         VMSTATE_UINT32(gerrorn, SMMUv3State),
143710a83cb9SPrem Mallappa         VMSTATE_UINT64(gerror_irq_cfg0, SMMUv3State),
143810a83cb9SPrem Mallappa         VMSTATE_UINT32(gerror_irq_cfg1, SMMUv3State),
143910a83cb9SPrem Mallappa         VMSTATE_UINT32(gerror_irq_cfg2, SMMUv3State),
144010a83cb9SPrem Mallappa         VMSTATE_UINT64(strtab_base, SMMUv3State),
144110a83cb9SPrem Mallappa         VMSTATE_UINT32(strtab_base_cfg, SMMUv3State),
144210a83cb9SPrem Mallappa         VMSTATE_UINT64(eventq_irq_cfg0, SMMUv3State),
144310a83cb9SPrem Mallappa         VMSTATE_UINT32(eventq_irq_cfg1, SMMUv3State),
144410a83cb9SPrem Mallappa         VMSTATE_UINT32(eventq_irq_cfg2, SMMUv3State),
144510a83cb9SPrem Mallappa 
144610a83cb9SPrem Mallappa         VMSTATE_STRUCT(cmdq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue),
144710a83cb9SPrem Mallappa         VMSTATE_STRUCT(eventq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue),
144810a83cb9SPrem Mallappa 
144910a83cb9SPrem Mallappa         VMSTATE_END_OF_LIST(),
145010a83cb9SPrem Mallappa     },
145110a83cb9SPrem Mallappa };
145210a83cb9SPrem Mallappa 
145310a83cb9SPrem Mallappa static void smmuv3_instance_init(Object *obj)
145410a83cb9SPrem Mallappa {
145510a83cb9SPrem Mallappa     /* Nothing much to do here as of now */
145610a83cb9SPrem Mallappa }
145710a83cb9SPrem Mallappa 
145810a83cb9SPrem Mallappa static void smmuv3_class_init(ObjectClass *klass, void *data)
145910a83cb9SPrem Mallappa {
146010a83cb9SPrem Mallappa     DeviceClass *dc = DEVICE_CLASS(klass);
146110a83cb9SPrem Mallappa     SMMUv3Class *c = ARM_SMMUV3_CLASS(klass);
146210a83cb9SPrem Mallappa 
146310a83cb9SPrem Mallappa     dc->vmsd = &vmstate_smmuv3;
146410a83cb9SPrem Mallappa     device_class_set_parent_reset(dc, smmu_reset, &c->parent_reset);
146510a83cb9SPrem Mallappa     c->parent_realize = dc->realize;
146610a83cb9SPrem Mallappa     dc->realize = smmu_realize;
146710a83cb9SPrem Mallappa }
146810a83cb9SPrem Mallappa 
14690d1ac82eSEric Auger static void smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu,
14700d1ac82eSEric Auger                                        IOMMUNotifierFlag old,
14710d1ac82eSEric Auger                                        IOMMUNotifierFlag new)
14720d1ac82eSEric Auger {
1473832e4222SEric Auger     SMMUDevice *sdev = container_of(iommu, SMMUDevice, iommu);
1474832e4222SEric Auger     SMMUv3State *s3 = sdev->smmu;
1475832e4222SEric Auger     SMMUState *s = &(s3->smmu_state);
1476832e4222SEric Auger 
1477832e4222SEric Auger     if (new & IOMMU_NOTIFIER_MAP) {
1478832e4222SEric Auger         int bus_num = pci_bus_num(sdev->bus);
1479832e4222SEric Auger         PCIDevice *pcidev = pci_find_device(sdev->bus, bus_num, sdev->devfn);
1480832e4222SEric Auger 
1481832e4222SEric Auger         warn_report("SMMUv3 does not support notification on MAP: "
1482832e4222SEric Auger                      "device %s will not function properly", pcidev->name);
1483832e4222SEric Auger     }
1484832e4222SEric Auger 
14850d1ac82eSEric Auger     if (old == IOMMU_NOTIFIER_NONE) {
1486832e4222SEric Auger         trace_smmuv3_notify_flag_add(iommu->parent_obj.name);
1487c6370441SEric Auger         QLIST_INSERT_HEAD(&s->devices_with_notifiers, sdev, next);
1488c6370441SEric Auger     } else if (new == IOMMU_NOTIFIER_NONE) {
1489832e4222SEric Auger         trace_smmuv3_notify_flag_del(iommu->parent_obj.name);
1490c6370441SEric Auger         QLIST_REMOVE(sdev, next);
14910d1ac82eSEric Auger     }
14920d1ac82eSEric Auger }
14930d1ac82eSEric Auger 
149410a83cb9SPrem Mallappa static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass,
149510a83cb9SPrem Mallappa                                                   void *data)
149610a83cb9SPrem Mallappa {
14979bde7f06SEric Auger     IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
14989bde7f06SEric Auger 
14999bde7f06SEric Auger     imrc->translate = smmuv3_translate;
15000d1ac82eSEric Auger     imrc->notify_flag_changed = smmuv3_notify_flag_changed;
150110a83cb9SPrem Mallappa }
150210a83cb9SPrem Mallappa 
150310a83cb9SPrem Mallappa static const TypeInfo smmuv3_type_info = {
150410a83cb9SPrem Mallappa     .name          = TYPE_ARM_SMMUV3,
150510a83cb9SPrem Mallappa     .parent        = TYPE_ARM_SMMU,
150610a83cb9SPrem Mallappa     .instance_size = sizeof(SMMUv3State),
150710a83cb9SPrem Mallappa     .instance_init = smmuv3_instance_init,
150810a83cb9SPrem Mallappa     .class_size    = sizeof(SMMUv3Class),
150910a83cb9SPrem Mallappa     .class_init    = smmuv3_class_init,
151010a83cb9SPrem Mallappa };
151110a83cb9SPrem Mallappa 
151210a83cb9SPrem Mallappa static const TypeInfo smmuv3_iommu_memory_region_info = {
151310a83cb9SPrem Mallappa     .parent = TYPE_IOMMU_MEMORY_REGION,
151410a83cb9SPrem Mallappa     .name = TYPE_SMMUV3_IOMMU_MEMORY_REGION,
151510a83cb9SPrem Mallappa     .class_init = smmuv3_iommu_memory_region_class_init,
151610a83cb9SPrem Mallappa };
151710a83cb9SPrem Mallappa 
151810a83cb9SPrem Mallappa static void smmuv3_register_types(void)
151910a83cb9SPrem Mallappa {
152010a83cb9SPrem Mallappa     type_register(&smmuv3_type_info);
152110a83cb9SPrem Mallappa     type_register(&smmuv3_iommu_memory_region_info);
152210a83cb9SPrem Mallappa }
152310a83cb9SPrem Mallappa 
152410a83cb9SPrem Mallappa type_init(smmuv3_register_types)
152510a83cb9SPrem Mallappa 
1526