qcom_stats.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/debugfs.h>
  6. #include <linux/device.h>
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/soc/qcom/smem.h>
  13. #include <clocksource/arm_arch_timer.h>
  14. #define RPM_DYNAMIC_ADDR 0x14
  15. #define RPM_DYNAMIC_ADDR_MASK 0xFFFF
  16. #define STAT_TYPE_OFFSET 0x0
  17. #define COUNT_OFFSET 0x4
  18. #define LAST_ENTERED_AT_OFFSET 0x8
  19. #define LAST_EXITED_AT_OFFSET 0x10
  20. #define ACCUMULATED_OFFSET 0x18
  21. #define CLIENT_VOTES_OFFSET 0x20
  22. struct subsystem_data {
  23. const char *name;
  24. u32 smem_item;
  25. u32 pid;
  26. };
  27. static const struct subsystem_data subsystems[] = {
  28. { "modem", 605, 1 },
  29. { "wpss", 605, 13 },
  30. { "adsp", 606, 2 },
  31. { "cdsp", 607, 5 },
  32. { "cdsp1", 607, 12 },
  33. { "gpdsp0", 607, 17 },
  34. { "gpdsp1", 607, 18 },
  35. { "slpi", 608, 3 },
  36. { "gpu", 609, 0 },
  37. { "display", 610, 0 },
  38. { "adsp_island", 613, 2 },
  39. { "slpi_island", 613, 3 },
  40. { "apss", 631, QCOM_SMEM_HOST_ANY },
  41. };
  42. struct stats_config {
  43. size_t stats_offset;
  44. size_t num_records;
  45. bool appended_stats_avail;
  46. bool dynamic_offset;
  47. bool subsystem_stats_in_smem;
  48. };
  49. struct stats_data {
  50. bool appended_stats_avail;
  51. void __iomem *base;
  52. };
  53. struct sleep_stats {
  54. u32 stat_type;
  55. u32 count;
  56. u64 last_entered_at;
  57. u64 last_exited_at;
  58. u64 accumulated;
  59. };
  60. struct appended_stats {
  61. u32 client_votes;
  62. u32 reserved[3];
  63. };
  64. static void qcom_print_stats(struct seq_file *s, const struct sleep_stats *stat)
  65. {
  66. u64 accumulated = stat->accumulated;
  67. /*
  68. * If a subsystem is in sleep when reading the sleep stats adjust
  69. * the accumulated sleep duration to show actual sleep time.
  70. */
  71. if (stat->last_entered_at > stat->last_exited_at)
  72. accumulated += arch_timer_read_counter() - stat->last_entered_at;
  73. seq_printf(s, "Count: %u\n", stat->count);
  74. seq_printf(s, "Last Entered At: %llu\n", stat->last_entered_at);
  75. seq_printf(s, "Last Exited At: %llu\n", stat->last_exited_at);
  76. seq_printf(s, "Accumulated Duration: %llu\n", accumulated);
  77. }
  78. static int qcom_subsystem_sleep_stats_show(struct seq_file *s, void *unused)
  79. {
  80. struct subsystem_data *subsystem = s->private;
  81. struct sleep_stats *stat;
  82. /* Items are allocated lazily, so lookup pointer each time */
  83. stat = qcom_smem_get(subsystem->pid, subsystem->smem_item, NULL);
  84. if (IS_ERR(stat))
  85. return 0;
  86. qcom_print_stats(s, stat);
  87. return 0;
  88. }
  89. static int qcom_soc_sleep_stats_show(struct seq_file *s, void *unused)
  90. {
  91. struct stats_data *d = s->private;
  92. void __iomem *reg = d->base;
  93. struct sleep_stats stat;
  94. memcpy_fromio(&stat, reg, sizeof(stat));
  95. qcom_print_stats(s, &stat);
  96. if (d->appended_stats_avail) {
  97. struct appended_stats votes;
  98. memcpy_fromio(&votes, reg + CLIENT_VOTES_OFFSET, sizeof(votes));
  99. seq_printf(s, "Client Votes: %#x\n", votes.client_votes);
  100. }
  101. return 0;
  102. }
  103. DEFINE_SHOW_ATTRIBUTE(qcom_soc_sleep_stats);
  104. DEFINE_SHOW_ATTRIBUTE(qcom_subsystem_sleep_stats);
  105. static void qcom_create_soc_sleep_stat_files(struct dentry *root, void __iomem *reg,
  106. struct stats_data *d,
  107. const struct stats_config *config)
  108. {
  109. char stat_type[sizeof(u32) + 1] = {0};
  110. size_t stats_offset = config->stats_offset;
  111. u32 offset = 0, type;
  112. int i, j;
  113. /*
  114. * On RPM targets, stats offset location is dynamic and changes from target
  115. * to target and sometimes from build to build for same target.
  116. *
  117. * In such cases the dynamic address is present at 0x14 offset from base
  118. * address in devicetree. The last 16bits indicates the stats_offset.
  119. */
  120. if (config->dynamic_offset) {
  121. stats_offset = readl(reg + RPM_DYNAMIC_ADDR);
  122. stats_offset &= RPM_DYNAMIC_ADDR_MASK;
  123. }
  124. for (i = 0; i < config->num_records; i++) {
  125. d[i].base = reg + offset + stats_offset;
  126. /*
  127. * Read the low power mode name and create debugfs file for it.
  128. * The names read could be of below,
  129. * (may change depending on low power mode supported).
  130. * For rpmh-sleep-stats: "aosd", "cxsd" and "ddr".
  131. * For rpm-sleep-stats: "vmin" and "vlow".
  132. */
  133. type = readl(d[i].base);
  134. for (j = 0; j < sizeof(u32); j++) {
  135. stat_type[j] = type & 0xff;
  136. type = type >> 8;
  137. }
  138. strim(stat_type);
  139. debugfs_create_file(stat_type, 0400, root, &d[i],
  140. &qcom_soc_sleep_stats_fops);
  141. offset += sizeof(struct sleep_stats);
  142. if (d[i].appended_stats_avail)
  143. offset += sizeof(struct appended_stats);
  144. }
  145. }
  146. static void qcom_create_subsystem_stat_files(struct dentry *root,
  147. const struct stats_config *config)
  148. {
  149. int i;
  150. if (!config->subsystem_stats_in_smem)
  151. return;
  152. for (i = 0; i < ARRAY_SIZE(subsystems); i++)
  153. debugfs_create_file(subsystems[i].name, 0400, root, (void *)&subsystems[i],
  154. &qcom_subsystem_sleep_stats_fops);
  155. }
  156. static int qcom_stats_probe(struct platform_device *pdev)
  157. {
  158. void __iomem *reg;
  159. struct dentry *root;
  160. const struct stats_config *config;
  161. struct stats_data *d;
  162. int i;
  163. config = device_get_match_data(&pdev->dev);
  164. if (!config)
  165. return -ENODEV;
  166. reg = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  167. if (IS_ERR(reg))
  168. return -ENOMEM;
  169. d = devm_kcalloc(&pdev->dev, config->num_records,
  170. sizeof(*d), GFP_KERNEL);
  171. if (!d)
  172. return -ENOMEM;
  173. for (i = 0; i < config->num_records; i++)
  174. d[i].appended_stats_avail = config->appended_stats_avail;
  175. root = debugfs_create_dir("qcom_stats", NULL);
  176. qcom_create_subsystem_stat_files(root, config);
  177. qcom_create_soc_sleep_stat_files(root, reg, d, config);
  178. platform_set_drvdata(pdev, root);
  179. device_set_pm_not_required(&pdev->dev);
  180. return 0;
  181. }
  182. static void qcom_stats_remove(struct platform_device *pdev)
  183. {
  184. struct dentry *root = platform_get_drvdata(pdev);
  185. debugfs_remove_recursive(root);
  186. }
  187. static const struct stats_config rpm_data = {
  188. .stats_offset = 0,
  189. .num_records = 2,
  190. .appended_stats_avail = true,
  191. .dynamic_offset = true,
  192. .subsystem_stats_in_smem = false,
  193. };
  194. /* Older RPM firmwares have the stats at a fixed offset instead */
  195. static const struct stats_config rpm_data_dba0 = {
  196. .stats_offset = 0xdba0,
  197. .num_records = 2,
  198. .appended_stats_avail = true,
  199. .dynamic_offset = false,
  200. .subsystem_stats_in_smem = false,
  201. };
  202. static const struct stats_config rpmh_data_sdm845 = {
  203. .stats_offset = 0x48,
  204. .num_records = 2,
  205. .appended_stats_avail = false,
  206. .dynamic_offset = false,
  207. .subsystem_stats_in_smem = true,
  208. };
  209. static const struct stats_config rpmh_data = {
  210. .stats_offset = 0x48,
  211. .num_records = 3,
  212. .appended_stats_avail = false,
  213. .dynamic_offset = false,
  214. .subsystem_stats_in_smem = true,
  215. };
  216. static const struct of_device_id qcom_stats_table[] = {
  217. { .compatible = "qcom,apq8084-rpm-stats", .data = &rpm_data_dba0 },
  218. { .compatible = "qcom,msm8226-rpm-stats", .data = &rpm_data_dba0 },
  219. { .compatible = "qcom,msm8916-rpm-stats", .data = &rpm_data_dba0 },
  220. { .compatible = "qcom,msm8974-rpm-stats", .data = &rpm_data_dba0 },
  221. { .compatible = "qcom,rpm-stats", .data = &rpm_data },
  222. { .compatible = "qcom,rpmh-stats", .data = &rpmh_data },
  223. { .compatible = "qcom,sdm845-rpmh-stats", .data = &rpmh_data_sdm845 },
  224. { }
  225. };
  226. MODULE_DEVICE_TABLE(of, qcom_stats_table);
  227. static struct platform_driver qcom_stats = {
  228. .probe = qcom_stats_probe,
  229. .remove_new = qcom_stats_remove,
  230. .driver = {
  231. .name = "qcom_stats",
  232. .of_match_table = qcom_stats_table,
  233. },
  234. };
  235. static int __init qcom_stats_init(void)
  236. {
  237. return platform_driver_register(&qcom_stats);
  238. }
  239. late_initcall(qcom_stats_init);
  240. static void __exit qcom_stats_exit(void)
  241. {
  242. platform_driver_unregister(&qcom_stats);
  243. }
  244. module_exit(qcom_stats_exit)
  245. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. (QTI) Stats driver");
  246. MODULE_LICENSE("GPL v2");