Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Elyes Haouas | 5a04746 | 2022-12-28 11:43:49 +0100 | [diff] [blame] | 3 | #include <device/dram/ddr2.h> |
Elyes Haouas | f82e68c | 2022-12-28 12:33:58 +0100 | [diff] [blame] | 4 | #include <device/dram/ddr3.h> |
Elyes Haouas | 305ee06 | 2024-04-30 22:38:25 +0200 | [diff] [blame] | 5 | #include <device/dram/ddr5.h> |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 6 | #include <dimm_info_util.h> |
| 7 | #include <spd.h> |
| 8 | #include <tests/test.h> |
| 9 | |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 10 | #define MAX_ALLOWED_MODULE_TYPE 3 |
| 11 | |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 12 | 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] | 13 | { |
| 14 | /* Non-ECC variants */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 15 | assert_int_equal(MEMORY_BUS_WIDTH_64, smbios_bus_width_to_spd_width(ddr_type, 64, 64)); |
| 16 | assert_int_equal(MEMORY_BUS_WIDTH_32, smbios_bus_width_to_spd_width(ddr_type, 32, 32)); |
| 17 | assert_int_equal(MEMORY_BUS_WIDTH_16, smbios_bus_width_to_spd_width(ddr_type, 16, 16)); |
| 18 | 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] | 19 | /* Incorrect data width. Fallback to 8-bit */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 20 | 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] | 21 | |
| 22 | /* ECC variants */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 23 | uint8_t extension_8bits = SPD_ECC_8BIT; |
| 24 | if (ddr_type == MEMORY_TYPE_DDR5 || ddr_type == MEMORY_TYPE_LPDDR5) |
| 25 | extension_8bits = SPD_ECC_8BIT_LP5_DDR5; |
| 26 | |
| 27 | assert_int_equal(MEMORY_BUS_WIDTH_64 | extension_8bits, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 28 | smbios_bus_width_to_spd_width(ddr_type, 64 + 8, 64)); |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 29 | assert_int_equal(MEMORY_BUS_WIDTH_32 | extension_8bits, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 30 | smbios_bus_width_to_spd_width(ddr_type, 32 + 8, 32)); |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 31 | assert_int_equal(MEMORY_BUS_WIDTH_16 | extension_8bits, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 32 | smbios_bus_width_to_spd_width(ddr_type, 16 + 8, 16)); |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 33 | assert_int_equal(MEMORY_BUS_WIDTH_8 | extension_8bits, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 34 | smbios_bus_width_to_spd_width(ddr_type, 8 + 8, 8)); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 35 | /* Incorrect data width. Fallback to 8-bit */ |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 36 | assert_int_equal(MEMORY_BUS_WIDTH_8 | extension_8bits, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 37 | smbios_bus_width_to_spd_width(ddr_type, 15 + 8, 15)); |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static void test_smbios_bus_width_to_spd_width(void **state) |
| 41 | { |
| 42 | smbios_memory_type memory_type[] = { |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 43 | MEMORY_TYPE_DDR2, MEMORY_TYPE_DDR3, MEMORY_TYPE_DDR4, MEMORY_TYPE_DDR5, |
| 44 | MEMORY_TYPE_LPDDR3, MEMORY_TYPE_LPDDR4, MEMORY_TYPE_LPDDR5, |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 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", |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 49 | memory_type[i]); |
Subrata Banik | 3306f37 | 2021-10-26 13:19:20 +0530 | [diff] [blame] | 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 | |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 89 | 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] | 90 | { |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 91 | const smbios_memory_form_factor undefined_factors[] = { |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 92 | MEMORY_FORMFACTOR_OTHER, MEMORY_FORMFACTOR_UNKNOWN, |
| 93 | MEMORY_FORMFACTOR_SIMM, MEMORY_FORMFACTOR_SIP, |
| 94 | MEMORY_FORMFACTOR_CHIP, MEMORY_FORMFACTOR_DIP, |
| 95 | MEMORY_FORMFACTOR_ZIP, MEMORY_FORMFACTOR_PROPRIETARY_CARD, |
| 96 | MEMORY_FORMFACTOR_TSOP, MEMORY_FORMFACTOR_ROC, |
| 97 | MEMORY_FORMFACTOR_SRIMM, MEMORY_FORMFACTOR_FBDIMM, |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 98 | MEMORY_FORMFACTOR_DIE, |
| 99 | }; |
| 100 | for (int i = 0; i < ARRAY_SIZE(undefined_factors); ++i) { |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 101 | assert_int_equal(SPD_UNDEFINED, smbios_form_factor_to_spd_mod_type( |
| 102 | memory_type, undefined_factors[i])); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 106 | static void test_smbios_form_factor_to_spd_mod_type_ddrx_parametrized( |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 107 | smbios_memory_type memory_type, const LargestIntegralType udimm_allowed[], |
| 108 | const LargestIntegralType rdimm_allowed[], LargestIntegralType expected_module_type) |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 109 | { |
| 110 | print_message("%s(%d)\n", __func__, memory_type); |
| 111 | |
| 112 | assert_in_set(smbios_form_factor_to_spd_mod_type(memory_type, MEMORY_FORMFACTOR_DIMM), |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 113 | udimm_allowed, MAX_ALLOWED_MODULE_TYPE); |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 114 | |
| 115 | assert_in_set(smbios_form_factor_to_spd_mod_type(memory_type, MEMORY_FORMFACTOR_RIMM), |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 116 | rdimm_allowed, MAX_ALLOWED_MODULE_TYPE); |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 117 | |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 118 | assert_int_equal(expected_module_type, smbios_form_factor_to_spd_mod_type( |
| 119 | memory_type, MEMORY_FORMFACTOR_SODIMM)); |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 120 | |
| 121 | test_smbios_form_factor_to_spd_mod_type_ddr(memory_type); |
| 122 | } |
| 123 | |
| 124 | static void test_smbios_form_factor_to_spd_mod_type_lpddrx(smbios_memory_type memory_type) |
| 125 | { |
| 126 | print_message("%s(%d)\n", __func__, memory_type); |
| 127 | /* Form factors defined in coreboot */ |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 128 | assert_int_equal(LPX_SPD_NONDIMM, smbios_form_factor_to_spd_mod_type( |
| 129 | memory_type, MEMORY_FORMFACTOR_ROC)); |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void test_smbios_form_factor_to_spd_mod_type(void **state) |
| 133 | { |
| 134 | const struct smbios_form_factor_test_info_ddrx { |
| 135 | smbios_memory_type memory_type; |
| 136 | const LargestIntegralType udimm_allowed[MAX_ALLOWED_MODULE_TYPE]; |
| 137 | const LargestIntegralType rdimm_allowed[MAX_ALLOWED_MODULE_TYPE]; |
| 138 | LargestIntegralType expected_module_type; |
| 139 | } ddrx_info[] = { |
| 140 | { |
| 141 | .memory_type = MEMORY_TYPE_DDR2, |
Elyes Haouas | 5a04746 | 2022-12-28 11:43:49 +0100 | [diff] [blame] | 142 | .udimm_allowed = {SPD_DDR2_DIMM_TYPE_UDIMM, |
| 143 | SPD_DDR2_DIMM_TYPE_MICRO_DIMM, |
| 144 | SPD_DDR2_DIMM_TYPE_MINI_UDIMM}, |
| 145 | .rdimm_allowed = {SPD_DDR2_DIMM_TYPE_RDIMM, |
| 146 | SPD_DDR2_DIMM_TYPE_MINI_RDIMM}, |
| 147 | .expected_module_type = SPD_DDR2_DIMM_TYPE_SO_DIMM, |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 148 | }, |
| 149 | { |
| 150 | .memory_type = MEMORY_TYPE_DDR3, |
Elyes Haouas | f82e68c | 2022-12-28 12:33:58 +0100 | [diff] [blame] | 151 | .udimm_allowed = {SPD_DDR3_DIMM_TYPE_UDIMM, |
| 152 | SPD_DDR3_DIMM_TYPE_MICRO_DIMM, |
| 153 | SPD_DDR3_DIMM_TYPE_MINI_UDIMM}, |
| 154 | .rdimm_allowed = {SPD_DDR3_DIMM_TYPE_RDIMM, |
| 155 | SPD_DDR3_DIMM_TYPE_MINI_RDIMM}, |
| 156 | .expected_module_type = SPD_DDR3_DIMM_TYPE_SO_DIMM, |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 157 | }, |
| 158 | { |
| 159 | .memory_type = MEMORY_TYPE_DDR4, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 160 | .udimm_allowed = {DDR4_SPD_UDIMM, DDR4_SPD_MINI_UDIMM}, |
| 161 | .rdimm_allowed = {DDR4_SPD_RDIMM, DDR4_SPD_MINI_RDIMM}, |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 162 | .expected_module_type = DDR4_SPD_SODIMM, |
| 163 | }, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 164 | {.memory_type = MEMORY_TYPE_DDR5, |
| 165 | .udimm_allowed = {DDR5_SPD_UDIMM, DDR5_SPD_MINI_UDIMM}, |
| 166 | .rdimm_allowed = {DDR5_SPD_RDIMM, DDR5_SPD_MINI_RDIMM}, |
| 167 | .expected_module_type = DDR5_SPD_SODIMM}, |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | /* Test for DDRx DIMM Modules */ |
| 171 | for (int i = 0; i < ARRAY_SIZE(ddrx_info); i++) |
| 172 | test_smbios_form_factor_to_spd_mod_type_ddrx_parametrized( |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 173 | ddrx_info[i].memory_type, ddrx_info[i].udimm_allowed, |
| 174 | ddrx_info[i].rdimm_allowed, ddrx_info[i].expected_module_type); |
Subrata Banik | 6de8b42 | 2021-10-26 20:46:21 +0530 | [diff] [blame] | 175 | |
| 176 | smbios_memory_type lpddrx_memory_type[] = { |
| 177 | MEMORY_TYPE_LPDDR3, |
| 178 | MEMORY_TYPE_LPDDR4, |
| 179 | MEMORY_TYPE_LPDDR5, |
| 180 | }; |
| 181 | |
| 182 | /* Test for Lpddrx DIMM Modules */ |
| 183 | for (int i = 0; i < ARRAY_SIZE(lpddrx_memory_type); i++) |
| 184 | test_smbios_form_factor_to_spd_mod_type_lpddrx(lpddrx_memory_type[i]); |
| 185 | } |
| 186 | |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 187 | int main(void) |
| 188 | { |
| 189 | const struct CMUnitTest tests[] = { |
| 190 | cmocka_unit_test(test_smbios_bus_width_to_spd_width), |
| 191 | cmocka_unit_test(test_smbios_memory_size_to_mib), |
| 192 | cmocka_unit_test(test_smbios_form_factor_to_spd_mod_type), |
| 193 | }; |
| 194 | |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 195 | return cb_run_group_tests(tests, NULL, NULL); |
Jakub Czapiga | dadcae6 | 2021-04-08 16:29:03 +0200 | [diff] [blame] | 196 | } |