blob: d878b4fac08592329d8beba102cc892b22e69460 [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>
zbao2c08f6a2012-07-02 15:32:58 +080045
46#define MAX_NODE_NUMS (MAX_NODES * MAX_DIES)
47
zbao2c08f6a2012-07-02 15:32:58 +080048typedef struct dram_base_mask {
49 u32 base; //[47:27] at [28:8]
50 u32 mask; //[47:27] at [28:8] and enable at bit 0
51} dram_base_mask_t;
52
53static unsigned node_nums;
54static unsigned sblink;
55static device_t __f0_dev[MAX_NODE_NUMS];
56static device_t __f1_dev[MAX_NODE_NUMS];
57static device_t __f2_dev[MAX_NODE_NUMS];
58static device_t __f4_dev[MAX_NODE_NUMS];
59static unsigned fx_devs = 0;
60
61static dram_base_mask_t get_dram_base_mask(u32 nodeid)
62{
63 device_t dev;
64 dram_base_mask_t d;
65 dev = __f1_dev[0];
66 u32 temp;
67 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
68 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
69 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
70 d.mask |= temp<<21;
71 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
72 d.mask |= (temp & 1); // enable bit
73 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
74 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
75 d.base |= temp<<21;
76 return d;
77}
78
79static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg,
80 u32 io_min, u32 io_max)
81{
82 u32 i;
83 u32 tempreg;
84 /* io range allocation */
85 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
86 for (i=0; i<node_nums; i++)
87 pci_write_config32(__f1_dev[i], reg+4, tempreg);
88 tempreg = 3 /*| ( 3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
89#if 0
90 // FIXME: can we use VGA reg instead?
91 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
92 printk(BIOS_SPEW, "%s, enabling legacy VGA IO forwarding for %s link %s\n",
93 __func__, dev_path(dev), link);
94 tempreg |= PCI_IO_BASE_VGA_EN;
95 }
96 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
97 tempreg |= PCI_IO_BASE_NO_ISA;
98 }
99#endif
100 for (i=0; i<node_nums; i++)
101 pci_write_config32(__f1_dev[i], reg, tempreg);
102}
103
104static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
105{
106 u32 i;
107 u32 tempreg;
108 /* io range allocation */
109 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
110 for (i=0; i<nodes; i++)
111 pci_write_config32(__f1_dev[i], reg+4, tempreg);
112 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
113 for (i=0; i<node_nums; i++)
114 pci_write_config32(__f1_dev[i], reg, tempreg);
115}
116
117static device_t get_node_pci(u32 nodeid, u32 fn)
118{
zbaod4627362012-07-23 19:49:40 +0800119#if MAX_NODE_NUMS + CONFIG_CDB >= 32
120 if ((CONFIG_CDB + nodeid) < 32) {
zbao2c08f6a2012-07-02 15:32:58 +0800121 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
122 } else {
123 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
124 }
125#else
126 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
127#endif
128}
129
130static void get_fx_devs(void)
131{
132 int i;
133 for (i = 0; i < MAX_NODE_NUMS; i++) {
134 __f0_dev[i] = get_node_pci(i, 0);
135 __f1_dev[i] = get_node_pci(i, 1);
136 __f2_dev[i] = get_node_pci(i, 2);
137 __f4_dev[i] = get_node_pci(i, 4);
138 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
139 fx_devs = i+1;
140 }
141 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
142 die("Cannot find 0:0x18.[0|1]\n");
143 }
144 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
145}
146
147static u32 f1_read_config32(unsigned reg)
148{
149 if (fx_devs == 0)
150 get_fx_devs();
151 return pci_read_config32(__f1_dev[0], reg);
152}
153
154static void f1_write_config32(unsigned reg, u32 value)
155{
156 int i;
157 if (fx_devs == 0)
158 get_fx_devs();
159 for(i = 0; i < fx_devs; i++) {
160 device_t dev;
161 dev = __f1_dev[i];
162 if (dev && dev->enabled) {
163 pci_write_config32(dev, reg, value);
164 }
165 }
166}
167
168static u32 amdfam15_nodeid(device_t dev)
169{
170#if MAX_NODE_NUMS == 64
171 unsigned busn;
172 busn = dev->bus->secondary;
173 if (busn != CONFIG_CBB) {
174 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
175 } else {
176 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
177 }
178
179#else
180 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
181#endif
182}
183
184static void set_vga_enable_reg(u32 nodeid, u32 linkn)
185{
186 u32 val;
187
188 val = 1 | (nodeid<<4) | (linkn<<12);
189 /* it will routing
190 * (1)mmio 0xa0000:0xbffff
191 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
192 */
193 f1_write_config32(0xf4, val);
194
195}
196
197/**
198 * @return
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100199 * @retval 2 resoure does not exist, usable
200 * @retval 0 resource exists, not usable
zbao2c08f6a2012-07-02 15:32:58 +0800201 * @retval 1 resource exist, resource has been allocated before
202 */
203static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
204 unsigned goal_link)
205{
206 struct resource *res;
207 unsigned nodeid, link = 0;
208 int result;
209 res = 0;
210 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
211 device_t dev;
212 dev = __f0_dev[nodeid];
213 if (!dev)
214 continue;
215 for (link = 0; !res && (link < 8); link++) {
216 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
217 }
218 }
219 result = 2;
220 if (res) {
221 result = 0;
222 if ((goal_link == (link - 1)) &&
223 (goal_nodeid == (nodeid - 1)) &&
224 (res->flags <= 1)) {
225 result = 1;
226 }
227 }
228 return result;
229}
230
231static struct resource *amdfam15_find_iopair(device_t dev, unsigned nodeid, unsigned link)
232{
233 struct resource *resource;
234 u32 free_reg, reg;
235 resource = 0;
236 free_reg = 0;
237 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
238 int result;
239 result = reg_useable(reg, dev, nodeid, link);
240 if (result == 1) {
241 /* I have been allocated this one */
242 break;
243 }
244 else if (result > 1) {
245 /* I have a free register pair */
246 free_reg = reg;
247 }
248 }
249 if (reg > 0xd8) {
250 reg = free_reg; // if no free, the free_reg still be 0
251 }
252
253 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
254
255 return resource;
256}
257
258static struct resource *amdfam15_find_mempair(device_t dev, u32 nodeid, u32 link)
259{
260 struct resource *resource;
261 u32 free_reg, reg;
262 resource = 0;
263 free_reg = 0;
264 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
265 int result;
266 result = reg_useable(reg, dev, nodeid, link);
267 if (result == 1) {
268 /* I have been allocated this one */
269 break;
270 }
271 else if (result > 1) {
272 /* I have a free register pair */
273 free_reg = reg;
274 }
275 }
276 if (reg > 0xb8) {
277 reg = free_reg;
278 }
279
280 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
281 return resource;
282}
283
284static void amdfam15_link_read_bases(device_t dev, u32 nodeid, u32 link)
285{
286 struct resource *resource;
287
288 /* Initialize the io space constraints on the current bus */
289 resource = amdfam15_find_iopair(dev, nodeid, link);
290 if (resource) {
291 u32 align;
292 align = log2(HT_IO_HOST_ALIGN);
293 resource->base = 0;
294 resource->size = 0;
295 resource->align = align;
296 resource->gran = align;
297 resource->limit = 0xffffUL;
298 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
299 }
300
301 /* Initialize the prefetchable memory constraints on the current bus */
302 resource = amdfam15_find_mempair(dev, nodeid, link);
303 if (resource) {
304 resource->base = 0;
305 resource->size = 0;
306 resource->align = log2(HT_MEM_HOST_ALIGN);
307 resource->gran = log2(HT_MEM_HOST_ALIGN);
308 resource->limit = 0xffffffffffULL;
309 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
310 resource->flags |= IORESOURCE_BRIDGE;
311 }
312
313 /* Initialize the memory constraints on the current bus */
314 resource = amdfam15_find_mempair(dev, nodeid, link);
315 if (resource) {
316 resource->base = 0;
317 resource->size = 0;
318 resource->align = log2(HT_MEM_HOST_ALIGN);
319 resource->gran = log2(HT_MEM_HOST_ALIGN);
320 resource->limit = 0xffffffffffULL;
321 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
322 }
323
324}
325
Steven Sherkf4340582013-01-29 16:13:35 -0700326static void nb_read_resources(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800327{
328 u32 nodeid;
329 struct bus *link;
330
331 nodeid = amdfam15_nodeid(dev);
332 for (link = dev->link_list; link; link = link->next) {
333 if (link->children) {
334 amdfam15_link_read_bases(dev, nodeid, link->link_num);
335 }
336 }
Steven Sherk1cbabb02013-02-01 09:22:35 -0700337
338 /*
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800339 * This MMCONF resource must be reserved in the PCI domain.
Steven Sherk1cbabb02013-02-01 09:22:35 -0700340 * It is not honored by the coreboot resource allocator if it is in
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800341 * the CPU_CLUSTER.
Steven Sherk1cbabb02013-02-01 09:22:35 -0700342 */
343#if CONFIG_MMCONF_SUPPORT
344 struct resource *resource = new_resource(dev, 0xc0010058);
345 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
346 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096 * 256;
347 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
348 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
349#endif
zbao2c08f6a2012-07-02 15:32:58 +0800350}
351
352static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
353{
354 resource_t rbase, rend;
355 unsigned reg, link_num;
356 char buf[50];
357
358 /* Make certain the resource has actually been set */
359 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
360 return;
361 }
362
363 /* If I have already stored this resource don't worry about it */
364 if (resource->flags & IORESOURCE_STORED) {
365 return;
366 }
367
368 /* Only handle PCI memory and IO resources */
369 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
370 return;
371
372 /* Ensure I am actually looking at a resource of function 1 */
373 if ((resource->index & 0xffff) < 0x1000) {
374 return;
375 }
376 /* Get the base address */
377 rbase = resource->base;
378
379 /* Get the limit (rounded up) */
380 rend = resource_end(resource);
381
382 /* Get the register and link */
383 reg = resource->index & 0xfff; // 4k
384 link_num = IOINDEX_LINK(resource->index);
385
386 if (resource->flags & IORESOURCE_IO) {
387 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
388 }
389 else if (resource->flags & IORESOURCE_MEM) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100390 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 +0800391 }
392 resource->flags |= IORESOURCE_STORED;
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100393 snprintf(buf, sizeof (buf), " <node %x link %x>",
zbao2c08f6a2012-07-02 15:32:58 +0800394 nodeid, link_num);
395 report_resource_stored(dev, resource, buf);
396}
397
398/**
399 * I tried to reuse the resource allocation code in set_resource()
400 * but it is too difficult to deal with the resource allocation magic.
401 */
402
403static void create_vga_resource(device_t dev, unsigned nodeid)
404{
405 struct bus *link;
406
407 /* find out which link the VGA card is connected,
408 * we only deal with the 'first' vga card */
409 for (link = dev->link_list; link; link = link->next) {
410 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
zbaod59d6242012-07-23 19:41:03 +0800411#if CONFIG_MULTIPLE_VGA_ADAPTERS
zbao2c08f6a2012-07-02 15:32:58 +0800412 extern device_t vga_pri; // the primary vga device, defined in device.c
413 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
414 link->secondary,link->subordinate);
415 /* We need to make sure the vga_pri is under the link */
416 if((vga_pri->bus->secondary >= link->secondary ) &&
417 (vga_pri->bus->secondary <= link->subordinate )
418 )
419#endif
420 break;
421 }
422 }
423
424 /* no VGA card installed */
425 if (link == NULL)
426 return;
427
428 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
429 set_vga_enable_reg(nodeid, sblink);
430}
431
Steven Sherkf4340582013-01-29 16:13:35 -0700432static void nb_set_resources(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800433{
434 unsigned nodeid;
435 struct bus *bus;
436 struct resource *res;
437
438 /* Find the nodeid */
439 nodeid = amdfam15_nodeid(dev);
440
441 create_vga_resource(dev, nodeid); //TODO: do we need this?
442
443 /* Set each resource we have found */
444 for (res = dev->resource_list; res; res = res->next) {
445 set_resource(dev, res, nodeid);
446 }
447
448 for (bus = dev->link_list; bus; bus = bus->next) {
449 if (bus->children) {
450 assign_resources(bus);
451 }
452 }
Steven Sherk1cbabb02013-02-01 09:22:35 -0700453
454 /* Print the MMCONF region if it has been reserved. */
455 res = find_resource(dev, 0xc0010058);
456 if (res) {
457 report_resource_stored(dev, res, " <mmconfig>");
458 }
zbao2c08f6a2012-07-02 15:32:58 +0800459}
460
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200461unsigned long acpi_fill_hest(acpi_hest_t *hest)
462{
463 void *addr, *current;
464
465 /* Skip the HEST header. */
466 current = (void *)(hest + 1);
467
468 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
469 if (addr != NULL)
470 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
471
472 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
473 if (addr != NULL)
474 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
475
476 return (unsigned long)current;
477}
478
479/* Implemented with AGESA-specific code. Dummy to keep linker happy. */
480unsigned long acpi_fill_slit(unsigned long current)
481{
482 return current;
483}
484
485/* Implemented with AGESA-specific code. Dummy to keep linker happy. */
486unsigned long acpi_fill_srat(unsigned long current)
487{
488 return current;
489}
490
491static void northbridge_fill_ssdt_generator(void)
492{
493 msr_t msr;
494 char pscope[] = "\\_SB.PCI0";
495
496 acpigen_write_scope(pscope);
497 msr = rdmsr(TOP_MEM);
498 acpigen_write_name_dword("TOM1", msr.lo);
499 msr = rdmsr(TOP_MEM2);
500 /*
501 * Since XP only implements parts of ACPI 2.0, we can't use a qword
502 * here.
503 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
504 * slide 22ff.
505 * Shift value right by 20 bit to make it fit into 32bit,
506 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
507 */
508 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
509 acpigen_pop_len();
510}
511
512static unsigned long agesa_write_acpi_tables(unsigned long current,
513 acpi_rsdp_t *rsdp)
514{
515 acpi_srat_t *srat;
516 acpi_slit_t *slit;
517 acpi_header_t *ssdt;
518 acpi_header_t *alib;
519 acpi_header_t *ivrs;
520 acpi_hest_t *hest;
521
522 /* HEST */
523 current = ALIGN(current, 8);
524 hest = (acpi_hest_t *)current;
525 acpi_write_hest((void *)current);
526 acpi_add_table(rsdp, (void *)current);
527 current += ((acpi_header_t *)current)->length;
528
529 current = ALIGN(current, 8);
530 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
531 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
532 if (ivrs != NULL) {
533 memcpy((void *)current, ivrs, ivrs->length);
534 ivrs = (acpi_header_t *) current;
535 current += ivrs->length;
536 acpi_add_table(rsdp, ivrs);
537 } else {
538 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
539 }
540
541 /* SRAT */
542 current = ALIGN(current, 8);
543 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
544 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
545 if (srat != NULL) {
546 memcpy((void *)current, srat, srat->header.length);
547 srat = (acpi_srat_t *) current;
548 current += srat->header.length;
549 acpi_add_table(rsdp, srat);
550 } else {
551 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
552 }
553
554 /* SLIT */
555 current = ALIGN(current, 8);
556 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
557 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
558 if (slit != NULL) {
559 memcpy((void *)current, slit, slit->header.length);
560 slit = (acpi_slit_t *) current;
561 current += slit->header.length;
562 acpi_add_table(rsdp, slit);
563 } else {
564 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
565 }
566
567 /* ALIB */
568 current = ALIGN(current, 16);
569 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
570 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
571 if (alib != NULL) {
572 memcpy((void *)current, alib, alib->length);
573 alib = (acpi_header_t *) current;
574 current += alib->length;
575 acpi_add_table(rsdp, (void *)alib);
576 }
577 else {
578 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
579 }
580
581 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
582 /* SSDT */
583 current = ALIGN(current, 16);
584 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
585 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
586 if (ssdt != NULL) {
587 memcpy((void *)current, ssdt, ssdt->length);
588 ssdt = (acpi_header_t *) current;
589 current += ssdt->length;
590 }
591 else {
592 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
593 }
594 acpi_add_table(rsdp,ssdt);
595
596 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
597
598 return current;
599}
600
601
zbao2c08f6a2012-07-02 15:32:58 +0800602static struct device_operations northbridge_operations = {
Steven Sherkf4340582013-01-29 16:13:35 -0700603 .read_resources = nb_read_resources,
604 .set_resources = nb_set_resources,
zbao2c08f6a2012-07-02 15:32:58 +0800605 .enable_resources = pci_dev_enable_resources,
Edward O'Callaghand994ef12014-11-21 02:22:33 +1100606 .init = DEVICE_NOOP,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200607 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
608 .write_acpi_tables = agesa_write_acpi_tables,
zbao2c08f6a2012-07-02 15:32:58 +0800609 .enable = 0,
610 .ops_pci = 0,
611};
612
613static const struct pci_driver family15_northbridge __pci_driver = {
614 .ops = &northbridge_operations,
615 .vendor = PCI_VENDOR_ID_AMD,
616 .device = PCI_DEVICE_ID_AMD_15H_MODEL_001F_NB_HT,
617};
618
619static const struct pci_driver family10_northbridge __pci_driver = {
620 .ops = &northbridge_operations,
621 .vendor = PCI_VENDOR_ID_AMD,
622 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
623};
624
625struct chip_operations northbridge_amd_agesa_family15tn_ops = {
626 CHIP_NAME("AMD FAM15 Northbridge")
627 .enable_dev = 0,
628};
629
630static void domain_read_resources(device_t dev)
631{
632 unsigned reg;
633
634 /* Find the already assigned resource pairs */
635 get_fx_devs();
636 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
637 u32 base, limit;
638 base = f1_read_config32(reg);
639 limit = f1_read_config32(reg + 0x04);
640 /* Is this register allocated? */
641 if ((base & 3) != 0) {
642 unsigned nodeid, reg_link;
643 device_t reg_dev;
644 if (reg<0xc0) { // mmio
645 nodeid = (limit & 0xf) + (base&0x30);
646 } else { // io
647 nodeid = (limit & 0xf) + ((base>>4)&0x30);
648 }
649 reg_link = (limit >> 4) & 7;
650 reg_dev = __f0_dev[nodeid];
651 if (reg_dev) {
652 /* Reserve the resource */
653 struct resource *res;
654 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
655 if (res) {
656 res->flags = 1;
657 }
658 }
659 }
660 }
661 /* FIXME: do we need to check extend conf space?
662 I don't believe that much preset value */
663
zbaod59d6242012-07-23 19:41:03 +0800664#if !CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800665 pci_domain_read_resources(dev);
666
667#else
668 struct bus *link;
669 struct resource *resource;
670 for (link=dev->link_list; link; link = link->next) {
671 /* Initialize the system wide io space constraints */
672 resource = new_resource(dev, 0|(link->link_num<<2));
673 resource->base = 0x400;
674 resource->limit = 0xffffUL;
675 resource->flags = IORESOURCE_IO;
676
677 /* Initialize the system wide prefetchable memory resources constraints */
678 resource = new_resource(dev, 1|(link->link_num<<2));
679 resource->limit = 0xfcffffffffULL;
680 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
681
682 /* Initialize the system wide memory resources constraints */
683 resource = new_resource(dev, 2|(link->link_num<<2));
684 resource->limit = 0xfcffffffffULL;
685 resource->flags = IORESOURCE_MEM;
686 }
687#endif
688}
689
zbao2c08f6a2012-07-02 15:32:58 +0800690static void domain_enable_resources(device_t dev)
691{
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +0300692 if (acpi_is_wakeup_s3())
Kyösti Mälkki1aa35c62014-10-21 14:19:04 +0300693 agesawrapper_fchs3laterestore();
zbao2c08f6a2012-07-02 15:32:58 +0800694
695 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkkib139b5e2014-10-20 07:41:20 +0300696 if (!acpi_is_wakeup_s3()) {
697 /* Enable MMIO on AMD CPU Address Map Controller */
Kyösti Mälkki48518f02014-11-25 14:20:57 +0200698 amd_initcpuio();
zbao2c08f6a2012-07-02 15:32:58 +0800699
Kyösti Mälkki1aa35c62014-10-21 14:19:04 +0300700 agesawrapper_amdinitmid();
Kyösti Mälkkib139b5e2014-10-20 07:41:20 +0300701 }
Mike Loptiene133aab2013-01-30 16:00:43 -0700702 printk(BIOS_DEBUG, " ader - leaving %s.\n", __func__);
zbao2c08f6a2012-07-02 15:32:58 +0800703}
704
705#if CONFIG_HW_MEM_HOLE_SIZEK != 0
706struct hw_mem_hole_info {
707 unsigned hole_startk;
708 int node_id;
709};
710static struct hw_mem_hole_info get_hw_mem_hole_info(void)
711{
712 struct hw_mem_hole_info mem_hole;
713 int i;
714 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
715 mem_hole.node_id = -1;
716 for (i = 0; i < node_nums; i++) {
717 dram_base_mask_t d;
718 u32 hole;
719 d = get_dram_base_mask(i);
720 if (!(d.mask & 1)) continue; // no memory on this node
721 hole = pci_read_config32(__f1_dev[i], 0xf0);
722 if (hole & 1) { // we find the hole
723 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
724 mem_hole.node_id = i; // record the node No with hole
725 break; // only one hole
726 }
727 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300728
729 /* We need to double check if there is special set on base reg and limit reg
730 * are not continuous instead of hole, it will find out its hole_startk.
731 */
zbao2c08f6a2012-07-02 15:32:58 +0800732 if (mem_hole.node_id == -1) {
733 resource_t limitk_pri = 0;
734 for (i=0; i<node_nums; i++) {
735 dram_base_mask_t d;
736 resource_t base_k, limit_k;
737 d = get_dram_base_mask(i);
738 if (!(d.base & 1)) continue;
739 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
740 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
741 if (limitk_pri != base_k) { // we find the hole
742 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
743 mem_hole.node_id = i;
744 break; //only one hole
745 }
zbao15dc3cc2012-08-03 15:56:21 +0800746 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800747 limitk_pri = limit_k;
748 }
749 }
750 return mem_hole;
751}
752#endif
753
zbao405cfe22012-07-23 19:44:29 +0800754#define ONE_MB_SHIFT 20
zbao6db7f342012-07-19 16:38:12 +0800755
Kyösti Mälkki6b5eb1c2012-07-19 19:26:43 +0300756static void setup_uma_memory(void)
zbao6db7f342012-07-19 16:38:12 +0800757{
758#if CONFIG_GFXUMA
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300759 uint32_t topmem = (uint32_t) bsp_topmem();
zbao6db7f342012-07-19 16:38:12 +0800760 uint32_t sys_mem;
761
zbao6db7f342012-07-19 16:38:12 +0800762 /* refer to UMA Size Consideration in Family15h BKDG. */
763 /* Please reference MemNGetUmaSizeOR () */
764 /*
765 * Total system memory UMASize
766 * >= 2G 512M
767 * >=1G 256M
768 * <1G 64M
769 */
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300770 sys_mem = topmem + (16 << ONE_MB_SHIFT); // Ignore 16MB allocated for C6 when finding UMA size
771 if ((bsp_topmem2()>>32) || (sys_mem >= 2048 << ONE_MB_SHIFT)) {
zbao405cfe22012-07-23 19:44:29 +0800772 uma_memory_size = 512 << ONE_MB_SHIFT;
773 } else if (sys_mem >= 1024 << ONE_MB_SHIFT) {
774 uma_memory_size = 256 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800775 } else {
zbao405cfe22012-07-23 19:44:29 +0800776 uma_memory_size = 64 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800777 }
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300778 uma_memory_base = topmem - uma_memory_size; /* TOP_MEM1 */
zbao6db7f342012-07-19 16:38:12 +0800779
780 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
781 __func__, uma_memory_size, uma_memory_base);
zbao6db7f342012-07-19 16:38:12 +0800782#endif
783}
784
785
zbao2c08f6a2012-07-02 15:32:58 +0800786static void domain_set_resources(device_t dev)
787{
zbaod59d6242012-07-23 19:41:03 +0800788#if CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800789 struct resource *io, *mem1, *mem2;
790 struct resource *res;
791#endif
792 unsigned long mmio_basek;
793 u32 pci_tolm;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300794 u64 ramtop = 0;
zbao2c08f6a2012-07-02 15:32:58 +0800795 int i, idx;
796 struct bus *link;
797#if CONFIG_HW_MEM_HOLE_SIZEK != 0
798 struct hw_mem_hole_info mem_hole;
799 u32 reset_memhole = 1;
800#endif
801
zbaod59d6242012-07-23 19:41:03 +0800802#if CONFIG_PCI_64BIT_PREF_MEM
zbao2c08f6a2012-07-02 15:32:58 +0800803
804 for (link = dev->link_list; link; link = link->next) {
805 /* Now reallocate the pci resources memory with the
806 * highest addresses I can manage.
807 */
808 mem1 = find_resource(dev, 1|(link->link_num<<2));
809 mem2 = find_resource(dev, 2|(link->link_num<<2));
810
811 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
812 mem1->base, mem1->limit, mem1->size, mem1->align);
813 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
814 mem2->base, mem2->limit, mem2->size, mem2->align);
815
816 /* See if both resources have roughly the same limits */
817 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
818 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
819 {
820 /* If so place the one with the most stringent alignment first */
821 if (mem2->align > mem1->align) {
822 struct resource *tmp;
823 tmp = mem1;
824 mem1 = mem2;
825 mem2 = tmp;
826 }
827 /* Now place the memory as high up as it will go */
828 mem2->base = resource_max(mem2);
829 mem1->limit = mem2->base - 1;
830 mem1->base = resource_max(mem1);
831 }
832 else {
833 /* Place the resources as high up as they will go */
834 mem2->base = resource_max(mem2);
835 mem1->base = resource_max(mem1);
836 }
837
838 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
839 mem1->base, mem1->limit, mem1->size, mem1->align);
840 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
841 mem2->base, mem2->limit, mem2->size, mem2->align);
842 }
843
844 for (res = &dev->resource_list; res; res = res->next)
845 {
846 res->flags |= IORESOURCE_ASSIGNED;
847 res->flags |= IORESOURCE_STORED;
848 report_resource_stored(dev, res, "");
849 }
850#endif
851
852 pci_tolm = 0xffffffffUL;
853 for (link = dev->link_list; link; link = link->next) {
854 pci_tolm = find_pci_tolm(link);
855 }
856
857 // FIXME handle interleaved nodes. If you fix this here, please fix
858 // amdk8, too.
859 mmio_basek = pci_tolm >> 10;
860 /* Round mmio_basek to something the processor can support */
861 mmio_basek &= ~((1 << 6) -1);
862
863 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
864 // MMIO hole. If you fix this here, please fix amdk8, too.
865 /* Round the mmio hole to 64M */
866 mmio_basek &= ~((64*1024) - 1);
867
868#if CONFIG_HW_MEM_HOLE_SIZEK != 0
869 /* if the hw mem hole is already set in raminit stage, here we will compare
870 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
871 * use hole_basek as mmio_basek and we don't need to reset hole.
872 * otherwise We reset the hole to the mmio_basek
873 */
874
875 mem_hole = get_hw_mem_hole_info();
876
877 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
878 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
879 mmio_basek = mem_hole.hole_startk;
880 reset_memhole = 0;
881 }
882#endif
883
884 idx = 0x10;
885 for (i = 0; i < node_nums; i++) {
886 dram_base_mask_t d;
887 resource_t basek, limitk, sizek; // 4 1T
888
889 d = get_dram_base_mask(i);
890
891 if (!(d.mask & 1)) continue;
892 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100893 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800894
895 sizek = limitk - basek;
896
897 /* see if we need a hole from 0xa0000 to 0xbffff */
898 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
899 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
900 idx += 0x10;
901 basek = (8*64)+(16*16);
902 sizek = limitk - ((8*64)+(16*16));
903
904 }
905
906 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
907
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300908 /* split the region to accommodate pci memory space */
zbao2c08f6a2012-07-02 15:32:58 +0800909 if ((basek < 4*1024*1024 ) && (limitk > mmio_basek)) {
910 if (basek <= mmio_basek) {
911 unsigned pre_sizek;
912 pre_sizek = mmio_basek - basek;
913 if (pre_sizek>0) {
914 ram_resource(dev, (idx | i), basek, pre_sizek);
915 idx += 0x10;
916 sizek -= pre_sizek;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300917 if (!ramtop)
918 ramtop = mmio_basek * 1024;
zbao2c08f6a2012-07-02 15:32:58 +0800919 }
920 basek = mmio_basek;
921 }
922 if ((basek + sizek) <= 4*1024*1024) {
923 sizek = 0;
924 }
925 else {
Siyuan Wang29840e22013-06-04 19:56:22 +0800926 uint64_t topmem2 = bsp_topmem2();
zbao2c08f6a2012-07-02 15:32:58 +0800927 basek = 4*1024*1024;
Siyuan Wang29840e22013-06-04 19:56:22 +0800928 sizek = topmem2/1024 - basek;
zbao2c08f6a2012-07-02 15:32:58 +0800929 }
930 }
931
zbao2c08f6a2012-07-02 15:32:58 +0800932 ram_resource(dev, (idx | i), basek, sizek);
933 idx += 0x10;
zbao2c08f6a2012-07-02 15:32:58 +0800934 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
935 i, mmio_basek, basek, limitk);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300936 if (!ramtop)
937 ramtop = limitk * 1024;
zbao2c08f6a2012-07-02 15:32:58 +0800938 }
939
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300940#if CONFIG_GFXUMA
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300941 set_top_of_ram(uma_memory_base);
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300942 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300943#else
944 set_top_of_ram(ramtop);
zbao2c08f6a2012-07-02 15:32:58 +0800945#endif
946
947 for(link = dev->link_list; link; link = link->next) {
948 if (link->children) {
949 assign_resources(link);
950 }
951 }
952}
953
954static struct device_operations pci_domain_ops = {
955 .read_resources = domain_read_resources,
956 .set_resources = domain_set_resources,
957 .enable_resources = domain_enable_resources,
958 .init = NULL,
959 .scan_bus = pci_domain_scan_bus,
Kyösti Mälkki872c9222013-07-03 09:44:28 +0300960 .ops_pci_bus = pci_bus_default_ops,
zbao2c08f6a2012-07-02 15:32:58 +0800961};
962
963static void sysconf_init(device_t dev) // first node
964{
965 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
966 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
967}
968
969static void add_more_links(device_t dev, unsigned total_links)
970{
971 struct bus *link, *last = NULL;
972 int link_num;
973
974 for (link = dev->link_list; link; link = link->next)
975 last = link;
976
977 if (last) {
978 int links = total_links - last->link_num;
979 link_num = last->link_num;
980 if (links > 0) {
981 link = malloc(links*sizeof(*link));
982 if (!link)
983 die("Couldn't allocate more links!\n");
984 memset(link, 0, links*sizeof(*link));
985 last->next = link;
986 }
987 }
988 else {
989 link_num = -1;
990 link = malloc(total_links*sizeof(*link));
991 memset(link, 0, total_links*sizeof(*link));
992 dev->link_list = link;
993 }
994
995 for (link_num = link_num + 1; link_num < total_links; link_num++) {
996 link->link_num = link_num;
997 link->dev = dev;
998 link->next = link + 1;
999 last = link;
1000 link = link->next;
1001 }
1002 last->next = NULL;
1003}
1004
zbao2c08f6a2012-07-02 15:32:58 +08001005static u32 cpu_bus_scan(device_t dev, u32 max)
1006{
1007 struct bus *cpu_bus;
1008 device_t dev_mc;
1009#if CONFIG_CBB
1010 device_t pci_domain;
1011#endif
1012 int i,j;
1013 int coreid_bits;
1014 int core_max = 0;
1015 unsigned ApicIdCoreIdSize;
1016 unsigned core_nums;
1017 int siblings = 0;
1018 unsigned int family;
1019
1020#if CONFIG_CBB
1021 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
1022 if (dev_mc && dev_mc->bus) {
1023 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
1024 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001025 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +08001026 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
1027 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
1028 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
1029 } else {
1030 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
1031 }
1032 printk(BIOS_DEBUG, "\n");
1033 }
1034 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1035 if (!dev_mc) {
1036 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
1037 if (dev_mc && dev_mc->bus) {
1038 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
1039 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001040 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +08001041 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
1042 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
1043 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
1044 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
1045 while (dev_mc) {
1046 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
1047 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
1048 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
1049 dev_mc = dev_mc->sibling;
1050 }
1051 }
1052 }
1053 }
1054 }
1055#endif
1056 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1057 if (!dev_mc) {
1058 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
1059 die("");
1060 }
1061 sysconf_init(dev_mc);
1062#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1063 if (node_nums>32) { // need to put node 32 to node 63 to bus 0xfe
1064 if (pci_domain->link_list && !pci_domain->link_list->next) {
1065 struct bus *new_link = new_link(pci_domain);
1066 pci_domain->link_list->next = new_link;
1067 new_link->link_num = 1;
1068 new_link->dev = pci_domain;
1069 new_link->children = 0;
1070 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
1071 }
1072 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
1073 }
1074#endif
1075
1076 /* Get Max Number of cores(MNC) */
1077 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
1078 core_max = 1 << (coreid_bits & 0x000F); //mnc
1079
1080 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1081 if (ApicIdCoreIdSize) {
1082 core_nums = (1 << ApicIdCoreIdSize) - 1;
1083 } else {
1084 core_nums = 3; //quad core
1085 }
1086
1087 /* Find which cpus are present */
1088 cpu_bus = dev->link_list;
1089 for (i = 0; i < node_nums; i++) {
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001090 device_t cdb_dev;
zbao2c08f6a2012-07-02 15:32:58 +08001091 unsigned busn, devn;
1092 struct bus *pbus;
1093
1094 busn = CONFIG_CBB;
1095 devn = CONFIG_CDB + i;
1096 pbus = dev_mc->bus;
1097#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
1098 if (i >= 32) {
1099 busn--;
1100 devn -= 32;
1101 pbus = pci_domain->link_list->next;
1102 }
1103#endif
1104
1105 /* Find the cpu's pci device */
1106 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1107 if (!cdb_dev) {
1108 /* If I am probing things in a weird order
1109 * ensure all of the cpu's pci devices are found.
1110 */
1111 int fn;
1112 for(fn = 0; fn <= 5; fn++) { //FBDIMM?
1113 cdb_dev = pci_probe_dev(NULL, pbus,
1114 PCI_DEVFN(devn, fn));
1115 }
1116 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1117 } else {
1118 /* Ok, We need to set the links for that device.
1119 * otherwise the device under it will not be scanned
1120 */
Kyösti Mälkki2a2d6132015-02-04 13:25:37 +02001121 add_more_links(cdb_dev, 4);
zbao2c08f6a2012-07-02 15:32:58 +08001122 }
1123
1124 family = cpuid_eax(1);
1125 family = (family >> 20) & 0xFF;
1126 if (family == 1) { //f10
1127 u32 dword;
1128 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1129 dword = pci_read_config32(cdb_dev, 0xe8);
1130 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1131 } else if (family == 6) {//f15
1132 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1133 if (cdb_dev && cdb_dev->enabled) {
1134 siblings = pci_read_config32(cdb_dev, 0x84);
1135 siblings &= 0xFF;
1136 }
1137 } else {
1138 siblings = 0; //default one core
1139 }
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001140 int enable_node = cdb_dev && cdb_dev->enabled;
zbao2c08f6a2012-07-02 15:32:58 +08001141 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1142 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1143
1144 for (j = 0; j <= siblings; j++ ) {
1145 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1146 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1147 u32 lapicid_start = 0;
1148
zbao2c08f6a2012-07-02 15:32:58 +08001149 /*
1150 * APIC ID calucation is tightly coupled with AGESA v5 code.
1151 * This calculation MUST match the assignment calculation done
1152 * in LocalApicInitializationAtEarly() function.
1153 * And reference GetLocalApicIdForCore()
1154 *
1155 * Apply apic enumeration rules
1156 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1157 * put the local-APICs at m..z
1158 *
1159 * This is needed because many IO-APIC devices only have 4 bits
1160 * for their APIC id and therefore must reside at 0..15
1161 */
1162#ifndef CFG_PLAT_NUM_IO_APICS /* defined in mainboard buildOpts.c */
1163#define CFG_PLAT_NUM_IO_APICS 3
1164#endif
1165 if ((node_nums * core_max) + CFG_PLAT_NUM_IO_APICS >= 0x10) {
1166 lapicid_start = (CFG_PLAT_NUM_IO_APICS - 1) / core_max;
1167 lapicid_start = (lapicid_start + 1) * core_max;
1168 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1169 }
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001170 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
zbao2c08f6a2012-07-02 15:32:58 +08001171 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001172 i, j, apic_id);
zbao2c08f6a2012-07-02 15:32:58 +08001173
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001174 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1175 if (cpu)
1176 amd_cpu_topology(cpu, i, j);
zbao2c08f6a2012-07-02 15:32:58 +08001177 } //j
1178 }
1179 return max;
1180}
1181
1182static void cpu_bus_init(device_t dev)
1183{
1184 initialize_cpus(dev->link_list);
1185}
1186
zbao2c08f6a2012-07-02 15:32:58 +08001187static struct device_operations cpu_bus_ops = {
Edward O'Callaghan2837ab22014-11-06 08:57:40 +11001188 .read_resources = DEVICE_NOOP,
1189 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001190 .enable_resources = DEVICE_NOOP,
zbao2c08f6a2012-07-02 15:32:58 +08001191 .init = cpu_bus_init,
1192 .scan_bus = cpu_bus_scan,
1193};
1194
1195static void root_complex_enable_dev(struct device *dev)
1196{
Kyösti Mälkki87213b62012-08-27 20:00:33 +03001197 static int done = 0;
1198
1199 /* Do not delay UMA setup, as a device on the PCI bus may evaluate
1200 the global uma_memory variables already in its enable function. */
1201 if (!done) {
1202 setup_bsp_ramtop();
1203 setup_uma_memory();
1204 done = 1;
1205 }
1206
zbao2c08f6a2012-07-02 15:32:58 +08001207 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001208 if (dev->path.type == DEVICE_PATH_DOMAIN) {
zbao2c08f6a2012-07-02 15:32:58 +08001209 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -08001210 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
zbao2c08f6a2012-07-02 15:32:58 +08001211 dev->ops = &cpu_bus_ops;
1212 }
1213}
1214
1215struct chip_operations northbridge_amd_agesa_family15tn_root_complex_ops = {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001216 CHIP_NAME("AMD FAM15tn Root Complex")
zbao2c08f6a2012-07-02 15:32:58 +08001217 .enable_dev = root_complex_enable_dev,
1218};
Dave Frodincbf3d402012-12-05 08:20:12 -07001219
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001220/*********************************************************************
1221 * Change the vendor / device IDs to match the generic VBIOS header. *
1222 *********************************************************************/
Dave Frodincbf3d402012-12-05 08:20:12 -07001223u32 map_oprom_vendev(u32 vendev)
1224{
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001225 u32 new_vendev = vendev;
Dave Frodincbf3d402012-12-05 08:20:12 -07001226
1227 switch(vendev) {
Bruce Griffith42e11f52013-07-08 18:19:08 -06001228 case 0x10029900: /* AMD Radeon HD 7660G (Trinity) */
1229 case 0x10029901: /* AMD Radeon HD 7660D (Trinity) */
1230 case 0x10029903: /* AMD Radeon HD 7640G (Trinity) */
1231 case 0x10029904: /* AMD Radeon HD 7560D (Trinity) */
1232 case 0x10029907: /* AMD Radeon HD 7620G (Trinity) */
1233 case 0x10029908: /* AMD Radeon HD 7600G (Trinity) */
1234 case 0x1002990A: /* AMD Radeon HD 7500G (Trinity) */
1235 case 0x1002990B: /* AMD Radeon HD 8650G (Richland) */
1236 case 0x1002990C: /* AMD Radeon HD 8670D (Richland) */
1237 case 0x1002990D: /* AMD Radeon HD 8550G (Richland) */
1238 case 0x1002990E: /* AMD Radeon HD 8570D (Richland) */
1239 case 0x1002990F: /* AMD Radeon HD 8610G (Richland) */
1240 case 0x10029910: /* AMD Radeon HD 7660G (Trinity) */
1241 case 0x10029913: /* AMD Radeon HD 7640G (Trinity) */
1242 case 0x10029917: /* AMD Radeon HD 7620G (Trinity) */
1243 case 0x10029918: /* AMD Radeon HD 7600G (Trinity) */
1244 case 0x10029919: /* AMD Radeon HD 7500G (Trinity) */
1245 case 0x10029990: /* AMD Radeon HD 7520G (Trinity) */
1246 case 0x10029991: /* AMD Radeon HD 7540D (Trinity) */
1247 case 0x10029992: /* AMD Radeon HD 7420G (Trinity) */
1248 case 0x10029993: /* AMD Radeon HD 7480D (Trinity) */
1249 case 0x10029994: /* AMD Radeon HD 7400G (Trinity) */
1250 case 0x10029995: /* AMD Radeon HD 8450G (Richland) */
1251 case 0x10029996: /* AMD Radeon HD 8470D (Richland) */
1252 case 0x10029997: /* AMD Radeon HD 8350G (Richland) */
1253 case 0x10029998: /* AMD Radeon HD 8370D (Richland) */
1254 case 0x10029999: /* AMD Radeon HD 8510G (Richland) */
1255 case 0x1002999A: /* AMD Radeon HD 8410G (Richland) */
1256 case 0x1002999B: /* AMD Radeon HD 8310G (Richland) */
1257 case 0x1002999C: /* AMD Radeon HD 8650D (Richland) */
1258 case 0x1002999D: /* AMD Radeon HD 8550D (Richland) */
1259 case 0x100299A0: /* AMD Radeon HD 7520G (Trinity) */
1260 case 0x100299A2: /* AMD Radeon HD 7420G (Trinity) */
1261 case 0x100299A4: /* AMD Radeon HD 7400G (Trinity) */
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001262 new_vendev = 0x10029901;
Dave Frodincbf3d402012-12-05 08:20:12 -07001263 break;
1264 }
1265
1266 return new_vendev;
1267}