blob: 434cf5f90a80ea5d0d07582c7e7cb5e8d544ef1d [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 {
42 COHERENCY_FABRIC_TYPE_ARMADA_370_XP,
43};
44
Gregory CLEMENT009f1312012-08-02 11:16:29 +030045static struct of_device_id of_coherency_table[] = {
Thomas Petazzoni924d38f2014-04-14 15:46:59 +020046 {.compatible = "marvell,coherency-fabric",
47 .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_370_XP },
Gregory CLEMENT009f1312012-08-02 11:16:29 +030048 { /* end of list */ },
49};
50
Gregory CLEMENT009f1312012-08-02 11:16:29 +030051/* Function defined in coherency_ll.S */
52int ll_set_cpu_coherent(void __iomem *base_addr, unsigned int hw_cpu_id);
53
54int set_cpu_coherent(unsigned int hw_cpu_id, int smp_group_id)
55{
56 if (!coherency_base) {
57 pr_warn("Can't make CPU %d cache coherent.\n", hw_cpu_id);
58 pr_warn("Coherency fabric is not initialized\n");
59 return 1;
60 }
61
62 return ll_set_cpu_coherent(coherency_base, hw_cpu_id);
63}
64
Gregory CLEMENTe60304f2012-10-12 19:20:36 +020065static inline void mvebu_hwcc_sync_io_barrier(void)
66{
67 writel(0x1, coherency_cpu_base + IO_SYNC_BARRIER_CTL_OFFSET);
68 while (readl(coherency_cpu_base + IO_SYNC_BARRIER_CTL_OFFSET) & 0x1);
69}
70
71static dma_addr_t mvebu_hwcc_dma_map_page(struct device *dev, struct page *page,
72 unsigned long offset, size_t size,
73 enum dma_data_direction dir,
74 struct dma_attrs *attrs)
75{
76 if (dir != DMA_TO_DEVICE)
77 mvebu_hwcc_sync_io_barrier();
78 return pfn_to_dma(dev, page_to_pfn(page)) + offset;
79}
80
81
82static void mvebu_hwcc_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
83 size_t size, enum dma_data_direction dir,
84 struct dma_attrs *attrs)
85{
86 if (dir != DMA_TO_DEVICE)
87 mvebu_hwcc_sync_io_barrier();
88}
89
90static void mvebu_hwcc_dma_sync(struct device *dev, dma_addr_t dma_handle,
91 size_t size, enum dma_data_direction dir)
92{
93 if (dir != DMA_TO_DEVICE)
94 mvebu_hwcc_sync_io_barrier();
95}
96
97static struct dma_map_ops mvebu_hwcc_dma_ops = {
98 .alloc = arm_dma_alloc,
99 .free = arm_dma_free,
100 .mmap = arm_dma_mmap,
101 .map_page = mvebu_hwcc_dma_map_page,
102 .unmap_page = mvebu_hwcc_dma_unmap_page,
103 .get_sgtable = arm_dma_get_sgtable,
104 .map_sg = arm_dma_map_sg,
105 .unmap_sg = arm_dma_unmap_sg,
106 .sync_single_for_cpu = mvebu_hwcc_dma_sync,
107 .sync_single_for_device = mvebu_hwcc_dma_sync,
108 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
109 .sync_sg_for_device = arm_dma_sync_sg_for_device,
110 .set_dma_mask = arm_dma_set_mask,
111};
112
113static int mvebu_hwcc_platform_notifier(struct notifier_block *nb,
114 unsigned long event, void *__dev)
115{
116 struct device *dev = __dev;
117
118 if (event != BUS_NOTIFY_ADD_DEVICE)
119 return NOTIFY_DONE;
120 set_dma_ops(dev, &mvebu_hwcc_dma_ops);
121
122 return NOTIFY_OK;
123}
124
125static struct notifier_block mvebu_hwcc_platform_nb = {
126 .notifier_call = mvebu_hwcc_platform_notifier,
127};
128
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200129static void __init armada_370_coherency_init(struct device_node *np)
130{
131 struct resource res;
132
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);
144 set_cpu_coherent(cpu_logical_map(smp_processor_id()), 0);
145}
146
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300147int __init coherency_init(void)
148{
149 struct device_node *np;
150
151 np = of_find_matching_node(NULL, of_coherency_table);
152 if (np) {
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200153 const struct of_device_id *match =
154 of_match_node(of_coherency_table, np);
155 int type;
156
157 type = (int) match->data;
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300158 pr_info("Initializing Coherency fabric\n");
Thomas Petazzoni924d38f2014-04-14 15:46:59 +0200159
160 if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP)
161 armada_370_coherency_init(np);
162
Jisheng Zhangabe511a2013-08-27 12:41:14 +0800163 of_node_put(np);
Gregory CLEMENT009f1312012-08-02 11:16:29 +0300164 }
165
166 return 0;
167}
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200168
169static int __init coherency_late_init(void)
170{
Jisheng Zhangabe511a2013-08-27 12:41:14 +0800171 struct device_node *np;
172
173 np = of_find_matching_node(NULL, of_coherency_table);
174 if (np) {
Thomas Petazzoni1919bff2013-06-20 09:45:26 +0200175 bus_register_notifier(&platform_bus_type,
176 &mvebu_hwcc_platform_nb);
Jisheng Zhangabe511a2013-08-27 12:41:14 +0800177 of_node_put(np);
178 }
Thomas Petazzoni865e0522013-06-05 09:04:55 +0200179 return 0;
180}
181
182postcore_initcall(coherency_late_init);