blob: ae1efb4db7a0f61f0b4e4e73dd771b7f68f483f2 [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.
zbao2c08f6a2012-07-02 15:32:58 +080014 */
15
16#include <console/console.h>
17#include <arch/io.h>
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +030018#include <arch/acpi.h>
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +020019#include <arch/acpigen.h>
zbao2c08f6a2012-07-02 15:32:58 +080020#include <stdint.h>
21#include <device/device.h>
22#include <device/pci.h>
23#include <device/pci_ids.h>
24#include <device/hypertransport.h>
25#include <stdlib.h>
26#include <string.h>
Ronald G. Minnich5079a0d2012-11-27 11:32:38 -080027#include <lib.h>
zbao2c08f6a2012-07-02 15:32:58 +080028#include <cpu/cpu.h>
29#include <cbmem.h>
Martin Roth73e86a82013-01-17 16:28:30 -070030#include <AGESA.h>
zbao2c08f6a2012-07-02 15:32:58 +080031
32#include <cpu/x86/lapic.h>
Kyösti Mälkkidbc47392012-08-05 12:11:40 +030033#include <cpu/amd/mtrr.h>
zbao2c08f6a2012-07-02 15:32:58 +080034
35#include <Porting.h>
zbao2c08f6a2012-07-02 15:32:58 +080036#include <Options.h>
37#include <Topology.h>
38#include <cpu/amd/amdfam15.h>
39#include <cpuRegisters.h>
Kyösti Mälkkif21c2ac2014-10-19 09:35:18 +030040#include <northbridge/amd/agesa/agesawrapper.h>
zbao2c08f6a2012-07-02 15:32:58 +080041
42#define MAX_NODE_NUMS (MAX_NODES * MAX_DIES)
43
zbao2c08f6a2012-07-02 15:32:58 +080044typedef struct dram_base_mask {
45 u32 base; //[47:27] at [28:8]
46 u32 mask; //[47:27] at [28:8] and enable at bit 0
47} dram_base_mask_t;
48
49static unsigned node_nums;
50static unsigned sblink;
51static device_t __f0_dev[MAX_NODE_NUMS];
52static device_t __f1_dev[MAX_NODE_NUMS];
53static device_t __f2_dev[MAX_NODE_NUMS];
54static device_t __f4_dev[MAX_NODE_NUMS];
55static unsigned fx_devs = 0;
56
57static dram_base_mask_t get_dram_base_mask(u32 nodeid)
58{
59 device_t dev;
60 dram_base_mask_t d;
61 dev = __f1_dev[0];
62 u32 temp;
63 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
64 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
65 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
66 d.mask |= temp<<21;
67 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
68 d.mask |= (temp & 1); // enable bit
69 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
70 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
71 d.base |= temp<<21;
72 return d;
73}
74
75static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg,
76 u32 io_min, u32 io_max)
77{
78 u32 i;
79 u32 tempreg;
80 /* io range allocation */
81 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020082 for (i = 0; i < node_nums; i++)
zbao2c08f6a2012-07-02 15:32:58 +080083 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020084 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020085 for (i = 0; i < node_nums; i++)
zbao2c08f6a2012-07-02 15:32:58 +080086 pci_write_config32(__f1_dev[i], reg, tempreg);
87}
88
89static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
90{
91 u32 i;
92 u32 tempreg;
93 /* io range allocation */
94 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020095 for (i = 0; i < nodes; i++)
zbao2c08f6a2012-07-02 15:32:58 +080096 pci_write_config32(__f1_dev[i], reg+4, tempreg);
97 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020098 for (i = 0; i < node_nums; i++)
zbao2c08f6a2012-07-02 15:32:58 +080099 pci_write_config32(__f1_dev[i], reg, tempreg);
100}
101
102static device_t get_node_pci(u32 nodeid, u32 fn)
103{
zbaod4627362012-07-23 19:49:40 +0800104#if MAX_NODE_NUMS + CONFIG_CDB >= 32
105 if ((CONFIG_CDB + nodeid) < 32) {
zbao2c08f6a2012-07-02 15:32:58 +0800106 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
107 } else {
108 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
109 }
110#else
111 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
112#endif
113}
114
115static void get_fx_devs(void)
116{
117 int i;
118 for (i = 0; i < MAX_NODE_NUMS; i++) {
119 __f0_dev[i] = get_node_pci(i, 0);
120 __f1_dev[i] = get_node_pci(i, 1);
121 __f2_dev[i] = get_node_pci(i, 2);
122 __f4_dev[i] = get_node_pci(i, 4);
123 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
124 fx_devs = i+1;
125 }
126 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
127 die("Cannot find 0:0x18.[0|1]\n");
128 }
129 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
130}
131
132static u32 f1_read_config32(unsigned reg)
133{
134 if (fx_devs == 0)
135 get_fx_devs();
136 return pci_read_config32(__f1_dev[0], reg);
137}
138
139static void f1_write_config32(unsigned reg, u32 value)
140{
141 int i;
142 if (fx_devs == 0)
143 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200144 for (i = 0; i < fx_devs; i++) {
zbao2c08f6a2012-07-02 15:32:58 +0800145 device_t dev;
146 dev = __f1_dev[i];
147 if (dev && dev->enabled) {
148 pci_write_config32(dev, reg, value);
149 }
150 }
151}
152
153static u32 amdfam15_nodeid(device_t dev)
154{
155#if MAX_NODE_NUMS == 64
156 unsigned busn;
157 busn = dev->bus->secondary;
158 if (busn != CONFIG_CBB) {
159 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
160 } else {
161 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
162 }
163
164#else
165 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
166#endif
167}
168
169static void set_vga_enable_reg(u32 nodeid, u32 linkn)
170{
171 u32 val;
172
173 val = 1 | (nodeid<<4) | (linkn<<12);
174 /* it will routing
175 * (1)mmio 0xa0000:0xbffff
176 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
177 */
178 f1_write_config32(0xf4, val);
179
180}
181
182/**
183 * @return
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100184 * @retval 2 resoure does not exist, usable
185 * @retval 0 resource exists, not usable
zbao2c08f6a2012-07-02 15:32:58 +0800186 * @retval 1 resource exist, resource has been allocated before
187 */
188static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
189 unsigned goal_link)
190{
191 struct resource *res;
192 unsigned nodeid, link = 0;
193 int result;
194 res = 0;
195 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
196 device_t dev;
197 dev = __f0_dev[nodeid];
198 if (!dev)
199 continue;
200 for (link = 0; !res && (link < 8); link++) {
201 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
202 }
203 }
204 result = 2;
205 if (res) {
206 result = 0;
207 if ((goal_link == (link - 1)) &&
208 (goal_nodeid == (nodeid - 1)) &&
209 (res->flags <= 1)) {
210 result = 1;
211 }
212 }
213 return result;
214}
215
216static struct resource *amdfam15_find_iopair(device_t dev, unsigned nodeid, unsigned link)
217{
218 struct resource *resource;
219 u32 free_reg, reg;
220 resource = 0;
221 free_reg = 0;
222 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
223 int result;
224 result = reg_useable(reg, dev, nodeid, link);
225 if (result == 1) {
226 /* I have been allocated this one */
227 break;
228 }
229 else if (result > 1) {
230 /* I have a free register pair */
231 free_reg = reg;
232 }
233 }
234 if (reg > 0xd8) {
235 reg = free_reg; // if no free, the free_reg still be 0
236 }
237
238 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
239
240 return resource;
241}
242
243static struct resource *amdfam15_find_mempair(device_t dev, u32 nodeid, u32 link)
244{
245 struct resource *resource;
246 u32 free_reg, reg;
247 resource = 0;
248 free_reg = 0;
249 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
250 int result;
251 result = reg_useable(reg, dev, nodeid, link);
252 if (result == 1) {
253 /* I have been allocated this one */
254 break;
255 }
256 else if (result > 1) {
257 /* I have a free register pair */
258 free_reg = reg;
259 }
260 }
261 if (reg > 0xb8) {
262 reg = free_reg;
263 }
264
265 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
266 return resource;
267}
268
269static void amdfam15_link_read_bases(device_t dev, u32 nodeid, u32 link)
270{
271 struct resource *resource;
272
273 /* Initialize the io space constraints on the current bus */
274 resource = amdfam15_find_iopair(dev, nodeid, link);
275 if (resource) {
276 u32 align;
277 align = log2(HT_IO_HOST_ALIGN);
278 resource->base = 0;
279 resource->size = 0;
280 resource->align = align;
281 resource->gran = align;
282 resource->limit = 0xffffUL;
283 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
284 }
285
286 /* Initialize the prefetchable memory constraints on the current bus */
287 resource = amdfam15_find_mempair(dev, nodeid, link);
288 if (resource) {
289 resource->base = 0;
290 resource->size = 0;
291 resource->align = log2(HT_MEM_HOST_ALIGN);
292 resource->gran = log2(HT_MEM_HOST_ALIGN);
293 resource->limit = 0xffffffffffULL;
294 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
295 resource->flags |= IORESOURCE_BRIDGE;
296 }
297
298 /* Initialize the memory constraints on the current bus */
299 resource = amdfam15_find_mempair(dev, nodeid, link);
300 if (resource) {
301 resource->base = 0;
302 resource->size = 0;
303 resource->align = log2(HT_MEM_HOST_ALIGN);
304 resource->gran = log2(HT_MEM_HOST_ALIGN);
305 resource->limit = 0xffffffffffULL;
306 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
307 }
308
309}
310
Steven Sherkf4340582013-01-29 16:13:35 -0700311static void nb_read_resources(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800312{
313 u32 nodeid;
314 struct bus *link;
315
316 nodeid = amdfam15_nodeid(dev);
317 for (link = dev->link_list; link; link = link->next) {
318 if (link->children) {
319 amdfam15_link_read_bases(dev, nodeid, link->link_num);
320 }
321 }
Steven Sherk1cbabb02013-02-01 09:22:35 -0700322
323 /*
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800324 * This MMCONF resource must be reserved in the PCI domain.
Steven Sherk1cbabb02013-02-01 09:22:35 -0700325 * It is not honored by the coreboot resource allocator if it is in
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800326 * the CPU_CLUSTER.
Steven Sherk1cbabb02013-02-01 09:22:35 -0700327 */
328#if CONFIG_MMCONF_SUPPORT
329 struct resource *resource = new_resource(dev, 0xc0010058);
330 resource->base = CONFIG_MMCONF_BASE_ADDRESS;
331 resource->size = CONFIG_MMCONF_BUS_NUMBER * 4096 * 256;
332 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
333 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
334#endif
zbao2c08f6a2012-07-02 15:32:58 +0800335}
336
337static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
338{
339 resource_t rbase, rend;
340 unsigned reg, link_num;
341 char buf[50];
342
343 /* Make certain the resource has actually been set */
344 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
345 return;
346 }
347
348 /* If I have already stored this resource don't worry about it */
349 if (resource->flags & IORESOURCE_STORED) {
350 return;
351 }
352
353 /* Only handle PCI memory and IO resources */
354 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
355 return;
356
357 /* Ensure I am actually looking at a resource of function 1 */
358 if ((resource->index & 0xffff) < 0x1000) {
359 return;
360 }
361 /* Get the base address */
362 rbase = resource->base;
363
364 /* Get the limit (rounded up) */
365 rend = resource_end(resource);
366
367 /* Get the register and link */
368 reg = resource->index & 0xfff; // 4k
369 link_num = IOINDEX_LINK(resource->index);
370
371 if (resource->flags & IORESOURCE_IO) {
372 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
373 }
374 else if (resource->flags & IORESOURCE_MEM) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100375 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 +0800376 }
377 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200378 snprintf(buf, sizeof(buf), " <node %x link %x>",
zbao2c08f6a2012-07-02 15:32:58 +0800379 nodeid, link_num);
380 report_resource_stored(dev, resource, buf);
381}
382
383/**
384 * I tried to reuse the resource allocation code in set_resource()
385 * but it is too difficult to deal with the resource allocation magic.
386 */
387
388static void create_vga_resource(device_t dev, unsigned nodeid)
389{
390 struct bus *link;
391
392 /* find out which link the VGA card is connected,
393 * we only deal with the 'first' vga card */
394 for (link = dev->link_list; link; link = link->next) {
395 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
zbaod59d6242012-07-23 19:41:03 +0800396#if CONFIG_MULTIPLE_VGA_ADAPTERS
zbao2c08f6a2012-07-02 15:32:58 +0800397 extern device_t vga_pri; // the primary vga device, defined in device.c
398 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
399 link->secondary,link->subordinate);
400 /* We need to make sure the vga_pri is under the link */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200401 if ((vga_pri->bus->secondary >= link->secondary) &&
402 (vga_pri->bus->secondary <= link->subordinate))
zbao2c08f6a2012-07-02 15:32:58 +0800403#endif
404 break;
405 }
406 }
407
408 /* no VGA card installed */
409 if (link == NULL)
410 return;
411
412 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
413 set_vga_enable_reg(nodeid, sblink);
414}
415
Steven Sherkf4340582013-01-29 16:13:35 -0700416static void nb_set_resources(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800417{
418 unsigned nodeid;
419 struct bus *bus;
420 struct resource *res;
421
422 /* Find the nodeid */
423 nodeid = amdfam15_nodeid(dev);
424
425 create_vga_resource(dev, nodeid); //TODO: do we need this?
426
427 /* Set each resource we have found */
428 for (res = dev->resource_list; res; res = res->next) {
429 set_resource(dev, res, nodeid);
430 }
431
432 for (bus = dev->link_list; bus; bus = bus->next) {
433 if (bus->children) {
434 assign_resources(bus);
435 }
436 }
Steven Sherk1cbabb02013-02-01 09:22:35 -0700437
438 /* Print the MMCONF region if it has been reserved. */
439 res = find_resource(dev, 0xc0010058);
440 if (res) {
441 report_resource_stored(dev, res, " <mmconfig>");
442 }
zbao2c08f6a2012-07-02 15:32:58 +0800443}
444
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100445
446static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200447{
448 void *addr, *current;
449
450 /* Skip the HEST header. */
451 current = (void *)(hest + 1);
452
453 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
454 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700455 current += acpi_create_hest_error_source(hest, current, 0,
456 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200457
458 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
459 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700460 current += acpi_create_hest_error_source(hest, current, 1,
461 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200462
463 return (unsigned long)current;
464}
465
Alexander Couzens5eea4582015-04-12 22:18:55 +0200466static void northbridge_fill_ssdt_generator(device_t device)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200467{
468 msr_t msr;
469 char pscope[] = "\\_SB.PCI0";
470
471 acpigen_write_scope(pscope);
472 msr = rdmsr(TOP_MEM);
473 acpigen_write_name_dword("TOM1", msr.lo);
474 msr = rdmsr(TOP_MEM2);
475 /*
476 * Since XP only implements parts of ACPI 2.0, we can't use a qword
477 * here.
478 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
479 * slide 22ff.
480 * Shift value right by 20 bit to make it fit into 32bit,
481 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
482 */
483 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
484 acpigen_pop_len();
485}
486
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200487static unsigned long agesa_write_acpi_tables(device_t device,
488 unsigned long current,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200489 acpi_rsdp_t *rsdp)
490{
491 acpi_srat_t *srat;
492 acpi_slit_t *slit;
493 acpi_header_t *ssdt;
494 acpi_header_t *alib;
495 acpi_header_t *ivrs;
496 acpi_hest_t *hest;
497
498 /* HEST */
499 current = ALIGN(current, 8);
500 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100501 acpi_write_hest((void *)current, acpi_fill_hest);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200502 acpi_add_table(rsdp, (void *)current);
503 current += ((acpi_header_t *)current)->length;
504
505 current = ALIGN(current, 8);
506 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
507 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
508 if (ivrs != NULL) {
509 memcpy((void *)current, ivrs, ivrs->length);
510 ivrs = (acpi_header_t *) current;
511 current += ivrs->length;
512 acpi_add_table(rsdp, ivrs);
513 } else {
514 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
515 }
516
517 /* SRAT */
518 current = ALIGN(current, 8);
519 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
520 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
521 if (srat != NULL) {
522 memcpy((void *)current, srat, srat->header.length);
523 srat = (acpi_srat_t *) current;
524 current += srat->header.length;
525 acpi_add_table(rsdp, srat);
526 } else {
527 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
528 }
529
530 /* SLIT */
531 current = ALIGN(current, 8);
532 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
533 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
534 if (slit != NULL) {
535 memcpy((void *)current, slit, slit->header.length);
536 slit = (acpi_slit_t *) current;
537 current += slit->header.length;
538 acpi_add_table(rsdp, slit);
539 } else {
540 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
541 }
542
543 /* ALIB */
544 current = ALIGN(current, 16);
545 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
546 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
547 if (alib != NULL) {
548 memcpy((void *)current, alib, alib->length);
549 alib = (acpi_header_t *) current;
550 current += alib->length;
551 acpi_add_table(rsdp, (void *)alib);
552 }
553 else {
554 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
555 }
556
557 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
558 /* SSDT */
559 current = ALIGN(current, 16);
560 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
561 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
562 if (ssdt != NULL) {
563 memcpy((void *)current, ssdt, ssdt->length);
564 ssdt = (acpi_header_t *) current;
565 current += ssdt->length;
566 }
567 else {
568 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
569 }
570 acpi_add_table(rsdp,ssdt);
571
572 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
573
574 return current;
575}
576
577
zbao2c08f6a2012-07-02 15:32:58 +0800578static struct device_operations northbridge_operations = {
Steven Sherkf4340582013-01-29 16:13:35 -0700579 .read_resources = nb_read_resources,
580 .set_resources = nb_set_resources,
zbao2c08f6a2012-07-02 15:32:58 +0800581 .enable_resources = pci_dev_enable_resources,
Edward O'Callaghand994ef12014-11-21 02:22:33 +1100582 .init = DEVICE_NOOP,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200583 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
584 .write_acpi_tables = agesa_write_acpi_tables,
zbao2c08f6a2012-07-02 15:32:58 +0800585 .enable = 0,
586 .ops_pci = 0,
587};
588
589static const struct pci_driver family15_northbridge __pci_driver = {
590 .ops = &northbridge_operations,
591 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600592 .device = PCI_DEVICE_ID_AMD_15H_MODEL_101F_NB_HT,
zbao2c08f6a2012-07-02 15:32:58 +0800593};
594
595static const struct pci_driver family10_northbridge __pci_driver = {
596 .ops = &northbridge_operations,
597 .vendor = PCI_VENDOR_ID_AMD,
598 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
599};
600
601struct chip_operations northbridge_amd_agesa_family15tn_ops = {
602 CHIP_NAME("AMD FAM15 Northbridge")
603 .enable_dev = 0,
604};
605
606static void domain_read_resources(device_t dev)
607{
608 unsigned reg;
609
610 /* Find the already assigned resource pairs */
611 get_fx_devs();
612 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
613 u32 base, limit;
614 base = f1_read_config32(reg);
615 limit = f1_read_config32(reg + 0x04);
616 /* Is this register allocated? */
617 if ((base & 3) != 0) {
618 unsigned nodeid, reg_link;
619 device_t reg_dev;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200620 if (reg < 0xc0) { // mmio
zbao2c08f6a2012-07-02 15:32:58 +0800621 nodeid = (limit & 0xf) + (base&0x30);
622 } else { // io
623 nodeid = (limit & 0xf) + ((base>>4)&0x30);
624 }
625 reg_link = (limit >> 4) & 7;
626 reg_dev = __f0_dev[nodeid];
627 if (reg_dev) {
628 /* Reserve the resource */
629 struct resource *res;
630 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
631 if (res) {
632 res->flags = 1;
633 }
634 }
635 }
636 }
637 /* FIXME: do we need to check extend conf space?
638 I don't believe that much preset value */
639
zbao2c08f6a2012-07-02 15:32:58 +0800640 pci_domain_read_resources(dev);
zbao2c08f6a2012-07-02 15:32:58 +0800641}
642
zbao2c08f6a2012-07-02 15:32:58 +0800643static void domain_enable_resources(device_t dev)
644{
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +0300645 if (acpi_is_wakeup_s3())
Kyösti Mälkki1aa35c62014-10-21 14:19:04 +0300646 agesawrapper_fchs3laterestore();
zbao2c08f6a2012-07-02 15:32:58 +0800647
648 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkkib139b5e2014-10-20 07:41:20 +0300649 if (!acpi_is_wakeup_s3()) {
650 /* Enable MMIO on AMD CPU Address Map Controller */
Kyösti Mälkki48518f02014-11-25 14:20:57 +0200651 amd_initcpuio();
zbao2c08f6a2012-07-02 15:32:58 +0800652
Kyösti Mälkki1aa35c62014-10-21 14:19:04 +0300653 agesawrapper_amdinitmid();
Kyösti Mälkkib139b5e2014-10-20 07:41:20 +0300654 }
Mike Loptiene133aab2013-01-30 16:00:43 -0700655 printk(BIOS_DEBUG, " ader - leaving %s.\n", __func__);
zbao2c08f6a2012-07-02 15:32:58 +0800656}
657
658#if CONFIG_HW_MEM_HOLE_SIZEK != 0
659struct hw_mem_hole_info {
660 unsigned hole_startk;
661 int node_id;
662};
663static struct hw_mem_hole_info get_hw_mem_hole_info(void)
664{
665 struct hw_mem_hole_info mem_hole;
666 int i;
667 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
668 mem_hole.node_id = -1;
669 for (i = 0; i < node_nums; i++) {
670 dram_base_mask_t d;
671 u32 hole;
672 d = get_dram_base_mask(i);
673 if (!(d.mask & 1)) continue; // no memory on this node
674 hole = pci_read_config32(__f1_dev[i], 0xf0);
675 if (hole & 1) { // we find the hole
676 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
677 mem_hole.node_id = i; // record the node No with hole
678 break; // only one hole
679 }
680 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300681
682 /* We need to double check if there is special set on base reg and limit reg
683 * are not continuous instead of hole, it will find out its hole_startk.
684 */
zbao2c08f6a2012-07-02 15:32:58 +0800685 if (mem_hole.node_id == -1) {
686 resource_t limitk_pri = 0;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200687 for (i = 0; i < node_nums; i++) {
zbao2c08f6a2012-07-02 15:32:58 +0800688 dram_base_mask_t d;
689 resource_t base_k, limit_k;
690 d = get_dram_base_mask(i);
691 if (!(d.base & 1)) continue;
692 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
693 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
694 if (limitk_pri != base_k) { // we find the hole
695 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
696 mem_hole.node_id = i;
697 break; //only one hole
698 }
zbao15dc3cc2012-08-03 15:56:21 +0800699 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800700 limitk_pri = limit_k;
701 }
702 }
703 return mem_hole;
704}
705#endif
706
zbao405cfe22012-07-23 19:44:29 +0800707#define ONE_MB_SHIFT 20
zbao6db7f342012-07-19 16:38:12 +0800708
Kyösti Mälkki6b5eb1c2012-07-19 19:26:43 +0300709static void setup_uma_memory(void)
zbao6db7f342012-07-19 16:38:12 +0800710{
711#if CONFIG_GFXUMA
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300712 uint32_t topmem = (uint32_t) bsp_topmem();
zbao6db7f342012-07-19 16:38:12 +0800713 uint32_t sys_mem;
714
zbao6db7f342012-07-19 16:38:12 +0800715 /* refer to UMA Size Consideration in Family15h BKDG. */
716 /* Please reference MemNGetUmaSizeOR () */
717 /*
718 * Total system memory UMASize
719 * >= 2G 512M
720 * >=1G 256M
721 * <1G 64M
722 */
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300723 sys_mem = topmem + (16 << ONE_MB_SHIFT); // Ignore 16MB allocated for C6 when finding UMA size
724 if ((bsp_topmem2()>>32) || (sys_mem >= 2048 << ONE_MB_SHIFT)) {
zbao405cfe22012-07-23 19:44:29 +0800725 uma_memory_size = 512 << ONE_MB_SHIFT;
726 } else if (sys_mem >= 1024 << ONE_MB_SHIFT) {
727 uma_memory_size = 256 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800728 } else {
zbao405cfe22012-07-23 19:44:29 +0800729 uma_memory_size = 64 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800730 }
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300731 uma_memory_base = topmem - uma_memory_size; /* TOP_MEM1 */
zbao6db7f342012-07-19 16:38:12 +0800732
733 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
734 __func__, uma_memory_size, uma_memory_base);
zbao6db7f342012-07-19 16:38:12 +0800735#endif
736}
737
738
zbao2c08f6a2012-07-02 15:32:58 +0800739static void domain_set_resources(device_t dev)
740{
zbao2c08f6a2012-07-02 15:32:58 +0800741 unsigned long mmio_basek;
742 u32 pci_tolm;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300743 u64 ramtop = 0;
zbao2c08f6a2012-07-02 15:32:58 +0800744 int i, idx;
745 struct bus *link;
746#if CONFIG_HW_MEM_HOLE_SIZEK != 0
747 struct hw_mem_hole_info mem_hole;
748 u32 reset_memhole = 1;
749#endif
750
zbao2c08f6a2012-07-02 15:32:58 +0800751 pci_tolm = 0xffffffffUL;
752 for (link = dev->link_list; link; link = link->next) {
753 pci_tolm = find_pci_tolm(link);
754 }
755
756 // FIXME handle interleaved nodes. If you fix this here, please fix
757 // amdk8, too.
758 mmio_basek = pci_tolm >> 10;
759 /* Round mmio_basek to something the processor can support */
760 mmio_basek &= ~((1 << 6) -1);
761
762 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
763 // MMIO hole. If you fix this here, please fix amdk8, too.
764 /* Round the mmio hole to 64M */
765 mmio_basek &= ~((64*1024) - 1);
766
767#if CONFIG_HW_MEM_HOLE_SIZEK != 0
768 /* if the hw mem hole is already set in raminit stage, here we will compare
769 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
770 * use hole_basek as mmio_basek and we don't need to reset hole.
771 * otherwise We reset the hole to the mmio_basek
772 */
773
774 mem_hole = get_hw_mem_hole_info();
775
776 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
777 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
778 mmio_basek = mem_hole.hole_startk;
779 reset_memhole = 0;
780 }
781#endif
782
783 idx = 0x10;
784 for (i = 0; i < node_nums; i++) {
785 dram_base_mask_t d;
786 resource_t basek, limitk, sizek; // 4 1T
787
788 d = get_dram_base_mask(i);
789
790 if (!(d.mask & 1)) continue;
791 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100792 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800793
794 sizek = limitk - basek;
795
796 /* see if we need a hole from 0xa0000 to 0xbffff */
797 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
798 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
799 idx += 0x10;
800 basek = (8*64)+(16*16);
801 sizek = limitk - ((8*64)+(16*16));
802
803 }
804
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300805 /* split the region to accommodate pci memory space */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200806 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
zbao2c08f6a2012-07-02 15:32:58 +0800807 if (basek <= mmio_basek) {
808 unsigned pre_sizek;
809 pre_sizek = mmio_basek - basek;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200810 if (pre_sizek > 0) {
zbao2c08f6a2012-07-02 15:32:58 +0800811 ram_resource(dev, (idx | i), basek, pre_sizek);
812 idx += 0x10;
813 sizek -= pre_sizek;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300814 if (!ramtop)
815 ramtop = mmio_basek * 1024;
zbao2c08f6a2012-07-02 15:32:58 +0800816 }
817 basek = mmio_basek;
818 }
819 if ((basek + sizek) <= 4*1024*1024) {
820 sizek = 0;
821 }
822 else {
Siyuan Wang29840e22013-06-04 19:56:22 +0800823 uint64_t topmem2 = bsp_topmem2();
zbao2c08f6a2012-07-02 15:32:58 +0800824 basek = 4*1024*1024;
Siyuan Wang29840e22013-06-04 19:56:22 +0800825 sizek = topmem2/1024 - basek;
zbao2c08f6a2012-07-02 15:32:58 +0800826 }
827 }
828
zbao2c08f6a2012-07-02 15:32:58 +0800829 ram_resource(dev, (idx | i), basek, sizek);
830 idx += 0x10;
zbao2c08f6a2012-07-02 15:32:58 +0800831 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
832 i, mmio_basek, basek, limitk);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300833 if (!ramtop)
834 ramtop = limitk * 1024;
zbao2c08f6a2012-07-02 15:32:58 +0800835 }
836
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300837#if CONFIG_GFXUMA
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300838 set_top_of_ram(uma_memory_base);
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300839 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300840#else
841 set_top_of_ram(ramtop);
zbao2c08f6a2012-07-02 15:32:58 +0800842#endif
843
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200844 for (link = dev->link_list; link; link = link->next) {
zbao2c08f6a2012-07-02 15:32:58 +0800845 if (link->children) {
846 assign_resources(link);
847 }
848 }
849}
850
851static struct device_operations pci_domain_ops = {
852 .read_resources = domain_read_resources,
853 .set_resources = domain_set_resources,
854 .enable_resources = domain_enable_resources,
Edward O'Callaghane9e1d7a2015-01-02 15:11:49 +1100855 .init = DEVICE_NOOP,
zbao2c08f6a2012-07-02 15:32:58 +0800856 .scan_bus = pci_domain_scan_bus,
Kyösti Mälkki872c9222013-07-03 09:44:28 +0300857 .ops_pci_bus = pci_bus_default_ops,
zbao2c08f6a2012-07-02 15:32:58 +0800858};
859
860static void sysconf_init(device_t dev) // first node
861{
862 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
863 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
864}
865
866static void add_more_links(device_t dev, unsigned total_links)
867{
868 struct bus *link, *last = NULL;
869 int link_num;
870
871 for (link = dev->link_list; link; link = link->next)
872 last = link;
873
874 if (last) {
875 int links = total_links - last->link_num;
876 link_num = last->link_num;
877 if (links > 0) {
878 link = malloc(links*sizeof(*link));
879 if (!link)
880 die("Couldn't allocate more links!\n");
881 memset(link, 0, links*sizeof(*link));
882 last->next = link;
883 }
884 }
885 else {
886 link_num = -1;
887 link = malloc(total_links*sizeof(*link));
888 memset(link, 0, total_links*sizeof(*link));
889 dev->link_list = link;
890 }
891
892 for (link_num = link_num + 1; link_num < total_links; link_num++) {
893 link->link_num = link_num;
894 link->dev = dev;
895 link->next = link + 1;
896 last = link;
897 link = link->next;
898 }
899 last->next = NULL;
900}
901
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200902static void cpu_bus_scan(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800903{
904 struct bus *cpu_bus;
905 device_t dev_mc;
906#if CONFIG_CBB
907 device_t pci_domain;
908#endif
909 int i,j;
910 int coreid_bits;
911 int core_max = 0;
912 unsigned ApicIdCoreIdSize;
913 unsigned core_nums;
914 int siblings = 0;
915 unsigned int family;
916
917#if CONFIG_CBB
918 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
919 if (dev_mc && dev_mc->bus) {
920 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
921 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800922 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +0800923 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
924 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
925 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
926 } else {
927 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
928 }
929 printk(BIOS_DEBUG, "\n");
930 }
931 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
932 if (!dev_mc) {
933 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
934 if (dev_mc && dev_mc->bus) {
935 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
936 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800937 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +0800938 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
939 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
940 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
941 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
942 while (dev_mc) {
943 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
944 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
945 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
946 dev_mc = dev_mc->sibling;
947 }
948 }
949 }
950 }
951 }
952#endif
953 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
954 if (!dev_mc) {
955 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
956 die("");
957 }
958 sysconf_init(dev_mc);
959#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200960 if (node_nums > 32) { // need to put node 32 to node 63 to bus 0xfe
zbao2c08f6a2012-07-02 15:32:58 +0800961 if (pci_domain->link_list && !pci_domain->link_list->next) {
962 struct bus *new_link = new_link(pci_domain);
963 pci_domain->link_list->next = new_link;
964 new_link->link_num = 1;
965 new_link->dev = pci_domain;
966 new_link->children = 0;
967 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
968 }
969 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
970 }
971#endif
972
973 /* Get Max Number of cores(MNC) */
974 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
975 core_max = 1 << (coreid_bits & 0x000F); //mnc
976
977 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
978 if (ApicIdCoreIdSize) {
979 core_nums = (1 << ApicIdCoreIdSize) - 1;
980 } else {
981 core_nums = 3; //quad core
982 }
983
984 /* Find which cpus are present */
985 cpu_bus = dev->link_list;
986 for (i = 0; i < node_nums; i++) {
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +0300987 device_t cdb_dev;
zbao2c08f6a2012-07-02 15:32:58 +0800988 unsigned busn, devn;
989 struct bus *pbus;
990
991 busn = CONFIG_CBB;
992 devn = CONFIG_CDB + i;
993 pbus = dev_mc->bus;
994#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
995 if (i >= 32) {
996 busn--;
997 devn -= 32;
998 pbus = pci_domain->link_list->next;
999 }
1000#endif
1001
1002 /* Find the cpu's pci device */
1003 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1004 if (!cdb_dev) {
1005 /* If I am probing things in a weird order
1006 * ensure all of the cpu's pci devices are found.
1007 */
1008 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001009 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
zbao2c08f6a2012-07-02 15:32:58 +08001010 cdb_dev = pci_probe_dev(NULL, pbus,
1011 PCI_DEVFN(devn, fn));
1012 }
1013 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1014 } else {
1015 /* Ok, We need to set the links for that device.
1016 * otherwise the device under it will not be scanned
1017 */
Kyösti Mälkki2a2d6132015-02-04 13:25:37 +02001018 add_more_links(cdb_dev, 4);
zbao2c08f6a2012-07-02 15:32:58 +08001019 }
1020
1021 family = cpuid_eax(1);
1022 family = (family >> 20) & 0xFF;
1023 if (family == 1) { //f10
1024 u32 dword;
1025 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1026 dword = pci_read_config32(cdb_dev, 0xe8);
1027 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1028 } else if (family == 6) {//f15
1029 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1030 if (cdb_dev && cdb_dev->enabled) {
1031 siblings = pci_read_config32(cdb_dev, 0x84);
1032 siblings &= 0xFF;
1033 }
1034 } else {
1035 siblings = 0; //default one core
1036 }
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001037 int enable_node = cdb_dev && cdb_dev->enabled;
zbao2c08f6a2012-07-02 15:32:58 +08001038 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1039 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1040
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +02001041 for (j = 0; j <= siblings; j++) {
zbao2c08f6a2012-07-02 15:32:58 +08001042 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1043 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1044 u32 lapicid_start = 0;
1045
zbao2c08f6a2012-07-02 15:32:58 +08001046 /*
1047 * APIC ID calucation is tightly coupled with AGESA v5 code.
1048 * This calculation MUST match the assignment calculation done
1049 * in LocalApicInitializationAtEarly() function.
1050 * And reference GetLocalApicIdForCore()
1051 *
1052 * Apply apic enumeration rules
1053 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1054 * put the local-APICs at m..z
1055 *
1056 * This is needed because many IO-APIC devices only have 4 bits
1057 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001058 */
Kyösti Mälkki50036322016-05-18 13:35:21 +03001059
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001060 u8 plat_num_io_apics = 3; /* FIXME */
Kyösti Mälkki50036322016-05-18 13:35:21 +03001061
1062 if ((node_nums * core_max) + plat_num_io_apics >= 0x10) {
1063 lapicid_start = (plat_num_io_apics - 1) / core_max;
zbao2c08f6a2012-07-02 15:32:58 +08001064 lapicid_start = (lapicid_start + 1) * core_max;
1065 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1066 }
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001067 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
zbao2c08f6a2012-07-02 15:32:58 +08001068 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001069 i, j, apic_id);
zbao2c08f6a2012-07-02 15:32:58 +08001070
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001071 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1072 if (cpu)
1073 amd_cpu_topology(cpu, i, j);
zbao2c08f6a2012-07-02 15:32:58 +08001074 } //j
1075 }
zbao2c08f6a2012-07-02 15:32:58 +08001076}
1077
1078static void cpu_bus_init(device_t dev)
1079{
1080 initialize_cpus(dev->link_list);
1081}
1082
zbao2c08f6a2012-07-02 15:32:58 +08001083static struct device_operations cpu_bus_ops = {
Edward O'Callaghan2837ab22014-11-06 08:57:40 +11001084 .read_resources = DEVICE_NOOP,
1085 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001086 .enable_resources = DEVICE_NOOP,
zbao2c08f6a2012-07-02 15:32:58 +08001087 .init = cpu_bus_init,
1088 .scan_bus = cpu_bus_scan,
1089};
1090
1091static void root_complex_enable_dev(struct device *dev)
1092{
Kyösti Mälkki87213b62012-08-27 20:00:33 +03001093 static int done = 0;
1094
1095 /* Do not delay UMA setup, as a device on the PCI bus may evaluate
1096 the global uma_memory variables already in its enable function. */
1097 if (!done) {
1098 setup_bsp_ramtop();
1099 setup_uma_memory();
1100 done = 1;
1101 }
1102
zbao2c08f6a2012-07-02 15:32:58 +08001103 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001104 if (dev->path.type == DEVICE_PATH_DOMAIN) {
zbao2c08f6a2012-07-02 15:32:58 +08001105 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -08001106 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
zbao2c08f6a2012-07-02 15:32:58 +08001107 dev->ops = &cpu_bus_ops;
1108 }
1109}
1110
1111struct chip_operations northbridge_amd_agesa_family15tn_root_complex_ops = {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001112 CHIP_NAME("AMD FAM15tn Root Complex")
zbao2c08f6a2012-07-02 15:32:58 +08001113 .enable_dev = root_complex_enable_dev,
1114};
Dave Frodincbf3d402012-12-05 08:20:12 -07001115
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001116/*********************************************************************
1117 * Change the vendor / device IDs to match the generic VBIOS header. *
1118 *********************************************************************/
Dave Frodincbf3d402012-12-05 08:20:12 -07001119u32 map_oprom_vendev(u32 vendev)
1120{
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001121 u32 new_vendev = vendev;
Dave Frodincbf3d402012-12-05 08:20:12 -07001122
1123 switch(vendev) {
Bruce Griffith42e11f52013-07-08 18:19:08 -06001124 case 0x10029900: /* AMD Radeon HD 7660G (Trinity) */
1125 case 0x10029901: /* AMD Radeon HD 7660D (Trinity) */
1126 case 0x10029903: /* AMD Radeon HD 7640G (Trinity) */
1127 case 0x10029904: /* AMD Radeon HD 7560D (Trinity) */
1128 case 0x10029907: /* AMD Radeon HD 7620G (Trinity) */
1129 case 0x10029908: /* AMD Radeon HD 7600G (Trinity) */
1130 case 0x1002990A: /* AMD Radeon HD 7500G (Trinity) */
1131 case 0x1002990B: /* AMD Radeon HD 8650G (Richland) */
1132 case 0x1002990C: /* AMD Radeon HD 8670D (Richland) */
1133 case 0x1002990D: /* AMD Radeon HD 8550G (Richland) */
1134 case 0x1002990E: /* AMD Radeon HD 8570D (Richland) */
1135 case 0x1002990F: /* AMD Radeon HD 8610G (Richland) */
1136 case 0x10029910: /* AMD Radeon HD 7660G (Trinity) */
1137 case 0x10029913: /* AMD Radeon HD 7640G (Trinity) */
1138 case 0x10029917: /* AMD Radeon HD 7620G (Trinity) */
1139 case 0x10029918: /* AMD Radeon HD 7600G (Trinity) */
1140 case 0x10029919: /* AMD Radeon HD 7500G (Trinity) */
1141 case 0x10029990: /* AMD Radeon HD 7520G (Trinity) */
1142 case 0x10029991: /* AMD Radeon HD 7540D (Trinity) */
1143 case 0x10029992: /* AMD Radeon HD 7420G (Trinity) */
1144 case 0x10029993: /* AMD Radeon HD 7480D (Trinity) */
1145 case 0x10029994: /* AMD Radeon HD 7400G (Trinity) */
1146 case 0x10029995: /* AMD Radeon HD 8450G (Richland) */
1147 case 0x10029996: /* AMD Radeon HD 8470D (Richland) */
1148 case 0x10029997: /* AMD Radeon HD 8350G (Richland) */
1149 case 0x10029998: /* AMD Radeon HD 8370D (Richland) */
1150 case 0x10029999: /* AMD Radeon HD 8510G (Richland) */
1151 case 0x1002999A: /* AMD Radeon HD 8410G (Richland) */
1152 case 0x1002999B: /* AMD Radeon HD 8310G (Richland) */
1153 case 0x1002999C: /* AMD Radeon HD 8650D (Richland) */
1154 case 0x1002999D: /* AMD Radeon HD 8550D (Richland) */
1155 case 0x100299A0: /* AMD Radeon HD 7520G (Trinity) */
1156 case 0x100299A2: /* AMD Radeon HD 7420G (Trinity) */
1157 case 0x100299A4: /* AMD Radeon HD 7400G (Trinity) */
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001158 new_vendev = 0x10029901;
Dave Frodincbf3d402012-12-05 08:20:12 -07001159 break;
1160 }
1161
1162 return new_vendev;
1163}