blob: 36ea8ed8cd88208e51cdc79aed82a057f6313a6c [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.
Jay Buddhabhatti26e138a2022-12-21 23:03:35 -08003 * Copyright (c) 2022-2023, 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
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080013#include <drivers/arm/gic_common.h>
14#include <lib/mmio.h>
15#include <lib/utils.h>
16#include <plat/common/platform.h>
17#include <platform_def.h>
18#include <pm_api_sys.h>
19#include <pm_client.h>
Tejas Patel9d09ff92019-01-08 01:46:35 -080020#include <pm_common.h>
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080021#include <pm_defs.h>
Tejas Patel9d09ff92019-01-08 01:46:35 -080022#include <pm_ipi.h>
Shubhrajyoti Dattaabf61222021-03-17 23:01:17 +053023#include "pm_svc_main.h"
Tejas Patel9d09ff92019-01-08 01:46:35 -080024
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080025#define NUM_GICD_ISENABLER ((IRQ_MAX >> 5U) + 1U)
26
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080027/* default shutdown/reboot scope is system(2) */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053028static uint32_t pm_shutdown_scope = XPM_SHUTDOWN_SUBTYPE_RST_SYSTEM;
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080029
30/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +053031 * pm_get_shutdown_scope() - Get the currently set shutdown scope.
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080032 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +053033 * Return: Shutdown scope value.
34 *
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080035 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053036uint32_t pm_get_shutdown_scope(void)
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080037{
38 return pm_shutdown_scope;
39}
40
Tejas Patel9d09ff92019-01-08 01:46:35 -080041/* PM API functions */
42
43/**
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080044 * pm_client_set_wakeup_sources - Set all devices with enabled interrupts as
45 * wake sources in the XilPM.
Prasad Kummari7d0623a2023-06-09 14:32:00 +053046 * @node_id: Node id of processor.
47 *
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080048 */
49void pm_client_set_wakeup_sources(uint32_t node_id)
50{
51 uint32_t reg_num, device_id;
52 uint8_t pm_wakeup_nodes_set[XPM_NODEIDX_DEV_MAX] = {0U};
Jay Buddhabhatti6da87942023-10-05 05:21:50 -070053 uint32_t isenabler1 = PLAT_ARM_GICD_BASE + GICD_ISENABLER + 4U;
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080054
55 zeromem(&pm_wakeup_nodes_set, (u_register_t)sizeof(pm_wakeup_nodes_set));
56
57 for (reg_num = 0U; reg_num < NUM_GICD_ISENABLER; reg_num++) {
58 uint32_t base_irq = reg_num << ISENABLER_SHIFT;
59 uint32_t reg = mmio_read_32(isenabler1 + (reg_num << 2));
60
61 if (reg == 0U) {
62 continue;
63 }
64
65 while (reg != 0U) {
66 enum pm_device_node_idx node_idx;
67 uint32_t idx, irq, lowest_set = reg & (-reg);
68 enum pm_ret_status ret;
69
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -080070 idx = (uint32_t)__builtin_ctz(lowest_set);
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080071 irq = base_irq + idx;
72
73 if (irq > IRQ_MAX) {
74 break;
75 }
76
77 node_idx = irq_to_pm_node_idx(irq);
78 reg &= ~lowest_set;
79
Jay Buddhabhattiac323fd2023-02-03 01:16:44 -080080 if (node_idx > XPM_NODEIDX_DEV_MIN) {
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080081 if (pm_wakeup_nodes_set[node_idx] == 0U) {
82 /* Get device ID from node index */
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -080083 device_id = PERIPH_DEVID((uint32_t)node_idx);
Jay Buddhabhattia63b3542023-02-28 02:22:02 -080084 ret = pm_set_wakeup_source(node_id,
85 device_id, 1U,
86 SECURE_FLAG);
87 pm_wakeup_nodes_set[node_idx] = (ret == PM_RET_SUCCESS) ?
88 1U : 0U;
89 }
90 }
91 }
92 }
93}
94
95/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +053096 * pm_handle_eemi_call() - PM call for processor to send eemi payload.
97 * @flag: 0 - Call from secure source.
98 * 1 - Call from non-secure source.
99 * @x0: Arguments received per SMC64 standard.
100 * @x1: Arguments received per SMC64 standard.
101 * @x2: Arguments received per SMC64 standard.
102 * @x3: Arguments received per SMC64 standard.
103 * @x4: Arguments received per SMC64 standard.
104 * @x5: Arguments received per SMC64 standard.
105 * @result: Payload received from firmware.
Tejas Patel9d09ff92019-01-08 01:46:35 -0800106 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530107 * Return: PM_RET_SUCCESS on success or error code.
108 *
Tejas Patel9d09ff92019-01-08 01:46:35 -0800109 */
Tanmay Shahff34daa2021-08-09 11:00:41 -0700110enum pm_ret_status pm_handle_eemi_call(uint32_t flag, uint32_t x0, uint32_t x1,
111 uint32_t x2, uint32_t x3, uint32_t x4,
112 uint32_t x5, uint64_t *result)
Tejas Patel9d09ff92019-01-08 01:46:35 -0800113{
Tanmay Shahff34daa2021-08-09 11:00:41 -0700114 uint32_t payload[PAYLOAD_ARG_CNT] = {0};
115 uint32_t module_id;
Tejas Patel9d09ff92019-01-08 01:46:35 -0800116
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530117 module_id = (x0 & MODULE_ID_MASK) >> 8U;
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800118
Tanmay Shahff34daa2021-08-09 11:00:41 -0700119 //default module id is for LIBPM
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530120 if (module_id == 0) {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700121 module_id = LIBPM_MODULE_ID;
Venkatesh Yadav Abbarapuab167132022-05-25 15:18:24 +0530122 }
Ravi Patel476b5b12019-08-12 03:10:10 -0700123
Tanmay Shahff34daa2021-08-09 11:00:41 -0700124 PM_PACK_PAYLOAD6(payload, module_id, flag, x0, x1, x2, x3, x4, x5);
125 return pm_ipi_send_sync(primary_proc, payload, (uint32_t *)result, PAYLOAD_ARG_CNT);
Ravi Patel476b5b12019-08-12 03:10:10 -0700126}
127
128/**
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800129 * pm_self_suspend() - PM call for processor to suspend itself
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530130 * @nid: Node id of the processor or subsystem.
131 * @latency: Requested maximum wakeup latency (not supported).
132 * @state: Requested state.
133 * @address: Resume address.
134 * @flag: 0 - Call from secure source.
135 * 1 - Call from non-secure source.
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800136 *
137 * This is a blocking call, it will return only once PMU has responded.
138 * On a wakeup, resume address will be automatically set by PMU.
139 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530140 * Return: Returns status, either success or error+reason.
141 *
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800142 */
143enum pm_ret_status pm_self_suspend(uint32_t nid,
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530144 uint32_t latency,
145 uint32_t state,
Tejas Patel18072da2021-02-25 20:16:56 -0800146 uintptr_t address, uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800147{
148 uint32_t payload[PAYLOAD_ARG_CNT];
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530149 uint32_t cpuid = plat_my_core_pos();
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800150 const struct pm_proc *proc = pm_get_proc(cpuid);
151
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700152 if (proc == NULL) {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800153 WARN("Failed to get proc %d\n", cpuid);
154 return PM_RET_ERROR_INTERNAL;
155 }
156
157 /*
158 * Do client specific suspend operations
159 * (e.g. set powerdown request bit)
160 */
161 pm_client_suspend(proc, state);
162
163 /* Send request to the PLM */
Tejas Patel18072da2021-02-25 20:16:56 -0800164 PM_PACK_PAYLOAD6(payload, LIBPM_MODULE_ID, flag, PM_SELF_SUSPEND,
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800165 proc->node_id, latency, state, address,
166 (address >> 32));
167 return pm_ipi_send_sync(proc, payload, NULL, 0);
168}
169
170/**
171 * pm_abort_suspend() - PM call to announce that a prior suspend request
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530172 * is to be aborted.
173 * @reason: Reason for the abort.
174 * @flag: 0 - Call from secure source.
175 * 1 - Call from non-secure source.
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800176 *
177 * Calling PU expects the PMU to abort the initiated suspend procedure.
178 * This is a non-blocking call without any acknowledge.
179 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530180 * Return: Returns status, either success or error+reason.
181 *
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800182 */
Tejas Patel18072da2021-02-25 20:16:56 -0800183enum pm_ret_status pm_abort_suspend(enum pm_abort_reason reason, uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800184{
185 uint32_t payload[PAYLOAD_ARG_CNT];
186
187 /*
188 * Do client specific abort suspend operations
189 * (e.g. enable interrupts and clear powerdown request bit)
190 */
191 pm_client_abort_suspend();
192
193 /* Send request to the PLM */
Tejas Patel18072da2021-02-25 20:16:56 -0800194 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_ABORT_SUSPEND,
195 reason, primary_proc->node_id);
Abhyuday Godhasara7ae761b2021-06-24 05:50:44 -0700196 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800197}
198
199/**
200 * pm_req_suspend() - PM call to request for another PU or subsystem to
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530201 * be suspended gracefully.
202 * @target: Node id of the targeted PU or subsystem.
203 * @ack: Flag to specify whether acknowledge is requested.
204 * @latency: Requested wakeup latency (not supported)
205 * @state: Requested state (not supported).
206 * @flag: 0 - Call from secure source.
207 * 1 - Call from non-secure source.
208 *
209 * Return: Returns status, either success or error+reason.
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800210 *
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800211 */
212enum pm_ret_status pm_req_suspend(uint32_t target, uint8_t ack,
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530213 uint32_t latency, uint32_t state,
Tejas Patel18072da2021-02-25 20:16:56 -0800214 uint32_t flag)
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800215{
216 uint32_t payload[PAYLOAD_ARG_CNT];
217
218 /* Send request to the PMU */
Tejas Patel18072da2021-02-25 20:16:56 -0800219 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_REQ_SUSPEND, target,
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800220 latency, state);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700221 if (ack == IPI_BLOCKING) {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800222 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700223 } else {
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800224 return pm_ipi_send(primary_proc, payload);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700225 }
Tejas Patelfe0e10a2019-12-08 23:29:44 -0800226}
Tejas Patel41f3e0b2019-01-08 01:46:37 -0800227
228/**
Tejas Patel49cd8712019-01-23 14:18:51 +0530229 * pm_req_wakeup() - PM call for processor to wake up selected processor
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530230 * or subsystem.
231 * @target: Device ID of the processor or subsystem to wake up.
232 * @set_address: Resume address presence indicator.
233 * 1 - resume address specified, 0 - otherwise.
234 * @address: Resume address.
235 * @ack: Flag to specify whether acknowledge requested.
236 * @flag: 0 - Call from secure source.
237 * 1 - Call from non-secure source.
Tejas Patel49cd8712019-01-23 14:18:51 +0530238 *
239 * This API function is either used to power up another APU core for SMP
240 * (by PSCI) or to power up an entirely different PU or subsystem, such
241 * as RPU0, RPU, or PL_CORE_xx. Resume address for the target PU will be
242 * automatically set by PMC.
243 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530244 * Return: Returns status, either success or error+reason.
245 *
Tejas Patel49cd8712019-01-23 14:18:51 +0530246 */
247enum pm_ret_status pm_req_wakeup(uint32_t target, uint32_t set_address,
Tejas Patel18072da2021-02-25 20:16:56 -0800248 uintptr_t address, uint8_t ack, uint32_t flag)
Tejas Patel49cd8712019-01-23 14:18:51 +0530249{
250 uint32_t payload[PAYLOAD_ARG_CNT];
251
252 /* Send request to the PMC to perform the wake of the PU */
Tejas Patel18072da2021-02-25 20:16:56 -0800253 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REQ_WAKEUP, target,
Tejas Patel49cd8712019-01-23 14:18:51 +0530254 set_address, address, ack);
255
256 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
257}
258
259/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530260 * pm_get_callbackdata() - Read from IPI response buffer.
261 * @data: array of PAYLOAD_ARG_CNT elements.
262 * @count: Number of values to return.
263 * @flag: 0 - Call from secure source.
264 * 1 - Call from non-secure source.
265 * @ack: 0 - Do not ack IPI after reading payload.
266 * 1 - Ack IPI after reading payload.
Rajan Vaja649e9242019-03-04 11:09:40 +0530267 *
268 * Read value from ipi buffer response buffer.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530269 * Return: Returns status, either success or error.
270 *
Rajan Vaja649e9242019-03-04 11:09:40 +0530271 */
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530272enum pm_ret_status pm_get_callbackdata(uint32_t *data, size_t count, uint32_t flag, uint32_t ack)
Rajan Vaja649e9242019-03-04 11:09:40 +0530273{
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530274 enum pm_ret_status ret = PM_RET_SUCCESS;
Rajan Vaja649e9242019-03-04 11:09:40 +0530275 /* Return if interrupt is not from PMU */
Abhyuday Godhasara44877942021-08-05 02:59:26 -0700276 if (pm_ipi_irq_status(primary_proc) == 0) {
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530277 return ret;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700278 }
Rajan Vaja649e9242019-03-04 11:09:40 +0530279
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530280 ret = pm_ipi_buff_read_callb(data, count);
Tanmay Shahfdae9e82022-08-26 15:06:00 -0700281
282 if (ack != 0U) {
283 pm_ipi_irq_clear(primary_proc);
284 }
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530285
286 return ret;
Rajan Vaja649e9242019-03-04 11:09:40 +0530287}
288
289/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530290 * pm_pll_set_param() - Set PLL parameter.
291 * @clk_id: PLL clock ID.
292 * @param: PLL parameter ID.
293 * @value: Value to set for PLL parameter.
294 * @flag: 0 - Call from secure source.
295 * 1 - Call from non-secure source.
Tejas Patel1f56fb12019-01-08 01:46:40 -0800296 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700297 * This API is deprecated and maintained here for backward compatibility.
298 * New use of this API should be avoided for versal platform.
299 * This API and its use cases will be removed for versal platform.
Tejas Patel84275bf2020-09-01 04:43:53 -0700300 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530301 * Return: Returns status, either success or error+reason.
Tejas Patel023116d2019-01-08 01:46:41 -0800302 *
Tejas Patel023116d2019-01-08 01:46:41 -0800303 */
304enum pm_ret_status pm_pll_set_param(uint32_t clk_id, uint32_t param,
Tejas Patel18072da2021-02-25 20:16:56 -0800305 uint32_t value, uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800306{
307 uint32_t payload[PAYLOAD_ARG_CNT];
308
309 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800310 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_PLL_SET_PARAMETER,
311 clk_id, param, value);
Tejas Patel023116d2019-01-08 01:46:41 -0800312
313 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
314}
315
316/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530317 * pm_pll_get_param() - Get PLL parameter value.
318 * @clk_id: PLL clock ID.
319 * @param: PLL parameter ID.
320 * @value: Buffer to store PLL parameter value.
321 * @flag: 0 - Call from secure source.
322 * 1 - Call from non-secure source.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700323 *
324 * This API is deprecated and maintained here for backward compatibility.
325 * New use of this API should be avoided for versal platform.
326 * This API and its use cases will be removed for versal platform.
327 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530328 * Return: Returns status, either success or error+reason.
Tejas Patel023116d2019-01-08 01:46:41 -0800329 *
Tejas Patel023116d2019-01-08 01:46:41 -0800330 */
331enum pm_ret_status pm_pll_get_param(uint32_t clk_id, uint32_t param,
Tejas Patel18072da2021-02-25 20:16:56 -0800332 uint32_t *value, uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -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_PLL_GET_PARAMETER,
338 clk_id, param);
Tejas Patel023116d2019-01-08 01:46:41 -0800339
340 return pm_ipi_send_sync(primary_proc, payload, value, 1);
341}
342
343/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530344 * pm_pll_set_mode() - Set PLL mode.
345 * @clk_id: PLL clock ID.
346 * @mode: PLL mode.
347 * @flag: 0 - Call from secure source.
348 * 1 - Call from non-secure source.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700349 *
350 * This API is deprecated and maintained here for backward compatibility.
351 * New use of this API should be avoided for versal platform.
352 * This API and its use cases will be removed for versal platform.
353 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530354 * Return: Returns status, either success or error+reason.
Tejas Patel023116d2019-01-08 01:46:41 -0800355 *
Tejas Patel023116d2019-01-08 01:46:41 -0800356 */
Tejas Patel18072da2021-02-25 20:16:56 -0800357enum pm_ret_status pm_pll_set_mode(uint32_t clk_id, uint32_t mode,
358 uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800359{
360 uint32_t payload[PAYLOAD_ARG_CNT];
361
362 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800363 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_PLL_SET_MODE,
364 clk_id, mode);
Tejas Patel023116d2019-01-08 01:46:41 -0800365
366 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
367}
368
369/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530370 * pm_pll_get_mode() - Get PLL mode.
371 * @clk_id: PLL clock ID.
372 * @mode: Buffer to store PLL mode.
373 * @flag: 0 - Call from secure source.
374 * 1 - Call from non-secure source.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700375 *
376 * This API is deprecated and maintained here for backward compatibility.
377 * New use of this API should be avoided for versal platform.
378 * This API and its use cases will be removed for versal platform.
379 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530380 * Return: Returns status, either success or error+reason.
Tejas Patel023116d2019-01-08 01:46:41 -0800381 *
Tejas Patel023116d2019-01-08 01:46:41 -0800382 */
Tejas Patel18072da2021-02-25 20:16:56 -0800383enum pm_ret_status pm_pll_get_mode(uint32_t clk_id, uint32_t *mode,
384 uint32_t flag)
Tejas Patel023116d2019-01-08 01:46:41 -0800385{
386 uint32_t payload[PAYLOAD_ARG_CNT];
387
388 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800389 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_PLL_GET_MODE,
390 clk_id);
Tejas Patel023116d2019-01-08 01:46:41 -0800391
392 return pm_ipi_send_sync(primary_proc, payload, mode, 1);
393}
Tejas Patel6b282252019-01-10 03:03:47 -0800394
395/**
396 * pm_force_powerdown() - PM call to request for another PU or subsystem to
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530397 * be powered down forcefully.
398 * @target: Device ID of the PU node to be forced powered down.
399 * @ack: Flag to specify whether acknowledge is requested
400 * @flag: 0 - Call from secure source
401 * 1 - Call from non-secure source
Tejas Patel6b282252019-01-10 03:03:47 -0800402 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530403 * Return: Returns status, either success or error+reason.
404 *
Tejas Patel6b282252019-01-10 03:03:47 -0800405 */
Tejas Patel18072da2021-02-25 20:16:56 -0800406enum pm_ret_status pm_force_powerdown(uint32_t target, uint8_t ack,
407 uint32_t flag)
Tejas Patel6b282252019-01-10 03:03:47 -0800408{
409 uint32_t payload[PAYLOAD_ARG_CNT];
410
411 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800412 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_FORCE_POWERDOWN,
413 target, ack);
Tejas Patel6b282252019-01-10 03:03:47 -0800414
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700415 if (ack == IPI_BLOCKING) {
Tejas Patel6b282252019-01-10 03:03:47 -0800416 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700417 } else {
Tejas Patel6b282252019-01-10 03:03:47 -0800418 return pm_ipi_send(primary_proc, payload);
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700419 }
Tejas Patel6b282252019-01-10 03:03:47 -0800420}
421
422/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530423 * pm_system_shutdown() - PM call to request a system shutdown or restart.
424 * @type: Shutdown or restart? 0=shutdown, 1=restart, 2=setscope.
425 * @subtype: Scope: 0=APU-subsystem, 1=PS, 2=system.
426 * @flag: 0 - Call from secure source.
427 * 1 - Call from non-secure source.
428 *
429 * Return: Returns status, either success or error+reason.
Tejas Patel6b282252019-01-10 03:03:47 -0800430 *
Tejas Patel6b282252019-01-10 03:03:47 -0800431 */
Tejas Patel18072da2021-02-25 20:16:56 -0800432enum pm_ret_status pm_system_shutdown(uint32_t type, uint32_t subtype,
433 uint32_t flag)
Tejas Patel6b282252019-01-10 03:03:47 -0800434{
435 uint32_t payload[PAYLOAD_ARG_CNT];
436
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800437 if (type == XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY) {
438 /* Setting scope for subsequent PSCI reboot or shutdown */
439 pm_shutdown_scope = subtype;
440 return PM_RET_SUCCESS;
441 }
442
Tejas Patel6b282252019-01-10 03:03:47 -0800443 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800444 PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SYSTEM_SHUTDOWN,
445 type, subtype);
Tejas Patel6b282252019-01-10 03:03:47 -0800446
447 return pm_ipi_send_non_blocking(primary_proc, payload);
448}
Tejas Patel9141f442019-01-10 03:03:48 -0800449
450/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530451 * pm_query_data() - PM API for querying firmware data.
452 * @qid: The type of data to query.
453 * @arg1: Argument 1 to requested query data call.
454 * @arg2: Argument 2 to requested query data call.
455 * @arg3: Argument 3 to requested query data call.
456 * @data: Returned output data.
457 * @flag: 0 - Call from secure source.
458 * 1 - Call from non-secure source.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700459 *
460 * This API is deprecated and maintained here for backward compatibility.
461 * New use of this API should be avoided for versal platform.
462 * This API and its use cases will be removed for versal platform.
463 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530464 * Return: 0 if success else non-zero error code of type
465 * enum pm_ret_status.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700466 *
Tanmay Shahff34daa2021-08-09 11:00:41 -0700467 */
Tejas Patela3e34ad2019-02-01 17:25:19 +0530468enum pm_ret_status pm_query_data(uint32_t qid, uint32_t arg1, uint32_t arg2,
Tejas Patel18072da2021-02-25 20:16:56 -0800469 uint32_t arg3, uint32_t *data, uint32_t flag)
Tejas Patela3e34ad2019-02-01 17:25:19 +0530470{
Rajan Vaja030620d2020-11-23 04:13:54 -0800471 uint32_t ret;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700472 uint32_t version[PAYLOAD_ARG_CNT] = {0};
Tejas Patela3e34ad2019-02-01 17:25:19 +0530473 uint32_t payload[PAYLOAD_ARG_CNT];
Rajan Vaja030620d2020-11-23 04:13:54 -0800474 uint32_t fw_api_version;
Tejas Patela3e34ad2019-02-01 17:25:19 +0530475
476 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800477 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_QUERY_DATA, qid,
478 arg1, arg2, arg3);
Rajan Vaja030620d2020-11-23 04:13:54 -0800479
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800480 ret = pm_feature_check((uint32_t)PM_QUERY_DATA, &version[0], flag);
Tanmay Shahff34daa2021-08-09 11:00:41 -0700481 if (ret == PM_RET_SUCCESS) {
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530482 fw_api_version = version[0] & 0xFFFFU;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700483 if ((fw_api_version == 2U) &&
484 ((qid == XPM_QID_CLOCK_GET_NAME) ||
485 (qid == XPM_QID_PINCTRL_GET_FUNCTION_NAME))) {
486 ret = pm_ipi_send_sync(primary_proc, payload, data, PAYLOAD_ARG_CNT);
487 if (ret == PM_RET_SUCCESS) {
488 ret = data[0];
489 data[0] = data[1];
490 data[1] = data[2];
491 data[2] = data[3];
492 }
Rajan Vaja030620d2020-11-23 04:13:54 -0800493 } else {
Tanmay Shahff34daa2021-08-09 11:00:41 -0700494 ret = pm_ipi_send_sync(primary_proc, payload, data, PAYLOAD_ARG_CNT);
Rajan Vaja030620d2020-11-23 04:13:54 -0800495 }
496 }
497 return ret;
Tejas Patela3e34ad2019-02-01 17:25:19 +0530498}
499/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530500 * pm_api_ioctl() - PM IOCTL API for device control and configs.
501 * @device_id: Device ID.
502 * @ioctl_id: ID of the requested IOCTL.
503 * @arg1: Argument 1 to requested IOCTL call.
504 * @arg2: Argument 2 to requested IOCTL call.
505 * @arg3: Argument 3 to requested IOCTL call.
506 * @value: Returned output value.
507 * @flag: 0 - Call from secure source.
508 * 1 - Call from non-secure source.
Tanmay Shahff34daa2021-08-09 11:00:41 -0700509 *
510 * This API is deprecated and maintained here for backward compatibility.
511 * New use of this API should be avoided for versal platform.
512 * This API and its use cases will be removed for versal platform.
513 *
Tejas Patel9141f442019-01-10 03:03:48 -0800514 * This function calls IOCTL to firmware for device control and configuration.
515 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530516 * Return: Returns status, either 0 on success or non-zero error code
517 * of type enum pm_ret_status.
518 *
Tejas Patel9141f442019-01-10 03:03:48 -0800519 */
520enum pm_ret_status pm_api_ioctl(uint32_t device_id, uint32_t ioctl_id,
Venkatesh Yadav Abbarapu4b233392021-10-20 22:11:53 -0600521 uint32_t arg1, uint32_t arg2, uint32_t arg3,
522 uint32_t *value, uint32_t flag)
Tejas Patel9141f442019-01-10 03:03:48 -0800523{
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700524 enum pm_ret_status ret;
Tejas Patel9141f442019-01-10 03:03:48 -0800525
526 switch (ioctl_id) {
527 case IOCTL_SET_PLL_FRAC_MODE:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700528 ret = pm_pll_set_mode(arg1, arg2, flag);
529 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800530 case IOCTL_GET_PLL_FRAC_MODE:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700531 ret = pm_pll_get_mode(arg1, value, flag);
532 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800533 case IOCTL_SET_PLL_FRAC_DATA:
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800534 ret = pm_pll_set_param(arg1, (uint32_t)PM_PLL_PARAM_DATA, arg2, flag);
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700535 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800536 case IOCTL_GET_PLL_FRAC_DATA:
Jay Buddhabhattif2f84b32023-02-09 22:56:53 -0800537 ret = pm_pll_get_param(arg1, (uint32_t)PM_PLL_PARAM_DATA, value, flag);
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700538 break;
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600539 case IOCTL_SET_SGI:
540 /* Get the sgi number */
Venkatesh Yadav Abbarapuc8bbedc2021-04-19 07:49:57 -0600541 ret = pm_register_sgi(arg1, arg2);
542 if (ret != 0) {
Venkatesh Yadav Abbarapuaa4898a2021-03-31 03:07:40 -0600543 return PM_RET_ERROR_ARGS;
544 }
Michal Simek89e3c292022-08-04 14:08:32 +0200545 ret = PM_RET_SUCCESS;
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700546 break;
Tejas Patel9141f442019-01-10 03:03:48 -0800547 default:
Tanmay Shahff34daa2021-08-09 11:00:41 -0700548 return PM_RET_ERROR_NOTSUPPORTED;
Tejas Patel9141f442019-01-10 03:03:48 -0800549 }
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700550
551 return ret;
Tejas Patel9141f442019-01-10 03:03:48 -0800552}
Tejas Pateldb812052019-01-23 14:18:53 +0530553
554/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530555 * pm_set_wakeup_source() - PM call to specify the wakeup source while
556 * suspended.
557 * @target: Device id of the targeted PU or subsystem
558 * @wkup_device: Device id of the wakeup peripheral
559 * @enable: Enable or disable the specified peripheral as wake source
560 * @flag: 0 - Call from secure source
561 * 1 - Call from non-secure source
Tejas Pateldb812052019-01-23 14:18:53 +0530562 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530563 * Return: Returns status, either success or error+reason.
564 *
Tejas Pateldb812052019-01-23 14:18:53 +0530565 */
566enum pm_ret_status pm_set_wakeup_source(uint32_t target, uint32_t wkup_device,
Tejas Patel18072da2021-02-25 20:16:56 -0800567 uint8_t enable, uint32_t flag)
Tejas Pateldb812052019-01-23 14:18:53 +0530568{
569 uint32_t payload[PAYLOAD_ARG_CNT];
570
Tejas Patel18072da2021-02-25 20:16:56 -0800571 PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_SET_WAKEUP_SOURCE,
572 target, wkup_device, enable);
Abhyuday Godhasara7ae761b2021-06-24 05:50:44 -0700573 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
Tejas Pateldb812052019-01-23 14:18:53 +0530574}
Ravi Patel22b0b492019-03-06 12:34:46 +0530575
576/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530577 * pm_feature_check() - Returns the supported API version if supported.
578 * @api_id: API ID to check.
579 * @flag: 0 - Call from secure source.
580 * 1 - Call from non-secure source.
581 * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of
582 * words Returned supported API version and bitmasks
583 * for IOCTL and QUERY ID
Ravi Patel22b0b492019-03-06 12:34:46 +0530584 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530585 * Return: Returns status, either success or error+reason.
586 *
Ravi Patel22b0b492019-03-06 12:34:46 +0530587 */
Ronak Jain70d1c582022-02-04 00:42:55 -0800588enum pm_ret_status pm_feature_check(uint32_t api_id, uint32_t *ret_payload,
Tejas Patel18072da2021-02-25 20:16:56 -0800589 uint32_t flag)
Ravi Patel22b0b492019-03-06 12:34:46 +0530590{
Ronak Jain70d1c582022-02-04 00:42:55 -0800591 uint32_t payload[PAYLOAD_ARG_CNT];
Tanmay Shahff34daa2021-08-09 11:00:41 -0700592 uint32_t module_id;
593
Prasad Kummarie0783112023-04-26 11:02:07 +0530594 /* Return version of API which are implemented in TF-A only */
Ravi Patel22b0b492019-03-06 12:34:46 +0530595 switch (api_id) {
596 case PM_GET_CALLBACK_DATA:
597 case PM_GET_TRUSTZONE_VERSION:
Ronak Jain70d1c582022-02-04 00:42:55 -0800598 ret_payload[0] = PM_API_VERSION_2;
599 return PM_RET_SUCCESS;
Tanmay Shah5e3af332022-08-26 15:13:48 -0700600 case TF_A_PM_REGISTER_SGI:
Ronak Jain70d1c582022-02-04 00:42:55 -0800601 ret_payload[0] = PM_API_BASE_VERSION;
Tanmay Shahff34daa2021-08-09 11:00:41 -0700602 return PM_RET_SUCCESS;
Ravi Patel22b0b492019-03-06 12:34:46 +0530603 default:
Abhyuday Godhasara95374b42021-08-05 07:07:35 -0700604 break;
605 }
606
Venkatesh Yadav Abbarapua0657d92022-07-20 09:03:22 +0530607 module_id = (api_id & MODULE_ID_MASK) >> 8U;
Ravi Patel22b0b492019-03-06 12:34:46 +0530608
Ronak Jain70d1c582022-02-04 00:42:55 -0800609 /*
610 * feature check should be done only for LIBPM module
611 * If module_id is 0, then we consider it LIBPM module as default id
612 */
613 if ((module_id > 0) && (module_id != LIBPM_MODULE_ID)) {
614 return PM_RET_SUCCESS;
Abhyuday Godhasaraddc46622021-08-05 03:14:17 -0700615 }
Ravi Patel22b0b492019-03-06 12:34:46 +0530616
Ronak Jain70d1c582022-02-04 00:42:55 -0800617 PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag,
618 PM_FEATURE_CHECK, api_id);
619 return pm_ipi_send_sync(primary_proc, payload, ret_payload, PAYLOAD_ARG_CNT);
Ravi Patel22b0b492019-03-06 12:34:46 +0530620}
Jolly Shahed05a712019-03-22 05:33:39 +0530621
622/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530623 * pm_load_pdi() - Load the PDI. This function provides support to load
624 * PDI from linux.
Jolly Shahed05a712019-03-22 05:33:39 +0530625 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530626 * @src: Source device of pdi(DDR, OCM, SD etc).
627 * @address_low: lower 32-bit Linear memory space address.
628 * @address_high: higher 32-bit Linear memory space address.
629 * @flag: 0 - Call from secure source.
630 * 1 - Call from non-secure source.
Jolly Shahed05a712019-03-22 05:33:39 +0530631 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530632 * Return: Returns status, either success or error+reason.
Jolly Shahed05a712019-03-22 05:33:39 +0530633 *
Jolly Shahed05a712019-03-22 05:33:39 +0530634 */
Tejas Patel18072da2021-02-25 20:16:56 -0800635enum pm_ret_status pm_load_pdi(uint32_t src, uint32_t address_low,
636 uint32_t address_high, uint32_t flag)
Jolly Shahed05a712019-03-22 05:33:39 +0530637{
638 uint32_t payload[PAYLOAD_ARG_CNT];
639
640 /* Send request to the PMU */
Tejas Patel18072da2021-02-25 20:16:56 -0800641 PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, flag, PM_LOAD_PDI, src,
Jolly Shahed05a712019-03-22 05:33:39 +0530642 address_high, address_low);
643 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
644}
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700645
646/**
Tejas Patel42015552020-11-25 01:56:57 -0800647 * pm_register_notifier() - PM call to register a subsystem to be notified
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530648 * about the device event.
649 * @device_id: Device ID for the Node to which the event is related.
650 * @event: Event in question.
651 * @wake: Wake subsystem upon capturing the event if value 1.
652 * @enable: Enable the registration for value 1, disable for value 0.
653 * @flag: 0 - Call from secure source.
654 * 1 - Call from non-secure source.
Tejas Patel42015552020-11-25 01:56:57 -0800655 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530656 * Return: Returns status, either success or error+reason.
657 *
Tejas Patel42015552020-11-25 01:56:57 -0800658 */
659enum pm_ret_status pm_register_notifier(uint32_t device_id, uint32_t event,
Tejas Patel18072da2021-02-25 20:16:56 -0800660 uint32_t wake, uint32_t enable,
661 uint32_t flag)
Tejas Patel42015552020-11-25 01:56:57 -0800662{
663 uint32_t payload[PAYLOAD_ARG_CNT];
664
665 /* Send request to the PMC */
Tejas Patel18072da2021-02-25 20:16:56 -0800666 PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REGISTER_NOTIFIER,
Tejas Patel42015552020-11-25 01:56:57 -0800667 device_id, event, wake, enable);
668
669 return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
670}
Akshay Belsareec1181c2023-04-03 10:51:14 +0530671
672/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530673 * pm_get_chipid() - Read silicon ID registers.
674 * @value: Buffer for two 32bit words.
Akshay Belsareec1181c2023-04-03 10:51:14 +0530675 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530676 * Return: Returns status, either success or error+reason and,
677 * optionally, @value.
Akshay Belsareec1181c2023-04-03 10:51:14 +0530678 */
679enum pm_ret_status pm_get_chipid(uint32_t *value)
680{
681 uint32_t payload[PAYLOAD_ARG_CNT];
682
683 PM_PACK_PAYLOAD1(payload, LIBPM_MODULE_ID, SECURE_FLAG, PM_GET_CHIPID);
684
685 return pm_ipi_send_sync(primary_proc, payload, value, 2);
686}