xref: /linux/drivers/platform/x86/intel/vsec_tpmi.c (revision 6736b1801908acfa64ef2b651c5bb78389a8a4c6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver to enumerate TPMI features and create devices
4  *
5  * Copyright (c) 2023, Intel Corporation.
6  * All Rights Reserved.
7  *
8  * The TPMI (Topology Aware Register and PM Capsule Interface) provides a
9  * flexible, extendable and PCIe enumerable MMIO interface for PM features.
10  *
11  * For example Intel RAPL (Running Average Power Limit) provides a MMIO
12  * interface using TPMI. This has advantage over traditional MSR
13  * (Model Specific Register) interface, where a thread needs to be scheduled
14  * on the target CPU to read or write. Also the RAPL features vary between
15  * CPU models, and hence lot of model specific code. Here TPMI provides an
16  * architectural interface by providing hierarchical tables and fields,
17  * which will not need any model specific implementation.
18  *
19  * The TPMI interface uses a PCI VSEC structure to expose the location of
20  * MMIO region.
21  *
22  * This VSEC structure is present in the PCI configuration space of the
23  * Intel Out-of-Band (OOB) device, which  is handled by the Intel VSEC
24  * driver. The Intel VSEC driver parses VSEC structures present in the PCI
25  * configuration space of the given device and creates an auxiliary device
26  * object for each of them. In particular, it creates an auxiliary device
27  * object representing TPMI that can be bound by an auxiliary driver.
28  *
29  * This TPMI driver will bind to the TPMI auxiliary device object created
30  * by the Intel VSEC driver.
31  *
32  * The TPMI specification defines a PFS (PM Feature Structure) table.
33  * This table is present in the TPMI MMIO region. The starting address
34  * of PFS is derived from the tBIR (Bar Indicator Register) and "Address"
35  * field from the VSEC header.
36  *
37  * Each TPMI PM feature has one entry in the PFS with a unique TPMI
38  * ID and its access details. The TPMI driver creates device nodes
39  * for the supported PM features.
40  *
41  * The names of the devices created by the TPMI driver start with the
42  * "intel_vsec.tpmi-" prefix which is followed by a specific name of the
43  * given PM feature (for example, "intel_vsec.tpmi-rapl.0").
44  *
45  * The device nodes are create by using interface "intel_vsec_add_aux()"
46  * provided by the Intel VSEC driver.
47  */
48 
49 #include <linux/align.h>
50 #include <linux/auxiliary_bus.h>
51 #include <linux/bitfield.h>
52 #include <linux/debugfs.h>
53 #include <linux/cleanup.h>
54 #include <linux/delay.h>
55 #include <linux/intel_tpmi.h>
56 #include <linux/intel_vsec.h>
57 #include <linux/io.h>
58 #include <linux/iopoll.h>
59 #include <linux/module.h>
60 #include <linux/notifier.h>
61 #include <linux/pci.h>
62 #include <linux/security.h>
63 #include <linux/sizes.h>
64 #include <linux/string_helpers.h>
65 
66 /**
67  * struct intel_tpmi_pfs_entry - TPMI PM Feature Structure (PFS) entry
68  * @tpmi_id:	TPMI feature identifier (what the feature is and its data format).
69  * @num_entries: Number of feature interface instances present in the PFS.
70  *		 This represents the maximum number of Power domains in the SoC.
71  * @entry_size:	Interface instance entry size in 32-bit words.
72  * @cap_offset:	Offset from the PM_Features base address to the base of the PM VSEC
73  *		register bank in KB.
74  * @attribute:	Feature attribute: 0=BIOS. 1=OS. 2-3=Reserved.
75  * @reserved:	Bits for use in the future.
76  *
77  * Represents one TPMI feature entry data in the PFS retrieved as is
78  * from the hardware.
79  */
80 struct intel_tpmi_pfs_entry {
81 	u64 tpmi_id:8;
82 	u64 num_entries:8;
83 	u64 entry_size:16;
84 	u64 cap_offset:16;
85 	u64 attribute:2;
86 	u64 reserved:14;
87 } __packed;
88 
89 /**
90  * struct intel_tpmi_pm_feature - TPMI PM Feature information for a TPMI ID
91  * @pfs_header:	PFS header retireved from the hardware.
92  * @vsec_offset: Starting MMIO address for this feature in bytes. Essentially
93  *		 this offset = "Address" from VSEC header + PFS Capability
94  *		 offset for this feature entry.
95  * @vsec_dev:	Pointer to intel_vsec_device structure for this TPMI device
96  *
97  * Represents TPMI instance information for one TPMI ID.
98  */
99 struct intel_tpmi_pm_feature {
100 	struct intel_tpmi_pfs_entry pfs_header;
101 	u64 vsec_offset;
102 	struct intel_vsec_device *vsec_dev;
103 };
104 
105 /**
106  * struct intel_tpmi_info - TPMI information for all IDs in an instance
107  * @tpmi_features:	Pointer to a list of TPMI feature instances
108  * @vsec_dev:		Pointer to intel_vsec_device structure for this TPMI device
109  * @feature_count:	Number of TPMI of TPMI instances pointed by tpmi_features
110  * @pfs_start:		Start of PFS offset for the TPMI instances in this device
111  * @plat_info:		Stores platform info which can be used by the client drivers
112  * @tpmi_control_mem:	Memory mapped IO for getting control information
113  * @dbgfs_dir:		debugfs entry pointer
114  *
115  * Stores the information for all TPMI devices enumerated from a single PCI device.
116  */
117 struct intel_tpmi_info {
118 	struct intel_tpmi_pm_feature *tpmi_features;
119 	struct intel_vsec_device *vsec_dev;
120 	int feature_count;
121 	u64 pfs_start;
122 	struct oobmsm_plat_info plat_info;
123 	void __iomem *tpmi_control_mem;
124 	struct dentry *dbgfs_dir;
125 };
126 
127 /**
128  * struct tpmi_info_header - CPU package ID to PCI device mapping information
129  * @fn:		PCI function number
130  * @dev:	PCI device number
131  * @bus:	PCI bus number
132  * @pkg:	CPU Package id
133  * @segment:	PCI segment id
134  * @partition:	Package Partition id
135  * @cdie_mask:	Bitmap of compute dies in the current partition
136  * @reserved:	Reserved for future use
137  * @lock:	When set to 1 the register is locked and becomes read-only
138  *		until next reset. Not for use by the OS driver.
139  *
140  * The structure to read hardware provided mapping information.
141  */
142 struct tpmi_info_header {
143 	u64 fn:3;
144 	u64 dev:5;
145 	u64 bus:8;
146 	u64 pkg:8;
147 	u64 segment:8;
148 	u64 partition:2;
149 	u64 cdie_mask:16;
150 	u64 reserved:13;
151 	u64 lock:1;
152 } __packed;
153 
154 /**
155  * struct tpmi_feature_state - Structure to read hardware state of a feature
156  * @enabled:	Enable state of a feature, 1: enabled, 0: disabled
157  * @reserved_1:	Reserved for future use
158  * @write_blocked: Writes are blocked means all write operations are ignored
159  * @read_blocked: Reads are blocked means will read 0xFFs
160  * @pcs_select:	Interface used by out of band software, not used in OS
161  * @reserved_2:	Reserved for future use
162  * @id:		TPMI ID of the feature
163  * @reserved_3:	Reserved for future use
164  * @locked:	When set to 1, OS can't change this register.
165  *
166  * The structure is used to read hardware state of a TPMI feature. This
167  * information is used for debug and restricting operations for this feature.
168  */
169 struct tpmi_feature_state {
170 	u32 enabled:1;
171 	u32 reserved_1:3;
172 	u32 write_blocked:1;
173 	u32 read_blocked:1;
174 	u32 pcs_select:1;
175 	u32 reserved_2:1;
176 	u32 id:8;
177 	u32 reserved_3:15;
178 	u32 locked:1;
179 } __packed;
180 
181 /*
182  * The size from hardware is in u32 units. This size is from a trusted hardware,
183  * but better to verify for pre silicon platforms. Set size to 0, when invalid.
184  */
185 #define TPMI_GET_SINGLE_ENTRY_SIZE(pfs)							\
186 ({											\
187 	pfs->pfs_header.entry_size > SZ_1K ? 0 : pfs->pfs_header.entry_size << 2;	\
188 })
189 
190 /* Used during auxbus device creation */
191 static DEFINE_IDA(intel_vsec_tpmi_ida);
192 
193 static BLOCKING_NOTIFIER_HEAD(tpmi_notify_list);
194 
195 int tpmi_register_notifier(struct notifier_block *nb)
196 {
197 	return blocking_notifier_chain_register(&tpmi_notify_list, nb);
198 }
199 EXPORT_SYMBOL_NS_GPL(tpmi_register_notifier, "INTEL_TPMI");
200 
201 int tpmi_unregister_notifier(struct notifier_block *nb)
202 {
203 	return blocking_notifier_chain_unregister(&tpmi_notify_list, nb);
204 }
205 EXPORT_SYMBOL_NS_GPL(tpmi_unregister_notifier, "INTEL_TPMI");
206 
207 struct oobmsm_plat_info *tpmi_get_platform_data(struct auxiliary_device *auxdev)
208 {
209 	struct intel_vsec_device *vsec_dev = auxdev_to_ivdev(auxdev);
210 
211 	return vsec_dev->priv_data;
212 }
213 EXPORT_SYMBOL_NS_GPL(tpmi_get_platform_data, "INTEL_TPMI");
214 
215 int tpmi_get_resource_count(struct auxiliary_device *auxdev)
216 {
217 	struct intel_vsec_device *vsec_dev = auxdev_to_ivdev(auxdev);
218 
219 	if (vsec_dev)
220 		return vsec_dev->num_resources;
221 
222 	return 0;
223 }
224 EXPORT_SYMBOL_NS_GPL(tpmi_get_resource_count, "INTEL_TPMI");
225 
226 struct resource *tpmi_get_resource_at_index(struct auxiliary_device *auxdev, int index)
227 {
228 	struct intel_vsec_device *vsec_dev = auxdev_to_ivdev(auxdev);
229 
230 	if (vsec_dev && index < vsec_dev->num_resources)
231 		return &vsec_dev->resource[index];
232 
233 	return NULL;
234 }
235 EXPORT_SYMBOL_NS_GPL(tpmi_get_resource_at_index, "INTEL_TPMI");
236 
237 /* TPMI Control Interface */
238 
239 #define TPMI_CONTROL_STATUS_OFFSET	0x00
240 #define TPMI_COMMAND_OFFSET		0x08
241 #define TMPI_CONTROL_DATA_VAL_OFFSET	0x0c
242 
243 /*
244  * Spec is calling for max 1 seconds to get ownership at the worst
245  * case. Read at 10 ms timeouts and repeat up to 1 second.
246  */
247 #define TPMI_CONTROL_TIMEOUT_US		(10 * USEC_PER_MSEC)
248 #define TPMI_CONTROL_TIMEOUT_MAX_US	(1 * USEC_PER_SEC)
249 
250 #define TPMI_RB_TIMEOUT_US		(10 * USEC_PER_MSEC)
251 #define TPMI_RB_TIMEOUT_MAX_US		USEC_PER_SEC
252 
253 /* TPMI Control status register defines */
254 
255 #define TPMI_CONTROL_STATUS_RB		BIT_ULL(0)
256 
257 #define TPMI_CONTROL_STATUS_OWNER	GENMASK_ULL(5, 4)
258 #define TPMI_OWNER_NONE			0
259 #define TPMI_OWNER_IN_BAND		1
260 
261 #define TPMI_CONTROL_STATUS_CPL		BIT_ULL(6)
262 #define TPMI_CONTROL_STATUS_RESULT	GENMASK_ULL(15, 8)
263 #define TPMI_CONTROL_STATUS_LEN		GENMASK_ULL(31, 16)
264 
265 #define TPMI_CMD_PKT_LEN		2
266 #define TPMI_CMD_STATUS_SUCCESS		0x40
267 
268 /* TPMI command data registers */
269 #define TMPI_CONTROL_DATA_CMD		GENMASK_ULL(7, 0)
270 #define TPMI_CONTROL_DATA_VAL_FEATURE	GENMASK_ULL(48, 40)
271 
272 /* Command to send via control interface */
273 #define TPMI_CONTROL_GET_STATE_CMD	0x10
274 
275 #define TPMI_CONTROL_CMD_MASK		GENMASK_ULL(48, 40)
276 
277 #define TPMI_CMD_LEN_MASK		GENMASK_ULL(18, 16)
278 
279 /* Mutex to complete get feature status without interruption */
280 static DEFINE_MUTEX(tpmi_dev_lock);
281 
282 static int tpmi_wait_for_owner(struct intel_tpmi_info *tpmi_info, u8 owner)
283 {
284 	u64 control;
285 
286 	return readq_poll_timeout(tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET,
287 				  control, owner == FIELD_GET(TPMI_CONTROL_STATUS_OWNER, control),
288 				  TPMI_CONTROL_TIMEOUT_US, TPMI_CONTROL_TIMEOUT_MAX_US);
289 }
290 
291 static int tpmi_read_feature_status(struct intel_tpmi_info *tpmi_info, int feature_id,
292 				    struct tpmi_feature_state *feature_state)
293 {
294 	u64 control, data;
295 	int ret;
296 
297 	if (!tpmi_info->tpmi_control_mem)
298 		return -EFAULT;
299 
300 	mutex_lock(&tpmi_dev_lock);
301 
302 	/* Wait for owner bit set to 0 (none) */
303 	ret = tpmi_wait_for_owner(tpmi_info, TPMI_OWNER_NONE);
304 	if (ret)
305 		goto err_unlock;
306 
307 	/* set command id to 0x10 for TPMI_GET_STATE */
308 	data = FIELD_PREP(TMPI_CONTROL_DATA_CMD, TPMI_CONTROL_GET_STATE_CMD);
309 
310 	/* 32 bits for DATA offset and +8 for feature_id field */
311 	data |= FIELD_PREP(TPMI_CONTROL_DATA_VAL_FEATURE, feature_id);
312 
313 	/* Write at command offset for qword access */
314 	writeq(data, tpmi_info->tpmi_control_mem + TPMI_COMMAND_OFFSET);
315 
316 	/* Wait for owner bit set to in-band */
317 	ret = tpmi_wait_for_owner(tpmi_info, TPMI_OWNER_IN_BAND);
318 	if (ret)
319 		goto err_unlock;
320 
321 	/* Set Run Busy and packet length of 2 dwords */
322 	control = TPMI_CONTROL_STATUS_RB;
323 	control |= FIELD_PREP(TPMI_CONTROL_STATUS_LEN, TPMI_CMD_PKT_LEN);
324 
325 	/* Write at status offset for qword access */
326 	writeq(control, tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);
327 
328 	/* Wait for Run Busy clear */
329 	ret = readq_poll_timeout(tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET,
330 				 control, !(control & TPMI_CONTROL_STATUS_RB),
331 				 TPMI_RB_TIMEOUT_US, TPMI_RB_TIMEOUT_MAX_US);
332 	if (ret)
333 		goto done_proc;
334 
335 	control = FIELD_GET(TPMI_CONTROL_STATUS_RESULT, control);
336 	if (control != TPMI_CMD_STATUS_SUCCESS) {
337 		ret = -EBUSY;
338 		goto done_proc;
339 	}
340 
341 	/* Response is ready */
342 	memcpy_fromio(feature_state, tpmi_info->tpmi_control_mem + TMPI_CONTROL_DATA_VAL_OFFSET,
343 		      sizeof(*feature_state));
344 
345 	ret = 0;
346 
347 done_proc:
348 	/* Set CPL "completion" bit */
349 	writeq(TPMI_CONTROL_STATUS_CPL, tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);
350 
351 err_unlock:
352 	mutex_unlock(&tpmi_dev_lock);
353 
354 	return ret;
355 }
356 
357 int tpmi_get_feature_status(struct auxiliary_device *auxdev,
358 			    int feature_id, bool *read_blocked, bool *write_blocked)
359 {
360 	struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(auxdev->dev.parent);
361 	struct intel_tpmi_info *tpmi_info = auxiliary_get_drvdata(&intel_vsec_dev->auxdev);
362 	struct tpmi_feature_state feature_state;
363 	int ret;
364 
365 	ret = tpmi_read_feature_status(tpmi_info, feature_id, &feature_state);
366 	if (ret)
367 		return ret;
368 
369 	*read_blocked = feature_state.read_blocked;
370 	*write_blocked = feature_state.write_blocked;
371 
372 	return 0;
373 }
374 EXPORT_SYMBOL_NS_GPL(tpmi_get_feature_status, "INTEL_TPMI");
375 
376 struct dentry *tpmi_get_debugfs_dir(struct auxiliary_device *auxdev)
377 {
378 	struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(auxdev->dev.parent);
379 	struct intel_tpmi_info *tpmi_info = auxiliary_get_drvdata(&intel_vsec_dev->auxdev);
380 
381 	return tpmi_info->dbgfs_dir;
382 }
383 EXPORT_SYMBOL_NS_GPL(tpmi_get_debugfs_dir, "INTEL_TPMI");
384 
385 static int tpmi_pfs_dbg_show(struct seq_file *s, void *unused)
386 {
387 	struct intel_tpmi_info *tpmi_info = s->private;
388 	int locked, disabled, read_blocked, write_blocked;
389 	struct tpmi_feature_state feature_state;
390 	struct intel_tpmi_pm_feature *pfs;
391 	int ret, i;
392 
393 
394 	seq_printf(s, "tpmi PFS start offset 0x:%llx\n", tpmi_info->pfs_start);
395 	seq_puts(s, "tpmi_id\t\tentries\t\tsize\t\tcap_offset\tattribute\tvsec_offset\tlocked\tdisabled\tread_blocked\twrite_blocked\n");
396 	for (i = 0; i < tpmi_info->feature_count; ++i) {
397 		pfs = &tpmi_info->tpmi_features[i];
398 		ret = tpmi_read_feature_status(tpmi_info, pfs->pfs_header.tpmi_id, &feature_state);
399 		if (ret) {
400 			locked = 'U';
401 			disabled = 'U';
402 			read_blocked = 'U';
403 			write_blocked = 'U';
404 		} else {
405 			disabled = feature_state.enabled ? 'N' : 'Y';
406 			locked = feature_state.locked ? 'Y' : 'N';
407 			read_blocked = feature_state.read_blocked ? 'Y' : 'N';
408 			write_blocked = feature_state.write_blocked ? 'Y' : 'N';
409 		}
410 		seq_printf(s, "0x%02x\t\t0x%02x\t\t0x%04x\t\t0x%04x\t\t0x%02x\t\t0x%016llx\t%c\t%c\t\t%c\t\t%c\n",
411 			   pfs->pfs_header.tpmi_id, pfs->pfs_header.num_entries,
412 			   pfs->pfs_header.entry_size, pfs->pfs_header.cap_offset,
413 			   pfs->pfs_header.attribute, pfs->vsec_offset, locked, disabled,
414 			   read_blocked, write_blocked);
415 	}
416 
417 	return 0;
418 }
419 DEFINE_SHOW_ATTRIBUTE(tpmi_pfs_dbg);
420 
421 #define MEM_DUMP_COLUMN_COUNT	8
422 
423 static int tpmi_mem_dump_show(struct seq_file *s, void *unused)
424 {
425 	size_t row_size = MEM_DUMP_COLUMN_COUNT * sizeof(u32);
426 	struct intel_tpmi_pm_feature *pfs = s->private;
427 	int count, ret = 0;
428 	void __iomem *mem;
429 	u32 size;
430 	u64 off;
431 	u8 *buffer;
432 
433 	size = TPMI_GET_SINGLE_ENTRY_SIZE(pfs);
434 	if (!size)
435 		return -EIO;
436 
437 	buffer = kmalloc(size, GFP_KERNEL);
438 	if (!buffer)
439 		return -ENOMEM;
440 
441 	off = pfs->vsec_offset;
442 
443 	mutex_lock(&tpmi_dev_lock);
444 
445 	for (count = 0; count < pfs->pfs_header.num_entries; ++count) {
446 		seq_printf(s, "TPMI Instance:%d offset:0x%llx\n", count, off);
447 
448 		mem = ioremap(off, size);
449 		if (!mem) {
450 			ret = -ENOMEM;
451 			break;
452 		}
453 
454 		memcpy_fromio(buffer, mem, size);
455 
456 		seq_hex_dump(s, " ", DUMP_PREFIX_OFFSET, row_size, sizeof(u32), buffer, size,
457 			     false);
458 
459 		iounmap(mem);
460 
461 		off += size;
462 	}
463 
464 	mutex_unlock(&tpmi_dev_lock);
465 
466 	kfree(buffer);
467 
468 	return ret;
469 }
470 DEFINE_SHOW_ATTRIBUTE(tpmi_mem_dump);
471 
472 static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t len, loff_t *ppos)
473 {
474 	struct seq_file *m = file->private_data;
475 	struct intel_tpmi_pm_feature *pfs = m->private;
476 	u32 addr, value, punit, size;
477 	u32 num_elems;
478 	void __iomem *mem;
479 	int ret;
480 
481 	size = TPMI_GET_SINGLE_ENTRY_SIZE(pfs);
482 	if (!size)
483 		return -EIO;
484 
485 	u32 *array __free(kfree) = NULL;
486 	ret = parse_int_array_user(userbuf, len, (int **)&array);
487 	if (ret < 0)
488 		return ret;
489 
490 	num_elems = *array;
491 	if (num_elems != 3)
492 		return -EINVAL;
493 
494 	punit = array[1];
495 	addr = array[2];
496 	value = array[3];
497 
498 	if (!IS_ALIGNED(addr, sizeof(u32)))
499 		return -EINVAL;
500 
501 	if (punit >= pfs->pfs_header.num_entries)
502 		return -EINVAL;
503 
504 	if (addr >= size)
505 		return -EINVAL;
506 
507 	guard(mutex)(&tpmi_dev_lock);
508 
509 	mem = ioremap(pfs->vsec_offset + punit * size, size);
510 	if (!mem)
511 		return -ENOMEM;
512 
513 	writel(value, mem + addr);
514 
515 	iounmap(mem);
516 
517 	return len;
518 }
519 
520 static int mem_write_show(struct seq_file *s, void *unused)
521 {
522 	return 0;
523 }
524 
525 static int mem_write_open(struct inode *inode, struct file *file)
526 {
527 	return single_open(file, mem_write_show, inode->i_private);
528 }
529 
530 static const struct file_operations mem_write_ops = {
531 	.open           = mem_write_open,
532 	.read           = seq_read,
533 	.write          = mem_write,
534 	.llseek         = seq_lseek,
535 	.release        = single_release,
536 };
537 
538 #define tpmi_to_dev(info)	((info)->vsec_dev->dev)
539 
540 static void tpmi_dbgfs_register(struct intel_tpmi_info *tpmi_info)
541 {
542 	char name[64];
543 	int i;
544 
545 	snprintf(name, sizeof(name), "tpmi-%s", dev_name(tpmi_to_dev(tpmi_info)));
546 	tpmi_info->dbgfs_dir = debugfs_create_dir(name, NULL);
547 
548 	debugfs_create_file("pfs_dump", 0444, tpmi_info->dbgfs_dir, tpmi_info, &tpmi_pfs_dbg_fops);
549 
550 	for (i = 0; i < tpmi_info->feature_count; ++i) {
551 		struct intel_tpmi_pm_feature *pfs;
552 		struct dentry *dir;
553 
554 		pfs = &tpmi_info->tpmi_features[i];
555 		snprintf(name, sizeof(name), "tpmi-id-%02x", pfs->pfs_header.tpmi_id);
556 		dir = debugfs_create_dir(name, tpmi_info->dbgfs_dir);
557 
558 		debugfs_create_file("mem_dump", 0444, dir, pfs, &tpmi_mem_dump_fops);
559 		debugfs_create_file("mem_write", 0644, dir, pfs, &mem_write_ops);
560 	}
561 }
562 
563 static void tpmi_set_control_base(struct auxiliary_device *auxdev,
564 				  struct intel_tpmi_info *tpmi_info,
565 				  struct intel_tpmi_pm_feature *pfs)
566 {
567 	void __iomem *mem;
568 	u32 size;
569 
570 	size = TPMI_GET_SINGLE_ENTRY_SIZE(pfs);
571 	if (!size)
572 		return;
573 
574 	mem = devm_ioremap(&auxdev->dev, pfs->vsec_offset, size);
575 	if (!mem)
576 		return;
577 
578 	/* mem is pointing to TPMI CONTROL base */
579 	tpmi_info->tpmi_control_mem = mem;
580 }
581 
582 static const char *intel_tpmi_name(enum intel_tpmi_id id)
583 {
584 	switch (id) {
585 	case TPMI_ID_RAPL:
586 		return "rapl";
587 	case TPMI_ID_PEM:
588 		return "pem";
589 	case TPMI_ID_UNCORE:
590 		return "uncore";
591 	case TPMI_ID_SST:
592 		return "sst";
593 	case TPMI_ID_PLR:
594 		return "plr";
595 	default:
596 		return NULL;
597 	}
598 }
599 
600 /* String Length for tpmi-"feature_name(upto 8 bytes)" */
601 #define TPMI_FEATURE_NAME_LEN	14
602 
603 static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,
604 			      struct intel_tpmi_pm_feature *pfs,
605 			      u64 pfs_start)
606 {
607 	struct intel_vsec_device *vsec_dev = tpmi_info->vsec_dev;
608 	char feature_id_name[TPMI_FEATURE_NAME_LEN];
609 	struct intel_vsec_device *feature_vsec_dev;
610 	struct tpmi_feature_state feature_state;
611 	struct resource *res, *tmp;
612 	const char *name;
613 	int i, ret;
614 
615 	ret = tpmi_read_feature_status(tpmi_info, pfs->pfs_header.tpmi_id, &feature_state);
616 	if (ret)
617 		return ret;
618 
619 	/*
620 	 * If not enabled, continue to look at other features in the PFS, so return -EOPNOTSUPP.
621 	 * This will not cause failure of loading of this driver.
622 	 */
623 	if (!feature_state.enabled)
624 		return -EOPNOTSUPP;
625 
626 	name = intel_tpmi_name(pfs->pfs_header.tpmi_id);
627 	if (!name)
628 		return -EOPNOTSUPP;
629 
630 	feature_vsec_dev = kzalloc_flex(*feature_vsec_dev, resource, pfs->pfs_header.num_entries);
631 	if (!feature_vsec_dev)
632 		return -ENOMEM;
633 
634 	feature_vsec_dev->num_resources = pfs->pfs_header.num_entries;
635 	res = feature_vsec_dev->resource;
636 
637 	snprintf(feature_id_name, sizeof(feature_id_name), "tpmi-%s", name);
638 
639 	for (i = 0, tmp = res; i < pfs->pfs_header.num_entries; i++, tmp++) {
640 		u64 entry_size_bytes = pfs->pfs_header.entry_size * sizeof(u32);
641 
642 		tmp->start = pfs->vsec_offset + entry_size_bytes * i;
643 		tmp->end = tmp->start + entry_size_bytes - 1;
644 		tmp->flags = IORESOURCE_MEM;
645 	}
646 
647 	feature_vsec_dev->dev = vsec_dev->dev;
648 	feature_vsec_dev->priv_data = &tpmi_info->plat_info;
649 	feature_vsec_dev->priv_data_size = sizeof(tpmi_info->plat_info);
650 	feature_vsec_dev->ida = &intel_vsec_tpmi_ida;
651 
652 	/*
653 	 * intel_vsec_add_aux() is resource managed, no explicit
654 	 * delete is required on error or on module unload.
655 	 * feature_vsec_dev and res memory are also freed as part of
656 	 * device deletion.
657 	 */
658 	return intel_vsec_add_aux(&vsec_dev->auxdev.dev,
659 				  feature_vsec_dev, feature_id_name);
660 }
661 
662 static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)
663 {
664 	struct intel_vsec_device *vsec_dev = tpmi_info->vsec_dev;
665 	int ret, i;
666 
667 	for (i = 0; i < vsec_dev->num_resources; i++) {
668 		ret = tpmi_create_device(tpmi_info, &tpmi_info->tpmi_features[i],
669 					 tpmi_info->pfs_start);
670 		/*
671 		 * Fail, if the supported features fails to create device,
672 		 * otherwise, continue. Even if one device failed to create,
673 		 * fail the loading of driver. Since intel_vsec_add_aux()
674 		 * is resource managed, no clean up is required for the
675 		 * successfully created devices.
676 		 */
677 		if (ret && ret != -EOPNOTSUPP)
678 			return ret;
679 	}
680 
681 	return 0;
682 }
683 
684 #define TPMI_INFO_BUS_INFO_OFFSET	0x08
685 #define TPMI_INFO_MAJOR_VERSION		0x00
686 #define TPMI_INFO_MINOR_VERSION		0x02
687 
688 static int tpmi_process_info(struct intel_tpmi_info *tpmi_info,
689 			     struct intel_tpmi_pm_feature *pfs)
690 {
691 	struct tpmi_info_header header;
692 	void __iomem *info_mem;
693 	u64 feature_header;
694 	int ret = 0;
695 
696 	info_mem = ioremap(pfs->vsec_offset, pfs->pfs_header.entry_size * sizeof(u32));
697 	if (!info_mem)
698 		return -ENOMEM;
699 
700 	feature_header = readq(info_mem);
701 	if (TPMI_MAJOR_VERSION(feature_header) != TPMI_INFO_MAJOR_VERSION) {
702 		ret = -ENODEV;
703 		goto error_info_header;
704 	}
705 
706 	memcpy_fromio(&header, info_mem + TPMI_INFO_BUS_INFO_OFFSET, sizeof(header));
707 
708 	tpmi_info->plat_info.package_id = header.pkg;
709 	tpmi_info->plat_info.bus_number = header.bus;
710 	tpmi_info->plat_info.device_number = header.dev;
711 	tpmi_info->plat_info.function_number = header.fn;
712 
713 	if (TPMI_MINOR_VERSION(feature_header) >= TPMI_INFO_MINOR_VERSION) {
714 		tpmi_info->plat_info.cdie_mask = header.cdie_mask;
715 		tpmi_info->plat_info.partition = header.partition;
716 		tpmi_info->plat_info.segment = header.segment;
717 	}
718 
719 error_info_header:
720 	iounmap(info_mem);
721 
722 	return ret;
723 }
724 
725 static int tpmi_fetch_pfs_header(struct intel_tpmi_pm_feature *pfs, u64 start, int size)
726 {
727 	void __iomem *pfs_mem;
728 
729 	pfs_mem = ioremap(start, size);
730 	if (!pfs_mem)
731 		return -ENOMEM;
732 
733 	memcpy_fromio(&pfs->pfs_header, pfs_mem, sizeof(pfs->pfs_header));
734 
735 	iounmap(pfs_mem);
736 
737 	return 0;
738 }
739 
740 #define TPMI_CAP_OFFSET_UNIT	1024
741 
742 static int intel_vsec_tpmi_init(struct auxiliary_device *auxdev)
743 {
744 	struct intel_vsec_device *vsec_dev = auxdev_to_ivdev(auxdev);
745 	struct pci_dev *pci_dev = to_pci_dev(vsec_dev->dev);
746 	struct intel_tpmi_info *tpmi_info;
747 	u64 pfs_start = 0;
748 	int ret, i;
749 
750 	tpmi_info = devm_kzalloc(&auxdev->dev, sizeof(*tpmi_info), GFP_KERNEL);
751 	if (!tpmi_info)
752 		return -ENOMEM;
753 
754 	tpmi_info->vsec_dev = vsec_dev;
755 	tpmi_info->feature_count = vsec_dev->num_resources;
756 	tpmi_info->plat_info.bus_number = pci_dev->bus->number;
757 
758 	tpmi_info->tpmi_features = devm_kcalloc(&auxdev->dev, vsec_dev->num_resources,
759 						sizeof(*tpmi_info->tpmi_features),
760 						GFP_KERNEL);
761 	if (!tpmi_info->tpmi_features)
762 		return -ENOMEM;
763 
764 	for (i = 0; i < vsec_dev->num_resources; i++) {
765 		struct intel_tpmi_pm_feature *pfs;
766 		struct resource *res;
767 		u64 res_start;
768 		int size, ret;
769 
770 		pfs = &tpmi_info->tpmi_features[i];
771 		pfs->vsec_dev = vsec_dev;
772 
773 		res = &vsec_dev->resource[i];
774 		if (!res)
775 			continue;
776 
777 		res_start = res->start;
778 		size = resource_size(res);
779 		if (size < 0)
780 			continue;
781 
782 		ret = tpmi_fetch_pfs_header(pfs, res_start, size);
783 		if (ret)
784 			continue;
785 
786 		if (!pfs_start)
787 			pfs_start = res_start;
788 
789 		pfs->vsec_offset = pfs_start + pfs->pfs_header.cap_offset * TPMI_CAP_OFFSET_UNIT;
790 
791 		/*
792 		 * Process TPMI_INFO to get PCI device to CPU package ID.
793 		 * Device nodes for TPMI features are not created in this
794 		 * for loop. So, the mapping information will be available
795 		 * when actual device nodes created outside this
796 		 * loop via tpmi_create_devices().
797 		 */
798 		if (pfs->pfs_header.tpmi_id == TPMI_INFO_ID) {
799 			ret = tpmi_process_info(tpmi_info, pfs);
800 			if (ret)
801 				return ret;
802 
803 			ret = intel_vsec_set_mapping(&tpmi_info->plat_info, vsec_dev);
804 			if (ret)
805 				return ret;
806 		}
807 
808 		if (pfs->pfs_header.tpmi_id == TPMI_CONTROL_ID)
809 			tpmi_set_control_base(auxdev, tpmi_info, pfs);
810 	}
811 
812 	tpmi_info->pfs_start = pfs_start;
813 
814 	auxiliary_set_drvdata(auxdev, tpmi_info);
815 
816 	/*
817 	 * Allow debugfs when security policy allows. Everything this debugfs
818 	 * interface provides, can also be done via /dev/mem access. If
819 	 * /dev/mem interface is locked, don't allow debugfs to present any
820 	 * information. Also check for CAP_SYS_RAWIO as /dev/mem interface.
821 	 */
822 	if (!security_locked_down(LOCKDOWN_DEV_MEM) && capable(CAP_SYS_RAWIO))
823 		tpmi_dbgfs_register(tpmi_info);
824 
825 	ret = tpmi_create_devices(tpmi_info);
826 	if (ret) {
827 		debugfs_remove_recursive(tpmi_info->dbgfs_dir);
828 		return ret;
829 	}
830 
831 	blocking_notifier_call_chain(&tpmi_notify_list, TPMI_CORE_INIT, auxdev);
832 
833 	return 0;
834 }
835 
836 static int tpmi_probe(struct auxiliary_device *auxdev,
837 		      const struct auxiliary_device_id *id)
838 {
839 	return intel_vsec_tpmi_init(auxdev);
840 }
841 
842 static void tpmi_remove(struct auxiliary_device *auxdev)
843 {
844 	struct intel_tpmi_info *tpmi_info = auxiliary_get_drvdata(auxdev);
845 
846 	blocking_notifier_call_chain(&tpmi_notify_list, TPMI_CORE_EXIT, auxdev);
847 
848 	debugfs_remove_recursive(tpmi_info->dbgfs_dir);
849 }
850 
851 static const struct auxiliary_device_id tpmi_id_table[] = {
852 	{ .name = "intel_vsec.tpmi" },
853 	{}
854 };
855 MODULE_DEVICE_TABLE(auxiliary, tpmi_id_table);
856 
857 static struct auxiliary_driver tpmi_aux_driver = {
858 	.id_table	= tpmi_id_table,
859 	.probe		= tpmi_probe,
860 	.remove         = tpmi_remove,
861 };
862 
863 module_auxiliary_driver(tpmi_aux_driver);
864 
865 MODULE_IMPORT_NS("INTEL_VSEC");
866 MODULE_DESCRIPTION("Intel TPMI enumeration module");
867 MODULE_LICENSE("GPL");
868