1 /* 2 * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved. 3 * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 /* 9 * Top-level SMC handler for ZynqMP power management calls and 10 * IPI setup functions for communication with PMU. 11 */ 12 13 #include <errno.h> 14 15 #include <arch_helpers.h> 16 #include <common/runtime_svc.h> 17 #include <drivers/arm/gicv2.h> 18 #include <lib/mmio.h> 19 #include <lib/spinlock.h> 20 #include <plat/common/platform.h> 21 22 #include <plat_private.h> 23 #include "pm_client.h" 24 #include "pm_ipi.h" 25 #include "zynqmp_pm_api_sys.h" 26 #include "zynqmp_pm_defs.h" 27 28 /* pm_up = !0 - UP, pm_up = 0 - DOWN */ 29 static int32_t pm_up, ipi_irq_flag; 30 31 #if ZYNQMP_WDT_RESTART 32 static spinlock_t inc_lock; 33 static int active_cores = 0; 34 #endif 35 36 /** 37 * typedef pm_ctx_t - Structure which contains data for power management. 38 * @api_version: version of PM API, must match with one on PMU side. 39 * @payload: payload array used to store received. 40 * data from ipi buffer registers. 41 * 42 */ 43 typedef struct { 44 uint32_t api_version; 45 uint32_t payload[PAYLOAD_ARG_CNT]; 46 } pm_ctx_t; 47 48 static pm_ctx_t pm_ctx; 49 50 #if ZYNQMP_WDT_RESTART 51 /** 52 * trigger_wdt_restart() - Trigger warm restart event to APU cores. 53 * 54 * This function triggers SGI for all active APU CPUs. SGI handler then 55 * power down CPU and call system reset. 56 * 57 */ 58 static void trigger_wdt_restart(void) 59 { 60 uint32_t core_count = 0; 61 uint32_t core_status[3]; 62 uint32_t target_cpu_list = 0; 63 int i; 64 65 for (i = 0; i < 4; i++) { 66 pm_get_node_status(NODE_APU_0 + i, core_status); 67 if (core_status[0] == 1) { 68 core_count++; 69 target_cpu_list |= (1 << i); 70 } 71 } 72 73 spin_lock(&inc_lock); 74 active_cores = core_count; 75 spin_unlock(&inc_lock); 76 77 INFO("Active Cores: %d\n", active_cores); 78 79 for (i = PLATFORM_CORE_COUNT - 1; i >= 0; i--) { 80 if (target_cpu_list & (1 << i)) { 81 /* trigger SGI to active cores */ 82 plat_ic_raise_el3_sgi(ARM_IRQ_SEC_SGI_7, i); 83 } 84 } 85 } 86 87 /** 88 * ttc_fiq_handler() - TTC Handler for timer event. 89 * @id: number of the highest priority pending interrupt of the type 90 * that this handler was registered for. 91 * @flags: security state, bit[0]. 92 * @handle: pointer to 'cpu_context' structure of the current CPU for the 93 * security state specified in the 'flags' parameter. 94 * @cookie: unused. 95 * 96 * Function registered as INTR_TYPE_EL3 interrupt handler. 97 * 98 * When WDT event is received in PMU, PMU needs to notify master to do cleanup 99 * if required. PMU sets up timer and starts timer to overflow in zero time upon 100 * WDT event. TF-A handles this timer event and takes necessary action required 101 * for warm restart. 102 * 103 * In presence of non-secure software layers (EL1/2) sets the interrupt 104 * at registered entrance in GIC and informs that PMU responded or demands 105 * action. 106 * 107 * Return: 0 on success. 108 * 109 */ 110 static uint64_t ttc_fiq_handler(uint32_t id, uint32_t flags, void *handle, 111 void *cookie) 112 { 113 INFO("BL31: Got TTC FIQ\n"); 114 115 plat_ic_end_of_interrupt(id); 116 117 /* Clear TTC interrupt by reading interrupt register */ 118 mmio_read_32(TTC3_INTR_REGISTER_1); 119 120 /* Disable the timer interrupts */ 121 mmio_write_32(TTC3_INTR_ENABLE_1, 0); 122 123 trigger_wdt_restart(); 124 125 return 0; 126 } 127 128 /** 129 * zynqmp_sgi7_irq() - Handler for SGI7 IRQ. 130 * @id: number of the highest priority pending interrupt of the type 131 * that this handler was registered for. 132 * @flags: security state, bit[0]. 133 * @handle: pointer to 'cpu_context' structure of the current CPU for the 134 * security state specified in the 'flags' parameter. 135 * @cookie: unused. 136 * 137 * Function registered as INTR_TYPE_EL3 interrupt handler 138 * 139 * On receiving WDT event from PMU, TF-A generates SGI7 to all running CPUs. 140 * In response to SGI7 interrupt, each CPUs do clean up if required and last 141 * running CPU calls system restart. 142 * 143 * Return: This function does not return a value and it enters into wfi. 144 */ 145 static uint64_t __unused __dead2 zynqmp_sgi7_irq(uint32_t id, uint32_t flags, 146 void *handle, void *cookie) 147 { 148 int i; 149 uint32_t value; 150 151 /* enter wfi and stay there */ 152 INFO("Entering wfi\n"); 153 154 spin_lock(&inc_lock); 155 active_cores--; 156 157 for (i = 0; i < 4; i++) { 158 mmio_write_32(BASE_GICD_BASE + GICD_CPENDSGIR + 4 * i, 159 0xffffffff); 160 } 161 162 dsb(); 163 164 spin_unlock(&inc_lock); 165 166 if (active_cores == 0) { 167 pm_mmio_read(PMU_GLOBAL_GEN_STORAGE4, &value); 168 value = (value & RESTART_SCOPE_MASK) >> RESTART_SCOPE_SHIFT; 169 pm_system_shutdown(PMF_SHUTDOWN_TYPE_RESET, value); 170 } 171 172 /* enter wfi and stay there */ 173 while (1) 174 wfi(); 175 } 176 177 /** 178 * pm_wdt_restart_setup() - Setup warm restart interrupts. 179 * 180 * Return: Returns status, 0 on success or error+reason. 181 * 182 * This function sets up handler for SGI7 and TTC interrupts 183 * used for warm restart. 184 */ 185 static int pm_wdt_restart_setup(void) 186 { 187 int ret; 188 189 /* register IRQ handler for SGI7 */ 190 ret = request_intr_type_el3(ARM_IRQ_SEC_SGI_7, zynqmp_sgi7_irq); 191 if (ret) { 192 WARN("BL31: registering SGI7 interrupt failed\n"); 193 goto err; 194 } 195 196 ret = request_intr_type_el3(IRQ_TTC3_1, ttc_fiq_handler); 197 if (ret) 198 WARN("BL31: registering TTC3 interrupt failed\n"); 199 200 err: 201 return ret; 202 } 203 #endif 204 205 /** 206 * pm_setup() - PM service setup. 207 * 208 * Return: On success, the initialization function must return 0. 209 * Any other return value will cause the framework to ignore 210 * the service. 211 * 212 * Initialization functions for ZynqMP power management for 213 * communicaton with PMU. 214 * 215 * Called from sip_svc_setup initialization function with the 216 * rt_svc_init signature. 217 * 218 */ 219 int32_t pm_setup(void) 220 { 221 enum pm_ret_status err; 222 223 pm_ipi_init(primary_proc); 224 225 err = pm_get_api_version(&pm_ctx.api_version); 226 if (err != PM_RET_SUCCESS) { 227 ERROR("BL31: Failed to read Platform Management API version. " 228 "Return: %d\n", err); 229 return -EINVAL; 230 } 231 if (pm_ctx.api_version < PM_VERSION) { 232 ERROR("BL31: Platform Management API version error. Expected: " 233 "v%d.%d - Found: v%d.%d\n", PM_VERSION_MAJOR, 234 PM_VERSION_MINOR, pm_ctx.api_version >> 16, 235 pm_ctx.api_version & 0xFFFFU); 236 return -EINVAL; 237 } 238 239 int32_t status = 0, ret = 0; 240 #if ZYNQMP_WDT_RESTART 241 status = pm_wdt_restart_setup(); 242 if (status) 243 WARN("BL31: warm-restart setup failed\n"); 244 #endif 245 246 if (status >= 0) { 247 INFO("BL31: PM Service Init Complete: API v%d.%d\n", 248 PM_VERSION_MAJOR, PM_VERSION_MINOR); 249 ret = 0; 250 } else { 251 INFO("BL31: PM Service Init Failed, Error Code %d!\n", status); 252 ret = status; 253 } 254 255 pm_up = !status; 256 257 return ret; 258 } 259 260 /** 261 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2. 262 * @smc_fid: Function Identifier. 263 * @x1: Arguments. 264 * @x2: Arguments. 265 * @x3: Arguments. 266 * @x4: Arguments. 267 * @cookie: Unused. 268 * @handle: Pointer to caller's context structure. 269 * @flags: SECURE_FLAG or NON_SECURE_FLAG. 270 * 271 * Determines that smc_fid is valid and supported PM SMC Function ID from the 272 * list of pm_api_ids, otherwise completes the request with 273 * the unknown SMC Function ID. 274 * 275 * The SMC calls for PM service are forwarded from SIP Service SMC handler 276 * function with rt_svc_handle signature. 277 * 278 * Return: Unused. 279 * 280 */ 281 uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, 282 uint64_t x4, const void *cookie, void *handle, uint64_t flags) 283 { 284 (void)x4; 285 (void)cookie; 286 (void)flags; 287 enum pm_ret_status ret; 288 uint32_t payload[PAYLOAD_ARG_CNT]; 289 290 uint32_t pm_arg[5]; 291 uint32_t result[RET_PAYLOAD_ARG_CNT] = {0}; 292 uint32_t api_id; 293 294 /* Handle case where PM wasn't initialized properly */ 295 if (pm_up == 0) 296 SMC_RET1(handle, SMC_UNK); 297 298 pm_arg[0] = (uint32_t)x1; 299 pm_arg[1] = (uint32_t)(x1 >> 32); 300 pm_arg[2] = (uint32_t)x2; 301 pm_arg[3] = (uint32_t)(x2 >> 32); 302 pm_arg[4] = (uint32_t)x3; 303 304 api_id = smc_fid & FUNCID_NUM_MASK; 305 306 switch (api_id) { 307 /* PM API Functions */ 308 case PM_SELF_SUSPEND: 309 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2], 310 pm_arg[3]); 311 SMC_RET1(handle, (uint64_t)ret); 312 313 case PM_REQ_SUSPEND: 314 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2], 315 pm_arg[3]); 316 SMC_RET1(handle, (uint64_t)ret); 317 318 case PM_REQ_WAKEUP: 319 { 320 /* Use address flag is encoded in the 1st bit of the low-word */ 321 uint32_t set_addr = pm_arg[1] & 0x1U; 322 uint64_t address = (uint64_t)pm_arg[2] << 32U; 323 324 address |= pm_arg[1] & (~0x1U); 325 ret = pm_req_wakeup(pm_arg[0], set_addr, address, 326 pm_arg[3]); 327 SMC_RET1(handle, (uint64_t)ret); 328 } 329 330 case PM_FORCE_POWERDOWN: 331 ret = pm_force_powerdown(pm_arg[0], pm_arg[1]); 332 SMC_RET1(handle, (uint64_t)ret); 333 334 case PM_ABORT_SUSPEND: 335 ret = pm_abort_suspend(pm_arg[0]); 336 SMC_RET1(handle, (uint64_t)ret); 337 338 case PM_SET_WAKEUP_SOURCE: 339 ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]); 340 SMC_RET1(handle, (uint64_t)ret); 341 342 case PM_SYSTEM_SHUTDOWN: 343 ret = pm_system_shutdown(pm_arg[0], pm_arg[1]); 344 SMC_RET1(handle, (uint64_t)ret); 345 346 case PM_REQ_NODE: 347 ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); 348 SMC_RET1(handle, (uint64_t)ret); 349 350 case PM_SET_REQUIREMENT: 351 ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2], 352 pm_arg[3]); 353 SMC_RET1(handle, (uint64_t)ret); 354 355 case PM_GET_API_VERSION: 356 if (ipi_irq_flag == 0U) { 357 /* 358 * Enable IPI IRQ 359 * assume the rich OS is OK to handle callback IRQs now. 360 * Even if we were wrong, it would not enable the IRQ in 361 * the GIC. 362 */ 363 pm_ipi_irq_enable(primary_proc); 364 ipi_irq_flag = 1U; 365 } 366 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | 367 ((uint64_t)pm_ctx.api_version << 32)); 368 case PM_FPGA_LOAD: 369 ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); 370 SMC_RET1(handle, (uint64_t)ret); 371 372 case PM_FPGA_GET_STATUS: 373 { 374 uint32_t value = 0U; 375 376 ret = pm_fpga_get_status(&value); 377 SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32))); 378 } 379 380 case PM_SECURE_RSA_AES: 381 ret = pm_secure_rsaaes(pm_arg[0], pm_arg[1], pm_arg[2], 382 pm_arg[3]); 383 SMC_RET1(handle, (uint64_t)ret); 384 385 case PM_GET_CALLBACK_DATA: 386 ret = pm_get_callbackdata(result, ARRAY_SIZE(result)); 387 if (ret != PM_RET_SUCCESS) { 388 result[0] = ret; 389 } 390 391 SMC_RET2(handle, 392 ((uint64_t)result[0] | ((uint64_t)result[1] << 32)), 393 ((uint64_t)result[2] | ((uint64_t)result[3] << 32))); 394 case PM_IOCTL: 395 { 396 uint32_t value = 0U; 397 398 ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2], 399 pm_arg[3], &value); 400 SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32))); 401 } 402 403 case PM_QUERY_DATA: 404 { 405 uint32_t data[4] = { 0 }; 406 407 pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2], 408 pm_arg[3], data); 409 SMC_RET2(handle, ((uint64_t)data[0] | ((uint64_t)data[1] << 32)), 410 ((uint64_t)data[2] | ((uint64_t)data[3] << 32))); 411 } 412 413 case PM_CLOCK_ENABLE: 414 ret = pm_clock_enable(pm_arg[0]); 415 SMC_RET1(handle, (uint64_t)ret); 416 417 case PM_CLOCK_DISABLE: 418 ret = pm_clock_disable(pm_arg[0]); 419 SMC_RET1(handle, (uint64_t)ret); 420 421 case PM_CLOCK_GETSTATE: 422 { 423 uint32_t value = 0U; 424 425 ret = pm_clock_getstate(pm_arg[0], &value); 426 SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32))); 427 } 428 429 case PM_CLOCK_SETDIVIDER: 430 ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]); 431 SMC_RET1(handle, (uint64_t)ret); 432 433 case PM_CLOCK_GETDIVIDER: 434 { 435 uint32_t value = 0U; 436 437 ret = pm_clock_getdivider(pm_arg[0], &value); 438 SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32))); 439 } 440 441 case PM_CLOCK_SETPARENT: 442 ret = pm_clock_setparent(pm_arg[0], pm_arg[1]); 443 SMC_RET1(handle, (uint64_t)ret); 444 445 case PM_CLOCK_GETPARENT: 446 { 447 uint32_t value = 0U; 448 449 ret = pm_clock_getparent(pm_arg[0], &value); 450 SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U))); 451 } 452 453 case PM_GET_TRUSTZONE_VERSION: 454 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | 455 ((uint64_t)ZYNQMP_TZ_VERSION << 32U)); 456 457 case PM_SET_SUSPEND_MODE: 458 ret = pm_set_suspend_mode(pm_arg[0]); 459 SMC_RET1(handle, (uint64_t)ret); 460 461 case PM_SECURE_SHA: 462 ret = pm_sha_hash(pm_arg[0], pm_arg[1], pm_arg[2], 463 pm_arg[3]); 464 SMC_RET1(handle, (uint64_t)ret); 465 466 case PM_SECURE_RSA: 467 ret = pm_rsa_core(pm_arg[0], pm_arg[1], pm_arg[2], 468 pm_arg[3]); 469 SMC_RET1(handle, (uint64_t)ret); 470 471 case PM_SECURE_IMAGE: 472 { 473 ret = pm_secure_image(pm_arg[0], pm_arg[1], pm_arg[2], 474 pm_arg[3], &result[0]); 475 SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)result[0] << 32U)), 476 result[1]); 477 } 478 479 case PM_FPGA_READ: 480 { 481 uint32_t value = 0U; 482 483 ret = pm_fpga_read(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3], 484 &value); 485 SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U))); 486 } 487 488 case PM_SECURE_AES: 489 { 490 uint32_t value = 0U; 491 492 ret = pm_aes_engine(pm_arg[0], pm_arg[1], &value); 493 SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U))); 494 } 495 496 case PM_PLL_SET_PARAMETER: 497 ret = pm_pll_set_parameter(pm_arg[0], pm_arg[1], pm_arg[2]); 498 SMC_RET1(handle, (uint64_t)ret); 499 500 case PM_PLL_GET_PARAMETER: 501 { 502 uint32_t value = 0U; 503 504 ret = pm_pll_get_parameter(pm_arg[0], pm_arg[1], &value); 505 SMC_RET1(handle, ((uint64_t)ret | ((uint64_t)value << 32U))); 506 } 507 508 case PM_PLL_SET_MODE: 509 ret = pm_pll_set_mode(pm_arg[0], pm_arg[1]); 510 SMC_RET1(handle, (uint64_t)ret); 511 512 case PM_PLL_GET_MODE: 513 { 514 uint32_t mode = 0U; 515 516 ret = pm_pll_get_mode(pm_arg[0], &mode); 517 SMC_RET1(handle, ((uint64_t)ret | ((uint64_t)mode << 32U))); 518 } 519 520 case PM_REGISTER_ACCESS: 521 { 522 uint32_t value = 0U; 523 524 ret = pm_register_access(pm_arg[0], pm_arg[1], pm_arg[2], 525 pm_arg[3], &value); 526 SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U))); 527 } 528 529 case PM_EFUSE_ACCESS: 530 { 531 uint32_t value = 0U; 532 533 #if defined(ZYNQMP_SECURE_EFUSES) 534 if (is_caller_non_secure(flags)) { 535 SMC_RET1(handle, 536 (((uint64_t)PM_RET_ERROR_NOT_ENABLED) << 32U) | 537 (uint64_t)PM_RET_ERROR_ACCESS); 538 } 539 #endif 540 ret = pm_efuse_access(pm_arg[0], pm_arg[1], &value); 541 SMC_RET1(handle, (uint64_t)ret | (((uint64_t)value) << 32U)); 542 } 543 544 case PM_FPGA_GET_VERSION: 545 case PM_FPGA_GET_FEATURE_LIST: 546 { 547 uint32_t ret_payload[PAYLOAD_ARG_CNT]; 548 549 PM_PACK_PAYLOAD5(payload, smc_fid & FUNCID_NUM_MASK, 550 pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]); 551 ret = pm_ipi_send_sync(primary_proc, payload, ret_payload, 3U); 552 SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)ret_payload[0] << 32U)), 553 ((uint64_t)ret_payload[1] | ((uint64_t)ret_payload[2] << 32U))); 554 } 555 556 case PM_FEATURE_CHECK: 557 { 558 uint32_t version = 0; 559 uint32_t bit_mask[2] = {0}; 560 561 ret = pm_feature_check(pm_arg[0], &version, bit_mask, 562 ARRAY_SIZE(bit_mask)); 563 SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)version << 32U)), 564 ((uint64_t)bit_mask[0] | ((uint64_t)bit_mask[1] << 32U))); 565 } 566 567 default: 568 /* Send request to the PMU */ 569 PM_PACK_PAYLOAD6(payload, api_id, pm_arg[0], pm_arg[1], 570 pm_arg[2], pm_arg[3], pm_arg[4]); 571 ret = pm_ipi_send_sync(primary_proc, payload, result, 572 RET_PAYLOAD_ARG_CNT); 573 SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)result[0] << 32U)), 574 ((uint64_t)result[1] | ((uint64_t)result[2] << 32U))); 575 } 576 } 577