blob: afe7cf042045a2158964be4e2979d0195d2eb3f6 [file] [log] [blame]
Marc Jones1587dc82017-05-15 18:55:11 -06001/*
2 * This file is part of the coreboot project.
3 *
Marc Jones1587dc82017-05-15 18:55:11 -06004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
Michał Żygowskif65c1e42019-12-01 18:14:39 +010015#include <amdblocks/biosram.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020016#include <device/pci_ops.h>
Marc Jonesd6a82002018-03-31 22:46:57 -060017#include <arch/ioapic.h>
Marc Jones1587dc82017-05-15 18:55:11 -060018#include <arch/acpi.h>
19#include <arch/acpigen.h>
20#include <cbmem.h>
Marc Jones1587dc82017-05-15 18:55:11 -060021#include <console/console.h>
Marc Jones1587dc82017-05-15 18:55:11 -060022#include <cpu/amd/mtrr.h>
Marshall Dawson154239a2017-11-02 09:49:30 -060023#include <cpu/x86/lapic_def.h>
Marshall Dawsonf82aa102017-09-20 18:01:41 -060024#include <cpu/x86/msr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020025#include <cpu/amd/msr.h>
Marc Jones1587dc82017-05-15 18:55:11 -060026#include <device/device.h>
27#include <device/pci.h>
28#include <device/pci_ids.h>
Marshall Dawson8f2a7e02017-11-01 11:44:48 -060029#include <romstage_handoff.h>
Richard Spiegel0ad74ac2017-12-08 16:53:29 -070030#include <amdblocks/agesawrapper.h>
31#include <amdblocks/agesawrapper_call.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070032#include <agesa_headers.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060033#include <soc/cpu.h>
Marc Jones1587dc82017-05-15 18:55:11 -060034#include <soc/northbridge.h>
Marshall Dawson38bded02017-09-01 09:54:48 -060035#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070036#include <soc/iomap.h>
Marc Jones1587dc82017-05-15 18:55:11 -060037#include <stdint.h>
Marc Jones1587dc82017-05-15 18:55:11 -060038#include <string.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060039#include <arch/bert_storage.h>
Marc Jones1587dc82017-05-15 18:55:11 -060040
Elyes HAOUASc3385072019-03-21 15:38:06 +010041#include "chip.h"
42
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020043static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Marc Jones1587dc82017-05-15 18:55:11 -060044 u32 io_min, u32 io_max)
45{
46 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060047
Marshall Dawson4e101ad2017-06-15 12:17:38 -060048 /* io range allocation. Limit */
49 tempreg = (nodeid & 0xf) | ((nodeid & 0x30) << (8 - 4)) | (linkn << 4)
50 | ((io_max & 0xf0) << (12 - 4));
Richard Spiegel41baf0c2018-10-22 13:57:18 -070051 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060052 tempreg = 3 | ((io_min & 0xf0) << (12 - 4)); /* base: ISA and VGA ? */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070053 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060054}
55
Marshall Dawson4e101ad2017-06-15 12:17:38 -060056static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index,
57 u32 mmio_min, u32 mmio_max)
Marc Jones1587dc82017-05-15 18:55:11 -060058{
59 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060060
Marshall Dawson4e101ad2017-06-15 12:17:38 -060061 /* io range allocation. Limit */
62 tempreg = (nodeid & 0xf) | (linkn << 4) | (mmio_max & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070063 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060064 tempreg = 3 | (nodeid & 0x30) | (mmio_min & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070065 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060066}
67
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020068static void read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060069{
Marc Jonesd6a82002018-03-31 22:46:57 -060070 struct resource *res;
71
Marc Jones1587dc82017-05-15 18:55:11 -060072 /*
73 * This MMCONF resource must be reserved in the PCI domain.
74 * It is not honored by the coreboot resource allocator if it is in
75 * the CPU_CLUSTER.
76 */
Aaron Durbin3173d442017-11-03 12:14:25 -060077 mmconf_resource(dev, MMIO_CONF_BASE);
Marc Jonesd6a82002018-03-31 22:46:57 -060078
79 /* NB IOAPIC2 resource */
80 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
81 res->base = IO_APIC2_ADDR;
82 res->size = 0x00001000;
83 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Marc Jones1587dc82017-05-15 18:55:11 -060084}
85
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070086static void set_resource(struct device *dev, struct resource *res, u32 nodeid)
Marc Jones1587dc82017-05-15 18:55:11 -060087{
88 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060089 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -060090 char buf[50];
91
92 /* Make certain the resource has actually been set */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070093 if (!(res->flags & IORESOURCE_ASSIGNED))
Marc Jones1587dc82017-05-15 18:55:11 -060094 return;
95
96 /* If I have already stored this resource don't worry about it */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070097 if (res->flags & IORESOURCE_STORED)
Marc Jones1587dc82017-05-15 18:55:11 -060098 return;
99
100 /* Only handle PCI memory and IO resources */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700101 if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Marc Jones1587dc82017-05-15 18:55:11 -0600102 return;
103
104 /* Ensure I am actually looking at a resource of function 1 */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700105 if ((res->index & 0xffff) < 0x1000)
Marc Jones1587dc82017-05-15 18:55:11 -0600106 return;
107
108 /* Get the base address */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700109 rbase = res->base;
Marc Jones1587dc82017-05-15 18:55:11 -0600110
111 /* Get the limit (rounded up) */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700112 rend = resource_end(res);
Marc Jones1587dc82017-05-15 18:55:11 -0600113
114 /* Get the register and link */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700115 reg = res->index & 0xfff; /* 4k */
116 link_num = IOINDEX_LINK(res->index);
Marc Jones1587dc82017-05-15 18:55:11 -0600117
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700118 if (res->flags & IORESOURCE_IO)
Marc Jones1587dc82017-05-15 18:55:11 -0600119 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700120 else if (res->flags & IORESOURCE_MEM)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600121 set_mmio_addr_reg(nodeid, link_num, reg,
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700122 (res->index >> 24), rbase >> 8, rend >> 8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600123
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700124 res->flags |= IORESOURCE_STORED;
Marc Jones1587dc82017-05-15 18:55:11 -0600125 snprintf(buf, sizeof(buf), " <node %x link %x>",
126 nodeid, link_num);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700127 report_resource_stored(dev, res, buf);
Marc Jones1587dc82017-05-15 18:55:11 -0600128}
129
130/**
131 * I tried to reuse the resource allocation code in set_resource()
132 * but it is too difficult to deal with the resource allocation magic.
133 */
134
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200135static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600136{
137 struct bus *link;
138
139 /* find out which link the VGA card is connected,
140 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600141 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600142 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
143 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600144
145 /* no VGA card installed */
146 if (link == NULL)
147 return;
148
Marshall Dawsone2697de2017-09-06 10:46:36 -0600149 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600150 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700151 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
Marc Jones1587dc82017-05-15 18:55:11 -0600152}
153
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200154static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600155{
156 struct bus *bus;
157 struct resource *res;
158
159
160 /* do we need this? */
161 create_vga_resource(dev);
162
163 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600164 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600165 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600166
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600167 for (bus = dev->link_list ; bus ; bus = bus->next)
168 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600169 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600170}
171
172static void northbridge_init(struct device *dev)
173{
Marc Jonesd6a82002018-03-31 22:46:57 -0600174 setup_ioapic((u8 *)IO_APIC2_ADDR, CONFIG_MAX_CPUS+1);
Marc Jones1587dc82017-05-15 18:55:11 -0600175}
176
Marshall Dawsone09caf62019-05-02 17:58:12 -0600177unsigned long acpi_fill_mcfg(unsigned long current)
178{
179
180 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
181 CONFIG_MMCONF_BASE_ADDRESS,
182 0,
183 0,
184 CONFIG_MMCONF_BUS_NUMBER);
185
186 return current;
187}
188
Marc Jones1587dc82017-05-15 18:55:11 -0600189static unsigned long acpi_fill_hest(acpi_hest_t *hest)
190{
191 void *addr, *current;
192
193 /* Skip the HEST header. */
194 current = (void *)(hest + 1);
195
196 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
197 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600198 current += acpi_create_hest_error_source(hest, current, 0,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700199 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600200
201 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
202 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600203 current += acpi_create_hest_error_source(hest, current, 1,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700204 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600205
206 return (unsigned long)current;
207}
208
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200209static void northbridge_fill_ssdt_generator(struct device *device)
Marc Jones1587dc82017-05-15 18:55:11 -0600210{
211 msr_t msr;
212 char pscope[] = "\\_SB.PCI0";
213
214 acpigen_write_scope(pscope);
215 msr = rdmsr(TOP_MEM);
216 acpigen_write_name_dword("TOM1", msr.lo);
217 msr = rdmsr(TOP_MEM2);
218 /*
219 * Since XP only implements parts of ACPI 2.0, we can't use a qword
220 * here.
221 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
222 * slide 22ff.
223 * Shift value right by 20 bit to make it fit into 32bit,
224 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
225 */
226 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
227 acpigen_pop_len();
228}
229
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200230static unsigned long agesa_write_acpi_tables(struct device *device,
Marc Jones1587dc82017-05-15 18:55:11 -0600231 unsigned long current,
232 acpi_rsdp_t *rsdp)
233{
234 acpi_srat_t *srat;
235 acpi_slit_t *slit;
236 acpi_header_t *ssdt;
237 acpi_header_t *alib;
238 acpi_header_t *ivrs;
239 acpi_hest_t *hest;
Marshall Dawson653f7602018-09-04 13:25:39 -0600240 acpi_bert_t *bert;
Marc Jones1587dc82017-05-15 18:55:11 -0600241
242 /* HEST */
243 current = ALIGN(current, 8);
244 hest = (acpi_hest_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700245 acpi_write_hest(hest, acpi_fill_hest);
Marc Jones1587dc82017-05-15 18:55:11 -0600246 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700247 current += hest->header.length;
Marc Jones1587dc82017-05-15 18:55:11 -0600248
Marshall Dawson653f7602018-09-04 13:25:39 -0600249 /* BERT */
Julius Wernercd49cce2019-03-05 16:53:33 -0800250 if (CONFIG(ACPI_BERT) && bert_errors_present()) {
Marshall Dawson653f7602018-09-04 13:25:39 -0600251 /* Skip the table if no errors are present. ACPI driver reports
252 * a table with a 0-length region:
253 * BERT: [Firmware Bug]: table invalid.
254 */
255 void *rgn;
256 size_t size;
257 bert_errors_region(&rgn, &size);
258 if (!rgn) {
259 printk(BIOS_ERR, "Error: Can't find BERT storage area\n");
260 } else {
261 current = ALIGN(current, 8);
262 bert = (acpi_bert_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700263 acpi_write_bert(bert, (uintptr_t)rgn, size);
Marshall Dawson653f7602018-09-04 13:25:39 -0600264 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700265 current += bert->header.length;
Marshall Dawson653f7602018-09-04 13:25:39 -0600266 }
267 }
268
Marc Jones1587dc82017-05-15 18:55:11 -0600269 current = ALIGN(current, 8);
270 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
271 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
272 if (ivrs != NULL) {
273 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600274 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600275 current += ivrs->length;
276 acpi_add_table(rsdp, ivrs);
277 } else {
278 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
279 }
280
281 /* SRAT */
282 current = ALIGN(current, 8);
283 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600284 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600285 if (srat != NULL) {
286 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600287 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600288 current += srat->header.length;
289 acpi_add_table(rsdp, srat);
290 } else {
291 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
292 }
293
294 /* SLIT */
295 current = ALIGN(current, 8);
296 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600297 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600298 if (slit != NULL) {
299 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600300 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600301 current += slit->header.length;
302 acpi_add_table(rsdp, slit);
303 } else {
304 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
305 }
306
307 /* ALIB */
308 current = ALIGN(current, 16);
309 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600310 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600311 if (alib != NULL) {
312 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600313 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600314 current += alib->length;
315 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600316 } else {
317 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
318 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600319 }
320
Marc Jones1587dc82017-05-15 18:55:11 -0600321 current = ALIGN(current, 16);
322 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600323 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Marc Jones1587dc82017-05-15 18:55:11 -0600324 if (ssdt != NULL) {
325 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600326 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600327 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600328 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600329 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
330 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600331 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600332
333 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
334 return current;
335}
336
337static struct device_operations northbridge_operations = {
338 .read_resources = read_resources,
339 .set_resources = set_resources,
340 .enable_resources = pci_dev_enable_resources,
341 .init = northbridge_init,
342 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
343 .write_acpi_tables = agesa_write_acpi_tables,
344 .enable = 0,
345 .ops_pci = 0,
346};
347
Richard Spiegel9247e862019-06-28 09:18:47 -0700348static const unsigned short pci_device_ids[] = {
349 PCI_DEVICE_ID_AMD_15H_MODEL_606F_NB_HT,
350 PCI_DEVICE_ID_AMD_15H_MODEL_707F_NB_HT,
351 0 };
352
Marc Jones1587dc82017-05-15 18:55:11 -0600353static const struct pci_driver family15_northbridge __pci_driver = {
354 .ops = &northbridge_operations,
355 .vendor = PCI_VENDOR_ID_AMD,
Richard Spiegel9247e862019-06-28 09:18:47 -0700356 .devices = pci_device_ids,
Marc Jones1587dc82017-05-15 18:55:11 -0600357};
358
Marshall Dawson154239a2017-11-02 09:49:30 -0600359/*
360 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
361 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
362 * MMIO to posted. Route all I/O to the southbridge.
363 */
364void amd_initcpuio(void)
365{
366 uintptr_t topmem = bsp_topmem();
367 uintptr_t base, limit;
368
369 /* Enable legacy video routing: D18F1xF4 VGA Enable */
370 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
371
372 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
373 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
374 limit = (ALIGN_DOWN(LOCAL_APIC_ADDR - 1, 64 * KiB) >> 8) | MMIO_NP;
375 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
376 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
377
378 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
379 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
380 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
381 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
382 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
383
384 /* Route all I/O downstream */
385 base = 0 | IO_WE | IO_RE;
386 limit = ALIGN_DOWN(0xffff, 4 * KiB);
387 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
388 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
389}
390
Marc Jones1587dc82017-05-15 18:55:11 -0600391void fam15_finalize(void *chip_info)
392{
Marc Jones1587dc82017-05-15 18:55:11 -0600393 u32 value;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700394
395 /* TODO: move IOAPIC code to dsdt.asl */
396 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_INDEX, 0);
397 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_DATA, 5);
Marc Jones1587dc82017-05-15 18:55:11 -0600398
399 /* disable No Snoop */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700400 value = pci_read_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700401 value &= ~HDA_NO_SNOOP_EN;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700402 pci_write_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600403}
404
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200405void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600406{
Marc Jones1587dc82017-05-15 18:55:11 -0600407 /* Must be called after PCI enumeration and resource allocation */
Marshall Dawson8f2a7e02017-11-01 11:44:48 -0600408 if (!romstage_handoff_is_resume())
Kyösti Mälkki6e512c42018-06-14 06:57:05 +0300409 do_agesawrapper(AMD_INIT_MID, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600410}
411
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200412void domain_set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600413{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700414 uint64_t uma_base = get_uma_base();
415 uint32_t uma_size = get_uma_size();
416 uint32_t mem_useable = (uintptr_t)cbmem_top();
417 msr_t tom = rdmsr(TOP_MEM);
418 msr_t high_tom = rdmsr(TOP_MEM2);
419 uint64_t high_mem_useable;
420 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600421
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700422 /* 0x0 -> 0x9ffff */
423 ram_resource(dev, idx++, 0, 0xa0000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600424
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700425 /* 0xa0000 -> 0xbffff: legacy VGA */
426 mmio_resource(dev, idx++, 0xa0000 / KiB, 0x20000 / KiB);
427
428 /* 0xc0000 -> 0xfffff: Option ROM */
429 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, 0x40000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600430
Marshall Dawson29f1b742017-09-06 14:59:45 -0600431 /*
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700432 * 0x100000 (1MiB) -> low top useable RAM
433 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600434 */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700435 ram_resource(dev, idx++, (1 * MiB) / KiB,
436 (mem_useable - (1 * MiB)) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600437
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700438 /* Low top useable RAM -> Low top RAM (bottom pci mmio hole) */
439 reserved_ram_resource(dev, idx++, mem_useable / KiB,
440 (tom.lo - mem_useable) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600441
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700442 /* If there is memory above 4GiB */
443 if (high_tom.hi) {
444 /* 4GiB -> high top useable */
445 if (uma_base >= (4ull * GiB))
446 high_mem_useable = uma_base;
447 else
448 high_mem_useable = ((uint64_t)high_tom.lo |
449 ((uint64_t)high_tom.hi << 32));
Marc Jones1587dc82017-05-15 18:55:11 -0600450
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700451 ram_resource(dev, idx++, (4ull * GiB) / KiB,
452 ((high_mem_useable - (4ull * GiB)) / KiB));
Marc Jones1587dc82017-05-15 18:55:11 -0600453
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700454 /* High top useable RAM -> high top RAM */
455 if (uma_base >= (4ull * GiB)) {
456 reserved_ram_resource(dev, idx++, uma_base / KiB,
457 uma_size / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600458 }
Marc Jones1587dc82017-05-15 18:55:11 -0600459 }
460
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700461 assign_resources(dev->link_list);
Marc Jones1587dc82017-05-15 18:55:11 -0600462}
463
Marc Jones1587dc82017-05-15 18:55:11 -0600464/*********************************************************************
465 * Change the vendor / device IDs to match the generic VBIOS header. *
466 *********************************************************************/
467u32 map_oprom_vendev(u32 vendev)
468{
469 u32 new_vendev;
Richard Spiegel9247e862019-06-28 09:18:47 -0700470
471 if ((vendev >= 0x100298e0) && (vendev <= 0x100298ef))
472 new_vendev = 0x100298e0;
473 else if ((vendev >= 0x10029870) && (vendev <= 0x1002987f))
474 new_vendev = 0x10029870;
475 else
476 new_vendev = vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600477
478 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600479 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
480 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600481
482 return new_vendev;
483}
Marshall Dawson2942db62017-12-14 10:00:27 -0700484
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700485__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
486
Marshall Dawson2942db62017-12-14 10:00:27 -0700487void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
488{
Martin Roth50f2e4c2018-10-29 11:16:53 -0600489 const struct device *dev = SOC_IOMMU_DEV;
490 params->IommuSupport = dev && dev->enabled;
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700491 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700492}
493
494void SetNbMidParams(GNB_MID_CONFIGURATION *params)
495{
496 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
497 params->iGpuVgaMode = 0;
498 params->GnbIoapicAddress = IO_APIC2_ADDR;
499}