xref: /qemu/hw/arm/smmuv3.c (revision 2c91bcf273ffb95898d2ca901b699558d9e73fd1)
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"
2610a83cb9SPrem Mallappa #include "trace.h"
2710a83cb9SPrem Mallappa #include "qemu/log.h"
2810a83cb9SPrem Mallappa #include "qemu/error-report.h"
2910a83cb9SPrem Mallappa #include "qapi/error.h"
3010a83cb9SPrem Mallappa 
3110a83cb9SPrem Mallappa #include "hw/arm/smmuv3.h"
3210a83cb9SPrem Mallappa #include "smmuv3-internal.h"
3310a83cb9SPrem Mallappa 
346a736033SEric Auger /**
356a736033SEric Auger  * smmuv3_trigger_irq - pulse @irq if enabled and update
366a736033SEric Auger  * GERROR register in case of GERROR interrupt
376a736033SEric Auger  *
386a736033SEric Auger  * @irq: irq type
396a736033SEric Auger  * @gerror_mask: mask of gerrors to toggle (relevant if @irq is GERROR)
406a736033SEric Auger  */
41fae4be38SEric Auger static void smmuv3_trigger_irq(SMMUv3State *s, SMMUIrq irq,
42fae4be38SEric Auger                                uint32_t gerror_mask)
436a736033SEric Auger {
446a736033SEric Auger 
456a736033SEric Auger     bool pulse = false;
466a736033SEric Auger 
476a736033SEric Auger     switch (irq) {
486a736033SEric Auger     case SMMU_IRQ_EVTQ:
496a736033SEric Auger         pulse = smmuv3_eventq_irq_enabled(s);
506a736033SEric Auger         break;
516a736033SEric Auger     case SMMU_IRQ_PRIQ:
526a736033SEric Auger         qemu_log_mask(LOG_UNIMP, "PRI not yet supported\n");
536a736033SEric Auger         break;
546a736033SEric Auger     case SMMU_IRQ_CMD_SYNC:
556a736033SEric Auger         pulse = true;
566a736033SEric Auger         break;
576a736033SEric Auger     case SMMU_IRQ_GERROR:
586a736033SEric Auger     {
596a736033SEric Auger         uint32_t pending = s->gerror ^ s->gerrorn;
606a736033SEric Auger         uint32_t new_gerrors = ~pending & gerror_mask;
616a736033SEric Auger 
626a736033SEric Auger         if (!new_gerrors) {
636a736033SEric Auger             /* only toggle non pending errors */
646a736033SEric Auger             return;
656a736033SEric Auger         }
666a736033SEric Auger         s->gerror ^= new_gerrors;
676a736033SEric Auger         trace_smmuv3_write_gerror(new_gerrors, s->gerror);
686a736033SEric Auger 
696a736033SEric Auger         pulse = smmuv3_gerror_irq_enabled(s);
706a736033SEric Auger         break;
716a736033SEric Auger     }
726a736033SEric Auger     }
736a736033SEric Auger     if (pulse) {
746a736033SEric Auger             trace_smmuv3_trigger_irq(irq);
756a736033SEric Auger             qemu_irq_pulse(s->irq[irq]);
766a736033SEric Auger     }
776a736033SEric Auger }
786a736033SEric Auger 
79fae4be38SEric Auger static void smmuv3_write_gerrorn(SMMUv3State *s, uint32_t new_gerrorn)
806a736033SEric Auger {
816a736033SEric Auger     uint32_t pending = s->gerror ^ s->gerrorn;
826a736033SEric Auger     uint32_t toggled = s->gerrorn ^ new_gerrorn;
836a736033SEric Auger 
846a736033SEric Auger     if (toggled & ~pending) {
856a736033SEric Auger         qemu_log_mask(LOG_GUEST_ERROR,
866a736033SEric Auger                       "guest toggles non pending errors = 0x%x\n",
876a736033SEric Auger                       toggled & ~pending);
886a736033SEric Auger     }
896a736033SEric Auger 
906a736033SEric Auger     /*
916a736033SEric Auger      * We do not raise any error in case guest toggles bits corresponding
926a736033SEric Auger      * to not active IRQs (CONSTRAINED UNPREDICTABLE)
936a736033SEric Auger      */
946a736033SEric Auger     s->gerrorn = new_gerrorn;
956a736033SEric Auger 
966a736033SEric Auger     trace_smmuv3_write_gerrorn(toggled & pending, s->gerrorn);
976a736033SEric Auger }
986a736033SEric Auger 
99dadd1a08SEric Auger static inline MemTxResult queue_read(SMMUQueue *q, void *data)
100dadd1a08SEric Auger {
101dadd1a08SEric Auger     dma_addr_t addr = Q_CONS_ENTRY(q);
102dadd1a08SEric Auger 
103dadd1a08SEric Auger     return dma_memory_read(&address_space_memory, addr, data, q->entry_size);
104dadd1a08SEric Auger }
105dadd1a08SEric Auger 
106dadd1a08SEric Auger static MemTxResult queue_write(SMMUQueue *q, void *data)
107dadd1a08SEric Auger {
108dadd1a08SEric Auger     dma_addr_t addr = Q_PROD_ENTRY(q);
109dadd1a08SEric Auger     MemTxResult ret;
110dadd1a08SEric Auger 
111dadd1a08SEric Auger     ret = dma_memory_write(&address_space_memory, addr, data, q->entry_size);
112dadd1a08SEric Auger     if (ret != MEMTX_OK) {
113dadd1a08SEric Auger         return ret;
114dadd1a08SEric Auger     }
115dadd1a08SEric Auger 
116dadd1a08SEric Auger     queue_prod_incr(q);
117dadd1a08SEric Auger     return MEMTX_OK;
118dadd1a08SEric Auger }
119dadd1a08SEric Auger 
120bb981004SEric Auger static MemTxResult smmuv3_write_eventq(SMMUv3State *s, Evt *evt)
121dadd1a08SEric Auger {
122dadd1a08SEric Auger     SMMUQueue *q = &s->eventq;
123bb981004SEric Auger     MemTxResult r;
124bb981004SEric Auger 
125bb981004SEric Auger     if (!smmuv3_eventq_enabled(s)) {
126bb981004SEric Auger         return MEMTX_ERROR;
127bb981004SEric Auger     }
128bb981004SEric Auger 
129bb981004SEric Auger     if (smmuv3_q_full(q)) {
130bb981004SEric Auger         return MEMTX_ERROR;
131bb981004SEric Auger     }
132bb981004SEric Auger 
133bb981004SEric Auger     r = queue_write(q, evt);
134bb981004SEric Auger     if (r != MEMTX_OK) {
135bb981004SEric Auger         return r;
136bb981004SEric Auger     }
137bb981004SEric Auger 
138bb981004SEric Auger     if (smmuv3_q_empty(q)) {
139bb981004SEric Auger         smmuv3_trigger_irq(s, SMMU_IRQ_EVTQ, 0);
140bb981004SEric Auger     }
141bb981004SEric Auger     return MEMTX_OK;
142bb981004SEric Auger }
143bb981004SEric Auger 
144bb981004SEric Auger void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info)
145bb981004SEric Auger {
14624af32e0SEric Auger     Evt evt = {};
147bb981004SEric Auger     MemTxResult r;
148dadd1a08SEric Auger 
149dadd1a08SEric Auger     if (!smmuv3_eventq_enabled(s)) {
150dadd1a08SEric Auger         return;
151dadd1a08SEric Auger     }
152dadd1a08SEric Auger 
153bb981004SEric Auger     EVT_SET_TYPE(&evt, info->type);
154bb981004SEric Auger     EVT_SET_SID(&evt, info->sid);
155bb981004SEric Auger 
156bb981004SEric Auger     switch (info->type) {
157bb981004SEric Auger     case SMMU_EVT_OK:
158dadd1a08SEric Auger         return;
159bb981004SEric Auger     case SMMU_EVT_F_UUT:
160bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_uut.ssid);
161bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.f_uut.ssv);
162bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_uut.addr);
163bb981004SEric Auger         EVT_SET_RNW(&evt,  info->u.f_uut.rnw);
164bb981004SEric Auger         EVT_SET_PNU(&evt,  info->u.f_uut.pnu);
165bb981004SEric Auger         EVT_SET_IND(&evt,  info->u.f_uut.ind);
166bb981004SEric Auger         break;
167bb981004SEric Auger     case SMMU_EVT_C_BAD_STREAMID:
168bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.c_bad_streamid.ssid);
169bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.c_bad_streamid.ssv);
170bb981004SEric Auger         break;
171bb981004SEric Auger     case SMMU_EVT_F_STE_FETCH:
172bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_ste_fetch.ssid);
173bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.f_ste_fetch.ssv);
174bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_ste_fetch.addr);
175bb981004SEric Auger         break;
176bb981004SEric Auger     case SMMU_EVT_C_BAD_STE:
177bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.c_bad_ste.ssid);
178bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.c_bad_ste.ssv);
179bb981004SEric Auger         break;
180bb981004SEric Auger     case SMMU_EVT_F_STREAM_DISABLED:
181bb981004SEric Auger         break;
182bb981004SEric Auger     case SMMU_EVT_F_TRANS_FORBIDDEN:
183bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_transl_forbidden.addr);
184bb981004SEric Auger         EVT_SET_RNW(&evt, info->u.f_transl_forbidden.rnw);
185bb981004SEric Auger         break;
186bb981004SEric Auger     case SMMU_EVT_C_BAD_SUBSTREAMID:
187bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.c_bad_substream.ssid);
188bb981004SEric Auger         break;
189bb981004SEric Auger     case SMMU_EVT_F_CD_FETCH:
190bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_cd_fetch.ssid);
191bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.f_cd_fetch.ssv);
192bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_cd_fetch.addr);
193bb981004SEric Auger         break;
194bb981004SEric Auger     case SMMU_EVT_C_BAD_CD:
195bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.c_bad_cd.ssid);
196bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.c_bad_cd.ssv);
197bb981004SEric Auger         break;
198bb981004SEric Auger     case SMMU_EVT_F_WALK_EABT:
199bb981004SEric Auger     case SMMU_EVT_F_TRANSLATION:
200bb981004SEric Auger     case SMMU_EVT_F_ADDR_SIZE:
201bb981004SEric Auger     case SMMU_EVT_F_ACCESS:
202bb981004SEric Auger     case SMMU_EVT_F_PERMISSION:
203bb981004SEric Auger         EVT_SET_STALL(&evt, info->u.f_walk_eabt.stall);
204bb981004SEric Auger         EVT_SET_STAG(&evt, info->u.f_walk_eabt.stag);
205bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_walk_eabt.ssid);
206bb981004SEric Auger         EVT_SET_SSV(&evt, info->u.f_walk_eabt.ssv);
207bb981004SEric Auger         EVT_SET_S2(&evt, info->u.f_walk_eabt.s2);
208bb981004SEric Auger         EVT_SET_ADDR(&evt, info->u.f_walk_eabt.addr);
209bb981004SEric Auger         EVT_SET_RNW(&evt, info->u.f_walk_eabt.rnw);
210bb981004SEric Auger         EVT_SET_PNU(&evt, info->u.f_walk_eabt.pnu);
211bb981004SEric Auger         EVT_SET_IND(&evt, info->u.f_walk_eabt.ind);
212bb981004SEric Auger         EVT_SET_CLASS(&evt, info->u.f_walk_eabt.class);
213bb981004SEric Auger         EVT_SET_ADDR2(&evt, info->u.f_walk_eabt.addr2);
214bb981004SEric Auger         break;
215bb981004SEric Auger     case SMMU_EVT_F_CFG_CONFLICT:
216bb981004SEric Auger         EVT_SET_SSID(&evt, info->u.f_cfg_conflict.ssid);
217bb981004SEric Auger         EVT_SET_SSV(&evt,  info->u.f_cfg_conflict.ssv);
218bb981004SEric Auger         break;
219bb981004SEric Auger     /* rest is not implemented */
220bb981004SEric Auger     case SMMU_EVT_F_BAD_ATS_TREQ:
221bb981004SEric Auger     case SMMU_EVT_F_TLB_CONFLICT:
222bb981004SEric Auger     case SMMU_EVT_E_PAGE_REQ:
223bb981004SEric Auger     default:
224bb981004SEric Auger         g_assert_not_reached();
225dadd1a08SEric Auger     }
226dadd1a08SEric Auger 
227bb981004SEric Auger     trace_smmuv3_record_event(smmu_event_string(info->type), info->sid);
228bb981004SEric Auger     r = smmuv3_write_eventq(s, &evt);
229bb981004SEric Auger     if (r != MEMTX_OK) {
230bb981004SEric Auger         smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_EVENTQ_ABT_ERR_MASK);
231dadd1a08SEric Auger     }
232bb981004SEric Auger     info->recorded = true;
233dadd1a08SEric Auger }
234dadd1a08SEric Auger 
23510a83cb9SPrem Mallappa static void smmuv3_init_regs(SMMUv3State *s)
23610a83cb9SPrem Mallappa {
23710a83cb9SPrem Mallappa     /**
23810a83cb9SPrem Mallappa      * IDR0: stage1 only, AArch64 only, coherent access, 16b ASID,
23910a83cb9SPrem Mallappa      *       multi-level stream table
24010a83cb9SPrem Mallappa      */
24110a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, S1P, 1); /* stage 1 supported */
24210a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTF, 2); /* AArch64 PTW only */
24310a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, COHACC, 1); /* IO coherent */
24410a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, ASID16, 1); /* 16-bit ASID */
24510a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TTENDIAN, 2); /* little endian */
24610a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STALL_MODEL, 1); /* No stall */
24710a83cb9SPrem Mallappa     /* terminated transaction will always be aborted/error returned */
24810a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, TERM_MODEL, 1);
24910a83cb9SPrem Mallappa     /* 2-level stream table supported */
25010a83cb9SPrem Mallappa     s->idr[0] = FIELD_DP32(s->idr[0], IDR0, STLEVEL, 1);
25110a83cb9SPrem Mallappa 
25210a83cb9SPrem Mallappa     s->idr[1] = FIELD_DP32(s->idr[1], IDR1, SIDSIZE, SMMU_IDR1_SIDSIZE);
25310a83cb9SPrem Mallappa     s->idr[1] = FIELD_DP32(s->idr[1], IDR1, EVENTQS, SMMU_EVENTQS);
25410a83cb9SPrem Mallappa     s->idr[1] = FIELD_DP32(s->idr[1], IDR1, CMDQS,   SMMU_CMDQS);
25510a83cb9SPrem Mallappa 
25610a83cb9SPrem Mallappa    /* 4K and 64K granule support */
25710a83cb9SPrem Mallappa     s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN4K, 1);
25810a83cb9SPrem Mallappa     s->idr[5] = FIELD_DP32(s->idr[5], IDR5, GRAN64K, 1);
25910a83cb9SPrem Mallappa     s->idr[5] = FIELD_DP32(s->idr[5], IDR5, OAS, SMMU_IDR5_OAS); /* 44 bits */
26010a83cb9SPrem Mallappa 
26110a83cb9SPrem Mallappa     s->cmdq.base = deposit64(s->cmdq.base, 0, 5, SMMU_CMDQS);
26210a83cb9SPrem Mallappa     s->cmdq.prod = 0;
26310a83cb9SPrem Mallappa     s->cmdq.cons = 0;
26410a83cb9SPrem Mallappa     s->cmdq.entry_size = sizeof(struct Cmd);
26510a83cb9SPrem Mallappa     s->eventq.base = deposit64(s->eventq.base, 0, 5, SMMU_EVENTQS);
26610a83cb9SPrem Mallappa     s->eventq.prod = 0;
26710a83cb9SPrem Mallappa     s->eventq.cons = 0;
26810a83cb9SPrem Mallappa     s->eventq.entry_size = sizeof(struct Evt);
26910a83cb9SPrem Mallappa 
27010a83cb9SPrem Mallappa     s->features = 0;
27110a83cb9SPrem Mallappa     s->sid_split = 0;
27210a83cb9SPrem Mallappa }
27310a83cb9SPrem Mallappa 
2749bde7f06SEric Auger static int smmu_get_ste(SMMUv3State *s, dma_addr_t addr, STE *buf,
2759bde7f06SEric Auger                         SMMUEventInfo *event)
2769bde7f06SEric Auger {
2779bde7f06SEric Auger     int ret;
2789bde7f06SEric Auger 
2799bde7f06SEric Auger     trace_smmuv3_get_ste(addr);
2809bde7f06SEric Auger     /* TODO: guarantee 64-bit single-copy atomicity */
2819bde7f06SEric Auger     ret = dma_memory_read(&address_space_memory, addr,
2829bde7f06SEric Auger                           (void *)buf, sizeof(*buf));
2839bde7f06SEric Auger     if (ret != MEMTX_OK) {
2849bde7f06SEric Auger         qemu_log_mask(LOG_GUEST_ERROR,
2859bde7f06SEric Auger                       "Cannot fetch pte at address=0x%"PRIx64"\n", addr);
2869bde7f06SEric Auger         event->type = SMMU_EVT_F_STE_FETCH;
2879bde7f06SEric Auger         event->u.f_ste_fetch.addr = addr;
2889bde7f06SEric Auger         return -EINVAL;
2899bde7f06SEric Auger     }
2909bde7f06SEric Auger     return 0;
2919bde7f06SEric Auger 
2929bde7f06SEric Auger }
2939bde7f06SEric Auger 
2949bde7f06SEric Auger /* @ssid > 0 not supported yet */
2959bde7f06SEric Auger static int smmu_get_cd(SMMUv3State *s, STE *ste, uint32_t ssid,
2969bde7f06SEric Auger                        CD *buf, SMMUEventInfo *event)
2979bde7f06SEric Auger {
2989bde7f06SEric Auger     dma_addr_t addr = STE_CTXPTR(ste);
2999bde7f06SEric Auger     int ret;
3009bde7f06SEric Auger 
3019bde7f06SEric Auger     trace_smmuv3_get_cd(addr);
3029bde7f06SEric Auger     /* TODO: guarantee 64-bit single-copy atomicity */
3039bde7f06SEric Auger     ret = dma_memory_read(&address_space_memory, addr,
3049bde7f06SEric Auger                            (void *)buf, sizeof(*buf));
3059bde7f06SEric Auger     if (ret != MEMTX_OK) {
3069bde7f06SEric Auger         qemu_log_mask(LOG_GUEST_ERROR,
3079bde7f06SEric Auger                       "Cannot fetch pte at address=0x%"PRIx64"\n", addr);
3089bde7f06SEric Auger         event->type = SMMU_EVT_F_CD_FETCH;
3099bde7f06SEric Auger         event->u.f_ste_fetch.addr = addr;
3109bde7f06SEric Auger         return -EINVAL;
3119bde7f06SEric Auger     }
3129bde7f06SEric Auger     return 0;
3139bde7f06SEric Auger }
3149bde7f06SEric Auger 
3159bde7f06SEric Auger /* Returns <0 if the caller has no need to continue the translation */
3169bde7f06SEric Auger static int decode_ste(SMMUv3State *s, SMMUTransCfg *cfg,
3179bde7f06SEric Auger                       STE *ste, SMMUEventInfo *event)
3189bde7f06SEric Auger {
3199bde7f06SEric Auger     uint32_t config;
3209bde7f06SEric Auger     int ret = -EINVAL;
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)) {
3299bde7f06SEric Auger         cfg->aborted = true; /* abort but don't record any event */
3309bde7f06SEric Auger         return ret;
3319bde7f06SEric Auger     }
3329bde7f06SEric Auger 
3339bde7f06SEric Auger     if (STE_CFG_BYPASS(config)) {
3349bde7f06SEric Auger         cfg->bypassed = true;
3359bde7f06SEric Auger         return ret;
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  *
5129bde7f06SEric Auger  * return < 0 if the translation needs to be aborted (@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;
5219bde7f06SEric Auger     int ret = -EINVAL;
5229bde7f06SEric Auger     STE ste;
5239bde7f06SEric Auger     CD cd;
5249bde7f06SEric Auger 
5259bde7f06SEric Auger     if (smmu_find_ste(s, sid, &ste, event)) {
5269bde7f06SEric Auger         return ret;
5279bde7f06SEric Auger     }
5289bde7f06SEric Auger 
5299bde7f06SEric Auger     if (decode_ste(s, cfg, &ste, event)) {
5309bde7f06SEric Auger         return ret;
5319bde7f06SEric Auger     }
5329bde7f06SEric Auger 
5339bde7f06SEric Auger     if (smmu_get_cd(s, &ste, 0 /* ssid */, &cd, event)) {
5349bde7f06SEric Auger         return ret;
5359bde7f06SEric Auger     }
5369bde7f06SEric Auger 
5379bde7f06SEric Auger     return decode_cd(cfg, &cd, event);
5389bde7f06SEric Auger }
5399bde7f06SEric Auger 
5409bde7f06SEric Auger static IOMMUTLBEntry smmuv3_translate(IOMMUMemoryRegion *mr, hwaddr addr,
541*2c91bcf2SPeter Maydell                                       IOMMUAccessFlags flag, int iommu_idx)
5429bde7f06SEric Auger {
5439bde7f06SEric Auger     SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
5449bde7f06SEric Auger     SMMUv3State *s = sdev->smmu;
5459bde7f06SEric Auger     uint32_t sid = smmu_get_sid(sdev);
5469bde7f06SEric Auger     SMMUEventInfo event = {.type = SMMU_EVT_OK, .sid = sid};
5479bde7f06SEric Auger     SMMUPTWEventInfo ptw_info = {};
5489bde7f06SEric Auger     SMMUTransCfg cfg = {};
5499bde7f06SEric Auger     IOMMUTLBEntry entry = {
5509bde7f06SEric Auger         .target_as = &address_space_memory,
5519bde7f06SEric Auger         .iova = addr,
5529bde7f06SEric Auger         .translated_addr = addr,
5539bde7f06SEric Auger         .addr_mask = ~(hwaddr)0,
5549bde7f06SEric Auger         .perm = IOMMU_NONE,
5559bde7f06SEric Auger     };
5569bde7f06SEric Auger     int ret = 0;
5579bde7f06SEric Auger 
5589bde7f06SEric Auger     if (!smmu_enabled(s)) {
5599bde7f06SEric Auger         goto out;
5609bde7f06SEric Auger     }
5619bde7f06SEric Auger 
5629bde7f06SEric Auger     ret = smmuv3_decode_config(mr, &cfg, &event);
5639bde7f06SEric Auger     if (ret) {
5649bde7f06SEric Auger         goto out;
5659bde7f06SEric Auger     }
5669bde7f06SEric Auger 
5679bde7f06SEric Auger     if (cfg.aborted) {
5689bde7f06SEric Auger         goto out;
5699bde7f06SEric Auger     }
5709bde7f06SEric Auger 
5719bde7f06SEric Auger     ret = smmu_ptw(&cfg, addr, flag, &entry, &ptw_info);
5729bde7f06SEric Auger     if (ret) {
5739bde7f06SEric Auger         switch (ptw_info.type) {
5749bde7f06SEric Auger         case SMMU_PTW_ERR_WALK_EABT:
5759bde7f06SEric Auger             event.type = SMMU_EVT_F_WALK_EABT;
5769bde7f06SEric Auger             event.u.f_walk_eabt.addr = addr;
5779bde7f06SEric Auger             event.u.f_walk_eabt.rnw = flag & 0x1;
5789bde7f06SEric Auger             event.u.f_walk_eabt.class = 0x1;
5799bde7f06SEric Auger             event.u.f_walk_eabt.addr2 = ptw_info.addr;
5809bde7f06SEric Auger             break;
5819bde7f06SEric Auger         case SMMU_PTW_ERR_TRANSLATION:
5829bde7f06SEric Auger             if (event.record_trans_faults) {
5839bde7f06SEric Auger                 event.type = SMMU_EVT_F_TRANSLATION;
5849bde7f06SEric Auger                 event.u.f_translation.addr = addr;
5859bde7f06SEric Auger                 event.u.f_translation.rnw = flag & 0x1;
5869bde7f06SEric Auger             }
5879bde7f06SEric Auger             break;
5889bde7f06SEric Auger         case SMMU_PTW_ERR_ADDR_SIZE:
5899bde7f06SEric Auger             if (event.record_trans_faults) {
5909bde7f06SEric Auger                 event.type = SMMU_EVT_F_ADDR_SIZE;
5919bde7f06SEric Auger                 event.u.f_addr_size.addr = addr;
5929bde7f06SEric Auger                 event.u.f_addr_size.rnw = flag & 0x1;
5939bde7f06SEric Auger             }
5949bde7f06SEric Auger             break;
5959bde7f06SEric Auger         case SMMU_PTW_ERR_ACCESS:
5969bde7f06SEric Auger             if (event.record_trans_faults) {
5979bde7f06SEric Auger                 event.type = SMMU_EVT_F_ACCESS;
5989bde7f06SEric Auger                 event.u.f_access.addr = addr;
5999bde7f06SEric Auger                 event.u.f_access.rnw = flag & 0x1;
6009bde7f06SEric Auger             }
6019bde7f06SEric Auger             break;
6029bde7f06SEric Auger         case SMMU_PTW_ERR_PERMISSION:
6039bde7f06SEric Auger             if (event.record_trans_faults) {
6049bde7f06SEric Auger                 event.type = SMMU_EVT_F_PERMISSION;
6059bde7f06SEric Auger                 event.u.f_permission.addr = addr;
6069bde7f06SEric Auger                 event.u.f_permission.rnw = flag & 0x1;
6079bde7f06SEric Auger             }
6089bde7f06SEric Auger             break;
6099bde7f06SEric Auger         default:
6109bde7f06SEric Auger             g_assert_not_reached();
6119bde7f06SEric Auger         }
6129bde7f06SEric Auger     }
6139bde7f06SEric Auger out:
6149bde7f06SEric Auger     if (ret) {
6159bde7f06SEric Auger         qemu_log_mask(LOG_GUEST_ERROR,
6169bde7f06SEric Auger                       "%s translation failed for iova=0x%"PRIx64"(%d)\n",
6179bde7f06SEric Auger                       mr->parent_obj.name, addr, ret);
6189bde7f06SEric Auger         entry.perm = IOMMU_NONE;
6199bde7f06SEric Auger         smmuv3_record_event(s, &event);
6209bde7f06SEric Auger     } else if (!cfg.aborted) {
6219bde7f06SEric Auger         entry.perm = flag;
6229bde7f06SEric Auger         trace_smmuv3_translate(mr->parent_obj.name, sid, addr,
6239bde7f06SEric Auger                                entry.translated_addr, entry.perm);
6249bde7f06SEric Auger     }
6259bde7f06SEric Auger 
6269bde7f06SEric Auger     return entry;
6279bde7f06SEric Auger }
6289bde7f06SEric Auger 
629fae4be38SEric Auger static int smmuv3_cmdq_consume(SMMUv3State *s)
630dadd1a08SEric Auger {
631dadd1a08SEric Auger     SMMUCmdError cmd_error = SMMU_CERROR_NONE;
632dadd1a08SEric Auger     SMMUQueue *q = &s->cmdq;
633dadd1a08SEric Auger     SMMUCommandType type = 0;
634dadd1a08SEric Auger 
635dadd1a08SEric Auger     if (!smmuv3_cmdq_enabled(s)) {
636dadd1a08SEric Auger         return 0;
637dadd1a08SEric Auger     }
638dadd1a08SEric Auger     /*
639dadd1a08SEric Auger      * some commands depend on register values, typically CR0. In case those
640dadd1a08SEric Auger      * register values change while handling the command, spec says it
641dadd1a08SEric Auger      * is UNPREDICTABLE whether the command is interpreted under the new
642dadd1a08SEric Auger      * or old value.
643dadd1a08SEric Auger      */
644dadd1a08SEric Auger 
645dadd1a08SEric Auger     while (!smmuv3_q_empty(q)) {
646dadd1a08SEric Auger         uint32_t pending = s->gerror ^ s->gerrorn;
647dadd1a08SEric Auger         Cmd cmd;
648dadd1a08SEric Auger 
649dadd1a08SEric Auger         trace_smmuv3_cmdq_consume(Q_PROD(q), Q_CONS(q),
650dadd1a08SEric Auger                                   Q_PROD_WRAP(q), Q_CONS_WRAP(q));
651dadd1a08SEric Auger 
652dadd1a08SEric Auger         if (FIELD_EX32(pending, GERROR, CMDQ_ERR)) {
653dadd1a08SEric Auger             break;
654dadd1a08SEric Auger         }
655dadd1a08SEric Auger 
656dadd1a08SEric Auger         if (queue_read(q, &cmd) != MEMTX_OK) {
657dadd1a08SEric Auger             cmd_error = SMMU_CERROR_ABT;
658dadd1a08SEric Auger             break;
659dadd1a08SEric Auger         }
660dadd1a08SEric Auger 
661dadd1a08SEric Auger         type = CMD_TYPE(&cmd);
662dadd1a08SEric Auger 
663dadd1a08SEric Auger         trace_smmuv3_cmdq_opcode(smmu_cmd_string(type));
664dadd1a08SEric Auger 
665dadd1a08SEric Auger         switch (type) {
666dadd1a08SEric Auger         case SMMU_CMD_SYNC:
667dadd1a08SEric Auger             if (CMD_SYNC_CS(&cmd) & CMD_SYNC_SIG_IRQ) {
668dadd1a08SEric Auger                 smmuv3_trigger_irq(s, SMMU_IRQ_CMD_SYNC, 0);
669dadd1a08SEric Auger             }
670dadd1a08SEric Auger             break;
671dadd1a08SEric Auger         case SMMU_CMD_PREFETCH_CONFIG:
672dadd1a08SEric Auger         case SMMU_CMD_PREFETCH_ADDR:
673dadd1a08SEric Auger         case SMMU_CMD_CFGI_STE:
674dadd1a08SEric Auger         case SMMU_CMD_CFGI_STE_RANGE: /* same as SMMU_CMD_CFGI_ALL */
675dadd1a08SEric Auger         case SMMU_CMD_CFGI_CD:
676dadd1a08SEric Auger         case SMMU_CMD_CFGI_CD_ALL:
677dadd1a08SEric Auger         case SMMU_CMD_TLBI_NH_ALL:
678dadd1a08SEric Auger         case SMMU_CMD_TLBI_NH_ASID:
679dadd1a08SEric Auger         case SMMU_CMD_TLBI_NH_VA:
680dadd1a08SEric Auger         case SMMU_CMD_TLBI_NH_VAA:
681dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL3_ALL:
682dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL3_VA:
683dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL2_ALL:
684dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL2_ASID:
685dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL2_VA:
686dadd1a08SEric Auger         case SMMU_CMD_TLBI_EL2_VAA:
687dadd1a08SEric Auger         case SMMU_CMD_TLBI_S12_VMALL:
688dadd1a08SEric Auger         case SMMU_CMD_TLBI_S2_IPA:
689dadd1a08SEric Auger         case SMMU_CMD_TLBI_NSNH_ALL:
690dadd1a08SEric Auger         case SMMU_CMD_ATC_INV:
691dadd1a08SEric Auger         case SMMU_CMD_PRI_RESP:
692dadd1a08SEric Auger         case SMMU_CMD_RESUME:
693dadd1a08SEric Auger         case SMMU_CMD_STALL_TERM:
694dadd1a08SEric Auger             trace_smmuv3_unhandled_cmd(type);
695dadd1a08SEric Auger             break;
696dadd1a08SEric Auger         default:
697dadd1a08SEric Auger             cmd_error = SMMU_CERROR_ILL;
698dadd1a08SEric Auger             qemu_log_mask(LOG_GUEST_ERROR,
699dadd1a08SEric Auger                           "Illegal command type: %d\n", CMD_TYPE(&cmd));
700dadd1a08SEric Auger             break;
701dadd1a08SEric Auger         }
702dadd1a08SEric Auger         if (cmd_error) {
703dadd1a08SEric Auger             break;
704dadd1a08SEric Auger         }
705dadd1a08SEric Auger         /*
706dadd1a08SEric Auger          * We only increment the cons index after the completion of
707dadd1a08SEric Auger          * the command. We do that because the SYNC returns immediately
708dadd1a08SEric Auger          * and does not check the completion of previous commands
709dadd1a08SEric Auger          */
710dadd1a08SEric Auger         queue_cons_incr(q);
711dadd1a08SEric Auger     }
712dadd1a08SEric Auger 
713dadd1a08SEric Auger     if (cmd_error) {
714dadd1a08SEric Auger         trace_smmuv3_cmdq_consume_error(smmu_cmd_string(type), cmd_error);
715dadd1a08SEric Auger         smmu_write_cmdq_err(s, cmd_error);
716dadd1a08SEric Auger         smmuv3_trigger_irq(s, SMMU_IRQ_GERROR, R_GERROR_CMDQ_ERR_MASK);
717dadd1a08SEric Auger     }
718dadd1a08SEric Auger 
719dadd1a08SEric Auger     trace_smmuv3_cmdq_consume_out(Q_PROD(q), Q_CONS(q),
720dadd1a08SEric Auger                                   Q_PROD_WRAP(q), Q_CONS_WRAP(q));
721dadd1a08SEric Auger 
722dadd1a08SEric Auger     return 0;
723dadd1a08SEric Auger }
724dadd1a08SEric Auger 
725fae4be38SEric Auger static MemTxResult smmu_writell(SMMUv3State *s, hwaddr offset,
726fae4be38SEric Auger                                uint64_t data, MemTxAttrs attrs)
727fae4be38SEric Auger {
728fae4be38SEric Auger     switch (offset) {
729fae4be38SEric Auger     case A_GERROR_IRQ_CFG0:
730fae4be38SEric Auger         s->gerror_irq_cfg0 = data;
731fae4be38SEric Auger         return MEMTX_OK;
732fae4be38SEric Auger     case A_STRTAB_BASE:
733fae4be38SEric Auger         s->strtab_base = data;
734fae4be38SEric Auger         return MEMTX_OK;
735fae4be38SEric Auger     case A_CMDQ_BASE:
736fae4be38SEric Auger         s->cmdq.base = data;
737fae4be38SEric Auger         s->cmdq.log2size = extract64(s->cmdq.base, 0, 5);
738fae4be38SEric Auger         if (s->cmdq.log2size > SMMU_CMDQS) {
739fae4be38SEric Auger             s->cmdq.log2size = SMMU_CMDQS;
740fae4be38SEric Auger         }
741fae4be38SEric Auger         return MEMTX_OK;
742fae4be38SEric Auger     case A_EVENTQ_BASE:
743fae4be38SEric Auger         s->eventq.base = data;
744fae4be38SEric Auger         s->eventq.log2size = extract64(s->eventq.base, 0, 5);
745fae4be38SEric Auger         if (s->eventq.log2size > SMMU_EVENTQS) {
746fae4be38SEric Auger             s->eventq.log2size = SMMU_EVENTQS;
747fae4be38SEric Auger         }
748fae4be38SEric Auger         return MEMTX_OK;
749fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG0:
750fae4be38SEric Auger         s->eventq_irq_cfg0 = data;
751fae4be38SEric Auger         return MEMTX_OK;
752fae4be38SEric Auger     default:
753fae4be38SEric Auger         qemu_log_mask(LOG_UNIMP,
754fae4be38SEric Auger                       "%s Unexpected 64-bit access to 0x%"PRIx64" (WI)\n",
755fae4be38SEric Auger                       __func__, offset);
756fae4be38SEric Auger         return MEMTX_OK;
757fae4be38SEric Auger     }
758fae4be38SEric Auger }
759fae4be38SEric Auger 
760fae4be38SEric Auger static MemTxResult smmu_writel(SMMUv3State *s, hwaddr offset,
761fae4be38SEric Auger                                uint64_t data, MemTxAttrs attrs)
762fae4be38SEric Auger {
763fae4be38SEric Auger     switch (offset) {
764fae4be38SEric Auger     case A_CR0:
765fae4be38SEric Auger         s->cr[0] = data;
766fae4be38SEric Auger         s->cr0ack = data & ~SMMU_CR0_RESERVED;
767fae4be38SEric Auger         /* in case the command queue has been enabled */
768fae4be38SEric Auger         smmuv3_cmdq_consume(s);
769fae4be38SEric Auger         return MEMTX_OK;
770fae4be38SEric Auger     case A_CR1:
771fae4be38SEric Auger         s->cr[1] = data;
772fae4be38SEric Auger         return MEMTX_OK;
773fae4be38SEric Auger     case A_CR2:
774fae4be38SEric Auger         s->cr[2] = data;
775fae4be38SEric Auger         return MEMTX_OK;
776fae4be38SEric Auger     case A_IRQ_CTRL:
777fae4be38SEric Auger         s->irq_ctrl = data;
778fae4be38SEric Auger         return MEMTX_OK;
779fae4be38SEric Auger     case A_GERRORN:
780fae4be38SEric Auger         smmuv3_write_gerrorn(s, data);
781fae4be38SEric Auger         /*
782fae4be38SEric Auger          * By acknowledging the CMDQ_ERR, SW may notify cmds can
783fae4be38SEric Auger          * be processed again
784fae4be38SEric Auger          */
785fae4be38SEric Auger         smmuv3_cmdq_consume(s);
786fae4be38SEric Auger         return MEMTX_OK;
787fae4be38SEric Auger     case A_GERROR_IRQ_CFG0: /* 64b */
788fae4be38SEric Auger         s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 0, 32, data);
789fae4be38SEric Auger         return MEMTX_OK;
790fae4be38SEric Auger     case A_GERROR_IRQ_CFG0 + 4:
791fae4be38SEric Auger         s->gerror_irq_cfg0 = deposit64(s->gerror_irq_cfg0, 32, 32, data);
792fae4be38SEric Auger         return MEMTX_OK;
793fae4be38SEric Auger     case A_GERROR_IRQ_CFG1:
794fae4be38SEric Auger         s->gerror_irq_cfg1 = data;
795fae4be38SEric Auger         return MEMTX_OK;
796fae4be38SEric Auger     case A_GERROR_IRQ_CFG2:
797fae4be38SEric Auger         s->gerror_irq_cfg2 = data;
798fae4be38SEric Auger         return MEMTX_OK;
799fae4be38SEric Auger     case A_STRTAB_BASE: /* 64b */
800fae4be38SEric Auger         s->strtab_base = deposit64(s->strtab_base, 0, 32, data);
801fae4be38SEric Auger         return MEMTX_OK;
802fae4be38SEric Auger     case A_STRTAB_BASE + 4:
803fae4be38SEric Auger         s->strtab_base = deposit64(s->strtab_base, 32, 32, data);
804fae4be38SEric Auger         return MEMTX_OK;
805fae4be38SEric Auger     case A_STRTAB_BASE_CFG:
806fae4be38SEric Auger         s->strtab_base_cfg = data;
807fae4be38SEric Auger         if (FIELD_EX32(data, STRTAB_BASE_CFG, FMT) == 1) {
808fae4be38SEric Auger             s->sid_split = FIELD_EX32(data, STRTAB_BASE_CFG, SPLIT);
809fae4be38SEric Auger             s->features |= SMMU_FEATURE_2LVL_STE;
810fae4be38SEric Auger         }
811fae4be38SEric Auger         return MEMTX_OK;
812fae4be38SEric Auger     case A_CMDQ_BASE: /* 64b */
813fae4be38SEric Auger         s->cmdq.base = deposit64(s->cmdq.base, 0, 32, data);
814fae4be38SEric Auger         s->cmdq.log2size = extract64(s->cmdq.base, 0, 5);
815fae4be38SEric Auger         if (s->cmdq.log2size > SMMU_CMDQS) {
816fae4be38SEric Auger             s->cmdq.log2size = SMMU_CMDQS;
817fae4be38SEric Auger         }
818fae4be38SEric Auger         return MEMTX_OK;
819fae4be38SEric Auger     case A_CMDQ_BASE + 4: /* 64b */
820fae4be38SEric Auger         s->cmdq.base = deposit64(s->cmdq.base, 32, 32, data);
821fae4be38SEric Auger         return MEMTX_OK;
822fae4be38SEric Auger     case A_CMDQ_PROD:
823fae4be38SEric Auger         s->cmdq.prod = data;
824fae4be38SEric Auger         smmuv3_cmdq_consume(s);
825fae4be38SEric Auger         return MEMTX_OK;
826fae4be38SEric Auger     case A_CMDQ_CONS:
827fae4be38SEric Auger         s->cmdq.cons = data;
828fae4be38SEric Auger         return MEMTX_OK;
829fae4be38SEric Auger     case A_EVENTQ_BASE: /* 64b */
830fae4be38SEric Auger         s->eventq.base = deposit64(s->eventq.base, 0, 32, data);
831fae4be38SEric Auger         s->eventq.log2size = extract64(s->eventq.base, 0, 5);
832fae4be38SEric Auger         if (s->eventq.log2size > SMMU_EVENTQS) {
833fae4be38SEric Auger             s->eventq.log2size = SMMU_EVENTQS;
834fae4be38SEric Auger         }
835fae4be38SEric Auger         return MEMTX_OK;
836fae4be38SEric Auger     case A_EVENTQ_BASE + 4:
837fae4be38SEric Auger         s->eventq.base = deposit64(s->eventq.base, 32, 32, data);
838fae4be38SEric Auger         return MEMTX_OK;
839fae4be38SEric Auger     case A_EVENTQ_PROD:
840fae4be38SEric Auger         s->eventq.prod = data;
841fae4be38SEric Auger         return MEMTX_OK;
842fae4be38SEric Auger     case A_EVENTQ_CONS:
843fae4be38SEric Auger         s->eventq.cons = data;
844fae4be38SEric Auger         return MEMTX_OK;
845fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG0: /* 64b */
846fae4be38SEric Auger         s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 0, 32, data);
847fae4be38SEric Auger         return MEMTX_OK;
848fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG0 + 4:
849fae4be38SEric Auger         s->eventq_irq_cfg0 = deposit64(s->eventq_irq_cfg0, 32, 32, data);
850fae4be38SEric Auger         return MEMTX_OK;
851fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG1:
852fae4be38SEric Auger         s->eventq_irq_cfg1 = data;
853fae4be38SEric Auger         return MEMTX_OK;
854fae4be38SEric Auger     case A_EVENTQ_IRQ_CFG2:
855fae4be38SEric Auger         s->eventq_irq_cfg2 = data;
856fae4be38SEric Auger         return MEMTX_OK;
857fae4be38SEric Auger     default:
858fae4be38SEric Auger         qemu_log_mask(LOG_UNIMP,
859fae4be38SEric Auger                       "%s Unexpected 32-bit access to 0x%"PRIx64" (WI)\n",
860fae4be38SEric Auger                       __func__, offset);
861fae4be38SEric Auger         return MEMTX_OK;
862fae4be38SEric Auger     }
863fae4be38SEric Auger }
864fae4be38SEric Auger 
86510a83cb9SPrem Mallappa static MemTxResult smmu_write_mmio(void *opaque, hwaddr offset, uint64_t data,
86610a83cb9SPrem Mallappa                                    unsigned size, MemTxAttrs attrs)
86710a83cb9SPrem Mallappa {
868fae4be38SEric Auger     SMMUState *sys = opaque;
869fae4be38SEric Auger     SMMUv3State *s = ARM_SMMUV3(sys);
870fae4be38SEric Auger     MemTxResult r;
871fae4be38SEric Auger 
872fae4be38SEric Auger     /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */
873fae4be38SEric Auger     offset &= ~0x10000;
874fae4be38SEric Auger 
875fae4be38SEric Auger     switch (size) {
876fae4be38SEric Auger     case 8:
877fae4be38SEric Auger         r = smmu_writell(s, offset, data, attrs);
878fae4be38SEric Auger         break;
879fae4be38SEric Auger     case 4:
880fae4be38SEric Auger         r = smmu_writel(s, offset, data, attrs);
881fae4be38SEric Auger         break;
882fae4be38SEric Auger     default:
883fae4be38SEric Auger         r = MEMTX_ERROR;
884fae4be38SEric Auger         break;
885fae4be38SEric Auger     }
886fae4be38SEric Auger 
887fae4be38SEric Auger     trace_smmuv3_write_mmio(offset, data, size, r);
888fae4be38SEric Auger     return r;
88910a83cb9SPrem Mallappa }
89010a83cb9SPrem Mallappa 
89110a83cb9SPrem Mallappa static MemTxResult smmu_readll(SMMUv3State *s, hwaddr offset,
89210a83cb9SPrem Mallappa                                uint64_t *data, MemTxAttrs attrs)
89310a83cb9SPrem Mallappa {
89410a83cb9SPrem Mallappa     switch (offset) {
89510a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG0:
89610a83cb9SPrem Mallappa         *data = s->gerror_irq_cfg0;
89710a83cb9SPrem Mallappa         return MEMTX_OK;
89810a83cb9SPrem Mallappa     case A_STRTAB_BASE:
89910a83cb9SPrem Mallappa         *data = s->strtab_base;
90010a83cb9SPrem Mallappa         return MEMTX_OK;
90110a83cb9SPrem Mallappa     case A_CMDQ_BASE:
90210a83cb9SPrem Mallappa         *data = s->cmdq.base;
90310a83cb9SPrem Mallappa         return MEMTX_OK;
90410a83cb9SPrem Mallappa     case A_EVENTQ_BASE:
90510a83cb9SPrem Mallappa         *data = s->eventq.base;
90610a83cb9SPrem Mallappa         return MEMTX_OK;
90710a83cb9SPrem Mallappa     default:
90810a83cb9SPrem Mallappa         *data = 0;
90910a83cb9SPrem Mallappa         qemu_log_mask(LOG_UNIMP,
91010a83cb9SPrem Mallappa                       "%s Unexpected 64-bit access to 0x%"PRIx64" (RAZ)\n",
91110a83cb9SPrem Mallappa                       __func__, offset);
91210a83cb9SPrem Mallappa         return MEMTX_OK;
91310a83cb9SPrem Mallappa     }
91410a83cb9SPrem Mallappa }
91510a83cb9SPrem Mallappa 
91610a83cb9SPrem Mallappa static MemTxResult smmu_readl(SMMUv3State *s, hwaddr offset,
91710a83cb9SPrem Mallappa                               uint64_t *data, MemTxAttrs attrs)
91810a83cb9SPrem Mallappa {
91910a83cb9SPrem Mallappa     switch (offset) {
92010a83cb9SPrem Mallappa     case A_IDREGS ... A_IDREGS + 0x1f:
92110a83cb9SPrem Mallappa         *data = smmuv3_idreg(offset - A_IDREGS);
92210a83cb9SPrem Mallappa         return MEMTX_OK;
92310a83cb9SPrem Mallappa     case A_IDR0 ... A_IDR5:
92410a83cb9SPrem Mallappa         *data = s->idr[(offset - A_IDR0) / 4];
92510a83cb9SPrem Mallappa         return MEMTX_OK;
92610a83cb9SPrem Mallappa     case A_IIDR:
92710a83cb9SPrem Mallappa         *data = s->iidr;
92810a83cb9SPrem Mallappa         return MEMTX_OK;
92910a83cb9SPrem Mallappa     case A_CR0:
93010a83cb9SPrem Mallappa         *data = s->cr[0];
93110a83cb9SPrem Mallappa         return MEMTX_OK;
93210a83cb9SPrem Mallappa     case A_CR0ACK:
93310a83cb9SPrem Mallappa         *data = s->cr0ack;
93410a83cb9SPrem Mallappa         return MEMTX_OK;
93510a83cb9SPrem Mallappa     case A_CR1:
93610a83cb9SPrem Mallappa         *data = s->cr[1];
93710a83cb9SPrem Mallappa         return MEMTX_OK;
93810a83cb9SPrem Mallappa     case A_CR2:
93910a83cb9SPrem Mallappa         *data = s->cr[2];
94010a83cb9SPrem Mallappa         return MEMTX_OK;
94110a83cb9SPrem Mallappa     case A_STATUSR:
94210a83cb9SPrem Mallappa         *data = s->statusr;
94310a83cb9SPrem Mallappa         return MEMTX_OK;
94410a83cb9SPrem Mallappa     case A_IRQ_CTRL:
94510a83cb9SPrem Mallappa     case A_IRQ_CTRL_ACK:
94610a83cb9SPrem Mallappa         *data = s->irq_ctrl;
94710a83cb9SPrem Mallappa         return MEMTX_OK;
94810a83cb9SPrem Mallappa     case A_GERROR:
94910a83cb9SPrem Mallappa         *data = s->gerror;
95010a83cb9SPrem Mallappa         return MEMTX_OK;
95110a83cb9SPrem Mallappa     case A_GERRORN:
95210a83cb9SPrem Mallappa         *data = s->gerrorn;
95310a83cb9SPrem Mallappa         return MEMTX_OK;
95410a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG0: /* 64b */
95510a83cb9SPrem Mallappa         *data = extract64(s->gerror_irq_cfg0, 0, 32);
95610a83cb9SPrem Mallappa         return MEMTX_OK;
95710a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG0 + 4:
95810a83cb9SPrem Mallappa         *data = extract64(s->gerror_irq_cfg0, 32, 32);
95910a83cb9SPrem Mallappa         return MEMTX_OK;
96010a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG1:
96110a83cb9SPrem Mallappa         *data = s->gerror_irq_cfg1;
96210a83cb9SPrem Mallappa         return MEMTX_OK;
96310a83cb9SPrem Mallappa     case A_GERROR_IRQ_CFG2:
96410a83cb9SPrem Mallappa         *data = s->gerror_irq_cfg2;
96510a83cb9SPrem Mallappa         return MEMTX_OK;
96610a83cb9SPrem Mallappa     case A_STRTAB_BASE: /* 64b */
96710a83cb9SPrem Mallappa         *data = extract64(s->strtab_base, 0, 32);
96810a83cb9SPrem Mallappa         return MEMTX_OK;
96910a83cb9SPrem Mallappa     case A_STRTAB_BASE + 4: /* 64b */
97010a83cb9SPrem Mallappa         *data = extract64(s->strtab_base, 32, 32);
97110a83cb9SPrem Mallappa         return MEMTX_OK;
97210a83cb9SPrem Mallappa     case A_STRTAB_BASE_CFG:
97310a83cb9SPrem Mallappa         *data = s->strtab_base_cfg;
97410a83cb9SPrem Mallappa         return MEMTX_OK;
97510a83cb9SPrem Mallappa     case A_CMDQ_BASE: /* 64b */
97610a83cb9SPrem Mallappa         *data = extract64(s->cmdq.base, 0, 32);
97710a83cb9SPrem Mallappa         return MEMTX_OK;
97810a83cb9SPrem Mallappa     case A_CMDQ_BASE + 4:
97910a83cb9SPrem Mallappa         *data = extract64(s->cmdq.base, 32, 32);
98010a83cb9SPrem Mallappa         return MEMTX_OK;
98110a83cb9SPrem Mallappa     case A_CMDQ_PROD:
98210a83cb9SPrem Mallappa         *data = s->cmdq.prod;
98310a83cb9SPrem Mallappa         return MEMTX_OK;
98410a83cb9SPrem Mallappa     case A_CMDQ_CONS:
98510a83cb9SPrem Mallappa         *data = s->cmdq.cons;
98610a83cb9SPrem Mallappa         return MEMTX_OK;
98710a83cb9SPrem Mallappa     case A_EVENTQ_BASE: /* 64b */
98810a83cb9SPrem Mallappa         *data = extract64(s->eventq.base, 0, 32);
98910a83cb9SPrem Mallappa         return MEMTX_OK;
99010a83cb9SPrem Mallappa     case A_EVENTQ_BASE + 4: /* 64b */
99110a83cb9SPrem Mallappa         *data = extract64(s->eventq.base, 32, 32);
99210a83cb9SPrem Mallappa         return MEMTX_OK;
99310a83cb9SPrem Mallappa     case A_EVENTQ_PROD:
99410a83cb9SPrem Mallappa         *data = s->eventq.prod;
99510a83cb9SPrem Mallappa         return MEMTX_OK;
99610a83cb9SPrem Mallappa     case A_EVENTQ_CONS:
99710a83cb9SPrem Mallappa         *data = s->eventq.cons;
99810a83cb9SPrem Mallappa         return MEMTX_OK;
99910a83cb9SPrem Mallappa     default:
100010a83cb9SPrem Mallappa         *data = 0;
100110a83cb9SPrem Mallappa         qemu_log_mask(LOG_UNIMP,
100210a83cb9SPrem Mallappa                       "%s unhandled 32-bit access at 0x%"PRIx64" (RAZ)\n",
100310a83cb9SPrem Mallappa                       __func__, offset);
100410a83cb9SPrem Mallappa         return MEMTX_OK;
100510a83cb9SPrem Mallappa     }
100610a83cb9SPrem Mallappa }
100710a83cb9SPrem Mallappa 
100810a83cb9SPrem Mallappa static MemTxResult smmu_read_mmio(void *opaque, hwaddr offset, uint64_t *data,
100910a83cb9SPrem Mallappa                                   unsigned size, MemTxAttrs attrs)
101010a83cb9SPrem Mallappa {
101110a83cb9SPrem Mallappa     SMMUState *sys = opaque;
101210a83cb9SPrem Mallappa     SMMUv3State *s = ARM_SMMUV3(sys);
101310a83cb9SPrem Mallappa     MemTxResult r;
101410a83cb9SPrem Mallappa 
101510a83cb9SPrem Mallappa     /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */
101610a83cb9SPrem Mallappa     offset &= ~0x10000;
101710a83cb9SPrem Mallappa 
101810a83cb9SPrem Mallappa     switch (size) {
101910a83cb9SPrem Mallappa     case 8:
102010a83cb9SPrem Mallappa         r = smmu_readll(s, offset, data, attrs);
102110a83cb9SPrem Mallappa         break;
102210a83cb9SPrem Mallappa     case 4:
102310a83cb9SPrem Mallappa         r = smmu_readl(s, offset, data, attrs);
102410a83cb9SPrem Mallappa         break;
102510a83cb9SPrem Mallappa     default:
102610a83cb9SPrem Mallappa         r = MEMTX_ERROR;
102710a83cb9SPrem Mallappa         break;
102810a83cb9SPrem Mallappa     }
102910a83cb9SPrem Mallappa 
103010a83cb9SPrem Mallappa     trace_smmuv3_read_mmio(offset, *data, size, r);
103110a83cb9SPrem Mallappa     return r;
103210a83cb9SPrem Mallappa }
103310a83cb9SPrem Mallappa 
103410a83cb9SPrem Mallappa static const MemoryRegionOps smmu_mem_ops = {
103510a83cb9SPrem Mallappa     .read_with_attrs = smmu_read_mmio,
103610a83cb9SPrem Mallappa     .write_with_attrs = smmu_write_mmio,
103710a83cb9SPrem Mallappa     .endianness = DEVICE_LITTLE_ENDIAN,
103810a83cb9SPrem Mallappa     .valid = {
103910a83cb9SPrem Mallappa         .min_access_size = 4,
104010a83cb9SPrem Mallappa         .max_access_size = 8,
104110a83cb9SPrem Mallappa     },
104210a83cb9SPrem Mallappa     .impl = {
104310a83cb9SPrem Mallappa         .min_access_size = 4,
104410a83cb9SPrem Mallappa         .max_access_size = 8,
104510a83cb9SPrem Mallappa     },
104610a83cb9SPrem Mallappa };
104710a83cb9SPrem Mallappa 
104810a83cb9SPrem Mallappa static void smmu_init_irq(SMMUv3State *s, SysBusDevice *dev)
104910a83cb9SPrem Mallappa {
105010a83cb9SPrem Mallappa     int i;
105110a83cb9SPrem Mallappa 
105210a83cb9SPrem Mallappa     for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
105310a83cb9SPrem Mallappa         sysbus_init_irq(dev, &s->irq[i]);
105410a83cb9SPrem Mallappa     }
105510a83cb9SPrem Mallappa }
105610a83cb9SPrem Mallappa 
105710a83cb9SPrem Mallappa static void smmu_reset(DeviceState *dev)
105810a83cb9SPrem Mallappa {
105910a83cb9SPrem Mallappa     SMMUv3State *s = ARM_SMMUV3(dev);
106010a83cb9SPrem Mallappa     SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s);
106110a83cb9SPrem Mallappa 
106210a83cb9SPrem Mallappa     c->parent_reset(dev);
106310a83cb9SPrem Mallappa 
106410a83cb9SPrem Mallappa     smmuv3_init_regs(s);
106510a83cb9SPrem Mallappa }
106610a83cb9SPrem Mallappa 
106710a83cb9SPrem Mallappa static void smmu_realize(DeviceState *d, Error **errp)
106810a83cb9SPrem Mallappa {
106910a83cb9SPrem Mallappa     SMMUState *sys = ARM_SMMU(d);
107010a83cb9SPrem Mallappa     SMMUv3State *s = ARM_SMMUV3(sys);
107110a83cb9SPrem Mallappa     SMMUv3Class *c = ARM_SMMUV3_GET_CLASS(s);
107210a83cb9SPrem Mallappa     SysBusDevice *dev = SYS_BUS_DEVICE(d);
107310a83cb9SPrem Mallappa     Error *local_err = NULL;
107410a83cb9SPrem Mallappa 
107510a83cb9SPrem Mallappa     c->parent_realize(d, &local_err);
107610a83cb9SPrem Mallappa     if (local_err) {
107710a83cb9SPrem Mallappa         error_propagate(errp, local_err);
107810a83cb9SPrem Mallappa         return;
107910a83cb9SPrem Mallappa     }
108010a83cb9SPrem Mallappa 
108110a83cb9SPrem Mallappa     memory_region_init_io(&sys->iomem, OBJECT(s),
108210a83cb9SPrem Mallappa                           &smmu_mem_ops, sys, TYPE_ARM_SMMUV3, 0x20000);
108310a83cb9SPrem Mallappa 
108410a83cb9SPrem Mallappa     sys->mrtypename = TYPE_SMMUV3_IOMMU_MEMORY_REGION;
108510a83cb9SPrem Mallappa 
108610a83cb9SPrem Mallappa     sysbus_init_mmio(dev, &sys->iomem);
108710a83cb9SPrem Mallappa 
108810a83cb9SPrem Mallappa     smmu_init_irq(s, dev);
108910a83cb9SPrem Mallappa }
109010a83cb9SPrem Mallappa 
109110a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3_queue = {
109210a83cb9SPrem Mallappa     .name = "smmuv3_queue",
109310a83cb9SPrem Mallappa     .version_id = 1,
109410a83cb9SPrem Mallappa     .minimum_version_id = 1,
109510a83cb9SPrem Mallappa     .fields = (VMStateField[]) {
109610a83cb9SPrem Mallappa         VMSTATE_UINT64(base, SMMUQueue),
109710a83cb9SPrem Mallappa         VMSTATE_UINT32(prod, SMMUQueue),
109810a83cb9SPrem Mallappa         VMSTATE_UINT32(cons, SMMUQueue),
109910a83cb9SPrem Mallappa         VMSTATE_UINT8(log2size, SMMUQueue),
110010a83cb9SPrem Mallappa     },
110110a83cb9SPrem Mallappa };
110210a83cb9SPrem Mallappa 
110310a83cb9SPrem Mallappa static const VMStateDescription vmstate_smmuv3 = {
110410a83cb9SPrem Mallappa     .name = "smmuv3",
110510a83cb9SPrem Mallappa     .version_id = 1,
110610a83cb9SPrem Mallappa     .minimum_version_id = 1,
110710a83cb9SPrem Mallappa     .fields = (VMStateField[]) {
110810a83cb9SPrem Mallappa         VMSTATE_UINT32(features, SMMUv3State),
110910a83cb9SPrem Mallappa         VMSTATE_UINT8(sid_size, SMMUv3State),
111010a83cb9SPrem Mallappa         VMSTATE_UINT8(sid_split, SMMUv3State),
111110a83cb9SPrem Mallappa 
111210a83cb9SPrem Mallappa         VMSTATE_UINT32_ARRAY(cr, SMMUv3State, 3),
111310a83cb9SPrem Mallappa         VMSTATE_UINT32(cr0ack, SMMUv3State),
111410a83cb9SPrem Mallappa         VMSTATE_UINT32(statusr, SMMUv3State),
111510a83cb9SPrem Mallappa         VMSTATE_UINT32(irq_ctrl, SMMUv3State),
111610a83cb9SPrem Mallappa         VMSTATE_UINT32(gerror, SMMUv3State),
111710a83cb9SPrem Mallappa         VMSTATE_UINT32(gerrorn, SMMUv3State),
111810a83cb9SPrem Mallappa         VMSTATE_UINT64(gerror_irq_cfg0, SMMUv3State),
111910a83cb9SPrem Mallappa         VMSTATE_UINT32(gerror_irq_cfg1, SMMUv3State),
112010a83cb9SPrem Mallappa         VMSTATE_UINT32(gerror_irq_cfg2, SMMUv3State),
112110a83cb9SPrem Mallappa         VMSTATE_UINT64(strtab_base, SMMUv3State),
112210a83cb9SPrem Mallappa         VMSTATE_UINT32(strtab_base_cfg, SMMUv3State),
112310a83cb9SPrem Mallappa         VMSTATE_UINT64(eventq_irq_cfg0, SMMUv3State),
112410a83cb9SPrem Mallappa         VMSTATE_UINT32(eventq_irq_cfg1, SMMUv3State),
112510a83cb9SPrem Mallappa         VMSTATE_UINT32(eventq_irq_cfg2, SMMUv3State),
112610a83cb9SPrem Mallappa 
112710a83cb9SPrem Mallappa         VMSTATE_STRUCT(cmdq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue),
112810a83cb9SPrem Mallappa         VMSTATE_STRUCT(eventq, SMMUv3State, 0, vmstate_smmuv3_queue, SMMUQueue),
112910a83cb9SPrem Mallappa 
113010a83cb9SPrem Mallappa         VMSTATE_END_OF_LIST(),
113110a83cb9SPrem Mallappa     },
113210a83cb9SPrem Mallappa };
113310a83cb9SPrem Mallappa 
113410a83cb9SPrem Mallappa static void smmuv3_instance_init(Object *obj)
113510a83cb9SPrem Mallappa {
113610a83cb9SPrem Mallappa     /* Nothing much to do here as of now */
113710a83cb9SPrem Mallappa }
113810a83cb9SPrem Mallappa 
113910a83cb9SPrem Mallappa static void smmuv3_class_init(ObjectClass *klass, void *data)
114010a83cb9SPrem Mallappa {
114110a83cb9SPrem Mallappa     DeviceClass *dc = DEVICE_CLASS(klass);
114210a83cb9SPrem Mallappa     SMMUv3Class *c = ARM_SMMUV3_CLASS(klass);
114310a83cb9SPrem Mallappa 
114410a83cb9SPrem Mallappa     dc->vmsd = &vmstate_smmuv3;
114510a83cb9SPrem Mallappa     device_class_set_parent_reset(dc, smmu_reset, &c->parent_reset);
114610a83cb9SPrem Mallappa     c->parent_realize = dc->realize;
114710a83cb9SPrem Mallappa     dc->realize = smmu_realize;
114810a83cb9SPrem Mallappa }
114910a83cb9SPrem Mallappa 
11500d1ac82eSEric Auger static void smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu,
11510d1ac82eSEric Auger                                        IOMMUNotifierFlag old,
11520d1ac82eSEric Auger                                        IOMMUNotifierFlag new)
11530d1ac82eSEric Auger {
11540d1ac82eSEric Auger     if (old == IOMMU_NOTIFIER_NONE) {
11550d1ac82eSEric Auger         warn_report("SMMUV3 does not support vhost/vfio integration yet: "
11560d1ac82eSEric Auger                     "devices of those types will not function properly");
11570d1ac82eSEric Auger     }
11580d1ac82eSEric Auger }
11590d1ac82eSEric Auger 
116010a83cb9SPrem Mallappa static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass,
116110a83cb9SPrem Mallappa                                                   void *data)
116210a83cb9SPrem Mallappa {
11639bde7f06SEric Auger     IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
11649bde7f06SEric Auger 
11659bde7f06SEric Auger     imrc->translate = smmuv3_translate;
11660d1ac82eSEric Auger     imrc->notify_flag_changed = smmuv3_notify_flag_changed;
116710a83cb9SPrem Mallappa }
116810a83cb9SPrem Mallappa 
116910a83cb9SPrem Mallappa static const TypeInfo smmuv3_type_info = {
117010a83cb9SPrem Mallappa     .name          = TYPE_ARM_SMMUV3,
117110a83cb9SPrem Mallappa     .parent        = TYPE_ARM_SMMU,
117210a83cb9SPrem Mallappa     .instance_size = sizeof(SMMUv3State),
117310a83cb9SPrem Mallappa     .instance_init = smmuv3_instance_init,
117410a83cb9SPrem Mallappa     .class_size    = sizeof(SMMUv3Class),
117510a83cb9SPrem Mallappa     .class_init    = smmuv3_class_init,
117610a83cb9SPrem Mallappa };
117710a83cb9SPrem Mallappa 
117810a83cb9SPrem Mallappa static const TypeInfo smmuv3_iommu_memory_region_info = {
117910a83cb9SPrem Mallappa     .parent = TYPE_IOMMU_MEMORY_REGION,
118010a83cb9SPrem Mallappa     .name = TYPE_SMMUV3_IOMMU_MEMORY_REGION,
118110a83cb9SPrem Mallappa     .class_init = smmuv3_iommu_memory_region_class_init,
118210a83cb9SPrem Mallappa };
118310a83cb9SPrem Mallappa 
118410a83cb9SPrem Mallappa static void smmuv3_register_types(void)
118510a83cb9SPrem Mallappa {
118610a83cb9SPrem Mallappa     type_register(&smmuv3_type_info);
118710a83cb9SPrem Mallappa     type_register(&smmuv3_iommu_memory_region_info);
118810a83cb9SPrem Mallappa }
118910a83cb9SPrem Mallappa 
119010a83cb9SPrem Mallappa type_init(smmuv3_register_types)
119110a83cb9SPrem Mallappa 
1192