blob: e80f0dde218919dab8d7a2b5873a4962755f3ee2 [file] [log] [blame]
Gregory CLEMENT009f1312012-08-02 11:16:29 +03001/*
Thomas Petazzonie12f12a2014-11-13 10:39:00 +01002 * Coherency fabric (Aurora) support for Armada 370, 375, 38x and XP
3 * platforms.
Gregory CLEMENT009f1312012-08-02 11:16:29 +03004 *
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 *
Thomas Petazzonie12f12a2014-11-13 10:39:00 +010015 * The Armada 370, 375, 38x and XP SOCs have a coherency fabric which is
Gregory CLEMENT009f1312012-08-02 11:16:29 +030016 * 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
Thomas Petazzoni5ab5afd2014-04-14 15:47:05 +020021#define pr_fmt(fmt) "mvebu-coherency: " fmt
22
Gregory CLEMENT009f1312012-08-02 11:16:29 +030023#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/of_address.h>
26#include <linux/io.h>
27#include <linux/smp.h>
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020028#include <linux/dma-mapping.h>
29#include <linux/platform_device.h>
Thomas Petazzoni5ab5afd2014-04-14 15:47:05 +020030#include <linux/slab.h>
31#include <linux/mbus.h>
Thomas Petazzonib0063aa2014-05-13 18:04:30 +020032#include <linux/pci.h>
Gregory CLEMENT009f1312012-08-02 11:16:29 +030033#include <asm/smp_plat.h>
Thomas Petazzoni580ff0e2013-06-06 12:24:28 +020034#include <asm/cacheflush.h>
Thomas Petazzoni497a9232014-05-15 16:59:34 +020035#include <asm/mach/map.h>
Thomas Petazzoni1bd4d8a2015-01-16 17:11:29 +010036#include <asm/dma-mapping.h>
Jisheng Zhangb12634e2013-11-07 17:02:38 +080037#include "coherency.h"
Thomas Petazzoni39438562014-05-05 17:05:26 +020038#include "mvebu-soc-id.h"
Gregory CLEMENT009f1312012-08-02 11:16:29 +030039
Paul Gortmaker8bd26e32013-06-17 15:43:14 -040040unsigned long coherency_phys_base;
Gregory CLEMENTccd6a132014-04-14 17:10:05 +020041void __iomem *coherency_base;
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020042static void __iomem *coherency_cpu_base;
Thomas Petazzonid492ccc2015-07-08 16:09:20 +020043static void __iomem *cpu_config_base;
Gregory CLEMENT009f1312012-08-02 11:16:29 +030044
45/* Coherency fabric registers */
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020046#define IO_SYNC_BARRIER_CTL_OFFSET 0x0
47
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020048enum {
Thomas Petazzoni501f9282014-04-14 15:47:00 +020049 COHERENCY_FABRIC_TYPE_NONE,
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020050 COHERENCY_FABRIC_TYPE_ARMADA_370_XP,
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +020051 COHERENCY_FABRIC_TYPE_ARMADA_375,
Thomas Petazzonid0de9322014-04-14 15:47:06 +020052 COHERENCY_FABRIC_TYPE_ARMADA_380,
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020053};
54
Uwe Kleine-König444d2d32015-02-18 21:19:56 +010055static const struct of_device_id of_coherency_table[] = {
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020056 {.compatible = "marvell,coherency-fabric",
57 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_370_XP },
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +020058 {.compatible = "marvell,armada-375-coherency-fabric",
59 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_375 },
Thomas Petazzonid0de9322014-04-14 15:47:06 +020060 {.compatible = "marvell,armada-380-coherency-fabric",
61 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_380 },
Gregory CLEMENT009f1312012-08-02 11:16:29 +030062 { /* end of list */ },
63};
64
Gregory CLEMENT2e8a5942014-04-14 17:10:08 +020065/* Functions defined in coherency_ll.S */
66int ll_enable_coherency(void);
67void ll_add_cpu_to_smp_group(void);
Gregory CLEMENT009f1312012-08-02 11:16:29 +030068
Thomas Petazzonid492ccc2015-07-08 16:09:20 +020069#define CPU_CONFIG_SHARED_L2 BIT(16)
70
71/*
72 * Disable the "Shared L2 Present" bit in CPU Configuration register
73 * on Armada XP.
74 *
75 * The "Shared L2 Present" bit affects the "level of coherence" value
76 * in the clidr CP15 register. Cache operation functions such as
77 * "flush all" and "invalidate all" operate on all the cache levels
78 * that included in the defined level of coherence. When HW I/O
79 * coherency is used, this bit causes unnecessary flushes of the L2
80 * cache.
81 */
82static void armada_xp_clear_shared_l2(void)
83{
84 u32 reg;
85
86 if (!cpu_config_base)
87 return;
88
89 reg = readl(cpu_config_base);
90 reg &= ~CPU_CONFIG_SHARED_L2;
91 writel(reg, cpu_config_base);
92}
93
Thomas Petazzonib0063aa2014-05-13 18:04:30 +020094static int mvebu_hwcc_notifier(struct notifier_block *nb,
95 unsigned long event, void *__dev)
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020096{
97 struct device *dev = __dev;
98
99 if (event != BUS_NOTIFY_ADD_DEVICE)
100 return NOTIFY_DONE;
Thomas Petazzoni1bd4d8a2015-01-16 17:11:29 +0100101 set_dma_ops(dev, &arm_coherent_dma_ops);
Gregory CLEMENTe60304f2012-10-12 19:20:36 +0200102
103 return NOTIFY_OK;
104}
105
Thomas Petazzonib0063aa2014-05-13 18:04:30 +0200106static struct notifier_block mvebu_hwcc_nb = {
107 .notifier_call = mvebu_hwcc_notifier,
Gregory CLEMENTe60304f2012-10-12 19:20:36 +0200108};
109
Arnd Bergmann60f23952016-02-23 15:06:39 +0100110static struct notifier_block mvebu_hwcc_pci_nb __maybe_unused = {
Ezequiel Garciaa728b972014-07-08 10:37:37 -0300111 .notifier_call = mvebu_hwcc_notifier,
112};
113
Thomas Petazzonid492ccc2015-07-08 16:09:20 +0200114static int armada_xp_clear_shared_l2_notifier_func(struct notifier_block *nfb,
115 unsigned long action, void *hcpu)
116{
117 if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
118 armada_xp_clear_shared_l2();
119
120 return NOTIFY_OK;
121}
122
123static struct notifier_block armada_xp_clear_shared_l2_notifier = {
124 .notifier_call = armada_xp_clear_shared_l2_notifier_func,
125 .priority = 100,
126};
127
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200128static void __init armada_370_coherency_init(struct device_node *np)
129{
130 struct resource res;
Thomas Petazzonid492ccc2015-07-08 16:09:20 +0200131 struct device_node *cpu_config_np;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200132
133 of_address_to_resource(np, 0, &res);
134 coherency_phys_base = res.start;
135 /*
136 * Ensure secondary CPUs will see the updated value,
137 * which they read before they join the coherency
138 * fabric, and therefore before they are coherent with
139 * the boot CPU cache.
140 */
141 sync_cache_w(&coherency_phys_base);
142 coherency_base = of_iomap(np, 0);
143 coherency_cpu_base = of_iomap(np, 1);
Thomas Petazzonid492ccc2015-07-08 16:09:20 +0200144
145 cpu_config_np = of_find_compatible_node(NULL, NULL,
146 "marvell,armada-xp-cpu-config");
147 if (!cpu_config_np)
148 goto exit;
149
150 cpu_config_base = of_iomap(cpu_config_np, 0);
151 if (!cpu_config_base) {
152 of_node_put(cpu_config_np);
153 goto exit;
154 }
155
156 of_node_put(cpu_config_np);
157
158 register_cpu_notifier(&armada_xp_clear_shared_l2_notifier);
159
160exit:
Gregory CLEMENT952f4ca2014-04-14 17:10:07 +0200161 set_cpu_coherent();
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200162}
163
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200164/*
Thomas Petazzonic5379ba2016-06-16 15:42:25 +0200165 * This ioremap hook is used on Armada 375/38x to ensure that all MMIO
166 * areas are mapped as MT_UNCACHED instead of MT_DEVICE. This is
167 * needed for the HW I/O coherency mechanism to work properly without
168 * deadlock.
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200169 */
170static void __iomem *
Thomas Petazzonic5379ba2016-06-16 15:42:25 +0200171armada_wa_ioremap_caller(phys_addr_t phys_addr, size_t size,
172 unsigned int mtype, void *caller)
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200173{
Thomas Petazzonic5379ba2016-06-16 15:42:25 +0200174 mtype = MT_UNCACHED;
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200175 return __arm_ioremap_caller(phys_addr, size, mtype, caller);
176}
177
Thomas Petazzonid0de9322014-04-14 15:47:06 +0200178static void __init armada_375_380_coherency_init(struct device_node *np)
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200179{
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200180 struct device_node *cache_dn;
181
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200182 coherency_cpu_base = of_iomap(np, 0);
Thomas Petazzonic5379ba2016-06-16 15:42:25 +0200183 arch_ioremap_caller = armada_wa_ioremap_caller;
Thomas Petazzoni6a027342016-06-16 15:42:26 +0200184 pci_ioremap_set_mem_type(MT_UNCACHED);
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200185
186 /*
Thomas Petazzonidcad6882015-01-28 12:55:45 +0100187 * We should switch the PL310 to I/O coherency mode only if
188 * I/O coherency is actually enabled.
189 */
190 if (!coherency_available())
191 return;
192
193 /*
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200194 * Add the PL310 property "arm,io-coherent". This makes sure the
195 * outer sync operation is not used, which allows to
196 * workaround the system erratum that causes deadlocks when
197 * doing PCIe in an SMP situation on Armada 375 and Armada
198 * 38x.
199 */
200 for_each_compatible_node(cache_dn, NULL, "arm,pl310-cache") {
201 struct property *p;
202
203 p = kzalloc(sizeof(*p), GFP_KERNEL);
204 p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
205 of_add_property(cache_dn, p);
206 }
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200207}
208
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200209static int coherency_type(void)
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300210{
211 struct device_node *np;
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200212 const struct of_device_id *match;
Thomas Petazzonie5535542014-11-13 10:38:57 +0100213 int type;
214
215 /*
216 * The coherency fabric is needed:
217 * - For coherency between processors on Armada XP, so only
218 * when SMP is enabled.
219 * - For coherency between the processor and I/O devices, but
220 * this coherency requires many pre-requisites (write
221 * allocate cache policy, shareable pages, SMP bit set) that
222 * are only meant in SMP situations.
223 *
224 * Note that this means that on Armada 370, there is currently
225 * no way to use hardware I/O coherency, because even when
226 * CONFIG_SMP is enabled, is_smp() returns false due to the
227 * Armada 370 being a single-core processor. To lift this
228 * limitation, we would have to find a way to make the cache
229 * policy set to write-allocate (on all Armada SoCs), and to
230 * set the shareable attribute in page tables (on all Armada
231 * SoCs except the Armada 370). Unfortunately, such decisions
232 * are taken very early in the kernel boot process, at a point
233 * where we don't know yet on which SoC we are running.
234
235 */
236 if (!is_smp())
237 return COHERENCY_FABRIC_TYPE_NONE;
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300238
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200239 np = of_find_matching_node_and_match(NULL, of_coherency_table, &match);
Thomas Petazzonie5535542014-11-13 10:38:57 +0100240 if (!np)
241 return COHERENCY_FABRIC_TYPE_NONE;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200242
Thomas Petazzonie5535542014-11-13 10:38:57 +0100243 type = (int) match->data;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200244
Thomas Petazzonie5535542014-11-13 10:38:57 +0100245 of_node_put(np);
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200246
Thomas Petazzonie5535542014-11-13 10:38:57 +0100247 return type;
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200248}
249
Nadav Haklai01049a52015-07-08 17:02:30 +0200250int set_cpu_coherent(void)
251{
252 int type = coherency_type();
253
254 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP) {
255 if (!coherency_base) {
256 pr_warn("Can't make current CPU cache coherent.\n");
257 pr_warn("Coherency fabric is not initialized\n");
258 return 1;
259 }
Thomas Petazzonid492ccc2015-07-08 16:09:20 +0200260
261 armada_xp_clear_shared_l2();
Nadav Haklai01049a52015-07-08 17:02:30 +0200262 ll_add_cpu_to_smp_group();
263 return ll_enable_coherency();
264 }
265
266 return 0;
267}
268
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200269int coherency_available(void)
270{
Thomas Petazzoni1bd4d8a2015-01-16 17:11:29 +0100271 return coherency_type() != COHERENCY_FABRIC_TYPE_NONE;
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200272}
273
274int __init coherency_init(void)
275{
276 int type = coherency_type();
277 struct device_node *np;
278
279 np = of_find_matching_node(NULL, of_coherency_table);
280
281 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
282 armada_370_coherency_init(np);
Thomas Petazzonid0de9322014-04-14 15:47:06 +0200283 else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375 ||
284 type == COHERENCY_FABRIC_TYPE_ARMADA_380)
285 armada_375_380_coherency_init(np);
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200286
Thomas Petazzoni2eb04ae2014-10-27 16:32:35 +0100287 of_node_put(np);
288
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300289 return 0;
290}
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200291
292static int __init coherency_late_init(void)
293{
Thomas Petazzonief01c6c2014-11-13 10:38:59 +0100294 if (coherency_available())
295 bus_register_notifier(&platform_bus_type,
296 &mvebu_hwcc_nb);
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200297 return 0;
298}
299
300postcore_initcall(coherency_late_init);
Thomas Petazzonib0063aa2014-05-13 18:04:30 +0200301
Thomas Petazzoni8828ccc2014-05-20 17:13:03 +0200302#if IS_ENABLED(CONFIG_PCI)
Thomas Petazzonib0063aa2014-05-13 18:04:30 +0200303static int __init coherency_pci_init(void)
304{
305 if (coherency_available())
306 bus_register_notifier(&pci_bus_type,
Ezequiel Garciaa728b972014-07-08 10:37:37 -0300307 &mvebu_hwcc_pci_nb);
Thomas Petazzonib0063aa2014-05-13 18:04:30 +0200308 return 0;
309}
310
311arch_initcall(coherency_pci_init);
Thomas Petazzoni8828ccc2014-05-20 17:13:03 +0200312#endif