blob: 9a31751b1d164d1e549747c9b732beeb2c051c1c [file] [log] [blame]
zbao2c08f6a2012-07-02 15:32:58 +08001/*
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
52typedef 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
57static unsigned node_nums;
58static unsigned sblink;
59static device_t __f0_dev[MAX_NODE_NUMS];
60static device_t __f1_dev[MAX_NODE_NUMS];
61static device_t __f2_dev[MAX_NODE_NUMS];
62static device_t __f4_dev[MAX_NODE_NUMS];
63static unsigned fx_devs = 0;
64
65static 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
83static 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
108static 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
121static 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
134static 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
151static 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
158static 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
172static 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
188static 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 */
207static 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
235static 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
262static 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
288static void amdfam15_link_read_bases(device_t dev, u32 nodeid, u32 link)
289{
290 struct resource *resource;
291
292 /* Initialize the io space constraints on the current bus */
293 resource = amdfam15_find_iopair(dev, nodeid, link);
294 if (resource) {
295 u32 align;
296 align = log2(HT_IO_HOST_ALIGN);
297 resource->base = 0;
298 resource->size = 0;
299 resource->align = align;
300 resource->gran = align;
301 resource->limit = 0xffffUL;
302 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
303 }
304
305 /* Initialize the prefetchable memory constraints on the current bus */
306 resource = amdfam15_find_mempair(dev, nodeid, link);
307 if (resource) {
308 resource->base = 0;
309 resource->size = 0;
310 resource->align = log2(HT_MEM_HOST_ALIGN);
311 resource->gran = log2(HT_MEM_HOST_ALIGN);
312 resource->limit = 0xffffffffffULL;
313 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
314 resource->flags |= IORESOURCE_BRIDGE;
315 }
316
317 /* Initialize the memory constraints on the current bus */
318 resource = amdfam15_find_mempair(dev, nodeid, link);
319 if (resource) {
320 resource->base = 0;
321 resource->size = 0;
322 resource->align = log2(HT_MEM_HOST_ALIGN);
323 resource->gran = log2(HT_MEM_HOST_ALIGN);
324 resource->limit = 0xffffffffffULL;
325 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
326 }
327
328}
329
330static void read_resources(device_t dev)
331{
332 u32 nodeid;
333 struct bus *link;
334
335 nodeid = amdfam15_nodeid(dev);
336 for (link = dev->link_list; link; link = link->next) {
337 if (link->children) {
338 amdfam15_link_read_bases(dev, nodeid, link->link_num);
339 }
340 }
341}
342
343static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
344{
345 resource_t rbase, rend;
346 unsigned reg, link_num;
347 char buf[50];
348
349 /* Make certain the resource has actually been set */
350 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
351 return;
352 }
353
354 /* If I have already stored this resource don't worry about it */
355 if (resource->flags & IORESOURCE_STORED) {
356 return;
357 }
358
359 /* Only handle PCI memory and IO resources */
360 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
361 return;
362
363 /* Ensure I am actually looking at a resource of function 1 */
364 if ((resource->index & 0xffff) < 0x1000) {
365 return;
366 }
367 /* Get the base address */
368 rbase = resource->base;
369
370 /* Get the limit (rounded up) */
371 rend = resource_end(resource);
372
373 /* Get the register and link */
374 reg = resource->index & 0xfff; // 4k
375 link_num = IOINDEX_LINK(resource->index);
376
377 if (resource->flags & IORESOURCE_IO) {
378 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
379 }
380 else if (resource->flags & IORESOURCE_MEM) {
381 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums) ;// [39:8]
382 }
383 resource->flags |= IORESOURCE_STORED;
384 sprintf(buf, " <node %x link %x>",
385 nodeid, link_num);
386 report_resource_stored(dev, resource, buf);
387}
388
389/**
390 * I tried to reuse the resource allocation code in set_resource()
391 * but it is too difficult to deal with the resource allocation magic.
392 */
393
394static void create_vga_resource(device_t dev, unsigned nodeid)
395{
396 struct bus *link;
397
398 /* find out which link the VGA card is connected,
399 * we only deal with the 'first' vga card */
400 for (link = dev->link_list; link; link = link->next) {
401 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
zbaod59d6242012-07-23 19:41:03 +0800402#if CONFIG_MULTIPLE_VGA_ADAPTERS
zbao2c08f6a2012-07-02 15:32:58 +0800403 extern device_t vga_pri; // the primary vga device, defined in device.c
404 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
405 link->secondary,link->subordinate);
406 /* We need to make sure the vga_pri is under the link */
407 if((vga_pri->bus->secondary >= link->secondary ) &&
408 (vga_pri->bus->secondary <= link->subordinate )
409 )
410#endif
411 break;
412 }
413 }
414
415 /* no VGA card installed */
416 if (link == NULL)
417 return;
418
419 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
420 set_vga_enable_reg(nodeid, sblink);
421}
422
423static void set_resources(device_t dev)
424{
425 unsigned nodeid;
426 struct bus *bus;
427 struct resource *res;
428
429 /* Find the nodeid */
430 nodeid = amdfam15_nodeid(dev);
431
432 create_vga_resource(dev, nodeid); //TODO: do we need this?
433
434 /* Set each resource we have found */
435 for (res = dev->resource_list; res; res = res->next) {
436 set_resource(dev, res, nodeid);
437 }
438
439 for (bus = dev->link_list; bus; bus = bus->next) {
440 if (bus->children) {
441 assign_resources(bus);
442 }
443 }
444}
445
446static void northbridge_init(struct device *dev)
447{
448}
449
zbaod59d6242012-07-23 19:41:03 +0800450static unsigned scan_chains(device_t dev, unsigned max)
451{
452 unsigned nodeid;
453 struct bus *link;
454 device_t io_hub = NULL;
455 u32 next_unitid = 0x18;
456 nodeid = amdfam15_nodeid(dev);
457 if (nodeid == 0) {
458 for (link = dev->link_list; link; link = link->next) {
459 //if (link->link_num == sblink) { /* devicetree put IO Hub on link_lsit[sblink] */
460 if (link->link_num == 0) { /* devicetree put IO Hub on link_lsit[0] */
461 io_hub = link->children;
462 if (!io_hub || !io_hub->enabled) {
463 die("I can't find the IO Hub, or IO Hub not enabled, please check the device tree.\n");
464 }
465 /* Now that nothing is overlapping it is safe to scan the children. */
466 max = pci_scan_bus(link, 0x00, ((next_unitid - 1) << 3) | 7, 0);
467 }
468 }
469 }
470 return max;
471}
472
zbao2c08f6a2012-07-02 15:32:58 +0800473static struct device_operations northbridge_operations = {
474 .read_resources = read_resources,
475 .set_resources = set_resources,
476 .enable_resources = pci_dev_enable_resources,
477 .init = northbridge_init,
zbaod59d6242012-07-23 19:41:03 +0800478 .scan_bus = scan_chains,
zbao2c08f6a2012-07-02 15:32:58 +0800479 .enable = 0,
480 .ops_pci = 0,
481};
482
483static const struct pci_driver family15_northbridge __pci_driver = {
484 .ops = &northbridge_operations,
485 .vendor = PCI_VENDOR_ID_AMD,
486 .device = PCI_DEVICE_ID_AMD_15H_MODEL_001F_NB_HT,
487};
488
489static const struct pci_driver family10_northbridge __pci_driver = {
490 .ops = &northbridge_operations,
491 .vendor = PCI_VENDOR_ID_AMD,
492 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
493};
494
495struct chip_operations northbridge_amd_agesa_family15tn_ops = {
496 CHIP_NAME("AMD FAM15 Northbridge")
497 .enable_dev = 0,
498};
499
500static void domain_read_resources(device_t dev)
501{
502 unsigned reg;
503
504 /* Find the already assigned resource pairs */
505 get_fx_devs();
506 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
507 u32 base, limit;
508 base = f1_read_config32(reg);
509 limit = f1_read_config32(reg + 0x04);
510 /* Is this register allocated? */
511 if ((base & 3) != 0) {
512 unsigned nodeid, reg_link;
513 device_t reg_dev;
514 if (reg<0xc0) { // mmio
515 nodeid = (limit & 0xf) + (base&0x30);
516 } else { // io
517 nodeid = (limit & 0xf) + ((base>>4)&0x30);
518 }
519 reg_link = (limit >> 4) & 7;
520 reg_dev = __f0_dev[nodeid];
521 if (reg_dev) {
522 /* Reserve the resource */
523 struct resource *res;
524 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
525 if (res) {
526 res->flags = 1;
527 }
528 }
529 }
530 }
531 /* FIXME: do we need to check extend conf space?
532 I don't believe that much preset value */
533
zbaod59d6242012-07-23 19:41:03 +0800534#if !CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800535 pci_domain_read_resources(dev);
536
537#else
538 struct bus *link;
539 struct resource *resource;
540 for (link=dev->link_list; link; link = link->next) {
541 /* Initialize the system wide io space constraints */
542 resource = new_resource(dev, 0|(link->link_num<<2));
543 resource->base = 0x400;
544 resource->limit = 0xffffUL;
545 resource->flags = IORESOURCE_IO;
546
547 /* Initialize the system wide prefetchable memory resources constraints */
548 resource = new_resource(dev, 1|(link->link_num<<2));
549 resource->limit = 0xfcffffffffULL;
550 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
551
552 /* Initialize the system wide memory resources constraints */
553 resource = new_resource(dev, 2|(link->link_num<<2));
554 resource->limit = 0xfcffffffffULL;
555 resource->flags = IORESOURCE_MEM;
556 }
557#endif
558}
559
560extern u8 acpi_slp_type;
561
562static void domain_enable_resources(device_t dev)
563{
564 u32 val;
565#if CONFIG_HAVE_ACPI_RESUME
566 if (acpi_slp_type == 3)
567 agesawrapper_fchs3laterestore();
568#endif
569
570 /* Must be called after PCI enumeration and resource allocation */
571 printk(BIOS_DEBUG, "\nFam15 - domain_enable_resources: AmdInitMid.\n");
572#if CONFIG_HAVE_ACPI_RESUME
573 if (acpi_slp_type != 3) {
574 printk(BIOS_DEBUG, "agesawrapper_amdinitmid ");
575 val = agesawrapper_amdinitmid ();
576 if (val)
577 printk(BIOS_DEBUG, "error level: %x \n", val);
578 else
579 printk(BIOS_DEBUG, "passed.\n");
580 }
581#else
582 printk(BIOS_DEBUG, "agesawrapper_amdinitmid ");
583 val = agesawrapper_amdinitmid ();
584 if (val)
585 printk(BIOS_DEBUG, "error level: %x \n", val);
586 else
587 printk(BIOS_DEBUG, "passed.\n");
588#endif
589
590 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
591}
592
593#if CONFIG_HW_MEM_HOLE_SIZEK != 0
594struct hw_mem_hole_info {
595 unsigned hole_startk;
596 int node_id;
597};
598static struct hw_mem_hole_info get_hw_mem_hole_info(void)
599{
600 struct hw_mem_hole_info mem_hole;
601 int i;
602 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
603 mem_hole.node_id = -1;
604 for (i = 0; i < node_nums; i++) {
605 dram_base_mask_t d;
606 u32 hole;
607 d = get_dram_base_mask(i);
608 if (!(d.mask & 1)) continue; // no memory on this node
609 hole = pci_read_config32(__f1_dev[i], 0xf0);
610 if (hole & 1) { // we find the hole
611 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
612 mem_hole.node_id = i; // record the node No with hole
613 break; // only one hole
614 }
615 }
616 //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
617 if (mem_hole.node_id == -1) {
618 resource_t limitk_pri = 0;
619 for (i=0; i<node_nums; i++) {
620 dram_base_mask_t d;
621 resource_t base_k, limit_k;
622 d = get_dram_base_mask(i);
623 if (!(d.base & 1)) continue;
624 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
625 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
626 if (limitk_pri != base_k) { // we find the hole
627 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
628 mem_hole.node_id = i;
629 break; //only one hole
630 }
631 limit_k = ((resource_t)((d.mask + 0x00000100) & 0x1fffff00)) << 9;
632 limitk_pri = limit_k;
633 }
634 }
635 return mem_hole;
636}
637#endif
638
zbao405cfe22012-07-23 19:44:29 +0800639#define ONE_MB_SHIFT 20
zbao6db7f342012-07-19 16:38:12 +0800640
Kyösti Mälkki6b5eb1c2012-07-19 19:26:43 +0300641static void setup_uma_memory(void)
zbao6db7f342012-07-19 16:38:12 +0800642{
643#if CONFIG_GFXUMA
644 msr_t msr, msr2;
645 uint32_t sys_mem;
646
647 /* TOP_MEM: the top of DRAM below 4G */
648 msr = rdmsr(TOP_MEM);
649 printk
650 (BIOS_INFO, "%s, TOP MEM: msr.lo = 0x%08x, msr.hi = 0x%08x\n",
651 __func__, msr.lo, msr.hi);
652
653 /* TOP_MEM2: the top of DRAM above 4G */
654 msr2 = rdmsr(TOP_MEM2);
655 printk (BIOS_INFO, "%s, TOP MEM2: msr2.lo = 0x%08x, msr2.hi = 0x%08x\n",
656 __func__, msr2.lo, msr2.hi);
657
658 /* refer to UMA Size Consideration in Family15h BKDG. */
659 /* Please reference MemNGetUmaSizeOR () */
660 /*
661 * Total system memory UMASize
662 * >= 2G 512M
663 * >=1G 256M
664 * <1G 64M
665 */
zbao405cfe22012-07-23 19:44:29 +0800666 sys_mem = msr.lo + (16 << ONE_MB_SHIFT); // Ignore 16MB allocated for C6 when finding UMA size
667 if ((msr2.hi & 0x0000000F) || (sys_mem >= 2048 << ONE_MB_SHIFT)) {
668 uma_memory_size = 512 << ONE_MB_SHIFT;
669 } else if (sys_mem >= 1024 << ONE_MB_SHIFT) {
670 uma_memory_size = 256 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800671 } else {
zbao405cfe22012-07-23 19:44:29 +0800672 uma_memory_size = 64 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800673 }
674 uma_memory_base = msr.lo - uma_memory_size; /* TOP_MEM1 */
675
676 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
677 __func__, uma_memory_size, uma_memory_base);
678
679 /* TODO: TOP_MEM2 */
zbao6db7f342012-07-19 16:38:12 +0800680#endif
681}
682
683
zbao2c08f6a2012-07-02 15:32:58 +0800684static void domain_set_resources(device_t dev)
685{
zbaod59d6242012-07-23 19:41:03 +0800686#if CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800687 struct resource *io, *mem1, *mem2;
688 struct resource *res;
689#endif
690 unsigned long mmio_basek;
691 u32 pci_tolm;
692 int i, idx;
693 struct bus *link;
694#if CONFIG_HW_MEM_HOLE_SIZEK != 0
695 struct hw_mem_hole_info mem_hole;
696 u32 reset_memhole = 1;
697#endif
698
Kyösti Mälkki6b5eb1c2012-07-19 19:26:43 +0300699 setup_uma_memory();
700
zbaod59d6242012-07-23 19:41:03 +0800701#if CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800702
703 for (link = dev->link_list; link; link = link->next) {
704 /* Now reallocate the pci resources memory with the
705 * highest addresses I can manage.
706 */
707 mem1 = find_resource(dev, 1|(link->link_num<<2));
708 mem2 = find_resource(dev, 2|(link->link_num<<2));
709
710 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
711 mem1->base, mem1->limit, mem1->size, mem1->align);
712 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
713 mem2->base, mem2->limit, mem2->size, mem2->align);
714
715 /* See if both resources have roughly the same limits */
716 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
717 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
718 {
719 /* If so place the one with the most stringent alignment first */
720 if (mem2->align > mem1->align) {
721 struct resource *tmp;
722 tmp = mem1;
723 mem1 = mem2;
724 mem2 = tmp;
725 }
726 /* Now place the memory as high up as it will go */
727 mem2->base = resource_max(mem2);
728 mem1->limit = mem2->base - 1;
729 mem1->base = resource_max(mem1);
730 }
731 else {
732 /* Place the resources as high up as they will go */
733 mem2->base = resource_max(mem2);
734 mem1->base = resource_max(mem1);
735 }
736
737 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
738 mem1->base, mem1->limit, mem1->size, mem1->align);
739 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
740 mem2->base, mem2->limit, mem2->size, mem2->align);
741 }
742
743 for (res = &dev->resource_list; res; res = res->next)
744 {
745 res->flags |= IORESOURCE_ASSIGNED;
746 res->flags |= IORESOURCE_STORED;
747 report_resource_stored(dev, res, "");
748 }
749#endif
750
751 pci_tolm = 0xffffffffUL;
752 for (link = dev->link_list; link; link = link->next) {
753 pci_tolm = find_pci_tolm(link);
754 }
755
756 // FIXME handle interleaved nodes. If you fix this here, please fix
757 // amdk8, too.
758 mmio_basek = pci_tolm >> 10;
759 /* Round mmio_basek to something the processor can support */
760 mmio_basek &= ~((1 << 6) -1);
761
762 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
763 // MMIO hole. If you fix this here, please fix amdk8, too.
764 /* Round the mmio hole to 64M */
765 mmio_basek &= ~((64*1024) - 1);
766
767#if CONFIG_HW_MEM_HOLE_SIZEK != 0
768 /* if the hw mem hole is already set in raminit stage, here we will compare
769 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
770 * use hole_basek as mmio_basek and we don't need to reset hole.
771 * otherwise We reset the hole to the mmio_basek
772 */
773
774 mem_hole = get_hw_mem_hole_info();
775
776 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
777 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
778 mmio_basek = mem_hole.hole_startk;
779 reset_memhole = 0;
780 }
781#endif
782
783 idx = 0x10;
784 for (i = 0; i < node_nums; i++) {
785 dram_base_mask_t d;
786 resource_t basek, limitk, sizek; // 4 1T
787
788 d = get_dram_base_mask(i);
789
790 if (!(d.mask & 1)) continue;
791 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
zbao9fd183e2012-08-01 18:23:49 +0800792 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9 ;
zbao2c08f6a2012-07-02 15:32:58 +0800793
794 sizek = limitk - basek;
795
796 /* see if we need a hole from 0xa0000 to 0xbffff */
797 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
798 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
799 idx += 0x10;
800 basek = (8*64)+(16*16);
801 sizek = limitk - ((8*64)+(16*16));
802
803 }
804
805 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
806
807 /* split the region to accomodate pci memory space */
808 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
809 if (basek <= mmio_basek) {
810 unsigned pre_sizek;
811 pre_sizek = mmio_basek - basek;
812 if (pre_sizek>0) {
813 ram_resource(dev, (idx | i), basek, pre_sizek);
814 idx += 0x10;
815 sizek -= pre_sizek;
zbaod59d6242012-07-23 19:41:03 +0800816#if CONFIG_WRITE_HIGH_TABLES
zbao2c08f6a2012-07-02 15:32:58 +0800817 if (high_tables_base==0) {
818 /* Leave some space for ACPI, PIRQ and MP tables */
zbaod59d6242012-07-23 19:41:03 +0800819#if CONFIG_GFXUMA
zbao2c08f6a2012-07-02 15:32:58 +0800820 high_tables_base = uma_memory_base - HIGH_MEMORY_SIZE;
821#else
822 high_tables_base = (mmio_basek * 1024) - HIGH_MEMORY_SIZE;
823#endif
824 high_tables_size = HIGH_MEMORY_SIZE;
825 printk(BIOS_DEBUG, " split: %dK table at =%08llx\n",
826 (u32)(high_tables_size / 1024), high_tables_base);
827 }
828#endif
829 }
830 basek = mmio_basek;
831 }
832 if ((basek + sizek) <= 4*1024*1024) {
833 sizek = 0;
834 }
835 else {
836 basek = 4*1024*1024;
837 sizek -= (4*1024*1024 - mmio_basek);
838 }
839 }
840
zbao2c08f6a2012-07-02 15:32:58 +0800841 ram_resource(dev, (idx | i), basek, sizek);
842 idx += 0x10;
zbaod59d6242012-07-23 19:41:03 +0800843#if CONFIG_WRITE_HIGH_TABLES
zbao2c08f6a2012-07-02 15:32:58 +0800844 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
845 i, mmio_basek, basek, limitk);
846 if (high_tables_base==0) {
847 /* Leave some space for ACPI, PIRQ and MP tables */
zbaod59d6242012-07-23 19:41:03 +0800848#if CONFIG_GFXUMA
zbao2c08f6a2012-07-02 15:32:58 +0800849 high_tables_base = uma_memory_base - HIGH_MEMORY_SIZE;
850#else
851 high_tables_base = (limitk * 1024) - HIGH_MEMORY_SIZE;
852#endif
853 high_tables_size = HIGH_MEMORY_SIZE;
854 }
855#endif
856 }
857
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300858#if CONFIG_GFXUMA
859 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
zbao2c08f6a2012-07-02 15:32:58 +0800860#endif
861
862 for(link = dev->link_list; link; link = link->next) {
863 if (link->children) {
864 assign_resources(link);
865 }
866 }
867}
868
869static struct device_operations pci_domain_ops = {
870 .read_resources = domain_read_resources,
871 .set_resources = domain_set_resources,
872 .enable_resources = domain_enable_resources,
873 .init = NULL,
874 .scan_bus = pci_domain_scan_bus,
875
876#if CONFIG_MMCONF_SUPPORT_DEFAULT
877 .ops_pci_bus = &pci_ops_mmconf,
878#else
879 .ops_pci_bus = &pci_cf8_conf1,
880#endif
881};
882
883static void sysconf_init(device_t dev) // first node
884{
885 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
886 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
887}
888
889static void add_more_links(device_t dev, unsigned total_links)
890{
891 struct bus *link, *last = NULL;
892 int link_num;
893
894 for (link = dev->link_list; link; link = link->next)
895 last = link;
896
897 if (last) {
898 int links = total_links - last->link_num;
899 link_num = last->link_num;
900 if (links > 0) {
901 link = malloc(links*sizeof(*link));
902 if (!link)
903 die("Couldn't allocate more links!\n");
904 memset(link, 0, links*sizeof(*link));
905 last->next = link;
906 }
907 }
908 else {
909 link_num = -1;
910 link = malloc(total_links*sizeof(*link));
911 memset(link, 0, total_links*sizeof(*link));
912 dev->link_list = link;
913 }
914
915 for (link_num = link_num + 1; link_num < total_links; link_num++) {
916 link->link_num = link_num;
917 link->dev = dev;
918 link->next = link + 1;
919 last = link;
920 link = link->next;
921 }
922 last->next = NULL;
923}
924
zbao2c08f6a2012-07-02 15:32:58 +0800925static u32 cpu_bus_scan(device_t dev, u32 max)
926{
927 struct bus *cpu_bus;
928 device_t dev_mc;
929#if CONFIG_CBB
930 device_t pci_domain;
931#endif
932 int i,j;
933 int coreid_bits;
934 int core_max = 0;
935 unsigned ApicIdCoreIdSize;
936 unsigned core_nums;
937 int siblings = 0;
938 unsigned int family;
939
940#if CONFIG_CBB
941 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
942 if (dev_mc && dev_mc->bus) {
943 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
944 pci_domain = dev_mc->bus->dev;
945 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
946 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
947 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
948 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
949 } else {
950 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
951 }
952 printk(BIOS_DEBUG, "\n");
953 }
954 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
955 if (!dev_mc) {
956 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
957 if (dev_mc && dev_mc->bus) {
958 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
959 pci_domain = dev_mc->bus->dev;
960 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_PCI_DOMAIN)) {
961 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
962 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
963 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
964 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
965 while (dev_mc) {
966 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
967 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
968 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
969 dev_mc = dev_mc->sibling;
970 }
971 }
972 }
973 }
974 }
975#endif
976 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
977 if (!dev_mc) {
978 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
979 die("");
980 }
981 sysconf_init(dev_mc);
982#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
983 if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
984 if (pci_domain->link_list && !pci_domain->link_list->next) {
985 struct bus *new_link = new_link(pci_domain);
986 pci_domain->link_list->next = new_link;
987 new_link->link_num = 1;
988 new_link->dev = pci_domain;
989 new_link->children = 0;
990 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
991 }
992 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
993 }
994#endif
995
996 /* Get Max Number of cores(MNC) */
997 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
998 core_max = 1 << (coreid_bits & 0x000F); //mnc
999
1000 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1001 if (ApicIdCoreIdSize) {
1002 core_nums = (1 << ApicIdCoreIdSize) - 1;
1003 } else {
1004 core_nums = 3; //quad core
1005 }
1006
1007 /* Find which cpus are present */
1008 cpu_bus = dev->link_list;
1009 for (i = 0; i < node_nums; i++) {
1010 device_t cdb_dev, cpu;
1011 struct device_path cpu_path;
1012 unsigned busn, devn;
1013 struct bus *pbus;
1014
1015 busn = CONFIG_CBB;
1016 devn = CONFIG_CDB + i;
1017 pbus = dev_mc->bus;
1018#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1019 if (i >= 32) {
1020 busn--;
1021 devn -= 32;
1022 pbus = pci_domain->link_list->next;
1023 }
1024#endif
1025
1026 /* Find the cpu's pci device */
1027 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1028 if (!cdb_dev) {
1029 /* If I am probing things in a weird order
1030 * ensure all of the cpu's pci devices are found.
1031 */
1032 int fn;
1033 for(fn = 0; fn <= 5; fn++) { //FBDIMM?
1034 cdb_dev = pci_probe_dev(NULL, pbus,
1035 PCI_DEVFN(devn, fn));
1036 }
1037 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1038 } else {
1039 /* Ok, We need to set the links for that device.
1040 * otherwise the device under it will not be scanned
1041 */
1042 int linknum;
zbaod59d6242012-07-23 19:41:03 +08001043#if CONFIG_HT3_SUPPORT
zbao2c08f6a2012-07-02 15:32:58 +08001044 linknum = 8;
1045#else
1046 linknum = 4;
1047#endif
1048 add_more_links(cdb_dev, linknum);
1049 }
1050
1051 family = cpuid_eax(1);
1052 family = (family >> 20) & 0xFF;
1053 if (family == 1) { //f10
1054 u32 dword;
1055 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1056 dword = pci_read_config32(cdb_dev, 0xe8);
1057 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1058 } else if (family == 6) {//f15
1059 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1060 if (cdb_dev && cdb_dev->enabled) {
1061 siblings = pci_read_config32(cdb_dev, 0x84);
1062 siblings &= 0xFF;
1063 }
1064 } else {
1065 siblings = 0; //default one core
1066 }
1067 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1068 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1069
1070 for (j = 0; j <= siblings; j++ ) {
1071 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1072 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1073 u32 lapicid_start = 0;
1074
1075 /* Build the cpu device path */
1076 cpu_path.type = DEVICE_PATH_APIC;
1077 /*
1078 * APIC ID calucation is tightly coupled with AGESA v5 code.
1079 * This calculation MUST match the assignment calculation done
1080 * in LocalApicInitializationAtEarly() function.
1081 * And reference GetLocalApicIdForCore()
1082 *
1083 * Apply apic enumeration rules
1084 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1085 * put the local-APICs at m..z
1086 *
1087 * This is needed because many IO-APIC devices only have 4 bits
1088 * for their APIC id and therefore must reside at 0..15
1089 */
1090#ifndef CFG_PLAT_NUM_IO_APICS /* defined in mainboard buildOpts.c */
1091#define CFG_PLAT_NUM_IO_APICS 3
1092#endif
1093 if ((node_nums * core_max) + CFG_PLAT_NUM_IO_APICS >= 0x10) {
1094 lapicid_start = (CFG_PLAT_NUM_IO_APICS - 1) / core_max;
1095 lapicid_start = (lapicid_start + 1) * core_max;
1096 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1097 }
1098 cpu_path.apic.apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
1099 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
1100 i, j, cpu_path.apic.apic_id);
1101
1102 /* See if I can find the cpu */
1103 cpu = find_dev_path(cpu_bus, &cpu_path);
1104 /* Enable the cpu if I have the processor */
1105 if (cdb_dev && cdb_dev->enabled) {
1106 if (!cpu) {
1107 cpu = alloc_dev(cpu_bus, &cpu_path);
1108 }
1109 if (cpu) {
1110 cpu->enabled = 1;
1111 }
1112 }
1113 /* Disable the cpu if I don't have the processor */
1114 if (cpu && (!cdb_dev || !cdb_dev->enabled)) {
1115 cpu->enabled = 0;
1116 }
1117 /* Report what I have done */
1118 if (cpu) {
1119 cpu->path.apic.node_id = i;
1120 cpu->path.apic.core_id = j;
zbao2c08f6a2012-07-02 15:32:58 +08001121 printk(BIOS_DEBUG, "CPU: %s %s\n",
1122 dev_path(cpu), cpu->enabled?"enabled":"disabled");
1123 }
1124 } //j
1125 }
1126 return max;
1127}
1128
1129static void cpu_bus_init(device_t dev)
1130{
1131 initialize_cpus(dev->link_list);
1132}
1133
1134static void cpu_bus_noop(device_t dev)
1135{
1136}
1137
1138static void cpu_bus_read_resources(device_t dev)
1139{
1140#if CONFIG_MMCONF_SUPPORT
1141 struct resource *resource = new_resource(dev, 0xc0010058);
1142 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
1143 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096*256;
1144 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
1145 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
1146#endif
1147}
1148
1149static void cpu_bus_set_resources(device_t dev)
1150{
1151 struct resource *resource = find_resource(dev, 0xc0010058);
1152 if (resource) {
1153 report_resource_stored(dev, resource, " <mmconfig>");
1154 }
1155 pci_dev_set_resources(dev);
1156}
1157
1158static struct device_operations cpu_bus_ops = {
1159 .read_resources = cpu_bus_read_resources,
1160 .set_resources = cpu_bus_set_resources,
1161 .enable_resources = cpu_bus_noop,
1162 .init = cpu_bus_init,
1163 .scan_bus = cpu_bus_scan,
1164};
1165
1166static void root_complex_enable_dev(struct device *dev)
1167{
1168 /* Set the operations if it is a special bus type */
1169 if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
1170 dev->ops = &pci_domain_ops;
1171 } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
1172 dev->ops = &cpu_bus_ops;
1173 }
1174}
1175
1176struct chip_operations northbridge_amd_agesa_family15tn_root_complex_ops = {
1177 CHIP_NAME("AMD FAM15 Root Complex")
1178 .enable_dev = root_complex_enable_dev,
1179};