2 * This file is part of the coreboot project.
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
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.
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.
17 #include <console/console.h>
18 #include <arch/acpi.h>
22 #include <cpu/intel/haswell/haswell.h>
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <device/pci_ids.h>
29 #include <cpu/x86/smm.h>
30 #include <boot/tables.h>
35 static int get_pcie_bar(struct device
*dev
, unsigned int index
, u32
*base
,
44 pciexbar_reg
= pci_read_config32(dev
, index
);
46 if (!(pciexbar_reg
& (1 << 0)))
49 switch ((pciexbar_reg
>> 1) & 3) {
51 mask
= (1UL << 31) | (1 << 30) | (1 << 29) | (1 << 28);
52 *base
= pciexbar_reg
& mask
;
53 *len
= 256 * 1024 * 1024;
56 mask
= (1UL << 31) | (1 << 30) | (1 << 29) | (1 << 28);
58 *base
= pciexbar_reg
& mask
;
59 *len
= 128 * 1024 * 1024;
62 mask
= (1UL << 31) | (1 << 30) | (1 << 29) | (1 << 28);
63 mask
|= (1 << 27) | (1 << 26);
64 *base
= pciexbar_reg
& mask
;
65 *len
= 64 * 1024 * 1024;
72 static void pci_domain_set_resources(struct device
*dev
)
74 assign_resources(dev
->link_list
);
77 /* TODO We could determine how many PCIe busses we need in
78 * the bar. For now that number is hardcoded to a max of 64.
79 * See e7525/northbridge.c for an example.
81 static struct device_operations pci_domain_ops
= {
82 .read_resources
= pci_domain_read_resources
,
83 .set_resources
= pci_domain_set_resources
,
84 .enable_resources
= NULL
,
86 .scan_bus
= pci_domain_scan_bus
,
87 .write_acpi_tables
= northbridge_write_acpi_tables
,
90 static int get_bar(struct device
*dev
, unsigned int index
, u32
*base
, u32
*len
)
94 bar
= pci_read_config32(dev
, index
);
96 /* If not enabled don't report it. */
100 /* Knock down the enable bit. */
106 /* There are special BARs that actually are programmed in the MCHBAR. These
107 * Intel special features, but they do consume resources that need to be
109 static int get_bar_in_mchbar(struct device
*dev
, unsigned int index
,
114 bar
= MCHBAR32(index
);
116 /* If not enabled don't report it. */
120 /* Knock down the enable bit. */
126 struct fixed_mmio_descriptor
{
129 int (*get_resource
)(struct device
*dev
, unsigned int index
,
130 u32
*base
, u32
*size
);
131 const char *description
;
134 #define SIZE_KB(x) ((x)*1024)
135 struct fixed_mmio_descriptor mc_fixed_resources
[] = {
136 { PCIEXBAR
, SIZE_KB(0), get_pcie_bar
, "PCIEXBAR" },
137 { MCHBAR
, SIZE_KB(32), get_bar
, "MCHBAR" },
138 { DMIBAR
, SIZE_KB(4), get_bar
, "DMIBAR" },
139 { EPBAR
, SIZE_KB(4), get_bar
, "EPBAR" },
140 { 0x5420, SIZE_KB(4), get_bar_in_mchbar
, "GDXCBAR" },
141 { 0x5408, SIZE_KB(16), get_bar_in_mchbar
, "EDRAMBAR" },
146 * Add all known fixed MMIO ranges that hang off the host bridge/memory
149 static void mc_add_fixed_mmio_resources(struct device
*dev
)
153 for (i
= 0; i
< ARRAY_SIZE(mc_fixed_resources
); i
++) {
156 struct resource
*resource
;
159 size
= mc_fixed_resources
[i
].size
;
160 index
= mc_fixed_resources
[i
].index
;
161 if (!mc_fixed_resources
[i
].get_resource(dev
, index
,
165 resource
= new_resource(dev
, mc_fixed_resources
[i
].index
);
166 resource
->flags
= IORESOURCE_MEM
| IORESOURCE_FIXED
|
167 IORESOURCE_STORED
| IORESOURCE_RESERVE
|
169 resource
->base
= base
;
170 resource
->size
= size
;
171 printk(BIOS_DEBUG
, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
172 __func__
, mc_fixed_resources
[i
].description
, index
,
173 (unsigned long)base
, (unsigned long)(base
+ size
- 1));
179 * +--------------------------+ TOUUD
181 * +--------------------------+ 4GiB
182 * | PCI Address Space |
183 * +--------------------------+ TOLUD (also maps into MC address space)
185 * +--------------------------+ BDSM
187 * +--------------------------+ BGSM
189 * +--------------------------+ TSEGMB
191 * +--------------------------+ 0
193 * Some of the base registers above can be equal making the size of those
194 * regions 0. The reason is because the memory controller internally subtracts
195 * the base registers from each other to determine sizes of the regions. In
196 * other words, the memory map is in a fixed order no matter what.
203 const char *description
;
206 static void read_map_entry(struct device
*dev
, struct map_entry
*entry
,
212 /* All registers are on a 1MiB granularity. */
213 mask
= ((1ULL<<20)-1);
218 if (entry
->is_64_bit
) {
219 value
= pci_read_config32(dev
, entry
->reg
+ 4);
223 value
|= pci_read_config32(dev
, entry
->reg
);
232 #define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
235 .is_64_bit = is_64_, \
236 .is_limit = is_limit_, \
237 .description = desc_, \
240 #define MAP_ENTRY_BASE_64(reg_, desc_) \
241 MAP_ENTRY(reg_, 1, 0, desc_)
242 #define MAP_ENTRY_LIMIT_64(reg_, desc_) \
243 MAP_ENTRY(reg_, 1, 1, desc_)
244 #define MAP_ENTRY_BASE_32(reg_, desc_) \
245 MAP_ENTRY(reg_, 0, 0, desc_)
262 static struct map_entry memory_map
[NUM_MAP_ENTRIES
] = {
263 [TOM_REG
] = MAP_ENTRY_BASE_64(TOM
, "TOM"),
264 [TOUUD_REG
] = MAP_ENTRY_BASE_64(TOUUD
, "TOUUD"),
265 [MESEG_BASE_REG
] = MAP_ENTRY_BASE_64(MESEG_BASE
, "MESEG_BASE"),
266 [MESEG_LIMIT_REG
] = MAP_ENTRY_LIMIT_64(MESEG_LIMIT
, "MESEG_LIMIT"),
267 [REMAP_BASE_REG
] = MAP_ENTRY_BASE_64(REMAPBASE
, "REMAP_BASE"),
268 [REMAP_LIMIT_REG
] = MAP_ENTRY_LIMIT_64(REMAPLIMIT
, "REMAP_LIMIT"),
269 [TOLUD_REG
] = MAP_ENTRY_BASE_32(TOLUD
, "TOLUD"),
270 [BDSM_REG
] = MAP_ENTRY_BASE_32(BDSM
, "BDSM"),
271 [BGSM_REG
] = MAP_ENTRY_BASE_32(BGSM
, "BGSM"),
272 [TSEG_REG
] = MAP_ENTRY_BASE_32(TSEG
, "TESGMB"),
275 static void mc_read_map_entries(struct device
*dev
, uint64_t *values
)
278 for (i
= 0; i
< NUM_MAP_ENTRIES
; i
++) {
279 read_map_entry(dev
, &memory_map
[i
], &values
[i
]);
283 static void mc_report_map_entries(struct device
*dev
, uint64_t *values
)
286 for (i
= 0; i
< NUM_MAP_ENTRIES
; i
++) {
287 printk(BIOS_DEBUG
, "MC MAP: %s: 0x%llx\n",
288 memory_map
[i
].description
, values
[i
]);
290 /* One can validate the BDSM and BGSM against the GGC. */
291 printk(BIOS_DEBUG
, "MC MAP: GGC: 0x%x\n", pci_read_config16(dev
, GGC
));
294 static void mc_add_dram_resources(struct device
*dev
, int *resource_cnt
)
296 unsigned long base_k
, size_k
;
297 unsigned long touud_k
;
299 struct resource
*resource
;
300 uint64_t mc_values
[NUM_MAP_ENTRIES
];
302 /* Read in the MAP registers and report their values. */
303 mc_read_map_entries(dev
, &mc_values
[0]);
304 mc_report_map_entries(dev
, &mc_values
[0]);
307 * These are the host memory ranges that should be added:
308 * - 0 -> 0xa0000: cacheable
309 * - 0xc0000 -> TSEG : cacheable
310 * - TESG -> BGSM: cacheable with standard MTRRs and reserved
311 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
312 * - 4GiB -> TOUUD: cacheable
314 * The default SMRAM space is reserved so that the range doesn't
315 * have to be saved during S3 Resume. Once marked reserved the OS
316 * cannot use the memory. This is a bit of an odd place to reserve
317 * the region, but the CPU devices don't have dev_ops->read_resources()
320 * The range 0xa0000 -> 0xc0000 does not have any resources
321 * associated with it to handle legacy VGA memory. If this range
322 * is not omitted the mtrr code will setup the area as cacheable
323 * causing VGA access to not work.
325 * The TSEG region is mapped as cacheable so that one can perform
326 * SMRAM relocation faster. Once the SMRR is enabled the SMRR takes
327 * precedence over the existing MTRRs covering this region.
329 * It should be noted that cacheable entry types need to be added in
330 * order. The reason is that the current MTRR code assumes this and
331 * falls over itself if it isn't.
333 * The resource index starts low and should not meet or exceed
334 * PCI_BASE_ADDRESS_0.
336 index
= *resource_cnt
;
340 size_k
= (0xa0000 >> 10) - base_k
;
341 ram_resource(dev
, index
++, base_k
, size_k
);
343 /* 0xc0000 -> TSEG */
344 base_k
= 0xc0000 >> 10;
345 size_k
= (unsigned long)(mc_values
[TSEG_REG
] >> 10) - base_k
;
346 ram_resource(dev
, index
++, base_k
, size_k
);
349 resource
= new_resource(dev
, index
++);
350 resource
->base
= mc_values
[TSEG_REG
];
351 resource
->size
= mc_values
[BGSM_REG
] - resource
->base
;
352 resource
->flags
= IORESOURCE_MEM
| IORESOURCE_FIXED
|
353 IORESOURCE_STORED
| IORESOURCE_RESERVE
|
354 IORESOURCE_ASSIGNED
| IORESOURCE_CACHEABLE
;
357 resource
= new_resource(dev
, index
++);
358 resource
->base
= mc_values
[BGSM_REG
];
359 resource
->size
= mc_values
[TOLUD_REG
] - resource
->base
;
360 resource
->flags
= IORESOURCE_MEM
| IORESOURCE_FIXED
|
361 IORESOURCE_STORED
| IORESOURCE_RESERVE
|
365 base_k
= 4096 * 1024; /* 4GiB */
366 touud_k
= mc_values
[TOUUD_REG
] >> 10;
367 size_k
= touud_k
- base_k
;
368 if (touud_k
> base_k
)
369 ram_resource(dev
, index
++, base_k
, size_k
);
371 /* Reserve everything between A segment and 1MB:
373 * 0xa0000 - 0xbffff: legacy VGA
374 * 0xc0000 - 0xfffff: RAM
376 mmio_resource(dev
, index
++, (0xa0000 >> 10), (0xc0000 - 0xa0000) >> 10);
377 reserved_ram_resource(dev
, index
++, (0xc0000 >> 10),
378 (0x100000 - 0xc0000) >> 10);
379 #if IS_ENABLED(CONFIG_CHROMEOS_RAMOOPS)
380 reserved_ram_resource(dev
, index
++,
381 CONFIG_CHROMEOS_RAMOOPS_RAM_START
>> 10,
382 CONFIG_CHROMEOS_RAMOOPS_RAM_SIZE
>> 10);
384 *resource_cnt
= index
;
387 static void mc_read_resources(struct device
*dev
)
390 const bool vtd_capable
=
391 !(pci_read_config32(dev
, CAPID0_A
) & VTD_DISABLE
);
393 /* Read standard PCI resources. */
394 pci_dev_read_resources(dev
);
396 /* Add all fixed MMIO resources. */
397 mc_add_fixed_mmio_resources(dev
);
399 /* Add VT-d MMIO resources if capable */
401 mmio_resource(dev
, index
++, GFXVT_BASE_ADDRESS
/ KiB
,
402 GFXVT_BASE_SIZE
/ KiB
);
403 mmio_resource(dev
, index
++, VTVC0_BASE_ADDRESS
/ KiB
,
404 VTVC0_BASE_SIZE
/ KiB
);
407 /* Calculate and add DRAM resources. */
408 mc_add_dram_resources(dev
, &index
);
411 static void intel_set_subsystem(struct device
*dev
, unsigned vendor
,
414 if (!vendor
|| !device
) {
415 pci_write_config32(dev
, PCI_SUBSYSTEM_VENDOR_ID
,
416 pci_read_config32(dev
, PCI_VENDOR_ID
));
418 pci_write_config32(dev
, PCI_SUBSYSTEM_VENDOR_ID
,
419 ((device
& 0xffff) << 16) | (vendor
& 0xffff));
423 static void northbridge_init(struct device
*dev
)
425 u8 bios_reset_cpl
, pair
;
427 /* Enable Power Aware Interrupt Routing */
428 pair
= MCHBAR8(0x5418);
429 pair
&= ~0x7; /* Clear 2:0 */
430 pair
|= 0x4; /* Fixed Priority */
431 MCHBAR8(0x5418) = pair
;
434 * Set bits 0+1 of BIOS_RESET_CPL to indicate to the CPU
435 * that BIOS has initialized memory and power management
437 bios_reset_cpl
= MCHBAR8(BIOS_RESET_CPL
);
439 MCHBAR8(BIOS_RESET_CPL
) = bios_reset_cpl
;
440 printk(BIOS_DEBUG
, "Set BIOS_RESET_CPL\n");
442 /* Configure turbo power limits 1ms after reset complete bit */
444 set_power_limits(28);
446 /* Set here before graphics PM init */
447 MCHBAR32(0x5500) = 0x00100001;
450 static struct pci_operations intel_pci_ops
= {
451 .set_subsystem
= intel_set_subsystem
,
454 static struct device_operations mc_ops
= {
455 .read_resources
= mc_read_resources
,
456 .set_resources
= pci_dev_set_resources
,
457 .enable_resources
= pci_dev_enable_resources
,
458 .init
= northbridge_init
,
459 .acpi_fill_ssdt_generator
= generate_cpu_entries
,
461 .ops_pci
= &intel_pci_ops
,
464 static const struct pci_driver mc_driver_hsw_mobile __pci_driver
= {
466 .vendor
= PCI_VENDOR_ID_INTEL
,
467 .device
= PCI_DEVICE_ID_HSW_MOBILE
,
470 static const struct pci_driver mc_driver_hsw_ult __pci_driver
= {
472 .vendor
= PCI_VENDOR_ID_INTEL
,
473 .device
= PCI_DEVICE_ID_HSW_ULT
,
476 static void cpu_bus_init(struct device
*dev
)
478 bsp_init_and_start_aps(dev
->link_list
);
481 static struct device_operations cpu_bus_ops
= {
482 .read_resources
= DEVICE_NOOP
,
483 .set_resources
= DEVICE_NOOP
,
484 .enable_resources
= DEVICE_NOOP
,
485 .init
= cpu_bus_init
,
489 static void enable_dev(struct device
*dev
)
491 /* Set the operations if it is a special bus type */
492 if (dev
->path
.type
== DEVICE_PATH_DOMAIN
) {
493 dev
->ops
= &pci_domain_ops
;
494 } else if (dev
->path
.type
== DEVICE_PATH_CPU_CLUSTER
) {
495 dev
->ops
= &cpu_bus_ops
;
499 struct chip_operations northbridge_intel_haswell_ops
= {
500 CHIP_NAME("Intel i7 (Haswell) integrated Northbridge")
501 .enable_dev
= enable_dev
,