blob: 6389cc439c995ea4354f3878b695468539272b08 [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Marc Jones1587dc82017-05-15 18:55:11 -06002
Furquan Shaikh91a7abf2020-04-27 18:48:48 -07003#include <assert.h>
Michał Żygowskif65c1e42019-12-01 18:14:39 +01004#include <amdblocks/biosram.h>
Furquan Shaikh91a7abf2020-04-27 18:48:48 -07005#include <amdblocks/hda.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Felix Held4b2464f2022-02-23 17:54:20 +01007#include <arch/hpet.h>
Marc Jonesd6a82002018-03-31 22:46:57 -06008#include <arch/ioapic.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07009#include <acpi/acpi.h>
10#include <acpi/acpigen.h>
Marc Jones1587dc82017-05-15 18:55:11 -060011#include <cbmem.h>
Marc Jones1587dc82017-05-15 18:55:11 -060012#include <console/console.h>
Marc Jones1587dc82017-05-15 18:55:11 -060013#include <cpu/amd/mtrr.h>
Marshall Dawson154239a2017-11-02 09:49:30 -060014#include <cpu/x86/lapic_def.h>
Marshall Dawsonf82aa102017-09-20 18:01:41 -060015#include <cpu/x86/msr.h>
Marc Jones1587dc82017-05-15 18:55:11 -060016#include <device/device.h>
17#include <device/pci.h>
18#include <device/pci_ids.h>
Richard Spiegel0ad74ac2017-12-08 16:53:29 -070019#include <amdblocks/agesawrapper.h>
20#include <amdblocks/agesawrapper_call.h>
Felix Held604ffa62021-02-12 00:43:20 +010021#include <amdblocks/ioapic.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070022#include <agesa_headers.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060023#include <soc/cpu.h>
Marc Jones1587dc82017-05-15 18:55:11 -060024#include <soc/northbridge.h>
Marshall Dawson38bded02017-09-01 09:54:48 -060025#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070026#include <soc/iomap.h>
Marc Jones1587dc82017-05-15 18:55:11 -060027#include <stdint.h>
Marc Jones1587dc82017-05-15 18:55:11 -060028#include <string.h>
29
Elyes HAOUASc3385072019-03-21 15:38:06 +010030#include "chip.h"
31
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020032static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Marc Jones1587dc82017-05-15 18:55:11 -060033 u32 io_min, u32 io_max)
34{
35 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060036
Marshall Dawson4e101ad2017-06-15 12:17:38 -060037 /* io range allocation. Limit */
38 tempreg = (nodeid & 0xf) | ((nodeid & 0x30) << (8 - 4)) | (linkn << 4)
39 | ((io_max & 0xf0) << (12 - 4));
Richard Spiegel41baf0c2018-10-22 13:57:18 -070040 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060041 tempreg = 3 | ((io_min & 0xf0) << (12 - 4)); /* base: ISA and VGA ? */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070042 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060043}
44
Marshall Dawson4e101ad2017-06-15 12:17:38 -060045static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index,
46 u32 mmio_min, u32 mmio_max)
Marc Jones1587dc82017-05-15 18:55:11 -060047{
48 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060049
Marshall Dawson4e101ad2017-06-15 12:17:38 -060050 /* io range allocation. Limit */
51 tempreg = (nodeid & 0xf) | (linkn << 4) | (mmio_max & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070052 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060053 tempreg = 3 | (nodeid & 0x30) | (mmio_min & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070054 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060055}
56
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020057static void read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060058{
Felix Held56b037b2022-03-02 22:57:01 +010059 unsigned int idx = 0;
Marc Jonesd6a82002018-03-31 22:46:57 -060060 struct resource *res;
61
Felix Heldaf17f0b2022-03-02 23:36:55 +010062 /* The northbridge has no PCI BARs implemented, so there's no need to call
63 pci_dev_read_resources for it */
64
Marc Jones1587dc82017-05-15 18:55:11 -060065 /*
66 * This MMCONF resource must be reserved in the PCI domain.
67 * It is not honored by the coreboot resource allocator if it is in
68 * the CPU_CLUSTER.
69 */
Felix Held56b037b2022-03-02 22:57:01 +010070 mmconf_resource(dev, idx++);
Marc Jonesd6a82002018-03-31 22:46:57 -060071
72 /* NB IOAPIC2 resource */
Felix Heldb1197af2022-03-02 23:02:31 +010073 res = new_resource(dev, idx++); /* IOAPIC2 */
Marc Jonesd6a82002018-03-31 22:46:57 -060074 res->base = IO_APIC2_ADDR;
75 res->size = 0x00001000;
76 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Marc Jones1587dc82017-05-15 18:55:11 -060077}
78
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070079static void set_resource(struct device *dev, struct resource *res, u32 nodeid)
Marc Jones1587dc82017-05-15 18:55:11 -060080{
81 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060082 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -060083 char buf[50];
84
85 /* Make certain the resource has actually been set */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070086 if (!(res->flags & IORESOURCE_ASSIGNED))
Marc Jones1587dc82017-05-15 18:55:11 -060087 return;
88
89 /* If I have already stored this resource don't worry about it */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070090 if (res->flags & IORESOURCE_STORED)
Marc Jones1587dc82017-05-15 18:55:11 -060091 return;
92
93 /* Only handle PCI memory and IO resources */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070094 if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Marc Jones1587dc82017-05-15 18:55:11 -060095 return;
96
97 /* Ensure I am actually looking at a resource of function 1 */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070098 if ((res->index & 0xffff) < 0x1000)
Marc Jones1587dc82017-05-15 18:55:11 -060099 return;
100
101 /* Get the base address */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700102 rbase = res->base;
Marc Jones1587dc82017-05-15 18:55:11 -0600103
104 /* Get the limit (rounded up) */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700105 rend = resource_end(res);
Marc Jones1587dc82017-05-15 18:55:11 -0600106
107 /* Get the register and link */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700108 reg = res->index & 0xfff; /* 4k */
109 link_num = IOINDEX_LINK(res->index);
Marc Jones1587dc82017-05-15 18:55:11 -0600110
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700111 if (res->flags & IORESOURCE_IO)
Elyes Haouas55d0f402022-07-16 09:53:05 +0200112 set_io_addr_reg(dev, nodeid, link_num, reg, rbase >> 8, rend >> 8);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700113 else if (res->flags & IORESOURCE_MEM)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600114 set_mmio_addr_reg(nodeid, link_num, reg,
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700115 (res->index >> 24), rbase >> 8, rend >> 8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600116
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700117 res->flags |= IORESOURCE_STORED;
Marc Jones1587dc82017-05-15 18:55:11 -0600118 snprintf(buf, sizeof(buf), " <node %x link %x>",
119 nodeid, link_num);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700120 report_resource_stored(dev, res, buf);
Marc Jones1587dc82017-05-15 18:55:11 -0600121}
122
123/**
124 * I tried to reuse the resource allocation code in set_resource()
125 * but it is too difficult to deal with the resource allocation magic.
126 */
127
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200128static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600129{
130 struct bus *link;
131
132 /* find out which link the VGA card is connected,
133 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600134 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600135 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
136 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600137
138 /* no VGA card installed */
139 if (link == NULL)
140 return;
141
Marshall Dawsone2697de2017-09-06 10:46:36 -0600142 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600143 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700144 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
Marc Jones1587dc82017-05-15 18:55:11 -0600145}
146
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200147static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600148{
149 struct bus *bus;
150 struct resource *res;
151
Marc Jones1587dc82017-05-15 18:55:11 -0600152 /* do we need this? */
153 create_vga_resource(dev);
154
155 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600156 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600157 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600158
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600159 for (bus = dev->link_list ; bus ; bus = bus->next)
160 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600161 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600162}
163
164static void northbridge_init(struct device *dev)
165{
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +0300166 register_new_ioapic((u8 *)IO_APIC2_ADDR);
Marc Jones1587dc82017-05-15 18:55:11 -0600167}
168
169static unsigned long acpi_fill_hest(acpi_hest_t *hest)
170{
171 void *addr, *current;
172
173 /* Skip the HEST header. */
174 current = (void *)(hest + 1);
175
176 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
177 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600178 current += acpi_create_hest_error_source(hest, current, 0,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700179 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600180
181 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
182 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600183 current += acpi_create_hest_error_source(hest, current, 1,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700184 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600185
186 return (unsigned long)current;
187}
188
Furquan Shaikh7536a392020-04-24 21:59:21 -0700189static void northbridge_fill_ssdt_generator(const struct device *device)
Marc Jones1587dc82017-05-15 18:55:11 -0600190{
191 msr_t msr;
192 char pscope[] = "\\_SB.PCI0";
193
194 acpigen_write_scope(pscope);
195 msr = rdmsr(TOP_MEM);
196 acpigen_write_name_dword("TOM1", msr.lo);
197 msr = rdmsr(TOP_MEM2);
198 /*
199 * Since XP only implements parts of ACPI 2.0, we can't use a qword
200 * here.
201 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
202 * slide 22ff.
203 * Shift value right by 20 bit to make it fit into 32bit,
204 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
205 */
206 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
207 acpigen_pop_len();
208}
209
Michał Żygowski9550e972020-03-20 13:56:46 +0100210static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
211{
212 unsigned int len = ssdt->length - sizeof(acpi_header_t);
213 unsigned int i;
214
215 for (i = sizeof(acpi_header_t); i < len; i++) {
216 /* Search for _PR_ scope and replace it with _SB_ */
217 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
218 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
219 }
220 /* Recalculate checksum */
221 ssdt->checksum = 0;
222 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
223}
224
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700225static unsigned long agesa_write_acpi_tables(const struct device *device,
Marc Jones1587dc82017-05-15 18:55:11 -0600226 unsigned long current,
227 acpi_rsdp_t *rsdp)
228{
229 acpi_srat_t *srat;
230 acpi_slit_t *slit;
231 acpi_header_t *ssdt;
232 acpi_header_t *alib;
233 acpi_header_t *ivrs;
234 acpi_hest_t *hest;
235
236 /* HEST */
Felix Held9abc4112023-01-18 15:47:39 +0100237 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600238 hest = (acpi_hest_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700239 acpi_write_hest(hest, acpi_fill_hest);
Marc Jones1587dc82017-05-15 18:55:11 -0600240 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700241 current += hest->header.length;
Marc Jones1587dc82017-05-15 18:55:11 -0600242
Felix Held9abc4112023-01-18 15:47:39 +0100243 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600244 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
245 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
246 if (ivrs != NULL) {
247 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600248 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600249 current += ivrs->length;
250 acpi_add_table(rsdp, ivrs);
251 } else {
252 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
253 }
254
255 /* SRAT */
Felix Held9abc4112023-01-18 15:47:39 +0100256 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600257 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600258 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600259 if (srat != NULL) {
260 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600261 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600262 current += srat->header.length;
263 acpi_add_table(rsdp, srat);
264 } else {
265 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
266 }
267
268 /* SLIT */
Felix Held9abc4112023-01-18 15:47:39 +0100269 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600270 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600271 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600272 if (slit != NULL) {
273 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600274 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600275 current += slit->header.length;
276 acpi_add_table(rsdp, slit);
277 } else {
278 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
279 }
280
281 /* ALIB */
Felix Held9abc4112023-01-18 15:47:39 +0100282 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600283 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600284 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600285 if (alib != NULL) {
286 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600287 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600288 current += alib->length;
289 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600290 } else {
291 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
292 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600293 }
294
Felix Held9abc4112023-01-18 15:47:39 +0100295 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600296 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600297 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Marc Jones1587dc82017-05-15 18:55:11 -0600298 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100299 patch_ssdt_processor_scope(ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600300 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600301 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600302 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600303 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600304 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
305 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600306 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600307
308 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
309 return current;
310}
311
Felix Held26651c82022-10-13 16:12:40 +0200312struct device_operations stoneyridge_northbridge_operations = {
Marc Jones1587dc82017-05-15 18:55:11 -0600313 .read_resources = read_resources,
314 .set_resources = set_resources,
315 .enable_resources = pci_dev_enable_resources,
316 .init = northbridge_init,
Nico Huber68680dd2020-03-31 17:34:52 +0200317 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Marc Jones1587dc82017-05-15 18:55:11 -0600318 .write_acpi_tables = agesa_write_acpi_tables,
Marc Jones1587dc82017-05-15 18:55:11 -0600319};
320
Marshall Dawson154239a2017-11-02 09:49:30 -0600321/*
322 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
323 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
324 * MMIO to posted. Route all I/O to the southbridge.
325 */
326void amd_initcpuio(void)
327{
Arthur Heymansc4350382021-10-28 12:35:39 +0200328 uintptr_t topmem = amd_topmem();
Marshall Dawson154239a2017-11-02 09:49:30 -0600329 uintptr_t base, limit;
330
331 /* Enable legacy video routing: D18F1xF4 VGA Enable */
332 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
333
334 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
335 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
Kyösti Mälkkidea42e02021-05-31 20:26:16 +0300336 limit = (ALIGN_DOWN(LAPIC_DEFAULT_BASE - 1, 64 * KiB) >> 8) | MMIO_NP;
Marshall Dawson154239a2017-11-02 09:49:30 -0600337 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
338 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
339
340 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
341 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
342 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
343 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
344 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
345
346 /* Route all I/O downstream */
347 base = 0 | IO_WE | IO_RE;
348 limit = ALIGN_DOWN(0xffff, 4 * KiB);
349 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
350 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
351}
352
Marc Jones1587dc82017-05-15 18:55:11 -0600353void fam15_finalize(void *chip_info)
354{
Marc Jones1587dc82017-05-15 18:55:11 -0600355 u32 value;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700356
357 /* TODO: move IOAPIC code to dsdt.asl */
358 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_INDEX, 0);
359 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_DATA, 5);
Marc Jones1587dc82017-05-15 18:55:11 -0600360
361 /* disable No Snoop */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700362 value = pci_read_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700363 value &= ~HDA_NO_SNOOP_EN;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700364 pci_write_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600365}
366
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200367void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600368{
Marc Jones1587dc82017-05-15 18:55:11 -0600369 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkki9e591c42021-01-09 12:37:25 +0200370 if (!acpi_is_wakeup_s3())
Kyösti Mälkki6e512c42018-06-14 06:57:05 +0300371 do_agesawrapper(AMD_INIT_MID, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600372}
373
Furquan Shaikhfc752b62020-05-13 12:14:11 -0700374void domain_read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600375{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700376 uint64_t uma_base = get_uma_base();
377 uint32_t uma_size = get_uma_size();
378 uint32_t mem_useable = (uintptr_t)cbmem_top();
379 msr_t tom = rdmsr(TOP_MEM);
380 msr_t high_tom = rdmsr(TOP_MEM2);
381 uint64_t high_mem_useable;
382 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600383
Furquan Shaikhfc752b62020-05-13 12:14:11 -0700384 pci_domain_read_resources(dev);
385
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700386 /* 0x0 -> 0x9ffff */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300387 ram_resource_kb(dev, idx++, 0, 0xa0000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600388
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700389 /* 0xa0000 -> 0xbffff: legacy VGA */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300390 mmio_resource_kb(dev, idx++, 0xa0000 / KiB, 0x20000 / KiB);
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700391
392 /* 0xc0000 -> 0xfffff: Option ROM */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300393 reserved_ram_resource_kb(dev, idx++, 0xc0000 / KiB, 0x40000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600394
Marshall Dawson29f1b742017-09-06 14:59:45 -0600395 /*
Martin Roth26f97f92021-10-01 14:53:22 -0600396 * 0x100000 (1MiB) -> low top usable RAM
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700397 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600398 */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300399 ram_resource_kb(dev, idx++, (1 * MiB) / KiB,
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700400 (mem_useable - (1 * MiB)) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600401
Martin Roth26f97f92021-10-01 14:53:22 -0600402 /* Low top usable RAM -> Low top RAM (bottom pci mmio hole) */
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300403 reserved_ram_resource_kb(dev, idx++, mem_useable / KiB,
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700404 (tom.lo - mem_useable) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600405
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700406 /* If there is memory above 4GiB */
407 if (high_tom.hi) {
Martin Roth26f97f92021-10-01 14:53:22 -0600408 /* 4GiB -> high top usable */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700409 if (uma_base >= (4ull * GiB))
410 high_mem_useable = uma_base;
411 else
412 high_mem_useable = ((uint64_t)high_tom.lo |
413 ((uint64_t)high_tom.hi << 32));
Marc Jones1587dc82017-05-15 18:55:11 -0600414
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300415 ram_resource_kb(dev, idx++, (4ull * GiB) / KiB,
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700416 ((high_mem_useable - (4ull * GiB)) / KiB));
Marc Jones1587dc82017-05-15 18:55:11 -0600417
Martin Roth26f97f92021-10-01 14:53:22 -0600418 /* High top usable RAM -> high top RAM */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700419 if (uma_base >= (4ull * GiB)) {
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300420 reserved_ram_resource_kb(dev, idx++, uma_base / KiB,
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700421 uma_size / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600422 }
Marc Jones1587dc82017-05-15 18:55:11 -0600423 }
Marc Jones1587dc82017-05-15 18:55:11 -0600424}
425
Marc Jones1587dc82017-05-15 18:55:11 -0600426/*********************************************************************
427 * Change the vendor / device IDs to match the generic VBIOS header. *
428 *********************************************************************/
429u32 map_oprom_vendev(u32 vendev)
430{
431 u32 new_vendev;
Richard Spiegel9247e862019-06-28 09:18:47 -0700432
433 if ((vendev >= 0x100298e0) && (vendev <= 0x100298ef))
434 new_vendev = 0x100298e0;
435 else if ((vendev >= 0x10029870) && (vendev <= 0x1002987f))
436 new_vendev = 0x10029870;
437 else
438 new_vendev = vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600439
440 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600441 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
442 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600443
444 return new_vendev;
445}
Marshall Dawson2942db62017-12-14 10:00:27 -0700446
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700447__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
448
Marshall Dawson2942db62017-12-14 10:00:27 -0700449void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
450{
Martin Roth50f2e4c2018-10-29 11:16:53 -0600451 const struct device *dev = SOC_IOMMU_DEV;
452 params->IommuSupport = dev && dev->enabled;
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700453 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700454}
455
456void SetNbMidParams(GNB_MID_CONFIGURATION *params)
457{
458 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
459 params->iGpuVgaMode = 0;
460 params->GnbIoapicAddress = IO_APIC2_ADDR;
461}
Furquan Shaikh91a7abf2020-04-27 18:48:48 -0700462
463void hda_soc_ssdt_quirks(const struct device *dev)
464{
465 const char *scope = acpi_device_path(dev);
466 static const struct fieldlist list[] = {
467 FIELDLIST_OFFSET(0x42),
468 FIELDLIST_NAMESTR("NSDI", 1),
469 FIELDLIST_NAMESTR("NSDO", 1),
470 FIELDLIST_NAMESTR("NSEN", 1),
471 };
472 struct opregion opreg = OPREGION("AZPD", PCI_CONFIG, 0x0, 0x100);
473
474 assert(scope);
475
476 acpigen_write_scope(scope);
477
478 /*
479 * OperationRegion(AZPD, PCI_Config, 0x00, 0x100)
480 * Field (AZPD, AnyAcc, NoLock, Preserve) {
481 * Offset (0x42),
482 * NSDI, 1,
483 * NSDO, 1,
484 * NSEN, 1,
485 * }
486 */
487 acpigen_write_opregion(&opreg);
488 acpigen_write_field(opreg.name, list, ARRAY_SIZE(list),
489 FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
490
491 /*
492 * Method (_INI, 0, NotSerialized) {
Kyösti Mälkkiff9ba542021-02-09 17:38:23 +0200493 * Store (Zero, NSEN)
494 * Store (One, NSDO)
495 * Store (One, NSDI)
Furquan Shaikh91a7abf2020-04-27 18:48:48 -0700496 * }
497 */
498 acpigen_write_method("_INI", 0);
499
Furquan Shaikhac204ba2021-02-19 10:23:17 -0800500 acpigen_write_store_op_to_namestr(ZERO_OP, "NSEN");
501 acpigen_write_store_op_to_namestr(ONE_OP, "NSDO");
502 acpigen_write_store_op_to_namestr(ONE_OP, "NSDI");
Furquan Shaikh91a7abf2020-04-27 18:48:48 -0700503
Furquan Shaikh91a7abf2020-04-27 18:48:48 -0700504 acpigen_pop_len(); /* Method _INI */
505
506 acpigen_pop_len(); /* Scope */
507}