Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016 MediaTek Inc. |
| 3 | * Author: Yong Wu <yong.wu@mediatek.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
Yong Wu | 01e23c9 | 2016-03-14 06:01:11 +0800 | [diff] [blame] | 14 | #include <linux/bootmem.h> |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 15 | #include <linux/bug.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/component.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/dma-iommu.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/iommu.h> |
| 24 | #include <linux/iopoll.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/of_address.h> |
| 27 | #include <linux/of_iommu.h> |
| 28 | #include <linux/of_irq.h> |
| 29 | #include <linux/of_platform.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <asm/barrier.h> |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 34 | #include <soc/mediatek/smi.h> |
| 35 | |
Honghui Zhang | 9ca340c | 2016-06-08 17:50:58 +0800 | [diff] [blame] | 36 | #include "mtk_iommu.h" |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 37 | |
| 38 | #define REG_MMU_PT_BASE_ADDR 0x000 |
| 39 | |
| 40 | #define REG_MMU_INVALIDATE 0x020 |
| 41 | #define F_ALL_INVLD 0x2 |
| 42 | #define F_MMU_INV_RANGE 0x1 |
| 43 | |
| 44 | #define REG_MMU_INVLD_START_A 0x024 |
| 45 | #define REG_MMU_INVLD_END_A 0x028 |
| 46 | |
| 47 | #define REG_MMU_INV_SEL 0x038 |
| 48 | #define F_INVLD_EN0 BIT(0) |
| 49 | #define F_INVLD_EN1 BIT(1) |
| 50 | |
| 51 | #define REG_MMU_STANDARD_AXI_MODE 0x048 |
| 52 | #define REG_MMU_DCM_DIS 0x050 |
| 53 | |
| 54 | #define REG_MMU_CTRL_REG 0x110 |
| 55 | #define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4) |
| 56 | #define F_MMU_TF_PROTECT_SEL(prot) (((prot) & 0x3) << 5) |
| 57 | |
| 58 | #define REG_MMU_IVRP_PADDR 0x114 |
Yong Wu | 01e23c9 | 2016-03-14 06:01:11 +0800 | [diff] [blame] | 59 | #define F_MMU_IVRP_PA_SET(pa, ext) (((pa) >> 1) | ((!!(ext)) << 31)) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 60 | |
| 61 | #define REG_MMU_INT_CONTROL0 0x120 |
| 62 | #define F_L2_MULIT_HIT_EN BIT(0) |
| 63 | #define F_TABLE_WALK_FAULT_INT_EN BIT(1) |
| 64 | #define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2) |
| 65 | #define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3) |
| 66 | #define F_PREFETCH_FIFO_ERR_INT_EN BIT(5) |
| 67 | #define F_MISS_FIFO_ERR_INT_EN BIT(6) |
| 68 | #define F_INT_CLR_BIT BIT(12) |
| 69 | |
| 70 | #define REG_MMU_INT_MAIN_CONTROL 0x124 |
| 71 | #define F_INT_TRANSLATION_FAULT BIT(0) |
| 72 | #define F_INT_MAIN_MULTI_HIT_FAULT BIT(1) |
| 73 | #define F_INT_INVALID_PA_FAULT BIT(2) |
| 74 | #define F_INT_ENTRY_REPLACEMENT_FAULT BIT(3) |
| 75 | #define F_INT_TLB_MISS_FAULT BIT(4) |
| 76 | #define F_INT_MISS_TRANSACTION_FIFO_FAULT BIT(5) |
| 77 | #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT BIT(6) |
| 78 | |
| 79 | #define REG_MMU_CPE_DONE 0x12C |
| 80 | |
| 81 | #define REG_MMU_FAULT_ST1 0x134 |
| 82 | |
| 83 | #define REG_MMU_FAULT_VA 0x13c |
| 84 | #define F_MMU_FAULT_VA_MSK 0xfffff000 |
| 85 | #define F_MMU_FAULT_VA_WRITE_BIT BIT(1) |
| 86 | #define F_MMU_FAULT_VA_LAYER_BIT BIT(0) |
| 87 | |
| 88 | #define REG_MMU_INVLD_PA 0x140 |
| 89 | #define REG_MMU_INT_ID 0x150 |
| 90 | #define F_MMU0_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7) |
| 91 | #define F_MMU0_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f) |
| 92 | |
| 93 | #define MTK_PROTECT_PA_ALIGN 128 |
| 94 | |
Yong Wu | a9467d9 | 2017-08-21 19:00:15 +0800 | [diff] [blame^] | 95 | /* |
| 96 | * Get the local arbiter ID and the portid within the larb arbiter |
| 97 | * from mtk_m4u_id which is defined by MTK_M4U_ID. |
| 98 | */ |
| 99 | #define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0x7) |
| 100 | #define MTK_M4U_TO_PORT(id) ((id) & 0x1f) |
| 101 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 102 | struct mtk_iommu_domain { |
| 103 | spinlock_t pgtlock; /* lock for page table */ |
| 104 | |
| 105 | struct io_pgtable_cfg cfg; |
| 106 | struct io_pgtable_ops *iop; |
| 107 | |
| 108 | struct iommu_domain domain; |
| 109 | }; |
| 110 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 111 | static struct iommu_ops mtk_iommu_ops; |
| 112 | |
| 113 | static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom) |
| 114 | { |
| 115 | return container_of(dom, struct mtk_iommu_domain, domain); |
| 116 | } |
| 117 | |
| 118 | static void mtk_iommu_tlb_flush_all(void *cookie) |
| 119 | { |
| 120 | struct mtk_iommu_data *data = cookie; |
| 121 | |
| 122 | writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, data->base + REG_MMU_INV_SEL); |
| 123 | writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE); |
| 124 | wmb(); /* Make sure the tlb flush all done */ |
| 125 | } |
| 126 | |
| 127 | static void mtk_iommu_tlb_add_flush_nosync(unsigned long iova, size_t size, |
| 128 | size_t granule, bool leaf, |
| 129 | void *cookie) |
| 130 | { |
| 131 | struct mtk_iommu_data *data = cookie; |
| 132 | |
| 133 | writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, data->base + REG_MMU_INV_SEL); |
| 134 | |
| 135 | writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A); |
| 136 | writel_relaxed(iova + size - 1, data->base + REG_MMU_INVLD_END_A); |
| 137 | writel_relaxed(F_MMU_INV_RANGE, data->base + REG_MMU_INVALIDATE); |
Robin Murphy | 98a8f63 | 2017-07-06 17:55:30 +0100 | [diff] [blame] | 138 | data->tlb_flush_active = true; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void mtk_iommu_tlb_sync(void *cookie) |
| 142 | { |
| 143 | struct mtk_iommu_data *data = cookie; |
| 144 | int ret; |
| 145 | u32 tmp; |
| 146 | |
Robin Murphy | 98a8f63 | 2017-07-06 17:55:30 +0100 | [diff] [blame] | 147 | /* Avoid timing out if there's nothing to wait for */ |
| 148 | if (!data->tlb_flush_active) |
| 149 | return; |
| 150 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 151 | ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE, tmp, |
| 152 | tmp != 0, 10, 100000); |
| 153 | if (ret) { |
| 154 | dev_warn(data->dev, |
| 155 | "Partial TLB flush timed out, falling back to full flush\n"); |
| 156 | mtk_iommu_tlb_flush_all(cookie); |
| 157 | } |
| 158 | /* Clear the CPE status */ |
| 159 | writel_relaxed(0, data->base + REG_MMU_CPE_DONE); |
Robin Murphy | 98a8f63 | 2017-07-06 17:55:30 +0100 | [diff] [blame] | 160 | data->tlb_flush_active = false; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static const struct iommu_gather_ops mtk_iommu_gather_ops = { |
| 164 | .tlb_flush_all = mtk_iommu_tlb_flush_all, |
| 165 | .tlb_add_flush = mtk_iommu_tlb_add_flush_nosync, |
| 166 | .tlb_sync = mtk_iommu_tlb_sync, |
| 167 | }; |
| 168 | |
| 169 | static irqreturn_t mtk_iommu_isr(int irq, void *dev_id) |
| 170 | { |
| 171 | struct mtk_iommu_data *data = dev_id; |
| 172 | struct mtk_iommu_domain *dom = data->m4u_dom; |
| 173 | u32 int_state, regval, fault_iova, fault_pa; |
| 174 | unsigned int fault_larb, fault_port; |
| 175 | bool layer, write; |
| 176 | |
| 177 | /* Read error info from registers */ |
| 178 | int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1); |
| 179 | fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA); |
| 180 | layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT; |
| 181 | write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT; |
| 182 | fault_iova &= F_MMU_FAULT_VA_MSK; |
| 183 | fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA); |
| 184 | regval = readl_relaxed(data->base + REG_MMU_INT_ID); |
| 185 | fault_larb = F_MMU0_INT_ID_LARB_ID(regval); |
| 186 | fault_port = F_MMU0_INT_ID_PORT_ID(regval); |
| 187 | |
| 188 | if (report_iommu_fault(&dom->domain, data->dev, fault_iova, |
| 189 | write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) { |
| 190 | dev_err_ratelimited( |
| 191 | data->dev, |
| 192 | "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n", |
| 193 | int_state, fault_iova, fault_pa, fault_larb, fault_port, |
| 194 | layer, write ? "write" : "read"); |
| 195 | } |
| 196 | |
| 197 | /* Interrupt clear */ |
| 198 | regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0); |
| 199 | regval |= F_INT_CLR_BIT; |
| 200 | writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0); |
| 201 | |
| 202 | mtk_iommu_tlb_flush_all(data); |
| 203 | |
| 204 | return IRQ_HANDLED; |
| 205 | } |
| 206 | |
| 207 | static void mtk_iommu_config(struct mtk_iommu_data *data, |
| 208 | struct device *dev, bool enable) |
| 209 | { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 210 | struct mtk_smi_larb_iommu *larb_mmu; |
| 211 | unsigned int larbid, portid; |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 212 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 213 | int i; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 214 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 215 | for (i = 0; i < fwspec->num_ids; ++i) { |
| 216 | larbid = MTK_M4U_TO_LARB(fwspec->ids[i]); |
| 217 | portid = MTK_M4U_TO_PORT(fwspec->ids[i]); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 218 | larb_mmu = &data->smi_imu.larb_imu[larbid]; |
| 219 | |
| 220 | dev_dbg(dev, "%s iommu port: %d\n", |
| 221 | enable ? "enable" : "disable", portid); |
| 222 | |
| 223 | if (enable) |
| 224 | larb_mmu->mmu |= MTK_SMI_MMU_EN(portid); |
| 225 | else |
| 226 | larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static int mtk_iommu_domain_finalise(struct mtk_iommu_data *data) |
| 231 | { |
| 232 | struct mtk_iommu_domain *dom = data->m4u_dom; |
| 233 | |
| 234 | spin_lock_init(&dom->pgtlock); |
| 235 | |
| 236 | dom->cfg = (struct io_pgtable_cfg) { |
| 237 | .quirks = IO_PGTABLE_QUIRK_ARM_NS | |
| 238 | IO_PGTABLE_QUIRK_NO_PERMS | |
| 239 | IO_PGTABLE_QUIRK_TLBI_ON_MAP, |
| 240 | .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap, |
| 241 | .ias = 32, |
| 242 | .oas = 32, |
| 243 | .tlb = &mtk_iommu_gather_ops, |
| 244 | .iommu_dev = data->dev, |
| 245 | }; |
| 246 | |
Yong Wu | 01e23c9 | 2016-03-14 06:01:11 +0800 | [diff] [blame] | 247 | if (data->enable_4GB) |
| 248 | dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_4GB; |
| 249 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 250 | dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data); |
| 251 | if (!dom->iop) { |
| 252 | dev_err(data->dev, "Failed to alloc io pgtable\n"); |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | /* Update our support page sizes bitmap */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 257 | dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 258 | |
| 259 | writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0], |
| 260 | data->base + REG_MMU_PT_BASE_ADDR); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type) |
| 265 | { |
| 266 | struct mtk_iommu_domain *dom; |
| 267 | |
| 268 | if (type != IOMMU_DOMAIN_DMA) |
| 269 | return NULL; |
| 270 | |
| 271 | dom = kzalloc(sizeof(*dom), GFP_KERNEL); |
| 272 | if (!dom) |
| 273 | return NULL; |
| 274 | |
| 275 | if (iommu_get_dma_cookie(&dom->domain)) { |
| 276 | kfree(dom); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | dom->domain.geometry.aperture_start = 0; |
| 281 | dom->domain.geometry.aperture_end = DMA_BIT_MASK(32); |
| 282 | dom->domain.geometry.force_aperture = true; |
| 283 | |
| 284 | return &dom->domain; |
| 285 | } |
| 286 | |
| 287 | static void mtk_iommu_domain_free(struct iommu_domain *domain) |
| 288 | { |
| 289 | iommu_put_dma_cookie(domain); |
| 290 | kfree(to_mtk_domain(domain)); |
| 291 | } |
| 292 | |
| 293 | static int mtk_iommu_attach_device(struct iommu_domain *domain, |
| 294 | struct device *dev) |
| 295 | { |
| 296 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 297 | struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 298 | int ret; |
| 299 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 300 | if (!data) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 301 | return -ENODEV; |
| 302 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 303 | if (!data->m4u_dom) { |
| 304 | data->m4u_dom = dom; |
| 305 | ret = mtk_iommu_domain_finalise(data); |
| 306 | if (ret) { |
| 307 | data->m4u_dom = NULL; |
| 308 | return ret; |
| 309 | } |
| 310 | } else if (data->m4u_dom != dom) { |
| 311 | /* All the client devices should be in the same m4u domain */ |
| 312 | dev_err(dev, "try to attach into the error iommu domain\n"); |
| 313 | return -EPERM; |
| 314 | } |
| 315 | |
| 316 | mtk_iommu_config(data, dev, true); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void mtk_iommu_detach_device(struct iommu_domain *domain, |
| 321 | struct device *dev) |
| 322 | { |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 323 | struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 324 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 325 | if (!data) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 326 | return; |
| 327 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 328 | mtk_iommu_config(data, dev, false); |
| 329 | } |
| 330 | |
| 331 | static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 332 | phys_addr_t paddr, size_t size, int prot) |
| 333 | { |
| 334 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
| 335 | unsigned long flags; |
| 336 | int ret; |
| 337 | |
| 338 | spin_lock_irqsave(&dom->pgtlock, flags); |
| 339 | ret = dom->iop->map(dom->iop, iova, paddr, size, prot); |
| 340 | spin_unlock_irqrestore(&dom->pgtlock, flags); |
| 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | static size_t mtk_iommu_unmap(struct iommu_domain *domain, |
| 346 | unsigned long iova, size_t size) |
| 347 | { |
| 348 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
| 349 | unsigned long flags; |
| 350 | size_t unmapsz; |
| 351 | |
| 352 | spin_lock_irqsave(&dom->pgtlock, flags); |
| 353 | unmapsz = dom->iop->unmap(dom->iop, iova, size); |
| 354 | spin_unlock_irqrestore(&dom->pgtlock, flags); |
| 355 | |
| 356 | return unmapsz; |
| 357 | } |
| 358 | |
| 359 | static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain, |
| 360 | dma_addr_t iova) |
| 361 | { |
| 362 | struct mtk_iommu_domain *dom = to_mtk_domain(domain); |
| 363 | unsigned long flags; |
| 364 | phys_addr_t pa; |
| 365 | |
| 366 | spin_lock_irqsave(&dom->pgtlock, flags); |
| 367 | pa = dom->iop->iova_to_phys(dom->iop, iova); |
| 368 | spin_unlock_irqrestore(&dom->pgtlock, flags); |
| 369 | |
| 370 | return pa; |
| 371 | } |
| 372 | |
| 373 | static int mtk_iommu_add_device(struct device *dev) |
| 374 | { |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 375 | struct mtk_iommu_data *data; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 376 | struct iommu_group *group; |
| 377 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 378 | if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops) |
| 379 | return -ENODEV; /* Not a iommu client device */ |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 380 | |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 381 | data = dev->iommu_fwspec->iommu_priv; |
| 382 | iommu_device_link(&data->iommu, dev); |
| 383 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 384 | group = iommu_group_get_for_dev(dev); |
| 385 | if (IS_ERR(group)) |
| 386 | return PTR_ERR(group); |
| 387 | |
| 388 | iommu_group_put(group); |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static void mtk_iommu_remove_device(struct device *dev) |
| 393 | { |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 394 | struct mtk_iommu_data *data; |
| 395 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 396 | if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 397 | return; |
| 398 | |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 399 | data = dev->iommu_fwspec->iommu_priv; |
| 400 | iommu_device_unlink(&data->iommu, dev); |
| 401 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 402 | iommu_group_remove_device(dev); |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 403 | iommu_fwspec_free(dev); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static struct iommu_group *mtk_iommu_device_group(struct device *dev) |
| 407 | { |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 408 | struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv; |
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 ERR_PTR(-ENODEV); |
| 412 | |
| 413 | /* All the client devices are in the same m4u iommu-group */ |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 414 | if (!data->m4u_group) { |
| 415 | data->m4u_group = iommu_group_alloc(); |
| 416 | if (IS_ERR(data->m4u_group)) |
| 417 | dev_err(dev, "Failed to allocate M4U IOMMU group\n"); |
Robin Murphy | 3a8d40b | 2016-11-11 17:59:24 +0000 | [diff] [blame] | 418 | } else { |
| 419 | iommu_group_ref_get(data->m4u_group); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 420 | } |
| 421 | return data->m4u_group; |
| 422 | } |
| 423 | |
| 424 | static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args) |
| 425 | { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 426 | struct platform_device *m4updev; |
| 427 | |
| 428 | if (args->args_count != 1) { |
| 429 | dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n", |
| 430 | args->args_count); |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 434 | if (!dev->iommu_fwspec->iommu_priv) { |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 435 | /* Get the m4u device */ |
| 436 | m4updev = of_find_device_by_node(args->np); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 437 | if (WARN_ON(!m4updev)) |
| 438 | return -EINVAL; |
| 439 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 440 | dev->iommu_fwspec->iommu_priv = platform_get_drvdata(m4updev); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 441 | } |
| 442 | |
Robin Murphy | 58f0d1d | 2016-10-17 12:49:20 +0100 | [diff] [blame] | 443 | return iommu_fwspec_add_ids(dev, args->args, 1); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static struct iommu_ops mtk_iommu_ops = { |
| 447 | .domain_alloc = mtk_iommu_domain_alloc, |
| 448 | .domain_free = mtk_iommu_domain_free, |
| 449 | .attach_dev = mtk_iommu_attach_device, |
| 450 | .detach_dev = mtk_iommu_detach_device, |
| 451 | .map = mtk_iommu_map, |
| 452 | .unmap = mtk_iommu_unmap, |
| 453 | .map_sg = default_iommu_map_sg, |
| 454 | .iova_to_phys = mtk_iommu_iova_to_phys, |
| 455 | .add_device = mtk_iommu_add_device, |
| 456 | .remove_device = mtk_iommu_remove_device, |
| 457 | .device_group = mtk_iommu_device_group, |
| 458 | .of_xlate = mtk_iommu_of_xlate, |
| 459 | .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M, |
| 460 | }; |
| 461 | |
| 462 | static int mtk_iommu_hw_init(const struct mtk_iommu_data *data) |
| 463 | { |
| 464 | u32 regval; |
| 465 | int ret; |
| 466 | |
| 467 | ret = clk_prepare_enable(data->bclk); |
| 468 | if (ret) { |
| 469 | dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret); |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | regval = F_MMU_PREFETCH_RT_REPLACE_MOD | |
| 474 | F_MMU_TF_PROTECT_SEL(2); |
| 475 | writel_relaxed(regval, data->base + REG_MMU_CTRL_REG); |
| 476 | |
| 477 | regval = F_L2_MULIT_HIT_EN | |
| 478 | F_TABLE_WALK_FAULT_INT_EN | |
| 479 | F_PREETCH_FIFO_OVERFLOW_INT_EN | |
| 480 | F_MISS_FIFO_OVERFLOW_INT_EN | |
| 481 | F_PREFETCH_FIFO_ERR_INT_EN | |
| 482 | F_MISS_FIFO_ERR_INT_EN; |
| 483 | writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0); |
| 484 | |
| 485 | regval = F_INT_TRANSLATION_FAULT | |
| 486 | F_INT_MAIN_MULTI_HIT_FAULT | |
| 487 | F_INT_INVALID_PA_FAULT | |
| 488 | F_INT_ENTRY_REPLACEMENT_FAULT | |
| 489 | F_INT_TLB_MISS_FAULT | |
| 490 | F_INT_MISS_TRANSACTION_FIFO_FAULT | |
| 491 | F_INT_PRETETCH_TRANSATION_FIFO_FAULT; |
| 492 | writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL); |
| 493 | |
Yong Wu | 01e23c9 | 2016-03-14 06:01:11 +0800 | [diff] [blame] | 494 | writel_relaxed(F_MMU_IVRP_PA_SET(data->protect_base, data->enable_4GB), |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 495 | data->base + REG_MMU_IVRP_PADDR); |
| 496 | |
| 497 | writel_relaxed(0, data->base + REG_MMU_DCM_DIS); |
| 498 | writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE); |
| 499 | |
| 500 | if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0, |
| 501 | dev_name(data->dev), (void *)data)) { |
| 502 | writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR); |
| 503 | clk_disable_unprepare(data->bclk); |
| 504 | dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq); |
| 505 | return -ENODEV; |
| 506 | } |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 511 | static const struct component_master_ops mtk_iommu_com_ops = { |
| 512 | .bind = mtk_iommu_bind, |
| 513 | .unbind = mtk_iommu_unbind, |
| 514 | }; |
| 515 | |
| 516 | static int mtk_iommu_probe(struct platform_device *pdev) |
| 517 | { |
| 518 | struct mtk_iommu_data *data; |
| 519 | struct device *dev = &pdev->dev; |
| 520 | struct resource *res; |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 521 | resource_size_t ioaddr; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 522 | struct component_match *match = NULL; |
| 523 | void *protect; |
Andrzej Hajda | 0b6c0ad | 2016-03-01 10:36:23 +0100 | [diff] [blame] | 524 | int i, larb_nr, ret; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 525 | |
| 526 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 527 | if (!data) |
| 528 | return -ENOMEM; |
| 529 | data->dev = dev; |
| 530 | |
| 531 | /* Protect memory. HW will access here while translation fault.*/ |
| 532 | protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL); |
| 533 | if (!protect) |
| 534 | return -ENOMEM; |
| 535 | data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN); |
| 536 | |
Yong Wu | 01e23c9 | 2016-03-14 06:01:11 +0800 | [diff] [blame] | 537 | /* Whether the current dram is over 4GB */ |
| 538 | data->enable_4GB = !!(max_pfn > (0xffffffffUL >> PAGE_SHIFT)); |
| 539 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 540 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 541 | data->base = devm_ioremap_resource(dev, res); |
| 542 | if (IS_ERR(data->base)) |
| 543 | return PTR_ERR(data->base); |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 544 | ioaddr = res->start; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 545 | |
| 546 | data->irq = platform_get_irq(pdev, 0); |
| 547 | if (data->irq < 0) |
| 548 | return data->irq; |
| 549 | |
| 550 | data->bclk = devm_clk_get(dev, "bclk"); |
| 551 | if (IS_ERR(data->bclk)) |
| 552 | return PTR_ERR(data->bclk); |
| 553 | |
| 554 | larb_nr = of_count_phandle_with_args(dev->of_node, |
| 555 | "mediatek,larbs", NULL); |
| 556 | if (larb_nr < 0) |
| 557 | return larb_nr; |
| 558 | data->smi_imu.larb_nr = larb_nr; |
| 559 | |
| 560 | for (i = 0; i < larb_nr; i++) { |
| 561 | struct device_node *larbnode; |
| 562 | struct platform_device *plarbdev; |
| 563 | |
| 564 | larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i); |
| 565 | if (!larbnode) |
| 566 | return -EINVAL; |
| 567 | |
| 568 | if (!of_device_is_available(larbnode)) |
| 569 | continue; |
| 570 | |
| 571 | plarbdev = of_find_device_by_node(larbnode); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 572 | if (!plarbdev) { |
| 573 | plarbdev = of_platform_device_create( |
| 574 | larbnode, NULL, |
| 575 | platform_bus_type.dev_root); |
Russell King | 00c7c81 | 2016-10-19 11:30:34 +0100 | [diff] [blame] | 576 | if (!plarbdev) { |
| 577 | of_node_put(larbnode); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 578 | return -EPROBE_DEFER; |
Russell King | 00c7c81 | 2016-10-19 11:30:34 +0100 | [diff] [blame] | 579 | } |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 580 | } |
| 581 | data->smi_imu.larb_imu[i].dev = &plarbdev->dev; |
| 582 | |
Russell King | 00c7c81 | 2016-10-19 11:30:34 +0100 | [diff] [blame] | 583 | component_match_add_release(dev, &match, release_of, |
| 584 | compare_of, larbnode); |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | platform_set_drvdata(pdev, data); |
| 588 | |
| 589 | ret = mtk_iommu_hw_init(data); |
| 590 | if (ret) |
| 591 | return ret; |
| 592 | |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 593 | ret = iommu_device_sysfs_add(&data->iommu, dev, NULL, |
| 594 | "mtk-iommu.%pa", &ioaddr); |
| 595 | if (ret) |
| 596 | return ret; |
| 597 | |
| 598 | iommu_device_set_ops(&data->iommu, &mtk_iommu_ops); |
| 599 | iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode); |
| 600 | |
| 601 | ret = iommu_device_register(&data->iommu); |
| 602 | if (ret) |
| 603 | return ret; |
| 604 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 605 | if (!iommu_present(&platform_bus_type)) |
| 606 | bus_set_iommu(&platform_bus_type, &mtk_iommu_ops); |
| 607 | |
| 608 | return component_master_add_with_match(dev, &mtk_iommu_com_ops, match); |
| 609 | } |
| 610 | |
| 611 | static int mtk_iommu_remove(struct platform_device *pdev) |
| 612 | { |
| 613 | struct mtk_iommu_data *data = platform_get_drvdata(pdev); |
| 614 | |
Joerg Roedel | b16c017 | 2017-02-03 12:57:32 +0100 | [diff] [blame] | 615 | iommu_device_sysfs_remove(&data->iommu); |
| 616 | iommu_device_unregister(&data->iommu); |
| 617 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 618 | if (iommu_present(&platform_bus_type)) |
| 619 | bus_set_iommu(&platform_bus_type, NULL); |
| 620 | |
| 621 | free_io_pgtable_ops(data->m4u_dom->iop); |
| 622 | clk_disable_unprepare(data->bclk); |
| 623 | devm_free_irq(&pdev->dev, data->irq, data); |
| 624 | component_master_del(&pdev->dev, &mtk_iommu_com_ops); |
| 625 | return 0; |
| 626 | } |
| 627 | |
Arnd Bergmann | fd99f79 | 2016-02-29 10:19:07 +0100 | [diff] [blame] | 628 | static int __maybe_unused mtk_iommu_suspend(struct device *dev) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 629 | { |
| 630 | struct mtk_iommu_data *data = dev_get_drvdata(dev); |
| 631 | struct mtk_iommu_suspend_reg *reg = &data->reg; |
| 632 | void __iomem *base = data->base; |
| 633 | |
| 634 | reg->standard_axi_mode = readl_relaxed(base + |
| 635 | REG_MMU_STANDARD_AXI_MODE); |
| 636 | reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS); |
| 637 | reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG); |
| 638 | reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0); |
| 639 | reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL); |
| 640 | return 0; |
| 641 | } |
| 642 | |
Arnd Bergmann | fd99f79 | 2016-02-29 10:19:07 +0100 | [diff] [blame] | 643 | static int __maybe_unused mtk_iommu_resume(struct device *dev) |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 644 | { |
| 645 | struct mtk_iommu_data *data = dev_get_drvdata(dev); |
| 646 | struct mtk_iommu_suspend_reg *reg = &data->reg; |
| 647 | void __iomem *base = data->base; |
| 648 | |
| 649 | writel_relaxed(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0], |
| 650 | base + REG_MMU_PT_BASE_ADDR); |
| 651 | writel_relaxed(reg->standard_axi_mode, |
| 652 | base + REG_MMU_STANDARD_AXI_MODE); |
| 653 | writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS); |
| 654 | writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG); |
| 655 | writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0); |
| 656 | writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL); |
Yong Wu | 01e23c9 | 2016-03-14 06:01:11 +0800 | [diff] [blame] | 657 | writel_relaxed(F_MMU_IVRP_PA_SET(data->protect_base, data->enable_4GB), |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 658 | base + REG_MMU_IVRP_PADDR); |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | const struct dev_pm_ops mtk_iommu_pm_ops = { |
| 663 | SET_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume) |
| 664 | }; |
| 665 | |
| 666 | static const struct of_device_id mtk_iommu_of_ids[] = { |
| 667 | { .compatible = "mediatek,mt8173-m4u", }, |
| 668 | {} |
| 669 | }; |
| 670 | |
| 671 | static struct platform_driver mtk_iommu_driver = { |
| 672 | .probe = mtk_iommu_probe, |
| 673 | .remove = mtk_iommu_remove, |
| 674 | .driver = { |
| 675 | .name = "mtk-iommu", |
| 676 | .of_match_table = mtk_iommu_of_ids, |
| 677 | .pm = &mtk_iommu_pm_ops, |
| 678 | } |
| 679 | }; |
| 680 | |
| 681 | static int mtk_iommu_init_fn(struct device_node *np) |
| 682 | { |
| 683 | int ret; |
| 684 | struct platform_device *pdev; |
| 685 | |
| 686 | pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root); |
Dan Carpenter | 3189e49 | 2016-03-02 13:10:27 +0300 | [diff] [blame] | 687 | if (!pdev) |
| 688 | return -ENOMEM; |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 689 | |
| 690 | ret = platform_driver_register(&mtk_iommu_driver); |
| 691 | if (ret) { |
| 692 | pr_err("%s: Failed to register driver\n", __func__); |
| 693 | return ret; |
| 694 | } |
| 695 | |
Yong Wu | 0df4fab | 2016-02-23 01:20:50 +0800 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | IOMMU_OF_DECLARE(mtkm4u, "mediatek,mt8173-m4u", mtk_iommu_init_fn); |