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*11aa6a32SMarek Vasut static int check_cache_range(unsigned long start, unsigned long stop) 20*11aa6a32SMarek Vasut { 21*11aa6a32SMarek Vasut int ok = 1; 22*11aa6a32SMarek Vasut 23*11aa6a32SMarek Vasut if (start & (CONFIG_SYS_CACHELINE_SIZE - 1)) 24*11aa6a32SMarek Vasut ok = 0; 25*11aa6a32SMarek Vasut 26*11aa6a32SMarek Vasut if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1)) 27*11aa6a32SMarek Vasut ok = 0; 28*11aa6a32SMarek Vasut 29*11aa6a32SMarek Vasut if (!ok) 30*11aa6a32SMarek Vasut debug("CACHE: Misaligned operation at range [%08lx, %08lx]\n", 31*11aa6a32SMarek Vasut start, stop); 32*11aa6a32SMarek Vasut 33*11aa6a32SMarek Vasut return ok; 34*11aa6a32SMarek Vasut } 35*11aa6a32SMarek Vasut 362c451f78SAneesh V /* 372c451f78SAneesh V * Write the level and type you want to Cache Size Selection Register(CSSELR) 382c451f78SAneesh V * to get size details from Current Cache Size ID Register(CCSIDR) 392c451f78SAneesh V */ 402c451f78SAneesh V static void set_csselr(u32 level, u32 type) 41b9297c22SThierry Reding { 42b9297c22SThierry Reding u32 csselr = level << 1 | type; 432c451f78SAneesh V 442c451f78SAneesh V /* Write to Cache Size Selection Register(CSSELR) */ 452c451f78SAneesh V asm volatile ("mcr p15, 2, %0, c0, c0, 0" : : "r" (csselr)); 462c451f78SAneesh V } 472c451f78SAneesh V 482c451f78SAneesh V static u32 get_ccsidr(void) 492c451f78SAneesh V { 502c451f78SAneesh V u32 ccsidr; 512c451f78SAneesh V 522c451f78SAneesh V /* Read current CP15 Cache Size ID Register */ 532c451f78SAneesh V asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr)); 542c451f78SAneesh V return ccsidr; 552c451f78SAneesh V } 562c451f78SAneesh V 572c451f78SAneesh V static u32 get_clidr(void) 582c451f78SAneesh V { 592c451f78SAneesh V u32 clidr; 602c451f78SAneesh V 612c451f78SAneesh V /* Read current CP15 Cache Level ID Register */ 622c451f78SAneesh V asm volatile ("mrc p15,1,%0,c0,c0,1" : "=r" (clidr)); 632c451f78SAneesh V return clidr; 642c451f78SAneesh V } 652c451f78SAneesh V 662c451f78SAneesh V static void v7_inval_dcache_level_setway(u32 level, u32 num_sets, 672c451f78SAneesh V u32 num_ways, u32 way_shift, 682c451f78SAneesh V u32 log2_line_len) 692c451f78SAneesh V { 70b9297c22SThierry Reding int way, set; 71b9297c22SThierry Reding u32 setway; 722c451f78SAneesh V 732c451f78SAneesh V /* 742c451f78SAneesh V * For optimal assembly code: 752c451f78SAneesh V * a. count down 762c451f78SAneesh V * b. have bigger loop inside 772c451f78SAneesh V */ 782c451f78SAneesh V for (way = num_ways - 1; way >= 0 ; way--) { 792c451f78SAneesh V for (set = num_sets - 1; set >= 0; set--) { 802c451f78SAneesh V setway = (level << 1) | (set << log2_line_len) | 812c451f78SAneesh V (way << way_shift); 822c451f78SAneesh V /* Invalidate data/unified cache line by set/way */ 832c451f78SAneesh V asm volatile (" mcr p15, 0, %0, c7, c6, 2" 842c451f78SAneesh V : : "r" (setway)); 852c451f78SAneesh V } 862c451f78SAneesh V } 87882f80b9SAneesh V /* DSB to make sure the operation is complete */ 889ba379adSValentine Barshak DSB; 892c451f78SAneesh V } 902c451f78SAneesh V 912c451f78SAneesh V static void v7_clean_inval_dcache_level_setway(u32 level, u32 num_sets, 922c451f78SAneesh V u32 num_ways, u32 way_shift, 932c451f78SAneesh V u32 log2_line_len) 942c451f78SAneesh V { 95b9297c22SThierry Reding int way, set; 96b9297c22SThierry Reding u32 setway; 972c451f78SAneesh V 982c451f78SAneesh V /* 992c451f78SAneesh V * For optimal assembly code: 1002c451f78SAneesh V * a. count down 1012c451f78SAneesh V * b. have bigger loop inside 1022c451f78SAneesh V */ 1032c451f78SAneesh V for (way = num_ways - 1; way >= 0 ; way--) { 1042c451f78SAneesh V for (set = num_sets - 1; set >= 0; set--) { 1052c451f78SAneesh V setway = (level << 1) | (set << log2_line_len) | 1062c451f78SAneesh V (way << way_shift); 1072c451f78SAneesh V /* 1082c451f78SAneesh V * Clean & Invalidate data/unified 1092c451f78SAneesh V * cache line by set/way 1102c451f78SAneesh V */ 1112c451f78SAneesh V asm volatile (" mcr p15, 0, %0, c7, c14, 2" 1122c451f78SAneesh V : : "r" (setway)); 1132c451f78SAneesh V } 1142c451f78SAneesh V } 115882f80b9SAneesh V /* DSB to make sure the operation is complete */ 1169ba379adSValentine Barshak DSB; 1172c451f78SAneesh V } 1182c451f78SAneesh V 1192c451f78SAneesh V static void v7_maint_dcache_level_setway(u32 level, u32 operation) 1202c451f78SAneesh V { 1212c451f78SAneesh V u32 ccsidr; 1222c451f78SAneesh V u32 num_sets, num_ways, log2_line_len, log2_num_ways; 1232c451f78SAneesh V u32 way_shift; 1242c451f78SAneesh V 1252c451f78SAneesh V set_csselr(level, ARMV7_CSSELR_IND_DATA_UNIFIED); 1262c451f78SAneesh V 1272c451f78SAneesh V ccsidr = get_ccsidr(); 1282c451f78SAneesh V 1292c451f78SAneesh V log2_line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >> 1302c451f78SAneesh V CCSIDR_LINE_SIZE_OFFSET) + 2; 1312c451f78SAneesh V /* Converting from words to bytes */ 1322c451f78SAneesh V log2_line_len += 2; 1332c451f78SAneesh V 1342c451f78SAneesh V num_ways = ((ccsidr & CCSIDR_ASSOCIATIVITY_MASK) >> 1352c451f78SAneesh V CCSIDR_ASSOCIATIVITY_OFFSET) + 1; 1362c451f78SAneesh V num_sets = ((ccsidr & CCSIDR_NUM_SETS_MASK) >> 1372c451f78SAneesh V CCSIDR_NUM_SETS_OFFSET) + 1; 1382c451f78SAneesh V /* 1392c451f78SAneesh V * According to ARMv7 ARM number of sets and number of ways need 1402c451f78SAneesh V * not be a power of 2 1412c451f78SAneesh V */ 1422c451f78SAneesh V log2_num_ways = log_2_n_round_up(num_ways); 1432c451f78SAneesh V 1442c451f78SAneesh V way_shift = (32 - log2_num_ways); 1452c451f78SAneesh V if (operation == ARMV7_DCACHE_INVAL_ALL) { 1462c451f78SAneesh V v7_inval_dcache_level_setway(level, num_sets, num_ways, 1472c451f78SAneesh V way_shift, log2_line_len); 1482c451f78SAneesh V } else if (operation == ARMV7_DCACHE_CLEAN_INVAL_ALL) { 1492c451f78SAneesh V v7_clean_inval_dcache_level_setway(level, num_sets, num_ways, 1502c451f78SAneesh V way_shift, log2_line_len); 1512c451f78SAneesh V } 1522c451f78SAneesh V } 1532c451f78SAneesh V 1542c451f78SAneesh V static void v7_maint_dcache_all(u32 operation) 1552c451f78SAneesh V { 1562c451f78SAneesh V u32 level, cache_type, level_start_bit = 0; 1572c451f78SAneesh V u32 clidr = get_clidr(); 1582c451f78SAneesh V 1592c451f78SAneesh V for (level = 0; level < 7; level++) { 1602c451f78SAneesh V cache_type = (clidr >> level_start_bit) & 0x7; 1612c451f78SAneesh V if ((cache_type == ARMV7_CLIDR_CTYPE_DATA_ONLY) || 1622c451f78SAneesh V (cache_type == ARMV7_CLIDR_CTYPE_INSTRUCTION_DATA) || 1632c451f78SAneesh V (cache_type == ARMV7_CLIDR_CTYPE_UNIFIED)) 1642c451f78SAneesh V v7_maint_dcache_level_setway(level, operation); 1652c451f78SAneesh V level_start_bit += 3; 1662c451f78SAneesh V } 1672c451f78SAneesh V } 1682c451f78SAneesh V 169b9297c22SThierry Reding static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len) 1702c451f78SAneesh V { 1712c451f78SAneesh V u32 mva; 1722c451f78SAneesh V 1732c451f78SAneesh V /* Align start to cache line boundary */ 1742c451f78SAneesh V start &= ~(line_len - 1); 1752c451f78SAneesh V for (mva = start; mva < stop; mva = mva + line_len) { 1762c451f78SAneesh V /* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */ 1772c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva)); 1782c451f78SAneesh V } 1792c451f78SAneesh V } 1802c451f78SAneesh V 1812c451f78SAneesh V static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len) 1822c451f78SAneesh V { 1832c451f78SAneesh V u32 mva; 1842c451f78SAneesh V 1852c451f78SAneesh V /* 186cabe2878SAneesh V * If start address is not aligned to cache-line do not 187cabe2878SAneesh V * invalidate the first cache-line 1882c451f78SAneesh V */ 1892c451f78SAneesh V if (start & (line_len - 1)) { 190cabe2878SAneesh V printf("ERROR: %s - start address is not aligned - 0x%08x\n", 191cabe2878SAneesh V __func__, start); 1922c451f78SAneesh V /* move to next cache line */ 1932c451f78SAneesh V start = (start + line_len - 1) & ~(line_len - 1); 1942c451f78SAneesh V } 1952c451f78SAneesh V 1962c451f78SAneesh V /* 197cabe2878SAneesh V * If stop address is not aligned to cache-line do not 198cabe2878SAneesh V * invalidate the last cache-line 1992c451f78SAneesh V */ 2002c451f78SAneesh V if (stop & (line_len - 1)) { 201cabe2878SAneesh V printf("ERROR: %s - stop address is not aligned - 0x%08x\n", 202cabe2878SAneesh V __func__, stop); 2032c451f78SAneesh V /* align to the beginning of this cache line */ 2042c451f78SAneesh V stop &= ~(line_len - 1); 2052c451f78SAneesh V } 2062c451f78SAneesh V 2072c451f78SAneesh V for (mva = start; mva < stop; mva = mva + line_len) { 2082c451f78SAneesh V /* DCIMVAC - Invalidate data cache by MVA to PoC */ 2092c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva)); 2102c451f78SAneesh V } 2112c451f78SAneesh V } 2122c451f78SAneesh V 2132c451f78SAneesh V static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op) 2142c451f78SAneesh V { 2152c451f78SAneesh V u32 line_len, ccsidr; 2162c451f78SAneesh V 2172c451f78SAneesh V ccsidr = get_ccsidr(); 2182c451f78SAneesh V line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >> 2192c451f78SAneesh V CCSIDR_LINE_SIZE_OFFSET) + 2; 2202c451f78SAneesh V /* Converting from words to bytes */ 2212c451f78SAneesh V line_len += 2; 2222c451f78SAneesh V /* converting from log2(linelen) to linelen */ 2232c451f78SAneesh V line_len = 1 << line_len; 2242c451f78SAneesh V 2252c451f78SAneesh V switch (range_op) { 2262c451f78SAneesh V case ARMV7_DCACHE_CLEAN_INVAL_RANGE: 2272c451f78SAneesh V v7_dcache_clean_inval_range(start, stop, line_len); 2282c451f78SAneesh V break; 2292c451f78SAneesh V case ARMV7_DCACHE_INVAL_RANGE: 2302c451f78SAneesh V v7_dcache_inval_range(start, stop, line_len); 2312c451f78SAneesh V break; 2322c451f78SAneesh V } 2332c451f78SAneesh V 234882f80b9SAneesh V /* DSB to make sure the operation is complete */ 2359ba379adSValentine Barshak DSB; 2362c451f78SAneesh V } 2372c451f78SAneesh V 2382c451f78SAneesh V /* Invalidate TLB */ 2392c451f78SAneesh V static void v7_inval_tlb(void) 2402c451f78SAneesh V { 2412c451f78SAneesh V /* Invalidate entire unified TLB */ 2422c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0)); 2432c451f78SAneesh V /* Invalidate entire data TLB */ 2442c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0)); 2452c451f78SAneesh V /* Invalidate entire instruction TLB */ 2462c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0)); 2472c451f78SAneesh V /* Full system DSB - make sure that the invalidation is complete */ 2489ba379adSValentine Barshak DSB; 2492c451f78SAneesh V /* Full system ISB - make sure the instruction stream sees it */ 2509ba379adSValentine Barshak ISB; 2512c451f78SAneesh V } 2522c451f78SAneesh V 2532c451f78SAneesh V void invalidate_dcache_all(void) 2542c451f78SAneesh V { 2552c451f78SAneesh V v7_maint_dcache_all(ARMV7_DCACHE_INVAL_ALL); 2562c451f78SAneesh V 2572c451f78SAneesh V v7_outer_cache_inval_all(); 2582c451f78SAneesh V } 2592c451f78SAneesh V 2602c451f78SAneesh V /* 2612c451f78SAneesh V * Performs a clean & invalidation of the entire data cache 2622c451f78SAneesh V * at all levels 2632c451f78SAneesh V */ 2642c451f78SAneesh V void flush_dcache_all(void) 2652c451f78SAneesh V { 2662c451f78SAneesh V v7_maint_dcache_all(ARMV7_DCACHE_CLEAN_INVAL_ALL); 2672c451f78SAneesh V 2682c451f78SAneesh V v7_outer_cache_flush_all(); 2692c451f78SAneesh V } 2702c451f78SAneesh V 2712c451f78SAneesh V /* 2722c451f78SAneesh V * Invalidates range in all levels of D-cache/unified cache used: 2732c451f78SAneesh V * Affects the range [start, stop - 1] 2742c451f78SAneesh V */ 2752c451f78SAneesh V void invalidate_dcache_range(unsigned long start, unsigned long stop) 2762c451f78SAneesh V { 277*11aa6a32SMarek Vasut check_cache_range(start, stop); 278*11aa6a32SMarek Vasut 2792c451f78SAneesh V v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE); 2802c451f78SAneesh V 2812c451f78SAneesh V v7_outer_cache_inval_range(start, stop); 2822c451f78SAneesh V } 2832c451f78SAneesh V 2842c451f78SAneesh V /* 2852c451f78SAneesh V * Flush range(clean & invalidate) from all levels of D-cache/unified 2862c451f78SAneesh V * cache used: 2872c451f78SAneesh V * Affects the range [start, stop - 1] 2882c451f78SAneesh V */ 2892c451f78SAneesh V void flush_dcache_range(unsigned long start, unsigned long stop) 2902c451f78SAneesh V { 291*11aa6a32SMarek Vasut check_cache_range(start, stop); 292*11aa6a32SMarek Vasut 2932c451f78SAneesh V v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE); 2942c451f78SAneesh V 2952c451f78SAneesh V v7_outer_cache_flush_range(start, stop); 2962c451f78SAneesh V } 2972c451f78SAneesh V 2982c451f78SAneesh V void arm_init_before_mmu(void) 2992c451f78SAneesh V { 3002c451f78SAneesh V v7_outer_cache_enable(); 3012c451f78SAneesh V invalidate_dcache_all(); 3022c451f78SAneesh V v7_inval_tlb(); 3032c451f78SAneesh V } 3042c451f78SAneesh V 3050dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop) 3060dde7f53SSimon Glass { 3070dde7f53SSimon Glass flush_dcache_range(start, stop); 3080dde7f53SSimon Glass v7_inval_tlb(); 3090dde7f53SSimon Glass } 3102c451f78SAneesh V #else /* #ifndef CONFIG_SYS_DCACHE_OFF */ 3112c451f78SAneesh V void invalidate_dcache_all(void) 3122c451f78SAneesh V { 3132c451f78SAneesh V } 3142c451f78SAneesh V 3152c451f78SAneesh V void flush_dcache_all(void) 3162c451f78SAneesh V { 3172c451f78SAneesh V } 3182c451f78SAneesh V 3192c451f78SAneesh V void arm_init_before_mmu(void) 3202c451f78SAneesh V { 3212c451f78SAneesh V } 3222c451f78SAneesh V 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 */ 3469ba379adSValentine Barshak DSB; 3472c451f78SAneesh V 3482c451f78SAneesh V /* ISB - make sure the instruction stream sees it */ 3499ba379adSValentine 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