blob: 0a07dfa1d524df6c430cdb1761ef65988e028c7a [file] [log] [blame]
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001/*
2 * This file is part of the coreboot project.
3 *
Bruce Griffith27ed80b2014-08-15 11:46:25 -06004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Bruce Griffith27ed80b2014-08-15 11:46:25 -060013 */
14
15#include <console/console.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020016#include <device/pci_ops.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060017#include <arch/acpi.h>
Michał Żygowski208318c2020-03-20 15:54:27 +010018#include <arch/ioapic.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060019#include <stdint.h>
20#include <device/device.h>
21#include <device/pci.h>
22#include <device/pci_ids.h>
23#include <device/hypertransport.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060024#include <string.h>
25#include <lib.h>
26#include <cpu/cpu.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060027#include <Porting.h>
28#include <AGESA.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060029#include <Topology.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020030#include <cpu/x86/lapic.h>
31#include <cpu/amd/msr.h>
32#include <cpu/amd/mtrr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020033#include <arch/acpigen.h>
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020034#include <northbridge/amd/pi/nb_common.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030035#include <northbridge/amd/agesa/agesa_helper.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060036
Kyösti Mälkki113f6702018-05-20 20:12:32 +030037#define MAX_NODE_NUMS MAX_NODES
Michał Żygowski6ca5b472019-09-10 15:10:22 +020038#define PCIE_CAP_AER BIT(5)
39#define PCIE_CAP_ACS BIT(6)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060040
Bruce Griffith27ed80b2014-08-15 11:46:25 -060041typedef struct dram_base_mask {
42 u32 base; //[47:27] at [28:8]
43 u32 mask; //[47:27] at [28:8] and enable at bit 0
44} dram_base_mask_t;
45
Subrata Banikb1434fc2019-03-15 22:20:41 +053046static unsigned int node_nums;
47static unsigned int sblink;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030048static struct device *__f0_dev[MAX_NODE_NUMS];
49static struct device *__f1_dev[MAX_NODE_NUMS];
50static struct device *__f2_dev[MAX_NODE_NUMS];
51static struct device *__f4_dev[MAX_NODE_NUMS];
Subrata Banikb1434fc2019-03-15 22:20:41 +053052static unsigned int fx_devs = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060053
54static dram_base_mask_t get_dram_base_mask(u32 nodeid)
55{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030056 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060057 dram_base_mask_t d;
58 dev = __f1_dev[0];
59 u32 temp;
60 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
61 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
62 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
63 d.mask |= temp<<21;
64 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
65 d.mask |= (temp & 1); // enable bit
66 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
67 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
68 d.base |= temp<<21;
69 return d;
70}
71
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030072static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Bruce Griffith27ed80b2014-08-15 11:46:25 -060073 u32 io_min, u32 io_max)
74{
75 u32 i;
76 u32 tempreg;
77 /* io range allocation */
78 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060079 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060080 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUASa8131602016-09-19 10:27:57 -060081 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUASa8131602016-09-19 10:27:57 -060082 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060083 pci_write_config32(__f1_dev[i], reg, tempreg);
84}
85
86static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
87{
88 u32 i;
89 u32 tempreg;
90 /* io range allocation */
91 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060092 for (i = 0; i < nodes; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060093 pci_write_config32(__f1_dev[i], reg+4, tempreg);
94 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUASa8131602016-09-19 10:27:57 -060095 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060096 pci_write_config32(__f1_dev[i], reg, tempreg);
97}
98
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030099static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600100{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200101 return pcidev_on_root(DEV_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600102}
103
104static void get_fx_devs(void)
105{
106 int i;
107 for (i = 0; i < MAX_NODE_NUMS; i++) {
108 __f0_dev[i] = get_node_pci(i, 0);
109 __f1_dev[i] = get_node_pci(i, 1);
110 __f2_dev[i] = get_node_pci(i, 2);
111 __f4_dev[i] = get_node_pci(i, 4);
112 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
113 fx_devs = i+1;
114 }
115 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
116 die("Cannot find 0:0x18.[0|1]\n");
117 }
Elyes HAOUASa8131602016-09-19 10:27:57 -0600118 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600119}
120
Subrata Banikb1434fc2019-03-15 22:20:41 +0530121static u32 f1_read_config32(unsigned int reg)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600122{
123 if (fx_devs == 0)
124 get_fx_devs();
125 return pci_read_config32(__f1_dev[0], reg);
126}
127
Subrata Banikb1434fc2019-03-15 22:20:41 +0530128static void f1_write_config32(unsigned int reg, u32 value)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600129{
130 int i;
131 if (fx_devs == 0)
132 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200133 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300134 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600135 dev = __f1_dev[i];
136 if (dev && dev->enabled) {
137 pci_write_config32(dev, reg, value);
138 }
139 }
140}
141
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300142static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600143{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200144 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600145}
146
147static void set_vga_enable_reg(u32 nodeid, u32 linkn)
148{
149 u32 val;
150
151 val = 1 | (nodeid<<4) | (linkn<<12);
152 /* it will routing
153 * (1)mmio 0xa0000:0xbffff
154 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
155 */
156 f1_write_config32(0xf4, val);
157
158}
159
160/**
161 * @return
Elyes HAOUAS99b075a2019-12-30 14:29:31 +0100162 * @retval 2 resource does not exist, usable
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600163 * @retval 0 resource exists, not usable
164 * @retval 1 resource exist, resource has been allocated before
165 */
Subrata Banikb1434fc2019-03-15 22:20:41 +0530166static int reg_useable(unsigned int reg, struct device *goal_dev,
167 unsigned int goal_nodeid, unsigned int goal_link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600168{
169 struct resource *res;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530170 unsigned int nodeid, link = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600171 int result;
172 res = 0;
173 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300174 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600175 dev = __f0_dev[nodeid];
176 if (!dev)
177 continue;
178 for (link = 0; !res && (link < 8); link++) {
179 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
180 }
181 }
182 result = 2;
183 if (res) {
184 result = 0;
185 if ((goal_link == (link - 1)) &&
186 (goal_nodeid == (nodeid - 1)) &&
187 (res->flags <= 1)) {
188 result = 1;
189 }
190 }
191 return result;
192}
193
Subrata Banikb1434fc2019-03-15 22:20:41 +0530194static struct resource *amdfam16_find_iopair(struct device *dev,
195 unsigned int nodeid, unsigned int link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600196{
197 struct resource *resource;
198 u32 free_reg, reg;
199 resource = 0;
200 free_reg = 0;
201 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
202 int result;
203 result = reg_useable(reg, dev, nodeid, link);
204 if (result == 1) {
205 /* I have been allocated this one */
206 break;
207 }
208 else if (result > 1) {
209 /* I have a free register pair */
210 free_reg = reg;
211 }
212 }
213 if (reg > 0xd8) {
214 reg = free_reg; // if no free, the free_reg still be 0
215 }
216
217 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
218
219 return resource;
220}
221
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300222static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600223{
224 struct resource *resource;
225 u32 free_reg, reg;
226 resource = 0;
227 free_reg = 0;
228 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
229 int result;
230 result = reg_useable(reg, dev, nodeid, link);
231 if (result == 1) {
232 /* I have been allocated this one */
233 break;
234 }
235 else if (result > 1) {
236 /* I have a free register pair */
237 free_reg = reg;
238 }
239 }
240 if (reg > 0xb8) {
241 reg = free_reg;
242 }
243
244 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
245 return resource;
246}
247
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300248static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600249{
250 struct resource *resource;
251
252 /* Initialize the io space constraints on the current bus */
253 resource = amdfam16_find_iopair(dev, nodeid, link);
254 if (resource) {
255 u32 align;
256 align = log2(HT_IO_HOST_ALIGN);
257 resource->base = 0;
258 resource->size = 0;
259 resource->align = align;
260 resource->gran = align;
261 resource->limit = 0xffffUL;
262 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
263 }
264
265 /* Initialize the prefetchable memory constraints on the current bus */
266 resource = amdfam16_find_mempair(dev, nodeid, link);
267 if (resource) {
268 resource->base = 0;
269 resource->size = 0;
270 resource->align = log2(HT_MEM_HOST_ALIGN);
271 resource->gran = log2(HT_MEM_HOST_ALIGN);
272 resource->limit = 0xffffffffffULL;
273 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
274 resource->flags |= IORESOURCE_BRIDGE;
275 }
276
277 /* Initialize the memory constraints on the current bus */
278 resource = amdfam16_find_mempair(dev, nodeid, link);
279 if (resource) {
280 resource->base = 0;
281 resource->size = 0;
282 resource->align = log2(HT_MEM_HOST_ALIGN);
283 resource->gran = log2(HT_MEM_HOST_ALIGN);
284 resource->limit = 0xffffffffffULL;
285 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
286 }
287
288}
289
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300290static void read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600291{
292 u32 nodeid;
293 struct bus *link;
Michał Żygowski208318c2020-03-20 15:54:27 +0100294 struct resource *res;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600295
296 nodeid = amdfam16_nodeid(dev);
297 for (link = dev->link_list; link; link = link->next) {
298 if (link->children) {
299 amdfam16_link_read_bases(dev, nodeid, link->link_num);
300 }
301 }
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300302
303 /*
304 * This MMCONF resource must be reserved in the PCI domain.
305 * It is not honored by the coreboot resource allocator if it is in
306 * the CPU_CLUSTER.
307 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200308 mmconf_resource(dev, MMIO_CONF_BASE);
Michał Żygowski208318c2020-03-20 15:54:27 +0100309
310 /* NB IOAPIC2 resource */
311 res = new_resource(dev, IO_APIC2_ADDR); /* IOAPIC2 */
312 res->base = IO_APIC2_ADDR;
313 res->size = 0x00001000;
314 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600315}
316
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300317static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600318{
319 resource_t rbase, rend;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530320 unsigned int reg, link_num;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600321 char buf[50];
322
323 /* Make certain the resource has actually been set */
324 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
325 return;
326 }
327
328 /* If I have already stored this resource don't worry about it */
329 if (resource->flags & IORESOURCE_STORED) {
330 return;
331 }
332
333 /* Only handle PCI memory and IO resources */
334 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
335 return;
336
337 /* Ensure I am actually looking at a resource of function 1 */
338 if ((resource->index & 0xffff) < 0x1000) {
339 return;
340 }
341 /* Get the base address */
342 rbase = resource->base;
343
344 /* Get the limit (rounded up) */
345 rend = resource_end(resource);
346
347 /* Get the register and link */
348 reg = resource->index & 0xfff; // 4k
349 link_num = IOINDEX_LINK(resource->index);
350
351 if (resource->flags & IORESOURCE_IO) {
352 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
353 }
354 else if (resource->flags & IORESOURCE_MEM) {
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200355 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 -0600356 }
357 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200358 snprintf(buf, sizeof(buf), " <node %x link %x>",
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600359 nodeid, link_num);
360 report_resource_stored(dev, resource, buf);
361}
362
363/**
364 * I tried to reuse the resource allocation code in set_resource()
365 * but it is too difficult to deal with the resource allocation magic.
366 */
367
Subrata Banikb1434fc2019-03-15 22:20:41 +0530368static void create_vga_resource(struct device *dev, unsigned int nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600369{
370 struct bus *link;
371
372 /* find out which link the VGA card is connected,
373 * we only deal with the 'first' vga card */
374 for (link = dev->link_list; link; link = link->next) {
375 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800376#if CONFIG(MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300377 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600378 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
379 link->secondary,link->subordinate);
380 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600381 if ((vga_pri->bus->secondary >= link->secondary) &&
382 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600383#endif
384 break;
385 }
386 }
387
388 /* no VGA card installed */
389 if (link == NULL)
390 return;
391
392 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
393 set_vga_enable_reg(nodeid, sblink);
394}
395
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300396static void set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600397{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530398 unsigned int nodeid;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600399 struct bus *bus;
400 struct resource *res;
401
402 /* Find the nodeid */
403 nodeid = amdfam16_nodeid(dev);
404
405 create_vga_resource(dev, nodeid); //TODO: do we need this?
406
407 /* Set each resource we have found */
408 for (res = dev->resource_list; res; res = res->next) {
409 set_resource(dev, res, nodeid);
410 }
411
412 for (bus = dev->link_list; bus; bus = bus->next) {
413 if (bus->children) {
414 assign_resources(bus);
415 }
416 }
417}
418
419static void northbridge_init(struct device *dev)
420{
Michał Żygowski208318c2020-03-20 15:54:27 +0100421 setup_ioapic((u8 *)IO_APIC2_ADDR, CONFIG_MAX_CPUS+1);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600422}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200423
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100424static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200425{
426 void *addr, *current;
427
428 /* Skip the HEST header. */
429 current = (void *)(hest + 1);
430
431 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
432 if (addr != NULL)
433 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
434
435 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
436 if (addr != NULL)
437 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
438
439 return (unsigned long)current;
440}
441
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500442static void add_ivhd_dev_entry(struct device *parent, struct device *dev,
443 unsigned long *current, uint16_t *length,
444 uint8_t type, uint8_t data)
445{
446 uint8_t *p;
447 p = (uint8_t *) *current;
448
449 if (type == 0x2) {
450 /* Entry type */
451 p[0] = type;
452 /* Device */
453 p[1] = dev->path.pci.devfn;
454 /* Bus */
455 p[2] = dev->bus->secondary;
456 /* Data */
457 p[3] = data;
458 /* [4:7] Padding */
459 p[4] = 0x0;
460 p[5] = 0x0;
461 p[6] = 0x0;
462 p[7] = 0x0;
463 *length += 8;
464 *current += 8;
465 } else if (type == 0x42) {
466 /* Entry type */
467 p[0] = type;
468 /* Device */
469 p[1] = dev->path.pci.devfn;
470 /* Bus */
471 p[2] = dev->bus->secondary;
472 /* Data */
473 p[3] = 0x0;
474 /* Reserved */
475 p[4] = 0x0;
476 /* Device */
477 p[5] = parent->path.pci.devfn;
478 /* Bus */
479 p[6] = parent->bus->secondary;
480 /* Reserved */
481 p[7] = 0x0;
482 *length += 8;
483 *current += 8;
484 }
485}
486
487static void add_ivrs_device_entries(struct device *parent, struct device *dev,
488 unsigned int depth, int linknum, int8_t *root_level,
489 unsigned long *current, uint16_t *length)
490{
491 struct device *sibling;
492 struct bus *link;
493 unsigned int header_type;
494 unsigned int is_pcie;
495
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500496 if (dev->path.type == DEVICE_PATH_PCI) {
497
498 if ((dev->bus->secondary == 0x0) &&
499 (dev->path.pci.devfn == 0x0))
500 *root_level = depth;
501
502 if ((*root_level != -1) && (dev->enabled)) {
503 if (depth == *root_level) {
504 if (dev->path.pci.devfn == (0x14 << 3)) {
505 /* SMBUS controller */
506 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x97);
507 } else if (dev->path.pci.devfn != 0x2 &&
508 dev->path.pci.devfn < (0x2 << 3)) {
509 /* FCH control device */
510 } else {
511 /* Other devices */
512 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
513 }
514 } else {
515 header_type = dev->hdr_type & 0x7f;
516 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
517 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
518 (header_type == PCI_HEADER_TYPE_BRIDGE))
519 && is_pcie) {
520 /* Device or Bridge is PCIe */
521 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
522 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) &&
523 !is_pcie) {
524 add_ivhd_dev_entry(parent, dev, current, length, 0x42, 0x0);
525 /* Device is legacy PCI or PCI-X */
526 }
527 }
528 }
529 }
530
531 for (link = dev->link_list; link; link = link->next)
532 for (sibling = link->children; sibling; sibling =
533 sibling->sibling)
534 add_ivrs_device_entries(dev, sibling, depth + 1, depth,
535 root_level, current, length);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500536}
537
538unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
539{
540 uint8_t *p;
541
542 uint32_t apicid_sb800;
543 uint32_t apicid_northbridge;
544
545 apicid_sb800 = CONFIG_MAX_CPUS;
546 apicid_northbridge = CONFIG_MAX_CPUS + 1;
547
548 /* Describe NB IOAPIC */
549 p = (uint8_t *)current;
550 p[0] = 0x48; /* Entry type */
551 p[1] = 0; /* Device */
552 p[2] = 0; /* Bus */
553 p[3] = 0x0; /* Data */
554 p[4] = apicid_northbridge; /* IOAPIC ID */
555 p[5] = 0x0; /* Device 0 Function 0 */
556 p[6] = 0x0; /* Northbridge bus */
557 p[7] = 0x1; /* Variety */
558 current += 8;
559
560 /* Describe SB IOAPIC */
561 p = (uint8_t *)current;
562 p[0] = 0x48; /* Entry type */
563 p[1] = 0; /* Device */
564 p[2] = 0; /* Bus */
565 p[3] = 0xd7; /* Data */
566 p[4] = apicid_sb800; /* IOAPIC ID */
567 p[5] = 0x14 << 3; /* Device 0x14 Function 0 */
568 p[6] = 0x0; /* Southbridge bus */
569 p[7] = 0x1; /* Variety */
570 current += 8;
571
572 return current;
573}
574
575static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
576{
577 uint8_t *p;
Piotr Król063e1562018-07-22 20:52:26 +0200578 acpi_ivrs_t *ivrs_agesa;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500579
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300580 struct device *nb_dev = pcidev_on_root(0x0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500581 if (!nb_dev) {
582
583 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
584 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
585
586 return (unsigned long)ivrs;
587 }
588
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500589
Piotr Król063e1562018-07-22 20:52:26 +0200590 /* obtain IOMMU base address */
591 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
592 if (ivrs_agesa != NULL) {
593 ivrs->iv_info = 0x0;
594 /* Maximum supported virtual address size */
595 ivrs->iv_info |= (0x40 << 15);
596 /* Maximum supported physical address size */
597 ivrs->iv_info |= (0x30 << 8);
598 /* Guest virtual address width */
599 ivrs->iv_info |= (0x2 << 5);
600
601 ivrs->ivhd.type = 0x10;
602 ivrs->ivhd.flags = 0x0e;
603 /* Enable ATS support */
604 ivrs->ivhd.flags |= 0x10;
605 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
606 /* BDF <bus>:00.2 */
607 ivrs->ivhd.device_id = 0x2 | (nb_dev->bus->secondary << 8);
608 /* Capability block 0x40 (type 0xf, "Secure device") */
609 ivrs->ivhd.capability_offset = 0x40;
610 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
611 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
612 ivrs->ivhd.pci_segment_group = 0x0;
613 ivrs->ivhd.iommu_info = 0x0;
614 ivrs->ivhd.iommu_info |= (0x13 << 8);
615 /* use only performance counters related bits:
616 * PNCounters[16:13] and
617 * PNBanks[22:17],
618 * otherwise 0 */
619 ivrs->ivhd.iommu_feature_info =
620 ivrs_agesa->ivhd.iommu_feature_info & 0x7fe000;
621 } else {
622 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
623
624 return (unsigned long)ivrs;
625 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500626
627 /* Describe HPET */
628 p = (uint8_t *)current;
629 p[0] = 0x48; /* Entry type */
630 p[1] = 0; /* Device */
631 p[2] = 0; /* Bus */
632 p[3] = 0xd7; /* Data */
633 p[4] = 0x0; /* HPET number */
634 p[5] = 0x14 << 3; /* HPET device */
635 p[6] = nb_dev->bus->secondary; /* HPET bus */
636 p[7] = 0x2; /* Variety */
637 ivrs->ivhd.length += 8;
638 current += 8;
639
640 /* Describe PCI devices */
Jacob Garber293e6a92019-07-17 11:47:19 -0600641 int8_t root_level = -1;
642 add_ivrs_device_entries(NULL, all_devices, 0, -1, &root_level, &current,
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500643 &ivrs->ivhd.length);
644
645 /* Describe IOAPICs */
646 unsigned long prev_current = current;
647 current = acpi_fill_ivrs_ioapic(ivrs, current);
648 ivrs->ivhd.length += (current - prev_current);
649
650 return current;
651}
652
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300653static void northbridge_fill_ssdt_generator(struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200654{
655 msr_t msr;
656 char pscope[] = "\\_SB.PCI0";
657
658 acpigen_write_scope(pscope);
659 msr = rdmsr(TOP_MEM);
660 acpigen_write_name_dword("TOM1", msr.lo);
661 msr = rdmsr(TOP_MEM2);
662 /*
663 * Since XP only implements parts of ACPI 2.0, we can't use a qword
664 * here.
665 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
666 * slide 22ff.
667 * Shift value right by 20 bit to make it fit into 32bit,
668 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
669 */
670 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
671 acpigen_pop_len();
672}
673
Michał Żygowski9550e972020-03-20 13:56:46 +0100674static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
675{
676 unsigned int len = ssdt->length - sizeof(acpi_header_t);
677 unsigned int i;
678
679 for (i = sizeof(acpi_header_t); i < len; i++) {
680 /* Search for _PR_ scope and replace it with _SB_ */
681 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
682 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
683 }
684 /* Recalculate checksum */
685 ssdt->checksum = 0;
686 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
687}
688
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300689static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200690 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200691 acpi_rsdp_t *rsdp)
692{
693 acpi_srat_t *srat;
694 acpi_slit_t *slit;
695 acpi_header_t *ssdt;
696 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500697 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200698
699 /* HEST */
700 current = ALIGN(current, 8);
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100701 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200702 acpi_add_table(rsdp, (void *)current);
703 current += ((acpi_header_t *)current)->length;
704
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500705 /* IVRS */
706 current = ALIGN(current, 8);
707 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
708 ivrs = (acpi_ivrs_t *) current;
709 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
710 current += ivrs->header.length;
711 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200712
713 /* SRAT */
714 current = ALIGN(current, 8);
715 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
716 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
717 if (srat != NULL) {
718 memcpy((void *)current, srat, srat->header.length);
719 srat = (acpi_srat_t *) current;
720 current += srat->header.length;
721 acpi_add_table(rsdp, srat);
722 } else {
723 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
724 }
725
726 /* SLIT */
727 current = ALIGN(current, 8);
728 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
729 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
730 if (slit != NULL) {
731 memcpy((void *)current, slit, slit->header.length);
732 slit = (acpi_slit_t *) current;
733 current += slit->header.length;
734 acpi_add_table(rsdp, slit);
735 } else {
736 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
737 }
738
739 /* ALIB */
740 current = ALIGN(current, 16);
741 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
742 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
743 if (alib != NULL) {
744 memcpy((void *)current, alib, alib->length);
745 alib = (acpi_header_t *) current;
746 current += alib->length;
747 acpi_add_table(rsdp, (void *)alib);
748 }
749 else {
750 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
751 }
752
753 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
754 /* SSDT */
755 current = ALIGN(current, 16);
756 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
757 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
758 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100759 patch_ssdt_processor_scope(ssdt);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200760 memcpy((void *)current, ssdt, ssdt->length);
761 ssdt = (acpi_header_t *) current;
762 current += ssdt->length;
763 }
764 else {
765 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
766 }
767 acpi_add_table(rsdp,ssdt);
768
769 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
770 return current;
771}
772
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600773static struct device_operations northbridge_operations = {
774 .read_resources = read_resources,
775 .set_resources = set_resources,
776 .enable_resources = pci_dev_enable_resources,
777 .init = northbridge_init,
Nico Huber68680dd2020-03-31 17:34:52 +0200778 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200779 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600780 .enable = 0,
781 .ops_pci = 0,
782};
783
784static const struct pci_driver family16_northbridge __pci_driver = {
785 .ops = &northbridge_operations,
786 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600787 .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600788};
789
790static const struct pci_driver family10_northbridge __pci_driver = {
791 .ops = &northbridge_operations,
792 .vendor = PCI_VENDOR_ID_AMD,
793 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
794};
795
Dave Frodin891f71a2015-01-19 15:58:24 -0700796static void fam16_finalize(void *chip_info)
797{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300798 struct device *dev;
Dave Frodin891f71a2015-01-19 15:58:24 -0700799 u32 value;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300800 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Dave Frodin891f71a2015-01-19 15:58:24 -0700801 pci_write_config32(dev, 0xF8, 0);
802 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
803
Michał Żygowski6ca5b472019-09-10 15:10:22 +0200804 /*
805 * Currently it is impossible to enable ACS with AGESA by setting the
806 * correct bit for AmdInitMid phase. AGESA code path does not call the
807 * right function that enables these functionalities. Disabled ACS
808 * result in multiple PCIe devices to be assigned to the same IOMMU
809 * group. Without IOMMU group separation the devices cannot be passed
810 * through independently.
811 */
812
813 /* Select GPP link core IO Link Strap Control register 0xB0 */
814 pci_write_config32(dev, 0xE0, 0x014000B0);
815 value = pci_read_config32(dev, 0xE4);
816
817 /* Enable AER (bit 5) and ACS (bit 6 undocumented) */
818 value |= PCIE_CAP_AER | PCIE_CAP_ACS;
819 pci_write_config32(dev, 0xE4, value);
820
821 /* Select GPP link core Wrapper register 0x00 (undocumented) */
822 pci_write_config32(dev, 0xE0, 0x01300000);
823 value = pci_read_config32(dev, 0xE4);
824
825 /*
826 * Enable ACS capabilities straps including sub-items. From lspci it
827 * looks like these bits enable: Source Validation and Translation
828 * Blocking
829 */
830 value |= (BIT(24) | BIT(25) | BIT(26));
831 pci_write_config32(dev, 0xE4, value);
832
Dave Frodin891f71a2015-01-19 15:58:24 -0700833 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300834 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200835 if (dev != NULL) {
836 value = pci_read_config32(dev, 0x60);
837 value &= ~(1 << 11);
838 pci_write_config32(dev, 0x60, value);
839 }
Dave Frodin891f71a2015-01-19 15:58:24 -0700840}
841
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300842struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600843 CHIP_NAME("AMD FAM16 Northbridge")
844 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700845 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600846};
847
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300848static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600849{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530850 unsigned int reg;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600851
852 /* Find the already assigned resource pairs */
853 get_fx_devs();
854 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
855 u32 base, limit;
856 base = f1_read_config32(reg);
857 limit = f1_read_config32(reg + 0x04);
858 /* Is this register allocated? */
859 if ((base & 3) != 0) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530860 unsigned int nodeid, reg_link;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300861 struct device *reg_dev;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600862 if (reg < 0xc0) { // mmio
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600863 nodeid = (limit & 0xf) + (base&0x30);
864 } else { // io
865 nodeid = (limit & 0xf) + ((base>>4)&0x30);
866 }
867 reg_link = (limit >> 4) & 7;
868 reg_dev = __f0_dev[nodeid];
869 if (reg_dev) {
870 /* Reserve the resource */
871 struct resource *res;
872 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
873 if (res) {
874 res->flags = 1;
875 }
876 }
877 }
878 }
879 /* FIXME: do we need to check extend conf space?
880 I don't believe that much preset value */
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600881 pci_domain_read_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600882}
883
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300884static void domain_enable_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600885{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600886}
887
888#if CONFIG_HW_MEM_HOLE_SIZEK != 0
889struct hw_mem_hole_info {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530890 unsigned int hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600891 int node_id;
892};
893static struct hw_mem_hole_info get_hw_mem_hole_info(void)
894{
895 struct hw_mem_hole_info mem_hole;
896 int i;
897 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
898 mem_hole.node_id = -1;
899 for (i = 0; i < node_nums; i++) {
900 dram_base_mask_t d;
901 u32 hole;
902 d = get_dram_base_mask(i);
903 if (!(d.mask & 1)) continue; // no memory on this node
904 hole = pci_read_config32(__f1_dev[i], 0xf0);
905 if (hole & 2) { // we find the hole
906 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
907 mem_hole.node_id = i; // record the node No with hole
908 break; // only one hole
909 }
910 }
911
912 /* We need to double check if there is special set on base reg and limit reg
913 * are not continuous instead of hole, it will find out its hole_startk.
914 */
915 if (mem_hole.node_id == -1) {
916 resource_t limitk_pri = 0;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600917 for (i = 0; i < node_nums; i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600918 dram_base_mask_t d;
919 resource_t base_k, limit_k;
920 d = get_dram_base_mask(i);
921 if (!(d.base & 1)) continue;
922 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
923 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
924 if (limitk_pri != base_k) { // we find the hole
Elyes HAOUAS38a4f2a92020-01-07 19:53:36 +0100925 mem_hole.hole_startk = (unsigned int)limitk_pri; // must be below 4G
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600926 mem_hole.node_id = i;
927 break; //only one hole
928 }
929 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
930 limitk_pri = limit_k;
931 }
932 }
933 return mem_hole;
934}
935#endif
936
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300937static void domain_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600938{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600939 unsigned long mmio_basek;
940 u32 pci_tolm;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600941 int i, idx;
942 struct bus *link;
943#if CONFIG_HW_MEM_HOLE_SIZEK != 0
944 struct hw_mem_hole_info mem_hole;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600945#endif
946
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600947 pci_tolm = 0xffffffffUL;
948 for (link = dev->link_list; link; link = link->next) {
949 pci_tolm = find_pci_tolm(link);
950 }
951
952 // FIXME handle interleaved nodes. If you fix this here, please fix
953 // amdk8, too.
954 mmio_basek = pci_tolm >> 10;
955 /* Round mmio_basek to something the processor can support */
956 mmio_basek &= ~((1 << 6) -1);
957
958 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
959 // MMIO hole. If you fix this here, please fix amdk8, too.
960 /* Round the mmio hole to 64M */
961 mmio_basek &= ~((64*1024) - 1);
962
963#if CONFIG_HW_MEM_HOLE_SIZEK != 0
964 /* if the hw mem hole is already set in raminit stage, here we will compare
965 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
966 * use hole_basek as mmio_basek and we don't need to reset hole.
967 * otherwise We reset the hole to the mmio_basek
968 */
969
970 mem_hole = get_hw_mem_hole_info();
971
972 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
973 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
974 mmio_basek = mem_hole.hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600975 }
976#endif
977
978 idx = 0x10;
979 for (i = 0; i < node_nums; i++) {
980 dram_base_mask_t d;
981 resource_t basek, limitk, sizek; // 4 1T
982
983 d = get_dram_base_mask(i);
984
985 if (!(d.mask & 1)) continue;
986 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200987 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600988
989 sizek = limitk - basek;
990
991 /* see if we need a hole from 0xa0000 to 0xbffff */
992 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
993 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
994 idx += 0x10;
995 basek = (8*64)+(16*16);
996 sizek = limitk - ((8*64)+(16*16));
997
998 }
999
1000 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
1001
1002 /* split the region to accommodate pci memory space */
Elyes HAOUASa8131602016-09-19 10:27:57 -06001003 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001004 if (basek <= mmio_basek) {
Subrata Banikb1434fc2019-03-15 22:20:41 +05301005 unsigned int pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001006 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001007 if (pre_sizek > 0) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001008 ram_resource(dev, (idx | i), basek, pre_sizek);
1009 idx += 0x10;
1010 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001011 }
1012 basek = mmio_basek;
1013 }
1014 if ((basek + sizek) <= 4*1024*1024) {
1015 sizek = 0;
1016 }
1017 else {
1018 uint64_t topmem2 = bsp_topmem2();
1019 basek = 4*1024*1024;
1020 sizek = topmem2/1024 - basek;
1021 }
1022 }
1023
1024 ram_resource(dev, (idx | i), basek, sizek);
1025 idx += 0x10;
1026 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
1027 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001028 }
1029
Kyösti Mälkkie87564f2017-04-15 20:07:53 +03001030 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001031
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001032 for (link = dev->link_list; link; link = link->next) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001033 if (link->children) {
1034 assign_resources(link);
1035 }
1036 }
1037}
1038
Aaron Durbinaa090cb2017-09-13 16:01:52 -06001039static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001040{
1041 if (dev->path.type == DEVICE_PATH_DOMAIN)
1042 return "PCI0";
1043
1044 return NULL;
1045}
1046
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001047static struct device_operations pci_domain_ops = {
1048 .read_resources = domain_read_resources,
1049 .set_resources = domain_set_resources,
1050 .enable_resources = domain_enable_resources,
1051 .init = NULL,
1052 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001053 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001054};
1055
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001056static void sysconf_init(struct device *dev) // first node
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001057{
1058 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1059 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1060}
1061
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001062static void cpu_bus_scan(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001063{
1064 struct bus *cpu_bus;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001065 struct device *dev_mc;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001066 int i,j;
1067 int coreid_bits;
1068 int core_max = 0;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301069 unsigned int ApicIdCoreIdSize;
1070 unsigned int core_nums;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001071 int siblings = 0;
1072 unsigned int family;
1073 u32 modules = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001074 int ioapic_count = 0;
1075
Michał Żygowskie7192882019-11-23 19:02:19 +01001076 /* For binaryPI there is no multiprocessor configuration, the number of
1077 * modules will always be 1. */
1078 modules = 1;
1079 ioapic_count = CONFIG_NUM_OF_IOAPICS;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001080
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001081 dev_mc = pcidev_on_root(DEV_CDB, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001082 if (!dev_mc) {
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001083 printk(BIOS_ERR, "0:%02x.0 not found", DEV_CDB);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001084 die("");
1085 }
1086 sysconf_init(dev_mc);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001087
1088 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +03001089 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001090 core_max = 1 << (coreid_bits & 0x000F); //mnc
1091
1092 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1093 if (ApicIdCoreIdSize) {
1094 core_nums = (1 << ApicIdCoreIdSize) - 1;
1095 } else {
1096 core_nums = 3; //quad core
1097 }
1098
1099 /* Find which cpus are present */
1100 cpu_bus = dev->link_list;
1101 for (i = 0; i < node_nums; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001102 struct device *cdb_dev;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301103 unsigned int devn;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001104 struct bus *pbus;
1105
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001106 devn = DEV_CDB + i;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001107 pbus = dev_mc->bus;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001108
1109 /* Find the cpu's pci device */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001110 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001111 if (!cdb_dev) {
1112 /* If I am probing things in a weird order
1113 * ensure all of the cpu's pci devices are found.
1114 */
1115 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001116 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001117 cdb_dev = pci_probe_dev(NULL, pbus,
1118 PCI_DEVFN(devn, fn));
1119 }
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001120 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001121 } else {
1122 /* Ok, We need to set the links for that device.
1123 * otherwise the device under it will not be scanned
1124 */
Kyösti Mälkkic5163ed82015-02-04 13:25:37 +02001125
1126 add_more_links(cdb_dev, 4);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001127 }
1128
1129 family = cpuid_eax(1);
1130 family = (family >> 20) & 0xFF;
1131 if (family == 1) { //f10
1132 u32 dword;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001133 cdb_dev = pcidev_on_root(devn, 3);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001134 dword = pci_read_config32(cdb_dev, 0xe8);
1135 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1136 } else if (family == 7) {//f16
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001137 cdb_dev = pcidev_on_root(devn, 5);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001138 if (cdb_dev && cdb_dev->enabled) {
1139 siblings = pci_read_config32(cdb_dev, 0x84);
1140 siblings &= 0xFF;
1141 }
1142 } else {
1143 siblings = 0; //default one core
1144 }
1145 int enable_node = cdb_dev && cdb_dev->enabled;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001146 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 -06001147 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1148
Elyes HAOUASa8131602016-09-19 10:27:57 -06001149 for (j = 0; j <= siblings; j++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001150 u32 lapicid_start = 0;
1151
1152 /*
Elyes HAOUAS38a4f2a92020-01-07 19:53:36 +01001153 * APIC ID calculation is tightly coupled with AGESA v5 code.
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001154 * This calculation MUST match the assignment calculation done
1155 * in LocalApicInitializationAtEarly() function.
1156 * And reference GetLocalApicIdForCore()
1157 *
Elyes HAOUASa5b0bc42020-02-20 20:04:29 +01001158 * Apply APIC enumeration rules
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001159 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1160 * put the local-APICs at m..z
1161 *
1162 * This is needed because many IO-APIC devices only have 4 bits
1163 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001164 */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001165 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1166 lapicid_start = (ioapic_count - 1) / core_max;
1167 lapicid_start = (lapicid_start + 1) * core_max;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001168 printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001169 }
1170 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
Elyes HAOUASa8131602016-09-19 10:27:57 -06001171 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001172 i, j, apic_id);
1173
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001174 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001175 if (cpu)
1176 amd_cpu_topology(cpu, i, j);
1177 } //j
1178 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001179}
1180
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001181static void cpu_bus_init(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001182{
1183 initialize_cpus(dev->link_list);
1184}
1185
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001186static struct device_operations cpu_bus_ops = {
Kyösti Mälkki48f82a92016-12-02 16:02:30 +02001187 .read_resources = DEVICE_NOOP,
1188 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001189 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001190 .init = cpu_bus_init,
1191 .scan_bus = cpu_bus_scan,
1192};
1193
1194static void root_complex_enable_dev(struct device *dev)
1195{
1196 static int done = 0;
1197
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001198 if (!done) {
1199 setup_bsp_ramtop();
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001200 done = 1;
1201 }
1202
1203 /* Set the operations if it is a special bus type */
1204 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1205 dev->ops = &pci_domain_ops;
1206 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1207 dev->ops = &cpu_bus_ops;
1208 }
1209}
1210
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001211struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001212 CHIP_NAME("AMD FAM16 Root Complex")
1213 .enable_dev = root_complex_enable_dev,
1214};
1215
1216/*********************************************************************
1217 * Change the vendor / device IDs to match the generic VBIOS header. *
1218 *********************************************************************/
1219u32 map_oprom_vendev(u32 vendev)
1220{
1221 u32 new_vendev;
1222 new_vendev =
1223 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1224
1225 if (vendev != new_vendev)
1226 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1227
1228 return new_vendev;
1229}