blob: f769887900111612a953c49d113c1ad9a6dffcc3 [file] [log] [blame]
Florian Fainellif6f9be12017-12-01 01:10:09 +01001/*
2 * Broadcom Brahma-B15 CPU read-ahead cache management functions
3 *
4 * Copyright (C) 2015-2016 Broadcom
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/err.h>
12#include <linux/spinlock.h>
13#include <linux/io.h>
14#include <linux/bitops.h>
15#include <linux/of_address.h>
Florian Fainelli55de8872017-12-01 01:10:11 +010016#include <linux/notifier.h>
17#include <linux/cpu.h>
Florian Fainelli534f5f32017-12-01 01:10:12 +010018#include <linux/syscore_ops.h>
Florian Fainelli576a0862017-12-01 01:10:13 +010019#include <linux/reboot.h>
Florian Fainellif6f9be12017-12-01 01:10:09 +010020
21#include <asm/cacheflush.h>
22#include <asm/hardware/cache-b15-rac.h>
23
24extern void v7_flush_kern_cache_all(void);
25
26/* RAC register offsets, relative to the HIF_CPU_BIUCTRL register base */
27#define RAC_CONFIG0_REG (0x78)
28#define RACENPREF_MASK (0x3)
29#define RACPREFINST_SHIFT (0)
30#define RACENINST_SHIFT (2)
31#define RACPREFDATA_SHIFT (4)
32#define RACENDATA_SHIFT (6)
33#define RAC_CPU_SHIFT (8)
34#define RACCFG_MASK (0xff)
35#define RAC_CONFIG1_REG (0x7c)
36#define RAC_FLUSH_REG (0x80)
37#define FLUSH_RAC (1 << 0)
38
39/* Bitmask to enable instruction and data prefetching with a 256-bytes stride */
40#define RAC_DATA_INST_EN_MASK (1 << RACPREFINST_SHIFT | \
41 RACENPREF_MASK << RACENINST_SHIFT | \
42 1 << RACPREFDATA_SHIFT | \
43 RACENPREF_MASK << RACENDATA_SHIFT)
44
45#define RAC_ENABLED 0
Florian Fainelli534f5f32017-12-01 01:10:12 +010046/* Special state where we want to bypass the spinlock and call directly
47 * into the v7 cache maintenance operations during suspend/resume
48 */
49#define RAC_SUSPENDED 1
Florian Fainellif6f9be12017-12-01 01:10:09 +010050
51static void __iomem *b15_rac_base;
52static DEFINE_SPINLOCK(rac_lock);
Florian Fainelli55de8872017-12-01 01:10:11 +010053static u32 rac_config0_reg;
Florian Fainellif6f9be12017-12-01 01:10:09 +010054
55/* Initialization flag to avoid checking for b15_rac_base, and to prevent
56 * multi-platform kernels from crashing here as well.
57 */
58static unsigned long b15_rac_flags;
59
60static inline u32 __b15_rac_disable(void)
61{
62 u32 val = __raw_readl(b15_rac_base + RAC_CONFIG0_REG);
63 __raw_writel(0, b15_rac_base + RAC_CONFIG0_REG);
64 dmb();
65 return val;
66}
67
68static inline void __b15_rac_flush(void)
69{
70 u32 reg;
71
72 __raw_writel(FLUSH_RAC, b15_rac_base + RAC_FLUSH_REG);
73 do {
74 /* This dmb() is required to force the Bus Interface Unit
75 * to clean oustanding writes, and forces an idle cycle
76 * to be inserted.
77 */
78 dmb();
79 reg = __raw_readl(b15_rac_base + RAC_FLUSH_REG);
80 } while (reg & FLUSH_RAC);
81}
82
83static inline u32 b15_rac_disable_and_flush(void)
84{
85 u32 reg;
86
87 reg = __b15_rac_disable();
88 __b15_rac_flush();
89 return reg;
90}
91
92static inline void __b15_rac_enable(u32 val)
93{
94 __raw_writel(val, b15_rac_base + RAC_CONFIG0_REG);
95 /* dsb() is required here to be consistent with __flush_icache_all() */
96 dsb();
97}
98
99#define BUILD_RAC_CACHE_OP(name, bar) \
100void b15_flush_##name(void) \
101{ \
102 unsigned int do_flush; \
103 u32 val = 0; \
104 \
Florian Fainelli534f5f32017-12-01 01:10:12 +0100105 if (test_bit(RAC_SUSPENDED, &b15_rac_flags)) { \
106 v7_flush_##name(); \
107 bar; \
108 return; \
109 } \
110 \
Florian Fainellif6f9be12017-12-01 01:10:09 +0100111 spin_lock(&rac_lock); \
112 do_flush = test_bit(RAC_ENABLED, &b15_rac_flags); \
113 if (do_flush) \
114 val = b15_rac_disable_and_flush(); \
115 v7_flush_##name(); \
116 if (!do_flush) \
117 bar; \
118 else \
119 __b15_rac_enable(val); \
120 spin_unlock(&rac_lock); \
121}
122
123#define nobarrier
124
125/* The readahead cache present in the Brahma-B15 CPU is a special piece of
126 * hardware after the integrated L2 cache of the B15 CPU complex whose purpose
127 * is to prefetch instruction and/or data with a line size of either 64 bytes
128 * or 256 bytes. The rationale is that the data-bus of the CPU interface is
129 * optimized for 256-bytes transactions, and enabling the readahead cache
130 * provides a significant performance boost we want it enabled (typically
131 * twice the performance for a memcpy benchmark application).
132 *
133 * The readahead cache is transparent for Modified Virtual Addresses
134 * cache maintenance operations: ICIMVAU, DCIMVAC, DCCMVAC, DCCMVAU and
135 * DCCIMVAC.
136 *
137 * It is however not transparent for the following cache maintenance
138 * operations: DCISW, DCCSW, DCCISW, ICIALLUIS and ICIALLU which is precisely
139 * what we are patching here with our BUILD_RAC_CACHE_OP here.
140 */
141BUILD_RAC_CACHE_OP(kern_cache_all, nobarrier);
142
143static void b15_rac_enable(void)
144{
145 unsigned int cpu;
146 u32 enable = 0;
147
148 for_each_possible_cpu(cpu)
149 enable |= (RAC_DATA_INST_EN_MASK << (cpu * RAC_CPU_SHIFT));
150
151 b15_rac_disable_and_flush();
152 __b15_rac_enable(enable);
153}
154
Florian Fainelli576a0862017-12-01 01:10:13 +0100155static int b15_rac_reboot_notifier(struct notifier_block *nb,
156 unsigned long action,
157 void *data)
158{
159 /* During kexec, we are not yet migrated on the boot CPU, so we need to
160 * make sure we are SMP safe here. Once the RAC is disabled, flag it as
161 * suspended such that the hotplug notifier returns early.
162 */
163 if (action == SYS_RESTART) {
164 spin_lock(&rac_lock);
165 b15_rac_disable_and_flush();
166 clear_bit(RAC_ENABLED, &b15_rac_flags);
167 set_bit(RAC_SUSPENDED, &b15_rac_flags);
168 spin_unlock(&rac_lock);
169 }
170
171 return NOTIFY_DONE;
172}
173
174static struct notifier_block b15_rac_reboot_nb = {
175 .notifier_call = b15_rac_reboot_notifier,
176};
177
Florian Fainelli55de8872017-12-01 01:10:11 +0100178#ifdef CONFIG_HOTPLUG_CPU
179/* The CPU hotplug case is the most interesting one, we basically need to make
180 * sure that the RAC is disabled for the entire system prior to having a CPU
181 * die, in particular prior to this dying CPU having exited the coherency
182 * domain.
183 *
184 * Once this CPU is marked dead, we can safely re-enable the RAC for the
185 * remaining CPUs in the system which are still online.
186 *
187 * Offlining a CPU is the problematic case, onlining a CPU is not much of an
188 * issue since the CPU and its cache-level hierarchy will start filling with
189 * the RAC disabled, so L1 and L2 only.
190 *
191 * In this function, we should NOT have to verify any unsafe setting/condition
192 * b15_rac_base:
193 *
194 * It is protected by the RAC_ENABLED flag which is cleared by default, and
195 * being cleared when initial procedure is done. b15_rac_base had been set at
196 * that time.
197 *
198 * RAC_ENABLED:
199 * There is a small timing windows, in b15_rac_init(), between
200 * cpuhp_setup_state_*()
201 * ...
202 * set RAC_ENABLED
203 * However, there is no hotplug activity based on the Linux booting procedure.
204 *
205 * Since we have to disable RAC for all cores, we keep RAC on as long as as
206 * possible (disable it as late as possible) to gain the cache benefit.
207 *
208 * Thus, dying/dead states are chosen here
209 *
210 * We are choosing not do disable the RAC on a per-CPU basis, here, if we did
211 * we would want to consider disabling it as early as possible to benefit the
212 * other active CPUs.
213 */
214
215/* Running on the dying CPU */
216static int b15_rac_dying_cpu(unsigned int cpu)
217{
Florian Fainelli576a0862017-12-01 01:10:13 +0100218 /* During kexec/reboot, the RAC is disabled via the reboot notifier
219 * return early here.
220 */
221 if (test_bit(RAC_SUSPENDED, &b15_rac_flags))
222 return 0;
223
Florian Fainelli55de8872017-12-01 01:10:11 +0100224 spin_lock(&rac_lock);
225
226 /* Indicate that we are starting a hotplug procedure */
227 __clear_bit(RAC_ENABLED, &b15_rac_flags);
228
229 /* Disable the readahead cache and save its value to a global */
230 rac_config0_reg = b15_rac_disable_and_flush();
231
232 spin_unlock(&rac_lock);
233
234 return 0;
235}
236
237/* Running on a non-dying CPU */
238static int b15_rac_dead_cpu(unsigned int cpu)
239{
Florian Fainelli576a0862017-12-01 01:10:13 +0100240 /* During kexec/reboot, the RAC is disabled via the reboot notifier
241 * return early here.
242 */
243 if (test_bit(RAC_SUSPENDED, &b15_rac_flags))
244 return 0;
245
Florian Fainelli55de8872017-12-01 01:10:11 +0100246 spin_lock(&rac_lock);
247
248 /* And enable it */
249 __b15_rac_enable(rac_config0_reg);
250 __set_bit(RAC_ENABLED, &b15_rac_flags);
251
252 spin_unlock(&rac_lock);
253
254 return 0;
255}
256#endif /* CONFIG_HOTPLUG_CPU */
257
Florian Fainelli534f5f32017-12-01 01:10:12 +0100258#ifdef CONFIG_PM_SLEEP
259static int b15_rac_suspend(void)
260{
261 /* Suspend the read-ahead cache oeprations, forcing our cache
262 * implementation to fallback to the regular ARMv7 calls.
263 *
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
266 * here.
267 */
268 rac_config0_reg = b15_rac_disable_and_flush();
269 set_bit(RAC_SUSPENDED, &b15_rac_flags);
270
271 return 0;
272}
273
274static void b15_rac_resume(void)
275{
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.
280 */
281 __b15_rac_enable(rac_config0_reg);
282 clear_bit(RAC_SUSPENDED, &b15_rac_flags);
283}
284
285static struct syscore_ops b15_rac_syscore_ops = {
286 .suspend = b15_rac_suspend,
287 .resume = b15_rac_resume,
288};
289#endif
290
Florian Fainellif6f9be12017-12-01 01:10:09 +0100291static int __init b15_rac_init(void)
292{
293 struct device_node *dn;
294 int ret = 0, cpu;
295 u32 reg, en_mask = 0;
296
297 dn = of_find_compatible_node(NULL, NULL, "brcm,brcmstb-cpu-biu-ctrl");
298 if (!dn)
299 return -ENODEV;
300
301 if (WARN(num_possible_cpus() > 4, "RAC only supports 4 CPUs\n"))
302 goto out;
303
304 b15_rac_base = of_iomap(dn, 0);
305 if (!b15_rac_base) {
306 pr_err("failed to remap BIU control base\n");
307 ret = -ENOMEM;
308 goto out;
309 }
310
Florian Fainelli576a0862017-12-01 01:10:13 +0100311 ret = register_reboot_notifier(&b15_rac_reboot_nb);
312 if (ret) {
313 pr_err("failed to register reboot notifier\n");
314 iounmap(b15_rac_base);
315 goto out;
316 }
317
Florian Fainelli55de8872017-12-01 01:10:11 +0100318#ifdef CONFIG_HOTPLUG_CPU
319 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CACHE_B15_RAC_DEAD,
320 "arm/cache-b15-rac:dead",
321 NULL, b15_rac_dead_cpu);
322 if (ret)
323 goto out_unmap;
324
325 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CACHE_B15_RAC_DYING,
326 "arm/cache-b15-rac:dying",
327 NULL, b15_rac_dying_cpu);
328 if (ret)
329 goto out_cpu_dead;
330#endif
331
Florian Fainelli534f5f32017-12-01 01:10:12 +0100332#ifdef CONFIG_PM_SLEEP
333 register_syscore_ops(&b15_rac_syscore_ops);
334#endif
335
Florian Fainellif6f9be12017-12-01 01:10:09 +0100336 spin_lock(&rac_lock);
337 reg = __raw_readl(b15_rac_base + RAC_CONFIG0_REG);
338 for_each_possible_cpu(cpu)
339 en_mask |= ((1 << RACPREFDATA_SHIFT) << (cpu * RAC_CPU_SHIFT));
340 WARN(reg & en_mask, "Read-ahead cache not previously disabled\n");
341
342 b15_rac_enable();
343 set_bit(RAC_ENABLED, &b15_rac_flags);
344 spin_unlock(&rac_lock);
345
346 pr_info("Broadcom Brahma-B15 readahead cache at: 0x%p\n",
347 b15_rac_base + RAC_CONFIG0_REG);
348
Florian Fainelli55de8872017-12-01 01:10:11 +0100349 goto out;
350
351out_cpu_dead:
352 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CACHE_B15_RAC_DYING);
353out_unmap:
Florian Fainelli576a0862017-12-01 01:10:13 +0100354 unregister_reboot_notifier(&b15_rac_reboot_nb);
Florian Fainelli55de8872017-12-01 01:10:11 +0100355 iounmap(b15_rac_base);
Florian Fainellif6f9be12017-12-01 01:10:09 +0100356out:
357 of_node_put(dn);
358 return ret;
359}
360arch_initcall(b15_rac_init);