1 /*
2  * Copyright (c) 2019-2020, Xilinx, Inc. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * Top-level SMC handler for Versal power management calls and
9  * IPI setup functions for communication with PMC.
10  */
11 
12 #include <errno.h>
13 #include <plat_private.h>
14 #include <stdbool.h>
15 #include <common/runtime_svc.h>
16 #include <plat/common/platform.h>
17 #include "pm_api_sys.h"
18 #include "pm_client.h"
19 #include "pm_ipi.h"
20 #include <drivers/arm/gicv3.h>
21 
22 #define XSCUGIC_SGIR_EL1_INITID_SHIFT    24U
23 #define INVALID_SGI    0xFF
DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1,S3_0_C12_C11_6)24 DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1, S3_0_C12_C11_6)
25 
26 /* pm_up = true - UP, pm_up = false - DOWN */
27 static bool pm_up;
28 static unsigned int sgi = INVALID_SGI;
29 
30 static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle,
31 				void *cookie)
32 {
33 	int cpu;
34 	unsigned int reg;
35 
36 	(void)plat_ic_acknowledge_interrupt();
37 	cpu = plat_my_core_pos() + 1;
38 
39 	if (sgi != INVALID_SGI) {
40 		reg = (cpu | (sgi << XSCUGIC_SGIR_EL1_INITID_SHIFT));
41 		write_icc_asgi1r_el1(reg);
42 	}
43 
44 	/* Clear FIQ */
45 	plat_ic_end_of_interrupt(id);
46 
47 	return 0;
48 }
49 
50 /**
51  * pm_register_sgi() - PM register the IPI interrupt
52  *
53  * @sgi -  SGI number to be used for communication.
54  * @return	On success, the initialization function must return 0.
55  *		Any other return value will cause the framework to ignore
56  *		the service
57  *
58  * Update the SGI number to be used.
59  *
60  */
pm_register_sgi(unsigned int sgi_num)61 int pm_register_sgi(unsigned int sgi_num)
62 {
63 	if (sgi != INVALID_SGI) {
64 		return -EBUSY;
65 	}
66 
67 	if (sgi_num >= GICV3_MAX_SGI_TARGETS) {
68 		return -EINVAL;
69 	}
70 
71 	sgi = sgi_num;
72 	return 0;
73 }
74 
75 /**
76  * pm_setup() - PM service setup
77  *
78  * @return	On success, the initialization function must return 0.
79  *		Any other return value will cause the framework to ignore
80  *		the service
81  *
82  * Initialization functions for Versal power management for
83  * communicaton with PMC.
84  *
85  * Called from sip_svc_setup initialization function with the
86  * rt_svc_init signature.
87  */
pm_setup(void)88 int pm_setup(void)
89 {
90 	int status, ret = 0;
91 
92 	status = pm_ipi_init(primary_proc);
93 
94 	if (status < 0) {
95 		INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
96 		ret = status;
97 	} else {
98 		pm_up = true;
99 	}
100 
101 	/*
102 	 * Enable IPI IRQ
103 	 * assume the rich OS is OK to handle callback IRQs now.
104 	 * Even if we were wrong, it would not enable the IRQ in
105 	 * the GIC.
106 	 */
107 	pm_ipi_irq_enable(primary_proc);
108 
109 	ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler);
110 	if (ret) {
111 		WARN("BL31: registering IPI interrupt failed\n");
112 	}
113 	return ret;
114 }
115 
116 /**
117  * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
118  * @smc_fid - Function Identifier
119  * @x1 - x4 - Arguments
120  * @cookie  - Unused
121  * @handler - Pointer to caller's context structure
122  *
123  * @return  - Unused
124  *
125  * Determines that smc_fid is valid and supported PM SMC Function ID from the
126  * list of pm_api_ids, otherwise completes the request with
127  * the unknown SMC Function ID
128  *
129  * The SMC calls for PM service are forwarded from SIP Service SMC handler
130  * function with rt_svc_handle signature
131  */
pm_smc_handler(uint32_t smc_fid,uint64_t x1,uint64_t x2,uint64_t x3,uint64_t x4,void * cookie,void * handle,uint64_t flags)132 uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
133 			uint64_t x4, void *cookie, void *handle, uint64_t flags)
134 {
135 	enum pm_ret_status ret;
136 
137 	uint32_t pm_arg[4];
138 	uint32_t security_flag = SECURE_FLAG;
139 
140 	/* Handle case where PM wasn't initialized properly */
141 	if (!pm_up)
142 		SMC_RET1(handle, SMC_UNK);
143 
144 	pm_arg[0] = (uint32_t)x1;
145 	pm_arg[1] = (uint32_t)(x1 >> 32);
146 	pm_arg[2] = (uint32_t)x2;
147 	pm_arg[3] = (uint32_t)(x2 >> 32);
148 
149 	/*
150 	 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as non-secure (1)
151 	 * if smc called is non secure
152 	 */
153 	if (is_caller_non_secure(flags)) {
154 		security_flag = NON_SECURE_FLAG;
155 	}
156 
157 	switch (smc_fid & FUNCID_NUM_MASK) {
158 	/* PM API Functions */
159 	case PM_SELF_SUSPEND:
160 		ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
161 				      pm_arg[3], security_flag);
162 		SMC_RET1(handle, (uint64_t)ret);
163 
164 	case PM_FORCE_POWERDOWN:
165 		ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
166 		SMC_RET1(handle, (uint64_t)ret);
167 
168 	case PM_REQ_SUSPEND:
169 		ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
170 				     pm_arg[3], security_flag);
171 		SMC_RET1(handle, (uint64_t)ret);
172 
173 	case PM_ABORT_SUSPEND:
174 		ret = pm_abort_suspend(pm_arg[0], security_flag);
175 		SMC_RET1(handle, (uint64_t)ret);
176 
177 	case PM_SYSTEM_SHUTDOWN:
178 		ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
179 		SMC_RET1(handle, (uint64_t)ret);
180 
181 	case PM_REQ_WAKEUP:
182 		ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3],
183 				    security_flag);
184 		SMC_RET1(handle, (uint64_t)ret);
185 
186 	case PM_SET_WAKEUP_SOURCE:
187 		ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2],
188 					   security_flag);
189 		SMC_RET1(handle, (uint64_t)ret);
190 
191 	case PM_REQUEST_DEVICE:
192 		ret = pm_request_device(pm_arg[0], pm_arg[1], pm_arg[2],
193 					pm_arg[3], security_flag);
194 		SMC_RET1(handle, (uint64_t)ret);
195 
196 	case PM_RELEASE_DEVICE:
197 		ret = pm_release_device(pm_arg[0], security_flag);
198 		SMC_RET1(handle, (uint64_t)ret);
199 
200 	case PM_SET_REQUIREMENT:
201 		ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
202 					 pm_arg[3], security_flag);
203 		SMC_RET1(handle, (uint64_t)ret);
204 
205 	case PM_GET_API_VERSION:
206 	{
207 		uint32_t api_version;
208 
209 		ret = pm_get_api_version(&api_version, security_flag);
210 		SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
211 				 ((uint64_t)api_version << 32));
212 	}
213 
214 	case PM_GET_DEVICE_STATUS:
215 	{
216 		uint32_t buff[3];
217 
218 		ret = pm_get_device_status(pm_arg[0], buff, security_flag);
219 		SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buff[0] << 32),
220 			 (uint64_t)buff[1] | ((uint64_t)buff[2] << 32));
221 	}
222 
223 	case PM_RESET_ASSERT:
224 		ret = pm_reset_assert(pm_arg[0], pm_arg[1], security_flag);
225 		SMC_RET1(handle, (uint64_t)ret);
226 
227 	case PM_RESET_GET_STATUS:
228 	{
229 		uint32_t reset_status;
230 
231 		ret = pm_reset_get_status(pm_arg[0], &reset_status,
232 					  security_flag);
233 		SMC_RET1(handle, (uint64_t)ret |
234 			 ((uint64_t)reset_status << 32));
235 	}
236 
237 	case PM_INIT_FINALIZE:
238 		ret = pm_init_finalize(security_flag);
239 		SMC_RET1(handle, (uint64_t)ret);
240 
241 	case PM_GET_CALLBACK_DATA:
242 	{
243 		uint32_t result[4] = {0};
244 
245 		pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag);
246 		SMC_RET2(handle,
247 			 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
248 			 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
249 	}
250 
251 	case PM_PINCTRL_REQUEST:
252 		ret = pm_pinctrl_request(pm_arg[0], security_flag);
253 		SMC_RET1(handle, (uint64_t)ret);
254 
255 	case PM_PINCTRL_RELEASE:
256 		ret = pm_pinctrl_release(pm_arg[0], security_flag);
257 		SMC_RET1(handle, (uint64_t)ret);
258 
259 	case PM_PINCTRL_GET_FUNCTION:
260 	{
261 		uint32_t value = 0;
262 
263 		ret = pm_pinctrl_get_function(pm_arg[0], &value, security_flag);
264 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
265 	}
266 
267 	case PM_PINCTRL_SET_FUNCTION:
268 		ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1],
269 					      security_flag);
270 		SMC_RET1(handle, (uint64_t)ret);
271 
272 	case PM_PINCTRL_CONFIG_PARAM_GET:
273 	{
274 		uint32_t value;
275 
276 		ret = pm_pinctrl_get_pin_param(pm_arg[0], pm_arg[1], &value,
277 					       security_flag);
278 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
279 	}
280 
281 	case PM_PINCTRL_CONFIG_PARAM_SET:
282 		ret = pm_pinctrl_set_pin_param(pm_arg[0], pm_arg[1], pm_arg[2],
283 					       security_flag);
284 		SMC_RET1(handle, (uint64_t)ret);
285 
286 	case PM_IOCTL:
287 	{
288 		uint32_t value;
289 
290 		ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
291 				   pm_arg[3], &value, security_flag);
292 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
293 	}
294 
295 	case PM_QUERY_DATA:
296 	{
297 		uint32_t data[8] = { 0 };
298 
299 		ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
300 				      pm_arg[3], data, security_flag);
301 
302 		SMC_RET2(handle, (uint64_t)ret  | ((uint64_t)data[0] << 32),
303 				 (uint64_t)data[1] | ((uint64_t)data[2] << 32));
304 
305 	}
306 	case PM_CLOCK_ENABLE:
307 		ret = pm_clock_enable(pm_arg[0], security_flag);
308 		SMC_RET1(handle, (uint64_t)ret);
309 
310 	case PM_CLOCK_DISABLE:
311 		ret = pm_clock_disable(pm_arg[0], security_flag);
312 		SMC_RET1(handle, (uint64_t)ret);
313 
314 	case PM_CLOCK_GETSTATE:
315 	{
316 		uint32_t value;
317 
318 		ret = pm_clock_get_state(pm_arg[0], &value, security_flag);
319 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
320 	}
321 
322 	case PM_CLOCK_SETDIVIDER:
323 		ret = pm_clock_set_divider(pm_arg[0], pm_arg[1], security_flag);
324 		SMC_RET1(handle, (uint64_t)ret);
325 
326 	case PM_CLOCK_GETDIVIDER:
327 	{
328 		uint32_t value;
329 
330 		ret = pm_clock_get_divider(pm_arg[0], &value, security_flag);
331 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
332 	}
333 
334 	case PM_CLOCK_SETPARENT:
335 		ret = pm_clock_set_parent(pm_arg[0], pm_arg[1], security_flag);
336 		SMC_RET1(handle, (uint64_t)ret);
337 
338 	case PM_CLOCK_GETPARENT:
339 	{
340 		uint32_t value;
341 
342 		ret = pm_clock_get_parent(pm_arg[0], &value, security_flag);
343 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
344 	}
345 
346 	case PM_CLOCK_GETRATE:
347 	{
348 		uint32_t rate[2] = { 0 };
349 
350 		ret = pm_clock_get_rate(pm_arg[0], rate, security_flag);
351 		SMC_RET2(handle, (uint64_t)ret | ((uint64_t)rate[0] << 32),
352 			 rate[1]);
353 	}
354 
355 	case PM_PLL_SET_PARAMETER:
356 		ret = pm_pll_set_param(pm_arg[0], pm_arg[1], pm_arg[2],
357 				       security_flag);
358 		SMC_RET1(handle, (uint64_t)ret);
359 
360 	case PM_PLL_GET_PARAMETER:
361 	{
362 		uint32_t value;
363 
364 		ret = pm_pll_get_param(pm_arg[0], pm_arg[1], &value,
365 				       security_flag);
366 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value << 32));
367 	}
368 
369 	case PM_PLL_SET_MODE:
370 		ret = pm_pll_set_mode(pm_arg[0], pm_arg[1], security_flag);
371 		SMC_RET1(handle, (uint64_t)ret);
372 
373 	case PM_PLL_GET_MODE:
374 	{
375 		uint32_t mode;
376 
377 		ret = pm_pll_get_mode(pm_arg[0], &mode, security_flag);
378 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)mode << 32));
379 	}
380 
381 	case PM_GET_TRUSTZONE_VERSION:
382 		SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
383 			 ((uint64_t)VERSAL_TZ_VERSION << 32));
384 
385 	case PM_GET_CHIPID:
386 	{
387 		uint32_t result[2];
388 
389 		ret = pm_get_chipid(result, security_flag);
390 		SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
391 			 result[1]);
392 	}
393 
394 	case PM_FEATURE_CHECK:
395 	{
396 		uint32_t version;
397 
398 		ret = pm_feature_check(pm_arg[0], &version, security_flag);
399 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)version << 32));
400 	}
401 
402 	case PM_LOAD_PDI:
403 	{
404 		ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
405 				  security_flag);
406 		SMC_RET1(handle, (uint64_t)ret);
407 	}
408 
409 	case PM_GET_OP_CHARACTERISTIC:
410 	{
411 		uint32_t result;
412 
413 		ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result,
414 					       security_flag);
415 		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
416 	}
417 
418 	case PM_SET_MAX_LATENCY:
419 	{
420 		ret = pm_set_max_latency(pm_arg[0], pm_arg[1], security_flag);
421 		SMC_RET1(handle, (uint64_t)ret);
422 	}
423 
424 	case PM_REGISTER_NOTIFIER:
425 	{
426 		ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
427 					   pm_arg[3], security_flag);
428 		SMC_RET1(handle, (uint64_t)ret);
429 	}
430 
431 	default:
432 		WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
433 		SMC_RET1(handle, SMC_UNK);
434 	}
435 }
436