blob: 188fb4256f3730a723f08889a24d4d20a24dde4e [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Sven Schnelle164bcfd2011-08-14 20:56:34 +02002
Angel Ponsd62a5012021-06-28 17:18:06 +02003#include <assert.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +02004#include <string.h>
5#include <smbios.h>
6#include <console/console.h>
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +02007#include <version.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +02008#include <device/device.h>
Patrick Rudolphff251d22021-02-11 15:09:22 +01009#include <device/dram/spd.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020010#include <arch/cpu.h>
11#include <cpu/x86/name.h>
Duncan Laurie472ec9c2012-06-23 16:13:42 -070012#include <elog.h>
Julius Werner9ff8f6f2015-02-23 14:31:09 -080013#include <endian.h>
Kane Chen33faac62014-07-27 12:54:44 -070014#include <memory_info.h>
15#include <spd.h>
16#include <cbmem.h>
Elyes HAOUASe2d152c2019-06-21 07:06:50 +020017#include <commonlib/helpers.h>
Christian Walter9e5b0622019-05-21 17:37:58 +020018#include <device/pci_ids.h>
Christian Walter9e5b0622019-05-21 17:37:58 +020019#include <device/pci.h>
Johnny Linc746a742020-06-03 11:44:22 +080020#include <drivers/vpd/vpd.h>
21#include <stdlib.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020022
Patrick Rudolphfc5b8092019-03-30 17:37:28 +010023#define update_max(len, max_len, stmt) \
24 do { \
25 int tmp = stmt; \
26 \
27 max_len = MAX(max_len, tmp); \
28 len += tmp; \
29 } while (0)
30
Sven Schnelle164bcfd2011-08-14 20:56:34 +020031static u8 smbios_checksum(u8 *p, u32 length)
32{
33 u8 ret = 0;
34 while (length--)
35 ret += *p++;
36 return -ret;
37}
38
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030039int smbios_add_string(u8 *start, const char *str)
Sven Schnelle164bcfd2011-08-14 20:56:34 +020040{
41 int i = 1;
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030042 char *p = (char *)start;
Sven Schnelle164bcfd2011-08-14 20:56:34 +020043
Ben Gardner6b07cba2015-12-09 11:24:35 -060044 /*
45 * Return 0 as required for empty strings.
46 * See Section 6.1.3 "Text Strings" of the SMBIOS specification.
47 */
48 if (*str == '\0')
49 return 0;
50
Elyes HAOUASdbf30672016-08-21 17:37:15 +020051 for (;;) {
Sven Schnelle164bcfd2011-08-14 20:56:34 +020052 if (!*p) {
53 strcpy(p, str);
54 p += strlen(str);
55 *p++ = '\0';
56 *p++ = '\0';
57 return i;
58 }
59
60 if (!strcmp(p, str))
61 return i;
62
63 p += strlen(p)+1;
64 i++;
65 }
66}
67
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030068int smbios_string_table_len(u8 *start)
Sven Schnelle164bcfd2011-08-14 20:56:34 +020069{
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030070 char *p = (char *)start;
Sven Schnelle164bcfd2011-08-14 20:56:34 +020071 int i, len = 0;
72
Lee Leahy024b13d2017-03-16 13:41:11 -070073 while (*p) {
Sven Schnelle164bcfd2011-08-14 20:56:34 +020074 i = strlen(p) + 1;
75 p += i;
76 len += i;
77 }
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030078
79 if (!len)
80 return 2;
81
Sven Schnelle164bcfd2011-08-14 20:56:34 +020082 return len + 1;
83}
84
Angel Ponsa37701a2021-06-28 17:36:53 +020085int smbios_full_table_len(struct smbios_header *header, u8 *str_table_start)
86{
87 return header->length + smbios_string_table_len(str_table_start);
88}
89
Angel Ponsd62a5012021-06-28 17:18:06 +020090void *smbios_carve_table(unsigned long start, u8 type, u8 length, u16 handle)
91{
92 struct smbios_header *t = (struct smbios_header *)start;
93
94 assert(length >= sizeof(*t));
95 memset(t, 0, length);
96 t->type = type;
97 t->length = length - 2;
98 t->handle = handle;
99 return t;
100}
101
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +0300102static int smbios_cpu_vendor(u8 *start)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200103{
Rudolf Marek06253cd2012-02-25 23:51:12 +0100104 if (cpu_have_cpuid()) {
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700105 u32 tmp[4];
106 const struct cpuid_result res = cpuid(0);
107 tmp[0] = res.ebx;
108 tmp[1] = res.edx;
109 tmp[2] = res.ecx;
110 tmp[3] = 0;
111 return smbios_add_string(start, (const char *)tmp);
112 } else {
113 return smbios_add_string(start, "Unknown");
Rudolf Marek06253cd2012-02-25 23:51:12 +0100114 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200115}
116
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +0300117static int smbios_processor_name(u8 *start)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200118{
Nico Huberaa9643e2017-06-20 14:49:04 +0200119 u32 tmp[13];
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700120 const char *str = "Unknown Processor Name";
Rudolf Marek06253cd2012-02-25 23:51:12 +0100121 if (cpu_have_cpuid()) {
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700122 int i;
Felix Held73e9ac62023-02-20 17:46:41 +0100123 struct cpuid_result res;
124 if (cpu_cpuid_extended_level() >= 0x80000004) {
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700125 int j = 0;
Rudolf Marek06253cd2012-02-25 23:51:12 +0100126 for (i = 0; i < 3; i++) {
127 res = cpuid(0x80000002 + i);
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700128 tmp[j++] = res.eax;
129 tmp[j++] = res.ebx;
130 tmp[j++] = res.ecx;
131 tmp[j++] = res.edx;
Rudolf Marek06253cd2012-02-25 23:51:12 +0100132 }
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700133 tmp[12] = 0;
134 str = (const char *)tmp;
Rudolf Marek06253cd2012-02-25 23:51:12 +0100135 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200136 }
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700137 return smbios_add_string(start, str);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200138}
139
Angel Pons9630ced2020-07-29 18:29:28 +0200140/* this function will fill the corresponding manufacturer */
141void smbios_fill_dimm_manufacturer_from_id(uint16_t mod_id, struct smbios_type17 *t)
142{
Patrick Rudolphff251d22021-02-11 15:09:22 +0100143 const char *const manufacturer = spd_manufacturer_name(mod_id);
Angel Pons9630ced2020-07-29 18:29:28 +0200144
145 if (manufacturer) {
146 t->manufacturer = smbios_add_string(t->eos, manufacturer);
147 } else {
148 char string_buffer[256];
149
150 snprintf(string_buffer, sizeof(string_buffer), "Unknown (%x)", mod_id);
151 t->manufacturer = smbios_add_string(t->eos, string_buffer);
Kane Chen33faac62014-07-27 12:54:44 -0700152 }
153}
154
Raul E Rangel50021cd2018-03-20 12:40:55 -0600155static void trim_trailing_whitespace(char *buffer, size_t buffer_size)
Richard Spiegelbd654802018-02-22 10:03:39 -0700156{
Raul E Rangel50021cd2018-03-20 12:40:55 -0600157 size_t len = strnlen(buffer, buffer_size);
158
159 if (len == 0)
160 return;
161
162 for (char *p = buffer + len - 1; p >= buffer; --p) {
163 if (*p == ' ')
164 *p = 0;
165 else
166 break;
167 }
168}
169
170/** This function will fill the corresponding part number */
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200171static void smbios_fill_dimm_part_number(const char *part_number, struct smbios_type17 *t)
Raul E Rangel50021cd2018-03-20 12:40:55 -0600172{
Raul E Rangel50021cd2018-03-20 12:40:55 -0600173 int invalid;
174 size_t i, len;
Jacob Garber9172b692019-06-26 16:18:16 -0600175 char trimmed_part_number[DIMM_INFO_PART_NUMBER_SIZE];
Raul E Rangel50021cd2018-03-20 12:40:55 -0600176
Jacob Garber9172b692019-06-26 16:18:16 -0600177 strncpy(trimmed_part_number, part_number, sizeof(trimmed_part_number));
178 trimmed_part_number[sizeof(trimmed_part_number) - 1] = '\0';
Raul E Rangel50021cd2018-03-20 12:40:55 -0600179
180 /*
181 * SPD mandates that unused characters be represented with a ' '.
182 * We don't want to publish the whitespace in the SMBIOS tables.
183 */
Jacob Garber9172b692019-06-26 16:18:16 -0600184 trim_trailing_whitespace(trimmed_part_number, sizeof(trimmed_part_number));
Raul E Rangel50021cd2018-03-20 12:40:55 -0600185
186 len = strlen(trimmed_part_number);
Richard Spiegelbd654802018-02-22 10:03:39 -0700187
188 invalid = 0; /* assume valid */
Lijian Zhaob1fc13a2018-03-26 18:27:02 -0700189 for (i = 0; i < len; i++) {
Raul E Rangel50021cd2018-03-20 12:40:55 -0600190 if (trimmed_part_number[i] < ' ') {
Richard Spiegelbd654802018-02-22 10:03:39 -0700191 invalid = 1;
Raul E Rangel50021cd2018-03-20 12:40:55 -0600192 trimmed_part_number[i] = '*';
Richard Spiegelbd654802018-02-22 10:03:39 -0700193 }
194 }
Raul E Rangel50021cd2018-03-20 12:40:55 -0600195
Lijian Zhaob1fc13a2018-03-26 18:27:02 -0700196 if (len == 0) {
197 /* Null String in Part Number will have "None" instead. */
198 t->part_number = smbios_add_string(t->eos, "None");
199 } else if (invalid) {
Jacob Garber9172b692019-06-26 16:18:16 -0600200 char string_buffer[sizeof(trimmed_part_number) + 10];
Richard Spiegelbd654802018-02-22 10:03:39 -0700201
202 snprintf(string_buffer, sizeof(string_buffer), "Invalid (%s)",
Raul E Rangel50021cd2018-03-20 12:40:55 -0600203 trimmed_part_number);
Richard Spiegelbd654802018-02-22 10:03:39 -0700204 t->part_number = smbios_add_string(t->eos, string_buffer);
Raul E Rangel50021cd2018-03-20 12:40:55 -0600205 } else {
206 t->part_number = smbios_add_string(t->eos, trimmed_part_number);
207 }
Richard Spiegelbd654802018-02-22 10:03:39 -0700208}
209
Raul E Rangel99f54a62018-04-11 10:58:14 -0600210/* Encodes the SPD serial number into hex */
211static void smbios_fill_dimm_serial_number(const struct dimm_info *dimm,
212 struct smbios_type17 *t)
213{
214 char serial[9];
215
216 snprintf(serial, sizeof(serial), "%02hhx%02hhx%02hhx%02hhx",
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200217 dimm->serial[0], dimm->serial[1], dimm->serial[2], dimm->serial[3]);
Raul E Rangel99f54a62018-04-11 10:58:14 -0600218
219 t->serial_number = smbios_add_string(t->eos, serial);
220}
221
Kane Chen33faac62014-07-27 12:54:44 -0700222static int create_smbios_type17_for_dimm(struct dimm_info *dimm,
Patrick Rudolph5e007802020-07-27 15:37:43 +0200223 unsigned long *current, int *handle,
224 int type16_handle)
Kane Chen33faac62014-07-27 12:54:44 -0700225{
Subrata Banik6de8b422021-10-26 20:46:21 +0530226 struct spd_info info;
227 get_spd_info(dimm->ddr_type, dimm->mod_type, &info);
228
Angel Ponsd62a5012021-06-28 17:18:06 +0200229 struct smbios_type17 *t = smbios_carve_table(*current, SMBIOS_MEMORY_DEVICE,
230 sizeof(*t), *handle);
Kane Chen33faac62014-07-27 12:54:44 -0700231
Kane Chen33faac62014-07-27 12:54:44 -0700232 t->memory_type = dimm->ddr_type;
Rob Barnes327f1052020-09-01 10:26:57 -0600233 if (dimm->configured_speed_mts != 0)
234 t->clock_speed = dimm->configured_speed_mts;
235 else
236 t->clock_speed = dimm->ddr_frequency;
237 if (dimm->max_speed_mts != 0)
238 t->speed = dimm->max_speed_mts;
239 else
240 t->speed = dimm->ddr_frequency;
Julien Viard de Galbert99891712018-01-24 11:04:46 +0100241 if (dimm->dimm_size < 0x7fff) {
242 t->size = dimm->dimm_size;
243 } else {
244 t->size = 0x7fff;
245 t->extended_size = dimm->dimm_size & 0x7fffffff;
246 }
Kane Chen33faac62014-07-27 12:54:44 -0700247 t->data_width = 8 * (1 << (dimm->bus_width & 0x7));
248 t->total_width = t->data_width + 8 * ((dimm->bus_width & 0x18) >> 3);
Subrata Banik6de8b422021-10-26 20:46:21 +0530249 t->form_factor = info.form_factor;
Kane Chen33faac62014-07-27 12:54:44 -0700250
Timothy Pearson4785f2a2015-03-27 23:05:36 -0500251 smbios_fill_dimm_manufacturer_from_id(dimm->mod_id, t);
Raul E Rangel99f54a62018-04-11 10:58:14 -0600252 smbios_fill_dimm_serial_number(dimm, t);
Tim Chu31b42092020-12-23 19:15:21 -0800253 smbios_fill_dimm_asset_tag(dimm, t);
Lijian Zhao10ea93c2019-04-11 00:45:10 -0700254 smbios_fill_dimm_locator(dimm, t);
Kane Chen33faac62014-07-27 12:54:44 -0700255
256 /* put '\0' in the end of data */
Richard Spiegelbd654802018-02-22 10:03:39 -0700257 dimm->module_part_number[DIMM_INFO_PART_NUMBER_SIZE - 1] = '\0';
258 smbios_fill_dimm_part_number((char *)dimm->module_part_number, t);
Kane Chen33faac62014-07-27 12:54:44 -0700259
Christian Walterf9723222019-05-28 10:37:24 +0200260 /* Voltage Levels */
261 t->configured_voltage = dimm->vdd_voltage;
262 t->minimum_voltage = dimm->vdd_voltage;
263 t->maximum_voltage = dimm->vdd_voltage;
264
Tim Chu0c094ae2021-01-12 01:44:37 -0800265 /* Fill in type detail */
Subrata Banik6de8b422021-10-26 20:46:21 +0530266 t->type_detail = info.type_detail;
267
Kane Chen33faac62014-07-27 12:54:44 -0700268 /* Synchronous = 1 */
Tim Chu0c094ae2021-01-12 01:44:37 -0800269 t->type_detail |= MEMORY_TYPE_DETAIL_SYNCHRONOUS;
Kane Chen33faac62014-07-27 12:54:44 -0700270 /* no handle for error information */
271 t->memory_error_information_handle = 0xFFFE;
272 t->attributes = dimm->rank_per_dimm;
Patrick Rudolph5e007802020-07-27 15:37:43 +0200273 t->phys_memory_array_handle = type16_handle;
274
Kane Chen33faac62014-07-27 12:54:44 -0700275 *handle += 1;
Angel Ponsa37701a2021-06-28 17:36:53 +0200276 return smbios_full_table_len(&t->header, t->eos);
Kane Chen33faac62014-07-27 12:54:44 -0700277}
278
Angel Pons2b482582022-05-03 18:12:18 +0200279static int create_smbios_type17_for_empty_slot(struct dimm_info *dimm,
280 unsigned long *current, int *handle,
281 int type16_handle)
282{
283 struct smbios_type17 *t = smbios_carve_table(*current, SMBIOS_MEMORY_DEVICE,
284 sizeof(*t), *handle);
285 t->phys_memory_array_handle = type16_handle;
286 /* no handle for error information */
287 t->memory_error_information_handle = 0xfffe;
288 t->total_width = 0xffff; /* Unknown */
289 t->data_width = 0xffff; /* Unknown */
290 t->form_factor = 0x2; /* Unknown */
291 smbios_fill_dimm_locator(dimm, t); /* Device and Bank */
292 t->memory_type = 0x2; /* Unknown */
293 t->type_detail = 0x2; /* Unknown */
294
295 *handle += 1;
296 return smbios_full_table_len(&t->header, t->eos);
297}
298
Johnny Linc746a742020-06-03 11:44:22 +0800299#define VERSION_VPD "firmware_version"
300static const char *vpd_get_bios_version(void)
301{
302 int size;
303 const char *s;
304 char *version;
305
306 s = vpd_find(VERSION_VPD, &size, VPD_RO);
307 if (!s) {
308 printk(BIOS_ERR, "Find version from VPD %s failed\n", VERSION_VPD);
309 return NULL;
310 }
311
312 version = malloc(size + 1);
313 if (!version) {
314 printk(BIOS_ERR, "Failed to malloc %d bytes for VPD version\n", size + 1);
315 return NULL;
316 }
317 memcpy(version, s, size);
318 version[size] = '\0';
319 printk(BIOS_DEBUG, "Firmware version %s from VPD %s\n", version, VERSION_VPD);
320 return version;
321}
322
323static const char *get_bios_version(void)
324{
325 const char *s;
326
327#define SPACES \
328 " "
329
330 if (CONFIG(CHROMEOS))
331 return SPACES;
332
333 if (CONFIG(VPD_SMBIOS_VERSION)) {
334 s = vpd_get_bios_version();
335 if (s != NULL)
336 return s;
337 }
338
339 s = smbios_mainboard_bios_version();
340 if (s != NULL)
341 return s;
342
343 if (strlen(CONFIG_LOCALVERSION) != 0) {
344 printk(BIOS_DEBUG, "BIOS version set to CONFIG_LOCALVERSION: '%s'\n",
345 CONFIG_LOCALVERSION);
346 return CONFIG_LOCALVERSION;
347 }
348
349 printk(BIOS_DEBUG, "SMBIOS firmware version is set to coreboot_version: '%s'\n",
350 coreboot_version);
351 return coreboot_version;
352}
353
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200354static int smbios_write_type0(unsigned long *current, int handle)
355{
Angel Ponsd62a5012021-06-28 17:18:06 +0200356 struct smbios_type0 *t = smbios_carve_table(*current, SMBIOS_BIOS_INFORMATION,
357 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200358
359 t->vendor = smbios_add_string(t->eos, "coreboot");
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +0200360 t->bios_release_date = smbios_add_string(t->eos, coreboot_dmi_date);
Christian Gmeiner5e272a42013-02-04 16:22:46 +0100361
Kyösti Mälkki84d10cc2021-02-10 17:53:34 +0200362 if (CONFIG(CHROMEOS_NVS)) {
Kyösti Mälkki0fcbd3a2020-12-20 08:27:21 +0200363 uintptr_t version_address = (uintptr_t)t->eos;
364 /* SMBIOS offsets start at 1 rather than 0 */
365 version_address += (u32)smbios_string_table_len(t->eos) - 1;
366 smbios_type0_bios_version(version_address);
367 }
Johnny Linc746a742020-06-03 11:44:22 +0800368 t->bios_version = smbios_add_string(t->eos, get_bios_version());
Lee Leahy67358712016-06-08 12:47:07 -0700369 uint32_t rom_size = CONFIG_ROM_SIZE;
370 rom_size = MIN(CONFIG_ROM_SIZE, 16 * MiB);
371 t->bios_rom_size = (rom_size / 65535) - 1;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800372
Fabio Aiuto0805c702022-08-19 17:25:25 +0200373 if (CONFIG_ROM_SIZE >= 1 * GiB)
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200374 t->extended_bios_rom_size = DIV_ROUND_UP(CONFIG_ROM_SIZE, GiB) | (1 << 14);
Fabio Aiuto0805c702022-08-19 17:25:25 +0200375 else
Elyes HAOUAS358cbb32019-02-14 14:19:22 +0100376 t->extended_bios_rom_size = DIV_ROUND_UP(CONFIG_ROM_SIZE, MiB);
Elyes HAOUAS358cbb32019-02-14 14:19:22 +0100377
Elyes HAOUAS94ad3762019-02-15 17:39:56 +0100378 t->system_bios_major_release = coreboot_major_revision;
379 t->system_bios_minor_release = coreboot_minor_revision;
380
Tim Chuf2f53c42020-09-07 02:30:19 -0700381 smbios_ec_revision(&t->ec_major_release, &t->ec_minor_release);
382
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200383 t->bios_characteristics =
384 BIOS_CHARACTERISTICS_PCI_SUPPORTED |
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200385 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
386 BIOS_CHARACTERISTICS_UPGRADEABLE;
387
Julius Wernercd49cce2019-03-05 16:53:33 -0800388 if (CONFIG(CARDBUS_PLUGIN_SUPPORT))
Martin Roth898a7752017-06-01 11:39:59 -0600389 t->bios_characteristics |= BIOS_CHARACTERISTICS_PC_CARD;
390
Julius Wernercd49cce2019-03-05 16:53:33 -0800391 if (CONFIG(HAVE_ACPI_TABLES))
Martin Roth898a7752017-06-01 11:39:59 -0600392 t->bios_characteristics_ext1 = BIOS_EXT1_CHARACTERISTICS_ACPI;
393
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200394 t->bios_characteristics_ext2 = BIOS_EXT2_CHARACTERISTICS_TARGET;
Angel Ponsa37701a2021-06-28 17:36:53 +0200395 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200396 *current += len;
397 return len;
398}
399
Elyes HAOUAS28fa33c2019-01-25 13:46:43 +0100400static int get_socket_type(void)
401{
402 if (CONFIG(CPU_INTEL_SLOT_1))
Li, Jinchengaa990122023-01-03 15:43:40 +0800403 return PROCESSOR_UPGRADE_SLOT_1;
Kyösti Mälkki7b73e8522022-11-08 04:43:41 +0000404 if (CONFIG(CPU_INTEL_SOCKET_MPGA604))
Li, Jinchengaa990122023-01-03 15:43:40 +0800405 return PROCESSOR_UPGRADE_SOCKET_MPGA604;
Elyes HAOUAS28fa33c2019-01-25 13:46:43 +0100406 if (CONFIG(CPU_INTEL_SOCKET_LGA775))
Li, Jinchengaa990122023-01-03 15:43:40 +0800407 return PROCESSOR_UPGRADE_SOCKET_LGA775;
Zhixing Ma42bb7df2022-09-30 14:22:41 -0700408 if (CONFIG(SOC_INTEL_ALDERLAKE))
Li, Jinchengaa990122023-01-03 15:43:40 +0800409 return PROCESSOR_UPGRADE_SOCKET_LGA1700;
Jay Patelb7da7d52023-03-13 11:29:59 -0700410 if (CONFIG(SOC_INTEL_METEORLAKE))
411 return PROCESSOR_UPGRADE_OTHER;
David Hendricks60416992023-01-28 15:35:37 -0800412 if (CONFIG(SOC_INTEL_SKYLAKE_SP))
413 return PROCESSOR_UPGRADE_SOCKET_LGA3647_1;
414 if (CONFIG(SOC_INTEL_COOPERLAKE_SP))
415 return PROCESSOR_UPGRADE_SOCKET_LGA4189;
Christian Walter55129b32022-09-08 13:17:50 +0200416 if (CONFIG(SOC_INTEL_SAPPHIRERAPIDS_SP))
417 return PROCESSOR_UPGRADE_SOCKET_LGA4677;
Elyes HAOUAS28fa33c2019-01-25 13:46:43 +0100418
Li, Jinchengaa990122023-01-03 15:43:40 +0800419 return PROCESSOR_UPGRADE_UNKNOWN;
Elyes HAOUAS28fa33c2019-01-25 13:46:43 +0100420}
421
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800422unsigned int __weak smbios_processor_external_clock(void)
423{
424 return 0; /* Unknown */
425}
426
427unsigned int __weak smbios_processor_characteristics(void)
428{
429 return 0;
430}
431
432unsigned int __weak smbios_processor_family(struct cpuid_result res)
433{
434 return (res.eax > 0) ? 0x0c : 0x6;
435}
436
Morgan Jang8ae391d2020-10-06 16:26:17 +0800437unsigned int __weak smbios_cache_error_correction_type(u8 level)
438{
439 return SMBIOS_CACHE_ERROR_CORRECTION_UNKNOWN;
440}
441
442unsigned int __weak smbios_cache_sram_type(void)
443{
444 return SMBIOS_CACHE_SRAM_TYPE_UNKNOWN;
445}
446
447unsigned int __weak smbios_cache_conf_operation_mode(u8 level)
448{
449 return SMBIOS_CACHE_OP_MODE_UNKNOWN; /* Unknown */
450}
451
Patrick Rudolphb01ac7e2020-07-26 14:23:37 +0200452/* Returns the processor voltage in 100mV units */
453unsigned int __weak smbios_cpu_get_voltage(void)
454{
455 return 0; /* Unknown */
456}
457
Subrata Banike2b5fee2021-07-23 21:02:45 +0530458static size_t get_number_of_caches(size_t max_logical_cpus_sharing_cache)
Morgan Jang8ae391d2020-10-06 16:26:17 +0800459{
Morgan Jang8ae391d2020-10-06 16:26:17 +0800460 size_t number_of_cpus_per_package = 0;
461 size_t max_logical_cpus_per_package = 0;
462 struct cpuid_result res;
463
464 if (!cpu_have_cpuid())
465 return 1;
466
467 res = cpuid(1);
468
469 max_logical_cpus_per_package = (res.ebx >> 16) & 0xff;
470
Morgan Jang8ae391d2020-10-06 16:26:17 +0800471 /* Check if it's last level cache */
472 if (max_logical_cpus_sharing_cache == max_logical_cpus_per_package)
473 return 1;
474
475 if (cpuid_get_max_func() >= 0xb) {
476 res = cpuid_ext(0xb, 1);
477 number_of_cpus_per_package = res.ebx & 0xff;
478 } else {
479 number_of_cpus_per_package = max_logical_cpus_per_package;
480 }
481
482 return number_of_cpus_per_package / max_logical_cpus_sharing_cache;
483}
484
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200485static int smbios_write_type1(unsigned long *current, int handle)
486{
Angel Ponsd62a5012021-06-28 17:18:06 +0200487 struct smbios_type1 *t = smbios_carve_table(*current, SMBIOS_SYSTEM_INFORMATION,
488 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200489
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200490 t->manufacturer = smbios_add_string(t->eos, smbios_system_manufacturer());
491 t->product_name = smbios_add_string(t->eos, smbios_system_product_name());
492 t->serial_number = smbios_add_string(t->eos, smbios_system_serial_number());
Tim Chu13ab1d72021-10-19 01:45:12 +0000493 t->wakeup_type = smbios_system_wakeup_type();
Nico Huberebd8a4f2017-11-01 09:49:16 +0100494 t->sku = smbios_add_string(t->eos, smbios_system_sku());
495 t->version = smbios_add_string(t->eos, smbios_system_version());
Marc Jonesf43ba9c2015-06-09 21:10:43 -0600496#ifdef CONFIG_MAINBOARD_FAMILY
Nico Huberebd8a4f2017-11-01 09:49:16 +0100497 t->family = smbios_add_string(t->eos, CONFIG_MAINBOARD_FAMILY);
Marc Jonesf43ba9c2015-06-09 21:10:43 -0600498#endif
Nico Huberebd8a4f2017-11-01 09:49:16 +0100499 smbios_system_set_uuid(t->uuid);
Angel Ponsa37701a2021-06-28 17:36:53 +0200500 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200501 *current += len;
502 return len;
503}
504
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200505static int smbios_write_type2(unsigned long *current, int handle, const int chassis_handle)
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100506{
Angel Ponsd62a5012021-06-28 17:18:06 +0200507 struct smbios_type2 *t = smbios_carve_table(*current, SMBIOS_BOARD_INFORMATION,
508 sizeof(*t), handle);
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100509
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200510 t->manufacturer = smbios_add_string(t->eos, smbios_mainboard_manufacturer());
511 t->product_name = smbios_add_string(t->eos, smbios_mainboard_product_name());
512 t->serial_number = smbios_add_string(t->eos, smbios_mainboard_serial_number());
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100513 t->version = smbios_add_string(t->eos, smbios_mainboard_version());
Julien Viard de Galbert9a31dfe2018-02-22 16:39:58 +0100514 t->asset_tag = smbios_add_string(t->eos, smbios_mainboard_asset_tag());
515 t->feature_flags = smbios_mainboard_feature_flags();
516 t->location_in_chassis = smbios_add_string(t->eos,
517 smbios_mainboard_location_in_chassis());
518 t->board_type = smbios_mainboard_board_type();
519 t->chassis_handle = chassis_handle;
Angel Ponsa37701a2021-06-28 17:36:53 +0200520 const int len = smbios_full_table_len(&t->header, t->eos);
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100521 *current += len;
522 return len;
523}
524
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200525static int smbios_write_type3(unsigned long *current, int handle)
526{
Angel Ponsd62a5012021-06-28 17:18:06 +0200527 struct smbios_type3 *t = smbios_carve_table(*current, SMBIOS_SYSTEM_ENCLOSURE,
528 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200529
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200530 t->manufacturer = smbios_add_string(t->eos, smbios_system_manufacturer());
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200531 t->bootup_state = SMBIOS_STATE_SAFE;
532 t->power_supply_state = SMBIOS_STATE_SAFE;
533 t->thermal_state = SMBIOS_STATE_SAFE;
Mathew Kinga7d55cf2019-07-31 15:50:15 -0600534 t->_type = smbios_mainboard_enclosure_type();
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200535 t->security_status = SMBIOS_STATE_SAFE;
JingleHsuWiwynn4330b962021-01-28 09:13:42 +0800536 t->number_of_power_cords = smbios_chassis_power_cords();
Johnny Lind3440542020-01-30 18:21:22 +0800537 t->asset_tag_number = smbios_add_string(t->eos, smbios_mainboard_asset_tag());
538 t->version = smbios_add_string(t->eos, smbios_chassis_version());
539 t->serial_number = smbios_add_string(t->eos, smbios_chassis_serial_number());
Angel Ponsa37701a2021-06-28 17:36:53 +0200540 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200541 *current += len;
542 return len;
543}
544
Arthur Heymans13c8dc52022-04-07 22:08:23 +0200545#define MAX_CPUS_ENABLED (CONFIG_MAX_CPUS > 0xff ? 0xff : CONFIG_MAX_CPUS)
546
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200547static int smbios_write_type4(unsigned long *current, int handle)
548{
Patrick Rudolphb01ac7e2020-07-26 14:23:37 +0200549 unsigned int cpu_voltage;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200550 struct cpuid_result res;
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800551 uint16_t characteristics = 0;
Tim Chu27bf0c82020-09-16 00:34:08 -0700552 static unsigned int cnt = 0;
553 char buf[8];
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200554
Rudolf Marek06253cd2012-02-25 23:51:12 +0100555 /* Provide sane defaults even for CPU without CPUID */
556 res.eax = res.edx = 0;
557 res.ebx = 0x10000;
558
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700559 if (cpu_have_cpuid())
Rudolf Marek06253cd2012-02-25 23:51:12 +0100560 res = cpuid(1);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200561
Angel Ponsd62a5012021-06-28 17:18:06 +0200562 struct smbios_type4 *t = smbios_carve_table(*current, SMBIOS_PROCESSOR_INFORMATION,
563 sizeof(*t), handle);
Tim Chu27bf0c82020-09-16 00:34:08 -0700564
565 snprintf(buf, sizeof(buf), "CPU%d", cnt++);
566 t->socket_designation = smbios_add_string(t->eos, buf);
567
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200568 t->processor_id[0] = res.eax;
569 t->processor_id[1] = res.edx;
570 t->processor_manufacturer = smbios_cpu_vendor(t->eos);
571 t->processor_version = smbios_processor_name(t->eos);
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800572 t->processor_family = smbios_processor_family(res);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200573 t->processor_type = 3; /* System Processor */
Andrey Petrov515ef382019-11-15 13:19:08 -0800574 /*
575 * If CPUID leaf 11 is available, calculate "core count" by dividing
576 * SMT_ID (logical processors in a core) by Core_ID (number of cores).
577 * This seems to be the way to arrive to a number of cores mentioned on
578 * ark.intel.com.
579 */
580 if (cpu_have_cpuid() && cpuid_get_max_func() >= 0xb) {
581 uint32_t leaf_b_cores = 0, leaf_b_threads = 0;
582 res = cpuid_ext(0xb, 1);
583 leaf_b_cores = res.ebx;
584 res = cpuid_ext(0xb, 0);
585 leaf_b_threads = res.ebx;
586 /* if hyperthreading is not available, pretend this is 1 */
Fabio Aiuto0805c702022-08-19 17:25:25 +0200587 if (leaf_b_threads == 0)
Andrey Petrov515ef382019-11-15 13:19:08 -0800588 leaf_b_threads = 1;
Fabio Aiuto0805c702022-08-19 17:25:25 +0200589
Patrick Rudolph7a835822020-07-22 16:00:53 +0200590 t->core_count2 = leaf_b_cores / leaf_b_threads;
591 t->core_count = t->core_count2 > 0xff ? 0xff : t->core_count2;
Francois Toguo597922e2019-03-27 18:13:07 -0700592 t->thread_count2 = leaf_b_cores;
593 t->thread_count = t->thread_count2 > 0xff ? 0xff : t->thread_count2;
Andrey Petrov515ef382019-11-15 13:19:08 -0800594 } else {
595 t->core_count = (res.ebx >> 16) & 0xff;
Patrick Rudolph7a835822020-07-22 16:00:53 +0200596 t->core_count2 = t->core_count;
597 t->thread_count2 = t->core_count2;
Francois Toguo597922e2019-03-27 18:13:07 -0700598 t->thread_count = t->thread_count2;
Andrey Petrov515ef382019-11-15 13:19:08 -0800599 }
Andrey Petrov2e032f02019-10-23 15:31:51 -0700600 /* Assume we enable all the cores always, capped only by MAX_CPUS */
Arthur Heymans13c8dc52022-04-07 22:08:23 +0200601 t->core_enabled = MIN(t->core_count, MAX_CPUS_ENABLED);
Patrick Rudolph7a835822020-07-22 16:00:53 +0200602 t->core_enabled2 = MIN(t->core_count2, CONFIG_MAX_CPUS);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200603 t->l1_cache_handle = 0xffff;
604 t->l2_cache_handle = 0xffff;
605 t->l3_cache_handle = 0xffff;
Johnny Lind3440542020-01-30 18:21:22 +0800606 t->serial_number = smbios_add_string(t->eos, smbios_processor_serial_number());
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200607 t->status = SMBIOS_PROCESSOR_STATUS_CPU_ENABLED | SMBIOS_PROCESSOR_STATUS_POPULATED;
Elyes HAOUAS28fa33c2019-01-25 13:46:43 +0100608 t->processor_upgrade = get_socket_type();
Andrey Petrov2e032f02019-10-23 15:31:51 -0700609 if (cpu_have_cpuid() && cpuid_get_max_func() >= 0x16) {
Andrey Petrov2e032f02019-10-23 15:31:51 -0700610 t->current_speed = cpuid_eax(0x16); /* base frequency */
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800611 t->external_clock = cpuid_ecx(0x16);
Andrey Petrov2e032f02019-10-23 15:31:51 -0700612 } else {
Andrey Petrov2e032f02019-10-23 15:31:51 -0700613 t->current_speed = smbios_cpu_get_current_speed_mhz();
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800614 t->external_clock = smbios_processor_external_clock();
Andrey Petrov2e032f02019-10-23 15:31:51 -0700615 }
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800616
Tim Chu40d45992020-12-14 21:44:40 -0800617 /* This field identifies a capability for the system, not the processor itself. */
618 t->max_speed = smbios_cpu_get_max_speed_mhz();
619
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800620 if (cpu_have_cpuid()) {
621 res = cpuid(1);
622
623 if ((res.ecx) & BIT(5))
624 characteristics |= BIT(6); /* BIT6: Enhanced Virtualization */
625
626 if ((res.edx) & BIT(28))
627 characteristics |= BIT(4); /* BIT4: Hardware Thread */
628
629 if (((cpuid_eax(0x80000000) - 0x80000000) + 1) > 2) {
630 res = cpuid(0x80000001);
631
632 if ((res.edx) & BIT(20))
633 characteristics |= BIT(5); /* BIT5: Execute Protection */
634 }
635 }
636 t->processor_characteristics = characteristics | smbios_processor_characteristics();
Patrick Rudolphb01ac7e2020-07-26 14:23:37 +0200637 cpu_voltage = smbios_cpu_get_voltage();
638 if (cpu_voltage > 0)
639 t->voltage = 0x80 | cpu_voltage;
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800640
Angel Ponsa37701a2021-06-28 17:36:53 +0200641 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200642 *current += len;
643 return len;
644}
645
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100646/*
647 * Write SMBIOS type 7.
648 * Fill in some fields with constant values, as gathering the information
649 * from CPUID is impossible.
650 */
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200651static int smbios_write_type7(unsigned long *current,
652 const int handle,
653 const u8 level,
654 const u8 sram_type,
655 const enum smbios_cache_associativity associativity,
656 const enum smbios_cache_type type,
657 const size_t max_cache_size,
658 const size_t cache_size)
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100659{
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100660 char buf[8];
661
Angel Ponsd62a5012021-06-28 17:18:06 +0200662 struct smbios_type7 *t = smbios_carve_table(*current, SMBIOS_CACHE_INFORMATION,
663 sizeof(*t), handle);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100664
Morgan Jang8ae391d2020-10-06 16:26:17 +0800665 snprintf(buf, sizeof(buf), "CACHE%x", level);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100666 t->socket_designation = smbios_add_string(t->eos, buf);
667
668 t->cache_configuration = SMBIOS_CACHE_CONF_LEVEL(level) |
669 SMBIOS_CACHE_CONF_LOCATION(0) | /* Internal */
670 SMBIOS_CACHE_CONF_ENABLED(1) | /* Enabled */
Morgan Jang8ae391d2020-10-06 16:26:17 +0800671 SMBIOS_CACHE_CONF_OPERATION_MODE(smbios_cache_conf_operation_mode(level));
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100672
673 if (max_cache_size < (SMBIOS_CACHE_SIZE_MASK * KiB)) {
674 t->max_cache_size = max_cache_size / KiB;
675 t->max_cache_size2 = t->max_cache_size;
676
677 t->max_cache_size |= SMBIOS_CACHE_SIZE_UNIT_1KB;
678 t->max_cache_size2 |= SMBIOS_CACHE_SIZE2_UNIT_1KB;
679 } else {
Patrick Rudolph8f702672019-04-13 09:44:02 +0200680 if (max_cache_size < (SMBIOS_CACHE_SIZE_MASK * 64 * KiB))
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100681 t->max_cache_size = max_cache_size / (64 * KiB);
682 else
683 t->max_cache_size = SMBIOS_CACHE_SIZE_OVERFLOW;
684 t->max_cache_size2 = max_cache_size / (64 * KiB);
685
686 t->max_cache_size |= SMBIOS_CACHE_SIZE_UNIT_64KB;
687 t->max_cache_size2 |= SMBIOS_CACHE_SIZE2_UNIT_64KB;
688 }
689
690 if (cache_size < (SMBIOS_CACHE_SIZE_MASK * KiB)) {
691 t->installed_size = cache_size / KiB;
692 t->installed_size2 = t->installed_size;
693
694 t->installed_size |= SMBIOS_CACHE_SIZE_UNIT_1KB;
695 t->installed_size2 |= SMBIOS_CACHE_SIZE2_UNIT_1KB;
696 } else {
697 if (cache_size < (SMBIOS_CACHE_SIZE_MASK * 64 * KiB))
698 t->installed_size = cache_size / (64 * KiB);
699 else
700 t->installed_size = SMBIOS_CACHE_SIZE_OVERFLOW;
701 t->installed_size2 = cache_size / (64 * KiB);
702
703 t->installed_size |= SMBIOS_CACHE_SIZE_UNIT_64KB;
704 t->installed_size2 |= SMBIOS_CACHE_SIZE2_UNIT_64KB;
705 }
706
707 t->associativity = associativity;
708 t->supported_sram_type = sram_type;
709 t->current_sram_type = sram_type;
710 t->cache_speed = 0; /* Unknown */
Morgan Jang8ae391d2020-10-06 16:26:17 +0800711 t->error_correction_type = smbios_cache_error_correction_type(level);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100712 t->system_cache_type = type;
713
Angel Ponsa37701a2021-06-28 17:36:53 +0200714 const int len = smbios_full_table_len(&t->header, t->eos);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100715 *current += len;
716 return len;
717}
718
719/* Convert the associativity as integer to the SMBIOS enum if available */
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200720static enum smbios_cache_associativity smbios_cache_associativity(const u8 num)
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100721{
722 switch (num) {
723 case 1:
724 return SMBIOS_CACHE_ASSOCIATIVITY_DIRECT;
725 case 2:
726 return SMBIOS_CACHE_ASSOCIATIVITY_2WAY;
727 case 4:
728 return SMBIOS_CACHE_ASSOCIATIVITY_4WAY;
729 case 8:
730 return SMBIOS_CACHE_ASSOCIATIVITY_8WAY;
731 case 12:
732 return SMBIOS_CACHE_ASSOCIATIVITY_12WAY;
733 case 16:
734 return SMBIOS_CACHE_ASSOCIATIVITY_16WAY;
735 case 20:
736 return SMBIOS_CACHE_ASSOCIATIVITY_20WAY;
737 case 24:
738 return SMBIOS_CACHE_ASSOCIATIVITY_24WAY;
739 case 32:
740 return SMBIOS_CACHE_ASSOCIATIVITY_32WAY;
741 case 48:
742 return SMBIOS_CACHE_ASSOCIATIVITY_48WAY;
743 case 64:
744 return SMBIOS_CACHE_ASSOCIATIVITY_64WAY;
745 case 0xff:
746 return SMBIOS_CACHE_ASSOCIATIVITY_FULL;
747 default:
748 return SMBIOS_CACHE_ASSOCIATIVITY_UNKNOWN;
749 };
750}
751
752/*
753 * Parse the "Deterministic Cache Parameters" as provided by Intel in
754 * leaf 4 or AMD in extended leaf 0x8000001d.
755 *
756 * @param current Pointer to memory address to write the tables to
757 * @param handle Pointer to handle for the tables
758 * @param max_struct_size Pointer to maximum struct size
Patrick Rudolph15589b42019-03-30 17:51:06 +0100759 * @param type4 Pointer to SMBIOS type 4 structure
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100760 */
761static int smbios_write_type7_cache_parameters(unsigned long *current,
762 int *handle,
Patrick Rudolph15589b42019-03-30 17:51:06 +0100763 int *max_struct_size,
764 struct smbios_type4 *type4)
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100765{
Subrata Banike2b5fee2021-07-23 21:02:45 +0530766 unsigned int cnt = CACHE_L1D;
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100767 int len = 0;
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100768
769 if (!cpu_have_cpuid())
770 return len;
771
Subrata Banik6efc4aa2021-09-01 12:40:47 +0530772 enum cpu_type dcache_cpuid = cpu_check_deterministic_cache_cpuid_supported();
773 if (dcache_cpuid == CPUID_TYPE_INVALID || dcache_cpuid == CPUID_COMMAND_UNSUPPORTED) {
Subrata Banik5586c792021-09-02 13:17:18 +0530774 printk(BIOS_DEBUG, "SMBIOS: Unknown CPU or CPU doesn't support Deterministic "
775 "Cache CPUID leaf\n");
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100776 return len;
777 }
778
779 while (1) {
780 enum smbios_cache_associativity associativity;
781 enum smbios_cache_type type;
Subrata Banike2b5fee2021-07-23 21:02:45 +0530782 struct cpu_cache_info info;
783 if (!fill_cpu_cache_info(cnt++, &info))
784 continue;
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100785
Subrata Banike2b5fee2021-07-23 21:02:45 +0530786 const u8 cache_type = info.type;
787 const u8 level = info.level;
788 const size_t assoc = info.num_ways;
789 const size_t cache_share = info.num_cores_shared;
790 const size_t cache_size = info.size * get_number_of_caches(cache_share);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100791
792 if (!cache_type)
793 /* No more caches in the system */
794 break;
795
796 switch (cache_type) {
797 case 1:
798 type = SMBIOS_CACHE_TYPE_DATA;
799 break;
800 case 2:
801 type = SMBIOS_CACHE_TYPE_INSTRUCTION;
802 break;
803 case 3:
804 type = SMBIOS_CACHE_TYPE_UNIFIED;
805 break;
806 default:
807 type = SMBIOS_CACHE_TYPE_UNKNOWN;
808 break;
809 }
810
Subrata Banike2b5fee2021-07-23 21:02:45 +0530811 if (info.fully_associative)
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100812 associativity = SMBIOS_CACHE_ASSOCIATIVITY_FULL;
813 else
814 associativity = smbios_cache_associativity(assoc);
815
Patrick Rudolph15589b42019-03-30 17:51:06 +0100816 const int h = (*handle)++;
817
818 update_max(len, *max_struct_size, smbios_write_type7(current, h,
Morgan Jang8ae391d2020-10-06 16:26:17 +0800819 level, smbios_cache_sram_type(), associativity,
Patrick Rudolph15589b42019-03-30 17:51:06 +0100820 type, cache_size, cache_size));
821
822 if (type4) {
823 switch (level) {
824 case 1:
825 type4->l1_cache_handle = h;
826 break;
827 case 2:
828 type4->l2_cache_handle = h;
829 break;
830 case 3:
831 type4->l3_cache_handle = h;
832 break;
833 }
834 }
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100835 };
836
837 return len;
838}
BryantOu4a8e58f2020-04-20 19:45:20 -0700839
840int smbios_write_type8(unsigned long *current, int *handle,
841 const struct port_information *port,
842 size_t num_ports)
843{
BryantOu4a8e58f2020-04-20 19:45:20 -0700844 unsigned int totallen = 0, i;
845
846 for (i = 0; i < num_ports; i++, port++) {
Angel Ponsd62a5012021-06-28 17:18:06 +0200847 struct smbios_type8 *t = smbios_carve_table(*current,
848 SMBIOS_PORT_CONNECTOR_INFORMATION,
849 sizeof(*t), *handle);
BryantOu4a8e58f2020-04-20 19:45:20 -0700850 t->internal_reference_designator =
851 smbios_add_string(t->eos, port->internal_reference_designator);
852 t->internal_connector_type = port->internal_connector_type;
853 t->external_reference_designator =
854 smbios_add_string(t->eos, port->external_reference_designator);
855 t->external_connector_type = port->external_connector_type;
856 t->port_type = port->port_type;
857 *handle += 1;
Angel Ponsa37701a2021-06-28 17:36:53 +0200858 const int len = smbios_full_table_len(&t->header, t->eos);
859 *current += len;
860 totallen += len;
BryantOu4a8e58f2020-04-20 19:45:20 -0700861 }
862 return totallen;
863}
864
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700865int smbios_write_type9(unsigned long *current, int *handle,
866 const char *name, const enum misc_slot_type type,
867 const enum slot_data_bus_bandwidth bandwidth,
868 const enum misc_slot_usage usage,
869 const enum misc_slot_length length,
JingleHsuWiwynn20fa59f2021-01-26 09:55:34 +0800870 const u16 id, u8 slot_char1, u8 slot_char2, u8 bus, u8 dev_func)
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700871{
Angel Ponsd62a5012021-06-28 17:18:06 +0200872 struct smbios_type9 *t = smbios_carve_table(*current, SMBIOS_SYSTEM_SLOTS,
873 sizeof(*t), *handle);
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700874
Angel Pons3c13da72020-07-29 18:23:23 +0200875 t->slot_designation = smbios_add_string(t->eos, name ? name : "SLOT");
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700876 t->slot_type = type;
877 /* TODO add slot_id supoort, will be "_SUN" for ACPI devices */
JingleHsuWiwynn20fa59f2021-01-26 09:55:34 +0800878 t->slot_id = id;
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700879 t->slot_data_bus_width = bandwidth;
880 t->current_usage = usage;
881 t->slot_length = length;
882 t->slot_characteristics_1 = slot_char1;
883 t->slot_characteristics_2 = slot_char2;
884 t->segment_group_number = 0;
885 t->bus_number = bus;
886 t->device_function_number = dev_func;
887 t->data_bus_width = SlotDataBusWidthOther;
888
Angel Ponsa37701a2021-06-28 17:36:53 +0200889 const int len = smbios_full_table_len(&t->header, t->eos);
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700890 *current += len;
891 *handle += 1;
892 return len;
893}
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100894
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200895static int smbios_write_type11(unsigned long *current, int *handle)
Peter Stugec392b642013-07-06 19:51:12 +0200896{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100897 struct device *dev;
Angel Ponsd62a5012021-06-28 17:18:06 +0200898 struct smbios_type11 *t = smbios_carve_table(*current, SMBIOS_OEM_STRINGS,
899 sizeof(*t), *handle);
Peter Stugec392b642013-07-06 19:51:12 +0200900
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200901 for (dev = all_devices; dev; dev = dev->next) {
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200902 if (dev->ops && dev->ops->get_smbios_strings)
903 dev->ops->get_smbios_strings(dev, t);
904 }
905
906 if (t->count == 0) {
Lee Leahy0b5678f2017-03-16 16:01:40 -0700907 memset(t, 0, sizeof(*t));
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200908 return 0;
909 }
Peter Stugec392b642013-07-06 19:51:12 +0200910
Angel Ponsa37701a2021-06-28 17:36:53 +0200911 const int len = smbios_full_table_len(&t->header, t->eos);
Peter Stugec392b642013-07-06 19:51:12 +0200912 *current += len;
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200913 (*handle)++;
Peter Stugec392b642013-07-06 19:51:12 +0200914 return len;
915}
916
Patrick Rudolph5e007802020-07-27 15:37:43 +0200917static int smbios_write_type16(unsigned long *current, int *handle)
918{
Patrick Rudolph5e007802020-07-27 15:37:43 +0200919 int i;
Tim Chu1ee8ddc2021-01-22 01:10:45 -0800920 uint64_t max_capacity;
Patrick Rudolph5e007802020-07-27 15:37:43 +0200921
922 struct memory_info *meminfo;
923 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
924 if (meminfo == NULL)
925 return 0; /* can't find mem info in cbmem */
926
927 printk(BIOS_INFO, "Create SMBIOS type 16\n");
928
929 if (meminfo->max_capacity_mib == 0 || meminfo->number_of_devices == 0) {
930 /* Fill in defaults if not provided */
931 meminfo->number_of_devices = 0;
932 meminfo->max_capacity_mib = 0;
933 for (i = 0; i < meminfo->dimm_cnt && i < ARRAY_SIZE(meminfo->dimm); i++) {
934 meminfo->max_capacity_mib += meminfo->dimm[i].dimm_size;
935 meminfo->number_of_devices += !!meminfo->dimm[i].dimm_size;
936 }
937 }
938
Angel Ponsd62a5012021-06-28 17:18:06 +0200939 struct smbios_type16 *t = smbios_carve_table(*current, SMBIOS_PHYS_MEMORY_ARRAY,
940 sizeof(*t), *handle);
Patrick Rudolph5e007802020-07-27 15:37:43 +0200941
942 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
943 t->use = MEMORY_ARRAY_USE_SYSTEM;
Angel Pons6e82ebf2021-01-31 15:15:11 +0100944 t->memory_error_correction = meminfo->ecc_type;
Patrick Rudolph5e007802020-07-27 15:37:43 +0200945
946 /* no error information handle available */
947 t->memory_error_information_handle = 0xFFFE;
Tim Chu1ee8ddc2021-01-22 01:10:45 -0800948 max_capacity = meminfo->max_capacity_mib;
949 if (max_capacity * (MiB / KiB) < SMBIOS_USE_EXTENDED_MAX_CAPACITY)
950 t->maximum_capacity = max_capacity * (MiB / KiB);
951 else {
952 t->maximum_capacity = SMBIOS_USE_EXTENDED_MAX_CAPACITY;
953 t->extended_maximum_capacity = max_capacity * MiB;
954 }
Patrick Rudolph5e007802020-07-27 15:37:43 +0200955 t->number_of_memory_devices = meminfo->number_of_devices;
956
Angel Ponsa37701a2021-06-28 17:36:53 +0200957 const int len = smbios_full_table_len(&t->header, t->eos);
Patrick Rudolph5e007802020-07-27 15:37:43 +0200958 *current += len;
959 (*handle)++;
960 return len;
961}
962
963static int smbios_write_type17(unsigned long *current, int *handle, int type16)
Kane Chen33faac62014-07-27 12:54:44 -0700964{
Iru Cai36775202016-03-09 23:22:58 +0800965 int totallen = 0;
Kane Chen33faac62014-07-27 12:54:44 -0700966 int i;
967
968 struct memory_info *meminfo;
969 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
970 if (meminfo == NULL)
971 return 0; /* can't find mem info in cbmem */
972
973 printk(BIOS_INFO, "Create SMBIOS type 17\n");
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200974 for (i = 0; i < meminfo->dimm_cnt && i < ARRAY_SIZE(meminfo->dimm); i++) {
Angel Pons2b482582022-05-03 18:12:18 +0200975 struct dimm_info *d = &meminfo->dimm[i];
Patrick Rudolph5e007802020-07-27 15:37:43 +0200976 /*
977 * Windows 10 GetPhysicallyInstalledSystemMemory functions reads SMBIOS tables
978 * type 16 and type 17. The type 17 tables need to point to a type 16 table.
979 * Otherwise, the physical installed memory size is guessed from the system
980 * memory map, which results in a slightly smaller value than the actual size.
981 */
Angel Pons2b482582022-05-03 18:12:18 +0200982 int len;
983 if (d->dimm_size > 0)
984 len = create_smbios_type17_for_dimm(d, current, handle, type16);
985 else
986 len = create_smbios_type17_for_empty_slot(d, current, handle, type16);
987
Kane Chen33faac62014-07-27 12:54:44 -0700988 *current += len;
Iru Cai36775202016-03-09 23:22:58 +0800989 totallen += len;
Kane Chen33faac62014-07-27 12:54:44 -0700990 }
Iru Cai36775202016-03-09 23:22:58 +0800991 return totallen;
Kane Chen33faac62014-07-27 12:54:44 -0700992}
993
Tim Chue41f5952020-11-02 21:33:52 -0800994static int smbios_write_type19(unsigned long *current, int *handle, int type16)
Patrick Rudolph604295e2020-07-21 14:53:37 +0200995{
Patrick Rudolph604295e2020-07-21 14:53:37 +0200996 int i;
997
998 struct memory_info *meminfo;
999 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
1000 if (meminfo == NULL)
1001 return 0; /* can't find mem info in cbmem */
1002
Angel Ponsd62a5012021-06-28 17:18:06 +02001003 struct smbios_type19 *t = smbios_carve_table(*current,
1004 SMBIOS_MEMORY_ARRAY_MAPPED_ADDRESS,
1005 sizeof(*t), *handle);
Patrick Rudolph604295e2020-07-21 14:53:37 +02001006
Tim Chue41f5952020-11-02 21:33:52 -08001007 t->memory_array_handle = type16;
Patrick Rudolph604295e2020-07-21 14:53:37 +02001008
1009 for (i = 0; i < meminfo->dimm_cnt && i < ARRAY_SIZE(meminfo->dimm); i++) {
1010 if (meminfo->dimm[i].dimm_size > 0) {
1011 t->extended_ending_address += meminfo->dimm[i].dimm_size;
1012 t->partition_width++;
1013 }
1014 }
1015 t->extended_ending_address *= MiB;
1016
1017 /* Check if it fits into regular address */
1018 if (t->extended_ending_address >= KiB &&
1019 t->extended_ending_address < 0x40000000000ULL) {
1020 /*
1021 * FIXME: The starting address is SoC specific, but SMBIOS tables are only
1022 * exported on x86 where it's always 0.
1023 */
1024
1025 t->starting_address = 0;
1026 t->ending_address = t->extended_ending_address / KiB - 1;
1027 t->extended_starting_address = ~0;
1028 t->extended_ending_address = ~0;
1029 } else {
1030 t->starting_address = ~0;
1031 t->ending_address = ~0;
1032 t->extended_starting_address = 0;
1033 t->extended_ending_address--;
1034 }
1035
Angel Ponsa37701a2021-06-28 17:36:53 +02001036 const int len = smbios_full_table_len(&t->header, t->eos);
Patrick Rudolph604295e2020-07-21 14:53:37 +02001037 *current += len;
1038 *handle += 1;
1039 return len;
1040}
1041
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001042static int smbios_write_type20_table(unsigned long *current, int *handle, u32 addr_start,
1043 u32 addr_end, int type17_handle, int type19_handle)
1044{
1045 struct smbios_type20 *t = smbios_carve_table(*current, SMBIOS_MEMORY_DEVICE_MAPPED_ADDRESS,
1046 sizeof(*t), *handle);
1047
1048 t->memory_device_handle = type17_handle;
1049 t->memory_array_mapped_address_handle = type19_handle;
1050 t->addr_start = addr_start;
1051 t->addr_end = addr_end;
1052 t->partition_row_pos = 0xff;
1053 t->interleave_pos = 0xff;
1054 t->interleave_depth = 0xff;
1055
1056 const int len = smbios_full_table_len(&t->header, t->eos);
1057 *current += len;
1058 *handle += 1;
1059 return len;
1060}
1061
1062static int smbios_write_type20(unsigned long *current, int *handle,
1063 int type17_handle, int type19_handle)
1064{
1065 u32 start_addr = 0;
1066 int totallen = 0;
1067 int i;
1068
1069 struct memory_info *meminfo;
1070 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
1071 if (meminfo == NULL)
1072 return 0; /* can't find mem info in cbmem */
1073
1074 printk(BIOS_INFO, "Create SMBIOS type 20\n");
1075 for (i = 0; i < meminfo->dimm_cnt && i < ARRAY_SIZE(meminfo->dimm); i++) {
1076 struct dimm_info *dimm;
1077 dimm = &meminfo->dimm[i];
Angel Pons2b482582022-05-03 18:12:18 +02001078 if (dimm->dimm_size == 0)
1079 continue;
1080
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001081 u32 end_addr = start_addr + (dimm->dimm_size << 10) - 1;
1082 totallen += smbios_write_type20_table(current, handle, start_addr, end_addr,
1083 type17_handle, type19_handle);
1084 start_addr = end_addr + 1;
1085 }
1086 return totallen;
1087}
1088
Erik van den Bogaert93781522022-09-28 12:35:51 +02001089int smbios_write_type28(unsigned long *current, int *handle,
1090 const char *name,
1091 const enum smbios_temp_location location,
1092 const enum smbios_temp_status status,
1093 u16 max_value, u16 min_value,
1094 u16 resolution, u16 tolerance,
1095 u16 accuracy,
1096 u32 oem,
1097 u16 nominal_value)
1098{
1099 struct smbios_type28 *t = smbios_carve_table(*current, SMBIOS_TEMPERATURE_PROBE,
1100 sizeof(*t), *handle);
1101
1102 t->description = smbios_add_string(t->eos, name ? name : "Temperature");
1103 t->location_and_status = location | (status << 5);
1104 t->maximum_value = max_value;
1105 t->minimum_value = min_value;
1106 t->resolution = resolution;
1107 t->tolerance = tolerance;
1108 t->accuracy = accuracy;
1109 t->oem_defined = oem;
1110 t->nominal_value = nominal_value;
1111
1112 const int len = smbios_full_table_len(&t->header, t->eos);
1113 *current += len;
1114 *handle += 1;
1115 return len;
1116}
1117
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001118static int smbios_write_type32(unsigned long *current, int handle)
1119{
Angel Ponsd62a5012021-06-28 17:18:06 +02001120 struct smbios_type32 *t = smbios_carve_table(*current, SMBIOS_SYSTEM_BOOT_INFORMATION,
1121 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001122
Angel Ponsa37701a2021-06-28 17:36:53 +02001123 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001124 *current += len;
1125 return len;
1126}
1127
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001128int smbios_write_type38(unsigned long *current, int *handle,
1129 const enum smbios_bmc_interface_type interface_type,
1130 const u8 ipmi_rev, const u8 i2c_addr, const u8 nv_addr,
1131 const u64 base_addr, const u8 base_modifier,
1132 const u8 irq)
1133{
Angel Ponsd62a5012021-06-28 17:18:06 +02001134 struct smbios_type38 *t = smbios_carve_table(*current, SMBIOS_IPMI_DEVICE_INFORMATION,
1135 sizeof(*t), *handle);
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001136
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001137 t->interface_type = interface_type;
1138 t->ipmi_rev = ipmi_rev;
1139 t->i2c_slave_addr = i2c_addr;
1140 t->nv_storage_addr = nv_addr;
1141 t->base_address = base_addr;
1142 t->base_address_modifier = base_modifier;
1143 t->irq = irq;
1144
Angel Ponsa37701a2021-06-28 17:36:53 +02001145 const int len = smbios_full_table_len(&t->header, t->eos);
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001146 *current += len;
1147 *handle += 1;
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001148 return len;
1149}
1150
Jonathan Zhangd57b8212022-10-14 17:06:26 -07001151int smbios_write_type39(unsigned long *current, int *handle,
1152 u8 unit_group, const char *loc, const char *dev_name,
1153 const char *man, const char *serial_num,
1154 const char *tag_num, const char *part_num,
1155 const char *rev_lvl, u16 max_pow_cap,
1156 const struct power_supply_ch *ps_ch)
1157{
1158 struct smbios_type39 *t = smbios_carve_table(*current,
1159 SMBIOS_SYSTEM_POWER_SUPPLY,
1160 sizeof(*t), *handle);
1161
1162 uint16_t val = 0;
1163 uint16_t ps_type, ps_status, vol_switch, ps_unplug, ps_present, hot_rep;
1164
1165 t->power_unit_group = unit_group;
1166 t->location = smbios_add_string(t->eos, loc);
1167 t->device_name = smbios_add_string(t->eos, dev_name);
1168 t->manufacturer = smbios_add_string(t->eos, man);
1169 t->serial_number = smbios_add_string(t->eos, serial_num);
1170 t->asset_tag_number = smbios_add_string(t->eos, tag_num);
1171 t->model_part_number = smbios_add_string(t->eos, part_num);
1172 t->revision_level = smbios_add_string(t->eos, rev_lvl);
1173 t->max_power_capacity = max_pow_cap;
1174
1175 ps_type = ps_ch->power_supply_type & 0xF;
1176 ps_status = ps_ch->power_supply_status & 0x7;
1177 vol_switch = ps_ch->input_voltage_range_switch & 0xF;
1178 ps_unplug = ps_ch->power_supply_unplugged & 0x1;
1179 ps_present = ps_ch->power_supply_present & 0x1;
1180 hot_rep = ps_ch->power_supply_hot_replaceble & 0x1;
1181
1182 val |= (ps_type << 10);
1183 val |= (ps_status << 7);
1184 val |= (vol_switch << 3);
1185 val |= (ps_unplug << 2);
1186 val |= (ps_present << 1);
1187 val |= hot_rep;
1188 t->power_supply_characteristics = val;
1189
1190 t->input_voltage_probe_handle = 0xFFFF;
1191 t->cooling_device_handle = 0xFFFF;
1192 t->input_current_probe_handle = 0xFFFF;
1193
1194 const int len = smbios_full_table_len(&t->header, t->eos);
1195 *current += len;
1196 *handle += 1;
1197 return len;
1198}
1199
Duncan Laurie21a78702013-05-23 14:17:05 -07001200int smbios_write_type41(unsigned long *current, int *handle,
1201 const char *name, u8 instance, u16 segment,
Christian Waltere6afab12019-05-21 17:22:49 +02001202 u8 bus, u8 device, u8 function, u8 device_type)
Duncan Laurie21a78702013-05-23 14:17:05 -07001203{
Angel Ponsd62a5012021-06-28 17:18:06 +02001204 struct smbios_type41 *t = smbios_carve_table(*current,
1205 SMBIOS_ONBOARD_DEVICES_EXTENDED_INFORMATION,
1206 sizeof(*t), *handle);
Duncan Laurie21a78702013-05-23 14:17:05 -07001207
Duncan Laurie21a78702013-05-23 14:17:05 -07001208 t->reference_designation = smbios_add_string(t->eos, name);
Christian Waltere6afab12019-05-21 17:22:49 +02001209 t->device_type = device_type;
Duncan Laurie21a78702013-05-23 14:17:05 -07001210 t->device_status = 1;
1211 t->device_type_instance = instance;
1212 t->segment_group_number = segment;
1213 t->bus_number = bus;
1214 t->device_number = device;
1215 t->function_number = function;
1216
Angel Ponsa37701a2021-06-28 17:36:53 +02001217 const int len = smbios_full_table_len(&t->header, t->eos);
Duncan Laurie21a78702013-05-23 14:17:05 -07001218 *current += len;
1219 *handle += 1;
1220 return len;
1221}
1222
Michał Żygowskie7795232022-05-04 14:10:45 +02001223int smbios_write_type43(unsigned long *current, int *handle, const u32 vendor_id,
1224 const u8 major_spec_ver, const u8 minor_spec_ver,
1225 const u32 fw_ver1, const u32 fw_ver2, const char *description,
1226 const u64 characteristics, const u32 oem_defined)
1227{
1228 struct smbios_type43 *t = smbios_carve_table(*current, SMBIOS_TPM_DEVICE,
1229 sizeof(*t), *handle);
1230
1231 t->vendor_id = vendor_id;
1232 t->major_spec_ver = major_spec_ver;
1233 t->minor_spec_ver = minor_spec_ver;
1234 t->fw_ver1 = fw_ver1;
1235 t->fw_ver2 = fw_ver2;
1236 t->characteristics = characteristics;
1237 t->oem_defined = oem_defined;
1238 t->description = smbios_add_string(t->eos, description);
1239
1240 const int len = smbios_full_table_len(&t->header, t->eos);
1241 *current += len;
1242 *handle += 1;
1243 return len;
1244}
1245
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001246static int smbios_write_type127(unsigned long *current, int handle)
1247{
Angel Ponsd62a5012021-06-28 17:18:06 +02001248 struct smbios_type127 *t = smbios_carve_table(*current, SMBIOS_END_OF_TABLE,
1249 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001250
Angel Ponsa37701a2021-06-28 17:36:53 +02001251 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001252 *current += len;
1253 return len;
1254}
1255
Angel Ponsbe2e2bb2021-08-27 10:34:05 +02001256/* Get the device type 41 from the dev struct */
1257static u8 smbios_get_device_type_from_dev(struct device *dev)
1258{
1259 u16 pci_basesubclass = (dev->class >> 8) & 0xFFFF;
1260
1261 switch (pci_basesubclass) {
1262 case PCI_CLASS_NOT_DEFINED:
1263 return SMBIOS_DEVICE_TYPE_OTHER;
1264 case PCI_CLASS_DISPLAY_VGA:
1265 case PCI_CLASS_DISPLAY_XGA:
1266 case PCI_CLASS_DISPLAY_3D:
1267 case PCI_CLASS_DISPLAY_OTHER:
1268 return SMBIOS_DEVICE_TYPE_VIDEO;
1269 case PCI_CLASS_STORAGE_SCSI:
1270 return SMBIOS_DEVICE_TYPE_SCSI;
1271 case PCI_CLASS_NETWORK_ETHERNET:
1272 return SMBIOS_DEVICE_TYPE_ETHERNET;
1273 case PCI_CLASS_NETWORK_TOKEN_RING:
1274 return SMBIOS_DEVICE_TYPE_TOKEN_RING;
1275 case PCI_CLASS_MULTIMEDIA_VIDEO:
1276 case PCI_CLASS_MULTIMEDIA_AUDIO:
1277 case PCI_CLASS_MULTIMEDIA_PHONE:
1278 case PCI_CLASS_MULTIMEDIA_OTHER:
1279 return SMBIOS_DEVICE_TYPE_SOUND;
1280 case PCI_CLASS_STORAGE_ATA:
1281 return SMBIOS_DEVICE_TYPE_PATA;
1282 case PCI_CLASS_STORAGE_SATA:
1283 return SMBIOS_DEVICE_TYPE_SATA;
1284 case PCI_CLASS_STORAGE_SAS:
1285 return SMBIOS_DEVICE_TYPE_SAS;
1286 default:
1287 return SMBIOS_DEVICE_TYPE_UNKNOWN;
1288 }
1289}
1290
Angel Pons437da712021-09-03 16:51:40 +02001291static bool smbios_get_type41_instance_id(struct device *dev, u8 device_type, u8 *instance_id)
1292{
1293#if CONFIG(SMBIOS_TYPE41_PROVIDED_BY_DEVTREE)
1294 *instance_id = dev->smbios_instance_id;
1295 return dev->smbios_instance_id_valid;
1296#else
1297 static u8 type41_inst_cnt[SMBIOS_DEVICE_TYPE_COUNT + 1] = {};
1298
1299 if (device_type == SMBIOS_DEVICE_TYPE_OTHER ||
1300 device_type == SMBIOS_DEVICE_TYPE_UNKNOWN)
1301 return false;
1302
1303 if (device_type > SMBIOS_DEVICE_TYPE_COUNT)
1304 return false;
1305
1306 *instance_id = type41_inst_cnt[device_type]++;
1307 return true;
1308#endif
1309}
1310
1311static const char *smbios_get_type41_refdes(struct device *dev)
1312{
1313#if CONFIG(SMBIOS_TYPE41_PROVIDED_BY_DEVTREE)
1314 if (dev->smbios_refdes)
1315 return dev->smbios_refdes;
1316#endif
1317 return get_pci_subclass_name(dev);
1318}
1319
Angel Pons12b809b2021-09-03 11:58:04 +02001320static int smbios_generate_type41_from_devtree(struct device *dev, int *handle,
1321 unsigned long *current)
Christian Walter9e5b0622019-05-21 17:37:58 +02001322{
Christian Walter9e5b0622019-05-21 17:37:58 +02001323 if (dev->path.type != DEVICE_PATH_PCI)
1324 return 0;
1325 if (!dev->on_mainboard)
1326 return 0;
1327
Angel Pons437da712021-09-03 16:51:40 +02001328 const u8 device_type = smbios_get_device_type_from_dev(dev);
Christian Walter9e5b0622019-05-21 17:37:58 +02001329
Angel Pons437da712021-09-03 16:51:40 +02001330 u8 instance_id;
1331
1332 if (!smbios_get_type41_instance_id(dev, device_type, &instance_id))
Christian Walter9e5b0622019-05-21 17:37:58 +02001333 return 0;
1334
Angel Pons437da712021-09-03 16:51:40 +02001335 const char *name = smbios_get_type41_refdes(dev);
Christian Walter9e5b0622019-05-21 17:37:58 +02001336
1337 return smbios_write_type41(current, handle,
1338 name, // name
Angel Pons437da712021-09-03 16:51:40 +02001339 instance_id, // inst
Christian Walter9e5b0622019-05-21 17:37:58 +02001340 0, // segment
1341 dev->bus->secondary, //bus
1342 PCI_SLOT(dev->path.pci.devfn), // device
1343 PCI_FUNC(dev->path.pci.devfn), // func
1344 device_type);
1345}
1346
Angel Pons12b809b2021-09-03 11:58:04 +02001347static int smbios_generate_type9_from_devtree(struct device *dev, int *handle,
1348 unsigned long *current)
Patrick Rudolphf5b93692019-04-12 15:59:40 +02001349{
1350 enum misc_slot_usage usage;
1351 enum slot_data_bus_bandwidth bandwidth;
1352 enum misc_slot_type type;
1353 enum misc_slot_length length;
1354
1355 if (dev->path.type != DEVICE_PATH_PCI)
1356 return 0;
1357
1358 if (!dev->smbios_slot_type && !dev->smbios_slot_data_width &&
1359 !dev->smbios_slot_designation && !dev->smbios_slot_length)
1360 return 0;
1361
1362 if (dev_is_active_bridge(dev))
1363 usage = SlotUsageInUse;
1364 else if (dev->enabled)
1365 usage = SlotUsageAvailable;
1366 else
1367 usage = SlotUsageUnknown;
1368
1369 if (dev->smbios_slot_data_width)
1370 bandwidth = dev->smbios_slot_data_width;
1371 else
1372 bandwidth = SlotDataBusWidthUnknown;
1373
1374 if (dev->smbios_slot_type)
1375 type = dev->smbios_slot_type;
1376 else
1377 type = SlotTypeUnknown;
1378
1379 if (dev->smbios_slot_length)
1380 length = dev->smbios_slot_length;
1381 else
1382 length = SlotLengthUnknown;
1383
1384 return smbios_write_type9(current, handle,
1385 dev->smbios_slot_designation,
1386 type,
1387 bandwidth,
1388 usage,
1389 length,
JingleHsuWiwynn20fa59f2021-01-26 09:55:34 +08001390 0,
Patrick Rudolphf5b93692019-04-12 15:59:40 +02001391 1,
1392 0,
1393 dev->bus->secondary,
1394 dev->path.pci.devfn);
1395}
1396
Angel Pons6a73b242021-09-03 12:18:10 +02001397int get_smbios_data(struct device *dev, int *handle, unsigned long *current)
1398{
1399 int len = 0;
1400
1401 len += smbios_generate_type9_from_devtree(dev, handle, current);
1402 len += smbios_generate_type41_from_devtree(dev, handle, current);
1403
1404 return len;
1405}
1406
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001407static int smbios_walk_device_tree(struct device *tree, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001408{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001409 struct device *dev;
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001410 int len = 0;
1411
Elyes HAOUASdbf30672016-08-21 17:37:15 +02001412 for (dev = tree; dev; dev = dev->next) {
Angel Pons8b98f8b2021-09-07 14:00:17 +02001413 if (!dev->enabled)
1414 continue;
1415
1416 if (dev->ops && dev->ops->get_smbios_data) {
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001417 printk(BIOS_INFO, "%s (%s)\n", dev_path(dev), dev_name(dev));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001418 len += dev->ops->get_smbios_data(dev, handle, current);
Angel Pons6a73b242021-09-03 12:18:10 +02001419 } else {
1420 len += get_smbios_data(dev, handle, current);
Naresh G Solanki20c893e2018-06-05 18:58:53 +05301421 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001422 }
1423 return len;
1424}
1425
1426unsigned long smbios_write_tables(unsigned long current)
1427{
1428 struct smbios_entry *se;
Patrick Rudolph7a835822020-07-22 16:00:53 +02001429 struct smbios_entry30 *se3;
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001430 unsigned long tables;
Ben Frisch72af5d72015-05-09 19:52:18 -05001431 int len = 0;
1432 int max_struct_size = 0;
1433 int handle = 0;
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001434
Felix Heldfcbb3c52019-06-20 14:01:54 +02001435 current = ALIGN_UP(current, 16);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001436 printk(BIOS_DEBUG, "%s: %08lx\n", __func__, current);
1437
1438 se = (struct smbios_entry *)current;
Angel Pons35b99c62021-06-28 15:36:23 +02001439 current += sizeof(*se);
Felix Heldfcbb3c52019-06-20 14:01:54 +02001440 current = ALIGN_UP(current, 16);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001441
Patrick Rudolph7a835822020-07-22 16:00:53 +02001442 se3 = (struct smbios_entry30 *)current;
Angel Pons35b99c62021-06-28 15:36:23 +02001443 current += sizeof(*se3);
Patrick Rudolph7a835822020-07-22 16:00:53 +02001444 current = ALIGN_UP(current, 16);
1445
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001446 tables = current;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001447 update_max(len, max_struct_size, smbios_write_type0(&current, handle++));
1448 update_max(len, max_struct_size, smbios_write_type1(&current, handle++));
1449
1450 /* The chassis handle is the next one */
1451 update_max(len, max_struct_size, smbios_write_type2(&current, handle, handle + 1));
Julien Viard de Galbert9a31dfe2018-02-22 16:39:58 +01001452 handle++;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001453 update_max(len, max_struct_size, smbios_write_type3(&current, handle++));
Patrick Rudolph15589b42019-03-30 17:51:06 +01001454
1455 struct smbios_type4 *type4 = (struct smbios_type4 *)current;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001456 update_max(len, max_struct_size, smbios_write_type4(&current, handle++));
1457 len += smbios_write_type7_cache_parameters(&current, &handle, &max_struct_size, type4);
1458 update_max(len, max_struct_size, smbios_write_type11(&current, &handle));
Julius Wernercd49cce2019-03-05 16:53:33 -08001459 if (CONFIG(ELOG))
Martin Roth898a7752017-06-01 11:39:59 -06001460 update_max(len, max_struct_size,
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001461 elog_smbios_write_type15(&current, handle++));
Patrick Rudolph5e007802020-07-27 15:37:43 +02001462
1463 const int type16 = handle;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001464 update_max(len, max_struct_size, smbios_write_type16(&current, &handle));
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001465 const int type17 = handle;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001466 update_max(len, max_struct_size, smbios_write_type17(&current, &handle, type16));
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001467 const int type19 = handle;
Tim Chue41f5952020-11-02 21:33:52 -08001468 update_max(len, max_struct_size, smbios_write_type19(&current, &handle, type16));
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001469 update_max(len, max_struct_size,
1470 smbios_write_type20(&current, &handle, type17, type19));
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001471 update_max(len, max_struct_size, smbios_write_type32(&current, handle++));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001472
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001473 update_max(len, max_struct_size, smbios_walk_device_tree(all_devices,
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001474 &handle, &current));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001475
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001476 update_max(len, max_struct_size, smbios_write_type127(&current, handle++));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001477
Patrick Rudolph7a835822020-07-22 16:00:53 +02001478 /* Install SMBIOS 2.1 entry point */
Angel Pons35b99c62021-06-28 15:36:23 +02001479 memset(se, 0, sizeof(*se));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001480 memcpy(se->anchor, "_SM_", 4);
Angel Pons35b99c62021-06-28 15:36:23 +02001481 se->length = sizeof(*se);
Patrick Rudolph7a835822020-07-22 16:00:53 +02001482 se->major_version = 3;
1483 se->minor_version = 0;
Ben Frisch72af5d72015-05-09 19:52:18 -05001484 se->max_struct_size = max_struct_size;
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001485 se->struct_count = handle;
1486 memcpy(se->intermediate_anchor_string, "_DMI_", 5);
1487
1488 se->struct_table_address = (u32)tables;
1489 se->struct_table_length = len;
1490
Angel Pons35b99c62021-06-28 15:36:23 +02001491 se->intermediate_checksum = smbios_checksum((u8 *)se + 0x10, sizeof(*se) - 0x10);
1492 se->checksum = smbios_checksum((u8 *)se, sizeof(*se));
Patrick Rudolph7a835822020-07-22 16:00:53 +02001493
1494 /* Install SMBIOS 3.0 entry point */
Angel Pons35b99c62021-06-28 15:36:23 +02001495 memset(se3, 0, sizeof(*se3));
Patrick Rudolph7a835822020-07-22 16:00:53 +02001496 memcpy(se3->anchor, "_SM3_", 5);
Angel Pons35b99c62021-06-28 15:36:23 +02001497 se3->length = sizeof(*se3);
Patrick Rudolph7a835822020-07-22 16:00:53 +02001498 se3->major_version = 3;
1499 se3->minor_version = 0;
1500
1501 se3->struct_table_address = (u64)tables;
1502 se3->struct_table_length = len;
1503
Angel Pons35b99c62021-06-28 15:36:23 +02001504 se3->checksum = smbios_checksum((u8 *)se3, sizeof(*se3));
Patrick Rudolph7a835822020-07-22 16:00:53 +02001505
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001506 return current;
1507}