coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
northbridge.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/pci_def.h>
6 #include <device/pci_ops.h>
7 #include <stdint.h>
8 #include <device/device.h>
9 #include <boot/tables.h>
10 #include <acpi/acpi.h>
14 #include <cpu/intel/smm_reloc.h>
15 
16 static const int legacy_hole_base_k = 0xa0000 / 1024;
17 
18 static void mch_domain_read_resources(struct device *dev)
19 {
20  u8 index;
21  u64 tom, touud;
22  u32 tomk, tolud, delta_cbmem;
23  u32 uma_sizek = 0;
24 
25  const u32 top32memk = 4 * (GiB / KiB);
26  index = 3;
27 
29 
30  struct device *mch = pcidev_on_root(0, 0);
31 
32  /* Top of Upper Usable DRAM, including remap */
33  touud = pci_read_config16(mch, D0F0_TOUUD);
34  touud <<= 20;
35 
36  /* Top of Lower Usable DRAM */
37  tolud = pci_read_config16(mch, D0F0_TOLUD) & 0xfff0;
38  tolud <<= 16;
39 
40  /* Top of Memory - does not account for any UMA */
41  tom = pci_read_config16(mch, D0F0_TOM) & 0x01ff;
42  tom <<= 26;
43 
44  printk(BIOS_DEBUG, "TOUUD 0x%llx TOLUD 0x%08x TOM 0x%llx\n", touud, tolud, tom);
45 
46  tomk = tolud >> 10;
47 
48  /* Graphics memory comes next */
49 
50  const u16 ggc = pci_read_config16(mch, D0F0_GGC);
51  printk(BIOS_DEBUG, "IGD decoded, subtracting ");
52 
53  /* Graphics memory */
54  const u32 gms_sizek = decode_igd_memory_size((ggc >> 4) & 0xf);
55  printk(BIOS_DEBUG, "%uM UMA", gms_sizek >> 10);
56  tomk -= gms_sizek;
57  uma_sizek += gms_sizek;
58 
59  /* GTT Graphics Stolen Memory Size (GGMS) */
60  const u32 gsm_sizek = decode_igd_gtt_size((ggc >> 8) & 0xf);
61  printk(BIOS_DEBUG, " and %uM GTT\n", gsm_sizek >> 10);
62  tomk -= gsm_sizek;
63  uma_sizek += gsm_sizek;
64 
65  printk(BIOS_DEBUG, "TSEG decoded, subtracting ");
66  const u32 tseg_sizek = decode_tseg_size(
67  pci_read_config8(dev, D0F0_ESMRAMC)) >> 10;
68  uma_sizek += tseg_sizek;
69  tomk -= tseg_sizek;
70 
71  printk(BIOS_DEBUG, "%dM\n", tseg_sizek >> 10);
72 
73  /* cbmem_top can be shifted downwards due to alignment.
74  Mark the region between cbmem_top and tomk as unusable */
75  delta_cbmem = tomk - ((uint32_t)(uintptr_t)cbmem_top() >> 10);
76  tomk -= delta_cbmem;
77  uma_sizek += delta_cbmem;
78 
79  printk(BIOS_DEBUG, "Unused RAM between cbmem_top and TOM: 0x%xK\n", delta_cbmem);
80 
81  printk(BIOS_INFO, "Available memory below 4GB: %uM\n", tomk >> 10);
82 
83  /* Report the memory regions */
84  ram_resource(dev, index++, 0, legacy_hole_base_k);
85  mmio_resource(dev, index++, legacy_hole_base_k,
86  (0xc0000 >> 10) - legacy_hole_base_k);
87  reserved_ram_resource(dev, index++, 0xc0000 >> 10,
88  (0x100000 - 0xc0000) >> 10);
89  ram_resource(dev, index++, 0x100000 >> 10, (tomk - (0x100000 >> 10)));
90 
91  /*
92  * If >= 4GB installed then memory from TOLUD to 4GB
93  * is remapped above TOM, TOUUD will account for both
94  */
95  touud >>= 10; /* Convert to KB */
96  if (touud > top32memk) {
97  ram_resource(dev, index++, top32memk, touud - top32memk);
98  printk(BIOS_INFO, "Available memory above 4GB: %lluM\n",
99  (touud - top32memk) >> 10);
100  }
101 
102  printk(BIOS_DEBUG, "Adding UMA memory area base=0x%08x size=0x%08x\n",
103  tomk << 10, uma_sizek << 10);
104  uma_resource(dev, index++, tomk, uma_sizek);
105 
106  /* Reserve high memory where the NB BARs are up to 4GiB */
107  fixed_mem_resource(dev, index++, DEFAULT_HECIBAR >> 10,
108  top32memk - (DEFAULT_HECIBAR >> 10),
110 
111  mmconf_resource(dev, index++);
112 }
113 
114 static void mch_domain_set_resources(struct device *dev)
115 {
116  struct resource *res;
117 
118  for (res = dev->resource_list; res; res = res->next)
119  report_resource_stored(dev, res, "");
120 
122 }
123 
124 static void mch_domain_init(struct device *dev)
125 {
126  /* Enable SERR */
128 }
129 
130 static const char *northbridge_acpi_name(const struct device *dev)
131 {
132  if (dev->path.type == DEVICE_PATH_DOMAIN)
133  return "PCI0";
134 
135  if (dev->path.type != DEVICE_PATH_PCI || dev->bus->secondary != 0)
136  return NULL;
137 
138  switch (dev->path.pci.devfn) {
139  case PCI_DEVFN(0, 0):
140  return "MCHC";
141  }
142 
143  return NULL;
144 }
145 
147 {
148  struct device *dev = pcidev_on_root(0, 0);
149 
150  if (dev == NULL)
151  die("could not find pci 00:00.0!\n");
152 
153  pci_write_config8(dev, D0F0_SMRAM, smram);
154 }
155 
156 static struct device_operations pci_domain_ops = {
158  .set_resources = mch_domain_set_resources,
159  .init = mch_domain_init,
160  .scan_bus = pci_domain_scan_bus,
161  .write_acpi_tables = northbridge_write_acpi_tables,
162  .acpi_fill_ssdt = generate_cpu_entries,
163  .acpi_name = northbridge_acpi_name,
164 };
165 
166 static struct device_operations cpu_bus_ops = {
168  .set_resources = noop_set_resources,
169  .init = mp_cpu_bus_init,
170 };
171 
172 static void enable_dev(struct device *dev)
173 {
174  /* Set the operations if it is a special bus type */
175  if (dev->path.type == DEVICE_PATH_DOMAIN)
176  dev->ops = &pci_domain_ops;
177  else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER)
178  dev->ops = &cpu_bus_ops;
179 }
180 
181 static void hide_pci_fn(const int dev_bit_base, const struct device *dev)
182 {
183  if (!dev || dev->enabled)
184  return;
185  const unsigned int fn = PCI_FUNC(dev->path.pci.devfn);
186  const struct device *const d0f0 = pcidev_on_root(0, 0);
187  pci_update_config32(d0f0, D0F0_DEVEN, ~(1 << (dev_bit_base + fn)), 0);
188 }
189 
190 static void hide_pci_dev(const int dev, int functions, const int dev_bit_base)
191 {
192  for (; functions >= 0; functions--)
193  hide_pci_fn(dev_bit_base, pcidev_on_root(dev, functions));
194 }
195 
196 static void x4x_init(void *const chip_info)
197 {
198  struct device *const d0f0 = pcidev_on_root(0x0, 0);
199 
200  /* Hide internal functions based on devicetree info. */
201  hide_pci_dev(6, 0, 13); /* PEG1: only on P45 */
202  hide_pci_dev(3, 3, 6); /* ME */
203  hide_pci_dev(2, 1, 3); /* IGD */
204  hide_pci_dev(1, 0, 1); /* PEG0 */
205 
206  const u32 deven = pci_read_config32(d0f0, D0F0_DEVEN);
207  if (!(deven & (0xf << 6)))
208  pci_write_config32(d0f0, D0F0_DEVEN, deven & ~(1 << 14));
209 }
210 
212  CHIP_NAME("Intel 4-Series Northbridge")
213  .enable_dev = enable_dev,
214  .init = x4x_init,
215 };
#define KiB
Definition: helpers.h:75
#define GiB
Definition: helpers.h:77
void * cbmem_top(void)
Definition: imd_cbmem.c:18
#define printk(level,...)
Definition: stdlib.h:16
void __noreturn die(const char *fmt,...)
Definition: die.c:17
void generate_cpu_entries(const struct device *device)
Definition: acpi.c:334
void assign_resources(struct bus *bus)
Assign the computed resources to the devices on the bus.
Definition: device.c:268
DEVTREE_CONST struct device * pcidev_on_root(uint8_t dev, uint8_t fn)
Definition: device_const.c:260
void fixed_mem_resource(struct device *dev, unsigned long index, unsigned long basek, unsigned long sizek, unsigned long type)
Definition: device_util.c:825
void mmconf_resource(struct device *dev, unsigned long index)
Definition: device_util.c:857
void report_resource_stored(struct device *dev, const struct resource *resource, const char *comment)
Print the resource that was just stored.
Definition: device_util.c:508
u32 decode_igd_memory_size(u32 gms)
Decodes used Graphics Mode Select (GMS) to kilobytes.
Definition: memmap.c:24
#define D0F0_ESMRAMC
Definition: gm45.h:187
#define D0F0_TOM
Definition: gm45.h:188
u32 decode_tseg_size(u8 esmramc)
Definition: memmap.c:57
#define D0F0_SMRAM
Definition: gm45.h:186
#define D0F0_TOLUD
Definition: gm45.h:190
u32 decode_igd_gtt_size(u32 gsm)
Decodes used Graphics Stolen Memory (GSM) to kilobytes.
Definition: memmap.c:36
#define D0F0_TOUUD
Definition: gm45.h:189
#define D0F0_DEVEN
Definition: gm45.h:177
#define D0F0_GGC
Definition: gm45.h:176
#define CHIP_NAME(X)
Definition: device.h:32
#define uma_resource(dev, idx, basek, sizek)
Definition: device.h:331
static void noop_read_resources(struct device *dev)
Standard device operations function pointers shims.
Definition: device.h:73
static void noop_set_resources(struct device *dev)
Definition: device.h:74
static void mp_cpu_bus_init(struct device *dev)
Definition: device.h:240
#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 void pci_write_config32(const struct device *dev, u16 reg, u32 val)
Definition: pci_ops.h:76
static __always_inline void pci_update_config32(const struct device *dev, u16 reg, u32 mask, u32 or)
Definition: pci_ops.h:120
static __always_inline void pci_or_config16(const struct device *dev, u16 reg, u16 ormask)
Definition: pci_ops.h:180
static __always_inline u16 pci_read_config16(const struct device *dev, u16 reg)
Definition: pci_ops.h:52
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
static __always_inline u8 pci_read_config8(const struct device *dev, u16 reg)
Definition: pci_ops.h:46
static __always_inline void pci_write_config8(const struct device *dev, u16 reg, u8 val)
Definition: pci_ops.h:64
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
unsigned long northbridge_write_acpi_tables(struct device *device, unsigned long start)
void northbridge_write_smram(u8 smram)
Definition: northbridge.c:169
#define DEFAULT_HECIBAR
Definition: memmap.h:6
static struct device_operations cpu_bus_ops
Definition: northbridge.c:166
static struct device_operations pci_domain_ops
Definition: northbridge.c:156
static void enable_dev(struct device *dev)
Definition: northbridge.c:172
static void hide_pci_fn(const int dev_bit_base, const struct device *dev)
Definition: northbridge.c:181
static void mch_domain_set_resources(struct device *dev)
Definition: northbridge.c:114
static const int legacy_hole_base_k
Definition: northbridge.c:16
static void hide_pci_dev(const int dev, int functions, const int dev_bit_base)
Definition: northbridge.c:190
static void x4x_init(void *const chip_info)
Definition: northbridge.c:196
struct chip_operations northbridge_intel_x4x_ops
Definition: northbridge.c:211
static void mch_domain_read_resources(struct device *dev)
Definition: northbridge.c:18
static void mch_domain_init(struct device *dev)
Definition: northbridge.c:124
static const char * northbridge_acpi_name(const struct device *dev)
Definition: northbridge.c:130
@ DEVICE_PATH_PCI
Definition: path.h:9
@ DEVICE_PATH_CPU_CLUSTER
Definition: path.h:14
@ DEVICE_PATH_DOMAIN
Definition: path.h:13
#define PCI_COMMAND_SERR
Definition: pci_def.h:19
#define PCI_DEVFN(slot, func)
Definition: pci_def.h:548
#define PCI_FUNC(devfn)
Definition: pci_def.h:550
#define PCI_COMMAND
Definition: pci_def.h:10
void pci_domain_read_resources(struct device *dev)
Definition: pci_device.c:547
void pci_domain_scan_bus(struct device *dev)
Scan a PCI domain.
Definition: pci_device.c:1610
#define IORESOURCE_RESERVE
Definition: resource.h:30
#define NULL
Definition: stddef.h:19
uint64_t u64
Definition: stdint.h:54
unsigned int uint32_t
Definition: stdint.h:14
uint32_t u32
Definition: stdint.h:51
unsigned long uintptr_t
Definition: stdint.h:21
uint16_t u16
Definition: stdint.h:48
uint8_t u8
Definition: stdint.h:45
uint16_t secondary
Definition: device.h:84
void(* read_resources)(struct device *dev)
Definition: device.h:39
struct pci_path pci
Definition: path.h:116
enum device_path_type type
Definition: path.h:114
Definition: device.h:107
struct device_path path
Definition: device.h:115
struct device_operations * ops
Definition: device.h:143
DEVTREE_CONST struct bus * bus
Definition: device.h:108
DEVTREE_CONST struct bus * link_list
Definition: device.h:139
DEVTREE_CONST struct resource * resource_list
Definition: device.h:134
DEVTREE_CONST void * chip_info
Definition: device.h:164
unsigned int enabled
Definition: device.h:122
unsigned int devfn
Definition: path.h:54
DEVTREE_CONST struct resource * next
Definition: resource.h:48