1e86d1aa8SWill Deacon // SPDX-License-Identifier: GPL-2.0-only
2e86d1aa8SWill Deacon // Miscellaneous Arm SMMU implementation and integration quirks
3e86d1aa8SWill Deacon // Copyright (C) 2019 Arm Limited
4e86d1aa8SWill Deacon
5e86d1aa8SWill Deacon #define pr_fmt(fmt) "arm-smmu: " fmt
6e86d1aa8SWill Deacon
7e86d1aa8SWill Deacon #include <linux/bitfield.h>
8e86d1aa8SWill Deacon #include <linux/of.h>
9e86d1aa8SWill Deacon
10e86d1aa8SWill Deacon #include "arm-smmu.h"
11e86d1aa8SWill Deacon
12e86d1aa8SWill Deacon
arm_smmu_gr0_ns(int offset)13e86d1aa8SWill Deacon static int arm_smmu_gr0_ns(int offset)
14e86d1aa8SWill Deacon {
15e86d1aa8SWill Deacon switch (offset) {
16e86d1aa8SWill Deacon case ARM_SMMU_GR0_sCR0:
17e86d1aa8SWill Deacon case ARM_SMMU_GR0_sACR:
18e86d1aa8SWill Deacon case ARM_SMMU_GR0_sGFSR:
19e86d1aa8SWill Deacon case ARM_SMMU_GR0_sGFSYNR0:
20e86d1aa8SWill Deacon case ARM_SMMU_GR0_sGFSYNR1:
21e86d1aa8SWill Deacon case ARM_SMMU_GR0_sGFSYNR2:
22e86d1aa8SWill Deacon return offset + 0x400;
23e86d1aa8SWill Deacon default:
24e86d1aa8SWill Deacon return offset;
25e86d1aa8SWill Deacon }
26e86d1aa8SWill Deacon }
27e86d1aa8SWill Deacon
arm_smmu_read_ns(struct arm_smmu_device * smmu,int page,int offset)28e86d1aa8SWill Deacon static u32 arm_smmu_read_ns(struct arm_smmu_device *smmu, int page,
29e86d1aa8SWill Deacon int offset)
30e86d1aa8SWill Deacon {
31e86d1aa8SWill Deacon if (page == ARM_SMMU_GR0)
32e86d1aa8SWill Deacon offset = arm_smmu_gr0_ns(offset);
33e86d1aa8SWill Deacon return readl_relaxed(arm_smmu_page(smmu, page) + offset);
34e86d1aa8SWill Deacon }
35e86d1aa8SWill Deacon
arm_smmu_write_ns(struct arm_smmu_device * smmu,int page,int offset,u32 val)36e86d1aa8SWill Deacon static void arm_smmu_write_ns(struct arm_smmu_device *smmu, int page,
37e86d1aa8SWill Deacon int offset, u32 val)
38e86d1aa8SWill Deacon {
39e86d1aa8SWill Deacon if (page == ARM_SMMU_GR0)
40e86d1aa8SWill Deacon offset = arm_smmu_gr0_ns(offset);
41e86d1aa8SWill Deacon writel_relaxed(val, arm_smmu_page(smmu, page) + offset);
42e86d1aa8SWill Deacon }
43e86d1aa8SWill Deacon
44e86d1aa8SWill Deacon /* Since we don't care for sGFAR, we can do without 64-bit accessors */
45e86d1aa8SWill Deacon static const struct arm_smmu_impl calxeda_impl = {
46e86d1aa8SWill Deacon .read_reg = arm_smmu_read_ns,
47e86d1aa8SWill Deacon .write_reg = arm_smmu_write_ns,
48e86d1aa8SWill Deacon };
49e86d1aa8SWill Deacon
50e86d1aa8SWill Deacon
51e86d1aa8SWill Deacon struct cavium_smmu {
52e86d1aa8SWill Deacon struct arm_smmu_device smmu;
53e86d1aa8SWill Deacon u32 id_base;
54e86d1aa8SWill Deacon };
55e86d1aa8SWill Deacon
cavium_cfg_probe(struct arm_smmu_device * smmu)56e86d1aa8SWill Deacon static int cavium_cfg_probe(struct arm_smmu_device *smmu)
57e86d1aa8SWill Deacon {
58e86d1aa8SWill Deacon static atomic_t context_count = ATOMIC_INIT(0);
59e86d1aa8SWill Deacon struct cavium_smmu *cs = container_of(smmu, struct cavium_smmu, smmu);
60e86d1aa8SWill Deacon /*
61e86d1aa8SWill Deacon * Cavium CN88xx erratum #27704.
62e86d1aa8SWill Deacon * Ensure ASID and VMID allocation is unique across all SMMUs in
63e86d1aa8SWill Deacon * the system.
64e86d1aa8SWill Deacon */
65e86d1aa8SWill Deacon cs->id_base = atomic_fetch_add(smmu->num_context_banks, &context_count);
66e86d1aa8SWill Deacon dev_notice(smmu->dev, "\tenabling workaround for Cavium erratum 27704\n");
67e86d1aa8SWill Deacon
68e86d1aa8SWill Deacon return 0;
69e86d1aa8SWill Deacon }
70e86d1aa8SWill Deacon
cavium_init_context(struct arm_smmu_domain * smmu_domain,struct io_pgtable_cfg * pgtbl_cfg,struct device * dev)71dd147a89SJordan Crouse static int cavium_init_context(struct arm_smmu_domain *smmu_domain,
72556db53aSJordan Crouse struct io_pgtable_cfg *pgtbl_cfg, struct device *dev)
73e86d1aa8SWill Deacon {
74e86d1aa8SWill Deacon struct cavium_smmu *cs = container_of(smmu_domain->smmu,
75e86d1aa8SWill Deacon struct cavium_smmu, smmu);
76e86d1aa8SWill Deacon
77e86d1aa8SWill Deacon if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
78e86d1aa8SWill Deacon smmu_domain->cfg.vmid += cs->id_base;
79e86d1aa8SWill Deacon else
80e86d1aa8SWill Deacon smmu_domain->cfg.asid += cs->id_base;
81e86d1aa8SWill Deacon
82e86d1aa8SWill Deacon return 0;
83e86d1aa8SWill Deacon }
84e86d1aa8SWill Deacon
85e86d1aa8SWill Deacon static const struct arm_smmu_impl cavium_impl = {
86e86d1aa8SWill Deacon .cfg_probe = cavium_cfg_probe,
87e86d1aa8SWill Deacon .init_context = cavium_init_context,
88e86d1aa8SWill Deacon };
89e86d1aa8SWill Deacon
cavium_smmu_impl_init(struct arm_smmu_device * smmu)90e86d1aa8SWill Deacon static struct arm_smmu_device *cavium_smmu_impl_init(struct arm_smmu_device *smmu)
91e86d1aa8SWill Deacon {
92e86d1aa8SWill Deacon struct cavium_smmu *cs;
93e86d1aa8SWill Deacon
94af9da914SRobin Murphy cs = devm_krealloc(smmu->dev, smmu, sizeof(*cs), GFP_KERNEL);
95e86d1aa8SWill Deacon if (!cs)
96e86d1aa8SWill Deacon return ERR_PTR(-ENOMEM);
97e86d1aa8SWill Deacon
98e86d1aa8SWill Deacon cs->smmu.impl = &cavium_impl;
99e86d1aa8SWill Deacon
100e86d1aa8SWill Deacon return &cs->smmu;
101e86d1aa8SWill Deacon }
102e86d1aa8SWill Deacon
103e86d1aa8SWill Deacon
104e86d1aa8SWill Deacon #define ARM_MMU500_ACTLR_CPRE (1 << 1)
105e86d1aa8SWill Deacon
106e86d1aa8SWill Deacon #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
107e86d1aa8SWill Deacon #define ARM_MMU500_ACR_S2CRB_TLBEN (1 << 10)
108e86d1aa8SWill Deacon #define ARM_MMU500_ACR_SMTNMB_TLBEN (1 << 8)
109e86d1aa8SWill Deacon
arm_mmu500_reset(struct arm_smmu_device * smmu)110e86d1aa8SWill Deacon int arm_mmu500_reset(struct arm_smmu_device *smmu)
111e86d1aa8SWill Deacon {
112e86d1aa8SWill Deacon u32 reg, major;
113e86d1aa8SWill Deacon int i;
114e86d1aa8SWill Deacon /*
115e86d1aa8SWill Deacon * On MMU-500 r2p0 onwards we need to clear ACR.CACHE_LOCK before
116e86d1aa8SWill Deacon * writes to the context bank ACTLRs will stick. And we just hope that
117e86d1aa8SWill Deacon * Secure has also cleared SACR.CACHE_LOCK for this to take effect...
118e86d1aa8SWill Deacon */
119e86d1aa8SWill Deacon reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID7);
120e86d1aa8SWill Deacon major = FIELD_GET(ARM_SMMU_ID7_MAJOR, reg);
121e86d1aa8SWill Deacon reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sACR);
122e86d1aa8SWill Deacon if (major >= 2)
123e86d1aa8SWill Deacon reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
124e86d1aa8SWill Deacon /*
125e86d1aa8SWill Deacon * Allow unmatched Stream IDs to allocate bypass
126e86d1aa8SWill Deacon * TLB entries for reduced latency.
127e86d1aa8SWill Deacon */
128e86d1aa8SWill Deacon reg |= ARM_MMU500_ACR_SMTNMB_TLBEN | ARM_MMU500_ACR_S2CRB_TLBEN;
129e86d1aa8SWill Deacon arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sACR, reg);
130e86d1aa8SWill Deacon
131e86d1aa8SWill Deacon /*
132e86d1aa8SWill Deacon * Disable MMU-500's not-particularly-beneficial next-page
133*8902a522SRobin Murphy * prefetcher for the sake of at least 5 known errata.
134e86d1aa8SWill Deacon */
135e86d1aa8SWill Deacon for (i = 0; i < smmu->num_context_banks; ++i) {
136e86d1aa8SWill Deacon reg = arm_smmu_cb_read(smmu, i, ARM_SMMU_CB_ACTLR);
137e86d1aa8SWill Deacon reg &= ~ARM_MMU500_ACTLR_CPRE;
138e86d1aa8SWill Deacon arm_smmu_cb_write(smmu, i, ARM_SMMU_CB_ACTLR, reg);
139f87f6e5bSChen Lin reg = arm_smmu_cb_read(smmu, i, ARM_SMMU_CB_ACTLR);
140f87f6e5bSChen Lin if (reg & ARM_MMU500_ACTLR_CPRE)
141*8902a522SRobin Murphy dev_warn_once(smmu->dev, "Failed to disable prefetcher for errata workarounds, check SACR.CACHE_LOCK\n");
142e86d1aa8SWill Deacon }
143e86d1aa8SWill Deacon
144e86d1aa8SWill Deacon return 0;
145e86d1aa8SWill Deacon }
146e86d1aa8SWill Deacon
147e86d1aa8SWill Deacon static const struct arm_smmu_impl arm_mmu500_impl = {
148e86d1aa8SWill Deacon .reset = arm_mmu500_reset,
149e86d1aa8SWill Deacon };
150e86d1aa8SWill Deacon
mrvl_mmu500_readq(struct arm_smmu_device * smmu,int page,int off)151e86d1aa8SWill Deacon static u64 mrvl_mmu500_readq(struct arm_smmu_device *smmu, int page, int off)
152e86d1aa8SWill Deacon {
153e86d1aa8SWill Deacon /*
154e86d1aa8SWill Deacon * Marvell Armada-AP806 erratum #582743.
155e86d1aa8SWill Deacon * Split all the readq to double readl
156e86d1aa8SWill Deacon */
157e86d1aa8SWill Deacon return hi_lo_readq_relaxed(arm_smmu_page(smmu, page) + off);
158e86d1aa8SWill Deacon }
159e86d1aa8SWill Deacon
mrvl_mmu500_writeq(struct arm_smmu_device * smmu,int page,int off,u64 val)160e86d1aa8SWill Deacon static void mrvl_mmu500_writeq(struct arm_smmu_device *smmu, int page, int off,
161e86d1aa8SWill Deacon u64 val)
162e86d1aa8SWill Deacon {
163e86d1aa8SWill Deacon /*
164e86d1aa8SWill Deacon * Marvell Armada-AP806 erratum #582743.
165e86d1aa8SWill Deacon * Split all the writeq to double writel
166e86d1aa8SWill Deacon */
167e86d1aa8SWill Deacon hi_lo_writeq_relaxed(val, arm_smmu_page(smmu, page) + off);
168e86d1aa8SWill Deacon }
169e86d1aa8SWill Deacon
mrvl_mmu500_cfg_probe(struct arm_smmu_device * smmu)170e86d1aa8SWill Deacon static int mrvl_mmu500_cfg_probe(struct arm_smmu_device *smmu)
171e86d1aa8SWill Deacon {
172e86d1aa8SWill Deacon
173e86d1aa8SWill Deacon /*
174e86d1aa8SWill Deacon * Armada-AP806 erratum #582743.
175e86d1aa8SWill Deacon * Hide the SMMU_IDR2.PTFSv8 fields to sidestep the AArch64
176e86d1aa8SWill Deacon * formats altogether and allow using 32 bits access on the
177e86d1aa8SWill Deacon * interconnect.
178e86d1aa8SWill Deacon */
179e86d1aa8SWill Deacon smmu->features &= ~(ARM_SMMU_FEAT_FMT_AARCH64_4K |
180e86d1aa8SWill Deacon ARM_SMMU_FEAT_FMT_AARCH64_16K |
181e86d1aa8SWill Deacon ARM_SMMU_FEAT_FMT_AARCH64_64K);
182e86d1aa8SWill Deacon
183e86d1aa8SWill Deacon return 0;
184e86d1aa8SWill Deacon }
185e86d1aa8SWill Deacon
186e86d1aa8SWill Deacon static const struct arm_smmu_impl mrvl_mmu500_impl = {
187e86d1aa8SWill Deacon .read_reg64 = mrvl_mmu500_readq,
188e86d1aa8SWill Deacon .write_reg64 = mrvl_mmu500_writeq,
189e86d1aa8SWill Deacon .cfg_probe = mrvl_mmu500_cfg_probe,
190e86d1aa8SWill Deacon .reset = arm_mmu500_reset,
191e86d1aa8SWill Deacon };
192e86d1aa8SWill Deacon
193e86d1aa8SWill Deacon
arm_smmu_impl_init(struct arm_smmu_device * smmu)194e86d1aa8SWill Deacon struct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu)
195e86d1aa8SWill Deacon {
196e86d1aa8SWill Deacon const struct device_node *np = smmu->dev->of_node;
197e86d1aa8SWill Deacon
198e86d1aa8SWill Deacon /*
199e86d1aa8SWill Deacon * Set the impl for model-specific implementation quirks first,
200e86d1aa8SWill Deacon * such that platform integration quirks can pick it up and
201e86d1aa8SWill Deacon * inherit from it if necessary.
202e86d1aa8SWill Deacon */
203e86d1aa8SWill Deacon switch (smmu->model) {
204e86d1aa8SWill Deacon case ARM_MMU500:
205e86d1aa8SWill Deacon smmu->impl = &arm_mmu500_impl;
206e86d1aa8SWill Deacon break;
207e86d1aa8SWill Deacon case CAVIUM_SMMUV2:
208e86d1aa8SWill Deacon return cavium_smmu_impl_init(smmu);
209e86d1aa8SWill Deacon default:
210e86d1aa8SWill Deacon break;
211e86d1aa8SWill Deacon }
212e86d1aa8SWill Deacon
213e86d1aa8SWill Deacon /* This is implicitly MMU-400 */
214e86d1aa8SWill Deacon if (of_property_read_bool(np, "calxeda,smmu-secure-config-access"))
215e86d1aa8SWill Deacon smmu->impl = &calxeda_impl;
216e86d1aa8SWill Deacon
2175ca21615SThierry Reding if (of_device_is_compatible(np, "nvidia,tegra234-smmu") ||
2185ca21615SThierry Reding of_device_is_compatible(np, "nvidia,tegra194-smmu") ||
2192c1bc371SThierry Reding of_device_is_compatible(np, "nvidia,tegra186-smmu"))
220e86d1aa8SWill Deacon return nvidia_smmu_impl_init(smmu);
221e86d1aa8SWill Deacon
222424953cfSArnd Bergmann if (IS_ENABLED(CONFIG_ARM_SMMU_QCOM))
22300597f9fSSai Prakash Ranjan smmu = qcom_smmu_impl_init(smmu);
2245c7469c6SJordan Crouse
225e86d1aa8SWill Deacon if (of_device_is_compatible(np, "marvell,ap806-smmu-500"))
226e86d1aa8SWill Deacon smmu->impl = &mrvl_mmu500_impl;
227e86d1aa8SWill Deacon
228e86d1aa8SWill Deacon return smmu;
229e86d1aa8SWill Deacon }
230