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