blob: 644e2d5e3d347daf852116003ed9708f646eebda [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>
18#include <AGESA.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060019#include <Topology.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020020#include <cpu/amd/msr.h>
21#include <cpu/amd/mtrr.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070022#include <acpi/acpigen.h>
Angel Ponsec5cf152020-11-10 20:42:07 +010023#include <northbridge/amd/nb_common.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030024#include <northbridge/amd/agesa/agesa_helper.h>
Michał Żygowski2f399b72020-04-02 19:51:37 +020025#include <southbridge/amd/pi/hudson/pci_devs.h>
Kyösti Mälkki8f86fa02022-12-05 19:31:01 +020026#include <southbridge/amd/pi/hudson/ioapic.h>
Arthur Heymans44807ac2022-09-13 12:43:37 +020027#include <amdblocks/cpu.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060028
Kyösti Mälkki113f6702018-05-20 20:12:32 +030029#define MAX_NODE_NUMS MAX_NODES
Michał Żygowski6ca5b472019-09-10 15:10:22 +020030#define PCIE_CAP_AER BIT(5)
31#define PCIE_CAP_ACS BIT(6)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060032
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030033static struct device *__f0_dev[MAX_NODE_NUMS];
34static struct device *__f1_dev[MAX_NODE_NUMS];
35static struct device *__f2_dev[MAX_NODE_NUMS];
36static struct device *__f4_dev[MAX_NODE_NUMS];
Subrata Banikb1434fc2019-03-15 22:20:41 +053037static unsigned int fx_devs = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060038
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030039static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060040{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020041 return pcidev_on_root(DEV_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -060042}
43
Michał Kopećca1e8aa2021-12-03 15:17:46 +010044static struct device *get_mc_dev(void)
45{
46 return pcidev_on_root(DEV_CDB, 0);
47}
48
49static unsigned int get_node_nums(void)
50{
51 static unsigned int node_nums;
52
53 if (node_nums)
54 return node_nums;
55
Elyes Haouasf9b535e2022-07-16 09:47:42 +020056 node_nums = ((pci_read_config32(get_mc_dev(), 0x60) >> 4) & 7) + 1; //NodeCnt[2:0]
Michał Kopećca1e8aa2021-12-03 15:17:46 +010057
58 return node_nums;
59}
60
Bruce Griffith27ed80b2014-08-15 11:46:25 -060061static void get_fx_devs(void)
62{
63 int i;
64 for (i = 0; i < MAX_NODE_NUMS; i++) {
65 __f0_dev[i] = get_node_pci(i, 0);
66 __f1_dev[i] = get_node_pci(i, 1);
67 __f2_dev[i] = get_node_pci(i, 2);
68 __f4_dev[i] = get_node_pci(i, 4);
69 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
Elyes Haouasf9b535e2022-07-16 09:47:42 +020070 fx_devs = i + 1;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060071 }
72 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
73 die("Cannot find 0:0x18.[0|1]\n");
74 }
Elyes HAOUASa8131602016-09-19 10:27:57 -060075 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -060076}
77
Subrata Banikb1434fc2019-03-15 22:20:41 +053078static void f1_write_config32(unsigned int reg, u32 value)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060079{
80 int i;
81 if (fx_devs == 0)
82 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +020083 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030084 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060085 dev = __f1_dev[i];
86 if (dev && dev->enabled) {
87 pci_write_config32(dev, reg, value);
88 }
89 }
90}
91
Michał Żygowski88a0ce62021-05-05 09:52:59 +020092static int get_dram_base_limit(u32 nodeid, resource_t *basek, resource_t *limitk)
93{
94 u32 temp;
95
96 if (fx_devs == 0)
97 get_fx_devs();
98
99
100 temp = pci_read_config32(__f1_dev[nodeid], 0x40 + (nodeid << 3)); //[39:24] at [31:16]
101 if (!(temp & 1))
102 return 0; // this memory range is not enabled
103 /*
104 * BKDG: {DramBase[39:24], 00_0000h} <= address[39:0] so shift left by 8 bits
105 * for physical address and the convert to KiB by shifting 10 bits left
106 */
107 *basek = ((temp & 0xffff0000)) >> (10 - 8);
108 /*
109 * BKDG address[39:0] <= {DramLimit[39:24], FF_FFFFh} converted as above but
110 * ORed with 0xffff to get real limit before shifting.
111 */
112 temp = pci_read_config32(__f1_dev[nodeid], 0x44 + (nodeid << 3)); //[39:24] at [31:16]
113 *limitk = ((temp & 0xffff0000) | 0xffff) >> (10 - 8);
114 *limitk += 1; // round up last byte
115
116 return 1;
117}
118
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300119static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600120{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200121 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600122}
123
124static void set_vga_enable_reg(u32 nodeid, u32 linkn)
125{
126 u32 val;
127
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200128 val = 1 | (nodeid << 4) | (linkn << 12);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600129 /* it will routing
130 * (1)mmio 0xa0000:0xbffff
131 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
132 */
133 f1_write_config32(0xf4, val);
134
135}
136
Michał Żygowski58d6f962021-05-05 10:52:08 +0200137static void add_fixed_resources(struct device *dev, int index)
138{
139 /* Reserve everything between A segment and 1MB:
140 *
141 * 0xa0000 - 0xbffff: legacy VGA
142 * 0xc0000 - 0xfffff: option ROMs and SeaBIOS (if used)
143 */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300144 mmio_resource_kb(dev, index++, 0xa0000 >> 10, (0xc0000 - 0xa0000) >> 10);
145 reserved_ram_resource_kb(dev, index++, 0xc0000 >> 10, (0x100000 - 0xc0000) >> 10);
Michał Żygowski58d6f962021-05-05 10:52:08 +0200146
147 if (fx_devs == 0)
148 get_fx_devs();
149
150 /* Check if CC6 save area is enabled (bit 18 CC6SaveEn) */
151 if (pci_read_config32(__f2_dev[0], 0x118) & (1 << 18)) {
152 /* Add CC6 DRAM UC resource residing at DRAM Limit of size 16MB as per BKDG */
153 resource_t basek, limitk;
154 if (!get_dram_base_limit(0, &basek, &limitk))
155 return;
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200156 mmio_resource_kb(dev, index++, limitk, 16 * 1024);
Michał Żygowski58d6f962021-05-05 10:52:08 +0200157 }
158}
159
Michał Żygowskifb198c62021-05-09 13:54:09 +0200160static void nb_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600161{
162 struct resource *res;
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300163
164 /*
165 * This MMCONF resource must be reserved in the PCI domain.
166 * It is not honored by the coreboot resource allocator if it is in
167 * the CPU_CLUSTER.
168 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200169 mmconf_resource(dev, MMIO_CONF_BASE);
Michał Żygowski208318c2020-03-20 15:54:27 +0100170
171 /* NB IOAPIC2 resource */
172 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
173 res->base = IO_APIC2_ADDR;
174 res->size = 0x00001000;
175 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Michał Żygowski58d6f962021-05-05 10:52:08 +0200176
177 add_fixed_resources(dev, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600178}
179
Subrata Banikb1434fc2019-03-15 22:20:41 +0530180static void create_vga_resource(struct device *dev, unsigned int nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600181{
182 struct bus *link;
Michał Kopećca1e8aa2021-12-03 15:17:46 +0100183 unsigned int sblink;
184
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200185 sblink = (pci_read_config32(get_mc_dev(), 0x64) >> 8) & 7; // don't forget sublink1
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600186
187 /* find out which link the VGA card is connected,
188 * we only deal with the 'first' vga card */
189 for (link = dev->link_list; link; link = link->next) {
190 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800191#if CONFIG(MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300192 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600193 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 +0200194 link->secondary, link->subordinate);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600195 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600196 if ((vga_pri->bus->secondary >= link->secondary) &&
197 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600198#endif
199 break;
200 }
201 }
202
203 /* no VGA card installed */
204 if (link == NULL)
205 return;
206
207 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
208 set_vga_enable_reg(nodeid, sblink);
209}
210
Michał Żygowskifb198c62021-05-09 13:54:09 +0200211static void nb_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600212{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530213 unsigned int nodeid;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600214
215 /* Find the nodeid */
216 nodeid = amdfam16_nodeid(dev);
217
218 create_vga_resource(dev, nodeid); //TODO: do we need this?
219
Michał Żygowskifb198c62021-05-09 13:54:09 +0200220 pci_dev_set_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600221}
222
223static void northbridge_init(struct device *dev)
224{
Kyösti Mälkki8f86fa02022-12-05 19:31:01 +0200225 setup_ioapic((u8 *)IO_APIC2_ADDR, GNB_IOAPIC_ID);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600226}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200227
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100228static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200229{
230 void *addr, *current;
231
232 /* Skip the HEST header. */
233 current = (void *)(hest + 1);
234
235 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
236 if (addr != NULL)
237 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
238
239 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
240 if (addr != NULL)
241 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
242
243 return (unsigned long)current;
244}
245
Michał Żygowski2f399b72020-04-02 19:51:37 +0200246unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500247{
Michał Żygowski2f399b72020-04-02 19:51:37 +0200248 /* 8-byte IVHD structures must be aligned to the 8-byte boundary. */
249 current = ALIGN_UP(current, 8);
250 ivrs_ivhd_special_t *ivhd_ioapic = (ivrs_ivhd_special_t *)current;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500251
Michał Żygowski2f399b72020-04-02 19:51:37 +0200252 ivhd_ioapic->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
253 ivhd_ioapic->reserved = 0x0000;
254 ivhd_ioapic->dte_setting = IVHD_DTE_LINT_1_PASS | IVHD_DTE_LINT_0_PASS |
255 IVHD_DTE_SYS_MGT_NO_TRANS | IVHD_DTE_NMI_PASS |
256 IVHD_DTE_EXT_INT_PASS | IVHD_DTE_INIT_PASS;
Kyösti Mälkki8f86fa02022-12-05 19:31:01 +0200257 ivhd_ioapic->handle = FCH_IOAPIC_ID;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200258 ivhd_ioapic->source_dev_id = PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC);
259 ivhd_ioapic->variety = IVHD_SPECIAL_DEV_IOAPIC;
260 current += sizeof(ivrs_ivhd_special_t);
261
262 ivhd_ioapic = (ivrs_ivhd_special_t *)current;
263
264 ivhd_ioapic->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
265 ivhd_ioapic->reserved = 0x0000;
266 ivhd_ioapic->dte_setting = 0x00;
Kyösti Mälkki8f86fa02022-12-05 19:31:01 +0200267 ivhd_ioapic->handle = GNB_IOAPIC_ID;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200268 ivhd_ioapic->source_dev_id = PCI_DEVFN(0, 1);
269 ivhd_ioapic->variety = IVHD_SPECIAL_DEV_IOAPIC;
270 current += sizeof(ivrs_ivhd_special_t);
271
272 return current;
273}
274
275static unsigned long ivhd_describe_hpet(unsigned long current)
276{
277 /* 8-byte IVHD structures must be aligned to the 8-byte boundary. */
278 current = ALIGN_UP(current, 8);
279 ivrs_ivhd_special_t *ivhd_hpet = (ivrs_ivhd_special_t *)current;
280
281 ivhd_hpet->type = IVHD_DEV_8_BYTE_EXT_SPECIAL_DEV;
282 ivhd_hpet->reserved = 0x0000;
283 ivhd_hpet->dte_setting = 0x00;
284 ivhd_hpet->handle = 0x00;
285 ivhd_hpet->source_dev_id = PCI_DEVFN(SMBUS_DEV, SMBUS_FUNC);
286 ivhd_hpet->variety = IVHD_SPECIAL_DEV_HPET;
287 current += sizeof(ivrs_ivhd_special_t);
288
289 return current;
290}
291
292static unsigned long ivhd_dev_range(unsigned long current, uint16_t start_devid,
293 uint16_t end_devid, uint8_t setting)
294{
295 /* 4-byte IVHD structures must be aligned to the 4-byte boundary. */
296 current = ALIGN_UP(current, 4);
297 ivrs_ivhd_generic_t *ivhd_range = (ivrs_ivhd_generic_t *)current;
298
299 /* Create the start range IVHD entry */
300 ivhd_range->type = IVHD_DEV_4_BYTE_START_RANGE;
301 ivhd_range->dev_id = start_devid;
302 ivhd_range->dte_setting = setting;
303 current += sizeof(ivrs_ivhd_generic_t);
304
305 /* Create the end range IVHD entry */
306 ivhd_range = (ivrs_ivhd_generic_t *)current;
307 ivhd_range->type = IVHD_DEV_4_BYTE_END_RANGE;
308 ivhd_range->dev_id = end_devid;
309 ivhd_range->dte_setting = setting;
310 current += sizeof(ivrs_ivhd_generic_t);
311
312 return current;
313}
314
315static unsigned long add_ivhd_dev_entry(struct device *parent, struct device *dev,
316 unsigned long *current, uint8_t type, uint8_t data)
317{
318 if (type == IVHD_DEV_4_BYTE_SELECT) {
319 /* 4-byte IVHD structures must be aligned to the 4-byte boundary. */
320 *current = ALIGN_UP(*current, 4);
321 ivrs_ivhd_generic_t *ivhd_entry = (ivrs_ivhd_generic_t *)*current;
322
323 ivhd_entry->type = type;
324 ivhd_entry->dev_id = dev->path.pci.devfn | (dev->bus->secondary << 8);
325 ivhd_entry->dte_setting = data;
326 *current += sizeof(ivrs_ivhd_generic_t);
327 } else if (type == IVHD_DEV_8_BYTE_ALIAS_SELECT) {
328 /* 8-byte IVHD structures must be aligned to the 8-byte boundary. */
329 *current = ALIGN_UP(*current, 8);
330 ivrs_ivhd_alias_t *ivhd_entry = (ivrs_ivhd_alias_t *)*current;
331
332 ivhd_entry->type = type;
333 ivhd_entry->dev_id = dev->path.pci.devfn | (dev->bus->secondary << 8);
334 ivhd_entry->dte_setting = data;
335 ivhd_entry->reserved1 = 0;
336 ivhd_entry->reserved2 = 0;
337 ivhd_entry->source_dev_id = parent->path.pci.devfn |
338 (parent->bus->secondary << 8);
339 *current += sizeof(ivrs_ivhd_alias_t);
340 }
341
342 return *current;
343}
344
345static void ivrs_add_device_or_bridge(struct device *parent, struct device *dev,
346 unsigned long *current, uint16_t *ivhd_length)
347{
348 unsigned int header_type, is_pcie;
349 unsigned long current_backup;
350
351 header_type = dev->hdr_type & 0x7f;
352 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
353
354 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
355 (header_type == PCI_HEADER_TYPE_BRIDGE)) && is_pcie) {
356 /* Device or Bridge is PCIe */
357 current_backup = *current;
358 add_ivhd_dev_entry(parent, dev, current, IVHD_DEV_4_BYTE_SELECT, 0x0);
359 *ivhd_length += (*current - current_backup);
360 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) && !is_pcie) {
361 /* Device is legacy PCI or PCI-X */
362 current_backup = *current;
363 add_ivhd_dev_entry(parent, dev, current, IVHD_DEV_8_BYTE_ALIAS_SELECT, 0x0);
364 *ivhd_length += (*current - current_backup);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500365 }
366}
367
Michał Żygowski2f399b72020-04-02 19:51:37 +0200368static void add_ivhd_device_entries(struct device *parent, struct device *dev,
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500369 unsigned int depth, int linknum, int8_t *root_level,
Michał Żygowski2f399b72020-04-02 19:51:37 +0200370 unsigned long *current, uint16_t *ivhd_length)
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500371{
372 struct device *sibling;
373 struct bus *link;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200374
375 if (!root_level) {
376 root_level = malloc(sizeof(int8_t));
377 *root_level = -1;
378 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500379
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500380 if (dev->path.type == DEVICE_PATH_PCI) {
381
382 if ((dev->bus->secondary == 0x0) &&
383 (dev->path.pci.devfn == 0x0))
384 *root_level = depth;
385
386 if ((*root_level != -1) && (dev->enabled)) {
Michał Żygowski2f399b72020-04-02 19:51:37 +0200387 if (depth != *root_level)
388 ivrs_add_device_or_bridge(parent, dev, current, ivhd_length);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500389 }
390 }
391
392 for (link = dev->link_list; link; link = link->next)
393 for (sibling = link->children; sibling; sibling =
394 sibling->sibling)
Michał Żygowski2f399b72020-04-02 19:51:37 +0200395 add_ivhd_device_entries(dev, sibling, depth + 1, depth, root_level,
396 current, ivhd_length);
397
398 free(root_level);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500399}
400
Michał Żygowski2f399b72020-04-02 19:51:37 +0200401#define IOMMU_MMIO32(x) (*((volatile uint32_t *)(x)))
402#define EFR_SUPPORT BIT(27)
403
404static unsigned long acpi_fill_ivrs11(unsigned long current, acpi_ivrs_t *ivrs_agesa)
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500405{
Michał Żygowski2f399b72020-04-02 19:51:37 +0200406 acpi_ivrs_ivhd11_t *ivhd_11;
407 unsigned long current_backup;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500408
Michał Żygowski2f399b72020-04-02 19:51:37 +0200409 /*
410 * These devices should be already found by previous function.
411 * Do not perform NULL checks.
412 */
413 struct device *nb_dev = pcidev_on_root(0, 0);
414 struct device *iommu_dev = pcidev_on_root(0, 2);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500415
Michał Żygowski2f399b72020-04-02 19:51:37 +0200416 /*
417 * In order to utilize all features, firmware should expose type 11h
418 * IVHD which supersedes the type 10h.
419 */
420 memset((void *)current, 0, sizeof(acpi_ivrs_ivhd11_t));
421 ivhd_11 = (acpi_ivrs_ivhd11_t *)current;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500422
Michał Żygowski2f399b72020-04-02 19:51:37 +0200423 /* Enable EFR */
424 ivhd_11->type = IVHD_BLOCK_TYPE_FULL__FIXED;
425 /* For type 11h bits 6 and 7 are reserved */
426 ivhd_11->flags = ivrs_agesa->ivhd.flags & 0x3f;
427 ivhd_11->length = sizeof(struct acpi_ivrs_ivhd_11);
428 /* BDF <bus>:00.2 */
429 ivhd_11->device_id = 0x02 | (nb_dev->bus->secondary << 8);
430 /* PCI Capability block 0x40 (type 0xf, "Secure device") */
431 ivhd_11->capability_offset = 0x40;
432 ivhd_11->iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
433 ivhd_11->iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
434 ivhd_11->pci_segment_group = 0x0000;
435 ivhd_11->iommu_info = ivrs_agesa->ivhd.iommu_info;
436 ivhd_11->iommu_attributes.perf_counters =
437 (IOMMU_MMIO32(ivhd_11->iommu_base_low + 0x4000) >> 7) & 0xf;
438 ivhd_11->iommu_attributes.perf_counter_banks =
439 (IOMMU_MMIO32(ivhd_11->iommu_base_low + 0x4000) >> 12) & 0x3f;
440 ivhd_11->iommu_attributes.msi_num_ppr =
441 (pci_read_config32(iommu_dev, ivhd_11->capability_offset + 0x10) >> 27) & 0x1f;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500442
Michał Żygowski2f399b72020-04-02 19:51:37 +0200443 if (pci_read_config32(iommu_dev, ivhd_11->capability_offset) & EFR_SUPPORT) {
444 ivhd_11->efr_reg_image_low = IOMMU_MMIO32(ivhd_11->iommu_base_low + 0x30);
445 ivhd_11->efr_reg_image_high = IOMMU_MMIO32(ivhd_11->iommu_base_low + 0x34);
446 }
447
448 current += sizeof(acpi_ivrs_ivhd11_t);
449
450 /* Now repeat all the device entries from type 10h */
451 current_backup = current;
452 current = ivhd_dev_range(current, PCI_DEVFN(1, 0), PCI_DEVFN(0x1f, 6), 0);
453 ivhd_11->length += (current - current_backup);
454 add_ivhd_device_entries(NULL, all_devices, 0, -1, NULL, &current, &ivhd_11->length);
455
456 /* Describe HPET */
457 current_backup = current;
458 current = ivhd_describe_hpet(current);
459 ivhd_11->length += (current - current_backup);
460
461 /* Describe IOAPICs */
462 current_backup = current;
463 current = acpi_fill_ivrs_ioapic(ivrs_agesa, current);
464 ivhd_11->length += (current - current_backup);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500465
466 return current;
467}
468
469static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
470{
Piotr Król063e1562018-07-22 20:52:26 +0200471 acpi_ivrs_t *ivrs_agesa;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200472 unsigned long current_backup;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500473
Michał Żygowski2f399b72020-04-02 19:51:37 +0200474 struct device *nb_dev = pcidev_on_root(0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500475 if (!nb_dev) {
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500476 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
477 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
478
479 return (unsigned long)ivrs;
480 }
481
Michał Żygowski2f399b72020-04-02 19:51:37 +0200482 struct device *iommu_dev = pcidev_on_root(0, 2);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500483
Michał Żygowski2f399b72020-04-02 19:51:37 +0200484 if (!iommu_dev) {
485 printk(BIOS_WARNING, "%s: IOMMU device not found\n", __func__);
486
487 return (unsigned long)ivrs;
488 }
489
Piotr Król063e1562018-07-22 20:52:26 +0200490 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
491 if (ivrs_agesa != NULL) {
Michał Żygowski2f399b72020-04-02 19:51:37 +0200492 ivrs->iv_info = ivrs_agesa->iv_info;
493 ivrs->ivhd.type = IVHD_BLOCK_TYPE_LEGACY__FIXED;
494 ivrs->ivhd.flags = ivrs_agesa->ivhd.flags;
Piotr Król063e1562018-07-22 20:52:26 +0200495 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
496 /* BDF <bus>:00.2 */
Michał Żygowski2f399b72020-04-02 19:51:37 +0200497 ivrs->ivhd.device_id = 0x02 | (nb_dev->bus->secondary << 8);
498 /* PCI Capability block 0x40 (type 0xf, "Secure device") */
Piotr Król063e1562018-07-22 20:52:26 +0200499 ivrs->ivhd.capability_offset = 0x40;
500 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
501 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
Michał Żygowski2f399b72020-04-02 19:51:37 +0200502 ivrs->ivhd.pci_segment_group = 0x0000;
503 ivrs->ivhd.iommu_info = ivrs_agesa->ivhd.iommu_info;
504 ivrs->ivhd.iommu_feature_info = ivrs_agesa->ivhd.iommu_feature_info;
505 /* Enable EFR if supported */
506 if (pci_read_config32(iommu_dev, ivrs->ivhd.capability_offset) & EFR_SUPPORT)
507 ivrs->iv_info |= IVINFO_EFR_SUPPORTED;
Piotr Król063e1562018-07-22 20:52:26 +0200508 } else {
509 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
510
511 return (unsigned long)ivrs;
512 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500513
Michał Żygowski2f399b72020-04-02 19:51:37 +0200514 /*
515 * Add all possible PCI devices on bus 0 that can generate transactions
516 * processed by IOMMU. Start with device 00:01.0 since IOMMU does not
517 * translate transactions generated by itself.
518 */
519 current_backup = current;
520 current = ivhd_dev_range(current, PCI_DEVFN(1, 0), PCI_DEVFN(0x1f, 6), 0);
521 ivrs->ivhd.length += (current - current_backup);
522 add_ivhd_device_entries(NULL, all_devices, 0, -1, NULL, &current, &ivrs->ivhd.length);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500523
Michał Żygowski2f399b72020-04-02 19:51:37 +0200524 /* Describe HPET */
525 current_backup = current;
526 current = ivhd_describe_hpet(current);
527 ivrs->ivhd.length += (current - current_backup);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500528
529 /* Describe IOAPICs */
Michał Żygowski2f399b72020-04-02 19:51:37 +0200530 current_backup = current;
531 current = acpi_fill_ivrs_ioapic(ivrs_agesa, current);
532 ivrs->ivhd.length += (current - current_backup);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500533
Michał Żygowski2f399b72020-04-02 19:51:37 +0200534 /* If EFR is not supported, IVHD type 11h is reserved */
535 if (!(ivrs->iv_info & IVINFO_EFR_SUPPORTED))
536 return current;
537
538 return acpi_fill_ivrs11(current, ivrs_agesa);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500539}
540
Furquan Shaikh7536a392020-04-24 21:59:21 -0700541static void northbridge_fill_ssdt_generator(const struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200542{
543 msr_t msr;
544 char pscope[] = "\\_SB.PCI0";
545
546 acpigen_write_scope(pscope);
547 msr = rdmsr(TOP_MEM);
548 acpigen_write_name_dword("TOM1", msr.lo);
549 msr = rdmsr(TOP_MEM2);
550 /*
551 * Since XP only implements parts of ACPI 2.0, we can't use a qword
552 * here.
553 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
554 * slide 22ff.
555 * Shift value right by 20 bit to make it fit into 32bit,
556 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
557 */
558 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
559 acpigen_pop_len();
560}
561
Michał Żygowski9550e972020-03-20 13:56:46 +0100562static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
563{
564 unsigned int len = ssdt->length - sizeof(acpi_header_t);
565 unsigned int i;
566
567 for (i = sizeof(acpi_header_t); i < len; i++) {
568 /* Search for _PR_ scope and replace it with _SB_ */
569 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
570 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
571 }
572 /* Recalculate checksum */
573 ssdt->checksum = 0;
574 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
575}
576
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700577static unsigned long agesa_write_acpi_tables(const struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200578 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200579 acpi_rsdp_t *rsdp)
580{
581 acpi_srat_t *srat;
582 acpi_slit_t *slit;
583 acpi_header_t *ssdt;
584 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500585 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200586
587 /* HEST */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200588 current = ALIGN_UP(current, 8);
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100589 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200590 acpi_add_table(rsdp, (void *)current);
591 current += ((acpi_header_t *)current)->length;
592
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500593 /* IVRS */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200594 current = ALIGN_UP(current, 8);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500595 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200596 ivrs = (acpi_ivrs_t *)current;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500597 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
598 current += ivrs->header.length;
599 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200600
601 /* SRAT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200602 current = ALIGN_UP(current, 8);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200603 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200604 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200605 if (srat != NULL) {
606 memcpy((void *)current, srat, srat->header.length);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200607 srat = (acpi_srat_t *)current;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200608 current += srat->header.length;
609 acpi_add_table(rsdp, srat);
610 } else {
611 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
612 }
613
614 /* SLIT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200615 current = ALIGN_UP(current, 8);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200616 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200617 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200618 if (slit != NULL) {
619 memcpy((void *)current, slit, slit->header.length);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200620 slit = (acpi_slit_t *)current;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200621 current += slit->header.length;
622 acpi_add_table(rsdp, slit);
623 } else {
624 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
625 }
626
627 /* ALIB */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200628 current = ALIGN_UP(current, 16);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200629 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200630 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200631 if (alib != NULL) {
632 memcpy((void *)current, alib, alib->length);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200633 alib = (acpi_header_t *)current;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200634 current += alib->length;
635 acpi_add_table(rsdp, (void *)alib);
636 }
637 else {
638 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
639 }
640
641 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
642 /* SSDT */
Elyes Haouasd6b6b222022-10-10 12:34:21 +0200643 current = ALIGN_UP(current, 16);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200644 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200645 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200646 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100647 patch_ssdt_processor_scope(ssdt);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200648 memcpy((void *)current, ssdt, ssdt->length);
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200649 ssdt = (acpi_header_t *)current;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200650 current += ssdt->length;
651 }
652 else {
653 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
654 }
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200655 acpi_add_table(rsdp, ssdt);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200656
657 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
658 return current;
659}
660
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600661static struct device_operations northbridge_operations = {
Michał Żygowskifb198c62021-05-09 13:54:09 +0200662 .read_resources = nb_read_resources,
663 .set_resources = nb_set_resources,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600664 .enable_resources = pci_dev_enable_resources,
665 .init = northbridge_init,
Michał Żygowskifb198c62021-05-09 13:54:09 +0200666 .ops_pci = &pci_dev_ops_pci,
Nico Huber68680dd2020-03-31 17:34:52 +0200667 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200668 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600669};
670
671static const struct pci_driver family16_northbridge __pci_driver = {
672 .ops = &northbridge_operations,
Felix Singer43b7f412022-03-07 04:34:52 +0100673 .vendor = PCI_VID_AMD,
674 .device = PCI_DID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600675};
676
677static const struct pci_driver family10_northbridge __pci_driver = {
678 .ops = &northbridge_operations,
Felix Singer43b7f412022-03-07 04:34:52 +0100679 .vendor = PCI_VID_AMD,
680 .device = PCI_DID_AMD_10H_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600681};
682
Dave Frodin891f71a2015-01-19 15:58:24 -0700683static void fam16_finalize(void *chip_info)
684{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300685 struct device *dev;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300686 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Elyes Haouasa1f5ad02022-02-17 18:14:08 +0100687
Dave Frodin891f71a2015-01-19 15:58:24 -0700688 pci_write_config32(dev, 0xF8, 0);
689 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
690
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200691 /*
692 * Currently it is impossible to enable ACS with AGESA by setting the
693 * correct bit for AmdInitMid phase. AGESA code path does not call the
694 * right function that enables these functionalities. Disabled ACS
695 * result in multiple PCIe devices to be assigned to the same IOMMU
696 * group. Without IOMMU group separation the devices cannot be passed
697 * through independently.
698 */
699
700 /* Select GPP link core IO Link Strap Control register 0xB0 */
701 pci_write_config32(dev, 0xE0, 0x014000B0);
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200702
703 /* Enable AER (bit 5) and ACS (bit 6 undocumented) */
Elyes Haouasa1f5ad02022-02-17 18:14:08 +0100704 pci_or_config32(dev, 0xE4, PCIE_CAP_AER | PCIE_CAP_ACS);
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200705
706 /* Select GPP link core Wrapper register 0x00 (undocumented) */
707 pci_write_config32(dev, 0xE0, 0x01300000);
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200708
709 /*
710 * Enable ACS capabilities straps including sub-items. From lspci it
711 * looks like these bits enable: Source Validation and Translation
712 * Blocking
713 */
Elyes Haouasa1f5ad02022-02-17 18:14:08 +0100714 pci_or_config32(dev, 0xE4, (BIT(24) | BIT(25) | BIT(26)));
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200715
Dave Frodin891f71a2015-01-19 15:58:24 -0700716 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300717 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200718 if (dev != NULL) {
Elyes Haouasa1f5ad02022-02-17 18:14:08 +0100719 pci_and_config32(dev, 0x60, ~(1 << 11));
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200720 }
Dave Frodin891f71a2015-01-19 15:58:24 -0700721}
722
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300723struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600724 CHIP_NAME("AMD FAM16 Northbridge")
725 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700726 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600727};
728
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600729#if CONFIG_HW_MEM_HOLE_SIZEK != 0
730struct hw_mem_hole_info {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530731 unsigned int hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600732 int node_id;
733};
734static struct hw_mem_hole_info get_hw_mem_hole_info(void)
735{
736 struct hw_mem_hole_info mem_hole;
737 int i;
738 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
739 mem_hole.node_id = -1;
Michał Kopećca1e8aa2021-12-03 15:17:46 +0100740 for (i = 0; i < get_node_nums(); i++) {
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200741 resource_t basek, limitk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600742 u32 hole;
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200743 if (!get_dram_base_limit(i, &basek, &limitk))
744 continue; // no memory on this node
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600745 hole = pci_read_config32(__f1_dev[i], 0xf0);
746 if (hole & 2) { // we find the hole
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200747 mem_hole.hole_startk = (hole & (0xff << 24)) >> 10;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600748 mem_hole.node_id = i; // record the node No with hole
749 break; // only one hole
750 }
751 }
752
753 /* We need to double check if there is special set on base reg and limit reg
754 * are not continuous instead of hole, it will find out its hole_startk.
755 */
756 if (mem_hole.node_id == -1) {
757 resource_t limitk_pri = 0;
Michał Kopećca1e8aa2021-12-03 15:17:46 +0100758 for (i = 0; i < get_node_nums(); i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600759 resource_t base_k, limit_k;
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200760 if (!get_dram_base_limit(i, &base_k, &limit_k))
761 continue; // no memory on this node
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200762 if (base_k > 4 * 1024 * 1024) break; // don't need to go to check
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600763 if (limitk_pri != base_k) { // we find the hole
Elyes HAOUAS38a4f2a92020-01-07 19:53:36 +0100764 mem_hole.hole_startk = (unsigned int)limitk_pri; // must be below 4G
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600765 mem_hole.node_id = i;
766 break; //only one hole
767 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600768 limitk_pri = limit_k;
769 }
770 }
771 return mem_hole;
772}
773#endif
774
Michał Żygowskif5d457d2021-05-09 13:58:04 +0200775static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600776{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600777 unsigned long mmio_basek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600778 int i, idx;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600779#if CONFIG_HW_MEM_HOLE_SIZEK != 0
780 struct hw_mem_hole_info mem_hole;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600781#endif
782
Michał Żygowskif5d457d2021-05-09 13:58:04 +0200783 pci_domain_read_resources(dev);
784
Michał Żygowski58d6f962021-05-05 10:52:08 +0200785 /* TOP_MEM MSR is our boundary between DRAM and MMIO under 4G */
Arthur Heymansc4350382021-10-28 12:35:39 +0200786 mmio_basek = amd_topmem() >> 10;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600787
788#if CONFIG_HW_MEM_HOLE_SIZEK != 0
789 /* if the hw mem hole is already set in raminit stage, here we will compare
790 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
791 * use hole_basek as mmio_basek and we don't need to reset hole.
792 * otherwise We reset the hole to the mmio_basek
793 */
794
795 mem_hole = get_hw_mem_hole_info();
796
797 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
798 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
799 mmio_basek = mem_hole.hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600800 }
801#endif
802
803 idx = 0x10;
Michał Kopećca1e8aa2021-12-03 15:17:46 +0100804 for (i = 0; i < get_node_nums(); i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600805 resource_t basek, limitk, sizek; // 4 1T
806
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200807 if (!get_dram_base_limit(i, &basek, &limitk))
808 continue; // no memory on this node
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600809
810 sizek = limitk - basek;
811
Michał Żygowski58d6f962021-05-05 10:52:08 +0200812 printk(BIOS_DEBUG, "node %d: basek=%08llx, limitk=%08llx, sizek=%08llx,\n",
813 i, basek, limitk, sizek);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600814
Elyes Haouas5213b192022-02-25 18:13:03 +0100815 /* See if we need a hole from 0xa0000 (640K) to 0xfffff (1024K) */
Elyes Haouas9d8df302022-02-25 18:23:01 +0100816 if (basek < 640 && sizek > 1024) {
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300817 ram_resource_kb(dev, (idx | i), basek, 640 - basek);
Michał Żygowski58d6f962021-05-05 10:52:08 +0200818 idx += 0x10;
Elyes Haouas9d8df302022-02-25 18:23:01 +0100819 basek = 1024;
Michał Żygowski58d6f962021-05-05 10:52:08 +0200820 sizek = limitk - basek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600821 }
822
Michał Żygowski58d6f962021-05-05 10:52:08 +0200823 printk(BIOS_DEBUG, "node %d: basek=%08llx, limitk=%08llx, sizek=%08llx,\n",
824 i, basek, limitk, sizek);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600825
826 /* split the region to accommodate pci memory space */
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200827 if ((basek < 4 * 1024 * 1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600828 if (basek <= mmio_basek) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530829 unsigned int pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600830 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600831 if (pre_sizek > 0) {
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300832 ram_resource_kb(dev, (idx | i), basek, pre_sizek);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600833 idx += 0x10;
834 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600835 }
836 basek = mmio_basek;
837 }
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200838 if ((basek + sizek) <= 4 * 1024 * 1024) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600839 sizek = 0;
840 }
841 else {
Arthur Heymansc4350382021-10-28 12:35:39 +0200842 uint64_t topmem2 = amd_topmem2();
Elyes Haouasf9b535e2022-07-16 09:47:42 +0200843 basek = 4 * 1024 * 1024;
844 sizek = topmem2 / 1024 - basek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600845 }
846 }
847
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300848 ram_resource_kb(dev, (idx | i), basek, sizek);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600849 idx += 0x10;
850 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
851 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600852 }
853
Kyösti Mälkkie87564f2017-04-15 20:07:53 +0300854 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600855}
856
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600857static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100858{
859 if (dev->path.type == DEVICE_PATH_DOMAIN)
860 return "PCI0";
861
862 return NULL;
863}
864
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600865static struct device_operations pci_domain_ops = {
866 .read_resources = domain_read_resources,
Michał Żygowskif5d457d2021-05-09 13:58:04 +0200867 .set_resources = pci_domain_set_resources,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600868 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100869 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600870};
871
Michał Kopećdc35d2a2021-11-30 17:40:52 +0100872static void pre_mp_init(void)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600873{
Michał Kopećdc35d2a2021-11-30 17:40:52 +0100874 x86_setup_mtrrs_with_detect();
875 x86_mtrr_check();
876}
877
Michał Kopećdc35d2a2021-11-30 17:40:52 +0100878static const struct mp_ops mp_ops = {
879 .pre_mp_init = pre_mp_init,
880 .get_cpu_count = get_cpu_count,
881};
882
883void mp_init_cpus(struct bus *cpu_bus)
884{
885 /* TODO: Handle mp_init_with_smm failure? */
886 mp_init_with_smm(cpu_bus, &mp_ops);
887
888 /* The flash is now no longer cacheable. Reset to WP for performance. */
889 mtrr_use_temp_range(OPTIMAL_CACHE_ROM_BASE, OPTIMAL_CACHE_ROM_SIZE,
890 MTRR_TYPE_WRPROT);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600891}
892
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600893static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200894 .read_resources = noop_read_resources,
895 .set_resources = noop_set_resources,
Michał Kopećdc35d2a2021-11-30 17:40:52 +0100896 .init = mp_cpu_bus_init,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600897};
898
899static void root_complex_enable_dev(struct device *dev)
900{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600901 /* Set the operations if it is a special bus type */
902 if (dev->path.type == DEVICE_PATH_DOMAIN) {
903 dev->ops = &pci_domain_ops;
904 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
905 dev->ops = &cpu_bus_ops;
906 }
907}
908
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300909struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600910 CHIP_NAME("AMD FAM16 Root Complex")
911 .enable_dev = root_complex_enable_dev,
912};
913
914/*********************************************************************
915 * Change the vendor / device IDs to match the generic VBIOS header. *
916 *********************************************************************/
917u32 map_oprom_vendev(u32 vendev)
918{
919 u32 new_vendev;
920 new_vendev =
921 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
922
923 if (vendev != new_vendev)
924 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
925
926 return new_vendev;
927}