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_wakeup() - PM call for processor to wake up selected processor 206 * or subsystem. 207 * @target: Device ID of the processor or subsystem to wake up. 208 * @set_address: Resume address presence indicator. 209 * 1 - resume address specified, 0 - otherwise. 210 * @address: Resume address. 211 * @ack: Flag to specify whether acknowledge requested. 212 * @flag: 0 - Call from secure source. 213 * 1 - Call from non-secure source. 214 * 215 * This API function is either used to power up another APU core for SMP 216 * (by PSCI) or to power up an entirely different PU or subsystem, such 217 * as RPU0, RPU, or PL_CORE_xx. Resume address for the target PU will be 218 * automatically set by PMC. 219 * 220 * Return: Returns status, either success or error+reason. 221 * 222 */ 223 enum pm_ret_status pm_req_wakeup(uint32_t target, uint32_t set_address, 224 uintptr_t address, uint8_t ack, uint32_t flag) 225 { 226 uint32_t payload[PAYLOAD_ARG_CNT]; 227 228 /* Send request to the PMC to perform the wake of the PU */ 229 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REQ_WAKEUP, target, 230 set_address, address, ack); 231 232 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 233 } 234 235 /** 236 * pm_get_callbackdata() - Read from IPI response buffer. 237 * @data: array of PAYLOAD_ARG_CNT elements. 238 * @count: Number of values to return. 239 * @flag: 0 - Call from secure source. 240 * 1 - Call from non-secure source. 241 * @ack: 0 - Do not ack IPI after reading payload. 242 * 1 - Ack IPI after reading payload. 243 * 244 * Read value from ipi buffer response buffer. 245 * Return: Returns status, either success or error. 246 * 247 */ 248 enum pm_ret_status pm_get_callbackdata(uint32_t *data, size_t count, uint32_t flag, uint32_t ack) 249 { 250 enum pm_ret_status ret = PM_RET_SUCCESS; 251 252 /* 253 * Typecasting to void to intentionally retain the variable and avoid 254 * MISRA violation for unused parameters. This may be used in the 255 * future if callbacks to a secure target are required. 256 */ 257 (void)flag; 258 259 /* Return if interrupt is not from PMU */ 260 if (pm_ipi_irq_status(primary_proc) != 0U) { 261 262 ret = pm_ipi_buff_read_callb(data, count); 263 264 if (ack != 0U) { 265 pm_ipi_irq_clear(primary_proc); 266 } 267 } 268 269 return ret; 270 } 271 272 /** 273 * pm_force_powerdown() - PM call to request for another PU or subsystem to 274 * be powered down forcefully. 275 * @target: Device ID of the PU node to be forced powered down. 276 * @ack: Flag to specify whether acknowledge is requested 277 * @flag: 0 - Call from secure source 278 * 1 - Call from non-secure source 279 * 280 * Return: Returns status, either success or error+reason. 281 * 282 */ 283 enum pm_ret_status pm_force_powerdown(uint32_t target, uint8_t ack, 284 uint32_t flag) 285 { 286 uint32_t payload[PAYLOAD_ARG_CNT]; 287 enum pm_ret_status ret = PM_RET_SUCCESS; 288 289 /* Send request to the PMC */ 290 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_FORCE_POWERDOWN, 291 target, ack); 292 293 if (ack == (uint32_t)IPI_BLOCKING) { 294 ret = pm_ipi_send_sync(primary_proc, payload, NULL, 0); 295 } else { 296 ret = pm_ipi_send(primary_proc, payload); 297 } 298 299 return ret; 300 } 301 302 /** 303 * pm_system_shutdown() - PM call to request a system shutdown or restart. 304 * @type: Shutdown or restart? 0=shutdown, 1=restart, 2=setscope. 305 * @subtype: Scope: 0=APU-subsystem, 1=PS, 2=system. 306 * @flag: 0 - Call from secure source. 307 * 1 - Call from non-secure source. 308 * 309 * Return: Returns status, either success or error+reason. 310 * 311 */ 312 enum pm_ret_status pm_system_shutdown(uint32_t type, uint32_t subtype, 313 uint32_t flag) 314 { 315 uint32_t payload[PAYLOAD_ARG_CNT]; 316 enum pm_ret_status ret = PM_RET_SUCCESS; 317 318 if (type == XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY) { 319 /* Setting scope for subsequent PSCI reboot or shutdown */ 320 pm_shutdown_scope = subtype; 321 goto exit_label; 322 } 323 324 /* Send request to the PMC */ 325 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SYSTEM_SHUTDOWN, 326 type, subtype); 327 328 ret = pm_ipi_send_non_blocking(primary_proc, payload); 329 330 exit_label: 331 return ret; 332 } 333 334 /** 335 * pm_set_wakeup_source() - PM call to specify the wakeup source while 336 * suspended. 337 * @target: Device id of the targeted PU or subsystem 338 * @wkup_device: Device id of the wakeup peripheral 339 * @enable: Enable or disable the specified peripheral as wake source 340 * @flag: 0 - Call from secure source 341 * 1 - Call from non-secure source 342 * 343 * Return: Returns status, either success or error+reason. 344 * 345 */ 346 enum pm_ret_status pm_set_wakeup_source(uint32_t target, uint32_t wkup_device, 347 uint8_t enable, uint32_t flag) 348 { 349 uint32_t payload[PAYLOAD_ARG_CNT]; 350 351 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_SET_WAKEUP_SOURCE, 352 target, wkup_device, enable); 353 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 354 } 355 356 /** 357 * eemi_feature_check() - Returns the supported API version if supported. 358 * @api_id: API ID to check. 359 * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of 360 * words Returned supported API version 361 * 362 * Return: Returns status, either success or error+reason. 363 */ 364 enum pm_ret_status eemi_feature_check(uint32_t api_id, uint32_t *ret_payload) 365 { 366 enum pm_ret_status ret; 367 368 /* Return version of API which are implemented in TF-A only */ 369 switch (api_id) { 370 case PM_GET_CALLBACK_DATA: 371 case PM_GET_TRUSTZONE_VERSION: 372 ret_payload[0] = PM_API_VERSION_2; 373 ret = PM_RET_SUCCESS; 374 break; 375 case TF_A_PM_REGISTER_SGI: 376 case TF_A_FEATURE_CHECK: 377 case TF_A_CLEAR_PM_STATE: 378 ret_payload[0] = PM_API_BASE_VERSION; 379 ret = PM_RET_SUCCESS; 380 break; 381 default: 382 ret = PM_RET_ERROR_NO_FEATURE; 383 break; 384 } 385 386 return ret; 387 } 388 389 /** 390 * pm_feature_check() - Returns the supported API version if supported. 391 * @api_id: API ID to check. 392 * @flag: 0 - Call from secure source. 393 * 1 - Call from non-secure source. 394 * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of 395 * words Returned supported API version and bitmasks 396 * for IOCTL and QUERY ID 397 * 398 * Return: Returns status, either success or error+reason. 399 * 400 */ 401 enum pm_ret_status pm_feature_check(uint32_t api_id, uint32_t *ret_payload, 402 uint32_t flag) 403 { 404 uint32_t payload[PAYLOAD_ARG_CNT]; 405 uint32_t module_id; 406 enum pm_ret_status ret; 407 408 /* Return version of API which are implemented in TF-A only */ 409 switch (api_id) { 410 case PM_GET_CALLBACK_DATA: 411 case PM_GET_TRUSTZONE_VERSION: 412 ret_payload[0] = PM_API_VERSION_2; 413 ret = PM_RET_SUCCESS; 414 break; 415 case TF_A_PM_REGISTER_SGI: 416 ret_payload[0] = PM_API_BASE_VERSION; 417 ret = PM_RET_SUCCESS; 418 break; 419 default: 420 module_id = (api_id & MODULE_ID_MASK) >> 8U; 421 422 /* 423 * feature check should be done only for LIBPM module 424 * If module_id is 0, then we consider it LIBPM module as default id 425 */ 426 if ((module_id > 0U) && (module_id != LIBPM_MODULE_ID)) { 427 ret = PM_RET_SUCCESS; 428 break; 429 } 430 431 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, 432 PM_FEATURE_CHECK, api_id); 433 ret = pm_ipi_send_sync(primary_proc, payload, ret_payload, RET_PAYLOAD_ARG_CNT); 434 435 break; 436 } 437 438 return ret; 439 } 440 441 /** 442 * pm_load_pdi() - Load the PDI. This function provides support to load 443 * PDI from linux. 444 * 445 * @src: Source device of pdi(DDR, OCM, SD etc). 446 * @address_low: lower 32-bit Linear memory space address. 447 * @address_high: higher 32-bit Linear memory space address. 448 * @flag: 0 - Call from secure source. 449 * 1 - Call from non-secure source. 450 * 451 * Return: Returns status, either success or error+reason. 452 * 453 */ 454 enum pm_ret_status pm_load_pdi(uint32_t src, uint32_t address_low, 455 uint32_t address_high, uint32_t flag) 456 { 457 uint32_t payload[PAYLOAD_ARG_CNT]; 458 459 /* Send request to the PMU */ 460 PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, flag, PM_LOAD_PDI, src, 461 address_high, address_low); 462 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 463 } 464 465 /** 466 * pm_register_notifier() - PM call to register a subsystem to be notified 467 * about the device event. 468 * @device_id: Device ID for the Node to which the event is related. 469 * @event: Event in question. 470 * @wake: Wake subsystem upon capturing the event if value 1. 471 * @enable: Enable the registration for value 1, disable for value 0. 472 * @flag: 0 - Call from secure source. 473 * 1 - Call from non-secure source. 474 * 475 * Return: Returns status, either success or error+reason. 476 * 477 */ 478 enum pm_ret_status pm_register_notifier(uint32_t device_id, uint32_t event, 479 uint32_t wake, uint32_t enable, 480 uint32_t flag) 481 { 482 uint32_t payload[PAYLOAD_ARG_CNT]; 483 484 /* Send request to the PMC */ 485 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REGISTER_NOTIFIER, 486 device_id, event, wake, enable); 487 488 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 489 } 490 491 /** 492 * pm_get_chipid() - Read silicon ID registers. 493 * @value: Buffer for two 32bit words. 494 * 495 * Return: Returns status, either success or error+reason and, 496 * optionally, @value. 497 */ 498 enum pm_ret_status pm_get_chipid(uint32_t *value) 499 { 500 uint32_t payload[PAYLOAD_ARG_CNT]; 501 502 PM_PACK_PAYLOAD1(payload, LIBPM_MODULE_ID, SECURE_FLAG, PM_GET_CHIPID); 503 504 return pm_ipi_send_sync(primary_proc, payload, value, 2); 505 } 506