1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Zynq MPSoC Firmware layer for debugfs APIs
5 * Copyright (C) 2014-2018 Xilinx, Inc.
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>
13 #include <linux/compiler.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/debugfs.h>
17 #include <linux/uaccess.h>
19 #include <linux/firmware/xlnx-zynqmp.h>
20 #include "zynqmp-debug.h"
22 #define PM_API_NAME_LEN 50
26 char api_name
[PM_API_NAME_LEN
];
30 static char debugfs_buf
[PAGE_SIZE
];
32 #define PM_API(id) {id, #id, strlen(#id)}
33 static struct pm_api_info pm_api_list
[] = {
34 PM_API(PM_GET_API_VERSION
),
35 PM_API(PM_QUERY_DATA
),
38 struct dentry
*firmware_debugfs_root
;
41 * zynqmp_pm_argument_value() - Extract argument value from a PM-API request
42 * @arg: Entered PM-API argument in string format
44 * Return: Argument value in unsigned integer format on success
47 static u64
zynqmp_pm_argument_value(char *arg
)
54 if (!kstrtou64(arg
, 0, &value
))
61 * get_pm_api_id() - Extract API-ID from a PM-API request
62 * @pm_api_req: Entered PM-API argument in string format
65 * Return: 0 on success else error code
67 static int get_pm_api_id(char *pm_api_req
, u32
*pm_id
)
71 for (i
= 0; i
< ARRAY_SIZE(pm_api_list
) ; i
++) {
72 if (!strncasecmp(pm_api_req
, pm_api_list
[i
].api_name
,
73 pm_api_list
[i
].api_name_len
)) {
74 *pm_id
= pm_api_list
[i
].api_id
;
79 /* If no name was entered look for PM-API ID instead */
80 if (i
== ARRAY_SIZE(pm_api_list
) && kstrtouint(pm_api_req
, 10, pm_id
))
86 static int process_api_request(u32 pm_id
, u64
*pm_api_arg
, u32
*pm_api_ret
)
88 const struct zynqmp_eemi_ops
*eemi_ops
= zynqmp_pm_get_eemi_ops();
91 struct zynqmp_pm_query_data qdata
= {0};
97 case PM_GET_API_VERSION
:
98 ret
= eemi_ops
->get_api_version(&pm_api_version
);
99 sprintf(debugfs_buf
, "PM-API Version = %d.%d\n",
100 pm_api_version
>> 16, pm_api_version
& 0xffff);
103 qdata
.qid
= pm_api_arg
[0];
104 qdata
.arg1
= pm_api_arg
[1];
105 qdata
.arg2
= pm_api_arg
[2];
106 qdata
.arg3
= pm_api_arg
[3];
108 ret
= eemi_ops
->query_data(qdata
, pm_api_ret
);
113 case PM_QID_CLOCK_GET_NAME
:
114 sprintf(debugfs_buf
, "Clock name = %s\n",
117 case PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS
:
118 sprintf(debugfs_buf
, "Multiplier = %d, Divider = %d\n",
119 pm_api_ret
[1], pm_api_ret
[2]);
123 "data[0] = 0x%08x\ndata[1] = 0x%08x\n data[2] = 0x%08x\ndata[3] = 0x%08x\n",
124 pm_api_ret
[0], pm_api_ret
[1],
125 pm_api_ret
[2], pm_api_ret
[3]);
129 sprintf(debugfs_buf
, "Unsupported PM-API request\n");
137 * zynqmp_pm_debugfs_api_write() - debugfs write function
139 * @ptr: User entered PM-API string
140 * @len: Length of the userspace buffer
141 * @off: Offset within the file
143 * Used for triggering pm api functions by writing
144 * echo <pm_api_id> > /sys/kernel/debug/zynqmp_pm/power or
145 * echo <pm_api_name> > /sys/kernel/debug/zynqmp_pm/power
147 * Return: Number of bytes copied if PM-API request succeeds,
148 * the corresponding error code otherwise
150 static ssize_t
zynqmp_pm_debugfs_api_write(struct file
*file
,
151 const char __user
*ptr
, size_t len
,
154 char *kern_buff
, *tmp_buff
;
157 u64 pm_api_arg
[4] = {0, 0, 0, 0};
158 /* Return values from PM APIs calls */
159 u32 pm_api_ret
[4] = {0, 0, 0, 0};
164 strcpy(debugfs_buf
, "");
166 if (*off
!= 0 || len
== 0)
169 kern_buff
= kzalloc(len
, GFP_KERNEL
);
173 tmp_buff
= kern_buff
;
175 ret
= strncpy_from_user(kern_buff
, ptr
, len
);
181 /* Read the API name from a user request */
182 pm_api_req
= strsep(&kern_buff
, " ");
184 ret
= get_pm_api_id(pm_api_req
, &pm_id
);
188 /* Read node_id and arguments from the PM-API request */
189 pm_api_req
= strsep(&kern_buff
, " ");
190 while ((i
< ARRAY_SIZE(pm_api_arg
)) && pm_api_req
) {
191 pm_api_arg
[i
++] = zynqmp_pm_argument_value(pm_api_req
);
192 pm_api_req
= strsep(&kern_buff
, " ");
195 ret
= process_api_request(pm_id
, pm_api_arg
, pm_api_ret
);
206 * zynqmp_pm_debugfs_api_read() - debugfs read function
208 * @ptr: Requested pm_api_version string
209 * @len: Length of the userspace buffer
210 * @off: Offset within the file
212 * Return: Length of the version string on success
215 static ssize_t
zynqmp_pm_debugfs_api_read(struct file
*file
, char __user
*ptr
,
216 size_t len
, loff_t
*off
)
218 return simple_read_from_buffer(ptr
, len
, off
, debugfs_buf
,
219 strlen(debugfs_buf
));
222 /* Setup debugfs fops */
223 static const struct file_operations fops_zynqmp_pm_dbgfs
= {
224 .owner
= THIS_MODULE
,
225 .write
= zynqmp_pm_debugfs_api_write
,
226 .read
= zynqmp_pm_debugfs_api_read
,
230 * zynqmp_pm_api_debugfs_init - Initialize debugfs interface
234 void zynqmp_pm_api_debugfs_init(void)
236 /* Initialize debugfs interface */
237 firmware_debugfs_root
= debugfs_create_dir("zynqmp-firmware", NULL
);
238 debugfs_create_file("pm", 0660, firmware_debugfs_root
, NULL
,
239 &fops_zynqmp_pm_dbgfs
);
243 * zynqmp_pm_api_debugfs_exit - Remove debugfs interface
247 void zynqmp_pm_api_debugfs_exit(void)
249 debugfs_remove_recursive(firmware_debugfs_root
);