2 * This file is part of the coreboot project.
4 * Copyright (C) 2007-2009 coresystems GmbH
5 * Copyright (C) 2014 Google Inc.
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.
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
21 #include <console/console.h>
22 #include <arch/acpi.h>
26 #include <device/device.h>
27 #include <device/pci.h>
28 #include <device/pci_ids.h>
32 #include <vendorcode/google/chromeos/chromeos.h>
34 #include <soc/iomap.h>
35 #include <soc/pci_devs.h>
36 #include <soc/ramstage.h>
37 #include <soc/systemagent.h>
39 u8
systemagent_revision(void)
41 return pci_read_config8(SA_DEV_ROOT
, PCI_REVISION_ID
);
44 static int get_pcie_bar(device_t dev
, unsigned int index
, u32
*base
, u32
*len
)
51 pciexbar_reg
= pci_read_config32(dev
, index
);
53 if (!(pciexbar_reg
& (1 << 0)))
56 switch ((pciexbar_reg
>> 1) & 3) {
58 *base
= pciexbar_reg
& ((1 << 31)|(1 << 30)|(1 << 29)|
60 *len
= 256 * 1024 * 1024;
63 *base
= pciexbar_reg
& ((1 << 31)|(1 << 30)|(1 << 29)|
65 *len
= 128 * 1024 * 1024;
68 *base
= pciexbar_reg
& ((1 << 31)|(1 << 30)|(1 << 29)|
69 (1 << 28)|(1 << 27)|(1 << 26));
70 *len
= 64 * 1024 * 1024;
77 static int get_bar(device_t dev
, unsigned int index
, u32
*base
, u32
*len
)
81 bar
= pci_read_config32(dev
, index
);
83 /* If not enabled don't report it. */
87 /* Knock down the enable bit. */
93 /* There are special BARs that actually are programmed in the MCHBAR. These
94 * Intel special features, but they do consume resources that need to be
96 static int get_bar_in_mchbar(device_t dev
, unsigned int index
, u32
*base
,
101 bar
= MCHBAR32(index
);
103 /* If not enabled don't report it. */
107 /* Knock down the enable bit. */
113 struct fixed_mmio_descriptor
{
116 int (*get_resource
)(device_t dev
, unsigned int index
,
117 u32
*base
, u32
*size
);
118 const char *description
;
121 struct fixed_mmio_descriptor mc_fixed_resources
[] = {
122 { PCIEXBAR
, 0, get_pcie_bar
, "PCIEXBAR" },
123 { MCHBAR
, MCH_BASE_SIZE
, get_bar
, "MCHBAR" },
124 { DMIBAR
, DMI_BASE_SIZE
, get_bar
, "DMIBAR" },
125 { EPBAR
, EP_BASE_SIZE
, get_bar
, "EPBAR" },
126 { GDXCBAR
, GDXC_BASE_SIZE
, get_bar_in_mchbar
, "GDXCBAR" },
127 { EDRAMBAR
, EDRAM_BASE_SIZE
, get_bar_in_mchbar
, "EDRAMBAR" },
131 * Add all known fixed MMIO ranges that hang off the host bridge/memory
134 static void mc_add_fixed_mmio_resources(device_t dev
)
138 for (i
= 0; i
< ARRAY_SIZE(mc_fixed_resources
); i
++) {
141 struct resource
*resource
;
144 size
= mc_fixed_resources
[i
].size
;
145 index
= mc_fixed_resources
[i
].index
;
146 if (!mc_fixed_resources
[i
].get_resource(dev
, index
,
150 resource
= new_resource(dev
, mc_fixed_resources
[i
].index
);
151 resource
->flags
= IORESOURCE_MEM
| IORESOURCE_FIXED
|
152 IORESOURCE_STORED
| IORESOURCE_RESERVE
|
154 resource
->base
= base
;
155 resource
->size
= size
;
156 printk(BIOS_DEBUG
, "%s: Adding %s @ %x 0x%08lx-0x%08lx.\n",
157 __func__
, mc_fixed_resources
[i
].description
, index
,
158 (unsigned long)base
, (unsigned long)(base
+ size
- 1));
164 * +--------------------------+ TOUUD
166 * +--------------------------+ 4GiB
167 * | PCI Address Space |
168 * +--------------------------+ TOLUD (also maps into MC address space)
170 * +--------------------------+ BDSM
172 * +--------------------------+ BGSM
174 * +--------------------------+ TSEGMB
176 * +--------------------------+ 0
178 * Some of the base registers above can be equal making the size of those
179 * regions 0. The reason is because the memory controller internally subtracts
180 * the base registers from each other to determine sizes of the regions. In
181 * other words, the memory map is in a fixed order no matter what.
188 const char *description
;
191 static void read_map_entry(device_t dev
, struct map_entry
*entry
,
197 /* All registers are on a 1MiB granularity. */
198 mask
= ((1ULL<<20)-1);
203 if (entry
->is_64_bit
) {
204 value
= pci_read_config32(dev
, entry
->reg
+ 4);
208 value
|= pci_read_config32(dev
, entry
->reg
);
217 #define MAP_ENTRY(reg_, is_64_, is_limit_, desc_) \
220 .is_64_bit = is_64_, \
221 .is_limit = is_limit_, \
222 .description = desc_, \
225 #define MAP_ENTRY_BASE_64(reg_, desc_) \
226 MAP_ENTRY(reg_, 1, 0, desc_)
227 #define MAP_ENTRY_LIMIT_64(reg_, desc_) \
228 MAP_ENTRY(reg_, 1, 1, desc_)
229 #define MAP_ENTRY_BASE_32(reg_, desc_) \
230 MAP_ENTRY(reg_, 0, 0, desc_)
247 static struct map_entry memory_map
[NUM_MAP_ENTRIES
] = {
248 [TOM_REG
] = MAP_ENTRY_BASE_64(TOM
, "TOM"),
249 [TOUUD_REG
] = MAP_ENTRY_BASE_64(TOUUD
, "TOUUD"),
250 [MESEG_BASE_REG
] = MAP_ENTRY_BASE_64(MESEG_BASE
, "MESEG_BASE"),
251 [MESEG_LIMIT_REG
] = MAP_ENTRY_LIMIT_64(MESEG_LIMIT
, "MESEG_LIMIT"),
252 [REMAP_BASE_REG
] = MAP_ENTRY_BASE_64(REMAPBASE
, "REMAP_BASE"),
253 [REMAP_LIMIT_REG
] = MAP_ENTRY_LIMIT_64(REMAPLIMIT
, "REMAP_LIMIT"),
254 [TOLUD_REG
] = MAP_ENTRY_BASE_32(TOLUD
, "TOLUD"),
255 [BDSM_REG
] = MAP_ENTRY_BASE_32(BDSM
, "BDSM"),
256 [BGSM_REG
] = MAP_ENTRY_BASE_32(BGSM
, "BGSM"),
257 [TSEG_REG
] = MAP_ENTRY_BASE_32(TSEG
, "TESGMB"),
260 static void mc_read_map_entries(device_t dev
, uint64_t *values
)
263 for (i
= 0; i
< NUM_MAP_ENTRIES
; i
++) {
264 read_map_entry(dev
, &memory_map
[i
], &values
[i
]);
268 static void mc_report_map_entries(device_t dev
, uint64_t *values
)
271 for (i
= 0; i
< NUM_MAP_ENTRIES
; i
++) {
272 printk(BIOS_DEBUG
, "MC MAP: %s: 0x%llx\n",
273 memory_map
[i
].description
, values
[i
]);
275 /* One can validate the BDSM and BGSM against the GGC. */
276 printk(BIOS_DEBUG
, "MC MAP: GGC: 0x%x\n", pci_read_config16(dev
, GGC
));
279 static void mc_add_dram_resources(device_t dev
)
281 unsigned long base_k
, size_k
;
282 unsigned long touud_k
;
284 struct resource
*resource
;
285 uint64_t mc_values
[NUM_MAP_ENTRIES
];
286 unsigned long dpr_size
= 0;
289 /* Read in the MAP registers and report their values. */
290 mc_read_map_entries(dev
, &mc_values
[0]);
291 mc_report_map_entries(dev
, &mc_values
[0]);
294 * DMA Protected Range can be reserved below TSEG for PCODE patch
295 * or TXT/BootGuard related data. Rather than report a base address
296 * the DPR register reports the TOP of the region, which is the same
297 * as TSEG base. The region size is reported in MiB in bits 11:4.
299 dpr_reg
= pci_read_config32(SA_DEV_ROOT
, DPR
);
300 if (dpr_reg
& DPR_EPM
) {
301 dpr_size
= (dpr_reg
& DPR_SIZE_MASK
) << 16;
302 printk(BIOS_INFO
, "DPR SIZE: 0x%lx\n", dpr_size
);
306 * These are the host memory ranges that should be added:
307 * - 0 -> 0xa0000: cacheable
308 * - 0xc0000 -> TSEG : cacheable
309 * - TESG -> BGSM: cacheable with standard MTRRs and reserved
310 * - BGSM -> TOLUD: not cacheable with standard MTRRs and reserved
311 * - 4GiB -> TOUUD: cacheable
313 * The default SMRAM space is reserved so that the range doesn't
314 * have to be saved during S3 Resume. Once marked reserved the OS
315 * cannot use the memory. This is a bit of an odd place to reserve
316 * the region, but the CPU devices don't have dev_ops->read_resources()
319 * The range 0xa0000 -> 0xc0000 does not have any resources
320 * associated with it to handle legacy VGA memory. If this range
321 * is not omitted the mtrr code will setup the area as cacheable
322 * causing VGA access to not work.
324 * The TSEG region is mapped as cacheable so that one can perform
325 * SMRAM relocation faster. Once the SMRR is enabled the SMRR takes
326 * precedence over the existing MTRRs covering this region.
328 * It should be noted that cacheable entry types need to be added in
329 * order. The reason is that the current MTRR code assumes this and
330 * falls over itself if it isn't.
332 * The resource index starts low and should not meet or exceed
333 * PCI_BASE_ADDRESS_0.
339 size_k
= (0xa0000 >> 10) - base_k
;
340 ram_resource(dev
, index
++, base_k
, size_k
);
342 /* 0xc0000 -> TSEG - DPR */
343 base_k
= 0xc0000 >> 10;
344 size_k
= (unsigned long)(mc_values
[TSEG_REG
] >> 10) - base_k
;
345 size_k
-= dpr_size
>> 10;
346 ram_resource(dev
, index
++, base_k
, size_k
);
348 /* TSEG - DPR -> BGSM */
349 resource
= new_resource(dev
, index
++);
350 resource
->base
= mc_values
[TSEG_REG
] - dpr_size
;
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);
380 chromeos_reserve_ram_oops(dev
, index
++);
383 static void systemagent_read_resources(device_t dev
)
385 /* Read standard PCI resources. */
386 pci_dev_read_resources(dev
);
388 /* Add all fixed MMIO resources. */
389 mc_add_fixed_mmio_resources(dev
);
391 /* Calculate and add DRAM resources. */
392 mc_add_dram_resources(dev
);
395 static void systemagent_init(struct device
*dev
)
397 u8 bios_reset_cpl
, pair
;
399 /* Enable Power Aware Interrupt Routing */
400 pair
= MCHBAR8(MCH_PAIR
);
401 pair
&= ~0x7; /* Clear 2:0 */
402 pair
|= 0x4; /* Fixed Priority */
403 MCHBAR8(MCH_PAIR
) = pair
;
406 * Set bits 0+1 of BIOS_RESET_CPL to indicate to the CPU
407 * that BIOS has initialized memory and power management
409 bios_reset_cpl
= MCHBAR8(BIOS_RESET_CPL
);
411 MCHBAR8(BIOS_RESET_CPL
) = bios_reset_cpl
;
412 printk(BIOS_DEBUG
, "Set BIOS_RESET_CPL\n");
414 /* Configure turbo power limits 1ms after reset complete bit */
416 set_power_limits(28);
419 static struct device_operations systemagent_ops
= {
420 .read_resources
= &systemagent_read_resources
,
421 .acpi_fill_ssdt_generator
= &generate_cpu_entries
,
422 .set_resources
= &pci_dev_set_resources
,
423 .enable_resources
= &pci_dev_enable_resources
,
424 .init
= &systemagent_init
,
425 .ops_pci
= &broadwell_pci_ops
,
428 static const unsigned short systemagent_ids
[] = {
429 0x0a04, /* Haswell ULT */
430 0x1604, /* Broadwell-U/Y */
431 0x1610, /* Broadwell-H Desktop */
432 0x1614, /* Broadwell-H Mobile */
436 static const struct pci_driver systemagent_driver __pci_driver
= {
437 .ops
= &systemagent_ops
,
438 .vendor
= PCI_VENDOR_ID_INTEL
,
439 .devices
= systemagent_ids