blob: 3d7b883d1778adf8d9711770d4c357d66913df89 [file] [log] [blame]
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 Advanced Micro Devices, Inc.
Timothy Pearson9ef07d82016-06-13 13:48:58 -05005 * Copyright (C) 2016 Raptor Engineering, LLC
6 * Copyright (C) 2018 3mdeb Embedded Systems Consulting
Bruce Griffith27ed80b2014-08-15 11:46:25 -06007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Bruce Griffith27ed80b2014-08-15 11:46:25 -060016 */
17
18#include <console/console.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060020#include <arch/acpi.h>
21#include <stdint.h>
22#include <device/device.h>
23#include <device/pci.h>
24#include <device/pci_ids.h>
25#include <device/hypertransport.h>
26#include <stdlib.h>
27#include <string.h>
28#include <lib.h>
29#include <cpu/cpu.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060030#include <Porting.h>
31#include <AGESA.h>
Bruce Griffith27ed80b2014-08-15 11:46:25 -060032#include <Topology.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020033#include <cpu/x86/lapic.h>
34#include <cpu/amd/msr.h>
35#include <cpu/amd/mtrr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020036#include <arch/acpigen.h>
Kyösti Mälkkibbd23772019-01-10 05:41:23 +020037#include <northbridge/amd/pi/nb_common.h>
Kyösti Mälkkied8d2772017-07-15 17:12:44 +030038#include <northbridge/amd/agesa/agesa_helper.h>
Julius Wernercd49cce2019-03-05 16:53:33 -080039#if CONFIG(BINARYPI_LEGACY_WRAPPER)
Kyösti Mälkki023ed1f2014-10-22 08:05:36 +030040#include <northbridge/amd/pi/agesawrapper.h>
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +030041#include <northbridge/amd/pi/agesawrapper_call.h>
Kyösti Mälkki903ce252016-11-25 11:21:02 +020042#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -060043
Kyösti Mälkki113f6702018-05-20 20:12:32 +030044#define MAX_NODE_NUMS MAX_NODES
Bruce Griffith27ed80b2014-08-15 11:46:25 -060045
Bruce Griffith27ed80b2014-08-15 11:46:25 -060046typedef struct dram_base_mask {
47 u32 base; //[47:27] at [28:8]
48 u32 mask; //[47:27] at [28:8] and enable at bit 0
49} dram_base_mask_t;
50
Subrata Banikb1434fc2019-03-15 22:20:41 +053051static unsigned int node_nums;
52static unsigned int sblink;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030053static struct device *__f0_dev[MAX_NODE_NUMS];
54static struct device *__f1_dev[MAX_NODE_NUMS];
55static struct device *__f2_dev[MAX_NODE_NUMS];
56static struct device *__f4_dev[MAX_NODE_NUMS];
Subrata Banikb1434fc2019-03-15 22:20:41 +053057static unsigned int fx_devs = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060058
59static dram_base_mask_t get_dram_base_mask(u32 nodeid)
60{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030061 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -060062 dram_base_mask_t d;
63 dev = __f1_dev[0];
64 u32 temp;
65 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
66 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
67 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
68 d.mask |= temp<<21;
69 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
70 d.mask |= (temp & 1); // enable bit
71 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
72 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
73 d.base |= temp<<21;
74 return d;
75}
76
Kyösti Mälkki90ac7362018-05-20 20:59:52 +030077static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Bruce Griffith27ed80b2014-08-15 11:46:25 -060078 u32 io_min, u32 io_max)
79{
80 u32 i;
81 u32 tempreg;
82 /* io range allocation */
83 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060084 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060085 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUASa8131602016-09-19 10:27:57 -060086 tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUASa8131602016-09-19 10:27:57 -060087 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060088 pci_write_config32(__f1_dev[i], reg, tempreg);
89}
90
91static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
92{
93 u32 i;
94 u32 tempreg;
95 /* io range allocation */
96 tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit
Elyes HAOUASa8131602016-09-19 10:27:57 -060097 for (i = 0; i < nodes; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -060098 pci_write_config32(__f1_dev[i], reg+4, tempreg);
99 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUASa8131602016-09-19 10:27:57 -0600100 for (i = 0; i < node_nums; i++)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600101 pci_write_config32(__f1_dev[i], reg, tempreg);
102}
103
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300104static struct device *get_node_pci(u32 nodeid, u32 fn)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600105{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200106 return pcidev_on_root(DEV_CDB + nodeid, fn);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600107}
108
109static void get_fx_devs(void)
110{
111 int i;
112 for (i = 0; i < MAX_NODE_NUMS; i++) {
113 __f0_dev[i] = get_node_pci(i, 0);
114 __f1_dev[i] = get_node_pci(i, 1);
115 __f2_dev[i] = get_node_pci(i, 2);
116 __f4_dev[i] = get_node_pci(i, 4);
117 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
118 fx_devs = i+1;
119 }
120 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
121 die("Cannot find 0:0x18.[0|1]\n");
122 }
Elyes HAOUASa8131602016-09-19 10:27:57 -0600123 printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600124}
125
Subrata Banikb1434fc2019-03-15 22:20:41 +0530126static u32 f1_read_config32(unsigned int reg)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600127{
128 if (fx_devs == 0)
129 get_fx_devs();
130 return pci_read_config32(__f1_dev[0], reg);
131}
132
Subrata Banikb1434fc2019-03-15 22:20:41 +0530133static void f1_write_config32(unsigned int reg, u32 value)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600134{
135 int i;
136 if (fx_devs == 0)
137 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200138 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300139 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600140 dev = __f1_dev[i];
141 if (dev && dev->enabled) {
142 pci_write_config32(dev, reg, value);
143 }
144 }
145}
146
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300147static u32 amdfam16_nodeid(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600148{
Kyösti Mälkkibbd23772019-01-10 05:41:23 +0200149 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600150}
151
152static void set_vga_enable_reg(u32 nodeid, u32 linkn)
153{
154 u32 val;
155
156 val = 1 | (nodeid<<4) | (linkn<<12);
157 /* it will routing
158 * (1)mmio 0xa0000:0xbffff
159 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
160 */
161 f1_write_config32(0xf4, val);
162
163}
164
165/**
166 * @return
167 * @retval 2 resoure does not exist, usable
168 * @retval 0 resource exists, not usable
169 * @retval 1 resource exist, resource has been allocated before
170 */
Subrata Banikb1434fc2019-03-15 22:20:41 +0530171static int reg_useable(unsigned int reg, struct device *goal_dev,
172 unsigned int goal_nodeid, unsigned int goal_link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600173{
174 struct resource *res;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530175 unsigned int nodeid, link = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600176 int result;
177 res = 0;
178 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300179 struct device *dev;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600180 dev = __f0_dev[nodeid];
181 if (!dev)
182 continue;
183 for (link = 0; !res && (link < 8); link++) {
184 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
185 }
186 }
187 result = 2;
188 if (res) {
189 result = 0;
190 if ((goal_link == (link - 1)) &&
191 (goal_nodeid == (nodeid - 1)) &&
192 (res->flags <= 1)) {
193 result = 1;
194 }
195 }
196 return result;
197}
198
Subrata Banikb1434fc2019-03-15 22:20:41 +0530199static struct resource *amdfam16_find_iopair(struct device *dev,
200 unsigned int nodeid, unsigned int link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600201{
202 struct resource *resource;
203 u32 free_reg, reg;
204 resource = 0;
205 free_reg = 0;
206 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
207 int result;
208 result = reg_useable(reg, dev, nodeid, link);
209 if (result == 1) {
210 /* I have been allocated this one */
211 break;
212 }
213 else if (result > 1) {
214 /* I have a free register pair */
215 free_reg = reg;
216 }
217 }
218 if (reg > 0xd8) {
219 reg = free_reg; // if no free, the free_reg still be 0
220 }
221
222 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
223
224 return resource;
225}
226
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300227static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600228{
229 struct resource *resource;
230 u32 free_reg, reg;
231 resource = 0;
232 free_reg = 0;
233 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
234 int result;
235 result = reg_useable(reg, dev, nodeid, link);
236 if (result == 1) {
237 /* I have been allocated this one */
238 break;
239 }
240 else if (result > 1) {
241 /* I have a free register pair */
242 free_reg = reg;
243 }
244 }
245 if (reg > 0xb8) {
246 reg = free_reg;
247 }
248
249 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
250 return resource;
251}
252
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300253static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600254{
255 struct resource *resource;
256
257 /* Initialize the io space constraints on the current bus */
258 resource = amdfam16_find_iopair(dev, nodeid, link);
259 if (resource) {
260 u32 align;
261 align = log2(HT_IO_HOST_ALIGN);
262 resource->base = 0;
263 resource->size = 0;
264 resource->align = align;
265 resource->gran = align;
266 resource->limit = 0xffffUL;
267 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
268 }
269
270 /* Initialize the prefetchable memory constraints on the current bus */
271 resource = amdfam16_find_mempair(dev, nodeid, link);
272 if (resource) {
273 resource->base = 0;
274 resource->size = 0;
275 resource->align = log2(HT_MEM_HOST_ALIGN);
276 resource->gran = log2(HT_MEM_HOST_ALIGN);
277 resource->limit = 0xffffffffffULL;
278 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
279 resource->flags |= IORESOURCE_BRIDGE;
280 }
281
282 /* Initialize the memory constraints on the current bus */
283 resource = amdfam16_find_mempair(dev, nodeid, link);
284 if (resource) {
285 resource->base = 0;
286 resource->size = 0;
287 resource->align = log2(HT_MEM_HOST_ALIGN);
288 resource->gran = log2(HT_MEM_HOST_ALIGN);
289 resource->limit = 0xffffffffffULL;
290 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
291 }
292
293}
294
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300295static void read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600296{
297 u32 nodeid;
298 struct bus *link;
299
300 nodeid = amdfam16_nodeid(dev);
301 for (link = dev->link_list; link; link = link->next) {
302 if (link->children) {
303 amdfam16_link_read_bases(dev, nodeid, link->link_num);
304 }
305 }
Kyösti Mälkki5d490382015-05-27 07:58:22 +0300306
307 /*
308 * This MMCONF resource must be reserved in the PCI domain.
309 * It is not honored by the coreboot resource allocator if it is in
310 * the CPU_CLUSTER.
311 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200312 mmconf_resource(dev, MMIO_CONF_BASE);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600313}
314
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300315static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600316{
317 resource_t rbase, rend;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530318 unsigned int reg, link_num;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600319 char buf[50];
320
321 /* Make certain the resource has actually been set */
322 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
323 return;
324 }
325
326 /* If I have already stored this resource don't worry about it */
327 if (resource->flags & IORESOURCE_STORED) {
328 return;
329 }
330
331 /* Only handle PCI memory and IO resources */
332 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
333 return;
334
335 /* Ensure I am actually looking at a resource of function 1 */
336 if ((resource->index & 0xffff) < 0x1000) {
337 return;
338 }
339 /* Get the base address */
340 rbase = resource->base;
341
342 /* Get the limit (rounded up) */
343 rend = resource_end(resource);
344
345 /* Get the register and link */
346 reg = resource->index & 0xfff; // 4k
347 link_num = IOINDEX_LINK(resource->index);
348
349 if (resource->flags & IORESOURCE_IO) {
350 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
351 }
352 else if (resource->flags & IORESOURCE_MEM) {
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200353 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 -0600354 }
355 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200356 snprintf(buf, sizeof(buf), " <node %x link %x>",
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600357 nodeid, link_num);
358 report_resource_stored(dev, resource, buf);
359}
360
361/**
362 * I tried to reuse the resource allocation code in set_resource()
363 * but it is too difficult to deal with the resource allocation magic.
364 */
365
Subrata Banikb1434fc2019-03-15 22:20:41 +0530366static void create_vga_resource(struct device *dev, unsigned int nodeid)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600367{
368 struct bus *link;
369
370 /* find out which link the VGA card is connected,
371 * we only deal with the 'first' vga card */
372 for (link = dev->link_list; link; link = link->next) {
373 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800374#if CONFIG(MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300375 extern struct device *vga_pri; // the primary vga device, defined in device.c
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600376 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
377 link->secondary,link->subordinate);
378 /* We need to make sure the vga_pri is under the link */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600379 if ((vga_pri->bus->secondary >= link->secondary) &&
380 (vga_pri->bus->secondary <= link->subordinate))
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600381#endif
382 break;
383 }
384 }
385
386 /* no VGA card installed */
387 if (link == NULL)
388 return;
389
390 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
391 set_vga_enable_reg(nodeid, sblink);
392}
393
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300394static void set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600395{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530396 unsigned int nodeid;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600397 struct bus *bus;
398 struct resource *res;
399
400 /* Find the nodeid */
401 nodeid = amdfam16_nodeid(dev);
402
403 create_vga_resource(dev, nodeid); //TODO: do we need this?
404
405 /* Set each resource we have found */
406 for (res = dev->resource_list; res; res = res->next) {
407 set_resource(dev, res, nodeid);
408 }
409
410 for (bus = dev->link_list; bus; bus = bus->next) {
411 if (bus->children) {
412 assign_resources(bus);
413 }
414 }
415}
416
417static void northbridge_init(struct device *dev)
418{
419}
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200420
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100421static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200422{
423 void *addr, *current;
424
425 /* Skip the HEST header. */
426 current = (void *)(hest + 1);
427
428 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
429 if (addr != NULL)
430 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
431
432 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
433 if (addr != NULL)
434 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
435
436 return (unsigned long)current;
437}
438
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500439static void add_ivhd_dev_entry(struct device *parent, struct device *dev,
440 unsigned long *current, uint16_t *length,
441 uint8_t type, uint8_t data)
442{
443 uint8_t *p;
444 p = (uint8_t *) *current;
445
446 if (type == 0x2) {
447 /* Entry type */
448 p[0] = type;
449 /* Device */
450 p[1] = dev->path.pci.devfn;
451 /* Bus */
452 p[2] = dev->bus->secondary;
453 /* Data */
454 p[3] = data;
455 /* [4:7] Padding */
456 p[4] = 0x0;
457 p[5] = 0x0;
458 p[6] = 0x0;
459 p[7] = 0x0;
460 *length += 8;
461 *current += 8;
462 } else if (type == 0x42) {
463 /* Entry type */
464 p[0] = type;
465 /* Device */
466 p[1] = dev->path.pci.devfn;
467 /* Bus */
468 p[2] = dev->bus->secondary;
469 /* Data */
470 p[3] = 0x0;
471 /* Reserved */
472 p[4] = 0x0;
473 /* Device */
474 p[5] = parent->path.pci.devfn;
475 /* Bus */
476 p[6] = parent->bus->secondary;
477 /* Reserved */
478 p[7] = 0x0;
479 *length += 8;
480 *current += 8;
481 }
482}
483
484static void add_ivrs_device_entries(struct device *parent, struct device *dev,
485 unsigned int depth, int linknum, int8_t *root_level,
486 unsigned long *current, uint16_t *length)
487{
488 struct device *sibling;
489 struct bus *link;
490 unsigned int header_type;
491 unsigned int is_pcie;
492
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500493 if (dev->path.type == DEVICE_PATH_PCI) {
494
495 if ((dev->bus->secondary == 0x0) &&
496 (dev->path.pci.devfn == 0x0))
497 *root_level = depth;
498
499 if ((*root_level != -1) && (dev->enabled)) {
500 if (depth == *root_level) {
501 if (dev->path.pci.devfn == (0x14 << 3)) {
502 /* SMBUS controller */
503 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x97);
504 } else if (dev->path.pci.devfn != 0x2 &&
505 dev->path.pci.devfn < (0x2 << 3)) {
506 /* FCH control device */
507 } else {
508 /* Other devices */
509 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
510 }
511 } else {
512 header_type = dev->hdr_type & 0x7f;
513 is_pcie = pci_find_capability(dev, PCI_CAP_ID_PCIE);
514 if (((header_type == PCI_HEADER_TYPE_NORMAL) ||
515 (header_type == PCI_HEADER_TYPE_BRIDGE))
516 && is_pcie) {
517 /* Device or Bridge is PCIe */
518 add_ivhd_dev_entry(parent, dev, current, length, 0x2, 0x0);
519 } else if ((header_type == PCI_HEADER_TYPE_NORMAL) &&
520 !is_pcie) {
521 add_ivhd_dev_entry(parent, dev, current, length, 0x42, 0x0);
522 /* Device is legacy PCI or PCI-X */
523 }
524 }
525 }
526 }
527
528 for (link = dev->link_list; link; link = link->next)
529 for (sibling = link->children; sibling; sibling =
530 sibling->sibling)
531 add_ivrs_device_entries(dev, sibling, depth + 1, depth,
532 root_level, current, length);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500533}
534
535unsigned long acpi_fill_ivrs_ioapic(acpi_ivrs_t *ivrs, unsigned long current)
536{
537 uint8_t *p;
538
539 uint32_t apicid_sb800;
540 uint32_t apicid_northbridge;
541
542 apicid_sb800 = CONFIG_MAX_CPUS;
543 apicid_northbridge = CONFIG_MAX_CPUS + 1;
544
545 /* Describe NB IOAPIC */
546 p = (uint8_t *)current;
547 p[0] = 0x48; /* Entry type */
548 p[1] = 0; /* Device */
549 p[2] = 0; /* Bus */
550 p[3] = 0x0; /* Data */
551 p[4] = apicid_northbridge; /* IOAPIC ID */
552 p[5] = 0x0; /* Device 0 Function 0 */
553 p[6] = 0x0; /* Northbridge bus */
554 p[7] = 0x1; /* Variety */
555 current += 8;
556
557 /* Describe SB IOAPIC */
558 p = (uint8_t *)current;
559 p[0] = 0x48; /* Entry type */
560 p[1] = 0; /* Device */
561 p[2] = 0; /* Bus */
562 p[3] = 0xd7; /* Data */
563 p[4] = apicid_sb800; /* IOAPIC ID */
564 p[5] = 0x14 << 3; /* Device 0x14 Function 0 */
565 p[6] = 0x0; /* Southbridge bus */
566 p[7] = 0x1; /* Variety */
567 current += 8;
568
569 return current;
570}
571
572static unsigned long acpi_fill_ivrs(acpi_ivrs_t *ivrs, unsigned long current)
573{
574 uint8_t *p;
Piotr Król063e1562018-07-22 20:52:26 +0200575 acpi_ivrs_t *ivrs_agesa;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500576
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300577 struct device *nb_dev = pcidev_on_root(0x0, 0);
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500578 if (!nb_dev) {
579
580 printk(BIOS_WARNING, "%s: G-series northbridge device not present!\n", __func__);
581 printk(BIOS_WARNING, "%s: IVRS table not generated...\n", __func__);
582
583 return (unsigned long)ivrs;
584 }
585
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500586
Piotr Król063e1562018-07-22 20:52:26 +0200587 /* obtain IOMMU base address */
588 ivrs_agesa = agesawrapper_getlateinitptr(PICK_IVRS);
589 if (ivrs_agesa != NULL) {
590 ivrs->iv_info = 0x0;
591 /* Maximum supported virtual address size */
592 ivrs->iv_info |= (0x40 << 15);
593 /* Maximum supported physical address size */
594 ivrs->iv_info |= (0x30 << 8);
595 /* Guest virtual address width */
596 ivrs->iv_info |= (0x2 << 5);
597
598 ivrs->ivhd.type = 0x10;
599 ivrs->ivhd.flags = 0x0e;
600 /* Enable ATS support */
601 ivrs->ivhd.flags |= 0x10;
602 ivrs->ivhd.length = sizeof(struct acpi_ivrs_ivhd);
603 /* BDF <bus>:00.2 */
604 ivrs->ivhd.device_id = 0x2 | (nb_dev->bus->secondary << 8);
605 /* Capability block 0x40 (type 0xf, "Secure device") */
606 ivrs->ivhd.capability_offset = 0x40;
607 ivrs->ivhd.iommu_base_low = ivrs_agesa->ivhd.iommu_base_low;
608 ivrs->ivhd.iommu_base_high = ivrs_agesa->ivhd.iommu_base_high;
609 ivrs->ivhd.pci_segment_group = 0x0;
610 ivrs->ivhd.iommu_info = 0x0;
611 ivrs->ivhd.iommu_info |= (0x13 << 8);
612 /* use only performance counters related bits:
613 * PNCounters[16:13] and
614 * PNBanks[22:17],
615 * otherwise 0 */
616 ivrs->ivhd.iommu_feature_info =
617 ivrs_agesa->ivhd.iommu_feature_info & 0x7fe000;
618 } else {
619 printk(BIOS_WARNING, "%s: AGESA returned NULL IVRS\n", __func__);
620
621 return (unsigned long)ivrs;
622 }
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500623
624 /* Describe HPET */
625 p = (uint8_t *)current;
626 p[0] = 0x48; /* Entry type */
627 p[1] = 0; /* Device */
628 p[2] = 0; /* Bus */
629 p[3] = 0xd7; /* Data */
630 p[4] = 0x0; /* HPET number */
631 p[5] = 0x14 << 3; /* HPET device */
632 p[6] = nb_dev->bus->secondary; /* HPET bus */
633 p[7] = 0x2; /* Variety */
634 ivrs->ivhd.length += 8;
635 current += 8;
636
637 /* Describe PCI devices */
Jacob Garber293e6a92019-07-17 11:47:19 -0600638 int8_t root_level = -1;
639 add_ivrs_device_entries(NULL, all_devices, 0, -1, &root_level, &current,
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500640 &ivrs->ivhd.length);
641
642 /* Describe IOAPICs */
643 unsigned long prev_current = current;
644 current = acpi_fill_ivrs_ioapic(ivrs, current);
645 ivrs->ivhd.length += (current - prev_current);
646
647 return current;
648}
649
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300650static void northbridge_fill_ssdt_generator(struct device *device)
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200651{
652 msr_t msr;
653 char pscope[] = "\\_SB.PCI0";
654
655 acpigen_write_scope(pscope);
656 msr = rdmsr(TOP_MEM);
657 acpigen_write_name_dword("TOM1", msr.lo);
658 msr = rdmsr(TOP_MEM2);
659 /*
660 * Since XP only implements parts of ACPI 2.0, we can't use a qword
661 * here.
662 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
663 * slide 22ff.
664 * Shift value right by 20 bit to make it fit into 32bit,
665 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
666 */
667 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
668 acpigen_pop_len();
669}
670
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300671static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200672 unsigned long current,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200673 acpi_rsdp_t *rsdp)
674{
675 acpi_srat_t *srat;
676 acpi_slit_t *slit;
677 acpi_header_t *ssdt;
678 acpi_header_t *alib;
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500679 acpi_ivrs_t *ivrs;
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200680
681 /* HEST */
682 current = ALIGN(current, 8);
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100683 acpi_write_hest((void *)current, acpi_fill_hest);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200684 acpi_add_table(rsdp, (void *)current);
685 current += ((acpi_header_t *)current)->length;
686
Timothy Pearson9ef07d82016-06-13 13:48:58 -0500687 /* IVRS */
688 current = ALIGN(current, 8);
689 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
690 ivrs = (acpi_ivrs_t *) current;
691 acpi_create_ivrs(ivrs, acpi_fill_ivrs);
692 current += ivrs->header.length;
693 acpi_add_table(rsdp, ivrs);
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200694
695 /* SRAT */
696 current = ALIGN(current, 8);
697 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
698 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
699 if (srat != NULL) {
700 memcpy((void *)current, srat, srat->header.length);
701 srat = (acpi_srat_t *) current;
702 current += srat->header.length;
703 acpi_add_table(rsdp, srat);
704 } else {
705 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
706 }
707
708 /* SLIT */
709 current = ALIGN(current, 8);
710 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
711 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
712 if (slit != NULL) {
713 memcpy((void *)current, slit, slit->header.length);
714 slit = (acpi_slit_t *) current;
715 current += slit->header.length;
716 acpi_add_table(rsdp, slit);
717 } else {
718 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
719 }
720
721 /* ALIB */
722 current = ALIGN(current, 16);
723 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
724 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
725 if (alib != NULL) {
726 memcpy((void *)current, alib, alib->length);
727 alib = (acpi_header_t *) current;
728 current += alib->length;
729 acpi_add_table(rsdp, (void *)alib);
730 }
731 else {
732 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
733 }
734
735 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
736 /* SSDT */
737 current = ALIGN(current, 16);
738 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
739 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
740 if (ssdt != NULL) {
741 memcpy((void *)current, ssdt, ssdt->length);
742 ssdt = (acpi_header_t *) current;
743 current += ssdt->length;
744 }
745 else {
746 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
747 }
748 acpi_add_table(rsdp,ssdt);
749
750 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
751 return current;
752}
753
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600754static struct device_operations northbridge_operations = {
755 .read_resources = read_resources,
756 .set_resources = set_resources,
757 .enable_resources = pci_dev_enable_resources,
758 .init = northbridge_init,
Kyösti Mälkki0b5b5412014-11-26 08:11:07 +0200759 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
760 .write_acpi_tables = agesa_write_acpi_tables,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600761 .enable = 0,
762 .ops_pci = 0,
763};
764
765static const struct pci_driver family16_northbridge __pci_driver = {
766 .ops = &northbridge_operations,
767 .vendor = PCI_VENDOR_ID_AMD,
Marshall Dawson463f46e2016-10-14 20:46:08 -0600768 .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600769};
770
771static const struct pci_driver family10_northbridge __pci_driver = {
772 .ops = &northbridge_operations,
773 .vendor = PCI_VENDOR_ID_AMD,
774 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
775};
776
Dave Frodin891f71a2015-01-19 15:58:24 -0700777static void fam16_finalize(void *chip_info)
778{
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300779 struct device *dev;
Dave Frodin891f71a2015-01-19 15:58:24 -0700780 u32 value;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300781 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Dave Frodin891f71a2015-01-19 15:58:24 -0700782 pci_write_config32(dev, 0xF8, 0);
783 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
784
785 /* disable No Snoop */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +0300786 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200787 if (dev != NULL) {
788 value = pci_read_config32(dev, 0x60);
789 value &= ~(1 << 11);
790 pci_write_config32(dev, 0x60, value);
791 }
Dave Frodin891f71a2015-01-19 15:58:24 -0700792}
793
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +0300794struct chip_operations northbridge_amd_pi_00730F01_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600795 CHIP_NAME("AMD FAM16 Northbridge")
796 .enable_dev = 0,
Dave Frodin891f71a2015-01-19 15:58:24 -0700797 .final = fam16_finalize,
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600798};
799
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300800static void domain_read_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600801{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530802 unsigned int reg;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600803
804 /* Find the already assigned resource pairs */
805 get_fx_devs();
806 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
807 u32 base, limit;
808 base = f1_read_config32(reg);
809 limit = f1_read_config32(reg + 0x04);
810 /* Is this register allocated? */
811 if ((base & 3) != 0) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530812 unsigned int nodeid, reg_link;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300813 struct device *reg_dev;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600814 if (reg < 0xc0) { // mmio
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600815 nodeid = (limit & 0xf) + (base&0x30);
816 } else { // io
817 nodeid = (limit & 0xf) + ((base>>4)&0x30);
818 }
819 reg_link = (limit >> 4) & 7;
820 reg_dev = __f0_dev[nodeid];
821 if (reg_dev) {
822 /* Reserve the resource */
823 struct resource *res;
824 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
825 if (res) {
826 res->flags = 1;
827 }
828 }
829 }
830 }
831 /* FIXME: do we need to check extend conf space?
832 I don't believe that much preset value */
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600833 pci_domain_read_resources(dev);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600834}
835
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300836static void domain_enable_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600837{
Julius Wernercd49cce2019-03-05 16:53:33 -0800838#if CONFIG(BINARYPI_LEGACY_WRAPPER)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600839 /* Must be called after PCI enumeration and resource allocation */
840 if (!acpi_is_wakeup_s3())
841 AGESAWRAPPER(amdinitmid);
842
843 printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n");
Kyösti Mälkki903ce252016-11-25 11:21:02 +0200844#endif
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600845}
846
847#if CONFIG_HW_MEM_HOLE_SIZEK != 0
848struct hw_mem_hole_info {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530849 unsigned int hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600850 int node_id;
851};
852static struct hw_mem_hole_info get_hw_mem_hole_info(void)
853{
854 struct hw_mem_hole_info mem_hole;
855 int i;
856 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
857 mem_hole.node_id = -1;
858 for (i = 0; i < node_nums; i++) {
859 dram_base_mask_t d;
860 u32 hole;
861 d = get_dram_base_mask(i);
862 if (!(d.mask & 1)) continue; // no memory on this node
863 hole = pci_read_config32(__f1_dev[i], 0xf0);
864 if (hole & 2) { // we find the hole
865 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
866 mem_hole.node_id = i; // record the node No with hole
867 break; // only one hole
868 }
869 }
870
871 /* We need to double check if there is special set on base reg and limit reg
872 * are not continuous instead of hole, it will find out its hole_startk.
873 */
874 if (mem_hole.node_id == -1) {
875 resource_t limitk_pri = 0;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600876 for (i = 0; i < node_nums; i++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600877 dram_base_mask_t d;
878 resource_t base_k, limit_k;
879 d = get_dram_base_mask(i);
880 if (!(d.base & 1)) continue;
881 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
882 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
883 if (limitk_pri != base_k) { // we find the hole
Martin Roth468d02c2019-10-23 21:44:42 -0600884 mem_hole.hole_startk = (unsigned int)limitk_pri; // must beblow 4G
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600885 mem_hole.node_id = i;
886 break; //only one hole
887 }
888 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
889 limitk_pri = limit_k;
890 }
891 }
892 return mem_hole;
893}
894#endif
895
Kyösti Mälkki90ac7362018-05-20 20:59:52 +0300896static void domain_set_resources(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600897{
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600898 unsigned long mmio_basek;
899 u32 pci_tolm;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600900 int i, idx;
901 struct bus *link;
902#if CONFIG_HW_MEM_HOLE_SIZEK != 0
903 struct hw_mem_hole_info mem_hole;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600904#endif
905
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600906 pci_tolm = 0xffffffffUL;
907 for (link = dev->link_list; link; link = link->next) {
908 pci_tolm = find_pci_tolm(link);
909 }
910
911 // FIXME handle interleaved nodes. If you fix this here, please fix
912 // amdk8, too.
913 mmio_basek = pci_tolm >> 10;
914 /* Round mmio_basek to something the processor can support */
915 mmio_basek &= ~((1 << 6) -1);
916
917 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
918 // MMIO hole. If you fix this here, please fix amdk8, too.
919 /* Round the mmio hole to 64M */
920 mmio_basek &= ~((64*1024) - 1);
921
922#if CONFIG_HW_MEM_HOLE_SIZEK != 0
923 /* if the hw mem hole is already set in raminit stage, here we will compare
924 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
925 * use hole_basek as mmio_basek and we don't need to reset hole.
926 * otherwise We reset the hole to the mmio_basek
927 */
928
929 mem_hole = get_hw_mem_hole_info();
930
931 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
932 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
933 mmio_basek = mem_hole.hole_startk;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600934 }
935#endif
936
937 idx = 0x10;
938 for (i = 0; i < node_nums; i++) {
939 dram_base_mask_t d;
940 resource_t basek, limitk, sizek; // 4 1T
941
942 d = get_dram_base_mask(i);
943
944 if (!(d.mask & 1)) continue;
945 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200946 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600947
948 sizek = limitk - basek;
949
950 /* see if we need a hole from 0xa0000 to 0xbffff */
951 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
952 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
953 idx += 0x10;
954 basek = (8*64)+(16*16);
955 sizek = limitk - ((8*64)+(16*16));
956
957 }
958
959 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
960
961 /* split the region to accommodate pci memory space */
Elyes HAOUASa8131602016-09-19 10:27:57 -0600962 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600963 if (basek <= mmio_basek) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530964 unsigned int pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600965 pre_sizek = mmio_basek - basek;
Elyes HAOUASa8131602016-09-19 10:27:57 -0600966 if (pre_sizek > 0) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600967 ram_resource(dev, (idx | i), basek, pre_sizek);
968 idx += 0x10;
969 sizek -= pre_sizek;
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600970 }
971 basek = mmio_basek;
972 }
973 if ((basek + sizek) <= 4*1024*1024) {
974 sizek = 0;
975 }
976 else {
977 uint64_t topmem2 = bsp_topmem2();
978 basek = 4*1024*1024;
979 sizek = topmem2/1024 - basek;
980 }
981 }
982
983 ram_resource(dev, (idx | i), basek, sizek);
984 idx += 0x10;
985 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
986 i, mmio_basek, basek, limitk);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600987 }
988
Kyösti Mälkkie87564f2017-04-15 20:07:53 +0300989 add_uma_resource_below_tolm(dev, 7);
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600990
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200991 for (link = dev->link_list; link; link = link->next) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -0600992 if (link->children) {
993 assign_resources(link);
994 }
995 }
996}
997
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600998static const char *domain_acpi_name(const struct device *dev)
Philipp Deppenwiese30670122017-03-01 02:24:33 +0100999{
1000 if (dev->path.type == DEVICE_PATH_DOMAIN)
1001 return "PCI0";
1002
1003 return NULL;
1004}
1005
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001006static struct device_operations pci_domain_ops = {
1007 .read_resources = domain_read_resources,
1008 .set_resources = domain_set_resources,
1009 .enable_resources = domain_enable_resources,
1010 .init = NULL,
1011 .scan_bus = pci_domain_scan_bus,
Philipp Deppenwiese30670122017-03-01 02:24:33 +01001012 .acpi_name = domain_acpi_name,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001013};
1014
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001015static void sysconf_init(struct device *dev) // first node
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001016{
1017 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
1018 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
1019}
1020
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001021static void cpu_bus_scan(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001022{
1023 struct bus *cpu_bus;
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001024 struct device *dev_mc;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001025 int i,j;
1026 int coreid_bits;
1027 int core_max = 0;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301028 unsigned int ApicIdCoreIdSize;
1029 unsigned int core_nums;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001030 int siblings = 0;
1031 unsigned int family;
1032 u32 modules = 0;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001033 int ioapic_count = 0;
1034
Michał Żygowskie7192882019-11-23 19:02:19 +01001035 /* For binaryPI there is no multiprocessor configuration, the number of
1036 * modules will always be 1. */
1037 modules = 1;
1038 ioapic_count = CONFIG_NUM_OF_IOAPICS;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001039
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001040 dev_mc = pcidev_on_root(DEV_CDB, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001041 if (!dev_mc) {
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001042 printk(BIOS_ERR, "0:%02x.0 not found", DEV_CDB);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001043 die("");
1044 }
1045 sysconf_init(dev_mc);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001046
1047 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +03001048 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001049 core_max = 1 << (coreid_bits & 0x000F); //mnc
1050
1051 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
1052 if (ApicIdCoreIdSize) {
1053 core_nums = (1 << ApicIdCoreIdSize) - 1;
1054 } else {
1055 core_nums = 3; //quad core
1056 }
1057
1058 /* Find which cpus are present */
1059 cpu_bus = dev->link_list;
1060 for (i = 0; i < node_nums; i++) {
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001061 struct device *cdb_dev;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301062 unsigned int devn;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001063 struct bus *pbus;
1064
Kyösti Mälkkibbd23772019-01-10 05:41:23 +02001065 devn = DEV_CDB + i;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001066 pbus = dev_mc->bus;
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001067
1068 /* Find the cpu's pci device */
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001069 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001070 if (!cdb_dev) {
1071 /* If I am probing things in a weird order
1072 * ensure all of the cpu's pci devices are found.
1073 */
1074 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +02001075 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001076 cdb_dev = pci_probe_dev(NULL, pbus,
1077 PCI_DEVFN(devn, fn));
1078 }
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001079 cdb_dev = pcidev_on_root(devn, 0);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001080 } else {
1081 /* Ok, We need to set the links for that device.
1082 * otherwise the device under it will not be scanned
1083 */
Kyösti Mälkkic5163ed82015-02-04 13:25:37 +02001084
1085 add_more_links(cdb_dev, 4);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001086 }
1087
1088 family = cpuid_eax(1);
1089 family = (family >> 20) & 0xFF;
1090 if (family == 1) { //f10
1091 u32 dword;
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001092 cdb_dev = pcidev_on_root(devn, 3);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001093 dword = pci_read_config32(cdb_dev, 0xe8);
1094 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
1095 } else if (family == 7) {//f16
Kyösti Mälkki33ff44c2018-05-22 01:15:22 +03001096 cdb_dev = pcidev_on_root(devn, 5);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001097 if (cdb_dev && cdb_dev->enabled) {
1098 siblings = pci_read_config32(cdb_dev, 0x84);
1099 siblings &= 0xFF;
1100 }
1101 } else {
1102 siblings = 0; //default one core
1103 }
1104 int enable_node = cdb_dev && cdb_dev->enabled;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001105 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 -06001106 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
1107
Elyes HAOUASa8131602016-09-19 10:27:57 -06001108 for (j = 0; j <= siblings; j++) {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001109 u32 lapicid_start = 0;
1110
1111 /*
1112 * APIC ID calucation is tightly coupled with AGESA v5 code.
1113 * This calculation MUST match the assignment calculation done
1114 * in LocalApicInitializationAtEarly() function.
1115 * And reference GetLocalApicIdForCore()
1116 *
1117 * Apply apic enumeration rules
1118 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
1119 * put the local-APICs at m..z
1120 *
1121 * This is needed because many IO-APIC devices only have 4 bits
1122 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +02001123 */
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001124 if ((node_nums * core_max) + ioapic_count >= 0x10) {
1125 lapicid_start = (ioapic_count - 1) / core_max;
1126 lapicid_start = (lapicid_start + 1) * core_max;
Elyes HAOUASa8131602016-09-19 10:27:57 -06001127 printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001128 }
1129 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
Elyes HAOUASa8131602016-09-19 10:27:57 -06001130 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n",
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001131 i, j, apic_id);
1132
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001133 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001134 if (cpu)
1135 amd_cpu_topology(cpu, i, j);
1136 } //j
1137 }
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001138}
1139
Kyösti Mälkki90ac7362018-05-20 20:59:52 +03001140static void cpu_bus_init(struct device *dev)
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001141{
1142 initialize_cpus(dev->link_list);
1143}
1144
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001145static struct device_operations cpu_bus_ops = {
Kyösti Mälkki48f82a92016-12-02 16:02:30 +02001146 .read_resources = DEVICE_NOOP,
1147 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001148 .enable_resources = DEVICE_NOOP,
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001149 .init = cpu_bus_init,
1150 .scan_bus = cpu_bus_scan,
1151};
1152
1153static void root_complex_enable_dev(struct device *dev)
1154{
1155 static int done = 0;
1156
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001157 if (!done) {
1158 setup_bsp_ramtop();
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001159 done = 1;
1160 }
1161
1162 /* Set the operations if it is a special bus type */
1163 if (dev->path.type == DEVICE_PATH_DOMAIN) {
1164 dev->ops = &pci_domain_ops;
1165 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
1166 dev->ops = &cpu_bus_ops;
1167 }
1168}
1169
Kyösti Mälkkie4c17ce2014-10-21 18:22:32 +03001170struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = {
Bruce Griffith27ed80b2014-08-15 11:46:25 -06001171 CHIP_NAME("AMD FAM16 Root Complex")
1172 .enable_dev = root_complex_enable_dev,
1173};
1174
1175/*********************************************************************
1176 * Change the vendor / device IDs to match the generic VBIOS header. *
1177 *********************************************************************/
1178u32 map_oprom_vendev(u32 vendev)
1179{
1180 u32 new_vendev;
1181 new_vendev =
1182 ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev;
1183
1184 if (vendev != new_vendev)
1185 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
1186
1187 return new_vendev;
1188}