Lines Matching refs:smmu

24 	struct tegra_smmu *smmu;  member
54 struct tegra_smmu *smmu; member
70 static inline void smmu_writel(struct tegra_smmu *smmu, u32 value, in smmu_writel() argument
73 writel(value, smmu->regs + offset); in smmu_writel()
76 static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset) in smmu_readl() argument
78 return readl(smmu->regs + offset); in smmu_readl()
87 #define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \ argument
88 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
166 static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr) in smmu_dma_addr_valid() argument
169 return (addr & smmu->pfn_mask) == addr; in smmu_dma_addr_valid()
172 static dma_addr_t smmu_pde_to_dma(struct tegra_smmu *smmu, u32 pde) in smmu_pde_to_dma() argument
174 return (dma_addr_t)(pde & smmu->pfn_mask) << 12; in smmu_pde_to_dma()
177 static void smmu_flush_ptc_all(struct tegra_smmu *smmu) in smmu_flush_ptc_all() argument
179 smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH); in smmu_flush_ptc_all()
182 static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma, in smmu_flush_ptc() argument
187 offset &= ~(smmu->mc->soc->atom_size - 1); in smmu_flush_ptc()
189 if (smmu->mc->soc->num_address_bits > 32) { in smmu_flush_ptc()
195 smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI); in smmu_flush_ptc()
199 smmu_writel(smmu, value, SMMU_PTC_FLUSH); in smmu_flush_ptc()
202 static inline void smmu_flush_tlb(struct tegra_smmu *smmu) in smmu_flush_tlb() argument
204 smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH); in smmu_flush_tlb()
207 static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu, in smmu_flush_tlb_asid() argument
212 if (smmu->soc->num_asids == 4) in smmu_flush_tlb_asid()
218 smmu_writel(smmu, value, SMMU_TLB_FLUSH); in smmu_flush_tlb_asid()
221 static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu, in smmu_flush_tlb_section() argument
227 if (smmu->soc->num_asids == 4) in smmu_flush_tlb_section()
233 smmu_writel(smmu, value, SMMU_TLB_FLUSH); in smmu_flush_tlb_section()
236 static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu, in smmu_flush_tlb_group() argument
242 if (smmu->soc->num_asids == 4) in smmu_flush_tlb_group()
248 smmu_writel(smmu, value, SMMU_TLB_FLUSH); in smmu_flush_tlb_group()
251 static inline void smmu_flush(struct tegra_smmu *smmu) in smmu_flush() argument
253 smmu_readl(smmu, SMMU_PTB_ASID); in smmu_flush()
256 static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp) in tegra_smmu_alloc_asid() argument
260 id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids); in tegra_smmu_alloc_asid()
261 if (id >= smmu->soc->num_asids) in tegra_smmu_alloc_asid()
264 set_bit(id, smmu->asids); in tegra_smmu_alloc_asid()
270 static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id) in tegra_smmu_free_asid() argument
272 clear_bit(id, smmu->asids); in tegra_smmu_free_asid()
332 tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup) in tegra_smmu_find_swgroup() argument
337 for (i = 0; i < smmu->soc->num_swgroups; i++) { in tegra_smmu_find_swgroup()
338 if (smmu->soc->swgroups[i].swgroup == swgroup) { in tegra_smmu_find_swgroup()
339 group = &smmu->soc->swgroups[i]; in tegra_smmu_find_swgroup()
347 static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup, in tegra_smmu_enable() argument
354 group = tegra_smmu_find_swgroup(smmu, swgroup); in tegra_smmu_enable()
356 value = smmu_readl(smmu, group->reg); in tegra_smmu_enable()
360 smmu_writel(smmu, value, group->reg); in tegra_smmu_enable()
368 for (i = 0; i < smmu->soc->num_clients; i++) { in tegra_smmu_enable()
369 const struct tegra_mc_client *client = &smmu->soc->clients[i]; in tegra_smmu_enable()
374 value = smmu_readl(smmu, client->regs.smmu.reg); in tegra_smmu_enable()
375 value |= BIT(client->regs.smmu.bit); in tegra_smmu_enable()
376 smmu_writel(smmu, value, client->regs.smmu.reg); in tegra_smmu_enable()
380 static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup, in tegra_smmu_disable() argument
387 group = tegra_smmu_find_swgroup(smmu, swgroup); in tegra_smmu_disable()
389 value = smmu_readl(smmu, group->reg); in tegra_smmu_disable()
393 smmu_writel(smmu, value, group->reg); in tegra_smmu_disable()
396 for (i = 0; i < smmu->soc->num_clients; i++) { in tegra_smmu_disable()
397 const struct tegra_mc_client *client = &smmu->soc->clients[i]; in tegra_smmu_disable()
402 value = smmu_readl(smmu, client->regs.smmu.reg); in tegra_smmu_disable()
403 value &= ~BIT(client->regs.smmu.bit); in tegra_smmu_disable()
404 smmu_writel(smmu, value, client->regs.smmu.reg); in tegra_smmu_disable()
408 static int tegra_smmu_as_prepare(struct tegra_smmu *smmu, in tegra_smmu_as_prepare() argument
414 mutex_lock(&smmu->lock); in tegra_smmu_as_prepare()
421 as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD, in tegra_smmu_as_prepare()
423 if (dma_mapping_error(smmu->dev, as->pd_dma)) { in tegra_smmu_as_prepare()
429 if (!smmu_dma_addr_valid(smmu, as->pd_dma)) { in tegra_smmu_as_prepare()
434 err = tegra_smmu_alloc_asid(smmu, &as->id); in tegra_smmu_as_prepare()
438 smmu_flush_ptc(smmu, as->pd_dma, 0); in tegra_smmu_as_prepare()
439 smmu_flush_tlb_asid(smmu, as->id); in tegra_smmu_as_prepare()
441 smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID); in tegra_smmu_as_prepare()
443 smmu_writel(smmu, value, SMMU_PTB_DATA); in tegra_smmu_as_prepare()
444 smmu_flush(smmu); in tegra_smmu_as_prepare()
446 as->smmu = smmu; in tegra_smmu_as_prepare()
449 mutex_unlock(&smmu->lock); in tegra_smmu_as_prepare()
454 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE); in tegra_smmu_as_prepare()
456 mutex_unlock(&smmu->lock); in tegra_smmu_as_prepare()
461 static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, in tegra_smmu_as_unprepare() argument
464 mutex_lock(&smmu->lock); in tegra_smmu_as_unprepare()
467 mutex_unlock(&smmu->lock); in tegra_smmu_as_unprepare()
471 tegra_smmu_free_asid(smmu, as->id); in tegra_smmu_as_unprepare()
473 dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE); in tegra_smmu_as_unprepare()
475 as->smmu = NULL; in tegra_smmu_as_unprepare()
477 mutex_unlock(&smmu->lock); in tegra_smmu_as_unprepare()
484 struct tegra_smmu *smmu = dev_iommu_priv_get(dev); in tegra_smmu_attach_dev() local
493 err = tegra_smmu_as_prepare(smmu, as); in tegra_smmu_attach_dev()
497 tegra_smmu_enable(smmu, fwspec->ids[index], as->id); in tegra_smmu_attach_dev()
507 tegra_smmu_disable(smmu, fwspec->ids[index], as->id); in tegra_smmu_attach_dev()
508 tegra_smmu_as_unprepare(smmu, as); in tegra_smmu_attach_dev()
519 struct tegra_smmu *smmu = as->smmu; in tegra_smmu_set_platform_dma() local
526 tegra_smmu_disable(smmu, fwspec->ids[index], as->id); in tegra_smmu_set_platform_dma()
527 tegra_smmu_as_unprepare(smmu, as); in tegra_smmu_set_platform_dma()
535 struct tegra_smmu *smmu = as->smmu; in tegra_smmu_set_pde() local
543 dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset, in tegra_smmu_set_pde()
547 smmu_flush_ptc(smmu, as->pd_dma, offset); in tegra_smmu_set_pde()
548 smmu_flush_tlb_section(smmu, as->id, iova); in tegra_smmu_set_pde()
549 smmu_flush(smmu); in tegra_smmu_set_pde()
563 struct tegra_smmu *smmu = as->smmu; in tegra_smmu_pte_lookup() local
572 *dmap = smmu_pde_to_dma(smmu, pd[pd_index]); in tegra_smmu_pte_lookup()
581 struct tegra_smmu *smmu = as->smmu; in as_get_pte() local
586 dma = dma_map_page(smmu->dev, page, 0, SMMU_SIZE_PT, in as_get_pte()
588 if (dma_mapping_error(smmu->dev, dma)) { in as_get_pte()
593 if (!smmu_dma_addr_valid(smmu, dma)) { in as_get_pte()
594 dma_unmap_page(smmu->dev, dma, SMMU_SIZE_PT, in as_get_pte()
609 *dmap = smmu_pde_to_dma(smmu, pd[pde]); in as_get_pte()
632 struct tegra_smmu *smmu = as->smmu; in tegra_smmu_pte_put_use() local
634 dma_addr_t pte_dma = smmu_pde_to_dma(smmu, pd[pde]); in tegra_smmu_pte_put_use()
638 dma_unmap_page(smmu->dev, pte_dma, SMMU_SIZE_PT, DMA_TO_DEVICE); in tegra_smmu_pte_put_use()
647 struct tegra_smmu *smmu = as->smmu; in tegra_smmu_set_pte() local
652 dma_sync_single_range_for_device(smmu->dev, pte_dma, offset, in tegra_smmu_set_pte()
654 smmu_flush_ptc(smmu, pte_dma, offset); in tegra_smmu_set_pte()
655 smmu_flush_tlb_group(smmu, as->id, iova); in tegra_smmu_set_pte()
656 smmu_flush(smmu); in tegra_smmu_set_pte()
792 pfn = *pte & as->smmu->pfn_mask; in tegra_smmu_iova_to_phys()
812 return mc->smmu; in tegra_smmu_find()
815 static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev, in tegra_smmu_configure() argument
818 const struct iommu_ops *ops = smmu->iommu.ops; in tegra_smmu_configure()
840 struct tegra_smmu *smmu = NULL; in tegra_smmu_probe_device() local
847 smmu = tegra_smmu_find(args.np); in tegra_smmu_probe_device()
848 if (smmu) { in tegra_smmu_probe_device()
849 err = tegra_smmu_configure(smmu, dev, &args); in tegra_smmu_probe_device()
861 smmu = dev_iommu_priv_get(dev); in tegra_smmu_probe_device()
862 if (!smmu) in tegra_smmu_probe_device()
865 return &smmu->iommu; in tegra_smmu_probe_device()
869 tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup) in tegra_smmu_find_group() argument
873 for (i = 0; i < smmu->soc->num_groups; i++) in tegra_smmu_find_group()
874 for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++) in tegra_smmu_find_group()
875 if (smmu->soc->groups[i].swgroups[j] == swgroup) in tegra_smmu_find_group()
876 return &smmu->soc->groups[i]; in tegra_smmu_find_group()
884 struct tegra_smmu *smmu = group->smmu; in tegra_smmu_group_release() local
886 mutex_lock(&smmu->lock); in tegra_smmu_group_release()
888 mutex_unlock(&smmu->lock); in tegra_smmu_group_release()
894 struct tegra_smmu *smmu = dev_iommu_priv_get(dev); in tegra_smmu_device_group() local
901 soc = tegra_smmu_find_group(smmu, swgroup); in tegra_smmu_device_group()
903 mutex_lock(&smmu->lock); in tegra_smmu_device_group()
906 list_for_each_entry(group, &smmu->groups, list) in tegra_smmu_device_group()
909 mutex_unlock(&smmu->lock); in tegra_smmu_device_group()
913 group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL); in tegra_smmu_device_group()
915 mutex_unlock(&smmu->lock); in tegra_smmu_device_group()
921 group->smmu = smmu; in tegra_smmu_device_group()
930 devm_kfree(smmu->dev, group); in tegra_smmu_device_group()
931 mutex_unlock(&smmu->lock); in tegra_smmu_device_group()
938 list_add_tail(&group->list, &smmu->groups); in tegra_smmu_device_group()
939 mutex_unlock(&smmu->lock); in tegra_smmu_device_group()
960 dev_iommu_priv_set(dev, mc->smmu); in tegra_smmu_of_xlate()
998 struct tegra_smmu *smmu = s->private; in tegra_smmu_swgroups_show() local
1005 for (i = 0; i < smmu->soc->num_swgroups; i++) { in tegra_smmu_swgroups_show()
1006 const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i]; in tegra_smmu_swgroups_show()
1010 value = smmu_readl(smmu, group->reg); in tegra_smmu_swgroups_show()
1030 struct tegra_smmu *smmu = s->private; in tegra_smmu_clients_show() local
1037 for (i = 0; i < smmu->soc->num_clients; i++) { in tegra_smmu_clients_show()
1038 const struct tegra_mc_client *client = &smmu->soc->clients[i]; in tegra_smmu_clients_show()
1041 value = smmu_readl(smmu, client->regs.smmu.reg); in tegra_smmu_clients_show()
1043 if (value & BIT(client->regs.smmu.bit)) in tegra_smmu_clients_show()
1056 static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu) in tegra_smmu_debugfs_init() argument
1058 smmu->debugfs = debugfs_create_dir("smmu", NULL); in tegra_smmu_debugfs_init()
1059 if (!smmu->debugfs) in tegra_smmu_debugfs_init()
1062 debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu, in tegra_smmu_debugfs_init()
1064 debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu, in tegra_smmu_debugfs_init()
1068 static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu) in tegra_smmu_debugfs_exit() argument
1070 debugfs_remove_recursive(smmu->debugfs); in tegra_smmu_debugfs_exit()
1077 struct tegra_smmu *smmu; in tegra_smmu_probe() local
1081 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL); in tegra_smmu_probe()
1082 if (!smmu) in tegra_smmu_probe()
1093 mc->smmu = smmu; in tegra_smmu_probe()
1095 smmu->asids = devm_bitmap_zalloc(dev, soc->num_asids, GFP_KERNEL); in tegra_smmu_probe()
1096 if (!smmu->asids) in tegra_smmu_probe()
1099 INIT_LIST_HEAD(&smmu->groups); in tegra_smmu_probe()
1100 mutex_init(&smmu->lock); in tegra_smmu_probe()
1102 smmu->regs = mc->regs; in tegra_smmu_probe()
1103 smmu->soc = soc; in tegra_smmu_probe()
1104 smmu->dev = dev; in tegra_smmu_probe()
1105 smmu->mc = mc; in tegra_smmu_probe()
1107 smmu->pfn_mask = in tegra_smmu_probe()
1110 mc->soc->num_address_bits, smmu->pfn_mask); in tegra_smmu_probe()
1111 smmu->tlb_mask = (1 << fls(smmu->soc->num_tlb_lines)) - 1; in tegra_smmu_probe()
1112 dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines, in tegra_smmu_probe()
1113 smmu->tlb_mask); in tegra_smmu_probe()
1120 smmu_writel(smmu, value, SMMU_PTC_CONFIG); in tegra_smmu_probe()
1123 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu); in tegra_smmu_probe()
1128 smmu_writel(smmu, value, SMMU_TLB_CONFIG); in tegra_smmu_probe()
1130 smmu_flush_ptc_all(smmu); in tegra_smmu_probe()
1131 smmu_flush_tlb(smmu); in tegra_smmu_probe()
1132 smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG); in tegra_smmu_probe()
1133 smmu_flush(smmu); in tegra_smmu_probe()
1137 err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev)); in tegra_smmu_probe()
1141 err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev); in tegra_smmu_probe()
1143 iommu_device_sysfs_remove(&smmu->iommu); in tegra_smmu_probe()
1148 tegra_smmu_debugfs_init(smmu); in tegra_smmu_probe()
1150 return smmu; in tegra_smmu_probe()
1153 void tegra_smmu_remove(struct tegra_smmu *smmu) in tegra_smmu_remove() argument
1155 iommu_device_unregister(&smmu->iommu); in tegra_smmu_remove()
1156 iommu_device_sysfs_remove(&smmu->iommu); in tegra_smmu_remove()
1159 tegra_smmu_debugfs_exit(smmu); in tegra_smmu_remove()