blob: ab67b43e812cf7ac34f17a3b2c1502a9cfcbe517 [file] [log] [blame]
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 Advanced Micro Devices, Inc.
Timothy Pearson9ef07d82016-06-13 13:48:58 -05005 * Copyright (C) 2016 Raptor Engineering, LLC
6 * Copyright (C) 2018 3mdeb Embedded Systems Consulting
Bruce Griffith27ed80b2014-08-15 11:46:25 -06007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Bruce Griffith27ed80b2014-08-15 11:46:25 -060016 */
17
18#include <console/console.h>
19#include <arch/io.h>
20#include <arch/acpi.h>
21#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>
28#include <lib.h>
29#include <cpu/cpu.h>
30#include <cbmem.h>
31
32#include <Porting.h>
33#include <AGESA.h>
34#include <FieldAccessors.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060035#include <Topology.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030036#include <northbridge/amd/agesa/agesa_helper.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020037#if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER)
Kyösti Mälkki023ed1f2014-10-22 08:05:36 +030038#include <northbridge/amd/pi/agesawrapper.h>
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +030039#include <northbridge/amd/pi/agesawrapper_call.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020040#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -060041#include "northbridge.h"
42
43#include <cpu/x86/lapic.h>
44#include <cpu/amd/mtrr.h>
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +020045#include <arch/acpi.h>
46#include <arch/acpigen.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060047
Kyösti Mälkki113f6702018-05-20 20:12:32 +030048#define MAX_NODE_NUMS MAX_NODES
Bruce Griffith27ed80b2014-08-15 11:46:25 -060049
Bruce Griffith27ed80b2014-08-15 11:46:25 -060050typedef struct dram_base_mask {
51 u32 base; //[47:27] at [28:8]
52 u32 mask; //[47:27] at [28:8] and enable at bit 0
53} dram_base_mask_t;
54
55static unsigned node_nums;
56static unsigned sblink;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030057static struct device *__f0_dev[MAX_NODE_NUMS];
58static struct device *__f1_dev[MAX_NODE_NUMS];
59static struct device *__f2_dev[MAX_NODE_NUMS];
60static struct device *__f4_dev[MAX_NODE_NUMS];
Bruce Griffith27ed80b2014-08-15 11:46:25 -060061static unsigned fx_devs = 0;
62
63static dram_base_mask_t get_dram_base_mask(u32 nodeid)
64{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030065 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060066 dram_base_mask_t d;
67 dev = __f1_dev[0];
68 u32 temp;
69 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
70 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
71 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
72 d.mask |= temp<<21;
73 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
74 d.mask |= (temp & 1); // enable bit
75 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
76 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
77 d.base |= temp<<21;
78 return d;
79}
80
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030081static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Bruce Griffith27ed80b2014-08-15 11:46:25 -060082 u32 io_min, u32 io_max)
83{
84 u32 i;
85 u32 tempreg;
86 /* io range allocation */
87 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060088 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060089 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUASa8131602016-09-19 10:27:57 -060090 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUASa8131602016-09-19 10:27:57 -060091 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060092 pci_write_config32(__f1_dev[i], reg, tempreg);
93}
94
95static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
96{
97 u32 i;
98 u32 tempreg;
99 /* io range allocation */
100 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -0600101 for (i = 0; i < nodes; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600102 pci_write_config32(__f1_dev[i], reg+4, tempreg);
103 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUASa8131602016-09-19 10:27:57 -0600104 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600105 pci_write_config32(__f1_dev[i], reg, tempreg);
106}
107
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300108static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600109{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600110 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600111}
112
113static void get_fx_devs(void)
114{
115 int i;
116 for (i = 0; i < MAX_NODE_NUMS; i++) {
117 __f0_dev[i] = get_node_pci(i, 0);
118 __f1_dev[i] = get_node_pci(i, 1);
119 __f2_dev[i] = get_node_pci(i, 2);
120 __f4_dev[i] = get_node_pci(i, 4);
121 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
122 fx_devs = i+1;
123 }
124 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
125 die("Cannot find 0:0x18.[0|1]\n");
126 }
Elyes HAOUASa8131602016-09-19 10:27:57 -0600127 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600128}
129
130static u32 f1_read_config32(unsigned reg)
131{
132 if (fx_devs == 0)
133 get_fx_devs();
134 return pci_read_config32(__f1_dev[0], reg);
135}
136
137static void f1_write_config32(unsigned reg, u32 value)
138{
139 int i;
140 if (fx_devs == 0)
141 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200142 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300143 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600144 dev = __f1_dev[i];
145 if (dev && dev->enabled) {
146 pci_write_config32(dev, reg, value);
147 }
148 }
149}
150
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300151static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600152{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600153 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600154}
155
156static void set_vga_enable_reg(u32 nodeid, u32 linkn)
157{
158 u32 val;
159
160 val = 1 | (nodeid<<4) | (linkn<<12);
161 /* it will routing
162 * (1)mmio 0xa0000:0xbffff
163 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
164 */
165 f1_write_config32(0xf4, val);
166
167}
168
169/**
170 * @return
171 * @retval 2 resoure does not exist, usable
172 * @retval 0 resource exists, not usable
173 * @retval 1 resource exist, resource has been allocated before
174 */
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300175static int reg_useable(unsigned reg, struct device *goal_dev, unsigned goal_nodeid,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600176 unsigned goal_link)
177{
178 struct resource *res;
179 unsigned nodeid, link = 0;
180 int result;
181 res = 0;
182 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300183 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600184 dev = __f0_dev[nodeid];
185 if (!dev)
186 continue;
187 for (link = 0; !res && (link < 8); link++) {
188 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
189 }
190 }
191 result = 2;
192 if (res) {
193 result = 0;
194 if ((goal_link == (link - 1)) &&
195 (goal_nodeid == (nodeid - 1)) &&
196 (res->flags <= 1)) {
197 result = 1;
198 }
199 }
200 return result;
201}
202
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300203static struct resource *amdfam16_find_iopair(struct device *dev, unsigned nodeid, unsigned link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600204{
205 struct resource *resource;
206 u32 free_reg, reg;
207 resource = 0;
208 free_reg = 0;
209 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
210 int result;
211 result = reg_useable(reg, dev, nodeid, link);
212 if (result == 1) {
213 /* I have been allocated this one */
214 break;
215 }
216 else if (result > 1) {
217 /* I have a free register pair */
218 free_reg = reg;
219 }
220 }
221 if (reg > 0xd8) {
222 reg = free_reg; // if no free, the free_reg still be 0
223 }
224
225 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
226
227 return resource;
228}
229
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300230static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600231{
232 struct resource *resource;
233 u32 free_reg, reg;
234 resource = 0;
235 free_reg = 0;
236 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
237 int result;
238 result = reg_useable(reg, dev, nodeid, link);
239 if (result == 1) {
240 /* I have been allocated this one */
241 break;
242 }
243 else if (result > 1) {
244 /* I have a free register pair */
245 free_reg = reg;
246 }
247 }
248 if (reg > 0xb8) {
249 reg = free_reg;
250 }
251
252 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
253 return resource;
254}
255
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300256static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600257{
258 struct resource *resource;
259
260 /* Initialize the io space constraints on the current bus */
261 resource = amdfam16_find_iopair(dev, nodeid, link);
262 if (resource) {
263 u32 align;
264 align = log2(HT_IO_HOST_ALIGN);
265 resource->base = 0;
266 resource->size = 0;
267 resource->align = align;
268 resource->gran = align;
269 resource->limit = 0xffffUL;
270 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
271 }
272
273 /* Initialize the prefetchable memory constraints on the current bus */
274 resource = amdfam16_find_mempair(dev, nodeid, link);
275 if (resource) {
276 resource->base = 0;
277 resource->size = 0;
278 resource->align = log2(HT_MEM_HOST_ALIGN);
279 resource->gran = log2(HT_MEM_HOST_ALIGN);
280 resource->limit = 0xffffffffffULL;
281 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
282 resource->flags |= IORESOURCE_BRIDGE;
283 }
284
285 /* Initialize the memory constraints on the current bus */
286 resource = amdfam16_find_mempair(dev, nodeid, link);
287 if (resource) {
288 resource->base = 0;
289 resource->size = 0;
290 resource->align = log2(HT_MEM_HOST_ALIGN);
291 resource->gran = log2(HT_MEM_HOST_ALIGN);
292 resource->limit = 0xffffffffffULL;
293 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
294 }
295
296}
297
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300298static void read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600299{
300 u32 nodeid;
301 struct bus *link;
302
303 nodeid = amdfam16_nodeid(dev);
304 for (link = dev->link_list; link; link = link->next) {
305 if (link->children) {
306 amdfam16_link_read_bases(dev, nodeid, link->link_num);
307 }
308 }
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300309
310 /*
311 * This MMCONF resource must be reserved in the PCI domain.
312 * It is not honored by the coreboot resource allocator if it is in
313 * the CPU_CLUSTER.
314 */
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200315 mmconf_resource(dev, 0xc0010058);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600316}
317
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300318static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600319{
320 resource_t rbase, rend;
321 unsigned reg, link_num;
322 char buf[50];
323
324 /* Make certain the resource has actually been set */
325 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
326 return;
327 }
328
329 /* If I have already stored this resource don't worry about it */
330 if (resource->flags & IORESOURCE_STORED) {
331 return;
332 }
333
334 /* Only handle PCI memory and IO resources */
335 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
336 return;
337
338 /* Ensure I am actually looking at a resource of function 1 */
339 if ((resource->index & 0xffff) < 0x1000) {
340 return;
341 }
342 /* Get the base address */
343 rbase = resource->base;
344
345 /* Get the limit (rounded up) */
346 rend = resource_end(resource);
347
348 /* Get the register and link */
349 reg = resource->index & 0xfff; // 4k
350 link_num = IOINDEX_LINK(resource->index);
351
352 if (resource->flags & IORESOURCE_IO) {
353 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
354 }
355 else if (resource->flags & IORESOURCE_MEM) {
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200356 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums); // [39:8]
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600357 }
358 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200359 snprintf(buf, sizeof(buf), " <node %x link %x>",
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600360 nodeid, link_num);
361 report_resource_stored(dev, resource, buf);
362}
363
364/**
365 * I tried to reuse the resource allocation code in set_resource()
366 * but it is too difficult to deal with the resource allocation magic.
367 */
368
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300369static void create_vga_resource(struct device *dev, unsigned nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600370{
371 struct bus *link;
372
373 /* find out which link the VGA card is connected,
374 * we only deal with the 'first' vga card */
375 for (link = dev->link_list; link; link = link->next) {
376 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Martin Roth77a58b92017-06-24 14:45:48 -0600377#if IS_ENABLED(CONFIG_MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300378 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600379 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
380 link->secondary,link->subordinate);
381 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600382 if ((vga_pri->bus->secondary >= link->secondary) &&
383 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600384#endif
385 break;
386 }
387 }
388
389 /* no VGA card installed */
390 if (link == NULL)
391 return;
392
393 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
394 set_vga_enable_reg(nodeid, sblink);
395}
396
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300397static void set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600398{
399 unsigned nodeid;
400 struct bus *bus;
401 struct resource *res;
402
403 /* Find the nodeid */
404 nodeid = amdfam16_nodeid(dev);
405
406 create_vga_resource(dev, nodeid); //TODO: do we need this?
407
408 /* Set each resource we have found */
409 for (res = dev->resource_list; res; res = res->next) {
410 set_resource(dev, res, nodeid);
411 }
412
413 for (bus = dev->link_list; bus; bus = bus->next) {
414 if (bus->children) {
415 assign_resources(bus);
416 }
417 }
418}
419
420static void northbridge_init(struct device *dev)
421{
422}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200423
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100424static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200425{
426 void *addr, *current;
427
428 /* Skip the HEST header. */
429 current = (void *)(hest + 1);
430
431 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
432 if (addr != NULL)
433 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
434
435 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
436 if (addr != NULL)
437 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
438
439 return (unsigned long)current;
440}
441
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500442static void add_ivhd_dev_entry(struct device *parent, struct device *dev,
443 unsigned long *current, uint16_t *length,
444 uint8_t type, uint8_t data)
445{
446 uint8_t *p;
447 p = (uint8_t *) *current;
448
449 if (type == 0x2) {
450 /* Entry type */
451 p[0] = type;
452 /* Device */
453 p[1] = dev->path.pci.devfn;
454 /* Bus */
455 p[2] = dev->bus->secondary;
456 /* Data */
457 p[3] = data;
458 /* [4:7] Padding */
459 p[4] = 0x0;
460 p[5] = 0x0;
461 p[6] = 0x0;
462 p[7] = 0x0;
463 *length += 8;
464 *current += 8;
465 } else if (type == 0x42) {
466 /* Entry type */
467 p[0] = type;
468 /* Device */
469 p[1] = dev->path.pci.devfn;
470 /* Bus */
471 p[2] = dev->bus->secondary;
472 /* Data */
473 p[3] = 0x0;
474 /* Reserved */
475 p[4] = 0x0;
476 /* Device */
477 p[5] = parent->path.pci.devfn;
478 /* Bus */
479 p[6] = parent->bus->secondary;
480 /* Reserved */
481 p[7] = 0x0;
482 *length += 8;
483 *current += 8;
484 }
485}
486
487static void add_ivrs_device_entries(struct device *parent, struct device *dev,
488 unsigned int depth, int linknum, int8_t *root_level,
489 unsigned long *current, uint16_t *length)
490{
491 struct device *sibling;
492 struct bus *link;
493 unsigned int header_type;
494 unsigned int is_pcie;
495
496 if (!root_level) {
497 root_level = malloc(sizeof(int8_t));
498 *root_level = -1;
499 }
500
501 if (dev->path.type == DEVICE_PATH_PCI) {
502
503 if ((dev->bus->secondary == 0x0) &&
504 (dev->path.pci.devfn == 0x0))
505 *root_level = depth;
506
507 if ((*root_level != -1) && (dev->enabled)) {
508 if (depth == *root_level) {
509 if (dev->path.pci.devfn == (0x14 << 3)) {
510 /* SMBUS controller */
511 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x97);
512 } else if (dev->path.pci.devfn != 0x2 &&
513 dev->path.pci.devfn < (0x2 << 3)) {
514 /* FCH control device */
515 } else {
516 /* Other devices */
517 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
518 }
519 } else {
520 header_type = dev->hdr_type & 0x7f;
521 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
522 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
523 (header_type == PCI_HEADER_TYPE_BRIDGE))
524 && is_pcie) {
525 /* Device or Bridge is PCIe */
526 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
527 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) &&
528 !is_pcie) {
529 add_ivhd_dev_entry(parent, dev, current, length, 0x42, 0x0);
530 /* Device is legacy PCI or PCI-X */
531 }
532 }
533 }
534 }
535
536 for (link = dev->link_list; link; link = link->next)
537 for (sibling = link->children; sibling; sibling =
538 sibling->sibling)
539 add_ivrs_device_entries(dev, sibling, depth + 1, depth,
540 root_level, current, length);
541
542 free(root_level);
543}
544
545unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
546{
547 uint8_t *p;
548
549 uint32_t apicid_sb800;
550 uint32_t apicid_northbridge;
551
552 apicid_sb800 = CONFIG_MAX_CPUS;
553 apicid_northbridge = CONFIG_MAX_CPUS + 1;
554
555 /* Describe NB IOAPIC */
556 p = (uint8_t *)current;
557 p[0] = 0x48; /* Entry type */
558 p[1] = 0; /* Device */
559 p[2] = 0; /* Bus */
560 p[3] = 0x0; /* Data */
561 p[4] = apicid_northbridge; /* IOAPIC ID */
562 p[5] = 0x0; /* Device 0 Function 0 */
563 p[6] = 0x0; /* Northbridge bus */
564 p[7] = 0x1; /* Variety */
565 current += 8;
566
567 /* Describe SB IOAPIC */
568 p = (uint8_t *)current;
569 p[0] = 0x48; /* Entry type */
570 p[1] = 0; /* Device */
571 p[2] = 0; /* Bus */
572 p[3] = 0xd7; /* Data */
573 p[4] = apicid_sb800; /* IOAPIC ID */
574 p[5] = 0x14 << 3; /* Device 0x14 Function 0 */
575 p[6] = 0x0; /* Southbridge bus */
576 p[7] = 0x1; /* Variety */
577 current += 8;
578
579 return current;
580}
581
582static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
583{
584 uint8_t *p;
585
Elyes HAOUAS8ce59d52018-09-16 16:57:23 +0200586 struct device *nb_dev = dev_find_slot(0, PCI_DEVFN(0, 0));
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500587 if (!nb_dev) {
588
589 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
590 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
591
592 return (unsigned long)ivrs;
593 }
594
595 ivrs->iv_info = 0x0;
596 /* Maximum supported virtual address size */
597 ivrs->iv_info |= (0x40 << 15);
598 /* Maximum supported physical address size */
599 ivrs->iv_info |= (0x30 << 8);
600 /* Guest virtual address width */
601 ivrs->iv_info |= (0x2 << 5);
602
603 ivrs->ivhd.type = 0x10;
604 ivrs->ivhd.flags = 0x0e;
605 /* Enable ATS support */
606 ivrs->ivhd.flags |= 0x10;
607 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
608 /* BDF <bus>:00.2 */
609 ivrs->ivhd.device_id = 0x2 | (nb_dev->bus->secondary << 8);
610 /* Capability block 0x40 (type 0xf, "Secure device") */
611 ivrs->ivhd.capability_offset = 0x40;
612 ivrs->ivhd.iommu_base_low = 0xfeb00000;
613 ivrs->ivhd.iommu_base_high = 0x0;
614 ivrs->ivhd.pci_segment_group = 0x0;
615 ivrs->ivhd.iommu_info = 0x0;
616 ivrs->ivhd.iommu_info |= (0x13 << 8);
617 ivrs->ivhd.iommu_feature_info = 0x0;
618
619 /* Describe HPET */
620 p = (uint8_t *)current;
621 p[0] = 0x48; /* Entry type */
622 p[1] = 0; /* Device */
623 p[2] = 0; /* Bus */
624 p[3] = 0xd7; /* Data */
625 p[4] = 0x0; /* HPET number */
626 p[5] = 0x14 << 3; /* HPET device */
627 p[6] = nb_dev->bus->secondary; /* HPET bus */
628 p[7] = 0x2; /* Variety */
629 ivrs->ivhd.length += 8;
630 current += 8;
631
632 /* Describe PCI devices */
633 add_ivrs_device_entries(NULL, all_devices, 0, -1, NULL, &current,
634 &ivrs->ivhd.length);
635
636 /* Describe IOAPICs */
637 unsigned long prev_current = current;
638 current = acpi_fill_ivrs_ioapic(ivrs, current);
639 ivrs->ivhd.length += (current - prev_current);
640
641 return current;
642}
643
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300644static void northbridge_fill_ssdt_generator(struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200645{
646 msr_t msr;
647 char pscope[] = "\\_SB.PCI0";
648
649 acpigen_write_scope(pscope);
650 msr = rdmsr(TOP_MEM);
651 acpigen_write_name_dword("TOM1", msr.lo);
652 msr = rdmsr(TOP_MEM2);
653 /*
654 * Since XP only implements parts of ACPI 2.0, we can't use a qword
655 * here.
656 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
657 * slide 22ff.
658 * Shift value right by 20 bit to make it fit into 32bit,
659 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
660 */
661 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
662 acpigen_pop_len();
663}
664
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300665static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200666 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200667 acpi_rsdp_t *rsdp)
668{
669 acpi_srat_t *srat;
670 acpi_slit_t *slit;
671 acpi_header_t *ssdt;
672 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500673 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200674 acpi_hest_t *hest;
675
676 /* HEST */
677 current = ALIGN(current, 8);
678 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100679 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200680 acpi_add_table(rsdp, (void *)current);
681 current += ((acpi_header_t *)current)->length;
682
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500683 /* IVRS */
684 current = ALIGN(current, 8);
685 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
686 ivrs = (acpi_ivrs_t *) current;
687 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
688 current += ivrs->header.length;
689 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200690
691 /* SRAT */
692 current = ALIGN(current, 8);
693 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
694 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
695 if (srat != NULL) {
696 memcpy((void *)current, srat, srat->header.length);
697 srat = (acpi_srat_t *) current;
698 current += srat->header.length;
699 acpi_add_table(rsdp, srat);
700 } else {
701 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
702 }
703
704 /* SLIT */
705 current = ALIGN(current, 8);
706 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
707 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
708 if (slit != NULL) {
709 memcpy((void *)current, slit, slit->header.length);
710 slit = (acpi_slit_t *) current;
711 current += slit->header.length;
712 acpi_add_table(rsdp, slit);
713 } else {
714 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
715 }
716
717 /* ALIB */
718 current = ALIGN(current, 16);
719 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
720 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
721 if (alib != NULL) {
722 memcpy((void *)current, alib, alib->length);
723 alib = (acpi_header_t *) current;
724 current += alib->length;
725 acpi_add_table(rsdp, (void *)alib);
726 }
727 else {
728 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
729 }
730
731 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
732 /* SSDT */
733 current = ALIGN(current, 16);
734 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
735 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
736 if (ssdt != NULL) {
737 memcpy((void *)current, ssdt, ssdt->length);
738 ssdt = (acpi_header_t *) current;
739 current += ssdt->length;
740 }
741 else {
742 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
743 }
744 acpi_add_table(rsdp,ssdt);
745
746 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
747 return current;
748}
749
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600750static struct device_operations northbridge_operations = {
751 .read_resources = read_resources,
752 .set_resources = set_resources,
753 .enable_resources = pci_dev_enable_resources,
754 .init = northbridge_init,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200755 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
756 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600757 .enable = 0,
758 .ops_pci = 0,
759};
760
761static const struct pci_driver family16_northbridge __pci_driver = {
762 .ops = &northbridge_operations,
763 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600764 .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600765};
766
767static const struct pci_driver family10_northbridge __pci_driver = {
768 .ops = &northbridge_operations,
769 .vendor = PCI_VENDOR_ID_AMD,
770 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
771};
772
Dave Frodin891f71a2015-01-19 15:58:24 -0700773static void fam16_finalize(void *chip_info)
774{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300775 struct device *dev;
Dave Frodin891f71a2015-01-19 15:58:24 -0700776 u32 value;
777 dev = dev_find_slot(0, PCI_DEVFN(0, 0)); /* clear IoapicSbFeatureEn */
778 pci_write_config32(dev, 0xF8, 0);
779 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
780
781 /* disable No Snoop */
782 dev = dev_find_slot(0, PCI_DEVFN(1, 1));
783 value = pci_read_config32(dev, 0x60);
784 value &= ~(1 << 11);
785 pci_write_config32(dev, 0x60, value);
786}
787
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300788struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600789 CHIP_NAME("AMD FAM16 Northbridge")
790 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700791 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600792};
793
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300794static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600795{
796 unsigned reg;
797
798 /* Find the already assigned resource pairs */
799 get_fx_devs();
800 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
801 u32 base, limit;
802 base = f1_read_config32(reg);
803 limit = f1_read_config32(reg + 0x04);
804 /* Is this register allocated? */
805 if ((base & 3) != 0) {
806 unsigned nodeid, reg_link;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300807 struct device *reg_dev;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600808 if (reg < 0xc0) { // mmio
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600809 nodeid = (limit & 0xf) + (base&0x30);
810 } else { // io
811 nodeid = (limit & 0xf) + ((base>>4)&0x30);
812 }
813 reg_link = (limit >> 4) & 7;
814 reg_dev = __f0_dev[nodeid];
815 if (reg_dev) {
816 /* Reserve the resource */
817 struct resource *res;
818 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
819 if (res) {
820 res->flags = 1;
821 }
822 }
823 }
824 }
825 /* FIXME: do we need to check extend conf space?
826 I don't believe that much preset value */
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600827 pci_domain_read_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600828}
829
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300830static void domain_enable_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600831{
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200832#if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600833 /* Must be called after PCI enumeration and resource allocation */
834 if (!acpi_is_wakeup_s3())
835 AGESAWRAPPER(amdinitmid);
836
837 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200838#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600839}
840
841#if CONFIG_HW_MEM_HOLE_SIZEK != 0
842struct hw_mem_hole_info {
843 unsigned hole_startk;
844 int node_id;
845};
846static struct hw_mem_hole_info get_hw_mem_hole_info(void)
847{
848 struct hw_mem_hole_info mem_hole;
849 int i;
850 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
851 mem_hole.node_id = -1;
852 for (i = 0; i < node_nums; i++) {
853 dram_base_mask_t d;
854 u32 hole;
855 d = get_dram_base_mask(i);
856 if (!(d.mask & 1)) continue; // no memory on this node
857 hole = pci_read_config32(__f1_dev[i], 0xf0);
858 if (hole & 2) { // we find the hole
859 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
860 mem_hole.node_id = i; // record the node No with hole
861 break; // only one hole
862 }
863 }
864
865 /* We need to double check if there is special set on base reg and limit reg
866 * are not continuous instead of hole, it will find out its hole_startk.
867 */
868 if (mem_hole.node_id == -1) {
869 resource_t limitk_pri = 0;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600870 for (i = 0; i < node_nums; i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600871 dram_base_mask_t d;
872 resource_t base_k, limit_k;
873 d = get_dram_base_mask(i);
874 if (!(d.base & 1)) continue;
875 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
876 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
877 if (limitk_pri != base_k) { // we find the hole
878 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
879 mem_hole.node_id = i;
880 break; //only one hole
881 }
882 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
883 limitk_pri = limit_k;
884 }
885 }
886 return mem_hole;
887}
888#endif
889
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300890static void domain_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600891{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600892 unsigned long mmio_basek;
893 u32 pci_tolm;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600894 int i, idx;
895 struct bus *link;
896#if CONFIG_HW_MEM_HOLE_SIZEK != 0
897 struct hw_mem_hole_info mem_hole;
898 u32 reset_memhole = 1;
899#endif
900
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600901 pci_tolm = 0xffffffffUL;
902 for (link = dev->link_list; link; link = link->next) {
903 pci_tolm = find_pci_tolm(link);
904 }
905
906 // FIXME handle interleaved nodes. If you fix this here, please fix
907 // amdk8, too.
908 mmio_basek = pci_tolm >> 10;
909 /* Round mmio_basek to something the processor can support */
910 mmio_basek &= ~((1 << 6) -1);
911
912 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
913 // MMIO hole. If you fix this here, please fix amdk8, too.
914 /* Round the mmio hole to 64M */
915 mmio_basek &= ~((64*1024) - 1);
916
917#if CONFIG_HW_MEM_HOLE_SIZEK != 0
918 /* if the hw mem hole is already set in raminit stage, here we will compare
919 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
920 * use hole_basek as mmio_basek and we don't need to reset hole.
921 * otherwise We reset the hole to the mmio_basek
922 */
923
924 mem_hole = get_hw_mem_hole_info();
925
926 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
927 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
928 mmio_basek = mem_hole.hole_startk;
929 reset_memhole = 0;
930 }
931#endif
932
933 idx = 0x10;
934 for (i = 0; i < node_nums; i++) {
935 dram_base_mask_t d;
936 resource_t basek, limitk, sizek; // 4 1T
937
938 d = get_dram_base_mask(i);
939
940 if (!(d.mask & 1)) continue;
941 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200942 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600943
944 sizek = limitk - basek;
945
946 /* see if we need a hole from 0xa0000 to 0xbffff */
947 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
948 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
949 idx += 0x10;
950 basek = (8*64)+(16*16);
951 sizek = limitk - ((8*64)+(16*16));
952
953 }
954
955 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
956
957 /* split the region to accommodate pci memory space */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600958 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600959 if (basek <= mmio_basek) {
960 unsigned pre_sizek;
961 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600962 if (pre_sizek > 0) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600963 ram_resource(dev, (idx | i), basek, pre_sizek);
964 idx += 0x10;
965 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600966 }
967 basek = mmio_basek;
968 }
969 if ((basek + sizek) <= 4*1024*1024) {
970 sizek = 0;
971 }
972 else {
973 uint64_t topmem2 = bsp_topmem2();
974 basek = 4*1024*1024;
975 sizek = topmem2/1024 - basek;
976 }
977 }
978
979 ram_resource(dev, (idx | i), basek, sizek);
980 idx += 0x10;
981 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
982 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600983 }
984
Kyösti Mälkkie87564f2017-04-15 20:07:53 +0300985 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600986
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200987 for (link = dev->link_list; link; link = link->next) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600988 if (link->children) {
989 assign_resources(link);
990 }
991 }
992}
993
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600994static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100995{
996 if (dev->path.type == DEVICE_PATH_DOMAIN)
997 return "PCI0";
998
999 return NULL;
1000}
1001
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001002static struct device_operations pci_domain_ops = {
1003 .read_resources = domain_read_resources,
1004 .set_resources = domain_set_resources,
1005 .enable_resources = domain_enable_resources,
1006 .init = NULL,
1007 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001008 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001009};
1010
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001011static void sysconf_init(struct device *dev) // first node
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001012{
1013 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1014 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1015}
1016
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001017static void add_more_links(struct device *dev, unsigned total_links)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001018{
1019 struct bus *link, *last = NULL;
1020 int link_num;
1021
1022 for (link = dev->link_list; link; link = link->next)
1023 last = link;
1024
1025 if (last) {
1026 int links = total_links - last->link_num;
1027 link_num = last->link_num;
1028 if (links > 0) {
1029 link = malloc(links*sizeof(*link));
1030 if (!link)
1031 die("Couldn't allocate more links!\n");
1032 memset(link, 0, links*sizeof(*link));
1033 last->next = link;
1034 }
1035 }
1036 else {
1037 link_num = -1;
1038 link = malloc(total_links*sizeof(*link));
1039 memset(link, 0, total_links*sizeof(*link));
1040 dev->link_list = link;
1041 }
1042
1043 for (link_num = link_num + 1; link_num < total_links; link_num++) {
1044 link->link_num = link_num;
1045 link->dev = dev;
1046 link->next = link + 1;
1047 last = link;
1048 link = link->next;
1049 }
1050 last->next = NULL;
1051}
1052
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001053static void cpu_bus_scan(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001054{
1055 struct bus *cpu_bus;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001056 struct device *dev_mc;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001057 int i,j;
1058 int coreid_bits;
1059 int core_max = 0;
1060 unsigned ApicIdCoreIdSize;
1061 unsigned core_nums;
1062 int siblings = 0;
1063 unsigned int family;
1064 u32 modules = 0;
1065 VOID* modules_ptr = &modules;
1066 BUILD_OPT_CFG* options = NULL;
1067 int ioapic_count = 0;
1068
1069 // TODO Remove the printk's.
1070 printk(BIOS_SPEW, "MullinsPI Debug: Grabbing the AMD Topology Information.\n");
1071 AmdGetValue(AMD_GLOBAL_USER_OPTIONS, (VOID**)&options, sizeof(options));
1072 AmdGetValue(AMD_GLOBAL_NUM_MODULES, &modules_ptr, sizeof(modules));
Alexandru Gagniuc2e0cf142014-12-28 20:38:32 -06001073 modules = *(u32*)modules_ptr;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001074 ASSERT(modules > 0);
1075 ASSERT(options);
1076 ioapic_count = (int)options->CfgPlatNumIoApics;
1077 ASSERT(ioapic_count > 0);
1078 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of Modules (@0x%p) is %d\n", modules_ptr, modules);
1079 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of IOAPICs (@0x%p) is %d\n", options, (int)options->CfgPlatNumIoApics);
1080
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001081 dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0));
1082 if (!dev_mc) {
1083 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
1084 die("");
1085 }
1086 sysconf_init(dev_mc);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001087
1088 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +03001089 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001090 core_max = 1 << (coreid_bits & 0x000F); //mnc
1091
1092 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1093 if (ApicIdCoreIdSize) {
1094 core_nums = (1 << ApicIdCoreIdSize) - 1;
1095 } else {
1096 core_nums = 3; //quad core
1097 }
1098
1099 /* Find which cpus are present */
1100 cpu_bus = dev->link_list;
1101 for (i = 0; i < node_nums; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001102 struct device *cdb_dev;
Kyösti Mälkkiedf51d22018-05-20 22:38:00 +03001103 unsigned devn;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001104 struct bus *pbus;
1105
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001106 devn = CONFIG_CDB + i;
1107 pbus = dev_mc->bus;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001108
1109 /* Find the cpu's pci device */
Kyösti Mälkkiedf51d22018-05-20 22:38:00 +03001110 cdb_dev = dev_find_slot(CONFIG_CBB, PCI_DEVFN(devn, 0));
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001111 if (!cdb_dev) {
1112 /* If I am probing things in a weird order
1113 * ensure all of the cpu's pci devices are found.
1114 */
1115 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001116 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001117 cdb_dev = pci_probe_dev(NULL, pbus,
1118 PCI_DEVFN(devn, fn));
1119 }
Kyösti Mälkkiedf51d22018-05-20 22:38:00 +03001120 cdb_dev = dev_find_slot(CONFIG_CBB, PCI_DEVFN(devn, 0));
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001121 } else {
1122 /* Ok, We need to set the links for that device.
1123 * otherwise the device under it will not be scanned
1124 */
Kyösti Mälkkic5163ed82015-02-04 13:25:37 +02001125
1126 add_more_links(cdb_dev, 4);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001127 }
1128
1129 family = cpuid_eax(1);
1130 family = (family >> 20) & 0xFF;
1131 if (family == 1) { //f10
1132 u32 dword;
Kyösti Mälkkiedf51d22018-05-20 22:38:00 +03001133 cdb_dev = dev_find_slot(CONFIG_CBB, PCI_DEVFN(devn, 3));
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001134 dword = pci_read_config32(cdb_dev, 0xe8);
1135 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1136 } else if (family == 7) {//f16
Kyösti Mälkkiedf51d22018-05-20 22:38:00 +03001137 cdb_dev = dev_find_slot(CONFIG_CBB, PCI_DEVFN(devn, 5));
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001138 if (cdb_dev && cdb_dev->enabled) {
1139 siblings = pci_read_config32(cdb_dev, 0x84);
1140 siblings &= 0xFF;
1141 }
1142 } else {
1143 siblings = 0; //default one core
1144 }
1145 int enable_node = cdb_dev && cdb_dev->enabled;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001146 printk(BIOS_SPEW, "%s family%xh, core_max = 0x%x, core_nums = 0x%x, siblings = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001147 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1148
Elyes HAOUASa8131602016-09-19 10:27:57 -06001149 for (j = 0; j <= siblings; j++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001150 u32 lapicid_start = 0;
1151
1152 /*
1153 * APIC ID calucation is tightly coupled with AGESA v5 code.
1154 * This calculation MUST match the assignment calculation done
1155 * in LocalApicInitializationAtEarly() function.
1156 * And reference GetLocalApicIdForCore()
1157 *
1158 * Apply apic enumeration rules
1159 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1160 * put the local-APICs at m..z
1161 *
1162 * This is needed because many IO-APIC devices only have 4 bits
1163 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001164 */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001165 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1166 lapicid_start = (ioapic_count - 1) / core_max;
1167 lapicid_start = (lapicid_start + 1) * core_max;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001168 printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001169 }
1170 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
Elyes HAOUASa8131602016-09-19 10:27:57 -06001171 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001172 i, j, apic_id);
1173
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001174 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001175 if (cpu)
1176 amd_cpu_topology(cpu, i, j);
1177 } //j
1178 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001179}
1180
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001181static void cpu_bus_init(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001182{
1183 initialize_cpus(dev->link_list);
1184}
1185
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001186static struct device_operations cpu_bus_ops = {
Kyösti Mälkki48f82a92016-12-02 16:02:30 +02001187 .read_resources = DEVICE_NOOP,
1188 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001189 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001190 .init = cpu_bus_init,
1191 .scan_bus = cpu_bus_scan,
1192};
1193
1194static void root_complex_enable_dev(struct device *dev)
1195{
1196 static int done = 0;
1197
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001198 if (!done) {
1199 setup_bsp_ramtop();
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001200 done = 1;
1201 }
1202
1203 /* Set the operations if it is a special bus type */
1204 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1205 dev->ops = &pci_domain_ops;
1206 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1207 dev->ops = &cpu_bus_ops;
1208 }
1209}
1210
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001211struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001212 CHIP_NAME("AMD FAM16 Root Complex")
1213 .enable_dev = root_complex_enable_dev,
1214};
1215
1216/*********************************************************************
1217 * Change the vendor / device IDs to match the generic VBIOS header. *
1218 *********************************************************************/
1219u32 map_oprom_vendev(u32 vendev)
1220{
1221 u32 new_vendev;
1222 new_vendev =
1223 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1224
1225 if (vendev != new_vendev)
1226 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1227
1228 return new_vendev;
1229}