Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <dimm_info_util.h> |
| 4 | #include <spd.h> |
| 5 | #include <tests/test.h> |
| 6 | |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 7 | static void test_smbios_bus_width_to_spd_width_parametrized(smbios_memory_type ddr_type) |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 8 | { |
| 9 | /* Non-ECC variants */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 10 | assert_int_equal(MEMORY_BUS_WIDTH_64, smbios_bus_width_to_spd_width(ddr_type, 64, 64)); |
| 11 | assert_int_equal(MEMORY_BUS_WIDTH_32, smbios_bus_width_to_spd_width(ddr_type, 32, 32)); |
| 12 | assert_int_equal(MEMORY_BUS_WIDTH_16, smbios_bus_width_to_spd_width(ddr_type, 16, 16)); |
| 13 | assert_int_equal(MEMORY_BUS_WIDTH_8, smbios_bus_width_to_spd_width(ddr_type, 8, 8)); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 14 | /* Incorrect data width. Fallback to 8-bit */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 15 | assert_int_equal(MEMORY_BUS_WIDTH_8, smbios_bus_width_to_spd_width(ddr_type, 15, 15)); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 16 | |
| 17 | /* ECC variants */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 18 | uint8_t extension_8bits = SPD_ECC_8BIT; |
| 19 | if (ddr_type == MEMORY_TYPE_DDR5 || ddr_type == MEMORY_TYPE_LPDDR5) |
| 20 | extension_8bits = SPD_ECC_8BIT_LP5_DDR5; |
| 21 | |
| 22 | assert_int_equal(MEMORY_BUS_WIDTH_64 | extension_8bits, |
| 23 | smbios_bus_width_to_spd_width(ddr_type, 64 + 8, 64)); |
| 24 | assert_int_equal(MEMORY_BUS_WIDTH_32 | extension_8bits, |
| 25 | smbios_bus_width_to_spd_width(ddr_type, 32 + 8, 32)); |
| 26 | assert_int_equal(MEMORY_BUS_WIDTH_16 | extension_8bits, |
| 27 | smbios_bus_width_to_spd_width(ddr_type, 16 + 8, 16)); |
| 28 | assert_int_equal(MEMORY_BUS_WIDTH_8 | extension_8bits, |
| 29 | smbios_bus_width_to_spd_width(ddr_type, 8 + 8, 8)); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 30 | /* Incorrect data width. Fallback to 8-bit */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 31 | assert_int_equal(MEMORY_BUS_WIDTH_8 | extension_8bits, |
| 32 | smbios_bus_width_to_spd_width(ddr_type, 15 + 8, 15)); |
| 33 | } |
| 34 | |
| 35 | static void test_smbios_bus_width_to_spd_width(void **state) |
| 36 | { |
| 37 | smbios_memory_type memory_type[] = { |
| 38 | MEMORY_TYPE_DDR2, |
| 39 | MEMORY_TYPE_DDR3, |
| 40 | MEMORY_TYPE_DDR4, |
| 41 | MEMORY_TYPE_DDR5, |
| 42 | MEMORY_TYPE_LPDDR3, |
| 43 | MEMORY_TYPE_LPDDR4, |
| 44 | MEMORY_TYPE_LPDDR5, |
| 45 | }; |
| 46 | |
| 47 | for (int i = 0; i < ARRAY_SIZE(memory_type); i++) { |
| 48 | print_message("test_smbios_bus_width_to_spd_width_parametrized(%d)\n", |
| 49 | memory_type[i]); |
| 50 | test_smbios_bus_width_to_spd_width_parametrized(memory_type[i]); |
| 51 | } |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void test_smbios_memory_size_to_mib(void **state) |
| 55 | { |
| 56 | uint32_t extended_size; |
| 57 | uint16_t memory_size; |
| 58 | |
| 59 | /* Unknown memory size */ |
| 60 | assert_int_equal(0, smbios_memory_size_to_mib(0xFFFF, 0)); |
| 61 | assert_int_equal(0, smbios_memory_size_to_mib(0xFFFF, 0xFFFF)); |
| 62 | assert_int_equal(0, smbios_memory_size_to_mib(0xFFFF, 87642)); |
| 63 | |
| 64 | /* 32GiB - 1MiB */ |
| 65 | extended_size = 0; |
| 66 | assert_int_equal(extended_size, smbios_memory_size_to_mib(0x7FFF, extended_size)); |
| 67 | extended_size = 0xFFFFFFFF; |
| 68 | assert_int_equal(extended_size, smbios_memory_size_to_mib(0x7FFF, extended_size)); |
| 69 | extended_size = 0xDEDE6666; |
| 70 | assert_int_equal(extended_size, smbios_memory_size_to_mib(0x7FFF, extended_size)); |
| 71 | |
| 72 | /* Memory size in KiB when MSB is flipped */ |
| 73 | memory_size = 0x0 & 0x8000; /* Zero bytes */ |
| 74 | assert_int_equal(0, smbios_memory_size_to_mib(memory_size, 0)); |
| 75 | assert_int_equal(0, smbios_memory_size_to_mib(memory_size, 0xFFFFFFFF)); |
| 76 | assert_int_equal(0, smbios_memory_size_to_mib(memory_size, 2345568)); |
| 77 | memory_size = (31 * KiB) | 0x8000; |
| 78 | assert_int_equal(31, smbios_memory_size_to_mib(memory_size, 0)); |
| 79 | assert_int_equal(31, smbios_memory_size_to_mib(memory_size, 0xFFFFFFFF)); |
| 80 | assert_int_equal(31, smbios_memory_size_to_mib(memory_size, 72594344)); |
| 81 | |
| 82 | /* Value in MiB Only when memory size is not 0xFFFF and 0x7FFF and MSB is not set */ |
| 83 | memory_size = 32766; /* value in MiB */ |
| 84 | assert_int_equal(memory_size, smbios_memory_size_to_mib(memory_size, 0)); |
| 85 | assert_int_equal(memory_size, smbios_memory_size_to_mib(memory_size, 0xFFFFFFFF)); |
| 86 | assert_int_equal(memory_size, smbios_memory_size_to_mib(memory_size, 694735)); |
| 87 | } |
| 88 | |
| 89 | static void test_smbios_form_factor_to_spd_mod_type(void **state) |
| 90 | { |
| 91 | /* Form factors defined in coreboot */ |
| 92 | const LargestIntegralType udimm_allowed[] = { |
| 93 | SPD_UDIMM, SPD_MICRO_DIMM, SPD_MINI_UDIMM, |
| 94 | }; |
| 95 | assert_in_set(smbios_form_factor_to_spd_mod_type(MEMORY_FORMFACTOR_DIMM), |
| 96 | udimm_allowed, ARRAY_SIZE(udimm_allowed)); |
| 97 | |
| 98 | const LargestIntegralType rdimm_allowed[] = { SPD_RDIMM, SPD_MINI_RDIMM }; |
| 99 | assert_in_set(smbios_form_factor_to_spd_mod_type(MEMORY_FORMFACTOR_RIMM), |
| 100 | rdimm_allowed, ARRAY_SIZE(rdimm_allowed)); |
| 101 | |
| 102 | assert_int_equal(SPD_SODIMM, |
| 103 | smbios_form_factor_to_spd_mod_type(MEMORY_FORMFACTOR_SODIMM)); |
| 104 | |
| 105 | const smbios_memory_form_factor undefined_factors[] = { |
| 106 | MEMORY_FORMFACTOR_OTHER, |
| 107 | MEMORY_FORMFACTOR_UNKNOWN, |
| 108 | MEMORY_FORMFACTOR_SIMM, |
| 109 | MEMORY_FORMFACTOR_SIP, |
| 110 | MEMORY_FORMFACTOR_CHIP, |
| 111 | MEMORY_FORMFACTOR_DIP, |
| 112 | MEMORY_FORMFACTOR_ZIP, |
| 113 | MEMORY_FORMFACTOR_PROPRIETARY_CARD, |
| 114 | MEMORY_FORMFACTOR_TSOP, |
| 115 | MEMORY_FORMFACTOR_ROC, |
| 116 | MEMORY_FORMFACTOR_SRIMM, |
| 117 | MEMORY_FORMFACTOR_FBDIMM, |
| 118 | MEMORY_FORMFACTOR_DIE, |
| 119 | }; |
| 120 | for (int i = 0; i < ARRAY_SIZE(undefined_factors); ++i) { |
| 121 | assert_int_equal(SPD_UNDEFINED, |
| 122 | smbios_form_factor_to_spd_mod_type(undefined_factors[i])); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | int main(void) |
| 127 | { |
| 128 | const struct CMUnitTest tests[] = { |
| 129 | cmocka_unit_test(test_smbios_bus_width_to_spd_width), |
| 130 | cmocka_unit_test(test_smbios_memory_size_to_mib), |
| 131 | cmocka_unit_test(test_smbios_form_factor_to_spd_mod_type), |
| 132 | }; |
| 133 | |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 134 | return cb_run_group_tests(tests, NULL, NULL); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 135 | } |