blob: 1c6a8fce8ba88e9b46b1867e1d2c7881909eab4e [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <console/console.h>
22#include <arch/acpi.h>
23#include <arch/io.h>
24#include <stdint.h>
25#include <delay.h>
26#include <cpu/intel/haswell/haswell.h>
27#include <cpu/x86/msr.h>
28#include <device/device.h>
29#include <device/pci.h>
30#include <device/pci_ids.h>
31#include <device/hypertransport.h>
32#include <stdlib.h>
33#include <string.h>
34#include <cpu/cpu.h>
35#include <boot/tables.h>
36#include <cbmem.h>
37#include "chip.h"
38#include "haswell.h"
39
40static int bridge_revision_id = -1;
41
42int bridge_silicon_revision(void)
43{
44 if (bridge_revision_id < 0) {
45 uint8_t stepping = cpuid_eax(1) & 0xf;
46 uint8_t bridge_id = pci_read_config16(
47 dev_find_slot(0, PCI_DEVFN(0, 0)),
48 PCI_DEVICE_ID) & 0xf0;
49 bridge_revision_id = bridge_id | stepping;
50 }
51 return bridge_revision_id;
52}
53
54/* Reserve everything between A segment and 1MB:
55 *
56 * 0xa0000 - 0xbffff: legacy VGA
57 * 0xc0000 - 0xcffff: VGA OPROM (needed by kernel)
58 * 0xe0000 - 0xfffff: SeaBIOS, if used, otherwise DMI
59 */
60static const int legacy_hole_base_k = 0xa0000 / 1024;
61static const int legacy_hole_size_k = 384;
62
63void cbmem_post_handling(void)
64{
65 update_mrc_cache();
66}
67
68static int get_pcie_bar(u32 *base, u32 *len)
69{
70 device_t dev;
71 u32 pciexbar_reg;
72
73 *base = 0;
74 *len = 0;
75
76 dev = dev_find_slot(0, PCI_DEVFN(0, 0));
77 if (!dev)
78 return 0;
79
80 pciexbar_reg = pci_read_config32(dev, PCIEXBAR);
81
82 if (!(pciexbar_reg & (1 << 0)))
83 return 0;
84
85 switch ((pciexbar_reg >> 1) & 3) {
86 case 0: // 256MB
87 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28));
88 *len = 256 * 1024 * 1024;
89 return 1;
90 case 1: // 128M
91 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
92 *len = 128 * 1024 * 1024;
93 return 1;
94 case 2: // 64M
95 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27)|(1 << 26));
96 *len = 64 * 1024 * 1024;
97 return 1;
98 }
99
100 return 0;
101}
102
103static void add_fixed_resources(struct device *dev, int index)
104{
105 struct resource *resource;
106 u32 pcie_config_base, pcie_config_size;
107
108 printk(BIOS_DEBUG, "Adding UMA memory area base=0x%llx "
109 "size=0x%llx\n", uma_memory_base, uma_memory_size);
110 resource = new_resource(dev, index);
111 resource->base = (resource_t) uma_memory_base;
112 resource->size = (resource_t) uma_memory_size;
113 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
114 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
115
116 /* Clear these values here so they don't get used by MTRR code */
117 uma_memory_base = 0;
118 uma_memory_size = 0;
119
120 if (get_pcie_bar(&pcie_config_base, &pcie_config_size)) {
121 printk(BIOS_DEBUG, "Adding PCIe config bar base=0x%08x "
122 "size=0x%x\n", pcie_config_base, pcie_config_size);
123 resource = new_resource(dev, index+1);
124 resource->base = (resource_t) pcie_config_base;
125 resource->size = (resource_t) pcie_config_size;
126 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
127 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
128 }
129
130 mmio_resource(dev, index++, legacy_hole_base_k, legacy_hole_size_k);
131
132#if CONFIG_CHROMEOS_RAMOOPS
133 mmio_resource(dev, index++, CONFIG_CHROMEOS_RAMOOPS_RAM_START >> 10,
134 CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE >> 10);
135#endif
136
137 /* Required for SandyBridge sighting 3715511 */
138 bad_ram_resource(dev, index++, 0x20000000 >> 10, 0x00200000 >> 10);
139 bad_ram_resource(dev, index++, 0x40000000 >> 10, 0x00200000 >> 10);
140}
141
142static void pci_domain_set_resources(device_t dev)
143{
144 uint64_t tom, me_base, touud;
145 uint32_t tseg_base, uma_size, tolud;
146 uint16_t ggc;
147 unsigned long long tomk;
148
149 /* Total Memory 2GB example:
150 *
151 * 00000000 0000MB-1992MB 1992MB RAM (writeback)
152 * 7c800000 1992MB-2000MB 8MB TSEG (SMRR)
153 * 7d000000 2000MB-2002MB 2MB GFX GTT (uncached)
154 * 7d200000 2002MB-2034MB 32MB GFX UMA (uncached)
155 * 7f200000 2034MB TOLUD
156 * 7f800000 2040MB MEBASE
157 * 7f800000 2040MB-2048MB 8MB ME UMA (uncached)
158 * 80000000 2048MB TOM
159 * 100000000 4096MB-4102MB 6MB RAM (writeback)
160 *
161 * Total Memory 4GB example:
162 *
163 * 00000000 0000MB-2768MB 2768MB RAM (writeback)
164 * ad000000 2768MB-2776MB 8MB TSEG (SMRR)
165 * ad800000 2776MB-2778MB 2MB GFX GTT (uncached)
166 * ada00000 2778MB-2810MB 32MB GFX UMA (uncached)
167 * afa00000 2810MB TOLUD
168 * ff800000 4088MB MEBASE
169 * ff800000 4088MB-4096MB 8MB ME UMA (uncached)
170 * 100000000 4096MB TOM
171 * 100000000 4096MB-5374MB 1278MB RAM (writeback)
172 * 14fe00000 5368MB TOUUD
173 */
174
175 /* Top of Upper Usable DRAM, including remap */
176 touud = pci_read_config32(dev, TOUUD+4);
177 touud <<= 32;
178 touud |= pci_read_config32(dev, TOUUD);
179
180 /* Top of Lower Usable DRAM */
181 tolud = pci_read_config32(dev, TOLUD);
182
183 /* Top of Memory - does not account for any UMA */
184 tom = pci_read_config32(dev, 0xa4);
185 tom <<= 32;
186 tom |= pci_read_config32(dev, 0xa0);
187
188 printk(BIOS_DEBUG, "TOUUD 0x%llx TOLUD 0x%08x TOM 0x%llx\n",
189 touud, tolud, tom);
190
191 /* ME UMA needs excluding if total memory <4GB */
192 me_base = pci_read_config32(dev, 0x74);
193 me_base <<= 32;
194 me_base |= pci_read_config32(dev, 0x70);
195
196 printk(BIOS_DEBUG, "MEBASE 0x%llx\n", me_base);
197
198 tomk = tolud >> 10;
199 if (me_base == tolud) {
200 /* ME is from MEBASE-TOM */
201 uma_size = (tom - me_base) >> 10;
202 /* Increment TOLUD to account for ME as RAM */
203 tolud += uma_size << 10;
204 /* UMA starts at old TOLUD */
205 uma_memory_base = tomk * 1024ULL;
206 uma_memory_size = uma_size * 1024ULL;
207 printk(BIOS_DEBUG, "ME UMA base 0x%llx size %uM\n",
208 me_base, uma_size >> 10);
209 }
210
211 /* Graphics memory comes next */
212 ggc = pci_read_config16(dev, GGC);
213 if (!(ggc & 2)) {
214 printk(BIOS_DEBUG, "IGD decoded, subtracting ");
215
216 /* Graphics memory */
217 uma_size = ((ggc >> 3) & 0x1f) * 32 * 1024ULL;
218 printk(BIOS_DEBUG, "%uM UMA", uma_size >> 10);
219 tomk -= uma_size;
220 uma_memory_base = tomk * 1024ULL;
221 uma_memory_size += uma_size * 1024ULL;
222
223 /* GTT Graphics Stolen Memory Size (GGMS) */
224 uma_size = ((ggc >> 8) & 0x3) * 1024ULL;
225 tomk -= uma_size;
226 uma_memory_base = tomk * 1024ULL;
227 uma_memory_size += uma_size * 1024ULL;
228 printk(BIOS_DEBUG, " and %uM GTT\n", uma_size >> 10);
229 }
230
231 /* Calculate TSEG size from its base which must be below GTT */
232 tseg_base = pci_read_config32(dev, 0xb8);
233 uma_size = (uma_memory_base - tseg_base) >> 10;
234 tomk -= uma_size;
235 uma_memory_base = tomk * 1024ULL;
236 uma_memory_size += uma_size * 1024ULL;
237 printk(BIOS_DEBUG, "TSEG base 0x%08x size %uM\n",
238 tseg_base, uma_size >> 10);
239
240 printk(BIOS_INFO, "Available memory below 4GB: %lluM\n", tomk >> 10);
241
242 /* Report the memory regions */
243 ram_resource(dev, 3, 0, legacy_hole_base_k);
244 ram_resource(dev, 4, legacy_hole_base_k + legacy_hole_size_k,
245 (tomk - (legacy_hole_base_k + legacy_hole_size_k)));
246
247 /*
248 * If >= 4GB installed then memory from TOLUD to 4GB
249 * is remapped above TOM, TOUUD will account for both
250 */
251 touud >>= 10; /* Convert to KB */
252 if (touud > 4096 * 1024) {
253 ram_resource(dev, 5, 4096 * 1024, touud - (4096 * 1024));
254 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n",
255 (touud >> 10) - 4096);
256 }
257
258 add_fixed_resources(dev, 6);
259
260 assign_resources(dev->link_list);
261
262 /* Leave some space for ACPI, PIRQ and MP tables */
263 high_tables_base = (tomk * 1024) - HIGH_MEMORY_SIZE;
264 high_tables_size = HIGH_MEMORY_SIZE;
265}
266
267 /* TODO We could determine how many PCIe busses we need in
268 * the bar. For now that number is hardcoded to a max of 64.
269 * See e7525/northbridge.c for an example.
270 */
271static struct device_operations pci_domain_ops = {
272 .read_resources = pci_domain_read_resources,
273 .set_resources = pci_domain_set_resources,
274 .enable_resources = NULL,
275 .init = NULL,
276 .scan_bus = pci_domain_scan_bus,
277#if CONFIG_MMCONF_SUPPORT_DEFAULT
278 .ops_pci_bus = &pci_ops_mmconf,
279#else
280 .ops_pci_bus = &pci_cf8_conf1,
281#endif
282};
283
284static void mc_read_resources(device_t dev)
285{
286 struct resource *resource;
287
288 pci_dev_read_resources(dev);
289
290 /* So, this is one of the big mysteries in the coreboot resource
291 * allocator. This resource should make sure that the address space
292 * of the PCIe memory mapped config space bar. But it does not.
293 */
294
295 /* We use 0xcf as an unused index for our PCIe bar so that we find it again */
296 resource = new_resource(dev, 0xcf);
297 resource->base = DEFAULT_PCIEXBAR;
298 resource->size = 64 * 1024 * 1024; /* 64MB hard coded PCIe config space */
299 resource->flags =
300 IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
301 IORESOURCE_ASSIGNED;
302 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
303 (unsigned long)(resource->base), (unsigned long)(resource->base + resource->size));
304}
305
306static void mc_set_resources(device_t dev)
307{
308 struct resource *resource;
309
310 /* Report the PCIe BAR */
311 resource = find_resource(dev, 0xcf);
312 if (resource) {
313 report_resource_stored(dev, resource, "<mmconfig>");
314 }
315
316 /* And call the normal set_resources */
317 pci_dev_set_resources(dev);
318}
319
320static void intel_set_subsystem(device_t dev, unsigned vendor, unsigned device)
321{
322 if (!vendor || !device) {
323 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
324 pci_read_config32(dev, PCI_VENDOR_ID));
325 } else {
326 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
327 ((device & 0xffff) << 16) | (vendor & 0xffff));
328 }
329}
330
331static void northbridge_dmi_init(struct device *dev)
332{
333 u32 reg32;
334
335 /* Clear error status bits */
336 DMIBAR32(0x1c4) = 0xffffffff;
337 DMIBAR32(0x1d0) = 0xffffffff;
338
339 /* Steps prior to DMI ASPM */
340 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_SNB) {
341 reg32 = DMIBAR32(0x250);
342 reg32 &= ~((1 << 22)|(1 << 20));
343 reg32 |= (1 << 21);
344 DMIBAR32(0x250) = reg32;
345 }
346
347 reg32 = DMIBAR32(0x238);
348 reg32 |= (1 << 29);
349 DMIBAR32(0x238) = reg32;
350
351 if (bridge_silicon_revision() >= SNB_STEP_D0) {
352 reg32 = DMIBAR32(0x1f8);
353 reg32 |= (1 << 16);
354 DMIBAR32(0x1f8) = reg32;
355 } else if (bridge_silicon_revision() >= SNB_STEP_D1) {
356 reg32 = DMIBAR32(0x1f8);
357 reg32 &= ~(1 << 26);
358 reg32 |= (1 << 16);
359 DMIBAR32(0x1f8) = reg32;
360
361 reg32 = DMIBAR32(0x1fc);
362 reg32 |= (1 << 12) | (1 << 23);
363 DMIBAR32(0x1fc) = reg32;
364 }
365
366 /* Enable ASPM on SNB link, should happen before PCH link */
367 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_SNB) {
368 reg32 = DMIBAR32(0xd04);
369 reg32 |= (1 << 4);
370 DMIBAR32(0xd04) = reg32;
371 }
372
373 reg32 = DMIBAR32(0x88);
374 reg32 |= (1 << 1) | (1 << 0);
375 DMIBAR32(0x88) = reg32;
376}
377
378static void northbridge_init(struct device *dev)
379{
380 u8 bios_reset_cpl;
381 u32 bridge_type;
382
383 northbridge_dmi_init(dev);
384
385 bridge_type = MCHBAR32(0x5f10);
386 bridge_type &= ~0xff;
387
388 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_IVB) {
389 /* Enable Power Aware Interrupt Routing */
390 u8 pair = MCHBAR8(0x5418);
391 pair &= ~0xf; /* Clear 3:0 */
392 pair |= 0x4; /* Fixed Priority */
393 MCHBAR8(0x5418) = pair;
394
395 /* 30h for IvyBridge */
396 bridge_type |= 0x30;
397 } else {
398 /* 20h for Sandybridge */
399 bridge_type |= 0x20;
400 }
401 MCHBAR32(0x5f10) = bridge_type;
402
403 /*
404 * Set bit 0 of BIOS_RESET_CPL to indicate to the CPU
405 * that BIOS has initialized memory and power management
406 */
407 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
408 bios_reset_cpl |= 1;
409 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
410 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
411
412 /* Configure turbo power limits 1ms after reset complete bit */
413 mdelay(1);
414 set_power_limits(28);
415
416 /*
417 * CPUs with configurable TDP also need power limits set
418 * in MCHBAR. Use same values from MSR_PKG_POWER_LIMIT.
419 */
420 if (cpu_config_tdp_levels()) {
421 msr_t msr = rdmsr(MSR_PKG_POWER_LIMIT);
422 MCHBAR32(0x59A0) = msr.lo;
423 MCHBAR32(0x59A4) = msr.hi;
424 }
425
426 /* Set here before graphics PM init */
427 MCHBAR32(0x5500) = 0x00100001;
428}
429
430static void northbridge_enable(device_t dev)
431{
432#if CONFIG_HAVE_ACPI_RESUME
433 switch (pci_read_config32(dev, SKPAD)) {
434 case 0xcafebabe:
435 printk(BIOS_DEBUG, "Normal boot.\n");
436 acpi_slp_type=0;
437 break;
438 case 0xcafed00d:
439 printk(BIOS_DEBUG, "S3 Resume.\n");
440 acpi_slp_type=3;
441 break;
442 default:
443 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
444 acpi_slp_type=0;
445 break;
446 }
447#endif
448}
449
450static struct pci_operations intel_pci_ops = {
451 .set_subsystem = intel_set_subsystem,
452};
453
454static struct device_operations mc_ops = {
455 .read_resources = mc_read_resources,
456 .set_resources = mc_set_resources,
457 .enable_resources = pci_dev_enable_resources,
458 .init = northbridge_init,
459 .enable = northbridge_enable,
460 .scan_bus = 0,
461 .ops_pci = &intel_pci_ops,
462};
463
464static const struct pci_driver mc_driver_0100 __pci_driver = {
465 .ops = &mc_ops,
466 .vendor = PCI_VENDOR_ID_INTEL,
467 .device = 0x0100,
468};
469
470static const struct pci_driver mc_driver __pci_driver = {
471 .ops = &mc_ops,
472 .vendor = PCI_VENDOR_ID_INTEL,
473 .device = 0x0104, /* Sandy bridge */
474};
475
476static const struct pci_driver mc_driver_1 __pci_driver = {
477 .ops = &mc_ops,
478 .vendor = PCI_VENDOR_ID_INTEL,
479 .device = 0x0154, /* Ivy bridge */
480};
481
482static void cpu_bus_init(device_t dev)
483{
484 initialize_cpus(dev->link_list);
485}
486
487static void cpu_bus_noop(device_t dev)
488{
489}
490
491static struct device_operations cpu_bus_ops = {
492 .read_resources = cpu_bus_noop,
493 .set_resources = cpu_bus_noop,
494 .enable_resources = cpu_bus_noop,
495 .init = cpu_bus_init,
496 .scan_bus = 0,
497};
498
499static void enable_dev(device_t dev)
500{
501 /* Set the operations if it is a special bus type */
502 if (dev->path.type == DEVICE_PATH_DOMAIN) {
503 dev->ops = &pci_domain_ops;
504 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
505 dev->ops = &cpu_bus_ops;
506 }
507}
508
509struct chip_operations northbridge_intel_haswell_ops = {
510 CHIP_NAME("Intel i7 (Haswell) integrated Northbridge")
511 .enable_dev = enable_dev,
512};