PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/mach-mvebu/coherency.c

https://github.com/othane/linux
C | 317 lines | 186 code | 55 blank | 76 comment | 26 complexity | fe600044efcb2194bb0378a5466d4e31 MD5 | raw file
  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_shared_l2_notifier_func(struct notifier_block *nfb,
  98. unsigned long action, void *hcpu)
  99. {
  100. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  101. armada_xp_clear_shared_l2();
  102. return NOTIFY_OK;
  103. }
  104. static struct notifier_block armada_xp_clear_shared_l2_notifier = {
  105. .notifier_call = armada_xp_clear_shared_l2_notifier_func,
  106. .priority = 100,
  107. };
  108. static void __init armada_370_coherency_init(struct device_node *np)
  109. {
  110. struct resource res;
  111. struct device_node *cpu_config_np;
  112. of_address_to_resource(np, 0, &res);
  113. coherency_phys_base = res.start;
  114. /*
  115. * Ensure secondary CPUs will see the updated value,
  116. * which they read before they join the coherency
  117. * fabric, and therefore before they are coherent with
  118. * the boot CPU cache.
  119. */
  120. sync_cache_w(&coherency_phys_base);
  121. coherency_base = of_iomap(np, 0);
  122. coherency_cpu_base = of_iomap(np, 1);
  123. cpu_config_np = of_find_compatible_node(NULL, NULL,
  124. "marvell,armada-xp-cpu-config");
  125. if (!cpu_config_np)
  126. goto exit;
  127. cpu_config_base = of_iomap(cpu_config_np, 0);
  128. if (!cpu_config_base) {
  129. of_node_put(cpu_config_np);
  130. goto exit;
  131. }
  132. of_node_put(cpu_config_np);
  133. register_cpu_notifier(&armada_xp_clear_shared_l2_notifier);
  134. exit:
  135. set_cpu_coherent();
  136. }
  137. /*
  138. * This ioremap hook is used on Armada 375/38x to ensure that PCIe
  139. * memory areas are mapped as MT_UNCACHED instead of MT_DEVICE. This
  140. * is needed as a workaround for a deadlock issue between the PCIe
  141. * interface and the cache controller.
  142. */
  143. static void __iomem *
  144. armada_pcie_wa_ioremap_caller(phys_addr_t phys_addr, size_t size,
  145. unsigned int mtype, void *caller)
  146. {
  147. struct resource pcie_mem;
  148. mvebu_mbus_get_pcie_mem_aperture(&pcie_mem);
  149. if (pcie_mem.start <= phys_addr && (phys_addr + size) <= pcie_mem.end)
  150. mtype = MT_UNCACHED;
  151. return __arm_ioremap_caller(phys_addr, size, mtype, caller);
  152. }
  153. static void __init armada_375_380_coherency_init(struct device_node *np)
  154. {
  155. struct device_node *cache_dn;
  156. coherency_cpu_base = of_iomap(np, 0);
  157. arch_ioremap_caller = armada_pcie_wa_ioremap_caller;
  158. /*
  159. * We should switch the PL310 to I/O coherency mode only if
  160. * I/O coherency is actually enabled.
  161. */
  162. if (!coherency_available())
  163. return;
  164. /*
  165. * Add the PL310 property "arm,io-coherent". This makes sure the
  166. * outer sync operation is not used, which allows to
  167. * workaround the system erratum that causes deadlocks when
  168. * doing PCIe in an SMP situation on Armada 375 and Armada
  169. * 38x.
  170. */
  171. for_each_compatible_node(cache_dn, NULL, "arm,pl310-cache") {
  172. struct property *p;
  173. p = kzalloc(sizeof(*p), GFP_KERNEL);
  174. p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
  175. of_add_property(cache_dn, p);
  176. }
  177. }
  178. static int coherency_type(void)
  179. {
  180. struct device_node *np;
  181. const struct of_device_id *match;
  182. int type;
  183. /*
  184. * The coherency fabric is needed:
  185. * - For coherency between processors on Armada XP, so only
  186. * when SMP is enabled.
  187. * - For coherency between the processor and I/O devices, but
  188. * this coherency requires many pre-requisites (write
  189. * allocate cache policy, shareable pages, SMP bit set) that
  190. * are only meant in SMP situations.
  191. *
  192. * Note that this means that on Armada 370, there is currently
  193. * no way to use hardware I/O coherency, because even when
  194. * CONFIG_SMP is enabled, is_smp() returns false due to the
  195. * Armada 370 being a single-core processor. To lift this
  196. * limitation, we would have to find a way to make the cache
  197. * policy set to write-allocate (on all Armada SoCs), and to
  198. * set the shareable attribute in page tables (on all Armada
  199. * SoCs except the Armada 370). Unfortunately, such decisions
  200. * are taken very early in the kernel boot process, at a point
  201. * where we don't know yet on which SoC we are running.
  202. */
  203. if (!is_smp())
  204. return COHERENCY_FABRIC_TYPE_NONE;
  205. np = of_find_matching_node_and_match(NULL, of_coherency_table, &match);
  206. if (!np)
  207. return COHERENCY_FABRIC_TYPE_NONE;
  208. type = (int) match->data;
  209. of_node_put(np);
  210. return type;
  211. }
  212. int set_cpu_coherent(void)
  213. {
  214. int type = coherency_type();
  215. if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP) {
  216. if (!coherency_base) {
  217. pr_warn("Can't make current CPU cache coherent.\n");
  218. pr_warn("Coherency fabric is not initialized\n");
  219. return 1;
  220. }
  221. armada_xp_clear_shared_l2();
  222. ll_add_cpu_to_smp_group();
  223. return ll_enable_coherency();
  224. }
  225. return 0;
  226. }
  227. int coherency_available(void)
  228. {
  229. return coherency_type() != COHERENCY_FABRIC_TYPE_NONE;
  230. }
  231. int __init coherency_init(void)
  232. {
  233. int type = coherency_type();
  234. struct device_node *np;
  235. np = of_find_matching_node(NULL, of_coherency_table);
  236. if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
  237. armada_370_coherency_init(np);
  238. else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375 ||
  239. type == COHERENCY_FABRIC_TYPE_ARMADA_380)
  240. armada_375_380_coherency_init(np);
  241. of_node_put(np);
  242. return 0;
  243. }
  244. static int __init coherency_late_init(void)
  245. {
  246. if (coherency_available())
  247. bus_register_notifier(&platform_bus_type,
  248. &mvebu_hwcc_nb);
  249. return 0;
  250. }
  251. postcore_initcall(coherency_late_init);
  252. #if IS_ENABLED(CONFIG_PCI)
  253. static int __init coherency_pci_init(void)
  254. {
  255. if (coherency_available())
  256. bus_register_notifier(&pci_bus_type,
  257. &mvebu_hwcc_pci_nb);
  258. return 0;
  259. }
  260. arch_initcall(coherency_pci_init);
  261. #endif