1fc058d37SRobin Murphy // SPDX-License-Identifier: GPL-2.0-only
2fc058d37SRobin Murphy // Miscellaneous Arm SMMU implementation and integration quirks
3fc058d37SRobin Murphy // Copyright (C) 2019 Arm Limited
4fc058d37SRobin Murphy
5fc058d37SRobin Murphy #define pr_fmt(fmt) "arm-smmu: " fmt
6fc058d37SRobin Murphy
762b993a3SRobin Murphy #include <linux/bitfield.h>
86d7dff62SRobin Murphy #include <linux/of.h>
96d7dff62SRobin Murphy
10fc058d37SRobin Murphy #include "arm-smmu.h"
11fc058d37SRobin Murphy
12fc058d37SRobin Murphy
arm_smmu_gr0_ns(int offset)136d7dff62SRobin Murphy static int arm_smmu_gr0_ns(int offset)
146d7dff62SRobin Murphy {
156d7dff62SRobin Murphy switch (offset) {
166d7dff62SRobin Murphy case ARM_SMMU_GR0_sCR0:
176d7dff62SRobin Murphy case ARM_SMMU_GR0_sACR:
186d7dff62SRobin Murphy case ARM_SMMU_GR0_sGFSR:
196d7dff62SRobin Murphy case ARM_SMMU_GR0_sGFSYNR0:
206d7dff62SRobin Murphy case ARM_SMMU_GR0_sGFSYNR1:
216d7dff62SRobin Murphy case ARM_SMMU_GR0_sGFSYNR2:
226d7dff62SRobin Murphy return offset + 0x400;
236d7dff62SRobin Murphy default:
246d7dff62SRobin Murphy return offset;
256d7dff62SRobin Murphy }
266d7dff62SRobin Murphy }
276d7dff62SRobin Murphy
arm_smmu_read_ns(struct arm_smmu_device * smmu,int page,int offset)286d7dff62SRobin Murphy static u32 arm_smmu_read_ns(struct arm_smmu_device *smmu, int page,
296d7dff62SRobin Murphy int offset)
306d7dff62SRobin Murphy {
316d7dff62SRobin Murphy if (page == ARM_SMMU_GR0)
326d7dff62SRobin Murphy offset = arm_smmu_gr0_ns(offset);
336d7dff62SRobin Murphy return readl_relaxed(arm_smmu_page(smmu, page) + offset);
346d7dff62SRobin Murphy }
356d7dff62SRobin Murphy
arm_smmu_write_ns(struct arm_smmu_device * smmu,int page,int offset,u32 val)366d7dff62SRobin Murphy static void arm_smmu_write_ns(struct arm_smmu_device *smmu, int page,
376d7dff62SRobin Murphy int offset, u32 val)
386d7dff62SRobin Murphy {
396d7dff62SRobin Murphy if (page == ARM_SMMU_GR0)
406d7dff62SRobin Murphy offset = arm_smmu_gr0_ns(offset);
416d7dff62SRobin Murphy writel_relaxed(val, arm_smmu_page(smmu, page) + offset);
426d7dff62SRobin Murphy }
436d7dff62SRobin Murphy
446d7dff62SRobin Murphy /* Since we don't care for sGFAR, we can do without 64-bit accessors */
454b67f1ddSWill Deacon static const struct arm_smmu_impl calxeda_impl = {
466d7dff62SRobin Murphy .read_reg = arm_smmu_read_ns,
476d7dff62SRobin Murphy .write_reg = arm_smmu_write_ns,
486d7dff62SRobin Murphy };
496d7dff62SRobin Murphy
506d7dff62SRobin Murphy
51ba7e4a08SRobin Murphy struct cavium_smmu {
52ba7e4a08SRobin Murphy struct arm_smmu_device smmu;
53ba7e4a08SRobin Murphy u32 id_base;
54ba7e4a08SRobin Murphy };
55ba7e4a08SRobin Murphy
cavium_cfg_probe(struct arm_smmu_device * smmu)563995e186SRobin Murphy static int cavium_cfg_probe(struct arm_smmu_device *smmu)
573995e186SRobin Murphy {
583995e186SRobin Murphy static atomic_t context_count = ATOMIC_INIT(0);
59ba7e4a08SRobin Murphy struct cavium_smmu *cs = container_of(smmu, struct cavium_smmu, smmu);
603995e186SRobin Murphy /*
613995e186SRobin Murphy * Cavium CN88xx erratum #27704.
623995e186SRobin Murphy * Ensure ASID and VMID allocation is unique across all SMMUs in
633995e186SRobin Murphy * the system.
643995e186SRobin Murphy */
65ba7e4a08SRobin Murphy cs->id_base = atomic_fetch_add(smmu->num_context_banks, &context_count);
663995e186SRobin Murphy dev_notice(smmu->dev, "\tenabling workaround for Cavium erratum 27704\n");
673995e186SRobin Murphy
683995e186SRobin Murphy return 0;
693995e186SRobin Murphy }
703995e186SRobin Murphy
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)
73ba7e4a08SRobin Murphy {
74ba7e4a08SRobin Murphy struct cavium_smmu *cs = container_of(smmu_domain->smmu,
75ba7e4a08SRobin Murphy struct cavium_smmu, smmu);
76ba7e4a08SRobin Murphy
77ba7e4a08SRobin Murphy if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
78ba7e4a08SRobin Murphy smmu_domain->cfg.vmid += cs->id_base;
79ba7e4a08SRobin Murphy else
80ba7e4a08SRobin Murphy smmu_domain->cfg.asid += cs->id_base;
81ba7e4a08SRobin Murphy
82ba7e4a08SRobin Murphy return 0;
83ba7e4a08SRobin Murphy }
84ba7e4a08SRobin Murphy
854b67f1ddSWill Deacon static const struct arm_smmu_impl cavium_impl = {
863995e186SRobin Murphy .cfg_probe = cavium_cfg_probe,
87ba7e4a08SRobin Murphy .init_context = cavium_init_context,
883995e186SRobin Murphy };
893995e186SRobin Murphy
cavium_smmu_impl_init(struct arm_smmu_device * smmu)904b67f1ddSWill Deacon static struct arm_smmu_device *cavium_smmu_impl_init(struct arm_smmu_device *smmu)
91ba7e4a08SRobin Murphy {
92ba7e4a08SRobin Murphy struct cavium_smmu *cs;
93ba7e4a08SRobin Murphy
94af9da914SRobin Murphy cs = devm_krealloc(smmu->dev, smmu, sizeof(*cs), GFP_KERNEL);
95ba7e4a08SRobin Murphy if (!cs)
96ba7e4a08SRobin Murphy return ERR_PTR(-ENOMEM);
97ba7e4a08SRobin Murphy
98ba7e4a08SRobin Murphy cs->smmu.impl = &cavium_impl;
99ba7e4a08SRobin Murphy
100ba7e4a08SRobin Murphy return &cs->smmu;
101ba7e4a08SRobin Murphy }
102ba7e4a08SRobin Murphy
1033995e186SRobin Murphy
10462b993a3SRobin Murphy #define ARM_MMU500_ACTLR_CPRE (1 << 1)
10562b993a3SRobin Murphy
10662b993a3SRobin Murphy #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
10762b993a3SRobin Murphy #define ARM_MMU500_ACR_S2CRB_TLBEN (1 << 10)
10862b993a3SRobin Murphy #define ARM_MMU500_ACR_SMTNMB_TLBEN (1 << 8)
10962b993a3SRobin Murphy
arm_mmu500_reset(struct arm_smmu_device * smmu)110759aaa10SVivek Gautam int arm_mmu500_reset(struct arm_smmu_device *smmu)
11162b993a3SRobin Murphy {
11262b993a3SRobin Murphy u32 reg, major;
11362b993a3SRobin Murphy /*
11462b993a3SRobin Murphy * On MMU-500 r2p0 onwards we need to clear ACR.CACHE_LOCK before
11562b993a3SRobin Murphy * writes to the context bank ACTLRs will stick. And we just hope that
11662b993a3SRobin Murphy * Secure has also cleared SACR.CACHE_LOCK for this to take effect...
11762b993a3SRobin Murphy */
11862b993a3SRobin Murphy reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID7);
119fba6e960SWill Deacon major = FIELD_GET(ARM_SMMU_ID7_MAJOR, reg);
12062b993a3SRobin Murphy reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sACR);
12162b993a3SRobin Murphy if (major >= 2)
12262b993a3SRobin Murphy reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
12362b993a3SRobin Murphy /*
12462b993a3SRobin Murphy * Allow unmatched Stream IDs to allocate bypass
12562b993a3SRobin Murphy * TLB entries for reduced latency.
12662b993a3SRobin Murphy */
12762b993a3SRobin Murphy reg |= ARM_MMU500_ACR_SMTNMB_TLBEN | ARM_MMU500_ACR_S2CRB_TLBEN;
12862b993a3SRobin Murphy arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sACR, reg);
12962b993a3SRobin Murphy
130*ef4144b1SBibek Kumar Patro #ifdef CONFIG_ARM_SMMU_MMU_500_CPRE_ERRATA
13162b993a3SRobin Murphy /*
13262b993a3SRobin Murphy * Disable MMU-500's not-particularly-beneficial next-page
1330dfe314cSRobin Murphy * prefetcher for the sake of at least 5 known errata.
13462b993a3SRobin Murphy */
135*ef4144b1SBibek Kumar Patro for (int i = 0; i < smmu->num_context_banks; ++i) {
13662b993a3SRobin Murphy reg = arm_smmu_cb_read(smmu, i, ARM_SMMU_CB_ACTLR);
13762b993a3SRobin Murphy reg &= ~ARM_MMU500_ACTLR_CPRE;
13862b993a3SRobin Murphy 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)
1410dfe314cSRobin Murphy dev_warn_once(smmu->dev, "Failed to disable prefetcher for errata workarounds, check SACR.CACHE_LOCK\n");
14262b993a3SRobin Murphy }
143*ef4144b1SBibek Kumar Patro #endif
14462b993a3SRobin Murphy
14562b993a3SRobin Murphy return 0;
14662b993a3SRobin Murphy }
14762b993a3SRobin Murphy
1484b67f1ddSWill Deacon static const struct arm_smmu_impl arm_mmu500_impl = {
14962b993a3SRobin Murphy .reset = arm_mmu500_reset,
15062b993a3SRobin Murphy };
15162b993a3SRobin Murphy
mrvl_mmu500_readq(struct arm_smmu_device * smmu,int page,int off)152f2d9848aSHanna Hawa static u64 mrvl_mmu500_readq(struct arm_smmu_device *smmu, int page, int off)
153f2d9848aSHanna Hawa {
154f2d9848aSHanna Hawa /*
155f2d9848aSHanna Hawa * Marvell Armada-AP806 erratum #582743.
156f2d9848aSHanna Hawa * Split all the readq to double readl
157f2d9848aSHanna Hawa */
158f2d9848aSHanna Hawa return hi_lo_readq_relaxed(arm_smmu_page(smmu, page) + off);
159f2d9848aSHanna Hawa }
160f2d9848aSHanna Hawa
mrvl_mmu500_writeq(struct arm_smmu_device * smmu,int page,int off,u64 val)161f2d9848aSHanna Hawa static void mrvl_mmu500_writeq(struct arm_smmu_device *smmu, int page, int off,
162f2d9848aSHanna Hawa u64 val)
163f2d9848aSHanna Hawa {
164f2d9848aSHanna Hawa /*
165f2d9848aSHanna Hawa * Marvell Armada-AP806 erratum #582743.
166f2d9848aSHanna Hawa * Split all the writeq to double writel
167f2d9848aSHanna Hawa */
168f2d9848aSHanna Hawa hi_lo_writeq_relaxed(val, arm_smmu_page(smmu, page) + off);
169f2d9848aSHanna Hawa }
170f2d9848aSHanna Hawa
mrvl_mmu500_cfg_probe(struct arm_smmu_device * smmu)171f2d9848aSHanna Hawa static int mrvl_mmu500_cfg_probe(struct arm_smmu_device *smmu)
172f2d9848aSHanna Hawa {
173f2d9848aSHanna Hawa
174f2d9848aSHanna Hawa /*
175f2d9848aSHanna Hawa * Armada-AP806 erratum #582743.
176f2d9848aSHanna Hawa * Hide the SMMU_IDR2.PTFSv8 fields to sidestep the AArch64
177f2d9848aSHanna Hawa * formats altogether and allow using 32 bits access on the
178f2d9848aSHanna Hawa * interconnect.
179f2d9848aSHanna Hawa */
180f2d9848aSHanna Hawa smmu->features &= ~(ARM_SMMU_FEAT_FMT_AARCH64_4K |
181f2d9848aSHanna Hawa ARM_SMMU_FEAT_FMT_AARCH64_16K |
182f2d9848aSHanna Hawa ARM_SMMU_FEAT_FMT_AARCH64_64K);
183f2d9848aSHanna Hawa
184f2d9848aSHanna Hawa return 0;
185f2d9848aSHanna Hawa }
186f2d9848aSHanna Hawa
187f2d9848aSHanna Hawa static const struct arm_smmu_impl mrvl_mmu500_impl = {
188f2d9848aSHanna Hawa .read_reg64 = mrvl_mmu500_readq,
189f2d9848aSHanna Hawa .write_reg64 = mrvl_mmu500_writeq,
190f2d9848aSHanna Hawa .cfg_probe = mrvl_mmu500_cfg_probe,
191f2d9848aSHanna Hawa .reset = arm_mmu500_reset,
192f2d9848aSHanna Hawa };
193f2d9848aSHanna Hawa
19462b993a3SRobin Murphy
arm_smmu_impl_init(struct arm_smmu_device * smmu)195fc058d37SRobin Murphy struct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu)
196fc058d37SRobin Murphy {
19764510edeSSai Prakash Ranjan const struct device_node *np = smmu->dev->of_node;
19864510edeSSai Prakash Ranjan
1993995e186SRobin Murphy /*
20097649292SRobin Murphy * Set the impl for model-specific implementation quirks first,
20197649292SRobin Murphy * such that platform integration quirks can pick it up and
20297649292SRobin Murphy * inherit from it if necessary.
2033995e186SRobin Murphy */
2043995e186SRobin Murphy switch (smmu->model) {
20562b993a3SRobin Murphy case ARM_MMU500:
20662b993a3SRobin Murphy smmu->impl = &arm_mmu500_impl;
20762b993a3SRobin Murphy break;
2083995e186SRobin Murphy case CAVIUM_SMMUV2:
209ba7e4a08SRobin Murphy return cavium_smmu_impl_init(smmu);
2103995e186SRobin Murphy default:
2113995e186SRobin Murphy break;
2123995e186SRobin Murphy }
2133995e186SRobin Murphy
21497649292SRobin Murphy /* This is implicitly MMU-400 */
21564510edeSSai Prakash Ranjan if (of_property_read_bool(np, "calxeda,smmu-secure-config-access"))
2166d7dff62SRobin Murphy smmu->impl = &calxeda_impl;
2176d7dff62SRobin Murphy
2185ca21615SThierry Reding if (of_device_is_compatible(np, "nvidia,tegra234-smmu") ||
2195ca21615SThierry Reding of_device_is_compatible(np, "nvidia,tegra194-smmu") ||
2202c1bc371SThierry Reding of_device_is_compatible(np, "nvidia,tegra186-smmu"))
221aab5a1c8SKrishna Reddy return nvidia_smmu_impl_init(smmu);
222aab5a1c8SKrishna Reddy
223424953cfSArnd Bergmann if (IS_ENABLED(CONFIG_ARM_SMMU_QCOM))
22400597f9fSSai Prakash Ranjan smmu = qcom_smmu_impl_init(smmu);
2255c7469c6SJordan Crouse
226f2d9848aSHanna Hawa if (of_device_is_compatible(np, "marvell,ap806-smmu-500"))
227f2d9848aSHanna Hawa smmu->impl = &mrvl_mmu500_impl;
228f2d9848aSHanna Hawa
229fc058d37SRobin Murphy return smmu;
230fc058d37SRobin Murphy }
231