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[] = {
40 { "display", 610, 0 },
41 { "adsp_island", 613, 2 },
42 { "slpi_island", 613, 3 },
48 bool appended_stats_avail;
50 bool subsystem_stats_in_smem;
54 bool appended_stats_avail;
66 struct appended_stats {
71 static void qcom_print_stats(struct seq_file *s, const struct sleep_stats *stat)
73 u64 accumulated = stat->accumulated;
75 * If a subsystem is in sleep when reading the sleep stats adjust
76 * the accumulated sleep duration to show actual sleep time.
78 if (stat->last_entered_at > stat->last_exited_at)
79 accumulated += arch_timer_read_counter() - stat->last_entered_at;
81 seq_printf(s, "Count: %u\n", stat->count);
82 seq_printf(s, "Last Entered At: %llu\n", stat->last_entered_at);
83 seq_printf(s, "Last Exited At: %llu\n", stat->last_exited_at);
84 seq_printf(s, "Accumulated Duration: %llu\n", accumulated);
87 static int qcom_subsystem_sleep_stats_show(struct seq_file *s, void *unused)
89 struct subsystem_data *subsystem = s->private;
90 struct sleep_stats *stat;
92 /* Items are allocated lazily, so lookup pointer each time */
93 stat = qcom_smem_get(subsystem->pid, subsystem->smem_item, NULL);
97 qcom_print_stats(s, stat);
102 static int qcom_soc_sleep_stats_show(struct seq_file *s, void *unused)
104 struct stats_data *d = s->private;
105 void __iomem *reg = d->base;
106 struct sleep_stats stat;
108 memcpy_fromio(&stat, reg, sizeof(stat));
109 qcom_print_stats(s, &stat);
111 if (d->appended_stats_avail) {
112 struct appended_stats votes;
114 memcpy_fromio(&votes, reg + CLIENT_VOTES_OFFSET, sizeof(votes));
115 seq_printf(s, "Client Votes: %#x\n", votes.client_votes);
121 DEFINE_SHOW_ATTRIBUTE(qcom_soc_sleep_stats);
122 DEFINE_SHOW_ATTRIBUTE(qcom_subsystem_sleep_stats);
124 static void qcom_create_soc_sleep_stat_files(struct dentry *root, void __iomem *reg,
125 struct stats_data *d,
126 const struct stats_config *config)
128 char stat_type[sizeof(u32) + 1] = {0};
129 size_t stats_offset = config->stats_offset;
130 u32 offset = 0, type;
134 * On RPM targets, stats offset location is dynamic and changes from target
135 * to target and sometimes from build to build for same target.
137 * In such cases the dynamic address is present at 0x14 offset from base
138 * address in devicetree. The last 16bits indicates the stats_offset.
140 if (config->dynamic_offset) {
141 stats_offset = readl(reg + RPM_DYNAMIC_ADDR);
142 stats_offset &= RPM_DYNAMIC_ADDR_MASK;
145 for (i = 0; i < config->num_records; i++) {
146 d[i].base = reg + offset + stats_offset;
149 * Read the low power mode name and create debugfs file for it.
150 * The names read could be of below,
151 * (may change depending on low power mode supported).
152 * For rpmh-sleep-stats: "aosd", "cxsd" and "ddr".
153 * For rpm-sleep-stats: "vmin" and "vlow".
155 type = readl(d[i].base);
156 for (j = 0; j < sizeof(u32); j++) {
157 stat_type[j] = type & 0xff;
161 debugfs_create_file(stat_type, 0400, root, &d[i],
162 &qcom_soc_sleep_stats_fops);
164 offset += sizeof(struct sleep_stats);
165 if (d[i].appended_stats_avail)
166 offset += sizeof(struct appended_stats);
170 static void qcom_create_subsystem_stat_files(struct dentry *root,
171 const struct stats_config *config)
175 if (!config->subsystem_stats_in_smem)
178 for (i = 0; i < ARRAY_SIZE(subsystems); i++)
179 debugfs_create_file(subsystems[i].name, 0400, root, (void *)&subsystems[i],
180 &qcom_subsystem_sleep_stats_fops);
183 static int qcom_stats_probe(struct platform_device *pdev)
187 const struct stats_config *config;
188 struct stats_data *d;
191 config = device_get_match_data(&pdev->dev);
195 reg = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
199 d = devm_kcalloc(&pdev->dev, config->num_records,
200 sizeof(*d), GFP_KERNEL);
204 for (i = 0; i < config->num_records; i++)
205 d[i].appended_stats_avail = config->appended_stats_avail;
207 root = debugfs_create_dir("qcom_stats", NULL);
209 qcom_create_subsystem_stat_files(root, config);
210 qcom_create_soc_sleep_stat_files(root, reg, d, config);
212 platform_set_drvdata(pdev, root);
214 device_set_pm_not_required(&pdev->dev);
219 static int qcom_stats_remove(struct platform_device *pdev)
221 struct dentry *root = platform_get_drvdata(pdev);
223 debugfs_remove_recursive(root);
228 static const struct stats_config rpm_data = {
231 .appended_stats_avail = true,
232 .dynamic_offset = true,
233 .subsystem_stats_in_smem = false,
236 /* Older RPM firmwares have the stats at a fixed offset instead */
237 static const struct stats_config rpm_data_dba0 = {
238 .stats_offset = 0xdba0,
240 .appended_stats_avail = true,
241 .dynamic_offset = false,
242 .subsystem_stats_in_smem = false,
245 static const struct stats_config rpmh_data_sdm845 = {
246 .stats_offset = 0x48,
248 .appended_stats_avail = false,
249 .dynamic_offset = false,
250 .subsystem_stats_in_smem = true,
253 static const struct stats_config rpmh_data = {
254 .stats_offset = 0x48,
256 .appended_stats_avail = false,
257 .dynamic_offset = false,
258 .subsystem_stats_in_smem = true,
261 static const struct of_device_id qcom_stats_table[] = {
262 { .compatible = "qcom,apq8084-rpm-stats", .data = &rpm_data_dba0 },
263 { .compatible = "qcom,msm8226-rpm-stats", .data = &rpm_data_dba0 },
264 { .compatible = "qcom,msm8916-rpm-stats", .data = &rpm_data_dba0 },
265 { .compatible = "qcom,msm8974-rpm-stats", .data = &rpm_data_dba0 },
266 { .compatible = "qcom,rpm-stats", .data = &rpm_data },
267 { .compatible = "qcom,rpmh-stats", .data = &rpmh_data },
268 { .compatible = "qcom,sdm845-rpmh-stats", .data = &rpmh_data_sdm845 },
271 MODULE_DEVICE_TABLE(of, qcom_stats_table);
273 static struct platform_driver qcom_stats = {
274 .probe = qcom_stats_probe,
275 .remove = qcom_stats_remove,
277 .name = "qcom_stats",
278 .of_match_table = qcom_stats_table,
282 static int __init qcom_stats_init(void)
284 return platform_driver_register(&qcom_stats);
286 late_initcall(qcom_stats_init);
288 static void __exit qcom_stats_exit(void)
290 platform_driver_unregister(&qcom_stats);
292 module_exit(qcom_stats_exit)
294 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. (QTI) Stats driver");
295 MODULE_LICENSE("GPL v2");