blob: 2579d370a7a0216284958f2a5a321bade3d0da0f [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>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060030#include <Porting.h>
31#include <AGESA.h>
32#include <FieldAccessors.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060033#include <Topology.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020034#include <cpu/x86/lapic.h>
35#include <cpu/amd/msr.h>
36#include <cpu/amd/mtrr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020037#include <arch/acpigen.h>
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020038#include <northbridge/amd/pi/nb_common.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030039#include <northbridge/amd/agesa/agesa_helper.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020040#if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER)
Kyösti Mälkki023ed1f2014-10-22 08:05:36 +030041#include <northbridge/amd/pi/agesawrapper.h>
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +030042#include <northbridge/amd/pi/agesawrapper_call.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020043#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -060044
Kyösti Mälkki113f6702018-05-20 20:12:32 +030045#define MAX_NODE_NUMS MAX_NODES
Bruce Griffith27ed80b2014-08-15 11:46:25 -060046
Bruce Griffith27ed80b2014-08-15 11:46:25 -060047typedef 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;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030054static struct device *__f0_dev[MAX_NODE_NUMS];
55static struct device *__f1_dev[MAX_NODE_NUMS];
56static struct device *__f2_dev[MAX_NODE_NUMS];
57static struct device *__f4_dev[MAX_NODE_NUMS];
Bruce Griffith27ed80b2014-08-15 11:46:25 -060058static unsigned fx_devs = 0;
59
60static dram_base_mask_t get_dram_base_mask(u32 nodeid)
61{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030062 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060063 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]
69 d.mask |= temp<<21;
70 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]
74 d.base |= temp<<21;
75 return d;
76}
77
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030078static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Bruce Griffith27ed80b2014-08-15 11:46:25 -060079 u32 io_min, u32 io_max)
80{
81 u32 i;
82 u32 tempreg;
83 /* io range allocation */
84 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060085 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060086 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUASa8131602016-09-19 10:27:57 -060087 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
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, 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 */
97 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060098 for (i = 0; i < nodes; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060099 pci_write_config32(__f1_dev[i], reg+4, tempreg);
100 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUASa8131602016-09-19 10:27:57 -0600101 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600102 pci_write_config32(__f1_dev[i], reg, tempreg);
103}
104
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300105static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600106{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200107 return pcidev_on_root(DEV_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600108}
109
110static void get_fx_devs(void)
111{
112 int i;
113 for (i = 0; i < MAX_NODE_NUMS; i++) {
114 __f0_dev[i] = get_node_pci(i, 0);
115 __f1_dev[i] = get_node_pci(i, 1);
116 __f2_dev[i] = get_node_pci(i, 2);
117 __f4_dev[i] = get_node_pci(i, 4);
118 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
119 fx_devs = i+1;
120 }
121 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
122 die("Cannot find 0:0x18.[0|1]\n");
123 }
Elyes HAOUASa8131602016-09-19 10:27:57 -0600124 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600125}
126
127static u32 f1_read_config32(unsigned reg)
128{
129 if (fx_devs == 0)
130 get_fx_devs();
131 return pci_read_config32(__f1_dev[0], reg);
132}
133
134static void f1_write_config32(unsigned reg, u32 value)
135{
136 int i;
137 if (fx_devs == 0)
138 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200139 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300140 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600141 dev = __f1_dev[i];
142 if (dev && dev->enabled) {
143 pci_write_config32(dev, reg, value);
144 }
145 }
146}
147
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300148static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600149{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200150 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600151}
152
153static void set_vga_enable_reg(u32 nodeid, u32 linkn)
154{
155 u32 val;
156
157 val = 1 | (nodeid<<4) | (linkn<<12);
158 /* it will routing
159 * (1)mmio 0xa0000:0xbffff
160 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
161 */
162 f1_write_config32(0xf4, val);
163
164}
165
166/**
167 * @return
168 * @retval 2 resoure does not exist, usable
169 * @retval 0 resource exists, not usable
170 * @retval 1 resource exist, resource has been allocated before
171 */
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300172static int reg_useable(unsigned reg, struct device *goal_dev, unsigned goal_nodeid,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600173 unsigned goal_link)
174{
175 struct resource *res;
176 unsigned nodeid, link = 0;
177 int result;
178 res = 0;
179 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300180 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600181 dev = __f0_dev[nodeid];
182 if (!dev)
183 continue;
184 for (link = 0; !res && (link < 8); link++) {
185 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
186 }
187 }
188 result = 2;
189 if (res) {
190 result = 0;
191 if ((goal_link == (link - 1)) &&
192 (goal_nodeid == (nodeid - 1)) &&
193 (res->flags <= 1)) {
194 result = 1;
195 }
196 }
197 return result;
198}
199
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300200static struct resource *amdfam16_find_iopair(struct device *dev, unsigned nodeid, unsigned link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600201{
202 struct resource *resource;
203 u32 free_reg, reg;
204 resource = 0;
205 free_reg = 0;
206 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
207 int result;
208 result = reg_useable(reg, dev, nodeid, link);
209 if (result == 1) {
210 /* I have been allocated this one */
211 break;
212 }
213 else if (result > 1) {
214 /* I have a free register pair */
215 free_reg = reg;
216 }
217 }
218 if (reg > 0xd8) {
219 reg = free_reg; // if no free, the free_reg still be 0
220 }
221
222 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
223
224 return resource;
225}
226
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300227static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600228{
229 struct resource *resource;
230 u32 free_reg, reg;
231 resource = 0;
232 free_reg = 0;
233 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
234 int result;
235 result = reg_useable(reg, dev, nodeid, link);
236 if (result == 1) {
237 /* I have been allocated this one */
238 break;
239 }
240 else if (result > 1) {
241 /* I have a free register pair */
242 free_reg = reg;
243 }
244 }
245 if (reg > 0xb8) {
246 reg = free_reg;
247 }
248
249 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
250 return resource;
251}
252
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300253static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600254{
255 struct resource *resource;
256
257 /* Initialize the io space constraints on the current bus */
258 resource = amdfam16_find_iopair(dev, nodeid, link);
259 if (resource) {
260 u32 align;
261 align = log2(HT_IO_HOST_ALIGN);
262 resource->base = 0;
263 resource->size = 0;
264 resource->align = align;
265 resource->gran = align;
266 resource->limit = 0xffffUL;
267 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
268 }
269
270 /* Initialize the prefetchable memory constraints on the current bus */
271 resource = amdfam16_find_mempair(dev, nodeid, link);
272 if (resource) {
273 resource->base = 0;
274 resource->size = 0;
275 resource->align = log2(HT_MEM_HOST_ALIGN);
276 resource->gran = log2(HT_MEM_HOST_ALIGN);
277 resource->limit = 0xffffffffffULL;
278 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
279 resource->flags |= IORESOURCE_BRIDGE;
280 }
281
282 /* Initialize the memory constraints on the current bus */
283 resource = amdfam16_find_mempair(dev, nodeid, link);
284 if (resource) {
285 resource->base = 0;
286 resource->size = 0;
287 resource->align = log2(HT_MEM_HOST_ALIGN);
288 resource->gran = log2(HT_MEM_HOST_ALIGN);
289 resource->limit = 0xffffffffffULL;
290 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
291 }
292
293}
294
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300295static void read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600296{
297 u32 nodeid;
298 struct bus *link;
299
300 nodeid = amdfam16_nodeid(dev);
301 for (link = dev->link_list; link; link = link->next) {
302 if (link->children) {
303 amdfam16_link_read_bases(dev, nodeid, link->link_num);
304 }
305 }
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300306
307 /*
308 * This MMCONF resource must be reserved in the PCI domain.
309 * It is not honored by the coreboot resource allocator if it is in
310 * the CPU_CLUSTER.
311 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200312 mmconf_resource(dev, MMIO_CONF_BASE);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600313}
314
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300315static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600316{
317 resource_t rbase, rend;
318 unsigned reg, link_num;
319 char buf[50];
320
321 /* Make certain the resource has actually been set */
322 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
323 return;
324 }
325
326 /* If I have already stored this resource don't worry about it */
327 if (resource->flags & IORESOURCE_STORED) {
328 return;
329 }
330
331 /* Only handle PCI memory and IO resources */
332 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
333 return;
334
335 /* Ensure I am actually looking at a resource of function 1 */
336 if ((resource->index & 0xffff) < 0x1000) {
337 return;
338 }
339 /* Get the base address */
340 rbase = resource->base;
341
342 /* Get the limit (rounded up) */
343 rend = resource_end(resource);
344
345 /* Get the register and link */
346 reg = resource->index & 0xfff; // 4k
347 link_num = IOINDEX_LINK(resource->index);
348
349 if (resource->flags & IORESOURCE_IO) {
350 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
351 }
352 else if (resource->flags & IORESOURCE_MEM) {
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200353 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 -0600354 }
355 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200356 snprintf(buf, sizeof(buf), " <node %x link %x>",
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600357 nodeid, link_num);
358 report_resource_stored(dev, resource, buf);
359}
360
361/**
362 * I tried to reuse the resource allocation code in set_resource()
363 * but it is too difficult to deal with the resource allocation magic.
364 */
365
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300366static void create_vga_resource(struct device *dev, unsigned nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600367{
368 struct bus *link;
369
370 /* find out which link the VGA card is connected,
371 * we only deal with the 'first' vga card */
372 for (link = dev->link_list; link; link = link->next) {
373 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Martin Roth77a58b92017-06-24 14:45:48 -0600374#if IS_ENABLED(CONFIG_MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300375 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600376 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
377 link->secondary,link->subordinate);
378 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600379 if ((vga_pri->bus->secondary >= link->secondary) &&
380 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600381#endif
382 break;
383 }
384 }
385
386 /* no VGA card installed */
387 if (link == NULL)
388 return;
389
390 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
391 set_vga_enable_reg(nodeid, sblink);
392}
393
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300394static void set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600395{
396 unsigned nodeid;
397 struct bus *bus;
398 struct resource *res;
399
400 /* Find the nodeid */
401 nodeid = amdfam16_nodeid(dev);
402
403 create_vga_resource(dev, nodeid); //TODO: do we need this?
404
405 /* Set each resource we have found */
406 for (res = dev->resource_list; res; res = res->next) {
407 set_resource(dev, res, nodeid);
408 }
409
410 for (bus = dev->link_list; bus; bus = bus->next) {
411 if (bus->children) {
412 assign_resources(bus);
413 }
414 }
415}
416
417static void northbridge_init(struct device *dev)
418{
419}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200420
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100421static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200422{
423 void *addr, *current;
424
425 /* Skip the HEST header. */
426 current = (void *)(hest + 1);
427
428 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
429 if (addr != NULL)
430 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
431
432 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
433 if (addr != NULL)
434 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
435
436 return (unsigned long)current;
437}
438
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500439static void add_ivhd_dev_entry(struct device *parent, struct device *dev,
440 unsigned long *current, uint16_t *length,
441 uint8_t type, uint8_t data)
442{
443 uint8_t *p;
444 p = (uint8_t *) *current;
445
446 if (type == 0x2) {
447 /* Entry type */
448 p[0] = type;
449 /* Device */
450 p[1] = dev->path.pci.devfn;
451 /* Bus */
452 p[2] = dev->bus->secondary;
453 /* Data */
454 p[3] = data;
455 /* [4:7] Padding */
456 p[4] = 0x0;
457 p[5] = 0x0;
458 p[6] = 0x0;
459 p[7] = 0x0;
460 *length += 8;
461 *current += 8;
462 } else if (type == 0x42) {
463 /* Entry type */
464 p[0] = type;
465 /* Device */
466 p[1] = dev->path.pci.devfn;
467 /* Bus */
468 p[2] = dev->bus->secondary;
469 /* Data */
470 p[3] = 0x0;
471 /* Reserved */
472 p[4] = 0x0;
473 /* Device */
474 p[5] = parent->path.pci.devfn;
475 /* Bus */
476 p[6] = parent->bus->secondary;
477 /* Reserved */
478 p[7] = 0x0;
479 *length += 8;
480 *current += 8;
481 }
482}
483
484static void add_ivrs_device_entries(struct device *parent, struct device *dev,
485 unsigned int depth, int linknum, int8_t *root_level,
486 unsigned long *current, uint16_t *length)
487{
488 struct device *sibling;
489 struct bus *link;
490 unsigned int header_type;
491 unsigned int is_pcie;
492
493 if (!root_level) {
494 root_level = malloc(sizeof(int8_t));
495 *root_level = -1;
496 }
497
498 if (dev->path.type == DEVICE_PATH_PCI) {
499
500 if ((dev->bus->secondary == 0x0) &&
501 (dev->path.pci.devfn == 0x0))
502 *root_level = depth;
503
504 if ((*root_level != -1) && (dev->enabled)) {
505 if (depth == *root_level) {
506 if (dev->path.pci.devfn == (0x14 << 3)) {
507 /* SMBUS controller */
508 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x97);
509 } else if (dev->path.pci.devfn != 0x2 &&
510 dev->path.pci.devfn < (0x2 << 3)) {
511 /* FCH control device */
512 } else {
513 /* Other devices */
514 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
515 }
516 } else {
517 header_type = dev->hdr_type & 0x7f;
518 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
519 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
520 (header_type == PCI_HEADER_TYPE_BRIDGE))
521 && is_pcie) {
522 /* Device or Bridge is PCIe */
523 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
524 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) &&
525 !is_pcie) {
526 add_ivhd_dev_entry(parent, dev, current, length, 0x42, 0x0);
527 /* Device is legacy PCI or PCI-X */
528 }
529 }
530 }
531 }
532
533 for (link = dev->link_list; link; link = link->next)
534 for (sibling = link->children; sibling; sibling =
535 sibling->sibling)
536 add_ivrs_device_entries(dev, sibling, depth + 1, depth,
537 root_level, current, length);
538
539 free(root_level);
540}
541
542unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
543{
544 uint8_t *p;
545
546 uint32_t apicid_sb800;
547 uint32_t apicid_northbridge;
548
549 apicid_sb800 = CONFIG_MAX_CPUS;
550 apicid_northbridge = CONFIG_MAX_CPUS + 1;
551
552 /* Describe NB IOAPIC */
553 p = (uint8_t *)current;
554 p[0] = 0x48; /* Entry type */
555 p[1] = 0; /* Device */
556 p[2] = 0; /* Bus */
557 p[3] = 0x0; /* Data */
558 p[4] = apicid_northbridge; /* IOAPIC ID */
559 p[5] = 0x0; /* Device 0 Function 0 */
560 p[6] = 0x0; /* Northbridge bus */
561 p[7] = 0x1; /* Variety */
562 current += 8;
563
564 /* Describe SB IOAPIC */
565 p = (uint8_t *)current;
566 p[0] = 0x48; /* Entry type */
567 p[1] = 0; /* Device */
568 p[2] = 0; /* Bus */
569 p[3] = 0xd7; /* Data */
570 p[4] = apicid_sb800; /* IOAPIC ID */
571 p[5] = 0x14 << 3; /* Device 0x14 Function 0 */
572 p[6] = 0x0; /* Southbridge bus */
573 p[7] = 0x1; /* Variety */
574 current += 8;
575
576 return current;
577}
578
579static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
580{
581 uint8_t *p;
Piotr Król063e1562018-07-22 20:52:26 +0200582 acpi_ivrs_t *ivrs_agesa;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500583
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300584 struct device *nb_dev = pcidev_on_root(0x0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500585 if (!nb_dev) {
586
587 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
588 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
589
590 return (unsigned long)ivrs;
591 }
592
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500593
Piotr Król063e1562018-07-22 20:52:26 +0200594 /* obtain IOMMU base address */
595 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
596 if (ivrs_agesa != NULL) {
597 ivrs->iv_info = 0x0;
598 /* Maximum supported virtual address size */
599 ivrs->iv_info |= (0x40 << 15);
600 /* Maximum supported physical address size */
601 ivrs->iv_info |= (0x30 << 8);
602 /* Guest virtual address width */
603 ivrs->iv_info |= (0x2 << 5);
604
605 ivrs->ivhd.type = 0x10;
606 ivrs->ivhd.flags = 0x0e;
607 /* Enable ATS support */
608 ivrs->ivhd.flags |= 0x10;
609 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
610 /* BDF <bus>:00.2 */
611 ivrs->ivhd.device_id = 0x2 | (nb_dev->bus->secondary << 8);
612 /* Capability block 0x40 (type 0xf, "Secure device") */
613 ivrs->ivhd.capability_offset = 0x40;
614 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
615 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
616 ivrs->ivhd.pci_segment_group = 0x0;
617 ivrs->ivhd.iommu_info = 0x0;
618 ivrs->ivhd.iommu_info |= (0x13 << 8);
619 /* use only performance counters related bits:
620 * PNCounters[16:13] and
621 * PNBanks[22:17],
622 * otherwise 0 */
623 ivrs->ivhd.iommu_feature_info =
624 ivrs_agesa->ivhd.iommu_feature_info & 0x7fe000;
625 } else {
626 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
627
628 return (unsigned long)ivrs;
629 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500630
631 /* Describe HPET */
632 p = (uint8_t *)current;
633 p[0] = 0x48; /* Entry type */
634 p[1] = 0; /* Device */
635 p[2] = 0; /* Bus */
636 p[3] = 0xd7; /* Data */
637 p[4] = 0x0; /* HPET number */
638 p[5] = 0x14 << 3; /* HPET device */
639 p[6] = nb_dev->bus->secondary; /* HPET bus */
640 p[7] = 0x2; /* Variety */
641 ivrs->ivhd.length += 8;
642 current += 8;
643
644 /* Describe PCI devices */
645 add_ivrs_device_entries(NULL, all_devices, 0, -1, NULL, &current,
646 &ivrs->ivhd.length);
647
648 /* Describe IOAPICs */
649 unsigned long prev_current = current;
650 current = acpi_fill_ivrs_ioapic(ivrs, current);
651 ivrs->ivhd.length += (current - prev_current);
652
653 return current;
654}
655
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300656static void northbridge_fill_ssdt_generator(struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200657{
658 msr_t msr;
659 char pscope[] = "\\_SB.PCI0";
660
661 acpigen_write_scope(pscope);
662 msr = rdmsr(TOP_MEM);
663 acpigen_write_name_dword("TOM1", msr.lo);
664 msr = rdmsr(TOP_MEM2);
665 /*
666 * Since XP only implements parts of ACPI 2.0, we can't use a qword
667 * here.
668 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
669 * slide 22ff.
670 * Shift value right by 20 bit to make it fit into 32bit,
671 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
672 */
673 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
674 acpigen_pop_len();
675}
676
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300677static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200678 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200679 acpi_rsdp_t *rsdp)
680{
681 acpi_srat_t *srat;
682 acpi_slit_t *slit;
683 acpi_header_t *ssdt;
684 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500685 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200686 acpi_hest_t *hest;
687
688 /* HEST */
689 current = ALIGN(current, 8);
690 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100691 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200692 acpi_add_table(rsdp, (void *)current);
693 current += ((acpi_header_t *)current)->length;
694
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500695 /* IVRS */
696 current = ALIGN(current, 8);
697 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
698 ivrs = (acpi_ivrs_t *) current;
699 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
700 current += ivrs->header.length;
701 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200702
703 /* SRAT */
704 current = ALIGN(current, 8);
705 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
706 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
707 if (srat != NULL) {
708 memcpy((void *)current, srat, srat->header.length);
709 srat = (acpi_srat_t *) current;
710 current += srat->header.length;
711 acpi_add_table(rsdp, srat);
712 } else {
713 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
714 }
715
716 /* SLIT */
717 current = ALIGN(current, 8);
718 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
719 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
720 if (slit != NULL) {
721 memcpy((void *)current, slit, slit->header.length);
722 slit = (acpi_slit_t *) current;
723 current += slit->header.length;
724 acpi_add_table(rsdp, slit);
725 } else {
726 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
727 }
728
729 /* ALIB */
730 current = ALIGN(current, 16);
731 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
732 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
733 if (alib != NULL) {
734 memcpy((void *)current, alib, alib->length);
735 alib = (acpi_header_t *) current;
736 current += alib->length;
737 acpi_add_table(rsdp, (void *)alib);
738 }
739 else {
740 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
741 }
742
743 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
744 /* SSDT */
745 current = ALIGN(current, 16);
746 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
747 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
748 if (ssdt != NULL) {
749 memcpy((void *)current, ssdt, ssdt->length);
750 ssdt = (acpi_header_t *) current;
751 current += ssdt->length;
752 }
753 else {
754 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
755 }
756 acpi_add_table(rsdp,ssdt);
757
758 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
759 return current;
760}
761
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600762static struct device_operations northbridge_operations = {
763 .read_resources = read_resources,
764 .set_resources = set_resources,
765 .enable_resources = pci_dev_enable_resources,
766 .init = northbridge_init,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200767 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
768 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600769 .enable = 0,
770 .ops_pci = 0,
771};
772
773static const struct pci_driver family16_northbridge __pci_driver = {
774 .ops = &northbridge_operations,
775 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600776 .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600777};
778
779static const struct pci_driver family10_northbridge __pci_driver = {
780 .ops = &northbridge_operations,
781 .vendor = PCI_VENDOR_ID_AMD,
782 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
783};
784
Dave Frodin891f71a2015-01-19 15:58:24 -0700785static void fam16_finalize(void *chip_info)
786{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300787 struct device *dev;
Dave Frodin891f71a2015-01-19 15:58:24 -0700788 u32 value;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300789 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Dave Frodin891f71a2015-01-19 15:58:24 -0700790 pci_write_config32(dev, 0xF8, 0);
791 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
792
793 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300794 dev = pcidev_on_root(1, 1);
Dave Frodin891f71a2015-01-19 15:58:24 -0700795 value = pci_read_config32(dev, 0x60);
796 value &= ~(1 << 11);
797 pci_write_config32(dev, 0x60, value);
798}
799
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300800struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600801 CHIP_NAME("AMD FAM16 Northbridge")
802 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700803 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600804};
805
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300806static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600807{
808 unsigned reg;
809
810 /* Find the already assigned resource pairs */
811 get_fx_devs();
812 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
813 u32 base, limit;
814 base = f1_read_config32(reg);
815 limit = f1_read_config32(reg + 0x04);
816 /* Is this register allocated? */
817 if ((base & 3) != 0) {
818 unsigned nodeid, reg_link;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300819 struct device *reg_dev;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600820 if (reg < 0xc0) { // mmio
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600821 nodeid = (limit & 0xf) + (base&0x30);
822 } else { // io
823 nodeid = (limit & 0xf) + ((base>>4)&0x30);
824 }
825 reg_link = (limit >> 4) & 7;
826 reg_dev = __f0_dev[nodeid];
827 if (reg_dev) {
828 /* Reserve the resource */
829 struct resource *res;
830 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
831 if (res) {
832 res->flags = 1;
833 }
834 }
835 }
836 }
837 /* FIXME: do we need to check extend conf space?
838 I don't believe that much preset value */
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600839 pci_domain_read_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600840}
841
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300842static void domain_enable_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600843{
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200844#if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600845 /* Must be called after PCI enumeration and resource allocation */
846 if (!acpi_is_wakeup_s3())
847 AGESAWRAPPER(amdinitmid);
848
849 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200850#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600851}
852
853#if CONFIG_HW_MEM_HOLE_SIZEK != 0
854struct hw_mem_hole_info {
855 unsigned hole_startk;
856 int node_id;
857};
858static struct hw_mem_hole_info get_hw_mem_hole_info(void)
859{
860 struct hw_mem_hole_info mem_hole;
861 int i;
862 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
863 mem_hole.node_id = -1;
864 for (i = 0; i < node_nums; i++) {
865 dram_base_mask_t d;
866 u32 hole;
867 d = get_dram_base_mask(i);
868 if (!(d.mask & 1)) continue; // no memory on this node
869 hole = pci_read_config32(__f1_dev[i], 0xf0);
870 if (hole & 2) { // we find the hole
871 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
872 mem_hole.node_id = i; // record the node No with hole
873 break; // only one hole
874 }
875 }
876
877 /* We need to double check if there is special set on base reg and limit reg
878 * are not continuous instead of hole, it will find out its hole_startk.
879 */
880 if (mem_hole.node_id == -1) {
881 resource_t limitk_pri = 0;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600882 for (i = 0; i < node_nums; i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600883 dram_base_mask_t d;
884 resource_t base_k, limit_k;
885 d = get_dram_base_mask(i);
886 if (!(d.base & 1)) continue;
887 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
888 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
889 if (limitk_pri != base_k) { // we find the hole
890 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
891 mem_hole.node_id = i;
892 break; //only one hole
893 }
894 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
895 limitk_pri = limit_k;
896 }
897 }
898 return mem_hole;
899}
900#endif
901
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300902static void domain_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600903{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600904 unsigned long mmio_basek;
905 u32 pci_tolm;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600906 int i, idx;
907 struct bus *link;
908#if CONFIG_HW_MEM_HOLE_SIZEK != 0
909 struct hw_mem_hole_info mem_hole;
910 u32 reset_memhole = 1;
911#endif
912
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600913 pci_tolm = 0xffffffffUL;
914 for (link = dev->link_list; link; link = link->next) {
915 pci_tolm = find_pci_tolm(link);
916 }
917
918 // FIXME handle interleaved nodes. If you fix this here, please fix
919 // amdk8, too.
920 mmio_basek = pci_tolm >> 10;
921 /* Round mmio_basek to something the processor can support */
922 mmio_basek &= ~((1 << 6) -1);
923
924 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
925 // MMIO hole. If you fix this here, please fix amdk8, too.
926 /* Round the mmio hole to 64M */
927 mmio_basek &= ~((64*1024) - 1);
928
929#if CONFIG_HW_MEM_HOLE_SIZEK != 0
930 /* if the hw mem hole is already set in raminit stage, here we will compare
931 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
932 * use hole_basek as mmio_basek and we don't need to reset hole.
933 * otherwise We reset the hole to the mmio_basek
934 */
935
936 mem_hole = get_hw_mem_hole_info();
937
938 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
939 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
940 mmio_basek = mem_hole.hole_startk;
941 reset_memhole = 0;
942 }
943#endif
944
945 idx = 0x10;
946 for (i = 0; i < node_nums; i++) {
947 dram_base_mask_t d;
948 resource_t basek, limitk, sizek; // 4 1T
949
950 d = get_dram_base_mask(i);
951
952 if (!(d.mask & 1)) continue;
953 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200954 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600955
956 sizek = limitk - basek;
957
958 /* see if we need a hole from 0xa0000 to 0xbffff */
959 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
960 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
961 idx += 0x10;
962 basek = (8*64)+(16*16);
963 sizek = limitk - ((8*64)+(16*16));
964
965 }
966
967 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
968
969 /* split the region to accommodate pci memory space */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600970 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600971 if (basek <= mmio_basek) {
972 unsigned pre_sizek;
973 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600974 if (pre_sizek > 0) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600975 ram_resource(dev, (idx | i), basek, pre_sizek);
976 idx += 0x10;
977 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600978 }
979 basek = mmio_basek;
980 }
981 if ((basek + sizek) <= 4*1024*1024) {
982 sizek = 0;
983 }
984 else {
985 uint64_t topmem2 = bsp_topmem2();
986 basek = 4*1024*1024;
987 sizek = topmem2/1024 - basek;
988 }
989 }
990
991 ram_resource(dev, (idx | i), basek, sizek);
992 idx += 0x10;
993 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
994 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600995 }
996
Kyösti Mälkkie87564f2017-04-15 20:07:53 +0300997 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600998
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200999 for (link = dev->link_list; link; link = link->next) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001000 if (link->children) {
1001 assign_resources(link);
1002 }
1003 }
1004}
1005
Aaron Durbinaa090cb2017-09-13 16:01:52 -06001006static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001007{
1008 if (dev->path.type == DEVICE_PATH_DOMAIN)
1009 return "PCI0";
1010
1011 return NULL;
1012}
1013
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001014static struct device_operations pci_domain_ops = {
1015 .read_resources = domain_read_resources,
1016 .set_resources = domain_set_resources,
1017 .enable_resources = domain_enable_resources,
1018 .init = NULL,
1019 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001020 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001021};
1022
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001023static void sysconf_init(struct device *dev) // first node
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001024{
1025 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1026 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1027}
1028
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001029static void add_more_links(struct device *dev, unsigned total_links)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001030{
1031 struct bus *link, *last = NULL;
1032 int link_num;
1033
1034 for (link = dev->link_list; link; link = link->next)
1035 last = link;
1036
1037 if (last) {
1038 int links = total_links - last->link_num;
1039 link_num = last->link_num;
1040 if (links > 0) {
1041 link = malloc(links*sizeof(*link));
1042 if (!link)
1043 die("Couldn't allocate more links!\n");
1044 memset(link, 0, links*sizeof(*link));
1045 last->next = link;
1046 }
1047 }
1048 else {
1049 link_num = -1;
1050 link = malloc(total_links*sizeof(*link));
1051 memset(link, 0, total_links*sizeof(*link));
1052 dev->link_list = link;
1053 }
1054
1055 for (link_num = link_num + 1; link_num < total_links; link_num++) {
1056 link->link_num = link_num;
1057 link->dev = dev;
1058 link->next = link + 1;
1059 last = link;
1060 link = link->next;
1061 }
1062 last->next = NULL;
1063}
1064
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001065static void cpu_bus_scan(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001066{
1067 struct bus *cpu_bus;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001068 struct device *dev_mc;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001069 int i,j;
1070 int coreid_bits;
1071 int core_max = 0;
1072 unsigned ApicIdCoreIdSize;
1073 unsigned core_nums;
1074 int siblings = 0;
1075 unsigned int family;
1076 u32 modules = 0;
1077 VOID* modules_ptr = &modules;
1078 BUILD_OPT_CFG* options = NULL;
1079 int ioapic_count = 0;
1080
1081 // TODO Remove the printk's.
1082 printk(BIOS_SPEW, "MullinsPI Debug: Grabbing the AMD Topology Information.\n");
1083 AmdGetValue(AMD_GLOBAL_USER_OPTIONS, (VOID**)&options, sizeof(options));
1084 AmdGetValue(AMD_GLOBAL_NUM_MODULES, &modules_ptr, sizeof(modules));
Alexandru Gagniuc2e0cf142014-12-28 20:38:32 -06001085 modules = *(u32*)modules_ptr;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001086 ASSERT(modules > 0);
1087 ASSERT(options);
1088 ioapic_count = (int)options->CfgPlatNumIoApics;
1089 ASSERT(ioapic_count > 0);
1090 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of Modules (@0x%p) is %d\n", modules_ptr, modules);
1091 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of IOAPICs (@0x%p) is %d\n", options, (int)options->CfgPlatNumIoApics);
1092
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001093 dev_mc = pcidev_on_root(DEV_CDB, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001094 if (!dev_mc) {
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001095 printk(BIOS_ERR, "0:%02x.0 not found", DEV_CDB);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001096 die("");
1097 }
1098 sysconf_init(dev_mc);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001099
1100 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +03001101 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001102 core_max = 1 << (coreid_bits & 0x000F); //mnc
1103
1104 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1105 if (ApicIdCoreIdSize) {
1106 core_nums = (1 << ApicIdCoreIdSize) - 1;
1107 } else {
1108 core_nums = 3; //quad core
1109 }
1110
1111 /* Find which cpus are present */
1112 cpu_bus = dev->link_list;
1113 for (i = 0; i < node_nums; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001114 struct device *cdb_dev;
Kyösti Mälkkiedf51d22018-05-20 22:38:00 +03001115 unsigned devn;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001116 struct bus *pbus;
1117
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001118 devn = DEV_CDB + i;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001119 pbus = dev_mc->bus;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001120
1121 /* Find the cpu's pci device */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001122 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001123 if (!cdb_dev) {
1124 /* If I am probing things in a weird order
1125 * ensure all of the cpu's pci devices are found.
1126 */
1127 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001128 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001129 cdb_dev = pci_probe_dev(NULL, pbus,
1130 PCI_DEVFN(devn, fn));
1131 }
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001132 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001133 } else {
1134 /* Ok, We need to set the links for that device.
1135 * otherwise the device under it will not be scanned
1136 */
Kyösti Mälkkic5163ed82015-02-04 13:25:37 +02001137
1138 add_more_links(cdb_dev, 4);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001139 }
1140
1141 family = cpuid_eax(1);
1142 family = (family >> 20) & 0xFF;
1143 if (family == 1) { //f10
1144 u32 dword;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001145 cdb_dev = pcidev_on_root(devn, 3);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001146 dword = pci_read_config32(cdb_dev, 0xe8);
1147 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1148 } else if (family == 7) {//f16
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001149 cdb_dev = pcidev_on_root(devn, 5);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001150 if (cdb_dev && cdb_dev->enabled) {
1151 siblings = pci_read_config32(cdb_dev, 0x84);
1152 siblings &= 0xFF;
1153 }
1154 } else {
1155 siblings = 0; //default one core
1156 }
1157 int enable_node = cdb_dev && cdb_dev->enabled;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001158 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 -06001159 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1160
Elyes HAOUASa8131602016-09-19 10:27:57 -06001161 for (j = 0; j <= siblings; j++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001162 u32 lapicid_start = 0;
1163
1164 /*
1165 * APIC ID calucation is tightly coupled with AGESA v5 code.
1166 * This calculation MUST match the assignment calculation done
1167 * in LocalApicInitializationAtEarly() function.
1168 * And reference GetLocalApicIdForCore()
1169 *
1170 * Apply apic enumeration rules
1171 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1172 * put the local-APICs at m..z
1173 *
1174 * This is needed because many IO-APIC devices only have 4 bits
1175 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001176 */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001177 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1178 lapicid_start = (ioapic_count - 1) / core_max;
1179 lapicid_start = (lapicid_start + 1) * core_max;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001180 printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001181 }
1182 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
Elyes HAOUASa8131602016-09-19 10:27:57 -06001183 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001184 i, j, apic_id);
1185
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001186 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001187 if (cpu)
1188 amd_cpu_topology(cpu, i, j);
1189 } //j
1190 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001191}
1192
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001193static void cpu_bus_init(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001194{
1195 initialize_cpus(dev->link_list);
1196}
1197
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001198static struct device_operations cpu_bus_ops = {
Kyösti Mälkki48f82a92016-12-02 16:02:30 +02001199 .read_resources = DEVICE_NOOP,
1200 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001201 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001202 .init = cpu_bus_init,
1203 .scan_bus = cpu_bus_scan,
1204};
1205
1206static void root_complex_enable_dev(struct device *dev)
1207{
1208 static int done = 0;
1209
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001210 if (!done) {
1211 setup_bsp_ramtop();
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001212 done = 1;
1213 }
1214
1215 /* Set the operations if it is a special bus type */
1216 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1217 dev->ops = &pci_domain_ops;
1218 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1219 dev->ops = &cpu_bus_ops;
1220 }
1221}
1222
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001223struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001224 CHIP_NAME("AMD FAM16 Root Complex")
1225 .enable_dev = root_complex_enable_dev,
1226};
1227
1228/*********************************************************************
1229 * Change the vendor / device IDs to match the generic VBIOS header. *
1230 *********************************************************************/
1231u32 map_oprom_vendev(u32 vendev)
1232{
1233 u32 new_vendev;
1234 new_vendev =
1235 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1236
1237 if (vendev != new_vendev)
1238 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1239
1240 return new_vendev;
1241}