blob: 0c97e4bfa845bacbb87424bb66240cb5a5990edc [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>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060021#include <arch/acpi.h>
22#include <stdint.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
26#include <device/hypertransport.h>
27#include <stdlib.h>
28#include <string.h>
29#include <lib.h>
30#include <cpu/cpu.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060031#include <Porting.h>
32#include <AGESA.h>
33#include <FieldAccessors.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060034#include <Topology.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020035#include <cpu/x86/lapic.h>
36#include <cpu/amd/msr.h>
37#include <cpu/amd/mtrr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020038#include <arch/acpigen.h>
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020039#include <northbridge/amd/pi/nb_common.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030040#include <northbridge/amd/agesa/agesa_helper.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020041#if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER)
Kyösti Mälkki023ed1f2014-10-22 08:05:36 +030042#include <northbridge/amd/pi/agesawrapper.h>
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +030043#include <northbridge/amd/pi/agesawrapper_call.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020044#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -060045
Kyösti Mälkki113f6702018-05-20 20:12:32 +030046#define MAX_NODE_NUMS MAX_NODES
Bruce Griffith27ed80b2014-08-15 11:46:25 -060047
Bruce Griffith27ed80b2014-08-15 11:46:25 -060048typedef struct dram_base_mask {
49 u32 base; //[47:27] at [28:8]
50 u32 mask; //[47:27] at [28:8] and enable at bit 0
51} dram_base_mask_t;
52
53static unsigned node_nums;
54static unsigned sblink;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030055static struct device *__f0_dev[MAX_NODE_NUMS];
56static struct device *__f1_dev[MAX_NODE_NUMS];
57static struct device *__f2_dev[MAX_NODE_NUMS];
58static struct device *__f4_dev[MAX_NODE_NUMS];
Bruce Griffith27ed80b2014-08-15 11:46:25 -060059static unsigned fx_devs = 0;
60
61static dram_base_mask_t get_dram_base_mask(u32 nodeid)
62{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030063 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060064 dram_base_mask_t d;
65 dev = __f1_dev[0];
66 u32 temp;
67 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
68 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
69 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
70 d.mask |= temp<<21;
71 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
72 d.mask |= (temp & 1); // enable bit
73 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
74 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
75 d.base |= temp<<21;
76 return d;
77}
78
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030079static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Bruce Griffith27ed80b2014-08-15 11:46:25 -060080 u32 io_min, u32 io_max)
81{
82 u32 i;
83 u32 tempreg;
84 /* io range allocation */
85 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060086 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060087 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUASa8131602016-09-19 10:27:57 -060088 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUASa8131602016-09-19 10:27:57 -060089 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060090 pci_write_config32(__f1_dev[i], reg, tempreg);
91}
92
93static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
94{
95 u32 i;
96 u32 tempreg;
97 /* io range allocation */
98 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060099 for (i = 0; i < nodes; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600100 pci_write_config32(__f1_dev[i], reg+4, tempreg);
101 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUASa8131602016-09-19 10:27:57 -0600102 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600103 pci_write_config32(__f1_dev[i], reg, tempreg);
104}
105
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300106static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600107{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200108 return pcidev_on_root(DEV_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600109}
110
111static void get_fx_devs(void)
112{
113 int i;
114 for (i = 0; i < MAX_NODE_NUMS; i++) {
115 __f0_dev[i] = get_node_pci(i, 0);
116 __f1_dev[i] = get_node_pci(i, 1);
117 __f2_dev[i] = get_node_pci(i, 2);
118 __f4_dev[i] = get_node_pci(i, 4);
119 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
120 fx_devs = i+1;
121 }
122 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
123 die("Cannot find 0:0x18.[0|1]\n");
124 }
Elyes HAOUASa8131602016-09-19 10:27:57 -0600125 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600126}
127
128static u32 f1_read_config32(unsigned reg)
129{
130 if (fx_devs == 0)
131 get_fx_devs();
132 return pci_read_config32(__f1_dev[0], reg);
133}
134
135static void f1_write_config32(unsigned reg, u32 value)
136{
137 int i;
138 if (fx_devs == 0)
139 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200140 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300141 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600142 dev = __f1_dev[i];
143 if (dev && dev->enabled) {
144 pci_write_config32(dev, reg, value);
145 }
146 }
147}
148
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300149static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600150{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200151 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600152}
153
154static void set_vga_enable_reg(u32 nodeid, u32 linkn)
155{
156 u32 val;
157
158 val = 1 | (nodeid<<4) | (linkn<<12);
159 /* it will routing
160 * (1)mmio 0xa0000:0xbffff
161 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
162 */
163 f1_write_config32(0xf4, val);
164
165}
166
167/**
168 * @return
169 * @retval 2 resoure does not exist, usable
170 * @retval 0 resource exists, not usable
171 * @retval 1 resource exist, resource has been allocated before
172 */
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300173static int reg_useable(unsigned reg, struct device *goal_dev, unsigned goal_nodeid,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600174 unsigned goal_link)
175{
176 struct resource *res;
177 unsigned nodeid, link = 0;
178 int result;
179 res = 0;
180 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300181 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600182 dev = __f0_dev[nodeid];
183 if (!dev)
184 continue;
185 for (link = 0; !res && (link < 8); link++) {
186 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
187 }
188 }
189 result = 2;
190 if (res) {
191 result = 0;
192 if ((goal_link == (link - 1)) &&
193 (goal_nodeid == (nodeid - 1)) &&
194 (res->flags <= 1)) {
195 result = 1;
196 }
197 }
198 return result;
199}
200
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300201static struct resource *amdfam16_find_iopair(struct device *dev, unsigned nodeid, unsigned link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600202{
203 struct resource *resource;
204 u32 free_reg, reg;
205 resource = 0;
206 free_reg = 0;
207 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
208 int result;
209 result = reg_useable(reg, dev, nodeid, link);
210 if (result == 1) {
211 /* I have been allocated this one */
212 break;
213 }
214 else if (result > 1) {
215 /* I have a free register pair */
216 free_reg = reg;
217 }
218 }
219 if (reg > 0xd8) {
220 reg = free_reg; // if no free, the free_reg still be 0
221 }
222
223 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
224
225 return resource;
226}
227
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300228static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600229{
230 struct resource *resource;
231 u32 free_reg, reg;
232 resource = 0;
233 free_reg = 0;
234 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
235 int result;
236 result = reg_useable(reg, dev, nodeid, link);
237 if (result == 1) {
238 /* I have been allocated this one */
239 break;
240 }
241 else if (result > 1) {
242 /* I have a free register pair */
243 free_reg = reg;
244 }
245 }
246 if (reg > 0xb8) {
247 reg = free_reg;
248 }
249
250 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
251 return resource;
252}
253
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300254static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600255{
256 struct resource *resource;
257
258 /* Initialize the io space constraints on the current bus */
259 resource = amdfam16_find_iopair(dev, nodeid, link);
260 if (resource) {
261 u32 align;
262 align = log2(HT_IO_HOST_ALIGN);
263 resource->base = 0;
264 resource->size = 0;
265 resource->align = align;
266 resource->gran = align;
267 resource->limit = 0xffffUL;
268 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
269 }
270
271 /* Initialize the prefetchable memory constraints on the current bus */
272 resource = amdfam16_find_mempair(dev, nodeid, link);
273 if (resource) {
274 resource->base = 0;
275 resource->size = 0;
276 resource->align = log2(HT_MEM_HOST_ALIGN);
277 resource->gran = log2(HT_MEM_HOST_ALIGN);
278 resource->limit = 0xffffffffffULL;
279 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
280 resource->flags |= IORESOURCE_BRIDGE;
281 }
282
283 /* Initialize the memory constraints on the current bus */
284 resource = amdfam16_find_mempair(dev, nodeid, link);
285 if (resource) {
286 resource->base = 0;
287 resource->size = 0;
288 resource->align = log2(HT_MEM_HOST_ALIGN);
289 resource->gran = log2(HT_MEM_HOST_ALIGN);
290 resource->limit = 0xffffffffffULL;
291 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
292 }
293
294}
295
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300296static void read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600297{
298 u32 nodeid;
299 struct bus *link;
300
301 nodeid = amdfam16_nodeid(dev);
302 for (link = dev->link_list; link; link = link->next) {
303 if (link->children) {
304 amdfam16_link_read_bases(dev, nodeid, link->link_num);
305 }
306 }
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300307
308 /*
309 * This MMCONF resource must be reserved in the PCI domain.
310 * It is not honored by the coreboot resource allocator if it is in
311 * the CPU_CLUSTER.
312 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200313 mmconf_resource(dev, MMIO_CONF_BASE);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600314}
315
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300316static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600317{
318 resource_t rbase, rend;
319 unsigned reg, link_num;
320 char buf[50];
321
322 /* Make certain the resource has actually been set */
323 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
324 return;
325 }
326
327 /* If I have already stored this resource don't worry about it */
328 if (resource->flags & IORESOURCE_STORED) {
329 return;
330 }
331
332 /* Only handle PCI memory and IO resources */
333 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
334 return;
335
336 /* Ensure I am actually looking at a resource of function 1 */
337 if ((resource->index & 0xffff) < 0x1000) {
338 return;
339 }
340 /* Get the base address */
341 rbase = resource->base;
342
343 /* Get the limit (rounded up) */
344 rend = resource_end(resource);
345
346 /* Get the register and link */
347 reg = resource->index & 0xfff; // 4k
348 link_num = IOINDEX_LINK(resource->index);
349
350 if (resource->flags & IORESOURCE_IO) {
351 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
352 }
353 else if (resource->flags & IORESOURCE_MEM) {
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200354 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 -0600355 }
356 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200357 snprintf(buf, sizeof(buf), " <node %x link %x>",
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600358 nodeid, link_num);
359 report_resource_stored(dev, resource, buf);
360}
361
362/**
363 * I tried to reuse the resource allocation code in set_resource()
364 * but it is too difficult to deal with the resource allocation magic.
365 */
366
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300367static void create_vga_resource(struct device *dev, unsigned nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600368{
369 struct bus *link;
370
371 /* find out which link the VGA card is connected,
372 * we only deal with the 'first' vga card */
373 for (link = dev->link_list; link; link = link->next) {
374 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Martin Roth77a58b92017-06-24 14:45:48 -0600375#if IS_ENABLED(CONFIG_MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300376 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600377 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
378 link->secondary,link->subordinate);
379 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600380 if ((vga_pri->bus->secondary >= link->secondary) &&
381 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600382#endif
383 break;
384 }
385 }
386
387 /* no VGA card installed */
388 if (link == NULL)
389 return;
390
391 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
392 set_vga_enable_reg(nodeid, sblink);
393}
394
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300395static void set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600396{
397 unsigned nodeid;
398 struct bus *bus;
399 struct resource *res;
400
401 /* Find the nodeid */
402 nodeid = amdfam16_nodeid(dev);
403
404 create_vga_resource(dev, nodeid); //TODO: do we need this?
405
406 /* Set each resource we have found */
407 for (res = dev->resource_list; res; res = res->next) {
408 set_resource(dev, res, nodeid);
409 }
410
411 for (bus = dev->link_list; bus; bus = bus->next) {
412 if (bus->children) {
413 assign_resources(bus);
414 }
415 }
416}
417
418static void northbridge_init(struct device *dev)
419{
420}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200421
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100422static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200423{
424 void *addr, *current;
425
426 /* Skip the HEST header. */
427 current = (void *)(hest + 1);
428
429 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
430 if (addr != NULL)
431 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
432
433 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
434 if (addr != NULL)
435 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
436
437 return (unsigned long)current;
438}
439
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500440static void add_ivhd_dev_entry(struct device *parent, struct device *dev,
441 unsigned long *current, uint16_t *length,
442 uint8_t type, uint8_t data)
443{
444 uint8_t *p;
445 p = (uint8_t *) *current;
446
447 if (type == 0x2) {
448 /* Entry type */
449 p[0] = type;
450 /* Device */
451 p[1] = dev->path.pci.devfn;
452 /* Bus */
453 p[2] = dev->bus->secondary;
454 /* Data */
455 p[3] = data;
456 /* [4:7] Padding */
457 p[4] = 0x0;
458 p[5] = 0x0;
459 p[6] = 0x0;
460 p[7] = 0x0;
461 *length += 8;
462 *current += 8;
463 } else if (type == 0x42) {
464 /* Entry type */
465 p[0] = type;
466 /* Device */
467 p[1] = dev->path.pci.devfn;
468 /* Bus */
469 p[2] = dev->bus->secondary;
470 /* Data */
471 p[3] = 0x0;
472 /* Reserved */
473 p[4] = 0x0;
474 /* Device */
475 p[5] = parent->path.pci.devfn;
476 /* Bus */
477 p[6] = parent->bus->secondary;
478 /* Reserved */
479 p[7] = 0x0;
480 *length += 8;
481 *current += 8;
482 }
483}
484
485static void add_ivrs_device_entries(struct device *parent, struct device *dev,
486 unsigned int depth, int linknum, int8_t *root_level,
487 unsigned long *current, uint16_t *length)
488{
489 struct device *sibling;
490 struct bus *link;
491 unsigned int header_type;
492 unsigned int is_pcie;
493
494 if (!root_level) {
495 root_level = malloc(sizeof(int8_t));
496 *root_level = -1;
497 }
498
499 if (dev->path.type == DEVICE_PATH_PCI) {
500
501 if ((dev->bus->secondary == 0x0) &&
502 (dev->path.pci.devfn == 0x0))
503 *root_level = depth;
504
505 if ((*root_level != -1) && (dev->enabled)) {
506 if (depth == *root_level) {
507 if (dev->path.pci.devfn == (0x14 << 3)) {
508 /* SMBUS controller */
509 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x97);
510 } else if (dev->path.pci.devfn != 0x2 &&
511 dev->path.pci.devfn < (0x2 << 3)) {
512 /* FCH control device */
513 } else {
514 /* Other devices */
515 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
516 }
517 } else {
518 header_type = dev->hdr_type & 0x7f;
519 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
520 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
521 (header_type == PCI_HEADER_TYPE_BRIDGE))
522 && is_pcie) {
523 /* Device or Bridge is PCIe */
524 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
525 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) &&
526 !is_pcie) {
527 add_ivhd_dev_entry(parent, dev, current, length, 0x42, 0x0);
528 /* Device is legacy PCI or PCI-X */
529 }
530 }
531 }
532 }
533
534 for (link = dev->link_list; link; link = link->next)
535 for (sibling = link->children; sibling; sibling =
536 sibling->sibling)
537 add_ivrs_device_entries(dev, sibling, depth + 1, depth,
538 root_level, current, length);
539
540 free(root_level);
541}
542
543unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
544{
545 uint8_t *p;
546
547 uint32_t apicid_sb800;
548 uint32_t apicid_northbridge;
549
550 apicid_sb800 = CONFIG_MAX_CPUS;
551 apicid_northbridge = CONFIG_MAX_CPUS + 1;
552
553 /* Describe NB IOAPIC */
554 p = (uint8_t *)current;
555 p[0] = 0x48; /* Entry type */
556 p[1] = 0; /* Device */
557 p[2] = 0; /* Bus */
558 p[3] = 0x0; /* Data */
559 p[4] = apicid_northbridge; /* IOAPIC ID */
560 p[5] = 0x0; /* Device 0 Function 0 */
561 p[6] = 0x0; /* Northbridge bus */
562 p[7] = 0x1; /* Variety */
563 current += 8;
564
565 /* Describe SB IOAPIC */
566 p = (uint8_t *)current;
567 p[0] = 0x48; /* Entry type */
568 p[1] = 0; /* Device */
569 p[2] = 0; /* Bus */
570 p[3] = 0xd7; /* Data */
571 p[4] = apicid_sb800; /* IOAPIC ID */
572 p[5] = 0x14 << 3; /* Device 0x14 Function 0 */
573 p[6] = 0x0; /* Southbridge bus */
574 p[7] = 0x1; /* Variety */
575 current += 8;
576
577 return current;
578}
579
580static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
581{
582 uint8_t *p;
Piotr Król063e1562018-07-22 20:52:26 +0200583 acpi_ivrs_t *ivrs_agesa;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500584
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300585 struct device *nb_dev = pcidev_on_root(0x0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500586 if (!nb_dev) {
587
588 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
589 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
590
591 return (unsigned long)ivrs;
592 }
593
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500594
Piotr Król063e1562018-07-22 20:52:26 +0200595 /* obtain IOMMU base address */
596 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
597 if (ivrs_agesa != NULL) {
598 ivrs->iv_info = 0x0;
599 /* Maximum supported virtual address size */
600 ivrs->iv_info |= (0x40 << 15);
601 /* Maximum supported physical address size */
602 ivrs->iv_info |= (0x30 << 8);
603 /* Guest virtual address width */
604 ivrs->iv_info |= (0x2 << 5);
605
606 ivrs->ivhd.type = 0x10;
607 ivrs->ivhd.flags = 0x0e;
608 /* Enable ATS support */
609 ivrs->ivhd.flags |= 0x10;
610 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
611 /* BDF <bus>:00.2 */
612 ivrs->ivhd.device_id = 0x2 | (nb_dev->bus->secondary << 8);
613 /* Capability block 0x40 (type 0xf, "Secure device") */
614 ivrs->ivhd.capability_offset = 0x40;
615 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
616 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
617 ivrs->ivhd.pci_segment_group = 0x0;
618 ivrs->ivhd.iommu_info = 0x0;
619 ivrs->ivhd.iommu_info |= (0x13 << 8);
620 /* use only performance counters related bits:
621 * PNCounters[16:13] and
622 * PNBanks[22:17],
623 * otherwise 0 */
624 ivrs->ivhd.iommu_feature_info =
625 ivrs_agesa->ivhd.iommu_feature_info & 0x7fe000;
626 } else {
627 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
628
629 return (unsigned long)ivrs;
630 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500631
632 /* Describe HPET */
633 p = (uint8_t *)current;
634 p[0] = 0x48; /* Entry type */
635 p[1] = 0; /* Device */
636 p[2] = 0; /* Bus */
637 p[3] = 0xd7; /* Data */
638 p[4] = 0x0; /* HPET number */
639 p[5] = 0x14 << 3; /* HPET device */
640 p[6] = nb_dev->bus->secondary; /* HPET bus */
641 p[7] = 0x2; /* Variety */
642 ivrs->ivhd.length += 8;
643 current += 8;
644
645 /* Describe PCI devices */
646 add_ivrs_device_entries(NULL, all_devices, 0, -1, NULL, &current,
647 &ivrs->ivhd.length);
648
649 /* Describe IOAPICs */
650 unsigned long prev_current = current;
651 current = acpi_fill_ivrs_ioapic(ivrs, current);
652 ivrs->ivhd.length += (current - prev_current);
653
654 return current;
655}
656
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300657static void northbridge_fill_ssdt_generator(struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200658{
659 msr_t msr;
660 char pscope[] = "\\_SB.PCI0";
661
662 acpigen_write_scope(pscope);
663 msr = rdmsr(TOP_MEM);
664 acpigen_write_name_dword("TOM1", msr.lo);
665 msr = rdmsr(TOP_MEM2);
666 /*
667 * Since XP only implements parts of ACPI 2.0, we can't use a qword
668 * here.
669 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
670 * slide 22ff.
671 * Shift value right by 20 bit to make it fit into 32bit,
672 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
673 */
674 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
675 acpigen_pop_len();
676}
677
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300678static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200679 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200680 acpi_rsdp_t *rsdp)
681{
682 acpi_srat_t *srat;
683 acpi_slit_t *slit;
684 acpi_header_t *ssdt;
685 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500686 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200687 acpi_hest_t *hest;
688
689 /* HEST */
690 current = ALIGN(current, 8);
691 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100692 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200693 acpi_add_table(rsdp, (void *)current);
694 current += ((acpi_header_t *)current)->length;
695
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500696 /* IVRS */
697 current = ALIGN(current, 8);
698 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
699 ivrs = (acpi_ivrs_t *) current;
700 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
701 current += ivrs->header.length;
702 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200703
704 /* SRAT */
705 current = ALIGN(current, 8);
706 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
707 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
708 if (srat != NULL) {
709 memcpy((void *)current, srat, srat->header.length);
710 srat = (acpi_srat_t *) current;
711 current += srat->header.length;
712 acpi_add_table(rsdp, srat);
713 } else {
714 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
715 }
716
717 /* SLIT */
718 current = ALIGN(current, 8);
719 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
720 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
721 if (slit != NULL) {
722 memcpy((void *)current, slit, slit->header.length);
723 slit = (acpi_slit_t *) current;
724 current += slit->header.length;
725 acpi_add_table(rsdp, slit);
726 } else {
727 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
728 }
729
730 /* ALIB */
731 current = ALIGN(current, 16);
732 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
733 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
734 if (alib != NULL) {
735 memcpy((void *)current, alib, alib->length);
736 alib = (acpi_header_t *) current;
737 current += alib->length;
738 acpi_add_table(rsdp, (void *)alib);
739 }
740 else {
741 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
742 }
743
744 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
745 /* SSDT */
746 current = ALIGN(current, 16);
747 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
748 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
749 if (ssdt != NULL) {
750 memcpy((void *)current, ssdt, ssdt->length);
751 ssdt = (acpi_header_t *) current;
752 current += ssdt->length;
753 }
754 else {
755 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
756 }
757 acpi_add_table(rsdp,ssdt);
758
759 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
760 return current;
761}
762
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600763static struct device_operations northbridge_operations = {
764 .read_resources = read_resources,
765 .set_resources = set_resources,
766 .enable_resources = pci_dev_enable_resources,
767 .init = northbridge_init,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200768 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
769 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600770 .enable = 0,
771 .ops_pci = 0,
772};
773
774static const struct pci_driver family16_northbridge __pci_driver = {
775 .ops = &northbridge_operations,
776 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600777 .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600778};
779
780static const struct pci_driver family10_northbridge __pci_driver = {
781 .ops = &northbridge_operations,
782 .vendor = PCI_VENDOR_ID_AMD,
783 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
784};
785
Dave Frodin891f71a2015-01-19 15:58:24 -0700786static void fam16_finalize(void *chip_info)
787{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300788 struct device *dev;
Dave Frodin891f71a2015-01-19 15:58:24 -0700789 u32 value;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300790 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Dave Frodin891f71a2015-01-19 15:58:24 -0700791 pci_write_config32(dev, 0xF8, 0);
792 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
793
794 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300795 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200796 if (dev != NULL) {
797 value = pci_read_config32(dev, 0x60);
798 value &= ~(1 << 11);
799 pci_write_config32(dev, 0x60, value);
800 }
Dave Frodin891f71a2015-01-19 15:58:24 -0700801}
802
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300803struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600804 CHIP_NAME("AMD FAM16 Northbridge")
805 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700806 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600807};
808
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300809static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600810{
811 unsigned reg;
812
813 /* Find the already assigned resource pairs */
814 get_fx_devs();
815 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
816 u32 base, limit;
817 base = f1_read_config32(reg);
818 limit = f1_read_config32(reg + 0x04);
819 /* Is this register allocated? */
820 if ((base & 3) != 0) {
821 unsigned nodeid, reg_link;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300822 struct device *reg_dev;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600823 if (reg < 0xc0) { // mmio
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600824 nodeid = (limit & 0xf) + (base&0x30);
825 } else { // io
826 nodeid = (limit & 0xf) + ((base>>4)&0x30);
827 }
828 reg_link = (limit >> 4) & 7;
829 reg_dev = __f0_dev[nodeid];
830 if (reg_dev) {
831 /* Reserve the resource */
832 struct resource *res;
833 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
834 if (res) {
835 res->flags = 1;
836 }
837 }
838 }
839 }
840 /* FIXME: do we need to check extend conf space?
841 I don't believe that much preset value */
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600842 pci_domain_read_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600843}
844
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300845static void domain_enable_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600846{
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200847#if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600848 /* Must be called after PCI enumeration and resource allocation */
849 if (!acpi_is_wakeup_s3())
850 AGESAWRAPPER(amdinitmid);
851
852 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200853#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600854}
855
856#if CONFIG_HW_MEM_HOLE_SIZEK != 0
857struct hw_mem_hole_info {
858 unsigned hole_startk;
859 int node_id;
860};
861static struct hw_mem_hole_info get_hw_mem_hole_info(void)
862{
863 struct hw_mem_hole_info mem_hole;
864 int i;
865 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
866 mem_hole.node_id = -1;
867 for (i = 0; i < node_nums; i++) {
868 dram_base_mask_t d;
869 u32 hole;
870 d = get_dram_base_mask(i);
871 if (!(d.mask & 1)) continue; // no memory on this node
872 hole = pci_read_config32(__f1_dev[i], 0xf0);
873 if (hole & 2) { // we find the hole
874 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
875 mem_hole.node_id = i; // record the node No with hole
876 break; // only one hole
877 }
878 }
879
880 /* We need to double check if there is special set on base reg and limit reg
881 * are not continuous instead of hole, it will find out its hole_startk.
882 */
883 if (mem_hole.node_id == -1) {
884 resource_t limitk_pri = 0;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600885 for (i = 0; i < node_nums; i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600886 dram_base_mask_t d;
887 resource_t base_k, limit_k;
888 d = get_dram_base_mask(i);
889 if (!(d.base & 1)) continue;
890 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
891 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
892 if (limitk_pri != base_k) { // we find the hole
893 mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G
894 mem_hole.node_id = i;
895 break; //only one hole
896 }
897 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
898 limitk_pri = limit_k;
899 }
900 }
901 return mem_hole;
902}
903#endif
904
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300905static void domain_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600906{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600907 unsigned long mmio_basek;
908 u32 pci_tolm;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600909 int i, idx;
910 struct bus *link;
911#if CONFIG_HW_MEM_HOLE_SIZEK != 0
912 struct hw_mem_hole_info mem_hole;
913 u32 reset_memhole = 1;
914#endif
915
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600916 pci_tolm = 0xffffffffUL;
917 for (link = dev->link_list; link; link = link->next) {
918 pci_tolm = find_pci_tolm(link);
919 }
920
921 // FIXME handle interleaved nodes. If you fix this here, please fix
922 // amdk8, too.
923 mmio_basek = pci_tolm >> 10;
924 /* Round mmio_basek to something the processor can support */
925 mmio_basek &= ~((1 << 6) -1);
926
927 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
928 // MMIO hole. If you fix this here, please fix amdk8, too.
929 /* Round the mmio hole to 64M */
930 mmio_basek &= ~((64*1024) - 1);
931
932#if CONFIG_HW_MEM_HOLE_SIZEK != 0
933 /* if the hw mem hole is already set in raminit stage, here we will compare
934 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
935 * use hole_basek as mmio_basek and we don't need to reset hole.
936 * otherwise We reset the hole to the mmio_basek
937 */
938
939 mem_hole = get_hw_mem_hole_info();
940
941 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
942 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
943 mmio_basek = mem_hole.hole_startk;
944 reset_memhole = 0;
945 }
946#endif
947
948 idx = 0x10;
949 for (i = 0; i < node_nums; i++) {
950 dram_base_mask_t d;
951 resource_t basek, limitk, sizek; // 4 1T
952
953 d = get_dram_base_mask(i);
954
955 if (!(d.mask & 1)) continue;
956 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200957 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600958
959 sizek = limitk - basek;
960
961 /* see if we need a hole from 0xa0000 to 0xbffff */
962 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
963 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
964 idx += 0x10;
965 basek = (8*64)+(16*16);
966 sizek = limitk - ((8*64)+(16*16));
967
968 }
969
970 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
971
972 /* split the region to accommodate pci memory space */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600973 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600974 if (basek <= mmio_basek) {
975 unsigned pre_sizek;
976 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600977 if (pre_sizek > 0) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600978 ram_resource(dev, (idx | i), basek, pre_sizek);
979 idx += 0x10;
980 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600981 }
982 basek = mmio_basek;
983 }
984 if ((basek + sizek) <= 4*1024*1024) {
985 sizek = 0;
986 }
987 else {
988 uint64_t topmem2 = bsp_topmem2();
989 basek = 4*1024*1024;
990 sizek = topmem2/1024 - basek;
991 }
992 }
993
994 ram_resource(dev, (idx | i), basek, sizek);
995 idx += 0x10;
996 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
997 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600998 }
999
Kyösti Mälkkie87564f2017-04-15 20:07:53 +03001000 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001001
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001002 for (link = dev->link_list; link; link = link->next) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001003 if (link->children) {
1004 assign_resources(link);
1005 }
1006 }
1007}
1008
Aaron Durbinaa090cb2017-09-13 16:01:52 -06001009static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001010{
1011 if (dev->path.type == DEVICE_PATH_DOMAIN)
1012 return "PCI0";
1013
1014 return NULL;
1015}
1016
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001017static struct device_operations pci_domain_ops = {
1018 .read_resources = domain_read_resources,
1019 .set_resources = domain_set_resources,
1020 .enable_resources = domain_enable_resources,
1021 .init = NULL,
1022 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001023 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001024};
1025
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001026static void sysconf_init(struct device *dev) // first node
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001027{
1028 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1029 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1030}
1031
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001032static void add_more_links(struct device *dev, unsigned total_links)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001033{
1034 struct bus *link, *last = NULL;
1035 int link_num;
1036
1037 for (link = dev->link_list; link; link = link->next)
1038 last = link;
1039
1040 if (last) {
1041 int links = total_links - last->link_num;
1042 link_num = last->link_num;
1043 if (links > 0) {
1044 link = malloc(links*sizeof(*link));
1045 if (!link)
1046 die("Couldn't allocate more links!\n");
1047 memset(link, 0, links*sizeof(*link));
1048 last->next = link;
1049 }
1050 }
1051 else {
1052 link_num = -1;
1053 link = malloc(total_links*sizeof(*link));
1054 memset(link, 0, total_links*sizeof(*link));
1055 dev->link_list = link;
1056 }
1057
1058 for (link_num = link_num + 1; link_num < total_links; link_num++) {
1059 link->link_num = link_num;
1060 link->dev = dev;
1061 link->next = link + 1;
1062 last = link;
1063 link = link->next;
1064 }
1065 last->next = NULL;
1066}
1067
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001068static void cpu_bus_scan(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001069{
1070 struct bus *cpu_bus;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001071 struct device *dev_mc;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001072 int i,j;
1073 int coreid_bits;
1074 int core_max = 0;
1075 unsigned ApicIdCoreIdSize;
1076 unsigned core_nums;
1077 int siblings = 0;
1078 unsigned int family;
1079 u32 modules = 0;
1080 VOID* modules_ptr = &modules;
1081 BUILD_OPT_CFG* options = NULL;
1082 int ioapic_count = 0;
1083
1084 // TODO Remove the printk's.
1085 printk(BIOS_SPEW, "MullinsPI Debug: Grabbing the AMD Topology Information.\n");
1086 AmdGetValue(AMD_GLOBAL_USER_OPTIONS, (VOID**)&options, sizeof(options));
1087 AmdGetValue(AMD_GLOBAL_NUM_MODULES, &modules_ptr, sizeof(modules));
Alexandru Gagniuc2e0cf142014-12-28 20:38:32 -06001088 modules = *(u32*)modules_ptr;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001089 ASSERT(modules > 0);
1090 ASSERT(options);
1091 ioapic_count = (int)options->CfgPlatNumIoApics;
1092 ASSERT(ioapic_count > 0);
1093 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of Modules (@0x%p) is %d\n", modules_ptr, modules);
1094 printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of IOAPICs (@0x%p) is %d\n", options, (int)options->CfgPlatNumIoApics);
1095
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001096 dev_mc = pcidev_on_root(DEV_CDB, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001097 if (!dev_mc) {
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001098 printk(BIOS_ERR, "0:%02x.0 not found", DEV_CDB);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001099 die("");
1100 }
1101 sysconf_init(dev_mc);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001102
1103 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +03001104 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001105 core_max = 1 << (coreid_bits & 0x000F); //mnc
1106
1107 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1108 if (ApicIdCoreIdSize) {
1109 core_nums = (1 << ApicIdCoreIdSize) - 1;
1110 } else {
1111 core_nums = 3; //quad core
1112 }
1113
1114 /* Find which cpus are present */
1115 cpu_bus = dev->link_list;
1116 for (i = 0; i < node_nums; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001117 struct device *cdb_dev;
Kyösti Mälkkiedf51d22018-05-20 22:38:00 +03001118 unsigned devn;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001119 struct bus *pbus;
1120
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001121 devn = DEV_CDB + i;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001122 pbus = dev_mc->bus;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001123
1124 /* Find the cpu's pci device */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001125 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001126 if (!cdb_dev) {
1127 /* If I am probing things in a weird order
1128 * ensure all of the cpu's pci devices are found.
1129 */
1130 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001131 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001132 cdb_dev = pci_probe_dev(NULL, pbus,
1133 PCI_DEVFN(devn, fn));
1134 }
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001135 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001136 } else {
1137 /* Ok, We need to set the links for that device.
1138 * otherwise the device under it will not be scanned
1139 */
Kyösti Mälkkic5163ed82015-02-04 13:25:37 +02001140
1141 add_more_links(cdb_dev, 4);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001142 }
1143
1144 family = cpuid_eax(1);
1145 family = (family >> 20) & 0xFF;
1146 if (family == 1) { //f10
1147 u32 dword;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001148 cdb_dev = pcidev_on_root(devn, 3);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001149 dword = pci_read_config32(cdb_dev, 0xe8);
1150 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1151 } else if (family == 7) {//f16
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001152 cdb_dev = pcidev_on_root(devn, 5);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001153 if (cdb_dev && cdb_dev->enabled) {
1154 siblings = pci_read_config32(cdb_dev, 0x84);
1155 siblings &= 0xFF;
1156 }
1157 } else {
1158 siblings = 0; //default one core
1159 }
1160 int enable_node = cdb_dev && cdb_dev->enabled;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001161 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 -06001162 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1163
Elyes HAOUASa8131602016-09-19 10:27:57 -06001164 for (j = 0; j <= siblings; j++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001165 u32 lapicid_start = 0;
1166
1167 /*
1168 * APIC ID calucation is tightly coupled with AGESA v5 code.
1169 * This calculation MUST match the assignment calculation done
1170 * in LocalApicInitializationAtEarly() function.
1171 * And reference GetLocalApicIdForCore()
1172 *
1173 * Apply apic enumeration rules
1174 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1175 * put the local-APICs at m..z
1176 *
1177 * This is needed because many IO-APIC devices only have 4 bits
1178 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001179 */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001180 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1181 lapicid_start = (ioapic_count - 1) / core_max;
1182 lapicid_start = (lapicid_start + 1) * core_max;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001183 printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001184 }
1185 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
Elyes HAOUASa8131602016-09-19 10:27:57 -06001186 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001187 i, j, apic_id);
1188
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001189 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001190 if (cpu)
1191 amd_cpu_topology(cpu, i, j);
1192 } //j
1193 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001194}
1195
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001196static void cpu_bus_init(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001197{
1198 initialize_cpus(dev->link_list);
1199}
1200
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001201static struct device_operations cpu_bus_ops = {
Kyösti Mälkki48f82a92016-12-02 16:02:30 +02001202 .read_resources = DEVICE_NOOP,
1203 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001204 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001205 .init = cpu_bus_init,
1206 .scan_bus = cpu_bus_scan,
1207};
1208
1209static void root_complex_enable_dev(struct device *dev)
1210{
1211 static int done = 0;
1212
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001213 if (!done) {
1214 setup_bsp_ramtop();
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001215 done = 1;
1216 }
1217
1218 /* Set the operations if it is a special bus type */
1219 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1220 dev->ops = &pci_domain_ops;
1221 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1222 dev->ops = &cpu_bus_ops;
1223 }
1224}
1225
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001226struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001227 CHIP_NAME("AMD FAM16 Root Complex")
1228 .enable_dev = root_complex_enable_dev,
1229};
1230
1231/*********************************************************************
1232 * Change the vendor / device IDs to match the generic VBIOS header. *
1233 *********************************************************************/
1234u32 map_oprom_vendev(u32 vendev)
1235{
1236 u32 new_vendev;
1237 new_vendev =
1238 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1239
1240 if (vendev != new_vendev)
1241 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1242
1243 return new_vendev;
1244}