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