xref: /rk3399_ARM-atf/lib/aarch64/cache_helpers.S (revision 54dc71e7ec9f2a069907e8c0c24b5c8f8cc5f66a)
14ecca339SDan Handley/*
24ecca339SDan Handley * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
34ecca339SDan Handley *
44ecca339SDan Handley * Redistribution and use in source and binary forms, with or without
54ecca339SDan Handley * modification, are permitted provided that the following conditions are met:
64ecca339SDan Handley *
74ecca339SDan Handley * Redistributions of source code must retain the above copyright notice, this
84ecca339SDan Handley * list of conditions and the following disclaimer.
94ecca339SDan Handley *
104ecca339SDan Handley * Redistributions in binary form must reproduce the above copyright notice,
114ecca339SDan Handley * this list of conditions and the following disclaimer in the documentation
124ecca339SDan Handley * and/or other materials provided with the distribution.
134ecca339SDan Handley *
144ecca339SDan Handley * Neither the name of ARM nor the names of its contributors may be used
154ecca339SDan Handley * to endorse or promote products derived from this software without specific
164ecca339SDan Handley * prior written permission.
174ecca339SDan Handley *
184ecca339SDan Handley * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
194ecca339SDan Handley * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204ecca339SDan Handley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214ecca339SDan Handley * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
224ecca339SDan Handley * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
234ecca339SDan Handley * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
244ecca339SDan Handley * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
254ecca339SDan Handley * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
264ecca339SDan Handley * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274ecca339SDan Handley * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284ecca339SDan Handley * POSSIBILITY OF SUCH DAMAGE.
294ecca339SDan Handley */
304ecca339SDan Handley
3197043ac9SDan Handley#include <arch.h>
324ecca339SDan Handley#include <asm_macros.S>
334ecca339SDan Handley
344ecca339SDan Handley	.globl	flush_dcache_range
35*54dc71e7SAchin Gupta	.globl	clean_dcache_range
364ecca339SDan Handley	.globl	inv_dcache_range
374ecca339SDan Handley	.globl	dcsw_op_louis
384ecca339SDan Handley	.globl	dcsw_op_all
398e857916SSoby Mathew	.globl	dcsw_op_level1
408e857916SSoby Mathew	.globl	dcsw_op_level2
418e857916SSoby Mathew	.globl	dcsw_op_level3
424ecca339SDan Handley
43*54dc71e7SAchin Gupta/*
44*54dc71e7SAchin Gupta * This macro can be used for implementing various data cache operations `op`
45*54dc71e7SAchin Gupta */
46*54dc71e7SAchin Gupta.macro do_dcache_maintenance_by_mva op
47*54dc71e7SAchin Gupta	dcache_line_size x2, x3
48*54dc71e7SAchin Gupta	add	x1, x0, x1
49*54dc71e7SAchin Gupta	sub	x3, x2, #1
50*54dc71e7SAchin Gupta	bic	x0, x0, x3
51*54dc71e7SAchin Guptaloop_\op:
52*54dc71e7SAchin Gupta	dc	\op, x0
53*54dc71e7SAchin Gupta	add	x0, x0, x2
54*54dc71e7SAchin Gupta	cmp	x0, x1
55*54dc71e7SAchin Gupta	b.lo    loop_\op
56*54dc71e7SAchin Gupta	dsb	sy
57*54dc71e7SAchin Gupta	ret
58*54dc71e7SAchin Gupta.endm
594ecca339SDan Handley	/* ------------------------------------------
604ecca339SDan Handley	 * Clean+Invalidate from base address till
614ecca339SDan Handley	 * size. 'x0' = addr, 'x1' = size
624ecca339SDan Handley	 * ------------------------------------------
634ecca339SDan Handley	 */
644ecca339SDan Handleyfunc flush_dcache_range
65*54dc71e7SAchin Gupta	do_dcache_maintenance_by_mva civac
668b779620SKévin Petitendfunc flush_dcache_range
674ecca339SDan Handley
68*54dc71e7SAchin Gupta	/* ------------------------------------------
69*54dc71e7SAchin Gupta	 * Clean from base address till size.
70*54dc71e7SAchin Gupta	 * 'x0' = addr, 'x1' = size
71*54dc71e7SAchin Gupta	 * ------------------------------------------
72*54dc71e7SAchin Gupta	 */
73*54dc71e7SAchin Guptafunc clean_dcache_range
74*54dc71e7SAchin Gupta	do_dcache_maintenance_by_mva cvac
75*54dc71e7SAchin Guptaendfunc clean_dcache_range
764ecca339SDan Handley
774ecca339SDan Handley	/* ------------------------------------------
784ecca339SDan Handley	 * Invalidate from base address till
794ecca339SDan Handley	 * size. 'x0' = addr, 'x1' = size
804ecca339SDan Handley	 * ------------------------------------------
814ecca339SDan Handley	 */
824ecca339SDan Handleyfunc inv_dcache_range
83*54dc71e7SAchin Gupta	do_dcache_maintenance_by_mva ivac
848b779620SKévin Petitendfunc inv_dcache_range
854ecca339SDan Handley
864ecca339SDan Handley
875f6032a8SAndrew Thoelke	/* ---------------------------------------------------------------
885f6032a8SAndrew Thoelke	 * Data cache operations by set/way to the level specified
895f6032a8SAndrew Thoelke	 *
905f6032a8SAndrew Thoelke	 * The main function, do_dcsw_op requires:
915f6032a8SAndrew Thoelke	 * x0: The operation type (0-2), as defined in arch.h
925f6032a8SAndrew Thoelke	 * x3: The last cache level to operate on
935f6032a8SAndrew Thoelke	 * x9: clidr_el1
948e857916SSoby Mathew	 * x10: The cache level to begin operation from
955f6032a8SAndrew Thoelke	 * and will carry out the operation on each data cache from level 0
965f6032a8SAndrew Thoelke	 * to the level in x3 in sequence
975f6032a8SAndrew Thoelke	 *
985f6032a8SAndrew Thoelke	 * The dcsw_op macro sets up the x3 and x9 parameters based on
995f6032a8SAndrew Thoelke	 * clidr_el1 cache information before invoking the main function
1005f6032a8SAndrew Thoelke	 * ---------------------------------------------------------------
1014ecca339SDan Handley	 */
1025f6032a8SAndrew Thoelke
1035f6032a8SAndrew Thoelke	.macro	dcsw_op shift, fw, ls
1045f6032a8SAndrew Thoelke	mrs	x9, clidr_el1
1055f6032a8SAndrew Thoelke	ubfx	x3, x9, \shift, \fw
1065f6032a8SAndrew Thoelke	lsl	x3, x3, \ls
1078e857916SSoby Mathew	mov	x10, xzr
1085f6032a8SAndrew Thoelke	b	do_dcsw_op
1095f6032a8SAndrew Thoelke	.endm
1105f6032a8SAndrew Thoelke
1115f6032a8SAndrew Thoelkefunc do_dcsw_op
1125f6032a8SAndrew Thoelke	cbz	x3, exit
1135f6032a8SAndrew Thoelke	adr	x14, dcsw_loop_table	// compute inner loop address
1145f6032a8SAndrew Thoelke	add	x14, x14, x0, lsl #5	// inner loop is 8x32-bit instructions
1155f6032a8SAndrew Thoelke	mov	x0, x9
1165f6032a8SAndrew Thoelke	mov	w8, #1
1175f6032a8SAndrew Thoelkeloop1:
1184ecca339SDan Handley	add	x2, x10, x10, lsr #1	// work out 3x current cache level
1194ecca339SDan Handley	lsr	x1, x0, x2		// extract cache type bits from clidr
1205f6032a8SAndrew Thoelke	and	x1, x1, #7		// mask the bits for current cache only
1214ecca339SDan Handley	cmp	x1, #2			// see what cache we have at this level
1225f6032a8SAndrew Thoelke	b.lt	level_done		// nothing to do if no cache or icache
1235f6032a8SAndrew Thoelke
1244ecca339SDan Handley	msr	csselr_el1, x10		// select current cache level in csselr
1254ecca339SDan Handley	isb				// isb to sych the new cssr&csidr
1264ecca339SDan Handley	mrs	x1, ccsidr_el1		// read the new ccsidr
1274ecca339SDan Handley	and	x2, x1, #7		// extract the length of the cache lines
1284ecca339SDan Handley	add	x2, x2, #4		// add 4 (line length offset)
1295f6032a8SAndrew Thoelke	ubfx	x4, x1, #3, #10		// maximum way number
1305f6032a8SAndrew Thoelke	clz	w5, w4			// bit position of way size increment
1315f6032a8SAndrew Thoelke	lsl	w9, w4, w5		// w9 = aligned max way number
1325f6032a8SAndrew Thoelke	lsl	w16, w8, w5		// w16 = way number loop decrement
1335f6032a8SAndrew Thoelke	orr	w9, w10, w9		// w9 = combine way and cache number
1345f6032a8SAndrew Thoelke	ubfx	w6, w1, #13, #15	// w6 = max set number
1355f6032a8SAndrew Thoelke	lsl	w17, w8, w2		// w17 = set number loop decrement
1365f6032a8SAndrew Thoelke	dsb	sy			// barrier before we start this level
1375f6032a8SAndrew Thoelke	br	x14			// jump to DC operation specific loop
1385f6032a8SAndrew Thoelke
1395f6032a8SAndrew Thoelke	.macro	dcsw_loop _op
1405f6032a8SAndrew Thoelkeloop2_\_op:
1415f6032a8SAndrew Thoelke	lsl	w7, w6, w2		// w7 = aligned max set number
1425f6032a8SAndrew Thoelke
1435f6032a8SAndrew Thoelkeloop3_\_op:
1445f6032a8SAndrew Thoelke	orr	w11, w9, w7		// combine cache, way and set number
1455f6032a8SAndrew Thoelke	dc	\_op, x11
1465f6032a8SAndrew Thoelke	subs	w7, w7, w17		// decrement set number
1475f6032a8SAndrew Thoelke	b.ge	loop3_\_op
1485f6032a8SAndrew Thoelke
1495f6032a8SAndrew Thoelke	subs	x9, x9, x16		// decrement way number
1505f6032a8SAndrew Thoelke	b.ge	loop2_\_op
1515f6032a8SAndrew Thoelke
1525f6032a8SAndrew Thoelke	b	level_done
1535f6032a8SAndrew Thoelke	.endm
1545f6032a8SAndrew Thoelke
1555f6032a8SAndrew Thoelkelevel_done:
1564ecca339SDan Handley	add	x10, x10, #2		// increment cache number
1574ecca339SDan Handley	cmp	x3, x10
1585f6032a8SAndrew Thoelke	b.gt    loop1
1595f6032a8SAndrew Thoelke	msr	csselr_el1, xzr		// select cache level 0 in csselr
1605f6032a8SAndrew Thoelke	dsb	sy			// barrier to complete final cache operation
1614ecca339SDan Handley	isb
1624ecca339SDan Handleyexit:
1634ecca339SDan Handley	ret
1648b779620SKévin Petitendfunc do_dcsw_op
1654ecca339SDan Handley
1665f6032a8SAndrew Thoelkedcsw_loop_table:
1675f6032a8SAndrew Thoelke	dcsw_loop isw
1685f6032a8SAndrew Thoelke	dcsw_loop cisw
1695f6032a8SAndrew Thoelke	dcsw_loop csw
1705f6032a8SAndrew Thoelke
1714ecca339SDan Handley
1724ecca339SDan Handleyfunc dcsw_op_louis
1735f6032a8SAndrew Thoelke	dcsw_op #LOUIS_SHIFT, #CLIDR_FIELD_WIDTH, #LEVEL_SHIFT
1748b779620SKévin Petitendfunc dcsw_op_louis
1754ecca339SDan Handley
1764ecca339SDan Handley
1774ecca339SDan Handleyfunc dcsw_op_all
1785f6032a8SAndrew Thoelke	dcsw_op #LOC_SHIFT, #CLIDR_FIELD_WIDTH, #LEVEL_SHIFT
1798b779620SKévin Petitendfunc dcsw_op_all
1808e857916SSoby Mathew
1818e857916SSoby Mathew	/* ---------------------------------------------------------------
1828e857916SSoby Mathew	 *  Helper macro for data cache operations by set/way for the
1838e857916SSoby Mathew	 *  level specified
1848e857916SSoby Mathew	 * ---------------------------------------------------------------
1858e857916SSoby Mathew	 */
1868e857916SSoby Mathew	.macro dcsw_op_level level
1878e857916SSoby Mathew	mrs	x9, clidr_el1
1888e857916SSoby Mathew	mov	x3, \level
1898e857916SSoby Mathew	sub	x10, x3, #2
1908e857916SSoby Mathew	b	do_dcsw_op
1918e857916SSoby Mathew	.endm
1928e857916SSoby Mathew
1938e857916SSoby Mathew	/* ---------------------------------------------------------------
1948e857916SSoby Mathew	 * Data cache operations by set/way for level 1 cache
1958e857916SSoby Mathew	 *
1968e857916SSoby Mathew	 * The main function, do_dcsw_op requires:
1978e857916SSoby Mathew	 * x0: The operation type (0-2), as defined in arch.h
1988e857916SSoby Mathew	 * ---------------------------------------------------------------
1998e857916SSoby Mathew	 */
2008e857916SSoby Mathewfunc dcsw_op_level1
2018e857916SSoby Mathew	dcsw_op_level #(1 << LEVEL_SHIFT)
2028b779620SKévin Petitendfunc dcsw_op_level1
2038e857916SSoby Mathew
2048e857916SSoby Mathew	/* ---------------------------------------------------------------
2058e857916SSoby Mathew	 * Data cache operations by set/way for level 2 cache
2068e857916SSoby Mathew	 *
2078e857916SSoby Mathew	 * The main function, do_dcsw_op requires:
2088e857916SSoby Mathew	 * x0: The operation type (0-2), as defined in arch.h
2098e857916SSoby Mathew	 * ---------------------------------------------------------------
2108e857916SSoby Mathew	 */
2118e857916SSoby Mathewfunc dcsw_op_level2
2128e857916SSoby Mathew	dcsw_op_level #(2 << LEVEL_SHIFT)
2138b779620SKévin Petitendfunc dcsw_op_level2
2148e857916SSoby Mathew
2158e857916SSoby Mathew	/* ---------------------------------------------------------------
2168e857916SSoby Mathew	 * Data cache operations by set/way for level 3 cache
2178e857916SSoby Mathew	 *
2188e857916SSoby Mathew	 * The main function, do_dcsw_op requires:
2198e857916SSoby Mathew	 * x0: The operation type (0-2), as defined in arch.h
2208e857916SSoby Mathew	 * ---------------------------------------------------------------
2218e857916SSoby Mathew	 */
2228e857916SSoby Mathewfunc dcsw_op_level3
2238e857916SSoby Mathew	dcsw_op_level #(3 << LEVEL_SHIFT)
2248b779620SKévin Petitendfunc dcsw_op_level3
225