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