xref: /rk3399_ARM-atf/plat/xilinx/common/pm_service/pm_api_sys.c (revision 7e8b7096d12cb72f6ee5c24804d4da81d0595dc9)
1 /*
2  * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
3  * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
4  *
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 <common/ep_info.h>
14 #include <common/debug.h>
15 #include <drivers/arm/gic_common.h>
16 #include <lib/mmio.h>
17 #include <lib/utils.h>
18 #include <plat/common/platform.h>
19 #include <platform_def.h>
20 #include <pm_api_sys.h>
21 #include <pm_client.h>
22 #include <pm_common.h>
23 #include <pm_defs.h>
24 #include <pm_ipi.h>
25 #include "pm_svc_main.h"
26 
27 #define NUM_GICD_ISENABLER	((IRQ_MAX >> 5U) + 1U)
28 
29 /* default shutdown/reboot scope is system(2) */
30 static uint32_t pm_shutdown_scope = XPM_SHUTDOWN_SUBTYPE_RST_SYSTEM;
31 
32 /**
33  * pm_get_shutdown_scope() - Get the currently set shutdown scope.
34  *
35  * Return: Shutdown scope value.
36  *
37  */
38 uint32_t pm_get_shutdown_scope(void)
39 {
40 	return pm_shutdown_scope;
41 }
42 
43 /* PM API functions */
44 
45 /**
46  * pm_client_set_wakeup_sources - Set all devices with enabled interrupts as
47  *                                wake sources in the XilPM.
48  * @node_id: Node id of processor.
49  * @flag: 0 - Call from secure source.
50  *	  1 - Call from non-secure source.
51  *
52  */
53 void pm_client_set_wakeup_sources(uint32_t node_id, uint32_t flag)
54 {
55 	uint32_t reg_num, device_id;
56 	uint8_t pm_wakeup_nodes_set[XPM_NODEIDX_DEV_MAX] = {0U};
57 	uint32_t isenabler1 = PLAT_ARM_GICD_BASE + GICD_ISENABLER + 4U;
58 
59 	zeromem(&pm_wakeup_nodes_set, (u_register_t)sizeof(pm_wakeup_nodes_set));
60 
61 	for (reg_num = 0U; reg_num < NUM_GICD_ISENABLER; reg_num++) {
62 		uint32_t base_irq = reg_num << ISENABLER_SHIFT;
63 		uint32_t reg = mmio_read_32((uint64_t)(isenabler1 + (reg_num << 2)));
64 
65 		if (reg == 0U) {
66 			continue;
67 		}
68 
69 		while (reg != 0U) {
70 			enum pm_device_node_idx node_idx;
71 			uint32_t idx, irq, lowest_set = reg & (-reg);
72 			enum pm_ret_status ret;
73 
74 			idx = (uint32_t)__builtin_ctz(lowest_set);
75 			irq = base_irq + idx;
76 
77 			if (irq > IRQ_MAX) {
78 				break;
79 			}
80 
81 			node_idx = irq_to_pm_node_idx(irq);
82 			reg &= ~lowest_set;
83 
84 			if (node_idx > XPM_NODEIDX_DEV_MIN) {
85 				if (pm_wakeup_nodes_set[node_idx] == 0U) {
86 					/* Get device ID from node index */
87 					device_id = PERIPH_DEVID((uint32_t)node_idx);
88 					ret = pm_set_wakeup_source(node_id,
89 								   device_id, 1U,
90 								   flag);
91 					pm_wakeup_nodes_set[node_idx] = (ret == PM_RET_SUCCESS) ?
92 										 1U : 0U;
93 				}
94 			}
95 		}
96 	}
97 }
98 
99 /**
100  * pm_handle_eemi_call() - PM call for processor to send eemi payload.
101  * @flag: 0 - Call from secure source.
102  *        1 - Call from non-secure source.
103  * @x0: Arguments received per SMC64 standard.
104  * @x1: Arguments received per SMC64 standard.
105  * @x2: Arguments received per SMC64 standard.
106  * @x3: Arguments received per SMC64 standard.
107  * @x4: Arguments received per SMC64 standard.
108  * @x5: Arguments received per SMC64 standard.
109  * @result: Payload received from firmware.
110  *
111  * Return: PM_RET_SUCCESS on success or error code.
112  *
113  */
114 enum pm_ret_status pm_handle_eemi_call(uint32_t flag, uint32_t x0, uint32_t x1,
115 				       uint32_t x2, uint32_t x3, uint32_t x4,
116 				       uint32_t x5, uint32_t *result)
117 {
118 	uint32_t payload[PAYLOAD_ARG_CNT] = {0};
119 	uint32_t module_id;
120 
121 	module_id = (x0 & MODULE_ID_MASK) >> 8U;
122 
123 	//default module id is for LIBPM
124 	if (module_id == 0U) {
125 		module_id = LIBPM_MODULE_ID;
126 	}
127 
128 	PM_PACK_PAYLOAD6(payload, module_id, flag, x0, x1, x2, x3, x4, x5);
129 	return pm_ipi_send_sync(primary_proc, payload, result, RET_PAYLOAD_ARG_CNT);
130 }
131 
132 /**
133  * pm_self_suspend() - PM call for processor to suspend itself
134  * @nid: Node id of the processor or subsystem.
135  * @latency: Requested maximum wakeup latency (not supported).
136  * @state: Requested state.
137  * @address: Resume address.
138  * @flag: 0 - Call from secure source.
139  *        1 - Call from non-secure source.
140  *
141  * This is a blocking call, it will return only once PMU has responded.
142  * On a wakeup, resume address will be automatically set by PMU.
143  *
144  * Return: Returns status, either success or error+reason.
145  *
146  */
147 enum pm_ret_status pm_self_suspend(uint32_t nid,
148 				   uint32_t latency,
149 				   uint32_t state,
150 				   uintptr_t address, uint32_t flag)
151 {
152 	uint32_t payload[PAYLOAD_ARG_CNT];
153 	uint32_t cpuid = plat_my_core_pos();
154 	const struct pm_proc *proc = pm_get_proc(cpuid);
155 	enum pm_ret_status ret = PM_RET_ERROR_INTERNAL;
156 
157 	if (proc == NULL) {
158 		WARN("Failed to get proc %d\n", cpuid);
159 		goto exit_label;
160 	}
161 
162 	/*
163 	 * Do client specific suspend operations
164 	 * (e.g. set powerdown request bit)
165 	 */
166 	pm_client_suspend(proc, state, flag);
167 
168 	/* Send request to the PLM */
169 	PM_PACK_PAYLOAD6(payload, LIBPM_MODULE_ID, flag, PM_SELF_SUSPEND,
170 			 nid, latency, state, address, (address >> 32));
171 	ret = pm_ipi_send_sync(proc, payload, NULL, 0);
172 
173 exit_label:
174 	return ret;
175 }
176 
177 /**
178  * pm_req_wakeup() - PM call for processor to wake up selected processor
179  *                   or subsystem.
180  * @target: Device ID of the processor or subsystem to wake up.
181  * @set_address: Resume address presence indicator.
182  *               1 - resume address specified, 0 - otherwise.
183  * @address: Resume address.
184  * @ack: Flag to specify whether acknowledge requested.
185  * @flag: 0 - Call from secure source.
186  *        1 - Call from non-secure source.
187  *
188  * This API function is either used to power up another APU core for SMP
189  * (by PSCI) or to power up an entirely different PU or subsystem, such
190  * as RPU0, RPU, or PL_CORE_xx. Resume address for the target PU will be
191  * automatically set by PMC.
192  *
193  * Return: Returns status, either success or error+reason.
194  *
195  */
196 enum pm_ret_status pm_req_wakeup(uint32_t target, uint32_t set_address,
197 				 uintptr_t address, uint8_t ack, uint32_t flag)
198 {
199 	uint32_t payload[PAYLOAD_ARG_CNT];
200 
201 	/* Send request to the PMC to perform the wake of the PU */
202 	PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REQ_WAKEUP, target,
203 			 set_address, address, ack);
204 
205 	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
206 }
207 
208 /**
209  * pm_get_callbackdata() - Read from IPI response buffer.
210  * @data: array of PAYLOAD_ARG_CNT elements.
211  * @count: Number of values to return.
212  * @flag: 0 - Call from secure source.
213  *        1 - Call from non-secure source.
214  * @ack: 0 - Do not ack IPI after reading payload.
215  *       1 - Ack IPI after reading payload.
216  *
217  * Read value from ipi buffer response buffer.
218  * Return: Returns status, either success or error.
219  *
220  */
221 enum pm_ret_status pm_get_callbackdata(uint32_t *data, size_t count, uint32_t flag, uint32_t ack)
222 {
223 	enum pm_ret_status ret = PM_RET_SUCCESS;
224 
225 	/*
226 	 * Typecasting to void to intentionally retain the variable and avoid
227 	 * MISRA violation for unused parameters. This may be used in the
228 	 * future if callbacks to a secure target are required.
229 	 */
230 	(void)flag;
231 
232 	/* Return if interrupt is not from PMU */
233 	if (pm_ipi_irq_status(primary_proc) != 0U) {
234 
235 		ret = pm_ipi_buff_read_callb(data, count);
236 
237 		if (ack != 0U) {
238 			pm_ipi_irq_clear(primary_proc);
239 		}
240 	}
241 
242 	return ret;
243 }
244 
245 /**
246  * pm_force_powerdown() - PM call to request for another PU or subsystem to
247  *                        be powered down forcefully.
248  * @target: Device ID of the PU node to be forced powered down.
249  * @ack: Flag to specify whether acknowledge is requested
250  * @flag: 0 - Call from secure source
251  *        1 - Call from non-secure source
252  *
253  * Return: Returns status, either success or error+reason.
254  *
255  */
256 enum pm_ret_status pm_force_powerdown(uint32_t target, uint8_t ack,
257 				      uint32_t flag)
258 {
259 	uint32_t payload[PAYLOAD_ARG_CNT];
260 	enum pm_ret_status ret = PM_RET_SUCCESS;
261 
262 	/* Send request to the PMC */
263 	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_FORCE_POWERDOWN,
264 			 target, ack);
265 
266 	if (ack == (uint32_t)IPI_BLOCKING) {
267 		ret = pm_ipi_send_sync(primary_proc, payload, NULL, 0);
268 	} else {
269 		ret = pm_ipi_send(primary_proc, payload);
270 	}
271 
272 	return ret;
273 }
274 
275 /**
276  * pm_system_shutdown() - PM call to request a system shutdown or restart.
277  * @type: Shutdown or restart? 0=shutdown, 1=restart, 2=setscope.
278  * @subtype: Scope: 0=APU-subsystem, 1=PS, 2=system.
279  * @flag: 0 - Call from secure source.
280  *        1 - Call from non-secure source.
281  *
282  * Return: Returns status, either success or error+reason.
283  *
284  */
285 enum pm_ret_status pm_system_shutdown(uint32_t type, uint32_t subtype,
286 				      uint32_t flag)
287 {
288 	uint32_t payload[PAYLOAD_ARG_CNT];
289 	enum pm_ret_status ret = PM_RET_SUCCESS;
290 
291 	if (type == XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY) {
292 		/* Setting scope for subsequent PSCI reboot or shutdown */
293 		pm_shutdown_scope = subtype;
294 		goto exit_label;
295 	}
296 
297 	/* Send request to the PMC */
298 	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SYSTEM_SHUTDOWN,
299 			 type, subtype);
300 
301 	ret = pm_ipi_send_non_blocking(primary_proc, payload);
302 
303 exit_label:
304 	return ret;
305 }
306 
307 /**
308  * pm_set_wakeup_source() - PM call to specify the wakeup source while
309  *                          suspended.
310  * @target: Device id of the targeted PU or subsystem
311  * @wkup_device: Device id of the wakeup peripheral
312  * @enable: Enable or disable the specified peripheral as wake source
313  * @flag: 0 - Call from secure source
314  *        1 - Call from non-secure source
315  *
316  * Return: Returns status, either success or error+reason.
317  *
318  */
319 enum pm_ret_status pm_set_wakeup_source(uint32_t target, uint32_t wkup_device,
320 					uint8_t enable, uint32_t flag)
321 {
322 	uint32_t payload[PAYLOAD_ARG_CNT];
323 
324 	PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_SET_WAKEUP_SOURCE,
325 			 target, wkup_device, enable);
326 	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
327 }
328 
329 /**
330  * tfa_api_feature_check() - Returns the supported TF-A API version if supported.
331  * @api_id: TF-A specific API ID to check.
332  * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of
333  *               words Returned supported API version
334  *
335  * Return: Returns status, either success or error+reason.
336  */
337 enum pm_ret_status tfa_api_feature_check(uint32_t api_id, uint32_t *ret_payload)
338 {
339 	enum pm_ret_status ret;
340 
341 	/* Return version of API which are implemented in TF-A only */
342 	switch (api_id) {
343 	case PM_GET_CALLBACK_DATA:
344 	case PM_GET_TRUSTZONE_VERSION:
345 		ret_payload[0] = PM_API_VERSION_2;
346 		ret = PM_RET_SUCCESS;
347 		break;
348 	case TF_A_PM_REGISTER_SGI:
349 	case TF_A_FEATURE_CHECK:
350 	case TF_A_CLEAR_PM_STATE:
351 		ret_payload[0] = PM_API_BASE_VERSION;
352 		ret = PM_RET_SUCCESS;
353 		break;
354 	default:
355 		ret = PM_RET_ERROR_NO_FEATURE;
356 		break;
357 	}
358 
359 	return ret;
360 }
361 
362 /**
363  * pm_feature_check() - Returns the supported API version if supported.
364  * @api_id: API ID to check.
365  * @flag: 0 - Call from secure source.
366  *        1 - Call from non-secure source.
367  * @ret_payload: pointer to array of PAYLOAD_ARG_CNT number of
368  *               words Returned supported API version and bitmasks
369  *               for IOCTL and QUERY ID
370  *
371  * Return: Returns status, either success or error+reason.
372  *
373  */
374 enum pm_ret_status pm_feature_check(uint32_t api_id, uint32_t *ret_payload,
375 				    uint32_t flag)
376 {
377 	uint32_t payload[PAYLOAD_ARG_CNT];
378 	uint32_t module_id;
379 	enum pm_ret_status ret;
380 
381 	WARN("pm_feature_check() will be deprecated in 2027.1 release."
382 	     " Use tfa_api_feature_check() for TF-A specific APIs.\n");
383 
384 	/* Return version of API which are implemented in TF-A only */
385 	switch (api_id) {
386 	case PM_GET_CALLBACK_DATA:
387 	case PM_GET_TRUSTZONE_VERSION:
388 		ret_payload[0] = PM_API_VERSION_2;
389 		ret = PM_RET_SUCCESS;
390 		break;
391 	case TF_A_PM_REGISTER_SGI:
392 		ret_payload[0] = PM_API_BASE_VERSION;
393 		ret = PM_RET_SUCCESS;
394 		break;
395 	default:
396 		module_id = (api_id & MODULE_ID_MASK) >> 8U;
397 
398 		/*
399 		 * feature check should be done only for LIBPM module
400 		 * If module_id is 0, then we consider it LIBPM module as default id
401 		 */
402 		if ((module_id > 0U) && (module_id != LIBPM_MODULE_ID)) {
403 			ret = PM_RET_SUCCESS;
404 			break;
405 		}
406 
407 		PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag,
408 				 PM_FEATURE_CHECK, api_id);
409 		ret = pm_ipi_send_sync(primary_proc, payload, ret_payload, RET_PAYLOAD_ARG_CNT);
410 
411 		break;
412 	}
413 
414 	return ret;
415 }
416 
417 /**
418  * pm_load_pdi() - Load the PDI. This function provides support to load
419  *                 PDI from linux.
420  *
421  * @src: Source device of pdi(DDR, OCM, SD etc).
422  * @address_low: lower 32-bit Linear memory space address.
423  * @address_high: higher 32-bit Linear memory space address.
424  * @flag: 0 - Call from secure source.
425  *        1 - Call from non-secure source.
426  *
427  * Return: Returns status, either success or error+reason.
428  *
429  */
430 enum pm_ret_status pm_load_pdi(uint32_t src, uint32_t address_low,
431 			       uint32_t address_high, uint32_t flag)
432 {
433 	uint32_t payload[PAYLOAD_ARG_CNT];
434 
435 	/* Send request to the PMU */
436 	PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, flag, PM_LOAD_PDI, src,
437 			 address_high, address_low);
438 	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
439 }
440 
441 /**
442  * pm_register_notifier() - PM call to register a subsystem to be notified
443  *                          about the device event.
444  * @device_id: Device ID for the Node to which the event is related.
445  * @event: Event in question.
446  * @wake: Wake subsystem upon capturing the event if value 1.
447  * @enable: Enable the registration for value 1, disable for value 0.
448  * @flag: 0 - Call from secure source.
449  *        1 - Call from non-secure source.
450  *
451  * Return: Returns status, either success or error+reason.
452  *
453  */
454 enum pm_ret_status pm_register_notifier(uint32_t device_id, uint32_t event,
455 					uint32_t wake, uint32_t enable,
456 					uint32_t flag)
457 {
458 	uint32_t payload[PAYLOAD_ARG_CNT];
459 
460 	/* Send request to the PMC */
461 	PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REGISTER_NOTIFIER,
462 			 device_id, event, wake, enable);
463 
464 	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
465 }
466 
467 /**
468  * pm_get_chipid() - Read silicon ID registers.
469  * @value: Buffer for two 32bit words.
470  *
471  * Return: Returns status, either success or error+reason and,
472  *         optionally, @value.
473  */
474 enum pm_ret_status pm_get_chipid(uint32_t *value)
475 {
476 	uint32_t payload[PAYLOAD_ARG_CNT];
477 
478 	PM_PACK_PAYLOAD1(payload, LIBPM_MODULE_ID, SECURE, PM_GET_CHIPID);
479 
480 	return pm_ipi_send_sync(primary_proc, payload, value, 2);
481 }
482