blob: 2c90d04ff91b1a3b0c5b70f70bb5ebe5751d7168 [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>
Marc Jonesd6a82002018-03-31 22:46:57 -06007#include <arch/ioapic.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07008#include <acpi/acpi.h>
9#include <acpi/acpigen.h>
Marc Jones1587dc82017-05-15 18:55:11 -060010#include <cbmem.h>
Marc Jones1587dc82017-05-15 18:55:11 -060011#include <console/console.h>
Marc Jones1587dc82017-05-15 18:55:11 -060012#include <cpu/amd/mtrr.h>
Marshall Dawson154239a2017-11-02 09:49:30 -060013#include <cpu/x86/lapic_def.h>
Marshall Dawsonf82aa102017-09-20 18:01:41 -060014#include <cpu/x86/msr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020015#include <cpu/amd/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>
Marshall Dawson653f7602018-09-04 13:25:39 -060029#include <arch/bert_storage.h>
Marc Jones1587dc82017-05-15 18:55:11 -060030
Elyes HAOUASc3385072019-03-21 15:38:06 +010031#include "chip.h"
32
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020033static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Marc Jones1587dc82017-05-15 18:55:11 -060034 u32 io_min, u32 io_max)
35{
36 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060037
Marshall Dawson4e101ad2017-06-15 12:17:38 -060038 /* io range allocation. Limit */
39 tempreg = (nodeid & 0xf) | ((nodeid & 0x30) << (8 - 4)) | (linkn << 4)
40 | ((io_max & 0xf0) << (12 - 4));
Richard Spiegel41baf0c2018-10-22 13:57:18 -070041 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060042 tempreg = 3 | ((io_min & 0xf0) << (12 - 4)); /* base: ISA and VGA ? */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070043 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060044}
45
Marshall Dawson4e101ad2017-06-15 12:17:38 -060046static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index,
47 u32 mmio_min, u32 mmio_max)
Marc Jones1587dc82017-05-15 18:55:11 -060048{
49 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060050
Marshall Dawson4e101ad2017-06-15 12:17:38 -060051 /* io range allocation. Limit */
52 tempreg = (nodeid & 0xf) | (linkn << 4) | (mmio_max & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070053 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060054 tempreg = 3 | (nodeid & 0x30) | (mmio_min & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070055 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060056}
57
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020058static void read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060059{
Marc Jonesd6a82002018-03-31 22:46:57 -060060 struct resource *res;
61
Marc Jones1587dc82017-05-15 18:55:11 -060062 /*
63 * This MMCONF resource must be reserved in the PCI domain.
64 * It is not honored by the coreboot resource allocator if it is in
65 * the CPU_CLUSTER.
66 */
Aaron Durbin3173d442017-11-03 12:14:25 -060067 mmconf_resource(dev, MMIO_CONF_BASE);
Marc Jonesd6a82002018-03-31 22:46:57 -060068
69 /* NB IOAPIC2 resource */
70 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
71 res->base = IO_APIC2_ADDR;
72 res->size = 0x00001000;
73 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Marc Jones1587dc82017-05-15 18:55:11 -060074}
75
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070076static void set_resource(struct device *dev, struct resource *res, u32 nodeid)
Marc Jones1587dc82017-05-15 18:55:11 -060077{
78 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060079 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -060080 char buf[50];
81
82 /* Make certain the resource has actually been set */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070083 if (!(res->flags & IORESOURCE_ASSIGNED))
Marc Jones1587dc82017-05-15 18:55:11 -060084 return;
85
86 /* If I have already stored this resource don't worry about it */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070087 if (res->flags & IORESOURCE_STORED)
Marc Jones1587dc82017-05-15 18:55:11 -060088 return;
89
90 /* Only handle PCI memory and IO resources */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070091 if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Marc Jones1587dc82017-05-15 18:55:11 -060092 return;
93
94 /* Ensure I am actually looking at a resource of function 1 */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070095 if ((res->index & 0xffff) < 0x1000)
Marc Jones1587dc82017-05-15 18:55:11 -060096 return;
97
98 /* Get the base address */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070099 rbase = res->base;
Marc Jones1587dc82017-05-15 18:55:11 -0600100
101 /* Get the limit (rounded up) */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700102 rend = resource_end(res);
Marc Jones1587dc82017-05-15 18:55:11 -0600103
104 /* Get the register and link */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700105 reg = res->index & 0xfff; /* 4k */
106 link_num = IOINDEX_LINK(res->index);
Marc Jones1587dc82017-05-15 18:55:11 -0600107
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700108 if (res->flags & IORESOURCE_IO)
Marc Jones1587dc82017-05-15 18:55:11 -0600109 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700110 else if (res->flags & IORESOURCE_MEM)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600111 set_mmio_addr_reg(nodeid, link_num, reg,
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700112 (res->index >> 24), rbase >> 8, rend >> 8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600113
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700114 res->flags |= IORESOURCE_STORED;
Marc Jones1587dc82017-05-15 18:55:11 -0600115 snprintf(buf, sizeof(buf), " <node %x link %x>",
116 nodeid, link_num);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700117 report_resource_stored(dev, res, buf);
Marc Jones1587dc82017-05-15 18:55:11 -0600118}
119
120/**
121 * I tried to reuse the resource allocation code in set_resource()
122 * but it is too difficult to deal with the resource allocation magic.
123 */
124
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200125static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600126{
127 struct bus *link;
128
129 /* find out which link the VGA card is connected,
130 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600131 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600132 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
133 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600134
135 /* no VGA card installed */
136 if (link == NULL)
137 return;
138
Marshall Dawsone2697de2017-09-06 10:46:36 -0600139 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600140 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700141 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
Marc Jones1587dc82017-05-15 18:55:11 -0600142}
143
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200144static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600145{
146 struct bus *bus;
147 struct resource *res;
148
Marc Jones1587dc82017-05-15 18:55:11 -0600149 /* do we need this? */
150 create_vga_resource(dev);
151
152 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600153 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600154 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600155
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600156 for (bus = dev->link_list ; bus ; bus = bus->next)
157 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600158 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600159}
160
161static void northbridge_init(struct device *dev)
162{
Felix Held604ffa62021-02-12 00:43:20 +0100163 setup_ioapic((u8 *)IO_APIC2_ADDR, GNB_IOAPIC_ID);
Marc Jones1587dc82017-05-15 18:55:11 -0600164}
165
166static unsigned long acpi_fill_hest(acpi_hest_t *hest)
167{
168 void *addr, *current;
169
170 /* Skip the HEST header. */
171 current = (void *)(hest + 1);
172
173 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
174 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600175 current += acpi_create_hest_error_source(hest, current, 0,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700176 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600177
178 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
179 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600180 current += acpi_create_hest_error_source(hest, current, 1,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700181 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600182
183 return (unsigned long)current;
184}
185
Furquan Shaikh7536a392020-04-24 21:59:21 -0700186static void northbridge_fill_ssdt_generator(const struct device *device)
Marc Jones1587dc82017-05-15 18:55:11 -0600187{
188 msr_t msr;
189 char pscope[] = "\\_SB.PCI0";
190
191 acpigen_write_scope(pscope);
192 msr = rdmsr(TOP_MEM);
193 acpigen_write_name_dword("TOM1", msr.lo);
194 msr = rdmsr(TOP_MEM2);
195 /*
196 * Since XP only implements parts of ACPI 2.0, we can't use a qword
197 * here.
198 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
199 * slide 22ff.
200 * Shift value right by 20 bit to make it fit into 32bit,
201 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
202 */
203 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
204 acpigen_pop_len();
205}
206
Michał Żygowski9550e972020-03-20 13:56:46 +0100207static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
208{
209 unsigned int len = ssdt->length - sizeof(acpi_header_t);
210 unsigned int i;
211
212 for (i = sizeof(acpi_header_t); i < len; i++) {
213 /* Search for _PR_ scope and replace it with _SB_ */
214 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
215 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
216 }
217 /* Recalculate checksum */
218 ssdt->checksum = 0;
219 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
220}
221
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700222static unsigned long agesa_write_acpi_tables(const struct device *device,
Marc Jones1587dc82017-05-15 18:55:11 -0600223 unsigned long current,
224 acpi_rsdp_t *rsdp)
225{
226 acpi_srat_t *srat;
227 acpi_slit_t *slit;
228 acpi_header_t *ssdt;
229 acpi_header_t *alib;
230 acpi_header_t *ivrs;
231 acpi_hest_t *hest;
Marshall Dawson653f7602018-09-04 13:25:39 -0600232 acpi_bert_t *bert;
Marc Jones1587dc82017-05-15 18:55:11 -0600233
234 /* HEST */
235 current = ALIGN(current, 8);
236 hest = (acpi_hest_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700237 acpi_write_hest(hest, acpi_fill_hest);
Marc Jones1587dc82017-05-15 18:55:11 -0600238 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700239 current += hest->header.length;
Marc Jones1587dc82017-05-15 18:55:11 -0600240
Marshall Dawson653f7602018-09-04 13:25:39 -0600241 /* BERT */
Julius Wernercd49cce2019-03-05 16:53:33 -0800242 if (CONFIG(ACPI_BERT) && bert_errors_present()) {
Marshall Dawson653f7602018-09-04 13:25:39 -0600243 /* Skip the table if no errors are present. ACPI driver reports
244 * a table with a 0-length region:
245 * BERT: [Firmware Bug]: table invalid.
246 */
247 void *rgn;
248 size_t size;
249 bert_errors_region(&rgn, &size);
250 if (!rgn) {
251 printk(BIOS_ERR, "Error: Can't find BERT storage area\n");
252 } else {
253 current = ALIGN(current, 8);
254 bert = (acpi_bert_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700255 acpi_write_bert(bert, (uintptr_t)rgn, size);
Marshall Dawson653f7602018-09-04 13:25:39 -0600256 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700257 current += bert->header.length;
Marshall Dawson653f7602018-09-04 13:25:39 -0600258 }
259 }
260
Marc Jones1587dc82017-05-15 18:55:11 -0600261 current = ALIGN(current, 8);
262 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
263 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
264 if (ivrs != NULL) {
265 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600266 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600267 current += ivrs->length;
268 acpi_add_table(rsdp, ivrs);
269 } else {
270 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
271 }
272
273 /* SRAT */
274 current = ALIGN(current, 8);
275 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600276 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600277 if (srat != NULL) {
278 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600279 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600280 current += srat->header.length;
281 acpi_add_table(rsdp, srat);
282 } else {
283 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
284 }
285
286 /* SLIT */
287 current = ALIGN(current, 8);
288 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600289 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600290 if (slit != NULL) {
291 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600292 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600293 current += slit->header.length;
294 acpi_add_table(rsdp, slit);
295 } else {
296 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
297 }
298
299 /* ALIB */
300 current = ALIGN(current, 16);
301 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600302 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600303 if (alib != NULL) {
304 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600305 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600306 current += alib->length;
307 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600308 } else {
309 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
310 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600311 }
312
Marc Jones1587dc82017-05-15 18:55:11 -0600313 current = ALIGN(current, 16);
314 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600315 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Marc Jones1587dc82017-05-15 18:55:11 -0600316 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100317 patch_ssdt_processor_scope(ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600318 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600319 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600320 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600321 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600322 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
323 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600324 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600325
326 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
327 return current;
328}
329
330static struct device_operations northbridge_operations = {
331 .read_resources = read_resources,
332 .set_resources = set_resources,
333 .enable_resources = pci_dev_enable_resources,
334 .init = northbridge_init,
Nico Huber68680dd2020-03-31 17:34:52 +0200335 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Marc Jones1587dc82017-05-15 18:55:11 -0600336 .write_acpi_tables = agesa_write_acpi_tables,
Marc Jones1587dc82017-05-15 18:55:11 -0600337};
338
Richard Spiegel9247e862019-06-28 09:18:47 -0700339static const unsigned short pci_device_ids[] = {
340 PCI_DEVICE_ID_AMD_15H_MODEL_606F_NB_HT,
341 PCI_DEVICE_ID_AMD_15H_MODEL_707F_NB_HT,
342 0 };
343
Marc Jones1587dc82017-05-15 18:55:11 -0600344static const struct pci_driver family15_northbridge __pci_driver = {
345 .ops = &northbridge_operations,
346 .vendor = PCI_VENDOR_ID_AMD,
Richard Spiegel9247e862019-06-28 09:18:47 -0700347 .devices = pci_device_ids,
Marc Jones1587dc82017-05-15 18:55:11 -0600348};
349
Marshall Dawson154239a2017-11-02 09:49:30 -0600350/*
351 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
352 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
353 * MMIO to posted. Route all I/O to the southbridge.
354 */
355void amd_initcpuio(void)
356{
357 uintptr_t topmem = bsp_topmem();
358 uintptr_t base, limit;
359
360 /* Enable legacy video routing: D18F1xF4 VGA Enable */
361 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
362
363 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
364 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
365 limit = (ALIGN_DOWN(LOCAL_APIC_ADDR - 1, 64 * KiB) >> 8) | MMIO_NP;
366 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
367 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
368
369 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
370 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
371 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
372 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
373 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
374
375 /* Route all I/O downstream */
376 base = 0 | IO_WE | IO_RE;
377 limit = ALIGN_DOWN(0xffff, 4 * KiB);
378 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
379 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
380}
381
Marc Jones1587dc82017-05-15 18:55:11 -0600382void fam15_finalize(void *chip_info)
383{
Marc Jones1587dc82017-05-15 18:55:11 -0600384 u32 value;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700385
386 /* TODO: move IOAPIC code to dsdt.asl */
387 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_INDEX, 0);
388 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_DATA, 5);
Marc Jones1587dc82017-05-15 18:55:11 -0600389
390 /* disable No Snoop */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700391 value = pci_read_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700392 value &= ~HDA_NO_SNOOP_EN;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700393 pci_write_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600394}
395
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200396void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600397{
Marc Jones1587dc82017-05-15 18:55:11 -0600398 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkki9e591c42021-01-09 12:37:25 +0200399 if (!acpi_is_wakeup_s3())
Kyösti Mälkki6e512c42018-06-14 06:57:05 +0300400 do_agesawrapper(AMD_INIT_MID, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600401}
402
Furquan Shaikhfc752b62020-05-13 12:14:11 -0700403void domain_read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600404{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700405 uint64_t uma_base = get_uma_base();
406 uint32_t uma_size = get_uma_size();
407 uint32_t mem_useable = (uintptr_t)cbmem_top();
408 msr_t tom = rdmsr(TOP_MEM);
409 msr_t high_tom = rdmsr(TOP_MEM2);
410 uint64_t high_mem_useable;
411 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600412
Furquan Shaikhfc752b62020-05-13 12:14:11 -0700413 pci_domain_read_resources(dev);
414
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700415 /* 0x0 -> 0x9ffff */
416 ram_resource(dev, idx++, 0, 0xa0000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600417
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700418 /* 0xa0000 -> 0xbffff: legacy VGA */
419 mmio_resource(dev, idx++, 0xa0000 / KiB, 0x20000 / KiB);
420
421 /* 0xc0000 -> 0xfffff: Option ROM */
422 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, 0x40000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600423
Marshall Dawson29f1b742017-09-06 14:59:45 -0600424 /*
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700425 * 0x100000 (1MiB) -> low top useable RAM
426 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600427 */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700428 ram_resource(dev, idx++, (1 * MiB) / KiB,
429 (mem_useable - (1 * MiB)) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600430
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700431 /* Low top useable RAM -> Low top RAM (bottom pci mmio hole) */
432 reserved_ram_resource(dev, idx++, mem_useable / KiB,
433 (tom.lo - mem_useable) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600434
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700435 /* If there is memory above 4GiB */
436 if (high_tom.hi) {
437 /* 4GiB -> high top useable */
438 if (uma_base >= (4ull * GiB))
439 high_mem_useable = uma_base;
440 else
441 high_mem_useable = ((uint64_t)high_tom.lo |
442 ((uint64_t)high_tom.hi << 32));
Marc Jones1587dc82017-05-15 18:55:11 -0600443
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700444 ram_resource(dev, idx++, (4ull * GiB) / KiB,
445 ((high_mem_useable - (4ull * GiB)) / KiB));
Marc Jones1587dc82017-05-15 18:55:11 -0600446
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700447 /* High top useable RAM -> high top RAM */
448 if (uma_base >= (4ull * GiB)) {
449 reserved_ram_resource(dev, idx++, uma_base / KiB,
450 uma_size / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600451 }
Marc Jones1587dc82017-05-15 18:55:11 -0600452 }
Marc Jones1587dc82017-05-15 18:55:11 -0600453}
454
Marc Jones1587dc82017-05-15 18:55:11 -0600455/*********************************************************************
456 * Change the vendor / device IDs to match the generic VBIOS header. *
457 *********************************************************************/
458u32 map_oprom_vendev(u32 vendev)
459{
460 u32 new_vendev;
Richard Spiegel9247e862019-06-28 09:18:47 -0700461
462 if ((vendev >= 0x100298e0) && (vendev <= 0x100298ef))
463 new_vendev = 0x100298e0;
464 else if ((vendev >= 0x10029870) && (vendev <= 0x1002987f))
465 new_vendev = 0x10029870;
466 else
467 new_vendev = vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600468
469 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600470 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
471 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600472
473 return new_vendev;
474}
Marshall Dawson2942db62017-12-14 10:00:27 -0700475
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700476__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
477
Marshall Dawson2942db62017-12-14 10:00:27 -0700478void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
479{
Martin Roth50f2e4c2018-10-29 11:16:53 -0600480 const struct device *dev = SOC_IOMMU_DEV;
481 params->IommuSupport = dev && dev->enabled;
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700482 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700483}
484
485void SetNbMidParams(GNB_MID_CONFIGURATION *params)
486{
487 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
488 params->iGpuVgaMode = 0;
489 params->GnbIoapicAddress = IO_APIC2_ADDR;
490}
Furquan Shaikh91a7abf2020-04-27 18:48:48 -0700491
492void hda_soc_ssdt_quirks(const struct device *dev)
493{
494 const char *scope = acpi_device_path(dev);
495 static const struct fieldlist list[] = {
496 FIELDLIST_OFFSET(0x42),
497 FIELDLIST_NAMESTR("NSDI", 1),
498 FIELDLIST_NAMESTR("NSDO", 1),
499 FIELDLIST_NAMESTR("NSEN", 1),
500 };
501 struct opregion opreg = OPREGION("AZPD", PCI_CONFIG, 0x0, 0x100);
502
503 assert(scope);
504
505 acpigen_write_scope(scope);
506
507 /*
508 * OperationRegion(AZPD, PCI_Config, 0x00, 0x100)
509 * Field (AZPD, AnyAcc, NoLock, Preserve) {
510 * Offset (0x42),
511 * NSDI, 1,
512 * NSDO, 1,
513 * NSEN, 1,
514 * }
515 */
516 acpigen_write_opregion(&opreg);
517 acpigen_write_field(opreg.name, list, ARRAY_SIZE(list),
518 FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
519
520 /*
521 * Method (_INI, 0, NotSerialized) {
522 * If (LEqual (OSVR, 0x03)) {
523 * Store (Zero, NSEN)
524 * Store (One, NSDO)
525 * Store (One, NSDI)
526 * }
527 * }
528 */
529 acpigen_write_method("_INI", 0);
530
531 acpigen_write_if_lequal_namestr_int("OSVR", 0x03);
532
Furquan Shaikhac204ba2021-02-19 10:23:17 -0800533 acpigen_write_store_op_to_namestr(ZERO_OP, "NSEN");
534 acpigen_write_store_op_to_namestr(ONE_OP, "NSDO");
535 acpigen_write_store_op_to_namestr(ONE_OP, "NSDI");
Furquan Shaikh91a7abf2020-04-27 18:48:48 -0700536
537 acpigen_pop_len(); /* If */
538
539 acpigen_pop_len(); /* Method _INI */
540
541 acpigen_pop_len(); /* Scope */
542}