blob: 7d7153e73c8c18267e45f5c89f50ef394e31c9f0 [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>
36#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
62int add_northbridge_resources(struct lb_memory *mem)
63{
64 lb_add_memory_range(mem, LB_MEM_RESERVED,
65 legacy_hole_base_k * 1024, legacy_hole_size_k * 1024);
66
67#if CONFIG_CHROMEOS_RAMOOPS
68 lb_add_memory_range(mem, LB_MEM_RESERVED,
69 CONFIG_CHROMEOS_RAMOOPS_RAM_START,
70 CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE);
71#endif
72
73 /* Required for SandyBridge sighting 3715511 */
74 lb_add_memory_range(mem, LB_MEM_RESERVED, 0x20000000, 0x00200000);
75 lb_add_memory_range(mem, LB_MEM_RESERVED, 0x40000000, 0x00200000);
76
77 return 0;
78}
79
Stefan Reinauer1244f4b2012-05-10 11:31:40 -070080void cbmem_post_handling(void);
81void 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
Patrick Georgie1667822012-05-05 15:29:32 +0200152#if CONFIG_WRITE_HIGH_TABLES
Stefan Reinauer00636b02012-04-04 00:08:51 +0200153#include <cbmem.h>
154#endif
155
156static void pci_domain_set_resources(device_t dev)
157{
158 uint64_t tom, me_base, touud;
159 uint32_t tseg_base, uma_size, tolud;
160 uint16_t ggc;
161 unsigned long long tomk;
162
163 /* Total Memory 2GB example:
164 *
165 * 00000000 0000MB-1992MB 1992MB RAM (writeback)
166 * 7c800000 1992MB-2000MB 8MB TSEG (SMRR)
167 * 7d000000 2000MB-2002MB 2MB GFX GTT (uncached)
168 * 7d200000 2002MB-2034MB 32MB GFX UMA (uncached)
169 * 7f200000 2034MB TOLUD
170 * 7f800000 2040MB MEBASE
171 * 7f800000 2040MB-2048MB 8MB ME UMA (uncached)
172 * 80000000 2048MB TOM
173 * 100000000 4096MB-4102MB 6MB RAM (writeback)
174 *
175 * Total Memory 4GB example:
176 *
177 * 00000000 0000MB-2768MB 2768MB RAM (writeback)
178 * ad000000 2768MB-2776MB 8MB TSEG (SMRR)
179 * ad800000 2776MB-2778MB 2MB GFX GTT (uncached)
180 * ada00000 2778MB-2810MB 32MB GFX UMA (uncached)
181 * afa00000 2810MB TOLUD
182 * ff800000 4088MB MEBASE
183 * ff800000 4088MB-4096MB 8MB ME UMA (uncached)
184 * 100000000 4096MB TOM
185 * 100000000 4096MB-5374MB 1278MB RAM (writeback)
186 * 14fe00000 5368MB TOUUD
187 */
188
189 /* Top of Upper Usable DRAM, including remap */
190 touud = pci_read_config32(dev, TOUUD+4);
191 touud <<= 32;
192 touud |= pci_read_config32(dev, TOUUD);
193
194 /* Top of Lower Usable DRAM */
195 tolud = pci_read_config32(dev, TOLUD);
196
197 /* Top of Memory - does not account for any UMA */
198 tom = pci_read_config32(dev, 0xa4);
199 tom <<= 32;
200 tom |= pci_read_config32(dev, 0xa0);
201
202 printk(BIOS_DEBUG, "TOUUD 0x%llx TOLUD 0x%08x TOM 0x%llx\n",
203 touud, tolud, tom);
204
205 /* ME UMA needs excluding if total memory <4GB */
206 me_base = pci_read_config32(dev, 0x74);
207 me_base <<= 32;
208 me_base |= pci_read_config32(dev, 0x70);
209
210 printk(BIOS_DEBUG, "MEBASE 0x%llx\n", me_base);
211
212 tomk = tolud >> 10;
213 if (me_base == tolud) {
214 /* ME is from MEBASE-TOM */
215 uma_size = (tom - me_base) >> 10;
216 /* Increment TOLUD to account for ME as RAM */
217 tolud += uma_size << 10;
218 /* UMA starts at old TOLUD */
219 uma_memory_base = tomk * 1024ULL;
220 uma_memory_size = uma_size * 1024ULL;
221 printk(BIOS_DEBUG, "ME UMA base 0x%llx size %uM\n",
222 me_base, uma_size >> 10);
223 }
224
225 /* Graphics memory comes next */
226 ggc = pci_read_config16(dev, GGC);
227 if (!(ggc & 2)) {
228 printk(BIOS_DEBUG, "IGD decoded, subtracting ");
229
230 /* Graphics memory */
231 uma_size = ((ggc >> 3) & 0x1f) * 32 * 1024ULL;
232 printk(BIOS_DEBUG, "%uM UMA", uma_size >> 10);
233 tomk -= uma_size;
234 uma_memory_base = tomk * 1024ULL;
235 uma_memory_size += uma_size * 1024ULL;
236
237 /* GTT Graphics Stolen Memory Size (GGMS) */
238 uma_size = ((ggc >> 8) & 0x3) * 1024ULL;
239 tomk -= uma_size;
240 uma_memory_base = tomk * 1024ULL;
241 uma_memory_size += uma_size * 1024ULL;
242 printk(BIOS_DEBUG, " and %uM GTT\n", uma_size >> 10);
243 }
244
245 /* Calculate TSEG size from its base which must be below GTT */
246 tseg_base = pci_read_config32(dev, 0xb8);
247 uma_size = (uma_memory_base - tseg_base) >> 10;
248 tomk -= uma_size;
249 uma_memory_base = tomk * 1024ULL;
250 uma_memory_size += uma_size * 1024ULL;
251 printk(BIOS_DEBUG, "TSEG base 0x%08x size %uM\n",
252 tseg_base, uma_size >> 10);
253
254 printk(BIOS_INFO, "Available memory below 4GB: %lluM\n", tomk >> 10);
255
256 /* Report the memory regions */
257 ram_resource(dev, 3, 0, legacy_hole_base_k);
258 ram_resource(dev, 4, legacy_hole_base_k + legacy_hole_size_k,
259 (tomk - (legacy_hole_base_k + legacy_hole_size_k)));
260
261 /*
262 * If >= 4GB installed then memory from TOLUD to 4GB
263 * is remapped above TOM, TOUUD will account for both
264 */
265 touud >>= 10; /* Convert to KB */
266 if (touud > 4096 * 1024) {
267 ram_resource(dev, 5, 4096 * 1024, touud - (4096 * 1024));
268 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n",
269 (touud >> 10) - 4096);
270 }
271
272 add_fixed_resources(dev, 6);
273
274 assign_resources(dev->link_list);
275
Patrick Georgie1667822012-05-05 15:29:32 +0200276#if CONFIG_WRITE_HIGH_TABLES
Stefan Reinauer00636b02012-04-04 00:08:51 +0200277 /* Leave some space for ACPI, PIRQ and MP tables */
278 high_tables_base = (tomk * 1024) - HIGH_MEMORY_SIZE;
279 high_tables_size = HIGH_MEMORY_SIZE;
280#endif
281}
282
283 /* TODO We could determine how many PCIe busses we need in
284 * the bar. For now that number is hardcoded to a max of 64.
285 * See e7525/northbridge.c for an example.
286 */
287static struct device_operations pci_domain_ops = {
288 .read_resources = pci_domain_read_resources,
289 .set_resources = pci_domain_set_resources,
290 .enable_resources = NULL,
291 .init = NULL,
292 .scan_bus = pci_domain_scan_bus,
293#if CONFIG_MMCONF_SUPPORT_DEFAULT
294 .ops_pci_bus = &pci_ops_mmconf,
295#else
296 .ops_pci_bus = &pci_cf8_conf1,
297#endif
298};
299
300static void mc_read_resources(device_t dev)
301{
302 struct resource *resource;
303
304 pci_dev_read_resources(dev);
305
306 /* So, this is one of the big mysteries in the coreboot resource
307 * allocator. This resource should make sure that the address space
308 * of the PCIe memory mapped config space bar. But it does not.
309 */
310
311 /* We use 0xcf as an unused index for our PCIe bar so that we find it again */
312 resource = new_resource(dev, 0xcf);
313 resource->base = DEFAULT_PCIEXBAR;
314 resource->size = 64 * 1024 * 1024; /* 64MB hard coded PCIe config space */
315 resource->flags =
316 IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
317 IORESOURCE_ASSIGNED;
318 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
319 (unsigned long)(resource->base), (unsigned long)(resource->base + resource->size));
320}
321
322static void mc_set_resources(device_t dev)
323{
324 struct resource *resource;
325
326 /* Report the PCIe BAR */
327 resource = find_resource(dev, 0xcf);
328 if (resource) {
329 report_resource_stored(dev, resource, "<mmconfig>");
330 }
331
332 /* And call the normal set_resources */
333 pci_dev_set_resources(dev);
334}
335
336static void intel_set_subsystem(device_t dev, unsigned vendor, unsigned device)
337{
338 if (!vendor || !device) {
339 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
340 pci_read_config32(dev, PCI_VENDOR_ID));
341 } else {
342 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
343 ((device & 0xffff) << 16) | (vendor & 0xffff));
344 }
345}
346
347static void northbridge_dmi_init(struct device *dev)
348{
349 u32 reg32;
350
351 /* Clear error status bits */
352 DMIBAR32(0x1c4) = 0xffffffff;
353 DMIBAR32(0x1d0) = 0xffffffff;
354
355 /* Steps prior to DMI ASPM */
Vincent Palatin0ff99b72012-03-28 16:10:29 -0700356 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_SNB) {
357 reg32 = DMIBAR32(0x250);
358 reg32 &= ~((1 << 22)|(1 << 20));
359 reg32 |= (1 << 21);
360 DMIBAR32(0x250) = reg32;
361 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200362
363 reg32 = DMIBAR32(0x238);
364 reg32 |= (1 << 29);
365 DMIBAR32(0x238) = reg32;
366
367 if (bridge_silicon_revision() >= SNB_STEP_D0) {
368 reg32 = DMIBAR32(0x1f8);
369 reg32 |= (1 << 16);
370 DMIBAR32(0x1f8) = reg32;
371 } else if (bridge_silicon_revision() >= SNB_STEP_D1) {
372 reg32 = DMIBAR32(0x1f8);
373 reg32 &= ~(1 << 26);
374 reg32 |= (1 << 16);
375 DMIBAR32(0x1f8) = reg32;
376
377 reg32 = DMIBAR32(0x1fc);
378 reg32 |= (1 << 12) | (1 << 23);
379 DMIBAR32(0x1fc) = reg32;
380 }
381
382 /* Enable ASPM on SNB link, should happen before PCH link */
Vincent Palatin0ff99b72012-03-28 16:10:29 -0700383 if ((bridge_silicon_revision() & BASE_REV_MASK) == BASE_REV_SNB) {
384 reg32 = DMIBAR32(0xd04);
385 reg32 |= (1 << 4);
386 DMIBAR32(0xd04) = reg32;
387 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200388
389 reg32 = DMIBAR32(0x88);
390 reg32 |= (1 << 1) | (1 << 0);
391 DMIBAR32(0x88) = reg32;
392}
393
394static void northbridge_init(struct device *dev)
395{
396 u8 bios_reset_cpl;
397
398 northbridge_dmi_init(dev);
399
400 /*
401 * Set bit 0 of BIOS_RESET_CPL to indicate to the CPU
402 * that BIOS has initialized memory and power management
403 */
404 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
405 bios_reset_cpl |= 1;
406 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
407 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
408
409 /* Configure turbo power limits 1ms after reset complete bit */
410 mdelay(1);
411 set_power_limits(28);
412
413 /* 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
451static const struct pci_driver mc_driver __pci_driver = {
452 .ops = &mc_ops,
453 .vendor = PCI_VENDOR_ID_INTEL,
454 .device = 0x0104, /* Sandy bridge */
455};
456
457static const struct pci_driver mc_driver_1 __pci_driver = {
458 .ops = &mc_ops,
459 .vendor = PCI_VENDOR_ID_INTEL,
460 .device = 0x0154, /* Ivy bridge */
461};
462
463static void cpu_bus_init(device_t dev)
464{
465 initialize_cpus(dev->link_list);
466}
467
468static void cpu_bus_noop(device_t dev)
469{
470}
471
472static struct device_operations cpu_bus_ops = {
473 .read_resources = cpu_bus_noop,
474 .set_resources = cpu_bus_noop,
475 .enable_resources = cpu_bus_noop,
476 .init = cpu_bus_init,
477 .scan_bus = 0,
478};
479
480static void enable_dev(device_t dev)
481{
482 /* Set the operations if it is a special bus type */
483 if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
484 dev->ops = &pci_domain_ops;
485 } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
486 dev->ops = &cpu_bus_ops;
487 }
488}
489
490struct chip_operations northbridge_intel_sandybridge_ops = {
491 CHIP_NAME("Intel i7 (Sandybridge) integrated Northbridge")
492 .enable_dev = enable_dev,
493};