xref: /rk3399_ARM-atf/plat/xilinx/common/pm_service/pm_svc_main.c (revision 83a4dae1af916b938659b39b7d0884359c638185)
1 /*
2  * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
3  * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /*
9  * Top-level SMC handler for Versal power management calls and
10  * IPI setup functions for communication with PMC.
11  */
12 
13 #include <errno.h>
14 #include <plat_private.h>
15 #include <stdbool.h>
16 #include <common/runtime_svc.h>
17 #include <plat/common/platform.h>
18 #include "pm_api_sys.h"
19 #include "pm_client.h"
20 #include "pm_ipi.h"
21 #include <drivers/arm/gicv3.h>
22 #include "../drivers/arm/gic/v3/gicv3_private.h"
23 
24 #define MODE				0x80000000U
25 
26 #define XSCUGIC_SGIR_EL1_INITID_SHIFT    24U
27 #define INVALID_SGI    0xFFU
28 #define PM_INIT_SUSPEND_CB	(30U)
29 #define PM_NOTIFY_CB		(32U)
30 DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1, S3_0_C12_C11_6)
31 
32 /* pm_up = true - UP, pm_up = false - DOWN */
33 static bool pm_up;
34 static uint32_t sgi = (uint32_t)INVALID_SGI;
35 
36 static void notify_os(void)
37 {
38 	int32_t cpu;
39 	uint32_t reg;
40 
41 	cpu = plat_my_core_pos() + 1U;
42 
43 	reg = (cpu | (sgi << XSCUGIC_SGIR_EL1_INITID_SHIFT));
44 	write_icc_asgi1r_el1(reg);
45 }
46 
47 static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle,
48 				void *cookie)
49 {
50 	uint32_t payload[4] = {0};
51 	enum pm_ret_status ret;
52 
53 	VERBOSE("Received IPI FIQ from firmware\n");
54 
55 	(void)plat_ic_acknowledge_interrupt();
56 
57 	ret = pm_get_callbackdata(payload, ARRAY_SIZE(payload), 0, 0);
58 	if (ret != PM_RET_SUCCESS) {
59 		payload[0] = ret;
60 	}
61 
62 	switch (payload[0]) {
63 	case PM_INIT_SUSPEND_CB:
64 	case PM_NOTIFY_CB:
65 		if (sgi != INVALID_SGI) {
66 			notify_os();
67 		}
68 		break;
69 	case PM_RET_ERROR_INVALID_CRC:
70 		pm_ipi_irq_clear(primary_proc);
71 		WARN("Invalid CRC in the payload\n");
72 		break;
73 
74 	default:
75 		pm_ipi_irq_clear(primary_proc);
76 		WARN("Invalid IPI payload\n");
77 		break;
78 	}
79 
80 	/* Clear FIQ */
81 	plat_ic_end_of_interrupt(id);
82 
83 	return 0;
84 }
85 
86 /**
87  * pm_register_sgi() - PM register the IPI interrupt.
88  * @sgi_num: SGI number to be used for communication.
89  * @reset: Reset to invalid SGI when reset=1.
90  *
91  * Return: On success, the initialization function must return 0.
92  *         Any other return value will cause the framework to ignore
93  *         the service.
94  *
95  * Update the SGI number to be used.
96  *
97  */
98 int32_t pm_register_sgi(uint32_t sgi_num, uint32_t reset)
99 {
100 	if (reset == 1U) {
101 		sgi = INVALID_SGI;
102 		return 0;
103 	}
104 
105 	if (sgi != INVALID_SGI) {
106 		return -EBUSY;
107 	}
108 
109 	if (sgi_num >= GICV3_MAX_SGI_TARGETS) {
110 		return -EINVAL;
111 	}
112 
113 	sgi = (uint32_t)sgi_num;
114 	return 0;
115 }
116 
117 /**
118  * pm_setup() - PM service setup.
119  *
120  * Return: On success, the initialization function must return 0.
121  *         Any other return value will cause the framework to ignore
122  *         the service.
123  *
124  * Initialization functions for Versal power management for
125  * communicaton with PMC.
126  *
127  * Called from sip_svc_setup initialization function with the
128  * rt_svc_init signature.
129  *
130  */
131 int32_t pm_setup(void)
132 {
133 	int32_t ret = 0;
134 
135 	pm_ipi_init(primary_proc);
136 	pm_up = true;
137 
138 	/*
139 	 * Enable IPI IRQ
140 	 * assume the rich OS is OK to handle callback IRQs now.
141 	 * Even if we were wrong, it would not enable the IRQ in
142 	 * the GIC.
143 	 */
144 	pm_ipi_irq_enable(primary_proc);
145 
146 	ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler);
147 	if (ret != 0) {
148 		WARN("BL31: registering IPI interrupt failed\n");
149 	}
150 
151 	gicd_write_irouter(gicv3_driver_data->gicd_base, PLAT_VERSAL_IPI_IRQ, MODE);
152 	return ret;
153 }
154 
155 /**
156  * eemi_for_compatibility() - EEMI calls handler for deprecated calls.
157  * @api_id: identifier for the API being called.
158  * @pm_arg: pointer to the argument data for the API call.
159  * @handle: Pointer to caller's context structure.
160  * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
161  *
162  * Return: If EEMI API found then, uintptr_t type address, else 0.
163  *
164  * Some EEMI API's use case needs to be changed in Linux driver, so they
165  * can take advantage of common EEMI handler in TF-A. As of now the old
166  * implementation of these APIs are required to maintain backward compatibility
167  * until their use case in linux driver changes.
168  *
169  */
170 static uintptr_t eemi_for_compatibility(uint32_t api_id, uint32_t *pm_arg,
171 					void *handle, uint32_t security_flag)
172 {
173 	enum pm_ret_status ret;
174 
175 	switch (api_id) {
176 
177 	case (uint32_t)PM_IOCTL:
178 	{
179 		uint32_t value = 0U;
180 
181 		ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
182 				   pm_arg[3], pm_arg[4],
183 				   &value, security_flag);
184 		if (ret == PM_RET_ERROR_NOTSUPPORTED)
185 			return (uintptr_t)0;
186 
187 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32U);
188 	}
189 
190 	case (uint32_t)PM_QUERY_DATA:
191 	{
192 		uint32_t data[PAYLOAD_ARG_CNT] = { 0 };
193 
194 		ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
195 				    pm_arg[3], data, security_flag);
196 
197 		SMC_RET2(handle, (uint64_t)ret | ((uint64_t)data[0] << 32U),
198 			 (uint64_t)data[1] | ((uint64_t)data[2] << 32U));
199 	}
200 
201 	case (uint32_t)PM_FEATURE_CHECK:
202 	{
203 		uint32_t result[PAYLOAD_ARG_CNT] = {0U};
204 
205 		ret = pm_feature_check(pm_arg[0], result, security_flag);
206 		SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U),
207 			 (uint64_t)result[1] | ((uint64_t)result[2] << 32U));
208 	}
209 
210 	case PM_LOAD_PDI:
211 	{
212 		ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
213 				  security_flag);
214 		SMC_RET1(handle, (uint64_t)ret);
215 	}
216 
217 	default:
218 		return (uintptr_t)0;
219 	}
220 }
221 
222 /**
223  * eemi_psci_debugfs_handler() - EEMI API invoked from PSCI.
224  * @api_id: identifier for the API being called.
225  * @pm_arg: pointer to the argument data for the API call.
226  * @handle: Pointer to caller's context structure.
227  * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
228  *
229  * These EEMI APIs performs CPU specific power management tasks.
230  * These EEMI APIs are invoked either from PSCI or from debugfs in kernel.
231  * These calls require CPU specific processing before sending IPI request to
232  * Platform Management Controller. For example enable/disable CPU specific
233  * interrupts. This requires separate handler for these calls and may not be
234  * handled using common eemi handler.
235  *
236  * Return: If EEMI API found then, uintptr_t type address, else 0.
237  *
238  */
239 static uintptr_t eemi_psci_debugfs_handler(uint32_t api_id, uint32_t *pm_arg,
240 					   void *handle, uint32_t security_flag)
241 {
242 	enum pm_ret_status ret;
243 
244 	switch (api_id) {
245 
246 	case (uint32_t)PM_SELF_SUSPEND:
247 		ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
248 				      pm_arg[3], security_flag);
249 		SMC_RET1(handle, (u_register_t)ret);
250 
251 	case (uint32_t)PM_FORCE_POWERDOWN:
252 		ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
253 		SMC_RET1(handle, (u_register_t)ret);
254 
255 	case (uint32_t)PM_REQ_SUSPEND:
256 		ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
257 				     pm_arg[3], security_flag);
258 		SMC_RET1(handle, (u_register_t)ret);
259 
260 	case (uint32_t)PM_ABORT_SUSPEND:
261 		ret = pm_abort_suspend(pm_arg[0], security_flag);
262 		SMC_RET1(handle, (u_register_t)ret);
263 
264 	case (uint32_t)PM_SYSTEM_SHUTDOWN:
265 		ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
266 		SMC_RET1(handle, (u_register_t)ret);
267 
268 	default:
269 		return (uintptr_t)0;
270 	}
271 }
272 
273 /**
274  * TF_A_specific_handler() - SMC handler for TF-A specific functionality.
275  * @api_id: identifier for the API being called.
276  * @pm_arg: pointer to the argument data for the API call.
277  * @handle: Pointer to caller's context structure.
278  * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
279  *
280  * These EEMI calls performs functionality that does not require
281  * IPI transaction. The handler ends in TF-A and returns requested data to
282  * kernel from TF-A.
283  *
284  * Return: If TF-A specific API found then, uintptr_t type address, else 0
285  *
286  */
287 static uintptr_t TF_A_specific_handler(uint32_t api_id, uint32_t *pm_arg,
288 				       void *handle, uint32_t security_flag)
289 {
290 	switch (api_id) {
291 
292 	case TF_A_PM_REGISTER_SGI:
293 	{
294 		int32_t ret;
295 
296 		ret = pm_register_sgi(pm_arg[0], pm_arg[1]);
297 		if (ret != 0) {
298 			SMC_RET1(handle, (uint32_t)PM_RET_ERROR_ARGS);
299 		}
300 
301 		SMC_RET1(handle, (uint32_t)PM_RET_SUCCESS);
302 	}
303 
304 	case PM_GET_CALLBACK_DATA:
305 	{
306 		uint32_t result[4] = {0};
307 		enum pm_ret_status ret;
308 
309 		ret = pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag, 1U);
310 		if (ret != 0) {
311 			result[0] = ret;
312 		}
313 
314 		SMC_RET2(handle,
315 			(uint64_t)result[0] | ((uint64_t)result[1] << 32U),
316 			(uint64_t)result[2] | ((uint64_t)result[3] << 32U));
317 	}
318 
319 	case PM_GET_TRUSTZONE_VERSION:
320 		SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
321 			 ((uint64_t)TZ_VERSION << 32U));
322 
323 	default:
324 		return (uintptr_t)0;
325 	}
326 }
327 
328 /**
329  * eemi_handler() - Prepare EEMI payload and perform IPI transaction.
330  * @api_id: identifier for the API being called.
331  * @pm_arg: pointer to the argument data for the API call.
332  * @handle: Pointer to caller's context structure.
333  * @security_flag: SECURE_FLAG or NON_SECURE_FLAG.
334  *
335  * EEMI - Embedded Energy Management Interface is Xilinx proprietary protocol
336  * to allow communication between power management controller and different
337  * processing clusters.
338  *
339  * This handler prepares EEMI protocol payload received from kernel and performs
340  * IPI transaction.
341  *
342  * Return: If EEMI API found then, uintptr_t type address, else 0
343  *
344  */
345 static uintptr_t eemi_handler(uint32_t api_id, uint32_t *pm_arg,
346 			      void *handle, uint32_t security_flag)
347 {
348 	enum pm_ret_status ret;
349 	uint32_t buf[PAYLOAD_ARG_CNT] = {0};
350 
351 	ret = pm_handle_eemi_call(security_flag, api_id, pm_arg[0], pm_arg[1],
352 				  pm_arg[2], pm_arg[3], pm_arg[4],
353 				  (uint64_t *)buf);
354 	/*
355 	 * Two IOCTLs, to get clock name and pinctrl name of pm_query_data API
356 	 * receives 5 words of respoonse from firmware. Currently linux driver can
357 	 * receive only 4 words from TF-A. So, this needs to be handled separately
358 	 * than other eemi calls.
359 	 */
360 	if (api_id == (uint32_t)PM_QUERY_DATA) {
361 		if ((pm_arg[0] == XPM_QID_CLOCK_GET_NAME ||
362 		    pm_arg[0] == XPM_QID_PINCTRL_GET_FUNCTION_NAME) &&
363 		    ret == PM_RET_SUCCESS) {
364 			SMC_RET2(handle, (uint64_t)buf[0] | ((uint64_t)buf[1] << 32U),
365 				(uint64_t)buf[2] | ((uint64_t)buf[3] << 32U));
366 		}
367 	}
368 
369 	SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U),
370 		 (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U));
371 }
372 
373 /**
374  * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
375  * @smc_fid: Function Identifier.
376  * @x1: SMC64 Arguments from kernel.
377  * @x2: SMC64 Arguments from kernel.
378  * @x3: SMC64 Arguments from kernel (upper 32-bits).
379  * @x4: Unused.
380  * @cookie: Unused.
381  * @handle: Pointer to caller's context structure.
382  * @flags: SECURE_FLAG or NON_SECURE_FLAG.
383  *
384  * Return: Unused.
385  *
386  * Determines that smc_fid is valid and supported PM SMC Function ID from the
387  * list of pm_api_ids, otherwise completes the request with
388  * the unknown SMC Function ID.
389  *
390  * The SMC calls for PM service are forwarded from SIP Service SMC handler
391  * function with rt_svc_handle signature.
392  *
393  */
394 uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
395 			uint64_t x4, const void *cookie, void *handle, uint64_t flags)
396 {
397 	uintptr_t ret;
398 	uint32_t pm_arg[PAYLOAD_ARG_CNT] = {0};
399 	uint32_t security_flag = SECURE_FLAG;
400 	uint32_t api_id;
401 
402 	/* Handle case where PM wasn't initialized properly */
403 	if (pm_up == false) {
404 		SMC_RET1(handle, SMC_UNK);
405 	}
406 
407 	/*
408 	 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as non-secure (1)
409 	 * if smc called is non secure
410 	 */
411 	if (is_caller_non_secure(flags) != 0) {
412 		security_flag = NON_SECURE_FLAG;
413 	}
414 
415 	pm_arg[0] = (uint32_t)x1;
416 	pm_arg[1] = (uint32_t)(x1 >> 32U);
417 	pm_arg[2] = (uint32_t)x2;
418 	pm_arg[3] = (uint32_t)(x2 >> 32U);
419 	pm_arg[4] = (uint32_t)x3;
420 	(void)(x4);
421 	api_id = smc_fid & FUNCID_NUM_MASK;
422 
423 	ret = eemi_for_compatibility(api_id, pm_arg, handle, security_flag);
424 	if (ret != (uintptr_t)0) {
425 		return ret;
426 	}
427 
428 	ret = eemi_psci_debugfs_handler(api_id, pm_arg, handle, flags);
429 	if (ret !=  (uintptr_t)0) {
430 		return ret;
431 	}
432 
433 	ret = TF_A_specific_handler(api_id, pm_arg, handle, security_flag);
434 	if (ret !=  (uintptr_t)0) {
435 		return ret;
436 	}
437 
438 	ret = eemi_handler(api_id, pm_arg, handle, security_flag);
439 
440 	return ret;
441 }
442