blob: 5af3e1603715ab6b2310f06f7765027625a35b36 [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älkki823020d2016-07-22 22:53:19 +030021#include <arch/cpu.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +010022#include <arch/io.h>
Patrick Georgi2efc8802012-11-06 11:03:53 +010023#include <device/pci_def.h>
24#include <console/console.h>
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030025#include <cpu/intel/romstage.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030026#include <cpu/x86/mtrr.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>
Patrick Georgi2efc8802012-11-06 11:03:53 +010029#include "gm45.h"
30
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010031/*
32 * Decodes used Graphics Mode Select (GMS) to kilobytes.
33 * The options for 1M, 4M, 8M and 16M preallocated igd memory are
34 * undocumented but are verified to work.
35 */
Patrick Georgi2efc8802012-11-06 11:03:53 +010036u32 decode_igd_memory_size(const u32 gms)
37{
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010038 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64, 128, 256,
39 96, 160, 224, 352 };
40
41 if (gms > ARRAY_SIZE(ggc2uma))
Patrick Georgi2efc8802012-11-06 11:03:53 +010042 die("Bad Graphics Mode Select (GMS) setting.\n");
Arthur Heymanseeaf9e42016-11-12 20:13:07 +010043
44 return ggc2uma[gms] << 10;
Patrick Georgi2efc8802012-11-06 11:03:53 +010045}
46
47/** Decodes used Graphics Stolen Memory (GSM) to kilobytes. */
48u32 decode_igd_gtt_size(const u32 gsm)
49{
50 switch (gsm) {
51 case 0:
52 return 0 << 10;
53 case 1:
54 return 1 << 10;
55 case 3:
56 case 9:
57 return 2 << 10;
58 case 10:
59 return 3 << 10;
60 case 11:
61 return 4 << 10;
62 default:
63 die("Bad Graphics Stolen Memory (GSM) setting.\n");
64 return 0;
65 }
66}
67
Arthur Heymans8b766052018-01-24 23:25:13 +010068/* Decodes TSEG region size to kilobytes. */
69u32 decode_tseg_size(u8 esmramc)
70{
71 if (!(esmramc & 1))
72 return 0;
73 switch ((esmramc >> 1) & 3) {
74 case 0:
75 return 1 << 10;
76 case 1:
77 return 2 << 10;
78 case 2:
79 return 8 << 10;
80 case 3:
81 default:
82 die("Bad TSEG setting.\n");
83 }
84}
85
Arthur Heymansaade90e2018-01-25 00:33:45 +010086uintptr_t smm_region_start(void)
Patrick Georgi2efc8802012-11-06 11:03:53 +010087{
Kyösti Mälkki3f9a62e2013-06-20 20:25:21 +030088 const pci_devfn_t dev = PCI_DEV(0, 0, 0);
Patrick Georgi2efc8802012-11-06 11:03:53 +010089
90 u32 tor;
91
92 /* Top of Lower Usable DRAM */
93 tor = (pci_read_config16(dev, D0F0_TOLUD) & 0xfff0) << 16;
94
95 /* Graphics memory comes next */
96 const u32 ggc = pci_read_config16(dev, D0F0_GGC);
Arthur Heymans8b766052018-01-24 23:25:13 +010097 const u8 esmramc = pci_read_config8(dev, D0F0_ESMRAMC);
Patrick Georgi2efc8802012-11-06 11:03:53 +010098 if (!(ggc & 2)) {
99 /* Graphics memory */
100 tor -= decode_igd_memory_size((ggc >> 4) & 0xf) << 10;
101 /* GTT Graphics Stolen Memory Size (GGMS) */
102 tor -= decode_igd_gtt_size((ggc >> 8) & 0xf) << 10;
103 }
Arthur Heymans8b766052018-01-24 23:25:13 +0100104 /* TSEG size */
105 tor -= decode_tseg_size(esmramc) << 10;
Patrick Georgi2efc8802012-11-06 11:03:53 +0100106 return tor;
107}
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200108
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300109/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +0200110 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300111 * CBMEM top downwards to 4 MiB boundary.
112 */
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200113void *cbmem_top(void)
114{
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300115 uintptr_t top_of_ram = ALIGN_DOWN(smm_region_start(), 4*MiB);
116 return (void *) top_of_ram;
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +0200117}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300118
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300119#define ROMSTAGE_RAM_STACK_SIZE 0x5000
120
Arthur Heymans3a4edb62018-06-03 12:42:10 +0200121/* platform_enter_postcar() determines the stack to use after
122 * cache-as-ram is torn down as well as the MTRR settings to use,
123 * and continues execution in postcar stage. */
124void platform_enter_postcar(void)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300125{
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300126 struct postcar_frame pcf;
127 uintptr_t top_of_ram;
128
129 if (postcar_frame_init(&pcf, ROMSTAGE_RAM_STACK_SIZE))
130 die("Unable to initialize postcar frame.\n");
131
132 /* Cache the ROM as WP just below 4GiB. */
Nico Huber089b9082018-05-27 14:37:32 +0200133 postcar_frame_add_romcache(&pcf, MTRR_TYPE_WRPROT);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300134
135 /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */
136 postcar_frame_add_mtrr(&pcf, 0, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK);
137
Arthur Heymansaade90e2018-01-25 00:33:45 +0100138 /* Cache a 8 MiB region below the top of ram and 8 MiB above top of
139 * ram to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300140 */
141 top_of_ram = (uintptr_t)cbmem_top();
Arthur Heymansaade90e2018-01-25 00:33:45 +0100142 postcar_frame_add_mtrr(&pcf, top_of_ram - 8*MiB, 16*MiB,
143 MTRR_TYPE_WRBACK);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300144
Arthur Heymans3a4edb62018-06-03 12:42:10 +0200145 run_postcar_phase(&pcf);
146
147 /* We do not return here. */
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300148}