blob: 7479a7834acb1c3dd9c07dca3f0b0faf68ac88e6 [file] [log] [blame]
Patrick Georgi2efc8802012-11-06 11:03:53 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
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.
Patrick Georgi2efc8802012-11-06 11:03:53 +010015 */
16
Kyösti Mälkkief844012013-06-25 23:17:43 +030017// Use simple device model for this file even in ramstage
18#define __SIMPLE_DEVICE__
19
Patrick Georgi2efc8802012-11-06 11:03:53 +010020#include <stdint.h>
Kyösti Mälkkia963acd2019-08-16 20:34:25 +030021#include <arch/romstage.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020022#include <device/pci_ops.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +010023#include <device/pci_def.h>
24#include <console/console.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030025#include <cpu/x86/mtrr.h>
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030026#include <cpu/x86/smm.h>
Kyösti Mälkkidcb688e2013-09-04 01:11:16 +030027#include <cbmem.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030028#include <program_loading.h>
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +030029#include <cpu/intel/smm_reloc.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +010030#include "gm45.h"
31
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010032/*
33 * Decodes used Graphics Mode Select (GMS) to kilobytes.
34 * The options for 1M, 4M, 8M and 16M preallocated igd memory are
35 * undocumented but are verified to work.
36 */
Patrick Georgi2efc8802012-11-06 11:03:53 +010037u32 decode_igd_memory_size(const u32 gms)
38{
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010039 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64, 128, 256,
40 96, 160, 224, 352 };
41
Jacob Garberf74f6cb2019-04-08 17:54:35 -060042 if (gms >= ARRAY_SIZE(ggc2uma))
Patrick Georgi2efc8802012-11-06 11:03:53 +010043 die("Bad Graphics Mode Select (GMS) setting.\n");
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010044
45 return ggc2uma[gms] << 10;
Patrick Georgi2efc8802012-11-06 11:03:53 +010046}
47
48/** Decodes used Graphics Stolen Memory (GSM) to kilobytes. */
49u32 decode_igd_gtt_size(const u32 gsm)
50{
51 switch (gsm) {
52 case 0:
53 return 0 << 10;
54 case 1:
55 return 1 << 10;
56 case 3:
57 case 9:
58 return 2 << 10;
59 case 10:
60 return 3 << 10;
61 case 11:
62 return 4 << 10;
63 default:
64 die("Bad Graphics Stolen Memory (GSM) setting.\n");
65 return 0;
66 }
67}
68
Arthur Heymans8b766052018-01-24 23:25:13 +010069/* Decodes TSEG region size to kilobytes. */
70u32 decode_tseg_size(u8 esmramc)
71{
72 if (!(esmramc & 1))
73 return 0;
74 switch ((esmramc >> 1) & 3) {
75 case 0:
76 return 1 << 10;
77 case 1:
78 return 2 << 10;
79 case 2:
80 return 8 << 10;
81 case 3:
82 default:
83 die("Bad TSEG setting.\n");
84 }
85}
86
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030087static uintptr_t northbridge_get_tseg_base(void)
Patrick Georgi2efc8802012-11-06 11:03:53 +010088{
Kyösti Mälkki3f9a62e2013-06-20 20:25:21 +030089 const pci_devfn_t dev = PCI_DEV(0, 0, 0);
Patrick Georgi2efc8802012-11-06 11:03:53 +010090
91 u32 tor;
92
93 /* Top of Lower Usable DRAM */
94 tor = (pci_read_config16(dev, D0F0_TOLUD) & 0xfff0) << 16;
95
96 /* Graphics memory comes next */
97 const u32 ggc = pci_read_config16(dev, D0F0_GGC);
Arthur Heymans8b766052018-01-24 23:25:13 +010098 const u8 esmramc = pci_read_config8(dev, D0F0_ESMRAMC);
Patrick Georgi2efc8802012-11-06 11:03:53 +010099 if (!(ggc & 2)) {
100 /* Graphics memory */
101 tor -= decode_igd_memory_size((ggc >> 4) & 0xf) << 10;
102 /* GTT Graphics Stolen Memory Size (GGMS) */
103 tor -= decode_igd_gtt_size((ggc >> 8) & 0xf) << 10;
104 }
Arthur Heymans8b766052018-01-24 23:25:13 +0100105 /* TSEG size */
106 tor -= decode_tseg_size(esmramc) << 10;
Patrick Georgi2efc8802012-11-06 11:03:53 +0100107 return tor;
108}
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200109
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300110static size_t northbridge_get_tseg_size(void)
Arthur Heymans009518e2018-11-27 14:06:21 +0100111{
112 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC);
113 return decode_tseg_size(esmramc) << 10;
114}
115
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300116/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +0200117 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300118 * CBMEM top downwards to 4 MiB boundary.
119 */
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200120void *cbmem_top(void)
121{
Arthur Heymans009518e2018-11-27 14:06:21 +0100122 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300123 return (void *) top_of_ram;
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200124}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300125
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300126void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300127{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +0300128 *start = northbridge_get_tseg_base();
129 *size = northbridge_get_tseg_size();
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300130}
131
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300132void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300133{
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300134 uintptr_t top_of_ram;
135
Arthur Heymans009518e2018-11-27 14:06:21 +0100136 /* Cache 8 MiB region below the top of ram and 2 MiB above top of
Arthur Heymansaade90e2018-01-25 00:33:45 +0100137 * ram to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300138 */
139 top_of_ram = (uintptr_t)cbmem_top();
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300140 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB,
Arthur Heymans009518e2018-11-27 14:06:21 +0100141 MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300142 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymans009518e2018-11-27 14:06:21 +0100143 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300144
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300145}