|
| 1 | +#![allow(unused)] |
| 2 | + |
| 3 | +#[repr(usize)] |
| 4 | +pub enum DcacheOp { |
| 5 | + CleanAndInvalidate = 0, |
| 6 | + InvalidateOnly = 1, |
| 7 | +} |
| 8 | + |
| 9 | +use core::arch::naked_asm; |
| 10 | + |
| 11 | +#[unsafe(naked)] |
| 12 | +pub unsafe extern "C" fn flush_dcache_range(start: usize, end: usize) { |
| 13 | + naked_asm!( |
| 14 | + " |
| 15 | + mrs x3, ctr_el0 |
| 16 | + ubfx x3, x3, #16, #4 |
| 17 | + mov x2, #4 |
| 18 | + lsl x2, x2, x3 /* cache line size */ |
| 19 | +
|
| 20 | + /* x2 <- minimal cache line size in cache system */ |
| 21 | + sub x3, x2, #1 |
| 22 | + bic x0, x0, x3 |
| 23 | +1: dc civac, x0 /* clean & invalidate data or unified cache */ |
| 24 | + add x0, x0, x2 |
| 25 | + cmp x0, x1 |
| 26 | + b.lo 1b |
| 27 | + dsb sy |
| 28 | + ret |
| 29 | + " |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +#[unsafe(naked)] |
| 34 | +pub unsafe extern "C" fn flush_invalidate_range(start: usize, end: usize) { |
| 35 | + naked_asm!( |
| 36 | + " |
| 37 | + mrs x3, ctr_el0 |
| 38 | + ubfx x3, x3, #16, #4 |
| 39 | + mov x2, #4 |
| 40 | + lsl x2, x2, x3 /* cache line size */ |
| 41 | +
|
| 42 | + /* x2 <- minimal cache line size in cache system */ |
| 43 | + sub x3, x2, #1 |
| 44 | + bic x0, x0, x3 |
| 45 | +1: dc ivac, x0 /* invalidate data or unified cache */ |
| 46 | + add x0, x0, x2 |
| 47 | + cmp x0, x1 |
| 48 | + b.lo 1b |
| 49 | + dsb sy |
| 50 | + ret |
| 51 | + " |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +#[unsafe(naked)] |
| 56 | +pub unsafe extern "C" fn invalidate_icache_all() { |
| 57 | + naked_asm!( |
| 58 | + " |
| 59 | + ic ialluis |
| 60 | + isb sy |
| 61 | + ret |
| 62 | + " |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +/// Flush and invalidate all cache levels |
| 67 | +/// |
| 68 | +/// x16: FEAT_CCIDX |
| 69 | +/// x2~x9: clobbered |
| 70 | +#[unsafe(naked)] |
| 71 | +pub unsafe extern "C" fn dcache_level(cache_level: usize, op: DcacheOp) { |
| 72 | + naked_asm!( |
| 73 | + " |
| 74 | + lsl x12, x0, #1 |
| 75 | + msr csselr_el1, x12 /* select cache level */ |
| 76 | + isb /* sync change of cssidr_el1 */ |
| 77 | + mrs x6, ccsidr_el1 /* read the new cssidr_el1 */ |
| 78 | + ubfx x2, x6, #0, #3 /* x2 <- log2(cache line size)-4 */ |
| 79 | + cbz x16, 3f /* check for FEAT_CCIDX */ |
| 80 | + ubfx x3, x6, #3, #21 /* x3 <- number of cache ways - 1 */ |
| 81 | + ubfx x4, x6, #32, #24 /* x4 <- number of cache sets - 1 */ |
| 82 | + b 4f |
| 83 | +3: |
| 84 | + ubfx x3, x6, #3, #10 /* x3 <- number of cache ways - 1 */ |
| 85 | + ubfx x4, x6, #13, #15 /* x4 <- number of cache sets - 1 */ |
| 86 | +4: |
| 87 | + add x2, x2, #4 /* x2 <- log2(cache line size) */ |
| 88 | + clz w5, w3 /* bit position of #ways */ |
| 89 | + /* x12 <- cache level << 1 */ |
| 90 | + /* x2 <- line length offset */ |
| 91 | + /* x3 <- number of cache ways - 1 */ |
| 92 | + /* x4 <- number of cache sets - 1 */ |
| 93 | + /* x5 <- bit position of #ways */ |
| 94 | +
|
| 95 | +5: |
| 96 | + mov x6, x3 /* x6 <- working copy of #ways */ |
| 97 | +6: |
| 98 | + lsl x7, x6, x5 |
| 99 | + orr x9, x12, x7 /* map way and level to cisw value */ |
| 100 | + lsl x7, x4, x2 |
| 101 | + orr x9, x9, x7 /* map set number to cisw value */ |
| 102 | + tbz w1, #0, 1f |
| 103 | + dc isw, x9 |
| 104 | + b 2f |
| 105 | +1: dc cisw, x9 /* clean & invalidate by set/way */ |
| 106 | +2: subs x6, x6, #1 /* decrement the way */ |
| 107 | + b.ge 6b |
| 108 | + subs x4, x4, #1 /* decrement the set */ |
| 109 | + b.ge 5b |
| 110 | +
|
| 111 | + ret |
| 112 | + " |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +/// Flush or invalidate all data cache by SET/WAY. |
| 117 | +#[unsafe(naked)] |
| 118 | +pub unsafe extern "C" fn dcache_all(op: DcacheOp) { |
| 119 | + naked_asm!( |
| 120 | + " |
| 121 | + mov x1, x0 |
| 122 | + dsb sy |
| 123 | + mrs x10, clidr_el1 /* read clidr_el1 */ |
| 124 | + ubfx x11, x10, #24, #3 /* x11 <- loc */ |
| 125 | + cbz x11, 3b /* if loc is 0, exit */ |
| 126 | + mov x15, lr |
| 127 | + mrs x16, s3_0_c0_c7_2 /* read value of id_aa64mmfr2_el1*/ |
| 128 | + ubfx x16, x16, #20, #4 /* save FEAT_CCIDX identifier in x16 */ |
| 129 | + mov x0, #0 /* start flush at cache level 0 */ |
| 130 | + /* x0 <- cache level */ |
| 131 | + /* x10 <- clidr_el1 */ |
| 132 | + /* x11 <- loc */ |
| 133 | + /* x15 <- return address */ |
| 134 | +/* loop level */ |
| 135 | +1: |
| 136 | + add x12, x0, x0, lsl #1 /* x12 <- tripled cache level */ |
| 137 | + lsr x12, x10, x12 |
| 138 | + and x12, x12, #7 /* x12 <- cache type */ |
| 139 | + cmp x12, #2 |
| 140 | + b.lt 2b /* skip if no cache or icache */ |
| 141 | + bl {dcache_level} /* x1 = 0 flush, 1 invalidate */ |
| 142 | +/* skip */ |
| 143 | +2: |
| 144 | + add x0, x0, #1 /* increment cache level */ |
| 145 | + cmp x11, x0 |
| 146 | + b.gt 1b |
| 147 | +
|
| 148 | + mov x0, #0 |
| 149 | + msr csselr_el1, x0 /* restore csselr_el1 */ |
| 150 | + dsb sy |
| 151 | + isb |
| 152 | + mov lr, x15 |
| 153 | +/* finished */ |
| 154 | +3: |
| 155 | + ret |
| 156 | + ", |
| 157 | + dcache_level = sym dcache_level |
| 158 | + ) |
| 159 | +} |
0 commit comments