blob: d28b0469865400818a209282b8eaae528d0692b0 [file] [log] [blame]
Siyuan Wang3e32cc02013-07-09 17:16:20 +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>
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +030022#include <arch/acpi.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080023#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 <cpu/x86/lapic.h>
35#include <cpu/amd/mtrr.h>
36
37#include <Porting.h>
38#include <AGESA.h>
39#include <Options.h>
40#include <Topology.h>
41#include <cpu/amd/amdfam16.h>
42#include <cpuRegisters.h>
43#include "agesawrapper.h"
Kyösti Mälkki7b23ae02014-07-04 16:14:37 +030044#include <northbridge/amd/agesa/agesawrapper_call.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080045
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 + CONFIG_CDB >= 32
124 if ((CONFIG_CDB + 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 amdfam16_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 does not exist, usable
204 * @retval 0 resource exists, 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 *amdfam16_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 *amdfam16_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 amdfam16_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 = amdfam16_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 = amdfam16_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 = amdfam16_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 = amdfam16_nodeid(dev);
336 for (link = dev->link_list; link; link = link->next) {
337 if (link->children) {
338 amdfam16_link_read_bases(dev, nodeid, link->link_num);
339 }
340 }
Edward O'Callaghan66c65322014-11-21 01:43:38 +1100341
342 /*
343 * This MMCONF resource must be reserved in the PCI_DOMAIN.
344 * It is not honored by the coreboot resource allocator if it is in
345 * the APIC_CLUSTER.
346 */
347#if CONFIG_MMCONF_SUPPORT
348 struct resource *resource = new_resource(dev, 0xc0010058);
349 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
350 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096 * 256;
351 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
352 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
353#endif
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800354}
355
356static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
357{
358 resource_t rbase, rend;
359 unsigned reg, link_num;
360 char buf[50];
361
362 /* Make certain the resource has actually been set */
363 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
364 return;
365 }
366
367 /* If I have already stored this resource don't worry about it */
368 if (resource->flags & IORESOURCE_STORED) {
369 return;
370 }
371
372 /* Only handle PCI memory and IO resources */
373 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
374 return;
375
376 /* Ensure I am actually looking at a resource of function 1 */
377 if ((resource->index & 0xffff) < 0x1000) {
378 return;
379 }
380 /* Get the base address */
381 rbase = resource->base;
382
383 /* Get the limit (rounded up) */
384 rend = resource_end(resource);
385
386 /* Get the register and link */
387 reg = resource->index & 0xfff; // 4k
388 link_num = IOINDEX_LINK(resource->index);
389
390 if (resource->flags & IORESOURCE_IO) {
391 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
392 }
393 else if (resource->flags & IORESOURCE_MEM) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100394 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums);// [39:8]
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800395 }
396 resource->flags |= IORESOURCE_STORED;
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100397 snprintf(buf, sizeof (buf), " <node %x link %x>",
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800398 nodeid, link_num);
399 report_resource_stored(dev, resource, buf);
400}
401
402/**
403 * I tried to reuse the resource allocation code in set_resource()
404 * but it is too difficult to deal with the resource allocation magic.
405 */
406
407static void create_vga_resource(device_t dev, unsigned nodeid)
408{
409 struct bus *link;
410
411 /* find out which link the VGA card is connected,
412 * we only deal with the 'first' vga card */
413 for (link = dev->link_list; link; link = link->next) {
414 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
415#if CONFIG_MULTIPLE_VGA_ADAPTERS
416 extern device_t vga_pri; // the primary vga device, defined in device.c
417 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
418 link->secondary,link->subordinate);
419 /* We need to make sure the vga_pri is under the link */
420 if((vga_pri->bus->secondary >= link->secondary ) &&
421 (vga_pri->bus->secondary <= link->subordinate )
422 )
423#endif
424 break;
425 }
426 }
427
428 /* no VGA card installed */
429 if (link == NULL)
430 return;
431
432 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
433 set_vga_enable_reg(nodeid, sblink);
434}
435
436static void set_resources(device_t dev)
437{
438 unsigned nodeid;
439 struct bus *bus;
440 struct resource *res;
441
442 /* Find the nodeid */
443 nodeid = amdfam16_nodeid(dev);
444
445 create_vga_resource(dev, nodeid); //TODO: do we need this?
446
447 /* Set each resource we have found */
448 for (res = dev->resource_list; res; res = res->next) {
449 set_resource(dev, res, nodeid);
450 }
451
452 for (bus = dev->link_list; bus; bus = bus->next) {
453 if (bus->children) {
454 assign_resources(bus);
455 }
456 }
Edward O'Callaghan66c65322014-11-21 01:43:38 +1100457
458 /* Print the MMCONF region if it has been reserved. */
459 res = find_resource(dev, 0xc0010058);
460 if (res) {
461 report_resource_stored(dev, res, " <mmconfig>");
462 }
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800463}
464
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800465#if 0 /* TODO: Check if needed. */
466static unsigned scan_chains(device_t dev, unsigned max)
467{
468 unsigned nodeid;
469 struct bus *link;
470 device_t io_hub = NULL;
471 u32 next_unitid = 0x18;
472 nodeid = amdfam16_nodeid(dev);
473 if (nodeid == 0) {
474 for (link = dev->link_list; link; link = link->next) {
475 //if (link->link_num == sblink) { /* devicetree put IO Hub on link_lsit[sblink] */
476 if (link->link_num == 0) { /* devicetree put IO Hub on link_lsit[0] */
477 io_hub = link->children;
478 if (!io_hub || !io_hub->enabled) {
479 die("I can't find the IO Hub, or IO Hub not enabled, please check the device tree.\n");
480 }
481 /* Now that nothing is overlapping it is safe to scan the children. */
482 max = pci_scan_bus(link, 0x00, ((next_unitid - 1) << 3) | 7, 0);
483 }
484 }
485 }
486 return max;
487}
488#endif
489static struct device_operations northbridge_operations = {
490 .read_resources = read_resources,
491 .set_resources = set_resources,
492 .enable_resources = pci_dev_enable_resources,
Edward O'Callaghand994ef12014-11-21 02:22:33 +1100493 .init = DEVICE_NOOP,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800494 //.scan_bus = scan_chains, /* TODO: */
495 .enable = 0,
496 .ops_pci = 0,
497};
498
499static const struct pci_driver family16_northbridge __pci_driver = {
500 .ops = &northbridge_operations,
501 .vendor = PCI_VENDOR_ID_AMD,
502 .device = PCI_DEVICE_ID_AMD_16H_MODEL_000F_NB_HT,
503};
504
505static const struct pci_driver family10_northbridge __pci_driver = {
506 .ops = &northbridge_operations,
507 .vendor = PCI_VENDOR_ID_AMD,
508 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
509};
510
511struct chip_operations northbridge_amd_agesa_family16kb_ops = {
512 CHIP_NAME("AMD FAM16 Northbridge")
513 .enable_dev = 0,
514};
515
516static void domain_read_resources(device_t dev)
517{
518 unsigned reg;
519
520 /* Find the already assigned resource pairs */
521 get_fx_devs();
522 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
523 u32 base, limit;
524 base = f1_read_config32(reg);
525 limit = f1_read_config32(reg + 0x04);
526 /* Is this register allocated? */
527 if ((base & 3) != 0) {
528 unsigned nodeid, reg_link;
529 device_t reg_dev;
530 if (reg<0xc0) { // mmio
531 nodeid = (limit & 0xf) + (base&0x30);
532 } else { // io
533 nodeid = (limit & 0xf) + ((base>>4)&0x30);
534 }
535 reg_link = (limit >> 4) & 7;
536 reg_dev = __f0_dev[nodeid];
537 if (reg_dev) {
538 /* Reserve the resource */
539 struct resource *res;
540 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
541 if (res) {
542 res->flags = 1;
543 }
544 }
545 }
546 }
547 /* FIXME: do we need to check extend conf space?
548 I don't believe that much preset value */
549
550#if !CONFIG_PCI_64BIT_PREF_MEM
551 pci_domain_read_resources(dev);
552
553#else
554 struct bus *link;
555 struct resource *resource;
556 for (link=dev->link_list; link; link = link->next) {
557 /* Initialize the system wide io space constraints */
558 resource = new_resource(dev, 0|(link->link_num<<2));
559 resource->base = 0x400;
560 resource->limit = 0xffffUL;
561 resource->flags = IORESOURCE_IO;
562
563 /* Initialize the system wide prefetchable memory resources constraints */
564 resource = new_resource(dev, 1|(link->link_num<<2));
565 resource->limit = 0xfcffffffffULL;
566 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
567
568 /* Initialize the system wide memory resources constraints */
569 resource = new_resource(dev, 2|(link->link_num<<2));
570 resource->limit = 0xfcffffffffULL;
571 resource->flags = IORESOURCE_MEM;
572 }
573#endif
574}
575
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800576static void domain_enable_resources(device_t dev)
577{
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +0300578 if (acpi_is_wakeup_s3())
Kyösti Mälkki7b23ae02014-07-04 16:14:37 +0300579 AGESAWRAPPER(fchs3laterestore);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800580
581 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkki7b23ae02014-07-04 16:14:37 +0300582 if (!acpi_is_wakeup_s3())
583 AGESAWRAPPER(amdinitmid);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800584
585 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
586}
587
588#if CONFIG_HW_MEM_HOLE_SIZEK != 0
589struct hw_mem_hole_info {
590 unsigned hole_startk;
591 int node_id;
592};
593static struct hw_mem_hole_info get_hw_mem_hole_info(void)
594{
595 struct hw_mem_hole_info mem_hole;
596 int i;
597 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
598 mem_hole.node_id = -1;
599 for (i = 0; i < node_nums; i++) {
600 dram_base_mask_t d;
601 u32 hole;
602 d = get_dram_base_mask(i);
603 if (!(d.mask & 1)) continue; // no memory on this node
604 hole = pci_read_config32(__f1_dev[i], 0xf0);
605 if (hole & 2) { // we find the hole
606 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
607 mem_hole.node_id = i; // record the node No with hole
608 break; // only one hole
609 }
610 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300611
612 /* We need to double check if there is special set on base reg and limit reg
613 * are not continuous instead of hole, it will find out its hole_startk.
614 */
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800615 if (mem_hole.node_id == -1) {
616 resource_t limitk_pri = 0;
617 for (i=0; i<node_nums; i++) {
618 dram_base_mask_t d;
619 resource_t base_k, limit_k;
620 d = get_dram_base_mask(i);
621 if (!(d.base & 1)) continue;
622 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
623 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
624 if (limitk_pri != base_k) { // we find the hole
625 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
626 mem_hole.node_id = i;
627 break; //only one hole
628 }
629 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
630 limitk_pri = limit_k;
631 }
632 }
633 return mem_hole;
634}
635#endif
636
637#define ONE_MB_SHIFT 20
638
639static void setup_uma_memory(void)
640{
641#if CONFIG_GFXUMA
642 uint32_t topmem = (uint32_t) bsp_topmem();
643 uint32_t sys_mem;
644
645 /* refer to UMA Size Consideration in Family16h BKDG. */
646 /* Please reference MemNGetUmaSizeOR () */
647 /*
648 * Total system memory UMASize
649 * >= 2G 512M
650 * >=1G 256M
651 * <1G 64M
652 */
653 sys_mem = topmem + (16 << ONE_MB_SHIFT); // Ignore 16MB allocated for C6 when finding UMA size
654 if ((bsp_topmem2()>>32) || (sys_mem >= 2048 << ONE_MB_SHIFT)) {
655 uma_memory_size = 512 << ONE_MB_SHIFT;
656 } else if (sys_mem >= 1024 << ONE_MB_SHIFT) {
657 uma_memory_size = 256 << ONE_MB_SHIFT;
658 } else {
659 uma_memory_size = 64 << ONE_MB_SHIFT;
660 }
661 uma_memory_base = topmem - uma_memory_size; /* TOP_MEM1 */
662
663 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
664 __func__, uma_memory_size, uma_memory_base);
665
666 /* TODO: TOP_MEM2 */
667#endif
668}
669
670
671static void domain_set_resources(device_t dev)
672{
673#if CONFIG_PCI_64BIT_PREF_MEM
674 struct resource *io, *mem1, *mem2;
675 struct resource *res;
676#endif
677 unsigned long mmio_basek;
678 u32 pci_tolm;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300679 u64 ramtop = 0;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800680 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
687#if CONFIG_PCI_64BIT_PREF_MEM
688
689 for (link = dev->link_list; link; link = link->next) {
690 /* Now reallocate the pci resources memory with the
691 * highest addresses I can manage.
692 */
693 mem1 = find_resource(dev, 1|(link->link_num<<2));
694 mem2 = find_resource(dev, 2|(link->link_num<<2));
695
696 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
697 mem1->base, mem1->limit, mem1->size, mem1->align);
698 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
699 mem2->base, mem2->limit, mem2->size, mem2->align);
700
701 /* See if both resources have roughly the same limits */
702 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
703 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
704 {
705 /* If so place the one with the most stringent alignment first */
706 if (mem2->align > mem1->align) {
707 struct resource *tmp;
708 tmp = mem1;
709 mem1 = mem2;
710 mem2 = tmp;
711 }
712 /* Now place the memory as high up as it will go */
713 mem2->base = resource_max(mem2);
714 mem1->limit = mem2->base - 1;
715 mem1->base = resource_max(mem1);
716 }
717 else {
718 /* Place the resources as high up as they will go */
719 mem2->base = resource_max(mem2);
720 mem1->base = resource_max(mem1);
721 }
722
723 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
724 mem1->base, mem1->limit, mem1->size, mem1->align);
725 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
726 mem2->base, mem2->limit, mem2->size, mem2->align);
727 }
728
729 for (res = &dev->resource_list; res; res = res->next)
730 {
731 res->flags |= IORESOURCE_ASSIGNED;
732 res->flags |= IORESOURCE_STORED;
733 report_resource_stored(dev, res, "");
734 }
735#endif
736
737 pci_tolm = 0xffffffffUL;
738 for (link = dev->link_list; link; link = link->next) {
739 pci_tolm = find_pci_tolm(link);
740 }
741
742 // FIXME handle interleaved nodes. If you fix this here, please fix
743 // amdk8, too.
744 mmio_basek = pci_tolm >> 10;
745 /* Round mmio_basek to something the processor can support */
746 mmio_basek &= ~((1 << 6) -1);
747
748 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
749 // MMIO hole. If you fix this here, please fix amdk8, too.
750 /* Round the mmio hole to 64M */
751 mmio_basek &= ~((64*1024) - 1);
752
753#if CONFIG_HW_MEM_HOLE_SIZEK != 0
754 /* if the hw mem hole is already set in raminit stage, here we will compare
755 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
756 * use hole_basek as mmio_basek and we don't need to reset hole.
757 * otherwise We reset the hole to the mmio_basek
758 */
759
760 mem_hole = get_hw_mem_hole_info();
761
762 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
763 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
764 mmio_basek = mem_hole.hole_startk;
765 reset_memhole = 0;
766 }
767#endif
768
769 idx = 0x10;
770 for (i = 0; i < node_nums; i++) {
771 dram_base_mask_t d;
772 resource_t basek, limitk, sizek; // 4 1T
773
774 d = get_dram_base_mask(i);
775
776 if (!(d.mask & 1)) continue;
777 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100778 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800779
780 sizek = limitk - basek;
781
782 /* see if we need a hole from 0xa0000 to 0xbffff */
783 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
784 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
785 idx += 0x10;
786 basek = (8*64)+(16*16);
787 sizek = limitk - ((8*64)+(16*16));
788
789 }
790
791 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
792
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300793 /* split the region to accommodate pci memory space */
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800794 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
795 if (basek <= mmio_basek) {
796 unsigned pre_sizek;
797 pre_sizek = mmio_basek - basek;
798 if (pre_sizek>0) {
799 ram_resource(dev, (idx | i), basek, pre_sizek);
800 idx += 0x10;
801 sizek -= pre_sizek;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300802 if (!ramtop)
803 ramtop = mmio_basek * 1024;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800804 }
805 basek = mmio_basek;
806 }
807 if ((basek + sizek) <= 4*1024*1024) {
808 sizek = 0;
809 }
810 else {
811 uint64_t topmem2 = bsp_topmem2();
812 basek = 4*1024*1024;
813 sizek = topmem2/1024 - basek;
814 }
815 }
816
817 ram_resource(dev, (idx | i), basek, sizek);
818 idx += 0x10;
819 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
820 i, mmio_basek, basek, limitk);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300821 if (!ramtop)
822 ramtop = limitk * 1024;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800823 }
824
825#if CONFIG_GFXUMA
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300826 set_top_of_ram(uma_memory_base);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800827 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300828#else
829 set_top_of_ram(ramtop);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800830#endif
831
832 for(link = dev->link_list; link; link = link->next) {
833 if (link->children) {
834 assign_resources(link);
835 }
836 }
837}
838
839static struct device_operations pci_domain_ops = {
840 .read_resources = domain_read_resources,
841 .set_resources = domain_set_resources,
842 .enable_resources = domain_enable_resources,
843 .init = NULL,
844 .scan_bus = pci_domain_scan_bus,
845 .ops_pci_bus = pci_bus_default_ops,
846};
847
848static void sysconf_init(device_t dev) // first node
849{
850 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
851 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
852}
853
854static void add_more_links(device_t dev, unsigned total_links)
855{
856 struct bus *link, *last = NULL;
857 int link_num;
858
859 for (link = dev->link_list; link; link = link->next)
860 last = link;
861
862 if (last) {
863 int links = total_links - last->link_num;
864 link_num = last->link_num;
865 if (links > 0) {
866 link = malloc(links*sizeof(*link));
867 if (!link)
868 die("Couldn't allocate more links!\n");
869 memset(link, 0, links*sizeof(*link));
870 last->next = link;
871 }
872 }
873 else {
874 link_num = -1;
875 link = malloc(total_links*sizeof(*link));
876 memset(link, 0, total_links*sizeof(*link));
877 dev->link_list = link;
878 }
879
880 for (link_num = link_num + 1; link_num < total_links; link_num++) {
881 link->link_num = link_num;
882 link->dev = dev;
883 link->next = link + 1;
884 last = link;
885 link = link->next;
886 }
887 last->next = NULL;
888}
889
890static u32 cpu_bus_scan(device_t dev, u32 max)
891{
892 struct bus *cpu_bus;
893 device_t dev_mc;
894#if CONFIG_CBB
895 device_t pci_domain;
896#endif
897 int i,j;
898 int coreid_bits;
899 int core_max = 0;
900 unsigned ApicIdCoreIdSize;
901 unsigned core_nums;
902 int siblings = 0;
903 unsigned int family;
904
905#if CONFIG_CBB
906 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
907 if (dev_mc && dev_mc->bus) {
908 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
909 pci_domain = dev_mc->bus->dev;
910 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
911 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
912 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
913 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
914 } else {
915 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
916 }
917 printk(BIOS_DEBUG, "\n");
918 }
919 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
920 if (!dev_mc) {
921 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
922 if (dev_mc && dev_mc->bus) {
923 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
924 pci_domain = dev_mc->bus->dev;
925 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
926 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
927 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
928 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
929 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
930 while (dev_mc) {
931 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
932 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
933 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
934 dev_mc = dev_mc->sibling;
935 }
936 }
937 }
938 }
939 }
940#endif
941 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
942 if (!dev_mc) {
943 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
944 die("");
945 }
946 sysconf_init(dev_mc);
947#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
948 if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
949 if (pci_domain->link_list && !pci_domain->link_list->next) {
950 struct bus *new_link = new_link(pci_domain);
951 pci_domain->link_list->next = new_link;
952 new_link->link_num = 1;
953 new_link->dev = pci_domain;
954 new_link->children = 0;
955 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
956 }
957 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
958 }
959#endif
960
961 /* Get Max Number of cores(MNC) */
962 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
963 core_max = 1 << (coreid_bits & 0x000F); //mnc
964
965 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
966 if (ApicIdCoreIdSize) {
967 core_nums = (1 << ApicIdCoreIdSize) - 1;
968 } else {
969 core_nums = 3; //quad core
970 }
971
972 /* Find which cpus are present */
973 cpu_bus = dev->link_list;
974 for (i = 0; i < node_nums; i++) {
975 device_t cdb_dev;
976 unsigned busn, devn;
977 struct bus *pbus;
978
979 busn = CONFIG_CBB;
980 devn = CONFIG_CDB + i;
981 pbus = dev_mc->bus;
982#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
983 if (i >= 32) {
984 busn--;
985 devn -= 32;
986 pbus = pci_domain->link_list->next;
987 }
988#endif
989
990 /* Find the cpu's pci device */
991 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
992 if (!cdb_dev) {
993 /* If I am probing things in a weird order
994 * ensure all of the cpu's pci devices are found.
995 */
996 int fn;
997 for(fn = 0; fn <= 5; fn++) { //FBDIMM?
998 cdb_dev = pci_probe_dev(NULL, pbus,
999 PCI_DEVFN(devn, fn));
1000 }
1001 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1002 } else {
1003 /* Ok, We need to set the links for that device.
1004 * otherwise the device under it will not be scanned
1005 */
1006 int linknum;
1007#if CONFIG_HT3_SUPPORT
1008 linknum = 8;
1009#else
1010 linknum = 4;
1011#endif
1012 add_more_links(cdb_dev, linknum);
1013 }
1014
1015 family = cpuid_eax(1);
1016 family = (family >> 20) & 0xFF;
1017 if (family == 1) { //f10
1018 u32 dword;
1019 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1020 dword = pci_read_config32(cdb_dev, 0xe8);
1021 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1022 } else if (family == 7) {//f16
1023 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1024 if (cdb_dev && cdb_dev->enabled) {
1025 siblings = pci_read_config32(cdb_dev, 0x84);
1026 siblings &= 0xFF;
1027 }
1028 } else {
1029 siblings = 0; //default one core
1030 }
1031 int enable_node = cdb_dev && cdb_dev->enabled;
1032 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1033 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1034
1035 for (j = 0; j <= siblings; j++ ) {
1036 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1037 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1038 u32 lapicid_start = 0;
1039
1040 /*
1041 * APIC ID calucation is tightly coupled with AGESA v5 code.
1042 * This calculation MUST match the assignment calculation done
1043 * in LocalApicInitializationAtEarly() function.
1044 * And reference GetLocalApicIdForCore()
1045 *
1046 * Apply apic enumeration rules
1047 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1048 * put the local-APICs at m..z
1049 *
1050 * This is needed because many IO-APIC devices only have 4 bits
1051 * for their APIC id and therefore must reside at 0..15
1052 */
1053#ifndef CFG_PLAT_NUM_IO_APICS /* defined in mainboard buildOpts.c */
1054#define CFG_PLAT_NUM_IO_APICS 3
1055#endif
1056 if ((node_nums * core_max) + CFG_PLAT_NUM_IO_APICS >= 0x10) {
1057 lapicid_start = (CFG_PLAT_NUM_IO_APICS - 1) / core_max;
1058 lapicid_start = (lapicid_start + 1) * core_max;
1059 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1060 }
1061 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
1062 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
1063 i, j, apic_id);
1064
1065 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1066 if (cpu)
1067 amd_cpu_topology(cpu, i, j);
1068 } //j
1069 }
1070 return max;
1071}
1072
1073static void cpu_bus_init(device_t dev)
1074{
1075 initialize_cpus(dev->link_list);
1076}
1077
Siyuan Wang3e32cc02013-07-09 17:16:20 +08001078static struct device_operations cpu_bus_ops = {
Edward O'Callaghan66c65322014-11-21 01:43:38 +11001079 .read_resources = DEVICE_NOOP,
1080 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001081 .enable_resources = DEVICE_NOOP,
Siyuan Wang3e32cc02013-07-09 17:16:20 +08001082 .init = cpu_bus_init,
1083 .scan_bus = cpu_bus_scan,
1084};
1085
1086static void root_complex_enable_dev(struct device *dev)
1087{
1088 static int done = 0;
1089
1090 /* Do not delay UMA setup, as a device on the PCI bus may evaluate
1091 the global uma_memory variables already in its enable function. */
1092 if (!done) {
1093 setup_bsp_ramtop();
1094 setup_uma_memory();
1095 done = 1;
1096 }
1097
1098 /* Set the operations if it is a special bus type */
1099 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1100 dev->ops = &pci_domain_ops;
1101 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1102 dev->ops = &cpu_bus_ops;
1103 }
1104}
1105
1106struct chip_operations northbridge_amd_agesa_family16kb_root_complex_ops = {
1107 CHIP_NAME("AMD FAM16 Root Complex")
1108 .enable_dev = root_complex_enable_dev,
1109};
Bruce Griffith76db07e2013-07-07 02:06:53 -06001110
1111/*********************************************************************
1112 * Change the vendor / device IDs to match the generic VBIOS header. *
1113 *********************************************************************/
1114u32 map_oprom_vendev(u32 vendev)
1115{
1116 u32 new_vendev = vendev;
1117
1118 switch(vendev) {
1119 case 0x10029830:
1120 case 0x10029831:
1121 case 0x10029832:
1122 case 0x10029833:
1123 case 0x10029834:
1124 case 0x10029835:
1125 case 0x10029836:
1126 case 0x10029837:
1127 case 0x10029838:
1128 case 0x10029839:
1129 case 0x1002983A:
1130 case 0x1002983D:
1131 new_vendev = 0x10029830; // This is the default value in AMD-generated VBIOS
1132 break;
1133 default:
1134 break;
1135 }
1136
1137 if (vendev != new_vendev)
1138 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1139
1140 return new_vendev;
1141}