blob: 912daa05e22eff22b4b4e4d359be0d31b85438bd [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>
Marc Jones1587dc82017-05-15 18:55:11 -060029#include <device/device.h>
30#include <device/pci.h>
31#include <device/pci_ids.h>
Marshall Dawson8f2a7e02017-11-01 11:44:48 -060032#include <romstage_handoff.h>
Richard Spiegel0ad74ac2017-12-08 16:53:29 -070033#include <amdblocks/agesawrapper.h>
34#include <amdblocks/agesawrapper_call.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070035#include <agesa_headers.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060036#include <soc/cpu.h>
Marc Jones1587dc82017-05-15 18:55:11 -060037#include <soc/northbridge.h>
Marshall Dawson154239a2017-11-02 09:49:30 -060038#include <soc/southbridge.h>
Marshall Dawson38bded02017-09-01 09:54:48 -060039#include <soc/pci_devs.h>
Marshall Dawson2942db62017-12-14 10:00:27 -070040#include <soc/iomap.h>
Marc Jones1587dc82017-05-15 18:55:11 -060041#include <stdint.h>
42#include <stdlib.h>
43#include <string.h>
Marshall Dawson653f7602018-09-04 13:25:39 -060044#include <arch/bert_storage.h>
Marc Jones1587dc82017-05-15 18:55:11 -060045
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020046static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Marc Jones1587dc82017-05-15 18:55:11 -060047 u32 io_min, u32 io_max)
48{
49 u32 tempreg;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020050 struct device *addr_map = dev_find_slot(0, ADDR_DEVFN);
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));
Marshall Dawson38bded02017-09-01 09:54:48 -060055 pci_write_config32(addr_map, reg + 4, tempreg);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060056 tempreg = 3 | ((io_min & 0xf0) << (12 - 4)); /* base: ISA and VGA ? */
Marshall Dawson38bded02017-09-01 09:54:48 -060057 pci_write_config32(addr_map, 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;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020064 struct device *addr_map = dev_find_slot(0, ADDR_DEVFN);
Marshall Dawson38bded02017-09-01 09:54:48 -060065
Marshall Dawson4e101ad2017-06-15 12:17:38 -060066 /* io range allocation. Limit */
67 tempreg = (nodeid & 0xf) | (linkn << 4) | (mmio_max & 0xffffff00);
Marshall Dawson38bded02017-09-01 09:54:48 -060068 pci_write_config32(addr_map, reg + 4, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060069 tempreg = 3 | (nodeid & 0x30) | (mmio_min & 0xffffff00);
Marshall Dawson38bded02017-09-01 09:54:48 -060070 pci_write_config32(addr_map, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060071}
72
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020073static void read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -060074{
Marc Jonesd6a82002018-03-31 22:46:57 -060075 struct resource *res;
76
Marc Jones1587dc82017-05-15 18:55:11 -060077 /*
78 * This MMCONF resource must be reserved in the PCI domain.
79 * It is not honored by the coreboot resource allocator if it is in
80 * the CPU_CLUSTER.
81 */
Aaron Durbin3173d442017-11-03 12:14:25 -060082 mmconf_resource(dev, MMIO_CONF_BASE);
Marc Jonesd6a82002018-03-31 22:46:57 -060083
84 /* NB IOAPIC2 resource */
85 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
86 res->base = IO_APIC2_ADDR;
87 res->size = 0x00001000;
88 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Marc Jones1587dc82017-05-15 18:55:11 -060089}
90
Elyes HAOUAS777ccd42018-05-22 10:52:05 +020091static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Marc Jones1587dc82017-05-15 18:55:11 -060092{
93 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060094 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -060095 char buf[50];
96
97 /* Make certain the resource has actually been set */
98 if (!(resource->flags & IORESOURCE_ASSIGNED))
99 return;
100
101 /* If I have already stored this resource don't worry about it */
102 if (resource->flags & IORESOURCE_STORED)
103 return;
104
105 /* Only handle PCI memory and IO resources */
106 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
107 return;
108
109 /* Ensure I am actually looking at a resource of function 1 */
110 if ((resource->index & 0xffff) < 0x1000)
111 return;
112
113 /* Get the base address */
114 rbase = resource->base;
115
116 /* Get the limit (rounded up) */
117 rend = resource_end(resource);
118
119 /* Get the register and link */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600120 reg = resource->index & 0xfff; /* 4k */
Marc Jones1587dc82017-05-15 18:55:11 -0600121 link_num = IOINDEX_LINK(resource->index);
122
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600123 if (resource->flags & IORESOURCE_IO)
Marc Jones1587dc82017-05-15 18:55:11 -0600124 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600125 else if (resource->flags & IORESOURCE_MEM)
126 set_mmio_addr_reg(nodeid, link_num, reg,
127 (resource->index >> 24), rbase >> 8, rend >> 8);
128
Marc Jones1587dc82017-05-15 18:55:11 -0600129 resource->flags |= IORESOURCE_STORED;
130 snprintf(buf, sizeof(buf), " <node %x link %x>",
131 nodeid, link_num);
132 report_resource_stored(dev, resource, buf);
133}
134
135/**
136 * I tried to reuse the resource allocation code in set_resource()
137 * but it is too difficult to deal with the resource allocation magic.
138 */
139
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200140static void create_vga_resource(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600141{
142 struct bus *link;
143
144 /* find out which link the VGA card is connected,
145 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600146 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600147 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
148 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600149
150 /* no VGA card installed */
151 if (link == NULL)
152 return;
153
Marshall Dawsone2697de2017-09-06 10:46:36 -0600154 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600155 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
156 pci_write_config32(dev_find_slot(0, ADDR_DEVFN), 0xf4, 1);
Marc Jones1587dc82017-05-15 18:55:11 -0600157}
158
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200159static void set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600160{
161 struct bus *bus;
162 struct resource *res;
163
164
165 /* do we need this? */
166 create_vga_resource(dev);
167
168 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600169 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600170 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600171
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600172 for (bus = dev->link_list ; bus ; bus = bus->next)
173 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600174 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600175}
176
177static void northbridge_init(struct device *dev)
178{
Marc Jonesd6a82002018-03-31 22:46:57 -0600179 setup_ioapic((u8 *)IO_APIC2_ADDR, CONFIG_MAX_CPUS+1);
Marc Jones1587dc82017-05-15 18:55:11 -0600180}
181
182static unsigned long acpi_fill_hest(acpi_hest_t *hest)
183{
184 void *addr, *current;
185
186 /* Skip the HEST header. */
187 current = (void *)(hest + 1);
188
189 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
190 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600191 current += acpi_create_hest_error_source(hest, current, 0,
192 (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600193
194 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
195 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600196 current += acpi_create_hest_error_source(hest, current, 1,
197 (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600198
199 return (unsigned long)current;
200}
201
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200202static void northbridge_fill_ssdt_generator(struct device *device)
Marc Jones1587dc82017-05-15 18:55:11 -0600203{
204 msr_t msr;
205 char pscope[] = "\\_SB.PCI0";
206
207 acpigen_write_scope(pscope);
208 msr = rdmsr(TOP_MEM);
209 acpigen_write_name_dword("TOM1", msr.lo);
210 msr = rdmsr(TOP_MEM2);
211 /*
212 * Since XP only implements parts of ACPI 2.0, we can't use a qword
213 * here.
214 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
215 * slide 22ff.
216 * Shift value right by 20 bit to make it fit into 32bit,
217 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
218 */
219 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
220 acpigen_pop_len();
221}
222
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200223static unsigned long agesa_write_acpi_tables(struct device *device,
Marc Jones1587dc82017-05-15 18:55:11 -0600224 unsigned long current,
225 acpi_rsdp_t *rsdp)
226{
227 acpi_srat_t *srat;
228 acpi_slit_t *slit;
229 acpi_header_t *ssdt;
230 acpi_header_t *alib;
231 acpi_header_t *ivrs;
232 acpi_hest_t *hest;
Marshall Dawson653f7602018-09-04 13:25:39 -0600233 acpi_bert_t *bert;
Marc Jones1587dc82017-05-15 18:55:11 -0600234
235 /* HEST */
236 current = ALIGN(current, 8);
237 hest = (acpi_hest_t *)current;
238 acpi_write_hest((void *)current, acpi_fill_hest);
239 acpi_add_table(rsdp, (void *)current);
240 current += ((acpi_header_t *)current)->length;
241
Marshall Dawson653f7602018-09-04 13:25:39 -0600242 /* BERT */
243 if (IS_ENABLED(CONFIG_ACPI_BERT) && bert_errors_present()) {
244 /* Skip the table if no errors are present. ACPI driver reports
245 * a table with a 0-length region:
246 * BERT: [Firmware Bug]: table invalid.
247 */
248 void *rgn;
249 size_t size;
250 bert_errors_region(&rgn, &size);
251 if (!rgn) {
252 printk(BIOS_ERR, "Error: Can't find BERT storage area\n");
253 } else {
254 current = ALIGN(current, 8);
255 bert = (acpi_bert_t *)current;
256 acpi_write_bert((void *)current, (uintptr_t)rgn, size);
257 acpi_add_table(rsdp, (void *)current);
258 current += ((acpi_header_t *)current)->length;
259 }
260 }
261
Marc Jones1587dc82017-05-15 18:55:11 -0600262 current = ALIGN(current, 8);
263 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
264 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
265 if (ivrs != NULL) {
266 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600267 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600268 current += ivrs->length;
269 acpi_add_table(rsdp, ivrs);
270 } else {
271 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
272 }
273
274 /* SRAT */
275 current = ALIGN(current, 8);
276 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600277 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600278 if (srat != NULL) {
279 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600280 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600281 current += srat->header.length;
282 acpi_add_table(rsdp, srat);
283 } else {
284 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
285 }
286
287 /* SLIT */
288 current = ALIGN(current, 8);
289 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600290 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600291 if (slit != NULL) {
292 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600293 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600294 current += slit->header.length;
295 acpi_add_table(rsdp, slit);
296 } else {
297 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
298 }
299
300 /* ALIB */
301 current = ALIGN(current, 16);
302 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600303 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600304 if (alib != NULL) {
305 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600306 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600307 current += alib->length;
308 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600309 } else {
310 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
311 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600312 }
313
Marc Jones1587dc82017-05-15 18:55:11 -0600314 current = ALIGN(current, 16);
315 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600316 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Marc Jones1587dc82017-05-15 18:55:11 -0600317 if (ssdt != NULL) {
318 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600319 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600320 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600321 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600322 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
323 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600324 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600325
326 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
327 return current;
328}
329
330static struct device_operations northbridge_operations = {
331 .read_resources = read_resources,
332 .set_resources = set_resources,
333 .enable_resources = pci_dev_enable_resources,
334 .init = northbridge_init,
335 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
336 .write_acpi_tables = agesa_write_acpi_tables,
337 .enable = 0,
338 .ops_pci = 0,
339};
340
341static const struct pci_driver family15_northbridge __pci_driver = {
342 .ops = &northbridge_operations,
343 .vendor = PCI_VENDOR_ID_AMD,
344 .device = PCI_DEVICE_ID_AMD_15H_MODEL_707F_NB_HT,
345};
346
Marshall Dawson154239a2017-11-02 09:49:30 -0600347/*
348 * Enable VGA cycles. Set memory ranges of the FCH legacy devices (TPM, HPET,
349 * BIOS RAM, Watchdog Timer, IOAPIC and ACPI) as non-posted. Set remaining
350 * MMIO to posted. Route all I/O to the southbridge.
351 */
352void amd_initcpuio(void)
353{
354 uintptr_t topmem = bsp_topmem();
355 uintptr_t base, limit;
356
357 /* Enable legacy video routing: D18F1xF4 VGA Enable */
358 pci_write_config32(SOC_ADDR_DEV, D18F1_VGAEN, VGA_ADDR_ENABLE);
359
360 /* Non-posted: range(HPET-LAPIC) or 0xfed00000 through 0xfee00000-1 */
361 base = (HPET_BASE_ADDRESS >> 8) | MMIO_WE | MMIO_RE;
362 limit = (ALIGN_DOWN(LOCAL_APIC_ADDR - 1, 64 * KiB) >> 8) | MMIO_NP;
363 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(0), limit);
364 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(0), base);
365
366 /* Remaining PCI hole posted MMIO: TOM-HPET (TOM through 0xfed00000-1 */
367 base = (topmem >> 8) | MMIO_WE | MMIO_RE;
368 limit = ALIGN_DOWN(HPET_BASE_ADDRESS - 1, 64 * KiB) >> 8;
369 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_LIMIT_LO(1), limit);
370 pci_write_config32(SOC_ADDR_DEV, NB_MMIO_BASE_LO(1), base);
371
372 /* Route all I/O downstream */
373 base = 0 | IO_WE | IO_RE;
374 limit = ALIGN_DOWN(0xffff, 4 * KiB);
375 pci_write_config32(SOC_ADDR_DEV, NB_IO_LIMIT(0), limit);
376 pci_write_config32(SOC_ADDR_DEV, NB_IO_BASE(0), base);
377}
378
Marc Jones1587dc82017-05-15 18:55:11 -0600379void fam15_finalize(void *chip_info)
380{
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200381 struct device *dev;
Marc Jones1587dc82017-05-15 18:55:11 -0600382 u32 value;
Chris Ching6a35fab2017-10-19 11:45:30 -0600383 dev = dev_find_slot(0, GNB_DEVFN); /* clear IoapicSbFeatureEn */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600384 pci_write_config32(dev, 0xf8, 0);
385 pci_write_config32(dev, 0xfc, 5); /* TODO: move it to dsdt.asl */
Marc Jones1587dc82017-05-15 18:55:11 -0600386
387 /* disable No Snoop */
Chris Ching6a35fab2017-10-19 11:45:30 -0600388 dev = dev_find_slot(0, HDA0_DEVFN);
Richard Spiegel3d34ae32018-04-13 13:20:08 -0700389 value = pci_read_config32(dev, HDA_DEV_CTRL_STATUS);
390 value &= ~HDA_NO_SNOOP_EN;
391 pci_write_config32(dev, HDA_DEV_CTRL_STATUS, value);
Marc Jones1587dc82017-05-15 18:55:11 -0600392}
393
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200394void domain_read_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600395{
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600396 unsigned int reg;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200397 struct device *addr_map = dev_find_slot(0, ADDR_DEVFN);
Marc Jones1587dc82017-05-15 18:55:11 -0600398
399 /* Find the already assigned resource pairs */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600400 for (reg = 0x80 ; reg <= 0xd8 ; reg += 0x08) {
Marc Jones1587dc82017-05-15 18:55:11 -0600401 u32 base, limit;
Marshall Dawson38bded02017-09-01 09:54:48 -0600402 base = pci_read_config32(addr_map, reg);
403 limit = pci_read_config32(addr_map, reg + 4);
Marc Jones1587dc82017-05-15 18:55:11 -0600404 /* Is this register allocated? */
405 if ((base & 3) != 0) {
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600406 unsigned int nodeid, reg_link;
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200407 struct device *reg_dev = dev_find_slot(0, HT_DEVFN);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600408 if (reg < 0xc0) /* mmio */
Marc Jones1587dc82017-05-15 18:55:11 -0600409 nodeid = (limit & 0xf) + (base & 0x30);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600410 else /* io */
Marc Jones1587dc82017-05-15 18:55:11 -0600411 nodeid = (limit & 0xf) + ((base >> 4) & 0x30);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600412
Marc Jones1587dc82017-05-15 18:55:11 -0600413 reg_link = (limit >> 4) & 7;
Marc Jones1587dc82017-05-15 18:55:11 -0600414 if (reg_dev) {
415 /* Reserve the resource */
416 struct resource *res;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600417 res = new_resource(reg_dev,
418 IOINDEX(0x1000 + reg,
419 reg_link));
420 if (res)
Marc Jones1587dc82017-05-15 18:55:11 -0600421 res->flags = 1;
Marc Jones1587dc82017-05-15 18:55:11 -0600422 }
423 }
424 }
425 /* FIXME: do we need to check extend conf space?
426 I don't believe that much preset value */
427
428 pci_domain_read_resources(dev);
429}
430
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200431void domain_enable_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600432{
Marc Jones1587dc82017-05-15 18:55:11 -0600433 /* Must be called after PCI enumeration and resource allocation */
Marshall Dawson8f2a7e02017-11-01 11:44:48 -0600434 if (!romstage_handoff_is_resume())
Richard Spiegel138a1d22017-12-13 13:26:21 -0700435 do_agesawrapper(agesawrapper_amdinitmid, "amdinitmid");
Marc Jones1587dc82017-05-15 18:55:11 -0600436}
437
Elyes HAOUAS777ccd42018-05-22 10:52:05 +0200438void domain_set_resources(struct device *dev)
Marc Jones1587dc82017-05-15 18:55:11 -0600439{
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700440 uint64_t uma_base = get_uma_base();
441 uint32_t uma_size = get_uma_size();
442 uint32_t mem_useable = (uintptr_t)cbmem_top();
443 msr_t tom = rdmsr(TOP_MEM);
444 msr_t high_tom = rdmsr(TOP_MEM2);
445 uint64_t high_mem_useable;
446 int idx = 0x10;
Marc Jones1587dc82017-05-15 18:55:11 -0600447
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700448 /* 0x0 -> 0x9ffff */
449 ram_resource(dev, idx++, 0, 0xa0000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600450
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700451 /* 0xa0000 -> 0xbffff: legacy VGA */
452 mmio_resource(dev, idx++, 0xa0000 / KiB, 0x20000 / KiB);
453
454 /* 0xc0000 -> 0xfffff: Option ROM */
455 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, 0x40000 / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600456
Marshall Dawson29f1b742017-09-06 14:59:45 -0600457 /*
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700458 * 0x100000 (1MiB) -> low top useable RAM
459 * cbmem_top() accounts for low UMA and TSEG if they are used.
Marc Jones1587dc82017-05-15 18:55:11 -0600460 */
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700461 ram_resource(dev, idx++, (1 * MiB) / KiB,
462 (mem_useable - (1 * MiB)) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600463
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700464 /* Low top useable RAM -> Low top RAM (bottom pci mmio hole) */
465 reserved_ram_resource(dev, idx++, mem_useable / KiB,
466 (tom.lo - mem_useable) / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600467
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700468 /* If there is memory above 4GiB */
469 if (high_tom.hi) {
470 /* 4GiB -> high top useable */
471 if (uma_base >= (4ull * GiB))
472 high_mem_useable = uma_base;
473 else
474 high_mem_useable = ((uint64_t)high_tom.lo |
475 ((uint64_t)high_tom.hi << 32));
Marc Jones1587dc82017-05-15 18:55:11 -0600476
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700477 ram_resource(dev, idx++, (4ull * GiB) / KiB,
478 ((high_mem_useable - (4ull * GiB)) / KiB));
Marc Jones1587dc82017-05-15 18:55:11 -0600479
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700480 /* High top useable RAM -> high top RAM */
481 if (uma_base >= (4ull * GiB)) {
482 reserved_ram_resource(dev, idx++, uma_base / KiB,
483 uma_size / KiB);
Marc Jones1587dc82017-05-15 18:55:11 -0600484 }
Marc Jones1587dc82017-05-15 18:55:11 -0600485 }
486
Marc Jones5fd1d5a2018-02-08 15:41:54 -0700487 assign_resources(dev->link_list);
Marc Jones1587dc82017-05-15 18:55:11 -0600488}
489
Marc Jones1587dc82017-05-15 18:55:11 -0600490/*********************************************************************
491 * Change the vendor / device IDs to match the generic VBIOS header. *
492 *********************************************************************/
493u32 map_oprom_vendev(u32 vendev)
494{
495 u32 new_vendev;
496 new_vendev =
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600497 ((vendev >= 0x100298e0) && (vendev <= 0x100298ef)) ?
498 0x100298e0 : vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600499
500 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600501 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
502 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600503
504 return new_vendev;
505}
Marshall Dawson2942db62017-12-14 10:00:27 -0700506
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700507__weak void set_board_env_params(GNB_ENV_CONFIGURATION *params) { }
508
Marshall Dawson2942db62017-12-14 10:00:27 -0700509void SetNbEnvParams(GNB_ENV_CONFIGURATION *params)
510{
511 params->IommuSupport = FALSE;
Richard Spiegel2e90ee32018-07-24 12:08:22 -0700512 set_board_env_params(params);
Marshall Dawson2942db62017-12-14 10:00:27 -0700513}
514
515void SetNbMidParams(GNB_MID_CONFIGURATION *params)
516{
517 /* 0=Primary and decode all VGA resources, 1=Secondary - decode none */
518 params->iGpuVgaMode = 0;
519 params->GnbIoapicAddress = IO_APIC2_ADDR;
520}