xref: /openbmc/u-boot/arch/arm/cpu/armv7/cache_v7.c (revision c09d29057ab0b04db0857d319c6bff74de31b9c3)
12c451f78SAneesh V /*
22c451f78SAneesh V  * (C) Copyright 2010
32c451f78SAneesh V  * Texas Instruments, <www.ti.com>
42c451f78SAneesh V  * Aneesh V <aneesh@ti.com>
52c451f78SAneesh V  *
61a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
72c451f78SAneesh V  */
82c451f78SAneesh V #include <linux/types.h>
92c451f78SAneesh V #include <common.h>
102c451f78SAneesh V #include <asm/armv7.h>
112c451f78SAneesh V #include <asm/utils.h>
122c451f78SAneesh V 
132c451f78SAneesh V #define ARMV7_DCACHE_INVAL_ALL		1
142c451f78SAneesh V #define ARMV7_DCACHE_CLEAN_INVAL_ALL	2
152c451f78SAneesh V #define ARMV7_DCACHE_INVAL_RANGE	3
162c451f78SAneesh V #define ARMV7_DCACHE_CLEAN_INVAL_RANGE	4
172c451f78SAneesh V 
182c451f78SAneesh V #ifndef CONFIG_SYS_DCACHE_OFF
19*c09d2905SHans de Goede 
20*c09d2905SHans de Goede /* Asm functions from cache_v7_asm.S */
21*c09d2905SHans de Goede void v7_flush_dcache_all(void);
22*c09d2905SHans de Goede 
2311aa6a32SMarek Vasut static int check_cache_range(unsigned long start, unsigned long stop)
2411aa6a32SMarek Vasut {
2511aa6a32SMarek Vasut 	int ok = 1;
2611aa6a32SMarek Vasut 
2711aa6a32SMarek Vasut 	if (start & (CONFIG_SYS_CACHELINE_SIZE - 1))
2811aa6a32SMarek Vasut 		ok = 0;
2911aa6a32SMarek Vasut 
3011aa6a32SMarek Vasut 	if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1))
3111aa6a32SMarek Vasut 		ok = 0;
3211aa6a32SMarek Vasut 
3311aa6a32SMarek Vasut 	if (!ok)
3411aa6a32SMarek Vasut 		debug("CACHE: Misaligned operation at range [%08lx, %08lx]\n",
3511aa6a32SMarek Vasut 			start, stop);
3611aa6a32SMarek Vasut 
3711aa6a32SMarek Vasut 	return ok;
3811aa6a32SMarek Vasut }
3911aa6a32SMarek Vasut 
402c451f78SAneesh V /*
412c451f78SAneesh V  * Write the level and type you want to Cache Size Selection Register(CSSELR)
422c451f78SAneesh V  * to get size details from Current Cache Size ID Register(CCSIDR)
432c451f78SAneesh V  */
442c451f78SAneesh V static void set_csselr(u32 level, u32 type)
45b9297c22SThierry Reding {
46b9297c22SThierry Reding 	u32 csselr = level << 1 | type;
472c451f78SAneesh V 
482c451f78SAneesh V 	/* Write to Cache Size Selection Register(CSSELR) */
492c451f78SAneesh V 	asm volatile ("mcr p15, 2, %0, c0, c0, 0" : : "r" (csselr));
502c451f78SAneesh V }
512c451f78SAneesh V 
522c451f78SAneesh V static u32 get_ccsidr(void)
532c451f78SAneesh V {
542c451f78SAneesh V 	u32 ccsidr;
552c451f78SAneesh V 
562c451f78SAneesh V 	/* Read current CP15 Cache Size ID Register */
572c451f78SAneesh V 	asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
582c451f78SAneesh V 	return ccsidr;
592c451f78SAneesh V }
602c451f78SAneesh V 
612c451f78SAneesh V static u32 get_clidr(void)
622c451f78SAneesh V {
632c451f78SAneesh V 	u32 clidr;
642c451f78SAneesh V 
652c451f78SAneesh V 	/* Read current CP15 Cache Level ID Register */
662c451f78SAneesh V 	asm volatile ("mrc p15,1,%0,c0,c0,1" : "=r" (clidr));
672c451f78SAneesh V 	return clidr;
682c451f78SAneesh V }
692c451f78SAneesh V 
702c451f78SAneesh V static void v7_inval_dcache_level_setway(u32 level, u32 num_sets,
712c451f78SAneesh V 					 u32 num_ways, u32 way_shift,
722c451f78SAneesh V 					 u32 log2_line_len)
732c451f78SAneesh V {
74b9297c22SThierry Reding 	int way, set;
75b9297c22SThierry Reding 	u32 setway;
762c451f78SAneesh V 
772c451f78SAneesh V 	/*
782c451f78SAneesh V 	 * For optimal assembly code:
792c451f78SAneesh V 	 *	a. count down
802c451f78SAneesh V 	 *	b. have bigger loop inside
812c451f78SAneesh V 	 */
822c451f78SAneesh V 	for (way = num_ways - 1; way >= 0 ; way--) {
832c451f78SAneesh V 		for (set = num_sets - 1; set >= 0; set--) {
842c451f78SAneesh V 			setway = (level << 1) | (set << log2_line_len) |
852c451f78SAneesh V 				 (way << way_shift);
862c451f78SAneesh V 			/* Invalidate data/unified cache line by set/way */
872c451f78SAneesh V 			asm volatile ("	mcr p15, 0, %0, c7, c6, 2"
882c451f78SAneesh V 					: : "r" (setway));
892c451f78SAneesh V 		}
902c451f78SAneesh V 	}
91882f80b9SAneesh V 	/* DSB to make sure the operation is complete */
929ba379adSValentine Barshak 	DSB;
932c451f78SAneesh V }
942c451f78SAneesh V 
952c451f78SAneesh V static void v7_maint_dcache_level_setway(u32 level, u32 operation)
962c451f78SAneesh V {
972c451f78SAneesh V 	u32 ccsidr;
982c451f78SAneesh V 	u32 num_sets, num_ways, log2_line_len, log2_num_ways;
992c451f78SAneesh V 	u32 way_shift;
1002c451f78SAneesh V 
1012c451f78SAneesh V 	set_csselr(level, ARMV7_CSSELR_IND_DATA_UNIFIED);
1022c451f78SAneesh V 
1032c451f78SAneesh V 	ccsidr = get_ccsidr();
1042c451f78SAneesh V 
1052c451f78SAneesh V 	log2_line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
1062c451f78SAneesh V 				CCSIDR_LINE_SIZE_OFFSET) + 2;
1072c451f78SAneesh V 	/* Converting from words to bytes */
1082c451f78SAneesh V 	log2_line_len += 2;
1092c451f78SAneesh V 
1102c451f78SAneesh V 	num_ways  = ((ccsidr & CCSIDR_ASSOCIATIVITY_MASK) >>
1112c451f78SAneesh V 			CCSIDR_ASSOCIATIVITY_OFFSET) + 1;
1122c451f78SAneesh V 	num_sets  = ((ccsidr & CCSIDR_NUM_SETS_MASK) >>
1132c451f78SAneesh V 			CCSIDR_NUM_SETS_OFFSET) + 1;
1142c451f78SAneesh V 	/*
1152c451f78SAneesh V 	 * According to ARMv7 ARM number of sets and number of ways need
1162c451f78SAneesh V 	 * not be a power of 2
1172c451f78SAneesh V 	 */
1182c451f78SAneesh V 	log2_num_ways = log_2_n_round_up(num_ways);
1192c451f78SAneesh V 
1202c451f78SAneesh V 	way_shift = (32 - log2_num_ways);
1212c451f78SAneesh V 	v7_inval_dcache_level_setway(level, num_sets, num_ways,
1222c451f78SAneesh V 				      way_shift, log2_line_len);
1232c451f78SAneesh V }
1242c451f78SAneesh V 
1252c451f78SAneesh V static void v7_maint_dcache_all(u32 operation)
1262c451f78SAneesh V {
1272c451f78SAneesh V 	u32 level, cache_type, level_start_bit = 0;
1282c451f78SAneesh V 	u32 clidr = get_clidr();
1292c451f78SAneesh V 
1302c451f78SAneesh V 	for (level = 0; level < 7; level++) {
1312c451f78SAneesh V 		cache_type = (clidr >> level_start_bit) & 0x7;
1322c451f78SAneesh V 		if ((cache_type == ARMV7_CLIDR_CTYPE_DATA_ONLY) ||
1332c451f78SAneesh V 		    (cache_type == ARMV7_CLIDR_CTYPE_INSTRUCTION_DATA) ||
1342c451f78SAneesh V 		    (cache_type == ARMV7_CLIDR_CTYPE_UNIFIED))
1352c451f78SAneesh V 			v7_maint_dcache_level_setway(level, operation);
1362c451f78SAneesh V 		level_start_bit += 3;
1372c451f78SAneesh V 	}
1382c451f78SAneesh V }
1392c451f78SAneesh V 
140b9297c22SThierry Reding static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len)
1412c451f78SAneesh V {
1422c451f78SAneesh V 	u32 mva;
1432c451f78SAneesh V 
1442c451f78SAneesh V 	/* Align start to cache line boundary */
1452c451f78SAneesh V 	start &= ~(line_len - 1);
1462c451f78SAneesh V 	for (mva = start; mva < stop; mva = mva + line_len) {
1472c451f78SAneesh V 		/* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */
1482c451f78SAneesh V 		asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
1492c451f78SAneesh V 	}
1502c451f78SAneesh V }
1512c451f78SAneesh V 
1522c451f78SAneesh V static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len)
1532c451f78SAneesh V {
1542c451f78SAneesh V 	u32 mva;
1552c451f78SAneesh V 
1562c451f78SAneesh V 	/*
157cabe2878SAneesh V 	 * If start address is not aligned to cache-line do not
158cabe2878SAneesh V 	 * invalidate the first cache-line
1592c451f78SAneesh V 	 */
1602c451f78SAneesh V 	if (start & (line_len - 1)) {
161cabe2878SAneesh V 		printf("ERROR: %s - start address is not aligned - 0x%08x\n",
162cabe2878SAneesh V 			__func__, start);
1632c451f78SAneesh V 		/* move to next cache line */
1642c451f78SAneesh V 		start = (start + line_len - 1) & ~(line_len - 1);
1652c451f78SAneesh V 	}
1662c451f78SAneesh V 
1672c451f78SAneesh V 	/*
168cabe2878SAneesh V 	 * If stop address is not aligned to cache-line do not
169cabe2878SAneesh V 	 * invalidate the last cache-line
1702c451f78SAneesh V 	 */
1712c451f78SAneesh V 	if (stop & (line_len - 1)) {
172cabe2878SAneesh V 		printf("ERROR: %s - stop address is not aligned - 0x%08x\n",
173cabe2878SAneesh V 			__func__, stop);
1742c451f78SAneesh V 		/* align to the beginning of this cache line */
1752c451f78SAneesh V 		stop &= ~(line_len - 1);
1762c451f78SAneesh V 	}
1772c451f78SAneesh V 
1782c451f78SAneesh V 	for (mva = start; mva < stop; mva = mva + line_len) {
1792c451f78SAneesh V 		/* DCIMVAC - Invalidate data cache by MVA to PoC */
1802c451f78SAneesh V 		asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva));
1812c451f78SAneesh V 	}
1822c451f78SAneesh V }
1832c451f78SAneesh V 
1842c451f78SAneesh V static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op)
1852c451f78SAneesh V {
1862c451f78SAneesh V 	u32 line_len, ccsidr;
1872c451f78SAneesh V 
1882c451f78SAneesh V 	ccsidr = get_ccsidr();
1892c451f78SAneesh V 	line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
1902c451f78SAneesh V 			CCSIDR_LINE_SIZE_OFFSET) + 2;
1912c451f78SAneesh V 	/* Converting from words to bytes */
1922c451f78SAneesh V 	line_len += 2;
1932c451f78SAneesh V 	/* converting from log2(linelen) to linelen */
1942c451f78SAneesh V 	line_len = 1 << line_len;
1952c451f78SAneesh V 
1962c451f78SAneesh V 	switch (range_op) {
1972c451f78SAneesh V 	case ARMV7_DCACHE_CLEAN_INVAL_RANGE:
1982c451f78SAneesh V 		v7_dcache_clean_inval_range(start, stop, line_len);
1992c451f78SAneesh V 		break;
2002c451f78SAneesh V 	case ARMV7_DCACHE_INVAL_RANGE:
2012c451f78SAneesh V 		v7_dcache_inval_range(start, stop, line_len);
2022c451f78SAneesh V 		break;
2032c451f78SAneesh V 	}
2042c451f78SAneesh V 
205882f80b9SAneesh V 	/* DSB to make sure the operation is complete */
2069ba379adSValentine Barshak 	DSB;
2072c451f78SAneesh V }
2082c451f78SAneesh V 
2092c451f78SAneesh V /* Invalidate TLB */
2102c451f78SAneesh V static void v7_inval_tlb(void)
2112c451f78SAneesh V {
2122c451f78SAneesh V 	/* Invalidate entire unified TLB */
2132c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0));
2142c451f78SAneesh V 	/* Invalidate entire data TLB */
2152c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0));
2162c451f78SAneesh V 	/* Invalidate entire instruction TLB */
2172c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0));
2182c451f78SAneesh V 	/* Full system DSB - make sure that the invalidation is complete */
2199ba379adSValentine Barshak 	DSB;
2202c451f78SAneesh V 	/* Full system ISB - make sure the instruction stream sees it */
2219ba379adSValentine Barshak 	ISB;
2222c451f78SAneesh V }
2232c451f78SAneesh V 
2242c451f78SAneesh V void invalidate_dcache_all(void)
2252c451f78SAneesh V {
2262c451f78SAneesh V 	v7_maint_dcache_all(ARMV7_DCACHE_INVAL_ALL);
2272c451f78SAneesh V 
2282c451f78SAneesh V 	v7_outer_cache_inval_all();
2292c451f78SAneesh V }
2302c451f78SAneesh V 
2312c451f78SAneesh V /*
2322c451f78SAneesh V  * Performs a clean & invalidation of the entire data cache
2332c451f78SAneesh V  * at all levels
2342c451f78SAneesh V  */
2352c451f78SAneesh V void flush_dcache_all(void)
2362c451f78SAneesh V {
237*c09d2905SHans de Goede 	v7_flush_dcache_all();
2382c451f78SAneesh V 
2392c451f78SAneesh V 	v7_outer_cache_flush_all();
2402c451f78SAneesh V }
2412c451f78SAneesh V 
2422c451f78SAneesh V /*
2432c451f78SAneesh V  * Invalidates range in all levels of D-cache/unified cache used:
2442c451f78SAneesh V  * Affects the range [start, stop - 1]
2452c451f78SAneesh V  */
2462c451f78SAneesh V void invalidate_dcache_range(unsigned long start, unsigned long stop)
2472c451f78SAneesh V {
24811aa6a32SMarek Vasut 	check_cache_range(start, stop);
24911aa6a32SMarek Vasut 
2502c451f78SAneesh V 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE);
2512c451f78SAneesh V 
2522c451f78SAneesh V 	v7_outer_cache_inval_range(start, stop);
2532c451f78SAneesh V }
2542c451f78SAneesh V 
2552c451f78SAneesh V /*
2562c451f78SAneesh V  * Flush range(clean & invalidate) from all levels of D-cache/unified
2572c451f78SAneesh V  * cache used:
2582c451f78SAneesh V  * Affects the range [start, stop - 1]
2592c451f78SAneesh V  */
2602c451f78SAneesh V void flush_dcache_range(unsigned long start, unsigned long stop)
2612c451f78SAneesh V {
26211aa6a32SMarek Vasut 	check_cache_range(start, stop);
26311aa6a32SMarek Vasut 
2642c451f78SAneesh V 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE);
2652c451f78SAneesh V 
2662c451f78SAneesh V 	v7_outer_cache_flush_range(start, stop);
2672c451f78SAneesh V }
2682c451f78SAneesh V 
2692c451f78SAneesh V void arm_init_before_mmu(void)
2702c451f78SAneesh V {
2712c451f78SAneesh V 	v7_outer_cache_enable();
2722c451f78SAneesh V 	invalidate_dcache_all();
2732c451f78SAneesh V 	v7_inval_tlb();
2742c451f78SAneesh V }
2752c451f78SAneesh V 
2760dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop)
2770dde7f53SSimon Glass {
2780dde7f53SSimon Glass 	flush_dcache_range(start, stop);
2790dde7f53SSimon Glass 	v7_inval_tlb();
2800dde7f53SSimon Glass }
2812c451f78SAneesh V #else /* #ifndef CONFIG_SYS_DCACHE_OFF */
2822c451f78SAneesh V void invalidate_dcache_all(void)
2832c451f78SAneesh V {
2842c451f78SAneesh V }
2852c451f78SAneesh V 
2862c451f78SAneesh V void flush_dcache_all(void)
2872c451f78SAneesh V {
2882c451f78SAneesh V }
2892c451f78SAneesh V 
2902c451f78SAneesh V void arm_init_before_mmu(void)
2912c451f78SAneesh V {
2922c451f78SAneesh V }
2932c451f78SAneesh V 
2940dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop)
2950dde7f53SSimon Glass {
2960dde7f53SSimon Glass }
2970dde7f53SSimon Glass 
298de63ac27SR Sricharan void arm_init_domains(void)
299de63ac27SR Sricharan {
300de63ac27SR Sricharan }
3012c451f78SAneesh V #endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
3022c451f78SAneesh V 
3032c451f78SAneesh V #ifndef CONFIG_SYS_ICACHE_OFF
3042c451f78SAneesh V /* Invalidate entire I-cache and branch predictor array */
3052c451f78SAneesh V void invalidate_icache_all(void)
3062c451f78SAneesh V {
3072c451f78SAneesh V 	/*
3082c451f78SAneesh V 	 * Invalidate all instruction caches to PoU.
3092c451f78SAneesh V 	 * Also flushes branch target cache.
3102c451f78SAneesh V 	 */
3112c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
3122c451f78SAneesh V 
3132c451f78SAneesh V 	/* Invalidate entire branch predictor array */
3142c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
3152c451f78SAneesh V 
3162c451f78SAneesh V 	/* Full system DSB - make sure that the invalidation is complete */
3179ba379adSValentine Barshak 	DSB;
3182c451f78SAneesh V 
3192c451f78SAneesh V 	/* ISB - make sure the instruction stream sees it */
3209ba379adSValentine Barshak 	ISB;
3212c451f78SAneesh V }
3222c451f78SAneesh V #else
3232c451f78SAneesh V void invalidate_icache_all(void)
3242c451f78SAneesh V {
3252c451f78SAneesh V }
3262c451f78SAneesh V #endif
3272c451f78SAneesh V 
328fcfddfd5SJeroen Hofstee /*  Stub implementations for outer cache operations */
329fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_enable(void) {}
330fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_disable(void) {}
331fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_all(void) {}
332fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_all(void) {}
333fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_range(u32 start, u32 end) {}
334fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_range(u32 start, u32 end) {}
335