blob: f9917a01293bd69b5625a665ac5ae04b03d2b297 [file] [log] [blame]
Tejas Patel354fe572018-12-14 00:55:37 -08001/*
Tanmay Shahff34daa2021-08-09 11:00:41 -07002 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
Jay Buddhabhatti26e138a2022-12-21 23:03:35 -08003 * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved.
Tejas Patel354fe572018-12-14 00:55:37 -08004 *
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>
Tejas Patel59c608a2019-01-09 04:10:29 -080014#include <stdbool.h>
Prasad Kummari536e1102023-06-22 10:50:02 +053015
16#include "../drivers/arm/gic/v3/gicv3_private.h"
17
Tejas Patel59c608a2019-01-09 04:10:29 -080018#include <common/runtime_svc.h>
Prasad Kummari536e1102023-06-22 10:50:02 +053019#include <drivers/arm/gicv3.h>
Jay Buddhabhatti3c06c7b2023-04-25 05:09:15 -070020#include <lib/psci/psci.h>
21#include <plat/arm/common/plat_arm.h>
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053022#include <plat/common/platform.h>
Prasad Kummari536e1102023-06-22 10:50:02 +053023
24#include <plat_private.h>
Tejas Patel59c608a2019-01-09 04:10:29 -080025#include "pm_api_sys.h"
Tejas Patel354fe572018-12-14 00:55:37 -080026#include "pm_client.h"
27#include "pm_ipi.h"
Jay Buddhabhatti10498a22023-07-31 00:11:00 -070028#include "pm_svc_main.h"
Tanmay Shahfde084c2022-09-13 10:24:27 -070029
30#define MODE 0x80000000U
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060031
32#define XSCUGIC_SGIR_EL1_INITID_SHIFT 24U
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -070033#define INVALID_SGI 0xFFU
Tanmay Shahfdae9e82022-08-26 15:06:00 -070034#define PM_INIT_SUSPEND_CB (30U)
35#define PM_NOTIFY_CB (32U)
Jay Buddhabhattia0f85572023-04-25 04:15:51 -070036#define EVENT_CPU_PWRDWN (4U)
Jay Buddhabhatti3c06c7b2023-04-25 05:09:15 -070037/* 1 sec of wait timeout for secondary core down */
38#define PWRDWN_WAIT_TIMEOUT (1000U)
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060039DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1, S3_0_C12_C11_6)
Tejas Patel354fe572018-12-14 00:55:37 -080040
Tejas Patel59c608a2019-01-09 04:10:29 -080041/* pm_up = true - UP, pm_up = false - DOWN */
42static bool pm_up;
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053043static uint32_t sgi = (uint32_t)INVALID_SGI;
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -070044bool pwrdwn_req_received;
Tejas Patel59c608a2019-01-09 04:10:29 -080045
Tanmay Shahfdae9e82022-08-26 15:06:00 -070046static void notify_os(void)
47{
Jay Buddhabhattibfe18c42023-09-11 23:50:53 -070048 plat_ic_raise_ns_sgi(sgi, read_mpidr_el1());
Tanmay Shahfdae9e82022-08-26 15:06:00 -070049}
50
Jay Buddhabhatti1dfe4972023-04-25 04:34:51 -070051static 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
Jay Buddhabhatti3c06c7b2023-04-25 05:09:15 -070064/**
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 */
71static void raise_pwr_down_interrupt(u_register_t mpidr)
72{
73 plat_ic_raise_el3_sgi(CPU_PWR_DOWN_REQ_INTR, mpidr);
74}
75
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -070076void request_cpu_pwrdwn(void)
Jay Buddhabhattia0f85572023-04-25 04:15:51 -070077{
Jay Buddhabhatti3c06c7b2023-04-25 05:09:15 -070078 enum pm_ret_status ret;
79
Jay Buddhabhattia0f85572023-04-25 04:15:51 -070080 VERBOSE("CPU power down request received\n");
Jay Buddhabhatti3c06c7b2023-04-25 05:09:15 -070081
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 */
Jay Buddhabhattia0f85572023-04-25 04:15:51 -070089 pm_ipi_irq_clear(primary_proc);
Jay Buddhabhatti3c06c7b2023-04-25 05:09:15 -070090
91 /* Deactivate IPI IRQ */
92 plat_ic_end_of_interrupt(PLAT_VERSAL_IPI_IRQ);
Jay Buddhabhattia0f85572023-04-25 04:15:51 -070093}
94
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053095static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle,
96 void *cookie)
97{
Tanmay Shahfdae9e82022-08-26 15:06:00 -070098 uint32_t payload[4] = {0};
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +053099 enum pm_ret_status ret;
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700100
101 VERBOSE("Received IPI FIQ from firmware\n");
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600102
Jay Buddhabhattiebf638d2023-10-08 22:08:02 -0700103 console_flush();
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530104 (void)plat_ic_acknowledge_interrupt();
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600105
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530106 ret = pm_get_callbackdata(payload, ARRAY_SIZE(payload), 0, 0);
107 if (ret != PM_RET_SUCCESS) {
108 payload[0] = ret;
109 }
110
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700111 switch (payload[0]) {
112 case PM_INIT_SUSPEND_CB:
Jay Buddhabhattia0f85572023-04-25 04:15:51 -0700113 if (sgi != INVALID_SGI) {
114 notify_os();
115 }
116 break;
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700117 case PM_NOTIFY_CB:
118 if (sgi != INVALID_SGI) {
Jay Buddhabhattia0f85572023-04-25 04:15:51 -0700119 if (payload[2] == EVENT_CPU_PWRDWN) {
120 if (pwrdwn_req_received) {
121 pwrdwn_req_received = false;
122 request_cpu_pwrdwn();
Jay Buddhabhatti3c06c7b2023-04-25 05:09:15 -0700123 (void)psci_cpu_off();
Jay Buddhabhattia0f85572023-04-25 04:15:51 -0700124 break;
125 } else {
126 pwrdwn_req_received = true;
127 }
128 }
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700129 notify_os();
130 }
131 break;
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530132 case PM_RET_ERROR_INVALID_CRC:
133 pm_ipi_irq_clear(primary_proc);
134 WARN("Invalid CRC in the payload\n");
135 break;
136
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700137 default:
138 pm_ipi_irq_clear(primary_proc);
139 WARN("Invalid IPI payload\n");
140 break;
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600141 }
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530142
143 /* Clear FIQ */
144 plat_ic_end_of_interrupt(id);
145
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600146 return 0;
147}
148
149/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530150 * pm_register_sgi() - PM register the IPI interrupt.
151 * @sgi_num: SGI number to be used for communication.
152 * @reset: Reset to invalid SGI when reset=1.
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600153 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530154 * Return: On success, the initialization function must return 0.
155 * Any other return value will cause the framework to ignore
156 * the service.
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600157 *
158 * Update the SGI number to be used.
159 *
160 */
Venkatesh Yadav Abbarapu2cefbcd2022-07-31 14:05:40 +0530161int32_t pm_register_sgi(uint32_t sgi_num, uint32_t reset)
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600162{
Venkatesh Yadav Abbarapuc8bbedc2021-04-19 07:49:57 -0600163 if (reset == 1U) {
164 sgi = INVALID_SGI;
165 return 0;
166 }
167
168 if (sgi != INVALID_SGI) {
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600169 return -EBUSY;
170 }
171
172 if (sgi_num >= GICV3_MAX_SGI_TARGETS) {
173 return -EINVAL;
174 }
175
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530176 sgi = (uint32_t)sgi_num;
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530177 return 0;
178}
179
Tejas Patel354fe572018-12-14 00:55:37 -0800180/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530181 * pm_setup() - PM service setup.
Tejas Patel354fe572018-12-14 00:55:37 -0800182 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530183 * Return: On success, the initialization function must return 0.
184 * Any other return value will cause the framework to ignore
185 * the service.
Tejas Patel354fe572018-12-14 00:55:37 -0800186 *
187 * Initialization functions for Versal power management for
188 * communicaton with PMC.
189 *
190 * Called from sip_svc_setup initialization function with the
191 * rt_svc_init signature.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530192 *
Tejas Patel354fe572018-12-14 00:55:37 -0800193 */
Venkatesh Yadav Abbarapu2cefbcd2022-07-31 14:05:40 +0530194int32_t pm_setup(void)
Tejas Patel354fe572018-12-14 00:55:37 -0800195{
HariBabu Gattemaa811712022-10-07 00:07:49 -0700196 int32_t ret = 0;
Tejas Patel354fe572018-12-14 00:55:37 -0800197
HariBabu Gattemaa811712022-10-07 00:07:49 -0700198 pm_ipi_init(primary_proc);
199 pm_up = true;
Tejas Patel354fe572018-12-14 00:55:37 -0800200
Jay Buddhabhatti1dfe4972023-04-25 04:34:51 -0700201 /* register SGI handler for CPU power down request */
202 ret = request_intr_type_el3(CPU_PWR_DOWN_REQ_INTR, cpu_pwrdwn_req_handler);
203 if (ret != 0) {
204 WARN("BL31: registering SGI interrupt failed\n");
205 }
206
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530207 /*
208 * Enable IPI IRQ
209 * assume the rich OS is OK to handle callback IRQs now.
210 * Even if we were wrong, it would not enable the IRQ in
211 * the GIC.
212 */
213 pm_ipi_irq_enable(primary_proc);
214
215 ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler);
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700216 if (ret != 0) {
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530217 WARN("BL31: registering IPI interrupt failed\n");
218 }
Tanmay Shahfde084c2022-09-13 10:24:27 -0700219
220 gicd_write_irouter(gicv3_driver_data->gicd_base, PLAT_VERSAL_IPI_IRQ, MODE);
Tejas Patel354fe572018-12-14 00:55:37 -0800221 return ret;
222}
Tejas Patel59c608a2019-01-09 04:10:29 -0800223
224/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530225 * eemi_for_compatibility() - EEMI calls handler for deprecated calls.
226 * @api_id: identifier for the API being called.
227 * @pm_arg: pointer to the argument data for the API call.
228 * @handle: Pointer to caller's context structure.
229 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tejas Patel59c608a2019-01-09 04:10:29 -0800230 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530231 * Return: If EEMI API found then, uintptr_t type address, else 0.
Tejas Patel59c608a2019-01-09 04:10:29 -0800232 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700233 * Some EEMI API's use case needs to be changed in Linux driver, so they
234 * can take advantage of common EEMI handler in TF-A. As of now the old
235 * implementation of these APIs are required to maintain backward compatibility
236 * until their use case in linux driver changes.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530237 *
Tejas Patel59c608a2019-01-09 04:10:29 -0800238 */
Tanmay Shahff34daa2021-08-09 11:00:41 -0700239static uintptr_t eemi_for_compatibility(uint32_t api_id, uint32_t *pm_arg,
240 void *handle, uint32_t security_flag)
Tejas Patel59c608a2019-01-09 04:10:29 -0800241{
242 enum pm_ret_status ret;
243
Tanmay Shahff34daa2021-08-09 11:00:41 -0700244 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800245
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800246 case (uint32_t)PM_IOCTL:
Tejas Patel9141f442019-01-10 03:03:48 -0800247 {
Naman Patelb8434b12022-11-16 01:54:23 -0800248 uint32_t value = 0U;
Tejas Patel9141f442019-01-10 03:03:48 -0800249
250 ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600251 pm_arg[3], pm_arg[4],
252 &value, security_flag);
Tanmay Shahff34daa2021-08-09 11:00:41 -0700253 if (ret == PM_RET_ERROR_NOTSUPPORTED)
254 return (uintptr_t)0;
255
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530256 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32U);
Tejas Patel9141f442019-01-10 03:03:48 -0800257 }
258
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800259 case (uint32_t)PM_QUERY_DATA:
Tejas Patela3e34ad2019-02-01 17:25:19 +0530260 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700261 uint32_t data[PAYLOAD_ARG_CNT] = { 0 };
Tejas Patela3e34ad2019-02-01 17:25:19 +0530262
263 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
Michal Simek57cf55a2022-07-29 07:48:59 +0200264 pm_arg[3], data, security_flag);
Rajan Vaja030620d2020-11-23 04:13:54 -0800265
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530266 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)data[0] << 32U),
Michal Simek57cf55a2022-07-29 07:48:59 +0200267 (uint64_t)data[1] | ((uint64_t)data[2] << 32U));
Rajan Vaja030620d2020-11-23 04:13:54 -0800268 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800269
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800270 case (uint32_t)PM_FEATURE_CHECK:
Tejas Patel59c608a2019-01-09 04:10:29 -0800271 {
Ronak Jain70d1c582022-02-04 00:42:55 -0800272 uint32_t result[PAYLOAD_ARG_CNT] = {0U};
Tejas Patel59c608a2019-01-09 04:10:29 -0800273
Ronak Jain70d1c582022-02-04 00:42:55 -0800274 ret = pm_feature_check(pm_arg[0], result, security_flag);
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530275 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U),
276 (uint64_t)result[1] | ((uint64_t)result[2] << 32U));
Tejas Patel59c608a2019-01-09 04:10:29 -0800277 }
278
Tanmay Shahff34daa2021-08-09 11:00:41 -0700279 case PM_LOAD_PDI:
Tejas Patel59c608a2019-01-09 04:10:29 -0800280 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700281 ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
282 security_flag);
283 SMC_RET1(handle, (uint64_t)ret);
284 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800285
Tanmay Shahff34daa2021-08-09 11:00:41 -0700286 default:
287 return (uintptr_t)0;
Tejas Patel59c608a2019-01-09 04:10:29 -0800288 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700289}
Tejas Patel59c608a2019-01-09 04:10:29 -0800290
Tanmay Shahff34daa2021-08-09 11:00:41 -0700291/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530292 * eemi_psci_debugfs_handler() - EEMI API invoked from PSCI.
293 * @api_id: identifier for the API being called.
294 * @pm_arg: pointer to the argument data for the API call.
295 * @handle: Pointer to caller's context structure.
296 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700297 *
298 * These EEMI APIs performs CPU specific power management tasks.
299 * These EEMI APIs are invoked either from PSCI or from debugfs in kernel.
300 * These calls require CPU specific processing before sending IPI request to
301 * Platform Management Controller. For example enable/disable CPU specific
302 * interrupts. This requires separate handler for these calls and may not be
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530303 * handled using common eemi handler.
304 *
305 * Return: If EEMI API found then, uintptr_t type address, else 0.
306 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700307 */
308static uintptr_t eemi_psci_debugfs_handler(uint32_t api_id, uint32_t *pm_arg,
309 void *handle, uint32_t security_flag)
310{
311 enum pm_ret_status ret;
Tejas Patel59c608a2019-01-09 04:10:29 -0800312
Tanmay Shahff34daa2021-08-09 11:00:41 -0700313 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800314
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800315 case (uint32_t)PM_SELF_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700316 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
317 pm_arg[3], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530318 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800319
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800320 case (uint32_t)PM_FORCE_POWERDOWN:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700321 ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530322 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel84275bf2020-09-01 04:43:53 -0700323
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800324 case (uint32_t)PM_REQ_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700325 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
326 pm_arg[3], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530327 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel84275bf2020-09-01 04:43:53 -0700328
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800329 case (uint32_t)PM_ABORT_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700330 ret = pm_abort_suspend(pm_arg[0], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530331 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800332
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800333 case (uint32_t)PM_SYSTEM_SHUTDOWN:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700334 ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530335 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800336
Tanmay Shahff34daa2021-08-09 11:00:41 -0700337 default:
338 return (uintptr_t)0;
Tejas Patel59c608a2019-01-09 04:10:29 -0800339 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700340}
Tejas Patel59c608a2019-01-09 04:10:29 -0800341
Tanmay Shahff34daa2021-08-09 11:00:41 -0700342/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530343 * TF_A_specific_handler() - SMC handler for TF-A specific functionality.
344 * @api_id: identifier for the API being called.
345 * @pm_arg: pointer to the argument data for the API call.
346 * @handle: Pointer to caller's context structure.
347 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700348 *
349 * These EEMI calls performs functionality that does not require
350 * IPI transaction. The handler ends in TF-A and returns requested data to
Michal Simek509bfc72022-09-14 09:32:47 +0200351 * kernel from TF-A.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530352 *
353 * Return: If TF-A specific API found then, uintptr_t type address, else 0
354 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700355 */
356static uintptr_t TF_A_specific_handler(uint32_t api_id, uint32_t *pm_arg,
357 void *handle, uint32_t security_flag)
358{
359 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800360
Tanmay Shah0dde16c2021-12-14 04:53:40 -0800361 case TF_A_PM_REGISTER_SGI:
362 {
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530363 int32_t ret;
Tanmay Shah0dde16c2021-12-14 04:53:40 -0800364
365 ret = pm_register_sgi(pm_arg[0], pm_arg[1]);
366 if (ret != 0) {
367 SMC_RET1(handle, (uint32_t)PM_RET_ERROR_ARGS);
368 }
369
370 SMC_RET1(handle, (uint32_t)PM_RET_SUCCESS);
371 }
372
Tanmay Shahff34daa2021-08-09 11:00:41 -0700373 case PM_GET_CALLBACK_DATA:
Tejas Patel59c608a2019-01-09 04:10:29 -0800374 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700375 uint32_t result[4] = {0};
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530376 enum pm_ret_status ret;
377
378 ret = pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag, 1U);
379 if (ret != 0) {
380 result[0] = ret;
381 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800382
Tanmay Shahff34daa2021-08-09 11:00:41 -0700383 SMC_RET2(handle,
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530384 (uint64_t)result[0] | ((uint64_t)result[1] << 32U),
385 (uint64_t)result[2] | ((uint64_t)result[3] << 32U));
Tejas Patel59c608a2019-01-09 04:10:29 -0800386 }
387
Tejas Patel044001d2019-01-21 17:56:48 +0530388 case PM_GET_TRUSTZONE_VERSION:
389 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
Jay Buddhabhattic3da9c02022-04-04 23:42:04 -0700390 ((uint64_t)TZ_VERSION << 32U));
Tejas Patel044001d2019-01-21 17:56:48 +0530391
Tanmay Shahff34daa2021-08-09 11:00:41 -0700392 default:
393 return (uintptr_t)0;
Ravi Patelbd4aa5a2019-08-12 03:17:54 -0700394 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700395}
Ravi Patelbd4aa5a2019-08-12 03:17:54 -0700396
Tanmay Shahff34daa2021-08-09 11:00:41 -0700397/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530398 * eemi_handler() - Prepare EEMI payload and perform IPI transaction.
399 * @api_id: identifier for the API being called.
400 * @pm_arg: pointer to the argument data for the API call.
401 * @handle: Pointer to caller's context structure.
402 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700403 *
404 * EEMI - Embedded Energy Management Interface is Xilinx proprietary protocol
405 * to allow communication between power management controller and different
406 * processing clusters.
407 *
408 * This handler prepares EEMI protocol payload received from kernel and performs
409 * IPI transaction.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530410 *
411 * Return: If EEMI API found then, uintptr_t type address, else 0
412 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700413 */
414static uintptr_t eemi_handler(uint32_t api_id, uint32_t *pm_arg,
415 void *handle, uint32_t security_flag)
416{
417 enum pm_ret_status ret;
418 uint32_t buf[PAYLOAD_ARG_CNT] = {0};
Ravi Patel22b0b492019-03-06 12:34:46 +0530419
Tanmay Shahff34daa2021-08-09 11:00:41 -0700420 ret = pm_handle_eemi_call(security_flag, api_id, pm_arg[0], pm_arg[1],
421 pm_arg[2], pm_arg[3], pm_arg[4],
422 (uint64_t *)buf);
423 /*
424 * Two IOCTLs, to get clock name and pinctrl name of pm_query_data API
425 * receives 5 words of respoonse from firmware. Currently linux driver can
426 * receive only 4 words from TF-A. So, this needs to be handled separately
427 * than other eemi calls.
428 */
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800429 if (api_id == (uint32_t)PM_QUERY_DATA) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700430 if ((pm_arg[0] == XPM_QID_CLOCK_GET_NAME ||
431 pm_arg[0] == XPM_QID_PINCTRL_GET_FUNCTION_NAME) &&
432 ret == PM_RET_SUCCESS) {
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530433 SMC_RET2(handle, (uint64_t)buf[0] | ((uint64_t)buf[1] << 32U),
434 (uint64_t)buf[2] | ((uint64_t)buf[3] << 32U));
Tanmay Shahff34daa2021-08-09 11:00:41 -0700435 }
Ravi Patel22b0b492019-03-06 12:34:46 +0530436 }
437
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530438 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U),
439 (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U));
Tanmay Shahff34daa2021-08-09 11:00:41 -0700440}
Jolly Shahed05a712019-03-22 05:33:39 +0530441
Tanmay Shahff34daa2021-08-09 11:00:41 -0700442/**
443 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530444 * @smc_fid: Function Identifier.
445 * @x1: SMC64 Arguments from kernel.
446 * @x2: SMC64 Arguments from kernel.
447 * @x3: SMC64 Arguments from kernel (upper 32-bits).
448 * @x4: Unused.
449 * @cookie: Unused.
450 * @handle: Pointer to caller's context structure.
451 * @flags: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700452 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530453 * Return: Unused.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700454 *
455 * Determines that smc_fid is valid and supported PM SMC Function ID from the
456 * list of pm_api_ids, otherwise completes the request with
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530457 * the unknown SMC Function ID.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700458 *
459 * The SMC calls for PM service are forwarded from SIP Service SMC handler
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530460 * function with rt_svc_handle signature.
461 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700462 */
463uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
Venkatesh Yadav Abbarapud3c0eb42022-05-24 14:02:52 +0530464 uint64_t x4, const void *cookie, void *handle, uint64_t flags)
Tanmay Shahff34daa2021-08-09 11:00:41 -0700465{
466 uintptr_t ret;
467 uint32_t pm_arg[PAYLOAD_ARG_CNT] = {0};
Jay Buddhabhattibd603bf2023-07-30 23:49:11 -0700468 uint32_t security_flag = NON_SECURE_FLAG;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700469 uint32_t api_id;
Jay Buddhabhatti10498a22023-07-31 00:11:00 -0700470 bool status = false, status_tmp = false;
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700471
Tanmay Shahff34daa2021-08-09 11:00:41 -0700472 /* Handle case where PM wasn't initialized properly */
Michal Simek5e2f5962022-09-13 11:48:53 +0200473 if (pm_up == false) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700474 SMC_RET1(handle, SMC_UNK);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530475 }
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700476
Tanmay Shahff34daa2021-08-09 11:00:41 -0700477 /*
Jay Buddhabhattibd603bf2023-07-30 23:49:11 -0700478 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as secure (0)
479 * if smc called is secure
Jay Buddhabhatti10498a22023-07-31 00:11:00 -0700480 *
481 * Add redundant macro call to immune the code from glitches
Tanmay Shahff34daa2021-08-09 11:00:41 -0700482 */
Jay Buddhabhatti10498a22023-07-31 00:11:00 -0700483 SECURE_REDUNDANT_CALL(status, status_tmp, is_caller_secure, flags);
484 if ((status != false) && (status_tmp != false)) {
Jay Buddhabhattibd603bf2023-07-30 23:49:11 -0700485 security_flag = SECURE_FLAG;
Tejas Patel5c154e12020-11-25 01:53:12 -0800486 }
487
Tanmay Shahff34daa2021-08-09 11:00:41 -0700488 pm_arg[0] = (uint32_t)x1;
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530489 pm_arg[1] = (uint32_t)(x1 >> 32U);
Tanmay Shahff34daa2021-08-09 11:00:41 -0700490 pm_arg[2] = (uint32_t)x2;
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530491 pm_arg[3] = (uint32_t)(x2 >> 32U);
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600492 pm_arg[4] = (uint32_t)x3;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700493 (void)(x4);
494 api_id = smc_fid & FUNCID_NUM_MASK;
Tejas Patel42015552020-11-25 01:56:57 -0800495
Tanmay Shahff34daa2021-08-09 11:00:41 -0700496 ret = eemi_for_compatibility(api_id, pm_arg, handle, security_flag);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530497 if (ret != (uintptr_t)0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700498 return ret;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530499 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700500
501 ret = eemi_psci_debugfs_handler(api_id, pm_arg, handle, flags);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530502 if (ret != (uintptr_t)0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700503 return ret;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530504 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700505
506 ret = TF_A_specific_handler(api_id, pm_arg, handle, security_flag);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530507 if (ret != (uintptr_t)0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700508 return ret;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530509 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700510
511 ret = eemi_handler(api_id, pm_arg, handle, security_flag);
512
513 return ret;
Tejas Patel59c608a2019-01-09 04:10:29 -0800514}