1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Zynq MPSoC Firmware layer for debugfs APIs
5 * Copyright (C) 2014-2018 Xilinx, Inc.
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_FORCE_POWERDOWN),
35 PM_API(PM_REQUEST_WAKEUP),
36 PM_API(PM_SYSTEM_SHUTDOWN),
37 PM_API(PM_REQUEST_NODE),
38 PM_API(PM_RELEASE_NODE),
39 PM_API(PM_SET_REQUIREMENT),
40 PM_API(PM_GET_API_VERSION),
41 PM_API(PM_REGISTER_NOTIFIER),
42 PM_API(PM_RESET_ASSERT),
43 PM_API(PM_RESET_GET_STATUS),
44 PM_API(PM_GET_CHIPID),
45 PM_API(PM_PINCTRL_SET_FUNCTION),
46 PM_API(PM_PINCTRL_CONFIG_PARAM_GET),
47 PM_API(PM_PINCTRL_CONFIG_PARAM_SET),
49 PM_API(PM_CLOCK_ENABLE),
50 PM_API(PM_CLOCK_DISABLE),
51 PM_API(PM_CLOCK_GETSTATE),
52 PM_API(PM_CLOCK_SETDIVIDER),
53 PM_API(PM_CLOCK_GETDIVIDER),
54 PM_API(PM_CLOCK_SETPARENT),
55 PM_API(PM_CLOCK_GETPARENT),
56 PM_API(PM_QUERY_DATA),
59 static struct dentry *firmware_debugfs_root;
62 * zynqmp_pm_ioctl - PM IOCTL for device control and configs
63 * @node: Node ID of the device
64 * @ioctl: ID of the requested IOCTL
65 * @arg1: Argument 1 of requested IOCTL call
66 * @arg2: Argument 2 of requested IOCTL call
67 * @arg3: Argument 3 of requested IOCTL call
68 * @out: Returned output value
70 * Return: Returns status, either success or error+reason
72 static int zynqmp_pm_ioctl(const u32 node, const u32 ioctl, const u32 arg1,
73 const u32 arg2, const u32 arg3, u32 *out)
75 return zynqmp_pm_invoke_fn(PM_IOCTL, out, 5, node, ioctl, arg1, arg2, arg3);
79 * zynqmp_pm_argument_value() - Extract argument value from a PM-API request
80 * @arg: Entered PM-API argument in string format
82 * Return: Argument value in unsigned integer format on success
85 static u64 zynqmp_pm_argument_value(char *arg)
92 if (!kstrtou64(arg, 0, &value))
99 * get_pm_api_id() - Extract API-ID from a PM-API request
100 * @pm_api_req: Entered PM-API argument in string format
103 * Return: 0 on success else error code
105 static int get_pm_api_id(char *pm_api_req, u32 *pm_id)
109 for (i = 0; i < ARRAY_SIZE(pm_api_list) ; i++) {
110 if (!strncasecmp(pm_api_req, pm_api_list[i].api_name,
111 pm_api_list[i].api_name_len)) {
112 *pm_id = pm_api_list[i].api_id;
117 /* If no name was entered look for PM-API ID instead */
118 if (i == ARRAY_SIZE(pm_api_list) && kstrtouint(pm_api_req, 10, pm_id))
124 static int process_api_request(u32 pm_id, u64 *pm_api_arg, u32 *pm_api_ret)
128 struct zynqmp_pm_query_data qdata = {0};
131 case PM_GET_API_VERSION:
132 ret = zynqmp_pm_get_api_version(&pm_api_version);
133 sprintf(debugfs_buf, "PM-API Version = %d.%d\n",
134 pm_api_version >> 16, pm_api_version & 0xffff);
136 case PM_FORCE_POWERDOWN:
137 ret = zynqmp_pm_force_pwrdwn(pm_api_arg[0],
138 pm_api_arg[1] ? pm_api_arg[1] :
139 ZYNQMP_PM_REQUEST_ACK_NO);
141 case PM_REQUEST_WAKEUP:
142 ret = zynqmp_pm_request_wake(pm_api_arg[0],
143 pm_api_arg[1], pm_api_arg[2],
144 pm_api_arg[3] ? pm_api_arg[3] :
145 ZYNQMP_PM_REQUEST_ACK_NO);
147 case PM_SYSTEM_SHUTDOWN:
148 ret = zynqmp_pm_system_shutdown(pm_api_arg[0], pm_api_arg[1]);
150 case PM_REQUEST_NODE:
151 ret = zynqmp_pm_request_node(pm_api_arg[0],
152 pm_api_arg[1] ? pm_api_arg[1] :
153 ZYNQMP_PM_CAPABILITY_ACCESS,
154 pm_api_arg[2] ? pm_api_arg[2] : 0,
155 pm_api_arg[3] ? pm_api_arg[3] :
156 ZYNQMP_PM_REQUEST_ACK_BLOCKING);
158 case PM_RELEASE_NODE:
159 ret = zynqmp_pm_release_node(pm_api_arg[0]);
161 case PM_SET_REQUIREMENT:
162 ret = zynqmp_pm_set_requirement(pm_api_arg[0],
163 pm_api_arg[1] ? pm_api_arg[1] :
164 ZYNQMP_PM_CAPABILITY_CONTEXT,
167 pm_api_arg[3] ? pm_api_arg[3] :
168 ZYNQMP_PM_REQUEST_ACK_BLOCKING);
170 case PM_REGISTER_NOTIFIER:
171 ret = zynqmp_pm_register_notifier(pm_api_arg[0],
179 case PM_RESET_ASSERT:
180 ret = zynqmp_pm_reset_assert(pm_api_arg[0], pm_api_arg[1]);
182 case PM_RESET_GET_STATUS:
183 ret = zynqmp_pm_reset_get_status(pm_api_arg[0], &pm_api_ret[0]);
185 sprintf(debugfs_buf, "Reset status: %u\n",
189 ret = zynqmp_pm_get_chipid(&pm_api_ret[0], &pm_api_ret[1]);
191 sprintf(debugfs_buf, "Idcode: %#x, Version:%#x\n",
192 pm_api_ret[0], pm_api_ret[1]);
194 case PM_PINCTRL_SET_FUNCTION:
195 ret = zynqmp_pm_pinctrl_set_function(pm_api_arg[0],
198 case PM_PINCTRL_CONFIG_PARAM_GET:
199 ret = zynqmp_pm_pinctrl_get_config(pm_api_arg[0], pm_api_arg[1],
203 "Pin: %llu, Param: %llu, Value: %u\n",
204 pm_api_arg[0], pm_api_arg[1],
207 case PM_PINCTRL_CONFIG_PARAM_SET:
208 ret = zynqmp_pm_pinctrl_set_config(pm_api_arg[0],
213 ret = zynqmp_pm_ioctl(pm_api_arg[0], pm_api_arg[1],
214 pm_api_arg[2], pm_api_arg[3],
215 pm_api_arg[4], &pm_api_ret[0]);
216 if (!ret && (pm_api_arg[1] == IOCTL_GET_RPU_OPER_MODE ||
217 pm_api_arg[1] == IOCTL_GET_PLL_FRAC_MODE ||
218 pm_api_arg[1] == IOCTL_GET_PLL_FRAC_DATA ||
219 pm_api_arg[1] == IOCTL_READ_GGS ||
220 pm_api_arg[1] == IOCTL_READ_PGGS ||
221 pm_api_arg[1] == IOCTL_READ_REG))
222 sprintf(debugfs_buf, "IOCTL return value: %u\n",
224 if (!ret && pm_api_arg[1] == IOCTL_GET_QOS)
225 sprintf(debugfs_buf, "Default QoS: %u\nCurrent QoS: %u\n",
226 pm_api_ret[1], pm_api_ret[2]);
228 case PM_CLOCK_ENABLE:
229 ret = zynqmp_pm_clock_enable(pm_api_arg[0]);
231 case PM_CLOCK_DISABLE:
232 ret = zynqmp_pm_clock_disable(pm_api_arg[0]);
234 case PM_CLOCK_GETSTATE:
235 ret = zynqmp_pm_clock_getstate(pm_api_arg[0], &pm_api_ret[0]);
237 sprintf(debugfs_buf, "Clock state: %u\n",
240 case PM_CLOCK_SETDIVIDER:
241 ret = zynqmp_pm_clock_setdivider(pm_api_arg[0], pm_api_arg[1]);
243 case PM_CLOCK_GETDIVIDER:
244 ret = zynqmp_pm_clock_getdivider(pm_api_arg[0], &pm_api_ret[0]);
246 sprintf(debugfs_buf, "Divider Value: %d\n",
249 case PM_CLOCK_SETPARENT:
250 ret = zynqmp_pm_clock_setparent(pm_api_arg[0], pm_api_arg[1]);
252 case PM_CLOCK_GETPARENT:
253 ret = zynqmp_pm_clock_getparent(pm_api_arg[0], &pm_api_ret[0]);
256 "Clock parent Index: %u\n", pm_api_ret[0]);
259 qdata.qid = pm_api_arg[0];
260 qdata.arg1 = pm_api_arg[1];
261 qdata.arg2 = pm_api_arg[2];
262 qdata.arg3 = pm_api_arg[3];
264 ret = zynqmp_pm_query_data(qdata, pm_api_ret);
269 case PM_QID_CLOCK_GET_NAME:
270 sprintf(debugfs_buf, "Clock name = %s\n",
273 case PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS:
274 sprintf(debugfs_buf, "Multiplier = %d, Divider = %d\n",
275 pm_api_ret[1], pm_api_ret[2]);
279 "data[0] = 0x%08x\ndata[1] = 0x%08x\n data[2] = 0x%08x\ndata[3] = 0x%08x\n",
280 pm_api_ret[0], pm_api_ret[1],
281 pm_api_ret[2], pm_api_ret[3]);
285 sprintf(debugfs_buf, "Unsupported PM-API request\n");
293 * zynqmp_pm_debugfs_api_write() - debugfs write function
295 * @ptr: User entered PM-API string
296 * @len: Length of the userspace buffer
297 * @off: Offset within the file
299 * Used for triggering pm api functions by writing
300 * echo <pm_api_id> > /sys/kernel/debug/zynqmp_pm/power or
301 * echo <pm_api_name> > /sys/kernel/debug/zynqmp_pm/power
303 * Return: Number of bytes copied if PM-API request succeeds,
304 * the corresponding error code otherwise
306 static ssize_t zynqmp_pm_debugfs_api_write(struct file *file,
307 const char __user *ptr, size_t len,
310 char *kern_buff, *tmp_buff;
313 u64 pm_api_arg[5] = {0, 0, 0, 0, 0};
314 /* Return values from PM APIs calls */
315 u32 pm_api_ret[4] = {0, 0, 0, 0};
320 strcpy(debugfs_buf, "");
322 if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)
325 kern_buff = memdup_user_nul(ptr, len);
326 if (IS_ERR(kern_buff))
327 return PTR_ERR(kern_buff);
328 tmp_buff = kern_buff;
330 /* Read the API name from a user request */
331 pm_api_req = strsep(&kern_buff, " ");
333 ret = get_pm_api_id(pm_api_req, &pm_id);
337 /* Read node_id and arguments from the PM-API request */
338 pm_api_req = strsep(&kern_buff, " ");
339 while ((i < ARRAY_SIZE(pm_api_arg)) && pm_api_req) {
340 pm_api_arg[i++] = zynqmp_pm_argument_value(pm_api_req);
341 pm_api_req = strsep(&kern_buff, " ");
344 ret = process_api_request(pm_id, pm_api_arg, pm_api_ret);
355 * zynqmp_pm_debugfs_api_read() - debugfs read function
357 * @ptr: Requested pm_api_version string
358 * @len: Length of the userspace buffer
359 * @off: Offset within the file
361 * Return: Length of the version string on success
364 static ssize_t zynqmp_pm_debugfs_api_read(struct file *file, char __user *ptr,
365 size_t len, loff_t *off)
367 return simple_read_from_buffer(ptr, len, off, debugfs_buf,
368 strlen(debugfs_buf));
371 /* Setup debugfs fops */
372 static const struct file_operations fops_zynqmp_pm_dbgfs = {
373 .owner = THIS_MODULE,
374 .write = zynqmp_pm_debugfs_api_write,
375 .read = zynqmp_pm_debugfs_api_read,
379 * zynqmp_pm_api_debugfs_init - Initialize debugfs interface
383 void zynqmp_pm_api_debugfs_init(void)
385 /* Initialize debugfs interface */
386 firmware_debugfs_root = debugfs_create_dir("zynqmp-firmware", NULL);
387 debugfs_create_file("pm", 0660, firmware_debugfs_root, NULL,
388 &fops_zynqmp_pm_dbgfs);
392 * zynqmp_pm_api_debugfs_exit - Remove debugfs interface
396 void zynqmp_pm_api_debugfs_exit(void)
398 debugfs_remove_recursive(firmware_debugfs_root);