blob: abce0abfba709c97565d188f68414ca09448eab8 [file] [log] [blame]
Mariusz Szafranskia4041332017-08-02 17:28:17 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2014 Google Inc.
6 * Copyright (C) 2015 - 2017 Intel Corporation.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <console/console.h>
19#include <arch/acpi.h>
20#include <arch/io.h>
21#include <stdint.h>
22#include <delay.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
26#include <stdlib.h>
27#include <string.h>
Mariusz Szafranskia4041332017-08-02 17:28:17 +020028#include <romstage_handoff.h>
Mariusz Szafranskia4041332017-08-02 17:28:17 +020029#include <timer.h>
30
31#include <soc/iomap.h>
32#include <soc/pci_devs.h>
33#include <soc/ramstage.h>
34#include <soc/systemagent.h>
35
36#define _1ms 1
37#define WAITING_STEP 100
38
Elyes HAOUAS2ec41832018-05-27 17:40:58 +020039static int get_pcie_bar(struct device *dev, unsigned int index, u32 *base,
40 u32 *len)
Mariusz Szafranskia4041332017-08-02 17:28:17 +020041{
42 u32 pciexbar_reg;
43
44 *base = 0;
45 *len = 0;
46
47 pciexbar_reg = pci_read_config32(dev, index);
48
49 if (!(pciexbar_reg & (1 << 0)))
50 return 0;
51
52 switch ((pciexbar_reg >> 1) & 3) {
53 case 0: /* 256MB */
54 *base = pciexbar_reg &
55 ((1 << 31) | (1 << 30) | (1 << 29) | (1 << 28));
56 *len = 256 * 1024 * 1024;
57 return 1;
58 case 1: /* 128M */
59 *base = pciexbar_reg & ((1 << 31) | (1 << 30) | (1 << 29) |
60 (1 << 28) | (1 << 27));
61 *len = 128 * 1024 * 1024;
62 return 1;
63 case 2: /* 64M */
64 *base = pciexbar_reg & ((1 << 31) | (1 << 30) | (1 << 29) |
65 (1 << 28) | (1 << 27) | (1 << 26));
66 *len = 64 * 1024 * 1024;
67 return 1;
68 }
69
70 return 0;
71}
72
Elyes HAOUAS2ec41832018-05-27 17:40:58 +020073static int get_bar(struct device *dev, unsigned int index, u32 *base, u32 *len)
Mariusz Szafranskia4041332017-08-02 17:28:17 +020074{
75 u32 bar;
76
77 bar = pci_read_config32(dev, index);
78
79 /* If not enabled don't report it. */
80 if (!(bar & 0x1))
81 return 0;
82
83 /* Knock down the enable bit. */
84 *base = bar & ~1;
85
86 return 1;
87}
88
89struct fixed_mmio_descriptor {
90 unsigned int index;
91 u32 size;
Elyes HAOUAS2ec41832018-05-27 17:40:58 +020092 int (*get_resource)(struct device *dev, unsigned int index, u32 *base,
Mariusz Szafranskia4041332017-08-02 17:28:17 +020093 u32 *size);
94 const char *description;
95};
96
97struct fixed_mmio_descriptor mc_fixed_resources[] = {
98 {PCIEXBAR, 0, get_pcie_bar, "PCIEXBAR"},
99 {MCHBAR, MCH_BASE_SIZE, get_bar, "MCHBAR"},
100};
101
102/*
103 * Add all known fixed MMIO ranges that hang off the host bridge/memory
104 * controller device.
105 */
Elyes HAOUAS2ec41832018-05-27 17:40:58 +0200106static void mc_add_fixed_mmio_resources(struct device *dev)
Mariusz Szafranskia4041332017-08-02 17:28:17 +0200107{
108 int i;
109
110 for (i = 0; i < ARRAY_SIZE(mc_fixed_resources); i++) {
111 u32 base;
112 u32 size;
113 struct resource *resource;
114 unsigned int index;
115
116 size = mc_fixed_resources[i].size;
117 index = mc_fixed_resources[i].index;
118 if (!mc_fixed_resources[i].get_resource(dev, index, &base,
119 &size))
120 continue;
121
122 resource = new_resource(dev, mc_fixed_resources[i].index);
123 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
124 IORESOURCE_STORED | IORESOURCE_RESERVE |
125 IORESOURCE_ASSIGNED;
126 resource->base = base;
127 resource->size = size;
128 printk(BIOS_DEBUG, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
129 __func__, mc_fixed_resources[i].description, index,
130 (unsigned long)base, (unsigned long)(base + size - 1));
131 }
132}
133
134struct map_entry {
135 int reg;
136 int is_64_bit;
137 int is_limit;
138 const char *description;
139};
140
Elyes HAOUAS2ec41832018-05-27 17:40:58 +0200141static void read_map_entry(struct device *dev, struct map_entry *entry,
Mariusz Szafranskia4041332017-08-02 17:28:17 +0200142 uint64_t *result)
143{
144 uint64_t value;
145 uint64_t mask;
146
147 /* All registers are on a 1MiB granularity. */
148 mask = ((1ULL << 20) - 1);
149 mask = ~mask;
150
151 value = 0;
152
153 if (entry->is_64_bit) {
154 value = pci_read_config32(dev, entry->reg + 4);
155 value <<= 32;
156 }
157
158 value |= (uint64_t)pci_read_config32(dev, entry->reg);
159 value &= mask;
160
161 if (entry->is_limit)
162 value |= ~mask;
163
164 *result = value;
165}
166
167#define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
168 { \
169 .reg = reg_, .is_64_bit = is_64_, .is_limit = is_limit_, \
170 .description = desc_, \
171 }
172
173#define MAP_ENTRY_BASE_64(reg_, desc_) MAP_ENTRY(reg_, 1, 0, desc_)
174#define MAP_ENTRY_LIMIT_64(reg_, desc_) MAP_ENTRY(reg_, 1, 1, desc_)
175#define MAP_ENTRY_BASE_32(reg_, desc_) MAP_ENTRY(reg_, 0, 0, desc_)
176
177enum {
178 TOUUD_REG,
179 TOLUD_REG,
180 TSEG_REG,
181 /* Must be last. */
182 NUM_MAP_ENTRIES
183};
184
185static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
186 [TOUUD_REG] = MAP_ENTRY_BASE_64(TOUUD, "TOUUD"),
187 [TOLUD_REG] = MAP_ENTRY_BASE_32(TOLUD, "TOLUD"),
188 [TSEG_REG] = MAP_ENTRY_BASE_32(TSEGMB, "TSEGMB"),
189};
190
Elyes HAOUAS2ec41832018-05-27 17:40:58 +0200191static void mc_read_map_entries(struct device *dev, uint64_t *values)
Mariusz Szafranskia4041332017-08-02 17:28:17 +0200192{
193 int i;
194 for (i = 0; i < NUM_MAP_ENTRIES; i++)
195 read_map_entry(dev, &memory_map[i], &values[i]);
196}
197
Elyes HAOUAS2ec41832018-05-27 17:40:58 +0200198static void mc_report_map_entries(struct device *dev, uint64_t *values)
Mariusz Szafranskia4041332017-08-02 17:28:17 +0200199{
200 int i;
201 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
202 printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
203 memory_map[i].description, values[i]);
204 }
205}
206
Elyes HAOUAS2ec41832018-05-27 17:40:58 +0200207static void mc_add_dram_resources(struct device *dev)
Mariusz Szafranskia4041332017-08-02 17:28:17 +0200208{
209 unsigned long base_k, size_k;
210 unsigned long touud_k;
211 unsigned long index;
212 struct resource *resource;
213 uint64_t mc_values[NUM_MAP_ENTRIES];
214
215 /* Read in the MAP registers and report their values. */
216 mc_read_map_entries(dev, &mc_values[0]);
217 mc_report_map_entries(dev, &mc_values[0]);
218
219 /*
220 * These are the host memory ranges that should be added:
221 * - 0 -> 0xa0000: cacheable
222 * - 0xc0000 -> 0x100000 : reserved
223 * - 0x100000 -> top_of_ram : cacheable
224 * - top_of_ram -> TSEG: uncacheable
225 * - TESG -> TOLUD: cacheable with standard MTRRs and reserved
226 * - 4GiB -> TOUUD: cacheable
227 *
228 * The default SMRAM space is reserved so that the range doesn't
229 * have to be saved during S3 Resume. Once marked reserved the OS
230 * cannot use the memory. This is a bit of an odd place to reserve
231 * the region, but the CPU devices don't have dev_ops->read_resources()
232 * called on them.
233 *
234 * The range 0xa0000 -> 0xc0000 does not have any resources
235 * associated with it to handle legacy VGA memory. If this range
236 * is not omitted the mtrr code will setup the area as cacheable
237 * causing VGA access to not work.
238 *
239 * The TSEG region is mapped as cacheable so that one can perform
240 * SMRAM relocation faster. Once the SMRR is enabled the SMRR takes
241 * precedence over the existing MTRRs covering this region.
242 *
243 * It should be noted that cacheable entry types need to be added in
244 * order. The reason is that the current MTRR code assumes this and
245 * falls over itself if it isn't.
246 *
247 * The resource index starts low and should not meet or exceed
248 * PCI_BASE_ADDRESS_0.
249 */
250 index = 0;
251
252 /* 0 - > 0xa0000 */
253 base_k = 0;
254 size_k = (0xa0000 >> 10) - base_k;
255 ram_resource(dev, index++, base_k, size_k);
256
257 /* 0x100000 -> top_of_ram */
258 base_k = 0x100000 >> 10;
259 size_k = (top_of_32bit_ram() >> 10) - base_k;
260 ram_resource(dev, index++, base_k, size_k);
261
262 /* top_of_ram -> TSEG */
263 resource = new_resource(dev, index++);
264 resource->base = top_of_32bit_ram();
265 resource->size = mc_values[TSEG_REG] - resource->base;
266 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
267 IORESOURCE_STORED | IORESOURCE_RESERVE |
268 IORESOURCE_ASSIGNED;
269
270 /* TSEG -> TOLUD */
271 resource = new_resource(dev, index++);
272 resource->base = mc_values[TSEG_REG];
273 resource->size = mc_values[TOLUD_REG] - resource->base;
274 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
275 IORESOURCE_STORED | IORESOURCE_RESERVE |
276 IORESOURCE_ASSIGNED | IORESOURCE_CACHEABLE;
277 printk(BIOS_DEBUG,
278 "SMM memory location: 0x%llx SMM memory size: 0x%llx\n",
279 resource->base, resource->size);
280
281 /* 4GiB -> TOUUD */
282 base_k = 4096 * 1024; /* 4GiB */
283 touud_k = mc_values[TOUUD_REG] >> 10;
284 size_k = touud_k - base_k;
285 if (touud_k > base_k)
286 ram_resource(dev, index++, base_k, size_k);
287
288 /*
289 * Reserve everything between A segment and 1MB:
290 *
291 * 0xa0000 - 0xbffff: legacy VGA
292 * 0xc0000 - 0xfffff: reserved RAM
293 */
294 mmio_resource(dev, index++, (0xa0000 >> 10), (0xc0000 - 0xa0000) >> 10);
295 reserved_ram_resource(dev, index++, (0xc0000 >> 10),
296 (0x100000 - 0xc0000) >> 10);
297}
298
Elyes HAOUAS2ec41832018-05-27 17:40:58 +0200299static void systemagent_read_resources(struct device *dev)
Mariusz Szafranskia4041332017-08-02 17:28:17 +0200300{
301 /* Read standard PCI resources. */
302 pci_dev_read_resources(dev);
303
304 /* Add all fixed MMIO resources. */
305 mc_add_fixed_mmio_resources(dev);
306
307 /* Calculate and add DRAM resources. */
308 mc_add_dram_resources(dev);
309}
310
311static void systemagent_init(struct device *dev)
312{
313 struct stopwatch sw;
314 void *bios_reset_cpl =
315 (void *)(DEFAULT_MCHBAR + MCH_BAR_BIOS_RESET_CPL);
316 uint32_t reg = read32(bios_reset_cpl);
317
318 /* Stage0 BIOS Reset Complete (RST_CPL) */
319 reg |= RST_CPL_BIT;
320 write32(bios_reset_cpl, reg);
321
322 /*
323 * Poll for bit 8 in same reg (RST_CPL).
324 * We wait here till 1 ms for the bit to get set.
325 */
326 stopwatch_init_msecs_expire(&sw, _1ms);
327 while (!(read32(bios_reset_cpl) & PCODE_INIT_DONE)) {
328 if (stopwatch_expired(&sw)) {
329 printk(BIOS_DEBUG, "Failed to set RST_CPL bit\n");
330 return;
331 }
332 udelay(WAITING_STEP);
333 }
334 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
335}
336
337static struct device_operations systemagent_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100338 .read_resources = systemagent_read_resources,
339 .set_resources = pci_dev_set_resources,
340 .enable_resources = pci_dev_enable_resources,
341 .init = systemagent_init,
Mariusz Szafranskia4041332017-08-02 17:28:17 +0200342 .ops_pci = &soc_pci_ops,
343};
344
345/* IDs for System Agent device of Intel Denverton SoC */
346static const unsigned short systemagent_ids[] = {
347 SA_DEVID, /* DVN System Agent */
Lew, Chee Soon0ade8f52017-11-07 10:47:11 +0800348 SA_DEVID_DNVAD, /* DVN-AD System Agent */
Mariusz Szafranskia4041332017-08-02 17:28:17 +0200349 0
350};
351
352static const struct pci_driver systemagent_driver __pci_driver = {
353 .ops = &systemagent_ops,
354 .vendor = PCI_VENDOR_ID_INTEL,
355 .devices = systemagent_ids
356};