platform.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef PLATFORM_H
  7. #define PLATFORM_H
  8. #include <stdint.h>
  9. #include <lib/psci/psci.h>
  10. #if defined(SPD_spmd)
  11. #include <services/spm_core_manifest.h>
  12. #endif
  13. #if ENABLE_RME
  14. #include <services/rmm_core_manifest.h>
  15. #include <services/rmm_el3_token_sign.h>
  16. #endif
  17. #include <drivers/fwu/fwu_metadata.h>
  18. #if TRNG_SUPPORT
  19. #include "plat_trng.h"
  20. #endif /* TRNG_SUPPORT */
  21. #if DRTM_SUPPORT
  22. #include "plat_drtm.h"
  23. #endif /* DRTM_SUPPORT */
  24. /*******************************************************************************
  25. * Forward declarations
  26. ******************************************************************************/
  27. struct auth_img_desc_s;
  28. struct meminfo;
  29. struct image_info;
  30. struct entry_point_info;
  31. struct image_desc;
  32. struct bl_load_info;
  33. struct bl_params;
  34. struct mmap_region;
  35. struct spm_mm_boot_info;
  36. struct sp_res_desc;
  37. struct rmm_manifest;
  38. enum fw_enc_status_t;
  39. /*******************************************************************************
  40. * Structure populated by platform specific code to export routines which
  41. * perform load images functions, and associated pointer to platform ops
  42. ******************************************************************************/
  43. struct plat_try_images_ops {
  44. int (*next_instance)(unsigned int image_id);
  45. };
  46. extern const struct plat_try_images_ops *plat_try_img_ops;
  47. /*******************************************************************************
  48. * plat_get_rotpk_info() flags
  49. ******************************************************************************/
  50. #define ROTPK_IS_HASH (1 << 0)
  51. /* Flag used to skip verification of the certificate ROTPK while the platform
  52. ROTPK is not deployed */
  53. #define ROTPK_NOT_DEPLOYED (1 << 1)
  54. static inline bool is_rotpk_flags_valid(unsigned int flags)
  55. {
  56. unsigned int valid_flags = ROTPK_IS_HASH;
  57. return (flags == ROTPK_NOT_DEPLOYED) || ((flags & ~valid_flags) == 0);
  58. }
  59. /*******************************************************************************
  60. * plat_get_enc_key_info() flags
  61. ******************************************************************************/
  62. /*
  63. * Flag used to notify caller that information provided in key buffer is an
  64. * identifier rather than an actual key.
  65. */
  66. #define ENC_KEY_IS_IDENTIFIER (1 << 0)
  67. /*******************************************************************************
  68. * Function declarations
  69. ******************************************************************************/
  70. /*******************************************************************************
  71. * Mandatory common functions
  72. ******************************************************************************/
  73. unsigned int plat_get_syscnt_freq2(void);
  74. int plat_get_image_source(unsigned int image_id,
  75. uintptr_t *dev_handle,
  76. uintptr_t *image_spec);
  77. uintptr_t plat_get_ns_image_entrypoint(void);
  78. unsigned int plat_my_core_pos(void);
  79. int plat_core_pos_by_mpidr(u_register_t mpidr);
  80. int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size);
  81. /*******************************************************************************
  82. * Simple routine to determine whether a mpidr is valid or not.
  83. ******************************************************************************/
  84. static inline bool is_valid_mpidr(u_register_t mpidr)
  85. {
  86. int pos = plat_core_pos_by_mpidr(mpidr);
  87. if ((pos < 0) || ((unsigned int)pos >= PLATFORM_CORE_COUNT)) {
  88. return false;
  89. }
  90. return true;
  91. }
  92. #if STACK_PROTECTOR_ENABLED
  93. /*
  94. * Return a new value to be used for the stack protection's canary.
  95. *
  96. * Ideally, this value is a random number that is impossible to predict by an
  97. * attacker.
  98. */
  99. u_register_t plat_get_stack_protector_canary(void);
  100. #endif /* STACK_PROTECTOR_ENABLED */
  101. /*******************************************************************************
  102. * Mandatory interrupt management functions
  103. ******************************************************************************/
  104. uint32_t plat_ic_get_pending_interrupt_id(void);
  105. uint32_t plat_ic_get_pending_interrupt_type(void);
  106. uint32_t plat_ic_acknowledge_interrupt(void);
  107. uint32_t plat_ic_get_interrupt_type(uint32_t id);
  108. void plat_ic_end_of_interrupt(uint32_t id);
  109. uint32_t plat_interrupt_type_to_line(uint32_t type,
  110. uint32_t security_state);
  111. /*******************************************************************************
  112. * Optional interrupt management functions, depending on chosen EL3 components.
  113. ******************************************************************************/
  114. unsigned int plat_ic_get_running_priority(void);
  115. int plat_ic_is_spi(unsigned int id);
  116. int plat_ic_is_ppi(unsigned int id);
  117. int plat_ic_is_sgi(unsigned int id);
  118. unsigned int plat_ic_get_interrupt_active(unsigned int id);
  119. void plat_ic_disable_interrupt(unsigned int id);
  120. void plat_ic_enable_interrupt(unsigned int id);
  121. bool plat_ic_has_interrupt_type(unsigned int type);
  122. void plat_ic_set_interrupt_type(unsigned int id, unsigned int type);
  123. void plat_ic_set_interrupt_priority(unsigned int id, unsigned int priority);
  124. void plat_ic_raise_el3_sgi(int sgi_num, u_register_t target);
  125. void plat_ic_raise_ns_sgi(int sgi_num, u_register_t target);
  126. void plat_ic_raise_s_el1_sgi(int sgi_num, u_register_t target);
  127. void plat_ic_set_spi_routing(unsigned int id, unsigned int routing_mode,
  128. u_register_t mpidr);
  129. void plat_ic_set_interrupt_pending(unsigned int id);
  130. void plat_ic_clear_interrupt_pending(unsigned int id);
  131. unsigned int plat_ic_set_priority_mask(unsigned int mask);
  132. unsigned int plat_ic_deactivate_priority(unsigned int mask);
  133. unsigned int plat_ic_get_interrupt_id(unsigned int raw);
  134. /*******************************************************************************
  135. * Optional common functions (may be overridden)
  136. ******************************************************************************/
  137. uintptr_t plat_get_my_stack(void);
  138. void plat_report_exception(unsigned int exception_type);
  139. void plat_report_prefetch_abort(unsigned int fault_address);
  140. void plat_report_data_abort(unsigned int fault_address);
  141. int plat_crash_console_init(void);
  142. int plat_crash_console_putc(int c);
  143. void plat_crash_console_flush(void);
  144. void plat_error_handler(int err) __dead2;
  145. void plat_panic_handler(void) __dead2;
  146. void plat_system_reset(void) __dead2;
  147. const char *plat_log_get_prefix(unsigned int log_level);
  148. void bl2_plat_preload_setup(void);
  149. void plat_setup_try_img_ops(const struct plat_try_images_ops *plat_try_ops);
  150. #if MEASURED_BOOT
  151. int plat_mboot_measure_image(unsigned int image_id, image_info_t *image_data);
  152. int plat_mboot_measure_critical_data(unsigned int critical_data_id,
  153. const void *base,
  154. size_t size);
  155. int plat_mboot_measure_key(const void *pk_oid, const void *pk_ptr,
  156. size_t pk_len);
  157. #else
  158. static inline int plat_mboot_measure_image(unsigned int image_id __unused,
  159. image_info_t *image_data __unused)
  160. {
  161. return 0;
  162. }
  163. static inline int plat_mboot_measure_critical_data(
  164. unsigned int critical_data_id __unused,
  165. const void *base __unused,
  166. size_t size __unused)
  167. {
  168. return 0;
  169. }
  170. static inline int plat_mboot_measure_key(const void *pk_oid __unused,
  171. const void *pk_ptr __unused,
  172. size_t pk_len __unused)
  173. {
  174. return 0;
  175. }
  176. #endif /* MEASURED_BOOT */
  177. #if EARLY_CONSOLE
  178. void plat_setup_early_console(void);
  179. #else
  180. static inline void plat_setup_early_console(void)
  181. {
  182. }
  183. #endif /* EARLY_CONSOLE */
  184. /*******************************************************************************
  185. * Mandatory BL1 functions
  186. ******************************************************************************/
  187. void bl1_early_platform_setup(void);
  188. void bl1_plat_arch_setup(void);
  189. void bl1_platform_setup(void);
  190. struct meminfo *bl1_plat_sec_mem_layout(void);
  191. /*******************************************************************************
  192. * Optional EL3 component functions in BL31
  193. ******************************************************************************/
  194. /* SDEI platform functions */
  195. #if SDEI_SUPPORT
  196. void plat_sdei_setup(void);
  197. int plat_sdei_validate_entry_point(uintptr_t ep, unsigned int client_mode);
  198. void plat_sdei_handle_masked_trigger(uint64_t mpidr, unsigned int intr);
  199. #endif
  200. void plat_default_ea_handler(unsigned int ea_reason, uint64_t syndrome, void *cookie,
  201. void *handle, uint64_t flags);
  202. void plat_ea_handler(unsigned int ea_reason, uint64_t syndrome, void *cookie,
  203. void *handle, uint64_t flags);
  204. /*
  205. * The following function is mandatory when the
  206. * firmware update feature is used.
  207. */
  208. int bl1_plat_mem_check(uintptr_t mem_base, unsigned int mem_size,
  209. unsigned int flags);
  210. /*******************************************************************************
  211. * Optional BL1 functions (may be overridden)
  212. ******************************************************************************/
  213. /*
  214. * The following functions are used for image loading process in BL1.
  215. */
  216. void bl1_plat_set_ep_info(unsigned int image_id,
  217. struct entry_point_info *ep_info);
  218. /*
  219. * The following functions are mandatory when firmware update
  220. * feature is used and optional otherwise.
  221. */
  222. unsigned int bl1_plat_get_next_image_id(void);
  223. struct image_desc *bl1_plat_get_image_desc(unsigned int image_id);
  224. /*
  225. * The following functions are used by firmware update
  226. * feature and may optionally be overridden.
  227. */
  228. __dead2 void bl1_plat_fwu_done(void *client_cookie, void *reserved);
  229. /*
  230. * This BL1 function can be used by the platforms to update/use image
  231. * information for a given `image_id`.
  232. */
  233. int bl1_plat_handle_pre_image_load(unsigned int image_id);
  234. int bl1_plat_handle_post_image_load(unsigned int image_id);
  235. /* Utility functions */
  236. void bl1_plat_calc_bl2_layout(const meminfo_t *bl1_mem_layout,
  237. meminfo_t *bl2_mem_layout);
  238. #if MEASURED_BOOT
  239. void bl1_plat_mboot_init(void);
  240. void bl1_plat_mboot_finish(void);
  241. #else
  242. static inline void bl1_plat_mboot_init(void)
  243. {
  244. }
  245. static inline void bl1_plat_mboot_finish(void)
  246. {
  247. }
  248. #endif /* MEASURED_BOOT || DICE_PROTECTION_ENVIRONMENT */
  249. /*******************************************************************************
  250. * Mandatory BL2 functions
  251. ******************************************************************************/
  252. void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3);
  253. void bl2_plat_arch_setup(void);
  254. void bl2_platform_setup(void);
  255. struct meminfo *bl2_plat_sec_mem_layout(void);
  256. /*
  257. * This function can be used by the platforms to update/use image
  258. * information for given `image_id`.
  259. */
  260. int bl2_plat_handle_pre_image_load(unsigned int image_id);
  261. int bl2_plat_handle_post_image_load(unsigned int image_id);
  262. /*******************************************************************************
  263. * Optional BL2 functions (may be overridden)
  264. ******************************************************************************/
  265. #if (MEASURED_BOOT || DICE_PROTECTION_ENVIRONMENT)
  266. void bl2_plat_mboot_init(void);
  267. void bl2_plat_mboot_finish(void);
  268. #else
  269. static inline void bl2_plat_mboot_init(void)
  270. {
  271. }
  272. static inline void bl2_plat_mboot_finish(void)
  273. {
  274. }
  275. #endif /* MEASURED_BOOT || DICE_PROTECTION_ENVIRONMENTs */
  276. /*******************************************************************************
  277. * Mandatory BL2 at EL3 functions: Must be implemented
  278. * if RESET_TO_BL2 image is supported
  279. ******************************************************************************/
  280. void bl2_el3_early_platform_setup(u_register_t arg0, u_register_t arg1,
  281. u_register_t arg2, u_register_t arg3);
  282. void bl2_el3_plat_arch_setup(void);
  283. /*******************************************************************************
  284. * Optional BL2 at EL3 functions (may be overridden)
  285. ******************************************************************************/
  286. void bl2_el3_plat_prepare_exit(void);
  287. /*******************************************************************************
  288. * Mandatory BL2U functions.
  289. ******************************************************************************/
  290. void bl2u_early_platform_setup(struct meminfo *mem_layout,
  291. void *plat_info);
  292. void bl2u_plat_arch_setup(void);
  293. void bl2u_platform_setup(void);
  294. /*******************************************************************************
  295. * Conditionally mandatory BL2U functions for CSS platforms.
  296. ******************************************************************************/
  297. /*
  298. * This function is used to perform any platform-specific actions required to
  299. * handle the BL2U_SCP firmware.
  300. */
  301. int bl2u_plat_handle_scp_bl2u(void);
  302. /*******************************************************************************
  303. * Mandatory BL31 functions
  304. ******************************************************************************/
  305. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  306. u_register_t arg2, u_register_t arg3);
  307. void bl31_plat_arch_setup(void);
  308. void bl31_platform_setup(void);
  309. void bl31_plat_runtime_setup(void);
  310. struct entry_point_info *bl31_plat_get_next_image_ep_info(uint32_t type);
  311. /*******************************************************************************
  312. * Mandatory PSCI functions (BL31)
  313. ******************************************************************************/
  314. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  315. const struct plat_psci_ops **psci_ops);
  316. const unsigned char *plat_get_power_domain_tree_desc(void);
  317. /*******************************************************************************
  318. * Optional PSCI functions (BL31).
  319. ******************************************************************************/
  320. void plat_psci_stat_accounting_start(const psci_power_state_t *state_info);
  321. void plat_psci_stat_accounting_stop(const psci_power_state_t *state_info);
  322. u_register_t plat_psci_stat_get_residency(unsigned int lvl,
  323. const psci_power_state_t *state_info,
  324. unsigned int last_cpu_idx);
  325. plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
  326. const plat_local_state_t *states,
  327. unsigned int ncpu);
  328. /*******************************************************************************
  329. * Mandatory BL31 functions when ENABLE_RME=1
  330. ******************************************************************************/
  331. #if ENABLE_RME
  332. int plat_rmmd_get_cca_attest_token(uintptr_t buf, size_t *len,
  333. uintptr_t hash, size_t hash_size,
  334. uint64_t *remaining_len);
  335. int plat_rmmd_get_cca_realm_attest_key(uintptr_t buf, size_t *len,
  336. unsigned int type);
  337. /* The following 3 functions are to be implement if
  338. * RMMD_ENABLE_EL3_TOKEN_SIGN=1.
  339. * The following three functions are expected to return E_RMM_* error codes.
  340. */
  341. int plat_rmmd_el3_token_sign_get_rak_pub(uintptr_t buf, size_t *len,
  342. unsigned int type);
  343. int plat_rmmd_el3_token_sign_push_req(
  344. const struct el3_token_sign_request *req);
  345. int plat_rmmd_el3_token_sign_pull_resp(struct el3_token_sign_response *resp);
  346. size_t plat_rmmd_get_el3_rmm_shared_mem(uintptr_t *shared);
  347. int plat_rmmd_load_manifest(struct rmm_manifest *manifest);
  348. #endif
  349. /*******************************************************************************
  350. * Optional BL31 functions (may be overridden)
  351. ******************************************************************************/
  352. void bl31_plat_enable_mmu(uint32_t flags);
  353. /*******************************************************************************
  354. * Optional BL32 functions (may be overridden)
  355. ******************************************************************************/
  356. void bl32_plat_enable_mmu(uint32_t flags);
  357. /*******************************************************************************
  358. * Trusted Board Boot functions
  359. ******************************************************************************/
  360. int plat_get_rotpk_info(void *cookie, void **key_ptr, unsigned int *key_len,
  361. unsigned int *flags);
  362. int plat_get_nv_ctr(void *cookie, unsigned int *nv_ctr);
  363. int plat_set_nv_ctr(void *cookie, unsigned int nv_ctr);
  364. int plat_set_nv_ctr2(void *cookie, const struct auth_img_desc_s *img_desc,
  365. unsigned int nv_ctr);
  366. int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size);
  367. int plat_get_enc_key_info(enum fw_enc_status_t fw_enc_status, uint8_t *key,
  368. size_t *key_len, unsigned int *flags,
  369. const uint8_t *img_id, size_t img_id_len);
  370. /*******************************************************************************
  371. * Secure Partitions functions
  372. ******************************************************************************/
  373. const struct mmap_region *plat_get_secure_partition_mmap(void *cookie);
  374. const struct spm_mm_boot_info *plat_get_secure_partition_boot_info(
  375. void *cookie);
  376. int plat_spm_sp_rd_load(struct sp_res_desc *rd, const void *ptr, size_t size);
  377. int plat_spm_sp_get_next_address(void **sp_base, size_t *sp_size,
  378. void **rd_base, size_t *rd_size);
  379. #if defined(SPD_spmd)
  380. int plat_spm_core_manifest_load(spmc_manifest_attribute_t *manifest,
  381. const void *pm_addr);
  382. #endif
  383. #if defined(SPMC_AT_EL3)
  384. int plat_spmc_shmem_datastore_get(uint8_t **datastore, size_t *size);
  385. #endif
  386. /*******************************************************************************
  387. * Mandatory BL image load functions(may be overridden).
  388. ******************************************************************************/
  389. /*
  390. * This function returns pointer to the list of images that the
  391. * platform has populated to load.
  392. */
  393. struct bl_load_info *plat_get_bl_image_load_info(void);
  394. /*
  395. * This function returns a pointer to the shared memory that the
  396. * platform has kept aside to pass trusted firmware related
  397. * information that next BL image could need.
  398. */
  399. struct bl_params *plat_get_next_bl_params(void);
  400. /*
  401. * This function flushes to main memory all the params that are
  402. * passed to next image.
  403. */
  404. void plat_flush_next_bl_params(void);
  405. /*
  406. * The below function enable Trusted Firmware components like SPDs which
  407. * haven't migrated to the new platform API to compile on platforms which
  408. * have the compatibility layer disabled.
  409. */
  410. unsigned int platform_core_pos_helper(unsigned long mpidr);
  411. /*
  412. * Optional function to get SOC version
  413. */
  414. int32_t plat_get_soc_version(void);
  415. /*
  416. * Optional function to get SOC revision
  417. */
  418. int32_t plat_get_soc_revision(void);
  419. /*
  420. * Optional function to check for SMCCC function availability for platform
  421. */
  422. int32_t plat_is_smccc_feature_available(u_register_t fid);
  423. /*******************************************************************************
  424. * FWU platform specific functions
  425. ******************************************************************************/
  426. int plat_fwu_set_metadata_image_source(unsigned int image_id,
  427. uintptr_t *dev_handle,
  428. uintptr_t *image_spec);
  429. void plat_fwu_set_images_source(const struct fwu_metadata *metadata);
  430. uint32_t plat_fwu_get_boot_idx(void);
  431. /*
  432. * Optional function to indicate if cache management operations can be
  433. * performed.
  434. */
  435. #if CONDITIONAL_CMO
  436. uint64_t plat_can_cmo(void);
  437. #else
  438. static inline uint64_t plat_can_cmo(void)
  439. {
  440. return 1;
  441. }
  442. #endif /* CONDITIONAL_CMO */
  443. #endif /* PLATFORM_H */