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