blob: e82dae17d92b92d379b0353d52dc9ee08fc9f113 [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.
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +03005 * Copyright (C) 2014 Edward O'Callaghan <eocallaghan@alterapraxis.com>
zbao2c08f6a2012-07-02 15:32:58 +08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
zbao2c08f6a2012-07-02 15:32:58 +080015 */
16
17#include <console/console.h>
18#include <arch/io.h>
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +030019#include <arch/acpi.h>
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +020020#include <arch/acpigen.h>
zbao2c08f6a2012-07-02 15:32:58 +080021#include <stdint.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <device/pci_ids.h>
25#include <device/hypertransport.h>
26#include <stdlib.h>
27#include <string.h>
Ronald G. Minnich5079a0d2012-11-27 11:32:38 -080028#include <lib.h>
zbao2c08f6a2012-07-02 15:32:58 +080029#include <cpu/cpu.h>
30#include <cbmem.h>
Martin Roth73e86a82013-01-17 16:28:30 -070031#include <AGESA.h>
zbao2c08f6a2012-07-02 15:32:58 +080032
33#include <cpu/x86/lapic.h>
Kyösti Mälkkidbc47392012-08-05 12:11:40 +030034#include <cpu/amd/mtrr.h>
zbao2c08f6a2012-07-02 15:32:58 +080035
36#include <Porting.h>
zbao2c08f6a2012-07-02 15:32:58 +080037#include <Options.h>
38#include <Topology.h>
zbao2c08f6a2012-07-02 15:32:58 +080039#include <cpuRegisters.h>
Kyösti Mälkkid610c582017-03-05 06:28:18 +020040
Kyösti Mälkkif21c2ac2014-10-19 09:35:18 +030041#include <northbridge/amd/agesa/agesawrapper.h>
Kyösti Mälkki28c4d2f2016-11-25 11:21:02 +020042#include <northbridge/amd/agesa/state_machine.h>
Kyösti Mälkkid610c582017-03-05 06:28:18 +020043#include <northbridge/amd/agesa/agesa_helper.h>
zbao2c08f6a2012-07-02 15:32:58 +080044
45#define MAX_NODE_NUMS (MAX_NODES * MAX_DIES)
46
zbao2c08f6a2012-07-02 15:32:58 +080047typedef struct dram_base_mask {
48 u32 base; //[47:27] at [28:8]
49 u32 mask; //[47:27] at [28:8] and enable at bit 0
50} dram_base_mask_t;
51
52static unsigned node_nums;
53static unsigned sblink;
54static device_t __f0_dev[MAX_NODE_NUMS];
55static device_t __f1_dev[MAX_NODE_NUMS];
56static device_t __f2_dev[MAX_NODE_NUMS];
57static device_t __f4_dev[MAX_NODE_NUMS];
58static unsigned fx_devs = 0;
59
60static dram_base_mask_t get_dram_base_mask(u32 nodeid)
61{
62 device_t dev;
63 dram_base_mask_t d;
64 dev = __f1_dev[0];
65 u32 temp;
66 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
67 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
68 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
Elyes HAOUAS27e18012017-06-27 23:14:51 +020069 d.mask |= temp << 21;
zbao2c08f6a2012-07-02 15:32:58 +080070 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
71 d.mask |= (temp & 1); // enable bit
72 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
73 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
Elyes HAOUAS27e18012017-06-27 23:14:51 +020074 d.base |= temp << 21;
zbao2c08f6a2012-07-02 15:32:58 +080075 return d;
76}
77
78static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg,
79 u32 io_min, u32 io_max)
80{
81 u32 i;
82 u32 tempreg;
83 /* io range allocation */
Elyes HAOUAS27e18012017-06-27 23:14:51 +020084 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn << 4) | ((io_max&0xf0)<<(12-4)); //limit
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+4, tempreg);
Elyes HAOUAS27e18012017-06-27 23:14:51 +020087 tempreg = 3 /*| (3 << 4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020088 for (i = 0; i < node_nums; i++)
zbao2c08f6a2012-07-02 15:32:58 +080089 pci_write_config32(__f1_dev[i], reg, tempreg);
90}
91
92static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
93{
94 u32 i;
95 u32 tempreg;
96 /* io range allocation */
Elyes HAOUAS27e18012017-06-27 23:14:51 +020097 tempreg = (nodeid&0xf) | (linkn << 4) | (mmio_max&0xffffff00); //limit
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020098 for (i = 0; i < nodes; i++)
zbao2c08f6a2012-07-02 15:32:58 +080099 pci_write_config32(__f1_dev[i], reg+4, tempreg);
100 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200101 for (i = 0; i < node_nums; i++)
zbao2c08f6a2012-07-02 15:32:58 +0800102 pci_write_config32(__f1_dev[i], reg, tempreg);
103}
104
105static device_t get_node_pci(u32 nodeid, u32 fn)
106{
zbaod4627362012-07-23 19:49:40 +0800107#if MAX_NODE_NUMS + CONFIG_CDB >= 32
108 if ((CONFIG_CDB + nodeid) < 32) {
zbao2c08f6a2012-07-02 15:32:58 +0800109 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
110 } else {
111 return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn));
112 }
113#else
114 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
115#endif
116}
117
118static void get_fx_devs(void)
119{
120 int i;
121 for (i = 0; i < MAX_NODE_NUMS; i++) {
122 __f0_dev[i] = get_node_pci(i, 0);
123 __f1_dev[i] = get_node_pci(i, 1);
124 __f2_dev[i] = get_node_pci(i, 2);
125 __f4_dev[i] = get_node_pci(i, 4);
126 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
127 fx_devs = i+1;
128 }
129 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
130 die("Cannot find 0:0x18.[0|1]\n");
131 }
132 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
133}
134
135static u32 f1_read_config32(unsigned reg)
136{
137 if (fx_devs == 0)
138 get_fx_devs();
139 return pci_read_config32(__f1_dev[0], reg);
140}
141
142static void f1_write_config32(unsigned reg, u32 value)
143{
144 int i;
145 if (fx_devs == 0)
146 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200147 for (i = 0; i < fx_devs; i++) {
zbao2c08f6a2012-07-02 15:32:58 +0800148 device_t dev;
149 dev = __f1_dev[i];
150 if (dev && dev->enabled) {
151 pci_write_config32(dev, reg, value);
152 }
153 }
154}
155
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300156static u32 amdfam15_nodeid(struct device *dev)
zbao2c08f6a2012-07-02 15:32:58 +0800157{
158#if MAX_NODE_NUMS == 64
159 unsigned busn;
160 busn = dev->bus->secondary;
161 if (busn != CONFIG_CBB) {
162 return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32;
163 } else {
164 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
165 }
166
167#else
168 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
169#endif
170}
171
172static void set_vga_enable_reg(u32 nodeid, u32 linkn)
173{
174 u32 val;
175
Elyes HAOUAS27e18012017-06-27 23:14:51 +0200176 val = 1 | (nodeid << 4) | (linkn << 12);
zbao2c08f6a2012-07-02 15:32:58 +0800177 /* it will routing
178 * (1)mmio 0xa0000:0xbffff
179 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
180 */
181 f1_write_config32(0xf4, val);
182
183}
184
185/**
186 * @return
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100187 * @retval 2 resoure does not exist, usable
188 * @retval 0 resource exists, not usable
zbao2c08f6a2012-07-02 15:32:58 +0800189 * @retval 1 resource exist, resource has been allocated before
190 */
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300191static int reg_useable(unsigned reg, struct device *goal_dev, unsigned goal_nodeid,
zbao2c08f6a2012-07-02 15:32:58 +0800192 unsigned goal_link)
193{
194 struct resource *res;
195 unsigned nodeid, link = 0;
196 int result;
197 res = 0;
198 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300199 struct device *dev;
zbao2c08f6a2012-07-02 15:32:58 +0800200 dev = __f0_dev[nodeid];
201 if (!dev)
202 continue;
203 for (link = 0; !res && (link < 8); link++) {
204 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
205 }
206 }
207 result = 2;
208 if (res) {
209 result = 0;
210 if ((goal_link == (link - 1)) &&
211 (goal_nodeid == (nodeid - 1)) &&
212 (res->flags <= 1)) {
213 result = 1;
214 }
215 }
216 return result;
217}
218
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300219static struct resource *amdfam15_find_iopair(struct device *dev, unsigned nodeid, unsigned link)
zbao2c08f6a2012-07-02 15:32:58 +0800220{
221 struct resource *resource;
222 u32 free_reg, reg;
223 resource = 0;
224 free_reg = 0;
225 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
226 int result;
227 result = reg_useable(reg, dev, nodeid, link);
228 if (result == 1) {
229 /* I have been allocated this one */
230 break;
231 }
232 else if (result > 1) {
233 /* I have a free register pair */
234 free_reg = reg;
235 }
236 }
237 if (reg > 0xd8) {
238 reg = free_reg; // if no free, the free_reg still be 0
239 }
240
241 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
242
243 return resource;
244}
245
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300246static struct resource *amdfam15_find_mempair(struct device *dev, u32 nodeid, u32 link)
zbao2c08f6a2012-07-02 15:32:58 +0800247{
248 struct resource *resource;
249 u32 free_reg, reg;
250 resource = 0;
251 free_reg = 0;
252 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
253 int result;
254 result = reg_useable(reg, dev, nodeid, link);
255 if (result == 1) {
256 /* I have been allocated this one */
257 break;
258 }
259 else if (result > 1) {
260 /* I have a free register pair */
261 free_reg = reg;
262 }
263 }
264 if (reg > 0xb8) {
265 reg = free_reg;
266 }
267
268 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
269 return resource;
270}
271
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300272static void amdfam15_link_read_bases(struct device *dev, u32 nodeid, u32 link)
zbao2c08f6a2012-07-02 15:32:58 +0800273{
274 struct resource *resource;
275
276 /* Initialize the io space constraints on the current bus */
277 resource = amdfam15_find_iopair(dev, nodeid, link);
278 if (resource) {
279 u32 align;
280 align = log2(HT_IO_HOST_ALIGN);
281 resource->base = 0;
282 resource->size = 0;
283 resource->align = align;
284 resource->gran = align;
285 resource->limit = 0xffffUL;
286 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
287 }
288
289 /* Initialize the prefetchable memory constraints on the current bus */
290 resource = amdfam15_find_mempair(dev, nodeid, link);
291 if (resource) {
292 resource->base = 0;
293 resource->size = 0;
294 resource->align = log2(HT_MEM_HOST_ALIGN);
295 resource->gran = log2(HT_MEM_HOST_ALIGN);
296 resource->limit = 0xffffffffffULL;
297 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
298 resource->flags |= IORESOURCE_BRIDGE;
299 }
300
301 /* Initialize the 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_BRIDGE;
310 }
311
312}
313
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300314static void nb_read_resources(struct device *dev)
zbao2c08f6a2012-07-02 15:32:58 +0800315{
316 u32 nodeid;
317 struct bus *link;
318
319 nodeid = amdfam15_nodeid(dev);
320 for (link = dev->link_list; link; link = link->next) {
321 if (link->children) {
322 amdfam15_link_read_bases(dev, nodeid, link->link_num);
323 }
324 }
Steven Sherk1cbabb02013-02-01 09:22:35 -0700325
326 /*
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800327 * This MMCONF resource must be reserved in the PCI domain.
Steven Sherk1cbabb02013-02-01 09:22:35 -0700328 * It is not honored by the coreboot resource allocator if it is in
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800329 * the CPU_CLUSTER.
Steven Sherk1cbabb02013-02-01 09:22:35 -0700330 */
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200331 mmconf_resource(dev, 0xc0010058);
zbao2c08f6a2012-07-02 15:32:58 +0800332}
333
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300334static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
zbao2c08f6a2012-07-02 15:32:58 +0800335{
336 resource_t rbase, rend;
337 unsigned reg, link_num;
338 char buf[50];
339
340 /* Make certain the resource has actually been set */
341 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
342 return;
343 }
344
345 /* If I have already stored this resource don't worry about it */
346 if (resource->flags & IORESOURCE_STORED) {
347 return;
348 }
349
350 /* Only handle PCI memory and IO resources */
351 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
352 return;
353
354 /* Ensure I am actually looking at a resource of function 1 */
355 if ((resource->index & 0xffff) < 0x1000) {
356 return;
357 }
358 /* Get the base address */
359 rbase = resource->base;
360
361 /* Get the limit (rounded up) */
362 rend = resource_end(resource);
363
364 /* Get the register and link */
365 reg = resource->index & 0xfff; // 4k
366 link_num = IOINDEX_LINK(resource->index);
367
368 if (resource->flags & IORESOURCE_IO) {
369 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
370 }
371 else if (resource->flags & IORESOURCE_MEM) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100372 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 +0800373 }
374 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200375 snprintf(buf, sizeof(buf), " <node %x link %x>",
zbao2c08f6a2012-07-02 15:32:58 +0800376 nodeid, link_num);
377 report_resource_stored(dev, resource, buf);
378}
379
380/**
381 * I tried to reuse the resource allocation code in set_resource()
382 * but it is too difficult to deal with the resource allocation magic.
383 */
384
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300385static void create_vga_resource(struct device *dev, unsigned nodeid)
zbao2c08f6a2012-07-02 15:32:58 +0800386{
387 struct bus *link;
388
389 /* find out which link the VGA card is connected,
390 * we only deal with the 'first' vga card */
391 for (link = dev->link_list; link; link = link->next) {
392 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Martin Roth77a58b92017-06-24 14:45:48 -0600393#if IS_ENABLED(CONFIG_MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300394 extern struct device *vga_pri; // the primary vga device, defined in device.c
zbao2c08f6a2012-07-02 15:32:58 +0800395 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
396 link->secondary,link->subordinate);
397 /* We need to make sure the vga_pri is under the link */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200398 if ((vga_pri->bus->secondary >= link->secondary) &&
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300399 (vga_pri->bus->secondary <= link->subordinate))
zbao2c08f6a2012-07-02 15:32:58 +0800400#endif
401 break;
402 }
403 }
404
405 /* no VGA card installed */
406 if (link == NULL)
407 return;
408
409 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
410 set_vga_enable_reg(nodeid, sblink);
411}
412
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300413static void nb_set_resources(struct device *dev)
zbao2c08f6a2012-07-02 15:32:58 +0800414{
415 unsigned nodeid;
416 struct bus *bus;
417 struct resource *res;
418
419 /* Find the nodeid */
420 nodeid = amdfam15_nodeid(dev);
421
422 create_vga_resource(dev, nodeid); //TODO: do we need this?
423
424 /* Set each resource we have found */
425 for (res = dev->resource_list; res; res = res->next) {
426 set_resource(dev, res, nodeid);
427 }
428
429 for (bus = dev->link_list; bus; bus = bus->next) {
430 if (bus->children) {
431 assign_resources(bus);
432 }
433 }
434}
435
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100436static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200437{
438 void *addr, *current;
439
440 /* Skip the HEST header. */
441 current = (void *)(hest + 1);
442
443 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
444 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700445 current += acpi_create_hest_error_source(hest, current, 0,
446 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200447
448 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
449 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700450 current += acpi_create_hest_error_source(hest, current, 1,
451 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200452
453 return (unsigned long)current;
454}
455
Alexander Couzens5eea4582015-04-12 22:18:55 +0200456static void northbridge_fill_ssdt_generator(device_t device)
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200457{
458 msr_t msr;
459 char pscope[] = "\\_SB.PCI0";
460
461 acpigen_write_scope(pscope);
462 msr = rdmsr(TOP_MEM);
463 acpigen_write_name_dword("TOM1", msr.lo);
464 msr = rdmsr(TOP_MEM2);
465 /*
466 * Since XP only implements parts of ACPI 2.0, we can't use a qword
467 * here.
468 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
469 * slide 22ff.
470 * Shift value right by 20 bit to make it fit into 32bit,
471 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
472 */
473 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
474 acpigen_pop_len();
475}
476
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200477static unsigned long agesa_write_acpi_tables(device_t device,
478 unsigned long current,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200479 acpi_rsdp_t *rsdp)
480{
481 acpi_srat_t *srat;
482 acpi_slit_t *slit;
483 acpi_header_t *ssdt;
484 acpi_header_t *alib;
485 acpi_header_t *ivrs;
486 acpi_hest_t *hest;
487
488 /* HEST */
489 current = ALIGN(current, 8);
490 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100491 acpi_write_hest((void *)current, acpi_fill_hest);
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200492 acpi_add_table(rsdp, (void *)current);
493 current += ((acpi_header_t *)current)->length;
494
495 current = ALIGN(current, 8);
496 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
497 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
498 if (ivrs != NULL) {
499 memcpy((void *)current, ivrs, ivrs->length);
500 ivrs = (acpi_header_t *) current;
501 current += ivrs->length;
502 acpi_add_table(rsdp, ivrs);
503 } else {
504 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
505 }
506
507 /* SRAT */
508 current = ALIGN(current, 8);
509 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
510 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
511 if (srat != NULL) {
512 memcpy((void *)current, srat, srat->header.length);
513 srat = (acpi_srat_t *) current;
514 current += srat->header.length;
515 acpi_add_table(rsdp, srat);
516 } else {
517 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
518 }
519
520 /* SLIT */
521 current = ALIGN(current, 8);
522 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
523 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
524 if (slit != NULL) {
525 memcpy((void *)current, slit, slit->header.length);
526 slit = (acpi_slit_t *) current;
527 current += slit->header.length;
528 acpi_add_table(rsdp, slit);
529 } else {
530 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
531 }
532
533 /* ALIB */
534 current = ALIGN(current, 16);
535 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
536 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
537 if (alib != NULL) {
538 memcpy((void *)current, alib, alib->length);
539 alib = (acpi_header_t *) current;
540 current += alib->length;
541 acpi_add_table(rsdp, (void *)alib);
542 }
543 else {
544 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
545 }
546
547 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
548 /* SSDT */
549 current = ALIGN(current, 16);
550 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
551 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
552 if (ssdt != NULL) {
553 memcpy((void *)current, ssdt, ssdt->length);
554 ssdt = (acpi_header_t *) current;
555 current += ssdt->length;
556 }
557 else {
558 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
559 }
560 acpi_add_table(rsdp,ssdt);
561
562 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
563
564 return current;
565}
566
567
zbao2c08f6a2012-07-02 15:32:58 +0800568static struct device_operations northbridge_operations = {
Steven Sherkf4340582013-01-29 16:13:35 -0700569 .read_resources = nb_read_resources,
570 .set_resources = nb_set_resources,
zbao2c08f6a2012-07-02 15:32:58 +0800571 .enable_resources = pci_dev_enable_resources,
Edward O'Callaghand994ef12014-11-21 02:22:33 +1100572 .init = DEVICE_NOOP,
Vladimir Serbinenko56f46d82014-10-08 22:06:27 +0200573 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
574 .write_acpi_tables = agesa_write_acpi_tables,
zbao2c08f6a2012-07-02 15:32:58 +0800575 .enable = 0,
576 .ops_pci = 0,
577};
578
579static const struct pci_driver family15_northbridge __pci_driver = {
580 .ops = &northbridge_operations,
581 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600582 .device = PCI_DEVICE_ID_AMD_15H_MODEL_101F_NB_HT,
zbao2c08f6a2012-07-02 15:32:58 +0800583};
584
585static const struct pci_driver family10_northbridge __pci_driver = {
586 .ops = &northbridge_operations,
587 .vendor = PCI_VENDOR_ID_AMD,
588 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
589};
590
591struct chip_operations northbridge_amd_agesa_family15tn_ops = {
592 CHIP_NAME("AMD FAM15 Northbridge")
593 .enable_dev = 0,
594};
595
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300596static void domain_read_resources(struct device *dev)
zbao2c08f6a2012-07-02 15:32:58 +0800597{
598 unsigned reg;
599
600 /* Find the already assigned resource pairs */
601 get_fx_devs();
602 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
603 u32 base, limit;
604 base = f1_read_config32(reg);
605 limit = f1_read_config32(reg + 0x04);
606 /* Is this register allocated? */
607 if ((base & 3) != 0) {
608 unsigned nodeid, reg_link;
609 device_t reg_dev;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200610 if (reg < 0xc0) { // mmio
zbao2c08f6a2012-07-02 15:32:58 +0800611 nodeid = (limit & 0xf) + (base&0x30);
612 } else { // io
613 nodeid = (limit & 0xf) + ((base>>4)&0x30);
614 }
615 reg_link = (limit >> 4) & 7;
616 reg_dev = __f0_dev[nodeid];
617 if (reg_dev) {
618 /* Reserve the resource */
619 struct resource *res;
620 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
621 if (res) {
622 res->flags = 1;
623 }
624 }
625 }
626 }
627 /* FIXME: do we need to check extend conf space?
628 I don't believe that much preset value */
629
zbao2c08f6a2012-07-02 15:32:58 +0800630 pci_domain_read_resources(dev);
zbao2c08f6a2012-07-02 15:32:58 +0800631}
632
zbao2c08f6a2012-07-02 15:32:58 +0800633static void domain_enable_resources(device_t dev)
634{
Kyösti Mälkki28c4d2f2016-11-25 11:21:02 +0200635#if IS_ENABLED(CONFIG_AGESA_LEGACY_WRAPPER)
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +0300636 if (acpi_is_wakeup_s3())
Kyösti Mälkki1aa35c62014-10-21 14:19:04 +0300637 agesawrapper_fchs3laterestore();
zbao2c08f6a2012-07-02 15:32:58 +0800638
639 /* Must be called after PCI enumeration and resource allocation */
Kyösti Mälkkib139b5e2014-10-20 07:41:20 +0300640 if (!acpi_is_wakeup_s3()) {
641 /* Enable MMIO on AMD CPU Address Map Controller */
Kyösti Mälkki48518f02014-11-25 14:20:57 +0200642 amd_initcpuio();
zbao2c08f6a2012-07-02 15:32:58 +0800643
Kyösti Mälkki1aa35c62014-10-21 14:19:04 +0300644 agesawrapper_amdinitmid();
Kyösti Mälkkib139b5e2014-10-20 07:41:20 +0300645 }
Mike Loptiene133aab2013-01-30 16:00:43 -0700646 printk(BIOS_DEBUG, " ader - leaving %s.\n", __func__);
Kyösti Mälkki28c4d2f2016-11-25 11:21:02 +0200647#endif
zbao2c08f6a2012-07-02 15:32:58 +0800648}
649
650#if CONFIG_HW_MEM_HOLE_SIZEK != 0
651struct hw_mem_hole_info {
652 unsigned hole_startk;
653 int node_id;
654};
655static struct hw_mem_hole_info get_hw_mem_hole_info(void)
656{
657 struct hw_mem_hole_info mem_hole;
658 int i;
659 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
660 mem_hole.node_id = -1;
661 for (i = 0; i < node_nums; i++) {
662 dram_base_mask_t d;
663 u32 hole;
664 d = get_dram_base_mask(i);
665 if (!(d.mask & 1)) continue; // no memory on this node
666 hole = pci_read_config32(__f1_dev[i], 0xf0);
667 if (hole & 1) { // we find the hole
Elyes HAOUAS27e18012017-06-27 23:14:51 +0200668 mem_hole.hole_startk = (hole & (0xff << 24)) >> 10;
zbao2c08f6a2012-07-02 15:32:58 +0800669 mem_hole.node_id = i; // record the node No with hole
670 break; // only one hole
671 }
672 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300673
674 /* We need to double check if there is special set on base reg and limit reg
675 * are not continuous instead of hole, it will find out its hole_startk.
676 */
zbao2c08f6a2012-07-02 15:32:58 +0800677 if (mem_hole.node_id == -1) {
678 resource_t limitk_pri = 0;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200679 for (i = 0; i < node_nums; i++) {
zbao2c08f6a2012-07-02 15:32:58 +0800680 dram_base_mask_t d;
681 resource_t base_k, limit_k;
682 d = get_dram_base_mask(i);
683 if (!(d.base & 1)) continue;
684 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
685 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
686 if (limitk_pri != base_k) { // we find the hole
687 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
688 mem_hole.node_id = i;
689 break; //only one hole
690 }
zbao15dc3cc2012-08-03 15:56:21 +0800691 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800692 limitk_pri = limit_k;
693 }
694 }
695 return mem_hole;
696}
697#endif
698
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300699static void domain_set_resources(struct device *dev)
zbao2c08f6a2012-07-02 15:32:58 +0800700{
zbao2c08f6a2012-07-02 15:32:58 +0800701 unsigned long mmio_basek;
702 u32 pci_tolm;
703 int i, idx;
704 struct bus *link;
705#if CONFIG_HW_MEM_HOLE_SIZEK != 0
706 struct hw_mem_hole_info mem_hole;
707 u32 reset_memhole = 1;
708#endif
709
zbao2c08f6a2012-07-02 15:32:58 +0800710 pci_tolm = 0xffffffffUL;
711 for (link = dev->link_list; link; link = link->next) {
712 pci_tolm = find_pci_tolm(link);
713 }
714
715 // FIXME handle interleaved nodes. If you fix this here, please fix
716 // amdk8, too.
717 mmio_basek = pci_tolm >> 10;
718 /* Round mmio_basek to something the processor can support */
719 mmio_basek &= ~((1 << 6) -1);
720
721 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
722 // MMIO hole. If you fix this here, please fix amdk8, too.
723 /* Round the mmio hole to 64M */
724 mmio_basek &= ~((64*1024) - 1);
725
726#if CONFIG_HW_MEM_HOLE_SIZEK != 0
727 /* if the hw mem hole is already set in raminit stage, here we will compare
728 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
729 * use hole_basek as mmio_basek and we don't need to reset hole.
730 * otherwise We reset the hole to the mmio_basek
731 */
732
733 mem_hole = get_hw_mem_hole_info();
734
735 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
736 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
737 mmio_basek = mem_hole.hole_startk;
738 reset_memhole = 0;
739 }
740#endif
741
742 idx = 0x10;
743 for (i = 0; i < node_nums; i++) {
744 dram_base_mask_t d;
745 resource_t basek, limitk, sizek; // 4 1T
746
747 d = get_dram_base_mask(i);
748
749 if (!(d.mask & 1)) continue;
750 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100751 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
zbao2c08f6a2012-07-02 15:32:58 +0800752
753 sizek = limitk - basek;
754
755 /* see if we need a hole from 0xa0000 to 0xbffff */
756 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
757 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
758 idx += 0x10;
759 basek = (8*64)+(16*16);
760 sizek = limitk - ((8*64)+(16*16));
761
762 }
763
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300764 /* split the region to accommodate pci memory space */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200765 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
zbao2c08f6a2012-07-02 15:32:58 +0800766 if (basek <= mmio_basek) {
767 unsigned pre_sizek;
768 pre_sizek = mmio_basek - basek;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200769 if (pre_sizek > 0) {
zbao2c08f6a2012-07-02 15:32:58 +0800770 ram_resource(dev, (idx | i), basek, pre_sizek);
771 idx += 0x10;
772 sizek -= pre_sizek;
zbao2c08f6a2012-07-02 15:32:58 +0800773 }
774 basek = mmio_basek;
775 }
776 if ((basek + sizek) <= 4*1024*1024) {
777 sizek = 0;
778 }
779 else {
Siyuan Wang29840e22013-06-04 19:56:22 +0800780 uint64_t topmem2 = bsp_topmem2();
zbao2c08f6a2012-07-02 15:32:58 +0800781 basek = 4*1024*1024;
Siyuan Wang29840e22013-06-04 19:56:22 +0800782 sizek = topmem2/1024 - basek;
zbao2c08f6a2012-07-02 15:32:58 +0800783 }
784 }
785
zbao2c08f6a2012-07-02 15:32:58 +0800786 ram_resource(dev, (idx | i), basek, sizek);
787 idx += 0x10;
zbao2c08f6a2012-07-02 15:32:58 +0800788 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
789 i, mmio_basek, basek, limitk);
zbao2c08f6a2012-07-02 15:32:58 +0800790 }
791
Kyösti Mälkki61be3602017-04-15 20:07:53 +0300792 add_uma_resource_below_tolm(dev, 7);
zbao2c08f6a2012-07-02 15:32:58 +0800793
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200794 for (link = dev->link_list; link; link = link->next) {
zbao2c08f6a2012-07-02 15:32:58 +0800795 if (link->children) {
796 assign_resources(link);
797 }
798 }
799}
800
801static struct device_operations pci_domain_ops = {
802 .read_resources = domain_read_resources,
803 .set_resources = domain_set_resources,
804 .enable_resources = domain_enable_resources,
Edward O'Callaghane9e1d7a2015-01-02 15:11:49 +1100805 .init = DEVICE_NOOP,
zbao2c08f6a2012-07-02 15:32:58 +0800806 .scan_bus = pci_domain_scan_bus,
Kyösti Mälkki872c9222013-07-03 09:44:28 +0300807 .ops_pci_bus = pci_bus_default_ops,
zbao2c08f6a2012-07-02 15:32:58 +0800808};
809
810static void sysconf_init(device_t dev) // first node
811{
812 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
813 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
814}
815
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +0300816static void add_more_links(struct device *dev, unsigned total_links)
zbao2c08f6a2012-07-02 15:32:58 +0800817{
818 struct bus *link, *last = NULL;
819 int link_num;
820
821 for (link = dev->link_list; link; link = link->next)
822 last = link;
823
824 if (last) {
825 int links = total_links - last->link_num;
826 link_num = last->link_num;
827 if (links > 0) {
828 link = malloc(links*sizeof(*link));
829 if (!link)
830 die("Couldn't allocate more links!\n");
831 memset(link, 0, links*sizeof(*link));
832 last->next = link;
833 }
834 }
835 else {
836 link_num = -1;
837 link = malloc(total_links*sizeof(*link));
838 memset(link, 0, total_links*sizeof(*link));
839 dev->link_list = link;
840 }
841
842 for (link_num = link_num + 1; link_num < total_links; link_num++) {
843 link->link_num = link_num;
844 link->dev = dev;
845 link->next = link + 1;
846 last = link;
847 link = link->next;
848 }
849 last->next = NULL;
850}
851
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200852static void cpu_bus_scan(device_t dev)
zbao2c08f6a2012-07-02 15:32:58 +0800853{
854 struct bus *cpu_bus;
855 device_t dev_mc;
856#if CONFIG_CBB
857 device_t pci_domain;
858#endif
859 int i,j;
860 int coreid_bits;
861 int core_max = 0;
862 unsigned ApicIdCoreIdSize;
863 unsigned core_nums;
864 int siblings = 0;
865 unsigned int family;
866
867#if CONFIG_CBB
868 dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00
869 if (dev_mc && dev_mc->bus) {
870 printk(BIOS_DEBUG, "%s found", dev_path(dev_mc));
871 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800872 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +0800873 printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc));
874 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
875 printk(BIOS_DEBUG, "%s",dev_path(dev_mc));
876 } else {
877 printk(BIOS_DEBUG, " but it is not under pci_domain directly ");
878 }
879 printk(BIOS_DEBUG, "\n");
880 }
881 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
882 if (!dev_mc) {
883 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
884 if (dev_mc && dev_mc->bus) {
885 printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc));
886 pci_domain = dev_mc->bus->dev;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800887 if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) {
zbao2c08f6a2012-07-02 15:32:58 +0800888 if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) {
889 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
890 dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff
891 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
892 while (dev_mc) {
893 printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc));
894 dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0);
895 printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc));
896 dev_mc = dev_mc->sibling;
897 }
898 }
899 }
900 }
901 }
902#endif
903 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
904 if (!dev_mc) {
905 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
906 die("");
907 }
908 sysconf_init(dev_mc);
909#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200910 if (node_nums > 32) { // need to put node 32 to node 63 to bus 0xfe
zbao2c08f6a2012-07-02 15:32:58 +0800911 if (pci_domain->link_list && !pci_domain->link_list->next) {
912 struct bus *new_link = new_link(pci_domain);
913 pci_domain->link_list->next = new_link;
914 new_link->link_num = 1;
915 new_link->dev = pci_domain;
916 new_link->children = 0;
917 printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain));
918 }
919 pci_domain->link_list->next->secondary = CONFIG_CBB - 1;
920 }
921#endif
922
923 /* Get Max Number of cores(MNC) */
924 coreid_bits = (cpuid_ecx(AMD_CPUID_ASIZE_PCCOUNT) & 0x0000F000) >> 12;
925 core_max = 1 << (coreid_bits & 0x000F); //mnc
926
927 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
928 if (ApicIdCoreIdSize) {
929 core_nums = (1 << ApicIdCoreIdSize) - 1;
930 } else {
931 core_nums = 3; //quad core
932 }
933
934 /* Find which cpus are present */
935 cpu_bus = dev->link_list;
936 for (i = 0; i < node_nums; i++) {
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +0300937 device_t cdb_dev;
zbao2c08f6a2012-07-02 15:32:58 +0800938 unsigned busn, devn;
939 struct bus *pbus;
940
941 busn = CONFIG_CBB;
942 devn = CONFIG_CDB + i;
943 pbus = dev_mc->bus;
944#if CONFIG_CBB && (MAX_NODE_NUMS > 32)
945 if (i >= 32) {
946 busn--;
947 devn -= 32;
948 pbus = pci_domain->link_list->next;
949 }
950#endif
951
952 /* Find the cpu's pci device */
953 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
954 if (!cdb_dev) {
955 /* If I am probing things in a weird order
956 * ensure all of the cpu's pci devices are found.
957 */
958 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200959 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
zbao2c08f6a2012-07-02 15:32:58 +0800960 cdb_dev = pci_probe_dev(NULL, pbus,
961 PCI_DEVFN(devn, fn));
962 }
963 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0));
964 } else {
965 /* Ok, We need to set the links for that device.
966 * otherwise the device under it will not be scanned
967 */
Kyösti Mälkki2a2d6132015-02-04 13:25:37 +0200968 add_more_links(cdb_dev, 4);
zbao2c08f6a2012-07-02 15:32:58 +0800969 }
970
971 family = cpuid_eax(1);
972 family = (family >> 20) & 0xFF;
973 if (family == 1) { //f10
974 u32 dword;
975 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3));
976 dword = pci_read_config32(cdb_dev, 0xe8);
977 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
978 } else if (family == 6) {//f15
979 cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5));
980 if (cdb_dev && cdb_dev->enabled) {
981 siblings = pci_read_config32(cdb_dev, 0x84);
982 siblings &= 0xFF;
983 }
984 } else {
985 siblings = 0; //default one core
986 }
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +0300987 int enable_node = cdb_dev && cdb_dev->enabled;
zbao2c08f6a2012-07-02 15:32:58 +0800988 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
989 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
990
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200991 for (j = 0; j <= siblings; j++) {
zbao2c08f6a2012-07-02 15:32:58 +0800992 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
993 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
994 u32 lapicid_start = 0;
995
zbao2c08f6a2012-07-02 15:32:58 +0800996 /*
997 * APIC ID calucation is tightly coupled with AGESA v5 code.
998 * This calculation MUST match the assignment calculation done
999 * in LocalApicInitializationAtEarly() function.
1000 * And reference GetLocalApicIdForCore()
1001 *
1002 * Apply apic enumeration rules
1003 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1004 * put the local-APICs at m..z
1005 *
1006 * This is needed because many IO-APIC devices only have 4 bits
1007 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001008 */
Kyösti Mälkki50036322016-05-18 13:35:21 +03001009
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001010 u8 plat_num_io_apics = 3; /* FIXME */
Kyösti Mälkki50036322016-05-18 13:35:21 +03001011
1012 if ((node_nums * core_max) + plat_num_io_apics >= 0x10) {
1013 lapicid_start = (plat_num_io_apics - 1) / core_max;
zbao2c08f6a2012-07-02 15:32:58 +08001014 lapicid_start = (lapicid_start + 1) * core_max;
1015 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
1016 }
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001017 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
zbao2c08f6a2012-07-02 15:32:58 +08001018 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001019 i, j, apic_id);
zbao2c08f6a2012-07-02 15:32:58 +08001020
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001021 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1022 if (cpu)
1023 amd_cpu_topology(cpu, i, j);
zbao2c08f6a2012-07-02 15:32:58 +08001024 } //j
1025 }
zbao2c08f6a2012-07-02 15:32:58 +08001026}
1027
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +03001028static void cpu_bus_init(struct device *dev)
zbao2c08f6a2012-07-02 15:32:58 +08001029{
1030 initialize_cpus(dev->link_list);
1031}
1032
zbao2c08f6a2012-07-02 15:32:58 +08001033static struct device_operations cpu_bus_ops = {
Edward O'Callaghan2837ab22014-11-06 08:57:40 +11001034 .read_resources = DEVICE_NOOP,
1035 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001036 .enable_resources = DEVICE_NOOP,
zbao2c08f6a2012-07-02 15:32:58 +08001037 .init = cpu_bus_init,
1038 .scan_bus = cpu_bus_scan,
1039};
1040
1041static void root_complex_enable_dev(struct device *dev)
1042{
Kyösti Mälkki87213b62012-08-27 20:00:33 +03001043 static int done = 0;
1044
Kyösti Mälkki87213b62012-08-27 20:00:33 +03001045 if (!done) {
1046 setup_bsp_ramtop();
Kyösti Mälkki87213b62012-08-27 20:00:33 +03001047 done = 1;
1048 }
1049
zbao2c08f6a2012-07-02 15:32:58 +08001050 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001051 if (dev->path.type == DEVICE_PATH_DOMAIN) {
zbao2c08f6a2012-07-02 15:32:58 +08001052 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -08001053 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
zbao2c08f6a2012-07-02 15:32:58 +08001054 dev->ops = &cpu_bus_ops;
1055 }
1056}
1057
1058struct chip_operations northbridge_amd_agesa_family15tn_root_complex_ops = {
Kyösti Mälkki2900d4b2017-07-25 14:55:29 +03001059 CHIP_NAME("AMD Family 15tn Root Complex")
zbao2c08f6a2012-07-02 15:32:58 +08001060 .enable_dev = root_complex_enable_dev,
1061};
Dave Frodincbf3d402012-12-05 08:20:12 -07001062
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001063/*********************************************************************
1064 * Change the vendor / device IDs to match the generic VBIOS header. *
1065 *********************************************************************/
Dave Frodincbf3d402012-12-05 08:20:12 -07001066u32 map_oprom_vendev(u32 vendev)
1067{
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001068 u32 new_vendev = vendev;
Dave Frodincbf3d402012-12-05 08:20:12 -07001069
1070 switch(vendev) {
Bruce Griffith42e11f52013-07-08 18:19:08 -06001071 case 0x10029900: /* AMD Radeon HD 7660G (Trinity) */
1072 case 0x10029901: /* AMD Radeon HD 7660D (Trinity) */
1073 case 0x10029903: /* AMD Radeon HD 7640G (Trinity) */
1074 case 0x10029904: /* AMD Radeon HD 7560D (Trinity) */
1075 case 0x10029907: /* AMD Radeon HD 7620G (Trinity) */
1076 case 0x10029908: /* AMD Radeon HD 7600G (Trinity) */
1077 case 0x1002990A: /* AMD Radeon HD 7500G (Trinity) */
1078 case 0x1002990B: /* AMD Radeon HD 8650G (Richland) */
1079 case 0x1002990C: /* AMD Radeon HD 8670D (Richland) */
1080 case 0x1002990D: /* AMD Radeon HD 8550G (Richland) */
1081 case 0x1002990E: /* AMD Radeon HD 8570D (Richland) */
1082 case 0x1002990F: /* AMD Radeon HD 8610G (Richland) */
1083 case 0x10029910: /* AMD Radeon HD 7660G (Trinity) */
1084 case 0x10029913: /* AMD Radeon HD 7640G (Trinity) */
1085 case 0x10029917: /* AMD Radeon HD 7620G (Trinity) */
1086 case 0x10029918: /* AMD Radeon HD 7600G (Trinity) */
1087 case 0x10029919: /* AMD Radeon HD 7500G (Trinity) */
1088 case 0x10029990: /* AMD Radeon HD 7520G (Trinity) */
1089 case 0x10029991: /* AMD Radeon HD 7540D (Trinity) */
1090 case 0x10029992: /* AMD Radeon HD 7420G (Trinity) */
1091 case 0x10029993: /* AMD Radeon HD 7480D (Trinity) */
1092 case 0x10029994: /* AMD Radeon HD 7400G (Trinity) */
1093 case 0x10029995: /* AMD Radeon HD 8450G (Richland) */
1094 case 0x10029996: /* AMD Radeon HD 8470D (Richland) */
1095 case 0x10029997: /* AMD Radeon HD 8350G (Richland) */
1096 case 0x10029998: /* AMD Radeon HD 8370D (Richland) */
1097 case 0x10029999: /* AMD Radeon HD 8510G (Richland) */
1098 case 0x1002999A: /* AMD Radeon HD 8410G (Richland) */
1099 case 0x1002999B: /* AMD Radeon HD 8310G (Richland) */
1100 case 0x1002999C: /* AMD Radeon HD 8650D (Richland) */
1101 case 0x1002999D: /* AMD Radeon HD 8550D (Richland) */
1102 case 0x100299A0: /* AMD Radeon HD 7520G (Trinity) */
1103 case 0x100299A2: /* AMD Radeon HD 7420G (Trinity) */
1104 case 0x100299A4: /* AMD Radeon HD 7400G (Trinity) */
Edward O'Callaghanae5fd342014-11-20 19:58:09 +11001105 new_vendev = 0x10029901;
Dave Frodincbf3d402012-12-05 08:20:12 -07001106 break;
1107 }
1108
1109 return new_vendev;
1110}