blob: 8a545dc1d6af5a5ad1c8f8853575b6bae90a6ec2 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <asm.h>
24#include <arch/ops.h>
25#include <arch/defines.h>
26
27.text
28
29#if ARM_WITH_CACHE
30
31/* low level cache routines for various cpu families */
32
33#if ARM_CPU_ARM1136 || ARM_CPU_ARM926
34
35/* void arch_disable_cache(uint flags) */
36FUNCTION(arch_disable_cache)
37 mov r12, #0 // zero register
38 mrs r3, cpsr // save the old interrupt state
39#if ARM_ISA_ARMv6
40 .word 0xf10c01c0 /* cpsid iaf */ // interrupts disabled
41#else
42 orr r3, r3, #(1<<7)
43 msr cpsr, r3
44#endif
45
46.Ldcache_disable:
47 tst r0, #DCACHE
48 beq .Licache_disable
49 mrc p15, 0, r1, c1, c0, 0 // cr1
50 tst r1, #(1<<2) // is the dcache already disabled?
51 beq .Licache_disable
52
53 bic r1, #(1<<2)
54 mcr p15, 0, r1, c1, c0, 0 // disable dcache
55
56#if ARM_CPU_ARM1136
57 mcr p15, 0, r12, c7, c14, 0 // clean & invalidate dcache
58#elif ARM_CPU_ARM926
590:
60 mrc p15, 0, r15, c7, c14, 3 // clean & invalidate dcache
61 bne 0b
62#else
63#error whut?
64#endif
65 mcr p15, 0, r0, c7, c10, 4 // data sync barrier (formerly drain write buffer)
66
67.Licache_disable:
68 tst r0, #ICACHE
69 beq .Ldone_disable
70
71 mrc p15, 0, r1, c1, c0, 0 // cr1
72 bic r1, #(1<<12)
73 mcr p15, 0, r1, c1, c0, 0 // disable icache
74
75 mcr p15, 0, r12, c7, c5, 0 // invalidate icache
76
77.Ldone_disable:
78 msr cpsr, r3
79 bx lr
80
81/* void arch_enable_cache(uint flags) */
82FUNCTION(arch_enable_cache)
83 mov r12, #0 // zero register
84 mrs r3, cpsr // save the old interrupt state
85#if ARM_ISA_ARMv6
86 .word 0xf10c01c0 /* cpsid iaf */ // interrupts disabled
87#else
88 orr r3, r3, #(1<<7)
89 msr cpsr, r3
90#endif
91
92.Ldcache_enable:
93 tst r0, #DCACHE
94 beq .Licache_enable
95 mrc p15, 0, r1, c1, c0, 0 // cr1
96 tst r1, #(1<<2) // is the dcache already enabled?
97 bne .Licache_enable
98
99 mcr p15, 0, r12, c7, c6, 0 // invalidate dcache
100
101 orr r1, #(1<<2)
102 mcr p15, 0, r1, c1, c0, 0 // enable dcache
103
104.Licache_enable:
105 tst r0, #ICACHE
106 beq .Ldone_enable
107
108 mcr p15, 0, r12, c7, c5, 0 // invalidate icache
109
110 mrc p15, 0, r1, c1, c0, 0 // cr1
111 orr r1, #(1<<12)
112 mcr p15, 0, r1, c1, c0, 0 // enable icache
113
114.Ldone_enable:
115 msr cpsr, r3
116 bx lr
117
118#elif ARM_CPU_CORTEX_A8
119
120/* void arch_disable_cache(uint flags) */
121FUNCTION(arch_disable_cache)
122 stmfd sp!, {r4-r11, lr}
123
124 mov r7, r0 // save flags
125
126 mrs r12, cpsr // save the old interrupt state
127 .word 0xf10c01c0 /* cpsid iaf */ // interrupts disabled
128
129.Ldcache_disable:
130 tst r7, #DCACHE
131 beq .Licache_disable
132 mrc p15, 0, r0, c1, c0, 0 // cr1
133 tst r0, #(1<<2) // is the dcache already disabled?
134 beq .Ldcache_already_disabled
135
136 bic r0, #(1<<2)
137 mcr p15, 0, r0, c1, c0, 0 // disable dcache
138
139 // flush and invalidate the dcache
140 // NOTE: trashes a bunch of registers, can't be spilling stuff to the stack
141 bl flush_invalidate_cache_v7
142
143 b .Ldcache_disable_L2
144
145.Ldcache_already_disabled:
146 // make sure all of the caches are invalidated
147 // NOTE: trashes a bunch of registers, can't be spilling stuff to the stack
148 bl invalidate_cache_v7
149
150.Ldcache_disable_L2:
151
152#if ARM_WITH_L2
153 // disable the L2, if present
154 mrc p15, 0, r0, c1, c0, 1 // aux cr1
155 bic r0, #(1<<1)
156 mcr p15, 0, r0, c1, c0, 1 // disable L2 dcache
157#endif
158
159.Licache_disable:
160 tst r7, #ICACHE
161 beq .Ldone_disable
162
163 mrc p15, 0, r0, c1, c0, 0 // cr1
164 bic r0, #(1<<12)
165 mcr p15, 0, r0, c1, c0, 0 // disable icache
166
167.Ldone_disable:
168 // make sure the icache is always invalidated
169 mov r0, #0
170 mcr p15, 0, r0, c7, c5, 0 // invalidate icache to PoU
171
172 msr cpsr, r12
173 ldmfd sp!, {r4-r11, pc}
174
175/* void arch_enable_cache(uint flags) */
176FUNCTION(arch_enable_cache)
177 stmfd sp!, {r4-r11, lr}
178
179 mov r7, r0 // save flags
180
181 mrs r12, cpsr // save the old interrupt state
182 .word 0xf10c01c0 /* cpsid iaf */ // interrupts disabled
183
184.Ldcache_enable:
185 tst r7, #DCACHE
186 beq .Licache_enable
187 mrc p15, 0, r0, c1, c0, 0 // cr1
188 tst r0, #(1<<2) // is the dcache already enabled?
189 bne .Licache_enable
190
191 // invalidate L1 and L2
192 // NOTE: trashes a bunch of registers, can't be spilling stuff to the stack
193 bl invalidate_cache_v7
194
195#if ARM_WITH_L2
196 // enable the L2, if present
197 mrc p15, 0, r0, c1, c0, 1 // aux cr1
198 orr r0, #(1<<1)
199 mcr p15, 0, r0, c1, c0, 1 // enable L2 dcache
200#endif
201
202 mrc p15, 0, r0, c1, c0, 0 // cr1
203 orr r0, #(1<<2)
204 mcr p15, 0, r0, c1, c0, 0 // enable dcache
205
206.Licache_enable:
207 tst r7, #ICACHE
208 beq .Ldone_enable
209
210 mov r0, #0
211 mcr p15, 0, r0, c7, c5, 0 // invalidate icache to PoU
212
213 mrc p15, 0, r0, c1, c0, 0 // cr1
214 orr r0, #(1<<12)
215 mcr p15, 0, r0, c1, c0, 0 // enable icache
216
217.Ldone_enable:
218 msr cpsr, r12
219 ldmfd sp!, {r4-r11, pc}
220
221// flush & invalidate cache routine, trashes r0-r6, r9-r11
222flush_invalidate_cache_v7:
223 /* from ARMv7 manual, B2-17 */
224 MRC p15, 1, R0, c0, c0, 1 // Read CLIDR
225 ANDS R3, R0, #0x7000000
226 MOV R3, R3, LSR #23 // Cache level value (naturally aligned)
227 BEQ .Lfinished
228 MOV R10, #0
229.Loop1:
230 ADD R2, R10, R10, LSR #1 // Work out 3xcachelevel
231 MOV R1, R0, LSR R2 // bottom 3 bits are the Cache type for this level
232 AND R1, R1, #7 // get those 3 bits alone
233 CMP R1, #2
234 BLT .Lskip // no cache or only instruction cache at this level
235 MCR p15, 2, R10, c0, c0, 0 // write the Cache Size selection register
236 .word 0xf57ff06f // ISB // ISB to sync the change to the CacheSizeID reg
237 MRC p15, 1, R1, c0, c0, 0 // reads current Cache Size ID register
238 AND R2, R1, #0x7 // extract the line length field
239 ADD R2, R2, #4 // add 4 for the line length offset (log2 16 bytes)
240 LDR R4, =0x3FF
241 ANDS R4, R4, R1, LSR #3 // R4 is the max number on the way size (right aligned)
242 CLZ R5, R4 // R5 is the bit position of the way size increment
243 LDR R6, =0x00007FFF
244 ANDS R6, R6, R1, LSR #13 // R6 is the max number of the index size (right aligned)
245.Loop2:
246 MOV R9, R4 // R9 working copy of the max way size (right aligned)
247.Loop3:
248 ORR R11, R10, R9, LSL R5 // factor in the way number and cache number into R11
249 ORR R11, R11, R6, LSL R2 // factor in the index number
250 MCR p15, 0, R11, c7, c14, 2 // clean & invalidate by set/way
251 SUBS R9, R9, #1 // decrement the way number
252 BGE .Loop3
253 SUBS R6, R6, #1 // decrement the index
254 BGE .Loop2
255.Lskip:
256 ADD R10, R10, #2 // increment the cache number
257 CMP R3, R10
258 BGT .Loop1
259
260.Lfinished:
261 mov r10, #0
262 mcr p15, 2, r10, c0, c0, 0 // select cache level 0
263 .word 0xf57ff06f // isb
264
265 bx lr
266
267// invalidate cache routine, trashes r0-r6, r9-r11
268invalidate_cache_v7:
269 /* from ARMv7 manual, B2-17 */
270 MRC p15, 1, R0, c0, c0, 1 // Read CLIDR
271 ANDS R3, R0, #0x7000000
272 MOV R3, R3, LSR #23 // Cache level value (naturally aligned)
273 BEQ .Lfinished_invalidate
274 MOV R10, #0
275.Loop1_invalidate:
276 ADD R2, R10, R10, LSR #1 // Work out 3xcachelevel
277 MOV R1, R0, LSR R2 // bottom 3 bits are the Cache type for this level
278 AND R1, R1, #7 // get those 3 bits alone
279 CMP R1, #2
280 BLT .Lskip_invalidate // no cache or only instruction cache at this level
281 MCR p15, 2, R10, c0, c0, 0 // write the Cache Size selection register
282 .word 0xf57ff06f // ISB // ISB to sync the change to the CacheSizeID reg
283 MRC p15, 1, R1, c0, c0, 0 // reads current Cache Size ID register
284 AND R2, R1, #0x7 // extract the line length field
285 ADD R2, R2, #4 // add 4 for the line length offset (log2 16 bytes)
286 LDR R4, =0x3FF
287 ANDS R4, R4, R1, LSR #3 // R4 is the max number on the way size (right aligned)
288 CLZ R5, R4 // R5 is the bit position of the way size increment
289 LDR R6, =0x00007FFF
290 ANDS R6, R6, R1, LSR #13 // R6 is the max number of the index size (right aligned)
291.Loop2_invalidate:
292 MOV R9, R4 // R9 working copy of the max way size (right aligned)
293.Loop3_invalidate:
294 ORR R11, R10, R9, LSL R5 // factor in the way number and cache number into R11
295 ORR R11, R11, R6, LSL R2 // factor in the index number
296 MCR p15, 0, R11, c7, c6, 2 // invalidate by set/way
297 SUBS R9, R9, #1 // decrement the way number
298 BGE .Loop3_invalidate
299 SUBS R6, R6, #1 // decrement the index
300 BGE .Loop2_invalidate
301.Lskip_invalidate:
302 ADD R10, R10, #2 // increment the cache number
303 CMP R3, R10
304 BGT .Loop1_invalidate
305
306.Lfinished_invalidate:
307 mov r10, #0
308 mcr p15, 2, r10, c0, c0, 0 // select cache level 0
309 .word 0xf57ff06f // isb
310
311 bx lr
312
313#else
314#error unhandled cpu
315#endif
316
317#if ARM_CPU_ARM926 || ARM_CPU_ARM1136 || ARM_CPU_CORTEX_A8
318/* shared cache flush routines */
319
320 /* void arch_flush_cache_range(addr_t start, size_t len); */
321FUNCTION(arch_clean_cache_range)
3220:
323 mcr p15, 0, r0, c7, c10, 1 // clean cache to PoC by MVA
324 add r0, r0, #CACHE_LINE
325 subs r1, r1, #CACHE_LINE
326 bhs 0b
327
328 mov r0, #0
329 mcr p15, 0, r0, c7, c10, 4 // data sync barrier (formerly drain write buffer)
330
331 bx lr
332
333 /* void arch_flush_invalidate_cache_range(addr_t start, size_t len); */
334FUNCTION(arch_clean_invalidate_cache_range)
3350:
336 mcr p15, 0, r0, c7, c14, 1 // clean & invalidate cache to PoC by MVA
337 add r0, r0, #CACHE_LINE
338 subs r1, r1, #CACHE_LINE
339 bhs 0b
340
341 mov r0, #0
342 mcr p15, 0, r0, c7, c10, 4 // data sync barrier (formerly drain write buffer)
343
344 bx lr
345#else
346#error unhandled cpu
347#endif
348
349#else
350
351/* no cache */
352
353FUNCTION(arch_disable_cache)
354 bx lr
355
356FUNCTION(arch_enable_cache)
357 bx lr
358
359FUNCTION(arch_clean_cache_range)
360 bx lr
361
362FUNCTION(arch_clean_invalidate_cache_range)
363 bx lr
364
365#endif // ARM_WITH_CACHE
366