1a92681d9SJay Buddhabhatti /*
2a92681d9SJay Buddhabhatti * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
31c12cd10SMichal Simek * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
4a92681d9SJay Buddhabhatti *
5a92681d9SJay Buddhabhatti * SPDX-License-Identifier: BSD-3-Clause
6a92681d9SJay Buddhabhatti */
7a92681d9SJay Buddhabhatti
8a92681d9SJay Buddhabhatti /*
9a92681d9SJay Buddhabhatti * Top-level SMC handler for Versal power management calls and
10a92681d9SJay Buddhabhatti * IPI setup functions for communication with PMC.
11a92681d9SJay Buddhabhatti */
12a92681d9SJay Buddhabhatti
13a92681d9SJay Buddhabhatti #include <errno.h>
14a92681d9SJay Buddhabhatti #include <stdbool.h>
1501a326abSPrasad Kummari
1601a326abSPrasad Kummari #include "../drivers/arm/gic/v3/gicv3_private.h"
1701a326abSPrasad Kummari
184fd510e0SRonak Jain #include <common/ep_info.h>
19a92681d9SJay Buddhabhatti #include <common/runtime_svc.h>
2001a326abSPrasad Kummari #include <drivers/arm/gicv3.h>
21c3280df1SJay Buddhabhatti #include <lib/psci/psci.h>
22c3280df1SJay Buddhabhatti #include <plat/arm/common/plat_arm.h>
23a92681d9SJay Buddhabhatti #include <plat/common/platform.h>
2401a326abSPrasad Kummari
2501a326abSPrasad Kummari #include <plat_private.h>
26a92681d9SJay Buddhabhatti #include "pm_api_sys.h"
27a92681d9SJay Buddhabhatti #include "pm_client.h"
28a92681d9SJay Buddhabhatti #include "pm_ipi.h"
29cebb7cc1SJay Buddhabhatti #include "pm_svc_main.h"
30a92681d9SJay Buddhabhatti
31a92681d9SJay Buddhabhatti #define MODE 0x80000000U
32a92681d9SJay Buddhabhatti
33a92681d9SJay Buddhabhatti #define INVALID_SGI 0xFFU
34a92681d9SJay Buddhabhatti #define PM_INIT_SUSPEND_CB (30U)
35a92681d9SJay Buddhabhatti #define PM_NOTIFY_CB (32U)
363dd118cfSJay Buddhabhatti #define EVENT_CPU_PWRDWN (4U)
379a7f892eSTanmay Shah #define MBOX_SGI_SHARED_IPI (7U)
389a7f892eSTanmay Shah
394661c8f5SJay Buddhabhatti /**
404661c8f5SJay Buddhabhatti * upper_32_bits - return bits 32-63 of a number
414661c8f5SJay Buddhabhatti * @n: the number we're accessing
424661c8f5SJay Buddhabhatti */
434661c8f5SJay Buddhabhatti #define upper_32_bits(n) ((uint32_t)((n) >> 32U))
444661c8f5SJay Buddhabhatti
454661c8f5SJay Buddhabhatti /**
464661c8f5SJay Buddhabhatti * lower_32_bits - return bits 0-31 of a number
474661c8f5SJay Buddhabhatti * @n: the number we're accessing
484661c8f5SJay Buddhabhatti */
494661c8f5SJay Buddhabhatti #define lower_32_bits(n) ((uint32_t)((n) & 0xffffffffU))
504661c8f5SJay Buddhabhatti
514661c8f5SJay Buddhabhatti /**
524661c8f5SJay Buddhabhatti * EXTRACT_SMC_ARGS - extracts 32-bit payloads from 64-bit SMC arguments
534661c8f5SJay Buddhabhatti * @pm_arg: array of 32-bit payloads
544661c8f5SJay Buddhabhatti * @x: array of 64-bit SMC arguments
554661c8f5SJay Buddhabhatti */
564661c8f5SJay Buddhabhatti #define EXTRACT_ARGS(pm_arg, x) \
574661c8f5SJay Buddhabhatti for (uint32_t i = 0U; i < (PAYLOAD_ARG_CNT - 1U); i++) { \
584661c8f5SJay Buddhabhatti if ((i % 2U) != 0U) { \
594661c8f5SJay Buddhabhatti pm_arg[i] = lower_32_bits(x[(i / 2U) + 1U]); \
604661c8f5SJay Buddhabhatti } else { \
614661c8f5SJay Buddhabhatti pm_arg[i] = upper_32_bits(x[i / 2U]); \
624661c8f5SJay Buddhabhatti } \
634661c8f5SJay Buddhabhatti }
644661c8f5SJay Buddhabhatti
65c3280df1SJay Buddhabhatti /* 1 sec of wait timeout for secondary core down */
66c3280df1SJay Buddhabhatti #define PWRDWN_WAIT_TIMEOUT (1000U)
67a92681d9SJay Buddhabhatti
68a92681d9SJay Buddhabhatti /* pm_up = true - UP, pm_up = false - DOWN */
69a92681d9SJay Buddhabhatti static bool pm_up;
70a92681d9SJay Buddhabhatti static uint32_t sgi = (uint32_t)INVALID_SGI;
71c0719d21SDevanshi Chauhan static bool pwrdwn_req_received;
72c0719d21SDevanshi Chauhan
pm_pwrdwn_req_status(void)73c0719d21SDevanshi Chauhan bool pm_pwrdwn_req_status(void)
74c0719d21SDevanshi Chauhan {
75c0719d21SDevanshi Chauhan return pwrdwn_req_received;
76c0719d21SDevanshi Chauhan }
77a92681d9SJay Buddhabhatti
notify_os(void)78a92681d9SJay Buddhabhatti static void notify_os(void)
79a92681d9SJay Buddhabhatti {
8072eb16b7SDevanshi Chauhan Alpeshbhai plat_ic_raise_ns_sgi((int)sgi, read_mpidr_el1());
81a92681d9SJay Buddhabhatti }
82a92681d9SJay Buddhabhatti
cpu_pwrdwn_req_handler(uint32_t id,uint32_t flags,void * handle,void * cookie)83ade92a64SJay Buddhabhatti static uint64_t cpu_pwrdwn_req_handler(uint32_t id, uint32_t flags,
84ade92a64SJay Buddhabhatti void *handle, void *cookie)
85ade92a64SJay Buddhabhatti {
86d3bb350cSMaheedhar Bollapalli (void)id;
87d3bb350cSMaheedhar Bollapalli (void)flags;
88d3bb350cSMaheedhar Bollapalli (void)handle;
89d3bb350cSMaheedhar Bollapalli (void)cookie;
90ade92a64SJay Buddhabhatti uint32_t cpu_id = plat_my_core_pos();
91ade92a64SJay Buddhabhatti
92ade92a64SJay Buddhabhatti VERBOSE("Powering down CPU %d\n", cpu_id);
93ade92a64SJay Buddhabhatti
94ade92a64SJay Buddhabhatti /* Deactivate CPU power down SGI */
95ade92a64SJay Buddhabhatti plat_ic_end_of_interrupt(CPU_PWR_DOWN_REQ_INTR);
96ade92a64SJay Buddhabhatti
9772eb16b7SDevanshi Chauhan Alpeshbhai return (uint64_t)psci_cpu_off();
98ade92a64SJay Buddhabhatti }
99ade92a64SJay Buddhabhatti
100c3280df1SJay Buddhabhatti /**
101c3280df1SJay Buddhabhatti * raise_pwr_down_interrupt() - Callback function to raise SGI.
102c3280df1SJay Buddhabhatti * @mpidr: MPIDR for the target CPU.
103c3280df1SJay Buddhabhatti *
104c3280df1SJay Buddhabhatti * Raise SGI interrupt to trigger the CPU power down sequence on all the
105c3280df1SJay Buddhabhatti * online secondary cores.
106c3280df1SJay Buddhabhatti */
raise_pwr_down_interrupt(u_register_t mpidr)107c3280df1SJay Buddhabhatti static void raise_pwr_down_interrupt(u_register_t mpidr)
108c3280df1SJay Buddhabhatti {
10972eb16b7SDevanshi Chauhan Alpeshbhai plat_ic_raise_el3_sgi((int)CPU_PWR_DOWN_REQ_INTR, mpidr);
110c3280df1SJay Buddhabhatti }
111c3280df1SJay Buddhabhatti
request_cpu_pwrdwn(void)11288ee0816SJay Buddhabhatti void request_cpu_pwrdwn(void)
1133dd118cfSJay Buddhabhatti {
11472eb16b7SDevanshi Chauhan Alpeshbhai int ret;
115c3280df1SJay Buddhabhatti
1163dd118cfSJay Buddhabhatti VERBOSE("CPU power down request received\n");
117c3280df1SJay Buddhabhatti
118c3280df1SJay Buddhabhatti /* Send powerdown request to online secondary core(s) */
1193b802105SBoyan Karatotev ret = psci_stop_other_cores(plat_my_core_pos(), PWRDWN_WAIT_TIMEOUT,
1203b802105SBoyan Karatotev raise_pwr_down_interrupt);
12172eb16b7SDevanshi Chauhan Alpeshbhai if (ret != PSCI_E_SUCCESS) {
122c3280df1SJay Buddhabhatti ERROR("Failed to powerdown secondary core(s)\n");
123c3280df1SJay Buddhabhatti }
124c3280df1SJay Buddhabhatti
125c3280df1SJay Buddhabhatti /* Clear IPI IRQ */
1263dd118cfSJay Buddhabhatti pm_ipi_irq_clear(primary_proc);
127c3280df1SJay Buddhabhatti
128c3280df1SJay Buddhabhatti /* Deactivate IPI IRQ */
129c3280df1SJay Buddhabhatti plat_ic_end_of_interrupt(PLAT_VERSAL_IPI_IRQ);
1303dd118cfSJay Buddhabhatti }
1313dd118cfSJay Buddhabhatti
ipi_fiq_handler(uint32_t id,uint32_t flags,void * handle,void * cookie)132a92681d9SJay Buddhabhatti static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle,
133a92681d9SJay Buddhabhatti void *cookie)
134a92681d9SJay Buddhabhatti {
135d3bb350cSMaheedhar Bollapalli (void)flags;
136d3bb350cSMaheedhar Bollapalli (void)handle;
137d3bb350cSMaheedhar Bollapalli (void)cookie;
138a92681d9SJay Buddhabhatti uint32_t payload[4] = {0};
139a92681d9SJay Buddhabhatti enum pm_ret_status ret;
140bdba3c84SDevanshi Chauhan Alpeshbhai uint32_t ipi_status, i;
141a92681d9SJay Buddhabhatti
142a92681d9SJay Buddhabhatti VERBOSE("Received IPI FIQ from firmware\n");
143a92681d9SJay Buddhabhatti
1447ec53afaSJay Buddhabhatti console_flush();
145a92681d9SJay Buddhabhatti (void)plat_ic_acknowledge_interrupt();
146a92681d9SJay Buddhabhatti
1479a7f892eSTanmay Shah /* Check status register for each IPI except PMC */
148bdba3c84SDevanshi Chauhan Alpeshbhai for (i = IPI_ID_APU; i <= IPI_ID_5; i++) {
149bdba3c84SDevanshi Chauhan Alpeshbhai ipi_status = ipi_mb_enquire_status(IPI_ID_APU, i);
1509a7f892eSTanmay Shah
1519a7f892eSTanmay Shah /* If any agent other than PMC has generated IPI FIQ then send SGI to mbox driver */
152a5d5cb3cSDevanshi Chauhan Alpeshbhai if ((ipi_status & IPI_MB_STATUS_RECV_PENDING) != 0U) {
15372eb16b7SDevanshi Chauhan Alpeshbhai plat_ic_raise_ns_sgi((int)MBOX_SGI_SHARED_IPI, read_mpidr_el1());
1549a7f892eSTanmay Shah break;
1559a7f892eSTanmay Shah }
1569a7f892eSTanmay Shah }
1579a7f892eSTanmay Shah
1589a7f892eSTanmay Shah /* If PMC has not generated interrupt then end ISR */
1599a7f892eSTanmay Shah ipi_status = ipi_mb_enquire_status(IPI_ID_APU, IPI_ID_PMC);
160bdba3c84SDevanshi Chauhan Alpeshbhai if ((ipi_status & IPI_MB_STATUS_RECV_PENDING) == 0U) {
1619a7f892eSTanmay Shah plat_ic_end_of_interrupt(id);
162906d5892SNithin G goto exit_label;
1639a7f892eSTanmay Shah }
1649a7f892eSTanmay Shah
1659a7f892eSTanmay Shah /* Handle PMC case */
166a92681d9SJay Buddhabhatti ret = pm_get_callbackdata(payload, ARRAY_SIZE(payload), 0, 0);
167a92681d9SJay Buddhabhatti if (ret != PM_RET_SUCCESS) {
1683a1a2daeSMaheedhar Bollapalli payload[0] = (uint32_t)ret;
169a92681d9SJay Buddhabhatti }
170a92681d9SJay Buddhabhatti
171a92681d9SJay Buddhabhatti switch (payload[0]) {
172a92681d9SJay Buddhabhatti case PM_INIT_SUSPEND_CB:
1733dd118cfSJay Buddhabhatti if (sgi != INVALID_SGI) {
1743dd118cfSJay Buddhabhatti notify_os();
1753dd118cfSJay Buddhabhatti }
1763dd118cfSJay Buddhabhatti break;
177a92681d9SJay Buddhabhatti case PM_NOTIFY_CB:
178a92681d9SJay Buddhabhatti if (sgi != INVALID_SGI) {
179a6dd46aeSNaman Trivedi if ((payload[2] == EVENT_CPU_PWRDWN) &&
180a6dd46aeSNaman Trivedi (NODECLASS(payload[1]) == (uint32_t)XPM_NODECLASS_DEVICE)) {
1813dd118cfSJay Buddhabhatti if (pwrdwn_req_received) {
1823dd118cfSJay Buddhabhatti pwrdwn_req_received = false;
1833dd118cfSJay Buddhabhatti request_cpu_pwrdwn();
184c3280df1SJay Buddhabhatti (void)psci_cpu_off();
1853dd118cfSJay Buddhabhatti break;
1863dd118cfSJay Buddhabhatti } else {
1873dd118cfSJay Buddhabhatti pwrdwn_req_received = true;
1883dd118cfSJay Buddhabhatti }
1893dd118cfSJay Buddhabhatti }
190a92681d9SJay Buddhabhatti notify_os();
191fd44cc7eSDevanshi Chauhan Alpeshbhai } else {
192a6dd46aeSNaman Trivedi if ((payload[2] == EVENT_CPU_PWRDWN) &&
193a6dd46aeSNaman Trivedi (NODECLASS(payload[1]) == (uint32_t)XPM_NODECLASS_DEVICE)) {
194c3ffa4c5SJay Buddhabhatti request_cpu_pwrdwn();
195c3ffa4c5SJay Buddhabhatti (void)psci_cpu_off();
196a92681d9SJay Buddhabhatti }
197fd44cc7eSDevanshi Chauhan Alpeshbhai }
198a92681d9SJay Buddhabhatti break;
19972eb16b7SDevanshi Chauhan Alpeshbhai case (uint32_t)PM_RET_ERROR_INVALID_CRC:
200a92681d9SJay Buddhabhatti pm_ipi_irq_clear(primary_proc);
201a92681d9SJay Buddhabhatti WARN("Invalid CRC in the payload\n");
202a92681d9SJay Buddhabhatti break;
203a92681d9SJay Buddhabhatti
204a92681d9SJay Buddhabhatti default:
205a92681d9SJay Buddhabhatti pm_ipi_irq_clear(primary_proc);
206a92681d9SJay Buddhabhatti WARN("Invalid IPI payload\n");
207a92681d9SJay Buddhabhatti break;
208a92681d9SJay Buddhabhatti }
209a92681d9SJay Buddhabhatti
210a92681d9SJay Buddhabhatti /* Clear FIQ */
211a92681d9SJay Buddhabhatti plat_ic_end_of_interrupt(id);
212a92681d9SJay Buddhabhatti
213906d5892SNithin G exit_label:
214a92681d9SJay Buddhabhatti return 0;
215a92681d9SJay Buddhabhatti }
216a92681d9SJay Buddhabhatti
217a92681d9SJay Buddhabhatti /**
218de7ed953SPrasad Kummari * pm_register_sgi() - PM register the IPI interrupt.
219de7ed953SPrasad Kummari * @sgi_num: SGI number to be used for communication.
220de7ed953SPrasad Kummari * @reset: Reset to invalid SGI when reset=1.
221a92681d9SJay Buddhabhatti *
222de7ed953SPrasad Kummari * Return: On success, the initialization function must return 0.
223a92681d9SJay Buddhabhatti * Any other return value will cause the framework to ignore
224de7ed953SPrasad Kummari * the service.
225a92681d9SJay Buddhabhatti *
226a92681d9SJay Buddhabhatti * Update the SGI number to be used.
227a92681d9SJay Buddhabhatti *
228a92681d9SJay Buddhabhatti */
pm_register_sgi(uint32_t sgi_num,uint32_t reset)229a92681d9SJay Buddhabhatti int32_t pm_register_sgi(uint32_t sgi_num, uint32_t reset)
230a92681d9SJay Buddhabhatti {
231906d5892SNithin G int32_t ret = 0;
232906d5892SNithin G
233a92681d9SJay Buddhabhatti if (reset == 1U) {
234a92681d9SJay Buddhabhatti sgi = INVALID_SGI;
235906d5892SNithin G } else if (sgi != INVALID_SGI) {
236906d5892SNithin G ret = -EBUSY;
237906d5892SNithin G } else if (sgi_num >= GICV3_MAX_SGI_TARGETS) {
238906d5892SNithin G ret = -EINVAL;
239906d5892SNithin G } else {
240a92681d9SJay Buddhabhatti sgi = (uint32_t)sgi_num;
241906d5892SNithin G }
242906d5892SNithin G
243906d5892SNithin G return ret;
244a92681d9SJay Buddhabhatti }
245a92681d9SJay Buddhabhatti
246a92681d9SJay Buddhabhatti /**
247de7ed953SPrasad Kummari * pm_setup() - PM service setup.
248a92681d9SJay Buddhabhatti *
249de7ed953SPrasad Kummari * Return: On success, the initialization function must return 0.
250a92681d9SJay Buddhabhatti * Any other return value will cause the framework to ignore
251de7ed953SPrasad Kummari * the service.
252a92681d9SJay Buddhabhatti *
253a92681d9SJay Buddhabhatti * Initialization functions for Versal power management for
254a92681d9SJay Buddhabhatti * communicaton with PMC.
255a92681d9SJay Buddhabhatti *
256a92681d9SJay Buddhabhatti * Called from sip_svc_setup initialization function with the
257a92681d9SJay Buddhabhatti * rt_svc_init signature.
258de7ed953SPrasad Kummari *
259a92681d9SJay Buddhabhatti */
pm_setup(void)260a92681d9SJay Buddhabhatti int32_t pm_setup(void)
261a92681d9SJay Buddhabhatti {
262a92681d9SJay Buddhabhatti int32_t ret = 0;
263a92681d9SJay Buddhabhatti
264a92681d9SJay Buddhabhatti pm_ipi_init(primary_proc);
265a92681d9SJay Buddhabhatti pm_up = true;
266c0719d21SDevanshi Chauhan pwrdwn_req_received = false;
267a92681d9SJay Buddhabhatti
268ade92a64SJay Buddhabhatti /* register SGI handler for CPU power down request */
269ade92a64SJay Buddhabhatti ret = request_intr_type_el3(CPU_PWR_DOWN_REQ_INTR, cpu_pwrdwn_req_handler);
270ade92a64SJay Buddhabhatti if (ret != 0) {
271ade92a64SJay Buddhabhatti WARN("BL31: registering SGI interrupt failed\n");
272ade92a64SJay Buddhabhatti }
273ade92a64SJay Buddhabhatti
274a92681d9SJay Buddhabhatti /*
275a92681d9SJay Buddhabhatti * Enable IPI IRQ
276a92681d9SJay Buddhabhatti * assume the rich OS is OK to handle callback IRQs now.
277a92681d9SJay Buddhabhatti * Even if we were wrong, it would not enable the IRQ in
278a92681d9SJay Buddhabhatti * the GIC.
279a92681d9SJay Buddhabhatti */
280a92681d9SJay Buddhabhatti pm_ipi_irq_enable(primary_proc);
281a92681d9SJay Buddhabhatti
282a92681d9SJay Buddhabhatti ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler);
283a92681d9SJay Buddhabhatti if (ret != 0) {
284a92681d9SJay Buddhabhatti WARN("BL31: registering IPI interrupt failed\n");
285a92681d9SJay Buddhabhatti }
286a92681d9SJay Buddhabhatti
287a92681d9SJay Buddhabhatti gicd_write_irouter(gicv3_driver_data->gicd_base, PLAT_VERSAL_IPI_IRQ, MODE);
288a3b0a342SJay Buddhabhatti
289a3b0a342SJay Buddhabhatti /* Register for idle callback during force power down/restart */
2903a1a2daeSMaheedhar Bollapalli ret = (int32_t)pm_register_notifier(primary_proc->node_id, EVENT_CPU_PWRDWN,
2914fd510e0SRonak Jain 0x0U, 0x1U, SECURE);
292a3b0a342SJay Buddhabhatti if (ret != 0) {
293a3b0a342SJay Buddhabhatti WARN("BL31: registering idle callback for restart/force power down failed\n");
294a3b0a342SJay Buddhabhatti }
295a3b0a342SJay Buddhabhatti
296a92681d9SJay Buddhabhatti return ret;
297a92681d9SJay Buddhabhatti }
298a92681d9SJay Buddhabhatti
299a92681d9SJay Buddhabhatti /**
300de7ed953SPrasad Kummari * eemi_for_compatibility() - EEMI calls handler for deprecated calls.
301de7ed953SPrasad Kummari * @api_id: identifier for the API being called.
302de7ed953SPrasad Kummari * @pm_arg: pointer to the argument data for the API call.
303de7ed953SPrasad Kummari * @handle: Pointer to caller's context structure.
3044fd510e0SRonak Jain * @security_flag: SECURE or NON_SECURE.
305a92681d9SJay Buddhabhatti *
306de7ed953SPrasad Kummari * Return: If EEMI API found then, uintptr_t type address, else 0.
307a92681d9SJay Buddhabhatti *
308a92681d9SJay Buddhabhatti * Some EEMI API's use case needs to be changed in Linux driver, so they
309a92681d9SJay Buddhabhatti * can take advantage of common EEMI handler in TF-A. As of now the old
310a92681d9SJay Buddhabhatti * implementation of these APIs are required to maintain backward compatibility
311a92681d9SJay Buddhabhatti * until their use case in linux driver changes.
312de7ed953SPrasad Kummari *
313a92681d9SJay Buddhabhatti */
eemi_for_compatibility(uint32_t api_id,const uint32_t * pm_arg,void * handle,uint32_t security_flag)314cd60ab79SDevanshi Chauhan Alpeshbhai static uintptr_t eemi_for_compatibility(uint32_t api_id, const uint32_t *pm_arg,
315a92681d9SJay Buddhabhatti void *handle, uint32_t security_flag)
316a92681d9SJay Buddhabhatti {
317a92681d9SJay Buddhabhatti enum pm_ret_status ret;
318a92681d9SJay Buddhabhatti
319a92681d9SJay Buddhabhatti switch (api_id) {
320a92681d9SJay Buddhabhatti
321964e5592SJay Buddhabhatti case (uint32_t)PM_FEATURE_CHECK:
322a92681d9SJay Buddhabhatti {
32303fa6f42SJay Buddhabhatti uint32_t result[RET_PAYLOAD_ARG_CNT] = {0U};
324a92681d9SJay Buddhabhatti
325a92681d9SJay Buddhabhatti ret = pm_feature_check(pm_arg[0], result, security_flag);
326a92681d9SJay Buddhabhatti SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U),
327a92681d9SJay Buddhabhatti (uint64_t)result[1] | ((uint64_t)result[2] << 32U));
328a92681d9SJay Buddhabhatti }
329a92681d9SJay Buddhabhatti
330a92681d9SJay Buddhabhatti case PM_LOAD_PDI:
331a92681d9SJay Buddhabhatti {
332a92681d9SJay Buddhabhatti ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
333a92681d9SJay Buddhabhatti security_flag);
334a92681d9SJay Buddhabhatti SMC_RET1(handle, (uint64_t)ret);
335a92681d9SJay Buddhabhatti }
336a92681d9SJay Buddhabhatti
337a92681d9SJay Buddhabhatti default:
338a92681d9SJay Buddhabhatti return (uintptr_t)0;
339a92681d9SJay Buddhabhatti }
340a92681d9SJay Buddhabhatti }
341a92681d9SJay Buddhabhatti
342a92681d9SJay Buddhabhatti /**
343de7ed953SPrasad Kummari * eemi_psci_debugfs_handler() - EEMI API invoked from PSCI.
344de7ed953SPrasad Kummari * @api_id: identifier for the API being called.
345de7ed953SPrasad Kummari * @pm_arg: pointer to the argument data for the API call.
346de7ed953SPrasad Kummari * @handle: Pointer to caller's context structure.
3474fd510e0SRonak Jain * @security_flag: SECURE or NON_SECURE.
348a92681d9SJay Buddhabhatti *
349a92681d9SJay Buddhabhatti * These EEMI APIs performs CPU specific power management tasks.
350a92681d9SJay Buddhabhatti * These EEMI APIs are invoked either from PSCI or from debugfs in kernel.
351a92681d9SJay Buddhabhatti * These calls require CPU specific processing before sending IPI request to
352a92681d9SJay Buddhabhatti * Platform Management Controller. For example enable/disable CPU specific
353a92681d9SJay Buddhabhatti * interrupts. This requires separate handler for these calls and may not be
354de7ed953SPrasad Kummari * handled using common eemi handler.
355de7ed953SPrasad Kummari *
356de7ed953SPrasad Kummari * Return: If EEMI API found then, uintptr_t type address, else 0.
357de7ed953SPrasad Kummari *
358a92681d9SJay Buddhabhatti */
eemi_psci_debugfs_handler(uint32_t api_id,const uint32_t * pm_arg,void * handle,uint32_t security_flag)359cd60ab79SDevanshi Chauhan Alpeshbhai static uintptr_t eemi_psci_debugfs_handler(uint32_t api_id, const uint32_t *pm_arg,
360a92681d9SJay Buddhabhatti void *handle, uint32_t security_flag)
361a92681d9SJay Buddhabhatti {
362a92681d9SJay Buddhabhatti enum pm_ret_status ret;
363a92681d9SJay Buddhabhatti
364a92681d9SJay Buddhabhatti switch (api_id) {
365a92681d9SJay Buddhabhatti
366964e5592SJay Buddhabhatti case (uint32_t)PM_SELF_SUSPEND:
367a92681d9SJay Buddhabhatti ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
368a92681d9SJay Buddhabhatti pm_arg[3], security_flag);
369a92681d9SJay Buddhabhatti SMC_RET1(handle, (u_register_t)ret);
370a92681d9SJay Buddhabhatti
371964e5592SJay Buddhabhatti case (uint32_t)PM_FORCE_POWERDOWN:
37272eb16b7SDevanshi Chauhan Alpeshbhai ret = pm_force_powerdown(pm_arg[0], (uint8_t)pm_arg[1], security_flag);
373a92681d9SJay Buddhabhatti SMC_RET1(handle, (u_register_t)ret);
374a92681d9SJay Buddhabhatti
375964e5592SJay Buddhabhatti case (uint32_t)PM_SYSTEM_SHUTDOWN:
376a92681d9SJay Buddhabhatti ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
377a92681d9SJay Buddhabhatti SMC_RET1(handle, (u_register_t)ret);
378a92681d9SJay Buddhabhatti
379a92681d9SJay Buddhabhatti default:
380a92681d9SJay Buddhabhatti return (uintptr_t)0;
381a92681d9SJay Buddhabhatti }
382a92681d9SJay Buddhabhatti }
383a92681d9SJay Buddhabhatti
384a92681d9SJay Buddhabhatti /**
3851d4372c4SJay Buddhabhatti * tfa_clear_pm_state() - Reset TF-A-specific PM state.
3861d4372c4SJay Buddhabhatti *
3871d4372c4SJay Buddhabhatti * This function resets TF-A-specific state that may have been modified,
3881d4372c4SJay Buddhabhatti * such as during a kexec-based kernel reload. It resets the SGI number
3891d4372c4SJay Buddhabhatti * and the shutdown scope to its default value.
3901d4372c4SJay Buddhabhatti */
tfa_clear_pm_state(void)3911d4372c4SJay Buddhabhatti static enum pm_ret_status tfa_clear_pm_state(void)
3921d4372c4SJay Buddhabhatti {
3931d4372c4SJay Buddhabhatti /* Reset SGI number to default value(-1). */
3941d4372c4SJay Buddhabhatti sgi = (uint32_t)INVALID_SGI;
3951d4372c4SJay Buddhabhatti
3961d4372c4SJay Buddhabhatti /* Reset the shutdown scope to its default value(system). */
3971d4372c4SJay Buddhabhatti return pm_system_shutdown(XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY, XPM_SHUTDOWN_SUBTYPE_RST_SYSTEM,
3981d4372c4SJay Buddhabhatti 0U);
3991d4372c4SJay Buddhabhatti }
4001d4372c4SJay Buddhabhatti
4011d4372c4SJay Buddhabhatti /**
402de7ed953SPrasad Kummari * TF_A_specific_handler() - SMC handler for TF-A specific functionality.
403de7ed953SPrasad Kummari * @api_id: identifier for the API being called.
404de7ed953SPrasad Kummari * @pm_arg: pointer to the argument data for the API call.
405de7ed953SPrasad Kummari * @handle: Pointer to caller's context structure.
4064fd510e0SRonak Jain * @security_flag: SECURE or NON_SECURE.
407a92681d9SJay Buddhabhatti *
408a92681d9SJay Buddhabhatti * These EEMI calls performs functionality that does not require
409a92681d9SJay Buddhabhatti * IPI transaction. The handler ends in TF-A and returns requested data to
410a92681d9SJay Buddhabhatti * kernel from TF-A.
411de7ed953SPrasad Kummari *
412de7ed953SPrasad Kummari * Return: If TF-A specific API found then, uintptr_t type address, else 0
413de7ed953SPrasad Kummari *
414a92681d9SJay Buddhabhatti */
TF_A_specific_handler(uint32_t api_id,const uint32_t * pm_arg,void * handle,uint32_t security_flag)415cd60ab79SDevanshi Chauhan Alpeshbhai static uintptr_t TF_A_specific_handler(uint32_t api_id, const uint32_t *pm_arg,
416a92681d9SJay Buddhabhatti void *handle, uint32_t security_flag)
417a92681d9SJay Buddhabhatti {
418a92681d9SJay Buddhabhatti switch (api_id) {
419a92681d9SJay Buddhabhatti
4209a0f5d12SJay Buddhabhatti case TF_A_FEATURE_CHECK:
4219a0f5d12SJay Buddhabhatti {
4229a0f5d12SJay Buddhabhatti enum pm_ret_status ret;
4239a0f5d12SJay Buddhabhatti uint32_t result[PAYLOAD_ARG_CNT] = {0U};
4249a0f5d12SJay Buddhabhatti
425*e25fad87SDevanshi Chauhan ret = tfa_api_feature_check(pm_arg[0], result);
4269a0f5d12SJay Buddhabhatti SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U));
4279a0f5d12SJay Buddhabhatti }
4289a0f5d12SJay Buddhabhatti
429a92681d9SJay Buddhabhatti case TF_A_PM_REGISTER_SGI:
430a92681d9SJay Buddhabhatti {
431a92681d9SJay Buddhabhatti int32_t ret;
432a92681d9SJay Buddhabhatti
433a92681d9SJay Buddhabhatti ret = pm_register_sgi(pm_arg[0], pm_arg[1]);
434a92681d9SJay Buddhabhatti if (ret != 0) {
435a92681d9SJay Buddhabhatti SMC_RET1(handle, (uint32_t)PM_RET_ERROR_ARGS);
436a92681d9SJay Buddhabhatti }
437a92681d9SJay Buddhabhatti
438a92681d9SJay Buddhabhatti SMC_RET1(handle, (uint32_t)PM_RET_SUCCESS);
439a92681d9SJay Buddhabhatti }
440a92681d9SJay Buddhabhatti
441a92681d9SJay Buddhabhatti case PM_GET_CALLBACK_DATA:
442a92681d9SJay Buddhabhatti {
443a92681d9SJay Buddhabhatti uint32_t result[4] = {0};
444a92681d9SJay Buddhabhatti enum pm_ret_status ret;
445a92681d9SJay Buddhabhatti
446a92681d9SJay Buddhabhatti ret = pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag, 1U);
447bdba3c84SDevanshi Chauhan Alpeshbhai if (ret != PM_RET_SUCCESS) {
4483a1a2daeSMaheedhar Bollapalli result[0] = (uint32_t)ret;
449a92681d9SJay Buddhabhatti }
450a92681d9SJay Buddhabhatti
451a92681d9SJay Buddhabhatti SMC_RET2(handle,
452a92681d9SJay Buddhabhatti (uint64_t)result[0] | ((uint64_t)result[1] << 32U),
453a92681d9SJay Buddhabhatti (uint64_t)result[2] | ((uint64_t)result[3] << 32U));
454a92681d9SJay Buddhabhatti }
455a92681d9SJay Buddhabhatti
456a92681d9SJay Buddhabhatti case PM_GET_TRUSTZONE_VERSION:
457a92681d9SJay Buddhabhatti SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
458a92681d9SJay Buddhabhatti ((uint64_t)TZ_VERSION << 32U));
459a92681d9SJay Buddhabhatti
4601d4372c4SJay Buddhabhatti case TF_A_CLEAR_PM_STATE:
4611d4372c4SJay Buddhabhatti {
4621d4372c4SJay Buddhabhatti enum pm_ret_status ret;
4631d4372c4SJay Buddhabhatti
4641d4372c4SJay Buddhabhatti ret = tfa_clear_pm_state();
4651d4372c4SJay Buddhabhatti
4661d4372c4SJay Buddhabhatti SMC_RET1(handle, (uint64_t)ret);
4671d4372c4SJay Buddhabhatti }
4681d4372c4SJay Buddhabhatti
469a92681d9SJay Buddhabhatti default:
470a92681d9SJay Buddhabhatti return (uintptr_t)0;
471a92681d9SJay Buddhabhatti }
472a92681d9SJay Buddhabhatti }
473a92681d9SJay Buddhabhatti
474a92681d9SJay Buddhabhatti /**
475de7ed953SPrasad Kummari * eemi_handler() - Prepare EEMI payload and perform IPI transaction.
476de7ed953SPrasad Kummari * @api_id: identifier for the API being called.
477de7ed953SPrasad Kummari * @pm_arg: pointer to the argument data for the API call.
478de7ed953SPrasad Kummari * @handle: Pointer to caller's context structure.
4794fd510e0SRonak Jain * @security_flag: SECURE or NON_SECURE.
480a92681d9SJay Buddhabhatti *
481a92681d9SJay Buddhabhatti * EEMI - Embedded Energy Management Interface is Xilinx proprietary protocol
482a92681d9SJay Buddhabhatti * to allow communication between power management controller and different
483a92681d9SJay Buddhabhatti * processing clusters.
484a92681d9SJay Buddhabhatti *
485a92681d9SJay Buddhabhatti * This handler prepares EEMI protocol payload received from kernel and performs
486a92681d9SJay Buddhabhatti * IPI transaction.
487de7ed953SPrasad Kummari *
488de7ed953SPrasad Kummari * Return: If EEMI API found then, uintptr_t type address, else 0
489de7ed953SPrasad Kummari *
490a92681d9SJay Buddhabhatti */
eemi_handler(uint32_t api_id,const uint32_t * pm_arg,void * handle,uint32_t security_flag)491cd60ab79SDevanshi Chauhan Alpeshbhai static uintptr_t eemi_handler(uint32_t api_id, const uint32_t *pm_arg,
492a92681d9SJay Buddhabhatti void *handle, uint32_t security_flag)
493a92681d9SJay Buddhabhatti {
494a92681d9SJay Buddhabhatti enum pm_ret_status ret;
49503fa6f42SJay Buddhabhatti uint32_t buf[RET_PAYLOAD_ARG_CNT] = {0};
496a92681d9SJay Buddhabhatti
497a92681d9SJay Buddhabhatti ret = pm_handle_eemi_call(security_flag, api_id, pm_arg[0], pm_arg[1],
498c35fe294SDevanshi Chauhan Alpeshbhai pm_arg[2], pm_arg[3], pm_arg[4], buf);
499a92681d9SJay Buddhabhatti /*
500a92681d9SJay Buddhabhatti * Two IOCTLs, to get clock name and pinctrl name of pm_query_data API
501a92681d9SJay Buddhabhatti * receives 5 words of respoonse from firmware. Currently linux driver can
502a92681d9SJay Buddhabhatti * receive only 4 words from TF-A. So, this needs to be handled separately
503a92681d9SJay Buddhabhatti * than other eemi calls.
504a92681d9SJay Buddhabhatti */
505964e5592SJay Buddhabhatti if (api_id == (uint32_t)PM_QUERY_DATA) {
50683bcef3fSMaheedhar Bollapalli if (((pm_arg[0] == (uint32_t)XPM_QID_CLOCK_GET_NAME) ||
50783bcef3fSMaheedhar Bollapalli (pm_arg[0] == (uint32_t)XPM_QID_PINCTRL_GET_FUNCTION_NAME)) &&
5088e9a5a51SNithin G (ret == PM_RET_SUCCESS)) {
509a92681d9SJay Buddhabhatti SMC_RET2(handle, (uint64_t)buf[0] | ((uint64_t)buf[1] << 32U),
510a92681d9SJay Buddhabhatti (uint64_t)buf[2] | ((uint64_t)buf[3] << 32U));
511a92681d9SJay Buddhabhatti }
512a92681d9SJay Buddhabhatti }
513a92681d9SJay Buddhabhatti
514a92681d9SJay Buddhabhatti SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U),
515a92681d9SJay Buddhabhatti (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U));
516a92681d9SJay Buddhabhatti }
517a92681d9SJay Buddhabhatti
518a92681d9SJay Buddhabhatti /**
5194661c8f5SJay Buddhabhatti * eemi_api_handler() - Prepare EEMI payload and perform IPI transaction.
5204661c8f5SJay Buddhabhatti * @api_id: identifier for the API being called.
5214661c8f5SJay Buddhabhatti * @pm_arg: pointer to the argument data for the API call.
5224661c8f5SJay Buddhabhatti * @handle: Pointer to caller's context structure.
5234fd510e0SRonak Jain * @security_flag: SECURE or NON_SECURE.
5244661c8f5SJay Buddhabhatti *
5254661c8f5SJay Buddhabhatti * EEMI - Embedded Energy Management Interface is AMD-Xilinx proprietary
5264661c8f5SJay Buddhabhatti * protocol to allow communication between power management controller and
5274661c8f5SJay Buddhabhatti * different processing clusters.
5284661c8f5SJay Buddhabhatti *
5294661c8f5SJay Buddhabhatti * This handler prepares EEMI protocol payload received from kernel and performs
5304661c8f5SJay Buddhabhatti * IPI transaction.
5314661c8f5SJay Buddhabhatti *
5324661c8f5SJay Buddhabhatti * Return: If EEMI API found then, uintptr_t type address, else 0
5334661c8f5SJay Buddhabhatti */
eemi_api_handler(uint32_t api_id,const uint32_t * pm_arg,void * handle,uint32_t security_flag)5344661c8f5SJay Buddhabhatti static uintptr_t eemi_api_handler(uint32_t api_id, const uint32_t *pm_arg,
5354661c8f5SJay Buddhabhatti void *handle, uint32_t security_flag)
5364661c8f5SJay Buddhabhatti {
5374661c8f5SJay Buddhabhatti enum pm_ret_status ret;
538e27b9491SJay Buddhabhatti uint32_t buf[RET_PAYLOAD_ARG_CNT] = {0U};
539e27b9491SJay Buddhabhatti uint32_t payload[PAYLOAD_ARG_CNT] = {0U};
5404661c8f5SJay Buddhabhatti uint32_t module_id;
5414661c8f5SJay Buddhabhatti
5424661c8f5SJay Buddhabhatti module_id = (api_id & MODULE_ID_MASK) >> 8U;
5434661c8f5SJay Buddhabhatti
5444661c8f5SJay Buddhabhatti PM_PACK_PAYLOAD7(payload, module_id, security_flag, api_id,
5454661c8f5SJay Buddhabhatti pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3],
5464661c8f5SJay Buddhabhatti pm_arg[4], pm_arg[5]);
5474661c8f5SJay Buddhabhatti
5484661c8f5SJay Buddhabhatti ret = pm_ipi_send_sync(primary_proc, payload, (uint32_t *)buf,
549e27b9491SJay Buddhabhatti RET_PAYLOAD_ARG_CNT);
5504661c8f5SJay Buddhabhatti
5514661c8f5SJay Buddhabhatti SMC_RET4(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U),
5524661c8f5SJay Buddhabhatti (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U),
5534661c8f5SJay Buddhabhatti (uint64_t)buf[3] | ((uint64_t)buf[4] << 32U),
5544661c8f5SJay Buddhabhatti (uint64_t)buf[5]);
5554661c8f5SJay Buddhabhatti }
5564661c8f5SJay Buddhabhatti
5574661c8f5SJay Buddhabhatti /**
558a92681d9SJay Buddhabhatti * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
559de7ed953SPrasad Kummari * @smc_fid: Function Identifier.
560de7ed953SPrasad Kummari * @x1: SMC64 Arguments from kernel.
561de7ed953SPrasad Kummari * @x2: SMC64 Arguments from kernel.
562de7ed953SPrasad Kummari * @x3: SMC64 Arguments from kernel (upper 32-bits).
563de7ed953SPrasad Kummari * @x4: Unused.
564de7ed953SPrasad Kummari * @cookie: Unused.
565de7ed953SPrasad Kummari * @handle: Pointer to caller's context structure.
5664fd510e0SRonak Jain * @flags: SECURE or NON_SECURE.
567a92681d9SJay Buddhabhatti *
568de7ed953SPrasad Kummari * Return: Unused.
569a92681d9SJay Buddhabhatti *
570a92681d9SJay Buddhabhatti * Determines that smc_fid is valid and supported PM SMC Function ID from the
571a92681d9SJay Buddhabhatti * list of pm_api_ids, otherwise completes the request with
572de7ed953SPrasad Kummari * the unknown SMC Function ID.
573a92681d9SJay Buddhabhatti *
574a92681d9SJay Buddhabhatti * The SMC calls for PM service are forwarded from SIP Service SMC handler
575de7ed953SPrasad Kummari * function with rt_svc_handle signature.
576de7ed953SPrasad Kummari *
577a92681d9SJay Buddhabhatti */
pm_smc_handler(uint32_t smc_fid,uint64_t x1,uint64_t x2,uint64_t x3,uint64_t x4,const void * cookie,void * handle,uint64_t flags)578a92681d9SJay Buddhabhatti uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
579a92681d9SJay Buddhabhatti uint64_t x4, const void *cookie, void *handle, uint64_t flags)
580a92681d9SJay Buddhabhatti {
581d3bb350cSMaheedhar Bollapalli (void)cookie;
582a92681d9SJay Buddhabhatti uintptr_t ret;
583a92681d9SJay Buddhabhatti uint32_t pm_arg[PAYLOAD_ARG_CNT] = {0};
5844fd510e0SRonak Jain uint32_t security_flag = NON_SECURE;
585a92681d9SJay Buddhabhatti uint32_t api_id;
586cebb7cc1SJay Buddhabhatti bool status = false, status_tmp = false;
5870f9f5575SMaheedhar Bollapalli const uint64_t x[4] = {x1, x2, x3, x4};
588a92681d9SJay Buddhabhatti
589a92681d9SJay Buddhabhatti /* Handle case where PM wasn't initialized properly */
590a92681d9SJay Buddhabhatti if (pm_up == false) {
591a92681d9SJay Buddhabhatti SMC_RET1(handle, SMC_UNK);
592a92681d9SJay Buddhabhatti }
593a92681d9SJay Buddhabhatti
594a92681d9SJay Buddhabhatti /*
595e8efb65aSJay Buddhabhatti * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as secure (0)
596e8efb65aSJay Buddhabhatti * if smc called is secure
597cebb7cc1SJay Buddhabhatti *
598cebb7cc1SJay Buddhabhatti * Add redundant macro call to immune the code from glitches
599a92681d9SJay Buddhabhatti */
600cebb7cc1SJay Buddhabhatti SECURE_REDUNDANT_CALL(status, status_tmp, is_caller_secure, flags);
601cebb7cc1SJay Buddhabhatti if ((status != false) && (status_tmp != false)) {
6024fd510e0SRonak Jain security_flag = SECURE;
603a92681d9SJay Buddhabhatti }
604a92681d9SJay Buddhabhatti
6054661c8f5SJay Buddhabhatti if ((smc_fid & FUNCID_NUM_MASK) == PASS_THROUGH_FW_CMD_ID) {
6064661c8f5SJay Buddhabhatti api_id = lower_32_bits(x[0]);
6074661c8f5SJay Buddhabhatti
6084661c8f5SJay Buddhabhatti EXTRACT_ARGS(pm_arg, x);
6094661c8f5SJay Buddhabhatti
6104661c8f5SJay Buddhabhatti return eemi_api_handler(api_id, pm_arg, handle, security_flag);
6114661c8f5SJay Buddhabhatti }
6124661c8f5SJay Buddhabhatti
613a92681d9SJay Buddhabhatti pm_arg[0] = (uint32_t)x1;
614a92681d9SJay Buddhabhatti pm_arg[1] = (uint32_t)(x1 >> 32U);
615a92681d9SJay Buddhabhatti pm_arg[2] = (uint32_t)x2;
616a92681d9SJay Buddhabhatti pm_arg[3] = (uint32_t)(x2 >> 32U);
617a92681d9SJay Buddhabhatti pm_arg[4] = (uint32_t)x3;
618a92681d9SJay Buddhabhatti (void)(x4);
619a92681d9SJay Buddhabhatti api_id = smc_fid & FUNCID_NUM_MASK;
620a92681d9SJay Buddhabhatti
621a92681d9SJay Buddhabhatti ret = eemi_for_compatibility(api_id, pm_arg, handle, security_flag);
622a92681d9SJay Buddhabhatti if (ret != (uintptr_t)0) {
623a92681d9SJay Buddhabhatti return ret;
624a92681d9SJay Buddhabhatti }
625a92681d9SJay Buddhabhatti
62672eb16b7SDevanshi Chauhan Alpeshbhai ret = eemi_psci_debugfs_handler(api_id, pm_arg, handle,
62772eb16b7SDevanshi Chauhan Alpeshbhai (uint32_t)flags);
628a92681d9SJay Buddhabhatti if (ret != (uintptr_t)0) {
629a92681d9SJay Buddhabhatti return ret;
630a92681d9SJay Buddhabhatti }
631a92681d9SJay Buddhabhatti
632a92681d9SJay Buddhabhatti ret = TF_A_specific_handler(api_id, pm_arg, handle, security_flag);
633a92681d9SJay Buddhabhatti if (ret != (uintptr_t)0) {
634a92681d9SJay Buddhabhatti return ret;
635a92681d9SJay Buddhabhatti }
636a92681d9SJay Buddhabhatti
637a92681d9SJay Buddhabhatti ret = eemi_handler(api_id, pm_arg, handle, security_flag);
638a92681d9SJay Buddhabhatti
639a92681d9SJay Buddhabhatti return ret;
640a92681d9SJay Buddhabhatti }
641