cache-b15-rac.c 10 KB

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