Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Xilinx Zynq MPSoC Firmware layer for debugfs APIs
0004  *
0005  *  Copyright (C) 2014-2018 Xilinx, Inc.
0006  *
0007  *  Michal Simek <michal.simek@xilinx.com>
0008  *  Davorin Mista <davorin.mista@aggios.com>
0009  *  Jolly Shah <jollys@xilinx.com>
0010  *  Rajan Vaja <rajanv@xilinx.com>
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  * zynqmp_pm_argument_value() - Extract argument value from a PM-API request
0042  * @arg:    Entered PM-API argument in string format
0043  *
0044  * Return: Argument value in unsigned integer format on success
0045  *     0 otherwise
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  * get_pm_api_id() - Extract API-ID from a PM-API request
0062  * @pm_api_req:     Entered PM-API argument in string format
0063  * @pm_id:      API-ID
0064  *
0065  * Return: 0 on success else error code
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     /* If no name was entered look for PM-API ID instead */
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  * zynqmp_pm_debugfs_api_write() - debugfs write function
0134  * @file:   User file
0135  * @ptr:    User entered PM-API string
0136  * @len:    Length of the userspace buffer
0137  * @off:    Offset within the file
0138  *
0139  * Used for triggering pm api functions by writing
0140  * echo <pm_api_id> > /sys/kernel/debug/zynqmp_pm/power or
0141  * echo <pm_api_name>   > /sys/kernel/debug/zynqmp_pm/power
0142  *
0143  * Return: Number of bytes copied if PM-API request succeeds,
0144  *     the corresponding error code otherwise
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     /* Return values from PM APIs calls */
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     /* Read the API name from a user request */
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     /* Read node_id and arguments from the PM-API request */
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  * zynqmp_pm_debugfs_api_read() - debugfs read function
0196  * @file:   User file
0197  * @ptr:    Requested pm_api_version string
0198  * @len:    Length of the userspace buffer
0199  * @off:    Offset within the file
0200  *
0201  * Return: Length of the version string on success
0202  *     else error code
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 /* Setup debugfs fops */
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  * zynqmp_pm_api_debugfs_init - Initialize debugfs interface
0220  *
0221  * Return:  None
0222  */
0223 void zynqmp_pm_api_debugfs_init(void)
0224 {
0225     /* Initialize debugfs interface */
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  * zynqmp_pm_api_debugfs_exit - Remove debugfs interface
0233  *
0234  * Return:  None
0235  */
0236 void zynqmp_pm_api_debugfs_exit(void)
0237 {
0238     debugfs_remove_recursive(firmware_debugfs_root);
0239 }