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>
33 #include <cpu/x86/lapic.h>
39 #include <cpu/amd/amdfam15.h>
40 #include <cpuRegisters.h>
41 #include "agesawrapper.h"
42 #include "root_complex/chip.h"
43 #include "northbridge.h"
46 #define MAX_NODE_NUMS (MAX_NODES * MAX_DIES)
48 #if (defined CONFIG_EXT_CONF_SUPPORT) && CONFIG_EXT_CONF_SUPPORT == 1
49 #error CONFIG_EXT_CONF_SUPPORT == 1 not support anymore!
52 typedef 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
57 static unsigned node_nums;
58 static unsigned sblink;
59 static device_t __f0_dev[MAX_NODE_NUMS];
60 static device_t __f1_dev[MAX_NODE_NUMS];
61 static device_t __f2_dev[MAX_NODE_NUMS];
62 static device_t __f4_dev[MAX_NODE_NUMS];
63 static unsigned fx_devs = 0;
66 static dram_base_mask_t get_dram_base_mask(u32 nodeid)
72 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
73 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
74 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
76 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
77 d.mask |= (temp & 1); // enable bit
78 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
79 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
84 static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg,
85 u32 io_min, u32 io_max)
89 /* io range allocation */
90 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
91 for (i=0; i<node_nums; i++)
92 pci_write_config32(__f1_dev[i], reg+4, tempreg);
93 tempreg = 3 /*| ( 3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
95 // FIXME: can we use VGA reg instead?
96 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
97 printk(BIOS_SPEW, "%s, enabling legacy VGA IO forwarding for %s link %s\n",
98 __func__, dev_path(dev), link);
99 tempreg |= PCI_IO_BASE_VGA_EN;
101 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
102 tempreg |= PCI_IO_BASE_NO_ISA;
105 for (i=0; i<node_nums; i++)
106 pci_write_config32(__f1_dev[i], reg, tempreg);
109 static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
113 /* io range allocation */
114 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
115 for (i=0; i<nodes; i++)
116 pci_write_config32(__f1_dev[i], reg+4, tempreg);
117 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
118 for (i=0; i<node_nums; i++)
119 pci_write_config32(__f1_dev[i], reg, tempreg);
122 static device_t get_node_pci(u32 nodeid, u32 fn)
124 #if MAX_NODE_NUMS == 64
126 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
128 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
131 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
135 static void get_fx_devs(void)
138 for (i = 0; i < MAX_NODE_NUMS; i++) {
139 __f0_dev[i] = get_node_pci(i, 0);
140 __f1_dev[i] = get_node_pci(i, 1);
141 __f2_dev[i] = get_node_pci(i, 2);
142 __f4_dev[i] = get_node_pci(i, 4);
143 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
146 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
147 die("Cannot find 0:0x18.[0|1]\n");
149 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
152 static u32 f1_read_config32(unsigned reg)
156 return pci_read_config32(__f1_dev[0], reg);
159 static void f1_write_config32(unsigned reg, u32 value)
164 for(i = 0; i < fx_devs; i++) {
167 if (dev && dev->enabled) {
168 pci_write_config32(dev, reg, value);
173 static u32 amdfam15_nodeid(device_t dev)
175 #if MAX_NODE_NUMS == 64
177 busn = dev->bus->secondary;
178 if (busn != CONFIG_CBB) {
179 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
181 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
185 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
189 static void set_vga_enable_reg(u32 nodeid, u32 linkn)
193 val = 1 | (nodeid<<4) | (linkn<<12);
195 * (1)mmio 0xa0000:0xbffff
196 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
198 f1_write_config32(0xf4, val);
204 * @retval 2 resoure not exist, usable
205 * @retval 0 resource exist, not usable
206 * @retval 1 resource exist, resource has been allocated before
208 static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
211 struct resource *res;
212 unsigned nodeid, link = 0;
215 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
217 dev = __f0_dev[nodeid];
220 for (link = 0; !res && (link < 8); link++) {
221 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
227 if ((goal_link == (link - 1)) &&
228 (goal_nodeid == (nodeid - 1)) &&
236 static struct resource *amdfam15_find_iopair(device_t dev, unsigned nodeid, unsigned link)
238 struct resource *resource;
242 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
244 result = reg_useable(reg, dev, nodeid, link);
246 /* I have been allocated this one */
249 else if (result > 1) {
250 /* I have a free register pair */
255 reg = free_reg; // if no free, the free_reg still be 0
258 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
263 static struct resource *amdfam15_find_mempair(device_t dev, u32 nodeid, u32 link)
265 struct resource *resource;
269 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
271 result = reg_useable(reg, dev, nodeid, link);
273 /* I have been allocated this one */
276 else if (result > 1) {
277 /* I have a free register pair */
285 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
290 static void amdfam15_link_read_bases(device_t dev, u32 nodeid, u32 link)
292 struct resource *resource;
294 /* Initialize the io space constraints on the current bus */
295 resource = amdfam15_find_iopair(dev, nodeid, link);
298 align = log2(HT_IO_HOST_ALIGN);
301 resource->align = align;
302 resource->gran = align;
303 resource->limit = 0xffffUL;
304 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
307 /* Initialize the prefetchable memory constraints on the current bus */
308 resource = amdfam15_find_mempair(dev, nodeid, link);
312 resource->align = log2(HT_MEM_HOST_ALIGN);
313 resource->gran = log2(HT_MEM_HOST_ALIGN);
314 resource->limit = 0xffffffffffULL;
315 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
316 resource->flags |= IORESOURCE_BRIDGE;
320 /* Initialize the memory constraints on the current bus */
321 resource = amdfam15_find_mempair(dev, nodeid, link);
325 resource->align = log2(HT_MEM_HOST_ALIGN);
326 resource->gran = log2(HT_MEM_HOST_ALIGN);
327 resource->limit = 0xffffffffffULL;
328 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
334 static void read_resources(device_t dev)
339 nodeid = amdfam15_nodeid(dev);
340 for (link = dev->link_list; link; link = link->next) {
341 if (link->children) {
342 amdfam15_link_read_bases(dev, nodeid, link->link_num);
348 static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
350 resource_t rbase, rend;
351 unsigned reg, link_num;
355 /* Make certain the resource has actually been set */
356 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
360 /* If I have already stored this resource don't worry about it */
361 if (resource->flags & IORESOURCE_STORED) {
365 /* Only handle PCI memory and IO resources */
366 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
369 /* Ensure I am actually looking at a resource of function 1 */
370 if ((resource->index & 0xffff) < 0x1000) {
373 /* Get the base address */
374 rbase = resource->base;
376 /* Get the limit (rounded up) */
377 rend = resource_end(resource);
379 /* Get the register and link */
380 reg = resource->index & 0xfff; // 4k
381 link_num = IOINDEX_LINK(resource->index);
383 if (resource->flags & IORESOURCE_IO) {
384 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
386 else if (resource->flags & IORESOURCE_MEM) {
387 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums) ;// [39:8]
389 resource->flags |= IORESOURCE_STORED;
390 sprintf(buf, " <node %x link %x>",
392 report_resource_stored(dev, resource, buf);
396 * I tried to reuse the resource allocation code in set_resource()
397 * but it is too difficult to deal with the resource allocation magic.
400 static void create_vga_resource(device_t dev, unsigned nodeid)
405 /* find out which link the VGA card is connected,
406 * we only deal with the 'first' vga card */
407 for (link = dev->link_list; link; link = link->next) {
408 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
409 #if CONFIG_MULTIPLE_VGA_ADAPTERS == 1
410 extern device_t vga_pri; // the primary vga device, defined in device.c
411 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
412 link->secondary,link->subordinate);
413 /* We need to make sure the vga_pri is under the link */
414 if((vga_pri->bus->secondary >= link->secondary ) &&
415 (vga_pri->bus->secondary <= link->subordinate )
422 /* no VGA card installed */
426 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
427 set_vga_enable_reg(nodeid, sblink);
431 static void set_resources(device_t dev)
435 struct resource *res;
437 /* Find the nodeid */
438 nodeid = amdfam15_nodeid(dev);
440 create_vga_resource(dev, nodeid); //TODO: do we need this?
442 /* Set each resource we have found */
443 for (res = dev->resource_list; res; res = res->next) {
444 set_resource(dev, res, nodeid);
447 for (bus = dev->link_list; bus; bus = bus->next) {
449 assign_resources(bus);
454 static void northbridge_init(struct device *dev)
458 static unsigned scan_chains(device_t dev, unsigned max)
462 device_t io_hub = NULL;
463 u32 next_unitid = 0x18;
464 nodeid = amdfam15_nodeid(dev);
466 for (link = dev->link_list; link; link = link->next) {
467 //if (link->link_num == sblink) { /* devicetree put IO Hub on link_lsit[sblink] */
468 if (link->link_num == 0) { /* devicetree put IO Hub on link_lsit[0] */
469 io_hub = link->children;
470 if (!io_hub || !io_hub->enabled) {
471 die("I can't find the IO Hub, or IO Hub not enabled, please check the device tree.\n");
473 /* Now that nothing is overlapping it is safe to scan the children. */
474 max = pci_scan_bus(link, 0x00, ((next_unitid - 1) << 3) | 7, 0);
481 static struct device_operations northbridge_operations = {
482 .read_resources = read_resources,
483 .set_resources = set_resources,
484 .enable_resources = pci_dev_enable_resources,
485 .init = northbridge_init,
486 .scan_bus = scan_chains,
491 static const struct pci_driver family15_northbridge __pci_driver = {
492 .ops = &northbridge_operations,
493 .vendor = PCI_VENDOR_ID_AMD,
494 .device = PCI_DEVICE_ID_AMD_15H_MODEL_000F_NB_HT,
497 static const struct pci_driver family10_northbridge __pci_driver = {
498 .ops = &northbridge_operations,
499 .vendor = PCI_VENDOR_ID_AMD,
500 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
503 struct chip_operations northbridge_amd_agesa_family15_ops = {
504 CHIP_NAME("AMD FAM15 Northbridge")
508 static void domain_read_resources(device_t dev)
513 /* Find the already assigned resource pairs */
515 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
517 base = f1_read_config32(reg);
518 limit = f1_read_config32(reg + 0x04);
519 /* Is this register allocated? */
520 if ((base & 3) != 0) {
521 unsigned nodeid, reg_link;
523 if (reg<0xc0) { // mmio
524 nodeid = (limit & 0xf) + (base&0x30);
526 nodeid = (limit & 0xf) + ((base>>4)&0x30);
528 reg_link = (limit >> 4) & 7;
529 reg_dev = __f0_dev[nodeid];
531 /* Reserve the resource */
532 struct resource *res;
533 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
540 /* FIXME: do we need to check extend conf space?
541 I don't believe that much preset value */
543 #if CONFIG_PCI_64BIT_PREF_MEM == 0
544 pci_domain_read_resources(dev);
549 struct resource *resource;
550 for (link=dev->link_list; link; link = link->next) {
551 /* Initialize the system wide io space constraints */
552 resource = new_resource(dev, 0|(link->link_num<<2));
553 resource->base = 0x400;
554 resource->limit = 0xffffUL;
555 resource->flags = IORESOURCE_IO;
557 /* Initialize the system wide prefetchable memory resources constraints */
558 resource = new_resource(dev, 1|(link->link_num<<2));
559 resource->limit = 0xfcffffffffULL;
560 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
562 /* Initialize the system wide memory resources constraints */
563 resource = new_resource(dev, 2|(link->link_num<<2));
564 resource->limit = 0xfcffffffffULL;
565 resource->flags = IORESOURCE_MEM;
570 static void domain_enable_resources(device_t dev)
573 /* Must be called after PCI enumeration and resource allocation */
574 printk(BIOS_DEBUG, "\nFam15 - domain_enable_resources: AmdInitMid.\n");
575 val = agesawrapper_amdinitmid();
577 printk(BIOS_DEBUG, "agesawrapper_amdinitmid failed: %x \n", val);
579 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
583 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
584 struct hw_mem_hole_info {
585 unsigned hole_startk;
588 static struct hw_mem_hole_info get_hw_mem_hole_info(void)
590 struct hw_mem_hole_info mem_hole;
592 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
593 mem_hole.node_id = -1;
594 for (i = 0; i < node_nums; i++) {
597 d = get_dram_base_mask(i);
598 if (!(d.mask & 1)) continue; // no memory on this node
599 hole = pci_read_config32(__f1_dev[i], 0xf0);
600 if (hole & 1) { // we find the hole
601 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
602 mem_hole.node_id = i; // record the node No with hole
603 break; // only one hole
606 //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
607 if (mem_hole.node_id == -1) {
608 resource_t limitk_pri = 0;
609 for (i=0; i<node_nums; i++) {
611 resource_t base_k, limit_k;
612 d = get_dram_base_mask(i);
613 if (!(d.base & 1)) continue;
614 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
615 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
616 if (limitk_pri != base_k) { // we find the hole
617 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
618 mem_hole.node_id = i;
619 break; //only one hole
621 limit_k = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9;
622 limitk_pri = limit_k;
629 #if CONFIG_GFXUMA == 1
630 extern uint64_t uma_memory_base, uma_memory_size;
631 static void add_uma_resource(struct device *dev, int index)
633 struct resource *resource;
635 printk(BIOS_DEBUG, "Adding UMA memory area\n");
636 resource = new_resource(dev, index);
637 resource->base = (resource_t) uma_memory_base;
638 resource->size = (resource_t) uma_memory_size;
639 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
640 IORESOURCE_FIXED | IORESOURCE_STORED |
645 static void domain_set_resources(device_t dev)
647 #if CONFIG_PCI_64BIT_PREF_MEM == 1
648 struct resource *io, *mem1, *mem2;
649 struct resource *res;
651 unsigned long mmio_basek;
655 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
656 struct hw_mem_hole_info mem_hole;
657 u32 reset_memhole = 1;
660 #if CONFIG_PCI_64BIT_PREF_MEM == 1
662 for (link = dev->link_list; link; link = link->next) {
663 /* Now reallocate the pci resources memory with the
664 * highest addresses I can manage.
666 mem1 = find_resource(dev, 1|(link->link_num<<2));
667 mem2 = find_resource(dev, 2|(link->link_num<<2));
669 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
670 mem1->base, mem1->limit, mem1->size, mem1->align);
671 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
672 mem2->base, mem2->limit, mem2->size, mem2->align);
674 /* See if both resources have roughly the same limits */
675 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
676 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
678 /* If so place the one with the most stringent alignment first */
679 if (mem2->align > mem1->align) {
680 struct resource *tmp;
685 /* Now place the memory as high up as it will go */
686 mem2->base = resource_max(mem2);
687 mem1->limit = mem2->base - 1;
688 mem1->base = resource_max(mem1);
691 /* Place the resources as high up as they will go */
692 mem2->base = resource_max(mem2);
693 mem1->base = resource_max(mem1);
696 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
697 mem1->base, mem1->limit, mem1->size, mem1->align);
698 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
699 mem2->base, mem2->limit, mem2->size, mem2->align);
702 for (res = &dev->resource_list; res; res = res->next)
704 res->flags |= IORESOURCE_ASSIGNED;
705 res->flags |= IORESOURCE_STORED;
706 report_resource_stored(dev, res, "");
710 pci_tolm = 0xffffffffUL;
711 for (link = dev->link_list; link; link = link->next) {
712 pci_tolm = find_pci_tolm(link);
715 // FIXME handle interleaved nodes. If you fix this here, please fix
717 mmio_basek = pci_tolm >> 10;
718 /* Round mmio_basek to something the processor can support */
719 mmio_basek &= ~((1 << 6) -1);
721 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
722 // MMIO hole. If you fix this here, please fix amdk8, too.
723 /* Round the mmio hole to 64M */
724 mmio_basek &= ~((64*1024) - 1);
726 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
727 /* if the hw mem hole is already set in raminit stage, here we will compare
728 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
729 * use hole_basek as mmio_basek and we don't need to reset hole.
730 * otherwise We reset the hole to the mmio_basek
733 mem_hole = get_hw_mem_hole_info();
735 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
736 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
737 mmio_basek = mem_hole.hole_startk;
743 for (i = 0; i < node_nums; i++) {
745 resource_t basek, limitk, sizek; // 4 1T
747 d = get_dram_base_mask(i);
749 if (!(d.mask & 1)) continue;
750 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
751 limitk = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9 ;
753 sizek = limitk - basek;
756 /* see if we need a hole from 0xa0000 to 0xbffff */
757 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
758 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
760 basek = (8*64)+(16*16);
761 sizek = limitk - ((8*64)+(16*16));
765 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
767 /* split the region to accomodate pci memory space */
768 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
769 if (basek <= mmio_basek) {
771 pre_sizek = mmio_basek - basek;
773 ram_resource(dev, (idx | i), basek, pre_sizek);
776 #if CONFIG_WRITE_HIGH_TABLES==1
777 if (high_tables_base==0) {
778 /* Leave some space for ACPI, PIRQ and MP tables */
779 #if CONFIG_GFXUMA == 1
780 high_tables_base = uma_memory_base - HIGH_MEMORY_SIZE;
782 high_tables_base = (mmio_basek * 1024) - HIGH_MEMORY_SIZE;
784 high_tables_size = HIGH_MEMORY_SIZE;
785 printk(BIOS_DEBUG, " split: %dK table at =%08llx\n",
786 (u32)(high_tables_size / 1024), high_tables_base);
792 if ((basek + sizek) <= 4*1024*1024) {
797 sizek -= (4*1024*1024 - mmio_basek);
801 #if CONFIG_GFXUMA == 1
802 /* Deduct uma memory before reporting because
803 * this is what the mtrr code expects */
804 sizek -= uma_memory_size / 1024;
806 ram_resource(dev, (idx | i), basek, sizek);
808 #if CONFIG_WRITE_HIGH_TABLES==1
809 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
810 i, mmio_basek, basek, limitk);
811 if (high_tables_base==0) {
812 /* Leave some space for ACPI, PIRQ and MP tables */
813 #if CONFIG_GFXUMA == 1
814 high_tables_base = uma_memory_base - HIGH_MEMORY_SIZE;
816 high_tables_base = (limitk * 1024) - HIGH_MEMORY_SIZE;
818 high_tables_size = HIGH_MEMORY_SIZE;
823 #if CONFIG_GFXUMA == 1
824 add_uma_resource(dev, 7);
827 for(link = dev->link_list; link; link = link->next) {
828 if (link->children) {
829 assign_resources(link);
835 static struct device_operations pci_domain_ops = {
836 .read_resources = domain_read_resources,
837 .set_resources = domain_set_resources,
838 .enable_resources = domain_enable_resources,
840 .scan_bus = pci_domain_scan_bus,
842 #if CONFIG_MMCONF_SUPPORT_DEFAULT
843 .ops_pci_bus = &pci_ops_mmconf,
845 .ops_pci_bus = &pci_cf8_conf1,
850 static void sysconf_init(device_t dev) // first node
852 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
853 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
856 static void add_more_links(device_t dev, unsigned total_links)
858 struct bus *link, *last = NULL;
861 for (link = dev->link_list; link; link = link->next)
865 int links = total_links - last->link_num;
866 link_num = last->link_num;
868 link = malloc(links*sizeof(*link));
870 die("Couldn't allocate more links!\n");
871 memset(link, 0, links*sizeof(*link));
877 link = malloc(total_links*sizeof(*link));
878 memset(link, 0, total_links*sizeof(*link));
879 dev->link_list = link;
882 for (link_num = link_num + 1; link_num < total_links; link_num++) {
883 link->link_num = link_num;
885 link->next = link + 1;
892 /* dummy read_resources */
893 static void lapic_read_resources(device_t dev)
897 static struct device_operations lapic_ops = {
898 .read_resources = lapic_read_resources,
899 .set_resources = pci_dev_set_resources,
900 .enable_resources = pci_dev_enable_resources,
907 static u32 cpu_bus_scan(device_t dev, u32 max)
917 unsigned ApicIdCoreIdSize;
923 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
924 if (dev_mc && dev_mc->bus) {
925 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
926 pci_domain = dev_mc->bus->dev;
927 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
928 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
929 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
930 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
932 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
934 printk(BIOS_DEBUG, "\n");
936 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
938 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
939 if (dev_mc && dev_mc->bus) {
940 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
941 pci_domain = dev_mc->bus->dev;
942 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
943 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
944 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
945 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
946 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
948 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
949 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
950 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
951 dev_mc = dev_mc->sibling;
958 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
960 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
963 sysconf_init(dev_mc);
964 #if CONFIG_CBB && (MAX_NODE_NUMS > 32)
965 if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
966 if (pci_domain->link_list && !pci_domain->link_list->next) {
967 struct bus *new_link = new_link(pci_domain);
968 pci_domain->link_list->next = new_link;
969 new_link->link_num = 1;
970 new_link->dev = pci_domain;
971 new_link->children = 0;
972 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
974 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
978 /* Get Max Number of cores(MNC) */
979 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
980 core_max = 1 << (coreid_bits & 0x000F); //mnc
982 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
983 if (ApicIdCoreIdSize) {
984 core_nums = (1 << ApicIdCoreIdSize) - 1;
986 core_nums = 3; //quad core
989 /* Find which cpus are present */
990 cpu_bus = dev->link_list;
991 for (i = 0; i < node_nums; i++) {
992 device_t cdb_dev, cpu;
993 struct device_path cpu_path;
998 devn = CONFIG_CDB + i;
1000 #if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1004 pbus = pci_domain->link_list->next;
1008 /* Find the cpu's pci device */
1009 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1011 /* If I am probing things in a weird order
1012 * ensure all of the cpu's pci devices are found.
1015 for(fn = 0; fn <= 5; fn++) { //FBDIMM?
1016 cdb_dev = pci_probe_dev(NULL, pbus,
1017 PCI_DEVFN(devn, fn));
1019 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1021 /* Ok, We need to set the links for that device.
1022 * otherwise the device under it will not be scanned
1025 #if CONFIG_HT3_SUPPORT==1
1030 add_more_links(cdb_dev, linknum);
1033 family = cpuid_eax(1);
1034 family = (family >> 20) & 0xFF;
1035 if (family == 1) { //f10
1037 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1038 dword = pci_read_config32(cdb_dev, 0xe8);
1039 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1040 } else if (family == 6) {//f15
1041 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1042 if (cdb_dev && cdb_dev->enabled) {
1043 siblings = pci_read_config32(cdb_dev, 0x84);
1047 siblings = 0; //default one core
1049 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1050 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1052 for (j = 0; j <= siblings; j++ ) {
1053 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1054 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1055 u32 lapicid_start = 0;
1057 /* Build the cpu device path */
1058 cpu_path.type = DEVICE_PATH_APIC;
1060 * APIC ID calucation is tightly coupled with AGESA v5 code.
1061 * This calculation MUST match the assignment calculation done
1062 * in LocalApicInitializationAtEarly() function.
1063 * And reference GetLocalApicIdForCore()
1065 * Apply apic enumeration rules
1066 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1067 * put the local-APICs at m..z
1069 * This is needed because many IO-APIC devices only have 4 bits
1070 * for their APIC id and therefore must reside at 0..15
1072 #ifndef CFG_PLAT_NUM_IO_APICS /* defined in mainboard buildOpts.c */
1073 #define CFG_PLAT_NUM_IO_APICS 3
1075 if ((node_nums * core_max) + CFG_PLAT_NUM_IO_APICS >= 0x10) {
1076 lapicid_start = (CFG_PLAT_NUM_IO_APICS - 1) / core_max;
1077 lapicid_start = (lapicid_start + 1) * core_max;
1078 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1080 cpu_path.apic.apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
1081 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
1082 i, j, cpu_path.apic.apic_id);
1084 /* See if I can find the cpu */
1085 cpu = find_dev_path(cpu_bus, &cpu_path);
1086 /* Enable the cpu if I have the processor */
1087 if (cdb_dev && cdb_dev->enabled) {
1089 cpu = alloc_dev(cpu_bus, &cpu_path);
1095 /* Disable the cpu if I don't have the processor */
1096 if (cpu && (!cdb_dev || !cdb_dev->enabled)) {
1099 /* Report what I have done */
1101 cpu->path.apic.node_id = i;
1102 cpu->path.apic.core_id = j;
1103 if (cpu->path.type == DEVICE_PATH_APIC) {
1104 cpu->ops = &lapic_ops;
1106 printk(BIOS_DEBUG, "CPU: %s %s\n",
1107 dev_path(cpu), cpu->enabled?"enabled":"disabled");
1114 static void cpu_bus_init(device_t dev)
1116 initialize_cpus(dev->link_list);
1119 static void cpu_bus_noop(device_t dev)
1123 static void cpu_bus_read_resources(device_t dev)
1125 #if CONFIG_MMCONF_SUPPORT
1126 struct resource *resource = new_resource(dev, 0xc0010058);
1127 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
1128 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096*256;
1129 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
1130 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
1134 static void cpu_bus_set_resources(device_t dev)
1136 struct resource *resource = find_resource(dev, 0xc0010058);
1138 report_resource_stored(dev, resource, " <mmconfig>");
1140 pci_dev_set_resources(dev);
1143 static struct device_operations cpu_bus_ops = {
1144 .read_resources = cpu_bus_read_resources,
1145 .set_resources = cpu_bus_set_resources,
1146 .enable_resources = cpu_bus_noop,
1147 .init = cpu_bus_init,
1148 .scan_bus = cpu_bus_scan,
1152 static void root_complex_enable_dev(struct device *dev)
1154 /* Set the operations if it is a special bus type */
1155 if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
1156 dev->ops = &pci_domain_ops;
1157 } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
1158 dev->ops = &cpu_bus_ops;
1163 struct chip_operations northbridge_amd_agesa_family15_root_complex_ops = {
1164 CHIP_NAME("AMD FAM15 Root Complex")
1165 .enable_dev = root_complex_enable_dev,