blob: 7ccaf87fd7729501a4bd1655c30a7681daf4581e [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
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/of_address.h>
23#include <linux/io.h>
24#include <linux/smp.h>
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020025#include <linux/dma-mapping.h>
26#include <linux/platform_device.h>
Gregory CLEMENT009f1312012-08-02 11:16:29 +030027#include <asm/smp_plat.h>
Thomas Petazzoni580ff0e2013-06-06 12:24:28 +020028#include <asm/cacheflush.h>
Gregory CLEMENT009f1312012-08-02 11:16:29 +030029#include "armada-370-xp.h"
Jisheng Zhangb12634e2013-11-07 17:02:38 +080030#include "coherency.h"
Gregory CLEMENT009f1312012-08-02 11:16:29 +030031
Paul Gortmaker8bd26e32013-06-17 15:43:14 -040032unsigned long coherency_phys_base;
Thomas Petazzoni865e0522013-06-05 09:04:55 +020033static void __iomem *coherency_base;
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020034static void __iomem *coherency_cpu_base;
Gregory CLEMENT009f1312012-08-02 11:16:29 +030035
36/* Coherency fabric registers */
37#define COHERENCY_FABRIC_CFG_OFFSET 0x4
38
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020039#define IO_SYNC_BARRIER_CTL_OFFSET 0x0
40
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020041enum {
Thomas Petazzoni501f9282014-04-14 15:47:00 +020042 COHERENCY_FABRIC_TYPE_NONE,
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020043 COHERENCY_FABRIC_TYPE_ARMADA_370_XP,
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +020044 COHERENCY_FABRIC_TYPE_ARMADA_375,
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020045};
46
Gregory CLEMENT009f1312012-08-02 11:16:29 +030047static struct of_device_id of_coherency_table[] = {
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020048 {.compatible = "marvell,coherency-fabric",
49 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_370_XP },
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +020050 {.compatible = "marvell,armada-375-coherency-fabric",
51 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_375 },
Gregory CLEMENT009f1312012-08-02 11:16:29 +030052 { /* end of list */ },
53};
54
Gregory CLEMENT009f1312012-08-02 11:16:29 +030055/* Function defined in coherency_ll.S */
56int ll_set_cpu_coherent(void __iomem *base_addr, unsigned int hw_cpu_id);
57
58int set_cpu_coherent(unsigned int hw_cpu_id, int smp_group_id)
59{
60 if (!coherency_base) {
61 pr_warn("Can't make CPU %d cache coherent.\n", hw_cpu_id);
62 pr_warn("Coherency fabric is not initialized\n");
63 return 1;
64 }
65
66 return ll_set_cpu_coherent(coherency_base, hw_cpu_id);
67}
68
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020069static inline void mvebu_hwcc_sync_io_barrier(void)
70{
71 writel(0x1, coherency_cpu_base + IO_SYNC_BARRIER_CTL_OFFSET);
72 while (readl(coherency_cpu_base + IO_SYNC_BARRIER_CTL_OFFSET) & 0x1);
73}
74
75static dma_addr_t mvebu_hwcc_dma_map_page(struct device *dev, struct page *page,
76 unsigned long offset, size_t size,
77 enum dma_data_direction dir,
78 struct dma_attrs *attrs)
79{
80 if (dir != DMA_TO_DEVICE)
81 mvebu_hwcc_sync_io_barrier();
82 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
83}
84
85
86static void mvebu_hwcc_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
87 size_t size, enum dma_data_direction dir,
88 struct dma_attrs *attrs)
89{
90 if (dir != DMA_TO_DEVICE)
91 mvebu_hwcc_sync_io_barrier();
92}
93
94static void mvebu_hwcc_dma_sync(struct device *dev, dma_addr_t dma_handle,
95 size_t size, enum dma_data_direction dir)
96{
97 if (dir != DMA_TO_DEVICE)
98 mvebu_hwcc_sync_io_barrier();
99}
100
101static struct dma_map_ops mvebu_hwcc_dma_ops = {
102 .alloc = arm_dma_alloc,
103 .free = arm_dma_free,
104 .mmap = arm_dma_mmap,
105 .map_page = mvebu_hwcc_dma_map_page,
106 .unmap_page = mvebu_hwcc_dma_unmap_page,
107 .get_sgtable = arm_dma_get_sgtable,
108 .map_sg = arm_dma_map_sg,
109 .unmap_sg = arm_dma_unmap_sg,
110 .sync_single_for_cpu = mvebu_hwcc_dma_sync,
111 .sync_single_for_device = mvebu_hwcc_dma_sync,
112 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
113 .sync_sg_for_device = arm_dma_sync_sg_for_device,
114 .set_dma_mask = arm_dma_set_mask,
115};
116
117static int mvebu_hwcc_platform_notifier(struct notifier_block *nb,
118 unsigned long event, void *__dev)
119{
120 struct device *dev = __dev;
121
122 if (event != BUS_NOTIFY_ADD_DEVICE)
123 return NOTIFY_DONE;
124 set_dma_ops(dev, &mvebu_hwcc_dma_ops);
125
126 return NOTIFY_OK;
127}
128
129static struct notifier_block mvebu_hwcc_platform_nb = {
130 .notifier_call = mvebu_hwcc_platform_notifier,
131};
132
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200133static void __init armada_370_coherency_init(struct device_node *np)
134{
135 struct resource res;
136
137 of_address_to_resource(np, 0, &res);
138 coherency_phys_base = res.start;
139 /*
140 * Ensure secondary CPUs will see the updated value,
141 * which they read before they join the coherency
142 * fabric, and therefore before they are coherent with
143 * the boot CPU cache.
144 */
145 sync_cache_w(&coherency_phys_base);
146 coherency_base = of_iomap(np, 0);
147 coherency_cpu_base = of_iomap(np, 1);
148 set_cpu_coherent(cpu_logical_map(smp_processor_id()), 0);
149}
150
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200151static void __init armada_375_coherency_init(struct device_node *np)
152{
153 coherency_cpu_base = of_iomap(np, 0);
154}
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;
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300160
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200161 np = of_find_matching_node_and_match(NULL, of_coherency_table, &match);
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300162 if (np) {
Thomas Petazzoni5fbba082014-04-14 15:47:02 +0200163 int type = (int) match->data;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200164
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200165 /* Armada 370/XP coherency works in both UP and SMP */
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200166 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200167 return type;
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200168
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200169 /* Armada 375 coherency works only on SMP */
170 else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375 && is_smp())
171 return type;
172
Jisheng Zhangabe511a2013-08-27 12:41:14 +0800173 of_node_put(np);
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300174 }
175
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200176 return COHERENCY_FABRIC_TYPE_NONE;
177}
178
179int coherency_available(void)
180{
181 return coherency_type() != COHERENCY_FABRIC_TYPE_NONE;
182}
183
184int __init coherency_init(void)
185{
186 int type = coherency_type();
187 struct device_node *np;
188
189 np = of_find_matching_node(NULL, of_coherency_table);
190
191 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
192 armada_370_coherency_init(np);
Thomas Petazzoni77fa4b92014-04-14 15:47:04 +0200193 else if (type == COHERENCY_FABRIC_TYPE_ARMADA_375)
194 armada_375_coherency_init(np);
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200195
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300196 return 0;
197}
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200198
199static int __init coherency_late_init(void)
200{
Thomas Petazzoni501f9282014-04-14 15:47:00 +0200201 if (coherency_available())
Thomas Petazzoni1919bff2013-06-20 09:45:26 +0200202 bus_register_notifier(&platform_bus_type,
203 &mvebu_hwcc_platform_nb);
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200204 return 0;
205}
206
207postcore_initcall(coherency_late_init);