xref: /rk3399_ARM-atf/lib/aarch64/cache_helpers.S (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
14ecca339SDan Handley/*
24ecca339SDan Handley * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
34ecca339SDan Handley *
4*82cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
54ecca339SDan Handley */
64ecca339SDan Handley
797043ac9SDan Handley#include <arch.h>
84ecca339SDan Handley#include <asm_macros.S>
94ecca339SDan Handley
104ecca339SDan Handley	.globl	flush_dcache_range
1154dc71e7SAchin Gupta	.globl	clean_dcache_range
124ecca339SDan Handley	.globl	inv_dcache_range
134ecca339SDan Handley	.globl	dcsw_op_louis
144ecca339SDan Handley	.globl	dcsw_op_all
158e857916SSoby Mathew	.globl	dcsw_op_level1
168e857916SSoby Mathew	.globl	dcsw_op_level2
178e857916SSoby Mathew	.globl	dcsw_op_level3
184ecca339SDan Handley
1954dc71e7SAchin Gupta/*
2054dc71e7SAchin Gupta * This macro can be used for implementing various data cache operations `op`
2154dc71e7SAchin Gupta */
2254dc71e7SAchin Gupta.macro do_dcache_maintenance_by_mva op
2354dc71e7SAchin Gupta	dcache_line_size x2, x3
2454dc71e7SAchin Gupta	add	x1, x0, x1
2554dc71e7SAchin Gupta	sub	x3, x2, #1
2654dc71e7SAchin Gupta	bic	x0, x0, x3
2754dc71e7SAchin Guptaloop_\op:
2854dc71e7SAchin Gupta	dc	\op, x0
2954dc71e7SAchin Gupta	add	x0, x0, x2
3054dc71e7SAchin Gupta	cmp	x0, x1
3154dc71e7SAchin Gupta	b.lo    loop_\op
3254dc71e7SAchin Gupta	dsb	sy
3354dc71e7SAchin Gupta	ret
3454dc71e7SAchin Gupta.endm
354ecca339SDan Handley	/* ------------------------------------------
364ecca339SDan Handley	 * Clean+Invalidate from base address till
374ecca339SDan Handley	 * size. 'x0' = addr, 'x1' = size
384ecca339SDan Handley	 * ------------------------------------------
394ecca339SDan Handley	 */
404ecca339SDan Handleyfunc flush_dcache_range
4154dc71e7SAchin Gupta	do_dcache_maintenance_by_mva civac
428b779620SKévin Petitendfunc flush_dcache_range
434ecca339SDan Handley
4454dc71e7SAchin Gupta	/* ------------------------------------------
4554dc71e7SAchin Gupta	 * Clean from base address till size.
4654dc71e7SAchin Gupta	 * 'x0' = addr, 'x1' = size
4754dc71e7SAchin Gupta	 * ------------------------------------------
4854dc71e7SAchin Gupta	 */
4954dc71e7SAchin Guptafunc clean_dcache_range
5054dc71e7SAchin Gupta	do_dcache_maintenance_by_mva cvac
5154dc71e7SAchin Guptaendfunc clean_dcache_range
524ecca339SDan Handley
534ecca339SDan Handley	/* ------------------------------------------
544ecca339SDan Handley	 * Invalidate from base address till
554ecca339SDan Handley	 * size. 'x0' = addr, 'x1' = size
564ecca339SDan Handley	 * ------------------------------------------
574ecca339SDan Handley	 */
584ecca339SDan Handleyfunc inv_dcache_range
5954dc71e7SAchin Gupta	do_dcache_maintenance_by_mva ivac
608b779620SKévin Petitendfunc inv_dcache_range
614ecca339SDan Handley
624ecca339SDan Handley
635f6032a8SAndrew Thoelke	/* ---------------------------------------------------------------
645f6032a8SAndrew Thoelke	 * Data cache operations by set/way to the level specified
655f6032a8SAndrew Thoelke	 *
665f6032a8SAndrew Thoelke	 * The main function, do_dcsw_op requires:
675f6032a8SAndrew Thoelke	 * x0: The operation type (0-2), as defined in arch.h
685f6032a8SAndrew Thoelke	 * x3: The last cache level to operate on
695f6032a8SAndrew Thoelke	 * x9: clidr_el1
708e857916SSoby Mathew	 * x10: The cache level to begin operation from
715f6032a8SAndrew Thoelke	 * and will carry out the operation on each data cache from level 0
725f6032a8SAndrew Thoelke	 * to the level in x3 in sequence
735f6032a8SAndrew Thoelke	 *
745f6032a8SAndrew Thoelke	 * The dcsw_op macro sets up the x3 and x9 parameters based on
755f6032a8SAndrew Thoelke	 * clidr_el1 cache information before invoking the main function
765f6032a8SAndrew Thoelke	 * ---------------------------------------------------------------
774ecca339SDan Handley	 */
785f6032a8SAndrew Thoelke
795f6032a8SAndrew Thoelke	.macro	dcsw_op shift, fw, ls
805f6032a8SAndrew Thoelke	mrs	x9, clidr_el1
815f6032a8SAndrew Thoelke	ubfx	x3, x9, \shift, \fw
825f6032a8SAndrew Thoelke	lsl	x3, x3, \ls
838e857916SSoby Mathew	mov	x10, xzr
845f6032a8SAndrew Thoelke	b	do_dcsw_op
855f6032a8SAndrew Thoelke	.endm
865f6032a8SAndrew Thoelke
875f6032a8SAndrew Thoelkefunc do_dcsw_op
885f6032a8SAndrew Thoelke	cbz	x3, exit
895f6032a8SAndrew Thoelke	adr	x14, dcsw_loop_table	// compute inner loop address
905f6032a8SAndrew Thoelke	add	x14, x14, x0, lsl #5	// inner loop is 8x32-bit instructions
915f6032a8SAndrew Thoelke	mov	x0, x9
925f6032a8SAndrew Thoelke	mov	w8, #1
935f6032a8SAndrew Thoelkeloop1:
944ecca339SDan Handley	add	x2, x10, x10, lsr #1	// work out 3x current cache level
954ecca339SDan Handley	lsr	x1, x0, x2		// extract cache type bits from clidr
965f6032a8SAndrew Thoelke	and	x1, x1, #7		// mask the bits for current cache only
974ecca339SDan Handley	cmp	x1, #2			// see what cache we have at this level
98355a5d03SDouglas Raillard	b.lo	level_done		// nothing to do if no cache or icache
995f6032a8SAndrew Thoelke
1004ecca339SDan Handley	msr	csselr_el1, x10		// select current cache level in csselr
1014ecca339SDan Handley	isb				// isb to sych the new cssr&csidr
1024ecca339SDan Handley	mrs	x1, ccsidr_el1		// read the new ccsidr
1034ecca339SDan Handley	and	x2, x1, #7		// extract the length of the cache lines
1044ecca339SDan Handley	add	x2, x2, #4		// add 4 (line length offset)
1055f6032a8SAndrew Thoelke	ubfx	x4, x1, #3, #10		// maximum way number
1065f6032a8SAndrew Thoelke	clz	w5, w4			// bit position of way size increment
1075f6032a8SAndrew Thoelke	lsl	w9, w4, w5		// w9 = aligned max way number
1085f6032a8SAndrew Thoelke	lsl	w16, w8, w5		// w16 = way number loop decrement
1095f6032a8SAndrew Thoelke	orr	w9, w10, w9		// w9 = combine way and cache number
1105f6032a8SAndrew Thoelke	ubfx	w6, w1, #13, #15	// w6 = max set number
1115f6032a8SAndrew Thoelke	lsl	w17, w8, w2		// w17 = set number loop decrement
1125f6032a8SAndrew Thoelke	dsb	sy			// barrier before we start this level
1135f6032a8SAndrew Thoelke	br	x14			// jump to DC operation specific loop
1145f6032a8SAndrew Thoelke
1155f6032a8SAndrew Thoelke	.macro	dcsw_loop _op
1165f6032a8SAndrew Thoelkeloop2_\_op:
1175f6032a8SAndrew Thoelke	lsl	w7, w6, w2		// w7 = aligned max set number
1185f6032a8SAndrew Thoelke
1195f6032a8SAndrew Thoelkeloop3_\_op:
1205f6032a8SAndrew Thoelke	orr	w11, w9, w7		// combine cache, way and set number
1215f6032a8SAndrew Thoelke	dc	\_op, x11
1225f6032a8SAndrew Thoelke	subs	w7, w7, w17		// decrement set number
123355a5d03SDouglas Raillard	b.hs	loop3_\_op
1245f6032a8SAndrew Thoelke
1255f6032a8SAndrew Thoelke	subs	x9, x9, x16		// decrement way number
126355a5d03SDouglas Raillard	b.hs	loop2_\_op
1275f6032a8SAndrew Thoelke
1285f6032a8SAndrew Thoelke	b	level_done
1295f6032a8SAndrew Thoelke	.endm
1305f6032a8SAndrew Thoelke
1315f6032a8SAndrew Thoelkelevel_done:
1324ecca339SDan Handley	add	x10, x10, #2		// increment cache number
1334ecca339SDan Handley	cmp	x3, x10
134355a5d03SDouglas Raillard	b.hi    loop1
1355f6032a8SAndrew Thoelke	msr	csselr_el1, xzr		// select cache level 0 in csselr
1365f6032a8SAndrew Thoelke	dsb	sy			// barrier to complete final cache operation
1374ecca339SDan Handley	isb
1384ecca339SDan Handleyexit:
1394ecca339SDan Handley	ret
1408b779620SKévin Petitendfunc do_dcsw_op
1414ecca339SDan Handley
1425f6032a8SAndrew Thoelkedcsw_loop_table:
1435f6032a8SAndrew Thoelke	dcsw_loop isw
1445f6032a8SAndrew Thoelke	dcsw_loop cisw
1455f6032a8SAndrew Thoelke	dcsw_loop csw
1465f6032a8SAndrew Thoelke
1474ecca339SDan Handley
1484ecca339SDan Handleyfunc dcsw_op_louis
1495f6032a8SAndrew Thoelke	dcsw_op #LOUIS_SHIFT, #CLIDR_FIELD_WIDTH, #LEVEL_SHIFT
1508b779620SKévin Petitendfunc dcsw_op_louis
1514ecca339SDan Handley
1524ecca339SDan Handley
1534ecca339SDan Handleyfunc dcsw_op_all
1545f6032a8SAndrew Thoelke	dcsw_op #LOC_SHIFT, #CLIDR_FIELD_WIDTH, #LEVEL_SHIFT
1558b779620SKévin Petitendfunc dcsw_op_all
1568e857916SSoby Mathew
1578e857916SSoby Mathew	/* ---------------------------------------------------------------
1588e857916SSoby Mathew	 *  Helper macro for data cache operations by set/way for the
1598e857916SSoby Mathew	 *  level specified
1608e857916SSoby Mathew	 * ---------------------------------------------------------------
1618e857916SSoby Mathew	 */
1628e857916SSoby Mathew	.macro dcsw_op_level level
1638e857916SSoby Mathew	mrs	x9, clidr_el1
1648e857916SSoby Mathew	mov	x3, \level
1658e857916SSoby Mathew	sub	x10, x3, #2
1668e857916SSoby Mathew	b	do_dcsw_op
1678e857916SSoby Mathew	.endm
1688e857916SSoby Mathew
1698e857916SSoby Mathew	/* ---------------------------------------------------------------
1708e857916SSoby Mathew	 * Data cache operations by set/way for level 1 cache
1718e857916SSoby Mathew	 *
1728e857916SSoby Mathew	 * The main function, do_dcsw_op requires:
1738e857916SSoby Mathew	 * x0: The operation type (0-2), as defined in arch.h
1748e857916SSoby Mathew	 * ---------------------------------------------------------------
1758e857916SSoby Mathew	 */
1768e857916SSoby Mathewfunc dcsw_op_level1
1778e857916SSoby Mathew	dcsw_op_level #(1 << LEVEL_SHIFT)
1788b779620SKévin Petitendfunc dcsw_op_level1
1798e857916SSoby Mathew
1808e857916SSoby Mathew	/* ---------------------------------------------------------------
1818e857916SSoby Mathew	 * Data cache operations by set/way for level 2 cache
1828e857916SSoby Mathew	 *
1838e857916SSoby Mathew	 * The main function, do_dcsw_op requires:
1848e857916SSoby Mathew	 * x0: The operation type (0-2), as defined in arch.h
1858e857916SSoby Mathew	 * ---------------------------------------------------------------
1868e857916SSoby Mathew	 */
1878e857916SSoby Mathewfunc dcsw_op_level2
1888e857916SSoby Mathew	dcsw_op_level #(2 << LEVEL_SHIFT)
1898b779620SKévin Petitendfunc dcsw_op_level2
1908e857916SSoby Mathew
1918e857916SSoby Mathew	/* ---------------------------------------------------------------
1928e857916SSoby Mathew	 * Data cache operations by set/way for level 3 cache
1938e857916SSoby Mathew	 *
1948e857916SSoby Mathew	 * The main function, do_dcsw_op requires:
1958e857916SSoby Mathew	 * x0: The operation type (0-2), as defined in arch.h
1968e857916SSoby Mathew	 * ---------------------------------------------------------------
1978e857916SSoby Mathew	 */
1988e857916SSoby Mathewfunc dcsw_op_level3
1998e857916SSoby Mathew	dcsw_op_level #(3 << LEVEL_SHIFT)
2008b779620SKévin Petitendfunc dcsw_op_level3
201