blob: 756aacfbb4f0dd7cdf08bbcd658a47a6a6520598 [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
Stefan Reinauer00636b02012-04-04 00:08:51 +0200121static void add_fixed_resources(struct device *dev, int index)
122{
123 struct resource *resource;
124 u32 pcie_config_base, pcie_config_size;
125
126 printk(BIOS_DEBUG, "Adding UMA memory area base=0x%llx "
127 "size=0x%llx\n", uma_memory_base, uma_memory_size);
128 resource = new_resource(dev, index);
129 resource->base = (resource_t) uma_memory_base;
130 resource->size = (resource_t) uma_memory_size;
131 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
132 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
133
134 /* Clear these values here so they don't get used by MTRR code */
135 uma_memory_base = 0;
136 uma_memory_size = 0;
137
138 if (get_pcie_bar(&pcie_config_base, &pcie_config_size)) {
139 printk(BIOS_DEBUG, "Adding PCIe config bar base=0x%08x "
140 "size=0x%x\n", pcie_config_base, pcie_config_size);
141 resource = new_resource(dev, index+1);
142 resource->base = (resource_t) pcie_config_base;
143 resource->size = (resource_t) pcie_config_size;
144 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
145 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
146 }
147}
148
Stefan Reinauer00636b02012-04-04 00:08:51 +0200149static void pci_domain_set_resources(device_t dev)
150{
151 uint64_t tom, me_base, touud;
152 uint32_t tseg_base, uma_size, tolud;
153 uint16_t ggc;
154 unsigned long long tomk;
155
156 /* Total Memory 2GB example:
157 *
158 * 00000000 0000MB-1992MB 1992MB RAM (writeback)
159 * 7c800000 1992MB-2000MB 8MB TSEG (SMRR)
160 * 7d000000 2000MB-2002MB 2MB GFX GTT (uncached)
161 * 7d200000 2002MB-2034MB 32MB GFX UMA (uncached)
162 * 7f200000 2034MB TOLUD
163 * 7f800000 2040MB MEBASE
164 * 7f800000 2040MB-2048MB 8MB ME UMA (uncached)
165 * 80000000 2048MB TOM
166 * 100000000 4096MB-4102MB 6MB RAM (writeback)
167 *
168 * Total Memory 4GB example:
169 *
170 * 00000000 0000MB-2768MB 2768MB RAM (writeback)
171 * ad000000 2768MB-2776MB 8MB TSEG (SMRR)
172 * ad800000 2776MB-2778MB 2MB GFX GTT (uncached)
173 * ada00000 2778MB-2810MB 32MB GFX UMA (uncached)
174 * afa00000 2810MB TOLUD
175 * ff800000 4088MB MEBASE
176 * ff800000 4088MB-4096MB 8MB ME UMA (uncached)
177 * 100000000 4096MB TOM
178 * 100000000 4096MB-5374MB 1278MB RAM (writeback)
179 * 14fe00000 5368MB TOUUD
180 */
181
182 /* Top of Upper Usable DRAM, including remap */
183 touud = pci_read_config32(dev, TOUUD+4);
184 touud <<= 32;
185 touud |= pci_read_config32(dev, TOUUD);
186
187 /* Top of Lower Usable DRAM */
188 tolud = pci_read_config32(dev, TOLUD);
189
190 /* Top of Memory - does not account for any UMA */
191 tom = pci_read_config32(dev, 0xa4);
192 tom <<= 32;
193 tom |= pci_read_config32(dev, 0xa0);
194
195 printk(BIOS_DEBUG, "TOUUD 0x%llx TOLUD 0x%08x TOM 0x%llx\n",
196 touud, tolud, tom);
197
198 /* ME UMA needs excluding if total memory <4GB */
199 me_base = pci_read_config32(dev, 0x74);
200 me_base <<= 32;
201 me_base |= pci_read_config32(dev, 0x70);
202
203 printk(BIOS_DEBUG, "MEBASE 0x%llx\n", me_base);
204
205 tomk = tolud >> 10;
206 if (me_base == tolud) {
207 /* ME is from MEBASE-TOM */
208 uma_size = (tom - me_base) >> 10;
209 /* Increment TOLUD to account for ME as RAM */
210 tolud += uma_size << 10;
211 /* UMA starts at old TOLUD */
212 uma_memory_base = tomk * 1024ULL;
213 uma_memory_size = uma_size * 1024ULL;
214 printk(BIOS_DEBUG, "ME UMA base 0x%llx size %uM\n",
215 me_base, uma_size >> 10);
216 }
217
218 /* Graphics memory comes next */
219 ggc = pci_read_config16(dev, GGC);
220 if (!(ggc & 2)) {
221 printk(BIOS_DEBUG, "IGD decoded, subtracting ");
222
223 /* Graphics memory */
224 uma_size = ((ggc >> 3) & 0x1f) * 32 * 1024ULL;
225 printk(BIOS_DEBUG, "%uM UMA", uma_size >> 10);
226 tomk -= uma_size;
227 uma_memory_base = tomk * 1024ULL;
228 uma_memory_size += uma_size * 1024ULL;
229
230 /* GTT Graphics Stolen Memory Size (GGMS) */
231 uma_size = ((ggc >> 8) & 0x3) * 1024ULL;
232 tomk -= uma_size;
233 uma_memory_base = tomk * 1024ULL;
234 uma_memory_size += uma_size * 1024ULL;
235 printk(BIOS_DEBUG, " and %uM GTT\n", uma_size >> 10);
236 }
237
238 /* Calculate TSEG size from its base which must be below GTT */
239 tseg_base = pci_read_config32(dev, 0xb8);
240 uma_size = (uma_memory_base - tseg_base) >> 10;
241 tomk -= uma_size;
242 uma_memory_base = tomk * 1024ULL;
243 uma_memory_size += uma_size * 1024ULL;
244 printk(BIOS_DEBUG, "TSEG base 0x%08x size %uM\n",
245 tseg_base, uma_size >> 10);
246
247 printk(BIOS_INFO, "Available memory below 4GB: %lluM\n", tomk >> 10);
248
249 /* Report the memory regions */
250 ram_resource(dev, 3, 0, legacy_hole_base_k);
251 ram_resource(dev, 4, legacy_hole_base_k + legacy_hole_size_k,
252 (tomk - (legacy_hole_base_k + legacy_hole_size_k)));
253
254 /*
255 * If >= 4GB installed then memory from TOLUD to 4GB
256 * is remapped above TOM, TOUUD will account for both
257 */
258 touud >>= 10; /* Convert to KB */
259 if (touud > 4096 * 1024) {
260 ram_resource(dev, 5, 4096 * 1024, touud - (4096 * 1024));
261 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n",
262 (touud >> 10) - 4096);
263 }
264
265 add_fixed_resources(dev, 6);
266
267 assign_resources(dev->link_list);
268
Patrick Georgie1667822012-05-05 15:29:32 +0200269#if CONFIG_WRITE_HIGH_TABLES
Stefan Reinauer00636b02012-04-04 00:08:51 +0200270 /* Leave some space for ACPI, PIRQ and MP tables */
271 high_tables_base = (tomk * 1024) - HIGH_MEMORY_SIZE;
272 high_tables_size = HIGH_MEMORY_SIZE;
273#endif
274}
275
276 /* TODO We could determine how many PCIe busses we need in
277 * the bar. For now that number is hardcoded to a max of 64.
278 * See e7525/northbridge.c for an example.
279 */
280static struct device_operations pci_domain_ops = {
281 .read_resources = pci_domain_read_resources,
282 .set_resources = pci_domain_set_resources,
283 .enable_resources = NULL,
284 .init = NULL,
285 .scan_bus = pci_domain_scan_bus,
286#if CONFIG_MMCONF_SUPPORT_DEFAULT
287 .ops_pci_bus = &pci_ops_mmconf,
288#else
289 .ops_pci_bus = &pci_cf8_conf1,
290#endif
291};
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;
390
391 northbridge_dmi_init(dev);
392
393 /*
394 * Set bit 0 of BIOS_RESET_CPL to indicate to the CPU
395 * that BIOS has initialized memory and power management
396 */
397 bios_reset_cpl = MCHBAR8(BIOS_RESET_CPL);
398 bios_reset_cpl |= 1;
399 MCHBAR8(BIOS_RESET_CPL) = bios_reset_cpl;
400 printk(BIOS_DEBUG, "Set BIOS_RESET_CPL\n");
401
402 /* Configure turbo power limits 1ms after reset complete bit */
403 mdelay(1);
404 set_power_limits(28);
405
406 /* Set here before graphics PM init */
407 MCHBAR32(0x5500) = 0x00100001;
408}
409
410static void northbridge_enable(device_t dev)
411{
412#if CONFIG_HAVE_ACPI_RESUME
413 switch (pci_read_config32(dev, SKPAD)) {
414 case 0xcafebabe:
415 printk(BIOS_DEBUG, "Normal boot.\n");
416 acpi_slp_type=0;
417 break;
418 case 0xcafed00d:
419 printk(BIOS_DEBUG, "S3 Resume.\n");
420 acpi_slp_type=3;
421 break;
422 default:
423 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
424 acpi_slp_type=0;
425 break;
426 }
427#endif
428}
429
430static struct pci_operations intel_pci_ops = {
431 .set_subsystem = intel_set_subsystem,
432};
433
434static struct device_operations mc_ops = {
435 .read_resources = mc_read_resources,
436 .set_resources = mc_set_resources,
437 .enable_resources = pci_dev_enable_resources,
438 .init = northbridge_init,
439 .enable = northbridge_enable,
440 .scan_bus = 0,
441 .ops_pci = &intel_pci_ops,
442};
443
Walter Murphy496f4a02012-04-23 11:08:03 -0700444static const struct pci_driver mc_driver_0100 __pci_driver = {
445 .ops = &mc_ops,
446 .vendor = PCI_VENDOR_ID_INTEL,
447 .device = 0x0100,
448};
449
Stefan Reinauer00636b02012-04-04 00:08:51 +0200450static const struct pci_driver mc_driver __pci_driver = {
451 .ops = &mc_ops,
452 .vendor = PCI_VENDOR_ID_INTEL,
453 .device = 0x0104, /* Sandy bridge */
454};
455
456static const struct pci_driver mc_driver_1 __pci_driver = {
457 .ops = &mc_ops,
458 .vendor = PCI_VENDOR_ID_INTEL,
459 .device = 0x0154, /* Ivy bridge */
460};
461
462static void cpu_bus_init(device_t dev)
463{
464 initialize_cpus(dev->link_list);
465}
466
467static void cpu_bus_noop(device_t dev)
468{
469}
470
471static struct device_operations cpu_bus_ops = {
472 .read_resources = cpu_bus_noop,
473 .set_resources = cpu_bus_noop,
474 .enable_resources = cpu_bus_noop,
475 .init = cpu_bus_init,
476 .scan_bus = 0,
477};
478
479static void enable_dev(device_t dev)
480{
481 /* Set the operations if it is a special bus type */
482 if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
483 dev->ops = &pci_domain_ops;
484 } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
485 dev->ops = &cpu_bus_ops;
486 }
487}
488
489struct chip_operations northbridge_intel_sandybridge_ops = {
490 CHIP_NAME("Intel i7 (Sandybridge) integrated Northbridge")
491 .enable_dev = enable_dev,
492};