summaryrefslogtreecommitdiff
path: root/drivers/platform/x86/intel/plr_tpmi.c
blob: 69ace6a629bc7959792bd34ba03bb037065b5b24 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Performance Limit Reasons via TPMI
 *
 * Copyright (c) 2024, Intel Corporation.
 */

#include <linux/array_size.h>
#include <linux/auxiliary_bus.h>
#include <linux/bitfield.h>
#include <linux/bitmap.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/gfp_types.h>
#include <linux/intel_tpmi.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/kstrtox.h>
#include <linux/lockdep.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/mutex.h>
#include <linux/seq_file.h>
#include <linux/sprintf.h>
#include <linux/types.h>

#include "tpmi_power_domains.h"

#define PLR_HEADER		0x00
#define PLR_MAILBOX_INTERFACE	0x08
#define PLR_MAILBOX_DATA	0x10
#define PLR_DIE_LEVEL		0x18

#define PLR_MODULE_ID_MASK	GENMASK_ULL(19, 12)
#define PLR_RUN_BUSY		BIT_ULL(63)

#define PLR_COMMAND_WRITE	1

#define PLR_INVALID		GENMASK_ULL(63, 0)

#define PLR_TIMEOUT_US		5
#define PLR_TIMEOUT_MAX_US	1000

#define PLR_COARSE_REASON_BITS	32

struct tpmi_plr;

struct tpmi_plr_die {
	void __iomem *base;
	struct mutex lock; /* Protect access to PLR mailbox */
	int package_id;
	int die_id;
	struct tpmi_plr *plr;
};

struct tpmi_plr {
	struct dentry *dbgfs_dir;
	struct tpmi_plr_die *die_info;
	int num_dies;
	struct auxiliary_device *auxdev;
};

static const char * const plr_coarse_reasons[] = {
	"FREQUENCY",
	"CURRENT",
	"POWER",
	"THERMAL",
	"PLATFORM",
	"MCP",
	"RAS",
	"MISC",
	"QOS",
	"DFC",
};

static const char * const plr_fine_reasons[] = {
	"FREQUENCY_CDYN0",
	"FREQUENCY_CDYN1",
	"FREQUENCY_CDYN2",
	"FREQUENCY_CDYN3",
	"FREQUENCY_CDYN4",
	"FREQUENCY_CDYN5",
	"FREQUENCY_FCT",
	"FREQUENCY_PCS_TRL",
	"CURRENT_MTPMAX",
	"POWER_FAST_RAPL",
	"POWER_PKG_PL1_MSR_TPMI",
	"POWER_PKG_PL1_MMIO",
	"POWER_PKG_PL1_PCS",
	"POWER_PKG_PL2_MSR_TPMI",
	"POWER_PKG_PL2_MMIO",
	"POWER_PKG_PL2_PCS",
	"POWER_PLATFORM_PL1_MSR_TPMI",
	"POWER_PLATFORM_PL1_MMIO",
	"POWER_PLATFORM_PL1_PCS",
	"POWER_PLATFORM_PL2_MSR_TPMI",
	"POWER_PLATFORM_PL2_MMIO",
	"POWER_PLATFORM_PL2_PCS",
	"UNKNOWN(22)",
	"THERMAL_PER_CORE",
	"DFC_UFS",
	"PLATFORM_PROCHOT",
	"PLATFORM_HOT_VR",
	"UNKNOWN(27)",
	"UNKNOWN(28)",
	"MISC_PCS_PSTATE",
};

static u64 plr_read(struct tpmi_plr_die *plr_die, int offset)
{
	return readq(plr_die->base + offset);
}

static void plr_write(u64 val, struct tpmi_plr_die *plr_die, int offset)
{
	writeq(val, plr_die->base + offset);
}

static int plr_read_cpu_status(struct tpmi_plr_die *plr_die, int cpu,
			       u64 *status)
{
	u64 regval;
	int ret;

	lockdep_assert_held(&plr_die->lock);

	regval = FIELD_PREP(PLR_MODULE_ID_MASK, tpmi_get_punit_core_number(cpu));
	regval |= PLR_RUN_BUSY;

	plr_write(regval, plr_die, PLR_MAILBOX_INTERFACE);

	ret = readq_poll_timeout(plr_die->base + PLR_MAILBOX_INTERFACE, regval,
				 !(regval & PLR_RUN_BUSY), PLR_TIMEOUT_US,
				 PLR_TIMEOUT_MAX_US);
	if (ret)
		return ret;

	*status = plr_read(plr_die, PLR_MAILBOX_DATA);

	return 0;
}

static int plr_clear_cpu_status(struct tpmi_plr_die *plr_die, int cpu)
{
	u64 regval;

	lockdep_assert_held(&plr_die->lock);

	regval = FIELD_PREP(PLR_MODULE_ID_MASK, tpmi_get_punit_core_number(cpu));
	regval |= PLR_RUN_BUSY | PLR_COMMAND_WRITE;

	plr_write(0, plr_die, PLR_MAILBOX_DATA);

	plr_write(regval, plr_die, PLR_MAILBOX_INTERFACE);

	return readq_poll_timeout(plr_die->base + PLR_MAILBOX_INTERFACE, regval,
				  !(regval & PLR_RUN_BUSY), PLR_TIMEOUT_US,
				  PLR_TIMEOUT_MAX_US);
}

static void plr_print_bits(struct seq_file *s, u64 val, int bits)
{
	const unsigned long mask[] = { BITMAP_FROM_U64(val) };
	int bit, index;

	for_each_set_bit(bit, mask, bits) {
		const char *str = NULL;

		if (bit < PLR_COARSE_REASON_BITS) {
			if (bit < ARRAY_SIZE(plr_coarse_reasons))
				str = plr_coarse_reasons[bit];
		} else {
			index = bit - PLR_COARSE_REASON_BITS;
			if (index < ARRAY_SIZE(plr_fine_reasons))
				str = plr_fine_reasons[index];
		}

		if (str)
			seq_printf(s, " %s", str);
		else
			seq_printf(s, " UNKNOWN(%d)", bit);
	}

	if (!val)
		seq_puts(s, " none");

	seq_putc(s, '\n');
}

static int plr_status_show(struct seq_file *s, void *unused)
{
	struct tpmi_plr_die *plr_die = s->private;
	int ret;
	u64 val;

	val = plr_read(plr_die, PLR_DIE_LEVEL);
	seq_puts(s, "cpus");
	plr_print_bits(s, val, 32);

	guard(mutex)(&plr_die->lock);

	for (int cpu = 0; cpu < nr_cpu_ids; cpu++) {
		if (plr_die->die_id != tpmi_get_power_domain_id(cpu))
			continue;

		if (plr_die->package_id != topology_physical_package_id(cpu))
			continue;

		seq_printf(s, "cpu%d", cpu);
		ret = plr_read_cpu_status(plr_die, cpu, &val);
		if (ret) {
			dev_err(&plr_die->plr->auxdev->dev, "Failed to read PLR for cpu %d, ret=%d\n",
				cpu, ret);
			return ret;
		}

		plr_print_bits(s, val, 64);
	}

	return 0;
}

static ssize_t plr_status_write(struct file *filp, const char __user *ubuf,
				size_t count, loff_t *ppos)
{
	struct seq_file *s = filp->private_data;
	struct tpmi_plr_die *plr_die = s->private;
	bool val;
	int ret;

	ret = kstrtobool_from_user(ubuf, count, &val);
	if (ret)
		return ret;

	if (val != 0)
		return -EINVAL;

	plr_write(0, plr_die, PLR_DIE_LEVEL);

	guard(mutex)(&plr_die->lock);

	for (int cpu = 0; cpu < nr_cpu_ids; cpu++) {
		if (plr_die->die_id != tpmi_get_power_domain_id(cpu))
			continue;

		if (plr_die->package_id != topology_physical_package_id(cpu))
			continue;

		plr_clear_cpu_status(plr_die, cpu);
	}

	return count;
}
DEFINE_SHOW_STORE_ATTRIBUTE(plr_status);

static int intel_plr_probe(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id)
{
	struct intel_tpmi_plat_info *plat_info;
	struct dentry *dentry;
	int i, num_resources;
	struct resource *res;
	struct tpmi_plr *plr;
	void __iomem *base;
	char name[16];
	int err;

	plat_info = tpmi_get_platform_data(auxdev);
	if (!plat_info)
		return dev_err_probe(&auxdev->dev, -EINVAL, "No platform info\n");

	dentry = tpmi_get_debugfs_dir(auxdev);
	if (!dentry)
		return dev_err_probe(&auxdev->dev, -ENODEV, "No TPMI debugfs directory.\n");

	num_resources = tpmi_get_resource_count(auxdev);
	if (!num_resources)
		return -EINVAL;

	plr = devm_kzalloc(&auxdev->dev, sizeof(*plr), GFP_KERNEL);
	if (!plr)
		return -ENOMEM;

	plr->die_info = devm_kcalloc(&auxdev->dev, num_resources, sizeof(*plr->die_info),
				     GFP_KERNEL);
	if (!plr->die_info)
		return -ENOMEM;

	plr->num_dies = num_resources;
	plr->dbgfs_dir = debugfs_create_dir("plr", dentry);
	plr->auxdev = auxdev;

	for (i = 0; i < num_resources; i++) {
		res = tpmi_get_resource_at_index(auxdev, i);
		if (!res) {
			err = dev_err_probe(&auxdev->dev, -EINVAL, "No resource\n");
			goto err;
		}

		base = devm_ioremap_resource(&auxdev->dev, res);
		if (IS_ERR(base)) {
			err = PTR_ERR(base);
			goto err;
		}

		plr->die_info[i].base = base;
		plr->die_info[i].package_id = plat_info->package_id;
		plr->die_info[i].die_id = i;
		plr->die_info[i].plr = plr;
		mutex_init(&plr->die_info[i].lock);

		if (plr_read(&plr->die_info[i], PLR_HEADER) == PLR_INVALID)
			continue;

		snprintf(name, sizeof(name), "domain%d", i);

		dentry = debugfs_create_dir(name, plr->dbgfs_dir);
		debugfs_create_file("status", 0444, dentry, &plr->die_info[i],
				    &plr_status_fops);
	}

	auxiliary_set_drvdata(auxdev, plr);

	return 0;

err:
	debugfs_remove_recursive(plr->dbgfs_dir);
	return err;
}

static void intel_plr_remove(struct auxiliary_device *auxdev)
{
	struct tpmi_plr *plr = auxiliary_get_drvdata(auxdev);

	debugfs_remove_recursive(plr->dbgfs_dir);
}

static const struct auxiliary_device_id intel_plr_id_table[] = {
	{ .name = "intel_vsec.tpmi-plr" },
	{}
};
MODULE_DEVICE_TABLE(auxiliary, intel_plr_id_table);

static struct auxiliary_driver intel_plr_aux_driver = {
	.id_table       = intel_plr_id_table,
	.remove         = intel_plr_remove,
	.probe          = intel_plr_probe,
};
module_auxiliary_driver(intel_plr_aux_driver);

MODULE_IMPORT_NS(INTEL_TPMI);
MODULE_IMPORT_NS(INTEL_TPMI_POWER_DOMAIN);
MODULE_DESCRIPTION("Intel TPMI PLR Driver");
MODULE_LICENSE("GPL");