blob: ac95ab5683b88695a7e49cc6ce71111fcd8a2bbf [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03002
3#define __SIMPLE_DEVICE__
4
Kyösti Mälkkia963acd2019-08-16 20:34:25 +03005#include <arch/romstage.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03007#include <cbmem.h>
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +03008#include <cpu/intel/smm_reloc.h>
Kyösti Mälkkibfca6702016-07-22 22:48:35 +03009#include <cpu/x86/mtrr.h>
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030010#include <cpu/x86/smm.h>
Kyösti Mälkkibfca6702016-07-22 22:48:35 +030011#include <program_loading.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030012#include "sandybridge.h"
Michał Żygowskiede87182021-11-21 11:53:42 +010013#include <security/intel/txt/txt_platform.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020014#include <stddef.h>
15#include <stdint.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030016
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030017static uintptr_t northbridge_get_tseg_base(void)
Kyösti Mälkkif6c20682019-08-02 06:14:50 +030018{
Angel Pons63c0dc92020-10-01 20:23:18 +020019 /* TSEG has 1 MiB granularity, and bit 0 is a lock */
20 return ALIGN_DOWN(pci_read_config32(HOST_BRIDGE, TSEGMB), 1 * MiB);
Kyösti Mälkkif6c20682019-08-02 06:14:50 +030021}
22
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030023static size_t northbridge_get_tseg_size(void)
Kyösti Mälkkif6c20682019-08-02 06:14:50 +030024{
25 return CONFIG_SMM_TSEG_SIZE;
26}
27
Michał Żygowskiede87182021-11-21 11:53:42 +010028union dpr_register txt_get_chipset_dpr(void)
29{
30 return (union dpr_register) { .raw = pci_read_config32(HOST_BRIDGE, DPR) };
31}
32
33/*
34 * Return the topmost memory address below 4 GiB available for general
35 * use, from software's view of memory. Do not confuse this with TOLUD,
36 * which applies to the DRAM as viewed by the memory controller itself.
37 */
38static uintptr_t top_of_low_usable_memory(void)
39{
40 /*
41 * Base of DPR is top of usable DRAM below 4 GiB. However, DPR
42 * may not always be enabled. Unlike most memory map registers,
43 * the DPR register stores top of DPR instead of its base address.
44 * Top of DPR is R/O, and mirrored from TSEG base by hardware.
45 */
46 uintptr_t tolum = northbridge_get_tseg_base();
47
48 const union dpr_register dpr = txt_get_chipset_dpr();
49
50 /* Subtract DMA Protected Range size if enabled */
51 if (dpr.epm)
52 tolum -= dpr.size * MiB;
53
54 return tolum;
55}
56
Elyes Haouas799c3212022-11-09 14:00:44 +010057uintptr_t cbmem_top_chipset(void)
Angel Pons63c0dc92020-10-01 20:23:18 +020058{
Elyes Haouas799c3212022-11-09 14:00:44 +010059 return top_of_low_usable_memory();
Angel Pons63c0dc92020-10-01 20:23:18 +020060}
61
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030062void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkif6c20682019-08-02 06:14:50 +030063{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030064 *start = northbridge_get_tseg_base();
Angel Pons7c49cb82020-03-16 23:17:32 +010065 *size = northbridge_get_tseg_size();
Kyösti Mälkkif6c20682019-08-02 06:14:50 +030066}
67
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030068void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030069{
Angel Pons7c49cb82020-03-16 23:17:32 +010070 uintptr_t top_of_ram = (uintptr_t)cbmem_top();
Kyösti Mälkkibfca6702016-07-22 22:48:35 +030071
Angel Pons7c49cb82020-03-16 23:17:32 +010072 /*
73 * Cache 8MiB below the top of ram. On sandybridge systems the top of
Elyes HAOUASef906092020-02-20 19:41:17 +010074 * RAM under 4GiB is the start of the TSEG region. It is required to
Kyösti Mälkkibfca6702016-07-22 22:48:35 +030075 * be 8MiB aligned. Set this area as cacheable so it can be used later
Angel Pons7c49cb82020-03-16 23:17:32 +010076 * for ramstage before setting up the entire RAM as cacheable.
77 */
78 postcar_frame_add_mtrr(pcf, top_of_ram - 8 * MiB, 8 * MiB, MTRR_TYPE_WRBACK);
Kyösti Mälkkibfca6702016-07-22 22:48:35 +030079
Angel Pons7c49cb82020-03-16 23:17:32 +010080 /*
81 * Cache 8MiB at the top of ram. Top of RAM on sandybridge systems
Kyösti Mälkkibfca6702016-07-22 22:48:35 +030082 * is where the TSEG region resides. However, it is not restricted
83 * to SMM mode until SMM has been relocated. By setting the region
84 * to cacheable it provides faster access when relocating the SMM
Angel Pons7c49cb82020-03-16 23:17:32 +010085 * handler as well as using the TSEG region for other purposes.
86 */
87 postcar_frame_add_mtrr(pcf, top_of_ram, 8 * MiB, MTRR_TYPE_WRBACK);
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030088}