zynqmp-debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. * Copyright (C) 2022 - 2025 Advanced Micro Devices, Inc.
  7. *
  8. * Michal Simek <michal.simek@amd.com>
  9. * Davorin Mista <davorin.mista@aggios.com>
  10. * Jolly Shah <jollys@xilinx.com>
  11. * Rajan Vaja <rajanv@xilinx.com>
  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. #include <linux/firmware/xlnx-zynqmp.h>
  19. #include "zynqmp-debug.h"
  20. #define PM_API_NAME_LEN 50
  21. struct pm_api_info {
  22. u32 api_id;
  23. char api_name[PM_API_NAME_LEN];
  24. char api_name_len;
  25. };
  26. static char debugfs_buf[PAGE_SIZE];
  27. #define PM_API(id) {id, #id, strlen(#id)}
  28. static struct pm_api_info pm_api_list[] = {
  29. PM_API(PM_FORCE_POWERDOWN),
  30. PM_API(PM_REQUEST_WAKEUP),
  31. PM_API(PM_SYSTEM_SHUTDOWN),
  32. PM_API(PM_REQUEST_NODE),
  33. PM_API(PM_RELEASE_NODE),
  34. PM_API(PM_SET_REQUIREMENT),
  35. PM_API(PM_GET_API_VERSION),
  36. PM_API(PM_GET_NODE_STATUS),
  37. PM_API(PM_REGISTER_NOTIFIER),
  38. PM_API(PM_RESET_ASSERT),
  39. PM_API(PM_RESET_GET_STATUS),
  40. PM_API(PM_GET_CHIPID),
  41. PM_API(PM_PINCTRL_SET_FUNCTION),
  42. PM_API(PM_PINCTRL_CONFIG_PARAM_GET),
  43. PM_API(PM_PINCTRL_CONFIG_PARAM_SET),
  44. PM_API(PM_IOCTL),
  45. PM_API(PM_CLOCK_ENABLE),
  46. PM_API(PM_CLOCK_DISABLE),
  47. PM_API(PM_CLOCK_GETSTATE),
  48. PM_API(PM_CLOCK_SETDIVIDER),
  49. PM_API(PM_CLOCK_GETDIVIDER),
  50. PM_API(PM_CLOCK_SETPARENT),
  51. PM_API(PM_CLOCK_GETPARENT),
  52. PM_API(PM_QUERY_DATA),
  53. };
  54. static struct dentry *firmware_debugfs_root;
  55. /**
  56. * zynqmp_pm_ioctl - PM IOCTL for device control and configs
  57. * @node: Node ID of the device
  58. * @ioctl: ID of the requested IOCTL
  59. * @arg1: Argument 1 of requested IOCTL call
  60. * @arg2: Argument 2 of requested IOCTL call
  61. * @arg3: Argument 3 of requested IOCTL call
  62. * @out: Returned output value
  63. *
  64. * Return: Returns status, either success or error+reason
  65. */
  66. static int zynqmp_pm_ioctl(const u32 node, const u32 ioctl, const u32 arg1,
  67. const u32 arg2, const u32 arg3, u32 *out)
  68. {
  69. return zynqmp_pm_invoke_fn(PM_IOCTL, out, 5, node, ioctl, arg1, arg2, arg3);
  70. }
  71. /**
  72. * zynqmp_pm_argument_value() - Extract argument value from a PM-API request
  73. * @arg: Entered PM-API argument in string format
  74. *
  75. * Return: Argument value in unsigned integer format on success
  76. * 0 otherwise
  77. */
  78. static u64 zynqmp_pm_argument_value(char *arg)
  79. {
  80. u64 value;
  81. if (!arg)
  82. return 0;
  83. if (!kstrtou64(arg, 0, &value))
  84. return value;
  85. return 0;
  86. }
  87. /**
  88. * get_pm_api_id() - Extract API-ID from a PM-API request
  89. * @pm_api_req: Entered PM-API argument in string format
  90. * @pm_id: API-ID
  91. *
  92. * Return: 0 on success else error code
  93. */
  94. static int get_pm_api_id(char *pm_api_req, u32 *pm_id)
  95. {
  96. int i;
  97. for (i = 0; i < ARRAY_SIZE(pm_api_list) ; i++) {
  98. if (!strncasecmp(pm_api_req, pm_api_list[i].api_name,
  99. pm_api_list[i].api_name_len)) {
  100. *pm_id = pm_api_list[i].api_id;
  101. break;
  102. }
  103. }
  104. /* If no name was entered look for PM-API ID instead */
  105. if (i == ARRAY_SIZE(pm_api_list) && kstrtouint(pm_api_req, 10, pm_id))
  106. return -EINVAL;
  107. return 0;
  108. }
  109. static int process_api_request(u32 pm_id, u64 *pm_api_arg, u32 *pm_api_ret)
  110. {
  111. u32 pm_api_version;
  112. int ret;
  113. struct zynqmp_pm_query_data qdata = {0};
  114. switch (pm_id) {
  115. case PM_GET_API_VERSION:
  116. ret = zynqmp_pm_get_api_version(&pm_api_version);
  117. sprintf(debugfs_buf, "PM-API Version = %d.%d\n",
  118. pm_api_version >> 16, pm_api_version & 0xffff);
  119. break;
  120. case PM_FORCE_POWERDOWN:
  121. ret = zynqmp_pm_force_pwrdwn(pm_api_arg[0],
  122. pm_api_arg[1] ? pm_api_arg[1] :
  123. ZYNQMP_PM_REQUEST_ACK_NO);
  124. break;
  125. case PM_REQUEST_WAKEUP:
  126. ret = zynqmp_pm_request_wake(pm_api_arg[0],
  127. pm_api_arg[1], pm_api_arg[2],
  128. pm_api_arg[3] ? pm_api_arg[3] :
  129. ZYNQMP_PM_REQUEST_ACK_NO);
  130. break;
  131. case PM_SYSTEM_SHUTDOWN:
  132. ret = zynqmp_pm_system_shutdown(pm_api_arg[0], pm_api_arg[1]);
  133. break;
  134. case PM_REQUEST_NODE:
  135. ret = zynqmp_pm_request_node(pm_api_arg[0],
  136. pm_api_arg[1] ? pm_api_arg[1] :
  137. ZYNQMP_PM_CAPABILITY_ACCESS,
  138. pm_api_arg[2] ? pm_api_arg[2] : 0,
  139. pm_api_arg[3] ? pm_api_arg[3] :
  140. ZYNQMP_PM_REQUEST_ACK_BLOCKING);
  141. break;
  142. case PM_RELEASE_NODE:
  143. ret = zynqmp_pm_release_node(pm_api_arg[0]);
  144. break;
  145. case PM_SET_REQUIREMENT:
  146. ret = zynqmp_pm_set_requirement(pm_api_arg[0],
  147. pm_api_arg[1] ? pm_api_arg[1] :
  148. ZYNQMP_PM_CAPABILITY_CONTEXT,
  149. pm_api_arg[2] ?
  150. pm_api_arg[2] : 0,
  151. pm_api_arg[3] ? pm_api_arg[3] :
  152. ZYNQMP_PM_REQUEST_ACK_BLOCKING);
  153. break;
  154. case PM_GET_NODE_STATUS:
  155. ret = zynqmp_pm_get_node_status(pm_api_arg[0],
  156. &pm_api_ret[0],
  157. &pm_api_ret[1],
  158. &pm_api_ret[2]);
  159. if (!ret)
  160. sprintf(debugfs_buf,
  161. "GET_NODE_STATUS:\n\tNodeId: %llu\n\tStatus: %u\n\tRequirements: %u\n\tUsage: %u\n",
  162. pm_api_arg[0], pm_api_ret[0],
  163. pm_api_ret[1], pm_api_ret[2]);
  164. break;
  165. case PM_REGISTER_NOTIFIER:
  166. ret = zynqmp_pm_register_notifier(pm_api_arg[0],
  167. pm_api_arg[1] ?
  168. pm_api_arg[1] : 0,
  169. pm_api_arg[2] ?
  170. pm_api_arg[2] : 0,
  171. pm_api_arg[3] ?
  172. pm_api_arg[3] : 0);
  173. break;
  174. case PM_RESET_ASSERT:
  175. ret = zynqmp_pm_reset_assert(pm_api_arg[0], pm_api_arg[1]);
  176. break;
  177. case PM_RESET_GET_STATUS:
  178. ret = zynqmp_pm_reset_get_status(pm_api_arg[0], &pm_api_ret[0]);
  179. if (!ret)
  180. sprintf(debugfs_buf, "Reset status: %u\n",
  181. pm_api_ret[0]);
  182. break;
  183. case PM_GET_CHIPID:
  184. ret = zynqmp_pm_get_chipid(&pm_api_ret[0], &pm_api_ret[1]);
  185. if (!ret)
  186. sprintf(debugfs_buf, "Idcode: %#x, Version:%#x\n",
  187. pm_api_ret[0], pm_api_ret[1]);
  188. break;
  189. case PM_PINCTRL_SET_FUNCTION:
  190. ret = zynqmp_pm_pinctrl_set_function(pm_api_arg[0],
  191. pm_api_arg[1]);
  192. break;
  193. case PM_PINCTRL_CONFIG_PARAM_GET:
  194. ret = zynqmp_pm_pinctrl_get_config(pm_api_arg[0], pm_api_arg[1],
  195. &pm_api_ret[0]);
  196. if (!ret)
  197. sprintf(debugfs_buf,
  198. "Pin: %llu, Param: %llu, Value: %u\n",
  199. pm_api_arg[0], pm_api_arg[1],
  200. pm_api_ret[0]);
  201. break;
  202. case PM_PINCTRL_CONFIG_PARAM_SET:
  203. ret = zynqmp_pm_pinctrl_set_config(pm_api_arg[0],
  204. pm_api_arg[1],
  205. pm_api_arg[2]);
  206. break;
  207. case PM_IOCTL:
  208. ret = zynqmp_pm_ioctl(pm_api_arg[0], pm_api_arg[1],
  209. pm_api_arg[2], pm_api_arg[3],
  210. pm_api_arg[4], &pm_api_ret[0]);
  211. if (!ret && (pm_api_arg[1] == IOCTL_GET_RPU_OPER_MODE ||
  212. pm_api_arg[1] == IOCTL_GET_PLL_FRAC_MODE ||
  213. pm_api_arg[1] == IOCTL_GET_PLL_FRAC_DATA ||
  214. pm_api_arg[1] == IOCTL_READ_GGS ||
  215. pm_api_arg[1] == IOCTL_READ_PGGS ||
  216. pm_api_arg[1] == IOCTL_READ_REG))
  217. sprintf(debugfs_buf, "IOCTL return value: %u\n",
  218. pm_api_ret[1]);
  219. if (!ret && pm_api_arg[1] == IOCTL_GET_QOS)
  220. sprintf(debugfs_buf, "Default QoS: %u\nCurrent QoS: %u\n",
  221. pm_api_ret[1], pm_api_ret[2]);
  222. break;
  223. case PM_CLOCK_ENABLE:
  224. ret = zynqmp_pm_clock_enable(pm_api_arg[0]);
  225. break;
  226. case PM_CLOCK_DISABLE:
  227. ret = zynqmp_pm_clock_disable(pm_api_arg[0]);
  228. break;
  229. case PM_CLOCK_GETSTATE:
  230. ret = zynqmp_pm_clock_getstate(pm_api_arg[0], &pm_api_ret[0]);
  231. if (!ret)
  232. sprintf(debugfs_buf, "Clock state: %u\n",
  233. pm_api_ret[0]);
  234. break;
  235. case PM_CLOCK_SETDIVIDER:
  236. ret = zynqmp_pm_clock_setdivider(pm_api_arg[0], pm_api_arg[1]);
  237. break;
  238. case PM_CLOCK_GETDIVIDER:
  239. ret = zynqmp_pm_clock_getdivider(pm_api_arg[0], &pm_api_ret[0]);
  240. if (!ret)
  241. sprintf(debugfs_buf, "Divider Value: %d\n",
  242. pm_api_ret[0]);
  243. break;
  244. case PM_CLOCK_SETPARENT:
  245. ret = zynqmp_pm_clock_setparent(pm_api_arg[0], pm_api_arg[1]);
  246. break;
  247. case PM_CLOCK_GETPARENT:
  248. ret = zynqmp_pm_clock_getparent(pm_api_arg[0], &pm_api_ret[0]);
  249. if (!ret)
  250. sprintf(debugfs_buf,
  251. "Clock parent Index: %u\n", pm_api_ret[0]);
  252. break;
  253. case PM_QUERY_DATA:
  254. qdata.qid = pm_api_arg[0];
  255. qdata.arg1 = pm_api_arg[1];
  256. qdata.arg2 = pm_api_arg[2];
  257. qdata.arg3 = pm_api_arg[3];
  258. ret = zynqmp_pm_query_data(qdata, pm_api_ret);
  259. if (ret)
  260. break;
  261. switch (qdata.qid) {
  262. case PM_QID_CLOCK_GET_NAME:
  263. sprintf(debugfs_buf, "Clock name = %s\n",
  264. (char *)pm_api_ret);
  265. break;
  266. case PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS:
  267. sprintf(debugfs_buf, "Multiplier = %d, Divider = %d\n",
  268. pm_api_ret[1], pm_api_ret[2]);
  269. break;
  270. default:
  271. sprintf(debugfs_buf,
  272. "data[0] = 0x%08x\ndata[1] = 0x%08x\n data[2] = 0x%08x\ndata[3] = 0x%08x\n",
  273. pm_api_ret[0], pm_api_ret[1],
  274. pm_api_ret[2], pm_api_ret[3]);
  275. }
  276. break;
  277. default:
  278. sprintf(debugfs_buf, "Unsupported PM-API request\n");
  279. ret = -EINVAL;
  280. }
  281. return ret;
  282. }
  283. /**
  284. * zynqmp_pm_debugfs_api_write() - debugfs write function
  285. * @file: User file
  286. * @ptr: User entered PM-API string
  287. * @len: Length of the userspace buffer
  288. * @off: Offset within the file
  289. *
  290. * Used for triggering pm api functions by writing
  291. * echo <pm_api_id> > /sys/kernel/debug/zynqmp_pm/power or
  292. * echo <pm_api_name> > /sys/kernel/debug/zynqmp_pm/power
  293. *
  294. * Return: Number of bytes copied if PM-API request succeeds,
  295. * the corresponding error code otherwise
  296. */
  297. static ssize_t zynqmp_pm_debugfs_api_write(struct file *file,
  298. const char __user *ptr, size_t len,
  299. loff_t *off)
  300. {
  301. char *kern_buff, *tmp_buff;
  302. char *pm_api_req;
  303. u32 pm_id = 0;
  304. u64 pm_api_arg[5] = {0, 0, 0, 0, 0};
  305. /* Return values from PM APIs calls */
  306. u32 pm_api_ret[4] = {0, 0, 0, 0};
  307. int ret;
  308. int i = 0;
  309. strcpy(debugfs_buf, "");
  310. if (*off != 0 || len <= 1 || len > PAGE_SIZE - 1)
  311. return -EINVAL;
  312. kern_buff = memdup_user_nul(ptr, len);
  313. if (IS_ERR(kern_buff))
  314. return PTR_ERR(kern_buff);
  315. tmp_buff = kern_buff;
  316. /* Read the API name from a user request */
  317. pm_api_req = strsep(&kern_buff, " ");
  318. ret = get_pm_api_id(pm_api_req, &pm_id);
  319. if (ret < 0)
  320. goto err;
  321. /* Read node_id and arguments from the PM-API request */
  322. pm_api_req = strsep(&kern_buff, " ");
  323. while ((i < ARRAY_SIZE(pm_api_arg)) && pm_api_req) {
  324. pm_api_arg[i++] = zynqmp_pm_argument_value(pm_api_req);
  325. pm_api_req = strsep(&kern_buff, " ");
  326. }
  327. ret = process_api_request(pm_id, pm_api_arg, pm_api_ret);
  328. err:
  329. kfree(tmp_buff);
  330. if (ret)
  331. return ret;
  332. return len;
  333. }
  334. /**
  335. * zynqmp_pm_debugfs_api_read() - debugfs read function
  336. * @file: User file
  337. * @ptr: Requested pm_api_version string
  338. * @len: Length of the userspace buffer
  339. * @off: Offset within the file
  340. *
  341. * Return: Length of the version string on success
  342. * else error code
  343. */
  344. static ssize_t zynqmp_pm_debugfs_api_read(struct file *file, char __user *ptr,
  345. size_t len, loff_t *off)
  346. {
  347. return simple_read_from_buffer(ptr, len, off, debugfs_buf,
  348. strlen(debugfs_buf));
  349. }
  350. /* Setup debugfs fops */
  351. static const struct file_operations fops_zynqmp_pm_dbgfs = {
  352. .owner = THIS_MODULE,
  353. .write = zynqmp_pm_debugfs_api_write,
  354. .read = zynqmp_pm_debugfs_api_read,
  355. };
  356. /**
  357. * zynqmp_pm_api_debugfs_init - Initialize debugfs interface
  358. *
  359. * Return: None
  360. */
  361. void zynqmp_pm_api_debugfs_init(void)
  362. {
  363. /* Initialize debugfs interface */
  364. firmware_debugfs_root = debugfs_create_dir("zynqmp-firmware", NULL);
  365. debugfs_create_file("pm", 0660, firmware_debugfs_root, NULL,
  366. &fops_zynqmp_pm_dbgfs);
  367. }
  368. /**
  369. * zynqmp_pm_api_debugfs_exit - Remove debugfs interface
  370. *
  371. * Return: None
  372. */
  373. void zynqmp_pm_api_debugfs_exit(void)
  374. {
  375. debugfs_remove_recursive(firmware_debugfs_root);
  376. }