xref: /openbmc/u-boot/arch/arm/cpu/armv7/cache_v7.c (revision 9ba379ade789e41cc4132d622315f3f021a47b9b)
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
192c451f78SAneesh V /*
202c451f78SAneesh V  * Write the level and type you want to Cache Size Selection Register(CSSELR)
212c451f78SAneesh V  * to get size details from Current Cache Size ID Register(CCSIDR)
222c451f78SAneesh V  */
232c451f78SAneesh V static void set_csselr(u32 level, u32 type)
24b9297c22SThierry Reding {
25b9297c22SThierry Reding 	u32 csselr = level << 1 | type;
262c451f78SAneesh V 
272c451f78SAneesh V 	/* Write to Cache Size Selection Register(CSSELR) */
282c451f78SAneesh V 	asm volatile ("mcr p15, 2, %0, c0, c0, 0" : : "r" (csselr));
292c451f78SAneesh V }
302c451f78SAneesh V 
312c451f78SAneesh V static u32 get_ccsidr(void)
322c451f78SAneesh V {
332c451f78SAneesh V 	u32 ccsidr;
342c451f78SAneesh V 
352c451f78SAneesh V 	/* Read current CP15 Cache Size ID Register */
362c451f78SAneesh V 	asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
372c451f78SAneesh V 	return ccsidr;
382c451f78SAneesh V }
392c451f78SAneesh V 
402c451f78SAneesh V static u32 get_clidr(void)
412c451f78SAneesh V {
422c451f78SAneesh V 	u32 clidr;
432c451f78SAneesh V 
442c451f78SAneesh V 	/* Read current CP15 Cache Level ID Register */
452c451f78SAneesh V 	asm volatile ("mrc p15,1,%0,c0,c0,1" : "=r" (clidr));
462c451f78SAneesh V 	return clidr;
472c451f78SAneesh V }
482c451f78SAneesh V 
492c451f78SAneesh V static void v7_inval_dcache_level_setway(u32 level, u32 num_sets,
502c451f78SAneesh V 					 u32 num_ways, u32 way_shift,
512c451f78SAneesh V 					 u32 log2_line_len)
522c451f78SAneesh V {
53b9297c22SThierry Reding 	int way, set;
54b9297c22SThierry Reding 	u32 setway;
552c451f78SAneesh V 
562c451f78SAneesh V 	/*
572c451f78SAneesh V 	 * For optimal assembly code:
582c451f78SAneesh V 	 *	a. count down
592c451f78SAneesh V 	 *	b. have bigger loop inside
602c451f78SAneesh V 	 */
612c451f78SAneesh V 	for (way = num_ways - 1; way >= 0 ; way--) {
622c451f78SAneesh V 		for (set = num_sets - 1; set >= 0; set--) {
632c451f78SAneesh V 			setway = (level << 1) | (set << log2_line_len) |
642c451f78SAneesh V 				 (way << way_shift);
652c451f78SAneesh V 			/* Invalidate data/unified cache line by set/way */
662c451f78SAneesh V 			asm volatile ("	mcr p15, 0, %0, c7, c6, 2"
672c451f78SAneesh V 					: : "r" (setway));
682c451f78SAneesh V 		}
692c451f78SAneesh V 	}
70882f80b9SAneesh V 	/* DSB to make sure the operation is complete */
71*9ba379adSValentine Barshak 	DSB;
722c451f78SAneesh V }
732c451f78SAneesh V 
742c451f78SAneesh V static void v7_clean_inval_dcache_level_setway(u32 level, u32 num_sets,
752c451f78SAneesh V 					       u32 num_ways, u32 way_shift,
762c451f78SAneesh V 					       u32 log2_line_len)
772c451f78SAneesh V {
78b9297c22SThierry Reding 	int way, set;
79b9297c22SThierry Reding 	u32 setway;
802c451f78SAneesh V 
812c451f78SAneesh V 	/*
822c451f78SAneesh V 	 * For optimal assembly code:
832c451f78SAneesh V 	 *	a. count down
842c451f78SAneesh V 	 *	b. have bigger loop inside
852c451f78SAneesh V 	 */
862c451f78SAneesh V 	for (way = num_ways - 1; way >= 0 ; way--) {
872c451f78SAneesh V 		for (set = num_sets - 1; set >= 0; set--) {
882c451f78SAneesh V 			setway = (level << 1) | (set << log2_line_len) |
892c451f78SAneesh V 				 (way << way_shift);
902c451f78SAneesh V 			/*
912c451f78SAneesh V 			 * Clean & Invalidate data/unified
922c451f78SAneesh V 			 * cache line by set/way
932c451f78SAneesh V 			 */
942c451f78SAneesh V 			asm volatile ("	mcr p15, 0, %0, c7, c14, 2"
952c451f78SAneesh V 					: : "r" (setway));
962c451f78SAneesh V 		}
972c451f78SAneesh V 	}
98882f80b9SAneesh V 	/* DSB to make sure the operation is complete */
99*9ba379adSValentine Barshak 	DSB;
1002c451f78SAneesh V }
1012c451f78SAneesh V 
1022c451f78SAneesh V static void v7_maint_dcache_level_setway(u32 level, u32 operation)
1032c451f78SAneesh V {
1042c451f78SAneesh V 	u32 ccsidr;
1052c451f78SAneesh V 	u32 num_sets, num_ways, log2_line_len, log2_num_ways;
1062c451f78SAneesh V 	u32 way_shift;
1072c451f78SAneesh V 
1082c451f78SAneesh V 	set_csselr(level, ARMV7_CSSELR_IND_DATA_UNIFIED);
1092c451f78SAneesh V 
1102c451f78SAneesh V 	ccsidr = get_ccsidr();
1112c451f78SAneesh V 
1122c451f78SAneesh V 	log2_line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
1132c451f78SAneesh V 				CCSIDR_LINE_SIZE_OFFSET) + 2;
1142c451f78SAneesh V 	/* Converting from words to bytes */
1152c451f78SAneesh V 	log2_line_len += 2;
1162c451f78SAneesh V 
1172c451f78SAneesh V 	num_ways  = ((ccsidr & CCSIDR_ASSOCIATIVITY_MASK) >>
1182c451f78SAneesh V 			CCSIDR_ASSOCIATIVITY_OFFSET) + 1;
1192c451f78SAneesh V 	num_sets  = ((ccsidr & CCSIDR_NUM_SETS_MASK) >>
1202c451f78SAneesh V 			CCSIDR_NUM_SETS_OFFSET) + 1;
1212c451f78SAneesh V 	/*
1222c451f78SAneesh V 	 * According to ARMv7 ARM number of sets and number of ways need
1232c451f78SAneesh V 	 * not be a power of 2
1242c451f78SAneesh V 	 */
1252c451f78SAneesh V 	log2_num_ways = log_2_n_round_up(num_ways);
1262c451f78SAneesh V 
1272c451f78SAneesh V 	way_shift = (32 - log2_num_ways);
1282c451f78SAneesh V 	if (operation == ARMV7_DCACHE_INVAL_ALL) {
1292c451f78SAneesh V 		v7_inval_dcache_level_setway(level, num_sets, num_ways,
1302c451f78SAneesh V 				      way_shift, log2_line_len);
1312c451f78SAneesh V 	} else if (operation == ARMV7_DCACHE_CLEAN_INVAL_ALL) {
1322c451f78SAneesh V 		v7_clean_inval_dcache_level_setway(level, num_sets, num_ways,
1332c451f78SAneesh V 						   way_shift, log2_line_len);
1342c451f78SAneesh V 	}
1352c451f78SAneesh V }
1362c451f78SAneesh V 
1372c451f78SAneesh V static void v7_maint_dcache_all(u32 operation)
1382c451f78SAneesh V {
1392c451f78SAneesh V 	u32 level, cache_type, level_start_bit = 0;
1402c451f78SAneesh V 	u32 clidr = get_clidr();
1412c451f78SAneesh V 
1422c451f78SAneesh V 	for (level = 0; level < 7; level++) {
1432c451f78SAneesh V 		cache_type = (clidr >> level_start_bit) & 0x7;
1442c451f78SAneesh V 		if ((cache_type == ARMV7_CLIDR_CTYPE_DATA_ONLY) ||
1452c451f78SAneesh V 		    (cache_type == ARMV7_CLIDR_CTYPE_INSTRUCTION_DATA) ||
1462c451f78SAneesh V 		    (cache_type == ARMV7_CLIDR_CTYPE_UNIFIED))
1472c451f78SAneesh V 			v7_maint_dcache_level_setway(level, operation);
1482c451f78SAneesh V 		level_start_bit += 3;
1492c451f78SAneesh V 	}
1502c451f78SAneesh V }
1512c451f78SAneesh V 
152b9297c22SThierry Reding static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len)
1532c451f78SAneesh V {
1542c451f78SAneesh V 	u32 mva;
1552c451f78SAneesh V 
1562c451f78SAneesh V 	/* Align start to cache line boundary */
1572c451f78SAneesh V 	start &= ~(line_len - 1);
1582c451f78SAneesh V 	for (mva = start; mva < stop; mva = mva + line_len) {
1592c451f78SAneesh V 		/* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */
1602c451f78SAneesh V 		asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
1612c451f78SAneesh V 	}
1622c451f78SAneesh V }
1632c451f78SAneesh V 
1642c451f78SAneesh V static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len)
1652c451f78SAneesh V {
1662c451f78SAneesh V 	u32 mva;
1672c451f78SAneesh V 
1682c451f78SAneesh V 	/*
169cabe2878SAneesh V 	 * If start address is not aligned to cache-line do not
170cabe2878SAneesh V 	 * invalidate the first cache-line
1712c451f78SAneesh V 	 */
1722c451f78SAneesh V 	if (start & (line_len - 1)) {
173cabe2878SAneesh V 		printf("ERROR: %s - start address is not aligned - 0x%08x\n",
174cabe2878SAneesh V 			__func__, start);
1752c451f78SAneesh V 		/* move to next cache line */
1762c451f78SAneesh V 		start = (start + line_len - 1) & ~(line_len - 1);
1772c451f78SAneesh V 	}
1782c451f78SAneesh V 
1792c451f78SAneesh V 	/*
180cabe2878SAneesh V 	 * If stop address is not aligned to cache-line do not
181cabe2878SAneesh V 	 * invalidate the last cache-line
1822c451f78SAneesh V 	 */
1832c451f78SAneesh V 	if (stop & (line_len - 1)) {
184cabe2878SAneesh V 		printf("ERROR: %s - stop address is not aligned - 0x%08x\n",
185cabe2878SAneesh V 			__func__, stop);
1862c451f78SAneesh V 		/* align to the beginning of this cache line */
1872c451f78SAneesh V 		stop &= ~(line_len - 1);
1882c451f78SAneesh V 	}
1892c451f78SAneesh V 
1902c451f78SAneesh V 	for (mva = start; mva < stop; mva = mva + line_len) {
1912c451f78SAneesh V 		/* DCIMVAC - Invalidate data cache by MVA to PoC */
1922c451f78SAneesh V 		asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva));
1932c451f78SAneesh V 	}
1942c451f78SAneesh V }
1952c451f78SAneesh V 
1962c451f78SAneesh V static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op)
1972c451f78SAneesh V {
1982c451f78SAneesh V 	u32 line_len, ccsidr;
1992c451f78SAneesh V 
2002c451f78SAneesh V 	ccsidr = get_ccsidr();
2012c451f78SAneesh V 	line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
2022c451f78SAneesh V 			CCSIDR_LINE_SIZE_OFFSET) + 2;
2032c451f78SAneesh V 	/* Converting from words to bytes */
2042c451f78SAneesh V 	line_len += 2;
2052c451f78SAneesh V 	/* converting from log2(linelen) to linelen */
2062c451f78SAneesh V 	line_len = 1 << line_len;
2072c451f78SAneesh V 
2082c451f78SAneesh V 	switch (range_op) {
2092c451f78SAneesh V 	case ARMV7_DCACHE_CLEAN_INVAL_RANGE:
2102c451f78SAneesh V 		v7_dcache_clean_inval_range(start, stop, line_len);
2112c451f78SAneesh V 		break;
2122c451f78SAneesh V 	case ARMV7_DCACHE_INVAL_RANGE:
2132c451f78SAneesh V 		v7_dcache_inval_range(start, stop, line_len);
2142c451f78SAneesh V 		break;
2152c451f78SAneesh V 	}
2162c451f78SAneesh V 
217882f80b9SAneesh V 	/* DSB to make sure the operation is complete */
218*9ba379adSValentine Barshak 	DSB;
2192c451f78SAneesh V }
2202c451f78SAneesh V 
2212c451f78SAneesh V /* Invalidate TLB */
2222c451f78SAneesh V static void v7_inval_tlb(void)
2232c451f78SAneesh V {
2242c451f78SAneesh V 	/* Invalidate entire unified TLB */
2252c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0));
2262c451f78SAneesh V 	/* Invalidate entire data TLB */
2272c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0));
2282c451f78SAneesh V 	/* Invalidate entire instruction TLB */
2292c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0));
2302c451f78SAneesh V 	/* Full system DSB - make sure that the invalidation is complete */
231*9ba379adSValentine Barshak 	DSB;
2322c451f78SAneesh V 	/* Full system ISB - make sure the instruction stream sees it */
233*9ba379adSValentine Barshak 	ISB;
2342c451f78SAneesh V }
2352c451f78SAneesh V 
2362c451f78SAneesh V void invalidate_dcache_all(void)
2372c451f78SAneesh V {
2382c451f78SAneesh V 	v7_maint_dcache_all(ARMV7_DCACHE_INVAL_ALL);
2392c451f78SAneesh V 
2402c451f78SAneesh V 	v7_outer_cache_inval_all();
2412c451f78SAneesh V }
2422c451f78SAneesh V 
2432c451f78SAneesh V /*
2442c451f78SAneesh V  * Performs a clean & invalidation of the entire data cache
2452c451f78SAneesh V  * at all levels
2462c451f78SAneesh V  */
2472c451f78SAneesh V void flush_dcache_all(void)
2482c451f78SAneesh V {
2492c451f78SAneesh V 	v7_maint_dcache_all(ARMV7_DCACHE_CLEAN_INVAL_ALL);
2502c451f78SAneesh V 
2512c451f78SAneesh V 	v7_outer_cache_flush_all();
2522c451f78SAneesh V }
2532c451f78SAneesh V 
2542c451f78SAneesh V /*
2552c451f78SAneesh V  * Invalidates range in all levels of D-cache/unified cache used:
2562c451f78SAneesh V  * Affects the range [start, stop - 1]
2572c451f78SAneesh V  */
2582c451f78SAneesh V void invalidate_dcache_range(unsigned long start, unsigned long stop)
2592c451f78SAneesh V {
2602c451f78SAneesh V 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE);
2612c451f78SAneesh V 
2622c451f78SAneesh V 	v7_outer_cache_inval_range(start, stop);
2632c451f78SAneesh V }
2642c451f78SAneesh V 
2652c451f78SAneesh V /*
2662c451f78SAneesh V  * Flush range(clean & invalidate) from all levels of D-cache/unified
2672c451f78SAneesh V  * cache used:
2682c451f78SAneesh V  * Affects the range [start, stop - 1]
2692c451f78SAneesh V  */
2702c451f78SAneesh V void flush_dcache_range(unsigned long start, unsigned long stop)
2712c451f78SAneesh V {
2722c451f78SAneesh V 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE);
2732c451f78SAneesh V 
2742c451f78SAneesh V 	v7_outer_cache_flush_range(start, stop);
2752c451f78SAneesh V }
2762c451f78SAneesh V 
2772c451f78SAneesh V void arm_init_before_mmu(void)
2782c451f78SAneesh V {
2792c451f78SAneesh V 	v7_outer_cache_enable();
2802c451f78SAneesh V 	invalidate_dcache_all();
2812c451f78SAneesh V 	v7_inval_tlb();
2822c451f78SAneesh V }
2832c451f78SAneesh V 
2840dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop)
2850dde7f53SSimon Glass {
2860dde7f53SSimon Glass 	flush_dcache_range(start, stop);
2870dde7f53SSimon Glass 	v7_inval_tlb();
2880dde7f53SSimon Glass }
2890dde7f53SSimon Glass 
2902c451f78SAneesh V /*
2912c451f78SAneesh V  * Flush range from all levels of d-cache/unified-cache used:
2922c451f78SAneesh V  * Affects the range [start, start + size - 1]
2932c451f78SAneesh V  */
2942c451f78SAneesh V void  flush_cache(unsigned long start, unsigned long size)
2952c451f78SAneesh V {
2962c451f78SAneesh V 	flush_dcache_range(start, start + size);
2972c451f78SAneesh V }
2982c451f78SAneesh V #else /* #ifndef CONFIG_SYS_DCACHE_OFF */
2992c451f78SAneesh V void invalidate_dcache_all(void)
3002c451f78SAneesh V {
3012c451f78SAneesh V }
3022c451f78SAneesh V 
3032c451f78SAneesh V void flush_dcache_all(void)
3042c451f78SAneesh V {
3052c451f78SAneesh V }
3062c451f78SAneesh V 
3072c451f78SAneesh V void invalidate_dcache_range(unsigned long start, unsigned long stop)
3082c451f78SAneesh V {
3092c451f78SAneesh V }
3102c451f78SAneesh V 
3112c451f78SAneesh V void flush_dcache_range(unsigned long start, unsigned long stop)
3122c451f78SAneesh V {
3132c451f78SAneesh V }
3142c451f78SAneesh V 
3152c451f78SAneesh V void arm_init_before_mmu(void)
3162c451f78SAneesh V {
3172c451f78SAneesh V }
3182c451f78SAneesh V 
3192c451f78SAneesh V void  flush_cache(unsigned long start, unsigned long size)
3202c451f78SAneesh V {
3212c451f78SAneesh V }
3220dde7f53SSimon Glass 
3230dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop)
3240dde7f53SSimon Glass {
3250dde7f53SSimon Glass }
3260dde7f53SSimon Glass 
327de63ac27SR Sricharan void arm_init_domains(void)
328de63ac27SR Sricharan {
329de63ac27SR Sricharan }
3302c451f78SAneesh V #endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
3312c451f78SAneesh V 
3322c451f78SAneesh V #ifndef CONFIG_SYS_ICACHE_OFF
3332c451f78SAneesh V /* Invalidate entire I-cache and branch predictor array */
3342c451f78SAneesh V void invalidate_icache_all(void)
3352c451f78SAneesh V {
3362c451f78SAneesh V 	/*
3372c451f78SAneesh V 	 * Invalidate all instruction caches to PoU.
3382c451f78SAneesh V 	 * Also flushes branch target cache.
3392c451f78SAneesh V 	 */
3402c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
3412c451f78SAneesh V 
3422c451f78SAneesh V 	/* Invalidate entire branch predictor array */
3432c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
3442c451f78SAneesh V 
3452c451f78SAneesh V 	/* Full system DSB - make sure that the invalidation is complete */
346*9ba379adSValentine Barshak 	DSB;
3472c451f78SAneesh V 
3482c451f78SAneesh V 	/* ISB - make sure the instruction stream sees it */
349*9ba379adSValentine Barshak 	ISB;
3502c451f78SAneesh V }
3512c451f78SAneesh V #else
3522c451f78SAneesh V void invalidate_icache_all(void)
3532c451f78SAneesh V {
3542c451f78SAneesh V }
3552c451f78SAneesh V #endif
3562c451f78SAneesh V 
357fcfddfd5SJeroen Hofstee /*  Stub implementations for outer cache operations */
358fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_enable(void) {}
359fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_disable(void) {}
360fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_all(void) {}
361fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_all(void) {}
362fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_range(u32 start, u32 end) {}
363fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_range(u32 start, u32 end) {}
364