blob: ecd8d082da6a4b5eee984615c4e3df661112be61 [file] [log] [blame]
Tejas Patel9d09ff92019-01-08 01:46:35 -08001/*
Tanmay Shahff34daa2021-08-09 11:00:41 -07002 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
Tanmay Shahfdae9e82022-08-26 15:06:00 -07003 * Copyright (c) 2022, Advanced Micro Devices, Inc. All rights reserved.
Tejas Patel9d09ff92019-01-08 01:46:35 -08004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8/*
9 * Versal system level PM-API functions and communication with PMC via
10 * IPI interrupts
11 */
12
13#include <pm_common.h>
14#include <pm_ipi.h>
Tejas Patelfe0e10a2019-12-08 23:29:44 -080015#include <plat/common/platform.h>
Tejas Patel9d09ff92019-01-08 01:46:35 -080016#include "pm_api_sys.h"
17#include "pm_client.h"
Rajan Vaja030620d2020-11-23 04:13:54 -080018#include "pm_defs.h"
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053019#include "pm_svc_main.h"
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060020#include "../drivers/arm/gic/v3/gicv3_private.h"
Tejas Patel9d09ff92019-01-08 01:46:35 -080021
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080022/* default shutdown/reboot scope is system(2) */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053023static uint32_t pm_shutdown_scope = XPM_SHUTDOWN_SUBTYPE_RST_SYSTEM;
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080024
25/**
26 * pm_get_shutdown_scope() - Get the currently set shutdown scope
27 *
28 * @return Shutdown scope value
29 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053030uint32_t pm_get_shutdown_scope(void)
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080031{
32 return pm_shutdown_scope;
33}
34
Tejas Patel9d09ff92019-01-08 01:46:35 -080035/* PM API functions */
36
37/**
Tanmay Shahff34daa2021-08-09 11:00:41 -070038 * pm_handle_eemi_call() - PM call for processor to send eemi payload
Tejas Patel18072da2021-02-25 20:16:56 -080039 * @flag 0 - Call from secure source
40 * 1 - Call from non-secure source
Tanmay Shahff34daa2021-08-09 11:00:41 -070041 * @x0 to x5 Arguments received per SMC64 standard
42 * @result Payload received from firmware
Tejas Patel9d09ff92019-01-08 01:46:35 -080043 *
Tanmay Shahff34daa2021-08-09 11:00:41 -070044 * @return PM_RET_SUCCESS on success or error code
Tejas Patel9d09ff92019-01-08 01:46:35 -080045 */
Tanmay Shahff34daa2021-08-09 11:00:41 -070046enum pm_ret_status pm_handle_eemi_call(uint32_t flag, uint32_t x0, uint32_t x1,
47 uint32_t x2, uint32_t x3, uint32_t x4,
48 uint32_t x5, uint64_t *result)
Tejas Patel9d09ff92019-01-08 01:46:35 -080049{
Tanmay Shahff34daa2021-08-09 11:00:41 -070050 uint32_t payload[PAYLOAD_ARG_CNT] = {0};
51 uint32_t module_id;
Tejas Patel9d09ff92019-01-08 01:46:35 -080052
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +053053 module_id = (x0 & MODULE_ID_MASK) >> 8U;
Tejas Patelfe0e10a2019-12-08 23:29:44 -080054
Tanmay Shahff34daa2021-08-09 11:00:41 -070055 //default module id is for LIBPM
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +053056 if (module_id == 0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -070057 module_id = LIBPM_MODULE_ID;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +053058 }
Ravi Patel476b5b12019-08-12 03:10:10 -070059
Tanmay Shahff34daa2021-08-09 11:00:41 -070060 PM_PACK_PAYLOAD6(payload, module_id, flag, x0, x1, x2, x3, x4, x5);
61 return pm_ipi_send_sync(primary_proc, payload, (uint32_t *)result, PAYLOAD_ARG_CNT);
Ravi Patel476b5b12019-08-12 03:10:10 -070062}
63
64/**
Tejas Patelfe0e10a2019-12-08 23:29:44 -080065 * pm_self_suspend() - PM call for processor to suspend itself
66 * @nid Node id of the processor or subsystem
67 * @latency Requested maximum wakeup latency (not supported)
68 * @state Requested state
69 * @address Resume address
Tejas Patel18072da2021-02-25 20:16:56 -080070 * @flag 0 - Call from secure source
71 * 1 - Call from non-secure source
Tejas Patelfe0e10a2019-12-08 23:29:44 -080072 *
73 * This is a blocking call, it will return only once PMU has responded.
74 * On a wakeup, resume address will be automatically set by PMU.
75 *
76 * @return Returns status, either success or error+reason
77 */
78enum pm_ret_status pm_self_suspend(uint32_t nid,
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053079 uint32_t latency,
80 uint32_t state,
Tejas Patel18072da2021-02-25 20:16:56 -080081 uintptr_t address, uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -080082{
83 uint32_t payload[PAYLOAD_ARG_CNT];
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053084 uint32_t cpuid = plat_my_core_pos();
Tejas Patelfe0e10a2019-12-08 23:29:44 -080085 const struct pm_proc *proc = pm_get_proc(cpuid);
86
Abhyuday Godhasara44877942021-08-05 02:59:26 -070087 if (proc == NULL) {
Tejas Patelfe0e10a2019-12-08 23:29:44 -080088 WARN("Failed to get proc %d\n", cpuid);
89 return PM_RET_ERROR_INTERNAL;
90 }
91
92 /*
93 * Do client specific suspend operations
94 * (e.g. set powerdown request bit)
95 */
96 pm_client_suspend(proc, state);
97
98 /* Send request to the PLM */
Tejas Patel18072da2021-02-25 20:16:56 -080099 PM_PACK_PAYLOAD6(payload, LIBPM_MODULE_ID, flag, PM_SELF_SUSPEND,
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800100 proc->node_id, latency, state, address,
101 (address >> 32));
102 return pm_ipi_send_sync(proc, payload, NULL, 0);
103}
104
105/**
106 * pm_abort_suspend() - PM call to announce that a prior suspend request
107 * is to be aborted.
108 * @reason Reason for the abort
Tejas Patel18072da2021-02-25 20:16:56 -0800109 * @flag 0 - Call from secure source
110 * 1 - Call from non-secure source
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800111 *
112 * Calling PU expects the PMU to abort the initiated suspend procedure.
113 * This is a non-blocking call without any acknowledge.
114 *
115 * @return Returns status, either success or error+reason
116 */
Tejas Patel18072da2021-02-25 20:16:56 -0800117enum pm_ret_status pm_abort_suspend(enum pm_abort_reason reason, uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800118{
119 uint32_t payload[PAYLOAD_ARG_CNT];
120
121 /*
122 * Do client specific abort suspend operations
123 * (e.g. enable interrupts and clear powerdown request bit)
124 */
125 pm_client_abort_suspend();
126
127 /* Send request to the PLM */
Tejas Patel18072da2021-02-25 20:16:56 -0800128 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_ABORT_SUSPEND,
129 reason, primary_proc->node_id);
Abhyuday Godhasara7ae761b2021-06-24 05:50:44 -0700130 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800131}
132
133/**
134 * pm_req_suspend() - PM call to request for another PU or subsystem to
135 * be suspended gracefully.
136 * @target Node id of the targeted PU or subsystem
137 * @ack Flag to specify whether acknowledge is requested
138 * @latency Requested wakeup latency (not supported)
139 * @state Requested state (not supported)
Tejas Patel18072da2021-02-25 20:16:56 -0800140 * @flag 0 - Call from secure source
141 * 1 - Call from non-secure source
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800142 *
143 * @return Returns status, either success or error+reason
144 */
145enum pm_ret_status pm_req_suspend(uint32_t target, uint8_t ack,
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530146 uint32_t latency, uint32_t state,
Tejas Patel18072da2021-02-25 20:16:56 -0800147 uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800148{
149 uint32_t payload[PAYLOAD_ARG_CNT];
150
151 /* Send request to the PMU */
Tejas Patel18072da2021-02-25 20:16:56 -0800152 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_REQ_SUSPEND, target,
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800153 latency, state);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700154 if (ack == IPI_BLOCKING) {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800155 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700156 } else {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800157 return pm_ipi_send(primary_proc, payload);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700158 }
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800159}
Tejas Patel41f3e0b2019-01-08 01:46:37 -0800160
161/**
Tejas Patel49cd8712019-01-23 14:18:51 +0530162 * pm_req_wakeup() - PM call for processor to wake up selected processor
163 * or subsystem
164 * @target Device ID of the processor or subsystem to wake up
165 * @set_address Resume address presence indicator
166 * 1 - resume address specified, 0 - otherwise
167 * @address Resume address
168 * @ack Flag to specify whether acknowledge requested
Tejas Patel18072da2021-02-25 20:16:56 -0800169 * @flag 0 - Call from secure source
170 * 1 - Call from non-secure source
Tejas Patel49cd8712019-01-23 14:18:51 +0530171 *
172 * This API function is either used to power up another APU core for SMP
173 * (by PSCI) or to power up an entirely different PU or subsystem, such
174 * as RPU0, RPU, or PL_CORE_xx. Resume address for the target PU will be
175 * automatically set by PMC.
176 *
177 * @return Returns status, either success or error+reason
178 */
179enum pm_ret_status pm_req_wakeup(uint32_t target, uint32_t set_address,
Tejas Patel18072da2021-02-25 20:16:56 -0800180 uintptr_t address, uint8_t ack, uint32_t flag)
Tejas Patel49cd8712019-01-23 14:18:51 +0530181{
182 uint32_t payload[PAYLOAD_ARG_CNT];
183
184 /* Send request to the PMC to perform the wake of the PU */
Tejas Patel18072da2021-02-25 20:16:56 -0800185 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REQ_WAKEUP, target,
Tejas Patel49cd8712019-01-23 14:18:51 +0530186 set_address, address, ack);
187
188 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
189}
190
191/**
Rajan Vaja649e9242019-03-04 11:09:40 +0530192 * pm_get_callbackdata() - Read from IPI response buffer
193 * @data - array of PAYLOAD_ARG_CNT elements
Tejas Patel18072da2021-02-25 20:16:56 -0800194 * @flag - 0 - Call from secure source
195 * 1 - Call from non-secure source
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700196 * @ack - 0 - Do not ack IPI after reading payload
197 * 1 - Ack IPI after reading payload
Rajan Vaja649e9242019-03-04 11:09:40 +0530198 *
199 * Read value from ipi buffer response buffer.
200 */
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700201void pm_get_callbackdata(uint32_t *data, size_t count, uint32_t flag, uint32_t ack)
Rajan Vaja649e9242019-03-04 11:09:40 +0530202{
203 /* Return if interrupt is not from PMU */
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700204 if (pm_ipi_irq_status(primary_proc) == 0) {
Rajan Vaja649e9242019-03-04 11:09:40 +0530205 return;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700206 }
Rajan Vaja649e9242019-03-04 11:09:40 +0530207
208 pm_ipi_buff_read_callb(data, count);
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700209
210 if (ack != 0U) {
211 pm_ipi_irq_clear(primary_proc);
212 }
Rajan Vaja649e9242019-03-04 11:09:40 +0530213}
214
215/**
Tanmay Shahff34daa2021-08-09 11:00:41 -0700216 * pm_pll_set_param() - Set PLL parameter
Tejas Patel1f56fb12019-01-08 01:46:40 -0800217 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700218 * This API is deprecated and maintained here for backward compatibility.
219 * New use of this API should be avoided for versal platform.
220 * This API and its use cases will be removed for versal platform.
Tejas Patel84275bf2020-09-01 04:43:53 -0700221 *
Tejas Patel023116d2019-01-08 01:46:41 -0800222 * @clk_id PLL clock ID
223 * @param PLL parameter ID
224 * @value Value to set for PLL parameter
Tejas Patel18072da2021-02-25 20:16:56 -0800225 * @flag 0 - Call from secure source
226 * 1 - Call from non-secure source
Tejas Patel023116d2019-01-08 01:46:41 -0800227 *
228 * @return Returns status, either success or error+reason
229 */
230enum pm_ret_status pm_pll_set_param(uint32_t clk_id, uint32_t param,
Tejas Patel18072da2021-02-25 20:16:56 -0800231 uint32_t value, uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800232{
233 uint32_t payload[PAYLOAD_ARG_CNT];
234
235 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800236 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_PLL_SET_PARAMETER,
237 clk_id, param, value);
Tejas Patel023116d2019-01-08 01:46:41 -0800238
239 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
240}
241
242/**
243 * pm_pll_get_param() - Get PLL parameter value
Tanmay Shahff34daa2021-08-09 11:00:41 -0700244 *
245 * This API is deprecated and maintained here for backward compatibility.
246 * New use of this API should be avoided for versal platform.
247 * This API and its use cases will be removed for versal platform.
248 *
Tejas Patel023116d2019-01-08 01:46:41 -0800249 * @clk_id PLL clock ID
250 * @param PLL parameter ID
251 * @value: Buffer to store PLL parameter value
Tejas Patel18072da2021-02-25 20:16:56 -0800252 * @flag 0 - Call from secure source
253 * 1 - Call from non-secure source
Tejas Patel023116d2019-01-08 01:46:41 -0800254 *
255 * @return Returns status, either success or error+reason
256 */
257enum pm_ret_status pm_pll_get_param(uint32_t clk_id, uint32_t param,
Tejas Patel18072da2021-02-25 20:16:56 -0800258 uint32_t *value, uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800259{
260 uint32_t payload[PAYLOAD_ARG_CNT];
261
262 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800263 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_PLL_GET_PARAMETER,
264 clk_id, param);
Tejas Patel023116d2019-01-08 01:46:41 -0800265
266 return pm_ipi_send_sync(primary_proc, payload, value, 1);
267}
268
269/**
270 * pm_pll_set_mode() - Set PLL mode
Tanmay Shahff34daa2021-08-09 11:00:41 -0700271 *
272 * This API is deprecated and maintained here for backward compatibility.
273 * New use of this API should be avoided for versal platform.
274 * This API and its use cases will be removed for versal platform.
275 *
Tejas Patel023116d2019-01-08 01:46:41 -0800276 * @clk_id PLL clock ID
277 * @mode PLL mode
Tejas Patel18072da2021-02-25 20:16:56 -0800278 * @flag 0 - Call from secure source
279 * 1 - Call from non-secure source
Tejas Patel023116d2019-01-08 01:46:41 -0800280 *
281 * @return Returns status, either success or error+reason
282 */
Tejas Patel18072da2021-02-25 20:16:56 -0800283enum pm_ret_status pm_pll_set_mode(uint32_t clk_id, uint32_t mode,
284 uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800285{
286 uint32_t payload[PAYLOAD_ARG_CNT];
287
288 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800289 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_PLL_SET_MODE,
290 clk_id, mode);
Tejas Patel023116d2019-01-08 01:46:41 -0800291
292 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
293}
294
295/**
296 * pm_pll_get_mode() - Get PLL mode
Tanmay Shahff34daa2021-08-09 11:00:41 -0700297 *
298 * This API is deprecated and maintained here for backward compatibility.
299 * New use of this API should be avoided for versal platform.
300 * This API and its use cases will be removed for versal platform.
301 *
Tejas Patel023116d2019-01-08 01:46:41 -0800302 * @clk_id PLL clock ID
303 * @mode: Buffer to store PLL mode
Tejas Patel18072da2021-02-25 20:16:56 -0800304 * @flag 0 - Call from secure source
305 * 1 - Call from non-secure source
Tejas Patel023116d2019-01-08 01:46:41 -0800306 *
307 * @return Returns status, either success or error+reason
308 */
Tejas Patel18072da2021-02-25 20:16:56 -0800309enum pm_ret_status pm_pll_get_mode(uint32_t clk_id, uint32_t *mode,
310 uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800311{
312 uint32_t payload[PAYLOAD_ARG_CNT];
313
314 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800315 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_PLL_GET_MODE,
316 clk_id);
Tejas Patel023116d2019-01-08 01:46:41 -0800317
318 return pm_ipi_send_sync(primary_proc, payload, mode, 1);
319}
Tejas Patel6b282252019-01-10 03:03:47 -0800320
321/**
322 * pm_force_powerdown() - PM call to request for another PU or subsystem to
323 * be powered down forcefully
324 * @target Device ID of the PU node to be forced powered down.
325 * @ack Flag to specify whether acknowledge is requested
Tejas Patel18072da2021-02-25 20:16:56 -0800326 * @flag 0 - Call from secure source
327 * 1 - Call from non-secure source
Tejas Patel6b282252019-01-10 03:03:47 -0800328 *
329 * @return Returns status, either success or error+reason
330 */
Tejas Patel18072da2021-02-25 20:16:56 -0800331enum pm_ret_status pm_force_powerdown(uint32_t target, uint8_t ack,
332 uint32_t flag)
Tejas Patel6b282252019-01-10 03:03:47 -0800333{
334 uint32_t payload[PAYLOAD_ARG_CNT];
335
336 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800337 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_FORCE_POWERDOWN,
338 target, ack);
Tejas Patel6b282252019-01-10 03:03:47 -0800339
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700340 if (ack == IPI_BLOCKING) {
Tejas Patel6b282252019-01-10 03:03:47 -0800341 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700342 } else {
Tejas Patel6b282252019-01-10 03:03:47 -0800343 return pm_ipi_send(primary_proc, payload);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700344 }
Tejas Patel6b282252019-01-10 03:03:47 -0800345}
346
347/**
348 * pm_system_shutdown() - PM call to request a system shutdown or restart
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800349 * @type Shutdown or restart? 0=shutdown, 1=restart, 2=setscope
Tejas Patel6b282252019-01-10 03:03:47 -0800350 * @subtype Scope: 0=APU-subsystem, 1=PS, 2=system
Tejas Patel18072da2021-02-25 20:16:56 -0800351 * @flag 0 - Call from secure source
352 * 1 - Call from non-secure source
Tejas Patel6b282252019-01-10 03:03:47 -0800353 *
354 * @return Returns status, either success or error+reason
355 */
Tejas Patel18072da2021-02-25 20:16:56 -0800356enum pm_ret_status pm_system_shutdown(uint32_t type, uint32_t subtype,
357 uint32_t flag)
Tejas Patel6b282252019-01-10 03:03:47 -0800358{
359 uint32_t payload[PAYLOAD_ARG_CNT];
360
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800361 if (type == XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY) {
362 /* Setting scope for subsequent PSCI reboot or shutdown */
363 pm_shutdown_scope = subtype;
364 return PM_RET_SUCCESS;
365 }
366
Tejas Patel6b282252019-01-10 03:03:47 -0800367 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800368 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SYSTEM_SHUTDOWN,
369 type, subtype);
Tejas Patel6b282252019-01-10 03:03:47 -0800370
371 return pm_ipi_send_non_blocking(primary_proc, payload);
372}
Tejas Patel9141f442019-01-10 03:03:48 -0800373
374/**
Tanmay Shahff34daa2021-08-09 11:00:41 -0700375 * pm_query_data() - PM API for querying firmware data
376 *
377 * This API is deprecated and maintained here for backward compatibility.
378 * New use of this API should be avoided for versal platform.
379 * This API and its use cases will be removed for versal platform.
380 *
381 * @qid The type of data to query
382 * @arg1 Argument 1 to requested query data call
383 * @arg2 Argument 2 to requested query data call
384 * @arg3 Argument 3 to requested query data call
385 * @data Returned output data
386 * @flag 0 - Call from secure source
387 * 1 - Call from non-secure source
388 *
389 * @retur - 0 if success else non-zero error code of type
390 * enum pm_ret_status
391 */
Tejas Patela3e34ad2019-02-01 17:25:19 +0530392enum pm_ret_status pm_query_data(uint32_t qid, uint32_t arg1, uint32_t arg2,
Tejas Patel18072da2021-02-25 20:16:56 -0800393 uint32_t arg3, uint32_t *data, uint32_t flag)
Tejas Patela3e34ad2019-02-01 17:25:19 +0530394{
Rajan Vaja030620d2020-11-23 04:13:54 -0800395 uint32_t ret;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700396 uint32_t version[PAYLOAD_ARG_CNT] = {0};
Tejas Patela3e34ad2019-02-01 17:25:19 +0530397 uint32_t payload[PAYLOAD_ARG_CNT];
Rajan Vaja030620d2020-11-23 04:13:54 -0800398 uint32_t fw_api_version;
Tejas Patela3e34ad2019-02-01 17:25:19 +0530399
400 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800401 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_QUERY_DATA, qid,
402 arg1, arg2, arg3);
Rajan Vaja030620d2020-11-23 04:13:54 -0800403
Tanmay Shahff34daa2021-08-09 11:00:41 -0700404 ret = pm_feature_check(PM_QUERY_DATA, &version[0], flag);
405 if (ret == PM_RET_SUCCESS) {
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530406 fw_api_version = version[0] & 0xFFFFU;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700407 if ((fw_api_version == 2U) &&
408 ((qid == XPM_QID_CLOCK_GET_NAME) ||
409 (qid == XPM_QID_PINCTRL_GET_FUNCTION_NAME))) {
410 ret = pm_ipi_send_sync(primary_proc, payload, data, PAYLOAD_ARG_CNT);
411 if (ret == PM_RET_SUCCESS) {
412 ret = data[0];
413 data[0] = data[1];
414 data[1] = data[2];
415 data[2] = data[3];
416 }
Rajan Vaja030620d2020-11-23 04:13:54 -0800417 } else {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700418 ret = pm_ipi_send_sync(primary_proc, payload, data, PAYLOAD_ARG_CNT);
Rajan Vaja030620d2020-11-23 04:13:54 -0800419 }
420 }
421 return ret;
Tejas Patela3e34ad2019-02-01 17:25:19 +0530422}
423/**
Tejas Patel9141f442019-01-10 03:03:48 -0800424 * pm_api_ioctl() - PM IOCTL API for device control and configs
Tanmay Shahff34daa2021-08-09 11:00:41 -0700425 *
426 * This API is deprecated and maintained here for backward compatibility.
427 * New use of this API should be avoided for versal platform.
428 * This API and its use cases will be removed for versal platform.
429 *
Tejas Patel9141f442019-01-10 03:03:48 -0800430 * @device_id Device ID
431 * @ioctl_id ID of the requested IOCTL
432 * @arg1 Argument 1 to requested IOCTL call
433 * @arg2 Argument 2 to requested IOCTL call
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600434 * @arg3 Argument 3 to requested IOCTL call
Tejas Patel9141f442019-01-10 03:03:48 -0800435 * @value Returned output value
Tejas Patel18072da2021-02-25 20:16:56 -0800436 * @flag 0 - Call from secure source
437 * 1 - Call from non-secure source
Tejas Patel9141f442019-01-10 03:03:48 -0800438 *
439 * This function calls IOCTL to firmware for device control and configuration.
440 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700441 * @return Returns status, either 0 on success or non-zero error code
442 * of type enum pm_ret_status
Tejas Patel9141f442019-01-10 03:03:48 -0800443 */
444enum pm_ret_status pm_api_ioctl(uint32_t device_id, uint32_t ioctl_id,
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600445 uint32_t arg1, uint32_t arg2, uint32_t arg3,
446 uint32_t *value, uint32_t flag)
Tejas Patel9141f442019-01-10 03:03:48 -0800447{
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700448 enum pm_ret_status ret;
Tejas Patel9141f442019-01-10 03:03:48 -0800449
450 switch (ioctl_id) {
451 case IOCTL_SET_PLL_FRAC_MODE:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700452 ret = pm_pll_set_mode(arg1, arg2, flag);
453 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800454 case IOCTL_GET_PLL_FRAC_MODE:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700455 ret = pm_pll_get_mode(arg1, value, flag);
456 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800457 case IOCTL_SET_PLL_FRAC_DATA:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700458 ret = pm_pll_set_param(arg1, PM_PLL_PARAM_DATA, arg2, flag);
459 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800460 case IOCTL_GET_PLL_FRAC_DATA:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700461 ret = pm_pll_get_param(arg1, PM_PLL_PARAM_DATA, value, flag);
462 break;
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600463 case IOCTL_SET_SGI:
464 /* Get the sgi number */
Venkatesh Yadav Abbarapuc8bbedc2021-04-19 07:49:57 -0600465 ret = pm_register_sgi(arg1, arg2);
466 if (ret != 0) {
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600467 return PM_RET_ERROR_ARGS;
468 }
469 gicd_write_irouter(gicv3_driver_data->gicd_base,
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530470 (uint32_t)PLAT_VERSAL_IPI_IRQ, MODE);
Michal Simek89e3c292022-08-04 14:08:32 +0200471 ret = PM_RET_SUCCESS;
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700472 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800473 default:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700474 return PM_RET_ERROR_NOTSUPPORTED;
Tejas Patel9141f442019-01-10 03:03:48 -0800475 }
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700476
477 return ret;
Tejas Patel9141f442019-01-10 03:03:48 -0800478}
Tejas Pateldb812052019-01-23 14:18:53 +0530479
480/**
481 * pm_set_wakeup_source() - PM call to specify the wakeup source while suspended
482 * @target Device id of the targeted PU or subsystem
483 * @wkup_node Device id of the wakeup peripheral
484 * @enable Enable or disable the specified peripheral as wake source
Tejas Patel18072da2021-02-25 20:16:56 -0800485 * @flag 0 - Call from secure source
486 * 1 - Call from non-secure source
Tejas Pateldb812052019-01-23 14:18:53 +0530487 *
488 * @return Returns status, either success or error+reason
489 */
490enum pm_ret_status pm_set_wakeup_source(uint32_t target, uint32_t wkup_device,
Tejas Patel18072da2021-02-25 20:16:56 -0800491 uint8_t enable, uint32_t flag)
Tejas Pateldb812052019-01-23 14:18:53 +0530492{
493 uint32_t payload[PAYLOAD_ARG_CNT];
494
Tejas Patel18072da2021-02-25 20:16:56 -0800495 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_SET_WAKEUP_SOURCE,
496 target, wkup_device, enable);
Abhyuday Godhasara7ae761b2021-06-24 05:50:44 -0700497 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Tejas Pateldb812052019-01-23 14:18:53 +0530498}
Ravi Patel22b0b492019-03-06 12:34:46 +0530499
500/**
501 * pm_feature_check() - Returns the supported API version if supported
502 * @api_id API ID to check
Tejas Patel18072da2021-02-25 20:16:56 -0800503 * @flag 0 - Call from secure source
504 * 1 - Call from non-secure source
Ronak Jain70d1c582022-02-04 00:42:55 -0800505 * @ret_payload pointer to array of PAYLOAD_ARG_CNT number of
506 * words Returned supported API version and bitmasks
507 * for IOCTL and QUERY ID
Ravi Patel22b0b492019-03-06 12:34:46 +0530508 *
509 * @return Returns status, either success or error+reason
510 */
Ronak Jain70d1c582022-02-04 00:42:55 -0800511enum pm_ret_status pm_feature_check(uint32_t api_id, uint32_t *ret_payload,
Tejas Patel18072da2021-02-25 20:16:56 -0800512 uint32_t flag)
Ravi Patel22b0b492019-03-06 12:34:46 +0530513{
Ronak Jain70d1c582022-02-04 00:42:55 -0800514 uint32_t payload[PAYLOAD_ARG_CNT];
Tanmay Shahff34daa2021-08-09 11:00:41 -0700515 uint32_t module_id;
516
Ronak Jain70d1c582022-02-04 00:42:55 -0800517 /* Return version of API which are implemented in ATF only */
Ravi Patel22b0b492019-03-06 12:34:46 +0530518 switch (api_id) {
519 case PM_GET_CALLBACK_DATA:
520 case PM_GET_TRUSTZONE_VERSION:
Ronak Jain70d1c582022-02-04 00:42:55 -0800521 ret_payload[0] = PM_API_VERSION_2;
522 return PM_RET_SUCCESS;
Tanmay Shah5e3af332022-08-26 15:13:48 -0700523 case TF_A_PM_REGISTER_SGI:
Ronak Jain70d1c582022-02-04 00:42:55 -0800524 ret_payload[0] = PM_API_BASE_VERSION;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700525 return PM_RET_SUCCESS;
Ravi Patel22b0b492019-03-06 12:34:46 +0530526 default:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700527 break;
528 }
529
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530530 module_id = (api_id & MODULE_ID_MASK) >> 8U;
Ravi Patel22b0b492019-03-06 12:34:46 +0530531
Ronak Jain70d1c582022-02-04 00:42:55 -0800532 /*
533 * feature check should be done only for LIBPM module
534 * If module_id is 0, then we consider it LIBPM module as default id
535 */
536 if ((module_id > 0) && (module_id != LIBPM_MODULE_ID)) {
537 return PM_RET_SUCCESS;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700538 }
Ravi Patel22b0b492019-03-06 12:34:46 +0530539
Ronak Jain70d1c582022-02-04 00:42:55 -0800540 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag,
541 PM_FEATURE_CHECK, api_id);
542 return pm_ipi_send_sync(primary_proc, payload, ret_payload, PAYLOAD_ARG_CNT);
Ravi Patel22b0b492019-03-06 12:34:46 +0530543}
Jolly Shahed05a712019-03-22 05:33:39 +0530544
545/**
546 * pm_load_pdi() - Load the PDI
547 *
548 * This function provides support to load PDI from linux
549 *
550 * src: Source device of pdi(DDR, OCM, SD etc)
551 * address_low: lower 32-bit Linear memory space address
552 * address_high: higher 32-bit Linear memory space address
Tejas Patel18072da2021-02-25 20:16:56 -0800553 * @flag 0 - Call from secure source
554 * 1 - Call from non-secure source
Jolly Shahed05a712019-03-22 05:33:39 +0530555 *
556 * @return Returns status, either success or error+reason
557 */
Tejas Patel18072da2021-02-25 20:16:56 -0800558enum pm_ret_status pm_load_pdi(uint32_t src, uint32_t address_low,
559 uint32_t address_high, uint32_t flag)
Jolly Shahed05a712019-03-22 05:33:39 +0530560{
561 uint32_t payload[PAYLOAD_ARG_CNT];
562
563 /* Send request to the PMU */
Tejas Patel18072da2021-02-25 20:16:56 -0800564 PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, flag, PM_LOAD_PDI, src,
Jolly Shahed05a712019-03-22 05:33:39 +0530565 address_high, address_low);
566 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
567}
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700568
569/**
Tejas Patel42015552020-11-25 01:56:57 -0800570 * pm_register_notifier() - PM call to register a subsystem to be notified
571 * about the device event
572 * @device_id Device ID for the Node to which the event is related
573 * @event Event in question
574 * @wake Wake subsystem upon capturing the event if value 1
575 * @enable Enable the registration for value 1, disable for value 0
Tejas Patel18072da2021-02-25 20:16:56 -0800576 * @flag 0 - Call from secure source
577 * 1 - Call from non-secure source
Tejas Patel42015552020-11-25 01:56:57 -0800578 *
579 * @return Returns status, either success or error+reason
580 */
581enum pm_ret_status pm_register_notifier(uint32_t device_id, uint32_t event,
Tejas Patel18072da2021-02-25 20:16:56 -0800582 uint32_t wake, uint32_t enable,
583 uint32_t flag)
Tejas Patel42015552020-11-25 01:56:57 -0800584{
585 uint32_t payload[PAYLOAD_ARG_CNT];
586
587 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800588 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REGISTER_NOTIFIER,
Tejas Patel42015552020-11-25 01:56:57 -0800589 device_id, event, wake, enable);
590
591 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
592}