coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
systemagent.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <cbmem.h>
4 #include <console/console.h>
5 #include <device/mmio.h>
6 #include <device/pci_ops.h>
7 #include <stdint.h>
8 #include <delay.h>
9 #include <device/device.h>
10 #include <device/pci.h>
11 #include <device/pci_ids.h>
12 #include <timer.h>
13 
14 #include <soc/iomap.h>
15 #include <soc/pci_devs.h>
16 #include <soc/ramstage.h>
17 #include <soc/systemagent.h>
18 #include <soc/acpi.h>
19 
20 #define _1ms 1
21 #define WAITING_STEP 100
22 
23 static int get_pcie_bar(struct device *dev, unsigned int index, u32 *base,
24  u32 *len)
25 {
26  u32 pciexbar_reg;
27 
28  *base = 0;
29  *len = 0;
30 
31  pciexbar_reg = pci_read_config32(dev, index);
32 
33  if (!(pciexbar_reg & (1 << 0)))
34  return 0;
35 
36  switch ((pciexbar_reg >> 1) & 3) {
37  case 0: /* 256MB */
38  *base = pciexbar_reg &
39  ((1 << 31) | (1 << 30) | (1 << 29) | (1 << 28));
40  *len = 256 * 1024 * 1024;
41  return 1;
42  case 1: /* 128M */
43  *base = pciexbar_reg & ((1 << 31) | (1 << 30) | (1 << 29) |
44  (1 << 28) | (1 << 27));
45  *len = 128 * 1024 * 1024;
46  return 1;
47  case 2: /* 64M */
48  *base = pciexbar_reg & ((1 << 31) | (1 << 30) | (1 << 29) |
49  (1 << 28) | (1 << 27) | (1 << 26));
50  *len = 64 * 1024 * 1024;
51  return 1;
52  }
53 
54  return 0;
55 }
56 
57 static int get_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
58 {
59  u32 bar;
60 
61  bar = pci_read_config32(dev, index);
62 
63  /* If not enabled don't report it. */
64  if (!(bar & 0x1))
65  return 0;
66 
67  /* Knock down the enable bit. */
68  *base = bar & ~1;
69 
70  return 1;
71 }
72 
73 struct fixed_mmio_descriptor {
74  unsigned int index;
75  u32 size;
76  int (*get_resource)(struct device *dev, unsigned int index, u32 *base,
77  u32 *size);
78  const char *description;
79 };
80 
82  {PCIEXBAR, 0, get_pcie_bar, "PCIEXBAR"},
83  {MCHBAR, MCH_BASE_SIZE, get_bar, "MCHBAR"},
84 };
85 
86 /*
87  * Add all known fixed MMIO ranges that hang off the host bridge/memory
88  * controller device.
89  */
90 static void mc_add_fixed_mmio_resources(struct device *dev)
91 {
92  int i;
93 
94  for (i = 0; i < ARRAY_SIZE(mc_fixed_resources); i++) {
95  u32 base;
96  u32 size;
97  struct resource *resource;
98  unsigned int index;
99 
102  if (!mc_fixed_resources[i].get_resource(dev, index, &base,
103  &size))
104  continue;
105 
110  resource->base = base;
111  resource->size = size;
112  printk(BIOS_DEBUG, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
113  __func__, mc_fixed_resources[i].description, index,
114  (unsigned long)base, (unsigned long)(base + size - 1));
115  }
116 }
117 
118 struct map_entry {
119  int reg;
120  int is_64_bit;
121  int is_limit;
122  const char *description;
123 };
124 
125 static void read_map_entry(struct device *dev, struct map_entry *entry,
126  uint64_t *result)
127 {
128  uint64_t value;
129  uint64_t mask;
130 
131  /* All registers are on a 1MiB granularity. */
132  mask = ((1ULL << 20) - 1);
133  mask = ~mask;
134 
135  value = 0;
136 
137  if (entry->is_64_bit) {
138  value = pci_read_config32(dev, entry->reg + 4);
139  value <<= 32;
140  }
141 
142  value |= (uint64_t)pci_read_config32(dev, entry->reg);
143  value &= mask;
144 
145  if (entry->is_limit)
146  value |= ~mask;
147 
148  *result = value;
149 }
150 
151 #define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
152  { \
153  .reg = reg_, .is_64_bit = is_64_, .is_limit = is_limit_, \
154  .description = desc_, \
155  }
156 
157 #define MAP_ENTRY_BASE_64(reg_, desc_) MAP_ENTRY(reg_, 1, 0, desc_)
158 #define MAP_ENTRY_LIMIT_64(reg_, desc_) MAP_ENTRY(reg_, 1, 1, desc_)
159 #define MAP_ENTRY_BASE_32(reg_, desc_) MAP_ENTRY(reg_, 0, 0, desc_)
160 
161 enum {
165  /* Must be last. */
167 };
168 
169 static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
170  [TOUUD_REG] = MAP_ENTRY_BASE_64(TOUUD, "TOUUD"),
171  [TOLUD_REG] = MAP_ENTRY_BASE_32(TOLUD, "TOLUD"),
172  [TSEG_REG] = MAP_ENTRY_BASE_32(TSEGMB, "TSEGMB"),
173 };
174 
175 static void mc_read_map_entries(struct device *dev, uint64_t *values)
176 {
177  int i;
178  for (i = 0; i < NUM_MAP_ENTRIES; i++)
179  read_map_entry(dev, &memory_map[i], &values[i]);
180 }
181 
182 static void mc_report_map_entries(struct device *dev, uint64_t *values)
183 {
184  int i;
185  for (i = 0; i < NUM_MAP_ENTRIES; i++) {
186  printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
187  memory_map[i].description, values[i]);
188  }
189 }
190 
191 static void mc_add_dram_resources(struct device *dev)
192 {
193  unsigned long base_k, size_k;
194  unsigned long touud_k;
195  unsigned long index;
196  struct resource *resource;
197  uint64_t mc_values[NUM_MAP_ENTRIES];
198  uintptr_t top_of_ram;
199 
200  /* Read in the MAP registers and report their values. */
201  mc_read_map_entries(dev, &mc_values[0]);
202  mc_report_map_entries(dev, &mc_values[0]);
203 
204  /*
205  * These are the host memory ranges that should be added:
206  * - 0 -> 0xa0000: cacheable
207  * - 0xc0000 -> 0x100000 : reserved
208  * - 0x100000 -> top_of_ram : cacheable
209  * - top_of_ram -> TSEG: uncacheable
210  * - TESG -> TOLUD: cacheable with standard MTRRs and reserved
211  * - 4GiB -> TOUUD: cacheable
212  *
213  * The default SMRAM space is reserved so that the range doesn't
214  * have to be saved during S3 Resume. Once marked reserved the OS
215  * cannot use the memory. This is a bit of an odd place to reserve
216  * the region, but the CPU devices don't have dev_ops->read_resources()
217  * called on them.
218  *
219  * The range 0xa0000 -> 0xc0000 does not have any resources
220  * associated with it to handle legacy VGA memory. If this range
221  * is not omitted the mtrr code will setup the area as cacheable
222  * causing VGA access to not work.
223  *
224  * The TSEG region is mapped as cacheable so that one can perform
225  * SMRAM relocation faster. Once the SMRR is enabled the SMRR takes
226  * precedence over the existing MTRRs covering this region.
227  *
228  * It should be noted that cacheable entry types need to be added in
229  * order. The reason is that the current MTRR code assumes this and
230  * falls over itself if it isn't.
231  *
232  * The resource index starts low and should not meet or exceed
233  * PCI_BASE_ADDRESS_0.
234  */
235  index = 0;
236  top_of_ram = (uintptr_t)cbmem_top();
237 
238  /* 0 - > 0xa0000 */
239  base_k = 0;
240  size_k = (0xa0000 >> 10) - base_k;
241  ram_resource(dev, index++, base_k, size_k);
242 
243  /* 0x100000 -> top_of_ram */
244  base_k = 0x100000 >> 10;
245  size_k = (top_of_ram >> 10) - base_k;
246  ram_resource(dev, index++, base_k, size_k);
247 
248  /* top_of_ram -> TSEG */
249  resource = new_resource(dev, index++);
250  resource->base = top_of_ram;
251  resource->size = mc_values[TSEG_REG] - resource->base;
255 
256  /* TSEG -> TOLUD */
257  resource = new_resource(dev, index++);
258  resource->base = mc_values[TSEG_REG];
259  resource->size = mc_values[TOLUD_REG] - resource->base;
264  "SMM memory location: 0x%llx SMM memory size: 0x%llx\n",
266 
267  /* 4GiB -> TOUUD */
268  base_k = 4096 * 1024; /* 4GiB */
269  touud_k = mc_values[TOUUD_REG] >> 10;
270  size_k = touud_k - base_k;
271  if (touud_k > base_k)
272  ram_resource(dev, index++, base_k, size_k);
273 
274  /*
275  * Reserve everything between A segment and 1MB:
276  *
277  * 0xa0000 - 0xbffff: legacy VGA
278  * 0xc0000 - 0xfffff: reserved RAM
279  */
280  mmio_resource(dev, index++, (0xa0000 >> 10), (0xc0000 - 0xa0000) >> 10);
281  reserved_ram_resource(dev, index++, (0xc0000 >> 10),
282  (0x100000 - 0xc0000) >> 10);
283 }
284 
285 static void systemagent_read_resources(struct device *dev)
286 {
287  /* Read standard PCI resources. */
289 
290  /* Add all fixed MMIO resources. */
292 
293  /* Calculate and add DRAM resources. */
295 }
296 
297 static void systemagent_init(struct device *dev)
298 {
299  struct stopwatch sw;
300  void *bios_reset_cpl =
302  uint32_t reg = read32(bios_reset_cpl);
303 
304  /* Stage0 BIOS Reset Complete (RST_CPL) */
305  reg |= RST_CPL_BIT;
306  write32(bios_reset_cpl, reg);
307 
308  /*
309  * Poll for bit 8 in same reg (RST_CPL).
310  * We wait here till 1 ms for the bit to get set.
311  */
313  while (!(read32(bios_reset_cpl) & PCODE_INIT_DONE)) {
314  if (stopwatch_expired(&sw)) {
315  printk(BIOS_DEBUG, "Failed to set RST_CPL bit\n");
316  return;
317  }
319  }
320  printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
321 }
322 
323 static struct device_operations systemagent_ops = {
325  .set_resources = pci_dev_set_resources,
326  .enable_resources = pci_dev_enable_resources,
327  .init = systemagent_init,
328  .ops_pci = &soc_pci_ops,
329 #if CONFIG(HAVE_ACPI_TABLES)
330  .write_acpi_tables = systemagent_write_acpi_tables,
331 #endif
332 };
333 
334 /* IDs for System Agent device of Intel Denverton SoC */
335 static const unsigned short systemagent_ids[] = {
338  0
339 };
340 
341 static const struct pci_driver systemagent_driver __pci_driver = {
342  .ops = &systemagent_ops,
343  .vendor = PCI_VID_INTEL,
344  .devices = systemagent_ids
345 };
#define PCODE_INIT_DONE
Definition: systemagent.h:13
pte_t value
Definition: mmu.c:91
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
#define ARRAY_SIZE(a)
Definition: helpers.h:12
void * cbmem_top(void)
Definition: imd_cbmem.c:18
#define printk(level,...)
Definition: stdlib.h:16
#define MCH_BAR_BIOS_RESET_CPL
Definition: systemagent.h:67
#define RST_CPL_BIT
Definition: systemagent.h:68
static struct map_entry memory_map[NUM_MAP_ENTRIES]
Definition: systemagent.c:169
static void systemagent_init(struct device *dev)
Definition: systemagent.c:297
static struct device_operations systemagent_ops
Definition: systemagent.c:323
@ TSEG_REG
Definition: systemagent.c:164
@ TOUUD_REG
Definition: systemagent.c:162
@ NUM_MAP_ENTRIES
Definition: systemagent.c:166
@ TOLUD_REG
Definition: systemagent.c:163
static const struct pci_driver systemagent_driver __pci_driver
Definition: systemagent.c:341
#define WAITING_STEP
Definition: systemagent.c:21
static void mc_report_map_entries(struct device *dev, uint64_t *values)
Definition: systemagent.c:182
static void mc_add_fixed_mmio_resources(struct device *dev)
Definition: systemagent.c:90
#define MAP_ENTRY_BASE_64(reg_, desc_)
Definition: systemagent.c:157
static void mc_read_map_entries(struct device *dev, uint64_t *values)
Definition: systemagent.c:175
#define MAP_ENTRY_BASE_32(reg_, desc_)
Definition: systemagent.c:159
static void mc_add_dram_resources(struct device *dev)
Definition: systemagent.c:191
static void read_map_entry(struct device *dev, struct map_entry *entry, uint64_t *result)
Definition: systemagent.c:125
static int get_pcie_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Definition: systemagent.c:23
static void systemagent_read_resources(struct device *dev)
Definition: systemagent.c:285
struct fixed_mmio_descriptor mc_fixed_resources[]
Definition: systemagent.c:81
static int get_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Definition: systemagent.c:57
#define _1ms
Definition: systemagent.c:20
static const unsigned short systemagent_ids[]
Definition: systemagent.c:335
struct resource * new_resource(struct device *dev, unsigned int index)
See if a resource structure already exists for a given index and if not allocate one.
Definition: device_util.c:346
#define MCHBAR
Definition: host_bridge.h:7
#define TOLUD
Definition: host_bridge.h:61
#define PCIEXBAR
Definition: host_bridge.h:32
#define TOUUD
Definition: host_bridge.h:57
#define ram_resource(dev, idx, basek, sizek)
Definition: device.h:321
#define mmio_resource(dev, idx, basek, sizek)
Definition: device.h:334
#define reserved_ram_resource(dev, idx, basek, sizek)
Definition: device.h:324
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
static int stopwatch_expired(struct stopwatch *sw)
Definition: timer.h:152
static void stopwatch_init_msecs_expire(struct stopwatch *sw, long ms)
Definition: timer.h:133
#define DEFAULT_MCHBAR
Definition: iomap.h:11
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
result
Definition: mrc_cache.c:35
#define MCH_BASE_SIZE
Definition: memmap.h:6
void pci_dev_enable_resources(struct device *dev)
Definition: pci_device.c:721
void pci_dev_read_resources(struct device *dev)
Definition: pci_device.c:534
void pci_dev_set_resources(struct device *dev)
Definition: pci_device.c:691
#define PCI_DID_INTEL_DNVAD_SA
Definition: pci_ids.h:2762
#define PCI_DID_INTEL_DNV_SA
Definition: pci_ids.h:2761
#define PCI_VID_INTEL
Definition: pci_ids.h:2157
#define IORESOURCE_RESERVE
Definition: resource.h:30
#define IORESOURCE_MEM
Definition: resource.h:10
#define IORESOURCE_CACHEABLE
Definition: resource.h:19
#define IORESOURCE_STORED
Definition: resource.h:32
#define IORESOURCE_ASSIGNED
Definition: resource.h:34
#define IORESOURCE_FIXED
Definition: resource.h:36
#define TSEGMB
Definition: host_bridge.h:48
uintptr_t base
Definition: uart.c:17
struct pci_operations soc_pci_ops
Definition: chip.c:51
static const int mask[4]
Definition: gpio.c:308
unsigned long systemagent_write_acpi_tables(const struct device *dev, unsigned long current, struct acpi_rsdp *const rsdp)
Definition: acpi.c:210
unsigned int uint32_t
Definition: stdint.h:14
uint32_t u32
Definition: stdint.h:51
unsigned long uintptr_t
Definition: stdint.h:21
unsigned long long uint64_t
Definition: stdint.h:17
void(* read_resources)(struct device *dev)
Definition: device.h:39
Definition: device.h:107
const char * description
Definition: northbridge.c:81
int(* get_resource)(struct device *dev, unsigned int index, u32 *base, u32 *size)
Definition: northbridge.c:80
unsigned int index
Definition: northbridge.c:78
Definition: northbridge.c:147
int is_64_bit
Definition: northbridge.c:149
const char * description
Definition: northbridge.c:151
int reg
Definition: northbridge.c:148
int is_limit
Definition: northbridge.c:150
unsigned long flags
Definition: resource.h:49
resource_t base
Definition: resource.h:45
unsigned long index
Definition: resource.h:50
resource_t size
Definition: resource.h:46
void udelay(uint32_t us)
Definition: udelay.c:15