2 * Copyright (C) 2014-2016 Broadcom Corporation
3 * Copyright (c) 2017 Red Hat, Inc.
4 * Written by Prem Mallappa, Eric Auger
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qemu/bitops.h"
22 #include "hw/sysbus.h"
23 #include "migration/vmstate.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/qdev-core.h"
26 #include "hw/pci/pci.h"
30 #include "qemu/error-report.h"
31 #include "qapi/error.h"
33 #include "hw/arm/smmuv3.h"
34 #include "smmuv3-internal.h"
35 #include "smmu-internal.h"
37 #define PTW_RECORD_FAULT(cfg) (((cfg)->stage == 1) ? (cfg)->record_faults : \
38 (cfg)->s2cfg.record_faults)
41 * smmuv3_trigger_irq - pulse @irq if enabled and update
42 * GERROR register in case of GERROR interrupt
45 * @gerror_mask: mask of gerrors to toggle (relevant if @irq is GERROR)
47 static void smmuv3_trigger_irq(SMMUv3State
*s
, SMMUIrq irq
,
55 pulse
= smmuv3_eventq_irq_enabled(s
);
58 qemu_log_mask(LOG_UNIMP
, "PRI not yet supported\n");
60 case SMMU_IRQ_CMD_SYNC
:
65 uint32_t pending
= s
->gerror
^ s
->gerrorn
;
66 uint32_t new_gerrors
= ~pending
& gerror_mask
;
69 /* only toggle non pending errors */
72 s
->gerror
^= new_gerrors
;
73 trace_smmuv3_write_gerror(new_gerrors
, s
->gerror
);
75 pulse
= smmuv3_gerror_irq_enabled(s
);
80 trace_smmuv3_trigger_irq(irq
);
81 qemu_irq_pulse(s
->irq
[irq
]);
85 static void smmuv3_write_gerrorn(SMMUv3State
*s
, uint32_t new_gerrorn
)
87 uint32_t pending
= s
->gerror
^ s
->gerrorn
;
88 uint32_t toggled
= s
->gerrorn
^ new_gerrorn
;
90 if (toggled
& ~pending
) {
91 qemu_log_mask(LOG_GUEST_ERROR
,
92 "guest toggles non pending errors = 0x%x\n",
97 * We do not raise any error in case guest toggles bits corresponding
98 * to not active IRQs (CONSTRAINED UNPREDICTABLE)
100 s
->gerrorn
= new_gerrorn
;
102 trace_smmuv3_write_gerrorn(toggled
& pending
, s
->gerrorn
);
105 static inline MemTxResult
queue_read(SMMUQueue
*q
, Cmd
*cmd
)
107 dma_addr_t addr
= Q_CONS_ENTRY(q
);
111 ret
= dma_memory_read(&address_space_memory
, addr
, cmd
, sizeof(Cmd
),
112 MEMTXATTRS_UNSPECIFIED
);
113 if (ret
!= MEMTX_OK
) {
116 for (i
= 0; i
< ARRAY_SIZE(cmd
->word
); i
++) {
117 le32_to_cpus(&cmd
->word
[i
]);
122 static MemTxResult
queue_write(SMMUQueue
*q
, Evt
*evt_in
)
124 dma_addr_t addr
= Q_PROD_ENTRY(q
);
129 for (i
= 0; i
< ARRAY_SIZE(evt
.word
); i
++) {
130 cpu_to_le32s(&evt
.word
[i
]);
132 ret
= dma_memory_write(&address_space_memory
, addr
, &evt
, sizeof(Evt
),
133 MEMTXATTRS_UNSPECIFIED
);
134 if (ret
!= MEMTX_OK
) {
142 static MemTxResult
smmuv3_write_eventq(SMMUv3State
*s
, Evt
*evt
)
144 SMMUQueue
*q
= &s
->eventq
;
147 if (!smmuv3_eventq_enabled(s
)) {
151 if (smmuv3_q_full(q
)) {
155 r
= queue_write(q
, evt
);
160 if (!smmuv3_q_empty(q
)) {
161 smmuv3_trigger_irq(s
, SMMU_IRQ_EVTQ
, 0);
166 void smmuv3_record_event(SMMUv3State
*s
, SMMUEventInfo
*info
)
171 if (!smmuv3_eventq_enabled(s
)) {
175 EVT_SET_TYPE(&evt
, info
->type
);
176 EVT_SET_SID(&evt
, info
->sid
);
178 switch (info
->type
) {
182 EVT_SET_SSID(&evt
, info
->u
.f_uut
.ssid
);
183 EVT_SET_SSV(&evt
, info
->u
.f_uut
.ssv
);
184 EVT_SET_ADDR(&evt
, info
->u
.f_uut
.addr
);
185 EVT_SET_RNW(&evt
, info
->u
.f_uut
.rnw
);
186 EVT_SET_PNU(&evt
, info
->u
.f_uut
.pnu
);
187 EVT_SET_IND(&evt
, info
->u
.f_uut
.ind
);
189 case SMMU_EVT_C_BAD_STREAMID
:
190 EVT_SET_SSID(&evt
, info
->u
.c_bad_streamid
.ssid
);
191 EVT_SET_SSV(&evt
, info
->u
.c_bad_streamid
.ssv
);
193 case SMMU_EVT_F_STE_FETCH
:
194 EVT_SET_SSID(&evt
, info
->u
.f_ste_fetch
.ssid
);
195 EVT_SET_SSV(&evt
, info
->u
.f_ste_fetch
.ssv
);
196 EVT_SET_ADDR2(&evt
, info
->u
.f_ste_fetch
.addr
);
198 case SMMU_EVT_C_BAD_STE
:
199 EVT_SET_SSID(&evt
, info
->u
.c_bad_ste
.ssid
);
200 EVT_SET_SSV(&evt
, info
->u
.c_bad_ste
.ssv
);
202 case SMMU_EVT_F_STREAM_DISABLED
:
204 case SMMU_EVT_F_TRANS_FORBIDDEN
:
205 EVT_SET_ADDR(&evt
, info
->u
.f_transl_forbidden
.addr
);
206 EVT_SET_RNW(&evt
, info
->u
.f_transl_forbidden
.rnw
);
208 case SMMU_EVT_C_BAD_SUBSTREAMID
:
209 EVT_SET_SSID(&evt
, info
->u
.c_bad_substream
.ssid
);
211 case SMMU_EVT_F_CD_FETCH
:
212 EVT_SET_SSID(&evt
, info
->u
.f_cd_fetch
.ssid
);
213 EVT_SET_SSV(&evt
, info
->u
.f_cd_fetch
.ssv
);
214 EVT_SET_ADDR(&evt
, info
->u
.f_cd_fetch
.addr
);
216 case SMMU_EVT_C_BAD_CD
:
217 EVT_SET_SSID(&evt
, info
->u
.c_bad_cd
.ssid
);
218 EVT_SET_SSV(&evt
, info
->u
.c_bad_cd
.ssv
);
220 case SMMU_EVT_F_WALK_EABT
:
221 case SMMU_EVT_F_TRANSLATION
:
222 case SMMU_EVT_F_ADDR_SIZE
:
223 case SMMU_EVT_F_ACCESS
:
224 case SMMU_EVT_F_PERMISSION
:
225 EVT_SET_STALL(&evt
, info
->u
.f_walk_eabt
.stall
);
226 EVT_SET_STAG(&evt
, info
->u
.f_walk_eabt
.stag
);
227 EVT_SET_SSID(&evt
, info
->u
.f_walk_eabt
.ssid
);
228 EVT_SET_SSV(&evt
, info
->u
.f_walk_eabt
.ssv
);
229 EVT_SET_S2(&evt
, info
->u
.f_walk_eabt
.s2
);
230 EVT_SET_ADDR(&evt
, info
->u
.f_walk_eabt
.addr
);
231 EVT_SET_RNW(&evt
, info
->u
.f_walk_eabt
.rnw
);
232 EVT_SET_PNU(&evt
, info
->u
.f_walk_eabt
.pnu
);
233 EVT_SET_IND(&evt
, info
->u
.f_walk_eabt
.ind
);
234 EVT_SET_CLASS(&evt
, info
->u
.f_walk_eabt
.class);
235 EVT_SET_ADDR2(&evt
, info
->u
.f_walk_eabt
.addr2
);
237 case SMMU_EVT_F_CFG_CONFLICT
:
238 EVT_SET_SSID(&evt
, info
->u
.f_cfg_conflict
.ssid
);
239 EVT_SET_SSV(&evt
, info
->u
.f_cfg_conflict
.ssv
);
241 /* rest is not implemented */
242 case SMMU_EVT_F_BAD_ATS_TREQ
:
243 case SMMU_EVT_F_TLB_CONFLICT
:
244 case SMMU_EVT_E_PAGE_REQ
:
246 g_assert_not_reached();
249 trace_smmuv3_record_event(smmu_event_string(info
->type
), info
->sid
);
250 r
= smmuv3_write_eventq(s
, &evt
);
252 smmuv3_trigger_irq(s
, SMMU_IRQ_GERROR
, R_GERROR_EVENTQ_ABT_ERR_MASK
);
254 info
->recorded
= true;
257 static void smmuv3_init_regs(SMMUv3State
*s
)
259 /* Based on sys property, the stages supported in smmu will be advertised.*/
260 if (s
->stage
&& !strcmp("2", s
->stage
)) {
261 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, S2P
, 1);
263 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, S1P
, 1);
266 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, TTF
, 2); /* AArch64 PTW only */
267 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, COHACC
, 1); /* IO coherent */
268 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, ASID16
, 1); /* 16-bit ASID */
269 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, VMID16
, 1); /* 16-bit VMID */
270 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, TTENDIAN
, 2); /* little endian */
271 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, STALL_MODEL
, 1); /* No stall */
272 /* terminated transaction will always be aborted/error returned */
273 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, TERM_MODEL
, 1);
274 /* 2-level stream table supported */
275 s
->idr
[0] = FIELD_DP32(s
->idr
[0], IDR0
, STLEVEL
, 1);
277 s
->idr
[1] = FIELD_DP32(s
->idr
[1], IDR1
, SIDSIZE
, SMMU_IDR1_SIDSIZE
);
278 s
->idr
[1] = FIELD_DP32(s
->idr
[1], IDR1
, EVENTQS
, SMMU_EVENTQS
);
279 s
->idr
[1] = FIELD_DP32(s
->idr
[1], IDR1
, CMDQS
, SMMU_CMDQS
);
281 s
->idr
[3] = FIELD_DP32(s
->idr
[3], IDR3
, HAD
, 1);
282 if (FIELD_EX32(s
->idr
[0], IDR0
, S2P
)) {
283 /* XNX is a stage-2-specific feature */
284 s
->idr
[3] = FIELD_DP32(s
->idr
[3], IDR3
, XNX
, 1);
286 s
->idr
[3] = FIELD_DP32(s
->idr
[3], IDR3
, RIL
, 1);
287 s
->idr
[3] = FIELD_DP32(s
->idr
[3], IDR3
, BBML
, 2);
289 s
->idr
[5] = FIELD_DP32(s
->idr
[5], IDR5
, OAS
, SMMU_IDR5_OAS
); /* 44 bits */
290 /* 4K, 16K and 64K granule support */
291 s
->idr
[5] = FIELD_DP32(s
->idr
[5], IDR5
, GRAN4K
, 1);
292 s
->idr
[5] = FIELD_DP32(s
->idr
[5], IDR5
, GRAN16K
, 1);
293 s
->idr
[5] = FIELD_DP32(s
->idr
[5], IDR5
, GRAN64K
, 1);
295 s
->cmdq
.base
= deposit64(s
->cmdq
.base
, 0, 5, SMMU_CMDQS
);
298 s
->cmdq
.entry_size
= sizeof(struct Cmd
);
299 s
->eventq
.base
= deposit64(s
->eventq
.base
, 0, 5, SMMU_EVENTQS
);
302 s
->eventq
.entry_size
= sizeof(struct Evt
);
313 s
->gbpa
= SMMU_GBPA_RESET_VAL
;
316 static int smmu_get_ste(SMMUv3State
*s
, dma_addr_t addr
, STE
*buf
,
317 SMMUEventInfo
*event
)
321 trace_smmuv3_get_ste(addr
);
322 /* TODO: guarantee 64-bit single-copy atomicity */
323 ret
= dma_memory_read(&address_space_memory
, addr
, buf
, sizeof(*buf
),
324 MEMTXATTRS_UNSPECIFIED
);
325 if (ret
!= MEMTX_OK
) {
326 qemu_log_mask(LOG_GUEST_ERROR
,
327 "Cannot fetch pte at address=0x%"PRIx64
"\n", addr
);
328 event
->type
= SMMU_EVT_F_STE_FETCH
;
329 event
->u
.f_ste_fetch
.addr
= addr
;
332 for (i
= 0; i
< ARRAY_SIZE(buf
->word
); i
++) {
333 le32_to_cpus(&buf
->word
[i
]);
339 /* @ssid > 0 not supported yet */
340 static int smmu_get_cd(SMMUv3State
*s
, STE
*ste
, uint32_t ssid
,
341 CD
*buf
, SMMUEventInfo
*event
)
343 dma_addr_t addr
= STE_CTXPTR(ste
);
346 trace_smmuv3_get_cd(addr
);
347 /* TODO: guarantee 64-bit single-copy atomicity */
348 ret
= dma_memory_read(&address_space_memory
, addr
, buf
, sizeof(*buf
),
349 MEMTXATTRS_UNSPECIFIED
);
350 if (ret
!= MEMTX_OK
) {
351 qemu_log_mask(LOG_GUEST_ERROR
,
352 "Cannot fetch pte at address=0x%"PRIx64
"\n", addr
);
353 event
->type
= SMMU_EVT_F_CD_FETCH
;
354 event
->u
.f_ste_fetch
.addr
= addr
;
357 for (i
= 0; i
< ARRAY_SIZE(buf
->word
); i
++) {
358 le32_to_cpus(&buf
->word
[i
]);
364 * Max valid value is 39 when SMMU_IDR3.STT == 0.
365 * In architectures after SMMUv3.0:
366 * - If STE.S2TG selects a 4KB or 16KB granule, the minimum valid value for this
367 * field is MAX(16, 64-IAS)
368 * - If STE.S2TG selects a 64KB granule, the minimum valid value for this field
370 * As we only support AA64, IAS = OAS.
372 static bool s2t0sz_valid(SMMUTransCfg
*cfg
)
374 if (cfg
->s2cfg
.tsz
> 39) {
378 if (cfg
->s2cfg
.granule_sz
== 16) {
379 return (cfg
->s2cfg
.tsz
>= 64 - oas2bits(SMMU_IDR5_OAS
));
382 return (cfg
->s2cfg
.tsz
>= MAX(64 - oas2bits(SMMU_IDR5_OAS
), 16));
386 * Return true if s2 page table config is valid.
387 * This checks with the configured start level, ias_bits and granularity we can
388 * have a valid page table as described in ARM ARM D8.2 Translation process.
389 * The idea here is to see for the highest possible number of IPA bits, how
390 * many concatenated tables we would need, if it is more than 16, then this is
393 static bool s2_pgtable_config_valid(uint8_t sl0
, uint8_t t0sz
, uint8_t gran
)
395 int level
= get_start_level(sl0
, gran
);
396 uint64_t ipa_bits
= 64 - t0sz
;
397 uint64_t max_ipa
= (1ULL << ipa_bits
) - 1;
398 int nr_concat
= pgd_concat_idx(level
, gran
, max_ipa
) + 1;
400 return nr_concat
<= VMSA_MAX_S2_CONCAT
;
403 static int decode_ste_s2_cfg(SMMUTransCfg
*cfg
, STE
*ste
)
407 if (STE_S2AA64(ste
) == 0x0) {
408 qemu_log_mask(LOG_UNIMP
,
409 "SMMUv3 AArch32 tables not supported\n");
410 g_assert_not_reached();
413 switch (STE_S2TG(ste
)) {
415 cfg
->s2cfg
.granule_sz
= 12;
418 cfg
->s2cfg
.granule_sz
= 16;
421 cfg
->s2cfg
.granule_sz
= 14;
424 qemu_log_mask(LOG_GUEST_ERROR
,
425 "SMMUv3 bad STE S2TG: %x\n", STE_S2TG(ste
));
429 cfg
->s2cfg
.vttb
= STE_S2TTB(ste
);
431 cfg
->s2cfg
.sl0
= STE_S2SL0(ste
);
432 /* FEAT_TTST not supported. */
433 if (cfg
->s2cfg
.sl0
== 0x3) {
434 qemu_log_mask(LOG_UNIMP
, "SMMUv3 S2SL0 = 0x3 has no meaning!\n");
438 /* For AA64, The effective S2PS size is capped to the OAS. */
439 cfg
->s2cfg
.eff_ps
= oas2bits(MIN(STE_S2PS(ste
), SMMU_IDR5_OAS
));
441 * It is ILLEGAL for the address in S2TTB to be outside the range
442 * described by the effective S2PS value.
444 if (cfg
->s2cfg
.vttb
& ~(MAKE_64BIT_MASK(0, cfg
->s2cfg
.eff_ps
))) {
445 qemu_log_mask(LOG_GUEST_ERROR
,
446 "SMMUv3 S2TTB too large 0x%" PRIx64
447 ", effective PS %d bits\n",
448 cfg
->s2cfg
.vttb
, cfg
->s2cfg
.eff_ps
);
452 cfg
->s2cfg
.tsz
= STE_S2T0SZ(ste
);
454 if (!s2t0sz_valid(cfg
)) {
455 qemu_log_mask(LOG_GUEST_ERROR
, "SMMUv3 bad STE S2T0SZ = %d\n",
460 if (!s2_pgtable_config_valid(cfg
->s2cfg
.sl0
, cfg
->s2cfg
.tsz
,
461 cfg
->s2cfg
.granule_sz
)) {
462 qemu_log_mask(LOG_GUEST_ERROR
,
463 "SMMUv3 STE stage 2 config not valid!\n");
467 /* Only LE supported(IDR0.TTENDIAN). */
468 if (STE_S2ENDI(ste
)) {
469 qemu_log_mask(LOG_GUEST_ERROR
,
470 "SMMUv3 STE_S2ENDI only supports LE!\n");
474 cfg
->s2cfg
.affd
= STE_S2AFFD(ste
);
476 cfg
->s2cfg
.record_faults
= STE_S2R(ste
);
477 /* As stall is not supported. */
479 qemu_log_mask(LOG_UNIMP
, "SMMUv3 Stall not implemented!\n");
489 /* Returns < 0 in case of invalid STE, 0 otherwise */
490 static int decode_ste(SMMUv3State
*s
, SMMUTransCfg
*cfg
,
491 STE
*ste
, SMMUEventInfo
*event
)
496 if (!STE_VALID(ste
)) {
497 if (!event
->inval_ste_allowed
) {
498 qemu_log_mask(LOG_GUEST_ERROR
, "invalid STE\n");
503 config
= STE_CONFIG(ste
);
505 if (STE_CFG_ABORT(config
)) {
510 if (STE_CFG_BYPASS(config
)) {
511 cfg
->bypassed
= true;
516 * If a stage is enabled in SW while not advertised, throw bad ste
517 * according to user manual(IHI0070E) "5.2 Stream Table Entry".
519 if (!STAGE1_SUPPORTED(s
) && STE_CFG_S1_ENABLED(config
)) {
520 qemu_log_mask(LOG_GUEST_ERROR
, "SMMUv3 S1 used but not supported.\n");
523 if (!STAGE2_SUPPORTED(s
) && STE_CFG_S2_ENABLED(config
)) {
524 qemu_log_mask(LOG_GUEST_ERROR
, "SMMUv3 S2 used but not supported.\n");
528 if (STAGE2_SUPPORTED(s
)) {
529 /* VMID is considered even if s2 is disabled. */
530 cfg
->s2cfg
.vmid
= STE_S2VMID(ste
);
533 cfg
->s2cfg
.vmid
= -1;
536 if (STE_CFG_S2_ENABLED(config
)) {
538 * Stage-1 OAS defaults to OAS even if not enabled as it would be used
539 * in input address check for stage-2.
541 cfg
->oas
= oas2bits(SMMU_IDR5_OAS
);
542 ret
= decode_ste_s2_cfg(cfg
, ste
);
548 if (STE_S1CDMAX(ste
) != 0) {
549 qemu_log_mask(LOG_UNIMP
,
550 "SMMUv3 does not support multiple context descriptors yet\n");
554 if (STE_S1STALLD(ste
)) {
555 qemu_log_mask(LOG_UNIMP
,
556 "SMMUv3 S1 stalling fault model not allowed yet\n");
562 event
->type
= SMMU_EVT_C_BAD_STE
;
567 * smmu_find_ste - Return the stream table entry associated
572 * @ste: returned stream table entry
573 * @event: handle to an event info
575 * Supports linear and 2-level stream table
576 * Return 0 on success, -EINVAL otherwise
578 static int smmu_find_ste(SMMUv3State
*s
, uint32_t sid
, STE
*ste
,
579 SMMUEventInfo
*event
)
581 dma_addr_t addr
, strtab_base
;
583 int strtab_size_shift
;
586 trace_smmuv3_find_ste(sid
, s
->features
, s
->sid_split
);
587 log2size
= FIELD_EX32(s
->strtab_base_cfg
, STRTAB_BASE_CFG
, LOG2SIZE
);
589 * Check SID range against both guest-configured and implementation limits
591 if (sid
>= (1 << MIN(log2size
, SMMU_IDR1_SIDSIZE
))) {
592 event
->type
= SMMU_EVT_C_BAD_STREAMID
;
595 if (s
->features
& SMMU_FEATURE_2LVL_STE
) {
596 int l1_ste_offset
, l2_ste_offset
, max_l2_ste
, span
, i
;
597 dma_addr_t l1ptr
, l2ptr
;
601 * Align strtab base address to table size. For this purpose, assume it
602 * is not bounded by SMMU_IDR1_SIDSIZE.
604 strtab_size_shift
= MAX(5, (int)log2size
- s
->sid_split
- 1 + 3);
605 strtab_base
= s
->strtab_base
& SMMU_BASE_ADDR_MASK
&
606 ~MAKE_64BIT_MASK(0, strtab_size_shift
);
607 l1_ste_offset
= sid
>> s
->sid_split
;
608 l2_ste_offset
= sid
& ((1 << s
->sid_split
) - 1);
609 l1ptr
= (dma_addr_t
)(strtab_base
+ l1_ste_offset
* sizeof(l1std
));
610 /* TODO: guarantee 64-bit single-copy atomicity */
611 ret
= dma_memory_read(&address_space_memory
, l1ptr
, &l1std
,
612 sizeof(l1std
), MEMTXATTRS_UNSPECIFIED
);
613 if (ret
!= MEMTX_OK
) {
614 qemu_log_mask(LOG_GUEST_ERROR
,
615 "Could not read L1PTR at 0X%"PRIx64
"\n", l1ptr
);
616 event
->type
= SMMU_EVT_F_STE_FETCH
;
617 event
->u
.f_ste_fetch
.addr
= l1ptr
;
620 for (i
= 0; i
< ARRAY_SIZE(l1std
.word
); i
++) {
621 le32_to_cpus(&l1std
.word
[i
]);
624 span
= L1STD_SPAN(&l1std
);
627 /* l2ptr is not valid */
628 if (!event
->inval_ste_allowed
) {
629 qemu_log_mask(LOG_GUEST_ERROR
,
630 "invalid sid=%d (L1STD span=0)\n", sid
);
632 event
->type
= SMMU_EVT_C_BAD_STREAMID
;
635 max_l2_ste
= (1 << span
) - 1;
636 l2ptr
= l1std_l2ptr(&l1std
);
637 trace_smmuv3_find_ste_2lvl(s
->strtab_base
, l1ptr
, l1_ste_offset
,
638 l2ptr
, l2_ste_offset
, max_l2_ste
);
639 if (l2_ste_offset
> max_l2_ste
) {
640 qemu_log_mask(LOG_GUEST_ERROR
,
641 "l2_ste_offset=%d > max_l2_ste=%d\n",
642 l2_ste_offset
, max_l2_ste
);
643 event
->type
= SMMU_EVT_C_BAD_STE
;
646 addr
= l2ptr
+ l2_ste_offset
* sizeof(*ste
);
648 strtab_size_shift
= log2size
+ 5;
649 strtab_base
= s
->strtab_base
& SMMU_BASE_ADDR_MASK
&
650 ~MAKE_64BIT_MASK(0, strtab_size_shift
);
651 addr
= strtab_base
+ sid
* sizeof(*ste
);
654 if (smmu_get_ste(s
, addr
, ste
, event
)) {
661 static int decode_cd(SMMUTransCfg
*cfg
, CD
*cd
, SMMUEventInfo
*event
)
666 if (!CD_VALID(cd
) || !CD_AARCH64(cd
)) {
670 goto bad_cd
; /* SMMU_IDR0.TERM_MODEL == 1 */
673 goto bad_cd
; /* !STE_SECURE && SMMU_IDR0.STALL_MODEL == 1 */
675 if (CD_HA(cd
) || CD_HD(cd
)) {
676 goto bad_cd
; /* HTTU = 0 */
679 /* we support only those at the moment */
683 cfg
->oas
= oas2bits(CD_IPS(cd
));
684 cfg
->oas
= MIN(oas2bits(SMMU_IDR5_OAS
), cfg
->oas
);
685 cfg
->tbi
= CD_TBI(cd
);
686 cfg
->asid
= CD_ASID(cd
);
687 cfg
->affd
= CD_AFFD(cd
);
689 trace_smmuv3_decode_cd(cfg
->oas
);
691 /* decode data dependent on TT */
692 for (i
= 0; i
<= 1; i
++) {
694 SMMUTransTableInfo
*tt
= &cfg
->tt
[i
];
696 cfg
->tt
[i
].disabled
= CD_EPD(cd
, i
);
697 if (cfg
->tt
[i
].disabled
) {
702 if (tsz
< 16 || tsz
> 39) {
707 tt
->granule_sz
= tg2granule(tg
, i
);
708 if ((tt
->granule_sz
!= 12 && tt
->granule_sz
!= 14 &&
709 tt
->granule_sz
!= 16) || CD_ENDI(cd
)) {
714 tt
->ttb
= CD_TTB(cd
, i
);
715 if (tt
->ttb
& ~(MAKE_64BIT_MASK(0, cfg
->oas
))) {
718 tt
->had
= CD_HAD(cd
, i
);
719 trace_smmuv3_decode_cd_tt(i
, tt
->tsz
, tt
->ttb
, tt
->granule_sz
, tt
->had
);
722 cfg
->record_faults
= CD_R(cd
);
727 event
->type
= SMMU_EVT_C_BAD_CD
;
732 * smmuv3_decode_config - Prepare the translation configuration
733 * for the @mr iommu region
734 * @mr: iommu memory region the translation config must be prepared for
735 * @cfg: output translation configuration which is populated through
736 * the different configuration decoding steps
737 * @event: must be zero'ed by the caller
739 * return < 0 in case of config decoding error (@event is filled
740 * accordingly). Return 0 otherwise.
742 static int smmuv3_decode_config(IOMMUMemoryRegion
*mr
, SMMUTransCfg
*cfg
,
743 SMMUEventInfo
*event
)
745 SMMUDevice
*sdev
= container_of(mr
, SMMUDevice
, iommu
);
746 uint32_t sid
= smmu_get_sid(sdev
);
747 SMMUv3State
*s
= sdev
->smmu
;
752 /* ASID defaults to -1 (if s1 is not supported). */
755 ret
= smmu_find_ste(s
, sid
, &ste
, event
);
760 ret
= decode_ste(s
, cfg
, &ste
, event
);
765 if (cfg
->aborted
|| cfg
->bypassed
|| (cfg
->stage
== 2)) {
769 ret
= smmu_get_cd(s
, &ste
, 0 /* ssid */, &cd
, event
);
774 return decode_cd(cfg
, &cd
, event
);
778 * smmuv3_get_config - Look up for a cached copy of configuration data for
779 * @sdev and on cache miss performs a configuration structure decoding from
782 * @sdev: SMMUDevice handle
783 * @event: output event info
785 * The configuration cache contains data resulting from both STE and CD
786 * decoding under the form of an SMMUTransCfg struct. The hash table is indexed
787 * by the SMMUDevice handle.
789 static SMMUTransCfg
*smmuv3_get_config(SMMUDevice
*sdev
, SMMUEventInfo
*event
)
791 SMMUv3State
*s
= sdev
->smmu
;
792 SMMUState
*bc
= &s
->smmu_state
;
795 cfg
= g_hash_table_lookup(bc
->configs
, sdev
);
797 sdev
->cfg_cache_hits
++;
798 trace_smmuv3_config_cache_hit(smmu_get_sid(sdev
),
799 sdev
->cfg_cache_hits
, sdev
->cfg_cache_misses
,
800 100 * sdev
->cfg_cache_hits
/
801 (sdev
->cfg_cache_hits
+ sdev
->cfg_cache_misses
));
803 sdev
->cfg_cache_misses
++;
804 trace_smmuv3_config_cache_miss(smmu_get_sid(sdev
),
805 sdev
->cfg_cache_hits
, sdev
->cfg_cache_misses
,
806 100 * sdev
->cfg_cache_hits
/
807 (sdev
->cfg_cache_hits
+ sdev
->cfg_cache_misses
));
808 cfg
= g_new0(SMMUTransCfg
, 1);
810 if (!smmuv3_decode_config(&sdev
->iommu
, cfg
, event
)) {
811 g_hash_table_insert(bc
->configs
, sdev
, cfg
);
820 static void smmuv3_flush_config(SMMUDevice
*sdev
)
822 SMMUv3State
*s
= sdev
->smmu
;
823 SMMUState
*bc
= &s
->smmu_state
;
825 trace_smmuv3_config_cache_inv(smmu_get_sid(sdev
));
826 g_hash_table_remove(bc
->configs
, sdev
);
829 static IOMMUTLBEntry
smmuv3_translate(IOMMUMemoryRegion
*mr
, hwaddr addr
,
830 IOMMUAccessFlags flag
, int iommu_idx
)
832 SMMUDevice
*sdev
= container_of(mr
, SMMUDevice
, iommu
);
833 SMMUv3State
*s
= sdev
->smmu
;
834 uint32_t sid
= smmu_get_sid(sdev
);
835 SMMUEventInfo event
= {.type
= SMMU_EVT_NONE
,
837 .inval_ste_allowed
= false};
838 SMMUPTWEventInfo ptw_info
= {};
839 SMMUTranslationStatus status
;
840 SMMUState
*bs
= ARM_SMMU(s
);
841 uint64_t page_mask
, aligned_addr
;
842 SMMUTLBEntry
*cached_entry
= NULL
;
843 SMMUTransTableInfo
*tt
;
844 SMMUTransCfg
*cfg
= NULL
;
845 IOMMUTLBEntry entry
= {
846 .target_as
= &address_space_memory
,
848 .translated_addr
= addr
,
849 .addr_mask
= ~(hwaddr
)0,
853 * Combined attributes used for TLB lookup, as only one stage is supported,
854 * it will hold attributes based on the enabled stage.
856 SMMUTransTableInfo tt_combined
;
858 qemu_mutex_lock(&s
->mutex
);
860 if (!smmu_enabled(s
)) {
861 if (FIELD_EX32(s
->gbpa
, GBPA
, ABORT
)) {
862 status
= SMMU_TRANS_ABORT
;
864 status
= SMMU_TRANS_DISABLE
;
869 cfg
= smmuv3_get_config(sdev
, &event
);
871 status
= SMMU_TRANS_ERROR
;
876 status
= SMMU_TRANS_ABORT
;
881 status
= SMMU_TRANS_BYPASS
;
885 if (cfg
->stage
== 1) {
886 /* Select stage1 translation table. */
887 tt
= select_tt(cfg
, addr
);
889 if (cfg
->record_faults
) {
890 event
.type
= SMMU_EVT_F_TRANSLATION
;
891 event
.u
.f_translation
.addr
= addr
;
892 event
.u
.f_translation
.rnw
= flag
& 0x1;
894 status
= SMMU_TRANS_ERROR
;
897 tt_combined
.granule_sz
= tt
->granule_sz
;
898 tt_combined
.tsz
= tt
->tsz
;
902 tt_combined
.granule_sz
= cfg
->s2cfg
.granule_sz
;
903 tt_combined
.tsz
= cfg
->s2cfg
.tsz
;
906 * TLB lookup looks for granule and input size for a translation stage,
907 * as only one stage is supported right now, choose the right values
908 * from the configuration.
910 page_mask
= (1ULL << tt_combined
.granule_sz
) - 1;
911 aligned_addr
= addr
& ~page_mask
;
913 cached_entry
= smmu_iotlb_lookup(bs
, cfg
, &tt_combined
, aligned_addr
);
915 if ((flag
& IOMMU_WO
) && !(cached_entry
->entry
.perm
& IOMMU_WO
)) {
916 status
= SMMU_TRANS_ERROR
;
918 * We know that the TLB only contains either stage-1 or stage-2 as
919 * nesting is not supported. So it is sufficient to check the
920 * translation stage to know the TLB stage for now.
922 event
.u
.f_walk_eabt
.s2
= (cfg
->stage
== 2);
923 if (PTW_RECORD_FAULT(cfg
)) {
924 event
.type
= SMMU_EVT_F_PERMISSION
;
925 event
.u
.f_permission
.addr
= addr
;
926 event
.u
.f_permission
.rnw
= flag
& 0x1;
929 status
= SMMU_TRANS_SUCCESS
;
934 cached_entry
= g_new0(SMMUTLBEntry
, 1);
936 if (smmu_ptw(cfg
, aligned_addr
, flag
, cached_entry
, &ptw_info
)) {
937 /* All faults from PTW has S2 field. */
938 event
.u
.f_walk_eabt
.s2
= (ptw_info
.stage
== 2);
939 g_free(cached_entry
);
940 switch (ptw_info
.type
) {
941 case SMMU_PTW_ERR_WALK_EABT
:
942 event
.type
= SMMU_EVT_F_WALK_EABT
;
943 event
.u
.f_walk_eabt
.addr
= addr
;
944 event
.u
.f_walk_eabt
.rnw
= flag
& 0x1;
945 /* Stage-2 (only) is class IN while stage-1 is class TT */
946 event
.u
.f_walk_eabt
.class = (ptw_info
.stage
== 2) ?
947 SMMU_CLASS_IN
: SMMU_CLASS_TT
;
948 event
.u
.f_walk_eabt
.addr2
= ptw_info
.addr
;
950 case SMMU_PTW_ERR_TRANSLATION
:
951 if (PTW_RECORD_FAULT(cfg
)) {
952 event
.type
= SMMU_EVT_F_TRANSLATION
;
953 event
.u
.f_translation
.addr
= addr
;
954 event
.u
.f_translation
.addr2
= ptw_info
.addr
;
955 event
.u
.f_translation
.class = SMMU_CLASS_IN
;
956 event
.u
.f_translation
.rnw
= flag
& 0x1;
959 case SMMU_PTW_ERR_ADDR_SIZE
:
960 if (PTW_RECORD_FAULT(cfg
)) {
961 event
.type
= SMMU_EVT_F_ADDR_SIZE
;
962 event
.u
.f_addr_size
.addr
= addr
;
963 event
.u
.f_addr_size
.addr2
= ptw_info
.addr
;
964 event
.u
.f_translation
.class = SMMU_CLASS_IN
;
965 event
.u
.f_addr_size
.rnw
= flag
& 0x1;
968 case SMMU_PTW_ERR_ACCESS
:
969 if (PTW_RECORD_FAULT(cfg
)) {
970 event
.type
= SMMU_EVT_F_ACCESS
;
971 event
.u
.f_access
.addr
= addr
;
972 event
.u
.f_access
.addr2
= ptw_info
.addr
;
973 event
.u
.f_translation
.class = SMMU_CLASS_IN
;
974 event
.u
.f_access
.rnw
= flag
& 0x1;
977 case SMMU_PTW_ERR_PERMISSION
:
978 if (PTW_RECORD_FAULT(cfg
)) {
979 event
.type
= SMMU_EVT_F_PERMISSION
;
980 event
.u
.f_permission
.addr
= addr
;
981 event
.u
.f_permission
.addr2
= ptw_info
.addr
;
982 event
.u
.f_translation
.class = SMMU_CLASS_IN
;
983 event
.u
.f_permission
.rnw
= flag
& 0x1;
987 g_assert_not_reached();
989 status
= SMMU_TRANS_ERROR
;
991 smmu_iotlb_insert(bs
, cfg
, cached_entry
);
992 status
= SMMU_TRANS_SUCCESS
;
996 qemu_mutex_unlock(&s
->mutex
);
998 case SMMU_TRANS_SUCCESS
:
999 entry
.perm
= cached_entry
->entry
.perm
;
1000 entry
.translated_addr
= cached_entry
->entry
.translated_addr
+
1001 (addr
& cached_entry
->entry
.addr_mask
);
1002 entry
.addr_mask
= cached_entry
->entry
.addr_mask
;
1003 trace_smmuv3_translate_success(mr
->parent_obj
.name
, sid
, addr
,
1004 entry
.translated_addr
, entry
.perm
);
1006 case SMMU_TRANS_DISABLE
:
1008 entry
.addr_mask
= ~TARGET_PAGE_MASK
;
1009 trace_smmuv3_translate_disable(mr
->parent_obj
.name
, sid
, addr
,
1012 case SMMU_TRANS_BYPASS
:
1014 entry
.addr_mask
= ~TARGET_PAGE_MASK
;
1015 trace_smmuv3_translate_bypass(mr
->parent_obj
.name
, sid
, addr
,
1018 case SMMU_TRANS_ABORT
:
1019 /* no event is recorded on abort */
1020 trace_smmuv3_translate_abort(mr
->parent_obj
.name
, sid
, addr
,
1023 case SMMU_TRANS_ERROR
:
1024 qemu_log_mask(LOG_GUEST_ERROR
,
1025 "%s translation failed for iova=0x%"PRIx64
" (%s)\n",
1026 mr
->parent_obj
.name
, addr
, smmu_event_string(event
.type
));
1027 smmuv3_record_event(s
, &event
);
1035 * smmuv3_notify_iova - call the notifier @n for a given
1036 * @asid and @iova tuple.
1038 * @mr: IOMMU mr region handle
1039 * @n: notifier to be called
1040 * @asid: address space ID or negative value if we don't care
1041 * @vmid: virtual machine ID or negative value if we don't care
1043 * @tg: translation granule (if communicated through range invalidation)
1044 * @num_pages: number of @granule sized pages (if tg != 0), otherwise 1
1046 static void smmuv3_notify_iova(IOMMUMemoryRegion
*mr
,
1049 dma_addr_t iova
, uint8_t tg
,
1052 SMMUDevice
*sdev
= container_of(mr
, SMMUDevice
, iommu
);
1053 IOMMUTLBEvent event
;
1055 SMMUv3State
*s
= sdev
->smmu
;
1058 SMMUEventInfo eventinfo
= {.inval_ste_allowed
= true};
1059 SMMUTransCfg
*cfg
= smmuv3_get_config(sdev
, &eventinfo
);
1060 SMMUTransTableInfo
*tt
;
1066 if (asid
>= 0 && cfg
->asid
!= asid
) {
1070 if (vmid
>= 0 && cfg
->s2cfg
.vmid
!= vmid
) {
1074 if (STAGE1_SUPPORTED(s
)) {
1075 tt
= select_tt(cfg
, iova
);
1079 granule
= tt
->granule_sz
;
1081 granule
= cfg
->s2cfg
.granule_sz
;
1085 granule
= tg
* 2 + 10;
1088 event
.type
= IOMMU_NOTIFIER_UNMAP
;
1089 event
.entry
.target_as
= &address_space_memory
;
1090 event
.entry
.iova
= iova
;
1091 event
.entry
.addr_mask
= num_pages
* (1 << granule
) - 1;
1092 event
.entry
.perm
= IOMMU_NONE
;
1094 memory_region_notify_iommu_one(n
, &event
);
1097 /* invalidate an asid/vmid/iova range tuple in all mr's */
1098 static void smmuv3_inv_notifiers_iova(SMMUState
*s
, int asid
, int vmid
,
1099 dma_addr_t iova
, uint8_t tg
,
1104 QLIST_FOREACH(sdev
, &s
->devices_with_notifiers
, next
) {
1105 IOMMUMemoryRegion
*mr
= &sdev
->iommu
;
1108 trace_smmuv3_inv_notifiers_iova(mr
->parent_obj
.name
, asid
, vmid
,
1109 iova
, tg
, num_pages
);
1111 IOMMU_NOTIFIER_FOREACH(n
, mr
) {
1112 smmuv3_notify_iova(mr
, n
, asid
, vmid
, iova
, tg
, num_pages
);
1117 static void smmuv3_range_inval(SMMUState
*s
, Cmd
*cmd
)
1119 dma_addr_t end
, addr
= CMD_ADDR(cmd
);
1120 uint8_t type
= CMD_TYPE(cmd
);
1122 uint8_t scale
= CMD_SCALE(cmd
);
1123 uint8_t num
= CMD_NUM(cmd
);
1124 uint8_t ttl
= CMD_TTL(cmd
);
1125 bool leaf
= CMD_LEAF(cmd
);
1126 uint8_t tg
= CMD_TG(cmd
);
1130 SMMUv3State
*smmuv3
= ARM_SMMUV3(s
);
1132 /* Only consider VMID if stage-2 is supported. */
1133 if (STAGE2_SUPPORTED(smmuv3
)) {
1134 vmid
= CMD_VMID(cmd
);
1137 if (type
== SMMU_CMD_TLBI_NH_VA
) {
1138 asid
= CMD_ASID(cmd
);
1142 trace_smmuv3_range_inval(vmid
, asid
, addr
, tg
, 1, ttl
, leaf
);
1143 smmuv3_inv_notifiers_iova(s
, asid
, vmid
, addr
, tg
, 1);
1144 smmu_iotlb_inv_iova(s
, asid
, vmid
, addr
, tg
, 1, ttl
);
1150 num_pages
= (num
+ 1) * BIT_ULL(scale
);
1151 granule
= tg
* 2 + 10;
1153 /* Split invalidations into ^2 range invalidations */
1154 end
= addr
+ (num_pages
<< granule
) - 1;
1156 while (addr
!= end
+ 1) {
1157 uint64_t mask
= dma_aligned_pow2_mask(addr
, end
, 64);
1159 num_pages
= (mask
+ 1) >> granule
;
1160 trace_smmuv3_range_inval(vmid
, asid
, addr
, tg
, num_pages
, ttl
, leaf
);
1161 smmuv3_inv_notifiers_iova(s
, asid
, vmid
, addr
, tg
, num_pages
);
1162 smmu_iotlb_inv_iova(s
, asid
, vmid
, addr
, tg
, num_pages
, ttl
);
1168 smmuv3_invalidate_ste(gpointer key
, gpointer value
, gpointer user_data
)
1170 SMMUDevice
*sdev
= (SMMUDevice
*)key
;
1171 uint32_t sid
= smmu_get_sid(sdev
);
1172 SMMUSIDRange
*sid_range
= (SMMUSIDRange
*)user_data
;
1174 if (sid
< sid_range
->start
|| sid
> sid_range
->end
) {
1177 trace_smmuv3_config_cache_inv(sid
);
1181 static int smmuv3_cmdq_consume(SMMUv3State
*s
)
1183 SMMUState
*bs
= ARM_SMMU(s
);
1184 SMMUCmdError cmd_error
= SMMU_CERROR_NONE
;
1185 SMMUQueue
*q
= &s
->cmdq
;
1186 SMMUCommandType type
= 0;
1188 if (!smmuv3_cmdq_enabled(s
)) {
1192 * some commands depend on register values, typically CR0. In case those
1193 * register values change while handling the command, spec says it
1194 * is UNPREDICTABLE whether the command is interpreted under the new
1198 while (!smmuv3_q_empty(q
)) {
1199 uint32_t pending
= s
->gerror
^ s
->gerrorn
;
1202 trace_smmuv3_cmdq_consume(Q_PROD(q
), Q_CONS(q
),
1203 Q_PROD_WRAP(q
), Q_CONS_WRAP(q
));
1205 if (FIELD_EX32(pending
, GERROR
, CMDQ_ERR
)) {
1209 if (queue_read(q
, &cmd
) != MEMTX_OK
) {
1210 cmd_error
= SMMU_CERROR_ABT
;
1214 type
= CMD_TYPE(&cmd
);
1216 trace_smmuv3_cmdq_opcode(smmu_cmd_string(type
));
1218 qemu_mutex_lock(&s
->mutex
);
1221 if (CMD_SYNC_CS(&cmd
) & CMD_SYNC_SIG_IRQ
) {
1222 smmuv3_trigger_irq(s
, SMMU_IRQ_CMD_SYNC
, 0);
1225 case SMMU_CMD_PREFETCH_CONFIG
:
1226 case SMMU_CMD_PREFETCH_ADDR
:
1228 case SMMU_CMD_CFGI_STE
:
1230 uint32_t sid
= CMD_SID(&cmd
);
1231 SMMUDevice
*sdev
= smmu_find_sdev(bs
, sid
);
1233 if (CMD_SSEC(&cmd
)) {
1234 cmd_error
= SMMU_CERROR_ILL
;
1242 trace_smmuv3_cmdq_cfgi_ste(sid
);
1243 smmuv3_flush_config(sdev
);
1247 case SMMU_CMD_CFGI_STE_RANGE
: /* same as SMMU_CMD_CFGI_ALL */
1249 uint32_t sid
= CMD_SID(&cmd
), mask
;
1250 uint8_t range
= CMD_STE_RANGE(&cmd
);
1251 SMMUSIDRange sid_range
;
1253 if (CMD_SSEC(&cmd
)) {
1254 cmd_error
= SMMU_CERROR_ILL
;
1258 mask
= (1ULL << (range
+ 1)) - 1;
1259 sid_range
.start
= sid
& ~mask
;
1260 sid_range
.end
= sid_range
.start
+ mask
;
1262 trace_smmuv3_cmdq_cfgi_ste_range(sid_range
.start
, sid_range
.end
);
1263 g_hash_table_foreach_remove(bs
->configs
, smmuv3_invalidate_ste
,
1267 case SMMU_CMD_CFGI_CD
:
1268 case SMMU_CMD_CFGI_CD_ALL
:
1270 uint32_t sid
= CMD_SID(&cmd
);
1271 SMMUDevice
*sdev
= smmu_find_sdev(bs
, sid
);
1273 if (CMD_SSEC(&cmd
)) {
1274 cmd_error
= SMMU_CERROR_ILL
;
1282 trace_smmuv3_cmdq_cfgi_cd(sid
);
1283 smmuv3_flush_config(sdev
);
1286 case SMMU_CMD_TLBI_NH_ASID
:
1288 uint16_t asid
= CMD_ASID(&cmd
);
1290 if (!STAGE1_SUPPORTED(s
)) {
1291 cmd_error
= SMMU_CERROR_ILL
;
1295 trace_smmuv3_cmdq_tlbi_nh_asid(asid
);
1296 smmu_inv_notifiers_all(&s
->smmu_state
);
1297 smmu_iotlb_inv_asid(bs
, asid
);
1300 case SMMU_CMD_TLBI_NH_ALL
:
1301 if (!STAGE1_SUPPORTED(s
)) {
1302 cmd_error
= SMMU_CERROR_ILL
;
1306 case SMMU_CMD_TLBI_NSNH_ALL
:
1307 trace_smmuv3_cmdq_tlbi_nh();
1308 smmu_inv_notifiers_all(&s
->smmu_state
);
1309 smmu_iotlb_inv_all(bs
);
1311 case SMMU_CMD_TLBI_NH_VAA
:
1312 case SMMU_CMD_TLBI_NH_VA
:
1313 if (!STAGE1_SUPPORTED(s
)) {
1314 cmd_error
= SMMU_CERROR_ILL
;
1317 smmuv3_range_inval(bs
, &cmd
);
1319 case SMMU_CMD_TLBI_S12_VMALL
:
1321 uint16_t vmid
= CMD_VMID(&cmd
);
1323 if (!STAGE2_SUPPORTED(s
)) {
1324 cmd_error
= SMMU_CERROR_ILL
;
1328 trace_smmuv3_cmdq_tlbi_s12_vmid(vmid
);
1329 smmu_inv_notifiers_all(&s
->smmu_state
);
1330 smmu_iotlb_inv_vmid(bs
, vmid
);
1333 case SMMU_CMD_TLBI_S2_IPA
:
1334 if (!STAGE2_SUPPORTED(s
)) {
1335 cmd_error
= SMMU_CERROR_ILL
;
1339 * As currently only either s1 or s2 are supported
1340 * we can reuse same function for s2.
1342 smmuv3_range_inval(bs
, &cmd
);
1344 case SMMU_CMD_TLBI_EL3_ALL
:
1345 case SMMU_CMD_TLBI_EL3_VA
:
1346 case SMMU_CMD_TLBI_EL2_ALL
:
1347 case SMMU_CMD_TLBI_EL2_ASID
:
1348 case SMMU_CMD_TLBI_EL2_VA
:
1349 case SMMU_CMD_TLBI_EL2_VAA
:
1350 case SMMU_CMD_ATC_INV
:
1351 case SMMU_CMD_PRI_RESP
:
1352 case SMMU_CMD_RESUME
:
1353 case SMMU_CMD_STALL_TERM
:
1354 trace_smmuv3_unhandled_cmd(type
);
1357 cmd_error
= SMMU_CERROR_ILL
;
1360 qemu_mutex_unlock(&s
->mutex
);
1362 if (cmd_error
== SMMU_CERROR_ILL
) {
1363 qemu_log_mask(LOG_GUEST_ERROR
,
1364 "Illegal command type: %d\n", CMD_TYPE(&cmd
));
1369 * We only increment the cons index after the completion of
1370 * the command. We do that because the SYNC returns immediately
1371 * and does not check the completion of previous commands
1377 trace_smmuv3_cmdq_consume_error(smmu_cmd_string(type
), cmd_error
);
1378 smmu_write_cmdq_err(s
, cmd_error
);
1379 smmuv3_trigger_irq(s
, SMMU_IRQ_GERROR
, R_GERROR_CMDQ_ERR_MASK
);
1382 trace_smmuv3_cmdq_consume_out(Q_PROD(q
), Q_CONS(q
),
1383 Q_PROD_WRAP(q
), Q_CONS_WRAP(q
));
1388 static MemTxResult
smmu_writell(SMMUv3State
*s
, hwaddr offset
,
1389 uint64_t data
, MemTxAttrs attrs
)
1392 case A_GERROR_IRQ_CFG0
:
1393 s
->gerror_irq_cfg0
= data
;
1396 s
->strtab_base
= data
;
1399 s
->cmdq
.base
= data
;
1400 s
->cmdq
.log2size
= extract64(s
->cmdq
.base
, 0, 5);
1401 if (s
->cmdq
.log2size
> SMMU_CMDQS
) {
1402 s
->cmdq
.log2size
= SMMU_CMDQS
;
1406 s
->eventq
.base
= data
;
1407 s
->eventq
.log2size
= extract64(s
->eventq
.base
, 0, 5);
1408 if (s
->eventq
.log2size
> SMMU_EVENTQS
) {
1409 s
->eventq
.log2size
= SMMU_EVENTQS
;
1412 case A_EVENTQ_IRQ_CFG0
:
1413 s
->eventq_irq_cfg0
= data
;
1416 qemu_log_mask(LOG_UNIMP
,
1417 "%s Unexpected 64-bit access to 0x%"PRIx64
" (WI)\n",
1423 static MemTxResult
smmu_writel(SMMUv3State
*s
, hwaddr offset
,
1424 uint64_t data
, MemTxAttrs attrs
)
1429 s
->cr0ack
= data
& ~SMMU_CR0_RESERVED
;
1430 /* in case the command queue has been enabled */
1431 smmuv3_cmdq_consume(s
);
1443 smmuv3_write_gerrorn(s
, data
);
1445 * By acknowledging the CMDQ_ERR, SW may notify cmds can
1446 * be processed again
1448 smmuv3_cmdq_consume(s
);
1450 case A_GERROR_IRQ_CFG0
: /* 64b */
1451 s
->gerror_irq_cfg0
= deposit64(s
->gerror_irq_cfg0
, 0, 32, data
);
1453 case A_GERROR_IRQ_CFG0
+ 4:
1454 s
->gerror_irq_cfg0
= deposit64(s
->gerror_irq_cfg0
, 32, 32, data
);
1456 case A_GERROR_IRQ_CFG1
:
1457 s
->gerror_irq_cfg1
= data
;
1459 case A_GERROR_IRQ_CFG2
:
1460 s
->gerror_irq_cfg2
= data
;
1464 * If UPDATE is not set, the write is ignored. This is the only
1465 * permitted behavior in SMMUv3.2 and later.
1467 if (data
& R_GBPA_UPDATE_MASK
) {
1468 /* Ignore update bit as write is synchronous. */
1469 s
->gbpa
= data
& ~R_GBPA_UPDATE_MASK
;
1472 case A_STRTAB_BASE
: /* 64b */
1473 s
->strtab_base
= deposit64(s
->strtab_base
, 0, 32, data
);
1475 case A_STRTAB_BASE
+ 4:
1476 s
->strtab_base
= deposit64(s
->strtab_base
, 32, 32, data
);
1478 case A_STRTAB_BASE_CFG
:
1479 s
->strtab_base_cfg
= data
;
1480 if (FIELD_EX32(data
, STRTAB_BASE_CFG
, FMT
) == 1) {
1481 s
->sid_split
= FIELD_EX32(data
, STRTAB_BASE_CFG
, SPLIT
);
1482 s
->features
|= SMMU_FEATURE_2LVL_STE
;
1485 case A_CMDQ_BASE
: /* 64b */
1486 s
->cmdq
.base
= deposit64(s
->cmdq
.base
, 0, 32, data
);
1487 s
->cmdq
.log2size
= extract64(s
->cmdq
.base
, 0, 5);
1488 if (s
->cmdq
.log2size
> SMMU_CMDQS
) {
1489 s
->cmdq
.log2size
= SMMU_CMDQS
;
1492 case A_CMDQ_BASE
+ 4: /* 64b */
1493 s
->cmdq
.base
= deposit64(s
->cmdq
.base
, 32, 32, data
);
1496 s
->cmdq
.prod
= data
;
1497 smmuv3_cmdq_consume(s
);
1500 s
->cmdq
.cons
= data
;
1502 case A_EVENTQ_BASE
: /* 64b */
1503 s
->eventq
.base
= deposit64(s
->eventq
.base
, 0, 32, data
);
1504 s
->eventq
.log2size
= extract64(s
->eventq
.base
, 0, 5);
1505 if (s
->eventq
.log2size
> SMMU_EVENTQS
) {
1506 s
->eventq
.log2size
= SMMU_EVENTQS
;
1509 case A_EVENTQ_BASE
+ 4:
1510 s
->eventq
.base
= deposit64(s
->eventq
.base
, 32, 32, data
);
1513 s
->eventq
.prod
= data
;
1516 s
->eventq
.cons
= data
;
1518 case A_EVENTQ_IRQ_CFG0
: /* 64b */
1519 s
->eventq_irq_cfg0
= deposit64(s
->eventq_irq_cfg0
, 0, 32, data
);
1521 case A_EVENTQ_IRQ_CFG0
+ 4:
1522 s
->eventq_irq_cfg0
= deposit64(s
->eventq_irq_cfg0
, 32, 32, data
);
1524 case A_EVENTQ_IRQ_CFG1
:
1525 s
->eventq_irq_cfg1
= data
;
1527 case A_EVENTQ_IRQ_CFG2
:
1528 s
->eventq_irq_cfg2
= data
;
1531 qemu_log_mask(LOG_UNIMP
,
1532 "%s Unexpected 32-bit access to 0x%"PRIx64
" (WI)\n",
1538 static MemTxResult
smmu_write_mmio(void *opaque
, hwaddr offset
, uint64_t data
,
1539 unsigned size
, MemTxAttrs attrs
)
1541 SMMUState
*sys
= opaque
;
1542 SMMUv3State
*s
= ARM_SMMUV3(sys
);
1545 /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */
1550 r
= smmu_writell(s
, offset
, data
, attrs
);
1553 r
= smmu_writel(s
, offset
, data
, attrs
);
1560 trace_smmuv3_write_mmio(offset
, data
, size
, r
);
1564 static MemTxResult
smmu_readll(SMMUv3State
*s
, hwaddr offset
,
1565 uint64_t *data
, MemTxAttrs attrs
)
1568 case A_GERROR_IRQ_CFG0
:
1569 *data
= s
->gerror_irq_cfg0
;
1572 *data
= s
->strtab_base
;
1575 *data
= s
->cmdq
.base
;
1578 *data
= s
->eventq
.base
;
1582 qemu_log_mask(LOG_UNIMP
,
1583 "%s Unexpected 64-bit access to 0x%"PRIx64
" (RAZ)\n",
1589 static MemTxResult
smmu_readl(SMMUv3State
*s
, hwaddr offset
,
1590 uint64_t *data
, MemTxAttrs attrs
)
1593 case A_IDREGS
... A_IDREGS
+ 0x2f:
1594 *data
= smmuv3_idreg(offset
- A_IDREGS
);
1596 case A_IDR0
... A_IDR5
:
1597 *data
= s
->idr
[(offset
- A_IDR0
) / 4];
1624 case A_IRQ_CTRL_ACK
:
1625 *data
= s
->irq_ctrl
;
1633 case A_GERROR_IRQ_CFG0
: /* 64b */
1634 *data
= extract64(s
->gerror_irq_cfg0
, 0, 32);
1636 case A_GERROR_IRQ_CFG0
+ 4:
1637 *data
= extract64(s
->gerror_irq_cfg0
, 32, 32);
1639 case A_GERROR_IRQ_CFG1
:
1640 *data
= s
->gerror_irq_cfg1
;
1642 case A_GERROR_IRQ_CFG2
:
1643 *data
= s
->gerror_irq_cfg2
;
1645 case A_STRTAB_BASE
: /* 64b */
1646 *data
= extract64(s
->strtab_base
, 0, 32);
1648 case A_STRTAB_BASE
+ 4: /* 64b */
1649 *data
= extract64(s
->strtab_base
, 32, 32);
1651 case A_STRTAB_BASE_CFG
:
1652 *data
= s
->strtab_base_cfg
;
1654 case A_CMDQ_BASE
: /* 64b */
1655 *data
= extract64(s
->cmdq
.base
, 0, 32);
1657 case A_CMDQ_BASE
+ 4:
1658 *data
= extract64(s
->cmdq
.base
, 32, 32);
1661 *data
= s
->cmdq
.prod
;
1664 *data
= s
->cmdq
.cons
;
1666 case A_EVENTQ_BASE
: /* 64b */
1667 *data
= extract64(s
->eventq
.base
, 0, 32);
1669 case A_EVENTQ_BASE
+ 4: /* 64b */
1670 *data
= extract64(s
->eventq
.base
, 32, 32);
1673 *data
= s
->eventq
.prod
;
1676 *data
= s
->eventq
.cons
;
1680 qemu_log_mask(LOG_UNIMP
,
1681 "%s unhandled 32-bit access at 0x%"PRIx64
" (RAZ)\n",
1687 static MemTxResult
smmu_read_mmio(void *opaque
, hwaddr offset
, uint64_t *data
,
1688 unsigned size
, MemTxAttrs attrs
)
1690 SMMUState
*sys
= opaque
;
1691 SMMUv3State
*s
= ARM_SMMUV3(sys
);
1694 /* CONSTRAINED UNPREDICTABLE choice to have page0/1 be exact aliases */
1699 r
= smmu_readll(s
, offset
, data
, attrs
);
1702 r
= smmu_readl(s
, offset
, data
, attrs
);
1709 trace_smmuv3_read_mmio(offset
, *data
, size
, r
);
1713 static const MemoryRegionOps smmu_mem_ops
= {
1714 .read_with_attrs
= smmu_read_mmio
,
1715 .write_with_attrs
= smmu_write_mmio
,
1716 .endianness
= DEVICE_LITTLE_ENDIAN
,
1718 .min_access_size
= 4,
1719 .max_access_size
= 8,
1722 .min_access_size
= 4,
1723 .max_access_size
= 8,
1727 static void smmu_init_irq(SMMUv3State
*s
, SysBusDevice
*dev
)
1731 for (i
= 0; i
< ARRAY_SIZE(s
->irq
); i
++) {
1732 sysbus_init_irq(dev
, &s
->irq
[i
]);
1736 static void smmu_reset_hold(Object
*obj
, ResetType type
)
1738 SMMUv3State
*s
= ARM_SMMUV3(obj
);
1739 SMMUv3Class
*c
= ARM_SMMUV3_GET_CLASS(s
);
1741 if (c
->parent_phases
.hold
) {
1742 c
->parent_phases
.hold(obj
, type
);
1745 smmuv3_init_regs(s
);
1748 static void smmu_realize(DeviceState
*d
, Error
**errp
)
1750 SMMUState
*sys
= ARM_SMMU(d
);
1751 SMMUv3State
*s
= ARM_SMMUV3(sys
);
1752 SMMUv3Class
*c
= ARM_SMMUV3_GET_CLASS(s
);
1753 SysBusDevice
*dev
= SYS_BUS_DEVICE(d
);
1754 Error
*local_err
= NULL
;
1756 c
->parent_realize(d
, &local_err
);
1758 error_propagate(errp
, local_err
);
1762 qemu_mutex_init(&s
->mutex
);
1764 memory_region_init_io(&sys
->iomem
, OBJECT(s
),
1765 &smmu_mem_ops
, sys
, TYPE_ARM_SMMUV3
, 0x20000);
1767 sys
->mrtypename
= TYPE_SMMUV3_IOMMU_MEMORY_REGION
;
1769 sysbus_init_mmio(dev
, &sys
->iomem
);
1771 smmu_init_irq(s
, dev
);
1774 static const VMStateDescription vmstate_smmuv3_queue
= {
1775 .name
= "smmuv3_queue",
1777 .minimum_version_id
= 1,
1778 .fields
= (const VMStateField
[]) {
1779 VMSTATE_UINT64(base
, SMMUQueue
),
1780 VMSTATE_UINT32(prod
, SMMUQueue
),
1781 VMSTATE_UINT32(cons
, SMMUQueue
),
1782 VMSTATE_UINT8(log2size
, SMMUQueue
),
1783 VMSTATE_END_OF_LIST(),
1787 static bool smmuv3_gbpa_needed(void *opaque
)
1789 SMMUv3State
*s
= opaque
;
1791 /* Only migrate GBPA if it has different reset value. */
1792 return s
->gbpa
!= SMMU_GBPA_RESET_VAL
;
1795 static const VMStateDescription vmstate_gbpa
= {
1796 .name
= "smmuv3/gbpa",
1798 .minimum_version_id
= 1,
1799 .needed
= smmuv3_gbpa_needed
,
1800 .fields
= (const VMStateField
[]) {
1801 VMSTATE_UINT32(gbpa
, SMMUv3State
),
1802 VMSTATE_END_OF_LIST()
1806 static const VMStateDescription vmstate_smmuv3
= {
1809 .minimum_version_id
= 1,
1810 .priority
= MIG_PRI_IOMMU
,
1811 .fields
= (const VMStateField
[]) {
1812 VMSTATE_UINT32(features
, SMMUv3State
),
1813 VMSTATE_UINT8(sid_size
, SMMUv3State
),
1814 VMSTATE_UINT8(sid_split
, SMMUv3State
),
1816 VMSTATE_UINT32_ARRAY(cr
, SMMUv3State
, 3),
1817 VMSTATE_UINT32(cr0ack
, SMMUv3State
),
1818 VMSTATE_UINT32(statusr
, SMMUv3State
),
1819 VMSTATE_UINT32(irq_ctrl
, SMMUv3State
),
1820 VMSTATE_UINT32(gerror
, SMMUv3State
),
1821 VMSTATE_UINT32(gerrorn
, SMMUv3State
),
1822 VMSTATE_UINT64(gerror_irq_cfg0
, SMMUv3State
),
1823 VMSTATE_UINT32(gerror_irq_cfg1
, SMMUv3State
),
1824 VMSTATE_UINT32(gerror_irq_cfg2
, SMMUv3State
),
1825 VMSTATE_UINT64(strtab_base
, SMMUv3State
),
1826 VMSTATE_UINT32(strtab_base_cfg
, SMMUv3State
),
1827 VMSTATE_UINT64(eventq_irq_cfg0
, SMMUv3State
),
1828 VMSTATE_UINT32(eventq_irq_cfg1
, SMMUv3State
),
1829 VMSTATE_UINT32(eventq_irq_cfg2
, SMMUv3State
),
1831 VMSTATE_STRUCT(cmdq
, SMMUv3State
, 0, vmstate_smmuv3_queue
, SMMUQueue
),
1832 VMSTATE_STRUCT(eventq
, SMMUv3State
, 0, vmstate_smmuv3_queue
, SMMUQueue
),
1834 VMSTATE_END_OF_LIST(),
1836 .subsections
= (const VMStateDescription
* const []) {
1842 static Property smmuv3_properties
[] = {
1844 * Stages of translation advertised.
1847 * Defaults to stage 1
1849 DEFINE_PROP_STRING("stage", SMMUv3State
, stage
),
1850 DEFINE_PROP_END_OF_LIST()
1853 static void smmuv3_instance_init(Object
*obj
)
1855 /* Nothing much to do here as of now */
1858 static void smmuv3_class_init(ObjectClass
*klass
, void *data
)
1860 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1861 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
1862 SMMUv3Class
*c
= ARM_SMMUV3_CLASS(klass
);
1864 dc
->vmsd
= &vmstate_smmuv3
;
1865 resettable_class_set_parent_phases(rc
, NULL
, smmu_reset_hold
, NULL
,
1867 device_class_set_parent_realize(dc
, smmu_realize
,
1868 &c
->parent_realize
);
1869 device_class_set_props(dc
, smmuv3_properties
);
1872 static int smmuv3_notify_flag_changed(IOMMUMemoryRegion
*iommu
,
1873 IOMMUNotifierFlag old
,
1874 IOMMUNotifierFlag
new,
1877 SMMUDevice
*sdev
= container_of(iommu
, SMMUDevice
, iommu
);
1878 SMMUv3State
*s3
= sdev
->smmu
;
1879 SMMUState
*s
= &(s3
->smmu_state
);
1881 if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP
) {
1882 error_setg(errp
, "SMMUv3 does not support dev-iotlb yet");
1886 if (new & IOMMU_NOTIFIER_MAP
) {
1888 "device %02x.%02x.%x requires iommu MAP notifier which is "
1889 "not currently supported", pci_bus_num(sdev
->bus
),
1890 PCI_SLOT(sdev
->devfn
), PCI_FUNC(sdev
->devfn
));
1894 if (old
== IOMMU_NOTIFIER_NONE
) {
1895 trace_smmuv3_notify_flag_add(iommu
->parent_obj
.name
);
1896 QLIST_INSERT_HEAD(&s
->devices_with_notifiers
, sdev
, next
);
1897 } else if (new == IOMMU_NOTIFIER_NONE
) {
1898 trace_smmuv3_notify_flag_del(iommu
->parent_obj
.name
);
1899 QLIST_REMOVE(sdev
, next
);
1904 static void smmuv3_iommu_memory_region_class_init(ObjectClass
*klass
,
1907 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
1909 imrc
->translate
= smmuv3_translate
;
1910 imrc
->notify_flag_changed
= smmuv3_notify_flag_changed
;
1913 static const TypeInfo smmuv3_type_info
= {
1914 .name
= TYPE_ARM_SMMUV3
,
1915 .parent
= TYPE_ARM_SMMU
,
1916 .instance_size
= sizeof(SMMUv3State
),
1917 .instance_init
= smmuv3_instance_init
,
1918 .class_size
= sizeof(SMMUv3Class
),
1919 .class_init
= smmuv3_class_init
,
1922 static const TypeInfo smmuv3_iommu_memory_region_info
= {
1923 .parent
= TYPE_IOMMU_MEMORY_REGION
,
1924 .name
= TYPE_SMMUV3_IOMMU_MEMORY_REGION
,
1925 .class_init
= smmuv3_iommu_memory_region_class_init
,
1928 static void smmuv3_register_types(void)
1930 type_register(&smmuv3_type_info
);
1931 type_register(&smmuv3_iommu_memory_region_info
);
1934 type_init(smmuv3_register_types
)