]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/firmware/xilinx/zynqmp/firmware.c
b049d2c55ecb40d33a7c11b04c50880ed7ad3de4
[zynq/linux.git] / drivers / firmware / xilinx / zynqmp / firmware.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Xilinx Zynq MPSoC Firmware layer
4  *
5  *  Copyright (C) 2014-2018 Xilinx, Inc.
6  *
7  *  Michal Simek <michal.simek@xilinx.com>
8  *  Davorin Mista <davorin.mista@aggios.com>
9  *  Jolly Shah <jollys@xilinx.com>
10  *  Rajan Vaja <rajanv@xilinx.com>
11  */
12
13 #include <linux/arm-smccc.h>
14 #include <linux/compiler.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20
21 #include <linux/firmware/xilinx/zynqmp/firmware.h>
22 #include <linux/firmware/xilinx/zynqmp/firmware-debug.h>
23
24 static unsigned long register_address;
25
26 /**
27  * zynqmp_pm_ret_code - Convert PMU-FW error codes to Linux error codes
28  * @ret_status:         PMUFW return code
29  *
30  * Return:              corresponding Linux error code
31  */
32 static int zynqmp_pm_ret_code(u32 ret_status)
33 {
34         switch (ret_status) {
35         case XST_PM_SUCCESS:
36         case XST_PM_DOUBLE_REQ:
37                 return 0;
38         case XST_PM_NO_ACCESS:
39                 return -EACCES;
40         case XST_PM_ABORT_SUSPEND:
41                 return -ECANCELED;
42         case XST_PM_INTERNAL:
43         case XST_PM_CONFLICT:
44         case XST_PM_INVALID_NODE:
45         default:
46                 return -EINVAL;
47         }
48 }
49
50 static noinline int do_fw_call_fail(u64 arg0, u64 arg1, u64 arg2,
51                                     u32 *ret_payload)
52 {
53         return -ENODEV;
54 }
55
56 /*
57  * PM function call wrapper
58  * Invoke do_fw_call_smc or do_fw_call_hvc, depending on the configuration
59  */
60 static int (*do_fw_call)(u64, u64, u64, u32 *ret_payload) = do_fw_call_fail;
61
62 /**
63  * do_fw_call_smc - Call system-level platform management layer (SMC)
64  * @arg0:               Argument 0 to SMC call
65  * @arg1:               Argument 1 to SMC call
66  * @arg2:               Argument 2 to SMC call
67  * @ret_payload:        Returned value array
68  *
69  * Return:              Returns status, either success or error+reason
70  *
71  * Invoke platform management function via SMC call (no hypervisor present)
72  */
73 static noinline int do_fw_call_smc(u64 arg0, u64 arg1, u64 arg2,
74                                    u32 *ret_payload)
75 {
76         struct arm_smccc_res res;
77
78         arm_smccc_smc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
79
80         if (ret_payload) {
81                 ret_payload[0] = lower_32_bits(res.a0);
82                 ret_payload[1] = upper_32_bits(res.a0);
83                 ret_payload[2] = lower_32_bits(res.a1);
84                 ret_payload[3] = upper_32_bits(res.a1);
85                 ret_payload[4] = lower_32_bits(res.a2);
86         }
87
88         return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
89 }
90
91 /**
92  * do_fw_call_hvc - Call system-level platform management layer (HVC)
93  * @arg0:               Argument 0 to HVC call
94  * @arg1:               Argument 1 to HVC call
95  * @arg2:               Argument 2 to HVC call
96  * @ret_payload:        Returned value array
97  *
98  * Return:              Returns status, either success or error+reason
99  *
100  * Invoke platform management function via HVC
101  * HVC-based for communication through hypervisor
102  * (no direct communication with ATF)
103  */
104 static noinline int do_fw_call_hvc(u64 arg0, u64 arg1, u64 arg2,
105                                    u32 *ret_payload)
106 {
107         struct arm_smccc_res res;
108
109         arm_smccc_hvc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
110
111         if (ret_payload) {
112                 ret_payload[0] = lower_32_bits(res.a0);
113                 ret_payload[1] = upper_32_bits(res.a0);
114                 ret_payload[2] = lower_32_bits(res.a1);
115                 ret_payload[3] = upper_32_bits(res.a1);
116                 ret_payload[4] = lower_32_bits(res.a2);
117         }
118
119         return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
120 }
121
122 /**
123  * zynqmp_pm_invoke_fn - Invoke the system-level platform management layer
124  *                       caller function depending on the configuration
125  * @pm_api_id:          Requested PM-API call
126  * @arg0:               Argument 0 to requested PM-API call
127  * @arg1:               Argument 1 to requested PM-API call
128  * @arg2:               Argument 2 to requested PM-API call
129  * @arg3:               Argument 3 to requested PM-API call
130  * @ret_payload:        Returned value array
131  *
132  * Return:              Returns status, either success or error+reason
133  *
134  * Invoke platform management function for SMC or HVC call, depending on
135  * configuration
136  * Following SMC Calling Convention (SMCCC) for SMC64:
137  * Pm Function Identifier,
138  * PM_SIP_SVC + PM_API_ID =
139  *      ((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)
140  *      ((SMC_64) << FUNCID_CC_SHIFT)
141  *      ((SIP_START) << FUNCID_OEN_SHIFT)
142  *      ((PM_API_ID) & FUNCID_NUM_MASK))
143  *
144  * PM_SIP_SVC   - Registered ZynqMP SIP Service Call
145  * PM_API_ID    - Platform Management API ID
146  */
147 int zynqmp_pm_invoke_fn(u32 pm_api_id, u32 arg0, u32 arg1,
148                         u32 arg2, u32 arg3, u32 *ret_payload)
149 {
150         /*
151          * Added SIP service call Function Identifier
152          * Make sure to stay in x0 register
153          */
154         u64 smc_arg[4];
155
156         smc_arg[0] = PM_SIP_SVC | pm_api_id;
157         smc_arg[1] = ((u64)arg1 << 32) | arg0;
158         smc_arg[2] = ((u64)arg3 << 32) | arg2;
159
160         return do_fw_call(smc_arg[0], smc_arg[1], smc_arg[2], ret_payload);
161 }
162
163 static u32 pm_api_version;
164 static u32 pm_tz_version;
165
166 /**
167  * zynqmp_pm_get_api_version - Get version number of PMU PM firmware
168  * @version:    Returned version value
169  *
170  * Return:      Returns status, either success or error+reason
171  */
172 static int zynqmp_pm_get_api_version(u32 *version)
173 {
174         u32 ret_payload[PAYLOAD_ARG_CNT];
175         int ret;
176
177         if (!version)
178                 return -EINVAL;
179
180         /* Check is PM API version already verified */
181         if (pm_api_version > 0) {
182                 *version = pm_api_version;
183                 return 0;
184         }
185         ret = zynqmp_pm_invoke_fn(PM_GET_API_VERSION, 0, 0, 0, 0, ret_payload);
186         *version = ret_payload[1];
187
188         return ret;
189 }
190
191 /**
192  * zynqmp_pm_get_trustzone_version - Get secure trustzone firmware version
193  * @version:    Returned version value
194  *
195  * Return:      Returns status, either success or error+reason
196  */
197 static int zynqmp_pm_get_trustzone_version(u32 *version)
198 {
199         u32 ret_payload[PAYLOAD_ARG_CNT];
200         int ret;
201
202         if (!version)
203                 return -EINVAL;
204
205         /* Check is PM trustzone version already verified */
206         if (pm_tz_version > 0) {
207                 *version = pm_tz_version;
208                 return 0;
209         }
210         ret = zynqmp_pm_invoke_fn(PM_GET_TRUSTZONE_VERSION, 0, 0,
211                                   0, 0, ret_payload);
212         *version = ret_payload[1];
213
214         return ret;
215 }
216
217 /**
218  * zynqmp_pm_get_chipid - Get silicon ID registers
219  * @idcode:     IDCODE register
220  * @version:    version register
221  *
222  * Return:      Returns the status of the operation and the idcode and version
223  *              registers in @idcode and @version.
224  */
225 static int zynqmp_pm_get_chipid(u32 *idcode, u32 *version)
226 {
227         u32 ret_payload[PAYLOAD_ARG_CNT];
228         int ret;
229
230         if (!idcode || !version)
231                 return -EINVAL;
232
233         ret = zynqmp_pm_invoke_fn(PM_GET_CHIPID, 0, 0, 0, 0, ret_payload);
234         *idcode = ret_payload[1];
235         *version = ret_payload[2];
236
237         return ret;
238 }
239
240 /**
241  * get_set_conduit_method - Choose SMC or HVC based communication
242  * @np:         Pointer to the device_node structure
243  *
244  * Use SMC or HVC-based functions to communicate with EL2/EL3
245  *
246  * Return:      Returns 0 on success or error code
247  */
248 static int get_set_conduit_method(struct device_node *np)
249 {
250         const char *method;
251
252         if (of_property_read_string(np, "method", &method)) {
253                 pr_warn("%s missing \"method\" property\n", __func__);
254                 return -ENXIO;
255         }
256
257         if (!strcmp("hvc", method)) {
258                 do_fw_call = do_fw_call_hvc;
259         } else if (!strcmp("smc", method)) {
260                 do_fw_call = do_fw_call_smc;
261         } else {
262                 pr_warn("%s Invalid \"method\" property: %s\n",
263                         __func__, method);
264                 return -EINVAL;
265         }
266
267         return 0;
268 }
269
270 /**
271  * zynqmp_pm_reset_assert - Request setting of reset (1 - assert, 0 - release)
272  * @reset:              Reset to be configured
273  * @assert_flag:        Flag stating should reset be asserted (1) or
274  *                      released (0)
275  *
276  * Return:              Returns status, either success or error+reason
277  */
278 static int zynqmp_pm_reset_assert(const enum zynqmp_pm_reset reset,
279                                   const enum zynqmp_pm_reset_action assert_flag)
280 {
281         return zynqmp_pm_invoke_fn(PM_RESET_ASSERT, reset, assert_flag,
282                                    0, 0, NULL);
283 }
284
285 /**
286  * zynqmp_pm_reset_get_status - Get status of the reset
287  * @reset:      Reset whose status should be returned
288  * @status:     Returned status
289  *
290  * Return:      Returns status, either success or error+reason
291  */
292 static int zynqmp_pm_reset_get_status(const enum zynqmp_pm_reset reset,
293                                       u32 *status)
294 {
295         u32 ret_payload[PAYLOAD_ARG_CNT];
296         int ret;
297
298         if (!status)
299                 return -EINVAL;
300
301         ret = zynqmp_pm_invoke_fn(PM_RESET_GET_STATUS, reset, 0,
302                                   0, 0, ret_payload);
303         *status = ret_payload[1];
304
305         return ret;
306 }
307
308 /**
309  * zynqmp_pm_fpga_load - Perform the fpga load
310  * @address:    Address to write to
311  * @size:       pl bitstream size
312  * @flags:
313  *      BIT(0) - Bit-stream type.
314  *               0 - Full Bit-stream.
315  *               1 - Partial Bit-stream.
316  *      BIT(1) - Authentication.
317  *               1 - Enable.
318  *               0 - Disable.
319  *      BIT(2) - Encryption.
320  *               1 - Enable.
321  *               0 - Disable.
322  * NOTE -
323  *      The current implementation supports only Full Bit-stream.
324  *
325  * This function provides access to xilfpga library to transfer
326  * the required bitstream into PL.
327  *
328  * Return:      Returns status, either success or error+reason
329  */
330 static int zynqmp_pm_fpga_load(const u64 address, const u32 size,
331                                const u32 flags)
332 {
333         return zynqmp_pm_invoke_fn(PM_FPGA_LOAD, (u32)address,
334                                    ((u32)(address >> 32)), size, flags, NULL);
335 }
336
337 /**
338  * zynqmp_pm_fpga_get_status - Read value from PCAP status register
339  * @value:      Value to read
340  *
341  * This function provides access to the xilfpga library to get
342  * the PCAP status
343  *
344  * Return:      Returns status, either success or error+reason
345  */
346 static int zynqmp_pm_fpga_get_status(u32 *value)
347 {
348         u32 ret_payload[PAYLOAD_ARG_CNT];
349         int ret;
350
351         if (!value)
352                 return -EINVAL;
353
354         ret = zynqmp_pm_invoke_fn(PM_FPGA_GET_STATUS, 0, 0, 0, 0, ret_payload);
355         *value = ret_payload[1];
356
357         return ret;
358 }
359
360 /**
361  * zynqmp_pm_fpga_read - Perform the fpga configuration readback
362  * @reg_numframes: Configuration register offset (or) Number of frames to read
363  * @phys_address: Physical Address of the buffer
364  * @readback_type: Type of fpga readback operation
365  * @value: Value to read
366  *
367  * This function provides access to xilfpga library to perform
368  * fpga configuration readback.
369  *
370  * Return:      Returns status, either success or error+reason
371  */
372 static int zynqmp_pm_fpga_read(const u32 reg_numframes, const u64 phys_address,
373                                u32 readback_type, u32 *value)
374 {
375         u32 ret_payload[PAYLOAD_ARG_CNT];
376         int ret;
377
378         if (!value)
379                 return -EINVAL;
380
381         ret = zynqmp_pm_invoke_fn(PM_FPGA_READ, reg_numframes,
382                                   lower_32_bits(phys_address),
383                                   upper_32_bits(phys_address), readback_type,
384                                   ret_payload);
385         *value = ret_payload[1];
386
387         return ret;
388 }
389
390 /**
391  * zynqmp_pm_request_suspend - PM call to request for another PU or subsystem to
392  *                                      be suspended gracefully.
393  * @node:       Node ID of the targeted PU or subsystem
394  * @ack:        Flag to specify whether acknowledge is requested
395  * @latency:    Requested wakeup latency (not supported)
396  * @state:      Requested state (not supported)
397  *
398  * Return:      Returns status, either success or error+reason
399  */
400 static int zynqmp_pm_request_suspend(const u32 node,
401                                      const enum zynqmp_pm_request_ack ack,
402                                      const u32 latency,
403                                      const u32 state)
404 {
405         return zynqmp_pm_invoke_fn(PM_REQUEST_SUSPEND, node, ack,
406                                    latency, state, NULL);
407 }
408
409 /**
410  * zynqmp_pm_force_powerdown - PM call to request for another PU or subsystem to
411  *                              be powered down forcefully
412  * @target:     Node ID of the targeted PU or subsystem
413  * @ack:        Flag to specify whether acknowledge is requested
414  *
415  * Return:      Returns status, either success or error+reason
416  */
417 static int zynqmp_pm_force_powerdown(const u32 target,
418                                      const enum zynqmp_pm_request_ack ack)
419 {
420         return zynqmp_pm_invoke_fn(PM_FORCE_POWERDOWN, target, ack, 0, 0, NULL);
421 }
422
423 /**
424  * zynqmp_pm_request_wakeup - PM call to wake up selected master or subsystem
425  * @node:       Node ID of the master or subsystem
426  * @set_addr:   Specifies whether the address argument is relevant
427  * @address:    Address from which to resume when woken up
428  * @ack:        Flag to specify whether acknowledge requested
429  *
430  * Return:      Returns status, either success or error+reason
431  */
432 static int zynqmp_pm_request_wakeup(const u32 node,
433                                     const bool set_addr,
434                                     const u64 address,
435                                     const enum zynqmp_pm_request_ack ack)
436 {
437         /* set_addr flag is encoded into 1st bit of address */
438         return zynqmp_pm_invoke_fn(PM_REQUEST_WAKEUP, node, address | set_addr,
439                                    address >> 32, ack, NULL);
440 }
441
442 /**
443  * zynqmp_pm_set_wakeup_source - PM call to specify the wakeup source
444  *                                      while suspended
445  * @target:     Node ID of the targeted PU or subsystem
446  * @wakeup_node:Node ID of the wakeup peripheral
447  * @enable:     Enable or disable the specified peripheral as wake source
448  *
449  * Return:      Returns status, either success or error+reason
450  */
451 static int zynqmp_pm_set_wakeup_source(const u32 target,
452                                        const u32 wakeup_node,
453                                        const u32 enable)
454 {
455         return zynqmp_pm_invoke_fn(PM_SET_WAKEUP_SOURCE, target,
456                                    wakeup_node, enable, 0, NULL);
457 }
458
459 /**
460  * zynqmp_pm_system_shutdown - PM call to request a system shutdown or restart
461  * @type:       Shutdown or restart? 0 for shutdown, 1 for restart
462  * @subtype:    Specifies which system should be restarted or shut down
463  *
464  * Return:      Returns status, either success or error+reason
465  */
466 static int zynqmp_pm_system_shutdown(const u32 type, const u32 subtype)
467 {
468         return zynqmp_pm_invoke_fn(PM_SYSTEM_SHUTDOWN, type, subtype,
469                                    0, 0, NULL);
470 }
471
472 /**
473  * zynqmp_pm_request_node - PM call to request a node with specific capabilities
474  * @node:               Node ID of the slave
475  * @capabilities:       Requested capabilities of the slave
476  * @qos:                Quality of service (not supported)
477  * @ack:                Flag to specify whether acknowledge is requested
478  *
479  * Return:              Returns status, either success or error+reason
480  */
481 static int zynqmp_pm_request_node(const u32 node, const u32 capabilities,
482                                   const u32 qos,
483                                   const enum zynqmp_pm_request_ack ack)
484 {
485         return zynqmp_pm_invoke_fn(PM_REQUEST_NODE, node, capabilities,
486                                    qos, ack, NULL);
487 }
488
489 /**
490  * zynqmp_pm_release_node - PM call to release a node
491  * @node:       Node ID of the slave
492  *
493  * Return:      Returns status, either success or error+reason
494  */
495 static int zynqmp_pm_release_node(const u32 node)
496 {
497         return zynqmp_pm_invoke_fn(PM_RELEASE_NODE, node, 0, 0, 0, NULL);
498 }
499
500 /**
501  * zynqmp_pm_set_requirement - PM call to set requirement for PM slaves
502  * @node:               Node ID of the slave
503  * @capabilities:       Requested capabilities of the slave
504  * @qos:                Quality of service (not supported)
505  * @ack:                Flag to specify whether acknowledge is requested
506  *
507  * This API function is to be used for slaves a PU already has requested
508  *
509  * Return:              Returns status, either success or error+reason
510  */
511 static int zynqmp_pm_set_requirement(const u32 node, const u32 capabilities,
512                                      const u32 qos,
513                                      const enum zynqmp_pm_request_ack ack)
514 {
515         return zynqmp_pm_invoke_fn(PM_SET_REQUIREMENT, node, capabilities,
516                                    qos, ack, NULL);
517 }
518
519 /**
520  * zynqmp_pm_set_max_latency - PM call to set wakeup latency requirements
521  * @node:       Node ID of the slave
522  * @latency:    Requested maximum wakeup latency
523  *
524  * Return:      Returns status, either success or error+reason
525  */
526 static int zynqmp_pm_set_max_latency(const u32 node, const u32 latency)
527 {
528         return zynqmp_pm_invoke_fn(PM_SET_MAX_LATENCY, node, latency,
529                                    0, 0, NULL);
530 }
531
532 /**
533  * zynqmp_pm_set_configuration - PM call to set system configuration
534  * @physical_addr:      Physical 32-bit address of data structure in memory
535  *
536  * Return:              Returns status, either success or error+reason
537  */
538 static int zynqmp_pm_set_configuration(const u32 physical_addr)
539 {
540         return zynqmp_pm_invoke_fn(PM_SET_CONFIGURATION, physical_addr, 0,
541                                    0, 0, NULL);
542 }
543
544 /**
545  * zynqmp_pm_get_node_status - PM call to request a node's current power state
546  * @node:               ID of the component or sub-system in question
547  * @status:             Current operating state of the requested node
548  * @requirements:       Current requirements asserted on the node,
549  *                      used for slave nodes only.
550  * @usage:              Usage information, used for slave nodes only:
551  *                      PM_USAGE_NO_MASTER      - No master is currently using
552  *                                                the node
553  *                      PM_USAGE_CURRENT_MASTER - Only requesting master is
554  *                                                currently using the node
555  *                      PM_USAGE_OTHER_MASTER   - Only other masters are
556  *                                                currently using the node
557  *                      PM_USAGE_BOTH_MASTERS   - Both the current and at least
558  *                                                one other master is currently
559  *                                                using the node
560  *
561  * Return:              Returns status, either success or error+reason
562  */
563 static int zynqmp_pm_get_node_status(const u32 node, u32 *const status,
564                                      u32 *const requirements, u32 *const usage)
565 {
566         u32 ret_payload[PAYLOAD_ARG_CNT];
567         int ret;
568
569         if (!status)
570                 return -EINVAL;
571
572         ret = zynqmp_pm_invoke_fn(PM_GET_NODE_STATUS, node, 0, 0,
573                                   0, ret_payload);
574         if (ret_payload[0] == XST_PM_SUCCESS) {
575                 *status = ret_payload[1];
576                 if (requirements)
577                         *requirements = ret_payload[2];
578                 if (usage)
579                         *usage = ret_payload[3];
580         }
581
582         return ret;
583 }
584
585 /**
586  * zynqmp_pm_get_operating_characteristic - PM call to request operating
587  *                                              characteristic information
588  * @node:       Node ID of the slave
589  * @type:       Type of the operating characteristic requested
590  * @result:     Used to return the requsted operating characteristic
591  *
592  * Return:      Returns status, either success or error+reason
593  */
594 static int zynqmp_pm_get_operating_characteristic(const u32 node,
595                 const enum zynqmp_pm_opchar_type type,
596                 u32 *const result)
597 {
598         u32 ret_payload[PAYLOAD_ARG_CNT];
599         int ret;
600
601         if (!result)
602                 return -EINVAL;
603
604         ret = zynqmp_pm_invoke_fn(PM_GET_OPERATING_CHARACTERISTIC,
605                                   node, type, 0, 0, ret_payload);
606         if (ret_payload[0] == XST_PM_SUCCESS)
607                 *result = ret_payload[1];
608
609         return ret;
610 }
611
612 /**
613  * zynqmp_pm_init_finalize - PM call to informi firmware that the caller master
614  *                              has initialized its own power management
615  *
616  * Return:      Returns status, either success or error+reason
617  */
618 static int zynqmp_pm_init_finalize(void)
619 {
620         return zynqmp_pm_invoke_fn(PM_PM_INIT_FINALIZE, 0, 0, 0, 0, NULL);
621 }
622
623 /**
624  * zynqmp_pm_set_suspend_mode   - Set system suspend mode
625  *
626  * @mode:       Mode to set for system suspend
627  *
628  * Return:      Returns status, either success or error+reason
629  */
630 static int zynqmp_pm_set_suspend_mode(u32 mode)
631 {
632         return zynqmp_pm_invoke_fn(PM_SET_SUSPEND_MODE, mode, 0, 0, 0, NULL);
633 }
634
635 /**
636  * zynqmp_pm_sha_hash - Access the SHA engine to calculate the hash
637  * @address:    Address of the data/ Address of output buffer where
638  *              hash should be stored.
639  * @size:       Size of the data.
640  * @flags:
641  *      BIT(0) - for initializing csudma driver and SHA3(Here address
642  *               and size inputs can be NULL).
643  *      BIT(1) - to call Sha3_Update API which can be called multiple
644  *               times when data is not contiguous.
645  *      BIT(2) - to get final hash of the whole updated data.
646  *               Hash will be overwritten at provided address with
647  *               48 bytes.
648  *
649  * Return:      Returns status, either success or error code.
650  */
651 static int zynqmp_pm_sha_hash(const u64 address, const u32 size,
652                               const u32 flags)
653 {
654         u32 lower_32_bits = (u32)address;
655         u32 upper_32_bits = (u32)(address >> 32);
656
657         return zynqmp_pm_invoke_fn(PM_SECURE_SHA, upper_32_bits, lower_32_bits,
658                                    size, flags, NULL);
659 }
660
661 /**
662  * zynqmp_pm_rsa - Access RSA hardware to encrypt/decrypt the data with RSA.
663  * @address:    Address of the data
664  * @size:       Size of the data.
665  * @flags:
666  *              BIT(0) - Encryption/Decryption
667  *                       0 - RSA decryption with private key
668  *                       1 - RSA encryption with public key.
669  *
670  * Return:      Returns status, either success or error code.
671  */
672 static int zynqmp_pm_rsa(const u64 address, const u32 size, const u32 flags)
673 {
674         u32 lower_32_bits = (u32)address;
675         u32 upper_32_bits = (u32)(address >> 32);
676
677         return zynqmp_pm_invoke_fn(PM_SECURE_RSA, upper_32_bits, lower_32_bits,
678                                    size, flags, NULL);
679 }
680
681 /**
682  * zynqmp_pm_aes - Access AES hardware to encrypt/decrypt the data using
683  * AES-GCM core.
684  * @address:    Address of the AesParams structure.
685  * @out:        Returned output value
686  *
687  * Return:      Returns status, either success or error code.
688  */
689 static int zynqmp_pm_aes_engine(const u64 address, u32 *out)
690 {
691         u32 ret_payload[PAYLOAD_ARG_CNT];
692         int ret;
693
694         if (!out)
695                 return -EINVAL;
696
697         ret = zynqmp_pm_invoke_fn(PM_SECURE_AES, upper_32_bits(address),
698                                   lower_32_bits(address),
699                                   0, 0, ret_payload);
700         *out = ret_payload[1];
701         return ret;
702 }
703
704 /**
705  * zynqmp_pm_pinctrl_request - Request Pin from firmware
706  * @pin:        Pin number to request
707  *
708  * This function requests pin from firmware.
709  *
710  * Return:      Returns status, either success or error+reason.
711  */
712 static int zynqmp_pm_pinctrl_request(const u32 pin)
713 {
714         return zynqmp_pm_invoke_fn(PM_PINCTRL_REQUEST, pin, 0, 0, 0, NULL);
715 }
716
717 /**
718  * zynqmp_pm_pinctrl_release - Inform firmware that Pin control is released
719  * @pin:        Pin number to release
720  *
721  * This function release pin from firmware.
722  *
723  * Return:      Returns status, either success or error+reason.
724  */
725 static int zynqmp_pm_pinctrl_release(const u32 pin)
726 {
727         return zynqmp_pm_invoke_fn(PM_PINCTRL_RELEASE, pin, 0, 0, 0, NULL);
728 }
729
730 /**
731  * zynqmp_pm_pinctrl_get_function - Read function id set for the given pin
732  * @pin:        Pin number
733  * @id:         Buffer to store function ID
734  *
735  * This function provides the function currently set for the given pin.
736  *
737  * Return:      Returns status, either success or error+reason
738  */
739 static int zynqmp_pm_pinctrl_get_function(const u32 pin, u32 *id)
740 {
741         u32 ret_payload[PAYLOAD_ARG_CNT];
742         int ret;
743
744         if (!id)
745                 return -EINVAL;
746
747         ret = zynqmp_pm_invoke_fn(PM_PINCTRL_GET_FUNCTION, pin, 0,
748                                   0, 0, ret_payload);
749         *id = ret_payload[1];
750
751         return ret;
752 }
753
754 /**
755  * zynqmp_pm_pinctrl_set_function - Set requested function for the pin
756  * @pin:        Pin number
757  * @id:         Function ID to set
758  *
759  * This function sets requested function for the given pin.
760  *
761  * Return:      Returns status, either success or error+reason.
762  */
763 static int zynqmp_pm_pinctrl_set_function(const u32 pin, const u32 id)
764 {
765         return zynqmp_pm_invoke_fn(PM_PINCTRL_SET_FUNCTION, pin, id,
766                                    0, 0, NULL);
767 }
768
769 /**
770  * zynqmp_pm_pinctrl_get_config - Get configuration parameter for the pin
771  * @pin:        Pin number
772  * @param:      Parameter to get
773  * @value:      Buffer to store parameter value
774  *
775  * This function gets requested configuration parameter for the given pin.
776  *
777  * Return:      Returns status, either success or error+reason.
778  */
779 static int zynqmp_pm_pinctrl_get_config(const u32 pin, const u32 param,
780                                         u32 *value)
781 {
782         u32 ret_payload[PAYLOAD_ARG_CNT];
783         int ret;
784
785         if (!value)
786                 return -EINVAL;
787
788         ret = zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_GET, pin, param,
789                                   0, 0, ret_payload);
790         *value = ret_payload[1];
791
792         return ret;
793 }
794
795 /**
796  * zynqmp_pm_pinctrl_set_config - Set configuration parameter for the pin
797  * @pin:        Pin number
798  * @param:      Parameter to set
799  * @value:      Parameter value to set
800  *
801  * This function sets requested configuration parameter for the given pin.
802  *
803  * Return:      Returns status, either success or error+reason.
804  */
805 static int zynqmp_pm_pinctrl_set_config(const u32 pin, const u32 param,
806                                         u32 value)
807 {
808         return zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_SET, pin,
809                                    param, value, 0, NULL);
810 }
811
812 /**
813  * zynqmp_pm_ioctl - PM IOCTL API for device control and configs
814  * @node_id:    Node ID of the device
815  * @ioctl_id:   ID of the requested IOCTL
816  * @arg1:       Argument 1 to requested IOCTL call
817  * @arg2:       Argument 2 to requested IOCTL call
818  * @out:        Returned output value
819  *
820  * This function calls IOCTL to firmware for device control and configuration.
821  *
822  * Return:      Returns status, either success or error+reason
823  */
824 static int zynqmp_pm_ioctl(u32 node_id, u32 ioctl_id, u32 arg1, u32 arg2,
825                            u32 *out)
826 {
827         return zynqmp_pm_invoke_fn(PM_IOCTL, node_id, ioctl_id,
828                                    arg1, arg2, out);
829 }
830
831 /**
832  * zynqmp_pm_config_reg_access - PM Config API for Config register access
833  * @register_access_id: ID of the requested REGISTER_ACCESS
834  * @address:            Address of the register to be accessed
835  * @mask:               Mask to be written to the register
836  * @value:              Value to be written to the register
837  * @out:                Returned output value
838  *
839  * This function calls REGISTER_ACCESS to configure CSU/PMU registers.
840  *
841  * Return:      Returns status, either success or error+reason
842  */
843
844 static int zynqmp_pm_config_reg_access(u32 register_access_id, u32 address,
845                                        u32 mask, u32 value, u32 *out)
846 {
847         return zynqmp_pm_invoke_fn(PM_REGISTER_ACCESS, register_access_id,
848                                    address, mask, value, out);
849 }
850
851 static int zynqmp_pm_query_data(struct zynqmp_pm_query_data qdata, u32 *out)
852 {
853         return zynqmp_pm_invoke_fn(PM_QUERY_DATA, qdata.qid, qdata.arg1,
854                                    qdata.arg2, qdata.arg3, out);
855 }
856
857 /**
858  * zynqmp_pm_clock_enable - Enable the clock for given id
859  * @clock_id:   ID of the clock to be enabled
860  *
861  * This function is used by master to enable the clock
862  * including peripherals and PLL clocks.
863  *
864  * Return:      Returns status, either success or error+reason.
865  */
866 static int zynqmp_pm_clock_enable(u32 clock_id)
867 {
868         return zynqmp_pm_invoke_fn(PM_CLOCK_ENABLE, clock_id, 0, 0, 0, NULL);
869 }
870
871 /**
872  * zynqmp_pm_clock_disable - Disable the clock for given id
873  * @clock_id:   ID of the clock to be disable
874  *
875  * This function is used by master to disable the clock
876  * including peripherals and PLL clocks.
877  *
878  * Return:      Returns status, either success or error+reason.
879  */
880 static int zynqmp_pm_clock_disable(u32 clock_id)
881 {
882         return zynqmp_pm_invoke_fn(PM_CLOCK_DISABLE, clock_id, 0, 0, 0, NULL);
883 }
884
885 /**
886  * zynqmp_pm_clock_getstate - Get the clock state for given id
887  * @clock_id:   ID of the clock to be queried
888  * @state:      1/0 (Enabled/Disabled)
889  *
890  * This function is used by master to get the state of clock
891  * including peripherals and PLL clocks.
892  *
893  * Return:      Returns status, either success or error+reason.
894  */
895 static int zynqmp_pm_clock_getstate(u32 clock_id, u32 *state)
896 {
897         u32 ret_payload[PAYLOAD_ARG_CNT];
898         int ret;
899
900         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETSTATE, clock_id, 0,
901                                   0, 0, ret_payload);
902         *state = ret_payload[1];
903
904         return ret;
905 }
906
907 /**
908  * zynqmp_pm_clock_setdivider - Set the clock divider for given id
909  * @clock_id:   ID of the clock
910  * @divider:    divider value.
911  *
912  * This function is used by master to set divider for any clock
913  * to achieve desired rate.
914  *
915  * Return:      Returns status, either success or error+reason.
916  */
917 static int zynqmp_pm_clock_setdivider(u32 clock_id, u32 divider)
918 {
919         return zynqmp_pm_invoke_fn(PM_CLOCK_SETDIVIDER, clock_id, divider,
920                                    0, 0, NULL);
921 }
922
923 /**
924  * zynqmp_pm_clock_getdivider - Get the clock divider for given id
925  * @clock_id:   ID of the clock
926  * @divider:    divider value.
927  *
928  * This function is used by master to get divider values
929  * for any clock.
930  *
931  * Return:      Returns status, either success or error+reason.
932  */
933 static int zynqmp_pm_clock_getdivider(u32 clock_id, u32 *divider)
934 {
935         u32 ret_payload[PAYLOAD_ARG_CNT];
936         int ret;
937
938         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETDIVIDER, clock_id, 0,
939                                   0, 0, ret_payload);
940         *divider = ret_payload[1];
941
942         return ret;
943 }
944
945 /**
946  * zynqmp_pm_clock_setrate - Set the clock rate for given id
947  * @clock_id:   ID of the clock
948  * @rate:       rate value in hz
949  *
950  * This function is used by master to set rate for any clock.
951  *
952  * Return:      Returns status, either success or error+reason.
953  */
954 static int zynqmp_pm_clock_setrate(u32 clock_id, u64 rate)
955 {
956         return zynqmp_pm_invoke_fn(PM_CLOCK_SETRATE, clock_id,
957                                    rate & 0xFFFFFFFF,
958                                    (rate >> 32) & 0xFFFFFFFF,
959                                    0, NULL);
960 }
961
962 /**
963  * zynqmp_pm_clock_getrate - Get the clock rate for given id
964  * @clock_id:   ID of the clock
965  * @rate:       rate value in hz
966  *
967  * This function is used by master to get rate
968  * for any clock.
969  *
970  * Return:      Returns status, either success or error+reason.
971  */
972 static int zynqmp_pm_clock_getrate(u32 clock_id, u64 *rate)
973 {
974         u32 ret_payload[PAYLOAD_ARG_CNT];
975         int ret;
976
977         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETRATE, clock_id, 0,
978                                   0, 0, ret_payload);
979         *rate = ((u64)ret_payload[2] << 32) | ret_payload[1];
980
981         return ret;
982 }
983
984 /**
985  * zynqmp_pm_clock_setparent - Set the clock parent for given id
986  * @clock_id:   ID of the clock
987  * @parent_id:  parent id
988  *
989  * This function is used by master to set parent for any clock.
990  *
991  * Return:      Returns status, either success or error+reason.
992  */
993 static int zynqmp_pm_clock_setparent(u32 clock_id, u32 parent_id)
994 {
995         return zynqmp_pm_invoke_fn(PM_CLOCK_SETPARENT, clock_id,
996                                    parent_id, 0, 0, NULL);
997 }
998
999 /**
1000  * zynqmp_pm_clock_getparent - Get the clock parent for given id
1001  * @clock_id:   ID of the clock
1002  * @parent_id:  parent id
1003  *
1004  * This function is used by master to get parent index
1005  * for any clock.
1006  *
1007  * Return:      Returns status, either success or error+reason.
1008  */
1009 static int zynqmp_pm_clock_getparent(u32 clock_id, u32 *parent_id)
1010 {
1011         u32 ret_payload[PAYLOAD_ARG_CNT];
1012         int ret;
1013
1014         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETPARENT, clock_id, 0,
1015                                   0, 0, ret_payload);
1016         *parent_id = ret_payload[1];
1017
1018         return ret;
1019 }
1020
1021 /**
1022  * zynqmp_pm_efuse_access - Provides access to efuse memory.
1023  * @address:    Address of the efuse params structure
1024  * @out:                Returned output value
1025  *
1026  * Return:      Returns status, either success or error code.
1027  */
1028 static int zynqmp_pm_efuse_access(const u64 address, u32 *out)
1029 {
1030         u32 ret_payload[PAYLOAD_ARG_CNT];
1031         int ret;
1032
1033         if (!out)
1034                 return -EINVAL;
1035
1036         ret = zynqmp_pm_invoke_fn(PM_EFUSE_ACCESS, upper_32_bits(address),
1037                                   lower_32_bits(address), 0, 0, ret_payload);
1038         *out = ret_payload[1];
1039
1040         return ret;
1041 }
1042
1043 static const struct zynqmp_eemi_ops eemi_ops = {
1044         .get_api_version = zynqmp_pm_get_api_version,
1045         .get_chipid = zynqmp_pm_get_chipid,
1046         .reset_assert = zynqmp_pm_reset_assert,
1047         .reset_get_status = zynqmp_pm_reset_get_status,
1048         .fpga_load = zynqmp_pm_fpga_load,
1049         .fpga_get_status = zynqmp_pm_fpga_get_status,
1050         .fpga_read = zynqmp_pm_fpga_read,
1051         .sha_hash = zynqmp_pm_sha_hash,
1052         .rsa = zynqmp_pm_rsa,
1053         .request_suspend = zynqmp_pm_request_suspend,
1054         .force_powerdown = zynqmp_pm_force_powerdown,
1055         .request_wakeup = zynqmp_pm_request_wakeup,
1056         .set_wakeup_source = zynqmp_pm_set_wakeup_source,
1057         .system_shutdown = zynqmp_pm_system_shutdown,
1058         .request_node = zynqmp_pm_request_node,
1059         .release_node = zynqmp_pm_release_node,
1060         .set_requirement = zynqmp_pm_set_requirement,
1061         .set_max_latency = zynqmp_pm_set_max_latency,
1062         .set_configuration = zynqmp_pm_set_configuration,
1063         .get_node_status = zynqmp_pm_get_node_status,
1064         .get_operating_characteristic = zynqmp_pm_get_operating_characteristic,
1065         .init_finalize = zynqmp_pm_init_finalize,
1066         .set_suspend_mode = zynqmp_pm_set_suspend_mode,
1067         .ioctl = zynqmp_pm_ioctl,
1068         .query_data = zynqmp_pm_query_data,
1069         .pinctrl_request = zynqmp_pm_pinctrl_request,
1070         .pinctrl_release = zynqmp_pm_pinctrl_release,
1071         .pinctrl_get_function = zynqmp_pm_pinctrl_get_function,
1072         .pinctrl_set_function = zynqmp_pm_pinctrl_set_function,
1073         .pinctrl_get_config = zynqmp_pm_pinctrl_get_config,
1074         .pinctrl_set_config = zynqmp_pm_pinctrl_set_config,
1075         .clock_enable = zynqmp_pm_clock_enable,
1076         .clock_disable = zynqmp_pm_clock_disable,
1077         .clock_getstate = zynqmp_pm_clock_getstate,
1078         .clock_setdivider = zynqmp_pm_clock_setdivider,
1079         .clock_getdivider = zynqmp_pm_clock_getdivider,
1080         .clock_setrate = zynqmp_pm_clock_setrate,
1081         .clock_getrate = zynqmp_pm_clock_getrate,
1082         .clock_setparent = zynqmp_pm_clock_setparent,
1083         .clock_getparent = zynqmp_pm_clock_getparent,
1084         .register_access = zynqmp_pm_config_reg_access,
1085         .aes = zynqmp_pm_aes_engine,
1086         .efuse_access = zynqmp_pm_efuse_access,
1087 };
1088
1089 /**
1090  * zynqmp_pm_get_eemi_ops - Get eemi ops functions
1091  *
1092  * Return:      - pointer of eemi_ops structure
1093  */
1094 const struct zynqmp_eemi_ops *zynqmp_pm_get_eemi_ops(void)
1095 {
1096         return &eemi_ops;
1097 }
1098 EXPORT_SYMBOL_GPL(zynqmp_pm_get_eemi_ops);
1099
1100 /**
1101  * struct zynqmp_pm_shutdown_scope - Struct for shutdown scope
1102  * @subtype:    Shutdown subtype
1103  * @name:       Matching string for scope argument
1104  *
1105  * This struct encapsulates mapping between shutdown scope ID and string.
1106  */
1107 struct zynqmp_pm_shutdown_scope {
1108         const enum zynqmp_pm_shutdown_subtype subtype;
1109         const char *name;
1110 };
1111
1112 static struct zynqmp_pm_shutdown_scope shutdown_scopes[] = {
1113         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM] = {
1114                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM,
1115                 .name = "subsystem",
1116         },
1117         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY] = {
1118                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY,
1119                 .name = "ps_only",
1120         },
1121         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM] = {
1122                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM,
1123                 .name = "system",
1124         },
1125 };
1126
1127 static struct zynqmp_pm_shutdown_scope *selected_scope =
1128                 &shutdown_scopes[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM];
1129
1130 /**
1131  * zynqmp_pm_is_shutdown_scope_valid - Check if shutdown scope string is valid
1132  * @scope_string:       Shutdown scope string
1133  *
1134  * Return:              Return pointer to matching shutdown scope struct from
1135  *                      array of available options in system if string is valid,
1136  *                      otherwise returns NULL.
1137  */
1138 static struct zynqmp_pm_shutdown_scope*
1139                 zynqmp_pm_is_shutdown_scope_valid(const char *scope_string)
1140 {
1141         int count;
1142
1143         for (count = 0; count < ARRAY_SIZE(shutdown_scopes); count++)
1144                 if (sysfs_streq(scope_string, shutdown_scopes[count].name))
1145                         return &shutdown_scopes[count];
1146
1147         return NULL;
1148 }
1149
1150 /**
1151  * shutdown_scope_show - Show shutdown_scope sysfs attribute
1152  * @kobj:       Kobject structure
1153  * @attr:       Kobject attribute structure
1154  * @buf:        Requested available shutdown_scope attributes string
1155  *
1156  * User-space interface for viewing the available scope options for system
1157  * shutdown. Scope option for next shutdown call is marked with [].
1158  *
1159  * Usage: cat /sys/firmware/zynqmp/shutdown_scope
1160  *
1161  * Return:      Number of bytes printed into the buffer.
1162  */
1163 static ssize_t shutdown_scope_show(struct kobject *kobj,
1164                                    struct kobj_attribute *attr,
1165                                    char *buf)
1166 {
1167         int i;
1168
1169         for (i = 0; i < ARRAY_SIZE(shutdown_scopes); i++) {
1170                 if (&shutdown_scopes[i] == selected_scope) {
1171                         strcat(buf, "[");
1172                         strcat(buf, shutdown_scopes[i].name);
1173                         strcat(buf, "]");
1174                 } else {
1175                         strcat(buf, shutdown_scopes[i].name);
1176                 }
1177                 strcat(buf, " ");
1178         }
1179         strcat(buf, "\n");
1180
1181         return strlen(buf);
1182 }
1183
1184 /**
1185  * shutdown_scope_store - Store shutdown_scope sysfs attribute
1186  * @kobj:       Kobject structure
1187  * @attr:       Kobject attribute structure
1188  * @buf:        User entered shutdown_scope attribute string
1189  * @count:      Buffer size
1190  *
1191  * User-space interface for setting the scope for the next system shutdown.
1192  * Usage: echo <scope> > /sys/firmware/zynqmp/shutdown_scope
1193  *
1194  * The Linux shutdown functionality implemented via PSCI system_off does not
1195  * include an option to set a scope, i.e. which parts of the system to shut
1196  * down.
1197  *
1198  * This API function allows to set the shutdown scope for the next shutdown
1199  * request by passing it to the ATF running in EL3. When the next shutdown
1200  * is performed, the platform specific portion of PSCI-system_off can use
1201  * the chosen shutdown scope.
1202  *
1203  * subsystem:   Only the APU along with all of its peripherals not used by other
1204  *              processing units will be shut down. This may result in the FPD
1205  *              power domain being shut down provided that no other processing
1206  *              unit uses FPD peripherals or DRAM.
1207  * ps_only:     The complete PS will be shut down, including the RPU, PMU, etc.
1208  *              Only the PL domain (FPGA) remains untouched.
1209  * system:      The complete system/device is shut down.
1210  *
1211  * Return:      count argument if request succeeds, the corresponding error
1212  *              code otherwise
1213  */
1214 static ssize_t shutdown_scope_store(struct kobject *kobj,
1215                                     struct kobj_attribute *attr,
1216                                     const char *buf, size_t count)
1217 {
1218         int ret;
1219         struct zynqmp_pm_shutdown_scope *scope;
1220
1221         scope = zynqmp_pm_is_shutdown_scope_valid(buf);
1222         if (!scope)
1223                 return -EINVAL;
1224
1225         ret = zynqmp_pm_system_shutdown(ZYNQMP_PM_SHUTDOWN_TYPE_SETSCOPE_ONLY,
1226                                         scope->subtype);
1227         if (ret) {
1228                 pr_err("unable to set shutdown scope %s\n", buf);
1229                 return ret;
1230         }
1231
1232         selected_scope = scope;
1233
1234         return count;
1235 }
1236
1237 static struct kobj_attribute zynqmp_attr_shutdown_scope =
1238                                                 __ATTR_RW(shutdown_scope);
1239
1240 /**
1241  * health_status_store - Store health_status sysfs attribute
1242  * @kobj:       Kobject structure
1243  * @attr:       Kobject attribute structure
1244  * @buf:        User entered health_status attribute string
1245  * @count:      Buffer size
1246  *
1247  * User-space interface for setting the boot health status.
1248  * Usage: echo <value> > /sys/firmware/zynqmp/health_status
1249  *
1250  * Value:
1251  *      1 - Set healthy bit to 1
1252  *      0 - Unset healthy bit
1253  *
1254  * Return:      count argument if request succeeds, the corresponding error
1255  *              code otherwise
1256  */
1257 static ssize_t health_status_store(struct kobject *kobj,
1258                                    struct kobj_attribute *attr,
1259                                    const char *buf, size_t count)
1260 {
1261         int ret;
1262         unsigned int value;
1263
1264         ret = kstrtouint(buf, 10, &value);
1265         if (ret)
1266                 return ret;
1267
1268         ret = zynqmp_pm_ioctl(0, IOCTL_SET_BOOT_HEALTH_STATUS, value, 0, NULL);
1269         if (ret) {
1270                 pr_err("unable to set healthy bit value to %u\n", value);
1271                 return ret;
1272         }
1273
1274         return count;
1275 }
1276
1277 static struct kobj_attribute zynqmp_attr_health_status =
1278                                                 __ATTR_WO(health_status);
1279
1280 /**
1281  * config_reg_store - Write config_reg sysfs attribute
1282  * @kobj:       Kobject structure
1283  * @attr:       Kobject attribute structure
1284  * @buf:        User entered health_status attribute string
1285  * @count:      Buffer size
1286  *
1287  * User-space interface for setting the config register.
1288  *
1289  * To write any CSU/PMU register
1290  * echo <address> <mask> <values> > /sys/firmware/zynqmp/config_reg
1291  * Usage:
1292  * echo 0x345AB234 0xFFFFFFFF 0x1234ABCD > /sys/firmware/zynqmp/config_reg
1293  *
1294  * To Read any CSU/PMU register, write address to the variable like below
1295  * echo <address> > /sys/firmware/zynqmp/config_reg
1296  *
1297  * Return:      count argument if request succeeds, the corresponding error
1298  *              code otherwise
1299  */
1300 static ssize_t config_reg_store(struct kobject *kobj,
1301                                 struct kobj_attribute *attr,
1302                                 const char *buf, size_t count)
1303 {
1304         char *kern_buff, *inbuf, *tok;
1305         unsigned long address, value, mask;
1306         int ret;
1307
1308         kern_buff = kzalloc(count, GFP_KERNEL);
1309         if (!kern_buff)
1310                 return -ENOMEM;
1311
1312         ret = strlcpy(kern_buff, buf, count);
1313         if (ret < 0) {
1314                 ret = -EFAULT;
1315                 goto err;
1316         }
1317
1318         inbuf = kern_buff;
1319
1320         /* Read the addess */
1321         tok = strsep(&inbuf, " ");
1322         if (!tok) {
1323                 ret = -EFAULT;
1324                 goto err;
1325         }
1326         ret = kstrtol(tok, 16, &address);
1327         if (ret) {
1328                 ret = -EFAULT;
1329                 goto err;
1330         }
1331         /* Read the write value */
1332         tok = strsep(&inbuf, " ");
1333         /*
1334          * If parameter provided is only address, then its a read operation.
1335          * Store the address in a global variable and retrieve whenever
1336          * required.
1337          */
1338         if (!tok) {
1339                 register_address = address;
1340                 goto err;
1341         }
1342         register_address = address;
1343
1344         ret = kstrtol(tok, 16, &mask);
1345         if (ret) {
1346                 ret = -EFAULT;
1347                 goto err;
1348         }
1349         tok = strsep(&inbuf, " ");
1350         if (!tok) {
1351                 ret = -EFAULT;
1352                 goto err;
1353         }
1354         ret = kstrtol(tok, 16, &value);
1355         if (!tok) {
1356                 ret = -EFAULT;
1357                 goto err;
1358         }
1359         ret = zynqmp_pm_config_reg_access(CONFIG_REG_WRITE, address,
1360                                           mask, value, NULL);
1361         if (ret)
1362                 pr_err("unable to write value to %lx\n", value);
1363 err:
1364         kfree(kern_buff);
1365         if (ret)
1366                 return ret;
1367         return count;
1368 }
1369
1370 /**
1371  * config_reg_show - Read config_reg sysfs attribute
1372  * @kobj:       Kobject structure
1373  * @attr:       Kobject attribute structure
1374  * @buf:        User entered health_status attribute string
1375  *
1376  * User-space interface for getting the config register.
1377  *
1378  * To Read any CSU/PMU register, write address to the variable like below
1379  * echo <address> > /sys/firmware/zynqmp/config_reg
1380  *
1381  * Then Read the address using below command
1382  * cat /sys/firmware/zynqmp/config_reg
1383  *
1384  * Return: number of chars written to buf.
1385  */
1386 static ssize_t config_reg_show(struct kobject *kobj,
1387                                struct kobj_attribute *attr,
1388                                char *buf)
1389 {
1390         int ret;
1391         u32 ret_payload[PAYLOAD_ARG_CNT];
1392
1393         ret = zynqmp_pm_config_reg_access(CONFIG_REG_READ, register_address,
1394                                           0, 0, ret_payload);
1395         if (ret)
1396                 return ret;
1397
1398         return sprintf(buf, "0x%x\n", ret_payload[1]);
1399 }
1400
1401 static struct kobj_attribute zynqmp_attr_config_reg =
1402                                         __ATTR_RW(config_reg);
1403
1404 static struct attribute *attrs[] = {
1405         &zynqmp_attr_shutdown_scope.attr,
1406         &zynqmp_attr_health_status.attr,
1407         &zynqmp_attr_config_reg.attr,
1408         NULL,
1409 };
1410
1411 static const struct attribute_group attr_group = {
1412         .attrs = attrs,
1413         NULL,
1414 };
1415
1416 static int zynqmp_pm_sysfs_init(void)
1417 {
1418         struct kobject *zynqmp_kobj;
1419         int ret;
1420
1421         zynqmp_kobj = kobject_create_and_add("zynqmp", firmware_kobj);
1422         if (!zynqmp_kobj) {
1423                 pr_err("zynqmp: Firmware kobj add failed.\n");
1424                 return -ENOMEM;
1425         }
1426
1427         ret = sysfs_create_group(zynqmp_kobj, &attr_group);
1428         if (ret) {
1429                 pr_err("%s() sysfs creation fail with error %d\n",
1430                        __func__, ret);
1431                 goto err;
1432         }
1433
1434         ret = zynqmp_pm_ggs_init(zynqmp_kobj);
1435         if (ret) {
1436                 pr_err("%s() GGS init fail with error %d\n",
1437                        __func__, ret);
1438                 goto err;
1439         }
1440 err:
1441         return ret;
1442 }
1443
1444 static int __init zynqmp_plat_init(void)
1445 {
1446         struct device_node *np;
1447         int ret = 0;
1448
1449         np = of_find_compatible_node(NULL, NULL, "xlnx,zynqmp");
1450         if (!np)
1451                 return 0;
1452         of_node_put(np);
1453
1454         /* We're running on a ZynqMP machine, the PM node is mandatory. */
1455         np = of_find_compatible_node(NULL, NULL, "xlnx,zynqmp-firmware");
1456         if (!np) {
1457                 pr_warn("%s: pm node not found\n", __func__);
1458                 return -ENXIO;
1459         }
1460
1461         ret = get_set_conduit_method(np);
1462         if (ret) {
1463                 of_node_put(np);
1464                 return ret;
1465         }
1466
1467         /* Check PM API version number */
1468         zynqmp_pm_get_api_version(&pm_api_version);
1469         if (pm_api_version < ZYNQMP_PM_VERSION) {
1470                 panic("%s Platform Management API version error. Expected: v%d.%d - Found: v%d.%d\n",
1471                       __func__,
1472                       ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR,
1473                       pm_api_version >> 16, pm_api_version & 0xFFFF);
1474         }
1475
1476         pr_info("%s Platform Management API v%d.%d\n", __func__,
1477                 pm_api_version >> 16, pm_api_version & 0xFFFF);
1478
1479         /* Check trustzone version number */
1480         ret = zynqmp_pm_get_trustzone_version(&pm_tz_version);
1481         if (ret)
1482                 panic("Legacy trustzone found without version support\n");
1483
1484         if (pm_tz_version < ZYNQMP_TZ_VERSION)
1485                 panic("%s Trustzone version error. Expected: v%d.%d - Found: v%d.%d\n",
1486                       __func__,
1487                       ZYNQMP_TZ_VERSION_MAJOR, ZYNQMP_TZ_VERSION_MINOR,
1488                       pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1489
1490         pr_info("%s Trustzone version v%d.%d\n", __func__,
1491                 pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1492
1493         of_node_put(np);
1494
1495         return ret;
1496 }
1497 early_initcall(zynqmp_plat_init);
1498
1499 static int zynqmp_firmware_init(void)
1500 {
1501         int ret;
1502
1503         ret = zynqmp_pm_sysfs_init();
1504         if (ret) {
1505                 pr_err("%s() sysfs init fail with error %d\n", __func__, ret);
1506                 return ret;
1507         }
1508
1509         zynqmp_pm_api_debugfs_init();
1510
1511         return ret;
1512 }
1513 device_initcall(zynqmp_firmware_init);