blob: 01eaa208f0a671fd96155ad7f263349821430a26 [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#include "northbridge.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{
124#if MAX_NODE_NUMS + CONFIG_CDB >= 32
125 if ((CONFIG_CDB + nodeid) < 32) {
126 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 amdfam16_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 does not exist, usable
205 * @retval 0 resource exists, 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 *amdfam16_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 *amdfam16_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 amdfam16_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 = amdfam16_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 = amdfam16_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 = amdfam16_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 = amdfam16_nodeid(dev);
337 for (link = dev->link_list; link; link = link->next) {
338 if (link->children) {
339 amdfam16_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) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100382 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 +0800383 }
384 resource->flags |= IORESOURCE_STORED;
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100385 snprintf(buf, sizeof (buf), " <node %x link %x>",
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800386 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) {
403#if CONFIG_MULTIPLE_VGA_ADAPTERS
404 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 = amdfam16_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
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800447#if 0 /* TODO: Check if needed. */
448static unsigned scan_chains(device_t dev, unsigned max)
449{
450 unsigned nodeid;
451 struct bus *link;
452 device_t io_hub = NULL;
453 u32 next_unitid = 0x18;
454 nodeid = amdfam16_nodeid(dev);
455 if (nodeid == 0) {
456 for (link = dev->link_list; link; link = link->next) {
457 //if (link->link_num == sblink) { /* devicetree put IO Hub on link_lsit[sblink] */
458 if (link->link_num == 0) { /* devicetree put IO Hub on link_lsit[0] */
459 io_hub = link->children;
460 if (!io_hub || !io_hub->enabled) {
461 die("I can't find the IO Hub, or IO Hub not enabled, please check the device tree.\n");
462 }
463 /* Now that nothing is overlapping it is safe to scan the children. */
464 max = pci_scan_bus(link, 0x00, ((next_unitid - 1) << 3) | 7, 0);
465 }
466 }
467 }
468 return max;
469}
470#endif
471static struct device_operations northbridge_operations = {
472 .read_resources = read_resources,
473 .set_resources = set_resources,
474 .enable_resources = pci_dev_enable_resources,
Edward O'Callaghand994ef12014-11-21 02:22:33 +1100475 .init = DEVICE_NOOP,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800476 //.scan_bus = scan_chains, /* TODO: */
477 .enable = 0,
478 .ops_pci = 0,
479};
480
481static const struct pci_driver family16_northbridge __pci_driver = {
482 .ops = &northbridge_operations,
483 .vendor = PCI_VENDOR_ID_AMD,
484 .device = PCI_DEVICE_ID_AMD_16H_MODEL_000F_NB_HT,
485};
486
487static const struct pci_driver family10_northbridge __pci_driver = {
488 .ops = &northbridge_operations,
489 .vendor = PCI_VENDOR_ID_AMD,
490 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
491};
492
493struct chip_operations northbridge_amd_agesa_family16kb_ops = {
494 CHIP_NAME("AMD FAM16 Northbridge")
495 .enable_dev = 0,
496};
497
498static void domain_read_resources(device_t dev)
499{
500 unsigned reg;
501
502 /* Find the already assigned resource pairs */
503 get_fx_devs();
504 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
505 u32 base, limit;
506 base = f1_read_config32(reg);
507 limit = f1_read_config32(reg + 0x04);
508 /* Is this register allocated? */
509 if ((base & 3) != 0) {
510 unsigned nodeid, reg_link;
511 device_t reg_dev;
512 if (reg<0xc0) { // mmio
513 nodeid = (limit & 0xf) + (base&0x30);
514 } else { // io
515 nodeid = (limit & 0xf) + ((base>>4)&0x30);
516 }
517 reg_link = (limit >> 4) & 7;
518 reg_dev = __f0_dev[nodeid];
519 if (reg_dev) {
520 /* Reserve the resource */
521 struct resource *res;
522 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
523 if (res) {
524 res->flags = 1;
525 }
526 }
527 }
528 }
529 /* FIXME: do we need to check extend conf space?
530 I don't believe that much preset value */
531
532#if !CONFIG_PCI_64BIT_PREF_MEM
533 pci_domain_read_resources(dev);
534
535#else
536 struct bus *link;
537 struct resource *resource;
538 for (link=dev->link_list; link; link = link->next) {
539 /* Initialize the system wide io space constraints */
540 resource = new_resource(dev, 0|(link->link_num<<2));
541 resource->base = 0x400;
542 resource->limit = 0xffffUL;
543 resource->flags = IORESOURCE_IO;
544
545 /* Initialize the system wide prefetchable memory resources constraints */
546 resource = new_resource(dev, 1|(link->link_num<<2));
547 resource->limit = 0xfcffffffffULL;
548 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
549
550 /* Initialize the system wide memory resources constraints */
551 resource = new_resource(dev, 2|(link->link_num<<2));
552 resource->limit = 0xfcffffffffULL;
553 resource->flags = IORESOURCE_MEM;
554 }
555#endif
556}
557
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800558static void domain_enable_resources(device_t dev)
559{
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +0300560 if (acpi_is_wakeup_s3())
Kyösti Mälkki7b23ae02014-07-04 16:14:37 +0300561 AGESAWRAPPER(fchs3laterestore);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800562
563 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkki7b23ae02014-07-04 16:14:37 +0300564 if (!acpi_is_wakeup_s3())
565 AGESAWRAPPER(amdinitmid);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800566
567 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
568}
569
570#if CONFIG_HW_MEM_HOLE_SIZEK != 0
571struct hw_mem_hole_info {
572 unsigned hole_startk;
573 int node_id;
574};
575static struct hw_mem_hole_info get_hw_mem_hole_info(void)
576{
577 struct hw_mem_hole_info mem_hole;
578 int i;
579 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
580 mem_hole.node_id = -1;
581 for (i = 0; i < node_nums; i++) {
582 dram_base_mask_t d;
583 u32 hole;
584 d = get_dram_base_mask(i);
585 if (!(d.mask & 1)) continue; // no memory on this node
586 hole = pci_read_config32(__f1_dev[i], 0xf0);
587 if (hole & 2) { // we find the hole
588 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
589 mem_hole.node_id = i; // record the node No with hole
590 break; // only one hole
591 }
592 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300593
594 /* We need to double check if there is special set on base reg and limit reg
595 * are not continuous instead of hole, it will find out its hole_startk.
596 */
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800597 if (mem_hole.node_id == -1) {
598 resource_t limitk_pri = 0;
599 for (i=0; i<node_nums; i++) {
600 dram_base_mask_t d;
601 resource_t base_k, limit_k;
602 d = get_dram_base_mask(i);
603 if (!(d.base & 1)) continue;
604 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
605 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
606 if (limitk_pri != base_k) { // we find the hole
607 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
608 mem_hole.node_id = i;
609 break; //only one hole
610 }
611 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
612 limitk_pri = limit_k;
613 }
614 }
615 return mem_hole;
616}
617#endif
618
619#define ONE_MB_SHIFT 20
620
621static void setup_uma_memory(void)
622{
623#if CONFIG_GFXUMA
624 uint32_t topmem = (uint32_t) bsp_topmem();
625 uint32_t sys_mem;
626
627 /* refer to UMA Size Consideration in Family16h BKDG. */
628 /* Please reference MemNGetUmaSizeOR () */
629 /*
630 * Total system memory UMASize
631 * >= 2G 512M
632 * >=1G 256M
633 * <1G 64M
634 */
635 sys_mem = topmem + (16 << ONE_MB_SHIFT); // Ignore 16MB allocated for C6 when finding UMA size
636 if ((bsp_topmem2()>>32) || (sys_mem >= 2048 << ONE_MB_SHIFT)) {
637 uma_memory_size = 512 << ONE_MB_SHIFT;
638 } else if (sys_mem >= 1024 << ONE_MB_SHIFT) {
639 uma_memory_size = 256 << ONE_MB_SHIFT;
640 } else {
641 uma_memory_size = 64 << ONE_MB_SHIFT;
642 }
643 uma_memory_base = topmem - uma_memory_size; /* TOP_MEM1 */
644
645 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
646 __func__, uma_memory_size, uma_memory_base);
647
648 /* TODO: TOP_MEM2 */
649#endif
650}
651
652
653static void domain_set_resources(device_t dev)
654{
655#if CONFIG_PCI_64BIT_PREF_MEM
656 struct resource *io, *mem1, *mem2;
657 struct resource *res;
658#endif
659 unsigned long mmio_basek;
660 u32 pci_tolm;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300661 u64 ramtop = 0;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800662 int i, idx;
663 struct bus *link;
664#if CONFIG_HW_MEM_HOLE_SIZEK != 0
665 struct hw_mem_hole_info mem_hole;
666 u32 reset_memhole = 1;
667#endif
668
669#if CONFIG_PCI_64BIT_PREF_MEM
670
671 for (link = dev->link_list; link; link = link->next) {
672 /* Now reallocate the pci resources memory with the
673 * highest addresses I can manage.
674 */
675 mem1 = find_resource(dev, 1|(link->link_num<<2));
676 mem2 = find_resource(dev, 2|(link->link_num<<2));
677
678 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
679 mem1->base, mem1->limit, mem1->size, mem1->align);
680 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
681 mem2->base, mem2->limit, mem2->size, mem2->align);
682
683 /* See if both resources have roughly the same limits */
684 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
685 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
686 {
687 /* If so place the one with the most stringent alignment first */
688 if (mem2->align > mem1->align) {
689 struct resource *tmp;
690 tmp = mem1;
691 mem1 = mem2;
692 mem2 = tmp;
693 }
694 /* Now place the memory as high up as it will go */
695 mem2->base = resource_max(mem2);
696 mem1->limit = mem2->base - 1;
697 mem1->base = resource_max(mem1);
698 }
699 else {
700 /* Place the resources as high up as they will go */
701 mem2->base = resource_max(mem2);
702 mem1->base = resource_max(mem1);
703 }
704
705 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
706 mem1->base, mem1->limit, mem1->size, mem1->align);
707 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
708 mem2->base, mem2->limit, mem2->size, mem2->align);
709 }
710
711 for (res = &dev->resource_list; res; res = res->next)
712 {
713 res->flags |= IORESOURCE_ASSIGNED;
714 res->flags |= IORESOURCE_STORED;
715 report_resource_stored(dev, res, "");
716 }
717#endif
718
719 pci_tolm = 0xffffffffUL;
720 for (link = dev->link_list; link; link = link->next) {
721 pci_tolm = find_pci_tolm(link);
722 }
723
724 // FIXME handle interleaved nodes. If you fix this here, please fix
725 // amdk8, too.
726 mmio_basek = pci_tolm >> 10;
727 /* Round mmio_basek to something the processor can support */
728 mmio_basek &= ~((1 << 6) -1);
729
730 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
731 // MMIO hole. If you fix this here, please fix amdk8, too.
732 /* Round the mmio hole to 64M */
733 mmio_basek &= ~((64*1024) - 1);
734
735#if CONFIG_HW_MEM_HOLE_SIZEK != 0
736 /* if the hw mem hole is already set in raminit stage, here we will compare
737 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
738 * use hole_basek as mmio_basek and we don't need to reset hole.
739 * otherwise We reset the hole to the mmio_basek
740 */
741
742 mem_hole = get_hw_mem_hole_info();
743
744 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
745 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
746 mmio_basek = mem_hole.hole_startk;
747 reset_memhole = 0;
748 }
749#endif
750
751 idx = 0x10;
752 for (i = 0; i < node_nums; i++) {
753 dram_base_mask_t d;
754 resource_t basek, limitk, sizek; // 4 1T
755
756 d = get_dram_base_mask(i);
757
758 if (!(d.mask & 1)) continue;
759 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100760 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800761
762 sizek = limitk - basek;
763
764 /* see if we need a hole from 0xa0000 to 0xbffff */
765 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
766 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
767 idx += 0x10;
768 basek = (8*64)+(16*16);
769 sizek = limitk - ((8*64)+(16*16));
770
771 }
772
773 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
774
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300775 /* split the region to accommodate pci memory space */
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800776 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
777 if (basek <= mmio_basek) {
778 unsigned pre_sizek;
779 pre_sizek = mmio_basek - basek;
780 if (pre_sizek>0) {
781 ram_resource(dev, (idx | i), basek, pre_sizek);
782 idx += 0x10;
783 sizek -= pre_sizek;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300784 if (!ramtop)
785 ramtop = mmio_basek * 1024;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800786 }
787 basek = mmio_basek;
788 }
789 if ((basek + sizek) <= 4*1024*1024) {
790 sizek = 0;
791 }
792 else {
793 uint64_t topmem2 = bsp_topmem2();
794 basek = 4*1024*1024;
795 sizek = topmem2/1024 - basek;
796 }
797 }
798
799 ram_resource(dev, (idx | i), basek, sizek);
800 idx += 0x10;
801 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
802 i, mmio_basek, basek, limitk);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300803 if (!ramtop)
804 ramtop = limitk * 1024;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800805 }
806
807#if CONFIG_GFXUMA
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300808 set_top_of_ram(uma_memory_base);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800809 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300810#else
811 set_top_of_ram(ramtop);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800812#endif
813
814 for(link = dev->link_list; link; link = link->next) {
815 if (link->children) {
816 assign_resources(link);
817 }
818 }
819}
820
821static struct device_operations pci_domain_ops = {
822 .read_resources = domain_read_resources,
823 .set_resources = domain_set_resources,
824 .enable_resources = domain_enable_resources,
825 .init = NULL,
826 .scan_bus = pci_domain_scan_bus,
827 .ops_pci_bus = pci_bus_default_ops,
828};
829
830static void sysconf_init(device_t dev) // first node
831{
832 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
833 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
834}
835
836static void add_more_links(device_t dev, unsigned total_links)
837{
838 struct bus *link, *last = NULL;
839 int link_num;
840
841 for (link = dev->link_list; link; link = link->next)
842 last = link;
843
844 if (last) {
845 int links = total_links - last->link_num;
846 link_num = last->link_num;
847 if (links > 0) {
848 link = malloc(links*sizeof(*link));
849 if (!link)
850 die("Couldn't allocate more links!\n");
851 memset(link, 0, links*sizeof(*link));
852 last->next = link;
853 }
854 }
855 else {
856 link_num = -1;
857 link = malloc(total_links*sizeof(*link));
858 memset(link, 0, total_links*sizeof(*link));
859 dev->link_list = link;
860 }
861
862 for (link_num = link_num + 1; link_num < total_links; link_num++) {
863 link->link_num = link_num;
864 link->dev = dev;
865 link->next = link + 1;
866 last = link;
867 link = link->next;
868 }
869 last->next = NULL;
870}
871
872static u32 cpu_bus_scan(device_t dev, u32 max)
873{
874 struct bus *cpu_bus;
875 device_t dev_mc;
876#if CONFIG_CBB
877 device_t pci_domain;
878#endif
879 int i,j;
880 int coreid_bits;
881 int core_max = 0;
882 unsigned ApicIdCoreIdSize;
883 unsigned core_nums;
884 int siblings = 0;
885 unsigned int family;
886
887#if CONFIG_CBB
888 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
889 if (dev_mc && dev_mc->bus) {
890 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
891 pci_domain = dev_mc->bus->dev;
892 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
893 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
894 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
895 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
896 } else {
897 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
898 }
899 printk(BIOS_DEBUG, "\n");
900 }
901 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
902 if (!dev_mc) {
903 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
904 if (dev_mc && dev_mc->bus) {
905 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
906 pci_domain = dev_mc->bus->dev;
907 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
908 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
909 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
910 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
911 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
912 while (dev_mc) {
913 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
914 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
915 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
916 dev_mc = dev_mc->sibling;
917 }
918 }
919 }
920 }
921 }
922#endif
923 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
924 if (!dev_mc) {
925 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
926 die("");
927 }
928 sysconf_init(dev_mc);
929#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
930 if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
931 if (pci_domain->link_list && !pci_domain->link_list->next) {
932 struct bus *new_link = new_link(pci_domain);
933 pci_domain->link_list->next = new_link;
934 new_link->link_num = 1;
935 new_link->dev = pci_domain;
936 new_link->children = 0;
937 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
938 }
939 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
940 }
941#endif
942
943 /* Get Max Number of cores(MNC) */
944 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
945 core_max = 1 << (coreid_bits & 0x000F); //mnc
946
947 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
948 if (ApicIdCoreIdSize) {
949 core_nums = (1 << ApicIdCoreIdSize) - 1;
950 } else {
951 core_nums = 3; //quad core
952 }
953
954 /* Find which cpus are present */
955 cpu_bus = dev->link_list;
956 for (i = 0; i < node_nums; i++) {
957 device_t cdb_dev;
958 unsigned busn, devn;
959 struct bus *pbus;
960
961 busn = CONFIG_CBB;
962 devn = CONFIG_CDB + i;
963 pbus = dev_mc->bus;
964#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
965 if (i >= 32) {
966 busn--;
967 devn -= 32;
968 pbus = pci_domain->link_list->next;
969 }
970#endif
971
972 /* Find the cpu's pci device */
973 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
974 if (!cdb_dev) {
975 /* If I am probing things in a weird order
976 * ensure all of the cpu's pci devices are found.
977 */
978 int fn;
979 for(fn = 0; fn <= 5; fn++) { //FBDIMM?
980 cdb_dev = pci_probe_dev(NULL, pbus,
981 PCI_DEVFN(devn, fn));
982 }
983 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
984 } else {
985 /* Ok, We need to set the links for that device.
986 * otherwise the device under it will not be scanned
987 */
988 int linknum;
989#if CONFIG_HT3_SUPPORT
990 linknum = 8;
991#else
992 linknum = 4;
993#endif
994 add_more_links(cdb_dev, linknum);
995 }
996
997 family = cpuid_eax(1);
998 family = (family >> 20) & 0xFF;
999 if (family == 1) { //f10
1000 u32 dword;
1001 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1002 dword = pci_read_config32(cdb_dev, 0xe8);
1003 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1004 } else if (family == 7) {//f16
1005 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1006 if (cdb_dev && cdb_dev->enabled) {
1007 siblings = pci_read_config32(cdb_dev, 0x84);
1008 siblings &= 0xFF;
1009 }
1010 } else {
1011 siblings = 0; //default one core
1012 }
1013 int enable_node = cdb_dev && cdb_dev->enabled;
1014 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1015 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1016
1017 for (j = 0; j <= siblings; j++ ) {
1018 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1019 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1020 u32 lapicid_start = 0;
1021
1022 /*
1023 * APIC ID calucation is tightly coupled with AGESA v5 code.
1024 * This calculation MUST match the assignment calculation done
1025 * in LocalApicInitializationAtEarly() function.
1026 * And reference GetLocalApicIdForCore()
1027 *
1028 * Apply apic enumeration rules
1029 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1030 * put the local-APICs at m..z
1031 *
1032 * This is needed because many IO-APIC devices only have 4 bits
1033 * for their APIC id and therefore must reside at 0..15
1034 */
1035#ifndef CFG_PLAT_NUM_IO_APICS /* defined in mainboard buildOpts.c */
1036#define CFG_PLAT_NUM_IO_APICS 3
1037#endif
1038 if ((node_nums * core_max) + CFG_PLAT_NUM_IO_APICS >= 0x10) {
1039 lapicid_start = (CFG_PLAT_NUM_IO_APICS - 1) / core_max;
1040 lapicid_start = (lapicid_start + 1) * core_max;
1041 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1042 }
1043 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
1044 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
1045 i, j, apic_id);
1046
1047 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1048 if (cpu)
1049 amd_cpu_topology(cpu, i, j);
1050 } //j
1051 }
1052 return max;
1053}
1054
1055static void cpu_bus_init(device_t dev)
1056{
1057 initialize_cpus(dev->link_list);
1058}
1059
Siyuan Wang3e32cc02013-07-09 17:16:20 +08001060static void cpu_bus_read_resources(device_t dev)
1061{
1062#if CONFIG_MMCONF_SUPPORT
1063 struct resource *resource = new_resource(dev, 0xc0010058);
1064 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
1065 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096*256;
1066 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
1067 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
1068#endif
1069}
1070
1071static void cpu_bus_set_resources(device_t dev)
1072{
1073 struct resource *resource = find_resource(dev, 0xc0010058);
1074 if (resource) {
1075 report_resource_stored(dev, resource, " <mmconfig>");
1076 }
1077 pci_dev_set_resources(dev);
1078}
1079
1080static struct device_operations cpu_bus_ops = {
1081 .read_resources = cpu_bus_read_resources,
1082 .set_resources = cpu_bus_set_resources,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001083 .enable_resources = DEVICE_NOOP,
Siyuan Wang3e32cc02013-07-09 17:16:20 +08001084 .init = cpu_bus_init,
1085 .scan_bus = cpu_bus_scan,
1086};
1087
1088static void root_complex_enable_dev(struct device *dev)
1089{
1090 static int done = 0;
1091
1092 /* Do not delay UMA setup, as a device on the PCI bus may evaluate
1093 the global uma_memory variables already in its enable function. */
1094 if (!done) {
1095 setup_bsp_ramtop();
1096 setup_uma_memory();
1097 done = 1;
1098 }
1099
1100 /* Set the operations if it is a special bus type */
1101 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1102 dev->ops = &pci_domain_ops;
1103 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1104 dev->ops = &cpu_bus_ops;
1105 }
1106}
1107
1108struct chip_operations northbridge_amd_agesa_family16kb_root_complex_ops = {
1109 CHIP_NAME("AMD FAM16 Root Complex")
1110 .enable_dev = root_complex_enable_dev,
1111};
Bruce Griffith76db07e2013-07-07 02:06:53 -06001112
1113/*********************************************************************
1114 * Change the vendor / device IDs to match the generic VBIOS header. *
1115 *********************************************************************/
1116u32 map_oprom_vendev(u32 vendev)
1117{
1118 u32 new_vendev = vendev;
1119
1120 switch(vendev) {
1121 case 0x10029830:
1122 case 0x10029831:
1123 case 0x10029832:
1124 case 0x10029833:
1125 case 0x10029834:
1126 case 0x10029835:
1127 case 0x10029836:
1128 case 0x10029837:
1129 case 0x10029838:
1130 case 0x10029839:
1131 case 0x1002983A:
1132 case 0x1002983D:
1133 new_vendev = 0x10029830; // This is the default value in AMD-generated VBIOS
1134 break;
1135 default:
1136 break;
1137 }
1138
1139 if (vendev != new_vendev)
1140 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1141
1142 return new_vendev;
1143}