Skip to contentSkip to conversation

peci: Remove dependency on x86 CPU variables

PATCH In review Dave Hansen 5 participants 5 messages Feb 20 – Feb 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 peci: Remove dependency on x86 CPU variables

From: Dave Hansen <[email protected]>

tl;dr: The non-x86 PECI driver #includes an arch/x86 header. This is ostensibly to avoid duplicating CPU model number constants, but the result is complexity and duplicated *code* which is a far worse fate than duplicated constants.

Remove the PECI dependency on arch/x86 by adding a list of supported "target" CPU models in the driver.

This is only compile tested.

Long version:

== Background ==

The "PECI" driver runs on non-x86 hardware inside an x86 system. It talks to the x86 CPU. The PECI hardware has different features based on platform generations and uses the CPU model to control feature detections.

Basically, instead of a PCI or USB device ID that a USB or PCI driver would use, the PECI driver uses the CPU model (and family).

The arch/x86 code unsurprisingly has a list of CPU model numbers and the PECI code currently reuses that list. But the arch/x86 list is maintained in the "Display" format which is different than the binary format that CPUID (and PECI hardware) uses.

== Problem ==

The end result is that the PECI code #includes the arch/x86 constants header and then duplicates some code that transforms the CPUID to the "Display" format. This is fragile because it's easy for us x86 folks to break the PECI driver when assuming that arch/x86 is x86-only.

== Solution ==

Remove the arch/x86 dependency. Instead of duplicating the CPUID=>Display functionality, just duplicate the constants.

Also rename the formerly "x86_vfm" variables. They are not in the VFM format any longer. They are purely device IDs. Name them appropriately.

The result is a net code removal. The only downside is that the PECI folks need to add a #define whenever there is a new CPU model. But, they need to go add new CPU model to the driver explicitly *anyway*.

== Notes ==

One little wrinkle in this is that the CPU identifier that comes back from the PECI hardware contains the CPU stepping just like CPUID.01H:EAX. But the stepping is ignored by the PECI driver.

So, the PECI_INTEL_* identifiers are just defined with the stepping shifted off the beginning. They could have been defined with a 0 there and then have the stepping masked somewhere.

Cc Iwona Winiarska <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Tony Luck <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

--

Changes from v1:
* Fix wrong Haswell model/family
* Fix peci_device kernel doc
---

 b/drivers/hwmon/peci/cputemp.c |   10 ++++-----
 b/drivers/peci/core.c          |    4 +--
 b/drivers/peci/cpu.c           |   16 +++++++-------
 b/drivers/peci/device.c        |   40 +++++++++++-------------------------
 b/drivers/peci/internal.h      |    4 +--
 b/include/linux/peci-cpu.h     |   45 ++++++++++++++++++-----------------------
 b/include/linux/peci.h         |    4 +--
 7 files changed, 52 insertions(+), 71 deletions(-)
drivers/peci/device.c renamed from drivers/peci/device.c~peci-sanity +13 -27
diff -puN drivers/peci/device.c~peci-sanity drivers/peci/device.c
@@ -57,39 +57,25 @@ static int peci_get_cpu_id(struct peci_d
 	if (ret)
 		goto out_req_free;
 
+	/*
+	 * The id that comes back from the hardware is in the raw
+	 * format of x86 CPUID.01H:EAX leaf and includes the CPU
+	 * Model, Family and Stepping.
+	 */
 	*cpu_id = peci_request_data_readl(req);
+
+	/*
+	 * Remove the stepping (CPUID.01H:EAX[3:0]) to match the
+	 * PECI_INTEL_* identifiers:
+	 */
+	*cpu_id >>= 4;
+
 out_req_free:
 	peci_request_free(req);
 
 	return ret;
 }
 
-static unsigned int peci_x86_cpu_family(unsigned int sig)
-{
-	unsigned int x86;
-
-	x86 = (sig >> 8) & 0xf;
-
-	if (x86 == 0xf)
-		x86 += (sig >> 20) & 0xff;
-
-	return x86;
-}
-
-static unsigned int peci_x86_cpu_model(unsigned int sig)
-{
-	unsigned int fam, model;
-
-	fam = peci_x86_cpu_family(sig);
-
-	model = (sig >> 4) & 0xf;
-
-	if (fam >= 0x6)
-		model += ((sig >> 16) & 0xf) << 4;
-
-	return model;
-}
-
 static int peci_device_info_init(struct peci_device *device)
 {
 	u8 revision;
@@ -100,7 +86,7 @@ static int peci_device_info_init(struct
 	if (ret)
 		return ret;
 
-	device->info.x86_vfm = IFM(peci_x86_cpu_family(cpu_id), peci_x86_cpu_model(cpu_id));
+	device->info.device_id = cpu_id;
 
 	ret = peci_get_revision(device, &revision);
 	if (ret)
drivers/peci/internal.h renamed from drivers/peci/internal.h~peci-sanity +2 -2
diff -puN drivers/peci/internal.h~peci-sanity drivers/peci/internal.h
@@ -66,11 +66,11 @@ struct peci_request *peci_xfer_ep_mmio64
 /**
  * struct peci_device_id - PECI device data to match
  * @data: pointer to driver private data specific to device
- * @x86_vfm: device vendor-family-model
+ * @device_id: device identifier, includes CPU vendor-family-model
  */
 struct peci_device_id {
 	const void *data;
-	u32 x86_vfm;
+	u32 device_id;
 };
 
 extern const struct device_type peci_device_type;
include/linux/peci.h renamed from include/linux/peci.h~peci-sanity +2 -2
diff -puN include/linux/peci.h~peci-sanity include/linux/peci.h
@@ -59,7 +59,7 @@ static inline struct peci_controller *to
  * struct peci_device - PECI device
  * @dev: device object to register PECI device to the device model
  * @info: PECI device characteristics
- * @info.x86_vfm: device vendor-family-model
+ * @info.device_id: device identifier (CPU family-model)
  * @info.peci_revision: PECI revision supported by the PECI device
  * @info.socket_id: the socket ID represented by the PECI device
  * @addr: address used on the PECI bus connected to the parent controller
@@ -72,7 +72,7 @@ static inline struct peci_controller *to
 struct peci_device {
 	struct device dev;
 	struct {
-		u32 x86_vfm;
+		u32 device_id;
 		u8 peci_revision;
 		u8 socket_id;
 	} info;
drivers/hwmon/peci/cputemp.c renamed from drivers/hwmon/peci/cputemp.c~peci-sanity +5 -5
diff -puN drivers/hwmon/peci/cputemp.c~peci-sanity drivers/hwmon/peci/cputemp.c
@@ -340,11 +340,11 @@ static int init_core_mask(struct peci_cp
 	int ret;
 
 	/* Get the RESOLVED_CORES register value */
-	switch (peci_dev->info.x86_vfm) {
-	case INTEL_ICELAKE_X:
-	case INTEL_ICELAKE_D:
-	case INTEL_SAPPHIRERAPIDS_X:
-	case INTEL_EMERALDRAPIDS_X:
+	switch (peci_dev->info.device_id) {
+	case PECI_INTEL_ICELAKE_X:
+	case PECI_INTEL_ICELAKE_D:
+	case PECI_INTEL_SAPPHIRERAPIDS_X:
+	case PECI_INTEL_EMERALDRAPIDS_X:
 		ret = peci_ep_pci_local_read(peci_dev, 0, reg->bus, reg->dev,
 					     reg->func, reg->offset + 4, &data);
 		if (ret)
drivers/peci/core.c renamed from drivers/peci/core.c~peci-sanity +2 -2
diff -puN drivers/peci/core.c~peci-sanity drivers/peci/core.c
@@ -163,8 +163,8 @@ EXPORT_SYMBOL_NS_GPL(devm_peci_controlle
 static const struct peci_device_id *
 peci_bus_match_device_id(const struct peci_device_id *id, struct peci_device *device)
 {
-	while (id->x86_vfm != 0) {
-		if (id->x86_vfm == device->info.x86_vfm)
+	while (id->device_id != 0) {
+		if (id->device_id == device->info.device_id)
 			return id;
 		id++;
 	}
include/linux/peci-cpu.h renamed from include/linux/peci-cpu.h~peci-sanity +20 -25

51 lines not on this page · raw message

drivers/peci/cpu.c renamed from drivers/peci/cpu.c~peci-sanity +8 -8
diff -puN drivers/peci/cpu.c~peci-sanity drivers/peci/cpu.c
@@ -294,35 +294,35 @@ peci_cpu_probe(struct peci_device *devic
 
 static const struct peci_device_id peci_cpu_device_ids[] = {
 	{ /* Haswell Xeon */
-		.x86_vfm = INTEL_HASWELL_X,
+		.device_id = PECI_INTEL_HASWELL_X,
 		.data	= "hsx",
 	},
 	{ /* Broadwell Xeon */
-		.x86_vfm = INTEL_BROADWELL_X,
+		.device_id = PECI_INTEL_BROADWELL_X,
 		.data	= "bdx",
 	},
 	{ /* Broadwell Xeon D */
-		.x86_vfm = INTEL_BROADWELL_D,
+		.device_id = PECI_INTEL_BROADWELL_D,
 		.data	= "bdxd",
 	},
 	{ /* Skylake Xeon */
-		.x86_vfm = INTEL_SKYLAKE_X,
+		.device_id = PECI_INTEL_SKYLAKE_X,
 		.data	= "skx",
 	},
 	{ /* Icelake Xeon */
-		.x86_vfm = INTEL_ICELAKE_X,
+		.device_id = PECI_INTEL_ICELAKE_X,
 		.data	= "icx",
 	},
 	{ /* Icelake Xeon D */
-		.x86_vfm = INTEL_ICELAKE_D,
+		.device_id = PECI_INTEL_ICELAKE_D,
 		.data	= "icxd",
 	},
 	{ /* Sapphire Rapids Xeon */
-		.x86_vfm = INTEL_SAPPHIRERAPIDS_X,
+		.device_id = PECI_INTEL_SAPPHIRERAPIDS_X,
 		.data	= "spr",
 	},
 	{ /* Emerald Rapids Xeon */
-		.x86_vfm  = INTEL_EMERALDRAPIDS_X,
+		.device_id  = PECI_INTEL_EMERALDRAPIDS_X,
 		.data  = "emr",
 	},
 	{ }

_

maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 72 lines · Dave Hansen

From: Dave Hansen <[email protected]>

tl;dr: The non-x86 PECI driver #includes an arch/x86 header. This is ostensibly to avoid duplicating CPU model number constants, but the result is complexity and duplicated *code* which is a far worse fate than duplicated constants.

Remove the PECI dependency on arch/x86 by adding a list of supported "target" CPU models in the driver.

This is only compile tested.

Long version:

== Background ==

The "PECI" driver runs on non-x86 hardware inside an x86 system. It talks to the x86 CPU. The PECI hardware has different features based on platform generations and uses the CPU model to control feature detections.

Basically, instead of a PCI or USB device ID that a USB or PCI driver would use, the PECI driver uses the CPU model (and family).

The arch/x86 code unsurprisingly has a list of CPU model numbers and the PECI code currently reuses that list. But the arch/x86 list is maintained in the "Display" format which is different than the binary format that CPUID (and PECI hardware) uses.

== Problem ==

The end result is that the PECI code #includes the arch/x86 constants header and then duplicates some code that transforms the CPUID to the "Display" format. This is fragile because it's easy for us x86 folks to break the PECI driver when assuming that arch/x86 is x86-only.

== Solution ==

Remove the arch/x86 dependency. Instead of duplicating the CPUID=>Display functionality, just duplicate the constants.

Also rename the formerly "x86_vfm" variables. They are not in the VFM format any longer. They are purely device IDs. Name them appropriately.

The result is a net code removal. The only downside is that the PECI folks need to add a #define whenever there is a new CPU model. But, they need to go add new CPU model to the driver explicitly *anyway*.

== Notes ==

One little wrinkle in this is that the CPU identifier that comes back from the PECI hardware contains the CPU stepping just like CPUID.01H:EAX. But the stepping is ignored by the PECI driver.

So, the PECI_INTEL_* identifiers are just defined with the stepping shifted off the beginning. They could have been defined with a 0 there and then have the stepping masked somewhere.

Cc Iwona Winiarska <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Tony Luck <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Quoted 8 lines

--

Changes from v1:
* Fix wrong Haswell model/family
* Fix peci_device kernel doc
---

 b/drivers/hwmon/peci/cputemp.c |   10 ++++-----

For hwmon:

Guenter

#2 lore raw
In reply to Dave Hansen
Quoted 4 lines · Dave Hansen

* struct peci_device_id - PECI device data to match
* @data: pointer to driver private data specific to device
- * @x86_vfm: device vendor-family-model
+ * @device_id: device identifier, includes CPU vendor-family-model

Just a nit. There is an inconsistency between the two Kdoc comments.

device_id no longer stores the vendor. The one below one is correct. CPUID.01H:EAX only gives the family-model-stepping information which is stored in the device_id after removing the stepping.

Quoted 5 lines

* struct peci_device - PECI device
* @dev: device object to register PECI device to the device model
* @info: PECI device characteristics
- * @info.x86_vfm: device vendor-family-model
+ * @info.device_id: device identifier (CPU family-model)

#3 lore raw
Quoted 11 lines · Sohil Mehta
Quoted 4 lines · Dave Hansen

* struct peci_device_id - PECI device data to match
* @data: pointer to driver private data specific to device
- * @x86_vfm: device vendor-family-model
+ * @device_id: device identifier, includes CPU vendor-family-model

Just a nit. There is an inconsistency between the two Kdoc comments.

device_id no longer stores the vendor. The one below one is correct. CPUID.01H:EAX only gives the family-model-stepping information which is stored in the device_id after removing the stepping.

Very true. I'll change that for v3. I'll just be:

* @device_id: device identifier, includes CPU family-model

maintainer Reviewed-by on v2 1/1
#4 lore raw
In reply to Dave Hansen
Quoted 59 lines · Dave Hansen

From: Dave Hansen <[email protected]>

tl;dr: The non-x86 PECI driver #includes an arch/x86 header.  This is ostensibly to avoid duplicating CPU model number constants, but the result is complexity and duplicated *code* which is a far worse fate than duplicated constants.

Remove the PECI dependency on arch/x86 by adding a list of supported "target" CPU models in the driver.

This is only compile tested.

Long version:

== Background ==

The "PECI" driver runs on non-x86 hardware inside an x86 system.  It talks to the x86 CPU. The PECI hardware has different features based on platform generations and uses the CPU model to control feature detections.

Basically, instead of a PCI or USB device ID that a USB or PCI driver would use, the PECI driver uses the CPU model (and family).

The arch/x86 code unsurprisingly has a list of CPU model numbers and the PECI code currently reuses that list. But the arch/x86 list is maintained in the "Display" format which is different than the binary format that CPUID (and PECI hardware) uses.

== Problem ==

The end result is that the PECI code #includes the arch/x86 constants header and then duplicates some code that transforms the CPUID to the "Display" format. This is fragile because it's easy for us x86 folks to break the PECI driver when assuming that arch/x86 is x86-only.

== Solution ==

Remove the arch/x86 dependency. Instead of duplicating the CPUID=>Display functionality, just duplicate the constants.

Also rename the formerly "x86_vfm" variables. They are not in the VFM format any longer. They are purely device IDs.  Name them appropriately.

The result is a net code removal. The only downside is that the PECI folks need to add a #define whenever there is a new CPU model. But, they need to go add new CPU model to the driver explicitly *anyway*.

== Notes ==

One little wrinkle in this is that the CPU identifier that comes back from the PECI hardware contains the CPU stepping just like CPUID.01H:EAX. But the stepping is ignored by the PECI driver.

So, the PECI_INTEL_* identifiers are just defined with the stepping shifted off the beginning. They could have been defined with a 0 there and then have the stepping masked somewhere.

Quoted 13 lines
Cc Iwona Winiarska <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Tony Luck <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

For v3, feel free to add

Thanks
-Iwona