Linux Documentation
 help / color / mirror / Atom feed
* [PATCH v14 01/13] dt-bindings: ingenic: Add DT bindings for TCU clocks
From: Paul Cercueil @ 2019-07-01 15:13 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek, Rob Herring
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

This header provides clock numbers for the ingenic,tcu
DT binding.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
Reviewed-by: Rob Herring <robh@kernel.org>
Acked-by: Stephen Boyd <sboyd@kernel.org>
---

Notes:
    v2: Use SPDX identifier for the license
    
    v3/v4: No change
    
    v5: s/JZ47*_/TCU_/ and dropped *_CLK_LAST defines
    
    v6-v14: No change

 include/dt-bindings/clock/ingenic,tcu.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 include/dt-bindings/clock/ingenic,tcu.h

diff --git a/include/dt-bindings/clock/ingenic,tcu.h b/include/dt-bindings/clock/ingenic,tcu.h
new file mode 100644
index 000000000000..d569650a7945
--- /dev/null
+++ b/include/dt-bindings/clock/ingenic,tcu.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * This header provides clock numbers for the ingenic,tcu DT binding.
+ */
+
+#ifndef __DT_BINDINGS_CLOCK_INGENIC_TCU_H__
+#define __DT_BINDINGS_CLOCK_INGENIC_TCU_H__
+
+#define TCU_CLK_TIMER0	0
+#define TCU_CLK_TIMER1	1
+#define TCU_CLK_TIMER2	2
+#define TCU_CLK_TIMER3	3
+#define TCU_CLK_TIMER4	4
+#define TCU_CLK_TIMER5	5
+#define TCU_CLK_TIMER6	6
+#define TCU_CLK_TIMER7	7
+#define TCU_CLK_WDT	8
+#define TCU_CLK_OST	9
+
+#endif /* __DT_BINDINGS_CLOCK_INGENIC_TCU_H__ */
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 07/13] clocksource: Add a new timer-ingenic driver
From: Paul Cercueil @ 2019-07-01 15:14 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

This driver handles the TCU (Timer Counter Unit) present on the Ingenic
JZ47xx SoCs, and provides the kernel with a system timer, a clocksource
and a sched_clock.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
---

Notes:
    v2: Use SPDX identifier for the license
    
    v3: - Move documentation to its own patch
    	- Search the devicetree for PWM clients, and use all the TCU
    	  channels that won't be used for PWM
    
    v4: - Add documentation about why we search for PWM clients
    	- Verify that the PWM clients are for the TCU PWM driver
    
    v5: Major overhaul. Too many changes to list. Consider it's a new
    	patch.
    
    v6: - Add two API functions ingenic_tcu_request_channel and
    	  ingenic_tcu_release_channel. To be used by the PWM driver to
    	  request the use of a TCU channel. The driver will now dynamically
    	  move away the system timer or clocksource to a new TCU channel.
    	- The system timer now defaults to channel 0, the clocksource now
    	  defaults to channel 1 and is no more optional. The
    	  ingenic,timer-channel and ingenic,clocksource-channel devicetree
    	  properties are now gone.
    	- Fix round_rate / set_rate not calculating the prescale divider
    	  the same way. This caused problems when (parent_rate / div) would
    	  give a non-integer result. The behaviour is correct now.
    	- The clocksource clock is turned off on suspend now.
    
    v7: Fix section mismatch by using builtin_platform_driver_probe()
    
    v8: - Removed ingenic_tcu_[request,release]_channel, and the mechanism
    	  to dynamically change the TCU channel of the system timer or
    	  the clocksource.
    	- The driver's devicetree node can now have two more children
    	  nodes, that correspond to the system timer and clocksource.
    	  For these two, the driver will use the TCU timer that
    	  correspond to the memory resource supplied in their
    	  respective node.
    
    v9: - Removed support for clocksource / timer children devicetree
    	  nodes. Now, we use a property "ingenic,pwm-channels-mask" to
    	  know which PWM channels are reserved for PWM use and should
    	  not be used as OS timers.
    
    v10: - Use CLK_SET_RATE_UNGATE instead of CLK_SET_RATE_GATE + manually
    	   un-gating the clock before changing rate. Same for re-parenting.
    	 - Unconditionally create the clocksource and sched_clock even if
    	   the SoC possesses a OS Timer. That gives the choice back to the
    	   user which clocksource should be selected.
    	 - Use subsys_initcall() instead of builtin_platform_driver_probe().
    	   The OS Timer driver calls builtin_platform_driver_probe, which
    	   requires the device to be created before that.
    	 - Cosmetic cleanups
    
    v11: - Change prototype of exported function
    	   ingenic_tcu_pwm_can_use_chn(), use a struct device * as first
    	   argument.
    	 - Read clocksource using the regmap instead of bypassing it.
    	   Bypassing the regmap makes sense only for the sched_clock where
    	   the read operation must be as fast as possible.
    	 - Fix incorrect format in pr_crit() macro
    
    v12: - Clock handling and IRQ handling are gone, and are now handled
    	   in their own driver.
    	 - Obtain regmap from the ingenic-tcu MFD driver. As a result, we
    	   cannot bypass the regmap anymore for the sched_clock.
    
    v13: No change
    
    v14: Remove empty lines in structure definitions

 drivers/clocksource/Kconfig         |  11 +
 drivers/clocksource/Makefile        |   1 +
 drivers/clocksource/ingenic-timer.c | 355 ++++++++++++++++++++++++++++
 3 files changed, 367 insertions(+)
 create mode 100644 drivers/clocksource/ingenic-timer.c

diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
index 3300739edce4..2d911c15db70 100644
--- a/drivers/clocksource/Kconfig
+++ b/drivers/clocksource/Kconfig
@@ -673,4 +673,15 @@ config MILBEAUT_TIMER
 	help
 	  Enables the support for Milbeaut timer driver.
 
+config INGENIC_TIMER
+	bool "Clocksource/timer using the TCU in Ingenic JZ SoCs"
+	default MACH_INGENIC
+	depends on MIPS || COMPILE_TEST
+	depends on COMMON_CLK
+	select INGENIC_TCU
+	select TIMER_OF
+	select IRQ_DOMAIN
+	help
+	  Support for the timer/counter unit of the Ingenic JZ SoCs.
+
 endmenu
diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile
index 236858fa7fbf..553f3c61717a 100644
--- a/drivers/clocksource/Makefile
+++ b/drivers/clocksource/Makefile
@@ -78,6 +78,7 @@ obj-$(CONFIG_ASM9260_TIMER)		+= asm9260_timer.o
 obj-$(CONFIG_H8300_TMR8)		+= h8300_timer8.o
 obj-$(CONFIG_H8300_TMR16)		+= h8300_timer16.o
 obj-$(CONFIG_H8300_TPU)			+= h8300_tpu.o
+obj-$(CONFIG_INGENIC_TIMER)		+= ingenic-timer.o
 obj-$(CONFIG_CLKSRC_ST_LPC)		+= clksrc_st_lpc.o
 obj-$(CONFIG_X86_NUMACHIP)		+= numachip.o
 obj-$(CONFIG_ATCPIT100_TIMER)		+= timer-atcpit100.o
diff --git a/drivers/clocksource/ingenic-timer.c b/drivers/clocksource/ingenic-timer.c
new file mode 100644
index 000000000000..f3b4d55b46fd
--- /dev/null
+++ b/drivers/clocksource/ingenic-timer.c
@@ -0,0 +1,355 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * JZ47xx SoCs TCU IRQ driver
+ * Copyright (C) 2019 Paul Cercueil <paul@crapouillou.net>
+ */
+
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/clockchips.h>
+#include <linux/clocksource.h>
+#include <linux/interrupt.h>
+#include <linux/mfd/ingenic-tcu.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_irq.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/sched_clock.h>
+
+#include <dt-bindings/clock/ingenic,tcu.h>
+
+struct ingenic_soc_info {
+	unsigned int num_channels;
+};
+
+struct ingenic_tcu {
+	struct regmap *map;
+	struct clk *timer_clk, *cs_clk;
+	unsigned int timer_channel, cs_channel;
+	struct clock_event_device cevt;
+	struct clocksource cs;
+	char name[4];
+	unsigned long pwm_channels_mask;
+};
+
+static struct ingenic_tcu *ingenic_tcu;
+
+static u64 notrace ingenic_tcu_timer_read(void)
+{
+	struct ingenic_tcu *tcu = ingenic_tcu;
+	unsigned int count;
+
+	regmap_read(tcu->map, TCU_REG_TCNTc(tcu->cs_channel), &count);
+
+	return count;
+}
+
+static u64 notrace ingenic_tcu_timer_cs_read(struct clocksource *cs)
+{
+	return ingenic_tcu_timer_read();
+}
+
+static inline struct ingenic_tcu *to_ingenic_tcu(struct clock_event_device *evt)
+{
+	return container_of(evt, struct ingenic_tcu, cevt);
+}
+
+static int ingenic_tcu_cevt_set_state_shutdown(struct clock_event_device *evt)
+{
+	struct ingenic_tcu *tcu = to_ingenic_tcu(evt);
+
+	regmap_write(tcu->map, TCU_REG_TECR, BIT(tcu->timer_channel));
+
+	return 0;
+}
+
+static int ingenic_tcu_cevt_set_next(unsigned long next,
+				     struct clock_event_device *evt)
+{
+	struct ingenic_tcu *tcu = to_ingenic_tcu(evt);
+
+	if (next > 0xffff)
+		return -EINVAL;
+
+	regmap_write(tcu->map, TCU_REG_TDFRc(tcu->timer_channel), next);
+	regmap_write(tcu->map, TCU_REG_TCNTc(tcu->timer_channel), 0);
+	regmap_write(tcu->map, TCU_REG_TESR, BIT(tcu->timer_channel));
+
+	return 0;
+}
+
+static irqreturn_t ingenic_tcu_cevt_cb(int irq, void *dev_id)
+{
+	struct clock_event_device *evt = dev_id;
+	struct ingenic_tcu *tcu = to_ingenic_tcu(evt);
+
+	regmap_write(tcu->map, TCU_REG_TECR, BIT(tcu->timer_channel));
+
+	if (evt->event_handler)
+		evt->event_handler(evt);
+
+	return IRQ_HANDLED;
+}
+
+static struct clk * __init ingenic_tcu_get_clock(struct device_node *np, int id)
+{
+	struct of_phandle_args args;
+
+	args.np = np;
+	args.args_count = 1;
+	args.args[0] = id;
+
+	return of_clk_get_from_provider(&args);
+}
+
+static int __init ingenic_tcu_timer_init(struct device_node *np,
+					 struct ingenic_tcu *tcu)
+{
+	unsigned int timer_virq, channel = tcu->timer_channel;
+	struct irq_domain *domain;
+	unsigned long rate;
+	int err;
+
+	tcu->timer_clk = ingenic_tcu_get_clock(np, channel);
+	if (IS_ERR(tcu->timer_clk))
+		return PTR_ERR(tcu->timer_clk);
+
+	err = clk_prepare_enable(tcu->timer_clk);
+	if (err)
+		goto err_clk_put;
+
+	rate = clk_get_rate(tcu->timer_clk);
+	if (!rate) {
+		err = -EINVAL;
+		goto err_clk_disable;
+	}
+
+	domain = irq_find_host(np);
+	if (!domain) {
+		err = -ENODEV;
+		goto err_clk_disable;
+	}
+
+	timer_virq = irq_create_mapping(domain, channel);
+	if (!timer_virq) {
+		err = -EINVAL;
+		goto err_clk_disable;
+	}
+
+	snprintf(tcu->name, sizeof(tcu->name), "TCU");
+
+	err = request_irq(timer_virq, ingenic_tcu_cevt_cb, IRQF_TIMER,
+			  tcu->name, &tcu->cevt);
+	if (err)
+		goto err_irq_dispose_mapping;
+
+	tcu->cevt.cpumask = cpumask_of(smp_processor_id());
+	tcu->cevt.features = CLOCK_EVT_FEAT_ONESHOT;
+	tcu->cevt.name = tcu->name;
+	tcu->cevt.rating = 200;
+	tcu->cevt.set_state_shutdown = ingenic_tcu_cevt_set_state_shutdown;
+	tcu->cevt.set_next_event = ingenic_tcu_cevt_set_next;
+
+	clockevents_config_and_register(&tcu->cevt, rate, 10, 0xffff);
+
+	return 0;
+
+err_irq_dispose_mapping:
+	irq_dispose_mapping(timer_virq);
+err_clk_disable:
+	clk_disable_unprepare(tcu->timer_clk);
+err_clk_put:
+	clk_put(tcu->timer_clk);
+	return err;
+}
+
+static int __init ingenic_tcu_clocksource_init(struct device_node *np,
+					       struct ingenic_tcu *tcu)
+{
+	unsigned int channel = tcu->cs_channel;
+	struct clocksource *cs = &tcu->cs;
+	unsigned long rate;
+	int err;
+
+	tcu->cs_clk = ingenic_tcu_get_clock(np, channel);
+	if (IS_ERR(tcu->cs_clk))
+		return PTR_ERR(tcu->cs_clk);
+
+	err = clk_prepare_enable(tcu->cs_clk);
+	if (err)
+		goto err_clk_put;
+
+	rate = clk_get_rate(tcu->cs_clk);
+	if (!rate) {
+		err = -EINVAL;
+		goto err_clk_disable;
+	}
+
+	/* Reset channel */
+	regmap_update_bits(tcu->map, TCU_REG_TCSRc(channel),
+			   0xffff & ~TCU_TCSR_RESERVED_BITS, 0);
+
+	/* Reset counter */
+	regmap_write(tcu->map, TCU_REG_TDFRc(channel), 0xffff);
+	regmap_write(tcu->map, TCU_REG_TCNTc(channel), 0);
+
+	/* Enable channel */
+	regmap_write(tcu->map, TCU_REG_TESR, BIT(channel));
+
+	cs->name = "ingenic-timer";
+	cs->rating = 200;
+	cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
+	cs->mask = CLOCKSOURCE_MASK(16);
+	cs->read = ingenic_tcu_timer_cs_read;
+
+	err = clocksource_register_hz(cs, rate);
+	if (err)
+		goto err_clk_disable;
+
+	return 0;
+
+err_clk_disable:
+	clk_disable_unprepare(tcu->cs_clk);
+err_clk_put:
+	clk_put(tcu->cs_clk);
+	return err;
+}
+
+static const struct ingenic_soc_info jz4740_soc_info = {
+	.num_channels = 8,
+};
+
+static const struct ingenic_soc_info jz4725b_soc_info = {
+	.num_channels = 6,
+};
+
+static const struct of_device_id ingenic_tcu_of_match[] = {
+	{ .compatible = "ingenic,jz4740-tcu", .data = &jz4740_soc_info, },
+	{ .compatible = "ingenic,jz4725b-tcu", .data = &jz4725b_soc_info, },
+	{ .compatible = "ingenic,jz4770-tcu", .data = &jz4740_soc_info, },
+	{ }
+};
+
+static int __init ingenic_tcu_init(struct device_node *np)
+{
+	const struct of_device_id *id = of_match_node(ingenic_tcu_of_match, np);
+	const struct ingenic_soc_info *soc_info = id->data;
+	struct ingenic_tcu *tcu;
+	struct regmap *map;
+	long rate;
+	int ret;
+
+	of_node_clear_flag(np, OF_POPULATED);
+
+	map = ingenic_tcu_get_regmap(np);
+	if (IS_ERR(map))
+		return PTR_ERR(map);
+
+	tcu = kzalloc(sizeof(*tcu), GFP_KERNEL);
+	if (!tcu)
+		return -ENOMEM;
+
+	/* Enable all TCU channels for PWM use by default except channels 0/1 */
+	tcu->pwm_channels_mask = GENMASK(soc_info->num_channels - 1, 2);
+	of_property_read_u32(np, "ingenic,pwm-channels-mask",
+			     (u32 *)&tcu->pwm_channels_mask);
+
+	/* Verify that we have at least two free channels */
+	if (hweight8(tcu->pwm_channels_mask) > soc_info->num_channels - 2) {
+		pr_crit("%s: Invalid PWM channel mask: 0x%02lx\n", __func__,
+			tcu->pwm_channels_mask);
+		ret = -EINVAL;
+		goto err_free_ingenic_tcu;
+	}
+
+	tcu->map = map;
+	ingenic_tcu = tcu;
+
+	tcu->timer_channel = find_first_zero_bit(&tcu->pwm_channels_mask,
+						 soc_info->num_channels);
+	tcu->cs_channel = find_next_zero_bit(&tcu->pwm_channels_mask,
+					     soc_info->num_channels,
+					     tcu->timer_channel + 1);
+
+	ret = ingenic_tcu_clocksource_init(np, tcu);
+	if (ret) {
+		pr_crit("%s: Unable to init clocksource: %d\n", __func__, ret);
+		goto err_free_ingenic_tcu;
+	}
+
+	ret = ingenic_tcu_timer_init(np, tcu);
+	if (ret)
+		goto err_tcu_clocksource_cleanup;
+
+	/* Register the sched_clock at the end as there's no way to undo it */
+	rate = clk_get_rate(tcu->cs_clk);
+	sched_clock_register(ingenic_tcu_timer_read, 16, rate);
+
+	return 0;
+
+err_tcu_clocksource_cleanup:
+	clocksource_unregister(&tcu->cs);
+	clk_disable_unprepare(tcu->cs_clk);
+	clk_put(tcu->cs_clk);
+err_free_ingenic_tcu:
+	kfree(tcu);
+	return ret;
+}
+
+TIMER_OF_DECLARE(jz4740_tcu_intc,  "ingenic,jz4740-tcu",  ingenic_tcu_init);
+TIMER_OF_DECLARE(jz4725b_tcu_intc, "ingenic,jz4725b-tcu", ingenic_tcu_init);
+TIMER_OF_DECLARE(jz4770_tcu_intc,  "ingenic,jz4770-tcu",  ingenic_tcu_init);
+
+
+static int __init ingenic_tcu_probe(struct platform_device *pdev)
+{
+	platform_set_drvdata(pdev, ingenic_tcu);
+
+	return 0;
+}
+
+static int __maybe_unused ingenic_tcu_suspend(struct device *dev)
+{
+	struct ingenic_tcu *tcu = dev_get_drvdata(dev);
+
+	clk_disable(tcu->cs_clk);
+	clk_disable(tcu->timer_clk);
+	return 0;
+}
+
+static int __maybe_unused ingenic_tcu_resume(struct device *dev)
+{
+	struct ingenic_tcu *tcu = dev_get_drvdata(dev);
+	int ret;
+
+	ret = clk_enable(tcu->timer_clk);
+	if (ret)
+		return ret;
+
+	ret = clk_enable(tcu->cs_clk);
+	if (ret) {
+		clk_disable(tcu->timer_clk);
+		return ret;
+	}
+
+	return 0;
+}
+
+static const struct dev_pm_ops __maybe_unused ingenic_tcu_pm_ops = {
+	/* _noirq: We want the TCU clocks to be gated last / ungated first */
+	.suspend_noirq = ingenic_tcu_suspend,
+	.resume_noirq  = ingenic_tcu_resume,
+};
+
+static struct platform_driver ingenic_tcu_driver = {
+	.driver = {
+		.name	= "ingenic-tcu-timer",
+#ifdef CONFIG_PM_SLEEP
+		.pm	= &ingenic_tcu_pm_ops,
+#endif
+		.of_match_table = ingenic_tcu_of_match,
+	},
+};
+builtin_platform_driver_probe(ingenic_tcu_driver, ingenic_tcu_probe);
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 08/13] clk: jz4740: Add TCU clock
From: Paul Cercueil @ 2019-07-01 15:14 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek, Rob Herring
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

Add the missing TCU clock to the list of clocks supplied by the CGU for
the JZ4740 SoC.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
Acked-by: Stephen Boyd <sboyd@kernel.org>
Acked-by: Rob Herring <robh@kernel.org>
---

Notes:
    v5: New patch
    
    v6-v14: No change

 drivers/clk/ingenic/jz4740-cgu.c       | 6 ++++++
 include/dt-bindings/clock/jz4740-cgu.h | 1 +
 2 files changed, 7 insertions(+)

diff --git a/drivers/clk/ingenic/jz4740-cgu.c b/drivers/clk/ingenic/jz4740-cgu.c
index c77f4e1506dc..176d911b5839 100644
--- a/drivers/clk/ingenic/jz4740-cgu.c
+++ b/drivers/clk/ingenic/jz4740-cgu.c
@@ -203,6 +203,12 @@ static const struct ingenic_cgu_clk_info jz4740_cgu_clocks[] = {
 		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
 		.gate = { CGU_REG_CLKGR, 5 },
 	},
+
+	[JZ4740_CLK_TCU] = {
+		"tcu", CGU_CLK_GATE,
+		.parents = { JZ4740_CLK_EXT, -1, -1, -1 },
+		.gate = { CGU_REG_CLKGR, 1 },
+	},
 };
 
 static void __init jz4740_cgu_init(struct device_node *np)
diff --git a/include/dt-bindings/clock/jz4740-cgu.h b/include/dt-bindings/clock/jz4740-cgu.h
index 6ed83f926ae7..e82d77028581 100644
--- a/include/dt-bindings/clock/jz4740-cgu.h
+++ b/include/dt-bindings/clock/jz4740-cgu.h
@@ -34,5 +34,6 @@
 #define JZ4740_CLK_ADC		19
 #define JZ4740_CLK_I2C		20
 #define JZ4740_CLK_AIC		21
+#define JZ4740_CLK_TCU		22
 
 #endif /* __DT_BINDINGS_CLOCK_JZ4740_CGU_H__ */
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 10/13] MIPS: qi_lb60: Reduce system timer and clocksource to 750 kHz
From: Paul Cercueil @ 2019-07-01 15:14 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

The default clock (12 MHz) is too fast for the system timer, which fails
to report time accurately.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
---

Notes:
    v5: New patch
    
    v6: Remove ingenic,clocksource-channel property
    
    v7-v14: No change

 arch/mips/boot/dts/ingenic/qi_lb60.dts | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/mips/boot/dts/ingenic/qi_lb60.dts b/arch/mips/boot/dts/ingenic/qi_lb60.dts
index 76aaf8982554..01b8c917cb33 100644
--- a/arch/mips/boot/dts/ingenic/qi_lb60.dts
+++ b/arch/mips/boot/dts/ingenic/qi_lb60.dts
@@ -2,6 +2,7 @@
 /dts-v1/;
 
 #include "jz4740.dtsi"
+#include <dt-bindings/clock/ingenic,tcu.h>
 
 / {
 	compatible = "qi,lb60", "ingenic,jz4740";
@@ -31,3 +32,9 @@
 		bias-disable;
 	};
 };
+
+&tcu {
+	/* 750 kHz for the system timer and clocksource */
+	assigned-clocks = <&tcu TCU_CLK_TIMER0>, <&tcu TCU_CLK_TIMER1>;
+	assigned-clock-rates = <750000>, <750000>;
+};
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 11/13] MIPS: CI20: Reduce system timer and clocksource to 3 MHz
From: Paul Cercueil @ 2019-07-01 15:14 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

The default clock (48 MHz) is too fast for the system timer.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
---

Notes:
    v5: New patch
    
    v6: Set also the rate for the clocksource channel's clock
    
    v7: No change
    
    v8: No change
    
    v9: Don't configure clock timer1, as the OS Timer is used as
    	clocksource on this SoC
    
    v10: Revert back to v8 bahaviour. Let the user choose what
    	 clocksource should be used.
    
    v11-v14: No change

 arch/mips/boot/dts/ingenic/ci20.dts | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/arch/mips/boot/dts/ingenic/ci20.dts b/arch/mips/boot/dts/ingenic/ci20.dts
index 4f7b1fa31cf5..2e9952311ecd 100644
--- a/arch/mips/boot/dts/ingenic/ci20.dts
+++ b/arch/mips/boot/dts/ingenic/ci20.dts
@@ -2,6 +2,7 @@
 /dts-v1/;
 
 #include "jz4780.dtsi"
+#include <dt-bindings/clock/ingenic,tcu.h>
 #include <dt-bindings/gpio/gpio.h>
 
 / {
@@ -238,3 +239,9 @@
 		bias-disable;
 	};
 };
+
+&tcu {
+	/* 3 MHz for the system timer and clocksource */
+	assigned-clocks = <&tcu TCU_CLK_TIMER0>, <&tcu TCU_CLK_TIMER1>;
+	assigned-clock-rates = <3000000>, <3000000>;
+};
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 12/13] MIPS: GCW0: Reduce system timer and clocksource to 750 kHz
From: Paul Cercueil @ 2019-07-01 15:14 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

The default clock (12 MHz) is too fast for the system timer.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
---

Notes:
    v8: New patch
    
    v9: Don't configure clock timer1, as the OS Timer is used as
    	clocksource on this SoC
    
    v10: Revert back to v8 bahaviour. Let the user choose what
    	 clocksource should be used.
    
    v11: No change
    
    v12: Move clocksource to channel 2, as channel 1 is used as PWM
    	 for the backlight.
    
    v13-v14: No change

 arch/mips/boot/dts/ingenic/gcw0.dts | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/mips/boot/dts/ingenic/gcw0.dts b/arch/mips/boot/dts/ingenic/gcw0.dts
index 35f0291e8d38..f58d239c2058 100644
--- a/arch/mips/boot/dts/ingenic/gcw0.dts
+++ b/arch/mips/boot/dts/ingenic/gcw0.dts
@@ -2,6 +2,7 @@
 /dts-v1/;
 
 #include "jz4770.dtsi"
+#include <dt-bindings/clock/ingenic,tcu.h>
 
 / {
 	compatible = "gcw,zero", "ingenic,jz4770";
@@ -60,3 +61,12 @@
 	/* The WiFi module is connected to the UHC. */
 	status = "okay";
 };
+
+&tcu {
+	/* 750 kHz for the system timer and clocksource */
+	assigned-clocks = <&tcu TCU_CLK_TIMER0>, <&tcu TCU_CLK_TIMER2>;
+	assigned-clock-rates = <750000>, <750000>;
+
+	/* PWM1 is in use, so reserve channel #2 for the clocksource */
+	ingenic,pwm-channels-mask = <0xfa>;
+};
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 13/13] MIPS: jz4740: Drop obsolete code
From: Paul Cercueil @ 2019-07-01 15:14 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

The old clocksource/timer platform code is now obsoleted by the newly
introduced TCU drivers.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
---

Notes:
    v5: New patch
    
    v6-v11: No change
    
    v12: Only remove clocksource code. The rest will eventually be
    	 removed in a future patchset when the PWM/watchdog drivers
    	 are updated.
    
    v13-v14: No change

 arch/mips/jz4740/time.c | 154 +---------------------------------------
 1 file changed, 2 insertions(+), 152 deletions(-)

diff --git a/arch/mips/jz4740/time.c b/arch/mips/jz4740/time.c
index a3260c754e65..5476899f0882 100644
--- a/arch/mips/jz4740/time.c
+++ b/arch/mips/jz4740/time.c
@@ -4,164 +4,14 @@
  *  JZ4740 platform time support
  */
 
-#include <linux/clk.h>
 #include <linux/clk-provider.h>
-#include <linux/interrupt.h>
-#include <linux/kernel.h>
-#include <linux/time.h>
+#include <linux/clocksource.h>
 
-#include <linux/clockchips.h>
-#include <linux/sched_clock.h>
-
-#include <asm/mach-jz4740/clock.h>
-#include <asm/mach-jz4740/irq.h>
 #include <asm/mach-jz4740/timer.h>
-#include <asm/time.h>
-
-#include "clock.h"
-
-#define TIMER_CLOCKEVENT 0
-#define TIMER_CLOCKSOURCE 1
-
-static uint16_t jz4740_jiffies_per_tick;
-
-static u64 jz4740_clocksource_read(struct clocksource *cs)
-{
-	return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
-}
-
-static struct clocksource jz4740_clocksource = {
-	.name = "jz4740-timer",
-	.rating = 200,
-	.read = jz4740_clocksource_read,
-	.mask = CLOCKSOURCE_MASK(16),
-	.flags = CLOCK_SOURCE_IS_CONTINUOUS,
-};
-
-static u64 notrace jz4740_read_sched_clock(void)
-{
-	return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
-}
-
-static irqreturn_t jz4740_clockevent_irq(int irq, void *devid)
-{
-	struct clock_event_device *cd = devid;
-
-	jz4740_timer_ack_full(TIMER_CLOCKEVENT);
-
-	if (!clockevent_state_periodic(cd))
-		jz4740_timer_disable(TIMER_CLOCKEVENT);
-
-	cd->event_handler(cd);
-
-	return IRQ_HANDLED;
-}
-
-static int jz4740_clockevent_set_periodic(struct clock_event_device *evt)
-{
-	jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
-	jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
-	jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
-	jz4740_timer_enable(TIMER_CLOCKEVENT);
-
-	return 0;
-}
-
-static int jz4740_clockevent_resume(struct clock_event_device *evt)
-{
-	jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
-	jz4740_timer_enable(TIMER_CLOCKEVENT);
-
-	return 0;
-}
-
-static int jz4740_clockevent_shutdown(struct clock_event_device *evt)
-{
-	jz4740_timer_disable(TIMER_CLOCKEVENT);
-
-	return 0;
-}
-
-static int jz4740_clockevent_set_next(unsigned long evt,
-	struct clock_event_device *cd)
-{
-	jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
-	jz4740_timer_set_period(TIMER_CLOCKEVENT, evt);
-	jz4740_timer_enable(TIMER_CLOCKEVENT);
-
-	return 0;
-}
-
-static struct clock_event_device jz4740_clockevent = {
-	.name = "jz4740-timer",
-	.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
-	.set_next_event = jz4740_clockevent_set_next,
-	.set_state_shutdown = jz4740_clockevent_shutdown,
-	.set_state_periodic = jz4740_clockevent_set_periodic,
-	.set_state_oneshot = jz4740_clockevent_shutdown,
-	.tick_resume = jz4740_clockevent_resume,
-	.rating = 200,
-#ifdef CONFIG_MACH_JZ4740
-	.irq = JZ4740_IRQ_TCU0,
-#endif
-#if defined(CONFIG_MACH_JZ4770) || defined(CONFIG_MACH_JZ4780)
-	.irq = JZ4780_IRQ_TCU2,
-#endif
-};
-
-static struct irqaction timer_irqaction = {
-	.handler	= jz4740_clockevent_irq,
-	.flags		= IRQF_PERCPU | IRQF_TIMER,
-	.name		= "jz4740-timerirq",
-	.dev_id		= &jz4740_clockevent,
-};
 
 void __init plat_time_init(void)
 {
-	int ret;
-	uint32_t clk_rate;
-	uint16_t ctrl;
-	struct clk *ext_clk;
-
 	of_clk_init(NULL);
 	jz4740_timer_init();
-
-	ext_clk = clk_get(NULL, "ext");
-	if (IS_ERR(ext_clk))
-		panic("unable to get ext clock");
-	clk_rate = clk_get_rate(ext_clk) >> 4;
-	clk_put(ext_clk);
-
-	jz4740_jiffies_per_tick = DIV_ROUND_CLOSEST(clk_rate, HZ);
-
-	clockevent_set_clock(&jz4740_clockevent, clk_rate);
-	jz4740_clockevent.min_delta_ns = clockevent_delta2ns(100, &jz4740_clockevent);
-	jz4740_clockevent.min_delta_ticks = 100;
-	jz4740_clockevent.max_delta_ns = clockevent_delta2ns(0xffff, &jz4740_clockevent);
-	jz4740_clockevent.max_delta_ticks = 0xffff;
-	jz4740_clockevent.cpumask = cpumask_of(0);
-
-	clockevents_register_device(&jz4740_clockevent);
-
-	ret = clocksource_register_hz(&jz4740_clocksource, clk_rate);
-
-	if (ret)
-		printk(KERN_ERR "Failed to register clocksource: %d\n", ret);
-
-	sched_clock_register(jz4740_read_sched_clock, 16, clk_rate);
-
-	setup_irq(jz4740_clockevent.irq, &timer_irqaction);
-
-	ctrl = JZ_TIMER_CTRL_PRESCALE_16 | JZ_TIMER_CTRL_SRC_EXT;
-
-	jz4740_timer_set_ctrl(TIMER_CLOCKEVENT, ctrl);
-	jz4740_timer_set_ctrl(TIMER_CLOCKSOURCE, ctrl);
-
-	jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
-	jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
-
-	jz4740_timer_set_period(TIMER_CLOCKSOURCE, 0xffff);
-
-	jz4740_timer_enable(TIMER_CLOCKEVENT);
-	jz4740_timer_enable(TIMER_CLOCKSOURCE);
+	timer_probe();
 }
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 09/13] MIPS: jz4740: Add DTS nodes for the TCU drivers
From: Paul Cercueil @ 2019-07-01 15:14 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

Add DTS nodes for the JZ4780, JZ4770 and JZ4740 devicetree files.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
---

Notes:
    v5: New patch
    
    v6: Fix register lengths in watchdog/pwm nodes
    
    v7: No change
    
    v8: - Fix wrong start address for PWM node
    	- Add system timer and clocksource sub-nodes
    
    v9: Drop timer and clocksource sub-nodes
    
    v10-v11: No change
    
    v12: Drop PWM/watchdog/OST sub-nodes, for now.
    
    v13-v14: No change

 arch/mips/boot/dts/ingenic/jz4740.dtsi | 22 ++++++++++++++++++++++
 arch/mips/boot/dts/ingenic/jz4770.dtsi | 21 +++++++++++++++++++++
 arch/mips/boot/dts/ingenic/jz4780.dtsi | 21 +++++++++++++++++++++
 3 files changed, 64 insertions(+)

diff --git a/arch/mips/boot/dts/ingenic/jz4740.dtsi b/arch/mips/boot/dts/ingenic/jz4740.dtsi
index 2beb78a62b7d..807d9702d4cf 100644
--- a/arch/mips/boot/dts/ingenic/jz4740.dtsi
+++ b/arch/mips/boot/dts/ingenic/jz4740.dtsi
@@ -53,6 +53,28 @@
 		clock-names = "rtc";
 	};
 
+	tcu: timer@10002000 {
+		compatible = "ingenic,jz4740-tcu";
+		reg = <0x10002000 0x1000>;
+		#address-cells = <1>;
+		#size-cells = <1>;
+		ranges = <0x0 0x10002000 0x1000>;
+
+		#clock-cells = <1>;
+
+		clocks = <&cgu JZ4740_CLK_RTC
+			  &cgu JZ4740_CLK_EXT
+			  &cgu JZ4740_CLK_PCLK
+			  &cgu JZ4740_CLK_TCU>;
+		clock-names = "rtc", "ext", "pclk", "tcu";
+
+		interrupt-controller;
+		#interrupt-cells = <1>;
+
+		interrupt-parent = <&intc>;
+		interrupts = <23 22 21>;
+	};
+
 	rtc_dev: rtc@10003000 {
 		compatible = "ingenic,jz4740-rtc";
 		reg = <0x10003000 0x40>;
diff --git a/arch/mips/boot/dts/ingenic/jz4770.dtsi b/arch/mips/boot/dts/ingenic/jz4770.dtsi
index 49ede6c14ff3..70932fd90902 100644
--- a/arch/mips/boot/dts/ingenic/jz4770.dtsi
+++ b/arch/mips/boot/dts/ingenic/jz4770.dtsi
@@ -46,6 +46,27 @@
 		#clock-cells = <1>;
 	};
 
+	tcu: timer@10002000 {
+		compatible = "ingenic,jz4770-tcu";
+		reg = <0x10002000 0x1000>;
+		#address-cells = <1>;
+		#size-cells = <1>;
+		ranges = <0x0 0x10002000 0x1000>;
+
+		#clock-cells = <1>;
+
+		clocks = <&cgu JZ4770_CLK_RTC
+			  &cgu JZ4770_CLK_EXT
+			  &cgu JZ4770_CLK_PCLK>;
+		clock-names = "rtc", "ext", "pclk";
+
+		interrupt-controller;
+		#interrupt-cells = <1>;
+
+		interrupt-parent = <&intc>;
+		interrupts = <27 26 25>;
+	};
+
 	pinctrl: pin-controller@10010000 {
 		compatible = "ingenic,jz4770-pinctrl";
 		reg = <0x10010000 0x600>;
diff --git a/arch/mips/boot/dts/ingenic/jz4780.dtsi b/arch/mips/boot/dts/ingenic/jz4780.dtsi
index b03cdec56de9..495082ce7fc5 100644
--- a/arch/mips/boot/dts/ingenic/jz4780.dtsi
+++ b/arch/mips/boot/dts/ingenic/jz4780.dtsi
@@ -46,6 +46,27 @@
 		#clock-cells = <1>;
 	};
 
+	tcu: timer@10002000 {
+		compatible = "ingenic,jz4770-tcu";
+		reg = <0x10002000 0x1000>;
+		#address-cells = <1>;
+		#size-cells = <1>;
+		ranges = <0x0 0x10002000 0x1000>;
+
+		#clock-cells = <1>;
+
+		clocks = <&cgu JZ4780_CLK_RTCLK
+			  &cgu JZ4780_CLK_EXCLK
+			  &cgu JZ4780_CLK_PCLK>;
+		clock-names = "rtc", "ext", "pclk";
+
+		interrupt-controller;
+		#interrupt-cells = <1>;
+
+		interrupt-parent = <&intc>;
+		interrupts = <27 26 25>;
+	};
+
 	rtc_dev: rtc@10003000 {
 		compatible = "ingenic,jz4780-rtc";
 		reg = <0x10003000 0x4c>;
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 06/13] irqchip: Add irq-ingenic-tcu driver
From: Paul Cercueil @ 2019-07-01 15:14 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

This driver handles the interrupt controller built in the Timer/Counter
Unit (TCU) of the JZ47xx SoCs from Ingenic.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
---

Notes:
    v12: New patch
    
    v13: No change
    
    v14: Remove empty lines in structure definitions

 drivers/irqchip/Kconfig           |  11 ++
 drivers/irqchip/Makefile          |   1 +
 drivers/irqchip/irq-ingenic-tcu.c | 181 ++++++++++++++++++++++++++++++
 3 files changed, 193 insertions(+)
 create mode 100644 drivers/irqchip/irq-ingenic-tcu.c

diff --git a/drivers/irqchip/Kconfig b/drivers/irqchip/Kconfig
index 659c5e0fb835..edc67268f777 100644
--- a/drivers/irqchip/Kconfig
+++ b/drivers/irqchip/Kconfig
@@ -294,6 +294,17 @@ config INGENIC_IRQ
 	depends on MACH_INGENIC
 	default y
 
+config INGENIC_TCU_IRQ
+	bool "Ingenic JZ47xx TCU interrupt controller"
+	default MACH_INGENIC
+	depends on MIPS || COMPILE_TEST
+	select INGENIC_TCU
+	help
+	  Support for interrupts in the Timer/Counter Unit (TCU) of the Ingenic
+	  JZ47xx SoCs.
+
+	  If unsure, say N.
+
 config RENESAS_H8300H_INTC
         bool
 	select IRQ_DOMAIN
diff --git a/drivers/irqchip/Makefile b/drivers/irqchip/Makefile
index 606a003a0000..f403b2c221e4 100644
--- a/drivers/irqchip/Makefile
+++ b/drivers/irqchip/Makefile
@@ -73,6 +73,7 @@ obj-$(CONFIG_RENESAS_H8300H_INTC)	+= irq-renesas-h8300h.o
 obj-$(CONFIG_RENESAS_H8S_INTC)		+= irq-renesas-h8s.o
 obj-$(CONFIG_ARCH_SA1100)		+= irq-sa11x0.o
 obj-$(CONFIG_INGENIC_IRQ)		+= irq-ingenic.o
+obj-$(CONFIG_INGENIC_TCU_IRQ)		+= irq-ingenic-tcu.o
 obj-$(CONFIG_IMX_GPCV2)			+= irq-imx-gpcv2.o
 obj-$(CONFIG_PIC32_EVIC)		+= irq-pic32-evic.o
 obj-$(CONFIG_MSCC_OCELOT_IRQ)		+= irq-mscc-ocelot.o
diff --git a/drivers/irqchip/irq-ingenic-tcu.c b/drivers/irqchip/irq-ingenic-tcu.c
new file mode 100644
index 000000000000..f43bd39d3a11
--- /dev/null
+++ b/drivers/irqchip/irq-ingenic-tcu.c
@@ -0,0 +1,181 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * JZ47xx SoCs TCU IRQ driver
+ * Copyright (C) 2019 Paul Cercueil <paul@crapouillou.net>
+ */
+
+#include <linux/clk.h>
+#include <linux/interrupt.h>
+#include <linux/irqchip.h>
+#include <linux/irqchip/chained_irq.h>
+#include <linux/mfd/ingenic-tcu.h>
+#include <linux/of_irq.h>
+#include <linux/regmap.h>
+
+struct ingenic_tcu {
+	struct regmap *map;
+	struct clk *clk;
+	struct irq_domain *domain;
+	unsigned int nb_parent_irqs;
+	u32 parent_irqs[3];
+};
+
+static void ingenic_tcu_intc_cascade(struct irq_desc *desc)
+{
+	struct irq_chip *irq_chip = irq_data_get_irq_chip(&desc->irq_data);
+	struct irq_domain *domain = irq_desc_get_handler_data(desc);
+	struct irq_chip_generic *gc = irq_get_domain_generic_chip(domain, 0);
+	struct regmap *map = gc->private;
+	uint32_t irq_reg, irq_mask;
+	unsigned int i;
+
+	regmap_read(map, TCU_REG_TFR, &irq_reg);
+	regmap_read(map, TCU_REG_TMR, &irq_mask);
+
+	chained_irq_enter(irq_chip, desc);
+
+	irq_reg &= ~irq_mask;
+
+	for_each_set_bit(i, (unsigned long *)&irq_reg, 32)
+		generic_handle_irq(irq_linear_revmap(domain, i));
+
+	chained_irq_exit(irq_chip, desc);
+}
+
+static void ingenic_tcu_gc_unmask_enable_reg(struct irq_data *d)
+{
+	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
+	struct irq_chip_type *ct = irq_data_get_chip_type(d);
+	struct regmap *map = gc->private;
+	u32 mask = d->mask;
+
+	irq_gc_lock(gc);
+	regmap_write(map, ct->regs.ack, mask);
+	regmap_write(map, ct->regs.enable, mask);
+	*ct->mask_cache |= mask;
+	irq_gc_unlock(gc);
+}
+
+static void ingenic_tcu_gc_mask_disable_reg(struct irq_data *d)
+{
+	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
+	struct irq_chip_type *ct = irq_data_get_chip_type(d);
+	struct regmap *map = gc->private;
+	u32 mask = d->mask;
+
+	irq_gc_lock(gc);
+	regmap_write(map, ct->regs.disable, mask);
+	*ct->mask_cache &= ~mask;
+	irq_gc_unlock(gc);
+}
+
+static void ingenic_tcu_gc_mask_disable_reg_and_ack(struct irq_data *d)
+{
+	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
+	struct irq_chip_type *ct = irq_data_get_chip_type(d);
+	struct regmap *map = gc->private;
+	u32 mask = d->mask;
+
+	irq_gc_lock(gc);
+	regmap_write(map, ct->regs.ack, mask);
+	regmap_write(map, ct->regs.disable, mask);
+	irq_gc_unlock(gc);
+}
+
+static int __init ingenic_tcu_irq_init(struct device_node *np,
+				       struct device_node *parent)
+{
+	struct irq_chip_generic *gc;
+	struct irq_chip_type *ct;
+	struct ingenic_tcu *tcu;
+	struct regmap *map;
+	unsigned int i;
+	int ret, irqs;
+
+	map = ingenic_tcu_get_regmap(np);
+	if (IS_ERR(map))
+		return PTR_ERR(map);
+
+	tcu = kzalloc(sizeof(*tcu), GFP_KERNEL);
+	if (!tcu)
+		return -ENOMEM;
+
+	tcu->map = map;
+
+	irqs = of_property_count_elems_of_size(np, "interrupts", sizeof(u32));
+	if (irqs < 0 || irqs > ARRAY_SIZE(tcu->parent_irqs)) {
+		pr_crit("%s: Invalid 'interrupts' property\n", __func__);
+		ret = -EINVAL;
+		goto err_free_tcu;
+	}
+
+	tcu->nb_parent_irqs = irqs;
+
+	tcu->domain = irq_domain_add_linear(np, 32, &irq_generic_chip_ops,
+					    NULL);
+	if (!tcu->domain) {
+		ret = -ENOMEM;
+		goto err_free_tcu;
+	}
+
+	ret = irq_alloc_domain_generic_chips(tcu->domain, 32, 1, "TCU",
+					     handle_level_irq, 0,
+					     IRQ_NOPROBE | IRQ_LEVEL, 0);
+	if (ret) {
+		pr_crit("%s: Invalid 'interrupts' property\n", __func__);
+		goto out_domain_remove;
+	}
+
+	gc = irq_get_domain_generic_chip(tcu->domain, 0);
+	ct = gc->chip_types;
+
+	gc->wake_enabled = IRQ_MSK(32);
+	gc->private = tcu->map;
+
+	ct->regs.disable = TCU_REG_TMSR;
+	ct->regs.enable = TCU_REG_TMCR;
+	ct->regs.ack = TCU_REG_TFCR;
+	ct->chip.irq_unmask = ingenic_tcu_gc_unmask_enable_reg;
+	ct->chip.irq_mask = ingenic_tcu_gc_mask_disable_reg;
+	ct->chip.irq_mask_ack = ingenic_tcu_gc_mask_disable_reg_and_ack;
+	ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE;
+
+	/* Mask all IRQs by default */
+	regmap_write(tcu->map, TCU_REG_TMSR, IRQ_MSK(32));
+
+	/*
+	 * On JZ4740, timer 0 and timer 1 have their own interrupt line;
+	 * timers 2-7 share one interrupt.
+	 * On SoCs >= JZ4770, timer 5 has its own interrupt line;
+	 * timers 0-4 and 6-7 share one single interrupt.
+	 *
+	 * To keep things simple, we just register the same handler to
+	 * all parent interrupts. The handler will properly detect which
+	 * channel fired the interrupt.
+	 */
+	for (i = 0; i < irqs; i++) {
+		tcu->parent_irqs[i] = irq_of_parse_and_map(np, i);
+		if (!tcu->parent_irqs[i]) {
+			ret = -EINVAL;
+			goto out_unmap_irqs;
+		}
+
+		irq_set_chained_handler_and_data(tcu->parent_irqs[i],
+						 ingenic_tcu_intc_cascade,
+						 tcu->domain);
+	}
+
+	return 0;
+
+out_unmap_irqs:
+	for (; i > 0; i--)
+		irq_dispose_mapping(tcu->parent_irqs[i - 1]);
+out_domain_remove:
+	irq_domain_remove(tcu->domain);
+err_free_tcu:
+	kfree(tcu);
+	return ret;
+}
+IRQCHIP_DECLARE(jz4740_tcu_irq, "ingenic,jz4740-tcu", ingenic_tcu_irq_init);
+IRQCHIP_DECLARE(jz4725b_tcu_irq, "ingenic,jz4725b-tcu", ingenic_tcu_irq_init);
+IRQCHIP_DECLARE(jz4770_tcu_irq, "ingenic,jz4770-tcu", ingenic_tcu_irq_init);
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 02/13] doc: Add doc for the Ingenic TCU hardware
From: Paul Cercueil @ 2019-07-01 15:13 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk, Paul Cercueil, Artur Rojek
In-Reply-To: <20190701151410.23127-1-paul@crapouillou.net>

Add documentation about the Timer/Counter Unit (TCU) present in the
Ingenic JZ47xx SoCs.

The Timer/Counter Unit (TCU) in Ingenic JZ47xx SoCs is a multi-function
hardware block. It features up to to eight channels, that can be used as
counters, timers, or PWM.

- JZ4725B, JZ4750, JZ4755 only have six TCU channels. The other SoCs all
  have eight channels.

- JZ4725B introduced a separate channel, called Operating System Timer
  (OST). It is a 32-bit programmable timer. On JZ4770 and above, it is
  64-bit.

- Each one of the TCU channels has its own clock, which can be reparented
  to three different clocks (pclk, ext, rtc), gated, and reclocked, through
  their TCSR register.
  * The watchdog and OST hardware blocks also feature a TCSR register with
    the same format in their register space.
  * The TCU registers used to gate/ungate can also gate/ungate the watchdog
    and OST clocks.

- Each TCU channel works in one of two modes:
  * mode TCU1: channels cannot work in sleep mode, but are easier to
    operate.
  * mode TCU2: channels can work in sleep mode, but the operation is a bit
    more complicated than with TCU1 channels.

- The mode of each TCU channel depends on the SoC used:
  * On the oldest SoCs (up to JZ4740), all of the eight channels operate in
    TCU1 mode.
  * On JZ4725B, channel 5 operates as TCU2, the others operate as TCU1.
  * On newest SoCs (JZ4750 and above), channels 1-2 operate as TCU2, the
    others operate as TCU1.

- Each channel can generate an interrupt. Some channels share an interrupt
  line, some don't, and this changes between SoC versions:
  * on older SoCs (JZ4740 and below), channel 0 and channel 1 have their
    own interrupt line; channels 2-7 share the last interrupt line.
  * On JZ4725B, channel 0 has its own interrupt; channels 1-5 share one
    interrupt line; the OST uses the last interrupt line.
  * on newer SoCs (JZ4750 and above), channel 5 has its own interrupt;
    channels 0-4 and (if eight channels) 6-7 all share one interrupt line;
    the OST uses the last interrupt line.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Tested-by: Mathieu Malaterre <malat@debian.org>
Tested-by: Artur Rojek <contact@artur-rojek.eu>
---

Notes:
    v4: New patch in this series
    
    v5: Added information about number of channels, and improved
        documentation about channel modes
    
    v6: Add info about OST (can be 32-bit on older SoCs)
    
    v7-v11: No change
    
    v12: Add details about new implementation
    
    v13: No change
    
    v14: Convert to ReStructured Text

 Documentation/index.rst            |  1 +
 Documentation/mips/index.rst       | 11 +++++
 Documentation/mips/ingenic-tcu.rst | 72 ++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+)
 create mode 100644 Documentation/mips/index.rst
 create mode 100644 Documentation/mips/ingenic-tcu.rst

diff --git a/Documentation/index.rst b/Documentation/index.rst
index a7566ef62411..00481ac8d75f 100644
--- a/Documentation/index.rst
+++ b/Documentation/index.rst
@@ -115,6 +115,7 @@ implementation.
    x86/index
    sh/index
    x86/index
+   mips/index
 
 Filesystem Documentation
 ------------------------
diff --git a/Documentation/mips/index.rst b/Documentation/mips/index.rst
new file mode 100644
index 000000000000..321b4794f3b8
--- /dev/null
+++ b/Documentation/mips/index.rst
@@ -0,0 +1,11 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+===========================
+MIPS-specific Documentation
+===========================
+
+.. toctree::
+   :maxdepth: 1
+   :numbered:
+
+   ingenic-tcu
diff --git a/Documentation/mips/ingenic-tcu.rst b/Documentation/mips/ingenic-tcu.rst
new file mode 100644
index 000000000000..651c2ebccf71
--- /dev/null
+++ b/Documentation/mips/ingenic-tcu.rst
@@ -0,0 +1,72 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+===============================================
+Ingenic JZ47xx SoCs Timer/Counter Unit hardware
+===============================================
+
+The Timer/Counter Unit (TCU) in Ingenic JZ47xx SoCs is a multi-function
+hardware block. It features up to to eight channels, that can be used as
+counters, timers, or PWM.
+
+- JZ4725B, JZ4750, JZ4755 only have six TCU channels. The other SoCs all
+  have eight channels.
+
+- JZ4725B introduced a separate channel, called Operating System Timer
+  (OST). It is a 32-bit programmable timer. On JZ4760B and above, it is
+  64-bit.
+
+- Each one of the TCU channels has its own clock, which can be reparented to three
+  different clocks (pclk, ext, rtc), gated, and reclocked, through their TCSR register.
+
+    - The watchdog and OST hardware blocks also feature a TCSR register with the same
+      format in their register space.
+    - The TCU registers used to gate/ungate can also gate/ungate the watchdog and
+      OST clocks.
+
+- Each TCU channel works in one of two modes:
+
+    - mode TCU1: channels cannot work in sleep mode, but are easier to
+      operate.
+    - mode TCU2: channels can work in sleep mode, but the operation is a bit
+      more complicated than with TCU1 channels.
+
+- The mode of each TCU channel depends on the SoC used:
+
+    - On the oldest SoCs (up to JZ4740), all of the eight channels operate in
+      TCU1 mode.
+    - On JZ4725B, channel 5 operates as TCU2, the others operate as TCU1.
+    - On newest SoCs (JZ4750 and above), channels 1-2 operate as TCU2, the
+      others operate as TCU1.
+
+- Each channel can generate an interrupt. Some channels share an interrupt
+  line, some don't, and this changes between SoC versions:
+
+    - on older SoCs (JZ4740 and below), channel 0 and channel 1 have their
+      own interrupt line; channels 2-7 share the last interrupt line.
+    - On JZ4725B, channel 0 has its own interrupt; channels 1-5 share one
+      interrupt line; the OST uses the last interrupt line.
+    - on newer SoCs (JZ4750 and above), channel 5 has its own interrupt;
+      channels 0-4 and (if eight channels) 6-7 all share one interrupt line;
+      the OST uses the last interrupt line.
+
+Implementation
+==============
+
+The functionalities of the TCU hardware are spread across multiple drivers:
+
+===========  =====
+boilerplate  drivers/mfd/ingenic-tcu.c
+clocks       drivers/clk/ingenic/tcu.c
+interrupts   drivers/irqchip/irq-ingenic-tcu.c
+timers       drivers/clocksource/ingenic-timer.c
+OST          drivers/clocksource/ingenic-ost.c
+PWM          drivers/pwm/pwm-jz4740.c
+watchdog     drivers/watchdog/jz4740_wdt.c
+===========  =====
+
+Because various functionalities of the TCU that belong to different drivers
+and frameworks can be controlled from the same registers, all of these
+drivers access their registers through the same regmap.
+
+For more information regarding the devicetree bindings of the TCU drivers,
+have a look at Documentation/devicetree/bindings/mfd/ingenic,tcu.txt.
-- 
2.21.0.593.g511ec345e18


^ permalink raw reply related

* [PATCH v14 00/13] Ingenic TCU patchset v14
From: Paul Cercueil @ 2019-07-01 15:13 UTC (permalink / raw)
  To: Lee Jones, Jonathan Corbet, Ralf Baechle, Paul Burton,
	James Hogan, Michael Turquette, Stephen Boyd, Thomas Gleixner,
	Daniel Lezcano
  Cc: Mathieu Malaterre, od, devicetree, linux-kernel, linux-doc,
	linux-mips, linux-clk

Hi,

Changelog from v13:

- [02/13]: The documentation has been converted to ReStructured Text.
- [04/13]: - Use ERR_CAST() instead of ERR_PTR(PTR_ERR())
           - Remove ingenic_tcu_can_use_pwm().
- [05/13]: Use %d instead of %i in messages
- [06/13]: Remove empty lines in structure definitions
- [07/13]: Remove empty lines in structure definitions

The patches that are not listed above did not see any change since v13.

Regards,
-Paul



^ permalink raw reply

* Re: [linux-kernel-mentees] [PATCH v1] Doc : fs : convert xfs.txt to ReST
From: Eric Sandeen @ 2019-07-01 14:54 UTC (permalink / raw)
  To: Jonathan Corbet, Sheriff Esseson
  Cc: skhan, linux-kernel-mentees, darrick.wong, linux-xfs, linux-doc,
	linux-kernel
In-Reply-To: <20190630113243.7265c33e@lwn.net>



On 6/30/19 12:32 PM, Jonathan Corbet wrote:
> On Sun, 30 Jun 2019 17:50:46 +0100
> Sheriff Esseson <sheriffesseson@gmail.com> wrote:
> 
>> On Sat, Jun 29, 2019 at 09:57:59PM +0100, Sheriff Esseson wrote:
>>> Signed-off-by: Sheriff Esseson <sheriffesseson@gmail.com>
>>> ---
>>>
>>> In v3:
>>> Update MAINTAINERS. Fix Indentation/long line issues. Insert Sphinx tag.
>>>
>>> -- 
>>> 2.22.0
>>>   
>>
>> Signed-off-by: Sheriff Esseson <sheriffesseson@gmail.com>
>> ---
>>
>> In v4:
>> dax.txt : 
>> 	fix broken reference to xfs.rst
> 
> I will now repeat what I said yesterday:
> 
>> Please do us a favor?  
>>
>> 1) Post each patch standalone, without quoted text, ready to be applied.

To be maybe more explicit,

While we appreciate the eagerness here I'd like to reiterate that the
followup V2, V3, ... patches should each be a standalone, self-contained
email, without any quoted text and with updated subject lines, i.e.

Subject: [PATCH] Doc : fs : convert xfs.txt to ReST

followed by:

Subject: [PATCH V2] Doc : fs : convert xfs.txt to ReST

followed by:

Subject: [PATCH V3] Doc : fs : convert xfs.txt to ReST

so it's trivial to see from subject lines when a new version has been
sent.

Also, what Jonathan said about time between iterations.  ;)

Thanks,
-Eric


>> 2) Wait a little while between postings so that you can address more than
>> one comment at a time.
>>
>> OK, two favors - off-by-one errors are my specialty...:)
> 
> I did actually mean that.  For "a little while" I meant "a few days or
> so".  Please slow down a little.  In any case, we're getting closer to the
> merge window, so this patch won't be applied for a while regardless.
> 
> Thanks,
> 
> jon
> 

^ permalink raw reply

* Re: [PATCH v12 01/11] MODSIGN: Export module signature definitions
From: Jessica Yu @ 2019-07-01 14:47 UTC (permalink / raw)
  To: Thiago Jung Bauermann
  Cc: linux-integrity, linux-security-module, keyrings, linux-crypto,
	linuxppc-dev, linux-doc, linux-kernel, Mimi Zohar,
	Dmitry Kasatkin, James Morris, Serge E. Hallyn, David Howells,
	David Woodhouse, Herbert Xu, David S. Miller, Jonathan Corbet,
	AKASHI, Takahiro
In-Reply-To: <20190628021934.4260-2-bauerman@linux.ibm.com>

+++ Thiago Jung Bauermann [27/06/19 23:19 -0300]:
>IMA will use the module_signature format for append signatures, so export
>the relevant definitions and factor out the code which verifies that the
>appended signature trailer is valid.
>
>Also, create a CONFIG_MODULE_SIG_FORMAT option so that IMA can select it
>and be able to use mod_check_sig() without having to depend on either
>CONFIG_MODULE_SIG or CONFIG_MODULES.
>
>Signed-off-by: Thiago Jung Bauermann <bauerman@linux.ibm.com>
>Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
>Cc: Jessica Yu <jeyu@kernel.org>
>---
> include/linux/module.h           |  3 --
> include/linux/module_signature.h | 44 +++++++++++++++++++++++++
> init/Kconfig                     |  6 +++-
> kernel/Makefile                  |  1 +
> kernel/module.c                  |  1 +
> kernel/module_signature.c        | 46 ++++++++++++++++++++++++++
> kernel/module_signing.c          | 56 +++++---------------------------
> scripts/Makefile                 |  2 +-
> 8 files changed, 106 insertions(+), 53 deletions(-)
>
>diff --git a/include/linux/module.h b/include/linux/module.h
>index 188998d3dca9..aa56f531cf1e 100644
>--- a/include/linux/module.h
>+++ b/include/linux/module.h
>@@ -25,9 +25,6 @@
> #include <linux/percpu.h>
> #include <asm/module.h>
>
>-/* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */
>-#define MODULE_SIG_STRING "~Module signature appended~\n"
>-

Hi Thiago, apologies for the delay.

It looks like arch/s390/kernel/machine_kexec_file.c also relies on
MODULE_SIG_STRING being defined, so module_signature.h will need to be
included there too, otherwise we'll run into a compilation error.

Other than that, the module-related changes look good to me:

Acked-by: Jessica Yu <jeyu@kernel.org>

Thanks!

Jessica

> /* Not Yet Implemented */
> #define MODULE_SUPPORTED_DEVICE(name)
>
>diff --git a/include/linux/module_signature.h b/include/linux/module_signature.h
>new file mode 100644
>index 000000000000..523617fc5b6a
>--- /dev/null
>+++ b/include/linux/module_signature.h
>@@ -0,0 +1,44 @@
>+/* SPDX-License-Identifier: GPL-2.0+ */
>+/*
>+ * Module signature handling.
>+ *
>+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
>+ * Written by David Howells (dhowells@redhat.com)
>+ */
>+
>+#ifndef _LINUX_MODULE_SIGNATURE_H
>+#define _LINUX_MODULE_SIGNATURE_H
>+
>+/* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */
>+#define MODULE_SIG_STRING "~Module signature appended~\n"
>+
>+enum pkey_id_type {
>+	PKEY_ID_PGP,		/* OpenPGP generated key ID */
>+	PKEY_ID_X509,		/* X.509 arbitrary subjectKeyIdentifier */
>+	PKEY_ID_PKCS7,		/* Signature in PKCS#7 message */
>+};
>+
>+/*
>+ * Module signature information block.
>+ *
>+ * The constituents of the signature section are, in order:
>+ *
>+ *	- Signer's name
>+ *	- Key identifier
>+ *	- Signature data
>+ *	- Information block
>+ */
>+struct module_signature {
>+	u8	algo;		/* Public-key crypto algorithm [0] */
>+	u8	hash;		/* Digest algorithm [0] */
>+	u8	id_type;	/* Key identifier type [PKEY_ID_PKCS7] */
>+	u8	signer_len;	/* Length of signer's name [0] */
>+	u8	key_id_len;	/* Length of key identifier [0] */
>+	u8	__pad[3];
>+	__be32	sig_len;	/* Length of signature data */
>+};
>+
>+int mod_check_sig(const struct module_signature *ms, size_t file_len,
>+		  const char *name);
>+
>+#endif /* _LINUX_MODULE_SIGNATURE_H */
>diff --git a/init/Kconfig b/init/Kconfig
>index 8b9ffe236e4f..c2286a3c74c5 100644
>--- a/init/Kconfig
>+++ b/init/Kconfig
>@@ -1852,6 +1852,10 @@ config BASE_SMALL
> 	default 0 if BASE_FULL
> 	default 1 if !BASE_FULL
>
>+config MODULE_SIG_FORMAT
>+	def_bool n
>+	select SYSTEM_DATA_VERIFICATION
>+
> menuconfig MODULES
> 	bool "Enable loadable module support"
> 	option modules
>@@ -1929,7 +1933,7 @@ config MODULE_SRCVERSION_ALL
> config MODULE_SIG
> 	bool "Module signature verification"
> 	depends on MODULES
>-	select SYSTEM_DATA_VERIFICATION
>+	select MODULE_SIG_FORMAT
> 	help
> 	  Check modules for valid signatures upon load: the signature
> 	  is simply appended to the module. For more information see
>diff --git a/kernel/Makefile b/kernel/Makefile
>index 33824f0385b3..f29ae2997a43 100644
>--- a/kernel/Makefile
>+++ b/kernel/Makefile
>@@ -58,6 +58,7 @@ endif
> obj-$(CONFIG_UID16) += uid16.o
> obj-$(CONFIG_MODULES) += module.o
> obj-$(CONFIG_MODULE_SIG) += module_signing.o
>+obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
> obj-$(CONFIG_KALLSYMS) += kallsyms.o
> obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
> obj-$(CONFIG_CRASH_CORE) += crash_core.o
>diff --git a/kernel/module.c b/kernel/module.c
>index 6e6712b3aaf5..2712f4d217f5 100644
>--- a/kernel/module.c
>+++ b/kernel/module.c
>@@ -19,6 +19,7 @@
> #include <linux/export.h>
> #include <linux/extable.h>
> #include <linux/moduleloader.h>
>+#include <linux/module_signature.h>
> #include <linux/trace_events.h>
> #include <linux/init.h>
> #include <linux/kallsyms.h>
>diff --git a/kernel/module_signature.c b/kernel/module_signature.c
>new file mode 100644
>index 000000000000..4224a1086b7d
>--- /dev/null
>+++ b/kernel/module_signature.c
>@@ -0,0 +1,46 @@
>+// SPDX-License-Identifier: GPL-2.0+
>+/*
>+ * Module signature checker
>+ *
>+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
>+ * Written by David Howells (dhowells@redhat.com)
>+ */
>+
>+#include <linux/errno.h>
>+#include <linux/printk.h>
>+#include <linux/module_signature.h>
>+#include <asm/byteorder.h>
>+
>+/**
>+ * mod_check_sig - check that the given signature is sane
>+ *
>+ * @ms:		Signature to check.
>+ * @file_len:	Size of the file to which @ms is appended.
>+ * @name:	What is being checked. Used for error messages.
>+ */
>+int mod_check_sig(const struct module_signature *ms, size_t file_len,
>+		  const char *name)
>+{
>+	if (be32_to_cpu(ms->sig_len) >= file_len - sizeof(*ms))
>+		return -EBADMSG;
>+
>+	if (ms->id_type != PKEY_ID_PKCS7) {
>+		pr_err("%s: Module is not signed with expected PKCS#7 message\n",
>+		       name);
>+		return -ENOPKG;
>+	}
>+
>+	if (ms->algo != 0 ||
>+	    ms->hash != 0 ||
>+	    ms->signer_len != 0 ||
>+	    ms->key_id_len != 0 ||
>+	    ms->__pad[0] != 0 ||
>+	    ms->__pad[1] != 0 ||
>+	    ms->__pad[2] != 0) {
>+		pr_err("%s: PKCS#7 signature info has unexpected non-zero params\n",
>+		       name);
>+		return -EBADMSG;
>+	}
>+
>+	return 0;
>+}
>diff --git a/kernel/module_signing.c b/kernel/module_signing.c
>index 6b9a926fd86b..cdd04a6b8074 100644
>--- a/kernel/module_signing.c
>+++ b/kernel/module_signing.c
>@@ -11,37 +11,13 @@
>
> #include <linux/kernel.h>
> #include <linux/errno.h>
>+#include <linux/module.h>
>+#include <linux/module_signature.h>
> #include <linux/string.h>
> #include <linux/verification.h>
> #include <crypto/public_key.h>
> #include "module-internal.h"
>
>-enum pkey_id_type {
>-	PKEY_ID_PGP,		/* OpenPGP generated key ID */
>-	PKEY_ID_X509,		/* X.509 arbitrary subjectKeyIdentifier */
>-	PKEY_ID_PKCS7,		/* Signature in PKCS#7 message */
>-};
>-
>-/*
>- * Module signature information block.
>- *
>- * The constituents of the signature section are, in order:
>- *
>- *	- Signer's name
>- *	- Key identifier
>- *	- Signature data
>- *	- Information block
>- */
>-struct module_signature {
>-	u8	algo;		/* Public-key crypto algorithm [0] */
>-	u8	hash;		/* Digest algorithm [0] */
>-	u8	id_type;	/* Key identifier type [PKEY_ID_PKCS7] */
>-	u8	signer_len;	/* Length of signer's name [0] */
>-	u8	key_id_len;	/* Length of key identifier [0] */
>-	u8	__pad[3];
>-	__be32	sig_len;	/* Length of signature data */
>-};
>-
> /*
>  * Verify the signature on a module.
>  */
>@@ -49,6 +25,7 @@ int mod_verify_sig(const void *mod, struct load_info *info)
> {
> 	struct module_signature ms;
> 	size_t sig_len, modlen = info->len;
>+	int ret;
>
> 	pr_devel("==>%s(,%zu)\n", __func__, modlen);
>
>@@ -56,32 +33,15 @@ int mod_verify_sig(const void *mod, struct load_info *info)
> 		return -EBADMSG;
>
> 	memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
>-	modlen -= sizeof(ms);
>+
>+	ret = mod_check_sig(&ms, modlen, info->name);
>+	if (ret)
>+		return ret;
>
> 	sig_len = be32_to_cpu(ms.sig_len);
>-	if (sig_len >= modlen)
>-		return -EBADMSG;
>-	modlen -= sig_len;
>+	modlen -= sig_len + sizeof(ms);
> 	info->len = modlen;
>
>-	if (ms.id_type != PKEY_ID_PKCS7) {
>-		pr_err("%s: Module is not signed with expected PKCS#7 message\n",
>-		       info->name);
>-		return -ENOPKG;
>-	}
>-
>-	if (ms.algo != 0 ||
>-	    ms.hash != 0 ||
>-	    ms.signer_len != 0 ||
>-	    ms.key_id_len != 0 ||
>-	    ms.__pad[0] != 0 ||
>-	    ms.__pad[1] != 0 ||
>-	    ms.__pad[2] != 0) {
>-		pr_err("%s: PKCS#7 signature info has unexpected non-zero params\n",
>-		       info->name);
>-		return -EBADMSG;
>-	}
>-
> 	return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len,
> 				      VERIFY_USE_SECONDARY_KEYRING,
> 				      VERIFYING_MODULE_SIGNATURE,
>diff --git a/scripts/Makefile b/scripts/Makefile
>index 9d442ee050bd..52098b080ab7 100644
>--- a/scripts/Makefile
>+++ b/scripts/Makefile
>@@ -17,7 +17,7 @@ hostprogs-$(CONFIG_VT)           += conmakehash
> hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount
> hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable
> hostprogs-$(CONFIG_ASN1)	 += asn1_compiler
>-hostprogs-$(CONFIG_MODULE_SIG)	 += sign-file
>+hostprogs-$(CONFIG_MODULE_SIG_FORMAT) += sign-file
> hostprogs-$(CONFIG_SYSTEM_TRUSTED_KEYRING) += extract-cert
> hostprogs-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE) += insert-sys-cert
>

^ permalink raw reply

* Re: [PATCH v12 00/11] Appended signatures support for IMA appraisal
From: Mimi Zohar @ 2019-07-01 14:38 UTC (permalink / raw)
  To: Thiago Jung Bauermann, linux-integrity
  Cc: linux-security-module, keyrings, linux-crypto, linuxppc-dev,
	linux-doc, linux-kernel, Dmitry Kasatkin, James Morris,
	Serge E. Hallyn, David Howells, David Woodhouse, Jessica Yu,
	Herbert Xu, David S. Miller, Jonathan Corbet, AKASHI, Takahiro
In-Reply-To: <20190628021934.4260-1-bauerman@linux.ibm.com>

On Thu, 2019-06-27 at 23:19 -0300, Thiago Jung Bauermann wrote:
> Hello,
> 
> This version is essentially identical to the last one.
> 
> It is only a rebase on top of today's linux-integrity/next-queued-testing,
> prompted by conflicts with Prakhar Srivastava's patches to measure the
> kernel command line. It also drops two patches that are already present in
> that branch.

Thanks, Thiago.  These patches are now in next-queued-testing waiting
for some additional reviews/acks.

Mimi


^ permalink raw reply

* [PATCH] Documentation: gpio: Fix reference to gpiod_get_array()
From: Geert Uytterhoeven @ 2019-07-01 14:10 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Jonathan Corbet
  Cc: Janusz Krzysztofik, linux-gpio, linux-doc, linux-kernel,
	Geert Uytterhoeven

The function is called gpiod_get_array(), not gpiod_array_get().

Fixes: 77588c14ac868cae ("gpiolib: Pass array info to get/set array functions")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
 Documentation/driver-api/gpio/consumer.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Documentation/driver-api/gpio/consumer.rst b/Documentation/driver-api/gpio/consumer.rst
index 23d68c321c5c7c7d..9559aa3cbcef25a2 100644
--- a/Documentation/driver-api/gpio/consumer.rst
+++ b/Documentation/driver-api/gpio/consumer.rst
@@ -364,7 +364,7 @@ accessed sequentially.
 The functions take three arguments:
 	* array_size	- the number of array elements
 	* desc_array	- an array of GPIO descriptors
-	* array_info	- optional information obtained from gpiod_array_get()
+	* array_info	- optional information obtained from gpiod_get_array()
 	* value_bitmap	- a bitmap to store the GPIOs' values (get) or
 			  a bitmap of values to assign to the GPIOs (set)
 
-- 
2.17.1


^ permalink raw reply related

* Re: [UPDATE][PATCH 10/10] tools/power/x86: A tool to validate Intel Speed Select commands
From: Andy Shevchenko @ 2019-07-01 11:32 UTC (permalink / raw)
  To: Srinivas Pandruvada
  Cc: Darren Hart, Andy Shevchenko, Andriy Shevchenko, Jonathan Corbet,
	Rafael J. Wysocki, Alan Cox, Len Brown, prarit, darcari,
	Linux Documentation List, Linux Kernel Mailing List,
	Platform Driver
In-Reply-To: <20190630171408.8673-1-srinivas.pandruvada@linux.intel.com>

On Sun, Jun 30, 2019 at 8:14 PM Srinivas Pandruvada
<srinivas.pandruvada@linux.intel.com> wrote:
>
> The Intel(R) Speed select technologies contains four features.
>
> Performance profile:An non architectural mechanism that allows multiple
> optimized performance profiles per system via static and/or dynamic
> adjustment of core count, workload, Tjmax, and TDP, etc. aka ISS
> in the documentation.
>
> Base Frequency: Enables users to increase guaranteed base frequency on
> certain cores (high priority cores) in exchange for lower base frequency
> on remaining cores (low priority cores). aka PBF in the documenation.
>
> Turbo frequency: Enables the ability to set different turbo ratio limits
> to cores based on priority. aka FACT in the documentation.
>
> Core power: An Interface that allows user to define per core/tile
> priority.
>
> There is a multi level help for commands and options. This can be used
> to check required arguments for each feature and commands for the
> feature.
>
> To start navigating the features start with
>
> $sudo intel-speed-select --help
>
> For help on a specific feature for example
> $sudo intel-speed-select perf-profile --help
>
> To get help for a command for a feature for example
> $sudo intel-speed-select perf-profile get-lock-status --help
>
> Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
> ---
> Updates:
> - Copied Makefile from tools/gpio and moified the Makefile here
> - Added entry to tools/build/Makefile
> - Rename directory to match the executable name
> - Fix one error message

Thanks!
I pushed to my review and testing queue, while still waiting for some ACKs.

It seems I can promote the driver itself now,w/o tools, if you want me to do so.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply

* Re: [PATCH 41/43] docs: extcon: convert it to ReST and move to acpi dir
From: Chanwoo Choi @ 2019-07-01  1:40 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Linux Doc Mailing List
  Cc: Mauro Carvalho Chehab, linux-kernel, Jonathan Corbet,
	MyungJoo Ham, Rafael J. Wysocki, Len Brown, linux-acpi
In-Reply-To: <bce6d8c98a188ec5f0efe78962aa12839c7442e9.1561723980.git.mchehab+samsung@kernel.org>

Hi Mauro,

On 19. 6. 28. 오후 9:20, Mauro Carvalho Chehab wrote:
> The intel-int3496.txt file is a documentation for an ACPI driver.
> 
> There's no reason to keep it on a separate directory.
> 
> So, instead of keeping it on some random location, move it
> to a sub-directory inside the ACPI documentation dir,
> renaming it to .rst.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
> ---
>  .../acpi/extcon-intel-int3496.rst}                 | 14 ++++++++++----
>  Documentation/firmware-guide/acpi/index.rst        |  1 +
>  MAINTAINERS                                        |  6 +++---
>  3 files changed, 14 insertions(+), 7 deletions(-)
>  rename Documentation/{extcon/intel-int3496.txt => firmware-guide/acpi/extcon-intel-int3496.rst} (66%)
> 
> diff --git a/Documentation/extcon/intel-int3496.txt b/Documentation/firmware-guide/acpi/extcon-intel-int3496.rst
> similarity index 66%
> rename from Documentation/extcon/intel-int3496.txt
> rename to Documentation/firmware-guide/acpi/extcon-intel-int3496.rst
> index 8155dbc7fad3..5137ca834b54 100644
> --- a/Documentation/extcon/intel-int3496.txt
> +++ b/Documentation/firmware-guide/acpi/extcon-intel-int3496.rst
> @@ -1,5 +1,6 @@
> +=====================================================
>  Intel INT3496 ACPI device extcon driver documentation
> ------------------------------------------------------
> +=====================================================
>  
>  The Intel INT3496 ACPI device extcon driver is a driver for ACPI
>  devices with an acpi-id of INT3496, such as found for example on
> @@ -13,15 +14,20 @@ between an USB host and an USB peripheral controller.
>  The ACPI devices exposes this functionality by returning an array with up
>  to 3 gpio descriptors from its ACPI _CRS (Current Resource Settings) call:
>  
> -Index 0: The input gpio for the id-pin, this is always present and valid
> -Index 1: The output gpio for enabling Vbus output from the device to the otg
> +=======  =====================================================================
> +Index 0  The input gpio for the id-pin, this is always present and valid
> +Index 1  The output gpio for enabling Vbus output from the device to the otg
>           port, write 1 to enable the Vbus output (this gpio descriptor may
>           be absent or invalid)
> -Index 2: The output gpio for muxing of the data pins between the USB host and
> +Index 2  The output gpio for muxing of the data pins between the USB host and
>           the USB peripheral controller, write 1 to mux to the peripheral
>           controller
> +=======  =====================================================================
>  
>  There is a mapping between indices and GPIO connection IDs as follows
> +
> +	======= =======
>  	id	index 0
>  	vbus	index 1
>  	mux	index 2
> +	======= =======
> diff --git a/Documentation/firmware-guide/acpi/index.rst b/Documentation/firmware-guide/acpi/index.rst
> index ae609eec4679..90c90d42d9ad 100644
> --- a/Documentation/firmware-guide/acpi/index.rst
> +++ b/Documentation/firmware-guide/acpi/index.rst
> @@ -24,3 +24,4 @@ ACPI Support
>     acpi-lid
>     lpit
>     video_extension
> +   extcon-intel-int3496
> diff --git a/MAINTAINERS b/MAINTAINERS
> index fd6fab0dec77..2cf8abf6d48e 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -321,7 +321,7 @@ F:	drivers/pnp/pnpacpi/
>  F:	include/linux/acpi.h
>  F:	include/linux/fwnode.h
>  F:	include/acpi/
> -F:	Documentation/acpi/
> +F:	Documentation/firmware-guide/acpi/
>  F:	Documentation/ABI/testing/sysfs-bus-acpi
>  F:	Documentation/ABI/testing/configfs-acpi
>  F:	drivers/pci/*acpi*
> @@ -4896,7 +4896,7 @@ S:	Maintained
>  F:	Documentation/
>  F:	scripts/kernel-doc
>  X:	Documentation/ABI/
> -X:	Documentation/acpi/
> +X:	Documentation/firmware-guide/acpi/
>  X:	Documentation/devicetree/
>  X:	Documentation/i2c/
>  X:	Documentation/media/
> @@ -6073,7 +6073,7 @@ S:	Maintained
>  F:	drivers/extcon/
>  F:	include/linux/extcon/
>  F:	include/linux/extcon.h
> -F:	Documentation/extcon/
> +F:	Documentation/firmware-guide/acpi/extcon-intel-int3496.rst
>  F:	Documentation/devicetree/bindings/extcon/
>  
>  EXYNOS DP DRIVER
> 

Acked-by: Chanwoo Choi <cw00.choi@samsung.com>

-- 
Best Regards,
Chanwoo Choi
Samsung Electronics

^ permalink raw reply

* Re: [PATCH 22/39] docs: ocxl.rst: add it to the uAPI book
From: Andrew Donnellan @ 2019-07-01  1:21 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Linux Doc Mailing List
  Cc: Mauro Carvalho Chehab, linux-kernel, Jonathan Corbet,
	Frederic Barrat, linuxppc-dev
In-Reply-To: <ee63ec4412f2f8c87da877f67f693f2cd85c1a37.1561724493.git.mchehab+samsung@kernel.org>

On 28/6/19 10:30 pm, Mauro Carvalho Chehab wrote:
> The content of this file is user-faced.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>

Acked-by: Andrew Donnellan <ajd@linux.ibm.com>

> ---
>   Documentation/{ => userspace-api}/accelerators/ocxl.rst | 2 --
>   Documentation/userspace-api/index.rst                   | 1 +
>   MAINTAINERS                                             | 2 +-
>   3 files changed, 2 insertions(+), 3 deletions(-)
>   rename Documentation/{ => userspace-api}/accelerators/ocxl.rst (99%)
> 
> diff --git a/Documentation/accelerators/ocxl.rst b/Documentation/userspace-api/accelerators/ocxl.rst
> similarity index 99%
> rename from Documentation/accelerators/ocxl.rst
> rename to Documentation/userspace-api/accelerators/ocxl.rst
> index b1cea19a90f5..14cefc020e2d 100644
> --- a/Documentation/accelerators/ocxl.rst
> +++ b/Documentation/userspace-api/accelerators/ocxl.rst
> @@ -1,5 +1,3 @@
> -:orphan:
> -
>   ========================================================
>   OpenCAPI (Open Coherent Accelerator Processor Interface)
>   ========================================================
> diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspace-api/index.rst
> index a3233da7fa88..ad494da40009 100644
> --- a/Documentation/userspace-api/index.rst
> +++ b/Documentation/userspace-api/index.rst
> @@ -20,6 +20,7 @@ place where this information is gathered.
>      seccomp_filter
>      unshare
>      spec_ctrl
> +   accelerators/ocxl
>   
>   .. only::  subproject and html
>   
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 29d1498ad39d..f723371dccd0 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -11483,7 +11483,7 @@ F:	arch/powerpc/include/asm/pnv-ocxl.h
>   F:	drivers/misc/ocxl/
>   F:	include/misc/ocxl*
>   F:	include/uapi/misc/ocxl.h
> -F:	Documentation/accelerators/ocxl.rst
> +F:	Documentation/userspace-api/accelerators/ocxl.rst
>   
>   OMAP AUDIO SUPPORT
>   M:	Peter Ujfalusi <peter.ujfalusi@ti.com>
> 

-- 
Andrew Donnellan              OzLabs, ADL Canberra
ajd@linux.ibm.com             IBM Australia Limited


^ permalink raw reply

* [PATCH 0/7] Compile-test UAPI and kernel headers
From: Masahiro Yamada @ 2019-07-01  0:58 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Joel Fernandes, Masahiro Yamada, Tony Luck,
	linux-doc, John Fastabend, Jonathan Corbet, Jakub Kicinski,
	linux-riscv, Daniel Borkmann, xdp-newbies, Anton Vorontsov,
	Palmer Dabbelt, Matthias Brugger, Song Liu, Yonghong Song,
	Michal Marek, Jesper Dangaard Brouer, Martin KaFai Lau,
	linux-mediatek, linux-arm-kernel, Albert Ou, Colin Cross,
	David S. Miller, Kees Cook, Alexei Starovoitov, netdev,
	linux-kernel, bpf


1/7: add CONFIG_CC_CAN_LINK to use it in 2/7

2/7: Compile-test exported headers

3/7: Do not generate intermediate wrappers.
     This will avoid header search path issue.

4/7: maybe useful for 7/7 and in some other places.
     Add header-test-pattern-y syntax.

5/7: Minor cleanup of gen_kheaders.sh

6/7: Exclude all files without ".h" extension
     from the kheaders_data.tar.xz
     This will be needed by 7/7 because we need to
     exclude "*.h.s" from the archive

7/7: Compile-test kernel-space headers in include/.


Masahiro Yamada (7):
  init/Kconfig: add CONFIG_CC_CAN_LINK
  kbuild: compile-test exported headers to ensure they are
    self-contained
  kbuild: do not create wrappers for header-test-y
  kbuild: support header-test-pattern-y
  kheaders: remove meaningless -R option of 'ls'
  kheaders: include only headers into kheaders_data.tar.xz
  kbuild: compile-test kernel headers to ensure they are self-contained

 .gitignore                         |    1 -
 Documentation/dontdiff             |    1 -
 Documentation/kbuild/makefiles.txt |   13 +-
 Makefile                           |    4 +-
 include/Kbuild                     | 1253 ++++++++++++++++++++++++++++
 init/Kconfig                       |   24 +
 kernel/gen_kheaders.sh             |   51 +-
 net/bpfilter/Kconfig               |    2 +-
 scripts/Makefile.build             |   10 +-
 scripts/Makefile.lib               |   13 +-
 usr/.gitignore                     |    1 -
 usr/Makefile                       |    2 +
 usr/include/.gitignore             |    3 +
 usr/include/Makefile               |  131 +++
 14 files changed, 1462 insertions(+), 47 deletions(-)
 create mode 100644 include/Kbuild
 create mode 100644 usr/include/.gitignore
 create mode 100644 usr/include/Makefile

-- 
2.17.1


^ permalink raw reply

* [PATCH 3/7] kbuild: do not create wrappers for header-test-y
From: Masahiro Yamada @ 2019-07-01  0:58 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Joel Fernandes, Masahiro Yamada, linux-doc,
	Jonathan Corbet, linux-kernel, Michal Marek
In-Reply-To: <20190701005845.12475-1-yamada.masahiro@socionext.com>

header-test-y does not work with headers in sub-directories.

For example, you may want to write a Makefile, like this:

include/linux/Kbuild:

  header-test-y += mtd/nand.h

This entry will create a wrapper include/linux/mtd/nand.hdrtest.c
with the following content:

  #include "mtd/nand.h"

To make this work, we need to add $(srctree)/include/linux to the
header search path. It would be tedious to add ccflags-y.

Instead, we could change the *.hdrtest.c rule to wrap:

  #include "nand.h"

This works for in-tree build since #include "..." searches in the
relative path from the header with this directive. For O=... build,
we need to add $(srctree)/include/linux/mtd to the header search path,
which will be even more tedious.

After all, I thought it would be handier to compile headers directly
without creating wrappers.

I added a new build rule to compile %.h into %.h.s

The target is %.h.s instead of %.h.o because it is slightly faster.
Also, as for GCC, an empty assembly is smaller than an empty object.

I wrote the build rule:

  $(CC) $(c_flags) -S -o $@ -x c /dev/null -include $<

instead of:

  $(CC) $(c_flags) -S -o $@ -x c $<

Both work fine with GCC, but the latter is bad for Clang.

This comes down to the difference in the -Wunused-function policy.
GCC does not warn about unused 'static inline' functions at all.
Clang does not warn about the ones in included headers, but does
about the ones in the source. So, we should handle headers as
headers, not as source files.

In fact, this has been hidden since commit abb2ea7dfd82 ("compiler,
clang: suppress warning for unused static inline functions"), but we
should not rely on that.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Jani Nikula <jani.nikula@intel.com>
Tested-by: Jani Nikula <jani.nikula@intel.com>
---

Changes in v4: None
Changes in v3: None
Changes in v2:
  - New patch

 .gitignore                         |  1 -
 Documentation/dontdiff             |  1 -
 Documentation/kbuild/makefiles.txt |  3 +--
 Makefile                           |  1 -
 scripts/Makefile.build             | 10 +++++-----
 scripts/Makefile.lib               |  2 +-
 6 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore
index 4bb60f0fa23b..7587ef56b92d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,7 +22,6 @@
 *.elf
 *.gcno
 *.gz
-*.hdrtest.c
 *.i
 *.ko
 *.lex.c
diff --git a/Documentation/dontdiff b/Documentation/dontdiff
index 554dfe4883d2..5eba889ea84d 100644
--- a/Documentation/dontdiff
+++ b/Documentation/dontdiff
@@ -19,7 +19,6 @@
 *.grep
 *.grp
 *.gz
-*.hdrtest.c
 *.html
 *.i
 *.jpeg
diff --git a/Documentation/kbuild/makefiles.txt b/Documentation/kbuild/makefiles.txt
index ca4b24ec0399..5080fec34609 100644
--- a/Documentation/kbuild/makefiles.txt
+++ b/Documentation/kbuild/makefiles.txt
@@ -1023,8 +1023,7 @@ When kbuild executes, the following steps are followed (roughly):
 	header-test-y specifies headers (*.h) in the current directory that
 	should be compile tested to ensure they are self-contained,
 	i.e. compilable as standalone units. If CONFIG_HEADER_TEST is enabled,
-	this autogenerates dummy sources to include the headers, and builds them
-	as part of extra-y.
+	this builds them as part of extra-y.
 
 --- 6.7 Commands useful for building a boot image
 
diff --git a/Makefile b/Makefile
index f23516980796..7f293b0431fe 100644
--- a/Makefile
+++ b/Makefile
@@ -1648,7 +1648,6 @@ clean: $(clean-dirs)
 		-o -name '*.dwo' -o -name '*.lst' \
 		-o -name '*.su'  \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
-		-o -name '*.hdrtest.c' \
 		-o -name '*.lex.c' -o -name '*.tab.[ch]' \
 		-o -name '*.asn1.[ch]' \
 		-o -name '*.symtypes' -o -name 'modules.order' \
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index ee0319560513..776842b7e6a3 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -294,14 +294,14 @@ quiet_cmd_cc_lst_c = MKLST   $@
 $(obj)/%.lst: $(src)/%.c FORCE
 	$(call if_changed_dep,cc_lst_c)
 
-# Dummy C sources for header test (header-test-y target)
+# header test (header-test-y target)
 # ---------------------------------------------------------------------------
 
-quiet_cmd_header_test = HDRTEST $@
-      cmd_header_test = echo "\#include \"$*.h\"" > $@
+quiet_cmd_cc_s_h = CC      $@
+      cmd_cc_s_h = $(CC) $(c_flags) -S -o $@ -x c /dev/null -include $<
 
-$(obj)/%.hdrtest.c:
-	$(call cmd,header_test)
+$(obj)/%.h.s: $(src)/%.h FORCE
+	$(call if_changed_dep,cc_s_h)
 
 # Compile assembler sources (.S)
 # ---------------------------------------------------------------------------
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3e630fcaffd1..55ae1ec65342 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -67,7 +67,7 @@ extra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-))
 endif
 
 # Test self-contained headers
-extra-$(CONFIG_HEADER_TEST) += $(patsubst %.h,%.hdrtest.o,$(header-test-y))
+extra-$(CONFIG_HEADER_TEST) += $(addsuffix .s, $(header-test-y))
 
 # Add subdir path
 
-- 
2.17.1


^ permalink raw reply related

* [PATCH 4/7] kbuild: support header-test-pattern-y
From: Masahiro Yamada @ 2019-07-01  0:58 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Sam Ravnborg, Joel Fernandes, Masahiro Yamada, linux-doc,
	linux-kernel, Jonathan Corbet, Michal Marek
In-Reply-To: <20190701005845.12475-1-yamada.masahiro@socionext.com>

In my view, most of headers can be self-contained. So, it would be
tedious to add every header to header-test-y explicitly. We usually
end up with "all headers with some exceptions".

There are two types in exceptions:

[1] headers that are never compiled as standalone units

  For examples, include/linux/compiler-gcc.h is not intended for
  direct inclusion. We should always exclude such ones.

[2] headers that are conditionally compiled as standalone units

  Some headers can be compiled only for particular architectures.
  For example, include/linux/arm-cci.h can be compiled only for
  arm/arm64 because it requires <asm/arm-cci.h> to exist.
  Clang can compile include/soc/nps/mtm.h only for arc because
  it contains an arch-specific register in inline assembler.

So, you can write Makefile like this:

  header-test-                += linux/compiler-gcc.h
  header-test-$(CONFIG_ARM)   += linux/arm-cci.h
  header-test-$(CONFIG_ARM64) += linux/arm-cci.h
  header-test-$(CONFIG_ARC)   += soc/nps/mtm.h

The new syntax header-test-pattern-y will be useful to specify
"the rest".

The typical usage is like this:

  header-test-pattern-y += */*.h

This will add all the headers in sub-directories to the test coverage,
excluding $(header-test-). In this regards, header-test-pattern-y
behaves like a weaker variant of header-test-y.

Caveat:
The patterns in header-test-pattern-y are prefixed with $(srctree)/$(src)/
but not $(objtree)/$(obj)/. Stale generated headers are often left over
when you traverse the git history without cleaning. Wildcard patterns for
$(objtree) may match to stale headers, which could fail to compile.
One pitfall is $(srctree)/$(src)/ and $(objtree)/$(obj)/ point to the
same directory for in-tree building. So, header-test-pattern-y should
be used with care since it can potentially match to stale headers.

Caveat2:
You could use wildcard for header-test-. For example,

  header-test- += asm-generic/%

... will exclude headers in asm-generic directory. Unfortunately, the
wildcard character is '%' instead of '*' here because this is evaluated
by $(filter-out ...) whereas header-test-pattern-y is evaluated by
$(wildcard ...). This is a kludge, but seems useful in some places...

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Tested-by: Jani Nikula <jani.nikula@intel.com>
---

Changes in v4: None
Changes in v3: None
Changes in v2:
  - New patch

 Documentation/kbuild/makefiles.txt | 10 ++++++++++
 scripts/Makefile.lib               | 11 +++++++++++
 2 files changed, 21 insertions(+)

diff --git a/Documentation/kbuild/makefiles.txt b/Documentation/kbuild/makefiles.txt
index 5080fec34609..b817e6cefb77 100644
--- a/Documentation/kbuild/makefiles.txt
+++ b/Documentation/kbuild/makefiles.txt
@@ -1025,6 +1025,16 @@ When kbuild executes, the following steps are followed (roughly):
 	i.e. compilable as standalone units. If CONFIG_HEADER_TEST is enabled,
 	this builds them as part of extra-y.
 
+    header-test-pattern-y
+
+	This works as a weaker version of header-test-y, and accepts wildcard
+	patterns. The typical usage is:
+
+		  header-test-pattern-y += *.h
+
+	This specifies all the files that matches to '*.h' in the current
+	directory, but the files in 'header-test-' are excluded.
+
 --- 6.7 Commands useful for building a boot image
 
 	Kbuild provides a few macros that are useful when building a
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 55ae1ec65342..281864fcf0fe 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -67,6 +67,17 @@ extra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-))
 endif
 
 # Test self-contained headers
+
+# Wildcard searches in $(srctree)/$(src)/, but not in $(objtree)/$(obj)/.
+# Stale generated headers are often left over, so pattern matching should
+# be avoided. Please notice $(srctree)/$(src)/ and $(objtree)/$(obj) point
+# to the same location for in-tree building. So, header-test-pattern-y should
+# be used with care.
+header-test-y	+= $(filter-out $(header-test-), \
+		$(patsubst $(srctree)/$(src)/%, %, \
+		$(wildcard $(addprefix $(srctree)/$(src)/, \
+		$(header-test-pattern-y)))))
+
 extra-$(CONFIG_HEADER_TEST) += $(addsuffix .s, $(header-test-y))
 
 # Add subdir path
-- 
2.17.1


^ permalink raw reply related

* Re: [PATCH] docs: ipmb: place it at driver-api and convert to ReST
From: Corey Minyard @ 2019-07-01  0:34 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: Linux Doc Mailing List, Mauro Carvalho Chehab, linux-kernel,
	Jonathan Corbet, Greg Kroah-Hartman, Randy Dunlap,
	Rafael J. Wysocki, Logan Gunthorpe, Asmaa Mnebhi, vadimp
In-Reply-To: <d23c36ca65fe6ad56af1723bf70f7a7f4154c410.1561804596.git.mchehab+samsung@kernel.org>

On Sat, Jun 29, 2019 at 07:36:46AM -0300, Mauro Carvalho Chehab wrote:
> No new doc should be added at the main Documentation/ directory.
> 
> Instead, new docs should be added as ReST files, within the
> Kernel documentation body.

Got it, thanks.

-corey

> 
> Fixes: 51bd6f291583 ("Add support for IPMB driver")
> Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
> ---
>  Documentation/driver-api/index.rst            |  1 +
>  .../{IPMB.txt => driver-api/ipmb.rst}         | 62 ++++++++++---------
>  2 files changed, 33 insertions(+), 30 deletions(-)
>  rename Documentation/{IPMB.txt => driver-api/ipmb.rst} (71%)
> 
> diff --git a/Documentation/driver-api/index.rst b/Documentation/driver-api/index.rst
> index e33849b948c7..e49c34bf16c0 100644
> --- a/Documentation/driver-api/index.rst
> +++ b/Documentation/driver-api/index.rst
> @@ -75,6 +75,7 @@ available subsections can be seen below.
>     dell_rbu
>     edid
>     eisa
> +   ipmb
>     isa
>     isapnp
>     generic-counter
> diff --git a/Documentation/IPMB.txt b/Documentation/driver-api/ipmb.rst
> similarity index 71%
> rename from Documentation/IPMB.txt
> rename to Documentation/driver-api/ipmb.rst
> index cd20c9764705..3ec3baed84c4 100644
> --- a/Documentation/IPMB.txt
> +++ b/Documentation/driver-api/ipmb.rst
> @@ -32,11 +32,11 @@ This driver works with the I2C driver and a userspace
>  program such as OpenIPMI:
>  
>  1) It is an I2C slave backend driver. So, it defines a callback
> -function to set the Satellite MC as an I2C slave.
> -This callback function handles the received IPMI requests.
> +   function to set the Satellite MC as an I2C slave.
> +   This callback function handles the received IPMI requests.
>  
>  2) It defines the read and write functions to enable a user
> -space program (such as OpenIPMI) to communicate with the kernel.
> +   space program (such as OpenIPMI) to communicate with the kernel.
>  
>  
>  Load the IPMB driver
> @@ -48,34 +48,35 @@ CONFIG_IPMB_DEVICE_INTERFACE=y
>  
>  1) If you want the driver to be loaded at boot time:
>  
> -a) Add this entry to your ACPI table, under the appropriate SMBus:
> +a) Add this entry to your ACPI table, under the appropriate SMBus::
>  
> -Device (SMB0) // Example SMBus host controller
> -{
> -  Name (_HID, "<Vendor-Specific HID>") // Vendor-Specific HID
> -  Name (_UID, 0) // Unique ID of particular host controller
> -  :
> -  :
> -    Device (IPMB)
> -    {
> -      Name (_HID, "IPMB0001") // IPMB device interface
> -      Name (_UID, 0) // Unique device identifier
> -    }
> -}
> +     Device (SMB0) // Example SMBus host controller
> +     {
> +     Name (_HID, "<Vendor-Specific HID>") // Vendor-Specific HID
> +     Name (_UID, 0) // Unique ID of particular host controller
> +     :
> +     :
> +       Device (IPMB)
> +       {
> +         Name (_HID, "IPMB0001") // IPMB device interface
> +         Name (_UID, 0) // Unique device identifier
> +       }
> +     }
>  
> -b) Example for device tree:
> +b) Example for device tree::
>  
> -&i2c2 {
> -         status = "okay";
> +     &i2c2 {
> +            status = "okay";
>  
> -         ipmb@10 {
> -                 compatible = "ipmb-dev";
> -                 reg = <0x10>;
> -         };
> -};
> +            ipmb@10 {
> +                    compatible = "ipmb-dev";
> +                    reg = <0x10>;
> +            };
> +     };
>  
> -2) Manually from Linux:
> -modprobe ipmb-dev-int
> +2) Manually from Linux::
> +
> +     modprobe ipmb-dev-int
>  
>  
>  Instantiate the device
> @@ -86,15 +87,16 @@ described in 'Documentation/i2c/instantiating-devices.rst'.
>  If you have multiple BMCs, each connected to your Satellite MC via
>  a different I2C bus, you can instantiate a device for each of
>  those BMCs.
> +
>  The name of the instantiated device contains the I2C bus number
> -associated with it as follows:
> +associated with it as follows::
>  
> -BMC1 ------ IPMB/I2C bus 1 ---------|   /dev/ipmb-1
> +  BMC1 ------ IPMB/I2C bus 1 ---------|   /dev/ipmb-1
>  				Satellite MC
> -BMC1 ------ IPMB/I2C bus 2 ---------|   /dev/ipmb-2
> +  BMC1 ------ IPMB/I2C bus 2 ---------|   /dev/ipmb-2
>  
>  For instance, you can instantiate the ipmb-dev-int device from
> -user space at the 7 bit address 0x10 on bus 2:
> +user space at the 7 bit address 0x10 on bus 2::
>  
>    # echo ipmb-dev 0x1010 > /sys/bus/i2c/devices/i2c-2/new_device
>  
> -- 
> 2.21.0
> 

^ permalink raw reply

* Re: [linux-kernel-mentees] [PATCH v1] Doc : fs : convert xfs.txt to ReST
From: Jonathan Corbet @ 2019-06-30 17:32 UTC (permalink / raw)
  To: Sheriff Esseson
  Cc: skhan, linux-kernel-mentees, darrick.wong, linux-xfs, linux-doc,
	linux-kernel
In-Reply-To: <20190630165046.GB5193@localhost>

On Sun, 30 Jun 2019 17:50:46 +0100
Sheriff Esseson <sheriffesseson@gmail.com> wrote:

> On Sat, Jun 29, 2019 at 09:57:59PM +0100, Sheriff Esseson wrote:
> > Signed-off-by: Sheriff Esseson <sheriffesseson@gmail.com>
> > ---
> > 
> > In v3:
> > Update MAINTAINERS. Fix Indentation/long line issues. Insert Sphinx tag.
> > 
> > -- 
> > 2.22.0
> >   
> 
> Signed-off-by: Sheriff Esseson <sheriffesseson@gmail.com>
> ---
> 
> In v4:
> dax.txt : 
> 	fix broken reference to xfs.rst

I will now repeat what I said yesterday:

> Please do us a favor?  
> 
> 1) Post each patch standalone, without quoted text, ready to be applied.
> 
> 2) Wait a little while between postings so that you can address more than
> one comment at a time.
> 
> OK, two favors - off-by-one errors are my specialty...:)

I did actually mean that.  For "a little while" I meant "a few days or
so".  Please slow down a little.  In any case, we're getting closer to the
merge window, so this patch won't be applied for a while regardless.

Thanks,

jon

^ permalink raw reply

* [UPDATE][PATCH 10/10] tools/power/x86: A tool to validate Intel Speed Select commands
From: Srinivas Pandruvada @ 2019-06-30 17:14 UTC (permalink / raw)
  To: dvhart, andy, andriy.shevchenko, corbet
  Cc: rjw, alan, lenb, prarit, darcari, linux-doc, linux-kernel,
	platform-driver-x86, Srinivas Pandruvada

The Intel(R) Speed select technologies contains four features.

Performance profile:An non architectural mechanism that allows multiple
optimized performance profiles per system via static and/or dynamic
adjustment of core count, workload, Tjmax, and TDP, etc. aka ISS
in the documentation.

Base Frequency: Enables users to increase guaranteed base frequency on
certain cores (high priority cores) in exchange for lower base frequency
on remaining cores (low priority cores). aka PBF in the documenation.

Turbo frequency: Enables the ability to set different turbo ratio limits
to cores based on priority. aka FACT in the documentation.

Core power: An Interface that allows user to define per core/tile
priority.

There is a multi level help for commands and options. This can be used
to check required arguments for each feature and commands for the
feature.

To start navigating the features start with

$sudo intel-speed-select --help

For help on a specific feature for example
$sudo intel-speed-select perf-profile --help

To get help for a command for a feature for example
$sudo intel-speed-select perf-profile get-lock-status --help

Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
---
Updates:
- Copied Makefile from tools/gpio and moified the Makefile here
- Added entry to tools/build/Makefile
- Rename directory to match the executable name
- Fix one error message

 tools/Makefile                                |   12 +-
 tools/power/x86/intel-speed-select/Build      |    1 +
 tools/power/x86/intel-speed-select/Makefile   |   56 +
 .../x86/intel-speed-select/isst-config.c      | 1607 +++++++++++++++++
 .../power/x86/intel-speed-select/isst-core.c  |  721 ++++++++
 .../x86/intel-speed-select/isst-display.c     |  479 +++++
 tools/power/x86/intel-speed-select/isst.h     |  231 +++
 7 files changed, 3102 insertions(+), 5 deletions(-)
 create mode 100644 tools/power/x86/intel-speed-select/Build
 create mode 100644 tools/power/x86/intel-speed-select/Makefile
 create mode 100644 tools/power/x86/intel-speed-select/isst-config.c
 create mode 100644 tools/power/x86/intel-speed-select/isst-core.c
 create mode 100644 tools/power/x86/intel-speed-select/isst-display.c
 create mode 100644 tools/power/x86/intel-speed-select/isst.h

diff --git a/tools/Makefile b/tools/Makefile
index 3dfd72ae6c1a..68defd7ecf5d 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -19,6 +19,7 @@ help:
 	@echo '  gpio                   - GPIO tools'
 	@echo '  hv                     - tools used when in Hyper-V clients'
 	@echo '  iio                    - IIO tools'
+	@echo '  intel-speed-select     - Intel Speed Select tool'
 	@echo '  kvm_stat               - top-like utility for displaying kvm statistics'
 	@echo '  leds                   - LEDs  tools'
 	@echo '  liblockdep             - user-space wrapper for kernel locking-validator'
@@ -82,7 +83,7 @@ perf: FORCE
 selftests: FORCE
 	$(call descend,testing/$@)
 
-turbostat x86_energy_perf_policy: FORCE
+turbostat x86_energy_perf_policy intel-speed-select: FORCE
 	$(call descend,power/x86/$@)
 
 tmon: FORCE
@@ -115,7 +116,7 @@ liblockdep_install:
 selftests_install:
 	$(call descend,testing/$(@:_install=),install)
 
-turbostat_install x86_energy_perf_policy_install:
+turbostat_install x86_energy_perf_policy_install intel-speed-select_install:
 	$(call descend,power/x86/$(@:_install=),install)
 
 tmon_install:
@@ -132,7 +133,7 @@ install: acpi_install cgroup_install cpupower_install gpio_install \
 		perf_install selftests_install turbostat_install usb_install \
 		virtio_install vm_install bpf_install x86_energy_perf_policy_install \
 		tmon_install freefall_install objtool_install kvm_stat_install \
-		wmi_install pci_install debugging_install
+		wmi_install pci_install debugging_install intel-speed-select_install
 
 acpi_clean:
 	$(call descend,power/acpi,clean)
@@ -162,7 +163,7 @@ perf_clean:
 selftests_clean:
 	$(call descend,testing/$(@:_clean=),clean)
 
-turbostat_clean x86_energy_perf_policy_clean:
+turbostat_clean x86_energy_perf_policy_clean intel-speed-select_clean:
 	$(call descend,power/x86/$(@:_clean=),clean)
 
 tmon_clean:
@@ -178,6 +179,7 @@ clean: acpi_clean cgroup_clean cpupower_clean hv_clean firewire_clean \
 		perf_clean selftests_clean turbostat_clean spi_clean usb_clean virtio_clean \
 		vm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
 		freefall_clean build_clean libbpf_clean libsubcmd_clean liblockdep_clean \
-		gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean debugging_clean
+		gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean debugging_clean \
+		intel-speed-select_clean
 
 .PHONY: FORCE
diff --git a/tools/power/x86/intel-speed-select/Build b/tools/power/x86/intel-speed-select/Build
new file mode 100644
index 000000000000..b61456d75190
--- /dev/null
+++ b/tools/power/x86/intel-speed-select/Build
@@ -0,0 +1 @@
+intel-speed-select-y +=  isst-config.o isst-core.o isst-display.o
diff --git a/tools/power/x86/intel-speed-select/Makefile b/tools/power/x86/intel-speed-select/Makefile
new file mode 100644
index 000000000000..12c6939dca2a
--- /dev/null
+++ b/tools/power/x86/intel-speed-select/Makefile
@@ -0,0 +1,56 @@
+# SPDX-License-Identifier: GPL-2.0
+include ../../../scripts/Makefile.include
+
+bindir ?= /usr/bin
+
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+endif
+
+# Do not use make's built-in rules
+# (this improves performance and avoids hard-to-debug behaviour);
+MAKEFLAGS += -r
+
+override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+
+ALL_TARGETS := intel-speed-select
+ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
+
+all: $(ALL_PROGRAMS)
+
+export srctree OUTPUT CC LD CFLAGS
+include $(srctree)/tools/build/Makefile.include
+
+#
+# We need the following to be outside of kernel tree
+#
+$(OUTPUT)include/linux/isst_if.h: ../../../../include/uapi/linux/isst_if.h
+	mkdir -p $(OUTPUT)include/linux 2>&1 || true
+	ln -sf $(CURDIR)/../../../../include/uapi/linux/isst_if.h $@
+
+prepare: $(OUTPUT)include/linux/isst_if.h
+
+ISST_IN := $(OUTPUT)intel-speed-select-in.o
+
+$(ISST_IN): prepare FORCE
+	$(Q)$(MAKE) $(build)=intel-speed-select
+$(OUTPUT)intel-speed-select: $(ISST_IN)
+	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
+
+clean:
+	rm -f $(ALL_PROGRAMS)
+	rm -rf $(OUTPUT)include/linux/isst_if.h
+	find $(if $(OUTPUT),$(OUTPUT),.) -name '*.o' -delete -o -name '\.*.d' -delete
+
+install: $(ALL_PROGRAMS)
+	install -d -m 755 $(DESTDIR)$(bindir);		\
+	for program in $(ALL_PROGRAMS); do		\
+		install $$program $(DESTDIR)$(bindir);	\
+	done
+
+FORCE:
+
+.PHONY: all install clean FORCE prepare
diff --git a/tools/power/x86/intel-speed-select/isst-config.c b/tools/power/x86/intel-speed-select/isst-config.c
new file mode 100644
index 000000000000..91c5ad1685a1
--- /dev/null
+++ b/tools/power/x86/intel-speed-select/isst-config.c
@@ -0,0 +1,1607 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Intel Speed Select -- Enumerate and control features
+ * Copyright (c) 2019 Intel Corporation.
+ */
+
+#include <linux/isst_if.h>
+
+#include "isst.h"
+
+struct process_cmd_struct {
+	char *feature;
+	char *command;
+	void (*process_fn)(void);
+};
+
+static const char *version_str = "v1.0";
+static const int supported_api_ver = 1;
+static struct isst_if_platform_info isst_platform_info;
+static char *progname;
+static int debug_flag;
+static FILE *outf;
+
+static int cpu_model;
+
+#define MAX_CPUS_IN_ONE_REQ 64
+static short max_target_cpus;
+static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
+
+static int topo_max_cpus;
+static size_t present_cpumask_size;
+static cpu_set_t *present_cpumask;
+static size_t target_cpumask_size;
+static cpu_set_t *target_cpumask;
+static int tdp_level = 0xFF;
+static int fact_bucket = 0xFF;
+static int fact_avx = 0xFF;
+static unsigned long long fact_trl;
+static int out_format_json;
+static int cmd_help;
+
+/* clos related */
+static int current_clos = -1;
+static int clos_epp = -1;
+static int clos_prop_prio = -1;
+static int clos_min = -1;
+static int clos_max = -1;
+static int clos_desired = -1;
+static int clos_priority_type;
+
+struct _cpu_map {
+	unsigned short core_id;
+	unsigned short pkg_id;
+	unsigned short die_id;
+	unsigned short punit_cpu;
+	unsigned short punit_cpu_core;
+};
+struct _cpu_map *cpu_map;
+
+void debug_printf(const char *format, ...)
+{
+	va_list args;
+
+	va_start(args, format);
+
+	if (debug_flag)
+		vprintf(format, args);
+
+	va_end(args);
+}
+
+static void update_cpu_model(void)
+{
+	unsigned int ebx, ecx, edx;
+	unsigned int fms, family;
+
+	__cpuid(1, fms, ebx, ecx, edx);
+	family = (fms >> 8) & 0xf;
+	cpu_model = (fms >> 4) & 0xf;
+	if (family == 6 || family == 0xf)
+		cpu_model += ((fms >> 16) & 0xf) << 4;
+}
+
+/* Open a file, and exit on failure */
+static FILE *fopen_or_exit(const char *path, const char *mode)
+{
+	FILE *filep = fopen(path, mode);
+
+	if (!filep)
+		err(1, "%s: open failed", path);
+
+	return filep;
+}
+
+/* Parse a file containing a single int */
+static int parse_int_file(int fatal, const char *fmt, ...)
+{
+	va_list args;
+	char path[PATH_MAX];
+	FILE *filep;
+	int value;
+
+	va_start(args, fmt);
+	vsnprintf(path, sizeof(path), fmt, args);
+	va_end(args);
+	if (fatal) {
+		filep = fopen_or_exit(path, "r");
+	} else {
+		filep = fopen(path, "r");
+		if (!filep)
+			return -1;
+	}
+	if (fscanf(filep, "%d", &value) != 1)
+		err(1, "%s: failed to parse number from file", path);
+	fclose(filep);
+
+	return value;
+}
+
+int cpufreq_sysfs_present(void)
+{
+	DIR *dir;
+
+	dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
+	if (dir) {
+		closedir(dir);
+		return 1;
+	}
+
+	return 0;
+}
+
+int out_format_is_json(void)
+{
+	return out_format_json;
+}
+
+int get_physical_package_id(int cpu)
+{
+	return parse_int_file(
+		1, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
+		cpu);
+}
+
+int get_physical_core_id(int cpu)
+{
+	return parse_int_file(
+		1, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
+}
+
+int get_physical_die_id(int cpu)
+{
+	int ret;
+
+	ret = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/topology/die_id",
+			     cpu);
+	if (ret < 0)
+		ret = 0;
+
+	return ret;
+}
+
+int get_topo_max_cpus(void)
+{
+	return topo_max_cpus;
+}
+
+#define MAX_PACKAGE_COUNT 8
+#define MAX_DIE_PER_PACKAGE 2
+static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
+							    void *, void *),
+					   void *arg1, void *arg2, void *arg3,
+					   void *arg4)
+{
+	int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
+	int pkg_index = 0, i;
+
+	memset(max_packages, 0xff, sizeof(max_packages));
+	for (i = 0; i < topo_max_cpus; ++i) {
+		int j, online, pkg_id, die_id = 0, skip = 0;
+
+		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
+			continue;
+		if (i)
+			online = parse_int_file(
+				1, "/sys/devices/system/cpu/cpu%d/online", i);
+		else
+			online =
+				1; /* online entry for CPU 0 needs some special configs */
+
+		die_id = get_physical_die_id(i);
+		if (die_id < 0)
+			die_id = 0;
+		pkg_id = get_physical_package_id(i);
+		/* Create an unique id for package, die combination to store */
+		pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
+
+		for (j = 0; j < pkg_index; ++j) {
+			if (max_packages[j] == pkg_id) {
+				skip = 1;
+				break;
+			}
+		}
+
+		if (!skip && online && callback) {
+			callback(i, arg1, arg2, arg3, arg4);
+			max_packages[pkg_index++] = pkg_id;
+		}
+	}
+}
+
+static void for_each_online_target_cpu_in_set(
+	void (*callback)(int, void *, void *, void *, void *), void *arg1,
+	void *arg2, void *arg3, void *arg4)
+{
+	int i;
+
+	for (i = 0; i < topo_max_cpus; ++i) {
+		int online;
+
+		if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
+			continue;
+		if (i)
+			online = parse_int_file(
+				1, "/sys/devices/system/cpu/cpu%d/online", i);
+		else
+			online =
+				1; /* online entry for CPU 0 needs some special configs */
+
+		if (online && callback)
+			callback(i, arg1, arg2, arg3, arg4);
+	}
+}
+
+#define BITMASK_SIZE 32
+static void set_max_cpu_num(void)
+{
+	FILE *filep;
+	unsigned long dummy;
+
+	topo_max_cpus = 0;
+	filep = fopen_or_exit(
+		"/sys/devices/system/cpu/cpu0/topology/thread_siblings", "r");
+	while (fscanf(filep, "%lx,", &dummy) == 1)
+		topo_max_cpus += BITMASK_SIZE;
+	fclose(filep);
+	topo_max_cpus--; /* 0 based */
+
+	debug_printf("max cpus %d\n", topo_max_cpus);
+}
+
+size_t alloc_cpu_set(cpu_set_t **cpu_set)
+{
+	cpu_set_t *_cpu_set;
+	size_t size;
+
+	_cpu_set = CPU_ALLOC((topo_max_cpus + 1));
+	if (_cpu_set == NULL)
+		err(3, "CPU_ALLOC");
+	size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
+	CPU_ZERO_S(size, _cpu_set);
+
+	*cpu_set = _cpu_set;
+	return size;
+}
+
+void free_cpu_set(cpu_set_t *cpu_set)
+{
+	CPU_FREE(cpu_set);
+}
+
+static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
+static void set_cpu_present_cpu_mask(void)
+{
+	size_t size;
+	DIR *dir;
+	int i;
+
+	size = alloc_cpu_set(&present_cpumask);
+	present_cpumask_size = size;
+	for (i = 0; i < topo_max_cpus; ++i) {
+		char buffer[256];
+
+		snprintf(buffer, sizeof(buffer),
+			 "/sys/devices/system/cpu/cpu%d", i);
+		dir = opendir(buffer);
+		if (dir) {
+			int pkg_id, die_id;
+
+			CPU_SET_S(i, size, present_cpumask);
+			die_id = get_physical_die_id(i);
+			if (die_id < 0)
+				die_id = 0;
+
+			pkg_id = get_physical_package_id(i);
+			if (pkg_id < MAX_PACKAGE_COUNT &&
+			    die_id < MAX_DIE_PER_PACKAGE)
+				cpu_cnt[pkg_id][die_id]++;
+		}
+		closedir(dir);
+	}
+}
+
+int get_cpu_count(int pkg_id, int die_id)
+{
+	if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
+		return cpu_cnt[pkg_id][die_id] + 1;
+
+	return 0;
+}
+
+static void set_cpu_target_cpu_mask(void)
+{
+	size_t size;
+	int i;
+
+	size = alloc_cpu_set(&target_cpumask);
+	target_cpumask_size = size;
+	for (i = 0; i < max_target_cpus; ++i) {
+		if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
+				 present_cpumask))
+			continue;
+
+		CPU_SET_S(target_cpus[i], size, target_cpumask);
+	}
+}
+
+static void create_cpu_map(void)
+{
+	const char *pathname = "/dev/isst_interface";
+	int i, fd = 0;
+	struct isst_if_cpu_maps map;
+
+	cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
+	if (!cpu_map)
+		err(3, "cpumap");
+
+	fd = open(pathname, O_RDWR);
+	if (fd < 0)
+		err(-1, "%s open failed", pathname);
+
+	for (i = 0; i < topo_max_cpus; ++i) {
+		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
+			continue;
+
+		map.cmd_count = 1;
+		map.cpu_map[0].logical_cpu = i;
+
+		debug_printf(" map logical_cpu:%d\n",
+			     map.cpu_map[0].logical_cpu);
+		if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
+			perror("ISST_IF_GET_PHY_ID");
+			fprintf(outf, "Error: map logical_cpu:%d\n",
+				map.cpu_map[0].logical_cpu);
+			continue;
+		}
+		cpu_map[i].core_id = get_physical_core_id(i);
+		cpu_map[i].pkg_id = get_physical_package_id(i);
+		cpu_map[i].die_id = get_physical_die_id(i);
+		cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
+		cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
+					     1); // shift to get core id
+
+		debug_printf(
+			"map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
+			i, cpu_map[i].core_id, cpu_map[i].die_id,
+			cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
+			cpu_map[i].punit_cpu_core);
+	}
+
+	if (fd)
+		close(fd);
+}
+
+int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
+{
+	int i;
+
+	for (i = 0; i < topo_max_cpus; ++i) {
+		if (cpu_map[i].pkg_id == pkg_id &&
+		    cpu_map[i].die_id == die_id &&
+		    cpu_map[i].punit_cpu_core == punit_core_id)
+			return i;
+	}
+
+	return -EINVAL;
+}
+
+void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
+				      size_t core_cpumask_size,
+				      cpu_set_t *core_cpumask, int *cpu_cnt)
+{
+	int i, cnt = 0;
+	int die_id, pkg_id;
+
+	*cpu_cnt = 0;
+	die_id = get_physical_die_id(cpu);
+	pkg_id = get_physical_package_id(cpu);
+
+	for (i = 0; i < 64; ++i) {
+		if (core_mask & BIT(i)) {
+			int j;
+
+			for (j = 0; j < topo_max_cpus; ++j) {
+				if (cpu_map[j].pkg_id == pkg_id &&
+				    cpu_map[j].die_id == die_id &&
+				    cpu_map[j].punit_cpu_core == i) {
+					CPU_SET_S(j, core_cpumask_size,
+						  core_cpumask);
+					++cnt;
+				}
+			}
+		}
+	}
+
+	*cpu_cnt = cnt;
+}
+
+int find_phy_core_num(int logical_cpu)
+{
+	if (logical_cpu < topo_max_cpus)
+		return cpu_map[logical_cpu].punit_cpu_core;
+
+	return -EINVAL;
+}
+
+static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
+				  unsigned int *value)
+{
+	struct isst_if_io_regs io_regs;
+	const char *pathname = "/dev/isst_interface";
+	int cmd;
+	int fd;
+
+	debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
+
+	fd = open(pathname, O_RDWR);
+	if (fd < 0)
+		err(-1, "%s open failed", pathname);
+
+	io_regs.req_count = 1;
+	io_regs.io_reg[0].logical_cpu = cpu;
+	io_regs.io_reg[0].reg = reg;
+	cmd = ISST_IF_IO_CMD;
+	if (write) {
+		io_regs.io_reg[0].read_write = 1;
+		io_regs.io_reg[0].value = *value;
+	} else {
+		io_regs.io_reg[0].read_write = 0;
+	}
+
+	if (ioctl(fd, cmd, &io_regs) == -1) {
+		perror("ISST_IF_IO_CMD");
+		fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
+			cpu, reg, write);
+	} else {
+		if (!write)
+			*value = io_regs.io_reg[0].value;
+
+		debug_printf(
+			"mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
+			cpu, reg, write, *value);
+	}
+
+	close(fd);
+
+	return 0;
+}
+
+int isst_send_mbox_command(unsigned int cpu, unsigned char command,
+			   unsigned char sub_command, unsigned int parameter,
+			   unsigned int req_data, unsigned int *resp)
+{
+	const char *pathname = "/dev/isst_interface";
+	int fd;
+	struct isst_if_mbox_cmds mbox_cmds = { 0 };
+
+	debug_printf(
+		"mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
+		cpu, command, sub_command, parameter, req_data);
+
+	if (isst_platform_info.mmio_supported && command == CONFIG_CLOS) {
+		unsigned int value;
+		int write = 0;
+		int clos_id, core_id, ret = 0;
+
+		debug_printf("CLOS %d\n", cpu);
+
+		if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
+			value = req_data;
+			write = 1;
+		}
+
+		switch (sub_command) {
+		case CLOS_PQR_ASSOC:
+			core_id = parameter & 0xff;
+			ret = isst_send_mmio_command(
+				cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
+				&value);
+			if (!ret && !write)
+				*resp = value;
+			break;
+		case CLOS_PM_CLOS:
+			clos_id = parameter & 0x03;
+			ret = isst_send_mmio_command(
+				cpu, PM_CLOS_OFFSET + clos_id * 4, write,
+				&value);
+			if (!ret && !write)
+				*resp = value;
+			break;
+		case CLOS_PM_QOS_CONFIG:
+			ret = isst_send_mmio_command(cpu, PM_QOS_CONFIG_OFFSET,
+						     write, &value);
+			if (!ret && !write)
+				*resp = value;
+			break;
+		case CLOS_STATUS:
+			break;
+		default:
+			break;
+		}
+		return ret;
+	}
+
+	mbox_cmds.cmd_count = 1;
+	mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
+	mbox_cmds.mbox_cmd[0].command = command;
+	mbox_cmds.mbox_cmd[0].sub_command = sub_command;
+	mbox_cmds.mbox_cmd[0].parameter = parameter;
+	mbox_cmds.mbox_cmd[0].req_data = req_data;
+
+	fd = open(pathname, O_RDWR);
+	if (fd < 0)
+		err(-1, "%s open failed", pathname);
+
+	if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
+		perror("ISST_IF_MBOX_COMMAND");
+		fprintf(outf,
+			"Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
+			cpu, command, sub_command, parameter, req_data);
+	} else {
+		*resp = mbox_cmds.mbox_cmd[0].resp_data;
+		debug_printf(
+			"mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
+			cpu, command, sub_command, parameter, req_data, *resp);
+	}
+
+	close(fd);
+
+	return 0;
+}
+
+int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
+			  unsigned long long *req_resp)
+{
+	struct isst_if_msr_cmds msr_cmds;
+	const char *pathname = "/dev/isst_interface";
+	int fd;
+
+	fd = open(pathname, O_RDWR);
+	if (fd < 0)
+		err(-1, "%s open failed", pathname);
+
+	msr_cmds.cmd_count = 1;
+	msr_cmds.msr_cmd[0].logical_cpu = cpu;
+	msr_cmds.msr_cmd[0].msr = msr;
+	msr_cmds.msr_cmd[0].read_write = write;
+	if (write)
+		msr_cmds.msr_cmd[0].data = *req_resp;
+
+	if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
+		perror("ISST_IF_MSR_COMMAD");
+		fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
+			cpu, msr, write);
+	} else {
+		if (!write)
+			*req_resp = msr_cmds.msr_cmd[0].data;
+
+		debug_printf(
+			"msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
+			cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
+	}
+
+	close(fd);
+
+	return 0;
+}
+
+static int isst_fill_platform_info(void)
+{
+	const char *pathname = "/dev/isst_interface";
+	int fd;
+
+	fd = open(pathname, O_RDWR);
+	if (fd < 0)
+		err(-1, "%s open failed", pathname);
+
+	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
+		perror("ISST_IF_GET_PLATFORM_INFO");
+		close(fd);
+		return -1;
+	}
+
+	close(fd);
+
+	return 0;
+}
+
+static void isst_print_platform_information(void)
+{
+	struct isst_if_platform_info platform_info;
+	const char *pathname = "/dev/isst_interface";
+	int fd;
+
+	fd = open(pathname, O_RDWR);
+	if (fd < 0)
+		err(-1, "%s open failed", pathname);
+
+	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
+		perror("ISST_IF_GET_PLATFORM_INFO");
+	} else {
+		fprintf(outf, "Platform: API version : %d\n",
+			platform_info.api_version);
+		fprintf(outf, "Platform: Driver version : %d\n",
+			platform_info.driver_version);
+		fprintf(outf, "Platform: mbox supported : %d\n",
+			platform_info.mbox_supported);
+		fprintf(outf, "Platform: mmio supported : %d\n",
+			platform_info.mmio_supported);
+	}
+
+	close(fd);
+
+	exit(0);
+}
+
+static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
+				 void *arg4)
+{
+	int (*fn_ptr)(int cpu, void *arg);
+	int ret;
+
+	fn_ptr = arg1;
+	ret = fn_ptr(cpu, arg2);
+	if (ret)
+		perror("get_tdp_*");
+	else
+		isst_display_result(cpu, outf, "perf-profile", (char *)arg3,
+				    *(unsigned int *)arg4);
+}
+
+#define _get_tdp_level(desc, suffix, object, help)                                \
+	static void get_tdp_##object(void)                                        \
+	{                                                                         \
+		struct isst_pkg_ctdp ctdp;                                        \
+\
+		if (cmd_help) {                                                   \
+			fprintf(stderr,                                           \
+				"Print %s [No command arguments are required]\n", \
+				help);                                            \
+			exit(0);                                                  \
+		}                                                                 \
+		isst_ctdp_display_information_start(outf);                        \
+		if (max_target_cpus)                                              \
+			for_each_online_target_cpu_in_set(                        \
+				exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
+				&ctdp, desc, &ctdp.object);                       \
+		else                                                              \
+			for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
+						       isst_get_ctdp_##suffix,    \
+						       &ctdp, desc,               \
+						       &ctdp.object);             \
+		isst_ctdp_display_information_end(outf);                          \
+	}
+
+_get_tdp_level("get-config-levels", levels, levels, "TDP levels");
+_get_tdp_level("get-config-version", levels, version, "TDP version");
+_get_tdp_level("get-config-enabled", levels, enabled, "TDP enable status");
+_get_tdp_level("get-config-current_level", levels, current_level,
+	       "Current TDP Level");
+_get_tdp_level("get-lock-status", levels, locked, "TDP lock status");
+
+static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
+				     void *arg3, void *arg4)
+{
+	struct isst_pkg_ctdp pkg_dev;
+	int ret;
+
+	memset(&pkg_dev, 0, sizeof(pkg_dev));
+	ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
+	if (ret) {
+		perror("isst_get_process_ctdp");
+	} else {
+		isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
+		isst_get_process_ctdp_complete(cpu, &pkg_dev);
+	}
+}
+
+static void dump_isst_config(void)
+{
+	if (cmd_help) {
+		fprintf(stderr,
+			"Print Intel(R) Speed Select Technology Performance profile configuration\n");
+		fprintf(stderr,
+			"including base frequency and turbo frequency configurations\n");
+		fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
+		fprintf(stderr,
+			"\tIf no arguments, dump information for all TDP levels\n");
+		exit(0);
+	}
+
+	isst_ctdp_display_information_start(outf);
+
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(dump_isst_config_for_cpu,
+						  NULL, NULL, NULL, NULL);
+	else
+		for_each_online_package_in_set(dump_isst_config_for_cpu, NULL,
+					       NULL, NULL, NULL);
+
+	isst_ctdp_display_information_end(outf);
+}
+
+static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
+				  void *arg4)
+{
+	int ret;
+
+	ret = isst_set_tdp_level(cpu, tdp_level);
+	if (ret)
+		perror("set_tdp_level_for_cpu");
+	else
+		isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
+				    ret);
+}
+
+static void set_tdp_level(void)
+{
+	if (cmd_help) {
+		fprintf(stderr, "Set Config TDP level\n");
+		fprintf(stderr,
+			"\t Arguments: -l|--level : Specify tdp level\n");
+		exit(0);
+	}
+
+	if (tdp_level == 0xff) {
+		fprintf(outf, "Invalid command: specify tdp_level\n");
+		exit(1);
+	}
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
+						  NULL, NULL, NULL);
+	else
+		for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
+					       NULL, NULL, NULL);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
+				    void *arg4)
+{
+	struct isst_pbf_info pbf_info;
+	int ret;
+
+	ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
+	if (ret) {
+		perror("isst_get_pbf_info");
+	} else {
+		isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
+		isst_get_pbf_info_complete(&pbf_info);
+	}
+}
+
+static void dump_pbf_config(void)
+{
+	if (cmd_help) {
+		fprintf(stderr,
+			"Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
+		fprintf(stderr,
+			"\tArguments: -l|--level : Specify tdp level\n");
+		exit(0);
+	}
+
+	if (tdp_level == 0xff) {
+		fprintf(outf, "Invalid command: specify tdp_level\n");
+		exit(1);
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(dump_pbf_config_for_cpu, NULL,
+						  NULL, NULL, NULL);
+	else
+		for_each_online_package_in_set(dump_pbf_config_for_cpu, NULL,
+					       NULL, NULL, NULL);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
+			    void *arg4)
+{
+	int ret;
+	int status = *(int *)arg4;
+
+	ret = isst_set_pbf_fact_status(cpu, 1, status);
+	if (ret) {
+		perror("isst_set_pbf");
+	} else {
+		if (status)
+			isst_display_result(cpu, outf, "base-freq", "enable",
+					    ret);
+		else
+			isst_display_result(cpu, outf, "base-freq", "disable",
+					    ret);
+	}
+}
+
+static void set_pbf_enable(void)
+{
+	int status = 1;
+
+	if (cmd_help) {
+		fprintf(stderr,
+			"Enable Intel Speed Select Technology base frequency feature [No command arguments are required]\n");
+		exit(0);
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
+						  NULL, &status);
+	else
+		for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
+					       NULL, &status);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void set_pbf_disable(void)
+{
+	int status = 0;
+
+	if (cmd_help) {
+		fprintf(stderr,
+			"Disable Intel Speed Select Technology base frequency feature [No command arguments are required]\n");
+		exit(0);
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
+						  NULL, &status);
+	else
+		for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
+					       NULL, &status);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
+				     void *arg3, void *arg4)
+{
+	struct isst_fact_info fact_info;
+	int ret;
+
+	ret = isst_get_fact_info(cpu, tdp_level, &fact_info);
+	if (ret)
+		perror("isst_get_fact_bucket_info");
+	else
+		isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
+					      fact_avx, &fact_info);
+}
+
+static void dump_fact_config(void)
+{
+	if (cmd_help) {
+		fprintf(stderr,
+			"Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
+		fprintf(stderr,
+			"\tArguments: -l|--level : Specify tdp level\n");
+		fprintf(stderr,
+			"\tArguments: -b|--bucket : Bucket index to dump\n");
+		fprintf(stderr,
+			"\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
+		exit(0);
+	}
+
+	if (tdp_level == 0xff) {
+		fprintf(outf, "Invalid command: specify tdp_level\n");
+		exit(1);
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
+						  NULL, NULL, NULL, NULL);
+	else
+		for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
+					       NULL, NULL, NULL);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
+			     void *arg4)
+{
+	int ret;
+	int status = *(int *)arg4;
+
+	ret = isst_set_pbf_fact_status(cpu, 0, status);
+	if (ret)
+		perror("isst_set_fact");
+	else {
+		if (status) {
+			struct isst_pkg_ctdp pkg_dev;
+
+			ret = isst_get_ctdp_levels(cpu, &pkg_dev);
+			if (ret) {
+				isst_display_result(cpu, outf, "turbo-freq",
+						    "enable", ret);
+				return;
+			}
+			ret = isst_set_trl(cpu, fact_trl);
+			isst_display_result(cpu, outf, "turbo-freq", "enable",
+					    ret);
+		} else {
+			/* Since we modified TRL during Fact enable, restore it */
+			isst_set_trl_from_current_tdp(cpu, fact_trl);
+			isst_display_result(cpu, outf, "turbo-freq", "disable",
+					    ret);
+		}
+	}
+}
+
+static void set_fact_enable(void)
+{
+	int status = 1;
+
+	if (cmd_help) {
+		fprintf(stderr,
+			"Enable Intel Speed Select Technology Turbo frequency feature\n");
+		fprintf(stderr,
+			"Optional: -t|--trl : Specify turbo ratio limit\n");
+		exit(0);
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
+						  NULL, &status);
+	else
+		for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
+					       NULL, &status);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void set_fact_disable(void)
+{
+	int status = 0;
+
+	if (cmd_help) {
+		fprintf(stderr,
+			"Disable Intel Speed Select Technology turbo frequency feature\n");
+		fprintf(stderr,
+			"Optional: -t|--trl : Specify turbo ratio limit\n");
+		exit(0);
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
+						  NULL, &status);
+	else
+		for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
+					       NULL, &status);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
+				   void *arg4)
+{
+	int ret;
+	int status = *(int *)arg4;
+
+	ret = isst_pm_qos_config(cpu, status, clos_priority_type);
+	if (ret) {
+		perror("isst_pm_qos_config");
+	} else {
+		if (status)
+			isst_display_result(cpu, outf, "core-power", "enable",
+					    ret);
+		else
+			isst_display_result(cpu, outf, "core-power", "disable",
+					    ret);
+	}
+}
+
+static void set_clos_enable(void)
+{
+	int status = 1;
+
+	if (cmd_help) {
+		fprintf(stderr, "Enable core-power for a package/die\n");
+		fprintf(stderr,
+			"\tClos Enable: Specify priority type with [--priority|-p]\n");
+		fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
+		exit(0);
+	}
+
+	if (cpufreq_sysfs_present()) {
+		fprintf(stderr,
+			"cpufreq subsystem and core-power enable will interfere with each other!\n");
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
+						  NULL, NULL, &status);
+	else
+		for_each_online_package_in_set(enable_clos_qos_config, NULL,
+					       NULL, NULL, &status);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void set_clos_disable(void)
+{
+	int status = 0;
+
+	if (cmd_help) {
+		fprintf(stderr,
+			"Disable core-power: [No command arguments are required]\n");
+		exit(0);
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
+						  NULL, NULL, &status);
+	else
+		for_each_online_package_in_set(enable_clos_qos_config, NULL,
+					       NULL, NULL, &status);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
+				     void *arg3, void *arg4)
+{
+	struct isst_clos_config clos_config;
+	int ret;
+
+	ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
+	if (ret)
+		perror("isst_pm_get_clos");
+	else
+		isst_clos_display_information(cpu, outf, current_clos,
+					      &clos_config);
+}
+
+static void dump_clos_config(void)
+{
+	if (cmd_help) {
+		fprintf(stderr,
+			"Print Intel Speed Select Technology core power configuration\n");
+		fprintf(stderr,
+			"\tArguments: [-c | --clos]: Specify clos id\n");
+		exit(0);
+	}
+	if (current_clos < 0 || current_clos > 3) {
+		fprintf(stderr, "Invalid clos id\n");
+		exit(0);
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
+						  NULL, NULL, NULL, NULL);
+	else
+		for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
+					       NULL, NULL, NULL);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
+				    void *arg4)
+{
+	struct isst_clos_config clos_config;
+	int ret;
+
+	clos_config.pkg_id = get_physical_package_id(cpu);
+	clos_config.die_id = get_physical_die_id(cpu);
+
+	clos_config.epp = clos_epp;
+	clos_config.clos_prop_prio = clos_prop_prio;
+	clos_config.clos_min = clos_min;
+	clos_config.clos_max = clos_max;
+	clos_config.clos_desired = clos_desired;
+	ret = isst_set_clos(cpu, current_clos, &clos_config);
+	if (ret)
+		perror("isst_set_clos");
+	else
+		isst_display_result(cpu, outf, "core-power", "config", ret);
+}
+
+static void set_clos_config(void)
+{
+	if (cmd_help) {
+		fprintf(stderr,
+			"Set core-power configuration for one of the four clos ids\n");
+		fprintf(stderr,
+			"\tSpecify targeted clos id with [--clos|-c]\n");
+		fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
+		fprintf(stderr,
+			"\tSpecify clos Proportional Priority [--weight|-w]\n");
+		fprintf(stderr, "\tSpecify clos min with [--min|-n]\n");
+		fprintf(stderr, "\tSpecify clos max with [--max|-m]\n");
+		fprintf(stderr, "\tSpecify clos desired with [--desired|-d]\n");
+		exit(0);
+	}
+
+	if (current_clos < 0 || current_clos > 3) {
+		fprintf(stderr, "Invalid clos id\n");
+		exit(0);
+	}
+	if (clos_epp < 0 || clos_epp > 0x0F) {
+		fprintf(stderr, "clos epp is not specified, default: 0\n");
+		clos_epp = 0;
+	}
+	if (clos_prop_prio < 0 || clos_prop_prio > 0x0F) {
+		fprintf(stderr,
+			"clos frequency weight is not specified, default: 0\n");
+		clos_prop_prio = 0;
+	}
+	if (clos_min < 0) {
+		fprintf(stderr, "clos min is not specified, default: 0\n");
+		clos_min = 0;
+	}
+	if (clos_max < 0) {
+		fprintf(stderr, "clos max is not specified, default: 0xff\n");
+		clos_max = 0xff;
+	}
+	if (clos_desired < 0) {
+		fprintf(stderr, "clos desired is not specified, default: 0\n");
+		clos_desired = 0x00;
+	}
+
+	isst_ctdp_display_information_start(outf);
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
+						  NULL, NULL, NULL);
+	else
+		for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
+					       NULL, NULL, NULL);
+	isst_ctdp_display_information_end(outf);
+}
+
+static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
+				   void *arg4)
+{
+	int ret;
+
+	ret = isst_clos_associate(cpu, current_clos);
+	if (ret)
+		perror("isst_clos_associate");
+	else
+		isst_display_result(cpu, outf, "core-power", "assoc", ret);
+}
+
+static void set_clos_assoc(void)
+{
+	if (cmd_help) {
+		fprintf(stderr, "Associate a clos id to a CPU\n");
+		fprintf(stderr,
+			"\tSpecify targeted clos id with [--clos|-c]\n");
+		exit(0);
+	}
+
+	if (current_clos < 0 || current_clos > 3) {
+		fprintf(stderr, "Invalid clos id\n");
+		exit(0);
+	}
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
+						  NULL, NULL, NULL);
+	else {
+		fprintf(stderr,
+			"Invalid target cpu. Specify with [-c|--cpu]\n");
+	}
+}
+
+static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
+				   void *arg4)
+{
+	int clos, ret;
+
+	ret = isst_clos_get_assoc_status(cpu, &clos);
+	if (ret)
+		perror("isst_clos_get_assoc_status");
+	else
+		isst_display_result(cpu, outf, "core-power", "get-assoc", clos);
+}
+
+static void get_clos_assoc(void)
+{
+	if (cmd_help) {
+		fprintf(stderr, "Get associate clos id to a CPU\n");
+		fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
+		exit(0);
+	}
+	if (max_target_cpus)
+		for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
+						  NULL, NULL, NULL);
+	else {
+		fprintf(stderr,
+			"Invalid target cpu. Specify with [-c|--cpu]\n");
+	}
+}
+
+static struct process_cmd_struct isst_cmds[] = {
+	{ "perf-profile", "get-lock-status", get_tdp_locked },
+	{ "perf-profile", "get-config-levels", get_tdp_levels },
+	{ "perf-profile", "get-config-version", get_tdp_version },
+	{ "perf-profile", "get-config-enabled", get_tdp_enabled },
+	{ "perf-profile", "get-config-current-level", get_tdp_current_level },
+	{ "perf-profile", "set-config-level", set_tdp_level },
+	{ "perf-profile", "info", dump_isst_config },
+	{ "base-freq", "info", dump_pbf_config },
+	{ "base-freq", "enable", set_pbf_enable },
+	{ "base-freq", "disable", set_pbf_disable },
+	{ "turbo-freq", "info", dump_fact_config },
+	{ "turbo-freq", "enable", set_fact_enable },
+	{ "turbo-freq", "disable", set_fact_disable },
+	{ "core-power", "info", dump_clos_config },
+	{ "core-power", "enable", set_clos_enable },
+	{ "core-power", "disable", set_clos_disable },
+	{ "core-power", "config", set_clos_config },
+	{ "core-power", "assoc", set_clos_assoc },
+	{ "core-power", "get-assoc", get_clos_assoc },
+	{ NULL, NULL, NULL }
+};
+
+/*
+ * parse cpuset with following syntax
+ * 1,2,4..6,8-10 and set bits in cpu_subset
+ */
+void parse_cpu_command(char *optarg)
+{
+	unsigned int start, end;
+	char *next;
+
+	next = optarg;
+
+	while (next && *next) {
+		if (*next == '-') /* no negative cpu numbers */
+			goto error;
+
+		start = strtoul(next, &next, 10);
+
+		if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
+			target_cpus[max_target_cpus++] = start;
+
+		if (*next == '\0')
+			break;
+
+		if (*next == ',') {
+			next += 1;
+			continue;
+		}
+
+		if (*next == '-') {
+			next += 1; /* start range */
+		} else if (*next == '.') {
+			next += 1;
+			if (*next == '.')
+				next += 1; /* start range */
+			else
+				goto error;
+		}
+
+		end = strtoul(next, &next, 10);
+		if (end <= start)
+			goto error;
+
+		while (++start <= end) {
+			if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
+				target_cpus[max_target_cpus++] = start;
+		}
+
+		if (*next == ',')
+			next += 1;
+		else if (*next != '\0')
+			goto error;
+	}
+
+#ifdef DEBUG
+	{
+		int i;
+
+		for (i = 0; i < max_target_cpus; ++i)
+			printf("cpu [%d] in arg\n", target_cpus[i]);
+	}
+#endif
+	return;
+
+error:
+	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
+	exit(-1);
+}
+
+static void parse_cmd_args(int argc, int start, char **argv)
+{
+	int opt;
+	int option_index;
+
+	static struct option long_options[] = {
+		{ "bucket", required_argument, 0, 'b' },
+		{ "level", required_argument, 0, 'l' },
+		{ "trl-type", required_argument, 0, 'r' },
+		{ "trl", required_argument, 0, 't' },
+		{ "help", no_argument, 0, 'h' },
+		{ "clos", required_argument, 0, 'c' },
+		{ "desired", required_argument, 0, 'd' },
+		{ "epp", required_argument, 0, 'e' },
+		{ "min", required_argument, 0, 'n' },
+		{ "max", required_argument, 0, 'm' },
+		{ "priority", required_argument, 0, 'p' },
+		{ "weight", required_argument, 0, 'w' },
+		{ 0, 0, 0, 0 }
+	};
+
+	option_index = start;
+
+	optind = start + 1;
+	while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:h",
+				  long_options, &option_index)) != -1) {
+		switch (opt) {
+		case 'b':
+			fact_bucket = atoi(optarg);
+			break;
+		case 'h':
+			cmd_help = 1;
+			break;
+		case 'l':
+			tdp_level = atoi(optarg);
+			break;
+		case 't':
+			sscanf(optarg, "0x%llx", &fact_trl);
+			break;
+		case 'r':
+			if (!strncmp(optarg, "sse", 3)) {
+				fact_avx = 0x01;
+			} else if (!strncmp(optarg, "avx2", 4)) {
+				fact_avx = 0x02;
+			} else if (!strncmp(optarg, "avx512", 4)) {
+				fact_avx = 0x04;
+			} else {
+				fprintf(outf, "Invalid sse,avx options\n");
+				exit(1);
+			}
+			break;
+		/* CLOS related */
+		case 'c':
+			current_clos = atoi(optarg);
+			printf("clos %d\n", current_clos);
+			break;
+		case 'd':
+			clos_desired = atoi(optarg);
+			break;
+		case 'e':
+			clos_epp = atoi(optarg);
+			break;
+		case 'n':
+			clos_min = atoi(optarg);
+			break;
+		case 'm':
+			clos_max = atoi(optarg);
+			break;
+		case 'p':
+			clos_priority_type = atoi(optarg);
+			break;
+		case 'w':
+			clos_prop_prio = atoi(optarg);
+			break;
+		default:
+			printf("no match\n");
+		}
+	}
+}
+
+static void isst_help(void)
+{
+	printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
+		performance profiles per system via static and/or dynamic\n\
+		adjustment of core count, workload, Tjmax, and\n\
+		TDP, etc.\n");
+	printf("\nCommands : For feature=perf-profile\n");
+	printf("\tinfo\n");
+	printf("\tget-lock-status\n");
+	printf("\tget-config-levels\n");
+	printf("\tget-config-version\n");
+	printf("\tget-config-enabled\n");
+	printf("\tget-config-current-level\n");
+	printf("\tset-config-level\n");
+}
+
+static void pbf_help(void)
+{
+	printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
+		on certain cores (high priority cores) in exchange for lower\n\
+		base frequency on remaining cores (low priority cores).\n");
+	printf("\tcommand : info\n");
+	printf("\tcommand : enable\n");
+	printf("\tcommand : disable\n");
+}
+
+static void fact_help(void)
+{
+	printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
+		limits to cores based on priority.\n");
+	printf("\nCommand: For feature=turbo-freq\n");
+	printf("\tcommand : info\n");
+	printf("\tcommand : enable\n");
+	printf("\tcommand : disable\n");
+}
+
+static void core_power_help(void)
+{
+	printf("core-power:\tInterface that allows user to define per core/tile\n\
+		priority.\n");
+	printf("\nCommands : For feature=core-power\n");
+	printf("\tinfo\n");
+	printf("\tenable\n");
+	printf("\tdisable\n");
+	printf("\tconfig\n");
+	printf("\tassoc\n");
+	printf("\tget-assoc\n");
+}
+
+struct process_cmd_help_struct {
+	char *feature;
+	void (*process_fn)(void);
+};
+
+static struct process_cmd_help_struct isst_help_cmds[] = {
+	{ "perf-profile", isst_help },
+	{ "base-freq", pbf_help },
+	{ "turbo-freq", fact_help },
+	{ "core-power", core_power_help },
+	{ NULL, NULL }
+};
+
+void process_command(int argc, char **argv)
+{
+	int i = 0, matched = 0;
+	char *feature = argv[optind];
+	char *cmd = argv[optind + 1];
+
+	if (!feature || !cmd)
+		return;
+
+	debug_printf("feature name [%s] command [%s]\n", feature, cmd);
+	if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
+		while (isst_help_cmds[i].feature) {
+			if (!strcmp(isst_help_cmds[i].feature, feature)) {
+				isst_help_cmds[i].process_fn();
+				exit(0);
+			}
+			++i;
+		}
+	}
+
+	create_cpu_map();
+
+	i = 0;
+	while (isst_cmds[i].feature) {
+		if (!strcmp(isst_cmds[i].feature, feature) &&
+		    !strcmp(isst_cmds[i].command, cmd)) {
+			parse_cmd_args(argc, optind + 1, argv);
+			isst_cmds[i].process_fn();
+			matched = 1;
+			break;
+		}
+		++i;
+	}
+
+	if (!matched)
+		fprintf(stderr, "Invalid command\n");
+}
+
+static void usage(void)
+{
+	printf("Intel(R) Speed Select Technology\n");
+	printf("\nUsage:\n");
+	printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
+	printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features,\n");
+	printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
+	printf("\nFor help on each feature, use --h|--help\n");
+	printf("\tFor example:  intel-speed-select perf-profile -h\n");
+
+	printf("\nFor additional help on each command for a feature, use --h|--help\n");
+	printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
+	printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
+
+	printf("\nOPTIONS\n");
+	printf("\t[-c|--cpu] : logical cpu number\n");
+	printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
+	printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
+	printf("\t[-d|--debug] : Debug mode\n");
+	printf("\t[-h|--help] : Print help\n");
+	printf("\t[-i|--info] : Print platform information\n");
+	printf("\t[-o|--out] : Output file\n");
+	printf("\t\t\tDefault : stderr\n");
+	printf("\t[-f|--format] : output format [json|text]. Default: text\n");
+	printf("\t[-v|--version] : Print version\n");
+
+	printf("\nResult format\n");
+	printf("\tResult display uses a common format for each command:\n");
+	printf("\tResults are formatted in text/JSON with\n");
+	printf("\t\tPackage, Die, CPU, and command specific results.\n");
+	printf("\t\t\tFor Set commands, status is 0 for success and rest for failures\n");
+	exit(1);
+}
+
+static void print_version(void)
+{
+	fprintf(outf, "Version %s\n", version_str);
+	fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
+	exit(0);
+}
+
+static void cmdline(int argc, char **argv)
+{
+	int opt;
+	int option_index = 0;
+
+	static struct option long_options[] = {
+		{ "cpu", required_argument, 0, 'c' },
+		{ "debug", no_argument, 0, 'd' },
+		{ "format", required_argument, 0, 'f' },
+		{ "help", no_argument, 0, 'h' },
+		{ "info", no_argument, 0, 'i' },
+		{ "out", required_argument, 0, 'o' },
+		{ "version", no_argument, 0, 'v' },
+		{ 0, 0, 0, 0 }
+	};
+
+	progname = argv[0];
+	while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
+				       &option_index)) != -1) {
+		switch (opt) {
+		case 'c':
+			parse_cpu_command(optarg);
+			break;
+		case 'd':
+			debug_flag = 1;
+			printf("Debug Mode ON\n");
+			break;
+		case 'f':
+			if (!strncmp(optarg, "json", 4))
+				out_format_json = 1;
+			break;
+		case 'h':
+			usage();
+			break;
+		case 'i':
+			isst_print_platform_information();
+			break;
+		case 'o':
+			if (outf)
+				fclose(outf);
+			outf = fopen_or_exit(optarg, "w");
+			break;
+		case 'v':
+			print_version();
+			break;
+		default:
+			usage();
+		}
+	}
+
+	if (geteuid() != 0) {
+		fprintf(stderr, "Must run as root\n");
+		exit(0);
+	}
+
+	if (optind > (argc - 2)) {
+		fprintf(stderr, "Feature name and|or command not specified\n");
+		exit(0);
+	}
+	update_cpu_model();
+	printf("Intel(R) Speed Select Technology\n");
+	printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
+	set_max_cpu_num();
+	set_cpu_present_cpu_mask();
+	set_cpu_target_cpu_mask();
+	isst_fill_platform_info();
+	if (isst_platform_info.api_version > supported_api_ver) {
+		printf("Incompatible API versions; Upgrade of tool is required\n");
+		exit(0);
+	}
+
+	process_command(argc, argv);
+}
+
+int main(int argc, char **argv)
+{
+	outf = stderr;
+	cmdline(argc, argv);
+	return 0;
+}
diff --git a/tools/power/x86/intel-speed-select/isst-core.c b/tools/power/x86/intel-speed-select/isst-core.c
new file mode 100644
index 000000000000..8de4ac39a008
--- /dev/null
+++ b/tools/power/x86/intel-speed-select/isst-core.c
@@ -0,0 +1,721 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Intel Speed Select -- Enumerate and control features
+ * Copyright (c) 2019 Intel Corporation.
+ */
+
+#include "isst.h"
+
+int isst_get_ctdp_levels(int cpu, struct isst_pkg_ctdp *pkg_dev)
+{
+	unsigned int resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+				     CONFIG_TDP_GET_LEVELS_INFO, 0, 0, &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CONFIG_TDP_GET_LEVELS_INFO resp:%x\n", cpu, resp);
+
+	pkg_dev->version = resp & 0xff;
+	pkg_dev->levels = (resp >> 8) & 0xff;
+	pkg_dev->current_level = (resp >> 16) & 0xff;
+	pkg_dev->locked = !!(resp & BIT(24));
+	pkg_dev->enabled = !!(resp & BIT(31));
+
+	return 0;
+}
+
+int isst_get_ctdp_control(int cpu, int config_index,
+			  struct isst_pkg_ctdp_level_info *ctdp_level)
+{
+	unsigned int resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+				     CONFIG_TDP_GET_TDP_CONTROL, 0,
+				     config_index, &resp);
+	if (ret)
+		return ret;
+
+	ctdp_level->fact_support = resp & BIT(0);
+	ctdp_level->pbf_support = !!(resp & BIT(1));
+	ctdp_level->fact_enabled = !!(resp & BIT(16));
+	ctdp_level->pbf_enabled = !!(resp & BIT(17));
+
+	debug_printf(
+		"cpu:%d CONFIG_TDP_GET_TDP_CONTROL resp:%x fact_support:%d pbf_support: %d fact_enabled:%d pbf_enabled:%d\n",
+		cpu, resp, ctdp_level->fact_support, ctdp_level->pbf_support,
+		ctdp_level->fact_enabled, ctdp_level->pbf_enabled);
+
+	return 0;
+}
+
+int isst_get_tdp_info(int cpu, int config_index,
+		      struct isst_pkg_ctdp_level_info *ctdp_level)
+{
+	unsigned int resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP, CONFIG_TDP_GET_TDP_INFO,
+				     0, config_index, &resp);
+	if (ret)
+		return ret;
+
+	ctdp_level->pkg_tdp = resp & GENMASK(14, 0);
+	ctdp_level->tdp_ratio = (resp & GENMASK(23, 16)) >> 16;
+
+	debug_printf(
+		"cpu:%d ctdp:%d CONFIG_TDP_GET_TDP_INFO resp:%x tdp_ratio:%d pkg_tdp:%d\n",
+		cpu, config_index, resp, ctdp_level->tdp_ratio,
+		ctdp_level->pkg_tdp);
+	return 0;
+}
+
+int isst_get_pwr_info(int cpu, int config_index,
+		      struct isst_pkg_ctdp_level_info *ctdp_level)
+{
+	unsigned int resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP, CONFIG_TDP_GET_PWR_INFO,
+				     0, config_index, &resp);
+	if (ret)
+		return ret;
+
+	ctdp_level->pkg_max_power = resp & GENMASK(14, 0);
+	ctdp_level->pkg_min_power = (resp & GENMASK(30, 16)) >> 16;
+
+	debug_printf(
+		"cpu:%d ctdp:%d CONFIG_TDP_GET_PWR_INFO resp:%x pkg_max_power:%d pkg_min_power:%d\n",
+		cpu, config_index, resp, ctdp_level->pkg_max_power,
+		ctdp_level->pkg_min_power);
+
+	return 0;
+}
+
+int isst_get_tjmax_info(int cpu, int config_index,
+			struct isst_pkg_ctdp_level_info *ctdp_level)
+{
+	unsigned int resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP, CONFIG_TDP_GET_TJMAX_INFO,
+				     0, config_index, &resp);
+	if (ret)
+		return ret;
+
+	ctdp_level->t_proc_hot = resp & GENMASK(7, 0);
+
+	debug_printf(
+		"cpu:%d ctdp:%d CONFIG_TDP_GET_TJMAX_INFO resp:%x t_proc_hot:%d\n",
+		cpu, config_index, resp, ctdp_level->t_proc_hot);
+
+	return 0;
+}
+
+int isst_get_coremask_info(int cpu, int config_index,
+			   struct isst_pkg_ctdp_level_info *ctdp_level)
+{
+	unsigned int resp;
+	int i, ret;
+
+	ctdp_level->cpu_count = 0;
+	for (i = 0; i < 2; ++i) {
+		unsigned long long mask;
+		int cpu_count = 0;
+
+		ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+					     CONFIG_TDP_GET_CORE_MASK, 0,
+					     (i << 8) | config_index, &resp);
+		if (ret)
+			return ret;
+
+		debug_printf(
+			"cpu:%d ctdp:%d mask:%d CONFIG_TDP_GET_CORE_MASK resp:%x\n",
+			cpu, config_index, i, resp);
+
+		mask = (unsigned long long)resp << (32 * i);
+		set_cpu_mask_from_punit_coremask(cpu, mask,
+						 ctdp_level->core_cpumask_size,
+						 ctdp_level->core_cpumask,
+						 &cpu_count);
+		ctdp_level->cpu_count += cpu_count;
+		debug_printf("cpu:%d ctdp:%d mask:%d cpu count:%d\n", cpu,
+			     config_index, i, ctdp_level->cpu_count);
+	}
+
+	return 0;
+}
+
+int isst_get_get_trl(int cpu, int level, int avx_level, int *trl)
+{
+	unsigned int req, resp;
+	int ret;
+
+	req = level | (avx_level << 16);
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+				     CONFIG_TDP_GET_TURBO_LIMIT_RATIOS, 0, req,
+				     &resp);
+	if (ret)
+		return ret;
+
+	debug_printf(
+		"cpu:%d CONFIG_TDP_GET_TURBO_LIMIT_RATIOS req:%x resp:%x\n",
+		cpu, req, resp);
+
+	trl[0] = resp & GENMASK(7, 0);
+	trl[1] = (resp & GENMASK(15, 8)) >> 8;
+	trl[2] = (resp & GENMASK(23, 16)) >> 16;
+	trl[3] = (resp & GENMASK(31, 24)) >> 24;
+
+	req = level | BIT(8) | (avx_level << 16);
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+				     CONFIG_TDP_GET_TURBO_LIMIT_RATIOS, 0, req,
+				     &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CONFIG_TDP_GET_TURBO_LIMIT req:%x resp:%x\n", cpu,
+		     req, resp);
+
+	trl[4] = resp & GENMASK(7, 0);
+	trl[5] = (resp & GENMASK(15, 8)) >> 8;
+	trl[6] = (resp & GENMASK(23, 16)) >> 16;
+	trl[7] = (resp & GENMASK(31, 24)) >> 24;
+
+	return 0;
+}
+
+int isst_set_tdp_level_msr(int cpu, int tdp_level)
+{
+	int ret;
+
+	debug_printf("cpu: tdp_level via MSR %d\n", cpu, tdp_level);
+
+	if (isst_get_config_tdp_lock_status(cpu)) {
+		debug_printf("cpu: tdp_locked %d\n", cpu);
+		return -1;
+	}
+
+	if (tdp_level > 2)
+		return -1; /* invalid value */
+
+	ret = isst_send_msr_command(cpu, 0x64b, 1,
+				    (unsigned long long *)&tdp_level);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu: tdp_level via MSR successful %d\n", cpu, tdp_level);
+
+	return 0;
+}
+
+int isst_set_tdp_level(int cpu, int tdp_level)
+{
+	unsigned int resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP, CONFIG_TDP_SET_LEVEL, 0,
+				     tdp_level, &resp);
+	if (ret)
+		return isst_set_tdp_level_msr(cpu, tdp_level);
+
+	return 0;
+}
+
+int isst_get_pbf_info(int cpu, int level, struct isst_pbf_info *pbf_info)
+{
+	unsigned int req, resp;
+	int i, ret;
+
+	pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
+
+	for (i = 0; i < 2; ++i) {
+		unsigned long long mask;
+		int count;
+
+		ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+					     CONFIG_TDP_PBF_GET_CORE_MASK_INFO,
+					     0, (i << 8) | level, &resp);
+		if (ret)
+			return ret;
+
+		debug_printf(
+			"cpu:%d CONFIG_TDP_PBF_GET_CORE_MASK_INFO resp:%x\n",
+			cpu, resp);
+
+		mask = (unsigned long long)resp << (32 * i);
+		set_cpu_mask_from_punit_coremask(cpu, mask,
+						 pbf_info->core_cpumask_size,
+						 pbf_info->core_cpumask,
+						 &count);
+	}
+
+	req = level;
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+				     CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO, 0, req,
+				     &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO resp:%x\n", cpu,
+		     resp);
+
+	pbf_info->p1_low = resp & 0xff;
+	pbf_info->p1_high = (resp & GENMASK(15, 8)) >> 8;
+
+	req = level;
+	ret = isst_send_mbox_command(
+		cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TDP_INFO, 0, req, &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TDP_INFO resp:%x\n", cpu, resp);
+
+	pbf_info->tdp = resp & 0xffff;
+
+	req = level;
+	ret = isst_send_mbox_command(
+		cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TJ_MAX_INFO, 0, req, &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TJ_MAX_INFO resp:%x\n", cpu,
+		     resp);
+	pbf_info->t_control = (resp >> 8) & 0xff;
+	pbf_info->t_prochot = resp & 0xff;
+
+	return 0;
+}
+
+void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info)
+{
+	free_cpu_set(pbf_info->core_cpumask);
+}
+
+int isst_set_pbf_fact_status(int cpu, int pbf, int enable)
+{
+	struct isst_pkg_ctdp pkg_dev;
+	struct isst_pkg_ctdp_level_info ctdp_level;
+	int current_level;
+	unsigned int req = 0, resp;
+	int ret;
+
+	ret = isst_get_ctdp_levels(cpu, &pkg_dev);
+	if (ret)
+		return ret;
+
+	current_level = pkg_dev.current_level;
+
+	ret = isst_get_ctdp_control(cpu, current_level, &ctdp_level);
+	if (ret)
+		return ret;
+
+	if (pbf) {
+		if (ctdp_level.fact_enabled)
+			req = BIT(16);
+
+		if (enable)
+			req |= BIT(17);
+		else
+			req &= ~BIT(17);
+	} else {
+		if (ctdp_level.pbf_enabled)
+			req = BIT(17);
+
+		if (enable)
+			req |= BIT(16);
+		else
+			req &= ~BIT(16);
+	}
+
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+				     CONFIG_TDP_SET_TDP_CONTROL, 0, req, &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CONFIG_TDP_SET_TDP_CONTROL pbf/fact:%d req:%x\n",
+		     cpu, pbf, req);
+
+	return 0;
+}
+
+int isst_get_fact_bucket_info(int cpu, int level,
+			      struct isst_fact_bucket_info *bucket_info)
+{
+	unsigned int resp;
+	int i, k, ret;
+
+	for (i = 0; i < 2; ++i) {
+		int j;
+
+		ret = isst_send_mbox_command(
+			cpu, CONFIG_TDP,
+			CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES, 0,
+			(i << 8) | level, &resp);
+		if (ret)
+			return ret;
+
+		debug_printf(
+			"cpu:%d CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES index:%d level:%d resp:%x\n",
+			cpu, i, level, resp);
+
+		for (j = 0; j < 4; ++j) {
+			bucket_info[j + (i * 4)].high_priority_cores_count =
+				(resp >> (j * 8)) & 0xff;
+		}
+	}
+
+	for (k = 0; k < 3; ++k) {
+		for (i = 0; i < 2; ++i) {
+			int j;
+
+			ret = isst_send_mbox_command(
+				cpu, CONFIG_TDP,
+				CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS, 0,
+				(k << 16) | (i << 8) | level, &resp);
+			if (ret)
+				return ret;
+
+			debug_printf(
+				"cpu:%d CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS index:%d level:%d avx:%d resp:%x\n",
+				cpu, i, level, k, resp);
+
+			for (j = 0; j < 4; ++j) {
+				switch (k) {
+				case 0:
+					bucket_info[j + (i * 4)].sse_trl =
+						(resp >> (j * 8)) & 0xff;
+					break;
+				case 1:
+					bucket_info[j + (i * 4)].avx_trl =
+						(resp >> (j * 8)) & 0xff;
+					break;
+				case 2:
+					bucket_info[j + (i * 4)].avx512_trl =
+						(resp >> (j * 8)) & 0xff;
+					break;
+				default:
+					break;
+				}
+			}
+		}
+	}
+
+	return 0;
+}
+
+int isst_get_fact_info(int cpu, int level, struct isst_fact_info *fact_info)
+{
+	unsigned int resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
+				     CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO, 0,
+				     level, &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO resp:%x\n",
+		     cpu, resp);
+
+	fact_info->lp_clipping_ratio_license_sse = resp & 0xff;
+	fact_info->lp_clipping_ratio_license_avx2 = (resp >> 8) & 0xff;
+	fact_info->lp_clipping_ratio_license_avx512 = (resp >> 16) & 0xff;
+
+	ret = isst_get_fact_bucket_info(cpu, level, fact_info->bucket_info);
+
+	return ret;
+}
+
+int isst_set_trl(int cpu, unsigned long long trl)
+{
+	int ret;
+
+	if (!trl)
+		trl = 0xFFFFFFFFFFFFFFFFULL;
+
+	ret = isst_send_msr_command(cpu, 0x1AD, 1, &trl);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+int isst_set_trl_from_current_tdp(int cpu, unsigned long long trl)
+{
+	unsigned long long msr_trl;
+	int ret;
+
+	if (trl) {
+		msr_trl = trl;
+	} else {
+		struct isst_pkg_ctdp pkg_dev;
+		int trl[8];
+		int i;
+
+		ret = isst_get_ctdp_levels(cpu, &pkg_dev);
+		if (ret)
+			return ret;
+
+		ret = isst_get_get_trl(cpu, pkg_dev.current_level, 0, trl);
+		if (ret)
+			return ret;
+
+		msr_trl = 0;
+		for (i = 0; i < 8; ++i) {
+			unsigned long long _trl = trl[i];
+
+			msr_trl |= (_trl << (i * 8));
+		}
+	}
+	ret = isst_send_msr_command(cpu, 0x1AD, 1, &msr_trl);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+/* Return 1 if locked */
+int isst_get_config_tdp_lock_status(int cpu)
+{
+	unsigned long long tdp_control = 0;
+	int ret;
+
+	ret = isst_send_msr_command(cpu, 0x64b, 0, &tdp_control);
+	if (ret)
+		return ret;
+
+	ret = !!(tdp_control & BIT(31));
+
+	return ret;
+}
+
+void isst_get_process_ctdp_complete(int cpu, struct isst_pkg_ctdp *pkg_dev)
+{
+	int i;
+
+	if (!pkg_dev->processed)
+		return;
+
+	for (i = 0; i < pkg_dev->levels; ++i) {
+		struct isst_pkg_ctdp_level_info *ctdp_level;
+
+		ctdp_level = &pkg_dev->ctdp_level[i];
+		if (ctdp_level->pbf_support)
+			free_cpu_set(ctdp_level->pbf_info.core_cpumask);
+		free_cpu_set(ctdp_level->core_cpumask);
+	}
+}
+
+int isst_get_process_ctdp(int cpu, int tdp_level, struct isst_pkg_ctdp *pkg_dev)
+{
+	int i, ret;
+
+	if (pkg_dev->processed)
+		return 0;
+
+	ret = isst_get_ctdp_levels(cpu, pkg_dev);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu: %d ctdp enable:%d current level: %d levels:%d\n",
+		     cpu, pkg_dev->enabled, pkg_dev->current_level,
+		     pkg_dev->levels);
+
+	for (i = 0; i <= pkg_dev->levels; ++i) {
+		struct isst_pkg_ctdp_level_info *ctdp_level;
+
+		if (tdp_level != 0xff && i != tdp_level)
+			continue;
+
+		debug_printf("cpu:%d Get Information for TDP level:%d\n", cpu,
+			     i);
+		ctdp_level = &pkg_dev->ctdp_level[i];
+
+		ctdp_level->processed = 1;
+		ctdp_level->level = i;
+		ctdp_level->control_cpu = cpu;
+		ctdp_level->pkg_id = get_physical_package_id(cpu);
+		ctdp_level->die_id = get_physical_die_id(cpu);
+
+		ret = isst_get_ctdp_control(cpu, i, ctdp_level);
+		if (ret)
+			return ret;
+
+		ret = isst_get_tdp_info(cpu, i, ctdp_level);
+		if (ret)
+			return ret;
+
+		ret = isst_get_pwr_info(cpu, i, ctdp_level);
+		if (ret)
+			return ret;
+
+		ret = isst_get_tjmax_info(cpu, i, ctdp_level);
+		if (ret)
+			return ret;
+
+		ctdp_level->core_cpumask_size =
+			alloc_cpu_set(&ctdp_level->core_cpumask);
+		ret = isst_get_coremask_info(cpu, i, ctdp_level);
+		if (ret)
+			return ret;
+
+		ret = isst_get_get_trl(cpu, i, 0,
+				       ctdp_level->trl_sse_active_cores);
+		if (ret)
+			return ret;
+
+		ret = isst_get_get_trl(cpu, i, 1,
+				       ctdp_level->trl_avx_active_cores);
+		if (ret)
+			return ret;
+
+		ret = isst_get_get_trl(cpu, i, 2,
+				       ctdp_level->trl_avx_512_active_cores);
+		if (ret)
+			return ret;
+
+		if (ctdp_level->pbf_support) {
+			ret = isst_get_pbf_info(cpu, i, &ctdp_level->pbf_info);
+			if (!ret)
+				ctdp_level->pbf_found = 1;
+		}
+
+		if (ctdp_level->fact_support) {
+			ret = isst_get_fact_info(cpu, i,
+						 &ctdp_level->fact_info);
+			if (ret)
+				return ret;
+		}
+	}
+
+	pkg_dev->processed = 1;
+
+	return 0;
+}
+
+int isst_pm_qos_config(int cpu, int enable_clos, int priority_type)
+{
+	unsigned int req, resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0,
+				     &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", cpu, resp);
+
+	req = resp;
+
+	if (enable_clos)
+		req = req | BIT(1);
+	else
+		req = req & ~BIT(1);
+
+	if (priority_type)
+		req = req | BIT(2);
+	else
+		req = req & ~BIT(2);
+
+	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG,
+				     BIT(MBOX_CMD_WRITE_BIT), req, &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CLOS_PM_QOS_CONFIG priority type:%d req:%x\n", cpu,
+		     priority_type, req);
+
+	return 0;
+}
+
+int isst_pm_get_clos(int cpu, int clos, struct isst_clos_config *clos_config)
+{
+	unsigned int resp;
+	int ret;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PM_CLOS, clos, 0,
+				     &resp);
+	if (ret)
+		return ret;
+
+	clos_config->pkg_id = get_physical_package_id(cpu);
+	clos_config->die_id = get_physical_die_id(cpu);
+
+	clos_config->epp = resp & 0x0f;
+	clos_config->clos_prop_prio = (resp >> 4) & 0x0f;
+	clos_config->clos_min = (resp >> 8) & 0xff;
+	clos_config->clos_max = (resp >> 16) & 0xff;
+	clos_config->clos_desired = (resp >> 24) & 0xff;
+
+	return 0;
+}
+
+int isst_set_clos(int cpu, int clos, struct isst_clos_config *clos_config)
+{
+	unsigned int req, resp;
+	unsigned int param;
+	int ret;
+
+	req = clos_config->epp & 0x0f;
+	req |= (clos_config->clos_prop_prio & 0x0f) << 4;
+	req |= (clos_config->clos_min & 0xff) << 8;
+	req |= (clos_config->clos_max & 0xff) << 16;
+	req |= (clos_config->clos_desired & 0xff) << 24;
+
+	param = BIT(MBOX_CMD_WRITE_BIT) | clos;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PM_CLOS, param, req,
+				     &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CLOS_PM_CLOS param:%x req:%x\n", cpu, param, req);
+
+	return 0;
+}
+
+int isst_clos_get_assoc_status(int cpu, int *clos_id)
+{
+	unsigned int resp;
+	unsigned int param;
+	int core_id, ret;
+
+	core_id = find_phy_core_num(cpu);
+	param = core_id;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param, 0,
+				     &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x resp:%x\n", cpu, param,
+		     resp);
+	*clos_id = (resp >> 16) & 0x03;
+
+	return 0;
+}
+
+int isst_clos_associate(int cpu, int clos_id)
+{
+	unsigned int req, resp;
+	unsigned int param;
+	int core_id, ret;
+
+	req = (clos_id & 0x03) << 16;
+	core_id = find_phy_core_num(cpu);
+	param = BIT(MBOX_CMD_WRITE_BIT) | core_id;
+
+	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param,
+				     req, &resp);
+	if (ret)
+		return ret;
+
+	debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x req:%x\n", cpu, param,
+		     req);
+
+	return 0;
+}
diff --git a/tools/power/x86/intel-speed-select/isst-display.c b/tools/power/x86/intel-speed-select/isst-display.c
new file mode 100644
index 000000000000..f368b8323742
--- /dev/null
+++ b/tools/power/x86/intel-speed-select/isst-display.c
@@ -0,0 +1,479 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Intel dynamic_speed_select -- Enumerate and control features
+ * Copyright (c) 2019 Intel Corporation.
+ */
+
+#include "isst.h"
+
+#define DISP_FREQ_MULTIPLIER 100000
+
+static void printcpumask(int str_len, char *str, int mask_size,
+			 cpu_set_t *cpu_mask)
+{
+	int i, max_cpus = get_topo_max_cpus();
+	unsigned int *mask;
+	int size, index, curr_index;
+
+	size = max_cpus / (sizeof(unsigned int) * 8);
+	if (max_cpus % (sizeof(unsigned int) * 8))
+		size++;
+
+	mask = calloc(size, sizeof(unsigned int));
+	if (!mask)
+		return;
+
+	for (i = 0; i < max_cpus; ++i) {
+		int mask_index, bit_index;
+
+		if (!CPU_ISSET_S(i, mask_size, cpu_mask))
+			continue;
+
+		mask_index = i / (sizeof(unsigned int) * 8);
+		bit_index = i % (sizeof(unsigned int) * 8);
+		mask[mask_index] |= BIT(bit_index);
+	}
+
+	curr_index = 0;
+	for (i = size - 1; i >= 0; --i) {
+		index = snprintf(&str[curr_index], str_len - curr_index, "%08x",
+				 mask[i]);
+		curr_index += index;
+		if (i) {
+			strncat(&str[curr_index], ",", str_len - curr_index);
+			curr_index++;
+		}
+	}
+
+	free(mask);
+}
+
+static void format_and_print_txt(FILE *outf, int level, char *header,
+				 char *value)
+{
+	char *spaces = "  ";
+	static char delimiters[256];
+	int i, j = 0;
+
+	if (!level)
+		return;
+
+	if (level == 1) {
+		strcpy(delimiters, " ");
+	} else {
+		for (i = 0; i < level - 1; ++i)
+			j += snprintf(&delimiters[j], sizeof(delimiters) - j,
+				      "%s", spaces);
+	}
+
+	if (header && value) {
+		fprintf(outf, "%s", delimiters);
+		fprintf(outf, "%s:%s\n", header, value);
+	} else if (header) {
+		fprintf(outf, "%s", delimiters);
+		fprintf(outf, "%s\n", header);
+	}
+}
+
+static int last_level;
+static void format_and_print(FILE *outf, int level, char *header, char *value)
+{
+	char *spaces = "  ";
+	static char delimiters[256];
+	int i;
+
+	if (!out_format_is_json()) {
+		format_and_print_txt(outf, level, header, value);
+		return;
+	}
+
+	if (level == 0) {
+		if (header)
+			fprintf(outf, "{");
+		else
+			fprintf(outf, "\n}\n");
+
+	} else {
+		int j = 0;
+
+		for (i = 0; i < level; ++i)
+			j += snprintf(&delimiters[j], sizeof(delimiters) - j,
+				      "%s", spaces);
+
+		if (last_level == level)
+			fprintf(outf, ",\n");
+
+		if (value) {
+			if (last_level != level)
+				fprintf(outf, "\n");
+
+			fprintf(outf, "%s\"%s\": ", delimiters, header);
+			fprintf(outf, "\"%s\"", value);
+		} else {
+			for (i = last_level - 1; i >= level; --i) {
+				int k = 0;
+
+				for (j = i; j > 0; --j)
+					k += snprintf(&delimiters[k],
+						      sizeof(delimiters) - k,
+						      "%s", spaces);
+				if (i == level && header)
+					fprintf(outf, "\n%s},", delimiters);
+				else
+					fprintf(outf, "\n%s}", delimiters);
+			}
+			if (abs(last_level - level) < 3)
+				fprintf(outf, "\n");
+			if (header)
+				fprintf(outf, "%s\"%s\": {", delimiters,
+					header);
+		}
+	}
+
+	last_level = level;
+}
+
+static void print_packag_info(int cpu, FILE *outf)
+{
+	char header[256];
+
+	snprintf(header, sizeof(header), "package-%d",
+		 get_physical_package_id(cpu));
+	format_and_print(outf, 1, header, NULL);
+	snprintf(header, sizeof(header), "die-%d", get_physical_die_id(cpu));
+	format_and_print(outf, 2, header, NULL);
+	snprintf(header, sizeof(header), "cpu-%d", cpu);
+	format_and_print(outf, 3, header, NULL);
+}
+
+static void _isst_pbf_display_information(int cpu, FILE *outf, int level,
+					  struct isst_pbf_info *pbf_info,
+					  int disp_level)
+{
+	char header[256];
+	char value[256];
+
+	snprintf(header, sizeof(header), "speed-select-base-freq");
+	format_and_print(outf, disp_level, header, NULL);
+
+	snprintf(header, sizeof(header), "high-priority-base-frequency(KHz)");
+	snprintf(value, sizeof(value), "%d",
+		 pbf_info->p1_high * DISP_FREQ_MULTIPLIER);
+	format_and_print(outf, disp_level + 1, header, value);
+
+	snprintf(header, sizeof(header), "high-priority-cpu-mask");
+	printcpumask(sizeof(value), value, pbf_info->core_cpumask_size,
+		     pbf_info->core_cpumask);
+	format_and_print(outf, disp_level + 1, header, value);
+
+	snprintf(header, sizeof(header), "low-priority-base-frequency(KHz)");
+	snprintf(value, sizeof(value), "%d",
+		 pbf_info->p1_low * DISP_FREQ_MULTIPLIER);
+	format_and_print(outf, disp_level + 1, header, value);
+
+	snprintf(header, sizeof(header), "tjunction-temperature(C)");
+	snprintf(value, sizeof(value), "%d", pbf_info->t_prochot);
+	format_and_print(outf, disp_level + 1, header, value);
+
+	snprintf(header, sizeof(header), "thermal-design-power(W)");
+	snprintf(value, sizeof(value), "%d", pbf_info->tdp);
+	format_and_print(outf, disp_level + 1, header, value);
+}
+
+static void _isst_fact_display_information(int cpu, FILE *outf, int level,
+					   int fact_bucket, int fact_avx,
+					   struct isst_fact_info *fact_info,
+					   int base_level)
+{
+	struct isst_fact_bucket_info *bucket_info = fact_info->bucket_info;
+	char header[256];
+	char value[256];
+	int j;
+
+	snprintf(header, sizeof(header), "speed-select-turbo-freq");
+	format_and_print(outf, base_level, header, NULL);
+	for (j = 0; j < ISST_FACT_MAX_BUCKETS; ++j) {
+		if (fact_bucket != 0xff && fact_bucket != j)
+			continue;
+
+		if (!bucket_info[j].high_priority_cores_count)
+			break;
+
+		snprintf(header, sizeof(header), "bucket-%d", j);
+		format_and_print(outf, base_level + 1, header, NULL);
+
+		snprintf(header, sizeof(header), "high-priority-cores-count");
+		snprintf(value, sizeof(value), "%d",
+			 bucket_info[j].high_priority_cores_count);
+		format_and_print(outf, base_level + 2, header, value);
+
+		if (fact_avx & 0x01) {
+			snprintf(header, sizeof(header),
+				 "high-priority-max-frequency(KHz)");
+			snprintf(value, sizeof(value), "%d",
+				 bucket_info[j].sse_trl * DISP_FREQ_MULTIPLIER);
+			format_and_print(outf, base_level + 2, header, value);
+		}
+
+		if (fact_avx & 0x02) {
+			snprintf(header, sizeof(header),
+				 "high-priority-max-avx2-frequency(KHz)");
+			snprintf(value, sizeof(value), "%d",
+				 bucket_info[j].avx_trl * DISP_FREQ_MULTIPLIER);
+			format_and_print(outf, base_level + 2, header, value);
+		}
+
+		if (fact_avx & 0x04) {
+			snprintf(header, sizeof(header),
+				 "high-priority-max-avx512-frequency(KHz)");
+			snprintf(value, sizeof(value), "%d",
+				 bucket_info[j].avx512_trl *
+					 DISP_FREQ_MULTIPLIER);
+			format_and_print(outf, base_level + 2, header, value);
+		}
+	}
+	snprintf(header, sizeof(header),
+		 "speed-select-turbo-freq-clip-frequencies");
+	format_and_print(outf, base_level + 1, header, NULL);
+	snprintf(header, sizeof(header), "low-priority-max-frequency(KHz)");
+	snprintf(value, sizeof(value), "%d",
+		 fact_info->lp_clipping_ratio_license_sse *
+			 DISP_FREQ_MULTIPLIER);
+	format_and_print(outf, base_level + 2, header, value);
+	snprintf(header, sizeof(header),
+		 "low-priority-max-avx2-frequency(KHz)");
+	snprintf(value, sizeof(value), "%d",
+		 fact_info->lp_clipping_ratio_license_avx2 *
+			 DISP_FREQ_MULTIPLIER);
+	format_and_print(outf, base_level + 2, header, value);
+	snprintf(header, sizeof(header),
+		 "low-priority-max-avx512-frequency(KHz)");
+	snprintf(value, sizeof(value), "%d",
+		 fact_info->lp_clipping_ratio_license_avx512 *
+			 DISP_FREQ_MULTIPLIER);
+	format_and_print(outf, base_level + 2, header, value);
+}
+
+void isst_ctdp_display_information(int cpu, FILE *outf, int tdp_level,
+				   struct isst_pkg_ctdp *pkg_dev)
+{
+	char header[256];
+	char value[256];
+	int i, base_level = 1;
+
+	print_packag_info(cpu, outf);
+
+	for (i = 0; i <= pkg_dev->levels; ++i) {
+		struct isst_pkg_ctdp_level_info *ctdp_level;
+		int j;
+
+		ctdp_level = &pkg_dev->ctdp_level[i];
+		if (!ctdp_level->processed)
+			continue;
+
+		snprintf(header, sizeof(header), "perf-profile-level-%d",
+			 ctdp_level->level);
+		format_and_print(outf, base_level + 3, header, NULL);
+
+		snprintf(header, sizeof(header), "cpu-count");
+		j = get_cpu_count(get_physical_die_id(cpu),
+				  get_physical_die_id(cpu));
+		snprintf(value, sizeof(value), "%d", j);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header), "enable-cpu-mask");
+		printcpumask(sizeof(value), value,
+			     ctdp_level->core_cpumask_size,
+			     ctdp_level->core_cpumask);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header), "thermal-design-power-ratio");
+		snprintf(value, sizeof(value), "%d", ctdp_level->tdp_ratio);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header), "base-frequency(KHz)");
+		snprintf(value, sizeof(value), "%d",
+			 ctdp_level->tdp_ratio * DISP_FREQ_MULTIPLIER);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header),
+			 "speed-select-turbo-freq-support");
+		snprintf(value, sizeof(value), "%d", ctdp_level->fact_support);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header),
+			 "speed-select-base-freq-support");
+		snprintf(value, sizeof(value), "%d", ctdp_level->pbf_support);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header),
+			 "speed-select-base-freq-enabled");
+		snprintf(value, sizeof(value), "%d", ctdp_level->pbf_enabled);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header),
+			 "speed-select-turbo-freq-enabled");
+		snprintf(value, sizeof(value), "%d", ctdp_level->fact_enabled);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header), "thermal-design-power(W)");
+		snprintf(value, sizeof(value), "%d", ctdp_level->pkg_tdp);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header), "tjunction-max(C)");
+		snprintf(value, sizeof(value), "%d", ctdp_level->t_proc_hot);
+		format_and_print(outf, base_level + 4, header, value);
+
+		snprintf(header, sizeof(header), "turbo-ratio-limits-sse");
+		format_and_print(outf, base_level + 4, header, NULL);
+		for (j = 0; j < 8; ++j) {
+			snprintf(header, sizeof(header), "bucket-%d", j);
+			format_and_print(outf, base_level + 5, header, NULL);
+
+			snprintf(header, sizeof(header), "core-count");
+			snprintf(value, sizeof(value), "%d", j);
+			format_and_print(outf, base_level + 6, header, value);
+
+			snprintf(header, sizeof(header), "turbo-ratio");
+			snprintf(value, sizeof(value), "%d",
+				 ctdp_level->trl_sse_active_cores[j]);
+			format_and_print(outf, base_level + 6, header, value);
+		}
+		snprintf(header, sizeof(header), "turbo-ratio-limits-avx");
+		format_and_print(outf, base_level + 4, header, NULL);
+		for (j = 0; j < 8; ++j) {
+			snprintf(header, sizeof(header), "bucket-%d", j);
+			format_and_print(outf, base_level + 5, header, NULL);
+
+			snprintf(header, sizeof(header), "core-count");
+			snprintf(value, sizeof(value), "%d", j);
+			format_and_print(outf, base_level + 6, header, value);
+
+			snprintf(header, sizeof(header), "turbo-ratio");
+			snprintf(value, sizeof(value), "%d",
+				 ctdp_level->trl_avx_active_cores[j]);
+			format_and_print(outf, base_level + 6, header, value);
+		}
+
+		snprintf(header, sizeof(header), "turbo-ratio-limits-avx512");
+		format_and_print(outf, base_level + 4, header, NULL);
+		for (j = 0; j < 8; ++j) {
+			snprintf(header, sizeof(header), "bucket-%d", j);
+			format_and_print(outf, base_level + 5, header, NULL);
+
+			snprintf(header, sizeof(header), "core-count");
+			snprintf(value, sizeof(value), "%d", j);
+			format_and_print(outf, base_level + 6, header, value);
+
+			snprintf(header, sizeof(header), "turbo-ratio");
+			snprintf(value, sizeof(value), "%d",
+				 ctdp_level->trl_avx_512_active_cores[j]);
+			format_and_print(outf, base_level + 6, header, value);
+		}
+		if (ctdp_level->pbf_support)
+			_isst_pbf_display_information(cpu, outf, i,
+						      &ctdp_level->pbf_info,
+						      base_level + 4);
+		if (ctdp_level->fact_support)
+			_isst_fact_display_information(cpu, outf, i, 0xff, 0xff,
+						       &ctdp_level->fact_info,
+						       base_level + 4);
+	}
+
+	format_and_print(outf, 1, NULL, NULL);
+}
+
+void isst_ctdp_display_information_start(FILE *outf)
+{
+	last_level = 0;
+	format_and_print(outf, 0, "start", NULL);
+}
+
+void isst_ctdp_display_information_end(FILE *outf)
+{
+	format_and_print(outf, 0, NULL, NULL);
+}
+
+void isst_pbf_display_information(int cpu, FILE *outf, int level,
+				  struct isst_pbf_info *pbf_info)
+{
+	print_packag_info(cpu, outf);
+	_isst_pbf_display_information(cpu, outf, level, pbf_info, 4);
+	format_and_print(outf, 1, NULL, NULL);
+}
+
+void isst_fact_display_information(int cpu, FILE *outf, int level,
+				   int fact_bucket, int fact_avx,
+				   struct isst_fact_info *fact_info)
+{
+	print_packag_info(cpu, outf);
+	_isst_fact_display_information(cpu, outf, level, fact_bucket, fact_avx,
+				       fact_info, 4);
+	format_and_print(outf, 1, NULL, NULL);
+}
+
+void isst_clos_display_information(int cpu, FILE *outf, int clos,
+				   struct isst_clos_config *clos_config)
+{
+	char header[256];
+	char value[256];
+
+	snprintf(header, sizeof(header), "package-%d",
+		 get_physical_package_id(cpu));
+	format_and_print(outf, 1, header, NULL);
+	snprintf(header, sizeof(header), "die-%d", get_physical_die_id(cpu));
+	format_and_print(outf, 2, header, NULL);
+	snprintf(header, sizeof(header), "cpu-%d", cpu);
+	format_and_print(outf, 3, header, NULL);
+
+	snprintf(header, sizeof(header), "core-power");
+	format_and_print(outf, 4, header, NULL);
+
+	snprintf(header, sizeof(header), "clos");
+	snprintf(value, sizeof(value), "%d", clos);
+	format_and_print(outf, 5, header, value);
+
+	snprintf(header, sizeof(header), "epp");
+	snprintf(value, sizeof(value), "%d", clos_config->epp);
+	format_and_print(outf, 5, header, value);
+
+	snprintf(header, sizeof(header), "clos-proportional-priority");
+	snprintf(value, sizeof(value), "%d", clos_config->clos_prop_prio);
+	format_and_print(outf, 5, header, value);
+
+	snprintf(header, sizeof(header), "clos-min");
+	snprintf(value, sizeof(value), "%d", clos_config->clos_min);
+	format_and_print(outf, 5, header, value);
+
+	snprintf(header, sizeof(header), "clos-max");
+	snprintf(value, sizeof(value), "%d", clos_config->clos_max);
+	format_and_print(outf, 5, header, value);
+
+	snprintf(header, sizeof(header), "clos-desired");
+	snprintf(value, sizeof(value), "%d", clos_config->clos_desired);
+	format_and_print(outf, 5, header, value);
+
+	format_and_print(outf, 1, NULL, NULL);
+}
+
+void isst_display_result(int cpu, FILE *outf, char *feature, char *cmd,
+			 int result)
+{
+	char header[256];
+	char value[256];
+
+	snprintf(header, sizeof(header), "package-%d",
+		 get_physical_package_id(cpu));
+	format_and_print(outf, 1, header, NULL);
+	snprintf(header, sizeof(header), "die-%d", get_physical_die_id(cpu));
+	format_and_print(outf, 2, header, NULL);
+	snprintf(header, sizeof(header), "cpu-%d", cpu);
+	format_and_print(outf, 3, header, NULL);
+	snprintf(header, sizeof(header), "%s", feature);
+	format_and_print(outf, 4, header, NULL);
+	snprintf(header, sizeof(header), "%s", cmd);
+	snprintf(value, sizeof(value), "%d", result);
+	format_and_print(outf, 5, header, value);
+
+	format_and_print(outf, 1, NULL, NULL);
+}
diff --git a/tools/power/x86/intel-speed-select/isst.h b/tools/power/x86/intel-speed-select/isst.h
new file mode 100644
index 000000000000..221881761609
--- /dev/null
+++ b/tools/power/x86/intel-speed-select/isst.h
@@ -0,0 +1,231 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Intel Speed Select -- Enumerate and control features
+ * Copyright (c) 2019 Intel Corporation.
+ */
+
+#ifndef _ISST_H_
+#define _ISST_H_
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <sys/stat.h>
+#include <sys/resource.h>
+#include <getopt.h>
+#include <err.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/time.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cpuid.h>
+#include <dirent.h>
+#include <errno.h>
+
+#include <stdarg.h>
+#include <sys/ioctl.h>
+
+#define BIT(x) (1 << (x))
+#define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (sizeof(long) * 8 - 1 - (h))))
+#define GENMASK_ULL(h, l)                                                      \
+	(((~0ULL) << (l)) & (~0ULL >> (sizeof(long long) * 8 - 1 - (h))))
+
+#define CONFIG_TDP				0x7f
+#define CONFIG_TDP_GET_LEVELS_INFO		0x00
+#define CONFIG_TDP_GET_TDP_CONTROL		0x01
+#define CONFIG_TDP_SET_TDP_CONTROL		0x02
+#define CONFIG_TDP_GET_TDP_INFO			0x03
+#define CONFIG_TDP_GET_PWR_INFO			0x04
+#define CONFIG_TDP_GET_TJMAX_INFO		0x05
+#define CONFIG_TDP_GET_CORE_MASK		0x06
+#define CONFIG_TDP_GET_TURBO_LIMIT_RATIOS	0x07
+#define CONFIG_TDP_SET_LEVEL			0x08
+#define CONFIG_TDP_GET_UNCORE_P0_P1_INFO	0X09
+#define CONFIG_TDP_GET_P1_INFO			0x0a
+#define CONFIG_TDP_GET_MEM_FREQ			0x0b
+
+#define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES	0x10
+#define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS	0x11
+#define CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO		0x12
+
+#define CONFIG_TDP_PBF_GET_CORE_MASK_INFO	0x20
+#define CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO	0x21
+#define CONFIG_TDP_PBF_GET_TJ_MAX_INFO		0x22
+#define CONFIG_TDP_PBF_GET_TDP_INFO		0X23
+
+#define CONFIG_CLOS				0xd0
+#define CLOS_PQR_ASSOC				0x00
+#define CLOS_PM_CLOS				0x01
+#define CLOS_PM_QOS_CONFIG			0x02
+#define CLOS_STATUS				0x03
+
+#define MBOX_CMD_WRITE_BIT			0x08
+
+#define PM_QOS_INFO_OFFSET			0x00
+#define PM_QOS_CONFIG_OFFSET			0x04
+#define PM_CLOS_OFFSET				0x08
+#define PQR_ASSOC_OFFSET			0x20
+
+struct isst_clos_config {
+	int pkg_id;
+	int die_id;
+	unsigned char epp;
+	unsigned char clos_prop_prio;
+	unsigned char clos_min;
+	unsigned char clos_max;
+	unsigned char clos_desired;
+};
+
+struct isst_fact_bucket_info {
+	int high_priority_cores_count;
+	int sse_trl;
+	int avx_trl;
+	int avx512_trl;
+};
+
+struct isst_pbf_info {
+	int pbf_acticated;
+	int pbf_available;
+	size_t core_cpumask_size;
+	cpu_set_t *core_cpumask;
+	int p1_high;
+	int p1_low;
+	int t_control;
+	int t_prochot;
+	int tdp;
+};
+
+#define ISST_TRL_MAX_ACTIVE_CORES	8
+#define ISST_FACT_MAX_BUCKETS		8
+struct isst_fact_info {
+	int lp_clipping_ratio_license_sse;
+	int lp_clipping_ratio_license_avx2;
+	int lp_clipping_ratio_license_avx512;
+	struct isst_fact_bucket_info bucket_info[ISST_FACT_MAX_BUCKETS];
+};
+
+struct isst_pkg_ctdp_level_info {
+	int processed;
+	int control_cpu;
+	int pkg_id;
+	int die_id;
+	int level;
+	int fact_support;
+	int pbf_support;
+	int fact_enabled;
+	int pbf_enabled;
+	int tdp_ratio;
+	int active;
+	int tdp_control;
+	int pkg_tdp;
+	int pkg_min_power;
+	int pkg_max_power;
+	int fact;
+	int t_proc_hot;
+	int uncore_p0;
+	int uncore_p1;
+	int sse_p1;
+	int avx2_p1;
+	int avx512_p1;
+	int mem_freq;
+	size_t core_cpumask_size;
+	cpu_set_t *core_cpumask;
+	int cpu_count;
+	int trl_sse_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
+	int trl_avx_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
+	int trl_avx_512_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
+	int kobj_bucket_index;
+	int active_bucket;
+	int fact_max_index;
+	int fact_max_config;
+	int pbf_found;
+	int pbf_active;
+	struct isst_pbf_info pbf_info;
+	struct isst_fact_info fact_info;
+};
+
+#define ISST_MAX_TDP_LEVELS	(4 + 1) /* +1 for base config */
+struct isst_pkg_ctdp {
+	int locked;
+	int version;
+	int processed;
+	int levels;
+	int current_level;
+	int enabled;
+	struct isst_pkg_ctdp_level_info ctdp_level[ISST_MAX_TDP_LEVELS];
+};
+
+extern int get_topo_max_cpus(void);
+extern int get_cpu_count(int pkg_id, int die_id);
+
+/* Common interfaces */
+extern void debug_printf(const char *format, ...);
+extern int out_format_is_json(void);
+extern int get_physical_package_id(int cpu);
+extern int get_physical_die_id(int cpu);
+extern size_t alloc_cpu_set(cpu_set_t **cpu_set);
+extern void free_cpu_set(cpu_set_t *cpu_set);
+extern int find_logical_cpu(int pkg_id, int die_id, int phy_cpu);
+extern int find_phy_cpu_num(int logical_cpu);
+extern int find_phy_core_num(int logical_cpu);
+extern void set_cpu_mask_from_punit_coremask(int cpu,
+					     unsigned long long core_mask,
+					     size_t core_cpumask_size,
+					     cpu_set_t *core_cpumask,
+					     int *cpu_cnt);
+
+extern int isst_send_mbox_command(unsigned int cpu, unsigned char command,
+				  unsigned char sub_command,
+				  unsigned int write,
+				  unsigned int req_data, unsigned int *resp);
+
+extern int isst_send_msr_command(unsigned int cpu, unsigned int command,
+				 int write, unsigned long long *req_resp);
+
+extern int isst_get_ctdp_levels(int cpu, struct isst_pkg_ctdp *pkg_dev);
+extern int isst_get_process_ctdp(int cpu, int tdp_level,
+				 struct isst_pkg_ctdp *pkg_dev);
+extern void isst_get_process_ctdp_complete(int cpu,
+					   struct isst_pkg_ctdp *pkg_dev);
+extern void isst_ctdp_display_information(int cpu, FILE *outf, int tdp_level,
+					  struct isst_pkg_ctdp *pkg_dev);
+extern void isst_ctdp_display_information_start(FILE *outf);
+extern void isst_ctdp_display_information_end(FILE *outf);
+extern void isst_pbf_display_information(int cpu, FILE *outf, int level,
+					 struct isst_pbf_info *info);
+extern int isst_set_tdp_level(int cpu, int tdp_level);
+extern int isst_set_tdp_level_msr(int cpu, int tdp_level);
+extern int isst_set_pbf_fact_status(int cpu, int pbf, int enable);
+extern int isst_get_pbf_info(int cpu, int level,
+			     struct isst_pbf_info *pbf_info);
+extern void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info);
+extern int isst_get_fact_info(int cpu, int level,
+			      struct isst_fact_info *fact_info);
+extern int isst_get_fact_bucket_info(int cpu, int level,
+				     struct isst_fact_bucket_info *bucket_info);
+extern void isst_fact_display_information(int cpu, FILE *outf, int level,
+					  int fact_bucket, int fact_avx,
+					  struct isst_fact_info *fact_info);
+extern int isst_set_trl(int cpu, unsigned long long trl);
+extern int isst_set_trl_from_current_tdp(int cpu, unsigned long long trl);
+extern int isst_get_config_tdp_lock_status(int cpu);
+
+extern int isst_pm_qos_config(int cpu, int enable_clos, int priority_type);
+extern int isst_pm_get_clos(int cpu, int clos,
+			    struct isst_clos_config *clos_config);
+extern int isst_set_clos(int cpu, int clos,
+			 struct isst_clos_config *clos_config);
+extern int isst_clos_associate(int cpu, int clos);
+extern int isst_clos_get_assoc_status(int cpu, int *clos_id);
+extern void isst_clos_display_information(int cpu, FILE *outf, int clos,
+					  struct isst_clos_config *clos_config);
+
+extern int isst_read_reg(unsigned short reg, unsigned int *val);
+extern int isst_write_reg(int reg, unsigned int val);
+
+extern void isst_display_result(int cpu, FILE *outf, char *feature, char *cmd,
+				int result);
+#endif
-- 
2.17.2


^ permalink raw reply related

* Re: [UPDATE][PATCH] tools/power/x86: A tool to validate Intel Speed Select commands
From: Srinivas Pandruvada @ 2019-06-30 17:12 UTC (permalink / raw)
  To: dvhart, andy, andriy.shevchenko, corbet
  Cc: rjw, alan, lenb, prarit, darcari, linux-doc, linux-kernel,
	platform-driver-x86
In-Reply-To: <20190630171054.8353-1-srinivas.pandruvada@linux.intel.com>

Please ignore. I will send again.

On Sun, 2019-06-30 at 10:10 -0700, Srinivas Pandruvada wrote:
> The Intel(R) Speed select technologies contains four features.
> 
> Performance profile:An non architectural mechanism that allows
> multiple
> optimized performance profiles per system via static and/or dynamic
> adjustment of core count, workload, Tjmax, and TDP, etc. aka ISS
> in the documentation.
> 
> Base Frequency: Enables users to increase guaranteed base frequency
> on
> certain cores (high priority cores) in exchange for lower base
> frequency
> on remaining cores (low priority cores). aka PBF in the documenation.
> 
> Turbo frequency: Enables the ability to set different turbo ratio
> limits
> to cores based on priority. aka FACT in the documentation.
> 
> Core power: An Interface that allows user to define per core/tile
> priority.
> 
> There is a multi level help for commands and options. This can be
> used
> to check required arguments for each feature and commands for the
> feature.
> 
> To start navigating the features start with
> 
> $sudo intel-speed-select --help
> 
> For help on a specific feature for example
> $sudo intel-speed-select perf-profile --help
> 
> To get help for a command for a feature for example
> $sudo intel-speed-select perf-profile get-lock-status --help
> 
> Signed-off-by: Srinivas Pandruvada <
> srinivas.pandruvada@linux.intel.com>
> ---
> Updates:
> - Copied Makefile from tools/gpio and moified the Makefile here
> - Added entry to tools/build/Makefile
> - Rename directory to match the executable name
> - Fix one error message
> 
>  tools/Makefile                                |   12 +-
>  tools/power/x86/intel-speed-select/Build      |    1 +
>  tools/power/x86/intel-speed-select/Makefile   |   56 +
>  .../x86/intel-speed-select/isst-config.c      | 1607
> +++++++++++++++++
>  .../power/x86/intel-speed-select/isst-core.c  |  721 ++++++++
>  .../x86/intel-speed-select/isst-display.c     |  479 +++++
>  tools/power/x86/intel-speed-select/isst.h     |  231 +++
>  7 files changed, 3102 insertions(+), 5 deletions(-)
>  create mode 100644 tools/power/x86/intel-speed-select/Build
>  create mode 100644 tools/power/x86/intel-speed-select/Makefile
>  create mode 100644 tools/power/x86/intel-speed-select/isst-config.c
>  create mode 100644 tools/power/x86/intel-speed-select/isst-core.c
>  create mode 100644 tools/power/x86/intel-speed-select/isst-display.c
>  create mode 100644 tools/power/x86/intel-speed-select/isst.h
> 
> diff --git a/tools/Makefile b/tools/Makefile
> index 3dfd72ae6c1a..68defd7ecf5d 100644
> --- a/tools/Makefile
> +++ b/tools/Makefile
> @@ -19,6 +19,7 @@ help:
>  	@echo '  gpio                   - GPIO tools'
>  	@echo '  hv                     - tools used when in Hyper-V
> clients'
>  	@echo '  iio                    - IIO tools'
> +	@echo '  intel-speed-select     - Intel Speed Select tool'
>  	@echo '  kvm_stat               - top-like utility for
> displaying kvm statistics'
>  	@echo '  leds                   - LEDs  tools'
>  	@echo '  liblockdep             - user-space wrapper for kernel
> locking-validator'
> @@ -82,7 +83,7 @@ perf: FORCE
>  selftests: FORCE
>  	$(call descend,testing/$@)
>  
> -turbostat x86_energy_perf_policy: FORCE
> +turbostat x86_energy_perf_policy intel-speed-select: FORCE
>  	$(call descend,power/x86/$@)
>  
>  tmon: FORCE
> @@ -115,7 +116,7 @@ liblockdep_install:
>  selftests_install:
>  	$(call descend,testing/$(@:_install=),install)
>  
> -turbostat_install x86_energy_perf_policy_install:
> +turbostat_install x86_energy_perf_policy_install intel-speed-
> select_install:
>  	$(call descend,power/x86/$(@:_install=),install)
>  
>  tmon_install:
> @@ -132,7 +133,7 @@ install: acpi_install cgroup_install
> cpupower_install gpio_install \
>  		perf_install selftests_install turbostat_install
> usb_install \
>  		virtio_install vm_install bpf_install
> x86_energy_perf_policy_install \
>  		tmon_install freefall_install objtool_install
> kvm_stat_install \
> -		wmi_install pci_install debugging_install
> +		wmi_install pci_install debugging_install intel-speed-
> select_install
>  
>  acpi_clean:
>  	$(call descend,power/acpi,clean)
> @@ -162,7 +163,7 @@ perf_clean:
>  selftests_clean:
>  	$(call descend,testing/$(@:_clean=),clean)
>  
> -turbostat_clean x86_energy_perf_policy_clean:
> +turbostat_clean x86_energy_perf_policy_clean intel-speed-
> select_clean:
>  	$(call descend,power/x86/$(@:_clean=),clean)
>  
>  tmon_clean:
> @@ -178,6 +179,7 @@ clean: acpi_clean cgroup_clean cpupower_clean
> hv_clean firewire_clean \
>  		perf_clean selftests_clean turbostat_clean spi_clean
> usb_clean virtio_clean \
>  		vm_clean bpf_clean iio_clean
> x86_energy_perf_policy_clean tmon_clean \
>  		freefall_clean build_clean libbpf_clean libsubcmd_clean
> liblockdep_clean \
> -		gpio_clean objtool_clean leds_clean wmi_clean pci_clean
> firmware_clean debugging_clean
> +		gpio_clean objtool_clean leds_clean wmi_clean pci_clean
> firmware_clean debugging_clean \
> +		intel-speed-select_clean
>  
>  .PHONY: FORCE
> diff --git a/tools/power/x86/intel-speed-select/Build
> b/tools/power/x86/intel-speed-select/Build
> new file mode 100644
> index 000000000000..b61456d75190
> --- /dev/null
> +++ b/tools/power/x86/intel-speed-select/Build
> @@ -0,0 +1 @@
> +intel-speed-select-y +=  isst-config.o isst-core.o isst-display.o
> diff --git a/tools/power/x86/intel-speed-select/Makefile
> b/tools/power/x86/intel-speed-select/Makefile
> new file mode 100644
> index 000000000000..12c6939dca2a
> --- /dev/null
> +++ b/tools/power/x86/intel-speed-select/Makefile
> @@ -0,0 +1,56 @@
> +# SPDX-License-Identifier: GPL-2.0
> +include ../../../scripts/Makefile.include
> +
> +bindir ?= /usr/bin
> +
> +ifeq ($(srctree),)
> +srctree := $(patsubst %/,%,$(dir $(CURDIR)))
> +srctree := $(patsubst %/,%,$(dir $(srctree)))
> +srctree := $(patsubst %/,%,$(dir $(srctree)))
> +srctree := $(patsubst %/,%,$(dir $(srctree)))
> +endif
> +
> +# Do not use make's built-in rules
> +# (this improves performance and avoids hard-to-debug behaviour);
> +MAKEFLAGS += -r
> +
> +override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
> +
> +ALL_TARGETS := intel-speed-select
> +ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
> +
> +all: $(ALL_PROGRAMS)
> +
> +export srctree OUTPUT CC LD CFLAGS
> +include $(srctree)/tools/build/Makefile.include
> +
> +#
> +# We need the following to be outside of kernel tree
> +#
> +$(OUTPUT)include/linux/isst_if.h:
> ../../../../include/uapi/linux/isst_if.h
> +	mkdir -p $(OUTPUT)include/linux 2>&1 || true
> +	ln -sf $(CURDIR)/../../../../include/uapi/linux/isst_if.h $@
> +
> +prepare: $(OUTPUT)include/linux/isst_if.h
> +
> +ISST_IN := $(OUTPUT)intel-speed-select-in.o
> +
> +$(ISST_IN): prepare FORCE
> +	$(Q)$(MAKE) $(build)=intel-speed-select
> +$(OUTPUT)intel-speed-select: $(ISST_IN)
> +	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
> +
> +clean:
> +	rm -f $(ALL_PROGRAMS)
> +	rm -rf $(OUTPUT)include/linux/isst_if.h
> +	find $(if $(OUTPUT),$(OUTPUT),.) -name '*.o' -delete -o -name
> '\.*.d' -delete
> +
> +install: $(ALL_PROGRAMS)
> +	install -d -m 755 $(DESTDIR)$(bindir);		\
> +	for program in $(ALL_PROGRAMS); do		\
> +		install $$program $(DESTDIR)$(bindir);	\
> +	done
> +
> +FORCE:
> +
> +.PHONY: all install clean FORCE prepare
> diff --git a/tools/power/x86/intel-speed-select/isst-config.c
> b/tools/power/x86/intel-speed-select/isst-config.c
> new file mode 100644
> index 000000000000..91c5ad1685a1
> --- /dev/null
> +++ b/tools/power/x86/intel-speed-select/isst-config.c
> @@ -0,0 +1,1607 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Intel Speed Select -- Enumerate and control features
> + * Copyright (c) 2019 Intel Corporation.
> + */
> +
> +#include <linux/isst_if.h>
> +
> +#include "isst.h"
> +
> +struct process_cmd_struct {
> +	char *feature;
> +	char *command;
> +	void (*process_fn)(void);
> +};
> +
> +static const char *version_str = "v1.0";
> +static const int supported_api_ver = 1;
> +static struct isst_if_platform_info isst_platform_info;
> +static char *progname;
> +static int debug_flag;
> +static FILE *outf;
> +
> +static int cpu_model;
> +
> +#define MAX_CPUS_IN_ONE_REQ 64
> +static short max_target_cpus;
> +static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
> +
> +static int topo_max_cpus;
> +static size_t present_cpumask_size;
> +static cpu_set_t *present_cpumask;
> +static size_t target_cpumask_size;
> +static cpu_set_t *target_cpumask;
> +static int tdp_level = 0xFF;
> +static int fact_bucket = 0xFF;
> +static int fact_avx = 0xFF;
> +static unsigned long long fact_trl;
> +static int out_format_json;
> +static int cmd_help;
> +
> +/* clos related */
> +static int current_clos = -1;
> +static int clos_epp = -1;
> +static int clos_prop_prio = -1;
> +static int clos_min = -1;
> +static int clos_max = -1;
> +static int clos_desired = -1;
> +static int clos_priority_type;
> +
> +struct _cpu_map {
> +	unsigned short core_id;
> +	unsigned short pkg_id;
> +	unsigned short die_id;
> +	unsigned short punit_cpu;
> +	unsigned short punit_cpu_core;
> +};
> +struct _cpu_map *cpu_map;
> +
> +void debug_printf(const char *format, ...)
> +{
> +	va_list args;
> +
> +	va_start(args, format);
> +
> +	if (debug_flag)
> +		vprintf(format, args);
> +
> +	va_end(args);
> +}
> +
> +static void update_cpu_model(void)
> +{
> +	unsigned int ebx, ecx, edx;
> +	unsigned int fms, family;
> +
> +	__cpuid(1, fms, ebx, ecx, edx);
> +	family = (fms >> 8) & 0xf;
> +	cpu_model = (fms >> 4) & 0xf;
> +	if (family == 6 || family == 0xf)
> +		cpu_model += ((fms >> 16) & 0xf) << 4;
> +}
> +
> +/* Open a file, and exit on failure */
> +static FILE *fopen_or_exit(const char *path, const char *mode)
> +{
> +	FILE *filep = fopen(path, mode);
> +
> +	if (!filep)
> +		err(1, "%s: open failed", path);
> +
> +	return filep;
> +}
> +
> +/* Parse a file containing a single int */
> +static int parse_int_file(int fatal, const char *fmt, ...)
> +{
> +	va_list args;
> +	char path[PATH_MAX];
> +	FILE *filep;
> +	int value;
> +
> +	va_start(args, fmt);
> +	vsnprintf(path, sizeof(path), fmt, args);
> +	va_end(args);
> +	if (fatal) {
> +		filep = fopen_or_exit(path, "r");
> +	} else {
> +		filep = fopen(path, "r");
> +		if (!filep)
> +			return -1;
> +	}
> +	if (fscanf(filep, "%d", &value) != 1)
> +		err(1, "%s: failed to parse number from file", path);
> +	fclose(filep);
> +
> +	return value;
> +}
> +
> +int cpufreq_sysfs_present(void)
> +{
> +	DIR *dir;
> +
> +	dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
> +	if (dir) {
> +		closedir(dir);
> +		return 1;
> +	}
> +
> +	return 0;
> +}
> +
> +int out_format_is_json(void)
> +{
> +	return out_format_json;
> +}
> +
> +int get_physical_package_id(int cpu)
> +{
> +	return parse_int_file(
> +		1,
> "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
> +		cpu);
> +}
> +
> +int get_physical_core_id(int cpu)
> +{
> +	return parse_int_file(
> +		1, "/sys/devices/system/cpu/cpu%d/topology/core_id",
> cpu);
> +}
> +
> +int get_physical_die_id(int cpu)
> +{
> +	int ret;
> +
> +	ret = parse_int_file(0,
> "/sys/devices/system/cpu/cpu%d/topology/die_id",
> +			     cpu);
> +	if (ret < 0)
> +		ret = 0;
> +
> +	return ret;
> +}
> +
> +int get_topo_max_cpus(void)
> +{
> +	return topo_max_cpus;
> +}
> +
> +#define MAX_PACKAGE_COUNT 8
> +#define MAX_DIE_PER_PACKAGE 2
> +static void for_each_online_package_in_set(void (*callback)(int,
> void *, void *,
> +							    void *,
> void *),
> +					   void *arg1, void *arg2, void
> *arg3,
> +					   void *arg4)
> +{
> +	int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
> +	int pkg_index = 0, i;
> +
> +	memset(max_packages, 0xff, sizeof(max_packages));
> +	for (i = 0; i < topo_max_cpus; ++i) {
> +		int j, online, pkg_id, die_id = 0, skip = 0;
> +
> +		if (!CPU_ISSET_S(i, present_cpumask_size,
> present_cpumask))
> +			continue;
> +		if (i)
> +			online = parse_int_file(
> +				1,
> "/sys/devices/system/cpu/cpu%d/online", i);
> +		else
> +			online =
> +				1; /* online entry for CPU 0 needs some
> special configs */
> +
> +		die_id = get_physical_die_id(i);
> +		if (die_id < 0)
> +			die_id = 0;
> +		pkg_id = get_physical_package_id(i);
> +		/* Create an unique id for package, die combination to
> store */
> +		pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
> +
> +		for (j = 0; j < pkg_index; ++j) {
> +			if (max_packages[j] == pkg_id) {
> +				skip = 1;
> +				break;
> +			}
> +		}
> +
> +		if (!skip && online && callback) {
> +			callback(i, arg1, arg2, arg3, arg4);
> +			max_packages[pkg_index++] = pkg_id;
> +		}
> +	}
> +}
> +
> +static void for_each_online_target_cpu_in_set(
> +	void (*callback)(int, void *, void *, void *, void *), void
> *arg1,
> +	void *arg2, void *arg3, void *arg4)
> +{
> +	int i;
> +
> +	for (i = 0; i < topo_max_cpus; ++i) {
> +		int online;
> +
> +		if (!CPU_ISSET_S(i, target_cpumask_size,
> target_cpumask))
> +			continue;
> +		if (i)
> +			online = parse_int_file(
> +				1,
> "/sys/devices/system/cpu/cpu%d/online", i);
> +		else
> +			online =
> +				1; /* online entry for CPU 0 needs some
> special configs */
> +
> +		if (online && callback)
> +			callback(i, arg1, arg2, arg3, arg4);
> +	}
> +}
> +
> +#define BITMASK_SIZE 32
> +static void set_max_cpu_num(void)
> +{
> +	FILE *filep;
> +	unsigned long dummy;
> +
> +	topo_max_cpus = 0;
> +	filep = fopen_or_exit(
> +		"/sys/devices/system/cpu/cpu0/topology/thread_siblings"
> , "r");
> +	while (fscanf(filep, "%lx,", &dummy) == 1)
> +		topo_max_cpus += BITMASK_SIZE;
> +	fclose(filep);
> +	topo_max_cpus--; /* 0 based */
> +
> +	debug_printf("max cpus %d\n", topo_max_cpus);
> +}
> +
> +size_t alloc_cpu_set(cpu_set_t **cpu_set)
> +{
> +	cpu_set_t *_cpu_set;
> +	size_t size;
> +
> +	_cpu_set = CPU_ALLOC((topo_max_cpus + 1));
> +	if (_cpu_set == NULL)
> +		err(3, "CPU_ALLOC");
> +	size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
> +	CPU_ZERO_S(size, _cpu_set);
> +
> +	*cpu_set = _cpu_set;
> +	return size;
> +}
> +
> +void free_cpu_set(cpu_set_t *cpu_set)
> +{
> +	CPU_FREE(cpu_set);
> +}
> +
> +static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
> +static void set_cpu_present_cpu_mask(void)
> +{
> +	size_t size;
> +	DIR *dir;
> +	int i;
> +
> +	size = alloc_cpu_set(&present_cpumask);
> +	present_cpumask_size = size;
> +	for (i = 0; i < topo_max_cpus; ++i) {
> +		char buffer[256];
> +
> +		snprintf(buffer, sizeof(buffer),
> +			 "/sys/devices/system/cpu/cpu%d", i);
> +		dir = opendir(buffer);
> +		if (dir) {
> +			int pkg_id, die_id;
> +
> +			CPU_SET_S(i, size, present_cpumask);
> +			die_id = get_physical_die_id(i);
> +			if (die_id < 0)
> +				die_id = 0;
> +
> +			pkg_id = get_physical_package_id(i);
> +			if (pkg_id < MAX_PACKAGE_COUNT &&
> +			    die_id < MAX_DIE_PER_PACKAGE)
> +				cpu_cnt[pkg_id][die_id]++;
> +		}
> +		closedir(dir);
> +	}
> +}
> +
> +int get_cpu_count(int pkg_id, int die_id)
> +{
> +	if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
> +		return cpu_cnt[pkg_id][die_id] + 1;
> +
> +	return 0;
> +}
> +
> +static void set_cpu_target_cpu_mask(void)
> +{
> +	size_t size;
> +	int i;
> +
> +	size = alloc_cpu_set(&target_cpumask);
> +	target_cpumask_size = size;
> +	for (i = 0; i < max_target_cpus; ++i) {
> +		if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
> +				 present_cpumask))
> +			continue;
> +
> +		CPU_SET_S(target_cpus[i], size, target_cpumask);
> +	}
> +}
> +
> +static void create_cpu_map(void)
> +{
> +	const char *pathname = "/dev/isst_interface";
> +	int i, fd = 0;
> +	struct isst_if_cpu_maps map;
> +
> +	cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
> +	if (!cpu_map)
> +		err(3, "cpumap");
> +
> +	fd = open(pathname, O_RDWR);
> +	if (fd < 0)
> +		err(-1, "%s open failed", pathname);
> +
> +	for (i = 0; i < topo_max_cpus; ++i) {
> +		if (!CPU_ISSET_S(i, present_cpumask_size,
> present_cpumask))
> +			continue;
> +
> +		map.cmd_count = 1;
> +		map.cpu_map[0].logical_cpu = i;
> +
> +		debug_printf(" map logical_cpu:%d\n",
> +			     map.cpu_map[0].logical_cpu);
> +		if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
> +			perror("ISST_IF_GET_PHY_ID");
> +			fprintf(outf, "Error: map logical_cpu:%d\n",
> +				map.cpu_map[0].logical_cpu);
> +			continue;
> +		}
> +		cpu_map[i].core_id = get_physical_core_id(i);
> +		cpu_map[i].pkg_id = get_physical_package_id(i);
> +		cpu_map[i].die_id = get_physical_die_id(i);
> +		cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
> +		cpu_map[i].punit_cpu_core =
> (map.cpu_map[0].physical_cpu >>
> +					     1); // shift to get core
> id
> +
> +		debug_printf(
> +			"map logical_cpu:%d core: %d die:%d pkg:%d
> punit_cpu:%d punit_core:%d\n",
> +			i, cpu_map[i].core_id, cpu_map[i].die_id,
> +			cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
> +			cpu_map[i].punit_cpu_core);
> +	}
> +
> +	if (fd)
> +		close(fd);
> +}
> +
> +int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
> +{
> +	int i;
> +
> +	for (i = 0; i < topo_max_cpus; ++i) {
> +		if (cpu_map[i].pkg_id == pkg_id &&
> +		    cpu_map[i].die_id == die_id &&
> +		    cpu_map[i].punit_cpu_core == punit_core_id)
> +			return i;
> +	}
> +
> +	return -EINVAL;
> +}
> +
> +void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long
> core_mask,
> +				      size_t core_cpumask_size,
> +				      cpu_set_t *core_cpumask, int
> *cpu_cnt)
> +{
> +	int i, cnt = 0;
> +	int die_id, pkg_id;
> +
> +	*cpu_cnt = 0;
> +	die_id = get_physical_die_id(cpu);
> +	pkg_id = get_physical_package_id(cpu);
> +
> +	for (i = 0; i < 64; ++i) {
> +		if (core_mask & BIT(i)) {
> +			int j;
> +
> +			for (j = 0; j < topo_max_cpus; ++j) {
> +				if (cpu_map[j].pkg_id == pkg_id &&
> +				    cpu_map[j].die_id == die_id &&
> +				    cpu_map[j].punit_cpu_core == i) {
> +					CPU_SET_S(j, core_cpumask_size,
> +						  core_cpumask);
> +					++cnt;
> +				}
> +			}
> +		}
> +	}
> +
> +	*cpu_cnt = cnt;
> +}
> +
> +int find_phy_core_num(int logical_cpu)
> +{
> +	if (logical_cpu < topo_max_cpus)
> +		return cpu_map[logical_cpu].punit_cpu_core;
> +
> +	return -EINVAL;
> +}
> +
> +static int isst_send_mmio_command(unsigned int cpu, unsigned int
> reg, int write,
> +				  unsigned int *value)
> +{
> +	struct isst_if_io_regs io_regs;
> +	const char *pathname = "/dev/isst_interface";
> +	int cmd;
> +	int fd;
> +
> +	debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg,
> write);
> +
> +	fd = open(pathname, O_RDWR);
> +	if (fd < 0)
> +		err(-1, "%s open failed", pathname);
> +
> +	io_regs.req_count = 1;
> +	io_regs.io_reg[0].logical_cpu = cpu;
> +	io_regs.io_reg[0].reg = reg;
> +	cmd = ISST_IF_IO_CMD;
> +	if (write) {
> +		io_regs.io_reg[0].read_write = 1;
> +		io_regs.io_reg[0].value = *value;
> +	} else {
> +		io_regs.io_reg[0].read_write = 0;
> +	}
> +
> +	if (ioctl(fd, cmd, &io_regs) == -1) {
> +		perror("ISST_IF_IO_CMD");
> +		fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x
> read_write:%x\n",
> +			cpu, reg, write);
> +	} else {
> +		if (!write)
> +			*value = io_regs.io_reg[0].value;
> +
> +		debug_printf(
> +			"mmio_cmd response: cpu:%d reg:%x rd_write:%x
> resp:%x\n",
> +			cpu, reg, write, *value);
> +	}
> +
> +	close(fd);
> +
> +	return 0;
> +}
> +
> +int isst_send_mbox_command(unsigned int cpu, unsigned char command,
> +			   unsigned char sub_command, unsigned int
> parameter,
> +			   unsigned int req_data, unsigned int *resp)
> +{
> +	const char *pathname = "/dev/isst_interface";
> +	int fd;
> +	struct isst_if_mbox_cmds mbox_cmds = { 0 };
> +
> +	debug_printf(
> +		"mbox_send: cpu:%d command:%x sub_command:%x
> parameter:%x req_data:%x\n",
> +		cpu, command, sub_command, parameter, req_data);
> +
> +	if (isst_platform_info.mmio_supported && command ==
> CONFIG_CLOS) {
> +		unsigned int value;
> +		int write = 0;
> +		int clos_id, core_id, ret = 0;
> +
> +		debug_printf("CLOS %d\n", cpu);
> +
> +		if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
> +			value = req_data;
> +			write = 1;
> +		}
> +
> +		switch (sub_command) {
> +		case CLOS_PQR_ASSOC:
> +			core_id = parameter & 0xff;
> +			ret = isst_send_mmio_command(
> +				cpu, PQR_ASSOC_OFFSET + core_id * 4,
> write,
> +				&value);
> +			if (!ret && !write)
> +				*resp = value;
> +			break;
> +		case CLOS_PM_CLOS:
> +			clos_id = parameter & 0x03;
> +			ret = isst_send_mmio_command(
> +				cpu, PM_CLOS_OFFSET + clos_id * 4,
> write,
> +				&value);
> +			if (!ret && !write)
> +				*resp = value;
> +			break;
> +		case CLOS_PM_QOS_CONFIG:
> +			ret = isst_send_mmio_command(cpu,
> PM_QOS_CONFIG_OFFSET,
> +						     write, &value);
> +			if (!ret && !write)
> +				*resp = value;
> +			break;
> +		case CLOS_STATUS:
> +			break;
> +		default:
> +			break;
> +		}
> +		return ret;
> +	}
> +
> +	mbox_cmds.cmd_count = 1;
> +	mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
> +	mbox_cmds.mbox_cmd[0].command = command;
> +	mbox_cmds.mbox_cmd[0].sub_command = sub_command;
> +	mbox_cmds.mbox_cmd[0].parameter = parameter;
> +	mbox_cmds.mbox_cmd[0].req_data = req_data;
> +
> +	fd = open(pathname, O_RDWR);
> +	if (fd < 0)
> +		err(-1, "%s open failed", pathname);
> +
> +	if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
> +		perror("ISST_IF_MBOX_COMMAND");
> +		fprintf(outf,
> +			"Error: mbox_cmd cpu:%d command:%x
> sub_command:%x parameter:%x req_data:%x\n",
> +			cpu, command, sub_command, parameter,
> req_data);
> +	} else {
> +		*resp = mbox_cmds.mbox_cmd[0].resp_data;
> +		debug_printf(
> +			"mbox_cmd response: cpu:%d command:%x
> sub_command:%x parameter:%x req_data:%x resp:%x\n",
> +			cpu, command, sub_command, parameter, req_data,
> *resp);
> +	}
> +
> +	close(fd);
> +
> +	return 0;
> +}
> +
> +int isst_send_msr_command(unsigned int cpu, unsigned int msr, int
> write,
> +			  unsigned long long *req_resp)
> +{
> +	struct isst_if_msr_cmds msr_cmds;
> +	const char *pathname = "/dev/isst_interface";
> +	int fd;
> +
> +	fd = open(pathname, O_RDWR);
> +	if (fd < 0)
> +		err(-1, "%s open failed", pathname);
> +
> +	msr_cmds.cmd_count = 1;
> +	msr_cmds.msr_cmd[0].logical_cpu = cpu;
> +	msr_cmds.msr_cmd[0].msr = msr;
> +	msr_cmds.msr_cmd[0].read_write = write;
> +	if (write)
> +		msr_cmds.msr_cmd[0].data = *req_resp;
> +
> +	if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
> +		perror("ISST_IF_MSR_COMMAD");
> +		fprintf(outf, "Error: msr_cmd cpu:%d msr:%x
> read_write:%d\n",
> +			cpu, msr, write);
> +	} else {
> +		if (!write)
> +			*req_resp = msr_cmds.msr_cmd[0].data;
> +
> +		debug_printf(
> +			"msr_cmd response: cpu:%d msr:%x rd_write:%x
> resp:%llx %llx\n",
> +			cpu, msr, write, *req_resp,
> msr_cmds.msr_cmd[0].data);
> +	}
> +
> +	close(fd);
> +
> +	return 0;
> +}
> +
> +static int isst_fill_platform_info(void)
> +{
> +	const char *pathname = "/dev/isst_interface";
> +	int fd;
> +
> +	fd = open(pathname, O_RDWR);
> +	if (fd < 0)
> +		err(-1, "%s open failed", pathname);
> +
> +	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info)
> == -1) {
> +		perror("ISST_IF_GET_PLATFORM_INFO");
> +		close(fd);
> +		return -1;
> +	}
> +
> +	close(fd);
> +
> +	return 0;
> +}
> +
> +static void isst_print_platform_information(void)
> +{
> +	struct isst_if_platform_info platform_info;
> +	const char *pathname = "/dev/isst_interface";
> +	int fd;
> +
> +	fd = open(pathname, O_RDWR);
> +	if (fd < 0)
> +		err(-1, "%s open failed", pathname);
> +
> +	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1)
> {
> +		perror("ISST_IF_GET_PLATFORM_INFO");
> +	} else {
> +		fprintf(outf, "Platform: API version : %d\n",
> +			platform_info.api_version);
> +		fprintf(outf, "Platform: Driver version : %d\n",
> +			platform_info.driver_version);
> +		fprintf(outf, "Platform: mbox supported : %d\n",
> +			platform_info.mbox_supported);
> +		fprintf(outf, "Platform: mmio supported : %d\n",
> +			platform_info.mmio_supported);
> +	}
> +
> +	close(fd);
> +
> +	exit(0);
> +}
> +
> +static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2,
> void *arg3,
> +				 void *arg4)
> +{
> +	int (*fn_ptr)(int cpu, void *arg);
> +	int ret;
> +
> +	fn_ptr = arg1;
> +	ret = fn_ptr(cpu, arg2);
> +	if (ret)
> +		perror("get_tdp_*");
> +	else
> +		isst_display_result(cpu, outf, "perf-profile", (char
> *)arg3,
> +				    *(unsigned int *)arg4);
> +}
> +
> +#define _get_tdp_level(desc, suffix, object,
> help)                                \
> +	static void
> get_tdp_##object(void)                                        \
> +	{                                                              
>            \
> +		struct isst_pkg_ctdp
> ctdp;                                        \
> +\
> +		if (cmd_help)
> {                                                   \
> +			fprintf(stderr,                                
>            \
> +				"Print %s [No command arguments are
> required]\n", \
> +				help);                                 
>            \
> +			exit(0);                                       
>            \
> +		}                                                      
>            \
> +		isst_ctdp_display_information_start(outf);             
>            \
> +		if
> (max_target_cpus)                                              \
> +			for_each_online_target_cpu_in_set(             
>            \
> +				exec_on_get_ctdp_cpu,
> isst_get_ctdp_##suffix,     \
> +				&ctdp, desc,
> &ctdp.object);                       \
> +		else                                                   
>            \
> +			for_each_online_package_in_set(exec_on_get_ctdp
> _cpu,      \
> +						       isst_get_ctdp_##
> suffix,    \
> +						       &ctdp,
> desc,               \
> +						       &ctdp.object);  
>            \
> +		isst_ctdp_display_information_end(outf);               
>            \
> +	}
> +
> +_get_tdp_level("get-config-levels", levels, levels, "TDP levels");
> +_get_tdp_level("get-config-version", levels, version, "TDP
> version");
> +_get_tdp_level("get-config-enabled", levels, enabled, "TDP enable
> status");
> +_get_tdp_level("get-config-current_level", levels, current_level,
> +	       "Current TDP Level");
> +_get_tdp_level("get-lock-status", levels, locked, "TDP lock
> status");
> +
> +static void dump_isst_config_for_cpu(int cpu, void *arg1, void
> *arg2,
> +				     void *arg3, void *arg4)
> +{
> +	struct isst_pkg_ctdp pkg_dev;
> +	int ret;
> +
> +	memset(&pkg_dev, 0, sizeof(pkg_dev));
> +	ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
> +	if (ret) {
> +		perror("isst_get_process_ctdp");
> +	} else {
> +		isst_ctdp_display_information(cpu, outf, tdp_level,
> &pkg_dev);
> +		isst_get_process_ctdp_complete(cpu, &pkg_dev);
> +	}
> +}
> +
> +static void dump_isst_config(void)
> +{
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Print Intel(R) Speed Select Technology
> Performance profile configuration\n");
> +		fprintf(stderr,
> +			"including base frequency and turbo frequency
> configurations\n");
> +		fprintf(stderr, "Optional: -l|--level : Specify tdp
> level\n");
> +		fprintf(stderr,
> +			"\tIf no arguments, dump information for all
> TDP levels\n");
> +		exit(0);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(dump_isst_config_for_
> cpu,
> +						  NULL, NULL, NULL,
> NULL);
> +	else
> +		for_each_online_package_in_set(dump_isst_config_for_cpu
> , NULL,
> +					       NULL, NULL, NULL);
> +
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2,
> void *arg3,
> +				  void *arg4)
> +{
> +	int ret;
> +
> +	ret = isst_set_tdp_level(cpu, tdp_level);
> +	if (ret)
> +		perror("set_tdp_level_for_cpu");
> +	else
> +		isst_display_result(cpu, outf, "perf-profile",
> "set_tdp_level",
> +				    ret);
> +}
> +
> +static void set_tdp_level(void)
> +{
> +	if (cmd_help) {
> +		fprintf(stderr, "Set Config TDP level\n");
> +		fprintf(stderr,
> +			"\t Arguments: -l|--level : Specify tdp
> level\n");
> +		exit(0);
> +	}
> +
> +	if (tdp_level == 0xff) {
> +		fprintf(outf, "Invalid command: specify tdp_level\n");
> +		exit(1);
> +	}
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(set_tdp_level_for_cpu
> , NULL,
> +						  NULL, NULL, NULL);
> +	else
> +		for_each_online_package_in_set(set_tdp_level_for_cpu,
> NULL,
> +					       NULL, NULL, NULL);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2,
> void *arg3,
> +				    void *arg4)
> +{
> +	struct isst_pbf_info pbf_info;
> +	int ret;
> +
> +	ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
> +	if (ret) {
> +		perror("isst_get_pbf_info");
> +	} else {
> +		isst_pbf_display_information(cpu, outf, tdp_level,
> &pbf_info);
> +		isst_get_pbf_info_complete(&pbf_info);
> +	}
> +}
> +
> +static void dump_pbf_config(void)
> +{
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Print Intel(R) Speed Select Technology base
> frequency configuration for a TDP level\n");
> +		fprintf(stderr,
> +			"\tArguments: -l|--level : Specify tdp
> level\n");
> +		exit(0);
> +	}
> +
> +	if (tdp_level == 0xff) {
> +		fprintf(outf, "Invalid command: specify tdp_level\n");
> +		exit(1);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(dump_pbf_config_for_c
> pu, NULL,
> +						  NULL, NULL, NULL);
> +	else
> +		for_each_online_package_in_set(dump_pbf_config_for_cpu,
> NULL,
> +					       NULL, NULL, NULL);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void
> *arg3,
> +			    void *arg4)
> +{
> +	int ret;
> +	int status = *(int *)arg4;
> +
> +	ret = isst_set_pbf_fact_status(cpu, 1, status);
> +	if (ret) {
> +		perror("isst_set_pbf");
> +	} else {
> +		if (status)
> +			isst_display_result(cpu, outf, "base-freq",
> "enable",
> +					    ret);
> +		else
> +			isst_display_result(cpu, outf, "base-freq",
> "disable",
> +					    ret);
> +	}
> +}
> +
> +static void set_pbf_enable(void)
> +{
> +	int status = 1;
> +
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Enable Intel Speed Select Technology base
> frequency feature [No command arguments are required]\n");
> +		exit(0);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(set_pbf_for_cpu,
> NULL, NULL,
> +						  NULL, &status);
> +	else
> +		for_each_online_package_in_set(set_pbf_for_cpu, NULL,
> NULL,
> +					       NULL, &status);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void set_pbf_disable(void)
> +{
> +	int status = 0;
> +
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Disable Intel Speed Select Technology base
> frequency feature [No command arguments are required]\n");
> +		exit(0);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(set_pbf_for_cpu,
> NULL, NULL,
> +						  NULL, &status);
> +	else
> +		for_each_online_package_in_set(set_pbf_for_cpu, NULL,
> NULL,
> +					       NULL, &status);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void dump_fact_config_for_cpu(int cpu, void *arg1, void
> *arg2,
> +				     void *arg3, void *arg4)
> +{
> +	struct isst_fact_info fact_info;
> +	int ret;
> +
> +	ret = isst_get_fact_info(cpu, tdp_level, &fact_info);
> +	if (ret)
> +		perror("isst_get_fact_bucket_info");
> +	else
> +		isst_fact_display_information(cpu, outf, tdp_level,
> fact_bucket,
> +					      fact_avx, &fact_info);
> +}
> +
> +static void dump_fact_config(void)
> +{
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Print complete Intel Speed Select Technology
> turbo frequency configuration for a TDP level. Other arguments are
> optional.\n");
> +		fprintf(stderr,
> +			"\tArguments: -l|--level : Specify tdp
> level\n");
> +		fprintf(stderr,
> +			"\tArguments: -b|--bucket : Bucket index to
> dump\n");
> +		fprintf(stderr,
> +			"\tArguments: -r|--trl-type : Specify trl type:
> sse|avx2|avx512\n");
> +		exit(0);
> +	}
> +
> +	if (tdp_level == 0xff) {
> +		fprintf(outf, "Invalid command: specify tdp_level\n");
> +		exit(1);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(dump_fact_config_for_
> cpu,
> +						  NULL, NULL, NULL,
> NULL);
> +	else
> +		for_each_online_package_in_set(dump_fact_config_for_cpu
> , NULL,
> +					       NULL, NULL, NULL);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void
> *arg3,
> +			     void *arg4)
> +{
> +	int ret;
> +	int status = *(int *)arg4;
> +
> +	ret = isst_set_pbf_fact_status(cpu, 0, status);
> +	if (ret)
> +		perror("isst_set_fact");
> +	else {
> +		if (status) {
> +			struct isst_pkg_ctdp pkg_dev;
> +
> +			ret = isst_get_ctdp_levels(cpu, &pkg_dev);
> +			if (ret) {
> +				isst_display_result(cpu, outf, "turbo-
> freq",
> +						    "enable", ret);
> +				return;
> +			}
> +			ret = isst_set_trl(cpu, fact_trl);
> +			isst_display_result(cpu, outf, "turbo-freq",
> "enable",
> +					    ret);
> +		} else {
> +			/* Since we modified TRL during Fact enable,
> restore it */
> +			isst_set_trl_from_current_tdp(cpu, fact_trl);
> +			isst_display_result(cpu, outf, "turbo-freq",
> "disable",
> +					    ret);
> +		}
> +	}
> +}
> +
> +static void set_fact_enable(void)
> +{
> +	int status = 1;
> +
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Enable Intel Speed Select Technology Turbo
> frequency feature\n");
> +		fprintf(stderr,
> +			"Optional: -t|--trl : Specify turbo ratio
> limit\n");
> +		exit(0);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(set_fact_for_cpu,
> NULL, NULL,
> +						  NULL, &status);
> +	else
> +		for_each_online_package_in_set(set_fact_for_cpu, NULL,
> NULL,
> +					       NULL, &status);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void set_fact_disable(void)
> +{
> +	int status = 0;
> +
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Disable Intel Speed Select Technology turbo
> frequency feature\n");
> +		fprintf(stderr,
> +			"Optional: -t|--trl : Specify turbo ratio
> limit\n");
> +		exit(0);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(set_fact_for_cpu,
> NULL, NULL,
> +						  NULL, &status);
> +	else
> +		for_each_online_package_in_set(set_fact_for_cpu, NULL,
> NULL,
> +					       NULL, &status);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void enable_clos_qos_config(int cpu, void *arg1, void *arg2,
> void *arg3,
> +				   void *arg4)
> +{
> +	int ret;
> +	int status = *(int *)arg4;
> +
> +	ret = isst_pm_qos_config(cpu, status, clos_priority_type);
> +	if (ret) {
> +		perror("isst_pm_qos_config");
> +	} else {
> +		if (status)
> +			isst_display_result(cpu, outf, "core-power",
> "enable",
> +					    ret);
> +		else
> +			isst_display_result(cpu, outf, "core-power",
> "disable",
> +					    ret);
> +	}
> +}
> +
> +static void set_clos_enable(void)
> +{
> +	int status = 1;
> +
> +	if (cmd_help) {
> +		fprintf(stderr, "Enable core-power for a
> package/die\n");
> +		fprintf(stderr,
> +			"\tClos Enable: Specify priority type with [
> --priority|-p]\n");
> +		fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
> +		exit(0);
> +	}
> +
> +	if (cpufreq_sysfs_present()) {
> +		fprintf(stderr,
> +			"cpufreq subsystem and core-power enable will
> interfere with each other!\n");
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(enable_clos_qos_confi
> g, NULL,
> +						  NULL, NULL, &status);
> +	else
> +		for_each_online_package_in_set(enable_clos_qos_config,
> NULL,
> +					       NULL, NULL, &status);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void set_clos_disable(void)
> +{
> +	int status = 0;
> +
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Disable core-power: [No command arguments are
> required]\n");
> +		exit(0);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(enable_clos_qos_confi
> g, NULL,
> +						  NULL, NULL, &status);
> +	else
> +		for_each_online_package_in_set(enable_clos_qos_config,
> NULL,
> +					       NULL, NULL, &status);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void dump_clos_config_for_cpu(int cpu, void *arg1, void
> *arg2,
> +				     void *arg3, void *arg4)
> +{
> +	struct isst_clos_config clos_config;
> +	int ret;
> +
> +	ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
> +	if (ret)
> +		perror("isst_pm_get_clos");
> +	else
> +		isst_clos_display_information(cpu, outf, current_clos,
> +					      &clos_config);
> +}
> +
> +static void dump_clos_config(void)
> +{
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Print Intel Speed Select Technology core power
> configuration\n");
> +		fprintf(stderr,
> +			"\tArguments: [-c | --clos]: Specify clos
> id\n");
> +		exit(0);
> +	}
> +	if (current_clos < 0 || current_clos > 3) {
> +		fprintf(stderr, "Invalid clos id\n");
> +		exit(0);
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(dump_clos_config_for_
> cpu,
> +						  NULL, NULL, NULL,
> NULL);
> +	else
> +		for_each_online_package_in_set(dump_clos_config_for_cpu
> , NULL,
> +					       NULL, NULL, NULL);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
> void *arg3,
> +				    void *arg4)
> +{
> +	struct isst_clos_config clos_config;
> +	int ret;
> +
> +	clos_config.pkg_id = get_physical_package_id(cpu);
> +	clos_config.die_id = get_physical_die_id(cpu);
> +
> +	clos_config.epp = clos_epp;
> +	clos_config.clos_prop_prio = clos_prop_prio;
> +	clos_config.clos_min = clos_min;
> +	clos_config.clos_max = clos_max;
> +	clos_config.clos_desired = clos_desired;
> +	ret = isst_set_clos(cpu, current_clos, &clos_config);
> +	if (ret)
> +		perror("isst_set_clos");
> +	else
> +		isst_display_result(cpu, outf, "core-power", "config",
> ret);
> +}
> +
> +static void set_clos_config(void)
> +{
> +	if (cmd_help) {
> +		fprintf(stderr,
> +			"Set core-power configuration for one of the
> four clos ids\n");
> +		fprintf(stderr,
> +			"\tSpecify targeted clos id with [--clos|-
> c]\n");
> +		fprintf(stderr, "\tSpecify clos EPP with [--epp|-
> e]\n");
> +		fprintf(stderr,
> +			"\tSpecify clos Proportional Priority [
> --weight|-w]\n");
> +		fprintf(stderr, "\tSpecify clos min with [--min|-
> n]\n");
> +		fprintf(stderr, "\tSpecify clos max with [--max|-
> m]\n");
> +		fprintf(stderr, "\tSpecify clos desired with [
> --desired|-d]\n");
> +		exit(0);
> +	}
> +
> +	if (current_clos < 0 || current_clos > 3) {
> +		fprintf(stderr, "Invalid clos id\n");
> +		exit(0);
> +	}
> +	if (clos_epp < 0 || clos_epp > 0x0F) {
> +		fprintf(stderr, "clos epp is not specified, default:
> 0\n");
> +		clos_epp = 0;
> +	}
> +	if (clos_prop_prio < 0 || clos_prop_prio > 0x0F) {
> +		fprintf(stderr,
> +			"clos frequency weight is not specified,
> default: 0\n");
> +		clos_prop_prio = 0;
> +	}
> +	if (clos_min < 0) {
> +		fprintf(stderr, "clos min is not specified, default:
> 0\n");
> +		clos_min = 0;
> +	}
> +	if (clos_max < 0) {
> +		fprintf(stderr, "clos max is not specified, default:
> 0xff\n");
> +		clos_max = 0xff;
> +	}
> +	if (clos_desired < 0) {
> +		fprintf(stderr, "clos desired is not specified,
> default: 0\n");
> +		clos_desired = 0x00;
> +	}
> +
> +	isst_ctdp_display_information_start(outf);
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(set_clos_config_for_c
> pu, NULL,
> +						  NULL, NULL, NULL);
> +	else
> +		for_each_online_package_in_set(set_clos_config_for_cpu,
> NULL,
> +					       NULL, NULL, NULL);
> +	isst_ctdp_display_information_end(outf);
> +}
> +
> +static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2,
> void *arg3,
> +				   void *arg4)
> +{
> +	int ret;
> +
> +	ret = isst_clos_associate(cpu, current_clos);
> +	if (ret)
> +		perror("isst_clos_associate");
> +	else
> +		isst_display_result(cpu, outf, "core-power", "assoc",
> ret);
> +}
> +
> +static void set_clos_assoc(void)
> +{
> +	if (cmd_help) {
> +		fprintf(stderr, "Associate a clos id to a CPU\n");
> +		fprintf(stderr,
> +			"\tSpecify targeted clos id with [--clos|-
> c]\n");
> +		exit(0);
> +	}
> +
> +	if (current_clos < 0 || current_clos > 3) {
> +		fprintf(stderr, "Invalid clos id\n");
> +		exit(0);
> +	}
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(set_clos_assoc_for_cp
> u, NULL,
> +						  NULL, NULL, NULL);
> +	else {
> +		fprintf(stderr,
> +			"Invalid target cpu. Specify with [-c|
> --cpu]\n");
> +	}
> +}
> +
> +static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2,
> void *arg3,
> +				   void *arg4)
> +{
> +	int clos, ret;
> +
> +	ret = isst_clos_get_assoc_status(cpu, &clos);
> +	if (ret)
> +		perror("isst_clos_get_assoc_status");
> +	else
> +		isst_display_result(cpu, outf, "core-power", "get-
> assoc", clos);
> +}
> +
> +static void get_clos_assoc(void)
> +{
> +	if (cmd_help) {
> +		fprintf(stderr, "Get associate clos id to a CPU\n");
> +		fprintf(stderr, "\tSpecify targeted cpu id with [
> --cpu|-c]\n");
> +		exit(0);
> +	}
> +	if (max_target_cpus)
> +		for_each_online_target_cpu_in_set(get_clos_assoc_for_cp
> u, NULL,
> +						  NULL, NULL, NULL);
> +	else {
> +		fprintf(stderr,
> +			"Invalid target cpu. Specify with [-c|
> --cpu]\n");
> +	}
> +}
> +
> +static struct process_cmd_struct isst_cmds[] = {
> +	{ "perf-profile", "get-lock-status", get_tdp_locked },
> +	{ "perf-profile", "get-config-levels", get_tdp_levels },
> +	{ "perf-profile", "get-config-version", get_tdp_version },
> +	{ "perf-profile", "get-config-enabled", get_tdp_enabled },
> +	{ "perf-profile", "get-config-current-level",
> get_tdp_current_level },
> +	{ "perf-profile", "set-config-level", set_tdp_level },
> +	{ "perf-profile", "info", dump_isst_config },
> +	{ "base-freq", "info", dump_pbf_config },
> +	{ "base-freq", "enable", set_pbf_enable },
> +	{ "base-freq", "disable", set_pbf_disable },
> +	{ "turbo-freq", "info", dump_fact_config },
> +	{ "turbo-freq", "enable", set_fact_enable },
> +	{ "turbo-freq", "disable", set_fact_disable },
> +	{ "core-power", "info", dump_clos_config },
> +	{ "core-power", "enable", set_clos_enable },
> +	{ "core-power", "disable", set_clos_disable },
> +	{ "core-power", "config", set_clos_config },
> +	{ "core-power", "assoc", set_clos_assoc },
> +	{ "core-power", "get-assoc", get_clos_assoc },
> +	{ NULL, NULL, NULL }
> +};
> +
> +/*
> + * parse cpuset with following syntax
> + * 1,2,4..6,8-10 and set bits in cpu_subset
> + */
> +void parse_cpu_command(char *optarg)
> +{
> +	unsigned int start, end;
> +	char *next;
> +
> +	next = optarg;
> +
> +	while (next && *next) {
> +		if (*next == '-') /* no negative cpu numbers */
> +			goto error;
> +
> +		start = strtoul(next, &next, 10);
> +
> +		if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
> +			target_cpus[max_target_cpus++] = start;
> +
> +		if (*next == '\0')
> +			break;
> +
> +		if (*next == ',') {
> +			next += 1;
> +			continue;
> +		}
> +
> +		if (*next == '-') {
> +			next += 1; /* start range */
> +		} else if (*next == '.') {
> +			next += 1;
> +			if (*next == '.')
> +				next += 1; /* start range */
> +			else
> +				goto error;
> +		}
> +
> +		end = strtoul(next, &next, 10);
> +		if (end <= start)
> +			goto error;
> +
> +		while (++start <= end) {
> +			if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
> +				target_cpus[max_target_cpus++] = start;
> +		}
> +
> +		if (*next == ',')
> +			next += 1;
> +		else if (*next != '\0')
> +			goto error;
> +	}
> +
> +#ifdef DEBUG
> +	{
> +		int i;
> +
> +		for (i = 0; i < max_target_cpus; ++i)
> +			printf("cpu [%d] in arg\n", target_cpus[i]);
> +	}
> +#endif
> +	return;
> +
> +error:
> +	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
> +	exit(-1);
> +}
> +
> +static void parse_cmd_args(int argc, int start, char **argv)
> +{
> +	int opt;
> +	int option_index;
> +
> +	static struct option long_options[] = {
> +		{ "bucket", required_argument, 0, 'b' },
> +		{ "level", required_argument, 0, 'l' },
> +		{ "trl-type", required_argument, 0, 'r' },
> +		{ "trl", required_argument, 0, 't' },
> +		{ "help", no_argument, 0, 'h' },
> +		{ "clos", required_argument, 0, 'c' },
> +		{ "desired", required_argument, 0, 'd' },
> +		{ "epp", required_argument, 0, 'e' },
> +		{ "min", required_argument, 0, 'n' },
> +		{ "max", required_argument, 0, 'm' },
> +		{ "priority", required_argument, 0, 'p' },
> +		{ "weight", required_argument, 0, 'w' },
> +		{ 0, 0, 0, 0 }
> +	};
> +
> +	option_index = start;
> +
> +	optind = start + 1;
> +	while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:h",
> +				  long_options, &option_index)) != -1)
> {
> +		switch (opt) {
> +		case 'b':
> +			fact_bucket = atoi(optarg);
> +			break;
> +		case 'h':
> +			cmd_help = 1;
> +			break;
> +		case 'l':
> +			tdp_level = atoi(optarg);
> +			break;
> +		case 't':
> +			sscanf(optarg, "0x%llx", &fact_trl);
> +			break;
> +		case 'r':
> +			if (!strncmp(optarg, "sse", 3)) {
> +				fact_avx = 0x01;
> +			} else if (!strncmp(optarg, "avx2", 4)) {
> +				fact_avx = 0x02;
> +			} else if (!strncmp(optarg, "avx512", 4)) {
> +				fact_avx = 0x04;
> +			} else {
> +				fprintf(outf, "Invalid sse,avx
> options\n");
> +				exit(1);
> +			}
> +			break;
> +		/* CLOS related */
> +		case 'c':
> +			current_clos = atoi(optarg);
> +			printf("clos %d\n", current_clos);
> +			break;
> +		case 'd':
> +			clos_desired = atoi(optarg);
> +			break;
> +		case 'e':
> +			clos_epp = atoi(optarg);
> +			break;
> +		case 'n':
> +			clos_min = atoi(optarg);
> +			break;
> +		case 'm':
> +			clos_max = atoi(optarg);
> +			break;
> +		case 'p':
> +			clos_priority_type = atoi(optarg);
> +			break;
> +		case 'w':
> +			clos_prop_prio = atoi(optarg);
> +			break;
> +		default:
> +			printf("no match\n");
> +		}
> +	}
> +}
> +
> +static void isst_help(void)
> +{
> +	printf("perf-profile:\tAn architectural mechanism that allows
> multiple optimized \n\
> +		performance profiles per system via static and/or
> dynamic\n\
> +		adjustment of core count, workload, Tjmax, and\n\
> +		TDP, etc.\n");
> +	printf("\nCommands : For feature=perf-profile\n");
> +	printf("\tinfo\n");
> +	printf("\tget-lock-status\n");
> +	printf("\tget-config-levels\n");
> +	printf("\tget-config-version\n");
> +	printf("\tget-config-enabled\n");
> +	printf("\tget-config-current-level\n");
> +	printf("\tset-config-level\n");
> +}
> +
> +static void pbf_help(void)
> +{
> +	printf("base-freq:\tEnables users to increase guaranteed base
> frequency\n\
> +		on certain cores (high priority cores) in exchange for
> lower\n\
> +		base frequency on remaining cores (low priority
> cores).\n");
> +	printf("\tcommand : info\n");
> +	printf("\tcommand : enable\n");
> +	printf("\tcommand : disable\n");
> +}
> +
> +static void fact_help(void)
> +{
> +	printf("turbo-freq:\tEnables the ability to set different turbo
> ratio\n\
> +		limits to cores based on priority.\n");
> +	printf("\nCommand: For feature=turbo-freq\n");
> +	printf("\tcommand : info\n");
> +	printf("\tcommand : enable\n");
> +	printf("\tcommand : disable\n");
> +}
> +
> +static void core_power_help(void)
> +{
> +	printf("core-power:\tInterface that allows user to define per
> core/tile\n\
> +		priority.\n");
> +	printf("\nCommands : For feature=core-power\n");
> +	printf("\tinfo\n");
> +	printf("\tenable\n");
> +	printf("\tdisable\n");
> +	printf("\tconfig\n");
> +	printf("\tassoc\n");
> +	printf("\tget-assoc\n");
> +}
> +
> +struct process_cmd_help_struct {
> +	char *feature;
> +	void (*process_fn)(void);
> +};
> +
> +static struct process_cmd_help_struct isst_help_cmds[] = {
> +	{ "perf-profile", isst_help },
> +	{ "base-freq", pbf_help },
> +	{ "turbo-freq", fact_help },
> +	{ "core-power", core_power_help },
> +	{ NULL, NULL }
> +};
> +
> +void process_command(int argc, char **argv)
> +{
> +	int i = 0, matched = 0;
> +	char *feature = argv[optind];
> +	char *cmd = argv[optind + 1];
> +
> +	if (!feature || !cmd)
> +		return;
> +
> +	debug_printf("feature name [%s] command [%s]\n", feature, cmd);
> +	if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
> +		while (isst_help_cmds[i].feature) {
> +			if (!strcmp(isst_help_cmds[i].feature,
> feature)) {
> +				isst_help_cmds[i].process_fn();
> +				exit(0);
> +			}
> +			++i;
> +		}
> +	}
> +
> +	create_cpu_map();
> +
> +	i = 0;
> +	while (isst_cmds[i].feature) {
> +		if (!strcmp(isst_cmds[i].feature, feature) &&
> +		    !strcmp(isst_cmds[i].command, cmd)) {
> +			parse_cmd_args(argc, optind + 1, argv);
> +			isst_cmds[i].process_fn();
> +			matched = 1;
> +			break;
> +		}
> +		++i;
> +	}
> +
> +	if (!matched)
> +		fprintf(stderr, "Invalid command\n");
> +}
> +
> +static void usage(void)
> +{
> +	printf("Intel(R) Speed Select Technology\n");
> +	printf("\nUsage:\n");
> +	printf("intel-speed-select [OPTIONS] FEATURE COMMAND
> COMMAND_ARGUMENTS\n");
> +	printf("\nUse this tool to enumerate and control the Intel
> Speed Select Technology features,\n");
> +	printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-
> power]\n");
> +	printf("\nFor help on each feature, use --h|--help\n");
> +	printf("\tFor example:  intel-speed-select perf-profile -h\n");
> +
> +	printf("\nFor additional help on each command for a feature,
> use --h|--help\n");
> +	printf("\tFor example:  intel-speed-select perf-profile get-
> lock-status -h\n");
> +	printf("\t\t This will print help for the command \"get-lock-
> status\" for the feature \"perf-profile\"\n");
> +
> +	printf("\nOPTIONS\n");
> +	printf("\t[-c|--cpu] : logical cpu number\n");
> +	printf("\t\tDefault: Die scoped for all dies in the system with
> multiple dies/package\n");
> +	printf("\t\t\t Or Package scoped for all Packages when each
> package contains one die\n");
> +	printf("\t[-d|--debug] : Debug mode\n");
> +	printf("\t[-h|--help] : Print help\n");
> +	printf("\t[-i|--info] : Print platform information\n");
> +	printf("\t[-o|--out] : Output file\n");
> +	printf("\t\t\tDefault : stderr\n");
> +	printf("\t[-f|--format] : output format [json|text]. Default:
> text\n");
> +	printf("\t[-v|--version] : Print version\n");
> +
> +	printf("\nResult format\n");
> +	printf("\tResult display uses a common format for each
> command:\n");
> +	printf("\tResults are formatted in text/JSON with\n");
> +	printf("\t\tPackage, Die, CPU, and command specific
> results.\n");
> +	printf("\t\t\tFor Set commands, status is 0 for success and
> rest for failures\n");
> +	exit(1);
> +}
> +
> +static void print_version(void)
> +{
> +	fprintf(outf, "Version %s\n", version_str);
> +	fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
> +	exit(0);
> +}
> +
> +static void cmdline(int argc, char **argv)
> +{
> +	int opt;
> +	int option_index = 0;
> +
> +	static struct option long_options[] = {
> +		{ "cpu", required_argument, 0, 'c' },
> +		{ "debug", no_argument, 0, 'd' },
> +		{ "format", required_argument, 0, 'f' },
> +		{ "help", no_argument, 0, 'h' },
> +		{ "info", no_argument, 0, 'i' },
> +		{ "out", required_argument, 0, 'o' },
> +		{ "version", no_argument, 0, 'v' },
> +		{ 0, 0, 0, 0 }
> +	};
> +
> +	progname = argv[0];
> +	while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v",
> long_options,
> +				       &option_index)) != -1) {
> +		switch (opt) {
> +		case 'c':
> +			parse_cpu_command(optarg);
> +			break;
> +		case 'd':
> +			debug_flag = 1;
> +			printf("Debug Mode ON\n");
> +			break;
> +		case 'f':
> +			if (!strncmp(optarg, "json", 4))
> +				out_format_json = 1;
> +			break;
> +		case 'h':
> +			usage();
> +			break;
> +		case 'i':
> +			isst_print_platform_information();
> +			break;
> +		case 'o':
> +			if (outf)
> +				fclose(outf);
> +			outf = fopen_or_exit(optarg, "w");
> +			break;
> +		case 'v':
> +			print_version();
> +			break;
> +		default:
> +			usage();
> +		}
> +	}
> +
> +	if (geteuid() != 0) {
> +		fprintf(stderr, "Must run as root\n");
> +		exit(0);
> +	}
> +
> +	if (optind > (argc - 2)) {
> +		fprintf(stderr, "Feature name and|or command not
> specified\n");
> +		exit(0);
> +	}
> +	update_cpu_model();
> +	printf("Intel(R) Speed Select Technology\n");
> +	printf("Executing on CPU model:%d[0x%x]\n", cpu_model,
> cpu_model);
> +	set_max_cpu_num();
> +	set_cpu_present_cpu_mask();
> +	set_cpu_target_cpu_mask();
> +	isst_fill_platform_info();
> +	if (isst_platform_info.api_version > supported_api_ver) {
> +		printf("Incompatible API versions; Upgrade of tool is
> required\n");
> +		exit(0);
> +	}
> +
> +	process_command(argc, argv);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +	outf = stderr;
> +	cmdline(argc, argv);
> +	return 0;
> +}
> diff --git a/tools/power/x86/intel-speed-select/isst-core.c
> b/tools/power/x86/intel-speed-select/isst-core.c
> new file mode 100644
> index 000000000000..8de4ac39a008
> --- /dev/null
> +++ b/tools/power/x86/intel-speed-select/isst-core.c
> @@ -0,0 +1,721 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Intel Speed Select -- Enumerate and control features
> + * Copyright (c) 2019 Intel Corporation.
> + */
> +
> +#include "isst.h"
> +
> +int isst_get_ctdp_levels(int cpu, struct isst_pkg_ctdp *pkg_dev)
> +{
> +	unsigned int resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +				     CONFIG_TDP_GET_LEVELS_INFO, 0, 0,
> &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CONFIG_TDP_GET_LEVELS_INFO resp:%x\n",
> cpu, resp);
> +
> +	pkg_dev->version = resp & 0xff;
> +	pkg_dev->levels = (resp >> 8) & 0xff;
> +	pkg_dev->current_level = (resp >> 16) & 0xff;
> +	pkg_dev->locked = !!(resp & BIT(24));
> +	pkg_dev->enabled = !!(resp & BIT(31));
> +
> +	return 0;
> +}
> +
> +int isst_get_ctdp_control(int cpu, int config_index,
> +			  struct isst_pkg_ctdp_level_info *ctdp_level)
> +{
> +	unsigned int resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +				     CONFIG_TDP_GET_TDP_CONTROL, 0,
> +				     config_index, &resp);
> +	if (ret)
> +		return ret;
> +
> +	ctdp_level->fact_support = resp & BIT(0);
> +	ctdp_level->pbf_support = !!(resp & BIT(1));
> +	ctdp_level->fact_enabled = !!(resp & BIT(16));
> +	ctdp_level->pbf_enabled = !!(resp & BIT(17));
> +
> +	debug_printf(
> +		"cpu:%d CONFIG_TDP_GET_TDP_CONTROL resp:%x
> fact_support:%d pbf_support: %d fact_enabled:%d pbf_enabled:%d\n",
> +		cpu, resp, ctdp_level->fact_support, ctdp_level-
> >pbf_support,
> +		ctdp_level->fact_enabled, ctdp_level->pbf_enabled);
> +
> +	return 0;
> +}
> +
> +int isst_get_tdp_info(int cpu, int config_index,
> +		      struct isst_pkg_ctdp_level_info *ctdp_level)
> +{
> +	unsigned int resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> CONFIG_TDP_GET_TDP_INFO,
> +				     0, config_index, &resp);
> +	if (ret)
> +		return ret;
> +
> +	ctdp_level->pkg_tdp = resp & GENMASK(14, 0);
> +	ctdp_level->tdp_ratio = (resp & GENMASK(23, 16)) >> 16;
> +
> +	debug_printf(
> +		"cpu:%d ctdp:%d CONFIG_TDP_GET_TDP_INFO resp:%x
> tdp_ratio:%d pkg_tdp:%d\n",
> +		cpu, config_index, resp, ctdp_level->tdp_ratio,
> +		ctdp_level->pkg_tdp);
> +	return 0;
> +}
> +
> +int isst_get_pwr_info(int cpu, int config_index,
> +		      struct isst_pkg_ctdp_level_info *ctdp_level)
> +{
> +	unsigned int resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> CONFIG_TDP_GET_PWR_INFO,
> +				     0, config_index, &resp);
> +	if (ret)
> +		return ret;
> +
> +	ctdp_level->pkg_max_power = resp & GENMASK(14, 0);
> +	ctdp_level->pkg_min_power = (resp & GENMASK(30, 16)) >> 16;
> +
> +	debug_printf(
> +		"cpu:%d ctdp:%d CONFIG_TDP_GET_PWR_INFO resp:%x
> pkg_max_power:%d pkg_min_power:%d\n",
> +		cpu, config_index, resp, ctdp_level->pkg_max_power,
> +		ctdp_level->pkg_min_power);
> +
> +	return 0;
> +}
> +
> +int isst_get_tjmax_info(int cpu, int config_index,
> +			struct isst_pkg_ctdp_level_info *ctdp_level)
> +{
> +	unsigned int resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> CONFIG_TDP_GET_TJMAX_INFO,
> +				     0, config_index, &resp);
> +	if (ret)
> +		return ret;
> +
> +	ctdp_level->t_proc_hot = resp & GENMASK(7, 0);
> +
> +	debug_printf(
> +		"cpu:%d ctdp:%d CONFIG_TDP_GET_TJMAX_INFO resp:%x
> t_proc_hot:%d\n",
> +		cpu, config_index, resp, ctdp_level->t_proc_hot);
> +
> +	return 0;
> +}
> +
> +int isst_get_coremask_info(int cpu, int config_index,
> +			   struct isst_pkg_ctdp_level_info *ctdp_level)
> +{
> +	unsigned int resp;
> +	int i, ret;
> +
> +	ctdp_level->cpu_count = 0;
> +	for (i = 0; i < 2; ++i) {
> +		unsigned long long mask;
> +		int cpu_count = 0;
> +
> +		ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +					     CONFIG_TDP_GET_CORE_MASK,
> 0,
> +					     (i << 8) | config_index,
> &resp);
> +		if (ret)
> +			return ret;
> +
> +		debug_printf(
> +			"cpu:%d ctdp:%d mask:%d
> CONFIG_TDP_GET_CORE_MASK resp:%x\n",
> +			cpu, config_index, i, resp);
> +
> +		mask = (unsigned long long)resp << (32 * i);
> +		set_cpu_mask_from_punit_coremask(cpu, mask,
> +						 ctdp_level-
> >core_cpumask_size,
> +						 ctdp_level-
> >core_cpumask,
> +						 &cpu_count);
> +		ctdp_level->cpu_count += cpu_count;
> +		debug_printf("cpu:%d ctdp:%d mask:%d cpu count:%d\n",
> cpu,
> +			     config_index, i, ctdp_level->cpu_count);
> +	}
> +
> +	return 0;
> +}
> +
> +int isst_get_get_trl(int cpu, int level, int avx_level, int *trl)
> +{
> +	unsigned int req, resp;
> +	int ret;
> +
> +	req = level | (avx_level << 16);
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +				     CONFIG_TDP_GET_TURBO_LIMIT_RATIOS,
> 0, req,
> +				     &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf(
> +		"cpu:%d CONFIG_TDP_GET_TURBO_LIMIT_RATIOS req:%x
> resp:%x\n",
> +		cpu, req, resp);
> +
> +	trl[0] = resp & GENMASK(7, 0);
> +	trl[1] = (resp & GENMASK(15, 8)) >> 8;
> +	trl[2] = (resp & GENMASK(23, 16)) >> 16;
> +	trl[3] = (resp & GENMASK(31, 24)) >> 24;
> +
> +	req = level | BIT(8) | (avx_level << 16);
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +				     CONFIG_TDP_GET_TURBO_LIMIT_RATIOS,
> 0, req,
> +				     &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CONFIG_TDP_GET_TURBO_LIMIT req:%x
> resp:%x\n", cpu,
> +		     req, resp);
> +
> +	trl[4] = resp & GENMASK(7, 0);
> +	trl[5] = (resp & GENMASK(15, 8)) >> 8;
> +	trl[6] = (resp & GENMASK(23, 16)) >> 16;
> +	trl[7] = (resp & GENMASK(31, 24)) >> 24;
> +
> +	return 0;
> +}
> +
> +int isst_set_tdp_level_msr(int cpu, int tdp_level)
> +{
> +	int ret;
> +
> +	debug_printf("cpu: tdp_level via MSR %d\n", cpu, tdp_level);
> +
> +	if (isst_get_config_tdp_lock_status(cpu)) {
> +		debug_printf("cpu: tdp_locked %d\n", cpu);
> +		return -1;
> +	}
> +
> +	if (tdp_level > 2)
> +		return -1; /* invalid value */
> +
> +	ret = isst_send_msr_command(cpu, 0x64b, 1,
> +				    (unsigned long long *)&tdp_level);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu: tdp_level via MSR successful %d\n", cpu,
> tdp_level);
> +
> +	return 0;
> +}
> +
> +int isst_set_tdp_level(int cpu, int tdp_level)
> +{
> +	unsigned int resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> CONFIG_TDP_SET_LEVEL, 0,
> +				     tdp_level, &resp);
> +	if (ret)
> +		return isst_set_tdp_level_msr(cpu, tdp_level);
> +
> +	return 0;
> +}
> +
> +int isst_get_pbf_info(int cpu, int level, struct isst_pbf_info
> *pbf_info)
> +{
> +	unsigned int req, resp;
> +	int i, ret;
> +
> +	pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info-
> >core_cpumask);
> +
> +	for (i = 0; i < 2; ++i) {
> +		unsigned long long mask;
> +		int count;
> +
> +		ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +					     CONFIG_TDP_PBF_GET_CORE_MA
> SK_INFO,
> +					     0, (i << 8) | level,
> &resp);
> +		if (ret)
> +			return ret;
> +
> +		debug_printf(
> +			"cpu:%d CONFIG_TDP_PBF_GET_CORE_MASK_INFO
> resp:%x\n",
> +			cpu, resp);
> +
> +		mask = (unsigned long long)resp << (32 * i);
> +		set_cpu_mask_from_punit_coremask(cpu, mask,
> +						 pbf_info-
> >core_cpumask_size,
> +						 pbf_info-
> >core_cpumask,
> +						 &count);
> +	}
> +
> +	req = level;
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +				     CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO,
> 0, req,
> +				     &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO
> resp:%x\n", cpu,
> +		     resp);
> +
> +	pbf_info->p1_low = resp & 0xff;
> +	pbf_info->p1_high = (resp & GENMASK(15, 8)) >> 8;
> +
> +	req = level;
> +	ret = isst_send_mbox_command(
> +		cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TDP_INFO, 0, req,
> &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TDP_INFO resp:%x\n",
> cpu, resp);
> +
> +	pbf_info->tdp = resp & 0xffff;
> +
> +	req = level;
> +	ret = isst_send_mbox_command(
> +		cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TJ_MAX_INFO, 0,
> req, &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TJ_MAX_INFO resp:%x\n",
> cpu,
> +		     resp);
> +	pbf_info->t_control = (resp >> 8) & 0xff;
> +	pbf_info->t_prochot = resp & 0xff;
> +
> +	return 0;
> +}
> +
> +void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info)
> +{
> +	free_cpu_set(pbf_info->core_cpumask);
> +}
> +
> +int isst_set_pbf_fact_status(int cpu, int pbf, int enable)
> +{
> +	struct isst_pkg_ctdp pkg_dev;
> +	struct isst_pkg_ctdp_level_info ctdp_level;
> +	int current_level;
> +	unsigned int req = 0, resp;
> +	int ret;
> +
> +	ret = isst_get_ctdp_levels(cpu, &pkg_dev);
> +	if (ret)
> +		return ret;
> +
> +	current_level = pkg_dev.current_level;
> +
> +	ret = isst_get_ctdp_control(cpu, current_level, &ctdp_level);
> +	if (ret)
> +		return ret;
> +
> +	if (pbf) {
> +		if (ctdp_level.fact_enabled)
> +			req = BIT(16);
> +
> +		if (enable)
> +			req |= BIT(17);
> +		else
> +			req &= ~BIT(17);
> +	} else {
> +		if (ctdp_level.pbf_enabled)
> +			req = BIT(17);
> +
> +		if (enable)
> +			req |= BIT(16);
> +		else
> +			req &= ~BIT(16);
> +	}
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +				     CONFIG_TDP_SET_TDP_CONTROL, 0,
> req, &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CONFIG_TDP_SET_TDP_CONTROL pbf/fact:%d
> req:%x\n",
> +		     cpu, pbf, req);
> +
> +	return 0;
> +}
> +
> +int isst_get_fact_bucket_info(int cpu, int level,
> +			      struct isst_fact_bucket_info
> *bucket_info)
> +{
> +	unsigned int resp;
> +	int i, k, ret;
> +
> +	for (i = 0; i < 2; ++i) {
> +		int j;
> +
> +		ret = isst_send_mbox_command(
> +			cpu, CONFIG_TDP,
> +			CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES, 0,
> +			(i << 8) | level, &resp);
> +		if (ret)
> +			return ret;
> +
> +		debug_printf(
> +			"cpu:%d
> CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES index:%d level:%d
> resp:%x\n",
> +			cpu, i, level, resp);
> +
> +		for (j = 0; j < 4; ++j) {
> +			bucket_info[j + (i *
> 4)].high_priority_cores_count =
> +				(resp >> (j * 8)) & 0xff;
> +		}
> +	}
> +
> +	for (k = 0; k < 3; ++k) {
> +		for (i = 0; i < 2; ++i) {
> +			int j;
> +
> +			ret = isst_send_mbox_command(
> +				cpu, CONFIG_TDP,
> +				CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATI
> OS, 0,
> +				(k << 16) | (i << 8) | level, &resp);
> +			if (ret)
> +				return ret;
> +
> +			debug_printf(
> +				"cpu:%d
> CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS index:%d level:%d avx:%d
> resp:%x\n",
> +				cpu, i, level, k, resp);
> +
> +			for (j = 0; j < 4; ++j) {
> +				switch (k) {
> +				case 0:
> +					bucket_info[j + (i *
> 4)].sse_trl =
> +						(resp >> (j * 8)) &
> 0xff;
> +					break;
> +				case 1:
> +					bucket_info[j + (i *
> 4)].avx_trl =
> +						(resp >> (j * 8)) &
> 0xff;
> +					break;
> +				case 2:
> +					bucket_info[j + (i *
> 4)].avx512_trl =
> +						(resp >> (j * 8)) &
> 0xff;
> +					break;
> +				default:
> +					break;
> +				}
> +			}
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +int isst_get_fact_info(int cpu, int level, struct isst_fact_info
> *fact_info)
> +{
> +	unsigned int resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_TDP,
> +				     CONFIG_TDP_GET_FACT_LP_CLIPPING_RA
> TIO, 0,
> +				     level, &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO
> resp:%x\n",
> +		     cpu, resp);
> +
> +	fact_info->lp_clipping_ratio_license_sse = resp & 0xff;
> +	fact_info->lp_clipping_ratio_license_avx2 = (resp >> 8) & 0xff;
> +	fact_info->lp_clipping_ratio_license_avx512 = (resp >> 16) &
> 0xff;
> +
> +	ret = isst_get_fact_bucket_info(cpu, level, fact_info-
> >bucket_info);
> +
> +	return ret;
> +}
> +
> +int isst_set_trl(int cpu, unsigned long long trl)
> +{
> +	int ret;
> +
> +	if (!trl)
> +		trl = 0xFFFFFFFFFFFFFFFFULL;
> +
> +	ret = isst_send_msr_command(cpu, 0x1AD, 1, &trl);
> +	if (ret)
> +		return ret;
> +
> +	return 0;
> +}
> +
> +int isst_set_trl_from_current_tdp(int cpu, unsigned long long trl)
> +{
> +	unsigned long long msr_trl;
> +	int ret;
> +
> +	if (trl) {
> +		msr_trl = trl;
> +	} else {
> +		struct isst_pkg_ctdp pkg_dev;
> +		int trl[8];
> +		int i;
> +
> +		ret = isst_get_ctdp_levels(cpu, &pkg_dev);
> +		if (ret)
> +			return ret;
> +
> +		ret = isst_get_get_trl(cpu, pkg_dev.current_level, 0,
> trl);
> +		if (ret)
> +			return ret;
> +
> +		msr_trl = 0;
> +		for (i = 0; i < 8; ++i) {
> +			unsigned long long _trl = trl[i];
> +
> +			msr_trl |= (_trl << (i * 8));
> +		}
> +	}
> +	ret = isst_send_msr_command(cpu, 0x1AD, 1, &msr_trl);
> +	if (ret)
> +		return ret;
> +
> +	return 0;
> +}
> +
> +/* Return 1 if locked */
> +int isst_get_config_tdp_lock_status(int cpu)
> +{
> +	unsigned long long tdp_control = 0;
> +	int ret;
> +
> +	ret = isst_send_msr_command(cpu, 0x64b, 0, &tdp_control);
> +	if (ret)
> +		return ret;
> +
> +	ret = !!(tdp_control & BIT(31));
> +
> +	return ret;
> +}
> +
> +void isst_get_process_ctdp_complete(int cpu, struct isst_pkg_ctdp
> *pkg_dev)
> +{
> +	int i;
> +
> +	if (!pkg_dev->processed)
> +		return;
> +
> +	for (i = 0; i < pkg_dev->levels; ++i) {
> +		struct isst_pkg_ctdp_level_info *ctdp_level;
> +
> +		ctdp_level = &pkg_dev->ctdp_level[i];
> +		if (ctdp_level->pbf_support)
> +			free_cpu_set(ctdp_level-
> >pbf_info.core_cpumask);
> +		free_cpu_set(ctdp_level->core_cpumask);
> +	}
> +}
> +
> +int isst_get_process_ctdp(int cpu, int tdp_level, struct
> isst_pkg_ctdp *pkg_dev)
> +{
> +	int i, ret;
> +
> +	if (pkg_dev->processed)
> +		return 0;
> +
> +	ret = isst_get_ctdp_levels(cpu, pkg_dev);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu: %d ctdp enable:%d current level: %d
> levels:%d\n",
> +		     cpu, pkg_dev->enabled, pkg_dev->current_level,
> +		     pkg_dev->levels);
> +
> +	for (i = 0; i <= pkg_dev->levels; ++i) {
> +		struct isst_pkg_ctdp_level_info *ctdp_level;
> +
> +		if (tdp_level != 0xff && i != tdp_level)
> +			continue;
> +
> +		debug_printf("cpu:%d Get Information for TDP
> level:%d\n", cpu,
> +			     i);
> +		ctdp_level = &pkg_dev->ctdp_level[i];
> +
> +		ctdp_level->processed = 1;
> +		ctdp_level->level = i;
> +		ctdp_level->control_cpu = cpu;
> +		ctdp_level->pkg_id = get_physical_package_id(cpu);
> +		ctdp_level->die_id = get_physical_die_id(cpu);
> +
> +		ret = isst_get_ctdp_control(cpu, i, ctdp_level);
> +		if (ret)
> +			return ret;
> +
> +		ret = isst_get_tdp_info(cpu, i, ctdp_level);
> +		if (ret)
> +			return ret;
> +
> +		ret = isst_get_pwr_info(cpu, i, ctdp_level);
> +		if (ret)
> +			return ret;
> +
> +		ret = isst_get_tjmax_info(cpu, i, ctdp_level);
> +		if (ret)
> +			return ret;
> +
> +		ctdp_level->core_cpumask_size =
> +			alloc_cpu_set(&ctdp_level->core_cpumask);
> +		ret = isst_get_coremask_info(cpu, i, ctdp_level);
> +		if (ret)
> +			return ret;
> +
> +		ret = isst_get_get_trl(cpu, i, 0,
> +				       ctdp_level-
> >trl_sse_active_cores);
> +		if (ret)
> +			return ret;
> +
> +		ret = isst_get_get_trl(cpu, i, 1,
> +				       ctdp_level-
> >trl_avx_active_cores);
> +		if (ret)
> +			return ret;
> +
> +		ret = isst_get_get_trl(cpu, i, 2,
> +				       ctdp_level-
> >trl_avx_512_active_cores);
> +		if (ret)
> +			return ret;
> +
> +		if (ctdp_level->pbf_support) {
> +			ret = isst_get_pbf_info(cpu, i, &ctdp_level-
> >pbf_info);
> +			if (!ret)
> +				ctdp_level->pbf_found = 1;
> +		}
> +
> +		if (ctdp_level->fact_support) {
> +			ret = isst_get_fact_info(cpu, i,
> +						 &ctdp_level-
> >fact_info);
> +			if (ret)
> +				return ret;
> +		}
> +	}
> +
> +	pkg_dev->processed = 1;
> +
> +	return 0;
> +}
> +
> +int isst_pm_qos_config(int cpu, int enable_clos, int priority_type)
> +{
> +	unsigned int req, resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_CLOS,
> CLOS_PM_QOS_CONFIG, 0, 0,
> +				     &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", cpu, resp);
> +
> +	req = resp;
> +
> +	if (enable_clos)
> +		req = req | BIT(1);
> +	else
> +		req = req & ~BIT(1);
> +
> +	if (priority_type)
> +		req = req | BIT(2);
> +	else
> +		req = req & ~BIT(2);
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_CLOS,
> CLOS_PM_QOS_CONFIG,
> +				     BIT(MBOX_CMD_WRITE_BIT), req,
> &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CLOS_PM_QOS_CONFIG priority type:%d
> req:%x\n", cpu,
> +		     priority_type, req);
> +
> +	return 0;
> +}
> +
> +int isst_pm_get_clos(int cpu, int clos, struct isst_clos_config
> *clos_config)
> +{
> +	unsigned int resp;
> +	int ret;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PM_CLOS,
> clos, 0,
> +				     &resp);
> +	if (ret)
> +		return ret;
> +
> +	clos_config->pkg_id = get_physical_package_id(cpu);
> +	clos_config->die_id = get_physical_die_id(cpu);
> +
> +	clos_config->epp = resp & 0x0f;
> +	clos_config->clos_prop_prio = (resp >> 4) & 0x0f;
> +	clos_config->clos_min = (resp >> 8) & 0xff;
> +	clos_config->clos_max = (resp >> 16) & 0xff;
> +	clos_config->clos_desired = (resp >> 24) & 0xff;
> +
> +	return 0;
> +}
> +
> +int isst_set_clos(int cpu, int clos, struct isst_clos_config
> *clos_config)
> +{
> +	unsigned int req, resp;
> +	unsigned int param;
> +	int ret;
> +
> +	req = clos_config->epp & 0x0f;
> +	req |= (clos_config->clos_prop_prio & 0x0f) << 4;
> +	req |= (clos_config->clos_min & 0xff) << 8;
> +	req |= (clos_config->clos_max & 0xff) << 16;
> +	req |= (clos_config->clos_desired & 0xff) << 24;
> +
> +	param = BIT(MBOX_CMD_WRITE_BIT) | clos;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PM_CLOS,
> param, req,
> +				     &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CLOS_PM_CLOS param:%x req:%x\n", cpu,
> param, req);
> +
> +	return 0;
> +}
> +
> +int isst_clos_get_assoc_status(int cpu, int *clos_id)
> +{
> +	unsigned int resp;
> +	unsigned int param;
> +	int core_id, ret;
> +
> +	core_id = find_phy_core_num(cpu);
> +	param = core_id;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PQR_ASSOC,
> param, 0,
> +				     &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x resp:%x\n", cpu,
> param,
> +		     resp);
> +	*clos_id = (resp >> 16) & 0x03;
> +
> +	return 0;
> +}
> +
> +int isst_clos_associate(int cpu, int clos_id)
> +{
> +	unsigned int req, resp;
> +	unsigned int param;
> +	int core_id, ret;
> +
> +	req = (clos_id & 0x03) << 16;
> +	core_id = find_phy_core_num(cpu);
> +	param = BIT(MBOX_CMD_WRITE_BIT) | core_id;
> +
> +	ret = isst_send_mbox_command(cpu, CONFIG_CLOS, CLOS_PQR_ASSOC,
> param,
> +				     req, &resp);
> +	if (ret)
> +		return ret;
> +
> +	debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x req:%x\n", cpu,
> param,
> +		     req);
> +
> +	return 0;
> +}
> diff --git a/tools/power/x86/intel-speed-select/isst-display.c
> b/tools/power/x86/intel-speed-select/isst-display.c
> new file mode 100644
> index 000000000000..f368b8323742
> --- /dev/null
> +++ b/tools/power/x86/intel-speed-select/isst-display.c
> @@ -0,0 +1,479 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Intel dynamic_speed_select -- Enumerate and control features
> + * Copyright (c) 2019 Intel Corporation.
> + */
> +
> +#include "isst.h"
> +
> +#define DISP_FREQ_MULTIPLIER 100000
> +
> +static void printcpumask(int str_len, char *str, int mask_size,
> +			 cpu_set_t *cpu_mask)
> +{
> +	int i, max_cpus = get_topo_max_cpus();
> +	unsigned int *mask;
> +	int size, index, curr_index;
> +
> +	size = max_cpus / (sizeof(unsigned int) * 8);
> +	if (max_cpus % (sizeof(unsigned int) * 8))
> +		size++;
> +
> +	mask = calloc(size, sizeof(unsigned int));
> +	if (!mask)
> +		return;
> +
> +	for (i = 0; i < max_cpus; ++i) {
> +		int mask_index, bit_index;
> +
> +		if (!CPU_ISSET_S(i, mask_size, cpu_mask))
> +			continue;
> +
> +		mask_index = i / (sizeof(unsigned int) * 8);
> +		bit_index = i % (sizeof(unsigned int) * 8);
> +		mask[mask_index] |= BIT(bit_index);
> +	}
> +
> +	curr_index = 0;
> +	for (i = size - 1; i >= 0; --i) {
> +		index = snprintf(&str[curr_index], str_len -
> curr_index, "%08x",
> +				 mask[i]);
> +		curr_index += index;
> +		if (i) {
> +			strncat(&str[curr_index], ",", str_len -
> curr_index);
> +			curr_index++;
> +		}
> +	}
> +
> +	free(mask);
> +}
> +
> +static void format_and_print_txt(FILE *outf, int level, char
> *header,
> +				 char *value)
> +{
> +	char *spaces = "  ";
> +	static char delimiters[256];
> +	int i, j = 0;
> +
> +	if (!level)
> +		return;
> +
> +	if (level == 1) {
> +		strcpy(delimiters, " ");
> +	} else {
> +		for (i = 0; i < level - 1; ++i)
> +			j += snprintf(&delimiters[j],
> sizeof(delimiters) - j,
> +				      "%s", spaces);
> +	}
> +
> +	if (header && value) {
> +		fprintf(outf, "%s", delimiters);
> +		fprintf(outf, "%s:%s\n", header, value);
> +	} else if (header) {
> +		fprintf(outf, "%s", delimiters);
> +		fprintf(outf, "%s\n", header);
> +	}
> +}
> +
> +static int last_level;
> +static void format_and_print(FILE *outf, int level, char *header,
> char *value)
> +{
> +	char *spaces = "  ";
> +	static char delimiters[256];
> +	int i;
> +
> +	if (!out_format_is_json()) {
> +		format_and_print_txt(outf, level, header, value);
> +		return;
> +	}
> +
> +	if (level == 0) {
> +		if (header)
> +			fprintf(outf, "{");
> +		else
> +			fprintf(outf, "\n}\n");
> +
> +	} else {
> +		int j = 0;
> +
> +		for (i = 0; i < level; ++i)
> +			j += snprintf(&delimiters[j],
> sizeof(delimiters) - j,
> +				      "%s", spaces);
> +
> +		if (last_level == level)
> +			fprintf(outf, ",\n");
> +
> +		if (value) {
> +			if (last_level != level)
> +				fprintf(outf, "\n");
> +
> +			fprintf(outf, "%s\"%s\": ", delimiters,
> header);
> +			fprintf(outf, "\"%s\"", value);
> +		} else {
> +			for (i = last_level - 1; i >= level; --i) {
> +				int k = 0;
> +
> +				for (j = i; j > 0; --j)
> +					k += snprintf(&delimiters[k],
> +						      sizeof(delimiters
> ) - k,
> +						      "%s", spaces);
> +				if (i == level && header)
> +					fprintf(outf, "\n%s},",
> delimiters);
> +				else
> +					fprintf(outf, "\n%s}",
> delimiters);
> +			}
> +			if (abs(last_level - level) < 3)
> +				fprintf(outf, "\n");
> +			if (header)
> +				fprintf(outf, "%s\"%s\": {",
> delimiters,
> +					header);
> +		}
> +	}
> +
> +	last_level = level;
> +}
> +
> +static void print_packag_info(int cpu, FILE *outf)
> +{
> +	char header[256];
> +
> +	snprintf(header, sizeof(header), "package-%d",
> +		 get_physical_package_id(cpu));
> +	format_and_print(outf, 1, header, NULL);
> +	snprintf(header, sizeof(header), "die-%d",
> get_physical_die_id(cpu));
> +	format_and_print(outf, 2, header, NULL);
> +	snprintf(header, sizeof(header), "cpu-%d", cpu);
> +	format_and_print(outf, 3, header, NULL);
> +}
> +
> +static void _isst_pbf_display_information(int cpu, FILE *outf, int
> level,
> +					  struct isst_pbf_info
> *pbf_info,
> +					  int disp_level)
> +{
> +	char header[256];
> +	char value[256];
> +
> +	snprintf(header, sizeof(header), "speed-select-base-freq");
> +	format_and_print(outf, disp_level, header, NULL);
> +
> +	snprintf(header, sizeof(header), "high-priority-base-
> frequency(KHz)");
> +	snprintf(value, sizeof(value), "%d",
> +		 pbf_info->p1_high * DISP_FREQ_MULTIPLIER);
> +	format_and_print(outf, disp_level + 1, header, value);
> +
> +	snprintf(header, sizeof(header), "high-priority-cpu-mask");
> +	printcpumask(sizeof(value), value, pbf_info->core_cpumask_size,
> +		     pbf_info->core_cpumask);
> +	format_and_print(outf, disp_level + 1, header, value);
> +
> +	snprintf(header, sizeof(header), "low-priority-base-
> frequency(KHz)");
> +	snprintf(value, sizeof(value), "%d",
> +		 pbf_info->p1_low * DISP_FREQ_MULTIPLIER);
> +	format_and_print(outf, disp_level + 1, header, value);
> +
> +	snprintf(header, sizeof(header), "tjunction-temperature(C)");
> +	snprintf(value, sizeof(value), "%d", pbf_info->t_prochot);
> +	format_and_print(outf, disp_level + 1, header, value);
> +
> +	snprintf(header, sizeof(header), "thermal-design-power(W)");
> +	snprintf(value, sizeof(value), "%d", pbf_info->tdp);
> +	format_and_print(outf, disp_level + 1, header, value);
> +}
> +
> +static void _isst_fact_display_information(int cpu, FILE *outf, int
> level,
> +					   int fact_bucket, int
> fact_avx,
> +					   struct isst_fact_info
> *fact_info,
> +					   int base_level)
> +{
> +	struct isst_fact_bucket_info *bucket_info = fact_info-
> >bucket_info;
> +	char header[256];
> +	char value[256];
> +	int j;
> +
> +	snprintf(header, sizeof(header), "speed-select-turbo-freq");
> +	format_and_print(outf, base_level, header, NULL);
> +	for (j = 0; j < ISST_FACT_MAX_BUCKETS; ++j) {
> +		if (fact_bucket != 0xff && fact_bucket != j)
> +			continue;
> +
> +		if (!bucket_info[j].high_priority_cores_count)
> +			break;
> +
> +		snprintf(header, sizeof(header), "bucket-%d", j);
> +		format_and_print(outf, base_level + 1, header, NULL);
> +
> +		snprintf(header, sizeof(header), "high-priority-cores-
> count");
> +		snprintf(value, sizeof(value), "%d",
> +			 bucket_info[j].high_priority_cores_count);
> +		format_and_print(outf, base_level + 2, header, value);
> +
> +		if (fact_avx & 0x01) {
> +			snprintf(header, sizeof(header),
> +				 "high-priority-max-frequency(KHz)");
> +			snprintf(value, sizeof(value), "%d",
> +				 bucket_info[j].sse_trl *
> DISP_FREQ_MULTIPLIER);
> +			format_and_print(outf, base_level + 2, header,
> value);
> +		}
> +
> +		if (fact_avx & 0x02) {
> +			snprintf(header, sizeof(header),
> +				 "high-priority-max-avx2-
> frequency(KHz)");
> +			snprintf(value, sizeof(value), "%d",
> +				 bucket_info[j].avx_trl *
> DISP_FREQ_MULTIPLIER);
> +			format_and_print(outf, base_level + 2, header,
> value);
> +		}
> +
> +		if (fact_avx & 0x04) {
> +			snprintf(header, sizeof(header),
> +				 "high-priority-max-avx512-
> frequency(KHz)");
> +			snprintf(value, sizeof(value), "%d",
> +				 bucket_info[j].avx512_trl *
> +					 DISP_FREQ_MULTIPLIER);
> +			format_and_print(outf, base_level + 2, header,
> value);
> +		}
> +	}
> +	snprintf(header, sizeof(header),
> +		 "speed-select-turbo-freq-clip-frequencies");
> +	format_and_print(outf, base_level + 1, header, NULL);
> +	snprintf(header, sizeof(header), "low-priority-max-
> frequency(KHz)");
> +	snprintf(value, sizeof(value), "%d",
> +		 fact_info->lp_clipping_ratio_license_sse *
> +			 DISP_FREQ_MULTIPLIER);
> +	format_and_print(outf, base_level + 2, header, value);
> +	snprintf(header, sizeof(header),
> +		 "low-priority-max-avx2-frequency(KHz)");
> +	snprintf(value, sizeof(value), "%d",
> +		 fact_info->lp_clipping_ratio_license_avx2 *
> +			 DISP_FREQ_MULTIPLIER);
> +	format_and_print(outf, base_level + 2, header, value);
> +	snprintf(header, sizeof(header),
> +		 "low-priority-max-avx512-frequency(KHz)");
> +	snprintf(value, sizeof(value), "%d",
> +		 fact_info->lp_clipping_ratio_license_avx512 *
> +			 DISP_FREQ_MULTIPLIER);
> +	format_and_print(outf, base_level + 2, header, value);
> +}
> +
> +void isst_ctdp_display_information(int cpu, FILE *outf, int
> tdp_level,
> +				   struct isst_pkg_ctdp *pkg_dev)
> +{
> +	char header[256];
> +	char value[256];
> +	int i, base_level = 1;
> +
> +	print_packag_info(cpu, outf);
> +
> +	for (i = 0; i <= pkg_dev->levels; ++i) {
> +		struct isst_pkg_ctdp_level_info *ctdp_level;
> +		int j;
> +
> +		ctdp_level = &pkg_dev->ctdp_level[i];
> +		if (!ctdp_level->processed)
> +			continue;
> +
> +		snprintf(header, sizeof(header), "perf-profile-level-
> %d",
> +			 ctdp_level->level);
> +		format_and_print(outf, base_level + 3, header, NULL);
> +
> +		snprintf(header, sizeof(header), "cpu-count");
> +		j = get_cpu_count(get_physical_die_id(cpu),
> +				  get_physical_die_id(cpu));
> +		snprintf(value, sizeof(value), "%d", j);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header), "enable-cpu-mask");
> +		printcpumask(sizeof(value), value,
> +			     ctdp_level->core_cpumask_size,
> +			     ctdp_level->core_cpumask);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header), "thermal-design-power-
> ratio");
> +		snprintf(value, sizeof(value), "%d", ctdp_level-
> >tdp_ratio);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header), "base-
> frequency(KHz)");
> +		snprintf(value, sizeof(value), "%d",
> +			 ctdp_level->tdp_ratio * DISP_FREQ_MULTIPLIER);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header),
> +			 "speed-select-turbo-freq-support");
> +		snprintf(value, sizeof(value), "%d", ctdp_level-
> >fact_support);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header),
> +			 "speed-select-base-freq-support");
> +		snprintf(value, sizeof(value), "%d", ctdp_level-
> >pbf_support);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header),
> +			 "speed-select-base-freq-enabled");
> +		snprintf(value, sizeof(value), "%d", ctdp_level-
> >pbf_enabled);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header),
> +			 "speed-select-turbo-freq-enabled");
> +		snprintf(value, sizeof(value), "%d", ctdp_level-
> >fact_enabled);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header), "thermal-design-
> power(W)");
> +		snprintf(value, sizeof(value), "%d", ctdp_level-
> >pkg_tdp);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header), "tjunction-max(C)");
> +		snprintf(value, sizeof(value), "%d", ctdp_level-
> >t_proc_hot);
> +		format_and_print(outf, base_level + 4, header, value);
> +
> +		snprintf(header, sizeof(header), "turbo-ratio-limits-
> sse");
> +		format_and_print(outf, base_level + 4, header, NULL);
> +		for (j = 0; j < 8; ++j) {
> +			snprintf(header, sizeof(header), "bucket-%d",
> j);
> +			format_and_print(outf, base_level + 5, header,
> NULL);
> +
> +			snprintf(header, sizeof(header), "core-count");
> +			snprintf(value, sizeof(value), "%d", j);
> +			format_and_print(outf, base_level + 6, header,
> value);
> +
> +			snprintf(header, sizeof(header), "turbo-
> ratio");
> +			snprintf(value, sizeof(value), "%d",
> +				 ctdp_level->trl_sse_active_cores[j]);
> +			format_and_print(outf, base_level + 6, header,
> value);
> +		}
> +		snprintf(header, sizeof(header), "turbo-ratio-limits-
> avx");
> +		format_and_print(outf, base_level + 4, header, NULL);
> +		for (j = 0; j < 8; ++j) {
> +			snprintf(header, sizeof(header), "bucket-%d",
> j);
> +			format_and_print(outf, base_level + 5, header,
> NULL);
> +
> +			snprintf(header, sizeof(header), "core-count");
> +			snprintf(value, sizeof(value), "%d", j);
> +			format_and_print(outf, base_level + 6, header,
> value);
> +
> +			snprintf(header, sizeof(header), "turbo-
> ratio");
> +			snprintf(value, sizeof(value), "%d",
> +				 ctdp_level->trl_avx_active_cores[j]);
> +			format_and_print(outf, base_level + 6, header,
> value);
> +		}
> +
> +		snprintf(header, sizeof(header), "turbo-ratio-limits-
> avx512");
> +		format_and_print(outf, base_level + 4, header, NULL);
> +		for (j = 0; j < 8; ++j) {
> +			snprintf(header, sizeof(header), "bucket-%d",
> j);
> +			format_and_print(outf, base_level + 5, header,
> NULL);
> +
> +			snprintf(header, sizeof(header), "core-count");
> +			snprintf(value, sizeof(value), "%d", j);
> +			format_and_print(outf, base_level + 6, header,
> value);
> +
> +			snprintf(header, sizeof(header), "turbo-
> ratio");
> +			snprintf(value, sizeof(value), "%d",
> +				 ctdp_level-
> >trl_avx_512_active_cores[j]);
> +			format_and_print(outf, base_level + 6, header,
> value);
> +		}
> +		if (ctdp_level->pbf_support)
> +			_isst_pbf_display_information(cpu, outf, i,
> +						      &ctdp_level-
> >pbf_info,
> +						      base_level + 4);
> +		if (ctdp_level->fact_support)
> +			_isst_fact_display_information(cpu, outf, i,
> 0xff, 0xff,
> +						       &ctdp_level-
> >fact_info,
> +						       base_level + 4);
> +	}
> +
> +	format_and_print(outf, 1, NULL, NULL);
> +}
> +
> +void isst_ctdp_display_information_start(FILE *outf)
> +{
> +	last_level = 0;
> +	format_and_print(outf, 0, "start", NULL);
> +}
> +
> +void isst_ctdp_display_information_end(FILE *outf)
> +{
> +	format_and_print(outf, 0, NULL, NULL);
> +}
> +
> +void isst_pbf_display_information(int cpu, FILE *outf, int level,
> +				  struct isst_pbf_info *pbf_info)
> +{
> +	print_packag_info(cpu, outf);
> +	_isst_pbf_display_information(cpu, outf, level, pbf_info, 4);
> +	format_and_print(outf, 1, NULL, NULL);
> +}
> +
> +void isst_fact_display_information(int cpu, FILE *outf, int level,
> +				   int fact_bucket, int fact_avx,
> +				   struct isst_fact_info *fact_info)
> +{
> +	print_packag_info(cpu, outf);
> +	_isst_fact_display_information(cpu, outf, level, fact_bucket,
> fact_avx,
> +				       fact_info, 4);
> +	format_and_print(outf, 1, NULL, NULL);
> +}
> +
> +void isst_clos_display_information(int cpu, FILE *outf, int clos,
> +				   struct isst_clos_config
> *clos_config)
> +{
> +	char header[256];
> +	char value[256];
> +
> +	snprintf(header, sizeof(header), "package-%d",
> +		 get_physical_package_id(cpu));
> +	format_and_print(outf, 1, header, NULL);
> +	snprintf(header, sizeof(header), "die-%d",
> get_physical_die_id(cpu));
> +	format_and_print(outf, 2, header, NULL);
> +	snprintf(header, sizeof(header), "cpu-%d", cpu);
> +	format_and_print(outf, 3, header, NULL);
> +
> +	snprintf(header, sizeof(header), "core-power");
> +	format_and_print(outf, 4, header, NULL);
> +
> +	snprintf(header, sizeof(header), "clos");
> +	snprintf(value, sizeof(value), "%d", clos);
> +	format_and_print(outf, 5, header, value);
> +
> +	snprintf(header, sizeof(header), "epp");
> +	snprintf(value, sizeof(value), "%d", clos_config->epp);
> +	format_and_print(outf, 5, header, value);
> +
> +	snprintf(header, sizeof(header), "clos-proportional-priority");
> +	snprintf(value, sizeof(value), "%d", clos_config-
> >clos_prop_prio);
> +	format_and_print(outf, 5, header, value);
> +
> +	snprintf(header, sizeof(header), "clos-min");
> +	snprintf(value, sizeof(value), "%d", clos_config->clos_min);
> +	format_and_print(outf, 5, header, value);
> +
> +	snprintf(header, sizeof(header), "clos-max");
> +	snprintf(value, sizeof(value), "%d", clos_config->clos_max);
> +	format_and_print(outf, 5, header, value);
> +
> +	snprintf(header, sizeof(header), "clos-desired");
> +	snprintf(value, sizeof(value), "%d", clos_config-
> >clos_desired);
> +	format_and_print(outf, 5, header, value);
> +
> +	format_and_print(outf, 1, NULL, NULL);
> +}
> +
> +void isst_display_result(int cpu, FILE *outf, char *feature, char
> *cmd,
> +			 int result)
> +{
> +	char header[256];
> +	char value[256];
> +
> +	snprintf(header, sizeof(header), "package-%d",
> +		 get_physical_package_id(cpu));
> +	format_and_print(outf, 1, header, NULL);
> +	snprintf(header, sizeof(header), "die-%d",
> get_physical_die_id(cpu));
> +	format_and_print(outf, 2, header, NULL);
> +	snprintf(header, sizeof(header), "cpu-%d", cpu);
> +	format_and_print(outf, 3, header, NULL);
> +	snprintf(header, sizeof(header), "%s", feature);
> +	format_and_print(outf, 4, header, NULL);
> +	snprintf(header, sizeof(header), "%s", cmd);
> +	snprintf(value, sizeof(value), "%d", result);
> +	format_and_print(outf, 5, header, value);
> +
> +	format_and_print(outf, 1, NULL, NULL);
> +}
> diff --git a/tools/power/x86/intel-speed-select/isst.h
> b/tools/power/x86/intel-speed-select/isst.h
> new file mode 100644
> index 000000000000..221881761609
> --- /dev/null
> +++ b/tools/power/x86/intel-speed-select/isst.h
> @@ -0,0 +1,231 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Intel Speed Select -- Enumerate and control features
> + * Copyright (c) 2019 Intel Corporation.
> + */
> +
> +#ifndef _ISST_H_
> +#define _ISST_H_
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <sys/types.h>
> +#include <sched.h>
> +#include <sys/stat.h>
> +#include <sys/resource.h>
> +#include <getopt.h>
> +#include <err.h>
> +#include <fcntl.h>
> +#include <signal.h>
> +#include <sys/time.h>
> +#include <limits.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <cpuid.h>
> +#include <dirent.h>
> +#include <errno.h>
> +
> +#include <stdarg.h>
> +#include <sys/ioctl.h>
> +
> +#define BIT(x) (1 << (x))
> +#define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (sizeof(long) * 8
> - 1 - (h))))
> +#define GENMASK_ULL(h,
> l)                                                      \
> +	(((~0ULL) << (l)) & (~0ULL >> (sizeof(long long) * 8 - 1 -
> (h))))
> +
> +#define CONFIG_TDP				0x7f
> +#define CONFIG_TDP_GET_LEVELS_INFO		0x00
> +#define CONFIG_TDP_GET_TDP_CONTROL		0x01
> +#define CONFIG_TDP_SET_TDP_CONTROL		0x02
> +#define CONFIG_TDP_GET_TDP_INFO			0x03
> +#define CONFIG_TDP_GET_PWR_INFO			0x04
> +#define CONFIG_TDP_GET_TJMAX_INFO		0x05
> +#define CONFIG_TDP_GET_CORE_MASK		0x06
> +#define CONFIG_TDP_GET_TURBO_LIMIT_RATIOS	0x07
> +#define CONFIG_TDP_SET_LEVEL			0x08
> +#define CONFIG_TDP_GET_UNCORE_P0_P1_INFO	0X09
> +#define CONFIG_TDP_GET_P1_INFO			0x0a
> +#define CONFIG_TDP_GET_MEM_FREQ			0x0b
> +
> +#define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES	0x10
> +#define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS	0x11
> +#define CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO		0x12
> +
> +#define CONFIG_TDP_PBF_GET_CORE_MASK_INFO	0x20
> +#define CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO	0x21
> +#define CONFIG_TDP_PBF_GET_TJ_MAX_INFO		0x22
> +#define CONFIG_TDP_PBF_GET_TDP_INFO		0X23
> +
> +#define CONFIG_CLOS				0xd0
> +#define CLOS_PQR_ASSOC				0x00
> +#define CLOS_PM_CLOS				0x01
> +#define CLOS_PM_QOS_CONFIG			0x02
> +#define CLOS_STATUS				0x03
> +
> +#define MBOX_CMD_WRITE_BIT			0x08
> +
> +#define PM_QOS_INFO_OFFSET			0x00
> +#define PM_QOS_CONFIG_OFFSET			0x04
> +#define PM_CLOS_OFFSET				0x08
> +#define PQR_ASSOC_OFFSET			0x20
> +
> +struct isst_clos_config {
> +	int pkg_id;
> +	int die_id;
> +	unsigned char epp;
> +	unsigned char clos_prop_prio;
> +	unsigned char clos_min;
> +	unsigned char clos_max;
> +	unsigned char clos_desired;
> +};
> +
> +struct isst_fact_bucket_info {
> +	int high_priority_cores_count;
> +	int sse_trl;
> +	int avx_trl;
> +	int avx512_trl;
> +};
> +
> +struct isst_pbf_info {
> +	int pbf_acticated;
> +	int pbf_available;
> +	size_t core_cpumask_size;
> +	cpu_set_t *core_cpumask;
> +	int p1_high;
> +	int p1_low;
> +	int t_control;
> +	int t_prochot;
> +	int tdp;
> +};
> +
> +#define ISST_TRL_MAX_ACTIVE_CORES	8
> +#define ISST_FACT_MAX_BUCKETS		8
> +struct isst_fact_info {
> +	int lp_clipping_ratio_license_sse;
> +	int lp_clipping_ratio_license_avx2;
> +	int lp_clipping_ratio_license_avx512;
> +	struct isst_fact_bucket_info
> bucket_info[ISST_FACT_MAX_BUCKETS];
> +};
> +
> +struct isst_pkg_ctdp_level_info {
> +	int processed;
> +	int control_cpu;
> +	int pkg_id;
> +	int die_id;
> +	int level;
> +	int fact_support;
> +	int pbf_support;
> +	int fact_enabled;
> +	int pbf_enabled;
> +	int tdp_ratio;
> +	int active;
> +	int tdp_control;
> +	int pkg_tdp;
> +	int pkg_min_power;
> +	int pkg_max_power;
> +	int fact;
> +	int t_proc_hot;
> +	int uncore_p0;
> +	int uncore_p1;
> +	int sse_p1;
> +	int avx2_p1;
> +	int avx512_p1;
> +	int mem_freq;
> +	size_t core_cpumask_size;
> +	cpu_set_t *core_cpumask;
> +	int cpu_count;
> +	int trl_sse_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
> +	int trl_avx_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
> +	int trl_avx_512_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
> +	int kobj_bucket_index;
> +	int active_bucket;
> +	int fact_max_index;
> +	int fact_max_config;
> +	int pbf_found;
> +	int pbf_active;
> +	struct isst_pbf_info pbf_info;
> +	struct isst_fact_info fact_info;
> +};
> +
> +#define ISST_MAX_TDP_LEVELS	(4 + 1) /* +1 for base config */
> +struct isst_pkg_ctdp {
> +	int locked;
> +	int version;
> +	int processed;
> +	int levels;
> +	int current_level;
> +	int enabled;
> +	struct isst_pkg_ctdp_level_info
> ctdp_level[ISST_MAX_TDP_LEVELS];
> +};
> +
> +extern int get_topo_max_cpus(void);
> +extern int get_cpu_count(int pkg_id, int die_id);
> +
> +/* Common interfaces */
> +extern void debug_printf(const char *format, ...);
> +extern int out_format_is_json(void);
> +extern int get_physical_package_id(int cpu);
> +extern int get_physical_die_id(int cpu);
> +extern size_t alloc_cpu_set(cpu_set_t **cpu_set);
> +extern void free_cpu_set(cpu_set_t *cpu_set);
> +extern int find_logical_cpu(int pkg_id, int die_id, int phy_cpu);
> +extern int find_phy_cpu_num(int logical_cpu);
> +extern int find_phy_core_num(int logical_cpu);
> +extern void set_cpu_mask_from_punit_coremask(int cpu,
> +					     unsigned long long
> core_mask,
> +					     size_t core_cpumask_size,
> +					     cpu_set_t *core_cpumask,
> +					     int *cpu_cnt);
> +
> +extern int isst_send_mbox_command(unsigned int cpu, unsigned char
> command,
> +				  unsigned char sub_command,
> +				  unsigned int write,
> +				  unsigned int req_data, unsigned int
> *resp);
> +
> +extern int isst_send_msr_command(unsigned int cpu, unsigned int
> command,
> +				 int write, unsigned long long
> *req_resp);
> +
> +extern int isst_get_ctdp_levels(int cpu, struct isst_pkg_ctdp
> *pkg_dev);
> +extern int isst_get_process_ctdp(int cpu, int tdp_level,
> +				 struct isst_pkg_ctdp *pkg_dev);
> +extern void isst_get_process_ctdp_complete(int cpu,
> +					   struct isst_pkg_ctdp
> *pkg_dev);
> +extern void isst_ctdp_display_information(int cpu, FILE *outf, int
> tdp_level,
> +					  struct isst_pkg_ctdp
> *pkg_dev);
> +extern void isst_ctdp_display_information_start(FILE *outf);
> +extern void isst_ctdp_display_information_end(FILE *outf);
> +extern void isst_pbf_display_information(int cpu, FILE *outf, int
> level,
> +					 struct isst_pbf_info *info);
> +extern int isst_set_tdp_level(int cpu, int tdp_level);
> +extern int isst_set_tdp_level_msr(int cpu, int tdp_level);
> +extern int isst_set_pbf_fact_status(int cpu, int pbf, int enable);
> +extern int isst_get_pbf_info(int cpu, int level,
> +			     struct isst_pbf_info *pbf_info);
> +extern void isst_get_pbf_info_complete(struct isst_pbf_info
> *pbf_info);
> +extern int isst_get_fact_info(int cpu, int level,
> +			      struct isst_fact_info *fact_info);
> +extern int isst_get_fact_bucket_info(int cpu, int level,
> +				     struct isst_fact_bucket_info
> *bucket_info);
> +extern void isst_fact_display_information(int cpu, FILE *outf, int
> level,
> +					  int fact_bucket, int
> fact_avx,
> +					  struct isst_fact_info
> *fact_info);
> +extern int isst_set_trl(int cpu, unsigned long long trl);
> +extern int isst_set_trl_from_current_tdp(int cpu, unsigned long long
> trl);
> +extern int isst_get_config_tdp_lock_status(int cpu);
> +
> +extern int isst_pm_qos_config(int cpu, int enable_clos, int
> priority_type);
> +extern int isst_pm_get_clos(int cpu, int clos,
> +			    struct isst_clos_config *clos_config);
> +extern int isst_set_clos(int cpu, int clos,
> +			 struct isst_clos_config *clos_config);
> +extern int isst_clos_associate(int cpu, int clos);
> +extern int isst_clos_get_assoc_status(int cpu, int *clos_id);
> +extern void isst_clos_display_information(int cpu, FILE *outf, int
> clos,
> +					  struct isst_clos_config
> *clos_config);
> +
> +extern int isst_read_reg(unsigned short reg, unsigned int *val);
> +extern int isst_write_reg(int reg, unsigned int val);
> +
> +extern void isst_display_result(int cpu, FILE *outf, char *feature,
> char *cmd,
> +				int result);
> +#endif


^ permalink raw reply


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox