blob: 44eedf331ae7df40bb76fcfec0358e89ce128d76 [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;
Gregory CLEMENT009f1312012-08-02 11:16:29 +030043
44/* Coherency fabric registers */
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020045#define IO_SYNC_BARRIER_CTL_OFFSET 0x0
46
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020047enum {
Thomas Petazzoni501f9282014-04-14 15:47:00 +020048 COHERENCY_FABRIC_TYPE_NONE,
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020049 COHERENCY_FABRIC_TYPE_ARMADA_370_XP,
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +020050 COHERENCY_FABRIC_TYPE_ARMADA_375,
Thomas Petazzonid0de9322014-04-14 15:47:06 +020051 COHERENCY_FABRIC_TYPE_ARMADA_380,
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020052};
53
Uwe Kleine-König444d2d32015-02-18 21:19:56 +010054static const struct of_device_id of_coherency_table[] = {
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020055 {.compatible = "marvell,coherency-fabric",
56 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_370_XP },
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +020057 {.compatible = "marvell,armada-375-coherency-fabric",
58 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_375 },
Thomas Petazzonid0de9322014-04-14 15:47:06 +020059 {.compatible = "marvell,armada-380-coherency-fabric",
60 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_380 },
Gregory CLEMENT009f1312012-08-02 11:16:29 +030061 { /* end of list */ },
62};
63
Gregory CLEMENT2e8a5942014-04-14 17:10:08 +020064/* Functions defined in coherency_ll.S */
65int ll_enable_coherency(void);
66void ll_add_cpu_to_smp_group(void);
Gregory CLEMENT009f1312012-08-02 11:16:29 +030067
Thomas Petazzonib0063aa2014-05-13 18:04:30 +020068static int mvebu_hwcc_notifier(struct notifier_block *nb,
69 unsigned long event, void *__dev)
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020070{
71 struct device *dev = __dev;
72
73 if (event != BUS_NOTIFY_ADD_DEVICE)
74 return NOTIFY_DONE;
Thomas Petazzoni1bd4d8a2015-01-16 17:11:29 +010075 set_dma_ops(dev, &arm_coherent_dma_ops);
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020076
77 return NOTIFY_OK;
78}
79
Thomas Petazzonib0063aa2014-05-13 18:04:30 +020080static struct notifier_block mvebu_hwcc_nb = {
81 .notifier_call = mvebu_hwcc_notifier,
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020082};
83
Ezequiel Garciaa728b972014-07-08 10:37:37 -030084static struct notifier_block mvebu_hwcc_pci_nb = {
85 .notifier_call = mvebu_hwcc_notifier,
86};
87
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020088static void __init armada_370_coherency_init(struct device_node *np)
89{
90 struct resource res;
91
92 of_address_to_resource(np, 0, &res);
93 coherency_phys_base = res.start;
94 /*
95 * Ensure secondary CPUs will see the updated value,
96 * which they read before they join the coherency
97 * fabric, and therefore before they are coherent with
98 * the boot CPU cache.
99 */
100 sync_cache_w(&coherency_phys_base);
101 coherency_base = of_iomap(np, 0);
102 coherency_cpu_base = of_iomap(np, 1);
Gregory CLEMENT952f4ca2014-04-14 17:10:07 +0200103 set_cpu_coherent();
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200104}
105
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200106/*
107 * This ioremap hook is used on Armada 375/38x to ensure that PCIe
108 * memory areas are mapped as MT_UNCACHED instead of MT_DEVICE. This
109 * is needed as a workaround for a deadlock issue between the PCIe
110 * interface and the cache controller.
111 */
112static void __iomem *
113armada_pcie_wa_ioremap_caller(phys_addr_t phys_addr, size_t size,
114 unsigned int mtype, void *caller)
115{
116 struct resource pcie_mem;
117
118 mvebu_mbus_get_pcie_mem_aperture(&pcie_mem);
119
120 if (pcie_mem.start <= phys_addr && (phys_addr + size) <= pcie_mem.end)
121 mtype = MT_UNCACHED;
122
123 return __arm_ioremap_caller(phys_addr, size, mtype, caller);
124}
125
Thomas Petazzonid0de9322014-04-14 15:47:06 +0200126static void __init armada_375_380_coherency_init(struct device_node *np)
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200127{
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200128 struct device_node *cache_dn;
129
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200130 coherency_cpu_base = of_iomap(np, 0);
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200131 arch_ioremap_caller = armada_pcie_wa_ioremap_caller;
132
133 /*
Thomas Petazzonidcad6882015-01-28 12:55:45 +0100134 * We should switch the PL310 to I/O coherency mode only if
135 * I/O coherency is actually enabled.
136 */
137 if (!coherency_available())
138 return;
139
140 /*
Thomas Petazzoni497a9232014-05-15 16:59:34 +0200141 * Add the PL310 property "arm,io-coherent". This makes sure the
142 * outer sync operation is not used, which allows to
143 * workaround the system erratum that causes deadlocks when
144 * doing PCIe in an SMP situation on Armada 375 and Armada
145 * 38x.
146 */
147 for_each_compatible_node(cache_dn, NULL, "arm,pl310-cache") {
148 struct property *p;
149
150 p = kzalloc(sizeof(*p), GFP_KERNEL);
151 p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
152 of_add_property(cache_dn, p);
153 }
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200154}
155
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200156static int coherency_type(void)
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300157{
158 struct device_node *np;
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200159 const struct of_device_id *match;
Thomas Petazzonie5535542014-11-13 10:38:57 +0100160 int type;
161
162 /*
163 * The coherency fabric is needed:
164 * - For coherency between processors on Armada XP, so only
165 * when SMP is enabled.
166 * - For coherency between the processor and I/O devices, but
167 * this coherency requires many pre-requisites (write
168 * allocate cache policy, shareable pages, SMP bit set) that
169 * are only meant in SMP situations.
170 *
171 * Note that this means that on Armada 370, there is currently
172 * no way to use hardware I/O coherency, because even when
173 * CONFIG_SMP is enabled, is_smp() returns false due to the
174 * Armada 370 being a single-core processor. To lift this
175 * limitation, we would have to find a way to make the cache
176 * policy set to write-allocate (on all Armada SoCs), and to
177 * set the shareable attribute in page tables (on all Armada
178 * SoCs except the Armada 370). Unfortunately, such decisions
179 * are taken very early in the kernel boot process, at a point
180 * where we don't know yet on which SoC we are running.
181
182 */
183 if (!is_smp())
184 return COHERENCY_FABRIC_TYPE_NONE;
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300185
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200186 np = of_find_matching_node_and_match(NULL, of_coherency_table, &match);
Thomas Petazzonie5535542014-11-13 10:38:57 +0100187 if (!np)
188 return COHERENCY_FABRIC_TYPE_NONE;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200189
Thomas Petazzonie5535542014-11-13 10:38:57 +0100190 type = (int) match->data;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200191
Thomas Petazzonie5535542014-11-13 10:38:57 +0100192 of_node_put(np);
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200193
Thomas Petazzonie5535542014-11-13 10:38:57 +0100194 return type;
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200195}
196
Nadav Haklai01049a52015-07-08 17:02:30 +0200197int set_cpu_coherent(void)
198{
199 int type = coherency_type();
200
201 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP) {
202 if (!coherency_base) {
203 pr_warn("Can't make current CPU cache coherent.\n");
204 pr_warn("Coherency fabric is not initialized\n");
205 return 1;
206 }
207 ll_add_cpu_to_smp_group();
208 return ll_enable_coherency();
209 }
210
211 return 0;
212}
213
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200214int coherency_available(void)
215{
Thomas Petazzoni1bd4d8a2015-01-16 17:11:29 +0100216 return coherency_type() != COHERENCY_FABRIC_TYPE_NONE;
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200217}
218
219int __init coherency_init(void)
220{
221 int type = coherency_type();
222 struct device_node *np;
223
224 np = of_find_matching_node(NULL, of_coherency_table);
225
226 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
227 armada_370_coherency_init(np);
Thomas Petazzonid0de9322014-04-14 15:47:06 +0200228 else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375 ||
229 type == COHERENCY_FABRIC_TYPE_ARMADA_380)
230 armada_375_380_coherency_init(np);
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200231
Thomas Petazzoni2eb04ae2014-10-27 16:32:35 +0100232 of_node_put(np);
233
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300234 return 0;
235}
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200236
237static int __init coherency_late_init(void)
238{
Thomas Petazzonief01c6c2014-11-13 10:38:59 +0100239 if (coherency_available())
240 bus_register_notifier(&platform_bus_type,
241 &mvebu_hwcc_nb);
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200242 return 0;
243}
244
245postcore_initcall(coherency_late_init);
Thomas Petazzonib0063aa2014-05-13 18:04:30 +0200246
Thomas Petazzoni8828ccc2014-05-20 17:13:03 +0200247#if IS_ENABLED(CONFIG_PCI)
Thomas Petazzonib0063aa2014-05-13 18:04:30 +0200248static int __init coherency_pci_init(void)
249{
250 if (coherency_available())
251 bus_register_notifier(&pci_bus_type,
Ezequiel Garciaa728b972014-07-08 10:37:37 -0300252 &mvebu_hwcc_pci_nb);
Thomas Petazzonib0063aa2014-05-13 18:04:30 +0200253 return 0;
254}
255
256arch_initcall(coherency_pci_init);
Thomas Petazzoni8828ccc2014-05-20 17:13:03 +0200257#endif