blob: cba57d866a2608267c82caf67a7b3fc2bbacfe78 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06002
Michał Żygowski2f399b72020-04-02 19:51:37 +02003#include <commonlib/helpers.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -06004#include <console/console.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02005#include <device/pci_ops.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07006#include <acpi/acpi.h>
7#include <acpi/acpi_ivrs.h>
Michał Żygowski208318c2020-03-20 15:54:27 +01008#include <arch/ioapic.h>
Elyes HAOUAS146d0c22020-07-22 11:47:08 +02009#include <types.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060010#include <device/device.h>
11#include <device/pci.h>
12#include <device/pci_ids.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060013#include <string.h>
Michał Żygowski2f399b72020-04-02 19:51:37 +020014#include <stdlib.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060015#include <lib.h>
Michał Kopećdc35d2a2021-11-30 17:40:52 +010016#include <cpu/x86/mp.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060017#include <Porting.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060018#include <Topology.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020019#include <cpu/amd/msr.h>
20#include <cpu/amd/mtrr.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070021#include <acpi/acpigen.h>
Angel Ponsec5cf152020-11-10 20:42:07 +010022#include <northbridge/amd/nb_common.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030023#include <northbridge/amd/agesa/agesa_helper.h>
Michał Żygowski2f399b72020-04-02 19:51:37 +020024#include <southbridge/amd/pi/hudson/pci_devs.h>
Arthur Heymans44807ac2022-09-13 12:43:37 +020025#include <amdblocks/cpu.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060026
Kyösti Mälkki113f6702018-05-20 20:12:32 +030027#define MAX_NODE_NUMS MAX_NODES
Michał Żygowski6ca5b472019-09-10 15:10:22 +020028#define PCIE_CAP_AER BIT(5)
29#define PCIE_CAP_ACS BIT(6)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060030
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030031static struct device *__f0_dev[MAX_NODE_NUMS];
32static struct device *__f1_dev[MAX_NODE_NUMS];
33static struct device *__f2_dev[MAX_NODE_NUMS];
34static struct device *__f4_dev[MAX_NODE_NUMS];
Subrata Banikb1434fc2019-03-15 22:20:41 +053035static unsigned int fx_devs = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060036
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030037static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060038{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020039 return pcidev_on_root(DEV_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -060040}
41
Michał Kopećca1e8aa2021-12-03 15:17:46 +010042static struct device *get_mc_dev(void)
43{
44 return pcidev_on_root(DEV_CDB, 0);
45}
46
47static unsigned int get_node_nums(void)
48{
49 static unsigned int node_nums;
50
51 if (node_nums)
52 return node_nums;
53
Elyes Haouasf9b535e2022-07-16 09:47:42 +020054 node_nums = ((pci_read_config32(get_mc_dev(), 0x60) >> 4) & 7) + 1; //NodeCnt[2:0]
Michał Kopećca1e8aa2021-12-03 15:17:46 +010055
56 return node_nums;
57}
58
Bruce Griffith27ed80b2014-08-15 11:46:25 -060059static void get_fx_devs(void)
60{
61 int i;
62 for (i = 0; i < MAX_NODE_NUMS; i++) {
63 __f0_dev[i] = get_node_pci(i, 0);
64 __f1_dev[i] = get_node_pci(i, 1);
65 __f2_dev[i] = get_node_pci(i, 2);
66 __f4_dev[i] = get_node_pci(i, 4);
67 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
Elyes Haouasf9b535e2022-07-16 09:47:42 +020068 fx_devs = i + 1;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060069 }
70 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
71 die("Cannot find 0:0x18.[0|1]\n");
72 }
Elyes HAOUASa8131602016-09-19 10:27:57 -060073 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -060074}
75
Subrata Banikb1434fc2019-03-15 22:20:41 +053076static void f1_write_config32(unsigned int reg, u32 value)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060077{
78 int i;
79 if (fx_devs == 0)
80 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +020081 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030082 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060083 dev = __f1_dev[i];
84 if (dev && dev->enabled) {
85 pci_write_config32(dev, reg, value);
86 }
87 }
88}
89
Michał Żygowski88a0ce62021-05-05 09:52:59 +020090static int get_dram_base_limit(u32 nodeid, resource_t *basek, resource_t *limitk)
91{
92 u32 temp;
93
94 if (fx_devs == 0)
95 get_fx_devs();
96
97
98 temp = pci_read_config32(__f1_dev[nodeid], 0x40 + (nodeid << 3)); //[39:24] at [31:16]
99 if (!(temp & 1))
100 return 0; // this memory range is not enabled
101 /*
102 * BKDG: {DramBase[39:24], 00_0000h} <= address[39:0] so shift left by 8 bits
103 * for physical address and the convert to KiB by shifting 10 bits left
104 */
105 *basek = ((temp & 0xffff0000)) >> (10 - 8);
106 /*
107 * BKDG address[39:0] <= {DramLimit[39:24], FF_FFFFh} converted as above but
108 * ORed with 0xffff to get real limit before shifting.
109 */
110 temp = pci_read_config32(__f1_dev[nodeid], 0x44 + (nodeid << 3)); //[39:24] at [31:16]
111 *limitk = ((temp & 0xffff0000) | 0xffff) >> (10 - 8);
112 *limitk += 1; // round up last byte
113
114 return 1;
115}
116
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300117static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600118{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200119 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600120}
121
122static void set_vga_enable_reg(u32 nodeid, u32 linkn)
123{
124 u32 val;
125
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200126 val = 1 | (nodeid << 4) | (linkn << 12);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600127 /* it will routing
128 * (1)mmio 0xa0000:0xbffff
129 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
130 */
131 f1_write_config32(0xf4, val);
132
133}
134
Michał Żygowski58d6f962021-05-05 10:52:08 +0200135static void add_fixed_resources(struct device *dev, int index)
136{
137 /* Reserve everything between A segment and 1MB:
138 *
139 * 0xa0000 - 0xbffff: legacy VGA
140 * 0xc0000 - 0xfffff: option ROMs and SeaBIOS (if used)
141 */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300142 mmio_resource_kb(dev, index++, 0xa0000 >> 10, (0xc0000 - 0xa0000) >> 10);
143 reserved_ram_resource_kb(dev, index++, 0xc0000 >> 10, (0x100000 - 0xc0000) >> 10);
Michał Żygowski58d6f962021-05-05 10:52:08 +0200144
145 if (fx_devs == 0)
146 get_fx_devs();
147
148 /* Check if CC6 save area is enabled (bit 18 CC6SaveEn) */
149 if (pci_read_config32(__f2_dev[0], 0x118) & (1 << 18)) {
150 /* Add CC6 DRAM UC resource residing at DRAM Limit of size 16MB as per BKDG */
151 resource_t basek, limitk;
152 if (!get_dram_base_limit(0, &basek, &limitk))
153 return;
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200154 mmio_resource_kb(dev, index++, limitk, 16 * 1024);
Michał Żygowski58d6f962021-05-05 10:52:08 +0200155 }
156}
157
Michał Żygowskifb198c62021-05-09 13:54:09 +0200158static void nb_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600159{
160 struct resource *res;
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300161
162 /*
163 * This MMCONF resource must be reserved in the PCI domain.
164 * It is not honored by the coreboot resource allocator if it is in
165 * the CPU_CLUSTER.
166 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200167 mmconf_resource(dev, MMIO_CONF_BASE);
Michał Żygowski208318c2020-03-20 15:54:27 +0100168
169 /* NB IOAPIC2 resource */
170 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
171 res->base = IO_APIC2_ADDR;
172 res->size = 0x00001000;
173 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Michał Żygowski58d6f962021-05-05 10:52:08 +0200174
175 add_fixed_resources(dev, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600176}
177
Subrata Banikb1434fc2019-03-15 22:20:41 +0530178static void create_vga_resource(struct device *dev, unsigned int nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600179{
180 struct bus *link;
Michał Kopećca1e8aa2021-12-03 15:17:46 +0100181 unsigned int sblink;
182
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200183 sblink = (pci_read_config32(get_mc_dev(), 0x64) >> 8) & 7; // don't forget sublink1
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600184
185 /* find out which link the VGA card is connected,
186 * we only deal with the 'first' vga card */
187 for (link = dev->link_list; link; link = link->next) {
188 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800189#if CONFIG(MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300190 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600191 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200192 link->secondary, link->subordinate);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600193 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600194 if ((vga_pri->bus->secondary >= link->secondary) &&
195 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600196#endif
197 break;
198 }
199 }
200
201 /* no VGA card installed */
202 if (link == NULL)
203 return;
204
205 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
206 set_vga_enable_reg(nodeid, sblink);
207}
208
Michał Żygowskifb198c62021-05-09 13:54:09 +0200209static void nb_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600210{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530211 unsigned int nodeid;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600212
213 /* Find the nodeid */
214 nodeid = amdfam16_nodeid(dev);
215
216 create_vga_resource(dev, nodeid); //TODO: do we need this?
217
Michał Żygowskifb198c62021-05-09 13:54:09 +0200218 pci_dev_set_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600219}
220
221static void northbridge_init(struct device *dev)
222{
Kyösti Mälkkid1534e42023-04-09 10:01:58 +0300223 register_new_ioapic((u8 *)IO_APIC2_ADDR);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600224}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200225
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100226static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200227{
228 void *addr, *current;
229
230 /* Skip the HEST header. */
231 current = (void *)(hest + 1);
232
233 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
234 if (addr != NULL)
235 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
236
237 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
238 if (addr != NULL)
239 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
240
241 return (unsigned long)current;
242}
243
Michał Żygowski2f399b72020-04-02 19:51:37 +0200244unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500245{
Michał Żygowski2f399b72020-04-02 19:51:37 +0200246 /* 8-byte IVHD structures must be aligned to the 8-byte boundary. */
247 current = ALIGN_UP(current, 8);
248 ivrs_ivhd_special_t *ivhd_ioapic = (ivrs_ivhd_special_t *)current;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500249
Michał Żygowski2f399b72020-04-02 19:51:37 +0200250 ivhd_ioapic->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
251 ivhd_ioapic->reserved = 0x0000;
252 ivhd_ioapic->dte_setting = IVHD_DTE_LINT_1_PASS | IVHD_DTE_LINT_0_PASS |
253 IVHD_DTE_SYS_MGT_NO_TRANS | IVHD_DTE_NMI_PASS |
254 IVHD_DTE_EXT_INT_PASS | IVHD_DTE_INIT_PASS;
Kyösti Mälkkid1534e42023-04-09 10:01:58 +0300255 ivhd_ioapic->handle = get_ioapic_id(VIO_APIC_VADDR);
Michał Żygowski2f399b72020-04-02 19:51:37 +0200256 ivhd_ioapic->source_dev_id = PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC);
257 ivhd_ioapic->variety = IVHD_SPECIAL_DEV_IOAPIC;
258 current += sizeof(ivrs_ivhd_special_t);
259
260 ivhd_ioapic = (ivrs_ivhd_special_t *)current;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200261 ivhd_ioapic->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
262 ivhd_ioapic->reserved = 0x0000;
263 ivhd_ioapic->dte_setting = 0x00;
Kyösti Mälkkid1534e42023-04-09 10:01:58 +0300264 ivhd_ioapic->handle = get_ioapic_id((u8 *)IO_APIC2_ADDR);
Michał Żygowski2f399b72020-04-02 19:51:37 +0200265 ivhd_ioapic->source_dev_id = PCI_DEVFN(0, 1);
266 ivhd_ioapic->variety = IVHD_SPECIAL_DEV_IOAPIC;
267 current += sizeof(ivrs_ivhd_special_t);
268
269 return current;
270}
271
272static unsigned long ivhd_describe_hpet(unsigned long current)
273{
274 /* 8-byte IVHD structures must be aligned to the 8-byte boundary. */
275 current = ALIGN_UP(current, 8);
276 ivrs_ivhd_special_t *ivhd_hpet = (ivrs_ivhd_special_t *)current;
277
278 ivhd_hpet->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
279 ivhd_hpet->reserved = 0x0000;
280 ivhd_hpet->dte_setting = 0x00;
281 ivhd_hpet->handle = 0x00;
282 ivhd_hpet->source_dev_id = PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC);
283 ivhd_hpet->variety = IVHD_SPECIAL_DEV_HPET;
284 current += sizeof(ivrs_ivhd_special_t);
285
286 return current;
287}
288
289static unsigned long ivhd_dev_range(unsigned long current, uint16_t start_devid,
290 uint16_t end_devid, uint8_t setting)
291{
292 /* 4-byte IVHD structures must be aligned to the 4-byte boundary. */
293 current = ALIGN_UP(current, 4);
294 ivrs_ivhd_generic_t *ivhd_range = (ivrs_ivhd_generic_t *)current;
295
296 /* Create the start range IVHD entry */
297 ivhd_range->type = IVHD_DEV_4_BYTE_START_RANGE;
298 ivhd_range->dev_id = start_devid;
299 ivhd_range->dte_setting = setting;
300 current += sizeof(ivrs_ivhd_generic_t);
301
302 /* Create the end range IVHD entry */
303 ivhd_range = (ivrs_ivhd_generic_t *)current;
304 ivhd_range->type = IVHD_DEV_4_BYTE_END_RANGE;
305 ivhd_range->dev_id = end_devid;
306 ivhd_range->dte_setting = setting;
307 current += sizeof(ivrs_ivhd_generic_t);
308
309 return current;
310}
311
312static unsigned long add_ivhd_dev_entry(struct device *parent, struct device *dev,
313 unsigned long *current, uint8_t type, uint8_t data)
314{
315 if (type == IVHD_DEV_4_BYTE_SELECT) {
316 /* 4-byte IVHD structures must be aligned to the 4-byte boundary. */
317 *current = ALIGN_UP(*current, 4);
318 ivrs_ivhd_generic_t *ivhd_entry = (ivrs_ivhd_generic_t *)*current;
319
320 ivhd_entry->type = type;
321 ivhd_entry->dev_id = dev->path.pci.devfn | (dev->bus->secondary << 8);
322 ivhd_entry->dte_setting = data;
323 *current += sizeof(ivrs_ivhd_generic_t);
324 } else if (type == IVHD_DEV_8_BYTE_ALIAS_SELECT) {
325 /* 8-byte IVHD structures must be aligned to the 8-byte boundary. */
326 *current = ALIGN_UP(*current, 8);
327 ivrs_ivhd_alias_t *ivhd_entry = (ivrs_ivhd_alias_t *)*current;
328
329 ivhd_entry->type = type;
330 ivhd_entry->dev_id = dev->path.pci.devfn | (dev->bus->secondary << 8);
331 ivhd_entry->dte_setting = data;
332 ivhd_entry->reserved1 = 0;
333 ivhd_entry->reserved2 = 0;
334 ivhd_entry->source_dev_id = parent->path.pci.devfn |
335 (parent->bus->secondary << 8);
336 *current += sizeof(ivrs_ivhd_alias_t);
337 }
338
339 return *current;
340}
341
342static void ivrs_add_device_or_bridge(struct device *parent, struct device *dev,
343 unsigned long *current, uint16_t *ivhd_length)
344{
345 unsigned int header_type, is_pcie;
346 unsigned long current_backup;
347
348 header_type = dev->hdr_type & 0x7f;
349 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
350
351 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
352 (header_type == PCI_HEADER_TYPE_BRIDGE)) && is_pcie) {
353 /* Device or Bridge is PCIe */
354 current_backup = *current;
355 add_ivhd_dev_entry(parent, dev, current, IVHD_DEV_4_BYTE_SELECT, 0x0);
356 *ivhd_length += (*current - current_backup);
357 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) && !is_pcie) {
358 /* Device is legacy PCI or PCI-X */
359 current_backup = *current;
360 add_ivhd_dev_entry(parent, dev, current, IVHD_DEV_8_BYTE_ALIAS_SELECT, 0x0);
361 *ivhd_length += (*current - current_backup);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500362 }
363}
364
Michał Żygowski2f399b72020-04-02 19:51:37 +0200365static void add_ivhd_device_entries(struct device *parent, struct device *dev,
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500366 unsigned int depth, int linknum, int8_t *root_level,
Michał Żygowski2f399b72020-04-02 19:51:37 +0200367 unsigned long *current, uint16_t *ivhd_length)
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500368{
369 struct device *sibling;
370 struct bus *link;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200371
372 if (!root_level) {
373 root_level = malloc(sizeof(int8_t));
374 *root_level = -1;
375 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500376
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500377 if (dev->path.type == DEVICE_PATH_PCI) {
378
379 if ((dev->bus->secondary == 0x0) &&
380 (dev->path.pci.devfn == 0x0))
381 *root_level = depth;
382
383 if ((*root_level != -1) && (dev->enabled)) {
Michał Żygowski2f399b72020-04-02 19:51:37 +0200384 if (depth != *root_level)
385 ivrs_add_device_or_bridge(parent, dev, current, ivhd_length);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500386 }
387 }
388
389 for (link = dev->link_list; link; link = link->next)
390 for (sibling = link->children; sibling; sibling =
391 sibling->sibling)
Michał Żygowski2f399b72020-04-02 19:51:37 +0200392 add_ivhd_device_entries(dev, sibling, depth + 1, depth, root_level,
393 current, ivhd_length);
394
395 free(root_level);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500396}
397
Michał Żygowski2f399b72020-04-02 19:51:37 +0200398#define IOMMU_MMIO32(x) (*((volatile uint32_t *)(x)))
399#define EFR_SUPPORT BIT(27)
400
401static unsigned long acpi_fill_ivrs11(unsigned long current, acpi_ivrs_t *ivrs_agesa)
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500402{
Michał Żygowski2f399b72020-04-02 19:51:37 +0200403 acpi_ivrs_ivhd11_t *ivhd_11;
404 unsigned long current_backup;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500405
Michał Żygowski2f399b72020-04-02 19:51:37 +0200406 /*
407 * These devices should be already found by previous function.
408 * Do not perform NULL checks.
409 */
410 struct device *nb_dev = pcidev_on_root(0, 0);
411 struct device *iommu_dev = pcidev_on_root(0, 2);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500412
Michał Żygowski2f399b72020-04-02 19:51:37 +0200413 /*
414 * In order to utilize all features, firmware should expose type 11h
415 * IVHD which supersedes the type 10h.
416 */
417 memset((void *)current, 0, sizeof(acpi_ivrs_ivhd11_t));
418 ivhd_11 = (acpi_ivrs_ivhd11_t *)current;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500419
Michał Żygowski2f399b72020-04-02 19:51:37 +0200420 /* Enable EFR */
421 ivhd_11->type = IVHD_BLOCK_TYPE_FULL__FIXED;
422 /* For type 11h bits 6 and 7 are reserved */
423 ivhd_11->flags = ivrs_agesa->ivhd.flags & 0x3f;
424 ivhd_11->length = sizeof(struct acpi_ivrs_ivhd_11);
425 /* BDF <bus>:00.2 */
426 ivhd_11->device_id = 0x02 | (nb_dev->bus->secondary << 8);
427 /* PCI Capability block 0x40 (type 0xf, "Secure device") */
428 ivhd_11->capability_offset = 0x40;
429 ivhd_11->iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
430 ivhd_11->iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
431 ivhd_11->pci_segment_group = 0x0000;
432 ivhd_11->iommu_info = ivrs_agesa->ivhd.iommu_info;
433 ivhd_11->iommu_attributes.perf_counters =
434 (IOMMU_MMIO32(ivhd_11->iommu_base_low + 0x4000) >> 7) & 0xf;
435 ivhd_11->iommu_attributes.perf_counter_banks =
436 (IOMMU_MMIO32(ivhd_11->iommu_base_low + 0x4000) >> 12) & 0x3f;
437 ivhd_11->iommu_attributes.msi_num_ppr =
438 (pci_read_config32(iommu_dev, ivhd_11->capability_offset + 0x10) >> 27) & 0x1f;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500439
Michał Żygowski2f399b72020-04-02 19:51:37 +0200440 if (pci_read_config32(iommu_dev, ivhd_11->capability_offset) & EFR_SUPPORT) {
441 ivhd_11->efr_reg_image_low = IOMMU_MMIO32(ivhd_11->iommu_base_low + 0x30);
442 ivhd_11->efr_reg_image_high = IOMMU_MMIO32(ivhd_11->iommu_base_low + 0x34);
443 }
444
445 current += sizeof(acpi_ivrs_ivhd11_t);
446
447 /* Now repeat all the device entries from type 10h */
448 current_backup = current;
449 current = ivhd_dev_range(current, PCI_DEVFN(1, 0), PCI_DEVFN(0x1f, 6), 0);
450 ivhd_11->length += (current - current_backup);
451 add_ivhd_device_entries(NULL, all_devices, 0, -1, NULL, &current, &ivhd_11->length);
452
453 /* Describe HPET */
454 current_backup = current;
455 current = ivhd_describe_hpet(current);
456 ivhd_11->length += (current - current_backup);
457
458 /* Describe IOAPICs */
459 current_backup = current;
460 current = acpi_fill_ivrs_ioapic(ivrs_agesa, current);
461 ivhd_11->length += (current - current_backup);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500462
463 return current;
464}
465
466static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
467{
Piotr Król063e1562018-07-22 20:52:26 +0200468 acpi_ivrs_t *ivrs_agesa;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200469 unsigned long current_backup;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500470
Michał Żygowski2f399b72020-04-02 19:51:37 +0200471 struct device *nb_dev = pcidev_on_root(0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500472 if (!nb_dev) {
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500473 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
474 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
475
476 return (unsigned long)ivrs;
477 }
478
Michał Żygowski2f399b72020-04-02 19:51:37 +0200479 struct device *iommu_dev = pcidev_on_root(0, 2);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500480
Michał Żygowski2f399b72020-04-02 19:51:37 +0200481 if (!iommu_dev) {
482 printk(BIOS_WARNING, "%s: IOMMU device not found\n", __func__);
483
484 return (unsigned long)ivrs;
485 }
486
Piotr Król063e1562018-07-22 20:52:26 +0200487 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
488 if (ivrs_agesa != NULL) {
Michał Żygowski2f399b72020-04-02 19:51:37 +0200489 ivrs->iv_info = ivrs_agesa->iv_info;
490 ivrs->ivhd.type = IVHD_BLOCK_TYPE_LEGACY__FIXED;
491 ivrs->ivhd.flags = ivrs_agesa->ivhd.flags;
Piotr Król063e1562018-07-22 20:52:26 +0200492 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
493 /* BDF <bus>:00.2 */
Michał Żygowski2f399b72020-04-02 19:51:37 +0200494 ivrs->ivhd.device_id = 0x02 | (nb_dev->bus->secondary << 8);
495 /* PCI Capability block 0x40 (type 0xf, "Secure device") */
Piotr Król063e1562018-07-22 20:52:26 +0200496 ivrs->ivhd.capability_offset = 0x40;
497 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
498 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200499 ivrs->ivhd.pci_segment_group = 0x0000;
500 ivrs->ivhd.iommu_info = ivrs_agesa->ivhd.iommu_info;
501 ivrs->ivhd.iommu_feature_info = ivrs_agesa->ivhd.iommu_feature_info;
502 /* Enable EFR if supported */
503 if (pci_read_config32(iommu_dev, ivrs->ivhd.capability_offset) & EFR_SUPPORT)
504 ivrs->iv_info |= IVINFO_EFR_SUPPORTED;
Piotr Król063e1562018-07-22 20:52:26 +0200505 } else {
506 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
507
508 return (unsigned long)ivrs;
509 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500510
Michał Żygowski2f399b72020-04-02 19:51:37 +0200511 /*
512 * Add all possible PCI devices on bus 0 that can generate transactions
513 * processed by IOMMU. Start with device 00:01.0 since IOMMU does not
514 * translate transactions generated by itself.
515 */
516 current_backup = current;
517 current = ivhd_dev_range(current, PCI_DEVFN(1, 0), PCI_DEVFN(0x1f, 6), 0);
518 ivrs->ivhd.length += (current - current_backup);
519 add_ivhd_device_entries(NULL, all_devices, 0, -1, NULL, &current, &ivrs->ivhd.length);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500520
Michał Żygowski2f399b72020-04-02 19:51:37 +0200521 /* Describe HPET */
522 current_backup = current;
523 current = ivhd_describe_hpet(current);
524 ivrs->ivhd.length += (current - current_backup);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500525
526 /* Describe IOAPICs */
Michał Żygowski2f399b72020-04-02 19:51:37 +0200527 current_backup = current;
528 current = acpi_fill_ivrs_ioapic(ivrs_agesa, current);
529 ivrs->ivhd.length += (current - current_backup);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500530
Michał Żygowski2f399b72020-04-02 19:51:37 +0200531 /* If EFR is not supported, IVHD type 11h is reserved */
532 if (!(ivrs->iv_info & IVINFO_EFR_SUPPORTED))
533 return current;
534
535 return acpi_fill_ivrs11(current, ivrs_agesa);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500536}
537
Furquan Shaikh7536a392020-04-24 21:59:21 -0700538static void northbridge_fill_ssdt_generator(const struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200539{
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200540 char pscope[] = "\\_SB.PCI0";
541
542 acpigen_write_scope(pscope);
Felix Helde3453782023-04-20 13:06:08 +0200543 acpigen_write_name_dword("TOM1", get_top_of_mem_below_4gb());
544
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200545 /*
546 * Since XP only implements parts of ACPI 2.0, we can't use a qword
547 * here.
548 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
549 * slide 22ff.
550 * Shift value right by 20 bit to make it fit into 32bit,
551 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
552 */
Felix Helde3453782023-04-20 13:06:08 +0200553 acpigen_write_name_dword("TOM2", get_top_of_mem_above_4g() >> 20);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200554 acpigen_pop_len();
555}
556
Michał Żygowski9550e972020-03-20 13:56:46 +0100557static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
558{
559 unsigned int len = ssdt->length - sizeof(acpi_header_t);
560 unsigned int i;
561
562 for (i = sizeof(acpi_header_t); i < len; i++) {
563 /* Search for _PR_ scope and replace it with _SB_ */
564 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
565 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
566 }
567 /* Recalculate checksum */
568 ssdt->checksum = 0;
569 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
570}
571
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700572static unsigned long agesa_write_acpi_tables(const struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200573 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200574 acpi_rsdp_t *rsdp)
575{
576 acpi_srat_t *srat;
577 acpi_slit_t *slit;
578 acpi_header_t *ssdt;
579 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500580 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200581
582 /* HEST */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200583 current = ALIGN_UP(current, 8);
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100584 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200585 acpi_add_table(rsdp, (void *)current);
586 current += ((acpi_header_t *)current)->length;
587
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500588 /* IVRS */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200589 current = ALIGN_UP(current, 8);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500590 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200591 ivrs = (acpi_ivrs_t *)current;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500592 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
593 current += ivrs->header.length;
594 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200595
596 /* SRAT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200597 current = ALIGN_UP(current, 8);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200598 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200599 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200600 if (srat != NULL) {
601 memcpy((void *)current, srat, srat->header.length);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200602 srat = (acpi_srat_t *)current;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200603 current += srat->header.length;
604 acpi_add_table(rsdp, srat);
605 } else {
606 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
607 }
608
609 /* SLIT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200610 current = ALIGN_UP(current, 8);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200611 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200612 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200613 if (slit != NULL) {
614 memcpy((void *)current, slit, slit->header.length);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200615 slit = (acpi_slit_t *)current;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200616 current += slit->header.length;
617 acpi_add_table(rsdp, slit);
618 } else {
619 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
620 }
621
622 /* ALIB */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200623 current = ALIGN_UP(current, 16);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200624 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200625 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200626 if (alib != NULL) {
627 memcpy((void *)current, alib, alib->length);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200628 alib = (acpi_header_t *)current;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200629 current += alib->length;
630 acpi_add_table(rsdp, (void *)alib);
631 }
632 else {
633 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
634 }
635
636 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
637 /* SSDT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200638 current = ALIGN_UP(current, 16);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200639 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200640 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200641 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100642 patch_ssdt_processor_scope(ssdt);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200643 memcpy((void *)current, ssdt, ssdt->length);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200644 ssdt = (acpi_header_t *)current;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200645 current += ssdt->length;
646 }
647 else {
648 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
649 }
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200650 acpi_add_table(rsdp, ssdt);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200651
652 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
653 return current;
654}
655
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600656static struct device_operations northbridge_operations = {
Michał Żygowskifb198c62021-05-09 13:54:09 +0200657 .read_resources = nb_read_resources,
658 .set_resources = nb_set_resources,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600659 .enable_resources = pci_dev_enable_resources,
660 .init = northbridge_init,
Michał Żygowskifb198c62021-05-09 13:54:09 +0200661 .ops_pci = &pci_dev_ops_pci,
Nico Huber68680dd2020-03-31 17:34:52 +0200662 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200663 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600664};
665
666static const struct pci_driver family16_northbridge __pci_driver = {
667 .ops = &northbridge_operations,
Felix Singer43b7f412022-03-07 04:34:52 +0100668 .vendor = PCI_VID_AMD,
669 .device = PCI_DID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600670};
671
672static const struct pci_driver family10_northbridge __pci_driver = {
673 .ops = &northbridge_operations,
Felix Singer43b7f412022-03-07 04:34:52 +0100674 .vendor = PCI_VID_AMD,
675 .device = PCI_DID_AMD_10H_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600676};
677
Dave Frodin891f71a2015-01-19 15:58:24 -0700678static void fam16_finalize(void *chip_info)
679{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300680 struct device *dev;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300681 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Elyes Haouasa1f5ad02022-02-17 18:14:08 +0100682
Dave Frodin891f71a2015-01-19 15:58:24 -0700683 pci_write_config32(dev, 0xF8, 0);
684 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
685
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200686 /*
687 * Currently it is impossible to enable ACS with AGESA by setting the
688 * correct bit for AmdInitMid phase. AGESA code path does not call the
689 * right function that enables these functionalities. Disabled ACS
690 * result in multiple PCIe devices to be assigned to the same IOMMU
691 * group. Without IOMMU group separation the devices cannot be passed
692 * through independently.
693 */
694
695 /* Select GPP link core IO Link Strap Control register 0xB0 */
696 pci_write_config32(dev, 0xE0, 0x014000B0);
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200697
698 /* Enable AER (bit 5) and ACS (bit 6 undocumented) */
Elyes Haouasa1f5ad02022-02-17 18:14:08 +0100699 pci_or_config32(dev, 0xE4, PCIE_CAP_AER | PCIE_CAP_ACS);
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200700
701 /* Select GPP link core Wrapper register 0x00 (undocumented) */
702 pci_write_config32(dev, 0xE0, 0x01300000);
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200703
704 /*
705 * Enable ACS capabilities straps including sub-items. From lspci it
706 * looks like these bits enable: Source Validation and Translation
707 * Blocking
708 */
Elyes Haouasa1f5ad02022-02-17 18:14:08 +0100709 pci_or_config32(dev, 0xE4, (BIT(24) | BIT(25) | BIT(26)));
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200710
Dave Frodin891f71a2015-01-19 15:58:24 -0700711 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300712 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200713 if (dev != NULL) {
Elyes Haouasa1f5ad02022-02-17 18:14:08 +0100714 pci_and_config32(dev, 0x60, ~(1 << 11));
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200715 }
Dave Frodin891f71a2015-01-19 15:58:24 -0700716}
717
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300718struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600719 CHIP_NAME("AMD FAM16 Northbridge")
720 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700721 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600722};
723
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600724#if CONFIG_HW_MEM_HOLE_SIZEK != 0
725struct hw_mem_hole_info {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530726 unsigned int hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600727 int node_id;
728};
729static struct hw_mem_hole_info get_hw_mem_hole_info(void)
730{
731 struct hw_mem_hole_info mem_hole;
732 int i;
733 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
734 mem_hole.node_id = -1;
Michał Kopećca1e8aa2021-12-03 15:17:46 +0100735 for (i = 0; i < get_node_nums(); i++) {
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200736 resource_t basek, limitk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600737 u32 hole;
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200738 if (!get_dram_base_limit(i, &basek, &limitk))
739 continue; // no memory on this node
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600740 hole = pci_read_config32(__f1_dev[i], 0xf0);
741 if (hole & 2) { // we find the hole
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200742 mem_hole.hole_startk = (hole & (0xff << 24)) >> 10;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600743 mem_hole.node_id = i; // record the node No with hole
744 break; // only one hole
745 }
746 }
747
748 /* We need to double check if there is special set on base reg and limit reg
749 * are not continuous instead of hole, it will find out its hole_startk.
750 */
751 if (mem_hole.node_id == -1) {
752 resource_t limitk_pri = 0;
Michał Kopećca1e8aa2021-12-03 15:17:46 +0100753 for (i = 0; i < get_node_nums(); i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600754 resource_t base_k, limit_k;
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200755 if (!get_dram_base_limit(i, &base_k, &limit_k))
756 continue; // no memory on this node
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200757 if (base_k > 4 * 1024 * 1024) break; // don't need to go to check
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600758 if (limitk_pri != base_k) { // we find the hole
Elyes HAOUAS38a4f2a92020-01-07 19:53:36 +0100759 mem_hole.hole_startk = (unsigned int)limitk_pri; // must be below 4G
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600760 mem_hole.node_id = i;
761 break; //only one hole
762 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600763 limitk_pri = limit_k;
764 }
765 }
766 return mem_hole;
767}
768#endif
769
Michał Żygowskif5d457d2021-05-09 13:58:04 +0200770static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600771{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600772 unsigned long mmio_basek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600773 int i, idx;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600774#if CONFIG_HW_MEM_HOLE_SIZEK != 0
775 struct hw_mem_hole_info mem_hole;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600776#endif
777
Michał Żygowskif5d457d2021-05-09 13:58:04 +0200778 pci_domain_read_resources(dev);
779
Michał Żygowski58d6f962021-05-05 10:52:08 +0200780 /* TOP_MEM MSR is our boundary between DRAM and MMIO under 4G */
Felix Held5e9afe72023-04-20 12:55:55 +0200781 mmio_basek = get_top_of_mem_below_4gb() >> 10;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600782
783#if CONFIG_HW_MEM_HOLE_SIZEK != 0
784 /* if the hw mem hole is already set in raminit stage, here we will compare
785 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
786 * use hole_basek as mmio_basek and we don't need to reset hole.
787 * otherwise We reset the hole to the mmio_basek
788 */
789
790 mem_hole = get_hw_mem_hole_info();
791
792 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
793 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
794 mmio_basek = mem_hole.hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600795 }
796#endif
797
798 idx = 0x10;
Michał Kopećca1e8aa2021-12-03 15:17:46 +0100799 for (i = 0; i < get_node_nums(); i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600800 resource_t basek, limitk, sizek; // 4 1T
801
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200802 if (!get_dram_base_limit(i, &basek, &limitk))
803 continue; // no memory on this node
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600804
805 sizek = limitk - basek;
806
Michał Żygowski58d6f962021-05-05 10:52:08 +0200807 printk(BIOS_DEBUG, "node %d: basek=%08llx, limitk=%08llx, sizek=%08llx,\n",
808 i, basek, limitk, sizek);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600809
Elyes Haouas5213b192022-02-25 18:13:03 +0100810 /* See if we need a hole from 0xa0000 (640K) to 0xfffff (1024K) */
Elyes Haouas9d8df302022-02-25 18:23:01 +0100811 if (basek < 640 && sizek > 1024) {
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300812 ram_resource_kb(dev, (idx | i), basek, 640 - basek);
Michał Żygowski58d6f962021-05-05 10:52:08 +0200813 idx += 0x10;
Elyes Haouas9d8df302022-02-25 18:23:01 +0100814 basek = 1024;
Michał Żygowski58d6f962021-05-05 10:52:08 +0200815 sizek = limitk - basek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600816 }
817
Michał Żygowski58d6f962021-05-05 10:52:08 +0200818 printk(BIOS_DEBUG, "node %d: basek=%08llx, limitk=%08llx, sizek=%08llx,\n",
819 i, basek, limitk, sizek);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600820
821 /* split the region to accommodate pci memory space */
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200822 if ((basek < 4 * 1024 * 1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600823 if (basek <= mmio_basek) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530824 unsigned int pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600825 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600826 if (pre_sizek > 0) {
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300827 ram_resource_kb(dev, (idx | i), basek, pre_sizek);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600828 idx += 0x10;
829 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600830 }
831 basek = mmio_basek;
832 }
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200833 if ((basek + sizek) <= 4 * 1024 * 1024) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600834 sizek = 0;
835 }
836 else {
Felix Held5e9afe72023-04-20 12:55:55 +0200837 uint64_t topmem2 = get_top_of_mem_above_4g();
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200838 basek = 4 * 1024 * 1024;
839 sizek = topmem2 / 1024 - basek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600840 }
841 }
842
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300843 ram_resource_kb(dev, (idx | i), basek, sizek);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600844 idx += 0x10;
845 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
846 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600847 }
848
Kyösti Mälkkie87564f2017-04-15 20:07:53 +0300849 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600850}
851
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600852static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100853{
854 if (dev->path.type == DEVICE_PATH_DOMAIN)
855 return "PCI0";
856
857 return NULL;
858}
859
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600860static struct device_operations pci_domain_ops = {
861 .read_resources = domain_read_resources,
Michał Żygowskif5d457d2021-05-09 13:58:04 +0200862 .set_resources = pci_domain_set_resources,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600863 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100864 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600865};
866
Michał Kopećdc35d2a2021-11-30 17:40:52 +0100867static void pre_mp_init(void)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600868{
Michał Kopećdc35d2a2021-11-30 17:40:52 +0100869 x86_setup_mtrrs_with_detect();
870 x86_mtrr_check();
871}
872
Michał Kopećdc35d2a2021-11-30 17:40:52 +0100873static const struct mp_ops mp_ops = {
874 .pre_mp_init = pre_mp_init,
875 .get_cpu_count = get_cpu_count,
876};
877
878void mp_init_cpus(struct bus *cpu_bus)
879{
880 /* TODO: Handle mp_init_with_smm failure? */
881 mp_init_with_smm(cpu_bus, &mp_ops);
882
883 /* The flash is now no longer cacheable. Reset to WP for performance. */
884 mtrr_use_temp_range(OPTIMAL_CACHE_ROM_BASE, OPTIMAL_CACHE_ROM_SIZE,
885 MTRR_TYPE_WRPROT);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600886}
887
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600888static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200889 .read_resources = noop_read_resources,
890 .set_resources = noop_set_resources,
Michał Kopećdc35d2a2021-11-30 17:40:52 +0100891 .init = mp_cpu_bus_init,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600892};
893
894static void root_complex_enable_dev(struct device *dev)
895{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600896 /* Set the operations if it is a special bus type */
897 if (dev->path.type == DEVICE_PATH_DOMAIN) {
898 dev->ops = &pci_domain_ops;
899 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
900 dev->ops = &cpu_bus_ops;
901 }
902}
903
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300904struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600905 CHIP_NAME("AMD FAM16 Root Complex")
906 .enable_dev = root_complex_enable_dev,
907};
908
909/*********************************************************************
910 * Change the vendor / device IDs to match the generic VBIOS header. *
911 *********************************************************************/
912u32 map_oprom_vendev(u32 vendev)
913{
914 u32 new_vendev;
915 new_vendev =
916 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
917
918 if (vendev != new_vendev)
919 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
920
921 return new_vendev;
922}