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