xref: /linux/drivers/firmware/xilinx/zynqmp.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Zynq MPSoC Firmware layer
4  *
5  *  Copyright (C) 2014-2022 Xilinx, Inc.
6  *  Copyright (C) 2022 - 2026 Advanced Micro Devices, Inc.
7  *
8  *  Michal Simek <michal.simek@amd.com>
9  *  Davorin Mista <davorin.mista@aggios.com>
10  *  Jolly Shah <jollys@xilinx.com>
11  *  Rajan Vaja <rajanv@xilinx.com>
12  */
13 
14 #include <linux/arm-smccc.h>
15 #include <linux/compiler.h>
16 #include <linux/crash_dump.h>
17 #include <linux/device.h>
18 #include <linux/init.h>
19 #include <linux/mfd/core.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_domain.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
27 #include <linux/hashtable.h>
28 
29 #include <linux/firmware/xlnx-zynqmp.h>
30 #include <linux/firmware/xlnx-event-manager.h>
31 #include "zynqmp-debug.h"
32 
33 /* Max HashMap Order for PM API feature check (1<<7 = 128) */
34 #define PM_API_FEATURE_CHECK_MAX_ORDER  7
35 
36 /* CRL registers and bitfields */
37 #define CRL_APB_BASE			0xFF5E0000U
38 /* BOOT_PIN_CTRL- Used to control the mode pins after boot */
39 #define CRL_APB_BOOT_PIN_CTRL		(CRL_APB_BASE + (0x250U))
40 /* BOOT_PIN_CTRL_MASK- out_val[11:8], out_en[3:0] */
41 #define CRL_APB_BOOTPIN_CTRL_MASK	0xF0FU
42 
43 /* IOCTL/QUERY feature payload size */
44 #define FEATURE_PAYLOAD_SIZE		2
45 
46 static bool feature_check_enabled;
47 static DEFINE_HASHTABLE(pm_api_features_map, PM_API_FEATURE_CHECK_MAX_ORDER);
48 static u32 ioctl_features[FEATURE_PAYLOAD_SIZE];
49 static u32 query_features[FEATURE_PAYLOAD_SIZE];
50 
51 static u32 sip_svc_version;
52 static struct platform_device *em_dev;
53 
54 /**
55  * struct zynqmp_devinfo - Structure for Zynqmp device instance
56  * @dev:		Device Pointer
57  * @feature_conf_id:	Feature conf id
58  */
59 struct zynqmp_devinfo {
60 	struct device *dev;
61 	u32 feature_conf_id;
62 };
63 
64 /**
65  * struct pm_api_feature_data - PM API Feature data
66  * @pm_api_id:		PM API Id, used as key to index into hashmap
67  * @feature_status:	status of PM API feature: valid, invalid
68  * @hentry:		hlist_node that hooks this entry into hashtable
69  */
70 struct pm_api_feature_data {
71 	u32 pm_api_id;
72 	int feature_status;
73 	struct hlist_node hentry;
74 };
75 
76 struct platform_fw_data {
77 	/*
78 	 * Family code for platform.
79 	 */
80 	const u32 family_code;
81 };
82 
83 static struct platform_fw_data *active_platform_fw_data;
84 
85 static const struct mfd_cell firmware_devs[] = {
86 	{
87 		.name = "zynqmp_power_controller",
88 	},
89 };
90 
91 /**
92  * zynqmp_pm_ret_code() - Convert PMU-FW error codes to Linux error codes
93  * @ret_status:		PMUFW return code
94  *
95  * Return: corresponding Linux error code
96  */
97 static int zynqmp_pm_ret_code(u32 ret_status)
98 {
99 	switch (ret_status) {
100 	case XST_PM_SUCCESS:
101 	case XST_PM_DOUBLE_REQ:
102 		return 0;
103 	case XST_PM_NO_FEATURE:
104 		return -ENOTSUPP;
105 	case XST_PM_INVALID_VERSION:
106 		return -EOPNOTSUPP;
107 	case XST_PM_NO_ACCESS:
108 		return -EACCES;
109 	case XST_PM_ABORT_SUSPEND:
110 		return -ECANCELED;
111 	case XST_PM_MULT_USER:
112 		return -EUSERS;
113 	case XST_PM_INTERNAL:
114 	case XST_PM_CONFLICT:
115 	case XST_PM_INVALID_NODE:
116 	case XST_PM_INVALID_CRC:
117 	default:
118 		return -EINVAL;
119 	}
120 }
121 
122 static noinline int do_fw_call_fail(u32 *ret_payload, u32 num_args, ...)
123 {
124 	return -ENODEV;
125 }
126 
127 /*
128  * PM function call wrapper
129  * Invoke do_fw_call_smc or do_fw_call_hvc, depending on the configuration
130  */
131 static int (*do_fw_call)(u32 *ret_payload, u32, ...) = do_fw_call_fail;
132 
133 /**
134  * do_fw_call_smc() - Call system-level platform management layer (SMC)
135  * @num_args:		Number of variable arguments should be <= 8
136  * @ret_payload:	Returned value array
137  *
138  * Invoke platform management function via SMC call (no hypervisor present).
139  *
140  * Return: Returns status, either success or error+reason
141  */
142 static noinline int do_fw_call_smc(u32 *ret_payload, u32 num_args, ...)
143 {
144 	struct arm_smccc_res res;
145 	u64 args[8] = {0};
146 	va_list arg_list;
147 	u8 i;
148 
149 	if (num_args > 8)
150 		return -EINVAL;
151 
152 	va_start(arg_list, num_args);
153 
154 	for (i = 0; i < num_args; i++)
155 		args[i] = va_arg(arg_list, u64);
156 
157 	va_end(arg_list);
158 
159 	arm_smccc_smc(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &res);
160 
161 	if (ret_payload) {
162 		ret_payload[0] = lower_32_bits(res.a0);
163 		ret_payload[1] = upper_32_bits(res.a0);
164 		ret_payload[2] = lower_32_bits(res.a1);
165 		ret_payload[3] = upper_32_bits(res.a1);
166 		ret_payload[4] = lower_32_bits(res.a2);
167 		ret_payload[5] = upper_32_bits(res.a2);
168 		ret_payload[6] = lower_32_bits(res.a3);
169 	}
170 
171 	return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
172 }
173 
174 /**
175  * do_fw_call_hvc() - Call system-level platform management layer (HVC)
176  * @num_args:		Number of variable arguments should be <= 8
177  * @ret_payload:	Returned value array
178  *
179  * Invoke platform management function via HVC
180  * HVC-based for communication through hypervisor
181  * (no direct communication with ATF).
182  *
183  * Return: Returns status, either success or error+reason
184  */
185 static noinline int do_fw_call_hvc(u32 *ret_payload, u32 num_args, ...)
186 {
187 	struct arm_smccc_res res;
188 	u64 args[8] = {0};
189 	va_list arg_list;
190 	u8 i;
191 
192 	if (num_args > 8)
193 		return -EINVAL;
194 
195 	va_start(arg_list, num_args);
196 
197 	for (i = 0; i < num_args; i++)
198 		args[i] = va_arg(arg_list, u64);
199 
200 	va_end(arg_list);
201 
202 	arm_smccc_hvc(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &res);
203 
204 	if (ret_payload) {
205 		ret_payload[0] = lower_32_bits(res.a0);
206 		ret_payload[1] = upper_32_bits(res.a0);
207 		ret_payload[2] = lower_32_bits(res.a1);
208 		ret_payload[3] = upper_32_bits(res.a1);
209 		ret_payload[4] = lower_32_bits(res.a2);
210 		ret_payload[5] = upper_32_bits(res.a2);
211 		ret_payload[6] = lower_32_bits(res.a3);
212 	}
213 
214 	return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
215 }
216 
217 static int __do_feature_check_call(const u32 api_id, u32 *ret_payload)
218 {
219 	int ret;
220 	u64 smc_arg[2];
221 	u32 module_id;
222 	u32 feature_check_api_id;
223 
224 	module_id = FIELD_GET(MODULE_ID_MASK, api_id);
225 
226 	/*
227 	 * Feature check of APIs belonging to PM and XSEM are handled by calling
228 	 * PM_FEATURE_CHECK API. For other modules, call PM_API_FEATURES API.
229 	 */
230 	if (module_id == PM_MODULE_ID || module_id == XSEM_MODULE_ID)
231 		feature_check_api_id = PM_FEATURE_CHECK;
232 	else
233 		feature_check_api_id = PM_API_FEATURES;
234 
235 	if (module_id == TF_A_MODULE_ID)
236 		smc_arg[1] = api_id;
237 	else
238 		smc_arg[1] = (api_id & API_ID_MASK);
239 
240 	smc_arg[0] = PM_SIP_SVC | FIELD_PREP(MODULE_ID_MASK, module_id) | feature_check_api_id;
241 
242 	ret = do_fw_call(ret_payload, 2, smc_arg[0], smc_arg[1]);
243 
244 	/*
245 	 * For TF-A APIs, if the feature check with PM_API_FEATURES fails,
246 	 * retry with the legacy PM_FEATURE_CHECK for backward compatibility.
247 	 */
248 	if (module_id == TF_A_MODULE_ID && ret) {
249 		smc_arg[0] = PM_SIP_SVC | FIELD_PREP(MODULE_ID_MASK, PM_MODULE_ID) |
250 			     PM_FEATURE_CHECK;
251 		ret = do_fw_call(ret_payload, 2, smc_arg[0], smc_arg[1]);
252 	}
253 
254 	if (ret)
255 		return ret;
256 
257 	return ret_payload[1];
258 }
259 
260 static int do_feature_check_call(const u32 api_id)
261 {
262 	int ret;
263 	u32 ret_payload[PAYLOAD_ARG_CNT];
264 	struct pm_api_feature_data *feature_data;
265 
266 	/* Check for existing entry in hash table for given api */
267 	hash_for_each_possible(pm_api_features_map, feature_data, hentry,
268 			       api_id) {
269 		if (feature_data->pm_api_id == api_id)
270 			return feature_data->feature_status;
271 	}
272 
273 	/* Add new entry if not present */
274 	feature_data = kmalloc_obj(*feature_data, GFP_ATOMIC);
275 	if (!feature_data)
276 		return -ENOMEM;
277 
278 	feature_data->pm_api_id = api_id;
279 	ret = __do_feature_check_call(api_id, ret_payload);
280 
281 	feature_data->feature_status = ret;
282 	hash_add(pm_api_features_map, &feature_data->hentry, api_id);
283 
284 	if (api_id == PM_IOCTL)
285 		/* Store supported IOCTL IDs mask */
286 		memcpy(ioctl_features, &ret_payload[2], FEATURE_PAYLOAD_SIZE * 4);
287 	else if (api_id == PM_QUERY_DATA)
288 		/* Store supported QUERY IDs mask */
289 		memcpy(query_features, &ret_payload[2], FEATURE_PAYLOAD_SIZE * 4);
290 
291 	return ret;
292 }
293 
294 /**
295  * zynqmp_pm_feature() - Check whether given feature is supported or not and
296  *			 store supported IOCTL/QUERY ID mask
297  * @api_id:		API ID to check
298  *
299  * Return: Returns status, either success or error+reason
300  */
301 int zynqmp_pm_feature(const u32 api_id)
302 {
303 	int ret;
304 
305 	if (!feature_check_enabled)
306 		return 0;
307 
308 	ret = do_feature_check_call(api_id);
309 
310 	return ret;
311 }
312 EXPORT_SYMBOL_GPL(zynqmp_pm_feature);
313 
314 /**
315  * zynqmp_pm_is_function_supported() - Check whether given IOCTL/QUERY function
316  *				       is supported or not
317  * @api_id:		PM_IOCTL or PM_QUERY_DATA
318  * @id:			IOCTL or QUERY function IDs
319  *
320  * Return: Returns status, either success or error+reason
321  */
322 int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
323 {
324 	int ret;
325 	u32 *bit_mask;
326 
327 	/* Input arguments validation */
328 	if (id >= 64 || (api_id != PM_IOCTL && api_id != PM_QUERY_DATA))
329 		return -EINVAL;
330 
331 	/* Check feature check API version */
332 	ret = do_feature_check_call(PM_FEATURE_CHECK);
333 	if (ret < 0)
334 		return ret;
335 
336 	/* Check if feature check version 2 is supported or not */
337 	if ((ret & FIRMWARE_VERSION_MASK) == PM_API_VERSION_2) {
338 		/*
339 		 * Call feature check for IOCTL/QUERY API to get IOCTL ID or
340 		 * QUERY ID feature status.
341 		 */
342 		ret = do_feature_check_call(api_id);
343 		if (ret < 0)
344 			return ret;
345 
346 		bit_mask = (api_id == PM_IOCTL) ? ioctl_features : query_features;
347 
348 		if ((bit_mask[(id / 32)] & BIT((id % 32))) == 0U)
349 			return -EOPNOTSUPP;
350 	} else {
351 		return -ENODATA;
352 	}
353 
354 	return 0;
355 }
356 EXPORT_SYMBOL_GPL(zynqmp_pm_is_function_supported);
357 
358 /**
359  * zynqmp_pm_invoke_fw_fn() - Invoke the system-level platform management layer
360  *			caller function depending on the configuration
361  * @pm_api_id:		Requested PM-API call
362  * @ret_payload:	Returned value array
363  * @num_args:		Number of arguments to requested PM-API call
364  *
365  * Invoke platform management function for SMC or HVC call, depending on
366  * configuration.
367  * Following SMC Calling Convention (SMCCC) for SMC64:
368  * Pm Function Identifier,
369  * PM_SIP_SVC + PASS_THROUGH_FW_CMD_ID =
370  *	((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)
371  *	((SMC_64) << FUNCID_CC_SHIFT)
372  *	((SIP_START) << FUNCID_OEN_SHIFT)
373  *	(PASS_THROUGH_FW_CMD_ID))
374  *
375  * PM_SIP_SVC - Registered ZynqMP SIP Service Call.
376  * PASS_THROUGH_FW_CMD_ID - Fixed SiP SVC call ID for FW specific calls.
377  *
378  * Return: Returns status, either success or error+reason
379  */
380 int zynqmp_pm_invoke_fw_fn(u32 pm_api_id, u32 *ret_payload, u32 num_args, ...)
381 {
382 	/*
383 	 * Added SIP service call Function Identifier
384 	 * Make sure to stay in x0 register
385 	 */
386 	u64 smc_arg[SMC_ARG_CNT_64];
387 	int ret, i;
388 	va_list arg_list;
389 	u32 args[SMC_ARG_CNT_32] = {0};
390 	u32 module_id;
391 
392 	if (num_args > SMC_ARG_CNT_32)
393 		return -EINVAL;
394 
395 	va_start(arg_list, num_args);
396 
397 	/* Check if feature is supported or not */
398 	ret = zynqmp_pm_feature(pm_api_id);
399 	if (ret < 0)
400 		return ret;
401 
402 	for (i = 0; i < num_args; i++)
403 		args[i] = va_arg(arg_list, u32);
404 
405 	va_end(arg_list);
406 
407 	module_id = FIELD_GET(PLM_MODULE_ID_MASK, pm_api_id);
408 
409 	if (module_id == 0)
410 		module_id = XPM_MODULE_ID;
411 
412 	smc_arg[0] = PM_SIP_SVC | PASS_THROUGH_FW_CMD_ID;
413 	smc_arg[1] = ((u64)args[0] << 32U) | FIELD_PREP(PLM_MODULE_ID_MASK, module_id) |
414 		      (pm_api_id & API_ID_MASK);
415 	for (i = 1; i < (SMC_ARG_CNT_64 - 1); i++)
416 		smc_arg[i + 1] = ((u64)args[(i * 2)] << 32U) | args[(i * 2) - 1];
417 
418 	return do_fw_call(ret_payload, 8, smc_arg[0], smc_arg[1], smc_arg[2], smc_arg[3],
419 			  smc_arg[4], smc_arg[5], smc_arg[6], smc_arg[7]);
420 }
421 
422 /**
423  * zynqmp_pm_invoke_fn() - Invoke the system-level platform management layer
424  *			   caller function depending on the configuration
425  * @pm_api_id:		Requested PM-API call
426  * @ret_payload:	Returned value array
427  * @num_args:		Number of arguments to requested PM-API call
428  *
429  * Invoke platform management function for SMC or HVC call, depending on
430  * configuration.
431  * Following SMC Calling Convention (SMCCC) for SMC64:
432  * Pm Function Identifier,
433  * PM_SIP_SVC + PM_API_ID =
434  *	((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)
435  *	((SMC_64) << FUNCID_CC_SHIFT)
436  *	((SIP_START) << FUNCID_OEN_SHIFT)
437  *	((PM_API_ID) & FUNCID_NUM_MASK))
438  *
439  * PM_SIP_SVC	- Registered ZynqMP SIP Service Call.
440  * PM_API_ID	- Platform Management API ID.
441  *
442  * Return: Returns status, either success or error+reason
443  */
444 int zynqmp_pm_invoke_fn(u32 pm_api_id, u32 *ret_payload, u32 num_args, ...)
445 {
446 	/*
447 	 * Added SIP service call Function Identifier
448 	 * Make sure to stay in x0 register
449 	 */
450 	u64 smc_arg[8];
451 	int ret, i;
452 	va_list arg_list;
453 	u32 args[14] = {0};
454 
455 	if (num_args > 14)
456 		return -EINVAL;
457 
458 	va_start(arg_list, num_args);
459 
460 	/* Check if feature is supported or not */
461 	ret = zynqmp_pm_feature(pm_api_id);
462 	if (ret < 0)
463 		return ret;
464 
465 	for (i = 0; i < num_args; i++)
466 		args[i] = va_arg(arg_list, u32);
467 
468 	va_end(arg_list);
469 
470 	smc_arg[0] = PM_SIP_SVC | pm_api_id;
471 	for (i = 0; i < 7; i++)
472 		smc_arg[i + 1] = ((u64)args[(i * 2) + 1] << 32) | args[i * 2];
473 
474 	return do_fw_call(ret_payload, 8, smc_arg[0], smc_arg[1], smc_arg[2], smc_arg[3],
475 			  smc_arg[4], smc_arg[5], smc_arg[6], smc_arg[7]);
476 }
477 
478 static u32 pm_api_version;
479 static u32 pm_tz_version;
480 
481 int zynqmp_pm_register_sgi(u32 sgi_num, u32 reset)
482 {
483 	int ret;
484 
485 	ret = zynqmp_pm_invoke_fn(TF_A_PM_REGISTER_SGI, NULL, 2, sgi_num, reset);
486 	if (ret != -EOPNOTSUPP && !ret)
487 		return ret;
488 
489 	/* try old implementation as fallback strategy if above fails */
490 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, IOCTL_REGISTER_SGI, sgi_num, reset);
491 }
492 
493 /**
494  * zynqmp_pm_get_api_version() - Get version number of PMU PM firmware
495  * @version:	Returned version value
496  *
497  * Return: Returns status, either success or error+reason
498  */
499 int zynqmp_pm_get_api_version(u32 *version)
500 {
501 	u32 ret_payload[PAYLOAD_ARG_CNT];
502 	int ret;
503 
504 	if (!version)
505 		return -EINVAL;
506 
507 	/* Check is PM API version already verified */
508 	if (pm_api_version > 0) {
509 		*version = pm_api_version;
510 		return 0;
511 	}
512 	ret = zynqmp_pm_invoke_fn(PM_GET_API_VERSION, ret_payload, 0);
513 	*version = ret_payload[1];
514 
515 	return ret;
516 }
517 EXPORT_SYMBOL_GPL(zynqmp_pm_get_api_version);
518 
519 /**
520  * zynqmp_pm_get_chipid - Get silicon ID registers
521  * @idcode:     IDCODE register
522  * @version:    version register
523  *
524  * Return:      Returns the status of the operation and the idcode and version
525  *              registers in @idcode and @version.
526  */
527 int zynqmp_pm_get_chipid(u32 *idcode, u32 *version)
528 {
529 	u32 ret_payload[PAYLOAD_ARG_CNT];
530 	int ret;
531 
532 	if (!idcode || !version)
533 		return -EINVAL;
534 
535 	ret = zynqmp_pm_invoke_fn(PM_GET_CHIPID, ret_payload, 0);
536 	*idcode = ret_payload[1];
537 	*version = ret_payload[2];
538 
539 	return ret;
540 }
541 EXPORT_SYMBOL_GPL(zynqmp_pm_get_chipid);
542 
543 /**
544  * zynqmp_pm_get_family_info() - Get family info of platform
545  * @family:	Returned family code value
546  *
547  * Return: Returns status, either success or error+reason
548  */
549 int zynqmp_pm_get_family_info(u32 *family)
550 {
551 	if (!active_platform_fw_data)
552 		return -ENODEV;
553 
554 	if (!family)
555 		return -EINVAL;
556 
557 	*family = active_platform_fw_data->family_code;
558 
559 	return 0;
560 }
561 EXPORT_SYMBOL_GPL(zynqmp_pm_get_family_info);
562 
563 /**
564  * zynqmp_pm_get_sip_svc_version() - Get SiP service call version
565  * @version:	Returned version value
566  *
567  * Return: Returns status, either success or error+reason
568  */
569 static int zynqmp_pm_get_sip_svc_version(u32 *version)
570 {
571 	struct arm_smccc_res res;
572 	u64 args[SMC_ARG_CNT_64] = {0};
573 
574 	if (!version)
575 		return -EINVAL;
576 
577 	/* Check if SiP SVC version already verified */
578 	if (sip_svc_version > 0) {
579 		*version = sip_svc_version;
580 		return 0;
581 	}
582 
583 	args[0] = GET_SIP_SVC_VERSION;
584 
585 	arm_smccc_smc(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &res);
586 
587 	*version = ((lower_32_bits(res.a0) << 16U) | lower_32_bits(res.a1));
588 
589 	return zynqmp_pm_ret_code(XST_PM_SUCCESS);
590 }
591 
592 /**
593  * zynqmp_pm_get_trustzone_version() - Get secure trustzone firmware version
594  * @version:	Returned version value
595  *
596  * Return: Returns status, either success or error+reason
597  */
598 static int zynqmp_pm_get_trustzone_version(u32 *version)
599 {
600 	u32 ret_payload[PAYLOAD_ARG_CNT];
601 	int ret;
602 
603 	if (!version)
604 		return -EINVAL;
605 
606 	/* Check is PM trustzone version already verified */
607 	if (pm_tz_version > 0) {
608 		*version = pm_tz_version;
609 		return 0;
610 	}
611 	ret = zynqmp_pm_invoke_fn(PM_GET_TRUSTZONE_VERSION, ret_payload, 0);
612 	*version = ret_payload[1];
613 
614 	return ret;
615 }
616 
617 /**
618  * get_set_conduit_method() - Choose SMC or HVC based communication
619  * @np:		Pointer to the device_node structure
620  *
621  * Use SMC or HVC-based functions to communicate with EL2/EL3.
622  *
623  * Return: Returns 0 on success or error code
624  */
625 static int get_set_conduit_method(struct device_node *np)
626 {
627 	const char *method;
628 
629 	if (of_property_read_string(np, "method", &method)) {
630 		pr_warn("%s missing \"method\" property\n", __func__);
631 		return -ENXIO;
632 	}
633 
634 	if (!strcmp("hvc", method)) {
635 		do_fw_call = do_fw_call_hvc;
636 	} else if (!strcmp("smc", method)) {
637 		do_fw_call = do_fw_call_smc;
638 	} else {
639 		pr_warn("%s Invalid \"method\" property: %s\n",
640 			__func__, method);
641 		return -EINVAL;
642 	}
643 
644 	return 0;
645 }
646 
647 /**
648  * zynqmp_pm_query_data() - Get query data from firmware
649  * @qdata:	Variable to the zynqmp_pm_query_data structure
650  * @out:	Returned output value
651  *
652  * Return: Returns status, either success or error+reason
653  */
654 int zynqmp_pm_query_data(struct zynqmp_pm_query_data qdata, u32 *out)
655 {
656 	int ret, i = 0;
657 	u32 ret_payload[PAYLOAD_ARG_CNT] = {0};
658 
659 	if (sip_svc_version >= SIP_SVC_PASSTHROUGH_VERSION) {
660 		ret = zynqmp_pm_invoke_fw_fn(PM_QUERY_DATA, ret_payload, 4,
661 					     qdata.qid, qdata.arg1,
662 					     qdata.arg2, qdata.arg3);
663 		/* To support backward compatibility */
664 		if (!ret && !ret_payload[0]) {
665 			/*
666 			 * TF-A passes return status on 0th index but
667 			 * api to get clock name reads data from 0th
668 			 * index so pass data at 0th index instead of
669 			 * return status
670 			 */
671 			if (qdata.qid == PM_QID_CLOCK_GET_NAME ||
672 			    qdata.qid == PM_QID_PINCTRL_GET_FUNCTION_NAME)
673 				i = 1;
674 
675 			for (; i < PAYLOAD_ARG_CNT; i++, out++)
676 				*out = ret_payload[i];
677 
678 			return ret;
679 		}
680 	}
681 
682 	ret = zynqmp_pm_invoke_fn(PM_QUERY_DATA, out, 4, qdata.qid,
683 				  qdata.arg1, qdata.arg2, qdata.arg3);
684 
685 	/*
686 	 * For clock name query, all bytes in SMC response are clock name
687 	 * characters and return code is always success. For invalid clocks,
688 	 * clock name bytes would be zeros.
689 	 */
690 	return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : ret;
691 }
692 EXPORT_SYMBOL_GPL(zynqmp_pm_query_data);
693 
694 /**
695  * zynqmp_pm_clock_enable() - Enable the clock for given id
696  * @clock_id:	ID of the clock to be enabled
697  *
698  * This function is used by master to enable the clock
699  * including peripherals and PLL clocks.
700  *
701  * Return: Returns status, either success or error+reason
702  */
703 int zynqmp_pm_clock_enable(u32 clock_id)
704 {
705 	return zynqmp_pm_invoke_fn(PM_CLOCK_ENABLE, NULL, 1, clock_id);
706 }
707 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_enable);
708 
709 /**
710  * zynqmp_pm_clock_disable() - Disable the clock for given id
711  * @clock_id:	ID of the clock to be disable
712  *
713  * This function is used by master to disable the clock
714  * including peripherals and PLL clocks.
715  *
716  * Return: Returns status, either success or error+reason
717  */
718 int zynqmp_pm_clock_disable(u32 clock_id)
719 {
720 	return zynqmp_pm_invoke_fn(PM_CLOCK_DISABLE, NULL, 1, clock_id);
721 }
722 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_disable);
723 
724 /**
725  * zynqmp_pm_clock_getstate() - Get the clock state for given id
726  * @clock_id:	ID of the clock to be queried
727  * @state:	1/0 (Enabled/Disabled)
728  *
729  * This function is used by master to get the state of clock
730  * including peripherals and PLL clocks.
731  *
732  * Return: Returns status, either success or error+reason
733  */
734 int zynqmp_pm_clock_getstate(u32 clock_id, u32 *state)
735 {
736 	u32 ret_payload[PAYLOAD_ARG_CNT];
737 	int ret;
738 
739 	ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETSTATE, ret_payload, 1, clock_id);
740 	*state = ret_payload[1];
741 
742 	return ret;
743 }
744 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getstate);
745 
746 /**
747  * zynqmp_pm_clock_setdivider() - Set the clock divider for given id
748  * @clock_id:	ID of the clock
749  * @divider:	divider value
750  *
751  * This function is used by master to set divider for any clock
752  * to achieve desired rate.
753  *
754  * Return: Returns status, either success or error+reason
755  */
756 int zynqmp_pm_clock_setdivider(u32 clock_id, u32 divider)
757 {
758 	return zynqmp_pm_invoke_fn(PM_CLOCK_SETDIVIDER, NULL, 2, clock_id, divider);
759 }
760 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setdivider);
761 
762 /**
763  * zynqmp_pm_clock_getdivider() - Get the clock divider for given id
764  * @clock_id:	ID of the clock
765  * @divider:	divider value
766  *
767  * This function is used by master to get divider values
768  * for any clock.
769  *
770  * Return: Returns status, either success or error+reason
771  */
772 int zynqmp_pm_clock_getdivider(u32 clock_id, u32 *divider)
773 {
774 	u32 ret_payload[PAYLOAD_ARG_CNT];
775 	int ret;
776 
777 	ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETDIVIDER, ret_payload, 1, clock_id);
778 	*divider = ret_payload[1];
779 
780 	return ret;
781 }
782 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getdivider);
783 
784 /**
785  * zynqmp_pm_clock_setparent() - Set the clock parent for given id
786  * @clock_id:	ID of the clock
787  * @parent_id:	parent id
788  *
789  * This function is used by master to set parent for any clock.
790  *
791  * Return: Returns status, either success or error+reason
792  */
793 int zynqmp_pm_clock_setparent(u32 clock_id, u32 parent_id)
794 {
795 	return zynqmp_pm_invoke_fn(PM_CLOCK_SETPARENT, NULL, 2, clock_id, parent_id);
796 }
797 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setparent);
798 
799 /**
800  * zynqmp_pm_clock_getparent() - Get the clock parent for given id
801  * @clock_id:	ID of the clock
802  * @parent_id:	parent id
803  *
804  * This function is used by master to get parent index
805  * for any clock.
806  *
807  * Return: Returns status, either success or error+reason
808  */
809 int zynqmp_pm_clock_getparent(u32 clock_id, u32 *parent_id)
810 {
811 	u32 ret_payload[PAYLOAD_ARG_CNT];
812 	int ret;
813 
814 	ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETPARENT, ret_payload, 1, clock_id);
815 	*parent_id = ret_payload[1];
816 
817 	return ret;
818 }
819 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getparent);
820 
821 /**
822  * zynqmp_pm_set_pll_frac_mode() - PM API for set PLL mode
823  *
824  * @clk_id:	PLL clock ID
825  * @mode:	PLL mode (PLL_MODE_FRAC/PLL_MODE_INT)
826  *
827  * This function sets PLL mode
828  *
829  * Return: Returns status, either success or error+reason
830  */
831 int zynqmp_pm_set_pll_frac_mode(u32 clk_id, u32 mode)
832 {
833 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_SET_PLL_FRAC_MODE, clk_id, mode);
834 }
835 EXPORT_SYMBOL_GPL(zynqmp_pm_set_pll_frac_mode);
836 
837 /**
838  * zynqmp_pm_get_pll_frac_mode() - PM API for get PLL mode
839  *
840  * @clk_id:	PLL clock ID
841  * @mode:	PLL mode
842  *
843  * This function return current PLL mode
844  *
845  * Return: Returns status, either success or error+reason
846  */
847 int zynqmp_pm_get_pll_frac_mode(u32 clk_id, u32 *mode)
848 {
849 	return zynqmp_pm_invoke_fn(PM_IOCTL, mode, 3, 0, IOCTL_GET_PLL_FRAC_MODE, clk_id);
850 }
851 EXPORT_SYMBOL_GPL(zynqmp_pm_get_pll_frac_mode);
852 
853 /**
854  * zynqmp_pm_set_pll_frac_data() - PM API for setting pll fraction data
855  *
856  * @clk_id:	PLL clock ID
857  * @data:	fraction data
858  *
859  * This function sets fraction data.
860  * It is valid for fraction mode only.
861  *
862  * Return: Returns status, either success or error+reason
863  */
864 int zynqmp_pm_set_pll_frac_data(u32 clk_id, u32 data)
865 {
866 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_SET_PLL_FRAC_DATA, clk_id, data);
867 }
868 EXPORT_SYMBOL_GPL(zynqmp_pm_set_pll_frac_data);
869 
870 /**
871  * zynqmp_pm_get_pll_frac_data() - PM API for getting pll fraction data
872  *
873  * @clk_id:	PLL clock ID
874  * @data:	fraction data
875  *
876  * This function returns fraction data value.
877  *
878  * Return: Returns status, either success or error+reason
879  */
880 int zynqmp_pm_get_pll_frac_data(u32 clk_id, u32 *data)
881 {
882 	return zynqmp_pm_invoke_fn(PM_IOCTL, data, 3, 0, IOCTL_GET_PLL_FRAC_DATA, clk_id);
883 }
884 EXPORT_SYMBOL_GPL(zynqmp_pm_get_pll_frac_data);
885 
886 /**
887  * zynqmp_pm_set_sd_tapdelay() -  Set tap delay for the SD device
888  *
889  * @node_id:	Node ID of the device
890  * @type:	Type of tap delay to set (input/output)
891  * @value:	Value to set fot the tap delay
892  *
893  * This function sets input/output tap delay for the SD device.
894  *
895  * Return:	Returns status, either success or error+reason
896  */
897 int zynqmp_pm_set_sd_tapdelay(u32 node_id, u32 type, u32 value)
898 {
899 	u32 reg = (type == PM_TAPDELAY_INPUT) ? SD_ITAPDLY : SD_OTAPDLYSEL;
900 	u32 mask = (node_id == NODE_SD_0) ? GENMASK(15, 0) : GENMASK(31, 16);
901 
902 	if (value) {
903 		return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, node_id, IOCTL_SET_SD_TAPDELAY, type,
904 					   value);
905 	}
906 
907 	/*
908 	 * Work around completely misdesigned firmware API on Xilinx ZynqMP.
909 	 * The IOCTL_SET_SD_TAPDELAY firmware call allows the caller to only
910 	 * ever set IOU_SLCR SD_ITAPDLY Register SD0_ITAPDLYENA/SD1_ITAPDLYENA
911 	 * bits, but there is no matching call to clear those bits. If those
912 	 * bits are not cleared, SDMMC tuning may fail.
913 	 *
914 	 * Luckily, there are PM_MMIO_READ/PM_MMIO_WRITE calls which seem to
915 	 * allow complete unrestricted access to all address space, including
916 	 * IOU_SLCR SD_ITAPDLY Register and all the other registers, access
917 	 * to which was supposed to be protected by the current firmware API.
918 	 *
919 	 * Use PM_MMIO_READ/PM_MMIO_WRITE to re-implement the missing counter
920 	 * part of IOCTL_SET_SD_TAPDELAY which clears SDx_ITAPDLYENA bits.
921 	 */
922 	return zynqmp_pm_invoke_fn(PM_MMIO_WRITE, NULL, 2, reg, mask);
923 }
924 EXPORT_SYMBOL_GPL(zynqmp_pm_set_sd_tapdelay);
925 
926 /**
927  * zynqmp_pm_sd_dll_reset() - Reset DLL logic
928  *
929  * @node_id:	Node ID of the device
930  * @type:	Reset type
931  *
932  * This function resets DLL logic for the SD device.
933  *
934  * Return:	Returns status, either success or error+reason
935  */
936 int zynqmp_pm_sd_dll_reset(u32 node_id, u32 type)
937 {
938 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, node_id, IOCTL_SD_DLL_RESET, type);
939 }
940 EXPORT_SYMBOL_GPL(zynqmp_pm_sd_dll_reset);
941 
942 /**
943  * zynqmp_pm_ospi_mux_select() - OSPI Mux selection
944  *
945  * @dev_id:	Device Id of the OSPI device.
946  * @select:	OSPI Mux select value.
947  *
948  * This function select the OSPI Mux.
949  *
950  * Return:	Returns status, either success or error+reason
951  */
952 int zynqmp_pm_ospi_mux_select(u32 dev_id, u32 select)
953 {
954 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, dev_id, IOCTL_OSPI_MUX_SELECT, select);
955 }
956 EXPORT_SYMBOL_GPL(zynqmp_pm_ospi_mux_select);
957 
958 /**
959  * zynqmp_pm_write_ggs() - PM API for writing global general storage (ggs)
960  * @index:	GGS register index
961  * @value:	Register value to be written
962  *
963  * This function writes value to GGS register.
964  *
965  * Return:      Returns status, either success or error+reason
966  */
967 int zynqmp_pm_write_ggs(u32 index, u32 value)
968 {
969 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_WRITE_GGS, index, value);
970 }
971 EXPORT_SYMBOL_GPL(zynqmp_pm_write_ggs);
972 
973 /**
974  * zynqmp_pm_read_ggs() - PM API for reading global general storage (ggs)
975  * @index:	GGS register index
976  * @value:	Register value to be written
977  *
978  * This function returns GGS register value.
979  *
980  * Return:	Returns status, either success or error+reason
981  */
982 int zynqmp_pm_read_ggs(u32 index, u32 *value)
983 {
984 	return zynqmp_pm_invoke_fn(PM_IOCTL, value, 3, 0, IOCTL_READ_GGS, index);
985 }
986 EXPORT_SYMBOL_GPL(zynqmp_pm_read_ggs);
987 
988 /**
989  * zynqmp_pm_write_pggs() - PM API for writing persistent global general
990  *			     storage (pggs)
991  * @index:	PGGS register index
992  * @value:	Register value to be written
993  *
994  * This function writes value to PGGS register.
995  *
996  * Return:	Returns status, either success or error+reason
997  */
998 int zynqmp_pm_write_pggs(u32 index, u32 value)
999 {
1000 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_WRITE_PGGS, index, value);
1001 }
1002 EXPORT_SYMBOL_GPL(zynqmp_pm_write_pggs);
1003 
1004 /**
1005  * zynqmp_pm_read_pggs() - PM API for reading persistent global general
1006  *			     storage (pggs)
1007  * @index:	PGGS register index
1008  * @value:	Register value to be written
1009  *
1010  * This function returns PGGS register value.
1011  *
1012  * Return:	Returns status, either success or error+reason
1013  */
1014 int zynqmp_pm_read_pggs(u32 index, u32 *value)
1015 {
1016 	return zynqmp_pm_invoke_fn(PM_IOCTL, value, 3, 0, IOCTL_READ_PGGS, index);
1017 }
1018 EXPORT_SYMBOL_GPL(zynqmp_pm_read_pggs);
1019 
1020 int zynqmp_pm_set_tapdelay_bypass(u32 index, u32 value)
1021 {
1022 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_SET_TAPDELAY_BYPASS, index, value);
1023 }
1024 EXPORT_SYMBOL_GPL(zynqmp_pm_set_tapdelay_bypass);
1025 
1026 /**
1027  * zynqmp_pm_set_boot_health_status() - PM API for setting healthy boot status
1028  * @value:	Status value to be written
1029  *
1030  * This function sets healthy bit value to indicate boot health status
1031  * to firmware.
1032  *
1033  * Return:	Returns status, either success or error+reason
1034  */
1035 int zynqmp_pm_set_boot_health_status(u32 value)
1036 {
1037 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, 0, IOCTL_SET_BOOT_HEALTH_STATUS, value);
1038 }
1039 
1040 /**
1041  * zynqmp_pm_reset_assert - Request setting of reset (1 - assert, 0 - release)
1042  * @reset:		Reset to be configured
1043  * @assert_flag:	Flag stating should reset be asserted (1) or
1044  *			released (0)
1045  *
1046  * Return: Returns status, either success or error+reason
1047  */
1048 int zynqmp_pm_reset_assert(const u32 reset,
1049 			   const enum zynqmp_pm_reset_action assert_flag)
1050 {
1051 	return zynqmp_pm_invoke_fn(PM_RESET_ASSERT, NULL, 2, reset, assert_flag);
1052 }
1053 EXPORT_SYMBOL_GPL(zynqmp_pm_reset_assert);
1054 
1055 /**
1056  * zynqmp_pm_reset_get_status - Get status of the reset
1057  * @reset:      Reset whose status should be returned
1058  * @status:     Returned status
1059  *
1060  * Return: Returns status, either success or error+reason
1061  */
1062 int zynqmp_pm_reset_get_status(const u32 reset, u32 *status)
1063 {
1064 	u32 ret_payload[PAYLOAD_ARG_CNT];
1065 	int ret;
1066 
1067 	if (!status)
1068 		return -EINVAL;
1069 
1070 	ret = zynqmp_pm_invoke_fn(PM_RESET_GET_STATUS, ret_payload, 1, reset);
1071 	*status = ret_payload[1];
1072 
1073 	return ret;
1074 }
1075 EXPORT_SYMBOL_GPL(zynqmp_pm_reset_get_status);
1076 
1077 /**
1078  * zynqmp_pm_fpga_load - Perform the fpga load
1079  * @address:	Address to write to
1080  * @size:	pl bitstream size
1081  * @flags:	Bitstream type
1082  *	-XILINX_ZYNQMP_PM_FPGA_FULL:  FPGA full reconfiguration
1083  *	-XILINX_ZYNQMP_PM_FPGA_PARTIAL: FPGA partial reconfiguration
1084  *
1085  * This function provides access to pmufw. To transfer
1086  * the required bitstream into PL.
1087  *
1088  * Return: Returns status, either success or error+reason
1089  */
1090 int zynqmp_pm_fpga_load(const u64 address, const u32 size, const u32 flags)
1091 {
1092 	u32 ret_payload[PAYLOAD_ARG_CNT];
1093 	int ret;
1094 
1095 	ret = zynqmp_pm_invoke_fn(PM_FPGA_LOAD, ret_payload, 4, lower_32_bits(address),
1096 				  upper_32_bits(address), size, flags);
1097 	if (ret_payload[0])
1098 		return -ret_payload[0];
1099 
1100 	return ret;
1101 }
1102 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_load);
1103 
1104 /**
1105  * zynqmp_pm_fpga_get_status - Read value from PCAP status register
1106  * @value: Value to read
1107  *
1108  * This function provides access to the pmufw to get the PCAP
1109  * status
1110  *
1111  * Return: Returns status, either success or error+reason
1112  */
1113 int zynqmp_pm_fpga_get_status(u32 *value)
1114 {
1115 	u32 ret_payload[PAYLOAD_ARG_CNT];
1116 	int ret;
1117 
1118 	if (!value)
1119 		return -EINVAL;
1120 
1121 	ret = zynqmp_pm_invoke_fn(PM_FPGA_GET_STATUS, ret_payload, 0);
1122 	*value = ret_payload[1];
1123 
1124 	return ret;
1125 }
1126 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_get_status);
1127 
1128 /**
1129  * zynqmp_pm_fpga_get_config_status - Get the FPGA configuration status.
1130  * @value: Buffer to store FPGA configuration status.
1131  *
1132  * This function provides access to the pmufw to get the FPGA configuration
1133  * status
1134  *
1135  * Return: 0 on success, a negative value on error
1136  */
1137 int zynqmp_pm_fpga_get_config_status(u32 *value)
1138 {
1139 	u32 ret_payload[PAYLOAD_ARG_CNT];
1140 	int ret;
1141 
1142 	if (!value)
1143 		return -EINVAL;
1144 
1145 	ret = zynqmp_pm_invoke_fn(PM_FPGA_READ, ret_payload, 4,
1146 				  XILINX_ZYNQMP_PM_FPGA_CONFIG_STAT_OFFSET, 0, 0,
1147 				  XILINX_ZYNQMP_PM_FPGA_READ_CONFIG_REG);
1148 
1149 	*value = ret_payload[1];
1150 
1151 	return ret;
1152 }
1153 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_get_config_status);
1154 
1155 /**
1156  * zynqmp_pm_pinctrl_request - Request Pin from firmware
1157  * @pin: Pin number to request
1158  *
1159  * This function requests pin from firmware.
1160  *
1161  * Return: Returns status, either success or error+reason.
1162  */
1163 int zynqmp_pm_pinctrl_request(const u32 pin)
1164 {
1165 	return zynqmp_pm_invoke_fn(PM_PINCTRL_REQUEST, NULL, 1, pin);
1166 }
1167 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_request);
1168 
1169 /**
1170  * zynqmp_pm_pinctrl_release - Inform firmware that Pin control is released
1171  * @pin: Pin number to release
1172  *
1173  * This function release pin from firmware.
1174  *
1175  * Return: Returns status, either success or error+reason.
1176  */
1177 int zynqmp_pm_pinctrl_release(const u32 pin)
1178 {
1179 	return zynqmp_pm_invoke_fn(PM_PINCTRL_RELEASE, NULL, 1, pin);
1180 }
1181 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_release);
1182 
1183 /**
1184  * zynqmp_pm_pinctrl_set_function - Set requested function for the pin
1185  * @pin: Pin number
1186  * @id: Function ID to set
1187  *
1188  * This function sets requested function for the given pin.
1189  *
1190  * Return: Returns status, either success or error+reason.
1191  */
1192 int zynqmp_pm_pinctrl_set_function(const u32 pin, const u32 id)
1193 {
1194 	return zynqmp_pm_invoke_fn(PM_PINCTRL_SET_FUNCTION, NULL, 2, pin, id);
1195 }
1196 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_set_function);
1197 
1198 /**
1199  * zynqmp_pm_pinctrl_get_config - Get configuration parameter for the pin
1200  * @pin: Pin number
1201  * @param: Parameter to get
1202  * @value: Buffer to store parameter value
1203  *
1204  * This function gets requested configuration parameter for the given pin.
1205  *
1206  * Return: Returns status, either success or error+reason.
1207  */
1208 int zynqmp_pm_pinctrl_get_config(const u32 pin, const u32 param,
1209 				 u32 *value)
1210 {
1211 	u32 ret_payload[PAYLOAD_ARG_CNT];
1212 	int ret;
1213 
1214 	if (!value)
1215 		return -EINVAL;
1216 
1217 	ret = zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_GET, ret_payload, 2, pin, param);
1218 	*value = ret_payload[1];
1219 
1220 	return ret;
1221 }
1222 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_get_config);
1223 
1224 /**
1225  * zynqmp_pm_pinctrl_set_config - Set configuration parameter for the pin
1226  * @pin: Pin number
1227  * @param: Parameter to set
1228  * @value: Parameter value to set
1229  *
1230  * This function sets requested configuration parameter for the given pin.
1231  *
1232  * Return: Returns status, either success or error+reason.
1233  */
1234 int zynqmp_pm_pinctrl_set_config(const u32 pin, const u32 param,
1235 				 u32 value)
1236 {
1237 	int ret;
1238 	u32 pm_family_code;
1239 
1240 	ret = zynqmp_pm_get_family_info(&pm_family_code);
1241 	if (ret)
1242 		return ret;
1243 
1244 	if (pm_family_code == PM_ZYNQMP_FAMILY_CODE &&
1245 	    param == PM_PINCTRL_CONFIG_TRI_STATE) {
1246 		ret = zynqmp_pm_feature(PM_PINCTRL_CONFIG_PARAM_SET);
1247 		if (ret < PM_PINCTRL_PARAM_SET_VERSION) {
1248 			pr_warn("The requested pinctrl feature is not supported in the current firmware.\n"
1249 				"Expected firmware version is 2023.1 and above for this feature to work.\r\n");
1250 			return -EOPNOTSUPP;
1251 		}
1252 	}
1253 
1254 	return zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_SET, NULL, 3, pin, param, value);
1255 }
1256 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_set_config);
1257 
1258 /**
1259  * zynqmp_pm_bootmode_read() - PM Config API for read bootpin status
1260  * @ps_mode: Returned output value of ps_mode
1261  *
1262  * This API function is to be used for notify the power management controller
1263  * to read bootpin status.
1264  *
1265  * Return: status, either success or error+reason
1266  */
1267 unsigned int zynqmp_pm_bootmode_read(u32 *ps_mode)
1268 {
1269 	unsigned int ret;
1270 	u32 ret_payload[PAYLOAD_ARG_CNT];
1271 
1272 	ret = zynqmp_pm_invoke_fn(PM_MMIO_READ, ret_payload, 1, CRL_APB_BOOT_PIN_CTRL);
1273 
1274 	*ps_mode = ret_payload[1];
1275 
1276 	return ret;
1277 }
1278 EXPORT_SYMBOL_GPL(zynqmp_pm_bootmode_read);
1279 
1280 /**
1281  * zynqmp_pm_bootmode_write() - PM Config API for Configure bootpin
1282  * @ps_mode: Value to be written to the bootpin ctrl register
1283  *
1284  * This API function is to be used for notify the power management controller
1285  * to configure bootpin.
1286  *
1287  * Return: Returns status, either success or error+reason
1288  */
1289 int zynqmp_pm_bootmode_write(u32 ps_mode)
1290 {
1291 	return zynqmp_pm_invoke_fn(PM_MMIO_WRITE, NULL, 3, CRL_APB_BOOT_PIN_CTRL,
1292 				   CRL_APB_BOOTPIN_CTRL_MASK, ps_mode);
1293 }
1294 EXPORT_SYMBOL_GPL(zynqmp_pm_bootmode_write);
1295 
1296 /**
1297  * zynqmp_pm_init_finalize() - PM call to inform firmware that the caller
1298  *			       master has initialized its own power management
1299  *
1300  * Return: Returns status, either success or error+reason
1301  *
1302  * This API function is to be used for notify the power management controller
1303  * about the completed power management initialization.
1304  */
1305 static int zynqmp_pm_init_finalize(void)
1306 {
1307 	return zynqmp_pm_invoke_fn(PM_PM_INIT_FINALIZE, NULL, 0);
1308 }
1309 
1310 /**
1311  * zynqmp_pm_set_suspend_mode()	- Set system suspend mode
1312  * @mode:	Mode to set for system suspend
1313  *
1314  * This API function is used to set mode of system suspend.
1315  *
1316  * Return: Returns status, either success or error+reason
1317  */
1318 int zynqmp_pm_set_suspend_mode(u32 mode)
1319 {
1320 	return zynqmp_pm_invoke_fn(PM_SET_SUSPEND_MODE, NULL, 1, mode);
1321 }
1322 EXPORT_SYMBOL_GPL(zynqmp_pm_set_suspend_mode);
1323 
1324 /**
1325  * zynqmp_pm_request_node() - Request a node with specific capabilities
1326  * @node:		Node ID of the slave
1327  * @capabilities:	Requested capabilities of the slave
1328  * @qos:		Quality of service (not supported)
1329  * @ack:		Flag to specify whether acknowledge is requested
1330  *
1331  * This function is used by master to request particular node from firmware.
1332  * Every master must request node before using it.
1333  *
1334  * Return: Returns status, either success or error+reason
1335  */
1336 int zynqmp_pm_request_node(const u32 node, const u32 capabilities,
1337 			   const u32 qos, const enum zynqmp_pm_request_ack ack)
1338 {
1339 	return zynqmp_pm_invoke_fn(PM_REQUEST_NODE, NULL, 4, node, capabilities, qos, ack);
1340 }
1341 EXPORT_SYMBOL_GPL(zynqmp_pm_request_node);
1342 
1343 /**
1344  * zynqmp_pm_release_node() - Release a node
1345  * @node:	Node ID of the slave
1346  *
1347  * This function is used by master to inform firmware that master
1348  * has released node. Once released, master must not use that node
1349  * without re-request.
1350  *
1351  * Return: Returns status, either success or error+reason
1352  */
1353 int zynqmp_pm_release_node(const u32 node)
1354 {
1355 	return zynqmp_pm_invoke_fn(PM_RELEASE_NODE, NULL, 1, node);
1356 }
1357 EXPORT_SYMBOL_GPL(zynqmp_pm_release_node);
1358 
1359 /**
1360  * zynqmp_pm_get_rpu_mode() - Get RPU mode
1361  * @node_id:	Node ID of the device
1362  * @rpu_mode:	return by reference value
1363  *		either split or lockstep
1364  *
1365  * Return:	return 0 on success or error+reason.
1366  *		if success, then  rpu_mode will be set
1367  *		to current rpu mode.
1368  */
1369 int zynqmp_pm_get_rpu_mode(u32 node_id, enum rpu_oper_mode *rpu_mode)
1370 {
1371 	u32 ret_payload[PAYLOAD_ARG_CNT];
1372 	int ret;
1373 
1374 	ret = zynqmp_pm_invoke_fn(PM_IOCTL, ret_payload, 2, node_id, IOCTL_GET_RPU_OPER_MODE);
1375 
1376 	/* only set rpu_mode if no error */
1377 	if (ret == XST_PM_SUCCESS)
1378 		*rpu_mode = ret_payload[0];
1379 
1380 	return ret;
1381 }
1382 EXPORT_SYMBOL_GPL(zynqmp_pm_get_rpu_mode);
1383 
1384 /**
1385  * zynqmp_pm_set_rpu_mode() - Set RPU mode
1386  * @node_id:	Node ID of the device
1387  * @rpu_mode:	Argument 1 to requested IOCTL call. either split or lockstep
1388  *
1389  *		This function is used to set RPU mode to split or
1390  *		lockstep
1391  *
1392  * Return:	Returns status, either success or error+reason
1393  */
1394 int zynqmp_pm_set_rpu_mode(u32 node_id, enum rpu_oper_mode rpu_mode)
1395 {
1396 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, node_id, IOCTL_SET_RPU_OPER_MODE,
1397 				   (u32)rpu_mode);
1398 }
1399 EXPORT_SYMBOL_GPL(zynqmp_pm_set_rpu_mode);
1400 
1401 /**
1402  * zynqmp_pm_set_tcm_config - configure TCM
1403  * @node_id:	Firmware specific TCM subsystem ID
1404  * @tcm_mode:	Argument 1 to requested IOCTL call
1405  *              either PM_RPU_TCM_COMB or PM_RPU_TCM_SPLIT
1406  *
1407  * This function is used to set RPU mode to split or combined
1408  *
1409  * Return: status: 0 for success, else failure
1410  */
1411 int zynqmp_pm_set_tcm_config(u32 node_id, enum rpu_tcm_comb tcm_mode)
1412 {
1413 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, node_id, IOCTL_TCM_COMB_CONFIG,
1414 				   (u32)tcm_mode);
1415 }
1416 EXPORT_SYMBOL_GPL(zynqmp_pm_set_tcm_config);
1417 
1418 /**
1419  * zynqmp_pm_get_node_status - PM call to request a node's current power state
1420  * @node:		ID of the component or sub-system in question
1421  * @status:		Current operating state of the requested node
1422  * @requirements:	Current requirements asserted on the node,
1423  *			used for slave nodes only.
1424  * @usage:		Usage information, used for slave nodes only:
1425  *			PM_USAGE_NO_MASTER	- No master is currently using
1426  *						  the node
1427  *			PM_USAGE_CURRENT_MASTER	- Only requesting master is
1428  *						  currently using the node
1429  *			PM_USAGE_OTHER_MASTER	- Only other masters are
1430  *						  currently using the node
1431  *			PM_USAGE_BOTH_MASTERS	- Both the current and at least
1432  *						  one other master is currently
1433  *						  using the node
1434  *
1435  * Return:		Returns status, either success or error+reason
1436  */
1437 int zynqmp_pm_get_node_status(const u32 node, u32 *const status,
1438 			      u32 *const requirements, u32 *const usage)
1439 {
1440 	u32 ret_payload[PAYLOAD_ARG_CNT];
1441 	int ret;
1442 
1443 	if (!status || !requirements || !usage)
1444 		return -EINVAL;
1445 
1446 	ret = zynqmp_pm_invoke_fn(PM_GET_NODE_STATUS, ret_payload, 1, node);
1447 	if (ret_payload[0] == XST_PM_SUCCESS) {
1448 		*status = ret_payload[1];
1449 		*requirements = ret_payload[2];
1450 		*usage = ret_payload[3];
1451 	}
1452 
1453 	return ret;
1454 }
1455 EXPORT_SYMBOL_GPL(zynqmp_pm_get_node_status);
1456 
1457 /**
1458  * zynqmp_pm_get_rpu_node_status - PM call to request a RPU node's current power state
1459  * @node:		ID of the RPU component or sub-system in question
1460  * @status:		Current operating state of the requested RPU node.
1461  * @requirements:	Current requirements asserted on the RPU node.
1462  * @usage:		Usage information, used for RPU slave nodes only:
1463  *			PM_USAGE_NO_MASTER	- No master is currently using
1464  *						  the node
1465  *			PM_USAGE_CURRENT_MASTER	- Only requesting master is
1466  *						  currently using the node
1467  *			PM_USAGE_OTHER_MASTER	- Only other masters are
1468  *						  currently using the node
1469  *			PM_USAGE_BOTH_MASTERS	- Both the current and at least
1470  *						  one other master is currently
1471  *						  using the node
1472  *
1473  * Return:		Returns status, either success or error+reason
1474  */
1475 int zynqmp_pm_get_rpu_node_status(const u32 node, u32 *const status,
1476 				  u32 *const requirements, u32 *const usage)
1477 {
1478 	if (zynqmp_pm_feature(PM_GET_NODE_STATUS) < PM_API_VERSION_2)
1479 		return -EOPNOTSUPP;
1480 
1481 	return zynqmp_pm_get_node_status(node, status, requirements, usage);
1482 }
1483 EXPORT_SYMBOL_GPL(zynqmp_pm_get_rpu_node_status);
1484 
1485 /**
1486  * zynqmp_pm_force_pwrdwn - PM call to request for another PU or subsystem to
1487  *             be powered down forcefully
1488  * @node:  Node ID of the targeted PU or subsystem
1489  * @ack:   Flag to specify whether acknowledge is requested
1490  *
1491  * Return: status, either success or error+reason
1492  */
1493 int zynqmp_pm_force_pwrdwn(const u32 node,
1494 			   const enum zynqmp_pm_request_ack ack)
1495 {
1496 	return zynqmp_pm_invoke_fn(PM_FORCE_POWERDOWN, NULL, 2, node, ack);
1497 }
1498 EXPORT_SYMBOL_GPL(zynqmp_pm_force_pwrdwn);
1499 
1500 /**
1501  * zynqmp_pm_request_wake - PM call to wake up selected master or subsystem
1502  * @node:  Node ID of the master or subsystem
1503  * @set_addr:  Specifies whether the address argument is relevant
1504  * @address:   Address from which to resume when woken up
1505  * @ack:   Flag to specify whether acknowledge requested
1506  *
1507  * Return: status, either success or error+reason
1508  */
1509 int zynqmp_pm_request_wake(const u32 node,
1510 			   const bool set_addr,
1511 			   const u64 address,
1512 			   const enum zynqmp_pm_request_ack ack)
1513 {
1514 	/* set_addr flag is encoded into 1st bit of address */
1515 	return zynqmp_pm_invoke_fn(PM_REQUEST_WAKEUP, NULL, 4, node, address | set_addr,
1516 				   address >> 32, ack);
1517 }
1518 EXPORT_SYMBOL_GPL(zynqmp_pm_request_wake);
1519 
1520 /**
1521  * zynqmp_pm_start_rpu - Boot Real-time Processing Unit (Cortex-R) on SoC
1522  *
1523  * @node: power-domains id of the core
1524  * @bootaddr: Boot address of elf
1525  *
1526  * Return: status, either success or error+reason
1527  */
1528 int zynqmp_pm_start_rpu(const u32 node, const u64 bootaddr)
1529 {
1530 	enum rpu_boot_mem bootmem;
1531 	int ret;
1532 
1533 	/*
1534 	 * The exception vector pointers (EVP) refer to the base-address of
1535 	 * exception vectors (for reset, IRQ, FIQ, etc). The reset-vector
1536 	 * starts at the base-address and subsequent vectors are on 4-byte
1537 	 * boundaries.
1538 	 *
1539 	 * Exception vectors can start either from 0x0000_0000 (LOVEC) or
1540 	 * from 0xFFFF_0000 (HIVEC) which is mapped in the OCM (On-Chip Memory)
1541 	 *
1542 	 * Usually firmware will put Exception vectors at LOVEC.
1543 	 *
1544 	 * It is not recommend that you change the exception vector.
1545 	 * Changing the EVP to HIVEC will result in increased interrupt latency
1546 	 * and jitter. Also, if the OCM is secured and the Cortex-R5F processor
1547 	 * is non-secured, then the Cortex-R5F processor cannot access the
1548 	 * HIVEC exception vectors in the OCM.
1549 	 */
1550 	bootmem = (bootaddr >= 0xFFFC0000) ?
1551 		   PM_RPU_BOOTMEM_HIVEC : PM_RPU_BOOTMEM_LOVEC;
1552 
1553 	pr_debug("RPU boot addr 0x%llx from %s.", bootaddr,
1554 		 bootmem == PM_RPU_BOOTMEM_HIVEC ? "OCM" : "TCM");
1555 
1556 	/* Request node before starting RPU core if new version of API is supported */
1557 	if (zynqmp_pm_feature(PM_REQUEST_NODE) > PM_API_VERSION_1) {
1558 		ret = zynqmp_pm_request_node(node,
1559 					     ZYNQMP_PM_CAPABILITY_ACCESS, 0,
1560 					     ZYNQMP_PM_REQUEST_ACK_BLOCKING);
1561 		if (ret < 0) {
1562 			pr_err("failed to request 0x%x", node);
1563 			return ret;
1564 		}
1565 	}
1566 
1567 	ret = zynqmp_pm_request_wake(node, true,
1568 				     bootmem, ZYNQMP_PM_REQUEST_ACK_NO);
1569 	if (ret)
1570 		pr_err("failed to start RPU = 0x%x\n", node);
1571 	return ret;
1572 }
1573 EXPORT_SYMBOL_GPL(zynqmp_pm_start_rpu);
1574 
1575 /**
1576  * zynqmp_pm_stop_rpu - Stop Real-time Processing Unit (Cortex-R) on SoC
1577  *
1578  * @node: power-domains id of the core
1579  *
1580  * Return: status, either success or error+reason
1581  */
1582 int zynqmp_pm_stop_rpu(const u32 node)
1583 {
1584 	int ret;
1585 
1586 	/* Use release node API to stop core if new version of API is supported */
1587 	if (zynqmp_pm_feature(PM_RELEASE_NODE) > PM_API_VERSION_1) {
1588 		ret = zynqmp_pm_release_node(node);
1589 		if (ret)
1590 			pr_err("failed to stop remoteproc RPU %d\n", ret);
1591 		return ret;
1592 	}
1593 
1594 	/*
1595 	 * Check expected version of EEMI call before calling it. This avoids
1596 	 * any error or warning prints from firmware as it is expected that fw
1597 	 * doesn't support it.
1598 	 */
1599 	if (zynqmp_pm_feature(PM_FORCE_POWERDOWN) != PM_API_VERSION_1) {
1600 		pr_debug("EEMI interface %d ver 1 not supported\n",
1601 			 PM_FORCE_POWERDOWN);
1602 		return -EOPNOTSUPP;
1603 	}
1604 
1605 	/* maintain force pwr down for backward compatibility */
1606 	ret = zynqmp_pm_force_pwrdwn(node, ZYNQMP_PM_REQUEST_ACK_BLOCKING);
1607 	if (ret)
1608 		pr_err("core force power down failed\n");
1609 	return ret;
1610 }
1611 EXPORT_SYMBOL_GPL(zynqmp_pm_stop_rpu);
1612 
1613 /**
1614  * zynqmp_pm_set_requirement() - PM call to set requirement for PM slaves
1615  * @node:		Node ID of the slave
1616  * @capabilities:	Requested capabilities of the slave
1617  * @qos:		Quality of service (not supported)
1618  * @ack:		Flag to specify whether acknowledge is requested
1619  *
1620  * This API function is to be used for slaves a PU already has requested
1621  * to change its capabilities.
1622  *
1623  * Return: Returns status, either success or error+reason
1624  */
1625 int zynqmp_pm_set_requirement(const u32 node, const u32 capabilities,
1626 			      const u32 qos,
1627 			      const enum zynqmp_pm_request_ack ack)
1628 {
1629 	return zynqmp_pm_invoke_fn(PM_SET_REQUIREMENT, NULL, 4, node, capabilities, qos, ack);
1630 }
1631 EXPORT_SYMBOL_GPL(zynqmp_pm_set_requirement);
1632 
1633 /**
1634  * zynqmp_pm_load_pdi - Load and process PDI
1635  * @src:	Source device where PDI is located
1636  * @address:	PDI src address
1637  *
1638  * This function provides support to load PDI from linux
1639  *
1640  * Return: Returns status, either success or error+reason
1641  */
1642 int zynqmp_pm_load_pdi(const u32 src, const u64 address)
1643 {
1644 	return zynqmp_pm_invoke_fn(PM_LOAD_PDI, NULL, 3, src, lower_32_bits(address),
1645 				   upper_32_bits(address));
1646 }
1647 EXPORT_SYMBOL_GPL(zynqmp_pm_load_pdi);
1648 
1649 /**
1650  * zynqmp_pm_efuse_access - Provides access to efuse memory.
1651  * @address:	Address of the efuse params structure
1652  * @out:		Returned output value
1653  *
1654  * Return:	Returns status, either success or error code.
1655  */
1656 int zynqmp_pm_efuse_access(const u64 address, u32 *out)
1657 {
1658 	u32 ret_payload[PAYLOAD_ARG_CNT];
1659 	int ret;
1660 
1661 	if (!out)
1662 		return -EINVAL;
1663 
1664 	ret = zynqmp_pm_invoke_fn(PM_EFUSE_ACCESS, ret_payload, 2,
1665 				  upper_32_bits(address),
1666 				  lower_32_bits(address));
1667 	*out = ret_payload[1];
1668 
1669 	return ret;
1670 }
1671 EXPORT_SYMBOL_GPL(zynqmp_pm_efuse_access);
1672 
1673 /**
1674  * zynqmp_pm_register_notifier() - PM API for register a subsystem
1675  *                                to be notified about specific
1676  *                                event/error.
1677  * @node:	Node ID to which the event is related.
1678  * @event:	Event Mask of Error events for which wants to get notified.
1679  * @wake:	Wake subsystem upon capturing the event if value 1
1680  * @enable:	Enable the registration for value 1, disable for value 0
1681  *
1682  * This function is used to register/un-register for particular node-event
1683  * combination in firmware.
1684  *
1685  * Return: Returns status, either success or error+reason
1686  */
1687 
1688 int zynqmp_pm_register_notifier(const u32 node, const u32 event,
1689 				const u32 wake, const u32 enable)
1690 {
1691 	return zynqmp_pm_invoke_fn(PM_REGISTER_NOTIFIER, NULL, 4, node, event, wake, enable);
1692 }
1693 EXPORT_SYMBOL_GPL(zynqmp_pm_register_notifier);
1694 
1695 /**
1696  * zynqmp_pm_system_shutdown - PM call to request a system shutdown or restart
1697  * @type:	Shutdown or restart? 0 for shutdown, 1 for restart
1698  * @subtype:	Specifies which system should be restarted or shut down
1699  *
1700  * Return:	Returns status, either success or error+reason
1701  */
1702 int zynqmp_pm_system_shutdown(const u32 type, const u32 subtype)
1703 {
1704 	return zynqmp_pm_invoke_fn(PM_SYSTEM_SHUTDOWN, NULL, 2, type, subtype);
1705 }
1706 
1707 /**
1708  * zynqmp_pm_set_feature_config - PM call to request IOCTL for feature config
1709  * @id:         The config ID of the feature to be configured
1710  * @value:      The config value of the feature to be configured
1711  *
1712  * Return:      Returns 0 on success or error value on failure.
1713  */
1714 int zynqmp_pm_set_feature_config(enum pm_feature_config_id id, u32 value)
1715 {
1716 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_SET_FEATURE_CONFIG, id, value);
1717 }
1718 
1719 /**
1720  * zynqmp_pm_get_feature_config - PM call to get value of configured feature
1721  * @id:         The config id of the feature to be queried
1722  * @payload:    Returned value array
1723  *
1724  * Return:      Returns 0 on success or error value on failure.
1725  */
1726 int zynqmp_pm_get_feature_config(enum pm_feature_config_id id,
1727 				 u32 *payload)
1728 {
1729 	return zynqmp_pm_invoke_fn(PM_IOCTL, payload, 3, 0, IOCTL_GET_FEATURE_CONFIG, id);
1730 }
1731 
1732 /**
1733  * zynqmp_pm_sec_read_reg - PM call to securely read from given offset
1734  *		of the node
1735  * @node_id:	Node Id of the device
1736  * @offset:	Offset to be used (20-bit)
1737  * @ret_value:	Output data read from the given offset after
1738  *		firmware access policy is successfully enforced
1739  *
1740  * Return:	Returns 0 on success or error value on failure
1741  */
1742 int zynqmp_pm_sec_read_reg(u32 node_id, u32 offset, u32 *ret_value)
1743 {
1744 	u32 ret_payload[PAYLOAD_ARG_CNT];
1745 	u32 count = 1;
1746 	int ret;
1747 
1748 	if (!ret_value)
1749 		return -EINVAL;
1750 
1751 	ret = zynqmp_pm_invoke_fn(PM_IOCTL, ret_payload, 4, node_id, IOCTL_READ_REG,
1752 				  offset, count);
1753 
1754 	*ret_value = ret_payload[1];
1755 
1756 	return ret;
1757 }
1758 EXPORT_SYMBOL_GPL(zynqmp_pm_sec_read_reg);
1759 
1760 /**
1761  * zynqmp_pm_sec_mask_write_reg - PM call to securely write to given offset
1762  *		of the node
1763  * @node_id:	Node Id of the device
1764  * @offset:	Offset to be used (20-bit)
1765  * @mask:	Mask to be used
1766  * @value:	Value to be written
1767  *
1768  * Return:	Returns 0 on success or error value on failure
1769  */
1770 int zynqmp_pm_sec_mask_write_reg(const u32 node_id, const u32 offset, u32 mask,
1771 				 u32 value)
1772 {
1773 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 5, node_id, IOCTL_MASK_WRITE_REG,
1774 				   offset, mask, value);
1775 }
1776 EXPORT_SYMBOL_GPL(zynqmp_pm_sec_mask_write_reg);
1777 
1778 /**
1779  * zynqmp_pm_set_sd_config - PM call to set value of SD config registers
1780  * @node:	SD node ID
1781  * @config:	The config type of SD registers
1782  * @value:	Value to be set
1783  *
1784  * Return:	Returns 0 on success or error value on failure.
1785  */
1786 int zynqmp_pm_set_sd_config(u32 node, enum pm_sd_config_type config, u32 value)
1787 {
1788 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, node, IOCTL_SET_SD_CONFIG, config, value);
1789 }
1790 EXPORT_SYMBOL_GPL(zynqmp_pm_set_sd_config);
1791 
1792 /**
1793  * zynqmp_pm_set_gem_config - PM call to set value of GEM config registers
1794  * @node:	GEM node ID
1795  * @config:	The config type of GEM registers
1796  * @value:	Value to be set
1797  *
1798  * Return:	Returns 0 on success or error value on failure.
1799  */
1800 int zynqmp_pm_set_gem_config(u32 node, enum pm_gem_config_type config,
1801 			     u32 value)
1802 {
1803 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, node, IOCTL_SET_GEM_CONFIG, config, value);
1804 }
1805 EXPORT_SYMBOL_GPL(zynqmp_pm_set_gem_config);
1806 
1807 /**
1808  * struct zynqmp_pm_shutdown_scope - Struct for shutdown scope
1809  * @subtype:	Shutdown subtype
1810  * @name:	Matching string for scope argument
1811  *
1812  * This struct encapsulates mapping between shutdown scope ID and string.
1813  */
1814 struct zynqmp_pm_shutdown_scope {
1815 	const enum zynqmp_pm_shutdown_subtype subtype;
1816 	const char *name;
1817 };
1818 
1819 static struct zynqmp_pm_shutdown_scope shutdown_scopes[] = {
1820 	[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM] = {
1821 		.subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM,
1822 		.name = "subsystem",
1823 	},
1824 	[ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY] = {
1825 		.subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY,
1826 		.name = "ps_only",
1827 	},
1828 	[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM] = {
1829 		.subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM,
1830 		.name = "system",
1831 	},
1832 };
1833 
1834 static struct zynqmp_pm_shutdown_scope *selected_scope =
1835 		&shutdown_scopes[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM];
1836 
1837 /**
1838  * zynqmp_pm_is_shutdown_scope_valid - Check if shutdown scope string is valid
1839  * @scope_string:	Shutdown scope string
1840  *
1841  * Return:		Return pointer to matching shutdown scope struct from
1842  *			array of available options in system if string is valid,
1843  *			otherwise returns NULL.
1844  */
1845 static struct zynqmp_pm_shutdown_scope*
1846 		zynqmp_pm_is_shutdown_scope_valid(const char *scope_string)
1847 {
1848 	int count;
1849 
1850 	for (count = 0; count < ARRAY_SIZE(shutdown_scopes); count++)
1851 		if (sysfs_streq(scope_string, shutdown_scopes[count].name))
1852 			return &shutdown_scopes[count];
1853 
1854 	return NULL;
1855 }
1856 
1857 static ssize_t shutdown_scope_show(struct device *device,
1858 				   struct device_attribute *attr,
1859 				   char *buf)
1860 {
1861 	int i;
1862 
1863 	for (i = 0; i < ARRAY_SIZE(shutdown_scopes); i++) {
1864 		if (&shutdown_scopes[i] == selected_scope) {
1865 			strcat(buf, "[");
1866 			strcat(buf, shutdown_scopes[i].name);
1867 			strcat(buf, "]");
1868 		} else {
1869 			strcat(buf, shutdown_scopes[i].name);
1870 		}
1871 		strcat(buf, " ");
1872 	}
1873 	strcat(buf, "\n");
1874 
1875 	return strlen(buf);
1876 }
1877 
1878 static ssize_t shutdown_scope_store(struct device *device,
1879 				    struct device_attribute *attr,
1880 				    const char *buf, size_t count)
1881 {
1882 	int ret;
1883 	struct zynqmp_pm_shutdown_scope *scope;
1884 
1885 	scope = zynqmp_pm_is_shutdown_scope_valid(buf);
1886 	if (!scope)
1887 		return -EINVAL;
1888 
1889 	ret = zynqmp_pm_system_shutdown(ZYNQMP_PM_SHUTDOWN_TYPE_SETSCOPE_ONLY,
1890 					scope->subtype);
1891 	if (ret) {
1892 		pr_err("unable to set shutdown scope %s\n", buf);
1893 		return ret;
1894 	}
1895 
1896 	selected_scope = scope;
1897 
1898 	return count;
1899 }
1900 
1901 static DEVICE_ATTR_RW(shutdown_scope);
1902 
1903 static ssize_t health_status_store(struct device *device,
1904 				   struct device_attribute *attr,
1905 				   const char *buf, size_t count)
1906 {
1907 	int ret;
1908 	unsigned int value;
1909 
1910 	ret = kstrtouint(buf, 10, &value);
1911 	if (ret)
1912 		return ret;
1913 
1914 	ret = zynqmp_pm_set_boot_health_status(value);
1915 	if (ret) {
1916 		dev_err(device, "unable to set healthy bit value to %u\n",
1917 			value);
1918 		return ret;
1919 	}
1920 
1921 	return count;
1922 }
1923 
1924 static DEVICE_ATTR_WO(health_status);
1925 
1926 static ssize_t ggs_show(struct device *device,
1927 			struct device_attribute *attr,
1928 			char *buf,
1929 			u32 reg)
1930 {
1931 	int ret;
1932 	u32 ret_payload[PAYLOAD_ARG_CNT];
1933 
1934 	ret = zynqmp_pm_read_ggs(reg, ret_payload);
1935 	if (ret)
1936 		return ret;
1937 
1938 	return sprintf(buf, "0x%x\n", ret_payload[1]);
1939 }
1940 
1941 static ssize_t ggs_store(struct device *device,
1942 			 struct device_attribute *attr,
1943 			 const char *buf, size_t count,
1944 			 u32 reg)
1945 {
1946 	long value;
1947 	int ret;
1948 
1949 	if (reg >= GSS_NUM_REGS)
1950 		return -EINVAL;
1951 
1952 	ret = kstrtol(buf, 16, &value);
1953 	if (ret) {
1954 		count = -EFAULT;
1955 		goto err;
1956 	}
1957 
1958 	ret = zynqmp_pm_write_ggs(reg, value);
1959 	if (ret)
1960 		count = -EFAULT;
1961 err:
1962 	return count;
1963 }
1964 
1965 /* GGS register show functions */
1966 #define GGS0_SHOW(N)						\
1967 	ssize_t ggs##N##_show(struct device *device,		\
1968 			      struct device_attribute *attr,	\
1969 			      char *buf)			\
1970 	{							\
1971 		return ggs_show(device, attr, buf, N);		\
1972 	}
1973 
1974 static GGS0_SHOW(0);
1975 static GGS0_SHOW(1);
1976 static GGS0_SHOW(2);
1977 static GGS0_SHOW(3);
1978 
1979 /* GGS register store function */
1980 #define GGS0_STORE(N)						\
1981 	ssize_t ggs##N##_store(struct device *device,		\
1982 			       struct device_attribute *attr,	\
1983 			       const char *buf,			\
1984 			       size_t count)			\
1985 	{							\
1986 		return ggs_store(device, attr, buf, count, N);	\
1987 	}
1988 
1989 static GGS0_STORE(0);
1990 static GGS0_STORE(1);
1991 static GGS0_STORE(2);
1992 static GGS0_STORE(3);
1993 
1994 static ssize_t pggs_show(struct device *device,
1995 			 struct device_attribute *attr,
1996 			 char *buf,
1997 			 u32 reg)
1998 {
1999 	int ret;
2000 	u32 ret_payload[PAYLOAD_ARG_CNT];
2001 
2002 	ret = zynqmp_pm_read_pggs(reg, ret_payload);
2003 	if (ret)
2004 		return ret;
2005 
2006 	return sprintf(buf, "0x%x\n", ret_payload[1]);
2007 }
2008 
2009 static ssize_t pggs_store(struct device *device,
2010 			  struct device_attribute *attr,
2011 			  const char *buf, size_t count,
2012 			  u32 reg)
2013 {
2014 	long value;
2015 	int ret;
2016 
2017 	if (reg >= GSS_NUM_REGS)
2018 		return -EINVAL;
2019 
2020 	ret = kstrtol(buf, 16, &value);
2021 	if (ret) {
2022 		count = -EFAULT;
2023 		goto err;
2024 	}
2025 
2026 	ret = zynqmp_pm_write_pggs(reg, value);
2027 	if (ret)
2028 		count = -EFAULT;
2029 
2030 err:
2031 	return count;
2032 }
2033 
2034 #define PGGS0_SHOW(N)						\
2035 	ssize_t pggs##N##_show(struct device *device,		\
2036 			       struct device_attribute *attr,	\
2037 			       char *buf)			\
2038 	{							\
2039 		return pggs_show(device, attr, buf, N);		\
2040 	}
2041 
2042 #define PGGS0_STORE(N)						\
2043 	ssize_t pggs##N##_store(struct device *device,		\
2044 				struct device_attribute *attr,	\
2045 				const char *buf,		\
2046 				size_t count)			\
2047 	{							\
2048 		return pggs_store(device, attr, buf, count, N);	\
2049 	}
2050 
2051 /* PGGS register show functions */
2052 static PGGS0_SHOW(0);
2053 static PGGS0_SHOW(1);
2054 static PGGS0_SHOW(2);
2055 static PGGS0_SHOW(3);
2056 
2057 /* PGGS register store functions */
2058 static PGGS0_STORE(0);
2059 static PGGS0_STORE(1);
2060 static PGGS0_STORE(2);
2061 static PGGS0_STORE(3);
2062 
2063 /* GGS register attributes */
2064 static DEVICE_ATTR_RW(ggs0);
2065 static DEVICE_ATTR_RW(ggs1);
2066 static DEVICE_ATTR_RW(ggs2);
2067 static DEVICE_ATTR_RW(ggs3);
2068 
2069 /* PGGS register attributes */
2070 static DEVICE_ATTR_RW(pggs0);
2071 static DEVICE_ATTR_RW(pggs1);
2072 static DEVICE_ATTR_RW(pggs2);
2073 static DEVICE_ATTR_RW(pggs3);
2074 
2075 static ssize_t feature_config_id_show(struct device *device,
2076 				      struct device_attribute *attr,
2077 				      char *buf)
2078 {
2079 	struct zynqmp_devinfo *devinfo = dev_get_drvdata(device);
2080 
2081 	return sysfs_emit(buf, "%d\n", devinfo->feature_conf_id);
2082 }
2083 
2084 static ssize_t feature_config_id_store(struct device *device,
2085 				       struct device_attribute *attr,
2086 				       const char *buf, size_t count)
2087 {
2088 	u32 config_id;
2089 	int ret;
2090 	struct zynqmp_devinfo *devinfo = dev_get_drvdata(device);
2091 
2092 	if (!buf)
2093 		return -EINVAL;
2094 
2095 	ret = kstrtou32(buf, 10, &config_id);
2096 	if (ret)
2097 		return ret;
2098 
2099 	devinfo->feature_conf_id = config_id;
2100 
2101 	return count;
2102 }
2103 
2104 static DEVICE_ATTR_RW(feature_config_id);
2105 
2106 static ssize_t feature_config_value_show(struct device *device,
2107 					 struct device_attribute *attr,
2108 					 char *buf)
2109 {
2110 	int ret;
2111 	u32 ret_payload[PAYLOAD_ARG_CNT];
2112 	struct zynqmp_devinfo *devinfo = dev_get_drvdata(device);
2113 
2114 	ret = zynqmp_pm_get_feature_config(devinfo->feature_conf_id,
2115 					   ret_payload);
2116 	if (ret)
2117 		return ret;
2118 
2119 	return sysfs_emit(buf, "%d\n", ret_payload[1]);
2120 }
2121 
2122 static ssize_t feature_config_value_store(struct device *device,
2123 					  struct device_attribute *attr,
2124 					  const char *buf, size_t count)
2125 {
2126 	u32 value;
2127 	int ret;
2128 	struct zynqmp_devinfo *devinfo = dev_get_drvdata(device);
2129 
2130 	if (!buf)
2131 		return -EINVAL;
2132 
2133 	ret = kstrtou32(buf, 10, &value);
2134 	if (ret)
2135 		return ret;
2136 
2137 	ret = zynqmp_pm_set_feature_config(devinfo->feature_conf_id,
2138 					   value);
2139 	if (ret)
2140 		return ret;
2141 
2142 	return count;
2143 }
2144 
2145 static DEVICE_ATTR_RW(feature_config_value);
2146 
2147 static struct attribute *zynqmp_firmware_attrs[] = {
2148 	&dev_attr_ggs0.attr,
2149 	&dev_attr_ggs1.attr,
2150 	&dev_attr_ggs2.attr,
2151 	&dev_attr_ggs3.attr,
2152 	&dev_attr_pggs0.attr,
2153 	&dev_attr_pggs1.attr,
2154 	&dev_attr_pggs2.attr,
2155 	&dev_attr_pggs3.attr,
2156 	&dev_attr_shutdown_scope.attr,
2157 	&dev_attr_health_status.attr,
2158 	&dev_attr_feature_config_id.attr,
2159 	&dev_attr_feature_config_value.attr,
2160 	NULL,
2161 };
2162 
2163 ATTRIBUTE_GROUPS(zynqmp_firmware);
2164 
2165 /**
2166  * zynqmp_clear_pm_state() - Clear subsystem state
2167  * @dev: Device pointer used for logging
2168  *
2169  * Clears PM specific data in EL3 and platform firmware.
2170  *
2171  * Return: Returns status, either success or error
2172  */
2173 static int zynqmp_clear_pm_state(struct device *dev)
2174 {
2175 	u32 pm_family_code;
2176 	int ret;
2177 
2178 	/* Get the Family code of platform */
2179 	ret = zynqmp_pm_get_family_info(&pm_family_code);
2180 	if (ret < 0)
2181 		return ret;
2182 
2183 	/* Supporting on Versal and Versal Net platforms only */
2184 	if (pm_family_code == PM_VERSAL_FAMILY_CODE ||
2185 	    pm_family_code == PM_VERSAL_NET_FAMILY_CODE) {
2186 		/* Check if EL3 firmware supports TF_A_CLEAR_PM_STATE */
2187 		ret = do_feature_check_call(TF_A_CLEAR_PM_STATE);
2188 		if (ret >= 0 && ((ret & FIRMWARE_VERSION_MASK) >= PM_API_VERSION_1)) {
2189 			/* Clear PM specific data in EL3 firmware */
2190 			ret = zynqmp_pm_invoke_fn(TF_A_CLEAR_PM_STATE, NULL, 0);
2191 			if (ret)
2192 				dev_err(dev,
2193 					"Failed to clear EL3 PM subsystem state: %d\n", ret);
2194 		} else {
2195 			dev_warn(dev, "TF_A_CLEAR_PM_STATE is not supported by EL3 firmware: %d\n", ret);
2196 			ret = 0;
2197 		}
2198 
2199 		/* Check if the firmware supports the PM_DEV_ALL_PERIPH node ID */
2200 		ret = do_feature_check_call(PM_RELEASE_NODE);
2201 		if (ret >= 0 && ((ret & FIRMWARE_VERSION_MASK) >= PM_API_VERSION_3)) {
2202 			/* Attempt to release all peripheral devices via firmware */
2203 			ret = zynqmp_pm_release_node(PM_DEV_ALL_PERIPH);
2204 			if (ret)
2205 				dev_err(dev, "Failed to release all peripheral devices: %d\n", ret);
2206 		} else {
2207 			dev_warn(dev,
2208 				 "Bulk device release is not supported by firmware: %d\n", ret);
2209 			ret = 0;
2210 		}
2211 
2212 		/* Check if the firmware supports the PM_ALL_NOTIFIERS node ID */
2213 		ret = do_feature_check_call(PM_REGISTER_NOTIFIER);
2214 		if (ret >= 0 && ((ret & FIRMWARE_VERSION_MASK) >= PM_API_VERSION_3)) {
2215 			/* Attempt to unregister all notifier callbacks via firmware */
2216 			ret = zynqmp_pm_register_notifier(PM_ALL_NOTIFIERS, 0, 0, 0);
2217 			if (ret)
2218 				dev_err(dev, "Failed to unregister all notifiers: %d\n", ret);
2219 		} else {
2220 			dev_warn(dev,
2221 				 "Firmware doesn't support unregister all notifiers at once: %d\n",
2222 				 ret);
2223 			ret = 0;
2224 		}
2225 	}
2226 
2227 	return ret;
2228 }
2229 
2230 static int zynqmp_firmware_probe(struct platform_device *pdev)
2231 {
2232 	struct device *dev = &pdev->dev;
2233 	struct zynqmp_devinfo *devinfo;
2234 	u32 pm_family_code;
2235 	int ret;
2236 
2237 	ret = get_set_conduit_method(dev->of_node);
2238 	if (ret)
2239 		return ret;
2240 
2241 	/* Get platform-specific firmware data from device tree match */
2242 	active_platform_fw_data = (struct platform_fw_data *)device_get_match_data(dev);
2243 	if (!active_platform_fw_data)
2244 		return -EINVAL;
2245 
2246 	/* Get SiP SVC version number */
2247 	ret = zynqmp_pm_get_sip_svc_version(&sip_svc_version);
2248 	if (ret)
2249 		return ret;
2250 
2251 	ret = do_feature_check_call(PM_FEATURE_CHECK);
2252 	if (ret >= 0 && ((ret & FIRMWARE_VERSION_MASK) >= PM_API_VERSION_1))
2253 		feature_check_enabled = true;
2254 
2255 	devinfo = devm_kzalloc(dev, sizeof(*devinfo), GFP_KERNEL);
2256 	if (!devinfo)
2257 		return -ENOMEM;
2258 
2259 	devinfo->dev = dev;
2260 
2261 	platform_set_drvdata(pdev, devinfo);
2262 
2263 	/* Check PM API version number */
2264 	ret = zynqmp_pm_get_api_version(&pm_api_version);
2265 	if (ret)
2266 		return ret;
2267 
2268 	if (pm_api_version < ZYNQMP_PM_VERSION) {
2269 		panic("%s Platform Management API version error. Expected: v%d.%d - Found: v%d.%d\n",
2270 		      __func__,
2271 		      ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR,
2272 		      pm_api_version >> 16, pm_api_version & 0xFFFF);
2273 	}
2274 
2275 	pr_info("%s Platform Management API v%d.%d\n", __func__,
2276 		pm_api_version >> 16, pm_api_version & 0xFFFF);
2277 
2278 	/* Get the Family code of platform */
2279 	ret = zynqmp_pm_get_family_info(&pm_family_code);
2280 	if (ret < 0)
2281 		return ret;
2282 
2283 	if (is_kdump_kernel())
2284 		zynqmp_clear_pm_state(dev);
2285 
2286 	/* Check trustzone version number */
2287 	ret = zynqmp_pm_get_trustzone_version(&pm_tz_version);
2288 	if (ret)
2289 		panic("Legacy trustzone found without version support\n");
2290 
2291 	if (pm_tz_version < ZYNQMP_TZ_VERSION)
2292 		panic("%s Trustzone version error. Expected: v%d.%d - Found: v%d.%d\n",
2293 		      __func__,
2294 		      ZYNQMP_TZ_VERSION_MAJOR, ZYNQMP_TZ_VERSION_MINOR,
2295 		      pm_tz_version >> 16, pm_tz_version & 0xFFFF);
2296 
2297 	pr_info("%s Trustzone version v%d.%d\n", __func__,
2298 		pm_tz_version >> 16, pm_tz_version & 0xFFFF);
2299 
2300 	ret = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE, firmware_devs,
2301 			      ARRAY_SIZE(firmware_devs), NULL, 0, NULL);
2302 	if (ret) {
2303 		dev_err(&pdev->dev, "failed to add MFD devices %d\n", ret);
2304 		return ret;
2305 	}
2306 
2307 	zynqmp_pm_api_debugfs_init();
2308 
2309 	if (pm_family_code != PM_ZYNQMP_FAMILY_CODE) {
2310 		em_dev = platform_device_register_data(&pdev->dev, "xlnx_event_manager",
2311 						       -1, NULL, 0);
2312 		if (IS_ERR(em_dev))
2313 			dev_err_probe(&pdev->dev, PTR_ERR(em_dev), "EM register fail with error\n");
2314 	}
2315 
2316 	return of_platform_populate(dev->of_node, NULL, NULL, dev);
2317 }
2318 
2319 static void zynqmp_firmware_shutdown(struct platform_device *pdev)
2320 {
2321 	zynqmp_clear_pm_state(&pdev->dev);
2322 }
2323 
2324 static void zynqmp_firmware_remove(struct platform_device *pdev)
2325 {
2326 	struct pm_api_feature_data *feature_data;
2327 	struct hlist_node *tmp;
2328 	int i;
2329 
2330 	mfd_remove_devices(&pdev->dev);
2331 	zynqmp_pm_api_debugfs_exit();
2332 
2333 	hash_for_each_safe(pm_api_features_map, i, tmp, feature_data, hentry) {
2334 		hash_del(&feature_data->hentry);
2335 		kfree(feature_data);
2336 	}
2337 
2338 	platform_device_unregister(em_dev);
2339 }
2340 
2341 static void zynqmp_firmware_sync_state(struct device *dev)
2342 {
2343 	struct device_node *np = dev->of_node;
2344 
2345 	if (!of_device_is_compatible(np, "xlnx,zynqmp-firmware"))
2346 		return;
2347 
2348 	of_genpd_sync_state(np);
2349 
2350 	if (zynqmp_pm_init_finalize())
2351 		dev_warn(dev, "failed to release power management to firmware\n");
2352 }
2353 
2354 static const struct platform_fw_data platform_fw_data_versal = {
2355 	.family_code = PM_VERSAL_FAMILY_CODE,
2356 };
2357 
2358 static const struct platform_fw_data platform_fw_data_versal_net = {
2359 	.family_code = PM_VERSAL_NET_FAMILY_CODE,
2360 };
2361 
2362 static const struct platform_fw_data platform_fw_data_zynqmp = {
2363 	.family_code = PM_ZYNQMP_FAMILY_CODE,
2364 };
2365 
2366 static const struct of_device_id zynqmp_firmware_of_match[] = {
2367 	{.compatible = "xlnx,zynqmp-firmware", .data = &platform_fw_data_zynqmp},
2368 	{.compatible = "xlnx,versal-firmware", .data = &platform_fw_data_versal},
2369 	{.compatible = "xlnx,versal-net-firmware", .data = &platform_fw_data_versal_net},
2370 	{},
2371 };
2372 MODULE_DEVICE_TABLE(of, zynqmp_firmware_of_match);
2373 
2374 static struct platform_driver zynqmp_firmware_driver = {
2375 	.driver = {
2376 		.name = "zynqmp_firmware",
2377 		.of_match_table = zynqmp_firmware_of_match,
2378 		.dev_groups = zynqmp_firmware_groups,
2379 		.sync_state = zynqmp_firmware_sync_state,
2380 	},
2381 	.probe = zynqmp_firmware_probe,
2382 	.remove = zynqmp_firmware_remove,
2383 	.shutdown = zynqmp_firmware_shutdown,
2384 };
2385 module_platform_driver(zynqmp_firmware_driver);
2386