Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 1 | /* |
Chee Hong Ang | 2382b11 | 2020-04-24 21:51:00 +0800 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, ARM Limited and Contributors. All rights reserved. |
Abdul Halim, Muhammad Hadi Asyrafi | 2f94ca4 | 2020-08-05 22:40:46 +0800 | [diff] [blame] | 3 | * Copyright (c) 2019-2022, Intel Corporation. All rights reserved. |
Sieu Mun Tang | 7c14cea | 2024-02-02 23:23:12 +0800 | [diff] [blame^] | 4 | * Copyright (c) 2024, Altera Corporation. All rights reserved. |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 5 | * |
| 6 | * SPDX-License-Identifier: BSD-3-Clause |
| 7 | */ |
| 8 | |
| 9 | #include <arch.h> |
| 10 | #include <arch_helpers.h> |
| 11 | #include <assert.h> |
| 12 | #include <common/bl_common.h> |
| 13 | #include <drivers/arm/gicv2.h> |
| 14 | #include <drivers/ti/uart/uart_16550.h> |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 15 | #include <lib/mmio.h> |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 16 | #include <lib/xlat_tables/xlat_tables.h> |
Sieu Mun Tang | 7c14cea | 2024-02-02 23:23:12 +0800 | [diff] [blame^] | 17 | #include <plat/common/platform.h> |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 18 | |
Abdul Halim, Muhammad Hadi Asyrafi | 2f94ca4 | 2020-08-05 22:40:46 +0800 | [diff] [blame] | 19 | #include "ccu/ncore_ccu.h" |
Hadi Asyrafi | 593c4c5 | 2019-12-17 19:22:17 +0800 | [diff] [blame] | 20 | #include "socfpga_mailbox.h" |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 21 | #include "socfpga_private.h" |
Sieu Mun Tang | bd8da63 | 2022-09-28 15:58:28 +0800 | [diff] [blame] | 22 | #include "socfpga_sip_svc.h" |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 23 | |
Sieu Mun Tang | 7c14cea | 2024-02-02 23:23:12 +0800 | [diff] [blame^] | 24 | /* Get non-secure SPSR for BL33. Zephyr and Linux */ |
| 25 | uint32_t arm_get_spsr_for_bl33_entry(void); |
| 26 | |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 27 | static entry_point_info_t bl32_image_ep_info; |
| 28 | static entry_point_info_t bl33_image_ep_info; |
| 29 | |
| 30 | entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type) |
| 31 | { |
| 32 | entry_point_info_t *next_image_info; |
| 33 | |
| 34 | next_image_info = (type == NON_SECURE) ? |
| 35 | &bl33_image_ep_info : &bl32_image_ep_info; |
| 36 | |
| 37 | /* None of the images on this platform can have 0x0 as the entrypoint */ |
| 38 | if (next_image_info->pc) |
| 39 | return next_image_info; |
| 40 | else |
| 41 | return NULL; |
| 42 | } |
| 43 | |
Sieu Mun Tang | bd8da63 | 2022-09-28 15:58:28 +0800 | [diff] [blame] | 44 | void setup_smmu_secure_context(void) |
| 45 | { |
| 46 | /* |
| 47 | * Program SCR0 register (0xFA000000) |
| 48 | * to set SMCFCFG bit[21] to 0x1 which raise stream match conflict fault |
| 49 | * to set CLIENTPD bit[0] to 0x0 which enables SMMU for secure context |
| 50 | */ |
| 51 | mmio_write_32(0xFA000000, 0x00200000); |
| 52 | |
| 53 | /* |
| 54 | * Program SCR1 register (0xFA000004) |
| 55 | * to set NSNUMSMRGO bit[14:8] to 0x4 which stream mapping register |
| 56 | * for non-secure context and the rest will be secure context |
| 57 | * to set NSNUMCBO bit[5:0] to 0x4 which allocate context bank |
| 58 | * for non-secure context and the rest will be secure context |
| 59 | */ |
| 60 | mmio_write_32(0xFA000004, 0x00000404); |
| 61 | } |
| 62 | |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 63 | void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1, |
| 64 | u_register_t arg2, u_register_t arg3) |
| 65 | { |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 66 | static console_t console; |
Chee Hong Ang | 2382b11 | 2020-04-24 21:51:00 +0800 | [diff] [blame] | 67 | mmio_write_64(PLAT_SEC_ENTRY, PLAT_SEC_WARM_ENTRY); |
Boon Khai Ng | b19ac61 | 2021-08-06 01:16:46 +0800 | [diff] [blame] | 68 | console_16550_register(PLAT_INTEL_UART_BASE, PLAT_UART_CLOCK, |
| 69 | PLAT_BAUDRATE, &console); |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 70 | /* |
| 71 | * Check params passed from BL31 should not be NULL, |
| 72 | */ |
| 73 | void *from_bl2 = (void *) arg0; |
| 74 | |
Sieu Mun Tang | 7c14cea | 2024-02-02 23:23:12 +0800 | [diff] [blame^] | 75 | #if RESET_TO_BL31 |
| 76 | /* There are no parameters from BL2 if BL31 is a reset vector */ |
| 77 | assert(from_bl2 == NULL); |
| 78 | void *plat_params_from_bl2 = (void *) arg3; |
| 79 | |
| 80 | assert(plat_params_from_bl2 == NULL); |
| 81 | |
| 82 | /* Populate entry point information for BL33 */ |
| 83 | SET_PARAM_HEAD(&bl33_image_ep_info, |
| 84 | PARAM_EP, |
| 85 | VERSION_1, |
| 86 | 0); |
| 87 | |
| 88 | # if ARM_LINUX_KERNEL_AS_BL33 |
| 89 | /* |
| 90 | * According to the file ``Documentation/arm64/booting.txt`` of the |
| 91 | * Linux kernel tree, Linux expects the physical address of the device |
| 92 | * tree blob (DTB) in x0, while x1-x3 are reserved for future use and |
| 93 | * must be 0. |
| 94 | */ |
| 95 | bl33_image_ep_info.args.arg0 = (u_register_t)ARM_PRELOADED_DTB_BASE; |
| 96 | bl33_image_ep_info.args.arg1 = 0U; |
| 97 | bl33_image_ep_info.args.arg2 = 0U; |
| 98 | bl33_image_ep_info.args.arg3 = 0U; |
| 99 | # endif |
| 100 | |
| 101 | #else /* RESET_TO_BL31 */ |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 102 | bl_params_t *params_from_bl2 = (bl_params_t *)from_bl2; |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 103 | assert(params_from_bl2 != NULL); |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Copy BL32 (if populated by BL31) and BL33 entry point information. |
| 107 | * They are stored in Secure RAM, in BL31's address space. |
| 108 | */ |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 109 | if (params_from_bl2->h.type == PARAM_BL_PARAMS && |
| 110 | params_from_bl2->h.version >= VERSION_2) { |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 111 | bl_params_node_t *bl_params = params_from_bl2->head; |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 112 | while (bl_params) { |
| 113 | if (bl_params->image_id == BL33_IMAGE_ID) |
| 114 | bl33_image_ep_info = *bl_params->ep_info; |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 115 | bl_params = bl_params->next_params_info; |
| 116 | } |
| 117 | } else { |
| 118 | struct socfpga_bl31_params *arg_from_bl2 = |
| 119 | (struct socfpga_bl31_params *) from_bl2; |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 120 | assert(arg_from_bl2->h.type == PARAM_BL31); |
| 121 | assert(arg_from_bl2->h.version >= VERSION_1); |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 122 | bl32_image_ep_info = *arg_from_bl2->bl32_ep_info; |
| 123 | bl33_image_ep_info = *arg_from_bl2->bl33_ep_info; |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 124 | } |
Sieu Mun Tang | 7c14cea | 2024-02-02 23:23:12 +0800 | [diff] [blame^] | 125 | |
| 126 | bl33_image_ep_info.args.arg0 = (u_register_t)ARM_PRELOADED_DTB_BASE; |
| 127 | bl33_image_ep_info.args.arg1 = 0U; |
| 128 | bl33_image_ep_info.args.arg2 = 0U; |
| 129 | bl33_image_ep_info.args.arg3 = 0U; |
| 130 | #endif |
| 131 | |
| 132 | /* |
| 133 | * Tell BL31 where the non-trusted software image |
| 134 | * is located and the entry state information |
| 135 | */ |
| 136 | bl33_image_ep_info.pc = plat_get_ns_image_entrypoint(); |
| 137 | bl33_image_ep_info.spsr = arm_get_spsr_for_bl33_entry(); |
| 138 | |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 139 | SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE); |
| 140 | } |
| 141 | |
| 142 | static const interrupt_prop_t s10_interrupt_props[] = { |
Hadi Asyrafi | 9f5dfc9 | 2019-10-23 16:26:53 +0800 | [diff] [blame] | 143 | PLAT_INTEL_SOCFPGA_G1S_IRQ_PROPS(GICV2_INTR_GROUP0), |
| 144 | PLAT_INTEL_SOCFPGA_G0_IRQ_PROPS(GICV2_INTR_GROUP0) |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | static unsigned int target_mask_array[PLATFORM_CORE_COUNT]; |
| 148 | |
| 149 | static const gicv2_driver_data_t plat_gicv2_gic_data = { |
Hadi Asyrafi | 9f5dfc9 | 2019-10-23 16:26:53 +0800 | [diff] [blame] | 150 | .gicd_base = PLAT_INTEL_SOCFPGA_GICD_BASE, |
| 151 | .gicc_base = PLAT_INTEL_SOCFPGA_GICC_BASE, |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 152 | .interrupt_props = s10_interrupt_props, |
| 153 | .interrupt_props_num = ARRAY_SIZE(s10_interrupt_props), |
| 154 | .target_masks = target_mask_array, |
| 155 | .target_masks_num = ARRAY_SIZE(target_mask_array), |
| 156 | }; |
| 157 | |
| 158 | /******************************************************************************* |
| 159 | * Perform any BL3-1 platform setup code |
| 160 | ******************************************************************************/ |
| 161 | void bl31_platform_setup(void) |
| 162 | { |
Chee Hong Ang | 6474096 | 2020-05-11 00:55:01 +0800 | [diff] [blame] | 163 | socfpga_delay_timer_init(); |
| 164 | |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 165 | /* Initialize the gic cpu and distributor interfaces */ |
| 166 | gicv2_driver_init(&plat_gicv2_gic_data); |
| 167 | gicv2_distif_init(); |
| 168 | gicv2_pcpu_distif_init(); |
| 169 | gicv2_cpuif_enable(); |
Sieu Mun Tang | bd8da63 | 2022-09-28 15:58:28 +0800 | [diff] [blame] | 170 | setup_smmu_secure_context(); |
Hadi Asyrafi | 218d8fe | 2020-01-14 10:51:31 +0800 | [diff] [blame] | 171 | |
| 172 | /* Signal secondary CPUs to jump to BL31 (BL2 = U-boot SPL) */ |
| 173 | mmio_write_64(PLAT_CPU_RELEASE_ADDR, |
| 174 | (uint64_t)plat_secondary_cpus_bl31_entry); |
Hadi Asyrafi | 593c4c5 | 2019-12-17 19:22:17 +0800 | [diff] [blame] | 175 | |
| 176 | mailbox_hps_stage_notify(HPS_EXECUTION_STATE_SSBL); |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | const mmap_region_t plat_agilex_mmap[] = { |
| 180 | MAP_REGION_FLAT(DRAM_BASE, DRAM_SIZE, MT_MEMORY | MT_RW | MT_NS), |
| 181 | MAP_REGION_FLAT(DEVICE1_BASE, DEVICE1_SIZE, MT_DEVICE | MT_RW | MT_NS), |
Hadi Asyrafi | e944d22 | 2019-07-30 10:56:38 +0800 | [diff] [blame] | 182 | MAP_REGION_FLAT(DEVICE2_BASE, DEVICE2_SIZE, MT_DEVICE | MT_RW | MT_SECURE), |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 183 | MAP_REGION_FLAT(OCRAM_BASE, OCRAM_SIZE, |
| 184 | MT_NON_CACHEABLE | MT_RW | MT_SECURE), |
| 185 | MAP_REGION_FLAT(DEVICE3_BASE, DEVICE3_SIZE, |
| 186 | MT_DEVICE | MT_RW | MT_SECURE), |
| 187 | MAP_REGION_FLAT(MEM64_BASE, MEM64_SIZE, MT_DEVICE | MT_RW | MT_NS), |
| 188 | MAP_REGION_FLAT(DEVICE4_BASE, DEVICE4_SIZE, MT_DEVICE | MT_RW | MT_NS), |
Hadi Asyrafi | 5ae876f | 2019-10-23 17:58:06 +0800 | [diff] [blame] | 189 | {0} |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | /******************************************************************************* |
| 193 | * Perform the very early platform specific architectural setup here. At the |
Elyes Haouas | 2be03c0 | 2023-02-13 09:14:48 +0100 | [diff] [blame] | 194 | * moment this is only initializes the mmu in a quick and dirty way. |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 195 | ******************************************************************************/ |
| 196 | void bl31_plat_arch_setup(void) |
| 197 | { |
| 198 | const mmap_region_t bl_regions[] = { |
| 199 | MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE, |
| 200 | MT_MEMORY | MT_RW | MT_SECURE), |
| 201 | MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE, |
| 202 | MT_CODE | MT_SECURE), |
| 203 | MAP_REGION_FLAT(BL_RO_DATA_BASE, |
| 204 | BL_RO_DATA_END - BL_RO_DATA_BASE, |
| 205 | MT_RO_DATA | MT_SECURE), |
| 206 | #if USE_COHERENT_MEM |
| 207 | MAP_REGION_FLAT(BL_COHERENT_RAM_BASE, |
| 208 | BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE, |
| 209 | MT_DEVICE | MT_RW | MT_SECURE), |
| 210 | #endif |
Hadi Asyrafi | 5ae876f | 2019-10-23 17:58:06 +0800 | [diff] [blame] | 211 | {0} |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 212 | }; |
Hadi Asyrafi | 616da77 | 2019-06-27 11:34:03 +0800 | [diff] [blame] | 213 | setup_page_tables(bl_regions, plat_agilex_mmap); |
| 214 | enable_mmu_el3(0); |
| 215 | } |
| 216 | |
Sieu Mun Tang | 7c14cea | 2024-02-02 23:23:12 +0800 | [diff] [blame^] | 217 | /* Get non-secure image entrypoint for BL33. Zephyr and Linux */ |
| 218 | uintptr_t plat_get_ns_image_entrypoint(void) |
| 219 | { |
| 220 | #ifdef PRELOADED_BL33_BASE |
| 221 | return PRELOADED_BL33_BASE; |
| 222 | #else |
| 223 | return PLAT_NS_IMAGE_OFFSET; |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | /* Get non-secure SPSR for BL33. Zephyr and Linux */ |
| 228 | uint32_t arm_get_spsr_for_bl33_entry(void) |
| 229 | { |
| 230 | unsigned int mode; |
| 231 | uint32_t spsr; |
| 232 | |
| 233 | /* Figure out what mode we enter the non-secure world in */ |
| 234 | mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1; |
| 235 | |
| 236 | /* |
| 237 | * TODO: Consider the possibility of specifying the SPSR in |
| 238 | * the FIP ToC and allowing the platform to have a say as |
| 239 | * well. |
| 240 | */ |
| 241 | spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); |
| 242 | return spsr; |
| 243 | } |