blob: cf4d78b4a694219ab59720488beb392a3f4a05bc [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>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060020#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>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060026#include <string.h>
27#include <lib.h>
28#include <cpu/cpu.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060029#include <Porting.h>
30#include <AGESA.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060031#include <Topology.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020032#include <cpu/x86/lapic.h>
33#include <cpu/amd/msr.h>
34#include <cpu/amd/mtrr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020035#include <arch/acpigen.h>
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020036#include <northbridge/amd/pi/nb_common.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030037#include <northbridge/amd/agesa/agesa_helper.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060038
Kyösti Mälkki113f6702018-05-20 20:12:32 +030039#define MAX_NODE_NUMS MAX_NODES
Michał Żygowski6ca5b472019-09-10 15:10:22 +020040#define PCIE_CAP_AER BIT(5)
41#define PCIE_CAP_ACS BIT(6)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060042
Bruce Griffith27ed80b2014-08-15 11:46:25 -060043typedef struct dram_base_mask {
44 u32 base; //[47:27] at [28:8]
45 u32 mask; //[47:27] at [28:8] and enable at bit 0
46} dram_base_mask_t;
47
Subrata Banikb1434fc2019-03-15 22:20:41 +053048static unsigned int node_nums;
49static unsigned int sblink;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030050static struct device *__f0_dev[MAX_NODE_NUMS];
51static struct device *__f1_dev[MAX_NODE_NUMS];
52static struct device *__f2_dev[MAX_NODE_NUMS];
53static struct device *__f4_dev[MAX_NODE_NUMS];
Subrata Banikb1434fc2019-03-15 22:20:41 +053054static unsigned int fx_devs = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060055
56static dram_base_mask_t get_dram_base_mask(u32 nodeid)
57{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030058 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060059 dram_base_mask_t d;
60 dev = __f1_dev[0];
61 u32 temp;
62 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
63 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
64 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
65 d.mask |= temp<<21;
66 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
67 d.mask |= (temp & 1); // enable bit
68 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
69 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
70 d.base |= temp<<21;
71 return d;
72}
73
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030074static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Bruce Griffith27ed80b2014-08-15 11:46:25 -060075 u32 io_min, u32 io_max)
76{
77 u32 i;
78 u32 tempreg;
79 /* io range allocation */
80 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060081 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060082 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUASa8131602016-09-19 10:27:57 -060083 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
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, tempreg);
86}
87
88static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
89{
90 u32 i;
91 u32 tempreg;
92 /* io range allocation */
93 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060094 for (i = 0; i < nodes; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060095 pci_write_config32(__f1_dev[i], reg+4, tempreg);
96 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUASa8131602016-09-19 10:27:57 -060097 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060098 pci_write_config32(__f1_dev[i], reg, tempreg);
99}
100
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300101static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600102{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200103 return pcidev_on_root(DEV_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600104}
105
106static void get_fx_devs(void)
107{
108 int i;
109 for (i = 0; i < MAX_NODE_NUMS; i++) {
110 __f0_dev[i] = get_node_pci(i, 0);
111 __f1_dev[i] = get_node_pci(i, 1);
112 __f2_dev[i] = get_node_pci(i, 2);
113 __f4_dev[i] = get_node_pci(i, 4);
114 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
115 fx_devs = i+1;
116 }
117 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
118 die("Cannot find 0:0x18.[0|1]\n");
119 }
Elyes HAOUASa8131602016-09-19 10:27:57 -0600120 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600121}
122
Subrata Banikb1434fc2019-03-15 22:20:41 +0530123static u32 f1_read_config32(unsigned int reg)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600124{
125 if (fx_devs == 0)
126 get_fx_devs();
127 return pci_read_config32(__f1_dev[0], reg);
128}
129
Subrata Banikb1434fc2019-03-15 22:20:41 +0530130static void f1_write_config32(unsigned int reg, u32 value)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600131{
132 int i;
133 if (fx_devs == 0)
134 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200135 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300136 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600137 dev = __f1_dev[i];
138 if (dev && dev->enabled) {
139 pci_write_config32(dev, reg, value);
140 }
141 }
142}
143
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300144static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600145{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200146 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600147}
148
149static void set_vga_enable_reg(u32 nodeid, u32 linkn)
150{
151 u32 val;
152
153 val = 1 | (nodeid<<4) | (linkn<<12);
154 /* it will routing
155 * (1)mmio 0xa0000:0xbffff
156 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
157 */
158 f1_write_config32(0xf4, val);
159
160}
161
162/**
163 * @return
Elyes HAOUAS99b075a2019-12-30 14:29:31 +0100164 * @retval 2 resource does not exist, usable
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600165 * @retval 0 resource exists, not usable
166 * @retval 1 resource exist, resource has been allocated before
167 */
Subrata Banikb1434fc2019-03-15 22:20:41 +0530168static int reg_useable(unsigned int reg, struct device *goal_dev,
169 unsigned int goal_nodeid, unsigned int goal_link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600170{
171 struct resource *res;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530172 unsigned int nodeid, link = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600173 int result;
174 res = 0;
175 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300176 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600177 dev = __f0_dev[nodeid];
178 if (!dev)
179 continue;
180 for (link = 0; !res && (link < 8); link++) {
181 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
182 }
183 }
184 result = 2;
185 if (res) {
186 result = 0;
187 if ((goal_link == (link - 1)) &&
188 (goal_nodeid == (nodeid - 1)) &&
189 (res->flags <= 1)) {
190 result = 1;
191 }
192 }
193 return result;
194}
195
Subrata Banikb1434fc2019-03-15 22:20:41 +0530196static struct resource *amdfam16_find_iopair(struct device *dev,
197 unsigned int nodeid, unsigned int link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600198{
199 struct resource *resource;
200 u32 free_reg, reg;
201 resource = 0;
202 free_reg = 0;
203 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
204 int result;
205 result = reg_useable(reg, dev, nodeid, link);
206 if (result == 1) {
207 /* I have been allocated this one */
208 break;
209 }
210 else if (result > 1) {
211 /* I have a free register pair */
212 free_reg = reg;
213 }
214 }
215 if (reg > 0xd8) {
216 reg = free_reg; // if no free, the free_reg still be 0
217 }
218
219 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
220
221 return resource;
222}
223
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300224static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600225{
226 struct resource *resource;
227 u32 free_reg, reg;
228 resource = 0;
229 free_reg = 0;
230 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
231 int result;
232 result = reg_useable(reg, dev, nodeid, link);
233 if (result == 1) {
234 /* I have been allocated this one */
235 break;
236 }
237 else if (result > 1) {
238 /* I have a free register pair */
239 free_reg = reg;
240 }
241 }
242 if (reg > 0xb8) {
243 reg = free_reg;
244 }
245
246 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
247 return resource;
248}
249
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300250static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600251{
252 struct resource *resource;
253
254 /* Initialize the io space constraints on the current bus */
255 resource = amdfam16_find_iopair(dev, nodeid, link);
256 if (resource) {
257 u32 align;
258 align = log2(HT_IO_HOST_ALIGN);
259 resource->base = 0;
260 resource->size = 0;
261 resource->align = align;
262 resource->gran = align;
263 resource->limit = 0xffffUL;
264 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
265 }
266
267 /* Initialize the prefetchable memory constraints on the current bus */
268 resource = amdfam16_find_mempair(dev, nodeid, link);
269 if (resource) {
270 resource->base = 0;
271 resource->size = 0;
272 resource->align = log2(HT_MEM_HOST_ALIGN);
273 resource->gran = log2(HT_MEM_HOST_ALIGN);
274 resource->limit = 0xffffffffffULL;
275 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
276 resource->flags |= IORESOURCE_BRIDGE;
277 }
278
279 /* Initialize the memory constraints on the current bus */
280 resource = amdfam16_find_mempair(dev, nodeid, link);
281 if (resource) {
282 resource->base = 0;
283 resource->size = 0;
284 resource->align = log2(HT_MEM_HOST_ALIGN);
285 resource->gran = log2(HT_MEM_HOST_ALIGN);
286 resource->limit = 0xffffffffffULL;
287 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
288 }
289
290}
291
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300292static void read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600293{
294 u32 nodeid;
295 struct bus *link;
296
297 nodeid = amdfam16_nodeid(dev);
298 for (link = dev->link_list; link; link = link->next) {
299 if (link->children) {
300 amdfam16_link_read_bases(dev, nodeid, link->link_num);
301 }
302 }
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300303
304 /*
305 * This MMCONF resource must be reserved in the PCI domain.
306 * It is not honored by the coreboot resource allocator if it is in
307 * the CPU_CLUSTER.
308 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200309 mmconf_resource(dev, MMIO_CONF_BASE);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600310}
311
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300312static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600313{
314 resource_t rbase, rend;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530315 unsigned int reg, link_num;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600316 char buf[50];
317
318 /* Make certain the resource has actually been set */
319 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
320 return;
321 }
322
323 /* If I have already stored this resource don't worry about it */
324 if (resource->flags & IORESOURCE_STORED) {
325 return;
326 }
327
328 /* Only handle PCI memory and IO resources */
329 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
330 return;
331
332 /* Ensure I am actually looking at a resource of function 1 */
333 if ((resource->index & 0xffff) < 0x1000) {
334 return;
335 }
336 /* Get the base address */
337 rbase = resource->base;
338
339 /* Get the limit (rounded up) */
340 rend = resource_end(resource);
341
342 /* Get the register and link */
343 reg = resource->index & 0xfff; // 4k
344 link_num = IOINDEX_LINK(resource->index);
345
346 if (resource->flags & IORESOURCE_IO) {
347 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
348 }
349 else if (resource->flags & IORESOURCE_MEM) {
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200350 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 -0600351 }
352 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200353 snprintf(buf, sizeof(buf), " <node %x link %x>",
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600354 nodeid, link_num);
355 report_resource_stored(dev, resource, buf);
356}
357
358/**
359 * I tried to reuse the resource allocation code in set_resource()
360 * but it is too difficult to deal with the resource allocation magic.
361 */
362
Subrata Banikb1434fc2019-03-15 22:20:41 +0530363static void create_vga_resource(struct device *dev, unsigned int nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600364{
365 struct bus *link;
366
367 /* find out which link the VGA card is connected,
368 * we only deal with the 'first' vga card */
369 for (link = dev->link_list; link; link = link->next) {
370 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800371#if CONFIG(MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300372 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600373 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
374 link->secondary,link->subordinate);
375 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600376 if ((vga_pri->bus->secondary >= link->secondary) &&
377 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600378#endif
379 break;
380 }
381 }
382
383 /* no VGA card installed */
384 if (link == NULL)
385 return;
386
387 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
388 set_vga_enable_reg(nodeid, sblink);
389}
390
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300391static void set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600392{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530393 unsigned int nodeid;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600394 struct bus *bus;
395 struct resource *res;
396
397 /* Find the nodeid */
398 nodeid = amdfam16_nodeid(dev);
399
400 create_vga_resource(dev, nodeid); //TODO: do we need this?
401
402 /* Set each resource we have found */
403 for (res = dev->resource_list; res; res = res->next) {
404 set_resource(dev, res, nodeid);
405 }
406
407 for (bus = dev->link_list; bus; bus = bus->next) {
408 if (bus->children) {
409 assign_resources(bus);
410 }
411 }
412}
413
414static void northbridge_init(struct device *dev)
415{
416}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200417
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100418static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200419{
420 void *addr, *current;
421
422 /* Skip the HEST header. */
423 current = (void *)(hest + 1);
424
425 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
426 if (addr != NULL)
427 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
428
429 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
430 if (addr != NULL)
431 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
432
433 return (unsigned long)current;
434}
435
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500436static void add_ivhd_dev_entry(struct device *parent, struct device *dev,
437 unsigned long *current, uint16_t *length,
438 uint8_t type, uint8_t data)
439{
440 uint8_t *p;
441 p = (uint8_t *) *current;
442
443 if (type == 0x2) {
444 /* Entry type */
445 p[0] = type;
446 /* Device */
447 p[1] = dev->path.pci.devfn;
448 /* Bus */
449 p[2] = dev->bus->secondary;
450 /* Data */
451 p[3] = data;
452 /* [4:7] Padding */
453 p[4] = 0x0;
454 p[5] = 0x0;
455 p[6] = 0x0;
456 p[7] = 0x0;
457 *length += 8;
458 *current += 8;
459 } else if (type == 0x42) {
460 /* Entry type */
461 p[0] = type;
462 /* Device */
463 p[1] = dev->path.pci.devfn;
464 /* Bus */
465 p[2] = dev->bus->secondary;
466 /* Data */
467 p[3] = 0x0;
468 /* Reserved */
469 p[4] = 0x0;
470 /* Device */
471 p[5] = parent->path.pci.devfn;
472 /* Bus */
473 p[6] = parent->bus->secondary;
474 /* Reserved */
475 p[7] = 0x0;
476 *length += 8;
477 *current += 8;
478 }
479}
480
481static void add_ivrs_device_entries(struct device *parent, struct device *dev,
482 unsigned int depth, int linknum, int8_t *root_level,
483 unsigned long *current, uint16_t *length)
484{
485 struct device *sibling;
486 struct bus *link;
487 unsigned int header_type;
488 unsigned int is_pcie;
489
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500490 if (dev->path.type == DEVICE_PATH_PCI) {
491
492 if ((dev->bus->secondary == 0x0) &&
493 (dev->path.pci.devfn == 0x0))
494 *root_level = depth;
495
496 if ((*root_level != -1) && (dev->enabled)) {
497 if (depth == *root_level) {
498 if (dev->path.pci.devfn == (0x14 << 3)) {
499 /* SMBUS controller */
500 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x97);
501 } else if (dev->path.pci.devfn != 0x2 &&
502 dev->path.pci.devfn < (0x2 << 3)) {
503 /* FCH control device */
504 } else {
505 /* Other devices */
506 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
507 }
508 } else {
509 header_type = dev->hdr_type & 0x7f;
510 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
511 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
512 (header_type == PCI_HEADER_TYPE_BRIDGE))
513 && is_pcie) {
514 /* Device or Bridge is PCIe */
515 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
516 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) &&
517 !is_pcie) {
518 add_ivhd_dev_entry(parent, dev, current, length, 0x42, 0x0);
519 /* Device is legacy PCI or PCI-X */
520 }
521 }
522 }
523 }
524
525 for (link = dev->link_list; link; link = link->next)
526 for (sibling = link->children; sibling; sibling =
527 sibling->sibling)
528 add_ivrs_device_entries(dev, sibling, depth + 1, depth,
529 root_level, current, length);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500530}
531
532unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
533{
534 uint8_t *p;
535
536 uint32_t apicid_sb800;
537 uint32_t apicid_northbridge;
538
539 apicid_sb800 = CONFIG_MAX_CPUS;
540 apicid_northbridge = CONFIG_MAX_CPUS + 1;
541
542 /* Describe NB IOAPIC */
543 p = (uint8_t *)current;
544 p[0] = 0x48; /* Entry type */
545 p[1] = 0; /* Device */
546 p[2] = 0; /* Bus */
547 p[3] = 0x0; /* Data */
548 p[4] = apicid_northbridge; /* IOAPIC ID */
549 p[5] = 0x0; /* Device 0 Function 0 */
550 p[6] = 0x0; /* Northbridge bus */
551 p[7] = 0x1; /* Variety */
552 current += 8;
553
554 /* Describe SB IOAPIC */
555 p = (uint8_t *)current;
556 p[0] = 0x48; /* Entry type */
557 p[1] = 0; /* Device */
558 p[2] = 0; /* Bus */
559 p[3] = 0xd7; /* Data */
560 p[4] = apicid_sb800; /* IOAPIC ID */
561 p[5] = 0x14 << 3; /* Device 0x14 Function 0 */
562 p[6] = 0x0; /* Southbridge bus */
563 p[7] = 0x1; /* Variety */
564 current += 8;
565
566 return current;
567}
568
569static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
570{
571 uint8_t *p;
Piotr Król063e1562018-07-22 20:52:26 +0200572 acpi_ivrs_t *ivrs_agesa;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500573
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300574 struct device *nb_dev = pcidev_on_root(0x0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500575 if (!nb_dev) {
576
577 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
578 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
579
580 return (unsigned long)ivrs;
581 }
582
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500583
Piotr Król063e1562018-07-22 20:52:26 +0200584 /* obtain IOMMU base address */
585 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
586 if (ivrs_agesa != NULL) {
587 ivrs->iv_info = 0x0;
588 /* Maximum supported virtual address size */
589 ivrs->iv_info |= (0x40 << 15);
590 /* Maximum supported physical address size */
591 ivrs->iv_info |= (0x30 << 8);
592 /* Guest virtual address width */
593 ivrs->iv_info |= (0x2 << 5);
594
595 ivrs->ivhd.type = 0x10;
596 ivrs->ivhd.flags = 0x0e;
597 /* Enable ATS support */
598 ivrs->ivhd.flags |= 0x10;
599 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
600 /* BDF <bus>:00.2 */
601 ivrs->ivhd.device_id = 0x2 | (nb_dev->bus->secondary << 8);
602 /* Capability block 0x40 (type 0xf, "Secure device") */
603 ivrs->ivhd.capability_offset = 0x40;
604 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
605 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
606 ivrs->ivhd.pci_segment_group = 0x0;
607 ivrs->ivhd.iommu_info = 0x0;
608 ivrs->ivhd.iommu_info |= (0x13 << 8);
609 /* use only performance counters related bits:
610 * PNCounters[16:13] and
611 * PNBanks[22:17],
612 * otherwise 0 */
613 ivrs->ivhd.iommu_feature_info =
614 ivrs_agesa->ivhd.iommu_feature_info & 0x7fe000;
615 } else {
616 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
617
618 return (unsigned long)ivrs;
619 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500620
621 /* Describe HPET */
622 p = (uint8_t *)current;
623 p[0] = 0x48; /* Entry type */
624 p[1] = 0; /* Device */
625 p[2] = 0; /* Bus */
626 p[3] = 0xd7; /* Data */
627 p[4] = 0x0; /* HPET number */
628 p[5] = 0x14 << 3; /* HPET device */
629 p[6] = nb_dev->bus->secondary; /* HPET bus */
630 p[7] = 0x2; /* Variety */
631 ivrs->ivhd.length += 8;
632 current += 8;
633
634 /* Describe PCI devices */
Jacob Garber293e6a92019-07-17 11:47:19 -0600635 int8_t root_level = -1;
636 add_ivrs_device_entries(NULL, all_devices, 0, -1, &root_level, &current,
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500637 &ivrs->ivhd.length);
638
639 /* Describe IOAPICs */
640 unsigned long prev_current = current;
641 current = acpi_fill_ivrs_ioapic(ivrs, current);
642 ivrs->ivhd.length += (current - prev_current);
643
644 return current;
645}
646
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300647static void northbridge_fill_ssdt_generator(struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200648{
649 msr_t msr;
650 char pscope[] = "\\_SB.PCI0";
651
652 acpigen_write_scope(pscope);
653 msr = rdmsr(TOP_MEM);
654 acpigen_write_name_dword("TOM1", msr.lo);
655 msr = rdmsr(TOP_MEM2);
656 /*
657 * Since XP only implements parts of ACPI 2.0, we can't use a qword
658 * here.
659 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
660 * slide 22ff.
661 * Shift value right by 20 bit to make it fit into 32bit,
662 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
663 */
664 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
665 acpigen_pop_len();
666}
667
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300668static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200669 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200670 acpi_rsdp_t *rsdp)
671{
672 acpi_srat_t *srat;
673 acpi_slit_t *slit;
674 acpi_header_t *ssdt;
675 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500676 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200677
678 /* HEST */
679 current = ALIGN(current, 8);
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100680 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200681 acpi_add_table(rsdp, (void *)current);
682 current += ((acpi_header_t *)current)->length;
683
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500684 /* IVRS */
685 current = ALIGN(current, 8);
686 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
687 ivrs = (acpi_ivrs_t *) current;
688 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
689 current += ivrs->header.length;
690 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200691
692 /* SRAT */
693 current = ALIGN(current, 8);
694 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
695 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
696 if (srat != NULL) {
697 memcpy((void *)current, srat, srat->header.length);
698 srat = (acpi_srat_t *) current;
699 current += srat->header.length;
700 acpi_add_table(rsdp, srat);
701 } else {
702 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
703 }
704
705 /* SLIT */
706 current = ALIGN(current, 8);
707 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
708 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
709 if (slit != NULL) {
710 memcpy((void *)current, slit, slit->header.length);
711 slit = (acpi_slit_t *) current;
712 current += slit->header.length;
713 acpi_add_table(rsdp, slit);
714 } else {
715 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
716 }
717
718 /* ALIB */
719 current = ALIGN(current, 16);
720 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
721 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
722 if (alib != NULL) {
723 memcpy((void *)current, alib, alib->length);
724 alib = (acpi_header_t *) current;
725 current += alib->length;
726 acpi_add_table(rsdp, (void *)alib);
727 }
728 else {
729 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
730 }
731
732 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
733 /* SSDT */
734 current = ALIGN(current, 16);
735 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
736 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
737 if (ssdt != NULL) {
738 memcpy((void *)current, ssdt, ssdt->length);
739 ssdt = (acpi_header_t *) current;
740 current += ssdt->length;
741 }
742 else {
743 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
744 }
745 acpi_add_table(rsdp,ssdt);
746
747 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
748 return current;
749}
750
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600751static struct device_operations northbridge_operations = {
752 .read_resources = read_resources,
753 .set_resources = set_resources,
754 .enable_resources = pci_dev_enable_resources,
755 .init = northbridge_init,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200756 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
757 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600758 .enable = 0,
759 .ops_pci = 0,
760};
761
762static const struct pci_driver family16_northbridge __pci_driver = {
763 .ops = &northbridge_operations,
764 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600765 .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600766};
767
768static const struct pci_driver family10_northbridge __pci_driver = {
769 .ops = &northbridge_operations,
770 .vendor = PCI_VENDOR_ID_AMD,
771 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
772};
773
Dave Frodin891f71a2015-01-19 15:58:24 -0700774static void fam16_finalize(void *chip_info)
775{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300776 struct device *dev;
Dave Frodin891f71a2015-01-19 15:58:24 -0700777 u32 value;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300778 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Dave Frodin891f71a2015-01-19 15:58:24 -0700779 pci_write_config32(dev, 0xF8, 0);
780 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
781
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200782 /*
783 * Currently it is impossible to enable ACS with AGESA by setting the
784 * correct bit for AmdInitMid phase. AGESA code path does not call the
785 * right function that enables these functionalities. Disabled ACS
786 * result in multiple PCIe devices to be assigned to the same IOMMU
787 * group. Without IOMMU group separation the devices cannot be passed
788 * through independently.
789 */
790
791 /* Select GPP link core IO Link Strap Control register 0xB0 */
792 pci_write_config32(dev, 0xE0, 0x014000B0);
793 value = pci_read_config32(dev, 0xE4);
794
795 /* Enable AER (bit 5) and ACS (bit 6 undocumented) */
796 value |= PCIE_CAP_AER | PCIE_CAP_ACS;
797 pci_write_config32(dev, 0xE4, value);
798
799 /* Select GPP link core Wrapper register 0x00 (undocumented) */
800 pci_write_config32(dev, 0xE0, 0x01300000);
801 value = pci_read_config32(dev, 0xE4);
802
803 /*
804 * Enable ACS capabilities straps including sub-items. From lspci it
805 * looks like these bits enable: Source Validation and Translation
806 * Blocking
807 */
808 value |= (BIT(24) | BIT(25) | BIT(26));
809 pci_write_config32(dev, 0xE4, value);
810
Dave Frodin891f71a2015-01-19 15:58:24 -0700811 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300812 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200813 if (dev != NULL) {
814 value = pci_read_config32(dev, 0x60);
815 value &= ~(1 << 11);
816 pci_write_config32(dev, 0x60, value);
817 }
Dave Frodin891f71a2015-01-19 15:58:24 -0700818}
819
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300820struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600821 CHIP_NAME("AMD FAM16 Northbridge")
822 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700823 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600824};
825
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300826static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600827{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530828 unsigned int reg;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600829
830 /* Find the already assigned resource pairs */
831 get_fx_devs();
832 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
833 u32 base, limit;
834 base = f1_read_config32(reg);
835 limit = f1_read_config32(reg + 0x04);
836 /* Is this register allocated? */
837 if ((base & 3) != 0) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530838 unsigned int nodeid, reg_link;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300839 struct device *reg_dev;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600840 if (reg < 0xc0) { // mmio
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600841 nodeid = (limit & 0xf) + (base&0x30);
842 } else { // io
843 nodeid = (limit & 0xf) + ((base>>4)&0x30);
844 }
845 reg_link = (limit >> 4) & 7;
846 reg_dev = __f0_dev[nodeid];
847 if (reg_dev) {
848 /* Reserve the resource */
849 struct resource *res;
850 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
851 if (res) {
852 res->flags = 1;
853 }
854 }
855 }
856 }
857 /* FIXME: do we need to check extend conf space?
858 I don't believe that much preset value */
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600859 pci_domain_read_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600860}
861
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300862static void domain_enable_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600863{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600864}
865
866#if CONFIG_HW_MEM_HOLE_SIZEK != 0
867struct hw_mem_hole_info {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530868 unsigned int hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600869 int node_id;
870};
871static struct hw_mem_hole_info get_hw_mem_hole_info(void)
872{
873 struct hw_mem_hole_info mem_hole;
874 int i;
875 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
876 mem_hole.node_id = -1;
877 for (i = 0; i < node_nums; i++) {
878 dram_base_mask_t d;
879 u32 hole;
880 d = get_dram_base_mask(i);
881 if (!(d.mask & 1)) continue; // no memory on this node
882 hole = pci_read_config32(__f1_dev[i], 0xf0);
883 if (hole & 2) { // we find the hole
884 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
885 mem_hole.node_id = i; // record the node No with hole
886 break; // only one hole
887 }
888 }
889
890 /* We need to double check if there is special set on base reg and limit reg
891 * are not continuous instead of hole, it will find out its hole_startk.
892 */
893 if (mem_hole.node_id == -1) {
894 resource_t limitk_pri = 0;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600895 for (i = 0; i < node_nums; i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600896 dram_base_mask_t d;
897 resource_t base_k, limit_k;
898 d = get_dram_base_mask(i);
899 if (!(d.base & 1)) continue;
900 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
901 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
902 if (limitk_pri != base_k) { // we find the hole
Elyes HAOUAS38a4f2a92020-01-07 19:53:36 +0100903 mem_hole.hole_startk = (unsigned int)limitk_pri; // must be below 4G
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600904 mem_hole.node_id = i;
905 break; //only one hole
906 }
907 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
908 limitk_pri = limit_k;
909 }
910 }
911 return mem_hole;
912}
913#endif
914
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300915static void domain_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600916{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600917 unsigned long mmio_basek;
918 u32 pci_tolm;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600919 int i, idx;
920 struct bus *link;
921#if CONFIG_HW_MEM_HOLE_SIZEK != 0
922 struct hw_mem_hole_info mem_hole;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600923#endif
924
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600925 pci_tolm = 0xffffffffUL;
926 for (link = dev->link_list; link; link = link->next) {
927 pci_tolm = find_pci_tolm(link);
928 }
929
930 // FIXME handle interleaved nodes. If you fix this here, please fix
931 // amdk8, too.
932 mmio_basek = pci_tolm >> 10;
933 /* Round mmio_basek to something the processor can support */
934 mmio_basek &= ~((1 << 6) -1);
935
936 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
937 // MMIO hole. If you fix this here, please fix amdk8, too.
938 /* Round the mmio hole to 64M */
939 mmio_basek &= ~((64*1024) - 1);
940
941#if CONFIG_HW_MEM_HOLE_SIZEK != 0
942 /* if the hw mem hole is already set in raminit stage, here we will compare
943 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
944 * use hole_basek as mmio_basek and we don't need to reset hole.
945 * otherwise We reset the hole to the mmio_basek
946 */
947
948 mem_hole = get_hw_mem_hole_info();
949
950 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
951 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
952 mmio_basek = mem_hole.hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600953 }
954#endif
955
956 idx = 0x10;
957 for (i = 0; i < node_nums; i++) {
958 dram_base_mask_t d;
959 resource_t basek, limitk, sizek; // 4 1T
960
961 d = get_dram_base_mask(i);
962
963 if (!(d.mask & 1)) continue;
964 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200965 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600966
967 sizek = limitk - basek;
968
969 /* see if we need a hole from 0xa0000 to 0xbffff */
970 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
971 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
972 idx += 0x10;
973 basek = (8*64)+(16*16);
974 sizek = limitk - ((8*64)+(16*16));
975
976 }
977
978 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
979
980 /* split the region to accommodate pci memory space */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600981 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600982 if (basek <= mmio_basek) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530983 unsigned int pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600984 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600985 if (pre_sizek > 0) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600986 ram_resource(dev, (idx | i), basek, pre_sizek);
987 idx += 0x10;
988 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600989 }
990 basek = mmio_basek;
991 }
992 if ((basek + sizek) <= 4*1024*1024) {
993 sizek = 0;
994 }
995 else {
996 uint64_t topmem2 = bsp_topmem2();
997 basek = 4*1024*1024;
998 sizek = topmem2/1024 - basek;
999 }
1000 }
1001
1002 ram_resource(dev, (idx | i), basek, sizek);
1003 idx += 0x10;
1004 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
1005 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001006 }
1007
Kyösti Mälkkie87564f2017-04-15 20:07:53 +03001008 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001009
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001010 for (link = dev->link_list; link; link = link->next) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001011 if (link->children) {
1012 assign_resources(link);
1013 }
1014 }
1015}
1016
Aaron Durbinaa090cb2017-09-13 16:01:52 -06001017static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001018{
1019 if (dev->path.type == DEVICE_PATH_DOMAIN)
1020 return "PCI0";
1021
1022 return NULL;
1023}
1024
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001025static struct device_operations pci_domain_ops = {
1026 .read_resources = domain_read_resources,
1027 .set_resources = domain_set_resources,
1028 .enable_resources = domain_enable_resources,
1029 .init = NULL,
1030 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001031 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001032};
1033
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001034static void sysconf_init(struct device *dev) // first node
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001035{
1036 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1037 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1038}
1039
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001040static void cpu_bus_scan(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001041{
1042 struct bus *cpu_bus;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001043 struct device *dev_mc;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001044 int i,j;
1045 int coreid_bits;
1046 int core_max = 0;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301047 unsigned int ApicIdCoreIdSize;
1048 unsigned int core_nums;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001049 int siblings = 0;
1050 unsigned int family;
1051 u32 modules = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001052 int ioapic_count = 0;
1053
Michał Żygowskie7192882019-11-23 19:02:19 +01001054 /* For binaryPI there is no multiprocessor configuration, the number of
1055 * modules will always be 1. */
1056 modules = 1;
1057 ioapic_count = CONFIG_NUM_OF_IOAPICS;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001058
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001059 dev_mc = pcidev_on_root(DEV_CDB, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001060 if (!dev_mc) {
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001061 printk(BIOS_ERR, "0:%02x.0 not found", DEV_CDB);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001062 die("");
1063 }
1064 sysconf_init(dev_mc);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001065
1066 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +03001067 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001068 core_max = 1 << (coreid_bits & 0x000F); //mnc
1069
1070 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1071 if (ApicIdCoreIdSize) {
1072 core_nums = (1 << ApicIdCoreIdSize) - 1;
1073 } else {
1074 core_nums = 3; //quad core
1075 }
1076
1077 /* Find which cpus are present */
1078 cpu_bus = dev->link_list;
1079 for (i = 0; i < node_nums; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001080 struct device *cdb_dev;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301081 unsigned int devn;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001082 struct bus *pbus;
1083
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001084 devn = DEV_CDB + i;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001085 pbus = dev_mc->bus;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001086
1087 /* Find the cpu's pci device */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001088 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001089 if (!cdb_dev) {
1090 /* If I am probing things in a weird order
1091 * ensure all of the cpu's pci devices are found.
1092 */
1093 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001094 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001095 cdb_dev = pci_probe_dev(NULL, pbus,
1096 PCI_DEVFN(devn, fn));
1097 }
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001098 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001099 } else {
1100 /* Ok, We need to set the links for that device.
1101 * otherwise the device under it will not be scanned
1102 */
Kyösti Mälkkic5163ed82015-02-04 13:25:37 +02001103
1104 add_more_links(cdb_dev, 4);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001105 }
1106
1107 family = cpuid_eax(1);
1108 family = (family >> 20) & 0xFF;
1109 if (family == 1) { //f10
1110 u32 dword;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001111 cdb_dev = pcidev_on_root(devn, 3);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001112 dword = pci_read_config32(cdb_dev, 0xe8);
1113 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1114 } else if (family == 7) {//f16
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001115 cdb_dev = pcidev_on_root(devn, 5);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001116 if (cdb_dev && cdb_dev->enabled) {
1117 siblings = pci_read_config32(cdb_dev, 0x84);
1118 siblings &= 0xFF;
1119 }
1120 } else {
1121 siblings = 0; //default one core
1122 }
1123 int enable_node = cdb_dev && cdb_dev->enabled;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001124 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 -06001125 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1126
Elyes HAOUASa8131602016-09-19 10:27:57 -06001127 for (j = 0; j <= siblings; j++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001128 u32 lapicid_start = 0;
1129
1130 /*
Elyes HAOUAS38a4f2a92020-01-07 19:53:36 +01001131 * APIC ID calculation is tightly coupled with AGESA v5 code.
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001132 * This calculation MUST match the assignment calculation done
1133 * in LocalApicInitializationAtEarly() function.
1134 * And reference GetLocalApicIdForCore()
1135 *
1136 * Apply apic enumeration rules
1137 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1138 * put the local-APICs at m..z
1139 *
1140 * This is needed because many IO-APIC devices only have 4 bits
1141 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001142 */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001143 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1144 lapicid_start = (ioapic_count - 1) / core_max;
1145 lapicid_start = (lapicid_start + 1) * core_max;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001146 printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001147 }
1148 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
Elyes HAOUASa8131602016-09-19 10:27:57 -06001149 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001150 i, j, apic_id);
1151
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001152 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001153 if (cpu)
1154 amd_cpu_topology(cpu, i, j);
1155 } //j
1156 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001157}
1158
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001159static void cpu_bus_init(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001160{
1161 initialize_cpus(dev->link_list);
1162}
1163
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001164static struct device_operations cpu_bus_ops = {
Kyösti Mälkki48f82a92016-12-02 16:02:30 +02001165 .read_resources = DEVICE_NOOP,
1166 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001167 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001168 .init = cpu_bus_init,
1169 .scan_bus = cpu_bus_scan,
1170};
1171
1172static void root_complex_enable_dev(struct device *dev)
1173{
1174 static int done = 0;
1175
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001176 if (!done) {
1177 setup_bsp_ramtop();
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001178 done = 1;
1179 }
1180
1181 /* Set the operations if it is a special bus type */
1182 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1183 dev->ops = &pci_domain_ops;
1184 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1185 dev->ops = &cpu_bus_ops;
1186 }
1187}
1188
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001189struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001190 CHIP_NAME("AMD FAM16 Root Complex")
1191 .enable_dev = root_complex_enable_dev,
1192};
1193
1194/*********************************************************************
1195 * Change the vendor / device IDs to match the generic VBIOS header. *
1196 *********************************************************************/
1197u32 map_oprom_vendev(u32 vendev)
1198{
1199 u32 new_vendev;
1200 new_vendev =
1201 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1202
1203 if (vendev != new_vendev)
1204 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1205
1206 return new_vendev;
1207}