Factor out a few commonly duplicated functions from northbridge.c.
[coreboot.git] / src / northbridge / amd / amdfam10 / northbridge.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 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 <stdint.h>
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <device/pci_ids.h>
26 #include <device/hypertransport.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <bitops.h>
30 #include <cpu/cpu.h>
31
32 #include <cpu/x86/lapic.h>
33
34 #if CONFIG_LOGICAL_CPUS==1
35 #include <cpu/amd/multicore.h>
36 #include <pc80/mc146818rtc.h>
37 #endif
38
39 #include "chip.h"
40 #include "root_complex/chip.h"
41 #include "northbridge.h"
42
43 #include "amdfam10.h"
44
45 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
46 #include <cpu/amd/model_10xxx_rev.h>
47 #endif
48
49 #include <cpu/amd/amdfam10_sysconf.h>
50
51 struct amdfam10_sysconf_t sysconf;
52
53 #define FX_DEVS NODE_NUMS
54 static device_t __f0_dev[FX_DEVS];
55 static device_t __f1_dev[FX_DEVS];
56 static device_t __f2_dev[FX_DEVS];
57 static device_t __f4_dev[FX_DEVS];
58 static unsigned fx_devs=0;
59
60 device_t get_node_pci(u32 nodeid, u32 fn)
61 {
62 #if NODE_NUMS == 64
63         if(nodeid<32) {
64                 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
65         } else {
66                 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
67         }
68
69 #else
70         return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
71 #endif
72 }
73
74 static void get_fx_devs(void)
75 {
76         int i;
77         for(i = 0; i < FX_DEVS; i++) {
78                 __f0_dev[i] = get_node_pci(i, 0);
79                 __f1_dev[i] = get_node_pci(i, 1);
80                 __f2_dev[i] = get_node_pci(i, 2);
81                 __f4_dev[i] = get_node_pci(i, 4);
82                 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
83                         fx_devs = i+1;
84         }
85         if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
86                 die("Cannot find 0:0x18.[0|1]\n");
87         }
88 }
89
90 static u32 f1_read_config32(unsigned reg)
91 {
92         if (fx_devs == 0)
93                 get_fx_devs();
94         return pci_read_config32(__f1_dev[0], reg);
95 }
96
97 static void f1_write_config32(unsigned reg, u32 value)
98 {
99         int i;
100         if (fx_devs == 0)
101                 get_fx_devs();
102         for(i = 0; i < fx_devs; i++) {
103                 device_t dev;
104                 dev = __f1_dev[i];
105                 if (dev && dev->enabled) {
106                         pci_write_config32(dev, reg, value);
107                 }
108         }
109 }
110
111 static u32 amdfam10_nodeid(device_t dev)
112 {
113 #if NODE_NUMS == 64
114         unsigned busn;
115         busn = dev->bus->secondary;
116         if(busn != CONFIG_CBB) {
117                 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
118         } else {
119                 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
120         }
121
122 #else
123         return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
124 #endif
125 }
126
127 #include "amdfam10_conf.c"
128
129 static void set_vga_enable_reg(u32 nodeid, u32 linkn)
130 {
131         u32 val;
132
133         val =  1 | (nodeid<<4) | (linkn<<12);
134         /* it will routing (1)mmio  0xa0000:0xbffff (2) io 0x3b0:0x3bb,
135          0x3c0:0x3df */
136         f1_write_config32(0xf4, val);
137
138 }
139
140 static u32 amdfam10_scan_chain(device_t dev, u32 nodeid, struct bus *link, u32 link_num, u32 sblink,
141                                 u32 max, u32 offset_unitid)
142 {
143 //      I want to put sb chain in bus 0 can I?
144
145
146                 u32 link_type;
147                 int i;
148                 u32 ht_c_index;
149                 u32 ht_unitid_base[4]; // here assume only 4 HT device on chain
150                 u32 max_bus;
151                 u32 min_bus;
152                 u32 is_sublink1 = (link_num>3);
153                 device_t devx;
154                 u32 busses;
155                 u32 segn = max>>8;
156 #if CONFIG_SB_HT_CHAIN_ON_BUS0 > 1
157                 u32 busn = max&0xff;
158 #endif
159                 u32 max_devfn;
160
161 #if CONFIG_HT3_SUPPORT==1
162                 if(is_sublink1) {
163                         u32 regpos;
164                         u32 reg;
165                         regpos = 0x170 + 4 * (link_num&3); // it is only on sublink0
166                         reg = pci_read_config32(dev, regpos);
167                         if(reg & 1) return max; // already ganged no sblink1
168                         devx = get_node_pci(nodeid, 4);
169                 } else
170 #endif
171                         devx = dev;
172
173
174                 link->cap = 0x80 + ((link_num&3) *0x20);
175                 do {
176                         link_type = pci_read_config32(devx, link->cap + 0x18);
177                 } while(link_type & ConnectionPending);
178                 if (!(link_type & LinkConnected)) {
179                         return max;
180                 }
181                 do {
182                         link_type = pci_read_config32(devx, link->cap + 0x18);
183                 } while(!(link_type & InitComplete));
184                 if (!(link_type & NonCoherent)) {
185                         return max;
186                 }
187                 /* See if there is an available configuration space mapping
188                  * register in function 1.
189                  */
190                 ht_c_index = get_ht_c_index(nodeid, link_num, &sysconf);
191
192 #if CONFIG_EXT_CONF_SUPPORT == 0
193                 if(ht_c_index>=4) return max;
194 #endif
195
196                 /* Set up the primary, secondary and subordinate bus numbers.
197                  * We have no idea how many busses are behind this bridge yet,
198                  * so we set the subordinate bus number to 0xff for the moment.
199                  */
200 #if CONFIG_SB_HT_CHAIN_ON_BUS0 > 0
201                 // first chain will on bus 0
202                 if((nodeid == 0) && (sblink==link_num)) { // actually max is 0 here
203                         min_bus = max;
204                 }
205         #if CONFIG_SB_HT_CHAIN_ON_BUS0 > 1
206                 // second chain will be on 0x40, third 0x80, forth 0xc0
207                 // i would refined that to  2, 3, 4 ==> 0, 0x, 40, 0x80, 0xc0
208                 //                          >4 will use  more segments, We can have 16 segmment and every segment have 256 bus, For that case need the kernel support mmio pci config.
209                 else {
210                         min_bus = ((busn>>3) + 1) << 3; // one node can have 8 link and segn is the same
211                 }
212                 max = min_bus | (segn<<8);
213         #else
214                 //other ...
215                 else {
216                         min_bus = ++max;
217                 }
218         #endif
219 #else
220                 min_bus = ++max;
221 #endif
222                 max_bus = 0xfc | (segn<<8);
223
224                 link->secondary = min_bus;
225                 link->subordinate = max_bus;
226
227                 /* Read the existing primary/secondary/subordinate bus
228                  * number configuration.
229                  */
230                 busses = pci_read_config32(devx, link->cap + 0x14);
231
232                 /* Configure the bus numbers for this bridge: the configuration
233                  * transactions will not be propagates by the bridge if it is
234                  * not correctly configured
235                  */
236                 busses &= 0xffff00ff;
237                 busses |= ((u32)(link->secondary) << 8);
238                 pci_write_config32(devx, link->cap + 0x14, busses);
239
240
241                 /* set the config map space */
242
243                 set_config_map_reg(nodeid, link_num, ht_c_index, link->secondary, link->subordinate, sysconf.segbit, sysconf.nodes);
244
245                 /* Now we can scan all of the subordinate busses i.e. the
246                  * chain on the hypertranport link
247                  */
248                 for(i=0;i<4;i++) {
249                         ht_unitid_base[i] = 0x20;
250                 }
251
252                 //if ext conf is enabled, only need use 0x1f
253                 if (min_bus == 0)
254                         max_devfn = (0x17<<3) | 7;
255                 else
256                         max_devfn = (0x1f<<3) | 7;
257
258                 max = hypertransport_scan_chain(link, 0, max_devfn, max, ht_unitid_base, offset_unitid);
259
260                 /* We know the number of busses behind this bridge.  Set the
261                  * subordinate bus number to it's real value
262                  */
263                 if(ht_c_index>3) { // clear the extend reg
264                         clear_config_map_reg(nodeid, link_num, ht_c_index, (max+1)>>sysconf.segbit, (link->subordinate)>>sysconf.segbit, sysconf.nodes);
265                 }
266
267                 link->subordinate = max;
268                 set_config_map_reg(nodeid, link_num, ht_c_index, link->secondary, link->subordinate, sysconf.segbit, sysconf.nodes);
269                 sysconf.ht_c_num++;
270
271                 {
272                         // use ht_unitid_base to update hcdn_reg
273                         u32 temp = 0;
274                         for(i=0;i<4;i++) {
275                                 temp |= (ht_unitid_base[i] & 0xff) << (i*8);
276                         }
277
278                         sysconf.hcdn_reg[ht_c_index] = temp;
279
280                 }
281         store_ht_c_conf_bus(nodeid, link_num, ht_c_index, link->secondary, link->subordinate, &sysconf);
282         return max;
283 }
284
285 static unsigned amdfam10_scan_chains(device_t dev, unsigned max)
286 {
287         unsigned nodeid;
288         struct bus *link;
289         unsigned sblink = sysconf.sblk;
290         unsigned offset_unitid = 0;
291
292         nodeid = amdfam10_nodeid(dev);
293
294 // Put sb chain in bus 0
295 #if CONFIG_SB_HT_CHAIN_ON_BUS0 > 0
296         if(nodeid==0) {
297         #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
298                 offset_unitid = 1;
299         #endif
300                 for (link = dev->link_list; link; link = link->next)
301                         if (link->link_num == sblink)
302                                 max = amdfam10_scan_chain(dev, nodeid, link, sblink, sblink, max, offset_unitid ); // do sb ht chain at first, in case s2885 put sb chain (8131/8111) on link2, but put 8151 on link0
303         }
304 #endif
305
306 #if CONFIG_PCI_BUS_SEGN_BITS
307         max = check_segn(dev, max, sysconf.nodes, &sysconf);
308 #endif
309
310         for(link = dev->link_list; link; link = link->next) {
311 #if CONFIG_SB_HT_CHAIN_ON_BUS0 > 0
312                 if( (nodeid == 0) && (sblink == link->link_num) ) continue; //already done
313 #endif
314                 offset_unitid = 0;
315                 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
316                         #if CONFIG_SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
317                         if((nodeid == 0) && (sblink == link->link_num))
318                         #endif
319                                 offset_unitid = 1;
320                 #endif
321
322                 max = amdfam10_scan_chain(dev, nodeid, link, link->link_num, sblink, max, offset_unitid);
323         }
324         return max;
325 }
326
327
328 static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
329                         unsigned goal_link)
330 {
331         struct resource *res;
332         unsigned nodeid, link = 0;
333         int result;
334         res = 0;
335         for(nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
336                 device_t dev;
337                 dev = __f0_dev[nodeid];
338                 if (!dev)
339                         continue;
340                 for(link = 0; !res && (link < 8); link++) {
341                         res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
342                 }
343         }
344         result = 2;
345         if (res) {
346                 result = 0;
347                 if (    (goal_link == (link - 1)) &&
348                         (goal_nodeid == (nodeid - 1)) &&
349                         (res->flags <= 1)) {
350                         result = 1;
351                 }
352         }
353         return result;
354 }
355
356 static struct resource *amdfam10_find_iopair(device_t dev, unsigned nodeid, unsigned link)
357 {
358         struct resource *resource;
359         u32 free_reg, reg;
360         resource = 0;
361         free_reg = 0;
362         for(reg = 0xc0; reg <= 0xd8; reg += 0x8) {
363                 int result;
364                 result = reg_useable(reg, dev, nodeid, link);
365                 if (result == 1) {
366                         /* I have been allocated this one */
367                         break;
368                 }
369                 else if (result > 1) {
370                         /* I have a free register pair */
371                         free_reg = reg;
372                 }
373         }
374         if (reg > 0xd8) {
375                 reg = free_reg; // if no free, the free_reg still be 0
376         }
377
378         //Ext conf space
379         if(!reg) {
380                 //because of Extend conf space, we will never run out of reg, but we need one index to differ them. so same node and same link can have multi range
381                 u32 index = get_io_addr_index(nodeid, link);
382                 reg = 0x110+ (index<<24) + (4<<20); // index could be 0, 255
383         }
384
385                 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
386
387         return resource;
388 }
389
390 static struct resource *amdfam10_find_mempair(device_t dev, u32 nodeid, u32 link)
391 {
392         struct resource *resource;
393         u32 free_reg, reg;
394         resource = 0;
395         free_reg = 0;
396         for(reg = 0x80; reg <= 0xb8; reg += 0x8) {
397                 int result;
398                 result = reg_useable(reg, dev, nodeid, link);
399                 if (result == 1) {
400                         /* I have been allocated this one */
401                         break;
402                 }
403                 else if (result > 1) {
404                         /* I have a free register pair */
405                         free_reg = reg;
406                 }
407         }
408         if (reg > 0xb8) {
409                 reg = free_reg;
410         }
411
412         //Ext conf space
413         if(!reg) {
414                 //because of Extend conf space, we will never run out of reg,
415                 // but we need one index to differ them. so same node and
416                 // same link can have multi range
417                 u32 index = get_mmio_addr_index(nodeid, link);
418                 reg = 0x110+ (index<<24) + (6<<20); // index could be 0, 63
419
420         }
421         resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
422         return resource;
423 }
424
425
426 static void amdfam10_link_read_bases(device_t dev, u32 nodeid, u32 link)
427 {
428         struct resource *resource;
429
430         /* Initialize the io space constraints on the current bus */
431         resource = amdfam10_find_iopair(dev, nodeid, link);
432         if (resource) {
433                 u32 align;
434 #if CONFIG_EXT_CONF_SUPPORT == 1
435                 if((resource->index & 0x1fff) == 0x1110) { // ext
436                         align = 8;
437                 }
438                 else
439 #endif
440                         align = log2(HT_IO_HOST_ALIGN);
441                 resource->base  = 0;
442                 resource->size  = 0;
443                 resource->align = align;
444                 resource->gran  = align;
445                 resource->limit = 0xffffUL;
446                 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
447         }
448
449         /* Initialize the prefetchable memory constraints on the current bus */
450         resource = amdfam10_find_mempair(dev, nodeid, link);
451         if (resource) {
452                 resource->base = 0;
453                 resource->size = 0;
454                 resource->align = log2(HT_MEM_HOST_ALIGN);
455                 resource->gran = log2(HT_MEM_HOST_ALIGN);
456                 resource->limit = 0xffffffffffULL;
457                 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
458                 resource->flags |= IORESOURCE_BRIDGE;
459
460 #if CONFIG_EXT_CONF_SUPPORT == 1
461                 if((resource->index & 0x1fff) == 0x1110) { // ext
462                         normalize_resource(resource);
463                 }
464 #endif
465
466         }
467
468         /* Initialize the memory constraints on the current bus */
469         resource = amdfam10_find_mempair(dev, nodeid, link);
470         if (resource) {
471                 resource->base = 0;
472                 resource->size = 0;
473                 resource->align = log2(HT_MEM_HOST_ALIGN);
474                 resource->gran = log2(HT_MEM_HOST_ALIGN);
475                 resource->limit = 0xffffffffffULL;
476                 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
477 #if CONFIG_EXT_CONF_SUPPORT == 1
478                 if((resource->index & 0x1fff) == 0x1110) { // ext
479                         normalize_resource(resource);
480                 }
481 #endif
482         }
483 }
484
485 static void amdfam10_read_resources(device_t dev)
486 {
487         u32 nodeid;
488         struct bus *link;
489         nodeid = amdfam10_nodeid(dev);
490         for(link = dev->link_list; link; link = link->next) {
491                 if (link->children) {
492                         amdfam10_link_read_bases(dev, nodeid, link->link_num);
493                 }
494         }
495 }
496
497 static void amdfam10_set_resource(device_t dev, struct resource *resource,
498                                 u32 nodeid)
499 {
500         resource_t rbase, rend;
501         unsigned reg, link_num;
502         char buf[50];
503
504         /* Make certain the resource has actually been set */
505         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
506                 return;
507         }
508
509         /* If I have already stored this resource don't worry about it */
510         if (resource->flags & IORESOURCE_STORED) {
511                 return;
512         }
513
514         /* Only handle PCI memory and IO resources */
515         if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
516                 return;
517
518         /* Ensure I am actually looking at a resource of function 1 */
519         if ((resource->index & 0xffff) < 0x1000) {
520                 return;
521         }
522         /* Get the base address */
523         rbase = resource->base;
524
525         /* Get the limit (rounded up) */
526         rend  = resource_end(resource);
527
528         /* Get the register and link */
529         reg  = resource->index & 0xfff; // 4k
530         link_num = IOINDEX_LINK(resource->index);
531
532         if (resource->flags & IORESOURCE_IO) {
533
534                 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
535                 store_conf_io_addr(nodeid, link_num, reg, (resource->index >> 24), rbase>>8, rend>>8);
536         }
537         else if (resource->flags & IORESOURCE_MEM) {
538                 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, sysconf.nodes) ;// [39:8]
539                 store_conf_mmio_addr(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8);
540         }
541         resource->flags |= IORESOURCE_STORED;
542         sprintf(buf, " <node %x link %x>",
543                 nodeid, link_num);
544         report_resource_stored(dev, resource, buf);
545 }
546
547 /**
548  *
549  * I tried to reuse the resource allocation code in amdfam10_set_resource()
550  * but it is too diffcult to deal with the resource allocation magic.
551  */
552 #if CONFIG_CONSOLE_VGA_MULTI == 1
553 extern device_t vga_pri;        // the primary vga device, defined in device.c
554 #endif
555
556 static void amdfam10_create_vga_resource(device_t dev, unsigned nodeid)
557 {
558         struct bus *link;
559
560         /* find out which link the VGA card is connected,
561          * we only deal with the 'first' vga card */
562         for (link = dev->link_list; link; link = link->next) {
563                 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
564 #if CONFIG_CONSOLE_VGA_MULTI == 1
565                         printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
566                                 link->secondary,link->subordinate);
567                         /* We need to make sure the vga_pri is under the link */
568                         if((vga_pri->bus->secondary >= link->secondary ) &&
569                                 (vga_pri->bus->secondary <= link->subordinate )
570                         )
571 #endif
572                         break;
573                 }
574         }
575
576         /* no VGA card installed */
577         if (link == NULL)
578                 return;
579
580         printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, link->link_num);
581         set_vga_enable_reg(nodeid, link->link_num);
582 }
583
584 static void amdfam10_set_resources(device_t dev)
585 {
586         unsigned nodeid;
587         struct bus *bus;
588         struct resource *res;
589
590         /* Find the nodeid */
591         nodeid = amdfam10_nodeid(dev);
592
593         amdfam10_create_vga_resource(dev, nodeid);
594
595         /* Set each resource we have found */
596         for(res = dev->resource_list; res; res = res->next) {
597                 amdfam10_set_resource(dev, res, nodeid);
598         }
599
600         for(bus = dev->link_list; bus; bus = bus->next) {
601                 if (bus->children) {
602                         assign_resources(bus);
603                 }
604         }
605 }
606
607 static void mcf0_control_init(struct device *dev)
608 {
609 }
610
611 static struct device_operations northbridge_operations = {
612         .read_resources   = amdfam10_read_resources,
613         .set_resources    = amdfam10_set_resources,
614         .enable_resources = pci_dev_enable_resources,
615         .init             = mcf0_control_init,
616         .scan_bus         = amdfam10_scan_chains,
617         .enable           = 0,
618         .ops_pci          = 0,
619 };
620
621
622 static const struct pci_driver mcf0_driver __pci_driver = {
623         .ops    = &northbridge_operations,
624         .vendor = PCI_VENDOR_ID_AMD,
625         .device = 0x1200,
626 };
627
628 struct chip_operations northbridge_amd_amdfam10_ops = {
629         CHIP_NAME("AMD FAM10 Northbridge")
630         .enable_dev = 0,
631 };
632
633 static void amdfam10_domain_read_resources(device_t dev)
634 {
635         unsigned reg;
636
637         /* Find the already assigned resource pairs */
638         get_fx_devs();
639         for(reg = 0x80; reg <= 0xd8; reg+= 0x08) {
640                 u32 base, limit;
641                 base  = f1_read_config32(reg);
642                 limit = f1_read_config32(reg + 0x04);
643                 /* Is this register allocated? */
644                 if ((base & 3) != 0) {
645                         unsigned nodeid, reg_link;
646                         device_t reg_dev;
647                         if(reg<0xc0) { // mmio
648                                 nodeid = (limit & 0xf) + (base&0x30);
649                         } else { // io
650                                 nodeid =  (limit & 0xf) + ((base>>4)&0x30);
651                         }
652                         reg_link = (limit >> 4) & 7;
653                         reg_dev = __f0_dev[nodeid];
654                         if (reg_dev) {
655                                 /* Reserve the resource  */
656                                 struct resource *res;
657                                 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
658                                 if (res) {
659                                         res->flags = 1;
660                                 }
661                         }
662                 }
663         }
664         /* FIXME: do we need to check extend conf space?
665            I don't believe that much preset value */
666
667 #if CONFIG_PCI_64BIT_PREF_MEM == 0
668         pci_domain_read_resources(dev);
669 #else
670         struct bus *link;
671         struct resource *resource;
672         for(link=dev->link_list; link; link = link->next) {
673                 /* Initialize the system wide io space constraints */
674                 resource = new_resource(dev, 0|(link->link_num<<2));
675                 resource->base  = 0x400;
676                 resource->limit = 0xffffUL;
677                 resource->flags = IORESOURCE_IO;
678
679                 /* Initialize the system wide prefetchable memory resources constraints */
680                 resource = new_resource(dev, 1|(link->link_num<<2));
681                 resource->limit = 0xfcffffffffULL;
682                 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
683
684                 /* Initialize the system wide memory resources constraints */
685                 resource = new_resource(dev, 2|(link->link_num<<2));
686                 resource->limit = 0xfcffffffffULL;
687                 resource->flags = IORESOURCE_MEM;
688         }
689 #endif
690 }
691
692 static u32 my_find_pci_tolm(struct bus *bus, u32 tolm)
693 {
694         struct resource *min;
695         min = 0;
696         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
697         if (min && tolm > min->base) {
698                 tolm = min->base;
699         }
700         return tolm;
701 }
702
703 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
704
705 struct hw_mem_hole_info {
706         unsigned hole_startk;
707         int node_id;
708 };
709
710 static struct hw_mem_hole_info get_hw_mem_hole_info(void)
711 {
712                 struct hw_mem_hole_info mem_hole;
713                 int i;
714
715                 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
716                 mem_hole.node_id = -1;
717
718                 for (i = 0; i < sysconf.nodes; i++) {
719                         struct dram_base_mask_t d;
720                         u32 hole;
721                         d = get_dram_base_mask(i);
722                         if(!(d.mask & 1)) continue; // no memory on this node
723
724                         hole = pci_read_config32(__f1_dev[i], 0xf0);
725                         if(hole & 1) { // we find the hole
726                                 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
727                                 mem_hole.node_id = i; // record the node No with hole
728                                 break; // only one hole
729                         }
730                 }
731
732                 //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
733                 if(mem_hole.node_id==-1) {
734                         resource_t limitk_pri = 0;
735                         for(i=0; i<sysconf.nodes; i++) {
736                                 struct dram_base_mask_t d;
737                                 resource_t base_k, limit_k;
738                                 d = get_dram_base_mask(i);
739                                 if(!(d.base & 1)) continue;
740
741                                 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
742                                 if(base_k > 4 *1024 * 1024) break; // don't need to go to check
743                                 if(limitk_pri != base_k) { // we find the hole
744                                         mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
745                                         mem_hole.node_id = i;
746                                         break; //only one hole
747                                 }
748
749                                 limit_k = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9;
750                                 limitk_pri = limit_k;
751                         }
752                 }
753                 return mem_hole;
754 }
755
756 // WHY this check? CONFIG_AMDMCT is enabled on all Fam10 boards.
757 // Does it make sense not to?
758 #if CONFIG_AMDMCT == 0
759 static void disable_hoist_memory(unsigned long hole_startk, int node_id)
760 {
761         int i;
762         device_t dev;
763         struct dram_base_mask_t d;
764         u32 sel_m;
765         u32 sel_hi_en;
766         u32 hoist;
767         u32 hole_sizek;
768
769         u32 one_DCT;
770         struct sys_info *sysinfox = (struct sys_info *)((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE); // in RAM
771         struct mem_info *meminfo;
772         meminfo = &sysinfox->meminfo[node_id];
773
774         one_DCT = get_one_DCT(meminfo);
775
776         // 1. find which node has hole
777         // 2. change limit in that node.
778         // 3. change base and limit in later node
779         // 4. clear that node f0
780
781         // if there is not mem hole enabled, we need to change it's base instead
782
783         hole_sizek = (4*1024*1024) - hole_startk;
784
785         for(i=NODE_NUMS-1;i>node_id;i--) {
786
787                 d = get_dram_base_mask(i);
788
789                 if(!(d.mask & 1)) continue;
790
791                 d.base -= (hole_sizek>>9);
792                 d.mask -= (hole_sizek>>9);
793                 set_dram_base_mask(i, d, sysconf.nodes);
794
795                 if(get_DctSelHiEn(i) & 1) {
796                         sel_m = get_DctSelBaseAddr(i);
797                         sel_m -= hole_startk>>10;
798                         set_DctSelBaseAddr(i, sel_m);
799                 }
800         }
801
802         d = get_dram_base_mask(node_id);
803         dev = __f1_dev[node_id];
804         sel_hi_en = get_DctSelHiEn(node_id);
805
806         if(sel_hi_en & 1) {
807                 sel_m = get_DctSelBaseAddr(node_id);
808         }
809         hoist = pci_read_config32(dev, 0xf0);
810         if(hoist & 1) {
811                 pci_write_config32(dev, 0xf0, 0);
812                 d.mask -= (hole_sizek>>9);
813                 set_dram_base_mask(node_id, d, sysconf.nodes);
814                 if(one_DCT || (sel_m >= (hole_startk>>10))) {
815                         if(sel_hi_en & 1) {
816                                 sel_m -= hole_startk>>10;
817                                 set_DctSelBaseAddr(node_id, sel_m);
818                         }
819                 }
820                 if(sel_hi_en & 1) {
821                         set_DctSelBaseOffset(node_id, 0);
822                 }
823         } else {
824                 d.base -= (hole_sizek>>9);
825                 d.mask -= (hole_sizek>>9);
826                 set_dram_base_mask(node_id, d, sysconf.nodes);
827
828                 if(sel_hi_en & 1) {
829                         sel_m -= hole_startk>>10;
830                         set_DctSelBaseAddr(node_id, sel_m);
831                 }
832         }
833
834 }
835 #endif
836
837 #endif
838
839 #if CONFIG_WRITE_HIGH_TABLES==1
840 #define HIGH_TABLES_SIZE 64     // maximum size of high tables in KB
841 extern uint64_t high_tables_base, high_tables_size;
842 #endif
843
844 #if CONFIG_GFXUMA == 1
845 extern uint64_t uma_memory_base, uma_memory_size;
846
847 static void add_uma_resource(struct device *dev, int index)
848 {
849         struct resource *resource;
850
851         printk(BIOS_DEBUG, "Adding UMA memory area\n");
852         resource = new_resource(dev, index);
853         resource->base = (resource_t) uma_memory_base;
854         resource->size = (resource_t) uma_memory_size;
855         resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
856             IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
857 }
858 #endif
859
860 static void amdfam10_domain_set_resources(device_t dev)
861 {
862 #if CONFIG_PCI_64BIT_PREF_MEM == 1
863         struct resource *io, *mem1, *mem2;
864         struct resource *res;
865 #endif
866         unsigned long mmio_basek;
867         u32 pci_tolm;
868         int i, idx;
869         struct bus *link;
870 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
871         struct hw_mem_hole_info mem_hole;
872         u32 reset_memhole = 1;
873 #endif
874
875 #if CONFIG_PCI_64BIT_PREF_MEM == 1
876
877         for(link = dev->link_list; link; link = link->next) {
878                 /* Now reallocate the pci resources memory with the
879                  * highest addresses I can manage.
880                  */
881                 mem1 = find_resource(dev, 1|(link->link_num<<2));
882                 mem2 = find_resource(dev, 2|(link->link_num<<2));
883
884                 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
885                         mem1->base, mem1->limit, mem1->size, mem1->align);
886                 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
887                         mem2->base, mem2->limit, mem2->size, mem2->align);
888
889                 /* See if both resources have roughly the same limits */
890                 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
891                         ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
892                 {
893                         /* If so place the one with the most stringent alignment first
894                          */
895                         if (mem2->align > mem1->align) {
896                                 struct resource *tmp;
897                                 tmp = mem1;
898                                 mem1 = mem2;
899                                 mem2 = tmp;
900                         }
901                         /* Now place the memory as high up as it will go */
902                         mem2->base = resource_max(mem2);
903                         mem1->limit = mem2->base - 1;
904                         mem1->base = resource_max(mem1);
905                 }
906                 else {
907                         /* Place the resources as high up as they will go */
908                         mem2->base = resource_max(mem2);
909                         mem1->base = resource_max(mem1);
910                 }
911
912                 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
913                         mem1->base, mem1->limit, mem1->size, mem1->align);
914                 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
915                         mem2->base, mem2->limit, mem2->size, mem2->align);
916         }
917
918         for(res = &dev->resource_list; res; res = res->next)
919         {
920                 res->flags |= IORESOURCE_ASSIGNED;
921                 res->flags |= IORESOURCE_STORED;
922                 report_resource_stored(dev, res, "");
923         }
924 #endif
925
926         pci_tolm = 0xffffffffUL;
927         for(link = dev->link_list; link; link = link->next) {
928                 pci_tolm = my_find_pci_tolm(link, pci_tolm);
929         }
930
931         // FIXME handle interleaved nodes. If you fix this here, please fix
932         // amdk8, too.
933         mmio_basek = pci_tolm >> 10;
934         /* Round mmio_basek to something the processor can support */
935         mmio_basek &= ~((1 << 6) -1);
936
937         // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
938         // MMIO hole. If you fix this here, please fix amdk8, too.
939         /* Round the mmio hole to 64M */
940         mmio_basek &= ~((64*1024) - 1);
941
942 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
943 /* if the hw mem hole is already set in raminit stage, here we will compare
944  * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
945  * use hole_basek as mmio_basek and we don't need to reset hole.
946  * otherwise We reset the hole to the mmio_basek
947  */
948
949         mem_hole = get_hw_mem_hole_info();
950
951         // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
952         if ((mem_hole.node_id !=  -1) && (mmio_basek > mem_hole.hole_startk)) {
953                 mmio_basek = mem_hole.hole_startk;
954                 reset_memhole = 0;
955         }
956
957         #if CONFIG_AMDMCT == 0
958         //mmio_basek = 3*1024*1024; // for debug to meet boundary
959
960         if(reset_memhole) {
961                 if(mem_hole.node_id!=-1) {
962                 /* We need to select CONFIG_HW_MEM_HOLE_SIZEK for raminit, it can not
963                     make hole_startk to some basek too!
964                    We need to reset our Mem Hole, because We want more big HOLE
965                     than we already set
966                    Before that We need to disable mem hole at first, becase
967                     memhole could already be set on i+1 instead
968                  */
969                         disable_hoist_memory(mem_hole.hole_startk, mem_hole.node_id);
970                 }
971
972         #if CONFIG_HW_MEM_HOLE_SIZE_AUTO_INC == 1
973                 // We need to double check if the mmio_basek is valid for hole
974                 // setting, if it is equal to basek, we need to decrease it some
975                 resource_t basek_pri;
976                 for (i = 0; i < sysconf.nodes; i++) {
977                         struct dram_base_mask_t d;
978                         resource_t basek;
979                         d = get_dram_base_mask(i);
980
981                         if(!(d.mask &1)) continue;
982
983                         basek = ((resource_t)(d.base & 0x1fffff00)) << 9;
984                         if(mmio_basek == (u32)basek) {
985                                 mmio_basek -= (uin32_t)(basek - basek_pri); // increase mem hole size to make sure it is on middle of pri node
986                                 break;
987                         }
988                         basek_pri = basek;
989                 }
990         #endif
991         }
992         #endif
993
994
995 #endif
996
997         idx = 0x10;
998         for(i = 0; i < sysconf.nodes; i++) {
999                 struct dram_base_mask_t d;
1000                 resource_t basek, limitk, sizek; // 4 1T
1001                 d = get_dram_base_mask(i);
1002
1003                 if(!(d.mask & 1)) continue;
1004                 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
1005                 limitk = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9 ;
1006                 sizek = limitk - basek;
1007
1008                 /* see if we need a hole from 0xa0000 to 0xbffff */
1009                 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
1010                         ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
1011                         idx += 0x10;
1012                         basek = (8*64)+(16*16);
1013                         sizek = limitk - ((8*64)+(16*16));
1014
1015                 }
1016
1017 //              printk(BIOS_DEBUG, "node %d : mmio_basek=%08x, basek=%08x, limitk=%08x\n", i, mmio_basek, basek, limitk);
1018
1019                 /* split the region to accomodate pci memory space */
1020                 if ( (basek < 4*1024*1024 ) && (limitk > mmio_basek) ) {
1021                         if (basek <= mmio_basek) {
1022                                 unsigned pre_sizek;
1023                                 pre_sizek = mmio_basek - basek;
1024                                 if(pre_sizek>0) {
1025                                         ram_resource(dev, (idx | i), basek, pre_sizek);
1026                                         idx += 0x10;
1027                                         sizek -= pre_sizek;
1028 #if CONFIG_WRITE_HIGH_TABLES==1
1029                                         if (high_tables_base==0) {
1030                                         /* Leave some space for ACPI, PIRQ and MP tables */
1031 #if CONFIG_GFXUMA == 1
1032                                                 high_tables_base = uma_memory_base - (HIGH_TABLES_SIZE * 1024);
1033 #else
1034                                                 high_tables_base = (mmio_basek - HIGH_TABLES_SIZE) * 1024;
1035 #endif
1036                                                 high_tables_size = HIGH_TABLES_SIZE * 1024;
1037                                                 printk(BIOS_DEBUG, " split: %dK table at =%08llx\n", HIGH_TABLES_SIZE,
1038                                                              high_tables_base);
1039                                         }
1040 #endif
1041                                 }
1042                                 #if CONFIG_AMDMCT == 0
1043                                 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
1044                                 if(reset_memhole) {
1045                                         struct sys_info *sysinfox = (struct sys_info *)((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE); // in RAM
1046                                         struct mem_info *meminfo;
1047                                         meminfo = &sysinfox->meminfo[i];
1048                                         sizek += hoist_memory(mmio_basek,i, get_one_DCT(meminfo), sysconf.nodes);
1049                                 }
1050                                 #endif
1051                                 #endif
1052
1053                                 basek = mmio_basek;
1054                         }
1055                         if ((basek + sizek) <= 4*1024*1024) {
1056                                 sizek = 0;
1057                         }
1058                         else {
1059                                 basek = 4*1024*1024;
1060                                 sizek -= (4*1024*1024 - mmio_basek);
1061                         }
1062                 }
1063                 ram_resource(dev, (idx | i), basek, sizek);
1064                 idx += 0x10;
1065 #if CONFIG_WRITE_HIGH_TABLES==1
1066                 printk(BIOS_DEBUG, "%d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
1067                              i, mmio_basek, basek, limitk);
1068                 if (high_tables_base==0) {
1069                 /* Leave some space for ACPI, PIRQ and MP tables */
1070 #if CONFIG_GFXUMA == 1
1071                         high_tables_base = uma_memory_base - (HIGH_TABLES_SIZE * 1024);
1072 #else
1073                         high_tables_base = (limitk - HIGH_TABLES_SIZE) * 1024;
1074 #endif
1075                         high_tables_size = HIGH_TABLES_SIZE * 1024;
1076                 }
1077 #endif
1078         }
1079
1080 #if CONFIG_GFXUMA == 1
1081         add_uma_resource(dev, 7);
1082 #endif
1083
1084         for(link = dev->link_list; link; link = link->next) {
1085                 if (link->children) {
1086                         assign_resources(link);
1087                 }
1088         }
1089 }
1090
1091 static u32 amdfam10_domain_scan_bus(device_t dev, u32 max)
1092 {
1093         u32 reg;
1094         int i;
1095         struct bus *link;
1096         /* Unmap all of the HT chains */
1097         for(reg = 0xe0; reg <= 0xec; reg += 4) {
1098                 f1_write_config32(reg, 0);
1099         }
1100 #if CONFIG_EXT_CONF_SUPPORT == 1
1101         // all nodes
1102         for(i = 0; i< sysconf.nodes; i++) {
1103                 int index;
1104                 for(index = 0; index < 64; index++) {
1105                         pci_write_config32(__f1_dev[i], 0x110, index | (6<<28));
1106                         pci_write_config32(__f1_dev[i], 0x114, 0);
1107                 }
1108
1109         }
1110 #endif
1111
1112
1113         for(link = dev->link_list; link; link = link->next) {
1114                 max = pci_scan_bus(link, PCI_DEVFN(CONFIG_CDB, 0), 0xff, max);
1115         }
1116
1117         /* Tune the hypertransport transaction for best performance.
1118          * Including enabling relaxed ordering if it is safe.
1119          */
1120         get_fx_devs();
1121         for(i = 0; i < fx_devs; i++) {
1122                 device_t f0_dev;
1123                 f0_dev = __f0_dev[i];
1124                 if (f0_dev && f0_dev->enabled) {
1125                         u32 httc;
1126                         httc = pci_read_config32(f0_dev, HT_TRANSACTION_CONTROL);
1127                         httc &= ~HTTC_RSP_PASS_PW;
1128                         if (!dev->link_list->disable_relaxed_ordering) {
1129                                 httc |= HTTC_RSP_PASS_PW;
1130                         }
1131                         printk(BIOS_SPEW, "%s passpw: %s\n",
1132                                 dev_path(dev),
1133                                 (!dev->link_list->disable_relaxed_ordering)?
1134                                 "enabled":"disabled");
1135                         pci_write_config32(f0_dev, HT_TRANSACTION_CONTROL, httc);
1136                 }
1137         }
1138         return max;
1139 }
1140
1141 static struct device_operations pci_domain_ops = {
1142         .read_resources   = amdfam10_domain_read_resources,
1143         .set_resources    = amdfam10_domain_set_resources,
1144         .enable_resources = NULL,
1145         .init             = NULL,
1146         .scan_bus         = amdfam10_domain_scan_bus,
1147 #if CONFIG_MMCONF_SUPPORT_DEFAULT
1148         .ops_pci_bus      = &pci_ops_mmconf,
1149 #else
1150         .ops_pci_bus      = &pci_cf8_conf1,
1151 #endif
1152 };
1153
1154 static void sysconf_init(device_t dev) // first node
1155 {
1156         sysconf.sblk = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1157         sysconf.segbit = 0;
1158         sysconf.ht_c_num = 0;
1159
1160         unsigned ht_c_index;
1161
1162         for(ht_c_index=0; ht_c_index<32; ht_c_index++) {
1163                 sysconf.ht_c_conf_bus[ht_c_index] = 0;
1164         }
1165
1166         sysconf.nodes = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1;
1167 #if CONFIG_MAX_PHYSICAL_CPUS > 8
1168         sysconf.nodes += (((pci_read_config32(dev, 0x160)>>4) & 7)<<3);
1169 #endif
1170
1171         sysconf.enabled_apic_ext_id = 0;
1172         sysconf.lift_bsp_apicid = 0;
1173
1174         /* Find the bootstrap processors apicid */
1175         sysconf.bsp_apicid = lapicid();
1176         sysconf.apicid_offset = sysconf.bsp_apicid;
1177
1178 #if (CONFIG_ENABLE_APIC_EXT_ID == 1)
1179         if (pci_read_config32(dev, 0x68) & (HTTC_APIC_EXT_ID|HTTC_APIC_EXT_BRD_CST))
1180         {
1181                 sysconf.enabled_apic_ext_id = 1;
1182         }
1183         #if (CONFIG_APIC_ID_OFFSET>0)
1184         if(sysconf.enabled_apic_ext_id) {
1185                 if(sysconf.bsp_apicid == 0) {
1186                         /* bsp apic id is not changed */
1187                         sysconf.apicid_offset = CONFIG_APIC_ID_OFFSET;
1188                 } else {
1189                         sysconf.lift_bsp_apicid = 1;
1190                 }
1191         }
1192         #endif
1193 #endif
1194 }
1195
1196 static void add_more_links(device_t dev, unsigned total_links)
1197 {
1198         struct bus *link, *last = NULL;
1199         int link_num;
1200
1201         for (link = dev->link_list; link; link = link->next)
1202                 last = link;
1203
1204         if (last) {
1205                 int links = total_links - last->link_num;
1206                 link_num = last->link_num;
1207                 if (links > 0) {
1208                         link = malloc(links*sizeof(*link));
1209                         if (!link)
1210                                 die("Couldn't allocate more links!\n");
1211                         memset(link, 0, links*sizeof(*link));
1212                         last->next = link;
1213                 }
1214         }
1215         else {
1216                 link_num = -1;
1217                 link = malloc(total_links*sizeof(*link));
1218                 memset(link, 0, total_links*sizeof(*link));
1219                 dev->link_list = link;
1220         }
1221
1222         for (link_num = link_num + 1; link_num < total_links; link_num++) {
1223                 link->link_num = link_num;
1224                 link->dev = dev;
1225                 link->next = link + 1;
1226                 last = link;
1227                 link = link->next;
1228         }
1229         last->next = NULL;
1230 }
1231
1232 static u32 cpu_bus_scan(device_t dev, u32 max)
1233 {
1234         struct bus *cpu_bus;
1235         device_t dev_mc;
1236 #if CONFIG_CBB
1237         device_t pci_domain;
1238 #endif
1239         int i,j;
1240         int nodes;
1241         unsigned nb_cfg_54;
1242         unsigned siblings;
1243         int cores_found;
1244         int disable_siblings;
1245         unsigned ApicIdCoreIdSize;
1246
1247         nb_cfg_54 = 0;
1248         ApicIdCoreIdSize = (cpuid_ecx(0x80000008)>>12 & 0xf);
1249         if(ApicIdCoreIdSize) {
1250                 siblings = (1<<ApicIdCoreIdSize)-1;
1251         } else {
1252                 siblings = 3; //quad core
1253         }
1254
1255         disable_siblings = !CONFIG_LOGICAL_CPUS;
1256 #if CONFIG_LOGICAL_CPUS == 1
1257         get_option(&disable_siblings, "multi_core");
1258 #endif
1259
1260         // How can I get the nb_cfg_54 of every node's nb_cfg_54 in bsp???
1261         nb_cfg_54 = read_nb_cfg_54();
1262
1263 #if CONFIG_CBB
1264         dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
1265         if(dev_mc && dev_mc->bus) {
1266                 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
1267                 pci_domain = dev_mc->bus->dev;
1268                 if(pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
1269                         printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
1270                         dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
1271                         printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
1272
1273                 } else {
1274                         printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
1275                 }
1276                 printk(BIOS_DEBUG, "\n");
1277         }
1278         dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1279         if(!dev_mc) {
1280                 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
1281                 if (dev_mc && dev_mc->bus) {
1282                         printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
1283                         pci_domain = dev_mc->bus->dev;
1284                         if(pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
1285                                 if((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
1286                                         printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
1287                                         dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
1288                                         printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
1289                                         while(dev_mc){
1290                                                 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
1291                                                 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
1292                                                 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
1293                                                 dev_mc = dev_mc->sibling;
1294                                         }
1295                                 }
1296                         }
1297                 }
1298         }
1299
1300 #endif
1301
1302         dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1303         if (!dev_mc) {
1304                 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
1305                 die("");
1306         }
1307
1308         sysconf_init(dev_mc);
1309
1310         nodes = sysconf.nodes;
1311
1312 #if CONFIG_CBB && (NODE_NUMS > 32)
1313         if(nodes>32) { // need to put node 32 to node 63 to bus 0xfe
1314                 if(pci_domain->link_list && !pci_domain->link_list->next) {
1315                         struct bus *new_link = new_link(pci_domain);
1316                         pci_domain->link_list->next = new_link;
1317                         new_link->link_num = 1;
1318                         new_link->dev = pci_domain;
1319                         new_link->children = 0;
1320                         printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
1321                 }
1322                 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
1323         }
1324 #endif
1325         /* Find which cpus are present */
1326         cpu_bus = dev->link_list;
1327         for(i = 0; i < nodes; i++) {
1328                 device_t cdb_dev, cpu;
1329                 struct device_path cpu_path;
1330                 unsigned busn, devn;
1331                 struct bus *pbus;
1332
1333                 busn = CONFIG_CBB;
1334                 devn = CONFIG_CDB+i;
1335                 pbus = dev_mc->bus;
1336 #if CONFIG_CBB && (NODE_NUMS > 32)
1337                 if(i>=32) {
1338                         busn--;
1339                         devn-=32;
1340                         pbus = pci_domain->link_list->next);
1341                 }
1342 #endif
1343
1344                 /* Find the cpu's pci device */
1345                 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1346                 if (!cdb_dev) {
1347                         /* If I am probing things in a weird order
1348                          * ensure all of the cpu's pci devices are found.
1349                          */
1350                         int fn;
1351                         for(fn = 0; fn <= 5; fn++) { //FBDIMM?
1352                                 cdb_dev = pci_probe_dev(NULL, pbus,
1353                                         PCI_DEVFN(devn, fn));
1354                         }
1355                         cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1356                 }
1357                 if (cdb_dev) {
1358                         /* Ok, We need to set the links for that device.
1359                          * otherwise the device under it will not be scanned
1360                          */
1361                         int linknum;
1362 #if CONFIG_HT3_SUPPORT==1
1363                         linknum = 8;
1364 #else
1365                         linknum = 4;
1366 #endif
1367                         add_more_links(cdb_dev, linknum);
1368                 }
1369
1370                 cores_found = 0; // one core
1371                 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1372                 if (cdb_dev && cdb_dev->enabled) {
1373                         j = pci_read_config32(cdb_dev, 0xe8);
1374                         cores_found = (j >> 12) & 3; // dev is func 3
1375                         if (siblings > 3)
1376                                 cores_found |= (j >> 13) & 4;
1377                         printk(BIOS_DEBUG, "  %s siblings=%d\n", dev_path(cdb_dev), cores_found);
1378                 }
1379
1380                 u32 jj;
1381                 if(disable_siblings) {
1382                         jj = 0;
1383                 } else
1384                 {
1385                         jj = cores_found;
1386                 }
1387
1388                 for (j = 0; j <=jj; j++ ) {
1389
1390                         /* Build the cpu device path */
1391                         cpu_path.type = DEVICE_PATH_APIC;
1392                         cpu_path.apic.apic_id = i * (nb_cfg_54?(siblings+1):1) + j * (nb_cfg_54?1:64); // ?
1393
1394                         /* See if I can find the cpu */
1395                         cpu = find_dev_path(cpu_bus, &cpu_path);
1396
1397                         /* Enable the cpu if I have the processor */
1398                         if (cdb_dev && cdb_dev->enabled) {
1399                                 if (!cpu) {
1400                                         cpu = alloc_dev(cpu_bus, &cpu_path);
1401                                 }
1402                                 if (cpu) {
1403                                         cpu->enabled = 1;
1404                                 }
1405                         }
1406
1407                         /* Disable the cpu if I don't have the processor */
1408                         if (cpu && (!cdb_dev || !cdb_dev->enabled)) {
1409                                 cpu->enabled = 0;
1410                         }
1411
1412                         /* Report what I have done */
1413                         if (cpu) {
1414                                 cpu->path.apic.node_id = i;
1415                                 cpu->path.apic.core_id = j;
1416         #if (CONFIG_ENABLE_APIC_EXT_ID == 1) && (CONFIG_APIC_ID_OFFSET>0)
1417                                 if(sysconf.enabled_apic_ext_id) {
1418                                         if(sysconf.lift_bsp_apicid) {
1419                                                 cpu->path.apic.apic_id += sysconf.apicid_offset;
1420                                         } else
1421                                         {
1422                                                 if (cpu->path.apic.apic_id != 0)
1423                                                         cpu->path.apic.apic_id += sysconf.apicid_offset;
1424                                         }
1425                                 }
1426         #endif
1427                                 printk(BIOS_DEBUG, "CPU: %s %s\n",
1428                                         dev_path(cpu), cpu->enabled?"enabled":"disabled");
1429                         }
1430
1431                 } //j
1432         }
1433         return max;
1434 }
1435
1436 static void cpu_bus_init(device_t dev)
1437 {
1438         initialize_cpus(dev->link_list);
1439 }
1440
1441 static void cpu_bus_noop(device_t dev)
1442 {
1443 }
1444
1445 static void cpu_bus_read_resources(device_t dev)
1446 {
1447 #if CONFIG_MMCONF_SUPPORT
1448         struct resource *resource = new_resource(dev, 0xc0010058);
1449         resource->base = CONFIG_MMCONF_BASE_ADDRESS;
1450         resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096*256;
1451         resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
1452                 IORESOURCE_FIXED | IORESOURCE_STORED |  IORESOURCE_ASSIGNED;
1453 #endif
1454 }
1455
1456 static void cpu_bus_set_resources(device_t dev)
1457 {
1458         struct resource *resource = find_resource(dev, 0xc0010058);
1459         if (resource) {
1460                 report_resource_stored(dev, resource, " <mmconfig>");
1461         }
1462         pci_dev_set_resources(dev);
1463 }
1464
1465 static struct device_operations cpu_bus_ops = {
1466         .read_resources   = cpu_bus_read_resources,
1467         .set_resources    = cpu_bus_set_resources,
1468         .enable_resources = cpu_bus_noop,
1469         .init             = cpu_bus_init,
1470         .scan_bus         = cpu_bus_scan,
1471 };
1472
1473 static void root_complex_enable_dev(struct device *dev)
1474 {
1475         /* Set the operations if it is a special bus type */
1476         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
1477                 dev->ops = &pci_domain_ops;
1478         }
1479         else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
1480                 dev->ops = &cpu_bus_ops;
1481         }
1482 }
1483
1484 struct chip_operations northbridge_amd_amdfam10_root_complex_ops = {
1485         CHIP_NAME("AMD FAM10 Root Complex")
1486         .enable_dev = root_complex_enable_dev,
1487 };