thermal_config.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "%s:%s " fmt, KBUILD_MODNAME, __func__
  6. #include <linux/debugfs.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include "../thermal_core.h"
  14. static struct dentry *thermal_debugfs_parent;
  15. static struct dentry *thermal_debugfs_config;
  16. #define UINT_MAX_CHARACTER 11
  17. static char tzone_sensor_name[THERMAL_NAME_LENGTH] = "";
  18. int buffer_overflow_check(char *buf, int offset, int buf_size)
  19. {
  20. if ((buf == NULL) || (offset >= buf_size) || (strlen(buf) >= buf_size))
  21. return -EINVAL;
  22. return 0;
  23. }
  24. static int fetch_and_populate_trip_data(char *buf, struct thermal_zone_device *tz,
  25. int idx, int offset, size_t size, bool is_hyst)
  26. {
  27. int ret = 0, temp;
  28. if (!is_hyst)
  29. ret = tz->ops->get_trip_temp(tz, idx, &temp);
  30. else
  31. ret = tz->ops->get_trip_hyst(tz, idx, &temp);
  32. if (ret)
  33. return ret;
  34. offset += scnprintf(buf + offset, size - offset, "%d ", temp);
  35. return offset;
  36. }
  37. static int fetch_and_populate_trips(char *config_buf, struct thermal_zone_device *tz,
  38. int offset)
  39. {
  40. size_t buf_size = 0;
  41. int i = 0, ret = 0;
  42. int buf1_offset = 0, buf2_offset = 0;
  43. char *buf_temp = NULL, *buf_hyst = NULL;
  44. buf_size = tz->num_trips * UINT_MAX_CHARACTER;
  45. buf_temp = kzalloc(buf_size, GFP_KERNEL);
  46. buf_hyst = kzalloc(buf_size, GFP_KERNEL);
  47. if (!buf_hyst || !buf_temp) {
  48. kfree(buf_temp);
  49. kfree(buf_hyst);
  50. return -ENOMEM;
  51. }
  52. for (i = 0; i < tz->num_trips; i++) {
  53. ret = fetch_and_populate_trip_data(buf_temp, tz, i, buf1_offset,
  54. buf_size, false);
  55. if (ret < 0)
  56. goto config_exit;
  57. buf1_offset = ret;
  58. if (!tz->ops->get_trip_hyst)
  59. continue;
  60. ret = fetch_and_populate_trip_data(buf_hyst, tz, i, buf2_offset,
  61. buf_size, true);
  62. if (ret < 0)
  63. goto config_exit;
  64. buf2_offset = ret;
  65. }
  66. ret = buffer_overflow_check(buf_temp, offset, PAGE_SIZE-offset);
  67. if (ret)
  68. goto config_exit;
  69. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset,
  70. "%*s%s\n", -15, "set_temp", buf_temp);
  71. if (buf2_offset) {
  72. ret = buffer_overflow_check(buf_hyst, offset, PAGE_SIZE-offset);
  73. if (ret)
  74. goto config_exit;
  75. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset,
  76. "%*s%s\n", -15, "clr_temp", buf_hyst);
  77. }
  78. config_exit:
  79. kfree(buf_temp);
  80. kfree(buf_hyst);
  81. return (ret < 0) ? ret : offset;
  82. }
  83. static int fetch_and_populate_cdevs(char *config_buf, struct thermal_zone_device *tz,
  84. int offset)
  85. {
  86. int ret = 0, i = 0;
  87. int buf_size = 0, buf_offset = 0, buf1_offset = 0, buf2_offset = 0;
  88. char *buf_cdev = NULL, *buf_cdev_upper = NULL, *buf_cdev_lower = NULL;
  89. struct thermal_instance *instance;
  90. mutex_lock(&tz->lock);
  91. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  92. if (instance->cdev)
  93. buf_size++;
  94. }
  95. if (!buf_size) {
  96. mutex_unlock(&tz->lock);
  97. return offset;
  98. }
  99. buf_size = (buf_size + tz->num_trips) * THERMAL_NAME_LENGTH;
  100. buf_cdev = kzalloc(buf_size, GFP_KERNEL);
  101. buf_cdev_upper = kzalloc(buf_size, GFP_KERNEL);
  102. buf_cdev_lower = kzalloc(buf_size, GFP_KERNEL);
  103. if (!buf_cdev || !buf_cdev_upper || !buf_cdev_lower) {
  104. mutex_unlock(&tz->lock);
  105. kfree(buf_cdev);
  106. kfree(buf_cdev_upper);
  107. kfree(buf_cdev_lower);
  108. return -ENOMEM;
  109. }
  110. for (i = 0; i < tz->num_trips; i++) {
  111. bool first_entry = true;
  112. bool no_cdevs = true;
  113. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  114. if (!instance->cdev || instance->trip != i)
  115. continue;
  116. no_cdevs = false;
  117. if (first_entry) {
  118. first_entry = false;
  119. buf_offset += scnprintf(
  120. buf_cdev + buf_offset,
  121. buf_size - buf_offset,
  122. " %s", instance->cdev->type);
  123. buf1_offset += scnprintf(
  124. buf_cdev_upper + buf1_offset,
  125. buf_size - buf1_offset,
  126. " %d", instance->upper);
  127. buf2_offset += scnprintf(
  128. buf_cdev_lower + buf2_offset,
  129. buf_size - buf2_offset,
  130. " %d", instance->lower);
  131. } else {
  132. buf_offset += scnprintf(
  133. buf_cdev + buf_offset,
  134. buf_size - buf_offset,
  135. "+%s", instance->cdev->type);
  136. buf1_offset += scnprintf(
  137. buf_cdev_upper + buf1_offset,
  138. buf_size - buf1_offset,
  139. "+%d", instance->upper);
  140. buf2_offset += scnprintf(
  141. buf_cdev_lower + buf2_offset,
  142. buf_size - buf2_offset,
  143. "+%d", instance->lower);
  144. }
  145. }
  146. if (no_cdevs) {
  147. buf_offset += scnprintf(
  148. buf_cdev + buf_offset,
  149. buf_size - buf_offset,
  150. " %s", "-");
  151. buf1_offset += scnprintf(
  152. buf_cdev_upper + buf1_offset,
  153. buf_size - buf1_offset,
  154. " %s", "-");
  155. buf2_offset += scnprintf(
  156. buf_cdev_lower + buf2_offset,
  157. buf_size - buf2_offset,
  158. " %s", "-");
  159. }
  160. }
  161. mutex_unlock(&tz->lock);
  162. ret = buffer_overflow_check(buf_cdev, offset, PAGE_SIZE-offset);
  163. if (ret)
  164. goto config_exit;
  165. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset,
  166. "%*s%s\n", -14, "device", buf_cdev);
  167. ret = buffer_overflow_check(buf_cdev_upper, offset, PAGE_SIZE-offset);
  168. if (ret)
  169. goto config_exit;
  170. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset,
  171. "%*s%s\n", -14, "upper_limit", buf_cdev_upper);
  172. ret = buffer_overflow_check(buf_cdev_lower, offset, PAGE_SIZE-offset);
  173. if (ret)
  174. goto config_exit;
  175. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset,
  176. "%*s%s\n", -14, "lower_limit", buf_cdev_lower);
  177. config_exit:
  178. kfree(buf_cdev);
  179. kfree(buf_cdev_upper);
  180. kfree(buf_cdev_lower);
  181. return (ret < 0) ? ret : offset;
  182. }
  183. ssize_t thermal_dbgfs_config_read(struct file *file, char __user *buf,
  184. size_t count, loff_t *ppos)
  185. {
  186. struct thermal_zone_device *tz = NULL;
  187. int offset = 0, buf_count = 0, ret;
  188. char *config_buf = NULL;
  189. tz = thermal_zone_get_zone_by_name((const char *)tzone_sensor_name);
  190. if (IS_ERR(tz)) {
  191. ret = PTR_ERR(tz);
  192. pr_err("No thermal zone for sensor:%s. err:%d\n",
  193. tzone_sensor_name, ret);
  194. return ret;
  195. }
  196. config_buf = kzalloc(sizeof(char) * PAGE_SIZE, GFP_KERNEL);
  197. if (!config_buf)
  198. return -ENOMEM;
  199. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset, "%*s%s\n",
  200. -15, "sensor", tz->type);
  201. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset, "%*s%s\n",
  202. -15, "algo_type", tz->governor->name);
  203. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset, "%*s%s\n",
  204. -15, "mode",
  205. (tz->mode == THERMAL_DEVICE_DISABLED)?"disabled":"enabled");
  206. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset, "%*s%d\n",
  207. -15, "polling_delay",
  208. jiffies_to_msecs(tz->polling_delay_jiffies));
  209. offset += scnprintf(config_buf + offset, PAGE_SIZE - offset, "%*s%d\n",
  210. -15, "passive_delay",
  211. jiffies_to_msecs(tz->passive_delay_jiffies));
  212. if (!tz->num_trips || !tz->ops->get_trip_temp) {
  213. if (offset >= PAGE_SIZE) {
  214. pr_err("%s sensor config rule length is more than buffer size\n",
  215. tz->type);
  216. ret = -ENOMEM;
  217. goto config_exit;
  218. }
  219. config_buf[offset] = '\0';
  220. ret = simple_read_from_buffer(buf, count, ppos, config_buf, offset);
  221. kfree(config_buf);
  222. return ret;
  223. }
  224. ret = fetch_and_populate_trips(config_buf, tz, offset);
  225. if (ret < 0)
  226. goto config_exit;
  227. offset = ret;
  228. ret = fetch_and_populate_cdevs(config_buf, tz, offset);
  229. if (ret < 0)
  230. goto config_exit;
  231. offset = ret;
  232. if (offset >= PAGE_SIZE) {
  233. pr_err("%s sensor config rule length is more than buffer size\n",
  234. tz->type);
  235. ret = -ENOMEM;
  236. goto config_exit;
  237. }
  238. config_buf[offset] = '\0';
  239. buf_count = simple_read_from_buffer(buf, count, ppos, config_buf, offset);
  240. config_exit:
  241. kfree(config_buf);
  242. return (ret < 0) ? ret : buf_count;
  243. }
  244. static ssize_t thermal_dbgfs_config_write(struct file *file,
  245. const char __user *user_buf, size_t count, loff_t *ppos)
  246. {
  247. struct thermal_zone_device *tz = NULL;
  248. char sensor_name[THERMAL_NAME_LENGTH] = "";
  249. if (!count || (count > THERMAL_NAME_LENGTH))
  250. return -EINVAL;
  251. if (copy_from_user(sensor_name, user_buf, count))
  252. return -EFAULT;
  253. if (sscanf(sensor_name, "%19[^\n\t ]", tzone_sensor_name) != 1)
  254. return -EINVAL;
  255. tz = thermal_zone_get_zone_by_name((const char *)tzone_sensor_name);
  256. if (IS_ERR(tz)) {
  257. pr_err("No thermal zone for sensor:%s. err:%d\n",
  258. tzone_sensor_name, PTR_ERR(tz));
  259. return PTR_ERR(tz);
  260. }
  261. return count;
  262. }
  263. static const struct file_operations thermal_dbgfs_config_fops = {
  264. .write = thermal_dbgfs_config_write,
  265. .read = thermal_dbgfs_config_read,
  266. };
  267. static int thermal_config_init(void)
  268. {
  269. int ret = 0;
  270. thermal_debugfs_parent = debugfs_create_dir("thermal", NULL);
  271. if (IS_ERR_OR_NULL(thermal_debugfs_parent)) {
  272. ret = PTR_ERR(thermal_debugfs_parent);
  273. pr_err("Error creating thermal debugfs directory. err:%d\n",
  274. ret);
  275. return ret;
  276. }
  277. thermal_debugfs_config = debugfs_create_file("config", 0600,
  278. thermal_debugfs_parent, NULL,
  279. &thermal_dbgfs_config_fops);
  280. if (IS_ERR_OR_NULL(thermal_debugfs_config)) {
  281. ret = PTR_ERR(thermal_debugfs_config);
  282. pr_err("Error creating thermal config debugfs. err:%d\n",
  283. ret);
  284. return ret;
  285. }
  286. return ret;
  287. }
  288. static void thermal_config_exit(void)
  289. {
  290. debugfs_remove_recursive(thermal_debugfs_parent);
  291. }
  292. module_init(thermal_config_init);
  293. module_exit(thermal_config_exit);
  294. MODULE_DESCRIPTION("Thermal Zone config debug driver");
  295. MODULE_LICENSE("GPL");