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