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 13df120142SHans de Goede #define ARMV7_DCACHE_INVAL_RANGE 1 14df120142SHans de Goede #define ARMV7_DCACHE_CLEAN_INVAL_RANGE 2 152c451f78SAneesh V 162c451f78SAneesh V #ifndef CONFIG_SYS_DCACHE_OFF 17c09d2905SHans de Goede 18c09d2905SHans de Goede /* Asm functions from cache_v7_asm.S */ 19c09d2905SHans de Goede void v7_flush_dcache_all(void); 20df120142SHans de Goede void v7_invalidate_dcache_all(void); 21c09d2905SHans de Goede 222c451f78SAneesh V static u32 get_ccsidr(void) 232c451f78SAneesh V { 242c451f78SAneesh V u32 ccsidr; 252c451f78SAneesh V 262c451f78SAneesh V /* Read current CP15 Cache Size ID Register */ 272c451f78SAneesh V asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr)); 282c451f78SAneesh V return ccsidr; 292c451f78SAneesh V } 302c451f78SAneesh V 31b9297c22SThierry Reding static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len) 322c451f78SAneesh V { 332c451f78SAneesh V u32 mva; 342c451f78SAneesh V 352c451f78SAneesh V /* Align start to cache line boundary */ 362c451f78SAneesh V start &= ~(line_len - 1); 372c451f78SAneesh V for (mva = start; mva < stop; mva = mva + line_len) { 382c451f78SAneesh V /* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */ 392c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva)); 402c451f78SAneesh V } 412c451f78SAneesh V } 422c451f78SAneesh V 432c451f78SAneesh V static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len) 442c451f78SAneesh V { 452c451f78SAneesh V u32 mva; 462c451f78SAneesh V 476b424611SSimon Glass if (!check_cache_range(start, stop)) 486b424611SSimon Glass return; 492c451f78SAneesh V 502c451f78SAneesh V for (mva = start; mva < stop; mva = mva + line_len) { 512c451f78SAneesh V /* DCIMVAC - Invalidate data cache by MVA to PoC */ 522c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva)); 532c451f78SAneesh V } 542c451f78SAneesh V } 552c451f78SAneesh V 562c451f78SAneesh V static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op) 572c451f78SAneesh V { 582c451f78SAneesh V u32 line_len, ccsidr; 592c451f78SAneesh V 602c451f78SAneesh V ccsidr = get_ccsidr(); 612c451f78SAneesh V line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >> 622c451f78SAneesh V CCSIDR_LINE_SIZE_OFFSET) + 2; 632c451f78SAneesh V /* Converting from words to bytes */ 642c451f78SAneesh V line_len += 2; 652c451f78SAneesh V /* converting from log2(linelen) to linelen */ 662c451f78SAneesh V line_len = 1 << line_len; 672c451f78SAneesh V 682c451f78SAneesh V switch (range_op) { 692c451f78SAneesh V case ARMV7_DCACHE_CLEAN_INVAL_RANGE: 702c451f78SAneesh V v7_dcache_clean_inval_range(start, stop, line_len); 712c451f78SAneesh V break; 722c451f78SAneesh V case ARMV7_DCACHE_INVAL_RANGE: 732c451f78SAneesh V v7_dcache_inval_range(start, stop, line_len); 742c451f78SAneesh V break; 752c451f78SAneesh V } 762c451f78SAneesh V 77882f80b9SAneesh V /* DSB to make sure the operation is complete */ 78*a78cd861STom Rini dsb(); 792c451f78SAneesh V } 802c451f78SAneesh V 812c451f78SAneesh V /* Invalidate TLB */ 822c451f78SAneesh V static void v7_inval_tlb(void) 832c451f78SAneesh V { 842c451f78SAneesh V /* Invalidate entire unified TLB */ 852c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0)); 862c451f78SAneesh V /* Invalidate entire data TLB */ 872c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0)); 882c451f78SAneesh V /* Invalidate entire instruction TLB */ 892c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0)); 902c451f78SAneesh V /* Full system DSB - make sure that the invalidation is complete */ 91*a78cd861STom Rini dsb(); 922c451f78SAneesh V /* Full system ISB - make sure the instruction stream sees it */ 93*a78cd861STom Rini isb(); 942c451f78SAneesh V } 952c451f78SAneesh V 962c451f78SAneesh V void invalidate_dcache_all(void) 972c451f78SAneesh V { 98df120142SHans de Goede v7_invalidate_dcache_all(); 992c451f78SAneesh V 1002c451f78SAneesh V v7_outer_cache_inval_all(); 1012c451f78SAneesh V } 1022c451f78SAneesh V 1032c451f78SAneesh V /* 1042c451f78SAneesh V * Performs a clean & invalidation of the entire data cache 1052c451f78SAneesh V * at all levels 1062c451f78SAneesh V */ 1072c451f78SAneesh V void flush_dcache_all(void) 1082c451f78SAneesh V { 109c09d2905SHans de Goede v7_flush_dcache_all(); 1102c451f78SAneesh V 1112c451f78SAneesh V v7_outer_cache_flush_all(); 1122c451f78SAneesh V } 1132c451f78SAneesh V 1142c451f78SAneesh V /* 1152c451f78SAneesh V * Invalidates range in all levels of D-cache/unified cache used: 1162c451f78SAneesh V * Affects the range [start, stop - 1] 1172c451f78SAneesh V */ 1182c451f78SAneesh V void invalidate_dcache_range(unsigned long start, unsigned long stop) 1192c451f78SAneesh V { 12011aa6a32SMarek Vasut check_cache_range(start, stop); 12111aa6a32SMarek Vasut 1222c451f78SAneesh V v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE); 1232c451f78SAneesh V 1242c451f78SAneesh V v7_outer_cache_inval_range(start, stop); 1252c451f78SAneesh V } 1262c451f78SAneesh V 1272c451f78SAneesh V /* 1282c451f78SAneesh V * Flush range(clean & invalidate) from all levels of D-cache/unified 1292c451f78SAneesh V * cache used: 1302c451f78SAneesh V * Affects the range [start, stop - 1] 1312c451f78SAneesh V */ 1322c451f78SAneesh V void flush_dcache_range(unsigned long start, unsigned long stop) 1332c451f78SAneesh V { 13411aa6a32SMarek Vasut check_cache_range(start, stop); 13511aa6a32SMarek Vasut 1362c451f78SAneesh V v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE); 1372c451f78SAneesh V 1382c451f78SAneesh V v7_outer_cache_flush_range(start, stop); 1392c451f78SAneesh V } 1402c451f78SAneesh V 1412c451f78SAneesh V void arm_init_before_mmu(void) 1422c451f78SAneesh V { 1432c451f78SAneesh V v7_outer_cache_enable(); 1442c451f78SAneesh V invalidate_dcache_all(); 1452c451f78SAneesh V v7_inval_tlb(); 1462c451f78SAneesh V } 1472c451f78SAneesh V 1480dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop) 1490dde7f53SSimon Glass { 1500dde7f53SSimon Glass flush_dcache_range(start, stop); 1510dde7f53SSimon Glass v7_inval_tlb(); 1520dde7f53SSimon Glass } 1532c451f78SAneesh V #else /* #ifndef CONFIG_SYS_DCACHE_OFF */ 1542c451f78SAneesh V void invalidate_dcache_all(void) 1552c451f78SAneesh V { 1562c451f78SAneesh V } 1572c451f78SAneesh V 1582c451f78SAneesh V void flush_dcache_all(void) 1592c451f78SAneesh V { 1602c451f78SAneesh V } 1612c451f78SAneesh V 162ec6f6100SDaniel Allred void invalidate_dcache_range(unsigned long start, unsigned long stop) 163ec6f6100SDaniel Allred { 164ec6f6100SDaniel Allred } 165ec6f6100SDaniel Allred 166ec6f6100SDaniel Allred void flush_dcache_range(unsigned long start, unsigned long stop) 167ec6f6100SDaniel Allred { 168ec6f6100SDaniel Allred } 169ec6f6100SDaniel Allred 1702c451f78SAneesh V void arm_init_before_mmu(void) 1712c451f78SAneesh V { 1722c451f78SAneesh V } 1732c451f78SAneesh V 1740dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop) 1750dde7f53SSimon Glass { 1760dde7f53SSimon Glass } 1770dde7f53SSimon Glass 178de63ac27SR Sricharan void arm_init_domains(void) 179de63ac27SR Sricharan { 180de63ac27SR Sricharan } 1812c451f78SAneesh V #endif /* #ifndef CONFIG_SYS_DCACHE_OFF */ 1822c451f78SAneesh V 1832c451f78SAneesh V #ifndef CONFIG_SYS_ICACHE_OFF 1842c451f78SAneesh V /* Invalidate entire I-cache and branch predictor array */ 1852c451f78SAneesh V void invalidate_icache_all(void) 1862c451f78SAneesh V { 1872c451f78SAneesh V /* 1882c451f78SAneesh V * Invalidate all instruction caches to PoU. 1892c451f78SAneesh V * Also flushes branch target cache. 1902c451f78SAneesh V */ 1912c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0)); 1922c451f78SAneesh V 1932c451f78SAneesh V /* Invalidate entire branch predictor array */ 1942c451f78SAneesh V asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0)); 1952c451f78SAneesh V 1962c451f78SAneesh V /* Full system DSB - make sure that the invalidation is complete */ 197*a78cd861STom Rini dsb(); 1982c451f78SAneesh V 1992c451f78SAneesh V /* ISB - make sure the instruction stream sees it */ 200*a78cd861STom Rini isb(); 2012c451f78SAneesh V } 2022c451f78SAneesh V #else 2032c451f78SAneesh V void invalidate_icache_all(void) 2042c451f78SAneesh V { 2052c451f78SAneesh V } 2062c451f78SAneesh V #endif 2072c451f78SAneesh V 208fcfddfd5SJeroen Hofstee /* Stub implementations for outer cache operations */ 209fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_enable(void) {} 210fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_disable(void) {} 211fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_all(void) {} 212fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_all(void) {} 213fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_range(u32 start, u32 end) {} 214fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_range(u32 start, u32 end) {} 215