mtk_iommu_v1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * Copyright (c) 2015-2016 MediaTek Inc.
  3. * Author: Honghui Zhang <[email protected]>
  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/bootmem.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-iommu.h>
  22. #include <linux/err.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/iommu.h>
  26. #include <linux/iopoll.h>
  27. #include <linux/kmemleak.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_iommu_client_priv *head, *cur, *next;
  179. struct mtk_smi_larb_iommu *larb_mmu;
  180. unsigned int larbid, portid;
  181. head = dev->archdata.iommu;
  182. list_for_each_entry_safe(cur, next, &head->client, client) {
  183. larbid = mt2701_m4u_to_larb(cur->mtk_m4u_id);
  184. portid = mt2701_m4u_to_port(cur->mtk_m4u_id);
  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_client_priv *priv = dev->archdata.iommu;
  230. struct mtk_iommu_data *data;
  231. int ret;
  232. if (!priv)
  233. return -ENODEV;
  234. data = dev_get_drvdata(priv->m4udev);
  235. if (!data->m4u_dom) {
  236. data->m4u_dom = dom;
  237. ret = mtk_iommu_domain_finalise(data);
  238. if (ret) {
  239. data->m4u_dom = NULL;
  240. return ret;
  241. }
  242. }
  243. mtk_iommu_config(data, dev, true);
  244. return 0;
  245. }
  246. static void mtk_iommu_detach_device(struct iommu_domain *domain,
  247. struct device *dev)
  248. {
  249. struct mtk_iommu_client_priv *priv = dev->archdata.iommu;
  250. struct mtk_iommu_data *data;
  251. if (!priv)
  252. return;
  253. data = dev_get_drvdata(priv->m4udev);
  254. mtk_iommu_config(data, dev, false);
  255. }
  256. static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
  257. phys_addr_t paddr, size_t size, int prot)
  258. {
  259. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  260. unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
  261. unsigned long flags;
  262. unsigned int i;
  263. u32 *pgt_base_iova = dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT);
  264. u32 pabase = (u32)paddr;
  265. int map_size = 0;
  266. spin_lock_irqsave(&dom->pgtlock, flags);
  267. for (i = 0; i < page_num; i++) {
  268. if (pgt_base_iova[i]) {
  269. memset(pgt_base_iova, 0, i * sizeof(u32));
  270. break;
  271. }
  272. pgt_base_iova[i] = pabase | F_DESC_VALID | F_DESC_NONSEC;
  273. pabase += MT2701_IOMMU_PAGE_SIZE;
  274. map_size += MT2701_IOMMU_PAGE_SIZE;
  275. }
  276. spin_unlock_irqrestore(&dom->pgtlock, flags);
  277. mtk_iommu_tlb_flush_range(dom->data, iova, size);
  278. return map_size == size ? 0 : -EEXIST;
  279. }
  280. static size_t mtk_iommu_unmap(struct iommu_domain *domain,
  281. unsigned long iova, size_t size)
  282. {
  283. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  284. unsigned long flags;
  285. u32 *pgt_base_iova = dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT);
  286. unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
  287. spin_lock_irqsave(&dom->pgtlock, flags);
  288. memset(pgt_base_iova, 0, page_num * sizeof(u32));
  289. spin_unlock_irqrestore(&dom->pgtlock, flags);
  290. mtk_iommu_tlb_flush_range(dom->data, iova, size);
  291. return size;
  292. }
  293. static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
  294. dma_addr_t iova)
  295. {
  296. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  297. unsigned long flags;
  298. phys_addr_t pa;
  299. spin_lock_irqsave(&dom->pgtlock, flags);
  300. pa = *(dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT));
  301. pa = pa & (~(MT2701_IOMMU_PAGE_SIZE - 1));
  302. spin_unlock_irqrestore(&dom->pgtlock, flags);
  303. return pa;
  304. }
  305. /*
  306. * MTK generation one iommu HW only support one iommu domain, and all the client
  307. * sharing the same iova address space.
  308. */
  309. static int mtk_iommu_create_mapping(struct device *dev,
  310. struct of_phandle_args *args)
  311. {
  312. struct mtk_iommu_client_priv *head, *priv, *next;
  313. struct platform_device *m4updev;
  314. struct dma_iommu_mapping *mtk_mapping;
  315. struct device *m4udev;
  316. int ret;
  317. if (args->args_count != 1) {
  318. dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
  319. args->args_count);
  320. return -EINVAL;
  321. }
  322. if (!dev->archdata.iommu) {
  323. /* Get the m4u device */
  324. m4updev = of_find_device_by_node(args->np);
  325. if (WARN_ON(!m4updev))
  326. return -EINVAL;
  327. head = kzalloc(sizeof(*head), GFP_KERNEL);
  328. if (!head)
  329. return -ENOMEM;
  330. dev->archdata.iommu = head;
  331. INIT_LIST_HEAD(&head->client);
  332. head->m4udev = &m4updev->dev;
  333. } else {
  334. head = dev->archdata.iommu;
  335. }
  336. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  337. if (!priv) {
  338. ret = -ENOMEM;
  339. goto err_free_mem;
  340. }
  341. priv->mtk_m4u_id = args->args[0];
  342. list_add_tail(&priv->client, &head->client);
  343. m4udev = head->m4udev;
  344. mtk_mapping = m4udev->archdata.iommu;
  345. if (!mtk_mapping) {
  346. /* MTK iommu support 4GB iova address space. */
  347. mtk_mapping = arm_iommu_create_mapping(&platform_bus_type,
  348. 0, 1ULL << 32);
  349. if (IS_ERR(mtk_mapping)) {
  350. ret = PTR_ERR(mtk_mapping);
  351. goto err_free_mem;
  352. }
  353. m4udev->archdata.iommu = mtk_mapping;
  354. }
  355. ret = arm_iommu_attach_device(dev, mtk_mapping);
  356. if (ret)
  357. goto err_release_mapping;
  358. return 0;
  359. err_release_mapping:
  360. arm_iommu_release_mapping(mtk_mapping);
  361. m4udev->archdata.iommu = NULL;
  362. err_free_mem:
  363. list_for_each_entry_safe(priv, next, &head->client, client)
  364. kfree(priv);
  365. kfree(head);
  366. dev->archdata.iommu = NULL;
  367. return ret;
  368. }
  369. static int mtk_iommu_add_device(struct device *dev)
  370. {
  371. struct iommu_group *group;
  372. struct of_phandle_args iommu_spec;
  373. struct of_phandle_iterator it;
  374. int err;
  375. of_for_each_phandle(&it, err, dev->of_node, "iommus",
  376. "#iommu-cells", 0) {
  377. int count = of_phandle_iterator_args(&it, iommu_spec.args,
  378. MAX_PHANDLE_ARGS);
  379. iommu_spec.np = of_node_get(it.node);
  380. iommu_spec.args_count = count;
  381. mtk_iommu_create_mapping(dev, &iommu_spec);
  382. of_node_put(iommu_spec.np);
  383. }
  384. if (!dev->archdata.iommu) /* Not a iommu client device */
  385. return -ENODEV;
  386. group = iommu_group_get_for_dev(dev);
  387. if (IS_ERR(group))
  388. return PTR_ERR(group);
  389. iommu_group_put(group);
  390. return 0;
  391. }
  392. static void mtk_iommu_remove_device(struct device *dev)
  393. {
  394. struct mtk_iommu_client_priv *head, *cur, *next;
  395. head = dev->archdata.iommu;
  396. if (!head)
  397. return;
  398. list_for_each_entry_safe(cur, next, &head->client, client) {
  399. list_del(&cur->client);
  400. kfree(cur);
  401. }
  402. kfree(head);
  403. dev->archdata.iommu = NULL;
  404. iommu_group_remove_device(dev);
  405. }
  406. static struct iommu_group *mtk_iommu_device_group(struct device *dev)
  407. {
  408. struct mtk_iommu_data *data;
  409. struct mtk_iommu_client_priv *priv;
  410. priv = dev->archdata.iommu;
  411. if (!priv)
  412. return ERR_PTR(-ENODEV);
  413. /* All the client devices are in the same m4u iommu-group */
  414. data = dev_get_drvdata(priv->m4udev);
  415. if (!data->m4u_group) {
  416. data->m4u_group = iommu_group_alloc();
  417. if (IS_ERR(data->m4u_group))
  418. dev_err(dev, "Failed to allocate M4U IOMMU group\n");
  419. }
  420. return data->m4u_group;
  421. }
  422. static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
  423. {
  424. u32 regval;
  425. int ret;
  426. ret = clk_prepare_enable(data->bclk);
  427. if (ret) {
  428. dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
  429. return ret;
  430. }
  431. regval = F_MMU_CTRL_COHERENT_EN | F_MMU_TF_PROTECT_SEL(2);
  432. writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
  433. regval = F_INT_TRANSLATION_FAULT |
  434. F_INT_MAIN_MULTI_HIT_FAULT |
  435. F_INT_INVALID_PA_FAULT |
  436. F_INT_ENTRY_REPLACEMENT_FAULT |
  437. F_INT_TABLE_WALK_FAULT |
  438. F_INT_TLB_MISS_FAULT |
  439. F_INT_PFH_DMA_FIFO_OVERFLOW |
  440. F_INT_MISS_DMA_FIFO_OVERFLOW;
  441. writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL);
  442. /* protect memory,hw will write here while translation fault */
  443. writel_relaxed(data->protect_base,
  444. data->base + REG_MMU_IVRP_PADDR);
  445. writel_relaxed(F_MMU_DCM_ON, data->base + REG_MMU_DCM);
  446. if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
  447. dev_name(data->dev), (void *)data)) {
  448. writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
  449. clk_disable_unprepare(data->bclk);
  450. dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
  451. return -ENODEV;
  452. }
  453. return 0;
  454. }
  455. static struct iommu_ops mtk_iommu_ops = {
  456. .domain_alloc = mtk_iommu_domain_alloc,
  457. .domain_free = mtk_iommu_domain_free,
  458. .attach_dev = mtk_iommu_attach_device,
  459. .detach_dev = mtk_iommu_detach_device,
  460. .map = mtk_iommu_map,
  461. .unmap = mtk_iommu_unmap,
  462. .map_sg = default_iommu_map_sg,
  463. .iova_to_phys = mtk_iommu_iova_to_phys,
  464. .add_device = mtk_iommu_add_device,
  465. .remove_device = mtk_iommu_remove_device,
  466. .device_group = mtk_iommu_device_group,
  467. .pgsize_bitmap = ~0UL << MT2701_IOMMU_PAGE_SHIFT,
  468. };
  469. static const struct of_device_id mtk_iommu_of_ids[] = {
  470. { .compatible = "mediatek,mt2701-m4u", },
  471. {}
  472. };
  473. static const struct component_master_ops mtk_iommu_com_ops = {
  474. .bind = mtk_iommu_bind,
  475. .unbind = mtk_iommu_unbind,
  476. };
  477. static int mtk_iommu_probe(struct platform_device *pdev)
  478. {
  479. struct mtk_iommu_data *data;
  480. struct device *dev = &pdev->dev;
  481. struct resource *res;
  482. struct component_match *match = NULL;
  483. struct of_phandle_args larb_spec;
  484. struct of_phandle_iterator it;
  485. void *protect;
  486. int larb_nr, ret, err;
  487. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  488. if (!data)
  489. return -ENOMEM;
  490. data->dev = dev;
  491. /* Protect memory. HW will access here while translation fault.*/
  492. protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
  493. GFP_KERNEL | GFP_DMA);
  494. if (!protect)
  495. return -ENOMEM;
  496. data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
  497. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  498. data->base = devm_ioremap_resource(dev, res);
  499. if (IS_ERR(data->base))
  500. return PTR_ERR(data->base);
  501. data->irq = platform_get_irq(pdev, 0);
  502. if (data->irq < 0)
  503. return data->irq;
  504. data->bclk = devm_clk_get(dev, "bclk");
  505. if (IS_ERR(data->bclk))
  506. return PTR_ERR(data->bclk);
  507. larb_nr = 0;
  508. of_for_each_phandle(&it, err, dev->of_node,
  509. "mediatek,larbs", NULL, 0) {
  510. struct platform_device *plarbdev;
  511. int count = of_phandle_iterator_args(&it, larb_spec.args,
  512. MAX_PHANDLE_ARGS);
  513. if (count)
  514. continue;
  515. larb_spec.np = of_node_get(it.node);
  516. if (!of_device_is_available(larb_spec.np))
  517. continue;
  518. plarbdev = of_find_device_by_node(larb_spec.np);
  519. of_node_put(larb_spec.np);
  520. if (!plarbdev) {
  521. plarbdev = of_platform_device_create(
  522. larb_spec.np, NULL,
  523. platform_bus_type.dev_root);
  524. if (!plarbdev)
  525. return -EPROBE_DEFER;
  526. }
  527. data->smi_imu.larb_imu[larb_nr].dev = &plarbdev->dev;
  528. component_match_add(dev, &match, compare_of, larb_spec.np);
  529. larb_nr++;
  530. }
  531. data->smi_imu.larb_nr = larb_nr;
  532. platform_set_drvdata(pdev, data);
  533. ret = mtk_iommu_hw_init(data);
  534. if (ret)
  535. return ret;
  536. if (!iommu_present(&platform_bus_type))
  537. bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
  538. return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
  539. }
  540. static int mtk_iommu_remove(struct platform_device *pdev)
  541. {
  542. struct mtk_iommu_data *data = platform_get_drvdata(pdev);
  543. if (iommu_present(&platform_bus_type))
  544. bus_set_iommu(&platform_bus_type, NULL);
  545. clk_disable_unprepare(data->bclk);
  546. devm_free_irq(&pdev->dev, data->irq, data);
  547. component_master_del(&pdev->dev, &mtk_iommu_com_ops);
  548. return 0;
  549. }
  550. static int __maybe_unused mtk_iommu_suspend(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. reg->standard_axi_mode = readl_relaxed(base +
  556. REG_MMU_STANDARD_AXI_MODE);
  557. reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM);
  558. reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
  559. reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL);
  560. return 0;
  561. }
  562. static int __maybe_unused mtk_iommu_resume(struct device *dev)
  563. {
  564. struct mtk_iommu_data *data = dev_get_drvdata(dev);
  565. struct mtk_iommu_suspend_reg *reg = &data->reg;
  566. void __iomem *base = data->base;
  567. writel_relaxed(data->m4u_dom->pgt_pa, base + REG_MMU_PT_BASE_ADDR);
  568. writel_relaxed(reg->standard_axi_mode,
  569. base + REG_MMU_STANDARD_AXI_MODE);
  570. writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM);
  571. writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
  572. writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL);
  573. writel_relaxed(data->protect_base, base + REG_MMU_IVRP_PADDR);
  574. return 0;
  575. }
  576. static const struct dev_pm_ops mtk_iommu_pm_ops = {
  577. SET_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
  578. };
  579. static struct platform_driver mtk_iommu_driver = {
  580. .probe = mtk_iommu_probe,
  581. .remove = mtk_iommu_remove,
  582. .driver = {
  583. .name = "mtk-iommu-v1",
  584. .of_match_table = mtk_iommu_of_ids,
  585. .pm = &mtk_iommu_pm_ops,
  586. }
  587. };
  588. static int __init m4u_init(void)
  589. {
  590. return platform_driver_register(&mtk_iommu_driver);
  591. }
  592. static void __exit m4u_exit(void)
  593. {
  594. return platform_driver_unregister(&mtk_iommu_driver);
  595. }
  596. subsys_initcall(m4u_init);
  597. module_exit(m4u_exit);
  598. MODULE_DESCRIPTION("IOMMU API for MTK architected m4u v1 implementations");
  599. MODULE_AUTHOR("Honghui Zhang <[email protected]>");
  600. MODULE_LICENSE("GPL v2");