2 * Optimized assembly for low-level CPU operations on ARMv7 processors.
4 * Cache flushing code based off sys/arch/arm/arm/cpufunc_asm_armv7.S in NetBSD
6 * Copyright (c) 2010 Per Odlund <per.odlund@armagedon.se>
7 * Copyright (c) 2014 Google Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Dcache invalidations by set/way work by passing a [way:sbz:set:sbz:level:0]
37 * bitfield in a register to the appropriate MCR instruction. This algorithm
38 * works by initializing a bitfield with the highest-numbered set and way, and
39 * generating a "set decrement" and a "way decrement". The former just contains
40 * the LSB of the set field, but the latter contains the LSB of the way field
41 * minus the highest valid set field... such that when you subtract it from a
42 * [way:0:level] field you end up with a [way - 1:highest_set:level] field
43 * through the magic of double subtraction. It's quite ingenius, really.
44 * Takes care to only use r0-r3 and ip so it's pefectly ABI-compatible without
45 * needing to write to memory.
48 .macro dcache_apply_all crm
50 mov r3, #-2 @ initialize level so that we start at 0
53 add r3, r3, #2 @ increment level
55 mrc p15, 1, r0, c0, c0, 1 @ read CLIDR
56 and ip, r0, #0x07000000 @ narrow to LoC
57 lsr ip, ip, #23 @ left align LoC (low 4 bits)
59 bge 3f @done @ else fall through (r0 == CLIDR)
61 add r2, r3, r3, lsr #1 @ r2 = (level << 1) * 3 / 2
62 mov r1, r0, lsr r2 @ r1 = cache type
64 cmp r1, #2 @ is it data or i&d?
65 blt 1b @next_level @ nope, skip level
67 mcr p15, 2, r3, c0, c0, 0 @ select cache level
69 mrc p15, 1, r0, c0, c0, 0 @ read CCSIDR
71 ubfx ip, r0, #0, #3 @ get linesize from CCSIDR
72 add ip, ip, #4 @ apply bias
73 ubfx r2, r0, #13, #15 @ get numsets - 1 from CCSIDR
74 lsl r2, r2, ip @ shift to set position
75 orr r3, r3, r2 @ merge set into way/set/level
77 lsl r1, r1, ip @ r1 = set decr
79 ubfx ip, r0, #3, #10 @ get numways - 1 from [to be discarded] CCSIDR
80 clz r2, ip @ number of bits to MSB of way
81 lsl ip, ip, r2 @ shift by that into way position
83 lsl r2, r0, r2 @ r2 now contains the way decr
84 mov r0, r3 @ get sets/level (no way yet)
85 orr r3, r3, ip @ merge way into way/set/level
86 bfc r0, #0, #4 @ clear low 4 bits (level) to get numset - 1
87 sub r2, r2, r0 @ subtract from way decr
89 /* r3 = ways/sets/level, r2 = way decr, r1 = set decr, r0 and ip are free */
90 2: mcr p15, 0, r3, c7, \crm, 2 @ writeback and/or invalidate line
91 cmp r3, #15 @ are we done with this level (way/set == 0)
92 bls 1b @next_level @ yes, go to next level
93 lsr r0, r3, #4 @ clear level bits leaving only way/set bits
94 lsls r0, r0, #14 @ clear way bits leaving only set bits
95 subne r3, r3, r1 @ non-zero?, decrement set #
96 subeq r3, r3, r2 @ zero?, decrement way # and restore set count
100 mov r0, #0 @ default back to cache level 0
101 mcr p15, 2, r0, c0, c0, 0 @ select cache level
107 ENTRY(dcache_invalidate_all)
108 dcache_apply_all crm=c6
109 ENDPROC(dcache_invalidate_all)
111 ENTRY(dcache_clean_all)
112 dcache_apply_all crm=c10
113 ENDPROC(dcache_clean_all)
115 ENTRY(dcache_clean_invalidate_all)
116 dcache_apply_all crm=c14
117 ENDPROC(dcache_clean_invalidate_all)