biuctrl.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Broadcom STB SoCs Bus Unit Interface controls
  4. *
  5. * Copyright (C) 2015, Broadcom Corporation
  6. */
  7. #define pr_fmt(fmt) "brcmstb: " KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/io.h>
  10. #include <linux/of_address.h>
  11. #include <linux/syscore_ops.h>
  12. #include <linux/soc/brcmstb/brcmstb.h>
  13. #define RACENPREF_MASK 0x3
  14. #define RACPREFINST_SHIFT 0
  15. #define RACENINST_SHIFT 2
  16. #define RACPREFDATA_SHIFT 4
  17. #define RACENDATA_SHIFT 6
  18. #define RAC_CPU_SHIFT 8
  19. #define RACCFG_MASK 0xff
  20. #define DPREF_LINE_2_SHIFT 24
  21. #define DPREF_LINE_2_MASK 0xff
  22. /* Bitmask to enable instruction and data prefetching with a 256-bytes stride */
  23. #define RAC_DATA_INST_EN_MASK (1 << RACPREFINST_SHIFT | \
  24. RACENPREF_MASK << RACENINST_SHIFT | \
  25. 1 << RACPREFDATA_SHIFT | \
  26. RACENPREF_MASK << RACENDATA_SHIFT)
  27. #define CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK 0x70000000
  28. #define CPU_CREDIT_REG_MCPx_READ_CRED_MASK 0xf
  29. #define CPU_CREDIT_REG_MCPx_WRITE_CRED_MASK 0xf
  30. #define CPU_CREDIT_REG_MCPx_READ_CRED_SHIFT(x) ((x) * 8)
  31. #define CPU_CREDIT_REG_MCPx_WRITE_CRED_SHIFT(x) (((x) * 8) + 4)
  32. #define CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_SHIFT(x) ((x) * 8)
  33. #define CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_MASK 0xff
  34. #define CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_THRESHOLD_MASK 0xf
  35. #define CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_MASK 0xf
  36. #define CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_SHIFT 4
  37. #define CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_ENABLE BIT(8)
  38. static void __iomem *cpubiuctrl_base;
  39. static bool mcp_wr_pairing_en;
  40. static const int *cpubiuctrl_regs;
  41. enum cpubiuctrl_regs {
  42. CPU_CREDIT_REG = 0,
  43. CPU_MCP_FLOW_REG,
  44. CPU_WRITEBACK_CTRL_REG,
  45. RAC_CONFIG0_REG,
  46. RAC_CONFIG1_REG,
  47. NUM_CPU_BIUCTRL_REGS,
  48. };
  49. static inline u32 cbc_readl(int reg)
  50. {
  51. int offset = cpubiuctrl_regs[reg];
  52. if (offset == -1 ||
  53. (IS_ENABLED(CONFIG_CACHE_B15_RAC) && reg >= RAC_CONFIG0_REG))
  54. return (u32)-1;
  55. return readl_relaxed(cpubiuctrl_base + offset);
  56. }
  57. static inline void cbc_writel(u32 val, int reg)
  58. {
  59. int offset = cpubiuctrl_regs[reg];
  60. if (offset == -1 ||
  61. (IS_ENABLED(CONFIG_CACHE_B15_RAC) && reg >= RAC_CONFIG0_REG))
  62. return;
  63. writel(val, cpubiuctrl_base + offset);
  64. }
  65. static const int b15_cpubiuctrl_regs[] = {
  66. [CPU_CREDIT_REG] = 0x184,
  67. [CPU_MCP_FLOW_REG] = -1,
  68. [CPU_WRITEBACK_CTRL_REG] = -1,
  69. [RAC_CONFIG0_REG] = -1,
  70. [RAC_CONFIG1_REG] = -1,
  71. };
  72. /* Odd cases, e.g: 7260A0 */
  73. static const int b53_cpubiuctrl_no_wb_regs[] = {
  74. [CPU_CREDIT_REG] = 0x0b0,
  75. [CPU_MCP_FLOW_REG] = 0x0b4,
  76. [CPU_WRITEBACK_CTRL_REG] = -1,
  77. [RAC_CONFIG0_REG] = 0x78,
  78. [RAC_CONFIG1_REG] = 0x7c,
  79. };
  80. static const int b53_cpubiuctrl_regs[] = {
  81. [CPU_CREDIT_REG] = 0x0b0,
  82. [CPU_MCP_FLOW_REG] = 0x0b4,
  83. [CPU_WRITEBACK_CTRL_REG] = 0x22c,
  84. [RAC_CONFIG0_REG] = 0x78,
  85. [RAC_CONFIG1_REG] = 0x7c,
  86. };
  87. static const int a72_cpubiuctrl_regs[] = {
  88. [CPU_CREDIT_REG] = 0x18,
  89. [CPU_MCP_FLOW_REG] = 0x1c,
  90. [CPU_WRITEBACK_CTRL_REG] = 0x20,
  91. [RAC_CONFIG0_REG] = 0x08,
  92. [RAC_CONFIG1_REG] = 0x0c,
  93. };
  94. static int __init mcp_write_pairing_set(void)
  95. {
  96. u32 creds = 0;
  97. if (!cpubiuctrl_base)
  98. return -1;
  99. creds = cbc_readl(CPU_CREDIT_REG);
  100. if (mcp_wr_pairing_en) {
  101. pr_info("MCP: Enabling write pairing\n");
  102. cbc_writel(creds | CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK,
  103. CPU_CREDIT_REG);
  104. } else if (creds & CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK) {
  105. pr_info("MCP: Disabling write pairing\n");
  106. cbc_writel(creds & ~CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK,
  107. CPU_CREDIT_REG);
  108. } else {
  109. pr_info("MCP: Write pairing already disabled\n");
  110. }
  111. return 0;
  112. }
  113. static const u32 a72_b53_mach_compat[] = {
  114. 0x7211,
  115. 0x72113,
  116. 0x72116,
  117. 0x7216,
  118. 0x72164,
  119. 0x72165,
  120. 0x7255,
  121. 0x7260,
  122. 0x7268,
  123. 0x7271,
  124. 0x7278,
  125. };
  126. /* The read-ahead cache present in the Brahma-B53 CPU is a special piece of
  127. * hardware after the integrated L2 cache of the B53 CPU complex whose purpose
  128. * is to prefetch instruction and/or data with a line size of either 64 bytes
  129. * or 256 bytes. The rationale is that the data-bus of the CPU interface is
  130. * optimized for 256-byte transactions, and enabling the read-ahead cache
  131. * provides a significant performance boost (typically twice the performance
  132. * for a memcpy benchmark application).
  133. *
  134. * The read-ahead cache is transparent for Virtual Address cache maintenance
  135. * operations: IC IVAU, DC IVAC, DC CVAC, DC CVAU and DC CIVAC. So no special
  136. * handling is needed for the DMA API above and beyond what is included in the
  137. * arm64 implementation.
  138. *
  139. * In addition, since the Point of Unification is typically between L1 and L2
  140. * for the Brahma-B53 processor no special read-ahead cache handling is needed
  141. * for the IC IALLU and IC IALLUIS cache maintenance operations.
  142. *
  143. * However, it is not possible to specify the cache level (L3) for the cache
  144. * maintenance instructions operating by set/way to operate on the read-ahead
  145. * cache. The read-ahead cache will maintain coherency when inner cache lines
  146. * are cleaned by set/way, but if it is necessary to invalidate inner cache
  147. * lines by set/way to maintain coherency with system masters operating on
  148. * shared memory that does not have hardware support for coherency, then it
  149. * will also be necessary to explicitly invalidate the read-ahead cache.
  150. */
  151. static void __init a72_b53_rac_enable_all(struct device_node *np)
  152. {
  153. unsigned int cpu;
  154. u32 enable = 0, pref_dist, shift;
  155. if (IS_ENABLED(CONFIG_CACHE_B15_RAC))
  156. return;
  157. if (WARN(num_possible_cpus() > 4, "RAC only supports 4 CPUs\n"))
  158. return;
  159. pref_dist = cbc_readl(RAC_CONFIG1_REG);
  160. for_each_possible_cpu(cpu) {
  161. shift = cpu * RAC_CPU_SHIFT + RACPREFDATA_SHIFT;
  162. enable |= RAC_DATA_INST_EN_MASK << (cpu * RAC_CPU_SHIFT);
  163. if (cpubiuctrl_regs == a72_cpubiuctrl_regs) {
  164. enable &= ~(RACENPREF_MASK << shift);
  165. enable |= 3 << shift;
  166. pref_dist |= 1 << (cpu + DPREF_LINE_2_SHIFT);
  167. }
  168. }
  169. cbc_writel(enable, RAC_CONFIG0_REG);
  170. cbc_writel(pref_dist, RAC_CONFIG1_REG);
  171. pr_info("%pOF: Broadcom %s read-ahead cache\n",
  172. np, cpubiuctrl_regs == a72_cpubiuctrl_regs ?
  173. "Cortex-A72" : "Brahma-B53");
  174. }
  175. static void __init mcp_a72_b53_set(void)
  176. {
  177. unsigned int i;
  178. u32 reg;
  179. reg = brcmstb_get_family_id();
  180. for (i = 0; i < ARRAY_SIZE(a72_b53_mach_compat); i++) {
  181. if (BRCM_ID(reg) == a72_b53_mach_compat[i])
  182. break;
  183. }
  184. if (i == ARRAY_SIZE(a72_b53_mach_compat))
  185. return;
  186. /* Set all 3 MCP interfaces to 8 credits */
  187. reg = cbc_readl(CPU_CREDIT_REG);
  188. for (i = 0; i < 3; i++) {
  189. reg &= ~(CPU_CREDIT_REG_MCPx_WRITE_CRED_MASK <<
  190. CPU_CREDIT_REG_MCPx_WRITE_CRED_SHIFT(i));
  191. reg &= ~(CPU_CREDIT_REG_MCPx_READ_CRED_MASK <<
  192. CPU_CREDIT_REG_MCPx_READ_CRED_SHIFT(i));
  193. reg |= 8 << CPU_CREDIT_REG_MCPx_WRITE_CRED_SHIFT(i);
  194. reg |= 8 << CPU_CREDIT_REG_MCPx_READ_CRED_SHIFT(i);
  195. }
  196. cbc_writel(reg, CPU_CREDIT_REG);
  197. /* Max out the number of in-flight Jwords reads on the MCP interface */
  198. reg = cbc_readl(CPU_MCP_FLOW_REG);
  199. for (i = 0; i < 3; i++)
  200. reg |= CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_MASK <<
  201. CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_SHIFT(i);
  202. cbc_writel(reg, CPU_MCP_FLOW_REG);
  203. /* Enable writeback throttling, set timeout to 128 cycles, 256 cycles
  204. * threshold
  205. */
  206. reg = cbc_readl(CPU_WRITEBACK_CTRL_REG);
  207. reg |= CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_ENABLE;
  208. reg &= ~CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_THRESHOLD_MASK;
  209. reg &= ~(CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_MASK <<
  210. CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_SHIFT);
  211. reg |= 8;
  212. reg |= 7 << CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_SHIFT;
  213. cbc_writel(reg, CPU_WRITEBACK_CTRL_REG);
  214. }
  215. static int __init setup_hifcpubiuctrl_regs(struct device_node *np)
  216. {
  217. struct device_node *cpu_dn;
  218. u32 family_id;
  219. int ret = 0;
  220. cpubiuctrl_base = of_iomap(np, 0);
  221. if (!cpubiuctrl_base) {
  222. pr_err("failed to remap BIU control base\n");
  223. ret = -ENOMEM;
  224. goto out;
  225. }
  226. mcp_wr_pairing_en = of_property_read_bool(np, "brcm,write-pairing");
  227. cpu_dn = of_get_cpu_node(0, NULL);
  228. if (!cpu_dn) {
  229. pr_err("failed to obtain CPU device node\n");
  230. ret = -ENODEV;
  231. goto out;
  232. }
  233. if (of_device_is_compatible(cpu_dn, "brcm,brahma-b15"))
  234. cpubiuctrl_regs = b15_cpubiuctrl_regs;
  235. else if (of_device_is_compatible(cpu_dn, "brcm,brahma-b53"))
  236. cpubiuctrl_regs = b53_cpubiuctrl_regs;
  237. else if (of_device_is_compatible(cpu_dn, "arm,cortex-a72"))
  238. cpubiuctrl_regs = a72_cpubiuctrl_regs;
  239. else {
  240. pr_err("unsupported CPU\n");
  241. ret = -EINVAL;
  242. }
  243. of_node_put(cpu_dn);
  244. family_id = brcmstb_get_family_id();
  245. if (BRCM_ID(family_id) == 0x7260 && BRCM_REV(family_id) == 0)
  246. cpubiuctrl_regs = b53_cpubiuctrl_no_wb_regs;
  247. out:
  248. if (ret && cpubiuctrl_base) {
  249. iounmap(cpubiuctrl_base);
  250. cpubiuctrl_base = NULL;
  251. }
  252. return ret;
  253. }
  254. #ifdef CONFIG_PM_SLEEP
  255. static u32 cpubiuctrl_reg_save[NUM_CPU_BIUCTRL_REGS];
  256. static int brcmstb_cpu_credit_reg_suspend(void)
  257. {
  258. unsigned int i;
  259. if (!cpubiuctrl_base)
  260. return 0;
  261. for (i = 0; i < NUM_CPU_BIUCTRL_REGS; i++)
  262. cpubiuctrl_reg_save[i] = cbc_readl(i);
  263. return 0;
  264. }
  265. static void brcmstb_cpu_credit_reg_resume(void)
  266. {
  267. unsigned int i;
  268. if (!cpubiuctrl_base)
  269. return;
  270. for (i = 0; i < NUM_CPU_BIUCTRL_REGS; i++)
  271. cbc_writel(cpubiuctrl_reg_save[i], i);
  272. }
  273. static struct syscore_ops brcmstb_cpu_credit_syscore_ops = {
  274. .suspend = brcmstb_cpu_credit_reg_suspend,
  275. .resume = brcmstb_cpu_credit_reg_resume,
  276. };
  277. #endif
  278. static int __init brcmstb_biuctrl_init(void)
  279. {
  280. struct device_node *np;
  281. int ret;
  282. /* We might be running on a multi-platform kernel, don't make this a
  283. * fatal error, just bail out early
  284. */
  285. np = of_find_compatible_node(NULL, NULL, "brcm,brcmstb-cpu-biu-ctrl");
  286. if (!np)
  287. return 0;
  288. ret = setup_hifcpubiuctrl_regs(np);
  289. if (ret)
  290. goto out_put;
  291. ret = mcp_write_pairing_set();
  292. if (ret) {
  293. pr_err("MCP: Unable to disable write pairing!\n");
  294. goto out_put;
  295. }
  296. a72_b53_rac_enable_all(np);
  297. mcp_a72_b53_set();
  298. #ifdef CONFIG_PM_SLEEP
  299. register_syscore_ops(&brcmstb_cpu_credit_syscore_ops);
  300. #endif
  301. ret = 0;
  302. out_put:
  303. of_node_put(np);
  304. return ret;
  305. }
  306. early_initcall(brcmstb_biuctrl_init);