psci.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016
  4. * Author: Chen-Yu Tsai <wens@csie.org>
  5. *
  6. * Based on assembly code by Marc Zyngier <marc.zyngier@arm.com>,
  7. * which was based on code by Carl van Schaik <carl@ok-labs.com>.
  8. */
  9. #include <config.h>
  10. #include <common.h>
  11. #include <asm/arch/cpu.h>
  12. #include <asm/arch/cpucfg.h>
  13. #include <asm/arch/prcm.h>
  14. #include <asm/armv7.h>
  15. #include <asm/gic.h>
  16. #include <asm/io.h>
  17. #include <asm/psci.h>
  18. #include <asm/secure.h>
  19. #include <asm/system.h>
  20. #include <linux/bitops.h>
  21. #define __irq __attribute__ ((interrupt ("IRQ")))
  22. #define GICD_BASE (SUNXI_GIC400_BASE + GIC_DIST_OFFSET)
  23. #define GICC_BASE (SUNXI_GIC400_BASE + GIC_CPU_OFFSET_A15)
  24. /*
  25. * R40 is different from other single cluster SoCs.
  26. *
  27. * The power clamps are located in the unused space after the per-core
  28. * reset controls for core 3. The secondary core entry address register
  29. * is in the SRAM controller address range.
  30. */
  31. #define SUN8I_R40_PWROFF (0x110)
  32. #define SUN8I_R40_PWR_CLAMP(cpu) (0x120 + (cpu) * 0x4)
  33. #define SUN8I_R40_SRAMC_SOFT_ENTRY_REG0 (0xbc)
  34. static void __secure cp15_write_cntp_tval(u32 tval)
  35. {
  36. asm volatile ("mcr p15, 0, %0, c14, c2, 0" : : "r" (tval));
  37. }
  38. static void __secure cp15_write_cntp_ctl(u32 val)
  39. {
  40. asm volatile ("mcr p15, 0, %0, c14, c2, 1" : : "r" (val));
  41. }
  42. static u32 __secure cp15_read_cntp_ctl(void)
  43. {
  44. u32 val;
  45. asm volatile ("mrc p15, 0, %0, c14, c2, 1" : "=r" (val));
  46. return val;
  47. }
  48. #define ONE_MS (COUNTER_FREQUENCY / 1000)
  49. static void __secure __mdelay(u32 ms)
  50. {
  51. u32 reg = ONE_MS * ms;
  52. cp15_write_cntp_tval(reg);
  53. isb();
  54. cp15_write_cntp_ctl(3);
  55. do {
  56. isb();
  57. reg = cp15_read_cntp_ctl();
  58. } while (!(reg & BIT(2)));
  59. cp15_write_cntp_ctl(0);
  60. isb();
  61. }
  62. static void __secure clamp_release(u32 __maybe_unused *clamp)
  63. {
  64. #if defined(CONFIG_MACH_SUN6I) || defined(CONFIG_MACH_SUN7I) || \
  65. defined(CONFIG_MACH_SUN8I_H3) || \
  66. defined(CONFIG_MACH_SUN8I_R40)
  67. u32 tmp = 0x1ff;
  68. do {
  69. tmp >>= 1;
  70. writel(tmp, clamp);
  71. } while (tmp);
  72. __mdelay(10);
  73. #endif
  74. }
  75. static void __secure clamp_set(u32 __maybe_unused *clamp)
  76. {
  77. #if defined(CONFIG_MACH_SUN6I) || defined(CONFIG_MACH_SUN7I) || \
  78. defined(CONFIG_MACH_SUN8I_H3) || \
  79. defined(CONFIG_MACH_SUN8I_R40)
  80. writel(0xff, clamp);
  81. #endif
  82. }
  83. static void __secure sunxi_power_switch(u32 *clamp, u32 *pwroff, bool on,
  84. int cpu)
  85. {
  86. if (on) {
  87. /* Release power clamp */
  88. clamp_release(clamp);
  89. /* Clear power gating */
  90. clrbits_le32(pwroff, BIT(cpu));
  91. } else {
  92. /* Set power gating */
  93. setbits_le32(pwroff, BIT(cpu));
  94. /* Activate power clamp */
  95. clamp_set(clamp);
  96. }
  97. }
  98. #ifdef CONFIG_MACH_SUN8I_R40
  99. /* secondary core entry address is programmed differently on R40 */
  100. static void __secure sunxi_set_entry_address(void *entry)
  101. {
  102. writel((u32)entry,
  103. SUNXI_SRAMC_BASE + SUN8I_R40_SRAMC_SOFT_ENTRY_REG0);
  104. }
  105. #else
  106. static void __secure sunxi_set_entry_address(void *entry)
  107. {
  108. struct sunxi_cpucfg_reg *cpucfg =
  109. (struct sunxi_cpucfg_reg *)SUNXI_CPUCFG_BASE;
  110. writel((u32)entry, &cpucfg->priv0);
  111. }
  112. #endif
  113. #ifdef CONFIG_MACH_SUN7I
  114. /* sun7i (A20) is different from other single cluster SoCs */
  115. static void __secure sunxi_cpu_set_power(int __always_unused cpu, bool on)
  116. {
  117. struct sunxi_cpucfg_reg *cpucfg =
  118. (struct sunxi_cpucfg_reg *)SUNXI_CPUCFG_BASE;
  119. sunxi_power_switch(&cpucfg->cpu1_pwr_clamp, &cpucfg->cpu1_pwroff,
  120. on, 0);
  121. }
  122. #elif defined CONFIG_MACH_SUN8I_R40
  123. static void __secure sunxi_cpu_set_power(int cpu, bool on)
  124. {
  125. struct sunxi_cpucfg_reg *cpucfg =
  126. (struct sunxi_cpucfg_reg *)SUNXI_CPUCFG_BASE;
  127. sunxi_power_switch((void *)cpucfg + SUN8I_R40_PWR_CLAMP(cpu),
  128. (void *)cpucfg + SUN8I_R40_PWROFF,
  129. on, 0);
  130. }
  131. #else /* ! CONFIG_MACH_SUN7I && ! CONFIG_MACH_SUN8I_R40 */
  132. static void __secure sunxi_cpu_set_power(int cpu, bool on)
  133. {
  134. struct sunxi_prcm_reg *prcm =
  135. (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
  136. sunxi_power_switch(&prcm->cpu_pwr_clamp[cpu], &prcm->cpu_pwroff,
  137. on, cpu);
  138. }
  139. #endif /* CONFIG_MACH_SUN7I */
  140. void __secure sunxi_cpu_power_off(u32 cpuid)
  141. {
  142. struct sunxi_cpucfg_reg *cpucfg =
  143. (struct sunxi_cpucfg_reg *)SUNXI_CPUCFG_BASE;
  144. u32 cpu = cpuid & 0x3;
  145. /* Wait for the core to enter WFI */
  146. while (1) {
  147. if (readl(&cpucfg->cpu[cpu].status) & BIT(2))
  148. break;
  149. __mdelay(1);
  150. }
  151. /* Assert reset on target CPU */
  152. writel(0, &cpucfg->cpu[cpu].rst);
  153. /* Lock CPU (Disable external debug access) */
  154. clrbits_le32(&cpucfg->dbg_ctrl1, BIT(cpu));
  155. /* Power down CPU */
  156. sunxi_cpu_set_power(cpuid, false);
  157. /* Unlock CPU (Disable external debug access) */
  158. setbits_le32(&cpucfg->dbg_ctrl1, BIT(cpu));
  159. }
  160. static u32 __secure cp15_read_scr(void)
  161. {
  162. u32 scr;
  163. asm volatile ("mrc p15, 0, %0, c1, c1, 0" : "=r" (scr));
  164. return scr;
  165. }
  166. static void __secure cp15_write_scr(u32 scr)
  167. {
  168. asm volatile ("mcr p15, 0, %0, c1, c1, 0" : : "r" (scr));
  169. isb();
  170. }
  171. /*
  172. * Although this is an FIQ handler, the FIQ is processed in monitor mode,
  173. * which means there's no FIQ banked registers. This is the same as IRQ
  174. * mode, so use the IRQ attribute to ask the compiler to handler entry
  175. * and return.
  176. */
  177. void __secure __irq psci_fiq_enter(void)
  178. {
  179. u32 scr, reg, cpu;
  180. /* Switch to secure mode */
  181. scr = cp15_read_scr();
  182. cp15_write_scr(scr & ~BIT(0));
  183. /* Validate reason based on IAR and acknowledge */
  184. reg = readl(GICC_BASE + GICC_IAR);
  185. /* Skip spurious interrupts 1022 and 1023 */
  186. if (reg == 1023 || reg == 1022)
  187. goto out;
  188. /* End of interrupt */
  189. writel(reg, GICC_BASE + GICC_EOIR);
  190. dsb();
  191. /* Get CPU number */
  192. cpu = (reg >> 10) & 0x7;
  193. /* Power off the CPU */
  194. sunxi_cpu_power_off(cpu);
  195. out:
  196. /* Restore security level */
  197. cp15_write_scr(scr);
  198. }
  199. int __secure psci_cpu_on(u32 __always_unused unused, u32 mpidr, u32 pc,
  200. u32 context_id)
  201. {
  202. struct sunxi_cpucfg_reg *cpucfg =
  203. (struct sunxi_cpucfg_reg *)SUNXI_CPUCFG_BASE;
  204. u32 cpu = (mpidr & 0x3);
  205. /* store target PC and context id */
  206. psci_save(cpu, pc, context_id);
  207. /* Set secondary core power on PC */
  208. sunxi_set_entry_address(&psci_cpu_entry);
  209. /* Assert reset on target CPU */
  210. writel(0, &cpucfg->cpu[cpu].rst);
  211. /* Invalidate L1 cache */
  212. clrbits_le32(&cpucfg->gen_ctrl, BIT(cpu));
  213. /* Lock CPU (Disable external debug access) */
  214. clrbits_le32(&cpucfg->dbg_ctrl1, BIT(cpu));
  215. /* Power up target CPU */
  216. sunxi_cpu_set_power(cpu, true);
  217. /* De-assert reset on target CPU */
  218. writel(BIT(1) | BIT(0), &cpucfg->cpu[cpu].rst);
  219. /* Unlock CPU (Disable external debug access) */
  220. setbits_le32(&cpucfg->dbg_ctrl1, BIT(cpu));
  221. return ARM_PSCI_RET_SUCCESS;
  222. }
  223. void __secure psci_cpu_off(void)
  224. {
  225. psci_cpu_off_common();
  226. /* Ask CPU0 via SGI15 to pull the rug... */
  227. writel(BIT(16) | 15, GICD_BASE + GICD_SGIR);
  228. dsb();
  229. /* Wait to be turned off */
  230. while (1)
  231. wfi();
  232. }
  233. void __secure psci_arch_init(void)
  234. {
  235. u32 reg;
  236. /* SGI15 as Group-0 */
  237. clrbits_le32(GICD_BASE + GICD_IGROUPRn, BIT(15));
  238. /* Set SGI15 priority to 0 */
  239. writeb(0, GICD_BASE + GICD_IPRIORITYRn + 15);
  240. /* Be cool with non-secure */
  241. writel(0xff, GICC_BASE + GICC_PMR);
  242. /* Switch FIQEn on */
  243. setbits_le32(GICC_BASE + GICC_CTLR, BIT(3));
  244. reg = cp15_read_scr();
  245. reg |= BIT(2); /* Enable FIQ in monitor mode */
  246. reg &= ~BIT(0); /* Secure mode */
  247. cp15_write_scr(reg);
  248. }