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