blob: a98d1e95ec6bb9bd8bfd0c1646f8051695806523 [file] [log] [blame]
zbao2c08f6a2012-07-02 15:32:58 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 Advanced Micro Devices, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
zbao2c08f6a2012-07-02 15:32:58 +080018 */
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>
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +020023#include <arch/acpigen.h>
zbao2c08f6a2012-07-02 15:32:58 +080024#include <stdint.h>
25#include <device/device.h>
26#include <device/pci.h>
27#include <device/pci_ids.h>
28#include <device/hypertransport.h>
29#include <stdlib.h>
30#include <string.h>
Ronald G. Minnich5079a0d2012-11-27 11:32:38 -080031#include <lib.h>
zbao2c08f6a2012-07-02 15:32:58 +080032#include <cpu/cpu.h>
33#include <cbmem.h>
Martin Roth73e86a82013-01-17 16:28:30 -070034#include <AGESA.h>
zbao2c08f6a2012-07-02 15:32:58 +080035
36#include <cpu/x86/lapic.h>
Kyösti Mälkkidbc47392012-08-05 12:11:40 +030037#include <cpu/amd/mtrr.h>
zbao2c08f6a2012-07-02 15:32:58 +080038
39#include <Porting.h>
zbao2c08f6a2012-07-02 15:32:58 +080040#include <Options.h>
41#include <Topology.h>
42#include <cpu/amd/amdfam15.h>
43#include <cpuRegisters.h>
Kyösti Mälkkif21c2ac2014-10-19 09:35:18 +030044#include <northbridge/amd/agesa/agesawrapper.h>
Kyösti Mälkki7b23ae02014-07-04 16:14:37 +030045#include <northbridge/amd/agesa/agesawrapper_call.h>
zbao2c08f6a2012-07-02 15:32:58 +080046
47#define MAX_NODE_NUMS (MAX_NODES * MAX_DIES)
48
49#if (defined CONFIG_EXT_CONF_SUPPORT) && CONFIG_EXT_CONF_SUPPORT == 1
50#error CONFIG_EXT_CONF_SUPPORT == 1 not support anymore!
51#endif
52
53typedef struct dram_base_mask {
54 u32 base; //[47:27] at [28:8]
55 u32 mask; //[47:27] at [28:8] and enable at bit 0
56} dram_base_mask_t;
57
58static unsigned node_nums;
59static unsigned sblink;
60static device_t __f0_dev[MAX_NODE_NUMS];
61static device_t __f1_dev[MAX_NODE_NUMS];
62static device_t __f2_dev[MAX_NODE_NUMS];
63static device_t __f4_dev[MAX_NODE_NUMS];
64static unsigned fx_devs = 0;
65
66static dram_base_mask_t get_dram_base_mask(u32 nodeid)
67{
68 device_t dev;
69 dram_base_mask_t d;
70 dev = __f1_dev[0];
71 u32 temp;
72 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
73 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
74 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
75 d.mask |= temp<<21;
76 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
77 d.mask |= (temp & 1); // enable bit
78 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
79 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
80 d.base |= temp<<21;
81 return d;
82}
83
84static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg,
85 u32 io_min, u32 io_max)
86{
87 u32 i;
88 u32 tempreg;
89 /* io range allocation */
90 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
91 for (i=0; i<node_nums; i++)
92 pci_write_config32(__f1_dev[i], reg+4, tempreg);
93 tempreg = 3 /*| ( 3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
94#if 0
95 // FIXME: can we use VGA reg instead?
96 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
97 printk(BIOS_SPEW, "%s, enabling legacy VGA IO forwarding for %s link %s\n",
98 __func__, dev_path(dev), link);
99 tempreg |= PCI_IO_BASE_VGA_EN;
100 }
101 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
102 tempreg |= PCI_IO_BASE_NO_ISA;
103 }
104#endif
105 for (i=0; i<node_nums; i++)
106 pci_write_config32(__f1_dev[i], reg, tempreg);
107}
108
109static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
110{
111 u32 i;
112 u32 tempreg;
113 /* io range allocation */
114 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
115 for (i=0; i<nodes; i++)
116 pci_write_config32(__f1_dev[i], reg+4, tempreg);
117 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
118 for (i=0; i<node_nums; i++)
119 pci_write_config32(__f1_dev[i], reg, tempreg);
120}
121
122static device_t get_node_pci(u32 nodeid, u32 fn)
123{
zbaod4627362012-07-23 19:49:40 +0800124#if MAX_NODE_NUMS + CONFIG_CDB >= 32
125 if ((CONFIG_CDB + nodeid) < 32) {
zbao2c08f6a2012-07-02 15:32:58 +0800126 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
127 } else {
128 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
129 }
130#else
131 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
132#endif
133}
134
135static void get_fx_devs(void)
136{
137 int i;
138 for (i = 0; i < MAX_NODE_NUMS; i++) {
139 __f0_dev[i] = get_node_pci(i, 0);
140 __f1_dev[i] = get_node_pci(i, 1);
141 __f2_dev[i] = get_node_pci(i, 2);
142 __f4_dev[i] = get_node_pci(i, 4);
143 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
144 fx_devs = i+1;
145 }
146 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
147 die("Cannot find 0:0x18.[0|1]\n");
148 }
149 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
150}
151
152static u32 f1_read_config32(unsigned reg)
153{
154 if (fx_devs == 0)
155 get_fx_devs();
156 return pci_read_config32(__f1_dev[0], reg);
157}
158
159static void f1_write_config32(unsigned reg, u32 value)
160{
161 int i;
162 if (fx_devs == 0)
163 get_fx_devs();
164 for(i = 0; i < fx_devs; i++) {
165 device_t dev;
166 dev = __f1_dev[i];
167 if (dev && dev->enabled) {
168 pci_write_config32(dev, reg, value);
169 }
170 }
171}
172
173static u32 amdfam15_nodeid(device_t dev)
174{
175#if MAX_NODE_NUMS == 64
176 unsigned busn;
177 busn = dev->bus->secondary;
178 if (busn != CONFIG_CBB) {
179 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
180 } else {
181 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
182 }
183
184#else
185 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
186#endif
187}
188
189static void set_vga_enable_reg(u32 nodeid, u32 linkn)
190{
191 u32 val;
192
193 val = 1 | (nodeid<<4) | (linkn<<12);
194 /* it will routing
195 * (1)mmio 0xa0000:0xbffff
196 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
197 */
198 f1_write_config32(0xf4, val);
199
200}
201
202/**
203 * @return
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100204 * @retval 2 resoure does not exist, usable
205 * @retval 0 resource exists, not usable
zbao2c08f6a2012-07-02 15:32:58 +0800206 * @retval 1 resource exist, resource has been allocated before
207 */
208static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
209 unsigned goal_link)
210{
211 struct resource *res;
212 unsigned nodeid, link = 0;
213 int result;
214 res = 0;
215 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
216 device_t dev;
217 dev = __f0_dev[nodeid];
218 if (!dev)
219 continue;
220 for (link = 0; !res && (link < 8); link++) {
221 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
222 }
223 }
224 result = 2;
225 if (res) {
226 result = 0;
227 if ((goal_link == (link - 1)) &&
228 (goal_nodeid == (nodeid - 1)) &&
229 (res->flags <= 1)) {
230 result = 1;
231 }
232 }
233 return result;
234}
235
236static struct resource *amdfam15_find_iopair(device_t dev, unsigned nodeid, unsigned link)
237{
238 struct resource *resource;
239 u32 free_reg, reg;
240 resource = 0;
241 free_reg = 0;
242 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
243 int result;
244 result = reg_useable(reg, dev, nodeid, link);
245 if (result == 1) {
246 /* I have been allocated this one */
247 break;
248 }
249 else if (result > 1) {
250 /* I have a free register pair */
251 free_reg = reg;
252 }
253 }
254 if (reg > 0xd8) {
255 reg = free_reg; // if no free, the free_reg still be 0
256 }
257
258 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
259
260 return resource;
261}
262
263static struct resource *amdfam15_find_mempair(device_t dev, u32 nodeid, u32 link)
264{
265 struct resource *resource;
266 u32 free_reg, reg;
267 resource = 0;
268 free_reg = 0;
269 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
270 int result;
271 result = reg_useable(reg, dev, nodeid, link);
272 if (result == 1) {
273 /* I have been allocated this one */
274 break;
275 }
276 else if (result > 1) {
277 /* I have a free register pair */
278 free_reg = reg;
279 }
280 }
281 if (reg > 0xb8) {
282 reg = free_reg;
283 }
284
285 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
286 return resource;
287}
288
289static void amdfam15_link_read_bases(device_t dev, u32 nodeid, u32 link)
290{
291 struct resource *resource;
292
293 /* Initialize the io space constraints on the current bus */
294 resource = amdfam15_find_iopair(dev, nodeid, link);
295 if (resource) {
296 u32 align;
297 align = log2(HT_IO_HOST_ALIGN);
298 resource->base = 0;
299 resource->size = 0;
300 resource->align = align;
301 resource->gran = align;
302 resource->limit = 0xffffUL;
303 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
304 }
305
306 /* Initialize the prefetchable memory constraints on the current bus */
307 resource = amdfam15_find_mempair(dev, nodeid, link);
308 if (resource) {
309 resource->base = 0;
310 resource->size = 0;
311 resource->align = log2(HT_MEM_HOST_ALIGN);
312 resource->gran = log2(HT_MEM_HOST_ALIGN);
313 resource->limit = 0xffffffffffULL;
314 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
315 resource->flags |= IORESOURCE_BRIDGE;
316 }
317
318 /* Initialize the memory constraints on the current bus */
319 resource = amdfam15_find_mempair(dev, nodeid, link);
320 if (resource) {
321 resource->base = 0;
322 resource->size = 0;
323 resource->align = log2(HT_MEM_HOST_ALIGN);
324 resource->gran = log2(HT_MEM_HOST_ALIGN);
325 resource->limit = 0xffffffffffULL;
326 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
327 }
328
329}
330
Steven Sherkf4340582013-01-29 16:13:35 -0700331static void nb_read_resources(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800332{
333 u32 nodeid;
334 struct bus *link;
335
336 nodeid = amdfam15_nodeid(dev);
337 for (link = dev->link_list; link; link = link->next) {
338 if (link->children) {
339 amdfam15_link_read_bases(dev, nodeid, link->link_num);
340 }
341 }
Steven Sherk1cbabb02013-02-01 09:22:35 -0700342
343 /*
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800344 * This MMCONF resource must be reserved in the PCI domain.
Steven Sherk1cbabb02013-02-01 09:22:35 -0700345 * It is not honored by the coreboot resource allocator if it is in
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800346 * the CPU_CLUSTER.
Steven Sherk1cbabb02013-02-01 09:22:35 -0700347 */
348#if CONFIG_MMCONF_SUPPORT
349 struct resource *resource = new_resource(dev, 0xc0010058);
350 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
351 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096 * 256;
352 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
353 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
354#endif
zbao2c08f6a2012-07-02 15:32:58 +0800355}
356
357static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
358{
359 resource_t rbase, rend;
360 unsigned reg, link_num;
361 char buf[50];
362
363 /* Make certain the resource has actually been set */
364 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
365 return;
366 }
367
368 /* If I have already stored this resource don't worry about it */
369 if (resource->flags & IORESOURCE_STORED) {
370 return;
371 }
372
373 /* Only handle PCI memory and IO resources */
374 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
375 return;
376
377 /* Ensure I am actually looking at a resource of function 1 */
378 if ((resource->index & 0xffff) < 0x1000) {
379 return;
380 }
381 /* Get the base address */
382 rbase = resource->base;
383
384 /* Get the limit (rounded up) */
385 rend = resource_end(resource);
386
387 /* Get the register and link */
388 reg = resource->index & 0xfff; // 4k
389 link_num = IOINDEX_LINK(resource->index);
390
391 if (resource->flags & IORESOURCE_IO) {
392 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
393 }
394 else if (resource->flags & IORESOURCE_MEM) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100395 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums);// [39:8]
zbao2c08f6a2012-07-02 15:32:58 +0800396 }
397 resource->flags |= IORESOURCE_STORED;
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100398 snprintf(buf, sizeof (buf), " <node %x link %x>",
zbao2c08f6a2012-07-02 15:32:58 +0800399 nodeid, link_num);
400 report_resource_stored(dev, resource, buf);
401}
402
403/**
404 * I tried to reuse the resource allocation code in set_resource()
405 * but it is too difficult to deal with the resource allocation magic.
406 */
407
408static void create_vga_resource(device_t dev, unsigned nodeid)
409{
410 struct bus *link;
411
412 /* find out which link the VGA card is connected,
413 * we only deal with the 'first' vga card */
414 for (link = dev->link_list; link; link = link->next) {
415 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
zbaod59d6242012-07-23 19:41:03 +0800416#if CONFIG_MULTIPLE_VGA_ADAPTERS
zbao2c08f6a2012-07-02 15:32:58 +0800417 extern device_t vga_pri; // the primary vga device, defined in device.c
418 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
419 link->secondary,link->subordinate);
420 /* We need to make sure the vga_pri is under the link */
421 if((vga_pri->bus->secondary >= link->secondary ) &&
422 (vga_pri->bus->secondary <= link->subordinate )
423 )
424#endif
425 break;
426 }
427 }
428
429 /* no VGA card installed */
430 if (link == NULL)
431 return;
432
433 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
434 set_vga_enable_reg(nodeid, sblink);
435}
436
Steven Sherkf4340582013-01-29 16:13:35 -0700437static void nb_set_resources(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800438{
439 unsigned nodeid;
440 struct bus *bus;
441 struct resource *res;
442
443 /* Find the nodeid */
444 nodeid = amdfam15_nodeid(dev);
445
446 create_vga_resource(dev, nodeid); //TODO: do we need this?
447
448 /* Set each resource we have found */
449 for (res = dev->resource_list; res; res = res->next) {
450 set_resource(dev, res, nodeid);
451 }
452
453 for (bus = dev->link_list; bus; bus = bus->next) {
454 if (bus->children) {
455 assign_resources(bus);
456 }
457 }
Steven Sherk1cbabb02013-02-01 09:22:35 -0700458
459 /* Print the MMCONF region if it has been reserved. */
460 res = find_resource(dev, 0xc0010058);
461 if (res) {
462 report_resource_stored(dev, res, " <mmconfig>");
463 }
zbao2c08f6a2012-07-02 15:32:58 +0800464}
465
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200466unsigned long acpi_fill_hest(acpi_hest_t *hest)
467{
468 void *addr, *current;
469
470 /* Skip the HEST header. */
471 current = (void *)(hest + 1);
472
473 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
474 if (addr != NULL)
475 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
476
477 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
478 if (addr != NULL)
479 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
480
481 return (unsigned long)current;
482}
483
484/* Implemented with AGESA-specific code. Dummy to keep linker happy. */
485unsigned long acpi_fill_slit(unsigned long current)
486{
487 return current;
488}
489
490/* Implemented with AGESA-specific code. Dummy to keep linker happy. */
491unsigned long acpi_fill_srat(unsigned long current)
492{
493 return current;
494}
495
496static void northbridge_fill_ssdt_generator(void)
497{
498 msr_t msr;
499 char pscope[] = "\\_SB.PCI0";
500
501 acpigen_write_scope(pscope);
502 msr = rdmsr(TOP_MEM);
503 acpigen_write_name_dword("TOM1", msr.lo);
504 msr = rdmsr(TOP_MEM2);
505 /*
506 * Since XP only implements parts of ACPI 2.0, we can't use a qword
507 * here.
508 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
509 * slide 22ff.
510 * Shift value right by 20 bit to make it fit into 32bit,
511 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
512 */
513 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
514 acpigen_pop_len();
515}
516
517static unsigned long agesa_write_acpi_tables(unsigned long current,
518 acpi_rsdp_t *rsdp)
519{
520 acpi_srat_t *srat;
521 acpi_slit_t *slit;
522 acpi_header_t *ssdt;
523 acpi_header_t *alib;
524 acpi_header_t *ivrs;
525 acpi_hest_t *hest;
526
527 /* HEST */
528 current = ALIGN(current, 8);
529 hest = (acpi_hest_t *)current;
530 acpi_write_hest((void *)current);
531 acpi_add_table(rsdp, (void *)current);
532 current += ((acpi_header_t *)current)->length;
533
534 current = ALIGN(current, 8);
535 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
536 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
537 if (ivrs != NULL) {
538 memcpy((void *)current, ivrs, ivrs->length);
539 ivrs = (acpi_header_t *) current;
540 current += ivrs->length;
541 acpi_add_table(rsdp, ivrs);
542 } else {
543 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
544 }
545
546 /* SRAT */
547 current = ALIGN(current, 8);
548 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
549 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
550 if (srat != NULL) {
551 memcpy((void *)current, srat, srat->header.length);
552 srat = (acpi_srat_t *) current;
553 current += srat->header.length;
554 acpi_add_table(rsdp, srat);
555 } else {
556 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
557 }
558
559 /* SLIT */
560 current = ALIGN(current, 8);
561 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
562 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
563 if (slit != NULL) {
564 memcpy((void *)current, slit, slit->header.length);
565 slit = (acpi_slit_t *) current;
566 current += slit->header.length;
567 acpi_add_table(rsdp, slit);
568 } else {
569 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
570 }
571
572 /* ALIB */
573 current = ALIGN(current, 16);
574 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
575 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
576 if (alib != NULL) {
577 memcpy((void *)current, alib, alib->length);
578 alib = (acpi_header_t *) current;
579 current += alib->length;
580 acpi_add_table(rsdp, (void *)alib);
581 }
582 else {
583 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
584 }
585
586 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
587 /* SSDT */
588 current = ALIGN(current, 16);
589 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
590 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
591 if (ssdt != NULL) {
592 memcpy((void *)current, ssdt, ssdt->length);
593 ssdt = (acpi_header_t *) current;
594 current += ssdt->length;
595 }
596 else {
597 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
598 }
599 acpi_add_table(rsdp,ssdt);
600
601 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
602
603 return current;
604}
605
606
zbao2c08f6a2012-07-02 15:32:58 +0800607static struct device_operations northbridge_operations = {
Steven Sherkf4340582013-01-29 16:13:35 -0700608 .read_resources = nb_read_resources,
609 .set_resources = nb_set_resources,
zbao2c08f6a2012-07-02 15:32:58 +0800610 .enable_resources = pci_dev_enable_resources,
Edward O'Callaghand994ef12014-11-21 02:22:33 +1100611 .init = DEVICE_NOOP,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200612 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
613 .write_acpi_tables = agesa_write_acpi_tables,
zbao2c08f6a2012-07-02 15:32:58 +0800614 .enable = 0,
615 .ops_pci = 0,
616};
617
618static const struct pci_driver family15_northbridge __pci_driver = {
619 .ops = &northbridge_operations,
620 .vendor = PCI_VENDOR_ID_AMD,
621 .device = PCI_DEVICE_ID_AMD_15H_MODEL_001F_NB_HT,
622};
623
624static const struct pci_driver family10_northbridge __pci_driver = {
625 .ops = &northbridge_operations,
626 .vendor = PCI_VENDOR_ID_AMD,
627 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
628};
629
630struct chip_operations northbridge_amd_agesa_family15tn_ops = {
631 CHIP_NAME("AMD FAM15 Northbridge")
632 .enable_dev = 0,
633};
634
635static void domain_read_resources(device_t dev)
636{
637 unsigned reg;
638
639 /* Find the already assigned resource pairs */
640 get_fx_devs();
641 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
642 u32 base, limit;
643 base = f1_read_config32(reg);
644 limit = f1_read_config32(reg + 0x04);
645 /* Is this register allocated? */
646 if ((base & 3) != 0) {
647 unsigned nodeid, reg_link;
648 device_t reg_dev;
649 if (reg<0xc0) { // mmio
650 nodeid = (limit & 0xf) + (base&0x30);
651 } else { // io
652 nodeid = (limit & 0xf) + ((base>>4)&0x30);
653 }
654 reg_link = (limit >> 4) & 7;
655 reg_dev = __f0_dev[nodeid];
656 if (reg_dev) {
657 /* Reserve the resource */
658 struct resource *res;
659 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
660 if (res) {
661 res->flags = 1;
662 }
663 }
664 }
665 }
666 /* FIXME: do we need to check extend conf space?
667 I don't believe that much preset value */
668
zbaod59d6242012-07-23 19:41:03 +0800669#if !CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800670 pci_domain_read_resources(dev);
671
672#else
673 struct bus *link;
674 struct resource *resource;
675 for (link=dev->link_list; link; link = link->next) {
676 /* Initialize the system wide io space constraints */
677 resource = new_resource(dev, 0|(link->link_num<<2));
678 resource->base = 0x400;
679 resource->limit = 0xffffUL;
680 resource->flags = IORESOURCE_IO;
681
682 /* Initialize the system wide prefetchable memory resources constraints */
683 resource = new_resource(dev, 1|(link->link_num<<2));
684 resource->limit = 0xfcffffffffULL;
685 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
686
687 /* Initialize the system wide memory resources constraints */
688 resource = new_resource(dev, 2|(link->link_num<<2));
689 resource->limit = 0xfcffffffffULL;
690 resource->flags = IORESOURCE_MEM;
691 }
692#endif
693}
694
zbao2c08f6a2012-07-02 15:32:58 +0800695static void domain_enable_resources(device_t dev)
696{
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +0300697 if (acpi_is_wakeup_s3())
Kyösti Mälkki7b23ae02014-07-04 16:14:37 +0300698 AGESAWRAPPER(fchs3laterestore);
zbao2c08f6a2012-07-02 15:32:58 +0800699
700 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkki7b23ae02014-07-04 16:14:37 +0300701 if (!acpi_is_wakeup_s3())
702 AGESAWRAPPER(amdinitmid);
zbao2c08f6a2012-07-02 15:32:58 +0800703
Mike Loptiene133aab2013-01-30 16:00:43 -0700704 printk(BIOS_DEBUG, " ader - leaving %s.\n", __func__);
zbao2c08f6a2012-07-02 15:32:58 +0800705}
706
707#if CONFIG_HW_MEM_HOLE_SIZEK != 0
708struct hw_mem_hole_info {
709 unsigned hole_startk;
710 int node_id;
711};
712static struct hw_mem_hole_info get_hw_mem_hole_info(void)
713{
714 struct hw_mem_hole_info mem_hole;
715 int i;
716 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
717 mem_hole.node_id = -1;
718 for (i = 0; i < node_nums; i++) {
719 dram_base_mask_t d;
720 u32 hole;
721 d = get_dram_base_mask(i);
722 if (!(d.mask & 1)) continue; // no memory on this node
723 hole = pci_read_config32(__f1_dev[i], 0xf0);
724 if (hole & 1) { // we find the hole
725 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
726 mem_hole.node_id = i; // record the node No with hole
727 break; // only one hole
728 }
729 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300730
731 /* We need to double check if there is special set on base reg and limit reg
732 * are not continuous instead of hole, it will find out its hole_startk.
733 */
zbao2c08f6a2012-07-02 15:32:58 +0800734 if (mem_hole.node_id == -1) {
735 resource_t limitk_pri = 0;
736 for (i=0; i<node_nums; i++) {
737 dram_base_mask_t d;
738 resource_t base_k, limit_k;
739 d = get_dram_base_mask(i);
740 if (!(d.base & 1)) continue;
741 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
742 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
743 if (limitk_pri != base_k) { // we find the hole
744 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
745 mem_hole.node_id = i;
746 break; //only one hole
747 }
zbao15dc3cc2012-08-03 15:56:21 +0800748 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800749 limitk_pri = limit_k;
750 }
751 }
752 return mem_hole;
753}
754#endif
755
zbao405cfe22012-07-23 19:44:29 +0800756#define ONE_MB_SHIFT 20
zbao6db7f342012-07-19 16:38:12 +0800757
Kyösti Mälkki6b5eb1c2012-07-19 19:26:43 +0300758static void setup_uma_memory(void)
zbao6db7f342012-07-19 16:38:12 +0800759{
760#if CONFIG_GFXUMA
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300761 uint32_t topmem = (uint32_t) bsp_topmem();
zbao6db7f342012-07-19 16:38:12 +0800762 uint32_t sys_mem;
763
zbao6db7f342012-07-19 16:38:12 +0800764 /* refer to UMA Size Consideration in Family15h BKDG. */
765 /* Please reference MemNGetUmaSizeOR () */
766 /*
767 * Total system memory UMASize
768 * >= 2G 512M
769 * >=1G 256M
770 * <1G 64M
771 */
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300772 sys_mem = topmem + (16 << ONE_MB_SHIFT); // Ignore 16MB allocated for C6 when finding UMA size
773 if ((bsp_topmem2()>>32) || (sys_mem >= 2048 << ONE_MB_SHIFT)) {
zbao405cfe22012-07-23 19:44:29 +0800774 uma_memory_size = 512 << ONE_MB_SHIFT;
775 } else if (sys_mem >= 1024 << ONE_MB_SHIFT) {
776 uma_memory_size = 256 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800777 } else {
zbao405cfe22012-07-23 19:44:29 +0800778 uma_memory_size = 64 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800779 }
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300780 uma_memory_base = topmem - uma_memory_size; /* TOP_MEM1 */
zbao6db7f342012-07-19 16:38:12 +0800781
782 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
783 __func__, uma_memory_size, uma_memory_base);
zbao6db7f342012-07-19 16:38:12 +0800784#endif
785}
786
787
zbao2c08f6a2012-07-02 15:32:58 +0800788static void domain_set_resources(device_t dev)
789{
zbaod59d6242012-07-23 19:41:03 +0800790#if CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800791 struct resource *io, *mem1, *mem2;
792 struct resource *res;
793#endif
794 unsigned long mmio_basek;
795 u32 pci_tolm;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300796 u64 ramtop = 0;
zbao2c08f6a2012-07-02 15:32:58 +0800797 int i, idx;
798 struct bus *link;
799#if CONFIG_HW_MEM_HOLE_SIZEK != 0
800 struct hw_mem_hole_info mem_hole;
801 u32 reset_memhole = 1;
802#endif
803
zbaod59d6242012-07-23 19:41:03 +0800804#if CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800805
806 for (link = dev->link_list; link; link = link->next) {
807 /* Now reallocate the pci resources memory with the
808 * highest addresses I can manage.
809 */
810 mem1 = find_resource(dev, 1|(link->link_num<<2));
811 mem2 = find_resource(dev, 2|(link->link_num<<2));
812
813 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
814 mem1->base, mem1->limit, mem1->size, mem1->align);
815 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
816 mem2->base, mem2->limit, mem2->size, mem2->align);
817
818 /* See if both resources have roughly the same limits */
819 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
820 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
821 {
822 /* If so place the one with the most stringent alignment first */
823 if (mem2->align > mem1->align) {
824 struct resource *tmp;
825 tmp = mem1;
826 mem1 = mem2;
827 mem2 = tmp;
828 }
829 /* Now place the memory as high up as it will go */
830 mem2->base = resource_max(mem2);
831 mem1->limit = mem2->base - 1;
832 mem1->base = resource_max(mem1);
833 }
834 else {
835 /* Place the resources as high up as they will go */
836 mem2->base = resource_max(mem2);
837 mem1->base = resource_max(mem1);
838 }
839
840 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
841 mem1->base, mem1->limit, mem1->size, mem1->align);
842 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
843 mem2->base, mem2->limit, mem2->size, mem2->align);
844 }
845
846 for (res = &dev->resource_list; res; res = res->next)
847 {
848 res->flags |= IORESOURCE_ASSIGNED;
849 res->flags |= IORESOURCE_STORED;
850 report_resource_stored(dev, res, "");
851 }
852#endif
853
854 pci_tolm = 0xffffffffUL;
855 for (link = dev->link_list; link; link = link->next) {
856 pci_tolm = find_pci_tolm(link);
857 }
858
859 // FIXME handle interleaved nodes. If you fix this here, please fix
860 // amdk8, too.
861 mmio_basek = pci_tolm >> 10;
862 /* Round mmio_basek to something the processor can support */
863 mmio_basek &= ~((1 << 6) -1);
864
865 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
866 // MMIO hole. If you fix this here, please fix amdk8, too.
867 /* Round the mmio hole to 64M */
868 mmio_basek &= ~((64*1024) - 1);
869
870#if CONFIG_HW_MEM_HOLE_SIZEK != 0
871 /* if the hw mem hole is already set in raminit stage, here we will compare
872 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
873 * use hole_basek as mmio_basek and we don't need to reset hole.
874 * otherwise We reset the hole to the mmio_basek
875 */
876
877 mem_hole = get_hw_mem_hole_info();
878
879 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
880 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
881 mmio_basek = mem_hole.hole_startk;
882 reset_memhole = 0;
883 }
884#endif
885
886 idx = 0x10;
887 for (i = 0; i < node_nums; i++) {
888 dram_base_mask_t d;
889 resource_t basek, limitk, sizek; // 4 1T
890
891 d = get_dram_base_mask(i);
892
893 if (!(d.mask & 1)) continue;
894 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100895 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800896
897 sizek = limitk - basek;
898
899 /* see if we need a hole from 0xa0000 to 0xbffff */
900 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
901 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
902 idx += 0x10;
903 basek = (8*64)+(16*16);
904 sizek = limitk - ((8*64)+(16*16));
905
906 }
907
908 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
909
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300910 /* split the region to accommodate pci memory space */
zbao2c08f6a2012-07-02 15:32:58 +0800911 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
912 if (basek <= mmio_basek) {
913 unsigned pre_sizek;
914 pre_sizek = mmio_basek - basek;
915 if (pre_sizek>0) {
916 ram_resource(dev, (idx | i), basek, pre_sizek);
917 idx += 0x10;
918 sizek -= pre_sizek;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300919 if (!ramtop)
920 ramtop = mmio_basek * 1024;
zbao2c08f6a2012-07-02 15:32:58 +0800921 }
922 basek = mmio_basek;
923 }
924 if ((basek + sizek) <= 4*1024*1024) {
925 sizek = 0;
926 }
927 else {
Siyuan Wang29840e22013-06-04 19:56:22 +0800928 uint64_t topmem2 = bsp_topmem2();
zbao2c08f6a2012-07-02 15:32:58 +0800929 basek = 4*1024*1024;
Siyuan Wang29840e22013-06-04 19:56:22 +0800930 sizek = topmem2/1024 - basek;
zbao2c08f6a2012-07-02 15:32:58 +0800931 }
932 }
933
zbao2c08f6a2012-07-02 15:32:58 +0800934 ram_resource(dev, (idx | i), basek, sizek);
935 idx += 0x10;
zbao2c08f6a2012-07-02 15:32:58 +0800936 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
937 i, mmio_basek, basek, limitk);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300938 if (!ramtop)
939 ramtop = limitk * 1024;
zbao2c08f6a2012-07-02 15:32:58 +0800940 }
941
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300942#if CONFIG_GFXUMA
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300943 set_top_of_ram(uma_memory_base);
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300944 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300945#else
946 set_top_of_ram(ramtop);
zbao2c08f6a2012-07-02 15:32:58 +0800947#endif
948
949 for(link = dev->link_list; link; link = link->next) {
950 if (link->children) {
951 assign_resources(link);
952 }
953 }
954}
955
956static struct device_operations pci_domain_ops = {
957 .read_resources = domain_read_resources,
958 .set_resources = domain_set_resources,
959 .enable_resources = domain_enable_resources,
960 .init = NULL,
961 .scan_bus = pci_domain_scan_bus,
Kyösti Mälkki872c9222013-07-03 09:44:28 +0300962 .ops_pci_bus = pci_bus_default_ops,
zbao2c08f6a2012-07-02 15:32:58 +0800963};
964
965static void sysconf_init(device_t dev) // first node
966{
967 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
968 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
969}
970
971static void add_more_links(device_t dev, unsigned total_links)
972{
973 struct bus *link, *last = NULL;
974 int link_num;
975
976 for (link = dev->link_list; link; link = link->next)
977 last = link;
978
979 if (last) {
980 int links = total_links - last->link_num;
981 link_num = last->link_num;
982 if (links > 0) {
983 link = malloc(links*sizeof(*link));
984 if (!link)
985 die("Couldn't allocate more links!\n");
986 memset(link, 0, links*sizeof(*link));
987 last->next = link;
988 }
989 }
990 else {
991 link_num = -1;
992 link = malloc(total_links*sizeof(*link));
993 memset(link, 0, total_links*sizeof(*link));
994 dev->link_list = link;
995 }
996
997 for (link_num = link_num + 1; link_num < total_links; link_num++) {
998 link->link_num = link_num;
999 link->dev = dev;
1000 link->next = link + 1;
1001 last = link;
1002 link = link->next;
1003 }
1004 last->next = NULL;
1005}
1006
zbao2c08f6a2012-07-02 15:32:58 +08001007static u32 cpu_bus_scan(device_t dev, u32 max)
1008{
1009 struct bus *cpu_bus;
1010 device_t dev_mc;
1011#if CONFIG_CBB
1012 device_t pci_domain;
1013#endif
1014 int i,j;
1015 int coreid_bits;
1016 int core_max = 0;
1017 unsigned ApicIdCoreIdSize;
1018 unsigned core_nums;
1019 int siblings = 0;
1020 unsigned int family;
1021
1022#if CONFIG_CBB
1023 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
1024 if (dev_mc && dev_mc->bus) {
1025 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
1026 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001027 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +08001028 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
1029 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
1030 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
1031 } else {
1032 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
1033 }
1034 printk(BIOS_DEBUG, "\n");
1035 }
1036 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1037 if (!dev_mc) {
1038 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
1039 if (dev_mc && dev_mc->bus) {
1040 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
1041 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001042 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +08001043 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
1044 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
1045 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
1046 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
1047 while (dev_mc) {
1048 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
1049 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
1050 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
1051 dev_mc = dev_mc->sibling;
1052 }
1053 }
1054 }
1055 }
1056 }
1057#endif
1058 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1059 if (!dev_mc) {
1060 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
1061 die("");
1062 }
1063 sysconf_init(dev_mc);
1064#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1065 if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
1066 if (pci_domain->link_list && !pci_domain->link_list->next) {
1067 struct bus *new_link = new_link(pci_domain);
1068 pci_domain->link_list->next = new_link;
1069 new_link->link_num = 1;
1070 new_link->dev = pci_domain;
1071 new_link->children = 0;
1072 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
1073 }
1074 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
1075 }
1076#endif
1077
1078 /* Get Max Number of cores(MNC) */
1079 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
1080 core_max = 1 << (coreid_bits & 0x000F); //mnc
1081
1082 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1083 if (ApicIdCoreIdSize) {
1084 core_nums = (1 << ApicIdCoreIdSize) - 1;
1085 } else {
1086 core_nums = 3; //quad core
1087 }
1088
1089 /* Find which cpus are present */
1090 cpu_bus = dev->link_list;
1091 for (i = 0; i < node_nums; i++) {
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001092 device_t cdb_dev;
zbao2c08f6a2012-07-02 15:32:58 +08001093 unsigned busn, devn;
1094 struct bus *pbus;
1095
1096 busn = CONFIG_CBB;
1097 devn = CONFIG_CDB + i;
1098 pbus = dev_mc->bus;
1099#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1100 if (i >= 32) {
1101 busn--;
1102 devn -= 32;
1103 pbus = pci_domain->link_list->next;
1104 }
1105#endif
1106
1107 /* Find the cpu's pci device */
1108 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1109 if (!cdb_dev) {
1110 /* If I am probing things in a weird order
1111 * ensure all of the cpu's pci devices are found.
1112 */
1113 int fn;
1114 for(fn = 0; fn <= 5; fn++) { //FBDIMM?
1115 cdb_dev = pci_probe_dev(NULL, pbus,
1116 PCI_DEVFN(devn, fn));
1117 }
1118 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1119 } else {
1120 /* Ok, We need to set the links for that device.
1121 * otherwise the device under it will not be scanned
1122 */
1123 int linknum;
zbaod59d6242012-07-23 19:41:03 +08001124#if CONFIG_HT3_SUPPORT
zbao2c08f6a2012-07-02 15:32:58 +08001125 linknum = 8;
1126#else
1127 linknum = 4;
1128#endif
1129 add_more_links(cdb_dev, linknum);
1130 }
1131
1132 family = cpuid_eax(1);
1133 family = (family >> 20) & 0xFF;
1134 if (family == 1) { //f10
1135 u32 dword;
1136 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1137 dword = pci_read_config32(cdb_dev, 0xe8);
1138 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1139 } else if (family == 6) {//f15
1140 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1141 if (cdb_dev && cdb_dev->enabled) {
1142 siblings = pci_read_config32(cdb_dev, 0x84);
1143 siblings &= 0xFF;
1144 }
1145 } else {
1146 siblings = 0; //default one core
1147 }
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001148 int enable_node = cdb_dev && cdb_dev->enabled;
zbao2c08f6a2012-07-02 15:32:58 +08001149 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1150 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1151
1152 for (j = 0; j <= siblings; j++ ) {
1153 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1154 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1155 u32 lapicid_start = 0;
1156
zbao2c08f6a2012-07-02 15:32:58 +08001157 /*
1158 * APIC ID calucation is tightly coupled with AGESA v5 code.
1159 * This calculation MUST match the assignment calculation done
1160 * in LocalApicInitializationAtEarly() function.
1161 * And reference GetLocalApicIdForCore()
1162 *
1163 * Apply apic enumeration rules
1164 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1165 * put the local-APICs at m..z
1166 *
1167 * This is needed because many IO-APIC devices only have 4 bits
1168 * for their APIC id and therefore must reside at 0..15
1169 */
1170#ifndef CFG_PLAT_NUM_IO_APICS /* defined in mainboard buildOpts.c */
1171#define CFG_PLAT_NUM_IO_APICS 3
1172#endif
1173 if ((node_nums * core_max) + CFG_PLAT_NUM_IO_APICS >= 0x10) {
1174 lapicid_start = (CFG_PLAT_NUM_IO_APICS - 1) / core_max;
1175 lapicid_start = (lapicid_start + 1) * core_max;
1176 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1177 }
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001178 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
zbao2c08f6a2012-07-02 15:32:58 +08001179 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001180 i, j, apic_id);
zbao2c08f6a2012-07-02 15:32:58 +08001181
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001182 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1183 if (cpu)
1184 amd_cpu_topology(cpu, i, j);
zbao2c08f6a2012-07-02 15:32:58 +08001185 } //j
1186 }
1187 return max;
1188}
1189
1190static void cpu_bus_init(device_t dev)
1191{
1192 initialize_cpus(dev->link_list);
1193}
1194
zbao2c08f6a2012-07-02 15:32:58 +08001195static struct device_operations cpu_bus_ops = {
Edward O'Callaghan2837ab22014-11-06 08:57:40 +11001196 .read_resources = DEVICE_NOOP,
1197 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001198 .enable_resources = DEVICE_NOOP,
zbao2c08f6a2012-07-02 15:32:58 +08001199 .init = cpu_bus_init,
1200 .scan_bus = cpu_bus_scan,
1201};
1202
1203static void root_complex_enable_dev(struct device *dev)
1204{
Kyösti Mälkki87213b62012-08-27 20:00:33 +03001205 static int done = 0;
1206
1207 /* Do not delay UMA setup, as a device on the PCI bus may evaluate
1208 the global uma_memory variables already in its enable function. */
1209 if (!done) {
1210 setup_bsp_ramtop();
1211 setup_uma_memory();
1212 done = 1;
1213 }
1214
zbao2c08f6a2012-07-02 15:32:58 +08001215 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001216 if (dev->path.type == DEVICE_PATH_DOMAIN) {
zbao2c08f6a2012-07-02 15:32:58 +08001217 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -08001218 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
zbao2c08f6a2012-07-02 15:32:58 +08001219 dev->ops = &cpu_bus_ops;
1220 }
1221}
1222
1223struct chip_operations northbridge_amd_agesa_family15tn_root_complex_ops = {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001224 CHIP_NAME("AMD FAM15tn Root Complex")
zbao2c08f6a2012-07-02 15:32:58 +08001225 .enable_dev = root_complex_enable_dev,
1226};
Dave Frodincbf3d402012-12-05 08:20:12 -07001227
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001228/*********************************************************************
1229 * Change the vendor / device IDs to match the generic VBIOS header. *
1230 *********************************************************************/
Dave Frodincbf3d402012-12-05 08:20:12 -07001231u32 map_oprom_vendev(u32 vendev)
1232{
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001233 u32 new_vendev = vendev;
Dave Frodincbf3d402012-12-05 08:20:12 -07001234
1235 switch(vendev) {
Bruce Griffith42e11f52013-07-08 18:19:08 -06001236 case 0x10029900: /* AMD Radeon HD 7660G (Trinity) */
1237 case 0x10029901: /* AMD Radeon HD 7660D (Trinity) */
1238 case 0x10029903: /* AMD Radeon HD 7640G (Trinity) */
1239 case 0x10029904: /* AMD Radeon HD 7560D (Trinity) */
1240 case 0x10029907: /* AMD Radeon HD 7620G (Trinity) */
1241 case 0x10029908: /* AMD Radeon HD 7600G (Trinity) */
1242 case 0x1002990A: /* AMD Radeon HD 7500G (Trinity) */
1243 case 0x1002990B: /* AMD Radeon HD 8650G (Richland) */
1244 case 0x1002990C: /* AMD Radeon HD 8670D (Richland) */
1245 case 0x1002990D: /* AMD Radeon HD 8550G (Richland) */
1246 case 0x1002990E: /* AMD Radeon HD 8570D (Richland) */
1247 case 0x1002990F: /* AMD Radeon HD 8610G (Richland) */
1248 case 0x10029910: /* AMD Radeon HD 7660G (Trinity) */
1249 case 0x10029913: /* AMD Radeon HD 7640G (Trinity) */
1250 case 0x10029917: /* AMD Radeon HD 7620G (Trinity) */
1251 case 0x10029918: /* AMD Radeon HD 7600G (Trinity) */
1252 case 0x10029919: /* AMD Radeon HD 7500G (Trinity) */
1253 case 0x10029990: /* AMD Radeon HD 7520G (Trinity) */
1254 case 0x10029991: /* AMD Radeon HD 7540D (Trinity) */
1255 case 0x10029992: /* AMD Radeon HD 7420G (Trinity) */
1256 case 0x10029993: /* AMD Radeon HD 7480D (Trinity) */
1257 case 0x10029994: /* AMD Radeon HD 7400G (Trinity) */
1258 case 0x10029995: /* AMD Radeon HD 8450G (Richland) */
1259 case 0x10029996: /* AMD Radeon HD 8470D (Richland) */
1260 case 0x10029997: /* AMD Radeon HD 8350G (Richland) */
1261 case 0x10029998: /* AMD Radeon HD 8370D (Richland) */
1262 case 0x10029999: /* AMD Radeon HD 8510G (Richland) */
1263 case 0x1002999A: /* AMD Radeon HD 8410G (Richland) */
1264 case 0x1002999B: /* AMD Radeon HD 8310G (Richland) */
1265 case 0x1002999C: /* AMD Radeon HD 8650D (Richland) */
1266 case 0x1002999D: /* AMD Radeon HD 8550D (Richland) */
1267 case 0x100299A0: /* AMD Radeon HD 7520G (Trinity) */
1268 case 0x100299A2: /* AMD Radeon HD 7420G (Trinity) */
1269 case 0x100299A4: /* AMD Radeon HD 7400G (Trinity) */
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001270 new_vendev = 0x10029901;
Dave Frodincbf3d402012-12-05 08:20:12 -07001271 break;
1272 }
1273
1274 return new_vendev;
1275}