|
| 1 | +//! Cache and branch predictor maintenance operations |
| 2 | +
|
| 3 | +use volatile_register::WO; |
| 4 | + |
| 5 | +/// Register block |
| 6 | +#[repr(C)] |
| 7 | +pub struct RegisterBlock { |
| 8 | + /// I-cache invalidate all to PoU |
| 9 | + pub iciallu: WO<u32>, |
| 10 | + reserved0: u32, |
| 11 | + /// I-cache invalidate by MVA to PoU |
| 12 | + pub icimvau: WO<u32>, |
| 13 | + /// D-cache invalidate by MVA to PoC |
| 14 | + pub dcimvac: WO<u32>, |
| 15 | + /// D-cache invalidate by set-way |
| 16 | + pub dcisw: WO<u32>, |
| 17 | + /// D-cache clean by MVA to PoU |
| 18 | + pub dccmvau: WO<u32>, |
| 19 | + /// D-cache clean by MVA to PoC |
| 20 | + pub dccmvac: WO<u32>, |
| 21 | + /// D-cache clean by set-way |
| 22 | + pub dccsw: WO<u32>, |
| 23 | + /// D-cache clean and invalidate by MVA to PoC |
| 24 | + pub dccimvac: WO<u32>, |
| 25 | + /// D-cache clean and invalidate by set-way |
| 26 | + pub dccisw: WO<u32>, |
| 27 | + /// Branch predictor invalidate all |
| 28 | + pub bpiall: WO<u32>, |
| 29 | +} |
| 30 | + |
| 31 | +const CBP_SW_WAY_POS: u32 = 30; |
| 32 | +const CBP_SW_WAY_MASK: u32 = 0x3 << CBP_SW_WAY_POS; |
| 33 | +const CBP_SW_SET_POS: u32 = 5; |
| 34 | +const CBP_SW_SET_MASK: u32 = 0x1FF << CBP_SW_SET_POS; |
| 35 | + |
| 36 | +impl RegisterBlock { |
| 37 | + /// I-cache invalidate all to PoU |
| 38 | + #[inline(always)] |
| 39 | + pub fn iciallu(&self) { |
| 40 | + unsafe { |
| 41 | + self.iciallu.write(0); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// I-cache invalidate by MVA to PoU |
| 46 | + #[inline(always)] |
| 47 | + pub fn icimvau(&self, mva: u32) { |
| 48 | + unsafe { |
| 49 | + self.icimvau.write(mva); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// D-cache invalidate by MVA to PoC |
| 54 | + #[inline(always)] |
| 55 | + pub fn dcimvac(&self, mva: u32) { |
| 56 | + unsafe { |
| 57 | + self.dcimvac.write(mva); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// D-cache invalidate by set-way |
| 62 | + /// |
| 63 | + /// `set` is masked to be between 0 and 3, and `way` between 0 and 511. |
| 64 | + #[inline(always)] |
| 65 | + pub fn dcisw(&self, set: u16, way: u16) { |
| 66 | + // The ARMv7-M Architecture Reference Manual, as of Revision E.b, says these set/way |
| 67 | + // operations have a register data format which depends on the implementation's |
| 68 | + // associativity and number of sets. Specifically the 'way' and 'set' fields have |
| 69 | + // offsets 32-log2(ASSOCIATIVITY) and log2(LINELEN) respectively. |
| 70 | + // |
| 71 | + // However, in Cortex-M7 devices, these offsets are fixed at 30 and 5, as per the Cortex-M7 |
| 72 | + // Generic User Guide section 4.8.3. Since no other ARMv7-M implementations except the |
| 73 | + // Cortex-M7 have a DCACHE or ICACHE at all, it seems safe to do the same thing as the |
| 74 | + // CMSIS-Core implementation and use fixed values. |
| 75 | + unsafe { |
| 76 | + self.dcisw.write( |
| 77 | + (((way as u32) & (CBP_SW_WAY_MASK >> CBP_SW_WAY_POS)) << CBP_SW_WAY_POS) |
| 78 | + | (((set as u32) & (CBP_SW_SET_MASK >> CBP_SW_SET_POS)) << CBP_SW_SET_POS), |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// D-cache clean by MVA to PoU |
| 84 | + #[inline(always)] |
| 85 | + pub fn dccmvau(&self, mva: u32) { |
| 86 | + unsafe { |
| 87 | + self.dccmvau.write(mva); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// D-cache clean by MVA to PoC |
| 92 | + #[inline(always)] |
| 93 | + pub fn dccmvac(&self, mva: u32) { |
| 94 | + unsafe { |
| 95 | + self.dccmvac.write(mva); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// D-cache clean by set-way |
| 100 | + /// |
| 101 | + /// `set` is masked to be between 0 and 3, and `way` between 0 and 511. |
| 102 | + #[inline(always)] |
| 103 | + pub fn dccsw(&self, set: u16, way: u16) { |
| 104 | + // See comment for dcisw() about the format here |
| 105 | + unsafe { |
| 106 | + self.dccsw.write( |
| 107 | + (((way as u32) & (CBP_SW_WAY_MASK >> CBP_SW_WAY_POS)) << CBP_SW_WAY_POS) |
| 108 | + | (((set as u32) & (CBP_SW_SET_MASK >> CBP_SW_SET_POS)) << CBP_SW_SET_POS), |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /// D-cache clean and invalidate by MVA to PoC |
| 114 | + #[inline(always)] |
| 115 | + pub fn dccimvac(&self, mva: u32) { |
| 116 | + unsafe { |
| 117 | + self.dccimvac.write(mva); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// D-cache clean and invalidate by set-way |
| 122 | + /// |
| 123 | + /// `set` is masked to be between 0 and 3, and `way` between 0 and 511. |
| 124 | + #[inline(always)] |
| 125 | + pub fn dccisw(&self, set: u16, way: u16) { |
| 126 | + // See comment for dcisw() about the format here |
| 127 | + unsafe { |
| 128 | + self.dccisw.write( |
| 129 | + (((way as u32) & (CBP_SW_WAY_MASK >> CBP_SW_WAY_POS)) << CBP_SW_WAY_POS) |
| 130 | + | (((set as u32) & (CBP_SW_SET_MASK >> CBP_SW_SET_POS)) << CBP_SW_SET_POS), |
| 131 | + ); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// Branch predictor invalidate all |
| 136 | + #[inline(always)] |
| 137 | + pub fn bpiall(&self) { |
| 138 | + unsafe { |
| 139 | + self.bpiall.write(0); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments