hw/arm/smmuv3: Correct stream ID and event address handling
v3 cover hw/arm/smmuv3: Correct stream ID and event address handling
While working on the Linux SMMUv3 driver, I noticed a few cases where the QEMU SMMUv3 behavior relating to stream tables was inconsistent with our hardware.
Also, when debugging those differences, I found that the errors reported through the QEMU SMMUv3 event queue contained the address fields in an incorrect position.
These patches correct the QEMU SMMUv3 behavior to match the specification (and the behavior that I observed in our hardware). Linux guests normally will not notice these issues, but other SMMUv3 driver implementations might.
Changes in v2:
* New patch "hw/arm/smmuv3: Correct SMMU_BASE_ADDR_MASK value" added
* Updated patch "hw/arm/smmuv3: Check stream IDs against actual table LOG2SIZE"
* Updated patch "hw/arm/smmuv3: Align stream table base address to table size"
Changes in v3:
* No changes, but sending again to correct a patch submission mishap that confused Patchew
Simon Veith (6):
hw/arm/smmuv3: Apply address mask to linear strtab base address hw/arm/smmuv3: Correct SMMU_BASE_ADDR_MASK value hw/arm/smmuv3: Check stream IDs against actual table LOG2SIZE hw/arm/smmuv3: Align stream table base address to table size hw/arm/smmuv3: Use correct bit positions in EVT_SET_ADDR2 macro hw/arm/smmuv3: Report F_STE_FETCH fault address in correct word
position
hw/arm/smmuv3-internal.h | 6 +++--- hw/arm/smmuv3.c | 28 +++++++++++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-)
Signature · 1 line
2.7.4
PATCH v3 1/6 hw/arm/smmuv3: Apply address mask to linear strtab base address
In the SMMU_STRTAB_BASE register, the stream table base address only occupies bits [51:6]. Other bits, such as RA (bit [62]), must be masked out to obtain the base address.
The branch for 2-level stream tables correctly applies this mask by way of SMMU_BASE_ADDR_MASK, but the one for linear stream tables does not.
Apply the missing mask in that case as well so that the correct stream base address is used by guests which configure a linear stream table.
Linux guests are unaffected by this change because they choose a 2-level stream table layout for the QEMU SMMUv3, based on the size of its stream ID space.
ref. ARM IHI 0070C, section 6.3.23.
--- hw/arm/smmuv3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/smmuv3.c +1 -1
@@ -429,7 +429,7 @@ static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, } addr = l2ptr + l2_ste_offset *sizeof (*ste); }else { - addr = s->strtab_base + sid *sizeof (*ste); + addr = (s->strtab_base &SMMU_BASE_ADDR_MASK ) + sid *sizeof (*ste); }if (smmu_get_ste (s, addr, ste, event)) {
Signature · 1 line
2.7.4
PATCH v3 2/6 hw/arm/smmuv3: Correct SMMU_BASE_ADDR_MASK value
In reply to Simon VeithThere are two issues with the current value of SMMU_BASE_ADDR_MASK:
- At the lower end, we are clearing bits [4:0]. Per the SMMUv3 spec, we should also be treating bit 5 as zero in the base address.
- At the upper end, we are clearing bits [63:48]. Per the SMMUv3 spec, only bits [63:52] must be explicitly treated as zero.
Update the SMMU_BASE_ADDR_MASK value to mask out bits [63:52] and [5:0].
ref. ARM IHI 0070C, section 6.3.23.
--- hw/arm/smmuv3-internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/smmuv3-internal.h +1 -1
@@ -99,7 +99,7 @@ REG32(GERROR_IRQ_CFG2, 0x74)#define A_STRTAB_BASE 0x80/* 64b */ -#define SMMU_BASE_ADDR_MASK 0xffffffffffe0 +#define SMMU_BASE_ADDR_MASK 0xfffffffffffc0 REG32(STRTAB_BASE_CFG, 0x88)FIELD (STRTAB_BASE_CFG ,FMT ,16 ,2 )
Signature · 1 line
2.7.4
Re: [PATCH v3 2/6] hw/arm/smmuv3: Correct SMMU_BASE_ADDR_MASK value
Hi Simon,
Quoted 7 lines · Simon Veith
There are two issues with the current value of
SMMU_BASE_ADDR_MASK:- At the lower end, we are clearing bits [4:0]. Per the SMMUv3 spec, we should also be treating bit 5 as zero in the base address.
- At the upper end, we are clearing bits [63:48]. Per the SMMUv3 spec, only bits [63:52] must be explicitly treated as zero.
Quoted 8 lines
Update the
SMMU_BASE_ADDR_MASKvalue to mask out bits [63:52] and [5:0].ref. ARM IHI 0070C, section 6.3.23.
Signed-off-by Simon Veith <[email protected]>Cc Eric Auger <[email protected]>
Thanks
Eric
Quoted 18 lines
--- hw/arm/smmuv3-internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/arm/smmuv3-internal.h +1
-1@@ -99,7 +99,7 @@ REG32(GERROR_IRQ_CFG2, 0x74)#define A_STRTAB_BASE 0x80/* 64b */ -#define SMMU_BASE_ADDR_MASK 0xffffffffffe0 +#define SMMU_BASE_ADDR_MASK 0xfffffffffffc0 REG32(STRTAB_BASE_CFG, 0x88)FIELD (STRTAB_BASE_CFG ,FMT ,16 ,2 )
PATCH v3 3/6 hw/arm/smmuv3: Check stream IDs against actual table LOG2SIZE
In reply to Simon VeithWhen checking whether a stream ID is in range of the stream table, we have so far been only checking it against our implementation limit (SMMU_IDR1_SIDSIZE). However, the guest can program the STRTAB_BASE_CFG.LOG2SIZE field to a size that is smaller than this limit.
Check the stream ID against this limit as well to match the hardware behavior of raising C_BAD_STREAMID events in case the limit is exceeded. Also, ensure that we do not go one entry beyond the end of the table by checking that its index is strictly smaller than the table size.
ref. ARM IHI 0070C, section 6.3.24.
---
Changed in v2:
* Also check that stream ID is strictly lower than the table size
hw/arm/smmuv3.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
hw/arm/smmuv3.c +6 -2
@@ -377,11 +377,15 @@ static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event) {dma_addr_t addr; +uint32_t log2size;int ret;trace_smmuv3_find_ste (sid, s->features, s->sid_split); -/* Check SID range */ -if (sid > (1 <<SMMU_IDR1_SIDSIZE )) { + log2size =FIELD_EX32 (s->strtab_base_cfg,STRTAB_BASE_CFG ,LOG2SIZE ); +/* +* Check SID range against both guest-configured and implementation limits +*/ +if (sid >= (1 <<MIN (log2size,SMMU_IDR1_SIDSIZE ))) { event->type =SMMU_EVT_C_BAD_STREAMID ;return -EINVAL ; }
Signature · 1 line
2.7.4
Re: [PATCH v3 3/6] hw/arm/smmuv3: Check stream IDs against actual table LOG2SIZE
Hi Simon,
Quoted 11 lines · Simon Veith
When checking whether a stream ID is in range of the stream table, we have so far been only checking it against our implementation limit (
SMMU_IDR1_SIDSIZE). However, the guest can program the STRTAB_BASE_CFG.LOG2SIZE field to a size that is smaller than this limit.Check the stream ID against this limit as well to match the hardware behavior of raising
C_BAD_STREAMIDevents in case the limit is exceeded. Also, ensure that we do not go one entry beyond the end of the table by checking that its index is strictly smaller than the table size.
Quoted 6 lines
ref. ARM IHI 0070C, section 6.3.24.
Signed-off-by Simon Veith <[email protected]>Cc Eric Auger <[email protected]>
Thanks
Eric
Quoted 31 lines
---
Changed in v2:* Also check that stream ID is strictly lower than the table size
hw/arm/smmuv3.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)hw/arm/smmuv3.c +6
-2@@ -377,11 +377,15 @@ static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event) {dma_addr_t addr; +uint32_t log2size;int ret;trace_smmuv3_find_ste (sid, s->features, s->sid_split); -/* Check SID range */ -if (sid > (1 <<SMMU_IDR1_SIDSIZE )) { + log2size =FIELD_EX32 (s->strtab_base_cfg,STRTAB_BASE_CFG ,LOG2SIZE ); +/* +* Check SID range against both guest-configured and implementation limits +*/ +if (sid >= (1 <<MIN (log2size,SMMU_IDR1_SIDSIZE ))) { event->type =SMMU_EVT_C_BAD_STREAMID ;return -EINVAL ; }
PATCH v3 4/6 hw/arm/smmuv3: Align stream table base address to table size
In reply to Simon VeithPer the specification, and as observed in hardware, the SMMUv3 aligns the SMMU_STRTAB_BASE address to the size of the table by masking out the respective least significant bits in the ADDR field.
Apply this masking logic to our smmu_find_ste() lookup function per the specification.
ref. ARM IHI 0070C, section 6.3.23.
---
Changed in v2:
* Now using MAKE_64BIT_MASK()
* Eliminated unnecessary branches by using MAX()
* Removed unnecessary range check against DMA_ADDR_BITS
hw/arm/smmuv3.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
hw/arm/smmuv3.c +14 -4
@@ -376,8 +376,9 @@ bad_ste:static int smmu_find_ste (SMMUv3State *s,uint32_t sid,STE *ste,SMMUEventInfo *event) { -dma_addr_t addr; +dma_addr_t addr, strtab_base;uint32_t log2size; +int strtab_size_shift;int ret;trace_smmuv3_find_ste (sid, s->features, s->sid_split);
@@ -391,10 +392,16 @@ static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, }if (s->features &SMMU_FEATURE_2LVL_STE ) {int l1_ste_offset, l2_ste_offset, max_l2_ste, span; -dma_addr_t strtab_base, l1ptr, l2ptr; +dma_addr_t l1ptr, l2ptr;STEDesc l1std; - strtab_base = s->strtab_base &SMMU_BASE_ADDR_MASK ; +/* +* Align strtab base address to table size. For this purpose, assume it +* is not bounded by SMMU_IDR1_SIDSIZE. +*/ + strtab_size_shift =MAX (5 , (int )log2size - s->sid_split -1 +3 ); + strtab_base = s->strtab_base &SMMU_BASE_ADDR_MASK & + ~MAKE_64BIT_MASK (0 , strtab_size_shift); l1_ste_offset = sid >> s->sid_split; l2_ste_offset = sid & ((1 << s->sid_split) -1 ); l1ptr = (dma_addr_t)(strtab_base + l1_ste_offset *sizeof (l1std));
@@ -433,7 +440,10 @@ static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, } addr = l2ptr + l2_ste_offset *sizeof (*ste); }else { - addr = (s->strtab_base &SMMU_BASE_ADDR_MASK ) + sid *sizeof (*ste); + strtab_size_shift = log2size +5 ; + strtab_base = s->strtab_base &SMMU_BASE_ADDR_MASK & + ~MAKE_64BIT_MASK (0 , strtab_size_shift); + addr = strtab_base + sid *sizeof (*ste); }if (smmu_get_ste (s, addr, ste, event)) {
Signature · 1 line
2.7.4
Re: [PATCH v3 4/6] hw/arm/smmuv3: Align stream table base address to table size
Hi Simon,
Quoted 7 lines · Simon Veith
Per the specification, and as observed in hardware, the SMMUv3 aligns the
SMMU_STRTAB_BASEaddress to the size of the table by masking out the respective least significant bits in the ADDR field.Apply this masking logic to our
smmu_find_ste()lookup function per the specification.
Quoted 6 lines
ref. ARM IHI 0070C, section 6.3.23.
Signed-off-by Simon Veith <[email protected]>Cc Eric Auger <[email protected]>
Looks good to me.
Thanks
Eric
Quoted 57 lines
---
Changed in v2:* Now using
MAKE_64BIT_MASK()
* Eliminated unnecessary branches by usingMAX()
* Removed unnecessary range check againstDMA_ADDR_BITShw/arm/smmuv3.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)hw/arm/smmuv3.c +14
-4@@ -376,8 +376,9 @@ bad_ste:static int smmu_find_ste (SMMUv3State *s,uint32_t sid,STE *ste,SMMUEventInfo *event) { -dma_addr_t addr; +dma_addr_t addr, strtab_base;uint32_t log2size; +int strtab_size_shift;int ret;trace_smmuv3_find_ste (sid, s->features, s->sid_split);@@ -391,10 +392,16 @@ static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, }if (s->features &SMMU_FEATURE_2LVL_STE ) {int l1_ste_offset, l2_ste_offset, max_l2_ste, span; -dma_addr_t strtab_base, l1ptr, l2ptr; +dma_addr_t l1ptr, l2ptr;STEDesc l1std; - strtab_base = s->strtab_base &SMMU_BASE_ADDR_MASK ; +/* +* Align strtab base address to table size. For this purpose, assume it +* is not bounded by SMMU_IDR1_SIDSIZE. +*/ + strtab_size_shift =MAX (5 , (int )log2size - s->sid_split -1 +3 ); + strtab_base = s->strtab_base &SMMU_BASE_ADDR_MASK & + ~MAKE_64BIT_MASK (0 , strtab_size_shift); l1_ste_offset = sid >> s->sid_split; l2_ste_offset = sid & ((1 << s->sid_split) -1 ); l1ptr = (dma_addr_t)(strtab_base + l1_ste_offset *sizeof (l1std));@@ -433,7 +440,10 @@ static int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, } addr = l2ptr + l2_ste_offset *sizeof (*ste); }else { - addr = (s->strtab_base &SMMU_BASE_ADDR_MASK ) + sid *sizeof (*ste); + strtab_size_shift = log2size +5 ; + strtab_base = s->strtab_base &SMMU_BASE_ADDR_MASK & + ~MAKE_64BIT_MASK (0 , strtab_size_shift); + addr = strtab_base + sid *sizeof (*ste); }if (smmu_get_ste (s, addr, ste, event)) {
PATCH v3 5/6 hw/arm/smmuv3: Use correct bit positions in EVT_SET_ADDR2 macro
In reply to Simon VeithThe bit offsets in the EVT_SET_ADDR2 macro do not match those specified in the ARM SMMUv3 Architecture Specification. In all events that use this macro, e.g. F_WALK_EABT, the faulting fetch address or IPA actually occupies the 32-bit words 6 and 7 in the event record contiguously, with the upper and lower unused bits clear due to alignment or maximum supported address bits. How many bits are clear depends on the individual event type.
Update the macro to write to the correct words in the event record so that guest drivers can obtain accurate address information on events.
ref. ARM IHI 0070C, sections 7.3.12 through 7.3.16.
--- hw/arm/smmuv3-internal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/arm/smmuv3-internal.h +2 -2
@@ -461,8 +461,8 @@ typedef struct SMMUEventInfo { } while (0)#define EVT_SET_ADDR2 (x, addr) \ do { \ - (x)->word[7] = deposit32((x)->word[7], 3, 29, addr >> 16); \ - (x)->word[7] = deposit32((x)->word[7], 0, 16, addr & 0xffff);\ + (x)->word[7] = (uint32_t)(addr >> 32); \ + (x)->word[6] = (uint32_t)(addr & 0xffffffff); \ } while (0)void smmuv3_record_event (SMMUv3State *s,SMMUEventInfo *event);
Signature · 1 line
2.7.4
PATCH v3 6/6 hw/arm/smmuv3: Report F_STE_FETCH fault address in correct word position
In reply to Simon VeithThe smmuv3_record_event() function that generates the F_STE_FETCH error uses the EVT_SET_ADDR macro to record the fetch address, placing it in 32-bit words 4 and 5.
The correct position for this address is in words 6 and 7, per the SMMUv3 Architecture Specification.
Update the function to use the EVT_SET_ADDR2 macro instead, which is the macro intended for writing to these words.
ref. ARM IHI 0070C, section 7.3.4.
--- hw/arm/smmuv3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/smmuv3.c +1 -1
@@ -172,7 +172,7 @@ void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info)case SMMU_EVT_F_STE_FETCH :EVT_SET_SSID (&evt, info->u.f_ste_fetch.ssid);EVT_SET_SSV (&evt, info->u.f_ste_fetch.ssv); -EVT_SET_ADDR (&evt, info->u.f_ste_fetch.addr); +EVT_SET_ADDR2 (&evt, info->u.f_ste_fetch.addr);break ;case SMMU_EVT_C_BAD_STE :EVT_SET_SSID (&evt, info->u.c_bad_ste.ssid);
Signature · 1 line
2.7.4
Hi,
Quoted 22 lines · Simon Veith
While working on the Linux SMMUv3 driver, I noticed a few cases where the QEMU SMMUv3 behavior relating to stream tables was inconsistent with our hardware.
Also, when debugging those differences, I found that the errors reported through the QEMU SMMUv3 event queue contained the address fields in an incorrect position.
These patches correct the QEMU SMMUv3 behavior to match the specification (and the behavior that I observed in our hardware). Linux guests normally will not notice these issues, but other SMMUv3 driver implementations might.
Changes in v2:
* New patch "hw/arm/smmuv3: Correct
SMMU_BASE_ADDR_MASKvalue" added
* Updated patch "hw/arm/smmuv3: Check stream IDs against actual table LOG2SIZE"
* Updated patch "hw/arm/smmuv3: Align stream table base address to table size"Changes in v3:
* No changes, but sending again to correct a patch submission mishap that confused Patchew
Quoted 8 lines
Simon Veith (6):
hw/arm/smmuv3: Apply address mask to linear strtab base address hw/arm/smmuv3: CorrectSMMU_BASE_ADDR_MASKvalue hw/arm/smmuv3: Check stream IDs against actual table LOG2SIZE hw/arm/smmuv3: Align stream table base address to table size hw/arm/smmuv3: Use correct bit positions inEVT_SET_ADDR2macro hw/arm/smmuv3: ReportF_STE_FETCHfault address in correct word
position
The series looks good to me. Also tested against non regression.
Thanks
Eric
Quoted 5 lines
hw/arm/smmuv3-internal.h | 6 +++--- hw/arm/smmuv3.c | 28 +++++++++++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-)
Peter Maydell
AppliedQuoted 35 lines · Auger Eric
Hi,
Quoted 22 lines · Simon Veith
While working on the Linux SMMUv3 driver, I noticed a few cases where the QEMU SMMUv3 behavior relating to stream tables was inconsistent with our hardware.
Also, when debugging those differences, I found that the errors reported through the QEMU SMMUv3 event queue contained the address fields in an incorrect position.
These patches correct the QEMU SMMUv3 behavior to match the specification (and the behavior that I observed in our hardware). Linux guests normally will not notice these issues, but other SMMUv3 driver implementations might.
Changes in v2:
* New patch "hw/arm/smmuv3: Correct
SMMU_BASE_ADDR_MASKvalue" added
* Updated patch "hw/arm/smmuv3: Check stream IDs against actual table LOG2SIZE"
* Updated patch "hw/arm/smmuv3: Align stream table base address to table size"Changes in v3:
* No changes, but sending again to correct a patch submission mishap that confused Patchew
Quoted 8 lines
Simon Veith (6):
hw/arm/smmuv3: Apply address mask to linear strtab base address hw/arm/smmuv3: CorrectSMMU_BASE_ADDR_MASKvalue hw/arm/smmuv3: Check stream IDs against actual table LOG2SIZE hw/arm/smmuv3: Align stream table base address to table size hw/arm/smmuv3: Use correct bit positions inEVT_SET_ADDR2macro hw/arm/smmuv3: ReportF_STE_FETCHfault address in correct word
position
Quoted 3 lines
The series looks good to me. Also tested against non regression.
Tested-by Eric Auger <[email protected]>
Applied to target-arm.next, thanks.
-- PMM