blob: 5fb28c028c2e6623f35c3528fb0e2391d7004151 [file] [log] [blame]
Marc Jones1587dc82017-05-15 18:55:11 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2015-2016 Advanced Micro Devices, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16
17#include <arch/io.h>
Marc Jonesd6a82002018-03-31 22:46:57 -060018#include <arch/ioapic.h>
Marc Jones1587dc82017-05-15 18:55:11 -060019#include <arch/acpi.h>
20#include <arch/acpigen.h>
21#include <cbmem.h>
22#include <chip.h>
23#include <console/console.h>
Marc Jones1587dc82017-05-15 18:55:11 -060024#include <cpu/amd/mtrr.h>
Aaron Durbin3173d442017-11-03 12:14:25 -060025#include <cpu/amd/amdfam15.h>
Marc Jones1587dc82017-05-15 18:55:11 -060026#include <cpu/cpu.h>
Marshall Dawson154239a2017-11-02 09:49:30 -060027#include <cpu/x86/lapic_def.h>
Marshall Dawsonf82aa102017-09-20 18:01:41 -060028#include <cpu/x86/msr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020029#include <cpu/amd/msr.h>
Marc Jones1587dc82017-05-15 18:55:11 -060030#include <device/device.h>
31#include <device/pci.h>
32#include <device/pci_ids.h>
Marshall Dawson8f2a7e02017-11-01 11:44:48 -060033#include <romstage_handoff.h>
Richard Spiegel0ad74ac2017-12-08 16:53:29 -070034#include <amdblocks/agesawrapper.h>
35#include <amdblocks/agesawrapper_call.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070036#include <agesa_headers.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060037#include <soc/cpu.h>
Marc Jones1587dc82017-05-15 18:55:11 -060038#include <soc/northbridge.h>
Marshall Dawson154239a2017-11-02 09:49:30 -060039#include <soc/southbridge.h>
Marshall Dawson38bded02017-09-01 09:54:48 -060040#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070041#include <soc/iomap.h>
Marc Jones1587dc82017-05-15 18:55:11 -060042#include <stdint.h>
43#include <stdlib.h>
44#include <string.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060045#include <arch/bert_storage.h>
Marc Jones1587dc82017-05-15 18:55:11 -060046
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020047static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Marc Jones1587dc82017-05-15 18:55:11 -060048 u32 io_min, u32 io_max)
49{
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) | ((nodeid & 0x30) << (8 - 4)) | (linkn << 4)
54 | ((io_max & 0xf0) << (12 - 4));
Richard Spiegel41baf0c2018-10-22 13:57:18 -070055 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060056 tempreg = 3 | ((io_min & 0xf0) << (12 - 4)); /* base: ISA and VGA ? */
Richard Spiegel41baf0c2018-10-22 13:57:18 -070057 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060058}
59
Marshall Dawson4e101ad2017-06-15 12:17:38 -060060static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index,
61 u32 mmio_min, u32 mmio_max)
Marc Jones1587dc82017-05-15 18:55:11 -060062{
63 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060064
Marshall Dawson4e101ad2017-06-15 12:17:38 -060065 /* io range allocation. Limit */
66 tempreg = (nodeid & 0xf) | (linkn << 4) | (mmio_max & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070067 pci_write_config32(SOC_ADDR_DEV, reg + 4, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060068 tempreg = 3 | (nodeid & 0x30) | (mmio_min & 0xffffff00);
Richard Spiegel41baf0c2018-10-22 13:57:18 -070069 pci_write_config32(SOC_ADDR_DEV, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060070}
71
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020072static void read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060073{
Marc Jonesd6a82002018-03-31 22:46:57 -060074 struct resource *res;
75
Marc Jones1587dc82017-05-15 18:55:11 -060076 /*
77 * This MMCONF resource must be reserved in the PCI domain.
78 * It is not honored by the coreboot resource allocator if it is in
79 * the CPU_CLUSTER.
80 */
Aaron Durbin3173d442017-11-03 12:14:25 -060081 mmconf_resource(dev, MMIO_CONF_BASE);
Marc Jonesd6a82002018-03-31 22:46:57 -060082
83 /* NB IOAPIC2 resource */
84 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
85 res->base = IO_APIC2_ADDR;
86 res->size = 0x00001000;
87 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Marc Jones1587dc82017-05-15 18:55:11 -060088}
89
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020090static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Marc Jones1587dc82017-05-15 18:55:11 -060091{
92 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060093 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -060094 char buf[50];
95
96 /* Make certain the resource has actually been set */
97 if (!(resource->flags & IORESOURCE_ASSIGNED))
98 return;
99
100 /* If I have already stored this resource don't worry about it */
101 if (resource->flags & IORESOURCE_STORED)
102 return;
103
104 /* Only handle PCI memory and IO resources */
105 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
106 return;
107
108 /* Ensure I am actually looking at a resource of function 1 */
109 if ((resource->index & 0xffff) < 0x1000)
110 return;
111
112 /* Get the base address */
113 rbase = resource->base;
114
115 /* Get the limit (rounded up) */
116 rend = resource_end(resource);
117
118 /* Get the register and link */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600119 reg = resource->index & 0xfff; /* 4k */
Marc Jones1587dc82017-05-15 18:55:11 -0600120 link_num = IOINDEX_LINK(resource->index);
121
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600122 if (resource->flags & IORESOURCE_IO)
Marc Jones1587dc82017-05-15 18:55:11 -0600123 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600124 else if (resource->flags & IORESOURCE_MEM)
125 set_mmio_addr_reg(nodeid, link_num, reg,
126 (resource->index >> 24), rbase >> 8, rend >> 8);
127
Marc Jones1587dc82017-05-15 18:55:11 -0600128 resource->flags |= IORESOURCE_STORED;
129 snprintf(buf, sizeof(buf), " <node %x link %x>",
130 nodeid, link_num);
131 report_resource_stored(dev, resource, buf);
132}
133
134/**
135 * I tried to reuse the resource allocation code in set_resource()
136 * but it is too difficult to deal with the resource allocation magic.
137 */
138
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200139static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600140{
141 struct bus *link;
142
143 /* find out which link the VGA card is connected,
144 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600145 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600146 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
147 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600148
149 /* no VGA card installed */
150 if (link == NULL)
151 return;
152
Marshall Dawsone2697de2017-09-06 10:46:36 -0600153 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600154 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700155 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
Marc Jones1587dc82017-05-15 18:55:11 -0600156}
157
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200158static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600159{
160 struct bus *bus;
161 struct resource *res;
162
163
164 /* do we need this? */
165 create_vga_resource(dev);
166
167 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600168 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600169 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600170
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600171 for (bus = dev->link_list ; bus ; bus = bus->next)
172 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600173 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600174}
175
176static void northbridge_init(struct device *dev)
177{
Marc Jonesd6a82002018-03-31 22:46:57 -0600178 setup_ioapic((u8 *)IO_APIC2_ADDR, CONFIG_MAX_CPUS+1);
Marc Jones1587dc82017-05-15 18:55:11 -0600179}
180
181static unsigned long acpi_fill_hest(acpi_hest_t *hest)
182{
183 void *addr, *current;
184
185 /* Skip the HEST header. */
186 current = (void *)(hest + 1);
187
188 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
189 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600190 current += acpi_create_hest_error_source(hest, current, 0,
191 (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600192
193 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
194 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600195 current += acpi_create_hest_error_source(hest, current, 1,
196 (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600197
198 return (unsigned long)current;
199}
200
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200201static void northbridge_fill_ssdt_generator(struct device *device)
Marc Jones1587dc82017-05-15 18:55:11 -0600202{
203 msr_t msr;
204 char pscope[] = "\\_SB.PCI0";
205
206 acpigen_write_scope(pscope);
207 msr = rdmsr(TOP_MEM);
208 acpigen_write_name_dword("TOM1", msr.lo);
209 msr = rdmsr(TOP_MEM2);
210 /*
211 * Since XP only implements parts of ACPI 2.0, we can't use a qword
212 * here.
213 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
214 * slide 22ff.
215 * Shift value right by 20 bit to make it fit into 32bit,
216 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
217 */
218 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
219 acpigen_pop_len();
220}
221
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200222static unsigned long agesa_write_acpi_tables(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;
237 acpi_write_hest((void *)current, acpi_fill_hest);
238 acpi_add_table(rsdp, (void *)current);
239 current += ((acpi_header_t *)current)->length;
240
Marshall Dawson653f7602018-09-04 13:25:39 -0600241 /* BERT */
242 if (IS_ENABLED(CONFIG_ACPI_BERT) && bert_errors_present()) {
243 /* 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;
255 acpi_write_bert((void *)current, (uintptr_t)rgn, size);
256 acpi_add_table(rsdp, (void *)current);
257 current += ((acpi_header_t *)current)->length;
258 }
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) {
317 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600318 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600319 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600320 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600321 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
322 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600323 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600324
325 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
326 return current;
327}
328
329static struct device_operations northbridge_operations = {
330 .read_resources = read_resources,
331 .set_resources = set_resources,
332 .enable_resources = pci_dev_enable_resources,
333 .init = northbridge_init,
334 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
335 .write_acpi_tables = agesa_write_acpi_tables,
336 .enable = 0,
337 .ops_pci = 0,
338};
339
340static const struct pci_driver family15_northbridge __pci_driver = {
341 .ops = &northbridge_operations,
342 .vendor = PCI_VENDOR_ID_AMD,
343 .device = PCI_DEVICE_ID_AMD_15H_MODEL_707F_NB_HT,
344};
345
Marshall Dawson154239a2017-11-02 09:49:30 -0600346/*
347 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
348 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
349 * MMIO to posted. Route all I/O to the southbridge.
350 */
351void amd_initcpuio(void)
352{
353 uintptr_t topmem = bsp_topmem();
354 uintptr_t base, limit;
355
356 /* Enable legacy video routing: D18F1xF4 VGA Enable */
357 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
358
359 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
360 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
361 limit = (ALIGN_DOWN(LOCAL_APIC_ADDR - 1, 64 * KiB) >> 8) | MMIO_NP;
362 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
363 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
364
365 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
366 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
367 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
368 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
369 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
370
371 /* Route all I/O downstream */
372 base = 0 | IO_WE | IO_RE;
373 limit = ALIGN_DOWN(0xffff, 4 * KiB);
374 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
375 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
376}
377
Marc Jones1587dc82017-05-15 18:55:11 -0600378void fam15_finalize(void *chip_info)
379{
Marc Jones1587dc82017-05-15 18:55:11 -0600380 u32 value;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700381
382 /* TODO: move IOAPIC code to dsdt.asl */
383 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_INDEX, 0);
384 pci_write_config32(SOC_GNB_DEV, NB_IOAPIC_DATA, 5);
Marc Jones1587dc82017-05-15 18:55:11 -0600385
386 /* disable No Snoop */
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700387 value = pci_read_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700388 value &= ~HDA_NO_SNOOP_EN;
Richard Spiegel41baf0c2018-10-22 13:57:18 -0700389 pci_write_config32(SOC_HDA0_DEV, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600390}
391
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200392void domain_read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600393{
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600394 unsigned int reg;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200395 struct device *addr_map = dev_find_slot(0, ADDR_DEVFN);
Marc Jones1587dc82017-05-15 18:55:11 -0600396
397 /* Find the already assigned resource pairs */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600398 for (reg = 0x80 ; reg <= 0xd8 ; reg += 0x08) {
Marc Jones1587dc82017-05-15 18:55:11 -0600399 u32 base, limit;
Marshall Dawson38bded02017-09-01 09:54:48 -0600400 base = pci_read_config32(addr_map, reg);
401 limit = pci_read_config32(addr_map, reg + 4);
Marc Jones1587dc82017-05-15 18:55:11 -0600402 /* Is this register allocated? */
403 if ((base & 3) != 0) {
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600404 unsigned int nodeid, reg_link;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200405 struct device *reg_dev = dev_find_slot(0, HT_DEVFN);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600406 if (reg < 0xc0) /* mmio */
Marc Jones1587dc82017-05-15 18:55:11 -0600407 nodeid = (limit & 0xf) + (base & 0x30);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600408 else /* io */
Marc Jones1587dc82017-05-15 18:55:11 -0600409 nodeid = (limit & 0xf) + ((base >> 4) & 0x30);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600410
Marc Jones1587dc82017-05-15 18:55:11 -0600411 reg_link = (limit >> 4) & 7;
Marc Jones1587dc82017-05-15 18:55:11 -0600412 if (reg_dev) {
413 /* Reserve the resource */
414 struct resource *res;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600415 res = new_resource(reg_dev,
416 IOINDEX(0x1000 + reg,
417 reg_link));
418 if (res)
Marc Jones1587dc82017-05-15 18:55:11 -0600419 res->flags = 1;
Marc Jones1587dc82017-05-15 18:55:11 -0600420 }
421 }
422 }
423 /* FIXME: do we need to check extend conf space?
424 I don't believe that much preset value */
425
426 pci_domain_read_resources(dev);
427}
428
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200429void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600430{
Marc Jones1587dc82017-05-15 18:55:11 -0600431 /* Must be called after PCI enumeration and resource allocation */
Marshall Dawson8f2a7e02017-11-01 11:44:48 -0600432 if (!romstage_handoff_is_resume())
Richard Spiegel138a1d22017-12-13 13:26:21 -0700433 do_agesawrapper(agesawrapper_amdinitmid, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600434}
435
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200436void domain_set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600437{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700438 uint64_t uma_base = get_uma_base();
439 uint32_t uma_size = get_uma_size();
440 uint32_t mem_useable = (uintptr_t)cbmem_top();
441 msr_t tom = rdmsr(TOP_MEM);
442 msr_t high_tom = rdmsr(TOP_MEM2);
443 uint64_t high_mem_useable;
444 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600445
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700446 /* 0x0 -> 0x9ffff */
447 ram_resource(dev, idx++, 0, 0xa0000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600448
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700449 /* 0xa0000 -> 0xbffff: legacy VGA */
450 mmio_resource(dev, idx++, 0xa0000 / KiB, 0x20000 / KiB);
451
452 /* 0xc0000 -> 0xfffff: Option ROM */
453 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, 0x40000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600454
Marshall Dawson29f1b742017-09-06 14:59:45 -0600455 /*
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700456 * 0x100000 (1MiB) -> low top useable RAM
457 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600458 */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700459 ram_resource(dev, idx++, (1 * MiB) / KiB,
460 (mem_useable - (1 * MiB)) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600461
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700462 /* Low top useable RAM -> Low top RAM (bottom pci mmio hole) */
463 reserved_ram_resource(dev, idx++, mem_useable / KiB,
464 (tom.lo - mem_useable) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600465
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700466 /* If there is memory above 4GiB */
467 if (high_tom.hi) {
468 /* 4GiB -> high top useable */
469 if (uma_base >= (4ull * GiB))
470 high_mem_useable = uma_base;
471 else
472 high_mem_useable = ((uint64_t)high_tom.lo |
473 ((uint64_t)high_tom.hi << 32));
Marc Jones1587dc82017-05-15 18:55:11 -0600474
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700475 ram_resource(dev, idx++, (4ull * GiB) / KiB,
476 ((high_mem_useable - (4ull * GiB)) / KiB));
Marc Jones1587dc82017-05-15 18:55:11 -0600477
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700478 /* High top useable RAM -> high top RAM */
479 if (uma_base >= (4ull * GiB)) {
480 reserved_ram_resource(dev, idx++, uma_base / KiB,
481 uma_size / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600482 }
Marc Jones1587dc82017-05-15 18:55:11 -0600483 }
484
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700485 assign_resources(dev->link_list);
Marc Jones1587dc82017-05-15 18:55:11 -0600486}
487
Marc Jones1587dc82017-05-15 18:55:11 -0600488/*********************************************************************
489 * Change the vendor / device IDs to match the generic VBIOS header. *
490 *********************************************************************/
491u32 map_oprom_vendev(u32 vendev)
492{
493 u32 new_vendev;
494 new_vendev =
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600495 ((vendev >= 0x100298e0) && (vendev <= 0x100298ef)) ?
496 0x100298e0 : vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600497
498 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600499 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
500 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600501
502 return new_vendev;
503}
Marshall Dawson2942db62017-12-14 10:00:27 -0700504
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700505__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
506
Marshall Dawson2942db62017-12-14 10:00:27 -0700507void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
508{
Marc Jonesbc94aea2018-09-26 09:57:08 -0600509 params->IommuSupport = TRUE;
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700510 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700511}
512
513void SetNbMidParams(GNB_MID_CONFIGURATION *params)
514{
515 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
516 params->iGpuVgaMode = 0;
517 params->GnbIoapicAddress = IO_APIC2_ADDR;
518}