arm-smmu-impl.c 5.9 KB

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