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