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