11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 20df4fabeSYong Wu /* 30df4fabeSYong Wu * Copyright (c) 2015-2016 MediaTek Inc. 40df4fabeSYong Wu * Author: Yong Wu <yong.wu@mediatek.com> 50df4fabeSYong Wu */ 6ef0f0986SYong Wu #include <linux/bitfield.h> 70df4fabeSYong Wu #include <linux/bug.h> 80df4fabeSYong Wu #include <linux/clk.h> 90df4fabeSYong Wu #include <linux/component.h> 100df4fabeSYong Wu #include <linux/device.h> 11803cf9e5SYong Wu #include <linux/dma-direct.h> 120df4fabeSYong Wu #include <linux/err.h> 130df4fabeSYong Wu #include <linux/interrupt.h> 140df4fabeSYong Wu #include <linux/io.h> 150df4fabeSYong Wu #include <linux/iommu.h> 160df4fabeSYong Wu #include <linux/iopoll.h> 170df4fabeSYong Wu #include <linux/list.h> 18c2c59456SMiles Chen #include <linux/mfd/syscon.h> 1918d8c74eSYong Wu #include <linux/module.h> 200df4fabeSYong Wu #include <linux/of_address.h> 210df4fabeSYong Wu #include <linux/of_irq.h> 220df4fabeSYong Wu #include <linux/of_platform.h> 230df4fabeSYong Wu #include <linux/platform_device.h> 24baf94e6eSYong Wu #include <linux/pm_runtime.h> 25c2c59456SMiles Chen #include <linux/regmap.h> 260df4fabeSYong Wu #include <linux/slab.h> 270df4fabeSYong Wu #include <linux/spinlock.h> 28c2c59456SMiles Chen #include <linux/soc/mediatek/infracfg.h> 290df4fabeSYong Wu #include <asm/barrier.h> 300df4fabeSYong Wu #include <soc/mediatek/smi.h> 310df4fabeSYong Wu 329ca340c9SHonghui Zhang #include "mtk_iommu.h" 330df4fabeSYong Wu 340df4fabeSYong Wu #define REG_MMU_PT_BASE_ADDR 0x000 35907ba6a1SYong Wu #define MMU_PT_ADDR_MASK GENMASK(31, 7) 360df4fabeSYong Wu 370df4fabeSYong Wu #define REG_MMU_INVALIDATE 0x020 380df4fabeSYong Wu #define F_ALL_INVLD 0x2 390df4fabeSYong Wu #define F_MMU_INV_RANGE 0x1 400df4fabeSYong Wu 410df4fabeSYong Wu #define REG_MMU_INVLD_START_A 0x024 420df4fabeSYong Wu #define REG_MMU_INVLD_END_A 0x028 430df4fabeSYong Wu 44068c86e9SChao Hao #define REG_MMU_INV_SEL_GEN2 0x02c 45b053bc71SChao Hao #define REG_MMU_INV_SEL_GEN1 0x038 460df4fabeSYong Wu #define F_INVLD_EN0 BIT(0) 470df4fabeSYong Wu #define F_INVLD_EN1 BIT(1) 480df4fabeSYong Wu 4975eed350SChao Hao #define REG_MMU_MISC_CTRL 0x048 504bb2bf4cSChao Hao #define F_MMU_IN_ORDER_WR_EN_MASK (BIT(1) | BIT(17)) 514bb2bf4cSChao Hao #define F_MMU_STANDARD_AXI_MODE_MASK (BIT(3) | BIT(19)) 524bb2bf4cSChao Hao 530df4fabeSYong Wu #define REG_MMU_DCM_DIS 0x050 5435c1b48dSChao Hao #define REG_MMU_WR_LEN_CTRL 0x054 5535c1b48dSChao Hao #define F_MMU_WR_THROT_DIS_MASK (BIT(5) | BIT(21)) 560df4fabeSYong Wu 570df4fabeSYong Wu #define REG_MMU_CTRL_REG 0x110 58acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR (2 << 4) 590df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4) 60acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173 (2 << 5) 610df4fabeSYong Wu 620df4fabeSYong Wu #define REG_MMU_IVRP_PADDR 0x114 6370ca608bSYong Wu 6430e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG 0x118 6530e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA) (((EA) << 8) | (SA)) 660df4fabeSYong Wu 670df4fabeSYong Wu #define REG_MMU_INT_CONTROL0 0x120 680df4fabeSYong Wu #define F_L2_MULIT_HIT_EN BIT(0) 690df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN BIT(1) 700df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2) 710df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3) 720df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN BIT(5) 730df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN BIT(6) 740df4fabeSYong Wu #define F_INT_CLR_BIT BIT(12) 750df4fabeSYong Wu 760df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL 0x124 7715a01f4cSYong Wu /* mmu0 | mmu1 */ 7815a01f4cSYong Wu #define F_INT_TRANSLATION_FAULT (BIT(0) | BIT(7)) 7915a01f4cSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT (BIT(1) | BIT(8)) 8015a01f4cSYong Wu #define F_INT_INVALID_PA_FAULT (BIT(2) | BIT(9)) 8115a01f4cSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT (BIT(3) | BIT(10)) 8215a01f4cSYong Wu #define F_INT_TLB_MISS_FAULT (BIT(4) | BIT(11)) 8315a01f4cSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT (BIT(5) | BIT(12)) 8415a01f4cSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT (BIT(6) | BIT(13)) 850df4fabeSYong Wu 860df4fabeSYong Wu #define REG_MMU_CPE_DONE 0x12C 870df4fabeSYong Wu 880df4fabeSYong Wu #define REG_MMU_FAULT_ST1 0x134 8915a01f4cSYong Wu #define F_REG_MMU0_FAULT_MASK GENMASK(6, 0) 9015a01f4cSYong Wu #define F_REG_MMU1_FAULT_MASK GENMASK(13, 7) 910df4fabeSYong Wu 9215a01f4cSYong Wu #define REG_MMU0_FAULT_VA 0x13c 93ef0f0986SYong Wu #define F_MMU_INVAL_VA_31_12_MASK GENMASK(31, 12) 94ef0f0986SYong Wu #define F_MMU_INVAL_VA_34_32_MASK GENMASK(11, 9) 95ef0f0986SYong Wu #define F_MMU_INVAL_PA_34_32_MASK GENMASK(8, 6) 960df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT BIT(1) 970df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT BIT(0) 980df4fabeSYong Wu 9915a01f4cSYong Wu #define REG_MMU0_INVLD_PA 0x140 10015a01f4cSYong Wu #define REG_MMU1_FAULT_VA 0x144 10115a01f4cSYong Wu #define REG_MMU1_INVLD_PA 0x148 10215a01f4cSYong Wu #define REG_MMU0_INT_ID 0x150 10315a01f4cSYong Wu #define REG_MMU1_INT_ID 0x154 10437276e00SChao Hao #define F_MMU_INT_ID_COMM_ID(a) (((a) >> 9) & 0x7) 10537276e00SChao Hao #define F_MMU_INT_ID_SUB_COMM_ID(a) (((a) >> 7) & 0x3) 10615a01f4cSYong Wu #define F_MMU_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7) 10715a01f4cSYong Wu #define F_MMU_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f) 1080df4fabeSYong Wu 109829316b3SChao Hao #define MTK_PROTECT_PA_ALIGN 256 1100df4fabeSYong Wu 1116b717796SChao Hao #define HAS_4GB_MODE BIT(0) 1126b717796SChao Hao /* HW will use the EMI clock if there isn't the "bclk". */ 1136b717796SChao Hao #define HAS_BCLK BIT(1) 1146b717796SChao Hao #define HAS_VLD_PA_RNG BIT(2) 1156b717796SChao Hao #define RESET_AXI BIT(3) 1164bb2bf4cSChao Hao #define OUT_ORDER_WR_EN BIT(4) 11737276e00SChao Hao #define HAS_SUB_COMM BIT(5) 11835c1b48dSChao Hao #define WR_THROT_EN BIT(6) 119d1b5ef00SFabien Parent #define HAS_LEGACY_IVRP_PADDR BIT(7) 1202f317da4SYong Wu #define IOVA_34_EN BIT(8) 1216b717796SChao Hao 1226b717796SChao Hao #define MTK_IOMMU_HAS_FLAG(pdata, _x) \ 1236b717796SChao Hao ((((pdata)->flags) & (_x)) == (_x)) 1246b717796SChao Hao 1250df4fabeSYong Wu struct mtk_iommu_domain { 1260df4fabeSYong Wu struct io_pgtable_cfg cfg; 1270df4fabeSYong Wu struct io_pgtable_ops *iop; 1280df4fabeSYong Wu 12908500c43SYong Wu struct mtk_iommu_data *data; 1300df4fabeSYong Wu struct iommu_domain domain; 1310df4fabeSYong Wu }; 1320df4fabeSYong Wu 133b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops; 1340df4fabeSYong Wu 1357f37a91dSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data); 1367f37a91dSYong Wu 137bfed8731SYong Wu #define MTK_IOMMU_TLB_ADDR(iova) ({ \ 138bfed8731SYong Wu dma_addr_t _addr = iova; \ 139bfed8731SYong Wu ((lower_32_bits(_addr) & GENMASK(31, 12)) | upper_32_bits(_addr));\ 140bfed8731SYong Wu }) 141bfed8731SYong Wu 14276ce6546SYong Wu /* 14376ce6546SYong Wu * In M4U 4GB mode, the physical address is remapped as below: 14476ce6546SYong Wu * 14576ce6546SYong Wu * CPU Physical address: 14676ce6546SYong Wu * ==================== 14776ce6546SYong Wu * 14876ce6546SYong Wu * 0 1G 2G 3G 4G 5G 14976ce6546SYong Wu * |---A---|---B---|---C---|---D---|---E---| 15076ce6546SYong Wu * +--I/O--+------------Memory-------------+ 15176ce6546SYong Wu * 15276ce6546SYong Wu * IOMMU output physical address: 15376ce6546SYong Wu * ============================= 15476ce6546SYong Wu * 15576ce6546SYong Wu * 4G 5G 6G 7G 8G 15676ce6546SYong Wu * |---E---|---B---|---C---|---D---| 15776ce6546SYong Wu * +------------Memory-------------+ 15876ce6546SYong Wu * 15976ce6546SYong Wu * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the 16076ce6546SYong Wu * bit32 of the CPU physical address always is needed to set, and for Region 16176ce6546SYong Wu * 'E', the CPU physical address keep as is. 16276ce6546SYong Wu * Additionally, The iommu consumers always use the CPU phyiscal address. 16376ce6546SYong Wu */ 164b4dad40eSYong Wu #define MTK_IOMMU_4GB_MODE_REMAP_BASE 0x140000000UL 16576ce6546SYong Wu 1667c3a2ec0SYong Wu static LIST_HEAD(m4ulist); /* List all the M4U HWs */ 1677c3a2ec0SYong Wu 1687c3a2ec0SYong Wu #define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list) 1697c3a2ec0SYong Wu 170585e58f4SYong Wu struct mtk_iommu_iova_region { 171585e58f4SYong Wu dma_addr_t iova_base; 172585e58f4SYong Wu unsigned long long size; 173585e58f4SYong Wu }; 174585e58f4SYong Wu 175585e58f4SYong Wu static const struct mtk_iommu_iova_region single_domain[] = { 176585e58f4SYong Wu {.iova_base = 0, .size = SZ_4G}, 177585e58f4SYong Wu }; 178585e58f4SYong Wu 1799e3489e0SYong Wu static const struct mtk_iommu_iova_region mt8192_multi_dom[] = { 1809e3489e0SYong Wu { .iova_base = 0x0, .size = SZ_4G}, /* disp: 0 ~ 4G */ 1819e3489e0SYong Wu #if IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) 1829e3489e0SYong Wu { .iova_base = SZ_4G, .size = SZ_4G}, /* vdec: 4G ~ 8G */ 1839e3489e0SYong Wu { .iova_base = SZ_4G * 2, .size = SZ_4G}, /* CAM/MDP: 8G ~ 12G */ 1849e3489e0SYong Wu { .iova_base = 0x240000000ULL, .size = 0x4000000}, /* CCU0 */ 1859e3489e0SYong Wu { .iova_base = 0x244000000ULL, .size = 0x4000000}, /* CCU1 */ 1869e3489e0SYong Wu #endif 1879e3489e0SYong Wu }; 1889e3489e0SYong Wu 1897c3a2ec0SYong Wu /* 1907c3a2ec0SYong Wu * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain 1917c3a2ec0SYong Wu * for the performance. 1927c3a2ec0SYong Wu * 1937c3a2ec0SYong Wu * Here always return the mtk_iommu_data of the first probed M4U where the 1947c3a2ec0SYong Wu * iommu domain information is recorded. 1957c3a2ec0SYong Wu */ 1967c3a2ec0SYong Wu static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void) 1977c3a2ec0SYong Wu { 1987c3a2ec0SYong Wu struct mtk_iommu_data *data; 1997c3a2ec0SYong Wu 2007c3a2ec0SYong Wu for_each_m4u(data) 2017c3a2ec0SYong Wu return data; 2027c3a2ec0SYong Wu 2037c3a2ec0SYong Wu return NULL; 2047c3a2ec0SYong Wu } 2057c3a2ec0SYong Wu 2060df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom) 2070df4fabeSYong Wu { 2080df4fabeSYong Wu return container_of(dom, struct mtk_iommu_domain, domain); 2090df4fabeSYong Wu } 2100df4fabeSYong Wu 2110954d61aSYong Wu static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data) 2120df4fabeSYong Wu { 21315672b6dSYong Wu unsigned long flags; 214c0b57581SYong Wu 21515672b6dSYong Wu spin_lock_irqsave(&data->tlb_lock, flags); 2167c3a2ec0SYong Wu writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, 217b053bc71SChao Hao data->base + data->plat_data->inv_sel_reg); 2180df4fabeSYong Wu writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE); 2190df4fabeSYong Wu wmb(); /* Make sure the tlb flush all done */ 22015672b6dSYong Wu spin_unlock_irqrestore(&data->tlb_lock, flags); 2217c3a2ec0SYong Wu } 2220df4fabeSYong Wu 2231f4fd624SYong Wu static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size, 2240954d61aSYong Wu size_t granule, 2250954d61aSYong Wu struct mtk_iommu_data *data) 2260df4fabeSYong Wu { 2271f4fd624SYong Wu unsigned long flags; 2281f4fd624SYong Wu int ret; 2291f4fd624SYong Wu u32 tmp; 2300df4fabeSYong Wu 2317c3a2ec0SYong Wu for_each_m4u(data) { 232c0b57581SYong Wu if (pm_runtime_get_if_in_use(data->dev) <= 0) 233c0b57581SYong Wu continue; 234c0b57581SYong Wu 2351f4fd624SYong Wu spin_lock_irqsave(&data->tlb_lock, flags); 2367c3a2ec0SYong Wu writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, 237b053bc71SChao Hao data->base + data->plat_data->inv_sel_reg); 2380df4fabeSYong Wu 239bfed8731SYong Wu writel_relaxed(MTK_IOMMU_TLB_ADDR(iova), 240bfed8731SYong Wu data->base + REG_MMU_INVLD_START_A); 241bfed8731SYong Wu writel_relaxed(MTK_IOMMU_TLB_ADDR(iova + size - 1), 2427c3a2ec0SYong Wu data->base + REG_MMU_INVLD_END_A); 2437c3a2ec0SYong Wu writel_relaxed(F_MMU_INV_RANGE, 2447c3a2ec0SYong Wu data->base + REG_MMU_INVALIDATE); 2450df4fabeSYong Wu 2461f4fd624SYong Wu /* tlb sync */ 2477c3a2ec0SYong Wu ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE, 248c90ae4a6SYong Wu tmp, tmp != 0, 10, 1000); 24915672b6dSYong Wu 25015672b6dSYong Wu /* Clear the CPE status */ 25115672b6dSYong Wu writel_relaxed(0, data->base + REG_MMU_CPE_DONE); 25215672b6dSYong Wu spin_unlock_irqrestore(&data->tlb_lock, flags); 25315672b6dSYong Wu 2540df4fabeSYong Wu if (ret) { 2550df4fabeSYong Wu dev_warn(data->dev, 2560df4fabeSYong Wu "Partial TLB flush timed out, falling back to full flush\n"); 2570954d61aSYong Wu mtk_iommu_tlb_flush_all(data); 2580df4fabeSYong Wu } 259c0b57581SYong Wu 260c0b57581SYong Wu pm_runtime_put(data->dev); 2610df4fabeSYong Wu } 2627c3a2ec0SYong Wu } 2630df4fabeSYong Wu 2640df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id) 2650df4fabeSYong Wu { 2660df4fabeSYong Wu struct mtk_iommu_data *data = dev_id; 2670df4fabeSYong Wu struct mtk_iommu_domain *dom = data->m4u_dom; 26837276e00SChao Hao unsigned int fault_larb, fault_port, sub_comm = 0; 269ef0f0986SYong Wu u32 int_state, regval, va34_32, pa34_32; 270ef0f0986SYong Wu u64 fault_iova, fault_pa; 2710df4fabeSYong Wu bool layer, write; 2720df4fabeSYong Wu 2730df4fabeSYong Wu /* Read error info from registers */ 2740df4fabeSYong Wu int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1); 27515a01f4cSYong Wu if (int_state & F_REG_MMU0_FAULT_MASK) { 27615a01f4cSYong Wu regval = readl_relaxed(data->base + REG_MMU0_INT_ID); 27715a01f4cSYong Wu fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA); 27815a01f4cSYong Wu fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA); 27915a01f4cSYong Wu } else { 28015a01f4cSYong Wu regval = readl_relaxed(data->base + REG_MMU1_INT_ID); 28115a01f4cSYong Wu fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA); 28215a01f4cSYong Wu fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA); 28315a01f4cSYong Wu } 2840df4fabeSYong Wu layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT; 2850df4fabeSYong Wu write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT; 286ef0f0986SYong Wu if (MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN)) { 287ef0f0986SYong Wu va34_32 = FIELD_GET(F_MMU_INVAL_VA_34_32_MASK, fault_iova); 288ef0f0986SYong Wu pa34_32 = FIELD_GET(F_MMU_INVAL_PA_34_32_MASK, fault_iova); 289ef0f0986SYong Wu fault_iova = fault_iova & F_MMU_INVAL_VA_31_12_MASK; 290ef0f0986SYong Wu fault_iova |= (u64)va34_32 << 32; 291ef0f0986SYong Wu fault_pa |= (u64)pa34_32 << 32; 292ef0f0986SYong Wu } 293ef0f0986SYong Wu 29415a01f4cSYong Wu fault_port = F_MMU_INT_ID_PORT_ID(regval); 29537276e00SChao Hao if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_SUB_COMM)) { 29637276e00SChao Hao fault_larb = F_MMU_INT_ID_COMM_ID(regval); 29737276e00SChao Hao sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval); 29837276e00SChao Hao } else { 29937276e00SChao Hao fault_larb = F_MMU_INT_ID_LARB_ID(regval); 30037276e00SChao Hao } 30137276e00SChao Hao fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm]; 302b3e5eee7SYong Wu 3030df4fabeSYong Wu if (report_iommu_fault(&dom->domain, data->dev, fault_iova, 3040df4fabeSYong Wu write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) { 3050df4fabeSYong Wu dev_err_ratelimited( 3060df4fabeSYong Wu data->dev, 307ef0f0986SYong Wu "fault type=0x%x iova=0x%llx pa=0x%llx larb=%d port=%d layer=%d %s\n", 3080df4fabeSYong Wu int_state, fault_iova, fault_pa, fault_larb, fault_port, 3090df4fabeSYong Wu layer, write ? "write" : "read"); 3100df4fabeSYong Wu } 3110df4fabeSYong Wu 3120df4fabeSYong Wu /* Interrupt clear */ 3130df4fabeSYong Wu regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0); 3140df4fabeSYong Wu regval |= F_INT_CLR_BIT; 3150df4fabeSYong Wu writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0); 3160df4fabeSYong Wu 3170df4fabeSYong Wu mtk_iommu_tlb_flush_all(data); 3180df4fabeSYong Wu 3190df4fabeSYong Wu return IRQ_HANDLED; 3200df4fabeSYong Wu } 3210df4fabeSYong Wu 322803cf9e5SYong Wu static int mtk_iommu_get_domain_id(struct device *dev, 323803cf9e5SYong Wu const struct mtk_iommu_plat_data *plat_data) 324803cf9e5SYong Wu { 325803cf9e5SYong Wu const struct mtk_iommu_iova_region *rgn = plat_data->iova_region; 326803cf9e5SYong Wu const struct bus_dma_region *dma_rgn = dev->dma_range_map; 327803cf9e5SYong Wu int i, candidate = -1; 328803cf9e5SYong Wu dma_addr_t dma_end; 329803cf9e5SYong Wu 330803cf9e5SYong Wu if (!dma_rgn || plat_data->iova_region_nr == 1) 331803cf9e5SYong Wu return 0; 332803cf9e5SYong Wu 333803cf9e5SYong Wu dma_end = dma_rgn->dma_start + dma_rgn->size - 1; 334803cf9e5SYong Wu for (i = 0; i < plat_data->iova_region_nr; i++, rgn++) { 335803cf9e5SYong Wu /* Best fit. */ 336803cf9e5SYong Wu if (dma_rgn->dma_start == rgn->iova_base && 337803cf9e5SYong Wu dma_end == rgn->iova_base + rgn->size - 1) 338803cf9e5SYong Wu return i; 339803cf9e5SYong Wu /* ok if it is inside this region. */ 340803cf9e5SYong Wu if (dma_rgn->dma_start >= rgn->iova_base && 341803cf9e5SYong Wu dma_end < rgn->iova_base + rgn->size) 342803cf9e5SYong Wu candidate = i; 343803cf9e5SYong Wu } 344803cf9e5SYong Wu 345803cf9e5SYong Wu if (candidate >= 0) 346803cf9e5SYong Wu return candidate; 347803cf9e5SYong Wu dev_err(dev, "Can NOT find the iommu domain id(%pad 0x%llx).\n", 348803cf9e5SYong Wu &dma_rgn->dma_start, dma_rgn->size); 349803cf9e5SYong Wu return -EINVAL; 350803cf9e5SYong Wu } 351803cf9e5SYong Wu 3528d2c749eSYong Wu static void mtk_iommu_config(struct mtk_iommu_data *data, struct device *dev, 3538d2c749eSYong Wu bool enable, unsigned int domid) 3540df4fabeSYong Wu { 3550df4fabeSYong Wu struct mtk_smi_larb_iommu *larb_mmu; 3560df4fabeSYong Wu unsigned int larbid, portid; 357a9bf2eecSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 3588d2c749eSYong Wu const struct mtk_iommu_iova_region *region; 35958f0d1d5SRobin Murphy int i; 3600df4fabeSYong Wu 36158f0d1d5SRobin Murphy for (i = 0; i < fwspec->num_ids; ++i) { 36258f0d1d5SRobin Murphy larbid = MTK_M4U_TO_LARB(fwspec->ids[i]); 36358f0d1d5SRobin Murphy portid = MTK_M4U_TO_PORT(fwspec->ids[i]); 3648d2c749eSYong Wu 3651ee9feb2SYong Wu larb_mmu = &data->larb_imu[larbid]; 3660df4fabeSYong Wu 3678d2c749eSYong Wu region = data->plat_data->iova_region + domid; 3688d2c749eSYong Wu larb_mmu->bank[portid] = upper_32_bits(region->iova_base); 3698d2c749eSYong Wu 3708d2c749eSYong Wu dev_dbg(dev, "%s iommu for larb(%s) port %d dom %d bank %d.\n", 3718d2c749eSYong Wu enable ? "enable" : "disable", dev_name(larb_mmu->dev), 3728d2c749eSYong Wu portid, domid, larb_mmu->bank[portid]); 3730df4fabeSYong Wu 3740df4fabeSYong Wu if (enable) 3750df4fabeSYong Wu larb_mmu->mmu |= MTK_SMI_MMU_EN(portid); 3760df4fabeSYong Wu else 3770df4fabeSYong Wu larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid); 3780df4fabeSYong Wu } 3790df4fabeSYong Wu } 3800df4fabeSYong Wu 3814f956c97SYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom, 382c3045f39SYong Wu struct mtk_iommu_data *data, 383c3045f39SYong Wu unsigned int domid) 3840df4fabeSYong Wu { 385c3045f39SYong Wu const struct mtk_iommu_iova_region *region; 386c3045f39SYong Wu 387c3045f39SYong Wu /* Use the exist domain as there is only one pgtable here. */ 388c3045f39SYong Wu if (data->m4u_dom) { 389c3045f39SYong Wu dom->iop = data->m4u_dom->iop; 390c3045f39SYong Wu dom->cfg = data->m4u_dom->cfg; 391c3045f39SYong Wu dom->domain.pgsize_bitmap = data->m4u_dom->cfg.pgsize_bitmap; 392c3045f39SYong Wu goto update_iova_region; 393c3045f39SYong Wu } 394c3045f39SYong Wu 3950df4fabeSYong Wu dom->cfg = (struct io_pgtable_cfg) { 3960df4fabeSYong Wu .quirks = IO_PGTABLE_QUIRK_ARM_NS | 3970df4fabeSYong Wu IO_PGTABLE_QUIRK_NO_PERMS | 398b4dad40eSYong Wu IO_PGTABLE_QUIRK_ARM_MTK_EXT, 3990df4fabeSYong Wu .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap, 4002f317da4SYong Wu .ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32, 4010df4fabeSYong Wu .iommu_dev = data->dev, 4020df4fabeSYong Wu }; 4030df4fabeSYong Wu 4049bdfe4c1SYong Wu if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) 4059bdfe4c1SYong Wu dom->cfg.oas = data->enable_4GB ? 33 : 32; 4069bdfe4c1SYong Wu else 4079bdfe4c1SYong Wu dom->cfg.oas = 35; 4089bdfe4c1SYong Wu 4090df4fabeSYong Wu dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data); 4100df4fabeSYong Wu if (!dom->iop) { 4110df4fabeSYong Wu dev_err(data->dev, "Failed to alloc io pgtable\n"); 4120df4fabeSYong Wu return -EINVAL; 4130df4fabeSYong Wu } 4140df4fabeSYong Wu 4150df4fabeSYong Wu /* Update our support page sizes bitmap */ 416d16e0faaSRobin Murphy dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap; 417b7875eb9SYong Wu 418c3045f39SYong Wu update_iova_region: 419c3045f39SYong Wu /* Update the iova region for this domain */ 420c3045f39SYong Wu region = data->plat_data->iova_region + domid; 421c3045f39SYong Wu dom->domain.geometry.aperture_start = region->iova_base; 422c3045f39SYong Wu dom->domain.geometry.aperture_end = region->iova_base + region->size - 1; 423b7875eb9SYong Wu dom->domain.geometry.force_aperture = true; 4240df4fabeSYong Wu return 0; 4250df4fabeSYong Wu } 4260df4fabeSYong Wu 4270df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type) 4280df4fabeSYong Wu { 4290df4fabeSYong Wu struct mtk_iommu_domain *dom; 4300df4fabeSYong Wu 4310df4fabeSYong Wu if (type != IOMMU_DOMAIN_DMA) 4320df4fabeSYong Wu return NULL; 4330df4fabeSYong Wu 4340df4fabeSYong Wu dom = kzalloc(sizeof(*dom), GFP_KERNEL); 4350df4fabeSYong Wu if (!dom) 4360df4fabeSYong Wu return NULL; 4370df4fabeSYong Wu 4384f956c97SYong Wu return &dom->domain; 4394f956c97SYong Wu } 4404f956c97SYong Wu 4410df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain) 4420df4fabeSYong Wu { 4430df4fabeSYong Wu kfree(to_mtk_domain(domain)); 4440df4fabeSYong Wu } 4450df4fabeSYong Wu 4460df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain, 4470df4fabeSYong Wu struct device *dev) 4480df4fabeSYong Wu { 449645b87c1SYong Wu struct mtk_iommu_data *data = dev_iommu_priv_get(dev), *frstdata; 4500df4fabeSYong Wu struct mtk_iommu_domain *dom = to_mtk_domain(domain); 451c0b57581SYong Wu struct device *m4udev = data->dev; 452803cf9e5SYong Wu int ret, domid; 4530df4fabeSYong Wu 454803cf9e5SYong Wu domid = mtk_iommu_get_domain_id(dev, data->plat_data); 455803cf9e5SYong Wu if (domid < 0) 456803cf9e5SYong Wu return domid; 457803cf9e5SYong Wu 4584f956c97SYong Wu if (!dom->data) { 459645b87c1SYong Wu /* Data is in the frstdata in sharing pgtable case. */ 460645b87c1SYong Wu frstdata = mtk_iommu_get_m4u_data(); 461645b87c1SYong Wu 462645b87c1SYong Wu if (mtk_iommu_domain_finalise(dom, frstdata, domid)) 4634f956c97SYong Wu return -ENODEV; 4644f956c97SYong Wu dom->data = data; 4654f956c97SYong Wu } 4664f956c97SYong Wu 467*0e5a3f2eSYong Wu mutex_lock(&data->mutex); 4687f37a91dSYong Wu if (!data->m4u_dom) { /* Initialize the M4U HW */ 469c0b57581SYong Wu ret = pm_runtime_resume_and_get(m4udev); 470c0b57581SYong Wu if (ret < 0) 471*0e5a3f2eSYong Wu goto err_unlock; 472c0b57581SYong Wu 473c0b57581SYong Wu ret = mtk_iommu_hw_init(data); 474c0b57581SYong Wu if (ret) { 475c0b57581SYong Wu pm_runtime_put(m4udev); 476*0e5a3f2eSYong Wu goto err_unlock; 477c0b57581SYong Wu } 4780df4fabeSYong Wu data->m4u_dom = dom; 479d1e5f26fSRobin Murphy writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, 4804b00f5acSYong Wu data->base + REG_MMU_PT_BASE_ADDR); 481c0b57581SYong Wu 482c0b57581SYong Wu pm_runtime_put(m4udev); 4830df4fabeSYong Wu } 484*0e5a3f2eSYong Wu mutex_unlock(&data->mutex); 4850df4fabeSYong Wu 4868d2c749eSYong Wu mtk_iommu_config(data, dev, true, domid); 4870df4fabeSYong Wu return 0; 488*0e5a3f2eSYong Wu 489*0e5a3f2eSYong Wu err_unlock: 490*0e5a3f2eSYong Wu mutex_unlock(&data->mutex); 491*0e5a3f2eSYong Wu return ret; 4920df4fabeSYong Wu } 4930df4fabeSYong Wu 4940df4fabeSYong Wu static void mtk_iommu_detach_device(struct iommu_domain *domain, 4950df4fabeSYong Wu struct device *dev) 4960df4fabeSYong Wu { 4973524b559SJoerg Roedel struct mtk_iommu_data *data = dev_iommu_priv_get(dev); 4980df4fabeSYong Wu 4998d2c749eSYong Wu mtk_iommu_config(data, dev, false, 0); 5000df4fabeSYong Wu } 5010df4fabeSYong Wu 5020df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova, 503781ca2deSTom Murphy phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 5040df4fabeSYong Wu { 5050df4fabeSYong Wu struct mtk_iommu_domain *dom = to_mtk_domain(domain); 5060df4fabeSYong Wu 507b4dad40eSYong Wu /* The "4GB mode" M4U physically can not use the lower remap of Dram. */ 50808500c43SYong Wu if (dom->data->enable_4GB) 509b4dad40eSYong Wu paddr |= BIT_ULL(32); 510b4dad40eSYong Wu 51160829b4dSYong Wu /* Synchronize with the tlb_lock */ 512f34ce7a7SBaolin Wang return dom->iop->map(dom->iop, iova, paddr, size, prot, gfp); 5130df4fabeSYong Wu } 5140df4fabeSYong Wu 5150df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain, 51656f8af5eSWill Deacon unsigned long iova, size_t size, 51756f8af5eSWill Deacon struct iommu_iotlb_gather *gather) 5180df4fabeSYong Wu { 5190df4fabeSYong Wu struct mtk_iommu_domain *dom = to_mtk_domain(domain); 5200df4fabeSYong Wu 5213136895cSRobin Murphy iommu_iotlb_gather_add_range(gather, iova, size); 52260829b4dSYong Wu return dom->iop->unmap(dom->iop, iova, size, gather); 5230df4fabeSYong Wu } 5240df4fabeSYong Wu 52556f8af5eSWill Deacon static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain) 52656f8af5eSWill Deacon { 52708500c43SYong Wu struct mtk_iommu_domain *dom = to_mtk_domain(domain); 52808500c43SYong Wu 52908500c43SYong Wu mtk_iommu_tlb_flush_all(dom->data); 53056f8af5eSWill Deacon } 53156f8af5eSWill Deacon 53256f8af5eSWill Deacon static void mtk_iommu_iotlb_sync(struct iommu_domain *domain, 53356f8af5eSWill Deacon struct iommu_iotlb_gather *gather) 5344d689b61SRobin Murphy { 53508500c43SYong Wu struct mtk_iommu_domain *dom = to_mtk_domain(domain); 536862c3715SYong Wu size_t length = gather->end - gather->start + 1; 537da3cc91bSYong Wu 5381f4fd624SYong Wu mtk_iommu_tlb_flush_range_sync(gather->start, length, gather->pgsize, 53908500c43SYong Wu dom->data); 5404d689b61SRobin Murphy } 5414d689b61SRobin Murphy 54220143451SYong Wu static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova, 54320143451SYong Wu size_t size) 54420143451SYong Wu { 54508500c43SYong Wu struct mtk_iommu_domain *dom = to_mtk_domain(domain); 54620143451SYong Wu 54708500c43SYong Wu mtk_iommu_tlb_flush_range_sync(iova, size, size, dom->data); 54820143451SYong Wu } 54920143451SYong Wu 5500df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain, 5510df4fabeSYong Wu dma_addr_t iova) 5520df4fabeSYong Wu { 5530df4fabeSYong Wu struct mtk_iommu_domain *dom = to_mtk_domain(domain); 5540df4fabeSYong Wu phys_addr_t pa; 5550df4fabeSYong Wu 5560df4fabeSYong Wu pa = dom->iop->iova_to_phys(dom->iop, iova); 557f13efafcSArnd Bergmann if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) && 558f13efafcSArnd Bergmann dom->data->enable_4GB && 559f13efafcSArnd Bergmann pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE) 560b4dad40eSYong Wu pa &= ~BIT_ULL(32); 56130e2fccfSYong Wu 5620df4fabeSYong Wu return pa; 5630df4fabeSYong Wu } 5640df4fabeSYong Wu 56580e4592aSJoerg Roedel static struct iommu_device *mtk_iommu_probe_device(struct device *dev) 5660df4fabeSYong Wu { 567a9bf2eecSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 568b16c0170SJoerg Roedel struct mtk_iommu_data *data; 569635319a4SYong Wu struct device_link *link; 570635319a4SYong Wu struct device *larbdev; 571635319a4SYong Wu unsigned int larbid, larbidx, i; 5720df4fabeSYong Wu 573a9bf2eecSJoerg Roedel if (!fwspec || fwspec->ops != &mtk_iommu_ops) 57480e4592aSJoerg Roedel return ERR_PTR(-ENODEV); /* Not a iommu client device */ 5750df4fabeSYong Wu 5763524b559SJoerg Roedel data = dev_iommu_priv_get(dev); 577b16c0170SJoerg Roedel 578635319a4SYong Wu /* 579635319a4SYong Wu * Link the consumer device with the smi-larb device(supplier). 580635319a4SYong Wu * The device that connects with each a larb is a independent HW. 581635319a4SYong Wu * All the ports in each a device should be in the same larbs. 582635319a4SYong Wu */ 583635319a4SYong Wu larbid = MTK_M4U_TO_LARB(fwspec->ids[0]); 584635319a4SYong Wu for (i = 1; i < fwspec->num_ids; i++) { 585635319a4SYong Wu larbidx = MTK_M4U_TO_LARB(fwspec->ids[i]); 586635319a4SYong Wu if (larbid != larbidx) { 587635319a4SYong Wu dev_err(dev, "Can only use one larb. Fail@larb%d-%d.\n", 588635319a4SYong Wu larbid, larbidx); 589635319a4SYong Wu return ERR_PTR(-EINVAL); 590635319a4SYong Wu } 591635319a4SYong Wu } 592635319a4SYong Wu larbdev = data->larb_imu[larbid].dev; 593635319a4SYong Wu link = device_link_add(dev, larbdev, 594635319a4SYong Wu DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS); 595635319a4SYong Wu if (!link) 596635319a4SYong Wu dev_err(dev, "Unable to link %s\n", dev_name(larbdev)); 59780e4592aSJoerg Roedel return &data->iommu; 5980df4fabeSYong Wu } 5990df4fabeSYong Wu 60080e4592aSJoerg Roedel static void mtk_iommu_release_device(struct device *dev) 6010df4fabeSYong Wu { 602a9bf2eecSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 603635319a4SYong Wu struct mtk_iommu_data *data; 604635319a4SYong Wu struct device *larbdev; 605635319a4SYong Wu unsigned int larbid; 606b16c0170SJoerg Roedel 607a9bf2eecSJoerg Roedel if (!fwspec || fwspec->ops != &mtk_iommu_ops) 6080df4fabeSYong Wu return; 6090df4fabeSYong Wu 610635319a4SYong Wu data = dev_iommu_priv_get(dev); 611635319a4SYong Wu larbid = MTK_M4U_TO_LARB(fwspec->ids[0]); 612635319a4SYong Wu larbdev = data->larb_imu[larbid].dev; 613635319a4SYong Wu device_link_remove(dev, larbdev); 614635319a4SYong Wu 61558f0d1d5SRobin Murphy iommu_fwspec_free(dev); 6160df4fabeSYong Wu } 6170df4fabeSYong Wu 6180df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev) 6190df4fabeSYong Wu { 6207c3a2ec0SYong Wu struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); 621c3045f39SYong Wu struct iommu_group *group; 622803cf9e5SYong Wu int domid; 6230df4fabeSYong Wu 62458f0d1d5SRobin Murphy if (!data) 6250df4fabeSYong Wu return ERR_PTR(-ENODEV); 6260df4fabeSYong Wu 627803cf9e5SYong Wu domid = mtk_iommu_get_domain_id(dev, data->plat_data); 628803cf9e5SYong Wu if (domid < 0) 629803cf9e5SYong Wu return ERR_PTR(domid); 630803cf9e5SYong Wu 631*0e5a3f2eSYong Wu mutex_lock(&data->mutex); 632c3045f39SYong Wu group = data->m4u_group[domid]; 633c3045f39SYong Wu if (!group) { 634c3045f39SYong Wu group = iommu_group_alloc(); 635c3045f39SYong Wu if (!IS_ERR(group)) 636c3045f39SYong Wu data->m4u_group[domid] = group; 6373a8d40b6SRobin Murphy } else { 638c3045f39SYong Wu iommu_group_ref_get(group); 6390df4fabeSYong Wu } 640*0e5a3f2eSYong Wu mutex_unlock(&data->mutex); 641c3045f39SYong Wu return group; 6420df4fabeSYong Wu } 6430df4fabeSYong Wu 6440df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args) 6450df4fabeSYong Wu { 6460df4fabeSYong Wu struct platform_device *m4updev; 6470df4fabeSYong Wu 6480df4fabeSYong Wu if (args->args_count != 1) { 6490df4fabeSYong Wu dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n", 6500df4fabeSYong Wu args->args_count); 6510df4fabeSYong Wu return -EINVAL; 6520df4fabeSYong Wu } 6530df4fabeSYong Wu 6543524b559SJoerg Roedel if (!dev_iommu_priv_get(dev)) { 6550df4fabeSYong Wu /* Get the m4u device */ 6560df4fabeSYong Wu m4updev = of_find_device_by_node(args->np); 6570df4fabeSYong Wu if (WARN_ON(!m4updev)) 6580df4fabeSYong Wu return -EINVAL; 6590df4fabeSYong Wu 6603524b559SJoerg Roedel dev_iommu_priv_set(dev, platform_get_drvdata(m4updev)); 6610df4fabeSYong Wu } 6620df4fabeSYong Wu 66358f0d1d5SRobin Murphy return iommu_fwspec_add_ids(dev, args->args, 1); 6640df4fabeSYong Wu } 6650df4fabeSYong Wu 666ab1d5281SYong Wu static void mtk_iommu_get_resv_regions(struct device *dev, 667ab1d5281SYong Wu struct list_head *head) 668ab1d5281SYong Wu { 669ab1d5281SYong Wu struct mtk_iommu_data *data = dev_iommu_priv_get(dev); 670ab1d5281SYong Wu unsigned int domid = mtk_iommu_get_domain_id(dev, data->plat_data), i; 671ab1d5281SYong Wu const struct mtk_iommu_iova_region *resv, *curdom; 672ab1d5281SYong Wu struct iommu_resv_region *region; 673ab1d5281SYong Wu int prot = IOMMU_WRITE | IOMMU_READ; 674ab1d5281SYong Wu 6757a566173SColin Ian King if ((int)domid < 0) 676ab1d5281SYong Wu return; 677ab1d5281SYong Wu curdom = data->plat_data->iova_region + domid; 678ab1d5281SYong Wu for (i = 0; i < data->plat_data->iova_region_nr; i++) { 679ab1d5281SYong Wu resv = data->plat_data->iova_region + i; 680ab1d5281SYong Wu 681ab1d5281SYong Wu /* Only reserve when the region is inside the current domain */ 682ab1d5281SYong Wu if (resv->iova_base <= curdom->iova_base || 683ab1d5281SYong Wu resv->iova_base + resv->size >= curdom->iova_base + curdom->size) 684ab1d5281SYong Wu continue; 685ab1d5281SYong Wu 686ab1d5281SYong Wu region = iommu_alloc_resv_region(resv->iova_base, resv->size, 687ab1d5281SYong Wu prot, IOMMU_RESV_RESERVED); 688ab1d5281SYong Wu if (!region) 689ab1d5281SYong Wu return; 690ab1d5281SYong Wu 691ab1d5281SYong Wu list_add_tail(®ion->list, head); 692ab1d5281SYong Wu } 693ab1d5281SYong Wu } 694ab1d5281SYong Wu 695b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = { 6960df4fabeSYong Wu .domain_alloc = mtk_iommu_domain_alloc, 69780e4592aSJoerg Roedel .probe_device = mtk_iommu_probe_device, 69880e4592aSJoerg Roedel .release_device = mtk_iommu_release_device, 6990df4fabeSYong Wu .device_group = mtk_iommu_device_group, 7000df4fabeSYong Wu .of_xlate = mtk_iommu_of_xlate, 701ab1d5281SYong Wu .get_resv_regions = mtk_iommu_get_resv_regions, 702ab1d5281SYong Wu .put_resv_regions = generic_iommu_put_resv_regions, 7030df4fabeSYong Wu .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M, 70418d8c74eSYong Wu .owner = THIS_MODULE, 7059a630a4bSLu Baolu .default_domain_ops = &(const struct iommu_domain_ops) { 7069a630a4bSLu Baolu .attach_dev = mtk_iommu_attach_device, 7079a630a4bSLu Baolu .detach_dev = mtk_iommu_detach_device, 7089a630a4bSLu Baolu .map = mtk_iommu_map, 7099a630a4bSLu Baolu .unmap = mtk_iommu_unmap, 7109a630a4bSLu Baolu .flush_iotlb_all = mtk_iommu_flush_iotlb_all, 7119a630a4bSLu Baolu .iotlb_sync = mtk_iommu_iotlb_sync, 7129a630a4bSLu Baolu .iotlb_sync_map = mtk_iommu_sync_map, 7139a630a4bSLu Baolu .iova_to_phys = mtk_iommu_iova_to_phys, 7149a630a4bSLu Baolu .free = mtk_iommu_domain_free, 7159a630a4bSLu Baolu } 7160df4fabeSYong Wu }; 7170df4fabeSYong Wu 7180df4fabeSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data) 7190df4fabeSYong Wu { 7200df4fabeSYong Wu u32 regval; 7210df4fabeSYong Wu 72286444413SChao Hao if (data->plat_data->m4u_plat == M4U_MT8173) { 723acb3c92aSYong Wu regval = F_MMU_PREFETCH_RT_REPLACE_MOD | 724acb3c92aSYong Wu F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173; 72586444413SChao Hao } else { 72686444413SChao Hao regval = readl_relaxed(data->base + REG_MMU_CTRL_REG); 72786444413SChao Hao regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR; 72886444413SChao Hao } 7290df4fabeSYong Wu writel_relaxed(regval, data->base + REG_MMU_CTRL_REG); 7300df4fabeSYong Wu 7310df4fabeSYong Wu regval = F_L2_MULIT_HIT_EN | 7320df4fabeSYong Wu F_TABLE_WALK_FAULT_INT_EN | 7330df4fabeSYong Wu F_PREETCH_FIFO_OVERFLOW_INT_EN | 7340df4fabeSYong Wu F_MISS_FIFO_OVERFLOW_INT_EN | 7350df4fabeSYong Wu F_PREFETCH_FIFO_ERR_INT_EN | 7360df4fabeSYong Wu F_MISS_FIFO_ERR_INT_EN; 7370df4fabeSYong Wu writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0); 7380df4fabeSYong Wu 7390df4fabeSYong Wu regval = F_INT_TRANSLATION_FAULT | 7400df4fabeSYong Wu F_INT_MAIN_MULTI_HIT_FAULT | 7410df4fabeSYong Wu F_INT_INVALID_PA_FAULT | 7420df4fabeSYong Wu F_INT_ENTRY_REPLACEMENT_FAULT | 7430df4fabeSYong Wu F_INT_TLB_MISS_FAULT | 7440df4fabeSYong Wu F_INT_MISS_TRANSACTION_FIFO_FAULT | 7450df4fabeSYong Wu F_INT_PRETETCH_TRANSATION_FIFO_FAULT; 7460df4fabeSYong Wu writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL); 7470df4fabeSYong Wu 748d1b5ef00SFabien Parent if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR)) 74970ca608bSYong Wu regval = (data->protect_base >> 1) | (data->enable_4GB << 31); 75070ca608bSYong Wu else 75170ca608bSYong Wu regval = lower_32_bits(data->protect_base) | 75270ca608bSYong Wu upper_32_bits(data->protect_base); 75370ca608bSYong Wu writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR); 75470ca608bSYong Wu 7556b717796SChao Hao if (data->enable_4GB && 7566b717796SChao Hao MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) { 75730e2fccfSYong Wu /* 75830e2fccfSYong Wu * If 4GB mode is enabled, the validate PA range is from 75930e2fccfSYong Wu * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30]. 76030e2fccfSYong Wu */ 76130e2fccfSYong Wu regval = F_MMU_VLD_PA_RNG(7, 4); 76230e2fccfSYong Wu writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG); 76330e2fccfSYong Wu } 7640df4fabeSYong Wu writel_relaxed(0, data->base + REG_MMU_DCM_DIS); 76535c1b48dSChao Hao if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) { 76635c1b48dSChao Hao /* write command throttling mode */ 76735c1b48dSChao Hao regval = readl_relaxed(data->base + REG_MMU_WR_LEN_CTRL); 76835c1b48dSChao Hao regval &= ~F_MMU_WR_THROT_DIS_MASK; 76935c1b48dSChao Hao writel_relaxed(regval, data->base + REG_MMU_WR_LEN_CTRL); 77035c1b48dSChao Hao } 771e6dec923SYong Wu 7726b717796SChao Hao if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) { 77375eed350SChao Hao /* The register is called STANDARD_AXI_MODE in this case */ 7744bb2bf4cSChao Hao regval = 0; 7754bb2bf4cSChao Hao } else { 7764bb2bf4cSChao Hao regval = readl_relaxed(data->base + REG_MMU_MISC_CTRL); 7774bb2bf4cSChao Hao regval &= ~F_MMU_STANDARD_AXI_MODE_MASK; 7784bb2bf4cSChao Hao if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN)) 7794bb2bf4cSChao Hao regval &= ~F_MMU_IN_ORDER_WR_EN_MASK; 78075eed350SChao Hao } 7814bb2bf4cSChao Hao writel_relaxed(regval, data->base + REG_MMU_MISC_CTRL); 7820df4fabeSYong Wu 7830df4fabeSYong Wu if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0, 7840df4fabeSYong Wu dev_name(data->dev), (void *)data)) { 7850df4fabeSYong Wu writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR); 7860df4fabeSYong Wu dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq); 7870df4fabeSYong Wu return -ENODEV; 7880df4fabeSYong Wu } 7890df4fabeSYong Wu 7900df4fabeSYong Wu return 0; 7910df4fabeSYong Wu } 7920df4fabeSYong Wu 7930df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = { 7940df4fabeSYong Wu .bind = mtk_iommu_bind, 7950df4fabeSYong Wu .unbind = mtk_iommu_unbind, 7960df4fabeSYong Wu }; 7970df4fabeSYong Wu 7980df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev) 7990df4fabeSYong Wu { 8000df4fabeSYong Wu struct mtk_iommu_data *data; 8010df4fabeSYong Wu struct device *dev = &pdev->dev; 802baf94e6eSYong Wu struct device_node *larbnode, *smicomm_node; 803baf94e6eSYong Wu struct platform_device *plarbdev; 804baf94e6eSYong Wu struct device_link *link; 8050df4fabeSYong Wu struct resource *res; 806b16c0170SJoerg Roedel resource_size_t ioaddr; 8070df4fabeSYong Wu struct component_match *match = NULL; 808c2c59456SMiles Chen struct regmap *infracfg; 8090df4fabeSYong Wu void *protect; 8100b6c0ad3SAndrzej Hajda int i, larb_nr, ret; 811c2c59456SMiles Chen u32 val; 812c2c59456SMiles Chen char *p; 8130df4fabeSYong Wu 8140df4fabeSYong Wu data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 8150df4fabeSYong Wu if (!data) 8160df4fabeSYong Wu return -ENOMEM; 8170df4fabeSYong Wu data->dev = dev; 818cecdce9dSYong Wu data->plat_data = of_device_get_match_data(dev); 8190df4fabeSYong Wu 8200df4fabeSYong Wu /* Protect memory. HW will access here while translation fault.*/ 8210df4fabeSYong Wu protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL); 8220df4fabeSYong Wu if (!protect) 8230df4fabeSYong Wu return -ENOMEM; 8240df4fabeSYong Wu data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN); 8250df4fabeSYong Wu 826c2c59456SMiles Chen if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) { 827c2c59456SMiles Chen switch (data->plat_data->m4u_plat) { 828c2c59456SMiles Chen case M4U_MT2712: 829c2c59456SMiles Chen p = "mediatek,mt2712-infracfg"; 830c2c59456SMiles Chen break; 831c2c59456SMiles Chen case M4U_MT8173: 832c2c59456SMiles Chen p = "mediatek,mt8173-infracfg"; 833c2c59456SMiles Chen break; 834c2c59456SMiles Chen default: 835c2c59456SMiles Chen p = NULL; 836c2c59456SMiles Chen } 837c2c59456SMiles Chen 838c2c59456SMiles Chen infracfg = syscon_regmap_lookup_by_compatible(p); 839c2c59456SMiles Chen 840c2c59456SMiles Chen if (IS_ERR(infracfg)) 841c2c59456SMiles Chen return PTR_ERR(infracfg); 842c2c59456SMiles Chen 843c2c59456SMiles Chen ret = regmap_read(infracfg, REG_INFRA_MISC, &val); 844c2c59456SMiles Chen if (ret) 845c2c59456SMiles Chen return ret; 846c2c59456SMiles Chen data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN); 847c2c59456SMiles Chen } 84801e23c93SYong Wu 8490df4fabeSYong Wu res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8500df4fabeSYong Wu data->base = devm_ioremap_resource(dev, res); 8510df4fabeSYong Wu if (IS_ERR(data->base)) 8520df4fabeSYong Wu return PTR_ERR(data->base); 853b16c0170SJoerg Roedel ioaddr = res->start; 8540df4fabeSYong Wu 8550df4fabeSYong Wu data->irq = platform_get_irq(pdev, 0); 8560df4fabeSYong Wu if (data->irq < 0) 8570df4fabeSYong Wu return data->irq; 8580df4fabeSYong Wu 8596b717796SChao Hao if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) { 8600df4fabeSYong Wu data->bclk = devm_clk_get(dev, "bclk"); 8610df4fabeSYong Wu if (IS_ERR(data->bclk)) 8620df4fabeSYong Wu return PTR_ERR(data->bclk); 8632aa4c259SYong Wu } 8640df4fabeSYong Wu 8650df4fabeSYong Wu larb_nr = of_count_phandle_with_args(dev->of_node, 8660df4fabeSYong Wu "mediatek,larbs", NULL); 8670df4fabeSYong Wu if (larb_nr < 0) 8680df4fabeSYong Wu return larb_nr; 8690df4fabeSYong Wu 8700df4fabeSYong Wu for (i = 0; i < larb_nr; i++) { 871e6dec923SYong Wu u32 id; 8720df4fabeSYong Wu 8730df4fabeSYong Wu larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i); 8740df4fabeSYong Wu if (!larbnode) 8750df4fabeSYong Wu return -EINVAL; 8760df4fabeSYong Wu 8771eb8e4e2SWen Yang if (!of_device_is_available(larbnode)) { 8781eb8e4e2SWen Yang of_node_put(larbnode); 8790df4fabeSYong Wu continue; 8801eb8e4e2SWen Yang } 8810df4fabeSYong Wu 882e6dec923SYong Wu ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id); 883e6dec923SYong Wu if (ret)/* The id is consecutive if there is no this property */ 884e6dec923SYong Wu id = i; 885e6dec923SYong Wu 8860df4fabeSYong Wu plarbdev = of_find_device_by_node(larbnode); 8871eb8e4e2SWen Yang if (!plarbdev) { 8881eb8e4e2SWen Yang of_node_put(larbnode); 8892fb0feedSYong Wu return -ENODEV; 8901eb8e4e2SWen Yang } 8917d09aaf8SYong Wu if (!plarbdev->dev.driver) { 8927d09aaf8SYong Wu of_node_put(larbnode); 8937d09aaf8SYong Wu return -EPROBE_DEFER; 8947d09aaf8SYong Wu } 8951ee9feb2SYong Wu data->larb_imu[id].dev = &plarbdev->dev; 8960df4fabeSYong Wu 8974811a485SYong Wu component_match_add_release(dev, &match, component_release_of, 8984811a485SYong Wu component_compare_of, larbnode); 8990df4fabeSYong Wu } 9000df4fabeSYong Wu 901baf94e6eSYong Wu /* Get smi-common dev from the last larb. */ 902baf94e6eSYong Wu smicomm_node = of_parse_phandle(larbnode, "mediatek,smi", 0); 903baf94e6eSYong Wu if (!smicomm_node) 904baf94e6eSYong Wu return -EINVAL; 905baf94e6eSYong Wu 906baf94e6eSYong Wu plarbdev = of_find_device_by_node(smicomm_node); 907baf94e6eSYong Wu of_node_put(smicomm_node); 908baf94e6eSYong Wu data->smicomm_dev = &plarbdev->dev; 909baf94e6eSYong Wu 910c0b57581SYong Wu pm_runtime_enable(dev); 911c0b57581SYong Wu 912baf94e6eSYong Wu link = device_link_add(data->smicomm_dev, dev, 913baf94e6eSYong Wu DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME); 914baf94e6eSYong Wu if (!link) { 915a92a90acSDan Carpenter dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev)); 916a92a90acSDan Carpenter ret = -EINVAL; 917c0b57581SYong Wu goto out_runtime_disable; 918baf94e6eSYong Wu } 919baf94e6eSYong Wu 9200df4fabeSYong Wu platform_set_drvdata(pdev, data); 921*0e5a3f2eSYong Wu mutex_init(&data->mutex); 9220df4fabeSYong Wu 923b16c0170SJoerg Roedel ret = iommu_device_sysfs_add(&data->iommu, dev, NULL, 924b16c0170SJoerg Roedel "mtk-iommu.%pa", &ioaddr); 925b16c0170SJoerg Roedel if (ret) 926baf94e6eSYong Wu goto out_link_remove; 927b16c0170SJoerg Roedel 9282d471b20SRobin Murphy ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev); 929b16c0170SJoerg Roedel if (ret) 930986d9ec5SYong Wu goto out_sysfs_remove; 931b16c0170SJoerg Roedel 932da3cc91bSYong Wu spin_lock_init(&data->tlb_lock); 9337c3a2ec0SYong Wu list_add_tail(&data->list, &m4ulist); 9347c3a2ec0SYong Wu 935986d9ec5SYong Wu if (!iommu_present(&platform_bus_type)) { 936986d9ec5SYong Wu ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops); 937986d9ec5SYong Wu if (ret) 938986d9ec5SYong Wu goto out_list_del; 939986d9ec5SYong Wu } 9400df4fabeSYong Wu 941986d9ec5SYong Wu ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match); 942986d9ec5SYong Wu if (ret) 943986d9ec5SYong Wu goto out_bus_set_null; 944986d9ec5SYong Wu return ret; 945986d9ec5SYong Wu 946986d9ec5SYong Wu out_bus_set_null: 947986d9ec5SYong Wu bus_set_iommu(&platform_bus_type, NULL); 948986d9ec5SYong Wu out_list_del: 949986d9ec5SYong Wu list_del(&data->list); 950986d9ec5SYong Wu iommu_device_unregister(&data->iommu); 951986d9ec5SYong Wu out_sysfs_remove: 952986d9ec5SYong Wu iommu_device_sysfs_remove(&data->iommu); 953baf94e6eSYong Wu out_link_remove: 954baf94e6eSYong Wu device_link_remove(data->smicomm_dev, dev); 955c0b57581SYong Wu out_runtime_disable: 956c0b57581SYong Wu pm_runtime_disable(dev); 957986d9ec5SYong Wu return ret; 9580df4fabeSYong Wu } 9590df4fabeSYong Wu 9600df4fabeSYong Wu static int mtk_iommu_remove(struct platform_device *pdev) 9610df4fabeSYong Wu { 9620df4fabeSYong Wu struct mtk_iommu_data *data = platform_get_drvdata(pdev); 9630df4fabeSYong Wu 964b16c0170SJoerg Roedel iommu_device_sysfs_remove(&data->iommu); 965b16c0170SJoerg Roedel iommu_device_unregister(&data->iommu); 966b16c0170SJoerg Roedel 967ee55f75eSYong Wu list_del(&data->list); 9680df4fabeSYong Wu 969baf94e6eSYong Wu device_link_remove(data->smicomm_dev, &pdev->dev); 970c0b57581SYong Wu pm_runtime_disable(&pdev->dev); 9710df4fabeSYong Wu devm_free_irq(&pdev->dev, data->irq, data); 9720df4fabeSYong Wu component_master_del(&pdev->dev, &mtk_iommu_com_ops); 9730df4fabeSYong Wu return 0; 9740df4fabeSYong Wu } 9750df4fabeSYong Wu 97634665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_suspend(struct device *dev) 9770df4fabeSYong Wu { 9780df4fabeSYong Wu struct mtk_iommu_data *data = dev_get_drvdata(dev); 9790df4fabeSYong Wu struct mtk_iommu_suspend_reg *reg = &data->reg; 9800df4fabeSYong Wu void __iomem *base = data->base; 9810df4fabeSYong Wu 98235c1b48dSChao Hao reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL); 98375eed350SChao Hao reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL); 9840df4fabeSYong Wu reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS); 9850df4fabeSYong Wu reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG); 9860df4fabeSYong Wu reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0); 9870df4fabeSYong Wu reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL); 98870ca608bSYong Wu reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR); 989b9475b34SYong Wu reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG); 9906254b64fSYong Wu clk_disable_unprepare(data->bclk); 9910df4fabeSYong Wu return 0; 9920df4fabeSYong Wu } 9930df4fabeSYong Wu 99434665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_resume(struct device *dev) 9950df4fabeSYong Wu { 9960df4fabeSYong Wu struct mtk_iommu_data *data = dev_get_drvdata(dev); 9970df4fabeSYong Wu struct mtk_iommu_suspend_reg *reg = &data->reg; 998907ba6a1SYong Wu struct mtk_iommu_domain *m4u_dom = data->m4u_dom; 9990df4fabeSYong Wu void __iomem *base = data->base; 10006254b64fSYong Wu int ret; 10010df4fabeSYong Wu 10026254b64fSYong Wu ret = clk_prepare_enable(data->bclk); 10036254b64fSYong Wu if (ret) { 10046254b64fSYong Wu dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret); 10056254b64fSYong Wu return ret; 10066254b64fSYong Wu } 1007b34ea31fSDafna Hirschfeld 1008b34ea31fSDafna Hirschfeld /* 1009b34ea31fSDafna Hirschfeld * Uppon first resume, only enable the clk and return, since the values of the 1010b34ea31fSDafna Hirschfeld * registers are not yet set. 1011b34ea31fSDafna Hirschfeld */ 1012b34ea31fSDafna Hirschfeld if (!m4u_dom) 1013b34ea31fSDafna Hirschfeld return 0; 1014b34ea31fSDafna Hirschfeld 101535c1b48dSChao Hao writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL); 101675eed350SChao Hao writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL); 10170df4fabeSYong Wu writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS); 10180df4fabeSYong Wu writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG); 10190df4fabeSYong Wu writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0); 10200df4fabeSYong Wu writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL); 102170ca608bSYong Wu writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR); 1022b9475b34SYong Wu writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG); 1023c0b57581SYong Wu writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, base + REG_MMU_PT_BASE_ADDR); 10244f23f6d4SYong Wu 10254f23f6d4SYong Wu /* 10264f23f6d4SYong Wu * Users may allocate dma buffer before they call pm_runtime_get, 10274f23f6d4SYong Wu * in which case it will lack the necessary tlb flush. 10284f23f6d4SYong Wu * Thus, make sure to update the tlb after each PM resume. 10294f23f6d4SYong Wu */ 10304f23f6d4SYong Wu mtk_iommu_tlb_flush_all(data); 10310df4fabeSYong Wu return 0; 10320df4fabeSYong Wu } 10330df4fabeSYong Wu 1034e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = { 103534665c79SYong Wu SET_RUNTIME_PM_OPS(mtk_iommu_runtime_suspend, mtk_iommu_runtime_resume, NULL) 103634665c79SYong Wu SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 103734665c79SYong Wu pm_runtime_force_resume) 10380df4fabeSYong Wu }; 10390df4fabeSYong Wu 1040cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt2712_data = { 1041cecdce9dSYong Wu .m4u_plat = M4U_MT2712, 10426b717796SChao Hao .flags = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG, 1043b053bc71SChao Hao .inv_sel_reg = REG_MMU_INV_SEL_GEN1, 1044585e58f4SYong Wu .iova_region = single_domain, 1045585e58f4SYong Wu .iova_region_nr = ARRAY_SIZE(single_domain), 104637276e00SChao Hao .larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}}, 1047cecdce9dSYong Wu }; 1048cecdce9dSYong Wu 1049068c86e9SChao Hao static const struct mtk_iommu_plat_data mt6779_data = { 1050068c86e9SChao Hao .m4u_plat = M4U_MT6779, 1051068c86e9SChao Hao .flags = HAS_SUB_COMM | OUT_ORDER_WR_EN | WR_THROT_EN, 1052068c86e9SChao Hao .inv_sel_reg = REG_MMU_INV_SEL_GEN2, 1053585e58f4SYong Wu .iova_region = single_domain, 1054585e58f4SYong Wu .iova_region_nr = ARRAY_SIZE(single_domain), 1055068c86e9SChao Hao .larbid_remap = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}}, 1056cecdce9dSYong Wu }; 1057cecdce9dSYong Wu 10583c213562SFabien Parent static const struct mtk_iommu_plat_data mt8167_data = { 10593c213562SFabien Parent .m4u_plat = M4U_MT8167, 10603c213562SFabien Parent .flags = RESET_AXI | HAS_LEGACY_IVRP_PADDR, 10613c213562SFabien Parent .inv_sel_reg = REG_MMU_INV_SEL_GEN1, 1062585e58f4SYong Wu .iova_region = single_domain, 1063585e58f4SYong Wu .iova_region_nr = ARRAY_SIZE(single_domain), 10643c213562SFabien Parent .larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */ 10653c213562SFabien Parent }; 10663c213562SFabien Parent 1067cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt8173_data = { 1068cecdce9dSYong Wu .m4u_plat = M4U_MT8173, 1069d1b5ef00SFabien Parent .flags = HAS_4GB_MODE | HAS_BCLK | RESET_AXI | 1070d1b5ef00SFabien Parent HAS_LEGACY_IVRP_PADDR, 1071b053bc71SChao Hao .inv_sel_reg = REG_MMU_INV_SEL_GEN1, 1072585e58f4SYong Wu .iova_region = single_domain, 1073585e58f4SYong Wu .iova_region_nr = ARRAY_SIZE(single_domain), 107437276e00SChao Hao .larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */ 1075cecdce9dSYong Wu }; 1076cecdce9dSYong Wu 1077907ba6a1SYong Wu static const struct mtk_iommu_plat_data mt8183_data = { 1078907ba6a1SYong Wu .m4u_plat = M4U_MT8183, 10796b717796SChao Hao .flags = RESET_AXI, 1080b053bc71SChao Hao .inv_sel_reg = REG_MMU_INV_SEL_GEN1, 1081585e58f4SYong Wu .iova_region = single_domain, 1082585e58f4SYong Wu .iova_region_nr = ARRAY_SIZE(single_domain), 108337276e00SChao Hao .larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}}, 1084907ba6a1SYong Wu }; 1085907ba6a1SYong Wu 10869e3489e0SYong Wu static const struct mtk_iommu_plat_data mt8192_data = { 10879e3489e0SYong Wu .m4u_plat = M4U_MT8192, 10889e3489e0SYong Wu .flags = HAS_BCLK | HAS_SUB_COMM | OUT_ORDER_WR_EN | 10899e3489e0SYong Wu WR_THROT_EN | IOVA_34_EN, 10909e3489e0SYong Wu .inv_sel_reg = REG_MMU_INV_SEL_GEN2, 10919e3489e0SYong Wu .iova_region = mt8192_multi_dom, 10929e3489e0SYong Wu .iova_region_nr = ARRAY_SIZE(mt8192_multi_dom), 10939e3489e0SYong Wu .larbid_remap = {{0}, {1}, {4, 5}, {7}, {2}, {9, 11, 19, 20}, 10949e3489e0SYong Wu {0, 14, 16}, {0, 13, 18, 17}}, 10959e3489e0SYong Wu }; 10969e3489e0SYong Wu 10970df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = { 1098cecdce9dSYong Wu { .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data}, 1099068c86e9SChao Hao { .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data}, 11003c213562SFabien Parent { .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data}, 1101cecdce9dSYong Wu { .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data}, 1102907ba6a1SYong Wu { .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data}, 11039e3489e0SYong Wu { .compatible = "mediatek,mt8192-m4u", .data = &mt8192_data}, 11040df4fabeSYong Wu {} 11050df4fabeSYong Wu }; 11060df4fabeSYong Wu 11070df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = { 11080df4fabeSYong Wu .probe = mtk_iommu_probe, 11090df4fabeSYong Wu .remove = mtk_iommu_remove, 11100df4fabeSYong Wu .driver = { 11110df4fabeSYong Wu .name = "mtk-iommu", 1112f53dd978SKrzysztof Kozlowski .of_match_table = mtk_iommu_of_ids, 11130df4fabeSYong Wu .pm = &mtk_iommu_pm_ops, 11140df4fabeSYong Wu } 11150df4fabeSYong Wu }; 111618d8c74eSYong Wu module_platform_driver(mtk_iommu_driver); 11170df4fabeSYong Wu 111818d8c74eSYong Wu MODULE_DESCRIPTION("IOMMU API for MediaTek M4U implementations"); 111918d8c74eSYong Wu MODULE_LICENSE("GPL v2"); 1120