1c97857dbSAmit Nagal /*
2c97857dbSAmit Nagal * Copyright (c) 2018-2019, Arm Limited and Contributors. All rights reserved.
3c97857dbSAmit Nagal * Copyright (c) 2018-2022, Xilinx, Inc. All rights reserved.
40cc5e210SSenthil Nathan Thangaraj * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
5c97857dbSAmit Nagal *
6c97857dbSAmit Nagal * SPDX-License-Identifier: BSD-3-Clause
7c97857dbSAmit Nagal */
8c97857dbSAmit Nagal
9c97857dbSAmit Nagal /* Top level SMC handler for SiP calls. Dispatch PM calls to PM SMC handler. */
10c97857dbSAmit Nagal
11c97857dbSAmit Nagal #include <errno.h>
12c97857dbSAmit Nagal #include <inttypes.h>
13c97857dbSAmit Nagal
14c97857dbSAmit Nagal #include <common/debug.h>
15c97857dbSAmit Nagal #include <common/runtime_svc.h>
16c97857dbSAmit Nagal #include <drivers/scmi-msg.h>
17c97857dbSAmit Nagal #include <scmi.h>
18c97857dbSAmit Nagal #include <tools_share/uuid.h>
19c97857dbSAmit Nagal
20*4efae6abSPrasad Kummari #include <custom_svc.h>
21c97857dbSAmit Nagal #include "ipi_mailbox_svc.h"
22c97857dbSAmit Nagal #include "plat_private.h"
23c97857dbSAmit Nagal #include "pm_svc_main.h"
24c97857dbSAmit Nagal
25c97857dbSAmit Nagal /* SMC function IDs for SiP Service queries */
26c97857dbSAmit Nagal #define SIP_SVC_UID (0x8200ff01U)
27c97857dbSAmit Nagal #define SIP_SVC_VERSION (0x8200ff03U)
28c97857dbSAmit Nagal
29c97857dbSAmit Nagal /* SiP Service Calls version numbers */
30c97857dbSAmit Nagal #define SIP_SVC_VERSION_MAJOR (0U)
310cc5e210SSenthil Nathan Thangaraj #define SIP_SVC_VERSION_MINOR (2U)
32c97857dbSAmit Nagal
33c97857dbSAmit Nagal /* These macros are used to identify PM calls from the SMC function ID */
34c97857dbSAmit Nagal #define SIP_FID_MASK GENMASK(23, 16)
35c97857dbSAmit Nagal #define XLNX_FID_MASK GENMASK(23, 12)
36c97857dbSAmit Nagal #define PM_FID_VALUE 0u
37c97857dbSAmit Nagal #define IPI_FID_VALUE 0x1000u
38c97857dbSAmit Nagal #define is_pm_fid(_fid) (((_fid) & XLNX_FID_MASK) == PM_FID_VALUE)
39c97857dbSAmit Nagal #define is_ipi_fid(_fid) (((_fid) & XLNX_FID_MASK) == IPI_FID_VALUE)
40c97857dbSAmit Nagal
41c97857dbSAmit Nagal /* SiP Service UUID */
42c97857dbSAmit Nagal DEFINE_SVC_UUID2(_sip_uuid,
43c97857dbSAmit Nagal 0x0499eb70, 0x5ed0, 0x11ee, 0xb3, 0x0a,
44c97857dbSAmit Nagal 0x87, 0xd1, 0x1d, 0x4f, 0x8a, 0x9b);
45c97857dbSAmit Nagal
46c97857dbSAmit Nagal /**
47c97857dbSAmit Nagal * sip_svc_setup() - Setup SiP Service
48c97857dbSAmit Nagal *
49c97857dbSAmit Nagal * Return: 0 on success, negative error code on failure.
50c97857dbSAmit Nagal *
51c97857dbSAmit Nagal */
sip_svc_setup(void)52c97857dbSAmit Nagal static int32_t sip_svc_setup(void)
53c97857dbSAmit Nagal {
54c97857dbSAmit Nagal return sip_svc_setup_init();
55c97857dbSAmit Nagal }
56c97857dbSAmit Nagal
57c97857dbSAmit Nagal /*
58c97857dbSAmit Nagal * sip_svc_smc_handler() - Top-level SiP Service SMC handler
59c97857dbSAmit Nagal *
60c97857dbSAmit Nagal * Handler for all SiP SMC calls. Handles standard SIP requests
61c97857dbSAmit Nagal * and calls PM SMC handler if the call is for a PM-API function.
62c97857dbSAmit Nagal */
sip_svc_smc_handler(uint32_t smc_fid,u_register_t x1,u_register_t x2,u_register_t x3,u_register_t x4,void * cookie,void * handle,u_register_t flags)63c97857dbSAmit Nagal static uintptr_t sip_svc_smc_handler(uint32_t smc_fid,
64c97857dbSAmit Nagal u_register_t x1,
65c97857dbSAmit Nagal u_register_t x2,
66c97857dbSAmit Nagal u_register_t x3,
67c97857dbSAmit Nagal u_register_t x4,
68c97857dbSAmit Nagal void *cookie,
69c97857dbSAmit Nagal void *handle,
70c97857dbSAmit Nagal u_register_t flags)
71c97857dbSAmit Nagal {
72c97857dbSAmit Nagal VERBOSE("SMCID: 0x%08x, x1: 0x%016" PRIx64 ", x2: 0x%016" PRIx64 ", x3: 0x%016" PRIx64 ", x4: 0x%016" PRIx64 "\n",
73c97857dbSAmit Nagal smc_fid, x1, x2, x3, x4);
74c97857dbSAmit Nagal
75fbc415d2SMaheedhar Bollapalli if ((smc_fid & SIP_FID_MASK) != 0U) {
76c97857dbSAmit Nagal WARN("SMC out of SiP assinged range: 0x%x\n", smc_fid);
77c97857dbSAmit Nagal SMC_RET1(handle, SMC_UNK);
78c97857dbSAmit Nagal }
79c97857dbSAmit Nagal
80c97857dbSAmit Nagal /* Let PM SMC handler deal with PM-related requests */
81c97857dbSAmit Nagal if (is_pm_fid(smc_fid)) {
82c97857dbSAmit Nagal return smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags);
83c97857dbSAmit Nagal }
84c97857dbSAmit Nagal
85c97857dbSAmit Nagal /* Let IPI SMC handler deal with IPI-related requests if platform */
86c97857dbSAmit Nagal if (is_ipi_fid(smc_fid)) {
87c97857dbSAmit Nagal return ipi_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags);
88c97857dbSAmit Nagal }
89c97857dbSAmit Nagal
90c97857dbSAmit Nagal /* Let PM SMC handler deal with PM-related requests */
91c97857dbSAmit Nagal switch (smc_fid) {
92c97857dbSAmit Nagal case SIP_SVC_UID:
93c97857dbSAmit Nagal SMC_UUID_RET(handle, _sip_uuid);
94c97857dbSAmit Nagal
95c97857dbSAmit Nagal case SIP_SVC_VERSION:
96c97857dbSAmit Nagal SMC_RET2(handle, SIP_SVC_VERSION_MAJOR, SIP_SVC_VERSION_MINOR);
97c97857dbSAmit Nagal
98d75ff915SPrasad Kummari #if (TFA_NO_PM == 1)
99c97857dbSAmit Nagal case SIP_SCMI:
100c97857dbSAmit Nagal if (platform_id != EMU) {
101c97857dbSAmit Nagal scmi_smt_fastcall_smc_entry(0);
102c97857dbSAmit Nagal SMC_RET1(handle, 0);
103c97857dbSAmit Nagal }
104c97857dbSAmit Nagal WARN("SCMI is not working on EMU\n");
105c97857dbSAmit Nagal SMC_RET1(handle, SMC_UNK);
106d75ff915SPrasad Kummari #endif
107*4efae6abSPrasad Kummari case SOC_SIP_SVC_CUSTOM:
108*4efae6abSPrasad Kummari case SOC_SIP_SVC64_CUSTOM:
109*4efae6abSPrasad Kummari return custom_smc_handler(smc_fid, x1, x2, x3, x4,
110*4efae6abSPrasad Kummari cookie, handle, flags);
111c97857dbSAmit Nagal default:
112c97857dbSAmit Nagal WARN("Unimplemented SiP Service Call: 0x%x\n", smc_fid);
113c97857dbSAmit Nagal SMC_RET1(handle, SMC_UNK);
114c97857dbSAmit Nagal }
115c97857dbSAmit Nagal }
116c97857dbSAmit Nagal
117c97857dbSAmit Nagal /* Register PM Service Calls as runtime service */
118c97857dbSAmit Nagal DECLARE_RT_SVC(
119c97857dbSAmit Nagal sip_svc,
120c97857dbSAmit Nagal OEN_SIP_START,
121c97857dbSAmit Nagal OEN_SIP_END,
122c97857dbSAmit Nagal SMC_TYPE_FAST,
123c97857dbSAmit Nagal sip_svc_setup,
124c97857dbSAmit Nagal sip_svc_smc_handler);
125