zynqmp-debug.c 5.6 KB

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