nonsec_virt.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * code for switching cores into non-secure state and into HYP mode
  3. *
  4. * Copyright (c) 2013 Andre Przywara <andre.przywara@linaro.org>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <config.h>
  9. #include <linux/linkage.h>
  10. #include <asm/gic.h>
  11. #include <asm/armv7.h>
  12. #include <asm/proc-armv/ptrace.h>
  13. .arch_extension sec
  14. .arch_extension virt
  15. .pushsection ._secure.text, "ax"
  16. .align 5
  17. /* the vector table for secure state and HYP mode */
  18. _monitor_vectors:
  19. .word 0 /* reset */
  20. .word 0 /* undef */
  21. adr pc, _secure_monitor
  22. .word 0
  23. .word 0
  24. .word 0
  25. .word 0
  26. .word 0
  27. .macro is_cpu_virt_capable tmp
  28. mrc p15, 0, \tmp, c0, c1, 1 @ read ID_PFR1
  29. and \tmp, \tmp, #CPUID_ARM_VIRT_MASK @ mask virtualization bits
  30. cmp \tmp, #(1 << CPUID_ARM_VIRT_SHIFT)
  31. .endm
  32. /*
  33. * secure monitor handler
  34. * U-boot calls this "software interrupt" in start.S
  35. * This is executed on a "smc" instruction, we use a "smc #0" to switch
  36. * to non-secure state.
  37. * r0, r1, r2: passed to the callee
  38. * ip: target PC
  39. */
  40. _secure_monitor:
  41. #ifdef CONFIG_ARMV7_PSCI
  42. ldr r5, =_psci_vectors @ Switch to the next monitor
  43. mcr p15, 0, r5, c12, c0, 1
  44. isb
  45. @ Obtain a secure stack, and configure the PSCI backend
  46. bl psci_arch_init
  47. #endif
  48. mrc p15, 0, r5, c1, c1, 0 @ read SCR
  49. bic r5, r5, #0x4a @ clear IRQ, EA, nET bits
  50. orr r5, r5, #0x31 @ enable NS, AW, FW bits
  51. @ FIQ preserved for secure mode
  52. mov r6, #SVC_MODE @ default mode is SVC
  53. is_cpu_virt_capable r4
  54. #ifdef CONFIG_ARMV7_VIRT
  55. orreq r5, r5, #0x100 @ allow HVC instruction
  56. moveq r6, #HYP_MODE @ Enter the kernel as HYP
  57. #endif
  58. mcr p15, 0, r5, c1, c1, 0 @ write SCR (with NS bit set)
  59. isb
  60. bne 1f
  61. @ Reset CNTVOFF to 0 before leaving monitor mode
  62. mrc p15, 0, r4, c0, c1, 1 @ read ID_PFR1
  63. ands r4, r4, #CPUID_ARM_GENTIMER_MASK @ test arch timer bits
  64. movne r4, #0
  65. mcrrne p15, 4, r4, r4, c14 @ Reset CNTVOFF to zero
  66. 1:
  67. mov lr, ip
  68. mov ip, #(F_BIT | I_BIT | A_BIT) @ Set A, I and F
  69. tst lr, #1 @ Check for Thumb PC
  70. orrne ip, ip, #T_BIT @ Set T if Thumb
  71. orr ip, ip, r6 @ Slot target mode in
  72. msr spsr_cxfs, ip @ Set full SPSR
  73. movs pc, lr @ ERET to non-secure
  74. ENTRY(_do_nonsec_entry)
  75. mov ip, r0
  76. mov r0, r1
  77. mov r1, r2
  78. mov r2, r3
  79. smc #0
  80. ENDPROC(_do_nonsec_entry)
  81. .macro get_cbar_addr addr
  82. #ifdef CONFIG_ARM_GIC_BASE_ADDRESS
  83. ldr \addr, =CONFIG_ARM_GIC_BASE_ADDRESS
  84. #else
  85. mrc p15, 4, \addr, c15, c0, 0 @ read CBAR
  86. bfc \addr, #0, #15 @ clear reserved bits
  87. #endif
  88. .endm
  89. .macro get_gicd_addr addr
  90. get_cbar_addr \addr
  91. add \addr, \addr, #GIC_DIST_OFFSET @ GIC dist i/f offset
  92. .endm
  93. .macro get_gicc_addr addr, tmp
  94. get_cbar_addr \addr
  95. is_cpu_virt_capable \tmp
  96. movne \tmp, #GIC_CPU_OFFSET_A9 @ GIC CPU offset for A9
  97. moveq \tmp, #GIC_CPU_OFFSET_A15 @ GIC CPU offset for A15/A7
  98. add \addr, \addr, \tmp
  99. .endm
  100. #ifndef CONFIG_ARMV7_PSCI
  101. /*
  102. * Secondary CPUs start here and call the code for the core specific parts
  103. * of the non-secure and HYP mode transition. The GIC distributor specific
  104. * code has already been executed by a C function before.
  105. * Then they go back to wfi and wait to be woken up by the kernel again.
  106. */
  107. ENTRY(_smp_pen)
  108. cpsid i
  109. cpsid f
  110. bl _nonsec_init
  111. adr r0, _smp_pen @ do not use this address again
  112. b smp_waitloop @ wait for IPIs, board specific
  113. ENDPROC(_smp_pen)
  114. #endif
  115. /*
  116. * Switch a core to non-secure state.
  117. *
  118. * 1. initialize the GIC per-core interface
  119. * 2. allow coprocessor access in non-secure modes
  120. *
  121. * Called from smp_pen by secondary cores and directly by the BSP.
  122. * Do not assume that the stack is available and only use registers
  123. * r0-r3 and r12.
  124. *
  125. * PERIPHBASE is used to get the GIC address. This could be 40 bits long,
  126. * though, but we check this in C before calling this function.
  127. */
  128. ENTRY(_nonsec_init)
  129. get_gicd_addr r3
  130. mvn r1, #0 @ all bits to 1
  131. str r1, [r3, #GICD_IGROUPRn] @ allow private interrupts
  132. get_gicc_addr r3, r1
  133. mov r1, #3 @ Enable both groups
  134. str r1, [r3, #GICC_CTLR] @ and clear all other bits
  135. mov r1, #0xff
  136. str r1, [r3, #GICC_PMR] @ set priority mask register
  137. mrc p15, 0, r0, c1, c1, 2
  138. movw r1, #0x3fff
  139. movt r1, #0x0004
  140. orr r0, r0, r1
  141. mcr p15, 0, r0, c1, c1, 2 @ NSACR = all copros to non-sec
  142. /* The CNTFRQ register of the generic timer needs to be
  143. * programmed in secure state. Some primary bootloaders / firmware
  144. * omit this, so if the frequency is provided in the configuration,
  145. * we do this here instead.
  146. * But first check if we have the generic timer.
  147. */
  148. #ifdef CONFIG_TIMER_CLK_FREQ
  149. mrc p15, 0, r0, c0, c1, 1 @ read ID_PFR1
  150. and r0, r0, #CPUID_ARM_GENTIMER_MASK @ mask arch timer bits
  151. cmp r0, #(1 << CPUID_ARM_GENTIMER_SHIFT)
  152. ldreq r1, =CONFIG_TIMER_CLK_FREQ
  153. mcreq p15, 0, r1, c14, c0, 0 @ write CNTFRQ
  154. #endif
  155. adr r1, _monitor_vectors
  156. mcr p15, 0, r1, c12, c0, 1 @ set MVBAR to secure vectors
  157. isb
  158. mov r0, r3 @ return GICC address
  159. bx lr
  160. ENDPROC(_nonsec_init)
  161. #ifdef CONFIG_SMP_PEN_ADDR
  162. /* void __weak smp_waitloop(unsigned previous_address); */
  163. ENTRY(smp_waitloop)
  164. wfi
  165. ldr r1, =CONFIG_SMP_PEN_ADDR @ load start address
  166. ldr r1, [r1]
  167. #ifdef CONFIG_PEN_ADDR_BIG_ENDIAN
  168. rev r1, r1
  169. #endif
  170. cmp r0, r1 @ make sure we dont execute this code
  171. beq smp_waitloop @ again (due to a spurious wakeup)
  172. mov r0, r1
  173. b _do_nonsec_entry
  174. ENDPROC(smp_waitloop)
  175. .weak smp_waitloop
  176. #endif
  177. .popsection