xref: /linux/drivers/firmware/xilinx/zynqmp-debug.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Zynq MPSoC Firmware layer for debugfs APIs
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/compiler.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/debugfs.h>
17 #include <linux/uaccess.h>
18 
19 #include <linux/firmware/xlnx-zynqmp.h>
20 #include "zynqmp-debug.h"
21 
22 #define PM_API_NAME_LEN			50
23 
24 struct pm_api_info {
25 	u32 api_id;
26 	char api_name[PM_API_NAME_LEN];
27 	char api_name_len;
28 };
29 
30 static char debugfs_buf[PAGE_SIZE];
31 
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),
36 };
37 
38 static struct dentry *firmware_debugfs_root;
39 
40 /**
41  * zynqmp_pm_argument_value() - Extract argument value from a PM-API request
42  * @arg:	Entered PM-API argument in string format
43  *
44  * Return: Argument value in unsigned integer format on success
45  *	   0 otherwise
46  */
47 static u64 zynqmp_pm_argument_value(char *arg)
48 {
49 	u64 value;
50 
51 	if (!arg)
52 		return 0;
53 
54 	if (!kstrtou64(arg, 0, &value))
55 		return value;
56 
57 	return 0;
58 }
59 
60 /**
61  * get_pm_api_id() - Extract API-ID from a PM-API request
62  * @pm_api_req:		Entered PM-API argument in string format
63  * @pm_id:		API-ID
64  *
65  * Return: 0 on success else error code
66  */
67 static int get_pm_api_id(char *pm_api_req, u32 *pm_id)
68 {
69 	int i;
70 
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;
75 			break;
76 		}
77 	}
78 
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))
81 		return -EINVAL;
82 
83 	return 0;
84 }
85 
86 static int process_api_request(u32 pm_id, u64 *pm_api_arg, u32 *pm_api_ret)
87 {
88 	u32 pm_api_version;
89 	int ret;
90 	struct zynqmp_pm_query_data qdata = {0};
91 
92 	switch (pm_id) {
93 	case PM_GET_API_VERSION:
94 		ret = zynqmp_pm_get_api_version(&pm_api_version);
95 		sprintf(debugfs_buf, "PM-API Version = %d.%d\n",
96 			pm_api_version >> 16, pm_api_version & 0xffff);
97 		break;
98 	case PM_QUERY_DATA:
99 		qdata.qid = pm_api_arg[0];
100 		qdata.arg1 = pm_api_arg[1];
101 		qdata.arg2 = pm_api_arg[2];
102 		qdata.arg3 = pm_api_arg[3];
103 
104 		ret = zynqmp_pm_query_data(qdata, pm_api_ret);
105 		if (ret)
106 			break;
107 
108 		switch (qdata.qid) {
109 		case PM_QID_CLOCK_GET_NAME:
110 			sprintf(debugfs_buf, "Clock name = %s\n",
111 				(char *)pm_api_ret);
112 			break;
113 		case PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS:
114 			sprintf(debugfs_buf, "Multiplier = %d, Divider = %d\n",
115 				pm_api_ret[1], pm_api_ret[2]);
116 			break;
117 		default:
118 			sprintf(debugfs_buf,
119 				"data[0] = 0x%08x\ndata[1] = 0x%08x\n data[2] = 0x%08x\ndata[3] = 0x%08x\n",
120 				pm_api_ret[0], pm_api_ret[1],
121 				pm_api_ret[2], pm_api_ret[3]);
122 		}
123 		break;
124 	default:
125 		sprintf(debugfs_buf, "Unsupported PM-API request\n");
126 		ret = -EINVAL;
127 	}
128 
129 	return ret;
130 }
131 
132 /**
133  * zynqmp_pm_debugfs_api_write() - debugfs write function
134  * @file:	User file
135  * @ptr:	User entered PM-API string
136  * @len:	Length of the userspace buffer
137  * @off:	Offset within the file
138  *
139  * Used for triggering pm api functions by writing
140  * echo <pm_api_id>	> /sys/kernel/debug/zynqmp_pm/power or
141  * echo <pm_api_name>	> /sys/kernel/debug/zynqmp_pm/power
142  *
143  * Return: Number of bytes copied if PM-API request succeeds,
144  *	   the corresponding error code otherwise
145  */
146 static ssize_t zynqmp_pm_debugfs_api_write(struct file *file,
147 					   const char __user *ptr, size_t len,
148 					   loff_t *off)
149 {
150 	char *kern_buff, *tmp_buff;
151 	char *pm_api_req;
152 	u32 pm_id = 0;
153 	u64 pm_api_arg[4] = {0, 0, 0, 0};
154 	/* Return values from PM APIs calls */
155 	u32 pm_api_ret[4] = {0, 0, 0, 0};
156 
157 	int ret;
158 	int i = 0;
159 
160 	strcpy(debugfs_buf, "");
161 
162 	if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)
163 		return -EINVAL;
164 
165 	kern_buff = memdup_user_nul(ptr, len);
166 	if (IS_ERR(kern_buff))
167 		return PTR_ERR(kern_buff);
168 	tmp_buff = kern_buff;
169 
170 	/* Read the API name from a user request */
171 	pm_api_req = strsep(&kern_buff, " ");
172 
173 	ret = get_pm_api_id(pm_api_req, &pm_id);
174 	if (ret < 0)
175 		goto err;
176 
177 	/* Read node_id and arguments from the PM-API request */
178 	pm_api_req = strsep(&kern_buff, " ");
179 	while ((i < ARRAY_SIZE(pm_api_arg)) && pm_api_req) {
180 		pm_api_arg[i++] = zynqmp_pm_argument_value(pm_api_req);
181 		pm_api_req = strsep(&kern_buff, " ");
182 	}
183 
184 	ret = process_api_request(pm_id, pm_api_arg, pm_api_ret);
185 
186 err:
187 	kfree(tmp_buff);
188 	if (ret)
189 		return ret;
190 
191 	return len;
192 }
193 
194 /**
195  * zynqmp_pm_debugfs_api_read() - debugfs read function
196  * @file:	User file
197  * @ptr:	Requested pm_api_version string
198  * @len:	Length of the userspace buffer
199  * @off:	Offset within the file
200  *
201  * Return: Length of the version string on success
202  *	   else error code
203  */
204 static ssize_t zynqmp_pm_debugfs_api_read(struct file *file, char __user *ptr,
205 					  size_t len, loff_t *off)
206 {
207 	return simple_read_from_buffer(ptr, len, off, debugfs_buf,
208 				       strlen(debugfs_buf));
209 }
210 
211 /* Setup debugfs fops */
212 static const struct file_operations fops_zynqmp_pm_dbgfs = {
213 	.owner = THIS_MODULE,
214 	.write = zynqmp_pm_debugfs_api_write,
215 	.read = zynqmp_pm_debugfs_api_read,
216 };
217 
218 /**
219  * zynqmp_pm_api_debugfs_init - Initialize debugfs interface
220  *
221  * Return:	None
222  */
223 void zynqmp_pm_api_debugfs_init(void)
224 {
225 	/* Initialize debugfs interface */
226 	firmware_debugfs_root = debugfs_create_dir("zynqmp-firmware", NULL);
227 	debugfs_create_file("pm", 0660, firmware_debugfs_root, NULL,
228 			    &fops_zynqmp_pm_dbgfs);
229 }
230 
231 /**
232  * zynqmp_pm_api_debugfs_exit - Remove debugfs interface
233  *
234  * Return:	None
235  */
236 void zynqmp_pm_api_debugfs_exit(void)
237 {
238 	debugfs_remove_recursive(firmware_debugfs_root);
239 }
240