blob: c92e4662d682f75e0cc525bbc25f49e58e790f1f [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03002
3// Use simple device model for this file even in ramstage
4#define __SIMPLE_DEVICE__
5
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Kyösti Mälkkia963acd2019-08-16 20:34:25 +03007#include <arch/romstage.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03008#include <cbmem.h>
9#include "i945.h"
Arthur Heymans874a8f92016-05-19 16:06:09 +020010#include <console/console.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030011#include <cpu/x86/mtrr.h>
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030012#include <cpu/x86/smm.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030013#include <program_loading.h>
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +030014#include <cpu/intel/smm_reloc.h>
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030015#include <stdint.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030016
Arthur Heymansf6d14772018-01-26 11:50:04 +010017/* Decodes TSEG region size to bytes. */
18u32 decode_tseg_size(const u8 esmramc)
19{
20 if (!(esmramc & 1))
21 return 0;
22 switch ((esmramc >> 1) & 3) {
23 case 0:
24 return 1 << 20;
25 case 1:
26 return 2 << 20;
27 case 2:
28 return 8 << 20;
29 case 3:
30 default:
31 die("Bad TSEG setting.\n");
32 }
33}
34
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030035static uintptr_t northbridge_get_tseg_base(void)
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030036{
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020037 uintptr_t tom;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030038
Angel Pons3580d812020-06-11 14:13:33 +020039 if (pci_read_config8(HOST_BRIDGE, DEVEN) & (DEVEN_D2F0 | DEVEN_D2F1))
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030040 /* IGD enabled, get top of Memory from BSM register */
Angel Pons3580d812020-06-11 14:13:33 +020041 tom = pci_read_config32(IGD_DEV, BSM);
Arthur Heymans70a8e342017-03-09 11:30:23 +010042 else
Angel Pons3580d812020-06-11 14:13:33 +020043 tom = (pci_read_config8(HOST_BRIDGE, TOLUD) & 0xf7) << 24;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030044
Elyes HAOUAS8cb5ea72019-12-05 14:33:07 +010045 /* subtract TSEG size */
Angel Pons3580d812020-06-11 14:13:33 +020046 tom -= decode_tseg_size(pci_read_config8(HOST_BRIDGE, ESMRAMC));
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020047 return tom;
48}
49
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030050static size_t northbridge_get_tseg_size(void)
Arthur Heymanscf3076e2018-04-10 12:57:42 +020051{
Angel Pons3580d812020-06-11 14:13:33 +020052 const u8 esmramc = pci_read_config8(HOST_BRIDGE, ESMRAMC);
Arthur Heymanscf3076e2018-04-10 12:57:42 +020053 return decode_tseg_size(esmramc);
54}
55
56/*
57 * Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +020058 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +030059 * CBMEM top downwards to 4 MiB boundary.
60 */
Arthur Heymans340e4b82019-10-23 17:25:58 +020061void *cbmem_top_chipset(void)
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020062{
Arthur Heymanscf3076e2018-04-10 12:57:42 +020063 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkki811932a2016-07-22 22:53:19 +030064 return (void *) top_of_ram;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030065}
Arthur Heymans874a8f92016-05-19 16:06:09 +020066
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020067/* Decodes used Graphics Mode Select (GMS) to kilobytes. */
Arthur Heymans874a8f92016-05-19 16:06:09 +020068u32 decode_igd_memory_size(const u32 gms)
69{
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020070 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64 };
Arthur Heymans874a8f92016-05-19 16:06:09 +020071
Jacob Garberf74f6cb2019-04-08 17:54:35 -060072 if (gms >= ARRAY_SIZE(ggc2uma))
Arthur Heymans874a8f92016-05-19 16:06:09 +020073 die("Bad Graphics Mode Select (GMS) setting.\n");
74
75 return ggc2uma[gms] << 10;
76}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030077
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030078void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030079{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030080 *start = northbridge_get_tseg_base();
81 *size = northbridge_get_tseg_size();
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030082}
83
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030084void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030085{
Kyösti Mälkki823020d2016-07-22 22:53:19 +030086 uintptr_t top_of_ram;
87
Elyes HAOUASef906092020-02-20 19:41:17 +010088 /* Cache 8 MiB region below the top of RAM and 2 MiB above top of
89 * RAM to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +030090 */
91 top_of_ram = (uintptr_t)cbmem_top();
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020092 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB, MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030093 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymanscf3076e2018-04-10 12:57:42 +020094 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030095}