1b4315306SDan Handley /* 2*a6ffddecSMax Shvetsov * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. 3b4315306SDan Handley * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5b4315306SDan Handley */ 615b94cc1SAntonio Nino Diaz #ifndef PLAT_ARM_H 715b94cc1SAntonio Nino Diaz #define PLAT_ARM_H 8b4315306SDan Handley 9b4315306SDan Handley #include <stdint.h> 1009d40e0eSAntonio Nino Diaz 1109d40e0eSAntonio Nino Diaz #include <drivers/arm/tzc_common.h> 1209d40e0eSAntonio Nino Diaz #include <lib/bakery_lock.h> 1309d40e0eSAntonio Nino Diaz #include <lib/cassert.h> 1409d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h> 1509d40e0eSAntonio Nino Diaz #include <lib/spinlock.h> 1609d40e0eSAntonio Nino Diaz #include <lib/utils_def.h> 1709d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h> 18b4315306SDan Handley 19afc931f5SSandrine Bailleux /******************************************************************************* 20afc931f5SSandrine Bailleux * Forward declarations 21afc931f5SSandrine Bailleux ******************************************************************************/ 22afc931f5SSandrine Bailleux struct meminfo; 23a8aa7fecSYatharth Kochar struct image_info; 24cab0b5b0SSoby Mathew struct bl_params; 25afc931f5SSandrine Bailleux 2623411d2cSSummer Qin typedef struct arm_tzc_regions_info { 2723411d2cSSummer Qin unsigned long long base; 2823411d2cSSummer Qin unsigned long long end; 29af6491f8SAntonio Nino Diaz unsigned int sec_attr; 3023411d2cSSummer Qin unsigned int nsaid_permissions; 3123411d2cSSummer Qin } arm_tzc_regions_info_t; 3223411d2cSSummer Qin 3323411d2cSSummer Qin /******************************************************************************* 3423411d2cSSummer Qin * Default mapping definition of the TrustZone Controller for ARM standard 3523411d2cSSummer Qin * platforms. 3623411d2cSSummer Qin * Configure: 3723411d2cSSummer Qin * - Region 0 with no access; 3823411d2cSSummer Qin * - Region 1 with secure access only; 3923411d2cSSummer Qin * - the remaining DRAM regions access from the given Non-Secure masters. 4023411d2cSSummer Qin ******************************************************************************/ 413f3c341aSPaul Beesley #if SPM_MM 4223411d2cSSummer Qin #define ARM_TZC_REGIONS_DEF \ 4323411d2cSSummer Qin {ARM_AP_TZC_DRAM1_BASE, ARM_EL3_TZC_DRAM1_END, \ 4423411d2cSSummer Qin TZC_REGION_S_RDWR, 0}, \ 4523411d2cSSummer Qin {ARM_NS_DRAM1_BASE, ARM_NS_DRAM1_END, ARM_TZC_NS_DRAM_S_ACCESS, \ 4623411d2cSSummer Qin PLAT_ARM_TZC_NS_DEV_ACCESS}, \ 4723411d2cSSummer Qin {ARM_DRAM2_BASE, ARM_DRAM2_END, ARM_TZC_NS_DRAM_S_ACCESS, \ 4823411d2cSSummer Qin PLAT_ARM_TZC_NS_DEV_ACCESS}, \ 490560efb9SArd Biesheuvel {PLAT_SP_IMAGE_NS_BUF_BASE, (PLAT_SP_IMAGE_NS_BUF_BASE + \ 500560efb9SArd Biesheuvel PLAT_SP_IMAGE_NS_BUF_SIZE) - 1, TZC_REGION_S_NONE, \ 5123411d2cSSummer Qin PLAT_ARM_TZC_NS_DEV_ACCESS} 5223411d2cSSummer Qin 5323411d2cSSummer Qin #else 5423411d2cSSummer Qin #define ARM_TZC_REGIONS_DEF \ 5523411d2cSSummer Qin {ARM_AP_TZC_DRAM1_BASE, ARM_EL3_TZC_DRAM1_END, \ 5623411d2cSSummer Qin TZC_REGION_S_RDWR, 0}, \ 5723411d2cSSummer Qin {ARM_NS_DRAM1_BASE, ARM_NS_DRAM1_END, ARM_TZC_NS_DRAM_S_ACCESS, \ 5823411d2cSSummer Qin PLAT_ARM_TZC_NS_DEV_ACCESS}, \ 5923411d2cSSummer Qin {ARM_DRAM2_BASE, ARM_DRAM2_END, ARM_TZC_NS_DRAM_S_ACCESS, \ 6023411d2cSSummer Qin PLAT_ARM_TZC_NS_DEV_ACCESS} 6123411d2cSSummer Qin #endif 6223411d2cSSummer Qin 63b4315306SDan Handley #define ARM_CASSERT_MMAP \ 64053b4f92SChris Kay CASSERT((ARRAY_SIZE(plat_arm_mmap) - 1) <= PLAT_ARM_MMAP_ENTRIES, \ 65053b4f92SChris Kay assert_plat_arm_mmap_mismatch); \ 66053b4f92SChris Kay CASSERT((PLAT_ARM_MMAP_ENTRIES + ARM_BL_REGIONS) \ 67b4315306SDan Handley <= MAX_MMAP_REGIONS, \ 68b4315306SDan Handley assert_max_mmap_regions); 69b4315306SDan Handley 701eb735d7SRoberto Vargas void arm_setup_romlib(void); 711eb735d7SRoberto Vargas 72402b3cf8SJulius Werner #if defined(IMAGE_BL31) || (!defined(__aarch64__) && defined(IMAGE_BL32)) 73b4315306SDan Handley /* 74b4315306SDan Handley * Use this macro to instantiate lock before it is used in below 75b4315306SDan Handley * arm_lock_xxx() macros 76b4315306SDan Handley */ 771931d1d7SSandrine Bailleux #define ARM_INSTANTIATE_LOCK static DEFINE_BAKERY_LOCK(arm_lock) 78c04a3b6cSSoby Mathew #define ARM_LOCK_GET_INSTANCE (&arm_lock) 7932aee841SRoberto Vargas 8032aee841SRoberto Vargas #if !HW_ASSISTED_COHERENCY 8132aee841SRoberto Vargas #define ARM_SCMI_INSTANTIATE_LOCK DEFINE_BAKERY_LOCK(arm_scmi_lock) 8232aee841SRoberto Vargas #else 8332aee841SRoberto Vargas #define ARM_SCMI_INSTANTIATE_LOCK spinlock_t arm_scmi_lock 8432aee841SRoberto Vargas #endif 8532aee841SRoberto Vargas #define ARM_SCMI_LOCK_GET_INSTANCE (&arm_scmi_lock) 8632aee841SRoberto Vargas 87b4315306SDan Handley /* 88b4315306SDan Handley * These are wrapper macros to the Coherent Memory Bakery Lock API. 89b4315306SDan Handley */ 90b4315306SDan Handley #define arm_lock_init() bakery_lock_init(&arm_lock) 91b4315306SDan Handley #define arm_lock_get() bakery_lock_get(&arm_lock) 92b4315306SDan Handley #define arm_lock_release() bakery_lock_release(&arm_lock) 93b4315306SDan Handley 94b4315306SDan Handley #else 95b4315306SDan Handley 96b4315306SDan Handley /* 976f249345SYatharth Kochar * Empty macros for all other BL stages other than BL31 and BL32 98b4315306SDan Handley */ 9919583169SJeenu Viswambharan #define ARM_INSTANTIATE_LOCK static int arm_lock __unused 100c04a3b6cSSoby Mathew #define ARM_LOCK_GET_INSTANCE 0 101b4315306SDan Handley #define arm_lock_init() 102b4315306SDan Handley #define arm_lock_get() 103b4315306SDan Handley #define arm_lock_release() 104b4315306SDan Handley 105402b3cf8SJulius Werner #endif /* defined(IMAGE_BL31) || (!defined(__aarch64__) && defined(IMAGE_BL32)) */ 106b4315306SDan Handley 1072204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC 1082204afdeSSoby Mathew /* 1092204afdeSSoby Mathew * Macros used to parse state information from State-ID if it is using the 1102204afdeSSoby Mathew * recommended encoding for State-ID. 1112204afdeSSoby Mathew */ 1122204afdeSSoby Mathew #define ARM_LOCAL_PSTATE_WIDTH 4 1132204afdeSSoby Mathew #define ARM_LOCAL_PSTATE_MASK ((1 << ARM_LOCAL_PSTATE_WIDTH) - 1) 1142204afdeSSoby Mathew 1152204afdeSSoby Mathew /* Macros to construct the composite power state */ 1162204afdeSSoby Mathew 1172204afdeSSoby Mathew /* Make composite power state parameter till power level 0 */ 1182204afdeSSoby Mathew #if PSCI_EXTENDED_STATE_ID 1192204afdeSSoby Mathew 1202204afdeSSoby Mathew #define arm_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ 1212204afdeSSoby Mathew (((lvl0_state) << PSTATE_ID_SHIFT) | ((type) << PSTATE_TYPE_SHIFT)) 1222204afdeSSoby Mathew #else 1232204afdeSSoby Mathew #define arm_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ 1242204afdeSSoby Mathew (((lvl0_state) << PSTATE_ID_SHIFT) | \ 1252204afdeSSoby Mathew ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \ 1262204afdeSSoby Mathew ((type) << PSTATE_TYPE_SHIFT)) 1272204afdeSSoby Mathew #endif /* __PSCI_EXTENDED_STATE_ID__ */ 1282204afdeSSoby Mathew 1292204afdeSSoby Mathew /* Make composite power state parameter till power level 1 */ 1302204afdeSSoby Mathew #define arm_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \ 1312204afdeSSoby Mathew (((lvl1_state) << ARM_LOCAL_PSTATE_WIDTH) | \ 1322204afdeSSoby Mathew arm_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type)) 1332204afdeSSoby Mathew 1345f3a6030SSoby Mathew /* Make composite power state parameter till power level 2 */ 1355f3a6030SSoby Mathew #define arm_make_pwrstate_lvl2(lvl2_state, lvl1_state, lvl0_state, pwr_lvl, type) \ 1365f3a6030SSoby Mathew (((lvl2_state) << (ARM_LOCAL_PSTATE_WIDTH * 2)) | \ 1375f3a6030SSoby Mathew arm_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type)) 1385f3a6030SSoby Mathew 1392204afdeSSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */ 1402204afdeSSoby Mathew 141b10d4499SJeenu Viswambharan /* ARM State switch error codes */ 142b10d4499SJeenu Viswambharan #define STATE_SW_E_PARAM (-2) 143b10d4499SJeenu Viswambharan #define STATE_SW_E_DENIED (-3) 144b4315306SDan Handley 145*a6ffddecSMax Shvetsov /* plat_get_rotpk_info() flags */ 146*a6ffddecSMax Shvetsov #define ARM_ROTPK_REGS_ID 1 147*a6ffddecSMax Shvetsov #define ARM_ROTPK_DEVEL_RSA_ID 2 148*a6ffddecSMax Shvetsov #define ARM_ROTPK_DEVEL_ECDSA_ID 3 149*a6ffddecSMax Shvetsov 150b4315306SDan Handley /* IO storage utility functions */ 151b4315306SDan Handley void arm_io_setup(void); 152b4315306SDan Handley 153b4315306SDan Handley /* Security utility functions */ 15423411d2cSSummer Qin void arm_tzc400_setup(const arm_tzc_regions_info_t *tzc_regions); 155618f0feeSVikram Kanigiri struct tzc_dmc500_driver_data; 15623411d2cSSummer Qin void arm_tzc_dmc500_setup(struct tzc_dmc500_driver_data *plat_driver_data, 15723411d2cSSummer Qin const arm_tzc_regions_info_t *tzc_regions); 158b4315306SDan Handley 15988a0523eSAntonio Nino Diaz /* Console utility functions */ 16088a0523eSAntonio Nino Diaz void arm_console_boot_init(void); 16188a0523eSAntonio Nino Diaz void arm_console_boot_end(void); 16288a0523eSAntonio Nino Diaz void arm_console_runtime_init(void); 16388a0523eSAntonio Nino Diaz void arm_console_runtime_end(void); 16488a0523eSAntonio Nino Diaz 165c1bb8a05SSoby Mathew /* Systimer utility function */ 166c1bb8a05SSoby Mathew void arm_configure_sys_timer(void); 167c1bb8a05SSoby Mathew 168b4315306SDan Handley /* PM utility functions */ 16938dce70fSSoby Mathew int arm_validate_power_state(unsigned int power_state, 17038dce70fSSoby Mathew psci_power_state_t *req_state); 17171e7a4e5SJeenu Viswambharan int arm_validate_psci_entrypoint(uintptr_t entrypoint); 172f9e858b1SSoby Mathew int arm_validate_ns_entrypoint(uintptr_t entrypoint); 173e35a3fb5SSoby Mathew void arm_system_pwr_domain_save(void); 174c1bb8a05SSoby Mathew void arm_system_pwr_domain_resume(void); 175dc6aad2eSRoberto Vargas int arm_psci_read_mem_protect(int *enabled); 176f145403cSRoberto Vargas int arm_nor_psci_write_mem_protect(int val); 177638b034cSRoberto Vargas void arm_nor_psci_do_static_mem_protect(void); 178638b034cSRoberto Vargas void arm_nor_psci_do_dyn_mem_protect(void); 179f145403cSRoberto Vargas int arm_psci_mem_protect_chk(uintptr_t base, u_register_t length); 18038dce70fSSoby Mathew 18138dce70fSSoby Mathew /* Topology utility function */ 18238dce70fSSoby Mathew int arm_check_mpidr(u_register_t mpidr); 183b4315306SDan Handley 184b4315306SDan Handley /* BL1 utility functions */ 185b4315306SDan Handley void arm_bl1_early_platform_setup(void); 186b4315306SDan Handley void arm_bl1_platform_setup(void); 187b4315306SDan Handley void arm_bl1_plat_arch_setup(void); 188b4315306SDan Handley 189b4315306SDan Handley /* BL2 utility functions */ 190cab0b5b0SSoby Mathew void arm_bl2_early_platform_setup(uintptr_t tb_fw_config, struct meminfo *mem_layout); 191b4315306SDan Handley void arm_bl2_platform_setup(void); 192b4315306SDan Handley void arm_bl2_plat_arch_setup(void); 193b4315306SDan Handley uint32_t arm_get_spsr_for_bl32_entry(void); 194b4315306SDan Handley uint32_t arm_get_spsr_for_bl33_entry(void); 195609e053cSAmbroise Vincent int arm_bl2_plat_handle_post_image_load(unsigned int image_id); 19607570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id); 1975b8d50e4SSathees Balya struct bl_params *arm_get_next_bl_params(void); 198b4315306SDan Handley 19981528dbcSRoberto Vargas /* BL2 at EL3 functions */ 20081528dbcSRoberto Vargas void arm_bl2_el3_early_platform_setup(void); 20181528dbcSRoberto Vargas void arm_bl2_el3_plat_arch_setup(void); 20281528dbcSRoberto Vargas 203dcda29f6SYatharth Kochar /* BL2U utility functions */ 204dcda29f6SYatharth Kochar void arm_bl2u_early_platform_setup(struct meminfo *mem_layout, 205dcda29f6SYatharth Kochar void *plat_info); 206dcda29f6SYatharth Kochar void arm_bl2u_platform_setup(void); 207dcda29f6SYatharth Kochar void arm_bl2u_plat_arch_setup(void); 208dcda29f6SYatharth Kochar 209d178637dSJuan Castillo /* BL31 utility functions */ 2100c306cc0SSoby Mathew void arm_bl31_early_platform_setup(void *from_bl2, uintptr_t soc_fw_config, 2110c306cc0SSoby Mathew uintptr_t hw_config, void *plat_params_from_bl2); 212b4315306SDan Handley void arm_bl31_platform_setup(void); 213080225daSSoby Mathew void arm_bl31_plat_runtime_setup(void); 214b4315306SDan Handley void arm_bl31_plat_arch_setup(void); 215b4315306SDan Handley 216b4315306SDan Handley /* TSP utility functions */ 217b4315306SDan Handley void arm_tsp_early_platform_setup(void); 218b4315306SDan Handley 219181bbd41SSoby Mathew /* SP_MIN utility functions */ 2200c306cc0SSoby Mathew void arm_sp_min_early_platform_setup(void *from_bl2, uintptr_t tos_fw_config, 2210c306cc0SSoby Mathew uintptr_t hw_config, void *plat_params_from_bl2); 22221568304SDimitris Papastamos void arm_sp_min_plat_runtime_setup(void); 223181bbd41SSoby Mathew 224436223deSYatharth Kochar /* FIP TOC validity check */ 225436223deSYatharth Kochar int arm_io_is_toc_valid(void); 226b4315306SDan Handley 227c228956aSSoby Mathew /* Utility functions for Dynamic Config */ 228c228956aSSoby Mathew void arm_load_tb_fw_config(void); 229cab0b5b0SSoby Mathew void arm_bl2_set_tb_cfg_addr(void *dtb); 230cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void); 231ba597da7SJohn Tsichritzis void arm_bl1_set_mbedtls_heap(void); 232ba597da7SJohn Tsichritzis int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size); 233c228956aSSoby Mathew 234b4315306SDan Handley /* 235cb4adb0dSDaniel Boulby * Free the memory storing initialization code only used during an images boot 236cb4adb0dSDaniel Boulby * time so it can be reclaimed for runtime data 237cb4adb0dSDaniel Boulby */ 238cb4adb0dSDaniel Boulby void arm_free_init_memory(void); 239cb4adb0dSDaniel Boulby 240cb4adb0dSDaniel Boulby /* 241b4315306SDan Handley * Mandatory functions required in ARM standard platforms 242b4315306SDan Handley */ 2430108047aSSoby Mathew unsigned int plat_arm_get_cluster_core_count(u_register_t mpidr); 24427573c59SAchin Gupta void plat_arm_gic_driver_init(void); 245b4315306SDan Handley void plat_arm_gic_init(void); 24627573c59SAchin Gupta void plat_arm_gic_cpuif_enable(void); 24727573c59SAchin Gupta void plat_arm_gic_cpuif_disable(void); 248d17b953aSJeenu Viswambharan void plat_arm_gic_redistif_on(void); 249d17b953aSJeenu Viswambharan void plat_arm_gic_redistif_off(void); 25027573c59SAchin Gupta void plat_arm_gic_pcpu_init(void); 251e35a3fb5SSoby Mathew void plat_arm_gic_save(void); 252e35a3fb5SSoby Mathew void plat_arm_gic_resume(void); 253b4315306SDan Handley void plat_arm_security_setup(void); 254b4315306SDan Handley void plat_arm_pwrc_setup(void); 2556355f234SVikram Kanigiri void plat_arm_interconnect_init(void); 2566355f234SVikram Kanigiri void plat_arm_interconnect_enter_coherency(void); 2576355f234SVikram Kanigiri void plat_arm_interconnect_exit_coherency(void); 2582a246d2eSDimitris Papastamos void plat_arm_program_trusted_mailbox(uintptr_t address); 2594da6f6cdSSathees Balya int plat_arm_bl1_fwu_needed(void); 26037b70031SAmbroise Vincent __dead2 void plat_arm_error_handler(int err); 261b4315306SDan Handley 26274c21244SVijayenthiran Subramaniam /* 263*a6ffddecSMax Shvetsov * Optional functions in ARM standard platforms 26474c21244SVijayenthiran Subramaniam */ 26574c21244SVijayenthiran Subramaniam void plat_arm_override_gicr_frames(const uintptr_t *plat_gicr_frames); 266*a6ffddecSMax Shvetsov int arm_get_rotpk_info(void **key_ptr, unsigned int *key_len, 267*a6ffddecSMax Shvetsov unsigned int *flags); 268*a6ffddecSMax Shvetsov int arm_get_rotpk_info_regs(void **key_ptr, unsigned int *key_len, 269*a6ffddecSMax Shvetsov unsigned int *flags); 270*a6ffddecSMax Shvetsov int arm_get_rotpk_info_cc(void **key_ptr, unsigned int *key_len, 271*a6ffddecSMax Shvetsov unsigned int *flags); 272*a6ffddecSMax Shvetsov int arm_get_rotpk_info_dev(void **key_ptr, unsigned int *key_len, 273*a6ffddecSMax Shvetsov unsigned int *flags); 27474c21244SVijayenthiran Subramaniam 275d8d6cf24SSummer Qin #if ARM_PLAT_MT 276d8d6cf24SSummer Qin unsigned int plat_arm_get_cpu_pe_count(u_register_t mpidr); 277d8d6cf24SSummer Qin #endif 278d8d6cf24SSummer Qin 279a8aa7fecSYatharth Kochar /* 280a8aa7fecSYatharth Kochar * This function is called after loading SCP_BL2 image and it is used to perform 281a8aa7fecSYatharth Kochar * any platform-specific actions required to handle the SCP firmware. 282a8aa7fecSYatharth Kochar */ 283a8aa7fecSYatharth Kochar int plat_arm_bl2_handle_scp_bl2(struct image_info *scp_bl2_image_info); 284a8aa7fecSYatharth Kochar 285b4315306SDan Handley /* 286b4315306SDan Handley * Optional functions required in ARM standard platforms 287b4315306SDan Handley */ 288b4315306SDan Handley void plat_arm_io_setup(void); 289b4315306SDan Handley int plat_arm_get_alt_image_source( 29016948ae1SJuan Castillo unsigned int image_id, 29116948ae1SJuan Castillo uintptr_t *dev_handle, 29216948ae1SJuan Castillo uintptr_t *image_spec); 29338dce70fSSoby Mathew unsigned int plat_arm_calc_core_pos(u_register_t mpidr); 29465cb1c4cSVikram Kanigiri const mmap_region_t *plat_arm_get_mmap(void); 295b4315306SDan Handley 2965486a965SSoby Mathew /* Allow platform to override psci_pm_ops during runtime */ 2975486a965SSoby Mathew const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops); 2985486a965SSoby Mathew 299b10d4499SJeenu Viswambharan /* Execution state switch in ARM platforms */ 300b10d4499SJeenu Viswambharan int arm_execution_state_switch(unsigned int smc_fid, 301b10d4499SJeenu Viswambharan uint32_t pc_hi, 302b10d4499SJeenu Viswambharan uint32_t pc_lo, 303b10d4499SJeenu Viswambharan uint32_t cookie_hi, 304b10d4499SJeenu Viswambharan uint32_t cookie_lo, 305b10d4499SJeenu Viswambharan void *handle); 306b10d4499SJeenu Viswambharan 3070ed8c001SSoby Mathew /* Optional functions for SP_MIN */ 3080ed8c001SSoby Mathew void plat_arm_sp_min_early_platform_setup(u_register_t arg0, u_register_t arg1, 3090ed8c001SSoby Mathew u_register_t arg2, u_register_t arg3); 3100ed8c001SSoby Mathew 3111af540efSRoberto Vargas /* global variables */ 3121af540efSRoberto Vargas extern plat_psci_ops_t plat_arm_psci_pm_ops; 3131af540efSRoberto Vargas extern const mmap_region_t plat_arm_mmap[]; 314ecd62429SJeenu Viswambharan extern const unsigned int arm_pm_idle_states[]; 3151af540efSRoberto Vargas 316b0c97dafSAditya Angadi /* secure watchdog */ 317b0c97dafSAditya Angadi void plat_arm_secure_wdt_start(void); 318b0c97dafSAditya Angadi void plat_arm_secure_wdt_stop(void); 319b0c97dafSAditya Angadi 32015b94cc1SAntonio Nino Diaz #endif /* PLAT_ARM_H */ 321