blob: d5a975b6a590aa939a4b2d3864faeaa51ef584b0 [file] [log] [blame]
Gregory CLEMENT009f1312012-08-02 11:16:29 +03001/*
2 * Coherency fabric (Aurora) support for Armada 370 and XP platforms.
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Yehuda Yitschak <yehuday@marvell.com>
7 * Gregory Clement <gregory.clement@free-electrons.com>
8 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 *
14 * The Armada 370 and Armada XP SOCs have a coherency fabric which is
15 * responsible for ensuring hardware coherency between all CPUs and between
16 * CPUs and I/O masters. This file initializes the coherency fabric and
17 * supplies basic routines for configuring and controlling hardware coherency
18 */
19
Thomas Petazzoni5ab5afd2014-04-14 15:47:05 +020020#define pr_fmt(fmt) "mvebu-coherency: " fmt
21
Gregory CLEMENT009f1312012-08-02 11:16:29 +030022#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/of_address.h>
25#include <linux/io.h>
26#include <linux/smp.h>
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020027#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Thomas Petazzoni5ab5afd2014-04-14 15:47:05 +020029#include <linux/slab.h>
30#include <linux/mbus.h>
31#include <linux/clk.h>
Gregory CLEMENT009f1312012-08-02 11:16:29 +030032#include <asm/smp_plat.h>
Thomas Petazzoni580ff0e2013-06-06 12:24:28 +020033#include <asm/cacheflush.h>
Gregory CLEMENT009f1312012-08-02 11:16:29 +030034#include "armada-370-xp.h"
Jisheng Zhangb12634e2013-11-07 17:02:38 +080035#include "coherency.h"
Thomas Petazzoni39438562014-05-05 17:05:26 +020036#include "mvebu-soc-id.h"
Gregory CLEMENT009f1312012-08-02 11:16:29 +030037
Paul Gortmaker8bd26e32013-06-17 15:43:14 -040038unsigned long coherency_phys_base;
Gregory CLEMENTccd6a132014-04-14 17:10:05 +020039void __iomem *coherency_base;
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020040static void __iomem *coherency_cpu_base;
Gregory CLEMENT009f1312012-08-02 11:16:29 +030041
42/* Coherency fabric registers */
43#define COHERENCY_FABRIC_CFG_OFFSET 0x4
44
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
Gregory CLEMENT009f1312012-08-02 11:16:29 +030054static 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
Gregory CLEMENT952f4ca2014-04-14 17:10:07 +020068int set_cpu_coherent(void)
Gregory CLEMENT009f1312012-08-02 11:16:29 +030069{
70 if (!coherency_base) {
Gregory CLEMENTb41375f2014-04-14 17:10:06 +020071 pr_warn("Can't make current CPU cache coherent.\n");
Gregory CLEMENT009f1312012-08-02 11:16:29 +030072 pr_warn("Coherency fabric is not initialized\n");
73 return 1;
74 }
75
Gregory CLEMENT2e8a5942014-04-14 17:10:08 +020076 ll_add_cpu_to_smp_group();
77 return ll_enable_coherency();
Gregory CLEMENT009f1312012-08-02 11:16:29 +030078}
79
Thomas Petazzoni5ab5afd2014-04-14 15:47:05 +020080/*
81 * The below code implements the I/O coherency workaround on Armada
82 * 375. This workaround consists in using the two channels of the
83 * first XOR engine to trigger a XOR transaction that serves as the
84 * I/O coherency barrier.
85 */
86
87static void __iomem *xor_base, *xor_high_base;
88static dma_addr_t coherency_wa_buf_phys[CONFIG_NR_CPUS];
89static void *coherency_wa_buf[CONFIG_NR_CPUS];
90static bool coherency_wa_enabled;
91
92#define XOR_CONFIG(chan) (0x10 + (chan * 4))
93#define XOR_ACTIVATION(chan) (0x20 + (chan * 4))
94#define WINDOW_BAR_ENABLE(chan) (0x240 + ((chan) << 2))
95#define WINDOW_BASE(w) (0x250 + ((w) << 2))
96#define WINDOW_SIZE(w) (0x270 + ((w) << 2))
97#define WINDOW_REMAP_HIGH(w) (0x290 + ((w) << 2))
98#define WINDOW_OVERRIDE_CTRL(chan) (0x2A0 + ((chan) << 2))
99#define XOR_DEST_POINTER(chan) (0x2B0 + (chan * 4))
100#define XOR_BLOCK_SIZE(chan) (0x2C0 + (chan * 4))
101#define XOR_INIT_VALUE_LOW 0x2E0
102#define XOR_INIT_VALUE_HIGH 0x2E4
103
104static inline void mvebu_hwcc_armada375_sync_io_barrier_wa(void)
105{
106 int idx = smp_processor_id();
107
108 /* Write '1' to the first word of the buffer */
109 writel(0x1, coherency_wa_buf[idx]);
110
111 /* Wait until the engine is idle */
112 while ((readl(xor_base + XOR_ACTIVATION(idx)) >> 4) & 0x3)
113 ;
114
115 dmb();
116
117 /* Trigger channel */
118 writel(0x1, xor_base + XOR_ACTIVATION(idx));
119
120 /* Poll the data until it is cleared by the XOR transaction */
121 while (readl(coherency_wa_buf[idx]))
122 ;
123}
124
125static void __init armada_375_coherency_init_wa(void)
126{
127 const struct mbus_dram_target_info *dram;
128 struct device_node *xor_node;
129 struct property *xor_status;
130 struct clk *xor_clk;
131 u32 win_enable = 0;
132 int i;
133
134 pr_warn("enabling coherency workaround for Armada 375 Z1, one XOR engine disabled\n");
135
136 /*
137 * Since the workaround uses one XOR engine, we grab a
138 * reference to its Device Tree node first.
139 */
140 xor_node = of_find_compatible_node(NULL, NULL, "marvell,orion-xor");
141 BUG_ON(!xor_node);
142
143 /*
144 * Then we mark it as disabled so that the real XOR driver
145 * will not use it.
146 */
147 xor_status = kzalloc(sizeof(struct property), GFP_KERNEL);
148 BUG_ON(!xor_status);
149
150 xor_status->value = kstrdup("disabled", GFP_KERNEL);
151 BUG_ON(!xor_status->value);
152
153 xor_status->length = 8;
154 xor_status->name = kstrdup("status", GFP_KERNEL);
155 BUG_ON(!xor_status->name);
156
157 of_update_property(xor_node, xor_status);
158
159 /*
160 * And we remap the registers, get the clock, and do the
161 * initial configuration of the XOR engine.
162 */
163 xor_base = of_iomap(xor_node, 0);
164 xor_high_base = of_iomap(xor_node, 1);
165
166 xor_clk = of_clk_get_by_name(xor_node, NULL);
167 BUG_ON(!xor_clk);
168
169 clk_prepare_enable(xor_clk);
170
171 dram = mv_mbus_dram_info();
172
173 for (i = 0; i < 8; i++) {
174 writel(0, xor_base + WINDOW_BASE(i));
175 writel(0, xor_base + WINDOW_SIZE(i));
176 if (i < 4)
177 writel(0, xor_base + WINDOW_REMAP_HIGH(i));
178 }
179
180 for (i = 0; i < dram->num_cs; i++) {
181 const struct mbus_dram_window *cs = dram->cs + i;
182 writel((cs->base & 0xffff0000) |
183 (cs->mbus_attr << 8) |
184 dram->mbus_dram_target_id, xor_base + WINDOW_BASE(i));
185 writel((cs->size - 1) & 0xffff0000, xor_base + WINDOW_SIZE(i));
186
187 win_enable |= (1 << i);
188 win_enable |= 3 << (16 + (2 * i));
189 }
190
191 writel(win_enable, xor_base + WINDOW_BAR_ENABLE(0));
192 writel(win_enable, xor_base + WINDOW_BAR_ENABLE(1));
193 writel(0, xor_base + WINDOW_OVERRIDE_CTRL(0));
194 writel(0, xor_base + WINDOW_OVERRIDE_CTRL(1));
195
196 for (i = 0; i < CONFIG_NR_CPUS; i++) {
197 coherency_wa_buf[i] = kzalloc(PAGE_SIZE, GFP_KERNEL);
198 BUG_ON(!coherency_wa_buf[i]);
199
200 /*
201 * We can't use the DMA mapping API, since we don't
202 * have a valid 'struct device' pointer
203 */
204 coherency_wa_buf_phys[i] =
205 virt_to_phys(coherency_wa_buf[i]);
206 BUG_ON(!coherency_wa_buf_phys[i]);
207
208 /*
209 * Configure the XOR engine for memset operation, with
210 * a 128 bytes block size
211 */
212 writel(0x444, xor_base + XOR_CONFIG(i));
213 writel(128, xor_base + XOR_BLOCK_SIZE(i));
214 writel(coherency_wa_buf_phys[i],
215 xor_base + XOR_DEST_POINTER(i));
216 }
217
218 writel(0x0, xor_base + XOR_INIT_VALUE_LOW);
219 writel(0x0, xor_base + XOR_INIT_VALUE_HIGH);
220
221 coherency_wa_enabled = true;
222}
223
Gregory CLEMENTe60304f2012-10-12 19:20:36 +0200224static inline void mvebu_hwcc_sync_io_barrier(void)
225{
Thomas Petazzoni5ab5afd2014-04-14 15:47:05 +0200226 if (coherency_wa_enabled) {
227 mvebu_hwcc_armada375_sync_io_barrier_wa();
228 return;
229 }
230
Gregory CLEMENTe60304f2012-10-12 19:20:36 +0200231 writel(0x1, coherency_cpu_base + IO_SYNC_BARRIER_CTL_OFFSET);
232 while (readl(coherency_cpu_base + IO_SYNC_BARRIER_CTL_OFFSET) & 0x1);
233}
234
235static dma_addr_t mvebu_hwcc_dma_map_page(struct device *dev, struct page *page,
236 unsigned long offset, size_t size,
237 enum dma_data_direction dir,
238 struct dma_attrs *attrs)
239{
240 if (dir != DMA_TO_DEVICE)
241 mvebu_hwcc_sync_io_barrier();
242 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
243}
244
245
246static void mvebu_hwcc_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
247 size_t size, enum dma_data_direction dir,
248 struct dma_attrs *attrs)
249{
250 if (dir != DMA_TO_DEVICE)
251 mvebu_hwcc_sync_io_barrier();
252}
253
254static void mvebu_hwcc_dma_sync(struct device *dev, dma_addr_t dma_handle,
255 size_t size, enum dma_data_direction dir)
256{
257 if (dir != DMA_TO_DEVICE)
258 mvebu_hwcc_sync_io_barrier();
259}
260
261static struct dma_map_ops mvebu_hwcc_dma_ops = {
262 .alloc = arm_dma_alloc,
263 .free = arm_dma_free,
264 .mmap = arm_dma_mmap,
265 .map_page = mvebu_hwcc_dma_map_page,
266 .unmap_page = mvebu_hwcc_dma_unmap_page,
267 .get_sgtable = arm_dma_get_sgtable,
268 .map_sg = arm_dma_map_sg,
269 .unmap_sg = arm_dma_unmap_sg,
270 .sync_single_for_cpu = mvebu_hwcc_dma_sync,
271 .sync_single_for_device = mvebu_hwcc_dma_sync,
272 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
273 .sync_sg_for_device = arm_dma_sync_sg_for_device,
274 .set_dma_mask = arm_dma_set_mask,
275};
276
277static int mvebu_hwcc_platform_notifier(struct notifier_block *nb,
278 unsigned long event, void *__dev)
279{
280 struct device *dev = __dev;
281
282 if (event != BUS_NOTIFY_ADD_DEVICE)
283 return NOTIFY_DONE;
284 set_dma_ops(dev, &mvebu_hwcc_dma_ops);
285
286 return NOTIFY_OK;
287}
288
289static struct notifier_block mvebu_hwcc_platform_nb = {
290 .notifier_call = mvebu_hwcc_platform_notifier,
291};
292
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200293static void __init armada_370_coherency_init(struct device_node *np)
294{
295 struct resource res;
296
297 of_address_to_resource(np, 0, &res);
298 coherency_phys_base = res.start;
299 /*
300 * Ensure secondary CPUs will see the updated value,
301 * which they read before they join the coherency
302 * fabric, and therefore before they are coherent with
303 * the boot CPU cache.
304 */
305 sync_cache_w(&coherency_phys_base);
306 coherency_base = of_iomap(np, 0);
307 coherency_cpu_base = of_iomap(np, 1);
Gregory CLEMENT952f4ca2014-04-14 17:10:07 +0200308 set_cpu_coherent();
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200309}
310
Thomas Petazzonid0de9322014-04-14 15:47:06 +0200311static void __init armada_375_380_coherency_init(struct device_node *np)
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200312{
313 coherency_cpu_base = of_iomap(np, 0);
314}
315
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200316static int coherency_type(void)
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300317{
318 struct device_node *np;
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200319 const struct of_device_id *match;
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300320
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200321 np = of_find_matching_node_and_match(NULL, of_coherency_table, &match);
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300322 if (np) {
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200323 int type = (int) match->data;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200324
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200325 /* Armada 370/XP coherency works in both UP and SMP */
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200326 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200327 return type;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200328
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200329 /* Armada 375 coherency works only on SMP */
330 else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375 && is_smp())
331 return type;
332
Thomas Petazzonid0de9322014-04-14 15:47:06 +0200333 /* Armada 380 coherency works only on SMP */
334 else if (type == COHERENCY_FABRIC_TYPE_ARMADA_380 && is_smp())
335 return type;
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300336 }
337
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200338 return COHERENCY_FABRIC_TYPE_NONE;
339}
340
341int coherency_available(void)
342{
343 return coherency_type() != COHERENCY_FABRIC_TYPE_NONE;
344}
345
346int __init coherency_init(void)
347{
348 int type = coherency_type();
349 struct device_node *np;
350
351 np = of_find_matching_node(NULL, of_coherency_table);
352
353 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
354 armada_370_coherency_init(np);
Thomas Petazzonid0de9322014-04-14 15:47:06 +0200355 else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375 ||
356 type == COHERENCY_FABRIC_TYPE_ARMADA_380)
357 armada_375_380_coherency_init(np);
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200358
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300359 return 0;
360}
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200361
362static int __init coherency_late_init(void)
363{
Thomas Petazzoni5ab5afd2014-04-14 15:47:05 +0200364 int type = coherency_type();
365
366 if (type == COHERENCY_FABRIC_TYPE_NONE)
367 return 0;
368
Thomas Petazzoni39438562014-05-05 17:05:26 +0200369 if (type == COHERENCY_FABRIC_TYPE_ARMADA_375) {
370 u32 dev, rev;
371
372 if (mvebu_get_soc_id(&dev, &rev) == 0 &&
373 rev == ARMADA_375_Z1_REV)
374 armada_375_coherency_init_wa();
375 }
Thomas Petazzoni5ab5afd2014-04-14 15:47:05 +0200376
377 bus_register_notifier(&platform_bus_type,
378 &mvebu_hwcc_platform_nb);
379
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200380 return 0;
381}
382
383postcore_initcall(coherency_late_init);