1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2023, Linaro Limited
6 * This driver supports what is known as "Master Stats v2" in Qualcomm
7 * downstream kernel terms, which seems to be the only version which has
8 * ever shipped, all the way from 2013 to 2023.
11 #include <linux/debugfs.h>
13 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/platform_device.h>
18 struct master_stats_data {
23 struct rpm_master_stats {
30 u32 wakeup_reason; /* 0 = "rude wakeup", 1 = scheduled wakeup */
31 u32 last_sleep_trans_dur;
32 u32 last_wake_trans_dur;
34 /* Per-subsystem (*not necessarily* SoC-wide) XO shutdown stats */
41 static int master_stats_show(struct seq_file *s, void *unused)
43 struct master_stats_data *data = s->private;
44 struct rpm_master_stats stat;
46 memcpy_fromio(&stat, data->base, sizeof(stat));
48 seq_printf(s, "%s:\n", data->label);
50 seq_printf(s, "\tLast shutdown @ %llu\n", stat.shutdown_req);
51 seq_printf(s, "\tLast bringup req @ %llu\n", stat.bringup_req);
52 seq_printf(s, "\tLast bringup ack @ %llu\n", stat.bringup_ack);
53 seq_printf(s, "\tLast wakeup idx: %llu\n", stat.wakeup_idx);
54 seq_printf(s, "\tLast XO shutdown enter @ %llu\n", stat.xo_last_enter);
55 seq_printf(s, "\tLast XO shutdown exit @ %llu\n", stat.last_exit);
56 seq_printf(s, "\tXO total duration: %llu\n", stat.xo_total_dur);
57 seq_printf(s, "\tLast sleep transition duration: %u\n", stat.last_sleep_trans_dur);
58 seq_printf(s, "\tLast wake transition duration: %u\n", stat.last_wake_trans_dur);
59 seq_printf(s, "\tXO shutdown count: %u\n", stat.xo_count);
60 seq_printf(s, "\tWakeup reason: 0x%x\n", stat.wakeup_reason);
61 seq_printf(s, "\tShutdown count: %u\n", stat.num_shutdowns);
62 seq_printf(s, "\tActive cores bitmask: 0x%x\n", stat.active_cores);
66 DEFINE_SHOW_ATTRIBUTE(master_stats);
68 static int master_stats_probe(struct platform_device *pdev)
70 struct device *dev = &pdev->dev;
71 struct master_stats_data *data;
72 struct device_node *msgram_np;
73 struct dentry *dent, *root;
77 count = of_property_count_strings(dev->of_node, "qcom,master-names");
81 data = devm_kzalloc(dev, count * sizeof(*data), GFP_KERNEL);
85 root = debugfs_create_dir("qcom_rpm_master_stats", NULL);
86 platform_set_drvdata(pdev, root);
88 for (i = 0; i < count; i++) {
89 msgram_np = of_parse_phandle(dev->of_node, "qcom,rpm-msg-ram", i);
91 debugfs_remove_recursive(root);
92 return dev_err_probe(dev, -ENODEV,
93 "Couldn't parse MSG RAM phandle idx %d", i);
97 * Purposefully skip devm_platform helpers as we're using a
100 ret = of_address_to_resource(msgram_np, 0, &res);
101 of_node_put(msgram_np);
103 debugfs_remove_recursive(root);
107 data[i].base = devm_ioremap(dev, res.start, resource_size(&res));
109 debugfs_remove_recursive(root);
110 return dev_err_probe(dev, -EINVAL,
111 "Could not map the MSG RAM slice idx %d!\n", i);
114 ret = of_property_read_string_index(dev->of_node, "qcom,master-names", i,
117 debugfs_remove_recursive(root);
118 return dev_err_probe(dev, ret,
119 "Could not read name idx %d!\n", i);
123 * Generally it's not advised to fail on debugfs errors, but this
124 * driver's only job is exposing data therein.
126 dent = debugfs_create_file(data[i].label, 0444, root,
127 &data[i], &master_stats_fops);
129 debugfs_remove_recursive(root);
130 return dev_err_probe(dev, PTR_ERR(dent),
131 "Failed to create debugfs file %s!\n", data[i].label);
135 device_set_pm_not_required(dev);
140 static void master_stats_remove(struct platform_device *pdev)
142 struct dentry *root = platform_get_drvdata(pdev);
144 debugfs_remove_recursive(root);
147 static const struct of_device_id rpm_master_table[] = {
148 { .compatible = "qcom,rpm-master-stats" },
152 static struct platform_driver master_stats_driver = {
153 .probe = master_stats_probe,
154 .remove_new = master_stats_remove,
156 .name = "qcom_rpm_master_stats",
157 .of_match_table = rpm_master_table,
160 module_platform_driver(master_stats_driver);
162 MODULE_DESCRIPTION("Qualcomm RPM Master Statistics driver");
163 MODULE_LICENSE("GPL");