pm_api_sys.c 31.4 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
 */

/*
 * Versal system level PM-API functions and communication with PMC via
 * IPI interrupts
 */

#include <pm_common.h>
#include <pm_ipi.h>
14
#include <plat/common/platform.h>
15
16
#include "pm_api_sys.h"
#include "pm_client.h"
17
#include "pm_defs.h"
18
#include "pm_svc_main.h"
19
#include "../drivers/arm/gic/v3/gicv3_private.h"
20
21
22
23
24
25
26

/*********************************************************************
 * Target module IDs macros
 ********************************************************************/
#define LIBPM_MODULE_ID		0x2
#define LOADER_MODULE_ID	0x7

27
#define  MODE	0x80000000
28
29
30
31
32
33
34
35
36
37
38
39
40
/* default shutdown/reboot scope is system(2) */
static unsigned int pm_shutdown_scope = XPM_SHUTDOWN_SUBTYPE_RST_SYSTEM;

/**
 * pm_get_shutdown_scope() - Get the currently set shutdown scope
 *
 * @return	Shutdown scope value
 */
unsigned int pm_get_shutdown_scope(void)
{
	return pm_shutdown_scope;
}

41
42
43
/**
 * Assigning of argument values into array elements.
 */
44
45
#define PM_PACK_PAYLOAD1(pl, mid, flag, arg0) {	\
	pl[0] = (uint32_t)((uint32_t)((arg0) & 0xFF) | (mid << 8) | ((flag) << 24)); \
46
47
}

48
49
50
#define PM_PACK_PAYLOAD2(pl, mid, flag, arg0, arg1) {		\
	pl[1] = (uint32_t)(arg1);				\
	PM_PACK_PAYLOAD1(pl, mid, flag, arg0);			\
51
52
}

53
54
55
#define PM_PACK_PAYLOAD3(pl, mid, flag, arg0, arg1, arg2) {	\
	pl[2] = (uint32_t)(arg2);				\
	PM_PACK_PAYLOAD2(pl, mid, flag, arg0, arg1);		\
56
57
}

58
59
60
#define PM_PACK_PAYLOAD4(pl, mid, flag, arg0, arg1, arg2, arg3) {	\
	pl[3] = (uint32_t)(arg3);					\
	PM_PACK_PAYLOAD3(pl, mid, flag, arg0, arg1, arg2);		\
61
62
}

63
#define PM_PACK_PAYLOAD5(pl, mid, flag, arg0, arg1, arg2, arg3, arg4) {	\
64
	pl[4] = (uint32_t)(arg4);					\
65
	PM_PACK_PAYLOAD4(pl, mid, flag, arg0, arg1, arg2, arg3);	\
66
67
}

68
69
70
#define PM_PACK_PAYLOAD6(pl, mid, flag, arg0, arg1, arg2, arg3, arg4, arg5) {	\
	pl[5] = (uint32_t)(arg5);						\
	PM_PACK_PAYLOAD5(pl, mid, flag, arg0, arg1, arg2, arg3, arg4);		\
71
72
}

73
74
75
76
77
/* PM API functions */

/**
 * pm_get_api_version() - Get version number of PMC PM firmware
 * @version	Returns 32-bit version number of PMC Power Management Firmware
78
79
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
80
81
82
 *
 * @return	Returns status, either success or error+reason
 */
83
enum pm_ret_status pm_get_api_version(unsigned int *version, uint32_t flag)
84
85
86
87
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
88
	PM_PACK_PAYLOAD1(payload, LIBPM_MODULE_ID, flag, PM_GET_API_VERSION);
89
90
	return pm_ipi_send_sync(primary_proc, payload, version, 1);
}
91

92
93
94
95
/**
 * pm_init_finalize() - Call to notify PMC PM firmware that master has power
 *			management enabled and that it has finished its
 *			initialization
96
97
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
98
99
100
 *
 * @return	Status returned by the PMU firmware
 */
101
enum pm_ret_status pm_init_finalize(uint32_t flag)
102
103
104
105
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMU */
106
	PM_PACK_PAYLOAD1(payload, LIBPM_MODULE_ID, flag, PM_INIT_FINALIZE);
107
108
109
	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

110
111
112
113
114
115
/**
 * pm_self_suspend() - PM call for processor to suspend itself
 * @nid		Node id of the processor or subsystem
 * @latency	Requested maximum wakeup latency (not supported)
 * @state	Requested state
 * @address	Resume address
116
117
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
118
119
120
121
122
123
124
125
126
 *
 * This is a blocking call, it will return only once PMU has responded.
 * On a wakeup, resume address will be automatically set by PMU.
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_self_suspend(uint32_t nid,
				   unsigned int latency,
				   unsigned int state,
127
				   uintptr_t address, uint32_t flag)
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{
	uint32_t payload[PAYLOAD_ARG_CNT];
	unsigned int cpuid = plat_my_core_pos();
	const struct pm_proc *proc = pm_get_proc(cpuid);

	if (!proc) {
		WARN("Failed to get proc %d\n", cpuid);
		return PM_RET_ERROR_INTERNAL;
	}

	/*
	 * Do client specific suspend operations
	 * (e.g. set powerdown request bit)
	 */
	pm_client_suspend(proc, state);

	/* Send request to the PLM */
145
	PM_PACK_PAYLOAD6(payload, LIBPM_MODULE_ID, flag, PM_SELF_SUSPEND,
146
147
148
149
150
151
152
153
154
			 proc->node_id, latency, state, address,
			 (address >> 32));
	return pm_ipi_send_sync(proc, payload, NULL, 0);
}

/**
 * pm_abort_suspend() - PM call to announce that a prior suspend request
 *			is to be aborted.
 * @reason	Reason for the abort
155
156
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
157
158
159
160
161
162
 *
 * Calling PU expects the PMU to abort the initiated suspend procedure.
 * This is a non-blocking call without any acknowledge.
 *
 * @return	Returns status, either success or error+reason
 */
163
enum pm_ret_status pm_abort_suspend(enum pm_abort_reason reason, uint32_t flag)
164
165
166
167
168
169
170
171
172
173
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/*
	 * Do client specific abort suspend operations
	 * (e.g. enable interrupts and clear powerdown request bit)
	 */
	pm_client_abort_suspend();

	/* Send request to the PLM */
174
175
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_ABORT_SUSPEND,
			 reason, primary_proc->node_id);
176
177
178
179
180
181
182
183
184
185
	return pm_ipi_send(primary_proc, payload);
}

/**
 * pm_req_suspend() - PM call to request for another PU or subsystem to
 *		      be suspended gracefully.
 * @target	Node id of the targeted PU or subsystem
 * @ack		Flag to specify whether acknowledge is requested
 * @latency	Requested wakeup latency (not supported)
 * @state	Requested state (not supported)
186
187
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
188
189
190
191
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_req_suspend(uint32_t target, uint8_t ack,
192
193
				  unsigned int latency, unsigned int state,
				  uint32_t flag)
194
195
196
197
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMU */
198
	PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_REQ_SUSPEND, target,
199
200
201
202
203
204
			 latency, state);
	if (ack == IPI_BLOCKING)
		return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
	else
		return pm_ipi_send(primary_proc, payload);
}
205

206
207
208
209
210
211
212
213
/**
 * pm_req_wakeup() - PM call for processor to wake up selected processor
 *		     or subsystem
 * @target	Device ID of the processor or subsystem to wake up
 * @set_address	Resume address presence indicator
 *		1 - resume address specified, 0 - otherwise
 * @address	Resume address
 * @ack		Flag to specify whether acknowledge requested
214
215
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
216
217
218
219
220
221
222
223
224
 *
 * This API function is either used to power up another APU core for SMP
 * (by PSCI) or to power up an entirely different PU or subsystem, such
 * as RPU0, RPU, or PL_CORE_xx. Resume address for the target PU will be
 * automatically set by PMC.
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_req_wakeup(uint32_t target, uint32_t set_address,
225
				 uintptr_t address, uint8_t ack, uint32_t flag)
226
227
228
229
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC to perform the wake of the PU */
230
	PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REQ_WAKEUP, target,
231
232
233
234
235
			 set_address, address, ack);

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

236
237
238
239
240
241
/**
 * pm_request_device() - Request a device
 * @device_id		Device ID
 * @capabilities	Requested capabilities for the device
 * @qos			Required Quality of Service
 * @ack			Flag to specify whether acknowledge requested
242
243
 * @flag		0 - Call from secure source
 *			1 - Call from non-secure source
244
245
246
247
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_request_device(uint32_t device_id, uint32_t capabilities,
248
				     uint32_t qos, uint32_t ack, uint32_t flag)
249
250
251
252
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
253
	PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REQUEST_DEVICE,
254
255
256
257
258
259
260
261
			 device_id, capabilities, qos, ack);

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_release_device() - Release a device
 * @device_id		Device ID
262
263
 * @flag		0 - Call from secure source
 *			1 - Call from non-secure source
264
265
266
 *
 * @return	Returns status, either success or error+reason
 */
267
enum pm_ret_status pm_release_device(uint32_t device_id, uint32_t flag)
268
269
270
271
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
272
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_RELEASE_DEVICE,
273
274
275
276
277
278
279
280
281
282
283
			 device_id);

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_set_requirement() - Set requirement for the device
 * @device_id		Device ID
 * @capabilities	Requested capabilities for the device
 * @latency		Requested maximum latency
 * @qos			Required Quality of Service
284
285
 * @flag		0 - Call from secure source
 *			1 - Call from non-secure source
286
287
288
289
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_set_requirement(uint32_t device_id, uint32_t capabilities,
290
291
				      uint32_t latency, uint32_t qos,
				      uint32_t flag)
292
293
294
295
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
296
	PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_SET_REQUIREMENT,
297
298
299
300
301
302
303
304
305
			 device_id, capabilities, latency, qos);

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_get_device_status() - Get device's status
 * @device_id		Device ID
 * @response		Buffer to store device status response
306
307
 * @flag		0 - Call from secure source
 *			1 - Call from non-secure source
308
309
310
 *
 * @return	Returns status, either success or error+reason
 */
311
312
enum pm_ret_status pm_get_device_status(uint32_t device_id, uint32_t *response,
					uint32_t flag)
313
314
315
316
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
317
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_GET_DEVICE_STATUS,
318
319
320
321
			 device_id);

	return pm_ipi_send_sync(primary_proc, payload, response, 3);
}
322
323
324
325
326

/**
 * pm_reset_assert() - Assert/De-assert reset
 * @reset	Reset ID
 * @assert	Assert (1) or de-assert (0)
327
328
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
329
330
331
 *
 * @return	Returns status, either success or error+reason
 */
332
enum pm_ret_status pm_reset_assert(uint32_t reset, bool assert, uint32_t flag)
333
334
335
336
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
337
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_RESET_ASSERT, reset,
338
339
340
341
342
343
344
345
346
			 assert);

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_reset_get_status() - Get current status of a reset line
 * @reset	Reset ID
 * @status	Returns current status of selected reset ID
347
348
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
349
350
351
 *
 * @return	Returns status, either success or error+reason
 */
352
353
enum pm_ret_status pm_reset_get_status(uint32_t reset, uint32_t *status,
				       uint32_t flag)
354
355
356
357
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
358
359
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_RESET_ASSERT,
			 reset);
360
361
362

	return pm_ipi_send_sync(primary_proc, payload, status, 1);
}
363

364
365
366
/**
 * pm_get_callbackdata() - Read from IPI response buffer
 * @data - array of PAYLOAD_ARG_CNT elements
367
368
 * @flag - 0 - Call from secure source
 *	   1 - Call from non-secure source
369
370
371
 *
 * Read value from ipi buffer response buffer.
 */
372
void pm_get_callbackdata(uint32_t *data, size_t count, uint32_t flag)
373
374
375
376
377
378
379
380
381
{
	/* Return if interrupt is not from PMU */
	if (!pm_ipi_irq_status(primary_proc))
		return;

	pm_ipi_buff_read_callb(data, count);
	pm_ipi_irq_clear(primary_proc);
}

382
383
384
/**
 * pm_pinctrl_request() - Request a pin
 * @pin		Pin ID
385
386
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
387
388
389
 *
 * @return	Returns status, either success or error+reason
 */
390
enum pm_ret_status pm_pinctrl_request(uint32_t pin, uint32_t flag)
391
392
393
394
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
395
396
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_PINCTRL_REQUEST,
			 pin);
397
398
399
400
401
402
403

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_pinctrl_release() - Release a pin
 * @pin		Pin ID
404
405
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
406
407
408
 *
 * @return	Returns status, either success or error+reason
 */
409
enum pm_ret_status pm_pinctrl_release(uint32_t pin, uint32_t flag)
410
411
412
413
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
414
415
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_PINCTRL_RELEASE,
			 pin);
416
417
418
419
420
421
422
423

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_pinctrl_set_function() - Set pin function
 * @pin		Pin ID
 * @function	Function ID
424
425
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
426
427
428
 *
 * @return	Returns status, either success or error+reason
 */
429
430
enum pm_ret_status pm_pinctrl_set_function(uint32_t pin, uint32_t function,
					   uint32_t flag)
431
432
433
434
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
435
436
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag,
			 PM_PINCTRL_SET_FUNCTION, pin, function)
437
438
439
440
441
442
443
444

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_pinctrl_get_function() - Get function set on the pin
 * @pin		Pin ID
 * @function	Function set on the pin
445
446
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
447
448
449
 *
 * @return	Returns status, either success or error+reason
 */
450
451
enum pm_ret_status pm_pinctrl_get_function(uint32_t pin, uint32_t *function,
					   uint32_t flag)
452
453
454
455
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
456
457
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag,
			 PM_PINCTRL_SET_FUNCTION, pin);
458
459
460
461
462
463
464
465
466

	return pm_ipi_send_sync(primary_proc, payload, function, 1);
}

/**
 * pm_pinctrl_set_pin_param() - Set configuration parameter for the pin
 * @pin		Pin ID
 * @param	Parameter ID
 * @value	Parameter value
467
468
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
469
470
471
472
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_pinctrl_set_pin_param(uint32_t pin, uint32_t param,
473
					    uint32_t value, uint32_t flag)
474
475
476
477
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
478
479
	PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag,
			 PM_PINCTRL_CONFIG_PARAM_SET, pin, param, value);
480
481
482
483
484
485
486
487
488

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_pinctrl_get_pin_param() - Get configuration parameter value for the pin
 * @pin		Pin ID
 * @param	Parameter ID
 * @value	Buffer to store parameter value
489
490
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
491
492
493
494
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_pinctrl_get_pin_param(uint32_t pin, uint32_t param,
495
					    uint32_t *value, uint32_t flag)
496
497
498
499
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
500
501
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag,
			 PM_PINCTRL_CONFIG_PARAM_GET, pin, param);
502
503
504

	return pm_ipi_send_sync(primary_proc, payload, value, 1);
}
505
506
507
508

/**
 * pm_clock_enable() - Enable the clock
 * @clk_id	Clock ID
509
510
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
511
512
513
 *
 * @return	Returns status, either success or error+reason
 */
514
enum pm_ret_status pm_clock_enable(uint32_t clk_id, uint32_t flag)
515
516
517
518
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
519
520
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_CLOCK_ENABLE,
			 clk_id);
521
522
523
524
525
526
527

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_clock_disable() - Disable the clock
 * @clk_id	Clock ID
528
529
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
530
531
532
 *
 * @return	Returns status, either success or error+reason
 */
533
enum pm_ret_status pm_clock_disable(uint32_t clk_id, uint32_t flag)
534
535
536
537
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
538
539
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_CLOCK_DISABLE,
			 clk_id);
540
541
542
543
544
545
546
547

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_clock_get_state() - Get clock status
 * @clk_id	Clock ID
 * @state:	Buffer to store clock status (1: Enabled, 0:Disabled)
548
549
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
550
551
552
 *
 * @return	Returns status, either success or error+reason
 */
553
554
enum pm_ret_status pm_clock_get_state(uint32_t clk_id, uint32_t *state,
				      uint32_t flag)
555
556
557
558
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
559
560
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_CLOCK_GETSTATE,
			 clk_id);
561
562
563
564
565
566
567
568

	return pm_ipi_send_sync(primary_proc, payload, state, 1);
}

/**
 * pm_clock_set_divider() - Set divider for the clock
 * @clk_id	Clock ID
 * @divider	Divider value
569
570
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
571
572
573
 *
 * @return	Returns status, either success or error+reason
 */
574
575
enum pm_ret_status pm_clock_set_divider(uint32_t clk_id, uint32_t divider,
					uint32_t flag)
576
577
578
579
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
580
581
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_CLOCK_SETDIVIDER,
			 clk_id, divider);
582
583
584
585
586
587
588
589

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_clock_get_divider() - Get divider value for the clock
 * @clk_id	Clock ID
 * @divider:	Buffer to store clock divider value
590
591
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
592
593
594
 *
 * @return	Returns status, either success or error+reason
 */
595
596
enum pm_ret_status pm_clock_get_divider(uint32_t clk_id, uint32_t *divider,
					uint32_t flag)
597
598
599
600
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
601
602
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_CLOCK_GETDIVIDER,
			 clk_id);
603
604
605
606
607
608
609
610

	return pm_ipi_send_sync(primary_proc, payload, divider, 1);
}

/**
 * pm_clock_set_parent() - Set parent for the clock
 * @clk_id	Clock ID
 * @parent	Parent ID
611
612
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
613
614
615
 *
 * @return	Returns status, either success or error+reason
 */
616
617
enum pm_ret_status pm_clock_set_parent(uint32_t clk_id, uint32_t parent,
				       uint32_t flag)
618
619
620
621
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
622
623
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_CLOCK_SETPARENT,
			 clk_id, parent);
624
625
626
627
628
629
630
631

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_clock_get_parent() - Get parent value for the clock
 * @clk_id	Clock ID
 * @parent:	Buffer to store clock parent value
632
633
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
634
635
636
 *
 * @return	Returns status, either success or error+reason
 */
637
638
enum pm_ret_status pm_clock_get_parent(uint32_t clk_id, uint32_t *parent,
				       uint32_t flag)
639
640
641
642
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
643
644
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_CLOCK_GETPARENT,
			 clk_id);
645
646
647

	return pm_ipi_send_sync(primary_proc, payload, parent, 1);
}
648
649
650
651
/**
 * pm_clock_get_rate() - Get the rate value for the clock
 * @clk_id	Clock ID
 * @rate:	Buffer to store clock rate value
652
653
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
654
655
656
 *
 * @return	Returns status, either success or error+reason
 */
657
658
enum pm_ret_status pm_clock_get_rate(uint32_t clk_id, uint32_t *clk_rate,
				     uint32_t flag)
659
660
661
662
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
663
664
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_CLOCK_GETRATE,
			 clk_id);
665
666
667

	return pm_ipi_send_sync(primary_proc, payload, clk_rate, 2);
}
668
669
670
671
672
673

/**
 * pm_pll_set_param() - Set PLL parameter
 * @clk_id	PLL clock ID
 * @param	PLL parameter ID
 * @value	Value to set for PLL parameter
674
675
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
676
677
678
679
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_pll_set_param(uint32_t clk_id, uint32_t param,
680
				    uint32_t value, uint32_t flag)
681
682
683
684
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
685
686
	PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_PLL_SET_PARAMETER,
			 clk_id, param, value);
687
688
689
690
691
692
693
694
695

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_pll_get_param() - Get PLL parameter value
 * @clk_id	PLL clock ID
 * @param	PLL parameter ID
 * @value:	Buffer to store PLL parameter value
696
697
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
698
699
700
701
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_pll_get_param(uint32_t clk_id, uint32_t param,
702
				    uint32_t *value, uint32_t flag)
703
704
705
706
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
707
708
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_PLL_GET_PARAMETER,
			 clk_id, param);
709
710
711
712
713
714
715
716

	return pm_ipi_send_sync(primary_proc, payload, value, 1);
}

/**
 * pm_pll_set_mode() - Set PLL mode
 * @clk_id	PLL clock ID
 * @mode	PLL mode
717
718
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
719
720
721
 *
 * @return	Returns status, either success or error+reason
 */
722
723
enum pm_ret_status pm_pll_set_mode(uint32_t clk_id, uint32_t mode,
				   uint32_t flag)
724
725
726
727
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
728
729
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_PLL_SET_MODE,
			 clk_id, mode);
730
731
732
733
734
735
736
737

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}

/**
 * pm_pll_get_mode() - Get PLL mode
 * @clk_id	PLL clock ID
 * @mode:	Buffer to store PLL mode
738
739
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
740
741
742
 *
 * @return	Returns status, either success or error+reason
 */
743
744
enum pm_ret_status pm_pll_get_mode(uint32_t clk_id, uint32_t *mode,
				   uint32_t flag)
745
746
747
748
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
749
750
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag, PM_PLL_GET_MODE,
			 clk_id);
751
752
753

	return pm_ipi_send_sync(primary_proc, payload, mode, 1);
}
754
755
756
757
758
759

/**
 * pm_force_powerdown() - PM call to request for another PU or subsystem to
 *			  be powered down forcefully
 * @target	Device ID of the PU node to be forced powered down.
 * @ack		Flag to specify whether acknowledge is requested
760
761
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
762
763
764
 *
 * @return	Returns status, either success or error+reason
 */
765
766
enum pm_ret_status pm_force_powerdown(uint32_t target, uint8_t ack,
				      uint32_t flag)
767
768
769
770
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
771
772
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_FORCE_POWERDOWN,
			 target, ack);
773
774
775
776
777
778
779
780
781

	if (ack == IPI_BLOCKING)
		return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
	else
		return pm_ipi_send(primary_proc, payload);
}

/**
 * pm_system_shutdown() - PM call to request a system shutdown or restart
782
 * @type	Shutdown or restart? 0=shutdown, 1=restart, 2=setscope
783
 * @subtype	Scope: 0=APU-subsystem, 1=PS, 2=system
784
785
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
786
787
788
 *
 * @return	Returns status, either success or error+reason
 */
789
790
enum pm_ret_status pm_system_shutdown(uint32_t type, uint32_t subtype,
				      uint32_t flag)
791
792
793
{
	uint32_t payload[PAYLOAD_ARG_CNT];

794
795
796
797
798
799
	if (type == XPM_SHUTDOWN_TYPE_SETSCOPE_ONLY) {
		/* Setting scope for subsequent PSCI reboot or shutdown */
		pm_shutdown_scope = subtype;
		return PM_RET_SUCCESS;
	}

800
	/* Send request to the PMC */
801
802
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SYSTEM_SHUTDOWN,
			 type, subtype);
803
804
805

	return pm_ipi_send_non_blocking(primary_proc, payload);
}
806

807
808
809
810
811
812
813
/**
* pm_query_data() -  PM API for querying firmware data
* @qid	The type of data to query
* @arg1	Argument 1 to requested query data call
* @arg2	Argument 2 to requested query data call
* @arg3	Argument 3 to requested query data call
* @data	Returned output data
814
815
* @flag 0 - Call from secure source
*	1 - Call from non-secure source
816
817
818
819
*
* This function returns requested data.
*/
enum pm_ret_status pm_query_data(uint32_t qid, uint32_t arg1, uint32_t arg2,
820
				 uint32_t arg3, uint32_t *data, uint32_t flag)
821
{
822
823
	uint32_t ret;
	uint32_t version;
824
	uint32_t payload[PAYLOAD_ARG_CNT];
825
	uint32_t fw_api_version;
826
827

	/* Send request to the PMC */
828
829
	PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_QUERY_DATA, qid,
			 arg1, arg2, arg3);
830

831
	ret = pm_feature_check(PM_QUERY_DATA, &version, flag);
832
	if (PM_RET_SUCCESS == ret) {
833
834
835
836
837
838
839
840
841
842
843
844
845
846
		fw_api_version = version & 0xFFFF ;
		if ((2U == fw_api_version) &&
		    ((XPM_QID_CLOCK_GET_NAME == qid) ||
		     (XPM_QID_PINCTRL_GET_FUNCTION_NAME == qid))) {
			ret = pm_ipi_send_sync(primary_proc, payload, data, 8);
			ret = data[0];
			data[0] = data[1];
			data[1] = data[2];
			data[2] = data[3];
		} else {
			ret = pm_ipi_send_sync(primary_proc, payload, data, 4);
		}
	}
	return ret;
847
}
848
849
850
851
852
853
854
/**
 * pm_api_ioctl() -  PM IOCTL API for device control and configs
 * @device_id	Device ID
 * @ioctl_id	ID of the requested IOCTL
 * @arg1	Argument 1 to requested IOCTL call
 * @arg2	Argument 2 to requested IOCTL call
 * @value	Returned output value
855
856
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
857
858
859
860
861
862
 *
 * This function calls IOCTL to firmware for device control and configuration.
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_api_ioctl(uint32_t device_id, uint32_t ioctl_id,
863
864
				uint32_t arg1, uint32_t arg2, uint32_t *value,
				uint32_t flag)
865
866
{
	uint32_t payload[PAYLOAD_ARG_CNT];
867
	int ret;
868
869
870

	switch (ioctl_id) {
	case IOCTL_SET_PLL_FRAC_MODE:
871
		return pm_pll_set_mode(arg1, arg2, flag);
872
	case IOCTL_GET_PLL_FRAC_MODE:
873
		return pm_pll_get_mode(arg1, value, flag);
874
	case IOCTL_SET_PLL_FRAC_DATA:
875
		return pm_pll_set_param(arg1, PM_PLL_PARAM_DATA, arg2, flag);
876
	case IOCTL_GET_PLL_FRAC_DATA:
877
		return pm_pll_get_param(arg1, PM_PLL_PARAM_DATA, value, flag);
878
879
880
881
882
883
884
885
886
	case IOCTL_SET_SGI:
		/* Get the sgi number */
		ret = pm_register_sgi(arg1);
		if (ret) {
			return PM_RET_ERROR_ARGS;
		}
		gicd_write_irouter(gicv3_driver_data->gicd_base,
				PLAT_VERSAL_IPI_IRQ, MODE);
		return PM_RET_SUCCESS;
887
888
	default:
		/* Send request to the PMC */
889
890
		PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_IOCTL,
				 device_id, ioctl_id, arg1, arg2);
891
892
893
		return pm_ipi_send_sync(primary_proc, payload, value, 1);
	}
}
894
895
896
897
898
899

/**
 * pm_set_wakeup_source() - PM call to specify the wakeup source while suspended
 * @target	Device id of the targeted PU or subsystem
 * @wkup_node	Device id of the wakeup peripheral
 * @enable	Enable or disable the specified peripheral as wake source
900
901
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
902
903
904
905
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_set_wakeup_source(uint32_t target, uint32_t wkup_device,
906
					uint8_t enable, uint32_t flag)
907
908
909
{
	uint32_t payload[PAYLOAD_ARG_CNT];

910
911
	PM_PACK_PAYLOAD4(payload, LIBPM_MODULE_ID, flag, PM_SET_WAKEUP_SOURCE,
			 target, wkup_device, enable);
912
913
	return pm_ipi_send(primary_proc, payload);
}
914

Ravi Patel's avatar
Ravi Patel committed
915
916
917
918
/**
 * pm_get_chipid() - Read silicon ID registers
 * @value       Buffer for return values. Must be large enough
 *		to hold 8 bytes.
919
920
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
Ravi Patel's avatar
Ravi Patel committed
921
922
923
 *
 * @return      Returns silicon ID registers
 */
924
enum pm_ret_status pm_get_chipid(uint32_t *value, uint32_t flag)
Ravi Patel's avatar
Ravi Patel committed
925
926
927
928
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
929
	PM_PACK_PAYLOAD1(payload, LIBPM_MODULE_ID, flag, PM_GET_CHIPID);
Ravi Patel's avatar
Ravi Patel committed
930
931
932
933

	return pm_ipi_send_sync(primary_proc, payload, value, 2);
}

934
935
936
937
/**
 * pm_feature_check() - Returns the supported API version if supported
 * @api_id	API ID to check
 * @value	Returned supported API version
938
939
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
940
941
942
 *
 * @return	Returns status, either success or error+reason
 */
943
944
enum pm_ret_status pm_feature_check(uint32_t api_id, unsigned int *version,
				    uint32_t flag)
945
946
947
948
949
950
951
{
	uint32_t payload[PAYLOAD_ARG_CNT], fw_api_version;
	uint32_t status;

	switch (api_id) {
	case PM_GET_CALLBACK_DATA:
	case PM_GET_TRUSTZONE_VERSION:
952
	case PM_LOAD_PDI:
953
954
955
956
		*version = (PM_API_BASE_VERSION << 16);
		return PM_RET_SUCCESS;
	case PM_GET_API_VERSION:
	case PM_GET_DEVICE_STATUS:
957
	case PM_GET_OP_CHARACTERISTIC:
958
959
960
961
962
963
964
965
966
967
968
969
	case PM_REQ_SUSPEND:
	case PM_SELF_SUSPEND:
	case PM_FORCE_POWERDOWN:
	case PM_ABORT_SUSPEND:
	case PM_REQ_WAKEUP:
	case PM_SET_WAKEUP_SOURCE:
	case PM_SYSTEM_SHUTDOWN:
	case PM_REQUEST_DEVICE:
	case PM_RELEASE_DEVICE:
	case PM_SET_REQUIREMENT:
	case PM_RESET_ASSERT:
	case PM_RESET_GET_STATUS:
970
	case PM_GET_CHIPID:
971
972
973
974
975
976
977
978
979
980
981
982
983
984
	case PM_PINCTRL_REQUEST:
	case PM_PINCTRL_RELEASE:
	case PM_PINCTRL_GET_FUNCTION:
	case PM_PINCTRL_SET_FUNCTION:
	case PM_PINCTRL_CONFIG_PARAM_GET:
	case PM_PINCTRL_CONFIG_PARAM_SET:
	case PM_IOCTL:
	case PM_CLOCK_ENABLE:
	case PM_CLOCK_DISABLE:
	case PM_CLOCK_GETSTATE:
	case PM_CLOCK_SETDIVIDER:
	case PM_CLOCK_GETDIVIDER:
	case PM_CLOCK_SETPARENT:
	case PM_CLOCK_GETPARENT:
985
	case PM_CLOCK_GETRATE:
986
987
988
989
990
	case PM_PLL_SET_PARAMETER:
	case PM_PLL_GET_PARAMETER:
	case PM_PLL_SET_MODE:
	case PM_PLL_GET_MODE:
	case PM_FEATURE_CHECK:
991
	case PM_INIT_FINALIZE:
992
	case PM_SET_MAX_LATENCY:
993
	case PM_REGISTER_NOTIFIER:
994
995
		*version = (PM_API_BASE_VERSION << 16);
		break;
996
997
998
	case PM_QUERY_DATA:
		*version = (PM_API_QUERY_DATA_VERSION << 16);
		break;
999
1000
1001
1002
1003
	default:
		*version = 0U;
		return PM_RET_ERROR_NOFEATURE;
	}

1004
1005
	PM_PACK_PAYLOAD2(payload, LIBPM_MODULE_ID, flag,
			 PM_FEATURE_CHECK, api_id);
1006
1007
1008
1009
1010
1011
1012
1013
1014

	status = pm_ipi_send_sync(primary_proc, payload, &fw_api_version, 1);
	if (status != PM_RET_SUCCESS)
		return status;

	*version |= fw_api_version;

	return PM_RET_SUCCESS;
}
1015
1016
1017
1018
1019
1020
1021
1022
1023

/**
 * pm_load_pdi() - Load the PDI
 *
 * This function provides support to load PDI from linux
 *
 * src:        Source device of pdi(DDR, OCM, SD etc)
 * address_low: lower 32-bit Linear memory space address
 * address_high: higher 32-bit Linear memory space address
1024
1025
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
1026
1027
1028
 *
 * @return      Returns status, either success or error+reason
 */
1029
1030
enum pm_ret_status pm_load_pdi(uint32_t src, uint32_t address_low,
			       uint32_t address_high, uint32_t flag)
1031
1032
1033
1034
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMU */
1035
	PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, flag, PM_LOAD_PDI, src,
1036
1037
1038
			 address_high, address_low);
	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}
1039
1040
1041
1042
1043
1044
1045
1046
1047

/**
 * pm_get_op_characteristic() - PM call to request operating characteristics
 *                              of a device
 * @device_id   Device id
 * @type        Type of the operating characteristic
 *              (power, temperature and latency)
 * @result      Returns the operating characteristic for the requested device,
 *              specified by the type
1048
1049
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
1050
1051
1052
1053
1054
 *
 * @return      Returns status, either success or error+reason
 */
enum pm_ret_status pm_get_op_characteristic(uint32_t device_id,
					    enum pm_opchar_type type,
1055
					    uint32_t *result, uint32_t flag)
1056
1057
1058
1059
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
1060
1061
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag,
			 PM_GET_OP_CHARACTERISTIC, device_id, type);
1062
1063
	return pm_ipi_send_sync(primary_proc, payload, result, 1);
}
1064
1065
1066
1067
1068
1069
1070

/**
 * pm_set_max_latency() - PM call to change in the maximum wake-up latency
 *			  requirements for a specific device currently
 *			  used by that CPU.
 * @device_id	Device ID
 * @latency	Latency value
1071
1072
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
1073
1074
1075
 *
 * @return	Returns status, either success or error+reason
 */
1076
1077
enum pm_ret_status pm_set_max_latency(uint32_t device_id, uint32_t latency,
				      uint32_t flag)
1078
1079
1080
1081
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
1082
	PM_PACK_PAYLOAD3(payload, LIBPM_MODULE_ID, flag, PM_SET_MAX_LATENCY,
1083
1084
1085
1086
			 device_id, latency);

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}
1087
1088
1089
1090
1091
1092
1093
1094

/**
 * pm_register_notifier() - PM call to register a subsystem to be notified
 * 			    about the device event
 * @device_id	Device ID for the Node to which the event is related
 * @event	Event in question
 * @wake	Wake subsystem upon capturing the event if value 1
 * @enable	Enable the registration for value 1, disable for value 0
1095
1096
 * @flag	0 - Call from secure source
 *		1 - Call from non-secure source
1097
1098
1099
1100
 *
 * @return	Returns status, either success or error+reason
 */
enum pm_ret_status pm_register_notifier(uint32_t device_id, uint32_t event,
1101
1102
					uint32_t wake, uint32_t enable,
					uint32_t flag)
1103
1104
1105
1106
{
	uint32_t payload[PAYLOAD_ARG_CNT];

	/* Send request to the PMC */
1107
	PM_PACK_PAYLOAD5(payload, LIBPM_MODULE_ID, flag, PM_REGISTER_NOTIFIER,
1108
1109
1110
1111
			 device_id, event, wake, enable);

	return pm_ipi_send_sync(primary_proc, payload, NULL, 0);
}