blob: c05f905892b1c1322ca6695154ec41aa6420c53a [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>
19#include <device/pci_def.h>
20#include <device/pci.h>
Johnny Linc746a742020-06-03 11:44:22 +080021#include <drivers/vpd/vpd.h>
22#include <stdlib.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020023
Patrick Rudolphfc5b8092019-03-30 17:37:28 +010024#define update_max(len, max_len, stmt) \
25 do { \
26 int tmp = stmt; \
27 \
28 max_len = MAX(max_len, tmp); \
29 len += tmp; \
30 } while (0)
31
Sven Schnelle164bcfd2011-08-14 20:56:34 +020032static u8 smbios_checksum(u8 *p, u32 length)
33{
34 u8 ret = 0;
35 while (length--)
36 ret += *p++;
37 return -ret;
38}
39
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030040int smbios_add_string(u8 *start, const char *str)
Sven Schnelle164bcfd2011-08-14 20:56:34 +020041{
42 int i = 1;
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030043 char *p = (char *)start;
Sven Schnelle164bcfd2011-08-14 20:56:34 +020044
Ben Gardner6b07cba2015-12-09 11:24:35 -060045 /*
46 * Return 0 as required for empty strings.
47 * See Section 6.1.3 "Text Strings" of the SMBIOS specification.
48 */
49 if (*str == '\0')
50 return 0;
51
Elyes HAOUASdbf30672016-08-21 17:37:15 +020052 for (;;) {
Sven Schnelle164bcfd2011-08-14 20:56:34 +020053 if (!*p) {
54 strcpy(p, str);
55 p += strlen(str);
56 *p++ = '\0';
57 *p++ = '\0';
58 return i;
59 }
60
61 if (!strcmp(p, str))
62 return i;
63
64 p += strlen(p)+1;
65 i++;
66 }
67}
68
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030069int smbios_string_table_len(u8 *start)
Sven Schnelle164bcfd2011-08-14 20:56:34 +020070{
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030071 char *p = (char *)start;
Sven Schnelle164bcfd2011-08-14 20:56:34 +020072 int i, len = 0;
73
Lee Leahy024b13d2017-03-16 13:41:11 -070074 while (*p) {
Sven Schnelle164bcfd2011-08-14 20:56:34 +020075 i = strlen(p) + 1;
76 p += i;
77 len += i;
78 }
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +030079
80 if (!len)
81 return 2;
82
Sven Schnelle164bcfd2011-08-14 20:56:34 +020083 return len + 1;
84}
85
Angel Ponsa37701a2021-06-28 17:36:53 +020086int smbios_full_table_len(struct smbios_header *header, u8 *str_table_start)
87{
88 return header->length + smbios_string_table_len(str_table_start);
89}
90
Angel Ponsd62a5012021-06-28 17:18:06 +020091void *smbios_carve_table(unsigned long start, u8 type, u8 length, u16 handle)
92{
93 struct smbios_header *t = (struct smbios_header *)start;
94
95 assert(length >= sizeof(*t));
96 memset(t, 0, length);
97 t->type = type;
98 t->length = length - 2;
99 t->handle = handle;
100 return t;
101}
102
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +0300103static int smbios_cpu_vendor(u8 *start)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200104{
Rudolf Marek06253cd2012-02-25 23:51:12 +0100105 if (cpu_have_cpuid()) {
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700106 u32 tmp[4];
107 const struct cpuid_result res = cpuid(0);
108 tmp[0] = res.ebx;
109 tmp[1] = res.edx;
110 tmp[2] = res.ecx;
111 tmp[3] = 0;
112 return smbios_add_string(start, (const char *)tmp);
113 } else {
114 return smbios_add_string(start, "Unknown");
Rudolf Marek06253cd2012-02-25 23:51:12 +0100115 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200116}
117
Konstantin Aladyshevd0df1d72017-08-01 15:52:46 +0300118static int smbios_processor_name(u8 *start)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200119{
Nico Huberaa9643e2017-06-20 14:49:04 +0200120 u32 tmp[13];
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700121 const char *str = "Unknown Processor Name";
Rudolf Marek06253cd2012-02-25 23:51:12 +0100122 if (cpu_have_cpuid()) {
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700123 int i;
124 struct cpuid_result res = cpuid(0x80000000);
Rudolf Marek06253cd2012-02-25 23:51:12 +0100125 if (res.eax >= 0x80000004) {
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700126 int j = 0;
Rudolf Marek06253cd2012-02-25 23:51:12 +0100127 for (i = 0; i < 3; i++) {
128 res = cpuid(0x80000002 + i);
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700129 tmp[j++] = res.eax;
130 tmp[j++] = res.ebx;
131 tmp[j++] = res.ecx;
132 tmp[j++] = res.edx;
Rudolf Marek06253cd2012-02-25 23:51:12 +0100133 }
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700134 tmp[12] = 0;
135 str = (const char *)tmp;
Rudolf Marek06253cd2012-02-25 23:51:12 +0100136 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200137 }
Ryan Salsamendi1b5eda02017-06-11 18:50:32 -0700138 return smbios_add_string(start, str);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200139}
140
Angel Pons9630ced2020-07-29 18:29:28 +0200141/* this function will fill the corresponding manufacturer */
142void smbios_fill_dimm_manufacturer_from_id(uint16_t mod_id, struct smbios_type17 *t)
143{
Patrick Rudolphff251d22021-02-11 15:09:22 +0100144 const char *const manufacturer = spd_manufacturer_name(mod_id);
Angel Pons9630ced2020-07-29 18:29:28 +0200145
146 if (manufacturer) {
147 t->manufacturer = smbios_add_string(t->eos, manufacturer);
148 } else {
149 char string_buffer[256];
150
151 snprintf(string_buffer, sizeof(string_buffer), "Unknown (%x)", mod_id);
152 t->manufacturer = smbios_add_string(t->eos, string_buffer);
Kane Chen33faac62014-07-27 12:54:44 -0700153 }
154}
155
Raul E Rangel50021cd2018-03-20 12:40:55 -0600156static void trim_trailing_whitespace(char *buffer, size_t buffer_size)
Richard Spiegelbd654802018-02-22 10:03:39 -0700157{
Raul E Rangel50021cd2018-03-20 12:40:55 -0600158 size_t len = strnlen(buffer, buffer_size);
159
160 if (len == 0)
161 return;
162
163 for (char *p = buffer + len - 1; p >= buffer; --p) {
164 if (*p == ' ')
165 *p = 0;
166 else
167 break;
168 }
169}
170
171/** This function will fill the corresponding part number */
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200172static void smbios_fill_dimm_part_number(const char *part_number, struct smbios_type17 *t)
Raul E Rangel50021cd2018-03-20 12:40:55 -0600173{
Raul E Rangel50021cd2018-03-20 12:40:55 -0600174 int invalid;
175 size_t i, len;
Jacob Garber9172b692019-06-26 16:18:16 -0600176 char trimmed_part_number[DIMM_INFO_PART_NUMBER_SIZE];
Raul E Rangel50021cd2018-03-20 12:40:55 -0600177
Jacob Garber9172b692019-06-26 16:18:16 -0600178 strncpy(trimmed_part_number, part_number, sizeof(trimmed_part_number));
179 trimmed_part_number[sizeof(trimmed_part_number) - 1] = '\0';
Raul E Rangel50021cd2018-03-20 12:40:55 -0600180
181 /*
182 * SPD mandates that unused characters be represented with a ' '.
183 * We don't want to publish the whitespace in the SMBIOS tables.
184 */
Jacob Garber9172b692019-06-26 16:18:16 -0600185 trim_trailing_whitespace(trimmed_part_number, sizeof(trimmed_part_number));
Raul E Rangel50021cd2018-03-20 12:40:55 -0600186
187 len = strlen(trimmed_part_number);
Richard Spiegelbd654802018-02-22 10:03:39 -0700188
189 invalid = 0; /* assume valid */
Lijian Zhaob1fc13a2018-03-26 18:27:02 -0700190 for (i = 0; i < len; i++) {
Raul E Rangel50021cd2018-03-20 12:40:55 -0600191 if (trimmed_part_number[i] < ' ') {
Richard Spiegelbd654802018-02-22 10:03:39 -0700192 invalid = 1;
Raul E Rangel50021cd2018-03-20 12:40:55 -0600193 trimmed_part_number[i] = '*';
Richard Spiegelbd654802018-02-22 10:03:39 -0700194 }
195 }
Raul E Rangel50021cd2018-03-20 12:40:55 -0600196
Lijian Zhaob1fc13a2018-03-26 18:27:02 -0700197 if (len == 0) {
198 /* Null String in Part Number will have "None" instead. */
199 t->part_number = smbios_add_string(t->eos, "None");
200 } else if (invalid) {
Jacob Garber9172b692019-06-26 16:18:16 -0600201 char string_buffer[sizeof(trimmed_part_number) + 10];
Richard Spiegelbd654802018-02-22 10:03:39 -0700202
203 snprintf(string_buffer, sizeof(string_buffer), "Invalid (%s)",
Raul E Rangel50021cd2018-03-20 12:40:55 -0600204 trimmed_part_number);
Richard Spiegelbd654802018-02-22 10:03:39 -0700205 t->part_number = smbios_add_string(t->eos, string_buffer);
Raul E Rangel50021cd2018-03-20 12:40:55 -0600206 } else {
207 t->part_number = smbios_add_string(t->eos, trimmed_part_number);
208 }
Richard Spiegelbd654802018-02-22 10:03:39 -0700209}
210
Raul E Rangel99f54a62018-04-11 10:58:14 -0600211/* Encodes the SPD serial number into hex */
212static void smbios_fill_dimm_serial_number(const struct dimm_info *dimm,
213 struct smbios_type17 *t)
214{
215 char serial[9];
216
217 snprintf(serial, sizeof(serial), "%02hhx%02hhx%02hhx%02hhx",
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200218 dimm->serial[0], dimm->serial[1], dimm->serial[2], dimm->serial[3]);
Raul E Rangel99f54a62018-04-11 10:58:14 -0600219
220 t->serial_number = smbios_add_string(t->eos, serial);
221}
222
Kane Chen33faac62014-07-27 12:54:44 -0700223static int create_smbios_type17_for_dimm(struct dimm_info *dimm,
Patrick Rudolph5e007802020-07-27 15:37:43 +0200224 unsigned long *current, int *handle,
225 int type16_handle)
Kane Chen33faac62014-07-27 12:54:44 -0700226{
Angel Ponsd62a5012021-06-28 17:18:06 +0200227 struct smbios_type17 *t = smbios_carve_table(*current, SMBIOS_MEMORY_DEVICE,
228 sizeof(*t), *handle);
Kane Chen33faac62014-07-27 12:54:44 -0700229
Kane Chen33faac62014-07-27 12:54:44 -0700230 t->memory_type = dimm->ddr_type;
Rob Barnes327f1052020-09-01 10:26:57 -0600231 if (dimm->configured_speed_mts != 0)
232 t->clock_speed = dimm->configured_speed_mts;
233 else
234 t->clock_speed = dimm->ddr_frequency;
235 if (dimm->max_speed_mts != 0)
236 t->speed = dimm->max_speed_mts;
237 else
238 t->speed = dimm->ddr_frequency;
Julien Viard de Galbert99891712018-01-24 11:04:46 +0100239 if (dimm->dimm_size < 0x7fff) {
240 t->size = dimm->dimm_size;
241 } else {
242 t->size = 0x7fff;
243 t->extended_size = dimm->dimm_size & 0x7fffffff;
244 }
Kane Chen33faac62014-07-27 12:54:44 -0700245 t->data_width = 8 * (1 << (dimm->bus_width & 0x7));
246 t->total_width = t->data_width + 8 * ((dimm->bus_width & 0x18) >> 3);
247
248 switch (dimm->mod_type) {
Lee Leahy0b5678f2017-03-16 16:01:40 -0700249 case SPD_RDIMM:
250 case SPD_MINI_RDIMM:
251 t->form_factor = MEMORY_FORMFACTOR_RIMM;
252 break;
253 case SPD_UDIMM:
254 case SPD_MICRO_DIMM:
255 case SPD_MINI_UDIMM:
256 t->form_factor = MEMORY_FORMFACTOR_DIMM;
257 break;
258 case SPD_SODIMM:
259 t->form_factor = MEMORY_FORMFACTOR_SODIMM;
260 break;
261 default:
262 t->form_factor = MEMORY_FORMFACTOR_UNKNOWN;
263 break;
Kane Chen33faac62014-07-27 12:54:44 -0700264 }
265
Timothy Pearson4785f2a2015-03-27 23:05:36 -0500266 smbios_fill_dimm_manufacturer_from_id(dimm->mod_id, t);
Raul E Rangel99f54a62018-04-11 10:58:14 -0600267 smbios_fill_dimm_serial_number(dimm, t);
Tim Chu31b42092020-12-23 19:15:21 -0800268 smbios_fill_dimm_asset_tag(dimm, t);
Lijian Zhao10ea93c2019-04-11 00:45:10 -0700269 smbios_fill_dimm_locator(dimm, t);
Kane Chen33faac62014-07-27 12:54:44 -0700270
271 /* put '\0' in the end of data */
Richard Spiegelbd654802018-02-22 10:03:39 -0700272 dimm->module_part_number[DIMM_INFO_PART_NUMBER_SIZE - 1] = '\0';
273 smbios_fill_dimm_part_number((char *)dimm->module_part_number, t);
Kane Chen33faac62014-07-27 12:54:44 -0700274
Christian Walterf9723222019-05-28 10:37:24 +0200275 /* Voltage Levels */
276 t->configured_voltage = dimm->vdd_voltage;
277 t->minimum_voltage = dimm->vdd_voltage;
278 t->maximum_voltage = dimm->vdd_voltage;
279
Tim Chu0c094ae2021-01-12 01:44:37 -0800280 /* Fill in type detail */
281 switch (dimm->mod_type) {
282 case SPD_RDIMM:
283 case SPD_MINI_RDIMM:
284 t->type_detail = MEMORY_TYPE_DETAIL_REGISTERED;
285 break;
286 case SPD_UDIMM:
287 case SPD_MINI_UDIMM:
288 t->type_detail = MEMORY_TYPE_DETAIL_UNBUFFERED;
289 break;
290 default:
291 t->type_detail = MEMORY_TYPE_DETAIL_UNKNOWN;
292 break;
293 }
Kane Chen33faac62014-07-27 12:54:44 -0700294 /* Synchronous = 1 */
Tim Chu0c094ae2021-01-12 01:44:37 -0800295 t->type_detail |= MEMORY_TYPE_DETAIL_SYNCHRONOUS;
Kane Chen33faac62014-07-27 12:54:44 -0700296 /* no handle for error information */
297 t->memory_error_information_handle = 0xFFFE;
298 t->attributes = dimm->rank_per_dimm;
Patrick Rudolph5e007802020-07-27 15:37:43 +0200299 t->phys_memory_array_handle = type16_handle;
300
Kane Chen33faac62014-07-27 12:54:44 -0700301 *handle += 1;
Angel Ponsa37701a2021-06-28 17:36:53 +0200302 return smbios_full_table_len(&t->header, t->eos);
Kane Chen33faac62014-07-27 12:54:44 -0700303}
304
Johnny Linc746a742020-06-03 11:44:22 +0800305#define VERSION_VPD "firmware_version"
306static const char *vpd_get_bios_version(void)
307{
308 int size;
309 const char *s;
310 char *version;
311
312 s = vpd_find(VERSION_VPD, &size, VPD_RO);
313 if (!s) {
314 printk(BIOS_ERR, "Find version from VPD %s failed\n", VERSION_VPD);
315 return NULL;
316 }
317
318 version = malloc(size + 1);
319 if (!version) {
320 printk(BIOS_ERR, "Failed to malloc %d bytes for VPD version\n", size + 1);
321 return NULL;
322 }
323 memcpy(version, s, size);
324 version[size] = '\0';
325 printk(BIOS_DEBUG, "Firmware version %s from VPD %s\n", version, VERSION_VPD);
326 return version;
327}
328
329static const char *get_bios_version(void)
330{
331 const char *s;
332
333#define SPACES \
334 " "
335
336 if (CONFIG(CHROMEOS))
337 return SPACES;
338
339 if (CONFIG(VPD_SMBIOS_VERSION)) {
340 s = vpd_get_bios_version();
341 if (s != NULL)
342 return s;
343 }
344
345 s = smbios_mainboard_bios_version();
346 if (s != NULL)
347 return s;
348
349 if (strlen(CONFIG_LOCALVERSION) != 0) {
350 printk(BIOS_DEBUG, "BIOS version set to CONFIG_LOCALVERSION: '%s'\n",
351 CONFIG_LOCALVERSION);
352 return CONFIG_LOCALVERSION;
353 }
354
355 printk(BIOS_DEBUG, "SMBIOS firmware version is set to coreboot_version: '%s'\n",
356 coreboot_version);
357 return coreboot_version;
358}
359
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200360static int smbios_write_type0(unsigned long *current, int handle)
361{
Angel Ponsd62a5012021-06-28 17:18:06 +0200362 struct smbios_type0 *t = smbios_carve_table(*current, SMBIOS_BIOS_INFORMATION,
363 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200364
365 t->vendor = smbios_add_string(t->eos, "coreboot");
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +0200366 t->bios_release_date = smbios_add_string(t->eos, coreboot_dmi_date);
Christian Gmeiner5e272a42013-02-04 16:22:46 +0100367
Kyösti Mälkki84d10cc2021-02-10 17:53:34 +0200368 if (CONFIG(CHROMEOS_NVS)) {
Kyösti Mälkki0fcbd3a2020-12-20 08:27:21 +0200369 uintptr_t version_address = (uintptr_t)t->eos;
370 /* SMBIOS offsets start at 1 rather than 0 */
371 version_address += (u32)smbios_string_table_len(t->eos) - 1;
372 smbios_type0_bios_version(version_address);
373 }
Johnny Linc746a742020-06-03 11:44:22 +0800374 t->bios_version = smbios_add_string(t->eos, get_bios_version());
Lee Leahy67358712016-06-08 12:47:07 -0700375 uint32_t rom_size = CONFIG_ROM_SIZE;
376 rom_size = MIN(CONFIG_ROM_SIZE, 16 * MiB);
377 t->bios_rom_size = (rom_size / 65535) - 1;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800378
Elyes HAOUAS358cbb32019-02-14 14:19:22 +0100379 if (CONFIG_ROM_SIZE >= 1 * GiB) {
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200380 t->extended_bios_rom_size = DIV_ROUND_UP(CONFIG_ROM_SIZE, GiB) | (1 << 14);
Elyes HAOUAS358cbb32019-02-14 14:19:22 +0100381 } else {
382 t->extended_bios_rom_size = DIV_ROUND_UP(CONFIG_ROM_SIZE, MiB);
383 }
384
Elyes HAOUAS94ad3762019-02-15 17:39:56 +0100385 t->system_bios_major_release = coreboot_major_revision;
386 t->system_bios_minor_release = coreboot_minor_revision;
387
Tim Chuf2f53c42020-09-07 02:30:19 -0700388 smbios_ec_revision(&t->ec_major_release, &t->ec_minor_release);
389
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200390 t->bios_characteristics =
391 BIOS_CHARACTERISTICS_PCI_SUPPORTED |
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200392 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
393 BIOS_CHARACTERISTICS_UPGRADEABLE;
394
Julius Wernercd49cce2019-03-05 16:53:33 -0800395 if (CONFIG(CARDBUS_PLUGIN_SUPPORT))
Martin Roth898a7752017-06-01 11:39:59 -0600396 t->bios_characteristics |= BIOS_CHARACTERISTICS_PC_CARD;
397
Julius Wernercd49cce2019-03-05 16:53:33 -0800398 if (CONFIG(HAVE_ACPI_TABLES))
Martin Roth898a7752017-06-01 11:39:59 -0600399 t->bios_characteristics_ext1 = BIOS_EXT1_CHARACTERISTICS_ACPI;
400
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200401 t->bios_characteristics_ext2 = BIOS_EXT2_CHARACTERISTICS_TARGET;
Angel Ponsa37701a2021-06-28 17:36:53 +0200402 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200403 *current += len;
404 return len;
405}
406
Elyes HAOUAS28fa33c2019-01-25 13:46:43 +0100407static int get_socket_type(void)
408{
409 if (CONFIG(CPU_INTEL_SLOT_1))
410 return 0x08;
411 if (CONFIG(CPU_INTEL_SOCKET_MPGA604))
412 return 0x13;
413 if (CONFIG(CPU_INTEL_SOCKET_LGA775))
414 return 0x15;
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800415 if (CONFIG(XEON_SP_COMMON_BASE))
416 return 0x36;
Elyes HAOUAS28fa33c2019-01-25 13:46:43 +0100417
418 return 0x02; /* Unknown */
419}
420
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800421unsigned int __weak smbios_processor_external_clock(void)
422{
423 return 0; /* Unknown */
424}
425
426unsigned int __weak smbios_processor_characteristics(void)
427{
428 return 0;
429}
430
431unsigned int __weak smbios_processor_family(struct cpuid_result res)
432{
433 return (res.eax > 0) ? 0x0c : 0x6;
434}
435
Morgan Jang8ae391d2020-10-06 16:26:17 +0800436unsigned int __weak smbios_cache_error_correction_type(u8 level)
437{
438 return SMBIOS_CACHE_ERROR_CORRECTION_UNKNOWN;
439}
440
441unsigned int __weak smbios_cache_sram_type(void)
442{
443 return SMBIOS_CACHE_SRAM_TYPE_UNKNOWN;
444}
445
446unsigned int __weak smbios_cache_conf_operation_mode(u8 level)
447{
448 return SMBIOS_CACHE_OP_MODE_UNKNOWN; /* Unknown */
449}
450
Patrick Rudolphb01ac7e2020-07-26 14:23:37 +0200451/* Returns the processor voltage in 100mV units */
452unsigned int __weak smbios_cpu_get_voltage(void)
453{
454 return 0; /* Unknown */
455}
456
Subrata Banike2b5fee2021-07-23 21:02:45 +0530457static size_t get_number_of_caches(size_t max_logical_cpus_sharing_cache)
Morgan Jang8ae391d2020-10-06 16:26:17 +0800458{
Morgan Jang8ae391d2020-10-06 16:26:17 +0800459 size_t number_of_cpus_per_package = 0;
460 size_t max_logical_cpus_per_package = 0;
461 struct cpuid_result res;
462
463 if (!cpu_have_cpuid())
464 return 1;
465
466 res = cpuid(1);
467
468 max_logical_cpus_per_package = (res.ebx >> 16) & 0xff;
469
Morgan Jang8ae391d2020-10-06 16:26:17 +0800470 /* Check if it's last level cache */
471 if (max_logical_cpus_sharing_cache == max_logical_cpus_per_package)
472 return 1;
473
474 if (cpuid_get_max_func() >= 0xb) {
475 res = cpuid_ext(0xb, 1);
476 number_of_cpus_per_package = res.ebx & 0xff;
477 } else {
478 number_of_cpus_per_package = max_logical_cpus_per_package;
479 }
480
481 return number_of_cpus_per_package / max_logical_cpus_sharing_cache;
482}
483
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200484static int smbios_write_type1(unsigned long *current, int handle)
485{
Angel Ponsd62a5012021-06-28 17:18:06 +0200486 struct smbios_type1 *t = smbios_carve_table(*current, SMBIOS_SYSTEM_INFORMATION,
487 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200488
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200489 t->manufacturer = smbios_add_string(t->eos, smbios_system_manufacturer());
490 t->product_name = smbios_add_string(t->eos, smbios_system_product_name());
491 t->serial_number = smbios_add_string(t->eos, smbios_system_serial_number());
Tim Chu13ab1d72021-10-19 01:45:12 +0000492 t->wakeup_type = smbios_system_wakeup_type();
Nico Huberebd8a4f2017-11-01 09:49:16 +0100493 t->sku = smbios_add_string(t->eos, smbios_system_sku());
494 t->version = smbios_add_string(t->eos, smbios_system_version());
Marc Jonesf43ba9c2015-06-09 21:10:43 -0600495#ifdef CONFIG_MAINBOARD_FAMILY
Nico Huberebd8a4f2017-11-01 09:49:16 +0100496 t->family = smbios_add_string(t->eos, CONFIG_MAINBOARD_FAMILY);
Marc Jonesf43ba9c2015-06-09 21:10:43 -0600497#endif
Nico Huberebd8a4f2017-11-01 09:49:16 +0100498 smbios_system_set_uuid(t->uuid);
Angel Ponsa37701a2021-06-28 17:36:53 +0200499 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200500 *current += len;
501 return len;
502}
503
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200504static int smbios_write_type2(unsigned long *current, int handle, const int chassis_handle)
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100505{
Angel Ponsd62a5012021-06-28 17:18:06 +0200506 struct smbios_type2 *t = smbios_carve_table(*current, SMBIOS_BOARD_INFORMATION,
507 sizeof(*t), handle);
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100508
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200509 t->manufacturer = smbios_add_string(t->eos, smbios_mainboard_manufacturer());
510 t->product_name = smbios_add_string(t->eos, smbios_mainboard_product_name());
511 t->serial_number = smbios_add_string(t->eos, smbios_mainboard_serial_number());
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100512 t->version = smbios_add_string(t->eos, smbios_mainboard_version());
Julien Viard de Galbert9a31dfe2018-02-22 16:39:58 +0100513 t->asset_tag = smbios_add_string(t->eos, smbios_mainboard_asset_tag());
514 t->feature_flags = smbios_mainboard_feature_flags();
515 t->location_in_chassis = smbios_add_string(t->eos,
516 smbios_mainboard_location_in_chassis());
517 t->board_type = smbios_mainboard_board_type();
518 t->chassis_handle = chassis_handle;
Angel Ponsa37701a2021-06-28 17:36:53 +0200519 const int len = smbios_full_table_len(&t->header, t->eos);
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100520 *current += len;
521 return len;
522}
523
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200524static int smbios_write_type3(unsigned long *current, int handle)
525{
Angel Ponsd62a5012021-06-28 17:18:06 +0200526 struct smbios_type3 *t = smbios_carve_table(*current, SMBIOS_SYSTEM_ENCLOSURE,
527 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200528
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200529 t->manufacturer = smbios_add_string(t->eos, smbios_system_manufacturer());
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200530 t->bootup_state = SMBIOS_STATE_SAFE;
531 t->power_supply_state = SMBIOS_STATE_SAFE;
532 t->thermal_state = SMBIOS_STATE_SAFE;
Mathew Kinga7d55cf2019-07-31 15:50:15 -0600533 t->_type = smbios_mainboard_enclosure_type();
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200534 t->security_status = SMBIOS_STATE_SAFE;
JingleHsuWiwynn4330b962021-01-28 09:13:42 +0800535 t->number_of_power_cords = smbios_chassis_power_cords();
Johnny Lind3440542020-01-30 18:21:22 +0800536 t->asset_tag_number = smbios_add_string(t->eos, smbios_mainboard_asset_tag());
537 t->version = smbios_add_string(t->eos, smbios_chassis_version());
538 t->serial_number = smbios_add_string(t->eos, smbios_chassis_serial_number());
Angel Ponsa37701a2021-06-28 17:36:53 +0200539 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200540 *current += len;
541 return len;
542}
543
544static int smbios_write_type4(unsigned long *current, int handle)
545{
Patrick Rudolphb01ac7e2020-07-26 14:23:37 +0200546 unsigned int cpu_voltage;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200547 struct cpuid_result res;
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800548 uint16_t characteristics = 0;
Tim Chu27bf0c82020-09-16 00:34:08 -0700549 static unsigned int cnt = 0;
550 char buf[8];
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200551
Rudolf Marek06253cd2012-02-25 23:51:12 +0100552 /* Provide sane defaults even for CPU without CPUID */
553 res.eax = res.edx = 0;
554 res.ebx = 0x10000;
555
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700556 if (cpu_have_cpuid())
Rudolf Marek06253cd2012-02-25 23:51:12 +0100557 res = cpuid(1);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200558
Angel Ponsd62a5012021-06-28 17:18:06 +0200559 struct smbios_type4 *t = smbios_carve_table(*current, SMBIOS_PROCESSOR_INFORMATION,
560 sizeof(*t), handle);
Tim Chu27bf0c82020-09-16 00:34:08 -0700561
562 snprintf(buf, sizeof(buf), "CPU%d", cnt++);
563 t->socket_designation = smbios_add_string(t->eos, buf);
564
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200565 t->processor_id[0] = res.eax;
566 t->processor_id[1] = res.edx;
567 t->processor_manufacturer = smbios_cpu_vendor(t->eos);
568 t->processor_version = smbios_processor_name(t->eos);
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800569 t->processor_family = smbios_processor_family(res);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200570 t->processor_type = 3; /* System Processor */
Andrey Petrov515ef382019-11-15 13:19:08 -0800571 /*
572 * If CPUID leaf 11 is available, calculate "core count" by dividing
573 * SMT_ID (logical processors in a core) by Core_ID (number of cores).
574 * This seems to be the way to arrive to a number of cores mentioned on
575 * ark.intel.com.
576 */
577 if (cpu_have_cpuid() && cpuid_get_max_func() >= 0xb) {
578 uint32_t leaf_b_cores = 0, leaf_b_threads = 0;
579 res = cpuid_ext(0xb, 1);
580 leaf_b_cores = res.ebx;
581 res = cpuid_ext(0xb, 0);
582 leaf_b_threads = res.ebx;
583 /* if hyperthreading is not available, pretend this is 1 */
584 if (leaf_b_threads == 0) {
585 leaf_b_threads = 1;
586 }
Patrick Rudolph7a835822020-07-22 16:00:53 +0200587 t->core_count2 = leaf_b_cores / leaf_b_threads;
588 t->core_count = t->core_count2 > 0xff ? 0xff : t->core_count2;
Francois Toguo597922e2019-03-27 18:13:07 -0700589 t->thread_count2 = leaf_b_cores;
590 t->thread_count = t->thread_count2 > 0xff ? 0xff : t->thread_count2;
Andrey Petrov515ef382019-11-15 13:19:08 -0800591 } else {
592 t->core_count = (res.ebx >> 16) & 0xff;
Patrick Rudolph7a835822020-07-22 16:00:53 +0200593 t->core_count2 = t->core_count;
594 t->thread_count2 = t->core_count2;
Francois Toguo597922e2019-03-27 18:13:07 -0700595 t->thread_count = t->thread_count2;
Andrey Petrov515ef382019-11-15 13:19:08 -0800596 }
Andrey Petrov2e032f02019-10-23 15:31:51 -0700597 /* Assume we enable all the cores always, capped only by MAX_CPUS */
Andrey Petrova7fb2302019-11-07 09:48:41 -0800598 t->core_enabled = MIN(t->core_count, CONFIG_MAX_CPUS);
Patrick Rudolph7a835822020-07-22 16:00:53 +0200599 t->core_enabled2 = MIN(t->core_count2, CONFIG_MAX_CPUS);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200600 t->l1_cache_handle = 0xffff;
601 t->l2_cache_handle = 0xffff;
602 t->l3_cache_handle = 0xffff;
Johnny Lind3440542020-01-30 18:21:22 +0800603 t->serial_number = smbios_add_string(t->eos, smbios_processor_serial_number());
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200604 t->status = SMBIOS_PROCESSOR_STATUS_CPU_ENABLED | SMBIOS_PROCESSOR_STATUS_POPULATED;
Elyes HAOUAS28fa33c2019-01-25 13:46:43 +0100605 t->processor_upgrade = get_socket_type();
Andrey Petrov2e032f02019-10-23 15:31:51 -0700606 if (cpu_have_cpuid() && cpuid_get_max_func() >= 0x16) {
Andrey Petrov2e032f02019-10-23 15:31:51 -0700607 t->current_speed = cpuid_eax(0x16); /* base frequency */
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800608 t->external_clock = cpuid_ecx(0x16);
Andrey Petrov2e032f02019-10-23 15:31:51 -0700609 } else {
Andrey Petrov2e032f02019-10-23 15:31:51 -0700610 t->current_speed = smbios_cpu_get_current_speed_mhz();
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800611 t->external_clock = smbios_processor_external_clock();
Andrey Petrov2e032f02019-10-23 15:31:51 -0700612 }
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800613
Tim Chu40d45992020-12-14 21:44:40 -0800614 /* This field identifies a capability for the system, not the processor itself. */
615 t->max_speed = smbios_cpu_get_max_speed_mhz();
616
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800617 if (cpu_have_cpuid()) {
618 res = cpuid(1);
619
620 if ((res.ecx) & BIT(5))
621 characteristics |= BIT(6); /* BIT6: Enhanced Virtualization */
622
623 if ((res.edx) & BIT(28))
624 characteristics |= BIT(4); /* BIT4: Hardware Thread */
625
626 if (((cpuid_eax(0x80000000) - 0x80000000) + 1) > 2) {
627 res = cpuid(0x80000001);
628
629 if ((res.edx) & BIT(20))
630 characteristics |= BIT(5); /* BIT5: Execute Protection */
631 }
632 }
633 t->processor_characteristics = characteristics | smbios_processor_characteristics();
Patrick Rudolphb01ac7e2020-07-26 14:23:37 +0200634 cpu_voltage = smbios_cpu_get_voltage();
635 if (cpu_voltage > 0)
636 t->voltage = 0x80 | cpu_voltage;
Morgan Jang92bcc4f2020-07-24 10:36:18 +0800637
Angel Ponsa37701a2021-06-28 17:36:53 +0200638 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200639 *current += len;
640 return len;
641}
642
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100643/*
644 * Write SMBIOS type 7.
645 * Fill in some fields with constant values, as gathering the information
646 * from CPUID is impossible.
647 */
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200648static int smbios_write_type7(unsigned long *current,
649 const int handle,
650 const u8 level,
651 const u8 sram_type,
652 const enum smbios_cache_associativity associativity,
653 const enum smbios_cache_type type,
654 const size_t max_cache_size,
655 const size_t cache_size)
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100656{
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100657 char buf[8];
658
Angel Ponsd62a5012021-06-28 17:18:06 +0200659 struct smbios_type7 *t = smbios_carve_table(*current, SMBIOS_CACHE_INFORMATION,
660 sizeof(*t), handle);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100661
Morgan Jang8ae391d2020-10-06 16:26:17 +0800662 snprintf(buf, sizeof(buf), "CACHE%x", level);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100663 t->socket_designation = smbios_add_string(t->eos, buf);
664
665 t->cache_configuration = SMBIOS_CACHE_CONF_LEVEL(level) |
666 SMBIOS_CACHE_CONF_LOCATION(0) | /* Internal */
667 SMBIOS_CACHE_CONF_ENABLED(1) | /* Enabled */
Morgan Jang8ae391d2020-10-06 16:26:17 +0800668 SMBIOS_CACHE_CONF_OPERATION_MODE(smbios_cache_conf_operation_mode(level));
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100669
670 if (max_cache_size < (SMBIOS_CACHE_SIZE_MASK * KiB)) {
671 t->max_cache_size = max_cache_size / KiB;
672 t->max_cache_size2 = t->max_cache_size;
673
674 t->max_cache_size |= SMBIOS_CACHE_SIZE_UNIT_1KB;
675 t->max_cache_size2 |= SMBIOS_CACHE_SIZE2_UNIT_1KB;
676 } else {
Patrick Rudolph8f702672019-04-13 09:44:02 +0200677 if (max_cache_size < (SMBIOS_CACHE_SIZE_MASK * 64 * KiB))
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100678 t->max_cache_size = max_cache_size / (64 * KiB);
679 else
680 t->max_cache_size = SMBIOS_CACHE_SIZE_OVERFLOW;
681 t->max_cache_size2 = max_cache_size / (64 * KiB);
682
683 t->max_cache_size |= SMBIOS_CACHE_SIZE_UNIT_64KB;
684 t->max_cache_size2 |= SMBIOS_CACHE_SIZE2_UNIT_64KB;
685 }
686
687 if (cache_size < (SMBIOS_CACHE_SIZE_MASK * KiB)) {
688 t->installed_size = cache_size / KiB;
689 t->installed_size2 = t->installed_size;
690
691 t->installed_size |= SMBIOS_CACHE_SIZE_UNIT_1KB;
692 t->installed_size2 |= SMBIOS_CACHE_SIZE2_UNIT_1KB;
693 } else {
694 if (cache_size < (SMBIOS_CACHE_SIZE_MASK * 64 * KiB))
695 t->installed_size = cache_size / (64 * KiB);
696 else
697 t->installed_size = SMBIOS_CACHE_SIZE_OVERFLOW;
698 t->installed_size2 = cache_size / (64 * KiB);
699
700 t->installed_size |= SMBIOS_CACHE_SIZE_UNIT_64KB;
701 t->installed_size2 |= SMBIOS_CACHE_SIZE2_UNIT_64KB;
702 }
703
704 t->associativity = associativity;
705 t->supported_sram_type = sram_type;
706 t->current_sram_type = sram_type;
707 t->cache_speed = 0; /* Unknown */
Morgan Jang8ae391d2020-10-06 16:26:17 +0800708 t->error_correction_type = smbios_cache_error_correction_type(level);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100709 t->system_cache_type = type;
710
Angel Ponsa37701a2021-06-28 17:36:53 +0200711 const int len = smbios_full_table_len(&t->header, t->eos);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100712 *current += len;
713 return len;
714}
715
716/* Convert the associativity as integer to the SMBIOS enum if available */
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200717static enum smbios_cache_associativity smbios_cache_associativity(const u8 num)
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100718{
719 switch (num) {
720 case 1:
721 return SMBIOS_CACHE_ASSOCIATIVITY_DIRECT;
722 case 2:
723 return SMBIOS_CACHE_ASSOCIATIVITY_2WAY;
724 case 4:
725 return SMBIOS_CACHE_ASSOCIATIVITY_4WAY;
726 case 8:
727 return SMBIOS_CACHE_ASSOCIATIVITY_8WAY;
728 case 12:
729 return SMBIOS_CACHE_ASSOCIATIVITY_12WAY;
730 case 16:
731 return SMBIOS_CACHE_ASSOCIATIVITY_16WAY;
732 case 20:
733 return SMBIOS_CACHE_ASSOCIATIVITY_20WAY;
734 case 24:
735 return SMBIOS_CACHE_ASSOCIATIVITY_24WAY;
736 case 32:
737 return SMBIOS_CACHE_ASSOCIATIVITY_32WAY;
738 case 48:
739 return SMBIOS_CACHE_ASSOCIATIVITY_48WAY;
740 case 64:
741 return SMBIOS_CACHE_ASSOCIATIVITY_64WAY;
742 case 0xff:
743 return SMBIOS_CACHE_ASSOCIATIVITY_FULL;
744 default:
745 return SMBIOS_CACHE_ASSOCIATIVITY_UNKNOWN;
746 };
747}
748
749/*
750 * Parse the "Deterministic Cache Parameters" as provided by Intel in
751 * leaf 4 or AMD in extended leaf 0x8000001d.
752 *
753 * @param current Pointer to memory address to write the tables to
754 * @param handle Pointer to handle for the tables
755 * @param max_struct_size Pointer to maximum struct size
Patrick Rudolph15589b42019-03-30 17:51:06 +0100756 * @param type4 Pointer to SMBIOS type 4 structure
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100757 */
758static int smbios_write_type7_cache_parameters(unsigned long *current,
759 int *handle,
Patrick Rudolph15589b42019-03-30 17:51:06 +0100760 int *max_struct_size,
761 struct smbios_type4 *type4)
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100762{
Subrata Banike2b5fee2021-07-23 21:02:45 +0530763 unsigned int cnt = CACHE_L1D;
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100764 int len = 0;
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100765
766 if (!cpu_have_cpuid())
767 return len;
768
Subrata Banik6efc4aa2021-09-01 12:40:47 +0530769 enum cpu_type dcache_cpuid = cpu_check_deterministic_cache_cpuid_supported();
770 if (dcache_cpuid == CPUID_TYPE_INVALID || dcache_cpuid == CPUID_COMMAND_UNSUPPORTED) {
Subrata Banik5586c792021-09-02 13:17:18 +0530771 printk(BIOS_DEBUG, "SMBIOS: Unknown CPU or CPU doesn't support Deterministic "
772 "Cache CPUID leaf\n");
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100773 return len;
774 }
775
776 while (1) {
777 enum smbios_cache_associativity associativity;
778 enum smbios_cache_type type;
Subrata Banike2b5fee2021-07-23 21:02:45 +0530779 struct cpu_cache_info info;
780 if (!fill_cpu_cache_info(cnt++, &info))
781 continue;
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100782
Subrata Banike2b5fee2021-07-23 21:02:45 +0530783 const u8 cache_type = info.type;
784 const u8 level = info.level;
785 const size_t assoc = info.num_ways;
786 const size_t cache_share = info.num_cores_shared;
787 const size_t cache_size = info.size * get_number_of_caches(cache_share);
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100788
789 if (!cache_type)
790 /* No more caches in the system */
791 break;
792
793 switch (cache_type) {
794 case 1:
795 type = SMBIOS_CACHE_TYPE_DATA;
796 break;
797 case 2:
798 type = SMBIOS_CACHE_TYPE_INSTRUCTION;
799 break;
800 case 3:
801 type = SMBIOS_CACHE_TYPE_UNIFIED;
802 break;
803 default:
804 type = SMBIOS_CACHE_TYPE_UNKNOWN;
805 break;
806 }
807
Subrata Banike2b5fee2021-07-23 21:02:45 +0530808 if (info.fully_associative)
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100809 associativity = SMBIOS_CACHE_ASSOCIATIVITY_FULL;
810 else
811 associativity = smbios_cache_associativity(assoc);
812
Patrick Rudolph15589b42019-03-30 17:51:06 +0100813 const int h = (*handle)++;
814
815 update_max(len, *max_struct_size, smbios_write_type7(current, h,
Morgan Jang8ae391d2020-10-06 16:26:17 +0800816 level, smbios_cache_sram_type(), associativity,
Patrick Rudolph15589b42019-03-30 17:51:06 +0100817 type, cache_size, cache_size));
818
819 if (type4) {
820 switch (level) {
821 case 1:
822 type4->l1_cache_handle = h;
823 break;
824 case 2:
825 type4->l2_cache_handle = h;
826 break;
827 case 3:
828 type4->l3_cache_handle = h;
829 break;
830 }
831 }
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100832 };
833
834 return len;
835}
BryantOu4a8e58f2020-04-20 19:45:20 -0700836
837int smbios_write_type8(unsigned long *current, int *handle,
838 const struct port_information *port,
839 size_t num_ports)
840{
BryantOu4a8e58f2020-04-20 19:45:20 -0700841 unsigned int totallen = 0, i;
842
843 for (i = 0; i < num_ports; i++, port++) {
Angel Ponsd62a5012021-06-28 17:18:06 +0200844 struct smbios_type8 *t = smbios_carve_table(*current,
845 SMBIOS_PORT_CONNECTOR_INFORMATION,
846 sizeof(*t), *handle);
BryantOu4a8e58f2020-04-20 19:45:20 -0700847 t->internal_reference_designator =
848 smbios_add_string(t->eos, port->internal_reference_designator);
849 t->internal_connector_type = port->internal_connector_type;
850 t->external_reference_designator =
851 smbios_add_string(t->eos, port->external_reference_designator);
852 t->external_connector_type = port->external_connector_type;
853 t->port_type = port->port_type;
854 *handle += 1;
Angel Ponsa37701a2021-06-28 17:36:53 +0200855 const int len = smbios_full_table_len(&t->header, t->eos);
856 *current += len;
857 totallen += len;
BryantOu4a8e58f2020-04-20 19:45:20 -0700858 }
859 return totallen;
860}
861
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700862int smbios_write_type9(unsigned long *current, int *handle,
863 const char *name, const enum misc_slot_type type,
864 const enum slot_data_bus_bandwidth bandwidth,
865 const enum misc_slot_usage usage,
866 const enum misc_slot_length length,
JingleHsuWiwynn20fa59f2021-01-26 09:55:34 +0800867 const u16 id, u8 slot_char1, u8 slot_char2, u8 bus, u8 dev_func)
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700868{
Angel Ponsd62a5012021-06-28 17:18:06 +0200869 struct smbios_type9 *t = smbios_carve_table(*current, SMBIOS_SYSTEM_SLOTS,
870 sizeof(*t), *handle);
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700871
Angel Pons3c13da72020-07-29 18:23:23 +0200872 t->slot_designation = smbios_add_string(t->eos, name ? name : "SLOT");
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700873 t->slot_type = type;
874 /* TODO add slot_id supoort, will be "_SUN" for ACPI devices */
JingleHsuWiwynn20fa59f2021-01-26 09:55:34 +0800875 t->slot_id = id;
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700876 t->slot_data_bus_width = bandwidth;
877 t->current_usage = usage;
878 t->slot_length = length;
879 t->slot_characteristics_1 = slot_char1;
880 t->slot_characteristics_2 = slot_char2;
881 t->segment_group_number = 0;
882 t->bus_number = bus;
883 t->device_function_number = dev_func;
884 t->data_bus_width = SlotDataBusWidthOther;
885
Angel Ponsa37701a2021-06-28 17:36:53 +0200886 const int len = smbios_full_table_len(&t->header, t->eos);
Lijian Zhaoe98a7512019-04-11 23:28:09 -0700887 *current += len;
888 *handle += 1;
889 return len;
890}
Patrick Rudolphfc5b8092019-03-30 17:37:28 +0100891
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200892static int smbios_write_type11(unsigned long *current, int *handle)
Peter Stugec392b642013-07-06 19:51:12 +0200893{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100894 struct device *dev;
Angel Ponsd62a5012021-06-28 17:18:06 +0200895 struct smbios_type11 *t = smbios_carve_table(*current, SMBIOS_OEM_STRINGS,
896 sizeof(*t), *handle);
Peter Stugec392b642013-07-06 19:51:12 +0200897
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200898 for (dev = all_devices; dev; dev = dev->next) {
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200899 if (dev->ops && dev->ops->get_smbios_strings)
900 dev->ops->get_smbios_strings(dev, t);
901 }
902
903 if (t->count == 0) {
Lee Leahy0b5678f2017-03-16 16:01:40 -0700904 memset(t, 0, sizeof(*t));
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200905 return 0;
906 }
Peter Stugec392b642013-07-06 19:51:12 +0200907
Angel Ponsa37701a2021-06-28 17:36:53 +0200908 const int len = smbios_full_table_len(&t->header, t->eos);
Peter Stugec392b642013-07-06 19:51:12 +0200909 *current += len;
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200910 (*handle)++;
Peter Stugec392b642013-07-06 19:51:12 +0200911 return len;
912}
913
Patrick Rudolph5e007802020-07-27 15:37:43 +0200914static int smbios_write_type16(unsigned long *current, int *handle)
915{
Patrick Rudolph5e007802020-07-27 15:37:43 +0200916 int i;
Tim Chu1ee8ddc2021-01-22 01:10:45 -0800917 uint64_t max_capacity;
Patrick Rudolph5e007802020-07-27 15:37:43 +0200918
919 struct memory_info *meminfo;
920 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
921 if (meminfo == NULL)
922 return 0; /* can't find mem info in cbmem */
923
924 printk(BIOS_INFO, "Create SMBIOS type 16\n");
925
926 if (meminfo->max_capacity_mib == 0 || meminfo->number_of_devices == 0) {
927 /* Fill in defaults if not provided */
928 meminfo->number_of_devices = 0;
929 meminfo->max_capacity_mib = 0;
930 for (i = 0; i < meminfo->dimm_cnt && i < ARRAY_SIZE(meminfo->dimm); i++) {
931 meminfo->max_capacity_mib += meminfo->dimm[i].dimm_size;
932 meminfo->number_of_devices += !!meminfo->dimm[i].dimm_size;
933 }
934 }
935
Angel Ponsd62a5012021-06-28 17:18:06 +0200936 struct smbios_type16 *t = smbios_carve_table(*current, SMBIOS_PHYS_MEMORY_ARRAY,
937 sizeof(*t), *handle);
Patrick Rudolph5e007802020-07-27 15:37:43 +0200938
939 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
940 t->use = MEMORY_ARRAY_USE_SYSTEM;
Angel Pons6e82ebf2021-01-31 15:15:11 +0100941 t->memory_error_correction = meminfo->ecc_type;
Patrick Rudolph5e007802020-07-27 15:37:43 +0200942
943 /* no error information handle available */
944 t->memory_error_information_handle = 0xFFFE;
Tim Chu1ee8ddc2021-01-22 01:10:45 -0800945 max_capacity = meminfo->max_capacity_mib;
946 if (max_capacity * (MiB / KiB) < SMBIOS_USE_EXTENDED_MAX_CAPACITY)
947 t->maximum_capacity = max_capacity * (MiB / KiB);
948 else {
949 t->maximum_capacity = SMBIOS_USE_EXTENDED_MAX_CAPACITY;
950 t->extended_maximum_capacity = max_capacity * MiB;
951 }
Patrick Rudolph5e007802020-07-27 15:37:43 +0200952 t->number_of_memory_devices = meminfo->number_of_devices;
953
Angel Ponsa37701a2021-06-28 17:36:53 +0200954 const int len = smbios_full_table_len(&t->header, t->eos);
Patrick Rudolph5e007802020-07-27 15:37:43 +0200955 *current += len;
956 (*handle)++;
957 return len;
958}
959
960static int smbios_write_type17(unsigned long *current, int *handle, int type16)
Kane Chen33faac62014-07-27 12:54:44 -0700961{
Iru Cai36775202016-03-09 23:22:58 +0800962 int totallen = 0;
Kane Chen33faac62014-07-27 12:54:44 -0700963 int i;
964
965 struct memory_info *meminfo;
966 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
967 if (meminfo == NULL)
968 return 0; /* can't find mem info in cbmem */
969
970 printk(BIOS_INFO, "Create SMBIOS type 17\n");
Angel Ponsbf2f91c2020-07-29 18:14:59 +0200971 for (i = 0; i < meminfo->dimm_cnt && i < ARRAY_SIZE(meminfo->dimm); i++) {
Kane Chen33faac62014-07-27 12:54:44 -0700972 struct dimm_info *dimm;
973 dimm = &meminfo->dimm[i];
Patrick Rudolph5e007802020-07-27 15:37:43 +0200974 /*
975 * Windows 10 GetPhysicallyInstalledSystemMemory functions reads SMBIOS tables
976 * type 16 and type 17. The type 17 tables need to point to a type 16 table.
977 * Otherwise, the physical installed memory size is guessed from the system
978 * memory map, which results in a slightly smaller value than the actual size.
979 */
Angel Ponsc6dab802021-06-28 15:52:37 +0200980 const int len = create_smbios_type17_for_dimm(dimm, current, handle, type16);
Kane Chen33faac62014-07-27 12:54:44 -0700981 *current += len;
Iru Cai36775202016-03-09 23:22:58 +0800982 totallen += len;
Kane Chen33faac62014-07-27 12:54:44 -0700983 }
Iru Cai36775202016-03-09 23:22:58 +0800984 return totallen;
Kane Chen33faac62014-07-27 12:54:44 -0700985}
986
Tim Chue41f5952020-11-02 21:33:52 -0800987static int smbios_write_type19(unsigned long *current, int *handle, int type16)
Patrick Rudolph604295e2020-07-21 14:53:37 +0200988{
Patrick Rudolph604295e2020-07-21 14:53:37 +0200989 int i;
990
991 struct memory_info *meminfo;
992 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
993 if (meminfo == NULL)
994 return 0; /* can't find mem info in cbmem */
995
Angel Ponsd62a5012021-06-28 17:18:06 +0200996 struct smbios_type19 *t = smbios_carve_table(*current,
997 SMBIOS_MEMORY_ARRAY_MAPPED_ADDRESS,
998 sizeof(*t), *handle);
Patrick Rudolph604295e2020-07-21 14:53:37 +0200999
Tim Chue41f5952020-11-02 21:33:52 -08001000 t->memory_array_handle = type16;
Patrick Rudolph604295e2020-07-21 14:53:37 +02001001
1002 for (i = 0; i < meminfo->dimm_cnt && i < ARRAY_SIZE(meminfo->dimm); i++) {
1003 if (meminfo->dimm[i].dimm_size > 0) {
1004 t->extended_ending_address += meminfo->dimm[i].dimm_size;
1005 t->partition_width++;
1006 }
1007 }
1008 t->extended_ending_address *= MiB;
1009
1010 /* Check if it fits into regular address */
1011 if (t->extended_ending_address >= KiB &&
1012 t->extended_ending_address < 0x40000000000ULL) {
1013 /*
1014 * FIXME: The starting address is SoC specific, but SMBIOS tables are only
1015 * exported on x86 where it's always 0.
1016 */
1017
1018 t->starting_address = 0;
1019 t->ending_address = t->extended_ending_address / KiB - 1;
1020 t->extended_starting_address = ~0;
1021 t->extended_ending_address = ~0;
1022 } else {
1023 t->starting_address = ~0;
1024 t->ending_address = ~0;
1025 t->extended_starting_address = 0;
1026 t->extended_ending_address--;
1027 }
1028
Angel Ponsa37701a2021-06-28 17:36:53 +02001029 const int len = smbios_full_table_len(&t->header, t->eos);
Patrick Rudolph604295e2020-07-21 14:53:37 +02001030 *current += len;
1031 *handle += 1;
1032 return len;
1033}
1034
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001035static int smbios_write_type20_table(unsigned long *current, int *handle, u32 addr_start,
1036 u32 addr_end, int type17_handle, int type19_handle)
1037{
1038 struct smbios_type20 *t = smbios_carve_table(*current, SMBIOS_MEMORY_DEVICE_MAPPED_ADDRESS,
1039 sizeof(*t), *handle);
1040
1041 t->memory_device_handle = type17_handle;
1042 t->memory_array_mapped_address_handle = type19_handle;
1043 t->addr_start = addr_start;
1044 t->addr_end = addr_end;
1045 t->partition_row_pos = 0xff;
1046 t->interleave_pos = 0xff;
1047 t->interleave_depth = 0xff;
1048
1049 const int len = smbios_full_table_len(&t->header, t->eos);
1050 *current += len;
1051 *handle += 1;
1052 return len;
1053}
1054
1055static int smbios_write_type20(unsigned long *current, int *handle,
1056 int type17_handle, int type19_handle)
1057{
1058 u32 start_addr = 0;
1059 int totallen = 0;
1060 int i;
1061
1062 struct memory_info *meminfo;
1063 meminfo = cbmem_find(CBMEM_ID_MEMINFO);
1064 if (meminfo == NULL)
1065 return 0; /* can't find mem info in cbmem */
1066
1067 printk(BIOS_INFO, "Create SMBIOS type 20\n");
1068 for (i = 0; i < meminfo->dimm_cnt && i < ARRAY_SIZE(meminfo->dimm); i++) {
1069 struct dimm_info *dimm;
1070 dimm = &meminfo->dimm[i];
1071 u32 end_addr = start_addr + (dimm->dimm_size << 10) - 1;
1072 totallen += smbios_write_type20_table(current, handle, start_addr, end_addr,
1073 type17_handle, type19_handle);
1074 start_addr = end_addr + 1;
1075 }
1076 return totallen;
1077}
1078
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001079static int smbios_write_type32(unsigned long *current, int handle)
1080{
Angel Ponsd62a5012021-06-28 17:18:06 +02001081 struct smbios_type32 *t = smbios_carve_table(*current, SMBIOS_SYSTEM_BOOT_INFORMATION,
1082 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001083
Angel Ponsa37701a2021-06-28 17:36:53 +02001084 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001085 *current += len;
1086 return len;
1087}
1088
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001089int smbios_write_type38(unsigned long *current, int *handle,
1090 const enum smbios_bmc_interface_type interface_type,
1091 const u8 ipmi_rev, const u8 i2c_addr, const u8 nv_addr,
1092 const u64 base_addr, const u8 base_modifier,
1093 const u8 irq)
1094{
Angel Ponsd62a5012021-06-28 17:18:06 +02001095 struct smbios_type38 *t = smbios_carve_table(*current, SMBIOS_IPMI_DEVICE_INFORMATION,
1096 sizeof(*t), *handle);
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001097
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001098 t->interface_type = interface_type;
1099 t->ipmi_rev = ipmi_rev;
1100 t->i2c_slave_addr = i2c_addr;
1101 t->nv_storage_addr = nv_addr;
1102 t->base_address = base_addr;
1103 t->base_address_modifier = base_modifier;
1104 t->irq = irq;
1105
Angel Ponsa37701a2021-06-28 17:36:53 +02001106 const int len = smbios_full_table_len(&t->header, t->eos);
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001107 *current += len;
1108 *handle += 1;
Patrick Rudolphfe98e902018-03-27 16:17:12 +02001109 return len;
1110}
1111
Duncan Laurie21a78702013-05-23 14:17:05 -07001112int smbios_write_type41(unsigned long *current, int *handle,
1113 const char *name, u8 instance, u16 segment,
Christian Waltere6afab12019-05-21 17:22:49 +02001114 u8 bus, u8 device, u8 function, u8 device_type)
Duncan Laurie21a78702013-05-23 14:17:05 -07001115{
Angel Ponsd62a5012021-06-28 17:18:06 +02001116 struct smbios_type41 *t = smbios_carve_table(*current,
1117 SMBIOS_ONBOARD_DEVICES_EXTENDED_INFORMATION,
1118 sizeof(*t), *handle);
Duncan Laurie21a78702013-05-23 14:17:05 -07001119
Duncan Laurie21a78702013-05-23 14:17:05 -07001120 t->reference_designation = smbios_add_string(t->eos, name);
Christian Waltere6afab12019-05-21 17:22:49 +02001121 t->device_type = device_type;
Duncan Laurie21a78702013-05-23 14:17:05 -07001122 t->device_status = 1;
1123 t->device_type_instance = instance;
1124 t->segment_group_number = segment;
1125 t->bus_number = bus;
1126 t->device_number = device;
1127 t->function_number = function;
1128
Angel Ponsa37701a2021-06-28 17:36:53 +02001129 const int len = smbios_full_table_len(&t->header, t->eos);
Duncan Laurie21a78702013-05-23 14:17:05 -07001130 *current += len;
1131 *handle += 1;
1132 return len;
1133}
1134
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001135static int smbios_write_type127(unsigned long *current, int handle)
1136{
Angel Ponsd62a5012021-06-28 17:18:06 +02001137 struct smbios_type127 *t = smbios_carve_table(*current, SMBIOS_END_OF_TABLE,
1138 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001139
Angel Ponsa37701a2021-06-28 17:36:53 +02001140 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001141 *current += len;
1142 return len;
1143}
1144
Angel Ponsbe2e2bb2021-08-27 10:34:05 +02001145/* Get the device type 41 from the dev struct */
1146static u8 smbios_get_device_type_from_dev(struct device *dev)
1147{
1148 u16 pci_basesubclass = (dev->class >> 8) & 0xFFFF;
1149
1150 switch (pci_basesubclass) {
1151 case PCI_CLASS_NOT_DEFINED:
1152 return SMBIOS_DEVICE_TYPE_OTHER;
1153 case PCI_CLASS_DISPLAY_VGA:
1154 case PCI_CLASS_DISPLAY_XGA:
1155 case PCI_CLASS_DISPLAY_3D:
1156 case PCI_CLASS_DISPLAY_OTHER:
1157 return SMBIOS_DEVICE_TYPE_VIDEO;
1158 case PCI_CLASS_STORAGE_SCSI:
1159 return SMBIOS_DEVICE_TYPE_SCSI;
1160 case PCI_CLASS_NETWORK_ETHERNET:
1161 return SMBIOS_DEVICE_TYPE_ETHERNET;
1162 case PCI_CLASS_NETWORK_TOKEN_RING:
1163 return SMBIOS_DEVICE_TYPE_TOKEN_RING;
1164 case PCI_CLASS_MULTIMEDIA_VIDEO:
1165 case PCI_CLASS_MULTIMEDIA_AUDIO:
1166 case PCI_CLASS_MULTIMEDIA_PHONE:
1167 case PCI_CLASS_MULTIMEDIA_OTHER:
1168 return SMBIOS_DEVICE_TYPE_SOUND;
1169 case PCI_CLASS_STORAGE_ATA:
1170 return SMBIOS_DEVICE_TYPE_PATA;
1171 case PCI_CLASS_STORAGE_SATA:
1172 return SMBIOS_DEVICE_TYPE_SATA;
1173 case PCI_CLASS_STORAGE_SAS:
1174 return SMBIOS_DEVICE_TYPE_SAS;
1175 default:
1176 return SMBIOS_DEVICE_TYPE_UNKNOWN;
1177 }
1178}
1179
Angel Pons437da712021-09-03 16:51:40 +02001180static bool smbios_get_type41_instance_id(struct device *dev, u8 device_type, u8 *instance_id)
1181{
1182#if CONFIG(SMBIOS_TYPE41_PROVIDED_BY_DEVTREE)
1183 *instance_id = dev->smbios_instance_id;
1184 return dev->smbios_instance_id_valid;
1185#else
1186 static u8 type41_inst_cnt[SMBIOS_DEVICE_TYPE_COUNT + 1] = {};
1187
1188 if (device_type == SMBIOS_DEVICE_TYPE_OTHER ||
1189 device_type == SMBIOS_DEVICE_TYPE_UNKNOWN)
1190 return false;
1191
1192 if (device_type > SMBIOS_DEVICE_TYPE_COUNT)
1193 return false;
1194
1195 *instance_id = type41_inst_cnt[device_type]++;
1196 return true;
1197#endif
1198}
1199
1200static const char *smbios_get_type41_refdes(struct device *dev)
1201{
1202#if CONFIG(SMBIOS_TYPE41_PROVIDED_BY_DEVTREE)
1203 if (dev->smbios_refdes)
1204 return dev->smbios_refdes;
1205#endif
1206 return get_pci_subclass_name(dev);
1207}
1208
Angel Pons12b809b2021-09-03 11:58:04 +02001209static int smbios_generate_type41_from_devtree(struct device *dev, int *handle,
1210 unsigned long *current)
Christian Walter9e5b0622019-05-21 17:37:58 +02001211{
Christian Walter9e5b0622019-05-21 17:37:58 +02001212 if (dev->path.type != DEVICE_PATH_PCI)
1213 return 0;
1214 if (!dev->on_mainboard)
1215 return 0;
1216
Angel Pons437da712021-09-03 16:51:40 +02001217 const u8 device_type = smbios_get_device_type_from_dev(dev);
Christian Walter9e5b0622019-05-21 17:37:58 +02001218
Angel Pons437da712021-09-03 16:51:40 +02001219 u8 instance_id;
1220
1221 if (!smbios_get_type41_instance_id(dev, device_type, &instance_id))
Christian Walter9e5b0622019-05-21 17:37:58 +02001222 return 0;
1223
Angel Pons437da712021-09-03 16:51:40 +02001224 const char *name = smbios_get_type41_refdes(dev);
Christian Walter9e5b0622019-05-21 17:37:58 +02001225
1226 return smbios_write_type41(current, handle,
1227 name, // name
Angel Pons437da712021-09-03 16:51:40 +02001228 instance_id, // inst
Christian Walter9e5b0622019-05-21 17:37:58 +02001229 0, // segment
1230 dev->bus->secondary, //bus
1231 PCI_SLOT(dev->path.pci.devfn), // device
1232 PCI_FUNC(dev->path.pci.devfn), // func
1233 device_type);
1234}
1235
Angel Pons12b809b2021-09-03 11:58:04 +02001236static int smbios_generate_type9_from_devtree(struct device *dev, int *handle,
1237 unsigned long *current)
Patrick Rudolphf5b93692019-04-12 15:59:40 +02001238{
1239 enum misc_slot_usage usage;
1240 enum slot_data_bus_bandwidth bandwidth;
1241 enum misc_slot_type type;
1242 enum misc_slot_length length;
1243
1244 if (dev->path.type != DEVICE_PATH_PCI)
1245 return 0;
1246
1247 if (!dev->smbios_slot_type && !dev->smbios_slot_data_width &&
1248 !dev->smbios_slot_designation && !dev->smbios_slot_length)
1249 return 0;
1250
1251 if (dev_is_active_bridge(dev))
1252 usage = SlotUsageInUse;
1253 else if (dev->enabled)
1254 usage = SlotUsageAvailable;
1255 else
1256 usage = SlotUsageUnknown;
1257
1258 if (dev->smbios_slot_data_width)
1259 bandwidth = dev->smbios_slot_data_width;
1260 else
1261 bandwidth = SlotDataBusWidthUnknown;
1262
1263 if (dev->smbios_slot_type)
1264 type = dev->smbios_slot_type;
1265 else
1266 type = SlotTypeUnknown;
1267
1268 if (dev->smbios_slot_length)
1269 length = dev->smbios_slot_length;
1270 else
1271 length = SlotLengthUnknown;
1272
1273 return smbios_write_type9(current, handle,
1274 dev->smbios_slot_designation,
1275 type,
1276 bandwidth,
1277 usage,
1278 length,
JingleHsuWiwynn20fa59f2021-01-26 09:55:34 +08001279 0,
Patrick Rudolphf5b93692019-04-12 15:59:40 +02001280 1,
1281 0,
1282 dev->bus->secondary,
1283 dev->path.pci.devfn);
1284}
1285
Angel Pons6a73b242021-09-03 12:18:10 +02001286int get_smbios_data(struct device *dev, int *handle, unsigned long *current)
1287{
1288 int len = 0;
1289
1290 len += smbios_generate_type9_from_devtree(dev, handle, current);
1291 len += smbios_generate_type41_from_devtree(dev, handle, current);
1292
1293 return len;
1294}
1295
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001296static int smbios_walk_device_tree(struct device *tree, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001297{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +11001298 struct device *dev;
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001299 int len = 0;
1300
Elyes HAOUASdbf30672016-08-21 17:37:15 +02001301 for (dev = tree; dev; dev = dev->next) {
Angel Pons8b98f8b2021-09-07 14:00:17 +02001302 if (!dev->enabled)
1303 continue;
1304
1305 if (dev->ops && dev->ops->get_smbios_data) {
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001306 printk(BIOS_INFO, "%s (%s)\n", dev_path(dev), dev_name(dev));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001307 len += dev->ops->get_smbios_data(dev, handle, current);
Angel Pons6a73b242021-09-03 12:18:10 +02001308 } else {
1309 len += get_smbios_data(dev, handle, current);
Naresh G Solanki20c893e2018-06-05 18:58:53 +05301310 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001311 }
1312 return len;
1313}
1314
1315unsigned long smbios_write_tables(unsigned long current)
1316{
1317 struct smbios_entry *se;
Patrick Rudolph7a835822020-07-22 16:00:53 +02001318 struct smbios_entry30 *se3;
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001319 unsigned long tables;
Ben Frisch72af5d72015-05-09 19:52:18 -05001320 int len = 0;
1321 int max_struct_size = 0;
1322 int handle = 0;
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001323
Felix Heldfcbb3c52019-06-20 14:01:54 +02001324 current = ALIGN_UP(current, 16);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001325 printk(BIOS_DEBUG, "%s: %08lx\n", __func__, current);
1326
1327 se = (struct smbios_entry *)current;
Angel Pons35b99c62021-06-28 15:36:23 +02001328 current += sizeof(*se);
Felix Heldfcbb3c52019-06-20 14:01:54 +02001329 current = ALIGN_UP(current, 16);
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001330
Patrick Rudolph7a835822020-07-22 16:00:53 +02001331 se3 = (struct smbios_entry30 *)current;
Angel Pons35b99c62021-06-28 15:36:23 +02001332 current += sizeof(*se3);
Patrick Rudolph7a835822020-07-22 16:00:53 +02001333 current = ALIGN_UP(current, 16);
1334
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001335 tables = current;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001336 update_max(len, max_struct_size, smbios_write_type0(&current, handle++));
1337 update_max(len, max_struct_size, smbios_write_type1(&current, handle++));
1338
1339 /* The chassis handle is the next one */
1340 update_max(len, max_struct_size, smbios_write_type2(&current, handle, handle + 1));
Julien Viard de Galbert9a31dfe2018-02-22 16:39:58 +01001341 handle++;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001342 update_max(len, max_struct_size, smbios_write_type3(&current, handle++));
Patrick Rudolph15589b42019-03-30 17:51:06 +01001343
1344 struct smbios_type4 *type4 = (struct smbios_type4 *)current;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001345 update_max(len, max_struct_size, smbios_write_type4(&current, handle++));
1346 len += smbios_write_type7_cache_parameters(&current, &handle, &max_struct_size, type4);
1347 update_max(len, max_struct_size, smbios_write_type11(&current, &handle));
Julius Wernercd49cce2019-03-05 16:53:33 -08001348 if (CONFIG(ELOG))
Martin Roth898a7752017-06-01 11:39:59 -06001349 update_max(len, max_struct_size,
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001350 elog_smbios_write_type15(&current, handle++));
Patrick Rudolph5e007802020-07-27 15:37:43 +02001351
1352 const int type16 = handle;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001353 update_max(len, max_struct_size, smbios_write_type16(&current, &handle));
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001354 const int type17 = handle;
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001355 update_max(len, max_struct_size, smbios_write_type17(&current, &handle, type16));
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001356 const int type19 = handle;
Tim Chue41f5952020-11-02 21:33:52 -08001357 update_max(len, max_struct_size, smbios_write_type19(&current, &handle, type16));
Matt DeVillierd1c1afd2016-09-02 21:41:26 -05001358 update_max(len, max_struct_size,
1359 smbios_write_type20(&current, &handle, type17, type19));
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001360 update_max(len, max_struct_size, smbios_write_type32(&current, handle++));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001361
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001362 update_max(len, max_struct_size, smbios_walk_device_tree(all_devices,
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001363 &handle, &current));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001364
Angel Ponsbf2f91c2020-07-29 18:14:59 +02001365 update_max(len, max_struct_size, smbios_write_type127(&current, handle++));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001366
Patrick Rudolph7a835822020-07-22 16:00:53 +02001367 /* Install SMBIOS 2.1 entry point */
Angel Pons35b99c62021-06-28 15:36:23 +02001368 memset(se, 0, sizeof(*se));
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001369 memcpy(se->anchor, "_SM_", 4);
Angel Pons35b99c62021-06-28 15:36:23 +02001370 se->length = sizeof(*se);
Patrick Rudolph7a835822020-07-22 16:00:53 +02001371 se->major_version = 3;
1372 se->minor_version = 0;
Ben Frisch72af5d72015-05-09 19:52:18 -05001373 se->max_struct_size = max_struct_size;
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001374 se->struct_count = handle;
1375 memcpy(se->intermediate_anchor_string, "_DMI_", 5);
1376
1377 se->struct_table_address = (u32)tables;
1378 se->struct_table_length = len;
1379
Angel Pons35b99c62021-06-28 15:36:23 +02001380 se->intermediate_checksum = smbios_checksum((u8 *)se + 0x10, sizeof(*se) - 0x10);
1381 se->checksum = smbios_checksum((u8 *)se, sizeof(*se));
Patrick Rudolph7a835822020-07-22 16:00:53 +02001382
1383 /* Install SMBIOS 3.0 entry point */
Angel Pons35b99c62021-06-28 15:36:23 +02001384 memset(se3, 0, sizeof(*se3));
Patrick Rudolph7a835822020-07-22 16:00:53 +02001385 memcpy(se3->anchor, "_SM3_", 5);
Angel Pons35b99c62021-06-28 15:36:23 +02001386 se3->length = sizeof(*se3);
Patrick Rudolph7a835822020-07-22 16:00:53 +02001387 se3->major_version = 3;
1388 se3->minor_version = 0;
1389
1390 se3->struct_table_address = (u64)tables;
1391 se3->struct_table_length = len;
1392
Angel Pons35b99c62021-06-28 15:36:23 +02001393 se3->checksum = smbios_checksum((u8 *)se3, sizeof(*se3));
Patrick Rudolph7a835822020-07-22 16:00:53 +02001394
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001395 return current;
1396}