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