blob: fb32f2a01bc82907070719fc47b7630b1e25a33b [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"
Tanmay Shahfde084c2022-09-13 10:24:27 -070026
27#define MODE 0x80000000U
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060028
29#define XSCUGIC_SGIR_EL1_INITID_SHIFT 24U
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -070030#define INVALID_SGI 0xFFU
Tanmay Shahfdae9e82022-08-26 15:06:00 -070031#define PM_INIT_SUSPEND_CB (30U)
32#define PM_NOTIFY_CB (32U)
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060033DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1, S3_0_C12_C11_6)
Tejas Patel354fe572018-12-14 00:55:37 -080034
Tejas Patel59c608a2019-01-09 04:10:29 -080035/* pm_up = true - UP, pm_up = false - DOWN */
36static bool pm_up;
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053037static uint32_t sgi = (uint32_t)INVALID_SGI;
Tejas Patel59c608a2019-01-09 04:10:29 -080038
Tanmay Shahfdae9e82022-08-26 15:06:00 -070039static void notify_os(void)
40{
41 int32_t cpu;
42 uint32_t reg;
43
44 cpu = plat_my_core_pos() + 1U;
45
46 reg = (cpu | (sgi << XSCUGIC_SGIR_EL1_INITID_SHIFT));
47 write_icc_asgi1r_el1(reg);
48}
49
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053050static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle,
51 void *cookie)
52{
Tanmay Shahfdae9e82022-08-26 15:06:00 -070053 uint32_t payload[4] = {0};
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +053054 enum pm_ret_status ret;
Tanmay Shahfdae9e82022-08-26 15:06:00 -070055
56 VERBOSE("Received IPI FIQ from firmware\n");
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060057
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053058 (void)plat_ic_acknowledge_interrupt();
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060059
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +053060 ret = pm_get_callbackdata(payload, ARRAY_SIZE(payload), 0, 0);
61 if (ret != PM_RET_SUCCESS) {
62 payload[0] = ret;
63 }
64
Tanmay Shahfdae9e82022-08-26 15:06:00 -070065 switch (payload[0]) {
66 case PM_INIT_SUSPEND_CB:
67 case PM_NOTIFY_CB:
68 if (sgi != INVALID_SGI) {
69 notify_os();
70 }
71 break;
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +053072 case PM_RET_ERROR_INVALID_CRC:
73 pm_ipi_irq_clear(primary_proc);
74 WARN("Invalid CRC in the payload\n");
75 break;
76
Tanmay Shahfdae9e82022-08-26 15:06:00 -070077 default:
78 pm_ipi_irq_clear(primary_proc);
79 WARN("Invalid IPI payload\n");
80 break;
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060081 }
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053082
83 /* Clear FIQ */
84 plat_ic_end_of_interrupt(id);
85
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060086 return 0;
87}
88
89/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +053090 * pm_register_sgi() - PM register the IPI interrupt.
91 * @sgi_num: SGI number to be used for communication.
92 * @reset: Reset to invalid SGI when reset=1.
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060093 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +053094 * Return: On success, the initialization function must return 0.
95 * Any other return value will cause the framework to ignore
96 * the service.
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060097 *
98 * Update the SGI number to be used.
99 *
100 */
Venkatesh Yadav Abbarapu2cefbcd2022-07-31 14:05:40 +0530101int32_t pm_register_sgi(uint32_t sgi_num, uint32_t reset)
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600102{
Venkatesh Yadav Abbarapuc8bbedc2021-04-19 07:49:57 -0600103 if (reset == 1U) {
104 sgi = INVALID_SGI;
105 return 0;
106 }
107
108 if (sgi != INVALID_SGI) {
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600109 return -EBUSY;
110 }
111
112 if (sgi_num >= GICV3_MAX_SGI_TARGETS) {
113 return -EINVAL;
114 }
115
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530116 sgi = (uint32_t)sgi_num;
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530117 return 0;
118}
119
Tejas Patel354fe572018-12-14 00:55:37 -0800120/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530121 * pm_setup() - PM service setup.
Tejas Patel354fe572018-12-14 00:55:37 -0800122 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530123 * Return: On success, the initialization function must return 0.
124 * Any other return value will cause the framework to ignore
125 * the service.
Tejas Patel354fe572018-12-14 00:55:37 -0800126 *
127 * Initialization functions for Versal power management for
128 * communicaton with PMC.
129 *
130 * Called from sip_svc_setup initialization function with the
131 * rt_svc_init signature.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530132 *
Tejas Patel354fe572018-12-14 00:55:37 -0800133 */
Venkatesh Yadav Abbarapu2cefbcd2022-07-31 14:05:40 +0530134int32_t pm_setup(void)
Tejas Patel354fe572018-12-14 00:55:37 -0800135{
HariBabu Gattemaa811712022-10-07 00:07:49 -0700136 int32_t ret = 0;
Tejas Patel354fe572018-12-14 00:55:37 -0800137
HariBabu Gattemaa811712022-10-07 00:07:49 -0700138 pm_ipi_init(primary_proc);
139 pm_up = true;
Tejas Patel354fe572018-12-14 00:55:37 -0800140
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530141 /*
142 * Enable IPI IRQ
143 * assume the rich OS is OK to handle callback IRQs now.
144 * Even if we were wrong, it would not enable the IRQ in
145 * the GIC.
146 */
147 pm_ipi_irq_enable(primary_proc);
148
149 ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler);
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700150 if (ret != 0) {
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +0530151 WARN("BL31: registering IPI interrupt failed\n");
152 }
Tanmay Shahfde084c2022-09-13 10:24:27 -0700153
154 gicd_write_irouter(gicv3_driver_data->gicd_base, PLAT_VERSAL_IPI_IRQ, MODE);
Tejas Patel354fe572018-12-14 00:55:37 -0800155 return ret;
156}
Tejas Patel59c608a2019-01-09 04:10:29 -0800157
158/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530159 * eemi_for_compatibility() - EEMI calls handler for deprecated calls.
160 * @api_id: identifier for the API being called.
161 * @pm_arg: pointer to the argument data for the API call.
162 * @handle: Pointer to caller's context structure.
163 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tejas Patel59c608a2019-01-09 04:10:29 -0800164 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530165 * Return: If EEMI API found then, uintptr_t type address, else 0.
Tejas Patel59c608a2019-01-09 04:10:29 -0800166 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700167 * Some EEMI API's use case needs to be changed in Linux driver, so they
168 * can take advantage of common EEMI handler in TF-A. As of now the old
169 * implementation of these APIs are required to maintain backward compatibility
170 * until their use case in linux driver changes.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530171 *
Tejas Patel59c608a2019-01-09 04:10:29 -0800172 */
Tanmay Shahff34daa2021-08-09 11:00:41 -0700173static uintptr_t eemi_for_compatibility(uint32_t api_id, uint32_t *pm_arg,
174 void *handle, uint32_t security_flag)
Tejas Patel59c608a2019-01-09 04:10:29 -0800175{
176 enum pm_ret_status ret;
177
Tanmay Shahff34daa2021-08-09 11:00:41 -0700178 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800179
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800180 case (uint32_t)PM_IOCTL:
Tejas Patel9141f442019-01-10 03:03:48 -0800181 {
Naman Patelb8434b12022-11-16 01:54:23 -0800182 uint32_t value = 0U;
Tejas Patel9141f442019-01-10 03:03:48 -0800183
184 ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600185 pm_arg[3], pm_arg[4],
186 &value, security_flag);
Tanmay Shahff34daa2021-08-09 11:00:41 -0700187 if (ret == PM_RET_ERROR_NOTSUPPORTED)
188 return (uintptr_t)0;
189
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530190 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32U);
Tejas Patel9141f442019-01-10 03:03:48 -0800191 }
192
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800193 case (uint32_t)PM_QUERY_DATA:
Tejas Patela3e34ad2019-02-01 17:25:19 +0530194 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700195 uint32_t data[PAYLOAD_ARG_CNT] = { 0 };
Tejas Patela3e34ad2019-02-01 17:25:19 +0530196
197 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
Michal Simek57cf55a2022-07-29 07:48:59 +0200198 pm_arg[3], data, security_flag);
Rajan Vaja030620d2020-11-23 04:13:54 -0800199
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530200 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)data[0] << 32U),
Michal Simek57cf55a2022-07-29 07:48:59 +0200201 (uint64_t)data[1] | ((uint64_t)data[2] << 32U));
Rajan Vaja030620d2020-11-23 04:13:54 -0800202 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800203
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800204 case (uint32_t)PM_FEATURE_CHECK:
Tejas Patel59c608a2019-01-09 04:10:29 -0800205 {
Ronak Jain70d1c582022-02-04 00:42:55 -0800206 uint32_t result[PAYLOAD_ARG_CNT] = {0U};
Tejas Patel59c608a2019-01-09 04:10:29 -0800207
Ronak Jain70d1c582022-02-04 00:42:55 -0800208 ret = pm_feature_check(pm_arg[0], result, security_flag);
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530209 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U),
210 (uint64_t)result[1] | ((uint64_t)result[2] << 32U));
Tejas Patel59c608a2019-01-09 04:10:29 -0800211 }
212
Tanmay Shahff34daa2021-08-09 11:00:41 -0700213 case PM_LOAD_PDI:
Tejas Patel59c608a2019-01-09 04:10:29 -0800214 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700215 ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
216 security_flag);
217 SMC_RET1(handle, (uint64_t)ret);
218 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800219
Tanmay Shahff34daa2021-08-09 11:00:41 -0700220 default:
221 return (uintptr_t)0;
Tejas Patel59c608a2019-01-09 04:10:29 -0800222 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700223}
Tejas Patel59c608a2019-01-09 04:10:29 -0800224
Tanmay Shahff34daa2021-08-09 11:00:41 -0700225/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530226 * eemi_psci_debugfs_handler() - EEMI API invoked from PSCI.
227 * @api_id: identifier for the API being called.
228 * @pm_arg: pointer to the argument data for the API call.
229 * @handle: Pointer to caller's context structure.
230 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700231 *
232 * These EEMI APIs performs CPU specific power management tasks.
233 * These EEMI APIs are invoked either from PSCI or from debugfs in kernel.
234 * These calls require CPU specific processing before sending IPI request to
235 * Platform Management Controller. For example enable/disable CPU specific
236 * interrupts. This requires separate handler for these calls and may not be
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530237 * handled using common eemi handler.
238 *
239 * Return: If EEMI API found then, uintptr_t type address, else 0.
240 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700241 */
242static uintptr_t eemi_psci_debugfs_handler(uint32_t api_id, uint32_t *pm_arg,
243 void *handle, uint32_t security_flag)
244{
245 enum pm_ret_status ret;
Tejas Patel59c608a2019-01-09 04:10:29 -0800246
Tanmay Shahff34daa2021-08-09 11:00:41 -0700247 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800248
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800249 case (uint32_t)PM_SELF_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700250 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
251 pm_arg[3], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530252 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800253
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800254 case (uint32_t)PM_FORCE_POWERDOWN:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700255 ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530256 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel84275bf2020-09-01 04:43:53 -0700257
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800258 case (uint32_t)PM_REQ_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700259 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
260 pm_arg[3], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530261 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel84275bf2020-09-01 04:43:53 -0700262
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800263 case (uint32_t)PM_ABORT_SUSPEND:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700264 ret = pm_abort_suspend(pm_arg[0], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530265 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800266
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800267 case (uint32_t)PM_SYSTEM_SHUTDOWN:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700268 ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
Venkatesh Yadav Abbarapud90e47b2022-07-28 08:50:30 +0530269 SMC_RET1(handle, (u_register_t)ret);
Tejas Patel59c608a2019-01-09 04:10:29 -0800270
Tanmay Shahff34daa2021-08-09 11:00:41 -0700271 default:
272 return (uintptr_t)0;
Tejas Patel59c608a2019-01-09 04:10:29 -0800273 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700274}
Tejas Patel59c608a2019-01-09 04:10:29 -0800275
Tanmay Shahff34daa2021-08-09 11:00:41 -0700276/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530277 * TF_A_specific_handler() - SMC handler for TF-A specific functionality.
278 * @api_id: identifier for the API being called.
279 * @pm_arg: pointer to the argument data for the API call.
280 * @handle: Pointer to caller's context structure.
281 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700282 *
283 * These EEMI calls performs functionality that does not require
284 * IPI transaction. The handler ends in TF-A and returns requested data to
Michal Simek509bfc72022-09-14 09:32:47 +0200285 * kernel from TF-A.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530286 *
287 * Return: If TF-A specific API found then, uintptr_t type address, else 0
288 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700289 */
290static uintptr_t TF_A_specific_handler(uint32_t api_id, uint32_t *pm_arg,
291 void *handle, uint32_t security_flag)
292{
293 switch (api_id) {
Tejas Patel59c608a2019-01-09 04:10:29 -0800294
Tanmay Shah0dde16c2021-12-14 04:53:40 -0800295 case TF_A_PM_REGISTER_SGI:
296 {
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530297 int32_t ret;
Tanmay Shah0dde16c2021-12-14 04:53:40 -0800298
299 ret = pm_register_sgi(pm_arg[0], pm_arg[1]);
300 if (ret != 0) {
301 SMC_RET1(handle, (uint32_t)PM_RET_ERROR_ARGS);
302 }
303
304 SMC_RET1(handle, (uint32_t)PM_RET_SUCCESS);
305 }
306
Tanmay Shahff34daa2021-08-09 11:00:41 -0700307 case PM_GET_CALLBACK_DATA:
Tejas Patel59c608a2019-01-09 04:10:29 -0800308 {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700309 uint32_t result[4] = {0};
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530310 enum pm_ret_status ret;
311
312 ret = pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag, 1U);
313 if (ret != 0) {
314 result[0] = ret;
315 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800316
Tanmay Shahff34daa2021-08-09 11:00:41 -0700317 SMC_RET2(handle,
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530318 (uint64_t)result[0] | ((uint64_t)result[1] << 32U),
319 (uint64_t)result[2] | ((uint64_t)result[3] << 32U));
Tejas Patel59c608a2019-01-09 04:10:29 -0800320 }
321
Tejas Patel044001d2019-01-21 17:56:48 +0530322 case PM_GET_TRUSTZONE_VERSION:
323 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
Jay Buddhabhattic3da9c02022-04-04 23:42:04 -0700324 ((uint64_t)TZ_VERSION << 32U));
Tejas Patel044001d2019-01-21 17:56:48 +0530325
Tanmay Shahff34daa2021-08-09 11:00:41 -0700326 default:
327 return (uintptr_t)0;
Ravi Patelbd4aa5a2019-08-12 03:17:54 -0700328 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700329}
Ravi Patelbd4aa5a2019-08-12 03:17:54 -0700330
Tanmay Shahff34daa2021-08-09 11:00:41 -0700331/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530332 * eemi_handler() - Prepare EEMI payload and perform IPI transaction.
333 * @api_id: identifier for the API being called.
334 * @pm_arg: pointer to the argument data for the API call.
335 * @handle: Pointer to caller's context structure.
336 * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700337 *
338 * EEMI - Embedded Energy Management Interface is Xilinx proprietary protocol
339 * to allow communication between power management controller and different
340 * processing clusters.
341 *
342 * This handler prepares EEMI protocol payload received from kernel and performs
343 * IPI transaction.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530344 *
345 * Return: If EEMI API found then, uintptr_t type address, else 0
346 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700347 */
348static uintptr_t eemi_handler(uint32_t api_id, uint32_t *pm_arg,
349 void *handle, uint32_t security_flag)
350{
351 enum pm_ret_status ret;
352 uint32_t buf[PAYLOAD_ARG_CNT] = {0};
Ravi Patel22b0b492019-03-06 12:34:46 +0530353
Tanmay Shahff34daa2021-08-09 11:00:41 -0700354 ret = pm_handle_eemi_call(security_flag, api_id, pm_arg[0], pm_arg[1],
355 pm_arg[2], pm_arg[3], pm_arg[4],
356 (uint64_t *)buf);
357 /*
358 * Two IOCTLs, to get clock name and pinctrl name of pm_query_data API
359 * receives 5 words of respoonse from firmware. Currently linux driver can
360 * receive only 4 words from TF-A. So, this needs to be handled separately
361 * than other eemi calls.
362 */
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800363 if (api_id == (uint32_t)PM_QUERY_DATA) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700364 if ((pm_arg[0] == XPM_QID_CLOCK_GET_NAME ||
365 pm_arg[0] == XPM_QID_PINCTRL_GET_FUNCTION_NAME) &&
366 ret == PM_RET_SUCCESS) {
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530367 SMC_RET2(handle, (uint64_t)buf[0] | ((uint64_t)buf[1] << 32U),
368 (uint64_t)buf[2] | ((uint64_t)buf[3] << 32U));
Tanmay Shahff34daa2021-08-09 11:00:41 -0700369 }
Ravi Patel22b0b492019-03-06 12:34:46 +0530370 }
371
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530372 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U),
373 (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U));
Tanmay Shahff34daa2021-08-09 11:00:41 -0700374}
Jolly Shahed05a712019-03-22 05:33:39 +0530375
Tanmay Shahff34daa2021-08-09 11:00:41 -0700376/**
377 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530378 * @smc_fid: Function Identifier.
379 * @x1: SMC64 Arguments from kernel.
380 * @x2: SMC64 Arguments from kernel.
381 * @x3: SMC64 Arguments from kernel (upper 32-bits).
382 * @x4: Unused.
383 * @cookie: Unused.
384 * @handle: Pointer to caller's context structure.
385 * @flags: SECURE_FLAG or NON_SECURE_FLAG.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700386 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530387 * Return: Unused.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700388 *
389 * Determines that smc_fid is valid and supported PM SMC Function ID from the
390 * list of pm_api_ids, otherwise completes the request with
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530391 * the unknown SMC Function ID.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700392 *
393 * The SMC calls for PM service are forwarded from SIP Service SMC handler
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530394 * function with rt_svc_handle signature.
395 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700396 */
397uint64_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 +0530398 uint64_t x4, const void *cookie, void *handle, uint64_t flags)
Tanmay Shahff34daa2021-08-09 11:00:41 -0700399{
400 uintptr_t ret;
401 uint32_t pm_arg[PAYLOAD_ARG_CNT] = {0};
402 uint32_t security_flag = SECURE_FLAG;
403 uint32_t api_id;
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700404
Tanmay Shahff34daa2021-08-09 11:00:41 -0700405 /* Handle case where PM wasn't initialized properly */
Michal Simek5e2f5962022-09-13 11:48:53 +0200406 if (pm_up == false) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700407 SMC_RET1(handle, SMC_UNK);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530408 }
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700409
Tanmay Shahff34daa2021-08-09 11:00:41 -0700410 /*
411 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as non-secure (1)
412 * if smc called is non secure
413 */
Michal Simek5e2f5962022-09-13 11:48:53 +0200414 if (is_caller_non_secure(flags) != 0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700415 security_flag = NON_SECURE_FLAG;
Tejas Patel5c154e12020-11-25 01:53:12 -0800416 }
417
Tanmay Shahff34daa2021-08-09 11:00:41 -0700418 pm_arg[0] = (uint32_t)x1;
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530419 pm_arg[1] = (uint32_t)(x1 >> 32U);
Tanmay Shahff34daa2021-08-09 11:00:41 -0700420 pm_arg[2] = (uint32_t)x2;
Venkatesh Yadav Abbarapu2d27cb92022-07-31 14:08:53 +0530421 pm_arg[3] = (uint32_t)(x2 >> 32U);
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600422 pm_arg[4] = (uint32_t)x3;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700423 (void)(x4);
424 api_id = smc_fid & FUNCID_NUM_MASK;
Tejas Patel42015552020-11-25 01:56:57 -0800425
Tanmay Shahff34daa2021-08-09 11:00:41 -0700426 ret = eemi_for_compatibility(api_id, pm_arg, handle, security_flag);
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530427 if (ret != (uintptr_t)0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700428 return ret;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530429 }
Tanmay Shahff34daa2021-08-09 11:00:41 -0700430
431 ret = eemi_psci_debugfs_handler(api_id, pm_arg, handle, flags);
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 = TF_A_specific_handler(api_id, pm_arg, handle, security_flag);
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 = eemi_handler(api_id, pm_arg, handle, security_flag);
442
443 return ret;
Tejas Patel59c608a2019-01-09 04:10:29 -0800444}