Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <console/console.h> |
| 17 | #include <arch/io.h> |
| 18 | #include <arch/acpi.h> |
| 19 | #include <stdint.h> |
| 20 | #include <device/device.h> |
| 21 | #include <device/pci.h> |
| 22 | #include <device/pci_ids.h> |
| 23 | #include <device/hypertransport.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <lib.h> |
| 27 | #include <cpu/cpu.h> |
| 28 | #include <cbmem.h> |
| 29 | |
| 30 | #include <Porting.h> |
| 31 | #include <AGESA.h> |
| 32 | #include <FieldAccessors.h> |
| 33 | #include <Options.h> |
| 34 | #include <Topology.h> |
Kyösti Mälkki | ed8d277 | 2017-07-15 17:12:44 +0300 | [diff] [blame] | 35 | #include <northbridge/amd/agesa/agesa_helper.h> |
Kyösti Mälkki | 903ce25 | 2016-11-25 11:21:02 +0200 | [diff] [blame] | 36 | #if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER) |
Kyösti Mälkki | 023ed1f | 2014-10-22 08:05:36 +0300 | [diff] [blame] | 37 | #include <northbridge/amd/pi/agesawrapper.h> |
Kyösti Mälkki | e4c17ce | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 38 | #include <northbridge/amd/pi/agesawrapper_call.h> |
Kyösti Mälkki | 903ce25 | 2016-11-25 11:21:02 +0200 | [diff] [blame] | 39 | #endif |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 40 | #include "northbridge.h" |
| 41 | |
| 42 | #include <cpu/x86/lapic.h> |
| 43 | #include <cpu/amd/mtrr.h> |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 44 | #include <arch/acpi.h> |
| 45 | #include <arch/acpigen.h> |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 46 | |
| 47 | #define MAX_NODE_NUMS (MAX_NODES * MAX_DIES) |
| 48 | |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 49 | typedef struct dram_base_mask { |
| 50 | u32 base; //[47:27] at [28:8] |
| 51 | u32 mask; //[47:27] at [28:8] and enable at bit 0 |
| 52 | } dram_base_mask_t; |
| 53 | |
| 54 | static unsigned node_nums; |
| 55 | static unsigned sblink; |
| 56 | static device_t __f0_dev[MAX_NODE_NUMS]; |
| 57 | static device_t __f1_dev[MAX_NODE_NUMS]; |
| 58 | static device_t __f2_dev[MAX_NODE_NUMS]; |
| 59 | static device_t __f4_dev[MAX_NODE_NUMS]; |
| 60 | static unsigned fx_devs = 0; |
| 61 | |
| 62 | static dram_base_mask_t get_dram_base_mask(u32 nodeid) |
| 63 | { |
| 64 | device_t dev; |
| 65 | dram_base_mask_t d; |
| 66 | dev = __f1_dev[0]; |
| 67 | u32 temp; |
| 68 | temp = pci_read_config32(dev, 0x44 + (nodeid << 3)); //[39:24] at [31:16] |
| 69 | d.mask = ((temp & 0xfff80000)>>(8+3)); // mask out DramMask [26:24] too |
| 70 | temp = pci_read_config32(dev, 0x144 + (nodeid <<3)) & 0xff; //[47:40] at [7:0] |
| 71 | d.mask |= temp<<21; |
| 72 | temp = pci_read_config32(dev, 0x40 + (nodeid << 3)); //[39:24] at [31:16] |
| 73 | d.mask |= (temp & 1); // enable bit |
| 74 | d.base = ((temp & 0xfff80000)>>(8+3)); // mask out DramBase [26:24) too |
| 75 | temp = pci_read_config32(dev, 0x140 + (nodeid <<3)) & 0xff; //[47:40] at [7:0] |
| 76 | d.base |= temp<<21; |
| 77 | return d; |
| 78 | } |
| 79 | |
| 80 | static void set_io_addr_reg(device_t dev, u32 nodeid, u32 linkn, u32 reg, |
| 81 | u32 io_min, u32 io_max) |
| 82 | { |
| 83 | u32 i; |
| 84 | u32 tempreg; |
| 85 | /* io range allocation */ |
| 86 | tempreg = (nodeid&0xf) | ((nodeid & 0x30)<<(8-4)) | (linkn<<4) | ((io_max&0xf0)<<(12-4)); //limit |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 87 | for (i = 0; i < node_nums; i++) |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 88 | pci_write_config32(__f1_dev[i], reg+4, tempreg); |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 89 | tempreg = 3 /*| (3<<4)*/ | ((io_min&0xf0)<<(12-4)); //base :ISA and VGA ? |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 90 | for (i = 0; i < node_nums; i++) |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 91 | pci_write_config32(__f1_dev[i], reg, tempreg); |
| 92 | } |
| 93 | |
| 94 | static void set_mmio_addr_reg(u32 nodeid, u32 linkn, u32 reg, u32 index, u32 mmio_min, u32 mmio_max, u32 nodes) |
| 95 | { |
| 96 | u32 i; |
| 97 | u32 tempreg; |
| 98 | /* io range allocation */ |
| 99 | tempreg = (nodeid&0xf) | (linkn<<4) | (mmio_max&0xffffff00); //limit |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 100 | for (i = 0; i < nodes; i++) |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 101 | pci_write_config32(__f1_dev[i], reg+4, tempreg); |
| 102 | tempreg = 3 | (nodeid & 0x30) | (mmio_min&0xffffff00); |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 103 | for (i = 0; i < node_nums; i++) |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 104 | pci_write_config32(__f1_dev[i], reg, tempreg); |
| 105 | } |
| 106 | |
| 107 | static device_t get_node_pci(u32 nodeid, u32 fn) |
| 108 | { |
| 109 | #if MAX_NODE_NUMS + CONFIG_CDB >= 32 |
| 110 | if ((CONFIG_CDB + nodeid) < 32) { |
| 111 | return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn)); |
| 112 | } else { |
| 113 | return dev_find_slot(CONFIG_CBB-1, PCI_DEVFN(CONFIG_CDB + nodeid - 32, fn)); |
| 114 | } |
| 115 | #else |
| 116 | return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn)); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | static void get_fx_devs(void) |
| 121 | { |
| 122 | int i; |
| 123 | for (i = 0; i < MAX_NODE_NUMS; i++) { |
| 124 | __f0_dev[i] = get_node_pci(i, 0); |
| 125 | __f1_dev[i] = get_node_pci(i, 1); |
| 126 | __f2_dev[i] = get_node_pci(i, 2); |
| 127 | __f4_dev[i] = get_node_pci(i, 4); |
| 128 | if (__f0_dev[i] != NULL && __f1_dev[i] != NULL) |
| 129 | fx_devs = i+1; |
| 130 | } |
| 131 | if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) { |
| 132 | die("Cannot find 0:0x18.[0|1]\n"); |
| 133 | } |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 134 | printk(BIOS_DEBUG, "fx_devs = 0x%x\n", fx_devs); |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static u32 f1_read_config32(unsigned reg) |
| 138 | { |
| 139 | if (fx_devs == 0) |
| 140 | get_fx_devs(); |
| 141 | return pci_read_config32(__f1_dev[0], reg); |
| 142 | } |
| 143 | |
| 144 | static void f1_write_config32(unsigned reg, u32 value) |
| 145 | { |
| 146 | int i; |
| 147 | if (fx_devs == 0) |
| 148 | get_fx_devs(); |
Elyes HAOUAS | 5a7e72f | 2016-08-23 21:36:02 +0200 | [diff] [blame] | 149 | for (i = 0; i < fx_devs; i++) { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 150 | device_t dev; |
| 151 | dev = __f1_dev[i]; |
| 152 | if (dev && dev->enabled) { |
| 153 | pci_write_config32(dev, reg, value); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static u32 amdfam16_nodeid(device_t dev) |
| 159 | { |
| 160 | #if MAX_NODE_NUMS == 64 |
| 161 | unsigned busn; |
| 162 | busn = dev->bus->secondary; |
| 163 | if (busn != CONFIG_CBB) { |
| 164 | return (dev->path.pci.devfn >> 3) - CONFIG_CDB + 32; |
| 165 | } else { |
| 166 | return (dev->path.pci.devfn >> 3) - CONFIG_CDB; |
| 167 | } |
| 168 | |
| 169 | #else |
| 170 | return (dev->path.pci.devfn >> 3) - CONFIG_CDB; |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | static void set_vga_enable_reg(u32 nodeid, u32 linkn) |
| 175 | { |
| 176 | u32 val; |
| 177 | |
| 178 | val = 1 | (nodeid<<4) | (linkn<<12); |
| 179 | /* it will routing |
| 180 | * (1)mmio 0xa0000:0xbffff |
| 181 | * (2)io 0x3b0:0x3bb, 0x3c0:0x3df |
| 182 | */ |
| 183 | f1_write_config32(0xf4, val); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @return |
| 189 | * @retval 2 resoure does not exist, usable |
| 190 | * @retval 0 resource exists, not usable |
| 191 | * @retval 1 resource exist, resource has been allocated before |
| 192 | */ |
| 193 | static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid, |
| 194 | unsigned goal_link) |
| 195 | { |
| 196 | struct resource *res; |
| 197 | unsigned nodeid, link = 0; |
| 198 | int result; |
| 199 | res = 0; |
| 200 | for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) { |
| 201 | device_t dev; |
| 202 | dev = __f0_dev[nodeid]; |
| 203 | if (!dev) |
| 204 | continue; |
| 205 | for (link = 0; !res && (link < 8); link++) { |
| 206 | res = probe_resource(dev, IOINDEX(0x1000 + reg, link)); |
| 207 | } |
| 208 | } |
| 209 | result = 2; |
| 210 | if (res) { |
| 211 | result = 0; |
| 212 | if ((goal_link == (link - 1)) && |
| 213 | (goal_nodeid == (nodeid - 1)) && |
| 214 | (res->flags <= 1)) { |
| 215 | result = 1; |
| 216 | } |
| 217 | } |
| 218 | return result; |
| 219 | } |
| 220 | |
| 221 | static struct resource *amdfam16_find_iopair(device_t dev, unsigned nodeid, unsigned link) |
| 222 | { |
| 223 | struct resource *resource; |
| 224 | u32 free_reg, reg; |
| 225 | resource = 0; |
| 226 | free_reg = 0; |
| 227 | for (reg = 0xc0; reg <= 0xd8; 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 > 0xd8) { |
| 240 | reg = free_reg; // if no free, the free_reg still be 0 |
| 241 | } |
| 242 | |
| 243 | resource = new_resource(dev, IOINDEX(0x1000 + reg, link)); |
| 244 | |
| 245 | return resource; |
| 246 | } |
| 247 | |
| 248 | static struct resource *amdfam16_find_mempair(device_t dev, u32 nodeid, u32 link) |
| 249 | { |
| 250 | struct resource *resource; |
| 251 | u32 free_reg, reg; |
| 252 | resource = 0; |
| 253 | free_reg = 0; |
| 254 | for (reg = 0x80; reg <= 0xb8; reg += 0x8) { |
| 255 | int result; |
| 256 | result = reg_useable(reg, dev, nodeid, link); |
| 257 | if (result == 1) { |
| 258 | /* I have been allocated this one */ |
| 259 | break; |
| 260 | } |
| 261 | else if (result > 1) { |
| 262 | /* I have a free register pair */ |
| 263 | free_reg = reg; |
| 264 | } |
| 265 | } |
| 266 | if (reg > 0xb8) { |
| 267 | reg = free_reg; |
| 268 | } |
| 269 | |
| 270 | resource = new_resource(dev, IOINDEX(0x1000 + reg, link)); |
| 271 | return resource; |
| 272 | } |
| 273 | |
| 274 | static void amdfam16_link_read_bases(device_t dev, u32 nodeid, u32 link) |
| 275 | { |
| 276 | struct resource *resource; |
| 277 | |
| 278 | /* Initialize the io space constraints on the current bus */ |
| 279 | resource = amdfam16_find_iopair(dev, nodeid, link); |
| 280 | if (resource) { |
| 281 | u32 align; |
| 282 | align = log2(HT_IO_HOST_ALIGN); |
| 283 | resource->base = 0; |
| 284 | resource->size = 0; |
| 285 | resource->align = align; |
| 286 | resource->gran = align; |
| 287 | resource->limit = 0xffffUL; |
| 288 | resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE; |
| 289 | } |
| 290 | |
| 291 | /* Initialize the prefetchable memory constraints on the current bus */ |
| 292 | resource = amdfam16_find_mempair(dev, nodeid, link); |
| 293 | if (resource) { |
| 294 | resource->base = 0; |
| 295 | resource->size = 0; |
| 296 | resource->align = log2(HT_MEM_HOST_ALIGN); |
| 297 | resource->gran = log2(HT_MEM_HOST_ALIGN); |
| 298 | resource->limit = 0xffffffffffULL; |
| 299 | resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH; |
| 300 | resource->flags |= IORESOURCE_BRIDGE; |
| 301 | } |
| 302 | |
| 303 | /* Initialize the memory constraints on the current bus */ |
| 304 | resource = amdfam16_find_mempair(dev, nodeid, link); |
| 305 | if (resource) { |
| 306 | resource->base = 0; |
| 307 | resource->size = 0; |
| 308 | resource->align = log2(HT_MEM_HOST_ALIGN); |
| 309 | resource->gran = log2(HT_MEM_HOST_ALIGN); |
| 310 | resource->limit = 0xffffffffffULL; |
| 311 | resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE; |
| 312 | } |
| 313 | |
| 314 | } |
| 315 | |
| 316 | static void read_resources(device_t dev) |
| 317 | { |
| 318 | u32 nodeid; |
| 319 | struct bus *link; |
| 320 | |
| 321 | nodeid = amdfam16_nodeid(dev); |
| 322 | for (link = dev->link_list; link; link = link->next) { |
| 323 | if (link->children) { |
| 324 | amdfam16_link_read_bases(dev, nodeid, link->link_num); |
| 325 | } |
| 326 | } |
Kyösti Mälkki | 5d49038 | 2015-05-27 07:58:22 +0300 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * This MMCONF resource must be reserved in the PCI domain. |
| 330 | * It is not honored by the coreboot resource allocator if it is in |
| 331 | * the CPU_CLUSTER. |
| 332 | */ |
Kyösti Mälkki | e25b5ef | 2016-12-02 08:56:05 +0200 | [diff] [blame] | 333 | mmconf_resource(dev, 0xc0010058); |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static void set_resource(device_t dev, struct resource *resource, u32 nodeid) |
| 337 | { |
| 338 | resource_t rbase, rend; |
| 339 | unsigned reg, link_num; |
| 340 | char buf[50]; |
| 341 | |
| 342 | /* Make certain the resource has actually been set */ |
| 343 | if (!(resource->flags & IORESOURCE_ASSIGNED)) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | /* If I have already stored this resource don't worry about it */ |
| 348 | if (resource->flags & IORESOURCE_STORED) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | /* Only handle PCI memory and IO resources */ |
| 353 | if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO))) |
| 354 | return; |
| 355 | |
| 356 | /* Ensure I am actually looking at a resource of function 1 */ |
| 357 | if ((resource->index & 0xffff) < 0x1000) { |
| 358 | return; |
| 359 | } |
| 360 | /* Get the base address */ |
| 361 | rbase = resource->base; |
| 362 | |
| 363 | /* Get the limit (rounded up) */ |
| 364 | rend = resource_end(resource); |
| 365 | |
| 366 | /* Get the register and link */ |
| 367 | reg = resource->index & 0xfff; // 4k |
| 368 | link_num = IOINDEX_LINK(resource->index); |
| 369 | |
| 370 | if (resource->flags & IORESOURCE_IO) { |
| 371 | set_io_addr_reg(dev, nodeid, link_num, reg, rbase>>8, rend>>8); |
| 372 | } |
| 373 | else if (resource->flags & IORESOURCE_MEM) { |
Elyes HAOUAS | 7db506c | 2016-10-02 11:56:39 +0200 | [diff] [blame] | 374 | set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >>24), rbase>>8, rend>>8, node_nums); // [39:8] |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 375 | } |
| 376 | resource->flags |= IORESOURCE_STORED; |
Elyes HAOUAS | 0d4b11a | 2016-10-03 21:57:21 +0200 | [diff] [blame] | 377 | snprintf(buf, sizeof(buf), " <node %x link %x>", |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 378 | nodeid, link_num); |
| 379 | report_resource_stored(dev, resource, buf); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * I tried to reuse the resource allocation code in set_resource() |
| 384 | * but it is too difficult to deal with the resource allocation magic. |
| 385 | */ |
| 386 | |
| 387 | static void create_vga_resource(device_t dev, unsigned nodeid) |
| 388 | { |
| 389 | struct bus *link; |
| 390 | |
| 391 | /* find out which link the VGA card is connected, |
| 392 | * we only deal with the 'first' vga card */ |
| 393 | for (link = dev->link_list; link; link = link->next) { |
| 394 | if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) { |
Martin Roth | 77a58b9 | 2017-06-24 14:45:48 -0600 | [diff] [blame] | 395 | #if IS_ENABLED(CONFIG_MULTIPLE_VGA_ADAPTERS) |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 396 | extern device_t vga_pri; // the primary vga device, defined in device.c |
| 397 | printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d bus range [%d,%d]\n", vga_pri->bus->secondary, |
| 398 | link->secondary,link->subordinate); |
| 399 | /* We need to make sure the vga_pri is under the link */ |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 400 | if ((vga_pri->bus->secondary >= link->secondary) && |
| 401 | (vga_pri->bus->secondary <= link->subordinate)) |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 402 | #endif |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /* no VGA card installed */ |
| 408 | if (link == NULL) |
| 409 | return; |
| 410 | |
| 411 | printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, sblink); |
| 412 | set_vga_enable_reg(nodeid, sblink); |
| 413 | } |
| 414 | |
| 415 | static void set_resources(device_t dev) |
| 416 | { |
| 417 | unsigned nodeid; |
| 418 | struct bus *bus; |
| 419 | struct resource *res; |
| 420 | |
| 421 | /* Find the nodeid */ |
| 422 | nodeid = amdfam16_nodeid(dev); |
| 423 | |
| 424 | create_vga_resource(dev, nodeid); //TODO: do we need this? |
| 425 | |
| 426 | /* Set each resource we have found */ |
| 427 | for (res = dev->resource_list; res; res = res->next) { |
| 428 | set_resource(dev, res, nodeid); |
| 429 | } |
| 430 | |
| 431 | for (bus = dev->link_list; bus; bus = bus->next) { |
| 432 | if (bus->children) { |
| 433 | assign_resources(bus); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | static void northbridge_init(struct device *dev) |
| 439 | { |
| 440 | } |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 441 | |
Vladimir Serbinenko | 807127f | 2014-11-09 13:36:18 +0100 | [diff] [blame] | 442 | static unsigned long acpi_fill_hest(acpi_hest_t *hest) |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 443 | { |
| 444 | void *addr, *current; |
| 445 | |
| 446 | /* Skip the HEST header. */ |
| 447 | current = (void *)(hest + 1); |
| 448 | |
| 449 | addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE); |
| 450 | if (addr != NULL) |
| 451 | current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2); |
| 452 | |
| 453 | addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC); |
| 454 | if (addr != NULL) |
| 455 | current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2); |
| 456 | |
| 457 | return (unsigned long)current; |
| 458 | } |
| 459 | |
Alexander Couzens | 5eea458 | 2015-04-12 22:18:55 +0200 | [diff] [blame] | 460 | static void northbridge_fill_ssdt_generator(device_t device) |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 461 | { |
| 462 | msr_t msr; |
| 463 | char pscope[] = "\\_SB.PCI0"; |
| 464 | |
| 465 | acpigen_write_scope(pscope); |
| 466 | msr = rdmsr(TOP_MEM); |
| 467 | acpigen_write_name_dword("TOM1", msr.lo); |
| 468 | msr = rdmsr(TOP_MEM2); |
| 469 | /* |
| 470 | * Since XP only implements parts of ACPI 2.0, we can't use a qword |
| 471 | * here. |
| 472 | * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt |
| 473 | * slide 22ff. |
| 474 | * Shift value right by 20 bit to make it fit into 32bit, |
| 475 | * giving us 1MB granularity and a limit of almost 4Exabyte of memory. |
| 476 | */ |
| 477 | acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20); |
| 478 | acpigen_pop_len(); |
| 479 | } |
| 480 | |
Alexander Couzens | 83fc32f | 2015-04-12 22:28:37 +0200 | [diff] [blame] | 481 | static unsigned long agesa_write_acpi_tables(device_t device, |
| 482 | unsigned long current, |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 483 | acpi_rsdp_t *rsdp) |
| 484 | { |
| 485 | acpi_srat_t *srat; |
| 486 | acpi_slit_t *slit; |
| 487 | acpi_header_t *ssdt; |
| 488 | acpi_header_t *alib; |
| 489 | acpi_header_t *ivrs; |
| 490 | acpi_hest_t *hest; |
| 491 | |
| 492 | /* HEST */ |
| 493 | current = ALIGN(current, 8); |
| 494 | hest = (acpi_hest_t *)current; |
Vladimir Serbinenko | 807127f | 2014-11-09 13:36:18 +0100 | [diff] [blame] | 495 | acpi_write_hest((void *)current, acpi_fill_hest); |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 496 | acpi_add_table(rsdp, (void *)current); |
| 497 | current += ((acpi_header_t *)current)->length; |
| 498 | |
| 499 | current = ALIGN(current, 8); |
| 500 | printk(BIOS_DEBUG, "ACPI: * IVRS at %lx\n", current); |
| 501 | ivrs = agesawrapper_getlateinitptr(PICK_IVRS); |
| 502 | if (ivrs != NULL) { |
| 503 | memcpy((void *)current, ivrs, ivrs->length); |
| 504 | ivrs = (acpi_header_t *) current; |
| 505 | current += ivrs->length; |
| 506 | acpi_add_table(rsdp, ivrs); |
| 507 | } else { |
| 508 | printk(BIOS_DEBUG, " AGESA IVRS table NULL. Skipping.\n"); |
| 509 | } |
| 510 | |
| 511 | /* SRAT */ |
| 512 | current = ALIGN(current, 8); |
| 513 | printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current); |
| 514 | srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT); |
| 515 | if (srat != NULL) { |
| 516 | memcpy((void *)current, srat, srat->header.length); |
| 517 | srat = (acpi_srat_t *) current; |
| 518 | current += srat->header.length; |
| 519 | acpi_add_table(rsdp, srat); |
| 520 | } else { |
| 521 | printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n"); |
| 522 | } |
| 523 | |
| 524 | /* SLIT */ |
| 525 | current = ALIGN(current, 8); |
| 526 | printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current); |
| 527 | slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT); |
| 528 | if (slit != NULL) { |
| 529 | memcpy((void *)current, slit, slit->header.length); |
| 530 | slit = (acpi_slit_t *) current; |
| 531 | current += slit->header.length; |
| 532 | acpi_add_table(rsdp, slit); |
| 533 | } else { |
| 534 | printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n"); |
| 535 | } |
| 536 | |
| 537 | /* ALIB */ |
| 538 | current = ALIGN(current, 16); |
| 539 | printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current); |
| 540 | alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB); |
| 541 | if (alib != NULL) { |
| 542 | memcpy((void *)current, alib, alib->length); |
| 543 | alib = (acpi_header_t *) current; |
| 544 | current += alib->length; |
| 545 | acpi_add_table(rsdp, (void *)alib); |
| 546 | } |
| 547 | else { |
| 548 | printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n"); |
| 549 | } |
| 550 | |
| 551 | /* this pstate ssdt may cause Blue Screen: Fixed: Keep this comment for a while. */ |
| 552 | /* SSDT */ |
| 553 | current = ALIGN(current, 16); |
| 554 | printk(BIOS_DEBUG, "ACPI: * SSDT at %lx\n", current); |
| 555 | ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE); |
| 556 | if (ssdt != NULL) { |
| 557 | memcpy((void *)current, ssdt, ssdt->length); |
| 558 | ssdt = (acpi_header_t *) current; |
| 559 | current += ssdt->length; |
| 560 | } |
| 561 | else { |
| 562 | printk(BIOS_DEBUG, " AGESA PState table NULL. Skipping.\n"); |
| 563 | } |
| 564 | acpi_add_table(rsdp,ssdt); |
| 565 | |
| 566 | printk(BIOS_DEBUG, "ACPI: * SSDT for PState at %lx\n", current); |
| 567 | return current; |
| 568 | } |
| 569 | |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 570 | static struct device_operations northbridge_operations = { |
| 571 | .read_resources = read_resources, |
| 572 | .set_resources = set_resources, |
| 573 | .enable_resources = pci_dev_enable_resources, |
| 574 | .init = northbridge_init, |
Kyösti Mälkki | 0b5b541 | 2014-11-26 08:11:07 +0200 | [diff] [blame] | 575 | .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator, |
| 576 | .write_acpi_tables = agesa_write_acpi_tables, |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 577 | .enable = 0, |
| 578 | .ops_pci = 0, |
| 579 | }; |
| 580 | |
| 581 | static const struct pci_driver family16_northbridge __pci_driver = { |
| 582 | .ops = &northbridge_operations, |
| 583 | .vendor = PCI_VENDOR_ID_AMD, |
Marshall Dawson | 463f46e | 2016-10-14 20:46:08 -0600 | [diff] [blame] | 584 | .device = PCI_DEVICE_ID_AMD_16H_MODEL_303F_NB_HT, |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 585 | }; |
| 586 | |
| 587 | static const struct pci_driver family10_northbridge __pci_driver = { |
| 588 | .ops = &northbridge_operations, |
| 589 | .vendor = PCI_VENDOR_ID_AMD, |
| 590 | .device = PCI_DEVICE_ID_AMD_10H_NB_HT, |
| 591 | }; |
| 592 | |
Dave Frodin | 891f71a | 2015-01-19 15:58:24 -0700 | [diff] [blame] | 593 | static void fam16_finalize(void *chip_info) |
| 594 | { |
| 595 | device_t dev; |
| 596 | u32 value; |
| 597 | dev = dev_find_slot(0, PCI_DEVFN(0, 0)); /* clear IoapicSbFeatureEn */ |
| 598 | pci_write_config32(dev, 0xF8, 0); |
| 599 | pci_write_config32(dev, 0xFC, 5); /* TODO: move it to dsdt.asl */ |
| 600 | |
| 601 | /* disable No Snoop */ |
| 602 | dev = dev_find_slot(0, PCI_DEVFN(1, 1)); |
| 603 | value = pci_read_config32(dev, 0x60); |
| 604 | value &= ~(1 << 11); |
| 605 | pci_write_config32(dev, 0x60, value); |
| 606 | } |
| 607 | |
Kyösti Mälkki | e4c17ce | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 608 | struct chip_operations northbridge_amd_pi_00730F01_ops = { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 609 | CHIP_NAME("AMD FAM16 Northbridge") |
| 610 | .enable_dev = 0, |
Dave Frodin | 891f71a | 2015-01-19 15:58:24 -0700 | [diff] [blame] | 611 | .final = fam16_finalize, |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | static void domain_read_resources(device_t dev) |
| 615 | { |
| 616 | unsigned reg; |
| 617 | |
| 618 | /* Find the already assigned resource pairs */ |
| 619 | get_fx_devs(); |
| 620 | for (reg = 0x80; reg <= 0xd8; reg+= 0x08) { |
| 621 | u32 base, limit; |
| 622 | base = f1_read_config32(reg); |
| 623 | limit = f1_read_config32(reg + 0x04); |
| 624 | /* Is this register allocated? */ |
| 625 | if ((base & 3) != 0) { |
| 626 | unsigned nodeid, reg_link; |
| 627 | device_t reg_dev; |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 628 | if (reg < 0xc0) { // mmio |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 629 | nodeid = (limit & 0xf) + (base&0x30); |
| 630 | } else { // io |
| 631 | nodeid = (limit & 0xf) + ((base>>4)&0x30); |
| 632 | } |
| 633 | reg_link = (limit >> 4) & 7; |
| 634 | reg_dev = __f0_dev[nodeid]; |
| 635 | if (reg_dev) { |
| 636 | /* Reserve the resource */ |
| 637 | struct resource *res; |
| 638 | res = new_resource(reg_dev, IOINDEX(0x1000 + reg, reg_link)); |
| 639 | if (res) { |
| 640 | res->flags = 1; |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | /* FIXME: do we need to check extend conf space? |
| 646 | I don't believe that much preset value */ |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 647 | pci_domain_read_resources(dev); |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void domain_enable_resources(device_t dev) |
| 651 | { |
Kyösti Mälkki | 903ce25 | 2016-11-25 11:21:02 +0200 | [diff] [blame] | 652 | #if IS_ENABLED(CONFIG_BINARYPI_LEGACY_WRAPPER) |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 653 | /* Must be called after PCI enumeration and resource allocation */ |
| 654 | if (!acpi_is_wakeup_s3()) |
| 655 | AGESAWRAPPER(amdinitmid); |
| 656 | |
| 657 | printk(BIOS_DEBUG, " ader - leaving domain_enable_resources.\n"); |
Kyösti Mälkki | 903ce25 | 2016-11-25 11:21:02 +0200 | [diff] [blame] | 658 | #endif |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | #if CONFIG_HW_MEM_HOLE_SIZEK != 0 |
| 662 | struct hw_mem_hole_info { |
| 663 | unsigned hole_startk; |
| 664 | int node_id; |
| 665 | }; |
| 666 | static struct hw_mem_hole_info get_hw_mem_hole_info(void) |
| 667 | { |
| 668 | struct hw_mem_hole_info mem_hole; |
| 669 | int i; |
| 670 | mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK; |
| 671 | mem_hole.node_id = -1; |
| 672 | for (i = 0; i < node_nums; i++) { |
| 673 | dram_base_mask_t d; |
| 674 | u32 hole; |
| 675 | d = get_dram_base_mask(i); |
| 676 | if (!(d.mask & 1)) continue; // no memory on this node |
| 677 | hole = pci_read_config32(__f1_dev[i], 0xf0); |
| 678 | if (hole & 2) { // we find the hole |
| 679 | mem_hole.hole_startk = (hole & (0xff<<24)) >> 10; |
| 680 | mem_hole.node_id = i; // record the node No with hole |
| 681 | break; // only one hole |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | /* We need to double check if there is special set on base reg and limit reg |
| 686 | * are not continuous instead of hole, it will find out its hole_startk. |
| 687 | */ |
| 688 | if (mem_hole.node_id == -1) { |
| 689 | resource_t limitk_pri = 0; |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 690 | for (i = 0; i < node_nums; i++) { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 691 | dram_base_mask_t d; |
| 692 | resource_t base_k, limit_k; |
| 693 | d = get_dram_base_mask(i); |
| 694 | if (!(d.base & 1)) continue; |
| 695 | base_k = ((resource_t)(d.base & 0x1fffff00)) <<9; |
| 696 | if (base_k > 4 *1024 * 1024) break; // don't need to go to check |
| 697 | if (limitk_pri != base_k) { // we find the hole |
| 698 | mem_hole.hole_startk = (unsigned)limitk_pri; // must beblow 4G |
| 699 | mem_hole.node_id = i; |
| 700 | break; //only one hole |
| 701 | } |
| 702 | limit_k = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9; |
| 703 | limitk_pri = limit_k; |
| 704 | } |
| 705 | } |
| 706 | return mem_hole; |
| 707 | } |
| 708 | #endif |
| 709 | |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 710 | static void domain_set_resources(device_t dev) |
| 711 | { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 712 | unsigned long mmio_basek; |
| 713 | u32 pci_tolm; |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 714 | int i, idx; |
| 715 | struct bus *link; |
| 716 | #if CONFIG_HW_MEM_HOLE_SIZEK != 0 |
| 717 | struct hw_mem_hole_info mem_hole; |
| 718 | u32 reset_memhole = 1; |
| 719 | #endif |
| 720 | |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 721 | pci_tolm = 0xffffffffUL; |
| 722 | for (link = dev->link_list; link; link = link->next) { |
| 723 | pci_tolm = find_pci_tolm(link); |
| 724 | } |
| 725 | |
| 726 | // FIXME handle interleaved nodes. If you fix this here, please fix |
| 727 | // amdk8, too. |
| 728 | mmio_basek = pci_tolm >> 10; |
| 729 | /* Round mmio_basek to something the processor can support */ |
| 730 | mmio_basek &= ~((1 << 6) -1); |
| 731 | |
| 732 | // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M |
| 733 | // MMIO hole. If you fix this here, please fix amdk8, too. |
| 734 | /* Round the mmio hole to 64M */ |
| 735 | mmio_basek &= ~((64*1024) - 1); |
| 736 | |
| 737 | #if CONFIG_HW_MEM_HOLE_SIZEK != 0 |
| 738 | /* if the hw mem hole is already set in raminit stage, here we will compare |
| 739 | * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will |
| 740 | * use hole_basek as mmio_basek and we don't need to reset hole. |
| 741 | * otherwise We reset the hole to the mmio_basek |
| 742 | */ |
| 743 | |
| 744 | mem_hole = get_hw_mem_hole_info(); |
| 745 | |
| 746 | // Use hole_basek as mmio_basek, and we don't need to reset hole anymore |
| 747 | if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) { |
| 748 | mmio_basek = mem_hole.hole_startk; |
| 749 | reset_memhole = 0; |
| 750 | } |
| 751 | #endif |
| 752 | |
| 753 | idx = 0x10; |
| 754 | for (i = 0; i < node_nums; i++) { |
| 755 | dram_base_mask_t d; |
| 756 | resource_t basek, limitk, sizek; // 4 1T |
| 757 | |
| 758 | d = get_dram_base_mask(i); |
| 759 | |
| 760 | if (!(d.mask & 1)) continue; |
| 761 | basek = ((resource_t)(d.base & 0x1fffff00)) << 9; // could overflow, we may lost 6 bit here |
Elyes HAOUAS | 7db506c | 2016-10-02 11:56:39 +0200 | [diff] [blame] | 762 | limitk = ((resource_t)(((d.mask & ~1) + 0x000FF) & 0x1fffff00)) << 9; |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 763 | |
| 764 | sizek = limitk - basek; |
| 765 | |
| 766 | /* see if we need a hole from 0xa0000 to 0xbffff */ |
| 767 | if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) { |
| 768 | ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek); |
| 769 | idx += 0x10; |
| 770 | basek = (8*64)+(16*16); |
| 771 | sizek = limitk - ((8*64)+(16*16)); |
| 772 | |
| 773 | } |
| 774 | |
| 775 | //printk(BIOS_DEBUG, "node %d : mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", i, mmio_basek, basek, limitk); |
| 776 | |
| 777 | /* split the region to accommodate pci memory space */ |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 778 | if ((basek < 4*1024*1024) && (limitk > mmio_basek)) { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 779 | if (basek <= mmio_basek) { |
| 780 | unsigned pre_sizek; |
| 781 | pre_sizek = mmio_basek - basek; |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 782 | if (pre_sizek > 0) { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 783 | ram_resource(dev, (idx | i), basek, pre_sizek); |
| 784 | idx += 0x10; |
| 785 | sizek -= pre_sizek; |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 786 | } |
| 787 | basek = mmio_basek; |
| 788 | } |
| 789 | if ((basek + sizek) <= 4*1024*1024) { |
| 790 | sizek = 0; |
| 791 | } |
| 792 | else { |
| 793 | uint64_t topmem2 = bsp_topmem2(); |
| 794 | basek = 4*1024*1024; |
| 795 | sizek = topmem2/1024 - basek; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | ram_resource(dev, (idx | i), basek, sizek); |
| 800 | idx += 0x10; |
| 801 | printk(BIOS_DEBUG, "node %d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", |
| 802 | i, mmio_basek, basek, limitk); |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 803 | } |
| 804 | |
Kyösti Mälkki | e87564f | 2017-04-15 20:07:53 +0300 | [diff] [blame] | 805 | add_uma_resource_below_tolm(dev, 7); |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 806 | |
Elyes HAOUAS | 5a7e72f | 2016-08-23 21:36:02 +0200 | [diff] [blame] | 807 | for (link = dev->link_list; link; link = link->next) { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 808 | if (link->children) { |
| 809 | assign_resources(link); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 814 | static const char *domain_acpi_name(const struct device *dev) |
Philipp Deppenwiese | 3067012 | 2017-03-01 02:24:33 +0100 | [diff] [blame] | 815 | { |
| 816 | if (dev->path.type == DEVICE_PATH_DOMAIN) |
| 817 | return "PCI0"; |
| 818 | |
| 819 | return NULL; |
| 820 | } |
| 821 | |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 822 | static struct device_operations pci_domain_ops = { |
| 823 | .read_resources = domain_read_resources, |
| 824 | .set_resources = domain_set_resources, |
| 825 | .enable_resources = domain_enable_resources, |
| 826 | .init = NULL, |
| 827 | .scan_bus = pci_domain_scan_bus, |
| 828 | .ops_pci_bus = pci_bus_default_ops, |
Philipp Deppenwiese | 3067012 | 2017-03-01 02:24:33 +0100 | [diff] [blame] | 829 | .acpi_name = domain_acpi_name, |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 830 | }; |
| 831 | |
| 832 | static void sysconf_init(device_t dev) // first node |
| 833 | { |
| 834 | sblink = (pci_read_config32(dev, 0x64)>>8) & 7; // don't forget sublink1 |
| 835 | node_nums = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1; //NodeCnt[2:0] |
| 836 | } |
| 837 | |
| 838 | static void add_more_links(device_t dev, unsigned total_links) |
| 839 | { |
| 840 | struct bus *link, *last = NULL; |
| 841 | int link_num; |
| 842 | |
| 843 | for (link = dev->link_list; link; link = link->next) |
| 844 | last = link; |
| 845 | |
| 846 | if (last) { |
| 847 | int links = total_links - last->link_num; |
| 848 | link_num = last->link_num; |
| 849 | if (links > 0) { |
| 850 | link = malloc(links*sizeof(*link)); |
| 851 | if (!link) |
| 852 | die("Couldn't allocate more links!\n"); |
| 853 | memset(link, 0, links*sizeof(*link)); |
| 854 | last->next = link; |
| 855 | } |
| 856 | } |
| 857 | else { |
| 858 | link_num = -1; |
| 859 | link = malloc(total_links*sizeof(*link)); |
| 860 | memset(link, 0, total_links*sizeof(*link)); |
| 861 | dev->link_list = link; |
| 862 | } |
| 863 | |
| 864 | for (link_num = link_num + 1; link_num < total_links; link_num++) { |
| 865 | link->link_num = link_num; |
| 866 | link->dev = dev; |
| 867 | link->next = link + 1; |
| 868 | last = link; |
| 869 | link = link->next; |
| 870 | } |
| 871 | last->next = NULL; |
| 872 | } |
| 873 | |
Kyösti Mälkki | 580e722 | 2015-03-19 21:04:23 +0200 | [diff] [blame] | 874 | static void cpu_bus_scan(device_t dev) |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 875 | { |
| 876 | struct bus *cpu_bus; |
| 877 | device_t dev_mc; |
| 878 | #if CONFIG_CBB |
| 879 | device_t pci_domain; |
| 880 | #endif |
| 881 | int i,j; |
| 882 | int coreid_bits; |
| 883 | int core_max = 0; |
| 884 | unsigned ApicIdCoreIdSize; |
| 885 | unsigned core_nums; |
| 886 | int siblings = 0; |
| 887 | unsigned int family; |
| 888 | u32 modules = 0; |
| 889 | VOID* modules_ptr = &modules; |
| 890 | BUILD_OPT_CFG* options = NULL; |
| 891 | int ioapic_count = 0; |
| 892 | |
| 893 | // TODO Remove the printk's. |
| 894 | printk(BIOS_SPEW, "MullinsPI Debug: Grabbing the AMD Topology Information.\n"); |
| 895 | AmdGetValue(AMD_GLOBAL_USER_OPTIONS, (VOID**)&options, sizeof(options)); |
| 896 | AmdGetValue(AMD_GLOBAL_NUM_MODULES, &modules_ptr, sizeof(modules)); |
Alexandru Gagniuc | 2e0cf14 | 2014-12-28 20:38:32 -0600 | [diff] [blame] | 897 | modules = *(u32*)modules_ptr; |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 898 | ASSERT(modules > 0); |
| 899 | ASSERT(options); |
| 900 | ioapic_count = (int)options->CfgPlatNumIoApics; |
| 901 | ASSERT(ioapic_count > 0); |
| 902 | printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of Modules (@0x%p) is %d\n", modules_ptr, modules); |
| 903 | printk(BIOS_SPEW, "MullinsPI Debug: AMD Topology Number of IOAPICs (@0x%p) is %d\n", options, (int)options->CfgPlatNumIoApics); |
| 904 | |
| 905 | #if CONFIG_CBB |
| 906 | dev_mc = dev_find_slot(0, PCI_DEVFN(CONFIG_CDB, 0)); //0x00 |
| 907 | if (dev_mc && dev_mc->bus) { |
| 908 | printk(BIOS_DEBUG, "%s found", dev_path(dev_mc)); |
| 909 | pci_domain = dev_mc->bus->dev; |
| 910 | if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) { |
| 911 | printk(BIOS_DEBUG, "\n%s move to ",dev_path(dev_mc)); |
| 912 | dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff |
| 913 | printk(BIOS_DEBUG, "%s",dev_path(dev_mc)); |
| 914 | } else { |
| 915 | printk(BIOS_DEBUG, " but it is not under pci_domain directly "); |
| 916 | } |
| 917 | printk(BIOS_DEBUG, "\n"); |
| 918 | } |
| 919 | dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0)); |
| 920 | if (!dev_mc) { |
| 921 | dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0)); |
| 922 | if (dev_mc && dev_mc->bus) { |
| 923 | printk(BIOS_DEBUG, "%s found\n", dev_path(dev_mc)); |
| 924 | pci_domain = dev_mc->bus->dev; |
| 925 | if (pci_domain && (pci_domain->path.type == DEVICE_PATH_DOMAIN)) { |
| 926 | if ((pci_domain->link_list) && (pci_domain->link_list->children == dev_mc)) { |
| 927 | printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc)); |
| 928 | dev_mc->bus->secondary = CONFIG_CBB; // move to 0xff |
| 929 | printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc)); |
| 930 | while (dev_mc) { |
| 931 | printk(BIOS_DEBUG, "%s move to ",dev_path(dev_mc)); |
| 932 | dev_mc->path.pci.devfn -= PCI_DEVFN(0x18,0); |
| 933 | printk(BIOS_DEBUG, "%s\n",dev_path(dev_mc)); |
| 934 | dev_mc = dev_mc->sibling; |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | #endif |
| 941 | dev_mc = dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB, 0)); |
| 942 | if (!dev_mc) { |
| 943 | printk(BIOS_ERR, "%02x:%02x.0 not found", CONFIG_CBB, CONFIG_CDB); |
| 944 | die(""); |
| 945 | } |
| 946 | sysconf_init(dev_mc); |
| 947 | #if CONFIG_CBB && (MAX_NODE_NUMS > 32) |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 948 | if (node_nums > 32) { // need to put node 32 to node 63 to bus 0xfe |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 949 | if (pci_domain->link_list && !pci_domain->link_list->next) { |
| 950 | struct bus *new_link = new_link(pci_domain); |
| 951 | pci_domain->link_list->next = new_link; |
| 952 | new_link->link_num = 1; |
| 953 | new_link->dev = pci_domain; |
| 954 | new_link->children = 0; |
| 955 | printk(BIOS_DEBUG, "%s links now 2\n", dev_path(pci_domain)); |
| 956 | } |
| 957 | pci_domain->link_list->next->secondary = CONFIG_CBB - 1; |
| 958 | } |
| 959 | #endif |
| 960 | |
| 961 | /* Get Max Number of cores(MNC) */ |
Kyösti Mälkki | d41feed | 2017-09-24 16:23:57 +0300 | [diff] [blame^] | 962 | coreid_bits = (cpuid_ecx(0x80000008) & 0x0000F000) >> 12; |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 963 | core_max = 1 << (coreid_bits & 0x000F); //mnc |
| 964 | |
| 965 | ApicIdCoreIdSize = ((cpuid_ecx(0x80000008)>>12) & 0xF); |
| 966 | if (ApicIdCoreIdSize) { |
| 967 | core_nums = (1 << ApicIdCoreIdSize) - 1; |
| 968 | } else { |
| 969 | core_nums = 3; //quad core |
| 970 | } |
| 971 | |
| 972 | /* Find which cpus are present */ |
| 973 | cpu_bus = dev->link_list; |
| 974 | for (i = 0; i < node_nums; i++) { |
| 975 | device_t cdb_dev; |
| 976 | unsigned busn, devn; |
| 977 | struct bus *pbus; |
| 978 | |
| 979 | busn = CONFIG_CBB; |
| 980 | devn = CONFIG_CDB + i; |
| 981 | pbus = dev_mc->bus; |
| 982 | #if CONFIG_CBB && (MAX_NODE_NUMS > 32) |
| 983 | if (i >= 32) { |
| 984 | busn--; |
| 985 | devn -= 32; |
| 986 | pbus = pci_domain->link_list->next; |
| 987 | } |
| 988 | #endif |
| 989 | |
| 990 | /* Find the cpu's pci device */ |
| 991 | cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0)); |
| 992 | if (!cdb_dev) { |
| 993 | /* If I am probing things in a weird order |
| 994 | * ensure all of the cpu's pci devices are found. |
| 995 | */ |
| 996 | int fn; |
Elyes HAOUAS | 5a7e72f | 2016-08-23 21:36:02 +0200 | [diff] [blame] | 997 | for (fn = 0; fn <= 5; fn++) { //FBDIMM? |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 998 | cdb_dev = pci_probe_dev(NULL, pbus, |
| 999 | PCI_DEVFN(devn, fn)); |
| 1000 | } |
| 1001 | cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 0)); |
| 1002 | } else { |
| 1003 | /* Ok, We need to set the links for that device. |
| 1004 | * otherwise the device under it will not be scanned |
| 1005 | */ |
Kyösti Mälkki | c5163ed8 | 2015-02-04 13:25:37 +0200 | [diff] [blame] | 1006 | |
| 1007 | add_more_links(cdb_dev, 4); |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | family = cpuid_eax(1); |
| 1011 | family = (family >> 20) & 0xFF; |
| 1012 | if (family == 1) { //f10 |
| 1013 | u32 dword; |
| 1014 | cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 3)); |
| 1015 | dword = pci_read_config32(cdb_dev, 0xe8); |
| 1016 | siblings = ((dword & BIT15) >> 13) | ((dword & (BIT13 | BIT12)) >> 12); |
| 1017 | } else if (family == 7) {//f16 |
| 1018 | cdb_dev = dev_find_slot(busn, PCI_DEVFN(devn, 5)); |
| 1019 | if (cdb_dev && cdb_dev->enabled) { |
| 1020 | siblings = pci_read_config32(cdb_dev, 0x84); |
| 1021 | siblings &= 0xFF; |
| 1022 | } |
| 1023 | } else { |
| 1024 | siblings = 0; //default one core |
| 1025 | } |
| 1026 | int enable_node = cdb_dev && cdb_dev->enabled; |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 1027 | printk(BIOS_SPEW, "%s family%xh, core_max = 0x%x, core_nums = 0x%x, siblings = 0x%x\n", |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1028 | dev_path(cdb_dev), 0x0f + family, core_max, core_nums, siblings); |
| 1029 | |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 1030 | for (j = 0; j <= siblings; j++) { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1031 | u32 lapicid_start = 0; |
| 1032 | |
| 1033 | /* |
| 1034 | * APIC ID calucation is tightly coupled with AGESA v5 code. |
| 1035 | * This calculation MUST match the assignment calculation done |
| 1036 | * in LocalApicInitializationAtEarly() function. |
| 1037 | * And reference GetLocalApicIdForCore() |
| 1038 | * |
| 1039 | * Apply apic enumeration rules |
| 1040 | * For systems with >= 16 APICs, put the IO-APICs at 0..n and |
| 1041 | * put the local-APICs at m..z |
| 1042 | * |
| 1043 | * This is needed because many IO-APIC devices only have 4 bits |
| 1044 | * for their APIC id and therefore must reside at 0..15 |
Elyes HAOUAS | 6e8b3c1 | 2016-09-02 19:22:00 +0200 | [diff] [blame] | 1045 | */ |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1046 | if ((node_nums * core_max) + ioapic_count >= 0x10) { |
| 1047 | lapicid_start = (ioapic_count - 1) / core_max; |
| 1048 | lapicid_start = (lapicid_start + 1) * core_max; |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 1049 | printk(BIOS_SPEW, "lpaicid_start = 0x%x ", lapicid_start); |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1050 | } |
| 1051 | u32 apic_id = (lapicid_start * (i/modules + 1)) + ((i % modules) ? (j + (siblings + 1)) : j); |
Elyes HAOUAS | a813160 | 2016-09-19 10:27:57 -0600 | [diff] [blame] | 1052 | printk(BIOS_SPEW, "node 0x%x core 0x%x apicid = 0x%x\n", |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1053 | i, j, apic_id); |
| 1054 | |
| 1055 | device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node); |
| 1056 | if (cpu) |
| 1057 | amd_cpu_topology(cpu, i, j); |
| 1058 | } //j |
| 1059 | } |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static void cpu_bus_init(device_t dev) |
| 1063 | { |
| 1064 | initialize_cpus(dev->link_list); |
| 1065 | } |
| 1066 | |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1067 | static struct device_operations cpu_bus_ops = { |
Kyösti Mälkki | 48f82a9 | 2016-12-02 16:02:30 +0200 | [diff] [blame] | 1068 | .read_resources = DEVICE_NOOP, |
| 1069 | .set_resources = DEVICE_NOOP, |
Edward O'Callaghan | 812d2a4 | 2014-10-31 08:17:23 +1100 | [diff] [blame] | 1070 | .enable_resources = DEVICE_NOOP, |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1071 | .init = cpu_bus_init, |
| 1072 | .scan_bus = cpu_bus_scan, |
| 1073 | }; |
| 1074 | |
| 1075 | static void root_complex_enable_dev(struct device *dev) |
| 1076 | { |
| 1077 | static int done = 0; |
| 1078 | |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1079 | if (!done) { |
| 1080 | setup_bsp_ramtop(); |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1081 | done = 1; |
| 1082 | } |
| 1083 | |
| 1084 | /* Set the operations if it is a special bus type */ |
| 1085 | if (dev->path.type == DEVICE_PATH_DOMAIN) { |
| 1086 | dev->ops = &pci_domain_ops; |
| 1087 | } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) { |
| 1088 | dev->ops = &cpu_bus_ops; |
| 1089 | } |
| 1090 | } |
| 1091 | |
Kyösti Mälkki | e4c17ce | 2014-10-21 18:22:32 +0300 | [diff] [blame] | 1092 | struct chip_operations northbridge_amd_pi_00730F01_root_complex_ops = { |
Bruce Griffith | 27ed80b | 2014-08-15 11:46:25 -0600 | [diff] [blame] | 1093 | CHIP_NAME("AMD FAM16 Root Complex") |
| 1094 | .enable_dev = root_complex_enable_dev, |
| 1095 | }; |
| 1096 | |
| 1097 | /********************************************************************* |
| 1098 | * Change the vendor / device IDs to match the generic VBIOS header. * |
| 1099 | *********************************************************************/ |
| 1100 | u32 map_oprom_vendev(u32 vendev) |
| 1101 | { |
| 1102 | u32 new_vendev; |
| 1103 | new_vendev = |
| 1104 | ((0x10029850 <= vendev) && (vendev <= 0x1002986F)) ? 0x10029850 : vendev; |
| 1105 | |
| 1106 | if (vendev != new_vendev) |
| 1107 | printk(BIOS_NOTICE, "Mapping PCI device %8x to %8x\n", vendev, new_vendev); |
| 1108 | |
| 1109 | return new_vendev; |
| 1110 | } |