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