Lines Matching +full:t8103 +full:- +full:pcie

1 // SPDX-License-Identifier: GPL-2.0-only
7 * Based on arm/arm-smmu/arm-ssmu.c and arm/arm-smmu-v3/arm-smmu-v3.c
10 * and on exynos-iommu.c
18 #include <linux/dma-mapping.h>
21 #include <linux/io-pgtable.h>
35 #include "dma-iommu.h"
145 #define DART_TCR(dart, sid) ((dart)->hw->tcr + ((sid) << 2))
147 #define DART_TTBR(dart, sid, idx) ((dart)->hw->ttbr + \
148 (((dart)->hw->ttbr_count * (sid)) << 2) + \
192 * @hw: SoC-specific hardware data
255 * @pgtbl_ops: pagetable ops allocated by io-pgtable
292 for (i = 0, stream_map = &(base)->stream_maps[0]; \
293 i < MAX_DARTS_PER_DEVICE && stream_map->dart; \
294 stream_map = &(base)->stream_maps[++i])
307 struct apple_dart *dart = stream_map->dart; in apple_dart_hw_enable_translation()
310 for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) in apple_dart_hw_enable_translation()
311 writel(dart->hw->tcr_enabled, dart->regs + DART_TCR(dart, sid)); in apple_dart_hw_enable_translation()
316 struct apple_dart *dart = stream_map->dart; in apple_dart_hw_disable_dma()
319 for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) in apple_dart_hw_disable_dma()
320 writel(dart->hw->tcr_disabled, dart->regs + DART_TCR(dart, sid)); in apple_dart_hw_disable_dma()
326 struct apple_dart *dart = stream_map->dart; in apple_dart_hw_enable_bypass()
329 WARN_ON(!stream_map->dart->supports_bypass); in apple_dart_hw_enable_bypass()
330 for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) in apple_dart_hw_enable_bypass()
331 writel(dart->hw->tcr_bypass, in apple_dart_hw_enable_bypass()
332 dart->regs + DART_TCR(dart, sid)); in apple_dart_hw_enable_bypass()
338 struct apple_dart *dart = stream_map->dart; in apple_dart_hw_set_ttbr()
341 WARN_ON(paddr & ((1 << dart->hw->ttbr_shift) - 1)); in apple_dart_hw_set_ttbr()
342 for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) in apple_dart_hw_set_ttbr()
343 writel(dart->hw->ttbr_valid | in apple_dart_hw_set_ttbr()
344 (paddr >> dart->hw->ttbr_shift) << dart->hw->ttbr_addr_field_shift, in apple_dart_hw_set_ttbr()
345 dart->regs + DART_TTBR(dart, sid, idx)); in apple_dart_hw_set_ttbr()
351 struct apple_dart *dart = stream_map->dart; in apple_dart_hw_clear_ttbr()
354 for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) in apple_dart_hw_clear_ttbr()
355 writel(0, dart->regs + DART_TTBR(dart, sid, idx)); in apple_dart_hw_clear_ttbr()
363 for (i = 0; i < stream_map->dart->hw->ttbr_count; ++i) in apple_dart_hw_clear_all_ttbrs()
375 spin_lock_irqsave(&stream_map->dart->lock, flags); in apple_dart_t8020_hw_stream_command()
377 for (i = 0; i < BITS_TO_U32(stream_map->dart->num_streams); i++) in apple_dart_t8020_hw_stream_command()
378 writel(stream_map->sidmap[i], in apple_dart_t8020_hw_stream_command()
379 stream_map->dart->regs + DART_T8020_STREAM_SELECT + 4 * i); in apple_dart_t8020_hw_stream_command()
380 writel(command, stream_map->dart->regs + DART_T8020_STREAM_COMMAND); in apple_dart_t8020_hw_stream_command()
383 stream_map->dart->regs + DART_T8020_STREAM_COMMAND, command_reg, in apple_dart_t8020_hw_stream_command()
387 spin_unlock_irqrestore(&stream_map->dart->lock, flags); in apple_dart_t8020_hw_stream_command()
390 dev_err(stream_map->dart->dev, in apple_dart_t8020_hw_stream_command()
392 command, stream_map->sidmap[0]); in apple_dart_t8020_hw_stream_command()
403 struct apple_dart *dart = stream_map->dart; in apple_dart_t8110_hw_tlb_command()
408 spin_lock_irqsave(&dart->lock, flags); in apple_dart_t8110_hw_tlb_command()
410 for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) { in apple_dart_t8110_hw_tlb_command()
413 writel(val, dart->regs + DART_T8110_TLB_CMD); in apple_dart_t8110_hw_tlb_command()
416 dart->regs + DART_T8110_TLB_CMD, val, in apple_dart_t8110_hw_tlb_command()
425 spin_unlock_irqrestore(&dart->lock, flags); in apple_dart_t8110_hw_tlb_command()
428 dev_err(stream_map->dart->dev, in apple_dart_t8110_hw_tlb_command()
457 config = readl(dart->regs + dart->hw->lock); in apple_dart_hw_reset()
458 if (config & dart->hw->lock_bit) { in apple_dart_hw_reset()
459 dev_err(dart->dev, "DART is locked down until reboot: %08x\n", in apple_dart_hw_reset()
461 return -EINVAL; in apple_dart_hw_reset()
466 bitmap_set(stream_map.sidmap, 0, dart->num_streams); in apple_dart_hw_reset()
471 for (i = 0; i < BITS_TO_U32(dart->num_streams); i++) in apple_dart_hw_reset()
472 writel(U32_MAX, dart->regs + dart->hw->enable_streams + 4 * i); in apple_dart_hw_reset()
475 writel(readl(dart->regs + dart->hw->error), dart->regs + dart->hw->error); in apple_dart_hw_reset()
477 if (dart->hw->type == DART_T8110) in apple_dart_hw_reset()
478 writel(0, dart->regs + DART_T8110_ERROR_MASK); in apple_dart_hw_reset()
480 return dart->hw->invalidate_tlb(&stream_map); in apple_dart_hw_reset()
490 stream_map.dart = domain_stream_map->dart; in apple_dart_domain_flush_tlb()
492 for (j = 0; j < BITS_TO_LONGS(stream_map.dart->num_streams); j++) in apple_dart_domain_flush_tlb()
493 stream_map.sidmap[j] = atomic_long_read(&domain_stream_map->sidmap[j]); in apple_dart_domain_flush_tlb()
495 stream_map.dart->hw->invalidate_tlb(&stream_map); in apple_dart_domain_flush_tlb()
521 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops; in apple_dart_iova_to_phys()
526 return ops->iova_to_phys(ops, iova); in apple_dart_iova_to_phys()
535 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops; in apple_dart_map_pages()
538 return -ENODEV; in apple_dart_map_pages()
540 return ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, gfp, in apple_dart_map_pages()
550 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops; in apple_dart_unmap_pages()
552 return ops->unmap_pages(ops, iova, pgsize, pgcount, gather); in apple_dart_unmap_pages()
561 &io_pgtable_ops_to_pgtable(domain->pgtbl_ops)->cfg; in apple_dart_setup_translation()
563 for (i = 0; i < pgtbl_cfg->apple_dart_cfg.n_ttbrs; ++i) in apple_dart_setup_translation()
565 pgtbl_cfg->apple_dart_cfg.ttbr[i]); in apple_dart_setup_translation()
566 for (; i < stream_map->dart->hw->ttbr_count; ++i) in apple_dart_setup_translation()
570 stream_map->dart->hw->invalidate_tlb(stream_map); in apple_dart_setup_translation()
576 struct apple_dart *dart = cfg->stream_maps[0].dart; in apple_dart_finalize_domain()
581 if (dart->pgsize > PAGE_SIZE) in apple_dart_finalize_domain()
582 return -EINVAL; in apple_dart_finalize_domain()
584 mutex_lock(&dart_domain->init_lock); in apple_dart_finalize_domain()
586 if (dart_domain->finalized) in apple_dart_finalize_domain()
590 dart_domain->stream_maps[i].dart = cfg->stream_maps[i].dart; in apple_dart_finalize_domain()
591 for (j = 0; j < BITS_TO_LONGS(dart->num_streams); j++) in apple_dart_finalize_domain()
592 atomic_long_set(&dart_domain->stream_maps[i].sidmap[j], in apple_dart_finalize_domain()
593 cfg->stream_maps[i].sidmap[j]); in apple_dart_finalize_domain()
597 .pgsize_bitmap = dart->pgsize, in apple_dart_finalize_domain()
598 .ias = dart->ias, in apple_dart_finalize_domain()
599 .oas = dart->oas, in apple_dart_finalize_domain()
601 .iommu_dev = dart->dev, in apple_dart_finalize_domain()
604 dart_domain->pgtbl_ops = alloc_io_pgtable_ops(dart->hw->fmt, &pgtbl_cfg, in apple_dart_finalize_domain()
605 &dart_domain->domain); in apple_dart_finalize_domain()
606 if (!dart_domain->pgtbl_ops) { in apple_dart_finalize_domain()
607 ret = -ENOMEM; in apple_dart_finalize_domain()
611 dart_domain->domain.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap; in apple_dart_finalize_domain()
612 dart_domain->domain.geometry.aperture_start = 0; in apple_dart_finalize_domain()
613 dart_domain->domain.geometry.aperture_end = in apple_dart_finalize_domain()
614 (dma_addr_t)DMA_BIT_MASK(dart->ias); in apple_dart_finalize_domain()
615 dart_domain->domain.geometry.force_aperture = true; in apple_dart_finalize_domain()
617 dart_domain->finalized = true; in apple_dart_finalize_domain()
620 mutex_unlock(&dart_domain->init_lock); in apple_dart_finalize_domain()
633 return -EINVAL; in apple_dart_mod_streams()
639 for (j = 0; j < BITS_TO_LONGS(domain_maps[i].dart->num_streams); j++) { in apple_dart_mod_streams()
655 return apple_dart_mod_streams(domain->stream_maps, cfg->stream_maps, in apple_dart_domain_add_streams()
687 if (!cfg->stream_maps[0].dart->supports_bypass) in apple_dart_attach_dev_identity()
688 return -EINVAL; in apple_dart_attach_dev_identity()
732 return ERR_PTR(-ENODEV); in apple_dart_probe_device()
736 dev, stream_map->dart->dev, in apple_dart_probe_device()
739 return &cfg->stream_maps[0].dart->iommu; in apple_dart_probe_device()
757 mutex_init(&dart_domain->init_lock); in apple_dart_domain_alloc_paging()
769 return &dart_domain->domain; in apple_dart_domain_alloc_paging()
776 if (dart_domain->pgtbl_ops) in apple_dart_domain_free()
777 free_io_pgtable_ops(dart_domain->pgtbl_ops); in apple_dart_domain_free()
786 struct platform_device *iommu_pdev = of_find_device_by_node(args->np); in apple_dart_of_xlate()
791 if (args->args_count != 1) in apple_dart_of_xlate()
792 return -EINVAL; in apple_dart_of_xlate()
793 sid = args->args[0]; in apple_dart_of_xlate()
798 return -ENOMEM; in apple_dart_of_xlate()
801 cfg_dart = cfg->stream_maps[0].dart; in apple_dart_of_xlate()
803 if (cfg_dart->supports_bypass != dart->supports_bypass) in apple_dart_of_xlate()
804 return -EINVAL; in apple_dart_of_xlate()
805 if (cfg_dart->pgsize != dart->pgsize) in apple_dart_of_xlate()
806 return -EINVAL; in apple_dart_of_xlate()
810 if (cfg->stream_maps[i].dart == dart) { in apple_dart_of_xlate()
811 set_bit(sid, cfg->stream_maps[i].sidmap); in apple_dart_of_xlate()
816 if (!cfg->stream_maps[i].dart) { in apple_dart_of_xlate()
817 cfg->stream_maps[i].dart = dart; in apple_dart_of_xlate()
818 set_bit(sid, cfg->stream_maps[i].sidmap); in apple_dart_of_xlate()
823 return -EINVAL; in apple_dart_of_xlate()
837 for_each_set_bit(sid, stream_map->sidmap, stream_map->dart->num_streams) in apple_dart_release_group()
838 stream_map->dart->sid2group[sid] = NULL; in apple_dart_release_group()
850 * PCIe devices from the same bus across multiple DARTs such that we can in apple_dart_merge_master_cfg()
853 if (src->stream_maps[1].dart) in apple_dart_merge_master_cfg()
854 return -EINVAL; in apple_dart_merge_master_cfg()
855 if (dst->stream_maps[1].dart) in apple_dart_merge_master_cfg()
856 return -EINVAL; in apple_dart_merge_master_cfg()
857 if (src->stream_maps[0].dart != dst->stream_maps[0].dart) in apple_dart_merge_master_cfg()
858 return -EINVAL; in apple_dart_merge_master_cfg()
860 bitmap_or(dst->stream_maps[0].sidmap, in apple_dart_merge_master_cfg()
861 dst->stream_maps[0].sidmap, in apple_dart_merge_master_cfg()
862 src->stream_maps[0].sidmap, in apple_dart_merge_master_cfg()
863 dst->stream_maps[0].dart->num_streams); in apple_dart_merge_master_cfg()
874 struct iommu_group *res = ERR_PTR(-EINVAL); in apple_dart_device_group()
879 for_each_set_bit(sid, stream_map->sidmap, stream_map->dart->num_streams) { in apple_dart_device_group()
881 stream_map->dart->sid2group[sid]; in apple_dart_device_group()
884 res = ERR_PTR(-EINVAL); in apple_dart_device_group()
904 res = ERR_PTR(-ENOMEM); in apple_dart_device_group()
932 for_each_set_bit(sid, stream_map->sidmap, stream_map->dart->num_streams) in apple_dart_device_group()
933 stream_map->dart->sid2group[sid] = group; in apple_dart_device_group()
946 if (cfg->stream_maps[0].dart->pgsize > PAGE_SIZE) in apple_dart_def_domain_type()
948 if (!cfg->stream_maps[0].dart->supports_bypass) in apple_dart_def_domain_type()
973 list_add_tail(&region->list, head); in apple_dart_get_resv_regions()
989 .pgsize_bitmap = -1UL, /* Restricted during dart probe */
1007 u32 error = readl(dart->regs + DART_T8020_ERROR); in apple_dart_t8020_irq()
1009 u32 addr_lo = readl(dart->regs + DART_T8020_ERROR_ADDR_LO); in apple_dart_t8020_irq()
1010 u32 addr_hi = readl(dart->regs + DART_T8020_ERROR_ADDR_HI); in apple_dart_t8020_irq()
1032 dart->dev, in apple_dart_t8020_irq()
1036 writel(error, dart->regs + DART_T8020_ERROR); in apple_dart_t8020_irq()
1044 u32 error = readl(dart->regs + DART_T8110_ERROR); in apple_dart_t8110_irq()
1046 u32 addr_lo = readl(dart->regs + DART_T8110_ERROR_ADDR_LO); in apple_dart_t8110_irq()
1047 u32 addr_hi = readl(dart->regs + DART_T8110_ERROR_ADDR_HI); in apple_dart_t8110_irq()
1071 dart->dev, in apple_dart_t8110_irq()
1075 writel(error, dart->regs + DART_T8110_ERROR); in apple_dart_t8110_irq()
1085 struct device *dev = &pdev->dev; in apple_dart_probe()
1089 return -ENOMEM; in apple_dart_probe()
1091 dart->dev = dev; in apple_dart_probe()
1092 dart->hw = of_device_get_match_data(dev); in apple_dart_probe()
1093 spin_lock_init(&dart->lock); in apple_dart_probe()
1095 dart->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); in apple_dart_probe()
1096 if (IS_ERR(dart->regs)) in apple_dart_probe()
1097 return PTR_ERR(dart->regs); in apple_dart_probe()
1101 return -EINVAL; in apple_dart_probe()
1104 dart->irq = platform_get_irq(pdev, 0); in apple_dart_probe()
1105 if (dart->irq < 0) in apple_dart_probe()
1106 return -ENODEV; in apple_dart_probe()
1108 ret = devm_clk_bulk_get_all(dev, &dart->clks); in apple_dart_probe()
1111 dart->num_clks = ret; in apple_dart_probe()
1113 ret = clk_bulk_prepare_enable(dart->num_clks, dart->clks); in apple_dart_probe()
1117 dart_params[0] = readl(dart->regs + DART_PARAMS1); in apple_dart_probe()
1118 dart_params[1] = readl(dart->regs + DART_PARAMS2); in apple_dart_probe()
1119 dart->pgsize = 1 << FIELD_GET(DART_PARAMS1_PAGE_SHIFT, dart_params[0]); in apple_dart_probe()
1120 dart->supports_bypass = dart_params[1] & DART_PARAMS2_BYPASS_SUPPORT; in apple_dart_probe()
1122 switch (dart->hw->type) { in apple_dart_probe()
1125 dart->ias = 32; in apple_dart_probe()
1126 dart->oas = dart->hw->oas; in apple_dart_probe()
1127 dart->num_streams = dart->hw->max_sid_count; in apple_dart_probe()
1131 dart_params[2] = readl(dart->regs + DART_T8110_PARAMS3); in apple_dart_probe()
1132 dart_params[3] = readl(dart->regs + DART_T8110_PARAMS4); in apple_dart_probe()
1133 dart->ias = FIELD_GET(DART_T8110_PARAMS3_VA_WIDTH, dart_params[2]); in apple_dart_probe()
1134 dart->oas = FIELD_GET(DART_T8110_PARAMS3_PA_WIDTH, dart_params[2]); in apple_dart_probe()
1135 dart->num_streams = FIELD_GET(DART_T8110_PARAMS4_NUM_SIDS, dart_params[3]); in apple_dart_probe()
1139 if (dart->num_streams > DART_MAX_STREAMS) { in apple_dart_probe()
1140 dev_err(&pdev->dev, "Too many streams (%d > %d)\n", in apple_dart_probe()
1141 dart->num_streams, DART_MAX_STREAMS); in apple_dart_probe()
1142 ret = -EINVAL; in apple_dart_probe()
1150 ret = request_irq(dart->irq, dart->hw->irq_handler, IRQF_SHARED, in apple_dart_probe()
1151 "apple-dart fault handler", dart); in apple_dart_probe()
1157 ret = iommu_device_sysfs_add(&dart->iommu, dev, NULL, "apple-dart.%s", in apple_dart_probe()
1158 dev_name(&pdev->dev)); in apple_dart_probe()
1162 ret = iommu_device_register(&dart->iommu, &apple_dart_iommu_ops, dev); in apple_dart_probe()
1167 &pdev->dev, in apple_dart_probe()
1169 dart->pgsize, dart->num_streams, dart->supports_bypass, in apple_dart_probe()
1170 dart->pgsize > PAGE_SIZE); in apple_dart_probe()
1174 iommu_device_sysfs_remove(&dart->iommu); in apple_dart_probe()
1176 free_irq(dart->irq, dart); in apple_dart_probe()
1178 clk_bulk_disable_unprepare(dart->num_clks, dart->clks); in apple_dart_probe()
1188 free_irq(dart->irq, dart); in apple_dart_remove()
1190 iommu_device_unregister(&dart->iommu); in apple_dart_remove()
1191 iommu_device_sysfs_remove(&dart->iommu); in apple_dart_remove()
1193 clk_bulk_disable_unprepare(dart->num_clks, dart->clks); in apple_dart_remove()
1304 for (sid = 0; sid < dart->num_streams; sid++) { in apple_dart_suspend()
1305 dart->save_tcr[sid] = readl(dart->regs + DART_TCR(dart, sid)); in apple_dart_suspend()
1306 for (idx = 0; idx < dart->hw->ttbr_count; idx++) in apple_dart_suspend()
1307 dart->save_ttbr[sid][idx] = in apple_dart_suspend()
1308 readl(dart->regs + DART_TTBR(dart, sid, idx)); in apple_dart_suspend()
1326 for (sid = 0; sid < dart->num_streams; sid++) { in apple_dart_resume()
1327 for (idx = 0; idx < dart->hw->ttbr_count; idx++) in apple_dart_resume()
1328 writel(dart->save_ttbr[sid][idx], in apple_dart_resume()
1329 dart->regs + DART_TTBR(dart, sid, idx)); in apple_dart_resume()
1330 writel(dart->save_tcr[sid], dart->regs + DART_TCR(dart, sid)); in apple_dart_resume()
1339 { .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 },
1340 { .compatible = "apple,t8103-usb4-dart", .data = &apple_dart_hw_t8103_usb4 },
1341 { .compatible = "apple,t8110-dart", .data = &apple_dart_hw_t8110 },
1342 { .compatible = "apple,t6000-dart", .data = &apple_dart_hw_t6000 },
1349 .name = "apple-dart",