blob: 2ed315635cc8225b904a9c214051511263eb0b5b [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>
18#include <arch/acpi.h>
19#include <arch/acpigen.h>
20#include <cbmem.h>
21#include <chip.h>
22#include <console/console.h>
23#include <cpu/amd/amdfam15.h>
24#include <cpu/amd/mtrr.h>
25#include <cpu/cpu.h>
26#include <device/device.h>
27#include <device/pci.h>
28#include <device/pci_ids.h>
29#include <device/hypertransport.h>
30#include <lib.h>
31#include <agesawrapper.h>
32#include <agesawrapper_call.h>
33#include <soc/northbridge.h>
Marshall Dawson38bded02017-09-01 09:54:48 -060034#include <soc/pci_devs.h>
Marc Jones1587dc82017-05-15 18:55:11 -060035#include <stdint.h>
36#include <stdlib.h>
37#include <string.h>
38
39/*
40 * AMD vendorcode files. Place at the end so coreboot defaults and maintained
41 * and not set by vendorcode
42 */
43#include <AGESA.h>
44#include <cpuRegisters.h>
45#include <FieldAccessors.h>
46#include <Options.h>
47#include <Porting.h>
48#include <Topology.h>
49
Martin Roth59981982017-07-13 11:05:35 -070050#if IS_ENABLED(CONFIG_EXT_CONF_SUPPORT)
Marc Jones1587dc82017-05-15 18:55:11 -060051#error CONFIG_EXT_CONF_SUPPORT == 1 not support anymore!
52#endif
53
54typedef struct dram_base_mask {
Marshall Dawson4e101ad2017-06-15 12:17:38 -060055 u32 base; /* [47:27] at [28:8] */
56 u32 mask; /* [47:27] at [28:8] and enable at bit 0 */
Marc Jones1587dc82017-05-15 18:55:11 -060057} dram_base_mask_t;
58
Marshall Dawson38bded02017-09-01 09:54:48 -060059static dram_base_mask_t get_dram_base_mask(void)
Marc Jones1587dc82017-05-15 18:55:11 -060060{
Marshall Dawson38bded02017-09-01 09:54:48 -060061 device_t dev = dev_find_slot(0, ADDR_DEVFN);
Marc Jones1587dc82017-05-15 18:55:11 -060062 dram_base_mask_t d;
63 u32 temp;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060064
65 /* [39:24] at [31:16] */
Marshall Dawson38bded02017-09-01 09:54:48 -060066 temp = pci_read_config32(dev, 0x44);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060067
68 /* mask out DramMask [26:24] too */
69 d.mask = ((temp & 0xfff80000) >> (8 + 3));
70
71 /* [47:40] at [7:0] */
Marshall Dawson38bded02017-09-01 09:54:48 -060072 temp = pci_read_config32(dev, 0x144) & 0xff;
Marc Jones1587dc82017-05-15 18:55:11 -060073 d.mask |= temp << 21;
Marshall Dawson4e101ad2017-06-15 12:17:38 -060074
Marshall Dawson38bded02017-09-01 09:54:48 -060075 temp = pci_read_config32(dev, 0x40);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060076 d.mask |= (temp & 1); /* enable bit */
77 d.base = ((temp & 0xfff80000) >> (8 + 3));
Marshall Dawson38bded02017-09-01 09:54:48 -060078 temp = pci_read_config32(dev, 0x140) & 0xff;
Marc Jones1587dc82017-05-15 18:55:11 -060079 d.base |= temp << 21;
80 return d;
81}
82
83static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg,
84 u32 io_min, u32 io_max)
85{
86 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -060087 device_t addr_map = dev_find_slot(0, ADDR_DEVFN);
88
Marshall Dawson4e101ad2017-06-15 12:17:38 -060089 /* io range allocation. Limit */
90 tempreg = (nodeid & 0xf) | ((nodeid & 0x30) << (8 - 4)) | (linkn << 4)
91 | ((io_max & 0xf0) << (12 - 4));
Marshall Dawson38bded02017-09-01 09:54:48 -060092 pci_write_config32(addr_map, reg + 4, tempreg);
Marshall Dawson4e101ad2017-06-15 12:17:38 -060093 tempreg = 3 | ((io_min & 0xf0) << (12 - 4)); /* base: ISA and VGA ? */
Marshall Dawson38bded02017-09-01 09:54:48 -060094 pci_write_config32(addr_map, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -060095}
96
Marshall Dawson4e101ad2017-06-15 12:17:38 -060097static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index,
98 u32 mmio_min, u32 mmio_max)
Marc Jones1587dc82017-05-15 18:55:11 -060099{
100 u32 tempreg;
Marshall Dawson38bded02017-09-01 09:54:48 -0600101 device_t addr_map = dev_find_slot(0, ADDR_DEVFN);
102
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600103 /* io range allocation. Limit */
104 tempreg = (nodeid & 0xf) | (linkn << 4) | (mmio_max & 0xffffff00);
Marshall Dawson38bded02017-09-01 09:54:48 -0600105 pci_write_config32(addr_map, reg + 4, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -0600106 tempreg = 3 | (nodeid & 0x30) | (mmio_min & 0xffffff00);
Marshall Dawson38bded02017-09-01 09:54:48 -0600107 pci_write_config32(addr_map, reg, tempreg);
Marc Jones1587dc82017-05-15 18:55:11 -0600108}
109
Marc Jones1587dc82017-05-15 18:55:11 -0600110static void read_resources(device_t dev)
111{
Marc Jones1587dc82017-05-15 18:55:11 -0600112 /*
113 * This MMCONF resource must be reserved in the PCI domain.
114 * It is not honored by the coreboot resource allocator if it is in
115 * the CPU_CLUSTER.
116 */
117 mmconf_resource(dev, 0xc0010058);
118}
119
120static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
121{
122 resource_t rbase, rend;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600123 unsigned int reg, link_num;
Marc Jones1587dc82017-05-15 18:55:11 -0600124 char buf[50];
125
126 /* Make certain the resource has actually been set */
127 if (!(resource->flags & IORESOURCE_ASSIGNED))
128 return;
129
130 /* If I have already stored this resource don't worry about it */
131 if (resource->flags & IORESOURCE_STORED)
132 return;
133
134 /* Only handle PCI memory and IO resources */
135 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
136 return;
137
138 /* Ensure I am actually looking at a resource of function 1 */
139 if ((resource->index & 0xffff) < 0x1000)
140 return;
141
142 /* Get the base address */
143 rbase = resource->base;
144
145 /* Get the limit (rounded up) */
146 rend = resource_end(resource);
147
148 /* Get the register and link */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600149 reg = resource->index & 0xfff; /* 4k */
Marc Jones1587dc82017-05-15 18:55:11 -0600150 link_num = IOINDEX_LINK(resource->index);
151
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600152 if (resource->flags & IORESOURCE_IO)
Marc Jones1587dc82017-05-15 18:55:11 -0600153 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600154 else if (resource->flags & IORESOURCE_MEM)
155 set_mmio_addr_reg(nodeid, link_num, reg,
156 (resource->index >> 24), rbase >> 8, rend >> 8);
157
Marc Jones1587dc82017-05-15 18:55:11 -0600158 resource->flags |= IORESOURCE_STORED;
159 snprintf(buf, sizeof(buf), " <node %x link %x>",
160 nodeid, link_num);
161 report_resource_stored(dev, resource, buf);
162}
163
164/**
165 * I tried to reuse the resource allocation code in set_resource()
166 * but it is too difficult to deal with the resource allocation magic.
167 */
168
169static void create_vga_resource(device_t dev)
170{
171 struct bus *link;
172
173 /* find out which link the VGA card is connected,
174 * we only deal with the 'first' vga card */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600175 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600176 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
177 break;
Marc Jones1587dc82017-05-15 18:55:11 -0600178
179 /* no VGA card installed */
180 if (link == NULL)
181 return;
182
Marshall Dawsone2697de2017-09-06 10:46:36 -0600183 printk(BIOS_DEBUG, "VGA: %s has VGA device\n", dev_path(dev));
Marshall Dawson38bded02017-09-01 09:54:48 -0600184 /* Route A0000-BFFFF, IO 3B0-3BB 3C0-3DF */
185 pci_write_config32(dev_find_slot(0, ADDR_DEVFN), 0xf4, 1);
Marc Jones1587dc82017-05-15 18:55:11 -0600186}
187
188static void set_resources(device_t dev)
189{
190 struct bus *bus;
191 struct resource *res;
192
193
194 /* do we need this? */
195 create_vga_resource(dev);
196
197 /* Set each resource we have found */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600198 for (res = dev->resource_list ; res ; res = res->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600199 set_resource(dev, res, 0);
Marc Jones1587dc82017-05-15 18:55:11 -0600200
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600201 for (bus = dev->link_list ; bus ; bus = bus->next)
202 if (bus->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600203 assign_resources(bus);
Marc Jones1587dc82017-05-15 18:55:11 -0600204}
205
206static void northbridge_init(struct device *dev)
207{
208}
209
210static unsigned long acpi_fill_hest(acpi_hest_t *hest)
211{
212 void *addr, *current;
213
214 /* Skip the HEST header. */
215 current = (void *)(hest + 1);
216
217 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
218 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600219 current += acpi_create_hest_error_source(hest, current, 0,
220 (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600221
222 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
223 if (addr != NULL)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600224 current += acpi_create_hest_error_source(hest, current, 1,
225 (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
Marc Jones1587dc82017-05-15 18:55:11 -0600226
227 return (unsigned long)current;
228}
229
230static void northbridge_fill_ssdt_generator(device_t device)
231{
232 msr_t msr;
233 char pscope[] = "\\_SB.PCI0";
234
235 acpigen_write_scope(pscope);
236 msr = rdmsr(TOP_MEM);
237 acpigen_write_name_dword("TOM1", msr.lo);
238 msr = rdmsr(TOP_MEM2);
239 /*
240 * Since XP only implements parts of ACPI 2.0, we can't use a qword
241 * here.
242 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
243 * slide 22ff.
244 * Shift value right by 20 bit to make it fit into 32bit,
245 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
246 */
247 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
248 acpigen_pop_len();
249}
250
251static unsigned long agesa_write_acpi_tables(device_t device,
252 unsigned long current,
253 acpi_rsdp_t *rsdp)
254{
255 acpi_srat_t *srat;
256 acpi_slit_t *slit;
257 acpi_header_t *ssdt;
258 acpi_header_t *alib;
259 acpi_header_t *ivrs;
260 acpi_hest_t *hest;
261
262 /* HEST */
263 current = ALIGN(current, 8);
264 hest = (acpi_hest_t *)current;
265 acpi_write_hest((void *)current, acpi_fill_hest);
266 acpi_add_table(rsdp, (void *)current);
267 current += ((acpi_header_t *)current)->length;
268
269 current = ALIGN(current, 8);
270 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
271 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
272 if (ivrs != NULL) {
273 memcpy((void *)current, ivrs, ivrs->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600274 ivrs = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600275 current += ivrs->length;
276 acpi_add_table(rsdp, ivrs);
277 } else {
278 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
279 }
280
281 /* SRAT */
282 current = ALIGN(current, 8);
283 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600284 srat = (acpi_srat_t *)agesawrapper_getlateinitptr(PICK_SRAT);
Marc Jones1587dc82017-05-15 18:55:11 -0600285 if (srat != NULL) {
286 memcpy((void *)current, srat, srat->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600287 srat = (acpi_srat_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600288 current += srat->header.length;
289 acpi_add_table(rsdp, srat);
290 } else {
291 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
292 }
293
294 /* SLIT */
295 current = ALIGN(current, 8);
296 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600297 slit = (acpi_slit_t *)agesawrapper_getlateinitptr(PICK_SLIT);
Marc Jones1587dc82017-05-15 18:55:11 -0600298 if (slit != NULL) {
299 memcpy((void *)current, slit, slit->header.length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600300 slit = (acpi_slit_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600301 current += slit->header.length;
302 acpi_add_table(rsdp, slit);
303 } else {
304 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
305 }
306
307 /* ALIB */
308 current = ALIGN(current, 16);
309 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600310 alib = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_ALIB);
Marc Jones1587dc82017-05-15 18:55:11 -0600311 if (alib != NULL) {
312 memcpy((void *)current, alib, alib->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600313 alib = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600314 current += alib->length;
315 acpi_add_table(rsdp, (void *)alib);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600316 } else {
317 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL."
318 " Skipping.\n");
Marc Jones1587dc82017-05-15 18:55:11 -0600319 }
320
Marc Jones1587dc82017-05-15 18:55:11 -0600321 current = ALIGN(current, 16);
322 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600323 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr(PICK_PSTATE);
Marc Jones1587dc82017-05-15 18:55:11 -0600324 if (ssdt != NULL) {
325 memcpy((void *)current, ssdt, ssdt->length);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600326 ssdt = (acpi_header_t *)current;
Marc Jones1587dc82017-05-15 18:55:11 -0600327 current += ssdt->length;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600328 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600329 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
330 }
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600331 acpi_add_table(rsdp, ssdt);
Marc Jones1587dc82017-05-15 18:55:11 -0600332
333 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
334 return current;
335}
336
337static struct device_operations northbridge_operations = {
338 .read_resources = read_resources,
339 .set_resources = set_resources,
340 .enable_resources = pci_dev_enable_resources,
341 .init = northbridge_init,
342 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
343 .write_acpi_tables = agesa_write_acpi_tables,
344 .enable = 0,
345 .ops_pci = 0,
346};
347
348static const struct pci_driver family15_northbridge __pci_driver = {
349 .ops = &northbridge_operations,
350 .vendor = PCI_VENDOR_ID_AMD,
351 .device = PCI_DEVICE_ID_AMD_15H_MODEL_707F_NB_HT,
352};
353
354void fam15_finalize(void *chip_info)
355{
356 device_t dev;
357 u32 value;
358 dev = dev_find_slot(0, PCI_DEVFN(0, 0)); /* clear IoapicSbFeatureEn */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600359 pci_write_config32(dev, 0xf8, 0);
360 pci_write_config32(dev, 0xfc, 5); /* TODO: move it to dsdt.asl */
Marc Jones1587dc82017-05-15 18:55:11 -0600361
362 /* disable No Snoop */
363 dev = dev_find_slot(0, PCI_DEVFN(1, 1));
364 value = pci_read_config32(dev, 0x60);
365 value &= ~(1 << 11);
366 pci_write_config32(dev, 0x60, value);
367}
368
369void domain_read_resources(device_t dev)
370{
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600371 unsigned int reg;
Marshall Dawson38bded02017-09-01 09:54:48 -0600372 device_t addr_map = dev_find_slot(0, ADDR_DEVFN);
Marc Jones1587dc82017-05-15 18:55:11 -0600373
374 /* Find the already assigned resource pairs */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600375 for (reg = 0x80 ; reg <= 0xd8 ; reg += 0x08) {
Marc Jones1587dc82017-05-15 18:55:11 -0600376 u32 base, limit;
Marshall Dawson38bded02017-09-01 09:54:48 -0600377 base = pci_read_config32(addr_map, reg);
378 limit = pci_read_config32(addr_map, reg + 4);
Marc Jones1587dc82017-05-15 18:55:11 -0600379 /* Is this register allocated? */
380 if ((base & 3) != 0) {
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600381 unsigned int nodeid, reg_link;
Marshall Dawson38bded02017-09-01 09:54:48 -0600382 device_t reg_dev = dev_find_slot(0, HT_DEVFN);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600383 if (reg < 0xc0) /* mmio */
Marc Jones1587dc82017-05-15 18:55:11 -0600384 nodeid = (limit & 0xf) + (base & 0x30);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600385 else /* io */
Marc Jones1587dc82017-05-15 18:55:11 -0600386 nodeid = (limit & 0xf) + ((base >> 4) & 0x30);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600387
Marc Jones1587dc82017-05-15 18:55:11 -0600388 reg_link = (limit >> 4) & 7;
Marc Jones1587dc82017-05-15 18:55:11 -0600389 if (reg_dev) {
390 /* Reserve the resource */
391 struct resource *res;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600392 res = new_resource(reg_dev,
393 IOINDEX(0x1000 + reg,
394 reg_link));
395 if (res)
Marc Jones1587dc82017-05-15 18:55:11 -0600396 res->flags = 1;
Marc Jones1587dc82017-05-15 18:55:11 -0600397 }
398 }
399 }
400 /* FIXME: do we need to check extend conf space?
401 I don't believe that much preset value */
402
403 pci_domain_read_resources(dev);
404}
405
406void domain_enable_resources(device_t dev)
407{
408 if (acpi_is_wakeup_s3())
409 AGESAWRAPPER(fchs3laterestore);
410
411 /* Must be called after PCI enumeration and resource allocation */
412 if (!acpi_is_wakeup_s3())
413 AGESAWRAPPER(amdinitmid);
414
415 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
416}
417
418#if CONFIG_HW_MEM_HOLE_SIZEK != 0
419struct hw_mem_hole_info {
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600420 unsigned int hole_startk;
Marc Jones1587dc82017-05-15 18:55:11 -0600421 int node_id;
422};
423
424static struct hw_mem_hole_info get_hw_mem_hole_info(void)
425{
426 struct hw_mem_hole_info mem_hole;
427 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
428 mem_hole.node_id = -1;
429 dram_base_mask_t d;
430 u32 hole;
Marshall Dawson38bded02017-09-01 09:54:48 -0600431 d = get_dram_base_mask();
432 hole = pci_read_config32(dev_find_slot(0, ADDR_DEVFN), 0xf0);
Marc Jones1587dc82017-05-15 18:55:11 -0600433 if (hole & 2) {
434 /* We found the hole */
435 mem_hole.hole_startk = (hole & (0xff << 24)) >> 10;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600436 mem_hole.node_id = 0; /* record the node # with hole */
Marc Jones1587dc82017-05-15 18:55:11 -0600437 }
438
439 return mem_hole;
440}
441#endif
442
443void domain_set_resources(device_t dev)
444{
445 unsigned long mmio_basek;
446 u32 pci_tolm;
Marshall Dawson38bded02017-09-01 09:54:48 -0600447 int idx;
Marc Jones1587dc82017-05-15 18:55:11 -0600448 struct bus *link;
449#if CONFIG_HW_MEM_HOLE_SIZEK != 0
450 struct hw_mem_hole_info mem_hole;
451 u32 reset_memhole = 1;
452#endif
453
454 pci_tolm = 0xffffffffUL;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600455 for (link = dev->link_list ; link ; link = link->next)
Marc Jones1587dc82017-05-15 18:55:11 -0600456 pci_tolm = find_pci_tolm(link);
Marc Jones1587dc82017-05-15 18:55:11 -0600457
458 mmio_basek = pci_tolm >> 10;
459 /* Round mmio_basek to something the processor can support */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600460 mmio_basek &= ~((1 << 6) - 1);
Marc Jones1587dc82017-05-15 18:55:11 -0600461
462 /* FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
463 * MMIO hole. If you fix this here, please fix amdk8, too.
464 */
465 /* Round the mmio hole to 64M */
466 mmio_basek &= ~((64 * 1024) - 1);
467
468#if CONFIG_HW_MEM_HOLE_SIZEK != 0
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600469 /* if the hw mem hole is already set in raminit stage, here we will
470 * compare mmio_basek and hole_basek. if mmio_basek is bigger that
471 * hole_basek and will use hole_basek as mmio_basek and we don't need
472 * to reset hole. Otherwise we reset the hole to the mmio_basek
Marc Jones1587dc82017-05-15 18:55:11 -0600473 */
474
475 mem_hole = get_hw_mem_hole_info();
476
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600477 /* Use hole_basek as mmio_basek, and no need to reset hole anymore */
Marc Jones1587dc82017-05-15 18:55:11 -0600478 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
479 mmio_basek = mem_hole.hole_startk;
480 reset_memhole = 0;
481 }
482#endif
483
484 idx = 0x10;
Marshall Dawson38bded02017-09-01 09:54:48 -0600485 dram_base_mask_t d;
486 resource_t basek, limitk, sizek; /* 4 1T */
Marc Jones1587dc82017-05-15 18:55:11 -0600487
Marshall Dawson38bded02017-09-01 09:54:48 -0600488 d = get_dram_base_mask();
Marc Jones1587dc82017-05-15 18:55:11 -0600489
Marshall Dawson38bded02017-09-01 09:54:48 -0600490 if ((d.mask & 1)) { /* if enabled... */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600491 /* could overflow, we may lose 6 bit here */
492 basek = ((resource_t)(d.base & 0x1fffff00)) << 9;
493 limitk = ((resource_t)(((d.mask & ~1) + 0x000ff)
494 & 0x1fffff00)) << 9;
Marc Jones1587dc82017-05-15 18:55:11 -0600495
496 sizek = limitk - basek;
497
498 /* see if we need a hole from 0xa0000 to 0xbffff */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600499 if ((basek < ((8 * 64) + (8 * 16))) && (sizek > ((8 * 64) +
500 (16 * 16)))) {
Marshall Dawson38bded02017-09-01 09:54:48 -0600501 ram_resource(dev, idx, basek,
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600502 ((8 * 64) + (8 * 16)) - basek);
Marc Jones1587dc82017-05-15 18:55:11 -0600503 idx += 0x10;
504 basek = (8 * 64) + (16 * 16);
505 sizek = limitk - ((8 * 64) + (16 * 16));
506
507 }
508
509 /* split the region to accommodate pci memory space */
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600510 if ((basek < 4 * 1024 * 1024) && (limitk > mmio_basek)) {
Marc Jones1587dc82017-05-15 18:55:11 -0600511 if (basek <= mmio_basek) {
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600512 unsigned int pre_sizek;
Marc Jones1587dc82017-05-15 18:55:11 -0600513 pre_sizek = mmio_basek - basek;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600514 if (pre_sizek > 0) {
Marshall Dawson38bded02017-09-01 09:54:48 -0600515 ram_resource(dev, idx, basek,
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600516 pre_sizek);
Marc Jones1587dc82017-05-15 18:55:11 -0600517 idx += 0x10;
518 sizek -= pre_sizek;
519 }
520 basek = mmio_basek;
521 }
522 if ((basek + sizek) <= 4 * 1024 * 1024) {
523 sizek = 0;
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600524 } else {
Marc Jones1587dc82017-05-15 18:55:11 -0600525 uint64_t topmem2 = bsp_topmem2();
526 basek = 4 * 1024 * 1024;
527 sizek = topmem2 / 1024 - basek;
528 }
529 }
530
Marshall Dawson38bded02017-09-01 09:54:48 -0600531 ram_resource(dev, idx, basek, sizek);
532 printk(BIOS_DEBUG, "node 0: mmio_basek=%08lx, basek=%08llx,"
533 " limitk=%08llx\n", mmio_basek, basek, limitk);
Marc Jones1587dc82017-05-15 18:55:11 -0600534 }
535
536 add_uma_resource_below_tolm(dev, 7);
537
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600538 for (link = dev->link_list ; link ; link = link->next)
539 if (link->children)
Marc Jones1587dc82017-05-15 18:55:11 -0600540 assign_resources(link);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600541
Marc Jones1587dc82017-05-15 18:55:11 -0600542 /*
543 * Reserve everything between A segment and 1MB:
544 *
545 * 0xa0000 - 0xbffff: legacy VGA
546 * 0xc0000 - 0xfffff: RAM
547 */
548 mmio_resource(dev, 0xa0000, 0xa0000 / KiB, 0x20000 / KiB);
549 reserved_ram_resource(dev, 0xc0000, 0xc0000 / KiB, 0x40000 / KiB);
550}
551
Marc Jones1587dc82017-05-15 18:55:11 -0600552void cpu_bus_scan(device_t dev)
553{
554 struct bus *cpu_bus;
555 device_t cpu;
556 device_t cdb_dev;
557 device_t dev_mc;
558 int j;
559 int core_max;
560 int core_nums;
561 int siblings;
562 int family;
563 int enable_node;
564 u32 lapicid_start;
565 u32 apic_id;
566 u32 pccount;
567
568
569 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
570 if (!dev_mc) {
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600571 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB,
572 CONFIG_CDB);
Marc Jones1587dc82017-05-15 18:55:11 -0600573 die("");
574 }
Marc Jones1587dc82017-05-15 18:55:11 -0600575
576 /* Get max and actual number of cores */
577 pccount = cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT);
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600578 core_max = 1 << ((pccount >> 12) & 0xf);
Marc Jones1587dc82017-05-15 18:55:11 -0600579 core_nums = (pccount & 0xF);
580
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600581 family = (cpuid_eax(1) >> 20) & 0xff;
Marc Jones1587dc82017-05-15 18:55:11 -0600582
583 cdb_dev = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 5));
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600584 siblings = pci_read_config32(cdb_dev, 0x84) & 0xff;
Marc Jones1587dc82017-05-15 18:55:11 -0600585
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600586 printk(BIOS_SPEW, "%s family%xh, core_max=%d, core_nums=%d,"
587 " siblings=%d\n", dev_path(cdb_dev), 0x0f + family,
588 core_max, core_nums, siblings);
Marc Jones1587dc82017-05-15 18:55:11 -0600589
590 /*
591 * APIC ID calucation is tightly coupled with AGESA v5 code.
592 * This calculation MUST match the assignment calculation done
593 * in LocalApicInitializationAtEarly() function.
594 * And reference GetLocalApicIdForCore()
595 *
596 * Apply apic enumeration rules
597 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
598 * put the local-APICs at m..z
599 *
600 * This is needed because many IO-APIC devices only have 4 bits
601 * for their APIC id and therefore must reside at 0..15
602 */
603
604 /*
605 * While the above statement is true, we know some things about
606 * this silicon. It is an SOC and can't have >= 16 APICs, but
607 * we will start numbering at 0x10. We also know there is only
608 * on physical node (module in AMD speak).
609 */
610
611 lapicid_start = 0x10; /* Get this from devicetree? see comment above. */
Martin Kepplinger9460a982017-06-29 10:57:55 +0200612 enable_node = cdb_dev->enabled;
Marc Jones1587dc82017-05-15 18:55:11 -0600613 cpu_bus = dev->link_list;
614
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600615 for (j = 0 ; j <= siblings ; j++) {
Marc Jones1587dc82017-05-15 18:55:11 -0600616 apic_id = lapicid_start + j;
Marshall Dawson38bded02017-09-01 09:54:48 -0600617 printk(BIOS_SPEW, "lapicid_start 0x%x, core 0x%x,"
618 " apicid=0x%x\n", lapicid_start, j, apic_id);
Marc Jones1587dc82017-05-15 18:55:11 -0600619
620 cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
621 if (cpu)
Marshall Dawson38bded02017-09-01 09:54:48 -0600622 amd_cpu_topology(cpu, 1, j);
Marc Jones1587dc82017-05-15 18:55:11 -0600623 }
624}
625
626/*********************************************************************
627 * Change the vendor / device IDs to match the generic VBIOS header. *
628 *********************************************************************/
629u32 map_oprom_vendev(u32 vendev)
630{
631 u32 new_vendev;
632 new_vendev =
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600633 ((vendev >= 0x100298e0) && (vendev <= 0x100298ef)) ?
634 0x100298e0 : vendev;
Marc Jones1587dc82017-05-15 18:55:11 -0600635
636 if (vendev != new_vendev)
Marshall Dawson4e101ad2017-06-15 12:17:38 -0600637 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n",
638 vendev, new_vendev);
Marc Jones1587dc82017-05-15 18:55:11 -0600639
640 return new_vendev;
641}