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