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>
23 struct tegra_smmu_group
{
24 struct list_head list
;
25 const struct tegra_smmu_group_soc
*soc
;
26 struct iommu_group
*group
;
34 const struct tegra_smmu_soc
*soc
;
36 struct list_head groups
;
38 unsigned long pfn_mask
;
39 unsigned long tlb_mask
;
44 struct list_head list
;
46 struct dentry
*debugfs
;
48 struct iommu_device iommu
; /* IOMMU Core code handle */
51 struct tegra_smmu_as
{
52 struct iommu_domain domain
;
53 struct tegra_smmu
*smmu
;
54 unsigned int use_count
;
63 static struct tegra_smmu_as
*to_smmu_as(struct iommu_domain
*dom
)
65 return container_of(dom
, struct tegra_smmu_as
, domain
);
68 static inline void smmu_writel(struct tegra_smmu
*smmu
, u32 value
,
71 writel(value
, smmu
->regs
+ offset
);
74 static inline u32
smmu_readl(struct tegra_smmu
*smmu
, unsigned long offset
)
76 return readl(smmu
->regs
+ offset
);
79 #define SMMU_CONFIG 0x010
80 #define SMMU_CONFIG_ENABLE (1 << 0)
82 #define SMMU_TLB_CONFIG 0x14
83 #define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
84 #define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
85 #define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
86 ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
88 #define SMMU_PTC_CONFIG 0x18
89 #define SMMU_PTC_CONFIG_ENABLE (1 << 29)
90 #define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
91 #define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
93 #define SMMU_PTB_ASID 0x01c
94 #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
96 #define SMMU_PTB_DATA 0x020
97 #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
99 #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
101 #define SMMU_TLB_FLUSH 0x030
102 #define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
103 #define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
104 #define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
105 #define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
106 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
107 #define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
108 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
109 #define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
111 #define SMMU_PTC_FLUSH 0x034
112 #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
113 #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
115 #define SMMU_PTC_FLUSH_HI 0x9b8
116 #define SMMU_PTC_FLUSH_HI_MASK 0x3
118 /* per-SWGROUP SMMU_*_ASID register */
119 #define SMMU_ASID_ENABLE (1 << 31)
120 #define SMMU_ASID_MASK 0x7f
121 #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
123 /* page table definitions */
124 #define SMMU_NUM_PDE 1024
125 #define SMMU_NUM_PTE 1024
127 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
128 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
130 #define SMMU_PDE_SHIFT 22
131 #define SMMU_PTE_SHIFT 12
133 #define SMMU_PD_READABLE (1 << 31)
134 #define SMMU_PD_WRITABLE (1 << 30)
135 #define SMMU_PD_NONSECURE (1 << 29)
137 #define SMMU_PDE_READABLE (1 << 31)
138 #define SMMU_PDE_WRITABLE (1 << 30)
139 #define SMMU_PDE_NONSECURE (1 << 29)
140 #define SMMU_PDE_NEXT (1 << 28)
142 #define SMMU_PTE_READABLE (1 << 31)
143 #define SMMU_PTE_WRITABLE (1 << 30)
144 #define SMMU_PTE_NONSECURE (1 << 29)
146 #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
148 #define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
151 static unsigned int iova_pd_index(unsigned long iova
)
153 return (iova
>> SMMU_PDE_SHIFT
) & (SMMU_NUM_PDE
- 1);
156 static unsigned int iova_pt_index(unsigned long iova
)
158 return (iova
>> SMMU_PTE_SHIFT
) & (SMMU_NUM_PTE
- 1);
161 static bool smmu_dma_addr_valid(struct tegra_smmu
*smmu
, dma_addr_t addr
)
164 return (addr
& smmu
->pfn_mask
) == addr
;
167 static dma_addr_t
smmu_pde_to_dma(struct tegra_smmu
*smmu
, u32 pde
)
169 return (dma_addr_t
)(pde
& smmu
->pfn_mask
) << 12;
172 static void smmu_flush_ptc_all(struct tegra_smmu
*smmu
)
174 smmu_writel(smmu
, SMMU_PTC_FLUSH_TYPE_ALL
, SMMU_PTC_FLUSH
);
177 static inline void smmu_flush_ptc(struct tegra_smmu
*smmu
, dma_addr_t dma
,
178 unsigned long offset
)
182 offset
&= ~(smmu
->mc
->soc
->atom_size
- 1);
184 if (smmu
->mc
->soc
->num_address_bits
> 32) {
185 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
186 value
= (dma
>> 32) & SMMU_PTC_FLUSH_HI_MASK
;
190 smmu_writel(smmu
, value
, SMMU_PTC_FLUSH_HI
);
193 value
= (dma
+ offset
) | SMMU_PTC_FLUSH_TYPE_ADR
;
194 smmu_writel(smmu
, value
, SMMU_PTC_FLUSH
);
197 static inline void smmu_flush_tlb(struct tegra_smmu
*smmu
)
199 smmu_writel(smmu
, SMMU_TLB_FLUSH_VA_MATCH_ALL
, SMMU_TLB_FLUSH
);
202 static inline void smmu_flush_tlb_asid(struct tegra_smmu
*smmu
,
207 if (smmu
->soc
->num_asids
== 4)
208 value
= (asid
& 0x3) << 29;
210 value
= (asid
& 0x7f) << 24;
212 value
|= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_VA_MATCH_ALL
;
213 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
216 static inline void smmu_flush_tlb_section(struct tegra_smmu
*smmu
,
222 if (smmu
->soc
->num_asids
== 4)
223 value
= (asid
& 0x3) << 29;
225 value
= (asid
& 0x7f) << 24;
227 value
|= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_VA_SECTION(iova
);
228 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
231 static inline void smmu_flush_tlb_group(struct tegra_smmu
*smmu
,
237 if (smmu
->soc
->num_asids
== 4)
238 value
= (asid
& 0x3) << 29;
240 value
= (asid
& 0x7f) << 24;
242 value
|= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_VA_GROUP(iova
);
243 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
246 static inline void smmu_flush(struct tegra_smmu
*smmu
)
248 smmu_readl(smmu
, SMMU_CONFIG
);
251 static int tegra_smmu_alloc_asid(struct tegra_smmu
*smmu
, unsigned int *idp
)
255 mutex_lock(&smmu
->lock
);
257 id
= find_first_zero_bit(smmu
->asids
, smmu
->soc
->num_asids
);
258 if (id
>= smmu
->soc
->num_asids
) {
259 mutex_unlock(&smmu
->lock
);
263 set_bit(id
, smmu
->asids
);
266 mutex_unlock(&smmu
->lock
);
270 static void tegra_smmu_free_asid(struct tegra_smmu
*smmu
, unsigned int id
)
272 mutex_lock(&smmu
->lock
);
273 clear_bit(id
, smmu
->asids
);
274 mutex_unlock(&smmu
->lock
);
277 static bool tegra_smmu_capable(enum iommu_cap cap
)
282 static struct iommu_domain
*tegra_smmu_domain_alloc(unsigned type
)
284 struct tegra_smmu_as
*as
;
286 if (type
!= IOMMU_DOMAIN_UNMANAGED
)
289 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
293 as
->attr
= SMMU_PD_READABLE
| SMMU_PD_WRITABLE
| SMMU_PD_NONSECURE
;
295 as
->pd
= alloc_page(GFP_KERNEL
| __GFP_DMA
| __GFP_ZERO
);
301 as
->count
= kcalloc(SMMU_NUM_PDE
, sizeof(u32
), GFP_KERNEL
);
308 as
->pts
= kcalloc(SMMU_NUM_PDE
, sizeof(*as
->pts
), GFP_KERNEL
);
317 as
->domain
.geometry
.aperture_start
= 0;
318 as
->domain
.geometry
.aperture_end
= 0xffffffff;
319 as
->domain
.geometry
.force_aperture
= true;
324 static void tegra_smmu_domain_free(struct iommu_domain
*domain
)
326 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
328 /* TODO: free page directory and page tables */
333 static const struct tegra_smmu_swgroup
*
334 tegra_smmu_find_swgroup(struct tegra_smmu
*smmu
, unsigned int swgroup
)
336 const struct tegra_smmu_swgroup
*group
= NULL
;
339 for (i
= 0; i
< smmu
->soc
->num_swgroups
; i
++) {
340 if (smmu
->soc
->swgroups
[i
].swgroup
== swgroup
) {
341 group
= &smmu
->soc
->swgroups
[i
];
349 static void tegra_smmu_enable(struct tegra_smmu
*smmu
, unsigned int swgroup
,
352 const struct tegra_smmu_swgroup
*group
;
356 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
357 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
359 if (client
->swgroup
!= swgroup
)
362 value
= smmu_readl(smmu
, client
->smmu
.reg
);
363 value
|= BIT(client
->smmu
.bit
);
364 smmu_writel(smmu
, value
, client
->smmu
.reg
);
367 group
= tegra_smmu_find_swgroup(smmu
, swgroup
);
369 value
= smmu_readl(smmu
, group
->reg
);
370 value
&= ~SMMU_ASID_MASK
;
371 value
|= SMMU_ASID_VALUE(asid
);
372 value
|= SMMU_ASID_ENABLE
;
373 smmu_writel(smmu
, value
, group
->reg
);
377 static void tegra_smmu_disable(struct tegra_smmu
*smmu
, unsigned int swgroup
,
380 const struct tegra_smmu_swgroup
*group
;
384 group
= tegra_smmu_find_swgroup(smmu
, swgroup
);
386 value
= smmu_readl(smmu
, group
->reg
);
387 value
&= ~SMMU_ASID_MASK
;
388 value
|= SMMU_ASID_VALUE(asid
);
389 value
&= ~SMMU_ASID_ENABLE
;
390 smmu_writel(smmu
, value
, group
->reg
);
393 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
394 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
396 if (client
->swgroup
!= swgroup
)
399 value
= smmu_readl(smmu
, client
->smmu
.reg
);
400 value
&= ~BIT(client
->smmu
.bit
);
401 smmu_writel(smmu
, value
, client
->smmu
.reg
);
405 static int tegra_smmu_as_prepare(struct tegra_smmu
*smmu
,
406 struct tegra_smmu_as
*as
)
411 if (as
->use_count
> 0) {
416 as
->pd_dma
= dma_map_page(smmu
->dev
, as
->pd
, 0, SMMU_SIZE_PD
,
418 if (dma_mapping_error(smmu
->dev
, as
->pd_dma
))
421 /* We can't handle 64-bit DMA addresses */
422 if (!smmu_dma_addr_valid(smmu
, as
->pd_dma
)) {
427 err
= tegra_smmu_alloc_asid(smmu
, &as
->id
);
431 smmu_flush_ptc(smmu
, as
->pd_dma
, 0);
432 smmu_flush_tlb_asid(smmu
, as
->id
);
434 smmu_writel(smmu
, as
->id
& 0x7f, SMMU_PTB_ASID
);
435 value
= SMMU_PTB_DATA_VALUE(as
->pd_dma
, as
->attr
);
436 smmu_writel(smmu
, value
, SMMU_PTB_DATA
);
445 dma_unmap_page(smmu
->dev
, as
->pd_dma
, SMMU_SIZE_PD
, DMA_TO_DEVICE
);
449 static void tegra_smmu_as_unprepare(struct tegra_smmu
*smmu
,
450 struct tegra_smmu_as
*as
)
452 if (--as
->use_count
> 0)
455 tegra_smmu_free_asid(smmu
, as
->id
);
457 dma_unmap_page(smmu
->dev
, as
->pd_dma
, SMMU_SIZE_PD
, DMA_TO_DEVICE
);
462 static int tegra_smmu_attach_dev(struct iommu_domain
*domain
,
465 struct tegra_smmu
*smmu
= dev
->archdata
.iommu
;
466 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
467 struct device_node
*np
= dev
->of_node
;
468 struct of_phandle_args args
;
469 unsigned int index
= 0;
472 while (!of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
474 unsigned int swgroup
= args
.args
[0];
476 if (args
.np
!= smmu
->dev
->of_node
) {
477 of_node_put(args
.np
);
481 of_node_put(args
.np
);
483 err
= tegra_smmu_as_prepare(smmu
, as
);
487 tegra_smmu_enable(smmu
, swgroup
, as
->id
);
497 static void tegra_smmu_detach_dev(struct iommu_domain
*domain
, struct device
*dev
)
499 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
500 struct device_node
*np
= dev
->of_node
;
501 struct tegra_smmu
*smmu
= as
->smmu
;
502 struct of_phandle_args args
;
503 unsigned int index
= 0;
505 while (!of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
507 unsigned int swgroup
= args
.args
[0];
509 if (args
.np
!= smmu
->dev
->of_node
) {
510 of_node_put(args
.np
);
514 of_node_put(args
.np
);
516 tegra_smmu_disable(smmu
, swgroup
, as
->id
);
517 tegra_smmu_as_unprepare(smmu
, as
);
522 static void tegra_smmu_set_pde(struct tegra_smmu_as
*as
, unsigned long iova
,
525 unsigned int pd_index
= iova_pd_index(iova
);
526 struct tegra_smmu
*smmu
= as
->smmu
;
527 u32
*pd
= page_address(as
->pd
);
528 unsigned long offset
= pd_index
* sizeof(*pd
);
530 /* Set the page directory entry first */
531 pd
[pd_index
] = value
;
533 /* The flush the page directory entry from caches */
534 dma_sync_single_range_for_device(smmu
->dev
, as
->pd_dma
, offset
,
535 sizeof(*pd
), DMA_TO_DEVICE
);
537 /* And flush the iommu */
538 smmu_flush_ptc(smmu
, as
->pd_dma
, offset
);
539 smmu_flush_tlb_section(smmu
, as
->id
, iova
);
543 static u32
*tegra_smmu_pte_offset(struct page
*pt_page
, unsigned long iova
)
545 u32
*pt
= page_address(pt_page
);
547 return pt
+ iova_pt_index(iova
);
550 static u32
*tegra_smmu_pte_lookup(struct tegra_smmu_as
*as
, unsigned long iova
,
553 unsigned int pd_index
= iova_pd_index(iova
);
554 struct tegra_smmu
*smmu
= as
->smmu
;
555 struct page
*pt_page
;
558 pt_page
= as
->pts
[pd_index
];
562 pd
= page_address(as
->pd
);
563 *dmap
= smmu_pde_to_dma(smmu
, pd
[pd_index
]);
565 return tegra_smmu_pte_offset(pt_page
, iova
);
568 static u32
*as_get_pte(struct tegra_smmu_as
*as
, dma_addr_t iova
,
571 unsigned int pde
= iova_pd_index(iova
);
572 struct tegra_smmu
*smmu
= as
->smmu
;
578 page
= alloc_page(GFP_KERNEL
| __GFP_DMA
| __GFP_ZERO
);
582 dma
= dma_map_page(smmu
->dev
, page
, 0, SMMU_SIZE_PT
,
584 if (dma_mapping_error(smmu
->dev
, dma
)) {
589 if (!smmu_dma_addr_valid(smmu
, dma
)) {
590 dma_unmap_page(smmu
->dev
, dma
, SMMU_SIZE_PT
,
598 tegra_smmu_set_pde(as
, iova
, SMMU_MK_PDE(dma
, SMMU_PDE_ATTR
|
603 u32
*pd
= page_address(as
->pd
);
605 *dmap
= smmu_pde_to_dma(smmu
, pd
[pde
]);
608 return tegra_smmu_pte_offset(as
->pts
[pde
], iova
);
611 static void tegra_smmu_pte_get_use(struct tegra_smmu_as
*as
, unsigned long iova
)
613 unsigned int pd_index
= iova_pd_index(iova
);
615 as
->count
[pd_index
]++;
618 static void tegra_smmu_pte_put_use(struct tegra_smmu_as
*as
, unsigned long iova
)
620 unsigned int pde
= iova_pd_index(iova
);
621 struct page
*page
= as
->pts
[pde
];
624 * When no entries in this page table are used anymore, return the
625 * memory page to the system.
627 if (--as
->count
[pde
] == 0) {
628 struct tegra_smmu
*smmu
= as
->smmu
;
629 u32
*pd
= page_address(as
->pd
);
630 dma_addr_t pte_dma
= smmu_pde_to_dma(smmu
, pd
[pde
]);
632 tegra_smmu_set_pde(as
, iova
, 0);
634 dma_unmap_page(smmu
->dev
, pte_dma
, SMMU_SIZE_PT
, DMA_TO_DEVICE
);
640 static void tegra_smmu_set_pte(struct tegra_smmu_as
*as
, unsigned long iova
,
641 u32
*pte
, dma_addr_t pte_dma
, u32 val
)
643 struct tegra_smmu
*smmu
= as
->smmu
;
644 unsigned long offset
= offset_in_page(pte
);
648 dma_sync_single_range_for_device(smmu
->dev
, pte_dma
, offset
,
650 smmu_flush_ptc(smmu
, pte_dma
, offset
);
651 smmu_flush_tlb_group(smmu
, as
->id
, iova
);
655 static int tegra_smmu_map(struct iommu_domain
*domain
, unsigned long iova
,
656 phys_addr_t paddr
, size_t size
, int prot
)
658 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
662 pte
= as_get_pte(as
, iova
, &pte_dma
);
666 /* If we aren't overwriting a pre-existing entry, increment use */
668 tegra_smmu_pte_get_use(as
, iova
);
670 tegra_smmu_set_pte(as
, iova
, pte
, pte_dma
,
671 __phys_to_pfn(paddr
) | SMMU_PTE_ATTR
);
676 static size_t tegra_smmu_unmap(struct iommu_domain
*domain
, unsigned long iova
,
679 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
683 pte
= tegra_smmu_pte_lookup(as
, iova
, &pte_dma
);
687 tegra_smmu_set_pte(as
, iova
, pte
, pte_dma
, 0);
688 tegra_smmu_pte_put_use(as
, iova
);
693 static phys_addr_t
tegra_smmu_iova_to_phys(struct iommu_domain
*domain
,
696 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
701 pte
= tegra_smmu_pte_lookup(as
, iova
, &pte_dma
);
705 pfn
= *pte
& as
->smmu
->pfn_mask
;
707 return PFN_PHYS(pfn
);
710 static struct tegra_smmu
*tegra_smmu_find(struct device_node
*np
)
712 struct platform_device
*pdev
;
715 pdev
= of_find_device_by_node(np
);
719 mc
= platform_get_drvdata(pdev
);
726 static int tegra_smmu_configure(struct tegra_smmu
*smmu
, struct device
*dev
,
727 struct of_phandle_args
*args
)
729 const struct iommu_ops
*ops
= smmu
->iommu
.ops
;
732 err
= iommu_fwspec_init(dev
, &dev
->of_node
->fwnode
, ops
);
734 dev_err(dev
, "failed to initialize fwspec: %d\n", err
);
738 err
= ops
->of_xlate(dev
, args
);
740 dev_err(dev
, "failed to parse SW group ID: %d\n", err
);
741 iommu_fwspec_free(dev
);
748 static int tegra_smmu_add_device(struct device
*dev
)
750 struct device_node
*np
= dev
->of_node
;
751 struct tegra_smmu
*smmu
= NULL
;
752 struct iommu_group
*group
;
753 struct of_phandle_args args
;
754 unsigned int index
= 0;
757 while (of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
759 smmu
= tegra_smmu_find(args
.np
);
761 err
= tegra_smmu_configure(smmu
, dev
, &args
);
762 of_node_put(args
.np
);
768 * Only a single IOMMU master interface is currently
769 * supported by the Linux kernel, so abort after the
772 dev
->archdata
.iommu
= smmu
;
774 iommu_device_link(&smmu
->iommu
, dev
);
779 of_node_put(args
.np
);
786 group
= iommu_group_get_for_dev(dev
);
788 return PTR_ERR(group
);
790 iommu_group_put(group
);
795 static void tegra_smmu_remove_device(struct device
*dev
)
797 struct tegra_smmu
*smmu
= dev
->archdata
.iommu
;
800 iommu_device_unlink(&smmu
->iommu
, dev
);
802 dev
->archdata
.iommu
= NULL
;
803 iommu_group_remove_device(dev
);
806 static const struct tegra_smmu_group_soc
*
807 tegra_smmu_find_group(struct tegra_smmu
*smmu
, unsigned int swgroup
)
811 for (i
= 0; i
< smmu
->soc
->num_groups
; i
++)
812 for (j
= 0; j
< smmu
->soc
->groups
[i
].num_swgroups
; j
++)
813 if (smmu
->soc
->groups
[i
].swgroups
[j
] == swgroup
)
814 return &smmu
->soc
->groups
[i
];
819 static struct iommu_group
*tegra_smmu_group_get(struct tegra_smmu
*smmu
,
820 unsigned int swgroup
)
822 const struct tegra_smmu_group_soc
*soc
;
823 struct tegra_smmu_group
*group
;
825 soc
= tegra_smmu_find_group(smmu
, swgroup
);
829 mutex_lock(&smmu
->lock
);
831 list_for_each_entry(group
, &smmu
->groups
, list
)
832 if (group
->soc
== soc
) {
833 mutex_unlock(&smmu
->lock
);
837 group
= devm_kzalloc(smmu
->dev
, sizeof(*group
), GFP_KERNEL
);
839 mutex_unlock(&smmu
->lock
);
843 INIT_LIST_HEAD(&group
->list
);
846 group
->group
= iommu_group_alloc();
847 if (IS_ERR(group
->group
)) {
848 devm_kfree(smmu
->dev
, group
);
849 mutex_unlock(&smmu
->lock
);
853 list_add_tail(&group
->list
, &smmu
->groups
);
854 mutex_unlock(&smmu
->lock
);
859 static struct iommu_group
*tegra_smmu_device_group(struct device
*dev
)
861 struct iommu_fwspec
*fwspec
= dev
->iommu_fwspec
;
862 struct tegra_smmu
*smmu
= dev
->archdata
.iommu
;
863 struct iommu_group
*group
;
865 group
= tegra_smmu_group_get(smmu
, fwspec
->ids
[0]);
867 group
= generic_device_group(dev
);
872 static int tegra_smmu_of_xlate(struct device
*dev
,
873 struct of_phandle_args
*args
)
875 u32 id
= args
->args
[0];
877 return iommu_fwspec_add_ids(dev
, &id
, 1);
880 static const struct iommu_ops tegra_smmu_ops
= {
881 .capable
= tegra_smmu_capable
,
882 .domain_alloc
= tegra_smmu_domain_alloc
,
883 .domain_free
= tegra_smmu_domain_free
,
884 .attach_dev
= tegra_smmu_attach_dev
,
885 .detach_dev
= tegra_smmu_detach_dev
,
886 .add_device
= tegra_smmu_add_device
,
887 .remove_device
= tegra_smmu_remove_device
,
888 .device_group
= tegra_smmu_device_group
,
889 .map
= tegra_smmu_map
,
890 .unmap
= tegra_smmu_unmap
,
891 .iova_to_phys
= tegra_smmu_iova_to_phys
,
892 .of_xlate
= tegra_smmu_of_xlate
,
893 .pgsize_bitmap
= SZ_4K
,
896 static void tegra_smmu_ahb_enable(void)
898 static const struct of_device_id ahb_match
[] = {
899 { .compatible
= "nvidia,tegra30-ahb", },
902 struct device_node
*ahb
;
904 ahb
= of_find_matching_node(NULL
, ahb_match
);
906 tegra_ahb_enable_smmu(ahb
);
911 static int tegra_smmu_swgroups_show(struct seq_file
*s
, void *data
)
913 struct tegra_smmu
*smmu
= s
->private;
917 seq_printf(s
, "swgroup enabled ASID\n");
918 seq_printf(s
, "------------------------\n");
920 for (i
= 0; i
< smmu
->soc
->num_swgroups
; i
++) {
921 const struct tegra_smmu_swgroup
*group
= &smmu
->soc
->swgroups
[i
];
925 value
= smmu_readl(smmu
, group
->reg
);
927 if (value
& SMMU_ASID_ENABLE
)
932 asid
= value
& SMMU_ASID_MASK
;
934 seq_printf(s
, "%-9s %-7s %#04x\n", group
->name
, status
,
941 static int tegra_smmu_swgroups_open(struct inode
*inode
, struct file
*file
)
943 return single_open(file
, tegra_smmu_swgroups_show
, inode
->i_private
);
946 static const struct file_operations tegra_smmu_swgroups_fops
= {
947 .open
= tegra_smmu_swgroups_open
,
950 .release
= single_release
,
953 static int tegra_smmu_clients_show(struct seq_file
*s
, void *data
)
955 struct tegra_smmu
*smmu
= s
->private;
959 seq_printf(s
, "client enabled\n");
960 seq_printf(s
, "--------------------\n");
962 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
963 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
966 value
= smmu_readl(smmu
, client
->smmu
.reg
);
968 if (value
& BIT(client
->smmu
.bit
))
973 seq_printf(s
, "%-12s %s\n", client
->name
, status
);
979 static int tegra_smmu_clients_open(struct inode
*inode
, struct file
*file
)
981 return single_open(file
, tegra_smmu_clients_show
, inode
->i_private
);
984 static const struct file_operations tegra_smmu_clients_fops
= {
985 .open
= tegra_smmu_clients_open
,
988 .release
= single_release
,
991 static void tegra_smmu_debugfs_init(struct tegra_smmu
*smmu
)
993 smmu
->debugfs
= debugfs_create_dir("smmu", NULL
);
997 debugfs_create_file("swgroups", S_IRUGO
, smmu
->debugfs
, smmu
,
998 &tegra_smmu_swgroups_fops
);
999 debugfs_create_file("clients", S_IRUGO
, smmu
->debugfs
, smmu
,
1000 &tegra_smmu_clients_fops
);
1003 static void tegra_smmu_debugfs_exit(struct tegra_smmu
*smmu
)
1005 debugfs_remove_recursive(smmu
->debugfs
);
1008 struct tegra_smmu
*tegra_smmu_probe(struct device
*dev
,
1009 const struct tegra_smmu_soc
*soc
,
1010 struct tegra_mc
*mc
)
1012 struct tegra_smmu
*smmu
;
1017 /* This can happen on Tegra20 which doesn't have an SMMU */
1021 smmu
= devm_kzalloc(dev
, sizeof(*smmu
), GFP_KERNEL
);
1023 return ERR_PTR(-ENOMEM
);
1026 * This is a bit of a hack. Ideally we'd want to simply return this
1027 * value. However the IOMMU registration process will attempt to add
1028 * all devices to the IOMMU when bus_set_iommu() is called. In order
1029 * not to rely on global variables to track the IOMMU instance, we
1030 * set it here so that it can be looked up from the .add_device()
1031 * callback via the IOMMU device's .drvdata field.
1035 size
= BITS_TO_LONGS(soc
->num_asids
) * sizeof(long);
1037 smmu
->asids
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
1039 return ERR_PTR(-ENOMEM
);
1041 INIT_LIST_HEAD(&smmu
->groups
);
1042 mutex_init(&smmu
->lock
);
1044 smmu
->regs
= mc
->regs
;
1049 smmu
->pfn_mask
= BIT_MASK(mc
->soc
->num_address_bits
- PAGE_SHIFT
) - 1;
1050 dev_dbg(dev
, "address bits: %u, PFN mask: %#lx\n",
1051 mc
->soc
->num_address_bits
, smmu
->pfn_mask
);
1052 smmu
->tlb_mask
= (smmu
->soc
->num_tlb_lines
<< 1) - 1;
1053 dev_dbg(dev
, "TLB lines: %u, mask: %#lx\n", smmu
->soc
->num_tlb_lines
,
1056 value
= SMMU_PTC_CONFIG_ENABLE
| SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1058 if (soc
->supports_request_limit
)
1059 value
|= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1061 smmu_writel(smmu
, value
, SMMU_PTC_CONFIG
);
1063 value
= SMMU_TLB_CONFIG_HIT_UNDER_MISS
|
1064 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu
);
1066 if (soc
->supports_round_robin_arbitration
)
1067 value
|= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION
;
1069 smmu_writel(smmu
, value
, SMMU_TLB_CONFIG
);
1071 smmu_flush_ptc_all(smmu
);
1072 smmu_flush_tlb(smmu
);
1073 smmu_writel(smmu
, SMMU_CONFIG_ENABLE
, SMMU_CONFIG
);
1076 tegra_smmu_ahb_enable();
1078 err
= iommu_device_sysfs_add(&smmu
->iommu
, dev
, NULL
, dev_name(dev
));
1080 return ERR_PTR(err
);
1082 iommu_device_set_ops(&smmu
->iommu
, &tegra_smmu_ops
);
1083 iommu_device_set_fwnode(&smmu
->iommu
, dev
->fwnode
);
1085 err
= iommu_device_register(&smmu
->iommu
);
1087 iommu_device_sysfs_remove(&smmu
->iommu
);
1088 return ERR_PTR(err
);
1091 err
= bus_set_iommu(&platform_bus_type
, &tegra_smmu_ops
);
1093 iommu_device_unregister(&smmu
->iommu
);
1094 iommu_device_sysfs_remove(&smmu
->iommu
);
1095 return ERR_PTR(err
);
1098 if (IS_ENABLED(CONFIG_DEBUG_FS
))
1099 tegra_smmu_debugfs_init(smmu
);
1104 void tegra_smmu_remove(struct tegra_smmu
*smmu
)
1106 iommu_device_unregister(&smmu
->iommu
);
1107 iommu_device_sysfs_remove(&smmu
->iommu
);
1109 if (IS_ENABLED(CONFIG_DEBUG_FS
))
1110 tegra_smmu_debugfs_exit(smmu
);