summaryrefslogtreecommitdiff
path: root/drivers/soc/qcom/rpm_master_stats.c
blob: 9ca13bcf67d3c047460752b029727b80b307b670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
 * Copyright (c) 2023, Linaro Limited
 *
 * This driver supports what is known as "Master Stats v2" in Qualcomm
 * downstream kernel terms, which seems to be the only version which has
 * ever shipped, all the way from 2013 to 2023.
 */

#include <linux/debugfs.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>

struct master_stats_data {
	void __iomem *base;
	const char *label;
};

struct rpm_master_stats {
	u32 active_cores;
	u32 num_shutdowns;
	u64 shutdown_req;
	u64 wakeup_idx;
	u64 bringup_req;
	u64 bringup_ack;
	u32 wakeup_reason; /* 0 = "rude wakeup", 1 = scheduled wakeup */
	u32 last_sleep_trans_dur;
	u32 last_wake_trans_dur;

	/* Per-subsystem (*not necessarily* SoC-wide) XO shutdown stats */
	u32 xo_count;
	u64 xo_last_enter;
	u64 last_exit;
	u64 xo_total_dur;
} __packed;

static int master_stats_show(struct seq_file *s, void *unused)
{
	struct master_stats_data *data = s->private;
	struct rpm_master_stats stat;

	memcpy_fromio(&stat, data->base, sizeof(stat));

	seq_printf(s, "%s:\n", data->label);

	seq_printf(s, "\tLast shutdown @ %llu\n", stat.shutdown_req);
	seq_printf(s, "\tLast bringup req @ %llu\n", stat.bringup_req);
	seq_printf(s, "\tLast bringup ack @ %llu\n", stat.bringup_ack);
	seq_printf(s, "\tLast wakeup idx: %llu\n", stat.wakeup_idx);
	seq_printf(s, "\tLast XO shutdown enter @ %llu\n", stat.xo_last_enter);
	seq_printf(s, "\tLast XO shutdown exit @ %llu\n", stat.last_exit);
	seq_printf(s, "\tXO total duration: %llu\n", stat.xo_total_dur);
	seq_printf(s, "\tLast sleep transition duration: %u\n", stat.last_sleep_trans_dur);
	seq_printf(s, "\tLast wake transition duration: %u\n", stat.last_wake_trans_dur);
	seq_printf(s, "\tXO shutdown count: %u\n", stat.xo_count);
	seq_printf(s, "\tWakeup reason: 0x%x\n", stat.wakeup_reason);
	seq_printf(s, "\tShutdown count: %u\n", stat.num_shutdowns);
	seq_printf(s, "\tActive cores bitmask: 0x%x\n", stat.active_cores);

	return 0;
}
DEFINE_SHOW_ATTRIBUTE(master_stats);

static int master_stats_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct master_stats_data *data;
	struct device_node *msgram_np;
	struct dentry *dent, *root;
	struct resource res;
	int count, i, ret;

	count = of_property_count_strings(dev->of_node, "qcom,master-names");
	if (count < 0)
		return count;

	data = devm_kzalloc(dev, count * sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	root = debugfs_create_dir("qcom_rpm_master_stats", NULL);
	platform_set_drvdata(pdev, root);

	for (i = 0; i < count; i++) {
		msgram_np = of_parse_phandle(dev->of_node, "qcom,rpm-msg-ram", i);
		if (!msgram_np) {
			debugfs_remove_recursive(root);
			return dev_err_probe(dev, -ENODEV,
					     "Couldn't parse MSG RAM phandle idx %d", i);
		}

		/*
		 * Purposefully skip devm_platform helpers as we're using a
		 * shared resource.
		 */
		ret = of_address_to_resource(msgram_np, 0, &res);
		of_node_put(msgram_np);
		if (ret < 0) {
			debugfs_remove_recursive(root);
			return ret;
		}

		data[i].base = devm_ioremap(dev, res.start, resource_size(&res));
		if (!data[i].base) {
			debugfs_remove_recursive(root);
			return dev_err_probe(dev, -EINVAL,
					     "Could not map the MSG RAM slice idx %d!\n", i);
		}

		ret = of_property_read_string_index(dev->of_node, "qcom,master-names", i,
						    &data[i].label);
		if (ret < 0) {
			debugfs_remove_recursive(root);
			return dev_err_probe(dev, ret,
					     "Could not read name idx %d!\n", i);
		}

		/*
		 * Generally it's not advised to fail on debugfs errors, but this
		 * driver's only job is exposing data therein.
		 */
		dent = debugfs_create_file(data[i].label, 0444, root,
					   &data[i], &master_stats_fops);
		if (IS_ERR(dent)) {
			debugfs_remove_recursive(root);
			return dev_err_probe(dev, PTR_ERR(dent),
					     "Failed to create debugfs file %s!\n", data[i].label);
		}
	}

	device_set_pm_not_required(dev);

	return 0;
}

static void master_stats_remove(struct platform_device *pdev)
{
	struct dentry *root = platform_get_drvdata(pdev);

	debugfs_remove_recursive(root);
}

static const struct of_device_id rpm_master_table[] = {
	{ .compatible = "qcom,rpm-master-stats" },
	{ },
};

static struct platform_driver master_stats_driver = {
	.probe = master_stats_probe,
	.remove_new = master_stats_remove,
	.driver = {
		.name = "qcom_rpm_master_stats",
		.of_match_table = rpm_master_table,
	},
};
module_platform_driver(master_stats_driver);

MODULE_DESCRIPTION("Qualcomm RPM Master Statistics driver");
MODULE_LICENSE("GPL");