/* SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "raminit.h" #include "pei_data.h" #include "haswell.h" #include #include "bdw_pei_data.h" #define MRC_CACHE_VERSION 1 void save_mrc_data(struct pei_data *pei_data) { /* Save the MRC S3 restore data to cbmem */ mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, pei_data->mrc_output, pei_data->mrc_output_len); } static const char *const ecc_decoder[] = { "inactive", "active on IO", "disabled on IO", "active", }; /* Print out the memory controller configuration, as per the values in its registers. */ static void report_memory_config(void) { int i; const u32 addr_decoder_common = MCHBAR32(MAD_CHNL); printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n", (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100); printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n", (addr_decoder_common >> 0) & 3, (addr_decoder_common >> 2) & 3, (addr_decoder_common >> 4) & 3); for (i = 0; i < NUM_CHANNELS; i++) { const u32 ch_conf = MCHBAR32(MAD_DIMM(i)); printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i, ch_conf); printk(BIOS_DEBUG, " ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]); printk(BIOS_DEBUG, " enhanced interleave mode %s\n", ((ch_conf >> 22) & 1) ? "on" : "off"); printk(BIOS_DEBUG, " rank interleave %s\n", ((ch_conf >> 21) & 1) ? "on" : "off"); printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n", ((ch_conf >> 0) & 0xff) * 256, ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32", ((ch_conf >> 17) & 1) ? "dual" : "single", ((ch_conf >> 16) & 1) ? "" : ", selected"); printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n", ((ch_conf >> 8) & 0xff) * 256, ((ch_conf >> 20) & 1) ? "x16" : "x8 or x32", ((ch_conf >> 18) & 1) ? "dual" : "single", ((ch_conf >> 16) & 1) ? ", selected" : ""); } } static void BDW_ABI_X86 send_to_console(unsigned char b) { console_tx_byte(b); } static void broadwell_fill_pei_data(struct bdw_pei_data *pei_data) { pei_data->pei_version = BDW_PEI_VERSION; pei_data->board_type = BDW_ULT; pei_data->usbdebug = CONFIG(USBDEBUG); pei_data->pciexbar = MCFG_BASE_ADDRESS; pei_data->smbusbar = SMBUS_BASE_ADDRESS; pei_data->ehcibar = EARLY_EHCI_BAR; pei_data->xhcibar = EARLY_XHCI_BAR; pei_data->gttbar = EARLY_GTT_BAR; pei_data->pmbase = ACPI_BASE_ADDRESS; pei_data->gpiobase = GPIO_BASE_ADDRESS; pei_data->tseg_size = CONFIG_SMM_TSEG_SIZE; pei_data->temp_mmio_base = EARLY_TEMP_MMIO; pei_data->tx_byte = &send_to_console; pei_data->ddr_refresh_2x = 1; } /** * Find PEI executable in coreboot filesystem and execute it. * * @param pei_data: configuration data for UEFI PEI reference code */ void sdram_initialize(struct pei_data *pei_data) { int BDW_ABI_X86 mrc_entry(struct bdw_pei_data *); int BDW_ABI_X86 refcode_entry(struct bdw_pei_data *); struct bdw_pei_data bdw_pei; memset(&bdw_pei, 0, sizeof(bdw_pei)); broadwell_fill_pei_data(&bdw_pei); bdw_pei.dimm_channel0_disabled = pei_data->dimm_channel0_disabled; bdw_pei.dimm_channel1_disabled = pei_data->dimm_channel1_disabled; bdw_pei.dq_pins_interleaved = pei_data->dq_pins_interleaved; memcpy(bdw_pei.spd_addresses, pei_data->spd_addresses, sizeof(bdw_pei.spd_addresses)); bdw_pei.disable_saved_data = 1; bdw_pei.saved_data = NULL; bdw_pei.saved_data_size = 0; for (int i = 0; i < BDW_MAX_USB2_PORTS; i++) { bdw_pei.usb2_ports[i].length = pei_data->usb2_ports[i].length; bdw_pei.usb2_ports[i].enable = pei_data->usb2_ports[i].enable; bdw_pei.usb2_ports[i].oc_pin = pei_data->usb2_ports[i].over_current_pin; bdw_pei.usb2_ports[i].location = pei_data->usb2_ports[i].location; } for (int i = 0; i < BDW_MAX_USB3_PORTS; i++) { bdw_pei.usb3_ports[i].enable = pei_data->usb3_ports[i].enable; bdw_pei.usb3_ports[i].oc_pin = pei_data->usb3_ports[i].over_current_pin; } printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n"); if (true) { int rv = mrc_entry(&bdw_pei); if (rv) { switch (rv) { case -1: printk(BIOS_ERR, "PEI version mismatch.\n"); break; case -2: printk(BIOS_ERR, "Invalid memory frequency.\n"); break; default: printk(BIOS_ERR, "MRC returned %x.\n", rv); } die_with_post_code(POST_INVALID_VENDOR_BINARY, "Nonzero MRC return value.\n"); } rv = refcode_entry(&bdw_pei); /* The mrc.bin reconfigures USB, so usbdebug needs to be reinitialized */ if (CONFIG(USBDEBUG_IN_PRE_RAM)) usbdebug_hw_init(true); if (rv) { printk(BIOS_ERR, "refcode returns %d.\n", rv); } } else { die("UEFI PEI System Agent not found.\n"); } /* Print the MRC version after executing the UEFI PEI stage */ u32 version = MCHBAR32(MRC_REVISION); printk(BIOS_DEBUG, "MRC Version %d.%d.%d Build %d\n", (version >> 24) & 0xff, (version >> 16) & 0xff, (version >> 8) & 0xff, (version >> 0) & 0xff); report_memory_config(); } static bool nb_supports_ecc(const uint32_t capid0_a) { return !(capid0_a & CAPID_ECCDIS); } static uint16_t nb_slots_per_channel(const uint32_t capid0_a) { return !(capid0_a & CAPID_DDPCD) + 1; } static uint16_t nb_number_of_channels(const uint32_t capid0_a) { return !(capid0_a & CAPID_PDCD) + 1; } static uint32_t nb_max_chan_capacity_mib(const uint32_t capid0_a) { uint32_t ddrsz; /* Values from documentation, which assume two DIMMs per channel */ switch (CAPID_DDRSZ(capid0_a)) { case 1: ddrsz = 8192; break; case 2: ddrsz = 2048; break; case 3: ddrsz = 512; break; default: ddrsz = 16384; break; } /* Account for the maximum number of DIMMs per channel */ return (ddrsz / 2) * nb_slots_per_channel(capid0_a); } void setup_sdram_meminfo(struct pei_data *pei_data) { struct memory_info *mem_info; struct dimm_info *dimm; int ch, d_num; int dimm_cnt = 0; mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info)); if (!mem_info) die("Failed to add memory info to CBMEM.\n"); memset(mem_info, 0, sizeof(struct memory_info)); const u32 ddr_frequency = (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100; for (ch = 0; ch < NUM_CHANNELS; ch++) { const u32 ch_conf = MCHBAR32(MAD_DIMM(ch)); /* DIMMs A/B */ for (d_num = 0; d_num < NUM_SLOTS; d_num++) { const u32 dimm_size = ((ch_conf >> (d_num * 8)) & 0xff) * 256; if (dimm_size) { dimm = &mem_info->dimm[dimm_cnt]; dimm->dimm_size = dimm_size; dimm->ddr_type = MEMORY_TYPE_DDR3; dimm->ddr_frequency = ddr_frequency; dimm->rank_per_dimm = 1 + ((ch_conf >> (17 + d_num)) & 1); dimm->channel_num = ch; dimm->dimm_num = d_num; dimm->bank_locator = ch * 2; memcpy(dimm->serial, &pei_data->spd_data[dimm_cnt][SPD_DIMM_SERIAL_NUM], SPD_DIMM_SERIAL_LEN); memcpy(dimm->module_part_number, &pei_data->spd_data[dimm_cnt][SPD_DIMM_PART_NUM], SPD_DIMM_PART_LEN); dimm->mod_id = (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID2] << 8) | (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID1] & 0xff); dimm->mod_type = SPD_SODIMM; dimm->bus_width = MEMORY_BUS_WIDTH_64; dimm_cnt++; } } } mem_info->dimm_cnt = dimm_cnt; const uint32_t capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A); const uint16_t channels = nb_number_of_channels(capid0_a); mem_info->ecc_capable = nb_supports_ecc(capid0_a); mem_info->max_capacity_mib = channels * nb_max_chan_capacity_mib(capid0_a); mem_info->number_of_devices = channels * nb_slots_per_channel(capid0_a); }