blob: 6170e41a3cf86cabd4d3d473e6e014562209d6a9 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Damien Zammit43a1f782015-08-19 15:16:59 +10002
3#define __SIMPLE_DEVICE__
4
Damien Zammit5680faf2016-01-22 22:12:30 +11005#include <cbmem.h>
Damien Zammit43a1f782015-08-19 15:16:59 +10006#include <commonlib/helpers.h>
Kyösti Mälkkia963acd2019-08-16 20:34:25 +03007#include <arch/romstage.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02008#include <device/pci_ops.h>
Damien Zammit43a1f782015-08-19 15:16:59 +10009#include <device/pci_def.h>
10#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>
Damien Zammit43a1f782015-08-19 15:16:59 +100013#include <northbridge/intel/x4x/x4x.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030014#include <program_loading.h>
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +030015#include <cpu/intel/smm_reloc.h>
Elyes HAOUAS030d3382021-02-12 08:17:35 +010016#include <types.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100017
18/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
19u32 decode_igd_memory_size(const u32 gms)
20{
Arthur Heymans27f94ee2016-06-18 21:08:58 +020021 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16,
Damien Zammit43a1f782015-08-19 15:16:59 +100022 32, 48, 64, 128, 256, 96, 160, 224, 352 };
23
Jacob Garberf74f6cb2019-04-08 17:54:35 -060024 if (gms >= ARRAY_SIZE(ggc2uma))
Damien Zammit43a1f782015-08-19 15:16:59 +100025 die("Bad Graphics Mode Select (GMS) setting.\n");
26
27 return ggc2uma[gms] << 10;
28}
29
30/** Decodes used GTT Graphics Memory Size (GGMS) to kilobytes. */
31u32 decode_igd_gtt_size(const u32 gsm)
32{
33 static const u8 ggc2gtt[] = { 0, 1, 0, 2, 0, 0, 0, 0, 0, 2, 3, 4};
34
Jacob Garberf74f6cb2019-04-08 17:54:35 -060035 if (gsm >= ARRAY_SIZE(ggc2gtt))
Damien Zammit43a1f782015-08-19 15:16:59 +100036 die("Bad GTT Graphics Memory Size (GGMS) setting.\n");
37
38 return ggc2gtt[gsm] << 10;
39}
40
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020041/** Decodes used TSEG size to bytes. */
42u32 decode_tseg_size(const u32 esmramc)
43{
44 if (!(esmramc & 1))
45 return 0;
46
47 switch ((esmramc >> 1) & 3) {
48 case 0:
49 return 1 << 20;
50 case 1:
51 return 2 << 20;
52 case 2:
53 return 8 << 20;
54 case 3:
55 default:
56 die("Bad TSEG setting.\n");
57 }
58}
59
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030060static size_t northbridge_get_tseg_size(void)
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020061{
Angel Ponsd1c590a2020-08-03 16:01:39 +020062 const u8 esmramc = pci_read_config8(HOST_BRIDGE, D0F0_ESMRAMC);
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020063 return decode_tseg_size(esmramc);
64}
65
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030066static uintptr_t northbridge_get_tseg_base(void)
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020067{
Angel Ponsd1c590a2020-08-03 16:01:39 +020068 return pci_read_config32(HOST_BRIDGE, D0F0_TSEG);
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020069}
70
Kyösti Mälkki811932a2016-07-22 22:53:19 +030071/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +020072 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +030073 * CBMEM top downwards to 4 MiB boundary.
74 */
Arthur Heymans340e4b82019-10-23 17:25:58 +020075void *cbmem_top_chipset(void)
Damien Zammit5680faf2016-01-22 22:12:30 +110076{
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020077 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkki811932a2016-07-22 22:53:19 +030078 return (void *) top_of_ram;
Damien Zammit5680faf2016-01-22 22:12:30 +110079}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030080
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030081void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030082{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030083 *start = northbridge_get_tseg_base();
84 *size = northbridge_get_tseg_size();
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030085}
86
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030087void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030088{
Kyösti Mälkki823020d2016-07-22 22:53:19 +030089 uintptr_t top_of_ram;
90
Elyes HAOUASef906092020-02-20 19:41:17 +010091 /* Cache 8 MiB region below the top of RAM and 2 MiB above top of
92 * RAM to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +030093 */
94 top_of_ram = (uintptr_t)cbmem_top();
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030095 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB,
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020096 MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030097 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020098 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030099}