enable apic ext id
[coreboot.git] / src / northbridge / amd / amdk8 / northbridge.c
1 #include <console/console.h>
2 #include <arch/io.h>
3 #include <stdint.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ids.h>
7 #include <device/hypertransport.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <bitops.h>
11 #include <cpu/cpu.h>
12 #include "chip.h"
13 #include "root_complex/chip.h"
14 #include "northbridge.h"
15 #include "amdk8.h"
16
17 #define FX_DEVS 8
18 static device_t __f0_dev[FX_DEVS];
19 static device_t __f1_dev[FX_DEVS];
20
21 #if 0
22 static void debug_fx_devs(void)
23 {
24         int i;
25         for (i = 0; i < FX_DEVS; i++) {
26                 device_t dev;
27                 dev = __f0_dev[i];
28                 if (dev) {
29                         printk_debug("__f0_dev[%d]: %s bus: %p\n",
30                                 i, dev_path(dev), dev->bus);
31                 }
32                 dev = __f1_dev[i];
33                 if (dev) {
34                         printk_debug("__f1_dev[%d]: %s bus: %p\n",
35                                 i, dev_path(dev), dev->bus);
36                 }
37         }
38 }
39 #endif
40
41 static void get_fx_devs(void)
42 {
43         int i;
44         if (__f1_dev[0]) {
45                 return;
46         }
47         for (i = 0; i < 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         }
51         if (!__f1_dev[0]) {
52                 die("Cannot find 0:0x18.1\n");
53         }
54 }
55
56 static uint32_t f1_read_config32(unsigned reg)
57 {
58         get_fx_devs();
59         return pci_read_config32(__f1_dev[0], reg);
60 }
61
62 static void f1_write_config32(unsigned reg, uint32_t value)
63 {
64         int i;
65         get_fx_devs();
66         for (i = 0; i < FX_DEVS; i++) {
67                 device_t dev;
68                 dev = __f1_dev[i];
69                 if (dev && dev->enabled) {
70                         pci_write_config32(dev, reg, value);
71                 }
72         }
73 }
74
75 static unsigned int amdk8_nodeid(device_t dev)
76 {
77         return (dev->path.u.pci.devfn >> 3) - 0x18;
78 }
79
80 static unsigned int amdk8_scan_chains(device_t dev, unsigned int max)
81 {
82         unsigned nodeid;
83         unsigned link;
84         nodeid = amdk8_nodeid(dev);
85 #if 0
86         printk_debug("%s amdk8_scan_chains max: %d starting...\n", 
87                      dev_path(dev), max);
88 #endif
89         for (link = 0; link < dev->links; link++) {
90                 uint32_t link_type;
91                 uint32_t busses, config_busses;
92                 unsigned free_reg, config_reg;
93                 dev->link[link].cap = 0x80 + (link *0x20);
94                 do {
95                         link_type = pci_read_config32(dev, dev->link[link].cap + 0x18);
96                 } while(link_type & ConnectionPending);
97                 if (!(link_type & LinkConnected)) {
98                         continue;
99                 }
100                 do {
101                         link_type = pci_read_config32(dev, dev->link[link].cap + 0x18);
102                 } while(!(link_type & InitComplete));
103                 if (!(link_type & NonCoherent)) {
104                         continue;
105                 }
106                 /* See if there is an available configuration space mapping
107                  * register in function 1. */
108                 free_reg = 0;
109                 for (config_reg = 0xe0; config_reg <= 0xec; config_reg += 4) {
110                         uint32_t config;
111                         config = f1_read_config32(config_reg);
112                         if (!free_reg && ((config & 3) == 0)) {
113                                 free_reg = config_reg;
114                                 continue;
115                         }
116                         if (((config & 3) == 3) && 
117                             (((config >> 4) & 7) == nodeid) &&
118                             (((config >> 8) & 3) == link)) {
119                                 break;
120                         }
121                 }
122                 if (free_reg && (config_reg > 0xec)) {
123                         config_reg = free_reg;
124                 }
125                 /* If we can't find an available configuration space mapping
126                  * register skip this bus */
127                 if (config_reg > 0xec) {
128                         continue;
129                 }
130
131                 /* Set up the primary, secondary and subordinate bus numbers.
132                  * We have no idea how many busses are behind this bridge yet,
133                  * so we set the subordinate bus number to 0xff for the moment.
134                  */
135                 dev->link[link].secondary = ++max;
136                 dev->link[link].subordinate = 0xff;
137
138                 /* Read the existing primary/secondary/subordinate bus
139                  * number configuration.
140                  */
141                 busses = pci_read_config32(dev, dev->link[link].cap + 0x14);
142                 config_busses = f1_read_config32(config_reg);
143
144                 /* Configure the bus numbers for this bridge: the configuration
145                  * transactions will not be propagates by the bridge if it is
146                  * not correctly configured
147                  */
148                 busses &= 0xff000000;
149                 busses |= (((unsigned int)(dev->bus->secondary) << 0) |
150                            ((unsigned int)(dev->link[link].secondary) << 8) |
151                            ((unsigned int)(dev->link[link].subordinate) << 16));
152                 pci_write_config32(dev, dev->link[link].cap + 0x14, busses);
153
154                 config_busses &= 0x000fc88;
155                 config_busses |= 
156                         (3 << 0) |  /* rw enable, no device compare */
157                         (( nodeid & 7) << 4) | 
158                         (( link & 3 ) << 8) |  
159                         ((dev->link[link].secondary) << 16) |
160                         ((dev->link[link].subordinate) << 24);
161                 f1_write_config32(config_reg, config_busses);
162
163 #if 0
164                 printk_debug("%s Hyper transport scan link: %d max: %d\n", 
165                         dev_path(dev), link, max);
166 #endif
167                 /* Now we can scan all of the subordinate busses i.e. the
168                  * chain on the hypertranport link */
169                 max = hypertransport_scan_chain(&dev->link[link], max);
170
171 #if 0
172                 printk_debug("%s Hyper transport scan link: %d new max: %d\n",
173                         dev_path(dev), link, max);
174 #endif
175
176                 /* We know the number of busses behind this bridge.  Set the
177                  * subordinate bus number to it's real value
178                  */
179                 dev->link[link].subordinate = max;
180                 busses = (busses & 0xff00ffff) |
181                         ((unsigned int) (dev->link[link].subordinate) << 16);
182                 pci_write_config32(dev, dev->link[link].cap + 0x14, busses);
183
184                 config_busses = (config_busses & 0x00ffffff) |
185                         (dev->link[link].subordinate << 24);
186                 f1_write_config32(config_reg, config_busses);
187 #if 0
188                 printk_debug("%s Hypertransport scan link: %d done\n",
189                         dev_path(dev), link);
190 #endif
191         }
192 #if 0
193         printk_debug("%s amdk8_scan_chains max: %d done\n", 
194                 dev_path(dev), max);
195 #endif
196         return max;
197 }
198
199 static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
200                        unsigned goal_link)
201 {
202         struct resource *res;
203         unsigned nodeid, link;
204         int result;
205         res = 0;
206         for (nodeid = 0; !res && (nodeid < 8); nodeid++) {
207                 device_t dev;
208                 dev = __f0_dev[nodeid];
209                 for (link = 0; !res && (link < 3); link++) {
210                         res = probe_resource(dev, 0x100 + (reg | link));
211                 }
212         }
213         result = 2;
214         if (res) {
215                 result = 0;
216                 if ((goal_link == (link - 1)) && 
217                     (goal_nodeid == (nodeid - 1)) &&
218                     (res->flags <= 1)) {
219                         result = 1;
220                 }
221         }
222 #if 0
223         printk_debug("reg: %02x result: %d gnodeid: %u glink: %u nodeid: %u link: %u\n",
224                      reg, result, goal_nodeid, goal_link, nodeid, link);
225 #endif
226         return result;
227 }
228
229 static struct resource *amdk8_find_iopair(device_t dev, unsigned nodeid, unsigned link)
230 {
231         struct resource *resource;
232         unsigned free_reg, reg;
233         resource = 0;
234         free_reg = 0;
235         for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
236                 int result;
237                 result = reg_useable(reg, dev, nodeid, link);
238                 if (result == 1) {
239                         /* I have been allocated this one */
240                         break;
241                 }
242                 else if (result > 1) {
243                         /* I have a free register pair */
244                         free_reg = reg;
245                 }
246         }
247         if (reg > 0xd8) {
248                 reg = free_reg;
249         }
250         if (reg > 0) {
251                 resource = new_resource(dev, 0x100 + (reg | link));
252         }
253         return resource;
254 }
255
256 static struct resource *amdk8_find_mempair(device_t dev, unsigned nodeid, unsigned link)
257 {
258         struct resource *resource;
259         unsigned free_reg, reg;
260         resource = 0;
261         free_reg = 0;
262         for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
263                 int result;
264                 result = reg_useable(reg, dev, nodeid, link);
265                 if (result == 1) {
266                         /* I have been allocated this one */
267                         break;
268                 }
269                 else if (result > 1) {
270                         /* I have a free register pair */
271                         free_reg = reg;
272                 }
273         }
274         if (reg > 0xb8) {
275                 reg = free_reg;
276         }
277         if (reg > 0) {
278                 resource = new_resource(dev, 0x100 + (reg | link));
279         }
280         return resource;
281 }
282
283 static void amdk8_link_read_bases(device_t dev, unsigned nodeid, unsigned link)
284 {
285         struct resource *resource;
286         
287         /* Initialize the io space constraints on the current bus */
288         resource =  amdk8_find_iopair(dev, nodeid, link);
289         if (resource) {
290                 resource->base  = 0;
291                 resource->size  = 0;
292                 resource->align = log2(HT_IO_HOST_ALIGN);
293                 resource->gran  = log2(HT_IO_HOST_ALIGN);
294                 resource->limit = 0xffffUL;
295                 resource->flags = IORESOURCE_IO;
296                 compute_allocate_resource(&dev->link[link], resource, 
297                         IORESOURCE_IO, IORESOURCE_IO);
298         }
299
300         /* Initialize the prefetchable memory constraints on the current bus */
301         resource = amdk8_find_mempair(dev, nodeid, link);
302         if (resource) {
303                 resource->base  = 0;
304                 resource->size  = 0;
305                 resource->align = log2(HT_MEM_HOST_ALIGN);
306                 resource->gran  = log2(HT_MEM_HOST_ALIGN);
307                 resource->limit = 0xffffffffffULL;
308                 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
309                 compute_allocate_resource(&dev->link[link], resource, 
310                         IORESOURCE_MEM | IORESOURCE_PREFETCH, 
311                         IORESOURCE_MEM | IORESOURCE_PREFETCH);
312         }
313
314         /* Initialize the memory constraints on the current bus */
315         resource = amdk8_find_mempair(dev, nodeid, link);
316         if (resource) {
317                 resource->base  = 0;
318                 resource->size  = 0;
319                 resource->align = log2(HT_MEM_HOST_ALIGN);
320                 resource->gran  = log2(HT_MEM_HOST_ALIGN);
321                 resource->limit = 0xffffffffffULL;
322                 resource->flags = IORESOURCE_MEM;
323                 compute_allocate_resource(&dev->link[link], resource, 
324                         IORESOURCE_MEM | IORESOURCE_PREFETCH, 
325                         IORESOURCE_MEM);
326         }
327 }
328
329 static void amdk8_read_resources(device_t dev)
330 {
331         unsigned nodeid, link;
332         nodeid = amdk8_nodeid(dev);
333         for (link = 0; link < dev->links; link++) {
334                 if (dev->link[link].children) {
335                         amdk8_link_read_bases(dev, nodeid, link);
336                 }
337         }
338 }
339
340 static void amdk8_set_resource(device_t dev, struct resource *resource, unsigned nodeid)
341 {
342         resource_t rbase, rend;
343         unsigned reg, link;
344         char buf[50];
345
346         /* Make certain the resource has actually been set */
347         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
348                 return;
349         }
350
351         /* If I have already stored this resource don't worry about it */
352         if (resource->flags & IORESOURCE_STORED) {
353                 return;
354         }
355         
356         /* Only handle PCI memory and IO resources */
357         if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
358                 return;
359
360         /* Ensure I am actually looking at a resource of function 1 */
361         if (resource->index < 0x100) {
362                 return;
363         }
364         /* Get the base address */
365         rbase = resource->base;
366         
367         /* Get the limit (rounded up) */
368         rend  = resource_end(resource);
369
370         /* Get the register and link */
371         reg  = resource->index & 0xfc;
372         link = resource->index & 3;
373
374         if (resource->flags & IORESOURCE_IO) {
375                 uint32_t base, limit;
376                 compute_allocate_resource(&dev->link[link], resource,
377                         IORESOURCE_IO, IORESOURCE_IO);
378                 base  = f1_read_config32(reg);
379                 limit = f1_read_config32(reg + 0x4);
380                 base  &= 0xfe000fcc;
381                 base  |= rbase  & 0x01fff000;
382                 base  |= 3;
383                 limit &= 0xfe000fc8;
384                 limit |= rend & 0x01fff000;
385                 limit |= (link & 3) << 4;
386                 limit |= (nodeid & 7);
387
388                 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
389                         base |= PCI_IO_BASE_VGA_EN;
390                 }
391                 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
392                         base |= PCI_IO_BASE_NO_ISA;
393                 }
394                 
395                 f1_write_config32(reg + 0x4, limit);
396                 f1_write_config32(reg, base);
397         }
398         else if (resource->flags & IORESOURCE_MEM) {
399                 uint32_t base, limit;
400                 compute_allocate_resource(&dev->link[link], resource,
401                         IORESOURCE_MEM | IORESOURCE_PREFETCH,
402                         resource->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH));
403                 base  = f1_read_config32(reg);
404                 limit = f1_read_config32(reg + 0x4);
405                 base  &= 0x000000f0;
406                 base  |= (rbase >> 8) & 0xffffff00;
407                 base  |= 3;
408                 limit &= 0x00000048;
409                 limit |= (rend >> 8) & 0xffffff00;
410                 limit |= (link & 3) << 4;
411                 limit |= (nodeid & 7);
412                 f1_write_config32(reg + 0x4, limit);
413                 f1_write_config32(reg, base);
414         }
415         resource->flags |= IORESOURCE_STORED;
416         sprintf(buf, " <node %d link %d>",
417                 nodeid, link);
418         report_resource_stored(dev, resource, buf);
419 }
420
421 /**
422  *
423  * I tried to reuse the resource allocation code in amdk8_set_resource()
424  * but it is too diffcult to deal with the resource allocation magic.
425  */
426 static void amdk8_create_vga_resource(device_t dev, unsigned nodeid)
427 {
428         struct resource *resource;
429         unsigned link;
430         uint32_t base, limit;
431         unsigned reg;
432
433         /* find out which link the VGA card is connected,
434          * we only deal with the 'first' vga card */
435         for (link = 0; link < dev->links; link++) {
436                 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
437                         break;
438                 }
439         }
440
441         /* no VGA card installed */
442         if (link == dev->links)
443                 return;
444
445         /* allocate a temp resrouce for legacy VGA buffer */
446         resource = amdk8_find_mempair(dev, nodeid, link);
447         resource->base = 0xa0000;
448         resource->size = 0x20000;
449
450         /* write the resource to the hardware */
451         reg  = resource->index & 0xfc;
452         base  = f1_read_config32(reg);
453         limit = f1_read_config32(reg + 0x4);
454         base  &= 0x000000f0;
455         base  |= (resource->base >> 8) & 0xffffff00;
456         base  |= 3;
457         limit &= 0x00000048;
458         limit |= ((resource->base + resource->size) >> 8) & 0xffffff00;
459         limit |= (resource->index & 3) << 4;
460         limit |= (nodeid & 7);
461         f1_write_config32(reg + 0x4, limit);
462         f1_write_config32(reg, base);
463
464         /* release the temp resource */
465         resource->flags = 0;
466 }
467
468 static void amdk8_set_resources(device_t dev)
469 {
470         unsigned nodeid, link;
471         int i;
472
473         /* Find the nodeid */
474         nodeid = amdk8_nodeid(dev);
475
476         amdk8_create_vga_resource(dev, nodeid);
477         
478         /* Set each resource we have found */
479         for (i = 0; i < dev->resources; i++) {
480                 amdk8_set_resource(dev, &dev->resource[i], nodeid);
481         }
482
483         for (link = 0; link < dev->links; link++) {
484                 struct bus *bus;
485                 bus = &dev->link[link];
486                 if (bus->children) {
487                         assign_resources(bus);
488                 }
489         }
490 }
491
492 static void amdk8_enable_resources(device_t dev)
493 {
494         pci_dev_enable_resources(dev);
495         enable_childrens_resources(dev);
496 }
497
498 static void mcf0_control_init(struct device *dev)
499 {
500         uint32_t cmd;
501
502 #if 0   
503         printk_debug("NB: Function 0 Misc Control.. ");
504 #endif
505 #if 1
506         /* improve latency and bandwith on HT */
507         cmd = pci_read_config32(dev, 0x68);
508         cmd &= 0xffff80ff;
509         cmd |= 0x00004800;
510         pci_write_config32(dev, 0x68, cmd );
511 #endif
512
513 #if 0   
514         /* over drive the ht port to 1000 Mhz */
515         cmd = pci_read_config32(dev, 0xa8);
516         cmd &= 0xfffff0ff;
517         cmd |= 0x00000600;
518         pci_write_config32(dev, 0xdc, cmd );
519 #endif  
520 #if 0
521         printk_debug("done.\n");
522 #endif
523 }
524
525 static struct device_operations northbridge_operations = {
526         .read_resources   = amdk8_read_resources,
527         .set_resources    = amdk8_set_resources,
528         .enable_resources = amdk8_enable_resources,
529         .init             = mcf0_control_init,
530         .scan_bus         = amdk8_scan_chains,
531         .enable           = 0,
532         .ops_pci          = 0,
533 };
534
535
536 static struct pci_driver mcf0_driver __pci_driver = {
537         .ops    = &northbridge_operations,
538         .vendor = PCI_VENDOR_ID_AMD,
539         .device = 0x1100,
540 };
541
542 #if CONFIG_CHIP_NAME == 1
543
544 struct chip_operations northbridge_amd_amdk8_ops = {
545         CHIP_NAME("AMD K8 Northbridge")
546         .enable_dev = 0,
547 };
548
549 #endif
550
551 static void pci_domain_read_resources(device_t dev)
552 {
553         struct resource *resource;
554         unsigned reg;
555
556         /* Find the already assigned resource pairs */
557         get_fx_devs();
558         for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
559                 uint32_t base, limit;
560                 base  = f1_read_config32(reg);
561                 limit = f1_read_config32(reg + 0x04);
562                 /* Is this register allocated? */
563                 if ((base & 3) != 0) {
564                         unsigned nodeid, link;
565                         device_t dev;
566                         nodeid = limit & 7;
567                         link   = (limit >> 4) & 3;
568                         dev = __f0_dev[nodeid];
569                         if (dev) {
570                                 /* Reserve the resource  */
571                                 struct resource *resource;
572                                 resource = new_resource(dev, 0x100 + (reg | link));
573                                 if (resource) {
574                                         resource->flags = 1;
575                                 }
576                         }
577                 }
578         }
579
580         /* Initialize the system wide io space constraints */
581         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
582         resource->base  = 0x400;
583         resource->limit = 0xffffUL;
584         resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
585
586         /* Initialize the system wide memory resources constraints */
587         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
588         resource->limit = 0xfcffffffffULL;
589         resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
590 }
591
592 static void ram_resource(device_t dev, unsigned long index,
593                          unsigned long basek, unsigned long sizek)
594 {
595         struct resource *resource;
596
597         if (!sizek) {
598                 return;
599         }
600         resource = new_resource(dev, index);
601         resource->base  = ((resource_t)basek) << 10;
602         resource->size  = ((resource_t)sizek) << 10;
603         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
604                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
605 }
606
607 static void tolm_test(void *gp, struct device *dev, struct resource *new)
608 {
609         struct resource **best_p = gp;
610         struct resource *best;
611         best = *best_p;
612         if (!best || (best->base > new->base)) {
613                 best = new;
614         }
615         *best_p = best;
616 }
617
618 static uint32_t find_pci_tolm(struct bus *bus)
619 {
620         struct resource *min;
621         uint32_t tolm;
622         min = 0;
623         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
624         tolm = 0xffffffffUL;
625         if (min && tolm > min->base) {
626                 tolm = min->base;
627         }
628         return tolm;
629 }
630
631 static void pci_domain_set_resources(device_t dev)
632 {
633         unsigned long mmio_basek;
634         uint32_t pci_tolm;
635         int i, idx;
636
637         pci_tolm = find_pci_tolm(&dev->link[0]);
638
639 #warning "FIXME handle interleaved nodes"
640         mmio_basek = pci_tolm >> 10;
641         /* Round mmio_basek to something the processor can support */
642         mmio_basek &= ~((1 << 6) -1);
643
644 #if 1
645 #warning "FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M MMIO hole"
646         /* Round the mmio hold to 64M */
647         mmio_basek &= ~((64*1024) - 1);
648 #endif
649
650         idx = 10;
651         for (i = 0; i < 8; i++) {
652                 uint32_t base, limit;
653                 unsigned basek, limitk, sizek;
654                 base  = f1_read_config32(0x40 + (i << 3));
655                 limit = f1_read_config32(0x44 + (i << 3));
656                 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
657                         continue;
658                 }
659                 basek = (base & 0xffff0000) >> 2;
660                 limitk = ((limit + 0x00010000) & 0xffff0000) >> 2;
661                 sizek = limitk - basek;
662
663                 /* see if we need a hole from 0xa0000 to 0xbffff */
664                 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
665                         ram_resource(dev, idx++, basek, ((8*64)+(8*16)) - basek);
666                         basek = (8*64)+(16*16);
667                         sizek = limitk - ((8*64)+(16*16));
668                         
669                 }
670
671                 
672                 /* See if I need to split the region to accomodate pci memory space */
673                 if ((basek < mmio_basek) && (limitk > mmio_basek)) {
674                         if (basek < mmio_basek) {
675                                 unsigned pre_sizek;
676                                 pre_sizek = mmio_basek - basek;
677                                 ram_resource(dev, idx++, basek, pre_sizek);
678                                 sizek -= pre_sizek;
679                                 basek = mmio_basek;
680                         }
681                         if ((basek + sizek) <= 4*1024*1024) {
682                                 sizek = 0;
683                         }
684                         else {
685                                 basek = 4*1024*1024;
686                                 sizek -= (4*1024*1024 - mmio_basek);
687                         }
688                 }
689                 ram_resource(dev, idx++, basek, sizek);
690         }
691         assign_resources(&dev->link[0]);
692 }
693
694 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
695 {
696         unsigned reg;
697         /* Unmap all of the HT chains */
698         for (reg = 0xe0; reg <= 0xec; reg += 4) {
699                 f1_write_config32(reg, 0);
700         }
701         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0x18, 0), 0xff, max);
702         return max;
703 }
704
705 static struct device_operations pci_domain_ops = {
706         .read_resources   = pci_domain_read_resources,
707         .set_resources    = pci_domain_set_resources,
708         .enable_resources = enable_childrens_resources,
709         .init             = 0,
710         .scan_bus         = pci_domain_scan_bus,
711         .ops_pci_bus      = &pci_cf8_conf1,
712 };
713
714 static unsigned int cpu_bus_scan(device_t dev, unsigned int max)
715 {
716         struct bus *cpu_bus;
717         int i;
718         int apic_id_offset = lapicid(); // bsp apicid
719
720         /* Find which cpus are present */
721         cpu_bus = &dev->link[0];
722         for (i = 0; i < 8; i++) {
723                 device_t dev, cpu;
724                 struct device_path cpu_path;
725
726                 /* Find the cpu's memory controller */
727                 dev = dev_find_slot(0, PCI_DEVFN(0x18 + i, 0));
728
729                 /* Build the cpu device path */
730                 cpu_path.type = DEVICE_PATH_APIC;
731                 cpu_path.u.apic.apic_id = i;
732
733                 /* See if I can find the cpu */
734                 cpu = find_dev_path(cpu_bus, &cpu_path);
735
736                 /* Enable the cpu if I have the processor */
737                 if (dev && dev->enabled) {
738                         if (!cpu) {
739                                 cpu = alloc_dev(cpu_bus, &cpu_path);
740                         }
741                         if (cpu) {
742                                 cpu->enabled = 1;
743                         }
744                 }
745                 
746                 /* Disable the cpu if I don't have the processor */
747                 if (cpu && (!dev || !dev->enabled)) {
748                         cpu->enabled = 0;
749                 }
750                 
751                 /* Report what I have done */
752                 if (cpu) {
753                         if(cpu->path.u.apic.apic_id<apic_id_offset) {
754                                 cpu->path.u.apic.apic_id += apic_id_offset;
755                         }  
756                         printk_debug("CPU: %s %s\n", dev_path(cpu),
757                                      cpu->enabled?"enabled":"disabled");
758                 }
759         }
760         return max;
761 }
762
763 static void cpu_bus_init(device_t dev)
764 {
765         initialize_cpus(&dev->link[0]);
766 }
767
768 static void cpu_bus_noop(device_t dev) 
769 {
770 }
771
772 static struct device_operations cpu_bus_ops = {
773         .read_resources   = cpu_bus_noop,
774         .set_resources    = cpu_bus_noop,
775         .enable_resources = cpu_bus_noop,
776         .init             = cpu_bus_init,
777         .scan_bus         = cpu_bus_scan,
778 };
779
780 static void root_complex_enable_dev(struct device *dev)
781 {
782         /* Set the operations if it is a special bus type */
783         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
784                 dev->ops = &pci_domain_ops;
785         }
786         else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
787                 dev->ops = &cpu_bus_ops;
788         }
789 }
790
791 struct chip_operations northbridge_amd_amdk8_root_complex_ops = {
792         CHIP_NAME("AMD K8 Root Complex")
793         .enable_dev = root_complex_enable_dev,
794 };