blob: 7a94aa583039ceb016e4433488e44e662942e382 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Siyuan Wang3e32cc02013-07-09 17:16:20 +08002
3#include <console/console.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02004#include <device/pci_ops.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
6#include <acpi/acpigen.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +08007#include <stdint.h>
8#include <device/device.h>
9#include <device/pci.h>
10#include <device/pci_ids.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080011#include <string.h>
12#include <lib.h>
13#include <cpu/cpu.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080014#include <cpu/x86/lapic.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020015#include <cpu/amd/msr.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080016#include <cpu/amd/mtrr.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080017#include <Porting.h>
18#include <AGESA.h>
19#include <Options.h>
20#include <Topology.h>
Angel Ponsec5cf152020-11-10 20:42:07 +010021#include <northbridge/amd/nb_common.h>
Kyösti Mälkki28c4d2f2016-11-25 11:21:02 +020022#include <northbridge/amd/agesa/state_machine.h>
Kyösti Mälkkid610c582017-03-05 06:28:18 +020023#include <northbridge/amd/agesa/agesa_helper.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080024
Kyösti Mälkki113f6702018-05-20 20:12:32 +030025#define MAX_NODE_NUMS MAX_NODES
Siyuan Wang3e32cc02013-07-09 17:16:20 +080026
Subrata Banikb1434fc2019-03-15 22:20:41 +053027static unsigned int node_nums;
28static unsigned int sblink;
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030029static struct device *__f0_dev[MAX_NODE_NUMS];
30static struct device *__f1_dev[MAX_NODE_NUMS];
31static struct device *__f2_dev[MAX_NODE_NUMS];
32static struct device *__f4_dev[MAX_NODE_NUMS];
Subrata Banikb1434fc2019-03-15 22:20:41 +053033static unsigned int fx_devs = 0;
Siyuan Wang3e32cc02013-07-09 17:16:20 +080034
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030035static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Siyuan Wang3e32cc02013-07-09 17:16:20 +080036 u32 io_min, u32 io_max)
37{
38 u32 i;
39 u32 tempreg;
40 /* io range allocation */
Elyes HAOUAS27e18012017-06-27 23:14:51 +020041 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn << 4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020042 for (i = 0; i < node_nums; i++)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080043 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUAS27e18012017-06-27 23:14:51 +020044 tempreg = 3 /*| (3 << 4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020045 for (i = 0; i < node_nums; i++)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080046 pci_write_config32(__f1_dev[i], reg, tempreg);
47}
48
49static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
50{
51 u32 i;
52 u32 tempreg;
53 /* io range allocation */
Elyes HAOUAS27e18012017-06-27 23:14:51 +020054 tempreg = (nodeid&0xf) | (linkn << 4) | (mmio_max&0xffffff00); //limit
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020055 for (i = 0; i < nodes; i++)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080056 pci_write_config32(__f1_dev[i], reg+4, tempreg);
57 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020058 for (i = 0; i < node_nums; i++)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080059 pci_write_config32(__f1_dev[i], reg, tempreg);
60}
61
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030062static struct device *get_node_pci(u32 nodeid, u32 fn)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080063{
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +020064 return pcidev_on_root(DEV_CDB + nodeid, fn);
Siyuan Wang3e32cc02013-07-09 17:16:20 +080065}
66
67static void get_fx_devs(void)
68{
69 int i;
70 for (i = 0; i < MAX_NODE_NUMS; i++) {
71 __f0_dev[i] = get_node_pci(i, 0);
72 __f1_dev[i] = get_node_pci(i, 1);
73 __f2_dev[i] = get_node_pci(i, 2);
74 __f4_dev[i] = get_node_pci(i, 4);
75 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
76 fx_devs = i+1;
77 }
78 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
79 die("Cannot find 0:0x18.[0|1]\n");
80 }
81 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
82}
83
Subrata Banikb1434fc2019-03-15 22:20:41 +053084static u32 f1_read_config32(unsigned int reg)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080085{
86 if (fx_devs == 0)
87 get_fx_devs();
88 return pci_read_config32(__f1_dev[0], reg);
89}
90
Subrata Banikb1434fc2019-03-15 22:20:41 +053091static void f1_write_config32(unsigned int reg, u32 value)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080092{
93 int i;
94 if (fx_devs == 0)
95 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +020096 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030097 struct device *dev;
Siyuan Wang3e32cc02013-07-09 17:16:20 +080098 dev = __f1_dev[i];
99 if (dev && dev->enabled) {
100 pci_write_config32(dev, reg, value);
101 }
102 }
103}
104
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200105static int get_dram_base_limit(u32 nodeid, resource_t *basek, resource_t *limitk)
106{
107 u32 temp;
108
109 if (fx_devs == 0)
110 get_fx_devs();
111
112
113 temp = pci_read_config32(__f1_dev[nodeid], 0x40 + (nodeid << 3)); //[39:24] at [31:16]
114 if (!(temp & 1))
115 return 0; // this memory range is not enabled
116 /*
117 * BKDG: {DramBase[39:24], 00_0000h} <= address[39:0] so shift left by 8 bits
118 * for physical address and the convert to KiB by shifting 10 bits left
119 */
120 *basek = ((temp & 0xffff0000)) >> (10 - 8);
121 /*
122 * BKDG address[39:0] <= {DramLimit[39:24], FF_FFFFh} converted as above but
123 * ORed with 0xffff to get real limit before shifting.
124 */
125 temp = pci_read_config32(__f1_dev[nodeid], 0x44 + (nodeid << 3)); //[39:24] at [31:16]
126 *limitk = ((temp & 0xffff0000) | 0xffff) >> (10 - 8);
127 *limitk += 1; // round up last byte
128
129 return 1;
130}
131
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300132static u32 amdfam16_nodeid(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800133{
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200134 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800135}
136
137static void set_vga_enable_reg(u32 nodeid, u32 linkn)
138{
139 u32 val;
140
Elyes HAOUAS27e18012017-06-27 23:14:51 +0200141 val = 1 | (nodeid << 4) | (linkn << 12);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800142 /* it will routing
143 * (1)mmio 0xa0000:0xbffff
144 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
145 */
146 f1_write_config32(0xf4, val);
147
148}
149
150/**
151 * @return
Elyes HAOUAS99b075a2019-12-30 14:29:31 +0100152 * @retval 2 resource does not exist, usable
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800153 * @retval 0 resource exists, not usable
154 * @retval 1 resource exist, resource has been allocated before
155 */
Subrata Banikb1434fc2019-03-15 22:20:41 +0530156static int reg_useable(unsigned int reg, struct device *goal_dev,
157 unsigned int goal_nodeid, unsigned int goal_link)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800158{
159 struct resource *res;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530160 unsigned int nodeid, link = 0;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800161 int result;
162 res = 0;
163 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300164 struct device *dev;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800165 dev = __f0_dev[nodeid];
166 if (!dev)
167 continue;
168 for (link = 0; !res && (link < 8); link++) {
169 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
170 }
171 }
172 result = 2;
173 if (res) {
174 result = 0;
175 if ((goal_link == (link - 1)) &&
176 (goal_nodeid == (nodeid - 1)) &&
177 (res->flags <= 1)) {
178 result = 1;
179 }
180 }
181 return result;
182}
183
Subrata Banikb1434fc2019-03-15 22:20:41 +0530184static struct resource *amdfam16_find_iopair(struct device *dev,
185 unsigned int nodeid, unsigned int link)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800186{
187 struct resource *resource;
188 u32 free_reg, reg;
189 resource = 0;
190 free_reg = 0;
191 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
192 int result;
193 result = reg_useable(reg, dev, nodeid, link);
194 if (result == 1) {
195 /* I have been allocated this one */
196 break;
197 }
198 else if (result > 1) {
199 /* I have a free register pair */
200 free_reg = reg;
201 }
202 }
203 if (reg > 0xd8) {
204 reg = free_reg; // if no free, the free_reg still be 0
205 }
206
207 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
208
209 return resource;
210}
211
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300212static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800213{
214 struct resource *resource;
215 u32 free_reg, reg;
216 resource = 0;
217 free_reg = 0;
218 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
219 int result;
220 result = reg_useable(reg, dev, nodeid, link);
221 if (result == 1) {
222 /* I have been allocated this one */
223 break;
224 }
225 else if (result > 1) {
226 /* I have a free register pair */
227 free_reg = reg;
228 }
229 }
230 if (reg > 0xb8) {
231 reg = free_reg;
232 }
233
234 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
235 return resource;
236}
237
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300238static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800239{
240 struct resource *resource;
241
242 /* Initialize the io space constraints on the current bus */
243 resource = amdfam16_find_iopair(dev, nodeid, link);
244 if (resource) {
245 u32 align;
246 align = log2(HT_IO_HOST_ALIGN);
247 resource->base = 0;
248 resource->size = 0;
249 resource->align = align;
250 resource->gran = align;
251 resource->limit = 0xffffUL;
252 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
253 }
254
255 /* Initialize the prefetchable memory constraints on the current bus */
256 resource = amdfam16_find_mempair(dev, nodeid, link);
257 if (resource) {
258 resource->base = 0;
259 resource->size = 0;
260 resource->align = log2(HT_MEM_HOST_ALIGN);
261 resource->gran = log2(HT_MEM_HOST_ALIGN);
262 resource->limit = 0xffffffffffULL;
263 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
264 resource->flags |= IORESOURCE_BRIDGE;
265 }
266
267 /* Initialize the memory constraints on the current bus */
268 resource = amdfam16_find_mempair(dev, nodeid, link);
269 if (resource) {
270 resource->base = 0;
271 resource->size = 0;
272 resource->align = log2(HT_MEM_HOST_ALIGN);
273 resource->gran = log2(HT_MEM_HOST_ALIGN);
274 resource->limit = 0xffffffffffULL;
275 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
276 }
277
278}
279
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300280static void read_resources(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800281{
282 u32 nodeid;
283 struct bus *link;
284
285 nodeid = amdfam16_nodeid(dev);
286 for (link = dev->link_list; link; link = link->next) {
287 if (link->children) {
288 amdfam16_link_read_bases(dev, nodeid, link->link_num);
289 }
290 }
Edward O'Callaghan66c65322014-11-21 01:43:38 +1100291
292 /*
293 * This MMCONF resource must be reserved in the PCI_DOMAIN.
294 * It is not honored by the coreboot resource allocator if it is in
295 * the APIC_CLUSTER.
296 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200297 mmconf_resource(dev, MMIO_CONF_BASE);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800298}
299
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300300static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800301{
302 resource_t rbase, rend;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530303 unsigned int reg, link_num;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800304 char buf[50];
305
306 /* Make certain the resource has actually been set */
307 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
308 return;
309 }
310
311 /* If I have already stored this resource don't worry about it */
312 if (resource->flags & IORESOURCE_STORED) {
313 return;
314 }
315
316 /* Only handle PCI memory and IO resources */
317 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
318 return;
319
320 /* Ensure I am actually looking at a resource of function 1 */
321 if ((resource->index & 0xffff) < 0x1000) {
322 return;
323 }
324 /* Get the base address */
325 rbase = resource->base;
326
327 /* Get the limit (rounded up) */
328 rend = resource_end(resource);
329
330 /* Get the register and link */
331 reg = resource->index & 0xfff; // 4k
332 link_num = IOINDEX_LINK(resource->index);
333
334 if (resource->flags & IORESOURCE_IO) {
335 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
336 }
337 else if (resource->flags & IORESOURCE_MEM) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100338 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums);// [39:8]
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800339 }
340 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200341 snprintf(buf, sizeof(buf), " <node %x link %x>",
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800342 nodeid, link_num);
343 report_resource_stored(dev, resource, buf);
344}
345
346/**
347 * I tried to reuse the resource allocation code in set_resource()
348 * but it is too difficult to deal with the resource allocation magic.
349 */
350
Subrata Banikb1434fc2019-03-15 22:20:41 +0530351static void create_vga_resource(struct device *dev, unsigned int nodeid)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800352{
353 struct bus *link;
354
355 /* find out which link the VGA card is connected,
356 * we only deal with the 'first' vga card */
357 for (link = dev->link_list; link; link = link->next) {
358 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800359#if CONFIG(MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300360 extern struct device *vga_pri; // the primary vga device, defined in device.c
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800361 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
362 link->secondary,link->subordinate);
363 /* We need to make sure the vga_pri is under the link */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200364 if ((vga_pri->bus->secondary >= link->secondary) &&
365 (vga_pri->bus->secondary <= link->subordinate))
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800366#endif
367 break;
368 }
369 }
370
371 /* no VGA card installed */
372 if (link == NULL)
373 return;
374
375 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
376 set_vga_enable_reg(nodeid, sblink);
377}
378
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300379static void set_resources(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800380{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530381 unsigned int nodeid;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800382 struct bus *bus;
383 struct resource *res;
384
385 /* Find the nodeid */
386 nodeid = amdfam16_nodeid(dev);
387
388 create_vga_resource(dev, nodeid); //TODO: do we need this?
389
390 /* Set each resource we have found */
391 for (res = dev->resource_list; res; res = res->next) {
392 set_resource(dev, res, nodeid);
393 }
394
395 for (bus = dev->link_list; bus; bus = bus->next) {
396 if (bus->children) {
397 assign_resources(bus);
398 }
399 }
400}
401
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100402static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200403{
404 void *addr, *current;
405
406 /* Skip the HEST header. */
407 current = (void *)(hest + 1);
408
409 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
410 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700411 current += acpi_create_hest_error_source(hest, current, 0,
412 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200413
414 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
415 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700416 current += acpi_create_hest_error_source(hest, current, 1,
417 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200418
419 return (unsigned long)current;
420}
421
Furquan Shaikh7536a392020-04-24 21:59:21 -0700422static void northbridge_fill_ssdt_generator(const struct device *device)
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200423{
424 msr_t msr;
425 char pscope[] = "\\_SB.PCI0";
426
427 acpigen_write_scope(pscope);
428 msr = rdmsr(TOP_MEM);
429 acpigen_write_name_dword("TOM1", msr.lo);
430 msr = rdmsr(TOP_MEM2);
431 /*
432 * Since XP only implements parts of ACPI 2.0, we can't use a qword
433 * here.
434 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
435 * slide 22ff.
436 * Shift value right by 20 bit to make it fit into 32bit,
437 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
438 */
439 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
440 acpigen_pop_len();
441}
442
Michał Żygowski9550e972020-03-20 13:56:46 +0100443static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
444{
445 unsigned int len = ssdt->length - sizeof(acpi_header_t);
446 unsigned int i;
447
448 for (i = sizeof(acpi_header_t); i < len; i++) {
449 /* Search for _PR_ scope and replace it with _SB_ */
450 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
451 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
452 }
453 /* Recalculate checksum */
454 ssdt->checksum = 0;
455 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
456}
457
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700458static unsigned long agesa_write_acpi_tables(const struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200459 unsigned long current,
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200460 acpi_rsdp_t *rsdp)
461{
462 acpi_srat_t *srat;
463 acpi_slit_t *slit;
464 acpi_header_t *ssdt;
465 acpi_header_t *alib;
466 acpi_header_t *ivrs;
467 acpi_hest_t *hest;
468
469 /* HEST */
470 current = ALIGN(current, 8);
471 hest = (acpi_hest_t *)current;
Arthur Heymanscc66ff32022-03-23 21:33:15 +0100472 acpi_write_hest(hest, acpi_fill_hest);
473 acpi_add_table(rsdp, hest);
474 current += hest->header.length;
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200475
476 current = ALIGN(current, 8);
477 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
478 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
479 if (ivrs != NULL) {
480 memcpy((void *)current, ivrs, ivrs->length);
481 ivrs = (acpi_header_t *) current;
482 current += ivrs->length;
483 acpi_add_table(rsdp, ivrs);
484 } else {
485 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
486 }
487
488 /* SRAT */
489 current = ALIGN(current, 8);
490 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
491 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
492 if (srat != NULL) {
493 memcpy((void *)current, srat, srat->header.length);
494 srat = (acpi_srat_t *) current;
495 current += srat->header.length;
496 acpi_add_table(rsdp, srat);
497 } else {
498 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
499 }
500
501 /* SLIT */
502 current = ALIGN(current, 8);
503 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
504 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
505 if (slit != NULL) {
506 memcpy((void *)current, slit, slit->header.length);
507 slit = (acpi_slit_t *) current;
508 current += slit->header.length;
509 acpi_add_table(rsdp, slit);
510 } else {
511 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
512 }
513
514 /* ALIB */
515 current = ALIGN(current, 16);
516 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
517 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
518 if (alib != NULL) {
519 memcpy((void *)current, alib, alib->length);
520 alib = (acpi_header_t *) current;
521 current += alib->length;
522 acpi_add_table(rsdp, (void *)alib);
523 }
524 else {
525 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
526 }
527
528 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
529 /* SSDT */
530 current = ALIGN(current, 16);
531 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
532 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
533 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100534 patch_ssdt_processor_scope(ssdt);
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200535 memcpy((void *)current, ssdt, ssdt->length);
536 ssdt = (acpi_header_t *) current;
537 current += ssdt->length;
538 }
539 else {
540 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
541 }
542 acpi_add_table(rsdp,ssdt);
543
544 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
545
546 return current;
547}
548
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800549static struct device_operations northbridge_operations = {
550 .read_resources = read_resources,
551 .set_resources = set_resources,
552 .enable_resources = pci_dev_enable_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200553 .acpi_fill_ssdt = northbridge_fill_ssdt_generator,
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200554 .write_acpi_tables = agesa_write_acpi_tables,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800555};
556
557static const struct pci_driver family16_northbridge __pci_driver = {
558 .ops = &northbridge_operations,
Felix Singer43b7f412022-03-07 04:34:52 +0100559 .vendor = PCI_VID_AMD,
560 .device = PCI_DID_AMD_16H_MODEL_000F_NB_HT,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800561};
562
563static const struct pci_driver family10_northbridge __pci_driver = {
564 .ops = &northbridge_operations,
Felix Singer43b7f412022-03-07 04:34:52 +0100565 .vendor = PCI_VID_AMD,
566 .device = PCI_DID_AMD_10H_NB_HT,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800567};
568
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200569static void fam16_finalize(void *chip_info)
570{
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300571 struct device *dev;
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200572 u32 value;
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300573 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200574 pci_write_config32(dev, 0xF8, 0);
575 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
576
577 /* disable No Snoop */
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300578 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200579 if (dev != NULL) {
580 value = pci_read_config32(dev, 0x60);
581 value &= ~(1 << 11);
582 pci_write_config32(dev, 0x60, value);
583 }
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200584}
585
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800586struct chip_operations northbridge_amd_agesa_family16kb_ops = {
587 CHIP_NAME("AMD FAM16 Northbridge")
588 .enable_dev = 0,
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200589 .final = fam16_finalize,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800590};
591
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300592static void domain_read_resources(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800593{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530594 unsigned int reg;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800595
596 /* Find the already assigned resource pairs */
597 get_fx_devs();
598 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
599 u32 base, limit;
600 base = f1_read_config32(reg);
601 limit = f1_read_config32(reg + 0x04);
602 /* Is this register allocated? */
603 if ((base & 3) != 0) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530604 unsigned int nodeid, reg_link;
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300605 struct device *reg_dev;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200606 if (reg < 0xc0) { // mmio
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800607 nodeid = (limit & 0xf) + (base&0x30);
608 } else { // io
609 nodeid = (limit & 0xf) + ((base>>4)&0x30);
610 }
611 reg_link = (limit >> 4) & 7;
612 reg_dev = __f0_dev[nodeid];
613 if (reg_dev) {
614 /* Reserve the resource */
615 struct resource *res;
616 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
617 if (res) {
618 res->flags = 1;
619 }
620 }
621 }
622 }
623 /* FIXME: do we need to check extend conf space?
624 I don't believe that much preset value */
625
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800626 pci_domain_read_resources(dev);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800627}
628
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800629#if CONFIG_HW_MEM_HOLE_SIZEK != 0
630struct hw_mem_hole_info {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530631 unsigned int hole_startk;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800632 int node_id;
633};
634static struct hw_mem_hole_info get_hw_mem_hole_info(void)
635{
636 struct hw_mem_hole_info mem_hole;
637 int i;
638 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
639 mem_hole.node_id = -1;
640 for (i = 0; i < node_nums; i++) {
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200641 resource_t basek, limitk;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800642 u32 hole;
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200643 if (!get_dram_base_limit(i, &basek, &limitk))
644 continue; // no memory on this node
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800645 hole = pci_read_config32(__f1_dev[i], 0xf0);
646 if (hole & 2) { // we find the hole
Elyes HAOUAS27e18012017-06-27 23:14:51 +0200647 mem_hole.hole_startk = (hole & (0xff << 24)) >> 10;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800648 mem_hole.node_id = i; // record the node No with hole
649 break; // only one hole
650 }
651 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300652
653 /* We need to double check if there is special set on base reg and limit reg
654 * are not continuous instead of hole, it will find out its hole_startk.
655 */
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800656 if (mem_hole.node_id == -1) {
657 resource_t limitk_pri = 0;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200658 for (i = 0; i < node_nums; i++) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800659 resource_t base_k, limit_k;
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200660 if (!get_dram_base_limit(i, &base_k, &limit_k))
661 continue; // no memory on this node
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800662 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
663 if (limitk_pri != base_k) { // we find the hole
Martin Roth468d02c2019-10-23 21:44:42 -0600664 mem_hole.hole_startk = (unsigned int)limitk_pri; // must beblow 4G
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800665 mem_hole.node_id = i;
666 break; //only one hole
667 }
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800668 limitk_pri = limit_k;
669 }
670 }
671 return mem_hole;
672}
673#endif
674
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300675static void domain_set_resources(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800676{
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800677 unsigned long mmio_basek;
678 u32 pci_tolm;
679 int i, idx;
680 struct bus *link;
681#if CONFIG_HW_MEM_HOLE_SIZEK != 0
682 struct hw_mem_hole_info mem_hole;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800683#endif
684
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800685 pci_tolm = 0xffffffffUL;
686 for (link = dev->link_list; link; link = link->next) {
687 pci_tolm = find_pci_tolm(link);
688 }
689
690 // FIXME handle interleaved nodes. If you fix this here, please fix
691 // amdk8, too.
692 mmio_basek = pci_tolm >> 10;
693 /* Round mmio_basek to something the processor can support */
694 mmio_basek &= ~((1 << 6) -1);
695
696 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
697 // MMIO hole. If you fix this here, please fix amdk8, too.
698 /* Round the mmio hole to 64M */
699 mmio_basek &= ~((64*1024) - 1);
700
701#if CONFIG_HW_MEM_HOLE_SIZEK != 0
702 /* if the hw mem hole is already set in raminit stage, here we will compare
703 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
704 * use hole_basek as mmio_basek and we don't need to reset hole.
705 * otherwise We reset the hole to the mmio_basek
706 */
707
708 mem_hole = get_hw_mem_hole_info();
709
710 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
Arthur Heymans99eab342022-03-23 21:34:20 +0100711 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk))
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800712 mmio_basek = mem_hole.hole_startk;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800713#endif
714
715 idx = 0x10;
716 for (i = 0; i < node_nums; i++) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800717 resource_t basek, limitk, sizek; // 4 1T
718
Michał Żygowski88a0ce62021-05-05 09:52:59 +0200719 if (!get_dram_base_limit(i, &basek, &limitk))
720 continue; // no memory on this node
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800721
722 sizek = limitk - basek;
723
Elyes Haouas5213b192022-02-25 18:13:03 +0100724 /* See if we need a hole from 0xa0000 (640K) to 0xbffff (768K) */
Elyes Haouas9d8df302022-02-25 18:23:01 +0100725 if (basek < 640 && sizek > 768) {
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300726 ram_resource_kb(dev, (idx | i), basek, 640 - basek);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800727 idx += 0x10;
Elyes Haouas9d8df302022-02-25 18:23:01 +0100728 basek = 768;
729 sizek = limitk - basek;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800730 }
731
732 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
733
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300734 /* split the region to accommodate pci memory space */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200735 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800736 if (basek <= mmio_basek) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530737 unsigned int pre_sizek;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800738 pre_sizek = mmio_basek - basek;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200739 if (pre_sizek > 0) {
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300740 ram_resource_kb(dev, (idx | i), basek, pre_sizek);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800741 idx += 0x10;
742 sizek -= pre_sizek;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800743 }
744 basek = mmio_basek;
745 }
746 if ((basek + sizek) <= 4*1024*1024) {
747 sizek = 0;
748 }
749 else {
Arthur Heymansc4350382021-10-28 12:35:39 +0200750 uint64_t topmem2 = amd_topmem2();
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800751 basek = 4*1024*1024;
752 sizek = topmem2/1024 - basek;
753 }
754 }
755
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300756 ram_resource_kb(dev, (idx | i), basek, sizek);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800757 idx += 0x10;
758 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
759 i, mmio_basek, basek, limitk);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800760 }
761
Kyösti Mälkki61be3602017-04-15 20:07:53 +0300762 add_uma_resource_below_tolm(dev, 7);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800763
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200764 for (link = dev->link_list; link; link = link->next) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800765 if (link->children) {
766 assign_resources(link);
767 }
768 }
769}
770
Kevin Cody-Little06d23232018-05-09 14:22:15 -0400771static const char *domain_acpi_name(const struct device *dev)
772{
773 if (dev->path.type == DEVICE_PATH_DOMAIN)
774 return "PCI0";
775
776 return NULL;
777}
778
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800779static struct device_operations pci_domain_ops = {
780 .read_resources = domain_read_resources,
781 .set_resources = domain_set_resources,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800782 .scan_bus = pci_domain_scan_bus,
Kevin Cody-Little06d23232018-05-09 14:22:15 -0400783 .acpi_name = domain_acpi_name,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800784};
785
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300786static void sysconf_init(struct device *dev) // first node
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800787{
788 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
789 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
790}
791
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300792static void cpu_bus_scan(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800793{
794 struct bus *cpu_bus;
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300795 struct device *dev_mc;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800796 int i,j;
797 int coreid_bits;
798 int core_max = 0;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530799 unsigned int ApicIdCoreIdSize;
800 unsigned int core_nums;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800801 int siblings = 0;
802 unsigned int family;
803
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200804 dev_mc = pcidev_on_root(DEV_CDB, 0);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800805 if (!dev_mc) {
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200806 printk(BIOS_ERR, "0:%02x.0 not found", DEV_CDB);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800807 die("");
808 }
809 sysconf_init(dev_mc);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800810
811 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +0300812 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800813 core_max = 1 << (coreid_bits & 0x000F); //mnc
814
815 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
816 if (ApicIdCoreIdSize) {
817 core_nums = (1 << ApicIdCoreIdSize) - 1;
818 } else {
819 core_nums = 3; //quad core
820 }
821
822 /* Find which cpus are present */
823 cpu_bus = dev->link_list;
824 for (i = 0; i < node_nums; i++) {
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300825 struct device *cdb_dev;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530826 unsigned int devn;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800827 struct bus *pbus;
828
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200829 devn = DEV_CDB + i;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800830 pbus = dev_mc->bus;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800831
832 /* Find the cpu's pci device */
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300833 cdb_dev = pcidev_on_root(devn, 0);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800834 if (!cdb_dev) {
835 /* If I am probing things in a weird order
836 * ensure all of the cpu's pci devices are found.
837 */
838 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200839 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800840 cdb_dev = pci_probe_dev(NULL, pbus,
841 PCI_DEVFN(devn, fn));
842 }
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300843 cdb_dev = pcidev_on_root(devn, 0);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800844 } else {
845 /* Ok, We need to set the links for that device.
846 * otherwise the device under it will not be scanned
847 */
Kyösti Mälkki2a2d6132015-02-04 13:25:37 +0200848 add_more_links(cdb_dev, 4);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800849 }
850
851 family = cpuid_eax(1);
852 family = (family >> 20) & 0xFF;
853 if (family == 1) { //f10
854 u32 dword;
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300855 cdb_dev = pcidev_on_root(devn, 3);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800856 dword = pci_read_config32(cdb_dev, 0xe8);
857 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
858 } else if (family == 7) {//f16
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300859 cdb_dev = pcidev_on_root(devn, 5);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800860 if (cdb_dev && cdb_dev->enabled) {
861 siblings = pci_read_config32(cdb_dev, 0x84);
862 siblings &= 0xFF;
863 }
864 } else {
865 siblings = 0; //default one core
866 }
867 int enable_node = cdb_dev && cdb_dev->enabled;
868 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
869 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
870
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200871 for (j = 0; j <= siblings; j++) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800872 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
873 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
874 u32 lapicid_start = 0;
875
876 /*
877 * APIC ID calucation is tightly coupled with AGESA v5 code.
878 * This calculation MUST match the assignment calculation done
879 * in LocalApicInitializationAtEarly() function.
880 * And reference GetLocalApicIdForCore()
881 *
Elyes HAOUASa5b0bc42020-02-20 20:04:29 +0100882 * Apply APIC enumeration rules
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800883 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
884 * put the local-APICs at m..z
885 *
886 * This is needed because many IO-APIC devices only have 4 bits
887 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +0200888 */
Kyösti Mälkki50036322016-05-18 13:35:21 +0300889
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +0200890 u8 plat_num_io_apics = 3; /* FIXME */
Kyösti Mälkki50036322016-05-18 13:35:21 +0300891
892 if ((node_nums * core_max) + plat_num_io_apics >= 0x10) {
893 lapicid_start = (plat_num_io_apics - 1) / core_max;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800894 lapicid_start = (lapicid_start + 1) * core_max;
895 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
896 }
897 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
898 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
899 i, j, apic_id);
900
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300901 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800902 if (cpu)
903 amd_cpu_topology(cpu, i, j);
904 } //j
905 }
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800906}
907
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300908static void cpu_bus_init(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800909{
910 initialize_cpus(dev->link_list);
911}
912
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800913static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200914 .read_resources = noop_read_resources,
915 .set_resources = noop_set_resources,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800916 .init = cpu_bus_init,
917 .scan_bus = cpu_bus_scan,
918};
919
920static void root_complex_enable_dev(struct device *dev)
921{
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800922 /* Set the operations if it is a special bus type */
923 if (dev->path.type == DEVICE_PATH_DOMAIN) {
924 dev->ops = &pci_domain_ops;
925 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
926 dev->ops = &cpu_bus_ops;
927 }
928}
929
930struct chip_operations northbridge_amd_agesa_family16kb_root_complex_ops = {
931 CHIP_NAME("AMD FAM16 Root Complex")
932 .enable_dev = root_complex_enable_dev,
933};
Bruce Griffith76db07e2013-07-07 02:06:53 -0600934
935/*********************************************************************
936 * Change the vendor / device IDs to match the generic VBIOS header. *
937 *********************************************************************/
938u32 map_oprom_vendev(u32 vendev)
939{
940 u32 new_vendev = vendev;
941
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +0100942 switch (vendev) {
Bruce Griffith76db07e2013-07-07 02:06:53 -0600943 case 0x10029830:
944 case 0x10029831:
945 case 0x10029832:
946 case 0x10029833:
947 case 0x10029834:
948 case 0x10029835:
949 case 0x10029836:
950 case 0x10029837:
951 case 0x10029838:
952 case 0x10029839:
953 case 0x1002983A:
954 case 0x1002983D:
955 new_vendev = 0x10029830; // This is the default value in AMD-generated VBIOS
956 break;
957 default:
958 break;
959 }
960
961 if (vendev != new_vendev)
962 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
963
964 return new_vendev;
965}