coherency.c 8.0 KB

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