Lines Matching full:iommu
14 #include <linux/iommu.h>
96 struct iommu_device iommu; member
98 /* Lock to modify the IOMMU registers */
121 struct sun50i_iommu *iommu; member
134 static u32 iommu_read(struct sun50i_iommu *iommu, u32 offset) in iommu_read() argument
136 return readl(iommu->base + offset); in iommu_read()
139 static void iommu_write(struct sun50i_iommu *iommu, u32 offset, u32 value) in iommu_write() argument
141 writel(value, iommu->base + offset); in iommu_write()
145 * The Allwinner H6 IOMMU uses a 2-level page table.
154 * The IOMMU supports a single DT, pointed by the IOMMU_TTB_REG
221 * The way permissions work is that the IOMMU has 16 "domains" that
232 * In order to make it work with the IOMMU framework, we will be using
235 * have each master setup in the same way, since the IOMMU framework
290 struct sun50i_iommu *iommu = sun50i_domain->iommu; in sun50i_table_flush() local
294 dma_sync_single_for_device(iommu->dev, dma, size, DMA_TO_DEVICE); in sun50i_table_flush()
297 static int sun50i_iommu_flush_all_tlb(struct sun50i_iommu *iommu) in sun50i_iommu_flush_all_tlb() argument
302 assert_spin_locked(&iommu->iommu_lock); in sun50i_iommu_flush_all_tlb()
304 iommu_write(iommu, in sun50i_iommu_flush_all_tlb()
315 ret = readl_poll_timeout_atomic(iommu->base + IOMMU_TLB_FLUSH_REG, in sun50i_iommu_flush_all_tlb()
319 dev_warn(iommu->dev, "TLB Flush timed out!\n"); in sun50i_iommu_flush_all_tlb()
327 struct sun50i_iommu *iommu = sun50i_domain->iommu; in sun50i_iommu_flush_iotlb_all() local
332 * .probe_device, and since we link our (single) domain to our iommu in in sun50i_iommu_flush_iotlb_all()
338 if (!iommu) in sun50i_iommu_flush_iotlb_all()
341 spin_lock_irqsave(&iommu->iommu_lock, flags); in sun50i_iommu_flush_iotlb_all()
342 sun50i_iommu_flush_all_tlb(iommu); in sun50i_iommu_flush_iotlb_all()
343 spin_unlock_irqrestore(&iommu->iommu_lock, flags); in sun50i_iommu_flush_iotlb_all()
352 static int sun50i_iommu_enable(struct sun50i_iommu *iommu) in sun50i_iommu_enable() argument
358 if (!iommu->domain) in sun50i_iommu_enable()
361 sun50i_domain = to_sun50i_domain(iommu->domain); in sun50i_iommu_enable()
363 ret = reset_control_deassert(iommu->reset); in sun50i_iommu_enable()
367 ret = clk_prepare_enable(iommu->clk); in sun50i_iommu_enable()
371 spin_lock_irqsave(&iommu->iommu_lock, flags); in sun50i_iommu_enable()
373 iommu_write(iommu, IOMMU_TTB_REG, sun50i_domain->dt_dma); in sun50i_iommu_enable()
374 iommu_write(iommu, IOMMU_TLB_PREFETCH_REG, in sun50i_iommu_enable()
381 iommu_write(iommu, IOMMU_INT_ENABLE_REG, IOMMU_INT_MASK); in sun50i_iommu_enable()
382 iommu_write(iommu, IOMMU_DM_AUT_CTRL_REG(SUN50I_IOMMU_ACI_NONE), in sun50i_iommu_enable()
396 iommu_write(iommu, IOMMU_DM_AUT_CTRL_REG(SUN50I_IOMMU_ACI_RD), in sun50i_iommu_enable()
404 iommu_write(iommu, IOMMU_DM_AUT_CTRL_REG(SUN50I_IOMMU_ACI_WR), in sun50i_iommu_enable()
412 ret = sun50i_iommu_flush_all_tlb(iommu); in sun50i_iommu_enable()
414 spin_unlock_irqrestore(&iommu->iommu_lock, flags); in sun50i_iommu_enable()
418 iommu_write(iommu, IOMMU_AUTO_GATING_REG, IOMMU_AUTO_GATING_ENABLE); in sun50i_iommu_enable()
419 iommu_write(iommu, IOMMU_ENABLE_REG, IOMMU_ENABLE_ENABLE); in sun50i_iommu_enable()
421 spin_unlock_irqrestore(&iommu->iommu_lock, flags); in sun50i_iommu_enable()
426 clk_disable_unprepare(iommu->clk); in sun50i_iommu_enable()
429 reset_control_assert(iommu->reset); in sun50i_iommu_enable()
434 static void sun50i_iommu_disable(struct sun50i_iommu *iommu) in sun50i_iommu_disable() argument
438 spin_lock_irqsave(&iommu->iommu_lock, flags); in sun50i_iommu_disable()
440 iommu_write(iommu, IOMMU_ENABLE_REG, 0); in sun50i_iommu_disable()
441 iommu_write(iommu, IOMMU_TTB_REG, 0); in sun50i_iommu_disable()
443 spin_unlock_irqrestore(&iommu->iommu_lock, flags); in sun50i_iommu_disable()
445 clk_disable_unprepare(iommu->clk); in sun50i_iommu_disable()
446 reset_control_assert(iommu->reset); in sun50i_iommu_disable()
449 static void *sun50i_iommu_alloc_page_table(struct sun50i_iommu *iommu, in sun50i_iommu_alloc_page_table() argument
455 page_table = kmem_cache_zalloc(iommu->pt_pool, gfp); in sun50i_iommu_alloc_page_table()
459 pt_dma = dma_map_single(iommu->dev, page_table, PT_SIZE, DMA_TO_DEVICE); in sun50i_iommu_alloc_page_table()
460 if (dma_mapping_error(iommu->dev, pt_dma)) { in sun50i_iommu_alloc_page_table()
461 dev_err(iommu->dev, "Couldn't map L2 Page Table\n"); in sun50i_iommu_alloc_page_table()
462 kmem_cache_free(iommu->pt_pool, page_table); in sun50i_iommu_alloc_page_table()
472 static void sun50i_iommu_free_page_table(struct sun50i_iommu *iommu, in sun50i_iommu_free_page_table() argument
477 dma_unmap_single(iommu->dev, pt_phys, PT_SIZE, DMA_TO_DEVICE); in sun50i_iommu_free_page_table()
478 kmem_cache_free(iommu->pt_pool, page_table); in sun50i_iommu_free_page_table()
484 struct sun50i_iommu *iommu = sun50i_domain->iommu; in sun50i_dte_get_page_table() local
497 page_table = sun50i_iommu_alloc_page_table(iommu, gfp); in sun50i_dte_get_page_table()
511 sun50i_iommu_free_page_table(iommu, drop_pt); in sun50i_dte_get_page_table()
524 struct sun50i_iommu *iommu = sun50i_domain->iommu; in sun50i_iommu_map() local
539 dev_err(iommu->dev, in sun50i_iommu_map()
641 static int sun50i_iommu_attach_domain(struct sun50i_iommu *iommu, in sun50i_iommu_attach_domain() argument
644 iommu->domain = &sun50i_domain->domain; in sun50i_iommu_attach_domain()
645 sun50i_domain->iommu = iommu; in sun50i_iommu_attach_domain()
647 sun50i_domain->dt_dma = dma_map_single(iommu->dev, sun50i_domain->dt, in sun50i_iommu_attach_domain()
649 if (dma_mapping_error(iommu->dev, sun50i_domain->dt_dma)) { in sun50i_iommu_attach_domain()
650 dev_err(iommu->dev, "Couldn't map L1 Page Table\n"); in sun50i_iommu_attach_domain()
654 return sun50i_iommu_enable(iommu); in sun50i_iommu_attach_domain()
657 static void sun50i_iommu_detach_domain(struct sun50i_iommu *iommu, in sun50i_iommu_detach_domain() argument
678 sun50i_iommu_free_page_table(iommu, page_table); in sun50i_iommu_detach_domain()
682 sun50i_iommu_disable(iommu); in sun50i_iommu_detach_domain()
684 dma_unmap_single(iommu->dev, virt_to_phys(sun50i_domain->dt), in sun50i_iommu_detach_domain()
687 iommu->domain = NULL; in sun50i_iommu_detach_domain()
694 struct sun50i_iommu *iommu = dev_iommu_priv_get(dev); in sun50i_iommu_detach_device() local
696 dev_dbg(dev, "Detaching from IOMMU domain\n"); in sun50i_iommu_detach_device()
698 if (iommu->domain != domain) in sun50i_iommu_detach_device()
702 sun50i_iommu_detach_domain(iommu, sun50i_domain); in sun50i_iommu_detach_device()
709 struct sun50i_iommu *iommu; in sun50i_iommu_attach_device() local
711 iommu = sun50i_iommu_from_dev(dev); in sun50i_iommu_attach_device()
712 if (!iommu) in sun50i_iommu_attach_device()
715 dev_dbg(dev, "Attaching to IOMMU domain\n"); in sun50i_iommu_attach_device()
719 if (iommu->domain == domain) in sun50i_iommu_attach_device()
722 if (iommu->domain) in sun50i_iommu_attach_device()
723 sun50i_iommu_detach_device(iommu->domain, dev); in sun50i_iommu_attach_device()
725 sun50i_iommu_attach_domain(iommu, sun50i_domain); in sun50i_iommu_attach_device()
732 struct sun50i_iommu *iommu; in sun50i_iommu_probe_device() local
734 iommu = sun50i_iommu_from_dev(dev); in sun50i_iommu_probe_device()
735 if (!iommu) in sun50i_iommu_probe_device()
738 return &iommu->iommu; in sun50i_iommu_probe_device()
743 struct sun50i_iommu *iommu = sun50i_iommu_from_dev(dev); in sun50i_iommu_device_group() local
745 return iommu_group_ref_get(iommu->group); in sun50i_iommu_device_group()
777 static void sun50i_iommu_report_fault(struct sun50i_iommu *iommu, in sun50i_iommu_report_fault() argument
781 dev_err(iommu->dev, "Page fault for %pad (master %d, dir %s)\n", in sun50i_iommu_report_fault()
784 if (iommu->domain) in sun50i_iommu_report_fault()
785 report_iommu_fault(iommu->domain, iommu->dev, iova, prot); in sun50i_iommu_report_fault()
787 dev_err(iommu->dev, "Page fault while iommu not attached to any domain?\n"); in sun50i_iommu_report_fault()
790 static phys_addr_t sun50i_iommu_handle_pt_irq(struct sun50i_iommu *iommu, in sun50i_iommu_handle_pt_irq() argument
798 assert_spin_locked(&iommu->iommu_lock); in sun50i_iommu_handle_pt_irq()
800 iova = iommu_read(iommu, addr_reg); in sun50i_iommu_handle_pt_irq()
801 blame = iommu_read(iommu, blame_reg); in sun50i_iommu_handle_pt_irq()
809 sun50i_iommu_report_fault(iommu, master, iova, IOMMU_FAULT_READ); in sun50i_iommu_handle_pt_irq()
814 static phys_addr_t sun50i_iommu_handle_perm_irq(struct sun50i_iommu *iommu) in sun50i_iommu_handle_perm_irq() argument
822 assert_spin_locked(&iommu->iommu_lock); in sun50i_iommu_handle_perm_irq()
824 blame = iommu_read(iommu, IOMMU_INT_STA_REG); in sun50i_iommu_handle_perm_irq()
826 iova = iommu_read(iommu, IOMMU_INT_ERR_ADDR_REG(master)); in sun50i_iommu_handle_perm_irq()
827 aci = sun50i_get_pte_aci(iommu_read(iommu, in sun50i_iommu_handle_perm_irq()
864 sun50i_iommu_report_fault(iommu, master, iova, dir); in sun50i_iommu_handle_perm_irq()
871 struct sun50i_iommu *iommu = dev_id; in sun50i_iommu_irq() local
874 spin_lock(&iommu->iommu_lock); in sun50i_iommu_irq()
876 status = iommu_read(iommu, IOMMU_INT_STA_REG); in sun50i_iommu_irq()
878 spin_unlock(&iommu->iommu_lock); in sun50i_iommu_irq()
883 sun50i_iommu_handle_pt_irq(iommu, in sun50i_iommu_irq()
887 sun50i_iommu_handle_pt_irq(iommu, in sun50i_iommu_irq()
891 sun50i_iommu_handle_perm_irq(iommu); in sun50i_iommu_irq()
893 iommu_write(iommu, IOMMU_INT_CLR_REG, status); in sun50i_iommu_irq()
895 iommu_write(iommu, IOMMU_RESET_REG, ~status); in sun50i_iommu_irq()
896 iommu_write(iommu, IOMMU_RESET_REG, status); in sun50i_iommu_irq()
898 spin_unlock(&iommu->iommu_lock); in sun50i_iommu_irq()
905 struct sun50i_iommu *iommu; in sun50i_iommu_probe() local
908 iommu = devm_kzalloc(&pdev->dev, sizeof(*iommu), GFP_KERNEL); in sun50i_iommu_probe()
909 if (!iommu) in sun50i_iommu_probe()
911 spin_lock_init(&iommu->iommu_lock); in sun50i_iommu_probe()
912 platform_set_drvdata(pdev, iommu); in sun50i_iommu_probe()
913 iommu->dev = &pdev->dev; in sun50i_iommu_probe()
915 iommu->pt_pool = kmem_cache_create(dev_name(&pdev->dev), in sun50i_iommu_probe()
919 if (!iommu->pt_pool) in sun50i_iommu_probe()
922 iommu->group = iommu_group_alloc(); in sun50i_iommu_probe()
923 if (IS_ERR(iommu->group)) { in sun50i_iommu_probe()
924 ret = PTR_ERR(iommu->group); in sun50i_iommu_probe()
928 iommu->base = devm_platform_ioremap_resource(pdev, 0); in sun50i_iommu_probe()
929 if (IS_ERR(iommu->base)) { in sun50i_iommu_probe()
930 ret = PTR_ERR(iommu->base); in sun50i_iommu_probe()
940 iommu->clk = devm_clk_get(&pdev->dev, NULL); in sun50i_iommu_probe()
941 if (IS_ERR(iommu->clk)) { in sun50i_iommu_probe()
943 ret = PTR_ERR(iommu->clk); in sun50i_iommu_probe()
947 iommu->reset = devm_reset_control_get(&pdev->dev, NULL); in sun50i_iommu_probe()
948 if (IS_ERR(iommu->reset)) { in sun50i_iommu_probe()
950 ret = PTR_ERR(iommu->reset); in sun50i_iommu_probe()
954 ret = iommu_device_sysfs_add(&iommu->iommu, &pdev->dev, in sun50i_iommu_probe()
959 ret = iommu_device_register(&iommu->iommu, &sun50i_iommu_ops, &pdev->dev); in sun50i_iommu_probe()
964 dev_name(&pdev->dev), iommu); in sun50i_iommu_probe()
971 iommu_device_unregister(&iommu->iommu); in sun50i_iommu_probe()
974 iommu_device_sysfs_remove(&iommu->iommu); in sun50i_iommu_probe()
977 iommu_group_put(iommu->group); in sun50i_iommu_probe()
980 kmem_cache_destroy(iommu->pt_pool); in sun50i_iommu_probe()
986 { .compatible = "allwinner,sun50i-h6-iommu", },
993 .name = "sun50i-iommu",
1000 MODULE_DESCRIPTION("Allwinner H6 IOMMU driver");