xref: /linux/drivers/soc/qcom/rpm_master_stats.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1a77b2a0bSKonrad Dybcio // SPDX-License-Identifier: GPL-2.0-only
2a77b2a0bSKonrad Dybcio /*
3a77b2a0bSKonrad Dybcio  * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
4a77b2a0bSKonrad Dybcio  * Copyright (c) 2023, Linaro Limited
5a77b2a0bSKonrad Dybcio  *
6a77b2a0bSKonrad Dybcio  * This driver supports what is known as "Master Stats v2" in Qualcomm
7a77b2a0bSKonrad Dybcio  * downstream kernel terms, which seems to be the only version which has
8a77b2a0bSKonrad Dybcio  * ever shipped, all the way from 2013 to 2023.
9a77b2a0bSKonrad Dybcio  */
10a77b2a0bSKonrad Dybcio 
11a77b2a0bSKonrad Dybcio #include <linux/debugfs.h>
12a77b2a0bSKonrad Dybcio #include <linux/io.h>
13a77b2a0bSKonrad Dybcio #include <linux/module.h>
14a77b2a0bSKonrad Dybcio #include <linux/of.h>
15a77b2a0bSKonrad Dybcio #include <linux/of_address.h>
16a77b2a0bSKonrad Dybcio #include <linux/platform_device.h>
17a77b2a0bSKonrad Dybcio 
18a77b2a0bSKonrad Dybcio struct master_stats_data {
19a77b2a0bSKonrad Dybcio 	void __iomem *base;
20a77b2a0bSKonrad Dybcio 	const char *label;
21a77b2a0bSKonrad Dybcio };
22a77b2a0bSKonrad Dybcio 
23a77b2a0bSKonrad Dybcio struct rpm_master_stats {
24a77b2a0bSKonrad Dybcio 	u32 active_cores;
25a77b2a0bSKonrad Dybcio 	u32 num_shutdowns;
26a77b2a0bSKonrad Dybcio 	u64 shutdown_req;
27a77b2a0bSKonrad Dybcio 	u64 wakeup_idx;
28a77b2a0bSKonrad Dybcio 	u64 bringup_req;
29a77b2a0bSKonrad Dybcio 	u64 bringup_ack;
30a77b2a0bSKonrad Dybcio 	u32 wakeup_reason; /* 0 = "rude wakeup", 1 = scheduled wakeup */
31a77b2a0bSKonrad Dybcio 	u32 last_sleep_trans_dur;
32a77b2a0bSKonrad Dybcio 	u32 last_wake_trans_dur;
33a77b2a0bSKonrad Dybcio 
34a77b2a0bSKonrad Dybcio 	/* Per-subsystem (*not necessarily* SoC-wide) XO shutdown stats */
35a77b2a0bSKonrad Dybcio 	u32 xo_count;
36a77b2a0bSKonrad Dybcio 	u64 xo_last_enter;
37a77b2a0bSKonrad Dybcio 	u64 last_exit;
38a77b2a0bSKonrad Dybcio 	u64 xo_total_dur;
39a77b2a0bSKonrad Dybcio } __packed;
40a77b2a0bSKonrad Dybcio 
master_stats_show(struct seq_file * s,void * unused)41a77b2a0bSKonrad Dybcio static int master_stats_show(struct seq_file *s, void *unused)
42a77b2a0bSKonrad Dybcio {
43a77b2a0bSKonrad Dybcio 	struct master_stats_data *data = s->private;
44a77b2a0bSKonrad Dybcio 	struct rpm_master_stats stat;
45a77b2a0bSKonrad Dybcio 
46a77b2a0bSKonrad Dybcio 	memcpy_fromio(&stat, data->base, sizeof(stat));
47a77b2a0bSKonrad Dybcio 
48a77b2a0bSKonrad Dybcio 	seq_printf(s, "%s:\n", data->label);
49a77b2a0bSKonrad Dybcio 
50a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tLast shutdown @ %llu\n", stat.shutdown_req);
51a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tLast bringup req @ %llu\n", stat.bringup_req);
52a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tLast bringup ack @ %llu\n", stat.bringup_ack);
53a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tLast wakeup idx: %llu\n", stat.wakeup_idx);
54a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tLast XO shutdown enter @ %llu\n", stat.xo_last_enter);
55a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tLast XO shutdown exit @ %llu\n", stat.last_exit);
56a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tXO total duration: %llu\n", stat.xo_total_dur);
57a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tLast sleep transition duration: %u\n", stat.last_sleep_trans_dur);
58a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tLast wake transition duration: %u\n", stat.last_wake_trans_dur);
59a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tXO shutdown count: %u\n", stat.xo_count);
60a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tWakeup reason: 0x%x\n", stat.wakeup_reason);
61a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tShutdown count: %u\n", stat.num_shutdowns);
62a77b2a0bSKonrad Dybcio 	seq_printf(s, "\tActive cores bitmask: 0x%x\n", stat.active_cores);
63a77b2a0bSKonrad Dybcio 
64a77b2a0bSKonrad Dybcio 	return 0;
65a77b2a0bSKonrad Dybcio }
66a77b2a0bSKonrad Dybcio DEFINE_SHOW_ATTRIBUTE(master_stats);
67a77b2a0bSKonrad Dybcio 
master_stats_probe(struct platform_device * pdev)68a77b2a0bSKonrad Dybcio static int master_stats_probe(struct platform_device *pdev)
69a77b2a0bSKonrad Dybcio {
70a77b2a0bSKonrad Dybcio 	struct device *dev = &pdev->dev;
71a77b2a0bSKonrad Dybcio 	struct master_stats_data *data;
72a77b2a0bSKonrad Dybcio 	struct device_node *msgram_np;
73a77b2a0bSKonrad Dybcio 	struct dentry *dent, *root;
74a77b2a0bSKonrad Dybcio 	struct resource res;
75a77b2a0bSKonrad Dybcio 	int count, i, ret;
76a77b2a0bSKonrad Dybcio 
77a77b2a0bSKonrad Dybcio 	count = of_property_count_strings(dev->of_node, "qcom,master-names");
78a77b2a0bSKonrad Dybcio 	if (count < 0)
79a77b2a0bSKonrad Dybcio 		return count;
80a77b2a0bSKonrad Dybcio 
81a77b2a0bSKonrad Dybcio 	data = devm_kzalloc(dev, count * sizeof(*data), GFP_KERNEL);
82a77b2a0bSKonrad Dybcio 	if (!data)
83a77b2a0bSKonrad Dybcio 		return -ENOMEM;
84a77b2a0bSKonrad Dybcio 
85a77b2a0bSKonrad Dybcio 	root = debugfs_create_dir("qcom_rpm_master_stats", NULL);
86a77b2a0bSKonrad Dybcio 	platform_set_drvdata(pdev, root);
87a77b2a0bSKonrad Dybcio 
88a77b2a0bSKonrad Dybcio 	for (i = 0; i < count; i++) {
89a77b2a0bSKonrad Dybcio 		msgram_np = of_parse_phandle(dev->of_node, "qcom,rpm-msg-ram", i);
90a77b2a0bSKonrad Dybcio 		if (!msgram_np) {
91a77b2a0bSKonrad Dybcio 			debugfs_remove_recursive(root);
92a77b2a0bSKonrad Dybcio 			return dev_err_probe(dev, -ENODEV,
93a77b2a0bSKonrad Dybcio 					     "Couldn't parse MSG RAM phandle idx %d", i);
94a77b2a0bSKonrad Dybcio 		}
95a77b2a0bSKonrad Dybcio 
96a77b2a0bSKonrad Dybcio 		/*
97a77b2a0bSKonrad Dybcio 		 * Purposefully skip devm_platform helpers as we're using a
98a77b2a0bSKonrad Dybcio 		 * shared resource.
99a77b2a0bSKonrad Dybcio 		 */
100a77b2a0bSKonrad Dybcio 		ret = of_address_to_resource(msgram_np, 0, &res);
101a77b2a0bSKonrad Dybcio 		of_node_put(msgram_np);
102a77b2a0bSKonrad Dybcio 		if (ret < 0) {
103a77b2a0bSKonrad Dybcio 			debugfs_remove_recursive(root);
104a77b2a0bSKonrad Dybcio 			return ret;
105a77b2a0bSKonrad Dybcio 		}
106a77b2a0bSKonrad Dybcio 
107a77b2a0bSKonrad Dybcio 		data[i].base = devm_ioremap(dev, res.start, resource_size(&res));
1083aed1129SDan Carpenter 		if (!data[i].base) {
109a77b2a0bSKonrad Dybcio 			debugfs_remove_recursive(root);
110a77b2a0bSKonrad Dybcio 			return dev_err_probe(dev, -EINVAL,
111a77b2a0bSKonrad Dybcio 					     "Could not map the MSG RAM slice idx %d!\n", i);
112a77b2a0bSKonrad Dybcio 		}
113a77b2a0bSKonrad Dybcio 
114a77b2a0bSKonrad Dybcio 		ret = of_property_read_string_index(dev->of_node, "qcom,master-names", i,
115a77b2a0bSKonrad Dybcio 						    &data[i].label);
116a77b2a0bSKonrad Dybcio 		if (ret < 0) {
117a77b2a0bSKonrad Dybcio 			debugfs_remove_recursive(root);
118a77b2a0bSKonrad Dybcio 			return dev_err_probe(dev, ret,
119a77b2a0bSKonrad Dybcio 					     "Could not read name idx %d!\n", i);
120a77b2a0bSKonrad Dybcio 		}
121a77b2a0bSKonrad Dybcio 
122a77b2a0bSKonrad Dybcio 		/*
123a77b2a0bSKonrad Dybcio 		 * Generally it's not advised to fail on debugfs errors, but this
124a77b2a0bSKonrad Dybcio 		 * driver's only job is exposing data therein.
125a77b2a0bSKonrad Dybcio 		 */
126a77b2a0bSKonrad Dybcio 		dent = debugfs_create_file(data[i].label, 0444, root,
127a77b2a0bSKonrad Dybcio 					   &data[i], &master_stats_fops);
128a77b2a0bSKonrad Dybcio 		if (IS_ERR(dent)) {
129a77b2a0bSKonrad Dybcio 			debugfs_remove_recursive(root);
130a77b2a0bSKonrad Dybcio 			return dev_err_probe(dev, PTR_ERR(dent),
131a77b2a0bSKonrad Dybcio 					     "Failed to create debugfs file %s!\n", data[i].label);
132a77b2a0bSKonrad Dybcio 		}
133a77b2a0bSKonrad Dybcio 	}
134a77b2a0bSKonrad Dybcio 
135a77b2a0bSKonrad Dybcio 	device_set_pm_not_required(dev);
136a77b2a0bSKonrad Dybcio 
137a77b2a0bSKonrad Dybcio 	return 0;
138a77b2a0bSKonrad Dybcio }
139a77b2a0bSKonrad Dybcio 
master_stats_remove(struct platform_device * pdev)140a77b2a0bSKonrad Dybcio static void master_stats_remove(struct platform_device *pdev)
141a77b2a0bSKonrad Dybcio {
142a77b2a0bSKonrad Dybcio 	struct dentry *root = platform_get_drvdata(pdev);
143a77b2a0bSKonrad Dybcio 
144a77b2a0bSKonrad Dybcio 	debugfs_remove_recursive(root);
145a77b2a0bSKonrad Dybcio }
146a77b2a0bSKonrad Dybcio 
147a77b2a0bSKonrad Dybcio static const struct of_device_id rpm_master_table[] = {
148a77b2a0bSKonrad Dybcio 	{ .compatible = "qcom,rpm-master-stats" },
149a77b2a0bSKonrad Dybcio 	{ },
150a77b2a0bSKonrad Dybcio };
151*77706838SKrzysztof Kozlowski /*
152*77706838SKrzysztof Kozlowski  * No MODULE_DEVICE_TABLE intentionally: that's a debugging module, to be
153*77706838SKrzysztof Kozlowski  * loaded manually only.
154*77706838SKrzysztof Kozlowski  */
155a77b2a0bSKonrad Dybcio 
156a77b2a0bSKonrad Dybcio static struct platform_driver master_stats_driver = {
157a77b2a0bSKonrad Dybcio 	.probe = master_stats_probe,
158a77b2a0bSKonrad Dybcio 	.remove_new = master_stats_remove,
159a77b2a0bSKonrad Dybcio 	.driver = {
160a77b2a0bSKonrad Dybcio 		.name = "qcom_rpm_master_stats",
161a77b2a0bSKonrad Dybcio 		.of_match_table = rpm_master_table,
162a77b2a0bSKonrad Dybcio 	},
163a77b2a0bSKonrad Dybcio };
164a77b2a0bSKonrad Dybcio module_platform_driver(master_stats_driver);
165a77b2a0bSKonrad Dybcio 
166a77b2a0bSKonrad Dybcio MODULE_DESCRIPTION("Qualcomm RPM Master Statistics driver");
167a77b2a0bSKonrad Dybcio MODULE_LICENSE("GPL");
168