blob: db78049005b19698cb9a1ee01e27b035f32c4afe [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.
Tejas Patel9d09ff92019-01-08 01:46:35 -08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/*
8 * Versal system level PM-API functions and communication with PMC via
9 * IPI interrupts
10 */
11
12#include <pm_common.h>
13#include <pm_ipi.h>
Tejas Patelfe0e10a2019-12-08 23:29:44 -080014#include <plat/common/platform.h>
Tejas Patel9d09ff92019-01-08 01:46:35 -080015#include "pm_api_sys.h"
16#include "pm_client.h"
Rajan Vaja030620d2020-11-23 04:13:54 -080017#include "pm_defs.h"
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053018#include "pm_svc_main.h"
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -060019#include "../drivers/arm/gic/v3/gicv3_private.h"
Tejas Patel9d09ff92019-01-08 01:46:35 -080020
21/*********************************************************************
22 * Target module IDs macros
23 ********************************************************************/
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -070024#define LIBPM_MODULE_ID 0x2U
25#define LOADER_MODULE_ID 0x7U
Tejas Patel9d09ff92019-01-08 01:46:35 -080026
Abhyuday Godhasaraedc38ae2021-08-04 23:58:46 -070027#define MODE 0x80000000U
Tanmay Shahff34daa2021-08-09 11:00:41 -070028#define MODULE_ID_MASK 0x0000ff00
29
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080030/* default shutdown/reboot scope is system(2) */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053031static uint32_t pm_shutdown_scope = XPM_SHUTDOWN_SUBTYPE_RST_SYSTEM;
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080032
33/**
34 * pm_get_shutdown_scope() - Get the currently set shutdown scope
35 *
36 * @return Shutdown scope value
37 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053038uint32_t pm_get_shutdown_scope(void)
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080039{
40 return pm_shutdown_scope;
41}
42
Tejas Patel9d09ff92019-01-08 01:46:35 -080043/**
44 * Assigning of argument values into array elements.
45 */
Tejas Patel18072da2021-02-25 20:16:56 -080046#define PM_PACK_PAYLOAD1(pl, mid, flag, arg0) { \
Abhyuday Godhasara19e47da2021-08-05 10:49:23 -070047 pl[0] = (uint32_t)(((uint32_t)(arg0) & 0xFFU) | ((mid) << 8U) | ((flag) << 24U)); \
Tejas Patel9d09ff92019-01-08 01:46:35 -080048}
49
Tejas Patel18072da2021-02-25 20:16:56 -080050#define PM_PACK_PAYLOAD2(pl, mid, flag, arg0, arg1) { \
51 pl[1] = (uint32_t)(arg1); \
Abhyuday Godhasara19e47da2021-08-05 10:49:23 -070052 PM_PACK_PAYLOAD1(pl, (mid), (flag), (arg0)); \
Tejas Patelfe0e10a2019-12-08 23:29:44 -080053}
54
Tejas Patel18072da2021-02-25 20:16:56 -080055#define PM_PACK_PAYLOAD3(pl, mid, flag, arg0, arg1, arg2) { \
56 pl[2] = (uint32_t)(arg2); \
Abhyuday Godhasara19e47da2021-08-05 10:49:23 -070057 PM_PACK_PAYLOAD2(pl, (mid), (flag), (arg0), (arg1)); \
Tejas Patelfe0e10a2019-12-08 23:29:44 -080058}
59
Tejas Patel18072da2021-02-25 20:16:56 -080060#define PM_PACK_PAYLOAD4(pl, mid, flag, arg0, arg1, arg2, arg3) { \
61 pl[3] = (uint32_t)(arg3); \
Abhyuday Godhasara19e47da2021-08-05 10:49:23 -070062 PM_PACK_PAYLOAD3(pl, (mid), (flag), (arg0), (arg1), (arg2)); \
Tejas Patelfe0e10a2019-12-08 23:29:44 -080063}
64
Tejas Patel18072da2021-02-25 20:16:56 -080065#define PM_PACK_PAYLOAD5(pl, mid, flag, arg0, arg1, arg2, arg3, arg4) { \
Tejas Patelfe0e10a2019-12-08 23:29:44 -080066 pl[4] = (uint32_t)(arg4); \
Abhyuday Godhasara19e47da2021-08-05 10:49:23 -070067 PM_PACK_PAYLOAD4(pl, (mid), (flag), (arg0), (arg1), (arg2), (arg3)); \
Tejas Patelfe0e10a2019-12-08 23:29:44 -080068}
69
Tejas Patel18072da2021-02-25 20:16:56 -080070#define PM_PACK_PAYLOAD6(pl, mid, flag, arg0, arg1, arg2, arg3, arg4, arg5) { \
71 pl[5] = (uint32_t)(arg5); \
Abhyuday Godhasara19e47da2021-08-05 10:49:23 -070072 PM_PACK_PAYLOAD5(pl, (mid), (flag), (arg0), (arg1), (arg2), (arg3), (arg4)); \
Tejas Patelfe0e10a2019-12-08 23:29:44 -080073}
74
Tejas Patel9d09ff92019-01-08 01:46:35 -080075/* PM API functions */
76
77/**
Tanmay Shahff34daa2021-08-09 11:00:41 -070078 * pm_handle_eemi_call() - PM call for processor to send eemi payload
Tejas Patel18072da2021-02-25 20:16:56 -080079 * @flag 0 - Call from secure source
80 * 1 - Call from non-secure source
Tanmay Shahff34daa2021-08-09 11:00:41 -070081 * @x0 to x5 Arguments received per SMC64 standard
82 * @result Payload received from firmware
Tejas Patel9d09ff92019-01-08 01:46:35 -080083 *
Tanmay Shahff34daa2021-08-09 11:00:41 -070084 * @return PM_RET_SUCCESS on success or error code
Tejas Patel9d09ff92019-01-08 01:46:35 -080085 */
Tanmay Shahff34daa2021-08-09 11:00:41 -070086enum pm_ret_status pm_handle_eemi_call(uint32_t flag, uint32_t x0, uint32_t x1,
87 uint32_t x2, uint32_t x3, uint32_t x4,
88 uint32_t x5, uint64_t *result)
Tejas Patel9d09ff92019-01-08 01:46:35 -080089{
Tanmay Shahff34daa2021-08-09 11:00:41 -070090 uint32_t payload[PAYLOAD_ARG_CNT] = {0};
91 uint32_t module_id;
Tejas Patel9d09ff92019-01-08 01:46:35 -080092
Tanmay Shahff34daa2021-08-09 11:00:41 -070093 module_id = (x0 & MODULE_ID_MASK) >> 8;
Tejas Patelfe0e10a2019-12-08 23:29:44 -080094
Tanmay Shahff34daa2021-08-09 11:00:41 -070095 //default module id is for LIBPM
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +053096 if (module_id == 0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -070097 module_id = LIBPM_MODULE_ID;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +053098 }
Ravi Patel476b5b12019-08-12 03:10:10 -070099
Tanmay Shahff34daa2021-08-09 11:00:41 -0700100 PM_PACK_PAYLOAD6(payload, module_id, flag, x0, x1, x2, x3, x4, x5);
101 return pm_ipi_send_sync(primary_proc, payload, (uint32_t *)result, PAYLOAD_ARG_CNT);
Ravi Patel476b5b12019-08-12 03:10:10 -0700102}
103
104/**
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800105 * pm_self_suspend() - PM call for processor to suspend itself
106 * @nid Node id of the processor or subsystem
107 * @latency Requested maximum wakeup latency (not supported)
108 * @state Requested state
109 * @address Resume address
Tejas Patel18072da2021-02-25 20:16:56 -0800110 * @flag 0 - Call from secure source
111 * 1 - Call from non-secure source
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800112 *
113 * This is a blocking call, it will return only once PMU has responded.
114 * On a wakeup, resume address will be automatically set by PMU.
115 *
116 * @return Returns status, either success or error+reason
117 */
118enum pm_ret_status pm_self_suspend(uint32_t nid,
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530119 uint32_t latency,
120 uint32_t state,
Tejas Patel18072da2021-02-25 20:16:56 -0800121 uintptr_t address, uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800122{
123 uint32_t payload[PAYLOAD_ARG_CNT];
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530124 uint32_t cpuid = plat_my_core_pos();
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800125 const struct pm_proc *proc = pm_get_proc(cpuid);
126
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700127 if (proc == NULL) {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800128 WARN("Failed to get proc %d\n", cpuid);
129 return PM_RET_ERROR_INTERNAL;
130 }
131
132 /*
133 * Do client specific suspend operations
134 * (e.g. set powerdown request bit)
135 */
136 pm_client_suspend(proc, state);
137
138 /* Send request to the PLM */
Tejas Patel18072da2021-02-25 20:16:56 -0800139 PM_PACK_PAYLOAD6(payload, LIBPM_MODULE_ID, flag, PM_SELF_SUSPEND,
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800140 proc->node_id, latency, state, address,
141 (address >> 32));
142 return pm_ipi_send_sync(proc, payload, NULL, 0);
143}
144
145/**
146 * pm_abort_suspend() - PM call to announce that a prior suspend request
147 * is to be aborted.
148 * @reason Reason for the abort
Tejas Patel18072da2021-02-25 20:16:56 -0800149 * @flag 0 - Call from secure source
150 * 1 - Call from non-secure source
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800151 *
152 * Calling PU expects the PMU to abort the initiated suspend procedure.
153 * This is a non-blocking call without any acknowledge.
154 *
155 * @return Returns status, either success or error+reason
156 */
Tejas Patel18072da2021-02-25 20:16:56 -0800157enum pm_ret_status pm_abort_suspend(enum pm_abort_reason reason, uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800158{
159 uint32_t payload[PAYLOAD_ARG_CNT];
160
161 /*
162 * Do client specific abort suspend operations
163 * (e.g. enable interrupts and clear powerdown request bit)
164 */
165 pm_client_abort_suspend();
166
167 /* Send request to the PLM */
Tejas Patel18072da2021-02-25 20:16:56 -0800168 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_ABORT_SUSPEND,
169 reason, primary_proc->node_id);
Abhyuday Godhasara7ae761b2021-06-24 05:50:44 -0700170 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800171}
172
173/**
174 * pm_req_suspend() - PM call to request for another PU or subsystem to
175 * be suspended gracefully.
176 * @target Node id of the targeted PU or subsystem
177 * @ack Flag to specify whether acknowledge is requested
178 * @latency Requested wakeup latency (not supported)
179 * @state Requested state (not supported)
Tejas Patel18072da2021-02-25 20:16:56 -0800180 * @flag 0 - Call from secure source
181 * 1 - Call from non-secure source
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800182 *
183 * @return Returns status, either success or error+reason
184 */
185enum pm_ret_status pm_req_suspend(uint32_t target, uint8_t ack,
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530186 uint32_t latency, uint32_t state,
Tejas Patel18072da2021-02-25 20:16:56 -0800187 uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800188{
189 uint32_t payload[PAYLOAD_ARG_CNT];
190
191 /* Send request to the PMU */
Tejas Patel18072da2021-02-25 20:16:56 -0800192 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_REQ_SUSPEND, target,
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800193 latency, state);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700194 if (ack == IPI_BLOCKING) {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800195 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700196 } else {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800197 return pm_ipi_send(primary_proc, payload);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700198 }
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800199}
Tejas Patel41f3e0b2019-01-08 01:46:37 -0800200
201/**
Tejas Patel49cd8712019-01-23 14:18:51 +0530202 * pm_req_wakeup() - PM call for processor to wake up selected processor
203 * or subsystem
204 * @target Device ID of the processor or subsystem to wake up
205 * @set_address Resume address presence indicator
206 * 1 - resume address specified, 0 - otherwise
207 * @address Resume address
208 * @ack Flag to specify whether acknowledge requested
Tejas Patel18072da2021-02-25 20:16:56 -0800209 * @flag 0 - Call from secure source
210 * 1 - Call from non-secure source
Tejas Patel49cd8712019-01-23 14:18:51 +0530211 *
212 * This API function is either used to power up another APU core for SMP
213 * (by PSCI) or to power up an entirely different PU or subsystem, such
214 * as RPU0, RPU, or PL_CORE_xx. Resume address for the target PU will be
215 * automatically set by PMC.
216 *
217 * @return Returns status, either success or error+reason
218 */
219enum pm_ret_status pm_req_wakeup(uint32_t target, uint32_t set_address,
Tejas Patel18072da2021-02-25 20:16:56 -0800220 uintptr_t address, uint8_t ack, uint32_t flag)
Tejas Patel49cd8712019-01-23 14:18:51 +0530221{
222 uint32_t payload[PAYLOAD_ARG_CNT];
223
224 /* Send request to the PMC to perform the wake of the PU */
Tejas Patel18072da2021-02-25 20:16:56 -0800225 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REQ_WAKEUP, target,
Tejas Patel49cd8712019-01-23 14:18:51 +0530226 set_address, address, ack);
227
228 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
229}
230
231/**
Rajan Vaja649e9242019-03-04 11:09:40 +0530232 * pm_get_callbackdata() - Read from IPI response buffer
233 * @data - array of PAYLOAD_ARG_CNT elements
Tejas Patel18072da2021-02-25 20:16:56 -0800234 * @flag - 0 - Call from secure source
235 * 1 - Call from non-secure source
Rajan Vaja649e9242019-03-04 11:09:40 +0530236 *
237 * Read value from ipi buffer response buffer.
238 */
Tejas Patel18072da2021-02-25 20:16:56 -0800239void pm_get_callbackdata(uint32_t *data, size_t count, uint32_t flag)
Rajan Vaja649e9242019-03-04 11:09:40 +0530240{
241 /* Return if interrupt is not from PMU */
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700242 if (pm_ipi_irq_status(primary_proc) == 0) {
Rajan Vaja649e9242019-03-04 11:09:40 +0530243 return;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700244 }
Rajan Vaja649e9242019-03-04 11:09:40 +0530245
246 pm_ipi_buff_read_callb(data, count);
247 pm_ipi_irq_clear(primary_proc);
248}
249
250/**
Tanmay Shahff34daa2021-08-09 11:00:41 -0700251 * pm_pll_set_param() - Set PLL parameter
Tejas Patel1f56fb12019-01-08 01:46:40 -0800252 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700253 * This API is deprecated and maintained here for backward compatibility.
254 * New use of this API should be avoided for versal platform.
255 * This API and its use cases will be removed for versal platform.
Tejas Patel84275bf2020-09-01 04:43:53 -0700256 *
Tejas Patel023116d2019-01-08 01:46:41 -0800257 * @clk_id PLL clock ID
258 * @param PLL parameter ID
259 * @value Value to set for PLL parameter
Tejas Patel18072da2021-02-25 20:16:56 -0800260 * @flag 0 - Call from secure source
261 * 1 - Call from non-secure source
Tejas Patel023116d2019-01-08 01:46:41 -0800262 *
263 * @return Returns status, either success or error+reason
264 */
265enum pm_ret_status pm_pll_set_param(uint32_t clk_id, uint32_t param,
Tejas Patel18072da2021-02-25 20:16:56 -0800266 uint32_t value, uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800267{
268 uint32_t payload[PAYLOAD_ARG_CNT];
269
270 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800271 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_PLL_SET_PARAMETER,
272 clk_id, param, value);
Tejas Patel023116d2019-01-08 01:46:41 -0800273
274 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
275}
276
277/**
278 * pm_pll_get_param() - Get PLL parameter value
Tanmay Shahff34daa2021-08-09 11:00:41 -0700279 *
280 * This API is deprecated and maintained here for backward compatibility.
281 * New use of this API should be avoided for versal platform.
282 * This API and its use cases will be removed for versal platform.
283 *
Tejas Patel023116d2019-01-08 01:46:41 -0800284 * @clk_id PLL clock ID
285 * @param PLL parameter ID
286 * @value: Buffer to store PLL parameter value
Tejas Patel18072da2021-02-25 20:16:56 -0800287 * @flag 0 - Call from secure source
288 * 1 - Call from non-secure source
Tejas Patel023116d2019-01-08 01:46:41 -0800289 *
290 * @return Returns status, either success or error+reason
291 */
292enum pm_ret_status pm_pll_get_param(uint32_t clk_id, uint32_t param,
Tejas Patel18072da2021-02-25 20:16:56 -0800293 uint32_t *value, uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800294{
295 uint32_t payload[PAYLOAD_ARG_CNT];
296
297 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800298 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_PLL_GET_PARAMETER,
299 clk_id, param);
Tejas Patel023116d2019-01-08 01:46:41 -0800300
301 return pm_ipi_send_sync(primary_proc, payload, value, 1);
302}
303
304/**
305 * pm_pll_set_mode() - Set PLL mode
Tanmay Shahff34daa2021-08-09 11:00:41 -0700306 *
307 * This API is deprecated and maintained here for backward compatibility.
308 * New use of this API should be avoided for versal platform.
309 * This API and its use cases will be removed for versal platform.
310 *
Tejas Patel023116d2019-01-08 01:46:41 -0800311 * @clk_id PLL clock ID
312 * @mode PLL mode
Tejas Patel18072da2021-02-25 20:16:56 -0800313 * @flag 0 - Call from secure source
314 * 1 - Call from non-secure source
Tejas Patel023116d2019-01-08 01:46:41 -0800315 *
316 * @return Returns status, either success or error+reason
317 */
Tejas Patel18072da2021-02-25 20:16:56 -0800318enum pm_ret_status pm_pll_set_mode(uint32_t clk_id, uint32_t mode,
319 uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800320{
321 uint32_t payload[PAYLOAD_ARG_CNT];
322
323 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800324 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_PLL_SET_MODE,
325 clk_id, mode);
Tejas Patel023116d2019-01-08 01:46:41 -0800326
327 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
328}
329
330/**
331 * pm_pll_get_mode() - Get PLL mode
Tanmay Shahff34daa2021-08-09 11:00:41 -0700332 *
333 * This API is deprecated and maintained here for backward compatibility.
334 * New use of this API should be avoided for versal platform.
335 * This API and its use cases will be removed for versal platform.
336 *
Tejas Patel023116d2019-01-08 01:46:41 -0800337 * @clk_id PLL clock ID
338 * @mode: Buffer to store PLL mode
Tejas Patel18072da2021-02-25 20:16:56 -0800339 * @flag 0 - Call from secure source
340 * 1 - Call from non-secure source
Tejas Patel023116d2019-01-08 01:46:41 -0800341 *
342 * @return Returns status, either success or error+reason
343 */
Tejas Patel18072da2021-02-25 20:16:56 -0800344enum pm_ret_status pm_pll_get_mode(uint32_t clk_id, uint32_t *mode,
345 uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800346{
347 uint32_t payload[PAYLOAD_ARG_CNT];
348
349 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800350 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_PLL_GET_MODE,
351 clk_id);
Tejas Patel023116d2019-01-08 01:46:41 -0800352
353 return pm_ipi_send_sync(primary_proc, payload, mode, 1);
354}
Tejas Patel6b282252019-01-10 03:03:47 -0800355
356/**
357 * pm_force_powerdown() - PM call to request for another PU or subsystem to
358 * be powered down forcefully
359 * @target Device ID of the PU node to be forced powered down.
360 * @ack Flag to specify whether acknowledge is requested
Tejas Patel18072da2021-02-25 20:16:56 -0800361 * @flag 0 - Call from secure source
362 * 1 - Call from non-secure source
Tejas Patel6b282252019-01-10 03:03:47 -0800363 *
364 * @return Returns status, either success or error+reason
365 */
Tejas Patel18072da2021-02-25 20:16:56 -0800366enum pm_ret_status pm_force_powerdown(uint32_t target, uint8_t ack,
367 uint32_t flag)
Tejas Patel6b282252019-01-10 03:03:47 -0800368{
369 uint32_t payload[PAYLOAD_ARG_CNT];
370
371 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800372 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_FORCE_POWERDOWN,
373 target, ack);
Tejas Patel6b282252019-01-10 03:03:47 -0800374
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700375 if (ack == IPI_BLOCKING) {
Tejas Patel6b282252019-01-10 03:03:47 -0800376 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700377 } else {
Tejas Patel6b282252019-01-10 03:03:47 -0800378 return pm_ipi_send(primary_proc, payload);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700379 }
Tejas Patel6b282252019-01-10 03:03:47 -0800380}
381
382/**
383 * pm_system_shutdown() - PM call to request a system shutdown or restart
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800384 * @type Shutdown or restart? 0=shutdown, 1=restart, 2=setscope
Tejas Patel6b282252019-01-10 03:03:47 -0800385 * @subtype Scope: 0=APU-subsystem, 1=PS, 2=system
Tejas Patel18072da2021-02-25 20:16:56 -0800386 * @flag 0 - Call from secure source
387 * 1 - Call from non-secure source
Tejas Patel6b282252019-01-10 03:03:47 -0800388 *
389 * @return Returns status, either success or error+reason
390 */
Tejas Patel18072da2021-02-25 20:16:56 -0800391enum pm_ret_status pm_system_shutdown(uint32_t type, uint32_t subtype,
392 uint32_t flag)
Tejas Patel6b282252019-01-10 03:03:47 -0800393{
394 uint32_t payload[PAYLOAD_ARG_CNT];
395
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800396 if (type == XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY) {
397 /* Setting scope for subsequent PSCI reboot or shutdown */
398 pm_shutdown_scope = subtype;
399 return PM_RET_SUCCESS;
400 }
401
Tejas Patel6b282252019-01-10 03:03:47 -0800402 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800403 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SYSTEM_SHUTDOWN,
404 type, subtype);
Tejas Patel6b282252019-01-10 03:03:47 -0800405
406 return pm_ipi_send_non_blocking(primary_proc, payload);
407}
Tejas Patel9141f442019-01-10 03:03:48 -0800408
409/**
Tanmay Shahff34daa2021-08-09 11:00:41 -0700410 * pm_query_data() - PM API for querying firmware data
411 *
412 * This API is deprecated and maintained here for backward compatibility.
413 * New use of this API should be avoided for versal platform.
414 * This API and its use cases will be removed for versal platform.
415 *
416 * @qid The type of data to query
417 * @arg1 Argument 1 to requested query data call
418 * @arg2 Argument 2 to requested query data call
419 * @arg3 Argument 3 to requested query data call
420 * @data Returned output data
421 * @flag 0 - Call from secure source
422 * 1 - Call from non-secure source
423 *
424 * @retur - 0 if success else non-zero error code of type
425 * enum pm_ret_status
426 */
Tejas Patela3e34ad2019-02-01 17:25:19 +0530427enum pm_ret_status pm_query_data(uint32_t qid, uint32_t arg1, uint32_t arg2,
Tejas Patel18072da2021-02-25 20:16:56 -0800428 uint32_t arg3, uint32_t *data, uint32_t flag)
Tejas Patela3e34ad2019-02-01 17:25:19 +0530429{
Rajan Vaja030620d2020-11-23 04:13:54 -0800430 uint32_t ret;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700431 uint32_t version[PAYLOAD_ARG_CNT] = {0};
Tejas Patela3e34ad2019-02-01 17:25:19 +0530432 uint32_t payload[PAYLOAD_ARG_CNT];
Rajan Vaja030620d2020-11-23 04:13:54 -0800433 uint32_t fw_api_version;
Tejas Patela3e34ad2019-02-01 17:25:19 +0530434
435 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800436 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_QUERY_DATA, qid,
437 arg1, arg2, arg3);
Rajan Vaja030620d2020-11-23 04:13:54 -0800438
Tanmay Shahff34daa2021-08-09 11:00:41 -0700439 ret = pm_feature_check(PM_QUERY_DATA, &version[0], flag);
440 if (ret == PM_RET_SUCCESS) {
441 fw_api_version = version[0] & 0xFFFF;
442 if ((fw_api_version == 2U) &&
443 ((qid == XPM_QID_CLOCK_GET_NAME) ||
444 (qid == XPM_QID_PINCTRL_GET_FUNCTION_NAME))) {
445 ret = pm_ipi_send_sync(primary_proc, payload, data, PAYLOAD_ARG_CNT);
446 if (ret == PM_RET_SUCCESS) {
447 ret = data[0];
448 data[0] = data[1];
449 data[1] = data[2];
450 data[2] = data[3];
451 }
Rajan Vaja030620d2020-11-23 04:13:54 -0800452 } else {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700453 ret = pm_ipi_send_sync(primary_proc, payload, data, PAYLOAD_ARG_CNT);
Rajan Vaja030620d2020-11-23 04:13:54 -0800454 }
455 }
456 return ret;
Tejas Patela3e34ad2019-02-01 17:25:19 +0530457}
458/**
Tejas Patel9141f442019-01-10 03:03:48 -0800459 * pm_api_ioctl() - PM IOCTL API for device control and configs
Tanmay Shahff34daa2021-08-09 11:00:41 -0700460 *
461 * This API is deprecated and maintained here for backward compatibility.
462 * New use of this API should be avoided for versal platform.
463 * This API and its use cases will be removed for versal platform.
464 *
Tejas Patel9141f442019-01-10 03:03:48 -0800465 * @device_id Device ID
466 * @ioctl_id ID of the requested IOCTL
467 * @arg1 Argument 1 to requested IOCTL call
468 * @arg2 Argument 2 to requested IOCTL call
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600469 * @arg3 Argument 3 to requested IOCTL call
Tejas Patel9141f442019-01-10 03:03:48 -0800470 * @value Returned output value
Tejas Patel18072da2021-02-25 20:16:56 -0800471 * @flag 0 - Call from secure source
472 * 1 - Call from non-secure source
Tejas Patel9141f442019-01-10 03:03:48 -0800473 *
474 * This function calls IOCTL to firmware for device control and configuration.
475 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700476 * @return Returns status, either 0 on success or non-zero error code
477 * of type enum pm_ret_status
Tejas Patel9141f442019-01-10 03:03:48 -0800478 */
479enum pm_ret_status pm_api_ioctl(uint32_t device_id, uint32_t ioctl_id,
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600480 uint32_t arg1, uint32_t arg2, uint32_t arg3,
481 uint32_t *value, uint32_t flag)
Tejas Patel9141f442019-01-10 03:03:48 -0800482{
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700483 enum pm_ret_status ret;
Tejas Patel9141f442019-01-10 03:03:48 -0800484
485 switch (ioctl_id) {
486 case IOCTL_SET_PLL_FRAC_MODE:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700487 ret = pm_pll_set_mode(arg1, arg2, flag);
488 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800489 case IOCTL_GET_PLL_FRAC_MODE:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700490 ret = pm_pll_get_mode(arg1, value, flag);
491 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800492 case IOCTL_SET_PLL_FRAC_DATA:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700493 ret = pm_pll_set_param(arg1, PM_PLL_PARAM_DATA, arg2, flag);
494 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800495 case IOCTL_GET_PLL_FRAC_DATA:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700496 ret = pm_pll_get_param(arg1, PM_PLL_PARAM_DATA, value, flag);
497 break;
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600498 case IOCTL_SET_SGI:
499 /* Get the sgi number */
Venkatesh Yadav Abbarapuc8bbedc2021-04-19 07:49:57 -0600500 ret = pm_register_sgi(arg1, arg2);
501 if (ret != 0) {
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600502 return PM_RET_ERROR_ARGS;
503 }
504 gicd_write_irouter(gicv3_driver_data->gicd_base,
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530505 (uint32_t)PLAT_VERSAL_IPI_IRQ, MODE);
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700506 ret = PM_RET_SUCCESS;
507 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800508 default:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700509 return PM_RET_ERROR_NOTSUPPORTED;
Tejas Patel9141f442019-01-10 03:03:48 -0800510 }
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700511
512 return ret;
Tejas Patel9141f442019-01-10 03:03:48 -0800513}
Tejas Pateldb812052019-01-23 14:18:53 +0530514
515/**
516 * pm_set_wakeup_source() - PM call to specify the wakeup source while suspended
517 * @target Device id of the targeted PU or subsystem
518 * @wkup_node Device id of the wakeup peripheral
519 * @enable Enable or disable the specified peripheral as wake source
Tejas Patel18072da2021-02-25 20:16:56 -0800520 * @flag 0 - Call from secure source
521 * 1 - Call from non-secure source
Tejas Pateldb812052019-01-23 14:18:53 +0530522 *
523 * @return Returns status, either success or error+reason
524 */
525enum pm_ret_status pm_set_wakeup_source(uint32_t target, uint32_t wkup_device,
Tejas Patel18072da2021-02-25 20:16:56 -0800526 uint8_t enable, uint32_t flag)
Tejas Pateldb812052019-01-23 14:18:53 +0530527{
528 uint32_t payload[PAYLOAD_ARG_CNT];
529
Tejas Patel18072da2021-02-25 20:16:56 -0800530 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_SET_WAKEUP_SOURCE,
531 target, wkup_device, enable);
Abhyuday Godhasara7ae761b2021-06-24 05:50:44 -0700532 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Tejas Pateldb812052019-01-23 14:18:53 +0530533}
Ravi Patel22b0b492019-03-06 12:34:46 +0530534
535/**
536 * pm_feature_check() - Returns the supported API version if supported
537 * @api_id API ID to check
Tejas Patel18072da2021-02-25 20:16:56 -0800538 * @flag 0 - Call from secure source
539 * 1 - Call from non-secure source
Ronak Jain70d1c582022-02-04 00:42:55 -0800540 * @ret_payload pointer to array of PAYLOAD_ARG_CNT number of
541 * words Returned supported API version and bitmasks
542 * for IOCTL and QUERY ID
Ravi Patel22b0b492019-03-06 12:34:46 +0530543 *
544 * @return Returns status, either success or error+reason
545 */
Ronak Jain70d1c582022-02-04 00:42:55 -0800546enum pm_ret_status pm_feature_check(uint32_t api_id, uint32_t *ret_payload,
Tejas Patel18072da2021-02-25 20:16:56 -0800547 uint32_t flag)
Ravi Patel22b0b492019-03-06 12:34:46 +0530548{
Ronak Jain70d1c582022-02-04 00:42:55 -0800549 uint32_t payload[PAYLOAD_ARG_CNT];
Tanmay Shahff34daa2021-08-09 11:00:41 -0700550 uint32_t module_id;
551
Ronak Jain70d1c582022-02-04 00:42:55 -0800552 /* Return version of API which are implemented in ATF only */
Ravi Patel22b0b492019-03-06 12:34:46 +0530553 switch (api_id) {
554 case PM_GET_CALLBACK_DATA:
555 case PM_GET_TRUSTZONE_VERSION:
Ronak Jain70d1c582022-02-04 00:42:55 -0800556 ret_payload[0] = PM_API_VERSION_2;
557 return PM_RET_SUCCESS;
Rajan Vajaad1ffff2021-01-20 00:53:45 -0800558 case PM_LOAD_PDI:
Ronak Jain70d1c582022-02-04 00:42:55 -0800559 ret_payload[0] = PM_API_BASE_VERSION;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700560 return PM_RET_SUCCESS;
Ravi Patel22b0b492019-03-06 12:34:46 +0530561 default:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700562 break;
563 }
564
Ronak Jain70d1c582022-02-04 00:42:55 -0800565 module_id = (api_id & MODULE_ID_MASK) >> 8;
Ravi Patel22b0b492019-03-06 12:34:46 +0530566
Ronak Jain70d1c582022-02-04 00:42:55 -0800567 /*
568 * feature check should be done only for LIBPM module
569 * If module_id is 0, then we consider it LIBPM module as default id
570 */
571 if ((module_id > 0) && (module_id != LIBPM_MODULE_ID)) {
572 return PM_RET_SUCCESS;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700573 }
Ravi Patel22b0b492019-03-06 12:34:46 +0530574
Ronak Jain70d1c582022-02-04 00:42:55 -0800575 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag,
576 PM_FEATURE_CHECK, api_id);
577 return pm_ipi_send_sync(primary_proc, payload, ret_payload, PAYLOAD_ARG_CNT);
Ravi Patel22b0b492019-03-06 12:34:46 +0530578}
Jolly Shahed05a712019-03-22 05:33:39 +0530579
580/**
581 * pm_load_pdi() - Load the PDI
582 *
583 * This function provides support to load PDI from linux
584 *
585 * src: Source device of pdi(DDR, OCM, SD etc)
586 * address_low: lower 32-bit Linear memory space address
587 * address_high: higher 32-bit Linear memory space address
Tejas Patel18072da2021-02-25 20:16:56 -0800588 * @flag 0 - Call from secure source
589 * 1 - Call from non-secure source
Jolly Shahed05a712019-03-22 05:33:39 +0530590 *
591 * @return Returns status, either success or error+reason
592 */
Tejas Patel18072da2021-02-25 20:16:56 -0800593enum pm_ret_status pm_load_pdi(uint32_t src, uint32_t address_low,
594 uint32_t address_high, uint32_t flag)
Jolly Shahed05a712019-03-22 05:33:39 +0530595{
596 uint32_t payload[PAYLOAD_ARG_CNT];
597
598 /* Send request to the PMU */
Tejas Patel18072da2021-02-25 20:16:56 -0800599 PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, flag, PM_LOAD_PDI, src,
Jolly Shahed05a712019-03-22 05:33:39 +0530600 address_high, address_low);
601 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
602}
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700603
604/**
Tejas Patel42015552020-11-25 01:56:57 -0800605 * pm_register_notifier() - PM call to register a subsystem to be notified
606 * about the device event
607 * @device_id Device ID for the Node to which the event is related
608 * @event Event in question
609 * @wake Wake subsystem upon capturing the event if value 1
610 * @enable Enable the registration for value 1, disable for value 0
Tejas Patel18072da2021-02-25 20:16:56 -0800611 * @flag 0 - Call from secure source
612 * 1 - Call from non-secure source
Tejas Patel42015552020-11-25 01:56:57 -0800613 *
614 * @return Returns status, either success or error+reason
615 */
616enum pm_ret_status pm_register_notifier(uint32_t device_id, uint32_t event,
Tejas Patel18072da2021-02-25 20:16:56 -0800617 uint32_t wake, uint32_t enable,
618 uint32_t flag)
Tejas Patel42015552020-11-25 01:56:57 -0800619{
620 uint32_t payload[PAYLOAD_ARG_CNT];
621
622 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800623 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REGISTER_NOTIFIER,
Tejas Patel42015552020-11-25 01:56:57 -0800624 device_id, event, wake, enable);
625
626 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
627}