blob: 5f9912ff6d32afd88decfb17b01f4a438ea21f74 [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
Kyösti Mälkkif7bfc342013-10-18 11:02:46 +030042/* IGD UMA memory */
43static uint64_t uma_memory_base = 0;
44static uint64_t uma_memory_size = 0;
45
Stefan Reinauer00636b02012-04-04 00:08:51 +020046int bridge_silicon_revision(void)
47{
48 if (bridge_revision_id < 0) {
49 uint8_t stepping = cpuid_eax(1) & 0xf;
50 uint8_t bridge_id = pci_read_config16(
51 dev_find_slot(0, PCI_DEVFN(0, 0)),
52 PCI_DEVICE_ID) & 0xf0;
53 bridge_revision_id = bridge_id | stepping;
54 }
55 return bridge_revision_id;
56}
57
58/* Reserve everything between A segment and 1MB:
59 *
60 * 0xa0000 - 0xbffff: legacy VGA
61 * 0xc0000 - 0xcffff: VGA OPROM (needed by kernel)
62 * 0xe0000 - 0xfffff: SeaBIOS, if used, otherwise DMI
63 */
64static const int legacy_hole_base_k = 0xa0000 / 1024;
65static const int legacy_hole_size_k = 384;
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 mmio_resource(dev, index++, uma_memory_base >> 10, uma_memory_size >> 10);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200108
109 if (get_pcie_bar(&pcie_config_base, &pcie_config_size)) {
110 printk(BIOS_DEBUG, "Adding PCIe config bar base=0x%08x "
111 "size=0x%x\n", pcie_config_base, pcie_config_size);
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300112 resource = new_resource(dev, index++);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200113 resource->base = (resource_t) pcie_config_base;
114 resource->size = (resource_t) pcie_config_size;
115 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
116 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
117 }
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300118
Aaron Durbinc9650762013-03-22 22:03:09 -0500119 mmio_resource(dev, index++, legacy_hole_base_k,
120 (0xc0000 >> 10) - legacy_hole_base_k);
121 reserved_ram_resource(dev, index++, 0xc0000 >> 10,
122 (0x100000 - 0xc0000) >> 10);
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300123
124#if CONFIG_CHROMEOS_RAMOOPS
Aaron Durbinc9650762013-03-22 22:03:09 -0500125 reserved_ram_resource(dev, index++,
126 CONFIG_CHROMEOS_RAMOOPS_RAM_START >> 10,
Kyösti Mälkki1ec5e742012-07-26 23:51:20 +0300127 CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE >> 10);
128#endif
129
130 /* Required for SandyBridge sighting 3715511 */
131 bad_ram_resource(dev, index++, 0x20000000 >> 10, 0x00200000 >> 10);
132 bad_ram_resource(dev, index++, 0x40000000 >> 10, 0x00200000 >> 10);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200133}
134
Stefan Reinauer00636b02012-04-04 00:08:51 +0200135static void pci_domain_set_resources(device_t dev)
136{
137 uint64_t tom, me_base, touud;
138 uint32_t tseg_base, uma_size, tolud;
139 uint16_t ggc;
140 unsigned long long tomk;
141
142 /* Total Memory 2GB example:
143 *
144 * 00000000 0000MB-1992MB 1992MB RAM (writeback)
145 * 7c800000 1992MB-2000MB 8MB TSEG (SMRR)
146 * 7d000000 2000MB-2002MB 2MB GFX GTT (uncached)
147 * 7d200000 2002MB-2034MB 32MB GFX UMA (uncached)
148 * 7f200000 2034MB TOLUD
149 * 7f800000 2040MB MEBASE
150 * 7f800000 2040MB-2048MB 8MB ME UMA (uncached)
151 * 80000000 2048MB TOM
152 * 100000000 4096MB-4102MB 6MB RAM (writeback)
153 *
154 * Total Memory 4GB example:
155 *
156 * 00000000 0000MB-2768MB 2768MB RAM (writeback)
157 * ad000000 2768MB-2776MB 8MB TSEG (SMRR)
158 * ad800000 2776MB-2778MB 2MB GFX GTT (uncached)
159 * ada00000 2778MB-2810MB 32MB GFX UMA (uncached)
160 * afa00000 2810MB TOLUD
161 * ff800000 4088MB MEBASE
162 * ff800000 4088MB-4096MB 8MB ME UMA (uncached)
163 * 100000000 4096MB TOM
164 * 100000000 4096MB-5374MB 1278MB RAM (writeback)
165 * 14fe00000 5368MB TOUUD
166 */
167
168 /* Top of Upper Usable DRAM, including remap */
169 touud = pci_read_config32(dev, TOUUD+4);
170 touud <<= 32;
171 touud |= pci_read_config32(dev, TOUUD);
172
173 /* Top of Lower Usable DRAM */
174 tolud = pci_read_config32(dev, TOLUD);
175
176 /* Top of Memory - does not account for any UMA */
177 tom = pci_read_config32(dev, 0xa4);
178 tom <<= 32;
179 tom |= pci_read_config32(dev, 0xa0);
180
181 printk(BIOS_DEBUG, "TOUUD 0x%llx TOLUD 0x%08x TOM 0x%llx\n",
182 touud, tolud, tom);
183
184 /* ME UMA needs excluding if total memory <4GB */
185 me_base = pci_read_config32(dev, 0x74);
186 me_base <<= 32;
187 me_base |= pci_read_config32(dev, 0x70);
188
189 printk(BIOS_DEBUG, "MEBASE 0x%llx\n", me_base);
190
191 tomk = tolud >> 10;
192 if (me_base == tolud) {
193 /* ME is from MEBASE-TOM */
194 uma_size = (tom - me_base) >> 10;
195 /* Increment TOLUD to account for ME as RAM */
196 tolud += uma_size << 10;
197 /* UMA starts at old TOLUD */
198 uma_memory_base = tomk * 1024ULL;
199 uma_memory_size = uma_size * 1024ULL;
200 printk(BIOS_DEBUG, "ME UMA base 0x%llx size %uM\n",
201 me_base, uma_size >> 10);
202 }
203
204 /* Graphics memory comes next */
205 ggc = pci_read_config16(dev, GGC);
206 if (!(ggc & 2)) {
207 printk(BIOS_DEBUG, "IGD decoded, subtracting ");
208
209 /* Graphics memory */
210 uma_size = ((ggc >> 3) & 0x1f) * 32 * 1024ULL;
211 printk(BIOS_DEBUG, "%uM UMA", uma_size >> 10);
212 tomk -= uma_size;
213 uma_memory_base = tomk * 1024ULL;
214 uma_memory_size += uma_size * 1024ULL;
215
216 /* GTT Graphics Stolen Memory Size (GGMS) */
217 uma_size = ((ggc >> 8) & 0x3) * 1024ULL;
218 tomk -= uma_size;
219 uma_memory_base = tomk * 1024ULL;
220 uma_memory_size += uma_size * 1024ULL;
221 printk(BIOS_DEBUG, " and %uM GTT\n", uma_size >> 10);
222 }
223
224 /* Calculate TSEG size from its base which must be below GTT */
225 tseg_base = pci_read_config32(dev, 0xb8);
226 uma_size = (uma_memory_base - tseg_base) >> 10;
227 tomk -= uma_size;
228 uma_memory_base = tomk * 1024ULL;
229 uma_memory_size += uma_size * 1024ULL;
230 printk(BIOS_DEBUG, "TSEG base 0x%08x size %uM\n",
231 tseg_base, uma_size >> 10);
232
233 printk(BIOS_INFO, "Available memory below 4GB: %lluM\n", tomk >> 10);
234
235 /* Report the memory regions */
236 ram_resource(dev, 3, 0, legacy_hole_base_k);
237 ram_resource(dev, 4, legacy_hole_base_k + legacy_hole_size_k,
238 (tomk - (legacy_hole_base_k + legacy_hole_size_k)));
239
240 /*
241 * If >= 4GB installed then memory from TOLUD to 4GB
242 * is remapped above TOM, TOUUD will account for both
243 */
244 touud >>= 10; /* Convert to KB */
245 if (touud > 4096 * 1024) {
246 ram_resource(dev, 5, 4096 * 1024, touud - (4096 * 1024));
247 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n",
248 (touud >> 10) - 4096);
249 }
250
251 add_fixed_resources(dev, 6);
252
253 assign_resources(dev->link_list);
254
Kyösti Mälkki42f46512013-06-27 08:20:09 +0300255 set_top_of_ram(tomk * 1024);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200256}
257
258 /* TODO We could determine how many PCIe busses we need in
259 * the bar. For now that number is hardcoded to a max of 64.
260 * See e7525/northbridge.c for an example.
261 */
262static struct device_operations pci_domain_ops = {
263 .read_resources = pci_domain_read_resources,
264 .set_resources = pci_domain_set_resources,
265 .enable_resources = NULL,
266 .init = NULL,
267 .scan_bus = pci_domain_scan_bus,
Kyösti Mälkki872c9222013-07-03 09:44:28 +0300268 .ops_pci_bus = pci_bus_default_ops,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200269};
270
271static void mc_read_resources(device_t dev)
272{
273 struct resource *resource;
274
275 pci_dev_read_resources(dev);
276
277 /* So, this is one of the big mysteries in the coreboot resource
278 * allocator. This resource should make sure that the address space
279 * of the PCIe memory mapped config space bar. But it does not.
280 */
281
282 /* We use 0xcf as an unused index for our PCIe bar so that we find it again */
283 resource = new_resource(dev, 0xcf);
284 resource->base = DEFAULT_PCIEXBAR;
285 resource->size = 64 * 1024 * 1024; /* 64MB hard coded PCIe config space */
286 resource->flags =
287 IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
288 IORESOURCE_ASSIGNED;
289 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
290 (unsigned long)(resource->base), (unsigned long)(resource->base + resource->size));
291}
292
293static void mc_set_resources(device_t dev)
294{
295 struct resource *resource;
296
297 /* Report the PCIe BAR */
298 resource = find_resource(dev, 0xcf);
299 if (resource) {
300 report_resource_stored(dev, resource, "<mmconfig>");
301 }
302
303 /* And call the normal set_resources */
304 pci_dev_set_resources(dev);
305}
306
307static void intel_set_subsystem(device_t dev, unsigned vendor, unsigned device)
308{
309 if (!vendor || !device) {
310 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
311 pci_read_config32(dev, PCI_VENDOR_ID));
312 } else {
313 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
314 ((device & 0xffff) << 16) | (vendor & 0xffff));
315 }
316}
317
318static void northbridge_dmi_init(struct device *dev)
319{
320 u32 reg32;
321
322 /* Clear error status bits */
323 DMIBAR32(0x1c4) = 0xffffffff;
324 DMIBAR32(0x1d0) = 0xffffffff;
325
326 /* Steps prior to DMI ASPM */
Vincent Palatin0ff99b72012-03-28 16:10:29 -0700327 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_SNB) {
328 reg32 = DMIBAR32(0x250);
329 reg32 &= ~((1 << 22)|(1 << 20));
330 reg32 |= (1 << 21);
331 DMIBAR32(0x250) = reg32;
332 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200333
334 reg32 = DMIBAR32(0x238);
335 reg32 |= (1 << 29);
336 DMIBAR32(0x238) = reg32;
337
338 if (bridge_silicon_revision() >= SNB_STEP_D0) {
339 reg32 = DMIBAR32(0x1f8);
340 reg32 |= (1 << 16);
341 DMIBAR32(0x1f8) = reg32;
342 } else if (bridge_silicon_revision() >= SNB_STEP_D1) {
343 reg32 = DMIBAR32(0x1f8);
344 reg32 &= ~(1 << 26);
345 reg32 |= (1 << 16);
346 DMIBAR32(0x1f8) = reg32;
347
348 reg32 = DMIBAR32(0x1fc);
349 reg32 |= (1 << 12) | (1 << 23);
350 DMIBAR32(0x1fc) = reg32;
351 }
352
353 /* Enable ASPM on SNB link, should happen before PCH link */
Vincent Palatin0ff99b72012-03-28 16:10:29 -0700354 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_SNB) {
355 reg32 = DMIBAR32(0xd04);
356 reg32 |= (1 << 4);
357 DMIBAR32(0xd04) = reg32;
358 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200359
360 reg32 = DMIBAR32(0x88);
361 reg32 |= (1 << 1) | (1 << 0);
362 DMIBAR32(0x88) = reg32;
363}
364
365static void northbridge_init(struct device *dev)
366{
367 u8 bios_reset_cpl;
Duncan Lauriefe7b5d22012-06-23 20:14:07 -0700368 u32 bridge_type;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200369
370 northbridge_dmi_init(dev);
371
Duncan Lauriefe7b5d22012-06-23 20:14:07 -0700372 bridge_type = MCHBAR32(0x5f10);
373 bridge_type &= ~0xff;
374
375 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_IVB) {
376 /* Enable Power Aware Interrupt Routing */
377 u8 pair = MCHBAR8(0x5418);
378 pair &= ~0xf; /* Clear 3:0 */
379 pair |= 0x4; /* Fixed Priority */
380 MCHBAR8(0x5418) = pair;
381
382 /* 30h for IvyBridge */
383 bridge_type |= 0x30;
384 } else {
385 /* 20h for Sandybridge */
386 bridge_type |= 0x20;
387 }
388 MCHBAR32(0x5f10) = bridge_type;
389
Stefan Reinauer00636b02012-04-04 00:08:51 +0200390 /*
391 * Set bit 0 of BIOS_RESET_CPL to indicate to the CPU
392 * that BIOS has initialized memory and power management
393 */
394 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
395 bios_reset_cpl |= 1;
396 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
397 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
398
399 /* Configure turbo power limits 1ms after reset complete bit */
400 mdelay(1);
401 set_power_limits(28);
402
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700403 /*
404 * CPUs with configurable TDP also need power limits set
405 * in MCHBAR. Use same values from MSR_PKG_POWER_LIMIT.
406 */
407 if (cpu_config_tdp_levels()) {
408 msr_t msr = rdmsr(MSR_PKG_POWER_LIMIT);
409 MCHBAR32(0x59A0) = msr.lo;
410 MCHBAR32(0x59A4) = msr.hi;
411 }
412
Stefan Reinauer00636b02012-04-04 00:08:51 +0200413 /* Set here before graphics PM init */
414 MCHBAR32(0x5500) = 0x00100001;
415}
416
417static void northbridge_enable(device_t dev)
418{
419#if CONFIG_HAVE_ACPI_RESUME
420 switch (pci_read_config32(dev, SKPAD)) {
421 case 0xcafebabe:
422 printk(BIOS_DEBUG, "Normal boot.\n");
423 acpi_slp_type=0;
424 break;
425 case 0xcafed00d:
426 printk(BIOS_DEBUG, "S3 Resume.\n");
427 acpi_slp_type=3;
428 break;
429 default:
430 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
431 acpi_slp_type=0;
432 break;
433 }
434#endif
435}
436
437static struct pci_operations intel_pci_ops = {
438 .set_subsystem = intel_set_subsystem,
439};
440
441static struct device_operations mc_ops = {
442 .read_resources = mc_read_resources,
443 .set_resources = mc_set_resources,
444 .enable_resources = pci_dev_enable_resources,
445 .init = northbridge_init,
446 .enable = northbridge_enable,
447 .scan_bus = 0,
448 .ops_pci = &intel_pci_ops,
449};
450
Walter Murphy496f4a02012-04-23 11:08:03 -0700451static const struct pci_driver mc_driver_0100 __pci_driver = {
452 .ops = &mc_ops,
453 .vendor = PCI_VENDOR_ID_INTEL,
454 .device = 0x0100,
455};
456
Stefan Reinauer00636b02012-04-04 00:08:51 +0200457static const struct pci_driver mc_driver __pci_driver = {
458 .ops = &mc_ops,
459 .vendor = PCI_VENDOR_ID_INTEL,
460 .device = 0x0104, /* Sandy bridge */
461};
462
463static const struct pci_driver mc_driver_1 __pci_driver = {
464 .ops = &mc_ops,
465 .vendor = PCI_VENDOR_ID_INTEL,
466 .device = 0x0154, /* Ivy bridge */
467};
468
469static void cpu_bus_init(device_t dev)
470{
471 initialize_cpus(dev->link_list);
472}
473
474static void cpu_bus_noop(device_t dev)
475{
476}
477
478static struct device_operations cpu_bus_ops = {
479 .read_resources = cpu_bus_noop,
480 .set_resources = cpu_bus_noop,
481 .enable_resources = cpu_bus_noop,
482 .init = cpu_bus_init,
483 .scan_bus = 0,
484};
485
486static void enable_dev(device_t dev)
487{
488 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800489 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Stefan Reinauer00636b02012-04-04 00:08:51 +0200490 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800491 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
Stefan Reinauer00636b02012-04-04 00:08:51 +0200492 dev->ops = &cpu_bus_ops;
493 }
494}
495
496struct chip_operations northbridge_intel_sandybridge_ops = {
Stefan Reinauer9ca1c0a2012-07-25 16:10:36 -0700497 CHIP_NAME("Intel i7 (SandyBridge/IvyBridge) integrated Northbridge")
Stefan Reinauer00636b02012-04-04 00:08:51 +0200498 .enable_dev = enable_dev,
499};