Skip to content

Commit 8f42e8f

Browse files
committed
Fix number of cache lines invalidated for sizes not a multiple of 32
1 parent da097d8 commit 8f42e8f

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/peripheral/mod.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -712,20 +712,25 @@ impl Scb {
712712
/// Invalidates cache starting from the lowest 32-byte aligned address represented by `addr`,
713713
/// in blocks of 32 bytes until at least `size` bytes have been invalidated.
714714
#[inline]
715-
pub fn invalidate_dcache_by_address(&self, addr: u32, size: u32) {
715+
pub fn invalidate_dcache_by_address(&self, addr: usize, size: usize) {
716+
// No-op zero sized operations
717+
if size == 0 {
718+
return;
719+
}
720+
716721
// All of CBP is write-only so no data races are possible
717722
let cbp = unsafe { &mut *CBP.get() };
718723

719724
::asm::dsb();
720725

721-
722726
// Cache lines are fixed to 32 bit on Cortex-M7 and not present in earlier Cortex-M
723-
const LINESIZE: u32 = 32;
727+
const LINESIZE: usize = 32;
728+
let num_lines = ((size - 1)/LINESIZE) + 1;
724729

725730
let mut addr = addr & 0xFFFF_FFE0;
726731

727-
for _ in 0..(size/LINESIZE) {
728-
cbp.dcimvac(addr);
732+
for _ in 0..num_lines {
733+
cbp.dcimvac(addr as u32);
729734
addr += LINESIZE;
730735
}
731736

@@ -741,19 +746,25 @@ impl Scb {
741746
/// Cleans cache starting from the lowest 32-byte aligned address represented by `addr`,
742747
/// in blocks of 32 bytes until at least `size` bytes have been cleaned.
743748
#[inline]
744-
pub fn clean_dcache_by_address(&self, addr: u32, size: u32) {
749+
pub fn clean_dcache_by_address(&self, addr: usize, size: usize) {
750+
// No-op zero sized operations
751+
if size == 0 {
752+
return;
753+
}
754+
745755
// All of CBP is write-only so no data races are possible
746756
let cbp = unsafe { &mut *CBP.get() };
747757

748758
::asm::dsb();
749759

750760
// Cache lines are fixed to 32 bit on Cortex-M7 and not present in earlier Cortex-M
751-
const LINESIZE: u32 = 32;
761+
const LINESIZE: usize = 32;
762+
let num_lines = ((size - 1)/LINESIZE) + 1;
752763

753764
let mut addr = addr & 0xFFFF_FFE0;
754765

755-
for _ in 0..(size/LINESIZE) {
756-
cbp.dccmvac(addr);
766+
for _ in 0..num_lines {
767+
cbp.dccmvac(addr as u32);
757768
addr += LINESIZE;
758769
}
759770

@@ -770,19 +781,25 @@ impl Scb {
770781
/// by `addr`, in blocks of 32 bytes until at least `size` bytes have been cleaned and
771782
/// invalidated.
772783
#[inline]
773-
pub fn clean_invalidate_dcache_by_address(&self, addr: u32, size: u32) {
784+
pub fn clean_invalidate_dcache_by_address(&self, addr: usize, size: usize) {
785+
// No-op zero sized operations
786+
if size == 0 {
787+
return;
788+
}
789+
774790
// All of CBP is write-only so no data races are possible
775791
let cbp = unsafe { &mut *CBP.get() };
776792

777793
::asm::dsb();
778794

779795
// Cache lines are fixed to 32 bit on Cortex-M7 and not present in earlier Cortex-M
780-
const LINESIZE: u32 = 32;
796+
const LINESIZE: usize = 32;
797+
let num_lines = ((size - 1)/LINESIZE) + 1;
781798

782799
let mut addr = addr & 0xFFFF_FFE0;
783800

784-
for _ in 0..(size/LINESIZE) {
785-
cbp.dccimvac(addr);
801+
for _ in 0..num_lines {
802+
cbp.dccimvac(addr as u32);
786803
addr += LINESIZE;
787804
}
788805

0 commit comments

Comments
 (0)