blob: 35ec41da46ce44ae78d911fa488c723763a0ca9a [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi2efc8802012-11-06 11:03:53 +01002
Kyösti Mälkkief844012013-06-25 23:17:43 +03003// Use simple device model for this file even in ramstage
4#define __SIMPLE_DEVICE__
5
Kyösti Mälkkia963acd2019-08-16 20:34:25 +03006#include <arch/romstage.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +01008#include <device/pci_def.h>
9#include <console/console.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030010#include <cpu/x86/mtrr.h>
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030011#include <cpu/x86/smm.h>
Kyösti Mälkkidcb688e2013-09-04 01:11:16 +030012#include <cbmem.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>
Elyes HAOUAS030d3382021-02-12 08:17:35 +010015#include <types.h>
16
Patrick Georgi2efc8802012-11-06 11:03:53 +010017#include "gm45.h"
18
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010019/*
20 * Decodes used Graphics Mode Select (GMS) to kilobytes.
21 * The options for 1M, 4M, 8M and 16M preallocated igd memory are
22 * undocumented but are verified to work.
23 */
Patrick Georgi2efc8802012-11-06 11:03:53 +010024u32 decode_igd_memory_size(const u32 gms)
25{
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010026 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64, 128, 256,
27 96, 160, 224, 352 };
28
Jacob Garberf74f6cb2019-04-08 17:54:35 -060029 if (gms >= ARRAY_SIZE(ggc2uma))
Patrick Georgi2efc8802012-11-06 11:03:53 +010030 die("Bad Graphics Mode Select (GMS) setting.\n");
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010031
32 return ggc2uma[gms] << 10;
Patrick Georgi2efc8802012-11-06 11:03:53 +010033}
34
35/** Decodes used Graphics Stolen Memory (GSM) to kilobytes. */
36u32 decode_igd_gtt_size(const u32 gsm)
37{
38 switch (gsm) {
39 case 0:
40 return 0 << 10;
41 case 1:
42 return 1 << 10;
43 case 3:
44 case 9:
45 return 2 << 10;
46 case 10:
47 return 3 << 10;
48 case 11:
49 return 4 << 10;
50 default:
51 die("Bad Graphics Stolen Memory (GSM) setting.\n");
52 return 0;
53 }
54}
55
Arthur Heymans8b766052018-01-24 23:25:13 +010056/* Decodes TSEG region size to kilobytes. */
57u32 decode_tseg_size(u8 esmramc)
58{
59 if (!(esmramc & 1))
60 return 0;
61 switch ((esmramc >> 1) & 3) {
62 case 0:
63 return 1 << 10;
64 case 1:
65 return 2 << 10;
66 case 2:
67 return 8 << 10;
68 case 3:
69 default:
70 die("Bad TSEG setting.\n");
71 }
72}
73
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030074static uintptr_t northbridge_get_tseg_base(void)
Patrick Georgi2efc8802012-11-06 11:03:53 +010075{
Kyösti Mälkki3f9a62e2013-06-20 20:25:21 +030076 const pci_devfn_t dev = PCI_DEV(0, 0, 0);
Patrick Georgi2efc8802012-11-06 11:03:53 +010077
78 u32 tor;
79
80 /* Top of Lower Usable DRAM */
81 tor = (pci_read_config16(dev, D0F0_TOLUD) & 0xfff0) << 16;
82
83 /* Graphics memory comes next */
84 const u32 ggc = pci_read_config16(dev, D0F0_GGC);
Arthur Heymans8b766052018-01-24 23:25:13 +010085 const u8 esmramc = pci_read_config8(dev, D0F0_ESMRAMC);
Patrick Georgi2efc8802012-11-06 11:03:53 +010086 if (!(ggc & 2)) {
87 /* Graphics memory */
88 tor -= decode_igd_memory_size((ggc >> 4) & 0xf) << 10;
89 /* GTT Graphics Stolen Memory Size (GGMS) */
90 tor -= decode_igd_gtt_size((ggc >> 8) & 0xf) << 10;
91 }
Arthur Heymans8b766052018-01-24 23:25:13 +010092 /* TSEG size */
93 tor -= decode_tseg_size(esmramc) << 10;
Patrick Georgi2efc8802012-11-06 11:03:53 +010094 return tor;
95}
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020096
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030097static size_t northbridge_get_tseg_size(void)
Arthur Heymans009518e2018-11-27 14:06:21 +010098{
99 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC);
100 return decode_tseg_size(esmramc) << 10;
101}
102
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300103/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +0200104 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300105 * CBMEM top downwards to 4 MiB boundary.
106 */
Elyes Haouas799c3212022-11-09 14:00:44 +0100107uintptr_t cbmem_top_chipset(void)
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200108{
Elyes Haouas799c3212022-11-09 14:00:44 +0100109 return ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200110}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300111
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300112void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300113{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300114 *start = northbridge_get_tseg_base();
115 *size = northbridge_get_tseg_size();
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300116}
117
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300118void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300119{
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300120 uintptr_t top_of_ram;
121
Elyes HAOUASef906092020-02-20 19:41:17 +0100122 /* Cache 8 MiB region below the top of RAM and 2 MiB above top of
123 * RAM to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300124 */
125 top_of_ram = (uintptr_t)cbmem_top();
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300126 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB,
Arthur Heymans009518e2018-11-27 14:06:21 +0100127 MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300128 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymans009518e2018-11-27 14:06:21 +0100129 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300130
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300131}