blob: 25e55a6bb5e4d0737bbee579808749015216d84c [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>
Marshall Dawson2942db62017-12-14 10:00:27 -070021#include <agesa_headers.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060022#include <soc/cpu.h>
Marc Jones1587dc82017-05-15 18:55:11 -060023#include <soc/northbridge.h>
Marshall Dawson38bded02017-09-01 09:54:48 -060024#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070025#include <soc/iomap.h>
Marc Jones1587dc82017-05-15 18:55:11 -060026#include <stdint.h>
Marc Jones1587dc82017-05-15 18:55:11 -060027#include <string.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060028#include <arch/bert_storage.h>
Marc Jones1587dc82017-05-15 18:55:11 -060029
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{
Marc Jonesd6a82002018-03-31 22:46:57 -060059 struct resource *res;
60
Marc Jones1587dc82017-05-15 18:55:11 -060061 /*
62 * This MMCONF resource must be reserved in the PCI domain.
63 * It is not honored by the coreboot resource allocator if it is in
64 * the CPU_CLUSTER.
65 */
Aaron Durbin3173d442017-11-03 12:14:25 -060066 mmconf_resource(dev, MMIO_CONF_BASE);
Marc Jonesd6a82002018-03-31 22:46:57 -060067
68 /* NB IOAPIC2 resource */
69 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
70 res->base = IO_APIC2_ADDR;
71 res->size = 0x00001000;
72 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Marc Jones1587dc82017-05-15 18:55:11 -060073}
74
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070075static void set_resource(struct device *dev, struct resource *res, u32 nodeid)
Marc Jones1587dc82017-05-15 18:55:11 -060076{
77 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060078 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -060079 char buf[50];
80
81 /* Make certain the resource has actually been set */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070082 if (!(res->flags & IORESOURCE_ASSIGNED))
Marc Jones1587dc82017-05-15 18:55:11 -060083 return;
84
85 /* If I have already stored this resource don't worry about it */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070086 if (res->flags & IORESOURCE_STORED)
Marc Jones1587dc82017-05-15 18:55:11 -060087 return;
88
89 /* Only handle PCI memory and IO resources */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070090 if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Marc Jones1587dc82017-05-15 18:55:11 -060091 return;
92
93 /* Ensure I am actually looking at a resource of function 1 */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070094 if ((res->index & 0xffff) < 0x1000)
Marc Jones1587dc82017-05-15 18:55:11 -060095 return;
96
97 /* Get the base address */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070098 rbase = res->base;
Marc Jones1587dc82017-05-15 18:55:11 -060099
100 /* Get the limit (rounded up) */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700101 rend = resource_end(res);
Marc Jones1587dc82017-05-15 18:55:11 -0600102
103 /* Get the register and link */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700104 reg = res->index & 0xfff; /* 4k */
105 link_num = IOINDEX_LINK(res->index);
Marc Jones1587dc82017-05-15 18:55:11 -0600106
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700107 if (res->flags & IORESOURCE_IO)
Marc Jones1587dc82017-05-15 18:55:11 -0600108 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700109 else if (res->flags & IORESOURCE_MEM)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600110 set_mmio_addr_reg(nodeid, link_num, reg,
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700111 (res->index >> 24), rbase >> 8, rend >> 8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600112
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700113 res->flags |= IORESOURCE_STORED;
Marc Jones1587dc82017-05-15 18:55:11 -0600114 snprintf(buf, sizeof(buf), " <node %x link %x>",
115 nodeid, link_num);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700116 report_resource_stored(dev, res, buf);
Marc Jones1587dc82017-05-15 18:55:11 -0600117}
118
119/**
120 * I tried to reuse the resource allocation code in set_resource()
121 * but it is too difficult to deal with the resource allocation magic.
122 */
123
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200124static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600125{
126 struct bus *link;
127
128 /* find out which link the VGA card is connected,
129 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600130 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600131 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
132 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600133
134 /* no VGA card installed */
135 if (link == NULL)
136 return;
137
Marshall Dawsone2697de2017-09-06 10:46:36 -0600138 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600139 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700140 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
Marc Jones1587dc82017-05-15 18:55:11 -0600141}
142
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200143static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600144{
145 struct bus *bus;
146 struct resource *res;
147
Marc Jones1587dc82017-05-15 18:55:11 -0600148 /* do we need this? */
149 create_vga_resource(dev);
150
151 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600152 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600153 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600154
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600155 for (bus = dev->link_list ; bus ; bus = bus->next)
156 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600157 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600158}
159
160static void northbridge_init(struct device *dev)
161{
Marc Jonesd6a82002018-03-31 22:46:57 -0600162 setup_ioapic((u8 *)IO_APIC2_ADDR, CONFIG_MAX_CPUS+1);
Marc Jones1587dc82017-05-15 18:55:11 -0600163}
164
Marshall Dawsone09caf62019-05-02 17:58:12 -0600165unsigned long acpi_fill_mcfg(unsigned long current)
166{
167
168 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
169 CONFIG_MMCONF_BASE_ADDRESS,
170 0,
171 0,
Arthur Heymans69c0b192019-10-08 10:04:51 +0200172 CONFIG_MMCONF_BUS_NUMBER - 1);
Marshall Dawsone09caf62019-05-02 17:58:12 -0600173
174 return current;
175}
176
Marc Jones1587dc82017-05-15 18:55:11 -0600177static unsigned long acpi_fill_hest(acpi_hest_t *hest)
178{
179 void *addr, *current;
180
181 /* Skip the HEST header. */
182 current = (void *)(hest + 1);
183
184 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
185 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600186 current += acpi_create_hest_error_source(hest, current, 0,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700187 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600188
189 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
190 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600191 current += acpi_create_hest_error_source(hest, current, 1,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700192 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600193
194 return (unsigned long)current;
195}
196
Furquan Shaikh7536a392020-04-24 21:59:21 -0700197static void northbridge_fill_ssdt_generator(const struct device *device)
Marc Jones1587dc82017-05-15 18:55:11 -0600198{
199 msr_t msr;
200 char pscope[] = "\\_SB.PCI0";
201
202 acpigen_write_scope(pscope);
203 msr = rdmsr(TOP_MEM);
204 acpigen_write_name_dword("TOM1", msr.lo);
205 msr = rdmsr(TOP_MEM2);
206 /*
207 * Since XP only implements parts of ACPI 2.0, we can't use a qword
208 * here.
209 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
210 * slide 22ff.
211 * Shift value right by 20 bit to make it fit into 32bit,
212 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
213 */
214 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
215 acpigen_pop_len();
216}
217
Michał Żygowski9550e972020-03-20 13:56:46 +0100218static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
219{
220 unsigned int len = ssdt->length - sizeof(acpi_header_t);
221 unsigned int i;
222
223 for (i = sizeof(acpi_header_t); i < len; i++) {
224 /* Search for _PR_ scope and replace it with _SB_ */
225 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
226 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
227 }
228 /* Recalculate checksum */
229 ssdt->checksum = 0;
230 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
231}
232
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700233static unsigned long agesa_write_acpi_tables(const struct device *device,
Marc Jones1587dc82017-05-15 18:55:11 -0600234 unsigned long current,
235 acpi_rsdp_t *rsdp)
236{
237 acpi_srat_t *srat;
238 acpi_slit_t *slit;
239 acpi_header_t *ssdt;
240 acpi_header_t *alib;
241 acpi_header_t *ivrs;
242 acpi_hest_t *hest;
Marshall Dawson653f7602018-09-04 13:25:39 -0600243 acpi_bert_t *bert;
Marc Jones1587dc82017-05-15 18:55:11 -0600244
245 /* HEST */
246 current = ALIGN(current, 8);
247 hest = (acpi_hest_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700248 acpi_write_hest(hest, acpi_fill_hest);
Marc Jones1587dc82017-05-15 18:55:11 -0600249 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700250 current += hest->header.length;
Marc Jones1587dc82017-05-15 18:55:11 -0600251
Marshall Dawson653f7602018-09-04 13:25:39 -0600252 /* BERT */
Julius Wernercd49cce2019-03-05 16:53:33 -0800253 if (CONFIG(ACPI_BERT) && bert_errors_present()) {
Marshall Dawson653f7602018-09-04 13:25:39 -0600254 /* Skip the table if no errors are present. ACPI driver reports
255 * a table with a 0-length region:
256 * BERT: [Firmware Bug]: table invalid.
257 */
258 void *rgn;
259 size_t size;
260 bert_errors_region(&rgn, &size);
261 if (!rgn) {
262 printk(BIOS_ERR, "Error: Can't find BERT storage area\n");
263 } else {
264 current = ALIGN(current, 8);
265 bert = (acpi_bert_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700266 acpi_write_bert(bert, (uintptr_t)rgn, size);
Marshall Dawson653f7602018-09-04 13:25:39 -0600267 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700268 current += bert->header.length;
Marshall Dawson653f7602018-09-04 13:25:39 -0600269 }
270 }
271
Marc Jones1587dc82017-05-15 18:55:11 -0600272 current = ALIGN(current, 8);
273 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
274 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
275 if (ivrs != NULL) {
276 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600277 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600278 current += ivrs->length;
279 acpi_add_table(rsdp, ivrs);
280 } else {
281 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
282 }
283
284 /* SRAT */
285 current = ALIGN(current, 8);
286 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600287 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600288 if (srat != NULL) {
289 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600290 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600291 current += srat->header.length;
292 acpi_add_table(rsdp, srat);
293 } else {
294 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
295 }
296
297 /* SLIT */
298 current = ALIGN(current, 8);
299 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600300 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600301 if (slit != NULL) {
302 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600303 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600304 current += slit->header.length;
305 acpi_add_table(rsdp, slit);
306 } else {
307 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
308 }
309
310 /* ALIB */
311 current = ALIGN(current, 16);
312 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600313 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600314 if (alib != NULL) {
315 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600316 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600317 current += alib->length;
318 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600319 } else {
320 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
321 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600322 }
323
Marc Jones1587dc82017-05-15 18:55:11 -0600324 current = ALIGN(current, 16);
325 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600326 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Marc Jones1587dc82017-05-15 18:55:11 -0600327 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100328 patch_ssdt_processor_scope(ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600329 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600330 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600331 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600332 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600333 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
334 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600335 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600336
337 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
338 return current;
339}
340
341static struct device_operations northbridge_operations = {
342 .read_resources = read_resources,
343 .set_resources = set_resources,
344 .enable_resources = pci_dev_enable_resources,
345 .init = northbridge_init,
Nico Huber68680dd2020-03-31 17:34:52 +0200346 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Marc Jones1587dc82017-05-15 18:55:11 -0600347 .write_acpi_tables = agesa_write_acpi_tables,
Marc Jones1587dc82017-05-15 18:55:11 -0600348};
349
Richard Spiegel9247e862019-06-28 09:18:47 -0700350static const unsigned short pci_device_ids[] = {
351 PCI_DEVICE_ID_AMD_15H_MODEL_606F_NB_HT,
352 PCI_DEVICE_ID_AMD_15H_MODEL_707F_NB_HT,
353 0 };
354
Marc Jones1587dc82017-05-15 18:55:11 -0600355static const struct pci_driver family15_northbridge __pci_driver = {
356 .ops = &northbridge_operations,
357 .vendor = PCI_VENDOR_ID_AMD,
Richard Spiegel9247e862019-06-28 09:18:47 -0700358 .devices = pci_device_ids,
Marc Jones1587dc82017-05-15 18:55:11 -0600359};
360
Marshall Dawson154239a2017-11-02 09:49:30 -0600361/*
362 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
363 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
364 * MMIO to posted. Route all I/O to the southbridge.
365 */
366void amd_initcpuio(void)
367{
368 uintptr_t topmem = bsp_topmem();
369 uintptr_t base, limit;
370
371 /* Enable legacy video routing: D18F1xF4 VGA Enable */
372 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
373
374 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
375 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
376 limit = (ALIGN_DOWN(LOCAL_APIC_ADDR - 1, 64 * KiB) >> 8) | MMIO_NP;
377 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
378 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
379
380 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
381 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
382 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
383 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
384 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
385
386 /* Route all I/O downstream */
387 base = 0 | IO_WE | IO_RE;
388 limit = ALIGN_DOWN(0xffff, 4 * KiB);
389 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
390 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
391}
392
Marc Jones1587dc82017-05-15 18:55:11 -0600393void fam15_finalize(void *chip_info)
394{
Marc Jones1587dc82017-05-15 18:55:11 -0600395 u32 value;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700396
397 /* TODO: move IOAPIC code to dsdt.asl */
398 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_INDEX, 0);
399 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_DATA, 5);
Marc Jones1587dc82017-05-15 18:55:11 -0600400
401 /* disable No Snoop */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700402 value = pci_read_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700403 value &= ~HDA_NO_SNOOP_EN;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700404 pci_write_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600405}
406
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200407void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600408{
Marc Jones1587dc82017-05-15 18:55:11 -0600409 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkki9e591c42021-01-09 12:37:25 +0200410 if (!acpi_is_wakeup_s3())
Kyösti Mälkki6e512c42018-06-14 06:57:05 +0300411 do_agesawrapper(AMD_INIT_MID, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600412}
413
Furquan Shaikhfc752b62020-05-13 12:14:11 -0700414void domain_read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600415{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700416 uint64_t uma_base = get_uma_base();
417 uint32_t uma_size = get_uma_size();
418 uint32_t mem_useable = (uintptr_t)cbmem_top();
419 msr_t tom = rdmsr(TOP_MEM);
420 msr_t high_tom = rdmsr(TOP_MEM2);
421 uint64_t high_mem_useable;
422 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600423
Furquan Shaikhfc752b62020-05-13 12:14:11 -0700424 pci_domain_read_resources(dev);
425
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700426 /* 0x0 -> 0x9ffff */
427 ram_resource(dev, idx++, 0, 0xa0000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600428
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700429 /* 0xa0000 -> 0xbffff: legacy VGA */
430 mmio_resource(dev, idx++, 0xa0000 / KiB, 0x20000 / KiB);
431
432 /* 0xc0000 -> 0xfffff: Option ROM */
433 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, 0x40000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600434
Marshall Dawson29f1b742017-09-06 14:59:45 -0600435 /*
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700436 * 0x100000 (1MiB) -> low top useable RAM
437 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600438 */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700439 ram_resource(dev, idx++, (1 * MiB) / KiB,
440 (mem_useable - (1 * MiB)) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600441
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700442 /* Low top useable RAM -> Low top RAM (bottom pci mmio hole) */
443 reserved_ram_resource(dev, idx++, mem_useable / KiB,
444 (tom.lo - mem_useable) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600445
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700446 /* If there is memory above 4GiB */
447 if (high_tom.hi) {
448 /* 4GiB -> high top useable */
449 if (uma_base >= (4ull * GiB))
450 high_mem_useable = uma_base;
451 else
452 high_mem_useable = ((uint64_t)high_tom.lo |
453 ((uint64_t)high_tom.hi << 32));
Marc Jones1587dc82017-05-15 18:55:11 -0600454
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700455 ram_resource(dev, idx++, (4ull * GiB) / KiB,
456 ((high_mem_useable - (4ull * GiB)) / KiB));
Marc Jones1587dc82017-05-15 18:55:11 -0600457
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700458 /* High top useable RAM -> high top RAM */
459 if (uma_base >= (4ull * GiB)) {
460 reserved_ram_resource(dev, idx++, uma_base / KiB,
461 uma_size / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600462 }
Marc Jones1587dc82017-05-15 18:55:11 -0600463 }
Marc Jones1587dc82017-05-15 18:55:11 -0600464}
465
Marc Jones1587dc82017-05-15 18:55:11 -0600466/*********************************************************************
467 * Change the vendor / device IDs to match the generic VBIOS header. *
468 *********************************************************************/
469u32 map_oprom_vendev(u32 vendev)
470{
471 u32 new_vendev;
Richard Spiegel9247e862019-06-28 09:18:47 -0700472
473 if ((vendev >= 0x100298e0) && (vendev <= 0x100298ef))
474 new_vendev = 0x100298e0;
475 else if ((vendev >= 0x10029870) && (vendev <= 0x1002987f))
476 new_vendev = 0x10029870;
477 else
478 new_vendev = vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600479
480 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600481 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
482 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600483
484 return new_vendev;
485}
Marshall Dawson2942db62017-12-14 10:00:27 -0700486
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700487__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
488
Marshall Dawson2942db62017-12-14 10:00:27 -0700489void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
490{
Martin Roth50f2e4c2018-10-29 11:16:53 -0600491 const struct device *dev = SOC_IOMMU_DEV;
492 params->IommuSupport = dev && dev->enabled;
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700493 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700494}
495
496void SetNbMidParams(GNB_MID_CONFIGURATION *params)
497{
498 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
499 params->iGpuVgaMode = 0;
500 params->GnbIoapicAddress = IO_APIC2_ADDR;
501}
Furquan Shaikh91a7abf2020-04-27 18:48:48 -0700502
503void hda_soc_ssdt_quirks(const struct device *dev)
504{
505 const char *scope = acpi_device_path(dev);
506 static const struct fieldlist list[] = {
507 FIELDLIST_OFFSET(0x42),
508 FIELDLIST_NAMESTR("NSDI", 1),
509 FIELDLIST_NAMESTR("NSDO", 1),
510 FIELDLIST_NAMESTR("NSEN", 1),
511 };
512 struct opregion opreg = OPREGION("AZPD", PCI_CONFIG, 0x0, 0x100);
513
514 assert(scope);
515
516 acpigen_write_scope(scope);
517
518 /*
519 * OperationRegion(AZPD, PCI_Config, 0x00, 0x100)
520 * Field (AZPD, AnyAcc, NoLock, Preserve) {
521 * Offset (0x42),
522 * NSDI, 1,
523 * NSDO, 1,
524 * NSEN, 1,
525 * }
526 */
527 acpigen_write_opregion(&opreg);
528 acpigen_write_field(opreg.name, list, ARRAY_SIZE(list),
529 FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
530
531 /*
532 * Method (_INI, 0, NotSerialized) {
533 * If (LEqual (OSVR, 0x03)) {
534 * Store (Zero, NSEN)
535 * Store (One, NSDO)
536 * Store (One, NSDI)
537 * }
538 * }
539 */
540 acpigen_write_method("_INI", 0);
541
542 acpigen_write_if_lequal_namestr_int("OSVR", 0x03);
543
544 acpigen_write_store_op_to_namestr(ONE_OP, "NSEN");
545 acpigen_write_store_op_to_namestr(ZERO_OP, "NSDO");
546 acpigen_write_store_op_to_namestr(ZERO_OP, "NSDI");
547
548 acpigen_pop_len(); /* If */
549
550 acpigen_pop_len(); /* Method _INI */
551
552 acpigen_pop_len(); /* Scope */
553}