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