2 * This file is part of the coreboot project.
4 * Copyright (C) 2012 Advanced Micro Devices, Inc.
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.
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.
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
20 #include <console/console.h>
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <device/pci_ids.h>
26 #include <device/hypertransport.h>
32 #include <cpu/x86/lapic.h>
38 #include <cpu/amd/amdfam15.h>
39 #include <cpuRegisters.h>
40 #include "agesawrapper.h"
41 #include "root_complex/chip.h"
42 #include "northbridge.h"
45 #define MAX_NODE_NUMS (MAX_NODES * MAX_DIES)
47 #if (defined CONFIG_EXT_CONF_SUPPORT) && CONFIG_EXT_CONF_SUPPORT == 1
48 #error CONFIG_EXT_CONF_SUPPORT == 1 not support anymore!
51 typedef struct dram_base_mask {
52 u32 base; //[47:27] at [28:8]
53 u32 mask; //[47:27] at [28:8] and enable at bit 0
56 static unsigned node_nums;
57 static unsigned sblink;
58 static device_t __f0_dev[MAX_NODE_NUMS];
59 static device_t __f1_dev[MAX_NODE_NUMS];
60 static device_t __f2_dev[MAX_NODE_NUMS];
61 static device_t __f4_dev[MAX_NODE_NUMS];
62 static unsigned fx_devs = 0;
65 static dram_base_mask_t get_dram_base_mask(u32 nodeid)
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]
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]
83 static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg,
84 u32 io_min, u32 io_max)
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 ?
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;
100 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
101 tempreg |= PCI_IO_BASE_NO_ISA;
104 for (i=0; i<node_nums; i++)
105 pci_write_config32(__f1_dev[i], reg, tempreg);
108 static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
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);
121 static device_t get_node_pci(u32 nodeid, u32 fn)
123 #if MAX_NODE_NUMS == 64
125 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
127 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
130 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
134 static void get_fx_devs(void)
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)
145 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
146 die("Cannot find 0:0x18.[0|1]\n");
148 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
151 static u32 f1_read_config32(unsigned reg)
155 return pci_read_config32(__f1_dev[0], reg);
158 static void f1_write_config32(unsigned reg, u32 value)
163 for(i = 0; i < fx_devs; i++) {
166 if (dev && dev->enabled) {
167 pci_write_config32(dev, reg, value);
172 static u32 amdfam15_nodeid(device_t dev)
174 #if MAX_NODE_NUMS == 64
176 busn = dev->bus->secondary;
177 if (busn != CONFIG_CBB) {
178 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
180 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
184 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
188 static void set_vga_enable_reg(u32 nodeid, u32 linkn)
192 val = 1 | (nodeid<<4) | (linkn<<12);
194 * (1)mmio 0xa0000:0xbffff
195 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
197 f1_write_config32(0xf4, val);
203 * @retval 2 resoure not exist, usable
204 * @retval 0 resource exist, not usable
205 * @retval 1 resource exist, resource has been allocated before
207 static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
210 struct resource *res;
211 unsigned nodeid, link = 0;
214 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
216 dev = __f0_dev[nodeid];
219 for (link = 0; !res && (link < 8); link++) {
220 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
226 if ((goal_link == (link - 1)) &&
227 (goal_nodeid == (nodeid - 1)) &&
235 static struct resource *amdfam15_find_iopair(device_t dev, unsigned nodeid, unsigned link)
237 struct resource *resource;
241 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
243 result = reg_useable(reg, dev, nodeid, link);
245 /* I have been allocated this one */
248 else if (result > 1) {
249 /* I have a free register pair */
254 reg = free_reg; // if no free, the free_reg still be 0
257 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
262 static struct resource *amdfam15_find_mempair(device_t dev, u32 nodeid, u32 link)
264 struct resource *resource;
268 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
270 result = reg_useable(reg, dev, nodeid, link);
272 /* I have been allocated this one */
275 else if (result > 1) {
276 /* I have a free register pair */
284 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
289 static void amdfam15_link_read_bases(device_t dev, u32 nodeid, u32 link)
291 struct resource *resource;
293 /* Initialize the io space constraints on the current bus */
294 resource = amdfam15_find_iopair(dev, nodeid, link);
297 align = log2(HT_IO_HOST_ALIGN);
300 resource->align = align;
301 resource->gran = align;
302 resource->limit = 0xffffUL;
303 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
306 /* Initialize the prefetchable memory constraints on the current bus */
307 resource = amdfam15_find_mempair(dev, nodeid, link);
311 resource->align = log2(HT_MEM_HOST_ALIGN);
312 resource->gran = log2(HT_MEM_HOST_ALIGN);
313 resource->limit = 0xffffffffffULL;
314 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
315 resource->flags |= IORESOURCE_BRIDGE;
319 /* Initialize the memory constraints on the current bus */
320 resource = amdfam15_find_mempair(dev, nodeid, link);
324 resource->align = log2(HT_MEM_HOST_ALIGN);
325 resource->gran = log2(HT_MEM_HOST_ALIGN);
326 resource->limit = 0xffffffffffULL;
327 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
333 static void read_resources(device_t dev)
338 nodeid = amdfam15_nodeid(dev);
339 for (link = dev->link_list; link; link = link->next) {
340 if (link->children) {
341 amdfam15_link_read_bases(dev, nodeid, link->link_num);
347 static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
349 resource_t rbase, rend;
350 unsigned reg, link_num;
354 /* Make certain the resource has actually been set */
355 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
359 /* If I have already stored this resource don't worry about it */
360 if (resource->flags & IORESOURCE_STORED) {
364 /* Only handle PCI memory and IO resources */
365 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
368 /* Ensure I am actually looking at a resource of function 1 */
369 if ((resource->index & 0xffff) < 0x1000) {
372 /* Get the base address */
373 rbase = resource->base;
375 /* Get the limit (rounded up) */
376 rend = resource_end(resource);
378 /* Get the register and link */
379 reg = resource->index & 0xfff; // 4k
380 link_num = IOINDEX_LINK(resource->index);
382 if (resource->flags & IORESOURCE_IO) {
383 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
385 else if (resource->flags & IORESOURCE_MEM) {
386 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums) ;// [39:8]
388 resource->flags |= IORESOURCE_STORED;
389 sprintf(buf, " <node %x link %x>",
391 report_resource_stored(dev, resource, buf);
395 * I tried to reuse the resource allocation code in set_resource()
396 * but it is too difficult to deal with the resource allocation magic.
399 static void create_vga_resource(device_t dev, unsigned nodeid)
404 /* find out which link the VGA card is connected,
405 * we only deal with the 'first' vga card */
406 for (link = dev->link_list; link; link = link->next) {
407 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
408 #if CONFIG_MULTIPLE_VGA_ADAPTERS == 1
409 extern device_t vga_pri; // the primary vga device, defined in device.c
410 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
411 link->secondary,link->subordinate);
412 /* We need to make sure the vga_pri is under the link */
413 if((vga_pri->bus->secondary >= link->secondary ) &&
414 (vga_pri->bus->secondary <= link->subordinate )
421 /* no VGA card installed */
425 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
426 set_vga_enable_reg(nodeid, sblink);
430 static void set_resources(device_t dev)
434 struct resource *res;
436 /* Find the nodeid */
437 nodeid = amdfam15_nodeid(dev);
439 create_vga_resource(dev, nodeid); //TODO: do we need this?
441 /* Set each resource we have found */
442 for (res = dev->resource_list; res; res = res->next) {
443 set_resource(dev, res, nodeid);
446 for (bus = dev->link_list; bus; bus = bus->next) {
448 assign_resources(bus);
453 static void northbridge_init(struct device *dev)
457 static unsigned scan_chains(device_t dev, unsigned max)
461 device_t io_hub = NULL;
462 u32 next_unitid = 0x18;
463 nodeid = amdfam15_nodeid(dev);
465 for (link = dev->link_list; link; link = link->next) {
466 //if (link->link_num == sblink) { /* devicetree put IO Hub on link_lsit[sblink] */
467 if (link->link_num == 0) { /* devicetree put IO Hub on link_lsit[0] */
468 io_hub = link->children;
469 if (!io_hub || !io_hub->enabled) {
470 die("I can't find the IO Hub, or IO Hub not enabled, please check the device tree.\n");
472 /* Now that nothing is overlapping it is safe to scan the children. */
473 max = pci_scan_bus(link, 0x00, ((next_unitid - 1) << 3) | 7, 0);
480 static struct device_operations northbridge_operations = {
481 .read_resources = read_resources,
482 .set_resources = set_resources,
483 .enable_resources = pci_dev_enable_resources,
484 .init = northbridge_init,
485 .scan_bus = scan_chains,
490 static const struct pci_driver family15_northbridge __pci_driver = {
491 .ops = &northbridge_operations,
492 .vendor = PCI_VENDOR_ID_AMD,
493 .device = PCI_DEVICE_ID_AMD_15H_MODEL_000F_NB_HT,
496 static const struct pci_driver family10_northbridge __pci_driver = {
497 .ops = &northbridge_operations,
498 .vendor = PCI_VENDOR_ID_AMD,
499 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
502 struct chip_operations northbridge_amd_agesa_family15_ops = {
503 CHIP_NAME("AMD FAM15 Northbridge")
507 static void domain_read_resources(device_t dev)
512 /* Find the already assigned resource pairs */
514 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
516 base = f1_read_config32(reg);
517 limit = f1_read_config32(reg + 0x04);
518 /* Is this register allocated? */
519 if ((base & 3) != 0) {
520 unsigned nodeid, reg_link;
522 if (reg<0xc0) { // mmio
523 nodeid = (limit & 0xf) + (base&0x30);
525 nodeid = (limit & 0xf) + ((base>>4)&0x30);
527 reg_link = (limit >> 4) & 7;
528 reg_dev = __f0_dev[nodeid];
530 /* Reserve the resource */
531 struct resource *res;
532 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
539 /* FIXME: do we need to check extend conf space?
540 I don't believe that much preset value */
542 #if CONFIG_PCI_64BIT_PREF_MEM == 0
543 pci_domain_read_resources(dev);
548 struct resource *resource;
549 for (link=dev->link_list; link; link = link->next) {
550 /* Initialize the system wide io space constraints */
551 resource = new_resource(dev, 0|(link->link_num<<2));
552 resource->base = 0x400;
553 resource->limit = 0xffffUL;
554 resource->flags = IORESOURCE_IO;
556 /* Initialize the system wide prefetchable memory resources constraints */
557 resource = new_resource(dev, 1|(link->link_num<<2));
558 resource->limit = 0xfcffffffffULL;
559 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
561 /* Initialize the system wide memory resources constraints */
562 resource = new_resource(dev, 2|(link->link_num<<2));
563 resource->limit = 0xfcffffffffULL;
564 resource->flags = IORESOURCE_MEM;
569 static void domain_enable_resources(device_t dev)
572 /* Must be called after PCI enumeration and resource allocation */
573 printk(BIOS_DEBUG, "\nFam15 - domain_enable_resources: AmdInitMid.\n");
574 val = agesawrapper_amdinitmid();
576 printk(BIOS_DEBUG, "agesawrapper_amdinitmid failed: %x \n", val);
578 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
582 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
583 struct hw_mem_hole_info {
584 unsigned hole_startk;
587 static struct hw_mem_hole_info get_hw_mem_hole_info(void)
589 struct hw_mem_hole_info mem_hole;
591 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
592 mem_hole.node_id = -1;
593 for (i = 0; i < node_nums; i++) {
596 d = get_dram_base_mask(i);
597 if (!(d.mask & 1)) continue; // no memory on this node
598 hole = pci_read_config32(__f1_dev[i], 0xf0);
599 if (hole & 1) { // we find the hole
600 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
601 mem_hole.node_id = i; // record the node No with hole
602 break; // only one hole
605 //We need to double check if there is speical set on base reg and limit reg are not continous instead of hole, it will find out it's hole_startk
606 if (mem_hole.node_id == -1) {
607 resource_t limitk_pri = 0;
608 for (i=0; i<node_nums; i++) {
610 resource_t base_k, limit_k;
611 d = get_dram_base_mask(i);
612 if (!(d.base & 1)) continue;
613 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
614 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
615 if (limitk_pri != base_k) { // we find the hole
616 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
617 mem_hole.node_id = i;
618 break; //only one hole
620 limit_k = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9;
621 limitk_pri = limit_k;
627 #if CONFIG_WRITE_HIGH_TABLES==1
628 #define HIGH_TABLES_SIZE 64 // maximum size of high tables in KB
629 extern uint64_t high_tables_base, high_tables_size;
631 #if CONFIG_GFXUMA == 1
632 extern uint64_t uma_memory_base, uma_memory_size;
633 static void add_uma_resource(struct device *dev, int index)
635 struct resource *resource;
637 printk(BIOS_DEBUG, "Adding UMA memory area\n");
638 resource = new_resource(dev, index);
639 resource->base = (resource_t) uma_memory_base;
640 resource->size = (resource_t) uma_memory_size;
641 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
642 IORESOURCE_FIXED | IORESOURCE_STORED |
647 static void domain_set_resources(device_t dev)
649 #if CONFIG_PCI_64BIT_PREF_MEM == 1
650 struct resource *io, *mem1, *mem2;
651 struct resource *res;
653 unsigned long mmio_basek;
657 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
658 struct hw_mem_hole_info mem_hole;
659 u32 reset_memhole = 1;
662 #if CONFIG_PCI_64BIT_PREF_MEM == 1
664 for (link = dev->link_list; link; link = link->next) {
665 /* Now reallocate the pci resources memory with the
666 * highest addresses I can manage.
668 mem1 = find_resource(dev, 1|(link->link_num<<2));
669 mem2 = find_resource(dev, 2|(link->link_num<<2));
671 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
672 mem1->base, mem1->limit, mem1->size, mem1->align);
673 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
674 mem2->base, mem2->limit, mem2->size, mem2->align);
676 /* See if both resources have roughly the same limits */
677 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
678 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
680 /* If so place the one with the most stringent alignment first */
681 if (mem2->align > mem1->align) {
682 struct resource *tmp;
687 /* Now place the memory as high up as it will go */
688 mem2->base = resource_max(mem2);
689 mem1->limit = mem2->base - 1;
690 mem1->base = resource_max(mem1);
693 /* Place the resources as high up as they will go */
694 mem2->base = resource_max(mem2);
695 mem1->base = resource_max(mem1);
698 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
699 mem1->base, mem1->limit, mem1->size, mem1->align);
700 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
701 mem2->base, mem2->limit, mem2->size, mem2->align);
704 for (res = &dev->resource_list; res; res = res->next)
706 res->flags |= IORESOURCE_ASSIGNED;
707 res->flags |= IORESOURCE_STORED;
708 report_resource_stored(dev, res, "");
712 pci_tolm = 0xffffffffUL;
713 for (link = dev->link_list; link; link = link->next) {
714 pci_tolm = find_pci_tolm(link);
717 // FIXME handle interleaved nodes. If you fix this here, please fix
719 mmio_basek = pci_tolm >> 10;
720 /* Round mmio_basek to something the processor can support */
721 mmio_basek &= ~((1 << 6) -1);
723 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
724 // MMIO hole. If you fix this here, please fix amdk8, too.
725 /* Round the mmio hole to 64M */
726 mmio_basek &= ~((64*1024) - 1);
728 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
729 /* if the hw mem hole is already set in raminit stage, here we will compare
730 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
731 * use hole_basek as mmio_basek and we don't need to reset hole.
732 * otherwise We reset the hole to the mmio_basek
735 mem_hole = get_hw_mem_hole_info();
737 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
738 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
739 mmio_basek = mem_hole.hole_startk;
745 for (i = 0; i < node_nums; i++) {
747 resource_t basek, limitk, sizek; // 4 1T
749 d = get_dram_base_mask(i);
751 if (!(d.mask & 1)) continue;
752 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
753 limitk = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9 ;
755 sizek = limitk - basek;
758 /* see if we need a hole from 0xa0000 to 0xbffff */
759 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
760 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
762 basek = (8*64)+(16*16);
763 sizek = limitk - ((8*64)+(16*16));
767 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
769 /* split the region to accomodate pci memory space */
770 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
771 if (basek <= mmio_basek) {
773 pre_sizek = mmio_basek - basek;
775 ram_resource(dev, (idx | i), basek, pre_sizek);
778 #if CONFIG_WRITE_HIGH_TABLES==1
779 if (high_tables_base==0) {
780 /* Leave some space for ACPI, PIRQ and MP tables */
781 #if CONFIG_GFXUMA == 1
782 high_tables_base = uma_memory_base - (HIGH_TABLES_SIZE * 1024);
784 high_tables_base = (mmio_basek - HIGH_TABLES_SIZE) * 1024;
786 high_tables_size = HIGH_TABLES_SIZE * 1024;
787 printk(BIOS_DEBUG, " split: %dK table at =%08llx\n",
788 HIGH_TABLES_SIZE, high_tables_base);
794 if ((basek + sizek) <= 4*1024*1024) {
799 sizek -= (4*1024*1024 - mmio_basek);
803 #if CONFIG_GFXUMA == 1
804 /* Deduct uma memory before reporting because
805 * this is what the mtrr code expects */
806 sizek -= uma_memory_size / 1024;
808 ram_resource(dev, (idx | i), basek, sizek);
810 #if CONFIG_WRITE_HIGH_TABLES==1
811 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
812 i, mmio_basek, basek, limitk);
813 if (high_tables_base==0) {
814 /* Leave some space for ACPI, PIRQ and MP tables */
815 #if CONFIG_GFXUMA == 1
816 high_tables_base = uma_memory_base - (HIGH_TABLES_SIZE * 1024);
818 high_tables_base = (limitk - HIGH_TABLES_SIZE) * 1024;
820 high_tables_size = HIGH_TABLES_SIZE * 1024;
825 #if CONFIG_GFXUMA == 1
826 add_uma_resource(dev, 7);
829 for(link = dev->link_list; link; link = link->next) {
830 if (link->children) {
831 assign_resources(link);
837 static struct device_operations pci_domain_ops = {
838 .read_resources = domain_read_resources,
839 .set_resources = domain_set_resources,
840 .enable_resources = domain_enable_resources,
842 .scan_bus = pci_domain_scan_bus,
844 #if CONFIG_MMCONF_SUPPORT_DEFAULT
845 .ops_pci_bus = &pci_ops_mmconf,
847 .ops_pci_bus = &pci_cf8_conf1,
852 static void sysconf_init(device_t dev) // first node
854 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
855 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
858 static void add_more_links(device_t dev, unsigned total_links)
860 struct bus *link, *last = NULL;
863 for (link = dev->link_list; link; link = link->next)
867 int links = total_links - last->link_num;
868 link_num = last->link_num;
870 link = malloc(links*sizeof(*link));
872 die("Couldn't allocate more links!\n");
873 memset(link, 0, links*sizeof(*link));
879 link = malloc(total_links*sizeof(*link));
880 memset(link, 0, total_links*sizeof(*link));
881 dev->link_list = link;
884 for (link_num = link_num + 1; link_num < total_links; link_num++) {
885 link->link_num = link_num;
887 link->next = link + 1;
894 /* dummy read_resources */
895 static void lapic_read_resources(device_t dev)
899 static struct device_operations lapic_ops = {
900 .read_resources = lapic_read_resources,
901 .set_resources = pci_dev_set_resources,
902 .enable_resources = pci_dev_enable_resources,
909 static u32 cpu_bus_scan(device_t dev, u32 max)
919 unsigned ApicIdCoreIdSize;
925 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
926 if (dev_mc && dev_mc->bus) {
927 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
928 pci_domain = dev_mc->bus->dev;
929 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
930 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
931 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
932 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
934 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
936 printk(BIOS_DEBUG, "\n");
938 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
940 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
941 if (dev_mc && dev_mc->bus) {
942 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
943 pci_domain = dev_mc->bus->dev;
944 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
945 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
946 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
947 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
948 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
950 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
951 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
952 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
953 dev_mc = dev_mc->sibling;
960 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
962 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
965 sysconf_init(dev_mc);
966 #if CONFIG_CBB && (MAX_NODE_NUMS > 32)
967 if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
968 if (pci_domain->link_list && !pci_domain->link_list->next) {
969 struct bus *new_link = new_link(pci_domain);
970 pci_domain->link_list->next = new_link;
971 new_link->link_num = 1;
972 new_link->dev = pci_domain;
973 new_link->children = 0;
974 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
976 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
980 /* Get Max Number of cores(MNC) */
981 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
982 core_max = 1 << (coreid_bits & 0x000F); //mnc
984 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
985 if (ApicIdCoreIdSize) {
986 core_nums = (1 << ApicIdCoreIdSize) - 1;
988 core_nums = 3; //quad core
991 /* Find which cpus are present */
992 cpu_bus = dev->link_list;
993 for (i = 0; i < node_nums; i++) {
994 device_t cdb_dev, cpu;
995 struct device_path cpu_path;
1000 devn = CONFIG_CDB + i;
1002 #if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1006 pbus = pci_domain->link_list->next;
1010 /* Find the cpu's pci device */
1011 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1013 /* If I am probing things in a weird order
1014 * ensure all of the cpu's pci devices are found.
1017 for(fn = 0; fn <= 5; fn++) { //FBDIMM?
1018 cdb_dev = pci_probe_dev(NULL, pbus,
1019 PCI_DEVFN(devn, fn));
1021 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1023 /* Ok, We need to set the links for that device.
1024 * otherwise the device under it will not be scanned
1027 #if CONFIG_HT3_SUPPORT==1
1032 add_more_links(cdb_dev, linknum);
1035 family = cpuid_eax(1);
1036 family = (family >> 20) & 0xFF;
1037 if (family == 1) { //f10
1039 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1040 dword = pci_read_config32(cdb_dev, 0xe8);
1041 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1042 } else if (family == 6) {//f15
1043 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1044 if (cdb_dev && cdb_dev->enabled) {
1045 siblings = pci_read_config32(cdb_dev, 0x84);
1049 siblings = 0; //default one core
1051 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1052 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1054 for (j = 0; j <= siblings; j++ ) {
1055 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1056 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1057 u32 lapicid_start = 0;
1059 /* Build the cpu device path */
1060 cpu_path.type = DEVICE_PATH_APIC;
1062 * APIC ID calucation is tightly coupled with AGESA v5 code.
1063 * This calculation MUST match the assignment calculation done
1064 * in LocalApicInitializationAtEarly() function.
1065 * And reference GetLocalApicIdForCore()
1067 * Apply apic enumeration rules
1068 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1069 * put the local-APICs at m..z
1071 * This is needed because many IO-APIC devices only have 4 bits
1072 * for their APIC id and therefore must reside at 0..15
1074 #ifndef CFG_PLAT_NUM_IO_APICS /* defined in mainboard buildOpts.c */
1075 #define CFG_PLAT_NUM_IO_APICS 3
1077 if ((node_nums * core_max) + CFG_PLAT_NUM_IO_APICS >= 0x10) {
1078 lapicid_start = (CFG_PLAT_NUM_IO_APICS - 1) / core_max;
1079 lapicid_start = (lapicid_start + 1) * core_max;
1080 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1082 cpu_path.apic.apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
1083 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
1084 i, j, cpu_path.apic.apic_id);
1086 /* See if I can find the cpu */
1087 cpu = find_dev_path(cpu_bus, &cpu_path);
1088 /* Enable the cpu if I have the processor */
1089 if (cdb_dev && cdb_dev->enabled) {
1091 cpu = alloc_dev(cpu_bus, &cpu_path);
1097 /* Disable the cpu if I don't have the processor */
1098 if (cpu && (!cdb_dev || !cdb_dev->enabled)) {
1101 /* Report what I have done */
1103 cpu->path.apic.node_id = i;
1104 cpu->path.apic.core_id = j;
1105 if (cpu->path.type == DEVICE_PATH_APIC) {
1106 cpu->ops = &lapic_ops;
1108 printk(BIOS_DEBUG, "CPU: %s %s\n",
1109 dev_path(cpu), cpu->enabled?"enabled":"disabled");
1116 static void cpu_bus_init(device_t dev)
1118 initialize_cpus(dev->link_list);
1121 static void cpu_bus_noop(device_t dev)
1125 static void cpu_bus_read_resources(device_t dev)
1127 #if CONFIG_MMCONF_SUPPORT
1128 struct resource *resource = new_resource(dev, 0xc0010058);
1129 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
1130 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096*256;
1131 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
1132 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
1136 static void cpu_bus_set_resources(device_t dev)
1138 struct resource *resource = find_resource(dev, 0xc0010058);
1140 report_resource_stored(dev, resource, " <mmconfig>");
1142 pci_dev_set_resources(dev);
1145 static struct device_operations cpu_bus_ops = {
1146 .read_resources = cpu_bus_read_resources,
1147 .set_resources = cpu_bus_set_resources,
1148 .enable_resources = cpu_bus_noop,
1149 .init = cpu_bus_init,
1150 .scan_bus = cpu_bus_scan,
1154 static void root_complex_enable_dev(struct device *dev)
1156 /* Set the operations if it is a special bus type */
1157 if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
1158 dev->ops = &pci_domain_ops;
1159 } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
1160 dev->ops = &cpu_bus_ops;
1165 struct chip_operations northbridge_amd_agesa_family15_root_complex_ops = {
1166 CHIP_NAME("AMD FAM15 Root Complex")
1167 .enable_dev = root_complex_enable_dev,