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