d1a322ca6c507eca01b7ad7ebb970fed13cd6468
[coreboot.git] / src / northbridge / amd / agesa / family15 / northbridge.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2012 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <console/console.h>
21 #include <arch/io.h>
22 #include <stdint.h>
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <device/pci_ids.h>
26 #include <device/hypertransport.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <bitops.h>
30 #include <cpu/cpu.h>
31 #include <cbmem.h>
32
33 #include <cpu/x86/lapic.h>
34
35 #include <Porting.h>
36 #include <AGESA.h>
37 #include <Options.h>
38 #include <Topology.h>
39 #include <cpu/amd/amdfam15.h>
40 #include <cpuRegisters.h>
41 #include "agesawrapper.h"
42 #include "root_complex/chip.h"
43 #include "northbridge.h"
44 #include "chip.h"
45
46 #define MAX_NODE_NUMS (MAX_NODES * MAX_DIES)
47
48 #if (defined CONFIG_EXT_CONF_SUPPORT) && CONFIG_EXT_CONF_SUPPORT == 1
49 #error CONFIG_EXT_CONF_SUPPORT == 1 not support anymore!
50 #endif
51
52 typedef struct dram_base_mask {
53         u32 base; //[47:27] at [28:8]
54         u32 mask; //[47:27] at [28:8] and enable at bit 0
55 } dram_base_mask_t;
56
57 static unsigned node_nums;
58 static unsigned sblink;
59 static device_t __f0_dev[MAX_NODE_NUMS];
60 static device_t __f1_dev[MAX_NODE_NUMS];
61 static device_t __f2_dev[MAX_NODE_NUMS];
62 static device_t __f4_dev[MAX_NODE_NUMS];
63 static unsigned fx_devs = 0;
64
65
66 static dram_base_mask_t get_dram_base_mask(u32 nodeid)
67 {
68         device_t dev;
69         dram_base_mask_t d;
70         dev = __f1_dev[0];
71         u32 temp;
72         temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
73         d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out  DramMask [26:24] too
74         temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
75         d.mask |= temp<<21;
76         temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
77         d.mask |= (temp & 1); // enable bit
78         d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
79         temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
80         d.base |= temp<<21;
81         return d;
82 }
83
84 static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg,
85                         u32 io_min, u32 io_max)
86 {
87         u32 i;
88         u32 tempreg;
89         /* io range allocation */
90         tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
91         for (i=0; i<node_nums; i++)
92                 pci_write_config32(__f1_dev[i], reg+4, tempreg);
93         tempreg = 3 /*| ( 3<<4)*/ | ((io_min&0xf0)<<(12-4));          //base :ISA and VGA ?
94 #if 0
95         // FIXME: can we use VGA reg instead?
96         if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
97                 printk(BIOS_SPEW, "%s, enabling legacy VGA IO forwarding for %s link %s\n",
98                                 __func__, dev_path(dev), link);
99                 tempreg |= PCI_IO_BASE_VGA_EN;
100         }
101         if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
102                 tempreg |= PCI_IO_BASE_NO_ISA;
103         }
104 #endif
105         for (i=0; i<node_nums; i++)
106                 pci_write_config32(__f1_dev[i], reg, tempreg);
107 }
108
109 static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
110 {
111         u32 i;
112         u32 tempreg;
113         /* io range allocation */
114         tempreg = (nodeid&0xf) | (linkn<<4) |    (mmio_max&0xffffff00); //limit
115         for (i=0; i<nodes; i++)
116                 pci_write_config32(__f1_dev[i], reg+4, tempreg);
117         tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
118         for (i=0; i<node_nums; i++)
119                 pci_write_config32(__f1_dev[i], reg, tempreg);
120 }
121
122 static device_t get_node_pci(u32 nodeid, u32 fn)
123 {
124 #if MAX_NODE_NUMS == 64
125         if (nodeid < 32) {
126                 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
127         } else {
128                 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
129         }
130 #else
131         return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
132 #endif
133 }
134
135 static void get_fx_devs(void)
136 {
137         int i;
138         for (i = 0; i < MAX_NODE_NUMS; i++) {
139                 __f0_dev[i] = get_node_pci(i, 0);
140                 __f1_dev[i] = get_node_pci(i, 1);
141                 __f2_dev[i] = get_node_pci(i, 2);
142                 __f4_dev[i] = get_node_pci(i, 4);
143                 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
144                         fx_devs = i+1;
145         }
146         if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
147                 die("Cannot find 0:0x18.[0|1]\n");
148         }
149         printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
150 }
151
152 static u32 f1_read_config32(unsigned reg)
153 {
154         if (fx_devs == 0)
155                 get_fx_devs();
156         return pci_read_config32(__f1_dev[0], reg);
157 }
158
159 static void f1_write_config32(unsigned reg, u32 value)
160 {
161         int i;
162         if (fx_devs == 0)
163                 get_fx_devs();
164         for(i = 0; i < fx_devs; i++) {
165                 device_t dev;
166                 dev = __f1_dev[i];
167                 if (dev && dev->enabled) {
168                         pci_write_config32(dev, reg, value);
169                 }
170         }
171 }
172
173 static u32 amdfam15_nodeid(device_t dev)
174 {
175 #if MAX_NODE_NUMS == 64
176         unsigned busn;
177         busn = dev->bus->secondary;
178         if (busn != CONFIG_CBB) {
179                 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
180         } else {
181                 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
182         }
183
184 #else
185         return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
186 #endif
187 }
188
189 static void set_vga_enable_reg(u32 nodeid, u32 linkn)
190 {
191         u32 val;
192
193         val =  1 | (nodeid<<4) | (linkn<<12);
194         /* it will routing
195          * (1)mmio 0xa0000:0xbffff
196          * (2)io   0x3b0:0x3bb, 0x3c0:0x3df
197          */
198         f1_write_config32(0xf4, val);
199
200 }
201
202 /**
203  * @return
204  *  @retval 2  resoure not exist, usable
205  *  @retval 0  resource exist, not usable
206  *  @retval 1  resource exist, resource has been allocated before
207  */
208 static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
209                         unsigned goal_link)
210 {
211         struct resource *res;
212         unsigned nodeid, link = 0;
213         int result;
214         res = 0;
215         for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
216                 device_t dev;
217                 dev = __f0_dev[nodeid];
218                 if (!dev)
219                         continue;
220                 for (link = 0; !res && (link < 8); link++) {
221                         res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
222                 }
223         }
224         result = 2;
225         if (res) {
226                 result = 0;
227                 if ((goal_link == (link - 1)) &&
228                         (goal_nodeid == (nodeid - 1)) &&
229                         (res->flags <= 1)) {
230                         result = 1;
231                 }
232         }
233         return result;
234 }
235
236 static struct resource *amdfam15_find_iopair(device_t dev, unsigned nodeid, unsigned link)
237 {
238         struct resource *resource;
239         u32 free_reg, reg;
240         resource = 0;
241         free_reg = 0;
242         for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
243                 int result;
244                 result = reg_useable(reg, dev, nodeid, link);
245                 if (result == 1) {
246                         /* I have been allocated this one */
247                         break;
248                 }
249                 else if (result > 1) {
250                         /* I have a free register pair */
251                         free_reg = reg;
252                 }
253         }
254         if (reg > 0xd8) {
255                 reg = free_reg; // if no free, the free_reg still be 0
256         }
257
258         resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
259
260         return resource;
261 }
262
263 static struct resource *amdfam15_find_mempair(device_t dev, u32 nodeid, u32 link)
264 {
265         struct resource *resource;
266         u32 free_reg, reg;
267         resource = 0;
268         free_reg = 0;
269         for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
270                 int result;
271                 result = reg_useable(reg, dev, nodeid, link);
272                 if (result == 1) {
273                         /* I have been allocated this one */
274                         break;
275                 }
276                 else if (result > 1) {
277                         /* I have a free register pair */
278                         free_reg = reg;
279                 }
280         }
281         if (reg > 0xb8) {
282                 reg = free_reg;
283         }
284
285         resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
286         return resource;
287 }
288
289
290 static void amdfam15_link_read_bases(device_t dev, u32 nodeid, u32 link)
291 {
292         struct resource *resource;
293
294         /* Initialize the io space constraints on the current bus */
295         resource = amdfam15_find_iopair(dev, nodeid, link);
296         if (resource) {
297                 u32 align;
298                 align = log2(HT_IO_HOST_ALIGN);
299                 resource->base  = 0;
300                 resource->size  = 0;
301                 resource->align = align;
302                 resource->gran  = align;
303                 resource->limit = 0xffffUL;
304                 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
305         }
306
307         /* Initialize the prefetchable memory constraints on the current bus */
308         resource = amdfam15_find_mempair(dev, nodeid, link);
309         if (resource) {
310                 resource->base = 0;
311                 resource->size = 0;
312                 resource->align = log2(HT_MEM_HOST_ALIGN);
313                 resource->gran = log2(HT_MEM_HOST_ALIGN);
314                 resource->limit = 0xffffffffffULL;
315                 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
316                 resource->flags |= IORESOURCE_BRIDGE;
317         }
318
319
320         /* Initialize the memory constraints on the current bus */
321         resource = amdfam15_find_mempair(dev, nodeid, link);
322         if (resource) {
323                 resource->base = 0;
324                 resource->size = 0;
325                 resource->align = log2(HT_MEM_HOST_ALIGN);
326                 resource->gran = log2(HT_MEM_HOST_ALIGN);
327                 resource->limit = 0xffffffffffULL;
328                 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
329         }
330
331 }
332
333
334 static void read_resources(device_t dev)
335 {
336         u32 nodeid;
337         struct bus *link;
338
339         nodeid = amdfam15_nodeid(dev);
340         for (link = dev->link_list; link; link = link->next) {
341                 if (link->children) {
342                         amdfam15_link_read_bases(dev, nodeid, link->link_num);
343                 }
344         }
345 }
346
347
348 static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
349 {
350         resource_t rbase, rend;
351         unsigned reg, link_num;
352         char buf[50];
353
354
355         /* Make certain the resource has actually been set */
356         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
357                 return;
358         }
359
360         /* If I have already stored this resource don't worry about it */
361         if (resource->flags & IORESOURCE_STORED) {
362                 return;
363         }
364
365         /* Only handle PCI memory and IO resources */
366         if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
367                 return;
368
369         /* Ensure I am actually looking at a resource of function 1 */
370         if ((resource->index & 0xffff) < 0x1000) {
371                 return;
372         }
373         /* Get the base address */
374         rbase = resource->base;
375
376         /* Get the limit (rounded up) */
377         rend  = resource_end(resource);
378
379         /* Get the register and link */
380         reg  = resource->index & 0xfff; // 4k
381         link_num = IOINDEX_LINK(resource->index);
382
383         if (resource->flags & IORESOURCE_IO) {
384                 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
385         }
386         else if (resource->flags & IORESOURCE_MEM) {
387                 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums) ;// [39:8]
388         }
389         resource->flags |= IORESOURCE_STORED;
390         sprintf(buf, " <node %x link %x>",
391                         nodeid, link_num);
392         report_resource_stored(dev, resource, buf);
393 }
394
395 /**
396  * I tried to reuse the resource allocation code in set_resource()
397  * but it is too difficult to deal with the resource allocation magic.
398  */
399
400 static void create_vga_resource(device_t dev, unsigned nodeid)
401 {
402         struct bus *link;
403
404
405         /* find out which link the VGA card is connected,
406          * we only deal with the 'first' vga card */
407         for (link = dev->link_list; link; link = link->next) {
408                 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
409 #if CONFIG_MULTIPLE_VGA_ADAPTERS == 1
410                         extern device_t vga_pri; // the primary vga device, defined in device.c
411                         printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
412                                         link->secondary,link->subordinate);
413                         /* We need to make sure the vga_pri is under the link */
414                         if((vga_pri->bus->secondary >= link->secondary ) &&
415                                         (vga_pri->bus->secondary <= link->subordinate )
416                           )
417 #endif
418                                 break;
419                 }
420         }
421
422         /* no VGA card installed */
423         if (link == NULL)
424                 return;
425
426         printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
427         set_vga_enable_reg(nodeid, sblink);
428 }
429
430
431 static void set_resources(device_t dev)
432 {
433         unsigned nodeid;
434         struct bus *bus;
435         struct resource *res;
436
437         /* Find the nodeid */
438         nodeid = amdfam15_nodeid(dev);
439
440         create_vga_resource(dev, nodeid); //TODO: do we need this?
441
442         /* Set each resource we have found */
443         for (res = dev->resource_list; res; res = res->next) {
444                 set_resource(dev, res, nodeid);
445         }
446
447         for (bus = dev->link_list; bus; bus = bus->next) {
448                 if (bus->children) {
449                         assign_resources(bus);
450                 }
451         }
452 }
453
454 static void northbridge_init(struct device *dev)
455 {
456 }
457
458 static unsigned scan_chains(device_t dev, unsigned max)
459 {
460         unsigned nodeid;
461         struct bus *link;
462         device_t io_hub = NULL;
463         u32 next_unitid = 0x18;
464         nodeid = amdfam15_nodeid(dev);
465         if (nodeid == 0) {
466                 for (link = dev->link_list; link; link = link->next) {
467                         //if (link->link_num == sblink) { /* devicetree put IO Hub on link_lsit[sblink] */
468                         if (link->link_num == 0) { /* devicetree put IO Hub on link_lsit[0] */
469                                 io_hub = link->children;
470                                 if (!io_hub || !io_hub->enabled) {
471                                         die("I can't find the IO Hub, or IO Hub not enabled, please check the device tree.\n");
472                                 }
473                                 /* Now that nothing is overlapping it is safe to scan the children. */
474                                 max = pci_scan_bus(link, 0x00, ((next_unitid - 1) << 3) | 7, 0);
475                         }
476                 }
477         }
478         return max;
479 }
480
481 static struct device_operations northbridge_operations = {
482         .read_resources   = read_resources,
483         .set_resources    = set_resources,
484         .enable_resources = pci_dev_enable_resources,
485         .init             = northbridge_init,
486         .scan_bus         = scan_chains,
487         .enable           = 0,
488         .ops_pci          = 0,
489 };
490
491 static const struct pci_driver family15_northbridge __pci_driver = {
492         .ops    = &northbridge_operations,
493         .vendor = PCI_VENDOR_ID_AMD,
494         .device = PCI_DEVICE_ID_AMD_15H_MODEL_000F_NB_HT,
495 };
496
497 static const struct pci_driver family10_northbridge __pci_driver = {
498         .ops    = &northbridge_operations,
499         .vendor = PCI_VENDOR_ID_AMD,
500         .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
501 };
502
503 struct chip_operations northbridge_amd_agesa_family15_ops = {
504         CHIP_NAME("AMD FAM15 Northbridge")
505         .enable_dev = 0,
506 };
507
508 static void domain_read_resources(device_t dev)
509 {
510         unsigned reg;
511
512
513         /* Find the already assigned resource pairs */
514         get_fx_devs();
515         for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
516                 u32 base, limit;
517                 base  = f1_read_config32(reg);
518                 limit = f1_read_config32(reg + 0x04);
519                 /* Is this register allocated? */
520                 if ((base & 3) != 0) {
521                         unsigned nodeid, reg_link;
522                         device_t reg_dev;
523                         if (reg<0xc0) { // mmio
524                                 nodeid = (limit & 0xf) + (base&0x30);
525                         } else { // io
526                                 nodeid =  (limit & 0xf) + ((base>>4)&0x30);
527                         }
528                         reg_link = (limit >> 4) & 7;
529                         reg_dev = __f0_dev[nodeid];
530                         if (reg_dev) {
531                                 /* Reserve the resource  */
532                                 struct resource *res;
533                                 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
534                                 if (res) {
535                                         res->flags = 1;
536                                 }
537                         }
538                 }
539         }
540         /* FIXME: do we need to check extend conf space?
541            I don't believe that much preset value */
542
543 #if CONFIG_PCI_64BIT_PREF_MEM == 0
544         pci_domain_read_resources(dev);
545
546
547 #else
548         struct bus *link;
549         struct resource *resource;
550         for (link=dev->link_list; link; link = link->next) {
551                 /* Initialize the system wide io space constraints */
552                 resource = new_resource(dev, 0|(link->link_num<<2));
553                 resource->base  = 0x400;
554                 resource->limit = 0xffffUL;
555                 resource->flags = IORESOURCE_IO;
556
557                 /* Initialize the system wide prefetchable memory resources constraints */
558                 resource = new_resource(dev, 1|(link->link_num<<2));
559                 resource->limit = 0xfcffffffffULL;
560                 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
561
562                 /* Initialize the system wide memory resources constraints */
563                 resource = new_resource(dev, 2|(link->link_num<<2));
564                 resource->limit = 0xfcffffffffULL;
565                 resource->flags = IORESOURCE_MEM;
566         }
567 #endif
568 }
569
570 static void domain_enable_resources(device_t dev)
571 {
572         u32 val;
573         /* Must be called after PCI enumeration and resource allocation */
574         printk(BIOS_DEBUG, "\nFam15 - domain_enable_resources: AmdInitMid.\n");
575         val = agesawrapper_amdinitmid();
576         if (val) {
577                 printk(BIOS_DEBUG, "agesawrapper_amdinitmid failed: %x \n", val);
578         }
579         printk(BIOS_DEBUG, "  ader - leaving domain_enable_resources.\n");
580 }
581
582
583 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
584 struct hw_mem_hole_info {
585         unsigned hole_startk;
586         int node_id;
587 };
588 static struct hw_mem_hole_info get_hw_mem_hole_info(void)
589 {
590         struct hw_mem_hole_info mem_hole;
591         int i;
592         mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
593         mem_hole.node_id = -1;
594         for (i = 0; i < node_nums; i++) {
595                 dram_base_mask_t d;
596                 u32 hole;
597                 d = get_dram_base_mask(i);
598                 if (!(d.mask & 1)) continue; // no memory on this node
599                 hole = pci_read_config32(__f1_dev[i], 0xf0);
600                 if (hole & 1) { // we find the hole
601                         mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
602                         mem_hole.node_id = i; // record the node No with hole
603                         break; // only one hole
604                 }
605         }
606         //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
607         if (mem_hole.node_id == -1) {
608                 resource_t limitk_pri = 0;
609                 for (i=0; i<node_nums; i++) {
610                         dram_base_mask_t d;
611                         resource_t base_k, limit_k;
612                         d = get_dram_base_mask(i);
613                         if (!(d.base & 1)) continue;
614                         base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
615                         if (base_k > 4 *1024 * 1024) break; // don't need to go to check
616                         if (limitk_pri != base_k) { // we find the hole
617                                 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
618                                 mem_hole.node_id = i;
619                                 break; //only one hole
620                         }
621                         limit_k = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9;
622                         limitk_pri = limit_k;
623                 }
624         }
625         return mem_hole;
626 }
627 #endif
628
629 #if CONFIG_GFXUMA == 1
630 extern uint64_t uma_memory_base, uma_memory_size;
631 static void add_uma_resource(struct device *dev, int index)
632 {
633         struct resource *resource;
634
635         printk(BIOS_DEBUG, "Adding UMA memory area\n");
636         resource = new_resource(dev, index);
637         resource->base = (resource_t) uma_memory_base;
638         resource->size = (resource_t) uma_memory_size;
639         resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
640                         IORESOURCE_FIXED | IORESOURCE_STORED |
641                         IORESOURCE_ASSIGNED;
642 }
643 #endif
644
645 static void domain_set_resources(device_t dev)
646 {
647 #if CONFIG_PCI_64BIT_PREF_MEM == 1
648         struct resource *io, *mem1, *mem2;
649         struct resource *res;
650 #endif
651         unsigned long mmio_basek;
652         u32 pci_tolm;
653         int i, idx;
654         struct bus *link;
655 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
656         struct hw_mem_hole_info mem_hole;
657         u32 reset_memhole = 1;
658 #endif
659
660 #if CONFIG_PCI_64BIT_PREF_MEM == 1
661
662         for (link = dev->link_list; link; link = link->next) {
663                 /* Now reallocate the pci resources memory with the
664                  * highest addresses I can manage.
665                  */
666                 mem1 = find_resource(dev, 1|(link->link_num<<2));
667                 mem2 = find_resource(dev, 2|(link->link_num<<2));
668
669                 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
670                                 mem1->base, mem1->limit, mem1->size, mem1->align);
671                 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
672                                 mem2->base, mem2->limit, mem2->size, mem2->align);
673
674                 /* See if both resources have roughly the same limits */
675                 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
676                                 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
677                 {
678                         /* If so place the one with the most stringent alignment first */
679                         if (mem2->align > mem1->align) {
680                                 struct resource *tmp;
681                                 tmp = mem1;
682                                 mem1 = mem2;
683                                 mem2 = tmp;
684                         }
685                         /* Now place the memory as high up as it will go */
686                         mem2->base = resource_max(mem2);
687                         mem1->limit = mem2->base - 1;
688                         mem1->base = resource_max(mem1);
689                 }
690                 else {
691                         /* Place the resources as high up as they will go */
692                         mem2->base = resource_max(mem2);
693                         mem1->base = resource_max(mem1);
694                 }
695
696                 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
697                                 mem1->base, mem1->limit, mem1->size, mem1->align);
698                 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
699                                 mem2->base, mem2->limit, mem2->size, mem2->align);
700         }
701
702         for (res = &dev->resource_list; res; res = res->next)
703         {
704                 res->flags |= IORESOURCE_ASSIGNED;
705                 res->flags |= IORESOURCE_STORED;
706                 report_resource_stored(dev, res, "");
707         }
708 #endif
709
710         pci_tolm = 0xffffffffUL;
711         for (link = dev->link_list; link; link = link->next) {
712                 pci_tolm = find_pci_tolm(link);
713         }
714
715         // FIXME handle interleaved nodes. If you fix this here, please fix
716         // amdk8, too.
717         mmio_basek = pci_tolm >> 10;
718         /* Round mmio_basek to something the processor can support */
719         mmio_basek &= ~((1 << 6) -1);
720
721         // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
722         // MMIO hole. If you fix this here, please fix amdk8, too.
723         /* Round the mmio hole to 64M */
724         mmio_basek &= ~((64*1024) - 1);
725
726 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
727         /* if the hw mem hole is already set in raminit stage, here we will compare
728          * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
729          * use hole_basek as mmio_basek and we don't need to reset hole.
730          * otherwise We reset the hole to the mmio_basek
731          */
732
733         mem_hole = get_hw_mem_hole_info();
734
735         // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
736         if ((mem_hole.node_id !=  -1) && (mmio_basek > mem_hole.hole_startk)) {
737                 mmio_basek = mem_hole.hole_startk;
738                 reset_memhole = 0;
739         }
740 #endif
741
742         idx = 0x10;
743         for (i = 0; i < node_nums; i++) {
744                 dram_base_mask_t d;
745                 resource_t basek, limitk, sizek; // 4 1T
746
747                 d = get_dram_base_mask(i);
748
749                 if (!(d.mask & 1)) continue;
750                 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
751                 limitk = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9 ;
752
753                 sizek = limitk - basek;
754
755
756                 /* see if we need a hole from 0xa0000 to 0xbffff */
757                 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
758                         ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
759                         idx += 0x10;
760                         basek = (8*64)+(16*16);
761                         sizek = limitk - ((8*64)+(16*16));
762
763                 }
764
765                 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
766
767                 /* split the region to accomodate pci memory space */
768                 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
769                         if (basek <= mmio_basek) {
770                                 unsigned pre_sizek;
771                                 pre_sizek = mmio_basek - basek;
772                                 if (pre_sizek>0) {
773                                         ram_resource(dev, (idx | i), basek, pre_sizek);
774                                         idx += 0x10;
775                                         sizek -= pre_sizek;
776 #if CONFIG_WRITE_HIGH_TABLES==1
777                                         if (high_tables_base==0) {
778                                                 /* Leave some space for ACPI, PIRQ and MP tables */
779 #if CONFIG_GFXUMA == 1
780                                                 high_tables_base = uma_memory_base - HIGH_MEMORY_SIZE;
781 #else
782                                                 high_tables_base = (mmio_basek * 1024) - HIGH_MEMORY_SIZE;
783 #endif
784                                                 high_tables_size = HIGH_MEMORY_SIZE;
785                                                 printk(BIOS_DEBUG, " split: %dK table at =%08llx\n",
786                                                          (u32)(high_tables_size / 1024), high_tables_base);
787                                         }
788 #endif
789                                 }
790                                 basek = mmio_basek;
791                         }
792                         if ((basek + sizek) <= 4*1024*1024) {
793                                 sizek = 0;
794                         }
795                         else {
796                                 basek = 4*1024*1024;
797                                 sizek -= (4*1024*1024 - mmio_basek);
798                         }
799                 }
800
801 #if CONFIG_GFXUMA == 1
802                 /* Deduct uma memory before reporting because
803                  * this is what the mtrr code expects */
804                 sizek -= uma_memory_size / 1024;
805 #endif
806                 ram_resource(dev, (idx | i), basek, sizek);
807                 idx += 0x10;
808 #if CONFIG_WRITE_HIGH_TABLES==1
809                 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
810                                 i, mmio_basek, basek, limitk);
811                 if (high_tables_base==0) {
812                         /* Leave some space for ACPI, PIRQ and MP tables */
813 #if CONFIG_GFXUMA == 1
814                         high_tables_base = uma_memory_base - HIGH_MEMORY_SIZE;
815 #else
816                         high_tables_base = (limitk * 1024) - HIGH_MEMORY_SIZE;
817 #endif
818                         high_tables_size = HIGH_MEMORY_SIZE;
819                 }
820 #endif
821         }
822
823 #if CONFIG_GFXUMA == 1
824         add_uma_resource(dev, 7);
825 #endif
826
827         for(link = dev->link_list; link; link = link->next) {
828                 if (link->children) {
829                         assign_resources(link);
830                 }
831         }
832 }
833
834
835 static struct device_operations pci_domain_ops = {
836         .read_resources   = domain_read_resources,
837         .set_resources    = domain_set_resources,
838         .enable_resources = domain_enable_resources,
839         .init             = NULL,
840         .scan_bus         = pci_domain_scan_bus,
841
842 #if CONFIG_MMCONF_SUPPORT_DEFAULT
843         .ops_pci_bus      = &pci_ops_mmconf,
844 #else
845         .ops_pci_bus      = &pci_cf8_conf1,
846 #endif
847 };
848
849
850 static void sysconf_init(device_t dev) // first node
851 {
852         sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
853         node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
854 }
855
856 static void add_more_links(device_t dev, unsigned total_links)
857 {
858         struct bus *link, *last = NULL;
859         int link_num;
860
861         for (link = dev->link_list; link; link = link->next)
862                 last = link;
863
864         if (last) {
865                 int links = total_links - last->link_num;
866                 link_num = last->link_num;
867                 if (links > 0) {
868                         link = malloc(links*sizeof(*link));
869                         if (!link)
870                                 die("Couldn't allocate more links!\n");
871                         memset(link, 0, links*sizeof(*link));
872                         last->next = link;
873                 }
874         }
875         else {
876                 link_num = -1;
877                 link = malloc(total_links*sizeof(*link));
878                 memset(link, 0, total_links*sizeof(*link));
879                 dev->link_list = link;
880         }
881
882         for (link_num = link_num + 1; link_num < total_links; link_num++) {
883                 link->link_num = link_num;
884                 link->dev = dev;
885                 link->next = link + 1;
886                 last = link;
887                 link = link->next;
888         }
889         last->next = NULL;
890 }
891
892 /* dummy read_resources */
893 static void lapic_read_resources(device_t dev)
894 {
895 }
896
897 static struct device_operations lapic_ops = {
898         .read_resources   = lapic_read_resources,
899         .set_resources    = pci_dev_set_resources,
900         .enable_resources = pci_dev_enable_resources,
901         .init             = 0,
902         .scan_bus         = 0,
903         .enable           = 0,
904         .ops_pci          = 0,
905 };
906
907 static u32 cpu_bus_scan(device_t dev, u32 max)
908 {
909         struct bus *cpu_bus;
910         device_t dev_mc;
911 #if CONFIG_CBB
912         device_t pci_domain;
913 #endif
914         int i,j;
915         int coreid_bits;
916         int core_max = 0;
917         unsigned ApicIdCoreIdSize;
918         unsigned core_nums;
919         int siblings = 0;
920         unsigned int family;
921
922 #if CONFIG_CBB
923         dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
924         if (dev_mc && dev_mc->bus) {
925                 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
926                 pci_domain = dev_mc->bus->dev;
927                 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
928                         printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
929                         dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
930                         printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
931                 } else {
932                         printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
933                 }
934                 printk(BIOS_DEBUG, "\n");
935         }
936         dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
937         if (!dev_mc) {
938                 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
939                 if (dev_mc && dev_mc->bus) {
940                         printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
941                         pci_domain = dev_mc->bus->dev;
942                         if (pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
943                                 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
944                                         printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
945                                         dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
946                                         printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
947                                         while (dev_mc) {
948                                                 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
949                                                 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
950                                                 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
951                                                 dev_mc = dev_mc->sibling;
952                                         }
953                                 }
954                         }
955                 }
956         }
957 #endif
958         dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
959         if (!dev_mc) {
960                 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
961                 die("");
962         }
963         sysconf_init(dev_mc);
964 #if CONFIG_CBB && (MAX_NODE_NUMS > 32)
965         if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
966                 if (pci_domain->link_list && !pci_domain->link_list->next) {
967                         struct bus *new_link = new_link(pci_domain);
968                         pci_domain->link_list->next = new_link;
969                         new_link->link_num = 1;
970                         new_link->dev = pci_domain;
971                         new_link->children = 0;
972                         printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
973                 }
974                 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
975         }
976 #endif
977
978         /* Get Max Number of cores(MNC) */
979         coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
980         core_max = 1 << (coreid_bits & 0x000F); //mnc
981
982         ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
983         if (ApicIdCoreIdSize) {
984                 core_nums = (1 << ApicIdCoreIdSize) - 1;
985         } else {
986                 core_nums = 3; //quad core
987         }
988
989         /* Find which cpus are present */
990         cpu_bus = dev->link_list;
991         for (i = 0; i < node_nums; i++) {
992                 device_t cdb_dev, cpu;
993                 struct device_path cpu_path;
994                 unsigned busn, devn;
995                 struct bus *pbus;
996
997                 busn = CONFIG_CBB;
998                 devn = CONFIG_CDB + i;
999                 pbus = dev_mc->bus;
1000 #if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1001                 if (i >= 32) {
1002                         busn--;
1003                         devn -= 32;
1004                         pbus = pci_domain->link_list->next;
1005                 }
1006 #endif
1007
1008                 /* Find the cpu's pci device */
1009                 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1010                 if (!cdb_dev) {
1011                         /* If I am probing things in a weird order
1012                          * ensure all of the cpu's pci devices are found.
1013                          */
1014                         int fn;
1015                         for(fn = 0; fn <= 5; fn++) { //FBDIMM?
1016                                 cdb_dev = pci_probe_dev(NULL, pbus,
1017                                                         PCI_DEVFN(devn, fn));
1018                         }
1019                         cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1020                 } else {
1021                         /* Ok, We need to set the links for that device.
1022                          * otherwise the device under it will not be scanned
1023                          */
1024                         int linknum;
1025 #if CONFIG_HT3_SUPPORT==1
1026                         linknum = 8;
1027 #else
1028                         linknum = 4;
1029 #endif
1030                         add_more_links(cdb_dev, linknum);
1031                 }
1032
1033                 family = cpuid_eax(1);
1034                 family = (family >> 20) & 0xFF;
1035                 if (family == 1) { //f10
1036                         u32 dword;
1037                         cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1038                         dword = pci_read_config32(cdb_dev, 0xe8);
1039                         siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1040                 } else if (family == 6) {//f15
1041                         cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1042                         if (cdb_dev && cdb_dev->enabled) {
1043                                 siblings = pci_read_config32(cdb_dev, 0x84);
1044                                 siblings &= 0xFF;
1045                         }
1046                 } else {
1047                         siblings = 0; //default one core
1048                 }
1049                 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1050                                 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1051
1052                 for (j = 0; j <= siblings; j++ ) {
1053                         extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1054                         u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1055                         u32 lapicid_start = 0;
1056
1057                         /* Build the cpu device path */
1058                         cpu_path.type = DEVICE_PATH_APIC;
1059                         /*
1060                          * APIC ID calucation is tightly coupled with AGESA v5 code.
1061                          * This calculation MUST match the assignment calculation done
1062                          * in LocalApicInitializationAtEarly() function.
1063                          * And reference GetLocalApicIdForCore()
1064                          *
1065                          * Apply apic enumeration rules
1066                          * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1067                          * put the local-APICs at m..z
1068                          *
1069                          * This is needed because many IO-APIC devices only have 4 bits
1070                          * for their APIC id and therefore must reside at 0..15
1071                          */
1072 #ifndef CFG_PLAT_NUM_IO_APICS /* defined in mainboard buildOpts.c */
1073 #define CFG_PLAT_NUM_IO_APICS 3
1074 #endif
1075                         if ((node_nums * core_max) + CFG_PLAT_NUM_IO_APICS >= 0x10) {
1076                                 lapicid_start = (CFG_PLAT_NUM_IO_APICS - 1) / core_max;
1077                                 lapicid_start = (lapicid_start + 1) * core_max;
1078                                 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1079                         }
1080                         cpu_path.apic.apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
1081                         printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
1082                                         i, j, cpu_path.apic.apic_id);
1083
1084                         /* See if I can find the cpu */
1085                         cpu = find_dev_path(cpu_bus, &cpu_path);
1086                         /* Enable the cpu if I have the processor */
1087                         if (cdb_dev && cdb_dev->enabled) {
1088                                 if (!cpu) {
1089                                         cpu = alloc_dev(cpu_bus, &cpu_path);
1090                                 }
1091                                 if (cpu) {
1092                                         cpu->enabled = 1;
1093                                 }
1094                         }
1095                         /* Disable the cpu if I don't have the processor */
1096                         if (cpu && (!cdb_dev || !cdb_dev->enabled)) {
1097                                 cpu->enabled = 0;
1098                         }
1099                         /* Report what I have done */
1100                         if (cpu) {
1101                                 cpu->path.apic.node_id = i;
1102                                 cpu->path.apic.core_id = j;
1103                                 if (cpu->path.type == DEVICE_PATH_APIC) {
1104                                         cpu->ops = &lapic_ops;
1105                                 }
1106                                 printk(BIOS_DEBUG, "CPU: %s %s\n",
1107                                         dev_path(cpu), cpu->enabled?"enabled":"disabled");
1108                         }
1109                 } //j
1110         }
1111         return max;
1112 }
1113
1114 static void cpu_bus_init(device_t dev)
1115 {
1116         initialize_cpus(dev->link_list);
1117 }
1118
1119 static void cpu_bus_noop(device_t dev)
1120 {
1121 }
1122
1123 static void cpu_bus_read_resources(device_t dev)
1124 {
1125 #if CONFIG_MMCONF_SUPPORT
1126         struct resource *resource = new_resource(dev, 0xc0010058);
1127         resource->base = CONFIG_MMCONF_BASE_ADDRESS;
1128         resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096*256;
1129         resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
1130                 IORESOURCE_FIXED | IORESOURCE_STORED |  IORESOURCE_ASSIGNED;
1131 #endif
1132 }
1133
1134 static void cpu_bus_set_resources(device_t dev)
1135 {
1136         struct resource *resource = find_resource(dev, 0xc0010058);
1137         if (resource) {
1138                 report_resource_stored(dev, resource, " <mmconfig>");
1139         }
1140         pci_dev_set_resources(dev);
1141 }
1142
1143 static struct device_operations cpu_bus_ops = {
1144         .read_resources   = cpu_bus_read_resources,
1145         .set_resources    = cpu_bus_set_resources,
1146         .enable_resources = cpu_bus_noop,
1147         .init             = cpu_bus_init,
1148         .scan_bus         = cpu_bus_scan,
1149 };
1150
1151
1152 static void root_complex_enable_dev(struct device *dev)
1153 {
1154         /* Set the operations if it is a special bus type */
1155         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
1156                 dev->ops = &pci_domain_ops;
1157         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
1158                 dev->ops = &cpu_bus_ops;
1159         }
1160 }
1161
1162
1163 struct chip_operations northbridge_amd_agesa_family15_root_complex_ops = {
1164         CHIP_NAME("AMD FAM15 Root Complex")
1165         .enable_dev = root_complex_enable_dev,
1166 };