Path: blob/master/drivers/firmware/xilinx/zynqmp-debug.c
26428 views
// SPDX-License-Identifier: GPL-2.01/*2* Xilinx Zynq MPSoC Firmware layer for debugfs APIs3*4* Copyright (C) 2014-2018 Xilinx, Inc.5*6* Michal Simek <[email protected]>7* Davorin Mista <[email protected]>8* Jolly Shah <[email protected]>9* Rajan Vaja <[email protected]>10*/1112#include <linux/compiler.h>13#include <linux/module.h>14#include <linux/slab.h>15#include <linux/debugfs.h>16#include <linux/uaccess.h>1718#include <linux/firmware/xlnx-zynqmp.h>19#include "zynqmp-debug.h"2021#define PM_API_NAME_LEN 502223struct pm_api_info {24u32 api_id;25char api_name[PM_API_NAME_LEN];26char api_name_len;27};2829static char debugfs_buf[PAGE_SIZE];3031#define PM_API(id) {id, #id, strlen(#id)}32static struct pm_api_info pm_api_list[] = {33PM_API(PM_FORCE_POWERDOWN),34PM_API(PM_REQUEST_WAKEUP),35PM_API(PM_SYSTEM_SHUTDOWN),36PM_API(PM_REQUEST_NODE),37PM_API(PM_RELEASE_NODE),38PM_API(PM_SET_REQUIREMENT),39PM_API(PM_GET_API_VERSION),40PM_API(PM_REGISTER_NOTIFIER),41PM_API(PM_RESET_ASSERT),42PM_API(PM_RESET_GET_STATUS),43PM_API(PM_GET_CHIPID),44PM_API(PM_PINCTRL_SET_FUNCTION),45PM_API(PM_PINCTRL_CONFIG_PARAM_GET),46PM_API(PM_PINCTRL_CONFIG_PARAM_SET),47PM_API(PM_IOCTL),48PM_API(PM_CLOCK_ENABLE),49PM_API(PM_CLOCK_DISABLE),50PM_API(PM_CLOCK_GETSTATE),51PM_API(PM_CLOCK_SETDIVIDER),52PM_API(PM_CLOCK_GETDIVIDER),53PM_API(PM_CLOCK_SETPARENT),54PM_API(PM_CLOCK_GETPARENT),55PM_API(PM_QUERY_DATA),56};5758static struct dentry *firmware_debugfs_root;5960/**61* zynqmp_pm_ioctl - PM IOCTL for device control and configs62* @node: Node ID of the device63* @ioctl: ID of the requested IOCTL64* @arg1: Argument 1 of requested IOCTL call65* @arg2: Argument 2 of requested IOCTL call66* @arg3: Argument 3 of requested IOCTL call67* @out: Returned output value68*69* Return: Returns status, either success or error+reason70*/71static int zynqmp_pm_ioctl(const u32 node, const u32 ioctl, const u32 arg1,72const u32 arg2, const u32 arg3, u32 *out)73{74return zynqmp_pm_invoke_fn(PM_IOCTL, out, 5, node, ioctl, arg1, arg2, arg3);75}7677/**78* zynqmp_pm_argument_value() - Extract argument value from a PM-API request79* @arg: Entered PM-API argument in string format80*81* Return: Argument value in unsigned integer format on success82* 0 otherwise83*/84static u64 zynqmp_pm_argument_value(char *arg)85{86u64 value;8788if (!arg)89return 0;9091if (!kstrtou64(arg, 0, &value))92return value;9394return 0;95}9697/**98* get_pm_api_id() - Extract API-ID from a PM-API request99* @pm_api_req: Entered PM-API argument in string format100* @pm_id: API-ID101*102* Return: 0 on success else error code103*/104static int get_pm_api_id(char *pm_api_req, u32 *pm_id)105{106int i;107108for (i = 0; i < ARRAY_SIZE(pm_api_list) ; i++) {109if (!strncasecmp(pm_api_req, pm_api_list[i].api_name,110pm_api_list[i].api_name_len)) {111*pm_id = pm_api_list[i].api_id;112break;113}114}115116/* If no name was entered look for PM-API ID instead */117if (i == ARRAY_SIZE(pm_api_list) && kstrtouint(pm_api_req, 10, pm_id))118return -EINVAL;119120return 0;121}122123static int process_api_request(u32 pm_id, u64 *pm_api_arg, u32 *pm_api_ret)124{125u32 pm_api_version;126int ret;127struct zynqmp_pm_query_data qdata = {0};128129switch (pm_id) {130case PM_GET_API_VERSION:131ret = zynqmp_pm_get_api_version(&pm_api_version);132sprintf(debugfs_buf, "PM-API Version = %d.%d\n",133pm_api_version >> 16, pm_api_version & 0xffff);134break;135case PM_FORCE_POWERDOWN:136ret = zynqmp_pm_force_pwrdwn(pm_api_arg[0],137pm_api_arg[1] ? pm_api_arg[1] :138ZYNQMP_PM_REQUEST_ACK_NO);139break;140case PM_REQUEST_WAKEUP:141ret = zynqmp_pm_request_wake(pm_api_arg[0],142pm_api_arg[1], pm_api_arg[2],143pm_api_arg[3] ? pm_api_arg[3] :144ZYNQMP_PM_REQUEST_ACK_NO);145break;146case PM_SYSTEM_SHUTDOWN:147ret = zynqmp_pm_system_shutdown(pm_api_arg[0], pm_api_arg[1]);148break;149case PM_REQUEST_NODE:150ret = zynqmp_pm_request_node(pm_api_arg[0],151pm_api_arg[1] ? pm_api_arg[1] :152ZYNQMP_PM_CAPABILITY_ACCESS,153pm_api_arg[2] ? pm_api_arg[2] : 0,154pm_api_arg[3] ? pm_api_arg[3] :155ZYNQMP_PM_REQUEST_ACK_BLOCKING);156break;157case PM_RELEASE_NODE:158ret = zynqmp_pm_release_node(pm_api_arg[0]);159break;160case PM_SET_REQUIREMENT:161ret = zynqmp_pm_set_requirement(pm_api_arg[0],162pm_api_arg[1] ? pm_api_arg[1] :163ZYNQMP_PM_CAPABILITY_CONTEXT,164pm_api_arg[2] ?165pm_api_arg[2] : 0,166pm_api_arg[3] ? pm_api_arg[3] :167ZYNQMP_PM_REQUEST_ACK_BLOCKING);168break;169case PM_REGISTER_NOTIFIER:170ret = zynqmp_pm_register_notifier(pm_api_arg[0],171pm_api_arg[1] ?172pm_api_arg[1] : 0,173pm_api_arg[2] ?174pm_api_arg[2] : 0,175pm_api_arg[3] ?176pm_api_arg[3] : 0);177break;178case PM_RESET_ASSERT:179ret = zynqmp_pm_reset_assert(pm_api_arg[0], pm_api_arg[1]);180break;181case PM_RESET_GET_STATUS:182ret = zynqmp_pm_reset_get_status(pm_api_arg[0], &pm_api_ret[0]);183if (!ret)184sprintf(debugfs_buf, "Reset status: %u\n",185pm_api_ret[0]);186break;187case PM_GET_CHIPID:188ret = zynqmp_pm_get_chipid(&pm_api_ret[0], &pm_api_ret[1]);189if (!ret)190sprintf(debugfs_buf, "Idcode: %#x, Version:%#x\n",191pm_api_ret[0], pm_api_ret[1]);192break;193case PM_PINCTRL_SET_FUNCTION:194ret = zynqmp_pm_pinctrl_set_function(pm_api_arg[0],195pm_api_arg[1]);196break;197case PM_PINCTRL_CONFIG_PARAM_GET:198ret = zynqmp_pm_pinctrl_get_config(pm_api_arg[0], pm_api_arg[1],199&pm_api_ret[0]);200if (!ret)201sprintf(debugfs_buf,202"Pin: %llu, Param: %llu, Value: %u\n",203pm_api_arg[0], pm_api_arg[1],204pm_api_ret[0]);205break;206case PM_PINCTRL_CONFIG_PARAM_SET:207ret = zynqmp_pm_pinctrl_set_config(pm_api_arg[0],208pm_api_arg[1],209pm_api_arg[2]);210break;211case PM_IOCTL:212ret = zynqmp_pm_ioctl(pm_api_arg[0], pm_api_arg[1],213pm_api_arg[2], pm_api_arg[3],214pm_api_arg[4], &pm_api_ret[0]);215if (!ret && (pm_api_arg[1] == IOCTL_GET_RPU_OPER_MODE ||216pm_api_arg[1] == IOCTL_GET_PLL_FRAC_MODE ||217pm_api_arg[1] == IOCTL_GET_PLL_FRAC_DATA ||218pm_api_arg[1] == IOCTL_READ_GGS ||219pm_api_arg[1] == IOCTL_READ_PGGS ||220pm_api_arg[1] == IOCTL_READ_REG))221sprintf(debugfs_buf, "IOCTL return value: %u\n",222pm_api_ret[1]);223if (!ret && pm_api_arg[1] == IOCTL_GET_QOS)224sprintf(debugfs_buf, "Default QoS: %u\nCurrent QoS: %u\n",225pm_api_ret[1], pm_api_ret[2]);226break;227case PM_CLOCK_ENABLE:228ret = zynqmp_pm_clock_enable(pm_api_arg[0]);229break;230case PM_CLOCK_DISABLE:231ret = zynqmp_pm_clock_disable(pm_api_arg[0]);232break;233case PM_CLOCK_GETSTATE:234ret = zynqmp_pm_clock_getstate(pm_api_arg[0], &pm_api_ret[0]);235if (!ret)236sprintf(debugfs_buf, "Clock state: %u\n",237pm_api_ret[0]);238break;239case PM_CLOCK_SETDIVIDER:240ret = zynqmp_pm_clock_setdivider(pm_api_arg[0], pm_api_arg[1]);241break;242case PM_CLOCK_GETDIVIDER:243ret = zynqmp_pm_clock_getdivider(pm_api_arg[0], &pm_api_ret[0]);244if (!ret)245sprintf(debugfs_buf, "Divider Value: %d\n",246pm_api_ret[0]);247break;248case PM_CLOCK_SETPARENT:249ret = zynqmp_pm_clock_setparent(pm_api_arg[0], pm_api_arg[1]);250break;251case PM_CLOCK_GETPARENT:252ret = zynqmp_pm_clock_getparent(pm_api_arg[0], &pm_api_ret[0]);253if (!ret)254sprintf(debugfs_buf,255"Clock parent Index: %u\n", pm_api_ret[0]);256break;257case PM_QUERY_DATA:258qdata.qid = pm_api_arg[0];259qdata.arg1 = pm_api_arg[1];260qdata.arg2 = pm_api_arg[2];261qdata.arg3 = pm_api_arg[3];262263ret = zynqmp_pm_query_data(qdata, pm_api_ret);264if (ret)265break;266267switch (qdata.qid) {268case PM_QID_CLOCK_GET_NAME:269sprintf(debugfs_buf, "Clock name = %s\n",270(char *)pm_api_ret);271break;272case PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS:273sprintf(debugfs_buf, "Multiplier = %d, Divider = %d\n",274pm_api_ret[1], pm_api_ret[2]);275break;276default:277sprintf(debugfs_buf,278"data[0] = 0x%08x\ndata[1] = 0x%08x\n data[2] = 0x%08x\ndata[3] = 0x%08x\n",279pm_api_ret[0], pm_api_ret[1],280pm_api_ret[2], pm_api_ret[3]);281}282break;283default:284sprintf(debugfs_buf, "Unsupported PM-API request\n");285ret = -EINVAL;286}287288return ret;289}290291/**292* zynqmp_pm_debugfs_api_write() - debugfs write function293* @file: User file294* @ptr: User entered PM-API string295* @len: Length of the userspace buffer296* @off: Offset within the file297*298* Used for triggering pm api functions by writing299* echo <pm_api_id> > /sys/kernel/debug/zynqmp_pm/power or300* echo <pm_api_name> > /sys/kernel/debug/zynqmp_pm/power301*302* Return: Number of bytes copied if PM-API request succeeds,303* the corresponding error code otherwise304*/305static ssize_t zynqmp_pm_debugfs_api_write(struct file *file,306const char __user *ptr, size_t len,307loff_t *off)308{309char *kern_buff, *tmp_buff;310char *pm_api_req;311u32 pm_id = 0;312u64 pm_api_arg[5] = {0, 0, 0, 0, 0};313/* Return values from PM APIs calls */314u32 pm_api_ret[4] = {0, 0, 0, 0};315316int ret;317int i = 0;318319strcpy(debugfs_buf, "");320321if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)322return -EINVAL;323324kern_buff = memdup_user_nul(ptr, len);325if (IS_ERR(kern_buff))326return PTR_ERR(kern_buff);327tmp_buff = kern_buff;328329/* Read the API name from a user request */330pm_api_req = strsep(&kern_buff, " ");331332ret = get_pm_api_id(pm_api_req, &pm_id);333if (ret < 0)334goto err;335336/* Read node_id and arguments from the PM-API request */337pm_api_req = strsep(&kern_buff, " ");338while ((i < ARRAY_SIZE(pm_api_arg)) && pm_api_req) {339pm_api_arg[i++] = zynqmp_pm_argument_value(pm_api_req);340pm_api_req = strsep(&kern_buff, " ");341}342343ret = process_api_request(pm_id, pm_api_arg, pm_api_ret);344345err:346kfree(tmp_buff);347if (ret)348return ret;349350return len;351}352353/**354* zynqmp_pm_debugfs_api_read() - debugfs read function355* @file: User file356* @ptr: Requested pm_api_version string357* @len: Length of the userspace buffer358* @off: Offset within the file359*360* Return: Length of the version string on success361* else error code362*/363static ssize_t zynqmp_pm_debugfs_api_read(struct file *file, char __user *ptr,364size_t len, loff_t *off)365{366return simple_read_from_buffer(ptr, len, off, debugfs_buf,367strlen(debugfs_buf));368}369370/* Setup debugfs fops */371static const struct file_operations fops_zynqmp_pm_dbgfs = {372.owner = THIS_MODULE,373.write = zynqmp_pm_debugfs_api_write,374.read = zynqmp_pm_debugfs_api_read,375};376377/**378* zynqmp_pm_api_debugfs_init - Initialize debugfs interface379*380* Return: None381*/382void zynqmp_pm_api_debugfs_init(void)383{384/* Initialize debugfs interface */385firmware_debugfs_root = debugfs_create_dir("zynqmp-firmware", NULL);386debugfs_create_file("pm", 0660, firmware_debugfs_root, NULL,387&fops_zynqmp_pm_dbgfs);388}389390/**391* zynqmp_pm_api_debugfs_exit - Remove debugfs interface392*393* Return: None394*/395void zynqmp_pm_api_debugfs_exit(void)396{397debugfs_remove_recursive(firmware_debugfs_root);398}399400401