Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> |
| 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, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "vx900.h" |
| 18 | #include "chip.h" |
| 19 | |
| 20 | #include <console/console.h> |
| 21 | #include <device/device.h> |
| 22 | #include <device/pci.h> |
| 23 | #include <device/pci_ids.h> |
| 24 | #include <cpu/cpu.h> |
| 25 | #include <cbmem.h> |
| 26 | #include <lib.h> |
| 27 | #include <reset.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #define RAM_4GB (((u64)1) << 32) |
| 31 | |
Kyösti Mälkki | 5cf8824 | 2013-10-18 11:02:56 +0300 | [diff] [blame] | 32 | static uint64_t uma_memory_base = 0; |
| 33 | static uint64_t uma_memory_size = 0; |
| 34 | |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 35 | /** |
Martin Roth | 543888d | 2015-01-06 10:20:42 -0700 | [diff] [blame] | 36 | * @file vx900/northbridge.c |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 37 | * |
| 38 | * STATUS: Pretty good |
| 39 | * One thing that needs to be thoroughly tested is the remap above 4G logic. |
| 40 | * Unfortunately, while we cannot initialize odd ranks, our testing |
| 41 | * possibilities are somewhat limited. A point of failure that is not covered is |
| 42 | * when the amount of RAM and PCI config space added up exceeds 8GB. The |
| 43 | * remapping mechanism will overflow, the effects of which are unknown. |
| 44 | */ |
| 45 | |
| 46 | void hard_reset(void) |
| 47 | { |
| 48 | outb((1 << 2) | (1 << 1), 0xcf9); |
| 49 | } |
| 50 | |
Kyösti Mälkki | 5cf8824 | 2013-10-18 11:02:56 +0300 | [diff] [blame] | 51 | uint64_t get_uma_memory_base(void) |
| 52 | { |
| 53 | printk(BIOS_DEBUG, "UMA base 0x%.8llx (%lluMB)\n", uma_memory_base, |
| 54 | uma_memory_base >> 20); |
| 55 | printk(BIOS_DEBUG, "UMA size 0x%.8llx (%lluMB)\n", uma_memory_size, |
| 56 | uma_memory_size >> 20); |
| 57 | return uma_memory_base; |
| 58 | } |
| 59 | |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 60 | static u64 vx900_get_top_of_ram(device_t mcu) |
| 61 | { |
| 62 | u16 reg16; |
| 63 | /* The last valid DRAM address is computed by the MCU |
| 64 | * One issue might be if we have a hole in the rank mappings, so that |
| 65 | * virtual ranks are not mapped successively in the linear address space |
| 66 | * (Ex: rank 0 mapped 0-1G, rank 1 mapped 2G-3G) |
| 67 | * We don't do this awkward mapping in RAM init, so we don't worry about |
| 68 | * it here, but it is something to keep in mind if having RAM issues */ |
| 69 | reg16 = pci_read_config16(mcu, 0x88) & 0x07ff; |
| 70 | return (u64) reg16 << 24; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * This guy is meant to go away, but for now, leave it in so that we can see |
| 75 | * if the logic to remap RAM above 4G has errors. |
| 76 | */ |
| 77 | static void killme_debug_4g_remap_reg(u32 reg32) |
| 78 | { |
| 79 | if (reg32 & (1 << 0)) |
Stefan Reinauer | 65b72ab | 2015-01-05 12:59:54 -0800 | [diff] [blame] | 80 | printk(BIOS_DEBUG, "Mem remapping enabled\n"); |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 81 | u64 remapstart = (reg32 >> 2) & 0x3ff; |
| 82 | u64 remapend = (reg32 >> 14) & 0x3ff; |
| 83 | remapstart <<= 26; |
| 84 | remapend <<= 26; |
| 85 | printk(BIOS_DEBUG, "Remapstart %lld(MB) \n", remapstart >> 20); |
| 86 | printk(BIOS_DEBUG, "Remapend %lld(MB) \n", remapend >> 20); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * \brief Remap low memory colliding with PCI MMIO space, above 4G |
| 91 | * |
| 92 | * @param mcu The memory controller |
| 93 | * @param tolm Top of low memory. |
| 94 | * |
| 95 | * @return The new top of memory. |
| 96 | */ |
| 97 | static u64 vx900_remap_above_4g(device_t mcu, u32 tolm) |
| 98 | { |
| 99 | size_t i; |
| 100 | u8 reg8, start8, end8, start, end; |
| 101 | u16 reg16; |
| 102 | u32 reg32; |
| 103 | u64 tor, newtor, chunk; |
| 104 | |
| 105 | /* |
| 106 | * The remapping mechanism works like this: |
| 107 | * |
| 108 | * - Choose the top of low memory. |
| 109 | * This becomes the "remap from" |
| 110 | * - Choose a chunk above 4G where to remap. |
| 111 | * This becomes "remap to" |
| 112 | * - Choose a chunk above 4G where to end the remapping. |
| 113 | * This becomes "remap until" |
| 114 | * |
| 115 | * This remaps a "chunk" of memory where we want to. |
| 116 | * sizeof(chunk) = until - to; |
| 117 | * |
| 118 | * Therefore the memory region from "from" to " from + sizeof(chunk)" |
| 119 | * becomes accessible at "to" to "until" |
| 120 | */ |
| 121 | if (tolm >= vx900_get_top_of_ram(mcu)) { |
Stefan Reinauer | 65b72ab | 2015-01-05 12:59:54 -0800 | [diff] [blame] | 122 | printk(BIOS_DEBUG, "Nothing to remap\n"); |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* This is how the Vendor BIOS. Keep it for comparison for now */ |
| 126 | killme_debug_4g_remap_reg(0x00180141); |
| 127 | /* We can remap with a granularity of 64MB, so align tolm */ |
| 128 | tolm &= ~((64 * MiB) - 1); |
| 129 | |
| 130 | /* The "start remapping from where ?" register */ |
| 131 | reg16 = ((tolm >> 20) & 0xfff) << 4; |
| 132 | pci_mod_config16(mcu, 0x84, 0xfff0, reg16); |
| 133 | |
| 134 | /* Find the chunk size */ |
| 135 | tor = vx900_get_top_of_ram(mcu); |
| 136 | printk(BIOS_DEBUG, "Top of RAM %lldMB\n", tor >> 20); |
| 137 | |
| 138 | if (tor < RAM_4GB) { |
| 139 | chunk = tor - tolm; |
| 140 | newtor = RAM_4GB + chunk; |
| 141 | } else { |
| 142 | chunk = (RAM_4GB - tolm); |
| 143 | newtor = tor + chunk; |
| 144 | } |
| 145 | printk(BIOS_DEBUG, "New top of RAM %lldMB\n", newtor >> 20); |
| 146 | |
| 147 | reg8 = tolm >> 26; |
| 148 | /* Which rank does the PCI TOLM fall on? */ |
| 149 | for (i = 0; i < VX900_MAX_MEM_RANKS; i++) { |
| 150 | end8 = pci_read_config8(mcu, 0x40 + i); |
| 151 | if (reg8 > end8) |
| 152 | continue; |
| 153 | start8 = pci_read_config8(mcu, 0x48 + i); |
| 154 | if (reg8 <= start8) |
| 155 | continue; |
Patrick Georgi | 6f7e4b2 | 2014-05-19 09:18:11 +0200 | [diff] [blame] | 156 | printk(BIOS_DEBUG, "Address %x falls on rank %zu\n", tolm, i); |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | |
| 160 | for (; i < VX900_MAX_MEM_RANKS; i++) { |
| 161 | start = pci_read_config8(mcu, 0x48 + i); |
| 162 | end = pci_read_config8(mcu, 0x40 + i); |
| 163 | |
| 164 | if (end == 0) { |
Patrick Georgi | 6f7e4b2 | 2014-05-19 09:18:11 +0200 | [diff] [blame] | 165 | printk(BIOS_DEBUG, "Huh? rank %zu empty?\n", i); |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 166 | continue; |
| 167 | } |
| 168 | |
| 169 | if (end < (tolm >> 26)) { |
Patrick Georgi | 6f7e4b2 | 2014-05-19 09:18:11 +0200 | [diff] [blame] | 170 | printk(BIOS_DEBUG, "Huh? rank %zu don't need remap?\n", |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 171 | i); |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | printk(BIOS_DEBUG, "Physical rank %u is mapped to\n" |
| 176 | " Start address: 0x%.10llx (%dMB)\n" |
| 177 | " End address: 0x%.10llx (%dMB)\n", |
| 178 | (int)i, |
| 179 | ((u64) start << 26), (start << (26 - 20)), |
| 180 | ((u64) end << 26), (end << (26 - 20))); |
| 181 | |
| 182 | if (end < (RAM_4GB >> 26)) |
| 183 | end = (RAM_4GB >> 26); |
| 184 | |
| 185 | if (end >= (tolm >> 26)) |
| 186 | end += chunk >> 26; |
| 187 | |
| 188 | if (start > (tolm >> 26)) |
| 189 | start += chunk >> 26; |
| 190 | |
| 191 | pci_write_config8(mcu, 0x48 + i, start); |
| 192 | pci_write_config8(mcu, 0x40 + i, end); |
| 193 | |
| 194 | printk(BIOS_DEBUG, "ReMapped Physical rank %u, to\n" |
| 195 | " Start address: 0x%.10llx (%dMB)\n" |
| 196 | " End address: 0x%.10llx (%dMB)\n", |
| 197 | (int)i, |
| 198 | ((u64) start << 26), (start << (26 - 20)), |
| 199 | ((u64) end << 26), (end << (26 - 20))); |
| 200 | } |
| 201 | |
| 202 | /* The "remap to where?" register */ |
Alexandru Gagniuc | 560433b | 2013-06-10 15:47:25 -0500 | [diff] [blame] | 203 | reg32 = ((MAX(tor, RAM_4GB) >> 26) & 0x3ff) << 2; |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 204 | /* The "remap until where?" register */ |
| 205 | reg32 |= ((newtor >> 26) & 0x3ff) << 14; |
| 206 | /* Now enable the goodies */ |
| 207 | reg32 |= (1 << 0); |
| 208 | pci_write_config32(mcu, 0xf8, reg32); |
| 209 | printk(BIOS_DEBUG, "Wrote remap map %x\n", reg32); |
| 210 | killme_debug_4g_remap_reg(reg32); |
| 211 | |
| 212 | printk(BIOS_DEBUG, "New top of memory is at %lldMB\n", newtor >> 20); |
| 213 | return newtor; |
| 214 | } |
| 215 | |
| 216 | static void vx900_set_resources(device_t dev) |
| 217 | { |
| 218 | u32 pci_tolm, tomk, vx900_tolm, full_tolmk, fbufk, tolmk; |
| 219 | |
Stefan Reinauer | 65b72ab | 2015-01-05 12:59:54 -0800 | [diff] [blame] | 220 | printk(BIOS_DEBUG, "========================================" |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 221 | "========================================\n"); |
Stefan Reinauer | 65b72ab | 2015-01-05 12:59:54 -0800 | [diff] [blame] | 222 | printk(BIOS_DEBUG, "============= VX900 memory sizing & Co. " |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 223 | "========================================\n"); |
Stefan Reinauer | 65b72ab | 2015-01-05 12:59:54 -0800 | [diff] [blame] | 224 | printk(BIOS_DEBUG, "========================================" |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 225 | "========================================\n"); |
| 226 | |
| 227 | int idx = 10; |
| 228 | const device_t mcu = dev_find_device(PCI_VENDOR_ID_VIA, |
| 229 | PCI_DEVICE_ID_VIA_VX900_MEMCTRL, |
| 230 | 0); |
| 231 | if (!mcu) { |
| 232 | die("Something is terribly wrong.\n" |
| 233 | " We tried locating the MCU on the PCI bus, " |
| 234 | "but couldn't find it. Halting.\n"); |
| 235 | } |
| 236 | |
| 237 | /* How much low adrress space do we have? */ |
| 238 | pci_tolm = find_pci_tolm(dev->link_list); |
| 239 | printk(BIOS_SPEW, "Found PCI tolm at %.8x\n", pci_tolm); |
| 240 | printk(BIOS_SPEW, "Found PCI tolm at %dMB\n", pci_tolm >> 20); |
| 241 | |
| 242 | /* Figure out the total amount of RAM */ |
| 243 | tomk = vx900_get_top_of_ram(mcu) >> 10; |
| 244 | printk(BIOS_SPEW, "Found top of memory at %dMB\n", tomk >> 10); |
| 245 | |
| 246 | /* Do the same for top of low RAM */ |
| 247 | vx900_tolm = (pci_read_config16(mcu, 0x84) & 0xfff0) >> 4; |
| 248 | full_tolmk = vx900_tolm << (20 - 10); |
| 249 | /* Remap above 4G if needed */ |
Alexandru Gagniuc | 560433b | 2013-06-10 15:47:25 -0500 | [diff] [blame] | 250 | full_tolmk = MIN(full_tolmk, pci_tolm >> 10); |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 251 | printk(BIOS_SPEW, "Found top of low memory at %dMB\n", |
| 252 | full_tolmk >> 10); |
| 253 | |
| 254 | /* What about the framebuffer for the integrated GPU? */ |
| 255 | fbufk = chrome9hd_fb_size() >> 10; |
| 256 | printk(BIOS_SPEW, "Integrated graphics buffer: %dMB\n", fbufk >> 10); |
| 257 | |
| 258 | /* Can't use the framebuffer as system RAM, sorry */ |
Alexandru Gagniuc | 560433b | 2013-06-10 15:47:25 -0500 | [diff] [blame] | 259 | tolmk = MIN(full_tolmk, tomk); |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 260 | tolmk -= fbufk; |
| 261 | ram_resource(dev, idx++, 0, 640); |
| 262 | printk(BIOS_SPEW, "System ram left: %dMB\n", tolmk >> 10); |
| 263 | /* FIXME: how can we avoid leaving this hole? |
| 264 | * Leave a hole for VGA, 0xa0000 - 0xc0000 ?? */ |
| 265 | /* TODO: VGA Memory hole can be disabled in SNMIC. Upper 64k of ROM seem |
| 266 | * to be always mapped to the top of 1M, but this can be overcome with |
| 267 | * some smart positive/subtractive resource decoding */ |
| 268 | ram_resource(dev, idx++, 768, (tolmk - 768)); |
| 269 | uma_memory_size = fbufk << 10; |
| 270 | uma_memory_base = tolmk << 10; |
| 271 | |
Kyösti Mälkki | 5cf8824 | 2013-10-18 11:02:56 +0300 | [diff] [blame] | 272 | //uma_resource(dev, idx++, uma_memory_base>>10, uma_memory_size>>10); |
| 273 | |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 274 | printk(BIOS_DEBUG, "UMA @ %lldMB + %lldMB\n", uma_memory_base >> 20, |
| 275 | uma_memory_size >> 20); |
| 276 | /* FIXME: How do we handle remapping above 4G? */ |
| 277 | u64 tor = vx900_remap_above_4g(mcu, pci_tolm); |
| 278 | ram_resource(dev, idx++, RAM_4GB >> 10, (tor - RAM_4GB) >> 10); |
| 279 | |
Kyösti Mälkki | 42f4651 | 2013-06-27 08:20:09 +0300 | [diff] [blame] | 280 | set_top_of_ram(tolmk << 10); |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 281 | |
Stefan Reinauer | 65b72ab | 2015-01-05 12:59:54 -0800 | [diff] [blame] | 282 | printk(BIOS_DEBUG, "======================================================\n"); |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 283 | assign_resources(dev->link_list); |
| 284 | } |
| 285 | |
| 286 | static void vx900_read_resources(device_t dev) |
| 287 | { |
| 288 | /* Our fixed resources start at 0 */ |
| 289 | int idx = 0; |
| 290 | /* Reserve our ROM mapped space */ |
| 291 | struct resource *res; |
| 292 | res = new_resource(dev, idx++); |
| 293 | res->size = CONFIG_ROM_SIZE; |
| 294 | res->base = 0xffffffff - (res->size - 1); |
| 295 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 296 | |
| 297 | /* Now do the same for our MMCONF |
| 298 | * We always run with MMCONF enabled. We need to access the extended |
| 299 | * config space when configuring PCI-Express links */ |
| 300 | res = new_resource(dev, idx++); |
| 301 | res->size = 256 * MiB; |
| 302 | res->base = CONFIG_MMCONF_BASE_ADDRESS; |
| 303 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 304 | |
| 305 | pci_domain_read_resources(dev); |
| 306 | } |
| 307 | |
| 308 | static struct device_operations pci_domain_ops = { |
| 309 | .read_resources = vx900_read_resources, |
| 310 | .set_resources = vx900_set_resources, |
| 311 | .enable_resources = NULL, |
| 312 | .init = NULL, |
| 313 | .scan_bus = pci_domain_scan_bus, |
Kyösti Mälkki | 872c922 | 2013-07-03 09:44:28 +0300 | [diff] [blame] | 314 | .ops_pci_bus = pci_bus_default_ops, |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | static void cpu_bus_init(device_t dev) |
| 318 | { |
| 319 | initialize_cpus(dev->link_list); |
| 320 | } |
| 321 | |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 322 | static struct device_operations cpu_bus_ops = { |
Edward O'Callaghan | d204073 | 2014-10-31 08:26:21 +1100 | [diff] [blame] | 323 | .read_resources = DEVICE_NOOP, |
| 324 | .set_resources = DEVICE_NOOP, |
| 325 | .enable_resources = DEVICE_NOOP, |
Alexandru Gagniuc | 23211b0 | 2013-06-09 16:06:07 -0500 | [diff] [blame] | 326 | .init = cpu_bus_init, |
| 327 | .scan_bus = 0, |
| 328 | }; |
| 329 | |
| 330 | static void enable_dev(device_t dev) |
| 331 | { |
| 332 | /* Set the operations if it is a special bus type */ |
| 333 | if (dev->path.type == DEVICE_PATH_DOMAIN) { |
| 334 | dev->ops = &pci_domain_ops; |
| 335 | } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) { |
| 336 | dev->ops = &cpu_bus_ops; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | struct chip_operations northbridge_via_vx900_ops = { |
| 341 | CHIP_NAME("VIA VX900 Chipset") |
| 342 | .enable_dev = enable_dev, |
| 343 | }; |