pm_svc_main.c 9.06 KB
Newer Older
1
/*
2
 * Copyright (c) 2019-2020, Xilinx, Inc. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

/*
 * Top-level SMC handler for Versal power management calls and
 * IPI setup functions for communication with PMC.
 */

#include <errno.h>
#include <plat_private.h>
14
15
16
#include <stdbool.h>
#include <common/runtime_svc.h>
#include "pm_api_sys.h"
17
18
19
#include "pm_client.h"
#include "pm_ipi.h"

20
21
22
/* pm_up = true - UP, pm_up = false - DOWN */
static bool pm_up;

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * pm_setup() - PM service setup
 *
 * @return	On success, the initialization function must return 0.
 *		Any other return value will cause the framework to ignore
 *		the service
 *
 * Initialization functions for Versal power management for
 * communicaton with PMC.
 *
 * Called from sip_svc_setup initialization function with the
 * rt_svc_init signature.
 */
int pm_setup(void)
{
	int status, ret = 0;

	status = pm_ipi_init(primary_proc);

	if (status < 0) {
		INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
		ret = status;
45
46
	} else {
		pm_up = true;
47
48
49
50
	}

	return ret;
}
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

/**
 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
 * @smc_fid - Function Identifier
 * @x1 - x4 - Arguments
 * @cookie  - Unused
 * @handler - Pointer to caller's context structure
 *
 * @return  - Unused
 *
 * Determines that smc_fid is valid and supported PM SMC Function ID from the
 * list of pm_api_ids, otherwise completes the request with
 * the unknown SMC Function ID
 *
 * The SMC calls for PM service are forwarded from SIP Service SMC handler
 * function with rt_svc_handle signature
 */
uint64_t 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)
{
	enum pm_ret_status ret;

	uint32_t pm_arg[4];
74
	uint32_t security_flag = SECURE_FLAG;
75
76
77
78
79
80
81
82
83
84

	/* Handle case where PM wasn't initialized properly */
	if (!pm_up)
		SMC_RET1(handle, SMC_UNK);

	pm_arg[0] = (uint32_t)x1;
	pm_arg[1] = (uint32_t)(x1 >> 32);
	pm_arg[2] = (uint32_t)x2;
	pm_arg[3] = (uint32_t)(x2 >> 32);

85
86
87
88
89
90
91
92
	/*
	 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as non-secure (1)
	 * if smc called is non secure
	 */
	if (is_caller_non_secure(flags)) {
		security_flag = NON_SECURE_FLAG;
	}

93
94
95
96
	switch (smc_fid & FUNCID_NUM_MASK) {
	/* PM API Functions */
	case PM_SELF_SUSPEND:
		ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
97
				      pm_arg[3], security_flag);
98
99
		SMC_RET1(handle, (uint64_t)ret);

100
	case PM_FORCE_POWERDOWN:
101
		ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
102
103
		SMC_RET1(handle, (uint64_t)ret);

104
105
	case PM_REQ_SUSPEND:
		ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
106
				     pm_arg[3], security_flag);
107
108
109
		SMC_RET1(handle, (uint64_t)ret);

	case PM_ABORT_SUSPEND:
110
		ret = pm_abort_suspend(pm_arg[0], security_flag);
111
112
		SMC_RET1(handle, (uint64_t)ret);

113
	case PM_SYSTEM_SHUTDOWN:
114
		ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
115
116
		SMC_RET1(handle, (uint64_t)ret);

117
	case PM_REQ_WAKEUP:
118
119
		ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3],
				    security_flag);
120
		SMC_RET1(handle, (uint64_t)ret);
121
122

	case PM_SET_WAKEUP_SOURCE:
123
124
		ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2],
					   security_flag);
125
		SMC_RET1(handle, (uint64_t)ret);
126

127
128
	case PM_REQUEST_DEVICE:
		ret = pm_request_device(pm_arg[0], pm_arg[1], pm_arg[2],
129
					pm_arg[3], security_flag);
130
131
132
		SMC_RET1(handle, (uint64_t)ret);

	case PM_RELEASE_DEVICE:
133
		ret = pm_release_device(pm_arg[0], security_flag);
134
135
136
137
		SMC_RET1(handle, (uint64_t)ret);

	case PM_SET_REQUIREMENT:
		ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
138
					 pm_arg[3], security_flag);
139
140
141
142
143
144
		SMC_RET1(handle, (uint64_t)ret);

	case PM_GET_API_VERSION:
	{
		uint32_t api_version;

145
		ret = pm_get_api_version(&api_version, security_flag);
146
147
148
149
150
151
152
153
		SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
				 ((uint64_t)api_version << 32));
	}

	case PM_GET_DEVICE_STATUS:
	{
		uint32_t buff[3];

154
		ret = pm_get_device_status(pm_arg[0], buff, security_flag);
155
156
157
158
159
		SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buff[0] << 32),
			 (uint64_t)buff[1] | ((uint64_t)buff[2] << 32));
	}

	case PM_RESET_ASSERT:
160
		ret = pm_reset_assert(pm_arg[0], pm_arg[1], security_flag);
161
162
163
164
165
166
		SMC_RET1(handle, (uint64_t)ret);

	case PM_RESET_GET_STATUS:
	{
		uint32_t reset_status;

167
168
		ret = pm_reset_get_status(pm_arg[0], &reset_status,
					  security_flag);
169
170
171
172
		SMC_RET1(handle, (uint64_t)ret |
			 ((uint64_t)reset_status << 32));
	}

173
	case PM_INIT_FINALIZE:
174
		ret = pm_init_finalize(security_flag);
175
		SMC_RET1(handle, (uint64_t)ret);
176

177
178
179
180
	case PM_GET_CALLBACK_DATA:
	{
		uint32_t result[4] = {0};

181
		pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag);
182
183
184
185
186
		SMC_RET2(handle,
			 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
			 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
	}

187
	case PM_PINCTRL_REQUEST:
188
		ret = pm_pinctrl_request(pm_arg[0], security_flag);
189
190
191
		SMC_RET1(handle, (uint64_t)ret);

	case PM_PINCTRL_RELEASE:
192
		ret = pm_pinctrl_release(pm_arg[0], security_flag);
193
194
195
196
197
198
		SMC_RET1(handle, (uint64_t)ret);

	case PM_PINCTRL_GET_FUNCTION:
	{
		uint32_t value = 0;

199
		ret = pm_pinctrl_get_function(pm_arg[0], &value, security_flag);
200
201
202
203
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
	}

	case PM_PINCTRL_SET_FUNCTION:
204
205
		ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1],
					      security_flag);
206
207
208
209
210
211
		SMC_RET1(handle, (uint64_t)ret);

	case PM_PINCTRL_CONFIG_PARAM_GET:
	{
		uint32_t value;

212
213
		ret = pm_pinctrl_get_pin_param(pm_arg[0], pm_arg[1], &value,
					       security_flag);
214
215
216
217
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
	}

	case PM_PINCTRL_CONFIG_PARAM_SET:
218
219
		ret = pm_pinctrl_set_pin_param(pm_arg[0], pm_arg[1], pm_arg[2],
					       security_flag);
220
221
		SMC_RET1(handle, (uint64_t)ret);

222
223
224
225
226
	case PM_IOCTL:
	{
		uint32_t value;

		ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
227
				   pm_arg[3], &value, security_flag);
228
229
230
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
	}

231
232
	case PM_QUERY_DATA:
	{
233
		uint32_t data[8] = { 0 };
234
235

		ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
236
				      pm_arg[3], data, security_flag);
237

238
		SMC_RET2(handle, (uint64_t)ret  | ((uint64_t)data[0] << 32),
239
				 (uint64_t)data[1] | ((uint64_t)data[2] << 32));
240

241
	}
242
	case PM_CLOCK_ENABLE:
243
		ret = pm_clock_enable(pm_arg[0], security_flag);
244
245
246
		SMC_RET1(handle, (uint64_t)ret);

	case PM_CLOCK_DISABLE:
247
		ret = pm_clock_disable(pm_arg[0], security_flag);
248
249
250
251
252
253
		SMC_RET1(handle, (uint64_t)ret);

	case PM_CLOCK_GETSTATE:
	{
		uint32_t value;

254
		ret = pm_clock_get_state(pm_arg[0], &value, security_flag);
255
256
257
258
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
	}

	case PM_CLOCK_SETDIVIDER:
259
		ret = pm_clock_set_divider(pm_arg[0], pm_arg[1], security_flag);
260
261
262
263
264
265
		SMC_RET1(handle, (uint64_t)ret);

	case PM_CLOCK_GETDIVIDER:
	{
		uint32_t value;

266
		ret = pm_clock_get_divider(pm_arg[0], &value, security_flag);
267
268
269
270
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
	}

	case PM_CLOCK_SETPARENT:
271
		ret = pm_clock_set_parent(pm_arg[0], pm_arg[1], security_flag);
272
273
274
275
276
277
		SMC_RET1(handle, (uint64_t)ret);

	case PM_CLOCK_GETPARENT:
	{
		uint32_t value;

278
		ret = pm_clock_get_parent(pm_arg[0], &value, security_flag);
279
280
281
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
	}

282
283
284
285
	case PM_CLOCK_GETRATE:
	{
		uint32_t rate[2] = { 0 };

286
		ret = pm_clock_get_rate(pm_arg[0], rate, security_flag);
287
288
289
290
		SMC_RET2(handle, (uint64_t)ret | ((uint64_t)rate[0] << 32),
			 rate[1]);
	}

291
	case PM_PLL_SET_PARAMETER:
292
293
		ret = pm_pll_set_param(pm_arg[0], pm_arg[1], pm_arg[2],
				       security_flag);
294
295
296
297
298
299
		SMC_RET1(handle, (uint64_t)ret);

	case PM_PLL_GET_PARAMETER:
	{
		uint32_t value;

300
301
		ret = pm_pll_get_param(pm_arg[0], pm_arg[1], &value,
				       security_flag);
302
303
304
305
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value << 32));
	}

	case PM_PLL_SET_MODE:
306
		ret = pm_pll_set_mode(pm_arg[0], pm_arg[1], security_flag);
307
308
309
310
311
312
		SMC_RET1(handle, (uint64_t)ret);

	case PM_PLL_GET_MODE:
	{
		uint32_t mode;

313
		ret = pm_pll_get_mode(pm_arg[0], &mode, security_flag);
314
315
316
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)mode << 32));
	}

317
318
319
320
	case PM_GET_TRUSTZONE_VERSION:
		SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
			 ((uint64_t)VERSAL_TZ_VERSION << 32));

Ravi Patel's avatar
Ravi Patel committed
321
322
323
324
	case PM_GET_CHIPID:
	{
		uint32_t result[2];

325
		ret = pm_get_chipid(result, security_flag);
Ravi Patel's avatar
Ravi Patel committed
326
327
328
329
		SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
			 result[1]);
	}

330
331
332
333
	case PM_FEATURE_CHECK:
	{
		uint32_t version;

334
		ret = pm_feature_check(pm_arg[0], &version, security_flag);
335
336
337
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)version << 32));
	}

338
339
	case PM_LOAD_PDI:
	{
340
341
		ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
				  security_flag);
342
343
344
		SMC_RET1(handle, (uint64_t)ret);
	}

345
346
347
348
	case PM_GET_OP_CHARACTERISTIC:
	{
		uint32_t result;

349
350
		ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result,
					       security_flag);
351
352
353
		SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
	}

354
355
	case PM_SET_MAX_LATENCY:
	{
356
		ret = pm_set_max_latency(pm_arg[0], pm_arg[1], security_flag);
357
358
359
		SMC_RET1(handle, (uint64_t)ret);
	}

360
361
	case PM_REGISTER_NOTIFIER:
	{
362
363
		ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
					   pm_arg[3], security_flag);
364
365
366
		SMC_RET1(handle, (uint64_t)ret);
	}

367
368
369
370
371
	default:
		WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
		SMC_RET1(handle, SMC_UNK);
	}
}