2 * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/bitops.h>
10 #include <linux/debugfs.h>
11 #include <linux/err.h>
12 #include <linux/iommu.h>
13 #include <linux/kernel.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
20 #include <soc/tegra/ahb.h>
21 #include <soc/tegra/mc.h>
28 const struct tegra_smmu_soc
*soc
;
30 unsigned long pfn_mask
;
31 unsigned long tlb_mask
;
36 struct list_head list
;
38 struct dentry
*debugfs
;
40 struct iommu_device iommu
; /* IOMMU Core code handle */
43 struct tegra_smmu_as
{
44 struct iommu_domain domain
;
45 struct tegra_smmu
*smmu
;
46 unsigned int use_count
;
55 static struct tegra_smmu_as
*to_smmu_as(struct iommu_domain
*dom
)
57 return container_of(dom
, struct tegra_smmu_as
, domain
);
60 static inline void smmu_writel(struct tegra_smmu
*smmu
, u32 value
,
63 writel(value
, smmu
->regs
+ offset
);
66 static inline u32
smmu_readl(struct tegra_smmu
*smmu
, unsigned long offset
)
68 return readl(smmu
->regs
+ offset
);
71 #define SMMU_CONFIG 0x010
72 #define SMMU_CONFIG_ENABLE (1 << 0)
74 #define SMMU_TLB_CONFIG 0x14
75 #define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
76 #define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
77 #define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
78 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
80 #define SMMU_PTC_CONFIG 0x18
81 #define SMMU_PTC_CONFIG_ENABLE (1 << 29)
82 #define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
83 #define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
85 #define SMMU_PTB_ASID 0x01c
86 #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
88 #define SMMU_PTB_DATA 0x020
89 #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
91 #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
93 #define SMMU_TLB_FLUSH 0x030
94 #define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
95 #define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
96 #define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
97 #define SMMU_TLB_FLUSH_ASID(x) (((x) & 0x7f) << 24)
98 #define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
99 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
100 #define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
101 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
102 #define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
104 #define SMMU_PTC_FLUSH 0x034
105 #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
106 #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
108 #define SMMU_PTC_FLUSH_HI 0x9b8
109 #define SMMU_PTC_FLUSH_HI_MASK 0x3
111 /* per-SWGROUP SMMU_*_ASID register */
112 #define SMMU_ASID_ENABLE (1 << 31)
113 #define SMMU_ASID_MASK 0x7f
114 #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
116 /* page table definitions */
117 #define SMMU_NUM_PDE 1024
118 #define SMMU_NUM_PTE 1024
120 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
121 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
123 #define SMMU_PDE_SHIFT 22
124 #define SMMU_PTE_SHIFT 12
126 #define SMMU_PD_READABLE (1 << 31)
127 #define SMMU_PD_WRITABLE (1 << 30)
128 #define SMMU_PD_NONSECURE (1 << 29)
130 #define SMMU_PDE_READABLE (1 << 31)
131 #define SMMU_PDE_WRITABLE (1 << 30)
132 #define SMMU_PDE_NONSECURE (1 << 29)
133 #define SMMU_PDE_NEXT (1 << 28)
135 #define SMMU_PTE_READABLE (1 << 31)
136 #define SMMU_PTE_WRITABLE (1 << 30)
137 #define SMMU_PTE_NONSECURE (1 << 29)
139 #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
141 #define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
144 static unsigned int iova_pd_index(unsigned long iova
)
146 return (iova
>> SMMU_PDE_SHIFT
) & (SMMU_NUM_PDE
- 1);
149 static unsigned int iova_pt_index(unsigned long iova
)
151 return (iova
>> SMMU_PTE_SHIFT
) & (SMMU_NUM_PTE
- 1);
154 static bool smmu_dma_addr_valid(struct tegra_smmu
*smmu
, dma_addr_t addr
)
157 return (addr
& smmu
->pfn_mask
) == addr
;
160 static dma_addr_t
smmu_pde_to_dma(u32 pde
)
165 static void smmu_flush_ptc_all(struct tegra_smmu
*smmu
)
167 smmu_writel(smmu
, SMMU_PTC_FLUSH_TYPE_ALL
, SMMU_PTC_FLUSH
);
170 static inline void smmu_flush_ptc(struct tegra_smmu
*smmu
, dma_addr_t dma
,
171 unsigned long offset
)
175 offset
&= ~(smmu
->mc
->soc
->atom_size
- 1);
177 if (smmu
->mc
->soc
->num_address_bits
> 32) {
178 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
179 value
= (dma
>> 32) & SMMU_PTC_FLUSH_HI_MASK
;
183 smmu_writel(smmu
, value
, SMMU_PTC_FLUSH_HI
);
186 value
= (dma
+ offset
) | SMMU_PTC_FLUSH_TYPE_ADR
;
187 smmu_writel(smmu
, value
, SMMU_PTC_FLUSH
);
190 static inline void smmu_flush_tlb(struct tegra_smmu
*smmu
)
192 smmu_writel(smmu
, SMMU_TLB_FLUSH_VA_MATCH_ALL
, SMMU_TLB_FLUSH
);
195 static inline void smmu_flush_tlb_asid(struct tegra_smmu
*smmu
,
200 value
= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_ASID(asid
) |
201 SMMU_TLB_FLUSH_VA_MATCH_ALL
;
202 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
205 static inline void smmu_flush_tlb_section(struct tegra_smmu
*smmu
,
211 value
= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_ASID(asid
) |
212 SMMU_TLB_FLUSH_VA_SECTION(iova
);
213 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
216 static inline void smmu_flush_tlb_group(struct tegra_smmu
*smmu
,
222 value
= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_ASID(asid
) |
223 SMMU_TLB_FLUSH_VA_GROUP(iova
);
224 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
227 static inline void smmu_flush(struct tegra_smmu
*smmu
)
229 smmu_readl(smmu
, SMMU_CONFIG
);
232 static int tegra_smmu_alloc_asid(struct tegra_smmu
*smmu
, unsigned int *idp
)
236 mutex_lock(&smmu
->lock
);
238 id
= find_first_zero_bit(smmu
->asids
, smmu
->soc
->num_asids
);
239 if (id
>= smmu
->soc
->num_asids
) {
240 mutex_unlock(&smmu
->lock
);
244 set_bit(id
, smmu
->asids
);
247 mutex_unlock(&smmu
->lock
);
251 static void tegra_smmu_free_asid(struct tegra_smmu
*smmu
, unsigned int id
)
253 mutex_lock(&smmu
->lock
);
254 clear_bit(id
, smmu
->asids
);
255 mutex_unlock(&smmu
->lock
);
258 static bool tegra_smmu_capable(enum iommu_cap cap
)
263 static struct iommu_domain
*tegra_smmu_domain_alloc(unsigned type
)
265 struct tegra_smmu_as
*as
;
267 if (type
!= IOMMU_DOMAIN_UNMANAGED
)
270 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
274 as
->attr
= SMMU_PD_READABLE
| SMMU_PD_WRITABLE
| SMMU_PD_NONSECURE
;
276 as
->pd
= alloc_page(GFP_KERNEL
| __GFP_DMA
| __GFP_ZERO
);
282 as
->count
= kcalloc(SMMU_NUM_PDE
, sizeof(u32
), GFP_KERNEL
);
289 as
->pts
= kcalloc(SMMU_NUM_PDE
, sizeof(*as
->pts
), GFP_KERNEL
);
298 as
->domain
.geometry
.aperture_start
= 0;
299 as
->domain
.geometry
.aperture_end
= 0xffffffff;
300 as
->domain
.geometry
.force_aperture
= true;
305 static void tegra_smmu_domain_free(struct iommu_domain
*domain
)
307 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
309 /* TODO: free page directory and page tables */
314 static const struct tegra_smmu_swgroup
*
315 tegra_smmu_find_swgroup(struct tegra_smmu
*smmu
, unsigned int swgroup
)
317 const struct tegra_smmu_swgroup
*group
= NULL
;
320 for (i
= 0; i
< smmu
->soc
->num_swgroups
; i
++) {
321 if (smmu
->soc
->swgroups
[i
].swgroup
== swgroup
) {
322 group
= &smmu
->soc
->swgroups
[i
];
330 static void tegra_smmu_enable(struct tegra_smmu
*smmu
, unsigned int swgroup
,
333 const struct tegra_smmu_swgroup
*group
;
337 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
338 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
340 if (client
->swgroup
!= swgroup
)
343 value
= smmu_readl(smmu
, client
->smmu
.reg
);
344 value
|= BIT(client
->smmu
.bit
);
345 smmu_writel(smmu
, value
, client
->smmu
.reg
);
348 group
= tegra_smmu_find_swgroup(smmu
, swgroup
);
350 value
= smmu_readl(smmu
, group
->reg
);
351 value
&= ~SMMU_ASID_MASK
;
352 value
|= SMMU_ASID_VALUE(asid
);
353 value
|= SMMU_ASID_ENABLE
;
354 smmu_writel(smmu
, value
, group
->reg
);
358 static void tegra_smmu_disable(struct tegra_smmu
*smmu
, unsigned int swgroup
,
361 const struct tegra_smmu_swgroup
*group
;
365 group
= tegra_smmu_find_swgroup(smmu
, swgroup
);
367 value
= smmu_readl(smmu
, group
->reg
);
368 value
&= ~SMMU_ASID_MASK
;
369 value
|= SMMU_ASID_VALUE(asid
);
370 value
&= ~SMMU_ASID_ENABLE
;
371 smmu_writel(smmu
, value
, group
->reg
);
374 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
375 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
377 if (client
->swgroup
!= swgroup
)
380 value
= smmu_readl(smmu
, client
->smmu
.reg
);
381 value
&= ~BIT(client
->smmu
.bit
);
382 smmu_writel(smmu
, value
, client
->smmu
.reg
);
386 static int tegra_smmu_as_prepare(struct tegra_smmu
*smmu
,
387 struct tegra_smmu_as
*as
)
392 if (as
->use_count
> 0) {
397 as
->pd_dma
= dma_map_page(smmu
->dev
, as
->pd
, 0, SMMU_SIZE_PD
,
399 if (dma_mapping_error(smmu
->dev
, as
->pd_dma
))
402 /* We can't handle 64-bit DMA addresses */
403 if (!smmu_dma_addr_valid(smmu
, as
->pd_dma
)) {
408 err
= tegra_smmu_alloc_asid(smmu
, &as
->id
);
412 smmu_flush_ptc(smmu
, as
->pd_dma
, 0);
413 smmu_flush_tlb_asid(smmu
, as
->id
);
415 smmu_writel(smmu
, as
->id
& 0x7f, SMMU_PTB_ASID
);
416 value
= SMMU_PTB_DATA_VALUE(as
->pd_dma
, as
->attr
);
417 smmu_writel(smmu
, value
, SMMU_PTB_DATA
);
426 dma_unmap_page(smmu
->dev
, as
->pd_dma
, SMMU_SIZE_PD
, DMA_TO_DEVICE
);
430 static void tegra_smmu_as_unprepare(struct tegra_smmu
*smmu
,
431 struct tegra_smmu_as
*as
)
433 if (--as
->use_count
> 0)
436 tegra_smmu_free_asid(smmu
, as
->id
);
438 dma_unmap_page(smmu
->dev
, as
->pd_dma
, SMMU_SIZE_PD
, DMA_TO_DEVICE
);
443 static int tegra_smmu_attach_dev(struct iommu_domain
*domain
,
446 struct tegra_smmu
*smmu
= dev
->archdata
.iommu
;
447 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
448 struct device_node
*np
= dev
->of_node
;
449 struct of_phandle_args args
;
450 unsigned int index
= 0;
453 while (!of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
455 unsigned int swgroup
= args
.args
[0];
457 if (args
.np
!= smmu
->dev
->of_node
) {
458 of_node_put(args
.np
);
462 of_node_put(args
.np
);
464 err
= tegra_smmu_as_prepare(smmu
, as
);
468 tegra_smmu_enable(smmu
, swgroup
, as
->id
);
478 static void tegra_smmu_detach_dev(struct iommu_domain
*domain
, struct device
*dev
)
480 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
481 struct device_node
*np
= dev
->of_node
;
482 struct tegra_smmu
*smmu
= as
->smmu
;
483 struct of_phandle_args args
;
484 unsigned int index
= 0;
486 while (!of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
488 unsigned int swgroup
= args
.args
[0];
490 if (args
.np
!= smmu
->dev
->of_node
) {
491 of_node_put(args
.np
);
495 of_node_put(args
.np
);
497 tegra_smmu_disable(smmu
, swgroup
, as
->id
);
498 tegra_smmu_as_unprepare(smmu
, as
);
503 static void tegra_smmu_set_pde(struct tegra_smmu_as
*as
, unsigned long iova
,
506 unsigned int pd_index
= iova_pd_index(iova
);
507 struct tegra_smmu
*smmu
= as
->smmu
;
508 u32
*pd
= page_address(as
->pd
);
509 unsigned long offset
= pd_index
* sizeof(*pd
);
511 /* Set the page directory entry first */
512 pd
[pd_index
] = value
;
514 /* The flush the page directory entry from caches */
515 dma_sync_single_range_for_device(smmu
->dev
, as
->pd_dma
, offset
,
516 sizeof(*pd
), DMA_TO_DEVICE
);
518 /* And flush the iommu */
519 smmu_flush_ptc(smmu
, as
->pd_dma
, offset
);
520 smmu_flush_tlb_section(smmu
, as
->id
, iova
);
524 static u32
*tegra_smmu_pte_offset(struct page
*pt_page
, unsigned long iova
)
526 u32
*pt
= page_address(pt_page
);
528 return pt
+ iova_pt_index(iova
);
531 static u32
*tegra_smmu_pte_lookup(struct tegra_smmu_as
*as
, unsigned long iova
,
534 unsigned int pd_index
= iova_pd_index(iova
);
535 struct page
*pt_page
;
538 pt_page
= as
->pts
[pd_index
];
542 pd
= page_address(as
->pd
);
543 *dmap
= smmu_pde_to_dma(pd
[pd_index
]);
545 return tegra_smmu_pte_offset(pt_page
, iova
);
548 static u32
*as_get_pte(struct tegra_smmu_as
*as
, dma_addr_t iova
,
551 unsigned int pde
= iova_pd_index(iova
);
552 struct tegra_smmu
*smmu
= as
->smmu
;
558 page
= alloc_page(GFP_KERNEL
| __GFP_DMA
| __GFP_ZERO
);
562 dma
= dma_map_page(smmu
->dev
, page
, 0, SMMU_SIZE_PT
,
564 if (dma_mapping_error(smmu
->dev
, dma
)) {
569 if (!smmu_dma_addr_valid(smmu
, dma
)) {
570 dma_unmap_page(smmu
->dev
, dma
, SMMU_SIZE_PT
,
578 tegra_smmu_set_pde(as
, iova
, SMMU_MK_PDE(dma
, SMMU_PDE_ATTR
|
583 u32
*pd
= page_address(as
->pd
);
585 *dmap
= smmu_pde_to_dma(pd
[pde
]);
588 return tegra_smmu_pte_offset(as
->pts
[pde
], iova
);
591 static void tegra_smmu_pte_get_use(struct tegra_smmu_as
*as
, unsigned long iova
)
593 unsigned int pd_index
= iova_pd_index(iova
);
595 as
->count
[pd_index
]++;
598 static void tegra_smmu_pte_put_use(struct tegra_smmu_as
*as
, unsigned long iova
)
600 unsigned int pde
= iova_pd_index(iova
);
601 struct page
*page
= as
->pts
[pde
];
604 * When no entries in this page table are used anymore, return the
605 * memory page to the system.
607 if (--as
->count
[pde
] == 0) {
608 struct tegra_smmu
*smmu
= as
->smmu
;
609 u32
*pd
= page_address(as
->pd
);
610 dma_addr_t pte_dma
= smmu_pde_to_dma(pd
[pde
]);
612 tegra_smmu_set_pde(as
, iova
, 0);
614 dma_unmap_page(smmu
->dev
, pte_dma
, SMMU_SIZE_PT
, DMA_TO_DEVICE
);
620 static void tegra_smmu_set_pte(struct tegra_smmu_as
*as
, unsigned long iova
,
621 u32
*pte
, dma_addr_t pte_dma
, u32 val
)
623 struct tegra_smmu
*smmu
= as
->smmu
;
624 unsigned long offset
= offset_in_page(pte
);
628 dma_sync_single_range_for_device(smmu
->dev
, pte_dma
, offset
,
630 smmu_flush_ptc(smmu
, pte_dma
, offset
);
631 smmu_flush_tlb_group(smmu
, as
->id
, iova
);
635 static int tegra_smmu_map(struct iommu_domain
*domain
, unsigned long iova
,
636 phys_addr_t paddr
, size_t size
, int prot
)
638 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
642 pte
= as_get_pte(as
, iova
, &pte_dma
);
646 /* If we aren't overwriting a pre-existing entry, increment use */
648 tegra_smmu_pte_get_use(as
, iova
);
650 tegra_smmu_set_pte(as
, iova
, pte
, pte_dma
,
651 __phys_to_pfn(paddr
) | SMMU_PTE_ATTR
);
656 static size_t tegra_smmu_unmap(struct iommu_domain
*domain
, unsigned long iova
,
659 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
663 pte
= tegra_smmu_pte_lookup(as
, iova
, &pte_dma
);
667 tegra_smmu_set_pte(as
, iova
, pte
, pte_dma
, 0);
668 tegra_smmu_pte_put_use(as
, iova
);
673 static phys_addr_t
tegra_smmu_iova_to_phys(struct iommu_domain
*domain
,
676 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
681 pte
= tegra_smmu_pte_lookup(as
, iova
, &pte_dma
);
685 pfn
= *pte
& as
->smmu
->pfn_mask
;
687 return PFN_PHYS(pfn
);
690 static struct tegra_smmu
*tegra_smmu_find(struct device_node
*np
)
692 struct platform_device
*pdev
;
695 pdev
= of_find_device_by_node(np
);
699 mc
= platform_get_drvdata(pdev
);
706 static int tegra_smmu_add_device(struct device
*dev
)
708 struct device_node
*np
= dev
->of_node
;
709 struct iommu_group
*group
;
710 struct of_phandle_args args
;
711 unsigned int index
= 0;
713 while (of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
715 struct tegra_smmu
*smmu
;
717 smmu
= tegra_smmu_find(args
.np
);
720 * Only a single IOMMU master interface is currently
721 * supported by the Linux kernel, so abort after the
724 dev
->archdata
.iommu
= smmu
;
726 iommu_device_link(&smmu
->iommu
, dev
);
734 group
= iommu_group_get_for_dev(dev
);
736 return PTR_ERR(group
);
738 iommu_group_put(group
);
743 static void tegra_smmu_remove_device(struct device
*dev
)
745 struct tegra_smmu
*smmu
= dev
->archdata
.iommu
;
748 iommu_device_unlink(&smmu
->iommu
, dev
);
750 dev
->archdata
.iommu
= NULL
;
751 iommu_group_remove_device(dev
);
754 static const struct iommu_ops tegra_smmu_ops
= {
755 .capable
= tegra_smmu_capable
,
756 .domain_alloc
= tegra_smmu_domain_alloc
,
757 .domain_free
= tegra_smmu_domain_free
,
758 .attach_dev
= tegra_smmu_attach_dev
,
759 .detach_dev
= tegra_smmu_detach_dev
,
760 .add_device
= tegra_smmu_add_device
,
761 .remove_device
= tegra_smmu_remove_device
,
762 .device_group
= generic_device_group
,
763 .map
= tegra_smmu_map
,
764 .unmap
= tegra_smmu_unmap
,
765 .map_sg
= default_iommu_map_sg
,
766 .iova_to_phys
= tegra_smmu_iova_to_phys
,
768 .pgsize_bitmap
= SZ_4K
,
771 static void tegra_smmu_ahb_enable(void)
773 static const struct of_device_id ahb_match
[] = {
774 { .compatible
= "nvidia,tegra30-ahb", },
777 struct device_node
*ahb
;
779 ahb
= of_find_matching_node(NULL
, ahb_match
);
781 tegra_ahb_enable_smmu(ahb
);
786 static int tegra_smmu_swgroups_show(struct seq_file
*s
, void *data
)
788 struct tegra_smmu
*smmu
= s
->private;
792 seq_printf(s
, "swgroup enabled ASID\n");
793 seq_printf(s
, "------------------------\n");
795 for (i
= 0; i
< smmu
->soc
->num_swgroups
; i
++) {
796 const struct tegra_smmu_swgroup
*group
= &smmu
->soc
->swgroups
[i
];
800 value
= smmu_readl(smmu
, group
->reg
);
802 if (value
& SMMU_ASID_ENABLE
)
807 asid
= value
& SMMU_ASID_MASK
;
809 seq_printf(s
, "%-9s %-7s %#04x\n", group
->name
, status
,
816 static int tegra_smmu_swgroups_open(struct inode
*inode
, struct file
*file
)
818 return single_open(file
, tegra_smmu_swgroups_show
, inode
->i_private
);
821 static const struct file_operations tegra_smmu_swgroups_fops
= {
822 .open
= tegra_smmu_swgroups_open
,
825 .release
= single_release
,
828 static int tegra_smmu_clients_show(struct seq_file
*s
, void *data
)
830 struct tegra_smmu
*smmu
= s
->private;
834 seq_printf(s
, "client enabled\n");
835 seq_printf(s
, "--------------------\n");
837 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
838 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
841 value
= smmu_readl(smmu
, client
->smmu
.reg
);
843 if (value
& BIT(client
->smmu
.bit
))
848 seq_printf(s
, "%-12s %s\n", client
->name
, status
);
854 static int tegra_smmu_clients_open(struct inode
*inode
, struct file
*file
)
856 return single_open(file
, tegra_smmu_clients_show
, inode
->i_private
);
859 static const struct file_operations tegra_smmu_clients_fops
= {
860 .open
= tegra_smmu_clients_open
,
863 .release
= single_release
,
866 static void tegra_smmu_debugfs_init(struct tegra_smmu
*smmu
)
868 smmu
->debugfs
= debugfs_create_dir("smmu", NULL
);
872 debugfs_create_file("swgroups", S_IRUGO
, smmu
->debugfs
, smmu
,
873 &tegra_smmu_swgroups_fops
);
874 debugfs_create_file("clients", S_IRUGO
, smmu
->debugfs
, smmu
,
875 &tegra_smmu_clients_fops
);
878 static void tegra_smmu_debugfs_exit(struct tegra_smmu
*smmu
)
880 debugfs_remove_recursive(smmu
->debugfs
);
883 struct tegra_smmu
*tegra_smmu_probe(struct device
*dev
,
884 const struct tegra_smmu_soc
*soc
,
887 struct tegra_smmu
*smmu
;
892 /* This can happen on Tegra20 which doesn't have an SMMU */
896 smmu
= devm_kzalloc(dev
, sizeof(*smmu
), GFP_KERNEL
);
898 return ERR_PTR(-ENOMEM
);
901 * This is a bit of a hack. Ideally we'd want to simply return this
902 * value. However the IOMMU registration process will attempt to add
903 * all devices to the IOMMU when bus_set_iommu() is called. In order
904 * not to rely on global variables to track the IOMMU instance, we
905 * set it here so that it can be looked up from the .add_device()
906 * callback via the IOMMU device's .drvdata field.
910 size
= BITS_TO_LONGS(soc
->num_asids
) * sizeof(long);
912 smmu
->asids
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
914 return ERR_PTR(-ENOMEM
);
916 mutex_init(&smmu
->lock
);
918 smmu
->regs
= mc
->regs
;
923 smmu
->pfn_mask
= BIT_MASK(mc
->soc
->num_address_bits
- PAGE_SHIFT
) - 1;
924 dev_dbg(dev
, "address bits: %u, PFN mask: %#lx\n",
925 mc
->soc
->num_address_bits
, smmu
->pfn_mask
);
926 smmu
->tlb_mask
= (smmu
->soc
->num_tlb_lines
<< 1) - 1;
927 dev_dbg(dev
, "TLB lines: %u, mask: %#lx\n", smmu
->soc
->num_tlb_lines
,
930 value
= SMMU_PTC_CONFIG_ENABLE
| SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
932 if (soc
->supports_request_limit
)
933 value
|= SMMU_PTC_CONFIG_REQ_LIMIT(8);
935 smmu_writel(smmu
, value
, SMMU_PTC_CONFIG
);
937 value
= SMMU_TLB_CONFIG_HIT_UNDER_MISS
|
938 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu
);
940 if (soc
->supports_round_robin_arbitration
)
941 value
|= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION
;
943 smmu_writel(smmu
, value
, SMMU_TLB_CONFIG
);
945 smmu_flush_ptc_all(smmu
);
946 smmu_flush_tlb(smmu
);
947 smmu_writel(smmu
, SMMU_CONFIG_ENABLE
, SMMU_CONFIG
);
950 tegra_smmu_ahb_enable();
952 err
= iommu_device_sysfs_add(&smmu
->iommu
, dev
, NULL
, dev_name(dev
));
956 iommu_device_set_ops(&smmu
->iommu
, &tegra_smmu_ops
);
958 err
= iommu_device_register(&smmu
->iommu
);
960 iommu_device_sysfs_remove(&smmu
->iommu
);
964 err
= bus_set_iommu(&platform_bus_type
, &tegra_smmu_ops
);
966 iommu_device_unregister(&smmu
->iommu
);
967 iommu_device_sysfs_remove(&smmu
->iommu
);
971 if (IS_ENABLED(CONFIG_DEBUG_FS
))
972 tegra_smmu_debugfs_init(smmu
);
977 void tegra_smmu_remove(struct tegra_smmu
*smmu
)
979 iommu_device_unregister(&smmu
->iommu
);
980 iommu_device_sysfs_remove(&smmu
->iommu
);
982 if (IS_ENABLED(CONFIG_DEBUG_FS
))
983 tegra_smmu_debugfs_exit(smmu
);