qcom_stats.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2025, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/device.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/soc/qcom/qcom_aoss.h>
  15. #include <linux/soc/qcom/smem.h>
  16. #include <clocksource/arm_arch_timer.h>
  17. #define RPM_DYNAMIC_ADDR 0x14
  18. #define RPM_DYNAMIC_ADDR_MASK 0xFFFF
  19. #define STAT_TYPE_OFFSET 0x0
  20. #define COUNT_OFFSET 0x4
  21. #define LAST_ENTERED_AT_OFFSET 0x8
  22. #define LAST_EXITED_AT_OFFSET 0x10
  23. #define ACCUMULATED_OFFSET 0x18
  24. #define CLIENT_VOTES_OFFSET 0x20
  25. #define DDR_STATS_MAGIC_KEY 0xA1157A75
  26. #define DDR_STATS_MAX_NUM_MODES 20
  27. #define DDR_STATS_MAGIC_KEY_ADDR 0x0
  28. #define DDR_STATS_NUM_MODES_ADDR 0x4
  29. #define DDR_STATS_ENTRY_START_ADDR 0x8
  30. #define DDR_STATS_CP_IDX(data) FIELD_GET(GENMASK(4, 0), data)
  31. #define DDR_STATS_LPM_NAME(data) FIELD_GET(GENMASK(7, 0), data)
  32. #define DDR_STATS_TYPE(data) FIELD_GET(GENMASK(15, 8), data)
  33. #define DDR_STATS_FREQ(data) FIELD_GET(GENMASK(31, 16), data)
  34. static struct qmp *qcom_stats_qmp;
  35. struct subsystem_data {
  36. const char *name;
  37. u32 smem_item;
  38. u32 pid;
  39. };
  40. static const struct subsystem_data subsystems[] = {
  41. { "modem", 605, 1 },
  42. { "wpss", 605, 13 },
  43. { "adsp", 606, 2 },
  44. { "cdsp", 607, 5 },
  45. { "cdsp1", 607, 12 },
  46. { "gpdsp0", 607, 17 },
  47. { "gpdsp1", 607, 18 },
  48. { "slpi", 608, 3 },
  49. { "gpu", 609, 0 },
  50. { "display", 610, 0 },
  51. { "adsp_island", 613, 2 },
  52. { "slpi_island", 613, 3 },
  53. { "apss", 631, QCOM_SMEM_HOST_ANY },
  54. };
  55. struct stats_config {
  56. size_t stats_offset;
  57. size_t ddr_stats_offset;
  58. size_t num_records;
  59. bool appended_stats_avail;
  60. bool dynamic_offset;
  61. bool subsystem_stats_in_smem;
  62. };
  63. struct ddr_stats_entry {
  64. u32 name;
  65. u32 count;
  66. u64 duration;
  67. };
  68. struct stats_data {
  69. bool appended_stats_avail;
  70. void __iomem *base;
  71. };
  72. struct sleep_stats {
  73. u32 stat_type;
  74. u32 count;
  75. u64 last_entered_at;
  76. u64 last_exited_at;
  77. u64 accumulated;
  78. };
  79. struct appended_stats {
  80. u32 client_votes;
  81. u32 reserved[3];
  82. };
  83. static void qcom_print_stats(struct seq_file *s, const struct sleep_stats *stat)
  84. {
  85. u64 accumulated = stat->accumulated;
  86. /*
  87. * If a subsystem is in sleep when reading the sleep stats adjust
  88. * the accumulated sleep duration to show actual sleep time.
  89. */
  90. if (stat->last_entered_at > stat->last_exited_at)
  91. accumulated += arch_timer_read_counter() - stat->last_entered_at;
  92. seq_printf(s, "Count: %u\n", stat->count);
  93. seq_printf(s, "Last Entered At: %llu\n", stat->last_entered_at);
  94. seq_printf(s, "Last Exited At: %llu\n", stat->last_exited_at);
  95. seq_printf(s, "Accumulated Duration: %llu\n", accumulated);
  96. }
  97. static int qcom_subsystem_sleep_stats_show(struct seq_file *s, void *unused)
  98. {
  99. struct subsystem_data *subsystem = s->private;
  100. struct sleep_stats *stat;
  101. /* Items are allocated lazily, so lookup pointer each time */
  102. stat = qcom_smem_get(subsystem->pid, subsystem->smem_item, NULL);
  103. if (IS_ERR(stat))
  104. return 0;
  105. qcom_print_stats(s, stat);
  106. return 0;
  107. }
  108. static int qcom_soc_sleep_stats_show(struct seq_file *s, void *unused)
  109. {
  110. struct stats_data *d = s->private;
  111. void __iomem *reg = d->base;
  112. struct sleep_stats stat;
  113. memcpy_fromio(&stat, reg, sizeof(stat));
  114. qcom_print_stats(s, &stat);
  115. if (d->appended_stats_avail) {
  116. struct appended_stats votes;
  117. memcpy_fromio(&votes, reg + CLIENT_VOTES_OFFSET, sizeof(votes));
  118. seq_printf(s, "Client Votes: %#x\n", votes.client_votes);
  119. }
  120. return 0;
  121. }
  122. static void qcom_ddr_stats_print(struct seq_file *s, struct ddr_stats_entry *data)
  123. {
  124. u32 cp_idx;
  125. /*
  126. * DDR statistic have two different types of details encoded.
  127. * (1) DDR LPM Stats
  128. * (2) DDR Frequency Stats
  129. *
  130. * The name field have details like which type of DDR stat (bits 8:15)
  131. * along with other details as explained below
  132. *
  133. * In case of DDR LPM stat, name field will be encoded as,
  134. * Bits - Meaning
  135. * 0:7 - DDR LPM name, can be of 0xd4, 0xd3, 0x11 and 0xd0.
  136. * 8:15 - 0x0 (indicates its a LPM stat)
  137. * 16:31 - Unused
  138. *
  139. * In case of DDR FREQ stats, name field will be encoded as,
  140. * Bits - Meaning
  141. * 0:4 - DDR Clock plan index (CP IDX)
  142. * 5:7 - Unused
  143. * 8:15 - 0x1 (indicates its Freq stat)
  144. * 16:31 - Frequency value in Mhz
  145. */
  146. switch (DDR_STATS_TYPE(data->name)) {
  147. case 0:
  148. seq_printf(s, "DDR LPM Stat Name:0x%lx\tcount:%u\tDuration (ticks):%llu\n",
  149. DDR_STATS_LPM_NAME(data->name), data->count, data->duration);
  150. break;
  151. case 1:
  152. if (!data->count || !DDR_STATS_FREQ(data->name))
  153. return;
  154. cp_idx = DDR_STATS_CP_IDX(data->name);
  155. seq_printf(s, "DDR Freq %luMhz:\tCP IDX:%u\tcount:%u\tDuration (ticks):%llu\n",
  156. DDR_STATS_FREQ(data->name), cp_idx, data->count, data->duration);
  157. break;
  158. }
  159. }
  160. static int qcom_ddr_stats_show(struct seq_file *s, void *d)
  161. {
  162. struct ddr_stats_entry data[DDR_STATS_MAX_NUM_MODES];
  163. void __iomem *reg = (void __iomem *)s->private;
  164. u32 entry_count;
  165. int i, ret;
  166. entry_count = readl_relaxed(reg + DDR_STATS_NUM_MODES_ADDR);
  167. if (entry_count > DDR_STATS_MAX_NUM_MODES)
  168. return -EINVAL;
  169. if (qcom_stats_qmp) {
  170. /*
  171. * Recent SoCs (SM8450 onwards) do not have duration field
  172. * populated from boot up onwards for both DDR LPM Stats
  173. * and DDR Frequency Stats.
  174. *
  175. * Send QMP message to Always on processor which will
  176. * populate duration field into MSG RAM area.
  177. *
  178. * Sent every time to read latest data.
  179. */
  180. ret = qmp_send(qcom_stats_qmp, "{class: ddr, action: freqsync}");
  181. if (ret)
  182. return ret;
  183. }
  184. reg += DDR_STATS_ENTRY_START_ADDR;
  185. memcpy_fromio(data, reg, sizeof(struct ddr_stats_entry) * entry_count);
  186. for (i = 0; i < entry_count; i++)
  187. qcom_ddr_stats_print(s, &data[i]);
  188. return 0;
  189. }
  190. DEFINE_SHOW_ATTRIBUTE(qcom_soc_sleep_stats);
  191. DEFINE_SHOW_ATTRIBUTE(qcom_subsystem_sleep_stats);
  192. DEFINE_SHOW_ATTRIBUTE(qcom_ddr_stats);
  193. static void qcom_create_ddr_stat_files(struct dentry *root, void __iomem *reg,
  194. const struct stats_config *config)
  195. {
  196. u32 key;
  197. if (!config->ddr_stats_offset)
  198. return;
  199. key = readl_relaxed(reg + config->ddr_stats_offset + DDR_STATS_MAGIC_KEY_ADDR);
  200. if (key == DDR_STATS_MAGIC_KEY)
  201. debugfs_create_file("ddr_stats", 0400, root,
  202. (__force void *)reg + config->ddr_stats_offset,
  203. &qcom_ddr_stats_fops);
  204. }
  205. static void qcom_create_soc_sleep_stat_files(struct dentry *root, void __iomem *reg,
  206. struct stats_data *d,
  207. const struct stats_config *config)
  208. {
  209. char stat_type[sizeof(u32) + 1] = {0};
  210. size_t stats_offset = config->stats_offset;
  211. u32 offset = 0, type;
  212. int i, j;
  213. /*
  214. * On RPM targets, stats offset location is dynamic and changes from target
  215. * to target and sometimes from build to build for same target.
  216. *
  217. * In such cases the dynamic address is present at 0x14 offset from base
  218. * address in devicetree. The last 16bits indicates the stats_offset.
  219. */
  220. if (config->dynamic_offset) {
  221. stats_offset = readl(reg + RPM_DYNAMIC_ADDR);
  222. stats_offset &= RPM_DYNAMIC_ADDR_MASK;
  223. }
  224. for (i = 0; i < config->num_records; i++) {
  225. d[i].base = reg + offset + stats_offset;
  226. /*
  227. * Read the low power mode name and create debugfs file for it.
  228. * The names read could be of below,
  229. * (may change depending on low power mode supported).
  230. * For rpmh-sleep-stats: "aosd", "cxsd" and "ddr".
  231. * For rpm-sleep-stats: "vmin" and "vlow".
  232. */
  233. type = readl(d[i].base);
  234. for (j = 0; j < sizeof(u32); j++) {
  235. stat_type[j] = type & 0xff;
  236. type = type >> 8;
  237. }
  238. strim(stat_type);
  239. debugfs_create_file(stat_type, 0400, root, &d[i],
  240. &qcom_soc_sleep_stats_fops);
  241. offset += sizeof(struct sleep_stats);
  242. if (d[i].appended_stats_avail)
  243. offset += sizeof(struct appended_stats);
  244. }
  245. }
  246. static void qcom_create_subsystem_stat_files(struct dentry *root,
  247. const struct stats_config *config)
  248. {
  249. int i;
  250. if (!config->subsystem_stats_in_smem)
  251. return;
  252. for (i = 0; i < ARRAY_SIZE(subsystems); i++)
  253. debugfs_create_file(subsystems[i].name, 0400, root, (void *)&subsystems[i],
  254. &qcom_subsystem_sleep_stats_fops);
  255. }
  256. static int qcom_stats_probe(struct platform_device *pdev)
  257. {
  258. void __iomem *reg;
  259. struct dentry *root;
  260. const struct stats_config *config;
  261. struct stats_data *d;
  262. int i;
  263. config = device_get_match_data(&pdev->dev);
  264. if (!config)
  265. return -ENODEV;
  266. reg = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  267. if (IS_ERR(reg))
  268. return -ENOMEM;
  269. d = devm_kcalloc(&pdev->dev, config->num_records,
  270. sizeof(*d), GFP_KERNEL);
  271. if (!d)
  272. return -ENOMEM;
  273. for (i = 0; i < config->num_records; i++)
  274. d[i].appended_stats_avail = config->appended_stats_avail;
  275. /*
  276. * QMP is used for DDR stats syncing to MSG RAM for recent SoCs (SM8450 onwards).
  277. * The prior SoCs do not need QMP handle as the required stats are already present
  278. * in MSG RAM, provided the DDR_STATS_MAGIC_KEY matches.
  279. */
  280. qcom_stats_qmp = qmp_get(&pdev->dev);
  281. if (IS_ERR(qcom_stats_qmp)) {
  282. /* We ignore error if QMP is not defined/needed */
  283. if (!of_property_present(pdev->dev.of_node, "qcom,qmp"))
  284. qcom_stats_qmp = NULL;
  285. else if (PTR_ERR(qcom_stats_qmp) == -EPROBE_DEFER)
  286. return -EPROBE_DEFER;
  287. else
  288. return PTR_ERR(qcom_stats_qmp);
  289. }
  290. root = debugfs_create_dir("qcom_stats", NULL);
  291. qcom_create_subsystem_stat_files(root, config);
  292. qcom_create_soc_sleep_stat_files(root, reg, d, config);
  293. qcom_create_ddr_stat_files(root, reg, config);
  294. platform_set_drvdata(pdev, root);
  295. device_set_pm_not_required(&pdev->dev);
  296. return 0;
  297. }
  298. static void qcom_stats_remove(struct platform_device *pdev)
  299. {
  300. struct dentry *root = platform_get_drvdata(pdev);
  301. debugfs_remove_recursive(root);
  302. }
  303. static const struct stats_config rpm_data = {
  304. .stats_offset = 0,
  305. .num_records = 2,
  306. .appended_stats_avail = true,
  307. .dynamic_offset = true,
  308. .subsystem_stats_in_smem = false,
  309. };
  310. /* Older RPM firmwares have the stats at a fixed offset instead */
  311. static const struct stats_config rpm_data_dba0 = {
  312. .stats_offset = 0xdba0,
  313. .num_records = 2,
  314. .appended_stats_avail = true,
  315. .dynamic_offset = false,
  316. .subsystem_stats_in_smem = false,
  317. };
  318. static const struct stats_config rpmh_data_sdm845 = {
  319. .stats_offset = 0x48,
  320. .num_records = 2,
  321. .appended_stats_avail = false,
  322. .dynamic_offset = false,
  323. .subsystem_stats_in_smem = true,
  324. };
  325. static const struct stats_config rpmh_data = {
  326. .stats_offset = 0x48,
  327. .ddr_stats_offset = 0xb8,
  328. .num_records = 3,
  329. .appended_stats_avail = false,
  330. .dynamic_offset = false,
  331. .subsystem_stats_in_smem = true,
  332. };
  333. static const struct of_device_id qcom_stats_table[] = {
  334. { .compatible = "qcom,apq8084-rpm-stats", .data = &rpm_data_dba0 },
  335. { .compatible = "qcom,msm8226-rpm-stats", .data = &rpm_data_dba0 },
  336. { .compatible = "qcom,msm8916-rpm-stats", .data = &rpm_data_dba0 },
  337. { .compatible = "qcom,msm8974-rpm-stats", .data = &rpm_data_dba0 },
  338. { .compatible = "qcom,rpm-stats", .data = &rpm_data },
  339. { .compatible = "qcom,rpmh-stats", .data = &rpmh_data },
  340. { .compatible = "qcom,sdm845-rpmh-stats", .data = &rpmh_data_sdm845 },
  341. { }
  342. };
  343. MODULE_DEVICE_TABLE(of, qcom_stats_table);
  344. static struct platform_driver qcom_stats = {
  345. .probe = qcom_stats_probe,
  346. .remove = qcom_stats_remove,
  347. .driver = {
  348. .name = "qcom_stats",
  349. .of_match_table = qcom_stats_table,
  350. },
  351. };
  352. static int __init qcom_stats_init(void)
  353. {
  354. return platform_driver_register(&qcom_stats);
  355. }
  356. late_initcall(qcom_stats_init);
  357. static void __exit qcom_stats_exit(void)
  358. {
  359. platform_driver_unregister(&qcom_stats);
  360. }
  361. module_exit(qcom_stats_exit)
  362. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. (QTI) Stats driver");
  363. MODULE_LICENSE("GPL v2");