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