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