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