blob: 516754caeaf919e4ba87711da4639d1832c56f31 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkedc48b62002-09-08 17:56:50 +00002/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkedc48b62002-09-08 17:56:50 +00005 */
6
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <config.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Thierry Redingc97d9742014-12-09 22:25:22 -070010#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
wdenkf8062712005-01-09 23:16:25 +000013
Ovidiu Panait68b371a2020-03-29 20:57:39 +030014DECLARE_GLOBAL_DATA_PTR;
15
Wu, Josh22190262015-07-27 11:40:17 +080016/*
17 * Flush range from all levels of d-cache/unified-cache.
18 * Affects the range [start, start + size - 1].
19 */
Jeroen Hofsteed7460772014-06-23 22:07:04 +020020__weak void flush_cache(unsigned long start, unsigned long size)
wdenkedc48b62002-09-08 17:56:50 +000021{
Wu, Josh22190262015-07-27 11:40:17 +080022 flush_dcache_range(start, start + size);
wdenkedc48b62002-09-08 17:56:50 +000023}
Aneesh V3bda3772011-06-16 23:30:50 +000024
25/*
26 * Default implementation:
27 * do a range flush for the entire range
28 */
Jeroen Hofsteed7460772014-06-23 22:07:04 +020029__weak void flush_dcache_all(void)
Aneesh V3bda3772011-06-16 23:30:50 +000030{
31 flush_cache(0, ~0);
32}
Aneesh Vfffbb972011-08-16 04:33:05 +000033
34/*
35 * Default implementation of enable_caches()
36 * Real implementation should be in platform code
37 */
Jeroen Hofsteed7460772014-06-23 22:07:04 +020038__weak void enable_caches(void)
Aneesh Vfffbb972011-08-16 04:33:05 +000039{
40 puts("WARNING: Caches not enabled\n");
41}
Thierry Redingc97d9742014-12-09 22:25:22 -070042
Wu, Joshaaa35452015-07-27 11:40:16 +080043__weak void invalidate_dcache_range(unsigned long start, unsigned long stop)
44{
45 /* An empty stub, real implementation should be in platform code */
46}
47__weak void flush_dcache_range(unsigned long start, unsigned long stop)
48{
49 /* An empty stub, real implementation should be in platform code */
50}
51
Simon Glass85406582016-06-19 19:43:01 -060052int check_cache_range(unsigned long start, unsigned long stop)
53{
54 int ok = 1;
55
56 if (start & (CONFIG_SYS_CACHELINE_SIZE - 1))
57 ok = 0;
58
59 if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1))
60 ok = 0;
61
62 if (!ok) {
Simon Glass5c1c9ea2024-09-29 19:49:41 -060063 warn_non_xpl("CACHE: Misaligned operation at range [%08lx, %08lx]\n",
Simon Glass143997a2016-06-19 19:43:05 -060064 start, stop);
Simon Glass85406582016-06-19 19:43:01 -060065 }
66
67 return ok;
68}
69
Thierry Redingc97d9742014-12-09 22:25:22 -070070#ifdef CONFIG_SYS_NONCACHED_MEMORY
71/*
72 * Reserve one MMU section worth of address space below the malloc() area that
73 * will be mapped uncached.
74 */
75static unsigned long noncached_start;
76static unsigned long noncached_end;
77static unsigned long noncached_next;
78
Patrice Chotarde2eb7212020-04-28 11:38:03 +020079void noncached_set_region(void)
80{
81#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
82 mmu_set_region_dcache_behaviour(noncached_start,
83 noncached_end - noncached_start,
84 DCACHE_OFF);
85#endif
86}
87
Ovidiu Panait1c45ed92020-11-28 10:43:13 +020088int noncached_init(void)
Thierry Redingc97d9742014-12-09 22:25:22 -070089{
90 phys_addr_t start, end;
91 size_t size;
92
Stephen Warren9b496432019-08-27 11:54:31 -060093 /* If this calculation changes, update board_f.c:reserve_noncached() */
Thierry Redingc97d9742014-12-09 22:25:22 -070094 end = ALIGN(mem_malloc_start, MMU_SECTION_SIZE) - MMU_SECTION_SIZE;
95 size = ALIGN(CONFIG_SYS_NONCACHED_MEMORY, MMU_SECTION_SIZE);
96 start = end - size;
97
98 debug("mapping memory %pa-%pa non-cached\n", &start, &end);
99
100 noncached_start = start;
101 noncached_end = end;
102 noncached_next = start;
103
Patrice Chotarde2eb7212020-04-28 11:38:03 +0200104 noncached_set_region();
Ovidiu Panait1c45ed92020-11-28 10:43:13 +0200105
106 return 0;
Thierry Redingc97d9742014-12-09 22:25:22 -0700107}
108
109phys_addr_t noncached_alloc(size_t size, size_t align)
110{
111 phys_addr_t next = ALIGN(noncached_next, align);
112
113 if (next >= noncached_end || (noncached_end - next) < size)
114 return 0;
115
116 debug("allocated %zu bytes of uncached memory @%pa\n", size, &next);
117 noncached_next = next + size;
118
119 return next;
120}
121#endif /* CONFIG_SYS_NONCACHED_MEMORY */
Albert ARIBAUDa3823222015-10-23 18:06:40 +0200122
Tom Rini1c640a62017-03-18 09:01:44 -0400123#if CONFIG_IS_ENABLED(SYS_THUMB_BUILD)
Albert ARIBAUDa3823222015-10-23 18:06:40 +0200124void invalidate_l2_cache(void)
125{
126 unsigned int val = 0;
127
128 asm volatile("mcr p15, 1, %0, c15, c11, 0 @ invl l2 cache"
129 : : "r" (val) : "cc");
130 isb();
131}
132#endif
Ovidiu Panait68b371a2020-03-29 20:57:39 +0300133
Ovidiu Panait2a2941b2020-03-29 20:57:41 +0300134int arch_reserve_mmu(void)
Ovidiu Panait68b371a2020-03-29 20:57:39 +0300135{
Ovidiu Panait2b618472020-03-29 20:57:40 +0300136 return arm_reserve_mmu();
137}
138
139__weak int arm_reserve_mmu(void)
140{
Ovidiu Panait68b371a2020-03-29 20:57:39 +0300141#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
142 /* reserve TLB table */
143 gd->arch.tlb_size = PGTABLE_SIZE;
144 gd->relocaddr -= gd->arch.tlb_size;
145
146 /* round down to next 64 kB limit */
147 gd->relocaddr &= ~(0x10000 - 1);
148
149 gd->arch.tlb_addr = gd->relocaddr;
150 debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
151 gd->arch.tlb_addr + gd->arch.tlb_size);
152
Tom Rini6a5dccc2022-11-16 13:10:41 -0500153#ifdef CFG_SYS_MEM_RESERVE_SECURE
Ovidiu Panait68b371a2020-03-29 20:57:39 +0300154 /*
155 * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
156 * with location within secure ram.
157 */
158 gd->arch.tlb_allocated = gd->arch.tlb_addr;
159#endif
Pierre-Clément Tosi0ac98042023-02-09 04:54:28 +0800160
161 if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
162 /*
163 * As invalidate_dcache_all() will be called before
164 * mmu_setup(), we should make sure that the PTs are
165 * already in a valid state.
166 */
167 memset((void *)gd->arch.tlb_addr, 0, gd->arch.tlb_size);
168 }
Ovidiu Panait68b371a2020-03-29 20:57:39 +0300169#endif
170
171 return 0;
172}