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 | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 7 | #define MAX_ALLOWED_MODULE_TYPE 3 |
| 8 | |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 9 | 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] | 10 | { |
| 11 | /* Non-ECC variants */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 12 | assert_int_equal(MEMORY_BUS_WIDTH_64, smbios_bus_width_to_spd_width(ddr_type, 64, 64)); |
| 13 | assert_int_equal(MEMORY_BUS_WIDTH_32, smbios_bus_width_to_spd_width(ddr_type, 32, 32)); |
| 14 | assert_int_equal(MEMORY_BUS_WIDTH_16, smbios_bus_width_to_spd_width(ddr_type, 16, 16)); |
| 15 | 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] | 16 | /* Incorrect data width. Fallback to 8-bit */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 17 | 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] | 18 | |
| 19 | /* ECC variants */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 20 | uint8_t extension_8bits = SPD_ECC_8BIT; |
| 21 | if (ddr_type == MEMORY_TYPE_DDR5 || ddr_type == MEMORY_TYPE_LPDDR5) |
| 22 | extension_8bits = SPD_ECC_8BIT_LP5_DDR5; |
| 23 | |
| 24 | assert_int_equal(MEMORY_BUS_WIDTH_64 | extension_8bits, |
| 25 | smbios_bus_width_to_spd_width(ddr_type, 64 + 8, 64)); |
| 26 | assert_int_equal(MEMORY_BUS_WIDTH_32 | extension_8bits, |
| 27 | smbios_bus_width_to_spd_width(ddr_type, 32 + 8, 32)); |
| 28 | assert_int_equal(MEMORY_BUS_WIDTH_16 | extension_8bits, |
| 29 | smbios_bus_width_to_spd_width(ddr_type, 16 + 8, 16)); |
| 30 | assert_int_equal(MEMORY_BUS_WIDTH_8 | extension_8bits, |
| 31 | smbios_bus_width_to_spd_width(ddr_type, 8 + 8, 8)); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 32 | /* Incorrect data width. Fallback to 8-bit */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 33 | assert_int_equal(MEMORY_BUS_WIDTH_8 | extension_8bits, |
| 34 | smbios_bus_width_to_spd_width(ddr_type, 15 + 8, 15)); |
| 35 | } |
| 36 | |
| 37 | static void test_smbios_bus_width_to_spd_width(void **state) |
| 38 | { |
| 39 | smbios_memory_type memory_type[] = { |
| 40 | MEMORY_TYPE_DDR2, |
| 41 | MEMORY_TYPE_DDR3, |
| 42 | MEMORY_TYPE_DDR4, |
| 43 | MEMORY_TYPE_DDR5, |
| 44 | MEMORY_TYPE_LPDDR3, |
| 45 | MEMORY_TYPE_LPDDR4, |
| 46 | MEMORY_TYPE_LPDDR5, |
| 47 | }; |
| 48 | |
| 49 | for (int i = 0; i < ARRAY_SIZE(memory_type); i++) { |
| 50 | print_message("test_smbios_bus_width_to_spd_width_parametrized(%d)\n", |
| 51 | memory_type[i]); |
| 52 | test_smbios_bus_width_to_spd_width_parametrized(memory_type[i]); |
| 53 | } |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void test_smbios_memory_size_to_mib(void **state) |
| 57 | { |
| 58 | uint32_t extended_size; |
| 59 | uint16_t memory_size; |
| 60 | |
| 61 | /* Unknown memory size */ |
| 62 | assert_int_equal(0, smbios_memory_size_to_mib(0xFFFF, 0)); |
| 63 | assert_int_equal(0, smbios_memory_size_to_mib(0xFFFF, 0xFFFF)); |
| 64 | assert_int_equal(0, smbios_memory_size_to_mib(0xFFFF, 87642)); |
| 65 | |
| 66 | /* 32GiB - 1MiB */ |
| 67 | extended_size = 0; |
| 68 | assert_int_equal(extended_size, smbios_memory_size_to_mib(0x7FFF, extended_size)); |
| 69 | extended_size = 0xFFFFFFFF; |
| 70 | assert_int_equal(extended_size, smbios_memory_size_to_mib(0x7FFF, extended_size)); |
| 71 | extended_size = 0xDEDE6666; |
| 72 | assert_int_equal(extended_size, smbios_memory_size_to_mib(0x7FFF, extended_size)); |
| 73 | |
| 74 | /* Memory size in KiB when MSB is flipped */ |
| 75 | memory_size = 0x0 & 0x8000; /* Zero bytes */ |
| 76 | assert_int_equal(0, smbios_memory_size_to_mib(memory_size, 0)); |
| 77 | assert_int_equal(0, smbios_memory_size_to_mib(memory_size, 0xFFFFFFFF)); |
| 78 | assert_int_equal(0, smbios_memory_size_to_mib(memory_size, 2345568)); |
| 79 | memory_size = (31 * KiB) | 0x8000; |
| 80 | assert_int_equal(31, smbios_memory_size_to_mib(memory_size, 0)); |
| 81 | assert_int_equal(31, smbios_memory_size_to_mib(memory_size, 0xFFFFFFFF)); |
| 82 | assert_int_equal(31, smbios_memory_size_to_mib(memory_size, 72594344)); |
| 83 | |
| 84 | /* Value in MiB Only when memory size is not 0xFFFF and 0x7FFF and MSB is not set */ |
| 85 | memory_size = 32766; /* value in MiB */ |
| 86 | assert_int_equal(memory_size, smbios_memory_size_to_mib(memory_size, 0)); |
| 87 | assert_int_equal(memory_size, smbios_memory_size_to_mib(memory_size, 0xFFFFFFFF)); |
| 88 | assert_int_equal(memory_size, smbios_memory_size_to_mib(memory_size, 694735)); |
| 89 | } |
| 90 | |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 91 | static void test_smbios_form_factor_to_spd_mod_type_ddr(smbios_memory_type memory_type) |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 92 | { |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 93 | const smbios_memory_form_factor undefined_factors[] = { |
| 94 | MEMORY_FORMFACTOR_OTHER, |
| 95 | MEMORY_FORMFACTOR_UNKNOWN, |
| 96 | MEMORY_FORMFACTOR_SIMM, |
| 97 | MEMORY_FORMFACTOR_SIP, |
| 98 | MEMORY_FORMFACTOR_CHIP, |
| 99 | MEMORY_FORMFACTOR_DIP, |
| 100 | MEMORY_FORMFACTOR_ZIP, |
| 101 | MEMORY_FORMFACTOR_PROPRIETARY_CARD, |
| 102 | MEMORY_FORMFACTOR_TSOP, |
| 103 | MEMORY_FORMFACTOR_ROC, |
| 104 | MEMORY_FORMFACTOR_SRIMM, |
| 105 | MEMORY_FORMFACTOR_FBDIMM, |
| 106 | MEMORY_FORMFACTOR_DIE, |
| 107 | }; |
| 108 | for (int i = 0; i < ARRAY_SIZE(undefined_factors); ++i) { |
| 109 | assert_int_equal(SPD_UNDEFINED, |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 110 | smbios_form_factor_to_spd_mod_type(memory_type, |
| 111 | undefined_factors[i])); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 115 | static void test_smbios_form_factor_to_spd_mod_type_ddrx_parametrized( |
| 116 | smbios_memory_type memory_type, |
| 117 | const LargestIntegralType udimm_allowed[], |
| 118 | const LargestIntegralType rdimm_allowed[], |
| 119 | LargestIntegralType expected_module_type) |
| 120 | { |
| 121 | print_message("%s(%d)\n", __func__, memory_type); |
| 122 | |
| 123 | assert_in_set(smbios_form_factor_to_spd_mod_type(memory_type, MEMORY_FORMFACTOR_DIMM), |
| 124 | udimm_allowed, MAX_ALLOWED_MODULE_TYPE); |
| 125 | |
| 126 | assert_in_set(smbios_form_factor_to_spd_mod_type(memory_type, MEMORY_FORMFACTOR_RIMM), |
| 127 | rdimm_allowed, MAX_ALLOWED_MODULE_TYPE); |
| 128 | |
| 129 | assert_int_equal(expected_module_type, smbios_form_factor_to_spd_mod_type(memory_type, |
| 130 | MEMORY_FORMFACTOR_SODIMM)); |
| 131 | |
| 132 | test_smbios_form_factor_to_spd_mod_type_ddr(memory_type); |
| 133 | } |
| 134 | |
| 135 | static void test_smbios_form_factor_to_spd_mod_type_lpddrx(smbios_memory_type memory_type) |
| 136 | { |
| 137 | print_message("%s(%d)\n", __func__, memory_type); |
| 138 | /* Form factors defined in coreboot */ |
| 139 | assert_int_equal(LPX_SPD_NONDIMM, smbios_form_factor_to_spd_mod_type(memory_type, |
| 140 | MEMORY_FORMFACTOR_ROC)); |
| 141 | } |
| 142 | |
| 143 | static void test_smbios_form_factor_to_spd_mod_type(void **state) |
| 144 | { |
| 145 | const struct smbios_form_factor_test_info_ddrx { |
| 146 | smbios_memory_type memory_type; |
| 147 | const LargestIntegralType udimm_allowed[MAX_ALLOWED_MODULE_TYPE]; |
| 148 | const LargestIntegralType rdimm_allowed[MAX_ALLOWED_MODULE_TYPE]; |
| 149 | LargestIntegralType expected_module_type; |
| 150 | } ddrx_info[] = { |
| 151 | { |
| 152 | .memory_type = MEMORY_TYPE_DDR2, |
| 153 | .udimm_allowed = { DDR2_SPD_UDIMM, DDR2_SPD_MICRO_DIMM, |
| 154 | DDR2_SPD_MINI_UDIMM }, |
| 155 | .rdimm_allowed = { DDR2_SPD_RDIMM, DDR2_SPD_MINI_RDIMM }, |
| 156 | .expected_module_type = DDR2_SPD_SODIMM, |
| 157 | }, |
| 158 | { |
| 159 | .memory_type = MEMORY_TYPE_DDR3, |
| 160 | .udimm_allowed = { DDR3_SPD_UDIMM, DDR3_SPD_MICRO_DIMM, |
| 161 | DDR3_SPD_MINI_UDIMM }, |
| 162 | .rdimm_allowed = { DDR3_SPD_RDIMM, DDR3_SPD_MINI_RDIMM }, |
| 163 | .expected_module_type = DDR3_SPD_SODIMM, |
| 164 | }, |
| 165 | { |
| 166 | .memory_type = MEMORY_TYPE_DDR4, |
| 167 | .udimm_allowed = { DDR4_SPD_UDIMM, DDR4_SPD_MINI_UDIMM }, |
| 168 | .rdimm_allowed = { DDR4_SPD_RDIMM, DDR4_SPD_MINI_RDIMM }, |
| 169 | .expected_module_type = DDR4_SPD_SODIMM, |
| 170 | }, |
| 171 | { |
| 172 | .memory_type = MEMORY_TYPE_DDR5, |
| 173 | .udimm_allowed = { DDR5_SPD_UDIMM, DDR5_SPD_MINI_UDIMM }, |
| 174 | .rdimm_allowed = { DDR5_SPD_RDIMM, DDR5_SPD_MINI_RDIMM }, |
| 175 | .expected_module_type = DDR5_SPD_SODIMM |
| 176 | }, |
| 177 | }; |
| 178 | |
| 179 | /* Test for DDRx DIMM Modules */ |
| 180 | for (int i = 0; i < ARRAY_SIZE(ddrx_info); i++) |
| 181 | test_smbios_form_factor_to_spd_mod_type_ddrx_parametrized( |
| 182 | ddrx_info[i].memory_type, ddrx_info[i].udimm_allowed, |
| 183 | ddrx_info[i].rdimm_allowed, ddrx_info[i].expected_module_type); |
| 184 | |
| 185 | smbios_memory_type lpddrx_memory_type[] = { |
| 186 | MEMORY_TYPE_LPDDR3, |
| 187 | MEMORY_TYPE_LPDDR4, |
| 188 | MEMORY_TYPE_LPDDR5, |
| 189 | }; |
| 190 | |
| 191 | /* Test for Lpddrx DIMM Modules */ |
| 192 | for (int i = 0; i < ARRAY_SIZE(lpddrx_memory_type); i++) |
| 193 | test_smbios_form_factor_to_spd_mod_type_lpddrx(lpddrx_memory_type[i]); |
| 194 | } |
| 195 | |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 196 | int main(void) |
| 197 | { |
| 198 | const struct CMUnitTest tests[] = { |
| 199 | cmocka_unit_test(test_smbios_bus_width_to_spd_width), |
| 200 | cmocka_unit_test(test_smbios_memory_size_to_mib), |
| 201 | cmocka_unit_test(test_smbios_form_factor_to_spd_mod_type), |
| 202 | }; |
| 203 | |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 204 | return cb_run_group_tests(tests, NULL, NULL); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 205 | } |