xref: /openbmc/u-boot/arch/arm/cpu/armv7/cache_v7.c (revision e8f80a5a)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
22c451f78SAneesh V /*
32c451f78SAneesh V  * (C) Copyright 2010
42c451f78SAneesh V  * Texas Instruments, <www.ti.com>
52c451f78SAneesh V  * Aneesh V <aneesh@ti.com>
62c451f78SAneesh V  */
72c451f78SAneesh V #include <linux/types.h>
82c451f78SAneesh V #include <common.h>
92c451f78SAneesh V #include <asm/armv7.h>
102c451f78SAneesh V #include <asm/utils.h>
112c451f78SAneesh V 
12df120142SHans de Goede #define ARMV7_DCACHE_INVAL_RANGE	1
13df120142SHans de Goede #define ARMV7_DCACHE_CLEAN_INVAL_RANGE	2
142c451f78SAneesh V 
152c451f78SAneesh V #ifndef CONFIG_SYS_DCACHE_OFF
16c09d2905SHans de Goede 
17c09d2905SHans de Goede /* Asm functions from cache_v7_asm.S */
18c09d2905SHans de Goede void v7_flush_dcache_all(void);
19df120142SHans de Goede void v7_invalidate_dcache_all(void);
20c09d2905SHans de Goede 
get_ccsidr(void)212c451f78SAneesh V static u32 get_ccsidr(void)
222c451f78SAneesh V {
232c451f78SAneesh V 	u32 ccsidr;
242c451f78SAneesh V 
252c451f78SAneesh V 	/* Read current CP15 Cache Size ID Register */
262c451f78SAneesh V 	asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
272c451f78SAneesh V 	return ccsidr;
282c451f78SAneesh V }
292c451f78SAneesh V 
v7_dcache_clean_inval_range(u32 start,u32 stop,u32 line_len)30b9297c22SThierry Reding static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len)
312c451f78SAneesh V {
322c451f78SAneesh V 	u32 mva;
332c451f78SAneesh V 
342c451f78SAneesh V 	/* Align start to cache line boundary */
352c451f78SAneesh V 	start &= ~(line_len - 1);
362c451f78SAneesh V 	for (mva = start; mva < stop; mva = mva + line_len) {
372c451f78SAneesh V 		/* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */
382c451f78SAneesh V 		asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
392c451f78SAneesh V 	}
402c451f78SAneesh V }
412c451f78SAneesh V 
v7_dcache_inval_range(u32 start,u32 stop,u32 line_len)422c451f78SAneesh V static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len)
432c451f78SAneesh V {
442c451f78SAneesh V 	u32 mva;
452c451f78SAneesh V 
466b424611SSimon Glass 	if (!check_cache_range(start, stop))
476b424611SSimon Glass 		return;
482c451f78SAneesh V 
492c451f78SAneesh V 	for (mva = start; mva < stop; mva = mva + line_len) {
502c451f78SAneesh V 		/* DCIMVAC - Invalidate data cache by MVA to PoC */
512c451f78SAneesh V 		asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva));
522c451f78SAneesh V 	}
532c451f78SAneesh V }
542c451f78SAneesh V 
v7_dcache_maint_range(u32 start,u32 stop,u32 range_op)552c451f78SAneesh V static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op)
562c451f78SAneesh V {
572c451f78SAneesh V 	u32 line_len, ccsidr;
582c451f78SAneesh V 
592c451f78SAneesh V 	ccsidr = get_ccsidr();
602c451f78SAneesh V 	line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
612c451f78SAneesh V 			CCSIDR_LINE_SIZE_OFFSET) + 2;
622c451f78SAneesh V 	/* Converting from words to bytes */
632c451f78SAneesh V 	line_len += 2;
642c451f78SAneesh V 	/* converting from log2(linelen) to linelen */
652c451f78SAneesh V 	line_len = 1 << line_len;
662c451f78SAneesh V 
672c451f78SAneesh V 	switch (range_op) {
682c451f78SAneesh V 	case ARMV7_DCACHE_CLEAN_INVAL_RANGE:
692c451f78SAneesh V 		v7_dcache_clean_inval_range(start, stop, line_len);
702c451f78SAneesh V 		break;
712c451f78SAneesh V 	case ARMV7_DCACHE_INVAL_RANGE:
722c451f78SAneesh V 		v7_dcache_inval_range(start, stop, line_len);
732c451f78SAneesh V 		break;
742c451f78SAneesh V 	}
752c451f78SAneesh V 
76882f80b9SAneesh V 	/* DSB to make sure the operation is complete */
77a78cd861STom Rini 	dsb();
782c451f78SAneesh V }
792c451f78SAneesh V 
802c451f78SAneesh V /* Invalidate TLB */
v7_inval_tlb(void)812c451f78SAneesh V static void v7_inval_tlb(void)
822c451f78SAneesh V {
832c451f78SAneesh V 	/* Invalidate entire unified TLB */
842c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0));
852c451f78SAneesh V 	/* Invalidate entire data TLB */
862c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0));
872c451f78SAneesh V 	/* Invalidate entire instruction TLB */
882c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0));
892c451f78SAneesh V 	/* Full system DSB - make sure that the invalidation is complete */
90a78cd861STom Rini 	dsb();
912c451f78SAneesh V 	/* Full system ISB - make sure the instruction stream sees it */
92a78cd861STom Rini 	isb();
932c451f78SAneesh V }
942c451f78SAneesh V 
invalidate_dcache_all(void)952c451f78SAneesh V void invalidate_dcache_all(void)
962c451f78SAneesh V {
97df120142SHans de Goede 	v7_invalidate_dcache_all();
982c451f78SAneesh V 
992c451f78SAneesh V 	v7_outer_cache_inval_all();
1002c451f78SAneesh V }
1012c451f78SAneesh V 
1022c451f78SAneesh V /*
1032c451f78SAneesh V  * Performs a clean & invalidation of the entire data cache
1042c451f78SAneesh V  * at all levels
1052c451f78SAneesh V  */
flush_dcache_all(void)1062c451f78SAneesh V void flush_dcache_all(void)
1072c451f78SAneesh V {
108c09d2905SHans de Goede 	v7_flush_dcache_all();
1092c451f78SAneesh V 
1102c451f78SAneesh V 	v7_outer_cache_flush_all();
1112c451f78SAneesh V }
1122c451f78SAneesh V 
1132c451f78SAneesh V /*
1142c451f78SAneesh V  * Invalidates range in all levels of D-cache/unified cache used:
1152c451f78SAneesh V  * Affects the range [start, stop - 1]
1162c451f78SAneesh V  */
invalidate_dcache_range(unsigned long start,unsigned long stop)1172c451f78SAneesh V void invalidate_dcache_range(unsigned long start, unsigned long stop)
1182c451f78SAneesh V {
11911aa6a32SMarek Vasut 	check_cache_range(start, stop);
12011aa6a32SMarek Vasut 
1212c451f78SAneesh V 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE);
1222c451f78SAneesh V 
1232c451f78SAneesh V 	v7_outer_cache_inval_range(start, stop);
1242c451f78SAneesh V }
1252c451f78SAneesh V 
1262c451f78SAneesh V /*
1272c451f78SAneesh V  * Flush range(clean & invalidate) from all levels of D-cache/unified
1282c451f78SAneesh V  * cache used:
1292c451f78SAneesh V  * Affects the range [start, stop - 1]
1302c451f78SAneesh V  */
flush_dcache_range(unsigned long start,unsigned long stop)1312c451f78SAneesh V void flush_dcache_range(unsigned long start, unsigned long stop)
1322c451f78SAneesh V {
13311aa6a32SMarek Vasut 	check_cache_range(start, stop);
13411aa6a32SMarek Vasut 
1352c451f78SAneesh V 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE);
1362c451f78SAneesh V 
1372c451f78SAneesh V 	v7_outer_cache_flush_range(start, stop);
1382c451f78SAneesh V }
1392c451f78SAneesh V 
arm_init_before_mmu(void)1402c451f78SAneesh V void arm_init_before_mmu(void)
1412c451f78SAneesh V {
1422c451f78SAneesh V 	v7_outer_cache_enable();
1432c451f78SAneesh V 	invalidate_dcache_all();
1442c451f78SAneesh V 	v7_inval_tlb();
1452c451f78SAneesh V }
1462c451f78SAneesh V 
mmu_page_table_flush(unsigned long start,unsigned long stop)1470dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop)
1480dde7f53SSimon Glass {
1490dde7f53SSimon Glass 	flush_dcache_range(start, stop);
1500dde7f53SSimon Glass 	v7_inval_tlb();
1510dde7f53SSimon Glass }
1522c451f78SAneesh V #else /* #ifndef CONFIG_SYS_DCACHE_OFF */
invalidate_dcache_all(void)1532c451f78SAneesh V void invalidate_dcache_all(void)
1542c451f78SAneesh V {
1552c451f78SAneesh V }
1562c451f78SAneesh V 
flush_dcache_all(void)1572c451f78SAneesh V void flush_dcache_all(void)
1582c451f78SAneesh V {
1592c451f78SAneesh V }
1602c451f78SAneesh V 
invalidate_dcache_range(unsigned long start,unsigned long stop)161ec6f6100SDaniel Allred void invalidate_dcache_range(unsigned long start, unsigned long stop)
162ec6f6100SDaniel Allred {
163ec6f6100SDaniel Allred }
164ec6f6100SDaniel Allred 
flush_dcache_range(unsigned long start,unsigned long stop)165ec6f6100SDaniel Allred void flush_dcache_range(unsigned long start, unsigned long stop)
166ec6f6100SDaniel Allred {
167ec6f6100SDaniel Allred }
168ec6f6100SDaniel Allred 
arm_init_before_mmu(void)1692c451f78SAneesh V void arm_init_before_mmu(void)
1702c451f78SAneesh V {
1712c451f78SAneesh V }
1722c451f78SAneesh V 
mmu_page_table_flush(unsigned long start,unsigned long stop)1730dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop)
1740dde7f53SSimon Glass {
1750dde7f53SSimon Glass }
1760dde7f53SSimon Glass 
arm_init_domains(void)177de63ac27SR Sricharan void arm_init_domains(void)
178de63ac27SR Sricharan {
179de63ac27SR Sricharan }
1802c451f78SAneesh V #endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
1812c451f78SAneesh V 
1822c451f78SAneesh V #ifndef CONFIG_SYS_ICACHE_OFF
1832c451f78SAneesh V /* Invalidate entire I-cache and branch predictor array */
invalidate_icache_all(void)1842c451f78SAneesh V void invalidate_icache_all(void)
1852c451f78SAneesh V {
1862c451f78SAneesh V 	/*
1872c451f78SAneesh V 	 * Invalidate all instruction caches to PoU.
1882c451f78SAneesh V 	 * Also flushes branch target cache.
1892c451f78SAneesh V 	 */
1902c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
1912c451f78SAneesh V 
1922c451f78SAneesh V 	/* Invalidate entire branch predictor array */
1932c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
1942c451f78SAneesh V 
1952c451f78SAneesh V 	/* Full system DSB - make sure that the invalidation is complete */
196a78cd861STom Rini 	dsb();
1972c451f78SAneesh V 
1982c451f78SAneesh V 	/* ISB - make sure the instruction stream sees it */
199a78cd861STom Rini 	isb();
2002c451f78SAneesh V }
2012c451f78SAneesh V #else
invalidate_icache_all(void)2022c451f78SAneesh V void invalidate_icache_all(void)
2032c451f78SAneesh V {
2042c451f78SAneesh V }
2052c451f78SAneesh V #endif
2062c451f78SAneesh V 
207fcfddfd5SJeroen Hofstee /*  Stub implementations for outer cache operations */
v7_outer_cache_enable(void)208fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_enable(void) {}
v7_outer_cache_disable(void)209fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_disable(void) {}
v7_outer_cache_flush_all(void)210fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_all(void) {}
v7_outer_cache_inval_all(void)211fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_all(void) {}
v7_outer_cache_flush_range(u32 start,u32 end)212fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_range(u32 start, u32 end) {}
v7_outer_cache_inval_range(u32 start,u32 end)213fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_range(u32 start, u32 end) {}
214