Lines Matching full:iommu
3 * IOMMU API for Rockchip
17 #include <linux/iommu.h>
92 /* list of clocks required by IOMMU */
113 struct iommu_device iommu; member
115 struct iommu_domain *domain; /* domain to which iommu is attached */
120 struct device_link *link; /* runtime PM link from IOMMU to master */
121 struct rk_iommu *iommu; member
142 * The Rockchip rk3288 iommu uses a 2-level page table.
151 * Each iommu device has a MMU_DTE_ADDR register that contains the physical
304 * rk3288 iova (IOMMU Virtual Address) format
345 static void rk_iommu_command(struct rk_iommu *iommu, u32 command) in rk_iommu_command() argument
349 for (i = 0; i < iommu->num_mmu; i++) in rk_iommu_command()
350 writel(command, iommu->bases[i] + RK_MMU_COMMAND); in rk_iommu_command()
357 static void rk_iommu_zap_lines(struct rk_iommu *iommu, dma_addr_t iova_start, in rk_iommu_zap_lines() argument
366 for (i = 0; i < iommu->num_mmu; i++) { in rk_iommu_zap_lines()
370 rk_iommu_write(iommu->bases[i], RK_MMU_ZAP_ONE_LINE, iova); in rk_iommu_zap_lines()
374 static bool rk_iommu_is_stall_active(struct rk_iommu *iommu) in rk_iommu_is_stall_active() argument
379 for (i = 0; i < iommu->num_mmu; i++) in rk_iommu_is_stall_active()
380 active &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) & in rk_iommu_is_stall_active()
386 static bool rk_iommu_is_paging_enabled(struct rk_iommu *iommu) in rk_iommu_is_paging_enabled() argument
391 for (i = 0; i < iommu->num_mmu; i++) in rk_iommu_is_paging_enabled()
392 enable &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) & in rk_iommu_is_paging_enabled()
398 static bool rk_iommu_is_reset_done(struct rk_iommu *iommu) in rk_iommu_is_reset_done() argument
403 for (i = 0; i < iommu->num_mmu; i++) in rk_iommu_is_reset_done()
404 done &= rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR) == 0; in rk_iommu_is_reset_done()
409 static int rk_iommu_enable_stall(struct rk_iommu *iommu) in rk_iommu_enable_stall() argument
414 if (rk_iommu_is_stall_active(iommu)) in rk_iommu_enable_stall()
418 if (!rk_iommu_is_paging_enabled(iommu)) in rk_iommu_enable_stall()
421 rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_STALL); in rk_iommu_enable_stall()
423 ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val, in rk_iommu_enable_stall()
427 for (i = 0; i < iommu->num_mmu; i++) in rk_iommu_enable_stall()
428 dev_err(iommu->dev, "Enable stall request timed out, status: %#08x\n", in rk_iommu_enable_stall()
429 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS)); in rk_iommu_enable_stall()
434 static int rk_iommu_disable_stall(struct rk_iommu *iommu) in rk_iommu_disable_stall() argument
439 if (!rk_iommu_is_stall_active(iommu)) in rk_iommu_disable_stall()
442 rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_STALL); in rk_iommu_disable_stall()
444 ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val, in rk_iommu_disable_stall()
448 for (i = 0; i < iommu->num_mmu; i++) in rk_iommu_disable_stall()
449 dev_err(iommu->dev, "Disable stall request timed out, status: %#08x\n", in rk_iommu_disable_stall()
450 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS)); in rk_iommu_disable_stall()
455 static int rk_iommu_enable_paging(struct rk_iommu *iommu) in rk_iommu_enable_paging() argument
460 if (rk_iommu_is_paging_enabled(iommu)) in rk_iommu_enable_paging()
463 rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_PAGING); in rk_iommu_enable_paging()
465 ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val, in rk_iommu_enable_paging()
469 for (i = 0; i < iommu->num_mmu; i++) in rk_iommu_enable_paging()
470 dev_err(iommu->dev, "Enable paging request timed out, status: %#08x\n", in rk_iommu_enable_paging()
471 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS)); in rk_iommu_enable_paging()
476 static int rk_iommu_disable_paging(struct rk_iommu *iommu) in rk_iommu_disable_paging() argument
481 if (!rk_iommu_is_paging_enabled(iommu)) in rk_iommu_disable_paging()
484 rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_PAGING); in rk_iommu_disable_paging()
486 ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val, in rk_iommu_disable_paging()
490 for (i = 0; i < iommu->num_mmu; i++) in rk_iommu_disable_paging()
491 dev_err(iommu->dev, "Disable paging request timed out, status: %#08x\n", in rk_iommu_disable_paging()
492 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS)); in rk_iommu_disable_paging()
497 static int rk_iommu_force_reset(struct rk_iommu *iommu) in rk_iommu_force_reset() argument
503 if (iommu->reset_disabled) in rk_iommu_force_reset()
510 for (i = 0; i < iommu->num_mmu; i++) { in rk_iommu_force_reset()
512 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, dte_addr); in rk_iommu_force_reset()
514 if (dte_addr != rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR)) { in rk_iommu_force_reset()
515 dev_err(iommu->dev, "Error during raw reset. MMU_DTE_ADDR is not functioning\n"); in rk_iommu_force_reset()
520 rk_iommu_command(iommu, RK_MMU_CMD_FORCE_RESET); in rk_iommu_force_reset()
522 ret = readx_poll_timeout(rk_iommu_is_reset_done, iommu, val, in rk_iommu_force_reset()
526 dev_err(iommu->dev, "FORCE_RESET command timed out\n"); in rk_iommu_force_reset()
533 static void log_iova(struct rk_iommu *iommu, int index, dma_addr_t iova) in log_iova() argument
535 void __iomem *base = iommu->bases[index]; in log_iova()
572 dev_err(iommu->dev, "iova = %pad: dte_index: %#03x pte_index: %#03x page_offset: %#03x\n", in log_iova()
574 …dev_err(iommu->dev, "mmu_dte_addr: %pa dte@%pa: %#08x valid: %u pte@%pa: %#08x valid: %u page@%pa … in log_iova()
582 struct rk_iommu *iommu = dev_id; in rk_iommu_irq() local
589 err = pm_runtime_get_if_in_use(iommu->dev); in rk_iommu_irq()
593 if (WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks))) in rk_iommu_irq()
596 for (i = 0; i < iommu->num_mmu; i++) { in rk_iommu_irq()
597 int_status = rk_iommu_read(iommu->bases[i], RK_MMU_INT_STATUS); in rk_iommu_irq()
602 iova = rk_iommu_read(iommu->bases[i], RK_MMU_PAGE_FAULT_ADDR); in rk_iommu_irq()
607 status = rk_iommu_read(iommu->bases[i], RK_MMU_STATUS); in rk_iommu_irq()
611 dev_err(iommu->dev, "Page fault at %pad of type %s\n", in rk_iommu_irq()
615 log_iova(iommu, i, iova); in rk_iommu_irq()
622 if (iommu->domain != &rk_identity_domain) in rk_iommu_irq()
623 report_iommu_fault(iommu->domain, iommu->dev, iova, in rk_iommu_irq()
626 dev_err(iommu->dev, "Page fault while iommu not attached to domain?\n"); in rk_iommu_irq()
628 rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE); in rk_iommu_irq()
629 rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE); in rk_iommu_irq()
633 dev_err(iommu->dev, "BUS_ERROR occurred at %pad\n", &iova); in rk_iommu_irq()
636 dev_err(iommu->dev, "unexpected int_status: %#08x\n", in rk_iommu_irq()
639 rk_iommu_write(iommu->bases[i], RK_MMU_INT_CLEAR, int_status); in rk_iommu_irq()
642 clk_bulk_disable(iommu->num_clocks, iommu->clocks); in rk_iommu_irq()
645 pm_runtime_put(iommu->dev); in rk_iommu_irq()
686 struct rk_iommu *iommu; in rk_iommu_zap_iova() local
689 iommu = list_entry(pos, struct rk_iommu, node); in rk_iommu_zap_iova()
692 ret = pm_runtime_get_if_in_use(iommu->dev); in rk_iommu_zap_iova()
696 WARN_ON(clk_bulk_enable(iommu->num_clocks, in rk_iommu_zap_iova()
697 iommu->clocks)); in rk_iommu_zap_iova()
698 rk_iommu_zap_lines(iommu, iova, size); in rk_iommu_zap_iova()
699 clk_bulk_disable(iommu->num_clocks, iommu->clocks); in rk_iommu_zap_iova()
700 pm_runtime_put(iommu->dev); in rk_iommu_zap_iova()
901 return data ? data->iommu : NULL; in rk_iommu_from_dev()
904 /* Must be called with iommu powered on and attached */
905 static void rk_iommu_disable(struct rk_iommu *iommu) in rk_iommu_disable() argument
910 WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks)); in rk_iommu_disable()
911 rk_iommu_enable_stall(iommu); in rk_iommu_disable()
912 rk_iommu_disable_paging(iommu); in rk_iommu_disable()
913 for (i = 0; i < iommu->num_mmu; i++) { in rk_iommu_disable()
914 rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0); in rk_iommu_disable()
915 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, 0); in rk_iommu_disable()
917 rk_iommu_disable_stall(iommu); in rk_iommu_disable()
918 clk_bulk_disable(iommu->num_clocks, iommu->clocks); in rk_iommu_disable()
921 /* Must be called with iommu powered on and attached */
922 static int rk_iommu_enable(struct rk_iommu *iommu) in rk_iommu_enable() argument
924 struct iommu_domain *domain = iommu->domain; in rk_iommu_enable()
928 ret = clk_bulk_enable(iommu->num_clocks, iommu->clocks); in rk_iommu_enable()
932 ret = rk_iommu_enable_stall(iommu); in rk_iommu_enable()
936 ret = rk_iommu_force_reset(iommu); in rk_iommu_enable()
940 for (i = 0; i < iommu->num_mmu; i++) { in rk_iommu_enable()
941 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, in rk_iommu_enable()
943 rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE); in rk_iommu_enable()
944 rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK); in rk_iommu_enable()
947 ret = rk_iommu_enable_paging(iommu); in rk_iommu_enable()
950 rk_iommu_disable_stall(iommu); in rk_iommu_enable()
952 clk_bulk_disable(iommu->num_clocks, iommu->clocks); in rk_iommu_enable()
959 struct rk_iommu *iommu; in rk_iommu_identity_attach() local
965 iommu = rk_iommu_from_dev(dev); in rk_iommu_identity_attach()
966 if (!iommu) in rk_iommu_identity_attach()
969 rk_domain = to_rk_domain(iommu->domain); in rk_iommu_identity_attach()
971 dev_dbg(dev, "Detaching from iommu domain\n"); in rk_iommu_identity_attach()
973 if (iommu->domain == identity_domain) in rk_iommu_identity_attach()
976 iommu->domain = identity_domain; in rk_iommu_identity_attach()
979 list_del_init(&iommu->node); in rk_iommu_identity_attach()
982 ret = pm_runtime_get_if_in_use(iommu->dev); in rk_iommu_identity_attach()
985 rk_iommu_disable(iommu); in rk_iommu_identity_attach()
986 pm_runtime_put(iommu->dev); in rk_iommu_identity_attach()
1016 struct rk_iommu *iommu; in rk_iommu_attach_device() local
1023 * Such a device does not belong to an iommu group. in rk_iommu_attach_device()
1025 iommu = rk_iommu_from_dev(dev); in rk_iommu_attach_device()
1026 if (!iommu) in rk_iommu_attach_device()
1029 dev_dbg(dev, "Attaching to iommu domain\n"); in rk_iommu_attach_device()
1031 /* iommu already attached */ in rk_iommu_attach_device()
1032 if (iommu->domain == domain) in rk_iommu_attach_device()
1039 iommu->domain = domain; in rk_iommu_attach_device()
1042 list_add_tail(&iommu->node, &rk_domain->iommus); in rk_iommu_attach_device()
1045 ret = pm_runtime_get_if_in_use(iommu->dev); in rk_iommu_attach_device()
1049 ret = rk_iommu_enable(iommu); in rk_iommu_attach_device()
1053 pm_runtime_put(iommu->dev); in rk_iommu_attach_device()
1137 struct rk_iommu *iommu; in rk_iommu_probe_device() local
1143 iommu = rk_iommu_from_dev(dev); in rk_iommu_probe_device()
1145 data->link = device_link_add(dev, iommu->dev, in rk_iommu_probe_device()
1148 return &iommu->iommu; in rk_iommu_probe_device()
1160 struct rk_iommu *iommu; in rk_iommu_device_group() local
1162 iommu = rk_iommu_from_dev(dev); in rk_iommu_device_group()
1164 return iommu_group_ref_get(iommu->group); in rk_iommu_device_group()
1179 data->iommu = platform_get_drvdata(iommu_dev); in rk_iommu_of_xlate()
1180 data->iommu->domain = &rk_identity_domain; in rk_iommu_of_xlate()
1210 struct rk_iommu *iommu; in rk_iommu_probe() local
1216 iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL); in rk_iommu_probe()
1217 if (!iommu) in rk_iommu_probe()
1220 platform_set_drvdata(pdev, iommu); in rk_iommu_probe()
1221 iommu->dev = dev; in rk_iommu_probe()
1222 iommu->num_mmu = 0; in rk_iommu_probe()
1235 iommu->bases = devm_kcalloc(dev, num_res, sizeof(*iommu->bases), in rk_iommu_probe()
1237 if (!iommu->bases) in rk_iommu_probe()
1244 iommu->bases[i] = devm_ioremap_resource(&pdev->dev, res); in rk_iommu_probe()
1245 if (IS_ERR(iommu->bases[i])) in rk_iommu_probe()
1247 iommu->num_mmu++; in rk_iommu_probe()
1249 if (iommu->num_mmu == 0) in rk_iommu_probe()
1250 return PTR_ERR(iommu->bases[0]); in rk_iommu_probe()
1252 iommu->num_irq = platform_irq_count(pdev); in rk_iommu_probe()
1253 if (iommu->num_irq < 0) in rk_iommu_probe()
1254 return iommu->num_irq; in rk_iommu_probe()
1256 iommu->reset_disabled = device_property_read_bool(dev, in rk_iommu_probe()
1259 iommu->num_clocks = ARRAY_SIZE(rk_iommu_clocks); in rk_iommu_probe()
1260 iommu->clocks = devm_kcalloc(iommu->dev, iommu->num_clocks, in rk_iommu_probe()
1261 sizeof(*iommu->clocks), GFP_KERNEL); in rk_iommu_probe()
1262 if (!iommu->clocks) in rk_iommu_probe()
1265 for (i = 0; i < iommu->num_clocks; ++i) in rk_iommu_probe()
1266 iommu->clocks[i].id = rk_iommu_clocks[i]; in rk_iommu_probe()
1269 * iommu clocks should be present for all new devices and devicetrees in rk_iommu_probe()
1273 err = devm_clk_bulk_get(iommu->dev, iommu->num_clocks, iommu->clocks); in rk_iommu_probe()
1275 iommu->num_clocks = 0; in rk_iommu_probe()
1279 err = clk_bulk_prepare(iommu->num_clocks, iommu->clocks); in rk_iommu_probe()
1283 iommu->group = iommu_group_alloc(); in rk_iommu_probe()
1284 if (IS_ERR(iommu->group)) { in rk_iommu_probe()
1285 err = PTR_ERR(iommu->group); in rk_iommu_probe()
1289 err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev)); in rk_iommu_probe()
1293 err = iommu_device_register(&iommu->iommu, &rk_iommu_ops, dev); in rk_iommu_probe()
1298 * Use the first registered IOMMU device for domain to use with DMA in rk_iommu_probe()
1300 * IOMMU device.. in rk_iommu_probe()
1307 for (i = 0; i < iommu->num_irq; i++) { in rk_iommu_probe()
1315 err = devm_request_irq(iommu->dev, irq, rk_iommu_irq, in rk_iommu_probe()
1316 IRQF_SHARED, dev_name(dev), iommu); in rk_iommu_probe()
1327 iommu_device_sysfs_remove(&iommu->iommu); in rk_iommu_probe()
1329 iommu_group_put(iommu->group); in rk_iommu_probe()
1331 clk_bulk_unprepare(iommu->num_clocks, iommu->clocks); in rk_iommu_probe()
1337 struct rk_iommu *iommu = platform_get_drvdata(pdev); in rk_iommu_shutdown() local
1340 for (i = 0; i < iommu->num_irq; i++) { in rk_iommu_shutdown()
1343 devm_free_irq(iommu->dev, irq, iommu); in rk_iommu_shutdown()
1351 struct rk_iommu *iommu = dev_get_drvdata(dev); in rk_iommu_suspend() local
1353 if (iommu->domain == &rk_identity_domain) in rk_iommu_suspend()
1356 rk_iommu_disable(iommu); in rk_iommu_suspend()
1362 struct rk_iommu *iommu = dev_get_drvdata(dev); in rk_iommu_resume() local
1364 if (iommu->domain == &rk_identity_domain) in rk_iommu_resume()
1367 return rk_iommu_enable(iommu); in rk_iommu_resume()
1393 { .compatible = "rockchip,iommu",
1396 { .compatible = "rockchip,rk3568-iommu",