blob: 1e5808cfd4ea7d75318a597a7f761cf5d61767b6 [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>
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053020#include <plat/common/platform.h>
Prasad Kummari536e1102023-06-22 10:50:02 +053021
22#include <plat_private.h>
Tejas Patel59c608a2019-01-09 04:10:29 -080023#include "pm_api_sys.h"
Tejas Patel354fe572018-12-14 00:55:37 -080024#include "pm_client.h"
25#include "pm_ipi.h"
Jay Buddhabhatti10498a22023-07-31 00:11:00 -070026#include "pm_svc_main.h"
Tanmay Shahfde084c2022-09-13 10:24:27 -070027
28#define MODE 0x80000000U
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060029
30#define XSCUGIC_SGIR_EL1_INITID_SHIFT 24U
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -070031#define INVALID_SGI 0xFFU
Tanmay Shahfdae9e82022-08-26 15:06:00 -070032#define PM_INIT_SUSPEND_CB (30U)
33#define PM_NOTIFY_CB (32U)
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060034DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1, S3_0_C12_C11_6)
Tejas Patel354fe572018-12-14 00:55:37 -080035
Tejas Patel59c608a2019-01-09 04:10:29 -080036/* pm_up = true - UP, pm_up = false - DOWN */
37static bool pm_up;
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053038static uint32_t sgi = (uint32_t)INVALID_SGI;
Tejas Patel59c608a2019-01-09 04:10:29 -080039
Tanmay Shahfdae9e82022-08-26 15:06:00 -070040static void notify_os(void)
41{
42 int32_t cpu;
43 uint32_t reg;
44
45 cpu = plat_my_core_pos() + 1U;
46
47 reg = (cpu | (sgi << XSCUGIC_SGIR_EL1_INITID_SHIFT));
48 write_icc_asgi1r_el1(reg);
49}
50
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053051static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle,
52 void *cookie)
53{
Tanmay Shahfdae9e82022-08-26 15:06:00 -070054 uint32_t payload[4] = {0};
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +053055 enum pm_ret_status ret;
Tanmay Shahfdae9e82022-08-26 15:06:00 -070056
57 VERBOSE("Received IPI FIQ from firmware\n");
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060058
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053059 (void)plat_ic_acknowledge_interrupt();
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060060
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +053061 ret = pm_get_callbackdata(payload, ARRAY_SIZE(payload), 0, 0);
62 if (ret != PM_RET_SUCCESS) {
63 payload[0] = ret;
64 }
65
Tanmay Shahfdae9e82022-08-26 15:06:00 -070066 switch (payload[0]) {
67 case PM_INIT_SUSPEND_CB:
68 case PM_NOTIFY_CB:
69 if (sgi != INVALID_SGI) {
70 notify_os();
71 }
72 break;
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +053073 case PM_RET_ERROR_INVALID_CRC:
74 pm_ipi_irq_clear(primary_proc);
75 WARN("Invalid CRC in the payload\n");
76 break;
77
Tanmay Shahfdae9e82022-08-26 15:06:00 -070078 default:
79 pm_ipi_irq_clear(primary_proc);
80 WARN("Invalid IPI payload\n");
81 break;
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060082 }
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053083
84 /* Clear FIQ */
85 plat_ic_end_of_interrupt(id);
86
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060087 return 0;
88}
89
90/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +053091 * pm_register_sgi() - PM register the IPI interrupt.
92 * @sgi_num: SGI number to be used for communication.
93 * @reset: Reset to invalid SGI when reset=1.
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060094 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +053095 * Return: On success, the initialization function must return 0.
96 * Any other return value will cause the framework to ignore
97 * the service.
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060098 *
99 * Update the SGI number to be used.
100 *
101 */
Venkatesh Yadav Abbarapu2cefbcd2022-07-31 14:05:40 +0530102int32_t pm_register_sgi(uint32_t sgi_num, uint32_t reset)
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600103{
Venkatesh Yadav Abbarapuc8bbedc2021-04-19 07:49:57 -0600104 if (reset == 1U) {
105 sgi = INVALID_SGI;
106 return 0;
107 }
108
109 if (sgi != INVALID_SGI) {
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600110 return -EBUSY;
111 }
112
113 if (sgi_num >= GICV3_MAX_SGI_TARGETS) {
114 return -EINVAL;
115 }
116
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530117 sgi = (uint32_t)sgi_num;
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530118 return 0;
119}
120
Tejas Patel354fe572018-12-14 00:55:37 -0800121/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530122 * pm_setup() - PM service setup.
Tejas Patel354fe572018-12-14 00:55:37 -0800123 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530124 * Return: On success, the initialization function must return 0.
125 * Any other return value will cause the framework to ignore
126 * the service.
Tejas Patel354fe572018-12-14 00:55:37 -0800127 *
128 * Initialization functions for Versal power management for
129 * communicaton with PMC.
130 *
131 * Called from sip_svc_setup initialization function with the
132 * rt_svc_init signature.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530133 *
Tejas Patel354fe572018-12-14 00:55:37 -0800134 */
Venkatesh Yadav Abbarapu2cefbcd2022-07-31 14:05:40 +0530135int32_t pm_setup(void)
Tejas Patel354fe572018-12-14 00:55:37 -0800136{
HariBabu Gattemaa811712022-10-07 00:07:49 -0700137 int32_t ret = 0;
Tejas Patel354fe572018-12-14 00:55:37 -0800138
HariBabu Gattemaa811712022-10-07 00:07:49 -0700139 pm_ipi_init(primary_proc);
140 pm_up = true;
Tejas Patel354fe572018-12-14 00:55:37 -0800141
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530142 /*
143 * Enable IPI IRQ
144 * assume the rich OS is OK to handle callback IRQs now.
145 * Even if we were wrong, it would not enable the IRQ in
146 * the GIC.
147 */
148 pm_ipi_irq_enable(primary_proc);
149
150 ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler);
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700151 if (ret != 0) {
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530152 WARN("BL31: registering IPI interrupt failed\n");
153 }
Tanmay Shahfde084c2022-09-13 10:24:27 -0700154
155 gicd_write_irouter(gicv3_driver_data->gicd_base, PLAT_VERSAL_IPI_IRQ, MODE);
Tejas Patel354fe572018-12-14 00:55:37 -0800156 return ret;
157}
Tejas Patel59c608a2019-01-09 04:10:29 -0800158
159/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530160 * eemi_for_compatibility() - EEMI calls handler for deprecated calls.
161 * @api_id: identifier for the API being called.
162 * @pm_arg: pointer to the argument data for the API call.
163 * @handle: Pointer to caller's context structure.
164 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tejas Patel59c608a2019-01-09 04:10:29 -0800165 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530166 * Return: If EEMI API found then, uintptr_t type address, else 0.
Tejas Patel59c608a2019-01-09 04:10:29 -0800167 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700168 * Some EEMI API's use case needs to be changed in Linux driver, so they
169 * can take advantage of common EEMI handler in TF-A. As of now the old
170 * implementation of these APIs are required to maintain backward compatibility
171 * until their use case in linux driver changes.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530172 *
Tejas Patel59c608a2019-01-09 04:10:29 -0800173 */
Tanmay Shahff34daa2021-08-09 11:00:41 -0700174static uintptr_t eemi_for_compatibility(uint32_t api_id, uint32_t *pm_arg,
175 void *handle, uint32_t security_flag)
Tejas Patel59c608a2019-01-09 04:10:29 -0800176{
177 enum pm_ret_status ret;
178
Tanmay Shahff34daa2021-08-09 11:00:41 -0700179 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800180
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800181 case (uint32_t)PM_IOCTL:
Tejas Patel9141f442019-01-10 03:03:48 -0800182 {
Naman Patelb8434b12022-11-16 01:54:23 -0800183 uint32_t value = 0U;
Tejas Patel9141f442019-01-10 03:03:48 -0800184
185 ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600186 pm_arg[3], pm_arg[4],
187 &value, security_flag);
Tanmay Shahff34daa2021-08-09 11:00:41 -0700188 if (ret == PM_RET_ERROR_NOTSUPPORTED)
189 return (uintptr_t)0;
190
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530191 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32U);
Tejas Patel9141f442019-01-10 03:03:48 -0800192 }
193
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800194 case (uint32_t)PM_QUERY_DATA:
Tejas Patela3e34ad2019-02-01 17:25:19 +0530195 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700196 uint32_t data[PAYLOAD_ARG_CNT] = { 0 };
Tejas Patela3e34ad2019-02-01 17:25:19 +0530197
198 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
Michal Simek57cf55a2022-07-29 07:48:59 +0200199 pm_arg[3], data, security_flag);
Rajan Vaja030620d2020-11-23 04:13:54 -0800200
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530201 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)data[0] << 32U),
Michal Simek57cf55a2022-07-29 07:48:59 +0200202 (uint64_t)data[1] | ((uint64_t)data[2] << 32U));
Rajan Vaja030620d2020-11-23 04:13:54 -0800203 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800204
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800205 case (uint32_t)PM_FEATURE_CHECK:
Tejas Patel59c608a2019-01-09 04:10:29 -0800206 {
Ronak Jain70d1c582022-02-04 00:42:55 -0800207 uint32_t result[PAYLOAD_ARG_CNT] = {0U};
Tejas Patel59c608a2019-01-09 04:10:29 -0800208
Ronak Jain70d1c582022-02-04 00:42:55 -0800209 ret = pm_feature_check(pm_arg[0], result, security_flag);
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530210 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U),
211 (uint64_t)result[1] | ((uint64_t)result[2] << 32U));
Tejas Patel59c608a2019-01-09 04:10:29 -0800212 }
213
Tanmay Shahff34daa2021-08-09 11:00:41 -0700214 case PM_LOAD_PDI:
Tejas Patel59c608a2019-01-09 04:10:29 -0800215 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700216 ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
217 security_flag);
218 SMC_RET1(handle, (uint64_t)ret);
219 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800220
Tanmay Shahff34daa2021-08-09 11:00:41 -0700221 default:
222 return (uintptr_t)0;
Tejas Patel59c608a2019-01-09 04:10:29 -0800223 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700224}
Tejas Patel59c608a2019-01-09 04:10:29 -0800225
Tanmay Shahff34daa2021-08-09 11:00:41 -0700226/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530227 * eemi_psci_debugfs_handler() - EEMI API invoked from PSCI.
228 * @api_id: identifier for the API being called.
229 * @pm_arg: pointer to the argument data for the API call.
230 * @handle: Pointer to caller's context structure.
231 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700232 *
233 * These EEMI APIs performs CPU specific power management tasks.
234 * These EEMI APIs are invoked either from PSCI or from debugfs in kernel.
235 * These calls require CPU specific processing before sending IPI request to
236 * Platform Management Controller. For example enable/disable CPU specific
237 * interrupts. This requires separate handler for these calls and may not be
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530238 * handled using common eemi handler.
239 *
240 * Return: If EEMI API found then, uintptr_t type address, else 0.
241 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700242 */
243static uintptr_t eemi_psci_debugfs_handler(uint32_t api_id, uint32_t *pm_arg,
244 void *handle, uint32_t security_flag)
245{
246 enum pm_ret_status ret;
Tejas Patel59c608a2019-01-09 04:10:29 -0800247
Tanmay Shahff34daa2021-08-09 11:00:41 -0700248 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800249
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800250 case (uint32_t)PM_SELF_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700251 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
252 pm_arg[3], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530253 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800254
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800255 case (uint32_t)PM_FORCE_POWERDOWN:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700256 ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530257 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel84275bf2020-09-01 04:43:53 -0700258
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800259 case (uint32_t)PM_REQ_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700260 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
261 pm_arg[3], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530262 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel84275bf2020-09-01 04:43:53 -0700263
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800264 case (uint32_t)PM_ABORT_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700265 ret = pm_abort_suspend(pm_arg[0], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530266 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800267
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800268 case (uint32_t)PM_SYSTEM_SHUTDOWN:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700269 ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530270 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800271
Tanmay Shahff34daa2021-08-09 11:00:41 -0700272 default:
273 return (uintptr_t)0;
Tejas Patel59c608a2019-01-09 04:10:29 -0800274 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700275}
Tejas Patel59c608a2019-01-09 04:10:29 -0800276
Tanmay Shahff34daa2021-08-09 11:00:41 -0700277/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530278 * TF_A_specific_handler() - SMC handler for TF-A specific functionality.
279 * @api_id: identifier for the API being called.
280 * @pm_arg: pointer to the argument data for the API call.
281 * @handle: Pointer to caller's context structure.
282 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700283 *
284 * These EEMI calls performs functionality that does not require
285 * IPI transaction. The handler ends in TF-A and returns requested data to
Michal Simek509bfc72022-09-14 09:32:47 +0200286 * kernel from TF-A.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530287 *
288 * Return: If TF-A specific API found then, uintptr_t type address, else 0
289 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700290 */
291static uintptr_t TF_A_specific_handler(uint32_t api_id, uint32_t *pm_arg,
292 void *handle, uint32_t security_flag)
293{
294 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800295
Tanmay Shah0dde16c2021-12-14 04:53:40 -0800296 case TF_A_PM_REGISTER_SGI:
297 {
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530298 int32_t ret;
Tanmay Shah0dde16c2021-12-14 04:53:40 -0800299
300 ret = pm_register_sgi(pm_arg[0], pm_arg[1]);
301 if (ret != 0) {
302 SMC_RET1(handle, (uint32_t)PM_RET_ERROR_ARGS);
303 }
304
305 SMC_RET1(handle, (uint32_t)PM_RET_SUCCESS);
306 }
307
Tanmay Shahff34daa2021-08-09 11:00:41 -0700308 case PM_GET_CALLBACK_DATA:
Tejas Patel59c608a2019-01-09 04:10:29 -0800309 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700310 uint32_t result[4] = {0};
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530311 enum pm_ret_status ret;
312
313 ret = pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag, 1U);
314 if (ret != 0) {
315 result[0] = ret;
316 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800317
Tanmay Shahff34daa2021-08-09 11:00:41 -0700318 SMC_RET2(handle,
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530319 (uint64_t)result[0] | ((uint64_t)result[1] << 32U),
320 (uint64_t)result[2] | ((uint64_t)result[3] << 32U));
Tejas Patel59c608a2019-01-09 04:10:29 -0800321 }
322
Tejas Patel044001d2019-01-21 17:56:48 +0530323 case PM_GET_TRUSTZONE_VERSION:
324 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
Jay Buddhabhattic3da9c02022-04-04 23:42:04 -0700325 ((uint64_t)TZ_VERSION << 32U));
Tejas Patel044001d2019-01-21 17:56:48 +0530326
Tanmay Shahff34daa2021-08-09 11:00:41 -0700327 default:
328 return (uintptr_t)0;
Ravi Patelbd4aa5a2019-08-12 03:17:54 -0700329 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700330}
Ravi Patelbd4aa5a2019-08-12 03:17:54 -0700331
Tanmay Shahff34daa2021-08-09 11:00:41 -0700332/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530333 * eemi_handler() - Prepare EEMI payload and perform IPI transaction.
334 * @api_id: identifier for the API being called.
335 * @pm_arg: pointer to the argument data for the API call.
336 * @handle: Pointer to caller's context structure.
337 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700338 *
339 * EEMI - Embedded Energy Management Interface is Xilinx proprietary protocol
340 * to allow communication between power management controller and different
341 * processing clusters.
342 *
343 * This handler prepares EEMI protocol payload received from kernel and performs
344 * IPI transaction.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530345 *
346 * Return: If EEMI API found then, uintptr_t type address, else 0
347 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700348 */
349static uintptr_t eemi_handler(uint32_t api_id, uint32_t *pm_arg,
350 void *handle, uint32_t security_flag)
351{
352 enum pm_ret_status ret;
353 uint32_t buf[PAYLOAD_ARG_CNT] = {0};
Ravi Patel22b0b492019-03-06 12:34:46 +0530354
Tanmay Shahff34daa2021-08-09 11:00:41 -0700355 ret = pm_handle_eemi_call(security_flag, api_id, pm_arg[0], pm_arg[1],
356 pm_arg[2], pm_arg[3], pm_arg[4],
357 (uint64_t *)buf);
358 /*
359 * Two IOCTLs, to get clock name and pinctrl name of pm_query_data API
360 * receives 5 words of respoonse from firmware. Currently linux driver can
361 * receive only 4 words from TF-A. So, this needs to be handled separately
362 * than other eemi calls.
363 */
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800364 if (api_id == (uint32_t)PM_QUERY_DATA) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700365 if ((pm_arg[0] == XPM_QID_CLOCK_GET_NAME ||
366 pm_arg[0] == XPM_QID_PINCTRL_GET_FUNCTION_NAME) &&
367 ret == PM_RET_SUCCESS) {
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530368 SMC_RET2(handle, (uint64_t)buf[0] | ((uint64_t)buf[1] << 32U),
369 (uint64_t)buf[2] | ((uint64_t)buf[3] << 32U));
Tanmay Shahff34daa2021-08-09 11:00:41 -0700370 }
Ravi Patel22b0b492019-03-06 12:34:46 +0530371 }
372
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530373 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U),
374 (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U));
Tanmay Shahff34daa2021-08-09 11:00:41 -0700375}
Jolly Shahed05a712019-03-22 05:33:39 +0530376
Tanmay Shahff34daa2021-08-09 11:00:41 -0700377/**
378 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530379 * @smc_fid: Function Identifier.
380 * @x1: SMC64 Arguments from kernel.
381 * @x2: SMC64 Arguments from kernel.
382 * @x3: SMC64 Arguments from kernel (upper 32-bits).
383 * @x4: Unused.
384 * @cookie: Unused.
385 * @handle: Pointer to caller's context structure.
386 * @flags: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700387 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530388 * Return: Unused.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700389 *
390 * Determines that smc_fid is valid and supported PM SMC Function ID from the
391 * list of pm_api_ids, otherwise completes the request with
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530392 * the unknown SMC Function ID.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700393 *
394 * The SMC calls for PM service are forwarded from SIP Service SMC handler
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530395 * function with rt_svc_handle signature.
396 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700397 */
398uint64_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 +0530399 uint64_t x4, const void *cookie, void *handle, uint64_t flags)
Tanmay Shahff34daa2021-08-09 11:00:41 -0700400{
401 uintptr_t ret;
402 uint32_t pm_arg[PAYLOAD_ARG_CNT] = {0};
Jay Buddhabhattibd603bf2023-07-30 23:49:11 -0700403 uint32_t security_flag = NON_SECURE_FLAG;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700404 uint32_t api_id;
Jay Buddhabhatti10498a22023-07-31 00:11:00 -0700405 bool status = false, status_tmp = false;
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700406
Tanmay Shahff34daa2021-08-09 11:00:41 -0700407 /* Handle case where PM wasn't initialized properly */
Michal Simek5e2f5962022-09-13 11:48:53 +0200408 if (pm_up == false) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700409 SMC_RET1(handle, SMC_UNK);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530410 }
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700411
Tanmay Shahff34daa2021-08-09 11:00:41 -0700412 /*
Jay Buddhabhattibd603bf2023-07-30 23:49:11 -0700413 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as secure (0)
414 * if smc called is secure
Jay Buddhabhatti10498a22023-07-31 00:11:00 -0700415 *
416 * Add redundant macro call to immune the code from glitches
Tanmay Shahff34daa2021-08-09 11:00:41 -0700417 */
Jay Buddhabhatti10498a22023-07-31 00:11:00 -0700418 SECURE_REDUNDANT_CALL(status, status_tmp, is_caller_secure, flags);
419 if ((status != false) && (status_tmp != false)) {
Jay Buddhabhattibd603bf2023-07-30 23:49:11 -0700420 security_flag = SECURE_FLAG;
Tejas Patel5c154e12020-11-25 01:53:12 -0800421 }
422
Tanmay Shahff34daa2021-08-09 11:00:41 -0700423 pm_arg[0] = (uint32_t)x1;
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530424 pm_arg[1] = (uint32_t)(x1 >> 32U);
Tanmay Shahff34daa2021-08-09 11:00:41 -0700425 pm_arg[2] = (uint32_t)x2;
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530426 pm_arg[3] = (uint32_t)(x2 >> 32U);
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600427 pm_arg[4] = (uint32_t)x3;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700428 (void)(x4);
429 api_id = smc_fid & FUNCID_NUM_MASK;
Tejas Patel42015552020-11-25 01:56:57 -0800430
Tanmay Shahff34daa2021-08-09 11:00:41 -0700431 ret = eemi_for_compatibility(api_id, pm_arg, handle, security_flag);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530432 if (ret != (uintptr_t)0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700433 return ret;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530434 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700435
436 ret = eemi_psci_debugfs_handler(api_id, pm_arg, handle, flags);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530437 if (ret != (uintptr_t)0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700438 return ret;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530439 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700440
441 ret = TF_A_specific_handler(api_id, pm_arg, handle, security_flag);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530442 if (ret != (uintptr_t)0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700443 return ret;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530444 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700445
446 ret = eemi_handler(api_id, pm_arg, handle, security_flag);
447
448 return ret;
Tejas Patel59c608a2019-01-09 04:10:29 -0800449}