mtk_iommu_v1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. * Copyright (c) 2015-2016 MediaTek Inc.
  3. * Author: Honghui Zhang <honghui.zhang@mediatek.com>
  4. *
  5. * Based on driver/iommu/mtk_iommu.c
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/memblock.h>
  17. #include <linux/bug.h>
  18. #include <linux/clk.h>
  19. #include <linux/component.h>
  20. #include <linux/device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dma-iommu.h>
  23. #include <linux/err.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/iommu.h>
  27. #include <linux/iopoll.h>
  28. #include <linux/list.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_iommu.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <asm/barrier.h>
  37. #include <asm/dma-iommu.h>
  38. #include <linux/module.h>
  39. #include <dt-bindings/memory/mt2701-larb-port.h>
  40. #include <soc/mediatek/smi.h>
  41. #include "mtk_iommu.h"
  42. #define REG_MMU_PT_BASE_ADDR 0x000
  43. #define F_ALL_INVLD 0x2
  44. #define F_MMU_INV_RANGE 0x1
  45. #define F_INVLD_EN0 BIT(0)
  46. #define F_INVLD_EN1 BIT(1)
  47. #define F_MMU_FAULT_VA_MSK 0xfffff000
  48. #define MTK_PROTECT_PA_ALIGN 128
  49. #define REG_MMU_CTRL_REG 0x210
  50. #define F_MMU_CTRL_COHERENT_EN BIT(8)
  51. #define REG_MMU_IVRP_PADDR 0x214
  52. #define REG_MMU_INT_CONTROL 0x220
  53. #define F_INT_TRANSLATION_FAULT BIT(0)
  54. #define F_INT_MAIN_MULTI_HIT_FAULT BIT(1)
  55. #define F_INT_INVALID_PA_FAULT BIT(2)
  56. #define F_INT_ENTRY_REPLACEMENT_FAULT BIT(3)
  57. #define F_INT_TABLE_WALK_FAULT BIT(4)
  58. #define F_INT_TLB_MISS_FAULT BIT(5)
  59. #define F_INT_PFH_DMA_FIFO_OVERFLOW BIT(6)
  60. #define F_INT_MISS_DMA_FIFO_OVERFLOW BIT(7)
  61. #define F_MMU_TF_PROTECT_SEL(prot) (((prot) & 0x3) << 5)
  62. #define F_INT_CLR_BIT BIT(12)
  63. #define REG_MMU_FAULT_ST 0x224
  64. #define REG_MMU_FAULT_VA 0x228
  65. #define REG_MMU_INVLD_PA 0x22C
  66. #define REG_MMU_INT_ID 0x388
  67. #define REG_MMU_INVALIDATE 0x5c0
  68. #define REG_MMU_INVLD_START_A 0x5c4
  69. #define REG_MMU_INVLD_END_A 0x5c8
  70. #define REG_MMU_INV_SEL 0x5d8
  71. #define REG_MMU_STANDARD_AXI_MODE 0x5e8
  72. #define REG_MMU_DCM 0x5f0
  73. #define F_MMU_DCM_ON BIT(1)
  74. #define REG_MMU_CPE_DONE 0x60c
  75. #define F_DESC_VALID 0x2
  76. #define F_DESC_NONSEC BIT(3)
  77. #define MT2701_M4U_TF_LARB(TF) (6 - (((TF) >> 13) & 0x7))
  78. #define MT2701_M4U_TF_PORT(TF) (((TF) >> 8) & 0xF)
  79. /* MTK generation one iommu HW only support 4K size mapping */
  80. #define MT2701_IOMMU_PAGE_SHIFT 12
  81. #define MT2701_IOMMU_PAGE_SIZE (1UL << MT2701_IOMMU_PAGE_SHIFT)
  82. /*
  83. * MTK m4u support 4GB iova address space, and only support 4K page
  84. * mapping. So the pagetable size should be exactly as 4M.
  85. */
  86. #define M2701_IOMMU_PGT_SIZE SZ_4M
  87. struct mtk_iommu_domain {
  88. spinlock_t pgtlock; /* lock for page table */
  89. struct iommu_domain domain;
  90. u32 *pgt_va;
  91. dma_addr_t pgt_pa;
  92. struct mtk_iommu_data *data;
  93. };
  94. static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
  95. {
  96. return container_of(dom, struct mtk_iommu_domain, domain);
  97. }
  98. static const int mt2701_m4u_in_larb[] = {
  99. LARB0_PORT_OFFSET, LARB1_PORT_OFFSET,
  100. LARB2_PORT_OFFSET, LARB3_PORT_OFFSET
  101. };
  102. static inline int mt2701_m4u_to_larb(int id)
  103. {
  104. int i;
  105. for (i = ARRAY_SIZE(mt2701_m4u_in_larb) - 1; i >= 0; i--)
  106. if ((id) >= mt2701_m4u_in_larb[i])
  107. return i;
  108. return 0;
  109. }
  110. static inline int mt2701_m4u_to_port(int id)
  111. {
  112. int larb = mt2701_m4u_to_larb(id);
  113. return id - mt2701_m4u_in_larb[larb];
  114. }
  115. static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
  116. {
  117. writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
  118. data->base + REG_MMU_INV_SEL);
  119. writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
  120. wmb(); /* Make sure the tlb flush all done */
  121. }
  122. static void mtk_iommu_tlb_flush_range(struct mtk_iommu_data *data,
  123. unsigned long iova, size_t size)
  124. {
  125. int ret;
  126. u32 tmp;
  127. writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
  128. data->base + REG_MMU_INV_SEL);
  129. writel_relaxed(iova & F_MMU_FAULT_VA_MSK,
  130. data->base + REG_MMU_INVLD_START_A);
  131. writel_relaxed((iova + size - 1) & F_MMU_FAULT_VA_MSK,
  132. data->base + REG_MMU_INVLD_END_A);
  133. writel_relaxed(F_MMU_INV_RANGE, data->base + REG_MMU_INVALIDATE);
  134. ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
  135. tmp, tmp != 0, 10, 100000);
  136. if (ret) {
  137. dev_warn(data->dev,
  138. "Partial TLB flush timed out, falling back to full flush\n");
  139. mtk_iommu_tlb_flush_all(data);
  140. }
  141. /* Clear the CPE status */
  142. writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
  143. }
  144. static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
  145. {
  146. struct mtk_iommu_data *data = dev_id;
  147. struct mtk_iommu_domain *dom = data->m4u_dom;
  148. u32 int_state, regval, fault_iova, fault_pa;
  149. unsigned int fault_larb, fault_port;
  150. /* Read error information from registers */
  151. int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST);
  152. fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
  153. fault_iova &= F_MMU_FAULT_VA_MSK;
  154. fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
  155. regval = readl_relaxed(data->base + REG_MMU_INT_ID);
  156. fault_larb = MT2701_M4U_TF_LARB(regval);
  157. fault_port = MT2701_M4U_TF_PORT(regval);
  158. /*
  159. * MTK v1 iommu HW could not determine whether the fault is read or
  160. * write fault, report as read fault.
  161. */
  162. if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
  163. IOMMU_FAULT_READ))
  164. dev_err_ratelimited(data->dev,
  165. "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d\n",
  166. int_state, fault_iova, fault_pa,
  167. fault_larb, fault_port);
  168. /* Interrupt clear */
  169. regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL);
  170. regval |= F_INT_CLR_BIT;
  171. writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL);
  172. mtk_iommu_tlb_flush_all(data);
  173. return IRQ_HANDLED;
  174. }
  175. static void mtk_iommu_config(struct mtk_iommu_data *data,
  176. struct device *dev, bool enable)
  177. {
  178. struct mtk_smi_larb_iommu *larb_mmu;
  179. unsigned int larbid, portid;
  180. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  181. int i;
  182. for (i = 0; i < fwspec->num_ids; ++i) {
  183. larbid = mt2701_m4u_to_larb(fwspec->ids[i]);
  184. portid = mt2701_m4u_to_port(fwspec->ids[i]);
  185. larb_mmu = &data->smi_imu.larb_imu[larbid];
  186. dev_dbg(dev, "%s iommu port: %d\n",
  187. enable ? "enable" : "disable", portid);
  188. if (enable)
  189. larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
  190. else
  191. larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
  192. }
  193. }
  194. static int mtk_iommu_domain_finalise(struct mtk_iommu_data *data)
  195. {
  196. struct mtk_iommu_domain *dom = data->m4u_dom;
  197. spin_lock_init(&dom->pgtlock);
  198. dom->pgt_va = dma_zalloc_coherent(data->dev,
  199. M2701_IOMMU_PGT_SIZE,
  200. &dom->pgt_pa, GFP_KERNEL);
  201. if (!dom->pgt_va)
  202. return -ENOMEM;
  203. writel(dom->pgt_pa, data->base + REG_MMU_PT_BASE_ADDR);
  204. dom->data = data;
  205. return 0;
  206. }
  207. static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
  208. {
  209. struct mtk_iommu_domain *dom;
  210. if (type != IOMMU_DOMAIN_UNMANAGED)
  211. return NULL;
  212. dom = kzalloc(sizeof(*dom), GFP_KERNEL);
  213. if (!dom)
  214. return NULL;
  215. return &dom->domain;
  216. }
  217. static void mtk_iommu_domain_free(struct iommu_domain *domain)
  218. {
  219. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  220. struct mtk_iommu_data *data = dom->data;
  221. dma_free_coherent(data->dev, M2701_IOMMU_PGT_SIZE,
  222. dom->pgt_va, dom->pgt_pa);
  223. kfree(to_mtk_domain(domain));
  224. }
  225. static int mtk_iommu_attach_device(struct iommu_domain *domain,
  226. struct device *dev)
  227. {
  228. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  229. struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
  230. int ret;
  231. if (!data)
  232. return -ENODEV;
  233. if (!data->m4u_dom) {
  234. data->m4u_dom = dom;
  235. ret = mtk_iommu_domain_finalise(data);
  236. if (ret) {
  237. data->m4u_dom = NULL;
  238. return ret;
  239. }
  240. }
  241. mtk_iommu_config(data, dev, true);
  242. return 0;
  243. }
  244. static void mtk_iommu_detach_device(struct iommu_domain *domain,
  245. struct device *dev)
  246. {
  247. struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
  248. if (!data)
  249. return;
  250. mtk_iommu_config(data, dev, false);
  251. }
  252. static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
  253. phys_addr_t paddr, size_t size, int prot)
  254. {
  255. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  256. unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
  257. unsigned long flags;
  258. unsigned int i;
  259. u32 *pgt_base_iova = dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT);
  260. u32 pabase = (u32)paddr;
  261. int map_size = 0;
  262. spin_lock_irqsave(&dom->pgtlock, flags);
  263. for (i = 0; i < page_num; i++) {
  264. if (pgt_base_iova[i]) {
  265. memset(pgt_base_iova, 0, i * sizeof(u32));
  266. break;
  267. }
  268. pgt_base_iova[i] = pabase | F_DESC_VALID | F_DESC_NONSEC;
  269. pabase += MT2701_IOMMU_PAGE_SIZE;
  270. map_size += MT2701_IOMMU_PAGE_SIZE;
  271. }
  272. spin_unlock_irqrestore(&dom->pgtlock, flags);
  273. mtk_iommu_tlb_flush_range(dom->data, iova, size);
  274. return map_size == size ? 0 : -EEXIST;
  275. }
  276. static size_t mtk_iommu_unmap(struct iommu_domain *domain,
  277. unsigned long iova, size_t size)
  278. {
  279. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  280. unsigned long flags;
  281. u32 *pgt_base_iova = dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT);
  282. unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
  283. spin_lock_irqsave(&dom->pgtlock, flags);
  284. memset(pgt_base_iova, 0, page_num * sizeof(u32));
  285. spin_unlock_irqrestore(&dom->pgtlock, flags);
  286. mtk_iommu_tlb_flush_range(dom->data, iova, size);
  287. return size;
  288. }
  289. static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
  290. dma_addr_t iova)
  291. {
  292. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  293. unsigned long flags;
  294. phys_addr_t pa;
  295. spin_lock_irqsave(&dom->pgtlock, flags);
  296. pa = *(dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT));
  297. pa = pa & (~(MT2701_IOMMU_PAGE_SIZE - 1));
  298. spin_unlock_irqrestore(&dom->pgtlock, flags);
  299. return pa;
  300. }
  301. static struct iommu_ops mtk_iommu_ops;
  302. /*
  303. * MTK generation one iommu HW only support one iommu domain, and all the client
  304. * sharing the same iova address space.
  305. */
  306. static int mtk_iommu_create_mapping(struct device *dev,
  307. struct of_phandle_args *args)
  308. {
  309. struct mtk_iommu_data *data;
  310. struct platform_device *m4updev;
  311. struct dma_iommu_mapping *mtk_mapping;
  312. struct device *m4udev;
  313. int ret;
  314. if (args->args_count != 1) {
  315. dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
  316. args->args_count);
  317. return -EINVAL;
  318. }
  319. if (!dev->iommu_fwspec) {
  320. ret = iommu_fwspec_init(dev, &args->np->fwnode, &mtk_iommu_ops);
  321. if (ret)
  322. return ret;
  323. } else if (dev->iommu_fwspec->ops != &mtk_iommu_ops) {
  324. return -EINVAL;
  325. }
  326. if (!dev->iommu_fwspec->iommu_priv) {
  327. /* Get the m4u device */
  328. m4updev = of_find_device_by_node(args->np);
  329. if (WARN_ON(!m4updev))
  330. return -EINVAL;
  331. dev->iommu_fwspec->iommu_priv = platform_get_drvdata(m4updev);
  332. }
  333. ret = iommu_fwspec_add_ids(dev, args->args, 1);
  334. if (ret)
  335. return ret;
  336. data = dev->iommu_fwspec->iommu_priv;
  337. m4udev = data->dev;
  338. mtk_mapping = m4udev->archdata.iommu;
  339. if (!mtk_mapping) {
  340. /* MTK iommu support 4GB iova address space. */
  341. mtk_mapping = arm_iommu_create_mapping(&platform_bus_type,
  342. 0, 1ULL << 32);
  343. if (IS_ERR(mtk_mapping))
  344. return PTR_ERR(mtk_mapping);
  345. m4udev->archdata.iommu = mtk_mapping;
  346. }
  347. return 0;
  348. }
  349. static int mtk_iommu_add_device(struct device *dev)
  350. {
  351. struct dma_iommu_mapping *mtk_mapping;
  352. struct of_phandle_args iommu_spec;
  353. struct of_phandle_iterator it;
  354. struct mtk_iommu_data *data;
  355. struct iommu_group *group;
  356. int err;
  357. of_for_each_phandle(&it, err, dev->of_node, "iommus",
  358. "#iommu-cells", 0) {
  359. int count = of_phandle_iterator_args(&it, iommu_spec.args,
  360. MAX_PHANDLE_ARGS);
  361. iommu_spec.np = of_node_get(it.node);
  362. iommu_spec.args_count = count;
  363. mtk_iommu_create_mapping(dev, &iommu_spec);
  364. of_node_put(iommu_spec.np);
  365. }
  366. if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)
  367. return -ENODEV; /* Not a iommu client device */
  368. /*
  369. * This is a short-term bodge because the ARM DMA code doesn't
  370. * understand multi-device groups, but we have to call into it
  371. * successfully (and not just rely on a normal IOMMU API attach
  372. * here) in order to set the correct DMA API ops on @dev.
  373. */
  374. group = iommu_group_alloc();
  375. if (IS_ERR(group))
  376. return PTR_ERR(group);
  377. err = iommu_group_add_device(group, dev);
  378. iommu_group_put(group);
  379. if (err)
  380. return err;
  381. data = dev->iommu_fwspec->iommu_priv;
  382. mtk_mapping = data->dev->archdata.iommu;
  383. err = arm_iommu_attach_device(dev, mtk_mapping);
  384. if (err) {
  385. iommu_group_remove_device(dev);
  386. return err;
  387. }
  388. return iommu_device_link(&data->iommu, dev);;
  389. }
  390. static void mtk_iommu_remove_device(struct device *dev)
  391. {
  392. struct mtk_iommu_data *data;
  393. if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)
  394. return;
  395. data = dev->iommu_fwspec->iommu_priv;
  396. iommu_device_unlink(&data->iommu, dev);
  397. iommu_group_remove_device(dev);
  398. iommu_fwspec_free(dev);
  399. }
  400. static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
  401. {
  402. u32 regval;
  403. int ret;
  404. ret = clk_prepare_enable(data->bclk);
  405. if (ret) {
  406. dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
  407. return ret;
  408. }
  409. regval = F_MMU_CTRL_COHERENT_EN | F_MMU_TF_PROTECT_SEL(2);
  410. writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
  411. regval = F_INT_TRANSLATION_FAULT |
  412. F_INT_MAIN_MULTI_HIT_FAULT |
  413. F_INT_INVALID_PA_FAULT |
  414. F_INT_ENTRY_REPLACEMENT_FAULT |
  415. F_INT_TABLE_WALK_FAULT |
  416. F_INT_TLB_MISS_FAULT |
  417. F_INT_PFH_DMA_FIFO_OVERFLOW |
  418. F_INT_MISS_DMA_FIFO_OVERFLOW;
  419. writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL);
  420. /* protect memory,hw will write here while translation fault */
  421. writel_relaxed(data->protect_base,
  422. data->base + REG_MMU_IVRP_PADDR);
  423. writel_relaxed(F_MMU_DCM_ON, data->base + REG_MMU_DCM);
  424. if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
  425. dev_name(data->dev), (void *)data)) {
  426. writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
  427. clk_disable_unprepare(data->bclk);
  428. dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
  429. return -ENODEV;
  430. }
  431. return 0;
  432. }
  433. static struct iommu_ops mtk_iommu_ops = {
  434. .domain_alloc = mtk_iommu_domain_alloc,
  435. .domain_free = mtk_iommu_domain_free,
  436. .attach_dev = mtk_iommu_attach_device,
  437. .detach_dev = mtk_iommu_detach_device,
  438. .map = mtk_iommu_map,
  439. .unmap = mtk_iommu_unmap,
  440. .iova_to_phys = mtk_iommu_iova_to_phys,
  441. .add_device = mtk_iommu_add_device,
  442. .remove_device = mtk_iommu_remove_device,
  443. .pgsize_bitmap = ~0UL << MT2701_IOMMU_PAGE_SHIFT,
  444. };
  445. static const struct of_device_id mtk_iommu_of_ids[] = {
  446. { .compatible = "mediatek,mt2701-m4u", },
  447. {}
  448. };
  449. static const struct component_master_ops mtk_iommu_com_ops = {
  450. .bind = mtk_iommu_bind,
  451. .unbind = mtk_iommu_unbind,
  452. };
  453. static int mtk_iommu_probe(struct platform_device *pdev)
  454. {
  455. struct mtk_iommu_data *data;
  456. struct device *dev = &pdev->dev;
  457. struct resource *res;
  458. struct component_match *match = NULL;
  459. struct of_phandle_args larb_spec;
  460. struct of_phandle_iterator it;
  461. void *protect;
  462. int larb_nr, ret, err;
  463. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  464. if (!data)
  465. return -ENOMEM;
  466. data->dev = dev;
  467. /* Protect memory. HW will access here while translation fault.*/
  468. protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
  469. GFP_KERNEL | GFP_DMA);
  470. if (!protect)
  471. return -ENOMEM;
  472. data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
  473. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  474. data->base = devm_ioremap_resource(dev, res);
  475. if (IS_ERR(data->base))
  476. return PTR_ERR(data->base);
  477. data->irq = platform_get_irq(pdev, 0);
  478. if (data->irq < 0)
  479. return data->irq;
  480. data->bclk = devm_clk_get(dev, "bclk");
  481. if (IS_ERR(data->bclk))
  482. return PTR_ERR(data->bclk);
  483. larb_nr = 0;
  484. of_for_each_phandle(&it, err, dev->of_node,
  485. "mediatek,larbs", NULL, 0) {
  486. struct platform_device *plarbdev;
  487. int count = of_phandle_iterator_args(&it, larb_spec.args,
  488. MAX_PHANDLE_ARGS);
  489. if (count)
  490. continue;
  491. larb_spec.np = of_node_get(it.node);
  492. if (!of_device_is_available(larb_spec.np))
  493. continue;
  494. plarbdev = of_find_device_by_node(larb_spec.np);
  495. if (!plarbdev) {
  496. plarbdev = of_platform_device_create(
  497. larb_spec.np, NULL,
  498. platform_bus_type.dev_root);
  499. if (!plarbdev) {
  500. of_node_put(larb_spec.np);
  501. return -EPROBE_DEFER;
  502. }
  503. }
  504. data->smi_imu.larb_imu[larb_nr].dev = &plarbdev->dev;
  505. component_match_add_release(dev, &match, release_of,
  506. compare_of, larb_spec.np);
  507. larb_nr++;
  508. }
  509. data->smi_imu.larb_nr = larb_nr;
  510. platform_set_drvdata(pdev, data);
  511. ret = mtk_iommu_hw_init(data);
  512. if (ret)
  513. return ret;
  514. ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
  515. dev_name(&pdev->dev));
  516. if (ret)
  517. return ret;
  518. iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
  519. ret = iommu_device_register(&data->iommu);
  520. if (ret)
  521. return ret;
  522. if (!iommu_present(&platform_bus_type))
  523. bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
  524. return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
  525. }
  526. static int mtk_iommu_remove(struct platform_device *pdev)
  527. {
  528. struct mtk_iommu_data *data = platform_get_drvdata(pdev);
  529. iommu_device_sysfs_remove(&data->iommu);
  530. iommu_device_unregister(&data->iommu);
  531. if (iommu_present(&platform_bus_type))
  532. bus_set_iommu(&platform_bus_type, NULL);
  533. clk_disable_unprepare(data->bclk);
  534. devm_free_irq(&pdev->dev, data->irq, data);
  535. component_master_del(&pdev->dev, &mtk_iommu_com_ops);
  536. return 0;
  537. }
  538. static int __maybe_unused mtk_iommu_suspend(struct device *dev)
  539. {
  540. struct mtk_iommu_data *data = dev_get_drvdata(dev);
  541. struct mtk_iommu_suspend_reg *reg = &data->reg;
  542. void __iomem *base = data->base;
  543. reg->standard_axi_mode = readl_relaxed(base +
  544. REG_MMU_STANDARD_AXI_MODE);
  545. reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM);
  546. reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
  547. reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL);
  548. return 0;
  549. }
  550. static int __maybe_unused mtk_iommu_resume(struct device *dev)
  551. {
  552. struct mtk_iommu_data *data = dev_get_drvdata(dev);
  553. struct mtk_iommu_suspend_reg *reg = &data->reg;
  554. void __iomem *base = data->base;
  555. writel_relaxed(data->m4u_dom->pgt_pa, base + REG_MMU_PT_BASE_ADDR);
  556. writel_relaxed(reg->standard_axi_mode,
  557. base + REG_MMU_STANDARD_AXI_MODE);
  558. writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM);
  559. writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
  560. writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL);
  561. writel_relaxed(data->protect_base, base + REG_MMU_IVRP_PADDR);
  562. return 0;
  563. }
  564. static const struct dev_pm_ops mtk_iommu_pm_ops = {
  565. SET_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
  566. };
  567. static struct platform_driver mtk_iommu_driver = {
  568. .probe = mtk_iommu_probe,
  569. .remove = mtk_iommu_remove,
  570. .driver = {
  571. .name = "mtk-iommu-v1",
  572. .of_match_table = mtk_iommu_of_ids,
  573. .pm = &mtk_iommu_pm_ops,
  574. }
  575. };
  576. static int __init m4u_init(void)
  577. {
  578. return platform_driver_register(&mtk_iommu_driver);
  579. }
  580. static void __exit m4u_exit(void)
  581. {
  582. return platform_driver_unregister(&mtk_iommu_driver);
  583. }
  584. subsys_initcall(m4u_init);
  585. module_exit(m4u_exit);
  586. MODULE_DESCRIPTION("IOMMU API for MTK architected m4u v1 implementations");
  587. MODULE_AUTHOR("Honghui Zhang <honghui.zhang@mediatek.com>");
  588. MODULE_LICENSE("GPL v2");