nonsec_virt.S 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. .arch_extension sec
  13. .arch_extension virt
  14. /* the vector table for secure state and HYP mode */
  15. _monitor_vectors:
  16. .word 0 /* reset */
  17. .word 0 /* undef */
  18. adr pc, _secure_monitor
  19. .word 0
  20. .word 0
  21. adr pc, _hyp_trap
  22. .word 0
  23. .word 0
  24. /*
  25. * secure monitor handler
  26. * U-boot calls this "software interrupt" in start.S
  27. * This is executed on a "smc" instruction, we use a "smc #0" to switch
  28. * to non-secure state.
  29. * We use only r0 and r1 here, due to constraints in the caller.
  30. */
  31. .align 5
  32. _secure_monitor:
  33. mrc p15, 0, r1, c1, c1, 0 @ read SCR
  34. bic r1, r1, #0x4e @ clear IRQ, FIQ, EA, nET bits
  35. orr r1, r1, #0x31 @ enable NS, AW, FW bits
  36. #ifdef CONFIG_ARMV7_VIRT
  37. mrc p15, 0, r0, c0, c1, 1 @ read ID_PFR1
  38. and r0, r0, #CPUID_ARM_VIRT_MASK @ mask virtualization bits
  39. cmp r0, #(1 << CPUID_ARM_VIRT_SHIFT)
  40. orreq r1, r1, #0x100 @ allow HVC instruction
  41. #endif
  42. mcr p15, 0, r1, c1, c1, 0 @ write SCR (with NS bit set)
  43. #ifdef CONFIG_ARMV7_VIRT
  44. mrceq p15, 0, r0, c12, c0, 1 @ get MVBAR value
  45. mcreq p15, 4, r0, c12, c0, 0 @ write HVBAR
  46. #endif
  47. movs pc, lr @ return to non-secure SVC
  48. _hyp_trap:
  49. mrs lr, elr_hyp @ for older asm: .byte 0x00, 0xe3, 0x0e, 0xe1
  50. mov pc, lr @ do no switch modes, but
  51. @ return to caller
  52. /*
  53. * Secondary CPUs start here and call the code for the core specific parts
  54. * of the non-secure and HYP mode transition. The GIC distributor specific
  55. * code has already been executed by a C function before.
  56. * Then they go back to wfi and wait to be woken up by the kernel again.
  57. */
  58. ENTRY(_smp_pen)
  59. mrs r0, cpsr
  60. orr r0, r0, #0xc0
  61. msr cpsr, r0 @ disable interrupts
  62. ldr r1, =_start
  63. mcr p15, 0, r1, c12, c0, 0 @ set VBAR
  64. bl _nonsec_init
  65. mov r12, r0 @ save GICC address
  66. #ifdef CONFIG_ARMV7_VIRT
  67. bl _switch_to_hyp
  68. #endif
  69. ldr r1, [r12, #GICC_IAR] @ acknowledge IPI
  70. str r1, [r12, #GICC_EOIR] @ signal end of interrupt
  71. adr r0, _smp_pen @ do not use this address again
  72. b smp_waitloop @ wait for IPIs, board specific
  73. ENDPROC(_smp_pen)
  74. /*
  75. * Switch a core to non-secure state.
  76. *
  77. * 1. initialize the GIC per-core interface
  78. * 2. allow coprocessor access in non-secure modes
  79. * 3. switch the cpu mode (by calling "smc #0")
  80. *
  81. * Called from smp_pen by secondary cores and directly by the BSP.
  82. * Do not assume that the stack is available and only use registers
  83. * r0-r3 and r12.
  84. *
  85. * PERIPHBASE is used to get the GIC address. This could be 40 bits long,
  86. * though, but we check this in C before calling this function.
  87. */
  88. ENTRY(_nonsec_init)
  89. #ifdef CONFIG_ARM_GIC_BASE_ADDRESS
  90. ldr r2, =CONFIG_ARM_GIC_BASE_ADDRESS
  91. #else
  92. mrc p15, 4, r2, c15, c0, 0 @ read CBAR
  93. bfc r2, #0, #15 @ clear reserved bits
  94. #endif
  95. add r3, r2, #GIC_DIST_OFFSET @ GIC dist i/f offset
  96. mvn r1, #0 @ all bits to 1
  97. str r1, [r3, #GICD_IGROUPRn] @ allow private interrupts
  98. mrc p15, 0, r0, c0, c0, 0 @ read MIDR
  99. ldr r1, =MIDR_PRIMARY_PART_MASK
  100. and r0, r0, r1 @ mask out variant and revision
  101. ldr r1, =MIDR_CORTEX_A7_R0P0 & MIDR_PRIMARY_PART_MASK
  102. cmp r0, r1 @ check for Cortex-A7
  103. ldr r1, =MIDR_CORTEX_A15_R0P0 & MIDR_PRIMARY_PART_MASK
  104. cmpne r0, r1 @ check for Cortex-A15
  105. movne r1, #GIC_CPU_OFFSET_A9 @ GIC CPU offset for A9
  106. moveq r1, #GIC_CPU_OFFSET_A15 @ GIC CPU offset for A15/A7
  107. add r3, r2, r1 @ r3 = GIC CPU i/f addr
  108. mov r1, #1 @ set GICC_CTLR[enable]
  109. str r1, [r3, #GICC_CTLR] @ and clear all other bits
  110. mov r1, #0xff
  111. str r1, [r3, #GICC_PMR] @ set priority mask register
  112. movw r1, #0x3fff
  113. movt r1, #0x0006
  114. mcr p15, 0, r1, c1, c1, 2 @ NSACR = all copros to non-sec
  115. /* The CNTFRQ register of the generic timer needs to be
  116. * programmed in secure state. Some primary bootloaders / firmware
  117. * omit this, so if the frequency is provided in the configuration,
  118. * we do this here instead.
  119. * But first check if we have the generic timer.
  120. */
  121. #ifdef CONFIG_SYS_CLK_FREQ
  122. mrc p15, 0, r0, c0, c1, 1 @ read ID_PFR1
  123. and r0, r0, #CPUID_ARM_GENTIMER_MASK @ mask arch timer bits
  124. cmp r0, #(1 << CPUID_ARM_GENTIMER_SHIFT)
  125. ldreq r1, =CONFIG_SYS_CLK_FREQ
  126. mcreq p15, 0, r1, c14, c0, 0 @ write CNTFRQ
  127. #endif
  128. adr r1, _monitor_vectors
  129. mcr p15, 0, r1, c12, c0, 1 @ set MVBAR to secure vectors
  130. mrc p15, 0, ip, c12, c0, 0 @ save secure copy of VBAR
  131. isb
  132. smc #0 @ call into MONITOR mode
  133. mcr p15, 0, ip, c12, c0, 0 @ write non-secure copy of VBAR
  134. mov r1, #1
  135. str r1, [r3, #GICC_CTLR] @ enable non-secure CPU i/f
  136. add r2, r2, #GIC_DIST_OFFSET
  137. str r1, [r2, #GICD_CTLR] @ allow private interrupts
  138. mov r0, r3 @ return GICC address
  139. bx lr
  140. ENDPROC(_nonsec_init)
  141. #ifdef CONFIG_SMP_PEN_ADDR
  142. /* void __weak smp_waitloop(unsigned previous_address); */
  143. ENTRY(smp_waitloop)
  144. wfi
  145. ldr r1, =CONFIG_SMP_PEN_ADDR @ load start address
  146. ldr r1, [r1]
  147. cmp r0, r1 @ make sure we dont execute this code
  148. beq smp_waitloop @ again (due to a spurious wakeup)
  149. mov pc, r1
  150. ENDPROC(smp_waitloop)
  151. .weak smp_waitloop
  152. #endif
  153. ENTRY(_switch_to_hyp)
  154. mov r0, lr
  155. mov r1, sp @ save SVC copy of LR and SP
  156. isb
  157. hvc #0 @ for older asm: .byte 0x70, 0x00, 0x40, 0xe1
  158. mov sp, r1
  159. mov lr, r0 @ restore SVC copy of LR and SP
  160. bx lr
  161. ENDPROC(_switch_to_hyp)