blob: a549acb0f69667ec68b3060265c10675addee9d2 [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Jonathan Zhang8f895492020-01-16 11:16:45 -08002
3#include <cbmem.h>
4#include <console/console.h>
Elyes HAOUAS32da3432020-05-17 17:15:31 +02005#include <cpu/x86/lapic_def.h>
Jonathan Zhang8f895492020-01-16 11:16:45 -08006#include <device/pci.h>
7#include <device/pci_ids.h>
8#include <soc/iomap.h>
9#include <soc/pci_devs.h>
10#include <soc/ramstage.h>
Andrey Petrov662da6c2020-03-16 22:46:57 -070011#include <soc/util.h>
12#include <fsp/util.h>
Jonathan Zhang8f895492020-01-16 11:16:45 -080013
14struct map_entry {
15 uint32_t reg;
16 int is_64_bit;
17 int is_limit;
18 int mask_bits;
19 const char *description;
20};
21
22enum {
23 TOHM_REG,
24 MMIOL_REG,
25 MMCFG_BASE_REG,
26 MMCFG_LIMIT_REG,
27 TOLM_REG,
28 ME_BASE_REG,
29 ME_LIMIT_REG,
30 TSEG_BASE_REG,
31 TSEG_LIMIT_REG,
32 /* Must be last. */
33 NUM_MAP_ENTRIES
34};
35
36static struct map_entry memory_map[NUM_MAP_ENTRIES] = {
37 [TOHM_REG] = MAP_ENTRY_LIMIT_64(VTD_TOHM_CSR, 26, "TOHM"),
38 [MMIOL_REG] = MAP_ENTRY_BASE_32(VTD_MMIOL_CSR, "MMIOL"),
39 [MMCFG_BASE_REG] = MAP_ENTRY_BASE_64(VTD_MMCFG_BASE_CSR, "MMCFG_BASE"),
40 [MMCFG_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_MMCFG_LIMIT_CSR, 26, "MMCFG_LIMIT"),
41 [TOLM_REG] = MAP_ENTRY_LIMIT_32(VTD_TOLM_CSR, 26, "TOLM"),
42 [ME_BASE_REG] = MAP_ENTRY_BASE_64(VTD_ME_BASE_CSR, "ME_BASE"),
43 [ME_LIMIT_REG] = MAP_ENTRY_LIMIT_64(VTD_ME_LIMIT_CSR, 19, "ME_LIMIT"),
44 [TSEG_BASE_REG] = MAP_ENTRY_BASE_32(VTD_TSEG_BASE_CSR, "TSEGMB_BASE"),
45 [TSEG_LIMIT_REG] = MAP_ENTRY_LIMIT_32(VTD_TSEG_LIMIT_CSR, 20, "TSEGMB_LIMIT"),
46};
47
48static void read_map_entry(struct device *dev, struct map_entry *entry,
49 uint64_t *result)
50{
51 uint64_t value;
52 uint64_t mask;
53
54 /* All registers are on a 1MiB granularity. */
55 mask = ((1ULL << entry->mask_bits) - 1);
56 mask = ~mask;
57
58 value = 0;
59
60 if (entry->is_64_bit) {
61 value = pci_read_config32(dev, entry->reg + sizeof(uint32_t));
62 value <<= 32;
63 }
64
65 value |= (uint64_t)pci_read_config32(dev, entry->reg);
66 value &= mask;
67
68 if (entry->is_limit)
69 value |= ~mask;
70
71 *result = value;
72}
73
74static void mc_read_map_entries(struct device *dev, uint64_t *values)
75{
76 int i;
77 for (i = 0; i < NUM_MAP_ENTRIES; i++)
78 read_map_entry(dev, &memory_map[i], &values[i]);
79}
80
81static void mc_report_map_entries(struct device *dev, uint64_t *values)
82{
83 int i;
84 for (i = 0; i < NUM_MAP_ENTRIES; i++) {
85 printk(BIOS_DEBUG, "MC MAP: %s: 0x%llx\n",
86 memory_map[i].description, values[i]);
87 }
88}
89
90/*
91 * Host Memory Map:
92 *
93 * +--------------------------+ TOCM (2 pow 46 - 1)
94 * | Reserved |
95 * +--------------------------+
96 * | MMIOH (relocatable) |
97 * +--------------------------+
98 * | PCISeg |
99 * +--------------------------+ TOHM
100 * | High DRAM Memory |
101 * +--------------------------+ 4GiB (0x100000000)
102 * +--------------------------+ 0xFFFF_FFFF
103 * | Firmware |
104 * +--------------------------+ 0xFF00_0000
105 * | Reserved |
106 * +--------------------------+ 0xFEF0_0000
107 * | Local xAPIC |
108 * +--------------------------+ 0xFEE0_0000
109 * | HPET/LT/TPM/Others |
110 * +--------------------------+ 0xFED0_0000
111 * | I/O xAPIC |
112 * +--------------------------+ 0xFEC0_0000
113 * | Reserved |
114 * +--------------------------+ 0xFEB8_0000
115 * | Reserved |
116 * +--------------------------+ 0xFEB0_0000
117 * | Reserved |
118 * +--------------------------+ 0xFE00_0000
119 * | MMIOL (relocatable) |
120 * | P2SB PCR cfg BAR | (0xfd000000 - 0xfdffffff
121 * | BAR space | [mem 0x90000000-0xfcffffff] available for PCI devices
122 * +--------------------------+ 0x9000_0000
123 * |PCIe MMCFG (relocatable) | CONFIG_MMCONF_BASE_ADDRESS 64 or 256MB
124 * | | (0x80000000 - 0x8fffffff, 0x40000)
125 * +--------------------------+ TOLM
126 * | MEseg (relocatable) | 32, 64, 128 or 256 MB (0x78000000 - 0x7fffffff, 0x20000)
127 * +--------------------------+
128 * | Tseg (relocatable) | N x 8MB (0x70000000 - 0x77ffffff, 0x20000)
129 * +--------------------------+ cbmem_top
130 * | Reserved - CBMEM | (0x6fffe000 - 0x6fffffff, 0x2000)
131 * +--------------------------+
132 * | Reserved - FSP | (0x6fbfe000 - 0x6fffdfff, 0x400000)
133 * +--------------------------+ top_of_ram (0x6fbfdfff)
134 * | Low DRAM Memory |
135 * +--------------------------+ FFFFF (1MB)
136 * | E & F segments |
137 * +--------------------------+ E0000
138 * | C & D segments |
139 * +--------------------------+ C0000
140 * | VGA & SMM Memory |
141 * +--------------------------+ A0000
142 * | Conventional Memory |
143 * | (DOS Range) |
144 * +--------------------------+ 0
145 */
146
147static void mc_add_dram_resources(struct device *dev, int *res_count)
148{
149 struct range_entry fsp_mem;
150 uint64_t base_kb;
151 uint64_t size_kb;
152 uint64_t top_of_ram;
153 uint64_t mc_values[NUM_MAP_ENTRIES];
154 struct resource *resource;
155 int index = *res_count;
156
157 fsp_find_reserved_memory(&fsp_mem);
158
159 /* Read in the MAP registers and report their values. */
160 mc_read_map_entries(dev, &mc_values[0]);
161 mc_report_map_entries(dev, &mc_values[0]);
162
163 top_of_ram = range_entry_base(&fsp_mem) - 1;
164 printk(BIOS_SPEW, "cbmem_top: 0x%lx, fsp range: [0x%llx - 0x%llx], top_of_ram: 0x%llx\n",
165 (uintptr_t) cbmem_top(), range_entry_base(&fsp_mem),
166 range_entry_end(&fsp_mem), top_of_ram);
167
168 /* Conventional Memory (DOS region, 0x0 to 0x9FFFF) */
169 base_kb = 0;
170 size_kb = (0xa0000 >> 10);
171 LOG_MEM_RESOURCE("legacy_ram", dev, index, base_kb, size_kb);
172 ram_resource(dev, index++, base_kb, size_kb);
173
174 /* 1MB -> top_of_ram i.e., fsp_mem_base+1*/
175 base_kb = (0x100000 >> 10);
176 size_kb = (top_of_ram - 0xfffff) >> 10;
177 LOG_MEM_RESOURCE("low_ram", dev, index, base_kb, size_kb);
178 ram_resource(dev, index++, base_kb, size_kb);
179
180 /*
181 * FSP meomoy, CBMem regions are already added as reserved
182 * Add TSEG and MESEG Regions as reserved memory
183 * src/drivers/intel/fsp2_0/memory_init.c sets CBMEM reserved size
184 * arch_upd->BootLoaderTolumSize = cbmem_overhead_size(); == 2 * CBMEM_ROOT_MIN_SIZE
185 * typically 0x2000
186 * Example config:
187 * FSP_RESERVED_MEMORY_RESOURCE_HOB
188 * FspReservedMemoryResource Base : 6FBFE000
189 * FspReservedMemoryResource Size : 400000
190 * FSP_BOOT_LOADER_TOLUM_HOB
191 * FspBootLoaderTolum Base : 6FFFE000
192 * FspBootLoaderTolum Size : 2000
193 */
194
195 /* Mark TSEG/SMM region as reserved */
196 base_kb = (mc_values[TSEG_BASE_REG] >> 10);
197 size_kb = (mc_values[TSEG_LIMIT_REG] - mc_values[TSEG_BASE_REG] + 1) >> 10;
198 LOG_MEM_RESOURCE("mmio_tseg", dev, index, base_kb, size_kb);
199 reserved_ram_resource(dev, index++, base_kb, size_kb);
200
201 /* Mark region between TSEG - TOLM (eg. MESEG) as reserved */
202 if (mc_values[TSEG_LIMIT_REG] < mc_values[TOLM_REG]) {
203 base_kb = ((mc_values[TSEG_LIMIT_REG] + 1) >> 10);
204 size_kb = (mc_values[TOLM_REG] - mc_values[TSEG_LIMIT_REG]) >> 10;
205 LOG_MEM_RESOURCE("mmio_tolm", dev, index, base_kb, size_kb);
206 reserved_ram_resource(dev, index++, base_kb, size_kb);
207 }
208
209 /* 4GiB -> TOHM */
210 if (mc_values[TOHM_REG] > 0x100000000) {
211 base_kb = (0x100000000 >> 10);
212 size_kb = (mc_values[TOHM_REG] - 0x100000000 + 1) >> 10;
213 LOG_MEM_RESOURCE("high_ram", dev, index, base_kb, size_kb);
214 ram_resource(dev, index++, base_kb, size_kb);
215 }
216
217 /* add MMIO CFG resource */
218 resource = new_resource(dev, index++);
219 resource->base = (resource_t) mc_values[MMCFG_BASE_REG];
220 resource->size = (resource_t) (mc_values[MMCFG_LIMIT_REG] -
221 mc_values[MMCFG_BASE_REG] + 1);
222 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
223 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
224 LOG_MEM_RESOURCE("mmiocfg_res", dev, index-1, (resource->base >> 10),
225 (resource->size >> 10));
226
227 /* add Local APIC resource */
228 resource = new_resource(dev, index++);
229 resource->base = LAPIC_DEFAULT_BASE;
230 resource->size = 0x00001000;
231 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
232 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
233 LOG_MEM_RESOURCE("apic_res", dev, index-1, (resource->base >> 10),
234 (resource->size >> 10));
235
236 /*
237 * Add legacy region as reserved - 0xa000 - 1MB
238 * Reserve everything between A segment and 1MB:
239 *
240 * 0xa0000 - 0xbffff: legacy VGA
241 * 0xc0000 - 0xfffff: RAM
242 */
243 base_kb = VGA_BASE_ADDRESS >> 10;
244 size_kb = VGA_BASE_SIZE >> 10;
245 LOG_MEM_RESOURCE("legacy_mmio", dev, index, base_kb, size_kb);
246 mmio_resource(dev, index++, base_kb, size_kb);
247
248 base_kb = (0xc0000 >> 10);
249 size_kb = (0x100000 - 0xc0000) >> 10;
250 LOG_MEM_RESOURCE("legacy_write_protect", dev, index, base_kb, size_kb);
251 reserved_ram_resource(dev, index++, base_kb, size_kb);
252
253 *res_count = index;
254}
255
256static void mmapvtd_read_resources(struct device *dev)
257{
258 int index = 0;
259
260 /* Read standard PCI resources. */
261 pci_dev_read_resources(dev);
262
263 /* Calculate and add DRAM resources. */
264 mc_add_dram_resources(dev, &index);
265}
266
267static void mmapvtd_init(struct device *dev)
268{
269}
270
271static struct device_operations mmapvtd_ops = {
272 .read_resources = mmapvtd_read_resources,
273 .set_resources = pci_dev_set_resources,
274 .enable_resources = pci_dev_enable_resources,
275 .init = mmapvtd_init,
276 .ops_pci = &soc_pci_ops,
Jonathan Zhang8f895492020-01-16 11:16:45 -0800277};
278
279static const unsigned short mmapvtd_ids[] = {
280 MMAP_VTD_CFG_REG_DEVID, /* Memory Map/IntelĀ® VT-d Configuration Registers */
281 0
282};
283
284static const struct pci_driver mmapvtd_driver __pci_driver = {
285 .ops = &mmapvtd_ops,
286 .vendor = PCI_VENDOR_ID_INTEL,
287 .devices = mmapvtd_ids
288};