cpu_macros.S 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * Copyright (c) 2014-2024, 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 <assert_macros.S>
  9. #include <lib/cpus/cpu_ops.h>
  10. #include <lib/cpus/errata.h>
  11. /*
  12. * Write given expressions as quad words
  13. *
  14. * _count:
  15. * Write at least _count quad words. If the given number of
  16. * expressions is less than _count, repeat the last expression to
  17. * fill _count quad words in total
  18. * _rest:
  19. * Optional list of expressions. _this is for parameter extraction
  20. * only, and has no significance to the caller
  21. *
  22. * Invoked as:
  23. * fill_constants 2, foo, bar, blah, ...
  24. */
  25. .macro fill_constants _count:req, _this, _rest:vararg
  26. .ifgt \_count
  27. /* Write the current expression */
  28. .ifb \_this
  29. .error "Nothing to fill"
  30. .endif
  31. .quad \_this
  32. /* Invoke recursively for remaining expressions */
  33. .ifnb \_rest
  34. fill_constants \_count-1, \_rest
  35. .else
  36. fill_constants \_count-1, \_this
  37. .endif
  38. .endif
  39. .endm
  40. /*
  41. * Declare CPU operations
  42. *
  43. * _name:
  44. * Name of the CPU for which operations are being specified
  45. * _midr:
  46. * Numeric value expected to read from CPU's MIDR
  47. * _resetfunc:
  48. * Reset function for the CPU. If there's no CPU reset function,
  49. * specify CPU_NO_RESET_FUNC
  50. * _extra1:
  51. * This is a placeholder for future per CPU operations. Currently,
  52. * some CPUs use this entry to set a test function to determine if
  53. * the workaround for CVE-2017-5715 needs to be applied or not.
  54. * _extra2:
  55. * This is a placeholder for future per CPU operations. Currently
  56. * some CPUs use this entry to set a function to disable the
  57. * workaround for CVE-2018-3639.
  58. * _extra3:
  59. * This is a placeholder for future per CPU operations. Currently,
  60. * some CPUs use this entry to set a test function to determine if
  61. * the workaround for CVE-2022-23960 needs to be applied or not.
  62. * _e_handler:
  63. * This is a placeholder for future per CPU exception handlers.
  64. * _power_down_ops:
  65. * Comma-separated list of functions to perform power-down
  66. * operatios on the CPU. At least one, and up to
  67. * CPU_MAX_PWR_DWN_OPS number of functions may be specified.
  68. * Starting at power level 0, these functions shall handle power
  69. * down at subsequent power levels. If there aren't exactly
  70. * CPU_MAX_PWR_DWN_OPS functions, the last specified one will be
  71. * used to handle power down at subsequent levels
  72. */
  73. .macro declare_cpu_ops_base _name:req, _midr:req, _resetfunc:req, \
  74. _extra1:req, _extra2:req, _extra3:req, _e_handler:req, _power_down_ops:vararg
  75. .section .cpu_ops, "a"
  76. .align 3
  77. .type cpu_ops_\_name, %object
  78. .quad \_midr
  79. #if defined(IMAGE_AT_EL3)
  80. .quad \_resetfunc
  81. #endif
  82. .quad \_extra1
  83. .quad \_extra2
  84. .quad \_extra3
  85. .quad \_e_handler
  86. #ifdef IMAGE_BL31
  87. /* Insert list of functions */
  88. fill_constants CPU_MAX_PWR_DWN_OPS, \_power_down_ops
  89. #endif
  90. /*
  91. * It is possible (although unlikely) that a cpu may have no errata in
  92. * code. In that case the start label will not be defined. The list is
  93. * intended to be used in a loop, so define it as zero-length for
  94. * predictable behaviour. Since this macro is always called at the end
  95. * of the cpu file (after all errata have been parsed) we can be sure
  96. * that we are at the end of the list. Some cpus call declare_cpu_ops
  97. * twice, so only do this once.
  98. */
  99. .pushsection .rodata.errata_entries
  100. .ifndef \_name\()_errata_list_start
  101. \_name\()_errata_list_start:
  102. .endif
  103. .ifndef \_name\()_errata_list_end
  104. \_name\()_errata_list_end:
  105. .endif
  106. .popsection
  107. /* and now put them in cpu_ops */
  108. .quad \_name\()_errata_list_start
  109. .quad \_name\()_errata_list_end
  110. #if REPORT_ERRATA
  111. .ifndef \_name\()_cpu_str
  112. /*
  113. * Place errata reported flag, and the spinlock to arbitrate access to
  114. * it in the data section.
  115. */
  116. .pushsection .data
  117. define_asm_spinlock \_name\()_errata_lock
  118. \_name\()_errata_reported:
  119. .word 0
  120. .popsection
  121. /* Place CPU string in rodata */
  122. .pushsection .rodata
  123. \_name\()_cpu_str:
  124. .asciz "\_name"
  125. .popsection
  126. .endif
  127. .quad \_name\()_cpu_str
  128. #ifdef IMAGE_BL31
  129. /* Pointers to errata lock and reported flag */
  130. .quad \_name\()_errata_lock
  131. .quad \_name\()_errata_reported
  132. #endif /* IMAGE_BL31 */
  133. #endif /* REPORT_ERRATA */
  134. #if defined(IMAGE_BL31) && CRASH_REPORTING
  135. .quad \_name\()_cpu_reg_dump
  136. #endif
  137. .endm
  138. .macro declare_cpu_ops _name:req, _midr:req, _resetfunc:req, \
  139. _power_down_ops:vararg
  140. declare_cpu_ops_base \_name, \_midr, \_resetfunc, 0, 0, 0, 0, \
  141. \_power_down_ops
  142. .endm
  143. .macro declare_cpu_ops_eh _name:req, _midr:req, _resetfunc:req, \
  144. _e_handler:req, _power_down_ops:vararg
  145. declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
  146. 0, 0, 0, \_e_handler, \_power_down_ops
  147. .endm
  148. .macro declare_cpu_ops_wa _name:req, _midr:req, \
  149. _resetfunc:req, _extra1:req, _extra2:req, \
  150. _extra3:req, _power_down_ops:vararg
  151. declare_cpu_ops_base \_name, \_midr, \_resetfunc, \
  152. \_extra1, \_extra2, \_extra3, 0, \_power_down_ops
  153. .endm
  154. /*
  155. * This macro is used on some CPUs to detect if they are vulnerable
  156. * to CVE-2017-5715.
  157. */
  158. .macro cpu_check_csv2 _reg _label
  159. mrs \_reg, id_aa64pfr0_el1
  160. ubfx \_reg, \_reg, #ID_AA64PFR0_CSV2_SHIFT, #ID_AA64PFR0_CSV2_LENGTH
  161. /*
  162. * If the field equals 1, branch targets trained in one context cannot
  163. * affect speculative execution in a different context.
  164. *
  165. * If the field equals 2, it means that the system is also aware of
  166. * SCXTNUM_ELx register contexts. We aren't using them in the TF, so we
  167. * expect users of the registers to do the right thing.
  168. *
  169. * Only apply mitigations if the value of this field is 0.
  170. */
  171. #if ENABLE_ASSERTIONS
  172. cmp \_reg, #3 /* Only values 0 to 2 are expected */
  173. ASM_ASSERT(lo)
  174. #endif
  175. cmp \_reg, #0
  176. bne \_label
  177. .endm
  178. /*
  179. * Helper macro that reads the part number of the current
  180. * CPU and jumps to the given label if it matches the CPU
  181. * MIDR provided.
  182. *
  183. * Clobbers x0.
  184. */
  185. .macro jump_if_cpu_midr _cpu_midr, _label
  186. mrs x0, midr_el1
  187. ubfx x0, x0, MIDR_PN_SHIFT, #12
  188. cmp w0, #((\_cpu_midr >> MIDR_PN_SHIFT) & MIDR_PN_MASK)
  189. b.eq \_label
  190. .endm
  191. /*
  192. * Workaround wrappers for errata that apply at reset or runtime. Reset errata
  193. * will be applied automatically
  194. *
  195. * _cpu:
  196. * Name of cpu as given to declare_cpu_ops
  197. *
  198. * _cve:
  199. * Whether erratum is a CVE. CVE year if yes, 0 otherwise
  200. *
  201. * _id:
  202. * Erratum or CVE number. Please combine with previous field with ERRATUM
  203. * or CVE macros
  204. *
  205. * _chosen:
  206. * Compile time flag on whether the erratum is included
  207. *
  208. * _apply_at_reset:
  209. * Whether the erratum should be automatically applied at reset
  210. */
  211. .macro add_erratum_entry _cpu:req, _cve:req, _id:req, _chosen:req, _apply_at_reset:req
  212. .pushsection .rodata.errata_entries
  213. .align 3
  214. .ifndef \_cpu\()_errata_list_start
  215. \_cpu\()_errata_list_start:
  216. .endif
  217. /* check if unused and compile out if no references */
  218. .if \_apply_at_reset && \_chosen
  219. .quad erratum_\_cpu\()_\_id\()_wa
  220. .else
  221. .quad 0
  222. .endif
  223. /* TODO(errata ABI): this prevents all checker functions from
  224. * being optimised away. Can be done away with unless the ABI
  225. * needs them */
  226. .quad check_erratum_\_cpu\()_\_id
  227. /* Will fit CVEs with up to 10 character in the ID field */
  228. .word \_id
  229. .hword \_cve
  230. .byte \_chosen
  231. /* TODO(errata ABI): mitigated field for known but unmitigated
  232. * errata */
  233. .byte 0x1
  234. .popsection
  235. .endm
  236. .macro _workaround_start _cpu:req, _cve:req, _id:req, _chosen:req, _apply_at_reset:req
  237. add_erratum_entry \_cpu, \_cve, \_id, \_chosen, \_apply_at_reset
  238. func erratum_\_cpu\()_\_id\()_wa
  239. mov x8, x30
  240. /* save rev_var for workarounds that might need it but don't
  241. * restore to x0 because few will care */
  242. mov x7, x0
  243. bl check_erratum_\_cpu\()_\_id
  244. cbz x0, erratum_\_cpu\()_\_id\()_skip
  245. .endm
  246. .macro _workaround_end _cpu:req, _id:req
  247. erratum_\_cpu\()_\_id\()_skip:
  248. ret x8
  249. endfunc erratum_\_cpu\()_\_id\()_wa
  250. .endm
  251. /*******************************************************************************
  252. * Errata workaround wrappers
  253. ******************************************************************************/
  254. /*
  255. * Workaround wrappers for errata that apply at reset or runtime. Reset errata
  256. * will be applied automatically
  257. *
  258. * _cpu:
  259. * Name of cpu as given to declare_cpu_ops
  260. *
  261. * _cve:
  262. * Whether erratum is a CVE. CVE year if yes, 0 otherwise
  263. *
  264. * _id:
  265. * Erratum or CVE number. Please combine with previous field with ERRATUM
  266. * or CVE macros
  267. *
  268. * _chosen:
  269. * Compile time flag on whether the erratum is included
  270. *
  271. * in body:
  272. * clobber x0 to x7 (please only use those)
  273. * argument x7 - cpu_rev_var
  274. *
  275. * _wa clobbers: x0-x8 (PCS compliant)
  276. */
  277. .macro workaround_reset_start _cpu:req, _cve:req, _id:req, _chosen:req
  278. _workaround_start \_cpu, \_cve, \_id, \_chosen, 1
  279. .endm
  280. /*
  281. * See `workaround_reset_start` for usage info. Additional arguments:
  282. *
  283. * _midr:
  284. * Check if CPU's MIDR matches the CPU it's meant for. Must be specified
  285. * for errata applied in generic code
  286. */
  287. .macro workaround_runtime_start _cpu:req, _cve:req, _id:req, _chosen:req, _midr
  288. /*
  289. * Let errata specify if they need MIDR checking. Sadly, storing the
  290. * MIDR in an .equ to retrieve automatically blows up as it stores some
  291. * brackets in the symbol
  292. */
  293. .ifnb \_midr
  294. jump_if_cpu_midr \_midr, 1f
  295. b erratum_\_cpu\()_\_id\()_skip
  296. 1:
  297. .endif
  298. _workaround_start \_cpu, \_cve, \_id, \_chosen, 0
  299. .endm
  300. /*
  301. * Usage and arguments identical to `workaround_reset_start`. The _cve argument
  302. * is kept here so the same #define can be used as that macro
  303. */
  304. .macro workaround_reset_end _cpu:req, _cve:req, _id:req
  305. _workaround_end \_cpu, \_id
  306. .endm
  307. /*
  308. * See `workaround_reset_start` for usage info. The _cve argument is kept here
  309. * so the same #define can be used as that macro. Additional arguments:
  310. *
  311. * _no_isb:
  312. * Optionally do not include the trailing isb. Please disable with the
  313. * NO_ISB macro
  314. */
  315. .macro workaround_runtime_end _cpu:req, _cve:req, _id:req, _no_isb
  316. /*
  317. * Runtime errata do not have a reset function to call the isb for them
  318. * and missing the isb could be very problematic. It is also likely as
  319. * they tend to be scattered in generic code.
  320. */
  321. .ifb \_no_isb
  322. isb
  323. .endif
  324. _workaround_end \_cpu, \_id
  325. .endm
  326. /*******************************************************************************
  327. * Errata workaround helpers
  328. ******************************************************************************/
  329. /*
  330. * Set a bit in a system register. Can set multiple bits but is limited by the
  331. * way the ORR instruction encodes them.
  332. *
  333. * _reg:
  334. * Register to write to
  335. *
  336. * _bit:
  337. * Bit to set. Please use a descriptive #define
  338. *
  339. * _assert:
  340. * Optionally whether to read back and assert that the bit has been
  341. * written. Please disable with NO_ASSERT macro
  342. *
  343. * clobbers: x1
  344. */
  345. .macro sysreg_bit_set _reg:req, _bit:req, _assert=1
  346. mrs x1, \_reg
  347. orr x1, x1, #\_bit
  348. msr \_reg, x1
  349. .endm
  350. /*
  351. * Clear a bit in a system register. Can clear multiple bits but is limited by
  352. * the way the BIC instrucion encodes them.
  353. *
  354. * see sysreg_bit_set for usage
  355. */
  356. .macro sysreg_bit_clear _reg:req, _bit:req
  357. mrs x1, \_reg
  358. bic x1, x1, #\_bit
  359. msr \_reg, x1
  360. .endm
  361. .macro override_vector_table _table:req
  362. adr x1, \_table
  363. msr vbar_el3, x1
  364. .endm
  365. /*
  366. * BFI : Inserts bitfield into a system register.
  367. *
  368. * BFI{cond} Rd, Rn, #lsb, #width
  369. */
  370. .macro sysreg_bitfield_insert _reg:req, _src:req, _lsb:req, _width:req
  371. /* Source value for BFI */
  372. mov x1, #\_src
  373. mrs x0, \_reg
  374. bfi x0, x1, #\_lsb, #\_width
  375. msr \_reg, x0
  376. .endm
  377. .macro sysreg_bitfield_insert_from_gpr _reg:req, _gpr:req, _lsb:req, _width:req
  378. /* Source value in register for BFI */
  379. mov x1, \_gpr
  380. mrs x0, \_reg
  381. bfi x0, x1, #\_lsb, #\_width
  382. msr \_reg, x0
  383. .endm
  384. /*
  385. * Apply erratum
  386. *
  387. * _cpu:
  388. * Name of cpu as given to declare_cpu_ops
  389. *
  390. * _cve:
  391. * Whether erratum is a CVE. CVE year if yes, 0 otherwise
  392. *
  393. * _id:
  394. * Erratum or CVE number. Please combine with previous field with ERRATUM
  395. * or CVE macros
  396. *
  397. * _chosen:
  398. * Compile time flag on whether the erratum is included
  399. *
  400. * _get_rev:
  401. * Optional parameter that determines whether to insert a call to the CPU revision fetching
  402. * procedure. Stores the result of this in the temporary register x10 to allow for chaining
  403. *
  404. * clobbers: x0-x10 (PCS compliant)
  405. */
  406. .macro apply_erratum _cpu:req, _cve:req, _id:req, _chosen:req, _get_rev=GET_CPU_REV
  407. .if (\_chosen & \_get_rev)
  408. mov x9, x30
  409. bl cpu_get_rev_var
  410. mov x10, x0
  411. .elseif (\_chosen)
  412. mov x9, x30
  413. mov x0, x10
  414. .endif
  415. .if \_chosen
  416. bl erratum_\_cpu\()_\_id\()_wa
  417. mov x30, x9
  418. .endif
  419. .endm
  420. /*
  421. * Helpers to select which revisions errata apply to. Don't leave a link
  422. * register as the cpu_rev_var_*** will call the ret and we can save on one.
  423. *
  424. * _cpu:
  425. * Name of cpu as given to declare_cpu_ops
  426. *
  427. * _cve:
  428. * Whether erratum is a CVE. CVE year if yes, 0 otherwise
  429. *
  430. * _id:
  431. * Erratum or CVE number. Please combine with previous field with ERRATUM
  432. * or CVE macros
  433. *
  434. * _rev_num:
  435. * Revision to apply to
  436. *
  437. * in body:
  438. * clobber: x0 to x4
  439. * argument: x0 - cpu_rev_var
  440. */
  441. .macro check_erratum_ls _cpu:req, _cve:req, _id:req, _rev_num:req
  442. func check_erratum_\_cpu\()_\_id
  443. mov x1, #\_rev_num
  444. b cpu_rev_var_ls
  445. endfunc check_erratum_\_cpu\()_\_id
  446. .endm
  447. .macro check_erratum_hs _cpu:req, _cve:req, _id:req, _rev_num:req
  448. func check_erratum_\_cpu\()_\_id
  449. mov x1, #\_rev_num
  450. b cpu_rev_var_hs
  451. endfunc check_erratum_\_cpu\()_\_id
  452. .endm
  453. .macro check_erratum_range _cpu:req, _cve:req, _id:req, _rev_num_lo:req, _rev_num_hi:req
  454. func check_erratum_\_cpu\()_\_id
  455. mov x1, #\_rev_num_lo
  456. mov x2, #\_rev_num_hi
  457. b cpu_rev_var_range
  458. endfunc check_erratum_\_cpu\()_\_id
  459. .endm
  460. .macro check_erratum_chosen _cpu:req, _cve:req, _id:req, _chosen:req
  461. func check_erratum_\_cpu\()_\_id
  462. .if \_chosen
  463. mov x0, #ERRATA_APPLIES
  464. .else
  465. mov x0, #ERRATA_MISSING
  466. .endif
  467. ret
  468. endfunc check_erratum_\_cpu\()_\_id
  469. .endm
  470. /* provide a shorthand for the name format for annoying errata */
  471. .macro check_erratum_custom_start _cpu:req, _cve:req, _id:req
  472. func check_erratum_\_cpu\()_\_id
  473. .endm
  474. .macro check_erratum_custom_end _cpu:req, _cve:req, _id:req
  475. endfunc check_erratum_\_cpu\()_\_id
  476. .endm
  477. /*******************************************************************************
  478. * CPU reset function wrapper
  479. ******************************************************************************/
  480. /*
  481. * Wrapper to automatically apply all reset-time errata. Will end with an isb.
  482. *
  483. * _cpu:
  484. * Name of cpu as given to declare_cpu_ops
  485. *
  486. * in body:
  487. * clobber x8 to x14
  488. * argument x14 - cpu_rev_var
  489. */
  490. .macro cpu_reset_func_start _cpu:req
  491. func \_cpu\()_reset_func
  492. mov x15, x30
  493. bl cpu_get_rev_var
  494. mov x14, x0
  495. /* short circuit the location to avoid searching the list */
  496. adrp x12, \_cpu\()_errata_list_start
  497. add x12, x12, :lo12:\_cpu\()_errata_list_start
  498. adrp x13, \_cpu\()_errata_list_end
  499. add x13, x13, :lo12:\_cpu\()_errata_list_end
  500. errata_begin:
  501. /* if head catches up with end of list, exit */
  502. cmp x12, x13
  503. b.eq errata_end
  504. ldr x10, [x12, #ERRATUM_WA_FUNC]
  505. /* TODO(errata ABI): check mitigated and checker function fields
  506. * for 0 */
  507. ldrb w11, [x12, #ERRATUM_CHOSEN]
  508. /* skip if not chosen */
  509. cbz x11, 1f
  510. /* skip if runtime erratum */
  511. cbz x10, 1f
  512. /* put cpu revision in x0 and call workaround */
  513. mov x0, x14
  514. blr x10
  515. 1:
  516. add x12, x12, #ERRATUM_ENTRY_SIZE
  517. b errata_begin
  518. errata_end:
  519. .endm
  520. .macro cpu_reset_func_end _cpu:req
  521. isb
  522. ret x15
  523. endfunc \_cpu\()_reset_func
  524. .endm
  525. #endif /* CPU_MACROS_S */