blob: 2cdcb5802332ab0d390db9b9b5731515607052a1 [file] [log] [blame]
Siyuan Wang3e32cc02013-07-09 17:16:20 +08001/*
2 * This file is part of the coreboot project.
3 *
Siyuan Wang3e32cc02013-07-09 17:16:20 +08004 *
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.
Siyuan Wang3e32cc02013-07-09 17:16:20 +080013 */
14
15#include <console/console.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020016#include <device/pci_ops.h>
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +030017#include <arch/acpi.h>
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +020018#include <arch/acpigen.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080019#include <stdint.h>
20#include <device/device.h>
21#include <device/pci.h>
22#include <device/pci_ids.h>
23#include <device/hypertransport.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080024#include <string.h>
25#include <lib.h>
26#include <cpu/cpu.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080027#include <cpu/x86/lapic.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020028#include <cpu/amd/msr.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080029#include <cpu/amd/mtrr.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080030#include <Porting.h>
31#include <AGESA.h>
32#include <Options.h>
33#include <Topology.h>
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +020034#include <northbridge/amd/agesa/nb_common.h>
Kyösti Mälkki28c4d2f2016-11-25 11:21:02 +020035#include <northbridge/amd/agesa/state_machine.h>
Kyösti Mälkkid610c582017-03-05 06:28:18 +020036#include <northbridge/amd/agesa/agesa_helper.h>
Siyuan Wang3e32cc02013-07-09 17:16:20 +080037
Kyösti Mälkki113f6702018-05-20 20:12:32 +030038#define MAX_NODE_NUMS MAX_NODES
Siyuan Wang3e32cc02013-07-09 17:16:20 +080039
Siyuan Wang3e32cc02013-07-09 17:16:20 +080040typedef struct dram_base_mask {
41 u32 base; //[47:27] at [28:8]
42 u32 mask; //[47:27] at [28:8] and enable at bit 0
43} dram_base_mask_t;
44
Subrata Banikb1434fc2019-03-15 22:20:41 +053045static unsigned int node_nums;
46static unsigned int sblink;
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030047static struct device *__f0_dev[MAX_NODE_NUMS];
48static struct device *__f1_dev[MAX_NODE_NUMS];
49static struct device *__f2_dev[MAX_NODE_NUMS];
50static struct device *__f4_dev[MAX_NODE_NUMS];
Subrata Banikb1434fc2019-03-15 22:20:41 +053051static unsigned int fx_devs = 0;
Siyuan Wang3e32cc02013-07-09 17:16:20 +080052
53static dram_base_mask_t get_dram_base_mask(u32 nodeid)
54{
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030055 struct device *dev;
Siyuan Wang3e32cc02013-07-09 17:16:20 +080056 dram_base_mask_t d;
57 dev = __f1_dev[0];
58 u32 temp;
59 temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16]
60 d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too
61 temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
Elyes HAOUAS27e18012017-06-27 23:14:51 +020062 d.mask |= temp << 21;
Siyuan Wang3e32cc02013-07-09 17:16:20 +080063 temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16]
64 d.mask |= (temp & 1); // enable bit
65 d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too
66 temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0]
Elyes HAOUAS27e18012017-06-27 23:14:51 +020067 d.base |= temp << 21;
Siyuan Wang3e32cc02013-07-09 17:16:20 +080068 return d;
69}
70
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030071static void set_io_addr_reg(struct device *dev, u32 nodeid, u32 linkn, u32 reg,
Siyuan Wang3e32cc02013-07-09 17:16:20 +080072 u32 io_min, u32 io_max)
73{
74 u32 i;
75 u32 tempreg;
76 /* io range allocation */
Elyes HAOUAS27e18012017-06-27 23:14:51 +020077 tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn << 4) | ((io_max&0xf0)<<(12-4)); //limit
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020078 for (i = 0; i < node_nums; i++)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080079 pci_write_config32(__f1_dev[i], reg+4, tempreg);
Elyes HAOUAS27e18012017-06-27 23:14:51 +020080 tempreg = 3 /*| (3 << 4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ?
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020081 for (i = 0; i < node_nums; i++)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080082 pci_write_config32(__f1_dev[i], reg, tempreg);
83}
84
85static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes)
86{
87 u32 i;
88 u32 tempreg;
89 /* io range allocation */
Elyes HAOUAS27e18012017-06-27 23:14:51 +020090 tempreg = (nodeid&0xf) | (linkn << 4) | (mmio_max&0xffffff00); //limit
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020091 for (i = 0; i < nodes; i++)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080092 pci_write_config32(__f1_dev[i], reg+4, tempreg);
93 tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00);
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +020094 for (i = 0; i < node_nums; i++)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080095 pci_write_config32(__f1_dev[i], reg, tempreg);
96}
97
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030098static struct device *get_node_pci(u32 nodeid, u32 fn)
Siyuan Wang3e32cc02013-07-09 17:16:20 +080099{
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200100 return pcidev_on_root(DEV_CDB + nodeid, fn);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800101}
102
103static void get_fx_devs(void)
104{
105 int i;
106 for (i = 0; i < MAX_NODE_NUMS; i++) {
107 __f0_dev[i] = get_node_pci(i, 0);
108 __f1_dev[i] = get_node_pci(i, 1);
109 __f2_dev[i] = get_node_pci(i, 2);
110 __f4_dev[i] = get_node_pci(i, 4);
111 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
112 fx_devs = i+1;
113 }
114 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
115 die("Cannot find 0:0x18.[0|1]\n");
116 }
117 printk(BIOS_DEBUG, "fx_devs=0x%x\n", fx_devs);
118}
119
Subrata Banikb1434fc2019-03-15 22:20:41 +0530120static u32 f1_read_config32(unsigned int reg)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800121{
122 if (fx_devs == 0)
123 get_fx_devs();
124 return pci_read_config32(__f1_dev[0], reg);
125}
126
Subrata Banikb1434fc2019-03-15 22:20:41 +0530127static void f1_write_config32(unsigned int reg, u32 value)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800128{
129 int i;
130 if (fx_devs == 0)
131 get_fx_devs();
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200132 for (i = 0; i < fx_devs; i++) {
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300133 struct device *dev;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800134 dev = __f1_dev[i];
135 if (dev && dev->enabled) {
136 pci_write_config32(dev, reg, value);
137 }
138 }
139}
140
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300141static u32 amdfam16_nodeid(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800142{
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200143 return (dev->path.pci.devfn >> 3) - DEV_CDB;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800144}
145
146static void set_vga_enable_reg(u32 nodeid, u32 linkn)
147{
148 u32 val;
149
Elyes HAOUAS27e18012017-06-27 23:14:51 +0200150 val = 1 | (nodeid << 4) | (linkn << 12);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800151 /* it will routing
152 * (1)mmio 0xa0000:0xbffff
153 * (2)io 0x3b0:0x3bb, 0x3c0:0x3df
154 */
155 f1_write_config32(0xf4, val);
156
157}
158
159/**
160 * @return
Elyes HAOUAS99b075a2019-12-30 14:29:31 +0100161 * @retval 2 resource does not exist, usable
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800162 * @retval 0 resource exists, not usable
163 * @retval 1 resource exist, resource has been allocated before
164 */
Subrata Banikb1434fc2019-03-15 22:20:41 +0530165static int reg_useable(unsigned int reg, struct device *goal_dev,
166 unsigned int goal_nodeid, unsigned int goal_link)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800167{
168 struct resource *res;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530169 unsigned int nodeid, link = 0;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800170 int result;
171 res = 0;
172 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300173 struct device *dev;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800174 dev = __f0_dev[nodeid];
175 if (!dev)
176 continue;
177 for (link = 0; !res && (link < 8); link++) {
178 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
179 }
180 }
181 result = 2;
182 if (res) {
183 result = 0;
184 if ((goal_link == (link - 1)) &&
185 (goal_nodeid == (nodeid - 1)) &&
186 (res->flags <= 1)) {
187 result = 1;
188 }
189 }
190 return result;
191}
192
Subrata Banikb1434fc2019-03-15 22:20:41 +0530193static struct resource *amdfam16_find_iopair(struct device *dev,
194 unsigned int nodeid, unsigned int link)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800195{
196 struct resource *resource;
197 u32 free_reg, reg;
198 resource = 0;
199 free_reg = 0;
200 for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
201 int result;
202 result = reg_useable(reg, dev, nodeid, link);
203 if (result == 1) {
204 /* I have been allocated this one */
205 break;
206 }
207 else if (result > 1) {
208 /* I have a free register pair */
209 free_reg = reg;
210 }
211 }
212 if (reg > 0xd8) {
213 reg = free_reg; // if no free, the free_reg still be 0
214 }
215
216 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
217
218 return resource;
219}
220
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300221static struct resource *amdfam16_find_mempair(struct device *dev, u32 nodeid, u32 link)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800222{
223 struct resource *resource;
224 u32 free_reg, reg;
225 resource = 0;
226 free_reg = 0;
227 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
228 int result;
229 result = reg_useable(reg, dev, nodeid, link);
230 if (result == 1) {
231 /* I have been allocated this one */
232 break;
233 }
234 else if (result > 1) {
235 /* I have a free register pair */
236 free_reg = reg;
237 }
238 }
239 if (reg > 0xb8) {
240 reg = free_reg;
241 }
242
243 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
244 return resource;
245}
246
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300247static void amdfam16_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800248{
249 struct resource *resource;
250
251 /* Initialize the io space constraints on the current bus */
252 resource = amdfam16_find_iopair(dev, nodeid, link);
253 if (resource) {
254 u32 align;
255 align = log2(HT_IO_HOST_ALIGN);
256 resource->base = 0;
257 resource->size = 0;
258 resource->align = align;
259 resource->gran = align;
260 resource->limit = 0xffffUL;
261 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
262 }
263
264 /* Initialize the prefetchable memory constraints on the current bus */
265 resource = amdfam16_find_mempair(dev, nodeid, link);
266 if (resource) {
267 resource->base = 0;
268 resource->size = 0;
269 resource->align = log2(HT_MEM_HOST_ALIGN);
270 resource->gran = log2(HT_MEM_HOST_ALIGN);
271 resource->limit = 0xffffffffffULL;
272 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
273 resource->flags |= IORESOURCE_BRIDGE;
274 }
275
276 /* Initialize the memory constraints on the current bus */
277 resource = amdfam16_find_mempair(dev, nodeid, link);
278 if (resource) {
279 resource->base = 0;
280 resource->size = 0;
281 resource->align = log2(HT_MEM_HOST_ALIGN);
282 resource->gran = log2(HT_MEM_HOST_ALIGN);
283 resource->limit = 0xffffffffffULL;
284 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
285 }
286
287}
288
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300289static void read_resources(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800290{
291 u32 nodeid;
292 struct bus *link;
293
294 nodeid = amdfam16_nodeid(dev);
295 for (link = dev->link_list; link; link = link->next) {
296 if (link->children) {
297 amdfam16_link_read_bases(dev, nodeid, link->link_num);
298 }
299 }
Edward O'Callaghan66c65322014-11-21 01:43:38 +1100300
301 /*
302 * This MMCONF resource must be reserved in the PCI_DOMAIN.
303 * It is not honored by the coreboot resource allocator if it is in
304 * the APIC_CLUSTER.
305 */
Elyes HAOUAS400ce552018-10-12 10:54:30 +0200306 mmconf_resource(dev, MMIO_CONF_BASE);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800307}
308
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300309static void set_resource(struct device *dev, struct resource *resource, u32 nodeid)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800310{
311 resource_t rbase, rend;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530312 unsigned int reg, link_num;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800313 char buf[50];
314
315 /* Make certain the resource has actually been set */
316 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
317 return;
318 }
319
320 /* If I have already stored this resource don't worry about it */
321 if (resource->flags & IORESOURCE_STORED) {
322 return;
323 }
324
325 /* Only handle PCI memory and IO resources */
326 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
327 return;
328
329 /* Ensure I am actually looking at a resource of function 1 */
330 if ((resource->index & 0xffff) < 0x1000) {
331 return;
332 }
333 /* Get the base address */
334 rbase = resource->base;
335
336 /* Get the limit (rounded up) */
337 rend = resource_end(resource);
338
339 /* Get the register and link */
340 reg = resource->index & 0xfff; // 4k
341 link_num = IOINDEX_LINK(resource->index);
342
343 if (resource->flags & IORESOURCE_IO) {
344 set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8);
345 }
346 else if (resource->flags & IORESOURCE_MEM) {
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100347 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 +0800348 }
349 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200350 snprintf(buf, sizeof(buf), " <node %x link %x>",
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800351 nodeid, link_num);
352 report_resource_stored(dev, resource, buf);
353}
354
355/**
356 * I tried to reuse the resource allocation code in set_resource()
357 * but it is too difficult to deal with the resource allocation magic.
358 */
359
Subrata Banikb1434fc2019-03-15 22:20:41 +0530360static void create_vga_resource(struct device *dev, unsigned int nodeid)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800361{
362 struct bus *link;
363
364 /* find out which link the VGA card is connected,
365 * we only deal with the 'first' vga card */
366 for (link = dev->link_list; link; link = link->next) {
367 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800368#if CONFIG(MULTIPLE_VGA_ADAPTERS)
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300369 extern struct device *vga_pri; // the primary vga device, defined in device.c
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800370 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary,
371 link->secondary,link->subordinate);
372 /* We need to make sure the vga_pri is under the link */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200373 if ((vga_pri->bus->secondary >= link->secondary) &&
374 (vga_pri->bus->secondary <= link->subordinate))
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800375#endif
376 break;
377 }
378 }
379
380 /* no VGA card installed */
381 if (link == NULL)
382 return;
383
384 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink);
385 set_vga_enable_reg(nodeid, sblink);
386}
387
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300388static void set_resources(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800389{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530390 unsigned int nodeid;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800391 struct bus *bus;
392 struct resource *res;
393
394 /* Find the nodeid */
395 nodeid = amdfam16_nodeid(dev);
396
397 create_vga_resource(dev, nodeid); //TODO: do we need this?
398
399 /* Set each resource we have found */
400 for (res = dev->resource_list; res; res = res->next) {
401 set_resource(dev, res, nodeid);
402 }
403
404 for (bus = dev->link_list; bus; bus = bus->next) {
405 if (bus->children) {
406 assign_resources(bus);
407 }
408 }
409}
410
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100411static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200412{
413 void *addr, *current;
414
415 /* Skip the HEST header. */
416 current = (void *)(hest + 1);
417
418 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
419 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700420 current += acpi_create_hest_error_source(hest, current, 0,
421 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200422
423 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
424 if (addr != NULL)
Stefan Reinauer77a1d1a2015-07-21 12:48:17 -0700425 current += acpi_create_hest_error_source(hest, current, 1,
426 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200427
428 return (unsigned long)current;
429}
430
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300431static void northbridge_fill_ssdt_generator(struct device *device)
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200432{
433 msr_t msr;
434 char pscope[] = "\\_SB.PCI0";
435
436 acpigen_write_scope(pscope);
437 msr = rdmsr(TOP_MEM);
438 acpigen_write_name_dword("TOM1", msr.lo);
439 msr = rdmsr(TOP_MEM2);
440 /*
441 * Since XP only implements parts of ACPI 2.0, we can't use a qword
442 * here.
443 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
444 * slide 22ff.
445 * Shift value right by 20 bit to make it fit into 32bit,
446 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
447 */
448 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
449 acpigen_pop_len();
450}
451
Michał Żygowski9550e972020-03-20 13:56:46 +0100452static void patch_ssdt_processor_scope(acpi_header_t *ssdt)
453{
454 unsigned int len = ssdt->length - sizeof(acpi_header_t);
455 unsigned int i;
456
457 for (i = sizeof(acpi_header_t); i < len; i++) {
458 /* Search for _PR_ scope and replace it with _SB_ */
459 if (*(uint32_t *)((unsigned long)ssdt + i) == 0x5f52505f)
460 *(uint32_t *)((unsigned long)ssdt + i) = 0x5f42535f;
461 }
462 /* Recalculate checksum */
463 ssdt->checksum = 0;
464 ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
465}
466
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300467static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200468 unsigned long current,
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200469 acpi_rsdp_t *rsdp)
470{
471 acpi_srat_t *srat;
472 acpi_slit_t *slit;
473 acpi_header_t *ssdt;
474 acpi_header_t *alib;
475 acpi_header_t *ivrs;
476 acpi_hest_t *hest;
477
478 /* HEST */
479 current = ALIGN(current, 8);
480 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100481 acpi_write_hest((void *)current, acpi_fill_hest);
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200482 acpi_add_table(rsdp, (void *)current);
483 current += ((acpi_header_t *)current)->length;
484
485 current = ALIGN(current, 8);
486 printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current);
487 ivrs = agesawrapper_getlateinitptr(PICK_IVRS);
488 if (ivrs != NULL) {
489 memcpy((void *)current, ivrs, ivrs->length);
490 ivrs = (acpi_header_t *) current;
491 current += ivrs->length;
492 acpi_add_table(rsdp, ivrs);
493 } else {
494 printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n");
495 }
496
497 /* SRAT */
498 current = ALIGN(current, 8);
499 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
500 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
501 if (srat != NULL) {
502 memcpy((void *)current, srat, srat->header.length);
503 srat = (acpi_srat_t *) current;
504 current += srat->header.length;
505 acpi_add_table(rsdp, srat);
506 } else {
507 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
508 }
509
510 /* SLIT */
511 current = ALIGN(current, 8);
512 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
513 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
514 if (slit != NULL) {
515 memcpy((void *)current, slit, slit->header.length);
516 slit = (acpi_slit_t *) current;
517 current += slit->header.length;
518 acpi_add_table(rsdp, slit);
519 } else {
520 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
521 }
522
523 /* ALIB */
524 current = ALIGN(current, 16);
525 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
526 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
527 if (alib != NULL) {
528 memcpy((void *)current, alib, alib->length);
529 alib = (acpi_header_t *) current;
530 current += alib->length;
531 acpi_add_table(rsdp, (void *)alib);
532 }
533 else {
534 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
535 }
536
537 /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */
538 /* SSDT */
539 current = ALIGN(current, 16);
540 printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current);
541 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
542 if (ssdt != NULL) {
Michał Żygowski9550e972020-03-20 13:56:46 +0100543 patch_ssdt_processor_scope(ssdt);
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200544 memcpy((void *)current, ssdt, ssdt->length);
545 ssdt = (acpi_header_t *) current;
546 current += ssdt->length;
547 }
548 else {
549 printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n");
550 }
551 acpi_add_table(rsdp,ssdt);
552
553 printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current);
554
555 return current;
556}
557
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800558static struct device_operations northbridge_operations = {
559 .read_resources = read_resources,
560 .set_resources = set_resources,
561 .enable_resources = pci_dev_enable_resources,
Edward O'Callaghand994ef12014-11-21 02:22:33 +1100562 .init = DEVICE_NOOP,
Vladimir Serbinenkodb09b0622014-10-08 22:15:17 +0200563 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
564 .write_acpi_tables = agesa_write_acpi_tables,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800565 .enable = 0,
566 .ops_pci = 0,
567};
568
569static const struct pci_driver family16_northbridge __pci_driver = {
570 .ops = &northbridge_operations,
571 .vendor = PCI_VENDOR_ID_AMD,
572 .device = PCI_DEVICE_ID_AMD_16H_MODEL_000F_NB_HT,
573};
574
575static const struct pci_driver family10_northbridge __pci_driver = {
576 .ops = &northbridge_operations,
577 .vendor = PCI_VENDOR_ID_AMD,
578 .device = PCI_DEVICE_ID_AMD_10H_NB_HT,
579};
580
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200581static void fam16_finalize(void *chip_info)
582{
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300583 struct device *dev;
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200584 u32 value;
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300585 dev = pcidev_on_root(0, 0); /* clear IoapicSbFeatureEn */
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200586 pci_write_config32(dev, 0xF8, 0);
587 pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */
588
589 /* disable No Snoop */
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300590 dev = pcidev_on_root(1, 1);
Kyösti Mälkki69f6fd42019-01-21 14:19:01 +0200591 if (dev != NULL) {
592 value = pci_read_config32(dev, 0x60);
593 value &= ~(1 << 11);
594 pci_write_config32(dev, 0x60, value);
595 }
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200596}
597
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800598struct chip_operations northbridge_amd_agesa_family16kb_ops = {
599 CHIP_NAME("AMD FAM16 Northbridge")
600 .enable_dev = 0,
Kyösti Mälkki4ee82c62014-11-25 16:03:12 +0200601 .final = fam16_finalize,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800602};
603
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300604static void domain_read_resources(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800605{
Subrata Banikb1434fc2019-03-15 22:20:41 +0530606 unsigned int reg;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800607
608 /* Find the already assigned resource pairs */
609 get_fx_devs();
610 for (reg = 0x80; reg <= 0xd8; reg+= 0x08) {
611 u32 base, limit;
612 base = f1_read_config32(reg);
613 limit = f1_read_config32(reg + 0x04);
614 /* Is this register allocated? */
615 if ((base & 3) != 0) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530616 unsigned int nodeid, reg_link;
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300617 struct device *reg_dev;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200618 if (reg < 0xc0) { // mmio
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800619 nodeid = (limit & 0xf) + (base&0x30);
620 } else { // io
621 nodeid = (limit & 0xf) + ((base>>4)&0x30);
622 }
623 reg_link = (limit >> 4) & 7;
624 reg_dev = __f0_dev[nodeid];
625 if (reg_dev) {
626 /* Reserve the resource */
627 struct resource *res;
628 res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link));
629 if (res) {
630 res->flags = 1;
631 }
632 }
633 }
634 }
635 /* FIXME: do we need to check extend conf space?
636 I don't believe that much preset value */
637
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800638 pci_domain_read_resources(dev);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800639}
640
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800641#if CONFIG_HW_MEM_HOLE_SIZEK != 0
642struct hw_mem_hole_info {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530643 unsigned int hole_startk;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800644 int node_id;
645};
646static struct hw_mem_hole_info get_hw_mem_hole_info(void)
647{
648 struct hw_mem_hole_info mem_hole;
649 int i;
650 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
651 mem_hole.node_id = -1;
652 for (i = 0; i < node_nums; i++) {
653 dram_base_mask_t d;
654 u32 hole;
655 d = get_dram_base_mask(i);
656 if (!(d.mask & 1)) continue; // no memory on this node
657 hole = pci_read_config32(__f1_dev[i], 0xf0);
658 if (hole & 2) { // we find the hole
Elyes HAOUAS27e18012017-06-27 23:14:51 +0200659 mem_hole.hole_startk = (hole & (0xff << 24)) >> 10;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800660 mem_hole.node_id = i; // record the node No with hole
661 break; // only one hole
662 }
663 }
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300664
665 /* We need to double check if there is special set on base reg and limit reg
666 * are not continuous instead of hole, it will find out its hole_startk.
667 */
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800668 if (mem_hole.node_id == -1) {
669 resource_t limitk_pri = 0;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200670 for (i = 0; i < node_nums; i++) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800671 dram_base_mask_t d;
672 resource_t base_k, limit_k;
673 d = get_dram_base_mask(i);
674 if (!(d.base & 1)) continue;
675 base_k = ((resource_t)(d.base & 0x1fffff00)) <<9;
676 if (base_k > 4 *1024 * 1024) break; // don't need to go to check
677 if (limitk_pri != base_k) { // we find the hole
Martin Roth468d02c2019-10-23 21:44:42 -0600678 mem_hole.hole_startk = (unsigned int)limitk_pri; // must beblow 4G
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800679 mem_hole.node_id = i;
680 break; //only one hole
681 }
682 limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
683 limitk_pri = limit_k;
684 }
685 }
686 return mem_hole;
687}
688#endif
689
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300690static void domain_set_resources(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800691{
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800692 unsigned long mmio_basek;
693 u32 pci_tolm;
694 int i, idx;
695 struct bus *link;
696#if CONFIG_HW_MEM_HOLE_SIZEK != 0
697 struct hw_mem_hole_info mem_hole;
698 u32 reset_memhole = 1;
699#endif
700
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800701 pci_tolm = 0xffffffffUL;
702 for (link = dev->link_list; link; link = link->next) {
703 pci_tolm = find_pci_tolm(link);
704 }
705
706 // FIXME handle interleaved nodes. If you fix this here, please fix
707 // amdk8, too.
708 mmio_basek = pci_tolm >> 10;
709 /* Round mmio_basek to something the processor can support */
710 mmio_basek &= ~((1 << 6) -1);
711
712 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
713 // MMIO hole. If you fix this here, please fix amdk8, too.
714 /* Round the mmio hole to 64M */
715 mmio_basek &= ~((64*1024) - 1);
716
717#if CONFIG_HW_MEM_HOLE_SIZEK != 0
718 /* if the hw mem hole is already set in raminit stage, here we will compare
719 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
720 * use hole_basek as mmio_basek and we don't need to reset hole.
721 * otherwise We reset the hole to the mmio_basek
722 */
723
724 mem_hole = get_hw_mem_hole_info();
725
726 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
727 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
728 mmio_basek = mem_hole.hole_startk;
729 reset_memhole = 0;
730 }
731#endif
732
733 idx = 0x10;
734 for (i = 0; i < node_nums; i++) {
735 dram_base_mask_t d;
736 resource_t basek, limitk, sizek; // 4 1T
737
738 d = get_dram_base_mask(i);
739
740 if (!(d.mask & 1)) continue;
741 basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here
Edward O'Callaghanae5fd342014-11-20 19:58:09 +1100742 limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800743
744 sizek = limitk - basek;
745
746 /* see if we need a hole from 0xa0000 to 0xbffff */
747 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
748 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
749 idx += 0x10;
750 basek = (8*64)+(16*16);
751 sizek = limitk - ((8*64)+(16*16));
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800752 }
753
754 //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk);
755
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300756 /* split the region to accommodate pci memory space */
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200757 if ((basek < 4*1024*1024) && (limitk > mmio_basek)) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800758 if (basek <= mmio_basek) {
Subrata Banikb1434fc2019-03-15 22:20:41 +0530759 unsigned int pre_sizek;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800760 pre_sizek = mmio_basek - basek;
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200761 if (pre_sizek > 0) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800762 ram_resource(dev, (idx | i), basek, pre_sizek);
763 idx += 0x10;
764 sizek -= pre_sizek;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800765 }
766 basek = mmio_basek;
767 }
768 if ((basek + sizek) <= 4*1024*1024) {
769 sizek = 0;
770 }
771 else {
772 uint64_t topmem2 = bsp_topmem2();
773 basek = 4*1024*1024;
774 sizek = topmem2/1024 - basek;
775 }
776 }
777
778 ram_resource(dev, (idx | i), basek, sizek);
779 idx += 0x10;
780 printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
781 i, mmio_basek, basek, limitk);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800782 }
783
Kyösti Mälkki61be3602017-04-15 20:07:53 +0300784 add_uma_resource_below_tolm(dev, 7);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800785
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200786 for (link = dev->link_list; link; link = link->next) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800787 if (link->children) {
788 assign_resources(link);
789 }
790 }
791}
792
Kevin Cody-Little06d23232018-05-09 14:22:15 -0400793static const char *domain_acpi_name(const struct device *dev)
794{
795 if (dev->path.type == DEVICE_PATH_DOMAIN)
796 return "PCI0";
797
798 return NULL;
799}
800
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800801static struct device_operations pci_domain_ops = {
802 .read_resources = domain_read_resources,
803 .set_resources = domain_set_resources,
Edward O'Callaghane9e1d7a2015-01-02 15:11:49 +1100804 .init = DEVICE_NOOP,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800805 .scan_bus = pci_domain_scan_bus,
Kevin Cody-Little06d23232018-05-09 14:22:15 -0400806 .acpi_name = domain_acpi_name,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800807};
808
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300809static void sysconf_init(struct device *dev) // first node
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800810{
811 sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1
812 node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0]
813}
814
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300815static void cpu_bus_scan(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800816{
817 struct bus *cpu_bus;
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300818 struct device *dev_mc;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800819 int i,j;
820 int coreid_bits;
821 int core_max = 0;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530822 unsigned int ApicIdCoreIdSize;
823 unsigned int core_nums;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800824 int siblings = 0;
825 unsigned int family;
826
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200827 dev_mc = pcidev_on_root(DEV_CDB, 0);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800828 if (!dev_mc) {
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200829 printk(BIOS_ERR, "0:%02x.0 not found", DEV_CDB);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800830 die("");
831 }
832 sysconf_init(dev_mc);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800833
834 /* Get Max Number of cores(MNC) */
Kyösti Mälkkid41feed2017-09-24 16:23:57 +0300835 coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800836 core_max = 1 << (coreid_bits & 0x000F); //mnc
837
838 ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF);
839 if (ApicIdCoreIdSize) {
840 core_nums = (1 << ApicIdCoreIdSize) - 1;
841 } else {
842 core_nums = 3; //quad core
843 }
844
845 /* Find which cpus are present */
846 cpu_bus = dev->link_list;
847 for (i = 0; i < node_nums; i++) {
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300848 struct device *cdb_dev;
Subrata Banikb1434fc2019-03-15 22:20:41 +0530849 unsigned int devn;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800850 struct bus *pbus;
851
Kyösti Mälkki3d3152e2019-01-10 09:05:30 +0200852 devn = DEV_CDB + i;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800853 pbus = dev_mc->bus;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800854
855 /* Find the cpu's pci device */
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300856 cdb_dev = pcidev_on_root(devn, 0);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800857 if (!cdb_dev) {
858 /* If I am probing things in a weird order
859 * ensure all of the cpu's pci devices are found.
860 */
861 int fn;
Elyes HAOUAS5a7e72f2016-08-23 21:36:02 +0200862 for (fn = 0; fn <= 5; fn++) { //FBDIMM?
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800863 cdb_dev = pci_probe_dev(NULL, pbus,
864 PCI_DEVFN(devn, fn));
865 }
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300866 cdb_dev = pcidev_on_root(devn, 0);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800867 } else {
868 /* Ok, We need to set the links for that device.
869 * otherwise the device under it will not be scanned
870 */
Kyösti Mälkki2a2d6132015-02-04 13:25:37 +0200871 add_more_links(cdb_dev, 4);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800872 }
873
874 family = cpuid_eax(1);
875 family = (family >> 20) & 0xFF;
876 if (family == 1) { //f10
877 u32 dword;
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300878 cdb_dev = pcidev_on_root(devn, 3);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800879 dword = pci_read_config32(cdb_dev, 0xe8);
880 siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12);
881 } else if (family == 7) {//f16
Kyösti Mälkki4ad7f5b2018-05-22 01:15:17 +0300882 cdb_dev = pcidev_on_root(devn, 5);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800883 if (cdb_dev && cdb_dev->enabled) {
884 siblings = pci_read_config32(cdb_dev, 0x84);
885 siblings &= 0xFF;
886 }
887 } else {
888 siblings = 0; //default one core
889 }
890 int enable_node = cdb_dev && cdb_dev->enabled;
891 printk(BIOS_SPEW, "%s family%xh, core_max=0x%x, core_nums=0x%x, siblings=0x%x\n",
892 dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings);
893
Elyes HAOUAS1d8daa62016-09-18 08:50:54 +0200894 for (j = 0; j <= siblings; j++) {
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800895 extern CONST OPTIONS_CONFIG_TOPOLOGY ROMDATA TopologyConfiguration;
896 u32 modules = TopologyConfiguration.PlatformNumberOfModules;
897 u32 lapicid_start = 0;
898
899 /*
900 * APIC ID calucation is tightly coupled with AGESA v5 code.
901 * This calculation MUST match the assignment calculation done
902 * in LocalApicInitializationAtEarly() function.
903 * And reference GetLocalApicIdForCore()
904 *
Elyes HAOUASa5b0bc42020-02-20 20:04:29 +0100905 * Apply APIC enumeration rules
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800906 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
907 * put the local-APICs at m..z
908 *
909 * This is needed because many IO-APIC devices only have 4 bits
910 * for their APIC id and therefore must reside at 0..15
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +0200911 */
Kyösti Mälkki50036322016-05-18 13:35:21 +0300912
Elyes HAOUAS6e8b3c12016-09-02 19:22:00 +0200913 u8 plat_num_io_apics = 3; /* FIXME */
Kyösti Mälkki50036322016-05-18 13:35:21 +0300914
915 if ((node_nums * core_max) + plat_num_io_apics >= 0x10) {
916 lapicid_start = (plat_num_io_apics - 1) / core_max;
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800917 lapicid_start = (lapicid_start + 1) * core_max;
918 printk(BIOS_SPEW, "lpaicid_start=0x%x ", lapicid_start);
919 }
920 u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j);
921 printk(BIOS_SPEW, "node 0x%x core 0x%x apicid=0x%x\n",
922 i, j, apic_id);
923
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300924 struct device *cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800925 if (cpu)
926 amd_cpu_topology(cpu, i, j);
927 } //j
928 }
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800929}
930
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300931static void cpu_bus_init(struct device *dev)
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800932{
933 initialize_cpus(dev->link_list);
934}
935
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800936static struct device_operations cpu_bus_ops = {
Edward O'Callaghan66c65322014-11-21 01:43:38 +1100937 .read_resources = DEVICE_NOOP,
938 .set_resources = DEVICE_NOOP,
Edward O'Callaghan812d2a42014-10-31 08:17:23 +1100939 .enable_resources = DEVICE_NOOP,
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800940 .init = cpu_bus_init,
941 .scan_bus = cpu_bus_scan,
942};
943
944static void root_complex_enable_dev(struct device *dev)
945{
946 static int done = 0;
947
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800948 if (!done) {
949 setup_bsp_ramtop();
Siyuan Wang3e32cc02013-07-09 17:16:20 +0800950 done = 1;
951 }
952
953 /* Set the operations if it is a special bus type */
954 if (dev->path.type == DEVICE_PATH_DOMAIN) {
955 dev->ops = &pci_domain_ops;
956 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
957 dev->ops = &cpu_bus_ops;
958 }
959}
960
961struct chip_operations northbridge_amd_agesa_family16kb_root_complex_ops = {
962 CHIP_NAME("AMD FAM16 Root Complex")
963 .enable_dev = root_complex_enable_dev,
964};
Bruce Griffith76db07e2013-07-07 02:06:53 -0600965
966/*********************************************************************
967 * Change the vendor / device IDs to match the generic VBIOS header. *
968 *********************************************************************/
969u32 map_oprom_vendev(u32 vendev)
970{
971 u32 new_vendev = vendev;
972
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +0100973 switch (vendev) {
Bruce Griffith76db07e2013-07-07 02:06:53 -0600974 case 0x10029830:
975 case 0x10029831:
976 case 0x10029832:
977 case 0x10029833:
978 case 0x10029834:
979 case 0x10029835:
980 case 0x10029836:
981 case 0x10029837:
982 case 0x10029838:
983 case 0x10029839:
984 case 0x1002983A:
985 case 0x1002983D:
986 new_vendev = 0x10029830; // This is the default value in AMD-generated VBIOS
987 break;
988 default:
989 break;
990 }
991
992 if (vendev != new_vendev)
993 printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev);
994
995 return new_vendev;
996}