mtk_iommu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. */
  14. #include <linux/bootmem.h>
  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>
  34. #include <dt-bindings/memory/mt8173-larb-port.h>
  35. #include <soc/mediatek/smi.h>
  36. #include "mtk_iommu.h"
  37. #define REG_MMU_PT_BASE_ADDR 0x000
  38. #define REG_MMU_INVALIDATE 0x020
  39. #define F_ALL_INVLD 0x2
  40. #define F_MMU_INV_RANGE 0x1
  41. #define REG_MMU_INVLD_START_A 0x024
  42. #define REG_MMU_INVLD_END_A 0x028
  43. #define REG_MMU_INV_SEL 0x038
  44. #define F_INVLD_EN0 BIT(0)
  45. #define F_INVLD_EN1 BIT(1)
  46. #define REG_MMU_STANDARD_AXI_MODE 0x048
  47. #define REG_MMU_DCM_DIS 0x050
  48. #define REG_MMU_CTRL_REG 0x110
  49. #define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4)
  50. #define F_MMU_TF_PROTECT_SEL(prot) (((prot) & 0x3) << 5)
  51. #define REG_MMU_IVRP_PADDR 0x114
  52. #define F_MMU_IVRP_PA_SET(pa, ext) (((pa) >> 1) | ((!!(ext)) << 31))
  53. #define REG_MMU_INT_CONTROL0 0x120
  54. #define F_L2_MULIT_HIT_EN BIT(0)
  55. #define F_TABLE_WALK_FAULT_INT_EN BIT(1)
  56. #define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2)
  57. #define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3)
  58. #define F_PREFETCH_FIFO_ERR_INT_EN BIT(5)
  59. #define F_MISS_FIFO_ERR_INT_EN BIT(6)
  60. #define F_INT_CLR_BIT BIT(12)
  61. #define REG_MMU_INT_MAIN_CONTROL 0x124
  62. #define F_INT_TRANSLATION_FAULT BIT(0)
  63. #define F_INT_MAIN_MULTI_HIT_FAULT BIT(1)
  64. #define F_INT_INVALID_PA_FAULT BIT(2)
  65. #define F_INT_ENTRY_REPLACEMENT_FAULT BIT(3)
  66. #define F_INT_TLB_MISS_FAULT BIT(4)
  67. #define F_INT_MISS_TRANSACTION_FIFO_FAULT BIT(5)
  68. #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT BIT(6)
  69. #define REG_MMU_CPE_DONE 0x12C
  70. #define REG_MMU_FAULT_ST1 0x134
  71. #define REG_MMU_FAULT_VA 0x13c
  72. #define F_MMU_FAULT_VA_MSK 0xfffff000
  73. #define F_MMU_FAULT_VA_WRITE_BIT BIT(1)
  74. #define F_MMU_FAULT_VA_LAYER_BIT BIT(0)
  75. #define REG_MMU_INVLD_PA 0x140
  76. #define REG_MMU_INT_ID 0x150
  77. #define F_MMU0_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7)
  78. #define F_MMU0_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f)
  79. #define MTK_PROTECT_PA_ALIGN 128
  80. struct mtk_iommu_domain {
  81. spinlock_t pgtlock; /* lock for page table */
  82. struct io_pgtable_cfg cfg;
  83. struct io_pgtable_ops *iop;
  84. struct iommu_domain domain;
  85. };
  86. static struct iommu_ops mtk_iommu_ops;
  87. static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
  88. {
  89. return container_of(dom, struct mtk_iommu_domain, domain);
  90. }
  91. static void mtk_iommu_tlb_flush_all(void *cookie)
  92. {
  93. struct mtk_iommu_data *data = cookie;
  94. writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, data->base + REG_MMU_INV_SEL);
  95. writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
  96. wmb(); /* Make sure the tlb flush all done */
  97. }
  98. static void mtk_iommu_tlb_add_flush_nosync(unsigned long iova, size_t size,
  99. size_t granule, bool leaf,
  100. void *cookie)
  101. {
  102. struct mtk_iommu_data *data = cookie;
  103. writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, data->base + REG_MMU_INV_SEL);
  104. writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
  105. writel_relaxed(iova + size - 1, data->base + REG_MMU_INVLD_END_A);
  106. writel_relaxed(F_MMU_INV_RANGE, data->base + REG_MMU_INVALIDATE);
  107. }
  108. static void mtk_iommu_tlb_sync(void *cookie)
  109. {
  110. struct mtk_iommu_data *data = cookie;
  111. int ret;
  112. u32 tmp;
  113. ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE, tmp,
  114. tmp != 0, 10, 100000);
  115. if (ret) {
  116. dev_warn(data->dev,
  117. "Partial TLB flush timed out, falling back to full flush\n");
  118. mtk_iommu_tlb_flush_all(cookie);
  119. }
  120. /* Clear the CPE status */
  121. writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
  122. }
  123. static const struct iommu_gather_ops mtk_iommu_gather_ops = {
  124. .tlb_flush_all = mtk_iommu_tlb_flush_all,
  125. .tlb_add_flush = mtk_iommu_tlb_add_flush_nosync,
  126. .tlb_sync = mtk_iommu_tlb_sync,
  127. };
  128. static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
  129. {
  130. struct mtk_iommu_data *data = dev_id;
  131. struct mtk_iommu_domain *dom = data->m4u_dom;
  132. u32 int_state, regval, fault_iova, fault_pa;
  133. unsigned int fault_larb, fault_port;
  134. bool layer, write;
  135. /* Read error info from registers */
  136. int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
  137. fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
  138. layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
  139. write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
  140. fault_iova &= F_MMU_FAULT_VA_MSK;
  141. fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
  142. regval = readl_relaxed(data->base + REG_MMU_INT_ID);
  143. fault_larb = F_MMU0_INT_ID_LARB_ID(regval);
  144. fault_port = F_MMU0_INT_ID_PORT_ID(regval);
  145. if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
  146. write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
  147. dev_err_ratelimited(
  148. data->dev,
  149. "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
  150. int_state, fault_iova, fault_pa, fault_larb, fault_port,
  151. layer, write ? "write" : "read");
  152. }
  153. /* Interrupt clear */
  154. regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
  155. regval |= F_INT_CLR_BIT;
  156. writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
  157. mtk_iommu_tlb_flush_all(data);
  158. return IRQ_HANDLED;
  159. }
  160. static void mtk_iommu_config(struct mtk_iommu_data *data,
  161. struct device *dev, bool enable)
  162. {
  163. struct mtk_smi_larb_iommu *larb_mmu;
  164. unsigned int larbid, portid;
  165. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  166. int i;
  167. for (i = 0; i < fwspec->num_ids; ++i) {
  168. larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
  169. portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
  170. larb_mmu = &data->smi_imu.larb_imu[larbid];
  171. dev_dbg(dev, "%s iommu port: %d\n",
  172. enable ? "enable" : "disable", portid);
  173. if (enable)
  174. larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
  175. else
  176. larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
  177. }
  178. }
  179. static int mtk_iommu_domain_finalise(struct mtk_iommu_data *data)
  180. {
  181. struct mtk_iommu_domain *dom = data->m4u_dom;
  182. spin_lock_init(&dom->pgtlock);
  183. dom->cfg = (struct io_pgtable_cfg) {
  184. .quirks = IO_PGTABLE_QUIRK_ARM_NS |
  185. IO_PGTABLE_QUIRK_NO_PERMS |
  186. IO_PGTABLE_QUIRK_TLBI_ON_MAP,
  187. .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
  188. .ias = 32,
  189. .oas = 32,
  190. .tlb = &mtk_iommu_gather_ops,
  191. .iommu_dev = data->dev,
  192. };
  193. if (data->enable_4GB)
  194. dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_4GB;
  195. dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
  196. if (!dom->iop) {
  197. dev_err(data->dev, "Failed to alloc io pgtable\n");
  198. return -EINVAL;
  199. }
  200. /* Update our support page sizes bitmap */
  201. dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
  202. writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0],
  203. data->base + REG_MMU_PT_BASE_ADDR);
  204. return 0;
  205. }
  206. static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
  207. {
  208. struct mtk_iommu_domain *dom;
  209. if (type != IOMMU_DOMAIN_DMA)
  210. return NULL;
  211. dom = kzalloc(sizeof(*dom), GFP_KERNEL);
  212. if (!dom)
  213. return NULL;
  214. if (iommu_get_dma_cookie(&dom->domain)) {
  215. kfree(dom);
  216. return NULL;
  217. }
  218. dom->domain.geometry.aperture_start = 0;
  219. dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
  220. dom->domain.geometry.force_aperture = true;
  221. return &dom->domain;
  222. }
  223. static void mtk_iommu_domain_free(struct iommu_domain *domain)
  224. {
  225. iommu_put_dma_cookie(domain);
  226. kfree(to_mtk_domain(domain));
  227. }
  228. static int mtk_iommu_attach_device(struct iommu_domain *domain,
  229. struct device *dev)
  230. {
  231. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  232. struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
  233. int ret;
  234. if (!data)
  235. return -ENODEV;
  236. if (!data->m4u_dom) {
  237. data->m4u_dom = dom;
  238. ret = mtk_iommu_domain_finalise(data);
  239. if (ret) {
  240. data->m4u_dom = NULL;
  241. return ret;
  242. }
  243. } else if (data->m4u_dom != dom) {
  244. /* All the client devices should be in the same m4u domain */
  245. dev_err(dev, "try to attach into the error iommu domain\n");
  246. return -EPERM;
  247. }
  248. mtk_iommu_config(data, dev, true);
  249. return 0;
  250. }
  251. static void mtk_iommu_detach_device(struct iommu_domain *domain,
  252. struct device *dev)
  253. {
  254. struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
  255. if (!data)
  256. return;
  257. mtk_iommu_config(data, dev, false);
  258. }
  259. static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
  260. phys_addr_t paddr, size_t size, int prot)
  261. {
  262. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  263. unsigned long flags;
  264. int ret;
  265. spin_lock_irqsave(&dom->pgtlock, flags);
  266. ret = dom->iop->map(dom->iop, iova, paddr, size, prot);
  267. spin_unlock_irqrestore(&dom->pgtlock, flags);
  268. return ret;
  269. }
  270. static size_t mtk_iommu_unmap(struct iommu_domain *domain,
  271. unsigned long iova, size_t size)
  272. {
  273. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  274. unsigned long flags;
  275. size_t unmapsz;
  276. spin_lock_irqsave(&dom->pgtlock, flags);
  277. unmapsz = dom->iop->unmap(dom->iop, iova, size);
  278. spin_unlock_irqrestore(&dom->pgtlock, flags);
  279. return unmapsz;
  280. }
  281. static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
  282. dma_addr_t iova)
  283. {
  284. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  285. unsigned long flags;
  286. phys_addr_t pa;
  287. spin_lock_irqsave(&dom->pgtlock, flags);
  288. pa = dom->iop->iova_to_phys(dom->iop, iova);
  289. spin_unlock_irqrestore(&dom->pgtlock, flags);
  290. return pa;
  291. }
  292. static int mtk_iommu_add_device(struct device *dev)
  293. {
  294. struct iommu_group *group;
  295. if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)
  296. return -ENODEV; /* Not a iommu client device */
  297. group = iommu_group_get_for_dev(dev);
  298. if (IS_ERR(group))
  299. return PTR_ERR(group);
  300. iommu_group_put(group);
  301. return 0;
  302. }
  303. static void mtk_iommu_remove_device(struct device *dev)
  304. {
  305. if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)
  306. return;
  307. iommu_group_remove_device(dev);
  308. iommu_fwspec_free(dev);
  309. }
  310. static struct iommu_group *mtk_iommu_device_group(struct device *dev)
  311. {
  312. struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
  313. if (!data)
  314. return ERR_PTR(-ENODEV);
  315. /* All the client devices are in the same m4u iommu-group */
  316. if (!data->m4u_group) {
  317. data->m4u_group = iommu_group_alloc();
  318. if (IS_ERR(data->m4u_group))
  319. dev_err(dev, "Failed to allocate M4U IOMMU group\n");
  320. } else {
  321. iommu_group_ref_get(data->m4u_group);
  322. }
  323. return data->m4u_group;
  324. }
  325. static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
  326. {
  327. struct platform_device *m4updev;
  328. if (args->args_count != 1) {
  329. dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
  330. args->args_count);
  331. return -EINVAL;
  332. }
  333. if (!dev->iommu_fwspec->iommu_priv) {
  334. /* Get the m4u device */
  335. m4updev = of_find_device_by_node(args->np);
  336. if (WARN_ON(!m4updev))
  337. return -EINVAL;
  338. dev->iommu_fwspec->iommu_priv = platform_get_drvdata(m4updev);
  339. }
  340. return iommu_fwspec_add_ids(dev, args->args, 1);
  341. }
  342. static struct iommu_ops mtk_iommu_ops = {
  343. .domain_alloc = mtk_iommu_domain_alloc,
  344. .domain_free = mtk_iommu_domain_free,
  345. .attach_dev = mtk_iommu_attach_device,
  346. .detach_dev = mtk_iommu_detach_device,
  347. .map = mtk_iommu_map,
  348. .unmap = mtk_iommu_unmap,
  349. .map_sg = default_iommu_map_sg,
  350. .iova_to_phys = mtk_iommu_iova_to_phys,
  351. .add_device = mtk_iommu_add_device,
  352. .remove_device = mtk_iommu_remove_device,
  353. .device_group = mtk_iommu_device_group,
  354. .of_xlate = mtk_iommu_of_xlate,
  355. .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
  356. };
  357. static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
  358. {
  359. u32 regval;
  360. int ret;
  361. ret = clk_prepare_enable(data->bclk);
  362. if (ret) {
  363. dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
  364. return ret;
  365. }
  366. regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
  367. F_MMU_TF_PROTECT_SEL(2);
  368. writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
  369. regval = F_L2_MULIT_HIT_EN |
  370. F_TABLE_WALK_FAULT_INT_EN |
  371. F_PREETCH_FIFO_OVERFLOW_INT_EN |
  372. F_MISS_FIFO_OVERFLOW_INT_EN |
  373. F_PREFETCH_FIFO_ERR_INT_EN |
  374. F_MISS_FIFO_ERR_INT_EN;
  375. writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
  376. regval = F_INT_TRANSLATION_FAULT |
  377. F_INT_MAIN_MULTI_HIT_FAULT |
  378. F_INT_INVALID_PA_FAULT |
  379. F_INT_ENTRY_REPLACEMENT_FAULT |
  380. F_INT_TLB_MISS_FAULT |
  381. F_INT_MISS_TRANSACTION_FIFO_FAULT |
  382. F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
  383. writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
  384. writel_relaxed(F_MMU_IVRP_PA_SET(data->protect_base, data->enable_4GB),
  385. data->base + REG_MMU_IVRP_PADDR);
  386. writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
  387. writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE);
  388. if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
  389. dev_name(data->dev), (void *)data)) {
  390. writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
  391. clk_disable_unprepare(data->bclk);
  392. dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
  393. return -ENODEV;
  394. }
  395. return 0;
  396. }
  397. static const struct component_master_ops mtk_iommu_com_ops = {
  398. .bind = mtk_iommu_bind,
  399. .unbind = mtk_iommu_unbind,
  400. };
  401. static int mtk_iommu_probe(struct platform_device *pdev)
  402. {
  403. struct mtk_iommu_data *data;
  404. struct device *dev = &pdev->dev;
  405. struct resource *res;
  406. struct component_match *match = NULL;
  407. void *protect;
  408. int i, larb_nr, ret;
  409. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  410. if (!data)
  411. return -ENOMEM;
  412. data->dev = dev;
  413. /* Protect memory. HW will access here while translation fault.*/
  414. protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
  415. if (!protect)
  416. return -ENOMEM;
  417. data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
  418. /* Whether the current dram is over 4GB */
  419. data->enable_4GB = !!(max_pfn > (0xffffffffUL >> PAGE_SHIFT));
  420. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  421. data->base = devm_ioremap_resource(dev, res);
  422. if (IS_ERR(data->base))
  423. return PTR_ERR(data->base);
  424. data->irq = platform_get_irq(pdev, 0);
  425. if (data->irq < 0)
  426. return data->irq;
  427. data->bclk = devm_clk_get(dev, "bclk");
  428. if (IS_ERR(data->bclk))
  429. return PTR_ERR(data->bclk);
  430. larb_nr = of_count_phandle_with_args(dev->of_node,
  431. "mediatek,larbs", NULL);
  432. if (larb_nr < 0)
  433. return larb_nr;
  434. data->smi_imu.larb_nr = larb_nr;
  435. for (i = 0; i < larb_nr; i++) {
  436. struct device_node *larbnode;
  437. struct platform_device *plarbdev;
  438. larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
  439. if (!larbnode)
  440. return -EINVAL;
  441. if (!of_device_is_available(larbnode))
  442. continue;
  443. plarbdev = of_find_device_by_node(larbnode);
  444. if (!plarbdev) {
  445. plarbdev = of_platform_device_create(
  446. larbnode, NULL,
  447. platform_bus_type.dev_root);
  448. if (!plarbdev) {
  449. of_node_put(larbnode);
  450. return -EPROBE_DEFER;
  451. }
  452. }
  453. data->smi_imu.larb_imu[i].dev = &plarbdev->dev;
  454. component_match_add_release(dev, &match, release_of,
  455. compare_of, larbnode);
  456. }
  457. platform_set_drvdata(pdev, data);
  458. ret = mtk_iommu_hw_init(data);
  459. if (ret)
  460. return ret;
  461. if (!iommu_present(&platform_bus_type))
  462. bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
  463. return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
  464. }
  465. static int mtk_iommu_remove(struct platform_device *pdev)
  466. {
  467. struct mtk_iommu_data *data = platform_get_drvdata(pdev);
  468. if (iommu_present(&platform_bus_type))
  469. bus_set_iommu(&platform_bus_type, NULL);
  470. free_io_pgtable_ops(data->m4u_dom->iop);
  471. clk_disable_unprepare(data->bclk);
  472. devm_free_irq(&pdev->dev, data->irq, data);
  473. component_master_del(&pdev->dev, &mtk_iommu_com_ops);
  474. return 0;
  475. }
  476. static int __maybe_unused mtk_iommu_suspend(struct device *dev)
  477. {
  478. struct mtk_iommu_data *data = dev_get_drvdata(dev);
  479. struct mtk_iommu_suspend_reg *reg = &data->reg;
  480. void __iomem *base = data->base;
  481. reg->standard_axi_mode = readl_relaxed(base +
  482. REG_MMU_STANDARD_AXI_MODE);
  483. reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
  484. reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
  485. reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
  486. reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
  487. return 0;
  488. }
  489. static int __maybe_unused mtk_iommu_resume(struct device *dev)
  490. {
  491. struct mtk_iommu_data *data = dev_get_drvdata(dev);
  492. struct mtk_iommu_suspend_reg *reg = &data->reg;
  493. void __iomem *base = data->base;
  494. writel_relaxed(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0],
  495. base + REG_MMU_PT_BASE_ADDR);
  496. writel_relaxed(reg->standard_axi_mode,
  497. base + REG_MMU_STANDARD_AXI_MODE);
  498. writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
  499. writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
  500. writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
  501. writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
  502. writel_relaxed(F_MMU_IVRP_PA_SET(data->protect_base, data->enable_4GB),
  503. base + REG_MMU_IVRP_PADDR);
  504. return 0;
  505. }
  506. const struct dev_pm_ops mtk_iommu_pm_ops = {
  507. SET_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
  508. };
  509. static const struct of_device_id mtk_iommu_of_ids[] = {
  510. { .compatible = "mediatek,mt8173-m4u", },
  511. {}
  512. };
  513. static struct platform_driver mtk_iommu_driver = {
  514. .probe = mtk_iommu_probe,
  515. .remove = mtk_iommu_remove,
  516. .driver = {
  517. .name = "mtk-iommu",
  518. .of_match_table = mtk_iommu_of_ids,
  519. .pm = &mtk_iommu_pm_ops,
  520. }
  521. };
  522. static int mtk_iommu_init_fn(struct device_node *np)
  523. {
  524. int ret;
  525. struct platform_device *pdev;
  526. pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root);
  527. if (!pdev)
  528. return -ENOMEM;
  529. ret = platform_driver_register(&mtk_iommu_driver);
  530. if (ret) {
  531. pr_err("%s: Failed to register driver\n", __func__);
  532. return ret;
  533. }
  534. of_iommu_set_ops(np, &mtk_iommu_ops);
  535. return 0;
  536. }
  537. IOMMU_OF_DECLARE(mtkm4u, "mediatek,mt8173-m4u", mtk_iommu_init_fn);