hibernate-asm.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Hibernate low-level support
  3. *
  4. * Copyright (C) 2016 ARM Ltd.
  5. * Author: James Morse <james.morse@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/linkage.h>
  20. #include <linux/errno.h>
  21. #include <asm/asm-offsets.h>
  22. #include <asm/assembler.h>
  23. #include <asm/cputype.h>
  24. #include <asm/memory.h>
  25. #include <asm/page.h>
  26. #include <asm/virt.h>
  27. /*
  28. * To prevent the possibility of old and new partial table walks being visible
  29. * in the tlb, switch the ttbr to a zero page when we invalidate the old
  30. * records. D4.7.1 'General TLB maintenance requirements' in ARM DDI 0487A.i
  31. * Even switching to our copied tables will cause a changed output address at
  32. * each stage of the walk.
  33. */
  34. .macro break_before_make_ttbr_switch zero_page, page_table
  35. msr ttbr1_el1, \zero_page
  36. isb
  37. tlbi vmalle1
  38. dsb nsh
  39. msr ttbr1_el1, \page_table
  40. isb
  41. .endm
  42. /*
  43. * Resume from hibernate
  44. *
  45. * Loads temporary page tables then restores the memory image.
  46. * Finally branches to cpu_resume() to restore the state saved by
  47. * swsusp_arch_suspend().
  48. *
  49. * Because this code has to be copied to a 'safe' page, it can't call out to
  50. * other functions by PC-relative address. Also remember that it may be
  51. * mid-way through over-writing other functions. For this reason it contains
  52. * code from flush_icache_range() and uses the copy_page() macro.
  53. *
  54. * This 'safe' page is mapped via ttbr0, and executed from there. This function
  55. * switches to a copy of the linear map in ttbr1, performs the restore, then
  56. * switches ttbr1 to the original kernel's swapper_pg_dir.
  57. *
  58. * All of memory gets written to, including code. We need to clean the kernel
  59. * text to the Point of Coherence (PoC) before secondary cores can be booted.
  60. * Because the kernel modules and executable pages mapped to user space are
  61. * also written as data, we clean all pages we touch to the Point of
  62. * Unification (PoU).
  63. *
  64. * x0: physical address of temporary page tables
  65. * x1: physical address of swapper page tables
  66. * x2: address of cpu_resume
  67. * x3: linear map address of restore_pblist in the current kernel
  68. * x4: physical address of __hyp_stub_vectors, or 0
  69. * x5: physical address of a zero page that remains zero after resume
  70. */
  71. .pushsection ".hibernate_exit.text", "ax"
  72. ENTRY(swsusp_arch_suspend_exit)
  73. /*
  74. * We execute from ttbr0, change ttbr1 to our copied linear map tables
  75. * with a break-before-make via the zero page
  76. */
  77. break_before_make_ttbr_switch x5, x0
  78. mov x21, x1
  79. mov x30, x2
  80. mov x24, x4
  81. mov x25, x5
  82. /* walk the restore_pblist and use copy_page() to over-write memory */
  83. mov x19, x3
  84. 1: ldr x10, [x19, #HIBERN_PBE_ORIG]
  85. mov x0, x10
  86. ldr x1, [x19, #HIBERN_PBE_ADDR]
  87. copy_page x0, x1, x2, x3, x4, x5, x6, x7, x8, x9
  88. add x1, x10, #PAGE_SIZE
  89. /* Clean the copied page to PoU - based on flush_icache_range() */
  90. raw_dcache_line_size x2, x3
  91. sub x3, x2, #1
  92. bic x4, x10, x3
  93. 2: dc cvau, x4 /* clean D line / unified line */
  94. add x4, x4, x2
  95. cmp x4, x1
  96. b.lo 2b
  97. ldr x19, [x19, #HIBERN_PBE_NEXT]
  98. cbnz x19, 1b
  99. dsb ish /* wait for PoU cleaning to finish */
  100. /* switch to the restored kernels page tables */
  101. break_before_make_ttbr_switch x25, x21
  102. ic ialluis
  103. dsb ish
  104. isb
  105. cbz x24, 3f /* Do we need to re-initialise EL2? */
  106. hvc #0
  107. 3: ret
  108. .ltorg
  109. ENDPROC(swsusp_arch_suspend_exit)
  110. /*
  111. * Restore the hyp stub.
  112. * This must be done before the hibernate page is unmapped by _cpu_resume(),
  113. * but happens before any of the hyp-stub's code is cleaned to PoC.
  114. *
  115. * x24: The physical address of __hyp_stub_vectors
  116. */
  117. el1_sync:
  118. msr vbar_el2, x24
  119. eret
  120. ENDPROC(el1_sync)
  121. .macro invalid_vector label
  122. \label:
  123. b \label
  124. ENDPROC(\label)
  125. .endm
  126. invalid_vector el2_sync_invalid
  127. invalid_vector el2_irq_invalid
  128. invalid_vector el2_fiq_invalid
  129. invalid_vector el2_error_invalid
  130. invalid_vector el1_sync_invalid
  131. invalid_vector el1_irq_invalid
  132. invalid_vector el1_fiq_invalid
  133. invalid_vector el1_error_invalid
  134. /* el2 vectors - switch el2 here while we restore the memory image. */
  135. .align 11
  136. ENTRY(hibernate_el2_vectors)
  137. ventry el2_sync_invalid // Synchronous EL2t
  138. ventry el2_irq_invalid // IRQ EL2t
  139. ventry el2_fiq_invalid // FIQ EL2t
  140. ventry el2_error_invalid // Error EL2t
  141. ventry el2_sync_invalid // Synchronous EL2h
  142. ventry el2_irq_invalid // IRQ EL2h
  143. ventry el2_fiq_invalid // FIQ EL2h
  144. ventry el2_error_invalid // Error EL2h
  145. ventry el1_sync // Synchronous 64-bit EL1
  146. ventry el1_irq_invalid // IRQ 64-bit EL1
  147. ventry el1_fiq_invalid // FIQ 64-bit EL1
  148. ventry el1_error_invalid // Error 64-bit EL1
  149. ventry el1_sync_invalid // Synchronous 32-bit EL1
  150. ventry el1_irq_invalid // IRQ 32-bit EL1
  151. ventry el1_fiq_invalid // FIQ 32-bit EL1
  152. ventry el1_error_invalid // Error 32-bit EL1
  153. END(hibernate_el2_vectors)
  154. .popsection