Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 1 | /* |
| 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 CLEMENT | e60304f | 2012-10-12 19:20:36 +0200 | [diff] [blame] | 25 | #include <linux/dma-mapping.h> |
| 26 | #include <linux/platform_device.h> |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 27 | #include <asm/smp_plat.h> |
Thomas Petazzoni | 580ff0e | 2013-06-06 12:24:28 +0200 | [diff] [blame] | 28 | #include <asm/cacheflush.h> |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 29 | #include "armada-370-xp.h" |
Jisheng Zhang | b12634e | 2013-11-07 17:02:38 +0800 | [diff] [blame] | 30 | #include "coherency.h" |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 31 | |
Paul Gortmaker | 8bd26e3 | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 32 | unsigned long coherency_phys_base; |
Thomas Petazzoni | 865e052 | 2013-06-05 09:04:55 +0200 | [diff] [blame] | 33 | static void __iomem *coherency_base; |
Gregory CLEMENT | e60304f | 2012-10-12 19:20:36 +0200 | [diff] [blame] | 34 | static void __iomem *coherency_cpu_base; |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 35 | |
| 36 | /* Coherency fabric registers */ |
| 37 | #define COHERENCY_FABRIC_CFG_OFFSET 0x4 |
| 38 | |
Gregory CLEMENT | e60304f | 2012-10-12 19:20:36 +0200 | [diff] [blame] | 39 | #define IO_SYNC_BARRIER_CTL_OFFSET 0x0 |
| 40 | |
Thomas Petazzoni | 924d38f | 2014-04-14 15:46:59 +0200 | [diff] [blame] | 41 | enum { |
Thomas Petazzoni | 501f928 | 2014-04-14 15:47:00 +0200 | [diff] [blame] | 42 | COHERENCY_FABRIC_TYPE_NONE, |
Thomas Petazzoni | 924d38f | 2014-04-14 15:46:59 +0200 | [diff] [blame] | 43 | COHERENCY_FABRIC_TYPE_ARMADA_370_XP, |
| 44 | }; |
| 45 | |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 46 | static struct of_device_id of_coherency_table[] = { |
Thomas Petazzoni | 924d38f | 2014-04-14 15:46:59 +0200 | [diff] [blame] | 47 | {.compatible = "marvell,coherency-fabric", |
| 48 | .data = (void *) COHERENCY_FABRIC_TYPE_ARMADA_370_XP }, |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 49 | { /* end of list */ }, |
| 50 | }; |
| 51 | |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 52 | /* Function defined in coherency_ll.S */ |
| 53 | int ll_set_cpu_coherent(void __iomem *base_addr, unsigned int hw_cpu_id); |
| 54 | |
| 55 | int set_cpu_coherent(unsigned int hw_cpu_id, int smp_group_id) |
| 56 | { |
| 57 | if (!coherency_base) { |
| 58 | pr_warn("Can't make CPU %d cache coherent.\n", hw_cpu_id); |
| 59 | pr_warn("Coherency fabric is not initialized\n"); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | return ll_set_cpu_coherent(coherency_base, hw_cpu_id); |
| 64 | } |
| 65 | |
Gregory CLEMENT | e60304f | 2012-10-12 19:20:36 +0200 | [diff] [blame] | 66 | static inline void mvebu_hwcc_sync_io_barrier(void) |
| 67 | { |
| 68 | writel(0x1, coherency_cpu_base + IO_SYNC_BARRIER_CTL_OFFSET); |
| 69 | while (readl(coherency_cpu_base + IO_SYNC_BARRIER_CTL_OFFSET) & 0x1); |
| 70 | } |
| 71 | |
| 72 | static dma_addr_t mvebu_hwcc_dma_map_page(struct device *dev, struct page *page, |
| 73 | unsigned long offset, size_t size, |
| 74 | enum dma_data_direction dir, |
| 75 | struct dma_attrs *attrs) |
| 76 | { |
| 77 | if (dir != DMA_TO_DEVICE) |
| 78 | mvebu_hwcc_sync_io_barrier(); |
| 79 | return pfn_to_dma(dev, page_to_pfn(page)) + offset; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static void mvebu_hwcc_dma_unmap_page(struct device *dev, dma_addr_t dma_handle, |
| 84 | size_t size, enum dma_data_direction dir, |
| 85 | struct dma_attrs *attrs) |
| 86 | { |
| 87 | if (dir != DMA_TO_DEVICE) |
| 88 | mvebu_hwcc_sync_io_barrier(); |
| 89 | } |
| 90 | |
| 91 | static void mvebu_hwcc_dma_sync(struct device *dev, dma_addr_t dma_handle, |
| 92 | size_t size, enum dma_data_direction dir) |
| 93 | { |
| 94 | if (dir != DMA_TO_DEVICE) |
| 95 | mvebu_hwcc_sync_io_barrier(); |
| 96 | } |
| 97 | |
| 98 | static struct dma_map_ops mvebu_hwcc_dma_ops = { |
| 99 | .alloc = arm_dma_alloc, |
| 100 | .free = arm_dma_free, |
| 101 | .mmap = arm_dma_mmap, |
| 102 | .map_page = mvebu_hwcc_dma_map_page, |
| 103 | .unmap_page = mvebu_hwcc_dma_unmap_page, |
| 104 | .get_sgtable = arm_dma_get_sgtable, |
| 105 | .map_sg = arm_dma_map_sg, |
| 106 | .unmap_sg = arm_dma_unmap_sg, |
| 107 | .sync_single_for_cpu = mvebu_hwcc_dma_sync, |
| 108 | .sync_single_for_device = mvebu_hwcc_dma_sync, |
| 109 | .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu, |
| 110 | .sync_sg_for_device = arm_dma_sync_sg_for_device, |
| 111 | .set_dma_mask = arm_dma_set_mask, |
| 112 | }; |
| 113 | |
| 114 | static int mvebu_hwcc_platform_notifier(struct notifier_block *nb, |
| 115 | unsigned long event, void *__dev) |
| 116 | { |
| 117 | struct device *dev = __dev; |
| 118 | |
| 119 | if (event != BUS_NOTIFY_ADD_DEVICE) |
| 120 | return NOTIFY_DONE; |
| 121 | set_dma_ops(dev, &mvebu_hwcc_dma_ops); |
| 122 | |
| 123 | return NOTIFY_OK; |
| 124 | } |
| 125 | |
| 126 | static struct notifier_block mvebu_hwcc_platform_nb = { |
| 127 | .notifier_call = mvebu_hwcc_platform_notifier, |
| 128 | }; |
| 129 | |
Thomas Petazzoni | 924d38f | 2014-04-14 15:46:59 +0200 | [diff] [blame] | 130 | static void __init armada_370_coherency_init(struct device_node *np) |
| 131 | { |
| 132 | struct resource res; |
| 133 | |
| 134 | of_address_to_resource(np, 0, &res); |
| 135 | coherency_phys_base = res.start; |
| 136 | /* |
| 137 | * Ensure secondary CPUs will see the updated value, |
| 138 | * which they read before they join the coherency |
| 139 | * fabric, and therefore before they are coherent with |
| 140 | * the boot CPU cache. |
| 141 | */ |
| 142 | sync_cache_w(&coherency_phys_base); |
| 143 | coherency_base = of_iomap(np, 0); |
| 144 | coherency_cpu_base = of_iomap(np, 1); |
| 145 | set_cpu_coherent(cpu_logical_map(smp_processor_id()), 0); |
| 146 | } |
| 147 | |
Thomas Petazzoni | 501f928 | 2014-04-14 15:47:00 +0200 | [diff] [blame] | 148 | static int coherency_type(void) |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 149 | { |
| 150 | struct device_node *np; |
Thomas Petazzoni | 5fbba08 | 2014-04-14 15:47:02 +0200 | [diff] [blame^] | 151 | const struct of_device_id *match; |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 152 | |
Thomas Petazzoni | 5fbba08 | 2014-04-14 15:47:02 +0200 | [diff] [blame^] | 153 | np = of_find_matching_node_and_match(NULL, of_coherency_table, &match); |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 154 | if (np) { |
Thomas Petazzoni | 5fbba08 | 2014-04-14 15:47:02 +0200 | [diff] [blame^] | 155 | int type = (int) match->data; |
Thomas Petazzoni | 924d38f | 2014-04-14 15:46:59 +0200 | [diff] [blame] | 156 | |
Thomas Petazzoni | 501f928 | 2014-04-14 15:47:00 +0200 | [diff] [blame] | 157 | /* Armada 370/XP coherency works in both UP and SMP */ |
Thomas Petazzoni | 924d38f | 2014-04-14 15:46:59 +0200 | [diff] [blame] | 158 | if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP) |
Thomas Petazzoni | 501f928 | 2014-04-14 15:47:00 +0200 | [diff] [blame] | 159 | return type; |
Thomas Petazzoni | 924d38f | 2014-04-14 15:46:59 +0200 | [diff] [blame] | 160 | |
Jisheng Zhang | abe511a | 2013-08-27 12:41:14 +0800 | [diff] [blame] | 161 | of_node_put(np); |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 162 | } |
| 163 | |
Thomas Petazzoni | 501f928 | 2014-04-14 15:47:00 +0200 | [diff] [blame] | 164 | return COHERENCY_FABRIC_TYPE_NONE; |
| 165 | } |
| 166 | |
| 167 | int coherency_available(void) |
| 168 | { |
| 169 | return coherency_type() != COHERENCY_FABRIC_TYPE_NONE; |
| 170 | } |
| 171 | |
| 172 | int __init coherency_init(void) |
| 173 | { |
| 174 | int type = coherency_type(); |
| 175 | struct device_node *np; |
| 176 | |
| 177 | np = of_find_matching_node(NULL, of_coherency_table); |
| 178 | |
| 179 | if (type == COHERENCY_FABRIC_TYPE_ARMADA_370_XP) |
| 180 | armada_370_coherency_init(np); |
| 181 | |
Gregory CLEMENT | 009f131 | 2012-08-02 11:16:29 +0300 | [diff] [blame] | 182 | return 0; |
| 183 | } |
Thomas Petazzoni | 865e052 | 2013-06-05 09:04:55 +0200 | [diff] [blame] | 184 | |
| 185 | static int __init coherency_late_init(void) |
| 186 | { |
Thomas Petazzoni | 501f928 | 2014-04-14 15:47:00 +0200 | [diff] [blame] | 187 | if (coherency_available()) |
Thomas Petazzoni | 1919bff | 2013-06-20 09:45:26 +0200 | [diff] [blame] | 188 | bus_register_notifier(&platform_bus_type, |
| 189 | &mvebu_hwcc_platform_nb); |
Thomas Petazzoni | 865e052 | 2013-06-05 09:04:55 +0200 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | postcore_initcall(coherency_late_init); |