Skip to content

Commit aab5a1c

Browse files
krishnareddy-dvwilldeacon
authored andcommitted
iommu/arm-smmu: add NVIDIA implementation for ARM MMU-500 usage
NVIDIA's Tegra194 SoC has three ARM MMU-500 instances. It uses two of the ARM MMU-500s together to interleave IOVA accesses across them and must be programmed identically. This implementation supports programming the two ARM MMU-500s that must be programmed identically. The third ARM MMU-500 instance is supported by standard arm-smmu.c driver itself. Signed-off-by: Krishna Reddy <[email protected]> Reviewed-by: Jon Hunter <[email protected]> Reviewed-by: Nicolin Chen <[email protected]> Reviewed-by: Pritesh Raithatha <[email protected]> Reviewed-by: Robin Murphy <[email protected]> Reviewed-by: Thierry Reding <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent 6c019f4 commit aab5a1c

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16810,8 +16810,10 @@ F: drivers/i2c/busses/i2c-tegra.c
1681016810

1681116811
TEGRA IOMMU DRIVERS
1681216812
M: Thierry Reding <[email protected]>
16813+
R: Krishna Reddy <[email protected]>
1681316814
1681416815
S: Supported
16816+
F: drivers/iommu/arm-smmu-nvidia.c
1681516817
F: drivers/iommu/tegra*
1681616818

1681716819
TEGRA KBC DRIVER

drivers/iommu/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ obj-$(CONFIG_AMD_IOMMU) += amd/iommu.o amd/init.o amd/quirks.o
1515
obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += amd/debugfs.o
1616
obj-$(CONFIG_AMD_IOMMU_V2) += amd/iommu_v2.o
1717
obj-$(CONFIG_ARM_SMMU) += arm_smmu.o
18-
arm_smmu-objs += arm-smmu.o arm-smmu-impl.o arm-smmu-qcom.o
18+
arm_smmu-objs += arm-smmu.o arm-smmu-impl.o arm-smmu-nvidia.o arm-smmu-qcom.o
1919
obj-$(CONFIG_ARM_SMMU_V3) += arm-smmu-v3.o
2020
obj-$(CONFIG_DMAR_TABLE) += intel/dmar.o
2121
obj-$(CONFIG_INTEL_IOMMU) += intel/iommu.o intel/pasid.o

drivers/iommu/arm-smmu-impl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ struct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu)
213213
if (of_property_read_bool(np, "calxeda,smmu-secure-config-access"))
214214
smmu->impl = &calxeda_impl;
215215

216+
if (of_device_is_compatible(np, "nvidia,tegra194-smmu"))
217+
return nvidia_smmu_impl_init(smmu);
218+
216219
if (of_device_is_compatible(np, "qcom,sdm845-smmu-500") ||
217220
of_device_is_compatible(np, "qcom,sc7180-smmu-500") ||
218221
of_device_is_compatible(np, "qcom,sm8150-smmu-500") ||

drivers/iommu/arm-smmu-nvidia.c

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
// Copyright (C) 2019-2020 NVIDIA CORPORATION. All rights reserved.
3+
4+
#include <linux/bitfield.h>
5+
#include <linux/delay.h>
6+
#include <linux/of.h>
7+
#include <linux/platform_device.h>
8+
#include <linux/slab.h>
9+
10+
#include "arm-smmu.h"
11+
12+
/*
13+
* Tegra194 has three ARM MMU-500 Instances.
14+
* Two of them are used together and must be programmed identically for
15+
* interleaved IOVA accesses across them and translates accesses from
16+
* non-isochronous HW devices.
17+
* Third one is used for translating accesses from isochronous HW devices.
18+
* This implementation supports programming of the two instances that must
19+
* be programmed identically.
20+
* The third instance usage is through standard arm-smmu driver itself and
21+
* is out of scope of this implementation.
22+
*/
23+
#define NUM_SMMU_INSTANCES 2
24+
25+
struct nvidia_smmu {
26+
struct arm_smmu_device smmu;
27+
void __iomem *bases[NUM_SMMU_INSTANCES];
28+
};
29+
30+
static inline void __iomem *nvidia_smmu_page(struct arm_smmu_device *smmu,
31+
unsigned int inst, int page)
32+
{
33+
struct nvidia_smmu *nvidia_smmu;
34+
35+
nvidia_smmu = container_of(smmu, struct nvidia_smmu, smmu);
36+
return nvidia_smmu->bases[inst] + (page << smmu->pgshift);
37+
}
38+
39+
static u32 nvidia_smmu_read_reg(struct arm_smmu_device *smmu,
40+
int page, int offset)
41+
{
42+
void __iomem *reg = nvidia_smmu_page(smmu, 0, page) + offset;
43+
44+
return readl_relaxed(reg);
45+
}
46+
47+
static void nvidia_smmu_write_reg(struct arm_smmu_device *smmu,
48+
int page, int offset, u32 val)
49+
{
50+
unsigned int i;
51+
52+
for (i = 0; i < NUM_SMMU_INSTANCES; i++) {
53+
void __iomem *reg = nvidia_smmu_page(smmu, i, page) + offset;
54+
55+
writel_relaxed(val, reg);
56+
}
57+
}
58+
59+
static u64 nvidia_smmu_read_reg64(struct arm_smmu_device *smmu,
60+
int page, int offset)
61+
{
62+
void __iomem *reg = nvidia_smmu_page(smmu, 0, page) + offset;
63+
64+
return readq_relaxed(reg);
65+
}
66+
67+
static void nvidia_smmu_write_reg64(struct arm_smmu_device *smmu,
68+
int page, int offset, u64 val)
69+
{
70+
unsigned int i;
71+
72+
for (i = 0; i < NUM_SMMU_INSTANCES; i++) {
73+
void __iomem *reg = nvidia_smmu_page(smmu, i, page) + offset;
74+
75+
writeq_relaxed(val, reg);
76+
}
77+
}
78+
79+
static void nvidia_smmu_tlb_sync(struct arm_smmu_device *smmu, int page,
80+
int sync, int status)
81+
{
82+
unsigned int delay;
83+
84+
arm_smmu_writel(smmu, page, sync, 0);
85+
86+
for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) {
87+
unsigned int spin_cnt;
88+
89+
for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) {
90+
u32 val = 0;
91+
unsigned int i;
92+
93+
for (i = 0; i < NUM_SMMU_INSTANCES; i++) {
94+
void __iomem *reg;
95+
96+
reg = nvidia_smmu_page(smmu, i, page) + status;
97+
val |= readl_relaxed(reg);
98+
}
99+
100+
if (!(val & ARM_SMMU_sTLBGSTATUS_GSACTIVE))
101+
return;
102+
103+
cpu_relax();
104+
}
105+
106+
udelay(delay);
107+
}
108+
109+
dev_err_ratelimited(smmu->dev,
110+
"TLB sync timed out -- SMMU may be deadlocked\n");
111+
}
112+
113+
static int nvidia_smmu_reset(struct arm_smmu_device *smmu)
114+
{
115+
unsigned int i;
116+
117+
for (i = 0; i < NUM_SMMU_INSTANCES; i++) {
118+
u32 val;
119+
void __iomem *reg = nvidia_smmu_page(smmu, i, ARM_SMMU_GR0) +
120+
ARM_SMMU_GR0_sGFSR;
121+
122+
/* clear global FSR */
123+
val = readl_relaxed(reg);
124+
writel_relaxed(val, reg);
125+
}
126+
127+
return 0;
128+
}
129+
130+
static const struct arm_smmu_impl nvidia_smmu_impl = {
131+
.read_reg = nvidia_smmu_read_reg,
132+
.write_reg = nvidia_smmu_write_reg,
133+
.read_reg64 = nvidia_smmu_read_reg64,
134+
.write_reg64 = nvidia_smmu_write_reg64,
135+
.reset = nvidia_smmu_reset,
136+
.tlb_sync = nvidia_smmu_tlb_sync,
137+
};
138+
139+
struct arm_smmu_device *nvidia_smmu_impl_init(struct arm_smmu_device *smmu)
140+
{
141+
struct resource *res;
142+
struct device *dev = smmu->dev;
143+
struct nvidia_smmu *nvidia_smmu;
144+
struct platform_device *pdev = to_platform_device(dev);
145+
146+
nvidia_smmu = devm_kzalloc(dev, sizeof(*nvidia_smmu), GFP_KERNEL);
147+
if (!nvidia_smmu)
148+
return ERR_PTR(-ENOMEM);
149+
150+
/*
151+
* Copy the data from struct arm_smmu_device *smmu allocated in
152+
* arm-smmu.c. The smmu from struct nvidia_smmu replaces the smmu
153+
* pointer used in arm-smmu.c once this function returns.
154+
* This is necessary to derive nvidia_smmu from smmu pointer passed
155+
* through arm_smmu_impl function calls subsequently.
156+
*/
157+
nvidia_smmu->smmu = *smmu;
158+
/* Instance 0 is ioremapped by arm-smmu.c. */
159+
nvidia_smmu->bases[0] = smmu->base;
160+
161+
res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
162+
if (!res)
163+
return ERR_PTR(-ENODEV);
164+
165+
nvidia_smmu->bases[1] = devm_ioremap_resource(dev, res);
166+
if (IS_ERR(nvidia_smmu->bases[1]))
167+
return ERR_CAST(nvidia_smmu->bases[1]);
168+
169+
nvidia_smmu->smmu.impl = &nvidia_smmu_impl;
170+
171+
/*
172+
* Free the struct arm_smmu_device *smmu allocated in arm-smmu.c.
173+
* Once this function returns, arm-smmu.c would use arm_smmu_device
174+
* allocated as part of struct nvidia_smmu.
175+
*/
176+
devm_kfree(dev, smmu);
177+
178+
return &nvidia_smmu->smmu;
179+
}

drivers/iommu/arm-smmu.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,7 @@ static const struct of_device_id arm_smmu_of_match[] = {
19461946
{ .compatible = "arm,mmu-401", .data = &arm_mmu401 },
19471947
{ .compatible = "arm,mmu-500", .data = &arm_mmu500 },
19481948
{ .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
1949+
{ .compatible = "nvidia,smmu-500", .data = &arm_mmu500 },
19491950
{ .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
19501951
{ },
19511952
};

drivers/iommu/arm-smmu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ static inline void arm_smmu_writeq(struct arm_smmu_device *smmu, int page,
452452
arm_smmu_writeq((s), ARM_SMMU_CB((s), (n)), (o), (v))
453453

454454
struct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu);
455+
struct arm_smmu_device *nvidia_smmu_impl_init(struct arm_smmu_device *smmu);
455456
struct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu);
456457

457458
int arm_mmu500_reset(struct arm_smmu_device *smmu);

0 commit comments

Comments
 (0)