summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRaymond Mao <[email protected]>2026-02-13 17:52:49 -0500
committerTom Rini <[email protected]>2026-02-18 08:27:51 -0600
commit23674dee60240393dd17836c05df5b4f4aa01e5c (patch)
treee4e8b2efd8670e96f8585b54dabee8ae7cd05ad2 /lib
parent374896158b435843d8ecf2c04c9985b0321295e0 (diff)
smbios: add support for dynamic generation of Type 17 table
This commit implements SMBIOS Type 17 (Memory Device) generation with a hybrid approach supporting both: 1. Explicit definition via Device Tree 'smbios' node: Child node under '/smbios/smbios/memory-device' will be used to populate as individual Type 17 structure directly. - Properties follow SMBIOS field names with lowercase letters and hyphen-separated words (e.g., 'physical-memory-array-handle', ' memory-error-information-handle', 'configured-memory-speed', etc.). - This method supports precise platform-defined overrides and system descriptions. 2. Fallback to automatic DT-based discovery: If child node under '/smbios/smbios/memory-device' does not exist, the implementation will: - Scan all top-level 'memory@' nodes to populate Type 17 structure with inferred size and location data. - Scan nodes named or marked as 'memory-controller' and parse associated 'dimm@' subnodes (if present) to extract DIMM sizes and map them accordingly. This dual-mode support enables flexible firmware SMBIOS reporting while aligning with spec-compliant naming and runtime-detected memory topology. Type 17 support is under GENERATE_SMBIOS_TABLE_VERBOSE to avoid increasing rom size for those platforms which only require basic SMBIOS support. Signed-off-by: Raymond Mao <[email protected]> Tested-by: Ilias Apalodimas <[email protected]>
Diffstat (limited to 'lib')
-rw-r--r--lib/smbios.c376
1 files changed, 376 insertions, 0 deletions
diff --git a/lib/smbios.c b/lib/smbios.c
index 27c9c975cf2..7c6ad63b1c7 100644
--- a/lib/smbios.c
+++ b/lib/smbios.c
@@ -135,6 +135,14 @@ typedef int (*smbios_write_subnode)(ulong *current, int handle,
struct smbios_ctx *ctx, int idx,
int type);
+typedef int (*smbios_write_memnode)(ulong *current, int handle,
+ struct smbios_ctx *ctx, int idx,
+ int type);
+
+typedef int (*smbios_write_memctrlnode)(ulong *current, int handle,
+ struct smbios_ctx *ctx, int idx,
+ u64 base, u64 sz);
+
/**
* Function prototype to write a specific type of SMBIOS structure
*
@@ -1432,6 +1440,373 @@ static int smbios_write_type16(ulong *current, int *handle,
return len;
}
+static void smbios_pop_type17_general_si(struct smbios_ctx *ctx,
+ struct smbios_type17 *t)
+{
+ t->mem_err_info_hdl =
+ smbios_get_val_si(ctx, "memory-error-information-handle",
+ SYSID_NONE, SMBIOS_MD_ERRINFO_NONE);
+ t->total_width = smbios_get_val_si(ctx, "total-width", SYSID_NONE, 0);
+ t->data_width = smbios_get_val_si(ctx, "data-width", SYSID_NONE, 0);
+ t->form_factor = smbios_get_val_si(ctx, "form-factor",
+ SYSID_NONE, SMBIOS_MD_FF_UNKNOWN);
+ t->dev_set = smbios_get_val_si(ctx, "device-set", SYSID_NONE,
+ SMBIOS_MD_DEVSET_UNKNOWN);
+ t->dev_locator = smbios_add_prop_si(ctx, "device-locator", SYSID_NONE,
+ NULL);
+ t->bank_locator = smbios_add_prop_si(ctx, "bank-locator", SYSID_NONE,
+ NULL);
+ t->mem_type = smbios_get_val_si(ctx, "memory-type",
+ SYSID_NONE, SMBIOS_MD_TYPE_UNKNOWN);
+ t->type_detail = smbios_get_val_si(ctx, "type-detail",
+ SYSID_NONE, SMBIOS_MD_TD_UNKNOWN);
+ t->speed = smbios_get_val_si(ctx, "speed", SYSID_NONE,
+ SMBIOS_MD_SPEED_UNKNOWN);
+ t->manufacturer = smbios_add_prop_si(ctx, "manufacturer", SYSID_NONE,
+ NULL);
+ t->serial_number = smbios_add_prop_si(ctx, "serial-number", SYSID_NONE,
+ NULL);
+ t->asset_tag = smbios_add_prop_si(ctx, "asset-tag", SYSID_NONE, NULL);
+ t->part_number = smbios_add_prop_si(ctx, "part-number", SYSID_NONE,
+ NULL);
+ t->attributes = smbios_get_val_si(ctx, "attributes", SYSID_NONE,
+ SMBIOS_MD_ATTR_RANK_UNKNOWN);
+ t->config_mem_speed = smbios_get_val_si(ctx, "configured-memory-speed",
+ SYSID_NONE,
+ SMBIOS_MD_CONFSPEED_UNKNOWN);
+ t->min_voltage = smbios_get_val_si(ctx, "minimum-voltage", SYSID_NONE,
+ SMBIOS_MD_VOLTAGE_UNKNOWN);
+ t->max_voltage = smbios_get_val_si(ctx, "maximum-voltage", SYSID_NONE,
+ SMBIOS_MD_VOLTAGE_UNKNOWN);
+ t->config_voltage = smbios_get_val_si(ctx, "configured-voltage",
+ SYSID_NONE,
+ SMBIOS_MD_VOLTAGE_UNKNOWN);
+ t->mem_tech = smbios_get_val_si(ctx, "memory-technology",
+ SYSID_NONE, SMBIOS_MD_TECH_UNKNOWN);
+ t->mem_op_mode_cap =
+ smbios_get_val_si(ctx, "memory-operating-mode-capability",
+ SYSID_NONE, SMBIOS_MD_OPMC_UNKNOWN);
+ t->fw_ver = smbios_add_prop_si(ctx, "firmware-version", SYSID_NONE,
+ NULL);
+ t->module_man_id = smbios_get_val_si(ctx, "module-manufacturer-id",
+ SYSID_NONE, 0);
+ t->module_prod_id = smbios_get_val_si(ctx, "module-product-id",
+ SYSID_NONE, 0);
+ t->mem_subsys_con_man_id =
+ smbios_get_val_si(ctx,
+ "memory-subsystem-controller-manufacturer-id",
+ SYSID_NONE, 0);
+ t->mem_subsys_con_prod_id =
+ smbios_get_val_si(ctx,
+ "memory-subsystem-controller-product-id",
+ SYSID_NONE, 0);
+ t->nonvolatile_size = smbios_get_u64_si(ctx, "non-volatile-size",
+ SYSID_NONE,
+ SMBIOS_MS_PORT_SIZE_UNKNOWN);
+ t->volatile_size = smbios_get_u64_si(ctx, "volatile-size",
+ SYSID_NONE,
+ SMBIOS_MS_PORT_SIZE_UNKNOWN);
+ t->cache_size = smbios_get_u64_si(ctx, "cache-size",
+ SYSID_NONE,
+ SMBIOS_MS_PORT_SIZE_UNKNOWN);
+ t->logical_size = smbios_get_u64_si(ctx, "logical-size",
+ SYSID_NONE,
+ SMBIOS_MS_PORT_SIZE_UNKNOWN);
+ t->ext_speed = smbios_get_val_si(ctx, "extended-speed", SYSID_NONE, 0);
+ t->ext_config_mem_speed =
+ smbios_get_val_si(ctx, "extended-configured-memory-speed",
+ SYSID_NONE, 0);
+ t->pmic0_man_id = smbios_get_val_si(ctx, "pmic0-manufacturer-id",
+ SYSID_NONE, 0);
+ t->pmic0_rev_num = smbios_get_val_si(ctx, "pmic0-revision-number",
+ SYSID_NONE, 0);
+ t->rcd_man_id = smbios_get_val_si(ctx, "rcd-manufacturer-id",
+ SYSID_NONE, 0);
+ t->rcd_rev_num = smbios_get_val_si(ctx, "rcd-revision-number",
+ SYSID_NONE, 0);
+}
+
+static void
+smbios_pop_type17_size_from_memory_node(ofnode node, struct smbios_type17 *t)
+{
+ const fdt32_t *reg;
+ int len;
+ u64 sz;
+ u32 size_mb;
+
+ /* Read property 'reg' from the node */
+ reg = ofnode_read_prop(node, "reg", &len);
+ if (!reg || len < sizeof(fdt32_t) * 4 || len % sizeof(fdt32_t))
+ return;
+
+ /* Combine hi/lo for size (typically 64-bit) */
+ sz = ((u64)fdt32_to_cpu(reg[2]) << 32) | fdt32_to_cpu(reg[3]);
+
+ /* Convert size to MB */
+ size_mb = (u32)(sz >> 20); /* 1 MB = 2^20 */
+ if (size_mb < SMBIOS_MD_SIZE_EXT) {
+ t->size = cpu_to_le16(size_mb);
+ t->ext_size = 0;
+ return;
+ }
+
+ t->size = cpu_to_le16(SMBIOS_MD_SIZE_EXT); /* Signal extended used */
+ t->ext_size = cpu_to_le32((u32)(sz >> 10)); /* In KB */
+}
+
+static void smbios_pop_type17_size_si(struct smbios_ctx *ctx,
+ struct smbios_type17 *t)
+{
+ t->size = smbios_get_val_si(ctx, "size", SYSID_NONE,
+ SMBIOS_MD_SIZE_UNKNOWN);
+ t->ext_size = smbios_get_val_si(ctx, "extended-size", SYSID_NONE, 0);
+}
+
+static int
+smbios_scan_memctrl_subnode(ulong *current, int *handle, struct smbios_ctx *ctx,
+ int idx, smbios_write_memctrlnode cb)
+{
+ int total_len = 0;
+ ofnode child;
+ int i = 0;
+ int hdl_base = *handle;
+ u64 base = 0;
+
+ /*
+ * Enumerate all subnodes of 'memory-controller' that contain 'size'
+ * property and generate one instance for each.
+ */
+ for (child = ofnode_first_subnode(ctx->node); ofnode_valid(child);
+ child = ofnode_next_subnode(child)) {
+ u64 sz = 0;
+ const fdt32_t *size;
+ int proplen;
+
+ size = ofnode_read_prop(child, "size", &proplen);
+ if (!size || proplen < sizeof(fdt32_t) ||
+ proplen % sizeof(fdt32_t))
+ continue;
+
+ /* 64-bit size: <hi lo> or 32-bit size */
+ if (proplen >= sizeof(fdt32_t) * 2)
+ sz = ((u64)fdt32_to_cpu(size[0]) << 32) |
+ fdt32_to_cpu(size[1]);
+ else
+ sz = fdt32_to_cpu(size[0]);
+
+ *handle = hdl_base + i;
+ total_len += cb(current, *handle, ctx, idx, base, sz);
+ base += sz;
+ i++;
+ }
+
+ return total_len;
+}
+
+static int
+smbios_write_type17_from_memctrl_node(ulong *current, int handle,
+ struct smbios_ctx *ctx, int idx,
+ u64 __maybe_unused base, u64 sz)
+{
+ struct smbios_type17 *t;
+ int len;
+ u8 *eos_addr;
+ u32 size_mb;
+ void *hdl;
+ size_t hdl_size;
+
+ len = sizeof(*t);
+ t = map_sysmem(*current, len);
+ memset(t, 0, len);
+
+ fill_smbios_header(t, SMBIOS_MEMORY_DEVICE, len, handle);
+
+ /* eos is at the end of the structure */
+ eos_addr = (u8 *)t + len - sizeof(t->eos);
+ smbios_set_eos(ctx, eos_addr);
+
+ /* Read the memory array handles */
+ if (!sysinfo_get_data(ctx->dev, SYSID_SM_MEMARRAY_HANDLE, &hdl,
+ &hdl_size) &&
+ hdl_size == SYSINFO_MEM_HANDLE_MAX * sizeof(u16))
+ t->phy_mem_array_hdl = *((u16 *)hdl + idx);
+
+ /* Convert to MB */
+ size_mb = (u32)(sz >> 20);
+ if (size_mb < SMBIOS_MD_SIZE_EXT) {
+ /* Use 16-bit size field */
+ t->size = cpu_to_le16(size_mb); /* In MB */
+ t->ext_size = cpu_to_le32(0);
+ } else {
+ /* Signal use of extended size field */
+ t->size = cpu_to_le16(SMBIOS_MD_SIZE_EXT);
+ t->ext_size = cpu_to_le32((u32)(sz >> 10)); /* In KB */
+ }
+
+ /* Write other general fields */
+ smbios_pop_type17_general_si(ctx, t);
+
+ len = t->hdr.length + smbios_string_table_len(ctx);
+ *current += len;
+ unmap_sysmem(t);
+
+ return len;
+}
+
+static int smbios_write_type17_mem(ulong *current, int handle,
+ struct smbios_ctx *ctx, int idx,
+ int type)
+{
+ struct smbios_type17 *t;
+ int len;
+ u8 *eos_addr;
+ void *hdl;
+ size_t hdl_size;
+
+ len = sizeof(*t);
+ t = map_sysmem(*current, len);
+ memset(t, 0, len);
+
+ fill_smbios_header(t, SMBIOS_MEMORY_DEVICE, len, handle);
+
+ /* eos is at the end of the structure */
+ eos_addr = (u8 *)t + len - sizeof(t->eos);
+ smbios_set_eos(ctx, eos_addr);
+
+ if (type == SMBIOS_MEM_CUSTOM) {
+ smbios_pop_type17_size_si(ctx, t);
+
+ t->phy_mem_array_hdl =
+ smbios_get_val_si(ctx, "physical-memory-array-handle",
+ SYSID_NONE, 0);
+ } else if (type == SMBIOS_MEM_FDT_MEM_NODE) {
+ smbios_pop_type17_size_from_memory_node(ctx->node, t);
+
+ /* Read the memory array handles */
+ if (!sysinfo_get_data(ctx->dev, SYSID_SM_MEMARRAY_HANDLE, &hdl,
+ &hdl_size) &&
+ hdl_size == SYSINFO_MEM_HANDLE_MAX * sizeof(u16))
+ t->phy_mem_array_hdl = *((u16 *)hdl + idx);
+ }
+
+ /* Write other general fields */
+ smbios_pop_type17_general_si(ctx, t);
+
+ len = t->hdr.length + smbios_string_table_len(ctx);
+ *current += len;
+ unmap_sysmem(t);
+
+ return len;
+}
+
+static int smbios_scan_mem_nodes(ulong *current, int *handle,
+ struct smbios_ctx *ctx,
+ smbios_write_memnode mem_cb,
+ int *idx)
+{
+ int len = 0;
+ struct smbios_ctx ctx_bak;
+ ofnode child;
+ int hdl_base = *handle;
+
+ memcpy(&ctx_bak, ctx, sizeof(ctx_bak));
+
+ for (child = ofnode_first_subnode(ofnode_root());
+ ofnode_valid(child); child = ofnode_next_subnode(child)) {
+ const char *str;
+
+ /* Look up for 'device_type = "memory"' */
+ str = ofnode_read_string(child, "device_type");
+ if (!str || strcmp(str, "memory"))
+ continue;
+
+ ctx->node = child;
+ *handle = hdl_base + *idx;
+ /* Generate one instance for each 'memory' node */
+ len += mem_cb(current, *handle, ctx, *idx,
+ SMBIOS_MEM_FDT_MEM_NODE);
+ memcpy(ctx, &ctx_bak, sizeof(*ctx));
+ (*idx)++;
+ }
+
+ return len;
+}
+
+static int smbios_scan_mctrl_subnodes(ulong *current, int *handle,
+ struct smbios_ctx *ctx,
+ smbios_write_memctrlnode mctrl_wcb,
+ int *idx)
+{
+ int len = 0;
+ struct smbios_ctx ctx_bak;
+ ofnode child;
+
+ memcpy(&ctx_bak, ctx, sizeof(ctx_bak));
+
+ for (child = ofnode_first_subnode(ofnode_root());
+ ofnode_valid(child); child = ofnode_next_subnode(child)) {
+ const char *compat;
+ const char *name;
+
+ /*
+ * Look up for node with name or property 'compatible'
+ * containing 'memory-controller'.
+ */
+ name = ofnode_get_name(child);
+ compat = ofnode_read_string(child, "compatible");
+ if ((!compat || !strstr(compat, "memory-controller")) &&
+ (!name || !strstr(name, "memory-controller")))
+ continue;
+
+ (*handle)++;
+ ctx->node = child;
+ /*
+ * Generate one instance for each subnode of
+ * 'memory-controller' which contains property 'size'.
+ */
+ len += smbios_scan_memctrl_subnode(current, handle, ctx,
+ *idx, mctrl_wcb);
+ memcpy(ctx, &ctx_bak, sizeof(*ctx));
+ (*idx)++;
+ }
+ return len;
+}
+
+static int smbios_write_type1719(ulong *current, int *handle,
+ struct smbios_ctx *ctx,
+ smbios_write_memnode mem_cb,
+ smbios_write_memctrlnode mctrl_wcb)
+{
+ int len = 0;
+ int idx;
+
+ if (!IS_ENABLED(CONFIG_OF_CONTROL))
+ return 0; /* Error, return 0-length */
+
+ /* Step 1: Scan any subnode exists */
+ len = smbios_scan_subnodes(current, ctx, handle, mem_cb,
+ SMBIOS_MEM_CUSTOM);
+ if (len)
+ return len;
+
+ /* Step 2: Scan 'memory' node from the entire FDT */
+ idx = 0;
+ len += smbios_scan_mem_nodes(current, handle, ctx, mem_cb, &idx);
+
+ /* Step 3: Scan 'memory-controller' node from the entire FDT */
+ len += smbios_scan_mctrl_subnodes(current, handle, ctx, mctrl_wcb, &idx);
+
+ return len;
+}
+
+static int smbios_write_type17(ulong *current, int *handle,
+ struct smbios_ctx *ctx)
+{
+ return smbios_write_type1719(current, handle, ctx,
+ smbios_write_type17_mem,
+ smbios_write_type17_from_memctrl_node);
+}
+
#endif /* #if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE) */
static int smbios_write_type32(ulong *current, int *handle,
@@ -1481,6 +1856,7 @@ static struct smbios_write_method smbios_write_funcs[] = {
#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
{ smbios_write_type9, "system-slot"},
{ smbios_write_type16, "memory-array"},
+ { smbios_write_type17, "memory-device"},
#endif
{ smbios_write_type32, },
{ smbios_write_type127 },