1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2021, The Linux Foundation. All rights reserved.
6 #include <linux/debugfs.h>
7 #include <linux/device.h>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/seq_file.h>
14 #include <linux/soc/qcom/smem.h>
15 #include <clocksource/arm_arch_timer.h>
17 #define RPM_DYNAMIC_ADDR 0x14
18 #define RPM_DYNAMIC_ADDR_MASK 0xFFFF
20 #define STAT_TYPE_OFFSET 0x0
21 #define COUNT_OFFSET 0x4
22 #define LAST_ENTERED_AT_OFFSET 0x8
23 #define LAST_EXITED_AT_OFFSET 0x10
24 #define ACCUMULATED_OFFSET 0x18
25 #define CLIENT_VOTES_OFFSET 0x20
27 struct subsystem_data {
33 static const struct subsystem_data subsystems[] = {
39 { "gpdsp0", 607, 17 },
40 { "gpdsp1", 607, 18 },
43 { "display", 610, 0 },
44 { "adsp_island", 613, 2 },
45 { "slpi_island", 613, 3 },
46 { "apss", 631, QCOM_SMEM_HOST_ANY },
52 bool appended_stats_avail;
54 bool subsystem_stats_in_smem;
58 bool appended_stats_avail;
70 struct appended_stats {
75 static void qcom_print_stats(struct seq_file *s, const struct sleep_stats *stat)
77 u64 accumulated = stat->accumulated;
79 * If a subsystem is in sleep when reading the sleep stats adjust
80 * the accumulated sleep duration to show actual sleep time.
82 if (stat->last_entered_at > stat->last_exited_at)
83 accumulated += arch_timer_read_counter() - stat->last_entered_at;
85 seq_printf(s, "Count: %u\n", stat->count);
86 seq_printf(s, "Last Entered At: %llu\n", stat->last_entered_at);
87 seq_printf(s, "Last Exited At: %llu\n", stat->last_exited_at);
88 seq_printf(s, "Accumulated Duration: %llu\n", accumulated);
91 static int qcom_subsystem_sleep_stats_show(struct seq_file *s, void *unused)
93 struct subsystem_data *subsystem = s->private;
94 struct sleep_stats *stat;
96 /* Items are allocated lazily, so lookup pointer each time */
97 stat = qcom_smem_get(subsystem->pid, subsystem->smem_item, NULL);
101 qcom_print_stats(s, stat);
106 static int qcom_soc_sleep_stats_show(struct seq_file *s, void *unused)
108 struct stats_data *d = s->private;
109 void __iomem *reg = d->base;
110 struct sleep_stats stat;
112 memcpy_fromio(&stat, reg, sizeof(stat));
113 qcom_print_stats(s, &stat);
115 if (d->appended_stats_avail) {
116 struct appended_stats votes;
118 memcpy_fromio(&votes, reg + CLIENT_VOTES_OFFSET, sizeof(votes));
119 seq_printf(s, "Client Votes: %#x\n", votes.client_votes);
125 DEFINE_SHOW_ATTRIBUTE(qcom_soc_sleep_stats);
126 DEFINE_SHOW_ATTRIBUTE(qcom_subsystem_sleep_stats);
128 static void qcom_create_soc_sleep_stat_files(struct dentry *root, void __iomem *reg,
129 struct stats_data *d,
130 const struct stats_config *config)
132 char stat_type[sizeof(u32) + 1] = {0};
133 size_t stats_offset = config->stats_offset;
134 u32 offset = 0, type;
138 * On RPM targets, stats offset location is dynamic and changes from target
139 * to target and sometimes from build to build for same target.
141 * In such cases the dynamic address is present at 0x14 offset from base
142 * address in devicetree. The last 16bits indicates the stats_offset.
144 if (config->dynamic_offset) {
145 stats_offset = readl(reg + RPM_DYNAMIC_ADDR);
146 stats_offset &= RPM_DYNAMIC_ADDR_MASK;
149 for (i = 0; i < config->num_records; i++) {
150 d[i].base = reg + offset + stats_offset;
153 * Read the low power mode name and create debugfs file for it.
154 * The names read could be of below,
155 * (may change depending on low power mode supported).
156 * For rpmh-sleep-stats: "aosd", "cxsd" and "ddr".
157 * For rpm-sleep-stats: "vmin" and "vlow".
159 type = readl(d[i].base);
160 for (j = 0; j < sizeof(u32); j++) {
161 stat_type[j] = type & 0xff;
165 debugfs_create_file(stat_type, 0400, root, &d[i],
166 &qcom_soc_sleep_stats_fops);
168 offset += sizeof(struct sleep_stats);
169 if (d[i].appended_stats_avail)
170 offset += sizeof(struct appended_stats);
174 static void qcom_create_subsystem_stat_files(struct dentry *root,
175 const struct stats_config *config)
179 if (!config->subsystem_stats_in_smem)
182 for (i = 0; i < ARRAY_SIZE(subsystems); i++)
183 debugfs_create_file(subsystems[i].name, 0400, root, (void *)&subsystems[i],
184 &qcom_subsystem_sleep_stats_fops);
187 static int qcom_stats_probe(struct platform_device *pdev)
191 const struct stats_config *config;
192 struct stats_data *d;
195 config = device_get_match_data(&pdev->dev);
199 reg = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
203 d = devm_kcalloc(&pdev->dev, config->num_records,
204 sizeof(*d), GFP_KERNEL);
208 for (i = 0; i < config->num_records; i++)
209 d[i].appended_stats_avail = config->appended_stats_avail;
211 root = debugfs_create_dir("qcom_stats", NULL);
213 qcom_create_subsystem_stat_files(root, config);
214 qcom_create_soc_sleep_stat_files(root, reg, d, config);
216 platform_set_drvdata(pdev, root);
218 device_set_pm_not_required(&pdev->dev);
223 static void qcom_stats_remove(struct platform_device *pdev)
225 struct dentry *root = platform_get_drvdata(pdev);
227 debugfs_remove_recursive(root);
230 static const struct stats_config rpm_data = {
233 .appended_stats_avail = true,
234 .dynamic_offset = true,
235 .subsystem_stats_in_smem = false,
238 /* Older RPM firmwares have the stats at a fixed offset instead */
239 static const struct stats_config rpm_data_dba0 = {
240 .stats_offset = 0xdba0,
242 .appended_stats_avail = true,
243 .dynamic_offset = false,
244 .subsystem_stats_in_smem = false,
247 static const struct stats_config rpmh_data_sdm845 = {
248 .stats_offset = 0x48,
250 .appended_stats_avail = false,
251 .dynamic_offset = false,
252 .subsystem_stats_in_smem = true,
255 static const struct stats_config rpmh_data = {
256 .stats_offset = 0x48,
258 .appended_stats_avail = false,
259 .dynamic_offset = false,
260 .subsystem_stats_in_smem = true,
263 static const struct of_device_id qcom_stats_table[] = {
264 { .compatible = "qcom,apq8084-rpm-stats", .data = &rpm_data_dba0 },
265 { .compatible = "qcom,msm8226-rpm-stats", .data = &rpm_data_dba0 },
266 { .compatible = "qcom,msm8916-rpm-stats", .data = &rpm_data_dba0 },
267 { .compatible = "qcom,msm8974-rpm-stats", .data = &rpm_data_dba0 },
268 { .compatible = "qcom,rpm-stats", .data = &rpm_data },
269 { .compatible = "qcom,rpmh-stats", .data = &rpmh_data },
270 { .compatible = "qcom,sdm845-rpmh-stats", .data = &rpmh_data_sdm845 },
273 MODULE_DEVICE_TABLE(of, qcom_stats_table);
275 static struct platform_driver qcom_stats = {
276 .probe = qcom_stats_probe,
277 .remove = qcom_stats_remove,
279 .name = "qcom_stats",
280 .of_match_table = qcom_stats_table,
284 static int __init qcom_stats_init(void)
286 return platform_driver_register(&qcom_stats);
288 late_initcall(qcom_stats_init);
290 static void __exit qcom_stats_exit(void)
292 platform_driver_unregister(&qcom_stats);
294 module_exit(qcom_stats_exit)
296 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. (QTI) Stats driver");
297 MODULE_LICENSE("GPL v2");