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 * Versal system level PM-API functions and communication with PMC via 10 * IPI interrupts 11 */ 12 13 #include <common/ep_info.h> 14 #include <common/debug.h> 15 #include <drivers/arm/gic_common.h> 16 #include <lib/mmio.h> 17 #include <lib/utils.h> 18 #include <plat/common/platform.h> 19 #include <platform_def.h> 20 #include <pm_api_sys.h> 21 #include <pm_client.h> 22 #include <pm_common.h> 23 #include <pm_defs.h> 24 #include <pm_ipi.h> 25 #include "pm_svc_main.h" 26 27 #define NUM_GICD_ISENABLER ((IRQ_MAX >> 5U) + 1U) 28 29 /* default shutdown/reboot scope is system(2) */ 30 static uint32_t pm_shutdown_scope = XPM_SHUTDOWN_SUBTYPE_RST_SYSTEM; 31 32 /** 33 * pm_get_shutdown_scope() - Get the currently set shutdown scope. 34 * 35 * Return: Shutdown scope value. 36 * 37 */ 38 uint32_t pm_get_shutdown_scope(void) 39 { 40 return pm_shutdown_scope; 41 } 42 43 /* PM API functions */ 44 45 /** 46 * pm_client_set_wakeup_sources - Set all devices with enabled interrupts as 47 * wake sources in the XilPM. 48 * @node_id: Node id of processor. 49 * @flag: 0 - Call from secure source. 50 * 1 - Call from non-secure source. 51 * 52 */ 53 void pm_client_set_wakeup_sources(uint32_t node_id, uint32_t flag) 54 { 55 uint32_t reg_num, device_id; 56 uint8_t pm_wakeup_nodes_set[XPM_NODEIDX_DEV_MAX] = {0U}; 57 uint32_t isenabler1 = PLAT_ARM_GICD_BASE + GICD_ISENABLER + 4U; 58 59 zeromem(&pm_wakeup_nodes_set, (u_register_t)sizeof(pm_wakeup_nodes_set)); 60 61 for (reg_num = 0U; reg_num < NUM_GICD_ISENABLER; reg_num++) { 62 uint32_t base_irq = reg_num << ISENABLER_SHIFT; 63 uint32_t reg = mmio_read_32((uint64_t)(isenabler1 + (reg_num << 2))); 64 65 if (reg == 0U) { 66 continue; 67 } 68 69 while (reg != 0U) { 70 enum pm_device_node_idx node_idx; 71 uint32_t idx, irq, lowest_set = reg & (-reg); 72 enum pm_ret_status ret; 73 74 idx = (uint32_t)__builtin_ctz(lowest_set); 75 irq = base_irq + idx; 76 77 if (irq > IRQ_MAX) { 78 break; 79 } 80 81 node_idx = irq_to_pm_node_idx(irq); 82 reg &= ~lowest_set; 83 84 if (node_idx > XPM_NODEIDX_DEV_MIN) { 85 if (pm_wakeup_nodes_set[node_idx] == 0U) { 86 /* Get device ID from node index */ 87 device_id = PERIPH_DEVID((uint32_t)node_idx); 88 ret = pm_set_wakeup_source(node_id, 89 device_id, 1U, 90 flag); 91 pm_wakeup_nodes_set[node_idx] = (ret == PM_RET_SUCCESS) ? 92 1U : 0U; 93 } 94 } 95 } 96 } 97 } 98 99 /** 100 * pm_handle_eemi_call() - PM call for processor to send eemi payload. 101 * @flag: 0 - Call from secure source. 102 * 1 - Call from non-secure source. 103 * @x0: Arguments received per SMC64 standard. 104 * @x1: Arguments received per SMC64 standard. 105 * @x2: Arguments received per SMC64 standard. 106 * @x3: Arguments received per SMC64 standard. 107 * @x4: Arguments received per SMC64 standard. 108 * @x5: Arguments received per SMC64 standard. 109 * @result: Payload received from firmware. 110 * 111 * Return: PM_RET_SUCCESS on success or error code. 112 * 113 */ 114 enum pm_ret_status pm_handle_eemi_call(uint32_t flag, uint32_t x0, uint32_t x1, 115 uint32_t x2, uint32_t x3, uint32_t x4, 116 uint32_t x5, uint32_t *result) 117 { 118 uint32_t payload[PAYLOAD_ARG_CNT] = {0}; 119 uint32_t module_id; 120 121 module_id = (x0 & MODULE_ID_MASK) >> 8U; 122 123 //default module id is for LIBPM 124 if (module_id == 0U) { 125 module_id = LIBPM_MODULE_ID; 126 } 127 128 PM_PACK_PAYLOAD6(payload, module_id, flag, x0, x1, x2, x3, x4, x5); 129 return pm_ipi_send_sync(primary_proc, payload, result, RET_PAYLOAD_ARG_CNT); 130 } 131 132 /** 133 * pm_self_suspend() - PM call for processor to suspend itself 134 * @nid: Node id of the processor or subsystem. 135 * @latency: Requested maximum wakeup latency (not supported). 136 * @state: Requested state. 137 * @address: Resume address. 138 * @flag: 0 - Call from secure source. 139 * 1 - Call from non-secure source. 140 * 141 * This is a blocking call, it will return only once PMU has responded. 142 * On a wakeup, resume address will be automatically set by PMU. 143 * 144 * Return: Returns status, either success or error+reason. 145 * 146 */ 147 enum pm_ret_status pm_self_suspend(uint32_t nid, 148 uint32_t latency, 149 uint32_t state, 150 uintptr_t address, uint32_t flag) 151 { 152 uint32_t payload[PAYLOAD_ARG_CNT]; 153 uint32_t cpuid = plat_my_core_pos(); 154 const struct pm_proc *proc = pm_get_proc(cpuid); 155 enum pm_ret_status ret = PM_RET_ERROR_INTERNAL; 156 157 if (proc == NULL) { 158 WARN("Failed to get proc %d\n", cpuid); 159 goto exit_label; 160 } 161 162 /* 163 * Do client specific suspend operations 164 * (e.g. set powerdown request bit) 165 */ 166 pm_client_suspend(proc, state, flag); 167 168 /* Send request to the PLM */ 169 PM_PACK_PAYLOAD6(payload, LIBPM_MODULE_ID, flag, PM_SELF_SUSPEND, 170 nid, latency, state, address, (address >> 32)); 171 ret = pm_ipi_send_sync(proc, payload, NULL, 0); 172 173 exit_label: 174 return ret; 175 } 176 177 /** 178 * pm_abort_suspend() - PM call to announce that a prior suspend request 179 * is to be aborted. 180 * @reason: Reason for the abort. 181 * @flag: 0 - Call from secure source. 182 * 1 - Call from non-secure source. 183 * 184 * Calling PU expects the PMU to abort the initiated suspend procedure. 185 * This is a non-blocking call without any acknowledge. 186 * 187 * Return: Returns status, either success or error+reason. 188 * 189 */ 190 enum pm_ret_status pm_abort_suspend(enum pm_abort_reason reason, uint32_t flag) 191 { 192 uint32_t payload[PAYLOAD_ARG_CNT]; 193 194 /* 195 * Do client specific abort suspend operations 196 * (e.g. enable interrupts and clear powerdown request bit) 197 */ 198 pm_client_abort_suspend(); 199 200 /* Send request to the PLM */ 201 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_ABORT_SUSPEND, 202 reason, primary_proc->node_id); 203 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 204 } 205 206 /** 207 * pm_req_wakeup() - PM call for processor to wake up selected processor 208 * or subsystem. 209 * @target: Device ID of the processor or subsystem to wake up. 210 * @set_address: Resume address presence indicator. 211 * 1 - resume address specified, 0 - otherwise. 212 * @address: Resume address. 213 * @ack: Flag to specify whether acknowledge requested. 214 * @flag: 0 - Call from secure source. 215 * 1 - Call from non-secure source. 216 * 217 * This API function is either used to power up another APU core for SMP 218 * (by PSCI) or to power up an entirely different PU or subsystem, such 219 * as RPU0, RPU, or PL_CORE_xx. Resume address for the target PU will be 220 * automatically set by PMC. 221 * 222 * Return: Returns status, either success or error+reason. 223 * 224 */ 225 enum pm_ret_status pm_req_wakeup(uint32_t target, uint32_t set_address, 226 uintptr_t address, uint8_t ack, uint32_t flag) 227 { 228 uint32_t payload[PAYLOAD_ARG_CNT]; 229 230 /* Send request to the PMC to perform the wake of the PU */ 231 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REQ_WAKEUP, target, 232 set_address, address, ack); 233 234 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 235 } 236 237 /** 238 * pm_get_callbackdata() - Read from IPI response buffer. 239 * @data: array of PAYLOAD_ARG_CNT elements. 240 * @count: Number of values to return. 241 * @flag: 0 - Call from secure source. 242 * 1 - Call from non-secure source. 243 * @ack: 0 - Do not ack IPI after reading payload. 244 * 1 - Ack IPI after reading payload. 245 * 246 * Read value from ipi buffer response buffer. 247 * Return: Returns status, either success or error. 248 * 249 */ 250 enum pm_ret_status pm_get_callbackdata(uint32_t *data, size_t count, uint32_t flag, uint32_t ack) 251 { 252 enum pm_ret_status ret = PM_RET_SUCCESS; 253 254 /* 255 * Typecasting to void to intentionally retain the variable and avoid 256 * MISRA violation for unused parameters. This may be used in the 257 * future if callbacks to a secure target are required. 258 */ 259 (void)flag; 260 261 /* Return if interrupt is not from PMU */ 262 if (pm_ipi_irq_status(primary_proc) != 0U) { 263 264 ret = pm_ipi_buff_read_callb(data, count); 265 266 if (ack != 0U) { 267 pm_ipi_irq_clear(primary_proc); 268 } 269 } 270 271 return ret; 272 } 273 274 /** 275 * pm_force_powerdown() - PM call to request for another PU or subsystem to 276 * be powered down forcefully. 277 * @target: Device ID of the PU node to be forced powered down. 278 * @ack: Flag to specify whether acknowledge is requested 279 * @flag: 0 - Call from secure source 280 * 1 - Call from non-secure source 281 * 282 * Return: Returns status, either success or error+reason. 283 * 284 */ 285 enum pm_ret_status pm_force_powerdown(uint32_t target, uint8_t ack, 286 uint32_t flag) 287 { 288 uint32_t payload[PAYLOAD_ARG_CNT]; 289 enum pm_ret_status ret = PM_RET_SUCCESS; 290 291 /* Send request to the PMC */ 292 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_FORCE_POWERDOWN, 293 target, ack); 294 295 if (ack == (uint32_t)IPI_BLOCKING) { 296 ret = pm_ipi_send_sync(primary_proc, payload, NULL, 0); 297 } else { 298 ret = pm_ipi_send(primary_proc, payload); 299 } 300 301 return ret; 302 } 303 304 /** 305 * pm_system_shutdown() - PM call to request a system shutdown or restart. 306 * @type: Shutdown or restart? 0=shutdown, 1=restart, 2=setscope. 307 * @subtype: Scope: 0=APU-subsystem, 1=PS, 2=system. 308 * @flag: 0 - Call from secure source. 309 * 1 - Call from non-secure source. 310 * 311 * Return: Returns status, either success or error+reason. 312 * 313 */ 314 enum pm_ret_status pm_system_shutdown(uint32_t type, uint32_t subtype, 315 uint32_t flag) 316 { 317 uint32_t payload[PAYLOAD_ARG_CNT]; 318 enum pm_ret_status ret = PM_RET_SUCCESS; 319 320 if (type == XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY) { 321 /* Setting scope for subsequent PSCI reboot or shutdown */ 322 pm_shutdown_scope = subtype; 323 goto exit_label; 324 } 325 326 /* Send request to the PMC */ 327 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SYSTEM_SHUTDOWN, 328 type, subtype); 329 330 ret = pm_ipi_send_non_blocking(primary_proc, payload); 331 332 exit_label: 333 return ret; 334 } 335 336 /** 337 * pm_set_wakeup_source() - PM call to specify the wakeup source while 338 * suspended. 339 * @target: Device id of the targeted PU or subsystem 340 * @wkup_device: Device id of the wakeup peripheral 341 * @enable: Enable or disable the specified peripheral as wake source 342 * @flag: 0 - Call from secure source 343 * 1 - Call from non-secure source 344 * 345 * Return: Returns status, either success or error+reason. 346 * 347 */ 348 enum pm_ret_status pm_set_wakeup_source(uint32_t target, uint32_t wkup_device, 349 uint8_t enable, uint32_t flag) 350 { 351 uint32_t payload[PAYLOAD_ARG_CNT]; 352 353 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_SET_WAKEUP_SOURCE, 354 target, wkup_device, enable); 355 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 356 } 357 358 /** 359 * tfa_api_feature_check() - Returns the supported TF-A API version if supported. 360 * @api_id: TF-A specific API ID to check. 361 * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of 362 * words Returned supported API version 363 * 364 * Return: Returns status, either success or error+reason. 365 */ 366 enum pm_ret_status tfa_api_feature_check(uint32_t api_id, uint32_t *ret_payload) 367 { 368 enum pm_ret_status ret; 369 370 /* Return version of API which are implemented in TF-A only */ 371 switch (api_id) { 372 case PM_GET_CALLBACK_DATA: 373 case PM_GET_TRUSTZONE_VERSION: 374 ret_payload[0] = PM_API_VERSION_2; 375 ret = PM_RET_SUCCESS; 376 break; 377 case TF_A_PM_REGISTER_SGI: 378 case TF_A_FEATURE_CHECK: 379 case TF_A_CLEAR_PM_STATE: 380 ret_payload[0] = PM_API_BASE_VERSION; 381 ret = PM_RET_SUCCESS; 382 break; 383 default: 384 ret = PM_RET_ERROR_NO_FEATURE; 385 break; 386 } 387 388 return ret; 389 } 390 391 /** 392 * pm_feature_check() - Returns the supported API version if supported. 393 * @api_id: API ID to check. 394 * @flag: 0 - Call from secure source. 395 * 1 - Call from non-secure source. 396 * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of 397 * words Returned supported API version and bitmasks 398 * for IOCTL and QUERY ID 399 * 400 * Return: Returns status, either success or error+reason. 401 * 402 */ 403 enum pm_ret_status pm_feature_check(uint32_t api_id, uint32_t *ret_payload, 404 uint32_t flag) 405 { 406 uint32_t payload[PAYLOAD_ARG_CNT]; 407 uint32_t module_id; 408 enum pm_ret_status ret; 409 410 WARN("pm_feature_check() will be deprecated in 2027.1 release." 411 " Use tfa_api_feature_check() for TF-A specific APIs.\n"); 412 413 /* Return version of API which are implemented in TF-A only */ 414 switch (api_id) { 415 case PM_GET_CALLBACK_DATA: 416 case PM_GET_TRUSTZONE_VERSION: 417 ret_payload[0] = PM_API_VERSION_2; 418 ret = PM_RET_SUCCESS; 419 break; 420 case TF_A_PM_REGISTER_SGI: 421 ret_payload[0] = PM_API_BASE_VERSION; 422 ret = PM_RET_SUCCESS; 423 break; 424 default: 425 module_id = (api_id & MODULE_ID_MASK) >> 8U; 426 427 /* 428 * feature check should be done only for LIBPM module 429 * If module_id is 0, then we consider it LIBPM module as default id 430 */ 431 if ((module_id > 0U) && (module_id != LIBPM_MODULE_ID)) { 432 ret = PM_RET_SUCCESS; 433 break; 434 } 435 436 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, 437 PM_FEATURE_CHECK, api_id); 438 ret = pm_ipi_send_sync(primary_proc, payload, ret_payload, RET_PAYLOAD_ARG_CNT); 439 440 break; 441 } 442 443 return ret; 444 } 445 446 /** 447 * pm_load_pdi() - Load the PDI. This function provides support to load 448 * PDI from linux. 449 * 450 * @src: Source device of pdi(DDR, OCM, SD etc). 451 * @address_low: lower 32-bit Linear memory space address. 452 * @address_high: higher 32-bit Linear memory space address. 453 * @flag: 0 - Call from secure source. 454 * 1 - Call from non-secure source. 455 * 456 * Return: Returns status, either success or error+reason. 457 * 458 */ 459 enum pm_ret_status pm_load_pdi(uint32_t src, uint32_t address_low, 460 uint32_t address_high, uint32_t flag) 461 { 462 uint32_t payload[PAYLOAD_ARG_CNT]; 463 464 /* Send request to the PMU */ 465 PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, flag, PM_LOAD_PDI, src, 466 address_high, address_low); 467 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 468 } 469 470 /** 471 * pm_register_notifier() - PM call to register a subsystem to be notified 472 * about the device event. 473 * @device_id: Device ID for the Node to which the event is related. 474 * @event: Event in question. 475 * @wake: Wake subsystem upon capturing the event if value 1. 476 * @enable: Enable the registration for value 1, disable for value 0. 477 * @flag: 0 - Call from secure source. 478 * 1 - Call from non-secure source. 479 * 480 * Return: Returns status, either success or error+reason. 481 * 482 */ 483 enum pm_ret_status pm_register_notifier(uint32_t device_id, uint32_t event, 484 uint32_t wake, uint32_t enable, 485 uint32_t flag) 486 { 487 uint32_t payload[PAYLOAD_ARG_CNT]; 488 489 /* Send request to the PMC */ 490 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REGISTER_NOTIFIER, 491 device_id, event, wake, enable); 492 493 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 494 } 495 496 /** 497 * pm_get_chipid() - Read silicon ID registers. 498 * @value: Buffer for two 32bit words. 499 * 500 * Return: Returns status, either success or error+reason and, 501 * optionally, @value. 502 */ 503 enum pm_ret_status pm_get_chipid(uint32_t *value) 504 { 505 uint32_t payload[PAYLOAD_ARG_CNT]; 506 507 PM_PACK_PAYLOAD1(payload, LIBPM_MODULE_ID, SECURE, PM_GET_CHIPID); 508 509 return pm_ipi_send_sync(primary_proc, payload, value, 2); 510 } 511