msm8916_config.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2021-2023, Stephan Gerhold <stephan@gerhold.net>
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <arch.h>
  8. #include <drivers/arm/cci.h>
  9. #include <lib/mmio.h>
  10. #include "msm8916_config.h"
  11. #include "msm8916_gicv2.h"
  12. #include <msm8916_mmap.h>
  13. #include <platform_def.h>
  14. static const int cci_map[] = { 3, 4 };
  15. void msm8916_configure_early(void)
  16. {
  17. if (PLATFORM_CLUSTER_COUNT > 1) {
  18. cci_init(APCS_CCI_BASE, cci_map, ARRAY_SIZE(cci_map));
  19. cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1()));
  20. }
  21. }
  22. static void msm8916_configure_timer(uintptr_t base)
  23. {
  24. /* Set timer frequency */
  25. mmio_write_32(base + CNTCTLBASE_CNTFRQ, PLAT_SYSCNT_FREQ);
  26. /* Make all timer frames available to non-secure world */
  27. mmio_write_32(base + CNTNSAR, GENMASK_32(7, 0));
  28. }
  29. /*
  30. * The APCS register regions always start with a SECURE register that should
  31. * be cleared to 0 to only allow secure access. Since BL31 handles most of
  32. * the CPU power management, most of them can be cleared to secure access only.
  33. */
  34. #define APCS_GLB_SECURE_STS_NS BIT_32(0)
  35. #define APCS_GLB_SECURE_PWR_NS BIT_32(1)
  36. #if PLATFORM_CORE_COUNT > 1
  37. #define APCS_BOOT_START_ADDR_SEC 0x04
  38. #define APCS_AA64NAA32_REG 0x0c
  39. #else
  40. #define APCS_BOOT_START_ADDR_SEC 0x18
  41. #endif
  42. #define REMAP_EN BIT_32(0)
  43. static void msm8916_configure_apcs_cluster(unsigned int cluster)
  44. {
  45. uintptr_t cfg = APCS_CFG(cluster);
  46. unsigned int cpu;
  47. /* Disallow non-secure access to boot remapper / TCM registers */
  48. mmio_write_32(cfg, 0);
  49. /*
  50. * Disallow non-secure access to power management registers.
  51. * However, allow STS and PWR since those also seem to control access
  52. * to CPU frequency related registers (e.g. APCS_CMD_RCGR). If these
  53. * bits are not set, CPU frequency control fails in the non-secure world.
  54. */
  55. mmio_write_32(APCS_GLB(cluster),
  56. APCS_GLB_SECURE_STS_NS | APCS_GLB_SECURE_PWR_NS);
  57. if (PLATFORM_CORE_COUNT > 1) {
  58. /* Disallow non-secure access to L2 SAW2 */
  59. mmio_write_32(APCS_L2_SAW2(cluster), 0);
  60. /* Disallow non-secure access to CPU ACS and SAW2 */
  61. for (cpu = 0; cpu < PLATFORM_CPUS_PER_CLUSTER; cpu++) {
  62. mmio_write_32(APCS_ALIAS_ACS(cluster, cpu), 0);
  63. mmio_write_32(APCS_ALIAS_SAW2(cluster, cpu), 0);
  64. }
  65. } else {
  66. /* There is just one core so no aliases exist */
  67. mmio_write_32(APCS_BANKED_ACS, 0);
  68. mmio_write_32(APCS_BANKED_SAW2, 0);
  69. }
  70. #ifdef __aarch64__
  71. /* Make sure all further warm boots end up in BL31 and aarch64 state */
  72. CASSERT((BL31_BASE & 0xffff) == 0, assert_bl31_base_64k_aligned);
  73. mmio_write_32(cfg + APCS_BOOT_START_ADDR_SEC, BL31_BASE | REMAP_EN);
  74. mmio_write_32(cfg + APCS_AA64NAA32_REG, 1);
  75. #else
  76. /* Make sure all further warm boots end up in BL32 */
  77. CASSERT((BL32_BASE & 0xffff) == 0, assert_bl32_base_64k_aligned);
  78. mmio_write_32(cfg + APCS_BOOT_START_ADDR_SEC, BL32_BASE | REMAP_EN);
  79. #endif
  80. msm8916_configure_timer(APCS_QTMR(cluster));
  81. }
  82. static void msm8916_configure_apcs(void)
  83. {
  84. unsigned int cluster;
  85. for (cluster = 0; cluster < PLATFORM_CLUSTER_COUNT; cluster++) {
  86. msm8916_configure_apcs_cluster(cluster);
  87. }
  88. if (PLATFORM_CLUSTER_COUNT > 1) {
  89. /* Disallow non-secure access to CCI ACS and SAW2 */
  90. mmio_write_32(APCS_CCI_ACS, 0);
  91. mmio_write_32(APCS_CCI_SAW2, 0);
  92. }
  93. }
  94. /*
  95. * MSM8916 has a special "interrupt aggregation logic" in the APPS SMMU,
  96. * which allows routing context bank interrupts to one of 3 interrupt numbers
  97. * ("TZ/HYP/NS"). Route all interrupts to the non-secure interrupt number
  98. * by default to avoid special setup on the non-secure side.
  99. */
  100. #define CLK_OFF BIT_32(31)
  101. #define GCC_APSS_TCU_CBCR (GCC_BASE + 0x12018)
  102. #define GCC_GFX_TCU_CBCR (GCC_BASE + 0x12020)
  103. #define GCC_SMMU_CFG_CBCR (GCC_BASE + 0x12038)
  104. #define GCC_RPM_SMMU_CLOCK_BRANCH_ENA_VOTE (GCC_BASE + 0x3600c)
  105. #define GCC_APCS_SMMU_CLOCK_BRANCH_ENA_VOTE (GCC_BASE + 0x4500c)
  106. #define APSS_TCU_CLK_ENA BIT_32(1)
  107. #define GFX_TCU_CLK_ENA BIT_32(2)
  108. #define GFX_TBU_CLK_ENA BIT_32(3)
  109. #define SMMU_CFG_CLK_ENA BIT_32(12)
  110. #define APPS_SMMU_INTR_SEL_NS (APPS_SMMU_QCOM + 0x2000)
  111. #define APPS_SMMU_INTR_SEL_NS_EN_ALL U(0xffffffff)
  112. #define SMMU_SACR 0x010
  113. #define SMMU_SACR_CACHE_LOCK BIT_32(26)
  114. #define SMMU_IDR7 0x03c
  115. #define SMMU_IDR7_MINOR(val) (((val) >> 0) & 0xf)
  116. #define SMMU_IDR7_MAJOR(val) (((val) >> 4) & 0xf)
  117. static void msm8916_smmu_cache_unlock(uintptr_t smmu_base, uintptr_t clk_cbcr)
  118. {
  119. uint32_t version;
  120. /* Wait for clock */
  121. while (mmio_read_32(clk_cbcr) & CLK_OFF) {
  122. }
  123. version = mmio_read_32(smmu_base + SMMU_IDR7);
  124. VERBOSE("SMMU(0x%lx) r%dp%d\n", smmu_base,
  125. SMMU_IDR7_MAJOR(version), SMMU_IDR7_MINOR(version));
  126. /* For SMMU r2p0+ clear CACHE_LOCK to allow writes to CBn_ACTLR */
  127. if (SMMU_IDR7_MAJOR(version) >= 2) {
  128. mmio_clrbits_32(smmu_base + SMMU_SACR, SMMU_SACR_CACHE_LOCK);
  129. }
  130. }
  131. static void msm8916_configure_smmu(void)
  132. {
  133. uint32_t ena_bits = APSS_TCU_CLK_ENA | SMMU_CFG_CLK_ENA;
  134. /* Single core (MDM) platforms do not have a GPU */
  135. if (PLATFORM_CORE_COUNT > 1) {
  136. ena_bits |= GFX_TCU_CLK_ENA | GFX_TBU_CLK_ENA;
  137. }
  138. /* Enable SMMU clocks to enable register access */
  139. mmio_write_32(GCC_APCS_SMMU_CLOCK_BRANCH_ENA_VOTE, ena_bits);
  140. /* Wait for configuration clock */
  141. while (mmio_read_32(GCC_SMMU_CFG_CBCR) & CLK_OFF) {
  142. }
  143. /* Route all context bank interrupts to non-secure interrupt */
  144. mmio_write_32(APPS_SMMU_INTR_SEL_NS, APPS_SMMU_INTR_SEL_NS_EN_ALL);
  145. /* Clear sACR.CACHE_LOCK bit if needed for MMU-500 r2p0+ */
  146. msm8916_smmu_cache_unlock(APPS_SMMU_BASE, GCC_APSS_TCU_CBCR);
  147. if (PLATFORM_CORE_COUNT > 1) {
  148. msm8916_smmu_cache_unlock(GPU_SMMU_BASE, GCC_GFX_TCU_CBCR);
  149. }
  150. /*
  151. * Keep APCS vote for SMMU clocks for rest of booting process, but make
  152. * sure other vote registers (such as RPM) do not keep permanent votes.
  153. */
  154. VERBOSE("Clearing GCC_RPM_SMMU_CLOCK_BRANCH_ENA_VOTE (was: 0x%x)\n",
  155. mmio_read_32(GCC_RPM_SMMU_CLOCK_BRANCH_ENA_VOTE));
  156. mmio_write_32(GCC_RPM_SMMU_CLOCK_BRANCH_ENA_VOTE, 0);
  157. }
  158. void msm8916_configure(void)
  159. {
  160. msm8916_gicv2_configure();
  161. msm8916_configure_apcs();
  162. msm8916_configure_smmu();
  163. }