coherency.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Coherency fabric (Aurora) support for Armada 370, 375, 38x and XP
  4. * platforms.
  5. *
  6. * Copyright (C) 2012 Marvell
  7. *
  8. * Yehuda Yitschak <[email protected]>
  9. * Gregory Clement <[email protected]>
  10. * Thomas Petazzoni <[email protected]>
  11. *
  12. * The Armada 370, 375, 38x and XP SOCs have a coherency fabric which is
  13. * responsible for ensuring hardware coherency between all CPUs and between
  14. * CPUs and I/O masters. This file initializes the coherency fabric and
  15. * supplies basic routines for configuring and controlling hardware coherency
  16. */
  17. #define pr_fmt(fmt) "mvebu-coherency: " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/of_address.h>
  21. #include <linux/io.h>
  22. #include <linux/smp.h>
  23. #include <linux/dma-map-ops.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/mbus.h>
  27. #include <linux/pci.h>
  28. #include <asm/smp_plat.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/mach/map.h>
  31. #include <asm/dma-mapping.h>
  32. #include "coherency.h"
  33. #include "mvebu-soc-id.h"
  34. unsigned long coherency_phys_base;
  35. void __iomem *coherency_base;
  36. static void __iomem *coherency_cpu_base;
  37. static void __iomem *cpu_config_base;
  38. /* Coherency fabric registers */
  39. #define IO_SYNC_BARRIER_CTL_OFFSET 0x0
  40. enum {
  41. COHERENCY_FABRIC_TYPE_NONE,
  42. COHERENCY_FABRIC_TYPE_ARMADA_370_XP,
  43. COHERENCY_FABRIC_TYPE_ARMADA_375,
  44. COHERENCY_FABRIC_TYPE_ARMADA_380,
  45. };
  46. static const struct of_device_id of_coherency_table[] = {
  47. {.compatible = "marvell,coherency-fabric",
  48. .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_370_XP },
  49. {.compatible = "marvell,armada-375-coherency-fabric",
  50. .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_375 },
  51. {.compatible = "marvell,armada-380-coherency-fabric",
  52. .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_380 },
  53. { /* end of list */ },
  54. };
  55. /* Functions defined in coherency_ll.S */
  56. int ll_enable_coherency(void);
  57. void ll_add_cpu_to_smp_group(void);
  58. #define CPU_CONFIG_SHARED_L2 BIT(16)
  59. /*
  60. * Disable the "Shared L2 Present" bit in CPU Configuration register
  61. * on Armada XP.
  62. *
  63. * The "Shared L2 Present" bit affects the "level of coherence" value
  64. * in the clidr CP15 register. Cache operation functions such as
  65. * "flush all" and "invalidate all" operate on all the cache levels
  66. * that included in the defined level of coherence. When HW I/O
  67. * coherency is used, this bit causes unnecessary flushes of the L2
  68. * cache.
  69. */
  70. static void armada_xp_clear_shared_l2(void)
  71. {
  72. u32 reg;
  73. if (!cpu_config_base)
  74. return;
  75. reg = readl(cpu_config_base);
  76. reg &= ~CPU_CONFIG_SHARED_L2;
  77. writel(reg, cpu_config_base);
  78. }
  79. static int mvebu_hwcc_notifier(struct notifier_block *nb,
  80. unsigned long event, void *__dev)
  81. {
  82. struct device *dev = __dev;
  83. if (event != BUS_NOTIFY_ADD_DEVICE)
  84. return NOTIFY_DONE;
  85. dev->dma_coherent = true;
  86. return NOTIFY_OK;
  87. }
  88. static struct notifier_block mvebu_hwcc_nb = {
  89. .notifier_call = mvebu_hwcc_notifier,
  90. };
  91. static struct notifier_block mvebu_hwcc_pci_nb __maybe_unused = {
  92. .notifier_call = mvebu_hwcc_notifier,
  93. };
  94. static int armada_xp_clear_l2_starting(unsigned int cpu)
  95. {
  96. armada_xp_clear_shared_l2();
  97. return 0;
  98. }
  99. static void __init armada_370_coherency_init(struct device_node *np)
  100. {
  101. struct resource res;
  102. struct device_node *cpu_config_np;
  103. of_address_to_resource(np, 0, &res);
  104. coherency_phys_base = res.start;
  105. /*
  106. * Ensure secondary CPUs will see the updated value,
  107. * which they read before they join the coherency
  108. * fabric, and therefore before they are coherent with
  109. * the boot CPU cache.
  110. */
  111. sync_cache_w(&coherency_phys_base);
  112. coherency_base = of_iomap(np, 0);
  113. coherency_cpu_base = of_iomap(np, 1);
  114. cpu_config_np = of_find_compatible_node(NULL, NULL,
  115. "marvell,armada-xp-cpu-config");
  116. if (!cpu_config_np)
  117. goto exit;
  118. cpu_config_base = of_iomap(cpu_config_np, 0);
  119. if (!cpu_config_base) {
  120. of_node_put(cpu_config_np);
  121. goto exit;
  122. }
  123. of_node_put(cpu_config_np);
  124. cpuhp_setup_state_nocalls(CPUHP_AP_ARM_MVEBU_COHERENCY,
  125. "arm/mvebu/coherency:starting",
  126. armada_xp_clear_l2_starting, NULL);
  127. exit:
  128. set_cpu_coherent();
  129. }
  130. /*
  131. * This ioremap hook is used on Armada 375/38x to ensure that all MMIO
  132. * areas are mapped as MT_UNCACHED instead of MT_DEVICE. This is
  133. * needed for the HW I/O coherency mechanism to work properly without
  134. * deadlock.
  135. */
  136. static void __iomem *
  137. armada_wa_ioremap_caller(phys_addr_t phys_addr, size_t size,
  138. unsigned int mtype, void *caller)
  139. {
  140. mtype = MT_UNCACHED;
  141. return __arm_ioremap_caller(phys_addr, size, mtype, caller);
  142. }
  143. static void __init armada_375_380_coherency_init(struct device_node *np)
  144. {
  145. struct device_node *cache_dn;
  146. coherency_cpu_base = of_iomap(np, 0);
  147. arch_ioremap_caller = armada_wa_ioremap_caller;
  148. pci_ioremap_set_mem_type(MT_UNCACHED);
  149. /*
  150. * We should switch the PL310 to I/O coherency mode only if
  151. * I/O coherency is actually enabled.
  152. */
  153. if (!coherency_available())
  154. return;
  155. /*
  156. * Add the PL310 property "arm,io-coherent". This makes sure the
  157. * outer sync operation is not used, which allows to
  158. * workaround the system erratum that causes deadlocks when
  159. * doing PCIe in an SMP situation on Armada 375 and Armada
  160. * 38x.
  161. */
  162. for_each_compatible_node(cache_dn, NULL, "arm,pl310-cache") {
  163. struct property *p;
  164. p = kzalloc(sizeof(*p), GFP_KERNEL);
  165. p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
  166. of_add_property(cache_dn, p);
  167. }
  168. }
  169. static int coherency_type(void)
  170. {
  171. struct device_node *np;
  172. const struct of_device_id *match;
  173. int type;
  174. /*
  175. * The coherency fabric is needed:
  176. * - For coherency between processors on Armada XP, so only
  177. * when SMP is enabled.
  178. * - For coherency between the processor and I/O devices, but
  179. * this coherency requires many pre-requisites (write
  180. * allocate cache policy, shareable pages, SMP bit set) that
  181. * are only meant in SMP situations.
  182. *
  183. * Note that this means that on Armada 370, there is currently
  184. * no way to use hardware I/O coherency, because even when
  185. * CONFIG_SMP is enabled, is_smp() returns false due to the
  186. * Armada 370 being a single-core processor. To lift this
  187. * limitation, we would have to find a way to make the cache
  188. * policy set to write-allocate (on all Armada SoCs), and to
  189. * set the shareable attribute in page tables (on all Armada
  190. * SoCs except the Armada 370). Unfortunately, such decisions
  191. * are taken very early in the kernel boot process, at a point
  192. * where we don't know yet on which SoC we are running.
  193. */
  194. if (!is_smp())
  195. return COHERENCY_FABRIC_TYPE_NONE;
  196. np = of_find_matching_node_and_match(NULL, of_coherency_table, &match);
  197. if (!np)
  198. return COHERENCY_FABRIC_TYPE_NONE;
  199. type = (int) match->data;
  200. of_node_put(np);
  201. return type;
  202. }
  203. int set_cpu_coherent(void)
  204. {
  205. int type = coherency_type();
  206. if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP) {
  207. if (!coherency_base) {
  208. pr_warn("Can't make current CPU cache coherent.\n");
  209. pr_warn("Coherency fabric is not initialized\n");
  210. return 1;
  211. }
  212. armada_xp_clear_shared_l2();
  213. ll_add_cpu_to_smp_group();
  214. return ll_enable_coherency();
  215. }
  216. return 0;
  217. }
  218. int coherency_available(void)
  219. {
  220. return coherency_type() != COHERENCY_FABRIC_TYPE_NONE;
  221. }
  222. int __init coherency_init(void)
  223. {
  224. int type = coherency_type();
  225. struct device_node *np;
  226. np = of_find_matching_node(NULL, of_coherency_table);
  227. if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
  228. armada_370_coherency_init(np);
  229. else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375 ||
  230. type == COHERENCY_FABRIC_TYPE_ARMADA_380)
  231. armada_375_380_coherency_init(np);
  232. of_node_put(np);
  233. return 0;
  234. }
  235. static int __init coherency_late_init(void)
  236. {
  237. if (coherency_available())
  238. bus_register_notifier(&platform_bus_type,
  239. &mvebu_hwcc_nb);
  240. return 0;
  241. }
  242. postcore_initcall(coherency_late_init);
  243. #if IS_ENABLED(CONFIG_PCI)
  244. static int __init coherency_pci_init(void)
  245. {
  246. if (coherency_available())
  247. bus_register_notifier(&pci_bus_type,
  248. &mvebu_hwcc_pci_nb);
  249. return 0;
  250. }
  251. arch_initcall(coherency_pci_init);
  252. #endif