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_ASID(x) (((x) & 0x7f) << 24)
106 #define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
107 SMMU_TLB_FLUSH_VA_MATCH_SECTION)
108 #define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
109 SMMU_TLB_FLUSH_VA_MATCH_GROUP)
110 #define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
112 #define SMMU_PTC_FLUSH 0x034
113 #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
114 #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
116 #define SMMU_PTC_FLUSH_HI 0x9b8
117 #define SMMU_PTC_FLUSH_HI_MASK 0x3
119 /* per-SWGROUP SMMU_*_ASID register */
120 #define SMMU_ASID_ENABLE (1 << 31)
121 #define SMMU_ASID_MASK 0x7f
122 #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
124 /* page table definitions */
125 #define SMMU_NUM_PDE 1024
126 #define SMMU_NUM_PTE 1024
128 #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
129 #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
131 #define SMMU_PDE_SHIFT 22
132 #define SMMU_PTE_SHIFT 12
134 #define SMMU_PD_READABLE (1 << 31)
135 #define SMMU_PD_WRITABLE (1 << 30)
136 #define SMMU_PD_NONSECURE (1 << 29)
138 #define SMMU_PDE_READABLE (1 << 31)
139 #define SMMU_PDE_WRITABLE (1 << 30)
140 #define SMMU_PDE_NONSECURE (1 << 29)
141 #define SMMU_PDE_NEXT (1 << 28)
143 #define SMMU_PTE_READABLE (1 << 31)
144 #define SMMU_PTE_WRITABLE (1 << 30)
145 #define SMMU_PTE_NONSECURE (1 << 29)
147 #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
149 #define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
152 static unsigned int iova_pd_index(unsigned long iova
)
154 return (iova
>> SMMU_PDE_SHIFT
) & (SMMU_NUM_PDE
- 1);
157 static unsigned int iova_pt_index(unsigned long iova
)
159 return (iova
>> SMMU_PTE_SHIFT
) & (SMMU_NUM_PTE
- 1);
162 static bool smmu_dma_addr_valid(struct tegra_smmu
*smmu
, dma_addr_t addr
)
165 return (addr
& smmu
->pfn_mask
) == addr
;
168 static dma_addr_t
smmu_pde_to_dma(u32 pde
)
173 static void smmu_flush_ptc_all(struct tegra_smmu
*smmu
)
175 smmu_writel(smmu
, SMMU_PTC_FLUSH_TYPE_ALL
, SMMU_PTC_FLUSH
);
178 static inline void smmu_flush_ptc(struct tegra_smmu
*smmu
, dma_addr_t dma
,
179 unsigned long offset
)
183 offset
&= ~(smmu
->mc
->soc
->atom_size
- 1);
185 if (smmu
->mc
->soc
->num_address_bits
> 32) {
186 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
187 value
= (dma
>> 32) & SMMU_PTC_FLUSH_HI_MASK
;
191 smmu_writel(smmu
, value
, SMMU_PTC_FLUSH_HI
);
194 value
= (dma
+ offset
) | SMMU_PTC_FLUSH_TYPE_ADR
;
195 smmu_writel(smmu
, value
, SMMU_PTC_FLUSH
);
198 static inline void smmu_flush_tlb(struct tegra_smmu
*smmu
)
200 smmu_writel(smmu
, SMMU_TLB_FLUSH_VA_MATCH_ALL
, SMMU_TLB_FLUSH
);
203 static inline void smmu_flush_tlb_asid(struct tegra_smmu
*smmu
,
208 value
= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_ASID(asid
) |
209 SMMU_TLB_FLUSH_VA_MATCH_ALL
;
210 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
213 static inline void smmu_flush_tlb_section(struct tegra_smmu
*smmu
,
219 value
= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_ASID(asid
) |
220 SMMU_TLB_FLUSH_VA_SECTION(iova
);
221 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
224 static inline void smmu_flush_tlb_group(struct tegra_smmu
*smmu
,
230 value
= SMMU_TLB_FLUSH_ASID_MATCH
| SMMU_TLB_FLUSH_ASID(asid
) |
231 SMMU_TLB_FLUSH_VA_GROUP(iova
);
232 smmu_writel(smmu
, value
, SMMU_TLB_FLUSH
);
235 static inline void smmu_flush(struct tegra_smmu
*smmu
)
237 smmu_readl(smmu
, SMMU_CONFIG
);
240 static int tegra_smmu_alloc_asid(struct tegra_smmu
*smmu
, unsigned int *idp
)
244 mutex_lock(&smmu
->lock
);
246 id
= find_first_zero_bit(smmu
->asids
, smmu
->soc
->num_asids
);
247 if (id
>= smmu
->soc
->num_asids
) {
248 mutex_unlock(&smmu
->lock
);
252 set_bit(id
, smmu
->asids
);
255 mutex_unlock(&smmu
->lock
);
259 static void tegra_smmu_free_asid(struct tegra_smmu
*smmu
, unsigned int id
)
261 mutex_lock(&smmu
->lock
);
262 clear_bit(id
, smmu
->asids
);
263 mutex_unlock(&smmu
->lock
);
266 static bool tegra_smmu_capable(enum iommu_cap cap
)
271 static struct iommu_domain
*tegra_smmu_domain_alloc(unsigned type
)
273 struct tegra_smmu_as
*as
;
275 if (type
!= IOMMU_DOMAIN_UNMANAGED
)
278 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
282 as
->attr
= SMMU_PD_READABLE
| SMMU_PD_WRITABLE
| SMMU_PD_NONSECURE
;
284 as
->pd
= alloc_page(GFP_KERNEL
| __GFP_DMA
| __GFP_ZERO
);
290 as
->count
= kcalloc(SMMU_NUM_PDE
, sizeof(u32
), GFP_KERNEL
);
297 as
->pts
= kcalloc(SMMU_NUM_PDE
, sizeof(*as
->pts
), GFP_KERNEL
);
306 as
->domain
.geometry
.aperture_start
= 0;
307 as
->domain
.geometry
.aperture_end
= 0xffffffff;
308 as
->domain
.geometry
.force_aperture
= true;
313 static void tegra_smmu_domain_free(struct iommu_domain
*domain
)
315 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
317 /* TODO: free page directory and page tables */
322 static const struct tegra_smmu_swgroup
*
323 tegra_smmu_find_swgroup(struct tegra_smmu
*smmu
, unsigned int swgroup
)
325 const struct tegra_smmu_swgroup
*group
= NULL
;
328 for (i
= 0; i
< smmu
->soc
->num_swgroups
; i
++) {
329 if (smmu
->soc
->swgroups
[i
].swgroup
== swgroup
) {
330 group
= &smmu
->soc
->swgroups
[i
];
338 static void tegra_smmu_enable(struct tegra_smmu
*smmu
, unsigned int swgroup
,
341 const struct tegra_smmu_swgroup
*group
;
345 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
346 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
348 if (client
->swgroup
!= swgroup
)
351 value
= smmu_readl(smmu
, client
->smmu
.reg
);
352 value
|= BIT(client
->smmu
.bit
);
353 smmu_writel(smmu
, value
, client
->smmu
.reg
);
356 group
= tegra_smmu_find_swgroup(smmu
, swgroup
);
358 value
= smmu_readl(smmu
, group
->reg
);
359 value
&= ~SMMU_ASID_MASK
;
360 value
|= SMMU_ASID_VALUE(asid
);
361 value
|= SMMU_ASID_ENABLE
;
362 smmu_writel(smmu
, value
, group
->reg
);
366 static void tegra_smmu_disable(struct tegra_smmu
*smmu
, unsigned int swgroup
,
369 const struct tegra_smmu_swgroup
*group
;
373 group
= tegra_smmu_find_swgroup(smmu
, swgroup
);
375 value
= smmu_readl(smmu
, group
->reg
);
376 value
&= ~SMMU_ASID_MASK
;
377 value
|= SMMU_ASID_VALUE(asid
);
378 value
&= ~SMMU_ASID_ENABLE
;
379 smmu_writel(smmu
, value
, group
->reg
);
382 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
383 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
385 if (client
->swgroup
!= swgroup
)
388 value
= smmu_readl(smmu
, client
->smmu
.reg
);
389 value
&= ~BIT(client
->smmu
.bit
);
390 smmu_writel(smmu
, value
, client
->smmu
.reg
);
394 static int tegra_smmu_as_prepare(struct tegra_smmu
*smmu
,
395 struct tegra_smmu_as
*as
)
400 if (as
->use_count
> 0) {
405 as
->pd_dma
= dma_map_page(smmu
->dev
, as
->pd
, 0, SMMU_SIZE_PD
,
407 if (dma_mapping_error(smmu
->dev
, as
->pd_dma
))
410 /* We can't handle 64-bit DMA addresses */
411 if (!smmu_dma_addr_valid(smmu
, as
->pd_dma
)) {
416 err
= tegra_smmu_alloc_asid(smmu
, &as
->id
);
420 smmu_flush_ptc(smmu
, as
->pd_dma
, 0);
421 smmu_flush_tlb_asid(smmu
, as
->id
);
423 smmu_writel(smmu
, as
->id
& 0x7f, SMMU_PTB_ASID
);
424 value
= SMMU_PTB_DATA_VALUE(as
->pd_dma
, as
->attr
);
425 smmu_writel(smmu
, value
, SMMU_PTB_DATA
);
434 dma_unmap_page(smmu
->dev
, as
->pd_dma
, SMMU_SIZE_PD
, DMA_TO_DEVICE
);
438 static void tegra_smmu_as_unprepare(struct tegra_smmu
*smmu
,
439 struct tegra_smmu_as
*as
)
441 if (--as
->use_count
> 0)
444 tegra_smmu_free_asid(smmu
, as
->id
);
446 dma_unmap_page(smmu
->dev
, as
->pd_dma
, SMMU_SIZE_PD
, DMA_TO_DEVICE
);
451 static int tegra_smmu_attach_dev(struct iommu_domain
*domain
,
454 struct tegra_smmu
*smmu
= dev
->archdata
.iommu
;
455 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
456 struct device_node
*np
= dev
->of_node
;
457 struct of_phandle_args args
;
458 unsigned int index
= 0;
461 while (!of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
463 unsigned int swgroup
= args
.args
[0];
465 if (args
.np
!= smmu
->dev
->of_node
) {
466 of_node_put(args
.np
);
470 of_node_put(args
.np
);
472 err
= tegra_smmu_as_prepare(smmu
, as
);
476 tegra_smmu_enable(smmu
, swgroup
, as
->id
);
486 static void tegra_smmu_detach_dev(struct iommu_domain
*domain
, struct device
*dev
)
488 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
489 struct device_node
*np
= dev
->of_node
;
490 struct tegra_smmu
*smmu
= as
->smmu
;
491 struct of_phandle_args args
;
492 unsigned int index
= 0;
494 while (!of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
496 unsigned int swgroup
= args
.args
[0];
498 if (args
.np
!= smmu
->dev
->of_node
) {
499 of_node_put(args
.np
);
503 of_node_put(args
.np
);
505 tegra_smmu_disable(smmu
, swgroup
, as
->id
);
506 tegra_smmu_as_unprepare(smmu
, as
);
511 static void tegra_smmu_set_pde(struct tegra_smmu_as
*as
, unsigned long iova
,
514 unsigned int pd_index
= iova_pd_index(iova
);
515 struct tegra_smmu
*smmu
= as
->smmu
;
516 u32
*pd
= page_address(as
->pd
);
517 unsigned long offset
= pd_index
* sizeof(*pd
);
519 /* Set the page directory entry first */
520 pd
[pd_index
] = value
;
522 /* The flush the page directory entry from caches */
523 dma_sync_single_range_for_device(smmu
->dev
, as
->pd_dma
, offset
,
524 sizeof(*pd
), DMA_TO_DEVICE
);
526 /* And flush the iommu */
527 smmu_flush_ptc(smmu
, as
->pd_dma
, offset
);
528 smmu_flush_tlb_section(smmu
, as
->id
, iova
);
532 static u32
*tegra_smmu_pte_offset(struct page
*pt_page
, unsigned long iova
)
534 u32
*pt
= page_address(pt_page
);
536 return pt
+ iova_pt_index(iova
);
539 static u32
*tegra_smmu_pte_lookup(struct tegra_smmu_as
*as
, unsigned long iova
,
542 unsigned int pd_index
= iova_pd_index(iova
);
543 struct page
*pt_page
;
546 pt_page
= as
->pts
[pd_index
];
550 pd
= page_address(as
->pd
);
551 *dmap
= smmu_pde_to_dma(pd
[pd_index
]);
553 return tegra_smmu_pte_offset(pt_page
, iova
);
556 static u32
*as_get_pte(struct tegra_smmu_as
*as
, dma_addr_t iova
,
559 unsigned int pde
= iova_pd_index(iova
);
560 struct tegra_smmu
*smmu
= as
->smmu
;
566 page
= alloc_page(GFP_KERNEL
| __GFP_DMA
| __GFP_ZERO
);
570 dma
= dma_map_page(smmu
->dev
, page
, 0, SMMU_SIZE_PT
,
572 if (dma_mapping_error(smmu
->dev
, dma
)) {
577 if (!smmu_dma_addr_valid(smmu
, dma
)) {
578 dma_unmap_page(smmu
->dev
, dma
, SMMU_SIZE_PT
,
586 tegra_smmu_set_pde(as
, iova
, SMMU_MK_PDE(dma
, SMMU_PDE_ATTR
|
591 u32
*pd
= page_address(as
->pd
);
593 *dmap
= smmu_pde_to_dma(pd
[pde
]);
596 return tegra_smmu_pte_offset(as
->pts
[pde
], iova
);
599 static void tegra_smmu_pte_get_use(struct tegra_smmu_as
*as
, unsigned long iova
)
601 unsigned int pd_index
= iova_pd_index(iova
);
603 as
->count
[pd_index
]++;
606 static void tegra_smmu_pte_put_use(struct tegra_smmu_as
*as
, unsigned long iova
)
608 unsigned int pde
= iova_pd_index(iova
);
609 struct page
*page
= as
->pts
[pde
];
612 * When no entries in this page table are used anymore, return the
613 * memory page to the system.
615 if (--as
->count
[pde
] == 0) {
616 struct tegra_smmu
*smmu
= as
->smmu
;
617 u32
*pd
= page_address(as
->pd
);
618 dma_addr_t pte_dma
= smmu_pde_to_dma(pd
[pde
]);
620 tegra_smmu_set_pde(as
, iova
, 0);
622 dma_unmap_page(smmu
->dev
, pte_dma
, SMMU_SIZE_PT
, DMA_TO_DEVICE
);
628 static void tegra_smmu_set_pte(struct tegra_smmu_as
*as
, unsigned long iova
,
629 u32
*pte
, dma_addr_t pte_dma
, u32 val
)
631 struct tegra_smmu
*smmu
= as
->smmu
;
632 unsigned long offset
= offset_in_page(pte
);
636 dma_sync_single_range_for_device(smmu
->dev
, pte_dma
, offset
,
638 smmu_flush_ptc(smmu
, pte_dma
, offset
);
639 smmu_flush_tlb_group(smmu
, as
->id
, iova
);
643 static int tegra_smmu_map(struct iommu_domain
*domain
, unsigned long iova
,
644 phys_addr_t paddr
, size_t size
, int prot
)
646 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
650 pte
= as_get_pte(as
, iova
, &pte_dma
);
654 /* If we aren't overwriting a pre-existing entry, increment use */
656 tegra_smmu_pte_get_use(as
, iova
);
658 tegra_smmu_set_pte(as
, iova
, pte
, pte_dma
,
659 __phys_to_pfn(paddr
) | SMMU_PTE_ATTR
);
664 static size_t tegra_smmu_unmap(struct iommu_domain
*domain
, unsigned long iova
,
667 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
671 pte
= tegra_smmu_pte_lookup(as
, iova
, &pte_dma
);
675 tegra_smmu_set_pte(as
, iova
, pte
, pte_dma
, 0);
676 tegra_smmu_pte_put_use(as
, iova
);
681 static phys_addr_t
tegra_smmu_iova_to_phys(struct iommu_domain
*domain
,
684 struct tegra_smmu_as
*as
= to_smmu_as(domain
);
689 pte
= tegra_smmu_pte_lookup(as
, iova
, &pte_dma
);
693 pfn
= *pte
& as
->smmu
->pfn_mask
;
695 return PFN_PHYS(pfn
);
698 static struct tegra_smmu
*tegra_smmu_find(struct device_node
*np
)
700 struct platform_device
*pdev
;
703 pdev
= of_find_device_by_node(np
);
707 mc
= platform_get_drvdata(pdev
);
714 static int tegra_smmu_configure(struct tegra_smmu
*smmu
, struct device
*dev
,
715 struct of_phandle_args
*args
)
717 const struct iommu_ops
*ops
= smmu
->iommu
.ops
;
720 err
= iommu_fwspec_init(dev
, &dev
->of_node
->fwnode
, ops
);
722 dev_err(dev
, "failed to initialize fwspec: %d\n", err
);
726 err
= ops
->of_xlate(dev
, args
);
728 dev_err(dev
, "failed to parse SW group ID: %d\n", err
);
729 iommu_fwspec_free(dev
);
736 static int tegra_smmu_add_device(struct device
*dev
)
738 struct device_node
*np
= dev
->of_node
;
739 struct tegra_smmu
*smmu
= NULL
;
740 struct iommu_group
*group
;
741 struct of_phandle_args args
;
742 unsigned int index
= 0;
745 while (of_parse_phandle_with_args(np
, "iommus", "#iommu-cells", index
,
747 smmu
= tegra_smmu_find(args
.np
);
749 err
= tegra_smmu_configure(smmu
, dev
, &args
);
750 of_node_put(args
.np
);
756 * Only a single IOMMU master interface is currently
757 * supported by the Linux kernel, so abort after the
760 dev
->archdata
.iommu
= smmu
;
762 iommu_device_link(&smmu
->iommu
, dev
);
767 of_node_put(args
.np
);
774 group
= iommu_group_get_for_dev(dev
);
776 return PTR_ERR(group
);
778 iommu_group_put(group
);
783 static void tegra_smmu_remove_device(struct device
*dev
)
785 struct tegra_smmu
*smmu
= dev
->archdata
.iommu
;
788 iommu_device_unlink(&smmu
->iommu
, dev
);
790 dev
->archdata
.iommu
= NULL
;
791 iommu_group_remove_device(dev
);
794 static const struct tegra_smmu_group_soc
*
795 tegra_smmu_find_group(struct tegra_smmu
*smmu
, unsigned int swgroup
)
799 for (i
= 0; i
< smmu
->soc
->num_groups
; i
++)
800 for (j
= 0; j
< smmu
->soc
->groups
[i
].num_swgroups
; j
++)
801 if (smmu
->soc
->groups
[i
].swgroups
[j
] == swgroup
)
802 return &smmu
->soc
->groups
[i
];
807 static struct iommu_group
*tegra_smmu_group_get(struct tegra_smmu
*smmu
,
808 unsigned int swgroup
)
810 const struct tegra_smmu_group_soc
*soc
;
811 struct tegra_smmu_group
*group
;
813 soc
= tegra_smmu_find_group(smmu
, swgroup
);
817 mutex_lock(&smmu
->lock
);
819 list_for_each_entry(group
, &smmu
->groups
, list
)
820 if (group
->soc
== soc
) {
821 mutex_unlock(&smmu
->lock
);
825 group
= devm_kzalloc(smmu
->dev
, sizeof(*group
), GFP_KERNEL
);
827 mutex_unlock(&smmu
->lock
);
831 INIT_LIST_HEAD(&group
->list
);
834 group
->group
= iommu_group_alloc();
835 if (IS_ERR(group
->group
)) {
836 devm_kfree(smmu
->dev
, group
);
837 mutex_unlock(&smmu
->lock
);
841 list_add_tail(&group
->list
, &smmu
->groups
);
842 mutex_unlock(&smmu
->lock
);
847 static struct iommu_group
*tegra_smmu_device_group(struct device
*dev
)
849 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
850 struct tegra_smmu
*smmu
= dev
->archdata
.iommu
;
851 struct iommu_group
*group
;
853 group
= tegra_smmu_group_get(smmu
, fwspec
->ids
[0]);
855 group
= generic_device_group(dev
);
860 static int tegra_smmu_of_xlate(struct device
*dev
,
861 struct of_phandle_args
*args
)
863 u32 id
= args
->args
[0];
865 return iommu_fwspec_add_ids(dev
, &id
, 1);
868 static const struct iommu_ops tegra_smmu_ops
= {
869 .capable
= tegra_smmu_capable
,
870 .domain_alloc
= tegra_smmu_domain_alloc
,
871 .domain_free
= tegra_smmu_domain_free
,
872 .attach_dev
= tegra_smmu_attach_dev
,
873 .detach_dev
= tegra_smmu_detach_dev
,
874 .add_device
= tegra_smmu_add_device
,
875 .remove_device
= tegra_smmu_remove_device
,
876 .device_group
= tegra_smmu_device_group
,
877 .map
= tegra_smmu_map
,
878 .unmap
= tegra_smmu_unmap
,
879 .iova_to_phys
= tegra_smmu_iova_to_phys
,
880 .of_xlate
= tegra_smmu_of_xlate
,
881 .pgsize_bitmap
= SZ_4K
,
884 static void tegra_smmu_ahb_enable(void)
886 static const struct of_device_id ahb_match
[] = {
887 { .compatible
= "nvidia,tegra30-ahb", },
890 struct device_node
*ahb
;
892 ahb
= of_find_matching_node(NULL
, ahb_match
);
894 tegra_ahb_enable_smmu(ahb
);
899 static int tegra_smmu_swgroups_show(struct seq_file
*s
, void *data
)
901 struct tegra_smmu
*smmu
= s
->private;
905 seq_printf(s
, "swgroup enabled ASID\n");
906 seq_printf(s
, "------------------------\n");
908 for (i
= 0; i
< smmu
->soc
->num_swgroups
; i
++) {
909 const struct tegra_smmu_swgroup
*group
= &smmu
->soc
->swgroups
[i
];
913 value
= smmu_readl(smmu
, group
->reg
);
915 if (value
& SMMU_ASID_ENABLE
)
920 asid
= value
& SMMU_ASID_MASK
;
922 seq_printf(s
, "%-9s %-7s %#04x\n", group
->name
, status
,
929 DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups
);
931 static int tegra_smmu_clients_show(struct seq_file
*s
, void *data
)
933 struct tegra_smmu
*smmu
= s
->private;
937 seq_printf(s
, "client enabled\n");
938 seq_printf(s
, "--------------------\n");
940 for (i
= 0; i
< smmu
->soc
->num_clients
; i
++) {
941 const struct tegra_mc_client
*client
= &smmu
->soc
->clients
[i
];
944 value
= smmu_readl(smmu
, client
->smmu
.reg
);
946 if (value
& BIT(client
->smmu
.bit
))
951 seq_printf(s
, "%-12s %s\n", client
->name
, status
);
957 DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients
);
959 static void tegra_smmu_debugfs_init(struct tegra_smmu
*smmu
)
961 smmu
->debugfs
= debugfs_create_dir("smmu", NULL
);
965 debugfs_create_file("swgroups", S_IRUGO
, smmu
->debugfs
, smmu
,
966 &tegra_smmu_swgroups_fops
);
967 debugfs_create_file("clients", S_IRUGO
, smmu
->debugfs
, smmu
,
968 &tegra_smmu_clients_fops
);
971 static void tegra_smmu_debugfs_exit(struct tegra_smmu
*smmu
)
973 debugfs_remove_recursive(smmu
->debugfs
);
976 struct tegra_smmu
*tegra_smmu_probe(struct device
*dev
,
977 const struct tegra_smmu_soc
*soc
,
980 struct tegra_smmu
*smmu
;
985 smmu
= devm_kzalloc(dev
, sizeof(*smmu
), GFP_KERNEL
);
987 return ERR_PTR(-ENOMEM
);
990 * This is a bit of a hack. Ideally we'd want to simply return this
991 * value. However the IOMMU registration process will attempt to add
992 * all devices to the IOMMU when bus_set_iommu() is called. In order
993 * not to rely on global variables to track the IOMMU instance, we
994 * set it here so that it can be looked up from the .add_device()
995 * callback via the IOMMU device's .drvdata field.
999 size
= BITS_TO_LONGS(soc
->num_asids
) * sizeof(long);
1001 smmu
->asids
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
1003 return ERR_PTR(-ENOMEM
);
1005 INIT_LIST_HEAD(&smmu
->groups
);
1006 mutex_init(&smmu
->lock
);
1008 smmu
->regs
= mc
->regs
;
1013 smmu
->pfn_mask
= BIT_MASK(mc
->soc
->num_address_bits
- PAGE_SHIFT
) - 1;
1014 dev_dbg(dev
, "address bits: %u, PFN mask: %#lx\n",
1015 mc
->soc
->num_address_bits
, smmu
->pfn_mask
);
1016 smmu
->tlb_mask
= (smmu
->soc
->num_tlb_lines
<< 1) - 1;
1017 dev_dbg(dev
, "TLB lines: %u, mask: %#lx\n", smmu
->soc
->num_tlb_lines
,
1020 value
= SMMU_PTC_CONFIG_ENABLE
| SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
1022 if (soc
->supports_request_limit
)
1023 value
|= SMMU_PTC_CONFIG_REQ_LIMIT(8);
1025 smmu_writel(smmu
, value
, SMMU_PTC_CONFIG
);
1027 value
= SMMU_TLB_CONFIG_HIT_UNDER_MISS
|
1028 SMMU_TLB_CONFIG_ACTIVE_LINES(smmu
);
1030 if (soc
->supports_round_robin_arbitration
)
1031 value
|= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION
;
1033 smmu_writel(smmu
, value
, SMMU_TLB_CONFIG
);
1035 smmu_flush_ptc_all(smmu
);
1036 smmu_flush_tlb(smmu
);
1037 smmu_writel(smmu
, SMMU_CONFIG_ENABLE
, SMMU_CONFIG
);
1040 tegra_smmu_ahb_enable();
1042 err
= iommu_device_sysfs_add(&smmu
->iommu
, dev
, NULL
, dev_name(dev
));
1044 return ERR_PTR(err
);
1046 iommu_device_set_ops(&smmu
->iommu
, &tegra_smmu_ops
);
1047 iommu_device_set_fwnode(&smmu
->iommu
, dev
->fwnode
);
1049 err
= iommu_device_register(&smmu
->iommu
);
1051 iommu_device_sysfs_remove(&smmu
->iommu
);
1052 return ERR_PTR(err
);
1055 err
= bus_set_iommu(&platform_bus_type
, &tegra_smmu_ops
);
1057 iommu_device_unregister(&smmu
->iommu
);
1058 iommu_device_sysfs_remove(&smmu
->iommu
);
1059 return ERR_PTR(err
);
1062 if (IS_ENABLED(CONFIG_DEBUG_FS
))
1063 tegra_smmu_debugfs_init(smmu
);
1068 void tegra_smmu_remove(struct tegra_smmu
*smmu
)
1070 iommu_device_unregister(&smmu
->iommu
);
1071 iommu_device_sysfs_remove(&smmu
->iommu
);
1073 if (IS_ENABLED(CONFIG_DEBUG_FS
))
1074 tegra_smmu_debugfs_exit(smmu
);