blob: a2ae52c09e86886ee2155dbf4e314669a07ad6f6 [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;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020051 struct device *addr_map = dev_find_slot(0, ADDR_DEVFN);
Marshall Dawson38bded02017-09-01 09:54:48 -060052
Marshall Dawson4e101ad2017-06-15 12:17:38 -060053 /* io range allocation. Limit */
54 tempreg = (nodeid & 0xf) | ((nodeid & 0x30) << (8 - 4)) | (linkn << 4)
55 | ((io_max & 0xf0) << (12 - 4));
Marshall Dawson38bded02017-09-01 09:54:48 -060056 pci_write_config32(addr_map, reg + 4, tempreg);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060057 tempreg = 3 | ((io_min & 0xf0) << (12 - 4)); /* base: ISA and VGA ? */
Marshall Dawson38bded02017-09-01 09:54:48 -060058 pci_write_config32(addr_map, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060059}
60
Marshall Dawson4e101ad2017-06-15 12:17:38 -060061static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index,
62 u32 mmio_min, u32 mmio_max)
Marc Jones1587dc82017-05-15 18:55:11 -060063{
64 u32 tempreg;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020065 struct device *addr_map = dev_find_slot(0, ADDR_DEVFN);
Marshall Dawson38bded02017-09-01 09:54:48 -060066
Marshall Dawson4e101ad2017-06-15 12:17:38 -060067 /* io range allocation. Limit */
68 tempreg = (nodeid & 0xf) | (linkn << 4) | (mmio_max & 0xffffff00);
Marshall Dawson38bded02017-09-01 09:54:48 -060069 pci_write_config32(addr_map, reg + 4, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060070 tempreg = 3 | (nodeid & 0x30) | (mmio_min & 0xffffff00);
Marshall Dawson38bded02017-09-01 09:54:48 -060071 pci_write_config32(addr_map, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060072}
73
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020074static void read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060075{
Marc Jonesd6a82002018-03-31 22:46:57 -060076 struct resource *res;
77
Marc Jones1587dc82017-05-15 18:55:11 -060078 /*
79 * This MMCONF resource must be reserved in the PCI domain.
80 * It is not honored by the coreboot resource allocator if it is in
81 * the CPU_CLUSTER.
82 */
Aaron Durbin3173d442017-11-03 12:14:25 -060083 mmconf_resource(dev, MMIO_CONF_BASE);
Marc Jonesd6a82002018-03-31 22:46:57 -060084
85 /* NB IOAPIC2 resource */
86 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
87 res->base = IO_APIC2_ADDR;
88 res->size = 0x00001000;
89 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Marc Jones1587dc82017-05-15 18:55:11 -060090}
91
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020092static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Marc Jones1587dc82017-05-15 18:55:11 -060093{
94 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060095 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -060096 char buf[50];
97
98 /* Make certain the resource has actually been set */
99 if (!(resource->flags & IORESOURCE_ASSIGNED))
100 return;
101
102 /* If I have already stored this resource don't worry about it */
103 if (resource->flags & IORESOURCE_STORED)
104 return;
105
106 /* Only handle PCI memory and IO resources */
107 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
108 return;
109
110 /* Ensure I am actually looking at a resource of function 1 */
111 if ((resource->index & 0xffff) < 0x1000)
112 return;
113
114 /* Get the base address */
115 rbase = resource->base;
116
117 /* Get the limit (rounded up) */
118 rend = resource_end(resource);
119
120 /* Get the register and link */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600121 reg = resource->index & 0xfff; /* 4k */
Marc Jones1587dc82017-05-15 18:55:11 -0600122 link_num = IOINDEX_LINK(resource->index);
123
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600124 if (resource->flags & IORESOURCE_IO)
Marc Jones1587dc82017-05-15 18:55:11 -0600125 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600126 else if (resource->flags & IORESOURCE_MEM)
127 set_mmio_addr_reg(nodeid, link_num, reg,
128 (resource->index >> 24), rbase >> 8, rend >> 8);
129
Marc Jones1587dc82017-05-15 18:55:11 -0600130 resource->flags |= IORESOURCE_STORED;
131 snprintf(buf, sizeof(buf), " <node %x link %x>",
132 nodeid, link_num);
133 report_resource_stored(dev, resource, buf);
134}
135
136/**
137 * I tried to reuse the resource allocation code in set_resource()
138 * but it is too difficult to deal with the resource allocation magic.
139 */
140
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200141static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600142{
143 struct bus *link;
144
145 /* find out which link the VGA card is connected,
146 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600147 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600148 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
149 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600150
151 /* no VGA card installed */
152 if (link == NULL)
153 return;
154
Marshall Dawsone2697de2017-09-06 10:46:36 -0600155 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600156 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
157 pci_write_config32(dev_find_slot(0, ADDR_DEVFN), 0xf4, 1);
Marc Jones1587dc82017-05-15 18:55:11 -0600158}
159
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200160static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600161{
162 struct bus *bus;
163 struct resource *res;
164
165
166 /* do we need this? */
167 create_vga_resource(dev);
168
169 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600170 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600171 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600172
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600173 for (bus = dev->link_list ; bus ; bus = bus->next)
174 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600175 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600176}
177
178static void northbridge_init(struct device *dev)
179{
Marc Jonesd6a82002018-03-31 22:46:57 -0600180 setup_ioapic((u8 *)IO_APIC2_ADDR, CONFIG_MAX_CPUS+1);
Marc Jones1587dc82017-05-15 18:55:11 -0600181}
182
183static unsigned long acpi_fill_hest(acpi_hest_t *hest)
184{
185 void *addr, *current;
186
187 /* Skip the HEST header. */
188 current = (void *)(hest + 1);
189
190 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
191 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600192 current += acpi_create_hest_error_source(hest, current, 0,
193 (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600194
195 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
196 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600197 current += acpi_create_hest_error_source(hest, current, 1,
198 (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600199
200 return (unsigned long)current;
201}
202
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200203static void northbridge_fill_ssdt_generator(struct device *device)
Marc Jones1587dc82017-05-15 18:55:11 -0600204{
205 msr_t msr;
206 char pscope[] = "\\_SB.PCI0";
207
208 acpigen_write_scope(pscope);
209 msr = rdmsr(TOP_MEM);
210 acpigen_write_name_dword("TOM1", msr.lo);
211 msr = rdmsr(TOP_MEM2);
212 /*
213 * Since XP only implements parts of ACPI 2.0, we can't use a qword
214 * here.
215 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
216 * slide 22ff.
217 * Shift value right by 20 bit to make it fit into 32bit,
218 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
219 */
220 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
221 acpigen_pop_len();
222}
223
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200224static unsigned long agesa_write_acpi_tables(struct device *device,
Marc Jones1587dc82017-05-15 18:55:11 -0600225 unsigned long current,
226 acpi_rsdp_t *rsdp)
227{
228 acpi_srat_t *srat;
229 acpi_slit_t *slit;
230 acpi_header_t *ssdt;
231 acpi_header_t *alib;
232 acpi_header_t *ivrs;
233 acpi_hest_t *hest;
Marshall Dawson653f7602018-09-04 13:25:39 -0600234 acpi_bert_t *bert;
Marc Jones1587dc82017-05-15 18:55:11 -0600235
236 /* HEST */
237 current = ALIGN(current, 8);
238 hest = (acpi_hest_t *)current;
239 acpi_write_hest((void *)current, acpi_fill_hest);
240 acpi_add_table(rsdp, (void *)current);
241 current += ((acpi_header_t *)current)->length;
242
Marshall Dawson653f7602018-09-04 13:25:39 -0600243 /* BERT */
244 if (IS_ENABLED(CONFIG_ACPI_BERT) && bert_errors_present()) {
245 /* Skip the table if no errors are present. ACPI driver reports
246 * a table with a 0-length region:
247 * BERT: [Firmware Bug]: table invalid.
248 */
249 void *rgn;
250 size_t size;
251 bert_errors_region(&rgn, &size);
252 if (!rgn) {
253 printk(BIOS_ERR, "Error: Can't find BERT storage area\n");
254 } else {
255 current = ALIGN(current, 8);
256 bert = (acpi_bert_t *)current;
257 acpi_write_bert((void *)current, (uintptr_t)rgn, size);
258 acpi_add_table(rsdp, (void *)current);
259 current += ((acpi_header_t *)current)->length;
260 }
261 }
262
Marc Jones1587dc82017-05-15 18:55:11 -0600263 current = ALIGN(current, 8);
264 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
265 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
266 if (ivrs != NULL) {
267 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600268 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600269 current += ivrs->length;
270 acpi_add_table(rsdp, ivrs);
271 } else {
272 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
273 }
274
275 /* SRAT */
276 current = ALIGN(current, 8);
277 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600278 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600279 if (srat != NULL) {
280 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600281 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600282 current += srat->header.length;
283 acpi_add_table(rsdp, srat);
284 } else {
285 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
286 }
287
288 /* SLIT */
289 current = ALIGN(current, 8);
290 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600291 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600292 if (slit != NULL) {
293 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600294 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600295 current += slit->header.length;
296 acpi_add_table(rsdp, slit);
297 } else {
298 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
299 }
300
301 /* ALIB */
302 current = ALIGN(current, 16);
303 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600304 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600305 if (alib != NULL) {
306 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600307 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600308 current += alib->length;
309 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600310 } else {
311 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
312 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600313 }
314
Marc Jones1587dc82017-05-15 18:55:11 -0600315 current = ALIGN(current, 16);
316 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600317 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Marc Jones1587dc82017-05-15 18:55:11 -0600318 if (ssdt != NULL) {
319 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600320 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600321 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600322 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600323 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
324 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600325 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600326
327 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
328 return current;
329}
330
331static struct device_operations northbridge_operations = {
332 .read_resources = read_resources,
333 .set_resources = set_resources,
334 .enable_resources = pci_dev_enable_resources,
335 .init = northbridge_init,
336 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
337 .write_acpi_tables = agesa_write_acpi_tables,
338 .enable = 0,
339 .ops_pci = 0,
340};
341
342static const struct pci_driver family15_northbridge __pci_driver = {
343 .ops = &northbridge_operations,
344 .vendor = PCI_VENDOR_ID_AMD,
345 .device = PCI_DEVICE_ID_AMD_15H_MODEL_707F_NB_HT,
346};
347
Marshall Dawson154239a2017-11-02 09:49:30 -0600348/*
349 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
350 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
351 * MMIO to posted. Route all I/O to the southbridge.
352 */
353void amd_initcpuio(void)
354{
355 uintptr_t topmem = bsp_topmem();
356 uintptr_t base, limit;
357
358 /* Enable legacy video routing: D18F1xF4 VGA Enable */
359 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
360
361 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
362 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
363 limit = (ALIGN_DOWN(LOCAL_APIC_ADDR - 1, 64 * KiB) >> 8) | MMIO_NP;
364 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
365 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
366
367 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
368 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
369 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
370 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
371 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
372
373 /* Route all I/O downstream */
374 base = 0 | IO_WE | IO_RE;
375 limit = ALIGN_DOWN(0xffff, 4 * KiB);
376 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
377 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
378}
379
Marc Jones1587dc82017-05-15 18:55:11 -0600380void fam15_finalize(void *chip_info)
381{
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200382 struct device *dev;
Marc Jones1587dc82017-05-15 18:55:11 -0600383 u32 value;
Chris Ching6a35fab2017-10-19 11:45:30 -0600384 dev = dev_find_slot(0, GNB_DEVFN); /* clear IoapicSbFeatureEn */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600385 pci_write_config32(dev, 0xf8, 0);
386 pci_write_config32(dev, 0xfc, 5); /* TODO: move it to dsdt.asl */
Marc Jones1587dc82017-05-15 18:55:11 -0600387
388 /* disable No Snoop */
Chris Ching6a35fab2017-10-19 11:45:30 -0600389 dev = dev_find_slot(0, HDA0_DEVFN);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700390 value = pci_read_config32(dev, HDA_DEV_CTRL_STATUS);
391 value &= ~HDA_NO_SNOOP_EN;
392 pci_write_config32(dev, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600393}
394
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200395void domain_read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600396{
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600397 unsigned int reg;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200398 struct device *addr_map = dev_find_slot(0, ADDR_DEVFN);
Marc Jones1587dc82017-05-15 18:55:11 -0600399
400 /* Find the already assigned resource pairs */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600401 for (reg = 0x80 ; reg <= 0xd8 ; reg += 0x08) {
Marc Jones1587dc82017-05-15 18:55:11 -0600402 u32 base, limit;
Marshall Dawson38bded02017-09-01 09:54:48 -0600403 base = pci_read_config32(addr_map, reg);
404 limit = pci_read_config32(addr_map, reg + 4);
Marc Jones1587dc82017-05-15 18:55:11 -0600405 /* Is this register allocated? */
406 if ((base & 3) != 0) {
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600407 unsigned int nodeid, reg_link;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200408 struct device *reg_dev = dev_find_slot(0, HT_DEVFN);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600409 if (reg < 0xc0) /* mmio */
Marc Jones1587dc82017-05-15 18:55:11 -0600410 nodeid = (limit & 0xf) + (base & 0x30);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600411 else /* io */
Marc Jones1587dc82017-05-15 18:55:11 -0600412 nodeid = (limit & 0xf) + ((base >> 4) & 0x30);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600413
Marc Jones1587dc82017-05-15 18:55:11 -0600414 reg_link = (limit >> 4) & 7;
Marc Jones1587dc82017-05-15 18:55:11 -0600415 if (reg_dev) {
416 /* Reserve the resource */
417 struct resource *res;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600418 res = new_resource(reg_dev,
419 IOINDEX(0x1000 + reg,
420 reg_link));
421 if (res)
Marc Jones1587dc82017-05-15 18:55:11 -0600422 res->flags = 1;
Marc Jones1587dc82017-05-15 18:55:11 -0600423 }
424 }
425 }
426 /* FIXME: do we need to check extend conf space?
427 I don't believe that much preset value */
428
429 pci_domain_read_resources(dev);
430}
431
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200432void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600433{
Marc Jones1587dc82017-05-15 18:55:11 -0600434 /* Must be called after PCI enumeration and resource allocation */
Marshall Dawson8f2a7e02017-11-01 11:44:48 -0600435 if (!romstage_handoff_is_resume())
Richard Spiegel138a1d22017-12-13 13:26:21 -0700436 do_agesawrapper(agesawrapper_amdinitmid, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600437}
438
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200439void domain_set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600440{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700441 uint64_t uma_base = get_uma_base();
442 uint32_t uma_size = get_uma_size();
443 uint32_t mem_useable = (uintptr_t)cbmem_top();
444 msr_t tom = rdmsr(TOP_MEM);
445 msr_t high_tom = rdmsr(TOP_MEM2);
446 uint64_t high_mem_useable;
447 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600448
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700449 /* 0x0 -> 0x9ffff */
450 ram_resource(dev, idx++, 0, 0xa0000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600451
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700452 /* 0xa0000 -> 0xbffff: legacy VGA */
453 mmio_resource(dev, idx++, 0xa0000 / KiB, 0x20000 / KiB);
454
455 /* 0xc0000 -> 0xfffff: Option ROM */
456 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, 0x40000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600457
Marshall Dawson29f1b742017-09-06 14:59:45 -0600458 /*
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700459 * 0x100000 (1MiB) -> low top useable RAM
460 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600461 */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700462 ram_resource(dev, idx++, (1 * MiB) / KiB,
463 (mem_useable - (1 * MiB)) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600464
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700465 /* Low top useable RAM -> Low top RAM (bottom pci mmio hole) */
466 reserved_ram_resource(dev, idx++, mem_useable / KiB,
467 (tom.lo - mem_useable) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600468
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700469 /* If there is memory above 4GiB */
470 if (high_tom.hi) {
471 /* 4GiB -> high top useable */
472 if (uma_base >= (4ull * GiB))
473 high_mem_useable = uma_base;
474 else
475 high_mem_useable = ((uint64_t)high_tom.lo |
476 ((uint64_t)high_tom.hi << 32));
Marc Jones1587dc82017-05-15 18:55:11 -0600477
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700478 ram_resource(dev, idx++, (4ull * GiB) / KiB,
479 ((high_mem_useable - (4ull * GiB)) / KiB));
Marc Jones1587dc82017-05-15 18:55:11 -0600480
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700481 /* High top useable RAM -> high top RAM */
482 if (uma_base >= (4ull * GiB)) {
483 reserved_ram_resource(dev, idx++, uma_base / KiB,
484 uma_size / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600485 }
Marc Jones1587dc82017-05-15 18:55:11 -0600486 }
487
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700488 assign_resources(dev->link_list);
Marc Jones1587dc82017-05-15 18:55:11 -0600489}
490
Marc Jones1587dc82017-05-15 18:55:11 -0600491/*********************************************************************
492 * Change the vendor / device IDs to match the generic VBIOS header. *
493 *********************************************************************/
494u32 map_oprom_vendev(u32 vendev)
495{
496 u32 new_vendev;
497 new_vendev =
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600498 ((vendev >= 0x100298e0) && (vendev <= 0x100298ef)) ?
499 0x100298e0 : vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600500
501 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600502 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
503 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600504
505 return new_vendev;
506}
Marshall Dawson2942db62017-12-14 10:00:27 -0700507
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700508__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
509
Marshall Dawson2942db62017-12-14 10:00:27 -0700510void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
511{
Marc Jonesbc94aea2018-09-26 09:57:08 -0600512 params->IommuSupport = TRUE;
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700513 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700514}
515
516void SetNbMidParams(GNB_MID_CONFIGURATION *params)
517{
518 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
519 params->iGpuVgaMode = 0;
520 params->GnbIoapicAddress = IO_APIC2_ADDR;
521}