xref: /openbmc/linux/arch/arm/mach-mvebu/coherency.c (revision 60f23952)
1009f1315SGregory CLEMENT /*
2e12f12acSThomas Petazzoni  * Coherency fabric (Aurora) support for Armada 370, 375, 38x and XP
3e12f12acSThomas Petazzoni  * platforms.
4009f1315SGregory CLEMENT  *
5009f1315SGregory CLEMENT  * Copyright (C) 2012 Marvell
6009f1315SGregory CLEMENT  *
7009f1315SGregory CLEMENT  * Yehuda Yitschak <yehuday@marvell.com>
8009f1315SGregory CLEMENT  * Gregory Clement <gregory.clement@free-electrons.com>
9009f1315SGregory CLEMENT  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
10009f1315SGregory CLEMENT  *
11009f1315SGregory CLEMENT  * This file is licensed under the terms of the GNU General Public
12009f1315SGregory CLEMENT  * License version 2.  This program is licensed "as is" without any
13009f1315SGregory CLEMENT  * warranty of any kind, whether express or implied.
14009f1315SGregory CLEMENT  *
15e12f12acSThomas Petazzoni  * The Armada 370, 375, 38x and XP SOCs have a coherency fabric which is
16009f1315SGregory CLEMENT  * responsible for ensuring hardware coherency between all CPUs and between
17009f1315SGregory CLEMENT  * CPUs and I/O masters. This file initializes the coherency fabric and
18009f1315SGregory CLEMENT  * supplies basic routines for configuring and controlling hardware coherency
19009f1315SGregory CLEMENT  */
20009f1315SGregory CLEMENT 
215ab5afd8SThomas Petazzoni #define pr_fmt(fmt) "mvebu-coherency: " fmt
225ab5afd8SThomas Petazzoni 
23009f1315SGregory CLEMENT #include <linux/kernel.h>
24009f1315SGregory CLEMENT #include <linux/init.h>
25009f1315SGregory CLEMENT #include <linux/of_address.h>
26009f1315SGregory CLEMENT #include <linux/io.h>
27009f1315SGregory CLEMENT #include <linux/smp.h>
28e60304f8SGregory CLEMENT #include <linux/dma-mapping.h>
29e60304f8SGregory CLEMENT #include <linux/platform_device.h>
305ab5afd8SThomas Petazzoni #include <linux/slab.h>
315ab5afd8SThomas Petazzoni #include <linux/mbus.h>
32b0063aadSThomas Petazzoni #include <linux/pci.h>
33009f1315SGregory CLEMENT #include <asm/smp_plat.h>
34580ff0eeSThomas Petazzoni #include <asm/cacheflush.h>
35497a9230SThomas Petazzoni #include <asm/mach/map.h>
361bd4d8a6SThomas Petazzoni #include <asm/dma-mapping.h>
37b12634e3SJisheng Zhang #include "coherency.h"
3839438567SThomas Petazzoni #include "mvebu-soc-id.h"
39009f1315SGregory CLEMENT 
408bd26e3aSPaul Gortmaker unsigned long coherency_phys_base;
41ccd6a131SGregory CLEMENT void __iomem *coherency_base;
42e60304f8SGregory CLEMENT static void __iomem *coherency_cpu_base;
43d492cccaSThomas Petazzoni static void __iomem *cpu_config_base;
44009f1315SGregory CLEMENT 
45009f1315SGregory CLEMENT /* Coherency fabric registers */
46e60304f8SGregory CLEMENT #define IO_SYNC_BARRIER_CTL_OFFSET		   0x0
47e60304f8SGregory CLEMENT 
48924d38f4SThomas Petazzoni enum {
49501f928eSThomas Petazzoni 	COHERENCY_FABRIC_TYPE_NONE,
50924d38f4SThomas Petazzoni 	COHERENCY_FABRIC_TYPE_ARMADA_370_XP,
5177fa4b9aSThomas Petazzoni 	COHERENCY_FABRIC_TYPE_ARMADA_375,
52d0de9323SThomas Petazzoni 	COHERENCY_FABRIC_TYPE_ARMADA_380,
53924d38f4SThomas Petazzoni };
54924d38f4SThomas Petazzoni 
55444d2d33SUwe Kleine-König static const struct of_device_id of_coherency_table[] = {
56924d38f4SThomas Petazzoni 	{.compatible = "marvell,coherency-fabric",
57924d38f4SThomas Petazzoni 	 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_370_XP },
5877fa4b9aSThomas Petazzoni 	{.compatible = "marvell,armada-375-coherency-fabric",
5977fa4b9aSThomas Petazzoni 	 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_375 },
60d0de9323SThomas Petazzoni 	{.compatible = "marvell,armada-380-coherency-fabric",
61d0de9323SThomas Petazzoni 	 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_380 },
62009f1315SGregory CLEMENT 	{ /* end of list */ },
63009f1315SGregory CLEMENT };
64009f1315SGregory CLEMENT 
652e8a5942SGregory CLEMENT /* Functions defined in coherency_ll.S */
662e8a5942SGregory CLEMENT int ll_enable_coherency(void);
672e8a5942SGregory CLEMENT void ll_add_cpu_to_smp_group(void);
68009f1315SGregory CLEMENT 
69d492cccaSThomas Petazzoni #define CPU_CONFIG_SHARED_L2 BIT(16)
70d492cccaSThomas Petazzoni 
71d492cccaSThomas Petazzoni /*
72d492cccaSThomas Petazzoni  * Disable the "Shared L2 Present" bit in CPU Configuration register
73d492cccaSThomas Petazzoni  * on Armada XP.
74d492cccaSThomas Petazzoni  *
75d492cccaSThomas Petazzoni  * The "Shared L2 Present" bit affects the "level of coherence" value
76d492cccaSThomas Petazzoni  * in the clidr CP15 register.  Cache operation functions such as
77d492cccaSThomas Petazzoni  * "flush all" and "invalidate all" operate on all the cache levels
78d492cccaSThomas Petazzoni  * that included in the defined level of coherence. When HW I/O
79d492cccaSThomas Petazzoni  * coherency is used, this bit causes unnecessary flushes of the L2
80d492cccaSThomas Petazzoni  * cache.
81d492cccaSThomas Petazzoni  */
82d492cccaSThomas Petazzoni static void armada_xp_clear_shared_l2(void)
83d492cccaSThomas Petazzoni {
84d492cccaSThomas Petazzoni 	u32 reg;
85d492cccaSThomas Petazzoni 
86d492cccaSThomas Petazzoni 	if (!cpu_config_base)
87d492cccaSThomas Petazzoni 		return;
88d492cccaSThomas Petazzoni 
89d492cccaSThomas Petazzoni 	reg = readl(cpu_config_base);
90d492cccaSThomas Petazzoni 	reg &= ~CPU_CONFIG_SHARED_L2;
91d492cccaSThomas Petazzoni 	writel(reg, cpu_config_base);
92d492cccaSThomas Petazzoni }
93d492cccaSThomas Petazzoni 
94b0063aadSThomas Petazzoni static int mvebu_hwcc_notifier(struct notifier_block *nb,
95e60304f8SGregory CLEMENT 			       unsigned long event, void *__dev)
96e60304f8SGregory CLEMENT {
97e60304f8SGregory CLEMENT 	struct device *dev = __dev;
98e60304f8SGregory CLEMENT 
99e60304f8SGregory CLEMENT 	if (event != BUS_NOTIFY_ADD_DEVICE)
100e60304f8SGregory CLEMENT 		return NOTIFY_DONE;
1011bd4d8a6SThomas Petazzoni 	set_dma_ops(dev, &arm_coherent_dma_ops);
102e60304f8SGregory CLEMENT 
103e60304f8SGregory CLEMENT 	return NOTIFY_OK;
104e60304f8SGregory CLEMENT }
105e60304f8SGregory CLEMENT 
106b0063aadSThomas Petazzoni static struct notifier_block mvebu_hwcc_nb = {
107b0063aadSThomas Petazzoni 	.notifier_call = mvebu_hwcc_notifier,
108e60304f8SGregory CLEMENT };
109e60304f8SGregory CLEMENT 
11060f23952SArnd Bergmann static struct notifier_block mvebu_hwcc_pci_nb __maybe_unused = {
111a728b977SEzequiel Garcia 	.notifier_call = mvebu_hwcc_notifier,
112a728b977SEzequiel Garcia };
113a728b977SEzequiel Garcia 
114d492cccaSThomas Petazzoni static int armada_xp_clear_shared_l2_notifier_func(struct notifier_block *nfb,
115d492cccaSThomas Petazzoni 					unsigned long action, void *hcpu)
116d492cccaSThomas Petazzoni {
117d492cccaSThomas Petazzoni 	if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
118d492cccaSThomas Petazzoni 		armada_xp_clear_shared_l2();
119d492cccaSThomas Petazzoni 
120d492cccaSThomas Petazzoni 	return NOTIFY_OK;
121d492cccaSThomas Petazzoni }
122d492cccaSThomas Petazzoni 
123d492cccaSThomas Petazzoni static struct notifier_block armada_xp_clear_shared_l2_notifier = {
124d492cccaSThomas Petazzoni 	.notifier_call = armada_xp_clear_shared_l2_notifier_func,
125d492cccaSThomas Petazzoni 	.priority = 100,
126d492cccaSThomas Petazzoni };
127d492cccaSThomas Petazzoni 
128924d38f4SThomas Petazzoni static void __init armada_370_coherency_init(struct device_node *np)
129009f1315SGregory CLEMENT {
130580ff0eeSThomas Petazzoni 	struct resource res;
131d492cccaSThomas Petazzoni 	struct device_node *cpu_config_np;
132924d38f4SThomas Petazzoni 
133580ff0eeSThomas Petazzoni 	of_address_to_resource(np, 0, &res);
134580ff0eeSThomas Petazzoni 	coherency_phys_base = res.start;
135580ff0eeSThomas Petazzoni 	/*
136580ff0eeSThomas Petazzoni 	 * Ensure secondary CPUs will see the updated value,
137580ff0eeSThomas Petazzoni 	 * which they read before they join the coherency
138580ff0eeSThomas Petazzoni 	 * fabric, and therefore before they are coherent with
139580ff0eeSThomas Petazzoni 	 * the boot CPU cache.
140580ff0eeSThomas Petazzoni 	 */
141580ff0eeSThomas Petazzoni 	sync_cache_w(&coherency_phys_base);
142009f1315SGregory CLEMENT 	coherency_base = of_iomap(np, 0);
143e60304f8SGregory CLEMENT 	coherency_cpu_base = of_iomap(np, 1);
144d492cccaSThomas Petazzoni 
145d492cccaSThomas Petazzoni 	cpu_config_np = of_find_compatible_node(NULL, NULL,
146d492cccaSThomas Petazzoni 						"marvell,armada-xp-cpu-config");
147d492cccaSThomas Petazzoni 	if (!cpu_config_np)
148d492cccaSThomas Petazzoni 		goto exit;
149d492cccaSThomas Petazzoni 
150d492cccaSThomas Petazzoni 	cpu_config_base = of_iomap(cpu_config_np, 0);
151d492cccaSThomas Petazzoni 	if (!cpu_config_base) {
152d492cccaSThomas Petazzoni 		of_node_put(cpu_config_np);
153d492cccaSThomas Petazzoni 		goto exit;
154d492cccaSThomas Petazzoni 	}
155d492cccaSThomas Petazzoni 
156d492cccaSThomas Petazzoni 	of_node_put(cpu_config_np);
157d492cccaSThomas Petazzoni 
158d492cccaSThomas Petazzoni 	register_cpu_notifier(&armada_xp_clear_shared_l2_notifier);
159d492cccaSThomas Petazzoni 
160d492cccaSThomas Petazzoni exit:
161952f4ca7SGregory CLEMENT 	set_cpu_coherent();
162924d38f4SThomas Petazzoni }
163924d38f4SThomas Petazzoni 
164497a9230SThomas Petazzoni /*
165497a9230SThomas Petazzoni  * This ioremap hook is used on Armada 375/38x to ensure that PCIe
166497a9230SThomas Petazzoni  * memory areas are mapped as MT_UNCACHED instead of MT_DEVICE. This
167497a9230SThomas Petazzoni  * is needed as a workaround for a deadlock issue between the PCIe
168497a9230SThomas Petazzoni  * interface and the cache controller.
169497a9230SThomas Petazzoni  */
170497a9230SThomas Petazzoni static void __iomem *
171497a9230SThomas Petazzoni armada_pcie_wa_ioremap_caller(phys_addr_t phys_addr, size_t size,
172497a9230SThomas Petazzoni 			      unsigned int mtype, void *caller)
173497a9230SThomas Petazzoni {
174497a9230SThomas Petazzoni 	struct resource pcie_mem;
175497a9230SThomas Petazzoni 
176497a9230SThomas Petazzoni 	mvebu_mbus_get_pcie_mem_aperture(&pcie_mem);
177497a9230SThomas Petazzoni 
178497a9230SThomas Petazzoni 	if (pcie_mem.start <= phys_addr && (phys_addr + size) <= pcie_mem.end)
179497a9230SThomas Petazzoni 		mtype = MT_UNCACHED;
180497a9230SThomas Petazzoni 
181497a9230SThomas Petazzoni 	return __arm_ioremap_caller(phys_addr, size, mtype, caller);
182497a9230SThomas Petazzoni }
183497a9230SThomas Petazzoni 
184d0de9323SThomas Petazzoni static void __init armada_375_380_coherency_init(struct device_node *np)
18577fa4b9aSThomas Petazzoni {
186497a9230SThomas Petazzoni 	struct device_node *cache_dn;
187497a9230SThomas Petazzoni 
18877fa4b9aSThomas Petazzoni 	coherency_cpu_base = of_iomap(np, 0);
189497a9230SThomas Petazzoni 	arch_ioremap_caller = armada_pcie_wa_ioremap_caller;
190497a9230SThomas Petazzoni 
191497a9230SThomas Petazzoni 	/*
192dcad6887SThomas Petazzoni 	 * We should switch the PL310 to I/O coherency mode only if
193dcad6887SThomas Petazzoni 	 * I/O coherency is actually enabled.
194dcad6887SThomas Petazzoni 	 */
195dcad6887SThomas Petazzoni 	if (!coherency_available())
196dcad6887SThomas Petazzoni 		return;
197dcad6887SThomas Petazzoni 
198dcad6887SThomas Petazzoni 	/*
199497a9230SThomas Petazzoni 	 * Add the PL310 property "arm,io-coherent". This makes sure the
200497a9230SThomas Petazzoni 	 * outer sync operation is not used, which allows to
201497a9230SThomas Petazzoni 	 * workaround the system erratum that causes deadlocks when
202497a9230SThomas Petazzoni 	 * doing PCIe in an SMP situation on Armada 375 and Armada
203497a9230SThomas Petazzoni 	 * 38x.
204497a9230SThomas Petazzoni 	 */
205497a9230SThomas Petazzoni 	for_each_compatible_node(cache_dn, NULL, "arm,pl310-cache") {
206497a9230SThomas Petazzoni 		struct property *p;
207497a9230SThomas Petazzoni 
208497a9230SThomas Petazzoni 		p = kzalloc(sizeof(*p), GFP_KERNEL);
209497a9230SThomas Petazzoni 		p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
210497a9230SThomas Petazzoni 		of_add_property(cache_dn, p);
211497a9230SThomas Petazzoni 	}
21277fa4b9aSThomas Petazzoni }
21377fa4b9aSThomas Petazzoni 
214501f928eSThomas Petazzoni static int coherency_type(void)
215924d38f4SThomas Petazzoni {
216924d38f4SThomas Petazzoni 	struct device_node *np;
2175fbba080SThomas Petazzoni 	const struct of_device_id *match;
218e5535545SThomas Petazzoni 	int type;
219e5535545SThomas Petazzoni 
220e5535545SThomas Petazzoni 	/*
221e5535545SThomas Petazzoni 	 * The coherency fabric is needed:
222e5535545SThomas Petazzoni 	 * - For coherency between processors on Armada XP, so only
223e5535545SThomas Petazzoni 	 *   when SMP is enabled.
224e5535545SThomas Petazzoni 	 * - For coherency between the processor and I/O devices, but
225e5535545SThomas Petazzoni 	 *   this coherency requires many pre-requisites (write
226e5535545SThomas Petazzoni 	 *   allocate cache policy, shareable pages, SMP bit set) that
227e5535545SThomas Petazzoni 	 *   are only meant in SMP situations.
228e5535545SThomas Petazzoni 	 *
229e5535545SThomas Petazzoni 	 * Note that this means that on Armada 370, there is currently
230e5535545SThomas Petazzoni 	 * no way to use hardware I/O coherency, because even when
231e5535545SThomas Petazzoni 	 * CONFIG_SMP is enabled, is_smp() returns false due to the
232e5535545SThomas Petazzoni 	 * Armada 370 being a single-core processor. To lift this
233e5535545SThomas Petazzoni 	 * limitation, we would have to find a way to make the cache
234e5535545SThomas Petazzoni 	 * policy set to write-allocate (on all Armada SoCs), and to
235e5535545SThomas Petazzoni 	 * set the shareable attribute in page tables (on all Armada
236e5535545SThomas Petazzoni 	 * SoCs except the Armada 370). Unfortunately, such decisions
237e5535545SThomas Petazzoni 	 * are taken very early in the kernel boot process, at a point
238e5535545SThomas Petazzoni 	 * where we don't know yet on which SoC we are running.
239e5535545SThomas Petazzoni 
240e5535545SThomas Petazzoni 	 */
241e5535545SThomas Petazzoni 	if (!is_smp())
242e5535545SThomas Petazzoni 		return COHERENCY_FABRIC_TYPE_NONE;
243924d38f4SThomas Petazzoni 
2445fbba080SThomas Petazzoni 	np = of_find_matching_node_and_match(NULL, of_coherency_table, &match);
245e5535545SThomas Petazzoni 	if (!np)
246501f928eSThomas Petazzoni 		return COHERENCY_FABRIC_TYPE_NONE;
247e5535545SThomas Petazzoni 
248e5535545SThomas Petazzoni 	type = (int) match->data;
249e5535545SThomas Petazzoni 
250e5535545SThomas Petazzoni 	of_node_put(np);
251e5535545SThomas Petazzoni 
252e5535545SThomas Petazzoni 	return type;
253501f928eSThomas Petazzoni }
254501f928eSThomas Petazzoni 
25501049a5dSNadav Haklai int set_cpu_coherent(void)
25601049a5dSNadav Haklai {
25701049a5dSNadav Haklai 	int type = coherency_type();
25801049a5dSNadav Haklai 
25901049a5dSNadav Haklai 	if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP) {
26001049a5dSNadav Haklai 		if (!coherency_base) {
26101049a5dSNadav Haklai 			pr_warn("Can't make current CPU cache coherent.\n");
26201049a5dSNadav Haklai 			pr_warn("Coherency fabric is not initialized\n");
26301049a5dSNadav Haklai 			return 1;
26401049a5dSNadav Haklai 		}
265d492cccaSThomas Petazzoni 
266d492cccaSThomas Petazzoni 		armada_xp_clear_shared_l2();
26701049a5dSNadav Haklai 		ll_add_cpu_to_smp_group();
26801049a5dSNadav Haklai 		return ll_enable_coherency();
26901049a5dSNadav Haklai 	}
27001049a5dSNadav Haklai 
27101049a5dSNadav Haklai 	return 0;
27201049a5dSNadav Haklai }
27301049a5dSNadav Haklai 
274501f928eSThomas Petazzoni int coherency_available(void)
275501f928eSThomas Petazzoni {
2761bd4d8a6SThomas Petazzoni 	return coherency_type() != COHERENCY_FABRIC_TYPE_NONE;
277501f928eSThomas Petazzoni }
278501f928eSThomas Petazzoni 
279501f928eSThomas Petazzoni int __init coherency_init(void)
280501f928eSThomas Petazzoni {
281501f928eSThomas Petazzoni 	int type = coherency_type();
282501f928eSThomas Petazzoni 	struct device_node *np;
283501f928eSThomas Petazzoni 
284501f928eSThomas Petazzoni 	np = of_find_matching_node(NULL, of_coherency_table);
285501f928eSThomas Petazzoni 
286501f928eSThomas Petazzoni 	if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
287501f928eSThomas Petazzoni 		armada_370_coherency_init(np);
288d0de9323SThomas Petazzoni 	else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375 ||
289d0de9323SThomas Petazzoni 		 type == COHERENCY_FABRIC_TYPE_ARMADA_380)
290d0de9323SThomas Petazzoni 		armada_375_380_coherency_init(np);
291501f928eSThomas Petazzoni 
2922eb04ae0SThomas Petazzoni 	of_node_put(np);
2932eb04ae0SThomas Petazzoni 
294009f1315SGregory CLEMENT 	return 0;
295009f1315SGregory CLEMENT }
296865e0527SThomas Petazzoni 
297865e0527SThomas Petazzoni static int __init coherency_late_init(void)
298865e0527SThomas Petazzoni {
299ef01c6c3SThomas Petazzoni 	if (coherency_available())
300865e0527SThomas Petazzoni 		bus_register_notifier(&platform_bus_type,
301b0063aadSThomas Petazzoni 				      &mvebu_hwcc_nb);
302865e0527SThomas Petazzoni 	return 0;
303865e0527SThomas Petazzoni }
304865e0527SThomas Petazzoni 
305865e0527SThomas Petazzoni postcore_initcall(coherency_late_init);
306b0063aadSThomas Petazzoni 
3078828ccc3SThomas Petazzoni #if IS_ENABLED(CONFIG_PCI)
308b0063aadSThomas Petazzoni static int __init coherency_pci_init(void)
309b0063aadSThomas Petazzoni {
310b0063aadSThomas Petazzoni 	if (coherency_available())
311b0063aadSThomas Petazzoni 		bus_register_notifier(&pci_bus_type,
312a728b977SEzequiel Garcia 				       &mvebu_hwcc_pci_nb);
313b0063aadSThomas Petazzoni 	return 0;
314b0063aadSThomas Petazzoni }
315b0063aadSThomas Petazzoni 
316b0063aadSThomas Petazzoni arch_initcall(coherency_pci_init);
3178828ccc3SThomas Petazzoni #endif
318