Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015-2016 MediaTek Inc. |
| 4 | * Author: Yong Wu <yong.wu@mediatek.com> |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 5 | */ |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 6 | #include <linux/memblock.h> |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 7 | #include <linux/bug.h> |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/component.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/dma-iommu.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/iommu.h> |
| 16 | #include <linux/iopoll.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/of_address.h> |
| 19 | #include <linux/of_iommu.h> |
| 20 | #include <linux/of_irq.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <asm/barrier.h> |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 26 | #include <soc/mediatek/smi.h> |
| 27 | |
Honghui Zhang | 9ca340c | 2016-06-08 17:50:58 +0800 | [diff] [blame] | 28 | #include "mtk_iommu.h" |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 29 | |
| 30 | #define REG_MMU_PT_BASE_ADDR 0x000 |
Yong Wu | 907ba6a | 2019-08-24 11:02:02 +0800 | [diff] [blame] | 31 | #define MMU_PT_ADDR_MASK GENMASK(31, 7) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 32 | |
| 33 | #define REG_MMU_INVALIDATE 0x020 |
| 34 | #define F_ALL_INVLD 0x2 |
| 35 | #define F_MMU_INV_RANGE 0x1 |
| 36 | |
| 37 | #define REG_MMU_INVLD_START_A 0x024 |
| 38 | #define REG_MMU_INVLD_END_A 0x028 |
| 39 | |
| 40 | #define REG_MMU_INV_SEL 0x038 |
| 41 | #define F_INVLD_EN0 BIT(0) |
| 42 | #define F_INVLD_EN1 BIT(1) |
| 43 | |
| 44 | #define REG_MMU_STANDARD_AXI_MODE 0x048 |
| 45 | #define REG_MMU_DCM_DIS 0x050 |
| 46 | |
| 47 | #define REG_MMU_CTRL_REG 0x110 |
Yong Wu | acb3c92 | 2019-08-24 11:01:58 +0800 | [diff] [blame] | 48 | #define F_MMU_TF_PROT_TO_PROGRAM_ADDR (2 << 4) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 49 | #define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4) |
Yong Wu | acb3c92 | 2019-08-24 11:01:58 +0800 | [diff] [blame] | 50 | #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173 (2 << 5) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 51 | |
| 52 | #define REG_MMU_IVRP_PADDR 0x114 |
Yong Wu | 70ca608 | 2018-03-18 09:52:54 +0800 | [diff] [blame] | 53 | |
Yong Wu | 30e2fcc | 2017-08-21 19:00:20 +0800 | [diff] [blame] | 54 | #define REG_MMU_VLD_PA_RNG 0x118 |
| 55 | #define F_MMU_VLD_PA_RNG(EA, SA) (((EA) << 8) | (SA)) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 56 | |
| 57 | #define REG_MMU_INT_CONTROL0 0x120 |
| 58 | #define F_L2_MULIT_HIT_EN BIT(0) |
| 59 | #define F_TABLE_WALK_FAULT_INT_EN BIT(1) |
| 60 | #define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2) |
| 61 | #define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3) |
| 62 | #define F_PREFETCH_FIFO_ERR_INT_EN BIT(5) |
| 63 | #define F_MISS_FIFO_ERR_INT_EN BIT(6) |
| 64 | #define F_INT_CLR_BIT BIT(12) |
| 65 | |
| 66 | #define REG_MMU_INT_MAIN_CONTROL 0x124 |
Yong Wu | 15a01f4 | 2019-08-24 11:02:03 +0800 | [diff] [blame] | 67 | /* mmu0 | mmu1 */ |
| 68 | #define F_INT_TRANSLATION_FAULT (BIT(0) | BIT(7)) |
| 69 | #define F_INT_MAIN_MULTI_HIT_FAULT (BIT(1) | BIT(8)) |
| 70 | #define F_INT_INVALID_PA_FAULT (BIT(2) | BIT(9)) |
| 71 | #define F_INT_ENTRY_REPLACEMENT_FAULT (BIT(3) | BIT(10)) |
| 72 | #define F_INT_TLB_MISS_FAULT (BIT(4) | BIT(11)) |
| 73 | #define F_INT_MISS_TRANSACTION_FIFO_FAULT (BIT(5) | BIT(12)) |
| 74 | #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT (BIT(6) | BIT(13)) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 75 | |
| 76 | #define REG_MMU_CPE_DONE 0x12C |
| 77 | |
| 78 | #define REG_MMU_FAULT_ST1 0x134 |
Yong Wu | 15a01f4 | 2019-08-24 11:02:03 +0800 | [diff] [blame] | 79 | #define F_REG_MMU0_FAULT_MASK GENMASK(6, 0) |
| 80 | #define F_REG_MMU1_FAULT_MASK GENMASK(13, 7) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 81 | |
Yong Wu | 15a01f4 | 2019-08-24 11:02:03 +0800 | [diff] [blame] | 82 | #define REG_MMU0_FAULT_VA 0x13c |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 83 | #define F_MMU_FAULT_VA_WRITE_BIT BIT(1) |
| 84 | #define F_MMU_FAULT_VA_LAYER_BIT BIT(0) |
| 85 | |
Yong Wu | 15a01f4 | 2019-08-24 11:02:03 +0800 | [diff] [blame] | 86 | #define REG_MMU0_INVLD_PA 0x140 |
| 87 | #define REG_MMU1_FAULT_VA 0x144 |
| 88 | #define REG_MMU1_INVLD_PA 0x148 |
| 89 | #define REG_MMU0_INT_ID 0x150 |
| 90 | #define REG_MMU1_INT_ID 0x154 |
| 91 | #define F_MMU_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7) |
| 92 | #define F_MMU_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 93 | |
| 94 | #define MTK_PROTECT_PA_ALIGN 128 |
| 95 | |
Yong Wu | a9467d9 | 2017-08-21 19:00:15 +0800 | [diff] [blame] | 96 | /* |
| 97 | * Get the local arbiter ID and the portid within the larb arbiter |
| 98 | * from mtk_m4u_id which is defined by MTK_M4U_ID. |
| 99 | */ |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 100 | #define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0xf) |
Yong Wu | a9467d9 | 2017-08-21 19:00:15 +0800 | [diff] [blame] | 101 | #define MTK_M4U_TO_PORT(id) ((id) & 0x1f) |
| 102 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 103 | struct mtk_iommu_domain { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 104 | struct io_pgtable_cfg cfg; |
| 105 | struct io_pgtable_ops *iop; |
| 106 | |
| 107 | struct iommu_domain domain; |
| 108 | }; |
| 109 | |
Arvind Yadav | b65f501 | 2018-10-18 19:13:38 +0800 | [diff] [blame] | 110 | static const struct iommu_ops mtk_iommu_ops; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 111 | |
Yong Wu | 76ce654 | 2019-08-24 11:01:50 +0800 | [diff] [blame] | 112 | /* |
| 113 | * In M4U 4GB mode, the physical address is remapped as below: |
| 114 | * |
| 115 | * CPU Physical address: |
| 116 | * ==================== |
| 117 | * |
| 118 | * 0 1G 2G 3G 4G 5G |
| 119 | * |---A---|---B---|---C---|---D---|---E---| |
| 120 | * +--I/O--+------------Memory-------------+ |
| 121 | * |
| 122 | * IOMMU output physical address: |
| 123 | * ============================= |
| 124 | * |
| 125 | * 4G 5G 6G 7G 8G |
| 126 | * |---E---|---B---|---C---|---D---| |
| 127 | * +------------Memory-------------+ |
| 128 | * |
| 129 | * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the |
| 130 | * bit32 of the CPU physical address always is needed to set, and for Region |
| 131 | * 'E', the CPU physical address keep as is. |
| 132 | * Additionally, The iommu consumers always use the CPU phyiscal address. |
| 133 | */ |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 134 | #define MTK_IOMMU_4GB_MODE_REMAP_BASE 0x140000000UL |
Yong Wu | 76ce654 | 2019-08-24 11:01:50 +0800 | [diff] [blame] | 135 | |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 136 | static LIST_HEAD(m4ulist); /* List all the M4U HWs */ |
| 137 | |
| 138 | #define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list) |
| 139 | |
| 140 | /* |
| 141 | * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain |
| 142 | * for the performance. |
| 143 | * |
| 144 | * Here always return the mtk_iommu_data of the first probed M4U where the |
| 145 | * iommu domain information is recorded. |
| 146 | */ |
| 147 | static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void) |
| 148 | { |
| 149 | struct mtk_iommu_data *data; |
| 150 | |
| 151 | for_each_m4u(data) |
| 152 | return data; |
| 153 | |
| 154 | return NULL; |
| 155 | } |
| 156 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 157 | static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom) |
| 158 | { |
| 159 | return container_of(dom, struct mtk_iommu_domain, domain); |
| 160 | } |
| 161 | |
| 162 | static void mtk_iommu_tlb_flush_all(void *cookie) |
| 163 | { |
| 164 | struct mtk_iommu_data *data = cookie; |
| 165 | |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 166 | for_each_m4u(data) { |
| 167 | writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, |
| 168 | data->base + REG_MMU_INV_SEL); |
| 169 | writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE); |
| 170 | wmb(); /* Make sure the tlb flush all done */ |
| 171 | } |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Yong Wu | 1f4fd62 | 2019-11-04 15:01:06 +0800 | [diff] [blame] | 174 | static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size, |
Yong Wu | 67caf7e | 2019-11-04 15:01:05 +0800 | [diff] [blame] | 175 | size_t granule, void *cookie) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 176 | { |
| 177 | struct mtk_iommu_data *data = cookie; |
Yong Wu | 1f4fd62 | 2019-11-04 15:01:06 +0800 | [diff] [blame] | 178 | unsigned long flags; |
| 179 | int ret; |
| 180 | u32 tmp; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 181 | |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 182 | for_each_m4u(data) { |
Yong Wu | 1f4fd62 | 2019-11-04 15:01:06 +0800 | [diff] [blame] | 183 | spin_lock_irqsave(&data->tlb_lock, flags); |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 184 | writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, |
| 185 | data->base + REG_MMU_INV_SEL); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 186 | |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 187 | writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A); |
| 188 | writel_relaxed(iova + size - 1, |
| 189 | data->base + REG_MMU_INVLD_END_A); |
| 190 | writel_relaxed(F_MMU_INV_RANGE, |
| 191 | data->base + REG_MMU_INVALIDATE); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 192 | |
Yong Wu | 1f4fd62 | 2019-11-04 15:01:06 +0800 | [diff] [blame] | 193 | /* tlb sync */ |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 194 | ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE, |
Yong Wu | c90ae4a | 2019-11-04 15:01:08 +0800 | [diff] [blame] | 195 | tmp, tmp != 0, 10, 1000); |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 196 | if (ret) { |
| 197 | dev_warn(data->dev, |
| 198 | "Partial TLB flush timed out, falling back to full flush\n"); |
| 199 | mtk_iommu_tlb_flush_all(cookie); |
| 200 | } |
| 201 | /* Clear the CPE status */ |
| 202 | writel_relaxed(0, data->base + REG_MMU_CPE_DONE); |
Yong Wu | 1f4fd62 | 2019-11-04 15:01:06 +0800 | [diff] [blame] | 203 | spin_unlock_irqrestore(&data->tlb_lock, flags); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 204 | } |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 205 | } |
| 206 | |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 207 | static void mtk_iommu_tlb_flush_page_nosync(struct iommu_iotlb_gather *gather, |
| 208 | unsigned long iova, size_t granule, |
Will Deacon | abfd6fe | 2019-07-02 16:44:41 +0100 | [diff] [blame] | 209 | void *cookie) |
| 210 | { |
Yong Wu | da3cc91 | 2019-11-04 15:01:03 +0800 | [diff] [blame] | 211 | struct mtk_iommu_data *data = cookie; |
Yong Wu | a7a04ea | 2019-11-04 15:01:04 +0800 | [diff] [blame] | 212 | struct iommu_domain *domain = &data->m4u_dom->domain; |
Yong Wu | da3cc91 | 2019-11-04 15:01:03 +0800 | [diff] [blame] | 213 | |
Yong Wu | a7a04ea | 2019-11-04 15:01:04 +0800 | [diff] [blame] | 214 | iommu_iotlb_gather_add_page(domain, gather, iova, granule); |
Will Deacon | abfd6fe | 2019-07-02 16:44:41 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Will Deacon | 298f7889 | 2019-07-02 16:43:34 +0100 | [diff] [blame] | 217 | static const struct iommu_flush_ops mtk_iommu_flush_ops = { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 218 | .tlb_flush_all = mtk_iommu_tlb_flush_all, |
Yong Wu | 1f4fd62 | 2019-11-04 15:01:06 +0800 | [diff] [blame] | 219 | .tlb_flush_walk = mtk_iommu_tlb_flush_range_sync, |
| 220 | .tlb_flush_leaf = mtk_iommu_tlb_flush_range_sync, |
Will Deacon | abfd6fe | 2019-07-02 16:44:41 +0100 | [diff] [blame] | 221 | .tlb_add_page = mtk_iommu_tlb_flush_page_nosync, |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | static irqreturn_t mtk_iommu_isr(int irq, void *dev_id) |
| 225 | { |
| 226 | struct mtk_iommu_data *data = dev_id; |
| 227 | struct mtk_iommu_domain *dom = data->m4u_dom; |
| 228 | u32 int_state, regval, fault_iova, fault_pa; |
| 229 | unsigned int fault_larb, fault_port; |
| 230 | bool layer, write; |
| 231 | |
| 232 | /* Read error info from registers */ |
| 233 | int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1); |
Yong Wu | 15a01f4 | 2019-08-24 11:02:03 +0800 | [diff] [blame] | 234 | if (int_state & F_REG_MMU0_FAULT_MASK) { |
| 235 | regval = readl_relaxed(data->base + REG_MMU0_INT_ID); |
| 236 | fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA); |
| 237 | fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA); |
| 238 | } else { |
| 239 | regval = readl_relaxed(data->base + REG_MMU1_INT_ID); |
| 240 | fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA); |
| 241 | fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA); |
| 242 | } |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 243 | layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT; |
| 244 | write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT; |
Yong Wu | 15a01f4 | 2019-08-24 11:02:03 +0800 | [diff] [blame] | 245 | fault_larb = F_MMU_INT_ID_LARB_ID(regval); |
| 246 | fault_port = F_MMU_INT_ID_PORT_ID(regval); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 247 | |
Yong Wu | b3e5eee7 | 2019-08-24 11:01:57 +0800 | [diff] [blame] | 248 | fault_larb = data->plat_data->larbid_remap[fault_larb]; |
| 249 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 250 | if (report_iommu_fault(&dom->domain, data->dev, fault_iova, |
| 251 | write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) { |
| 252 | dev_err_ratelimited( |
| 253 | data->dev, |
| 254 | "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n", |
| 255 | int_state, fault_iova, fault_pa, fault_larb, fault_port, |
| 256 | layer, write ? "write" : "read"); |
| 257 | } |
| 258 | |
| 259 | /* Interrupt clear */ |
| 260 | regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0); |
| 261 | regval |= F_INT_CLR_BIT; |
| 262 | writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0); |
| 263 | |
| 264 | mtk_iommu_tlb_flush_all(data); |
| 265 | |
| 266 | return IRQ_HANDLED; |
| 267 | } |
| 268 | |
| 269 | static void mtk_iommu_config(struct mtk_iommu_data *data, |
| 270 | struct device *dev, bool enable) |
| 271 | { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 272 | struct mtk_smi_larb_iommu *larb_mmu; |
| 273 | unsigned int larbid, portid; |
Joerg Roedel | a9bf2ee | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 274 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 275 | int i; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 276 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 277 | for (i = 0; i < fwspec->num_ids; ++i) { |
| 278 | larbid = MTK_M4U_TO_LARB(fwspec->ids[i]); |
| 279 | portid = MTK_M4U_TO_PORT(fwspec->ids[i]); |
Yong Wu | 1ee9feb | 2019-08-24 11:02:08 +0800 | [diff] [blame] | 280 | larb_mmu = &data->larb_imu[larbid]; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 281 | |
| 282 | dev_dbg(dev, "%s iommu port: %d\n", |
| 283 | enable ? "enable" : "disable", portid); |
| 284 | |
| 285 | if (enable) |
| 286 | larb_mmu->mmu |= MTK_SMI_MMU_EN(portid); |
| 287 | else |
| 288 | larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid); |
| 289 | } |
| 290 | } |
| 291 | |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 292 | static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 293 | { |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 294 | struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 295 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 296 | dom->cfg = (struct io_pgtable_cfg) { |
| 297 | .quirks = IO_PGTABLE_QUIRK_ARM_NS | |
| 298 | IO_PGTABLE_QUIRK_NO_PERMS | |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 299 | IO_PGTABLE_QUIRK_TLBI_ON_MAP | |
| 300 | IO_PGTABLE_QUIRK_ARM_MTK_EXT, |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 301 | .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap, |
| 302 | .ias = 32, |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 303 | .oas = 34, |
Will Deacon | 298f7889 | 2019-07-02 16:43:34 +0100 | [diff] [blame] | 304 | .tlb = &mtk_iommu_flush_ops, |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 305 | .iommu_dev = data->dev, |
| 306 | }; |
| 307 | |
| 308 | dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data); |
| 309 | if (!dom->iop) { |
| 310 | dev_err(data->dev, "Failed to alloc io pgtable\n"); |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
| 314 | /* Update our support page sizes bitmap */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 315 | dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type) |
| 320 | { |
| 321 | struct mtk_iommu_domain *dom; |
| 322 | |
| 323 | if (type != IOMMU_DOMAIN_DMA) |
| 324 | return NULL; |
| 325 | |
| 326 | dom = kzalloc(sizeof(*dom), GFP_KERNEL); |
| 327 | if (!dom) |
| 328 | return NULL; |
| 329 | |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 330 | if (iommu_get_dma_cookie(&dom->domain)) |
| 331 | goto free_dom; |
| 332 | |
| 333 | if (mtk_iommu_domain_finalise(dom)) |
| 334 | goto put_dma_cookie; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 335 | |
| 336 | dom->domain.geometry.aperture_start = 0; |
| 337 | dom->domain.geometry.aperture_end = DMA_BIT_MASK(32); |
| 338 | dom->domain.geometry.force_aperture = true; |
| 339 | |
| 340 | return &dom->domain; |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 341 | |
| 342 | put_dma_cookie: |
| 343 | iommu_put_dma_cookie(&dom->domain); |
| 344 | free_dom: |
| 345 | kfree(dom); |
| 346 | return NULL; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static void mtk_iommu_domain_free(struct iommu_domain *domain) |
| 350 | { |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 351 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
| 352 | |
| 353 | free_io_pgtable_ops(dom->iop); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 354 | iommu_put_dma_cookie(domain); |
| 355 | kfree(to_mtk_domain(domain)); |
| 356 | } |
| 357 | |
| 358 | static int mtk_iommu_attach_device(struct iommu_domain *domain, |
| 359 | struct device *dev) |
| 360 | { |
Joerg Roedel | 3524b55 | 2020-03-26 16:08:38 +0100 | [diff] [blame^] | 361 | struct mtk_iommu_data *data = dev_iommu_priv_get(dev); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 362 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 363 | |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 364 | if (!data) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 365 | return -ENODEV; |
| 366 | |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 367 | /* Update the pgtable base address register of the M4U HW */ |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 368 | if (!data->m4u_dom) { |
| 369 | data->m4u_dom = dom; |
Robin Murphy | d1e5f26 | 2019-10-25 19:08:37 +0100 | [diff] [blame] | 370 | writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 371 | data->base + REG_MMU_PT_BASE_ADDR); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 372 | } |
| 373 | |
Yong Wu | 4b00f5a | 2017-08-21 19:00:18 +0800 | [diff] [blame] | 374 | mtk_iommu_config(data, dev, true); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static void mtk_iommu_detach_device(struct iommu_domain *domain, |
| 379 | struct device *dev) |
| 380 | { |
Joerg Roedel | 3524b55 | 2020-03-26 16:08:38 +0100 | [diff] [blame^] | 381 | struct mtk_iommu_data *data = dev_iommu_priv_get(dev); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 382 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 383 | if (!data) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 384 | return; |
| 385 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 386 | mtk_iommu_config(data, dev, false); |
| 387 | } |
| 388 | |
| 389 | static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova, |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 390 | phys_addr_t paddr, size_t size, int prot, gfp_t gfp) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 391 | { |
| 392 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 393 | struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 394 | |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 395 | /* The "4GB mode" M4U physically can not use the lower remap of Dram. */ |
| 396 | if (data->enable_4GB) |
| 397 | paddr |= BIT_ULL(32); |
| 398 | |
Yong Wu | 60829b4 | 2019-11-04 15:01:07 +0800 | [diff] [blame] | 399 | /* Synchronize with the tlb_lock */ |
| 400 | return dom->iop->map(dom->iop, iova, paddr, size, prot); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static size_t mtk_iommu_unmap(struct iommu_domain *domain, |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 404 | unsigned long iova, size_t size, |
| 405 | struct iommu_iotlb_gather *gather) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 406 | { |
| 407 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 408 | |
Yong Wu | 60829b4 | 2019-11-04 15:01:07 +0800 | [diff] [blame] | 409 | return dom->iop->unmap(dom->iop, iova, size, gather); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 410 | } |
| 411 | |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 412 | static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain) |
| 413 | { |
Yong Wu | 2009122 | 2019-11-04 15:01:02 +0800 | [diff] [blame] | 414 | mtk_iommu_tlb_flush_all(mtk_iommu_get_m4u_data()); |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static void mtk_iommu_iotlb_sync(struct iommu_domain *domain, |
| 418 | struct iommu_iotlb_gather *gather) |
Robin Murphy | 4d689b6 | 2017-09-28 15:55:02 +0100 | [diff] [blame] | 419 | { |
Yong Wu | da3cc91 | 2019-11-04 15:01:03 +0800 | [diff] [blame] | 420 | struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); |
Yong Wu | a7a04ea | 2019-11-04 15:01:04 +0800 | [diff] [blame] | 421 | size_t length = gather->end - gather->start; |
Yong Wu | da3cc91 | 2019-11-04 15:01:03 +0800 | [diff] [blame] | 422 | |
Yong Wu | a7a04ea | 2019-11-04 15:01:04 +0800 | [diff] [blame] | 423 | if (gather->start == ULONG_MAX) |
| 424 | return; |
| 425 | |
Yong Wu | 1f4fd62 | 2019-11-04 15:01:06 +0800 | [diff] [blame] | 426 | mtk_iommu_tlb_flush_range_sync(gather->start, length, gather->pgsize, |
Yong Wu | 67caf7e | 2019-11-04 15:01:05 +0800 | [diff] [blame] | 427 | data); |
Robin Murphy | 4d689b6 | 2017-09-28 15:55:02 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 430 | static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain, |
| 431 | dma_addr_t iova) |
| 432 | { |
| 433 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
Yong Wu | 30e2fcc | 2017-08-21 19:00:20 +0800 | [diff] [blame] | 434 | struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 435 | phys_addr_t pa; |
| 436 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 437 | pa = dom->iop->iova_to_phys(dom->iop, iova); |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 438 | if (data->enable_4GB && pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE) |
| 439 | pa &= ~BIT_ULL(32); |
Yong Wu | 30e2fcc | 2017-08-21 19:00:20 +0800 | [diff] [blame] | 440 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 441 | return pa; |
| 442 | } |
| 443 | |
| 444 | static int mtk_iommu_add_device(struct device *dev) |
| 445 | { |
Joerg Roedel | a9bf2ee | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 446 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 447 | struct mtk_iommu_data *data; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 448 | struct iommu_group *group; |
| 449 | |
Joerg Roedel | a9bf2ee | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 450 | if (!fwspec || fwspec->ops != &mtk_iommu_ops) |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 451 | return -ENODEV; /* Not a iommu client device */ |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 452 | |
Joerg Roedel | 3524b55 | 2020-03-26 16:08:38 +0100 | [diff] [blame^] | 453 | data = dev_iommu_priv_get(dev); |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 454 | iommu_device_link(&data->iommu, dev); |
| 455 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 456 | group = iommu_group_get_for_dev(dev); |
| 457 | if (IS_ERR(group)) |
| 458 | return PTR_ERR(group); |
| 459 | |
| 460 | iommu_group_put(group); |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static void mtk_iommu_remove_device(struct device *dev) |
| 465 | { |
Joerg Roedel | a9bf2ee | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 466 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 467 | struct mtk_iommu_data *data; |
| 468 | |
Joerg Roedel | a9bf2ee | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 469 | if (!fwspec || fwspec->ops != &mtk_iommu_ops) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 470 | return; |
| 471 | |
Joerg Roedel | 3524b55 | 2020-03-26 16:08:38 +0100 | [diff] [blame^] | 472 | data = dev_iommu_priv_get(dev); |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 473 | iommu_device_unlink(&data->iommu, dev); |
| 474 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 475 | iommu_group_remove_device(dev); |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 476 | iommu_fwspec_free(dev); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static struct iommu_group *mtk_iommu_device_group(struct device *dev) |
| 480 | { |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 481 | struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 482 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 483 | if (!data) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 484 | return ERR_PTR(-ENODEV); |
| 485 | |
| 486 | /* All the client devices are in the same m4u iommu-group */ |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 487 | if (!data->m4u_group) { |
| 488 | data->m4u_group = iommu_group_alloc(); |
| 489 | if (IS_ERR(data->m4u_group)) |
| 490 | dev_err(dev, "Failed to allocate M4U IOMMU group\n"); |
Robin Murphy | 3a8d40b | 2016-11-11 17:59:24 +0000 | [diff] [blame] | 491 | } else { |
| 492 | iommu_group_ref_get(data->m4u_group); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 493 | } |
| 494 | return data->m4u_group; |
| 495 | } |
| 496 | |
| 497 | static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args) |
| 498 | { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 499 | struct platform_device *m4updev; |
| 500 | |
| 501 | if (args->args_count != 1) { |
| 502 | dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n", |
| 503 | args->args_count); |
| 504 | return -EINVAL; |
| 505 | } |
| 506 | |
Joerg Roedel | 3524b55 | 2020-03-26 16:08:38 +0100 | [diff] [blame^] | 507 | if (!dev_iommu_priv_get(dev)) { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 508 | /* Get the m4u device */ |
| 509 | m4updev = of_find_device_by_node(args->np); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 510 | if (WARN_ON(!m4updev)) |
| 511 | return -EINVAL; |
| 512 | |
Joerg Roedel | 3524b55 | 2020-03-26 16:08:38 +0100 | [diff] [blame^] | 513 | dev_iommu_priv_set(dev, platform_get_drvdata(m4updev)); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 514 | } |
| 515 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 516 | return iommu_fwspec_add_ids(dev, args->args, 1); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 517 | } |
| 518 | |
Arvind Yadav | b65f501 | 2018-10-18 19:13:38 +0800 | [diff] [blame] | 519 | static const struct iommu_ops mtk_iommu_ops = { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 520 | .domain_alloc = mtk_iommu_domain_alloc, |
| 521 | .domain_free = mtk_iommu_domain_free, |
| 522 | .attach_dev = mtk_iommu_attach_device, |
| 523 | .detach_dev = mtk_iommu_detach_device, |
| 524 | .map = mtk_iommu_map, |
| 525 | .unmap = mtk_iommu_unmap, |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 526 | .flush_iotlb_all = mtk_iommu_flush_iotlb_all, |
Robin Murphy | 4d689b6 | 2017-09-28 15:55:02 +0100 | [diff] [blame] | 527 | .iotlb_sync = mtk_iommu_iotlb_sync, |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 528 | .iova_to_phys = mtk_iommu_iova_to_phys, |
| 529 | .add_device = mtk_iommu_add_device, |
| 530 | .remove_device = mtk_iommu_remove_device, |
| 531 | .device_group = mtk_iommu_device_group, |
| 532 | .of_xlate = mtk_iommu_of_xlate, |
| 533 | .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M, |
| 534 | }; |
| 535 | |
| 536 | static int mtk_iommu_hw_init(const struct mtk_iommu_data *data) |
| 537 | { |
| 538 | u32 regval; |
| 539 | int ret; |
| 540 | |
| 541 | ret = clk_prepare_enable(data->bclk); |
| 542 | if (ret) { |
| 543 | dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret); |
| 544 | return ret; |
| 545 | } |
| 546 | |
Yong Wu | cecdce9 | 2019-08-24 11:01:47 +0800 | [diff] [blame] | 547 | if (data->plat_data->m4u_plat == M4U_MT8173) |
Yong Wu | acb3c92 | 2019-08-24 11:01:58 +0800 | [diff] [blame] | 548 | regval = F_MMU_PREFETCH_RT_REPLACE_MOD | |
| 549 | F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173; |
| 550 | else |
| 551 | regval = F_MMU_TF_PROT_TO_PROGRAM_ADDR; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 552 | writel_relaxed(regval, data->base + REG_MMU_CTRL_REG); |
| 553 | |
| 554 | regval = F_L2_MULIT_HIT_EN | |
| 555 | F_TABLE_WALK_FAULT_INT_EN | |
| 556 | F_PREETCH_FIFO_OVERFLOW_INT_EN | |
| 557 | F_MISS_FIFO_OVERFLOW_INT_EN | |
| 558 | F_PREFETCH_FIFO_ERR_INT_EN | |
| 559 | F_MISS_FIFO_ERR_INT_EN; |
| 560 | writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0); |
| 561 | |
| 562 | regval = F_INT_TRANSLATION_FAULT | |
| 563 | F_INT_MAIN_MULTI_HIT_FAULT | |
| 564 | F_INT_INVALID_PA_FAULT | |
| 565 | F_INT_ENTRY_REPLACEMENT_FAULT | |
| 566 | F_INT_TLB_MISS_FAULT | |
| 567 | F_INT_MISS_TRANSACTION_FIFO_FAULT | |
| 568 | F_INT_PRETETCH_TRANSATION_FIFO_FAULT; |
| 569 | writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL); |
| 570 | |
Yong Wu | cecdce9 | 2019-08-24 11:01:47 +0800 | [diff] [blame] | 571 | if (data->plat_data->m4u_plat == M4U_MT8173) |
Yong Wu | 70ca608 | 2018-03-18 09:52:54 +0800 | [diff] [blame] | 572 | regval = (data->protect_base >> 1) | (data->enable_4GB << 31); |
| 573 | else |
| 574 | regval = lower_32_bits(data->protect_base) | |
| 575 | upper_32_bits(data->protect_base); |
| 576 | writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR); |
| 577 | |
Yong Wu | 2b326d8 | 2019-08-24 11:02:00 +0800 | [diff] [blame] | 578 | if (data->enable_4GB && data->plat_data->has_vld_pa_rng) { |
Yong Wu | 30e2fcc | 2017-08-21 19:00:20 +0800 | [diff] [blame] | 579 | /* |
| 580 | * If 4GB mode is enabled, the validate PA range is from |
| 581 | * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30]. |
| 582 | */ |
| 583 | regval = F_MMU_VLD_PA_RNG(7, 4); |
| 584 | writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG); |
| 585 | } |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 586 | writel_relaxed(0, data->base + REG_MMU_DCM_DIS); |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 587 | |
Yong Wu | 50822b0 | 2019-08-24 11:01:59 +0800 | [diff] [blame] | 588 | if (data->plat_data->reset_axi) |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 589 | writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 590 | |
| 591 | if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0, |
| 592 | dev_name(data->dev), (void *)data)) { |
| 593 | writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR); |
| 594 | clk_disable_unprepare(data->bclk); |
| 595 | dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq); |
| 596 | return -ENODEV; |
| 597 | } |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 602 | static const struct component_master_ops mtk_iommu_com_ops = { |
| 603 | .bind = mtk_iommu_bind, |
| 604 | .unbind = mtk_iommu_unbind, |
| 605 | }; |
| 606 | |
| 607 | static int mtk_iommu_probe(struct platform_device *pdev) |
| 608 | { |
| 609 | struct mtk_iommu_data *data; |
| 610 | struct device *dev = &pdev->dev; |
| 611 | struct resource *res; |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 612 | resource_size_t ioaddr; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 613 | struct component_match *match = NULL; |
| 614 | void *protect; |
Andrzej Hajda | 0b6c0ad | 2016-03-01 10:36:23 +0100 | [diff] [blame] | 615 | int i, larb_nr, ret; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 616 | |
| 617 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 618 | if (!data) |
| 619 | return -ENOMEM; |
| 620 | data->dev = dev; |
Yong Wu | cecdce9 | 2019-08-24 11:01:47 +0800 | [diff] [blame] | 621 | data->plat_data = of_device_get_match_data(dev); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 622 | |
| 623 | /* Protect memory. HW will access here while translation fault.*/ |
| 624 | protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL); |
| 625 | if (!protect) |
| 626 | return -ENOMEM; |
| 627 | data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN); |
| 628 | |
Yong Wu | 01e23c9 | 2016-03-14 06:01:11 +0800 | [diff] [blame] | 629 | /* Whether the current dram is over 4GB */ |
Yong Wu | 4193998 | 2017-08-24 15:42:12 +0800 | [diff] [blame] | 630 | data->enable_4GB = !!(max_pfn > (BIT_ULL(32) >> PAGE_SHIFT)); |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 631 | if (!data->plat_data->has_4gb_mode) |
| 632 | data->enable_4GB = false; |
Yong Wu | 01e23c9 | 2016-03-14 06:01:11 +0800 | [diff] [blame] | 633 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 634 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 635 | data->base = devm_ioremap_resource(dev, res); |
| 636 | if (IS_ERR(data->base)) |
| 637 | return PTR_ERR(data->base); |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 638 | ioaddr = res->start; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 639 | |
| 640 | data->irq = platform_get_irq(pdev, 0); |
| 641 | if (data->irq < 0) |
| 642 | return data->irq; |
| 643 | |
Yong Wu | 2aa4c25 | 2019-08-24 11:01:56 +0800 | [diff] [blame] | 644 | if (data->plat_data->has_bclk) { |
| 645 | data->bclk = devm_clk_get(dev, "bclk"); |
| 646 | if (IS_ERR(data->bclk)) |
| 647 | return PTR_ERR(data->bclk); |
| 648 | } |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 649 | |
| 650 | larb_nr = of_count_phandle_with_args(dev->of_node, |
| 651 | "mediatek,larbs", NULL); |
| 652 | if (larb_nr < 0) |
| 653 | return larb_nr; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 654 | |
| 655 | for (i = 0; i < larb_nr; i++) { |
| 656 | struct device_node *larbnode; |
| 657 | struct platform_device *plarbdev; |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 658 | u32 id; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 659 | |
| 660 | larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i); |
| 661 | if (!larbnode) |
| 662 | return -EINVAL; |
| 663 | |
Wen Yang | 1eb8e4e | 2019-04-17 10:41:19 +0800 | [diff] [blame] | 664 | if (!of_device_is_available(larbnode)) { |
| 665 | of_node_put(larbnode); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 666 | continue; |
Wen Yang | 1eb8e4e | 2019-04-17 10:41:19 +0800 | [diff] [blame] | 667 | } |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 668 | |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 669 | ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id); |
| 670 | if (ret)/* The id is consecutive if there is no this property */ |
| 671 | id = i; |
| 672 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 673 | plarbdev = of_find_device_by_node(larbnode); |
Wen Yang | 1eb8e4e | 2019-04-17 10:41:19 +0800 | [diff] [blame] | 674 | if (!plarbdev) { |
| 675 | of_node_put(larbnode); |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 676 | return -EPROBE_DEFER; |
Wen Yang | 1eb8e4e | 2019-04-17 10:41:19 +0800 | [diff] [blame] | 677 | } |
Yong Wu | 1ee9feb | 2019-08-24 11:02:08 +0800 | [diff] [blame] | 678 | data->larb_imu[id].dev = &plarbdev->dev; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 679 | |
Russell King | 00c7c81 | 2016-10-19 11:30:34 +0100 | [diff] [blame] | 680 | component_match_add_release(dev, &match, release_of, |
| 681 | compare_of, larbnode); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | platform_set_drvdata(pdev, data); |
| 685 | |
| 686 | ret = mtk_iommu_hw_init(data); |
| 687 | if (ret) |
| 688 | return ret; |
| 689 | |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 690 | ret = iommu_device_sysfs_add(&data->iommu, dev, NULL, |
| 691 | "mtk-iommu.%pa", &ioaddr); |
| 692 | if (ret) |
| 693 | return ret; |
| 694 | |
| 695 | iommu_device_set_ops(&data->iommu, &mtk_iommu_ops); |
| 696 | iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode); |
| 697 | |
| 698 | ret = iommu_device_register(&data->iommu); |
| 699 | if (ret) |
| 700 | return ret; |
| 701 | |
Yong Wu | da3cc91 | 2019-11-04 15:01:03 +0800 | [diff] [blame] | 702 | spin_lock_init(&data->tlb_lock); |
Yong Wu | 7c3a2ec | 2017-08-21 19:00:17 +0800 | [diff] [blame] | 703 | list_add_tail(&data->list, &m4ulist); |
| 704 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 705 | if (!iommu_present(&platform_bus_type)) |
| 706 | bus_set_iommu(&platform_bus_type, &mtk_iommu_ops); |
| 707 | |
| 708 | return component_master_add_with_match(dev, &mtk_iommu_com_ops, match); |
| 709 | } |
| 710 | |
| 711 | static int mtk_iommu_remove(struct platform_device *pdev) |
| 712 | { |
| 713 | struct mtk_iommu_data *data = platform_get_drvdata(pdev); |
| 714 | |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 715 | iommu_device_sysfs_remove(&data->iommu); |
| 716 | iommu_device_unregister(&data->iommu); |
| 717 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 718 | if (iommu_present(&platform_bus_type)) |
| 719 | bus_set_iommu(&platform_bus_type, NULL); |
| 720 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 721 | clk_disable_unprepare(data->bclk); |
| 722 | devm_free_irq(&pdev->dev, data->irq, data); |
| 723 | component_master_del(&pdev->dev, &mtk_iommu_com_ops); |
| 724 | return 0; |
| 725 | } |
| 726 | |
Arnd Bergmann | fd99f79 | 2016-02-29 10:19:07 +0100 | [diff] [blame] | 727 | static int __maybe_unused mtk_iommu_suspend(struct device *dev) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 728 | { |
| 729 | struct mtk_iommu_data *data = dev_get_drvdata(dev); |
| 730 | struct mtk_iommu_suspend_reg *reg = &data->reg; |
| 731 | void __iomem *base = data->base; |
| 732 | |
| 733 | reg->standard_axi_mode = readl_relaxed(base + |
| 734 | REG_MMU_STANDARD_AXI_MODE); |
| 735 | reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS); |
| 736 | reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG); |
| 737 | reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0); |
| 738 | reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL); |
Yong Wu | 70ca608 | 2018-03-18 09:52:54 +0800 | [diff] [blame] | 739 | reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR); |
Yong Wu | b9475b3 | 2019-08-24 11:02:06 +0800 | [diff] [blame] | 740 | reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG); |
Yong Wu | 6254b64 | 2017-08-21 19:00:19 +0800 | [diff] [blame] | 741 | clk_disable_unprepare(data->bclk); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 742 | return 0; |
| 743 | } |
| 744 | |
Arnd Bergmann | fd99f79 | 2016-02-29 10:19:07 +0100 | [diff] [blame] | 745 | static int __maybe_unused mtk_iommu_resume(struct device *dev) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 746 | { |
| 747 | struct mtk_iommu_data *data = dev_get_drvdata(dev); |
| 748 | struct mtk_iommu_suspend_reg *reg = &data->reg; |
Yong Wu | 907ba6a | 2019-08-24 11:02:02 +0800 | [diff] [blame] | 749 | struct mtk_iommu_domain *m4u_dom = data->m4u_dom; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 750 | void __iomem *base = data->base; |
Yong Wu | 6254b64 | 2017-08-21 19:00:19 +0800 | [diff] [blame] | 751 | int ret; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 752 | |
Yong Wu | 6254b64 | 2017-08-21 19:00:19 +0800 | [diff] [blame] | 753 | ret = clk_prepare_enable(data->bclk); |
| 754 | if (ret) { |
| 755 | dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret); |
| 756 | return ret; |
| 757 | } |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 758 | writel_relaxed(reg->standard_axi_mode, |
| 759 | base + REG_MMU_STANDARD_AXI_MODE); |
| 760 | writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS); |
| 761 | writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG); |
| 762 | writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0); |
| 763 | writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL); |
Yong Wu | 70ca608 | 2018-03-18 09:52:54 +0800 | [diff] [blame] | 764 | writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR); |
Yong Wu | b9475b3 | 2019-08-24 11:02:06 +0800 | [diff] [blame] | 765 | writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG); |
Yong Wu | 907ba6a | 2019-08-24 11:02:02 +0800 | [diff] [blame] | 766 | if (m4u_dom) |
Robin Murphy | d1e5f26 | 2019-10-25 19:08:37 +0100 | [diff] [blame] | 767 | writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 768 | base + REG_MMU_PT_BASE_ADDR); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 769 | return 0; |
| 770 | } |
| 771 | |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 772 | static const struct dev_pm_ops mtk_iommu_pm_ops = { |
Yong Wu | 6254b64 | 2017-08-21 19:00:19 +0800 | [diff] [blame] | 773 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 774 | }; |
| 775 | |
Yong Wu | cecdce9 | 2019-08-24 11:01:47 +0800 | [diff] [blame] | 776 | static const struct mtk_iommu_plat_data mt2712_data = { |
| 777 | .m4u_plat = M4U_MT2712, |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 778 | .has_4gb_mode = true, |
Yong Wu | 2aa4c25 | 2019-08-24 11:01:56 +0800 | [diff] [blame] | 779 | .has_bclk = true, |
Yong Wu | 2b326d8 | 2019-08-24 11:02:00 +0800 | [diff] [blame] | 780 | .has_vld_pa_rng = true, |
Yong Wu | b3e5eee7 | 2019-08-24 11:01:57 +0800 | [diff] [blame] | 781 | .larbid_remap = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, |
Yong Wu | cecdce9 | 2019-08-24 11:01:47 +0800 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | static const struct mtk_iommu_plat_data mt8173_data = { |
| 785 | .m4u_plat = M4U_MT8173, |
Yong Wu | b4dad40 | 2019-08-24 11:01:55 +0800 | [diff] [blame] | 786 | .has_4gb_mode = true, |
Yong Wu | 2aa4c25 | 2019-08-24 11:01:56 +0800 | [diff] [blame] | 787 | .has_bclk = true, |
Yong Wu | 50822b0 | 2019-08-24 11:01:59 +0800 | [diff] [blame] | 788 | .reset_axi = true, |
Yong Wu | b3e5eee7 | 2019-08-24 11:01:57 +0800 | [diff] [blame] | 789 | .larbid_remap = {0, 1, 2, 3, 4, 5}, /* Linear mapping. */ |
Yong Wu | cecdce9 | 2019-08-24 11:01:47 +0800 | [diff] [blame] | 790 | }; |
| 791 | |
Yong Wu | 907ba6a | 2019-08-24 11:02:02 +0800 | [diff] [blame] | 792 | static const struct mtk_iommu_plat_data mt8183_data = { |
| 793 | .m4u_plat = M4U_MT8183, |
| 794 | .reset_axi = true, |
| 795 | .larbid_remap = {0, 4, 5, 6, 7, 2, 3, 1}, |
| 796 | }; |
| 797 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 798 | static const struct of_device_id mtk_iommu_of_ids[] = { |
Yong Wu | cecdce9 | 2019-08-24 11:01:47 +0800 | [diff] [blame] | 799 | { .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data}, |
| 800 | { .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data}, |
Yong Wu | 907ba6a | 2019-08-24 11:02:02 +0800 | [diff] [blame] | 801 | { .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data}, |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 802 | {} |
| 803 | }; |
| 804 | |
| 805 | static struct platform_driver mtk_iommu_driver = { |
| 806 | .probe = mtk_iommu_probe, |
| 807 | .remove = mtk_iommu_remove, |
| 808 | .driver = { |
| 809 | .name = "mtk-iommu", |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 810 | .of_match_table = of_match_ptr(mtk_iommu_of_ids), |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 811 | .pm = &mtk_iommu_pm_ops, |
| 812 | } |
| 813 | }; |
| 814 | |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 815 | static int __init mtk_iommu_init(void) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 816 | { |
| 817 | int ret; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 818 | |
| 819 | ret = platform_driver_register(&mtk_iommu_driver); |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 820 | if (ret != 0) |
| 821 | pr_err("Failed to register MTK IOMMU driver\n"); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 822 | |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 823 | return ret; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 824 | } |
| 825 | |
Yong Wu | e6dec92 | 2017-08-21 19:00:16 +0800 | [diff] [blame] | 826 | subsys_initcall(mtk_iommu_init) |