blob: d324689955834357d08a201cbf48559bd7a9fc82 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06003
4#include <console/console.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02005#include <device/pci_ops.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -06006#include <arch/acpi.h>
Michał Żygowski208318c2020-03-20 15:54:27 +01007#include <arch/ioapic.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -06008#include <stdint.h>
9#include <device/device.h>
10#include <device/pci.h>
11#include <device/pci_ids.h>
12#include <device/hypertransport.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060013#include <string.h>
14#include <lib.h>
15#include <cpu/cpu.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060016#include <Porting.h>
17#include <AGESA.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060018#include <Topology.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020019#include <cpu/x86/lapic.h>
20#include <cpu/amd/msr.h>
21#include <cpu/amd/mtrr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020022#include <arch/acpigen.h>
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020023#include <northbridge/amd/pi/nb_common.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030024#include <northbridge/amd/agesa/agesa_helper.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060025
Kyösti Mälkki113f6702018-05-20 20:12:32 +030026#define MAX_NODE_NUMS MAX_NODES
Michał Żygowski6ca5b472019-09-10 15:10:22 +020027#define PCIE_CAP_AER BIT(5)
28#define PCIE_CAP_ACS BIT(6)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060029
Bruce Griffith27ed80b2014-08-15 11:46:25 -060030typedef struct dram_base_mask {
31 u32 base; //[47:27] at [28:8]
32 u32 mask; //[47:27] at [28:8] and enable at bit 0
33} dram_base_mask_t;
34
Subrata Banikb1434fc2019-03-15 22:20:41 +053035static unsigned int node_nums;
36static unsigned int sblink;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030037static struct device *__f0_dev[MAX_NODE_NUMS];
38static struct device *__f1_dev[MAX_NODE_NUMS];
39static struct device *__f2_dev[MAX_NODE_NUMS];
40static struct device *__f4_dev[MAX_NODE_NUMS];
Subrata Banikb1434fc2019-03-15 22:20:41 +053041static unsigned int fx_devs = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060042
43static dram_base_mask_t get_dram_base_mask(u32 nodeid)
44{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030045 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060046 dram_base_mask_t d;
47 dev = __f1_dev[0];
48 u32 temp;
49 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
50 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
51 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
52 d.mask |= temp<<21;
53 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
54 d.mask |= (temp & 1); // enable bit
55 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
56 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
57 d.base |= temp<<21;
58 return d;
59}
60
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030061static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Bruce Griffith27ed80b2014-08-15 11:46:25 -060062 u32 io_min, u32 io_max)
63{
64 u32 i;
65 u32 tempreg;
66 /* io range allocation */
67 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060068 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060069 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUASa8131602016-09-19 10:27:57 -060070 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUASa8131602016-09-19 10:27:57 -060071 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060072 pci_write_config32(__f1_dev[i], reg, tempreg);
73}
74
75static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
76{
77 u32 i;
78 u32 tempreg;
79 /* io range allocation */
80 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060081 for (i = 0; i < nodes; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060082 pci_write_config32(__f1_dev[i], reg+4, tempreg);
83 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
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
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030088static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060089{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020090 return pcidev_on_root(DEV_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -060091}
92
93static void get_fx_devs(void)
94{
95 int i;
96 for (i = 0; i < MAX_NODE_NUMS; i++) {
97 __f0_dev[i] = get_node_pci(i, 0);
98 __f1_dev[i] = get_node_pci(i, 1);
99 __f2_dev[i] = get_node_pci(i, 2);
100 __f4_dev[i] = get_node_pci(i, 4);
101 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
102 fx_devs = i+1;
103 }
104 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
105 die("Cannot find 0:0x18.[0|1]\n");
106 }
Elyes HAOUASa8131602016-09-19 10:27:57 -0600107 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600108}
109
Subrata Banikb1434fc2019-03-15 22:20:41 +0530110static u32 f1_read_config32(unsigned int reg)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600111{
112 if (fx_devs == 0)
113 get_fx_devs();
114 return pci_read_config32(__f1_dev[0], reg);
115}
116
Subrata Banikb1434fc2019-03-15 22:20:41 +0530117static void f1_write_config32(unsigned int reg, u32 value)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600118{
119 int i;
120 if (fx_devs == 0)
121 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200122 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300123 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600124 dev = __f1_dev[i];
125 if (dev && dev->enabled) {
126 pci_write_config32(dev, reg, value);
127 }
128 }
129}
130
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300131static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600132{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200133 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600134}
135
136static void set_vga_enable_reg(u32 nodeid, u32 linkn)
137{
138 u32 val;
139
140 val = 1 | (nodeid<<4) | (linkn<<12);
141 /* it will routing
142 * (1)mmio 0xa0000:0xbffff
143 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
144 */
145 f1_write_config32(0xf4, val);
146
147}
148
149/**
150 * @return
Elyes HAOUAS99b075a2019-12-30 14:29:31 +0100151 * @retval 2 resource does not exist, usable
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600152 * @retval 0 resource exists, not usable
153 * @retval 1 resource exist, resource has been allocated before
154 */
Subrata Banikb1434fc2019-03-15 22:20:41 +0530155static int reg_useable(unsigned int reg, struct device *goal_dev,
156 unsigned int goal_nodeid, unsigned int goal_link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600157{
158 struct resource *res;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530159 unsigned int nodeid, link = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600160 int result;
161 res = 0;
162 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300163 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600164 dev = __f0_dev[nodeid];
165 if (!dev)
166 continue;
167 for (link = 0; !res && (link < 8); link++) {
168 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
169 }
170 }
171 result = 2;
172 if (res) {
173 result = 0;
174 if ((goal_link == (link - 1)) &&
175 (goal_nodeid == (nodeid - 1)) &&
176 (res->flags <= 1)) {
177 result = 1;
178 }
179 }
180 return result;
181}
182
Subrata Banikb1434fc2019-03-15 22:20:41 +0530183static struct resource *amdfam16_find_iopair(struct device *dev,
184 unsigned int nodeid, unsigned int link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600185{
186 struct resource *resource;
187 u32 free_reg, reg;
188 resource = 0;
189 free_reg = 0;
190 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
191 int result;
192 result = reg_useable(reg, dev, nodeid, link);
193 if (result == 1) {
194 /* I have been allocated this one */
195 break;
196 }
197 else if (result > 1) {
198 /* I have a free register pair */
199 free_reg = reg;
200 }
201 }
202 if (reg > 0xd8) {
203 reg = free_reg; // if no free, the free_reg still be 0
204 }
205
206 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
207
208 return resource;
209}
210
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300211static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600212{
213 struct resource *resource;
214 u32 free_reg, reg;
215 resource = 0;
216 free_reg = 0;
217 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
218 int result;
219 result = reg_useable(reg, dev, nodeid, link);
220 if (result == 1) {
221 /* I have been allocated this one */
222 break;
223 }
224 else if (result > 1) {
225 /* I have a free register pair */
226 free_reg = reg;
227 }
228 }
229 if (reg > 0xb8) {
230 reg = free_reg;
231 }
232
233 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
234 return resource;
235}
236
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300237static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600238{
239 struct resource *resource;
240
241 /* Initialize the io space constraints on the current bus */
242 resource = amdfam16_find_iopair(dev, nodeid, link);
243 if (resource) {
244 u32 align;
245 align = log2(HT_IO_HOST_ALIGN);
246 resource->base = 0;
247 resource->size = 0;
248 resource->align = align;
249 resource->gran = align;
250 resource->limit = 0xffffUL;
251 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
252 }
253
254 /* Initialize the prefetchable memory constraints on the current bus */
255 resource = amdfam16_find_mempair(dev, nodeid, link);
256 if (resource) {
257 resource->base = 0;
258 resource->size = 0;
259 resource->align = log2(HT_MEM_HOST_ALIGN);
260 resource->gran = log2(HT_MEM_HOST_ALIGN);
261 resource->limit = 0xffffffffffULL;
262 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
263 resource->flags |= IORESOURCE_BRIDGE;
264 }
265
266 /* Initialize the memory constraints on the current bus */
267 resource = amdfam16_find_mempair(dev, nodeid, link);
268 if (resource) {
269 resource->base = 0;
270 resource->size = 0;
271 resource->align = log2(HT_MEM_HOST_ALIGN);
272 resource->gran = log2(HT_MEM_HOST_ALIGN);
273 resource->limit = 0xffffffffffULL;
274 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
275 }
276
277}
278
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300279static void read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600280{
281 u32 nodeid;
282 struct bus *link;
Michał Żygowski208318c2020-03-20 15:54:27 +0100283 struct resource *res;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600284
285 nodeid = amdfam16_nodeid(dev);
286 for (link = dev->link_list; link; link = link->next) {
287 if (link->children) {
288 amdfam16_link_read_bases(dev, nodeid, link->link_num);
289 }
290 }
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300291
292 /*
293 * This MMCONF resource must be reserved in the PCI domain.
294 * It is not honored by the coreboot resource allocator if it is in
295 * the CPU_CLUSTER.
296 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200297 mmconf_resource(dev, MMIO_CONF_BASE);
Michał Żygowski208318c2020-03-20 15:54:27 +0100298
299 /* NB IOAPIC2 resource */
300 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
301 res->base = IO_APIC2_ADDR;
302 res->size = 0x00001000;
303 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600304}
305
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300306static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600307{
308 resource_t rbase, rend;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530309 unsigned int reg, link_num;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600310 char buf[50];
311
312 /* Make certain the resource has actually been set */
313 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
314 return;
315 }
316
317 /* If I have already stored this resource don't worry about it */
318 if (resource->flags & IORESOURCE_STORED) {
319 return;
320 }
321
322 /* Only handle PCI memory and IO resources */
323 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
324 return;
325
326 /* Ensure I am actually looking at a resource of function 1 */
327 if ((resource->index & 0xffff) < 0x1000) {
328 return;
329 }
330 /* Get the base address */
331 rbase = resource->base;
332
333 /* Get the limit (rounded up) */
334 rend = resource_end(resource);
335
336 /* Get the register and link */
337 reg = resource->index & 0xfff; // 4k
338 link_num = IOINDEX_LINK(resource->index);
339
340 if (resource->flags & IORESOURCE_IO) {
341 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
342 }
343 else if (resource->flags & IORESOURCE_MEM) {
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200344 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 -0600345 }
346 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200347 snprintf(buf, sizeof(buf), " <node %x link %x>",
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600348 nodeid, link_num);
349 report_resource_stored(dev, resource, buf);
350}
351
352/**
353 * I tried to reuse the resource allocation code in set_resource()
354 * but it is too difficult to deal with the resource allocation magic.
355 */
356
Subrata Banikb1434fc2019-03-15 22:20:41 +0530357static void create_vga_resource(struct device *dev, unsigned int nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600358{
359 struct bus *link;
360
361 /* find out which link the VGA card is connected,
362 * we only deal with the 'first' vga card */
363 for (link = dev->link_list; link; link = link->next) {
364 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800365#if CONFIG(MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300366 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600367 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
368 link->secondary,link->subordinate);
369 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600370 if ((vga_pri->bus->secondary >= link->secondary) &&
371 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600372#endif
373 break;
374 }
375 }
376
377 /* no VGA card installed */
378 if (link == NULL)
379 return;
380
381 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
382 set_vga_enable_reg(nodeid, sblink);
383}
384
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300385static void set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600386{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530387 unsigned int nodeid;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600388 struct bus *bus;
389 struct resource *res;
390
391 /* Find the nodeid */
392 nodeid = amdfam16_nodeid(dev);
393
394 create_vga_resource(dev, nodeid); //TODO: do we need this?
395
396 /* Set each resource we have found */
397 for (res = dev->resource_list; res; res = res->next) {
398 set_resource(dev, res, nodeid);
399 }
400
401 for (bus = dev->link_list; bus; bus = bus->next) {
402 if (bus->children) {
403 assign_resources(bus);
404 }
405 }
406}
407
408static void northbridge_init(struct device *dev)
409{
Michał Żygowski208318c2020-03-20 15:54:27 +0100410 setup_ioapic((u8 *)IO_APIC2_ADDR, CONFIG_MAX_CPUS+1);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600411}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200412
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100413static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200414{
415 void *addr, *current;
416
417 /* Skip the HEST header. */
418 current = (void *)(hest + 1);
419
420 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
421 if (addr != NULL)
422 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
423
424 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
425 if (addr != NULL)
426 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
427
428 return (unsigned long)current;
429}
430
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500431static void add_ivhd_dev_entry(struct device *parent, struct device *dev,
432 unsigned long *current, uint16_t *length,
433 uint8_t type, uint8_t data)
434{
435 uint8_t *p;
436 p = (uint8_t *) *current;
437
438 if (type == 0x2) {
439 /* Entry type */
440 p[0] = type;
441 /* Device */
442 p[1] = dev->path.pci.devfn;
443 /* Bus */
444 p[2] = dev->bus->secondary;
445 /* Data */
446 p[3] = data;
447 /* [4:7] Padding */
448 p[4] = 0x0;
449 p[5] = 0x0;
450 p[6] = 0x0;
451 p[7] = 0x0;
452 *length += 8;
453 *current += 8;
454 } else if (type == 0x42) {
455 /* Entry type */
456 p[0] = type;
457 /* Device */
458 p[1] = dev->path.pci.devfn;
459 /* Bus */
460 p[2] = dev->bus->secondary;
461 /* Data */
462 p[3] = 0x0;
463 /* Reserved */
464 p[4] = 0x0;
465 /* Device */
466 p[5] = parent->path.pci.devfn;
467 /* Bus */
468 p[6] = parent->bus->secondary;
469 /* Reserved */
470 p[7] = 0x0;
471 *length += 8;
472 *current += 8;
473 }
474}
475
476static void add_ivrs_device_entries(struct device *parent, struct device *dev,
477 unsigned int depth, int linknum, int8_t *root_level,
478 unsigned long *current, uint16_t *length)
479{
480 struct device *sibling;
481 struct bus *link;
482 unsigned int header_type;
483 unsigned int is_pcie;
484
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500485 if (dev->path.type == DEVICE_PATH_PCI) {
486
487 if ((dev->bus->secondary == 0x0) &&
488 (dev->path.pci.devfn == 0x0))
489 *root_level = depth;
490
491 if ((*root_level != -1) && (dev->enabled)) {
492 if (depth == *root_level) {
493 if (dev->path.pci.devfn == (0x14 << 3)) {
494 /* SMBUS controller */
495 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x97);
496 } else if (dev->path.pci.devfn != 0x2 &&
497 dev->path.pci.devfn < (0x2 << 3)) {
498 /* FCH control device */
499 } else {
500 /* Other devices */
501 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
502 }
503 } else {
504 header_type = dev->hdr_type & 0x7f;
505 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
506 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
507 (header_type == PCI_HEADER_TYPE_BRIDGE))
508 && is_pcie) {
509 /* Device or Bridge is PCIe */
510 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
511 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) &&
512 !is_pcie) {
513 add_ivhd_dev_entry(parent, dev, current, length, 0x42, 0x0);
514 /* Device is legacy PCI or PCI-X */
515 }
516 }
517 }
518 }
519
520 for (link = dev->link_list; link; link = link->next)
521 for (sibling = link->children; sibling; sibling =
522 sibling->sibling)
523 add_ivrs_device_entries(dev, sibling, depth + 1, depth,
524 root_level, current, length);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500525}
526
527unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
528{
529 uint8_t *p;
530
531 uint32_t apicid_sb800;
532 uint32_t apicid_northbridge;
533
534 apicid_sb800 = CONFIG_MAX_CPUS;
535 apicid_northbridge = CONFIG_MAX_CPUS + 1;
536
537 /* Describe NB IOAPIC */
538 p = (uint8_t *)current;
539 p[0] = 0x48; /* Entry type */
540 p[1] = 0; /* Device */
541 p[2] = 0; /* Bus */
542 p[3] = 0x0; /* Data */
543 p[4] = apicid_northbridge; /* IOAPIC ID */
544 p[5] = 0x0; /* Device 0 Function 0 */
545 p[6] = 0x0; /* Northbridge bus */
546 p[7] = 0x1; /* Variety */
547 current += 8;
548
549 /* Describe SB IOAPIC */
550 p = (uint8_t *)current;
551 p[0] = 0x48; /* Entry type */
552 p[1] = 0; /* Device */
553 p[2] = 0; /* Bus */
554 p[3] = 0xd7; /* Data */
555 p[4] = apicid_sb800; /* IOAPIC ID */
556 p[5] = 0x14 << 3; /* Device 0x14 Function 0 */
557 p[6] = 0x0; /* Southbridge bus */
558 p[7] = 0x1; /* Variety */
559 current += 8;
560
561 return current;
562}
563
564static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
565{
566 uint8_t *p;
Piotr Król063e1562018-07-22 20:52:26 +0200567 acpi_ivrs_t *ivrs_agesa;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500568
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300569 struct device *nb_dev = pcidev_on_root(0x0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500570 if (!nb_dev) {
571
572 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
573 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
574
575 return (unsigned long)ivrs;
576 }
577
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500578
Piotr Król063e1562018-07-22 20:52:26 +0200579 /* obtain IOMMU base address */
580 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
581 if (ivrs_agesa != NULL) {
582 ivrs->iv_info = 0x0;
583 /* Maximum supported virtual address size */
584 ivrs->iv_info |= (0x40 << 15);
585 /* Maximum supported physical address size */
586 ivrs->iv_info |= (0x30 << 8);
587 /* Guest virtual address width */
588 ivrs->iv_info |= (0x2 << 5);
589
590 ivrs->ivhd.type = 0x10;
591 ivrs->ivhd.flags = 0x0e;
592 /* Enable ATS support */
593 ivrs->ivhd.flags |= 0x10;
594 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
595 /* BDF <bus>:00.2 */
596 ivrs->ivhd.device_id = 0x2 | (nb_dev->bus->secondary << 8);
597 /* Capability block 0x40 (type 0xf, "Secure device") */
598 ivrs->ivhd.capability_offset = 0x40;
599 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
600 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
601 ivrs->ivhd.pci_segment_group = 0x0;
602 ivrs->ivhd.iommu_info = 0x0;
603 ivrs->ivhd.iommu_info |= (0x13 << 8);
604 /* use only performance counters related bits:
605 * PNCounters[16:13] and
606 * PNBanks[22:17],
607 * otherwise 0 */
608 ivrs->ivhd.iommu_feature_info =
609 ivrs_agesa->ivhd.iommu_feature_info & 0x7fe000;
610 } else {
611 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
612
613 return (unsigned long)ivrs;
614 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500615
616 /* Describe HPET */
617 p = (uint8_t *)current;
618 p[0] = 0x48; /* Entry type */
619 p[1] = 0; /* Device */
620 p[2] = 0; /* Bus */
621 p[3] = 0xd7; /* Data */
622 p[4] = 0x0; /* HPET number */
623 p[5] = 0x14 << 3; /* HPET device */
624 p[6] = nb_dev->bus->secondary; /* HPET bus */
625 p[7] = 0x2; /* Variety */
626 ivrs->ivhd.length += 8;
627 current += 8;
628
629 /* Describe PCI devices */
Jacob Garber293e6a92019-07-17 11:47:19 -0600630 int8_t root_level = -1;
631 add_ivrs_device_entries(NULL, all_devices, 0, -1, &root_level, &current,
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500632 &ivrs->ivhd.length);
633
634 /* Describe IOAPICs */
635 unsigned long prev_current = current;
636 current = acpi_fill_ivrs_ioapic(ivrs, current);
637 ivrs->ivhd.length += (current - prev_current);
638
639 return current;
640}
641
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300642static void northbridge_fill_ssdt_generator(struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200643{
644 msr_t msr;
645 char pscope[] = "\\_SB.PCI0";
646
647 acpigen_write_scope(pscope);
648 msr = rdmsr(TOP_MEM);
649 acpigen_write_name_dword("TOM1", msr.lo);
650 msr = rdmsr(TOP_MEM2);
651 /*
652 * Since XP only implements parts of ACPI 2.0, we can't use a qword
653 * here.
654 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
655 * slide 22ff.
656 * Shift value right by 20 bit to make it fit into 32bit,
657 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
658 */
659 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
660 acpigen_pop_len();
661}
662
Michał Żygowski9550e972020-03-20 13:56:46 +0100663static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
664{
665 unsigned int len = ssdt->length - sizeof(acpi_header_t);
666 unsigned int i;
667
668 for (i = sizeof(acpi_header_t); i < len; i++) {
669 /* Search for _PR_ scope and replace it with _SB_ */
670 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
671 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
672 }
673 /* Recalculate checksum */
674 ssdt->checksum = 0;
675 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
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
688 /* HEST */
689 current = ALIGN(current, 8);
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100690 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200691 acpi_add_table(rsdp, (void *)current);
692 current += ((acpi_header_t *)current)->length;
693
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500694 /* IVRS */
695 current = ALIGN(current, 8);
696 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
697 ivrs = (acpi_ivrs_t *) current;
698 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
699 current += ivrs->header.length;
700 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200701
702 /* SRAT */
703 current = ALIGN(current, 8);
704 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
705 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
706 if (srat != NULL) {
707 memcpy((void *)current, srat, srat->header.length);
708 srat = (acpi_srat_t *) current;
709 current += srat->header.length;
710 acpi_add_table(rsdp, srat);
711 } else {
712 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
713 }
714
715 /* SLIT */
716 current = ALIGN(current, 8);
717 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
718 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
719 if (slit != NULL) {
720 memcpy((void *)current, slit, slit->header.length);
721 slit = (acpi_slit_t *) current;
722 current += slit->header.length;
723 acpi_add_table(rsdp, slit);
724 } else {
725 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
726 }
727
728 /* ALIB */
729 current = ALIGN(current, 16);
730 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
731 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
732 if (alib != NULL) {
733 memcpy((void *)current, alib, alib->length);
734 alib = (acpi_header_t *) current;
735 current += alib->length;
736 acpi_add_table(rsdp, (void *)alib);
737 }
738 else {
739 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
740 }
741
742 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
743 /* SSDT */
744 current = ALIGN(current, 16);
745 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
746 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
747 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100748 patch_ssdt_processor_scope(ssdt);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200749 memcpy((void *)current, ssdt, ssdt->length);
750 ssdt = (acpi_header_t *) current;
751 current += ssdt->length;
752 }
753 else {
754 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
755 }
756 acpi_add_table(rsdp,ssdt);
757
758 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
759 return current;
760}
761
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600762static struct device_operations northbridge_operations = {
763 .read_resources = read_resources,
764 .set_resources = set_resources,
765 .enable_resources = pci_dev_enable_resources,
766 .init = northbridge_init,
Nico Huber68680dd2020-03-31 17:34:52 +0200767 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200768 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600769};
770
771static const struct pci_driver family16_northbridge __pci_driver = {
772 .ops = &northbridge_operations,
773 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600774 .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600775};
776
777static const struct pci_driver family10_northbridge __pci_driver = {
778 .ops = &northbridge_operations,
779 .vendor = PCI_VENDOR_ID_AMD,
780 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
781};
782
Dave Frodin891f71a2015-01-19 15:58:24 -0700783static void fam16_finalize(void *chip_info)
784{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300785 struct device *dev;
Dave Frodin891f71a2015-01-19 15:58:24 -0700786 u32 value;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300787 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Dave Frodin891f71a2015-01-19 15:58:24 -0700788 pci_write_config32(dev, 0xF8, 0);
789 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
790
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200791 /*
792 * Currently it is impossible to enable ACS with AGESA by setting the
793 * correct bit for AmdInitMid phase. AGESA code path does not call the
794 * right function that enables these functionalities. Disabled ACS
795 * result in multiple PCIe devices to be assigned to the same IOMMU
796 * group. Without IOMMU group separation the devices cannot be passed
797 * through independently.
798 */
799
800 /* Select GPP link core IO Link Strap Control register 0xB0 */
801 pci_write_config32(dev, 0xE0, 0x014000B0);
802 value = pci_read_config32(dev, 0xE4);
803
804 /* Enable AER (bit 5) and ACS (bit 6 undocumented) */
805 value |= PCIE_CAP_AER | PCIE_CAP_ACS;
806 pci_write_config32(dev, 0xE4, value);
807
808 /* Select GPP link core Wrapper register 0x00 (undocumented) */
809 pci_write_config32(dev, 0xE0, 0x01300000);
810 value = pci_read_config32(dev, 0xE4);
811
812 /*
813 * Enable ACS capabilities straps including sub-items. From lspci it
814 * looks like these bits enable: Source Validation and Translation
815 * Blocking
816 */
817 value |= (BIT(24) | BIT(25) | BIT(26));
818 pci_write_config32(dev, 0xE4, value);
819
Dave Frodin891f71a2015-01-19 15:58:24 -0700820 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300821 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200822 if (dev != NULL) {
823 value = pci_read_config32(dev, 0x60);
824 value &= ~(1 << 11);
825 pci_write_config32(dev, 0x60, value);
826 }
Dave Frodin891f71a2015-01-19 15:58:24 -0700827}
828
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300829struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600830 CHIP_NAME("AMD FAM16 Northbridge")
831 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700832 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600833};
834
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300835static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600836{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530837 unsigned int reg;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600838
839 /* Find the already assigned resource pairs */
840 get_fx_devs();
841 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
842 u32 base, limit;
843 base = f1_read_config32(reg);
844 limit = f1_read_config32(reg + 0x04);
845 /* Is this register allocated? */
846 if ((base & 3) != 0) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530847 unsigned int nodeid, reg_link;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300848 struct device *reg_dev;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600849 if (reg < 0xc0) { // mmio
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600850 nodeid = (limit & 0xf) + (base&0x30);
851 } else { // io
852 nodeid = (limit & 0xf) + ((base>>4)&0x30);
853 }
854 reg_link = (limit >> 4) & 7;
855 reg_dev = __f0_dev[nodeid];
856 if (reg_dev) {
857 /* Reserve the resource */
858 struct resource *res;
859 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
860 if (res) {
861 res->flags = 1;
862 }
863 }
864 }
865 }
866 /* FIXME: do we need to check extend conf space?
867 I don't believe that much preset value */
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600868 pci_domain_read_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600869}
870
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300871static void domain_enable_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600872{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600873}
874
875#if CONFIG_HW_MEM_HOLE_SIZEK != 0
876struct hw_mem_hole_info {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530877 unsigned int hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600878 int node_id;
879};
880static struct hw_mem_hole_info get_hw_mem_hole_info(void)
881{
882 struct hw_mem_hole_info mem_hole;
883 int i;
884 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
885 mem_hole.node_id = -1;
886 for (i = 0; i < node_nums; i++) {
887 dram_base_mask_t d;
888 u32 hole;
889 d = get_dram_base_mask(i);
890 if (!(d.mask & 1)) continue; // no memory on this node
891 hole = pci_read_config32(__f1_dev[i], 0xf0);
892 if (hole & 2) { // we find the hole
893 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
894 mem_hole.node_id = i; // record the node No with hole
895 break; // only one hole
896 }
897 }
898
899 /* We need to double check if there is special set on base reg and limit reg
900 * are not continuous instead of hole, it will find out its hole_startk.
901 */
902 if (mem_hole.node_id == -1) {
903 resource_t limitk_pri = 0;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600904 for (i = 0; i < node_nums; i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600905 dram_base_mask_t d;
906 resource_t base_k, limit_k;
907 d = get_dram_base_mask(i);
908 if (!(d.base & 1)) continue;
909 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
910 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
911 if (limitk_pri != base_k) { // we find the hole
Elyes HAOUAS38a4f2a92020-01-07 19:53:36 +0100912 mem_hole.hole_startk = (unsigned int)limitk_pri; // must be below 4G
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600913 mem_hole.node_id = i;
914 break; //only one hole
915 }
916 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
917 limitk_pri = limit_k;
918 }
919 }
920 return mem_hole;
921}
922#endif
923
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300924static void domain_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600925{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600926 unsigned long mmio_basek;
927 u32 pci_tolm;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600928 int i, idx;
929 struct bus *link;
930#if CONFIG_HW_MEM_HOLE_SIZEK != 0
931 struct hw_mem_hole_info mem_hole;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600932#endif
933
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600934 pci_tolm = 0xffffffffUL;
935 for (link = dev->link_list; link; link = link->next) {
936 pci_tolm = find_pci_tolm(link);
937 }
938
939 // FIXME handle interleaved nodes. If you fix this here, please fix
940 // amdk8, too.
941 mmio_basek = pci_tolm >> 10;
942 /* Round mmio_basek to something the processor can support */
943 mmio_basek &= ~((1 << 6) -1);
944
945 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
946 // MMIO hole. If you fix this here, please fix amdk8, too.
947 /* Round the mmio hole to 64M */
948 mmio_basek &= ~((64*1024) - 1);
949
950#if CONFIG_HW_MEM_HOLE_SIZEK != 0
951 /* if the hw mem hole is already set in raminit stage, here we will compare
952 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
953 * use hole_basek as mmio_basek and we don't need to reset hole.
954 * otherwise We reset the hole to the mmio_basek
955 */
956
957 mem_hole = get_hw_mem_hole_info();
958
959 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
960 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
961 mmio_basek = mem_hole.hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600962 }
963#endif
964
965 idx = 0x10;
966 for (i = 0; i < node_nums; i++) {
967 dram_base_mask_t d;
968 resource_t basek, limitk, sizek; // 4 1T
969
970 d = get_dram_base_mask(i);
971
972 if (!(d.mask & 1)) continue;
973 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200974 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600975
976 sizek = limitk - basek;
977
978 /* see if we need a hole from 0xa0000 to 0xbffff */
979 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
980 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
981 idx += 0x10;
982 basek = (8*64)+(16*16);
983 sizek = limitk - ((8*64)+(16*16));
984
985 }
986
987 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
988
989 /* split the region to accommodate pci memory space */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600990 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600991 if (basek <= mmio_basek) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530992 unsigned int pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600993 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600994 if (pre_sizek > 0) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600995 ram_resource(dev, (idx | i), basek, pre_sizek);
996 idx += 0x10;
997 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600998 }
999 basek = mmio_basek;
1000 }
1001 if ((basek + sizek) <= 4*1024*1024) {
1002 sizek = 0;
1003 }
1004 else {
1005 uint64_t topmem2 = bsp_topmem2();
1006 basek = 4*1024*1024;
1007 sizek = topmem2/1024 - basek;
1008 }
1009 }
1010
1011 ram_resource(dev, (idx | i), basek, sizek);
1012 idx += 0x10;
1013 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
1014 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001015 }
1016
Kyösti Mälkkie87564f2017-04-15 20:07:53 +03001017 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001018
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001019 for (link = dev->link_list; link; link = link->next) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001020 if (link->children) {
1021 assign_resources(link);
1022 }
1023 }
1024}
1025
Aaron Durbinaa090cb2017-09-13 16:01:52 -06001026static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001027{
1028 if (dev->path.type == DEVICE_PATH_DOMAIN)
1029 return "PCI0";
1030
1031 return NULL;
1032}
1033
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001034static struct device_operations pci_domain_ops = {
1035 .read_resources = domain_read_resources,
1036 .set_resources = domain_set_resources,
1037 .enable_resources = domain_enable_resources,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001038 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001039 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001040};
1041
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001042static void sysconf_init(struct device *dev) // first node
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001043{
1044 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1045 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1046}
1047
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001048static void cpu_bus_scan(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001049{
1050 struct bus *cpu_bus;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001051 struct device *dev_mc;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001052 int i,j;
1053 int coreid_bits;
1054 int core_max = 0;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301055 unsigned int ApicIdCoreIdSize;
1056 unsigned int core_nums;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001057 int siblings = 0;
1058 unsigned int family;
1059 u32 modules = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001060 int ioapic_count = 0;
1061
Michał Żygowskie7192882019-11-23 19:02:19 +01001062 /* For binaryPI there is no multiprocessor configuration, the number of
1063 * modules will always be 1. */
1064 modules = 1;
1065 ioapic_count = CONFIG_NUM_OF_IOAPICS;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001066
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001067 dev_mc = pcidev_on_root(DEV_CDB, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001068 if (!dev_mc) {
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001069 printk(BIOS_ERR, "0:%02x.0 not found", DEV_CDB);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001070 die("");
1071 }
1072 sysconf_init(dev_mc);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001073
1074 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +03001075 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001076 core_max = 1 << (coreid_bits & 0x000F); //mnc
1077
1078 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1079 if (ApicIdCoreIdSize) {
1080 core_nums = (1 << ApicIdCoreIdSize) - 1;
1081 } else {
1082 core_nums = 3; //quad core
1083 }
1084
1085 /* Find which cpus are present */
1086 cpu_bus = dev->link_list;
1087 for (i = 0; i < node_nums; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001088 struct device *cdb_dev;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301089 unsigned int devn;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001090 struct bus *pbus;
1091
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001092 devn = DEV_CDB + i;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001093 pbus = dev_mc->bus;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001094
1095 /* Find the cpu's pci device */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001096 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001097 if (!cdb_dev) {
1098 /* If I am probing things in a weird order
1099 * ensure all of the cpu's pci devices are found.
1100 */
1101 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001102 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001103 cdb_dev = pci_probe_dev(NULL, pbus,
1104 PCI_DEVFN(devn, fn));
1105 }
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001106 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001107 } else {
1108 /* Ok, We need to set the links for that device.
1109 * otherwise the device under it will not be scanned
1110 */
Kyösti Mälkkic5163ed82015-02-04 13:25:37 +02001111
1112 add_more_links(cdb_dev, 4);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001113 }
1114
1115 family = cpuid_eax(1);
1116 family = (family >> 20) & 0xFF;
1117 if (family == 1) { //f10
1118 u32 dword;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001119 cdb_dev = pcidev_on_root(devn, 3);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001120 dword = pci_read_config32(cdb_dev, 0xe8);
1121 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1122 } else if (family == 7) {//f16
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001123 cdb_dev = pcidev_on_root(devn, 5);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001124 if (cdb_dev && cdb_dev->enabled) {
1125 siblings = pci_read_config32(cdb_dev, 0x84);
1126 siblings &= 0xFF;
1127 }
1128 } else {
1129 siblings = 0; //default one core
1130 }
1131 int enable_node = cdb_dev && cdb_dev->enabled;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001132 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 -06001133 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1134
Elyes HAOUASa8131602016-09-19 10:27:57 -06001135 for (j = 0; j <= siblings; j++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001136 u32 lapicid_start = 0;
1137
1138 /*
Elyes HAOUAS38a4f2a92020-01-07 19:53:36 +01001139 * APIC ID calculation is tightly coupled with AGESA v5 code.
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001140 * This calculation MUST match the assignment calculation done
1141 * in LocalApicInitializationAtEarly() function.
1142 * And reference GetLocalApicIdForCore()
1143 *
Elyes HAOUASa5b0bc42020-02-20 20:04:29 +01001144 * Apply APIC enumeration rules
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001145 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1146 * put the local-APICs at m..z
1147 *
1148 * This is needed because many IO-APIC devices only have 4 bits
1149 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001150 */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001151 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1152 lapicid_start = (ioapic_count - 1) / core_max;
1153 lapicid_start = (lapicid_start + 1) * core_max;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001154 printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001155 }
1156 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
Elyes HAOUASa8131602016-09-19 10:27:57 -06001157 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001158 i, j, apic_id);
1159
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001160 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001161 if (cpu)
1162 amd_cpu_topology(cpu, i, j);
1163 } //j
1164 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001165}
1166
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001167static void cpu_bus_init(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001168{
1169 initialize_cpus(dev->link_list);
1170}
1171
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001172static struct device_operations cpu_bus_ops = {
Kyösti Mälkki48f82a92016-12-02 16:02:30 +02001173 .read_resources = DEVICE_NOOP,
1174 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001175 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001176 .init = cpu_bus_init,
1177 .scan_bus = cpu_bus_scan,
1178};
1179
1180static void root_complex_enable_dev(struct device *dev)
1181{
1182 static int done = 0;
1183
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001184 if (!done) {
1185 setup_bsp_ramtop();
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001186 done = 1;
1187 }
1188
1189 /* Set the operations if it is a special bus type */
1190 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1191 dev->ops = &pci_domain_ops;
1192 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1193 dev->ops = &cpu_bus_ops;
1194 }
1195}
1196
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001197struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001198 CHIP_NAME("AMD FAM16 Root Complex")
1199 .enable_dev = root_complex_enable_dev,
1200};
1201
1202/*********************************************************************
1203 * Change the vendor / device IDs to match the generic VBIOS header. *
1204 *********************************************************************/
1205u32 map_oprom_vendev(u32 vendev)
1206{
1207 u32 new_vendev;
1208 new_vendev =
1209 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1210
1211 if (vendev != new_vendev)
1212 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1213
1214 return new_vendev;
1215}