blob: 0544c17a66b507ff8e38f5429a8a3e789757ccdf [file] [log] [blame]
Alexandru Gagniuc23211b02013-06-09 16:06:07 -05001/*
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 Gagniuc23211b02013-06-09 16:06:07 -050015 */
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>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020023#include <device/pci_ops.h>
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050024#include <device/pci_ids.h>
25#include <cpu/cpu.h>
Nico Huber68dd00d2018-10-06 18:01:30 +020026#include <cf9_reset.h>
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050027#include <reset.h>
28#include <string.h>
29
30#define RAM_4GB (((u64)1) << 32)
31
Kyösti Mälkki5cf88242013-10-18 11:02:56 +030032static uint64_t uma_memory_base = 0;
33static uint64_t uma_memory_size = 0;
34
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050035/**
Martin Roth543888d2015-01-06 10:20:42 -070036 * @file vx900/northbridge.c
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050037 *
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
Nico Huber68dd00d2018-10-06 18:01:30 +020046void do_board_reset(void)
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050047{
Nico Huber68dd00d2018-10-06 18:01:30 +020048 system_reset();
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050049}
50
Kyösti Mälkki5cf88242013-10-18 11:02:56 +030051uint64_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
Elyes HAOUAS2a5f6cb2018-05-25 08:09:53 +020060static u64 vx900_get_top_of_ram(struct device *mcu)
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050061{
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 */
77static void killme_debug_4g_remap_reg(u32 reg32)
78{
79 if (reg32 & (1 << 0))
Stefan Reinauer65b72ab2015-01-05 12:59:54 -080080 printk(BIOS_DEBUG, "Mem remapping enabled\n");
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050081 u64 remapstart = (reg32 >> 2) & 0x3ff;
82 u64 remapend = (reg32 >> 14) & 0x3ff;
83 remapstart <<= 26;
84 remapend <<= 26;
Elyes HAOUAS38424982016-08-21 12:01:04 +020085 printk(BIOS_DEBUG, "Remapstart %lld(MB)\n", remapstart >> 20);
86 printk(BIOS_DEBUG, "Remapend %lld(MB)\n", remapend >> 20);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050087}
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 */
Elyes HAOUAS2a5f6cb2018-05-25 08:09:53 +020097static u64 vx900_remap_above_4g(struct device *mcu, u32 tolm)
Alexandru Gagniuc23211b02013-06-09 16:06:07 -050098{
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 Reinauer65b72ab2015-01-05 12:59:54 -0800122 printk(BIOS_DEBUG, "Nothing to remap\n");
Lubomir Rintelb2fa1b22017-11-01 07:40:31 +0100123 return 0;
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500124 }
125
126 /* This is how the Vendor BIOS. Keep it for comparison for now */
127 killme_debug_4g_remap_reg(0x00180141);
128 /* We can remap with a granularity of 64MB, so align tolm */
129 tolm &= ~((64 * MiB) - 1);
130
131 /* The "start remapping from where ?" register */
132 reg16 = ((tolm >> 20) & 0xfff) << 4;
Kyösti Mälkkic0434082019-02-07 16:18:20 +0200133 pci_update_config16(mcu, 0x84, (u16)~0xfff0, reg16);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500134
135 /* Find the chunk size */
136 tor = vx900_get_top_of_ram(mcu);
137 printk(BIOS_DEBUG, "Top of RAM %lldMB\n", tor >> 20);
138
139 if (tor < RAM_4GB) {
140 chunk = tor - tolm;
141 newtor = RAM_4GB + chunk;
142 } else {
143 chunk = (RAM_4GB - tolm);
144 newtor = tor + chunk;
145 }
146 printk(BIOS_DEBUG, "New top of RAM %lldMB\n", newtor >> 20);
147
148 reg8 = tolm >> 26;
149 /* Which rank does the PCI TOLM fall on? */
150 for (i = 0; i < VX900_MAX_MEM_RANKS; i++) {
151 end8 = pci_read_config8(mcu, 0x40 + i);
152 if (reg8 > end8)
153 continue;
154 start8 = pci_read_config8(mcu, 0x48 + i);
155 if (reg8 <= start8)
156 continue;
Patrick Georgi6f7e4b22014-05-19 09:18:11 +0200157 printk(BIOS_DEBUG, "Address %x falls on rank %zu\n", tolm, i);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500158 break;
159 }
160
161 for (; i < VX900_MAX_MEM_RANKS; i++) {
162 start = pci_read_config8(mcu, 0x48 + i);
163 end = pci_read_config8(mcu, 0x40 + i);
164
165 if (end == 0) {
Patrick Georgi6f7e4b22014-05-19 09:18:11 +0200166 printk(BIOS_DEBUG, "Huh? rank %zu empty?\n", i);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500167 continue;
168 }
169
170 if (end < (tolm >> 26)) {
Patrick Georgi6f7e4b22014-05-19 09:18:11 +0200171 printk(BIOS_DEBUG, "Huh? rank %zu don't need remap?\n",
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500172 i);
173 continue;
174 }
175
176 printk(BIOS_DEBUG, "Physical rank %u is mapped to\n"
177 " Start address: 0x%.10llx (%dMB)\n"
178 " End address: 0x%.10llx (%dMB)\n",
179 (int)i,
180 ((u64) start << 26), (start << (26 - 20)),
181 ((u64) end << 26), (end << (26 - 20)));
182
183 if (end < (RAM_4GB >> 26))
184 end = (RAM_4GB >> 26);
185
186 if (end >= (tolm >> 26))
187 end += chunk >> 26;
188
189 if (start > (tolm >> 26))
190 start += chunk >> 26;
191
192 pci_write_config8(mcu, 0x48 + i, start);
193 pci_write_config8(mcu, 0x40 + i, end);
194
195 printk(BIOS_DEBUG, "ReMapped Physical rank %u, to\n"
196 " Start address: 0x%.10llx (%dMB)\n"
197 " End address: 0x%.10llx (%dMB)\n",
198 (int)i,
199 ((u64) start << 26), (start << (26 - 20)),
200 ((u64) end << 26), (end << (26 - 20)));
201 }
202
203 /* The "remap to where?" register */
Alexandru Gagniuc560433b2013-06-10 15:47:25 -0500204 reg32 = ((MAX(tor, RAM_4GB) >> 26) & 0x3ff) << 2;
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500205 /* The "remap until where?" register */
206 reg32 |= ((newtor >> 26) & 0x3ff) << 14;
207 /* Now enable the goodies */
208 reg32 |= (1 << 0);
209 pci_write_config32(mcu, 0xf8, reg32);
210 printk(BIOS_DEBUG, "Wrote remap map %x\n", reg32);
211 killme_debug_4g_remap_reg(reg32);
212
213 printk(BIOS_DEBUG, "New top of memory is at %lldMB\n", newtor >> 20);
214 return newtor;
215}
216
Elyes HAOUAS2a5f6cb2018-05-25 08:09:53 +0200217static void vx900_set_resources(struct device *dev)
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500218{
219 u32 pci_tolm, tomk, vx900_tolm, full_tolmk, fbufk, tolmk;
220
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800221 printk(BIOS_DEBUG, "========================================"
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500222 "========================================\n");
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800223 printk(BIOS_DEBUG, "============= VX900 memory sizing & Co. "
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500224 "========================================\n");
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800225 printk(BIOS_DEBUG, "========================================"
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500226 "========================================\n");
227
228 int idx = 10;
Elyes HAOUAS2a5f6cb2018-05-25 08:09:53 +0200229 struct device *const mcu = dev_find_device(PCI_VENDOR_ID_VIA,
230 PCI_DEVICE_ID_VIA_VX900_MEMCTRL,
231 0);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500232 if (!mcu) {
233 die("Something is terribly wrong.\n"
234 " We tried locating the MCU on the PCI bus, "
235 "but couldn't find it. Halting.\n");
236 }
237
238 /* How much low adrress space do we have? */
239 pci_tolm = find_pci_tolm(dev->link_list);
240 printk(BIOS_SPEW, "Found PCI tolm at %.8x\n", pci_tolm);
241 printk(BIOS_SPEW, "Found PCI tolm at %dMB\n", pci_tolm >> 20);
242
243 /* Figure out the total amount of RAM */
244 tomk = vx900_get_top_of_ram(mcu) >> 10;
245 printk(BIOS_SPEW, "Found top of memory at %dMB\n", tomk >> 10);
246
247 /* Do the same for top of low RAM */
Lubomir Rinteld8ec9732017-12-28 01:48:09 +0100248 vx900_tolm = vx900_get_tolm();
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500249 full_tolmk = vx900_tolm << (20 - 10);
250 /* Remap above 4G if needed */
Alexandru Gagniuc560433b2013-06-10 15:47:25 -0500251 full_tolmk = MIN(full_tolmk, pci_tolm >> 10);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500252 printk(BIOS_SPEW, "Found top of low memory at %dMB\n",
253 full_tolmk >> 10);
254
255 /* What about the framebuffer for the integrated GPU? */
Lubomir Rinteld8ec9732017-12-28 01:48:09 +0100256 fbufk = vx900_get_chrome9hd_fb_size() << (20 - 10);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500257 printk(BIOS_SPEW, "Integrated graphics buffer: %dMB\n", fbufk >> 10);
258
259 /* Can't use the framebuffer as system RAM, sorry */
Alexandru Gagniuc560433b2013-06-10 15:47:25 -0500260 tolmk = MIN(full_tolmk, tomk);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500261 tolmk -= fbufk;
262 ram_resource(dev, idx++, 0, 640);
Elyes HAOUAS15279a92016-07-28 21:05:26 +0200263 printk(BIOS_SPEW, "System RAM left: %dMB\n", tolmk >> 10);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500264 /* FIXME: how can we avoid leaving this hole?
265 * Leave a hole for VGA, 0xa0000 - 0xc0000 ?? */
266 /* TODO: VGA Memory hole can be disabled in SNMIC. Upper 64k of ROM seem
267 * to be always mapped to the top of 1M, but this can be overcome with
268 * some smart positive/subtractive resource decoding */
269 ram_resource(dev, idx++, 768, (tolmk - 768));
270 uma_memory_size = fbufk << 10;
271 uma_memory_base = tolmk << 10;
272
Kyösti Mälkki5cf88242013-10-18 11:02:56 +0300273 //uma_resource(dev, idx++, uma_memory_base>>10, uma_memory_size>>10);
274
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500275 printk(BIOS_DEBUG, "UMA @ %lldMB + %lldMB\n", uma_memory_base >> 20,
276 uma_memory_size >> 20);
277 /* FIXME: How do we handle remapping above 4G? */
278 u64 tor = vx900_remap_above_4g(mcu, pci_tolm);
Lubomir Rintelb2fa1b22017-11-01 07:40:31 +0100279 if (tor)
280 ram_resource(dev, idx++, RAM_4GB >> 10, (tor - RAM_4GB) >> 10);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500281
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800282 printk(BIOS_DEBUG, "======================================================\n");
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500283 assign_resources(dev->link_list);
284}
285
Elyes HAOUAS2a5f6cb2018-05-25 08:09:53 +0200286static void vx900_read_resources(struct device *dev)
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500287{
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 */
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200300 mmconf_resource(dev, idx++);
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500301
302 pci_domain_read_resources(dev);
303}
304
305static struct device_operations pci_domain_ops = {
306 .read_resources = vx900_read_resources,
307 .set_resources = vx900_set_resources,
308 .enable_resources = NULL,
309 .init = NULL,
310 .scan_bus = pci_domain_scan_bus,
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500311};
312
Elyes HAOUAS2a5f6cb2018-05-25 08:09:53 +0200313static void cpu_bus_init(struct device *dev)
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500314{
315 initialize_cpus(dev->link_list);
316}
317
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500318static struct device_operations cpu_bus_ops = {
Edward O'Callaghand2040732014-10-31 08:26:21 +1100319 .read_resources = DEVICE_NOOP,
320 .set_resources = DEVICE_NOOP,
321 .enable_resources = DEVICE_NOOP,
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500322 .init = cpu_bus_init,
323 .scan_bus = 0,
324};
325
Elyes HAOUAS2a5f6cb2018-05-25 08:09:53 +0200326static void enable_dev(struct device *dev)
Alexandru Gagniuc23211b02013-06-09 16:06:07 -0500327{
328 /* Set the operations if it is a special bus type */
329 if (dev->path.type == DEVICE_PATH_DOMAIN) {
330 dev->ops = &pci_domain_ops;
331 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
332 dev->ops = &cpu_bus_ops;
333 }
334}
335
336struct chip_operations northbridge_via_vx900_ops = {
337 CHIP_NAME("VIA VX900 Chipset")
338 .enable_dev = enable_dev,
339};