blob: b95c4cdf2e8dde05bfc81ea03df26b8529cd6326 [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>
Felix Held915c3872023-04-11 21:21:35 +02004#include <amdblocks/acpi.h>
Michał Żygowskif65c1e42019-12-01 18:14:39 +01005#include <amdblocks/biosram.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>
Felix Helda8da0702023-06-05 21:19:27 +02009#include <arch/vga.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070010#include <acpi/acpi.h>
11#include <acpi/acpigen.h>
Marc Jones1587dc82017-05-15 18:55:11 -060012#include <cbmem.h>
Marc Jones1587dc82017-05-15 18:55:11 -060013#include <console/console.h>
Marc Jones1587dc82017-05-15 18:55:11 -060014#include <cpu/amd/mtrr.h>
Marshall Dawson154239a2017-11-02 09:49:30 -060015#include <cpu/x86/lapic_def.h>
Marshall Dawsonf82aa102017-09-20 18:01:41 -060016#include <cpu/x86/msr.h>
Marc Jones1587dc82017-05-15 18:55:11 -060017#include <device/device.h>
18#include <device/pci.h>
19#include <device/pci_ids.h>
Richard Spiegel0ad74ac2017-12-08 16:53:29 -070020#include <amdblocks/agesawrapper.h>
21#include <amdblocks/agesawrapper_call.h>
Felix Held604ffa62021-02-12 00:43:20 +010022#include <amdblocks/ioapic.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070023#include <agesa_headers.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060024#include <soc/cpu.h>
Marc Jones1587dc82017-05-15 18:55:11 -060025#include <soc/northbridge.h>
Marshall Dawson38bded02017-09-01 09:54:48 -060026#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070027#include <soc/iomap.h>
Marc Jones1587dc82017-05-15 18:55:11 -060028#include <stdint.h>
Marc Jones1587dc82017-05-15 18:55:11 -060029#include <string.h>
30
Elyes HAOUASc3385072019-03-21 15:38:06 +010031#include "chip.h"
32
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020033static void read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060034{
Felix Held56b037b2022-03-02 22:57:01 +010035 unsigned int idx = 0;
Marc Jonesd6a82002018-03-31 22:46:57 -060036
Felix Heldaf17f0b2022-03-02 23:36:55 +010037 /* The northbridge has no PCI BARs implemented, so there's no need to call
38 pci_dev_read_resources for it */
39
Marc Jones1587dc82017-05-15 18:55:11 -060040 /*
41 * This MMCONF resource must be reserved in the PCI domain.
42 * It is not honored by the coreboot resource allocator if it is in
43 * the CPU_CLUSTER.
44 */
Felix Held56b037b2022-03-02 22:57:01 +010045 mmconf_resource(dev, idx++);
Marc Jones1587dc82017-05-15 18:55:11 -060046}
47
Marc Jones1587dc82017-05-15 18:55:11 -060048/**
49 * I tried to reuse the resource allocation code in set_resource()
50 * but it is too difficult to deal with the resource allocation magic.
51 */
52
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020053static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060054{
Arthur Heymans80c79a52023-08-24 15:12:19 +020055 if (!dev->link_list)
56 return;
57 if (!(dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA))
Marc Jones1587dc82017-05-15 18:55:11 -060058 return;
59
Marshall Dawsone2697de2017-09-06 10:46:36 -060060 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -060061 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070062 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
Marc Jones1587dc82017-05-15 18:55:11 -060063}
64
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020065static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060066{
Marc Jones1587dc82017-05-15 18:55:11 -060067 /* do we need this? */
68 create_vga_resource(dev);
69
Arthur Heymans80c79a52023-08-24 15:12:19 +020070 if (dev->link_list && dev->link_list->children)
71 assign_resources(dev->link_list);
Marc Jones1587dc82017-05-15 18:55:11 -060072}
73
74static void northbridge_init(struct device *dev)
75{
Kyösti Mälkki2e65e9c2021-06-16 11:00:40 +030076 register_new_ioapic((u8 *)IO_APIC2_ADDR);
Marc Jones1587dc82017-05-15 18:55:11 -060077}
78
Felix Held8cab80c2023-05-05 15:20:15 +020079/* Used by \_SB.PCI0._CRS */
80static void acpi_fill_root_complex_tom(const struct device *device)
81{
82 const char *scope;
83
84 assert(device);
85
86 scope = acpi_device_scope(device);
87 assert(scope);
88 acpigen_write_scope(scope);
89
90 acpigen_write_name_dword("TOM1", get_top_of_mem_below_4gb());
91
92 /*
93 * Since XP only implements parts of ACPI 2.0, we can't use a qword
94 * here.
95 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
96 * slide 22ff.
97 * Shift value right by 20 bit to make it fit into 32bit,
98 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
99 */
100 acpigen_write_name_dword("TOM2", get_top_of_mem_above_4gb() >> 20);
101 acpigen_pop_len();
102}
103
Marc Jones1587dc82017-05-15 18:55:11 -0600104static unsigned long acpi_fill_hest(acpi_hest_t *hest)
105{
106 void *addr, *current;
107
108 /* Skip the HEST header. */
109 current = (void *)(hest + 1);
110
111 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
112 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600113 current += acpi_create_hest_error_source(hest, current, 0,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700114 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600115
116 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
117 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600118 current += acpi_create_hest_error_source(hest, current, 1,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700119 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600120
121 return (unsigned long)current;
122}
123
Felix Heldd9e82632024-01-26 14:22:31 +0100124unsigned long soc_acpi_write_tables(const struct device *device, unsigned long current,
125 acpi_rsdp_t *rsdp)
Marc Jones1587dc82017-05-15 18:55:11 -0600126{
127 acpi_srat_t *srat;
128 acpi_slit_t *slit;
Marc Jones1587dc82017-05-15 18:55:11 -0600129 acpi_header_t *alib;
130 acpi_header_t *ivrs;
131 acpi_hest_t *hest;
132
133 /* HEST */
Felix Held9abc4112023-01-18 15:47:39 +0100134 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600135 hest = (acpi_hest_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700136 acpi_write_hest(hest, acpi_fill_hest);
Marc Jones1587dc82017-05-15 18:55:11 -0600137 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700138 current += hest->header.length;
Marc Jones1587dc82017-05-15 18:55:11 -0600139
Felix Held9abc4112023-01-18 15:47:39 +0100140 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600141 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
142 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
143 if (ivrs != NULL) {
144 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600145 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600146 current += ivrs->length;
147 acpi_add_table(rsdp, ivrs);
148 } else {
149 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
150 }
151
152 /* SRAT */
Felix Held9abc4112023-01-18 15:47:39 +0100153 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600154 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600155 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600156 if (srat != NULL) {
157 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600158 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600159 current += srat->header.length;
160 acpi_add_table(rsdp, srat);
161 } else {
162 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
163 }
164
165 /* SLIT */
Felix Held9abc4112023-01-18 15:47:39 +0100166 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600167 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600168 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600169 if (slit != NULL) {
170 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600171 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600172 current += slit->header.length;
173 acpi_add_table(rsdp, slit);
174 } else {
175 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
176 }
177
178 /* ALIB */
Felix Held9abc4112023-01-18 15:47:39 +0100179 current = acpi_align_current(current);
Marc Jones1587dc82017-05-15 18:55:11 -0600180 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600181 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600182 if (alib != NULL) {
183 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600184 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600185 current += alib->length;
186 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600187 } else {
188 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
189 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600190 }
191
Marc Jones1587dc82017-05-15 18:55:11 -0600192 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
193 return current;
194}
195
Felix Held26651c82022-10-13 16:12:40 +0200196struct device_operations stoneyridge_northbridge_operations = {
Marc Jones1587dc82017-05-15 18:55:11 -0600197 .read_resources = read_resources,
198 .set_resources = set_resources,
199 .enable_resources = pci_dev_enable_resources,
200 .init = northbridge_init,
Felix Held915c3872023-04-11 21:21:35 +0200201 .acpi_fill_ssdt = acpi_fill_root_complex_tom,
Felix Held1b410d92024-01-26 14:05:58 +0100202 .write_acpi_tables = soc_acpi_write_tables,
Marc Jones1587dc82017-05-15 18:55:11 -0600203};
204
Marshall Dawson154239a2017-11-02 09:49:30 -0600205/*
206 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
207 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
208 * MMIO to posted. Route all I/O to the southbridge.
209 */
210void amd_initcpuio(void)
211{
Felix Held5e9afe72023-04-20 12:55:55 +0200212 uintptr_t topmem = get_top_of_mem_below_4gb();
Marshall Dawson154239a2017-11-02 09:49:30 -0600213 uintptr_t base, limit;
214
215 /* Enable legacy video routing: D18F1xF4 VGA Enable */
216 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
217
218 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
219 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
Kyösti Mälkkidea42e02021-05-31 20:26:16 +0300220 limit = (ALIGN_DOWN(LAPIC_DEFAULT_BASE - 1, 64 * KiB) >> 8) | MMIO_NP;
Marshall Dawson154239a2017-11-02 09:49:30 -0600221 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
222 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
223
224 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
225 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
226 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
227 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
228 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
229
230 /* Route all I/O downstream */
231 base = 0 | IO_WE | IO_RE;
232 limit = ALIGN_DOWN(0xffff, 4 * KiB);
233 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
234 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
235}
236
Marc Jones1587dc82017-05-15 18:55:11 -0600237void fam15_finalize(void *chip_info)
238{
Marc Jones1587dc82017-05-15 18:55:11 -0600239 u32 value;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700240
241 /* TODO: move IOAPIC code to dsdt.asl */
242 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_INDEX, 0);
243 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_DATA, 5);
Marc Jones1587dc82017-05-15 18:55:11 -0600244
245 /* disable No Snoop */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700246 value = pci_read_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700247 value &= ~HDA_NO_SNOOP_EN;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700248 pci_write_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600249}
250
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200251void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600252{
Marc Jones1587dc82017-05-15 18:55:11 -0600253 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkki9e591c42021-01-09 12:37:25 +0200254 if (!acpi_is_wakeup_s3())
Kyösti Mälkki6e512c42018-06-14 06:57:05 +0300255 do_agesawrapper(AMD_INIT_MID, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600256}
257
Furquan Shaikhfc752b62020-05-13 12:14:11 -0700258void domain_read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600259{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700260 uint64_t uma_base = get_uma_base();
261 uint32_t uma_size = get_uma_size();
262 uint32_t mem_useable = (uintptr_t)cbmem_top();
Felix Held392cf2f2023-04-20 13:23:23 +0200263 uint32_t tom = get_top_of_mem_below_4gb();
Felix Held27af3e62023-04-22 05:59:52 +0200264 uint64_t high_tom = get_top_of_mem_above_4gb();
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700265 uint64_t high_mem_useable;
266 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600267
Furquan Shaikhfc752b62020-05-13 12:14:11 -0700268 pci_domain_read_resources(dev);
269
Felix Heldd7ad1402023-06-05 15:30:10 +0200270 fixed_io_range_reserved(dev, idx++, PCI_IO_CONFIG_INDEX, PCI_IO_CONFIG_PORT_COUNT);
271
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700272 /* 0x0 -> 0x9ffff */
Arthur Heymans885efa12023-07-05 12:11:12 +0200273 ram_range(dev, idx++, 0, 0xa0000);
Marc Jones1587dc82017-05-15 18:55:11 -0600274
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700275 /* 0xa0000 -> 0xbffff: legacy VGA */
Arthur Heymans885efa12023-07-05 12:11:12 +0200276 mmio_range(dev, idx++, VGA_MMIO_BASE, VGA_MMIO_SIZE);
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700277
278 /* 0xc0000 -> 0xfffff: Option ROM */
Arthur Heymans885efa12023-07-05 12:11:12 +0200279 reserved_ram_from_to(dev, idx++, 0xc0000, 1 * MiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600280
Marshall Dawson29f1b742017-09-06 14:59:45 -0600281 /*
Martin Roth26f97f92021-10-01 14:53:22 -0600282 * 0x100000 (1MiB) -> low top usable RAM
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700283 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600284 */
Arthur Heymans885efa12023-07-05 12:11:12 +0200285 ram_from_to(dev, idx++, 1 * MiB, mem_useable);
Marc Jones1587dc82017-05-15 18:55:11 -0600286
Martin Roth26f97f92021-10-01 14:53:22 -0600287 /* Low top usable RAM -> Low top RAM (bottom pci mmio hole) */
Arthur Heymans885efa12023-07-05 12:11:12 +0200288 reserved_ram_from_to(dev, idx++, mem_useable, tom);
Marc Jones1587dc82017-05-15 18:55:11 -0600289
Felix Held5913a542024-01-09 16:59:37 +0100290 /* NB IOAPIC2 resource. IOMMU_IOAPIC_IDX is used as index, so that the common AMD MADT
291 code can find this resource */
292 mmio_range(dev, IOMMU_IOAPIC_IDX, IO_APIC2_ADDR, 0x1000);
293
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700294 /* If there is memory above 4GiB */
Felix Held392cf2f2023-04-20 13:23:23 +0200295 if (high_tom >> 32) {
Martin Roth26f97f92021-10-01 14:53:22 -0600296 /* 4GiB -> high top usable */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700297 if (uma_base >= (4ull * GiB))
298 high_mem_useable = uma_base;
299 else
Felix Held392cf2f2023-04-20 13:23:23 +0200300 high_mem_useable = high_tom;
Marc Jones1587dc82017-05-15 18:55:11 -0600301
Arthur Heymans885efa12023-07-05 12:11:12 +0200302 ram_from_to(dev, idx++, 4ull * GiB, high_mem_useable);
Marc Jones1587dc82017-05-15 18:55:11 -0600303
Martin Roth26f97f92021-10-01 14:53:22 -0600304 /* High top usable RAM -> high top RAM */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700305 if (uma_base >= (4ull * GiB)) {
Arthur Heymans885efa12023-07-05 12:11:12 +0200306 reserved_ram_range(dev, idx++, uma_base, uma_size);
Marc Jones1587dc82017-05-15 18:55:11 -0600307 }
Marc Jones1587dc82017-05-15 18:55:11 -0600308 }
Marc Jones1587dc82017-05-15 18:55:11 -0600309}
310
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700311__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
312
Marshall Dawson2942db62017-12-14 10:00:27 -0700313void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
314{
Felix Held727ee6672023-12-20 22:47:03 +0100315 params->IommuSupport = is_dev_enabled(DEV_PTR(iommu));
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700316 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700317}
318
319void SetNbMidParams(GNB_MID_CONFIGURATION *params)
320{
321 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
322 params->iGpuVgaMode = 0;
323 params->GnbIoapicAddress = IO_APIC2_ADDR;
324}