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