1 /* 2 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved. 3 * Copyright (c) 2022-2024, 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 * 48 */ 49 void pm_client_set_wakeup_sources(uint32_t node_id) 50 { 51 uint32_t reg_num, device_id; 52 uint8_t pm_wakeup_nodes_set[XPM_NODEIDX_DEV_MAX] = {0U}; 53 uint32_t isenabler1 = PLAT_ARM_GICD_BASE + GICD_ISENABLER + 4U; 54 55 zeromem(&pm_wakeup_nodes_set, (u_register_t)sizeof(pm_wakeup_nodes_set)); 56 57 for (reg_num = 0U; reg_num < NUM_GICD_ISENABLER; reg_num++) { 58 uint32_t base_irq = reg_num << ISENABLER_SHIFT; 59 isenabler1 += (reg_num << 2); 60 uint32_t reg = mmio_read_32((uint64_t)isenabler1); 61 62 if (reg == 0U) { 63 continue; 64 } 65 66 while (reg != 0U) { 67 enum pm_device_node_idx node_idx; 68 uint32_t idx, irq, lowest_set = reg & (-reg); 69 enum pm_ret_status ret; 70 71 idx = (uint32_t)__builtin_ctz(lowest_set); 72 irq = base_irq + idx; 73 74 if (irq > IRQ_MAX) { 75 break; 76 } 77 78 node_idx = irq_to_pm_node_idx(irq); 79 reg &= ~lowest_set; 80 81 if (node_idx > XPM_NODEIDX_DEV_MIN) { 82 if (pm_wakeup_nodes_set[node_idx] == 0U) { 83 /* Get device ID from node index */ 84 device_id = PERIPH_DEVID((uint32_t)node_idx); 85 ret = pm_set_wakeup_source(node_id, 86 device_id, 1U, 87 SECURE_FLAG); 88 pm_wakeup_nodes_set[node_idx] = (ret == PM_RET_SUCCESS) ? 89 1U : 0U; 90 } 91 } 92 } 93 } 94 } 95 96 /** 97 * pm_handle_eemi_call() - PM call for processor to send eemi payload. 98 * @flag: 0 - Call from secure source. 99 * 1 - Call from non-secure source. 100 * @x0: Arguments received per SMC64 standard. 101 * @x1: Arguments received per SMC64 standard. 102 * @x2: Arguments received per SMC64 standard. 103 * @x3: Arguments received per SMC64 standard. 104 * @x4: Arguments received per SMC64 standard. 105 * @x5: Arguments received per SMC64 standard. 106 * @result: Payload received from firmware. 107 * 108 * Return: PM_RET_SUCCESS on success or error code. 109 * 110 */ 111 enum pm_ret_status pm_handle_eemi_call(uint32_t flag, uint32_t x0, uint32_t x1, 112 uint32_t x2, uint32_t x3, uint32_t x4, 113 uint32_t x5, uint64_t *result) 114 { 115 uint32_t payload[PAYLOAD_ARG_CNT] = {0}; 116 uint32_t module_id; 117 118 module_id = (x0 & MODULE_ID_MASK) >> 8U; 119 120 //default module id is for LIBPM 121 if (module_id == 0U) { 122 module_id = LIBPM_MODULE_ID; 123 } 124 125 PM_PACK_PAYLOAD6(payload, module_id, flag, x0, x1, x2, x3, x4, x5); 126 return pm_ipi_send_sync(primary_proc, payload, (uint32_t *)result, RET_PAYLOAD_ARG_CNT); 127 } 128 129 /** 130 * pm_self_suspend() - PM call for processor to suspend itself 131 * @nid: Node id of the processor or subsystem. 132 * @latency: Requested maximum wakeup latency (not supported). 133 * @state: Requested state. 134 * @address: Resume address. 135 * @flag: 0 - Call from secure source. 136 * 1 - Call from non-secure source. 137 * 138 * This is a blocking call, it will return only once PMU has responded. 139 * On a wakeup, resume address will be automatically set by PMU. 140 * 141 * Return: Returns status, either success or error+reason. 142 * 143 */ 144 enum pm_ret_status pm_self_suspend(uint32_t nid, 145 uint32_t latency, 146 uint32_t state, 147 uintptr_t address, uint32_t flag) 148 { 149 uint32_t payload[PAYLOAD_ARG_CNT]; 150 uint32_t cpuid = plat_my_core_pos(); 151 const struct pm_proc *proc = pm_get_proc(cpuid); 152 enum pm_ret_status ret = PM_RET_ERROR_INTERNAL; 153 154 if (proc == NULL) { 155 WARN("Failed to get proc %d\n", cpuid); 156 goto exit_label; 157 } 158 159 /* 160 * Do client specific suspend operations 161 * (e.g. set powerdown request bit) 162 */ 163 pm_client_suspend(proc, state); 164 165 /* Send request to the PLM */ 166 PM_PACK_PAYLOAD6(payload, LIBPM_MODULE_ID, flag, PM_SELF_SUSPEND, 167 proc->node_id, latency, state, address, 168 (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 /* Return if interrupt is not from PMU */ 285 if (pm_ipi_irq_status(primary_proc) != 0U) { 286 287 ret = pm_ipi_buff_read_callb(data, count); 288 289 if (ack != 0U) { 290 pm_ipi_irq_clear(primary_proc); 291 } 292 } 293 294 return ret; 295 } 296 297 /** 298 * pm_force_powerdown() - PM call to request for another PU or subsystem to 299 * be powered down forcefully. 300 * @target: Device ID of the PU node to be forced powered down. 301 * @ack: Flag to specify whether acknowledge is requested 302 * @flag: 0 - Call from secure source 303 * 1 - Call from non-secure source 304 * 305 * Return: Returns status, either success or error+reason. 306 * 307 */ 308 enum pm_ret_status pm_force_powerdown(uint32_t target, uint8_t ack, 309 uint32_t flag) 310 { 311 uint32_t payload[PAYLOAD_ARG_CNT]; 312 enum pm_ret_status ret = PM_RET_SUCCESS; 313 314 /* Send request to the PMC */ 315 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_FORCE_POWERDOWN, 316 target, ack); 317 318 if (ack == (uint32_t)IPI_BLOCKING) { 319 ret = pm_ipi_send_sync(primary_proc, payload, NULL, 0); 320 } else { 321 ret = pm_ipi_send(primary_proc, payload); 322 } 323 324 return ret; 325 } 326 327 /** 328 * pm_system_shutdown() - PM call to request a system shutdown or restart. 329 * @type: Shutdown or restart? 0=shutdown, 1=restart, 2=setscope. 330 * @subtype: Scope: 0=APU-subsystem, 1=PS, 2=system. 331 * @flag: 0 - Call from secure source. 332 * 1 - Call from non-secure source. 333 * 334 * Return: Returns status, either success or error+reason. 335 * 336 */ 337 enum pm_ret_status pm_system_shutdown(uint32_t type, uint32_t subtype, 338 uint32_t flag) 339 { 340 uint32_t payload[PAYLOAD_ARG_CNT]; 341 enum pm_ret_status ret = PM_RET_SUCCESS; 342 343 if (type == XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY) { 344 /* Setting scope for subsequent PSCI reboot or shutdown */ 345 pm_shutdown_scope = subtype; 346 goto exit_label; 347 } 348 349 /* Send request to the PMC */ 350 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SYSTEM_SHUTDOWN, 351 type, subtype); 352 353 ret = pm_ipi_send_non_blocking(primary_proc, payload); 354 355 exit_label: 356 return ret; 357 } 358 359 /** 360 * pm_set_wakeup_source() - PM call to specify the wakeup source while 361 * suspended. 362 * @target: Device id of the targeted PU or subsystem 363 * @wkup_device: Device id of the wakeup peripheral 364 * @enable: Enable or disable the specified peripheral as wake source 365 * @flag: 0 - Call from secure source 366 * 1 - Call from non-secure source 367 * 368 * Return: Returns status, either success or error+reason. 369 * 370 */ 371 enum pm_ret_status pm_set_wakeup_source(uint32_t target, uint32_t wkup_device, 372 uint8_t enable, uint32_t flag) 373 { 374 uint32_t payload[PAYLOAD_ARG_CNT]; 375 376 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_SET_WAKEUP_SOURCE, 377 target, wkup_device, enable); 378 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 379 } 380 381 /** 382 * eemi_feature_check() - Returns the supported API version if supported. 383 * @api_id: API ID to check. 384 * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of 385 * words Returned supported API version 386 * 387 * Return: Returns status, either success or error+reason. 388 */ 389 enum pm_ret_status eemi_feature_check(uint32_t api_id, uint32_t *ret_payload) 390 { 391 enum pm_ret_status ret; 392 393 /* Return version of API which are implemented in TF-A only */ 394 switch (api_id) { 395 case PM_GET_CALLBACK_DATA: 396 case PM_GET_TRUSTZONE_VERSION: 397 ret_payload[0] = PM_API_VERSION_2; 398 ret = PM_RET_SUCCESS; 399 break; 400 case TF_A_PM_REGISTER_SGI: 401 case TF_A_FEATURE_CHECK: 402 ret_payload[0] = PM_API_BASE_VERSION; 403 ret = PM_RET_SUCCESS; 404 break; 405 default: 406 ret = PM_RET_ERROR_NO_FEATURE; 407 } 408 409 return ret; 410 } 411 412 /** 413 * pm_feature_check() - Returns the supported API version if supported. 414 * @api_id: API ID to check. 415 * @flag: 0 - Call from secure source. 416 * 1 - Call from non-secure source. 417 * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of 418 * words Returned supported API version and bitmasks 419 * for IOCTL and QUERY ID 420 * 421 * Return: Returns status, either success or error+reason. 422 * 423 */ 424 enum pm_ret_status pm_feature_check(uint32_t api_id, uint32_t *ret_payload, 425 uint32_t flag) 426 { 427 uint32_t payload[PAYLOAD_ARG_CNT]; 428 uint32_t module_id; 429 enum pm_ret_status ret; 430 431 /* Return version of API which are implemented in TF-A only */ 432 switch (api_id) { 433 case PM_GET_CALLBACK_DATA: 434 case PM_GET_TRUSTZONE_VERSION: 435 ret_payload[0] = PM_API_VERSION_2; 436 ret = PM_RET_SUCCESS; 437 goto exit_label; 438 case TF_A_PM_REGISTER_SGI: 439 ret_payload[0] = PM_API_BASE_VERSION; 440 ret = PM_RET_SUCCESS; 441 goto exit_label; 442 default: 443 break; 444 } 445 446 module_id = (api_id & MODULE_ID_MASK) >> 8U; 447 448 /* 449 * feature check should be done only for LIBPM module 450 * If module_id is 0, then we consider it LIBPM module as default id 451 */ 452 if ((module_id > 0U) && (module_id != LIBPM_MODULE_ID)) { 453 ret = PM_RET_SUCCESS; 454 goto exit_label; 455 } 456 457 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, 458 PM_FEATURE_CHECK, api_id); 459 ret = pm_ipi_send_sync(primary_proc, payload, ret_payload, RET_PAYLOAD_ARG_CNT); 460 461 exit_label: 462 return ret; 463 464 } 465 466 /** 467 * pm_load_pdi() - Load the PDI. This function provides support to load 468 * PDI from linux. 469 * 470 * @src: Source device of pdi(DDR, OCM, SD etc). 471 * @address_low: lower 32-bit Linear memory space address. 472 * @address_high: higher 32-bit Linear memory space address. 473 * @flag: 0 - Call from secure source. 474 * 1 - Call from non-secure source. 475 * 476 * Return: Returns status, either success or error+reason. 477 * 478 */ 479 enum pm_ret_status pm_load_pdi(uint32_t src, uint32_t address_low, 480 uint32_t address_high, uint32_t flag) 481 { 482 uint32_t payload[PAYLOAD_ARG_CNT]; 483 484 /* Send request to the PMU */ 485 PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, flag, PM_LOAD_PDI, src, 486 address_high, address_low); 487 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 488 } 489 490 /** 491 * pm_register_notifier() - PM call to register a subsystem to be notified 492 * about the device event. 493 * @device_id: Device ID for the Node to which the event is related. 494 * @event: Event in question. 495 * @wake: Wake subsystem upon capturing the event if value 1. 496 * @enable: Enable the registration for value 1, disable for value 0. 497 * @flag: 0 - Call from secure source. 498 * 1 - Call from non-secure source. 499 * 500 * Return: Returns status, either success or error+reason. 501 * 502 */ 503 enum pm_ret_status pm_register_notifier(uint32_t device_id, uint32_t event, 504 uint32_t wake, uint32_t enable, 505 uint32_t flag) 506 { 507 uint32_t payload[PAYLOAD_ARG_CNT]; 508 509 /* Send request to the PMC */ 510 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REGISTER_NOTIFIER, 511 device_id, event, wake, enable); 512 513 return pm_ipi_send_sync(primary_proc, payload, NULL, 0); 514 } 515 516 /** 517 * pm_get_chipid() - Read silicon ID registers. 518 * @value: Buffer for two 32bit words. 519 * 520 * Return: Returns status, either success or error+reason and, 521 * optionally, @value. 522 */ 523 enum pm_ret_status pm_get_chipid(uint32_t *value) 524 { 525 uint32_t payload[PAYLOAD_ARG_CNT]; 526 527 PM_PACK_PAYLOAD1(payload, LIBPM_MODULE_ID, SECURE_FLAG, PM_GET_CHIPID); 528 529 return pm_ipi_send_sync(primary_proc, payload, value, 2); 530 } 531