0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/compiler.h>
0014 #include <linux/module.h>
0015 #include <linux/slab.h>
0016 #include <linux/debugfs.h>
0017 #include <linux/uaccess.h>
0018
0019 #include <linux/firmware/xlnx-zynqmp.h>
0020 #include "zynqmp-debug.h"
0021
0022 #define PM_API_NAME_LEN 50
0023
0024 struct pm_api_info {
0025 u32 api_id;
0026 char api_name[PM_API_NAME_LEN];
0027 char api_name_len;
0028 };
0029
0030 static char debugfs_buf[PAGE_SIZE];
0031
0032 #define PM_API(id) {id, #id, strlen(#id)}
0033 static struct pm_api_info pm_api_list[] = {
0034 PM_API(PM_GET_API_VERSION),
0035 PM_API(PM_QUERY_DATA),
0036 };
0037
0038 static struct dentry *firmware_debugfs_root;
0039
0040
0041
0042
0043
0044
0045
0046
0047 static u64 zynqmp_pm_argument_value(char *arg)
0048 {
0049 u64 value;
0050
0051 if (!arg)
0052 return 0;
0053
0054 if (!kstrtou64(arg, 0, &value))
0055 return value;
0056
0057 return 0;
0058 }
0059
0060
0061
0062
0063
0064
0065
0066
0067 static int get_pm_api_id(char *pm_api_req, u32 *pm_id)
0068 {
0069 int i;
0070
0071 for (i = 0; i < ARRAY_SIZE(pm_api_list) ; i++) {
0072 if (!strncasecmp(pm_api_req, pm_api_list[i].api_name,
0073 pm_api_list[i].api_name_len)) {
0074 *pm_id = pm_api_list[i].api_id;
0075 break;
0076 }
0077 }
0078
0079
0080 if (i == ARRAY_SIZE(pm_api_list) && kstrtouint(pm_api_req, 10, pm_id))
0081 return -EINVAL;
0082
0083 return 0;
0084 }
0085
0086 static int process_api_request(u32 pm_id, u64 *pm_api_arg, u32 *pm_api_ret)
0087 {
0088 u32 pm_api_version;
0089 int ret;
0090 struct zynqmp_pm_query_data qdata = {0};
0091
0092 switch (pm_id) {
0093 case PM_GET_API_VERSION:
0094 ret = zynqmp_pm_get_api_version(&pm_api_version);
0095 sprintf(debugfs_buf, "PM-API Version = %d.%d\n",
0096 pm_api_version >> 16, pm_api_version & 0xffff);
0097 break;
0098 case PM_QUERY_DATA:
0099 qdata.qid = pm_api_arg[0];
0100 qdata.arg1 = pm_api_arg[1];
0101 qdata.arg2 = pm_api_arg[2];
0102 qdata.arg3 = pm_api_arg[3];
0103
0104 ret = zynqmp_pm_query_data(qdata, pm_api_ret);
0105 if (ret)
0106 break;
0107
0108 switch (qdata.qid) {
0109 case PM_QID_CLOCK_GET_NAME:
0110 sprintf(debugfs_buf, "Clock name = %s\n",
0111 (char *)pm_api_ret);
0112 break;
0113 case PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS:
0114 sprintf(debugfs_buf, "Multiplier = %d, Divider = %d\n",
0115 pm_api_ret[1], pm_api_ret[2]);
0116 break;
0117 default:
0118 sprintf(debugfs_buf,
0119 "data[0] = 0x%08x\ndata[1] = 0x%08x\n data[2] = 0x%08x\ndata[3] = 0x%08x\n",
0120 pm_api_ret[0], pm_api_ret[1],
0121 pm_api_ret[2], pm_api_ret[3]);
0122 }
0123 break;
0124 default:
0125 sprintf(debugfs_buf, "Unsupported PM-API request\n");
0126 ret = -EINVAL;
0127 }
0128
0129 return ret;
0130 }
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146 static ssize_t zynqmp_pm_debugfs_api_write(struct file *file,
0147 const char __user *ptr, size_t len,
0148 loff_t *off)
0149 {
0150 char *kern_buff, *tmp_buff;
0151 char *pm_api_req;
0152 u32 pm_id = 0;
0153 u64 pm_api_arg[4] = {0, 0, 0, 0};
0154
0155 u32 pm_api_ret[4] = {0, 0, 0, 0};
0156
0157 int ret;
0158 int i = 0;
0159
0160 strcpy(debugfs_buf, "");
0161
0162 if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)
0163 return -EINVAL;
0164
0165 kern_buff = memdup_user_nul(ptr, len);
0166 if (IS_ERR(kern_buff))
0167 return PTR_ERR(kern_buff);
0168 tmp_buff = kern_buff;
0169
0170
0171 pm_api_req = strsep(&kern_buff, " ");
0172
0173 ret = get_pm_api_id(pm_api_req, &pm_id);
0174 if (ret < 0)
0175 goto err;
0176
0177
0178 pm_api_req = strsep(&kern_buff, " ");
0179 while ((i < ARRAY_SIZE(pm_api_arg)) && pm_api_req) {
0180 pm_api_arg[i++] = zynqmp_pm_argument_value(pm_api_req);
0181 pm_api_req = strsep(&kern_buff, " ");
0182 }
0183
0184 ret = process_api_request(pm_id, pm_api_arg, pm_api_ret);
0185
0186 err:
0187 kfree(tmp_buff);
0188 if (ret)
0189 return ret;
0190
0191 return len;
0192 }
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204 static ssize_t zynqmp_pm_debugfs_api_read(struct file *file, char __user *ptr,
0205 size_t len, loff_t *off)
0206 {
0207 return simple_read_from_buffer(ptr, len, off, debugfs_buf,
0208 strlen(debugfs_buf));
0209 }
0210
0211
0212 static const struct file_operations fops_zynqmp_pm_dbgfs = {
0213 .owner = THIS_MODULE,
0214 .write = zynqmp_pm_debugfs_api_write,
0215 .read = zynqmp_pm_debugfs_api_read,
0216 };
0217
0218
0219
0220
0221
0222
0223 void zynqmp_pm_api_debugfs_init(void)
0224 {
0225
0226 firmware_debugfs_root = debugfs_create_dir("zynqmp-firmware", NULL);
0227 debugfs_create_file("pm", 0660, firmware_debugfs_root, NULL,
0228 &fops_zynqmp_pm_dbgfs);
0229 }
0230
0231
0232
0233
0234
0235
0236 void zynqmp_pm_api_debugfs_exit(void)
0237 {
0238 debugfs_remove_recursive(firmware_debugfs_root);
0239 }