1 // SPDX-License-Identifier: GPL-2.0-only
3 * Broadcom Brahma-B15 CPU read-ahead cache management functions
5 * Copyright (C) 2015-2016 Broadcom
8 #include <linux/cfi_types.h>
10 #include <linux/spinlock.h>
12 #include <linux/bitops.h>
13 #include <linux/of_address.h>
14 #include <linux/notifier.h>
15 #include <linux/cpu.h>
16 #include <linux/syscore_ops.h>
17 #include <linux/reboot.h>
19 #include <asm/cacheflush.h>
20 #include <asm/hardware/cache-b15-rac.h>
22 extern void v7_flush_kern_cache_all(void);
24 /* RAC register offsets, relative to the HIF_CPU_BIUCTRL register base */
25 #define RAC_CONFIG0_REG (0x78)
26 #define RACENPREF_MASK (0x3)
27 #define RACPREFINST_SHIFT (0)
28 #define RACENINST_SHIFT (2)
29 #define RACPREFDATA_SHIFT (4)
30 #define RACENDATA_SHIFT (6)
31 #define RAC_CPU_SHIFT (8)
32 #define RACCFG_MASK (0xff)
33 #define RAC_CONFIG1_REG (0x7c)
34 /* Brahma-B15 is a quad-core only design */
35 #define B15_RAC_FLUSH_REG (0x80)
36 /* Brahma-B53 is an octo-core design */
37 #define B53_RAC_FLUSH_REG (0x84)
38 #define FLUSH_RAC (1 << 0)
40 /* Bitmask to enable instruction and data prefetching with a 256-bytes stride */
41 #define RAC_DATA_INST_EN_MASK (1 << RACPREFINST_SHIFT | \
42 RACENPREF_MASK << RACENINST_SHIFT | \
43 1 << RACPREFDATA_SHIFT | \
44 RACENPREF_MASK << RACENDATA_SHIFT)
47 /* Special state where we want to bypass the spinlock and call directly
48 * into the v7 cache maintenance operations during suspend/resume
50 #define RAC_SUSPENDED 1
52 static void __iomem
*b15_rac_base
;
53 static DEFINE_SPINLOCK(rac_lock
);
55 static u32 rac_config0_reg
;
56 static u32 rac_flush_offset
;
58 /* Initialization flag to avoid checking for b15_rac_base, and to prevent
59 * multi-platform kernels from crashing here as well.
61 static unsigned long b15_rac_flags
;
63 static inline u32
__b15_rac_disable(void)
65 u32 val
= __raw_readl(b15_rac_base
+ RAC_CONFIG0_REG
);
66 __raw_writel(0, b15_rac_base
+ RAC_CONFIG0_REG
);
71 static inline void __b15_rac_flush(void)
75 __raw_writel(FLUSH_RAC
, b15_rac_base
+ rac_flush_offset
);
77 /* This dmb() is required to force the Bus Interface Unit
78 * to clean outstanding writes, and forces an idle cycle
82 reg
= __raw_readl(b15_rac_base
+ rac_flush_offset
);
83 } while (reg
& FLUSH_RAC
);
86 static inline u32
b15_rac_disable_and_flush(void)
90 reg
= __b15_rac_disable();
95 static inline void __b15_rac_enable(u32 val
)
97 __raw_writel(val
, b15_rac_base
+ RAC_CONFIG0_REG
);
98 /* dsb() is required here to be consistent with __flush_icache_all() */
102 #define BUILD_RAC_CACHE_OP(name, bar) \
103 void b15_flush_##name(void) \
105 unsigned int do_flush; \
108 if (test_bit(RAC_SUSPENDED, &b15_rac_flags)) { \
114 spin_lock(&rac_lock); \
115 do_flush = test_bit(RAC_ENABLED, &b15_rac_flags); \
117 val = b15_rac_disable_and_flush(); \
122 __b15_rac_enable(val); \
123 spin_unlock(&rac_lock); \
128 /* The readahead cache present in the Brahma-B15 CPU is a special piece of
129 * hardware after the integrated L2 cache of the B15 CPU complex whose purpose
130 * is to prefetch instruction and/or data with a line size of either 64 bytes
131 * or 256 bytes. The rationale is that the data-bus of the CPU interface is
132 * optimized for 256-bytes transactions, and enabling the readahead cache
133 * provides a significant performance boost we want it enabled (typically
134 * twice the performance for a memcpy benchmark application).
136 * The readahead cache is transparent for Modified Virtual Addresses
137 * cache maintenance operations: ICIMVAU, DCIMVAC, DCCMVAC, DCCMVAU and
140 * It is however not transparent for the following cache maintenance
141 * operations: DCISW, DCCSW, DCCISW, ICIALLUIS and ICIALLU which is precisely
142 * what we are patching here with our BUILD_RAC_CACHE_OP here.
144 BUILD_RAC_CACHE_OP(kern_cache_all
, nobarrier
);
146 static void b15_rac_enable(void)
151 for_each_possible_cpu(cpu
)
152 enable
|= (RAC_DATA_INST_EN_MASK
<< (cpu
* RAC_CPU_SHIFT
));
154 b15_rac_disable_and_flush();
155 __b15_rac_enable(enable
);
158 static int b15_rac_reboot_notifier(struct notifier_block
*nb
,
159 unsigned long action
,
162 /* During kexec, we are not yet migrated on the boot CPU, so we need to
163 * make sure we are SMP safe here. Once the RAC is disabled, flag it as
164 * suspended such that the hotplug notifier returns early.
166 if (action
== SYS_RESTART
) {
167 spin_lock(&rac_lock
);
168 b15_rac_disable_and_flush();
169 clear_bit(RAC_ENABLED
, &b15_rac_flags
);
170 set_bit(RAC_SUSPENDED
, &b15_rac_flags
);
171 spin_unlock(&rac_lock
);
177 static struct notifier_block b15_rac_reboot_nb
= {
178 .notifier_call
= b15_rac_reboot_notifier
,
181 /* The CPU hotplug case is the most interesting one, we basically need to make
182 * sure that the RAC is disabled for the entire system prior to having a CPU
183 * die, in particular prior to this dying CPU having exited the coherency
186 * Once this CPU is marked dead, we can safely re-enable the RAC for the
187 * remaining CPUs in the system which are still online.
189 * Offlining a CPU is the problematic case, onlining a CPU is not much of an
190 * issue since the CPU and its cache-level hierarchy will start filling with
191 * the RAC disabled, so L1 and L2 only.
193 * In this function, we should NOT have to verify any unsafe setting/condition
196 * It is protected by the RAC_ENABLED flag which is cleared by default, and
197 * being cleared when initial procedure is done. b15_rac_base had been set at
201 * There is a small timing windows, in b15_rac_init(), between
202 * cpuhp_setup_state_*()
205 * However, there is no hotplug activity based on the Linux booting procedure.
207 * Since we have to disable RAC for all cores, we keep RAC on as long as as
208 * possible (disable it as late as possible) to gain the cache benefit.
210 * Thus, dying/dead states are chosen here
212 * We are choosing not do disable the RAC on a per-CPU basis, here, if we did
213 * we would want to consider disabling it as early as possible to benefit the
217 /* Running on the dying CPU */
218 static int b15_rac_dying_cpu(unsigned int cpu
)
220 /* During kexec/reboot, the RAC is disabled via the reboot notifier
223 if (test_bit(RAC_SUSPENDED
, &b15_rac_flags
))
226 spin_lock(&rac_lock
);
228 /* Indicate that we are starting a hotplug procedure */
229 __clear_bit(RAC_ENABLED
, &b15_rac_flags
);
231 /* Disable the readahead cache and save its value to a global */
232 rac_config0_reg
= b15_rac_disable_and_flush();
234 spin_unlock(&rac_lock
);
239 /* Running on a non-dying CPU */
240 static int b15_rac_dead_cpu(unsigned int cpu
)
242 /* During kexec/reboot, the RAC is disabled via the reboot notifier
245 if (test_bit(RAC_SUSPENDED
, &b15_rac_flags
))
248 spin_lock(&rac_lock
);
251 __b15_rac_enable(rac_config0_reg
);
252 __set_bit(RAC_ENABLED
, &b15_rac_flags
);
254 spin_unlock(&rac_lock
);
259 static int b15_rac_suspend(void)
261 /* Suspend the read-ahead cache oeprations, forcing our cache
262 * implementation to fallback to the regular ARMv7 calls.
264 * We are guaranteed to be running on the boot CPU at this point and
265 * with every other CPU quiesced, so setting RAC_SUSPENDED is not racy
268 rac_config0_reg
= b15_rac_disable_and_flush();
269 set_bit(RAC_SUSPENDED
, &b15_rac_flags
);
274 static void b15_rac_resume(void)
276 /* Coming out of a S3 suspend/resume cycle, the read-ahead cache
277 * register RAC_CONFIG0_REG will be restored to its default value, make
278 * sure we re-enable it and set the enable flag, we are also guaranteed
279 * to run on the boot CPU, so not racy again.
281 __b15_rac_enable(rac_config0_reg
);
282 clear_bit(RAC_SUSPENDED
, &b15_rac_flags
);
285 static struct syscore_ops b15_rac_syscore_ops
= {
286 .suspend
= b15_rac_suspend
,
287 .resume
= b15_rac_resume
,
290 static int __init
b15_rac_init(void)
292 struct device_node
*dn
, *cpu_dn
;
294 u32 reg
, en_mask
= 0;
296 dn
= of_find_compatible_node(NULL
, NULL
, "brcm,brcmstb-cpu-biu-ctrl");
300 if (WARN(num_possible_cpus() > 4, "RAC only supports 4 CPUs\n"))
303 b15_rac_base
= of_iomap(dn
, 0);
305 pr_err("failed to remap BIU control base\n");
310 cpu_dn
= of_get_cpu_node(0, NULL
);
316 if (of_device_is_compatible(cpu_dn
, "brcm,brahma-b15"))
317 rac_flush_offset
= B15_RAC_FLUSH_REG
;
318 else if (of_device_is_compatible(cpu_dn
, "brcm,brahma-b53"))
319 rac_flush_offset
= B53_RAC_FLUSH_REG
;
321 pr_err("Unsupported CPU\n");
328 ret
= register_reboot_notifier(&b15_rac_reboot_nb
);
330 pr_err("failed to register reboot notifier\n");
331 iounmap(b15_rac_base
);
335 if (IS_ENABLED(CONFIG_HOTPLUG_CPU
)) {
336 ret
= cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CACHE_B15_RAC_DEAD
,
337 "arm/cache-b15-rac:dead",
338 NULL
, b15_rac_dead_cpu
);
342 ret
= cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CACHE_B15_RAC_DYING
,
343 "arm/cache-b15-rac:dying",
344 NULL
, b15_rac_dying_cpu
);
349 if (IS_ENABLED(CONFIG_PM_SLEEP
))
350 register_syscore_ops(&b15_rac_syscore_ops
);
352 spin_lock(&rac_lock
);
353 reg
= __raw_readl(b15_rac_base
+ RAC_CONFIG0_REG
);
354 for_each_possible_cpu(cpu
)
355 en_mask
|= ((1 << RACPREFDATA_SHIFT
) << (cpu
* RAC_CPU_SHIFT
));
356 WARN(reg
& en_mask
, "Read-ahead cache not previously disabled\n");
359 set_bit(RAC_ENABLED
, &b15_rac_flags
);
360 spin_unlock(&rac_lock
);
362 pr_info("%pOF: Broadcom Brahma-B15 readahead cache\n", dn
);
367 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CACHE_B15_RAC_DYING
);
369 unregister_reboot_notifier(&b15_rac_reboot_nb
);
370 iounmap(b15_rac_base
);
375 arch_initcall(b15_rac_init
);