Linux Audio

Check our new training course

Embedded Linux training

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