blob: b59ff6c84cc3c7ed13dda8555c46a88cefc181a7 [file] [log] [blame]
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <console/console.h>
21#include <arch/io.h>
22#include <arch/acpi.h>
23#include <stdint.h>
24#include <device/device.h>
25#include <device/pci.h>
26#include <device/pci_ids.h>
27#include <device/hypertransport.h>
28#include <stdlib.h>
29#include <string.h>
30#include <lib.h>
31#include <cpu/cpu.h>
32#include <cbmem.h>
33
34#include <Porting.h>
35#include <AGESA.h>
36#include <FieldAccessors.h>
37#include <Options.h>
38#include <Topology.h>
39#include <cpu/amd/amdfam16.h>
40#include <cpuRegisters.h>
Kyösti Mälkki023ed1f2014-10-22 08:05:36 +030041#include <northbridge/amd/pi/agesawrapper.h>
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +030042#include <northbridge/amd/pi/agesawrapper_call.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060043#include "northbridge.h"
44
45#include <cpu/x86/lapic.h>
46#include <cpu/amd/mtrr.h>
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +020047#include <arch/acpi.h>
48#include <arch/acpigen.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060049
50#define MAX_NODE_NUMS (MAX_NODES * MAX_DIES)
51
Bruce Griffith27ed80b2014-08-15 11:46:25 -060052typedef struct dram_base_mask {
53 u32 base; //[47:27] at [28:8]
54 u32 mask; //[47:27] at [28:8] and enable at bit 0
55} dram_base_mask_t;
56
57static unsigned node_nums;
58static unsigned sblink;
59static device_t __f0_dev[MAX_NODE_NUMS];
60static device_t __f1_dev[MAX_NODE_NUMS];
61static device_t __f2_dev[MAX_NODE_NUMS];
62static device_t __f4_dev[MAX_NODE_NUMS];
63static unsigned fx_devs = 0;
64
65static dram_base_mask_t get_dram_base_mask(u32 nodeid)
66{
67 device_t dev;
68 dram_base_mask_t d;
69 dev = __f1_dev[0];
70 u32 temp;
71 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
72 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
73 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
74 d.mask |= temp<<21;
75 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
76 d.mask |= (temp & 1); // enable bit
77 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
78 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
79 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 i;
87 u32 tempreg;
88 /* io range allocation */
89 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
90 for (i=0; i<node_nums; i++)
91 pci_write_config32(__f1_dev[i], reg+4, tempreg);
92 tempreg = 3 /*| ( 3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
93#if 0
94 // FIXME: can we use VGA reg instead?
95 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
96 printk(BIOS_SPEW, "%s, enabling legacy VGA IO forwarding for %s link %s\n",
97 __func__, dev_path(dev), link);
98 tempreg |= PCI_IO_BASE_VGA_EN;
99 }
100 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
101 tempreg |= PCI_IO_BASE_NO_ISA;
102 }
103#endif
104 for (i=0; i<node_nums; i++)
105 pci_write_config32(__f1_dev[i], reg, tempreg);
106}
107
108static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
109{
110 u32 i;
111 u32 tempreg;
112 /* io range allocation */
113 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
114 for (i=0; i<nodes; i++)
115 pci_write_config32(__f1_dev[i], reg+4, tempreg);
116 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
117 for (i=0; i<node_nums; i++)
118 pci_write_config32(__f1_dev[i], reg, tempreg);
119}
120
121static device_t get_node_pci(u32 nodeid, u32 fn)
122{
123#if MAX_NODE_NUMS + CONFIG_CDB >= 32
124 if ((CONFIG_CDB + nodeid) < 32) {
125 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
126 } else {
127 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
128 }
129#else
130 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
131#endif
132}
133
134static void get_fx_devs(void)
135{
136 int i;
137 for (i = 0; i < MAX_NODE_NUMS; i++) {
138 __f0_dev[i] = get_node_pci(i, 0);
139 __f1_dev[i] = get_node_pci(i, 1);
140 __f2_dev[i] = get_node_pci(i, 2);
141 __f4_dev[i] = get_node_pci(i, 4);
142 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
143 fx_devs = i+1;
144 }
145 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
146 die("Cannot find 0:0x18.[0|1]\n");
147 }
148 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
149}
150
151static u32 f1_read_config32(unsigned reg)
152{
153 if (fx_devs == 0)
154 get_fx_devs();
155 return pci_read_config32(__f1_dev[0], reg);
156}
157
158static void f1_write_config32(unsigned reg, u32 value)
159{
160 int i;
161 if (fx_devs == 0)
162 get_fx_devs();
163 for(i = 0; i < fx_devs; i++) {
164 device_t dev;
165 dev = __f1_dev[i];
166 if (dev && dev->enabled) {
167 pci_write_config32(dev, reg, value);
168 }
169 }
170}
171
172static u32 amdfam16_nodeid(device_t dev)
173{
174#if MAX_NODE_NUMS == 64
175 unsigned busn;
176 busn = dev->bus->secondary;
177 if (busn != CONFIG_CBB) {
178 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
179 } else {
180 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
181 }
182
183#else
184 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
185#endif
186}
187
188static void set_vga_enable_reg(u32 nodeid, u32 linkn)
189{
190 u32 val;
191
192 val = 1 | (nodeid<<4) | (linkn<<12);
193 /* it will routing
194 * (1)mmio 0xa0000:0xbffff
195 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
196 */
197 f1_write_config32(0xf4, val);
198
199}
200
201/**
202 * @return
203 * @retval 2 resoure does not exist, usable
204 * @retval 0 resource exists, not usable
205 * @retval 1 resource exist, resource has been allocated before
206 */
207static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
208 unsigned goal_link)
209{
210 struct resource *res;
211 unsigned nodeid, link = 0;
212 int result;
213 res = 0;
214 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
215 device_t dev;
216 dev = __f0_dev[nodeid];
217 if (!dev)
218 continue;
219 for (link = 0; !res && (link < 8); link++) {
220 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
221 }
222 }
223 result = 2;
224 if (res) {
225 result = 0;
226 if ((goal_link == (link - 1)) &&
227 (goal_nodeid == (nodeid - 1)) &&
228 (res->flags <= 1)) {
229 result = 1;
230 }
231 }
232 return result;
233}
234
235static struct resource *amdfam16_find_iopair(device_t dev, unsigned nodeid, unsigned link)
236{
237 struct resource *resource;
238 u32 free_reg, reg;
239 resource = 0;
240 free_reg = 0;
241 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
242 int result;
243 result = reg_useable(reg, dev, nodeid, link);
244 if (result == 1) {
245 /* I have been allocated this one */
246 break;
247 }
248 else if (result > 1) {
249 /* I have a free register pair */
250 free_reg = reg;
251 }
252 }
253 if (reg > 0xd8) {
254 reg = free_reg; // if no free, the free_reg still be 0
255 }
256
257 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
258
259 return resource;
260}
261
262static struct resource *amdfam16_find_mempair(device_t dev, u32 nodeid, u32 link)
263{
264 struct resource *resource;
265 u32 free_reg, reg;
266 resource = 0;
267 free_reg = 0;
268 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
269 int result;
270 result = reg_useable(reg, dev, nodeid, link);
271 if (result == 1) {
272 /* I have been allocated this one */
273 break;
274 }
275 else if (result > 1) {
276 /* I have a free register pair */
277 free_reg = reg;
278 }
279 }
280 if (reg > 0xb8) {
281 reg = free_reg;
282 }
283
284 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
285 return resource;
286}
287
288static void amdfam16_link_read_bases(device_t dev, u32 nodeid, u32 link)
289{
290 struct resource *resource;
291
292 /* Initialize the io space constraints on the current bus */
293 resource = amdfam16_find_iopair(dev, nodeid, link);
294 if (resource) {
295 u32 align;
296 align = log2(HT_IO_HOST_ALIGN);
297 resource->base = 0;
298 resource->size = 0;
299 resource->align = align;
300 resource->gran = align;
301 resource->limit = 0xffffUL;
302 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
303 }
304
305 /* Initialize the prefetchable memory constraints on the current bus */
306 resource = amdfam16_find_mempair(dev, nodeid, link);
307 if (resource) {
308 resource->base = 0;
309 resource->size = 0;
310 resource->align = log2(HT_MEM_HOST_ALIGN);
311 resource->gran = log2(HT_MEM_HOST_ALIGN);
312 resource->limit = 0xffffffffffULL;
313 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
314 resource->flags |= IORESOURCE_BRIDGE;
315 }
316
317 /* Initialize the memory constraints on the current bus */
318 resource = amdfam16_find_mempair(dev, nodeid, link);
319 if (resource) {
320 resource->base = 0;
321 resource->size = 0;
322 resource->align = log2(HT_MEM_HOST_ALIGN);
323 resource->gran = log2(HT_MEM_HOST_ALIGN);
324 resource->limit = 0xffffffffffULL;
325 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
326 }
327
328}
329
330static void read_resources(device_t dev)
331{
332 u32 nodeid;
333 struct bus *link;
334
335 nodeid = amdfam16_nodeid(dev);
336 for (link = dev->link_list; link; link = link->next) {
337 if (link->children) {
338 amdfam16_link_read_bases(dev, nodeid, link->link_num);
339 }
340 }
341}
342
343static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
344{
345 resource_t rbase, rend;
346 unsigned reg, link_num;
347 char buf[50];
348
349 /* Make certain the resource has actually been set */
350 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
351 return;
352 }
353
354 /* If I have already stored this resource don't worry about it */
355 if (resource->flags & IORESOURCE_STORED) {
356 return;
357 }
358
359 /* Only handle PCI memory and IO resources */
360 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
361 return;
362
363 /* Ensure I am actually looking at a resource of function 1 */
364 if ((resource->index & 0xffff) < 0x1000) {
365 return;
366 }
367 /* Get the base address */
368 rbase = resource->base;
369
370 /* Get the limit (rounded up) */
371 rend = resource_end(resource);
372
373 /* Get the register and link */
374 reg = resource->index & 0xfff; // 4k
375 link_num = IOINDEX_LINK(resource->index);
376
377 if (resource->flags & IORESOURCE_IO) {
378 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
379 }
380 else if (resource->flags & IORESOURCE_MEM) {
381 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums) ;// [39:8]
382 }
383 resource->flags |= IORESOURCE_STORED;
384 snprintf(buf, sizeof (buf), " <node %x link %x>",
385 nodeid, link_num);
386 report_resource_stored(dev, resource, buf);
387}
388
389/**
390 * I tried to reuse the resource allocation code in set_resource()
391 * but it is too difficult to deal with the resource allocation magic.
392 */
393
394static void create_vga_resource(device_t dev, unsigned nodeid)
395{
396 struct bus *link;
397
398 /* find out which link the VGA card is connected,
399 * we only deal with the 'first' vga card */
400 for (link = dev->link_list; link; link = link->next) {
401 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
402#if CONFIG_MULTIPLE_VGA_ADAPTERS
403 extern device_t vga_pri; // the primary vga device, defined in device.c
404 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
405 link->secondary,link->subordinate);
406 /* We need to make sure the vga_pri is under the link */
407 if((vga_pri->bus->secondary >= link->secondary ) &&
408 (vga_pri->bus->secondary <= link->subordinate )
409 )
410#endif
411 break;
412 }
413 }
414
415 /* no VGA card installed */
416 if (link == NULL)
417 return;
418
419 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
420 set_vga_enable_reg(nodeid, sblink);
421}
422
423static void set_resources(device_t dev)
424{
425 unsigned nodeid;
426 struct bus *bus;
427 struct resource *res;
428
429 /* Find the nodeid */
430 nodeid = amdfam16_nodeid(dev);
431
432 create_vga_resource(dev, nodeid); //TODO: do we need this?
433
434 /* Set each resource we have found */
435 for (res = dev->resource_list; res; res = res->next) {
436 set_resource(dev, res, nodeid);
437 }
438
439 for (bus = dev->link_list; bus; bus = bus->next) {
440 if (bus->children) {
441 assign_resources(bus);
442 }
443 }
444}
445
446static void northbridge_init(struct device *dev)
447{
448}
449#if 0 /* TODO: Check if needed. */
450static unsigned scan_chains(device_t dev, unsigned max)
451{
452 unsigned nodeid;
453 struct bus *link;
454 device_t io_hub = NULL;
455 u32 next_unitid = 0x18;
456 nodeid = amdfam16_nodeid(dev);
457 if (nodeid == 0) {
458 for (link = dev->link_list; link; link = link->next) {
459 //if (link->link_num == sblink) { /* devicetree put IO Hub on link_lsit[sblink] */
460 if (link->link_num == 0) { /* devicetree put IO Hub on link_lsit[0] */
461 io_hub = link->children;
462 if (!io_hub || !io_hub->enabled) {
463 die("I can't find the IO Hub, or IO Hub not enabled, please check the device tree.\n");
464 }
465 /* Now that nothing is overlapping it is safe to scan the children. */
466 max = pci_scan_bus(link, 0x00, ((next_unitid - 1) << 3) | 7, 0);
467 }
468 }
469 }
470 return max;
471}
472#endif
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200473
474
475unsigned long acpi_fill_hest(acpi_hest_t *hest)
476{
477 void *addr, *current;
478
479 /* Skip the HEST header. */
480 current = (void *)(hest + 1);
481
482 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
483 if (addr != NULL)
484 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
485
486 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
487 if (addr != NULL)
488 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
489
490 return (unsigned long)current;
491}
492
493/* Implemented with AGESA-specific code. Dummy to keep linker happy. */
494unsigned long acpi_fill_slit(unsigned long current)
495{
496 return current;
497}
498
499/* Implemented with AGESA-specific code. Dummy to keep linker happy. */
500unsigned long acpi_fill_srat(unsigned long current)
501{
502 return current;
503}
504
505static void northbridge_fill_ssdt_generator(void)
506{
507 msr_t msr;
508 char pscope[] = "\\_SB.PCI0";
509
510 acpigen_write_scope(pscope);
511 msr = rdmsr(TOP_MEM);
512 acpigen_write_name_dword("TOM1", msr.lo);
513 msr = rdmsr(TOP_MEM2);
514 /*
515 * Since XP only implements parts of ACPI 2.0, we can't use a qword
516 * here.
517 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
518 * slide 22ff.
519 * Shift value right by 20 bit to make it fit into 32bit,
520 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
521 */
522 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
523 acpigen_pop_len();
524}
525
526static unsigned long agesa_write_acpi_tables(unsigned long current,
527 acpi_rsdp_t *rsdp)
528{
529 acpi_srat_t *srat;
530 acpi_slit_t *slit;
531 acpi_header_t *ssdt;
532 acpi_header_t *alib;
533 acpi_header_t *ivrs;
534 acpi_hest_t *hest;
535
536 /* HEST */
537 current = ALIGN(current, 8);
538 hest = (acpi_hest_t *)current;
539 acpi_write_hest((void *)current);
540 acpi_add_table(rsdp, (void *)current);
541 current += ((acpi_header_t *)current)->length;
542
543 current = ALIGN(current, 8);
544 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
545 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
546 if (ivrs != NULL) {
547 memcpy((void *)current, ivrs, ivrs->length);
548 ivrs = (acpi_header_t *) current;
549 current += ivrs->length;
550 acpi_add_table(rsdp, ivrs);
551 } else {
552 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
553 }
554
555 /* SRAT */
556 current = ALIGN(current, 8);
557 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
558 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
559 if (srat != NULL) {
560 memcpy((void *)current, srat, srat->header.length);
561 srat = (acpi_srat_t *) current;
562 current += srat->header.length;
563 acpi_add_table(rsdp, srat);
564 } else {
565 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
566 }
567
568 /* SLIT */
569 current = ALIGN(current, 8);
570 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
571 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
572 if (slit != NULL) {
573 memcpy((void *)current, slit, slit->header.length);
574 slit = (acpi_slit_t *) current;
575 current += slit->header.length;
576 acpi_add_table(rsdp, slit);
577 } else {
578 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
579 }
580
581 /* ALIB */
582 current = ALIGN(current, 16);
583 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
584 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
585 if (alib != NULL) {
586 memcpy((void *)current, alib, alib->length);
587 alib = (acpi_header_t *) current;
588 current += alib->length;
589 acpi_add_table(rsdp, (void *)alib);
590 }
591 else {
592 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
593 }
594
595 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
596 /* SSDT */
597 current = ALIGN(current, 16);
598 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
599 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
600 if (ssdt != NULL) {
601 memcpy((void *)current, ssdt, ssdt->length);
602 ssdt = (acpi_header_t *) current;
603 current += ssdt->length;
604 }
605 else {
606 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
607 }
608 acpi_add_table(rsdp,ssdt);
609
610 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
611 return current;
612}
613
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600614static struct device_operations northbridge_operations = {
615 .read_resources = read_resources,
616 .set_resources = set_resources,
617 .enable_resources = pci_dev_enable_resources,
618 .init = northbridge_init,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200619 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
620 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600621 //.scan_bus = scan_chains, /* TODO: */
622 .enable = 0,
623 .ops_pci = 0,
624};
625
626static const struct pci_driver family16_northbridge __pci_driver = {
627 .ops = &northbridge_operations,
628 .vendor = PCI_VENDOR_ID_AMD,
629 .device = PCI_DEVICE_ID_AMD_16H_MODEL_003F_NB_HT,
630};
631
632static const struct pci_driver family10_northbridge __pci_driver = {
633 .ops = &northbridge_operations,
634 .vendor = PCI_VENDOR_ID_AMD,
635 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
636};
637
Dave Frodin891f71a2015-01-19 15:58:24 -0700638static void fam16_finalize(void *chip_info)
639{
640 device_t dev;
641 u32 value;
642 dev = dev_find_slot(0, PCI_DEVFN(0, 0)); /* clear IoapicSbFeatureEn */
643 pci_write_config32(dev, 0xF8, 0);
644 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
645
646 /* disable No Snoop */
647 dev = dev_find_slot(0, PCI_DEVFN(1, 1));
648 value = pci_read_config32(dev, 0x60);
649 value &= ~(1 << 11);
650 pci_write_config32(dev, 0x60, value);
651}
652
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300653struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600654 CHIP_NAME("AMD FAM16 Northbridge")
655 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700656 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600657};
658
659static void domain_read_resources(device_t dev)
660{
661 unsigned reg;
662
663 /* Find the already assigned resource pairs */
664 get_fx_devs();
665 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
666 u32 base, limit;
667 base = f1_read_config32(reg);
668 limit = f1_read_config32(reg + 0x04);
669 /* Is this register allocated? */
670 if ((base & 3) != 0) {
671 unsigned nodeid, reg_link;
672 device_t reg_dev;
673 if (reg<0xc0) { // mmio
674 nodeid = (limit & 0xf) + (base&0x30);
675 } else { // io
676 nodeid = (limit & 0xf) + ((base>>4)&0x30);
677 }
678 reg_link = (limit >> 4) & 7;
679 reg_dev = __f0_dev[nodeid];
680 if (reg_dev) {
681 /* Reserve the resource */
682 struct resource *res;
683 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
684 if (res) {
685 res->flags = 1;
686 }
687 }
688 }
689 }
690 /* FIXME: do we need to check extend conf space?
691 I don't believe that much preset value */
692
693#if !CONFIG_PCI_64BIT_PREF_MEM
694 pci_domain_read_resources(dev);
695
696#else
697 struct bus *link;
698 struct resource *resource;
699 for (link=dev->link_list; link; link = link->next) {
700 /* Initialize the system wide io space constraints */
701 resource = new_resource(dev, 0|(link->link_num<<2));
702 resource->base = 0x400;
703 resource->limit = 0xffffUL;
704 resource->flags = IORESOURCE_IO;
705
706 /* Initialize the system wide prefetchable memory resources constraints */
707 resource = new_resource(dev, 1|(link->link_num<<2));
708 resource->limit = 0xfcffffffffULL;
709 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
710
711 /* Initialize the system wide memory resources constraints */
712 resource = new_resource(dev, 2|(link->link_num<<2));
713 resource->limit = 0xfcffffffffULL;
714 resource->flags = IORESOURCE_MEM;
715 }
716#endif
717}
718
719static void domain_enable_resources(device_t dev)
720{
721 if (acpi_is_wakeup_s3())
722 AGESAWRAPPER(fchs3laterestore);
723
724 /* Must be called after PCI enumeration and resource allocation */
725 if (!acpi_is_wakeup_s3())
726 AGESAWRAPPER(amdinitmid);
727
728 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
729}
730
731#if CONFIG_HW_MEM_HOLE_SIZEK != 0
732struct hw_mem_hole_info {
733 unsigned hole_startk;
734 int node_id;
735};
736static struct hw_mem_hole_info get_hw_mem_hole_info(void)
737{
738 struct hw_mem_hole_info mem_hole;
739 int i;
740 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
741 mem_hole.node_id = -1;
742 for (i = 0; i < node_nums; i++) {
743 dram_base_mask_t d;
744 u32 hole;
745 d = get_dram_base_mask(i);
746 if (!(d.mask & 1)) continue; // no memory on this node
747 hole = pci_read_config32(__f1_dev[i], 0xf0);
748 if (hole & 2) { // we find the hole
749 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
750 mem_hole.node_id = i; // record the node No with hole
751 break; // only one hole
752 }
753 }
754
755 /* We need to double check if there is special set on base reg and limit reg
756 * are not continuous instead of hole, it will find out its hole_startk.
757 */
758 if (mem_hole.node_id == -1) {
759 resource_t limitk_pri = 0;
760 for (i=0; i<node_nums; i++) {
761 dram_base_mask_t d;
762 resource_t base_k, limit_k;
763 d = get_dram_base_mask(i);
764 if (!(d.base & 1)) continue;
765 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
766 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
767 if (limitk_pri != base_k) { // we find the hole
768 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
769 mem_hole.node_id = i;
770 break; //only one hole
771 }
772 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
773 limitk_pri = limit_k;
774 }
775 }
776 return mem_hole;
777}
778#endif
779
780#define ONE_MB_SHIFT 20
781#define ONE_GB_SHIFT 30
782
783static void setup_uma_memory(void)
784{
785#if CONFIG_GFXUMA
786 uint64_t topmem = bsp_topmem();
787 uint64_t topmem2 = bsp_topmem2();
788 uint32_t sysmem_mb, sysmem_gb;
789
790 /* refer to UMA_AUTO size computation in Family16h BKDG. */
791 /* Please reference MemNGetUmaSizeML() */
792 /*
793 * Total system memory UMASize
794 * >= 6G 1024M
795 * >= 4G 512M
796 * >= 2G 256M
797 * < 2G 128M
798 */
799
800 sysmem_mb = (topmem + (16ull << ONE_MB_SHIFT)) >> ONE_MB_SHIFT; // Ignore 16MB allocated for C6 when finding UMA size
801 sysmem_mb += topmem2 ? ((topmem2 >> ONE_MB_SHIFT) - 4096) : 0;
802 sysmem_gb = sysmem_mb >> (ONE_GB_SHIFT - ONE_MB_SHIFT);
803 printk(BIOS_SPEW, "%s: system memory size %luGB, topmem2 size %lluMB, topmem size %lluMB\n", __func__, (unsigned long)sysmem_gb, (topmem2 >> ONE_MB_SHIFT), (topmem >> ONE_MB_SHIFT));
804 if (sysmem_gb >= 6) {
805 uma_memory_size = 1024 << ONE_MB_SHIFT;
806 } else if (sysmem_gb >= 4) {
807 uma_memory_size = 512 << ONE_MB_SHIFT;
808 } else if (sysmem_gb >= 2) {
809 uma_memory_size = 256 << ONE_MB_SHIFT;
810 } else {
811 uma_memory_size = 128 << ONE_MB_SHIFT;
812 }
813 uma_memory_base = topmem - uma_memory_size; /* TOP_MEM1 */
814
815 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
816 __func__, uma_memory_size, uma_memory_base);
817
818 /* TODO: TOP_MEM2 */
819#endif
820}
821
822
823static void domain_set_resources(device_t dev)
824{
825#if CONFIG_PCI_64BIT_PREF_MEM
826 struct resource *io, *mem1, *mem2;
827 struct resource *res;
828#endif
829 unsigned long mmio_basek;
830 u32 pci_tolm;
831 u64 ramtop = 0;
832 int i, idx;
833 struct bus *link;
834#if CONFIG_HW_MEM_HOLE_SIZEK != 0
835 struct hw_mem_hole_info mem_hole;
836 u32 reset_memhole = 1;
837#endif
838
839#if CONFIG_PCI_64BIT_PREF_MEM
840
841 for (link = dev->link_list; link; link = link->next) {
842 /* Now reallocate the pci resources memory with the
843 * highest addresses I can manage.
844 */
845 mem1 = find_resource(dev, 1|(link->link_num<<2));
846 mem2 = find_resource(dev, 2|(link->link_num<<2));
847
848 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
849 mem1->base, mem1->limit, mem1->size, mem1->align);
850 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
851 mem2->base, mem2->limit, mem2->size, mem2->align);
852
853 /* See if both resources have roughly the same limits */
854 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
855 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
856 {
857 /* If so place the one with the most stringent alignment first */
858 if (mem2->align > mem1->align) {
859 struct resource *tmp;
860 tmp = mem1;
861 mem1 = mem2;
862 mem2 = tmp;
863 }
864 /* Now place the memory as high up as it will go */
865 mem2->base = resource_max(mem2);
866 mem1->limit = mem2->base - 1;
867 mem1->base = resource_max(mem1);
868 }
869 else {
870 /* Place the resources as high up as they will go */
871 mem2->base = resource_max(mem2);
872 mem1->base = resource_max(mem1);
873 }
874
875 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
876 mem1->base, mem1->limit, mem1->size, mem1->align);
877 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
878 mem2->base, mem2->limit, mem2->size, mem2->align);
879 }
880
881 for (res = &dev->resource_list; res; res = res->next)
882 {
883 res->flags |= IORESOURCE_ASSIGNED;
884 res->flags |= IORESOURCE_STORED;
885 report_resource_stored(dev, res, "");
886 }
887#endif
888
889 pci_tolm = 0xffffffffUL;
890 for (link = dev->link_list; link; link = link->next) {
891 pci_tolm = find_pci_tolm(link);
892 }
893
894 // FIXME handle interleaved nodes. If you fix this here, please fix
895 // amdk8, too.
896 mmio_basek = pci_tolm >> 10;
897 /* Round mmio_basek to something the processor can support */
898 mmio_basek &= ~((1 << 6) -1);
899
900 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
901 // MMIO hole. If you fix this here, please fix amdk8, too.
902 /* Round the mmio hole to 64M */
903 mmio_basek &= ~((64*1024) - 1);
904
905#if CONFIG_HW_MEM_HOLE_SIZEK != 0
906 /* if the hw mem hole is already set in raminit stage, here we will compare
907 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
908 * use hole_basek as mmio_basek and we don't need to reset hole.
909 * otherwise We reset the hole to the mmio_basek
910 */
911
912 mem_hole = get_hw_mem_hole_info();
913
914 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
915 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
916 mmio_basek = mem_hole.hole_startk;
917 reset_memhole = 0;
918 }
919#endif
920
921 idx = 0x10;
922 for (i = 0; i < node_nums; i++) {
923 dram_base_mask_t d;
924 resource_t basek, limitk, sizek; // 4 1T
925
926 d = get_dram_base_mask(i);
927
928 if (!(d.mask & 1)) continue;
929 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
930 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9 ;
931
932 sizek = limitk - basek;
933
934 /* see if we need a hole from 0xa0000 to 0xbffff */
935 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
936 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
937 idx += 0x10;
938 basek = (8*64)+(16*16);
939 sizek = limitk - ((8*64)+(16*16));
940
941 }
942
943 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
944
945 /* split the region to accommodate pci memory space */
946 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
947 if (basek <= mmio_basek) {
948 unsigned pre_sizek;
949 pre_sizek = mmio_basek - basek;
950 if (pre_sizek>0) {
951 ram_resource(dev, (idx | i), basek, pre_sizek);
952 idx += 0x10;
953 sizek -= pre_sizek;
954 if (!ramtop)
955 ramtop = mmio_basek * 1024;
956 }
957 basek = mmio_basek;
958 }
959 if ((basek + sizek) <= 4*1024*1024) {
960 sizek = 0;
961 }
962 else {
963 uint64_t topmem2 = bsp_topmem2();
964 basek = 4*1024*1024;
965 sizek = topmem2/1024 - basek;
966 }
967 }
968
969 ram_resource(dev, (idx | i), basek, sizek);
970 idx += 0x10;
971 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
972 i, mmio_basek, basek, limitk);
973 if (!ramtop)
974 ramtop = limitk * 1024;
975 }
976
977#if CONFIG_GFXUMA
978 set_top_of_ram(uma_memory_base);
979 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
980#else
981 set_top_of_ram(ramtop);
982#endif
983
984 for(link = dev->link_list; link; link = link->next) {
985 if (link->children) {
986 assign_resources(link);
987 }
988 }
989}
990
991static struct device_operations pci_domain_ops = {
992 .read_resources = domain_read_resources,
993 .set_resources = domain_set_resources,
994 .enable_resources = domain_enable_resources,
995 .init = NULL,
996 .scan_bus = pci_domain_scan_bus,
997 .ops_pci_bus = pci_bus_default_ops,
998};
999
1000static void sysconf_init(device_t dev) // first node
1001{
1002 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1003 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1004}
1005
1006static void add_more_links(device_t dev, unsigned total_links)
1007{
1008 struct bus *link, *last = NULL;
1009 int link_num;
1010
1011 for (link = dev->link_list; link; link = link->next)
1012 last = link;
1013
1014 if (last) {
1015 int links = total_links - last->link_num;
1016 link_num = last->link_num;
1017 if (links > 0) {
1018 link = malloc(links*sizeof(*link));
1019 if (!link)
1020 die("Couldn't allocate more links!\n");
1021 memset(link, 0, links*sizeof(*link));
1022 last->next = link;
1023 }
1024 }
1025 else {
1026 link_num = -1;
1027 link = malloc(total_links*sizeof(*link));
1028 memset(link, 0, total_links*sizeof(*link));
1029 dev->link_list = link;
1030 }
1031
1032 for (link_num = link_num + 1; link_num < total_links; link_num++) {
1033 link->link_num = link_num;
1034 link->dev = dev;
1035 link->next = link + 1;
1036 last = link;
1037 link = link->next;
1038 }
1039 last->next = NULL;
1040}
1041
1042static u32 cpu_bus_scan(device_t dev, u32 max)
1043{
1044 struct bus *cpu_bus;
1045 device_t dev_mc;
1046#if CONFIG_CBB
1047 device_t pci_domain;
1048#endif
1049 int i,j;
1050 int coreid_bits;
1051 int core_max = 0;
1052 unsigned ApicIdCoreIdSize;
1053 unsigned core_nums;
1054 int siblings = 0;
1055 unsigned int family;
1056 u32 modules = 0;
1057 VOID* modules_ptr = &modules;
1058 BUILD_OPT_CFG* options = NULL;
1059 int ioapic_count = 0;
1060
1061 // TODO Remove the printk's.
1062 printk(BIOS_SPEW, "MullinsPI Debug: Grabbing the AMD Topology Information.\n");
1063 AmdGetValue(AMD_GLOBAL_USER_OPTIONS, (VOID**)&options, sizeof(options));
1064 AmdGetValue(AMD_GLOBAL_NUM_MODULES, &modules_ptr, sizeof(modules));
Alexandru Gagniuc2e0cf142014-12-28 20:38:32 -06001065 modules = *(u32*)modules_ptr;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001066 ASSERT(modules > 0);
1067 ASSERT(options);
1068 ioapic_count = (int)options->CfgPlatNumIoApics;
1069 ASSERT(ioapic_count > 0);
1070 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of Modules (@0x%p) is %d\n", modules_ptr, modules);
1071 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of IOAPICs (@0x%p) is %d\n", options, (int)options->CfgPlatNumIoApics);
1072
1073#if CONFIG_CBB
1074 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
1075 if (dev_mc && dev_mc->bus) {
1076 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
1077 pci_domain = dev_mc->bus->dev;
1078 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
1079 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
1080 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
1081 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
1082 } else {
1083 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
1084 }
1085 printk(BIOS_DEBUG, "\n");
1086 }
1087 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1088 if (!dev_mc) {
1089 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
1090 if (dev_mc && dev_mc->bus) {
1091 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
1092 pci_domain = dev_mc->bus->dev;
1093 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
1094 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
1095 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
1096 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
1097 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
1098 while (dev_mc) {
1099 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
1100 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
1101 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
1102 dev_mc = dev_mc->sibling;
1103 }
1104 }
1105 }
1106 }
1107 }
1108#endif
1109 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1110 if (!dev_mc) {
1111 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
1112 die("");
1113 }
1114 sysconf_init(dev_mc);
1115#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1116 if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
1117 if (pci_domain->link_list && !pci_domain->link_list->next) {
1118 struct bus *new_link = new_link(pci_domain);
1119 pci_domain->link_list->next = new_link;
1120 new_link->link_num = 1;
1121 new_link->dev = pci_domain;
1122 new_link->children = 0;
1123 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
1124 }
1125 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
1126 }
1127#endif
1128
1129 /* Get Max Number of cores(MNC) */
1130 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
1131 core_max = 1 << (coreid_bits & 0x000F); //mnc
1132
1133 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1134 if (ApicIdCoreIdSize) {
1135 core_nums = (1 << ApicIdCoreIdSize) - 1;
1136 } else {
1137 core_nums = 3; //quad core
1138 }
1139
1140 /* Find which cpus are present */
1141 cpu_bus = dev->link_list;
1142 for (i = 0; i < node_nums; i++) {
1143 device_t cdb_dev;
1144 unsigned busn, devn;
1145 struct bus *pbus;
1146
1147 busn = CONFIG_CBB;
1148 devn = CONFIG_CDB + i;
1149 pbus = dev_mc->bus;
1150#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1151 if (i >= 32) {
1152 busn--;
1153 devn -= 32;
1154 pbus = pci_domain->link_list->next;
1155 }
1156#endif
1157
1158 /* Find the cpu's pci device */
1159 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1160 if (!cdb_dev) {
1161 /* If I am probing things in a weird order
1162 * ensure all of the cpu's pci devices are found.
1163 */
1164 int fn;
1165 for(fn = 0; fn <= 5; fn++) { //FBDIMM?
1166 cdb_dev = pci_probe_dev(NULL, pbus,
1167 PCI_DEVFN(devn, fn));
1168 }
1169 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1170 } else {
1171 /* Ok, We need to set the links for that device.
1172 * otherwise the device under it will not be scanned
1173 */
1174 int linknum;
1175#if CONFIG_HT3_SUPPORT
1176 linknum = 8;
1177#else
1178 linknum = 4;
1179#endif
1180 add_more_links(cdb_dev, linknum);
1181 }
1182
1183 family = cpuid_eax(1);
1184 family = (family >> 20) & 0xFF;
1185 if (family == 1) { //f10
1186 u32 dword;
1187 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1188 dword = pci_read_config32(cdb_dev, 0xe8);
1189 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1190 } else if (family == 7) {//f16
1191 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1192 if (cdb_dev && cdb_dev->enabled) {
1193 siblings = pci_read_config32(cdb_dev, 0x84);
1194 siblings &= 0xFF;
1195 }
1196 } else {
1197 siblings = 0; //default one core
1198 }
1199 int enable_node = cdb_dev && cdb_dev->enabled;
1200 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1201 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1202
1203 for (j = 0; j <= siblings; j++ ) {
1204 u32 lapicid_start = 0;
1205
1206 /*
1207 * APIC ID calucation is tightly coupled with AGESA v5 code.
1208 * This calculation MUST match the assignment calculation done
1209 * in LocalApicInitializationAtEarly() function.
1210 * And reference GetLocalApicIdForCore()
1211 *
1212 * Apply apic enumeration rules
1213 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1214 * put the local-APICs at m..z
1215 *
1216 * This is needed because many IO-APIC devices only have 4 bits
1217 * for their APIC id and therefore must reside at 0..15
1218 */
1219 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1220 lapicid_start = (ioapic_count - 1) / core_max;
1221 lapicid_start = (lapicid_start + 1) * core_max;
1222 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1223 }
1224 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
1225 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
1226 i, j, apic_id);
1227
1228 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1229 if (cpu)
1230 amd_cpu_topology(cpu, i, j);
1231 } //j
1232 }
1233 return max;
1234}
1235
1236static void cpu_bus_init(device_t dev)
1237{
1238 initialize_cpus(dev->link_list);
1239}
1240
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001241static void cpu_bus_read_resources(device_t dev)
1242{
1243#if CONFIG_MMCONF_SUPPORT
1244 struct resource *resource = new_resource(dev, 0xc0010058);
1245 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
1246 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096*256;
1247 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
1248 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
1249#endif
1250}
1251
1252static void cpu_bus_set_resources(device_t dev)
1253{
1254 struct resource *resource = find_resource(dev, 0xc0010058);
1255 if (resource) {
1256 report_resource_stored(dev, resource, " <mmconfig>");
1257 }
1258 pci_dev_set_resources(dev);
1259}
1260
1261static struct device_operations cpu_bus_ops = {
1262 .read_resources = cpu_bus_read_resources,
1263 .set_resources = cpu_bus_set_resources,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001264 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001265 .init = cpu_bus_init,
1266 .scan_bus = cpu_bus_scan,
1267};
1268
1269static void root_complex_enable_dev(struct device *dev)
1270{
1271 static int done = 0;
1272
1273 /* Do not delay UMA setup, as a device on the PCI bus may evaluate
1274 the global uma_memory variables already in its enable function. */
1275 if (!done) {
1276 setup_bsp_ramtop();
1277 setup_uma_memory();
1278 done = 1;
1279 }
1280
1281 /* Set the operations if it is a special bus type */
1282 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1283 dev->ops = &pci_domain_ops;
1284 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1285 dev->ops = &cpu_bus_ops;
1286 }
1287}
1288
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001289struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001290 CHIP_NAME("AMD FAM16 Root Complex")
1291 .enable_dev = root_complex_enable_dev,
1292};
1293
1294/*********************************************************************
1295 * Change the vendor / device IDs to match the generic VBIOS header. *
1296 *********************************************************************/
1297u32 map_oprom_vendev(u32 vendev)
1298{
1299 u32 new_vendev;
1300 new_vendev =
1301 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1302
1303 if (vendev != new_vendev)
1304 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1305
1306 return new_vendev;
1307}