Skip to contentSkip to conversation

Instantiate SPD EEPROMs at boot on x86

PATCH Superseded Jean Delvare 2 participants 9 messages Oct 14, 2019

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Instantiate SPD EEPROMs at boot on x86

This is my work to let decode-dimms work out of the box on most x86 desktop and laptop systems.

[PATCH 1/4] firmware: dmi: Remember the memory type
[PATCH 2/4] firmware: dmi: Add dmi_memdev_handle
[PATCH 3/4] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically [PATCH 4/4] i2c: i801: Instantiate SPD EEPROMs automatically

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#1 lore raw

PATCH 1/4 firmware: dmi: Remember the memory type

Store the memory type while walking the memory slots, and provide a way to retrieve it later.

---
 drivers/firmware/dmi_scan.c |   25 ++++++++++++++++++++++++-
 include/linux/dmi.h         |    2 ++
 2 files changed, 26 insertions(+), 1 deletion(-)
drivers/firmware/dmi_scan.c +24 -1
@@ -35,6 +35,7 @@ static struct dmi_memdev_info {
 	const char *bank;
 	u64 size;		/* bytes */
 	u16 handle;
+	u8 type;		/* DDR2, DDR3, DDR4 etc */
 } *dmi_memdev;
 static int dmi_memdev_nr;
 
@@ -391,7 +392,7 @@ static void __init save_mem_devices(cons
 	u64 bytes;
 	u16 size;
 
-	if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x12)
+	if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x13)
 		return;
 	if (nr >= dmi_memdev_nr) {
 		pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
@@ -400,6 +401,7 @@ static void __init save_mem_devices(cons
 	dmi_memdev[nr].handle = get_unaligned(&dm->handle);
 	dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
 	dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
+	dmi_memdev[nr].type = d[0x12];
 
 	size = get_unaligned((u16 *)&d[0xC]);
 	if (size == 0)
@@ -1128,3 +1130,24 @@ u64 dmi_memdev_size(u16 handle)
 	return ~0ull;
 }
 EXPORT_SYMBOL_GPL(dmi_memdev_size);
+
+/**
+ * dmi_memdev_type - get the memory type
+ * @handle: DMI structure handle
+ *
+ * Return the DMI memory type of the module in the slot associated with the
+ * given DMI handle, or 0x0 if no such DMI handle exists.
+ */
+u8 dmi_memdev_type(u16 handle)
+{
+	int n;
+
+	if (dmi_memdev) {
+		for (n = 0; n < dmi_memdev_nr; n++) {
+			if (handle == dmi_memdev[n].handle)
+				return dmi_memdev[n].type;
+		}
+	}
+	return 0x0;	/* Not a valid value */
+}
+EXPORT_SYMBOL_GPL(dmi_memdev_type);
include/linux/dmi.h +2 -0
@@ -113,6 +113,7 @@ extern int dmi_walk(void (*decode)(const
 extern bool dmi_match(enum dmi_field f, const char *str);
 extern void dmi_memdev_name(u16 handle, const char **bank, const char **device);
 extern u64 dmi_memdev_size(u16 handle);
+extern u8 dmi_memdev_type(u16 handle);
 
 #else
 
@@ -142,6 +143,7 @@ static inline bool dmi_match(enum dmi_fi
 static inline void dmi_memdev_name(u16 handle, const char **bank,
 		const char **device) { }
 static inline u64 dmi_memdev_size(u16 handle) { return ~0ul; }
+static inline u8 dmi_memdev_type(u16 handle) { return 0x0; }
 static inline const struct dmi_system_id *
 	dmi_first_match(const struct dmi_system_id *list) { return NULL; }
 
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#2 lore raw

PATCH 2/4 firmware: dmi: Add dmi_memdev_handle

In reply to Jean Delvare

Add a utility function dmi_memdev_handle() which returns the DMI handle associated with a given memory slot. This will allow kernel drivers to iterate over the memory slots.

---
 drivers/firmware/dmi_scan.c |   16 ++++++++++++++++
 include/linux/dmi.h         |    2 ++
 2 files changed, 18 insertions(+)
drivers/firmware/dmi_scan.c +16 -0
@@ -1151,3 +1151,19 @@ u8 dmi_memdev_type(u16 handle)
 	return 0x0;	/* Not a valid value */
 }
 EXPORT_SYMBOL_GPL(dmi_memdev_type);
+
+/**
+ *	dmi_memdev_handle - get the DMI handle of a memory slot
+ *	@slot: slot number
+ *
+ *	Return the DMI handle associated with a given memory slot, or %0xFFFF
+ *      if there is no such slot.
+ */
+u16 dmi_memdev_handle(int slot)
+{
+	if (dmi_memdev && slot >= 0 && slot < dmi_memdev_nr)
+		return dmi_memdev[slot].handle;
+
+	return 0xffff;	/* Not a valid value */
+}
+EXPORT_SYMBOL_GPL(dmi_memdev_handle);
include/linux/dmi.h +2 -0
@@ -114,6 +114,7 @@ extern bool dmi_match(enum dmi_field f,
 extern void dmi_memdev_name(u16 handle, const char **bank, const char **device);
 extern u64 dmi_memdev_size(u16 handle);
 extern u8 dmi_memdev_type(u16 handle);
+extern u16 dmi_memdev_handle(int slot);
 
 #else
 
@@ -144,6 +145,7 @@ static inline void dmi_memdev_name(u16 h
 		const char **device) { }
 static inline u64 dmi_memdev_size(u16 handle) { return ~0ul; }
 static inline u8 dmi_memdev_type(u16 handle) { return 0x0; }
+static inline u16 dmi_memdev_handle(int slot) { return 0xffff; }
 static inline const struct dmi_system_id *
 	dmi_first_match(const struct dmi_system_id *list) { return NULL; }
 
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#3 lore raw

PATCH 3/4 i2c: smbus: Add a way to instantiate SPD EEPROMs automatically

In reply to Jean Delvare

In simple cases we can instantiate SPD EEPROMs on the SMBus automatically. Start with just DDR2, DDR3 and DDR4 on x86 for now, and only for systems with no more than 4 memory slots. These limitations may be lifted later.

---
 drivers/i2c/i2c-smbus.c   |  104 +++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/i2c-smbus.h |   11 ++++
 2 files changed, 113 insertions(+), 2 deletions(-)
drivers/i2c/i2c-smbus.c +103 -1
@@ -3,10 +3,11 @@
  * i2c-smbus.c - SMBus extensions to the I2C protocol
  *
  * Copyright (C) 2008 David Brownell
- * Copyright (C) 2010 Jean Delvare <[email protected]>
+ * Copyright (C) 2010-2019 Jean Delvare <[email protected]>
  */
 
 #include <linux/device.h>
+#include <linux/dmi.h>
 #include <linux/i2c.h>
 #include <linux/i2c-smbus.h>
 #include <linux/interrupt.h>
@@ -203,6 +204,107 @@ EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert
 
 module_i2c_driver(smbalert_driver);
 
+/*
+ * SPD is not part of SMBus but we include it here for convenience as the
+ * target systems are the same.
+ * Restrictions to automatic SPD instantiation:
+ *  - Only works if all filled slots have the same memory type
+ *  - Only works for DDR2, DDR3 and DDR4 for now
+ *  - Only works on systems with 1 to 4 memory slots
+ */
+#if IS_ENABLED(CONFIG_DMI)
+void i2c_register_spd(struct i2c_adapter *adap)
+{
+	int n, slot_count = 0, dimm_count = 0;
+	u16 handle;
+	u8 common_mem_type = 0x0, mem_type;
+	u64 mem_size;
+	const char *name;
+
+	while ((handle = dmi_memdev_handle(slot_count)) != 0xffff) {
+		slot_count++;
+
+		/* Skip empty slots */
+		mem_size = dmi_memdev_size(handle);
+		if (!mem_size)
+			continue;
+
+		/* Skip undefined memory type */
+		mem_type = dmi_memdev_type(handle);
+		if (mem_type <= 0x02)		/* Invalid, Other, Unknown */
+			continue;
+
+		if (!common_mem_type) {
+			/* First filled slot */
+			common_mem_type = mem_type;
+		} else {
+			/* Check that all filled slots have the same type */
+			if (mem_type != common_mem_type) {
+				dev_warn(&adap->dev,
+					 "Different memory types mixed, not instantiating SPD\n");
+				return;
+			}
+		}
+		dimm_count++;
+	}
+
+	/* No useful DMI data, bail out */
+	if (!dimm_count)
+		return;
+
+	dev_info(&adap->dev, "%d/%d memory slots populated (from DMI)\n",
+		 dimm_count, slot_count);
+
+	if (slot_count > 4) {
+		dev_warn(&adap->dev,
+			 "Systems with more than 4 memory slots not supported yet, not instantiating SPD\n");
+		return;
+	}
+
+	switch (common_mem_type) {
+	case 0x13:	/* DDR2 */
+	case 0x18:	/* DDR3 */
+	case 0x1C:	/* LPDDR2 */
+	case 0x1D:	/* LPDDR3 */
+		name = "spd";
+		break;
+	case 0x1A:	/* DDR4 */
+	case 0x1E:	/* LPDDR4 */
+		name = "ee1004";
+		break;
+	default:
+		dev_info(&adap->dev,
+			 "Memory type 0x%02x not supported yet, not instantiating SPD\n",
+			 common_mem_type);
+		return;
+	}
+
+	/*
+	 * We don't know in which slots the memory modules are. We could
+	 * try to guess from the slot names, but that would be rather complex
+	 * and unreliable, so better probe all possible addresses until we
+	 * have found all memory modules.
+	 */
+	for (n = 0; n < slot_count && dimm_count; n++) {
+		struct i2c_board_info info;
+		unsigned short addr_list[2];
+
+		memset(&info, 0, sizeof(struct i2c_board_info));
+		strlcpy(info.type, name, I2C_NAME_SIZE);
+		addr_list[0] = 0x50 + n;
+		addr_list[1] = I2C_CLIENT_END;
+
+		if (i2c_new_probed_device(adap, &info, addr_list, NULL)) {
+			dev_info(&adap->dev,
+				 "Successfully instantiated SPD at 0x%hx\n",
+				 addr_list[0]);
+			dimm_count--;
+		}
+	}
+}
+EXPORT_SYMBOL_GPL(i2c_register_spd);
+#endif
+
 MODULE_AUTHOR("Jean Delvare <[email protected]>");
 MODULE_DESCRIPTION("SMBus protocol extensions support");
 MODULE_LICENSE("GPL");
include/linux/i2c-smbus.h +10 -1
@@ -2,7 +2,7 @@
 /*
  * i2c-smbus.h - SMBus extensions to the I2C protocol
  *
- * Copyright (C) 2010 Jean Delvare <[email protected]>
+ * Copyright (C) 2010-2019 Jean Delvare <[email protected]>
  */
 
 #ifndef _LINUX_I2C_SMBUS_H
@@ -42,6 +42,15 @@ static inline int of_i2c_setup_smbus_ale
 {
 	return 0;
 }
+#endif
+
+#if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_DMI)
+void i2c_register_spd(struct i2c_adapter *adap);
+#else
+static void i2c_register_spd(struct i2c_adapter *adap)
+{
+	return 0;
+}
 #endif
 
 #endif /* _LINUX_I2C_SMBUS_H */
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#6 lore raw

PATCH 4/4 i2c: i801: Instantiate SPD EEPROMs automatically

In reply to Jean Delvare

Call the function to instantiate SPD EEPROMs automatically on the main SMBus controller.

Multiplexed SMBus systems are excluded for now as they are more complex to handle.

---
 drivers/i2c/busses/i2c-i801.c |    4 ++++
 1 file changed, 4 insertions(+)
drivers/i2c/busses/i2c-i801.c +4 -0
@@ -1314,6 +1314,10 @@ static void i801_probe_optional_slaves(s
 
 	if (is_dell_system_with_lis3lv02d())
 		register_dell_lis3lv02d_i2c_device(priv);
+
+	/* Instantiate SPD EEPROMs unless the SMBus is multiplexed */
+	if (!priv->mux_drvdata)
+		i2c_register_spd(&priv->adapter);
 }
 #else
 static void __init input_apanel_init(void) {}
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#4 lore raw

Re: [PATCH 3/4] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically

In reply to Jean Delvare
I love your patch! Perhaps something to improve:

Hi Jean,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[cannot apply to v5.4-rc3 next-20191011]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Jean-Delvare/Instantiate-SPD-EEPROMs-at-boot-on-x86/20191014-174252
config: sparc64-allmodconfig (attached as .config)
compiler: sparc64-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=sparc64

If you fix the issue, kindly add following tag

All warnings (new ones prefixed by >>):

In file included from drivers/i2c/i2c-core-base.c:24:0:
include/linux/i2c-smbus.h: In function 'i2c_register_spd':

>> include/linux/i2c-smbus.h:52:9: warning: 'return' with a value, in function returning void
     return 0;
            ^

include/linux/i2c-smbus.h:50:13: note: declared here
static void i2c_register_spd(struct i2c_adapter *adap)
^~~~~~~~~~~~~~~~
In file included from drivers/i2c/i2c-core-base.c:24:0:
At top level:
include/linux/i2c-smbus.h:50:13: warning: 'i2c_register_spd' defined but not used [-Wunused-function]

vim +/return +52 include/linux/i2c-smbus.h

    46	
    47	#if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_DMI)
    48	void i2c_register_spd(struct i2c_adapter *adap);
    49	#else
    50	static void i2c_register_spd(struct i2c_adapter *adap)
    51	{
  > 52		return 0;
    53	}
    54	#endif
    55	

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#7 lore raw

Re: [PATCH 4/4] i2c: i801: Instantiate SPD EEPROMs automatically

In reply to Jean Delvare
I love your patch! Yet something to improve:

Hi Jean,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.4-rc3 next-20191011]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Jean-Delvare/Instantiate-SPD-EEPROMs-at-boot-on-x86/20191014-174252
config: i386-defconfig (attached as .config)
compiler: gcc-7 (Debian 7.4.0-13) 7.4.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

drivers/i2c/busses/i2c-i801.c: In function 'i801_probe_optional_slaves':

>> drivers/i2c/busses/i2c-i801.c:1319:11: error: 'struct i801_priv' has no member named 'mux_drvdata'
     if (!priv->mux_drvdata)
              ^~

vim +1319 drivers/i2c/busses/i2c-i801.c

1295
1296 /* Register optional slaves */
1297 static void i801_probe_optional_slaves(struct i801_priv *priv)
1298 {
1299 /* Only register slaves on main SMBus channel */
1300 if (priv->features & FEATURE_IDF)
1301 return;
1302
1303 if (apanel_addr) {
1304 struct i2c_board_info info;
1305
1306 memset(&info, 0, sizeof(struct i2c_board_info));
1307 info.addr = apanel_addr;
1308 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
1309 i2c_new_device(&priv->adapter, &info);
1310 }
1311
1312 if (dmi_name_in_vendors("FUJITSU"))
1313 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
1314
1315 if (is_dell_system_with_lis3lv02d())
1316 register_dell_lis3lv02d_i2c_device(priv);
1317
1318 /* Instantiate SPD EEPROMs unless the SMBus is multiplexed */

> 1319		if (!priv->mux_drvdata)

1320 i2c_register_spd(&priv->adapter);
1321 }
1322 #else
1323 static void __init input_apanel_init(void) {}
1324 static void i801_probe_optional_slaves(struct i801_priv *priv) {}
1325 #endif /* CONFIG_X86 && CONFIG_DMI */
1326

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#5 lore raw

Re: [PATCH 3/4] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically

In reply to kbuild test robot
Quoted 44 lines · kbuild test robot

Hi Jean,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[cannot apply to v5.4-rc3 next-20191011]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Jean-Delvare/Instantiate-SPD-EEPROMs-at-boot-on-x86/20191014-174252
config: sparc64-allmodconfig (attached as .config)
compiler: sparc64-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=sparc64

If you fix the issue, kindly add following tag

All warnings (new ones prefixed by >>):

In file included from drivers/i2c/i2c-core-base.c:24:0:
include/linux/i2c-smbus.h: In function 'i2c_register_spd':

Quoted 1 line
Quoted 1 line

include/linux/i2c-smbus.h:52:9: warning: 'return' with a value, in function returning void

     return 0;
            ^

include/linux/i2c-smbus.h:50:13: note: declared here
static void i2c_register_spd(struct i2c_adapter *adap)
^~~~~~~~~~~~~~~~
In file included from drivers/i2c/i2c-core-base.c:24:0:
At top level:
include/linux/i2c-smbus.h:50:13: warning: 'i2c_register_spd' defined but not used [-Wunused-function]

vim +/return +52 include/linux/i2c-smbus.h

    46	
    47	#if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_DMI)
    48	void i2c_register_spd(struct i2c_adapter *adap);
    49	#else
    50	static void i2c_register_spd(struct i2c_adapter *adap)
    51	{
Quoted 4 lines
Quoted 1 line

52 return 0;

    53	}
    54	#endif
    55	

Fixed, thanks Elliot.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#8 lore raw

Re: [PATCH 4/4] i2c: i801: Instantiate SPD EEPROMs automatically

In reply to kbuild test robot
Quoted 53 lines · kbuild test robot

Hi Jean,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.4-rc3 next-20191011]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Jean-Delvare/Instantiate-SPD-EEPROMs-at-boot-on-x86/20191014-174252
config: i386-defconfig (attached as .config)
compiler: gcc-7 (Debian 7.4.0-13) 7.4.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

   drivers/i2c/busses/i2c-i801.c: In function 'i801_probe_optional_slaves':
Quoted 1 line
Quoted 1 line

drivers/i2c/busses/i2c-i801.c:1319:11: error: 'struct i801_priv' has no member named 'mux_drvdata'

     if (!priv->mux_drvdata)
              ^~

vim +1319 drivers/i2c/busses/i2c-i801.c

1295
1296 /* Register optional slaves */
1297 static void i801_probe_optional_slaves(struct i801_priv *priv)
1298 {
1299 /* Only register slaves on main SMBus channel */
1300 if (priv->features & FEATURE_IDF)
1301 return;
1302
1303 if (apanel_addr) {
1304 struct i2c_board_info info;
1305
1306 memset(&info, 0, sizeof(struct i2c_board_info));
1307 info.addr = apanel_addr;
1308 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
1309 i2c_new_device(&priv->adapter, &info);
1310 }
1311
1312 if (dmi_name_in_vendors("FUJITSU"))
1313 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
1314
1315 if (is_dell_system_with_lis3lv02d())
1316 register_dell_lis3lv02d_i2c_device(priv);
1317
1318 /* Instantiate SPD EEPROMs unless the SMBus is multiplexed */

Quoted 8 lines
Quoted 1 line

1319 if (!priv->mux_drvdata)

1320 i2c_register_spd(&priv->adapter);
1321 }
1322 #else
1323 static void __init input_apanel_init(void) {}
1324 static void i801_probe_optional_slaves(struct i801_priv *priv) {}
1325 #endif /* CONFIG_X86 && CONFIG_DMI */
1326

Fixed, thanks.

Signature · 2 lines
Jean Delvare
SUSE L3 Support