1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2016-2020, Linaro Limited 4 * Copyright (c) 2014, STMicroelectronics International N.V. 5 */ 6 7 #include <arm.h> 8 #include <console.h> 9 #include <drivers/gic.h> 10 #include <drivers/hfic.h> 11 #include <drivers/pl011.h> 12 #include <drivers/tzc400.h> 13 #include <initcall.h> 14 #include <keep.h> 15 #include <kernel/boot.h> 16 #include <kernel/interrupt.h> 17 #include <kernel/misc.h> 18 #include <kernel/notif.h> 19 #include <kernel/panic.h> 20 #include <kernel/spinlock.h> 21 #include <kernel/tee_time.h> 22 #include <mm/core_memprot.h> 23 #include <mm/core_mmu.h> 24 #include <platform_config.h> 25 #include <sm/psci.h> 26 #include <stdint.h> 27 #include <string.h> 28 #include <trace.h> 29 30 static struct pl011_data console_data __nex_bss; 31 32 register_phys_mem_pgdir(MEM_AREA_IO_SEC, CONSOLE_UART_BASE, PL011_REG_SIZE); 33 #if defined(PLATFORM_FLAVOR_fvp) 34 register_phys_mem(MEM_AREA_RAM_SEC, TZCDRAM_BASE, TZCDRAM_SIZE); 35 #endif 36 #if defined(PLATFORM_FLAVOR_qemu_virt) 37 register_phys_mem_pgdir(MEM_AREA_IO_SEC, SECRAM_BASE, SECRAM_COHERENT_SIZE); 38 #endif 39 #ifdef DRAM0_BASE 40 register_ddr(DRAM0_BASE, DRAM0_SIZE); 41 #endif 42 #ifdef DRAM1_BASE 43 register_ddr(DRAM1_BASE, DRAM1_SIZE); 44 #endif 45 46 #ifdef CFG_GIC 47 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GICD_BASE, GIC_DIST_REG_SIZE); 48 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GICC_BASE, GIC_DIST_REG_SIZE); 49 50 void boot_primary_init_intc(void) 51 { 52 gic_init(GIC_BASE + GICC_OFFSET, GIC_BASE + GICD_OFFSET); 53 } 54 55 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 56 void boot_secondary_init_intc(void) 57 { 58 gic_cpu_init(); 59 } 60 #endif 61 #endif /*CFG_GIC*/ 62 63 #ifdef CFG_CORE_HAFNIUM_INTC 64 void boot_primary_init_intc(void) 65 { 66 hfic_init(); 67 } 68 #endif 69 70 void console_init(void) 71 { 72 pl011_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ, 73 CONSOLE_BAUDRATE); 74 register_serial_console(&console_data.chip); 75 } 76 77 #if (defined(CFG_GIC) || defined(CFG_CORE_HAFNIUM_INTC)) && \ 78 defined(IT_CONSOLE_UART) && \ 79 !defined(CFG_NS_VIRTUALIZATION) && \ 80 !(defined(CFG_WITH_ARM_TRUSTED_FW) && defined(CFG_ARM_GICV2)) 81 /* 82 * This cannot be enabled with TF-A and GICv3 because TF-A then need to 83 * assign the interrupt number of the UART to OP-TEE (S-EL1). Currently 84 * there's no way of TF-A to know which interrupts that OP-TEE will serve. 85 * If TF-A doesn't assign the interrupt we're enabling below to OP-TEE it 86 * will hang in EL3 since the interrupt will just be delivered again and 87 * again. 88 */ 89 90 static void read_console(void) 91 { 92 struct serial_chip *cons = &console_data.chip; 93 94 if (!cons->ops->getchar || !cons->ops->have_rx_data) 95 return; 96 97 while (cons->ops->have_rx_data(cons)) { 98 int ch __maybe_unused = cons->ops->getchar(cons); 99 100 DMSG("got 0x%x", ch); 101 } 102 } 103 104 static enum itr_return console_itr_cb(struct itr_handler *h __maybe_unused) 105 { 106 if (notif_async_is_started()) { 107 /* 108 * Asynchronous notifications are enabled, lets read from 109 * uart in the bottom half instead. 110 */ 111 itr_disable(IT_CONSOLE_UART); 112 notif_send_async(NOTIF_VALUE_DO_BOTTOM_HALF); 113 } else { 114 read_console(); 115 } 116 return ITRR_HANDLED; 117 } 118 119 static struct itr_handler console_itr = { 120 .it = IT_CONSOLE_UART, 121 .flags = ITRF_TRIGGER_LEVEL, 122 .handler = console_itr_cb, 123 }; 124 DECLARE_KEEP_PAGER(console_itr); 125 126 static void atomic_console_notif(struct notif_driver *ndrv __unused, 127 enum notif_event ev __maybe_unused) 128 { 129 DMSG("Asynchronous notifications started, event %d", (int)ev); 130 } 131 DECLARE_KEEP_PAGER(atomic_console_notif); 132 133 static void yielding_console_notif(struct notif_driver *ndrv __unused, 134 enum notif_event ev) 135 { 136 switch (ev) { 137 case NOTIF_EVENT_DO_BOTTOM_HALF: 138 read_console(); 139 itr_enable(IT_CONSOLE_UART); 140 break; 141 case NOTIF_EVENT_STOPPED: 142 DMSG("Asynchronous notifications stopped"); 143 itr_enable(IT_CONSOLE_UART); 144 break; 145 default: 146 EMSG("Unknown event %d", (int)ev); 147 } 148 } 149 150 struct notif_driver console_notif = { 151 .atomic_cb = atomic_console_notif, 152 .yielding_cb = yielding_console_notif, 153 }; 154 155 static TEE_Result init_console_itr(void) 156 { 157 itr_add(&console_itr); 158 itr_enable(IT_CONSOLE_UART); 159 if (IS_ENABLED(CFG_CORE_ASYNC_NOTIF)) 160 notif_register_driver(&console_notif); 161 return TEE_SUCCESS; 162 } 163 driver_init(init_console_itr); 164 #endif 165 166 #ifdef CFG_TZC400 167 register_phys_mem_pgdir(MEM_AREA_IO_SEC, TZC400_BASE, TZC400_REG_SIZE); 168 169 static TEE_Result init_tzc400(void) 170 { 171 void *va; 172 173 DMSG("Initializing TZC400"); 174 175 va = phys_to_virt(TZC400_BASE, MEM_AREA_IO_SEC, TZC400_REG_SIZE); 176 if (!va) { 177 EMSG("TZC400 not mapped"); 178 panic(); 179 } 180 181 tzc_init((vaddr_t)va); 182 tzc_dump_state(); 183 184 return TEE_SUCCESS; 185 } 186 187 service_init(init_tzc400); 188 #endif /*CFG_TZC400*/ 189 190 #if defined(PLATFORM_FLAVOR_qemu_virt) 191 static void release_secondary_early_hpen(size_t pos) 192 { 193 struct mailbox { 194 uint64_t ep; 195 uint64_t hpen[]; 196 } *mailbox; 197 198 if (cpu_mmu_enabled()) 199 mailbox = phys_to_virt(SECRAM_BASE, MEM_AREA_IO_SEC, 200 SECRAM_COHERENT_SIZE); 201 else 202 mailbox = (void *)SECRAM_BASE; 203 204 if (!mailbox) 205 panic(); 206 207 mailbox->ep = TEE_LOAD_ADDR; 208 dsb_ishst(); 209 mailbox->hpen[pos] = 1; 210 dsb_ishst(); 211 sev(); 212 } 213 214 int psci_cpu_on(uint32_t core_id, uint32_t entry, uint32_t context_id) 215 { 216 size_t pos = get_core_pos_mpidr(core_id); 217 static bool core_is_released[CFG_TEE_CORE_NB_CORE]; 218 219 if (!pos || pos >= CFG_TEE_CORE_NB_CORE) 220 return PSCI_RET_INVALID_PARAMETERS; 221 222 DMSG("core pos: %zu: ns_entry %#" PRIx32, pos, entry); 223 224 if (core_is_released[pos]) { 225 EMSG("core %zu already released", pos); 226 return PSCI_RET_DENIED; 227 } 228 core_is_released[pos] = true; 229 230 boot_set_core_ns_entry(pos, entry, context_id); 231 release_secondary_early_hpen(pos); 232 233 return PSCI_RET_SUCCESS; 234 } 235 #endif /*PLATFORM_FLAVOR_qemu_virt*/ 236