blob: 4b6808a0fb2f1a3d6b42e65165a9cb714dfc643b [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>
Stefan Reinauer00636b02012-04-04 00:08:51 +020034#include <cpu/cpu.h>
Stefan Reinauerbb11e602012-05-10 12:15:18 -070035#include <cbmem.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020036#include "chip.h"
37#include "sandybridge.h"
38
39static int bridge_revision_id = -1;
40
41int bridge_silicon_revision(void)
42{
43 if (bridge_revision_id < 0) {
44 uint8_t stepping = cpuid_eax(1) & 0xf;
45 uint8_t bridge_id = pci_read_config16(
46 dev_find_slot(0, PCI_DEVFN(0, 0)),
47 PCI_DEVICE_ID) & 0xf0;
48 bridge_revision_id = bridge_id | stepping;
49 }
50 return bridge_revision_id;
51}
52
53/* Reserve everything between A segment and 1MB:
54 *
55 * 0xa0000 - 0xbffff: legacy VGA
56 * 0xc0000 - 0xcffff: VGA OPROM (needed by kernel)
57 * 0xe0000 - 0xfffff: SeaBIOS, if used, otherwise DMI
58 */
59static const int legacy_hole_base_k = 0xa0000 / 1024;
60static const int legacy_hole_size_k = 384;
61
Stefan Reinauer1244f4b2012-05-10 11:31:40 -070062void cbmem_post_handling(void)
63{
64 update_mrc_cache();
65}
66
Stefan Reinauer00636b02012-04-04 00:08:51 +020067static int get_pcie_bar(u32 *base, u32 *len)
68{
69 device_t dev;
70 u32 pciexbar_reg;
71
72 *base = 0;
73 *len = 0;
74
75 dev = dev_find_slot(0, PCI_DEVFN(0, 0));
76 if (!dev)
77 return 0;
78
79 pciexbar_reg = pci_read_config32(dev, PCIEXBAR);
80
81 if (!(pciexbar_reg & (1 << 0)))
82 return 0;
83
84 switch ((pciexbar_reg >> 1) & 3) {
85 case 0: // 256MB
86 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28));
87 *len = 256 * 1024 * 1024;
88 return 1;
89 case 1: // 128M
90 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
91 *len = 128 * 1024 * 1024;
92 return 1;
93 case 2: // 64M
94 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27)|(1 << 26));
95 *len = 64 * 1024 * 1024;
96 return 1;
97 }
98
99 return 0;
100}
101
Stefan Reinauer00636b02012-04-04 00:08:51 +0200102static void add_fixed_resources(struct device *dev, int index)
103{
104 struct resource *resource;
105 u32 pcie_config_base, pcie_config_size;
106
Kyösti Mälkki7f189cc2012-07-27 13:12:03 +0300107 /* Using uma_resource() here would fail as base & size cannot
108 * be used as-is for a single MTRR. This would cause excessive
109 * use of MTRRs.
110 *
111 * Use of mmio_resource() instead does not create UC holes by using
112 * MTRRs, but making these regions uncacheable is taken care of by
113 * making sure they do not overlap with any ram_resource().
114 *
115 * The resources can be changed to use separate mmio_resource()
116 * calls after MTRR code is able to merge them wisely.
117 */
118 mmio_resource(dev, index++, uma_memory_base >> 10, uma_memory_size >> 10);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200119
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);
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300123 resource = new_resource(dev, index++);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200124 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 }
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300129
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);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200140}
141
Stefan Reinauer00636b02012-04-04 00:08:51 +0200142static 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
Patrick Georgie1667822012-05-05 15:29:32 +0200262#if CONFIG_WRITE_HIGH_TABLES
Stefan Reinauer00636b02012-04-04 00:08:51 +0200263 /* Leave some space for ACPI, PIRQ and MP tables */
264 high_tables_base = (tomk * 1024) - HIGH_MEMORY_SIZE;
265 high_tables_size = HIGH_MEMORY_SIZE;
266#endif
267}
268
269 /* TODO We could determine how many PCIe busses we need in
270 * the bar. For now that number is hardcoded to a max of 64.
271 * See e7525/northbridge.c for an example.
272 */
273static struct device_operations pci_domain_ops = {
274 .read_resources = pci_domain_read_resources,
275 .set_resources = pci_domain_set_resources,
276 .enable_resources = NULL,
277 .init = NULL,
278 .scan_bus = pci_domain_scan_bus,
279#if CONFIG_MMCONF_SUPPORT_DEFAULT
280 .ops_pci_bus = &pci_ops_mmconf,
281#else
282 .ops_pci_bus = &pci_cf8_conf1,
283#endif
284};
285
286static void mc_read_resources(device_t dev)
287{
288 struct resource *resource;
289
290 pci_dev_read_resources(dev);
291
292 /* So, this is one of the big mysteries in the coreboot resource
293 * allocator. This resource should make sure that the address space
294 * of the PCIe memory mapped config space bar. But it does not.
295 */
296
297 /* We use 0xcf as an unused index for our PCIe bar so that we find it again */
298 resource = new_resource(dev, 0xcf);
299 resource->base = DEFAULT_PCIEXBAR;
300 resource->size = 64 * 1024 * 1024; /* 64MB hard coded PCIe config space */
301 resource->flags =
302 IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
303 IORESOURCE_ASSIGNED;
304 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
305 (unsigned long)(resource->base), (unsigned long)(resource->base + resource->size));
306}
307
308static void mc_set_resources(device_t dev)
309{
310 struct resource *resource;
311
312 /* Report the PCIe BAR */
313 resource = find_resource(dev, 0xcf);
314 if (resource) {
315 report_resource_stored(dev, resource, "<mmconfig>");
316 }
317
318 /* And call the normal set_resources */
319 pci_dev_set_resources(dev);
320}
321
322static void intel_set_subsystem(device_t dev, unsigned vendor, unsigned device)
323{
324 if (!vendor || !device) {
325 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
326 pci_read_config32(dev, PCI_VENDOR_ID));
327 } else {
328 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
329 ((device & 0xffff) << 16) | (vendor & 0xffff));
330 }
331}
332
333static void northbridge_dmi_init(struct device *dev)
334{
335 u32 reg32;
336
337 /* Clear error status bits */
338 DMIBAR32(0x1c4) = 0xffffffff;
339 DMIBAR32(0x1d0) = 0xffffffff;
340
341 /* Steps prior to DMI ASPM */
Vincent Palatin0ff99b72012-03-28 16:10:29 -0700342 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_SNB) {
343 reg32 = DMIBAR32(0x250);
344 reg32 &= ~((1 << 22)|(1 << 20));
345 reg32 |= (1 << 21);
346 DMIBAR32(0x250) = reg32;
347 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200348
349 reg32 = DMIBAR32(0x238);
350 reg32 |= (1 << 29);
351 DMIBAR32(0x238) = reg32;
352
353 if (bridge_silicon_revision() >= SNB_STEP_D0) {
354 reg32 = DMIBAR32(0x1f8);
355 reg32 |= (1 << 16);
356 DMIBAR32(0x1f8) = reg32;
357 } else if (bridge_silicon_revision() >= SNB_STEP_D1) {
358 reg32 = DMIBAR32(0x1f8);
359 reg32 &= ~(1 << 26);
360 reg32 |= (1 << 16);
361 DMIBAR32(0x1f8) = reg32;
362
363 reg32 = DMIBAR32(0x1fc);
364 reg32 |= (1 << 12) | (1 << 23);
365 DMIBAR32(0x1fc) = reg32;
366 }
367
368 /* Enable ASPM on SNB link, should happen before PCH link */
Vincent Palatin0ff99b72012-03-28 16:10:29 -0700369 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_SNB) {
370 reg32 = DMIBAR32(0xd04);
371 reg32 |= (1 << 4);
372 DMIBAR32(0xd04) = reg32;
373 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200374
375 reg32 = DMIBAR32(0x88);
376 reg32 |= (1 << 1) | (1 << 0);
377 DMIBAR32(0x88) = reg32;
378}
379
380static void northbridge_init(struct device *dev)
381{
382 u8 bios_reset_cpl;
Duncan Lauriefe7b5d22012-06-23 20:14:07 -0700383 u32 bridge_type;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200384
385 northbridge_dmi_init(dev);
386
Duncan Lauriefe7b5d22012-06-23 20:14:07 -0700387 bridge_type = MCHBAR32(0x5f10);
388 bridge_type &= ~0xff;
389
390 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_IVB) {
391 /* Enable Power Aware Interrupt Routing */
392 u8 pair = MCHBAR8(0x5418);
393 pair &= ~0xf; /* Clear 3:0 */
394 pair |= 0x4; /* Fixed Priority */
395 MCHBAR8(0x5418) = pair;
396
397 /* 30h for IvyBridge */
398 bridge_type |= 0x30;
399 } else {
400 /* 20h for Sandybridge */
401 bridge_type |= 0x20;
402 }
403 MCHBAR32(0x5f10) = bridge_type;
404
Stefan Reinauer00636b02012-04-04 00:08:51 +0200405 /*
406 * Set bit 0 of BIOS_RESET_CPL to indicate to the CPU
407 * that BIOS has initialized memory and power management
408 */
409 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
410 bios_reset_cpl |= 1;
411 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
412 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
413
414 /* Configure turbo power limits 1ms after reset complete bit */
415 mdelay(1);
416 set_power_limits(28);
417
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700418 /*
419 * CPUs with configurable TDP also need power limits set
420 * in MCHBAR. Use same values from MSR_PKG_POWER_LIMIT.
421 */
422 if (cpu_config_tdp_levels()) {
423 msr_t msr = rdmsr(MSR_PKG_POWER_LIMIT);
424 MCHBAR32(0x59A0) = msr.lo;
425 MCHBAR32(0x59A4) = msr.hi;
426 }
427
Stefan Reinauer00636b02012-04-04 00:08:51 +0200428 /* Set here before graphics PM init */
429 MCHBAR32(0x5500) = 0x00100001;
430}
431
432static void northbridge_enable(device_t dev)
433{
434#if CONFIG_HAVE_ACPI_RESUME
435 switch (pci_read_config32(dev, SKPAD)) {
436 case 0xcafebabe:
437 printk(BIOS_DEBUG, "Normal boot.\n");
438 acpi_slp_type=0;
439 break;
440 case 0xcafed00d:
441 printk(BIOS_DEBUG, "S3 Resume.\n");
442 acpi_slp_type=3;
443 break;
444 default:
445 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
446 acpi_slp_type=0;
447 break;
448 }
449#endif
450}
451
452static struct pci_operations intel_pci_ops = {
453 .set_subsystem = intel_set_subsystem,
454};
455
456static struct device_operations mc_ops = {
457 .read_resources = mc_read_resources,
458 .set_resources = mc_set_resources,
459 .enable_resources = pci_dev_enable_resources,
460 .init = northbridge_init,
461 .enable = northbridge_enable,
462 .scan_bus = 0,
463 .ops_pci = &intel_pci_ops,
464};
465
Walter Murphy496f4a02012-04-23 11:08:03 -0700466static const struct pci_driver mc_driver_0100 __pci_driver = {
467 .ops = &mc_ops,
468 .vendor = PCI_VENDOR_ID_INTEL,
469 .device = 0x0100,
470};
471
Stefan Reinauer00636b02012-04-04 00:08:51 +0200472static const struct pci_driver mc_driver __pci_driver = {
473 .ops = &mc_ops,
474 .vendor = PCI_VENDOR_ID_INTEL,
475 .device = 0x0104, /* Sandy bridge */
476};
477
478static const struct pci_driver mc_driver_1 __pci_driver = {
479 .ops = &mc_ops,
480 .vendor = PCI_VENDOR_ID_INTEL,
481 .device = 0x0154, /* Ivy bridge */
482};
483
484static void cpu_bus_init(device_t dev)
485{
486 initialize_cpus(dev->link_list);
487}
488
489static void cpu_bus_noop(device_t dev)
490{
491}
492
493static struct device_operations cpu_bus_ops = {
494 .read_resources = cpu_bus_noop,
495 .set_resources = cpu_bus_noop,
496 .enable_resources = cpu_bus_noop,
497 .init = cpu_bus_init,
498 .scan_bus = 0,
499};
500
501static void enable_dev(device_t dev)
502{
503 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800504 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Stefan Reinauer00636b02012-04-04 00:08:51 +0200505 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800506 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
Stefan Reinauer00636b02012-04-04 00:08:51 +0200507 dev->ops = &cpu_bus_ops;
508 }
509}
510
511struct chip_operations northbridge_intel_sandybridge_ops = {
Stefan Reinauer9ca1c0a2012-07-25 16:10:36 -0700512 CHIP_NAME("Intel i7 (SandyBridge/IvyBridge) integrated Northbridge")
Stefan Reinauer00636b02012-04-04 00:08:51 +0200513 .enable_dev = enable_dev,
514};