mtk_iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 <soc/mediatek/smi.h>
  35. #include "mtk_iommu.h"
  36. #define REG_MMU_PT_BASE_ADDR 0x000
  37. #define REG_MMU_INVALIDATE 0x020
  38. #define F_ALL_INVLD 0x2
  39. #define F_MMU_INV_RANGE 0x1
  40. #define REG_MMU_INVLD_START_A 0x024
  41. #define REG_MMU_INVLD_END_A 0x028
  42. #define REG_MMU_INV_SEL 0x038
  43. #define F_INVLD_EN0 BIT(0)
  44. #define F_INVLD_EN1 BIT(1)
  45. #define REG_MMU_STANDARD_AXI_MODE 0x048
  46. #define REG_MMU_DCM_DIS 0x050
  47. #define REG_MMU_CTRL_REG 0x110
  48. #define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4)
  49. #define F_MMU_TF_PROTECT_SEL_SHIFT(data) \
  50. ((data)->m4u_plat == M4U_MT2712 ? 4 : 5)
  51. /* It's named by F_MMU_TF_PROT_SEL in mt2712. */
  52. #define F_MMU_TF_PROTECT_SEL(prot, data) \
  53. (((prot) & 0x3) << F_MMU_TF_PROTECT_SEL_SHIFT(data))
  54. #define REG_MMU_IVRP_PADDR 0x114
  55. #define REG_MMU_VLD_PA_RNG 0x118
  56. #define F_MMU_VLD_PA_RNG(EA, SA) (((EA) << 8) | (SA))
  57. #define REG_MMU_INT_CONTROL0 0x120
  58. #define F_L2_MULIT_HIT_EN BIT(0)
  59. #define F_TABLE_WALK_FAULT_INT_EN BIT(1)
  60. #define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2)
  61. #define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3)
  62. #define F_PREFETCH_FIFO_ERR_INT_EN BIT(5)
  63. #define F_MISS_FIFO_ERR_INT_EN BIT(6)
  64. #define F_INT_CLR_BIT BIT(12)
  65. #define REG_MMU_INT_MAIN_CONTROL 0x124
  66. #define F_INT_TRANSLATION_FAULT BIT(0)
  67. #define F_INT_MAIN_MULTI_HIT_FAULT BIT(1)
  68. #define F_INT_INVALID_PA_FAULT BIT(2)
  69. #define F_INT_ENTRY_REPLACEMENT_FAULT BIT(3)
  70. #define F_INT_TLB_MISS_FAULT BIT(4)
  71. #define F_INT_MISS_TRANSACTION_FIFO_FAULT BIT(5)
  72. #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT BIT(6)
  73. #define REG_MMU_CPE_DONE 0x12C
  74. #define REG_MMU_FAULT_ST1 0x134
  75. #define REG_MMU_FAULT_VA 0x13c
  76. #define F_MMU_FAULT_VA_WRITE_BIT BIT(1)
  77. #define F_MMU_FAULT_VA_LAYER_BIT BIT(0)
  78. #define REG_MMU_INVLD_PA 0x140
  79. #define REG_MMU_INT_ID 0x150
  80. #define F_MMU0_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7)
  81. #define F_MMU0_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f)
  82. #define MTK_PROTECT_PA_ALIGN 128
  83. /*
  84. * Get the local arbiter ID and the portid within the larb arbiter
  85. * from mtk_m4u_id which is defined by MTK_M4U_ID.
  86. */
  87. #define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0xf)
  88. #define MTK_M4U_TO_PORT(id) ((id) & 0x1f)
  89. struct mtk_iommu_domain {
  90. spinlock_t pgtlock; /* lock for page table */
  91. struct io_pgtable_cfg cfg;
  92. struct io_pgtable_ops *iop;
  93. struct iommu_domain domain;
  94. };
  95. static struct iommu_ops mtk_iommu_ops;
  96. /*
  97. * In M4U 4GB mode, the physical address is remapped as below:
  98. *
  99. * CPU Physical address:
  100. * ====================
  101. *
  102. * 0 1G 2G 3G 4G 5G
  103. * |---A---|---B---|---C---|---D---|---E---|
  104. * +--I/O--+------------Memory-------------+
  105. *
  106. * IOMMU output physical address:
  107. * =============================
  108. *
  109. * 4G 5G 6G 7G 8G
  110. * |---E---|---B---|---C---|---D---|
  111. * +------------Memory-------------+
  112. *
  113. * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
  114. * bit32 of the CPU physical address always is needed to set, and for Region
  115. * 'E', the CPU physical address keep as is.
  116. * Additionally, The iommu consumers always use the CPU phyiscal address.
  117. */
  118. #define MTK_IOMMU_4GB_MODE_REMAP_BASE 0x40000000
  119. static LIST_HEAD(m4ulist); /* List all the M4U HWs */
  120. #define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list)
  121. /*
  122. * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
  123. * for the performance.
  124. *
  125. * Here always return the mtk_iommu_data of the first probed M4U where the
  126. * iommu domain information is recorded.
  127. */
  128. static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
  129. {
  130. struct mtk_iommu_data *data;
  131. for_each_m4u(data)
  132. return data;
  133. return NULL;
  134. }
  135. static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
  136. {
  137. return container_of(dom, struct mtk_iommu_domain, domain);
  138. }
  139. static void mtk_iommu_tlb_flush_all(void *cookie)
  140. {
  141. struct mtk_iommu_data *data = cookie;
  142. for_each_m4u(data) {
  143. writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
  144. data->base + REG_MMU_INV_SEL);
  145. writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
  146. wmb(); /* Make sure the tlb flush all done */
  147. }
  148. }
  149. static void mtk_iommu_tlb_add_flush_nosync(unsigned long iova, size_t size,
  150. size_t granule, bool leaf,
  151. void *cookie)
  152. {
  153. struct mtk_iommu_data *data = cookie;
  154. for_each_m4u(data) {
  155. writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
  156. data->base + REG_MMU_INV_SEL);
  157. writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
  158. writel_relaxed(iova + size - 1,
  159. data->base + REG_MMU_INVLD_END_A);
  160. writel_relaxed(F_MMU_INV_RANGE,
  161. data->base + REG_MMU_INVALIDATE);
  162. data->tlb_flush_active = true;
  163. }
  164. }
  165. static void mtk_iommu_tlb_sync(void *cookie)
  166. {
  167. struct mtk_iommu_data *data = cookie;
  168. int ret;
  169. u32 tmp;
  170. for_each_m4u(data) {
  171. /* Avoid timing out if there's nothing to wait for */
  172. if (!data->tlb_flush_active)
  173. return;
  174. ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
  175. tmp, tmp != 0, 10, 100000);
  176. if (ret) {
  177. dev_warn(data->dev,
  178. "Partial TLB flush timed out, falling back to full flush\n");
  179. mtk_iommu_tlb_flush_all(cookie);
  180. }
  181. /* Clear the CPE status */
  182. writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
  183. data->tlb_flush_active = false;
  184. }
  185. }
  186. static const struct iommu_gather_ops mtk_iommu_gather_ops = {
  187. .tlb_flush_all = mtk_iommu_tlb_flush_all,
  188. .tlb_add_flush = mtk_iommu_tlb_add_flush_nosync,
  189. .tlb_sync = mtk_iommu_tlb_sync,
  190. };
  191. static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
  192. {
  193. struct mtk_iommu_data *data = dev_id;
  194. struct mtk_iommu_domain *dom = data->m4u_dom;
  195. u32 int_state, regval, fault_iova, fault_pa;
  196. unsigned int fault_larb, fault_port;
  197. bool layer, write;
  198. /* Read error info from registers */
  199. int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
  200. fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
  201. layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
  202. write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
  203. fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
  204. regval = readl_relaxed(data->base + REG_MMU_INT_ID);
  205. fault_larb = F_MMU0_INT_ID_LARB_ID(regval);
  206. fault_port = F_MMU0_INT_ID_PORT_ID(regval);
  207. if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
  208. write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
  209. dev_err_ratelimited(
  210. data->dev,
  211. "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
  212. int_state, fault_iova, fault_pa, fault_larb, fault_port,
  213. layer, write ? "write" : "read");
  214. }
  215. /* Interrupt clear */
  216. regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
  217. regval |= F_INT_CLR_BIT;
  218. writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
  219. mtk_iommu_tlb_flush_all(data);
  220. return IRQ_HANDLED;
  221. }
  222. static void mtk_iommu_config(struct mtk_iommu_data *data,
  223. struct device *dev, bool enable)
  224. {
  225. struct mtk_smi_larb_iommu *larb_mmu;
  226. unsigned int larbid, portid;
  227. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  228. int i;
  229. for (i = 0; i < fwspec->num_ids; ++i) {
  230. larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
  231. portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
  232. larb_mmu = &data->smi_imu.larb_imu[larbid];
  233. dev_dbg(dev, "%s iommu port: %d\n",
  234. enable ? "enable" : "disable", portid);
  235. if (enable)
  236. larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
  237. else
  238. larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
  239. }
  240. }
  241. static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom)
  242. {
  243. struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
  244. spin_lock_init(&dom->pgtlock);
  245. dom->cfg = (struct io_pgtable_cfg) {
  246. .quirks = IO_PGTABLE_QUIRK_ARM_NS |
  247. IO_PGTABLE_QUIRK_NO_PERMS |
  248. IO_PGTABLE_QUIRK_TLBI_ON_MAP,
  249. .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
  250. .ias = 32,
  251. .oas = 32,
  252. .tlb = &mtk_iommu_gather_ops,
  253. .iommu_dev = data->dev,
  254. };
  255. if (data->enable_4GB)
  256. dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_4GB;
  257. dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
  258. if (!dom->iop) {
  259. dev_err(data->dev, "Failed to alloc io pgtable\n");
  260. return -EINVAL;
  261. }
  262. /* Update our support page sizes bitmap */
  263. dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
  264. return 0;
  265. }
  266. static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
  267. {
  268. struct mtk_iommu_domain *dom;
  269. if (type != IOMMU_DOMAIN_DMA)
  270. return NULL;
  271. dom = kzalloc(sizeof(*dom), GFP_KERNEL);
  272. if (!dom)
  273. return NULL;
  274. if (iommu_get_dma_cookie(&dom->domain))
  275. goto free_dom;
  276. if (mtk_iommu_domain_finalise(dom))
  277. goto put_dma_cookie;
  278. dom->domain.geometry.aperture_start = 0;
  279. dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
  280. dom->domain.geometry.force_aperture = true;
  281. return &dom->domain;
  282. put_dma_cookie:
  283. iommu_put_dma_cookie(&dom->domain);
  284. free_dom:
  285. kfree(dom);
  286. return NULL;
  287. }
  288. static void mtk_iommu_domain_free(struct iommu_domain *domain)
  289. {
  290. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  291. free_io_pgtable_ops(dom->iop);
  292. iommu_put_dma_cookie(domain);
  293. kfree(to_mtk_domain(domain));
  294. }
  295. static int mtk_iommu_attach_device(struct iommu_domain *domain,
  296. struct device *dev)
  297. {
  298. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  299. struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
  300. if (!data)
  301. return -ENODEV;
  302. /* Update the pgtable base address register of the M4U HW */
  303. if (!data->m4u_dom) {
  304. data->m4u_dom = dom;
  305. writel(dom->cfg.arm_v7s_cfg.ttbr[0],
  306. data->base + REG_MMU_PT_BASE_ADDR);
  307. }
  308. mtk_iommu_config(data, dev, true);
  309. return 0;
  310. }
  311. static void mtk_iommu_detach_device(struct iommu_domain *domain,
  312. struct device *dev)
  313. {
  314. struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
  315. if (!data)
  316. return;
  317. mtk_iommu_config(data, dev, false);
  318. }
  319. static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
  320. phys_addr_t paddr, size_t size, int prot)
  321. {
  322. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  323. unsigned long flags;
  324. int ret;
  325. spin_lock_irqsave(&dom->pgtlock, flags);
  326. ret = dom->iop->map(dom->iop, iova, paddr & DMA_BIT_MASK(32),
  327. size, prot);
  328. spin_unlock_irqrestore(&dom->pgtlock, flags);
  329. return ret;
  330. }
  331. static size_t mtk_iommu_unmap(struct iommu_domain *domain,
  332. unsigned long iova, size_t size)
  333. {
  334. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  335. unsigned long flags;
  336. size_t unmapsz;
  337. spin_lock_irqsave(&dom->pgtlock, flags);
  338. unmapsz = dom->iop->unmap(dom->iop, iova, size);
  339. spin_unlock_irqrestore(&dom->pgtlock, flags);
  340. return unmapsz;
  341. }
  342. static void mtk_iommu_iotlb_sync(struct iommu_domain *domain)
  343. {
  344. mtk_iommu_tlb_flush_all(mtk_iommu_get_m4u_data());
  345. }
  346. static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
  347. dma_addr_t iova)
  348. {
  349. struct mtk_iommu_domain *dom = to_mtk_domain(domain);
  350. struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
  351. unsigned long flags;
  352. phys_addr_t pa;
  353. spin_lock_irqsave(&dom->pgtlock, flags);
  354. pa = dom->iop->iova_to_phys(dom->iop, iova);
  355. spin_unlock_irqrestore(&dom->pgtlock, flags);
  356. if (data->enable_4GB && pa < MTK_IOMMU_4GB_MODE_REMAP_BASE)
  357. pa |= BIT_ULL(32);
  358. return pa;
  359. }
  360. static int mtk_iommu_add_device(struct device *dev)
  361. {
  362. struct mtk_iommu_data *data;
  363. struct iommu_group *group;
  364. if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)
  365. return -ENODEV; /* Not a iommu client device */
  366. data = dev->iommu_fwspec->iommu_priv;
  367. iommu_device_link(&data->iommu, dev);
  368. group = iommu_group_get_for_dev(dev);
  369. if (IS_ERR(group))
  370. return PTR_ERR(group);
  371. iommu_group_put(group);
  372. return 0;
  373. }
  374. static void mtk_iommu_remove_device(struct device *dev)
  375. {
  376. struct mtk_iommu_data *data;
  377. if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)
  378. return;
  379. data = dev->iommu_fwspec->iommu_priv;
  380. iommu_device_unlink(&data->iommu, dev);
  381. iommu_group_remove_device(dev);
  382. iommu_fwspec_free(dev);
  383. }
  384. static struct iommu_group *mtk_iommu_device_group(struct device *dev)
  385. {
  386. struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
  387. if (!data)
  388. return ERR_PTR(-ENODEV);
  389. /* All the client devices are in the same m4u iommu-group */
  390. if (!data->m4u_group) {
  391. data->m4u_group = iommu_group_alloc();
  392. if (IS_ERR(data->m4u_group))
  393. dev_err(dev, "Failed to allocate M4U IOMMU group\n");
  394. } else {
  395. iommu_group_ref_get(data->m4u_group);
  396. }
  397. return data->m4u_group;
  398. }
  399. static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
  400. {
  401. struct platform_device *m4updev;
  402. if (args->args_count != 1) {
  403. dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
  404. args->args_count);
  405. return -EINVAL;
  406. }
  407. if (!dev->iommu_fwspec->iommu_priv) {
  408. /* Get the m4u device */
  409. m4updev = of_find_device_by_node(args->np);
  410. if (WARN_ON(!m4updev))
  411. return -EINVAL;
  412. dev->iommu_fwspec->iommu_priv = platform_get_drvdata(m4updev);
  413. }
  414. return iommu_fwspec_add_ids(dev, args->args, 1);
  415. }
  416. static struct iommu_ops mtk_iommu_ops = {
  417. .domain_alloc = mtk_iommu_domain_alloc,
  418. .domain_free = mtk_iommu_domain_free,
  419. .attach_dev = mtk_iommu_attach_device,
  420. .detach_dev = mtk_iommu_detach_device,
  421. .map = mtk_iommu_map,
  422. .unmap = mtk_iommu_unmap,
  423. .flush_iotlb_all = mtk_iommu_iotlb_sync,
  424. .iotlb_sync = mtk_iommu_iotlb_sync,
  425. .iova_to_phys = mtk_iommu_iova_to_phys,
  426. .add_device = mtk_iommu_add_device,
  427. .remove_device = mtk_iommu_remove_device,
  428. .device_group = mtk_iommu_device_group,
  429. .of_xlate = mtk_iommu_of_xlate,
  430. .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
  431. };
  432. static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
  433. {
  434. u32 regval;
  435. int ret;
  436. ret = clk_prepare_enable(data->bclk);
  437. if (ret) {
  438. dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
  439. return ret;
  440. }
  441. regval = F_MMU_TF_PROTECT_SEL(2, data);
  442. if (data->m4u_plat == M4U_MT8173)
  443. regval |= F_MMU_PREFETCH_RT_REPLACE_MOD;
  444. writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
  445. regval = F_L2_MULIT_HIT_EN |
  446. F_TABLE_WALK_FAULT_INT_EN |
  447. F_PREETCH_FIFO_OVERFLOW_INT_EN |
  448. F_MISS_FIFO_OVERFLOW_INT_EN |
  449. F_PREFETCH_FIFO_ERR_INT_EN |
  450. F_MISS_FIFO_ERR_INT_EN;
  451. writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
  452. regval = F_INT_TRANSLATION_FAULT |
  453. F_INT_MAIN_MULTI_HIT_FAULT |
  454. F_INT_INVALID_PA_FAULT |
  455. F_INT_ENTRY_REPLACEMENT_FAULT |
  456. F_INT_TLB_MISS_FAULT |
  457. F_INT_MISS_TRANSACTION_FIFO_FAULT |
  458. F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
  459. writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
  460. if (data->m4u_plat == M4U_MT8173)
  461. regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
  462. else
  463. regval = lower_32_bits(data->protect_base) |
  464. upper_32_bits(data->protect_base);
  465. writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
  466. if (data->enable_4GB && data->m4u_plat != M4U_MT8173) {
  467. /*
  468. * If 4GB mode is enabled, the validate PA range is from
  469. * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
  470. */
  471. regval = F_MMU_VLD_PA_RNG(7, 4);
  472. writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
  473. }
  474. writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
  475. /* It's MISC control register whose default value is ok except mt8173.*/
  476. if (data->m4u_plat == M4U_MT8173)
  477. writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE);
  478. if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
  479. dev_name(data->dev), (void *)data)) {
  480. writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
  481. clk_disable_unprepare(data->bclk);
  482. dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
  483. return -ENODEV;
  484. }
  485. return 0;
  486. }
  487. static const struct component_master_ops mtk_iommu_com_ops = {
  488. .bind = mtk_iommu_bind,
  489. .unbind = mtk_iommu_unbind,
  490. };
  491. static int mtk_iommu_probe(struct platform_device *pdev)
  492. {
  493. struct mtk_iommu_data *data;
  494. struct device *dev = &pdev->dev;
  495. struct resource *res;
  496. resource_size_t ioaddr;
  497. struct component_match *match = NULL;
  498. void *protect;
  499. int i, larb_nr, ret;
  500. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  501. if (!data)
  502. return -ENOMEM;
  503. data->dev = dev;
  504. data->m4u_plat = (enum mtk_iommu_plat)of_device_get_match_data(dev);
  505. /* Protect memory. HW will access here while translation fault.*/
  506. protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
  507. if (!protect)
  508. return -ENOMEM;
  509. data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
  510. /* Whether the current dram is over 4GB */
  511. data->enable_4GB = !!(max_pfn > (BIT_ULL(32) >> PAGE_SHIFT));
  512. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  513. data->base = devm_ioremap_resource(dev, res);
  514. if (IS_ERR(data->base))
  515. return PTR_ERR(data->base);
  516. ioaddr = res->start;
  517. data->irq = platform_get_irq(pdev, 0);
  518. if (data->irq < 0)
  519. return data->irq;
  520. data->bclk = devm_clk_get(dev, "bclk");
  521. if (IS_ERR(data->bclk))
  522. return PTR_ERR(data->bclk);
  523. larb_nr = of_count_phandle_with_args(dev->of_node,
  524. "mediatek,larbs", NULL);
  525. if (larb_nr < 0)
  526. return larb_nr;
  527. data->smi_imu.larb_nr = larb_nr;
  528. for (i = 0; i < larb_nr; i++) {
  529. struct device_node *larbnode;
  530. struct platform_device *plarbdev;
  531. u32 id;
  532. larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
  533. if (!larbnode)
  534. return -EINVAL;
  535. if (!of_device_is_available(larbnode))
  536. continue;
  537. ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
  538. if (ret)/* The id is consecutive if there is no this property */
  539. id = i;
  540. plarbdev = of_find_device_by_node(larbnode);
  541. if (!plarbdev)
  542. return -EPROBE_DEFER;
  543. data->smi_imu.larb_imu[id].dev = &plarbdev->dev;
  544. component_match_add_release(dev, &match, release_of,
  545. compare_of, larbnode);
  546. }
  547. platform_set_drvdata(pdev, data);
  548. ret = mtk_iommu_hw_init(data);
  549. if (ret)
  550. return ret;
  551. ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
  552. "mtk-iommu.%pa", &ioaddr);
  553. if (ret)
  554. return ret;
  555. iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
  556. iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);
  557. ret = iommu_device_register(&data->iommu);
  558. if (ret)
  559. return ret;
  560. list_add_tail(&data->list, &m4ulist);
  561. if (!iommu_present(&platform_bus_type))
  562. bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
  563. return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
  564. }
  565. static int mtk_iommu_remove(struct platform_device *pdev)
  566. {
  567. struct mtk_iommu_data *data = platform_get_drvdata(pdev);
  568. iommu_device_sysfs_remove(&data->iommu);
  569. iommu_device_unregister(&data->iommu);
  570. if (iommu_present(&platform_bus_type))
  571. bus_set_iommu(&platform_bus_type, NULL);
  572. clk_disable_unprepare(data->bclk);
  573. devm_free_irq(&pdev->dev, data->irq, data);
  574. component_master_del(&pdev->dev, &mtk_iommu_com_ops);
  575. return 0;
  576. }
  577. static int __maybe_unused mtk_iommu_suspend(struct device *dev)
  578. {
  579. struct mtk_iommu_data *data = dev_get_drvdata(dev);
  580. struct mtk_iommu_suspend_reg *reg = &data->reg;
  581. void __iomem *base = data->base;
  582. reg->standard_axi_mode = readl_relaxed(base +
  583. REG_MMU_STANDARD_AXI_MODE);
  584. reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
  585. reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
  586. reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
  587. reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
  588. reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
  589. clk_disable_unprepare(data->bclk);
  590. return 0;
  591. }
  592. static int __maybe_unused mtk_iommu_resume(struct device *dev)
  593. {
  594. struct mtk_iommu_data *data = dev_get_drvdata(dev);
  595. struct mtk_iommu_suspend_reg *reg = &data->reg;
  596. void __iomem *base = data->base;
  597. int ret;
  598. ret = clk_prepare_enable(data->bclk);
  599. if (ret) {
  600. dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
  601. return ret;
  602. }
  603. writel_relaxed(reg->standard_axi_mode,
  604. base + REG_MMU_STANDARD_AXI_MODE);
  605. writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
  606. writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
  607. writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
  608. writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
  609. writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
  610. if (data->m4u_dom)
  611. writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0],
  612. base + REG_MMU_PT_BASE_ADDR);
  613. return 0;
  614. }
  615. static const struct dev_pm_ops mtk_iommu_pm_ops = {
  616. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
  617. };
  618. static const struct of_device_id mtk_iommu_of_ids[] = {
  619. { .compatible = "mediatek,mt2712-m4u", .data = (void *)M4U_MT2712},
  620. { .compatible = "mediatek,mt8173-m4u", .data = (void *)M4U_MT8173},
  621. {}
  622. };
  623. static struct platform_driver mtk_iommu_driver = {
  624. .probe = mtk_iommu_probe,
  625. .remove = mtk_iommu_remove,
  626. .driver = {
  627. .name = "mtk-iommu",
  628. .of_match_table = of_match_ptr(mtk_iommu_of_ids),
  629. .pm = &mtk_iommu_pm_ops,
  630. }
  631. };
  632. static int __init mtk_iommu_init(void)
  633. {
  634. int ret;
  635. ret = platform_driver_register(&mtk_iommu_driver);
  636. if (ret != 0)
  637. pr_err("Failed to register MTK IOMMU driver\n");
  638. return ret;
  639. }
  640. subsys_initcall(mtk_iommu_init)