1 /* 2 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved. 3 * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 /* 9 * Top-level SMC handler for Versal power management calls and 10 * IPI setup functions for communication with PMC. 11 */ 12 13 #include <errno.h> 14 #include <stdbool.h> 15 16 #include "../drivers/arm/gic/v3/gicv3_private.h" 17 18 #include <common/runtime_svc.h> 19 #include <drivers/arm/gicv3.h> 20 #include <lib/psci/psci.h> 21 #include <plat/arm/common/plat_arm.h> 22 #include <plat/common/platform.h> 23 24 #include <plat_private.h> 25 #include "pm_api_sys.h" 26 #include "pm_client.h" 27 #include "pm_ipi.h" 28 #include "pm_svc_main.h" 29 30 #define MODE 0x80000000U 31 32 #define INVALID_SGI 0xFFU 33 #define PM_INIT_SUSPEND_CB (30U) 34 #define PM_NOTIFY_CB (32U) 35 #define EVENT_CPU_PWRDWN (4U) 36 #define MBOX_SGI_SHARED_IPI (7U) 37 38 /** 39 * upper_32_bits - return bits 32-63 of a number 40 * @n: the number we're accessing 41 */ 42 #define upper_32_bits(n) ((uint32_t)((n) >> 32U)) 43 44 /** 45 * lower_32_bits - return bits 0-31 of a number 46 * @n: the number we're accessing 47 */ 48 #define lower_32_bits(n) ((uint32_t)((n) & 0xffffffffU)) 49 50 /** 51 * EXTRACT_SMC_ARGS - extracts 32-bit payloads from 64-bit SMC arguments 52 * @pm_arg: array of 32-bit payloads 53 * @x: array of 64-bit SMC arguments 54 */ 55 #define EXTRACT_ARGS(pm_arg, x) \ 56 for (uint32_t i = 0U; i < (PAYLOAD_ARG_CNT - 1U); i++) { \ 57 if ((i % 2U) != 0U) { \ 58 pm_arg[i] = lower_32_bits(x[(i / 2U) + 1U]); \ 59 } else { \ 60 pm_arg[i] = upper_32_bits(x[i / 2U]); \ 61 } \ 62 } 63 64 /* 1 sec of wait timeout for secondary core down */ 65 #define PWRDWN_WAIT_TIMEOUT (1000U) 66 67 /* pm_up = true - UP, pm_up = false - DOWN */ 68 static bool pm_up; 69 static uint32_t sgi = (uint32_t)INVALID_SGI; 70 bool pwrdwn_req_received; 71 72 static void notify_os(void) 73 { 74 plat_ic_raise_ns_sgi((int)sgi, read_mpidr_el1()); 75 } 76 77 static uint64_t cpu_pwrdwn_req_handler(uint32_t id, uint32_t flags, 78 void *handle, void *cookie) 79 { 80 (void)id; 81 (void)flags; 82 (void)handle; 83 (void)cookie; 84 uint32_t cpu_id = plat_my_core_pos(); 85 86 VERBOSE("Powering down CPU %d\n", cpu_id); 87 88 /* Deactivate CPU power down SGI */ 89 plat_ic_end_of_interrupt(CPU_PWR_DOWN_REQ_INTR); 90 91 return (uint64_t)psci_cpu_off(); 92 } 93 94 /** 95 * raise_pwr_down_interrupt() - Callback function to raise SGI. 96 * @mpidr: MPIDR for the target CPU. 97 * 98 * Raise SGI interrupt to trigger the CPU power down sequence on all the 99 * online secondary cores. 100 */ 101 static void raise_pwr_down_interrupt(u_register_t mpidr) 102 { 103 plat_ic_raise_el3_sgi((int)CPU_PWR_DOWN_REQ_INTR, mpidr); 104 } 105 106 void request_cpu_pwrdwn(void) 107 { 108 int ret; 109 110 VERBOSE("CPU power down request received\n"); 111 112 /* Send powerdown request to online secondary core(s) */ 113 ret = psci_stop_other_cores(plat_my_core_pos(), PWRDWN_WAIT_TIMEOUT, 114 raise_pwr_down_interrupt); 115 if (ret != PSCI_E_SUCCESS) { 116 ERROR("Failed to powerdown secondary core(s)\n"); 117 } 118 119 /* Clear IPI IRQ */ 120 pm_ipi_irq_clear(primary_proc); 121 122 /* Deactivate IPI IRQ */ 123 plat_ic_end_of_interrupt(PLAT_VERSAL_IPI_IRQ); 124 } 125 126 static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle, 127 void *cookie) 128 { 129 (void)flags; 130 (void)handle; 131 (void)cookie; 132 uint32_t payload[4] = {0}; 133 enum pm_ret_status ret; 134 uint32_t ipi_status, i; 135 136 VERBOSE("Received IPI FIQ from firmware\n"); 137 138 console_flush(); 139 (void)plat_ic_acknowledge_interrupt(); 140 141 /* Check status register for each IPI except PMC */ 142 for (i = IPI_ID_APU; i <= IPI_ID_5; i++) { 143 ipi_status = ipi_mb_enquire_status(IPI_ID_APU, i); 144 145 /* If any agent other than PMC has generated IPI FIQ then send SGI to mbox driver */ 146 if ((ipi_status & IPI_MB_STATUS_RECV_PENDING) != 0U) { 147 plat_ic_raise_ns_sgi((int)MBOX_SGI_SHARED_IPI, read_mpidr_el1()); 148 break; 149 } 150 } 151 152 /* If PMC has not generated interrupt then end ISR */ 153 ipi_status = ipi_mb_enquire_status(IPI_ID_APU, IPI_ID_PMC); 154 if ((ipi_status & IPI_MB_STATUS_RECV_PENDING) == 0U) { 155 plat_ic_end_of_interrupt(id); 156 goto exit_label; 157 } 158 159 /* Handle PMC case */ 160 ret = pm_get_callbackdata(payload, ARRAY_SIZE(payload), 0, 0); 161 if (ret != PM_RET_SUCCESS) { 162 payload[0] = (uint32_t)ret; 163 } 164 165 switch (payload[0]) { 166 case PM_INIT_SUSPEND_CB: 167 if (sgi != INVALID_SGI) { 168 notify_os(); 169 } 170 break; 171 case PM_NOTIFY_CB: 172 if (sgi != INVALID_SGI) { 173 if ((payload[2] == EVENT_CPU_PWRDWN) && 174 (NODECLASS(payload[1]) == (uint32_t)XPM_NODECLASS_DEVICE)) { 175 if (pwrdwn_req_received) { 176 pwrdwn_req_received = false; 177 request_cpu_pwrdwn(); 178 (void)psci_cpu_off(); 179 break; 180 } else { 181 pwrdwn_req_received = true; 182 } 183 } 184 notify_os(); 185 } else { 186 if ((payload[2] == EVENT_CPU_PWRDWN) && 187 (NODECLASS(payload[1]) == (uint32_t)XPM_NODECLASS_DEVICE)) { 188 request_cpu_pwrdwn(); 189 (void)psci_cpu_off(); 190 } 191 } 192 break; 193 case (uint32_t)PM_RET_ERROR_INVALID_CRC: 194 pm_ipi_irq_clear(primary_proc); 195 WARN("Invalid CRC in the payload\n"); 196 break; 197 198 default: 199 pm_ipi_irq_clear(primary_proc); 200 WARN("Invalid IPI payload\n"); 201 break; 202 } 203 204 /* Clear FIQ */ 205 plat_ic_end_of_interrupt(id); 206 207 exit_label: 208 return 0; 209 } 210 211 /** 212 * pm_register_sgi() - PM register the IPI interrupt. 213 * @sgi_num: SGI number to be used for communication. 214 * @reset: Reset to invalid SGI when reset=1. 215 * 216 * Return: On success, the initialization function must return 0. 217 * Any other return value will cause the framework to ignore 218 * the service. 219 * 220 * Update the SGI number to be used. 221 * 222 */ 223 int32_t pm_register_sgi(uint32_t sgi_num, uint32_t reset) 224 { 225 int32_t ret = 0; 226 227 if (reset == 1U) { 228 sgi = INVALID_SGI; 229 } else if (sgi != INVALID_SGI) { 230 ret = -EBUSY; 231 } else if (sgi_num >= GICV3_MAX_SGI_TARGETS) { 232 ret = -EINVAL; 233 } else { 234 sgi = (uint32_t)sgi_num; 235 } 236 237 return ret; 238 } 239 240 /** 241 * pm_setup() - PM service setup. 242 * 243 * Return: On success, the initialization function must return 0. 244 * Any other return value will cause the framework to ignore 245 * the service. 246 * 247 * Initialization functions for Versal power management for 248 * communicaton with PMC. 249 * 250 * Called from sip_svc_setup initialization function with the 251 * rt_svc_init signature. 252 * 253 */ 254 int32_t pm_setup(void) 255 { 256 int32_t ret = 0; 257 258 pm_ipi_init(primary_proc); 259 pm_up = true; 260 261 /* register SGI handler for CPU power down request */ 262 ret = request_intr_type_el3(CPU_PWR_DOWN_REQ_INTR, cpu_pwrdwn_req_handler); 263 if (ret != 0) { 264 WARN("BL31: registering SGI interrupt failed\n"); 265 } 266 267 /* 268 * Enable IPI IRQ 269 * assume the rich OS is OK to handle callback IRQs now. 270 * Even if we were wrong, it would not enable the IRQ in 271 * the GIC. 272 */ 273 pm_ipi_irq_enable(primary_proc); 274 275 ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler); 276 if (ret != 0) { 277 WARN("BL31: registering IPI interrupt failed\n"); 278 } 279 280 gicd_write_irouter(gicv3_driver_data->gicd_base, PLAT_VERSAL_IPI_IRQ, MODE); 281 282 /* Register for idle callback during force power down/restart */ 283 ret = (int32_t)pm_register_notifier(primary_proc->node_id, EVENT_CPU_PWRDWN, 284 0x0U, 0x1U, SECURE_FLAG); 285 if (ret != 0) { 286 WARN("BL31: registering idle callback for restart/force power down failed\n"); 287 } 288 289 return ret; 290 } 291 292 /** 293 * eemi_for_compatibility() - EEMI calls handler for deprecated calls. 294 * @api_id: identifier for the API being called. 295 * @pm_arg: pointer to the argument data for the API call. 296 * @handle: Pointer to caller's context structure. 297 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 298 * 299 * Return: If EEMI API found then, uintptr_t type address, else 0. 300 * 301 * Some EEMI API's use case needs to be changed in Linux driver, so they 302 * can take advantage of common EEMI handler in TF-A. As of now the old 303 * implementation of these APIs are required to maintain backward compatibility 304 * until their use case in linux driver changes. 305 * 306 */ 307 static uintptr_t eemi_for_compatibility(uint32_t api_id, const uint32_t *pm_arg, 308 void *handle, uint32_t security_flag) 309 { 310 enum pm_ret_status ret; 311 312 switch (api_id) { 313 314 case (uint32_t)PM_FEATURE_CHECK: 315 { 316 uint32_t result[RET_PAYLOAD_ARG_CNT] = {0U}; 317 318 ret = pm_feature_check(pm_arg[0], result, security_flag); 319 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U), 320 (uint64_t)result[1] | ((uint64_t)result[2] << 32U)); 321 } 322 323 case PM_LOAD_PDI: 324 { 325 ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2], 326 security_flag); 327 SMC_RET1(handle, (uint64_t)ret); 328 } 329 330 default: 331 return (uintptr_t)0; 332 } 333 } 334 335 /** 336 * eemi_psci_debugfs_handler() - EEMI API invoked from PSCI. 337 * @api_id: identifier for the API being called. 338 * @pm_arg: pointer to the argument data for the API call. 339 * @handle: Pointer to caller's context structure. 340 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 341 * 342 * These EEMI APIs performs CPU specific power management tasks. 343 * These EEMI APIs are invoked either from PSCI or from debugfs in kernel. 344 * These calls require CPU specific processing before sending IPI request to 345 * Platform Management Controller. For example enable/disable CPU specific 346 * interrupts. This requires separate handler for these calls and may not be 347 * handled using common eemi handler. 348 * 349 * Return: If EEMI API found then, uintptr_t type address, else 0. 350 * 351 */ 352 static uintptr_t eemi_psci_debugfs_handler(uint32_t api_id, const uint32_t *pm_arg, 353 void *handle, uint32_t security_flag) 354 { 355 enum pm_ret_status ret; 356 357 switch (api_id) { 358 359 case (uint32_t)PM_SELF_SUSPEND: 360 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2], 361 pm_arg[3], security_flag); 362 SMC_RET1(handle, (u_register_t)ret); 363 364 case (uint32_t)PM_FORCE_POWERDOWN: 365 ret = pm_force_powerdown(pm_arg[0], (uint8_t)pm_arg[1], security_flag); 366 SMC_RET1(handle, (u_register_t)ret); 367 368 case (uint32_t)PM_REQ_SUSPEND: 369 ret = pm_req_suspend(pm_arg[0], (uint8_t)pm_arg[1], pm_arg[2], 370 pm_arg[3], security_flag); 371 SMC_RET1(handle, (u_register_t)ret); 372 373 case (uint32_t)PM_ABORT_SUSPEND: 374 ret = pm_abort_suspend((enum pm_abort_reason)pm_arg[0], security_flag); 375 SMC_RET1(handle, (u_register_t)ret); 376 377 case (uint32_t)PM_SYSTEM_SHUTDOWN: 378 ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag); 379 SMC_RET1(handle, (u_register_t)ret); 380 381 default: 382 return (uintptr_t)0; 383 } 384 } 385 386 /** 387 * TF_A_specific_handler() - SMC handler for TF-A specific functionality. 388 * @api_id: identifier for the API being called. 389 * @pm_arg: pointer to the argument data for the API call. 390 * @handle: Pointer to caller's context structure. 391 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 392 * 393 * These EEMI calls performs functionality that does not require 394 * IPI transaction. The handler ends in TF-A and returns requested data to 395 * kernel from TF-A. 396 * 397 * Return: If TF-A specific API found then, uintptr_t type address, else 0 398 * 399 */ 400 static uintptr_t TF_A_specific_handler(uint32_t api_id, const uint32_t *pm_arg, 401 void *handle, uint32_t security_flag) 402 { 403 switch (api_id) { 404 405 case TF_A_FEATURE_CHECK: 406 { 407 enum pm_ret_status ret; 408 uint32_t result[PAYLOAD_ARG_CNT] = {0U}; 409 410 ret = eemi_feature_check(pm_arg[0], result); 411 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U)); 412 } 413 414 case TF_A_PM_REGISTER_SGI: 415 { 416 int32_t ret; 417 418 ret = pm_register_sgi(pm_arg[0], pm_arg[1]); 419 if (ret != 0) { 420 SMC_RET1(handle, (uint32_t)PM_RET_ERROR_ARGS); 421 } 422 423 SMC_RET1(handle, (uint32_t)PM_RET_SUCCESS); 424 } 425 426 case PM_GET_CALLBACK_DATA: 427 { 428 uint32_t result[4] = {0}; 429 enum pm_ret_status ret; 430 431 ret = pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag, 1U); 432 if (ret != PM_RET_SUCCESS) { 433 result[0] = (uint32_t)ret; 434 } 435 436 SMC_RET2(handle, 437 (uint64_t)result[0] | ((uint64_t)result[1] << 32U), 438 (uint64_t)result[2] | ((uint64_t)result[3] << 32U)); 439 } 440 441 case PM_GET_TRUSTZONE_VERSION: 442 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | 443 ((uint64_t)TZ_VERSION << 32U)); 444 445 default: 446 return (uintptr_t)0; 447 } 448 } 449 450 /** 451 * eemi_handler() - Prepare EEMI payload and perform IPI transaction. 452 * @api_id: identifier for the API being called. 453 * @pm_arg: pointer to the argument data for the API call. 454 * @handle: Pointer to caller's context structure. 455 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 456 * 457 * EEMI - Embedded Energy Management Interface is Xilinx proprietary protocol 458 * to allow communication between power management controller and different 459 * processing clusters. 460 * 461 * This handler prepares EEMI protocol payload received from kernel and performs 462 * IPI transaction. 463 * 464 * Return: If EEMI API found then, uintptr_t type address, else 0 465 * 466 */ 467 static uintptr_t eemi_handler(uint32_t api_id, const uint32_t *pm_arg, 468 void *handle, uint32_t security_flag) 469 { 470 enum pm_ret_status ret; 471 uint32_t buf[RET_PAYLOAD_ARG_CNT] = {0}; 472 473 ret = pm_handle_eemi_call(security_flag, api_id, pm_arg[0], pm_arg[1], 474 pm_arg[2], pm_arg[3], pm_arg[4], buf); 475 /* 476 * Two IOCTLs, to get clock name and pinctrl name of pm_query_data API 477 * receives 5 words of respoonse from firmware. Currently linux driver can 478 * receive only 4 words from TF-A. So, this needs to be handled separately 479 * than other eemi calls. 480 */ 481 if (api_id == (uint32_t)PM_QUERY_DATA) { 482 if (((pm_arg[0] == (uint32_t)XPM_QID_CLOCK_GET_NAME) || 483 (pm_arg[0] == (uint32_t)XPM_QID_PINCTRL_GET_FUNCTION_NAME)) && 484 (ret == PM_RET_SUCCESS)) { 485 SMC_RET2(handle, (uint64_t)buf[0] | ((uint64_t)buf[1] << 32U), 486 (uint64_t)buf[2] | ((uint64_t)buf[3] << 32U)); 487 } 488 } 489 490 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U), 491 (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U)); 492 } 493 494 /** 495 * eemi_api_handler() - Prepare EEMI payload and perform IPI transaction. 496 * @api_id: identifier for the API being called. 497 * @pm_arg: pointer to the argument data for the API call. 498 * @handle: Pointer to caller's context structure. 499 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 500 * 501 * EEMI - Embedded Energy Management Interface is AMD-Xilinx proprietary 502 * protocol to allow communication between power management controller and 503 * different processing clusters. 504 * 505 * This handler prepares EEMI protocol payload received from kernel and performs 506 * IPI transaction. 507 * 508 * Return: If EEMI API found then, uintptr_t type address, else 0 509 */ 510 static uintptr_t eemi_api_handler(uint32_t api_id, const uint32_t *pm_arg, 511 void *handle, uint32_t security_flag) 512 { 513 enum pm_ret_status ret; 514 uint32_t buf[RET_PAYLOAD_ARG_CNT] = {0U}; 515 uint32_t payload[PAYLOAD_ARG_CNT] = {0U}; 516 uint32_t module_id; 517 518 module_id = (api_id & MODULE_ID_MASK) >> 8U; 519 520 PM_PACK_PAYLOAD7(payload, module_id, security_flag, api_id, 521 pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3], 522 pm_arg[4], pm_arg[5]); 523 524 ret = pm_ipi_send_sync(primary_proc, payload, (uint32_t *)buf, 525 RET_PAYLOAD_ARG_CNT); 526 527 SMC_RET4(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U), 528 (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U), 529 (uint64_t)buf[3] | ((uint64_t)buf[4] << 32U), 530 (uint64_t)buf[5]); 531 } 532 533 /** 534 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2. 535 * @smc_fid: Function Identifier. 536 * @x1: SMC64 Arguments from kernel. 537 * @x2: SMC64 Arguments from kernel. 538 * @x3: SMC64 Arguments from kernel (upper 32-bits). 539 * @x4: Unused. 540 * @cookie: Unused. 541 * @handle: Pointer to caller's context structure. 542 * @flags: SECURE_FLAG or NON_SECURE_FLAG. 543 * 544 * Return: Unused. 545 * 546 * Determines that smc_fid is valid and supported PM SMC Function ID from the 547 * list of pm_api_ids, otherwise completes the request with 548 * the unknown SMC Function ID. 549 * 550 * The SMC calls for PM service are forwarded from SIP Service SMC handler 551 * function with rt_svc_handle signature. 552 * 553 */ 554 uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, 555 uint64_t x4, const void *cookie, void *handle, uint64_t flags) 556 { 557 (void)cookie; 558 uintptr_t ret; 559 uint32_t pm_arg[PAYLOAD_ARG_CNT] = {0}; 560 uint32_t security_flag = NON_SECURE_FLAG; 561 uint32_t api_id; 562 bool status = false, status_tmp = false; 563 const uint64_t x[4] = {x1, x2, x3, x4}; 564 565 /* Handle case where PM wasn't initialized properly */ 566 if (pm_up == false) { 567 SMC_RET1(handle, SMC_UNK); 568 } 569 570 /* 571 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as secure (0) 572 * if smc called is secure 573 * 574 * Add redundant macro call to immune the code from glitches 575 */ 576 SECURE_REDUNDANT_CALL(status, status_tmp, is_caller_secure, flags); 577 if ((status != false) && (status_tmp != false)) { 578 security_flag = SECURE_FLAG; 579 } 580 581 if ((smc_fid & FUNCID_NUM_MASK) == PASS_THROUGH_FW_CMD_ID) { 582 api_id = lower_32_bits(x[0]); 583 584 EXTRACT_ARGS(pm_arg, x); 585 586 return eemi_api_handler(api_id, pm_arg, handle, security_flag); 587 } 588 589 pm_arg[0] = (uint32_t)x1; 590 pm_arg[1] = (uint32_t)(x1 >> 32U); 591 pm_arg[2] = (uint32_t)x2; 592 pm_arg[3] = (uint32_t)(x2 >> 32U); 593 pm_arg[4] = (uint32_t)x3; 594 (void)(x4); 595 api_id = smc_fid & FUNCID_NUM_MASK; 596 597 ret = eemi_for_compatibility(api_id, pm_arg, handle, security_flag); 598 if (ret != (uintptr_t)0) { 599 return ret; 600 } 601 602 ret = eemi_psci_debugfs_handler(api_id, pm_arg, handle, 603 (uint32_t)flags); 604 if (ret != (uintptr_t)0) { 605 return ret; 606 } 607 608 ret = TF_A_specific_handler(api_id, pm_arg, handle, security_flag); 609 if (ret != (uintptr_t)0) { 610 return ret; 611 } 612 613 ret = eemi_handler(api_id, pm_arg, handle, security_flag); 614 615 return ret; 616 } 617