blob: 6484326e5730e7fe8c4c0cbbb6498f631aa7b0f2 [file] [log] [blame]
Damien Zammit43a1f782015-08-19 15:16:59 +10001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 secunet Security Networks AG
5 * Copyright (C) 2015 Damien Zammit <damien@zamaudio.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
10 * the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#define __SIMPLE_DEVICE__
19
Damien Zammit5680faf2016-01-22 22:12:30 +110020#include <cbmem.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100021#include <commonlib/helpers.h>
22#include <stdint.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030023#include <arch/cpu.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100024#include <arch/io.h>
25#include <device/pci_def.h>
26#include <console/console.h>
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030027#include <cpu/intel/romstage.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030028#include <cpu/x86/mtrr.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100029#include <northbridge/intel/x4x/x4x.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030030#include <program_loading.h>
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020031#include <cpu/intel/smm/gen1/smi.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100032
33/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
34u32 decode_igd_memory_size(const u32 gms)
35{
Arthur Heymans27f94ee2016-06-18 21:08:58 +020036 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16,
Damien Zammit43a1f782015-08-19 15:16:59 +100037 32, 48, 64, 128, 256, 96, 160, 224, 352 };
38
39 if (gms > ARRAY_SIZE(ggc2uma))
40 die("Bad Graphics Mode Select (GMS) setting.\n");
41
42 return ggc2uma[gms] << 10;
43}
44
45/** Decodes used GTT Graphics Memory Size (GGMS) to kilobytes. */
46u32 decode_igd_gtt_size(const u32 gsm)
47{
48 static const u8 ggc2gtt[] = { 0, 1, 0, 2, 0, 0, 0, 0, 0, 2, 3, 4};
49
50 if (gsm > ARRAY_SIZE(ggc2gtt))
51 die("Bad GTT Graphics Memory Size (GGMS) setting.\n");
52
53 return ggc2gtt[gsm] << 10;
54}
55
Arthur Heymans4c65bfc2018-04-10 13:34:24 +020056/** Decodes used TSEG size to bytes. */
57u32 decode_tseg_size(const u32 esmramc)
58{
59 if (!(esmramc & 1))
60 return 0;
61
62 switch ((esmramc >> 1) & 3) {
63 case 0:
64 return 1 << 20;
65 case 1:
66 return 2 << 20;
67 case 2:
68 return 8 << 20;
69 case 3:
70 default:
71 die("Bad TSEG setting.\n");
72 }
73}
74
Damien Zammit43a1f782015-08-19 15:16:59 +100075u8 decode_pciebar(u32 *const base, u32 *const len)
76{
77 *base = 0;
78 *len = 0;
Arthur Heymans70a1dda2017-03-09 01:58:24 +010079 const pci_devfn_t dev = PCI_DEV(0, 0, 0);
Damien Zammit43a1f782015-08-19 15:16:59 +100080 u32 pciexbar = 0;
81 u32 pciexbar_reg;
82 u32 reg32;
83 int max_buses;
84 const struct {
85 u16 num_buses;
86 u32 addr_mask;
87 } busmask[] = {
88 {256, 0xf0000000},
89 {128, 0xf8000000},
90 {64, 0xfc000000},
91 {0, 0},
92 };
93
94 pciexbar_reg = pci_read_config32(dev, D0F0_PCIEXBAR_LO);
95
96 if (!(pciexbar_reg & 1)) {
97 printk(BIOS_WARNING, "WARNING: MMCONF not set\n");
98 return 0;
99 }
100
101 reg32 = (pciexbar_reg >> 1) & 3;
102 pciexbar = pciexbar_reg & busmask[reg32].addr_mask;
103 max_buses = busmask[reg32].num_buses;
104
105 if (!pciexbar) {
106 printk(BIOS_WARNING, "WARNING: pciexbar invalid\n");
107 return 0;
108 }
109
110 *base = pciexbar;
111 *len = max_buses << 20;
112 return 1;
113}
Damien Zammit5680faf2016-01-22 22:12:30 +1100114
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200115u32 northbridge_get_tseg_size(void)
116{
117 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC);
118 return decode_tseg_size(esmramc);
119}
120
121u32 northbridge_get_tseg_base(void)
122{
123 return pci_read_config32(PCI_DEV(0, 0, 0), D0F0_TSEG);
124}
125
126
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300127/* Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +0200128 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300129 * CBMEM top downwards to 4 MiB boundary.
130 */
Damien Zammit5680faf2016-01-22 22:12:30 +1100131void *cbmem_top(void)
132{
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200133 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkki811932a2016-07-22 22:53:19 +0300134 return (void *) top_of_ram;
Damien Zammit5680faf2016-01-22 22:12:30 +1100135}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300136
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300137#define ROMSTAGE_RAM_STACK_SIZE 0x5000
138
Arthur Heymans4ff675e2018-06-03 10:49:11 +0200139/* platform_enter_postcar() determines the stack to use after
140 * cache-as-ram is torn down as well as the MTRR settings to use,
141 * and continues execution in postcar stage. */
142void platform_enter_postcar(void)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300143{
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300144 struct postcar_frame pcf;
145 uintptr_t top_of_ram;
146
147 if (postcar_frame_init(&pcf, ROMSTAGE_RAM_STACK_SIZE))
148 die("Unable to initialize postcar frame.\n");
149
150 /* Cache the ROM as WP just below 4GiB. */
Nico Huber089b9082018-05-27 14:37:32 +0200151 postcar_frame_add_romcache(&pcf, MTRR_TYPE_WRPROT);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300152
153 /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */
154 postcar_frame_add_mtrr(&pcf, 0, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK);
155
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200156 /* Cache 8 MiB region below the top of ram and 2 MiB above top of
157 * ram to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300158 */
159 top_of_ram = (uintptr_t)cbmem_top();
Arthur Heymans4c65bfc2018-04-10 13:34:24 +0200160 postcar_frame_add_mtrr(&pcf, top_of_ram - 8*MiB, 8*MiB,
161 MTRR_TYPE_WRBACK);
162 postcar_frame_add_mtrr(&pcf, northbridge_get_tseg_base(),
163 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300164
Arthur Heymans4ff675e2018-06-03 10:49:11 +0200165 run_postcar_phase(&pcf);
166
167 /* We do not return here. */
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300168}