coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
romstage.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <amdblocks/acpi.h>
4 #include <amdblocks/biosram.h>
5 #include <device/pci_ops.h>
6 #include <arch/cpu.h>
7 #include <arch/romstage.h>
8 #include <acpi/acpi.h>
9 #include <cpu/x86/msr.h>
10 #include <cpu/x86/mtrr.h>
11 #include <cpu/x86/smm.h>
12 #include <cpu/amd/mtrr.h>
13 #include <cbmem.h>
14 #include <commonlib/helpers.h>
15 #include <console/console.h>
16 #include <device/device.h>
17 #include <program_loading.h>
18 #include <romstage_handoff.h>
19 #include <elog.h>
20 #include <amdblocks/agesawrapper.h>
22 #include <soc/northbridge.h>
23 #include <soc/pci_devs.h>
24 #include <soc/southbridge.h>
25 #include <amdblocks/psp.h>
26 
27 #include "chip.h"
28 
30 {
31  /* By default, don't do anything */
32 }
33 
34 static void agesa_call(void)
35 {
36  post_code(0x37);
37  do_agesawrapper(AMD_INIT_RESET, "amdinitreset");
38 
39  post_code(0x38);
40  /* APs will not exit amdinitearly */
41  do_agesawrapper(AMD_INIT_EARLY, "amdinitearly");
42 }
43 
44 static void bsp_agesa_call(void)
45 {
46  set_ap_entry_ptr(agesa_call); /* indicate the path to the AP */
47  agesa_call();
48 }
49 
51 {
52  struct postcar_frame pcf;
53  uintptr_t top_of_ram;
54  msr_t base, mask;
55  msr_t mtrr_cap = rdmsr(MTRR_CAP_MSR);
56  int vmtrrs = mtrr_cap.lo & MTRR_CAP_VCNT;
57  int s3_resume = acpi_is_wakeup_s3();
58  int i;
59 
60  console_init();
61 
63  if (CONFIG(SOC_AMD_PSP_SELECTABLE_SMU_FW))
65 
67  elog_boot_notify(s3_resume);
68 
70 
71  if (!s3_resume) {
72  post_code(0x40);
73  do_agesawrapper(AMD_INIT_POST, "amdinitpost");
74 
75  post_code(0x41);
76  /*
77  * TODO: This is a hack to work around current AGESA behavior.
78  * AGESA needs to change to reflect that coreboot owns
79  * the MTRRs.
80  *
81  * After setting up DRAM, AGESA also completes the configuration
82  * of the MTRRs, setting regions to WB. Anything written to
83  * memory between now and when CAR is dismantled will be
84  * in cache and lost. For now, set the regions UC to ensure
85  * the writes get to DRAM.
86  */
87  for (i = 0 ; i < vmtrrs ; i++) {
90  if (!(mask.lo & MTRR_PHYS_MASK_VALID))
91  continue;
92 
93  if ((base.lo & 0x7) == MTRR_TYPE_WRBACK) {
94  base.lo &= ~0x7;
97  }
98  }
99  /* Disable WB from to region 4GB-TOM2. */
100  msr_t sys_cfg = rdmsr(SYSCFG_MSR);
101  sys_cfg.lo &= ~SYSCFG_MSR_TOM2WB;
102  wrmsr(SYSCFG_MSR, sys_cfg);
103  } else {
104  printk(BIOS_INFO, "S3 detected\n");
105  post_code(0x60);
106  do_agesawrapper(AMD_INIT_RESUME, "amdinitresume");
107 
108  post_code(0x61);
109  }
110 
111  post_code(0x42);
112  psp_notify_dram();
113 
114  post_code(0x43);
115  if (cbmem_recovery(s3_resume))
116  printk(BIOS_CRIT, "Failed to recover cbmem\n");
117  if (romstage_handoff_init(s3_resume))
118  printk(BIOS_ERR, "Failed to set romstage handoff data\n");
119 
120  if (CONFIG(SMM_TSEG))
122 
123  post_code(0x44);
124  if (postcar_frame_init(&pcf, 0))
125  die("Unable to initialize postcar frame.\n");
126 
127  /*
128  * We need to make sure ramstage will be run cached. At this point exact
129  * location of ramstage in cbmem is not known. Instruct postcar to cache
130  * 16 megs under cbmem top which is a safe bet to cover ramstage.
131  */
132  top_of_ram = (uintptr_t) cbmem_top();
133  postcar_frame_add_mtrr(&pcf, top_of_ram - 16*MiB, 16*MiB,
135 
136  /* Cache the memory-mapped boot media. */
138 
139  /* Cache the TSEG region */
141 
142  post_code(0x45);
143  run_postcar_phase(&pcf);
144 
145  post_code(0x50); /* Should never see this post code. */
146 }
147 
148 void SetMemParams(AMD_POST_PARAMS *PostParams)
149 {
150  const struct soc_amd_stoneyridge_config *cfg;
151  const struct device *dev = pcidev_path_on_root(GNB_DEVFN);
152 
153  if (!dev || !dev->chip_info) {
154  printk(BIOS_ERR, "Cannot find SoC devicetree config\n");
155  /* In case of a BIOS error, only attempt to set UMA. */
156  PostParams->MemConfig.UmaMode = CONFIG(GFXUMA) ?
157  UMA_AUTO : UMA_NONE;
158  return;
159  }
160 
161  cfg = dev->chip_info;
162 
163  PostParams->MemConfig.EnableMemClr = cfg->dram_clear_on_reset;
164 
165  switch (cfg->uma_mode) {
166  case UMAMODE_NONE:
167  PostParams->MemConfig.UmaMode = UMA_NONE;
168  break;
169  case UMAMODE_SPECIFIED_SIZE:
170  PostParams->MemConfig.UmaMode = UMA_SPECIFIED;
171  /* 64 KiB blocks. */
172  PostParams->MemConfig.UmaSize = cfg->uma_size / (64 * KiB);
173  break;
174  case UMAMODE_AUTO_LEGACY:
175  PostParams->MemConfig.UmaMode = UMA_AUTO;
176  PostParams->MemConfig.UmaVersion = UMA_LEGACY;
177  break;
178  case UMAMODE_AUTO_NON_LEGACY:
179  PostParams->MemConfig.UmaMode = UMA_AUTO;
180  PostParams->MemConfig.UmaVersion = UMA_NON_LEGACY;
181  break;
182  }
183 }
184 
185 void soc_customize_init_early(AMD_EARLY_PARAMS *InitEarly)
186 {
187  const struct soc_amd_stoneyridge_config *cfg;
188  const struct device *dev = pcidev_path_on_root(GNB_DEVFN);
189  struct _PLATFORM_CONFIGURATION *platform;
190 
191  if (!dev || !dev->chip_info) {
192  printk(BIOS_WARNING, "Cannot find SoC devicetree"
193  " config, STAPM unchanged\n");
194  return;
195  }
196  cfg = dev->chip_info;
197  platform = &InitEarly->PlatformConfig;
198  if ((cfg->stapm_percent) && (cfg->stapm_time_ms) &&
199  (cfg->stapm_power_mw)) {
200  platform->PlatStapmConfig.CfgStapmScalar = cfg->stapm_percent;
201  platform->PlatStapmConfig.CfgStapmTimeConstant =
202  cfg->stapm_time_ms;
203  platform->PkgPwrLimitDC = cfg->stapm_power_mw;
204  platform->PkgPwrLimitAC = cfg->stapm_power_mw;
205  platform->PlatStapmConfig.CfgStapmBoost = StapmBoostEnabled;
206  }
207 }
208 
209 static void migrate_power_state(int is_recovery)
210 {
211  struct chipset_power_state *state;
213  if (state) {
214  acpi_fill_pm_gpe_state(&state->gpe_state);
216  }
217 }
static u32 do_agesawrapper(AGESA_STRUCT_NAME func, const char *name)
#define SYSCFG_MSR
Definition: mtrr.h:12
#define SYSCFG_MSR_TOM2WB
Definition: mtrr.h:13
static int acpi_is_wakeup_s3(void)
Definition: acpi.h:9
#define asmlinkage
Definition: cpu.h:8
void run_postcar_phase(struct postcar_frame *pcf)
int postcar_frame_init(struct postcar_frame *pcf, size_t stack_size)
void postcar_frame_add_mtrr(struct postcar_frame *pcf, uintptr_t addr, size_t size, int type)
void postcar_enable_tseg_cache(struct postcar_frame *pcf)
void postcar_frame_add_romcache(struct postcar_frame *pcf, int type)
void set_ap_entry_ptr(void *entry)
Definition: biosram.c:56
#define MiB
Definition: helpers.h:76
#define KiB
Definition: helpers.h:75
int cbmem_recovery(int s3resume)
Definition: imd_cbmem.c:125
void * cbmem_top(void)
Definition: imd_cbmem.c:18
void * cbmem_add(u32 id, u64 size)
Definition: imd_cbmem.c:144
#define CBMEM_ID_POWER_STATE
Definition: cbmem_id.h:43
#define printk(level,...)
Definition: stdlib.h:16
void __noreturn die(const char *fmt,...)
Definition: die.c:17
asmlinkage void car_stage_entry(void)
Definition: romstage.c:61
DEVTREE_CONST struct device * pcidev_path_on_root(pci_devfn_t devfn)
Definition: device_const.c:255
@ CONFIG
Definition: dsi_common.h:201
static __always_inline msr_t rdmsr(unsigned int index)
Definition: msr.h:146
static __always_inline void wrmsr(unsigned int index, msr_t msr)
Definition: msr.h:157
static void elog_boot_notify(int s3_resume)
Definition: elog.h:62
void console_init(void)
Definition: init.c:49
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_CRIT
BIOS_CRIT - Recovery unlikely.
Definition: loglevel.h:56
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
void mainboard_romstage_entry(void)
Definition: romstage.c:6
#define GNB_DEVFN
Definition: pci_devs.h:13
state
Definition: raminit.c:1787
#define post_code(value)
Definition: post_code.h:12
int psp_notify_dram(void)
Definition: psp_gen1.c:197
@ BLOB_SMU_FW
Definition: psp.h:66
int psp_load_named_blob(enum psp_blob_type type, const char *name)
Definition: psp_gen1.c:153
int romstage_handoff_init(int is_s3_resume)
const struct smm_save_state_ops *legacy_ops __weak
Definition: save_state.c:8
uintptr_t base
Definition: uart.c:17
void acpi_fill_pm_gpe_state(struct acpi_pm_gpe_state *state)
Definition: acpi.c:67
void acpi_pm_gpe_add_events_print_events(void)
Definition: acpi.c:77
void soc_customize_init_early(AMD_EARLY_PARAMS *InitEarly)
Definition: romstage.c:185
static void bsp_agesa_call(void)
Definition: romstage.c:44
void SetMemParams(AMD_POST_PARAMS *PostParams)
Definition: romstage.c:148
static void migrate_power_state(int is_recovery)
Definition: romstage.c:209
static void agesa_call(void)
Definition: romstage.c:34
ROMSTAGE_CBMEM_INIT_HOOK(migrate_power_state)
static const int mask[4]
Definition: gpio.c:308
unsigned long uintptr_t
Definition: stdint.h:21
void soc_enable_psp_early(void)
Definition: psp.c:11
Definition: device.h:107
DEVTREE_CONST void * chip_info
Definition: device.h:164
unsigned int lo
Definition: msr.h:111
enum soc_amd_stoneyridge_config::@429 uma_mode
enum soc_amd_stoneyridge_config::@428 dram_clear_on_reset
void smm_list_regions(void)
Definition: tseg_region.c:70
#define MTRR_PHYS_BASE(reg)
Definition: mtrr.h:39
#define MTRR_PHYS_MASK(reg)
Definition: mtrr.h:40
#define MTRR_TYPE_WRPROT
Definition: mtrr.h:13
#define MTRR_CAP_VCNT
Definition: mtrr.h:23
#define MTRR_CAP_MSR
Definition: mtrr.h:17
#define MTRR_TYPE_UNCACHEABLE
Definition: mtrr.h:10
#define MTRR_TYPE_WRBACK
Definition: mtrr.h:14
#define MTRR_PHYS_MASK_VALID
Definition: mtrr.h:41