blob: ebc1ca403724d3579b601dbcd814e89b52f9b4f8 [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älkkied8d2772017-07-15 17:12:44 +030038#include <northbridge/amd/agesa/agesa_helper.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020039#if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER)
Kyösti Mälkki023ed1f2014-10-22 08:05:36 +030040#include <northbridge/amd/pi/agesawrapper.h>
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +030041#include <northbridge/amd/pi/agesawrapper_call.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020042#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -060043
Kyösti Mälkki113f6702018-05-20 20:12:32 +030044#define MAX_NODE_NUMS MAX_NODES
Bruce Griffith27ed80b2014-08-15 11:46:25 -060045
Bruce Griffith27ed80b2014-08-15 11:46:25 -060046typedef struct dram_base_mask {
47 u32 base; //[47:27] at [28:8]
48 u32 mask; //[47:27] at [28:8] and enable at bit 0
49} dram_base_mask_t;
50
51static unsigned node_nums;
52static unsigned sblink;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030053static struct device *__f0_dev[MAX_NODE_NUMS];
54static struct device *__f1_dev[MAX_NODE_NUMS];
55static struct device *__f2_dev[MAX_NODE_NUMS];
56static struct device *__f4_dev[MAX_NODE_NUMS];
Bruce Griffith27ed80b2014-08-15 11:46:25 -060057static unsigned fx_devs = 0;
58
59static dram_base_mask_t get_dram_base_mask(u32 nodeid)
60{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030061 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060062 dram_base_mask_t d;
63 dev = __f1_dev[0];
64 u32 temp;
65 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
66 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
67 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
68 d.mask |= temp<<21;
69 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
70 d.mask |= (temp & 1); // enable bit
71 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
72 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
73 d.base |= temp<<21;
74 return d;
75}
76
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030077static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Bruce Griffith27ed80b2014-08-15 11:46:25 -060078 u32 io_min, u32 io_max)
79{
80 u32 i;
81 u32 tempreg;
82 /* io range allocation */
83 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060084 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060085 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUASa8131602016-09-19 10:27:57 -060086 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUASa8131602016-09-19 10:27:57 -060087 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060088 pci_write_config32(__f1_dev[i], reg, tempreg);
89}
90
91static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
92{
93 u32 i;
94 u32 tempreg;
95 /* io range allocation */
96 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060097 for (i = 0; i < nodes; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060098 pci_write_config32(__f1_dev[i], reg+4, tempreg);
99 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUASa8131602016-09-19 10:27:57 -0600100 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600101 pci_write_config32(__f1_dev[i], reg, tempreg);
102}
103
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300104static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600105{
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300106 return pcidev_on_root(CONFIG_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600107}
108
109static void get_fx_devs(void)
110{
111 int i;
112 for (i = 0; i < MAX_NODE_NUMS; i++) {
113 __f0_dev[i] = get_node_pci(i, 0);
114 __f1_dev[i] = get_node_pci(i, 1);
115 __f2_dev[i] = get_node_pci(i, 2);
116 __f4_dev[i] = get_node_pci(i, 4);
117 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
118 fx_devs = i+1;
119 }
120 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
121 die("Cannot find 0:0x18.[0|1]\n");
122 }
Elyes HAOUASa8131602016-09-19 10:27:57 -0600123 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600124}
125
126static u32 f1_read_config32(unsigned reg)
127{
128 if (fx_devs == 0)
129 get_fx_devs();
130 return pci_read_config32(__f1_dev[0], reg);
131}
132
133static void f1_write_config32(unsigned reg, u32 value)
134{
135 int i;
136 if (fx_devs == 0)
137 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200138 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300139 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600140 dev = __f1_dev[i];
141 if (dev && dev->enabled) {
142 pci_write_config32(dev, reg, value);
143 }
144 }
145}
146
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300147static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600148{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600149 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600150}
151
152static void set_vga_enable_reg(u32 nodeid, u32 linkn)
153{
154 u32 val;
155
156 val = 1 | (nodeid<<4) | (linkn<<12);
157 /* it will routing
158 * (1)mmio 0xa0000:0xbffff
159 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
160 */
161 f1_write_config32(0xf4, val);
162
163}
164
165/**
166 * @return
167 * @retval 2 resoure does not exist, usable
168 * @retval 0 resource exists, not usable
169 * @retval 1 resource exist, resource has been allocated before
170 */
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300171static int reg_useable(unsigned reg, struct device *goal_dev, unsigned goal_nodeid,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600172 unsigned goal_link)
173{
174 struct resource *res;
175 unsigned nodeid, link = 0;
176 int result;
177 res = 0;
178 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300179 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600180 dev = __f0_dev[nodeid];
181 if (!dev)
182 continue;
183 for (link = 0; !res && (link < 8); link++) {
184 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
185 }
186 }
187 result = 2;
188 if (res) {
189 result = 0;
190 if ((goal_link == (link - 1)) &&
191 (goal_nodeid == (nodeid - 1)) &&
192 (res->flags <= 1)) {
193 result = 1;
194 }
195 }
196 return result;
197}
198
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300199static struct resource *amdfam16_find_iopair(struct device *dev, unsigned nodeid, unsigned link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600200{
201 struct resource *resource;
202 u32 free_reg, reg;
203 resource = 0;
204 free_reg = 0;
205 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
206 int result;
207 result = reg_useable(reg, dev, nodeid, link);
208 if (result == 1) {
209 /* I have been allocated this one */
210 break;
211 }
212 else if (result > 1) {
213 /* I have a free register pair */
214 free_reg = reg;
215 }
216 }
217 if (reg > 0xd8) {
218 reg = free_reg; // if no free, the free_reg still be 0
219 }
220
221 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
222
223 return resource;
224}
225
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300226static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600227{
228 struct resource *resource;
229 u32 free_reg, reg;
230 resource = 0;
231 free_reg = 0;
232 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
233 int result;
234 result = reg_useable(reg, dev, nodeid, link);
235 if (result == 1) {
236 /* I have been allocated this one */
237 break;
238 }
239 else if (result > 1) {
240 /* I have a free register pair */
241 free_reg = reg;
242 }
243 }
244 if (reg > 0xb8) {
245 reg = free_reg;
246 }
247
248 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
249 return resource;
250}
251
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300252static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600253{
254 struct resource *resource;
255
256 /* Initialize the io space constraints on the current bus */
257 resource = amdfam16_find_iopair(dev, nodeid, link);
258 if (resource) {
259 u32 align;
260 align = log2(HT_IO_HOST_ALIGN);
261 resource->base = 0;
262 resource->size = 0;
263 resource->align = align;
264 resource->gran = align;
265 resource->limit = 0xffffUL;
266 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
267 }
268
269 /* Initialize the prefetchable memory constraints on the current bus */
270 resource = amdfam16_find_mempair(dev, nodeid, link);
271 if (resource) {
272 resource->base = 0;
273 resource->size = 0;
274 resource->align = log2(HT_MEM_HOST_ALIGN);
275 resource->gran = log2(HT_MEM_HOST_ALIGN);
276 resource->limit = 0xffffffffffULL;
277 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
278 resource->flags |= IORESOURCE_BRIDGE;
279 }
280
281 /* Initialize the memory constraints on the current bus */
282 resource = amdfam16_find_mempair(dev, nodeid, link);
283 if (resource) {
284 resource->base = 0;
285 resource->size = 0;
286 resource->align = log2(HT_MEM_HOST_ALIGN);
287 resource->gran = log2(HT_MEM_HOST_ALIGN);
288 resource->limit = 0xffffffffffULL;
289 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
290 }
291
292}
293
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300294static void read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600295{
296 u32 nodeid;
297 struct bus *link;
298
299 nodeid = amdfam16_nodeid(dev);
300 for (link = dev->link_list; link; link = link->next) {
301 if (link->children) {
302 amdfam16_link_read_bases(dev, nodeid, link->link_num);
303 }
304 }
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300305
306 /*
307 * This MMCONF resource must be reserved in the PCI domain.
308 * It is not honored by the coreboot resource allocator if it is in
309 * the CPU_CLUSTER.
310 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200311 mmconf_resource(dev, MMIO_CONF_BASE);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600312}
313
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300314static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600315{
316 resource_t rbase, rend;
317 unsigned reg, link_num;
318 char buf[50];
319
320 /* Make certain the resource has actually been set */
321 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
322 return;
323 }
324
325 /* If I have already stored this resource don't worry about it */
326 if (resource->flags & IORESOURCE_STORED) {
327 return;
328 }
329
330 /* Only handle PCI memory and IO resources */
331 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
332 return;
333
334 /* Ensure I am actually looking at a resource of function 1 */
335 if ((resource->index & 0xffff) < 0x1000) {
336 return;
337 }
338 /* Get the base address */
339 rbase = resource->base;
340
341 /* Get the limit (rounded up) */
342 rend = resource_end(resource);
343
344 /* Get the register and link */
345 reg = resource->index & 0xfff; // 4k
346 link_num = IOINDEX_LINK(resource->index);
347
348 if (resource->flags & IORESOURCE_IO) {
349 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
350 }
351 else if (resource->flags & IORESOURCE_MEM) {
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200352 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 -0600353 }
354 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200355 snprintf(buf, sizeof(buf), " <node %x link %x>",
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600356 nodeid, link_num);
357 report_resource_stored(dev, resource, buf);
358}
359
360/**
361 * I tried to reuse the resource allocation code in set_resource()
362 * but it is too difficult to deal with the resource allocation magic.
363 */
364
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300365static void create_vga_resource(struct device *dev, unsigned nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600366{
367 struct bus *link;
368
369 /* find out which link the VGA card is connected,
370 * we only deal with the 'first' vga card */
371 for (link = dev->link_list; link; link = link->next) {
372 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Martin Roth77a58b92017-06-24 14:45:48 -0600373#if IS_ENABLED(CONFIG_MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300374 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600375 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
376 link->secondary,link->subordinate);
377 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600378 if ((vga_pri->bus->secondary >= link->secondary) &&
379 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600380#endif
381 break;
382 }
383 }
384
385 /* no VGA card installed */
386 if (link == NULL)
387 return;
388
389 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
390 set_vga_enable_reg(nodeid, sblink);
391}
392
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300393static void set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600394{
395 unsigned nodeid;
396 struct bus *bus;
397 struct resource *res;
398
399 /* Find the nodeid */
400 nodeid = amdfam16_nodeid(dev);
401
402 create_vga_resource(dev, nodeid); //TODO: do we need this?
403
404 /* Set each resource we have found */
405 for (res = dev->resource_list; res; res = res->next) {
406 set_resource(dev, res, nodeid);
407 }
408
409 for (bus = dev->link_list; bus; bus = bus->next) {
410 if (bus->children) {
411 assign_resources(bus);
412 }
413 }
414}
415
416static void northbridge_init(struct device *dev)
417{
418}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200419
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100420static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200421{
422 void *addr, *current;
423
424 /* Skip the HEST header. */
425 current = (void *)(hest + 1);
426
427 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
428 if (addr != NULL)
429 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
430
431 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
432 if (addr != NULL)
433 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
434
435 return (unsigned long)current;
436}
437
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500438static void add_ivhd_dev_entry(struct device *parent, struct device *dev,
439 unsigned long *current, uint16_t *length,
440 uint8_t type, uint8_t data)
441{
442 uint8_t *p;
443 p = (uint8_t *) *current;
444
445 if (type == 0x2) {
446 /* Entry type */
447 p[0] = type;
448 /* Device */
449 p[1] = dev->path.pci.devfn;
450 /* Bus */
451 p[2] = dev->bus->secondary;
452 /* Data */
453 p[3] = data;
454 /* [4:7] Padding */
455 p[4] = 0x0;
456 p[5] = 0x0;
457 p[6] = 0x0;
458 p[7] = 0x0;
459 *length += 8;
460 *current += 8;
461 } else if (type == 0x42) {
462 /* Entry type */
463 p[0] = type;
464 /* Device */
465 p[1] = dev->path.pci.devfn;
466 /* Bus */
467 p[2] = dev->bus->secondary;
468 /* Data */
469 p[3] = 0x0;
470 /* Reserved */
471 p[4] = 0x0;
472 /* Device */
473 p[5] = parent->path.pci.devfn;
474 /* Bus */
475 p[6] = parent->bus->secondary;
476 /* Reserved */
477 p[7] = 0x0;
478 *length += 8;
479 *current += 8;
480 }
481}
482
483static void add_ivrs_device_entries(struct device *parent, struct device *dev,
484 unsigned int depth, int linknum, int8_t *root_level,
485 unsigned long *current, uint16_t *length)
486{
487 struct device *sibling;
488 struct bus *link;
489 unsigned int header_type;
490 unsigned int is_pcie;
491
492 if (!root_level) {
493 root_level = malloc(sizeof(int8_t));
494 *root_level = -1;
495 }
496
497 if (dev->path.type == DEVICE_PATH_PCI) {
498
499 if ((dev->bus->secondary == 0x0) &&
500 (dev->path.pci.devfn == 0x0))
501 *root_level = depth;
502
503 if ((*root_level != -1) && (dev->enabled)) {
504 if (depth == *root_level) {
505 if (dev->path.pci.devfn == (0x14 << 3)) {
506 /* SMBUS controller */
507 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x97);
508 } else if (dev->path.pci.devfn != 0x2 &&
509 dev->path.pci.devfn < (0x2 << 3)) {
510 /* FCH control device */
511 } else {
512 /* Other devices */
513 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
514 }
515 } else {
516 header_type = dev->hdr_type & 0x7f;
517 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
518 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
519 (header_type == PCI_HEADER_TYPE_BRIDGE))
520 && is_pcie) {
521 /* Device or Bridge is PCIe */
522 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
523 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) &&
524 !is_pcie) {
525 add_ivhd_dev_entry(parent, dev, current, length, 0x42, 0x0);
526 /* Device is legacy PCI or PCI-X */
527 }
528 }
529 }
530 }
531
532 for (link = dev->link_list; link; link = link->next)
533 for (sibling = link->children; sibling; sibling =
534 sibling->sibling)
535 add_ivrs_device_entries(dev, sibling, depth + 1, depth,
536 root_level, current, length);
537
538 free(root_level);
539}
540
541unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
542{
543 uint8_t *p;
544
545 uint32_t apicid_sb800;
546 uint32_t apicid_northbridge;
547
548 apicid_sb800 = CONFIG_MAX_CPUS;
549 apicid_northbridge = CONFIG_MAX_CPUS + 1;
550
551 /* Describe NB IOAPIC */
552 p = (uint8_t *)current;
553 p[0] = 0x48; /* Entry type */
554 p[1] = 0; /* Device */
555 p[2] = 0; /* Bus */
556 p[3] = 0x0; /* Data */
557 p[4] = apicid_northbridge; /* IOAPIC ID */
558 p[5] = 0x0; /* Device 0 Function 0 */
559 p[6] = 0x0; /* Northbridge bus */
560 p[7] = 0x1; /* Variety */
561 current += 8;
562
563 /* Describe SB IOAPIC */
564 p = (uint8_t *)current;
565 p[0] = 0x48; /* Entry type */
566 p[1] = 0; /* Device */
567 p[2] = 0; /* Bus */
568 p[3] = 0xd7; /* Data */
569 p[4] = apicid_sb800; /* IOAPIC ID */
570 p[5] = 0x14 << 3; /* Device 0x14 Function 0 */
571 p[6] = 0x0; /* Southbridge bus */
572 p[7] = 0x1; /* Variety */
573 current += 8;
574
575 return current;
576}
577
578static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
579{
580 uint8_t *p;
Piotr Król063e1562018-07-22 20:52:26 +0200581 acpi_ivrs_t *ivrs_agesa;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500582
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300583 struct device *nb_dev = pcidev_on_root(0x0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500584 if (!nb_dev) {
585
586 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
587 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
588
589 return (unsigned long)ivrs;
590 }
591
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500592
Piotr Król063e1562018-07-22 20:52:26 +0200593 /* obtain IOMMU base address */
594 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
595 if (ivrs_agesa != NULL) {
596 ivrs->iv_info = 0x0;
597 /* Maximum supported virtual address size */
598 ivrs->iv_info |= (0x40 << 15);
599 /* Maximum supported physical address size */
600 ivrs->iv_info |= (0x30 << 8);
601 /* Guest virtual address width */
602 ivrs->iv_info |= (0x2 << 5);
603
604 ivrs->ivhd.type = 0x10;
605 ivrs->ivhd.flags = 0x0e;
606 /* Enable ATS support */
607 ivrs->ivhd.flags |= 0x10;
608 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
609 /* BDF <bus>:00.2 */
610 ivrs->ivhd.device_id = 0x2 | (nb_dev->bus->secondary << 8);
611 /* Capability block 0x40 (type 0xf, "Secure device") */
612 ivrs->ivhd.capability_offset = 0x40;
613 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
614 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
615 ivrs->ivhd.pci_segment_group = 0x0;
616 ivrs->ivhd.iommu_info = 0x0;
617 ivrs->ivhd.iommu_info |= (0x13 << 8);
618 /* use only performance counters related bits:
619 * PNCounters[16:13] and
620 * PNBanks[22:17],
621 * otherwise 0 */
622 ivrs->ivhd.iommu_feature_info =
623 ivrs_agesa->ivhd.iommu_feature_info & 0x7fe000;
624 } else {
625 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
626
627 return (unsigned long)ivrs;
628 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500629
630 /* Describe HPET */
631 p = (uint8_t *)current;
632 p[0] = 0x48; /* Entry type */
633 p[1] = 0; /* Device */
634 p[2] = 0; /* Bus */
635 p[3] = 0xd7; /* Data */
636 p[4] = 0x0; /* HPET number */
637 p[5] = 0x14 << 3; /* HPET device */
638 p[6] = nb_dev->bus->secondary; /* HPET bus */
639 p[7] = 0x2; /* Variety */
640 ivrs->ivhd.length += 8;
641 current += 8;
642
643 /* Describe PCI devices */
644 add_ivrs_device_entries(NULL, all_devices, 0, -1, NULL, &current,
645 &ivrs->ivhd.length);
646
647 /* Describe IOAPICs */
648 unsigned long prev_current = current;
649 current = acpi_fill_ivrs_ioapic(ivrs, current);
650 ivrs->ivhd.length += (current - prev_current);
651
652 return current;
653}
654
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300655static void northbridge_fill_ssdt_generator(struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200656{
657 msr_t msr;
658 char pscope[] = "\\_SB.PCI0";
659
660 acpigen_write_scope(pscope);
661 msr = rdmsr(TOP_MEM);
662 acpigen_write_name_dword("TOM1", msr.lo);
663 msr = rdmsr(TOP_MEM2);
664 /*
665 * Since XP only implements parts of ACPI 2.0, we can't use a qword
666 * here.
667 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
668 * slide 22ff.
669 * Shift value right by 20 bit to make it fit into 32bit,
670 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
671 */
672 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
673 acpigen_pop_len();
674}
675
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300676static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200677 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200678 acpi_rsdp_t *rsdp)
679{
680 acpi_srat_t *srat;
681 acpi_slit_t *slit;
682 acpi_header_t *ssdt;
683 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500684 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200685 acpi_hest_t *hest;
686
687 /* HEST */
688 current = ALIGN(current, 8);
689 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100690 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200691 acpi_add_table(rsdp, (void *)current);
692 current += ((acpi_header_t *)current)->length;
693
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500694 /* IVRS */
695 current = ALIGN(current, 8);
696 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
697 ivrs = (acpi_ivrs_t *) current;
698 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
699 current += ivrs->header.length;
700 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200701
702 /* SRAT */
703 current = ALIGN(current, 8);
704 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
705 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
706 if (srat != NULL) {
707 memcpy((void *)current, srat, srat->header.length);
708 srat = (acpi_srat_t *) current;
709 current += srat->header.length;
710 acpi_add_table(rsdp, srat);
711 } else {
712 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
713 }
714
715 /* SLIT */
716 current = ALIGN(current, 8);
717 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
718 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
719 if (slit != NULL) {
720 memcpy((void *)current, slit, slit->header.length);
721 slit = (acpi_slit_t *) current;
722 current += slit->header.length;
723 acpi_add_table(rsdp, slit);
724 } else {
725 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
726 }
727
728 /* ALIB */
729 current = ALIGN(current, 16);
730 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
731 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
732 if (alib != NULL) {
733 memcpy((void *)current, alib, alib->length);
734 alib = (acpi_header_t *) current;
735 current += alib->length;
736 acpi_add_table(rsdp, (void *)alib);
737 }
738 else {
739 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
740 }
741
742 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
743 /* SSDT */
744 current = ALIGN(current, 16);
745 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
746 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
747 if (ssdt != NULL) {
748 memcpy((void *)current, ssdt, ssdt->length);
749 ssdt = (acpi_header_t *) current;
750 current += ssdt->length;
751 }
752 else {
753 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
754 }
755 acpi_add_table(rsdp,ssdt);
756
757 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
758 return current;
759}
760
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600761static struct device_operations northbridge_operations = {
762 .read_resources = read_resources,
763 .set_resources = set_resources,
764 .enable_resources = pci_dev_enable_resources,
765 .init = northbridge_init,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200766 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
767 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600768 .enable = 0,
769 .ops_pci = 0,
770};
771
772static const struct pci_driver family16_northbridge __pci_driver = {
773 .ops = &northbridge_operations,
774 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600775 .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600776};
777
778static const struct pci_driver family10_northbridge __pci_driver = {
779 .ops = &northbridge_operations,
780 .vendor = PCI_VENDOR_ID_AMD,
781 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
782};
783
Dave Frodin891f71a2015-01-19 15:58:24 -0700784static void fam16_finalize(void *chip_info)
785{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300786 struct device *dev;
Dave Frodin891f71a2015-01-19 15:58:24 -0700787 u32 value;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300788 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Dave Frodin891f71a2015-01-19 15:58:24 -0700789 pci_write_config32(dev, 0xF8, 0);
790 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
791
792 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300793 dev = pcidev_on_root(1, 1);
Dave Frodin891f71a2015-01-19 15:58:24 -0700794 value = pci_read_config32(dev, 0x60);
795 value &= ~(1 << 11);
796 pci_write_config32(dev, 0x60, value);
797}
798
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300799struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600800 CHIP_NAME("AMD FAM16 Northbridge")
801 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700802 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600803};
804
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300805static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600806{
807 unsigned reg;
808
809 /* Find the already assigned resource pairs */
810 get_fx_devs();
811 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
812 u32 base, limit;
813 base = f1_read_config32(reg);
814 limit = f1_read_config32(reg + 0x04);
815 /* Is this register allocated? */
816 if ((base & 3) != 0) {
817 unsigned nodeid, reg_link;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300818 struct device *reg_dev;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600819 if (reg < 0xc0) { // mmio
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600820 nodeid = (limit & 0xf) + (base&0x30);
821 } else { // io
822 nodeid = (limit & 0xf) + ((base>>4)&0x30);
823 }
824 reg_link = (limit >> 4) & 7;
825 reg_dev = __f0_dev[nodeid];
826 if (reg_dev) {
827 /* Reserve the resource */
828 struct resource *res;
829 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
830 if (res) {
831 res->flags = 1;
832 }
833 }
834 }
835 }
836 /* FIXME: do we need to check extend conf space?
837 I don't believe that much preset value */
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600838 pci_domain_read_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600839}
840
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300841static void domain_enable_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600842{
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200843#if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600844 /* Must be called after PCI enumeration and resource allocation */
845 if (!acpi_is_wakeup_s3())
846 AGESAWRAPPER(amdinitmid);
847
848 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200849#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600850}
851
852#if CONFIG_HW_MEM_HOLE_SIZEK != 0
853struct hw_mem_hole_info {
854 unsigned hole_startk;
855 int node_id;
856};
857static struct hw_mem_hole_info get_hw_mem_hole_info(void)
858{
859 struct hw_mem_hole_info mem_hole;
860 int i;
861 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
862 mem_hole.node_id = -1;
863 for (i = 0; i < node_nums; i++) {
864 dram_base_mask_t d;
865 u32 hole;
866 d = get_dram_base_mask(i);
867 if (!(d.mask & 1)) continue; // no memory on this node
868 hole = pci_read_config32(__f1_dev[i], 0xf0);
869 if (hole & 2) { // we find the hole
870 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
871 mem_hole.node_id = i; // record the node No with hole
872 break; // only one hole
873 }
874 }
875
876 /* We need to double check if there is special set on base reg and limit reg
877 * are not continuous instead of hole, it will find out its hole_startk.
878 */
879 if (mem_hole.node_id == -1) {
880 resource_t limitk_pri = 0;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600881 for (i = 0; i < node_nums; i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600882 dram_base_mask_t d;
883 resource_t base_k, limit_k;
884 d = get_dram_base_mask(i);
885 if (!(d.base & 1)) continue;
886 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
887 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
888 if (limitk_pri != base_k) { // we find the hole
889 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
890 mem_hole.node_id = i;
891 break; //only one hole
892 }
893 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
894 limitk_pri = limit_k;
895 }
896 }
897 return mem_hole;
898}
899#endif
900
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300901static void domain_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600902{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600903 unsigned long mmio_basek;
904 u32 pci_tolm;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600905 int i, idx;
906 struct bus *link;
907#if CONFIG_HW_MEM_HOLE_SIZEK != 0
908 struct hw_mem_hole_info mem_hole;
909 u32 reset_memhole = 1;
910#endif
911
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600912 pci_tolm = 0xffffffffUL;
913 for (link = dev->link_list; link; link = link->next) {
914 pci_tolm = find_pci_tolm(link);
915 }
916
917 // FIXME handle interleaved nodes. If you fix this here, please fix
918 // amdk8, too.
919 mmio_basek = pci_tolm >> 10;
920 /* Round mmio_basek to something the processor can support */
921 mmio_basek &= ~((1 << 6) -1);
922
923 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
924 // MMIO hole. If you fix this here, please fix amdk8, too.
925 /* Round the mmio hole to 64M */
926 mmio_basek &= ~((64*1024) - 1);
927
928#if CONFIG_HW_MEM_HOLE_SIZEK != 0
929 /* if the hw mem hole is already set in raminit stage, here we will compare
930 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
931 * use hole_basek as mmio_basek and we don't need to reset hole.
932 * otherwise We reset the hole to the mmio_basek
933 */
934
935 mem_hole = get_hw_mem_hole_info();
936
937 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
938 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
939 mmio_basek = mem_hole.hole_startk;
940 reset_memhole = 0;
941 }
942#endif
943
944 idx = 0x10;
945 for (i = 0; i < node_nums; i++) {
946 dram_base_mask_t d;
947 resource_t basek, limitk, sizek; // 4 1T
948
949 d = get_dram_base_mask(i);
950
951 if (!(d.mask & 1)) continue;
952 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200953 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600954
955 sizek = limitk - basek;
956
957 /* see if we need a hole from 0xa0000 to 0xbffff */
958 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
959 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
960 idx += 0x10;
961 basek = (8*64)+(16*16);
962 sizek = limitk - ((8*64)+(16*16));
963
964 }
965
966 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
967
968 /* split the region to accommodate pci memory space */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600969 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600970 if (basek <= mmio_basek) {
971 unsigned pre_sizek;
972 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600973 if (pre_sizek > 0) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600974 ram_resource(dev, (idx | i), basek, pre_sizek);
975 idx += 0x10;
976 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600977 }
978 basek = mmio_basek;
979 }
980 if ((basek + sizek) <= 4*1024*1024) {
981 sizek = 0;
982 }
983 else {
984 uint64_t topmem2 = bsp_topmem2();
985 basek = 4*1024*1024;
986 sizek = topmem2/1024 - basek;
987 }
988 }
989
990 ram_resource(dev, (idx | i), basek, sizek);
991 idx += 0x10;
992 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
993 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600994 }
995
Kyösti Mälkkie87564f2017-04-15 20:07:53 +0300996 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600997
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200998 for (link = dev->link_list; link; link = link->next) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600999 if (link->children) {
1000 assign_resources(link);
1001 }
1002 }
1003}
1004
Aaron Durbinaa090cb2017-09-13 16:01:52 -06001005static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001006{
1007 if (dev->path.type == DEVICE_PATH_DOMAIN)
1008 return "PCI0";
1009
1010 return NULL;
1011}
1012
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001013static struct device_operations pci_domain_ops = {
1014 .read_resources = domain_read_resources,
1015 .set_resources = domain_set_resources,
1016 .enable_resources = domain_enable_resources,
1017 .init = NULL,
1018 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001019 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001020};
1021
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001022static void sysconf_init(struct device *dev) // first node
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001023{
1024 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1025 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1026}
1027
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001028static void add_more_links(struct device *dev, unsigned total_links)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001029{
1030 struct bus *link, *last = NULL;
1031 int link_num;
1032
1033 for (link = dev->link_list; link; link = link->next)
1034 last = link;
1035
1036 if (last) {
1037 int links = total_links - last->link_num;
1038 link_num = last->link_num;
1039 if (links > 0) {
1040 link = malloc(links*sizeof(*link));
1041 if (!link)
1042 die("Couldn't allocate more links!\n");
1043 memset(link, 0, links*sizeof(*link));
1044 last->next = link;
1045 }
1046 }
1047 else {
1048 link_num = -1;
1049 link = malloc(total_links*sizeof(*link));
1050 memset(link, 0, total_links*sizeof(*link));
1051 dev->link_list = link;
1052 }
1053
1054 for (link_num = link_num + 1; link_num < total_links; link_num++) {
1055 link->link_num = link_num;
1056 link->dev = dev;
1057 link->next = link + 1;
1058 last = link;
1059 link = link->next;
1060 }
1061 last->next = NULL;
1062}
1063
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001064static void cpu_bus_scan(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001065{
1066 struct bus *cpu_bus;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001067 struct device *dev_mc;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001068 int i,j;
1069 int coreid_bits;
1070 int core_max = 0;
1071 unsigned ApicIdCoreIdSize;
1072 unsigned core_nums;
1073 int siblings = 0;
1074 unsigned int family;
1075 u32 modules = 0;
1076 VOID* modules_ptr = &modules;
1077 BUILD_OPT_CFG* options = NULL;
1078 int ioapic_count = 0;
1079
1080 // TODO Remove the printk's.
1081 printk(BIOS_SPEW, "MullinsPI Debug: Grabbing the AMD Topology Information.\n");
1082 AmdGetValue(AMD_GLOBAL_USER_OPTIONS, (VOID**)&options, sizeof(options));
1083 AmdGetValue(AMD_GLOBAL_NUM_MODULES, &modules_ptr, sizeof(modules));
Alexandru Gagniuc2e0cf142014-12-28 20:38:32 -06001084 modules = *(u32*)modules_ptr;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001085 ASSERT(modules > 0);
1086 ASSERT(options);
1087 ioapic_count = (int)options->CfgPlatNumIoApics;
1088 ASSERT(ioapic_count > 0);
1089 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of Modules (@0x%p) is %d\n", modules_ptr, modules);
1090 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of IOAPICs (@0x%p) is %d\n", options, (int)options->CfgPlatNumIoApics);
1091
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001092 dev_mc = pcidev_on_root(CONFIG_CDB, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001093 if (!dev_mc) {
1094 printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB);
1095 die("");
1096 }
1097 sysconf_init(dev_mc);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001098
1099 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +03001100 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001101 core_max = 1 << (coreid_bits & 0x000F); //mnc
1102
1103 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1104 if (ApicIdCoreIdSize) {
1105 core_nums = (1 << ApicIdCoreIdSize) - 1;
1106 } else {
1107 core_nums = 3; //quad core
1108 }
1109
1110 /* Find which cpus are present */
1111 cpu_bus = dev->link_list;
1112 for (i = 0; i < node_nums; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001113 struct device *cdb_dev;
Kyösti Mälkkiedf51d22018-05-20 22:38:00 +03001114 unsigned devn;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001115 struct bus *pbus;
1116
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001117 devn = CONFIG_CDB + i;
1118 pbus = dev_mc->bus;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001119
1120 /* Find the cpu's pci device */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001121 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001122 if (!cdb_dev) {
1123 /* If I am probing things in a weird order
1124 * ensure all of the cpu's pci devices are found.
1125 */
1126 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001127 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001128 cdb_dev = pci_probe_dev(NULL, pbus,
1129 PCI_DEVFN(devn, fn));
1130 }
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001131 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001132 } else {
1133 /* Ok, We need to set the links for that device.
1134 * otherwise the device under it will not be scanned
1135 */
Kyösti Mälkkic5163ed82015-02-04 13:25:37 +02001136
1137 add_more_links(cdb_dev, 4);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001138 }
1139
1140 family = cpuid_eax(1);
1141 family = (family >> 20) & 0xFF;
1142 if (family == 1) { //f10
1143 u32 dword;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001144 cdb_dev = pcidev_on_root(devn, 3);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001145 dword = pci_read_config32(cdb_dev, 0xe8);
1146 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1147 } else if (family == 7) {//f16
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001148 cdb_dev = pcidev_on_root(devn, 5);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001149 if (cdb_dev && cdb_dev->enabled) {
1150 siblings = pci_read_config32(cdb_dev, 0x84);
1151 siblings &= 0xFF;
1152 }
1153 } else {
1154 siblings = 0; //default one core
1155 }
1156 int enable_node = cdb_dev && cdb_dev->enabled;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001157 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 -06001158 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1159
Elyes HAOUASa8131602016-09-19 10:27:57 -06001160 for (j = 0; j <= siblings; j++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001161 u32 lapicid_start = 0;
1162
1163 /*
1164 * APIC ID calucation is tightly coupled with AGESA v5 code.
1165 * This calculation MUST match the assignment calculation done
1166 * in LocalApicInitializationAtEarly() function.
1167 * And reference GetLocalApicIdForCore()
1168 *
1169 * Apply apic enumeration rules
1170 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1171 * put the local-APICs at m..z
1172 *
1173 * This is needed because many IO-APIC devices only have 4 bits
1174 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001175 */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001176 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1177 lapicid_start = (ioapic_count - 1) / core_max;
1178 lapicid_start = (lapicid_start + 1) * core_max;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001179 printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001180 }
1181 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
Elyes HAOUASa8131602016-09-19 10:27:57 -06001182 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001183 i, j, apic_id);
1184
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001185 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001186 if (cpu)
1187 amd_cpu_topology(cpu, i, j);
1188 } //j
1189 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001190}
1191
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001192static void cpu_bus_init(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001193{
1194 initialize_cpus(dev->link_list);
1195}
1196
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001197static struct device_operations cpu_bus_ops = {
Kyösti Mälkki48f82a92016-12-02 16:02:30 +02001198 .read_resources = DEVICE_NOOP,
1199 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001200 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001201 .init = cpu_bus_init,
1202 .scan_bus = cpu_bus_scan,
1203};
1204
1205static void root_complex_enable_dev(struct device *dev)
1206{
1207 static int done = 0;
1208
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001209 if (!done) {
1210 setup_bsp_ramtop();
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001211 done = 1;
1212 }
1213
1214 /* Set the operations if it is a special bus type */
1215 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1216 dev->ops = &pci_domain_ops;
1217 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1218 dev->ops = &cpu_bus_ops;
1219 }
1220}
1221
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001222struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001223 CHIP_NAME("AMD FAM16 Root Complex")
1224 .enable_dev = root_complex_enable_dev,
1225};
1226
1227/*********************************************************************
1228 * Change the vendor / device IDs to match the generic VBIOS header. *
1229 *********************************************************************/
1230u32 map_oprom_vendev(u32 vendev)
1231{
1232 u32 new_vendev;
1233 new_vendev =
1234 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1235
1236 if (vendev != new_vendev)
1237 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1238
1239 return new_vendev;
1240}