2023-07-11 22:13:03

by srinivas pandruvada

[permalink] [raw]
Subject: [PATCH v2 0/3] TPMI control and debugfs support

The first patch provides interface to read feature status. This is
generic patch to be used by other feature drivers.

The second patch add support for debugfs. Debugfs also display
feature status using the first patch

Srinivas Pandruvada (3):
platform/x86/intel/tpmi: Read feature control status
platform/x86/intel/tpmi: Add debugfs interface
doc: TPMI: Add debugfs documentation

Documentation/ABI/testing/debugfs-tpmi | 31 ++
MAINTAINERS | 1 +
drivers/platform/x86/intel/tpmi.c | 414 ++++++++++++++++++++++++-
include/linux/intel_tpmi.h | 2 +
4 files changed, 441 insertions(+), 7 deletions(-)
create mode 100644 Documentation/ABI/testing/debugfs-tpmi

--
2.40.1



2023-07-11 22:35:37

by srinivas pandruvada

[permalink] [raw]
Subject: [PATCH v2 2/3] platform/x86/intel/tpmi: Add debugfs interface

Add debugfs interface for debugging TPMI configuration and register
contents. This shows PFS (PM Feature structure) for each TPMI device.

For each feature, show full register contents and allow to modify
register at an offset.

This debugfs interface is not present on locked down kernel with no
DEVMEM access and without CAP_SYS_RAWIO permission.

Signed-off-by: Srinivas Pandruvada <[email protected]>
---
v2
- Check for locked down kernel and permissions
- Move help to documentation folder
- Cosmetic changes
- size check

drivers/platform/x86/intel/tpmi.c | 236 +++++++++++++++++++++++++++++-
1 file changed, 229 insertions(+), 7 deletions(-)

diff --git a/drivers/platform/x86/intel/tpmi.c b/drivers/platform/x86/intel/tpmi.c
index 024e7671687a..bed4336c208b 100644
--- a/drivers/platform/x86/intel/tpmi.c
+++ b/drivers/platform/x86/intel/tpmi.c
@@ -48,12 +48,15 @@

#include <linux/auxiliary_bus.h>
#include <linux/bitfield.h>
+#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/intel_tpmi.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/module.h>
#include <linux/pci.h>
+#include <linux/security.h>
+#include <linux/string_helpers.h>

#include "vsec.h"

@@ -86,12 +89,14 @@ struct intel_tpmi_pfs_entry {
* @vsec_offset: Starting MMIO address for this feature in bytes. Essentially
* this offset = "Address" from VSEC header + PFS Capability
* offset for this feature entry.
+ * @vsec_dev: Pointer to intel_vsec_device structure for this TPMI device
*
* Represents TPMI instance information for one TPMI ID.
*/
struct intel_tpmi_pm_feature {
struct intel_tpmi_pfs_entry pfs_header;
unsigned int vsec_offset;
+ struct intel_vsec_device *vsec_dev;
};

/**
@@ -102,6 +107,7 @@ struct intel_tpmi_pm_feature {
* @pfs_start: Start of PFS offset for the TPMI instances in this device
* @plat_info: Stores platform info which can be used by the client drivers
* @tpmi_control_mem: Memory mapped IO for getting control information
+ * @dbgfs_dir: debugfs entry pointer
*
* Stores the information for all TPMI devices enumerated from a single PCI device.
*/
@@ -112,6 +118,7 @@ struct intel_tpmi_info {
u64 pfs_start;
struct intel_tpmi_plat_info plat_info;
void __iomem *tpmi_control_mem;
+ struct dentry *dbgfs_dir;
};

/**
@@ -330,6 +337,205 @@ int tpmi_get_feature_status(struct auxiliary_device *auxdev, int feature_id,
}
EXPORT_SYMBOL_NS_GPL(tpmi_get_feature_status, INTEL_TPMI);

+static int tpmi_pfs_dbg_show(struct seq_file *s, void *unused)
+{
+ struct intel_tpmi_info *tpmi_info = s->private;
+ int i, ret;
+
+ seq_printf(s, "tpmi PFS start offset 0x:%llx\n", tpmi_info->pfs_start);
+ seq_puts(s, "tpmi_id\t\tentries\t\tsize\t\tcap_offset\tattribute\tvsec_offset\tlocked\tdisabled\n");
+ for (i = 0; i < tpmi_info->feature_count; ++i) {
+ struct intel_tpmi_pm_feature *pfs;
+ int locked, disabled;
+
+ pfs = &tpmi_info->tpmi_features[i];
+ ret = tpmi_read_feature_status(tpmi_info, pfs->pfs_header.tpmi_id, &locked,
+ &disabled);
+ if (ret) {
+ locked = 'U';
+ disabled = 'U';
+ } else {
+ disabled = disabled ? 'Y' : 'N';
+ locked = locked ? 'Y' : 'N';
+ }
+ seq_printf(s, "0x%02x\t\t0x%02x\t\t0x%04x\t\t0x%04x\t\t0x%02x\t\t0x%08x\t%c\t%c\n",
+ pfs->pfs_header.tpmi_id, pfs->pfs_header.num_entries,
+ pfs->pfs_header.entry_size, pfs->pfs_header.cap_offset,
+ pfs->pfs_header.attribute, pfs->vsec_offset, locked, disabled);
+ }
+
+ return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(tpmi_pfs_dbg);
+
+#define MEM_DUMP_COLUMN_COUNT 8
+
+static int tpmi_mem_dump_show(struct seq_file *s, void *unused)
+{
+ size_t row_size = MEM_DUMP_COLUMN_COUNT * sizeof(u32);
+ struct intel_tpmi_pm_feature *pfs = s->private;
+ int count, ret = 0;
+ void __iomem *mem;
+ u32 off, size;
+ u8 *buffer;
+
+ off = pfs->vsec_offset;
+
+ mutex_lock(&tpmi_dev_lock);
+
+ for (count = 0; count < pfs->pfs_header.num_entries; ++count) {
+ size = pfs->pfs_header.entry_size * sizeof(u32);
+ /* The size is from a trusted hardware, but verify anyway */
+ if (size > TPMI_MAX_BUFFER_SIZE) {
+ /*
+ * The next offset depends on the current size. So, can't skip to the
+ * display of the next entry. Simply return from this function with error.
+ */
+ ret = -EIO;
+ goto done_mem_show;
+ }
+
+ buffer = kmalloc(size, GFP_KERNEL);
+ if (!buffer) {
+ ret = -ENOMEM;
+ goto done_mem_show;
+ }
+
+ seq_printf(s, "TPMI Instance:%d offset:0x%x\n", count, off);
+
+ mem = ioremap(off, size);
+ if (!mem) {
+ ret = -ENOMEM;
+ kfree(buffer);
+ goto done_mem_show;
+ }
+
+ memcpy_fromio(buffer, mem, size);
+
+ seq_hex_dump(s, " ", DUMP_PREFIX_OFFSET, row_size, sizeof(u32), buffer, size,
+ false);
+
+ iounmap(mem);
+ kfree(buffer);
+
+ off += size;
+ }
+
+done_mem_show:
+ mutex_unlock(&tpmi_dev_lock);
+
+ return ret;
+}
+DEFINE_SHOW_ATTRIBUTE(tpmi_mem_dump);
+
+static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t len, loff_t *ppos)
+{
+ struct seq_file *m = file->private_data;
+ struct intel_tpmi_pm_feature *pfs = m->private;
+ u32 addr, value, punit, size;
+ u32 num_elems, *array;
+ void __iomem *mem;
+ int ret;
+
+ size = pfs->pfs_header.entry_size * sizeof(u32);
+ if (size > TPMI_MAX_BUFFER_SIZE)
+ return -EIO;
+
+ ret = parse_int_array_user(userbuf, len, (int **)&array);
+ if (ret < 0)
+ return ret;
+
+ num_elems = *array;
+ if (num_elems != 3) {
+ ret = -EINVAL;
+ goto exit_write;
+ }
+
+ punit = array[1];
+ addr = array[2];
+ value = array[3];
+
+ if (punit >= pfs->pfs_header.num_entries) {
+ ret = -EINVAL;
+ goto exit_write;
+ }
+
+ if (addr >= size) {
+ ret = -EINVAL;
+ goto exit_write;
+ }
+
+ mutex_lock(&tpmi_dev_lock);
+
+ mem = ioremap(pfs->vsec_offset + punit * size, size);
+ if (!mem) {
+ ret = -ENOMEM;
+ goto unlock_mem_write;
+ }
+
+ writel(value, mem + addr);
+
+ iounmap(mem);
+
+ ret = len;
+
+unlock_mem_write:
+ mutex_unlock(&tpmi_dev_lock);
+
+exit_write:
+ kfree(array);
+
+ return ret;
+}
+
+static int mem_write_show(struct seq_file *s, void *unused)
+{
+ return 0;
+}
+
+static int mem_write_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, mem_write_show, inode->i_private);
+}
+
+static const struct file_operations mem_write_ops = {
+ .open = mem_write_open,
+ .read = seq_read,
+ .write = mem_write,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
+#define tpmi_to_dev(info) (&info->vsec_dev->pcidev->dev)
+
+static void tpmi_dbgfs_register(struct intel_tpmi_info *tpmi_info)
+{
+ struct dentry *top_dir;
+ char name[64];
+ int i;
+
+ snprintf(name, sizeof(name), "tpmi-%s", dev_name(tpmi_to_dev(tpmi_info)));
+ top_dir = debugfs_create_dir(name, NULL);
+ if (IS_ERR_OR_NULL(top_dir))
+ return;
+
+ tpmi_info->dbgfs_dir = top_dir;
+
+ debugfs_create_file("pfs_dump", 0444, top_dir, tpmi_info, &tpmi_pfs_dbg_fops);
+
+ for (i = 0; i < tpmi_info->feature_count; ++i) {
+ struct intel_tpmi_pm_feature *pfs;
+ struct dentry *dir;
+
+ pfs = &tpmi_info->tpmi_features[i];
+ snprintf(name, sizeof(name), "tpmi-id-%02x", pfs->pfs_header.tpmi_id);
+ dir = debugfs_create_dir(name, top_dir);
+
+ debugfs_create_file("mem_dump", 0444, dir, pfs, &tpmi_mem_dump_fops);
+ debugfs_create_file("mem_write", 0644, dir, pfs, &mem_write_ops);
+ }
+}
+
static void tpmi_set_control_base(struct auxiliary_device *auxdev,
struct intel_tpmi_info *tpmi_info,
struct intel_tpmi_pm_feature *pfs)
@@ -491,7 +697,7 @@ static int intel_vsec_tpmi_init(struct auxiliary_device *auxdev)
struct pci_dev *pci_dev = vsec_dev->pcidev;
struct intel_tpmi_info *tpmi_info;
u64 pfs_start = 0;
- int i;
+ int ret, i;

tpmi_info = devm_kzalloc(&auxdev->dev, sizeof(*tpmi_info), GFP_KERNEL);
if (!tpmi_info)
@@ -514,6 +720,7 @@ static int intel_vsec_tpmi_init(struct auxiliary_device *auxdev)
int size, ret;

pfs = &tpmi_info->tpmi_features[i];
+ pfs->vsec_dev = vsec_dev;

res = &vsec_dev->resource[i];
if (!res)
@@ -551,7 +758,20 @@ static int intel_vsec_tpmi_init(struct auxiliary_device *auxdev)

auxiliary_set_drvdata(auxdev, tpmi_info);

- return tpmi_create_devices(tpmi_info);
+ ret = tpmi_create_devices(tpmi_info);
+ if (ret)
+ return ret;
+
+ /*
+ * Allow debugfs when security policy allows. Everything this debugfs
+ * interface provides, can also be done via /dev/mem access. If
+ * /dev/mem interface is locked, don't allow debugfs to present any
+ * information. Also check for CAP_SYS_RAWIO as /dev/mem interface.
+ */
+ if (!security_locked_down(LOCKDOWN_DEV_MEM) && capable(CAP_SYS_RAWIO))
+ tpmi_dbgfs_register(tpmi_info);
+
+ return 0;
}

static int tpmi_probe(struct auxiliary_device *auxdev,
@@ -560,11 +780,12 @@ static int tpmi_probe(struct auxiliary_device *auxdev,
return intel_vsec_tpmi_init(auxdev);
}

-/*
- * Remove callback is not needed currently as there is no
- * cleanup required. All memory allocs are device managed. All
- * devices created by this modules are also device managed.
- */
+static void tpmi_remove(struct auxiliary_device *auxdev)
+{
+ struct intel_tpmi_info *tpmi_info = auxiliary_get_drvdata(auxdev);
+
+ debugfs_remove_recursive(tpmi_info->dbgfs_dir);
+}

static const struct auxiliary_device_id tpmi_id_table[] = {
{ .name = "intel_vsec.tpmi" },
@@ -575,6 +796,7 @@ MODULE_DEVICE_TABLE(auxiliary, tpmi_id_table);
static struct auxiliary_driver tpmi_aux_driver = {
.id_table = tpmi_id_table,
.probe = tpmi_probe,
+ .remove = tpmi_remove,
};

module_auxiliary_driver(tpmi_aux_driver);
--
2.40.1


2023-07-11 22:48:19

by srinivas pandruvada

[permalink] [raw]
Subject: [PATCH v2 1/3] platform/x86/intel/tpmi: Read feature control status

Some of the PM features can be locked or disabled. In that case, write
interface can be locked.

This status is read via a mailbox. There is one TPMI ID which provides
base address for interface and data register for mail box operation.
The mailbox operations is defined in the TPMI specification. Refer to
https://github.com/intel/tpmi_power_management/ for TPMI specifications.

An API is exposed to feature drivers to read feature control status.

Signed-off-by: Srinivas Pandruvada <[email protected]>
---
The external interface will be used by all feature drivers, will
submit patch for that.

v2
- Renaming of register names as suggested by llpo
- cosmetic changes as suggested by llpo
- size change to u32 for checking with max 4K size

v1
This has all changes suggested by Andi

drivers/platform/x86/intel/tpmi.c | 178 ++++++++++++++++++++++++++++++
include/linux/intel_tpmi.h | 2 +
2 files changed, 180 insertions(+)

diff --git a/drivers/platform/x86/intel/tpmi.c b/drivers/platform/x86/intel/tpmi.c
index d1fd6e69401c..024e7671687a 100644
--- a/drivers/platform/x86/intel/tpmi.c
+++ b/drivers/platform/x86/intel/tpmi.c
@@ -47,8 +47,11 @@
*/

#include <linux/auxiliary_bus.h>
+#include <linux/bitfield.h>
+#include <linux/delay.h>
#include <linux/intel_tpmi.h>
#include <linux/io.h>
+#include <linux/iopoll.h>
#include <linux/module.h>
#include <linux/pci.h>

@@ -98,6 +101,7 @@ struct intel_tpmi_pm_feature {
* @feature_count: Number of TPMI of TPMI instances pointed by tpmi_features
* @pfs_start: Start of PFS offset for the TPMI instances in this device
* @plat_info: Stores platform info which can be used by the client drivers
+ * @tpmi_control_mem: Memory mapped IO for getting control information
*
* Stores the information for all TPMI devices enumerated from a single PCI device.
*/
@@ -107,6 +111,7 @@ struct intel_tpmi_info {
int feature_count;
u64 pfs_start;
struct intel_tpmi_plat_info plat_info;
+ void __iomem *tpmi_control_mem;
};

/**
@@ -139,9 +144,16 @@ enum intel_tpmi_id {
TPMI_ID_PEM = 1, /* Power and Perf excursion Monitor */
TPMI_ID_UNCORE = 2, /* Uncore Frequency Scaling */
TPMI_ID_SST = 5, /* Speed Select Technology */
+ TPMI_CONTROL_ID = 0x80, /* Special ID for getting feature status */
TPMI_INFO_ID = 0x81, /* Special ID for PCI BDF and Package ID information */
};

+/*
+ * TPMI PFS and per feature memory size can't exceed 4K.
+ * Also PFS start and feature memory is 4K aligned.
+ */
+#define TPMI_MAX_BUFFER_SIZE (4 * 1024)
+
/* Used during auxbus device creation */
static DEFINE_IDA(intel_vsec_tpmi_ida);

@@ -175,6 +187,169 @@ struct resource *tpmi_get_resource_at_index(struct auxiliary_device *auxdev, int
}
EXPORT_SYMBOL_NS_GPL(tpmi_get_resource_at_index, INTEL_TPMI);

+/* TPMI Control Interface */
+
+#define TPMI_CONTROL_STATUS_OFFSET 0x00
+#define TPMI_COMMAND_OFFSET 0x08
+
+/*
+ * Spec is calling for max 1 seconds to get ownership at the worst
+ * case. Read at 10 ms timeouts and repeat up to 1 second.
+ */
+#define TPMI_CONTROL_TIMEOUT_US (10 * USEC_PER_MSEC)
+#define TPMI_CONTROL_TIMEOUT_MAX_US USEC_PER_SEC
+
+#define TPMI_RB_TIMEOUT_US (10 * USEC_PER_MSEC)
+#define TPMI_RB_TIMEOUT_MAX_US USEC_PER_SEC
+
+/* TPMI Control status register defines */
+
+#define TPMI_CONTROL_STATUS_RB BIT_ULL(0)
+
+#define TPMI_CONTROL_STATUS_OWNER GENMASK_ULL(5, 4)
+#define TPMI_OWNER_NONE 0
+#define TPMI_OWNER_IN_BAND 1
+
+#define TPMI_CONTROL_STATUS_CPL BIT_ULL(6)
+#define TPMI_CONTROL_STATUS_RESULT GENMASK_ULL(15, 8)
+#define TPMI_CONTROL_STATUS_LEN GENMASK_ULL(31, 16)
+
+#define TPMI_CMD_PKT_LEN 2
+#define TPMI_CMD_STATUS_SUCCESS 0x40
+
+/* TPMI command data registers */
+#define TMPI_CONTROL_DATA_CMD GENMASK_ULL(7, 0)
+#define TMPI_CONTROL_DATA_VAL GENMASK_ULL(63, 32)
+#define TPMI_CONTROL_DATA_VAL_FEATURE GENMASK_ULL(48, 40)
+
+/* Command to send via control interface */
+#define TPMI_CONTROL_GET_STATE_CMD 0x10
+
+#define TPMI_CONTROL_CMD_MASK GENMASK_ULL(48, 40)
+
+#define TPMI_CMD_LEN_MASK GENMASK_ULL(18, 16)
+
+#define TPMI_STATE_DISABLED BIT_ULL(0)
+#define TPMI_STATE_LOCKED BIT_ULL(31)
+
+/* Mutex to complete get feature status without interruption */
+static DEFINE_MUTEX(tpmi_dev_lock);
+
+static int tpmi_wait_for_owner(struct intel_tpmi_info *tpmi_info, u8 owner)
+{
+ u64 control;
+
+ return read_poll_timeout(readq, control,
+ owner == FIELD_GET(TPMI_CONTROL_STATUS_OWNER, control),
+ TPMI_CONTROL_TIMEOUT_US, TPMI_CONTROL_TIMEOUT_MAX_US, false,
+ tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);
+}
+
+static int tpmi_read_feature_status(struct intel_tpmi_info *tpmi_info, int feature_id,
+ int *locked, int *disabled)
+{
+ u64 control, data;
+ int ret;
+
+ if (!tpmi_info->tpmi_control_mem)
+ return -EFAULT;
+
+ mutex_lock(&tpmi_dev_lock);
+
+ /* Wait for owner bit set to 0 (none) */
+ ret = tpmi_wait_for_owner(tpmi_info, TPMI_OWNER_NONE);
+ if (ret)
+ goto err_unlock;
+
+ /* set command id to 0x10 for TPMI_GET_STATE */
+ data = FIELD_PREP(TMPI_CONTROL_DATA_CMD, TPMI_CONTROL_GET_STATE_CMD);
+
+ /* 32 bits for DATA offset and +8 for feature_id field */
+ data |= FIELD_PREP(TPMI_CONTROL_DATA_VAL_FEATURE, feature_id);
+
+ /* Write at command offset for qword access */
+ writeq(data, tpmi_info->tpmi_control_mem + TPMI_COMMAND_OFFSET);
+
+ /* Wait for owner bit set to in-band */
+ ret = tpmi_wait_for_owner(tpmi_info, TPMI_OWNER_IN_BAND);
+ if (ret)
+ goto err_unlock;
+
+ /* Set Run Busy and packet length of 2 dwords */
+ control = TPMI_CONTROL_STATUS_RB;
+ control |= FIELD_PREP(TPMI_CONTROL_STATUS_LEN, TPMI_CMD_PKT_LEN);
+
+ /* Write at status offset for qword access */
+ writeq(control, tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);
+
+ /* Wait for Run Busy clear */
+ ret = read_poll_timeout(readq, control, !(control & TPMI_CONTROL_STATUS_RB),
+ TPMI_RB_TIMEOUT_US, TPMI_RB_TIMEOUT_MAX_US, false,
+ tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);
+ if (ret)
+ goto done_proc;
+
+ control = FIELD_GET(TPMI_CONTROL_STATUS_RESULT, control);
+ if (control != TPMI_CMD_STATUS_SUCCESS) {
+ ret = -EBUSY;
+ goto done_proc;
+ }
+
+ /* Response is ready */
+ data = readq(tpmi_info->tpmi_control_mem + TPMI_COMMAND_OFFSET);
+ data = FIELD_GET(TMPI_CONTROL_DATA_VAL, data);
+
+ *disabled = 0;
+ *locked = 0;
+
+ if (!(data & TPMI_STATE_DISABLED))
+ *disabled = 1;
+
+ if (data & TPMI_STATE_LOCKED)
+ *locked = 1;
+
+ ret = 0;
+
+done_proc:
+ /* Set CPL "completion" bit */
+ writeq(TPMI_CONTROL_STATUS_CPL, tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);
+
+err_unlock:
+ mutex_unlock(&tpmi_dev_lock);
+
+ return ret;
+}
+
+int tpmi_get_feature_status(struct auxiliary_device *auxdev, int feature_id,
+ int *locked, int *disabled)
+{
+ struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(auxdev->dev.parent);
+ struct intel_tpmi_info *tpmi_info = auxiliary_get_drvdata(&intel_vsec_dev->auxdev);
+
+ return tpmi_read_feature_status(tpmi_info, feature_id, locked, disabled);
+}
+EXPORT_SYMBOL_NS_GPL(tpmi_get_feature_status, INTEL_TPMI);
+
+static void tpmi_set_control_base(struct auxiliary_device *auxdev,
+ struct intel_tpmi_info *tpmi_info,
+ struct intel_tpmi_pm_feature *pfs)
+{
+ void __iomem *mem;
+ u32 size;
+
+ size = pfs->pfs_header.num_entries * pfs->pfs_header.entry_size * sizeof(u32);
+ /* This size is coming from trusted hardware, but verify anyway */
+ if (size > TPMI_MAX_BUFFER_SIZE)
+ return;
+
+ mem = devm_ioremap(&auxdev->dev, pfs->vsec_offset, size);
+ if (!mem)
+ return;
+
+ /* mem is pointing to TPMI CONTROL base */
+ tpmi_info->tpmi_control_mem = mem;
+}
+
static const char *intel_tpmi_name(enum intel_tpmi_id id)
{
switch (id) {
@@ -367,6 +542,9 @@ static int intel_vsec_tpmi_init(struct auxiliary_device *auxdev)
*/
if (pfs->pfs_header.tpmi_id == TPMI_INFO_ID)
tpmi_process_info(tpmi_info, pfs);
+
+ if (pfs->pfs_header.tpmi_id == TPMI_CONTROL_ID)
+ tpmi_set_control_base(auxdev, tpmi_info, pfs);
}

tpmi_info->pfs_start = pfs_start;
diff --git a/include/linux/intel_tpmi.h b/include/linux/intel_tpmi.h
index f505788c05da..04d937ad4dc4 100644
--- a/include/linux/intel_tpmi.h
+++ b/include/linux/intel_tpmi.h
@@ -27,4 +27,6 @@ struct intel_tpmi_plat_info *tpmi_get_platform_data(struct auxiliary_device *aux
struct resource *tpmi_get_resource_at_index(struct auxiliary_device *auxdev, int index);
int tpmi_get_resource_count(struct auxiliary_device *auxdev);

+int tpmi_get_feature_status(struct auxiliary_device *auxdev, int feature_id, int *locked,
+ int *disabled);
#endif
--
2.40.1


2023-07-11 22:59:38

by srinivas pandruvada

[permalink] [raw]
Subject: [PATCH v2 3/3] doc: TPMI: Add debugfs documentation

Describe fields in the TPMI debugfs folder.

Signed-off-by: Srinivas Pandruvada <[email protected]>
---
v2
New.

Documentation/ABI/testing/debugfs-tpmi | 31 ++++++++++++++++++++++++++
MAINTAINERS | 1 +
2 files changed, 32 insertions(+)
create mode 100644 Documentation/ABI/testing/debugfs-tpmi

diff --git a/Documentation/ABI/testing/debugfs-tpmi b/Documentation/ABI/testing/debugfs-tpmi
new file mode 100644
index 000000000000..9b530c1aaa2d
--- /dev/null
+++ b/Documentation/ABI/testing/debugfs-tpmi
@@ -0,0 +1,31 @@
+What: /sys/kernel/debug/tpmi-<n>/pfs_dump
+Date: December 2023
+KernelVersion: 6.6
+Contact: [email protected]
+Description:
+The PFS (PM Feature Structure) table, shows details of each power
+management feature. This includes:
+tpmi_id, number of entries, entry size, offset, vsec offset, lock status
+and disabled status.
+Users: Debugging, any user space test suite
+
+What: /sys/kernel/debug/tpmi-<n>/tpmi-id-<n>/mem_dump
+Date: December 2023
+KernelVersion: 6.6
+Contact: [email protected]
+Description:
+Shows the memory dump of the MMIO region for a TPMI ID.
+Users: Debugging, any user space test suite
+
+What: /sys/kernel/debug/tpmi-<n>/tpmi-id-<n>/mem_write
+Date: December 2023
+KernelVersion: 6.6
+Contact: [email protected]
+Description:
+Allows to write at any offset. It doesn't check for Read/Write access
+as hardware will not allow to write at read-only memory. This write is
+at offset multiples of 4. The format is instance,offset,contents.
+Example:
+echo 0,0x20,0xff > mem_write
+echo 1,64,64 > mem_write
+Users: Debugging, any user space test suite
diff --git a/MAINTAINERS b/MAINTAINERS
index 5761b02183a7..4d439121fb36 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10720,6 +10720,7 @@ INTEL TPMI DRIVER
M: Srinivas Pandruvada <[email protected]>
L: [email protected]
S: Maintained
+F: Documentation/ABI/testing/debugfs-tpmi
F: drivers/platform/x86/intel/tpmi.c
F: include/linux/intel_tpmi.h

--
2.40.1


2023-07-12 15:18:38

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] platform/x86/intel/tpmi: Read feature control status

On Tue, Jul 11, 2023 at 03:09:47PM -0700, Srinivas Pandruvada wrote:
> Some of the PM features can be locked or disabled. In that case, write
> interface can be locked.
>
> This status is read via a mailbox. There is one TPMI ID which provides
> base address for interface and data register for mail box operation.
> The mailbox operations is defined in the TPMI specification. Refer to
> https://github.com/intel/tpmi_power_management/ for TPMI specifications.
>
> An API is exposed to feature drivers to read feature control status.

...

> +/*
> + * TPMI PFS and per feature memory size can't exceed 4K.
> + * Also PFS start and feature memory is 4K aligned.
> + */
> +#define TPMI_MAX_BUFFER_SIZE (4 * 1024)

SZ_4K from sizes.h?

...

> +#define TPMI_CONTROL_TIMEOUT_MAX_US USEC_PER_SEC

> +#define TPMI_RB_TIMEOUT_MAX_US USEC_PER_SEC

I think it's easier to get in a form (1 * ..._SEC)

...

> +static int tpmi_wait_for_owner(struct intel_tpmi_info *tpmi_info, u8 owner)
> +{
> + u64 control;
> +
> + return read_poll_timeout(readq, control,
> + owner == FIELD_GET(TPMI_CONTROL_STATUS_OWNER, control),
> + TPMI_CONTROL_TIMEOUT_US, TPMI_CONTROL_TIMEOUT_MAX_US, false,
> + tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);

Since you have "false" why not use readq_poll_timeout()?

> +}

...

> + /* Wait for Run Busy clear */
> + ret = read_poll_timeout(readq, control, !(control & TPMI_CONTROL_STATUS_RB),
> + TPMI_RB_TIMEOUT_US, TPMI_RB_TIMEOUT_MAX_US, false,
> + tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);

Ditto.

> + if (ret)
> + goto done_proc;

...

> + size = pfs->pfs_header.num_entries * pfs->pfs_header.entry_size * sizeof(u32);
> + /* This size is coming from trusted hardware, but verify anyway */

I would move this comment before size assignment that we already know that it's
from the trusted hw.

> + if (size > TPMI_MAX_BUFFER_SIZE)
> + return;

--
With Best Regards,
Andy Shevchenko



2023-07-12 15:25:03

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] platform/x86/intel/tpmi: Add debugfs interface

On Tue, Jul 11, 2023 at 03:09:48PM -0700, Srinivas Pandruvada wrote:
> Add debugfs interface for debugging TPMI configuration and register
> contents. This shows PFS (PM Feature structure) for each TPMI device.
>
> For each feature, show full register contents and allow to modify
> register at an offset.
>
> This debugfs interface is not present on locked down kernel with no
> DEVMEM access and without CAP_SYS_RAWIO permission.

...

> struct intel_tpmi_pm_feature {
> struct intel_tpmi_pfs_entry pfs_header;
> unsigned int vsec_offset;
> + struct intel_vsec_device *vsec_dev;

Hmm... I don't know the layout of pfs_header, but this may be 4 bytes less
if you move it upper.

> };

...

> + for (count = 0; count < pfs->pfs_header.num_entries; ++count) {

> + size = pfs->pfs_header.entry_size * sizeof(u32);

You already used this once, perhaps a macro helper?
Also you can add there a comment that this comes from the trusted hw.

> + /* The size is from a trusted hardware, but verify anyway */
> + if (size > TPMI_MAX_BUFFER_SIZE) {
> + /*
> + * The next offset depends on the current size. So, can't skip to the
> + * display of the next entry. Simply return from this function with error.
> + */
> + ret = -EIO;
> + goto done_mem_show;
> + }
> +
> + buffer = kmalloc(size, GFP_KERNEL);
> + if (!buffer) {
> + ret = -ENOMEM;
> + goto done_mem_show;
> + }
> +
> + seq_printf(s, "TPMI Instance:%d offset:0x%x\n", count, off);
> +
> + mem = ioremap(off, size);
> + if (!mem) {
> + ret = -ENOMEM;
> + kfree(buffer);
> + goto done_mem_show;
> + }
> +
> + memcpy_fromio(buffer, mem, size);
> +
> + seq_hex_dump(s, " ", DUMP_PREFIX_OFFSET, row_size, sizeof(u32), buffer, size,
> + false);
> +
> + iounmap(mem);
> + kfree(buffer);
> +
> + off += size;
> + }
> +
> +done_mem_show:
> + mutex_unlock(&tpmi_dev_lock);
> +
> + return ret;
> +}

...

> + size = pfs->pfs_header.entry_size * sizeof(u32);
> + if (size > TPMI_MAX_BUFFER_SIZE)
> + return -EIO;

Again a dup even with a check.

...

> + top_dir = debugfs_create_dir(name, NULL);
> + if (IS_ERR_OR_NULL(top_dir))

I dunno if I told you, but after a discussion (elsewhere), I realized
two things:
1) this one never returns NULL;
2) even if error pointer is returned, the debugfs API is aware and
will do nothing.

Hence this conditional is redundant.

> + return;

...

> + for (i = 0; i < tpmi_info->feature_count; ++i) {

Why preincrement?

> + struct intel_tpmi_pm_feature *pfs;
> + struct dentry *dir;
> +
> + pfs = &tpmi_info->tpmi_features[i];
> + snprintf(name, sizeof(name), "tpmi-id-%02x", pfs->pfs_header.tpmi_id);
> + dir = debugfs_create_dir(name, top_dir);
> +
> + debugfs_create_file("mem_dump", 0444, dir, pfs, &tpmi_mem_dump_fops);
> + debugfs_create_file("mem_write", 0644, dir, pfs, &mem_write_ops);
> + }

--
With Best Regards,
Andy Shevchenko



2023-07-12 15:43:05

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 3/3] doc: TPMI: Add debugfs documentation

On Tue, Jul 11, 2023 at 03:09:49PM -0700, Srinivas Pandruvada wrote:
> Describe fields in the TPMI debugfs folder.

...

> +What: /sys/kernel/debug/tpmi-<n>/pfs_dump
> +Date: December 2023

November ?

> +KernelVersion: 6.6

...

> +Date: December 2023

> +Date: December 2023

--
With Best Regards,
Andy Shevchenko



2023-07-12 23:12:44

by srinivas pandruvada

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] platform/x86/intel/tpmi: Add debugfs interface

On Wed, 2023-07-12 at 18:13 +0300, Andy Shevchenko wrote:
> On Tue, Jul 11, 2023 at 03:09:48PM -0700, Srinivas Pandruvada wrote:
> > Add debugfs interface for debugging TPMI configuration and register
> > contents. This shows PFS (PM Feature structure) for each TPMI
> > device.
> >
> > For each feature, show full register contents and allow to modify
> > register at an offset.
> >
> > This debugfs interface is not present on locked down kernel with no
> > DEVMEM access and without CAP_SYS_RAWIO permission.
>
> ...
>
> >  struct intel_tpmi_pm_feature {
> >         struct intel_tpmi_pfs_entry pfs_header;
> >         unsigned int vsec_offset;
> > +       struct intel_vsec_device *vsec_dev;
>
> Hmm... I don't know the layout of pfs_header, but this may be 4 bytes
> less
> if you move it upper.
The pfs_header is packed with size of 64 bit. So size will not change.

>
> >  };
>
> ...
>
> > +       for (count = 0; count < pfs->pfs_header.num_entries;
> > ++count) {
>
> > +               size = pfs->pfs_header.entry_size * sizeof(u32);
>
> You already used this once, perhaps a macro helper?
> Also you can add there a comment that this comes from the trusted hw.
>
Added.

> > +               /* The size is from a trusted hardware, but verify
> > anyway */
> > +               if (size > TPMI_MAX_BUFFER_SIZE) {
> > +                       /*
> > +                        * The next offset depends on the current
> > size. So, can't skip to the
> > +                        * display of the next entry. Simply return
> > from this function with error.
> > +                        */
> > +                       ret = -EIO;
> > +                       goto done_mem_show;
> > +               }
> > +
> > +               buffer = kmalloc(size, GFP_KERNEL);
> > +               if (!buffer) {
> > +                       ret = -ENOMEM;
> > +                       goto done_mem_show;
> > +               }
> > +
> > +               seq_printf(s, "TPMI Instance:%d offset:0x%x\n",
> > count, off);
> > +
> > +               mem = ioremap(off, size);
> > +               if (!mem) {
> > +                       ret = -ENOMEM;
> > +                       kfree(buffer);
> > +                       goto done_mem_show;
> > +               }
> > +
> > +               memcpy_fromio(buffer, mem, size);
> > +
> > +               seq_hex_dump(s, " ", DUMP_PREFIX_OFFSET, row_size,
> > sizeof(u32), buffer, size,
> > +                            false);
> > +
> > +               iounmap(mem);
> > +               kfree(buffer);
> > +
> > +               off += size;
> > +       }
> > +
> > +done_mem_show:
> > +       mutex_unlock(&tpmi_dev_lock);
> > +
> > +       return ret;
> > +}
>
> ...
>
> > +       size = pfs->pfs_header.entry_size * sizeof(u32);
> > +       if (size > TPMI_MAX_BUFFER_SIZE)
> > +               return -EIO;
>
> Again a dup even with a check.
>
> ...
>
> > +       top_dir = debugfs_create_dir(name, NULL);
> > +       if (IS_ERR_OR_NULL(top_dir))
>
> I dunno if I told you, but after a discussion (elsewhere), I realized
> two things:
> 1) this one never returns NULL;
> 2) even if error pointer is returned, the debugfs API is aware and
>    will do nothing.
>
> Hence this conditional is redundant.
Removed that. My original version didn't check the return value.

>
> > +               return;
>
> ...
>
> > +       for (i = 0; i < tpmi_info->feature_count; ++i) {
>
> Why preincrement?
Does it matter for a "for" loop increment?

Thanks,
Srinivas
>
> > +               struct intel_tpmi_pm_feature *pfs;
> > +               struct dentry *dir;
> > +
> > +               pfs = &tpmi_info->tpmi_features[i];
> > +               snprintf(name, sizeof(name), "tpmi-id-%02x", pfs-
> > >pfs_header.tpmi_id);
> > +               dir = debugfs_create_dir(name, top_dir);
> > +
> > +               debugfs_create_file("mem_dump", 0444, dir, pfs,
> > &tpmi_mem_dump_fops);
> > +               debugfs_create_file("mem_write", 0644, dir, pfs,
> > &mem_write_ops);
> > +       }
>


2023-07-12 23:14:33

by srinivas pandruvada

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] platform/x86/intel/tpmi: Read feature control status

On Wed, 2023-07-12 at 18:05 +0300, Andy Shevchenko wrote:
> On Tue, Jul 11, 2023 at 03:09:47PM -0700, Srinivas Pandruvada wrote:
> > Some of the PM features can be locked or disabled. In that case,
> > write
> > interface can be locked.
> >
> > This status is read via a mailbox. There is one TPMI ID which
> > provides
> > base address for interface and data register for mail box
> > operation.
> > The mailbox operations is defined in the TPMI specification. Refer
> > to
> > https://github.com/intel/tpmi_power_management/ for TPMI
> > specifications.
> >
> > An API is exposed to feature drivers to read feature control
> > status.
>
> ...
>
> > +/*
> > + * TPMI PFS and per feature memory size can't exceed 4K.
> > + * Also PFS start and feature memory is 4K aligned.
> > + */
> > +#define TPMI_MAX_BUFFER_SIZE    (4 * 1024)
>
> SZ_4K from sizes.h?
>
> ...
I added a macro for size and uses sizes.h define.

>
> > +#define TPMI_CONTROL_TIMEOUT_MAX_US    USEC_PER_SEC
>
> > +#define TPMI_RB_TIMEOUT_MAX_US         USEC_PER_SEC
>
> I think it's easier to get in a form (1 * ..._SEC)
>
OK

> ...
>
> > +static int tpmi_wait_for_owner(struct intel_tpmi_info *tpmi_info,
> > u8 owner)
> > +{
> > +       u64 control;
> > +
> > +       return read_poll_timeout(readq, control,
> > +                                owner ==
> > FIELD_GET(TPMI_CONTROL_STATUS_OWNER, control),
> > +                                TPMI_CONTROL_TIMEOUT_US,
> > TPMI_CONTROL_TIMEOUT_MAX_US, false,
> > +                                tpmi_info->tpmi_control_mem +
> > TPMI_CONTROL_STATUS_OFFSET);
>
> Since you have "false" why not use readq_poll_timeout()?
>
Changed in new version

> > +}
>
> ...
>
> > +       /* Wait for Run Busy clear */
> > +       ret = read_poll_timeout(readq, control, !(control &
> > TPMI_CONTROL_STATUS_RB),
> > +                               TPMI_RB_TIMEOUT_US,
> > TPMI_RB_TIMEOUT_MAX_US, false,
> > +                               tpmi_info->tpmi_control_mem +
> > TPMI_CONTROL_STATUS_OFFSET);
>
> Ditto.
Done.

>
> > +       if (ret)
> > +               goto done_proc;
>
> ...
>
> > +       size = pfs->pfs_header.num_entries * pfs-
> > >pfs_header.entry_size * sizeof(u32);
> > +       /* This size is coming from trusted hardware, but verify
> > anyway */
>
> I would move this comment before size assignment that we already know
> that it's
> from the trusted hw.
Created a macro.

Thanks,
Srinivas

>
> > +       if (size > TPMI_MAX_BUFFER_SIZE)
> > +               return;
>


2023-07-12 23:15:34

by srinivas pandruvada

[permalink] [raw]
Subject: Re: [PATCH v2 3/3] doc: TPMI: Add debugfs documentation

On Wed, 2023-07-12 at 18:14 +0300, Andy Shevchenko wrote:
> On Tue, Jul 11, 2023 at 03:09:49PM -0700, Srinivas Pandruvada wrote:
> > Describe fields in the TPMI debugfs folder.
>
> ...
>
> > +What:          /sys/kernel/debug/tpmi-<n>/pfs_dump
> > +Date:          December 2023
>
> November ?
>
Changed.


Thanks,
Srinivas

> > +KernelVersion: 6.6
>
> ...
>
> > +Date:          December 2023
>
> > +Date:          December 2023
>


2023-07-13 16:58:31

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] platform/x86/intel/tpmi: Add debugfs interface

On Wed, Jul 12, 2023 at 04:06:50PM -0700, srinivas pandruvada wrote:
> On Wed, 2023-07-12 at 18:13 +0300, Andy Shevchenko wrote:
> > On Tue, Jul 11, 2023 at 03:09:48PM -0700, Srinivas Pandruvada wrote:

...

> > > ?struct intel_tpmi_pm_feature {
> > > ????????struct intel_tpmi_pfs_entry pfs_header;
> > > ????????unsigned int vsec_offset;
> > > +???????struct intel_vsec_device *vsec_dev;
> >
> > Hmm... I don't know the layout of pfs_header, but this may be 4 bytes
> > less
> > if you move it upper.
> The pfs_header is packed with size of 64 bit. So size will not change.

So, it will be a gap of 4 bytes due to alignment, no?

> > > ?};

...

> > > +???????for (i = 0; i < tpmi_info->feature_count; ++i) {
> >
> > Why preincrement?
> Does it matter for a "for" loop increment?

Stylewise. Preincrement raises a flag to the reader "what the heck is special
here that we need preincrement". If not required, I would use postincrement.

--
With Best Regards,
Andy Shevchenko