blob: 135abefe91c5ae047da3462e38600674744efd9a [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Marc Jones1587dc82017-05-15 18:55:11 -06003
Furquan Shaikh91a7abf2020-04-27 18:48:48 -07004#include <assert.h>
Michał Żygowskif65c1e42019-12-01 18:14:39 +01005#include <amdblocks/biosram.h>
Furquan Shaikh91a7abf2020-04-27 18:48:48 -07006#include <amdblocks/hda.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.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>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020016#include <cpu/amd/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>
Marshall Dawson8f2a7e02017-11-01 11:44:48 -060020#include <romstage_handoff.h>
Richard Spiegel0ad74ac2017-12-08 16:53:29 -070021#include <amdblocks/agesawrapper.h>
22#include <amdblocks/agesawrapper_call.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>
Marshall Dawson653f7602018-09-04 13:25:39 -060030#include <arch/bert_storage.h>
Marc Jones1587dc82017-05-15 18:55:11 -060031
Elyes HAOUASc3385072019-03-21 15:38:06 +010032#include "chip.h"
33
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020034static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Marc Jones1587dc82017-05-15 18:55:11 -060035 u32 io_min, u32 io_max)
36{
37 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060038
Marshall Dawson4e101ad2017-06-15 12:17:38 -060039 /* io range allocation. Limit */
40 tempreg = (nodeid & 0xf) | ((nodeid & 0x30) << (8 - 4)) | (linkn << 4)
41 | ((io_max & 0xf0) << (12 - 4));
Richard Spiegel41baf0c2018-10-22 13:57:18 -070042 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060043 tempreg = 3 | ((io_min & 0xf0) << (12 - 4)); /* base: ISA and VGA ? */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070044 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060045}
46
Marshall Dawson4e101ad2017-06-15 12:17:38 -060047static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index,
48 u32 mmio_min, u32 mmio_max)
Marc Jones1587dc82017-05-15 18:55:11 -060049{
50 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060051
Marshall Dawson4e101ad2017-06-15 12:17:38 -060052 /* io range allocation. Limit */
53 tempreg = (nodeid & 0xf) | (linkn << 4) | (mmio_max & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070054 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060055 tempreg = 3 | (nodeid & 0x30) | (mmio_min & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070056 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060057}
58
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020059static void read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060060{
Marc Jonesd6a82002018-03-31 22:46:57 -060061 struct resource *res;
62
Marc Jones1587dc82017-05-15 18:55:11 -060063 /*
64 * This MMCONF resource must be reserved in the PCI domain.
65 * It is not honored by the coreboot resource allocator if it is in
66 * the CPU_CLUSTER.
67 */
Aaron Durbin3173d442017-11-03 12:14:25 -060068 mmconf_resource(dev, MMIO_CONF_BASE);
Marc Jonesd6a82002018-03-31 22:46:57 -060069
70 /* NB IOAPIC2 resource */
71 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
72 res->base = IO_APIC2_ADDR;
73 res->size = 0x00001000;
74 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Marc Jones1587dc82017-05-15 18:55:11 -060075}
76
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070077static void set_resource(struct device *dev, struct resource *res, u32 nodeid)
Marc Jones1587dc82017-05-15 18:55:11 -060078{
79 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060080 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -060081 char buf[50];
82
83 /* Make certain the resource has actually been set */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070084 if (!(res->flags & IORESOURCE_ASSIGNED))
Marc Jones1587dc82017-05-15 18:55:11 -060085 return;
86
87 /* If I have already stored this resource don't worry about it */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070088 if (res->flags & IORESOURCE_STORED)
Marc Jones1587dc82017-05-15 18:55:11 -060089 return;
90
91 /* Only handle PCI memory and IO resources */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070092 if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Marc Jones1587dc82017-05-15 18:55:11 -060093 return;
94
95 /* Ensure I am actually looking at a resource of function 1 */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -070096 if ((res->index & 0xffff) < 0x1000)
Marc Jones1587dc82017-05-15 18:55:11 -060097 return;
98
99 /* Get the base address */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700100 rbase = res->base;
Marc Jones1587dc82017-05-15 18:55:11 -0600101
102 /* Get the limit (rounded up) */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700103 rend = resource_end(res);
Marc Jones1587dc82017-05-15 18:55:11 -0600104
105 /* Get the register and link */
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700106 reg = res->index & 0xfff; /* 4k */
107 link_num = IOINDEX_LINK(res->index);
Marc Jones1587dc82017-05-15 18:55:11 -0600108
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700109 if (res->flags & IORESOURCE_IO)
Marc Jones1587dc82017-05-15 18:55:11 -0600110 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700111 else if (res->flags & IORESOURCE_MEM)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600112 set_mmio_addr_reg(nodeid, link_num, reg,
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700113 (res->index >> 24), rbase >> 8, rend >> 8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600114
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700115 res->flags |= IORESOURCE_STORED;
Marc Jones1587dc82017-05-15 18:55:11 -0600116 snprintf(buf, sizeof(buf), " <node %x link %x>",
117 nodeid, link_num);
Richard Spiegel2b7cd1d2018-10-22 14:39:37 -0700118 report_resource_stored(dev, res, buf);
Marc Jones1587dc82017-05-15 18:55:11 -0600119}
120
121/**
122 * I tried to reuse the resource allocation code in set_resource()
123 * but it is too difficult to deal with the resource allocation magic.
124 */
125
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200126static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600127{
128 struct bus *link;
129
130 /* find out which link the VGA card is connected,
131 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600132 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600133 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
134 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600135
136 /* no VGA card installed */
137 if (link == NULL)
138 return;
139
Marshall Dawsone2697de2017-09-06 10:46:36 -0600140 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600141 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700142 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
Marc Jones1587dc82017-05-15 18:55:11 -0600143}
144
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200145static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600146{
147 struct bus *bus;
148 struct resource *res;
149
150
151 /* do we need this? */
152 create_vga_resource(dev);
153
154 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600155 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600156 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600157
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600158 for (bus = dev->link_list ; bus ; bus = bus->next)
159 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600160 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600161}
162
163static void northbridge_init(struct device *dev)
164{
Marc Jonesd6a82002018-03-31 22:46:57 -0600165 setup_ioapic((u8 *)IO_APIC2_ADDR, CONFIG_MAX_CPUS+1);
Marc Jones1587dc82017-05-15 18:55:11 -0600166}
167
Marshall Dawsone09caf62019-05-02 17:58:12 -0600168unsigned long acpi_fill_mcfg(unsigned long current)
169{
170
171 current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *)current,
172 CONFIG_MMCONF_BASE_ADDRESS,
173 0,
174 0,
175 CONFIG_MMCONF_BUS_NUMBER);
176
177 return current;
178}
179
Marc Jones1587dc82017-05-15 18:55:11 -0600180static unsigned long acpi_fill_hest(acpi_hest_t *hest)
181{
182 void *addr, *current;
183
184 /* Skip the HEST header. */
185 current = (void *)(hest + 1);
186
187 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
188 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600189 current += acpi_create_hest_error_source(hest, current, 0,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700190 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600191
192 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
193 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600194 current += acpi_create_hest_error_source(hest, current, 1,
Richard Spiegel271b8a52018-11-06 16:32:28 -0700195 (void *)((u32)addr + 2), *(uint16_t *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600196
197 return (unsigned long)current;
198}
199
Furquan Shaikh7536a392020-04-24 21:59:21 -0700200static void northbridge_fill_ssdt_generator(const struct device *device)
Marc Jones1587dc82017-05-15 18:55:11 -0600201{
202 msr_t msr;
203 char pscope[] = "\\_SB.PCI0";
204
205 acpigen_write_scope(pscope);
206 msr = rdmsr(TOP_MEM);
207 acpigen_write_name_dword("TOM1", msr.lo);
208 msr = rdmsr(TOP_MEM2);
209 /*
210 * Since XP only implements parts of ACPI 2.0, we can't use a qword
211 * here.
212 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
213 * slide 22ff.
214 * Shift value right by 20 bit to make it fit into 32bit,
215 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
216 */
217 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
218 acpigen_pop_len();
219}
220
Michał Żygowski9550e972020-03-20 13:56:46 +0100221static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
222{
223 unsigned int len = ssdt->length - sizeof(acpi_header_t);
224 unsigned int i;
225
226 for (i = sizeof(acpi_header_t); i < len; i++) {
227 /* Search for _PR_ scope and replace it with _SB_ */
228 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
229 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
230 }
231 /* Recalculate checksum */
232 ssdt->checksum = 0;
233 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
234}
235
236
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700237static unsigned long agesa_write_acpi_tables(const struct device *device,
Marc Jones1587dc82017-05-15 18:55:11 -0600238 unsigned long current,
239 acpi_rsdp_t *rsdp)
240{
241 acpi_srat_t *srat;
242 acpi_slit_t *slit;
243 acpi_header_t *ssdt;
244 acpi_header_t *alib;
245 acpi_header_t *ivrs;
246 acpi_hest_t *hest;
Marshall Dawson653f7602018-09-04 13:25:39 -0600247 acpi_bert_t *bert;
Marc Jones1587dc82017-05-15 18:55:11 -0600248
249 /* HEST */
250 current = ALIGN(current, 8);
251 hest = (acpi_hest_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700252 acpi_write_hest(hest, acpi_fill_hest);
Marc Jones1587dc82017-05-15 18:55:11 -0600253 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700254 current += hest->header.length;
Marc Jones1587dc82017-05-15 18:55:11 -0600255
Marshall Dawson653f7602018-09-04 13:25:39 -0600256 /* BERT */
Julius Wernercd49cce2019-03-05 16:53:33 -0800257 if (CONFIG(ACPI_BERT) && bert_errors_present()) {
Marshall Dawson653f7602018-09-04 13:25:39 -0600258 /* Skip the table if no errors are present. ACPI driver reports
259 * a table with a 0-length region:
260 * BERT: [Firmware Bug]: table invalid.
261 */
262 void *rgn;
263 size_t size;
264 bert_errors_region(&rgn, &size);
265 if (!rgn) {
266 printk(BIOS_ERR, "Error: Can't find BERT storage area\n");
267 } else {
268 current = ALIGN(current, 8);
269 bert = (acpi_bert_t *)current;
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700270 acpi_write_bert(bert, (uintptr_t)rgn, size);
Marshall Dawson653f7602018-09-04 13:25:39 -0600271 acpi_add_table(rsdp, (void *)current);
Richard Spiegel6a9e6cd2018-11-30 10:53:40 -0700272 current += bert->header.length;
Marshall Dawson653f7602018-09-04 13:25:39 -0600273 }
274 }
275
Marc Jones1587dc82017-05-15 18:55:11 -0600276 current = ALIGN(current, 8);
277 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
278 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
279 if (ivrs != NULL) {
280 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600281 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600282 current += ivrs->length;
283 acpi_add_table(rsdp, ivrs);
284 } else {
285 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
286 }
287
288 /* SRAT */
289 current = ALIGN(current, 8);
290 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600291 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600292 if (srat != NULL) {
293 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600294 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600295 current += srat->header.length;
296 acpi_add_table(rsdp, srat);
297 } else {
298 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
299 }
300
301 /* SLIT */
302 current = ALIGN(current, 8);
303 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600304 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600305 if (slit != NULL) {
306 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600307 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600308 current += slit->header.length;
309 acpi_add_table(rsdp, slit);
310 } else {
311 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
312 }
313
314 /* ALIB */
315 current = ALIGN(current, 16);
316 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600317 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600318 if (alib != NULL) {
319 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600320 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600321 current += alib->length;
322 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600323 } else {
324 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
325 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600326 }
327
Marc Jones1587dc82017-05-15 18:55:11 -0600328 current = ALIGN(current, 16);
329 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600330 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Marc Jones1587dc82017-05-15 18:55:11 -0600331 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100332 patch_ssdt_processor_scope(ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600333 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600334 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600335 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600336 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600337 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
338 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600339 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600340
341 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
342 return current;
343}
344
345static struct device_operations northbridge_operations = {
346 .read_resources = read_resources,
347 .set_resources = set_resources,
348 .enable_resources = pci_dev_enable_resources,
349 .init = northbridge_init,
Nico Huber68680dd2020-03-31 17:34:52 +0200350 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Marc Jones1587dc82017-05-15 18:55:11 -0600351 .write_acpi_tables = agesa_write_acpi_tables,
Marc Jones1587dc82017-05-15 18:55:11 -0600352};
353
Richard Spiegel9247e862019-06-28 09:18:47 -0700354static const unsigned short pci_device_ids[] = {
355 PCI_DEVICE_ID_AMD_15H_MODEL_606F_NB_HT,
356 PCI_DEVICE_ID_AMD_15H_MODEL_707F_NB_HT,
357 0 };
358
Marc Jones1587dc82017-05-15 18:55:11 -0600359static const struct pci_driver family15_northbridge __pci_driver = {
360 .ops = &northbridge_operations,
361 .vendor = PCI_VENDOR_ID_AMD,
Richard Spiegel9247e862019-06-28 09:18:47 -0700362 .devices = pci_device_ids,
Marc Jones1587dc82017-05-15 18:55:11 -0600363};
364
Marshall Dawson154239a2017-11-02 09:49:30 -0600365/*
366 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
367 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
368 * MMIO to posted. Route all I/O to the southbridge.
369 */
370void amd_initcpuio(void)
371{
372 uintptr_t topmem = bsp_topmem();
373 uintptr_t base, limit;
374
375 /* Enable legacy video routing: D18F1xF4 VGA Enable */
376 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
377
378 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
379 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
380 limit = (ALIGN_DOWN(LOCAL_APIC_ADDR - 1, 64 * KiB) >> 8) | MMIO_NP;
381 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
382 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
383
384 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
385 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
386 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
387 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
388 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
389
390 /* Route all I/O downstream */
391 base = 0 | IO_WE | IO_RE;
392 limit = ALIGN_DOWN(0xffff, 4 * KiB);
393 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
394 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
395}
396
Marc Jones1587dc82017-05-15 18:55:11 -0600397void fam15_finalize(void *chip_info)
398{
Marc Jones1587dc82017-05-15 18:55:11 -0600399 u32 value;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700400
401 /* TODO: move IOAPIC code to dsdt.asl */
402 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_INDEX, 0);
403 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_DATA, 5);
Marc Jones1587dc82017-05-15 18:55:11 -0600404
405 /* disable No Snoop */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700406 value = pci_read_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700407 value &= ~HDA_NO_SNOOP_EN;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700408 pci_write_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600409}
410
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200411void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600412{
Marc Jones1587dc82017-05-15 18:55:11 -0600413 /* Must be called after PCI enumeration and resource allocation */
Marshall Dawson8f2a7e02017-11-01 11:44:48 -0600414 if (!romstage_handoff_is_resume())
Kyösti Mälkki6e512c42018-06-14 06:57:05 +0300415 do_agesawrapper(AMD_INIT_MID, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600416}
417
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200418void domain_set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600419{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700420 uint64_t uma_base = get_uma_base();
421 uint32_t uma_size = get_uma_size();
422 uint32_t mem_useable = (uintptr_t)cbmem_top();
423 msr_t tom = rdmsr(TOP_MEM);
424 msr_t high_tom = rdmsr(TOP_MEM2);
425 uint64_t high_mem_useable;
426 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600427
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700428 /* 0x0 -> 0x9ffff */
429 ram_resource(dev, idx++, 0, 0xa0000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600430
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700431 /* 0xa0000 -> 0xbffff: legacy VGA */
432 mmio_resource(dev, idx++, 0xa0000 / KiB, 0x20000 / KiB);
433
434 /* 0xc0000 -> 0xfffff: Option ROM */
435 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, 0x40000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600436
Marshall Dawson29f1b742017-09-06 14:59:45 -0600437 /*
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700438 * 0x100000 (1MiB) -> low top useable RAM
439 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600440 */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700441 ram_resource(dev, idx++, (1 * MiB) / KiB,
442 (mem_useable - (1 * MiB)) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600443
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700444 /* Low top useable RAM -> Low top RAM (bottom pci mmio hole) */
445 reserved_ram_resource(dev, idx++, mem_useable / KiB,
446 (tom.lo - mem_useable) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600447
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700448 /* If there is memory above 4GiB */
449 if (high_tom.hi) {
450 /* 4GiB -> high top useable */
451 if (uma_base >= (4ull * GiB))
452 high_mem_useable = uma_base;
453 else
454 high_mem_useable = ((uint64_t)high_tom.lo |
455 ((uint64_t)high_tom.hi << 32));
Marc Jones1587dc82017-05-15 18:55:11 -0600456
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700457 ram_resource(dev, idx++, (4ull * GiB) / KiB,
458 ((high_mem_useable - (4ull * GiB)) / KiB));
Marc Jones1587dc82017-05-15 18:55:11 -0600459
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700460 /* High top useable RAM -> high top RAM */
461 if (uma_base >= (4ull * GiB)) {
462 reserved_ram_resource(dev, idx++, uma_base / KiB,
463 uma_size / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600464 }
Marc Jones1587dc82017-05-15 18:55:11 -0600465 }
466
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700467 assign_resources(dev->link_list);
Marc Jones1587dc82017-05-15 18:55:11 -0600468}
469
Marc Jones1587dc82017-05-15 18:55:11 -0600470/*********************************************************************
471 * Change the vendor / device IDs to match the generic VBIOS header. *
472 *********************************************************************/
473u32 map_oprom_vendev(u32 vendev)
474{
475 u32 new_vendev;
Richard Spiegel9247e862019-06-28 09:18:47 -0700476
477 if ((vendev >= 0x100298e0) && (vendev <= 0x100298ef))
478 new_vendev = 0x100298e0;
479 else if ((vendev >= 0x10029870) && (vendev <= 0x1002987f))
480 new_vendev = 0x10029870;
481 else
482 new_vendev = vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600483
484 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600485 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
486 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600487
488 return new_vendev;
489}
Marshall Dawson2942db62017-12-14 10:00:27 -0700490
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700491__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
492
Marshall Dawson2942db62017-12-14 10:00:27 -0700493void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
494{
Martin Roth50f2e4c2018-10-29 11:16:53 -0600495 const struct device *dev = SOC_IOMMU_DEV;
496 params->IommuSupport = dev && dev->enabled;
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700497 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700498}
499
500void SetNbMidParams(GNB_MID_CONFIGURATION *params)
501{
502 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
503 params->iGpuVgaMode = 0;
504 params->GnbIoapicAddress = IO_APIC2_ADDR;
505}
Furquan Shaikh91a7abf2020-04-27 18:48:48 -0700506
507void hda_soc_ssdt_quirks(const struct device *dev)
508{
509 const char *scope = acpi_device_path(dev);
510 static const struct fieldlist list[] = {
511 FIELDLIST_OFFSET(0x42),
512 FIELDLIST_NAMESTR("NSDI", 1),
513 FIELDLIST_NAMESTR("NSDO", 1),
514 FIELDLIST_NAMESTR("NSEN", 1),
515 };
516 struct opregion opreg = OPREGION("AZPD", PCI_CONFIG, 0x0, 0x100);
517
518 assert(scope);
519
520 acpigen_write_scope(scope);
521
522 /*
523 * OperationRegion(AZPD, PCI_Config, 0x00, 0x100)
524 * Field (AZPD, AnyAcc, NoLock, Preserve) {
525 * Offset (0x42),
526 * NSDI, 1,
527 * NSDO, 1,
528 * NSEN, 1,
529 * }
530 */
531 acpigen_write_opregion(&opreg);
532 acpigen_write_field(opreg.name, list, ARRAY_SIZE(list),
533 FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
534
535 /*
536 * Method (_INI, 0, NotSerialized) {
537 * If (LEqual (OSVR, 0x03)) {
538 * Store (Zero, NSEN)
539 * Store (One, NSDO)
540 * Store (One, NSDI)
541 * }
542 * }
543 */
544 acpigen_write_method("_INI", 0);
545
546 acpigen_write_if_lequal_namestr_int("OSVR", 0x03);
547
548 acpigen_write_store_op_to_namestr(ONE_OP, "NSEN");
549 acpigen_write_store_op_to_namestr(ZERO_OP, "NSDO");
550 acpigen_write_store_op_to_namestr(ZERO_OP, "NSDI");
551
552 acpigen_pop_len(); /* If */
553
554 acpigen_pop_len(); /* Method _INI */
555
556 acpigen_pop_len(); /* Scope */
557}