blob: 7d36b13d296833abc34f4bd068ba2dd684610382 [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 */
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200328 mmconf_resource(dev, 0xc0010058);
zbao2c08f6a2012-07-02 15:32:58 +0800329}
330
331static void set_resource(device_t dev, struct resource *resource, u32 nodeid)
332{
333 resource_t rbase, rend;
334 unsigned reg, link_num;
335 char buf[50];
336
337 /* Make certain the resource has actually been set */
338 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
339 return;
340 }
341
342 /* If I have already stored this resource don't worry about it */
343 if (resource->flags & IORESOURCE_STORED) {
344 return;
345 }
346
347 /* Only handle PCI memory and IO resources */
348 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
349 return;
350
351 /* Ensure I am actually looking at a resource of function 1 */
352 if ((resource->index & 0xffff) < 0x1000) {
353 return;
354 }
355 /* Get the base address */
356 rbase = resource->base;
357
358 /* Get the limit (rounded up) */
359 rend = resource_end(resource);
360
361 /* Get the register and link */
362 reg = resource->index & 0xfff; // 4k
363 link_num = IOINDEX_LINK(resource->index);
364
365 if (resource->flags & IORESOURCE_IO) {
366 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
367 }
368 else if (resource->flags & IORESOURCE_MEM) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100369 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 +0800370 }
371 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200372 snprintf(buf, sizeof(buf), " <node %x link %x>",
zbao2c08f6a2012-07-02 15:32:58 +0800373 nodeid, link_num);
374 report_resource_stored(dev, resource, buf);
375}
376
377/**
378 * I tried to reuse the resource allocation code in set_resource()
379 * but it is too difficult to deal with the resource allocation magic.
380 */
381
382static void create_vga_resource(device_t dev, unsigned nodeid)
383{
384 struct bus *link;
385
386 /* find out which link the VGA card is connected,
387 * we only deal with the 'first' vga card */
388 for (link = dev->link_list; link; link = link->next) {
389 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
zbaod59d6242012-07-23 19:41:03 +0800390#if CONFIG_MULTIPLE_VGA_ADAPTERS
zbao2c08f6a2012-07-02 15:32:58 +0800391 extern device_t vga_pri; // the primary vga device, defined in device.c
392 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
393 link->secondary,link->subordinate);
394 /* We need to make sure the vga_pri is under the link */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200395 if ((vga_pri->bus->secondary >= link->secondary) &&
396 (vga_pri->bus->secondary <= link->subordinate))
zbao2c08f6a2012-07-02 15:32:58 +0800397#endif
398 break;
399 }
400 }
401
402 /* no VGA card installed */
403 if (link == NULL)
404 return;
405
406 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
407 set_vga_enable_reg(nodeid, sblink);
408}
409
Steven Sherkf4340582013-01-29 16:13:35 -0700410static void nb_set_resources(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800411{
412 unsigned nodeid;
413 struct bus *bus;
414 struct resource *res;
415
416 /* Find the nodeid */
417 nodeid = amdfam15_nodeid(dev);
418
419 create_vga_resource(dev, nodeid); //TODO: do we need this?
420
421 /* Set each resource we have found */
422 for (res = dev->resource_list; res; res = res->next) {
423 set_resource(dev, res, nodeid);
424 }
425
426 for (bus = dev->link_list; bus; bus = bus->next) {
427 if (bus->children) {
428 assign_resources(bus);
429 }
430 }
Steven Sherk1cbabb02013-02-01 09:22:35 -0700431
432 /* Print the MMCONF region if it has been reserved. */
433 res = find_resource(dev, 0xc0010058);
434 if (res) {
435 report_resource_stored(dev, res, " <mmconfig>");
436 }
zbao2c08f6a2012-07-02 15:32:58 +0800437}
438
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100439
440static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200441{
442 void *addr, *current;
443
444 /* Skip the HEST header. */
445 current = (void *)(hest + 1);
446
447 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
448 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700449 current += acpi_create_hest_error_source(hest, current, 0,
450 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200451
452 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
453 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700454 current += acpi_create_hest_error_source(hest, current, 1,
455 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200456
457 return (unsigned long)current;
458}
459
Alexander Couzens5eea4582015-04-12 22:18:55 +0200460static void northbridge_fill_ssdt_generator(device_t device)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200461{
462 msr_t msr;
463 char pscope[] = "\\_SB.PCI0";
464
465 acpigen_write_scope(pscope);
466 msr = rdmsr(TOP_MEM);
467 acpigen_write_name_dword("TOM1", msr.lo);
468 msr = rdmsr(TOP_MEM2);
469 /*
470 * Since XP only implements parts of ACPI 2.0, we can't use a qword
471 * here.
472 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
473 * slide 22ff.
474 * Shift value right by 20 bit to make it fit into 32bit,
475 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
476 */
477 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
478 acpigen_pop_len();
479}
480
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200481static unsigned long agesa_write_acpi_tables(device_t device,
482 unsigned long current,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200483 acpi_rsdp_t *rsdp)
484{
485 acpi_srat_t *srat;
486 acpi_slit_t *slit;
487 acpi_header_t *ssdt;
488 acpi_header_t *alib;
489 acpi_header_t *ivrs;
490 acpi_hest_t *hest;
491
492 /* HEST */
493 current = ALIGN(current, 8);
494 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100495 acpi_write_hest((void *)current, acpi_fill_hest);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200496 acpi_add_table(rsdp, (void *)current);
497 current += ((acpi_header_t *)current)->length;
498
499 current = ALIGN(current, 8);
500 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
501 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
502 if (ivrs != NULL) {
503 memcpy((void *)current, ivrs, ivrs->length);
504 ivrs = (acpi_header_t *) current;
505 current += ivrs->length;
506 acpi_add_table(rsdp, ivrs);
507 } else {
508 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
509 }
510
511 /* SRAT */
512 current = ALIGN(current, 8);
513 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
514 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
515 if (srat != NULL) {
516 memcpy((void *)current, srat, srat->header.length);
517 srat = (acpi_srat_t *) current;
518 current += srat->header.length;
519 acpi_add_table(rsdp, srat);
520 } else {
521 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
522 }
523
524 /* SLIT */
525 current = ALIGN(current, 8);
526 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
527 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
528 if (slit != NULL) {
529 memcpy((void *)current, slit, slit->header.length);
530 slit = (acpi_slit_t *) current;
531 current += slit->header.length;
532 acpi_add_table(rsdp, slit);
533 } else {
534 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
535 }
536
537 /* ALIB */
538 current = ALIGN(current, 16);
539 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
540 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
541 if (alib != NULL) {
542 memcpy((void *)current, alib, alib->length);
543 alib = (acpi_header_t *) current;
544 current += alib->length;
545 acpi_add_table(rsdp, (void *)alib);
546 }
547 else {
548 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
549 }
550
551 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
552 /* SSDT */
553 current = ALIGN(current, 16);
554 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
555 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
556 if (ssdt != NULL) {
557 memcpy((void *)current, ssdt, ssdt->length);
558 ssdt = (acpi_header_t *) current;
559 current += ssdt->length;
560 }
561 else {
562 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
563 }
564 acpi_add_table(rsdp,ssdt);
565
566 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
567
568 return current;
569}
570
571
zbao2c08f6a2012-07-02 15:32:58 +0800572static struct device_operations northbridge_operations = {
Steven Sherkf4340582013-01-29 16:13:35 -0700573 .read_resources = nb_read_resources,
574 .set_resources = nb_set_resources,
zbao2c08f6a2012-07-02 15:32:58 +0800575 .enable_resources = pci_dev_enable_resources,
Edward O'Callaghand994ef12014-11-21 02:22:33 +1100576 .init = DEVICE_NOOP,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200577 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
578 .write_acpi_tables = agesa_write_acpi_tables,
zbao2c08f6a2012-07-02 15:32:58 +0800579 .enable = 0,
580 .ops_pci = 0,
581};
582
583static const struct pci_driver family15_northbridge __pci_driver = {
584 .ops = &northbridge_operations,
585 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600586 .device = PCI_DEVICE_ID_AMD_15H_MODEL_101F_NB_HT,
zbao2c08f6a2012-07-02 15:32:58 +0800587};
588
589static const struct pci_driver family10_northbridge __pci_driver = {
590 .ops = &northbridge_operations,
591 .vendor = PCI_VENDOR_ID_AMD,
592 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
593};
594
595struct chip_operations northbridge_amd_agesa_family15tn_ops = {
596 CHIP_NAME("AMD FAM15 Northbridge")
597 .enable_dev = 0,
598};
599
600static void domain_read_resources(device_t dev)
601{
602 unsigned reg;
603
604 /* Find the already assigned resource pairs */
605 get_fx_devs();
606 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
607 u32 base, limit;
608 base = f1_read_config32(reg);
609 limit = f1_read_config32(reg + 0x04);
610 /* Is this register allocated? */
611 if ((base & 3) != 0) {
612 unsigned nodeid, reg_link;
613 device_t reg_dev;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200614 if (reg < 0xc0) { // mmio
zbao2c08f6a2012-07-02 15:32:58 +0800615 nodeid = (limit & 0xf) + (base&0x30);
616 } else { // io
617 nodeid = (limit & 0xf) + ((base>>4)&0x30);
618 }
619 reg_link = (limit >> 4) & 7;
620 reg_dev = __f0_dev[nodeid];
621 if (reg_dev) {
622 /* Reserve the resource */
623 struct resource *res;
624 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
625 if (res) {
626 res->flags = 1;
627 }
628 }
629 }
630 }
631 /* FIXME: do we need to check extend conf space?
632 I don't believe that much preset value */
633
zbao2c08f6a2012-07-02 15:32:58 +0800634 pci_domain_read_resources(dev);
zbao2c08f6a2012-07-02 15:32:58 +0800635}
636
zbao2c08f6a2012-07-02 15:32:58 +0800637static void domain_enable_resources(device_t dev)
638{
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +0300639 if (acpi_is_wakeup_s3())
Kyösti Mälkki1aa35c62014-10-21 14:19:04 +0300640 agesawrapper_fchs3laterestore();
zbao2c08f6a2012-07-02 15:32:58 +0800641
642 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkkib139b5e2014-10-20 07:41:20 +0300643 if (!acpi_is_wakeup_s3()) {
644 /* Enable MMIO on AMD CPU Address Map Controller */
Kyösti Mälkki48518f02014-11-25 14:20:57 +0200645 amd_initcpuio();
zbao2c08f6a2012-07-02 15:32:58 +0800646
Kyösti Mälkki1aa35c62014-10-21 14:19:04 +0300647 agesawrapper_amdinitmid();
Kyösti Mälkkib139b5e2014-10-20 07:41:20 +0300648 }
Mike Loptiene133aab2013-01-30 16:00:43 -0700649 printk(BIOS_DEBUG, " ader - leaving %s.\n", __func__);
zbao2c08f6a2012-07-02 15:32:58 +0800650}
651
652#if CONFIG_HW_MEM_HOLE_SIZEK != 0
653struct hw_mem_hole_info {
654 unsigned hole_startk;
655 int node_id;
656};
657static struct hw_mem_hole_info get_hw_mem_hole_info(void)
658{
659 struct hw_mem_hole_info mem_hole;
660 int i;
661 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
662 mem_hole.node_id = -1;
663 for (i = 0; i < node_nums; i++) {
664 dram_base_mask_t d;
665 u32 hole;
666 d = get_dram_base_mask(i);
667 if (!(d.mask & 1)) continue; // no memory on this node
668 hole = pci_read_config32(__f1_dev[i], 0xf0);
669 if (hole & 1) { // we find the hole
670 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
671 mem_hole.node_id = i; // record the node No with hole
672 break; // only one hole
673 }
674 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300675
676 /* We need to double check if there is special set on base reg and limit reg
677 * are not continuous instead of hole, it will find out its hole_startk.
678 */
zbao2c08f6a2012-07-02 15:32:58 +0800679 if (mem_hole.node_id == -1) {
680 resource_t limitk_pri = 0;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200681 for (i = 0; i < node_nums; i++) {
zbao2c08f6a2012-07-02 15:32:58 +0800682 dram_base_mask_t d;
683 resource_t base_k, limit_k;
684 d = get_dram_base_mask(i);
685 if (!(d.base & 1)) continue;
686 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
687 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
688 if (limitk_pri != base_k) { // we find the hole
689 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
690 mem_hole.node_id = i;
691 break; //only one hole
692 }
zbao15dc3cc2012-08-03 15:56:21 +0800693 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800694 limitk_pri = limit_k;
695 }
696 }
697 return mem_hole;
698}
699#endif
700
zbao405cfe22012-07-23 19:44:29 +0800701#define ONE_MB_SHIFT 20
zbao6db7f342012-07-19 16:38:12 +0800702
Kyösti Mälkki6b5eb1c2012-07-19 19:26:43 +0300703static void setup_uma_memory(void)
zbao6db7f342012-07-19 16:38:12 +0800704{
705#if CONFIG_GFXUMA
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300706 uint32_t topmem = (uint32_t) bsp_topmem();
zbao6db7f342012-07-19 16:38:12 +0800707 uint32_t sys_mem;
708
zbao6db7f342012-07-19 16:38:12 +0800709 /* refer to UMA Size Consideration in Family15h BKDG. */
710 /* Please reference MemNGetUmaSizeOR () */
711 /*
712 * Total system memory UMASize
713 * >= 2G 512M
714 * >=1G 256M
715 * <1G 64M
716 */
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300717 sys_mem = topmem + (16 << ONE_MB_SHIFT); // Ignore 16MB allocated for C6 when finding UMA size
718 if ((bsp_topmem2()>>32) || (sys_mem >= 2048 << ONE_MB_SHIFT)) {
zbao405cfe22012-07-23 19:44:29 +0800719 uma_memory_size = 512 << ONE_MB_SHIFT;
720 } else if (sys_mem >= 1024 << ONE_MB_SHIFT) {
721 uma_memory_size = 256 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800722 } else {
zbao405cfe22012-07-23 19:44:29 +0800723 uma_memory_size = 64 << ONE_MB_SHIFT;
zbao6db7f342012-07-19 16:38:12 +0800724 }
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300725 uma_memory_base = topmem - uma_memory_size; /* TOP_MEM1 */
zbao6db7f342012-07-19 16:38:12 +0800726
727 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
728 __func__, uma_memory_size, uma_memory_base);
zbao6db7f342012-07-19 16:38:12 +0800729#endif
730}
731
732
zbao2c08f6a2012-07-02 15:32:58 +0800733static void domain_set_resources(device_t dev)
734{
zbao2c08f6a2012-07-02 15:32:58 +0800735 unsigned long mmio_basek;
736 u32 pci_tolm;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300737 u64 ramtop = 0;
zbao2c08f6a2012-07-02 15:32:58 +0800738 int i, idx;
739 struct bus *link;
740#if CONFIG_HW_MEM_HOLE_SIZEK != 0
741 struct hw_mem_hole_info mem_hole;
742 u32 reset_memhole = 1;
743#endif
744
zbao2c08f6a2012-07-02 15:32:58 +0800745 pci_tolm = 0xffffffffUL;
746 for (link = dev->link_list; link; link = link->next) {
747 pci_tolm = find_pci_tolm(link);
748 }
749
750 // FIXME handle interleaved nodes. If you fix this here, please fix
751 // amdk8, too.
752 mmio_basek = pci_tolm >> 10;
753 /* Round mmio_basek to something the processor can support */
754 mmio_basek &= ~((1 << 6) -1);
755
756 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
757 // MMIO hole. If you fix this here, please fix amdk8, too.
758 /* Round the mmio hole to 64M */
759 mmio_basek &= ~((64*1024) - 1);
760
761#if CONFIG_HW_MEM_HOLE_SIZEK != 0
762 /* if the hw mem hole is already set in raminit stage, here we will compare
763 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
764 * use hole_basek as mmio_basek and we don't need to reset hole.
765 * otherwise We reset the hole to the mmio_basek
766 */
767
768 mem_hole = get_hw_mem_hole_info();
769
770 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
771 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
772 mmio_basek = mem_hole.hole_startk;
773 reset_memhole = 0;
774 }
775#endif
776
777 idx = 0x10;
778 for (i = 0; i < node_nums; i++) {
779 dram_base_mask_t d;
780 resource_t basek, limitk, sizek; // 4 1T
781
782 d = get_dram_base_mask(i);
783
784 if (!(d.mask & 1)) continue;
785 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100786 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800787
788 sizek = limitk - basek;
789
790 /* see if we need a hole from 0xa0000 to 0xbffff */
791 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
792 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
793 idx += 0x10;
794 basek = (8*64)+(16*16);
795 sizek = limitk - ((8*64)+(16*16));
796
797 }
798
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300799 /* split the region to accommodate pci memory space */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200800 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
zbao2c08f6a2012-07-02 15:32:58 +0800801 if (basek <= mmio_basek) {
802 unsigned pre_sizek;
803 pre_sizek = mmio_basek - basek;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200804 if (pre_sizek > 0) {
zbao2c08f6a2012-07-02 15:32:58 +0800805 ram_resource(dev, (idx | i), basek, pre_sizek);
806 idx += 0x10;
807 sizek -= pre_sizek;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300808 if (!ramtop)
809 ramtop = mmio_basek * 1024;
zbao2c08f6a2012-07-02 15:32:58 +0800810 }
811 basek = mmio_basek;
812 }
813 if ((basek + sizek) <= 4*1024*1024) {
814 sizek = 0;
815 }
816 else {
Siyuan Wang29840e22013-06-04 19:56:22 +0800817 uint64_t topmem2 = bsp_topmem2();
zbao2c08f6a2012-07-02 15:32:58 +0800818 basek = 4*1024*1024;
Siyuan Wang29840e22013-06-04 19:56:22 +0800819 sizek = topmem2/1024 - basek;
zbao2c08f6a2012-07-02 15:32:58 +0800820 }
821 }
822
zbao2c08f6a2012-07-02 15:32:58 +0800823 ram_resource(dev, (idx | i), basek, sizek);
824 idx += 0x10;
zbao2c08f6a2012-07-02 15:32:58 +0800825 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
826 i, mmio_basek, basek, limitk);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300827 if (!ramtop)
828 ramtop = limitk * 1024;
zbao2c08f6a2012-07-02 15:32:58 +0800829 }
830
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300831#if CONFIG_GFXUMA
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300832 set_top_of_ram(uma_memory_base);
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300833 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300834#else
835 set_top_of_ram(ramtop);
zbao2c08f6a2012-07-02 15:32:58 +0800836#endif
837
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200838 for (link = dev->link_list; link; link = link->next) {
zbao2c08f6a2012-07-02 15:32:58 +0800839 if (link->children) {
840 assign_resources(link);
841 }
842 }
843}
844
845static struct device_operations pci_domain_ops = {
846 .read_resources = domain_read_resources,
847 .set_resources = domain_set_resources,
848 .enable_resources = domain_enable_resources,
Edward O'Callaghane9e1d7a2015-01-02 15:11:49 +1100849 .init = DEVICE_NOOP,
zbao2c08f6a2012-07-02 15:32:58 +0800850 .scan_bus = pci_domain_scan_bus,
Kyösti Mälkki872c9222013-07-03 09:44:28 +0300851 .ops_pci_bus = pci_bus_default_ops,
zbao2c08f6a2012-07-02 15:32:58 +0800852};
853
854static void sysconf_init(device_t dev) // first node
855{
856 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
857 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
858}
859
860static void add_more_links(device_t dev, unsigned total_links)
861{
862 struct bus *link, *last = NULL;
863 int link_num;
864
865 for (link = dev->link_list; link; link = link->next)
866 last = link;
867
868 if (last) {
869 int links = total_links - last->link_num;
870 link_num = last->link_num;
871 if (links > 0) {
872 link = malloc(links*sizeof(*link));
873 if (!link)
874 die("Couldn't allocate more links!\n");
875 memset(link, 0, links*sizeof(*link));
876 last->next = link;
877 }
878 }
879 else {
880 link_num = -1;
881 link = malloc(total_links*sizeof(*link));
882 memset(link, 0, total_links*sizeof(*link));
883 dev->link_list = link;
884 }
885
886 for (link_num = link_num + 1; link_num < total_links; link_num++) {
887 link->link_num = link_num;
888 link->dev = dev;
889 link->next = link + 1;
890 last = link;
891 link = link->next;
892 }
893 last->next = NULL;
894}
895
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200896static void cpu_bus_scan(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800897{
898 struct bus *cpu_bus;
899 device_t dev_mc;
900#if CONFIG_CBB
901 device_t pci_domain;
902#endif
903 int i,j;
904 int coreid_bits;
905 int core_max = 0;
906 unsigned ApicIdCoreIdSize;
907 unsigned core_nums;
908 int siblings = 0;
909 unsigned int family;
910
911#if CONFIG_CBB
912 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
913 if (dev_mc && dev_mc->bus) {
914 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
915 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800916 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +0800917 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
918 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
919 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
920 } else {
921 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
922 }
923 printk(BIOS_DEBUG, "\n");
924 }
925 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
926 if (!dev_mc) {
927 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
928 if (dev_mc && dev_mc->bus) {
929 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
930 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800931 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +0800932 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
933 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
934 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
935 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
936 while (dev_mc) {
937 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
938 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
939 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
940 dev_mc = dev_mc->sibling;
941 }
942 }
943 }
944 }
945 }
946#endif
947 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
948 if (!dev_mc) {
949 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
950 die("");
951 }
952 sysconf_init(dev_mc);
953#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200954 if (node_nums > 32) { // need to put node 32 to node 63 to bus 0xfe
zbao2c08f6a2012-07-02 15:32:58 +0800955 if (pci_domain->link_list && !pci_domain->link_list->next) {
956 struct bus *new_link = new_link(pci_domain);
957 pci_domain->link_list->next = new_link;
958 new_link->link_num = 1;
959 new_link->dev = pci_domain;
960 new_link->children = 0;
961 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
962 }
963 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
964 }
965#endif
966
967 /* Get Max Number of cores(MNC) */
968 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
969 core_max = 1 << (coreid_bits & 0x000F); //mnc
970
971 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
972 if (ApicIdCoreIdSize) {
973 core_nums = (1 << ApicIdCoreIdSize) - 1;
974 } else {
975 core_nums = 3; //quad core
976 }
977
978 /* Find which cpus are present */
979 cpu_bus = dev->link_list;
980 for (i = 0; i < node_nums; i++) {
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +0300981 device_t cdb_dev;
zbao2c08f6a2012-07-02 15:32:58 +0800982 unsigned busn, devn;
983 struct bus *pbus;
984
985 busn = CONFIG_CBB;
986 devn = CONFIG_CDB + i;
987 pbus = dev_mc->bus;
988#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
989 if (i >= 32) {
990 busn--;
991 devn -= 32;
992 pbus = pci_domain->link_list->next;
993 }
994#endif
995
996 /* Find the cpu's pci device */
997 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
998 if (!cdb_dev) {
999 /* If I am probing things in a weird order
1000 * ensure all of the cpu's pci devices are found.
1001 */
1002 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001003 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
zbao2c08f6a2012-07-02 15:32:58 +08001004 cdb_dev = pci_probe_dev(NULL, pbus,
1005 PCI_DEVFN(devn, fn));
1006 }
1007 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
1008 } else {
1009 /* Ok, We need to set the links for that device.
1010 * otherwise the device under it will not be scanned
1011 */
Kyösti Mälkki2a2d6132015-02-04 13:25:37 +02001012 add_more_links(cdb_dev, 4);
zbao2c08f6a2012-07-02 15:32:58 +08001013 }
1014
1015 family = cpuid_eax(1);
1016 family = (family >> 20) & 0xFF;
1017 if (family == 1) { //f10
1018 u32 dword;
1019 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
1020 dword = pci_read_config32(cdb_dev, 0xe8);
1021 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1022 } else if (family == 6) {//f15
1023 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
1024 if (cdb_dev && cdb_dev->enabled) {
1025 siblings = pci_read_config32(cdb_dev, 0x84);
1026 siblings &= 0xFF;
1027 }
1028 } else {
1029 siblings = 0; //default one core
1030 }
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001031 int enable_node = cdb_dev && cdb_dev->enabled;
zbao2c08f6a2012-07-02 15:32:58 +08001032 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
1033 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1034
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +02001035 for (j = 0; j <= siblings; j++) {
zbao2c08f6a2012-07-02 15:32:58 +08001036 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
1037 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
1038 u32 lapicid_start = 0;
1039
zbao2c08f6a2012-07-02 15:32:58 +08001040 /*
1041 * APIC ID calucation is tightly coupled with AGESA v5 code.
1042 * This calculation MUST match the assignment calculation done
1043 * in LocalApicInitializationAtEarly() function.
1044 * And reference GetLocalApicIdForCore()
1045 *
1046 * Apply apic enumeration rules
1047 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1048 * put the local-APICs at m..z
1049 *
1050 * This is needed because many IO-APIC devices only have 4 bits
1051 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001052 */
Kyösti Mälkki50036322016-05-18 13:35:21 +03001053
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001054 u8 plat_num_io_apics = 3; /* FIXME */
Kyösti Mälkki50036322016-05-18 13:35:21 +03001055
1056 if ((node_nums * core_max) + plat_num_io_apics >= 0x10) {
1057 lapicid_start = (plat_num_io_apics - 1) / core_max;
zbao2c08f6a2012-07-02 15:32:58 +08001058 lapicid_start = (lapicid_start + 1) * core_max;
1059 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1060 }
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001061 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
zbao2c08f6a2012-07-02 15:32:58 +08001062 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001063 i, j, apic_id);
zbao2c08f6a2012-07-02 15:32:58 +08001064
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001065 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1066 if (cpu)
1067 amd_cpu_topology(cpu, i, j);
zbao2c08f6a2012-07-02 15:32:58 +08001068 } //j
1069 }
zbao2c08f6a2012-07-02 15:32:58 +08001070}
1071
1072static void cpu_bus_init(device_t dev)
1073{
1074 initialize_cpus(dev->link_list);
1075}
1076
zbao2c08f6a2012-07-02 15:32:58 +08001077static struct device_operations cpu_bus_ops = {
Edward O'Callaghan2837ab22014-11-06 08:57:40 +11001078 .read_resources = DEVICE_NOOP,
1079 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001080 .enable_resources = DEVICE_NOOP,
zbao2c08f6a2012-07-02 15:32:58 +08001081 .init = cpu_bus_init,
1082 .scan_bus = cpu_bus_scan,
1083};
1084
1085static void root_complex_enable_dev(struct device *dev)
1086{
Kyösti Mälkki87213b62012-08-27 20:00:33 +03001087 static int done = 0;
1088
1089 /* Do not delay UMA setup, as a device on the PCI bus may evaluate
1090 the global uma_memory variables already in its enable function. */
1091 if (!done) {
1092 setup_bsp_ramtop();
1093 setup_uma_memory();
1094 done = 1;
1095 }
1096
zbao2c08f6a2012-07-02 15:32:58 +08001097 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001098 if (dev->path.type == DEVICE_PATH_DOMAIN) {
zbao2c08f6a2012-07-02 15:32:58 +08001099 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -08001100 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
zbao2c08f6a2012-07-02 15:32:58 +08001101 dev->ops = &cpu_bus_ops;
1102 }
1103}
1104
1105struct chip_operations northbridge_amd_agesa_family15tn_root_complex_ops = {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001106 CHIP_NAME("AMD FAM15tn Root Complex")
zbao2c08f6a2012-07-02 15:32:58 +08001107 .enable_dev = root_complex_enable_dev,
1108};
Dave Frodincbf3d402012-12-05 08:20:12 -07001109
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001110/*********************************************************************
1111 * Change the vendor / device IDs to match the generic VBIOS header. *
1112 *********************************************************************/
Dave Frodincbf3d402012-12-05 08:20:12 -07001113u32 map_oprom_vendev(u32 vendev)
1114{
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001115 u32 new_vendev = vendev;
Dave Frodincbf3d402012-12-05 08:20:12 -07001116
1117 switch(vendev) {
Bruce Griffith42e11f52013-07-08 18:19:08 -06001118 case 0x10029900: /* AMD Radeon HD 7660G (Trinity) */
1119 case 0x10029901: /* AMD Radeon HD 7660D (Trinity) */
1120 case 0x10029903: /* AMD Radeon HD 7640G (Trinity) */
1121 case 0x10029904: /* AMD Radeon HD 7560D (Trinity) */
1122 case 0x10029907: /* AMD Radeon HD 7620G (Trinity) */
1123 case 0x10029908: /* AMD Radeon HD 7600G (Trinity) */
1124 case 0x1002990A: /* AMD Radeon HD 7500G (Trinity) */
1125 case 0x1002990B: /* AMD Radeon HD 8650G (Richland) */
1126 case 0x1002990C: /* AMD Radeon HD 8670D (Richland) */
1127 case 0x1002990D: /* AMD Radeon HD 8550G (Richland) */
1128 case 0x1002990E: /* AMD Radeon HD 8570D (Richland) */
1129 case 0x1002990F: /* AMD Radeon HD 8610G (Richland) */
1130 case 0x10029910: /* AMD Radeon HD 7660G (Trinity) */
1131 case 0x10029913: /* AMD Radeon HD 7640G (Trinity) */
1132 case 0x10029917: /* AMD Radeon HD 7620G (Trinity) */
1133 case 0x10029918: /* AMD Radeon HD 7600G (Trinity) */
1134 case 0x10029919: /* AMD Radeon HD 7500G (Trinity) */
1135 case 0x10029990: /* AMD Radeon HD 7520G (Trinity) */
1136 case 0x10029991: /* AMD Radeon HD 7540D (Trinity) */
1137 case 0x10029992: /* AMD Radeon HD 7420G (Trinity) */
1138 case 0x10029993: /* AMD Radeon HD 7480D (Trinity) */
1139 case 0x10029994: /* AMD Radeon HD 7400G (Trinity) */
1140 case 0x10029995: /* AMD Radeon HD 8450G (Richland) */
1141 case 0x10029996: /* AMD Radeon HD 8470D (Richland) */
1142 case 0x10029997: /* AMD Radeon HD 8350G (Richland) */
1143 case 0x10029998: /* AMD Radeon HD 8370D (Richland) */
1144 case 0x10029999: /* AMD Radeon HD 8510G (Richland) */
1145 case 0x1002999A: /* AMD Radeon HD 8410G (Richland) */
1146 case 0x1002999B: /* AMD Radeon HD 8310G (Richland) */
1147 case 0x1002999C: /* AMD Radeon HD 8650D (Richland) */
1148 case 0x1002999D: /* AMD Radeon HD 8550D (Richland) */
1149 case 0x100299A0: /* AMD Radeon HD 7520G (Trinity) */
1150 case 0x100299A2: /* AMD Radeon HD 7420G (Trinity) */
1151 case 0x100299A4: /* AMD Radeon HD 7400G (Trinity) */
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001152 new_vendev = 0x10029901;
Dave Frodincbf3d402012-12-05 08:20:12 -07001153 break;
1154 }
1155
1156 return new_vendev;
1157}