1 /* 2 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved. 3 * Copyright (c) 2022-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 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 XSCUGIC_SGIR_EL1_INITID_SHIFT 24U 33 #define INVALID_SGI 0xFFU 34 #define PM_INIT_SUSPEND_CB (30U) 35 #define PM_NOTIFY_CB (32U) 36 #define EVENT_CPU_PWRDWN (4U) 37 /* 1 sec of wait timeout for secondary core down */ 38 #define PWRDWN_WAIT_TIMEOUT (1000U) 39 DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1, S3_0_C12_C11_6) 40 41 /* pm_up = true - UP, pm_up = false - DOWN */ 42 static bool pm_up; 43 static uint32_t sgi = (uint32_t)INVALID_SGI; 44 bool pwrdwn_req_received; 45 46 static void notify_os(void) 47 { 48 plat_ic_raise_ns_sgi(sgi, read_mpidr_el1()); 49 } 50 51 static uint64_t cpu_pwrdwn_req_handler(uint32_t id, uint32_t flags, 52 void *handle, void *cookie) 53 { 54 uint32_t cpu_id = plat_my_core_pos(); 55 56 VERBOSE("Powering down CPU %d\n", cpu_id); 57 58 /* Deactivate CPU power down SGI */ 59 plat_ic_end_of_interrupt(CPU_PWR_DOWN_REQ_INTR); 60 61 return psci_cpu_off(); 62 } 63 64 /** 65 * raise_pwr_down_interrupt() - Callback function to raise SGI. 66 * @mpidr: MPIDR for the target CPU. 67 * 68 * Raise SGI interrupt to trigger the CPU power down sequence on all the 69 * online secondary cores. 70 */ 71 static void raise_pwr_down_interrupt(u_register_t mpidr) 72 { 73 plat_ic_raise_el3_sgi(CPU_PWR_DOWN_REQ_INTR, mpidr); 74 } 75 76 void request_cpu_pwrdwn(void) 77 { 78 enum pm_ret_status ret; 79 80 VERBOSE("CPU power down request received\n"); 81 82 /* Send powerdown request to online secondary core(s) */ 83 ret = psci_stop_other_cores(PWRDWN_WAIT_TIMEOUT, raise_pwr_down_interrupt); 84 if (ret != PSCI_E_SUCCESS) { 85 ERROR("Failed to powerdown secondary core(s)\n"); 86 } 87 88 /* Clear IPI IRQ */ 89 pm_ipi_irq_clear(primary_proc); 90 91 /* Deactivate IPI IRQ */ 92 plat_ic_end_of_interrupt(PLAT_VERSAL_IPI_IRQ); 93 } 94 95 static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle, 96 void *cookie) 97 { 98 uint32_t payload[4] = {0}; 99 enum pm_ret_status ret; 100 101 VERBOSE("Received IPI FIQ from firmware\n"); 102 103 (void)plat_ic_acknowledge_interrupt(); 104 105 ret = pm_get_callbackdata(payload, ARRAY_SIZE(payload), 0, 0); 106 if (ret != PM_RET_SUCCESS) { 107 payload[0] = ret; 108 } 109 110 switch (payload[0]) { 111 case PM_INIT_SUSPEND_CB: 112 if (sgi != INVALID_SGI) { 113 notify_os(); 114 } 115 break; 116 case PM_NOTIFY_CB: 117 if (sgi != INVALID_SGI) { 118 if (payload[2] == EVENT_CPU_PWRDWN) { 119 if (pwrdwn_req_received) { 120 pwrdwn_req_received = false; 121 request_cpu_pwrdwn(); 122 (void)psci_cpu_off(); 123 break; 124 } else { 125 pwrdwn_req_received = true; 126 } 127 } 128 notify_os(); 129 } 130 break; 131 case PM_RET_ERROR_INVALID_CRC: 132 pm_ipi_irq_clear(primary_proc); 133 WARN("Invalid CRC in the payload\n"); 134 break; 135 136 default: 137 pm_ipi_irq_clear(primary_proc); 138 WARN("Invalid IPI payload\n"); 139 break; 140 } 141 142 /* Clear FIQ */ 143 plat_ic_end_of_interrupt(id); 144 145 return 0; 146 } 147 148 /** 149 * pm_register_sgi() - PM register the IPI interrupt. 150 * @sgi_num: SGI number to be used for communication. 151 * @reset: Reset to invalid SGI when reset=1. 152 * 153 * Return: On success, the initialization function must return 0. 154 * Any other return value will cause the framework to ignore 155 * the service. 156 * 157 * Update the SGI number to be used. 158 * 159 */ 160 int32_t pm_register_sgi(uint32_t sgi_num, uint32_t reset) 161 { 162 if (reset == 1U) { 163 sgi = INVALID_SGI; 164 return 0; 165 } 166 167 if (sgi != INVALID_SGI) { 168 return -EBUSY; 169 } 170 171 if (sgi_num >= GICV3_MAX_SGI_TARGETS) { 172 return -EINVAL; 173 } 174 175 sgi = (uint32_t)sgi_num; 176 return 0; 177 } 178 179 /** 180 * pm_setup() - PM service setup. 181 * 182 * Return: On success, the initialization function must return 0. 183 * Any other return value will cause the framework to ignore 184 * the service. 185 * 186 * Initialization functions for Versal power management for 187 * communicaton with PMC. 188 * 189 * Called from sip_svc_setup initialization function with the 190 * rt_svc_init signature. 191 * 192 */ 193 int32_t pm_setup(void) 194 { 195 int32_t ret = 0; 196 197 pm_ipi_init(primary_proc); 198 pm_up = true; 199 200 /* register SGI handler for CPU power down request */ 201 ret = request_intr_type_el3(CPU_PWR_DOWN_REQ_INTR, cpu_pwrdwn_req_handler); 202 if (ret != 0) { 203 WARN("BL31: registering SGI interrupt failed\n"); 204 } 205 206 /* 207 * Enable IPI IRQ 208 * assume the rich OS is OK to handle callback IRQs now. 209 * Even if we were wrong, it would not enable the IRQ in 210 * the GIC. 211 */ 212 pm_ipi_irq_enable(primary_proc); 213 214 ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler); 215 if (ret != 0) { 216 WARN("BL31: registering IPI interrupt failed\n"); 217 } 218 219 gicd_write_irouter(gicv3_driver_data->gicd_base, PLAT_VERSAL_IPI_IRQ, MODE); 220 return ret; 221 } 222 223 /** 224 * eemi_for_compatibility() - EEMI calls handler for deprecated calls. 225 * @api_id: identifier for the API being called. 226 * @pm_arg: pointer to the argument data for the API call. 227 * @handle: Pointer to caller's context structure. 228 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 229 * 230 * Return: If EEMI API found then, uintptr_t type address, else 0. 231 * 232 * Some EEMI API's use case needs to be changed in Linux driver, so they 233 * can take advantage of common EEMI handler in TF-A. As of now the old 234 * implementation of these APIs are required to maintain backward compatibility 235 * until their use case in linux driver changes. 236 * 237 */ 238 static uintptr_t eemi_for_compatibility(uint32_t api_id, uint32_t *pm_arg, 239 void *handle, uint32_t security_flag) 240 { 241 enum pm_ret_status ret; 242 243 switch (api_id) { 244 245 case (uint32_t)PM_IOCTL: 246 { 247 uint32_t value = 0U; 248 249 ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2], 250 pm_arg[3], pm_arg[4], 251 &value, security_flag); 252 if (ret == PM_RET_ERROR_NOTSUPPORTED) 253 return (uintptr_t)0; 254 255 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32U); 256 } 257 258 case (uint32_t)PM_QUERY_DATA: 259 { 260 uint32_t data[PAYLOAD_ARG_CNT] = { 0 }; 261 262 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2], 263 pm_arg[3], data, security_flag); 264 265 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)data[0] << 32U), 266 (uint64_t)data[1] | ((uint64_t)data[2] << 32U)); 267 } 268 269 case (uint32_t)PM_FEATURE_CHECK: 270 { 271 uint32_t result[PAYLOAD_ARG_CNT] = {0U}; 272 273 ret = pm_feature_check(pm_arg[0], result, security_flag); 274 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U), 275 (uint64_t)result[1] | ((uint64_t)result[2] << 32U)); 276 } 277 278 case PM_LOAD_PDI: 279 { 280 ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2], 281 security_flag); 282 SMC_RET1(handle, (uint64_t)ret); 283 } 284 285 default: 286 return (uintptr_t)0; 287 } 288 } 289 290 /** 291 * eemi_psci_debugfs_handler() - EEMI API invoked from PSCI. 292 * @api_id: identifier for the API being called. 293 * @pm_arg: pointer to the argument data for the API call. 294 * @handle: Pointer to caller's context structure. 295 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 296 * 297 * These EEMI APIs performs CPU specific power management tasks. 298 * These EEMI APIs are invoked either from PSCI or from debugfs in kernel. 299 * These calls require CPU specific processing before sending IPI request to 300 * Platform Management Controller. For example enable/disable CPU specific 301 * interrupts. This requires separate handler for these calls and may not be 302 * handled using common eemi handler. 303 * 304 * Return: If EEMI API found then, uintptr_t type address, else 0. 305 * 306 */ 307 static uintptr_t eemi_psci_debugfs_handler(uint32_t api_id, 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_SELF_SUSPEND: 315 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2], 316 pm_arg[3], security_flag); 317 SMC_RET1(handle, (u_register_t)ret); 318 319 case (uint32_t)PM_FORCE_POWERDOWN: 320 ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag); 321 SMC_RET1(handle, (u_register_t)ret); 322 323 case (uint32_t)PM_REQ_SUSPEND: 324 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2], 325 pm_arg[3], security_flag); 326 SMC_RET1(handle, (u_register_t)ret); 327 328 case (uint32_t)PM_ABORT_SUSPEND: 329 ret = pm_abort_suspend(pm_arg[0], security_flag); 330 SMC_RET1(handle, (u_register_t)ret); 331 332 case (uint32_t)PM_SYSTEM_SHUTDOWN: 333 ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag); 334 SMC_RET1(handle, (u_register_t)ret); 335 336 default: 337 return (uintptr_t)0; 338 } 339 } 340 341 /** 342 * TF_A_specific_handler() - SMC handler for TF-A specific functionality. 343 * @api_id: identifier for the API being called. 344 * @pm_arg: pointer to the argument data for the API call. 345 * @handle: Pointer to caller's context structure. 346 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 347 * 348 * These EEMI calls performs functionality that does not require 349 * IPI transaction. The handler ends in TF-A and returns requested data to 350 * kernel from TF-A. 351 * 352 * Return: If TF-A specific API found then, uintptr_t type address, else 0 353 * 354 */ 355 static uintptr_t TF_A_specific_handler(uint32_t api_id, uint32_t *pm_arg, 356 void *handle, uint32_t security_flag) 357 { 358 switch (api_id) { 359 360 case TF_A_PM_REGISTER_SGI: 361 { 362 int32_t ret; 363 364 ret = pm_register_sgi(pm_arg[0], pm_arg[1]); 365 if (ret != 0) { 366 SMC_RET1(handle, (uint32_t)PM_RET_ERROR_ARGS); 367 } 368 369 SMC_RET1(handle, (uint32_t)PM_RET_SUCCESS); 370 } 371 372 case PM_GET_CALLBACK_DATA: 373 { 374 uint32_t result[4] = {0}; 375 enum pm_ret_status ret; 376 377 ret = pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag, 1U); 378 if (ret != 0) { 379 result[0] = ret; 380 } 381 382 SMC_RET2(handle, 383 (uint64_t)result[0] | ((uint64_t)result[1] << 32U), 384 (uint64_t)result[2] | ((uint64_t)result[3] << 32U)); 385 } 386 387 case PM_GET_TRUSTZONE_VERSION: 388 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS | 389 ((uint64_t)TZ_VERSION << 32U)); 390 391 default: 392 return (uintptr_t)0; 393 } 394 } 395 396 /** 397 * eemi_handler() - Prepare EEMI payload and perform IPI transaction. 398 * @api_id: identifier for the API being called. 399 * @pm_arg: pointer to the argument data for the API call. 400 * @handle: Pointer to caller's context structure. 401 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG. 402 * 403 * EEMI - Embedded Energy Management Interface is Xilinx proprietary protocol 404 * to allow communication between power management controller and different 405 * processing clusters. 406 * 407 * This handler prepares EEMI protocol payload received from kernel and performs 408 * IPI transaction. 409 * 410 * Return: If EEMI API found then, uintptr_t type address, else 0 411 * 412 */ 413 static uintptr_t eemi_handler(uint32_t api_id, uint32_t *pm_arg, 414 void *handle, uint32_t security_flag) 415 { 416 enum pm_ret_status ret; 417 uint32_t buf[PAYLOAD_ARG_CNT] = {0}; 418 419 ret = pm_handle_eemi_call(security_flag, api_id, pm_arg[0], pm_arg[1], 420 pm_arg[2], pm_arg[3], pm_arg[4], 421 (uint64_t *)buf); 422 /* 423 * Two IOCTLs, to get clock name and pinctrl name of pm_query_data API 424 * receives 5 words of respoonse from firmware. Currently linux driver can 425 * receive only 4 words from TF-A. So, this needs to be handled separately 426 * than other eemi calls. 427 */ 428 if (api_id == (uint32_t)PM_QUERY_DATA) { 429 if ((pm_arg[0] == XPM_QID_CLOCK_GET_NAME || 430 pm_arg[0] == XPM_QID_PINCTRL_GET_FUNCTION_NAME) && 431 ret == PM_RET_SUCCESS) { 432 SMC_RET2(handle, (uint64_t)buf[0] | ((uint64_t)buf[1] << 32U), 433 (uint64_t)buf[2] | ((uint64_t)buf[3] << 32U)); 434 } 435 } 436 437 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U), 438 (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U)); 439 } 440 441 /** 442 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2. 443 * @smc_fid: Function Identifier. 444 * @x1: SMC64 Arguments from kernel. 445 * @x2: SMC64 Arguments from kernel. 446 * @x3: SMC64 Arguments from kernel (upper 32-bits). 447 * @x4: Unused. 448 * @cookie: Unused. 449 * @handle: Pointer to caller's context structure. 450 * @flags: SECURE_FLAG or NON_SECURE_FLAG. 451 * 452 * Return: Unused. 453 * 454 * Determines that smc_fid is valid and supported PM SMC Function ID from the 455 * list of pm_api_ids, otherwise completes the request with 456 * the unknown SMC Function ID. 457 * 458 * The SMC calls for PM service are forwarded from SIP Service SMC handler 459 * function with rt_svc_handle signature. 460 * 461 */ 462 uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, 463 uint64_t x4, const void *cookie, void *handle, uint64_t flags) 464 { 465 uintptr_t ret; 466 uint32_t pm_arg[PAYLOAD_ARG_CNT] = {0}; 467 uint32_t security_flag = NON_SECURE_FLAG; 468 uint32_t api_id; 469 bool status = false, status_tmp = false; 470 471 /* Handle case where PM wasn't initialized properly */ 472 if (pm_up == false) { 473 SMC_RET1(handle, SMC_UNK); 474 } 475 476 /* 477 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as secure (0) 478 * if smc called is secure 479 * 480 * Add redundant macro call to immune the code from glitches 481 */ 482 SECURE_REDUNDANT_CALL(status, status_tmp, is_caller_secure, flags); 483 if ((status != false) && (status_tmp != false)) { 484 security_flag = SECURE_FLAG; 485 } 486 487 pm_arg[0] = (uint32_t)x1; 488 pm_arg[1] = (uint32_t)(x1 >> 32U); 489 pm_arg[2] = (uint32_t)x2; 490 pm_arg[3] = (uint32_t)(x2 >> 32U); 491 pm_arg[4] = (uint32_t)x3; 492 (void)(x4); 493 api_id = smc_fid & FUNCID_NUM_MASK; 494 495 ret = eemi_for_compatibility(api_id, pm_arg, handle, security_flag); 496 if (ret != (uintptr_t)0) { 497 return ret; 498 } 499 500 ret = eemi_psci_debugfs_handler(api_id, pm_arg, handle, flags); 501 if (ret != (uintptr_t)0) { 502 return ret; 503 } 504 505 ret = TF_A_specific_handler(api_id, pm_arg, handle, security_flag); 506 if (ret != (uintptr_t)0) { 507 return ret; 508 } 509 510 ret = eemi_handler(api_id, pm_arg, handle, security_flag); 511 512 return ret; 513 } 514