cpu_macros.S 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (c) 2014-2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef CPU_MACROS_S
  7. #define CPU_MACROS_S
  8. #include <arch.h>
  9. #include <assert_macros.S>
  10. #include <lib/cpus/errata_report.h>
  11. #define CPU_IMPL_PN_MASK (MIDR_IMPL_MASK << MIDR_IMPL_SHIFT) | \
  12. (MIDR_PN_MASK << MIDR_PN_SHIFT)
  13. /* The number of CPU operations allowed */
  14. #define CPU_MAX_PWR_DWN_OPS 2
  15. /* Special constant to specify that CPU has no reset function */
  16. #define CPU_NO_RESET_FUNC 0
  17. #define CPU_NO_EXTRA1_FUNC 0
  18. #define CPU_NO_EXTRA2_FUNC 0
  19. #define CPU_NO_EXTRA3_FUNC 0
  20. /* Word size for 64-bit CPUs */
  21. #define CPU_WORD_SIZE 8
  22. /*
  23. * Whether errata status needs reporting. Errata status is printed in debug
  24. * builds for both BL1 and BL31 images.
  25. */
  26. #if (defined(IMAGE_BL1) || defined(IMAGE_BL31)) && DEBUG
  27. # define REPORT_ERRATA 1
  28. #else
  29. # define REPORT_ERRATA 0
  30. #endif
  31. .equ CPU_MIDR_SIZE, CPU_WORD_SIZE
  32. .equ CPU_EXTRA1_FUNC_SIZE, CPU_WORD_SIZE
  33. .equ CPU_EXTRA2_FUNC_SIZE, CPU_WORD_SIZE
  34. .equ CPU_EXTRA3_FUNC_SIZE, CPU_WORD_SIZE
  35. .equ CPU_E_HANDLER_FUNC_SIZE, CPU_WORD_SIZE
  36. .equ CPU_RESET_FUNC_SIZE, CPU_WORD_SIZE
  37. .equ CPU_PWR_DWN_OPS_SIZE, CPU_WORD_SIZE * CPU_MAX_PWR_DWN_OPS
  38. .equ CPU_ERRATA_FUNC_SIZE, CPU_WORD_SIZE
  39. .equ CPU_ERRATA_LOCK_SIZE, CPU_WORD_SIZE
  40. .equ CPU_ERRATA_PRINTED_SIZE, CPU_WORD_SIZE
  41. .equ CPU_REG_DUMP_SIZE, CPU_WORD_SIZE
  42. #ifndef IMAGE_AT_EL3
  43. .equ CPU_RESET_FUNC_SIZE, 0
  44. #endif
  45. /* The power down core and cluster is needed only in BL31 */
  46. #ifndef IMAGE_BL31
  47. .equ CPU_PWR_DWN_OPS_SIZE, 0
  48. #endif
  49. /* Fields required to print errata status. */
  50. #if !REPORT_ERRATA
  51. .equ CPU_ERRATA_FUNC_SIZE, 0
  52. #endif
  53. /* Only BL31 requieres mutual exclusion and printed flag. */
  54. #if !(REPORT_ERRATA && defined(IMAGE_BL31))
  55. .equ CPU_ERRATA_LOCK_SIZE, 0
  56. .equ CPU_ERRATA_PRINTED_SIZE, 0
  57. #endif
  58. #if !defined(IMAGE_BL31) || !CRASH_REPORTING
  59. .equ CPU_REG_DUMP_SIZE, 0
  60. #endif
  61. /*
  62. * Define the offsets to the fields in cpu_ops structure.
  63. * Every offset is defined based in the offset and size of the previous
  64. * field.
  65. */
  66. .equ CPU_MIDR, 0
  67. .equ CPU_RESET_FUNC, CPU_MIDR + CPU_MIDR_SIZE
  68. .equ CPU_EXTRA1_FUNC, CPU_RESET_FUNC + CPU_RESET_FUNC_SIZE
  69. .equ CPU_EXTRA2_FUNC, CPU_EXTRA1_FUNC + CPU_EXTRA1_FUNC_SIZE
  70. .equ CPU_EXTRA3_FUNC, CPU_EXTRA2_FUNC + CPU_EXTRA2_FUNC_SIZE
  71. .equ CPU_E_HANDLER_FUNC, CPU_EXTRA3_FUNC + CPU_EXTRA3_FUNC_SIZE
  72. .equ CPU_PWR_DWN_OPS, CPU_E_HANDLER_FUNC + CPU_E_HANDLER_FUNC_SIZE
  73. .equ CPU_ERRATA_FUNC, CPU_PWR_DWN_OPS + CPU_PWR_DWN_OPS_SIZE
  74. .equ CPU_ERRATA_LOCK, CPU_ERRATA_FUNC + CPU_ERRATA_FUNC_SIZE
  75. .equ CPU_ERRATA_PRINTED, CPU_ERRATA_LOCK + CPU_ERRATA_LOCK_SIZE
  76. .equ CPU_REG_DUMP, CPU_ERRATA_PRINTED + CPU_ERRATA_PRINTED_SIZE
  77. .equ CPU_OPS_SIZE, CPU_REG_DUMP + CPU_REG_DUMP_SIZE
  78. /*
  79. * Write given expressions as quad words
  80. *
  81. * _count:
  82. * Write at least _count quad words. If the given number of
  83. * expressions is less than _count, repeat the last expression to
  84. * fill _count quad words in total
  85. * _rest:
  86. * Optional list of expressions. _this is for parameter extraction
  87. * only, and has no significance to the caller
  88. *
  89. * Invoked as:
  90. * fill_constants 2, foo, bar, blah, ...
  91. */
  92. .macro fill_constants _count:req, _this, _rest:vararg
  93. .ifgt \_count
  94. /* Write the current expression */
  95. .ifb \_this
  96. .error "Nothing to fill"
  97. .endif
  98. .quad \_this
  99. /* Invoke recursively for remaining expressions */
  100. .ifnb \_rest
  101. fill_constants \_count-1, \_rest
  102. .else
  103. fill_constants \_count-1, \_this
  104. .endif
  105. .endif
  106. .endm
  107. /*
  108. * Declare CPU operations
  109. *
  110. * _name:
  111. * Name of the CPU for which operations are being specified
  112. * _midr:
  113. * Numeric value expected to read from CPU's MIDR
  114. * _resetfunc:
  115. * Reset function for the CPU. If there's no CPU reset function,
  116. * specify CPU_NO_RESET_FUNC
  117. * _extra1:
  118. * This is a placeholder for future per CPU operations. Currently,
  119. * some CPUs use this entry to set a test function to determine if
  120. * the workaround for CVE-2017-5715 needs to be applied or not.
  121. * _extra2:
  122. * This is a placeholder for future per CPU operations. Currently
  123. * some CPUs use this entry to set a function to disable the
  124. * workaround for CVE-2018-3639.
  125. * _extra3:
  126. * This is a placeholder for future per CPU operations. Currently,
  127. * some CPUs use this entry to set a test function to determine if
  128. * the workaround for CVE-2022-23960 needs to be applied or not.
  129. * _e_handler:
  130. * This is a placeholder for future per CPU exception handlers.
  131. * _power_down_ops:
  132. * Comma-separated list of functions to perform power-down
  133. * operatios on the CPU. At least one, and up to
  134. * CPU_MAX_PWR_DWN_OPS number of functions may be specified.
  135. * Starting at power level 0, these functions shall handle power
  136. * down at subsequent power levels. If there aren't exactly
  137. * CPU_MAX_PWR_DWN_OPS functions, the last specified one will be
  138. * used to handle power down at subsequent levels
  139. */
  140. .macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \
  141. _extra1:req, _extra2:req, _extra3:req, _e_handler:req, _power_down_ops:vararg
  142. .section .cpu_ops, "a"
  143. .align 3
  144. .type cpu_ops_\_name, %object
  145. .quad \_midr
  146. #if defined(IMAGE_AT_EL3)
  147. .quad \_resetfunc
  148. #endif
  149. .quad \_extra1
  150. .quad \_extra2
  151. .quad \_extra3
  152. .quad \_e_handler
  153. #ifdef IMAGE_BL31
  154. /* Insert list of functions */
  155. fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
  156. #endif
  157. #if REPORT_ERRATA
  158. .ifndef \_name\()_cpu_str
  159. /*
  160. * Place errata reported flag, and the spinlock to arbitrate access to
  161. * it in the data section.
  162. */
  163. .pushsection .data
  164. define_asm_spinlock \_name\()_errata_lock
  165. \_name\()_errata_reported:
  166. .word 0
  167. .popsection
  168. /* Place CPU string in rodata */
  169. .pushsection .rodata
  170. \_name\()_cpu_str:
  171. .asciz "\_name"
  172. .popsection
  173. .endif
  174. /*
  175. * Mandatory errata status printing function for CPUs of
  176. * this class.
  177. */
  178. .quad \_name\()_errata_report
  179. #ifdef IMAGE_BL31
  180. /* Pointers to errata lock and reported flag */
  181. .quad \_name\()_errata_lock
  182. .quad \_name\()_errata_reported
  183. #endif
  184. #endif
  185. #if defined(IMAGE_BL31) && CRASH_REPORTING
  186. .quad \_name\()_cpu_reg_dump
  187. #endif
  188. .endm
  189. .macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
  190. _power_down_ops:vararg
  191. declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, 0, 0, \
  192. \_power_down_ops
  193. .endm
  194. .macro declare_cpu_ops_eh _name:req, _midr:req, _resetfunc:req, \
  195. _e_handler:req, _power_down_ops:vararg
  196. declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
  197. 0, 0, 0, \_e_handler, \_power_down_ops
  198. .endm
  199. .macro declare_cpu_ops_wa _name:req, _midr:req, \
  200. _resetfunc:req, _extra1:req, _extra2:req, \
  201. _extra3:req, _power_down_ops:vararg
  202. declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
  203. \_extra1, \_extra2, \_extra3, 0, \_power_down_ops
  204. .endm
  205. #if REPORT_ERRATA
  206. /*
  207. * Print status of a CPU errata
  208. *
  209. * _chosen:
  210. * Identifier indicating whether or not a CPU errata has been
  211. * compiled in.
  212. * _cpu:
  213. * Name of the CPU
  214. * _id:
  215. * Errata identifier
  216. * _rev_var:
  217. * Register containing the combined value CPU revision and variant
  218. * - typically the return value of cpu_get_rev_var
  219. */
  220. .macro report_errata _chosen, _cpu, _id, _rev_var=x8
  221. /* Stash a string with errata ID */
  222. .pushsection .rodata
  223. \_cpu\()_errata_\_id\()_str:
  224. .asciz "\_id"
  225. .popsection
  226. /* Check whether errata applies */
  227. mov x0, \_rev_var
  228. /* Shall clobber: x0-x7 */
  229. bl check_errata_\_id
  230. .ifeq \_chosen
  231. /*
  232. * Errata workaround has not been compiled in. If the errata would have
  233. * applied had it been compiled in, print its status as missing.
  234. */
  235. cbz x0, 900f
  236. mov x0, #ERRATA_MISSING
  237. .endif
  238. 900:
  239. adr x1, \_cpu\()_cpu_str
  240. adr x2, \_cpu\()_errata_\_id\()_str
  241. bl errata_print_msg
  242. .endm
  243. #endif
  244. /*
  245. * This macro is used on some CPUs to detect if they are vulnerable
  246. * to CVE-2017-5715.
  247. */
  248. .macro cpu_check_csv2 _reg _label
  249. mrs \_reg, id_aa64pfr0_el1
  250. ubfx \_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
  251. /*
  252. * If the field equals 1, branch targets trained in one context cannot
  253. * affect speculative execution in a different context.
  254. *
  255. * If the field equals 2, it means that the system is also aware of
  256. * SCXTNUM_ELx register contexts. We aren't using them in the TF, so we
  257. * expect users of the registers to do the right thing.
  258. *
  259. * Only apply mitigations if the value of this field is 0.
  260. */
  261. #if ENABLE_ASSERTIONS
  262. cmp \_reg, #3 /* Only values 0 to 2 are expected */
  263. ASM_ASSERT(lo)
  264. #endif
  265. cmp \_reg, #0
  266. bne \_label
  267. .endm
  268. /*
  269. * Helper macro that reads the part number of the current
  270. * CPU and jumps to the given label if it matches the CPU
  271. * MIDR provided.
  272. *
  273. * Clobbers x0.
  274. */
  275. .macro jump_if_cpu_midr _cpu_midr, _label
  276. mrs x0, midr_el1
  277. ubfx x0, x0, MIDR_PN_SHIFT, #12
  278. cmp w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
  279. b.eq \_label
  280. .endm
  281. #endif /* CPU_MACROS_S */