Advertisement
xerpi

Cache flush

Dec 21st, 2018
1,906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 3.16 KB | None | 0 0
  1.     ldr sp, =0x1F001000
  2.  
  3.     @ Get CPU ID
  4.     mrc p15, 0, r0, c0, c0, 5
  5.     and r0, #0xF
  6.     cmp r0, #0
  7.  
  8.     mov r0, r0, lsl #8
  9.     sub sp, r0
  10.  
  11.     @ Clean and invalidate the entire Dcache
  12.     stmfd   sp!, {r4-r5, r7, r9-r11, lr}
  13.     bl v7_flush_dcache_all
  14.     ldmfd   sp!, {r4-r5, r7, r9-r11, lr}
  15.  
  16.     @ Invalidate the entire Dcache
  17.     stmfd   sp!, {r4-r5, r7, r9-r11, lr}
  18.     bl v7_invalidate_l1
  19.     ldmfd   sp!, {r4-r5, r7, r9-r11, lr}
  20.  
  21. /*
  22.  *  v7_flush_dcache_all()
  23.  *
  24.  *  Flush the whole D-cache.
  25.  *
  26.  *  Corrupted registers: r0-r7, r9-r11 (r6 only in Thumb mode)
  27.  *
  28.  *  - mm    - mm_struct describing address space
  29.  */
  30. v7_flush_dcache_all:
  31.     dmb                 @ ensure ordering with previous memory accesses
  32.     mrc p15, 1, r0, c0, c0, 1       @ read clidr
  33.     mov r3, r0, lsr #23         @ move LoC into position
  34.     ands    r3, r3, #7 << 1         @ extract LoC*2 from clidr
  35.     beq finished            @ if loc is 0, then no need to clean
  36. start_flush_levels:
  37.     mov r10, #0             @ start clean at cache level 0
  38. flush_levels:
  39.     add r2, r10, r10, lsr #1        @ work out 3x current cache level
  40.     mov r1, r0, lsr r2          @ extract cache type bits from clidr
  41.     and r1, r1, #7          @ mask of the bits for current cache only
  42.     cmp r1, #2              @ see what cache we have at this level
  43.     blt skip                @ skip if no cache, or just i-cache
  44.     mcr p15, 2, r10, c0, c0, 0      @ select current cache level in cssr
  45.     isb                 @ isb to sych the new cssr&csidr
  46.     mrc p15, 1, r1, c0, c0, 0       @ read the new csidr
  47.     and r2, r1, #7          @ extract the length of the cache lines
  48.     add r2, r2, #4          @ add 4 (line length offset)
  49.     movw    r4, #0x3ff
  50.     ands    r4, r4, r1, lsr #3      @ find maximum number on the way size
  51.     clz r5, r4              @ find bit position of way size increment
  52.     movw    r7, #0x7fff
  53.     ands    r7, r7, r1, lsr #13     @ extract max number of the index size
  54. loop1:
  55.     mov r9, r7              @ create working copy of max index
  56. loop2:
  57.     orr r11, r10, r4, lsl r5        @ factor way and cache number into r11
  58.     orr r11, r11, r9, lsl r2        @ factor index number into r11
  59.     mcr p15, 0, r11, c7, c14, 2     @ clean & invalidate by set/way
  60.     subs    r9, r9, #1          @ decrement the index
  61.     bge loop2
  62.     subs    r4, r4, #1          @ decrement the way
  63.     bge loop1
  64. skip:
  65.     add r10, r10, #2            @ increment cache number
  66.     cmp r3, r10
  67.     bgt flush_levels
  68. finished:
  69.     mov r10, #0             @ switch back to cache level 0
  70.     mcr p15, 2, r10, c0, c0, 0      @ select current cache level in cssr
  71.     dsb st
  72.     isb
  73.     bx  lr
  74.  
  75. v7_invalidate_l1:
  76.        mov     r0, #0
  77.        mcr     p15, 2, r0, c0, c0, 0
  78.        mrc     p15, 1, r0, c0, c0, 0
  79.  
  80.        movw    r1, #0x7fff
  81.        and     r2, r1, r0, lsr #13
  82.  
  83.        movw    r1, #0x3ff
  84.  
  85.        and     r3, r1, r0, lsr #3      @ NumWays - 1
  86.        add     r2, r2, #1              @ NumSets
  87.  
  88.        and     r0, r0, #0x7
  89.        add     r0, r0, #4      @ SetShift
  90.  
  91.        clz     r1, r3          @ WayShift
  92.        add     r4, r3, #1      @ NumWays
  93. 1:     sub     r2, r2, #1      @ NumSets--
  94.        mov     r3, r4          @ Temp = NumWays
  95. 2:     subs    r3, r3, #1      @ Temp--
  96.        mov     r5, r3, lsl r1
  97.        mov     r6, r2, lsl r0
  98.        orr     r5, r5, r6      @ Reg = (Temp<<WayShift)|(NumSets<<SetShift)
  99.        mcr     p15, 0, r5, c7, c6, 2
  100.        bgt     2b
  101.        cmp     r2, #0
  102.        bgt     1b
  103.        dsb     st
  104.        isb
  105.        bx     lr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement